blob: e8c412dcac2189c5a1e4987f751d4db20214a53c [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>
36#include <asm/irq_remapping.h>
37#include <asm/io_apic.h>
38#include <asm/apic.h>
39#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020040#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020041#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090042#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010043#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020044#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020045
46#include "amd_iommu_proto.h"
47#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020048#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020049
50#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
51
Joerg Roedel815b33f2011-04-06 17:26:49 +020052#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020053
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020054/*
55 * This bitmap is used to advertise the page sizes our hardware support
56 * to the IOMMU core, which will then use this information to split
57 * physically contiguous memory regions it is mapping into page sizes
58 * that we support.
59 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010060 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020061 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010062#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020063
Joerg Roedelb6c02712008-06-26 21:27:53 +020064static DEFINE_RWLOCK(amd_iommu_devtable_lock);
65
Joerg Roedelbd60b732008-09-11 10:24:48 +020066/* A list of preallocated protection domains */
67static LIST_HEAD(iommu_pd_list);
68static DEFINE_SPINLOCK(iommu_pd_list_lock);
69
Joerg Roedel8fa5f802011-06-09 12:24:45 +020070/* List of all available dev_data structures */
71static LIST_HEAD(dev_data_list);
72static DEFINE_SPINLOCK(dev_data_list_lock);
73
Joerg Roedel6efed632012-06-14 15:52:58 +020074LIST_HEAD(ioapic_map);
75LIST_HEAD(hpet_map);
76
Joerg Roedel0feae532009-08-26 15:26:30 +020077/*
78 * Domain for untranslated devices - only allocated
79 * if iommu=pt passed on kernel cmd line.
80 */
81static struct protection_domain *pt_domain;
82
Thierry Redingb22f6432014-06-27 09:03:12 +020083static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010084
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010085static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010086int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010087
Joerg Roedelac1534a2012-06-21 14:52:40 +020088static struct dma_map_ops amd_iommu_dma_ops;
89
Joerg Roedel431b2a22008-07-11 17:14:22 +020090/*
Joerg Roedel50917e22014-08-05 16:38:38 +020091 * This struct contains device specific data for the IOMMU
92 */
93struct iommu_dev_data {
94 struct list_head list; /* For domain->dev_list */
95 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedelf251e182014-08-05 16:48:10 +020096 struct list_head alias_list; /* Link alias-groups together */
Joerg Roedel50917e22014-08-05 16:38:38 +020097 struct iommu_dev_data *alias_data;/* The alias dev_data */
98 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +020099 u16 devid; /* PCI Device ID */
100 bool iommu_v2; /* Device can make use of IOMMUv2 */
101 bool passthrough; /* Default for device is pt_domain */
102 struct {
103 bool enabled;
104 int qdep;
105 } ats; /* ATS state */
106 bool pri_tlp; /* PASID TLB required for
107 PPR completions */
108 u32 errata; /* Bitmap for errata to apply */
109};
110
111/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200112 * general struct to manage commands send to an IOMMU
113 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200114struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200115 u32 data[4];
116};
117
Joerg Roedel05152a02012-06-15 16:53:51 +0200118struct kmem_cache *amd_iommu_irq_cache;
119
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200120static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100121static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700122
Joerg Roedel15898bb2009-11-24 15:39:42 +0100123/****************************************************************************
124 *
125 * Helper functions
126 *
127 ****************************************************************************/
128
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100129static struct protection_domain *to_pdomain(struct iommu_domain *dom)
130{
131 return container_of(dom, struct protection_domain, domain);
132}
133
Joerg Roedelf62dda62011-06-09 12:55:35 +0200134static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200135{
136 struct iommu_dev_data *dev_data;
137 unsigned long flags;
138
139 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
140 if (!dev_data)
141 return NULL;
142
Joerg Roedelf251e182014-08-05 16:48:10 +0200143 INIT_LIST_HEAD(&dev_data->alias_list);
144
Joerg Roedelf62dda62011-06-09 12:55:35 +0200145 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200146
147 spin_lock_irqsave(&dev_data_list_lock, flags);
148 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
149 spin_unlock_irqrestore(&dev_data_list_lock, flags);
150
151 return dev_data;
152}
153
154static void free_dev_data(struct iommu_dev_data *dev_data)
155{
156 unsigned long flags;
157
158 spin_lock_irqsave(&dev_data_list_lock, flags);
159 list_del(&dev_data->dev_data_list);
160 spin_unlock_irqrestore(&dev_data_list_lock, flags);
161
162 kfree(dev_data);
163}
164
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200165static struct iommu_dev_data *search_dev_data(u16 devid)
166{
167 struct iommu_dev_data *dev_data;
168 unsigned long flags;
169
170 spin_lock_irqsave(&dev_data_list_lock, flags);
171 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
172 if (dev_data->devid == devid)
173 goto out_unlock;
174 }
175
176 dev_data = NULL;
177
178out_unlock:
179 spin_unlock_irqrestore(&dev_data_list_lock, flags);
180
181 return dev_data;
182}
183
184static struct iommu_dev_data *find_dev_data(u16 devid)
185{
186 struct iommu_dev_data *dev_data;
187
188 dev_data = search_dev_data(devid);
189
190 if (dev_data == NULL)
191 dev_data = alloc_dev_data(devid);
192
193 return dev_data;
194}
195
Joerg Roedel15898bb2009-11-24 15:39:42 +0100196static inline u16 get_device_id(struct device *dev)
197{
198 struct pci_dev *pdev = to_pci_dev(dev);
199
Shuah Khan6f2729b2013-02-27 17:07:30 -0700200 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100201}
202
Joerg Roedel657cbb62009-11-23 15:26:46 +0100203static struct iommu_dev_data *get_dev_data(struct device *dev)
204{
205 return dev->archdata.iommu;
206}
207
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100208static bool pci_iommuv2_capable(struct pci_dev *pdev)
209{
210 static const int caps[] = {
211 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100212 PCI_EXT_CAP_ID_PRI,
213 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100214 };
215 int i, pos;
216
217 for (i = 0; i < 3; ++i) {
218 pos = pci_find_ext_capability(pdev, caps[i]);
219 if (pos == 0)
220 return false;
221 }
222
223 return true;
224}
225
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100226static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
227{
228 struct iommu_dev_data *dev_data;
229
230 dev_data = get_dev_data(&pdev->dev);
231
232 return dev_data->errata & (1 << erratum) ? true : false;
233}
234
Joerg Roedel71c70982009-11-24 16:43:06 +0100235/*
236 * In this function the list of preallocated protection domains is traversed to
237 * find the domain for a specific device
238 */
239static struct dma_ops_domain *find_protection_domain(u16 devid)
240{
241 struct dma_ops_domain *entry, *ret = NULL;
242 unsigned long flags;
243 u16 alias = amd_iommu_alias_table[devid];
244
245 if (list_empty(&iommu_pd_list))
246 return NULL;
247
248 spin_lock_irqsave(&iommu_pd_list_lock, flags);
249
250 list_for_each_entry(entry, &iommu_pd_list, list) {
251 if (entry->target_dev == devid ||
252 entry->target_dev == alias) {
253 ret = entry;
254 break;
255 }
256 }
257
258 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
259
260 return ret;
261}
262
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100263/*
264 * This function checks if the driver got a valid device from the caller to
265 * avoid dereferencing invalid pointers.
266 */
267static bool check_device(struct device *dev)
268{
269 u16 devid;
270
271 if (!dev || !dev->dma_mask)
272 return false;
273
Yijing Wangb82a2272013-12-05 19:42:41 +0800274 /* No PCI device */
275 if (!dev_is_pci(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100276 return false;
277
278 devid = get_device_id(dev);
279
280 /* Out of our scope? */
281 if (devid > amd_iommu_last_bdf)
282 return false;
283
284 if (amd_iommu_rlookup_table[devid] == NULL)
285 return false;
286
287 return true;
288}
289
Alex Williamson25b11ce2014-09-19 10:03:13 -0600290static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600291{
Alex Williamson2851db22012-10-08 22:49:41 -0600292 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600293
Alex Williamson65d53522014-07-03 09:51:30 -0600294 group = iommu_group_get_for_dev(dev);
Alex Williamson25b11ce2014-09-19 10:03:13 -0600295 if (!IS_ERR(group))
296 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600297}
298
Alex Williamsonc1931092014-07-03 09:51:24 -0600299static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
300{
301 *(u16 *)data = alias;
302 return 0;
303}
304
305static u16 get_alias(struct device *dev)
306{
307 struct pci_dev *pdev = to_pci_dev(dev);
308 u16 devid, ivrs_alias, pci_alias;
309
310 devid = get_device_id(dev);
311 ivrs_alias = amd_iommu_alias_table[devid];
312 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
313
314 if (ivrs_alias == pci_alias)
315 return ivrs_alias;
316
317 /*
318 * DMA alias showdown
319 *
320 * The IVRS is fairly reliable in telling us about aliases, but it
321 * can't know about every screwy device. If we don't have an IVRS
322 * reported alias, use the PCI reported alias. In that case we may
323 * still need to initialize the rlookup and dev_table entries if the
324 * alias is to a non-existent device.
325 */
326 if (ivrs_alias == devid) {
327 if (!amd_iommu_rlookup_table[pci_alias]) {
328 amd_iommu_rlookup_table[pci_alias] =
329 amd_iommu_rlookup_table[devid];
330 memcpy(amd_iommu_dev_table[pci_alias].data,
331 amd_iommu_dev_table[devid].data,
332 sizeof(amd_iommu_dev_table[pci_alias].data));
333 }
334
335 return pci_alias;
336 }
337
338 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
339 "for device %s[%04x:%04x], kernel reported alias "
340 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
341 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
342 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
343 PCI_FUNC(pci_alias));
344
345 /*
346 * If we don't have a PCI DMA alias and the IVRS alias is on the same
347 * bus, then the IVRS table may know about a quirk that we don't.
348 */
349 if (pci_alias == devid &&
350 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
351 pdev->dev_flags |= PCI_DEV_FLAGS_DMA_ALIAS_DEVFN;
352 pdev->dma_alias_devfn = ivrs_alias & 0xff;
353 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
354 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
355 dev_name(dev));
356 }
357
358 return ivrs_alias;
359}
360
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600361static int iommu_init_device(struct device *dev)
362{
363 struct pci_dev *pdev = to_pci_dev(dev);
364 struct iommu_dev_data *dev_data;
365 u16 alias;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600366
367 if (dev->archdata.iommu)
368 return 0;
369
370 dev_data = find_dev_data(get_device_id(dev));
371 if (!dev_data)
372 return -ENOMEM;
373
Alex Williamsonc1931092014-07-03 09:51:24 -0600374 alias = get_alias(dev);
375
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600376 if (alias != dev_data->devid) {
377 struct iommu_dev_data *alias_data;
378
379 alias_data = find_dev_data(alias);
380 if (alias_data == NULL) {
381 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
382 dev_name(dev));
383 free_dev_data(dev_data);
384 return -ENOTSUPP;
385 }
386 dev_data->alias_data = alias_data;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600387
Joerg Roedelf251e182014-08-05 16:48:10 +0200388 /* Add device to the alias_list */
389 list_add(&dev_data->alias_list, &alias_data->alias_list);
Radmila Kompováe644a012013-05-02 17:24:25 +0200390 }
Alex Williamson9dcd6132012-05-30 14:19:07 -0600391
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100392 if (pci_iommuv2_capable(pdev)) {
393 struct amd_iommu *iommu;
394
395 iommu = amd_iommu_rlookup_table[dev_data->devid];
396 dev_data->iommu_v2 = iommu->is_iommu_v2;
397 }
398
Joerg Roedel657cbb62009-11-23 15:26:46 +0100399 dev->archdata.iommu = dev_data;
400
Alex Williamson066f2e92014-06-12 16:12:37 -0600401 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
402 dev);
403
Joerg Roedel657cbb62009-11-23 15:26:46 +0100404 return 0;
405}
406
Joerg Roedel26018872011-06-06 16:50:14 +0200407static void iommu_ignore_device(struct device *dev)
408{
409 u16 devid, alias;
410
411 devid = get_device_id(dev);
412 alias = amd_iommu_alias_table[devid];
413
414 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
415 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
416
417 amd_iommu_rlookup_table[devid] = NULL;
418 amd_iommu_rlookup_table[alias] = NULL;
419}
420
Joerg Roedel657cbb62009-11-23 15:26:46 +0100421static void iommu_uninit_device(struct device *dev)
422{
Alex Williamsonc1931092014-07-03 09:51:24 -0600423 struct iommu_dev_data *dev_data = search_dev_data(get_device_id(dev));
424
425 if (!dev_data)
426 return;
427
Alex Williamson066f2e92014-06-12 16:12:37 -0600428 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
429 dev);
430
Alex Williamson9dcd6132012-05-30 14:19:07 -0600431 iommu_group_remove_device(dev);
432
Alex Williamsonc1931092014-07-03 09:51:24 -0600433 /* Unlink from alias, it may change if another device is re-plugged */
434 dev_data->alias_data = NULL;
435
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200436 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600437 * We keep dev_data around for unplugged devices and reuse it when the
438 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200439 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100440}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100441
442void __init amd_iommu_uninit_devices(void)
443{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200444 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100445 struct pci_dev *pdev = NULL;
446
447 for_each_pci_dev(pdev) {
448
449 if (!check_device(&pdev->dev))
450 continue;
451
452 iommu_uninit_device(&pdev->dev);
453 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200454
455 /* Free all of our dev_data structures */
456 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
457 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100458}
459
460int __init amd_iommu_init_devices(void)
461{
462 struct pci_dev *pdev = NULL;
463 int ret = 0;
464
465 for_each_pci_dev(pdev) {
466
467 if (!check_device(&pdev->dev))
468 continue;
469
470 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200471 if (ret == -ENOTSUPP)
472 iommu_ignore_device(&pdev->dev);
473 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100474 goto out_free;
475 }
476
Alex Williamson25b11ce2014-09-19 10:03:13 -0600477 /*
478 * Initialize IOMMU groups only after iommu_init_device() has
479 * had a chance to populate any IVRS defined aliases.
480 */
481 for_each_pci_dev(pdev) {
482 if (check_device(&pdev->dev))
483 init_iommu_group(&pdev->dev);
484 }
485
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100486 return 0;
487
488out_free:
489
490 amd_iommu_uninit_devices();
491
492 return ret;
493}
Joerg Roedel7f265082008-12-12 13:50:21 +0100494#ifdef CONFIG_AMD_IOMMU_STATS
495
496/*
497 * Initialization code for statistics collection
498 */
499
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100500DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100501DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100502DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100503DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100504DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100505DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100506DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100507DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100508DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100509DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100510DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100511DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100512DECLARE_STATS_COUNTER(complete_ppr);
513DECLARE_STATS_COUNTER(invalidate_iotlb);
514DECLARE_STATS_COUNTER(invalidate_iotlb_all);
515DECLARE_STATS_COUNTER(pri_requests);
516
Joerg Roedel7f265082008-12-12 13:50:21 +0100517static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100518static struct dentry *de_fflush;
519
520static void amd_iommu_stats_add(struct __iommu_counter *cnt)
521{
522 if (stats_dir == NULL)
523 return;
524
525 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
526 &cnt->value);
527}
528
529static void amd_iommu_stats_init(void)
530{
531 stats_dir = debugfs_create_dir("amd-iommu", NULL);
532 if (stats_dir == NULL)
533 return;
534
Joerg Roedel7f265082008-12-12 13:50:21 +0100535 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300536 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100537
538 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100539 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100540 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100541 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100542 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100543 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100544 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100545 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100546 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100547 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100548 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100549 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100550 amd_iommu_stats_add(&complete_ppr);
551 amd_iommu_stats_add(&invalidate_iotlb);
552 amd_iommu_stats_add(&invalidate_iotlb_all);
553 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100554}
555
556#endif
557
Joerg Roedel431b2a22008-07-11 17:14:22 +0200558/****************************************************************************
559 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200560 * Interrupt handling functions
561 *
562 ****************************************************************************/
563
Joerg Roedele3e59872009-09-03 14:02:10 +0200564static void dump_dte_entry(u16 devid)
565{
566 int i;
567
Joerg Roedelee6c2862011-11-09 12:06:03 +0100568 for (i = 0; i < 4; ++i)
569 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200570 amd_iommu_dev_table[devid].data[i]);
571}
572
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200573static void dump_command(unsigned long phys_addr)
574{
575 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
576 int i;
577
578 for (i = 0; i < 4; ++i)
579 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
580}
581
Joerg Roedela345b232009-09-03 15:01:43 +0200582static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200583{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200584 int type, devid, domid, flags;
585 volatile u32 *event = __evt;
586 int count = 0;
587 u64 address;
588
589retry:
590 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
591 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
592 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
593 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
594 address = (u64)(((u64)event[3]) << 32) | event[2];
595
596 if (type == 0) {
597 /* Did we hit the erratum? */
598 if (++count == LOOP_TIMEOUT) {
599 pr_err("AMD-Vi: No event written to event log\n");
600 return;
601 }
602 udelay(1);
603 goto retry;
604 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200605
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200606 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200607
608 switch (type) {
609 case EVENT_TYPE_ILL_DEV:
610 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
611 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700612 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200613 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200614 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200615 break;
616 case EVENT_TYPE_IO_FAULT:
617 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
618 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700619 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200620 domid, address, flags);
621 break;
622 case EVENT_TYPE_DEV_TAB_ERR:
623 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
624 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700625 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200626 address, flags);
627 break;
628 case EVENT_TYPE_PAGE_TAB_ERR:
629 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
630 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700631 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200632 domid, address, flags);
633 break;
634 case EVENT_TYPE_ILL_CMD:
635 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200636 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200637 break;
638 case EVENT_TYPE_CMD_HARD_ERR:
639 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
640 "flags=0x%04x]\n", address, flags);
641 break;
642 case EVENT_TYPE_IOTLB_INV_TO:
643 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
644 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700645 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200646 address);
647 break;
648 case EVENT_TYPE_INV_DEV_REQ:
649 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
650 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700651 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200652 address, flags);
653 break;
654 default:
655 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
656 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200657
658 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200659}
660
661static void iommu_poll_events(struct amd_iommu *iommu)
662{
663 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200664
665 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
666 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
667
668 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200669 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200670 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
671 }
672
673 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200674}
675
Joerg Roedeleee53532012-06-01 15:20:23 +0200676static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100677{
678 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100679
Joerg Roedel399be2f2011-12-01 16:53:47 +0100680 INC_STATS_COUNTER(pri_requests);
681
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100682 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
683 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
684 return;
685 }
686
687 fault.address = raw[1];
688 fault.pasid = PPR_PASID(raw[0]);
689 fault.device_id = PPR_DEVID(raw[0]);
690 fault.tag = PPR_TAG(raw[0]);
691 fault.flags = PPR_FLAGS(raw[0]);
692
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100693 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
694}
695
696static void iommu_poll_ppr_log(struct amd_iommu *iommu)
697{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100698 u32 head, tail;
699
700 if (iommu->ppr_log == NULL)
701 return;
702
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100703 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
704 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
705
706 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200707 volatile u64 *raw;
708 u64 entry[2];
709 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100710
Joerg Roedeleee53532012-06-01 15:20:23 +0200711 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100712
Joerg Roedeleee53532012-06-01 15:20:23 +0200713 /*
714 * Hardware bug: Interrupt may arrive before the entry is
715 * written to memory. If this happens we need to wait for the
716 * entry to arrive.
717 */
718 for (i = 0; i < LOOP_TIMEOUT; ++i) {
719 if (PPR_REQ_TYPE(raw[0]) != 0)
720 break;
721 udelay(1);
722 }
723
724 /* Avoid memcpy function-call overhead */
725 entry[0] = raw[0];
726 entry[1] = raw[1];
727
728 /*
729 * To detect the hardware bug we need to clear the entry
730 * back to zero.
731 */
732 raw[0] = raw[1] = 0UL;
733
734 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100735 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
736 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200737
Joerg Roedeleee53532012-06-01 15:20:23 +0200738 /* Handle PPR entry */
739 iommu_handle_ppr_entry(iommu, entry);
740
Joerg Roedeleee53532012-06-01 15:20:23 +0200741 /* Refresh ring-buffer information */
742 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100743 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
744 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100745}
746
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200747irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200748{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500749 struct amd_iommu *iommu = (struct amd_iommu *) data;
750 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200751
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500752 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
753 /* Enable EVT and PPR interrupts again */
754 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
755 iommu->mmio_base + MMIO_STATUS_OFFSET);
756
757 if (status & MMIO_STATUS_EVT_INT_MASK) {
758 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
759 iommu_poll_events(iommu);
760 }
761
762 if (status & MMIO_STATUS_PPR_INT_MASK) {
763 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
764 iommu_poll_ppr_log(iommu);
765 }
766
767 /*
768 * Hardware bug: ERBT1312
769 * When re-enabling interrupt (by writing 1
770 * to clear the bit), the hardware might also try to set
771 * the interrupt bit in the event status register.
772 * In this scenario, the bit will be set, and disable
773 * subsequent interrupts.
774 *
775 * Workaround: The IOMMU driver should read back the
776 * status register and check if the interrupt bits are cleared.
777 * If not, driver will need to go through the interrupt handler
778 * again and re-clear the bits
779 */
780 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100781 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200782 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200783}
784
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200785irqreturn_t amd_iommu_int_handler(int irq, void *data)
786{
787 return IRQ_WAKE_THREAD;
788}
789
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200790/****************************************************************************
791 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200792 * IOMMU command queuing functions
793 *
794 ****************************************************************************/
795
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200796static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200797{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200798 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200799
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200800 while (*sem == 0 && i < LOOP_TIMEOUT) {
801 udelay(1);
802 i += 1;
803 }
804
805 if (i == LOOP_TIMEOUT) {
806 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
807 return -EIO;
808 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200809
810 return 0;
811}
812
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200813static void copy_cmd_to_buffer(struct amd_iommu *iommu,
814 struct iommu_cmd *cmd,
815 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200816{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200817 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200818
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200819 target = iommu->cmd_buf + tail;
820 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200821
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200822 /* Copy command to buffer */
823 memcpy(target, cmd, sizeof(*cmd));
824
825 /* Tell the IOMMU about it */
826 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
827}
828
Joerg Roedel815b33f2011-04-06 17:26:49 +0200829static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200830{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200831 WARN_ON(address & 0x7ULL);
832
Joerg Roedelded46732011-04-06 10:53:48 +0200833 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200834 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
835 cmd->data[1] = upper_32_bits(__pa(address));
836 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200837 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
838}
839
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200840static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
841{
842 memset(cmd, 0, sizeof(*cmd));
843 cmd->data[0] = devid;
844 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
845}
846
Joerg Roedel11b64022011-04-06 11:49:28 +0200847static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
848 size_t size, u16 domid, int pde)
849{
850 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100851 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200852
853 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100854 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200855
856 if (pages > 1) {
857 /*
858 * If we have to flush more than one page, flush all
859 * TLB entries for this domain
860 */
861 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100862 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200863 }
864
865 address &= PAGE_MASK;
866
867 memset(cmd, 0, sizeof(*cmd));
868 cmd->data[1] |= domid;
869 cmd->data[2] = lower_32_bits(address);
870 cmd->data[3] = upper_32_bits(address);
871 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
872 if (s) /* size bit - we flush more than one 4kb page */
873 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200874 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200875 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
876}
877
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200878static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
879 u64 address, size_t size)
880{
881 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100882 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200883
884 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100885 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200886
887 if (pages > 1) {
888 /*
889 * If we have to flush more than one page, flush all
890 * TLB entries for this domain
891 */
892 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100893 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200894 }
895
896 address &= PAGE_MASK;
897
898 memset(cmd, 0, sizeof(*cmd));
899 cmd->data[0] = devid;
900 cmd->data[0] |= (qdep & 0xff) << 24;
901 cmd->data[1] = devid;
902 cmd->data[2] = lower_32_bits(address);
903 cmd->data[3] = upper_32_bits(address);
904 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
905 if (s)
906 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
907}
908
Joerg Roedel22e266c2011-11-21 15:59:08 +0100909static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
910 u64 address, bool size)
911{
912 memset(cmd, 0, sizeof(*cmd));
913
914 address &= ~(0xfffULL);
915
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600916 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100917 cmd->data[1] = domid;
918 cmd->data[2] = lower_32_bits(address);
919 cmd->data[3] = upper_32_bits(address);
920 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
921 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
922 if (size)
923 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
924 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
925}
926
927static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
928 int qdep, u64 address, bool size)
929{
930 memset(cmd, 0, sizeof(*cmd));
931
932 address &= ~(0xfffULL);
933
934 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600935 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100936 cmd->data[0] |= (qdep & 0xff) << 24;
937 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600938 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100939 cmd->data[2] = lower_32_bits(address);
940 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
941 cmd->data[3] = upper_32_bits(address);
942 if (size)
943 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
944 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
945}
946
Joerg Roedelc99afa22011-11-21 18:19:25 +0100947static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
948 int status, int tag, bool gn)
949{
950 memset(cmd, 0, sizeof(*cmd));
951
952 cmd->data[0] = devid;
953 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600954 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100955 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
956 }
957 cmd->data[3] = tag & 0x1ff;
958 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
959
960 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
961}
962
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200963static void build_inv_all(struct iommu_cmd *cmd)
964{
965 memset(cmd, 0, sizeof(*cmd));
966 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200967}
968
Joerg Roedel7ef27982012-06-21 16:46:04 +0200969static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
970{
971 memset(cmd, 0, sizeof(*cmd));
972 cmd->data[0] = devid;
973 CMD_SET_TYPE(cmd, CMD_INV_IRT);
974}
975
Joerg Roedel431b2a22008-07-11 17:14:22 +0200976/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200977 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200978 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200979 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200980static int iommu_queue_command_sync(struct amd_iommu *iommu,
981 struct iommu_cmd *cmd,
982 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200983{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200984 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200985 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200986
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200987 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100988
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200989again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200990 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200991
992 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
993 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
994 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
995 left = (head - next_tail) % iommu->cmd_buf_size;
996
997 if (left <= 2) {
998 struct iommu_cmd sync_cmd;
999 volatile u64 sem = 0;
1000 int ret;
1001
1002 build_completion_wait(&sync_cmd, (u64)&sem);
1003 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1004
1005 spin_unlock_irqrestore(&iommu->lock, flags);
1006
1007 if ((ret = wait_on_sem(&sem)) != 0)
1008 return ret;
1009
1010 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001011 }
1012
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001013 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001014
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001015 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001016 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001017
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001018 spin_unlock_irqrestore(&iommu->lock, flags);
1019
Joerg Roedel815b33f2011-04-06 17:26:49 +02001020 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001021}
1022
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001023static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1024{
1025 return iommu_queue_command_sync(iommu, cmd, true);
1026}
1027
Joerg Roedel8d201962008-12-02 20:34:41 +01001028/*
1029 * This function queues a completion wait command into the command
1030 * buffer of an IOMMU
1031 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001032static int iommu_completion_wait(struct amd_iommu *iommu)
1033{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001034 struct iommu_cmd cmd;
1035 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001036 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001037
1038 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001039 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001040
Joerg Roedel815b33f2011-04-06 17:26:49 +02001041 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001042
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001043 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001044 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001045 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001046
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001047 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001048}
1049
Joerg Roedeld8c13082011-04-06 18:51:26 +02001050static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001051{
1052 struct iommu_cmd cmd;
1053
Joerg Roedeld8c13082011-04-06 18:51:26 +02001054 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001055
Joerg Roedeld8c13082011-04-06 18:51:26 +02001056 return iommu_queue_command(iommu, &cmd);
1057}
1058
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001059static void iommu_flush_dte_all(struct amd_iommu *iommu)
1060{
1061 u32 devid;
1062
1063 for (devid = 0; devid <= 0xffff; ++devid)
1064 iommu_flush_dte(iommu, devid);
1065
1066 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001067}
1068
1069/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001070 * This function uses heavy locking and may disable irqs for some time. But
1071 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001072 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001073static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001074{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001075 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001076
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001077 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1078 struct iommu_cmd cmd;
1079 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1080 dom_id, 1);
1081 iommu_queue_command(iommu, &cmd);
1082 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001083
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001084 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001085}
1086
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001087static void iommu_flush_all(struct amd_iommu *iommu)
1088{
1089 struct iommu_cmd cmd;
1090
1091 build_inv_all(&cmd);
1092
1093 iommu_queue_command(iommu, &cmd);
1094 iommu_completion_wait(iommu);
1095}
1096
Joerg Roedel7ef27982012-06-21 16:46:04 +02001097static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1098{
1099 struct iommu_cmd cmd;
1100
1101 build_inv_irt(&cmd, devid);
1102
1103 iommu_queue_command(iommu, &cmd);
1104}
1105
1106static void iommu_flush_irt_all(struct amd_iommu *iommu)
1107{
1108 u32 devid;
1109
1110 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1111 iommu_flush_irt(iommu, devid);
1112
1113 iommu_completion_wait(iommu);
1114}
1115
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001116void iommu_flush_all_caches(struct amd_iommu *iommu)
1117{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001118 if (iommu_feature(iommu, FEATURE_IA)) {
1119 iommu_flush_all(iommu);
1120 } else {
1121 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001122 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001123 iommu_flush_tlb_all(iommu);
1124 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001125}
1126
Joerg Roedel431b2a22008-07-11 17:14:22 +02001127/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001128 * Command send function for flushing on-device TLB
1129 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001130static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1131 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001132{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001133 struct amd_iommu *iommu;
1134 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001135 int qdep;
1136
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001137 qdep = dev_data->ats.qdep;
1138 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001139
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001140 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001141
1142 return iommu_queue_command(iommu, &cmd);
1143}
1144
1145/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001146 * Command send function for invalidating a device table entry
1147 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001148static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001149{
1150 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001151 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001152
Joerg Roedel6c542042011-06-09 17:07:31 +02001153 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001154
Joerg Roedelf62dda62011-06-09 12:55:35 +02001155 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001156 if (ret)
1157 return ret;
1158
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001159 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001160 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001161
1162 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001163}
1164
Joerg Roedel431b2a22008-07-11 17:14:22 +02001165/*
1166 * TLB invalidation function which is called from the mapping functions.
1167 * It invalidates a single PTE if the range to flush is within a single
1168 * page. Otherwise it flushes the whole TLB of the IOMMU.
1169 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001170static void __domain_flush_pages(struct protection_domain *domain,
1171 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001172{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001173 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001174 struct iommu_cmd cmd;
1175 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001176
Joerg Roedel11b64022011-04-06 11:49:28 +02001177 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001178
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001179 for (i = 0; i < amd_iommus_present; ++i) {
1180 if (!domain->dev_iommu[i])
1181 continue;
1182
1183 /*
1184 * Devices of this domain are behind this IOMMU
1185 * We need a TLB flush
1186 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001187 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001188 }
1189
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001190 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001191
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001192 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001193 continue;
1194
Joerg Roedel6c542042011-06-09 17:07:31 +02001195 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001196 }
1197
Joerg Roedel11b64022011-04-06 11:49:28 +02001198 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001199}
1200
Joerg Roedel17b124b2011-04-06 18:01:35 +02001201static void domain_flush_pages(struct protection_domain *domain,
1202 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001203{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001204 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001205}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001206
Joerg Roedel1c655772008-09-04 18:40:05 +02001207/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001208static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001209{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001210 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001211}
1212
Chris Wright42a49f92009-06-15 15:42:00 +02001213/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001214static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001215{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001216 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1217}
1218
1219static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001220{
1221 int i;
1222
1223 for (i = 0; i < amd_iommus_present; ++i) {
1224 if (!domain->dev_iommu[i])
1225 continue;
1226
1227 /*
1228 * Devices of this domain are behind this IOMMU
1229 * We need to wait for completion of all commands.
1230 */
1231 iommu_completion_wait(amd_iommus[i]);
1232 }
1233}
1234
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001235
Joerg Roedel43f49602008-12-02 21:01:12 +01001236/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001237 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001238 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001239static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001240{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001241 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001242
1243 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001244 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001245}
1246
Joerg Roedel431b2a22008-07-11 17:14:22 +02001247/****************************************************************************
1248 *
1249 * The functions below are used the create the page table mappings for
1250 * unity mapped regions.
1251 *
1252 ****************************************************************************/
1253
1254/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001255 * This function is used to add another level to an IO page table. Adding
1256 * another level increases the size of the address space by 9 bits to a size up
1257 * to 64 bits.
1258 */
1259static bool increase_address_space(struct protection_domain *domain,
1260 gfp_t gfp)
1261{
1262 u64 *pte;
1263
1264 if (domain->mode == PAGE_MODE_6_LEVEL)
1265 /* address space already 64 bit large */
1266 return false;
1267
1268 pte = (void *)get_zeroed_page(gfp);
1269 if (!pte)
1270 return false;
1271
1272 *pte = PM_LEVEL_PDE(domain->mode,
1273 virt_to_phys(domain->pt_root));
1274 domain->pt_root = pte;
1275 domain->mode += 1;
1276 domain->updated = true;
1277
1278 return true;
1279}
1280
1281static u64 *alloc_pte(struct protection_domain *domain,
1282 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001283 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001284 u64 **pte_page,
1285 gfp_t gfp)
1286{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001287 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001288 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001289
1290 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001291
1292 while (address > PM_LEVEL_SIZE(domain->mode))
1293 increase_address_space(domain, gfp);
1294
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001295 level = domain->mode - 1;
1296 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1297 address = PAGE_SIZE_ALIGN(address, page_size);
1298 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001299
1300 while (level > end_lvl) {
1301 if (!IOMMU_PTE_PRESENT(*pte)) {
1302 page = (u64 *)get_zeroed_page(gfp);
1303 if (!page)
1304 return NULL;
1305 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1306 }
1307
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001308 /* No level skipping support yet */
1309 if (PM_PTE_LEVEL(*pte) != level)
1310 return NULL;
1311
Joerg Roedel308973d2009-11-24 17:43:32 +01001312 level -= 1;
1313
1314 pte = IOMMU_PTE_PAGE(*pte);
1315
1316 if (pte_page && level == end_lvl)
1317 *pte_page = pte;
1318
1319 pte = &pte[PM_LEVEL_INDEX(level, address)];
1320 }
1321
1322 return pte;
1323}
1324
1325/*
1326 * This function checks if there is a PTE for a given dma address. If
1327 * there is one, it returns the pointer to it.
1328 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001329static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001330{
1331 int level;
1332 u64 *pte;
1333
Joerg Roedel24cd7722010-01-19 17:27:39 +01001334 if (address > PM_LEVEL_SIZE(domain->mode))
1335 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001336
Joerg Roedel24cd7722010-01-19 17:27:39 +01001337 level = domain->mode - 1;
1338 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1339
1340 while (level > 0) {
1341
1342 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001343 if (!IOMMU_PTE_PRESENT(*pte))
1344 return NULL;
1345
Joerg Roedel24cd7722010-01-19 17:27:39 +01001346 /* Large PTE */
1347 if (PM_PTE_LEVEL(*pte) == 0x07) {
1348 unsigned long pte_mask, __pte;
1349
1350 /*
1351 * If we have a series of large PTEs, make
1352 * sure to return a pointer to the first one.
1353 */
1354 pte_mask = PTE_PAGE_SIZE(*pte);
1355 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1356 __pte = ((unsigned long)pte) & pte_mask;
1357
1358 return (u64 *)__pte;
1359 }
1360
1361 /* No level skipping support yet */
1362 if (PM_PTE_LEVEL(*pte) != level)
1363 return NULL;
1364
Joerg Roedel308973d2009-11-24 17:43:32 +01001365 level -= 1;
1366
Joerg Roedel24cd7722010-01-19 17:27:39 +01001367 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001368 pte = IOMMU_PTE_PAGE(*pte);
1369 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001370 }
1371
1372 return pte;
1373}
1374
1375/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001376 * Generic mapping functions. It maps a physical address into a DMA
1377 * address space. It allocates the page table pages if necessary.
1378 * In the future it can be extended to a generic mapping function
1379 * supporting all features of AMD IOMMU page tables like level skipping
1380 * and full 64 bit address spaces.
1381 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001382static int iommu_map_page(struct protection_domain *dom,
1383 unsigned long bus_addr,
1384 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001385 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001386 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001387{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001388 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001389 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001390
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001391 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001392 return -EINVAL;
1393
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001394 bus_addr = PAGE_ALIGN(bus_addr);
1395 phys_addr = PAGE_ALIGN(phys_addr);
1396 count = PAGE_SIZE_PTE_COUNT(page_size);
1397 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001398
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001399 if (!pte)
1400 return -ENOMEM;
1401
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001402 for (i = 0; i < count; ++i)
1403 if (IOMMU_PTE_PRESENT(pte[i]))
1404 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001405
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001406 if (page_size > PAGE_SIZE) {
1407 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1408 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1409 } else
1410 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1411
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001412 if (prot & IOMMU_PROT_IR)
1413 __pte |= IOMMU_PTE_IR;
1414 if (prot & IOMMU_PROT_IW)
1415 __pte |= IOMMU_PTE_IW;
1416
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001417 for (i = 0; i < count; ++i)
1418 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001419
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001420 update_domain(dom);
1421
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001422 return 0;
1423}
1424
Joerg Roedel24cd7722010-01-19 17:27:39 +01001425static unsigned long iommu_unmap_page(struct protection_domain *dom,
1426 unsigned long bus_addr,
1427 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001428{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001429 unsigned long long unmap_size, unmapped;
1430 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001431
Joerg Roedel24cd7722010-01-19 17:27:39 +01001432 BUG_ON(!is_power_of_2(page_size));
1433
1434 unmapped = 0;
1435
1436 while (unmapped < page_size) {
1437
1438 pte = fetch_pte(dom, bus_addr);
1439
1440 if (!pte) {
1441 /*
1442 * No PTE for this address
1443 * move forward in 4kb steps
1444 */
1445 unmap_size = PAGE_SIZE;
1446 } else if (PM_PTE_LEVEL(*pte) == 0) {
1447 /* 4kb PTE found for this address */
1448 unmap_size = PAGE_SIZE;
1449 *pte = 0ULL;
1450 } else {
1451 int count, i;
1452
1453 /* Large PTE found which maps this address */
1454 unmap_size = PTE_PAGE_SIZE(*pte);
Alex Williamson60d0ca32013-06-21 14:33:19 -06001455
1456 /* Only unmap from the first pte in the page */
1457 if ((unmap_size - 1) & bus_addr)
1458 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001459 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1460 for (i = 0; i < count; i++)
1461 pte[i] = 0ULL;
1462 }
1463
1464 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1465 unmapped += unmap_size;
1466 }
1467
Alex Williamson60d0ca32013-06-21 14:33:19 -06001468 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001469
1470 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001471}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001472
Joerg Roedel431b2a22008-07-11 17:14:22 +02001473/*
1474 * This function checks if a specific unity mapping entry is needed for
1475 * this specific IOMMU.
1476 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001477static int iommu_for_unity_map(struct amd_iommu *iommu,
1478 struct unity_map_entry *entry)
1479{
1480 u16 bdf, i;
1481
1482 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1483 bdf = amd_iommu_alias_table[i];
1484 if (amd_iommu_rlookup_table[bdf] == iommu)
1485 return 1;
1486 }
1487
1488 return 0;
1489}
1490
Joerg Roedel431b2a22008-07-11 17:14:22 +02001491/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001492 * This function actually applies the mapping to the page table of the
1493 * dma_ops domain.
1494 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001495static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1496 struct unity_map_entry *e)
1497{
1498 u64 addr;
1499 int ret;
1500
1501 for (addr = e->address_start; addr < e->address_end;
1502 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001503 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001504 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001505 if (ret)
1506 return ret;
1507 /*
1508 * if unity mapping is in aperture range mark the page
1509 * as allocated in the aperture
1510 */
1511 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001512 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001513 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001514 }
1515
1516 return 0;
1517}
1518
Joerg Roedel431b2a22008-07-11 17:14:22 +02001519/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001520 * Init the unity mappings for a specific IOMMU in the system
1521 *
1522 * Basically iterates over all unity mapping entries and applies them to
1523 * the default domain DMA of that IOMMU if necessary.
1524 */
1525static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1526{
1527 struct unity_map_entry *entry;
1528 int ret;
1529
1530 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1531 if (!iommu_for_unity_map(iommu, entry))
1532 continue;
1533 ret = dma_ops_unity_map(iommu->default_dom, entry);
1534 if (ret)
1535 return ret;
1536 }
1537
1538 return 0;
1539}
1540
1541/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001542 * Inits the unity mappings required for a specific device
1543 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001544static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1545 u16 devid)
1546{
1547 struct unity_map_entry *e;
1548 int ret;
1549
1550 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1551 if (!(devid >= e->devid_start && devid <= e->devid_end))
1552 continue;
1553 ret = dma_ops_unity_map(dma_dom, e);
1554 if (ret)
1555 return ret;
1556 }
1557
1558 return 0;
1559}
1560
Joerg Roedel431b2a22008-07-11 17:14:22 +02001561/****************************************************************************
1562 *
1563 * The next functions belong to the address allocator for the dma_ops
1564 * interface functions. They work like the allocators in the other IOMMU
1565 * drivers. Its basically a bitmap which marks the allocated pages in
1566 * the aperture. Maybe it could be enhanced in the future to a more
1567 * efficient allocator.
1568 *
1569 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001570
Joerg Roedel431b2a22008-07-11 17:14:22 +02001571/*
Joerg Roedel384de722009-05-15 12:30:05 +02001572 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001573 *
1574 * called with domain->lock held
1575 */
Joerg Roedel384de722009-05-15 12:30:05 +02001576
Joerg Roedel9cabe892009-05-18 16:38:55 +02001577/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001578 * Used to reserve address ranges in the aperture (e.g. for exclusion
1579 * ranges.
1580 */
1581static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1582 unsigned long start_page,
1583 unsigned int pages)
1584{
1585 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1586
1587 if (start_page + pages > last_page)
1588 pages = last_page - start_page;
1589
1590 for (i = start_page; i < start_page + pages; ++i) {
1591 int index = i / APERTURE_RANGE_PAGES;
1592 int page = i % APERTURE_RANGE_PAGES;
1593 __set_bit(page, dom->aperture[index]->bitmap);
1594 }
1595}
1596
1597/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001598 * This function is used to add a new aperture range to an existing
1599 * aperture in case of dma_ops domain allocation or address allocation
1600 * failure.
1601 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001602static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001603 bool populate, gfp_t gfp)
1604{
1605 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001606 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001607 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001608
Joerg Roedelf5e97052009-05-22 12:31:53 +02001609#ifdef CONFIG_IOMMU_STRESS
1610 populate = false;
1611#endif
1612
Joerg Roedel9cabe892009-05-18 16:38:55 +02001613 if (index >= APERTURE_MAX_RANGES)
1614 return -ENOMEM;
1615
1616 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1617 if (!dma_dom->aperture[index])
1618 return -ENOMEM;
1619
1620 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1621 if (!dma_dom->aperture[index]->bitmap)
1622 goto out_free;
1623
1624 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1625
1626 if (populate) {
1627 unsigned long address = dma_dom->aperture_size;
1628 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1629 u64 *pte, *pte_page;
1630
1631 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001632 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001633 &pte_page, gfp);
1634 if (!pte)
1635 goto out_free;
1636
1637 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1638
1639 address += APERTURE_RANGE_SIZE / 64;
1640 }
1641 }
1642
Joerg Roedel17f5b562011-07-06 17:14:44 +02001643 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001644 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1645
Joerg Roedel17f5b562011-07-06 17:14:44 +02001646 /* Reserve address range used for MSI messages */
1647 if (old_size < MSI_ADDR_BASE_LO &&
1648 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1649 unsigned long spage;
1650 int pages;
1651
1652 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1653 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1654
1655 dma_ops_reserve_addresses(dma_dom, spage, pages);
1656 }
1657
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001658 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001659 for_each_iommu(iommu) {
1660 if (iommu->exclusion_start &&
1661 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1662 && iommu->exclusion_start < dma_dom->aperture_size) {
1663 unsigned long startpage;
1664 int pages = iommu_num_pages(iommu->exclusion_start,
1665 iommu->exclusion_length,
1666 PAGE_SIZE);
1667 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1668 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1669 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001670 }
1671
1672 /*
1673 * Check for areas already mapped as present in the new aperture
1674 * range and mark those pages as reserved in the allocator. Such
1675 * mappings may already exist as a result of requested unity
1676 * mappings for devices.
1677 */
1678 for (i = dma_dom->aperture[index]->offset;
1679 i < dma_dom->aperture_size;
1680 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001681 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001682 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1683 continue;
1684
Joerg Roedelfcd08612011-10-11 17:41:32 +02001685 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001686 }
1687
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001688 update_domain(&dma_dom->domain);
1689
Joerg Roedel9cabe892009-05-18 16:38:55 +02001690 return 0;
1691
1692out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001693 update_domain(&dma_dom->domain);
1694
Joerg Roedel9cabe892009-05-18 16:38:55 +02001695 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1696
1697 kfree(dma_dom->aperture[index]);
1698 dma_dom->aperture[index] = NULL;
1699
1700 return -ENOMEM;
1701}
1702
Joerg Roedel384de722009-05-15 12:30:05 +02001703static unsigned long dma_ops_area_alloc(struct device *dev,
1704 struct dma_ops_domain *dom,
1705 unsigned int pages,
1706 unsigned long align_mask,
1707 u64 dma_mask,
1708 unsigned long start)
1709{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001710 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001711 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1712 int i = start >> APERTURE_RANGE_SHIFT;
1713 unsigned long boundary_size;
1714 unsigned long address = -1;
1715 unsigned long limit;
1716
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001717 next_bit >>= PAGE_SHIFT;
1718
Joerg Roedel384de722009-05-15 12:30:05 +02001719 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1720 PAGE_SIZE) >> PAGE_SHIFT;
1721
1722 for (;i < max_index; ++i) {
1723 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1724
1725 if (dom->aperture[i]->offset >= dma_mask)
1726 break;
1727
1728 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1729 dma_mask >> PAGE_SHIFT);
1730
1731 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1732 limit, next_bit, pages, 0,
1733 boundary_size, align_mask);
1734 if (address != -1) {
1735 address = dom->aperture[i]->offset +
1736 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001737 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001738 break;
1739 }
1740
1741 next_bit = 0;
1742 }
1743
1744 return address;
1745}
1746
Joerg Roedeld3086442008-06-26 21:27:57 +02001747static unsigned long dma_ops_alloc_addresses(struct device *dev,
1748 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001749 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001750 unsigned long align_mask,
1751 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001752{
Joerg Roedeld3086442008-06-26 21:27:57 +02001753 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001754
Joerg Roedelfe16f082009-05-22 12:27:53 +02001755#ifdef CONFIG_IOMMU_STRESS
1756 dom->next_address = 0;
1757 dom->need_flush = true;
1758#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001759
Joerg Roedel384de722009-05-15 12:30:05 +02001760 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001761 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001762
Joerg Roedel1c655772008-09-04 18:40:05 +02001763 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001764 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001765 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1766 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001767 dom->need_flush = true;
1768 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001769
Joerg Roedel384de722009-05-15 12:30:05 +02001770 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001771 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001772
1773 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1774
1775 return address;
1776}
1777
Joerg Roedel431b2a22008-07-11 17:14:22 +02001778/*
1779 * The address free function.
1780 *
1781 * called with domain->lock held
1782 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001783static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1784 unsigned long address,
1785 unsigned int pages)
1786{
Joerg Roedel384de722009-05-15 12:30:05 +02001787 unsigned i = address >> APERTURE_RANGE_SHIFT;
1788 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001789
Joerg Roedel384de722009-05-15 12:30:05 +02001790 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1791
Joerg Roedel47bccd62009-05-22 12:40:54 +02001792#ifdef CONFIG_IOMMU_STRESS
1793 if (i < 4)
1794 return;
1795#endif
1796
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001797 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001798 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001799
1800 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001801
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001802 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001803
Joerg Roedeld3086442008-06-26 21:27:57 +02001804}
1805
Joerg Roedel431b2a22008-07-11 17:14:22 +02001806/****************************************************************************
1807 *
1808 * The next functions belong to the domain allocation. A domain is
1809 * allocated for every IOMMU as the default domain. If device isolation
1810 * is enabled, every device get its own domain. The most important thing
1811 * about domains is the page table mapping the DMA address space they
1812 * contain.
1813 *
1814 ****************************************************************************/
1815
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001816/*
1817 * This function adds a protection domain to the global protection domain list
1818 */
1819static void add_domain_to_list(struct protection_domain *domain)
1820{
1821 unsigned long flags;
1822
1823 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1824 list_add(&domain->list, &amd_iommu_pd_list);
1825 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1826}
1827
1828/*
1829 * This function removes a protection domain to the global
1830 * protection domain list
1831 */
1832static void del_domain_from_list(struct protection_domain *domain)
1833{
1834 unsigned long flags;
1835
1836 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1837 list_del(&domain->list);
1838 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1839}
1840
Joerg Roedelec487d12008-06-26 21:27:58 +02001841static u16 domain_id_alloc(void)
1842{
1843 unsigned long flags;
1844 int id;
1845
1846 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1847 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1848 BUG_ON(id == 0);
1849 if (id > 0 && id < MAX_DOMAIN_ID)
1850 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1851 else
1852 id = 0;
1853 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1854
1855 return id;
1856}
1857
Joerg Roedela2acfb72008-12-02 18:28:53 +01001858static void domain_id_free(int id)
1859{
1860 unsigned long flags;
1861
1862 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1863 if (id > 0 && id < MAX_DOMAIN_ID)
1864 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1865 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1866}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001867
Joerg Roedel5c34c402013-06-20 20:22:58 +02001868#define DEFINE_FREE_PT_FN(LVL, FN) \
1869static void free_pt_##LVL (unsigned long __pt) \
1870{ \
1871 unsigned long p; \
1872 u64 *pt; \
1873 int i; \
1874 \
1875 pt = (u64 *)__pt; \
1876 \
1877 for (i = 0; i < 512; ++i) { \
1878 if (!IOMMU_PTE_PRESENT(pt[i])) \
1879 continue; \
1880 \
1881 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1882 FN(p); \
1883 } \
1884 free_page((unsigned long)pt); \
1885}
1886
1887DEFINE_FREE_PT_FN(l2, free_page)
1888DEFINE_FREE_PT_FN(l3, free_pt_l2)
1889DEFINE_FREE_PT_FN(l4, free_pt_l3)
1890DEFINE_FREE_PT_FN(l5, free_pt_l4)
1891DEFINE_FREE_PT_FN(l6, free_pt_l5)
1892
Joerg Roedel86db2e52008-12-02 18:20:21 +01001893static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001894{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001895 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001896
Joerg Roedel5c34c402013-06-20 20:22:58 +02001897 switch (domain->mode) {
1898 case PAGE_MODE_NONE:
1899 break;
1900 case PAGE_MODE_1_LEVEL:
1901 free_page(root);
1902 break;
1903 case PAGE_MODE_2_LEVEL:
1904 free_pt_l2(root);
1905 break;
1906 case PAGE_MODE_3_LEVEL:
1907 free_pt_l3(root);
1908 break;
1909 case PAGE_MODE_4_LEVEL:
1910 free_pt_l4(root);
1911 break;
1912 case PAGE_MODE_5_LEVEL:
1913 free_pt_l5(root);
1914 break;
1915 case PAGE_MODE_6_LEVEL:
1916 free_pt_l6(root);
1917 break;
1918 default:
1919 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001920 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001921}
1922
Joerg Roedelb16137b2011-11-21 16:50:23 +01001923static void free_gcr3_tbl_level1(u64 *tbl)
1924{
1925 u64 *ptr;
1926 int i;
1927
1928 for (i = 0; i < 512; ++i) {
1929 if (!(tbl[i] & GCR3_VALID))
1930 continue;
1931
1932 ptr = __va(tbl[i] & PAGE_MASK);
1933
1934 free_page((unsigned long)ptr);
1935 }
1936}
1937
1938static void free_gcr3_tbl_level2(u64 *tbl)
1939{
1940 u64 *ptr;
1941 int i;
1942
1943 for (i = 0; i < 512; ++i) {
1944 if (!(tbl[i] & GCR3_VALID))
1945 continue;
1946
1947 ptr = __va(tbl[i] & PAGE_MASK);
1948
1949 free_gcr3_tbl_level1(ptr);
1950 }
1951}
1952
Joerg Roedel52815b72011-11-17 17:24:28 +01001953static void free_gcr3_table(struct protection_domain *domain)
1954{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001955 if (domain->glx == 2)
1956 free_gcr3_tbl_level2(domain->gcr3_tbl);
1957 else if (domain->glx == 1)
1958 free_gcr3_tbl_level1(domain->gcr3_tbl);
1959 else if (domain->glx != 0)
1960 BUG();
1961
Joerg Roedel52815b72011-11-17 17:24:28 +01001962 free_page((unsigned long)domain->gcr3_tbl);
1963}
1964
Joerg Roedel431b2a22008-07-11 17:14:22 +02001965/*
1966 * Free a domain, only used if something went wrong in the
1967 * allocation path and we need to free an already allocated page table
1968 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001969static void dma_ops_domain_free(struct dma_ops_domain *dom)
1970{
Joerg Roedel384de722009-05-15 12:30:05 +02001971 int i;
1972
Joerg Roedelec487d12008-06-26 21:27:58 +02001973 if (!dom)
1974 return;
1975
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001976 del_domain_from_list(&dom->domain);
1977
Joerg Roedel86db2e52008-12-02 18:20:21 +01001978 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001979
Joerg Roedel384de722009-05-15 12:30:05 +02001980 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1981 if (!dom->aperture[i])
1982 continue;
1983 free_page((unsigned long)dom->aperture[i]->bitmap);
1984 kfree(dom->aperture[i]);
1985 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001986
1987 kfree(dom);
1988}
1989
Joerg Roedel431b2a22008-07-11 17:14:22 +02001990/*
1991 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001992 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001993 * structures required for the dma_ops interface
1994 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001995static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001996{
1997 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001998
1999 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
2000 if (!dma_dom)
2001 return NULL;
2002
2003 spin_lock_init(&dma_dom->domain.lock);
2004
2005 dma_dom->domain.id = domain_id_alloc();
2006 if (dma_dom->domain.id == 0)
2007 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002008 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002009 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002010 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002011 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002012 dma_dom->domain.priv = dma_dom;
2013 if (!dma_dom->domain.pt_root)
2014 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002015
Joerg Roedel1c655772008-09-04 18:40:05 +02002016 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002017 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002018
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002019 add_domain_to_list(&dma_dom->domain);
2020
Joerg Roedel576175c2009-11-23 19:08:46 +01002021 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002022 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002023
Joerg Roedel431b2a22008-07-11 17:14:22 +02002024 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002025 * mark the first page as allocated so we never return 0 as
2026 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002027 */
Joerg Roedel384de722009-05-15 12:30:05 +02002028 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02002029 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002030
Joerg Roedelec487d12008-06-26 21:27:58 +02002031
2032 return dma_dom;
2033
2034free_dma_dom:
2035 dma_ops_domain_free(dma_dom);
2036
2037 return NULL;
2038}
2039
Joerg Roedel431b2a22008-07-11 17:14:22 +02002040/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002041 * little helper function to check whether a given protection domain is a
2042 * dma_ops domain
2043 */
2044static bool dma_ops_domain(struct protection_domain *domain)
2045{
2046 return domain->flags & PD_DMA_OPS_MASK;
2047}
2048
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002049static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002050{
Joerg Roedel132bd682011-11-17 14:18:46 +01002051 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002052 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002053
Joerg Roedel132bd682011-11-17 14:18:46 +01002054 if (domain->mode != PAGE_MODE_NONE)
2055 pte_root = virt_to_phys(domain->pt_root);
2056
Joerg Roedel38ddf412008-09-11 10:38:32 +02002057 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2058 << DEV_ENTRY_MODE_SHIFT;
2059 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002060
Joerg Roedelee6c2862011-11-09 12:06:03 +01002061 flags = amd_iommu_dev_table[devid].data[1];
2062
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002063 if (ats)
2064 flags |= DTE_FLAG_IOTLB;
2065
Joerg Roedel52815b72011-11-17 17:24:28 +01002066 if (domain->flags & PD_IOMMUV2_MASK) {
2067 u64 gcr3 = __pa(domain->gcr3_tbl);
2068 u64 glx = domain->glx;
2069 u64 tmp;
2070
2071 pte_root |= DTE_FLAG_GV;
2072 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2073
2074 /* First mask out possible old values for GCR3 table */
2075 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2076 flags &= ~tmp;
2077
2078 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2079 flags &= ~tmp;
2080
2081 /* Encode GCR3 table into DTE */
2082 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2083 pte_root |= tmp;
2084
2085 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2086 flags |= tmp;
2087
2088 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2089 flags |= tmp;
2090 }
2091
Joerg Roedelee6c2862011-11-09 12:06:03 +01002092 flags &= ~(0xffffUL);
2093 flags |= domain->id;
2094
2095 amd_iommu_dev_table[devid].data[1] = flags;
2096 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002097}
2098
Joerg Roedel15898bb2009-11-24 15:39:42 +01002099static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002100{
Joerg Roedel355bf552008-12-08 12:02:41 +01002101 /* remove entry from the device table seen by the hardware */
2102 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2103 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002104
Joerg Roedelc5cca142009-10-09 18:31:20 +02002105 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002106}
2107
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002108static void do_attach(struct iommu_dev_data *dev_data,
2109 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002110{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002111 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002112 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002113
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002114 iommu = amd_iommu_rlookup_table[dev_data->devid];
2115 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002116
2117 /* Update data structures */
2118 dev_data->domain = domain;
2119 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002120 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002121
2122 /* Do reference counting */
2123 domain->dev_iommu[iommu->index] += 1;
2124 domain->dev_cnt += 1;
2125
2126 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002127 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002128}
2129
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002130static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002131{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002132 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002133
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002134 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002135
Joerg Roedelc4596112009-11-20 14:57:32 +01002136 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002137 dev_data->domain->dev_iommu[iommu->index] -= 1;
2138 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002139
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002140 /* Update data structures */
2141 dev_data->domain = NULL;
2142 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002143 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002144
2145 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002146 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002147}
2148
2149/*
2150 * If a device is not yet associated with a domain, this function does
2151 * assigns it visible for the hardware
2152 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002153static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002154 struct protection_domain *domain)
2155{
Joerg Roedel397111a2014-08-05 17:31:51 +02002156 struct iommu_dev_data *head, *entry;
Julia Lawall84fe6c12010-05-27 12:31:51 +02002157 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002158
Joerg Roedel15898bb2009-11-24 15:39:42 +01002159 /* lock domain */
2160 spin_lock(&domain->lock);
2161
Joerg Roedel397111a2014-08-05 17:31:51 +02002162 head = dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002163
Joerg Roedel397111a2014-08-05 17:31:51 +02002164 if (head->alias_data != NULL)
2165 head = head->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002166
Joerg Roedel397111a2014-08-05 17:31:51 +02002167 /* Now we have the root of the alias group, if any */
Joerg Roedel2b02b092011-06-09 17:48:39 +02002168
Joerg Roedel397111a2014-08-05 17:31:51 +02002169 ret = -EBUSY;
2170 if (head->domain != NULL)
2171 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01002172
Joerg Roedel397111a2014-08-05 17:31:51 +02002173 /* Attach alias group root */
2174 do_attach(head, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002175
Joerg Roedel397111a2014-08-05 17:31:51 +02002176 /* Attach other devices in the alias group */
2177 list_for_each_entry(entry, &head->alias_list, alias_list)
2178 do_attach(entry, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002179
Julia Lawall84fe6c12010-05-27 12:31:51 +02002180 ret = 0;
2181
2182out_unlock:
2183
Joerg Roedel355bf552008-12-08 12:02:41 +01002184 /* ready */
2185 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002186
Julia Lawall84fe6c12010-05-27 12:31:51 +02002187 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002188}
2189
Joerg Roedel52815b72011-11-17 17:24:28 +01002190
2191static void pdev_iommuv2_disable(struct pci_dev *pdev)
2192{
2193 pci_disable_ats(pdev);
2194 pci_disable_pri(pdev);
2195 pci_disable_pasid(pdev);
2196}
2197
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002198/* FIXME: Change generic reset-function to do the same */
2199static int pri_reset_while_enabled(struct pci_dev *pdev)
2200{
2201 u16 control;
2202 int pos;
2203
Joerg Roedel46277b72011-12-07 14:34:02 +01002204 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002205 if (!pos)
2206 return -EINVAL;
2207
Joerg Roedel46277b72011-12-07 14:34:02 +01002208 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2209 control |= PCI_PRI_CTRL_RESET;
2210 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002211
2212 return 0;
2213}
2214
Joerg Roedel52815b72011-11-17 17:24:28 +01002215static int pdev_iommuv2_enable(struct pci_dev *pdev)
2216{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002217 bool reset_enable;
2218 int reqs, ret;
2219
2220 /* FIXME: Hardcode number of outstanding requests for now */
2221 reqs = 32;
2222 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2223 reqs = 1;
2224 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002225
2226 /* Only allow access to user-accessible pages */
2227 ret = pci_enable_pasid(pdev, 0);
2228 if (ret)
2229 goto out_err;
2230
2231 /* First reset the PRI state of the device */
2232 ret = pci_reset_pri(pdev);
2233 if (ret)
2234 goto out_err;
2235
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002236 /* Enable PRI */
2237 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002238 if (ret)
2239 goto out_err;
2240
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002241 if (reset_enable) {
2242 ret = pri_reset_while_enabled(pdev);
2243 if (ret)
2244 goto out_err;
2245 }
2246
Joerg Roedel52815b72011-11-17 17:24:28 +01002247 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2248 if (ret)
2249 goto out_err;
2250
2251 return 0;
2252
2253out_err:
2254 pci_disable_pri(pdev);
2255 pci_disable_pasid(pdev);
2256
2257 return ret;
2258}
2259
Joerg Roedelc99afa22011-11-21 18:19:25 +01002260/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002261#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002262
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002263static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002264{
Joerg Roedela3b93122012-04-12 12:49:26 +02002265 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002266 int pos;
2267
Joerg Roedel46277b72011-12-07 14:34:02 +01002268 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002269 if (!pos)
2270 return false;
2271
Joerg Roedela3b93122012-04-12 12:49:26 +02002272 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002273
Joerg Roedela3b93122012-04-12 12:49:26 +02002274 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002275}
2276
Joerg Roedel15898bb2009-11-24 15:39:42 +01002277/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002278 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002279 * assigns it visible for the hardware
2280 */
2281static int attach_device(struct device *dev,
2282 struct protection_domain *domain)
2283{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002284 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002285 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002286 unsigned long flags;
2287 int ret;
2288
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002289 dev_data = get_dev_data(dev);
2290
Joerg Roedel52815b72011-11-17 17:24:28 +01002291 if (domain->flags & PD_IOMMUV2_MASK) {
2292 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2293 return -EINVAL;
2294
2295 if (pdev_iommuv2_enable(pdev) != 0)
2296 return -EINVAL;
2297
2298 dev_data->ats.enabled = true;
2299 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002300 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002301 } else if (amd_iommu_iotlb_sup &&
2302 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002303 dev_data->ats.enabled = true;
2304 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2305 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002306
Joerg Roedel15898bb2009-11-24 15:39:42 +01002307 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002308 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002309 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2310
2311 /*
2312 * We might boot into a crash-kernel here. The crashed kernel
2313 * left the caches in the IOMMU dirty. So we have to flush
2314 * here to evict all dirty stuff.
2315 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002316 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002317
2318 return ret;
2319}
2320
2321/*
2322 * Removes a device from a protection domain (unlocked)
2323 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002324static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002325{
Joerg Roedel397111a2014-08-05 17:31:51 +02002326 struct iommu_dev_data *head, *entry;
Joerg Roedel2ca76272010-01-22 16:45:31 +01002327 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002328 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002329
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002330 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002331
Joerg Roedel2ca76272010-01-22 16:45:31 +01002332 domain = dev_data->domain;
2333
2334 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002335
Joerg Roedel397111a2014-08-05 17:31:51 +02002336 head = dev_data;
2337 if (head->alias_data != NULL)
2338 head = head->alias_data;
Joerg Roedel71f77582011-06-09 19:03:15 +02002339
Joerg Roedel397111a2014-08-05 17:31:51 +02002340 list_for_each_entry(entry, &head->alias_list, alias_list)
2341 do_detach(entry);
Joerg Roedel24100052009-11-25 15:59:57 +01002342
Joerg Roedel397111a2014-08-05 17:31:51 +02002343 do_detach(head);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002344
Joerg Roedel2ca76272010-01-22 16:45:31 +01002345 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002346
Joerg Roedel21129f72009-09-01 11:59:42 +02002347 /*
2348 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002349 * passthrough domain if it is detached from any other domain.
2350 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002351 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002352 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002353 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002354 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002355}
2356
2357/*
2358 * Removes a device from a protection domain (with devtable_lock held)
2359 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002360static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002361{
Joerg Roedel52815b72011-11-17 17:24:28 +01002362 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002363 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002364 unsigned long flags;
2365
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002366 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002367 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002368
Joerg Roedel355bf552008-12-08 12:02:41 +01002369 /* lock device table */
2370 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002371 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002372 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002373
Joerg Roedel52815b72011-11-17 17:24:28 +01002374 if (domain->flags & PD_IOMMUV2_MASK)
2375 pdev_iommuv2_disable(to_pci_dev(dev));
2376 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002377 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002378
2379 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002380}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002381
Joerg Roedel15898bb2009-11-24 15:39:42 +01002382/*
2383 * Find out the protection domain structure for a given PCI device. This
2384 * will give us the pointer to the page table root for example.
2385 */
2386static struct protection_domain *domain_for_device(struct device *dev)
2387{
Joerg Roedel71f77582011-06-09 19:03:15 +02002388 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002389 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002390 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002391
Joerg Roedel657cbb62009-11-23 15:26:46 +01002392 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002393
Joerg Roedel2b02b092011-06-09 17:48:39 +02002394 if (dev_data->domain)
2395 return dev_data->domain;
2396
Joerg Roedel71f77582011-06-09 19:03:15 +02002397 if (dev_data->alias_data != NULL) {
2398 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002399
2400 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2401 if (alias_data->domain != NULL) {
2402 __attach_device(dev_data, alias_data->domain);
2403 dom = alias_data->domain;
2404 }
2405 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002406 }
2407
Joerg Roedel15898bb2009-11-24 15:39:42 +01002408 return dom;
2409}
2410
Joerg Roedele275a2a2008-12-10 18:27:25 +01002411static int device_change_notifier(struct notifier_block *nb,
2412 unsigned long action, void *data)
2413{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002414 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002415 struct protection_domain *domain;
2416 struct iommu_dev_data *dev_data;
2417 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002418 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002419 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002420 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002421
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002422 if (!check_device(dev))
2423 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002424
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002425 devid = get_device_id(dev);
2426 iommu = amd_iommu_rlookup_table[devid];
2427 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002428
2429 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002430 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002431
2432 domain = domain_for_device(dev);
2433
Joerg Roedele275a2a2008-12-10 18:27:25 +01002434 if (!domain)
2435 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002436 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002437 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002438 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002439 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002440 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002441
2442 iommu_init_device(dev);
Alex Williamson25b11ce2014-09-19 10:03:13 -06002443 init_iommu_group(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002444
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002445 /*
2446 * dev_data is still NULL and
2447 * got initialized in iommu_init_device
2448 */
2449 dev_data = get_dev_data(dev);
2450
2451 if (iommu_pass_through || dev_data->iommu_v2) {
2452 dev_data->passthrough = true;
2453 attach_device(dev, pt_domain);
2454 break;
2455 }
2456
Joerg Roedel657cbb62009-11-23 15:26:46 +01002457 domain = domain_for_device(dev);
2458
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002459 /* allocate a protection domain if a device is added */
2460 dma_domain = find_protection_domain(devid);
Joerg Roedelc2a28762013-03-26 22:48:23 +01002461 if (!dma_domain) {
2462 dma_domain = dma_ops_domain_alloc();
2463 if (!dma_domain)
2464 goto out;
2465 dma_domain->target_dev = devid;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002466
Joerg Roedelc2a28762013-03-26 22:48:23 +01002467 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2468 list_add_tail(&dma_domain->list, &iommu_pd_list);
2469 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2470 }
Joerg Roedelac1534a2012-06-21 14:52:40 +02002471
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002472 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002473
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002474 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002475 case BUS_NOTIFY_DEL_DEVICE:
2476
2477 iommu_uninit_device(dev);
2478
Joerg Roedele275a2a2008-12-10 18:27:25 +01002479 default:
2480 goto out;
2481 }
2482
Joerg Roedele275a2a2008-12-10 18:27:25 +01002483 iommu_completion_wait(iommu);
2484
2485out:
2486 return 0;
2487}
2488
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302489static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002490 .notifier_call = device_change_notifier,
2491};
Joerg Roedel355bf552008-12-08 12:02:41 +01002492
Joerg Roedel8638c492009-12-10 11:12:25 +01002493void amd_iommu_init_notifier(void)
2494{
2495 bus_register_notifier(&pci_bus_type, &device_nb);
2496}
2497
Joerg Roedel431b2a22008-07-11 17:14:22 +02002498/*****************************************************************************
2499 *
2500 * The next functions belong to the dma_ops mapping/unmapping code.
2501 *
2502 *****************************************************************************/
2503
2504/*
2505 * In the dma_ops path we only have the struct device. This function
2506 * finds the corresponding IOMMU, the protection domain and the
2507 * requestor id for a given device.
2508 * If the device is not yet associated with a domain this is also done
2509 * in this function.
2510 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002511static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002512{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002513 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002514 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002515 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002516
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002517 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002518 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002519
Joerg Roedel94f6d192009-11-24 16:40:02 +01002520 domain = domain_for_device(dev);
2521 if (domain != NULL && !dma_ops_domain(domain))
2522 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002523
Joerg Roedel94f6d192009-11-24 16:40:02 +01002524 if (domain != NULL)
2525 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002526
Frank Arnolddf805ab2012-08-27 19:21:04 +02002527 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002528 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002529 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002530 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2531 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002532 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002533 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002534
Joerg Roedel94f6d192009-11-24 16:40:02 +01002535 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002536}
2537
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002538static void update_device_table(struct protection_domain *domain)
2539{
Joerg Roedel492667d2009-11-27 13:25:47 +01002540 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002541
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002542 list_for_each_entry(dev_data, &domain->dev_list, list)
2543 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002544}
2545
2546static void update_domain(struct protection_domain *domain)
2547{
2548 if (!domain->updated)
2549 return;
2550
2551 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002552
2553 domain_flush_devices(domain);
2554 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002555
2556 domain->updated = false;
2557}
2558
Joerg Roedel431b2a22008-07-11 17:14:22 +02002559/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002560 * This function fetches the PTE for a given address in the aperture
2561 */
2562static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2563 unsigned long address)
2564{
Joerg Roedel384de722009-05-15 12:30:05 +02002565 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002566 u64 *pte, *pte_page;
2567
Joerg Roedel384de722009-05-15 12:30:05 +02002568 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2569 if (!aperture)
2570 return NULL;
2571
2572 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002573 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002574 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002575 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002576 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2577 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002578 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002579
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002580 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002581
2582 return pte;
2583}
2584
2585/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002586 * This is the generic map function. It maps one 4kb page at paddr to
2587 * the given address in the DMA address space for the domain.
2588 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002589static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002590 unsigned long address,
2591 phys_addr_t paddr,
2592 int direction)
2593{
2594 u64 *pte, __pte;
2595
2596 WARN_ON(address > dom->aperture_size);
2597
2598 paddr &= PAGE_MASK;
2599
Joerg Roedel8bda3092009-05-12 12:02:46 +02002600 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002601 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002602 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002603
2604 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2605
2606 if (direction == DMA_TO_DEVICE)
2607 __pte |= IOMMU_PTE_IR;
2608 else if (direction == DMA_FROM_DEVICE)
2609 __pte |= IOMMU_PTE_IW;
2610 else if (direction == DMA_BIDIRECTIONAL)
2611 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2612
2613 WARN_ON(*pte);
2614
2615 *pte = __pte;
2616
2617 return (dma_addr_t)address;
2618}
2619
Joerg Roedel431b2a22008-07-11 17:14:22 +02002620/*
2621 * The generic unmapping function for on page in the DMA address space.
2622 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002623static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002624 unsigned long address)
2625{
Joerg Roedel384de722009-05-15 12:30:05 +02002626 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002627 u64 *pte;
2628
2629 if (address >= dom->aperture_size)
2630 return;
2631
Joerg Roedel384de722009-05-15 12:30:05 +02002632 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2633 if (!aperture)
2634 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002635
Joerg Roedel384de722009-05-15 12:30:05 +02002636 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2637 if (!pte)
2638 return;
2639
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002640 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002641
2642 WARN_ON(!*pte);
2643
2644 *pte = 0ULL;
2645}
2646
Joerg Roedel431b2a22008-07-11 17:14:22 +02002647/*
2648 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002649 * contiguous memory region into DMA address space. It is used by all
2650 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002651 * Must be called with the domain lock held.
2652 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002653static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002654 struct dma_ops_domain *dma_dom,
2655 phys_addr_t paddr,
2656 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002657 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002658 bool align,
2659 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002660{
2661 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002662 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002663 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002664 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002665 int i;
2666
Joerg Roedele3c449f2008-10-15 22:02:11 -07002667 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002668 paddr &= PAGE_MASK;
2669
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002670 INC_STATS_COUNTER(total_map_requests);
2671
Joerg Roedelc1858972008-12-12 15:42:39 +01002672 if (pages > 1)
2673 INC_STATS_COUNTER(cross_page);
2674
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002675 if (align)
2676 align_mask = (1UL << get_order(size)) - 1;
2677
Joerg Roedel11b83882009-05-19 10:23:15 +02002678retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002679 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2680 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002681 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002682 /*
2683 * setting next_address here will let the address
2684 * allocator only scan the new allocated range in the
2685 * first run. This is a small optimization.
2686 */
2687 dma_dom->next_address = dma_dom->aperture_size;
2688
Joerg Roedel576175c2009-11-23 19:08:46 +01002689 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002690 goto out;
2691
2692 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002693 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002694 * allocation again
2695 */
2696 goto retry;
2697 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002698
2699 start = address;
2700 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002701 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002702 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002703 goto out_unmap;
2704
Joerg Roedelcb76c322008-06-26 21:28:00 +02002705 paddr += PAGE_SIZE;
2706 start += PAGE_SIZE;
2707 }
2708 address += offset;
2709
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002710 ADD_STATS_COUNTER(alloced_io_mem, size);
2711
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002712 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002713 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002714 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002715 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002716 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002717
Joerg Roedelcb76c322008-06-26 21:28:00 +02002718out:
2719 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002720
2721out_unmap:
2722
2723 for (--i; i >= 0; --i) {
2724 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002725 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002726 }
2727
2728 dma_ops_free_addresses(dma_dom, address, pages);
2729
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002730 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002731}
2732
Joerg Roedel431b2a22008-07-11 17:14:22 +02002733/*
2734 * Does the reverse of the __map_single function. Must be called with
2735 * the domain lock held too
2736 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002737static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002738 dma_addr_t dma_addr,
2739 size_t size,
2740 int dir)
2741{
Joerg Roedel04e04632010-09-23 16:12:48 +02002742 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002743 dma_addr_t i, start;
2744 unsigned int pages;
2745
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002746 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002747 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002748 return;
2749
Joerg Roedel04e04632010-09-23 16:12:48 +02002750 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002751 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002752 dma_addr &= PAGE_MASK;
2753 start = dma_addr;
2754
2755 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002756 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002757 start += PAGE_SIZE;
2758 }
2759
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002760 SUB_STATS_COUNTER(alloced_io_mem, size);
2761
Joerg Roedelcb76c322008-06-26 21:28:00 +02002762 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002763
Joerg Roedel80be3082008-11-06 14:59:05 +01002764 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002765 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002766 dma_dom->need_flush = false;
2767 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002768}
2769
Joerg Roedel431b2a22008-07-11 17:14:22 +02002770/*
2771 * The exported map_single function for dma_ops.
2772 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002773static dma_addr_t map_page(struct device *dev, struct page *page,
2774 unsigned long offset, size_t size,
2775 enum dma_data_direction dir,
2776 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002777{
2778 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002779 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002780 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002781 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002782 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002783
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002784 INC_STATS_COUNTER(cnt_map_single);
2785
Joerg Roedel94f6d192009-11-24 16:40:02 +01002786 domain = get_domain(dev);
2787 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002788 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002789 else if (IS_ERR(domain))
2790 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002791
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002792 dma_mask = *dev->dma_mask;
2793
Joerg Roedel4da70b92008-06-26 21:28:01 +02002794 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002795
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002796 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002797 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002798 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002799 goto out;
2800
Joerg Roedel17b124b2011-04-06 18:01:35 +02002801 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002802
2803out:
2804 spin_unlock_irqrestore(&domain->lock, flags);
2805
2806 return addr;
2807}
2808
Joerg Roedel431b2a22008-07-11 17:14:22 +02002809/*
2810 * The exported unmap_single function for dma_ops.
2811 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002812static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2813 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002814{
2815 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002816 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002817
Joerg Roedel146a6912008-12-12 15:07:12 +01002818 INC_STATS_COUNTER(cnt_unmap_single);
2819
Joerg Roedel94f6d192009-11-24 16:40:02 +01002820 domain = get_domain(dev);
2821 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002822 return;
2823
Joerg Roedel4da70b92008-06-26 21:28:01 +02002824 spin_lock_irqsave(&domain->lock, flags);
2825
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002826 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002827
Joerg Roedel17b124b2011-04-06 18:01:35 +02002828 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002829
2830 spin_unlock_irqrestore(&domain->lock, flags);
2831}
2832
Joerg Roedel431b2a22008-07-11 17:14:22 +02002833/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002834 * The exported map_sg function for dma_ops (handles scatter-gather
2835 * lists).
2836 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002837static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002838 int nelems, enum dma_data_direction dir,
2839 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002840{
2841 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002842 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002843 int i;
2844 struct scatterlist *s;
2845 phys_addr_t paddr;
2846 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002847 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002848
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002849 INC_STATS_COUNTER(cnt_map_sg);
2850
Joerg Roedel94f6d192009-11-24 16:40:02 +01002851 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002852 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002853 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002854
Joerg Roedel832a90c2008-09-18 15:54:23 +02002855 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002856
Joerg Roedel65b050a2008-06-26 21:28:02 +02002857 spin_lock_irqsave(&domain->lock, flags);
2858
2859 for_each_sg(sglist, s, nelems, i) {
2860 paddr = sg_phys(s);
2861
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002862 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002863 paddr, s->length, dir, false,
2864 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002865
2866 if (s->dma_address) {
2867 s->dma_length = s->length;
2868 mapped_elems++;
2869 } else
2870 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002871 }
2872
Joerg Roedel17b124b2011-04-06 18:01:35 +02002873 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002874
2875out:
2876 spin_unlock_irqrestore(&domain->lock, flags);
2877
2878 return mapped_elems;
2879unmap:
2880 for_each_sg(sglist, s, mapped_elems, i) {
2881 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002882 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002883 s->dma_length, dir);
2884 s->dma_address = s->dma_length = 0;
2885 }
2886
2887 mapped_elems = 0;
2888
2889 goto out;
2890}
2891
Joerg Roedel431b2a22008-07-11 17:14:22 +02002892/*
2893 * The exported map_sg function for dma_ops (handles scatter-gather
2894 * lists).
2895 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002896static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002897 int nelems, enum dma_data_direction dir,
2898 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002899{
2900 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002901 struct protection_domain *domain;
2902 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002903 int i;
2904
Joerg Roedel55877a62008-12-12 15:12:14 +01002905 INC_STATS_COUNTER(cnt_unmap_sg);
2906
Joerg Roedel94f6d192009-11-24 16:40:02 +01002907 domain = get_domain(dev);
2908 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002909 return;
2910
Joerg Roedel65b050a2008-06-26 21:28:02 +02002911 spin_lock_irqsave(&domain->lock, flags);
2912
2913 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002914 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002915 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002916 s->dma_address = s->dma_length = 0;
2917 }
2918
Joerg Roedel17b124b2011-04-06 18:01:35 +02002919 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002920
2921 spin_unlock_irqrestore(&domain->lock, flags);
2922}
2923
Joerg Roedel431b2a22008-07-11 17:14:22 +02002924/*
2925 * The exported alloc_coherent function for dma_ops.
2926 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002927static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002928 dma_addr_t *dma_addr, gfp_t flag,
2929 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002930{
2931 unsigned long flags;
2932 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002933 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002934 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002935 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002936
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002937 INC_STATS_COUNTER(cnt_alloc_coherent);
2938
Joerg Roedel94f6d192009-11-24 16:40:02 +01002939 domain = get_domain(dev);
2940 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002941 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2942 *dma_addr = __pa(virt_addr);
2943 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002944 } else if (IS_ERR(domain))
2945 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002946
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002947 dma_mask = dev->coherent_dma_mask;
2948 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2949 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002950
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002951 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2952 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302953 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002954
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002955 paddr = virt_to_phys(virt_addr);
2956
Joerg Roedel832a90c2008-09-18 15:54:23 +02002957 if (!dma_mask)
2958 dma_mask = *dev->dma_mask;
2959
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002960 spin_lock_irqsave(&domain->lock, flags);
2961
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002962 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002963 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002964
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002965 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002966 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002967 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002968 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002969
Joerg Roedel17b124b2011-04-06 18:01:35 +02002970 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002971
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002972 spin_unlock_irqrestore(&domain->lock, flags);
2973
2974 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002975
2976out_free:
2977
2978 free_pages((unsigned long)virt_addr, get_order(size));
2979
2980 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002981}
2982
Joerg Roedel431b2a22008-07-11 17:14:22 +02002983/*
2984 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002985 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002986static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002987 void *virt_addr, dma_addr_t dma_addr,
2988 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002989{
2990 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002991 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002992
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002993 INC_STATS_COUNTER(cnt_free_coherent);
2994
Joerg Roedel94f6d192009-11-24 16:40:02 +01002995 domain = get_domain(dev);
2996 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002997 goto free_mem;
2998
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002999 spin_lock_irqsave(&domain->lock, flags);
3000
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003001 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003002
Joerg Roedel17b124b2011-04-06 18:01:35 +02003003 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003004
3005 spin_unlock_irqrestore(&domain->lock, flags);
3006
3007free_mem:
3008 free_pages((unsigned long)virt_addr, get_order(size));
3009}
3010
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003011/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003012 * This function is called by the DMA layer to find out if we can handle a
3013 * particular device. It is part of the dma_ops.
3014 */
3015static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3016{
Joerg Roedel420aef82009-11-23 16:14:57 +01003017 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003018}
3019
3020/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003021 * The function for pre-allocating protection domains.
3022 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003023 * If the driver core informs the DMA layer if a driver grabs a device
3024 * we don't need to preallocate the protection domains anymore.
3025 * For now we have to.
3026 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003027static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003028{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003029 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003030 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003031 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003032 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003033
Chris Wrightd18c69d2010-04-02 18:27:55 -07003034 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003035
3036 /* Do we handle this device? */
3037 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003038 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003039
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003040 dev_data = get_dev_data(&dev->dev);
3041 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3042 /* Make sure passthrough domain is allocated */
3043 alloc_passthrough_domain();
3044 dev_data->passthrough = true;
3045 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003046 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003047 dev_name(&dev->dev));
3048 }
3049
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003050 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003051 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003052 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003053
3054 devid = get_device_id(&dev->dev);
3055
Joerg Roedel87a64d52009-11-24 17:26:43 +01003056 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003057 if (!dma_dom)
3058 continue;
3059 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003060 dma_dom->target_dev = devid;
3061
Joerg Roedel15898bb2009-11-24 15:39:42 +01003062 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003063
Joerg Roedelbd60b732008-09-11 10:24:48 +02003064 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003065 }
3066}
3067
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003068static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003069 .alloc = alloc_coherent,
3070 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003071 .map_page = map_page,
3072 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003073 .map_sg = map_sg,
3074 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003075 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003076};
3077
Joerg Roedel27c21272011-05-30 15:56:24 +02003078static unsigned device_dma_ops_init(void)
3079{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003080 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003081 struct pci_dev *pdev = NULL;
3082 unsigned unhandled = 0;
3083
3084 for_each_pci_dev(pdev) {
3085 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003086
3087 iommu_ignore_device(&pdev->dev);
3088
Joerg Roedel27c21272011-05-30 15:56:24 +02003089 unhandled += 1;
3090 continue;
3091 }
3092
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003093 dev_data = get_dev_data(&pdev->dev);
3094
3095 if (!dev_data->passthrough)
3096 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3097 else
3098 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003099 }
3100
3101 return unhandled;
3102}
3103
Joerg Roedel431b2a22008-07-11 17:14:22 +02003104/*
3105 * The function which clues the AMD IOMMU driver into dma_ops.
3106 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003107
3108void __init amd_iommu_init_api(void)
3109{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003110 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003111}
3112
Joerg Roedel6631ee92008-06-26 21:28:05 +02003113int __init amd_iommu_init_dma_ops(void)
3114{
3115 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003116 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003117
Joerg Roedel431b2a22008-07-11 17:14:22 +02003118 /*
3119 * first allocate a default protection domain for every IOMMU we
3120 * found in the system. Devices not assigned to any other
3121 * protection domain will be assigned to the default one.
3122 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003123 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003124 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003125 if (iommu->default_dom == NULL)
3126 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003127 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003128 ret = iommu_init_unity_mappings(iommu);
3129 if (ret)
3130 goto free_domains;
3131 }
3132
Joerg Roedel431b2a22008-07-11 17:14:22 +02003133 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003134 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003135 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003136 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003137
3138 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003139 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003140
Joerg Roedel431b2a22008-07-11 17:14:22 +02003141 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003142 unhandled = device_dma_ops_init();
3143 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3144 /* There are unhandled devices - initialize swiotlb for them */
3145 swiotlb = 1;
3146 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003147
Joerg Roedel7f265082008-12-12 13:50:21 +01003148 amd_iommu_stats_init();
3149
Joerg Roedel62410ee2012-06-12 16:42:43 +02003150 if (amd_iommu_unmap_flush)
3151 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3152 else
3153 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3154
Joerg Roedel6631ee92008-06-26 21:28:05 +02003155 return 0;
3156
3157free_domains:
3158
Joerg Roedel3bd22172009-05-04 15:06:20 +02003159 for_each_iommu(iommu) {
Cyril Roelandt91457df2013-02-12 05:01:50 +01003160 dma_ops_domain_free(iommu->default_dom);
Joerg Roedel6631ee92008-06-26 21:28:05 +02003161 }
3162
3163 return ret;
3164}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003165
3166/*****************************************************************************
3167 *
3168 * The following functions belong to the exported interface of AMD IOMMU
3169 *
3170 * This interface allows access to lower level functions of the IOMMU
3171 * like protection domain handling and assignement of devices to domains
3172 * which is not possible with the dma_ops interface.
3173 *
3174 *****************************************************************************/
3175
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003176static void cleanup_domain(struct protection_domain *domain)
3177{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02003178 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003179 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003180
3181 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3182
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02003183 while (!list_empty(&domain->dev_list)) {
3184 entry = list_first_entry(&domain->dev_list,
3185 struct iommu_dev_data, list);
3186 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01003187 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003188
3189 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3190}
3191
Joerg Roedel26508152009-08-26 16:52:40 +02003192static void protection_domain_free(struct protection_domain *domain)
3193{
3194 if (!domain)
3195 return;
3196
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003197 del_domain_from_list(domain);
3198
Joerg Roedel26508152009-08-26 16:52:40 +02003199 if (domain->id)
3200 domain_id_free(domain->id);
3201
3202 kfree(domain);
3203}
3204
3205static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003206{
3207 struct protection_domain *domain;
3208
3209 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3210 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003211 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003212
3213 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003214 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003215 domain->id = domain_id_alloc();
3216 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003217 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003218 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003219
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003220 add_domain_to_list(domain);
3221
Joerg Roedel26508152009-08-26 16:52:40 +02003222 return domain;
3223
3224out_err:
3225 kfree(domain);
3226
3227 return NULL;
3228}
3229
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003230static int __init alloc_passthrough_domain(void)
3231{
3232 if (pt_domain != NULL)
3233 return 0;
3234
3235 /* allocate passthrough domain */
3236 pt_domain = protection_domain_alloc();
3237 if (!pt_domain)
3238 return -ENOMEM;
3239
3240 pt_domain->mode = PAGE_MODE_NONE;
3241
3242 return 0;
3243}
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003244
3245static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
3246{
3247 struct protection_domain *pdomain;
3248
3249 /* We only support unmanaged domains for now */
3250 if (type != IOMMU_DOMAIN_UNMANAGED)
3251 return NULL;
3252
3253 pdomain = protection_domain_alloc();
3254 if (!pdomain)
3255 goto out_free;
3256
3257 pdomain->mode = PAGE_MODE_3_LEVEL;
3258 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3259 if (!pdomain->pt_root)
3260 goto out_free;
3261
3262 pdomain->domain.geometry.aperture_start = 0;
3263 pdomain->domain.geometry.aperture_end = ~0ULL;
3264 pdomain->domain.geometry.force_aperture = true;
3265
3266 return &pdomain->domain;
3267
3268out_free:
3269 protection_domain_free(pdomain);
3270
3271 return NULL;
3272}
3273
3274static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02003275{
3276 struct protection_domain *domain;
3277
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003278 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01003279 return;
3280
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003281 domain = to_pdomain(dom);
3282
Joerg Roedel98383fc2008-12-02 18:34:12 +01003283 if (domain->dev_cnt > 0)
3284 cleanup_domain(domain);
3285
3286 BUG_ON(domain->dev_cnt != 0);
3287
Joerg Roedel132bd682011-11-17 14:18:46 +01003288 if (domain->mode != PAGE_MODE_NONE)
3289 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003290
Joerg Roedel52815b72011-11-17 17:24:28 +01003291 if (domain->flags & PD_IOMMUV2_MASK)
3292 free_gcr3_table(domain);
3293
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003294 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003295}
3296
Joerg Roedel684f2882008-12-08 12:07:44 +01003297static void amd_iommu_detach_device(struct iommu_domain *dom,
3298 struct device *dev)
3299{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003300 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003301 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003302 u16 devid;
3303
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003304 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003305 return;
3306
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003307 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003308
Joerg Roedel657cbb62009-11-23 15:26:46 +01003309 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003310 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003311
3312 iommu = amd_iommu_rlookup_table[devid];
3313 if (!iommu)
3314 return;
3315
Joerg Roedel684f2882008-12-08 12:07:44 +01003316 iommu_completion_wait(iommu);
3317}
3318
Joerg Roedel01106062008-12-02 19:34:11 +01003319static int amd_iommu_attach_device(struct iommu_domain *dom,
3320 struct device *dev)
3321{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003322 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003323 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003324 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003325 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003326
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003327 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003328 return -EINVAL;
3329
Joerg Roedel657cbb62009-11-23 15:26:46 +01003330 dev_data = dev->archdata.iommu;
3331
Joerg Roedelf62dda62011-06-09 12:55:35 +02003332 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003333 if (!iommu)
3334 return -EINVAL;
3335
Joerg Roedel657cbb62009-11-23 15:26:46 +01003336 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003337 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003338
Joerg Roedel15898bb2009-11-24 15:39:42 +01003339 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003340
3341 iommu_completion_wait(iommu);
3342
Joerg Roedel15898bb2009-11-24 15:39:42 +01003343 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003344}
3345
Joerg Roedel468e2362010-01-21 16:37:36 +01003346static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003347 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003348{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003349 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003350 int prot = 0;
3351 int ret;
3352
Joerg Roedel132bd682011-11-17 14:18:46 +01003353 if (domain->mode == PAGE_MODE_NONE)
3354 return -EINVAL;
3355
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003356 if (iommu_prot & IOMMU_READ)
3357 prot |= IOMMU_PROT_IR;
3358 if (iommu_prot & IOMMU_WRITE)
3359 prot |= IOMMU_PROT_IW;
3360
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003361 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003362 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003363 mutex_unlock(&domain->api_lock);
3364
Joerg Roedel795e74f72010-05-11 17:40:57 +02003365 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003366}
3367
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003368static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3369 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003370{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003371 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003372 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003373
Joerg Roedel132bd682011-11-17 14:18:46 +01003374 if (domain->mode == PAGE_MODE_NONE)
3375 return -EINVAL;
3376
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003377 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003378 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003379 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003380
Joerg Roedel17b124b2011-04-06 18:01:35 +02003381 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003382
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003383 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003384}
3385
Joerg Roedel645c4c82008-12-02 20:05:50 +01003386static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303387 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003388{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003389 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelf03152b2010-01-21 16:15:24 +01003390 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003391 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003392 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003393
Joerg Roedel132bd682011-11-17 14:18:46 +01003394 if (domain->mode == PAGE_MODE_NONE)
3395 return iova;
3396
Joerg Roedel24cd7722010-01-19 17:27:39 +01003397 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003398
Joerg Roedela6d41a42009-09-02 17:08:55 +02003399 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003400 return 0;
3401
Joerg Roedelf03152b2010-01-21 16:15:24 +01003402 if (PM_PTE_LEVEL(*pte) == 0)
3403 offset_mask = PAGE_SIZE - 1;
3404 else
3405 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3406
3407 __pte = *pte & PM_ADDR_MASK;
3408 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003409
3410 return paddr;
3411}
3412
Joerg Roedelab636482014-09-05 10:48:21 +02003413static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003414{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003415 switch (cap) {
3416 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003417 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003418 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003419 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003420 case IOMMU_CAP_NOEXEC:
3421 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003422 }
3423
Joerg Roedelab636482014-09-05 10:48:21 +02003424 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003425}
3426
Thierry Redingb22f6432014-06-27 09:03:12 +02003427static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003428 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003429 .domain_alloc = amd_iommu_domain_alloc,
3430 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003431 .attach_dev = amd_iommu_attach_device,
3432 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003433 .map = amd_iommu_map,
3434 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003435 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003436 .iova_to_phys = amd_iommu_iova_to_phys,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003437 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003438};
3439
Joerg Roedel0feae532009-08-26 15:26:30 +02003440/*****************************************************************************
3441 *
3442 * The next functions do a basic initialization of IOMMU for pass through
3443 * mode
3444 *
3445 * In passthrough mode the IOMMU is initialized and enabled but not used for
3446 * DMA-API translation.
3447 *
3448 *****************************************************************************/
3449
3450int __init amd_iommu_init_passthrough(void)
3451{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003452 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003453 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003454 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003455
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003456 ret = alloc_passthrough_domain();
3457 if (ret)
3458 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003459
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003460 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003461 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003462 continue;
3463
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003464 dev_data = get_dev_data(&dev->dev);
3465 dev_data->passthrough = true;
3466
Joerg Roedel15898bb2009-11-24 15:39:42 +01003467 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003468 }
3469
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003470 amd_iommu_stats_init();
3471
Joerg Roedel0feae532009-08-26 15:26:30 +02003472 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3473
3474 return 0;
3475}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003476
3477/* IOMMUv2 specific functions */
3478int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3479{
3480 return atomic_notifier_chain_register(&ppr_notifier, nb);
3481}
3482EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3483
3484int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3485{
3486 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3487}
3488EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003489
3490void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3491{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003492 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003493 unsigned long flags;
3494
3495 spin_lock_irqsave(&domain->lock, flags);
3496
3497 /* Update data structure */
3498 domain->mode = PAGE_MODE_NONE;
3499 domain->updated = true;
3500
3501 /* Make changes visible to IOMMUs */
3502 update_domain(domain);
3503
3504 /* Page-table is not visible to IOMMU anymore, so free it */
3505 free_pagetable(domain);
3506
3507 spin_unlock_irqrestore(&domain->lock, flags);
3508}
3509EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003510
3511int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3512{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003513 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003514 unsigned long flags;
3515 int levels, ret;
3516
3517 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3518 return -EINVAL;
3519
3520 /* Number of GCR3 table levels required */
3521 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3522 levels += 1;
3523
3524 if (levels > amd_iommu_max_glx_val)
3525 return -EINVAL;
3526
3527 spin_lock_irqsave(&domain->lock, flags);
3528
3529 /*
3530 * Save us all sanity checks whether devices already in the
3531 * domain support IOMMUv2. Just force that the domain has no
3532 * devices attached when it is switched into IOMMUv2 mode.
3533 */
3534 ret = -EBUSY;
3535 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3536 goto out;
3537
3538 ret = -ENOMEM;
3539 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3540 if (domain->gcr3_tbl == NULL)
3541 goto out;
3542
3543 domain->glx = levels;
3544 domain->flags |= PD_IOMMUV2_MASK;
3545 domain->updated = true;
3546
3547 update_domain(domain);
3548
3549 ret = 0;
3550
3551out:
3552 spin_unlock_irqrestore(&domain->lock, flags);
3553
3554 return ret;
3555}
3556EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003557
3558static int __flush_pasid(struct protection_domain *domain, int pasid,
3559 u64 address, bool size)
3560{
3561 struct iommu_dev_data *dev_data;
3562 struct iommu_cmd cmd;
3563 int i, ret;
3564
3565 if (!(domain->flags & PD_IOMMUV2_MASK))
3566 return -EINVAL;
3567
3568 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3569
3570 /*
3571 * IOMMU TLB needs to be flushed before Device TLB to
3572 * prevent device TLB refill from IOMMU TLB
3573 */
3574 for (i = 0; i < amd_iommus_present; ++i) {
3575 if (domain->dev_iommu[i] == 0)
3576 continue;
3577
3578 ret = iommu_queue_command(amd_iommus[i], &cmd);
3579 if (ret != 0)
3580 goto out;
3581 }
3582
3583 /* Wait until IOMMU TLB flushes are complete */
3584 domain_flush_complete(domain);
3585
3586 /* Now flush device TLBs */
3587 list_for_each_entry(dev_data, &domain->dev_list, list) {
3588 struct amd_iommu *iommu;
3589 int qdep;
3590
3591 BUG_ON(!dev_data->ats.enabled);
3592
3593 qdep = dev_data->ats.qdep;
3594 iommu = amd_iommu_rlookup_table[dev_data->devid];
3595
3596 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3597 qdep, address, size);
3598
3599 ret = iommu_queue_command(iommu, &cmd);
3600 if (ret != 0)
3601 goto out;
3602 }
3603
3604 /* Wait until all device TLBs are flushed */
3605 domain_flush_complete(domain);
3606
3607 ret = 0;
3608
3609out:
3610
3611 return ret;
3612}
3613
3614static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3615 u64 address)
3616{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003617 INC_STATS_COUNTER(invalidate_iotlb);
3618
Joerg Roedel22e266c2011-11-21 15:59:08 +01003619 return __flush_pasid(domain, pasid, address, false);
3620}
3621
3622int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3623 u64 address)
3624{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003625 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003626 unsigned long flags;
3627 int ret;
3628
3629 spin_lock_irqsave(&domain->lock, flags);
3630 ret = __amd_iommu_flush_page(domain, pasid, address);
3631 spin_unlock_irqrestore(&domain->lock, flags);
3632
3633 return ret;
3634}
3635EXPORT_SYMBOL(amd_iommu_flush_page);
3636
3637static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3638{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003639 INC_STATS_COUNTER(invalidate_iotlb_all);
3640
Joerg Roedel22e266c2011-11-21 15:59:08 +01003641 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3642 true);
3643}
3644
3645int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3646{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003647 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003648 unsigned long flags;
3649 int ret;
3650
3651 spin_lock_irqsave(&domain->lock, flags);
3652 ret = __amd_iommu_flush_tlb(domain, pasid);
3653 spin_unlock_irqrestore(&domain->lock, flags);
3654
3655 return ret;
3656}
3657EXPORT_SYMBOL(amd_iommu_flush_tlb);
3658
Joerg Roedelb16137b2011-11-21 16:50:23 +01003659static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3660{
3661 int index;
3662 u64 *pte;
3663
3664 while (true) {
3665
3666 index = (pasid >> (9 * level)) & 0x1ff;
3667 pte = &root[index];
3668
3669 if (level == 0)
3670 break;
3671
3672 if (!(*pte & GCR3_VALID)) {
3673 if (!alloc)
3674 return NULL;
3675
3676 root = (void *)get_zeroed_page(GFP_ATOMIC);
3677 if (root == NULL)
3678 return NULL;
3679
3680 *pte = __pa(root) | GCR3_VALID;
3681 }
3682
3683 root = __va(*pte & PAGE_MASK);
3684
3685 level -= 1;
3686 }
3687
3688 return pte;
3689}
3690
3691static int __set_gcr3(struct protection_domain *domain, int pasid,
3692 unsigned long cr3)
3693{
3694 u64 *pte;
3695
3696 if (domain->mode != PAGE_MODE_NONE)
3697 return -EINVAL;
3698
3699 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3700 if (pte == NULL)
3701 return -ENOMEM;
3702
3703 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3704
3705 return __amd_iommu_flush_tlb(domain, pasid);
3706}
3707
3708static int __clear_gcr3(struct protection_domain *domain, int pasid)
3709{
3710 u64 *pte;
3711
3712 if (domain->mode != PAGE_MODE_NONE)
3713 return -EINVAL;
3714
3715 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3716 if (pte == NULL)
3717 return 0;
3718
3719 *pte = 0;
3720
3721 return __amd_iommu_flush_tlb(domain, pasid);
3722}
3723
3724int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3725 unsigned long cr3)
3726{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003727 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003728 unsigned long flags;
3729 int ret;
3730
3731 spin_lock_irqsave(&domain->lock, flags);
3732 ret = __set_gcr3(domain, pasid, cr3);
3733 spin_unlock_irqrestore(&domain->lock, flags);
3734
3735 return ret;
3736}
3737EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3738
3739int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3740{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003741 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003742 unsigned long flags;
3743 int ret;
3744
3745 spin_lock_irqsave(&domain->lock, flags);
3746 ret = __clear_gcr3(domain, pasid);
3747 spin_unlock_irqrestore(&domain->lock, flags);
3748
3749 return ret;
3750}
3751EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003752
3753int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3754 int status, int tag)
3755{
3756 struct iommu_dev_data *dev_data;
3757 struct amd_iommu *iommu;
3758 struct iommu_cmd cmd;
3759
Joerg Roedel399be2f2011-12-01 16:53:47 +01003760 INC_STATS_COUNTER(complete_ppr);
3761
Joerg Roedelc99afa22011-11-21 18:19:25 +01003762 dev_data = get_dev_data(&pdev->dev);
3763 iommu = amd_iommu_rlookup_table[dev_data->devid];
3764
3765 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3766 tag, dev_data->pri_tlp);
3767
3768 return iommu_queue_command(iommu, &cmd);
3769}
3770EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003771
3772struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3773{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003774 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003775
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003776 pdomain = get_domain(&pdev->dev);
3777 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003778 return NULL;
3779
3780 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003781 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003782 return NULL;
3783
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003784 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003785}
3786EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003787
3788void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3789{
3790 struct iommu_dev_data *dev_data;
3791
3792 if (!amd_iommu_v2_supported())
3793 return;
3794
3795 dev_data = get_dev_data(&pdev->dev);
3796 dev_data->errata |= (1 << erratum);
3797}
3798EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003799
3800int amd_iommu_device_info(struct pci_dev *pdev,
3801 struct amd_iommu_device_info *info)
3802{
3803 int max_pasids;
3804 int pos;
3805
3806 if (pdev == NULL || info == NULL)
3807 return -EINVAL;
3808
3809 if (!amd_iommu_v2_supported())
3810 return -EINVAL;
3811
3812 memset(info, 0, sizeof(*info));
3813
3814 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3815 if (pos)
3816 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3817
3818 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3819 if (pos)
3820 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3821
3822 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3823 if (pos) {
3824 int features;
3825
3826 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3827 max_pasids = min(max_pasids, (1 << 20));
3828
3829 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3830 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3831
3832 features = pci_pasid_features(pdev);
3833 if (features & PCI_PASID_CAP_EXEC)
3834 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3835 if (features & PCI_PASID_CAP_PRIV)
3836 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3837 }
3838
3839 return 0;
3840}
3841EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003842
3843#ifdef CONFIG_IRQ_REMAP
3844
3845/*****************************************************************************
3846 *
3847 * Interrupt Remapping Implementation
3848 *
3849 *****************************************************************************/
3850
3851union irte {
3852 u32 val;
3853 struct {
3854 u32 valid : 1,
3855 no_fault : 1,
3856 int_type : 3,
3857 rq_eoi : 1,
3858 dm : 1,
3859 rsvd_1 : 1,
3860 destination : 8,
3861 vector : 8,
3862 rsvd_2 : 8;
3863 } fields;
3864};
3865
3866#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3867#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3868#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3869#define DTE_IRQ_REMAP_ENABLE 1ULL
3870
3871static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3872{
3873 u64 dte;
3874
3875 dte = amd_iommu_dev_table[devid].data[2];
3876 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3877 dte |= virt_to_phys(table->table);
3878 dte |= DTE_IRQ_REMAP_INTCTL;
3879 dte |= DTE_IRQ_TABLE_LEN;
3880 dte |= DTE_IRQ_REMAP_ENABLE;
3881
3882 amd_iommu_dev_table[devid].data[2] = dte;
3883}
3884
3885#define IRTE_ALLOCATED (~1U)
3886
3887static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3888{
3889 struct irq_remap_table *table = NULL;
3890 struct amd_iommu *iommu;
3891 unsigned long flags;
3892 u16 alias;
3893
3894 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3895
3896 iommu = amd_iommu_rlookup_table[devid];
3897 if (!iommu)
3898 goto out_unlock;
3899
3900 table = irq_lookup_table[devid];
3901 if (table)
3902 goto out;
3903
3904 alias = amd_iommu_alias_table[devid];
3905 table = irq_lookup_table[alias];
3906 if (table) {
3907 irq_lookup_table[devid] = table;
3908 set_dte_irq_entry(devid, table);
3909 iommu_flush_dte(iommu, devid);
3910 goto out;
3911 }
3912
3913 /* Nothing there yet, allocate new irq remapping table */
3914 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3915 if (!table)
3916 goto out;
3917
Joerg Roedel197887f2013-04-09 21:14:08 +02003918 /* Initialize table spin-lock */
3919 spin_lock_init(&table->lock);
3920
Joerg Roedel2b324502012-06-21 16:29:10 +02003921 if (ioapic)
3922 /* Keep the first 32 indexes free for IOAPIC interrupts */
3923 table->min_index = 32;
3924
3925 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3926 if (!table->table) {
3927 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003928 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003929 goto out;
3930 }
3931
3932 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3933
3934 if (ioapic) {
3935 int i;
3936
3937 for (i = 0; i < 32; ++i)
3938 table->table[i] = IRTE_ALLOCATED;
3939 }
3940
3941 irq_lookup_table[devid] = table;
3942 set_dte_irq_entry(devid, table);
3943 iommu_flush_dte(iommu, devid);
3944 if (devid != alias) {
3945 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003946 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003947 iommu_flush_dte(iommu, alias);
3948 }
3949
3950out:
3951 iommu_completion_wait(iommu);
3952
3953out_unlock:
3954 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3955
3956 return table;
3957}
3958
3959static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3960{
3961 struct irq_remap_table *table;
3962 unsigned long flags;
3963 int index, c;
3964
3965 table = get_irq_table(devid, false);
3966 if (!table)
3967 return -ENODEV;
3968
3969 spin_lock_irqsave(&table->lock, flags);
3970
3971 /* Scan table for free entries */
3972 for (c = 0, index = table->min_index;
3973 index < MAX_IRQS_PER_TABLE;
3974 ++index) {
3975 if (table->table[index] == 0)
3976 c += 1;
3977 else
3978 c = 0;
3979
3980 if (c == count) {
Joerg Roedel0dfedd62013-04-09 15:39:16 +02003981 struct irq_2_irte *irte_info;
Joerg Roedel2b324502012-06-21 16:29:10 +02003982
3983 for (; c != 0; --c)
3984 table->table[index - c + 1] = IRTE_ALLOCATED;
3985
3986 index -= count - 1;
3987
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02003988 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02003989 irte_info = &cfg->irq_2_irte;
3990 irte_info->devid = devid;
3991 irte_info->index = index;
Joerg Roedel2b324502012-06-21 16:29:10 +02003992
3993 goto out;
3994 }
3995 }
3996
3997 index = -ENOSPC;
3998
3999out:
4000 spin_unlock_irqrestore(&table->lock, flags);
4001
4002 return index;
4003}
4004
4005static int get_irte(u16 devid, int index, union irte *irte)
4006{
4007 struct irq_remap_table *table;
4008 unsigned long flags;
4009
4010 table = get_irq_table(devid, false);
4011 if (!table)
4012 return -ENOMEM;
4013
4014 spin_lock_irqsave(&table->lock, flags);
4015 irte->val = table->table[index];
4016 spin_unlock_irqrestore(&table->lock, flags);
4017
4018 return 0;
4019}
4020
4021static int modify_irte(u16 devid, int index, union irte irte)
4022{
4023 struct irq_remap_table *table;
4024 struct amd_iommu *iommu;
4025 unsigned long flags;
4026
4027 iommu = amd_iommu_rlookup_table[devid];
4028 if (iommu == NULL)
4029 return -EINVAL;
4030
4031 table = get_irq_table(devid, false);
4032 if (!table)
4033 return -ENOMEM;
4034
4035 spin_lock_irqsave(&table->lock, flags);
4036 table->table[index] = irte.val;
4037 spin_unlock_irqrestore(&table->lock, flags);
4038
4039 iommu_flush_irt(iommu, devid);
4040 iommu_completion_wait(iommu);
4041
4042 return 0;
4043}
4044
4045static void free_irte(u16 devid, int index)
4046{
4047 struct irq_remap_table *table;
4048 struct amd_iommu *iommu;
4049 unsigned long flags;
4050
4051 iommu = amd_iommu_rlookup_table[devid];
4052 if (iommu == NULL)
4053 return;
4054
4055 table = get_irq_table(devid, false);
4056 if (!table)
4057 return;
4058
4059 spin_lock_irqsave(&table->lock, flags);
4060 table->table[index] = 0;
4061 spin_unlock_irqrestore(&table->lock, flags);
4062
4063 iommu_flush_irt(iommu, devid);
4064 iommu_completion_wait(iommu);
4065}
4066
Joerg Roedel5527de72012-06-26 11:17:32 +02004067static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4068 unsigned int destination, int vector,
4069 struct io_apic_irq_attr *attr)
4070{
4071 struct irq_remap_table *table;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004072 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004073 struct irq_cfg *cfg;
4074 union irte irte;
4075 int ioapic_id;
4076 int index;
4077 int devid;
4078 int ret;
4079
Jiang Liu719b5302014-10-27 16:12:10 +08004080 cfg = irq_cfg(irq);
Joerg Roedel5527de72012-06-26 11:17:32 +02004081 if (!cfg)
4082 return -EINVAL;
4083
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004084 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004085 ioapic_id = mpc_ioapic_id(attr->ioapic);
4086 devid = get_ioapic_devid(ioapic_id);
4087
4088 if (devid < 0)
4089 return devid;
4090
4091 table = get_irq_table(devid, true);
4092 if (table == NULL)
4093 return -ENOMEM;
4094
4095 index = attr->ioapic_pin;
4096
4097 /* Setup IRQ remapping info */
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004098 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004099 irte_info->devid = devid;
4100 irte_info->index = index;
Joerg Roedel5527de72012-06-26 11:17:32 +02004101
4102 /* Setup IRTE for IOMMU */
4103 irte.val = 0;
4104 irte.fields.vector = vector;
4105 irte.fields.int_type = apic->irq_delivery_mode;
4106 irte.fields.destination = destination;
4107 irte.fields.dm = apic->irq_dest_mode;
4108 irte.fields.valid = 1;
4109
4110 ret = modify_irte(devid, index, irte);
4111 if (ret)
4112 return ret;
4113
4114 /* Setup IOAPIC entry */
4115 memset(entry, 0, sizeof(*entry));
4116
4117 entry->vector = index;
4118 entry->mask = 0;
4119 entry->trigger = attr->trigger;
4120 entry->polarity = attr->polarity;
4121
4122 /*
4123 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004124 */
4125 if (attr->trigger)
4126 entry->mask = 1;
4127
4128 return 0;
4129}
4130
4131static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4132 bool force)
4133{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004134 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004135 unsigned int dest, irq;
4136 struct irq_cfg *cfg;
4137 union irte irte;
4138 int err;
4139
4140 if (!config_enabled(CONFIG_SMP))
4141 return -1;
4142
Jiang Liu719b5302014-10-27 16:12:10 +08004143 cfg = irqd_cfg(data);
Joerg Roedel5527de72012-06-26 11:17:32 +02004144 irq = data->irq;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004145 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004146
4147 if (!cpumask_intersects(mask, cpu_online_mask))
4148 return -EINVAL;
4149
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004150 if (get_irte(irte_info->devid, irte_info->index, &irte))
Joerg Roedel5527de72012-06-26 11:17:32 +02004151 return -EBUSY;
4152
4153 if (assign_irq_vector(irq, cfg, mask))
4154 return -EBUSY;
4155
4156 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4157 if (err) {
4158 if (assign_irq_vector(irq, cfg, data->affinity))
4159 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4160 return err;
4161 }
4162
4163 irte.fields.vector = cfg->vector;
4164 irte.fields.destination = dest;
4165
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004166 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel5527de72012-06-26 11:17:32 +02004167
4168 if (cfg->move_in_progress)
4169 send_cleanup_vector(cfg);
4170
4171 cpumask_copy(data->affinity, mask);
4172
4173 return 0;
4174}
4175
4176static int free_irq(int irq)
4177{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004178 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004179 struct irq_cfg *cfg;
4180
Jiang Liu719b5302014-10-27 16:12:10 +08004181 cfg = irq_cfg(irq);
Joerg Roedel5527de72012-06-26 11:17:32 +02004182 if (!cfg)
4183 return -EINVAL;
4184
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004185 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004186
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004187 free_irte(irte_info->devid, irte_info->index);
Joerg Roedel5527de72012-06-26 11:17:32 +02004188
4189 return 0;
4190}
4191
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004192static void compose_msi_msg(struct pci_dev *pdev,
4193 unsigned int irq, unsigned int dest,
4194 struct msi_msg *msg, u8 hpet_id)
4195{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004196 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004197 struct irq_cfg *cfg;
4198 union irte irte;
4199
Jiang Liu719b5302014-10-27 16:12:10 +08004200 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004201 if (!cfg)
4202 return;
4203
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004204 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004205
4206 irte.val = 0;
4207 irte.fields.vector = cfg->vector;
4208 irte.fields.int_type = apic->irq_delivery_mode;
4209 irte.fields.destination = dest;
4210 irte.fields.dm = apic->irq_dest_mode;
4211 irte.fields.valid = 1;
4212
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004213 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004214
4215 msg->address_hi = MSI_ADDR_BASE_HI;
4216 msg->address_lo = MSI_ADDR_BASE_LO;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004217 msg->data = irte_info->index;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004218}
4219
4220static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4221{
4222 struct irq_cfg *cfg;
4223 int index;
4224 u16 devid;
4225
4226 if (!pdev)
4227 return -EINVAL;
4228
Jiang Liu719b5302014-10-27 16:12:10 +08004229 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004230 if (!cfg)
4231 return -EINVAL;
4232
4233 devid = get_device_id(&pdev->dev);
4234 index = alloc_irq_index(cfg, devid, nvec);
4235
4236 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4237}
4238
4239static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4240 int index, int offset)
4241{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004242 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004243 struct irq_cfg *cfg;
4244 u16 devid;
4245
4246 if (!pdev)
4247 return -EINVAL;
4248
Jiang Liu719b5302014-10-27 16:12:10 +08004249 cfg = irq_cfg(irq);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004250 if (!cfg)
4251 return -EINVAL;
4252
4253 if (index >= MAX_IRQS_PER_TABLE)
4254 return 0;
4255
4256 devid = get_device_id(&pdev->dev);
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004257 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004258
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004259 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004260 irte_info->devid = devid;
4261 irte_info->index = index + offset;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004262
4263 return 0;
4264}
4265
Yijing Wang5fc24d82014-09-17 17:32:19 +08004266static int alloc_hpet_msi(unsigned int irq, unsigned int id)
Joerg Roedeld9761952012-06-26 16:00:08 +02004267{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004268 struct irq_2_irte *irte_info;
Joerg Roedeld9761952012-06-26 16:00:08 +02004269 struct irq_cfg *cfg;
4270 int index, devid;
4271
Jiang Liu719b5302014-10-27 16:12:10 +08004272 cfg = irq_cfg(irq);
Joerg Roedeld9761952012-06-26 16:00:08 +02004273 if (!cfg)
4274 return -EINVAL;
4275
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004276 irte_info = &cfg->irq_2_irte;
Joerg Roedeld9761952012-06-26 16:00:08 +02004277 devid = get_hpet_devid(id);
4278 if (devid < 0)
4279 return devid;
4280
4281 index = alloc_irq_index(cfg, devid, 1);
4282 if (index < 0)
4283 return index;
4284
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004285 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004286 irte_info->devid = devid;
4287 irte_info->index = index;
Joerg Roedeld9761952012-06-26 16:00:08 +02004288
4289 return 0;
4290}
4291
Joerg Roedel6b474b82012-06-26 16:46:04 +02004292struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02004293 .prepare = amd_iommu_prepare,
4294 .enable = amd_iommu_enable,
4295 .disable = amd_iommu_disable,
4296 .reenable = amd_iommu_reenable,
4297 .enable_faulting = amd_iommu_enable_faulting,
4298 .setup_ioapic_entry = setup_ioapic_entry,
4299 .set_affinity = set_affinity,
4300 .free_irq = free_irq,
4301 .compose_msi_msg = compose_msi_msg,
4302 .msi_alloc_irq = msi_alloc_irq,
4303 .msi_setup_irq = msi_setup_irq,
Yijing Wang5fc24d82014-09-17 17:32:19 +08004304 .alloc_hpet_msi = alloc_hpet_msi,
Joerg Roedel6b474b82012-06-26 16:46:04 +02004305};
Joerg Roedel2b324502012-06-21 16:29:10 +02004306#endif