blob: a8e4c5adade72a05d0d19c842f4aa1afb0563123 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelb6c02712008-06-26 21:27:53 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -040022#include <linux/acpi.h>
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -040023#include <linux/amba/bus.h>
Wan Zongshun0076cd32016-05-10 09:21:01 -040024#include <linux/platform_device.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020025#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080026#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010028#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020029#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090030#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020031#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010032#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020033#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020034#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010035#include <linux/notifier.h>
36#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020037#include <linux/irq.h>
38#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020039#include <linux/dma-contiguous.h>
Jiang Liu7c71d302015-04-13 14:11:33 +080040#include <linux/irqdomain.h>
Joerg Roedel5f6bed52015-12-22 13:34:22 +010041#include <linux/percpu.h>
Joerg Roedel307d5852016-07-05 11:54:04 +020042#include <linux/iova.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020043#include <asm/irq_remapping.h>
44#include <asm/io_apic.h>
45#include <asm/apic.h>
46#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020047#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020048#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090049#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010050#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020051#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020052
53#include "amd_iommu_proto.h"
54#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020055#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020056
57#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
58
Joerg Roedel815b33f2011-04-06 17:26:49 +020059#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020060
Joerg Roedel307d5852016-07-05 11:54:04 +020061/* IO virtual address start page frame number */
62#define IOVA_START_PFN (1)
63#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
64#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
65
Joerg Roedel81cd07b2016-07-07 18:01:10 +020066/* Reserved IOVA ranges */
67#define MSI_RANGE_START (0xfee00000)
68#define MSI_RANGE_END (0xfeefffff)
69#define HT_RANGE_START (0xfd00000000ULL)
70#define HT_RANGE_END (0xffffffffffULL)
71
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020072/*
73 * This bitmap is used to advertise the page sizes our hardware support
74 * to the IOMMU core, which will then use this information to split
75 * physically contiguous memory regions it is mapping into page sizes
76 * that we support.
77 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010078 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020079 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010080#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020081
Joerg Roedelb6c02712008-06-26 21:27:53 +020082static DEFINE_RWLOCK(amd_iommu_devtable_lock);
83
Joerg Roedel8fa5f802011-06-09 12:24:45 +020084/* List of all available dev_data structures */
85static LIST_HEAD(dev_data_list);
86static DEFINE_SPINLOCK(dev_data_list_lock);
87
Joerg Roedel6efed632012-06-14 15:52:58 +020088LIST_HEAD(ioapic_map);
89LIST_HEAD(hpet_map);
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040090LIST_HEAD(acpihid_map);
Joerg Roedel6efed632012-06-14 15:52:58 +020091
Joerg Roedelc5b5da92016-07-06 11:55:37 +020092#define FLUSH_QUEUE_SIZE 256
93
94struct flush_queue_entry {
95 unsigned long iova_pfn;
96 unsigned long pages;
97 struct dma_ops_domain *dma_dom;
98};
99
100struct flush_queue {
101 spinlock_t lock;
102 unsigned next;
103 struct flush_queue_entry *entries;
104};
105
106DEFINE_PER_CPU(struct flush_queue, flush_queue);
107
Joerg Roedel0feae532009-08-26 15:26:30 +0200108/*
109 * Domain for untranslated devices - only allocated
110 * if iommu=pt passed on kernel cmd line.
111 */
Thierry Redingb22f6432014-06-27 09:03:12 +0200112static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +0100113
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100114static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +0100115int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100116
Joerg Roedelac1534a2012-06-21 14:52:40 +0200117static struct dma_map_ops amd_iommu_dma_ops;
118
Joerg Roedel431b2a22008-07-11 17:14:22 +0200119/*
Joerg Roedel50917e22014-08-05 16:38:38 +0200120 * This struct contains device specific data for the IOMMU
121 */
122struct iommu_dev_data {
123 struct list_head list; /* For domain->dev_list */
124 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +0200125 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +0200126 u16 devid; /* PCI Device ID */
Joerg Roedele3156042016-04-08 15:12:24 +0200127 u16 alias; /* Alias Device ID */
Joerg Roedel50917e22014-08-05 16:38:38 +0200128 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +0200129 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +0200130 struct {
131 bool enabled;
132 int qdep;
133 } ats; /* ATS state */
134 bool pri_tlp; /* PASID TLB required for
135 PPR completions */
136 u32 errata; /* Bitmap for errata to apply */
137};
138
139/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200140 * general struct to manage commands send to an IOMMU
141 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200142struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200143 u32 data[4];
144};
145
Joerg Roedel05152a02012-06-15 16:53:51 +0200146struct kmem_cache *amd_iommu_irq_cache;
147
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200148static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200149static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100150static void detach_device(struct device *dev);
Chris Wrightc1eee672009-05-21 00:56:58 -0700151
Joerg Roedel007b74b2015-12-21 12:53:54 +0100152/*
Joerg Roedel007b74b2015-12-21 12:53:54 +0100153 * Data container for a dma_ops specific protection domain
154 */
155struct dma_ops_domain {
156 /* generic protection domain information */
157 struct protection_domain domain;
158
Joerg Roedel307d5852016-07-05 11:54:04 +0200159 /* IOVA RB-Tree */
160 struct iova_domain iovad;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100161};
162
Joerg Roedel81cd07b2016-07-07 18:01:10 +0200163static struct iova_domain reserved_iova_ranges;
164static struct lock_class_key reserved_rbtree_key;
165
Joerg Roedel15898bb2009-11-24 15:39:42 +0100166/****************************************************************************
167 *
168 * Helper functions
169 *
170 ****************************************************************************/
171
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400172static inline int match_hid_uid(struct device *dev,
173 struct acpihid_map_entry *entry)
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100174{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400175 const char *hid, *uid;
176
177 hid = acpi_device_hid(ACPI_COMPANION(dev));
178 uid = acpi_device_uid(ACPI_COMPANION(dev));
179
180 if (!hid || !(*hid))
181 return -ENODEV;
182
183 if (!uid || !(*uid))
184 return strcmp(hid, entry->hid);
185
186 if (!(*entry->uid))
187 return strcmp(hid, entry->hid);
188
189 return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100190}
191
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400192static inline u16 get_pci_device_id(struct device *dev)
Joerg Roedele3156042016-04-08 15:12:24 +0200193{
194 struct pci_dev *pdev = to_pci_dev(dev);
195
196 return PCI_DEVID(pdev->bus->number, pdev->devfn);
197}
198
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400199static inline int get_acpihid_device_id(struct device *dev,
200 struct acpihid_map_entry **entry)
201{
202 struct acpihid_map_entry *p;
203
204 list_for_each_entry(p, &acpihid_map, list) {
205 if (!match_hid_uid(dev, p)) {
206 if (entry)
207 *entry = p;
208 return p->devid;
209 }
210 }
211 return -EINVAL;
212}
213
214static inline int get_device_id(struct device *dev)
215{
216 int devid;
217
218 if (dev_is_pci(dev))
219 devid = get_pci_device_id(dev);
220 else
221 devid = get_acpihid_device_id(dev, NULL);
222
223 return devid;
224}
225
Joerg Roedel15898bb2009-11-24 15:39:42 +0100226static struct protection_domain *to_pdomain(struct iommu_domain *dom)
227{
228 return container_of(dom, struct protection_domain, domain);
229}
230
Joerg Roedelf62dda62011-06-09 12:55:35 +0200231static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200232{
233 struct iommu_dev_data *dev_data;
234 unsigned long flags;
235
236 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
237 if (!dev_data)
238 return NULL;
239
Joerg Roedelf62dda62011-06-09 12:55:35 +0200240 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200241
242 spin_lock_irqsave(&dev_data_list_lock, flags);
243 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
244 spin_unlock_irqrestore(&dev_data_list_lock, flags);
245
246 return dev_data;
247}
248
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200249static struct iommu_dev_data *search_dev_data(u16 devid)
250{
251 struct iommu_dev_data *dev_data;
252 unsigned long flags;
253
254 spin_lock_irqsave(&dev_data_list_lock, flags);
255 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
256 if (dev_data->devid == devid)
257 goto out_unlock;
258 }
259
260 dev_data = NULL;
261
262out_unlock:
263 spin_unlock_irqrestore(&dev_data_list_lock, flags);
264
265 return dev_data;
266}
267
Joerg Roedele3156042016-04-08 15:12:24 +0200268static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
269{
270 *(u16 *)data = alias;
271 return 0;
272}
273
274static u16 get_alias(struct device *dev)
275{
276 struct pci_dev *pdev = to_pci_dev(dev);
277 u16 devid, ivrs_alias, pci_alias;
278
Joerg Roedel6c0b43d2016-05-09 19:39:17 +0200279 /* The callers make sure that get_device_id() does not fail here */
Joerg Roedele3156042016-04-08 15:12:24 +0200280 devid = get_device_id(dev);
281 ivrs_alias = amd_iommu_alias_table[devid];
282 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
283
284 if (ivrs_alias == pci_alias)
285 return ivrs_alias;
286
287 /*
288 * DMA alias showdown
289 *
290 * The IVRS is fairly reliable in telling us about aliases, but it
291 * can't know about every screwy device. If we don't have an IVRS
292 * reported alias, use the PCI reported alias. In that case we may
293 * still need to initialize the rlookup and dev_table entries if the
294 * alias is to a non-existent device.
295 */
296 if (ivrs_alias == devid) {
297 if (!amd_iommu_rlookup_table[pci_alias]) {
298 amd_iommu_rlookup_table[pci_alias] =
299 amd_iommu_rlookup_table[devid];
300 memcpy(amd_iommu_dev_table[pci_alias].data,
301 amd_iommu_dev_table[devid].data,
302 sizeof(amd_iommu_dev_table[pci_alias].data));
303 }
304
305 return pci_alias;
306 }
307
308 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
309 "for device %s[%04x:%04x], kernel reported alias "
310 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
311 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
312 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
313 PCI_FUNC(pci_alias));
314
315 /*
316 * If we don't have a PCI DMA alias and the IVRS alias is on the same
317 * bus, then the IVRS table may know about a quirk that we don't.
318 */
319 if (pci_alias == devid &&
320 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
Linus Torvalds7afd16f2016-05-19 13:10:54 -0700321 pci_add_dma_alias(pdev, ivrs_alias & 0xff);
Joerg Roedele3156042016-04-08 15:12:24 +0200322 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
323 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
324 dev_name(dev));
325 }
326
327 return ivrs_alias;
328}
329
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200330static struct iommu_dev_data *find_dev_data(u16 devid)
331{
332 struct iommu_dev_data *dev_data;
333
334 dev_data = search_dev_data(devid);
335
336 if (dev_data == NULL)
337 dev_data = alloc_dev_data(devid);
338
339 return dev_data;
340}
341
Joerg Roedel657cbb62009-11-23 15:26:46 +0100342static struct iommu_dev_data *get_dev_data(struct device *dev)
343{
344 return dev->archdata.iommu;
345}
346
Wan Zongshunb097d112016-04-01 09:06:04 -0400347/*
348* Find or create an IOMMU group for a acpihid device.
349*/
350static struct iommu_group *acpihid_device_group(struct device *dev)
351{
352 struct acpihid_map_entry *p, *entry = NULL;
Dan Carpenter2d8e1f02016-04-11 10:14:46 +0300353 int devid;
Wan Zongshunb097d112016-04-01 09:06:04 -0400354
355 devid = get_acpihid_device_id(dev, &entry);
356 if (devid < 0)
357 return ERR_PTR(devid);
358
359 list_for_each_entry(p, &acpihid_map, list) {
360 if ((devid == p->devid) && p->group)
361 entry->group = p->group;
362 }
363
364 if (!entry->group)
365 entry->group = generic_device_group(dev);
366
367 return entry->group;
368}
369
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100370static bool pci_iommuv2_capable(struct pci_dev *pdev)
371{
372 static const int caps[] = {
373 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100374 PCI_EXT_CAP_ID_PRI,
375 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100376 };
377 int i, pos;
378
379 for (i = 0; i < 3; ++i) {
380 pos = pci_find_ext_capability(pdev, caps[i]);
381 if (pos == 0)
382 return false;
383 }
384
385 return true;
386}
387
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100388static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
389{
390 struct iommu_dev_data *dev_data;
391
392 dev_data = get_dev_data(&pdev->dev);
393
394 return dev_data->errata & (1 << erratum) ? true : false;
395}
396
Joerg Roedel71c70982009-11-24 16:43:06 +0100397/*
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100398 * This function checks if the driver got a valid device from the caller to
399 * avoid dereferencing invalid pointers.
400 */
401static bool check_device(struct device *dev)
402{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400403 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100404
405 if (!dev || !dev->dma_mask)
406 return false;
407
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100408 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200409 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400410 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100411
412 /* Out of our scope? */
413 if (devid > amd_iommu_last_bdf)
414 return false;
415
416 if (amd_iommu_rlookup_table[devid] == NULL)
417 return false;
418
419 return true;
420}
421
Alex Williamson25b11ce2014-09-19 10:03:13 -0600422static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600423{
Alex Williamson2851db22012-10-08 22:49:41 -0600424 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600425
Alex Williamson65d53522014-07-03 09:51:30 -0600426 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200427 if (IS_ERR(group))
428 return;
429
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200430 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600431}
432
433static int iommu_init_device(struct device *dev)
434{
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600435 struct iommu_dev_data *dev_data;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400436 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600437
438 if (dev->archdata.iommu)
439 return 0;
440
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400441 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200442 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400443 return devid;
444
445 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600446 if (!dev_data)
447 return -ENOMEM;
448
Joerg Roedele3156042016-04-08 15:12:24 +0200449 dev_data->alias = get_alias(dev);
450
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400451 if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100452 struct amd_iommu *iommu;
453
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400454 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100455 dev_data->iommu_v2 = iommu->is_iommu_v2;
456 }
457
Joerg Roedel657cbb62009-11-23 15:26:46 +0100458 dev->archdata.iommu = dev_data;
459
Alex Williamson066f2e92014-06-12 16:12:37 -0600460 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
461 dev);
462
Joerg Roedel657cbb62009-11-23 15:26:46 +0100463 return 0;
464}
465
Joerg Roedel26018872011-06-06 16:50:14 +0200466static void iommu_ignore_device(struct device *dev)
467{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400468 u16 alias;
469 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200470
471 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200472 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400473 return;
474
Joerg Roedele3156042016-04-08 15:12:24 +0200475 alias = get_alias(dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200476
477 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
478 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
479
480 amd_iommu_rlookup_table[devid] = NULL;
481 amd_iommu_rlookup_table[alias] = NULL;
482}
483
Joerg Roedel657cbb62009-11-23 15:26:46 +0100484static void iommu_uninit_device(struct device *dev)
485{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400486 int devid;
487 struct iommu_dev_data *dev_data;
Alex Williamsonc1931092014-07-03 09:51:24 -0600488
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400489 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200490 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400491 return;
492
493 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600494 if (!dev_data)
495 return;
496
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100497 if (dev_data->domain)
498 detach_device(dev);
499
Alex Williamson066f2e92014-06-12 16:12:37 -0600500 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
501 dev);
502
Alex Williamson9dcd6132012-05-30 14:19:07 -0600503 iommu_group_remove_device(dev);
504
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200505 /* Remove dma-ops */
506 dev->archdata.dma_ops = NULL;
507
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200508 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600509 * We keep dev_data around for unplugged devices and reuse it when the
510 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200511 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100512}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100513
Joerg Roedel431b2a22008-07-11 17:14:22 +0200514/****************************************************************************
515 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200516 * Interrupt handling functions
517 *
518 ****************************************************************************/
519
Joerg Roedele3e59872009-09-03 14:02:10 +0200520static void dump_dte_entry(u16 devid)
521{
522 int i;
523
Joerg Roedelee6c2862011-11-09 12:06:03 +0100524 for (i = 0; i < 4; ++i)
525 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200526 amd_iommu_dev_table[devid].data[i]);
527}
528
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200529static void dump_command(unsigned long phys_addr)
530{
531 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
532 int i;
533
534 for (i = 0; i < 4; ++i)
535 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
536}
537
Joerg Roedela345b232009-09-03 15:01:43 +0200538static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200539{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200540 int type, devid, domid, flags;
541 volatile u32 *event = __evt;
542 int count = 0;
543 u64 address;
544
545retry:
546 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
547 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
548 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
549 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
550 address = (u64)(((u64)event[3]) << 32) | event[2];
551
552 if (type == 0) {
553 /* Did we hit the erratum? */
554 if (++count == LOOP_TIMEOUT) {
555 pr_err("AMD-Vi: No event written to event log\n");
556 return;
557 }
558 udelay(1);
559 goto retry;
560 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200561
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200562 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200563
564 switch (type) {
565 case EVENT_TYPE_ILL_DEV:
566 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
567 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700568 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200569 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200570 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200571 break;
572 case EVENT_TYPE_IO_FAULT:
573 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
574 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700575 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200576 domid, address, flags);
577 break;
578 case EVENT_TYPE_DEV_TAB_ERR:
579 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
580 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700581 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200582 address, flags);
583 break;
584 case EVENT_TYPE_PAGE_TAB_ERR:
585 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
586 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700587 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200588 domid, address, flags);
589 break;
590 case EVENT_TYPE_ILL_CMD:
591 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200592 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200593 break;
594 case EVENT_TYPE_CMD_HARD_ERR:
595 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
596 "flags=0x%04x]\n", address, flags);
597 break;
598 case EVENT_TYPE_IOTLB_INV_TO:
599 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
600 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700601 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200602 address);
603 break;
604 case EVENT_TYPE_INV_DEV_REQ:
605 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
606 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700607 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200608 address, flags);
609 break;
610 default:
611 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
612 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200613
614 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200615}
616
617static void iommu_poll_events(struct amd_iommu *iommu)
618{
619 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200620
621 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
622 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
623
624 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200625 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200626 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200627 }
628
629 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200630}
631
Joerg Roedeleee53532012-06-01 15:20:23 +0200632static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100633{
634 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100635
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100636 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
637 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
638 return;
639 }
640
641 fault.address = raw[1];
642 fault.pasid = PPR_PASID(raw[0]);
643 fault.device_id = PPR_DEVID(raw[0]);
644 fault.tag = PPR_TAG(raw[0]);
645 fault.flags = PPR_FLAGS(raw[0]);
646
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100647 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
648}
649
650static void iommu_poll_ppr_log(struct amd_iommu *iommu)
651{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100652 u32 head, tail;
653
654 if (iommu->ppr_log == NULL)
655 return;
656
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100657 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
658 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
659
660 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200661 volatile u64 *raw;
662 u64 entry[2];
663 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100664
Joerg Roedeleee53532012-06-01 15:20:23 +0200665 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100666
Joerg Roedeleee53532012-06-01 15:20:23 +0200667 /*
668 * Hardware bug: Interrupt may arrive before the entry is
669 * written to memory. If this happens we need to wait for the
670 * entry to arrive.
671 */
672 for (i = 0; i < LOOP_TIMEOUT; ++i) {
673 if (PPR_REQ_TYPE(raw[0]) != 0)
674 break;
675 udelay(1);
676 }
677
678 /* Avoid memcpy function-call overhead */
679 entry[0] = raw[0];
680 entry[1] = raw[1];
681
682 /*
683 * To detect the hardware bug we need to clear the entry
684 * back to zero.
685 */
686 raw[0] = raw[1] = 0UL;
687
688 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100689 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
690 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200691
Joerg Roedeleee53532012-06-01 15:20:23 +0200692 /* Handle PPR entry */
693 iommu_handle_ppr_entry(iommu, entry);
694
Joerg Roedeleee53532012-06-01 15:20:23 +0200695 /* Refresh ring-buffer information */
696 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100697 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
698 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100699}
700
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200701irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200702{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500703 struct amd_iommu *iommu = (struct amd_iommu *) data;
704 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200705
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500706 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
707 /* Enable EVT and PPR interrupts again */
708 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
709 iommu->mmio_base + MMIO_STATUS_OFFSET);
710
711 if (status & MMIO_STATUS_EVT_INT_MASK) {
712 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
713 iommu_poll_events(iommu);
714 }
715
716 if (status & MMIO_STATUS_PPR_INT_MASK) {
717 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
718 iommu_poll_ppr_log(iommu);
719 }
720
721 /*
722 * Hardware bug: ERBT1312
723 * When re-enabling interrupt (by writing 1
724 * to clear the bit), the hardware might also try to set
725 * the interrupt bit in the event status register.
726 * In this scenario, the bit will be set, and disable
727 * subsequent interrupts.
728 *
729 * Workaround: The IOMMU driver should read back the
730 * status register and check if the interrupt bits are cleared.
731 * If not, driver will need to go through the interrupt handler
732 * again and re-clear the bits
733 */
734 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100735 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200736 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200737}
738
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200739irqreturn_t amd_iommu_int_handler(int irq, void *data)
740{
741 return IRQ_WAKE_THREAD;
742}
743
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200744/****************************************************************************
745 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200746 * IOMMU command queuing functions
747 *
748 ****************************************************************************/
749
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200750static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200751{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200752 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200753
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200754 while (*sem == 0 && i < LOOP_TIMEOUT) {
755 udelay(1);
756 i += 1;
757 }
758
759 if (i == LOOP_TIMEOUT) {
760 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
761 return -EIO;
762 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200763
764 return 0;
765}
766
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200767static void copy_cmd_to_buffer(struct amd_iommu *iommu,
768 struct iommu_cmd *cmd,
769 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200770{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200771 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200772
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200773 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200774 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200775
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200776 /* Copy command to buffer */
777 memcpy(target, cmd, sizeof(*cmd));
778
779 /* Tell the IOMMU about it */
780 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
781}
782
Joerg Roedel815b33f2011-04-06 17:26:49 +0200783static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200784{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200785 WARN_ON(address & 0x7ULL);
786
Joerg Roedelded46732011-04-06 10:53:48 +0200787 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200788 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
789 cmd->data[1] = upper_32_bits(__pa(address));
790 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200791 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
792}
793
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200794static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
795{
796 memset(cmd, 0, sizeof(*cmd));
797 cmd->data[0] = devid;
798 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
799}
800
Joerg Roedel11b64022011-04-06 11:49:28 +0200801static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
802 size_t size, u16 domid, int pde)
803{
804 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100805 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200806
807 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100808 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200809
810 if (pages > 1) {
811 /*
812 * If we have to flush more than one page, flush all
813 * TLB entries for this domain
814 */
815 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100816 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200817 }
818
819 address &= PAGE_MASK;
820
821 memset(cmd, 0, sizeof(*cmd));
822 cmd->data[1] |= domid;
823 cmd->data[2] = lower_32_bits(address);
824 cmd->data[3] = upper_32_bits(address);
825 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
826 if (s) /* size bit - we flush more than one 4kb page */
827 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200828 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200829 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
830}
831
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200832static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
833 u64 address, size_t size)
834{
835 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100836 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200837
838 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100839 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200840
841 if (pages > 1) {
842 /*
843 * If we have to flush more than one page, flush all
844 * TLB entries for this domain
845 */
846 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100847 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200848 }
849
850 address &= PAGE_MASK;
851
852 memset(cmd, 0, sizeof(*cmd));
853 cmd->data[0] = devid;
854 cmd->data[0] |= (qdep & 0xff) << 24;
855 cmd->data[1] = devid;
856 cmd->data[2] = lower_32_bits(address);
857 cmd->data[3] = upper_32_bits(address);
858 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
859 if (s)
860 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
861}
862
Joerg Roedel22e266c2011-11-21 15:59:08 +0100863static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
864 u64 address, bool size)
865{
866 memset(cmd, 0, sizeof(*cmd));
867
868 address &= ~(0xfffULL);
869
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600870 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100871 cmd->data[1] = domid;
872 cmd->data[2] = lower_32_bits(address);
873 cmd->data[3] = upper_32_bits(address);
874 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
875 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
876 if (size)
877 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
878 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
879}
880
881static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
882 int qdep, u64 address, bool size)
883{
884 memset(cmd, 0, sizeof(*cmd));
885
886 address &= ~(0xfffULL);
887
888 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600889 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100890 cmd->data[0] |= (qdep & 0xff) << 24;
891 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600892 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100893 cmd->data[2] = lower_32_bits(address);
894 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
895 cmd->data[3] = upper_32_bits(address);
896 if (size)
897 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
898 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
899}
900
Joerg Roedelc99afa22011-11-21 18:19:25 +0100901static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
902 int status, int tag, bool gn)
903{
904 memset(cmd, 0, sizeof(*cmd));
905
906 cmd->data[0] = devid;
907 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600908 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100909 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
910 }
911 cmd->data[3] = tag & 0x1ff;
912 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
913
914 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
915}
916
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200917static void build_inv_all(struct iommu_cmd *cmd)
918{
919 memset(cmd, 0, sizeof(*cmd));
920 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200921}
922
Joerg Roedel7ef27982012-06-21 16:46:04 +0200923static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
924{
925 memset(cmd, 0, sizeof(*cmd));
926 cmd->data[0] = devid;
927 CMD_SET_TYPE(cmd, CMD_INV_IRT);
928}
929
Joerg Roedel431b2a22008-07-11 17:14:22 +0200930/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200931 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200932 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200933 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200934static int iommu_queue_command_sync(struct amd_iommu *iommu,
935 struct iommu_cmd *cmd,
936 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200937{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200938 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200939 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200940
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200941again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200942 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200943
944 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
945 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200946 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
947 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200948
949 if (left <= 2) {
950 struct iommu_cmd sync_cmd;
951 volatile u64 sem = 0;
952 int ret;
953
954 build_completion_wait(&sync_cmd, (u64)&sem);
955 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
956
957 spin_unlock_irqrestore(&iommu->lock, flags);
958
959 if ((ret = wait_on_sem(&sem)) != 0)
960 return ret;
961
962 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200963 }
964
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200965 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200966
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200967 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200968 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200969
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200970 spin_unlock_irqrestore(&iommu->lock, flags);
971
Joerg Roedel815b33f2011-04-06 17:26:49 +0200972 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100973}
974
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200975static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
976{
977 return iommu_queue_command_sync(iommu, cmd, true);
978}
979
Joerg Roedel8d201962008-12-02 20:34:41 +0100980/*
981 * This function queues a completion wait command into the command
982 * buffer of an IOMMU
983 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100984static int iommu_completion_wait(struct amd_iommu *iommu)
985{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200986 struct iommu_cmd cmd;
987 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200988 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100989
990 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200991 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100992
Joerg Roedel815b33f2011-04-06 17:26:49 +0200993 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100994
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200995 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100996 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200997 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100998
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200999 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001000}
1001
Joerg Roedeld8c13082011-04-06 18:51:26 +02001002static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001003{
1004 struct iommu_cmd cmd;
1005
Joerg Roedeld8c13082011-04-06 18:51:26 +02001006 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001007
Joerg Roedeld8c13082011-04-06 18:51:26 +02001008 return iommu_queue_command(iommu, &cmd);
1009}
1010
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001011static void iommu_flush_dte_all(struct amd_iommu *iommu)
1012{
1013 u32 devid;
1014
1015 for (devid = 0; devid <= 0xffff; ++devid)
1016 iommu_flush_dte(iommu, devid);
1017
1018 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001019}
1020
1021/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001022 * This function uses heavy locking and may disable irqs for some time. But
1023 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001024 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001025static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001026{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001027 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001028
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001029 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1030 struct iommu_cmd cmd;
1031 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1032 dom_id, 1);
1033 iommu_queue_command(iommu, &cmd);
1034 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001035
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001036 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001037}
1038
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001039static void iommu_flush_all(struct amd_iommu *iommu)
1040{
1041 struct iommu_cmd cmd;
1042
1043 build_inv_all(&cmd);
1044
1045 iommu_queue_command(iommu, &cmd);
1046 iommu_completion_wait(iommu);
1047}
1048
Joerg Roedel7ef27982012-06-21 16:46:04 +02001049static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1050{
1051 struct iommu_cmd cmd;
1052
1053 build_inv_irt(&cmd, devid);
1054
1055 iommu_queue_command(iommu, &cmd);
1056}
1057
1058static void iommu_flush_irt_all(struct amd_iommu *iommu)
1059{
1060 u32 devid;
1061
1062 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1063 iommu_flush_irt(iommu, devid);
1064
1065 iommu_completion_wait(iommu);
1066}
1067
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001068void iommu_flush_all_caches(struct amd_iommu *iommu)
1069{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001070 if (iommu_feature(iommu, FEATURE_IA)) {
1071 iommu_flush_all(iommu);
1072 } else {
1073 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001074 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001075 iommu_flush_tlb_all(iommu);
1076 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001077}
1078
Joerg Roedel431b2a22008-07-11 17:14:22 +02001079/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001080 * Command send function for flushing on-device TLB
1081 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001082static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1083 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001084{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001085 struct amd_iommu *iommu;
1086 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001087 int qdep;
1088
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001089 qdep = dev_data->ats.qdep;
1090 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001091
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001092 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001093
1094 return iommu_queue_command(iommu, &cmd);
1095}
1096
1097/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001098 * Command send function for invalidating a device table entry
1099 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001100static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001101{
1102 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001103 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001104 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001105
Joerg Roedel6c542042011-06-09 17:07:31 +02001106 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001107 alias = dev_data->alias;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001108
Joerg Roedelf62dda62011-06-09 12:55:35 +02001109 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001110 if (!ret && alias != dev_data->devid)
1111 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001112 if (ret)
1113 return ret;
1114
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001115 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001116 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001117
1118 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001119}
1120
Joerg Roedel431b2a22008-07-11 17:14:22 +02001121/*
1122 * TLB invalidation function which is called from the mapping functions.
1123 * It invalidates a single PTE if the range to flush is within a single
1124 * page. Otherwise it flushes the whole TLB of the IOMMU.
1125 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001126static void __domain_flush_pages(struct protection_domain *domain,
1127 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001128{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001129 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001130 struct iommu_cmd cmd;
1131 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001132
Joerg Roedel11b64022011-04-06 11:49:28 +02001133 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001134
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001135 for (i = 0; i < amd_iommus_present; ++i) {
1136 if (!domain->dev_iommu[i])
1137 continue;
1138
1139 /*
1140 * Devices of this domain are behind this IOMMU
1141 * We need a TLB flush
1142 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001143 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001144 }
1145
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001146 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001147
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001148 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001149 continue;
1150
Joerg Roedel6c542042011-06-09 17:07:31 +02001151 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001152 }
1153
Joerg Roedel11b64022011-04-06 11:49:28 +02001154 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001155}
1156
Joerg Roedel17b124b2011-04-06 18:01:35 +02001157static void domain_flush_pages(struct protection_domain *domain,
1158 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001159{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001160 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001161}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001162
Joerg Roedel1c655772008-09-04 18:40:05 +02001163/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001164static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001165{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001166 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001167}
1168
Chris Wright42a49f92009-06-15 15:42:00 +02001169/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001170static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001171{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001172 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1173}
1174
1175static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001176{
1177 int i;
1178
1179 for (i = 0; i < amd_iommus_present; ++i) {
Joerg Roedelf1eae7c2016-07-06 12:50:35 +02001180 if (domain && !domain->dev_iommu[i])
Joerg Roedelb6c02712008-06-26 21:27:53 +02001181 continue;
1182
1183 /*
1184 * Devices of this domain are behind this IOMMU
1185 * We need to wait for completion of all commands.
1186 */
1187 iommu_completion_wait(amd_iommus[i]);
1188 }
1189}
1190
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001191
Joerg Roedel43f49602008-12-02 21:01:12 +01001192/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001193 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001194 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001195static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001196{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001197 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001198
1199 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001200 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001201}
1202
Joerg Roedel431b2a22008-07-11 17:14:22 +02001203/****************************************************************************
1204 *
1205 * The functions below are used the create the page table mappings for
1206 * unity mapped regions.
1207 *
1208 ****************************************************************************/
1209
1210/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001211 * This function is used to add another level to an IO page table. Adding
1212 * another level increases the size of the address space by 9 bits to a size up
1213 * to 64 bits.
1214 */
1215static bool increase_address_space(struct protection_domain *domain,
1216 gfp_t gfp)
1217{
1218 u64 *pte;
1219
1220 if (domain->mode == PAGE_MODE_6_LEVEL)
1221 /* address space already 64 bit large */
1222 return false;
1223
1224 pte = (void *)get_zeroed_page(gfp);
1225 if (!pte)
1226 return false;
1227
1228 *pte = PM_LEVEL_PDE(domain->mode,
1229 virt_to_phys(domain->pt_root));
1230 domain->pt_root = pte;
1231 domain->mode += 1;
1232 domain->updated = true;
1233
1234 return true;
1235}
1236
1237static u64 *alloc_pte(struct protection_domain *domain,
1238 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001239 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001240 u64 **pte_page,
1241 gfp_t gfp)
1242{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001243 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001244 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001245
1246 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001247
1248 while (address > PM_LEVEL_SIZE(domain->mode))
1249 increase_address_space(domain, gfp);
1250
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001251 level = domain->mode - 1;
1252 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1253 address = PAGE_SIZE_ALIGN(address, page_size);
1254 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001255
1256 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001257 u64 __pte, __npte;
1258
1259 __pte = *pte;
1260
1261 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001262 page = (u64 *)get_zeroed_page(gfp);
1263 if (!page)
1264 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001265
1266 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1267
1268 if (cmpxchg64(pte, __pte, __npte)) {
1269 free_page((unsigned long)page);
1270 continue;
1271 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001272 }
1273
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001274 /* No level skipping support yet */
1275 if (PM_PTE_LEVEL(*pte) != level)
1276 return NULL;
1277
Joerg Roedel308973d2009-11-24 17:43:32 +01001278 level -= 1;
1279
1280 pte = IOMMU_PTE_PAGE(*pte);
1281
1282 if (pte_page && level == end_lvl)
1283 *pte_page = pte;
1284
1285 pte = &pte[PM_LEVEL_INDEX(level, address)];
1286 }
1287
1288 return pte;
1289}
1290
1291/*
1292 * This function checks if there is a PTE for a given dma address. If
1293 * there is one, it returns the pointer to it.
1294 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001295static u64 *fetch_pte(struct protection_domain *domain,
1296 unsigned long address,
1297 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001298{
1299 int level;
1300 u64 *pte;
1301
Joerg Roedel24cd7722010-01-19 17:27:39 +01001302 if (address > PM_LEVEL_SIZE(domain->mode))
1303 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001304
Joerg Roedel3039ca12015-04-01 14:58:48 +02001305 level = domain->mode - 1;
1306 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1307 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001308
1309 while (level > 0) {
1310
1311 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001312 if (!IOMMU_PTE_PRESENT(*pte))
1313 return NULL;
1314
Joerg Roedel24cd7722010-01-19 17:27:39 +01001315 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001316 if (PM_PTE_LEVEL(*pte) == 7 ||
1317 PM_PTE_LEVEL(*pte) == 0)
1318 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001319
1320 /* No level skipping support yet */
1321 if (PM_PTE_LEVEL(*pte) != level)
1322 return NULL;
1323
Joerg Roedel308973d2009-11-24 17:43:32 +01001324 level -= 1;
1325
Joerg Roedel24cd7722010-01-19 17:27:39 +01001326 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001327 pte = IOMMU_PTE_PAGE(*pte);
1328 pte = &pte[PM_LEVEL_INDEX(level, address)];
1329 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1330 }
1331
1332 if (PM_PTE_LEVEL(*pte) == 0x07) {
1333 unsigned long pte_mask;
1334
1335 /*
1336 * If we have a series of large PTEs, make
1337 * sure to return a pointer to the first one.
1338 */
1339 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1340 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1341 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001342 }
1343
1344 return pte;
1345}
1346
1347/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001348 * Generic mapping functions. It maps a physical address into a DMA
1349 * address space. It allocates the page table pages if necessary.
1350 * In the future it can be extended to a generic mapping function
1351 * supporting all features of AMD IOMMU page tables like level skipping
1352 * and full 64 bit address spaces.
1353 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001354static int iommu_map_page(struct protection_domain *dom,
1355 unsigned long bus_addr,
1356 unsigned long phys_addr,
Joerg Roedelb911b892016-07-05 14:29:11 +02001357 unsigned long page_size,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001358 int prot,
Joerg Roedelb911b892016-07-05 14:29:11 +02001359 gfp_t gfp)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001360{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001361 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001362 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001363
Joerg Roedeld4b03662015-04-01 14:58:52 +02001364 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1365 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1366
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001367 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001368 return -EINVAL;
1369
Joerg Roedeld4b03662015-04-01 14:58:52 +02001370 count = PAGE_SIZE_PTE_COUNT(page_size);
Joerg Roedelb911b892016-07-05 14:29:11 +02001371 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001372
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001373 if (!pte)
1374 return -ENOMEM;
1375
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001376 for (i = 0; i < count; ++i)
1377 if (IOMMU_PTE_PRESENT(pte[i]))
1378 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001379
Joerg Roedeld4b03662015-04-01 14:58:52 +02001380 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001381 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1382 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1383 } else
1384 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1385
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001386 if (prot & IOMMU_PROT_IR)
1387 __pte |= IOMMU_PTE_IR;
1388 if (prot & IOMMU_PROT_IW)
1389 __pte |= IOMMU_PTE_IW;
1390
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001391 for (i = 0; i < count; ++i)
1392 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001393
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001394 update_domain(dom);
1395
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001396 return 0;
1397}
1398
Joerg Roedel24cd7722010-01-19 17:27:39 +01001399static unsigned long iommu_unmap_page(struct protection_domain *dom,
1400 unsigned long bus_addr,
1401 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001402{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001403 unsigned long long unmapped;
1404 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001405 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001406
Joerg Roedel24cd7722010-01-19 17:27:39 +01001407 BUG_ON(!is_power_of_2(page_size));
1408
1409 unmapped = 0;
1410
1411 while (unmapped < page_size) {
1412
Joerg Roedel71b390e2015-04-01 14:58:49 +02001413 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001414
Joerg Roedel71b390e2015-04-01 14:58:49 +02001415 if (pte) {
1416 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001417
Joerg Roedel71b390e2015-04-01 14:58:49 +02001418 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001419 for (i = 0; i < count; i++)
1420 pte[i] = 0ULL;
1421 }
1422
1423 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1424 unmapped += unmap_size;
1425 }
1426
Alex Williamson60d0ca32013-06-21 14:33:19 -06001427 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001428
1429 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001430}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001431
Joerg Roedel431b2a22008-07-11 17:14:22 +02001432/****************************************************************************
1433 *
1434 * The next functions belong to the address allocator for the dma_ops
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001435 * interface functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001436 *
1437 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001438
Joerg Roedel9cabe892009-05-18 16:38:55 +02001439
Joerg Roedel256e4622016-07-05 14:23:01 +02001440static unsigned long dma_ops_alloc_iova(struct device *dev,
1441 struct dma_ops_domain *dma_dom,
1442 unsigned int pages, u64 dma_mask)
Joerg Roedela0f51442015-12-21 16:20:09 +01001443{
Joerg Roedel256e4622016-07-05 14:23:01 +02001444 unsigned long pfn = 0;
Joerg Roedela0f51442015-12-21 16:20:09 +01001445
Joerg Roedel256e4622016-07-05 14:23:01 +02001446 pages = __roundup_pow_of_two(pages);
Joerg Roedela0f51442015-12-21 16:20:09 +01001447
Joerg Roedel256e4622016-07-05 14:23:01 +02001448 if (dma_mask > DMA_BIT_MASK(32))
1449 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
1450 IOVA_PFN(DMA_BIT_MASK(32)));
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001451
Joerg Roedel256e4622016-07-05 14:23:01 +02001452 if (!pfn)
1453 pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask));
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001454
Joerg Roedel256e4622016-07-05 14:23:01 +02001455 return (pfn << PAGE_SHIFT);
Joerg Roedela0f51442015-12-21 16:20:09 +01001456}
1457
Joerg Roedel256e4622016-07-05 14:23:01 +02001458static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
1459 unsigned long address,
1460 unsigned int pages)
Joerg Roedel384de722009-05-15 12:30:05 +02001461{
Joerg Roedel256e4622016-07-05 14:23:01 +02001462 pages = __roundup_pow_of_two(pages);
1463 address >>= PAGE_SHIFT;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001464
Joerg Roedel256e4622016-07-05 14:23:01 +02001465 free_iova_fast(&dma_dom->iovad, address, pages);
Joerg Roedeld3086442008-06-26 21:27:57 +02001466}
1467
Joerg Roedel431b2a22008-07-11 17:14:22 +02001468/****************************************************************************
1469 *
1470 * The next functions belong to the domain allocation. A domain is
1471 * allocated for every IOMMU as the default domain. If device isolation
1472 * is enabled, every device get its own domain. The most important thing
1473 * about domains is the page table mapping the DMA address space they
1474 * contain.
1475 *
1476 ****************************************************************************/
1477
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001478/*
1479 * This function adds a protection domain to the global protection domain list
1480 */
1481static void add_domain_to_list(struct protection_domain *domain)
1482{
1483 unsigned long flags;
1484
1485 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1486 list_add(&domain->list, &amd_iommu_pd_list);
1487 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1488}
1489
1490/*
1491 * This function removes a protection domain to the global
1492 * protection domain list
1493 */
1494static void del_domain_from_list(struct protection_domain *domain)
1495{
1496 unsigned long flags;
1497
1498 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1499 list_del(&domain->list);
1500 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1501}
1502
Joerg Roedelec487d12008-06-26 21:27:58 +02001503static u16 domain_id_alloc(void)
1504{
1505 unsigned long flags;
1506 int id;
1507
1508 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1509 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1510 BUG_ON(id == 0);
1511 if (id > 0 && id < MAX_DOMAIN_ID)
1512 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1513 else
1514 id = 0;
1515 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1516
1517 return id;
1518}
1519
Joerg Roedela2acfb72008-12-02 18:28:53 +01001520static void domain_id_free(int id)
1521{
1522 unsigned long flags;
1523
1524 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1525 if (id > 0 && id < MAX_DOMAIN_ID)
1526 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1527 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1528}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001529
Joerg Roedel5c34c402013-06-20 20:22:58 +02001530#define DEFINE_FREE_PT_FN(LVL, FN) \
1531static void free_pt_##LVL (unsigned long __pt) \
1532{ \
1533 unsigned long p; \
1534 u64 *pt; \
1535 int i; \
1536 \
1537 pt = (u64 *)__pt; \
1538 \
1539 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001540 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001541 if (!IOMMU_PTE_PRESENT(pt[i])) \
1542 continue; \
1543 \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001544 /* Large PTE? */ \
1545 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1546 PM_PTE_LEVEL(pt[i]) == 7) \
1547 continue; \
1548 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001549 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1550 FN(p); \
1551 } \
1552 free_page((unsigned long)pt); \
1553}
1554
1555DEFINE_FREE_PT_FN(l2, free_page)
1556DEFINE_FREE_PT_FN(l3, free_pt_l2)
1557DEFINE_FREE_PT_FN(l4, free_pt_l3)
1558DEFINE_FREE_PT_FN(l5, free_pt_l4)
1559DEFINE_FREE_PT_FN(l6, free_pt_l5)
1560
Joerg Roedel86db2e52008-12-02 18:20:21 +01001561static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001562{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001563 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001564
Joerg Roedel5c34c402013-06-20 20:22:58 +02001565 switch (domain->mode) {
1566 case PAGE_MODE_NONE:
1567 break;
1568 case PAGE_MODE_1_LEVEL:
1569 free_page(root);
1570 break;
1571 case PAGE_MODE_2_LEVEL:
1572 free_pt_l2(root);
1573 break;
1574 case PAGE_MODE_3_LEVEL:
1575 free_pt_l3(root);
1576 break;
1577 case PAGE_MODE_4_LEVEL:
1578 free_pt_l4(root);
1579 break;
1580 case PAGE_MODE_5_LEVEL:
1581 free_pt_l5(root);
1582 break;
1583 case PAGE_MODE_6_LEVEL:
1584 free_pt_l6(root);
1585 break;
1586 default:
1587 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001588 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001589}
1590
Joerg Roedelb16137b2011-11-21 16:50:23 +01001591static void free_gcr3_tbl_level1(u64 *tbl)
1592{
1593 u64 *ptr;
1594 int i;
1595
1596 for (i = 0; i < 512; ++i) {
1597 if (!(tbl[i] & GCR3_VALID))
1598 continue;
1599
1600 ptr = __va(tbl[i] & PAGE_MASK);
1601
1602 free_page((unsigned long)ptr);
1603 }
1604}
1605
1606static void free_gcr3_tbl_level2(u64 *tbl)
1607{
1608 u64 *ptr;
1609 int i;
1610
1611 for (i = 0; i < 512; ++i) {
1612 if (!(tbl[i] & GCR3_VALID))
1613 continue;
1614
1615 ptr = __va(tbl[i] & PAGE_MASK);
1616
1617 free_gcr3_tbl_level1(ptr);
1618 }
1619}
1620
Joerg Roedel52815b72011-11-17 17:24:28 +01001621static void free_gcr3_table(struct protection_domain *domain)
1622{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001623 if (domain->glx == 2)
1624 free_gcr3_tbl_level2(domain->gcr3_tbl);
1625 else if (domain->glx == 1)
1626 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001627 else
1628 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001629
Joerg Roedel52815b72011-11-17 17:24:28 +01001630 free_page((unsigned long)domain->gcr3_tbl);
1631}
1632
Joerg Roedel431b2a22008-07-11 17:14:22 +02001633/*
1634 * Free a domain, only used if something went wrong in the
1635 * allocation path and we need to free an already allocated page table
1636 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001637static void dma_ops_domain_free(struct dma_ops_domain *dom)
1638{
1639 if (!dom)
1640 return;
1641
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001642 del_domain_from_list(&dom->domain);
1643
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001644 put_iova_domain(&dom->iovad);
1645
Joerg Roedel86db2e52008-12-02 18:20:21 +01001646 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001647
Joerg Roedelec487d12008-06-26 21:27:58 +02001648 kfree(dom);
1649}
1650
Joerg Roedel431b2a22008-07-11 17:14:22 +02001651/*
1652 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001653 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001654 * structures required for the dma_ops interface
1655 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001656static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001657{
1658 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001659
1660 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1661 if (!dma_dom)
1662 return NULL;
1663
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001664 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001665 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001666
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001667 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001668 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001669 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001670 dma_dom->domain.priv = dma_dom;
1671 if (!dma_dom->domain.pt_root)
1672 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001673
Joerg Roedel307d5852016-07-05 11:54:04 +02001674 init_iova_domain(&dma_dom->iovad, PAGE_SIZE,
1675 IOVA_START_PFN, DMA_32BIT_PFN);
1676
Joerg Roedel81cd07b2016-07-07 18:01:10 +02001677 /* Initialize reserved ranges */
1678 copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
1679
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001680 add_domain_to_list(&dma_dom->domain);
1681
Joerg Roedelec487d12008-06-26 21:27:58 +02001682 return dma_dom;
1683
1684free_dma_dom:
1685 dma_ops_domain_free(dma_dom);
1686
1687 return NULL;
1688}
1689
Joerg Roedel431b2a22008-07-11 17:14:22 +02001690/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001691 * little helper function to check whether a given protection domain is a
1692 * dma_ops domain
1693 */
1694static bool dma_ops_domain(struct protection_domain *domain)
1695{
1696 return domain->flags & PD_DMA_OPS_MASK;
1697}
1698
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001699static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001700{
Joerg Roedel132bd682011-11-17 14:18:46 +01001701 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001702 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001703
Joerg Roedel132bd682011-11-17 14:18:46 +01001704 if (domain->mode != PAGE_MODE_NONE)
1705 pte_root = virt_to_phys(domain->pt_root);
1706
Joerg Roedel38ddf412008-09-11 10:38:32 +02001707 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1708 << DEV_ENTRY_MODE_SHIFT;
1709 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001710
Joerg Roedelee6c2862011-11-09 12:06:03 +01001711 flags = amd_iommu_dev_table[devid].data[1];
1712
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001713 if (ats)
1714 flags |= DTE_FLAG_IOTLB;
1715
Joerg Roedel52815b72011-11-17 17:24:28 +01001716 if (domain->flags & PD_IOMMUV2_MASK) {
1717 u64 gcr3 = __pa(domain->gcr3_tbl);
1718 u64 glx = domain->glx;
1719 u64 tmp;
1720
1721 pte_root |= DTE_FLAG_GV;
1722 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1723
1724 /* First mask out possible old values for GCR3 table */
1725 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1726 flags &= ~tmp;
1727
1728 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1729 flags &= ~tmp;
1730
1731 /* Encode GCR3 table into DTE */
1732 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1733 pte_root |= tmp;
1734
1735 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1736 flags |= tmp;
1737
1738 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1739 flags |= tmp;
1740 }
1741
Joerg Roedelee6c2862011-11-09 12:06:03 +01001742 flags &= ~(0xffffUL);
1743 flags |= domain->id;
1744
1745 amd_iommu_dev_table[devid].data[1] = flags;
1746 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001747}
1748
Joerg Roedel15898bb2009-11-24 15:39:42 +01001749static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001750{
Joerg Roedel355bf552008-12-08 12:02:41 +01001751 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001752 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1753 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001754
Joerg Roedelc5cca142009-10-09 18:31:20 +02001755 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001756}
1757
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001758static void do_attach(struct iommu_dev_data *dev_data,
1759 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001760{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001761 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001762 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001763 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001764
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001765 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001766 alias = dev_data->alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001767 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001768
1769 /* Update data structures */
1770 dev_data->domain = domain;
1771 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001772
1773 /* Do reference counting */
1774 domain->dev_iommu[iommu->index] += 1;
1775 domain->dev_cnt += 1;
1776
Joerg Roedele25bfb52015-10-20 17:33:38 +02001777 /* Update device table */
1778 set_dte_entry(dev_data->devid, domain, ats);
1779 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08001780 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001781
Joerg Roedel6c542042011-06-09 17:07:31 +02001782 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001783}
1784
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001785static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001786{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001787 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001788 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001789
Joerg Roedel5adad992015-10-09 16:23:33 +02001790 /*
1791 * First check if the device is still attached. It might already
1792 * be detached from its domain because the generic
1793 * iommu_detach_group code detached it and we try again here in
1794 * our alias handling.
1795 */
1796 if (!dev_data->domain)
1797 return;
1798
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001799 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001800 alias = dev_data->alias;
Joerg Roedelc5cca142009-10-09 18:31:20 +02001801
Joerg Roedelc4596112009-11-20 14:57:32 +01001802 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001803 dev_data->domain->dev_iommu[iommu->index] -= 1;
1804 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001805
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001806 /* Update data structures */
1807 dev_data->domain = NULL;
1808 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001809 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001810 if (alias != dev_data->devid)
1811 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001812
1813 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001814 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001815}
1816
1817/*
1818 * If a device is not yet associated with a domain, this function does
1819 * assigns it visible for the hardware
1820 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001821static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001822 struct protection_domain *domain)
1823{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001824 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001825
Joerg Roedel272e4f92015-10-20 17:33:37 +02001826 /*
1827 * Must be called with IRQs disabled. Warn here to detect early
1828 * when its not.
1829 */
1830 WARN_ON(!irqs_disabled());
1831
Joerg Roedel15898bb2009-11-24 15:39:42 +01001832 /* lock domain */
1833 spin_lock(&domain->lock);
1834
Joerg Roedel397111a2014-08-05 17:31:51 +02001835 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02001836 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02001837 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01001838
Joerg Roedel397111a2014-08-05 17:31:51 +02001839 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02001840 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001841
Julia Lawall84fe6c12010-05-27 12:31:51 +02001842 ret = 0;
1843
1844out_unlock:
1845
Joerg Roedel355bf552008-12-08 12:02:41 +01001846 /* ready */
1847 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001848
Julia Lawall84fe6c12010-05-27 12:31:51 +02001849 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001850}
1851
Joerg Roedel52815b72011-11-17 17:24:28 +01001852
1853static void pdev_iommuv2_disable(struct pci_dev *pdev)
1854{
1855 pci_disable_ats(pdev);
1856 pci_disable_pri(pdev);
1857 pci_disable_pasid(pdev);
1858}
1859
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001860/* FIXME: Change generic reset-function to do the same */
1861static int pri_reset_while_enabled(struct pci_dev *pdev)
1862{
1863 u16 control;
1864 int pos;
1865
Joerg Roedel46277b72011-12-07 14:34:02 +01001866 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001867 if (!pos)
1868 return -EINVAL;
1869
Joerg Roedel46277b72011-12-07 14:34:02 +01001870 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1871 control |= PCI_PRI_CTRL_RESET;
1872 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001873
1874 return 0;
1875}
1876
Joerg Roedel52815b72011-11-17 17:24:28 +01001877static int pdev_iommuv2_enable(struct pci_dev *pdev)
1878{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001879 bool reset_enable;
1880 int reqs, ret;
1881
1882 /* FIXME: Hardcode number of outstanding requests for now */
1883 reqs = 32;
1884 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
1885 reqs = 1;
1886 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01001887
1888 /* Only allow access to user-accessible pages */
1889 ret = pci_enable_pasid(pdev, 0);
1890 if (ret)
1891 goto out_err;
1892
1893 /* First reset the PRI state of the device */
1894 ret = pci_reset_pri(pdev);
1895 if (ret)
1896 goto out_err;
1897
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001898 /* Enable PRI */
1899 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01001900 if (ret)
1901 goto out_err;
1902
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001903 if (reset_enable) {
1904 ret = pri_reset_while_enabled(pdev);
1905 if (ret)
1906 goto out_err;
1907 }
1908
Joerg Roedel52815b72011-11-17 17:24:28 +01001909 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1910 if (ret)
1911 goto out_err;
1912
1913 return 0;
1914
1915out_err:
1916 pci_disable_pri(pdev);
1917 pci_disable_pasid(pdev);
1918
1919 return ret;
1920}
1921
Joerg Roedelc99afa22011-11-21 18:19:25 +01001922/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02001923#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001924
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001925static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001926{
Joerg Roedela3b93122012-04-12 12:49:26 +02001927 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001928 int pos;
1929
Joerg Roedel46277b72011-12-07 14:34:02 +01001930 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01001931 if (!pos)
1932 return false;
1933
Joerg Roedela3b93122012-04-12 12:49:26 +02001934 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01001935
Joerg Roedela3b93122012-04-12 12:49:26 +02001936 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001937}
1938
Joerg Roedel15898bb2009-11-24 15:39:42 +01001939/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02001940 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01001941 * assigns it visible for the hardware
1942 */
1943static int attach_device(struct device *dev,
1944 struct protection_domain *domain)
1945{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001946 struct pci_dev *pdev;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001947 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001948 unsigned long flags;
1949 int ret;
1950
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001951 dev_data = get_dev_data(dev);
1952
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001953 if (!dev_is_pci(dev))
1954 goto skip_ats_check;
1955
1956 pdev = to_pci_dev(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01001957 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02001958 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01001959 return -EINVAL;
1960
Joerg Roedel02ca2022015-07-28 16:58:49 +02001961 if (dev_data->iommu_v2) {
1962 if (pdev_iommuv2_enable(pdev) != 0)
1963 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01001964
Joerg Roedel02ca2022015-07-28 16:58:49 +02001965 dev_data->ats.enabled = true;
1966 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1967 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
1968 }
Joerg Roedel52815b72011-11-17 17:24:28 +01001969 } else if (amd_iommu_iotlb_sup &&
1970 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001971 dev_data->ats.enabled = true;
1972 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1973 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001974
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001975skip_ats_check:
Joerg Roedel15898bb2009-11-24 15:39:42 +01001976 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001977 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001978 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1979
1980 /*
1981 * We might boot into a crash-kernel here. The crashed kernel
1982 * left the caches in the IOMMU dirty. So we have to flush
1983 * here to evict all dirty stuff.
1984 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001985 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001986
1987 return ret;
1988}
1989
1990/*
1991 * Removes a device from a protection domain (unlocked)
1992 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001993static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001994{
Joerg Roedel2ca76272010-01-22 16:45:31 +01001995 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001996
Joerg Roedel272e4f92015-10-20 17:33:37 +02001997 /*
1998 * Must be called with IRQs disabled. Warn here to detect early
1999 * when its not.
2000 */
2001 WARN_ON(!irqs_disabled());
2002
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002003 if (WARN_ON(!dev_data->domain))
2004 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002005
Joerg Roedel2ca76272010-01-22 16:45:31 +01002006 domain = dev_data->domain;
2007
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002008 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002009
Joerg Roedel150952f2015-10-20 17:33:35 +02002010 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002011
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002012 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002013}
2014
2015/*
2016 * Removes a device from a protection domain (with devtable_lock held)
2017 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002018static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002019{
Joerg Roedel52815b72011-11-17 17:24:28 +01002020 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002021 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002022 unsigned long flags;
2023
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002024 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002025 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002026
Joerg Roedel355bf552008-12-08 12:02:41 +01002027 /* lock device table */
2028 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002029 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002030 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002031
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002032 if (!dev_is_pci(dev))
2033 return;
2034
Joerg Roedel02ca2022015-07-28 16:58:49 +02002035 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002036 pdev_iommuv2_disable(to_pci_dev(dev));
2037 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002038 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002039
2040 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002041}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002042
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002043static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002044{
Joerg Roedel71f77582011-06-09 19:03:15 +02002045 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002046 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002047 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002048 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002049
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002050 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002051 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002052
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002053 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002054 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002055 return devid;
2056
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002057 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002058
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002059 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002060 if (ret) {
2061 if (ret != -ENOTSUPP)
2062 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2063 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002064
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002065 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002066 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002067 goto out;
2068 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002069 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002070
Joerg Roedel07ee8692015-05-28 18:41:42 +02002071 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002072
2073 BUG_ON(!dev_data);
2074
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002075 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002076 iommu_request_dm_for_dev(dev);
2077
2078 /* Domains are initialized for this device - have a look what we ended up with */
2079 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002080 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002081 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002082 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002083 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002084
2085out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002086 iommu_completion_wait(iommu);
2087
Joerg Roedele275a2a2008-12-10 18:27:25 +01002088 return 0;
2089}
2090
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002091static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002092{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002093 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002094 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002095
2096 if (!check_device(dev))
2097 return;
2098
2099 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002100 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002101 return;
2102
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002103 iommu = amd_iommu_rlookup_table[devid];
2104
2105 iommu_uninit_device(dev);
2106 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002107}
2108
Wan Zongshunb097d112016-04-01 09:06:04 -04002109static struct iommu_group *amd_iommu_device_group(struct device *dev)
2110{
2111 if (dev_is_pci(dev))
2112 return pci_device_group(dev);
2113
2114 return acpihid_device_group(dev);
2115}
2116
Joerg Roedel431b2a22008-07-11 17:14:22 +02002117/*****************************************************************************
2118 *
2119 * The next functions belong to the dma_ops mapping/unmapping code.
2120 *
2121 *****************************************************************************/
2122
Joerg Roedelb1516a12016-07-06 13:07:22 +02002123static void __queue_flush(struct flush_queue *queue)
2124{
2125 struct protection_domain *domain;
2126 unsigned long flags;
2127 int idx;
2128
2129 /* First flush TLB of all known domains */
2130 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
2131 list_for_each_entry(domain, &amd_iommu_pd_list, list)
2132 domain_flush_tlb(domain);
2133 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
2134
2135 /* Wait until flushes have completed */
2136 domain_flush_complete(NULL);
2137
2138 for (idx = 0; idx < queue->next; ++idx) {
2139 struct flush_queue_entry *entry;
2140
2141 entry = queue->entries + idx;
2142
2143 free_iova_fast(&entry->dma_dom->iovad,
2144 entry->iova_pfn,
2145 entry->pages);
2146
2147 /* Not really necessary, just to make sure we catch any bugs */
2148 entry->dma_dom = NULL;
2149 }
2150
2151 queue->next = 0;
2152}
2153
2154static void queue_add(struct dma_ops_domain *dma_dom,
2155 unsigned long address, unsigned long pages)
2156{
2157 struct flush_queue_entry *entry;
2158 struct flush_queue *queue;
2159 unsigned long flags;
2160 int idx;
2161
2162 pages = __roundup_pow_of_two(pages);
2163 address >>= PAGE_SHIFT;
2164
2165 queue = get_cpu_ptr(&flush_queue);
2166 spin_lock_irqsave(&queue->lock, flags);
2167
2168 if (queue->next == FLUSH_QUEUE_SIZE)
2169 __queue_flush(queue);
2170
2171 idx = queue->next++;
2172 entry = queue->entries + idx;
2173
2174 entry->iova_pfn = address;
2175 entry->pages = pages;
2176 entry->dma_dom = dma_dom;
2177
2178 spin_unlock_irqrestore(&queue->lock, flags);
2179 put_cpu_ptr(&flush_queue);
2180}
2181
2182
Joerg Roedel431b2a22008-07-11 17:14:22 +02002183/*
2184 * In the dma_ops path we only have the struct device. This function
2185 * finds the corresponding IOMMU, the protection domain and the
2186 * requestor id for a given device.
2187 * If the device is not yet associated with a domain this is also done
2188 * in this function.
2189 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002190static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002191{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002192 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002193 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002194
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002195 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002196 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002197
Joerg Roedel063071d2015-05-28 18:41:38 +02002198 io_domain = iommu_get_domain_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002199 if (!io_domain)
2200 return NULL;
Joerg Roedel063071d2015-05-28 18:41:38 +02002201
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002202 domain = to_pdomain(io_domain);
2203 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002204 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002205
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002206 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002207}
2208
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002209static void update_device_table(struct protection_domain *domain)
2210{
Joerg Roedel492667d2009-11-27 13:25:47 +01002211 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002212
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002213 list_for_each_entry(dev_data, &domain->dev_list, list)
2214 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002215}
2216
2217static void update_domain(struct protection_domain *domain)
2218{
2219 if (!domain->updated)
2220 return;
2221
2222 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002223
2224 domain_flush_devices(domain);
2225 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002226
2227 domain->updated = false;
2228}
2229
Joerg Roedel431b2a22008-07-11 17:14:22 +02002230/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002231 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002232 * contiguous memory region into DMA address space. It is used by all
2233 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002234 * Must be called with the domain lock held.
2235 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002236static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002237 struct dma_ops_domain *dma_dom,
2238 phys_addr_t paddr,
2239 size_t size,
Joerg Roedel518d9b42016-07-05 14:39:47 +02002240 int direction,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002241 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002242{
2243 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002244 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002245 unsigned int pages;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002246 int prot = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002247 int i;
2248
Joerg Roedele3c449f2008-10-15 22:02:11 -07002249 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002250 paddr &= PAGE_MASK;
2251
Joerg Roedel256e4622016-07-05 14:23:01 +02002252 address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002253 if (address == DMA_ERROR_CODE)
2254 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002255
Joerg Roedel518d9b42016-07-05 14:39:47 +02002256 if (direction == DMA_TO_DEVICE)
2257 prot = IOMMU_PROT_IR;
2258 else if (direction == DMA_FROM_DEVICE)
2259 prot = IOMMU_PROT_IW;
2260 else if (direction == DMA_BIDIRECTIONAL)
2261 prot = IOMMU_PROT_IW | IOMMU_PROT_IR;
2262
Joerg Roedelcb76c322008-06-26 21:28:00 +02002263 start = address;
2264 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002265 ret = iommu_map_page(&dma_dom->domain, start, paddr,
2266 PAGE_SIZE, prot, GFP_ATOMIC);
2267 if (ret)
Joerg Roedel53812c12009-05-12 12:17:38 +02002268 goto out_unmap;
2269
Joerg Roedelcb76c322008-06-26 21:28:00 +02002270 paddr += PAGE_SIZE;
2271 start += PAGE_SIZE;
2272 }
2273 address += offset;
2274
Joerg Roedelab7032b2015-12-21 18:47:11 +01002275 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002276 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002277 domain_flush_complete(&dma_dom->domain);
2278 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002279
Joerg Roedelcb76c322008-06-26 21:28:00 +02002280out:
2281 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002282
2283out_unmap:
2284
2285 for (--i; i >= 0; --i) {
2286 start -= PAGE_SIZE;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002287 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedel53812c12009-05-12 12:17:38 +02002288 }
2289
Joerg Roedel256e4622016-07-05 14:23:01 +02002290 domain_flush_tlb(&dma_dom->domain);
2291 domain_flush_complete(&dma_dom->domain);
2292
2293 dma_ops_free_iova(dma_dom, address, pages);
Joerg Roedel53812c12009-05-12 12:17:38 +02002294
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002295 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002296}
2297
Joerg Roedel431b2a22008-07-11 17:14:22 +02002298/*
2299 * Does the reverse of the __map_single function. Must be called with
2300 * the domain lock held too
2301 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002302static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002303 dma_addr_t dma_addr,
2304 size_t size,
2305 int dir)
2306{
Joerg Roedel04e04632010-09-23 16:12:48 +02002307 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002308 dma_addr_t i, start;
2309 unsigned int pages;
2310
Joerg Roedel04e04632010-09-23 16:12:48 +02002311 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002312 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002313 dma_addr &= PAGE_MASK;
2314 start = dma_addr;
2315
2316 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002317 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002318 start += PAGE_SIZE;
2319 }
2320
Joerg Roedelb1516a12016-07-06 13:07:22 +02002321 if (amd_iommu_unmap_flush) {
2322 dma_ops_free_iova(dma_dom, dma_addr, pages);
2323 domain_flush_tlb(&dma_dom->domain);
2324 domain_flush_complete(&dma_dom->domain);
2325 } else {
2326 queue_add(dma_dom, dma_addr, pages);
2327 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002328}
2329
Joerg Roedel431b2a22008-07-11 17:14:22 +02002330/*
2331 * The exported map_single function for dma_ops.
2332 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002333static dma_addr_t map_page(struct device *dev, struct page *page,
2334 unsigned long offset, size_t size,
2335 enum dma_data_direction dir,
2336 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002337{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002338 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002339 struct protection_domain *domain;
2340 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002341
Joerg Roedel94f6d192009-11-24 16:40:02 +01002342 domain = get_domain(dev);
2343 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002344 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002345 else if (IS_ERR(domain))
2346 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002347
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002348 dma_mask = *dev->dma_mask;
2349
Joerg Roedelbda350d2016-07-05 16:28:02 +02002350 return __map_single(dev, domain->priv, paddr, size, dir, dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002351}
2352
Joerg Roedel431b2a22008-07-11 17:14:22 +02002353/*
2354 * The exported unmap_single function for dma_ops.
2355 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002356static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2357 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002358{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002359 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002360
Joerg Roedel94f6d192009-11-24 16:40:02 +01002361 domain = get_domain(dev);
2362 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002363 return;
2364
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002365 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002366}
2367
Joerg Roedel431b2a22008-07-11 17:14:22 +02002368/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002369 * The exported map_sg function for dma_ops (handles scatter-gather
2370 * lists).
2371 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002372static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002373 int nelems, enum dma_data_direction dir,
2374 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002375{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002376 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002377 int i;
2378 struct scatterlist *s;
2379 phys_addr_t paddr;
2380 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002381 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002382
Joerg Roedel94f6d192009-11-24 16:40:02 +01002383 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002384 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002385 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002386
Joerg Roedel832a90c2008-09-18 15:54:23 +02002387 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002388
Joerg Roedel65b050a2008-06-26 21:28:02 +02002389 for_each_sg(sglist, s, nelems, i) {
2390 paddr = sg_phys(s);
2391
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002392 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedelbda350d2016-07-05 16:28:02 +02002393 paddr, s->length, dir, dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002394
2395 if (s->dma_address) {
2396 s->dma_length = s->length;
2397 mapped_elems++;
2398 } else
2399 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002400 }
2401
Joerg Roedel65b050a2008-06-26 21:28:02 +02002402 return mapped_elems;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002403
Joerg Roedel65b050a2008-06-26 21:28:02 +02002404unmap:
2405 for_each_sg(sglist, s, mapped_elems, i) {
2406 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002407 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002408 s->dma_length, dir);
2409 s->dma_address = s->dma_length = 0;
2410 }
2411
Joerg Roedel92d420e2015-12-21 19:31:33 +01002412 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002413}
2414
Joerg Roedel431b2a22008-07-11 17:14:22 +02002415/*
2416 * The exported map_sg function for dma_ops (handles scatter-gather
2417 * lists).
2418 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002419static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002420 int nelems, enum dma_data_direction dir,
2421 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002422{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002423 struct protection_domain *domain;
2424 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002425 int i;
2426
Joerg Roedel94f6d192009-11-24 16:40:02 +01002427 domain = get_domain(dev);
2428 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002429 return;
2430
Joerg Roedel65b050a2008-06-26 21:28:02 +02002431 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002432 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002433 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002434 s->dma_address = s->dma_length = 0;
2435 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002436}
2437
Joerg Roedel431b2a22008-07-11 17:14:22 +02002438/*
2439 * The exported alloc_coherent function for dma_ops.
2440 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002441static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002442 dma_addr_t *dma_addr, gfp_t flag,
2443 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002444{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002445 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002446 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002447 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002448
Joerg Roedel94f6d192009-11-24 16:40:02 +01002449 domain = get_domain(dev);
2450 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002451 page = alloc_pages(flag, get_order(size));
2452 *dma_addr = page_to_phys(page);
2453 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002454 } else if (IS_ERR(domain))
2455 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002456
Joerg Roedel3b839a52015-04-01 14:58:47 +02002457 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002458 dma_mask = dev->coherent_dma_mask;
2459 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002460 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002461
Joerg Roedel3b839a52015-04-01 14:58:47 +02002462 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2463 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002464 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002465 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002466
Joerg Roedel3b839a52015-04-01 14:58:47 +02002467 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2468 get_order(size));
2469 if (!page)
2470 return NULL;
2471 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002472
Joerg Roedel832a90c2008-09-18 15:54:23 +02002473 if (!dma_mask)
2474 dma_mask = *dev->dma_mask;
2475
Joerg Roedel3b839a52015-04-01 14:58:47 +02002476 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedelbda350d2016-07-05 16:28:02 +02002477 size, DMA_BIDIRECTIONAL, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002478
Joerg Roedel92d420e2015-12-21 19:31:33 +01002479 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002480 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002481
Joerg Roedel3b839a52015-04-01 14:58:47 +02002482 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002483
2484out_free:
2485
Joerg Roedel3b839a52015-04-01 14:58:47 +02002486 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2487 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002488
2489 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002490}
2491
Joerg Roedel431b2a22008-07-11 17:14:22 +02002492/*
2493 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002494 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002495static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002496 void *virt_addr, dma_addr_t dma_addr,
2497 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002498{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002499 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002500 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002501
Joerg Roedel3b839a52015-04-01 14:58:47 +02002502 page = virt_to_page(virt_addr);
2503 size = PAGE_ALIGN(size);
2504
Joerg Roedel94f6d192009-11-24 16:40:02 +01002505 domain = get_domain(dev);
2506 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002507 goto free_mem;
2508
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002509 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002510
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002511free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002512 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2513 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002514}
2515
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002516/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002517 * This function is called by the DMA layer to find out if we can handle a
2518 * particular device. It is part of the dma_ops.
2519 */
2520static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2521{
Joerg Roedel420aef82009-11-23 16:14:57 +01002522 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002523}
2524
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002525static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002526 .alloc = alloc_coherent,
2527 .free = free_coherent,
2528 .map_page = map_page,
2529 .unmap_page = unmap_page,
2530 .map_sg = map_sg,
2531 .unmap_sg = unmap_sg,
2532 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002533};
2534
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002535static int init_reserved_iova_ranges(void)
2536{
2537 struct pci_dev *pdev = NULL;
2538 struct iova *val;
2539
2540 init_iova_domain(&reserved_iova_ranges, PAGE_SIZE,
2541 IOVA_START_PFN, DMA_32BIT_PFN);
2542
2543 lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
2544 &reserved_rbtree_key);
2545
2546 /* MSI memory range */
2547 val = reserve_iova(&reserved_iova_ranges,
2548 IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
2549 if (!val) {
2550 pr_err("Reserving MSI range failed\n");
2551 return -ENOMEM;
2552 }
2553
2554 /* HT memory range */
2555 val = reserve_iova(&reserved_iova_ranges,
2556 IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
2557 if (!val) {
2558 pr_err("Reserving HT range failed\n");
2559 return -ENOMEM;
2560 }
2561
2562 /*
2563 * Memory used for PCI resources
2564 * FIXME: Check whether we can reserve the PCI-hole completly
2565 */
2566 for_each_pci_dev(pdev) {
2567 int i;
2568
2569 for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
2570 struct resource *r = &pdev->resource[i];
2571
2572 if (!(r->flags & IORESOURCE_MEM))
2573 continue;
2574
2575 val = reserve_iova(&reserved_iova_ranges,
2576 IOVA_PFN(r->start),
2577 IOVA_PFN(r->end));
2578 if (!val) {
2579 pr_err("Reserve pci-resource range failed\n");
2580 return -ENOMEM;
2581 }
2582 }
2583 }
2584
2585 return 0;
2586}
2587
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002588int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002589{
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002590 int ret, cpu, err = 0;
Joerg Roedel307d5852016-07-05 11:54:04 +02002591
2592 ret = iova_cache_get();
2593 if (ret)
2594 return ret;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002595
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002596 ret = init_reserved_iova_ranges();
2597 if (ret)
2598 return ret;
2599
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002600 for_each_possible_cpu(cpu) {
2601 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2602
2603 queue->entries = kzalloc(FLUSH_QUEUE_SIZE *
2604 sizeof(*queue->entries),
2605 GFP_KERNEL);
2606 if (!queue->entries)
2607 goto out_put_iova;
2608
2609 spin_lock_init(&queue->lock);
2610 }
2611
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002612 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2613 if (err)
2614 return err;
2615#ifdef CONFIG_ARM_AMBA
2616 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2617 if (err)
2618 return err;
2619#endif
Wan Zongshun0076cd32016-05-10 09:21:01 -04002620 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2621 if (err)
2622 return err;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002623 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002624
2625out_put_iova:
2626 for_each_possible_cpu(cpu) {
2627 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2628
2629 kfree(queue->entries);
2630 }
2631
2632 return -ENOMEM;
Joerg Roedelf5325092010-01-22 17:44:35 +01002633}
2634
Joerg Roedel6631ee92008-06-26 21:28:05 +02002635int __init amd_iommu_init_dma_ops(void)
2636{
Joerg Roedel32302322015-07-28 16:58:50 +02002637 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002638 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002639
Joerg Roedel52717822015-07-28 16:58:51 +02002640 /*
2641 * In case we don't initialize SWIOTLB (actually the common case
2642 * when AMD IOMMU is enabled), make sure there are global
2643 * dma_ops set as a fall-back for devices not handled by this
2644 * driver (for example non-PCI devices).
2645 */
2646 if (!swiotlb)
2647 dma_ops = &nommu_dma_ops;
2648
Joerg Roedel62410ee2012-06-12 16:42:43 +02002649 if (amd_iommu_unmap_flush)
2650 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2651 else
2652 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2653
Joerg Roedel6631ee92008-06-26 21:28:05 +02002654 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002655
Joerg Roedel6631ee92008-06-26 21:28:05 +02002656}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002657
2658/*****************************************************************************
2659 *
2660 * The following functions belong to the exported interface of AMD IOMMU
2661 *
2662 * This interface allows access to lower level functions of the IOMMU
2663 * like protection domain handling and assignement of devices to domains
2664 * which is not possible with the dma_ops interface.
2665 *
2666 *****************************************************************************/
2667
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002668static void cleanup_domain(struct protection_domain *domain)
2669{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002670 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002671 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002672
2673 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2674
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002675 while (!list_empty(&domain->dev_list)) {
2676 entry = list_first_entry(&domain->dev_list,
2677 struct iommu_dev_data, list);
2678 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002679 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002680
2681 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2682}
2683
Joerg Roedel26508152009-08-26 16:52:40 +02002684static void protection_domain_free(struct protection_domain *domain)
2685{
2686 if (!domain)
2687 return;
2688
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002689 del_domain_from_list(domain);
2690
Joerg Roedel26508152009-08-26 16:52:40 +02002691 if (domain->id)
2692 domain_id_free(domain->id);
2693
2694 kfree(domain);
2695}
2696
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002697static int protection_domain_init(struct protection_domain *domain)
2698{
2699 spin_lock_init(&domain->lock);
2700 mutex_init(&domain->api_lock);
2701 domain->id = domain_id_alloc();
2702 if (!domain->id)
2703 return -ENOMEM;
2704 INIT_LIST_HEAD(&domain->dev_list);
2705
2706 return 0;
2707}
2708
Joerg Roedel26508152009-08-26 16:52:40 +02002709static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002710{
2711 struct protection_domain *domain;
2712
2713 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2714 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002715 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002716
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002717 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002718 goto out_err;
2719
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002720 add_domain_to_list(domain);
2721
Joerg Roedel26508152009-08-26 16:52:40 +02002722 return domain;
2723
2724out_err:
2725 kfree(domain);
2726
2727 return NULL;
2728}
2729
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002730static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2731{
2732 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002733 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002734
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002735 switch (type) {
2736 case IOMMU_DOMAIN_UNMANAGED:
2737 pdomain = protection_domain_alloc();
2738 if (!pdomain)
2739 return NULL;
2740
2741 pdomain->mode = PAGE_MODE_3_LEVEL;
2742 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2743 if (!pdomain->pt_root) {
2744 protection_domain_free(pdomain);
2745 return NULL;
2746 }
2747
2748 pdomain->domain.geometry.aperture_start = 0;
2749 pdomain->domain.geometry.aperture_end = ~0ULL;
2750 pdomain->domain.geometry.force_aperture = true;
2751
2752 break;
2753 case IOMMU_DOMAIN_DMA:
2754 dma_domain = dma_ops_domain_alloc();
2755 if (!dma_domain) {
2756 pr_err("AMD-Vi: Failed to allocate\n");
2757 return NULL;
2758 }
2759 pdomain = &dma_domain->domain;
2760 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002761 case IOMMU_DOMAIN_IDENTITY:
2762 pdomain = protection_domain_alloc();
2763 if (!pdomain)
2764 return NULL;
2765
2766 pdomain->mode = PAGE_MODE_NONE;
2767 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002768 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002769 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002770 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002771
2772 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002773}
2774
2775static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002776{
2777 struct protection_domain *domain;
2778
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002779 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01002780 return;
2781
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002782 domain = to_pdomain(dom);
2783
Joerg Roedel98383fc2008-12-02 18:34:12 +01002784 if (domain->dev_cnt > 0)
2785 cleanup_domain(domain);
2786
2787 BUG_ON(domain->dev_cnt != 0);
2788
Joerg Roedel132bd682011-11-17 14:18:46 +01002789 if (domain->mode != PAGE_MODE_NONE)
2790 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002791
Joerg Roedel52815b72011-11-17 17:24:28 +01002792 if (domain->flags & PD_IOMMUV2_MASK)
2793 free_gcr3_table(domain);
2794
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002795 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002796}
2797
Joerg Roedel684f2882008-12-08 12:07:44 +01002798static void amd_iommu_detach_device(struct iommu_domain *dom,
2799 struct device *dev)
2800{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002801 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002802 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002803 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01002804
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002805 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002806 return;
2807
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002808 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002809 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002810 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01002811
Joerg Roedel657cbb62009-11-23 15:26:46 +01002812 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002813 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002814
2815 iommu = amd_iommu_rlookup_table[devid];
2816 if (!iommu)
2817 return;
2818
Joerg Roedel684f2882008-12-08 12:07:44 +01002819 iommu_completion_wait(iommu);
2820}
2821
Joerg Roedel01106062008-12-02 19:34:11 +01002822static int amd_iommu_attach_device(struct iommu_domain *dom,
2823 struct device *dev)
2824{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002825 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002826 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002827 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002828 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002829
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002830 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002831 return -EINVAL;
2832
Joerg Roedel657cbb62009-11-23 15:26:46 +01002833 dev_data = dev->archdata.iommu;
2834
Joerg Roedelf62dda62011-06-09 12:55:35 +02002835 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002836 if (!iommu)
2837 return -EINVAL;
2838
Joerg Roedel657cbb62009-11-23 15:26:46 +01002839 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002840 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002841
Joerg Roedel15898bb2009-11-24 15:39:42 +01002842 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002843
2844 iommu_completion_wait(iommu);
2845
Joerg Roedel15898bb2009-11-24 15:39:42 +01002846 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002847}
2848
Joerg Roedel468e2362010-01-21 16:37:36 +01002849static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002850 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002851{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002852 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002853 int prot = 0;
2854 int ret;
2855
Joerg Roedel132bd682011-11-17 14:18:46 +01002856 if (domain->mode == PAGE_MODE_NONE)
2857 return -EINVAL;
2858
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002859 if (iommu_prot & IOMMU_READ)
2860 prot |= IOMMU_PROT_IR;
2861 if (iommu_prot & IOMMU_WRITE)
2862 prot |= IOMMU_PROT_IW;
2863
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002864 mutex_lock(&domain->api_lock);
Joerg Roedelb911b892016-07-05 14:29:11 +02002865 ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002866 mutex_unlock(&domain->api_lock);
2867
Joerg Roedel795e74f2010-05-11 17:40:57 +02002868 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002869}
2870
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002871static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2872 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002873{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002874 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002875 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002876
Joerg Roedel132bd682011-11-17 14:18:46 +01002877 if (domain->mode == PAGE_MODE_NONE)
2878 return -EINVAL;
2879
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002880 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002881 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002882 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002883
Joerg Roedel17b124b2011-04-06 18:01:35 +02002884 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002885
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002886 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002887}
2888
Joerg Roedel645c4c82008-12-02 20:05:50 +01002889static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05302890 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01002891{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002892 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02002893 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002894 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002895
Joerg Roedel132bd682011-11-17 14:18:46 +01002896 if (domain->mode == PAGE_MODE_NONE)
2897 return iova;
2898
Joerg Roedel3039ca12015-04-01 14:58:48 +02002899 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002900
Joerg Roedela6d41a42009-09-02 17:08:55 +02002901 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002902 return 0;
2903
Joerg Roedelb24b1b62015-04-01 14:58:51 +02002904 offset_mask = pte_pgsize - 1;
2905 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002906
Joerg Roedelb24b1b62015-04-01 14:58:51 +02002907 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002908}
2909
Joerg Roedelab636482014-09-05 10:48:21 +02002910static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002911{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002912 switch (cap) {
2913 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02002914 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02002915 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02002916 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00002917 case IOMMU_CAP_NOEXEC:
2918 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02002919 }
2920
Joerg Roedelab636482014-09-05 10:48:21 +02002921 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002922}
2923
Joerg Roedel35cf2482015-05-28 18:41:37 +02002924static void amd_iommu_get_dm_regions(struct device *dev,
2925 struct list_head *head)
2926{
2927 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002928 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02002929
2930 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002931 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002932 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02002933
2934 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
2935 struct iommu_dm_region *region;
2936
2937 if (devid < entry->devid_start || devid > entry->devid_end)
2938 continue;
2939
2940 region = kzalloc(sizeof(*region), GFP_KERNEL);
2941 if (!region) {
2942 pr_err("Out of memory allocating dm-regions for %s\n",
2943 dev_name(dev));
2944 return;
2945 }
2946
2947 region->start = entry->address_start;
2948 region->length = entry->address_end - entry->address_start;
2949 if (entry->prot & IOMMU_PROT_IR)
2950 region->prot |= IOMMU_READ;
2951 if (entry->prot & IOMMU_PROT_IW)
2952 region->prot |= IOMMU_WRITE;
2953
2954 list_add_tail(&region->list, head);
2955 }
2956}
2957
2958static void amd_iommu_put_dm_regions(struct device *dev,
2959 struct list_head *head)
2960{
2961 struct iommu_dm_region *entry, *next;
2962
2963 list_for_each_entry_safe(entry, next, head, list)
2964 kfree(entry);
2965}
2966
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02002967static void amd_iommu_apply_dm_region(struct device *dev,
2968 struct iommu_domain *domain,
2969 struct iommu_dm_region *region)
2970{
2971 struct protection_domain *pdomain = to_pdomain(domain);
2972 struct dma_ops_domain *dma_dom = pdomain->priv;
2973 unsigned long start, end;
2974
2975 start = IOVA_PFN(region->start);
2976 end = IOVA_PFN(region->start + region->length);
2977
2978 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
2979}
2980
Thierry Redingb22f6432014-06-27 09:03:12 +02002981static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02002982 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002983 .domain_alloc = amd_iommu_domain_alloc,
2984 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002985 .attach_dev = amd_iommu_attach_device,
2986 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002987 .map = amd_iommu_map,
2988 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07002989 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002990 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002991 .add_device = amd_iommu_add_device,
2992 .remove_device = amd_iommu_remove_device,
Wan Zongshunb097d112016-04-01 09:06:04 -04002993 .device_group = amd_iommu_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02002994 .get_dm_regions = amd_iommu_get_dm_regions,
2995 .put_dm_regions = amd_iommu_put_dm_regions,
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02002996 .apply_dm_region = amd_iommu_apply_dm_region,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02002997 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002998};
2999
Joerg Roedel0feae532009-08-26 15:26:30 +02003000/*****************************************************************************
3001 *
3002 * The next functions do a basic initialization of IOMMU for pass through
3003 * mode
3004 *
3005 * In passthrough mode the IOMMU is initialized and enabled but not used for
3006 * DMA-API translation.
3007 *
3008 *****************************************************************************/
3009
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003010/* IOMMUv2 specific functions */
3011int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3012{
3013 return atomic_notifier_chain_register(&ppr_notifier, nb);
3014}
3015EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3016
3017int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3018{
3019 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3020}
3021EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003022
3023void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3024{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003025 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003026 unsigned long flags;
3027
3028 spin_lock_irqsave(&domain->lock, flags);
3029
3030 /* Update data structure */
3031 domain->mode = PAGE_MODE_NONE;
3032 domain->updated = true;
3033
3034 /* Make changes visible to IOMMUs */
3035 update_domain(domain);
3036
3037 /* Page-table is not visible to IOMMU anymore, so free it */
3038 free_pagetable(domain);
3039
3040 spin_unlock_irqrestore(&domain->lock, flags);
3041}
3042EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003043
3044int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3045{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003046 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003047 unsigned long flags;
3048 int levels, ret;
3049
3050 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3051 return -EINVAL;
3052
3053 /* Number of GCR3 table levels required */
3054 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3055 levels += 1;
3056
3057 if (levels > amd_iommu_max_glx_val)
3058 return -EINVAL;
3059
3060 spin_lock_irqsave(&domain->lock, flags);
3061
3062 /*
3063 * Save us all sanity checks whether devices already in the
3064 * domain support IOMMUv2. Just force that the domain has no
3065 * devices attached when it is switched into IOMMUv2 mode.
3066 */
3067 ret = -EBUSY;
3068 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3069 goto out;
3070
3071 ret = -ENOMEM;
3072 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3073 if (domain->gcr3_tbl == NULL)
3074 goto out;
3075
3076 domain->glx = levels;
3077 domain->flags |= PD_IOMMUV2_MASK;
3078 domain->updated = true;
3079
3080 update_domain(domain);
3081
3082 ret = 0;
3083
3084out:
3085 spin_unlock_irqrestore(&domain->lock, flags);
3086
3087 return ret;
3088}
3089EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003090
3091static int __flush_pasid(struct protection_domain *domain, int pasid,
3092 u64 address, bool size)
3093{
3094 struct iommu_dev_data *dev_data;
3095 struct iommu_cmd cmd;
3096 int i, ret;
3097
3098 if (!(domain->flags & PD_IOMMUV2_MASK))
3099 return -EINVAL;
3100
3101 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3102
3103 /*
3104 * IOMMU TLB needs to be flushed before Device TLB to
3105 * prevent device TLB refill from IOMMU TLB
3106 */
3107 for (i = 0; i < amd_iommus_present; ++i) {
3108 if (domain->dev_iommu[i] == 0)
3109 continue;
3110
3111 ret = iommu_queue_command(amd_iommus[i], &cmd);
3112 if (ret != 0)
3113 goto out;
3114 }
3115
3116 /* Wait until IOMMU TLB flushes are complete */
3117 domain_flush_complete(domain);
3118
3119 /* Now flush device TLBs */
3120 list_for_each_entry(dev_data, &domain->dev_list, list) {
3121 struct amd_iommu *iommu;
3122 int qdep;
3123
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003124 /*
3125 There might be non-IOMMUv2 capable devices in an IOMMUv2
3126 * domain.
3127 */
3128 if (!dev_data->ats.enabled)
3129 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003130
3131 qdep = dev_data->ats.qdep;
3132 iommu = amd_iommu_rlookup_table[dev_data->devid];
3133
3134 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3135 qdep, address, size);
3136
3137 ret = iommu_queue_command(iommu, &cmd);
3138 if (ret != 0)
3139 goto out;
3140 }
3141
3142 /* Wait until all device TLBs are flushed */
3143 domain_flush_complete(domain);
3144
3145 ret = 0;
3146
3147out:
3148
3149 return ret;
3150}
3151
3152static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3153 u64 address)
3154{
3155 return __flush_pasid(domain, pasid, address, false);
3156}
3157
3158int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3159 u64 address)
3160{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003161 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003162 unsigned long flags;
3163 int ret;
3164
3165 spin_lock_irqsave(&domain->lock, flags);
3166 ret = __amd_iommu_flush_page(domain, pasid, address);
3167 spin_unlock_irqrestore(&domain->lock, flags);
3168
3169 return ret;
3170}
3171EXPORT_SYMBOL(amd_iommu_flush_page);
3172
3173static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3174{
3175 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3176 true);
3177}
3178
3179int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3180{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003181 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003182 unsigned long flags;
3183 int ret;
3184
3185 spin_lock_irqsave(&domain->lock, flags);
3186 ret = __amd_iommu_flush_tlb(domain, pasid);
3187 spin_unlock_irqrestore(&domain->lock, flags);
3188
3189 return ret;
3190}
3191EXPORT_SYMBOL(amd_iommu_flush_tlb);
3192
Joerg Roedelb16137b2011-11-21 16:50:23 +01003193static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3194{
3195 int index;
3196 u64 *pte;
3197
3198 while (true) {
3199
3200 index = (pasid >> (9 * level)) & 0x1ff;
3201 pte = &root[index];
3202
3203 if (level == 0)
3204 break;
3205
3206 if (!(*pte & GCR3_VALID)) {
3207 if (!alloc)
3208 return NULL;
3209
3210 root = (void *)get_zeroed_page(GFP_ATOMIC);
3211 if (root == NULL)
3212 return NULL;
3213
3214 *pte = __pa(root) | GCR3_VALID;
3215 }
3216
3217 root = __va(*pte & PAGE_MASK);
3218
3219 level -= 1;
3220 }
3221
3222 return pte;
3223}
3224
3225static int __set_gcr3(struct protection_domain *domain, int pasid,
3226 unsigned long cr3)
3227{
3228 u64 *pte;
3229
3230 if (domain->mode != PAGE_MODE_NONE)
3231 return -EINVAL;
3232
3233 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3234 if (pte == NULL)
3235 return -ENOMEM;
3236
3237 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3238
3239 return __amd_iommu_flush_tlb(domain, pasid);
3240}
3241
3242static int __clear_gcr3(struct protection_domain *domain, int pasid)
3243{
3244 u64 *pte;
3245
3246 if (domain->mode != PAGE_MODE_NONE)
3247 return -EINVAL;
3248
3249 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3250 if (pte == NULL)
3251 return 0;
3252
3253 *pte = 0;
3254
3255 return __amd_iommu_flush_tlb(domain, pasid);
3256}
3257
3258int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3259 unsigned long cr3)
3260{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003261 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003262 unsigned long flags;
3263 int ret;
3264
3265 spin_lock_irqsave(&domain->lock, flags);
3266 ret = __set_gcr3(domain, pasid, cr3);
3267 spin_unlock_irqrestore(&domain->lock, flags);
3268
3269 return ret;
3270}
3271EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3272
3273int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3274{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003275 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003276 unsigned long flags;
3277 int ret;
3278
3279 spin_lock_irqsave(&domain->lock, flags);
3280 ret = __clear_gcr3(domain, pasid);
3281 spin_unlock_irqrestore(&domain->lock, flags);
3282
3283 return ret;
3284}
3285EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003286
3287int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3288 int status, int tag)
3289{
3290 struct iommu_dev_data *dev_data;
3291 struct amd_iommu *iommu;
3292 struct iommu_cmd cmd;
3293
3294 dev_data = get_dev_data(&pdev->dev);
3295 iommu = amd_iommu_rlookup_table[dev_data->devid];
3296
3297 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3298 tag, dev_data->pri_tlp);
3299
3300 return iommu_queue_command(iommu, &cmd);
3301}
3302EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003303
3304struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3305{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003306 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003307
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003308 pdomain = get_domain(&pdev->dev);
3309 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003310 return NULL;
3311
3312 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003313 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003314 return NULL;
3315
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003316 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003317}
3318EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003319
3320void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3321{
3322 struct iommu_dev_data *dev_data;
3323
3324 if (!amd_iommu_v2_supported())
3325 return;
3326
3327 dev_data = get_dev_data(&pdev->dev);
3328 dev_data->errata |= (1 << erratum);
3329}
3330EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003331
3332int amd_iommu_device_info(struct pci_dev *pdev,
3333 struct amd_iommu_device_info *info)
3334{
3335 int max_pasids;
3336 int pos;
3337
3338 if (pdev == NULL || info == NULL)
3339 return -EINVAL;
3340
3341 if (!amd_iommu_v2_supported())
3342 return -EINVAL;
3343
3344 memset(info, 0, sizeof(*info));
3345
3346 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3347 if (pos)
3348 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3349
3350 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3351 if (pos)
3352 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3353
3354 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3355 if (pos) {
3356 int features;
3357
3358 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3359 max_pasids = min(max_pasids, (1 << 20));
3360
3361 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3362 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3363
3364 features = pci_pasid_features(pdev);
3365 if (features & PCI_PASID_CAP_EXEC)
3366 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3367 if (features & PCI_PASID_CAP_PRIV)
3368 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3369 }
3370
3371 return 0;
3372}
3373EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003374
3375#ifdef CONFIG_IRQ_REMAP
3376
3377/*****************************************************************************
3378 *
3379 * Interrupt Remapping Implementation
3380 *
3381 *****************************************************************************/
3382
3383union irte {
3384 u32 val;
3385 struct {
3386 u32 valid : 1,
3387 no_fault : 1,
3388 int_type : 3,
3389 rq_eoi : 1,
3390 dm : 1,
3391 rsvd_1 : 1,
3392 destination : 8,
3393 vector : 8,
3394 rsvd_2 : 8;
3395 } fields;
3396};
3397
Jiang Liu9c724962015-04-14 10:29:52 +08003398struct irq_2_irte {
3399 u16 devid; /* Device ID for IRTE table */
3400 u16 index; /* Index into IRTE table*/
3401};
3402
Jiang Liu7c71d302015-04-13 14:11:33 +08003403struct amd_ir_data {
3404 struct irq_2_irte irq_2_irte;
3405 union irte irte_entry;
3406 union {
3407 struct msi_msg msi_entry;
3408 };
3409};
3410
3411static struct irq_chip amd_ir_chip;
3412
Joerg Roedel2b324502012-06-21 16:29:10 +02003413#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3414#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3415#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3416#define DTE_IRQ_REMAP_ENABLE 1ULL
3417
3418static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3419{
3420 u64 dte;
3421
3422 dte = amd_iommu_dev_table[devid].data[2];
3423 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3424 dte |= virt_to_phys(table->table);
3425 dte |= DTE_IRQ_REMAP_INTCTL;
3426 dte |= DTE_IRQ_TABLE_LEN;
3427 dte |= DTE_IRQ_REMAP_ENABLE;
3428
3429 amd_iommu_dev_table[devid].data[2] = dte;
3430}
3431
3432#define IRTE_ALLOCATED (~1U)
3433
3434static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3435{
3436 struct irq_remap_table *table = NULL;
3437 struct amd_iommu *iommu;
3438 unsigned long flags;
3439 u16 alias;
3440
3441 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3442
3443 iommu = amd_iommu_rlookup_table[devid];
3444 if (!iommu)
3445 goto out_unlock;
3446
3447 table = irq_lookup_table[devid];
3448 if (table)
3449 goto out;
3450
3451 alias = amd_iommu_alias_table[devid];
3452 table = irq_lookup_table[alias];
3453 if (table) {
3454 irq_lookup_table[devid] = table;
3455 set_dte_irq_entry(devid, table);
3456 iommu_flush_dte(iommu, devid);
3457 goto out;
3458 }
3459
3460 /* Nothing there yet, allocate new irq remapping table */
3461 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3462 if (!table)
3463 goto out;
3464
Joerg Roedel197887f2013-04-09 21:14:08 +02003465 /* Initialize table spin-lock */
3466 spin_lock_init(&table->lock);
3467
Joerg Roedel2b324502012-06-21 16:29:10 +02003468 if (ioapic)
3469 /* Keep the first 32 indexes free for IOAPIC interrupts */
3470 table->min_index = 32;
3471
3472 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3473 if (!table->table) {
3474 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003475 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003476 goto out;
3477 }
3478
3479 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3480
3481 if (ioapic) {
3482 int i;
3483
3484 for (i = 0; i < 32; ++i)
3485 table->table[i] = IRTE_ALLOCATED;
3486 }
3487
3488 irq_lookup_table[devid] = table;
3489 set_dte_irq_entry(devid, table);
3490 iommu_flush_dte(iommu, devid);
3491 if (devid != alias) {
3492 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003493 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003494 iommu_flush_dte(iommu, alias);
3495 }
3496
3497out:
3498 iommu_completion_wait(iommu);
3499
3500out_unlock:
3501 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3502
3503 return table;
3504}
3505
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003506static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003507{
3508 struct irq_remap_table *table;
3509 unsigned long flags;
3510 int index, c;
3511
3512 table = get_irq_table(devid, false);
3513 if (!table)
3514 return -ENODEV;
3515
3516 spin_lock_irqsave(&table->lock, flags);
3517
3518 /* Scan table for free entries */
3519 for (c = 0, index = table->min_index;
3520 index < MAX_IRQS_PER_TABLE;
3521 ++index) {
3522 if (table->table[index] == 0)
3523 c += 1;
3524 else
3525 c = 0;
3526
3527 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003528 for (; c != 0; --c)
3529 table->table[index - c + 1] = IRTE_ALLOCATED;
3530
3531 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003532 goto out;
3533 }
3534 }
3535
3536 index = -ENOSPC;
3537
3538out:
3539 spin_unlock_irqrestore(&table->lock, flags);
3540
3541 return index;
3542}
3543
Joerg Roedel2b324502012-06-21 16:29:10 +02003544static int modify_irte(u16 devid, int index, union irte irte)
3545{
3546 struct irq_remap_table *table;
3547 struct amd_iommu *iommu;
3548 unsigned long flags;
3549
3550 iommu = amd_iommu_rlookup_table[devid];
3551 if (iommu == NULL)
3552 return -EINVAL;
3553
3554 table = get_irq_table(devid, false);
3555 if (!table)
3556 return -ENOMEM;
3557
3558 spin_lock_irqsave(&table->lock, flags);
3559 table->table[index] = irte.val;
3560 spin_unlock_irqrestore(&table->lock, flags);
3561
3562 iommu_flush_irt(iommu, devid);
3563 iommu_completion_wait(iommu);
3564
3565 return 0;
3566}
3567
3568static void free_irte(u16 devid, int index)
3569{
3570 struct irq_remap_table *table;
3571 struct amd_iommu *iommu;
3572 unsigned long flags;
3573
3574 iommu = amd_iommu_rlookup_table[devid];
3575 if (iommu == NULL)
3576 return;
3577
3578 table = get_irq_table(devid, false);
3579 if (!table)
3580 return;
3581
3582 spin_lock_irqsave(&table->lock, flags);
3583 table->table[index] = 0;
3584 spin_unlock_irqrestore(&table->lock, flags);
3585
3586 iommu_flush_irt(iommu, devid);
3587 iommu_completion_wait(iommu);
3588}
3589
Jiang Liu7c71d302015-04-13 14:11:33 +08003590static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003591{
Jiang Liu7c71d302015-04-13 14:11:33 +08003592 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003593
Jiang Liu7c71d302015-04-13 14:11:33 +08003594 switch (info->type) {
3595 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3596 devid = get_ioapic_devid(info->ioapic_id);
3597 break;
3598 case X86_IRQ_ALLOC_TYPE_HPET:
3599 devid = get_hpet_devid(info->hpet_id);
3600 break;
3601 case X86_IRQ_ALLOC_TYPE_MSI:
3602 case X86_IRQ_ALLOC_TYPE_MSIX:
3603 devid = get_device_id(&info->msi_dev->dev);
3604 break;
3605 default:
3606 BUG_ON(1);
3607 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003608 }
3609
Jiang Liu7c71d302015-04-13 14:11:33 +08003610 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003611}
3612
Jiang Liu7c71d302015-04-13 14:11:33 +08003613static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003614{
Jiang Liu7c71d302015-04-13 14:11:33 +08003615 struct amd_iommu *iommu;
3616 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003617
Jiang Liu7c71d302015-04-13 14:11:33 +08003618 if (!info)
3619 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003620
Jiang Liu7c71d302015-04-13 14:11:33 +08003621 devid = get_devid(info);
3622 if (devid >= 0) {
3623 iommu = amd_iommu_rlookup_table[devid];
3624 if (iommu)
3625 return iommu->ir_domain;
3626 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003627
Jiang Liu7c71d302015-04-13 14:11:33 +08003628 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003629}
3630
Jiang Liu7c71d302015-04-13 14:11:33 +08003631static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003632{
Jiang Liu7c71d302015-04-13 14:11:33 +08003633 struct amd_iommu *iommu;
3634 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003635
Jiang Liu7c71d302015-04-13 14:11:33 +08003636 if (!info)
3637 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003638
Jiang Liu7c71d302015-04-13 14:11:33 +08003639 switch (info->type) {
3640 case X86_IRQ_ALLOC_TYPE_MSI:
3641 case X86_IRQ_ALLOC_TYPE_MSIX:
3642 devid = get_device_id(&info->msi_dev->dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003643 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003644 return NULL;
3645
Dan Carpenter1fb260b2016-01-07 12:36:06 +03003646 iommu = amd_iommu_rlookup_table[devid];
3647 if (iommu)
3648 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08003649 break;
3650 default:
3651 break;
3652 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003653
Jiang Liu7c71d302015-04-13 14:11:33 +08003654 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003655}
3656
Joerg Roedel6b474b82012-06-26 16:46:04 +02003657struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003658 .prepare = amd_iommu_prepare,
3659 .enable = amd_iommu_enable,
3660 .disable = amd_iommu_disable,
3661 .reenable = amd_iommu_reenable,
3662 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003663 .get_ir_irq_domain = get_ir_irq_domain,
3664 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003665};
Jiang Liu7c71d302015-04-13 14:11:33 +08003666
3667static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3668 struct irq_cfg *irq_cfg,
3669 struct irq_alloc_info *info,
3670 int devid, int index, int sub_handle)
3671{
3672 struct irq_2_irte *irte_info = &data->irq_2_irte;
3673 struct msi_msg *msg = &data->msi_entry;
3674 union irte *irte = &data->irte_entry;
3675 struct IO_APIC_route_entry *entry;
3676
Jiang Liu7c71d302015-04-13 14:11:33 +08003677 data->irq_2_irte.devid = devid;
3678 data->irq_2_irte.index = index + sub_handle;
3679
3680 /* Setup IRTE for IOMMU */
3681 irte->val = 0;
3682 irte->fields.vector = irq_cfg->vector;
3683 irte->fields.int_type = apic->irq_delivery_mode;
3684 irte->fields.destination = irq_cfg->dest_apicid;
3685 irte->fields.dm = apic->irq_dest_mode;
3686 irte->fields.valid = 1;
3687
3688 switch (info->type) {
3689 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3690 /* Setup IOAPIC entry */
3691 entry = info->ioapic_entry;
3692 info->ioapic_entry = NULL;
3693 memset(entry, 0, sizeof(*entry));
3694 entry->vector = index;
3695 entry->mask = 0;
3696 entry->trigger = info->ioapic_trigger;
3697 entry->polarity = info->ioapic_polarity;
3698 /* Mask level triggered irqs. */
3699 if (info->ioapic_trigger)
3700 entry->mask = 1;
3701 break;
3702
3703 case X86_IRQ_ALLOC_TYPE_HPET:
3704 case X86_IRQ_ALLOC_TYPE_MSI:
3705 case X86_IRQ_ALLOC_TYPE_MSIX:
3706 msg->address_hi = MSI_ADDR_BASE_HI;
3707 msg->address_lo = MSI_ADDR_BASE_LO;
3708 msg->data = irte_info->index;
3709 break;
3710
3711 default:
3712 BUG_ON(1);
3713 break;
3714 }
3715}
3716
3717static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3718 unsigned int nr_irqs, void *arg)
3719{
3720 struct irq_alloc_info *info = arg;
3721 struct irq_data *irq_data;
3722 struct amd_ir_data *data;
3723 struct irq_cfg *cfg;
3724 int i, ret, devid;
3725 int index = -1;
3726
3727 if (!info)
3728 return -EINVAL;
3729 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3730 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3731 return -EINVAL;
3732
3733 /*
3734 * With IRQ remapping enabled, don't need contiguous CPU vectors
3735 * to support multiple MSI interrupts.
3736 */
3737 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3738 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3739
3740 devid = get_devid(info);
3741 if (devid < 0)
3742 return -EINVAL;
3743
3744 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3745 if (ret < 0)
3746 return ret;
3747
Jiang Liu7c71d302015-04-13 14:11:33 +08003748 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3749 if (get_irq_table(devid, true))
3750 index = info->ioapic_pin;
3751 else
3752 ret = -ENOMEM;
3753 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003754 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08003755 }
3756 if (index < 0) {
3757 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08003758 goto out_free_parent;
3759 }
3760
3761 for (i = 0; i < nr_irqs; i++) {
3762 irq_data = irq_domain_get_irq_data(domain, virq + i);
3763 cfg = irqd_cfg(irq_data);
3764 if (!irq_data || !cfg) {
3765 ret = -EINVAL;
3766 goto out_free_data;
3767 }
3768
Joerg Roedela130e692015-08-13 11:07:25 +02003769 ret = -ENOMEM;
3770 data = kzalloc(sizeof(*data), GFP_KERNEL);
3771 if (!data)
3772 goto out_free_data;
3773
Jiang Liu7c71d302015-04-13 14:11:33 +08003774 irq_data->hwirq = (devid << 16) + i;
3775 irq_data->chip_data = data;
3776 irq_data->chip = &amd_ir_chip;
3777 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3778 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3779 }
Joerg Roedela130e692015-08-13 11:07:25 +02003780
Jiang Liu7c71d302015-04-13 14:11:33 +08003781 return 0;
3782
3783out_free_data:
3784 for (i--; i >= 0; i--) {
3785 irq_data = irq_domain_get_irq_data(domain, virq + i);
3786 if (irq_data)
3787 kfree(irq_data->chip_data);
3788 }
3789 for (i = 0; i < nr_irqs; i++)
3790 free_irte(devid, index + i);
3791out_free_parent:
3792 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3793 return ret;
3794}
3795
3796static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3797 unsigned int nr_irqs)
3798{
3799 struct irq_2_irte *irte_info;
3800 struct irq_data *irq_data;
3801 struct amd_ir_data *data;
3802 int i;
3803
3804 for (i = 0; i < nr_irqs; i++) {
3805 irq_data = irq_domain_get_irq_data(domain, virq + i);
3806 if (irq_data && irq_data->chip_data) {
3807 data = irq_data->chip_data;
3808 irte_info = &data->irq_2_irte;
3809 free_irte(irte_info->devid, irte_info->index);
3810 kfree(data);
3811 }
3812 }
3813 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3814}
3815
3816static void irq_remapping_activate(struct irq_domain *domain,
3817 struct irq_data *irq_data)
3818{
3819 struct amd_ir_data *data = irq_data->chip_data;
3820 struct irq_2_irte *irte_info = &data->irq_2_irte;
3821
3822 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3823}
3824
3825static void irq_remapping_deactivate(struct irq_domain *domain,
3826 struct irq_data *irq_data)
3827{
3828 struct amd_ir_data *data = irq_data->chip_data;
3829 struct irq_2_irte *irte_info = &data->irq_2_irte;
3830 union irte entry;
3831
3832 entry.val = 0;
3833 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3834}
3835
3836static struct irq_domain_ops amd_ir_domain_ops = {
3837 .alloc = irq_remapping_alloc,
3838 .free = irq_remapping_free,
3839 .activate = irq_remapping_activate,
3840 .deactivate = irq_remapping_deactivate,
3841};
3842
3843static int amd_ir_set_affinity(struct irq_data *data,
3844 const struct cpumask *mask, bool force)
3845{
3846 struct amd_ir_data *ir_data = data->chip_data;
3847 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3848 struct irq_cfg *cfg = irqd_cfg(data);
3849 struct irq_data *parent = data->parent_data;
3850 int ret;
3851
3852 ret = parent->chip->irq_set_affinity(parent, mask, force);
3853 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3854 return ret;
3855
3856 /*
3857 * Atomically updates the IRTE with the new destination, vector
3858 * and flushes the interrupt entry cache.
3859 */
3860 ir_data->irte_entry.fields.vector = cfg->vector;
3861 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
3862 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
3863
3864 /*
3865 * After this point, all the interrupts will start arriving
3866 * at the new destination. So, time to cleanup the previous
3867 * vector allocation.
3868 */
Jiang Liuc6c20022015-04-14 10:30:02 +08003869 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08003870
3871 return IRQ_SET_MASK_OK_DONE;
3872}
3873
3874static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3875{
3876 struct amd_ir_data *ir_data = irq_data->chip_data;
3877
3878 *msg = ir_data->msi_entry;
3879}
3880
3881static struct irq_chip amd_ir_chip = {
3882 .irq_ack = ir_ack_apic_edge,
3883 .irq_set_affinity = amd_ir_set_affinity,
3884 .irq_compose_msi_msg = ir_compose_msi_msg,
3885};
3886
3887int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3888{
3889 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
3890 if (!iommu->ir_domain)
3891 return -ENOMEM;
3892
3893 iommu->ir_domain->parent = arch_get_ir_parent_domain();
3894 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
3895
3896 return 0;
3897}
Joerg Roedel2b324502012-06-21 16:29:10 +02003898#endif