blob: e1c7e9e51045165db5312f540dfc8875e7b1f0b6 [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>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020022#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010025#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090027#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010029#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020030#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020031#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010032#include <linux/notifier.h>
33#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020034#include <linux/irq.h>
35#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020036#include <linux/dma-contiguous.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020037#include <asm/irq_remapping.h>
38#include <asm/io_apic.h>
39#include <asm/apic.h>
40#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020041#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020042#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090043#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010044#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020045#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020046
47#include "amd_iommu_proto.h"
48#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020049#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020050
51#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
52
Joerg Roedel815b33f2011-04-06 17:26:49 +020053#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020054
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020055/*
56 * This bitmap is used to advertise the page sizes our hardware support
57 * to the IOMMU core, which will then use this information to split
58 * physically contiguous memory regions it is mapping into page sizes
59 * that we support.
60 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010061 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020062 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010063#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020064
Joerg Roedelb6c02712008-06-26 21:27:53 +020065static DEFINE_RWLOCK(amd_iommu_devtable_lock);
66
Joerg Roedelbd60b732008-09-11 10:24:48 +020067/* A list of preallocated protection domains */
68static LIST_HEAD(iommu_pd_list);
69static DEFINE_SPINLOCK(iommu_pd_list_lock);
70
Joerg Roedel8fa5f802011-06-09 12:24:45 +020071/* List of all available dev_data structures */
72static LIST_HEAD(dev_data_list);
73static DEFINE_SPINLOCK(dev_data_list_lock);
74
Joerg Roedel6efed632012-06-14 15:52:58 +020075LIST_HEAD(ioapic_map);
76LIST_HEAD(hpet_map);
77
Joerg Roedel0feae532009-08-26 15:26:30 +020078/*
79 * Domain for untranslated devices - only allocated
80 * if iommu=pt passed on kernel cmd line.
81 */
82static struct protection_domain *pt_domain;
83
Thierry Redingb22f6432014-06-27 09:03:12 +020084static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010085
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010086static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010087int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010088
Joerg Roedelac1534a2012-06-21 14:52:40 +020089static struct dma_map_ops amd_iommu_dma_ops;
90
Joerg Roedel431b2a22008-07-11 17:14:22 +020091/*
Joerg Roedel50917e22014-08-05 16:38:38 +020092 * This struct contains device specific data for the IOMMU
93 */
94struct iommu_dev_data {
95 struct list_head list; /* For domain->dev_list */
96 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedelf251e182014-08-05 16:48:10 +020097 struct list_head alias_list; /* Link alias-groups together */
Joerg Roedel50917e22014-08-05 16:38:38 +020098 struct iommu_dev_data *alias_data;/* The alias dev_data */
99 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +0200100 u16 devid; /* PCI Device ID */
101 bool iommu_v2; /* Device can make use of IOMMUv2 */
102 bool passthrough; /* Default for device is pt_domain */
103 struct {
104 bool enabled;
105 int qdep;
106 } ats; /* ATS state */
107 bool pri_tlp; /* PASID TLB required for
108 PPR completions */
109 u32 errata; /* Bitmap for errata to apply */
110};
111
112/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200113 * general struct to manage commands send to an IOMMU
114 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200115struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200116 u32 data[4];
117};
118
Joerg Roedel05152a02012-06-15 16:53:51 +0200119struct kmem_cache *amd_iommu_irq_cache;
120
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200121static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100122static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700123
Joerg Roedel15898bb2009-11-24 15:39:42 +0100124/****************************************************************************
125 *
126 * Helper functions
127 *
128 ****************************************************************************/
129
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100130static struct protection_domain *to_pdomain(struct iommu_domain *dom)
131{
132 return container_of(dom, struct protection_domain, domain);
133}
134
Joerg Roedelf62dda62011-06-09 12:55:35 +0200135static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200136{
137 struct iommu_dev_data *dev_data;
138 unsigned long flags;
139
140 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
141 if (!dev_data)
142 return NULL;
143
Joerg Roedelf251e182014-08-05 16:48:10 +0200144 INIT_LIST_HEAD(&dev_data->alias_list);
145
Joerg Roedelf62dda62011-06-09 12:55:35 +0200146 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200147
148 spin_lock_irqsave(&dev_data_list_lock, flags);
149 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
150 spin_unlock_irqrestore(&dev_data_list_lock, flags);
151
152 return dev_data;
153}
154
155static void free_dev_data(struct iommu_dev_data *dev_data)
156{
157 unsigned long flags;
158
159 spin_lock_irqsave(&dev_data_list_lock, flags);
160 list_del(&dev_data->dev_data_list);
161 spin_unlock_irqrestore(&dev_data_list_lock, flags);
162
163 kfree(dev_data);
164}
165
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200166static struct iommu_dev_data *search_dev_data(u16 devid)
167{
168 struct iommu_dev_data *dev_data;
169 unsigned long flags;
170
171 spin_lock_irqsave(&dev_data_list_lock, flags);
172 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
173 if (dev_data->devid == devid)
174 goto out_unlock;
175 }
176
177 dev_data = NULL;
178
179out_unlock:
180 spin_unlock_irqrestore(&dev_data_list_lock, flags);
181
182 return dev_data;
183}
184
185static struct iommu_dev_data *find_dev_data(u16 devid)
186{
187 struct iommu_dev_data *dev_data;
188
189 dev_data = search_dev_data(devid);
190
191 if (dev_data == NULL)
192 dev_data = alloc_dev_data(devid);
193
194 return dev_data;
195}
196
Joerg Roedel15898bb2009-11-24 15:39:42 +0100197static inline u16 get_device_id(struct device *dev)
198{
199 struct pci_dev *pdev = to_pci_dev(dev);
200
Shuah Khan6f2729b2013-02-27 17:07:30 -0700201 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100202}
203
Joerg Roedel657cbb62009-11-23 15:26:46 +0100204static struct iommu_dev_data *get_dev_data(struct device *dev)
205{
206 return dev->archdata.iommu;
207}
208
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100209static bool pci_iommuv2_capable(struct pci_dev *pdev)
210{
211 static const int caps[] = {
212 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100213 PCI_EXT_CAP_ID_PRI,
214 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100215 };
216 int i, pos;
217
218 for (i = 0; i < 3; ++i) {
219 pos = pci_find_ext_capability(pdev, caps[i]);
220 if (pos == 0)
221 return false;
222 }
223
224 return true;
225}
226
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100227static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
228{
229 struct iommu_dev_data *dev_data;
230
231 dev_data = get_dev_data(&pdev->dev);
232
233 return dev_data->errata & (1 << erratum) ? true : false;
234}
235
Joerg Roedel71c70982009-11-24 16:43:06 +0100236/*
237 * In this function the list of preallocated protection domains is traversed to
238 * find the domain for a specific device
239 */
240static struct dma_ops_domain *find_protection_domain(u16 devid)
241{
242 struct dma_ops_domain *entry, *ret = NULL;
243 unsigned long flags;
244 u16 alias = amd_iommu_alias_table[devid];
245
246 if (list_empty(&iommu_pd_list))
247 return NULL;
248
249 spin_lock_irqsave(&iommu_pd_list_lock, flags);
250
251 list_for_each_entry(entry, &iommu_pd_list, list) {
252 if (entry->target_dev == devid ||
253 entry->target_dev == alias) {
254 ret = entry;
255 break;
256 }
257 }
258
259 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
260
261 return ret;
262}
263
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100264/*
265 * This function checks if the driver got a valid device from the caller to
266 * avoid dereferencing invalid pointers.
267 */
268static bool check_device(struct device *dev)
269{
270 u16 devid;
271
272 if (!dev || !dev->dma_mask)
273 return false;
274
Yijing Wangb82a2272013-12-05 19:42:41 +0800275 /* No PCI device */
276 if (!dev_is_pci(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100277 return false;
278
279 devid = get_device_id(dev);
280
281 /* Out of our scope? */
282 if (devid > amd_iommu_last_bdf)
283 return false;
284
285 if (amd_iommu_rlookup_table[devid] == NULL)
286 return false;
287
288 return true;
289}
290
Alex Williamson25b11ce2014-09-19 10:03:13 -0600291static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600292{
Alex Williamson2851db22012-10-08 22:49:41 -0600293 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600294
Alex Williamson65d53522014-07-03 09:51:30 -0600295 group = iommu_group_get_for_dev(dev);
Alex Williamson25b11ce2014-09-19 10:03:13 -0600296 if (!IS_ERR(group))
297 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600298}
299
Alex Williamsonc1931092014-07-03 09:51:24 -0600300static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
301{
302 *(u16 *)data = alias;
303 return 0;
304}
305
306static u16 get_alias(struct device *dev)
307{
308 struct pci_dev *pdev = to_pci_dev(dev);
309 u16 devid, ivrs_alias, pci_alias;
310
311 devid = get_device_id(dev);
312 ivrs_alias = amd_iommu_alias_table[devid];
313 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
314
315 if (ivrs_alias == pci_alias)
316 return ivrs_alias;
317
318 /*
319 * DMA alias showdown
320 *
321 * The IVRS is fairly reliable in telling us about aliases, but it
322 * can't know about every screwy device. If we don't have an IVRS
323 * reported alias, use the PCI reported alias. In that case we may
324 * still need to initialize the rlookup and dev_table entries if the
325 * alias is to a non-existent device.
326 */
327 if (ivrs_alias == devid) {
328 if (!amd_iommu_rlookup_table[pci_alias]) {
329 amd_iommu_rlookup_table[pci_alias] =
330 amd_iommu_rlookup_table[devid];
331 memcpy(amd_iommu_dev_table[pci_alias].data,
332 amd_iommu_dev_table[devid].data,
333 sizeof(amd_iommu_dev_table[pci_alias].data));
334 }
335
336 return pci_alias;
337 }
338
339 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
340 "for device %s[%04x:%04x], kernel reported alias "
341 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
342 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
343 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
344 PCI_FUNC(pci_alias));
345
346 /*
347 * If we don't have a PCI DMA alias and the IVRS alias is on the same
348 * bus, then the IVRS table may know about a quirk that we don't.
349 */
350 if (pci_alias == devid &&
351 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
352 pdev->dev_flags |= PCI_DEV_FLAGS_DMA_ALIAS_DEVFN;
353 pdev->dma_alias_devfn = ivrs_alias & 0xff;
354 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
355 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
356 dev_name(dev));
357 }
358
359 return ivrs_alias;
360}
361
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600362static int iommu_init_device(struct device *dev)
363{
364 struct pci_dev *pdev = to_pci_dev(dev);
365 struct iommu_dev_data *dev_data;
366 u16 alias;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600367
368 if (dev->archdata.iommu)
369 return 0;
370
371 dev_data = find_dev_data(get_device_id(dev));
372 if (!dev_data)
373 return -ENOMEM;
374
Alex Williamsonc1931092014-07-03 09:51:24 -0600375 alias = get_alias(dev);
376
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600377 if (alias != dev_data->devid) {
378 struct iommu_dev_data *alias_data;
379
380 alias_data = find_dev_data(alias);
381 if (alias_data == NULL) {
382 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
383 dev_name(dev));
384 free_dev_data(dev_data);
385 return -ENOTSUPP;
386 }
387 dev_data->alias_data = alias_data;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600388
Joerg Roedelf251e182014-08-05 16:48:10 +0200389 /* Add device to the alias_list */
390 list_add(&dev_data->alias_list, &alias_data->alias_list);
Radmila Kompováe644a012013-05-02 17:24:25 +0200391 }
Alex Williamson9dcd6132012-05-30 14:19:07 -0600392
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100393 if (pci_iommuv2_capable(pdev)) {
394 struct amd_iommu *iommu;
395
396 iommu = amd_iommu_rlookup_table[dev_data->devid];
397 dev_data->iommu_v2 = iommu->is_iommu_v2;
398 }
399
Joerg Roedel657cbb62009-11-23 15:26:46 +0100400 dev->archdata.iommu = dev_data;
401
Alex Williamson066f2e92014-06-12 16:12:37 -0600402 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
403 dev);
404
Joerg Roedel657cbb62009-11-23 15:26:46 +0100405 return 0;
406}
407
Joerg Roedel26018872011-06-06 16:50:14 +0200408static void iommu_ignore_device(struct device *dev)
409{
410 u16 devid, alias;
411
412 devid = get_device_id(dev);
413 alias = amd_iommu_alias_table[devid];
414
415 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
416 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
417
418 amd_iommu_rlookup_table[devid] = NULL;
419 amd_iommu_rlookup_table[alias] = NULL;
420}
421
Joerg Roedel657cbb62009-11-23 15:26:46 +0100422static void iommu_uninit_device(struct device *dev)
423{
Alex Williamsonc1931092014-07-03 09:51:24 -0600424 struct iommu_dev_data *dev_data = search_dev_data(get_device_id(dev));
425
426 if (!dev_data)
427 return;
428
Alex Williamson066f2e92014-06-12 16:12:37 -0600429 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
430 dev);
431
Alex Williamson9dcd6132012-05-30 14:19:07 -0600432 iommu_group_remove_device(dev);
433
Alex Williamsonc1931092014-07-03 09:51:24 -0600434 /* Unlink from alias, it may change if another device is re-plugged */
435 dev_data->alias_data = NULL;
436
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200437 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600438 * We keep dev_data around for unplugged devices and reuse it when the
439 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200440 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100441}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100442
443void __init amd_iommu_uninit_devices(void)
444{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200445 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100446 struct pci_dev *pdev = NULL;
447
448 for_each_pci_dev(pdev) {
449
450 if (!check_device(&pdev->dev))
451 continue;
452
453 iommu_uninit_device(&pdev->dev);
454 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200455
456 /* Free all of our dev_data structures */
457 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
458 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100459}
460
461int __init amd_iommu_init_devices(void)
462{
463 struct pci_dev *pdev = NULL;
464 int ret = 0;
465
466 for_each_pci_dev(pdev) {
467
468 if (!check_device(&pdev->dev))
469 continue;
470
471 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200472 if (ret == -ENOTSUPP)
473 iommu_ignore_device(&pdev->dev);
474 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100475 goto out_free;
476 }
477
Alex Williamson25b11ce2014-09-19 10:03:13 -0600478 /*
479 * Initialize IOMMU groups only after iommu_init_device() has
480 * had a chance to populate any IVRS defined aliases.
481 */
482 for_each_pci_dev(pdev) {
483 if (check_device(&pdev->dev))
484 init_iommu_group(&pdev->dev);
485 }
486
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100487 return 0;
488
489out_free:
490
491 amd_iommu_uninit_devices();
492
493 return ret;
494}
Joerg Roedel7f265082008-12-12 13:50:21 +0100495#ifdef CONFIG_AMD_IOMMU_STATS
496
497/*
498 * Initialization code for statistics collection
499 */
500
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100501DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100502DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100503DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100504DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100505DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100506DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100507DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100508DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100509DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100510DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100511DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100512DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100513DECLARE_STATS_COUNTER(complete_ppr);
514DECLARE_STATS_COUNTER(invalidate_iotlb);
515DECLARE_STATS_COUNTER(invalidate_iotlb_all);
516DECLARE_STATS_COUNTER(pri_requests);
517
Joerg Roedel7f265082008-12-12 13:50:21 +0100518static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100519static struct dentry *de_fflush;
520
521static void amd_iommu_stats_add(struct __iommu_counter *cnt)
522{
523 if (stats_dir == NULL)
524 return;
525
526 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
527 &cnt->value);
528}
529
530static void amd_iommu_stats_init(void)
531{
532 stats_dir = debugfs_create_dir("amd-iommu", NULL);
533 if (stats_dir == NULL)
534 return;
535
Joerg Roedel7f265082008-12-12 13:50:21 +0100536 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300537 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100538
539 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100540 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100541 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100542 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100543 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100544 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100545 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100546 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100547 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100548 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100549 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100550 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100551 amd_iommu_stats_add(&complete_ppr);
552 amd_iommu_stats_add(&invalidate_iotlb);
553 amd_iommu_stats_add(&invalidate_iotlb_all);
554 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100555}
556
557#endif
558
Joerg Roedel431b2a22008-07-11 17:14:22 +0200559/****************************************************************************
560 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200561 * Interrupt handling functions
562 *
563 ****************************************************************************/
564
Joerg Roedele3e59872009-09-03 14:02:10 +0200565static void dump_dte_entry(u16 devid)
566{
567 int i;
568
Joerg Roedelee6c2862011-11-09 12:06:03 +0100569 for (i = 0; i < 4; ++i)
570 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200571 amd_iommu_dev_table[devid].data[i]);
572}
573
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200574static void dump_command(unsigned long phys_addr)
575{
576 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
577 int i;
578
579 for (i = 0; i < 4; ++i)
580 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
581}
582
Joerg Roedela345b232009-09-03 15:01:43 +0200583static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200584{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200585 int type, devid, domid, flags;
586 volatile u32 *event = __evt;
587 int count = 0;
588 u64 address;
589
590retry:
591 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
592 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
593 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
594 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
595 address = (u64)(((u64)event[3]) << 32) | event[2];
596
597 if (type == 0) {
598 /* Did we hit the erratum? */
599 if (++count == LOOP_TIMEOUT) {
600 pr_err("AMD-Vi: No event written to event log\n");
601 return;
602 }
603 udelay(1);
604 goto retry;
605 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200606
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200607 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200608
609 switch (type) {
610 case EVENT_TYPE_ILL_DEV:
611 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
612 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700613 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200614 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200615 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200616 break;
617 case EVENT_TYPE_IO_FAULT:
618 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
619 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700620 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200621 domid, address, flags);
622 break;
623 case EVENT_TYPE_DEV_TAB_ERR:
624 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
625 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700626 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200627 address, flags);
628 break;
629 case EVENT_TYPE_PAGE_TAB_ERR:
630 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
631 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700632 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200633 domid, address, flags);
634 break;
635 case EVENT_TYPE_ILL_CMD:
636 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200637 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200638 break;
639 case EVENT_TYPE_CMD_HARD_ERR:
640 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
641 "flags=0x%04x]\n", address, flags);
642 break;
643 case EVENT_TYPE_IOTLB_INV_TO:
644 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
645 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700646 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200647 address);
648 break;
649 case EVENT_TYPE_INV_DEV_REQ:
650 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
651 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700652 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200653 address, flags);
654 break;
655 default:
656 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
657 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200658
659 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200660}
661
662static void iommu_poll_events(struct amd_iommu *iommu)
663{
664 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200665
666 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
667 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
668
669 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200670 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200671 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
672 }
673
674 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200675}
676
Joerg Roedeleee53532012-06-01 15:20:23 +0200677static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100678{
679 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100680
Joerg Roedel399be2f2011-12-01 16:53:47 +0100681 INC_STATS_COUNTER(pri_requests);
682
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100683 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
684 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
685 return;
686 }
687
688 fault.address = raw[1];
689 fault.pasid = PPR_PASID(raw[0]);
690 fault.device_id = PPR_DEVID(raw[0]);
691 fault.tag = PPR_TAG(raw[0]);
692 fault.flags = PPR_FLAGS(raw[0]);
693
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100694 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
695}
696
697static void iommu_poll_ppr_log(struct amd_iommu *iommu)
698{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100699 u32 head, tail;
700
701 if (iommu->ppr_log == NULL)
702 return;
703
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100704 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
705 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
706
707 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200708 volatile u64 *raw;
709 u64 entry[2];
710 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100711
Joerg Roedeleee53532012-06-01 15:20:23 +0200712 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100713
Joerg Roedeleee53532012-06-01 15:20:23 +0200714 /*
715 * Hardware bug: Interrupt may arrive before the entry is
716 * written to memory. If this happens we need to wait for the
717 * entry to arrive.
718 */
719 for (i = 0; i < LOOP_TIMEOUT; ++i) {
720 if (PPR_REQ_TYPE(raw[0]) != 0)
721 break;
722 udelay(1);
723 }
724
725 /* Avoid memcpy function-call overhead */
726 entry[0] = raw[0];
727 entry[1] = raw[1];
728
729 /*
730 * To detect the hardware bug we need to clear the entry
731 * back to zero.
732 */
733 raw[0] = raw[1] = 0UL;
734
735 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100736 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
737 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200738
Joerg Roedeleee53532012-06-01 15:20:23 +0200739 /* Handle PPR entry */
740 iommu_handle_ppr_entry(iommu, entry);
741
Joerg Roedeleee53532012-06-01 15:20:23 +0200742 /* Refresh ring-buffer information */
743 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100744 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
745 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100746}
747
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200748irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200749{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500750 struct amd_iommu *iommu = (struct amd_iommu *) data;
751 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200752
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500753 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
754 /* Enable EVT and PPR interrupts again */
755 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
756 iommu->mmio_base + MMIO_STATUS_OFFSET);
757
758 if (status & MMIO_STATUS_EVT_INT_MASK) {
759 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
760 iommu_poll_events(iommu);
761 }
762
763 if (status & MMIO_STATUS_PPR_INT_MASK) {
764 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
765 iommu_poll_ppr_log(iommu);
766 }
767
768 /*
769 * Hardware bug: ERBT1312
770 * When re-enabling interrupt (by writing 1
771 * to clear the bit), the hardware might also try to set
772 * the interrupt bit in the event status register.
773 * In this scenario, the bit will be set, and disable
774 * subsequent interrupts.
775 *
776 * Workaround: The IOMMU driver should read back the
777 * status register and check if the interrupt bits are cleared.
778 * If not, driver will need to go through the interrupt handler
779 * again and re-clear the bits
780 */
781 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100782 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200783 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200784}
785
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200786irqreturn_t amd_iommu_int_handler(int irq, void *data)
787{
788 return IRQ_WAKE_THREAD;
789}
790
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200791/****************************************************************************
792 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200793 * IOMMU command queuing functions
794 *
795 ****************************************************************************/
796
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200797static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200798{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200799 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200800
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200801 while (*sem == 0 && i < LOOP_TIMEOUT) {
802 udelay(1);
803 i += 1;
804 }
805
806 if (i == LOOP_TIMEOUT) {
807 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
808 return -EIO;
809 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200810
811 return 0;
812}
813
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200814static void copy_cmd_to_buffer(struct amd_iommu *iommu,
815 struct iommu_cmd *cmd,
816 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200817{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200818 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200819
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200820 target = iommu->cmd_buf + tail;
821 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200822
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200823 /* Copy command to buffer */
824 memcpy(target, cmd, sizeof(*cmd));
825
826 /* Tell the IOMMU about it */
827 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
828}
829
Joerg Roedel815b33f2011-04-06 17:26:49 +0200830static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200831{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200832 WARN_ON(address & 0x7ULL);
833
Joerg Roedelded46732011-04-06 10:53:48 +0200834 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200835 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
836 cmd->data[1] = upper_32_bits(__pa(address));
837 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200838 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
839}
840
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200841static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
842{
843 memset(cmd, 0, sizeof(*cmd));
844 cmd->data[0] = devid;
845 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
846}
847
Joerg Roedel11b64022011-04-06 11:49:28 +0200848static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
849 size_t size, u16 domid, int pde)
850{
851 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100852 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200853
854 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100855 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200856
857 if (pages > 1) {
858 /*
859 * If we have to flush more than one page, flush all
860 * TLB entries for this domain
861 */
862 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100863 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200864 }
865
866 address &= PAGE_MASK;
867
868 memset(cmd, 0, sizeof(*cmd));
869 cmd->data[1] |= domid;
870 cmd->data[2] = lower_32_bits(address);
871 cmd->data[3] = upper_32_bits(address);
872 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
873 if (s) /* size bit - we flush more than one 4kb page */
874 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200875 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200876 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
877}
878
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200879static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
880 u64 address, size_t size)
881{
882 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100883 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200884
885 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100886 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200887
888 if (pages > 1) {
889 /*
890 * If we have to flush more than one page, flush all
891 * TLB entries for this domain
892 */
893 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100894 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200895 }
896
897 address &= PAGE_MASK;
898
899 memset(cmd, 0, sizeof(*cmd));
900 cmd->data[0] = devid;
901 cmd->data[0] |= (qdep & 0xff) << 24;
902 cmd->data[1] = devid;
903 cmd->data[2] = lower_32_bits(address);
904 cmd->data[3] = upper_32_bits(address);
905 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
906 if (s)
907 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
908}
909
Joerg Roedel22e266c2011-11-21 15:59:08 +0100910static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
911 u64 address, bool size)
912{
913 memset(cmd, 0, sizeof(*cmd));
914
915 address &= ~(0xfffULL);
916
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600917 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100918 cmd->data[1] = domid;
919 cmd->data[2] = lower_32_bits(address);
920 cmd->data[3] = upper_32_bits(address);
921 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
922 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
923 if (size)
924 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
925 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
926}
927
928static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
929 int qdep, u64 address, bool size)
930{
931 memset(cmd, 0, sizeof(*cmd));
932
933 address &= ~(0xfffULL);
934
935 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600936 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100937 cmd->data[0] |= (qdep & 0xff) << 24;
938 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600939 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100940 cmd->data[2] = lower_32_bits(address);
941 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
942 cmd->data[3] = upper_32_bits(address);
943 if (size)
944 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
945 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
946}
947
Joerg Roedelc99afa22011-11-21 18:19:25 +0100948static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
949 int status, int tag, bool gn)
950{
951 memset(cmd, 0, sizeof(*cmd));
952
953 cmd->data[0] = devid;
954 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600955 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100956 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
957 }
958 cmd->data[3] = tag & 0x1ff;
959 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
960
961 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
962}
963
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200964static void build_inv_all(struct iommu_cmd *cmd)
965{
966 memset(cmd, 0, sizeof(*cmd));
967 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200968}
969
Joerg Roedel7ef27982012-06-21 16:46:04 +0200970static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
971{
972 memset(cmd, 0, sizeof(*cmd));
973 cmd->data[0] = devid;
974 CMD_SET_TYPE(cmd, CMD_INV_IRT);
975}
976
Joerg Roedel431b2a22008-07-11 17:14:22 +0200977/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200978 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200979 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200980 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200981static int iommu_queue_command_sync(struct amd_iommu *iommu,
982 struct iommu_cmd *cmd,
983 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200984{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200985 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200986 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200987
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200988 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100989
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200990again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200991 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200992
993 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
994 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
995 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
996 left = (head - next_tail) % iommu->cmd_buf_size;
997
998 if (left <= 2) {
999 struct iommu_cmd sync_cmd;
1000 volatile u64 sem = 0;
1001 int ret;
1002
1003 build_completion_wait(&sync_cmd, (u64)&sem);
1004 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1005
1006 spin_unlock_irqrestore(&iommu->lock, flags);
1007
1008 if ((ret = wait_on_sem(&sem)) != 0)
1009 return ret;
1010
1011 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001012 }
1013
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001014 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001015
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001016 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001017 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001018
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001019 spin_unlock_irqrestore(&iommu->lock, flags);
1020
Joerg Roedel815b33f2011-04-06 17:26:49 +02001021 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001022}
1023
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001024static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1025{
1026 return iommu_queue_command_sync(iommu, cmd, true);
1027}
1028
Joerg Roedel8d201962008-12-02 20:34:41 +01001029/*
1030 * This function queues a completion wait command into the command
1031 * buffer of an IOMMU
1032 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001033static int iommu_completion_wait(struct amd_iommu *iommu)
1034{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001035 struct iommu_cmd cmd;
1036 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001037 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001038
1039 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001040 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001041
Joerg Roedel815b33f2011-04-06 17:26:49 +02001042 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001043
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001044 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001045 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001046 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001047
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001048 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001049}
1050
Joerg Roedeld8c13082011-04-06 18:51:26 +02001051static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001052{
1053 struct iommu_cmd cmd;
1054
Joerg Roedeld8c13082011-04-06 18:51:26 +02001055 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001056
Joerg Roedeld8c13082011-04-06 18:51:26 +02001057 return iommu_queue_command(iommu, &cmd);
1058}
1059
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001060static void iommu_flush_dte_all(struct amd_iommu *iommu)
1061{
1062 u32 devid;
1063
1064 for (devid = 0; devid <= 0xffff; ++devid)
1065 iommu_flush_dte(iommu, devid);
1066
1067 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001068}
1069
1070/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001071 * This function uses heavy locking and may disable irqs for some time. But
1072 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001073 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001074static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001075{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001076 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001077
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001078 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1079 struct iommu_cmd cmd;
1080 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1081 dom_id, 1);
1082 iommu_queue_command(iommu, &cmd);
1083 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001084
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001085 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001086}
1087
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001088static void iommu_flush_all(struct amd_iommu *iommu)
1089{
1090 struct iommu_cmd cmd;
1091
1092 build_inv_all(&cmd);
1093
1094 iommu_queue_command(iommu, &cmd);
1095 iommu_completion_wait(iommu);
1096}
1097
Joerg Roedel7ef27982012-06-21 16:46:04 +02001098static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1099{
1100 struct iommu_cmd cmd;
1101
1102 build_inv_irt(&cmd, devid);
1103
1104 iommu_queue_command(iommu, &cmd);
1105}
1106
1107static void iommu_flush_irt_all(struct amd_iommu *iommu)
1108{
1109 u32 devid;
1110
1111 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1112 iommu_flush_irt(iommu, devid);
1113
1114 iommu_completion_wait(iommu);
1115}
1116
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001117void iommu_flush_all_caches(struct amd_iommu *iommu)
1118{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001119 if (iommu_feature(iommu, FEATURE_IA)) {
1120 iommu_flush_all(iommu);
1121 } else {
1122 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001123 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001124 iommu_flush_tlb_all(iommu);
1125 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001126}
1127
Joerg Roedel431b2a22008-07-11 17:14:22 +02001128/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001129 * Command send function for flushing on-device TLB
1130 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001131static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1132 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001133{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001134 struct amd_iommu *iommu;
1135 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001136 int qdep;
1137
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001138 qdep = dev_data->ats.qdep;
1139 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001140
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001141 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001142
1143 return iommu_queue_command(iommu, &cmd);
1144}
1145
1146/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001147 * Command send function for invalidating a device table entry
1148 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001149static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001150{
1151 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001152 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001153
Joerg Roedel6c542042011-06-09 17:07:31 +02001154 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001155
Joerg Roedelf62dda62011-06-09 12:55:35 +02001156 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001157 if (ret)
1158 return ret;
1159
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001160 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001161 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001162
1163 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001164}
1165
Joerg Roedel431b2a22008-07-11 17:14:22 +02001166/*
1167 * TLB invalidation function which is called from the mapping functions.
1168 * It invalidates a single PTE if the range to flush is within a single
1169 * page. Otherwise it flushes the whole TLB of the IOMMU.
1170 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001171static void __domain_flush_pages(struct protection_domain *domain,
1172 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001173{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001174 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001175 struct iommu_cmd cmd;
1176 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001177
Joerg Roedel11b64022011-04-06 11:49:28 +02001178 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001179
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001180 for (i = 0; i < amd_iommus_present; ++i) {
1181 if (!domain->dev_iommu[i])
1182 continue;
1183
1184 /*
1185 * Devices of this domain are behind this IOMMU
1186 * We need a TLB flush
1187 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001188 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001189 }
1190
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001191 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001192
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001193 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001194 continue;
1195
Joerg Roedel6c542042011-06-09 17:07:31 +02001196 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001197 }
1198
Joerg Roedel11b64022011-04-06 11:49:28 +02001199 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001200}
1201
Joerg Roedel17b124b2011-04-06 18:01:35 +02001202static void domain_flush_pages(struct protection_domain *domain,
1203 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001204{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001205 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001206}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001207
Joerg Roedel1c655772008-09-04 18:40:05 +02001208/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001209static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001210{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001211 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001212}
1213
Chris Wright42a49f92009-06-15 15:42:00 +02001214/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001215static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001216{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001217 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1218}
1219
1220static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001221{
1222 int i;
1223
1224 for (i = 0; i < amd_iommus_present; ++i) {
1225 if (!domain->dev_iommu[i])
1226 continue;
1227
1228 /*
1229 * Devices of this domain are behind this IOMMU
1230 * We need to wait for completion of all commands.
1231 */
1232 iommu_completion_wait(amd_iommus[i]);
1233 }
1234}
1235
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001236
Joerg Roedel43f49602008-12-02 21:01:12 +01001237/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001238 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001239 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001240static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001241{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001242 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001243
1244 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001245 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001246}
1247
Joerg Roedel431b2a22008-07-11 17:14:22 +02001248/****************************************************************************
1249 *
1250 * The functions below are used the create the page table mappings for
1251 * unity mapped regions.
1252 *
1253 ****************************************************************************/
1254
1255/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001256 * This function is used to add another level to an IO page table. Adding
1257 * another level increases the size of the address space by 9 bits to a size up
1258 * to 64 bits.
1259 */
1260static bool increase_address_space(struct protection_domain *domain,
1261 gfp_t gfp)
1262{
1263 u64 *pte;
1264
1265 if (domain->mode == PAGE_MODE_6_LEVEL)
1266 /* address space already 64 bit large */
1267 return false;
1268
1269 pte = (void *)get_zeroed_page(gfp);
1270 if (!pte)
1271 return false;
1272
1273 *pte = PM_LEVEL_PDE(domain->mode,
1274 virt_to_phys(domain->pt_root));
1275 domain->pt_root = pte;
1276 domain->mode += 1;
1277 domain->updated = true;
1278
1279 return true;
1280}
1281
1282static u64 *alloc_pte(struct protection_domain *domain,
1283 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001284 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001285 u64 **pte_page,
1286 gfp_t gfp)
1287{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001288 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001289 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001290
1291 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001292
1293 while (address > PM_LEVEL_SIZE(domain->mode))
1294 increase_address_space(domain, gfp);
1295
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001296 level = domain->mode - 1;
1297 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1298 address = PAGE_SIZE_ALIGN(address, page_size);
1299 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001300
1301 while (level > end_lvl) {
1302 if (!IOMMU_PTE_PRESENT(*pte)) {
1303 page = (u64 *)get_zeroed_page(gfp);
1304 if (!page)
1305 return NULL;
1306 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1307 }
1308
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001309 /* No level skipping support yet */
1310 if (PM_PTE_LEVEL(*pte) != level)
1311 return NULL;
1312
Joerg Roedel308973d2009-11-24 17:43:32 +01001313 level -= 1;
1314
1315 pte = IOMMU_PTE_PAGE(*pte);
1316
1317 if (pte_page && level == end_lvl)
1318 *pte_page = pte;
1319
1320 pte = &pte[PM_LEVEL_INDEX(level, address)];
1321 }
1322
1323 return pte;
1324}
1325
1326/*
1327 * This function checks if there is a PTE for a given dma address. If
1328 * there is one, it returns the pointer to it.
1329 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001330static u64 *fetch_pte(struct protection_domain *domain,
1331 unsigned long address,
1332 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001333{
1334 int level;
1335 u64 *pte;
1336
Joerg Roedel24cd7722010-01-19 17:27:39 +01001337 if (address > PM_LEVEL_SIZE(domain->mode))
1338 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001339
Joerg Roedel3039ca12015-04-01 14:58:48 +02001340 level = domain->mode - 1;
1341 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1342 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001343
1344 while (level > 0) {
1345
1346 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001347 if (!IOMMU_PTE_PRESENT(*pte))
1348 return NULL;
1349
Joerg Roedel24cd7722010-01-19 17:27:39 +01001350 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001351 if (PM_PTE_LEVEL(*pte) == 7 ||
1352 PM_PTE_LEVEL(*pte) == 0)
1353 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001354
1355 /* No level skipping support yet */
1356 if (PM_PTE_LEVEL(*pte) != level)
1357 return NULL;
1358
Joerg Roedel308973d2009-11-24 17:43:32 +01001359 level -= 1;
1360
Joerg Roedel24cd7722010-01-19 17:27:39 +01001361 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001362 pte = IOMMU_PTE_PAGE(*pte);
1363 pte = &pte[PM_LEVEL_INDEX(level, address)];
1364 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1365 }
1366
1367 if (PM_PTE_LEVEL(*pte) == 0x07) {
1368 unsigned long pte_mask;
1369
1370 /*
1371 * If we have a series of large PTEs, make
1372 * sure to return a pointer to the first one.
1373 */
1374 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1375 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1376 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001377 }
1378
1379 return pte;
1380}
1381
1382/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001383 * Generic mapping functions. It maps a physical address into a DMA
1384 * address space. It allocates the page table pages if necessary.
1385 * In the future it can be extended to a generic mapping function
1386 * supporting all features of AMD IOMMU page tables like level skipping
1387 * and full 64 bit address spaces.
1388 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001389static int iommu_map_page(struct protection_domain *dom,
1390 unsigned long bus_addr,
1391 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001392 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001393 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001394{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001395 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001396 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001397
Joerg Roedeld4b03662015-04-01 14:58:52 +02001398 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1399 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1400
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001401 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001402 return -EINVAL;
1403
Joerg Roedeld4b03662015-04-01 14:58:52 +02001404 count = PAGE_SIZE_PTE_COUNT(page_size);
1405 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001406
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001407 if (!pte)
1408 return -ENOMEM;
1409
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001410 for (i = 0; i < count; ++i)
1411 if (IOMMU_PTE_PRESENT(pte[i]))
1412 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001413
Joerg Roedeld4b03662015-04-01 14:58:52 +02001414 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001415 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1416 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1417 } else
1418 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1419
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001420 if (prot & IOMMU_PROT_IR)
1421 __pte |= IOMMU_PTE_IR;
1422 if (prot & IOMMU_PROT_IW)
1423 __pte |= IOMMU_PTE_IW;
1424
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001425 for (i = 0; i < count; ++i)
1426 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001427
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001428 update_domain(dom);
1429
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001430 return 0;
1431}
1432
Joerg Roedel24cd7722010-01-19 17:27:39 +01001433static unsigned long iommu_unmap_page(struct protection_domain *dom,
1434 unsigned long bus_addr,
1435 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001436{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001437 unsigned long long unmapped;
1438 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001439 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001440
Joerg Roedel24cd7722010-01-19 17:27:39 +01001441 BUG_ON(!is_power_of_2(page_size));
1442
1443 unmapped = 0;
1444
1445 while (unmapped < page_size) {
1446
Joerg Roedel71b390e2015-04-01 14:58:49 +02001447 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001448
Joerg Roedel71b390e2015-04-01 14:58:49 +02001449 if (pte) {
1450 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001451
Joerg Roedel71b390e2015-04-01 14:58:49 +02001452 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001453 for (i = 0; i < count; i++)
1454 pte[i] = 0ULL;
1455 }
1456
1457 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1458 unmapped += unmap_size;
1459 }
1460
Alex Williamson60d0ca32013-06-21 14:33:19 -06001461 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001462
1463 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001464}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001465
Joerg Roedel431b2a22008-07-11 17:14:22 +02001466/*
1467 * This function checks if a specific unity mapping entry is needed for
1468 * this specific IOMMU.
1469 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001470static int iommu_for_unity_map(struct amd_iommu *iommu,
1471 struct unity_map_entry *entry)
1472{
1473 u16 bdf, i;
1474
1475 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1476 bdf = amd_iommu_alias_table[i];
1477 if (amd_iommu_rlookup_table[bdf] == iommu)
1478 return 1;
1479 }
1480
1481 return 0;
1482}
1483
Joerg Roedel431b2a22008-07-11 17:14:22 +02001484/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001485 * This function actually applies the mapping to the page table of the
1486 * dma_ops domain.
1487 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001488static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1489 struct unity_map_entry *e)
1490{
1491 u64 addr;
1492 int ret;
1493
1494 for (addr = e->address_start; addr < e->address_end;
1495 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001496 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001497 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001498 if (ret)
1499 return ret;
1500 /*
1501 * if unity mapping is in aperture range mark the page
1502 * as allocated in the aperture
1503 */
1504 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001505 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001506 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001507 }
1508
1509 return 0;
1510}
1511
Joerg Roedel431b2a22008-07-11 17:14:22 +02001512/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001513 * Init the unity mappings for a specific IOMMU in the system
1514 *
1515 * Basically iterates over all unity mapping entries and applies them to
1516 * the default domain DMA of that IOMMU if necessary.
1517 */
1518static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1519{
1520 struct unity_map_entry *entry;
1521 int ret;
1522
1523 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1524 if (!iommu_for_unity_map(iommu, entry))
1525 continue;
1526 ret = dma_ops_unity_map(iommu->default_dom, entry);
1527 if (ret)
1528 return ret;
1529 }
1530
1531 return 0;
1532}
1533
1534/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001535 * Inits the unity mappings required for a specific device
1536 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001537static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1538 u16 devid)
1539{
1540 struct unity_map_entry *e;
1541 int ret;
1542
1543 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1544 if (!(devid >= e->devid_start && devid <= e->devid_end))
1545 continue;
1546 ret = dma_ops_unity_map(dma_dom, e);
1547 if (ret)
1548 return ret;
1549 }
1550
1551 return 0;
1552}
1553
Joerg Roedel431b2a22008-07-11 17:14:22 +02001554/****************************************************************************
1555 *
1556 * The next functions belong to the address allocator for the dma_ops
1557 * interface functions. They work like the allocators in the other IOMMU
1558 * drivers. Its basically a bitmap which marks the allocated pages in
1559 * the aperture. Maybe it could be enhanced in the future to a more
1560 * efficient allocator.
1561 *
1562 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001563
Joerg Roedel431b2a22008-07-11 17:14:22 +02001564/*
Joerg Roedel384de722009-05-15 12:30:05 +02001565 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001566 *
1567 * called with domain->lock held
1568 */
Joerg Roedel384de722009-05-15 12:30:05 +02001569
Joerg Roedel9cabe892009-05-18 16:38:55 +02001570/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001571 * Used to reserve address ranges in the aperture (e.g. for exclusion
1572 * ranges.
1573 */
1574static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1575 unsigned long start_page,
1576 unsigned int pages)
1577{
1578 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1579
1580 if (start_page + pages > last_page)
1581 pages = last_page - start_page;
1582
1583 for (i = start_page; i < start_page + pages; ++i) {
1584 int index = i / APERTURE_RANGE_PAGES;
1585 int page = i % APERTURE_RANGE_PAGES;
1586 __set_bit(page, dom->aperture[index]->bitmap);
1587 }
1588}
1589
1590/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001591 * This function is used to add a new aperture range to an existing
1592 * aperture in case of dma_ops domain allocation or address allocation
1593 * failure.
1594 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001595static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001596 bool populate, gfp_t gfp)
1597{
1598 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001599 struct amd_iommu *iommu;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001600 unsigned long i, old_size, pte_pgsize;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001601
Joerg Roedelf5e97052009-05-22 12:31:53 +02001602#ifdef CONFIG_IOMMU_STRESS
1603 populate = false;
1604#endif
1605
Joerg Roedel9cabe892009-05-18 16:38:55 +02001606 if (index >= APERTURE_MAX_RANGES)
1607 return -ENOMEM;
1608
1609 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1610 if (!dma_dom->aperture[index])
1611 return -ENOMEM;
1612
1613 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1614 if (!dma_dom->aperture[index]->bitmap)
1615 goto out_free;
1616
1617 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1618
1619 if (populate) {
1620 unsigned long address = dma_dom->aperture_size;
1621 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1622 u64 *pte, *pte_page;
1623
1624 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001625 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001626 &pte_page, gfp);
1627 if (!pte)
1628 goto out_free;
1629
1630 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1631
1632 address += APERTURE_RANGE_SIZE / 64;
1633 }
1634 }
1635
Joerg Roedel17f5b562011-07-06 17:14:44 +02001636 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001637 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1638
Joerg Roedel17f5b562011-07-06 17:14:44 +02001639 /* Reserve address range used for MSI messages */
1640 if (old_size < MSI_ADDR_BASE_LO &&
1641 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1642 unsigned long spage;
1643 int pages;
1644
1645 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1646 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1647
1648 dma_ops_reserve_addresses(dma_dom, spage, pages);
1649 }
1650
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001651 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001652 for_each_iommu(iommu) {
1653 if (iommu->exclusion_start &&
1654 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1655 && iommu->exclusion_start < dma_dom->aperture_size) {
1656 unsigned long startpage;
1657 int pages = iommu_num_pages(iommu->exclusion_start,
1658 iommu->exclusion_length,
1659 PAGE_SIZE);
1660 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1661 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1662 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001663 }
1664
1665 /*
1666 * Check for areas already mapped as present in the new aperture
1667 * range and mark those pages as reserved in the allocator. Such
1668 * mappings may already exist as a result of requested unity
1669 * mappings for devices.
1670 */
1671 for (i = dma_dom->aperture[index]->offset;
1672 i < dma_dom->aperture_size;
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001673 i += pte_pgsize) {
Joerg Roedel3039ca12015-04-01 14:58:48 +02001674 u64 *pte = fetch_pte(&dma_dom->domain, i, &pte_pgsize);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001675 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1676 continue;
1677
Joerg Roedel5d7c94c2015-04-01 14:58:50 +02001678 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT,
1679 pte_pgsize >> 12);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001680 }
1681
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001682 update_domain(&dma_dom->domain);
1683
Joerg Roedel9cabe892009-05-18 16:38:55 +02001684 return 0;
1685
1686out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001687 update_domain(&dma_dom->domain);
1688
Joerg Roedel9cabe892009-05-18 16:38:55 +02001689 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1690
1691 kfree(dma_dom->aperture[index]);
1692 dma_dom->aperture[index] = NULL;
1693
1694 return -ENOMEM;
1695}
1696
Joerg Roedel384de722009-05-15 12:30:05 +02001697static unsigned long dma_ops_area_alloc(struct device *dev,
1698 struct dma_ops_domain *dom,
1699 unsigned int pages,
1700 unsigned long align_mask,
1701 u64 dma_mask,
1702 unsigned long start)
1703{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001704 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001705 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1706 int i = start >> APERTURE_RANGE_SHIFT;
1707 unsigned long boundary_size;
1708 unsigned long address = -1;
1709 unsigned long limit;
1710
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001711 next_bit >>= PAGE_SHIFT;
1712
Joerg Roedel384de722009-05-15 12:30:05 +02001713 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1714 PAGE_SIZE) >> PAGE_SHIFT;
1715
1716 for (;i < max_index; ++i) {
1717 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1718
1719 if (dom->aperture[i]->offset >= dma_mask)
1720 break;
1721
1722 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1723 dma_mask >> PAGE_SHIFT);
1724
1725 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1726 limit, next_bit, pages, 0,
1727 boundary_size, align_mask);
1728 if (address != -1) {
1729 address = dom->aperture[i]->offset +
1730 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001731 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001732 break;
1733 }
1734
1735 next_bit = 0;
1736 }
1737
1738 return address;
1739}
1740
Joerg Roedeld3086442008-06-26 21:27:57 +02001741static unsigned long dma_ops_alloc_addresses(struct device *dev,
1742 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001743 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001744 unsigned long align_mask,
1745 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001746{
Joerg Roedeld3086442008-06-26 21:27:57 +02001747 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001748
Joerg Roedelfe16f082009-05-22 12:27:53 +02001749#ifdef CONFIG_IOMMU_STRESS
1750 dom->next_address = 0;
1751 dom->need_flush = true;
1752#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001753
Joerg Roedel384de722009-05-15 12:30:05 +02001754 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001755 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001756
Joerg Roedel1c655772008-09-04 18:40:05 +02001757 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001758 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001759 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1760 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001761 dom->need_flush = true;
1762 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001763
Joerg Roedel384de722009-05-15 12:30:05 +02001764 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001765 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001766
1767 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1768
1769 return address;
1770}
1771
Joerg Roedel431b2a22008-07-11 17:14:22 +02001772/*
1773 * The address free function.
1774 *
1775 * called with domain->lock held
1776 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001777static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1778 unsigned long address,
1779 unsigned int pages)
1780{
Joerg Roedel384de722009-05-15 12:30:05 +02001781 unsigned i = address >> APERTURE_RANGE_SHIFT;
1782 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001783
Joerg Roedel384de722009-05-15 12:30:05 +02001784 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1785
Joerg Roedel47bccd62009-05-22 12:40:54 +02001786#ifdef CONFIG_IOMMU_STRESS
1787 if (i < 4)
1788 return;
1789#endif
1790
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001791 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001792 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001793
1794 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001795
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001796 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001797
Joerg Roedeld3086442008-06-26 21:27:57 +02001798}
1799
Joerg Roedel431b2a22008-07-11 17:14:22 +02001800/****************************************************************************
1801 *
1802 * The next functions belong to the domain allocation. A domain is
1803 * allocated for every IOMMU as the default domain. If device isolation
1804 * is enabled, every device get its own domain. The most important thing
1805 * about domains is the page table mapping the DMA address space they
1806 * contain.
1807 *
1808 ****************************************************************************/
1809
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001810/*
1811 * This function adds a protection domain to the global protection domain list
1812 */
1813static void add_domain_to_list(struct protection_domain *domain)
1814{
1815 unsigned long flags;
1816
1817 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1818 list_add(&domain->list, &amd_iommu_pd_list);
1819 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1820}
1821
1822/*
1823 * This function removes a protection domain to the global
1824 * protection domain list
1825 */
1826static void del_domain_from_list(struct protection_domain *domain)
1827{
1828 unsigned long flags;
1829
1830 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1831 list_del(&domain->list);
1832 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1833}
1834
Joerg Roedelec487d12008-06-26 21:27:58 +02001835static u16 domain_id_alloc(void)
1836{
1837 unsigned long flags;
1838 int id;
1839
1840 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1841 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1842 BUG_ON(id == 0);
1843 if (id > 0 && id < MAX_DOMAIN_ID)
1844 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1845 else
1846 id = 0;
1847 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1848
1849 return id;
1850}
1851
Joerg Roedela2acfb72008-12-02 18:28:53 +01001852static void domain_id_free(int id)
1853{
1854 unsigned long flags;
1855
1856 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1857 if (id > 0 && id < MAX_DOMAIN_ID)
1858 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1859 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1860}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001861
Joerg Roedel5c34c402013-06-20 20:22:58 +02001862#define DEFINE_FREE_PT_FN(LVL, FN) \
1863static void free_pt_##LVL (unsigned long __pt) \
1864{ \
1865 unsigned long p; \
1866 u64 *pt; \
1867 int i; \
1868 \
1869 pt = (u64 *)__pt; \
1870 \
1871 for (i = 0; i < 512; ++i) { \
1872 if (!IOMMU_PTE_PRESENT(pt[i])) \
1873 continue; \
1874 \
1875 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1876 FN(p); \
1877 } \
1878 free_page((unsigned long)pt); \
1879}
1880
1881DEFINE_FREE_PT_FN(l2, free_page)
1882DEFINE_FREE_PT_FN(l3, free_pt_l2)
1883DEFINE_FREE_PT_FN(l4, free_pt_l3)
1884DEFINE_FREE_PT_FN(l5, free_pt_l4)
1885DEFINE_FREE_PT_FN(l6, free_pt_l5)
1886
Joerg Roedel86db2e52008-12-02 18:20:21 +01001887static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001888{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001889 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001890
Joerg Roedel5c34c402013-06-20 20:22:58 +02001891 switch (domain->mode) {
1892 case PAGE_MODE_NONE:
1893 break;
1894 case PAGE_MODE_1_LEVEL:
1895 free_page(root);
1896 break;
1897 case PAGE_MODE_2_LEVEL:
1898 free_pt_l2(root);
1899 break;
1900 case PAGE_MODE_3_LEVEL:
1901 free_pt_l3(root);
1902 break;
1903 case PAGE_MODE_4_LEVEL:
1904 free_pt_l4(root);
1905 break;
1906 case PAGE_MODE_5_LEVEL:
1907 free_pt_l5(root);
1908 break;
1909 case PAGE_MODE_6_LEVEL:
1910 free_pt_l6(root);
1911 break;
1912 default:
1913 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001914 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001915}
1916
Joerg Roedelb16137b2011-11-21 16:50:23 +01001917static void free_gcr3_tbl_level1(u64 *tbl)
1918{
1919 u64 *ptr;
1920 int i;
1921
1922 for (i = 0; i < 512; ++i) {
1923 if (!(tbl[i] & GCR3_VALID))
1924 continue;
1925
1926 ptr = __va(tbl[i] & PAGE_MASK);
1927
1928 free_page((unsigned long)ptr);
1929 }
1930}
1931
1932static void free_gcr3_tbl_level2(u64 *tbl)
1933{
1934 u64 *ptr;
1935 int i;
1936
1937 for (i = 0; i < 512; ++i) {
1938 if (!(tbl[i] & GCR3_VALID))
1939 continue;
1940
1941 ptr = __va(tbl[i] & PAGE_MASK);
1942
1943 free_gcr3_tbl_level1(ptr);
1944 }
1945}
1946
Joerg Roedel52815b72011-11-17 17:24:28 +01001947static void free_gcr3_table(struct protection_domain *domain)
1948{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001949 if (domain->glx == 2)
1950 free_gcr3_tbl_level2(domain->gcr3_tbl);
1951 else if (domain->glx == 1)
1952 free_gcr3_tbl_level1(domain->gcr3_tbl);
1953 else if (domain->glx != 0)
1954 BUG();
1955
Joerg Roedel52815b72011-11-17 17:24:28 +01001956 free_page((unsigned long)domain->gcr3_tbl);
1957}
1958
Joerg Roedel431b2a22008-07-11 17:14:22 +02001959/*
1960 * Free a domain, only used if something went wrong in the
1961 * allocation path and we need to free an already allocated page table
1962 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001963static void dma_ops_domain_free(struct dma_ops_domain *dom)
1964{
Joerg Roedel384de722009-05-15 12:30:05 +02001965 int i;
1966
Joerg Roedelec487d12008-06-26 21:27:58 +02001967 if (!dom)
1968 return;
1969
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001970 del_domain_from_list(&dom->domain);
1971
Joerg Roedel86db2e52008-12-02 18:20:21 +01001972 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001973
Joerg Roedel384de722009-05-15 12:30:05 +02001974 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1975 if (!dom->aperture[i])
1976 continue;
1977 free_page((unsigned long)dom->aperture[i]->bitmap);
1978 kfree(dom->aperture[i]);
1979 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001980
1981 kfree(dom);
1982}
1983
Joerg Roedel431b2a22008-07-11 17:14:22 +02001984/*
1985 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001986 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001987 * structures required for the dma_ops interface
1988 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001989static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001990{
1991 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001992
1993 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1994 if (!dma_dom)
1995 return NULL;
1996
1997 spin_lock_init(&dma_dom->domain.lock);
1998
1999 dma_dom->domain.id = domain_id_alloc();
2000 if (dma_dom->domain.id == 0)
2001 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002002 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002003 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002004 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002005 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002006 dma_dom->domain.priv = dma_dom;
2007 if (!dma_dom->domain.pt_root)
2008 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002009
Joerg Roedel1c655772008-09-04 18:40:05 +02002010 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002011 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002012
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002013 add_domain_to_list(&dma_dom->domain);
2014
Joerg Roedel576175c2009-11-23 19:08:46 +01002015 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002016 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002017
Joerg Roedel431b2a22008-07-11 17:14:22 +02002018 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002019 * mark the first page as allocated so we never return 0 as
2020 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002021 */
Joerg Roedel384de722009-05-15 12:30:05 +02002022 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02002023 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002024
Joerg Roedelec487d12008-06-26 21:27:58 +02002025
2026 return dma_dom;
2027
2028free_dma_dom:
2029 dma_ops_domain_free(dma_dom);
2030
2031 return NULL;
2032}
2033
Joerg Roedel431b2a22008-07-11 17:14:22 +02002034/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002035 * little helper function to check whether a given protection domain is a
2036 * dma_ops domain
2037 */
2038static bool dma_ops_domain(struct protection_domain *domain)
2039{
2040 return domain->flags & PD_DMA_OPS_MASK;
2041}
2042
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002043static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002044{
Joerg Roedel132bd682011-11-17 14:18:46 +01002045 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002046 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002047
Joerg Roedel132bd682011-11-17 14:18:46 +01002048 if (domain->mode != PAGE_MODE_NONE)
2049 pte_root = virt_to_phys(domain->pt_root);
2050
Joerg Roedel38ddf412008-09-11 10:38:32 +02002051 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2052 << DEV_ENTRY_MODE_SHIFT;
2053 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002054
Joerg Roedelee6c2862011-11-09 12:06:03 +01002055 flags = amd_iommu_dev_table[devid].data[1];
2056
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002057 if (ats)
2058 flags |= DTE_FLAG_IOTLB;
2059
Joerg Roedel52815b72011-11-17 17:24:28 +01002060 if (domain->flags & PD_IOMMUV2_MASK) {
2061 u64 gcr3 = __pa(domain->gcr3_tbl);
2062 u64 glx = domain->glx;
2063 u64 tmp;
2064
2065 pte_root |= DTE_FLAG_GV;
2066 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2067
2068 /* First mask out possible old values for GCR3 table */
2069 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2070 flags &= ~tmp;
2071
2072 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2073 flags &= ~tmp;
2074
2075 /* Encode GCR3 table into DTE */
2076 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2077 pte_root |= tmp;
2078
2079 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2080 flags |= tmp;
2081
2082 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2083 flags |= tmp;
2084 }
2085
Joerg Roedelee6c2862011-11-09 12:06:03 +01002086 flags &= ~(0xffffUL);
2087 flags |= domain->id;
2088
2089 amd_iommu_dev_table[devid].data[1] = flags;
2090 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002091}
2092
Joerg Roedel15898bb2009-11-24 15:39:42 +01002093static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002094{
Joerg Roedel355bf552008-12-08 12:02:41 +01002095 /* remove entry from the device table seen by the hardware */
2096 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2097 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002098
Joerg Roedelc5cca142009-10-09 18:31:20 +02002099 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002100}
2101
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002102static void do_attach(struct iommu_dev_data *dev_data,
2103 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002104{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002105 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002106 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002107
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002108 iommu = amd_iommu_rlookup_table[dev_data->devid];
2109 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002110
2111 /* Update data structures */
2112 dev_data->domain = domain;
2113 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002114 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002115
2116 /* Do reference counting */
2117 domain->dev_iommu[iommu->index] += 1;
2118 domain->dev_cnt += 1;
2119
2120 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002121 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002122}
2123
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002124static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002125{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002126 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002127
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002128 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002129
Joerg Roedelc4596112009-11-20 14:57:32 +01002130 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002131 dev_data->domain->dev_iommu[iommu->index] -= 1;
2132 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002133
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002134 /* Update data structures */
2135 dev_data->domain = NULL;
2136 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002137 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002138
2139 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002140 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002141}
2142
2143/*
2144 * If a device is not yet associated with a domain, this function does
2145 * assigns it visible for the hardware
2146 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002147static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002148 struct protection_domain *domain)
2149{
Joerg Roedel397111a2014-08-05 17:31:51 +02002150 struct iommu_dev_data *head, *entry;
Julia Lawall84fe6c12010-05-27 12:31:51 +02002151 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002152
Joerg Roedel15898bb2009-11-24 15:39:42 +01002153 /* lock domain */
2154 spin_lock(&domain->lock);
2155
Joerg Roedel397111a2014-08-05 17:31:51 +02002156 head = dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002157
Joerg Roedel397111a2014-08-05 17:31:51 +02002158 if (head->alias_data != NULL)
2159 head = head->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002160
Joerg Roedel397111a2014-08-05 17:31:51 +02002161 /* Now we have the root of the alias group, if any */
Joerg Roedel2b02b092011-06-09 17:48:39 +02002162
Joerg Roedel397111a2014-08-05 17:31:51 +02002163 ret = -EBUSY;
2164 if (head->domain != NULL)
2165 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002166
Joerg Roedel397111a2014-08-05 17:31:51 +02002167 /* Attach alias group root */
2168 do_attach(head, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002169
Joerg Roedel397111a2014-08-05 17:31:51 +02002170 /* Attach other devices in the alias group */
2171 list_for_each_entry(entry, &head->alias_list, alias_list)
2172 do_attach(entry, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002173
Julia Lawall84fe6c12010-05-27 12:31:51 +02002174 ret = 0;
2175
2176out_unlock:
2177
Joerg Roedel355bf552008-12-08 12:02:41 +01002178 /* ready */
2179 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002180
Julia Lawall84fe6c12010-05-27 12:31:51 +02002181 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002182}
2183
Joerg Roedel52815b72011-11-17 17:24:28 +01002184
2185static void pdev_iommuv2_disable(struct pci_dev *pdev)
2186{
2187 pci_disable_ats(pdev);
2188 pci_disable_pri(pdev);
2189 pci_disable_pasid(pdev);
2190}
2191
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002192/* FIXME: Change generic reset-function to do the same */
2193static int pri_reset_while_enabled(struct pci_dev *pdev)
2194{
2195 u16 control;
2196 int pos;
2197
Joerg Roedel46277b72011-12-07 14:34:02 +01002198 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002199 if (!pos)
2200 return -EINVAL;
2201
Joerg Roedel46277b72011-12-07 14:34:02 +01002202 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2203 control |= PCI_PRI_CTRL_RESET;
2204 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002205
2206 return 0;
2207}
2208
Joerg Roedel52815b72011-11-17 17:24:28 +01002209static int pdev_iommuv2_enable(struct pci_dev *pdev)
2210{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002211 bool reset_enable;
2212 int reqs, ret;
2213
2214 /* FIXME: Hardcode number of outstanding requests for now */
2215 reqs = 32;
2216 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2217 reqs = 1;
2218 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002219
2220 /* Only allow access to user-accessible pages */
2221 ret = pci_enable_pasid(pdev, 0);
2222 if (ret)
2223 goto out_err;
2224
2225 /* First reset the PRI state of the device */
2226 ret = pci_reset_pri(pdev);
2227 if (ret)
2228 goto out_err;
2229
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002230 /* Enable PRI */
2231 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002232 if (ret)
2233 goto out_err;
2234
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002235 if (reset_enable) {
2236 ret = pri_reset_while_enabled(pdev);
2237 if (ret)
2238 goto out_err;
2239 }
2240
Joerg Roedel52815b72011-11-17 17:24:28 +01002241 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2242 if (ret)
2243 goto out_err;
2244
2245 return 0;
2246
2247out_err:
2248 pci_disable_pri(pdev);
2249 pci_disable_pasid(pdev);
2250
2251 return ret;
2252}
2253
Joerg Roedelc99afa22011-11-21 18:19:25 +01002254/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002255#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002256
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002257static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002258{
Joerg Roedela3b93122012-04-12 12:49:26 +02002259 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002260 int pos;
2261
Joerg Roedel46277b72011-12-07 14:34:02 +01002262 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002263 if (!pos)
2264 return false;
2265
Joerg Roedela3b93122012-04-12 12:49:26 +02002266 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002267
Joerg Roedela3b93122012-04-12 12:49:26 +02002268 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002269}
2270
Joerg Roedel15898bb2009-11-24 15:39:42 +01002271/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002272 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002273 * assigns it visible for the hardware
2274 */
2275static int attach_device(struct device *dev,
2276 struct protection_domain *domain)
2277{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002278 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002279 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002280 unsigned long flags;
2281 int ret;
2282
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002283 dev_data = get_dev_data(dev);
2284
Joerg Roedel52815b72011-11-17 17:24:28 +01002285 if (domain->flags & PD_IOMMUV2_MASK) {
2286 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2287 return -EINVAL;
2288
2289 if (pdev_iommuv2_enable(pdev) != 0)
2290 return -EINVAL;
2291
2292 dev_data->ats.enabled = true;
2293 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002294 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002295 } else if (amd_iommu_iotlb_sup &&
2296 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002297 dev_data->ats.enabled = true;
2298 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2299 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002300
Joerg Roedel15898bb2009-11-24 15:39:42 +01002301 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002302 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002303 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2304
2305 /*
2306 * We might boot into a crash-kernel here. The crashed kernel
2307 * left the caches in the IOMMU dirty. So we have to flush
2308 * here to evict all dirty stuff.
2309 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002310 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002311
2312 return ret;
2313}
2314
2315/*
2316 * Removes a device from a protection domain (unlocked)
2317 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002318static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002319{
Joerg Roedel397111a2014-08-05 17:31:51 +02002320 struct iommu_dev_data *head, *entry;
Joerg Roedel2ca76272010-01-22 16:45:31 +01002321 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002322 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002323
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002324 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002325
Joerg Roedel2ca76272010-01-22 16:45:31 +01002326 domain = dev_data->domain;
2327
2328 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002329
Joerg Roedel397111a2014-08-05 17:31:51 +02002330 head = dev_data;
2331 if (head->alias_data != NULL)
2332 head = head->alias_data;
Joerg Roedel71f77582011-06-09 19:03:15 +02002333
Joerg Roedel397111a2014-08-05 17:31:51 +02002334 list_for_each_entry(entry, &head->alias_list, alias_list)
2335 do_detach(entry);
Joerg Roedel24100052009-11-25 15:59:57 +01002336
Joerg Roedel397111a2014-08-05 17:31:51 +02002337 do_detach(head);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002338
Joerg Roedel2ca76272010-01-22 16:45:31 +01002339 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002340
Joerg Roedel21129f72009-09-01 11:59:42 +02002341 /*
2342 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002343 * passthrough domain if it is detached from any other domain.
2344 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002345 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002346 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002347 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002348 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002349}
2350
2351/*
2352 * Removes a device from a protection domain (with devtable_lock held)
2353 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002354static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002355{
Joerg Roedel52815b72011-11-17 17:24:28 +01002356 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002357 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002358 unsigned long flags;
2359
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002360 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002361 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002362
Joerg Roedel355bf552008-12-08 12:02:41 +01002363 /* lock device table */
2364 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002365 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002366 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002367
Joerg Roedel52815b72011-11-17 17:24:28 +01002368 if (domain->flags & PD_IOMMUV2_MASK)
2369 pdev_iommuv2_disable(to_pci_dev(dev));
2370 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002371 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002372
2373 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002374}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002375
Joerg Roedel15898bb2009-11-24 15:39:42 +01002376/*
2377 * Find out the protection domain structure for a given PCI device. This
2378 * will give us the pointer to the page table root for example.
2379 */
2380static struct protection_domain *domain_for_device(struct device *dev)
2381{
Joerg Roedel71f77582011-06-09 19:03:15 +02002382 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002383 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002384 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002385
Joerg Roedel657cbb62009-11-23 15:26:46 +01002386 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002387
Joerg Roedel2b02b092011-06-09 17:48:39 +02002388 if (dev_data->domain)
2389 return dev_data->domain;
2390
Joerg Roedel71f77582011-06-09 19:03:15 +02002391 if (dev_data->alias_data != NULL) {
2392 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002393
2394 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2395 if (alias_data->domain != NULL) {
2396 __attach_device(dev_data, alias_data->domain);
2397 dom = alias_data->domain;
2398 }
2399 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002400 }
2401
Joerg Roedel15898bb2009-11-24 15:39:42 +01002402 return dom;
2403}
2404
Joerg Roedele275a2a2008-12-10 18:27:25 +01002405static int device_change_notifier(struct notifier_block *nb,
2406 unsigned long action, void *data)
2407{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002408 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002409 struct protection_domain *domain;
2410 struct iommu_dev_data *dev_data;
2411 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002412 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002413 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002414 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002415
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002416 if (!check_device(dev))
2417 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002418
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002419 devid = get_device_id(dev);
2420 iommu = amd_iommu_rlookup_table[devid];
2421 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002422
2423 switch (action) {
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002424 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002425
2426 iommu_init_device(dev);
Alex Williamson25b11ce2014-09-19 10:03:13 -06002427 init_iommu_group(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002428
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002429 /*
2430 * dev_data is still NULL and
2431 * got initialized in iommu_init_device
2432 */
2433 dev_data = get_dev_data(dev);
2434
2435 if (iommu_pass_through || dev_data->iommu_v2) {
2436 dev_data->passthrough = true;
2437 attach_device(dev, pt_domain);
2438 break;
2439 }
2440
Joerg Roedel657cbb62009-11-23 15:26:46 +01002441 domain = domain_for_device(dev);
2442
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002443 /* allocate a protection domain if a device is added */
2444 dma_domain = find_protection_domain(devid);
Joerg Roedelc2a28762013-03-26 22:48:23 +01002445 if (!dma_domain) {
2446 dma_domain = dma_ops_domain_alloc();
2447 if (!dma_domain)
2448 goto out;
2449 dma_domain->target_dev = devid;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002450
Joerg Roedelc2a28762013-03-26 22:48:23 +01002451 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2452 list_add_tail(&dma_domain->list, &iommu_pd_list);
2453 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2454 }
Joerg Roedelac1534a2012-06-21 14:52:40 +02002455
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002456 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002457
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002458 break;
Joerg Roedel6c5cc802015-04-01 14:58:44 +02002459 case BUS_NOTIFY_REMOVED_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002460
2461 iommu_uninit_device(dev);
2462
Joerg Roedele275a2a2008-12-10 18:27:25 +01002463 default:
2464 goto out;
2465 }
2466
Joerg Roedele275a2a2008-12-10 18:27:25 +01002467 iommu_completion_wait(iommu);
2468
2469out:
2470 return 0;
2471}
2472
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302473static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002474 .notifier_call = device_change_notifier,
2475};
Joerg Roedel355bf552008-12-08 12:02:41 +01002476
Joerg Roedel8638c492009-12-10 11:12:25 +01002477void amd_iommu_init_notifier(void)
2478{
2479 bus_register_notifier(&pci_bus_type, &device_nb);
2480}
2481
Joerg Roedel431b2a22008-07-11 17:14:22 +02002482/*****************************************************************************
2483 *
2484 * The next functions belong to the dma_ops mapping/unmapping code.
2485 *
2486 *****************************************************************************/
2487
2488/*
2489 * In the dma_ops path we only have the struct device. This function
2490 * finds the corresponding IOMMU, the protection domain and the
2491 * requestor id for a given device.
2492 * If the device is not yet associated with a domain this is also done
2493 * in this function.
2494 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002495static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002496{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002497 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002498 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002499 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002500
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002501 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002502 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002503
Joerg Roedel94f6d192009-11-24 16:40:02 +01002504 domain = domain_for_device(dev);
2505 if (domain != NULL && !dma_ops_domain(domain))
2506 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002507
Joerg Roedel94f6d192009-11-24 16:40:02 +01002508 if (domain != NULL)
2509 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002510
Frank Arnolddf805ab2012-08-27 19:21:04 +02002511 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002512 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002513 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002514 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2515 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002516 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002517 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002518
Joerg Roedel94f6d192009-11-24 16:40:02 +01002519 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002520}
2521
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002522static void update_device_table(struct protection_domain *domain)
2523{
Joerg Roedel492667d2009-11-27 13:25:47 +01002524 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002525
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002526 list_for_each_entry(dev_data, &domain->dev_list, list)
2527 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002528}
2529
2530static void update_domain(struct protection_domain *domain)
2531{
2532 if (!domain->updated)
2533 return;
2534
2535 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002536
2537 domain_flush_devices(domain);
2538 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002539
2540 domain->updated = false;
2541}
2542
Joerg Roedel431b2a22008-07-11 17:14:22 +02002543/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002544 * This function fetches the PTE for a given address in the aperture
2545 */
2546static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2547 unsigned long address)
2548{
Joerg Roedel384de722009-05-15 12:30:05 +02002549 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002550 u64 *pte, *pte_page;
2551
Joerg Roedel384de722009-05-15 12:30:05 +02002552 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2553 if (!aperture)
2554 return NULL;
2555
2556 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002557 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002558 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002559 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002560 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2561 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002562 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002563
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002564 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002565
2566 return pte;
2567}
2568
2569/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002570 * This is the generic map function. It maps one 4kb page at paddr to
2571 * the given address in the DMA address space for the domain.
2572 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002573static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002574 unsigned long address,
2575 phys_addr_t paddr,
2576 int direction)
2577{
2578 u64 *pte, __pte;
2579
2580 WARN_ON(address > dom->aperture_size);
2581
2582 paddr &= PAGE_MASK;
2583
Joerg Roedel8bda3092009-05-12 12:02:46 +02002584 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002585 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002586 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002587
2588 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2589
2590 if (direction == DMA_TO_DEVICE)
2591 __pte |= IOMMU_PTE_IR;
2592 else if (direction == DMA_FROM_DEVICE)
2593 __pte |= IOMMU_PTE_IW;
2594 else if (direction == DMA_BIDIRECTIONAL)
2595 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2596
2597 WARN_ON(*pte);
2598
2599 *pte = __pte;
2600
2601 return (dma_addr_t)address;
2602}
2603
Joerg Roedel431b2a22008-07-11 17:14:22 +02002604/*
2605 * The generic unmapping function for on page in the DMA address space.
2606 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002607static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002608 unsigned long address)
2609{
Joerg Roedel384de722009-05-15 12:30:05 +02002610 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002611 u64 *pte;
2612
2613 if (address >= dom->aperture_size)
2614 return;
2615
Joerg Roedel384de722009-05-15 12:30:05 +02002616 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2617 if (!aperture)
2618 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002619
Joerg Roedel384de722009-05-15 12:30:05 +02002620 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2621 if (!pte)
2622 return;
2623
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002624 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002625
2626 WARN_ON(!*pte);
2627
2628 *pte = 0ULL;
2629}
2630
Joerg Roedel431b2a22008-07-11 17:14:22 +02002631/*
2632 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002633 * contiguous memory region into DMA address space. It is used by all
2634 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002635 * Must be called with the domain lock held.
2636 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002637static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002638 struct dma_ops_domain *dma_dom,
2639 phys_addr_t paddr,
2640 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002641 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002642 bool align,
2643 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002644{
2645 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002646 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002647 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002648 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002649 int i;
2650
Joerg Roedele3c449f2008-10-15 22:02:11 -07002651 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002652 paddr &= PAGE_MASK;
2653
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002654 INC_STATS_COUNTER(total_map_requests);
2655
Joerg Roedelc1858972008-12-12 15:42:39 +01002656 if (pages > 1)
2657 INC_STATS_COUNTER(cross_page);
2658
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002659 if (align)
2660 align_mask = (1UL << get_order(size)) - 1;
2661
Joerg Roedel11b83882009-05-19 10:23:15 +02002662retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002663 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2664 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002665 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002666 /*
2667 * setting next_address here will let the address
2668 * allocator only scan the new allocated range in the
2669 * first run. This is a small optimization.
2670 */
2671 dma_dom->next_address = dma_dom->aperture_size;
2672
Joerg Roedel576175c2009-11-23 19:08:46 +01002673 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002674 goto out;
2675
2676 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002677 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002678 * allocation again
2679 */
2680 goto retry;
2681 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002682
2683 start = address;
2684 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002685 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002686 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002687 goto out_unmap;
2688
Joerg Roedelcb76c322008-06-26 21:28:00 +02002689 paddr += PAGE_SIZE;
2690 start += PAGE_SIZE;
2691 }
2692 address += offset;
2693
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002694 ADD_STATS_COUNTER(alloced_io_mem, size);
2695
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002696 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002697 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002698 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002699 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002700 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002701
Joerg Roedelcb76c322008-06-26 21:28:00 +02002702out:
2703 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002704
2705out_unmap:
2706
2707 for (--i; i >= 0; --i) {
2708 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002709 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002710 }
2711
2712 dma_ops_free_addresses(dma_dom, address, pages);
2713
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002714 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002715}
2716
Joerg Roedel431b2a22008-07-11 17:14:22 +02002717/*
2718 * Does the reverse of the __map_single function. Must be called with
2719 * the domain lock held too
2720 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002721static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002722 dma_addr_t dma_addr,
2723 size_t size,
2724 int dir)
2725{
Joerg Roedel04e04632010-09-23 16:12:48 +02002726 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002727 dma_addr_t i, start;
2728 unsigned int pages;
2729
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002730 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002731 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002732 return;
2733
Joerg Roedel04e04632010-09-23 16:12:48 +02002734 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002735 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002736 dma_addr &= PAGE_MASK;
2737 start = dma_addr;
2738
2739 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002740 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002741 start += PAGE_SIZE;
2742 }
2743
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002744 SUB_STATS_COUNTER(alloced_io_mem, size);
2745
Joerg Roedelcb76c322008-06-26 21:28:00 +02002746 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002747
Joerg Roedel80be3082008-11-06 14:59:05 +01002748 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002749 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002750 dma_dom->need_flush = false;
2751 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002752}
2753
Joerg Roedel431b2a22008-07-11 17:14:22 +02002754/*
2755 * The exported map_single function for dma_ops.
2756 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002757static dma_addr_t map_page(struct device *dev, struct page *page,
2758 unsigned long offset, size_t size,
2759 enum dma_data_direction dir,
2760 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002761{
2762 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002763 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002764 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002765 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002766 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002767
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002768 INC_STATS_COUNTER(cnt_map_single);
2769
Joerg Roedel94f6d192009-11-24 16:40:02 +01002770 domain = get_domain(dev);
2771 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002772 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002773 else if (IS_ERR(domain))
2774 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002775
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002776 dma_mask = *dev->dma_mask;
2777
Joerg Roedel4da70b92008-06-26 21:28:01 +02002778 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002779
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002780 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002781 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002782 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002783 goto out;
2784
Joerg Roedel17b124b2011-04-06 18:01:35 +02002785 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002786
2787out:
2788 spin_unlock_irqrestore(&domain->lock, flags);
2789
2790 return addr;
2791}
2792
Joerg Roedel431b2a22008-07-11 17:14:22 +02002793/*
2794 * The exported unmap_single function for dma_ops.
2795 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002796static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2797 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002798{
2799 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002800 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002801
Joerg Roedel146a6912008-12-12 15:07:12 +01002802 INC_STATS_COUNTER(cnt_unmap_single);
2803
Joerg Roedel94f6d192009-11-24 16:40:02 +01002804 domain = get_domain(dev);
2805 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002806 return;
2807
Joerg Roedel4da70b92008-06-26 21:28:01 +02002808 spin_lock_irqsave(&domain->lock, flags);
2809
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002810 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002811
Joerg Roedel17b124b2011-04-06 18:01:35 +02002812 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002813
2814 spin_unlock_irqrestore(&domain->lock, flags);
2815}
2816
Joerg Roedel431b2a22008-07-11 17:14:22 +02002817/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002818 * The exported map_sg function for dma_ops (handles scatter-gather
2819 * lists).
2820 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002821static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002822 int nelems, enum dma_data_direction dir,
2823 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002824{
2825 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002826 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002827 int i;
2828 struct scatterlist *s;
2829 phys_addr_t paddr;
2830 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002831 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002832
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002833 INC_STATS_COUNTER(cnt_map_sg);
2834
Joerg Roedel94f6d192009-11-24 16:40:02 +01002835 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002836 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002837 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002838
Joerg Roedel832a90c2008-09-18 15:54:23 +02002839 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002840
Joerg Roedel65b050a2008-06-26 21:28:02 +02002841 spin_lock_irqsave(&domain->lock, flags);
2842
2843 for_each_sg(sglist, s, nelems, i) {
2844 paddr = sg_phys(s);
2845
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002846 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002847 paddr, s->length, dir, false,
2848 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002849
2850 if (s->dma_address) {
2851 s->dma_length = s->length;
2852 mapped_elems++;
2853 } else
2854 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002855 }
2856
Joerg Roedel17b124b2011-04-06 18:01:35 +02002857 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002858
2859out:
2860 spin_unlock_irqrestore(&domain->lock, flags);
2861
2862 return mapped_elems;
2863unmap:
2864 for_each_sg(sglist, s, mapped_elems, i) {
2865 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002866 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002867 s->dma_length, dir);
2868 s->dma_address = s->dma_length = 0;
2869 }
2870
2871 mapped_elems = 0;
2872
2873 goto out;
2874}
2875
Joerg Roedel431b2a22008-07-11 17:14:22 +02002876/*
2877 * The exported map_sg function for dma_ops (handles scatter-gather
2878 * lists).
2879 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002880static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002881 int nelems, enum dma_data_direction dir,
2882 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002883{
2884 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002885 struct protection_domain *domain;
2886 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002887 int i;
2888
Joerg Roedel55877a62008-12-12 15:12:14 +01002889 INC_STATS_COUNTER(cnt_unmap_sg);
2890
Joerg Roedel94f6d192009-11-24 16:40:02 +01002891 domain = get_domain(dev);
2892 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002893 return;
2894
Joerg Roedel65b050a2008-06-26 21:28:02 +02002895 spin_lock_irqsave(&domain->lock, flags);
2896
2897 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002898 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002899 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002900 s->dma_address = s->dma_length = 0;
2901 }
2902
Joerg Roedel17b124b2011-04-06 18:01:35 +02002903 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002904
2905 spin_unlock_irqrestore(&domain->lock, flags);
2906}
2907
Joerg Roedel431b2a22008-07-11 17:14:22 +02002908/*
2909 * The exported alloc_coherent function for dma_ops.
2910 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002911static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002912 dma_addr_t *dma_addr, gfp_t flag,
2913 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002914{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002915 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002916 struct protection_domain *domain;
2917 unsigned long flags;
2918 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002919
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002920 INC_STATS_COUNTER(cnt_alloc_coherent);
2921
Joerg Roedel94f6d192009-11-24 16:40:02 +01002922 domain = get_domain(dev);
2923 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002924 page = alloc_pages(flag, get_order(size));
2925 *dma_addr = page_to_phys(page);
2926 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002927 } else if (IS_ERR(domain))
2928 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002929
Joerg Roedel3b839a52015-04-01 14:58:47 +02002930 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002931 dma_mask = dev->coherent_dma_mask;
2932 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002933 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002934
Joerg Roedel3b839a52015-04-01 14:58:47 +02002935 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2936 if (!page) {
2937 if (!(flag & __GFP_WAIT))
2938 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002939
Joerg Roedel3b839a52015-04-01 14:58:47 +02002940 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2941 get_order(size));
2942 if (!page)
2943 return NULL;
2944 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002945
Joerg Roedel832a90c2008-09-18 15:54:23 +02002946 if (!dma_mask)
2947 dma_mask = *dev->dma_mask;
2948
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002949 spin_lock_irqsave(&domain->lock, flags);
2950
Joerg Roedel3b839a52015-04-01 14:58:47 +02002951 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedel832a90c2008-09-18 15:54:23 +02002952 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002953
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002954 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002955 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002956 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002957 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002958
Joerg Roedel17b124b2011-04-06 18:01:35 +02002959 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002960
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002961 spin_unlock_irqrestore(&domain->lock, flags);
2962
Joerg Roedel3b839a52015-04-01 14:58:47 +02002963 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002964
2965out_free:
2966
Joerg Roedel3b839a52015-04-01 14:58:47 +02002967 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2968 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002969
2970 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002971}
2972
Joerg Roedel431b2a22008-07-11 17:14:22 +02002973/*
2974 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002975 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002976static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002977 void *virt_addr, dma_addr_t dma_addr,
2978 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002979{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002980 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002981 unsigned long flags;
2982 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002983
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002984 INC_STATS_COUNTER(cnt_free_coherent);
2985
Joerg Roedel3b839a52015-04-01 14:58:47 +02002986 page = virt_to_page(virt_addr);
2987 size = PAGE_ALIGN(size);
2988
Joerg Roedel94f6d192009-11-24 16:40:02 +01002989 domain = get_domain(dev);
2990 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002991 goto free_mem;
2992
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002993 spin_lock_irqsave(&domain->lock, flags);
2994
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002995 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002996
Joerg Roedel17b124b2011-04-06 18:01:35 +02002997 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002998
2999 spin_unlock_irqrestore(&domain->lock, flags);
3000
3001free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02003002 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3003 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003004}
3005
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003006/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003007 * This function is called by the DMA layer to find out if we can handle a
3008 * particular device. It is part of the dma_ops.
3009 */
3010static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3011{
Joerg Roedel420aef82009-11-23 16:14:57 +01003012 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003013}
3014
3015/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003016 * The function for pre-allocating protection domains.
3017 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003018 * If the driver core informs the DMA layer if a driver grabs a device
3019 * we don't need to preallocate the protection domains anymore.
3020 * For now we have to.
3021 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003022static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003023{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003024 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003025 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003026 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003027 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003028
Chris Wrightd18c69d2010-04-02 18:27:55 -07003029 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003030
3031 /* Do we handle this device? */
3032 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003033 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003034
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003035 dev_data = get_dev_data(&dev->dev);
3036 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3037 /* Make sure passthrough domain is allocated */
3038 alloc_passthrough_domain();
3039 dev_data->passthrough = true;
3040 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003041 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003042 dev_name(&dev->dev));
3043 }
3044
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003045 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003046 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003047 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003048
3049 devid = get_device_id(&dev->dev);
3050
Joerg Roedel87a64d52009-11-24 17:26:43 +01003051 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003052 if (!dma_dom)
3053 continue;
3054 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003055 dma_dom->target_dev = devid;
3056
Joerg Roedel15898bb2009-11-24 15:39:42 +01003057 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003058
Joerg Roedelbd60b732008-09-11 10:24:48 +02003059 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003060 }
3061}
3062
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003063static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003064 .alloc = alloc_coherent,
3065 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003066 .map_page = map_page,
3067 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003068 .map_sg = map_sg,
3069 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003070 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003071};
3072
Joerg Roedel27c21272011-05-30 15:56:24 +02003073static unsigned device_dma_ops_init(void)
3074{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003075 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003076 struct pci_dev *pdev = NULL;
3077 unsigned unhandled = 0;
3078
3079 for_each_pci_dev(pdev) {
3080 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003081
3082 iommu_ignore_device(&pdev->dev);
3083
Joerg Roedel27c21272011-05-30 15:56:24 +02003084 unhandled += 1;
3085 continue;
3086 }
3087
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003088 dev_data = get_dev_data(&pdev->dev);
3089
3090 if (!dev_data->passthrough)
3091 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3092 else
3093 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003094 }
3095
3096 return unhandled;
3097}
3098
Joerg Roedel431b2a22008-07-11 17:14:22 +02003099/*
3100 * The function which clues the AMD IOMMU driver into dma_ops.
3101 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003102
3103void __init amd_iommu_init_api(void)
3104{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003105 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003106}
3107
Joerg Roedel6631ee92008-06-26 21:28:05 +02003108int __init amd_iommu_init_dma_ops(void)
3109{
3110 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003111 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003112
Joerg Roedel431b2a22008-07-11 17:14:22 +02003113 /*
3114 * first allocate a default protection domain for every IOMMU we
3115 * found in the system. Devices not assigned to any other
3116 * protection domain will be assigned to the default one.
3117 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003118 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003119 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003120 if (iommu->default_dom == NULL)
3121 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003122 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003123 ret = iommu_init_unity_mappings(iommu);
3124 if (ret)
3125 goto free_domains;
3126 }
3127
Joerg Roedel431b2a22008-07-11 17:14:22 +02003128 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003129 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003130 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003131 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003132
3133 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003134 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003135
Joerg Roedel431b2a22008-07-11 17:14:22 +02003136 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003137 unhandled = device_dma_ops_init();
3138 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3139 /* There are unhandled devices - initialize swiotlb for them */
3140 swiotlb = 1;
3141 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003142
Joerg Roedel7f265082008-12-12 13:50:21 +01003143 amd_iommu_stats_init();
3144
Joerg Roedel62410ee2012-06-12 16:42:43 +02003145 if (amd_iommu_unmap_flush)
3146 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3147 else
3148 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3149
Joerg Roedel6631ee92008-06-26 21:28:05 +02003150 return 0;
3151
3152free_domains:
3153
Joerg Roedel3bd22172009-05-04 15:06:20 +02003154 for_each_iommu(iommu) {
Cyril Roelandt91457df2013-02-12 05:01:50 +01003155 dma_ops_domain_free(iommu->default_dom);
Joerg Roedel6631ee92008-06-26 21:28:05 +02003156 }
3157
3158 return ret;
3159}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003160
3161/*****************************************************************************
3162 *
3163 * The following functions belong to the exported interface of AMD IOMMU
3164 *
3165 * This interface allows access to lower level functions of the IOMMU
3166 * like protection domain handling and assignement of devices to domains
3167 * which is not possible with the dma_ops interface.
3168 *
3169 *****************************************************************************/
3170
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003171static void cleanup_domain(struct protection_domain *domain)
3172{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02003173 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003174 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003175
3176 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3177
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02003178 while (!list_empty(&domain->dev_list)) {
3179 entry = list_first_entry(&domain->dev_list,
3180 struct iommu_dev_data, list);
3181 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01003182 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003183
3184 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3185}
3186
Joerg Roedel26508152009-08-26 16:52:40 +02003187static void protection_domain_free(struct protection_domain *domain)
3188{
3189 if (!domain)
3190 return;
3191
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003192 del_domain_from_list(domain);
3193
Joerg Roedel26508152009-08-26 16:52:40 +02003194 if (domain->id)
3195 domain_id_free(domain->id);
3196
3197 kfree(domain);
3198}
3199
3200static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003201{
3202 struct protection_domain *domain;
3203
3204 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3205 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003206 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003207
3208 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003209 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003210 domain->id = domain_id_alloc();
3211 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003212 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003213 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003214
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003215 add_domain_to_list(domain);
3216
Joerg Roedel26508152009-08-26 16:52:40 +02003217 return domain;
3218
3219out_err:
3220 kfree(domain);
3221
3222 return NULL;
3223}
3224
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003225static int __init alloc_passthrough_domain(void)
3226{
3227 if (pt_domain != NULL)
3228 return 0;
3229
3230 /* allocate passthrough domain */
3231 pt_domain = protection_domain_alloc();
3232 if (!pt_domain)
3233 return -ENOMEM;
3234
3235 pt_domain->mode = PAGE_MODE_NONE;
3236
3237 return 0;
3238}
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003239
3240static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
3241{
3242 struct protection_domain *pdomain;
3243
3244 /* We only support unmanaged domains for now */
3245 if (type != IOMMU_DOMAIN_UNMANAGED)
3246 return NULL;
3247
3248 pdomain = protection_domain_alloc();
3249 if (!pdomain)
3250 goto out_free;
3251
3252 pdomain->mode = PAGE_MODE_3_LEVEL;
3253 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3254 if (!pdomain->pt_root)
3255 goto out_free;
3256
3257 pdomain->domain.geometry.aperture_start = 0;
3258 pdomain->domain.geometry.aperture_end = ~0ULL;
3259 pdomain->domain.geometry.force_aperture = true;
3260
3261 return &pdomain->domain;
3262
3263out_free:
3264 protection_domain_free(pdomain);
3265
3266 return NULL;
3267}
3268
3269static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02003270{
3271 struct protection_domain *domain;
3272
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003273 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01003274 return;
3275
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003276 domain = to_pdomain(dom);
3277
Joerg Roedel98383fc2008-12-02 18:34:12 +01003278 if (domain->dev_cnt > 0)
3279 cleanup_domain(domain);
3280
3281 BUG_ON(domain->dev_cnt != 0);
3282
Joerg Roedel132bd682011-11-17 14:18:46 +01003283 if (domain->mode != PAGE_MODE_NONE)
3284 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003285
Joerg Roedel52815b72011-11-17 17:24:28 +01003286 if (domain->flags & PD_IOMMUV2_MASK)
3287 free_gcr3_table(domain);
3288
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003289 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003290}
3291
Joerg Roedel684f2882008-12-08 12:07:44 +01003292static void amd_iommu_detach_device(struct iommu_domain *dom,
3293 struct device *dev)
3294{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003295 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003296 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003297 u16 devid;
3298
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003299 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003300 return;
3301
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003302 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003303
Joerg Roedel657cbb62009-11-23 15:26:46 +01003304 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003305 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003306
3307 iommu = amd_iommu_rlookup_table[devid];
3308 if (!iommu)
3309 return;
3310
Joerg Roedel684f2882008-12-08 12:07:44 +01003311 iommu_completion_wait(iommu);
3312}
3313
Joerg Roedel01106062008-12-02 19:34:11 +01003314static int amd_iommu_attach_device(struct iommu_domain *dom,
3315 struct device *dev)
3316{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003317 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003318 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003319 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003320 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003321
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003322 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003323 return -EINVAL;
3324
Joerg Roedel657cbb62009-11-23 15:26:46 +01003325 dev_data = dev->archdata.iommu;
3326
Joerg Roedelf62dda62011-06-09 12:55:35 +02003327 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003328 if (!iommu)
3329 return -EINVAL;
3330
Joerg Roedel657cbb62009-11-23 15:26:46 +01003331 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003332 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003333
Joerg Roedel15898bb2009-11-24 15:39:42 +01003334 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003335
3336 iommu_completion_wait(iommu);
3337
Joerg Roedel15898bb2009-11-24 15:39:42 +01003338 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003339}
3340
Joerg Roedel468e2362010-01-21 16:37:36 +01003341static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003342 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003343{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003344 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003345 int prot = 0;
3346 int ret;
3347
Joerg Roedel132bd682011-11-17 14:18:46 +01003348 if (domain->mode == PAGE_MODE_NONE)
3349 return -EINVAL;
3350
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003351 if (iommu_prot & IOMMU_READ)
3352 prot |= IOMMU_PROT_IR;
3353 if (iommu_prot & IOMMU_WRITE)
3354 prot |= IOMMU_PROT_IW;
3355
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003356 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003357 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003358 mutex_unlock(&domain->api_lock);
3359
Joerg Roedel795e74f72010-05-11 17:40:57 +02003360 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003361}
3362
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003363static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3364 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003365{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003366 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003367 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003368
Joerg Roedel132bd682011-11-17 14:18:46 +01003369 if (domain->mode == PAGE_MODE_NONE)
3370 return -EINVAL;
3371
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003372 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003373 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003374 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003375
Joerg Roedel17b124b2011-04-06 18:01:35 +02003376 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003377
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003378 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003379}
3380
Joerg Roedel645c4c82008-12-02 20:05:50 +01003381static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303382 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003383{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003384 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003385 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003386 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003387
Joerg Roedel132bd682011-11-17 14:18:46 +01003388 if (domain->mode == PAGE_MODE_NONE)
3389 return iova;
3390
Joerg Roedel3039ca12015-04-01 14:58:48 +02003391 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003392
Joerg Roedela6d41a42009-09-02 17:08:55 +02003393 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003394 return 0;
3395
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003396 offset_mask = pte_pgsize - 1;
3397 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003398
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003399 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003400}
3401
Joerg Roedelab636482014-09-05 10:48:21 +02003402static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003403{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003404 switch (cap) {
3405 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003406 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003407 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003408 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003409 case IOMMU_CAP_NOEXEC:
3410 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003411 }
3412
Joerg Roedelab636482014-09-05 10:48:21 +02003413 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003414}
3415
Thierry Redingb22f6432014-06-27 09:03:12 +02003416static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003417 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003418 .domain_alloc = amd_iommu_domain_alloc,
3419 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003420 .attach_dev = amd_iommu_attach_device,
3421 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003422 .map = amd_iommu_map,
3423 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003424 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003425 .iova_to_phys = amd_iommu_iova_to_phys,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003426 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003427};
3428
Joerg Roedel0feae532009-08-26 15:26:30 +02003429/*****************************************************************************
3430 *
3431 * The next functions do a basic initialization of IOMMU for pass through
3432 * mode
3433 *
3434 * In passthrough mode the IOMMU is initialized and enabled but not used for
3435 * DMA-API translation.
3436 *
3437 *****************************************************************************/
3438
3439int __init amd_iommu_init_passthrough(void)
3440{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003441 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003442 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003443 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003444
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003445 ret = alloc_passthrough_domain();
3446 if (ret)
3447 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003448
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003449 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003450 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003451 continue;
3452
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003453 dev_data = get_dev_data(&dev->dev);
3454 dev_data->passthrough = true;
3455
Joerg Roedel15898bb2009-11-24 15:39:42 +01003456 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003457 }
3458
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003459 amd_iommu_stats_init();
3460
Joerg Roedel0feae532009-08-26 15:26:30 +02003461 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3462
3463 return 0;
3464}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003465
3466/* IOMMUv2 specific functions */
3467int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3468{
3469 return atomic_notifier_chain_register(&ppr_notifier, nb);
3470}
3471EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3472
3473int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3474{
3475 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3476}
3477EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003478
3479void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3480{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003481 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003482 unsigned long flags;
3483
3484 spin_lock_irqsave(&domain->lock, flags);
3485
3486 /* Update data structure */
3487 domain->mode = PAGE_MODE_NONE;
3488 domain->updated = true;
3489
3490 /* Make changes visible to IOMMUs */
3491 update_domain(domain);
3492
3493 /* Page-table is not visible to IOMMU anymore, so free it */
3494 free_pagetable(domain);
3495
3496 spin_unlock_irqrestore(&domain->lock, flags);
3497}
3498EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003499
3500int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3501{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003502 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003503 unsigned long flags;
3504 int levels, ret;
3505
3506 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3507 return -EINVAL;
3508
3509 /* Number of GCR3 table levels required */
3510 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3511 levels += 1;
3512
3513 if (levels > amd_iommu_max_glx_val)
3514 return -EINVAL;
3515
3516 spin_lock_irqsave(&domain->lock, flags);
3517
3518 /*
3519 * Save us all sanity checks whether devices already in the
3520 * domain support IOMMUv2. Just force that the domain has no
3521 * devices attached when it is switched into IOMMUv2 mode.
3522 */
3523 ret = -EBUSY;
3524 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3525 goto out;
3526
3527 ret = -ENOMEM;
3528 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3529 if (domain->gcr3_tbl == NULL)
3530 goto out;
3531
3532 domain->glx = levels;
3533 domain->flags |= PD_IOMMUV2_MASK;
3534 domain->updated = true;
3535
3536 update_domain(domain);
3537
3538 ret = 0;
3539
3540out:
3541 spin_unlock_irqrestore(&domain->lock, flags);
3542
3543 return ret;
3544}
3545EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003546
3547static int __flush_pasid(struct protection_domain *domain, int pasid,
3548 u64 address, bool size)
3549{
3550 struct iommu_dev_data *dev_data;
3551 struct iommu_cmd cmd;
3552 int i, ret;
3553
3554 if (!(domain->flags & PD_IOMMUV2_MASK))
3555 return -EINVAL;
3556
3557 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3558
3559 /*
3560 * IOMMU TLB needs to be flushed before Device TLB to
3561 * prevent device TLB refill from IOMMU TLB
3562 */
3563 for (i = 0; i < amd_iommus_present; ++i) {
3564 if (domain->dev_iommu[i] == 0)
3565 continue;
3566
3567 ret = iommu_queue_command(amd_iommus[i], &cmd);
3568 if (ret != 0)
3569 goto out;
3570 }
3571
3572 /* Wait until IOMMU TLB flushes are complete */
3573 domain_flush_complete(domain);
3574
3575 /* Now flush device TLBs */
3576 list_for_each_entry(dev_data, &domain->dev_list, list) {
3577 struct amd_iommu *iommu;
3578 int qdep;
3579
3580 BUG_ON(!dev_data->ats.enabled);
3581
3582 qdep = dev_data->ats.qdep;
3583 iommu = amd_iommu_rlookup_table[dev_data->devid];
3584
3585 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3586 qdep, address, size);
3587
3588 ret = iommu_queue_command(iommu, &cmd);
3589 if (ret != 0)
3590 goto out;
3591 }
3592
3593 /* Wait until all device TLBs are flushed */
3594 domain_flush_complete(domain);
3595
3596 ret = 0;
3597
3598out:
3599
3600 return ret;
3601}
3602
3603static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3604 u64 address)
3605{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003606 INC_STATS_COUNTER(invalidate_iotlb);
3607
Joerg Roedel22e266c2011-11-21 15:59:08 +01003608 return __flush_pasid(domain, pasid, address, false);
3609}
3610
3611int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3612 u64 address)
3613{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003614 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003615 unsigned long flags;
3616 int ret;
3617
3618 spin_lock_irqsave(&domain->lock, flags);
3619 ret = __amd_iommu_flush_page(domain, pasid, address);
3620 spin_unlock_irqrestore(&domain->lock, flags);
3621
3622 return ret;
3623}
3624EXPORT_SYMBOL(amd_iommu_flush_page);
3625
3626static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3627{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003628 INC_STATS_COUNTER(invalidate_iotlb_all);
3629
Joerg Roedel22e266c2011-11-21 15:59:08 +01003630 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3631 true);
3632}
3633
3634int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3635{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003636 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003637 unsigned long flags;
3638 int ret;
3639
3640 spin_lock_irqsave(&domain->lock, flags);
3641 ret = __amd_iommu_flush_tlb(domain, pasid);
3642 spin_unlock_irqrestore(&domain->lock, flags);
3643
3644 return ret;
3645}
3646EXPORT_SYMBOL(amd_iommu_flush_tlb);
3647
Joerg Roedelb16137b2011-11-21 16:50:23 +01003648static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3649{
3650 int index;
3651 u64 *pte;
3652
3653 while (true) {
3654
3655 index = (pasid >> (9 * level)) & 0x1ff;
3656 pte = &root[index];
3657
3658 if (level == 0)
3659 break;
3660
3661 if (!(*pte & GCR3_VALID)) {
3662 if (!alloc)
3663 return NULL;
3664
3665 root = (void *)get_zeroed_page(GFP_ATOMIC);
3666 if (root == NULL)
3667 return NULL;
3668
3669 *pte = __pa(root) | GCR3_VALID;
3670 }
3671
3672 root = __va(*pte & PAGE_MASK);
3673
3674 level -= 1;
3675 }
3676
3677 return pte;
3678}
3679
3680static int __set_gcr3(struct protection_domain *domain, int pasid,
3681 unsigned long cr3)
3682{
3683 u64 *pte;
3684
3685 if (domain->mode != PAGE_MODE_NONE)
3686 return -EINVAL;
3687
3688 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3689 if (pte == NULL)
3690 return -ENOMEM;
3691
3692 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3693
3694 return __amd_iommu_flush_tlb(domain, pasid);
3695}
3696
3697static int __clear_gcr3(struct protection_domain *domain, int pasid)
3698{
3699 u64 *pte;
3700
3701 if (domain->mode != PAGE_MODE_NONE)
3702 return -EINVAL;
3703
3704 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3705 if (pte == NULL)
3706 return 0;
3707
3708 *pte = 0;
3709
3710 return __amd_iommu_flush_tlb(domain, pasid);
3711}
3712
3713int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3714 unsigned long cr3)
3715{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003716 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003717 unsigned long flags;
3718 int ret;
3719
3720 spin_lock_irqsave(&domain->lock, flags);
3721 ret = __set_gcr3(domain, pasid, cr3);
3722 spin_unlock_irqrestore(&domain->lock, flags);
3723
3724 return ret;
3725}
3726EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3727
3728int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3729{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003730 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003731 unsigned long flags;
3732 int ret;
3733
3734 spin_lock_irqsave(&domain->lock, flags);
3735 ret = __clear_gcr3(domain, pasid);
3736 spin_unlock_irqrestore(&domain->lock, flags);
3737
3738 return ret;
3739}
3740EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003741
3742int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3743 int status, int tag)
3744{
3745 struct iommu_dev_data *dev_data;
3746 struct amd_iommu *iommu;
3747 struct iommu_cmd cmd;
3748
Joerg Roedel399be2f2011-12-01 16:53:47 +01003749 INC_STATS_COUNTER(complete_ppr);
3750
Joerg Roedelc99afa22011-11-21 18:19:25 +01003751 dev_data = get_dev_data(&pdev->dev);
3752 iommu = amd_iommu_rlookup_table[dev_data->devid];
3753
3754 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3755 tag, dev_data->pri_tlp);
3756
3757 return iommu_queue_command(iommu, &cmd);
3758}
3759EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003760
3761struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3762{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003763 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003764
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003765 pdomain = get_domain(&pdev->dev);
3766 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003767 return NULL;
3768
3769 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003770 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003771 return NULL;
3772
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003773 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003774}
3775EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003776
3777void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3778{
3779 struct iommu_dev_data *dev_data;
3780
3781 if (!amd_iommu_v2_supported())
3782 return;
3783
3784 dev_data = get_dev_data(&pdev->dev);
3785 dev_data->errata |= (1 << erratum);
3786}
3787EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003788
3789int amd_iommu_device_info(struct pci_dev *pdev,
3790 struct amd_iommu_device_info *info)
3791{
3792 int max_pasids;
3793 int pos;
3794
3795 if (pdev == NULL || info == NULL)
3796 return -EINVAL;
3797
3798 if (!amd_iommu_v2_supported())
3799 return -EINVAL;
3800
3801 memset(info, 0, sizeof(*info));
3802
3803 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3804 if (pos)
3805 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3806
3807 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3808 if (pos)
3809 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3810
3811 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3812 if (pos) {
3813 int features;
3814
3815 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3816 max_pasids = min(max_pasids, (1 << 20));
3817
3818 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3819 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3820
3821 features = pci_pasid_features(pdev);
3822 if (features & PCI_PASID_CAP_EXEC)
3823 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3824 if (features & PCI_PASID_CAP_PRIV)
3825 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3826 }
3827
3828 return 0;
3829}
3830EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003831
3832#ifdef CONFIG_IRQ_REMAP
3833
3834/*****************************************************************************
3835 *
3836 * Interrupt Remapping Implementation
3837 *
3838 *****************************************************************************/
3839
3840union irte {
3841 u32 val;
3842 struct {
3843 u32 valid : 1,
3844 no_fault : 1,
3845 int_type : 3,
3846 rq_eoi : 1,
3847 dm : 1,
3848 rsvd_1 : 1,
3849 destination : 8,
3850 vector : 8,
3851 rsvd_2 : 8;
3852 } fields;
3853};
3854
3855#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3856#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3857#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3858#define DTE_IRQ_REMAP_ENABLE 1ULL
3859
3860static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3861{
3862 u64 dte;
3863
3864 dte = amd_iommu_dev_table[devid].data[2];
3865 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3866 dte |= virt_to_phys(table->table);
3867 dte |= DTE_IRQ_REMAP_INTCTL;
3868 dte |= DTE_IRQ_TABLE_LEN;
3869 dte |= DTE_IRQ_REMAP_ENABLE;
3870
3871 amd_iommu_dev_table[devid].data[2] = dte;
3872}
3873
3874#define IRTE_ALLOCATED (~1U)
3875
3876static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3877{
3878 struct irq_remap_table *table = NULL;
3879 struct amd_iommu *iommu;
3880 unsigned long flags;
3881 u16 alias;
3882
3883 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3884
3885 iommu = amd_iommu_rlookup_table[devid];
3886 if (!iommu)
3887 goto out_unlock;
3888
3889 table = irq_lookup_table[devid];
3890 if (table)
3891 goto out;
3892
3893 alias = amd_iommu_alias_table[devid];
3894 table = irq_lookup_table[alias];
3895 if (table) {
3896 irq_lookup_table[devid] = table;
3897 set_dte_irq_entry(devid, table);
3898 iommu_flush_dte(iommu, devid);
3899 goto out;
3900 }
3901
3902 /* Nothing there yet, allocate new irq remapping table */
3903 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3904 if (!table)
3905 goto out;
3906
Joerg Roedel197887f2013-04-09 21:14:08 +02003907 /* Initialize table spin-lock */
3908 spin_lock_init(&table->lock);
3909
Joerg Roedel2b324502012-06-21 16:29:10 +02003910 if (ioapic)
3911 /* Keep the first 32 indexes free for IOAPIC interrupts */
3912 table->min_index = 32;
3913
3914 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3915 if (!table->table) {
3916 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003917 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003918 goto out;
3919 }
3920
3921 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3922
3923 if (ioapic) {
3924 int i;
3925
3926 for (i = 0; i < 32; ++i)
3927 table->table[i] = IRTE_ALLOCATED;
3928 }
3929
3930 irq_lookup_table[devid] = table;
3931 set_dte_irq_entry(devid, table);
3932 iommu_flush_dte(iommu, devid);
3933 if (devid != alias) {
3934 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003935 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003936 iommu_flush_dte(iommu, alias);
3937 }
3938
3939out:
3940 iommu_completion_wait(iommu);
3941
3942out_unlock:
3943 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3944
3945 return table;
3946}
3947
3948static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3949{
3950 struct irq_remap_table *table;
3951 unsigned long flags;
3952 int index, c;
3953
3954 table = get_irq_table(devid, false);
3955 if (!table)
3956 return -ENODEV;
3957
3958 spin_lock_irqsave(&table->lock, flags);
3959
3960 /* Scan table for free entries */
3961 for (c = 0, index = table->min_index;
3962 index < MAX_IRQS_PER_TABLE;
3963 ++index) {
3964 if (table->table[index] == 0)
3965 c += 1;
3966 else
3967 c = 0;
3968
3969 if (c == count) {
Joerg Roedel0dfedd62013-04-09 15:39:16 +02003970 struct irq_2_irte *irte_info;
Joerg Roedel2b324502012-06-21 16:29:10 +02003971
3972 for (; c != 0; --c)
3973 table->table[index - c + 1] = IRTE_ALLOCATED;
3974
3975 index -= count - 1;
3976
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02003977 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02003978 irte_info = &cfg->irq_2_irte;
3979 irte_info->devid = devid;
3980 irte_info->index = index;
Joerg Roedel2b324502012-06-21 16:29:10 +02003981
3982 goto out;
3983 }
3984 }
3985
3986 index = -ENOSPC;
3987
3988out:
3989 spin_unlock_irqrestore(&table->lock, flags);
3990
3991 return index;
3992}
3993
3994static int get_irte(u16 devid, int index, union irte *irte)
3995{
3996 struct irq_remap_table *table;
3997 unsigned long flags;
3998
3999 table = get_irq_table(devid, false);
4000 if (!table)
4001 return -ENOMEM;
4002
4003 spin_lock_irqsave(&table->lock, flags);
4004 irte->val = table->table[index];
4005 spin_unlock_irqrestore(&table->lock, flags);
4006
4007 return 0;
4008}
4009
4010static int modify_irte(u16 devid, int index, union irte irte)
4011{
4012 struct irq_remap_table *table;
4013 struct amd_iommu *iommu;
4014 unsigned long flags;
4015
4016 iommu = amd_iommu_rlookup_table[devid];
4017 if (iommu == NULL)
4018 return -EINVAL;
4019
4020 table = get_irq_table(devid, false);
4021 if (!table)
4022 return -ENOMEM;
4023
4024 spin_lock_irqsave(&table->lock, flags);
4025 table->table[index] = irte.val;
4026 spin_unlock_irqrestore(&table->lock, flags);
4027
4028 iommu_flush_irt(iommu, devid);
4029 iommu_completion_wait(iommu);
4030
4031 return 0;
4032}
4033
4034static void free_irte(u16 devid, int index)
4035{
4036 struct irq_remap_table *table;
4037 struct amd_iommu *iommu;
4038 unsigned long flags;
4039
4040 iommu = amd_iommu_rlookup_table[devid];
4041 if (iommu == NULL)
4042 return;
4043
4044 table = get_irq_table(devid, false);
4045 if (!table)
4046 return;
4047
4048 spin_lock_irqsave(&table->lock, flags);
4049 table->table[index] = 0;
4050 spin_unlock_irqrestore(&table->lock, flags);
4051
4052 iommu_flush_irt(iommu, devid);
4053 iommu_completion_wait(iommu);
4054}
4055
Joerg Roedel5527de72012-06-26 11:17:32 +02004056static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4057 unsigned int destination, int vector,
4058 struct io_apic_irq_attr *attr)
4059{
4060 struct irq_remap_table *table;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004061 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004062 struct irq_cfg *cfg;
4063 union irte irte;
4064 int ioapic_id;
4065 int index;
4066 int devid;
4067 int ret;
4068
Jiang Liu719b5302014-10-27 16:12:10 +08004069 cfg = irq_cfg(irq);
Joerg Roedel5527de72012-06-26 11:17:32 +02004070 if (!cfg)
4071 return -EINVAL;
4072
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004073 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004074 ioapic_id = mpc_ioapic_id(attr->ioapic);
4075 devid = get_ioapic_devid(ioapic_id);
4076
4077 if (devid < 0)
4078 return devid;
4079
4080 table = get_irq_table(devid, true);
4081 if (table == NULL)
4082 return -ENOMEM;
4083
4084 index = attr->ioapic_pin;
4085
4086 /* Setup IRQ remapping info */
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004087 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004088 irte_info->devid = devid;
4089 irte_info->index = index;
Joerg Roedel5527de72012-06-26 11:17:32 +02004090
4091 /* Setup IRTE for IOMMU */
4092 irte.val = 0;
4093 irte.fields.vector = vector;
4094 irte.fields.int_type = apic->irq_delivery_mode;
4095 irte.fields.destination = destination;
4096 irte.fields.dm = apic->irq_dest_mode;
4097 irte.fields.valid = 1;
4098
4099 ret = modify_irte(devid, index, irte);
4100 if (ret)
4101 return ret;
4102
4103 /* Setup IOAPIC entry */
4104 memset(entry, 0, sizeof(*entry));
4105
4106 entry->vector = index;
4107 entry->mask = 0;
4108 entry->trigger = attr->trigger;
4109 entry->polarity = attr->polarity;
4110
4111 /*
4112 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004113 */
4114 if (attr->trigger)
4115 entry->mask = 1;
4116
4117 return 0;
4118}
4119
4120static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4121 bool force)
4122{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004123 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004124 unsigned int dest, irq;
4125 struct irq_cfg *cfg;
4126 union irte irte;
4127 int err;
4128
4129 if (!config_enabled(CONFIG_SMP))
4130 return -1;
4131
Jiang Liu719b5302014-10-27 16:12:10 +08004132 cfg = irqd_cfg(data);
Joerg Roedel5527de72012-06-26 11:17:32 +02004133 irq = data->irq;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004134 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004135
4136 if (!cpumask_intersects(mask, cpu_online_mask))
4137 return -EINVAL;
4138
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004139 if (get_irte(irte_info->devid, irte_info->index, &irte))
Joerg Roedel5527de72012-06-26 11:17:32 +02004140 return -EBUSY;
4141
4142 if (assign_irq_vector(irq, cfg, mask))
4143 return -EBUSY;
4144
4145 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4146 if (err) {
4147 if (assign_irq_vector(irq, cfg, data->affinity))
4148 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4149 return err;
4150 }
4151
4152 irte.fields.vector = cfg->vector;
4153 irte.fields.destination = dest;
4154
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004155 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel5527de72012-06-26 11:17:32 +02004156
4157 if (cfg->move_in_progress)
4158 send_cleanup_vector(cfg);
4159
4160 cpumask_copy(data->affinity, mask);
4161
4162 return 0;
4163}
4164
4165static int free_irq(int irq)
4166{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004167 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004168 struct irq_cfg *cfg;
4169
Jiang Liu719b5302014-10-27 16:12:10 +08004170 cfg = irq_cfg(irq);
Joerg Roedel5527de72012-06-26 11:17:32 +02004171 if (!cfg)
4172 return -EINVAL;
4173
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004174 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004175
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004176 free_irte(irte_info->devid, irte_info->index);
Joerg Roedel5527de72012-06-26 11:17:32 +02004177
4178 return 0;
4179}
4180
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004181static void compose_msi_msg(struct pci_dev *pdev,
4182 unsigned int irq, unsigned int dest,
4183 struct msi_msg *msg, u8 hpet_id)
4184{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004185 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004186 struct irq_cfg *cfg;
4187 union irte irte;
4188
Jiang Liu719b5302014-10-27 16:12:10 +08004189 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004190 if (!cfg)
4191 return;
4192
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004193 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004194
4195 irte.val = 0;
4196 irte.fields.vector = cfg->vector;
4197 irte.fields.int_type = apic->irq_delivery_mode;
4198 irte.fields.destination = dest;
4199 irte.fields.dm = apic->irq_dest_mode;
4200 irte.fields.valid = 1;
4201
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004202 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004203
4204 msg->address_hi = MSI_ADDR_BASE_HI;
4205 msg->address_lo = MSI_ADDR_BASE_LO;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004206 msg->data = irte_info->index;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004207}
4208
4209static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4210{
4211 struct irq_cfg *cfg;
4212 int index;
4213 u16 devid;
4214
4215 if (!pdev)
4216 return -EINVAL;
4217
Jiang Liu719b5302014-10-27 16:12:10 +08004218 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004219 if (!cfg)
4220 return -EINVAL;
4221
4222 devid = get_device_id(&pdev->dev);
4223 index = alloc_irq_index(cfg, devid, nvec);
4224
4225 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4226}
4227
4228static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4229 int index, int offset)
4230{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004231 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004232 struct irq_cfg *cfg;
4233 u16 devid;
4234
4235 if (!pdev)
4236 return -EINVAL;
4237
Jiang Liu719b5302014-10-27 16:12:10 +08004238 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004239 if (!cfg)
4240 return -EINVAL;
4241
4242 if (index >= MAX_IRQS_PER_TABLE)
4243 return 0;
4244
4245 devid = get_device_id(&pdev->dev);
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004246 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004247
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004248 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004249 irte_info->devid = devid;
4250 irte_info->index = index + offset;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004251
4252 return 0;
4253}
4254
Yijing Wang5fc24d82014-09-17 17:32:19 +08004255static int alloc_hpet_msi(unsigned int irq, unsigned int id)
Joerg Roedeld9761952012-06-26 16:00:08 +02004256{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004257 struct irq_2_irte *irte_info;
Joerg Roedeld9761952012-06-26 16:00:08 +02004258 struct irq_cfg *cfg;
4259 int index, devid;
4260
Jiang Liu719b5302014-10-27 16:12:10 +08004261 cfg = irq_cfg(irq);
Joerg Roedeld9761952012-06-26 16:00:08 +02004262 if (!cfg)
4263 return -EINVAL;
4264
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004265 irte_info = &cfg->irq_2_irte;
Joerg Roedeld9761952012-06-26 16:00:08 +02004266 devid = get_hpet_devid(id);
4267 if (devid < 0)
4268 return devid;
4269
4270 index = alloc_irq_index(cfg, devid, 1);
4271 if (index < 0)
4272 return index;
4273
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004274 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004275 irte_info->devid = devid;
4276 irte_info->index = index;
Joerg Roedeld9761952012-06-26 16:00:08 +02004277
4278 return 0;
4279}
4280
Joerg Roedel6b474b82012-06-26 16:46:04 +02004281struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02004282 .prepare = amd_iommu_prepare,
4283 .enable = amd_iommu_enable,
4284 .disable = amd_iommu_disable,
4285 .reenable = amd_iommu_reenable,
4286 .enable_faulting = amd_iommu_enable_faulting,
4287 .setup_ioapic_entry = setup_ioapic_entry,
4288 .set_affinity = set_affinity,
4289 .free_irq = free_irq,
4290 .compose_msi_msg = compose_msi_msg,
4291 .msi_alloc_irq = msi_alloc_irq,
4292 .msi_setup_irq = msi_setup_irq,
Yijing Wang5fc24d82014-09-17 17:32:19 +08004293 .alloc_hpet_msi = alloc_hpet_msi,
Joerg Roedel6b474b82012-06-26 16:46:04 +02004294};
Joerg Roedel2b324502012-06-21 16:29:10 +02004295#endif