blob: 6f514831af1f307fe5035dbf3980b1049b0ab41f [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 Roedelb6c02712008-06-26 21:27:53 +02003 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * 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 Roedel17f5b562011-07-06 17:14:44 +020034#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020035#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090036#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010037#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020038#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020039
40#include "amd_iommu_proto.h"
41#include "amd_iommu_types.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020042
43#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
44
Joerg Roedel815b33f2011-04-06 17:26:49 +020045#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020046
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020047/*
48 * This bitmap is used to advertise the page sizes our hardware support
49 * to the IOMMU core, which will then use this information to split
50 * physically contiguous memory regions it is mapping into page sizes
51 * that we support.
52 *
53 * Traditionally the IOMMU core just handed us the mappings directly,
54 * after making sure the size is an order of a 4KiB page and that the
55 * mapping has natural alignment.
56 *
57 * To retain this behavior, we currently advertise that we support
58 * all page sizes that are an order of 4KiB.
59 *
60 * If at some point we'd like to utilize the IOMMU core's new behavior,
61 * we could change this to advertise the real page sizes we support.
62 */
63#define AMD_IOMMU_PGSIZES (~0xFFFUL)
64
Joerg Roedelb6c02712008-06-26 21:27:53 +020065static DEFINE_RWLOCK(amd_iommu_devtable_lock);
66
Joerg Roedelbd60b732008-09-11 10:24:48 +020067/* A list of preallocated protection domains */
68static LIST_HEAD(iommu_pd_list);
69static DEFINE_SPINLOCK(iommu_pd_list_lock);
70
Joerg Roedel8fa5f802011-06-09 12:24:45 +020071/* List of all available dev_data structures */
72static LIST_HEAD(dev_data_list);
73static DEFINE_SPINLOCK(dev_data_list_lock);
74
Joerg Roedel6efed632012-06-14 15:52:58 +020075LIST_HEAD(ioapic_map);
76LIST_HEAD(hpet_map);
77
Joerg Roedel0feae532009-08-26 15:26:30 +020078/*
79 * Domain for untranslated devices - only allocated
80 * if iommu=pt passed on kernel cmd line.
81 */
82static struct protection_domain *pt_domain;
83
Joerg Roedel26961ef2008-12-03 17:00:17 +010084static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010085
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010086static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010087int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010088
Joerg Roedelac1534a2012-06-21 14:52:40 +020089static struct dma_map_ops amd_iommu_dma_ops;
90
Joerg Roedel431b2a22008-07-11 17:14:22 +020091/*
92 * general struct to manage commands send to an IOMMU
93 */
Joerg Roedeld6449532008-07-11 17:14:28 +020094struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020095 u32 data[4];
96};
97
Joerg Roedel04bfdd82009-09-02 16:00:23 +020098static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +010099static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700100
Joerg Roedel15898bb2009-11-24 15:39:42 +0100101/****************************************************************************
102 *
103 * Helper functions
104 *
105 ****************************************************************************/
106
Joerg Roedelf62dda62011-06-09 12:55:35 +0200107static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200108{
109 struct iommu_dev_data *dev_data;
110 unsigned long flags;
111
112 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
113 if (!dev_data)
114 return NULL;
115
Joerg Roedelf62dda62011-06-09 12:55:35 +0200116 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200117 atomic_set(&dev_data->bind, 0);
118
119 spin_lock_irqsave(&dev_data_list_lock, flags);
120 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
121 spin_unlock_irqrestore(&dev_data_list_lock, flags);
122
123 return dev_data;
124}
125
126static void free_dev_data(struct iommu_dev_data *dev_data)
127{
128 unsigned long flags;
129
130 spin_lock_irqsave(&dev_data_list_lock, flags);
131 list_del(&dev_data->dev_data_list);
132 spin_unlock_irqrestore(&dev_data_list_lock, flags);
133
134 kfree(dev_data);
135}
136
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200137static struct iommu_dev_data *search_dev_data(u16 devid)
138{
139 struct iommu_dev_data *dev_data;
140 unsigned long flags;
141
142 spin_lock_irqsave(&dev_data_list_lock, flags);
143 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
144 if (dev_data->devid == devid)
145 goto out_unlock;
146 }
147
148 dev_data = NULL;
149
150out_unlock:
151 spin_unlock_irqrestore(&dev_data_list_lock, flags);
152
153 return dev_data;
154}
155
156static struct iommu_dev_data *find_dev_data(u16 devid)
157{
158 struct iommu_dev_data *dev_data;
159
160 dev_data = search_dev_data(devid);
161
162 if (dev_data == NULL)
163 dev_data = alloc_dev_data(devid);
164
165 return dev_data;
166}
167
Joerg Roedel15898bb2009-11-24 15:39:42 +0100168static inline u16 get_device_id(struct device *dev)
169{
170 struct pci_dev *pdev = to_pci_dev(dev);
171
172 return calc_devid(pdev->bus->number, pdev->devfn);
173}
174
Joerg Roedel657cbb62009-11-23 15:26:46 +0100175static struct iommu_dev_data *get_dev_data(struct device *dev)
176{
177 return dev->archdata.iommu;
178}
179
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100180static bool pci_iommuv2_capable(struct pci_dev *pdev)
181{
182 static const int caps[] = {
183 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100184 PCI_EXT_CAP_ID_PRI,
185 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100186 };
187 int i, pos;
188
189 for (i = 0; i < 3; ++i) {
190 pos = pci_find_ext_capability(pdev, caps[i]);
191 if (pos == 0)
192 return false;
193 }
194
195 return true;
196}
197
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100198static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
199{
200 struct iommu_dev_data *dev_data;
201
202 dev_data = get_dev_data(&pdev->dev);
203
204 return dev_data->errata & (1 << erratum) ? true : false;
205}
206
Joerg Roedel71c70982009-11-24 16:43:06 +0100207/*
208 * In this function the list of preallocated protection domains is traversed to
209 * find the domain for a specific device
210 */
211static struct dma_ops_domain *find_protection_domain(u16 devid)
212{
213 struct dma_ops_domain *entry, *ret = NULL;
214 unsigned long flags;
215 u16 alias = amd_iommu_alias_table[devid];
216
217 if (list_empty(&iommu_pd_list))
218 return NULL;
219
220 spin_lock_irqsave(&iommu_pd_list_lock, flags);
221
222 list_for_each_entry(entry, &iommu_pd_list, list) {
223 if (entry->target_dev == devid ||
224 entry->target_dev == alias) {
225 ret = entry;
226 break;
227 }
228 }
229
230 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
231
232 return ret;
233}
234
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100235/*
236 * This function checks if the driver got a valid device from the caller to
237 * avoid dereferencing invalid pointers.
238 */
239static bool check_device(struct device *dev)
240{
241 u16 devid;
242
243 if (!dev || !dev->dma_mask)
244 return false;
245
246 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100247 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100248 return false;
249
250 devid = get_device_id(dev);
251
252 /* Out of our scope? */
253 if (devid > amd_iommu_last_bdf)
254 return false;
255
256 if (amd_iommu_rlookup_table[devid] == NULL)
257 return false;
258
259 return true;
260}
261
Alex Williamson664b6002012-05-30 14:19:31 -0600262static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
263{
264 pci_dev_put(*from);
265 *from = to;
266}
267
268#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
269
Joerg Roedel657cbb62009-11-23 15:26:46 +0100270static int iommu_init_device(struct device *dev)
271{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600272 struct pci_dev *dma_pdev, *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100273 struct iommu_dev_data *dev_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600274 struct iommu_group *group;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200275 u16 alias;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600276 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100277
278 if (dev->archdata.iommu)
279 return 0;
280
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200281 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100282 if (!dev_data)
283 return -ENOMEM;
284
Joerg Roedelf62dda62011-06-09 12:55:35 +0200285 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200286 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200287 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100288
Joerg Roedel71f77582011-06-09 19:03:15 +0200289 alias_data = find_dev_data(alias);
290 if (alias_data == NULL) {
291 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
292 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200293 free_dev_data(dev_data);
294 return -ENOTSUPP;
295 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200296 dev_data->alias_data = alias_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600297
298 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
299 } else
300 dma_pdev = pci_dev_get(pdev);
301
Alex Williamson31fe9432012-08-04 12:09:03 -0600302 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600303 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
304
Alex Williamson31fe9432012-08-04 12:09:03 -0600305 /*
306 * If it's a multifunction device that does not support our
307 * required ACS flags, add to the same group as function 0.
308 */
Alex Williamson664b6002012-05-30 14:19:31 -0600309 if (dma_pdev->multifunction &&
310 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
311 swap_pci_ref(&dma_pdev,
312 pci_get_slot(dma_pdev->bus,
313 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
314 0)));
315
Alex Williamson31fe9432012-08-04 12:09:03 -0600316 /*
317 * Devices on the root bus go through the iommu. If that's not us,
318 * find the next upstream device and test ACS up to the root bus.
319 * Finding the next device may require skipping virtual buses.
320 */
Alex Williamson664b6002012-05-30 14:19:31 -0600321 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson31fe9432012-08-04 12:09:03 -0600322 struct pci_bus *bus = dma_pdev->bus;
323
324 while (!bus->self) {
325 if (!pci_is_root_bus(bus))
326 bus = bus->parent;
327 else
328 goto root_bus;
329 }
330
331 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600332 break;
333
Alex Williamson31fe9432012-08-04 12:09:03 -0600334 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200335 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100336
Alex Williamson31fe9432012-08-04 12:09:03 -0600337root_bus:
Alex Williamson9dcd6132012-05-30 14:19:07 -0600338 group = iommu_group_get(&dma_pdev->dev);
339 pci_dev_put(dma_pdev);
340 if (!group) {
341 group = iommu_group_alloc();
342 if (IS_ERR(group))
343 return PTR_ERR(group);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100344 }
345
Alex Williamson9dcd6132012-05-30 14:19:07 -0600346 ret = iommu_group_add_device(group, dev);
347
348 iommu_group_put(group);
349
350 if (ret)
351 return ret;
352
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100353 if (pci_iommuv2_capable(pdev)) {
354 struct amd_iommu *iommu;
355
356 iommu = amd_iommu_rlookup_table[dev_data->devid];
357 dev_data->iommu_v2 = iommu->is_iommu_v2;
358 }
359
Joerg Roedel657cbb62009-11-23 15:26:46 +0100360 dev->archdata.iommu = dev_data;
361
Joerg Roedel657cbb62009-11-23 15:26:46 +0100362 return 0;
363}
364
Joerg Roedel26018872011-06-06 16:50:14 +0200365static void iommu_ignore_device(struct device *dev)
366{
367 u16 devid, alias;
368
369 devid = get_device_id(dev);
370 alias = amd_iommu_alias_table[devid];
371
372 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
373 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
374
375 amd_iommu_rlookup_table[devid] = NULL;
376 amd_iommu_rlookup_table[alias] = NULL;
377}
378
Joerg Roedel657cbb62009-11-23 15:26:46 +0100379static void iommu_uninit_device(struct device *dev)
380{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600381 iommu_group_remove_device(dev);
382
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200383 /*
384 * Nothing to do here - we keep dev_data around for unplugged devices
385 * and reuse it when the device is re-plugged - not doing so would
386 * introduce a ton of races.
387 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100388}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100389
390void __init amd_iommu_uninit_devices(void)
391{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200392 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100393 struct pci_dev *pdev = NULL;
394
395 for_each_pci_dev(pdev) {
396
397 if (!check_device(&pdev->dev))
398 continue;
399
400 iommu_uninit_device(&pdev->dev);
401 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200402
403 /* Free all of our dev_data structures */
404 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
405 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100406}
407
408int __init amd_iommu_init_devices(void)
409{
410 struct pci_dev *pdev = NULL;
411 int ret = 0;
412
413 for_each_pci_dev(pdev) {
414
415 if (!check_device(&pdev->dev))
416 continue;
417
418 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200419 if (ret == -ENOTSUPP)
420 iommu_ignore_device(&pdev->dev);
421 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100422 goto out_free;
423 }
424
425 return 0;
426
427out_free:
428
429 amd_iommu_uninit_devices();
430
431 return ret;
432}
Joerg Roedel7f265082008-12-12 13:50:21 +0100433#ifdef CONFIG_AMD_IOMMU_STATS
434
435/*
436 * Initialization code for statistics collection
437 */
438
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100439DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100440DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100441DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100442DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100443DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100444DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100445DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100446DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100447DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100448DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100449DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100450DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100451DECLARE_STATS_COUNTER(complete_ppr);
452DECLARE_STATS_COUNTER(invalidate_iotlb);
453DECLARE_STATS_COUNTER(invalidate_iotlb_all);
454DECLARE_STATS_COUNTER(pri_requests);
455
Joerg Roedel7f265082008-12-12 13:50:21 +0100456static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100457static struct dentry *de_fflush;
458
459static void amd_iommu_stats_add(struct __iommu_counter *cnt)
460{
461 if (stats_dir == NULL)
462 return;
463
464 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
465 &cnt->value);
466}
467
468static void amd_iommu_stats_init(void)
469{
470 stats_dir = debugfs_create_dir("amd-iommu", NULL);
471 if (stats_dir == NULL)
472 return;
473
Joerg Roedel7f265082008-12-12 13:50:21 +0100474 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300475 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100476
477 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100478 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100479 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100480 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100481 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100482 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100483 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100484 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100485 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100486 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100487 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100488 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100489 amd_iommu_stats_add(&complete_ppr);
490 amd_iommu_stats_add(&invalidate_iotlb);
491 amd_iommu_stats_add(&invalidate_iotlb_all);
492 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100493}
494
495#endif
496
Joerg Roedel431b2a22008-07-11 17:14:22 +0200497/****************************************************************************
498 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200499 * Interrupt handling functions
500 *
501 ****************************************************************************/
502
Joerg Roedele3e59872009-09-03 14:02:10 +0200503static void dump_dte_entry(u16 devid)
504{
505 int i;
506
Joerg Roedelee6c2862011-11-09 12:06:03 +0100507 for (i = 0; i < 4; ++i)
508 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200509 amd_iommu_dev_table[devid].data[i]);
510}
511
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200512static void dump_command(unsigned long phys_addr)
513{
514 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
515 int i;
516
517 for (i = 0; i < 4; ++i)
518 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
519}
520
Joerg Roedela345b232009-09-03 15:01:43 +0200521static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200522{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200523 int type, devid, domid, flags;
524 volatile u32 *event = __evt;
525 int count = 0;
526 u64 address;
527
528retry:
529 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
530 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
531 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
532 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
533 address = (u64)(((u64)event[3]) << 32) | event[2];
534
535 if (type == 0) {
536 /* Did we hit the erratum? */
537 if (++count == LOOP_TIMEOUT) {
538 pr_err("AMD-Vi: No event written to event log\n");
539 return;
540 }
541 udelay(1);
542 goto retry;
543 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200544
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200545 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200546
547 switch (type) {
548 case EVENT_TYPE_ILL_DEV:
549 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
550 "address=0x%016llx flags=0x%04x]\n",
551 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
552 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200553 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200554 break;
555 case EVENT_TYPE_IO_FAULT:
556 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
557 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
558 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
559 domid, address, flags);
560 break;
561 case EVENT_TYPE_DEV_TAB_ERR:
562 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
563 "address=0x%016llx flags=0x%04x]\n",
564 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
565 address, flags);
566 break;
567 case EVENT_TYPE_PAGE_TAB_ERR:
568 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
569 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
570 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
571 domid, address, flags);
572 break;
573 case EVENT_TYPE_ILL_CMD:
574 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200575 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200576 break;
577 case EVENT_TYPE_CMD_HARD_ERR:
578 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
579 "flags=0x%04x]\n", address, flags);
580 break;
581 case EVENT_TYPE_IOTLB_INV_TO:
582 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
583 "address=0x%016llx]\n",
584 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
585 address);
586 break;
587 case EVENT_TYPE_INV_DEV_REQ:
588 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
589 "address=0x%016llx flags=0x%04x]\n",
590 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
591 address, flags);
592 break;
593 default:
594 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
595 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200596
597 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200598}
599
600static void iommu_poll_events(struct amd_iommu *iommu)
601{
602 u32 head, tail;
603 unsigned long flags;
604
605 spin_lock_irqsave(&iommu->lock, flags);
606
607 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
608 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
609
610 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200611 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200612 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
613 }
614
615 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
616
617 spin_unlock_irqrestore(&iommu->lock, flags);
618}
619
Joerg Roedeleee53532012-06-01 15:20:23 +0200620static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100621{
622 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100623
Joerg Roedel399be2f2011-12-01 16:53:47 +0100624 INC_STATS_COUNTER(pri_requests);
625
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100626 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
627 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
628 return;
629 }
630
631 fault.address = raw[1];
632 fault.pasid = PPR_PASID(raw[0]);
633 fault.device_id = PPR_DEVID(raw[0]);
634 fault.tag = PPR_TAG(raw[0]);
635 fault.flags = PPR_FLAGS(raw[0]);
636
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100637 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
638}
639
640static void iommu_poll_ppr_log(struct amd_iommu *iommu)
641{
642 unsigned long flags;
643 u32 head, tail;
644
645 if (iommu->ppr_log == NULL)
646 return;
647
Joerg Roedeleee53532012-06-01 15:20:23 +0200648 /* enable ppr interrupts again */
649 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
650
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100651 spin_lock_irqsave(&iommu->lock, flags);
652
653 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
654 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
655
656 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200657 volatile u64 *raw;
658 u64 entry[2];
659 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100660
Joerg Roedeleee53532012-06-01 15:20:23 +0200661 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100662
Joerg Roedeleee53532012-06-01 15:20:23 +0200663 /*
664 * Hardware bug: Interrupt may arrive before the entry is
665 * written to memory. If this happens we need to wait for the
666 * entry to arrive.
667 */
668 for (i = 0; i < LOOP_TIMEOUT; ++i) {
669 if (PPR_REQ_TYPE(raw[0]) != 0)
670 break;
671 udelay(1);
672 }
673
674 /* Avoid memcpy function-call overhead */
675 entry[0] = raw[0];
676 entry[1] = raw[1];
677
678 /*
679 * To detect the hardware bug we need to clear the entry
680 * back to zero.
681 */
682 raw[0] = raw[1] = 0UL;
683
684 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100685 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
686 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200687
688 /*
689 * Release iommu->lock because ppr-handling might need to
690 * re-aquire it
691 */
692 spin_unlock_irqrestore(&iommu->lock, flags);
693
694 /* Handle PPR entry */
695 iommu_handle_ppr_entry(iommu, entry);
696
697 spin_lock_irqsave(&iommu->lock, flags);
698
699 /* Refresh ring-buffer information */
700 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100701 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
702 }
703
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100704 spin_unlock_irqrestore(&iommu->lock, flags);
705}
706
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200707irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200708{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200709 struct amd_iommu *iommu;
710
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100711 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200712 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100713 iommu_poll_ppr_log(iommu);
714 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200715
716 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200717}
718
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200719irqreturn_t amd_iommu_int_handler(int irq, void *data)
720{
721 return IRQ_WAKE_THREAD;
722}
723
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200724/****************************************************************************
725 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200726 * IOMMU command queuing functions
727 *
728 ****************************************************************************/
729
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200730static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200731{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200732 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200733
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200734 while (*sem == 0 && i < LOOP_TIMEOUT) {
735 udelay(1);
736 i += 1;
737 }
738
739 if (i == LOOP_TIMEOUT) {
740 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
741 return -EIO;
742 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200743
744 return 0;
745}
746
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200747static void copy_cmd_to_buffer(struct amd_iommu *iommu,
748 struct iommu_cmd *cmd,
749 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200750{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200751 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200752
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200753 target = iommu->cmd_buf + tail;
754 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200755
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200756 /* Copy command to buffer */
757 memcpy(target, cmd, sizeof(*cmd));
758
759 /* Tell the IOMMU about it */
760 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
761}
762
Joerg Roedel815b33f2011-04-06 17:26:49 +0200763static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200764{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200765 WARN_ON(address & 0x7ULL);
766
Joerg Roedelded46732011-04-06 10:53:48 +0200767 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200768 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
769 cmd->data[1] = upper_32_bits(__pa(address));
770 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200771 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
772}
773
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200774static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
775{
776 memset(cmd, 0, sizeof(*cmd));
777 cmd->data[0] = devid;
778 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
779}
780
Joerg Roedel11b64022011-04-06 11:49:28 +0200781static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
782 size_t size, u16 domid, int pde)
783{
784 u64 pages;
785 int s;
786
787 pages = iommu_num_pages(address, size, PAGE_SIZE);
788 s = 0;
789
790 if (pages > 1) {
791 /*
792 * If we have to flush more than one page, flush all
793 * TLB entries for this domain
794 */
795 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
796 s = 1;
797 }
798
799 address &= PAGE_MASK;
800
801 memset(cmd, 0, sizeof(*cmd));
802 cmd->data[1] |= domid;
803 cmd->data[2] = lower_32_bits(address);
804 cmd->data[3] = upper_32_bits(address);
805 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
806 if (s) /* size bit - we flush more than one 4kb page */
807 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
808 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
809 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
810}
811
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200812static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
813 u64 address, size_t size)
814{
815 u64 pages;
816 int s;
817
818 pages = iommu_num_pages(address, size, PAGE_SIZE);
819 s = 0;
820
821 if (pages > 1) {
822 /*
823 * If we have to flush more than one page, flush all
824 * TLB entries for this domain
825 */
826 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
827 s = 1;
828 }
829
830 address &= PAGE_MASK;
831
832 memset(cmd, 0, sizeof(*cmd));
833 cmd->data[0] = devid;
834 cmd->data[0] |= (qdep & 0xff) << 24;
835 cmd->data[1] = devid;
836 cmd->data[2] = lower_32_bits(address);
837 cmd->data[3] = upper_32_bits(address);
838 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
839 if (s)
840 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
841}
842
Joerg Roedel22e266c2011-11-21 15:59:08 +0100843static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
844 u64 address, bool size)
845{
846 memset(cmd, 0, sizeof(*cmd));
847
848 address &= ~(0xfffULL);
849
850 cmd->data[0] = pasid & PASID_MASK;
851 cmd->data[1] = domid;
852 cmd->data[2] = lower_32_bits(address);
853 cmd->data[3] = upper_32_bits(address);
854 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
855 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
856 if (size)
857 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
858 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
859}
860
861static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
862 int qdep, u64 address, bool size)
863{
864 memset(cmd, 0, sizeof(*cmd));
865
866 address &= ~(0xfffULL);
867
868 cmd->data[0] = devid;
869 cmd->data[0] |= (pasid & 0xff) << 16;
870 cmd->data[0] |= (qdep & 0xff) << 24;
871 cmd->data[1] = devid;
872 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
873 cmd->data[2] = lower_32_bits(address);
874 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
875 cmd->data[3] = upper_32_bits(address);
876 if (size)
877 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
878 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
879}
880
Joerg Roedelc99afa22011-11-21 18:19:25 +0100881static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
882 int status, int tag, bool gn)
883{
884 memset(cmd, 0, sizeof(*cmd));
885
886 cmd->data[0] = devid;
887 if (gn) {
888 cmd->data[1] = pasid & PASID_MASK;
889 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
890 }
891 cmd->data[3] = tag & 0x1ff;
892 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
893
894 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
895}
896
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200897static void build_inv_all(struct iommu_cmd *cmd)
898{
899 memset(cmd, 0, sizeof(*cmd));
900 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200901}
902
Joerg Roedel431b2a22008-07-11 17:14:22 +0200903/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200904 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200905 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200906 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200907static int iommu_queue_command_sync(struct amd_iommu *iommu,
908 struct iommu_cmd *cmd,
909 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200910{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200911 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200912 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200913
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200914 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100915
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200916again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200917 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200918
919 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
920 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
921 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
922 left = (head - next_tail) % iommu->cmd_buf_size;
923
924 if (left <= 2) {
925 struct iommu_cmd sync_cmd;
926 volatile u64 sem = 0;
927 int ret;
928
929 build_completion_wait(&sync_cmd, (u64)&sem);
930 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
931
932 spin_unlock_irqrestore(&iommu->lock, flags);
933
934 if ((ret = wait_on_sem(&sem)) != 0)
935 return ret;
936
937 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200938 }
939
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200940 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200941
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200942 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200943 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200944
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200945 spin_unlock_irqrestore(&iommu->lock, flags);
946
Joerg Roedel815b33f2011-04-06 17:26:49 +0200947 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100948}
949
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200950static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
951{
952 return iommu_queue_command_sync(iommu, cmd, true);
953}
954
Joerg Roedel8d201962008-12-02 20:34:41 +0100955/*
956 * This function queues a completion wait command into the command
957 * buffer of an IOMMU
958 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100959static int iommu_completion_wait(struct amd_iommu *iommu)
960{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200961 struct iommu_cmd cmd;
962 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200963 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100964
965 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200966 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100967
Joerg Roedel815b33f2011-04-06 17:26:49 +0200968 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100969
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200970 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100971 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200972 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100973
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200974 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200975}
976
Joerg Roedeld8c13082011-04-06 18:51:26 +0200977static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200978{
979 struct iommu_cmd cmd;
980
Joerg Roedeld8c13082011-04-06 18:51:26 +0200981 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200982
Joerg Roedeld8c13082011-04-06 18:51:26 +0200983 return iommu_queue_command(iommu, &cmd);
984}
985
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200986static void iommu_flush_dte_all(struct amd_iommu *iommu)
987{
988 u32 devid;
989
990 for (devid = 0; devid <= 0xffff; ++devid)
991 iommu_flush_dte(iommu, devid);
992
993 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200994}
995
996/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200997 * This function uses heavy locking and may disable irqs for some time. But
998 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200999 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001000static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001001{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001002 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001003
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001004 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1005 struct iommu_cmd cmd;
1006 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1007 dom_id, 1);
1008 iommu_queue_command(iommu, &cmd);
1009 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001010
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001011 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001012}
1013
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001014static void iommu_flush_all(struct amd_iommu *iommu)
1015{
1016 struct iommu_cmd cmd;
1017
1018 build_inv_all(&cmd);
1019
1020 iommu_queue_command(iommu, &cmd);
1021 iommu_completion_wait(iommu);
1022}
1023
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001024void iommu_flush_all_caches(struct amd_iommu *iommu)
1025{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001026 if (iommu_feature(iommu, FEATURE_IA)) {
1027 iommu_flush_all(iommu);
1028 } else {
1029 iommu_flush_dte_all(iommu);
1030 iommu_flush_tlb_all(iommu);
1031 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001032}
1033
Joerg Roedel431b2a22008-07-11 17:14:22 +02001034/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001035 * Command send function for flushing on-device TLB
1036 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001037static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1038 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001039{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001040 struct amd_iommu *iommu;
1041 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001042 int qdep;
1043
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001044 qdep = dev_data->ats.qdep;
1045 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001046
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001047 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001048
1049 return iommu_queue_command(iommu, &cmd);
1050}
1051
1052/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001053 * Command send function for invalidating a device table entry
1054 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001055static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001056{
1057 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001058 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001059
Joerg Roedel6c542042011-06-09 17:07:31 +02001060 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001061
Joerg Roedelf62dda62011-06-09 12:55:35 +02001062 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001063 if (ret)
1064 return ret;
1065
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001066 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001067 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001068
1069 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001070}
1071
Joerg Roedel431b2a22008-07-11 17:14:22 +02001072/*
1073 * TLB invalidation function which is called from the mapping functions.
1074 * It invalidates a single PTE if the range to flush is within a single
1075 * page. Otherwise it flushes the whole TLB of the IOMMU.
1076 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001077static void __domain_flush_pages(struct protection_domain *domain,
1078 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001079{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001080 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001081 struct iommu_cmd cmd;
1082 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001083
Joerg Roedel11b64022011-04-06 11:49:28 +02001084 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001085
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001086 for (i = 0; i < amd_iommus_present; ++i) {
1087 if (!domain->dev_iommu[i])
1088 continue;
1089
1090 /*
1091 * Devices of this domain are behind this IOMMU
1092 * We need a TLB flush
1093 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001094 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001095 }
1096
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001097 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001098
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001099 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001100 continue;
1101
Joerg Roedel6c542042011-06-09 17:07:31 +02001102 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001103 }
1104
Joerg Roedel11b64022011-04-06 11:49:28 +02001105 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001106}
1107
Joerg Roedel17b124b2011-04-06 18:01:35 +02001108static void domain_flush_pages(struct protection_domain *domain,
1109 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001110{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001111 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001112}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001113
Joerg Roedel1c655772008-09-04 18:40:05 +02001114/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001115static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001116{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001117 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001118}
1119
Chris Wright42a49f92009-06-15 15:42:00 +02001120/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001121static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001122{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001123 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1124}
1125
1126static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001127{
1128 int i;
1129
1130 for (i = 0; i < amd_iommus_present; ++i) {
1131 if (!domain->dev_iommu[i])
1132 continue;
1133
1134 /*
1135 * Devices of this domain are behind this IOMMU
1136 * We need to wait for completion of all commands.
1137 */
1138 iommu_completion_wait(amd_iommus[i]);
1139 }
1140}
1141
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001142
Joerg Roedel43f49602008-12-02 21:01:12 +01001143/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001144 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001145 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001146static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001147{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001148 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001149
1150 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001151 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001152}
1153
Joerg Roedel431b2a22008-07-11 17:14:22 +02001154/****************************************************************************
1155 *
1156 * The functions below are used the create the page table mappings for
1157 * unity mapped regions.
1158 *
1159 ****************************************************************************/
1160
1161/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001162 * This function is used to add another level to an IO page table. Adding
1163 * another level increases the size of the address space by 9 bits to a size up
1164 * to 64 bits.
1165 */
1166static bool increase_address_space(struct protection_domain *domain,
1167 gfp_t gfp)
1168{
1169 u64 *pte;
1170
1171 if (domain->mode == PAGE_MODE_6_LEVEL)
1172 /* address space already 64 bit large */
1173 return false;
1174
1175 pte = (void *)get_zeroed_page(gfp);
1176 if (!pte)
1177 return false;
1178
1179 *pte = PM_LEVEL_PDE(domain->mode,
1180 virt_to_phys(domain->pt_root));
1181 domain->pt_root = pte;
1182 domain->mode += 1;
1183 domain->updated = true;
1184
1185 return true;
1186}
1187
1188static u64 *alloc_pte(struct protection_domain *domain,
1189 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001190 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001191 u64 **pte_page,
1192 gfp_t gfp)
1193{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001194 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001195 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001196
1197 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001198
1199 while (address > PM_LEVEL_SIZE(domain->mode))
1200 increase_address_space(domain, gfp);
1201
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001202 level = domain->mode - 1;
1203 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1204 address = PAGE_SIZE_ALIGN(address, page_size);
1205 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001206
1207 while (level > end_lvl) {
1208 if (!IOMMU_PTE_PRESENT(*pte)) {
1209 page = (u64 *)get_zeroed_page(gfp);
1210 if (!page)
1211 return NULL;
1212 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1213 }
1214
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001215 /* No level skipping support yet */
1216 if (PM_PTE_LEVEL(*pte) != level)
1217 return NULL;
1218
Joerg Roedel308973d2009-11-24 17:43:32 +01001219 level -= 1;
1220
1221 pte = IOMMU_PTE_PAGE(*pte);
1222
1223 if (pte_page && level == end_lvl)
1224 *pte_page = pte;
1225
1226 pte = &pte[PM_LEVEL_INDEX(level, address)];
1227 }
1228
1229 return pte;
1230}
1231
1232/*
1233 * This function checks if there is a PTE for a given dma address. If
1234 * there is one, it returns the pointer to it.
1235 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001236static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001237{
1238 int level;
1239 u64 *pte;
1240
Joerg Roedel24cd7722010-01-19 17:27:39 +01001241 if (address > PM_LEVEL_SIZE(domain->mode))
1242 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001243
Joerg Roedel24cd7722010-01-19 17:27:39 +01001244 level = domain->mode - 1;
1245 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1246
1247 while (level > 0) {
1248
1249 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001250 if (!IOMMU_PTE_PRESENT(*pte))
1251 return NULL;
1252
Joerg Roedel24cd7722010-01-19 17:27:39 +01001253 /* Large PTE */
1254 if (PM_PTE_LEVEL(*pte) == 0x07) {
1255 unsigned long pte_mask, __pte;
1256
1257 /*
1258 * If we have a series of large PTEs, make
1259 * sure to return a pointer to the first one.
1260 */
1261 pte_mask = PTE_PAGE_SIZE(*pte);
1262 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1263 __pte = ((unsigned long)pte) & pte_mask;
1264
1265 return (u64 *)__pte;
1266 }
1267
1268 /* No level skipping support yet */
1269 if (PM_PTE_LEVEL(*pte) != level)
1270 return NULL;
1271
Joerg Roedel308973d2009-11-24 17:43:32 +01001272 level -= 1;
1273
Joerg Roedel24cd7722010-01-19 17:27:39 +01001274 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001275 pte = IOMMU_PTE_PAGE(*pte);
1276 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001277 }
1278
1279 return pte;
1280}
1281
1282/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001283 * Generic mapping functions. It maps a physical address into a DMA
1284 * address space. It allocates the page table pages if necessary.
1285 * In the future it can be extended to a generic mapping function
1286 * supporting all features of AMD IOMMU page tables like level skipping
1287 * and full 64 bit address spaces.
1288 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001289static int iommu_map_page(struct protection_domain *dom,
1290 unsigned long bus_addr,
1291 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001292 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001293 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001294{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001295 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001296 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001297
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001298 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001299 return -EINVAL;
1300
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001301 bus_addr = PAGE_ALIGN(bus_addr);
1302 phys_addr = PAGE_ALIGN(phys_addr);
1303 count = PAGE_SIZE_PTE_COUNT(page_size);
1304 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001305
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001306 for (i = 0; i < count; ++i)
1307 if (IOMMU_PTE_PRESENT(pte[i]))
1308 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001309
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001310 if (page_size > PAGE_SIZE) {
1311 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1312 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1313 } else
1314 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1315
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001316 if (prot & IOMMU_PROT_IR)
1317 __pte |= IOMMU_PTE_IR;
1318 if (prot & IOMMU_PROT_IW)
1319 __pte |= IOMMU_PTE_IW;
1320
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001321 for (i = 0; i < count; ++i)
1322 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001323
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001324 update_domain(dom);
1325
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001326 return 0;
1327}
1328
Joerg Roedel24cd7722010-01-19 17:27:39 +01001329static unsigned long iommu_unmap_page(struct protection_domain *dom,
1330 unsigned long bus_addr,
1331 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001332{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001333 unsigned long long unmap_size, unmapped;
1334 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001335
Joerg Roedel24cd7722010-01-19 17:27:39 +01001336 BUG_ON(!is_power_of_2(page_size));
1337
1338 unmapped = 0;
1339
1340 while (unmapped < page_size) {
1341
1342 pte = fetch_pte(dom, bus_addr);
1343
1344 if (!pte) {
1345 /*
1346 * No PTE for this address
1347 * move forward in 4kb steps
1348 */
1349 unmap_size = PAGE_SIZE;
1350 } else if (PM_PTE_LEVEL(*pte) == 0) {
1351 /* 4kb PTE found for this address */
1352 unmap_size = PAGE_SIZE;
1353 *pte = 0ULL;
1354 } else {
1355 int count, i;
1356
1357 /* Large PTE found which maps this address */
1358 unmap_size = PTE_PAGE_SIZE(*pte);
1359 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1360 for (i = 0; i < count; i++)
1361 pte[i] = 0ULL;
1362 }
1363
1364 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1365 unmapped += unmap_size;
1366 }
1367
1368 BUG_ON(!is_power_of_2(unmapped));
1369
1370 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001371}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001372
Joerg Roedel431b2a22008-07-11 17:14:22 +02001373/*
1374 * This function checks if a specific unity mapping entry is needed for
1375 * this specific IOMMU.
1376 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001377static int iommu_for_unity_map(struct amd_iommu *iommu,
1378 struct unity_map_entry *entry)
1379{
1380 u16 bdf, i;
1381
1382 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1383 bdf = amd_iommu_alias_table[i];
1384 if (amd_iommu_rlookup_table[bdf] == iommu)
1385 return 1;
1386 }
1387
1388 return 0;
1389}
1390
Joerg Roedel431b2a22008-07-11 17:14:22 +02001391/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001392 * This function actually applies the mapping to the page table of the
1393 * dma_ops domain.
1394 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001395static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1396 struct unity_map_entry *e)
1397{
1398 u64 addr;
1399 int ret;
1400
1401 for (addr = e->address_start; addr < e->address_end;
1402 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001403 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001404 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001405 if (ret)
1406 return ret;
1407 /*
1408 * if unity mapping is in aperture range mark the page
1409 * as allocated in the aperture
1410 */
1411 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001412 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001413 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001414 }
1415
1416 return 0;
1417}
1418
Joerg Roedel431b2a22008-07-11 17:14:22 +02001419/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001420 * Init the unity mappings for a specific IOMMU in the system
1421 *
1422 * Basically iterates over all unity mapping entries and applies them to
1423 * the default domain DMA of that IOMMU if necessary.
1424 */
1425static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1426{
1427 struct unity_map_entry *entry;
1428 int ret;
1429
1430 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1431 if (!iommu_for_unity_map(iommu, entry))
1432 continue;
1433 ret = dma_ops_unity_map(iommu->default_dom, entry);
1434 if (ret)
1435 return ret;
1436 }
1437
1438 return 0;
1439}
1440
1441/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001442 * Inits the unity mappings required for a specific device
1443 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001444static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1445 u16 devid)
1446{
1447 struct unity_map_entry *e;
1448 int ret;
1449
1450 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1451 if (!(devid >= e->devid_start && devid <= e->devid_end))
1452 continue;
1453 ret = dma_ops_unity_map(dma_dom, e);
1454 if (ret)
1455 return ret;
1456 }
1457
1458 return 0;
1459}
1460
Joerg Roedel431b2a22008-07-11 17:14:22 +02001461/****************************************************************************
1462 *
1463 * The next functions belong to the address allocator for the dma_ops
1464 * interface functions. They work like the allocators in the other IOMMU
1465 * drivers. Its basically a bitmap which marks the allocated pages in
1466 * the aperture. Maybe it could be enhanced in the future to a more
1467 * efficient allocator.
1468 *
1469 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001470
Joerg Roedel431b2a22008-07-11 17:14:22 +02001471/*
Joerg Roedel384de722009-05-15 12:30:05 +02001472 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001473 *
1474 * called with domain->lock held
1475 */
Joerg Roedel384de722009-05-15 12:30:05 +02001476
Joerg Roedel9cabe892009-05-18 16:38:55 +02001477/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001478 * Used to reserve address ranges in the aperture (e.g. for exclusion
1479 * ranges.
1480 */
1481static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1482 unsigned long start_page,
1483 unsigned int pages)
1484{
1485 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1486
1487 if (start_page + pages > last_page)
1488 pages = last_page - start_page;
1489
1490 for (i = start_page; i < start_page + pages; ++i) {
1491 int index = i / APERTURE_RANGE_PAGES;
1492 int page = i % APERTURE_RANGE_PAGES;
1493 __set_bit(page, dom->aperture[index]->bitmap);
1494 }
1495}
1496
1497/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001498 * This function is used to add a new aperture range to an existing
1499 * aperture in case of dma_ops domain allocation or address allocation
1500 * failure.
1501 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001502static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001503 bool populate, gfp_t gfp)
1504{
1505 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001506 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001507 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001508
Joerg Roedelf5e97052009-05-22 12:31:53 +02001509#ifdef CONFIG_IOMMU_STRESS
1510 populate = false;
1511#endif
1512
Joerg Roedel9cabe892009-05-18 16:38:55 +02001513 if (index >= APERTURE_MAX_RANGES)
1514 return -ENOMEM;
1515
1516 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1517 if (!dma_dom->aperture[index])
1518 return -ENOMEM;
1519
1520 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1521 if (!dma_dom->aperture[index]->bitmap)
1522 goto out_free;
1523
1524 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1525
1526 if (populate) {
1527 unsigned long address = dma_dom->aperture_size;
1528 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1529 u64 *pte, *pte_page;
1530
1531 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001532 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001533 &pte_page, gfp);
1534 if (!pte)
1535 goto out_free;
1536
1537 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1538
1539 address += APERTURE_RANGE_SIZE / 64;
1540 }
1541 }
1542
Joerg Roedel17f5b562011-07-06 17:14:44 +02001543 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001544 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1545
Joerg Roedel17f5b562011-07-06 17:14:44 +02001546 /* Reserve address range used for MSI messages */
1547 if (old_size < MSI_ADDR_BASE_LO &&
1548 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1549 unsigned long spage;
1550 int pages;
1551
1552 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1553 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1554
1555 dma_ops_reserve_addresses(dma_dom, spage, pages);
1556 }
1557
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001558 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001559 for_each_iommu(iommu) {
1560 if (iommu->exclusion_start &&
1561 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1562 && iommu->exclusion_start < dma_dom->aperture_size) {
1563 unsigned long startpage;
1564 int pages = iommu_num_pages(iommu->exclusion_start,
1565 iommu->exclusion_length,
1566 PAGE_SIZE);
1567 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1568 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1569 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001570 }
1571
1572 /*
1573 * Check for areas already mapped as present in the new aperture
1574 * range and mark those pages as reserved in the allocator. Such
1575 * mappings may already exist as a result of requested unity
1576 * mappings for devices.
1577 */
1578 for (i = dma_dom->aperture[index]->offset;
1579 i < dma_dom->aperture_size;
1580 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001581 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001582 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1583 continue;
1584
Joerg Roedelfcd08612011-10-11 17:41:32 +02001585 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001586 }
1587
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001588 update_domain(&dma_dom->domain);
1589
Joerg Roedel9cabe892009-05-18 16:38:55 +02001590 return 0;
1591
1592out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001593 update_domain(&dma_dom->domain);
1594
Joerg Roedel9cabe892009-05-18 16:38:55 +02001595 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1596
1597 kfree(dma_dom->aperture[index]);
1598 dma_dom->aperture[index] = NULL;
1599
1600 return -ENOMEM;
1601}
1602
Joerg Roedel384de722009-05-15 12:30:05 +02001603static unsigned long dma_ops_area_alloc(struct device *dev,
1604 struct dma_ops_domain *dom,
1605 unsigned int pages,
1606 unsigned long align_mask,
1607 u64 dma_mask,
1608 unsigned long start)
1609{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001610 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001611 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1612 int i = start >> APERTURE_RANGE_SHIFT;
1613 unsigned long boundary_size;
1614 unsigned long address = -1;
1615 unsigned long limit;
1616
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001617 next_bit >>= PAGE_SHIFT;
1618
Joerg Roedel384de722009-05-15 12:30:05 +02001619 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1620 PAGE_SIZE) >> PAGE_SHIFT;
1621
1622 for (;i < max_index; ++i) {
1623 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1624
1625 if (dom->aperture[i]->offset >= dma_mask)
1626 break;
1627
1628 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1629 dma_mask >> PAGE_SHIFT);
1630
1631 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1632 limit, next_bit, pages, 0,
1633 boundary_size, align_mask);
1634 if (address != -1) {
1635 address = dom->aperture[i]->offset +
1636 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001637 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001638 break;
1639 }
1640
1641 next_bit = 0;
1642 }
1643
1644 return address;
1645}
1646
Joerg Roedeld3086442008-06-26 21:27:57 +02001647static unsigned long dma_ops_alloc_addresses(struct device *dev,
1648 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001649 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001650 unsigned long align_mask,
1651 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001652{
Joerg Roedeld3086442008-06-26 21:27:57 +02001653 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001654
Joerg Roedelfe16f082009-05-22 12:27:53 +02001655#ifdef CONFIG_IOMMU_STRESS
1656 dom->next_address = 0;
1657 dom->need_flush = true;
1658#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001659
Joerg Roedel384de722009-05-15 12:30:05 +02001660 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001661 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001662
Joerg Roedel1c655772008-09-04 18:40:05 +02001663 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001664 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001665 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1666 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001667 dom->need_flush = true;
1668 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001669
Joerg Roedel384de722009-05-15 12:30:05 +02001670 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001671 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001672
1673 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1674
1675 return address;
1676}
1677
Joerg Roedel431b2a22008-07-11 17:14:22 +02001678/*
1679 * The address free function.
1680 *
1681 * called with domain->lock held
1682 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001683static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1684 unsigned long address,
1685 unsigned int pages)
1686{
Joerg Roedel384de722009-05-15 12:30:05 +02001687 unsigned i = address >> APERTURE_RANGE_SHIFT;
1688 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001689
Joerg Roedel384de722009-05-15 12:30:05 +02001690 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1691
Joerg Roedel47bccd62009-05-22 12:40:54 +02001692#ifdef CONFIG_IOMMU_STRESS
1693 if (i < 4)
1694 return;
1695#endif
1696
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001697 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001698 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001699
1700 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001701
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001702 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001703
Joerg Roedeld3086442008-06-26 21:27:57 +02001704}
1705
Joerg Roedel431b2a22008-07-11 17:14:22 +02001706/****************************************************************************
1707 *
1708 * The next functions belong to the domain allocation. A domain is
1709 * allocated for every IOMMU as the default domain. If device isolation
1710 * is enabled, every device get its own domain. The most important thing
1711 * about domains is the page table mapping the DMA address space they
1712 * contain.
1713 *
1714 ****************************************************************************/
1715
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001716/*
1717 * This function adds a protection domain to the global protection domain list
1718 */
1719static void add_domain_to_list(struct protection_domain *domain)
1720{
1721 unsigned long flags;
1722
1723 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1724 list_add(&domain->list, &amd_iommu_pd_list);
1725 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1726}
1727
1728/*
1729 * This function removes a protection domain to the global
1730 * protection domain list
1731 */
1732static void del_domain_from_list(struct protection_domain *domain)
1733{
1734 unsigned long flags;
1735
1736 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1737 list_del(&domain->list);
1738 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1739}
1740
Joerg Roedelec487d12008-06-26 21:27:58 +02001741static u16 domain_id_alloc(void)
1742{
1743 unsigned long flags;
1744 int id;
1745
1746 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1747 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1748 BUG_ON(id == 0);
1749 if (id > 0 && id < MAX_DOMAIN_ID)
1750 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1751 else
1752 id = 0;
1753 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1754
1755 return id;
1756}
1757
Joerg Roedela2acfb72008-12-02 18:28:53 +01001758static void domain_id_free(int id)
1759{
1760 unsigned long flags;
1761
1762 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1763 if (id > 0 && id < MAX_DOMAIN_ID)
1764 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1765 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1766}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001767
Joerg Roedel86db2e52008-12-02 18:20:21 +01001768static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001769{
1770 int i, j;
1771 u64 *p1, *p2, *p3;
1772
Joerg Roedel86db2e52008-12-02 18:20:21 +01001773 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001774
1775 if (!p1)
1776 return;
1777
1778 for (i = 0; i < 512; ++i) {
1779 if (!IOMMU_PTE_PRESENT(p1[i]))
1780 continue;
1781
1782 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001783 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001784 if (!IOMMU_PTE_PRESENT(p2[j]))
1785 continue;
1786 p3 = IOMMU_PTE_PAGE(p2[j]);
1787 free_page((unsigned long)p3);
1788 }
1789
1790 free_page((unsigned long)p2);
1791 }
1792
1793 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001794
1795 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001796}
1797
Joerg Roedelb16137b2011-11-21 16:50:23 +01001798static void free_gcr3_tbl_level1(u64 *tbl)
1799{
1800 u64 *ptr;
1801 int i;
1802
1803 for (i = 0; i < 512; ++i) {
1804 if (!(tbl[i] & GCR3_VALID))
1805 continue;
1806
1807 ptr = __va(tbl[i] & PAGE_MASK);
1808
1809 free_page((unsigned long)ptr);
1810 }
1811}
1812
1813static void free_gcr3_tbl_level2(u64 *tbl)
1814{
1815 u64 *ptr;
1816 int i;
1817
1818 for (i = 0; i < 512; ++i) {
1819 if (!(tbl[i] & GCR3_VALID))
1820 continue;
1821
1822 ptr = __va(tbl[i] & PAGE_MASK);
1823
1824 free_gcr3_tbl_level1(ptr);
1825 }
1826}
1827
Joerg Roedel52815b72011-11-17 17:24:28 +01001828static void free_gcr3_table(struct protection_domain *domain)
1829{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001830 if (domain->glx == 2)
1831 free_gcr3_tbl_level2(domain->gcr3_tbl);
1832 else if (domain->glx == 1)
1833 free_gcr3_tbl_level1(domain->gcr3_tbl);
1834 else if (domain->glx != 0)
1835 BUG();
1836
Joerg Roedel52815b72011-11-17 17:24:28 +01001837 free_page((unsigned long)domain->gcr3_tbl);
1838}
1839
Joerg Roedel431b2a22008-07-11 17:14:22 +02001840/*
1841 * Free a domain, only used if something went wrong in the
1842 * allocation path and we need to free an already allocated page table
1843 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001844static void dma_ops_domain_free(struct dma_ops_domain *dom)
1845{
Joerg Roedel384de722009-05-15 12:30:05 +02001846 int i;
1847
Joerg Roedelec487d12008-06-26 21:27:58 +02001848 if (!dom)
1849 return;
1850
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001851 del_domain_from_list(&dom->domain);
1852
Joerg Roedel86db2e52008-12-02 18:20:21 +01001853 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001854
Joerg Roedel384de722009-05-15 12:30:05 +02001855 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1856 if (!dom->aperture[i])
1857 continue;
1858 free_page((unsigned long)dom->aperture[i]->bitmap);
1859 kfree(dom->aperture[i]);
1860 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001861
1862 kfree(dom);
1863}
1864
Joerg Roedel431b2a22008-07-11 17:14:22 +02001865/*
1866 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001867 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001868 * structures required for the dma_ops interface
1869 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001870static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001871{
1872 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001873
1874 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1875 if (!dma_dom)
1876 return NULL;
1877
1878 spin_lock_init(&dma_dom->domain.lock);
1879
1880 dma_dom->domain.id = domain_id_alloc();
1881 if (dma_dom->domain.id == 0)
1882 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001883 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001884 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001885 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001886 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001887 dma_dom->domain.priv = dma_dom;
1888 if (!dma_dom->domain.pt_root)
1889 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001890
Joerg Roedel1c655772008-09-04 18:40:05 +02001891 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001892 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001893
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001894 add_domain_to_list(&dma_dom->domain);
1895
Joerg Roedel576175c2009-11-23 19:08:46 +01001896 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001897 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001898
Joerg Roedel431b2a22008-07-11 17:14:22 +02001899 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001900 * mark the first page as allocated so we never return 0 as
1901 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001902 */
Joerg Roedel384de722009-05-15 12:30:05 +02001903 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001904 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001905
Joerg Roedelec487d12008-06-26 21:27:58 +02001906
1907 return dma_dom;
1908
1909free_dma_dom:
1910 dma_ops_domain_free(dma_dom);
1911
1912 return NULL;
1913}
1914
Joerg Roedel431b2a22008-07-11 17:14:22 +02001915/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001916 * little helper function to check whether a given protection domain is a
1917 * dma_ops domain
1918 */
1919static bool dma_ops_domain(struct protection_domain *domain)
1920{
1921 return domain->flags & PD_DMA_OPS_MASK;
1922}
1923
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001924static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001925{
Joerg Roedel132bd682011-11-17 14:18:46 +01001926 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001927 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001928
Joerg Roedel132bd682011-11-17 14:18:46 +01001929 if (domain->mode != PAGE_MODE_NONE)
1930 pte_root = virt_to_phys(domain->pt_root);
1931
Joerg Roedel38ddf412008-09-11 10:38:32 +02001932 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1933 << DEV_ENTRY_MODE_SHIFT;
1934 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001935
Joerg Roedelee6c2862011-11-09 12:06:03 +01001936 flags = amd_iommu_dev_table[devid].data[1];
1937
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001938 if (ats)
1939 flags |= DTE_FLAG_IOTLB;
1940
Joerg Roedel52815b72011-11-17 17:24:28 +01001941 if (domain->flags & PD_IOMMUV2_MASK) {
1942 u64 gcr3 = __pa(domain->gcr3_tbl);
1943 u64 glx = domain->glx;
1944 u64 tmp;
1945
1946 pte_root |= DTE_FLAG_GV;
1947 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1948
1949 /* First mask out possible old values for GCR3 table */
1950 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1951 flags &= ~tmp;
1952
1953 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1954 flags &= ~tmp;
1955
1956 /* Encode GCR3 table into DTE */
1957 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1958 pte_root |= tmp;
1959
1960 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1961 flags |= tmp;
1962
1963 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1964 flags |= tmp;
1965 }
1966
Joerg Roedelee6c2862011-11-09 12:06:03 +01001967 flags &= ~(0xffffUL);
1968 flags |= domain->id;
1969
1970 amd_iommu_dev_table[devid].data[1] = flags;
1971 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001972}
1973
Joerg Roedel15898bb2009-11-24 15:39:42 +01001974static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001975{
Joerg Roedel355bf552008-12-08 12:02:41 +01001976 /* remove entry from the device table seen by the hardware */
1977 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1978 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01001979
Joerg Roedelc5cca142009-10-09 18:31:20 +02001980 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001981}
1982
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001983static void do_attach(struct iommu_dev_data *dev_data,
1984 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001985{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001986 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001987 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001988
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001989 iommu = amd_iommu_rlookup_table[dev_data->devid];
1990 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001991
1992 /* Update data structures */
1993 dev_data->domain = domain;
1994 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001995 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001996
1997 /* Do reference counting */
1998 domain->dev_iommu[iommu->index] += 1;
1999 domain->dev_cnt += 1;
2000
2001 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002002 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002003}
2004
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002005static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002006{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002007 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002008
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002009 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002010
Joerg Roedelc4596112009-11-20 14:57:32 +01002011 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002012 dev_data->domain->dev_iommu[iommu->index] -= 1;
2013 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002014
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002015 /* Update data structures */
2016 dev_data->domain = NULL;
2017 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002018 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002019
2020 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002021 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002022}
2023
2024/*
2025 * If a device is not yet associated with a domain, this function does
2026 * assigns it visible for the hardware
2027 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002028static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002029 struct protection_domain *domain)
2030{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002031 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002032
Joerg Roedel15898bb2009-11-24 15:39:42 +01002033 /* lock domain */
2034 spin_lock(&domain->lock);
2035
Joerg Roedel71f77582011-06-09 19:03:15 +02002036 if (dev_data->alias_data != NULL) {
2037 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002038
Joerg Roedel2b02b092011-06-09 17:48:39 +02002039 /* Some sanity checks */
2040 ret = -EBUSY;
2041 if (alias_data->domain != NULL &&
2042 alias_data->domain != domain)
2043 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002044
Joerg Roedel2b02b092011-06-09 17:48:39 +02002045 if (dev_data->domain != NULL &&
2046 dev_data->domain != domain)
2047 goto out_unlock;
2048
2049 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002050 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002051 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002052
2053 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002054 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002055
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002056 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002057 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002058
Joerg Roedel24100052009-11-25 15:59:57 +01002059 atomic_inc(&dev_data->bind);
2060
Julia Lawall84fe6c12010-05-27 12:31:51 +02002061 ret = 0;
2062
2063out_unlock:
2064
Joerg Roedel355bf552008-12-08 12:02:41 +01002065 /* ready */
2066 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002067
Julia Lawall84fe6c12010-05-27 12:31:51 +02002068 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002069}
2070
Joerg Roedel52815b72011-11-17 17:24:28 +01002071
2072static void pdev_iommuv2_disable(struct pci_dev *pdev)
2073{
2074 pci_disable_ats(pdev);
2075 pci_disable_pri(pdev);
2076 pci_disable_pasid(pdev);
2077}
2078
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002079/* FIXME: Change generic reset-function to do the same */
2080static int pri_reset_while_enabled(struct pci_dev *pdev)
2081{
2082 u16 control;
2083 int pos;
2084
Joerg Roedel46277b72011-12-07 14:34:02 +01002085 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002086 if (!pos)
2087 return -EINVAL;
2088
Joerg Roedel46277b72011-12-07 14:34:02 +01002089 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2090 control |= PCI_PRI_CTRL_RESET;
2091 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002092
2093 return 0;
2094}
2095
Joerg Roedel52815b72011-11-17 17:24:28 +01002096static int pdev_iommuv2_enable(struct pci_dev *pdev)
2097{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002098 bool reset_enable;
2099 int reqs, ret;
2100
2101 /* FIXME: Hardcode number of outstanding requests for now */
2102 reqs = 32;
2103 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2104 reqs = 1;
2105 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002106
2107 /* Only allow access to user-accessible pages */
2108 ret = pci_enable_pasid(pdev, 0);
2109 if (ret)
2110 goto out_err;
2111
2112 /* First reset the PRI state of the device */
2113 ret = pci_reset_pri(pdev);
2114 if (ret)
2115 goto out_err;
2116
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002117 /* Enable PRI */
2118 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002119 if (ret)
2120 goto out_err;
2121
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002122 if (reset_enable) {
2123 ret = pri_reset_while_enabled(pdev);
2124 if (ret)
2125 goto out_err;
2126 }
2127
Joerg Roedel52815b72011-11-17 17:24:28 +01002128 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2129 if (ret)
2130 goto out_err;
2131
2132 return 0;
2133
2134out_err:
2135 pci_disable_pri(pdev);
2136 pci_disable_pasid(pdev);
2137
2138 return ret;
2139}
2140
Joerg Roedelc99afa22011-11-21 18:19:25 +01002141/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002142#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002143
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002144static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002145{
Joerg Roedela3b93122012-04-12 12:49:26 +02002146 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002147 int pos;
2148
Joerg Roedel46277b72011-12-07 14:34:02 +01002149 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002150 if (!pos)
2151 return false;
2152
Joerg Roedela3b93122012-04-12 12:49:26 +02002153 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002154
Joerg Roedela3b93122012-04-12 12:49:26 +02002155 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002156}
2157
Joerg Roedel15898bb2009-11-24 15:39:42 +01002158/*
2159 * If a device is not yet associated with a domain, this function does
2160 * assigns it visible for the hardware
2161 */
2162static int attach_device(struct device *dev,
2163 struct protection_domain *domain)
2164{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002165 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002166 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002167 unsigned long flags;
2168 int ret;
2169
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002170 dev_data = get_dev_data(dev);
2171
Joerg Roedel52815b72011-11-17 17:24:28 +01002172 if (domain->flags & PD_IOMMUV2_MASK) {
2173 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2174 return -EINVAL;
2175
2176 if (pdev_iommuv2_enable(pdev) != 0)
2177 return -EINVAL;
2178
2179 dev_data->ats.enabled = true;
2180 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002181 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002182 } else if (amd_iommu_iotlb_sup &&
2183 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002184 dev_data->ats.enabled = true;
2185 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2186 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002187
Joerg Roedel15898bb2009-11-24 15:39:42 +01002188 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002189 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002190 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2191
2192 /*
2193 * We might boot into a crash-kernel here. The crashed kernel
2194 * left the caches in the IOMMU dirty. So we have to flush
2195 * here to evict all dirty stuff.
2196 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002197 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002198
2199 return ret;
2200}
2201
2202/*
2203 * Removes a device from a protection domain (unlocked)
2204 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002205static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002206{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002207 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002208 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002209
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002210 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002211
Joerg Roedel2ca76272010-01-22 16:45:31 +01002212 domain = dev_data->domain;
2213
2214 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002215
Joerg Roedel71f77582011-06-09 19:03:15 +02002216 if (dev_data->alias_data != NULL) {
2217 struct iommu_dev_data *alias_data = dev_data->alias_data;
2218
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002219 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002220 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002221 }
2222
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002223 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002224 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002225
Joerg Roedel2ca76272010-01-22 16:45:31 +01002226 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002227
Joerg Roedel21129f72009-09-01 11:59:42 +02002228 /*
2229 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002230 * passthrough domain if it is detached from any other domain.
2231 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002232 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002233 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002234 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002235 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002236}
2237
2238/*
2239 * Removes a device from a protection domain (with devtable_lock held)
2240 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002241static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002242{
Joerg Roedel52815b72011-11-17 17:24:28 +01002243 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002244 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002245 unsigned long flags;
2246
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002247 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002248 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002249
Joerg Roedel355bf552008-12-08 12:02:41 +01002250 /* lock device table */
2251 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002252 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002253 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002254
Joerg Roedel52815b72011-11-17 17:24:28 +01002255 if (domain->flags & PD_IOMMUV2_MASK)
2256 pdev_iommuv2_disable(to_pci_dev(dev));
2257 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002258 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002259
2260 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002261}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002262
Joerg Roedel15898bb2009-11-24 15:39:42 +01002263/*
2264 * Find out the protection domain structure for a given PCI device. This
2265 * will give us the pointer to the page table root for example.
2266 */
2267static struct protection_domain *domain_for_device(struct device *dev)
2268{
Joerg Roedel71f77582011-06-09 19:03:15 +02002269 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002270 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002271 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002272
Joerg Roedel657cbb62009-11-23 15:26:46 +01002273 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002274
Joerg Roedel2b02b092011-06-09 17:48:39 +02002275 if (dev_data->domain)
2276 return dev_data->domain;
2277
Joerg Roedel71f77582011-06-09 19:03:15 +02002278 if (dev_data->alias_data != NULL) {
2279 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002280
2281 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2282 if (alias_data->domain != NULL) {
2283 __attach_device(dev_data, alias_data->domain);
2284 dom = alias_data->domain;
2285 }
2286 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002287 }
2288
Joerg Roedel15898bb2009-11-24 15:39:42 +01002289 return dom;
2290}
2291
Joerg Roedele275a2a2008-12-10 18:27:25 +01002292static int device_change_notifier(struct notifier_block *nb,
2293 unsigned long action, void *data)
2294{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002295 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002296 struct protection_domain *domain;
2297 struct iommu_dev_data *dev_data;
2298 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002299 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002300 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002301 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002302
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002303 if (!check_device(dev))
2304 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002305
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002306 devid = get_device_id(dev);
2307 iommu = amd_iommu_rlookup_table[devid];
2308 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002309
2310 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002311 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002312
2313 domain = domain_for_device(dev);
2314
Joerg Roedele275a2a2008-12-10 18:27:25 +01002315 if (!domain)
2316 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002317 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002318 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002319 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002320 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002321 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002322
2323 iommu_init_device(dev);
2324
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002325 /*
2326 * dev_data is still NULL and
2327 * got initialized in iommu_init_device
2328 */
2329 dev_data = get_dev_data(dev);
2330
2331 if (iommu_pass_through || dev_data->iommu_v2) {
2332 dev_data->passthrough = true;
2333 attach_device(dev, pt_domain);
2334 break;
2335 }
2336
Joerg Roedel657cbb62009-11-23 15:26:46 +01002337 domain = domain_for_device(dev);
2338
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002339 /* allocate a protection domain if a device is added */
2340 dma_domain = find_protection_domain(devid);
2341 if (dma_domain)
2342 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002343 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002344 if (!dma_domain)
2345 goto out;
2346 dma_domain->target_dev = devid;
2347
2348 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2349 list_add_tail(&dma_domain->list, &iommu_pd_list);
2350 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2351
Joerg Roedelac1534a2012-06-21 14:52:40 +02002352 dev_data = get_dev_data(dev);
2353
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002354 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002355
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002356 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002357 case BUS_NOTIFY_DEL_DEVICE:
2358
2359 iommu_uninit_device(dev);
2360
Joerg Roedele275a2a2008-12-10 18:27:25 +01002361 default:
2362 goto out;
2363 }
2364
Joerg Roedele275a2a2008-12-10 18:27:25 +01002365 iommu_completion_wait(iommu);
2366
2367out:
2368 return 0;
2369}
2370
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302371static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002372 .notifier_call = device_change_notifier,
2373};
Joerg Roedel355bf552008-12-08 12:02:41 +01002374
Joerg Roedel8638c492009-12-10 11:12:25 +01002375void amd_iommu_init_notifier(void)
2376{
2377 bus_register_notifier(&pci_bus_type, &device_nb);
2378}
2379
Joerg Roedel431b2a22008-07-11 17:14:22 +02002380/*****************************************************************************
2381 *
2382 * The next functions belong to the dma_ops mapping/unmapping code.
2383 *
2384 *****************************************************************************/
2385
2386/*
2387 * In the dma_ops path we only have the struct device. This function
2388 * finds the corresponding IOMMU, the protection domain and the
2389 * requestor id for a given device.
2390 * If the device is not yet associated with a domain this is also done
2391 * in this function.
2392 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002393static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002394{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002395 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002396 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002397 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002398
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002399 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002400 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002401
Joerg Roedel94f6d192009-11-24 16:40:02 +01002402 domain = domain_for_device(dev);
2403 if (domain != NULL && !dma_ops_domain(domain))
2404 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002405
Joerg Roedel94f6d192009-11-24 16:40:02 +01002406 if (domain != NULL)
2407 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002408
Joerg Roedel15898bb2009-11-24 15:39:42 +01002409 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002410 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002411 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002412 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2413 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002414 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002415 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002416
Joerg Roedel94f6d192009-11-24 16:40:02 +01002417 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002418}
2419
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002420static void update_device_table(struct protection_domain *domain)
2421{
Joerg Roedel492667d2009-11-27 13:25:47 +01002422 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002423
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002424 list_for_each_entry(dev_data, &domain->dev_list, list)
2425 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002426}
2427
2428static void update_domain(struct protection_domain *domain)
2429{
2430 if (!domain->updated)
2431 return;
2432
2433 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002434
2435 domain_flush_devices(domain);
2436 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002437
2438 domain->updated = false;
2439}
2440
Joerg Roedel431b2a22008-07-11 17:14:22 +02002441/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002442 * This function fetches the PTE for a given address in the aperture
2443 */
2444static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2445 unsigned long address)
2446{
Joerg Roedel384de722009-05-15 12:30:05 +02002447 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002448 u64 *pte, *pte_page;
2449
Joerg Roedel384de722009-05-15 12:30:05 +02002450 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2451 if (!aperture)
2452 return NULL;
2453
2454 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002455 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002456 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002457 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002458 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2459 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002460 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002461
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002462 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002463
2464 return pte;
2465}
2466
2467/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002468 * This is the generic map function. It maps one 4kb page at paddr to
2469 * the given address in the DMA address space for the domain.
2470 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002471static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002472 unsigned long address,
2473 phys_addr_t paddr,
2474 int direction)
2475{
2476 u64 *pte, __pte;
2477
2478 WARN_ON(address > dom->aperture_size);
2479
2480 paddr &= PAGE_MASK;
2481
Joerg Roedel8bda3092009-05-12 12:02:46 +02002482 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002483 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002484 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002485
2486 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2487
2488 if (direction == DMA_TO_DEVICE)
2489 __pte |= IOMMU_PTE_IR;
2490 else if (direction == DMA_FROM_DEVICE)
2491 __pte |= IOMMU_PTE_IW;
2492 else if (direction == DMA_BIDIRECTIONAL)
2493 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2494
2495 WARN_ON(*pte);
2496
2497 *pte = __pte;
2498
2499 return (dma_addr_t)address;
2500}
2501
Joerg Roedel431b2a22008-07-11 17:14:22 +02002502/*
2503 * The generic unmapping function for on page in the DMA address space.
2504 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002505static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002506 unsigned long address)
2507{
Joerg Roedel384de722009-05-15 12:30:05 +02002508 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002509 u64 *pte;
2510
2511 if (address >= dom->aperture_size)
2512 return;
2513
Joerg Roedel384de722009-05-15 12:30:05 +02002514 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2515 if (!aperture)
2516 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002517
Joerg Roedel384de722009-05-15 12:30:05 +02002518 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2519 if (!pte)
2520 return;
2521
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002522 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002523
2524 WARN_ON(!*pte);
2525
2526 *pte = 0ULL;
2527}
2528
Joerg Roedel431b2a22008-07-11 17:14:22 +02002529/*
2530 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002531 * contiguous memory region into DMA address space. It is used by all
2532 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002533 * Must be called with the domain lock held.
2534 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002535static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002536 struct dma_ops_domain *dma_dom,
2537 phys_addr_t paddr,
2538 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002539 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002540 bool align,
2541 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002542{
2543 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002544 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002545 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002546 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002547 int i;
2548
Joerg Roedele3c449f2008-10-15 22:02:11 -07002549 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002550 paddr &= PAGE_MASK;
2551
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002552 INC_STATS_COUNTER(total_map_requests);
2553
Joerg Roedelc1858972008-12-12 15:42:39 +01002554 if (pages > 1)
2555 INC_STATS_COUNTER(cross_page);
2556
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002557 if (align)
2558 align_mask = (1UL << get_order(size)) - 1;
2559
Joerg Roedel11b83882009-05-19 10:23:15 +02002560retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002561 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2562 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002563 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002564 /*
2565 * setting next_address here will let the address
2566 * allocator only scan the new allocated range in the
2567 * first run. This is a small optimization.
2568 */
2569 dma_dom->next_address = dma_dom->aperture_size;
2570
Joerg Roedel576175c2009-11-23 19:08:46 +01002571 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002572 goto out;
2573
2574 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002575 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002576 * allocation again
2577 */
2578 goto retry;
2579 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002580
2581 start = address;
2582 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002583 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002584 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002585 goto out_unmap;
2586
Joerg Roedelcb76c322008-06-26 21:28:00 +02002587 paddr += PAGE_SIZE;
2588 start += PAGE_SIZE;
2589 }
2590 address += offset;
2591
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002592 ADD_STATS_COUNTER(alloced_io_mem, size);
2593
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002594 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002595 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002596 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002597 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002598 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002599
Joerg Roedelcb76c322008-06-26 21:28:00 +02002600out:
2601 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002602
2603out_unmap:
2604
2605 for (--i; i >= 0; --i) {
2606 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002607 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002608 }
2609
2610 dma_ops_free_addresses(dma_dom, address, pages);
2611
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002612 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002613}
2614
Joerg Roedel431b2a22008-07-11 17:14:22 +02002615/*
2616 * Does the reverse of the __map_single function. Must be called with
2617 * the domain lock held too
2618 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002619static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002620 dma_addr_t dma_addr,
2621 size_t size,
2622 int dir)
2623{
Joerg Roedel04e04632010-09-23 16:12:48 +02002624 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002625 dma_addr_t i, start;
2626 unsigned int pages;
2627
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002628 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002629 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002630 return;
2631
Joerg Roedel04e04632010-09-23 16:12:48 +02002632 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002633 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002634 dma_addr &= PAGE_MASK;
2635 start = dma_addr;
2636
2637 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002638 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002639 start += PAGE_SIZE;
2640 }
2641
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002642 SUB_STATS_COUNTER(alloced_io_mem, size);
2643
Joerg Roedelcb76c322008-06-26 21:28:00 +02002644 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002645
Joerg Roedel80be3082008-11-06 14:59:05 +01002646 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002647 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002648 dma_dom->need_flush = false;
2649 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002650}
2651
Joerg Roedel431b2a22008-07-11 17:14:22 +02002652/*
2653 * The exported map_single function for dma_ops.
2654 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002655static dma_addr_t map_page(struct device *dev, struct page *page,
2656 unsigned long offset, size_t size,
2657 enum dma_data_direction dir,
2658 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002659{
2660 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002661 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002662 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002663 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002664 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002665
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002666 INC_STATS_COUNTER(cnt_map_single);
2667
Joerg Roedel94f6d192009-11-24 16:40:02 +01002668 domain = get_domain(dev);
2669 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002670 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002671 else if (IS_ERR(domain))
2672 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002673
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002674 dma_mask = *dev->dma_mask;
2675
Joerg Roedel4da70b92008-06-26 21:28:01 +02002676 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002677
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002678 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002679 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002680 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002681 goto out;
2682
Joerg Roedel17b124b2011-04-06 18:01:35 +02002683 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002684
2685out:
2686 spin_unlock_irqrestore(&domain->lock, flags);
2687
2688 return addr;
2689}
2690
Joerg Roedel431b2a22008-07-11 17:14:22 +02002691/*
2692 * The exported unmap_single function for dma_ops.
2693 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002694static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2695 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002696{
2697 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002698 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002699
Joerg Roedel146a6912008-12-12 15:07:12 +01002700 INC_STATS_COUNTER(cnt_unmap_single);
2701
Joerg Roedel94f6d192009-11-24 16:40:02 +01002702 domain = get_domain(dev);
2703 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002704 return;
2705
Joerg Roedel4da70b92008-06-26 21:28:01 +02002706 spin_lock_irqsave(&domain->lock, flags);
2707
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002708 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002709
Joerg Roedel17b124b2011-04-06 18:01:35 +02002710 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002711
2712 spin_unlock_irqrestore(&domain->lock, flags);
2713}
2714
Joerg Roedel431b2a22008-07-11 17:14:22 +02002715/*
2716 * This is a special map_sg function which is used if we should map a
2717 * device which is not handled by an AMD IOMMU in the system.
2718 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002719static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2720 int nelems, int dir)
2721{
2722 struct scatterlist *s;
2723 int i;
2724
2725 for_each_sg(sglist, s, nelems, i) {
2726 s->dma_address = (dma_addr_t)sg_phys(s);
2727 s->dma_length = s->length;
2728 }
2729
2730 return nelems;
2731}
2732
Joerg Roedel431b2a22008-07-11 17:14:22 +02002733/*
2734 * The exported map_sg function for dma_ops (handles scatter-gather
2735 * lists).
2736 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002737static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002738 int nelems, enum dma_data_direction dir,
2739 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002740{
2741 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002742 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002743 int i;
2744 struct scatterlist *s;
2745 phys_addr_t paddr;
2746 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002747 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002748
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002749 INC_STATS_COUNTER(cnt_map_sg);
2750
Joerg Roedel94f6d192009-11-24 16:40:02 +01002751 domain = get_domain(dev);
2752 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002753 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002754 else if (IS_ERR(domain))
2755 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002756
Joerg Roedel832a90c2008-09-18 15:54:23 +02002757 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002758
Joerg Roedel65b050a2008-06-26 21:28:02 +02002759 spin_lock_irqsave(&domain->lock, flags);
2760
2761 for_each_sg(sglist, s, nelems, i) {
2762 paddr = sg_phys(s);
2763
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002764 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002765 paddr, s->length, dir, false,
2766 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002767
2768 if (s->dma_address) {
2769 s->dma_length = s->length;
2770 mapped_elems++;
2771 } else
2772 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002773 }
2774
Joerg Roedel17b124b2011-04-06 18:01:35 +02002775 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002776
2777out:
2778 spin_unlock_irqrestore(&domain->lock, flags);
2779
2780 return mapped_elems;
2781unmap:
2782 for_each_sg(sglist, s, mapped_elems, i) {
2783 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002784 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002785 s->dma_length, dir);
2786 s->dma_address = s->dma_length = 0;
2787 }
2788
2789 mapped_elems = 0;
2790
2791 goto out;
2792}
2793
Joerg Roedel431b2a22008-07-11 17:14:22 +02002794/*
2795 * The exported map_sg function for dma_ops (handles scatter-gather
2796 * lists).
2797 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002798static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002799 int nelems, enum dma_data_direction dir,
2800 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002801{
2802 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002803 struct protection_domain *domain;
2804 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002805 int i;
2806
Joerg Roedel55877a62008-12-12 15:12:14 +01002807 INC_STATS_COUNTER(cnt_unmap_sg);
2808
Joerg Roedel94f6d192009-11-24 16:40:02 +01002809 domain = get_domain(dev);
2810 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002811 return;
2812
Joerg Roedel65b050a2008-06-26 21:28:02 +02002813 spin_lock_irqsave(&domain->lock, flags);
2814
2815 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002816 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002817 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002818 s->dma_address = s->dma_length = 0;
2819 }
2820
Joerg Roedel17b124b2011-04-06 18:01:35 +02002821 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002822
2823 spin_unlock_irqrestore(&domain->lock, flags);
2824}
2825
Joerg Roedel431b2a22008-07-11 17:14:22 +02002826/*
2827 * The exported alloc_coherent function for dma_ops.
2828 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002829static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002830 dma_addr_t *dma_addr, gfp_t flag,
2831 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002832{
2833 unsigned long flags;
2834 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002835 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002836 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002837 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002838
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002839 INC_STATS_COUNTER(cnt_alloc_coherent);
2840
Joerg Roedel94f6d192009-11-24 16:40:02 +01002841 domain = get_domain(dev);
2842 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002843 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2844 *dma_addr = __pa(virt_addr);
2845 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002846 } else if (IS_ERR(domain))
2847 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002848
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002849 dma_mask = dev->coherent_dma_mask;
2850 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2851 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002852
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002853 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2854 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302855 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002856
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002857 paddr = virt_to_phys(virt_addr);
2858
Joerg Roedel832a90c2008-09-18 15:54:23 +02002859 if (!dma_mask)
2860 dma_mask = *dev->dma_mask;
2861
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002862 spin_lock_irqsave(&domain->lock, flags);
2863
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002864 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002865 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002866
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002867 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002868 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002869 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002870 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002871
Joerg Roedel17b124b2011-04-06 18:01:35 +02002872 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002873
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002874 spin_unlock_irqrestore(&domain->lock, flags);
2875
2876 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002877
2878out_free:
2879
2880 free_pages((unsigned long)virt_addr, get_order(size));
2881
2882 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002883}
2884
Joerg Roedel431b2a22008-07-11 17:14:22 +02002885/*
2886 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002887 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002888static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002889 void *virt_addr, dma_addr_t dma_addr,
2890 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002891{
2892 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002893 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002894
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002895 INC_STATS_COUNTER(cnt_free_coherent);
2896
Joerg Roedel94f6d192009-11-24 16:40:02 +01002897 domain = get_domain(dev);
2898 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002899 goto free_mem;
2900
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002901 spin_lock_irqsave(&domain->lock, flags);
2902
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002903 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002904
Joerg Roedel17b124b2011-04-06 18:01:35 +02002905 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002906
2907 spin_unlock_irqrestore(&domain->lock, flags);
2908
2909free_mem:
2910 free_pages((unsigned long)virt_addr, get_order(size));
2911}
2912
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002913/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002914 * This function is called by the DMA layer to find out if we can handle a
2915 * particular device. It is part of the dma_ops.
2916 */
2917static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2918{
Joerg Roedel420aef82009-11-23 16:14:57 +01002919 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002920}
2921
2922/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002923 * The function for pre-allocating protection domains.
2924 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002925 * If the driver core informs the DMA layer if a driver grabs a device
2926 * we don't need to preallocate the protection domains anymore.
2927 * For now we have to.
2928 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01002929static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002930{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002931 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002932 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002933 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002934 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002935
Chris Wrightd18c69d2010-04-02 18:27:55 -07002936 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002937
2938 /* Do we handle this device? */
2939 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002940 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002941
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002942 dev_data = get_dev_data(&dev->dev);
2943 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2944 /* Make sure passthrough domain is allocated */
2945 alloc_passthrough_domain();
2946 dev_data->passthrough = true;
2947 attach_device(&dev->dev, pt_domain);
2948 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2949 dev_name(&dev->dev));
2950 }
2951
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002952 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002953 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002954 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002955
2956 devid = get_device_id(&dev->dev);
2957
Joerg Roedel87a64d52009-11-24 17:26:43 +01002958 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002959 if (!dma_dom)
2960 continue;
2961 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002962 dma_dom->target_dev = devid;
2963
Joerg Roedel15898bb2009-11-24 15:39:42 +01002964 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002965
Joerg Roedelbd60b732008-09-11 10:24:48 +02002966 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002967 }
2968}
2969
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002970static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002971 .alloc = alloc_coherent,
2972 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002973 .map_page = map_page,
2974 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002975 .map_sg = map_sg,
2976 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002977 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002978};
2979
Joerg Roedel27c21272011-05-30 15:56:24 +02002980static unsigned device_dma_ops_init(void)
2981{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002982 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002983 struct pci_dev *pdev = NULL;
2984 unsigned unhandled = 0;
2985
2986 for_each_pci_dev(pdev) {
2987 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01002988
2989 iommu_ignore_device(&pdev->dev);
2990
Joerg Roedel27c21272011-05-30 15:56:24 +02002991 unhandled += 1;
2992 continue;
2993 }
2994
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002995 dev_data = get_dev_data(&pdev->dev);
2996
2997 if (!dev_data->passthrough)
2998 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2999 else
3000 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003001 }
3002
3003 return unhandled;
3004}
3005
Joerg Roedel431b2a22008-07-11 17:14:22 +02003006/*
3007 * The function which clues the AMD IOMMU driver into dma_ops.
3008 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003009
3010void __init amd_iommu_init_api(void)
3011{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003012 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003013}
3014
Joerg Roedel6631ee92008-06-26 21:28:05 +02003015int __init amd_iommu_init_dma_ops(void)
3016{
3017 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003018 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003019
Joerg Roedel431b2a22008-07-11 17:14:22 +02003020 /*
3021 * first allocate a default protection domain for every IOMMU we
3022 * found in the system. Devices not assigned to any other
3023 * protection domain will be assigned to the default one.
3024 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003025 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003026 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003027 if (iommu->default_dom == NULL)
3028 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003029 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003030 ret = iommu_init_unity_mappings(iommu);
3031 if (ret)
3032 goto free_domains;
3033 }
3034
Joerg Roedel431b2a22008-07-11 17:14:22 +02003035 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003036 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003037 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003038 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003039
3040 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003041 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003042
Joerg Roedel431b2a22008-07-11 17:14:22 +02003043 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003044 unhandled = device_dma_ops_init();
3045 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3046 /* There are unhandled devices - initialize swiotlb for them */
3047 swiotlb = 1;
3048 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003049
Joerg Roedel7f265082008-12-12 13:50:21 +01003050 amd_iommu_stats_init();
3051
Joerg Roedel62410ee2012-06-12 16:42:43 +02003052 if (amd_iommu_unmap_flush)
3053 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3054 else
3055 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3056
Joerg Roedel6631ee92008-06-26 21:28:05 +02003057 return 0;
3058
3059free_domains:
3060
Joerg Roedel3bd22172009-05-04 15:06:20 +02003061 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003062 if (iommu->default_dom)
3063 dma_ops_domain_free(iommu->default_dom);
3064 }
3065
3066 return ret;
3067}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003068
3069/*****************************************************************************
3070 *
3071 * The following functions belong to the exported interface of AMD IOMMU
3072 *
3073 * This interface allows access to lower level functions of the IOMMU
3074 * like protection domain handling and assignement of devices to domains
3075 * which is not possible with the dma_ops interface.
3076 *
3077 *****************************************************************************/
3078
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003079static void cleanup_domain(struct protection_domain *domain)
3080{
Joerg Roedel492667d2009-11-27 13:25:47 +01003081 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003082 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003083
3084 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3085
Joerg Roedel492667d2009-11-27 13:25:47 +01003086 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003087 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003088 atomic_set(&dev_data->bind, 0);
3089 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003090
3091 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3092}
3093
Joerg Roedel26508152009-08-26 16:52:40 +02003094static void protection_domain_free(struct protection_domain *domain)
3095{
3096 if (!domain)
3097 return;
3098
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003099 del_domain_from_list(domain);
3100
Joerg Roedel26508152009-08-26 16:52:40 +02003101 if (domain->id)
3102 domain_id_free(domain->id);
3103
3104 kfree(domain);
3105}
3106
3107static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003108{
3109 struct protection_domain *domain;
3110
3111 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3112 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003113 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003114
3115 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003116 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003117 domain->id = domain_id_alloc();
3118 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003119 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003120 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003121
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003122 add_domain_to_list(domain);
3123
Joerg Roedel26508152009-08-26 16:52:40 +02003124 return domain;
3125
3126out_err:
3127 kfree(domain);
3128
3129 return NULL;
3130}
3131
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003132static int __init alloc_passthrough_domain(void)
3133{
3134 if (pt_domain != NULL)
3135 return 0;
3136
3137 /* allocate passthrough domain */
3138 pt_domain = protection_domain_alloc();
3139 if (!pt_domain)
3140 return -ENOMEM;
3141
3142 pt_domain->mode = PAGE_MODE_NONE;
3143
3144 return 0;
3145}
Joerg Roedel26508152009-08-26 16:52:40 +02003146static int amd_iommu_domain_init(struct iommu_domain *dom)
3147{
3148 struct protection_domain *domain;
3149
3150 domain = protection_domain_alloc();
3151 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003152 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003153
3154 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003155 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3156 if (!domain->pt_root)
3157 goto out_free;
3158
Joerg Roedelf3572db2011-11-23 12:36:25 +01003159 domain->iommu_domain = dom;
3160
Joerg Roedelc156e342008-12-02 18:13:27 +01003161 dom->priv = domain;
3162
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003163 dom->geometry.aperture_start = 0;
3164 dom->geometry.aperture_end = ~0ULL;
3165 dom->geometry.force_aperture = true;
3166
Joerg Roedelc156e342008-12-02 18:13:27 +01003167 return 0;
3168
3169out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003170 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003171
3172 return -ENOMEM;
3173}
3174
Joerg Roedel98383fc2008-12-02 18:34:12 +01003175static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3176{
3177 struct protection_domain *domain = dom->priv;
3178
3179 if (!domain)
3180 return;
3181
3182 if (domain->dev_cnt > 0)
3183 cleanup_domain(domain);
3184
3185 BUG_ON(domain->dev_cnt != 0);
3186
Joerg Roedel132bd682011-11-17 14:18:46 +01003187 if (domain->mode != PAGE_MODE_NONE)
3188 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003189
Joerg Roedel52815b72011-11-17 17:24:28 +01003190 if (domain->flags & PD_IOMMUV2_MASK)
3191 free_gcr3_table(domain);
3192
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003193 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003194
3195 dom->priv = NULL;
3196}
3197
Joerg Roedel684f2882008-12-08 12:07:44 +01003198static void amd_iommu_detach_device(struct iommu_domain *dom,
3199 struct device *dev)
3200{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003201 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003202 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003203 u16 devid;
3204
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003205 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003206 return;
3207
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003208 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003209
Joerg Roedel657cbb62009-11-23 15:26:46 +01003210 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003211 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003212
3213 iommu = amd_iommu_rlookup_table[devid];
3214 if (!iommu)
3215 return;
3216
Joerg Roedel684f2882008-12-08 12:07:44 +01003217 iommu_completion_wait(iommu);
3218}
3219
Joerg Roedel01106062008-12-02 19:34:11 +01003220static int amd_iommu_attach_device(struct iommu_domain *dom,
3221 struct device *dev)
3222{
3223 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003224 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003225 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003226 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003227
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003228 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003229 return -EINVAL;
3230
Joerg Roedel657cbb62009-11-23 15:26:46 +01003231 dev_data = dev->archdata.iommu;
3232
Joerg Roedelf62dda62011-06-09 12:55:35 +02003233 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003234 if (!iommu)
3235 return -EINVAL;
3236
Joerg Roedel657cbb62009-11-23 15:26:46 +01003237 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003238 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003239
Joerg Roedel15898bb2009-11-24 15:39:42 +01003240 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003241
3242 iommu_completion_wait(iommu);
3243
Joerg Roedel15898bb2009-11-24 15:39:42 +01003244 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003245}
3246
Joerg Roedel468e2362010-01-21 16:37:36 +01003247static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003248 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003249{
3250 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003251 int prot = 0;
3252 int ret;
3253
Joerg Roedel132bd682011-11-17 14:18:46 +01003254 if (domain->mode == PAGE_MODE_NONE)
3255 return -EINVAL;
3256
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003257 if (iommu_prot & IOMMU_READ)
3258 prot |= IOMMU_PROT_IR;
3259 if (iommu_prot & IOMMU_WRITE)
3260 prot |= IOMMU_PROT_IW;
3261
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003262 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003263 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003264 mutex_unlock(&domain->api_lock);
3265
Joerg Roedel795e74f2010-05-11 17:40:57 +02003266 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003267}
3268
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003269static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3270 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003271{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003272 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003273 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003274
Joerg Roedel132bd682011-11-17 14:18:46 +01003275 if (domain->mode == PAGE_MODE_NONE)
3276 return -EINVAL;
3277
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003278 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003279 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003280 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003281
Joerg Roedel17b124b2011-04-06 18:01:35 +02003282 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003283
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003284 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003285}
3286
Joerg Roedel645c4c82008-12-02 20:05:50 +01003287static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3288 unsigned long iova)
3289{
3290 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003291 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003292 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003293 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003294
Joerg Roedel132bd682011-11-17 14:18:46 +01003295 if (domain->mode == PAGE_MODE_NONE)
3296 return iova;
3297
Joerg Roedel24cd7722010-01-19 17:27:39 +01003298 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003299
Joerg Roedela6d41a42009-09-02 17:08:55 +02003300 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003301 return 0;
3302
Joerg Roedelf03152b2010-01-21 16:15:24 +01003303 if (PM_PTE_LEVEL(*pte) == 0)
3304 offset_mask = PAGE_SIZE - 1;
3305 else
3306 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3307
3308 __pte = *pte & PM_ADDR_MASK;
3309 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003310
3311 return paddr;
3312}
3313
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003314static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3315 unsigned long cap)
3316{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003317 switch (cap) {
3318 case IOMMU_CAP_CACHE_COHERENCY:
3319 return 1;
3320 }
3321
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003322 return 0;
3323}
3324
Joerg Roedel26961ef2008-12-03 17:00:17 +01003325static struct iommu_ops amd_iommu_ops = {
3326 .domain_init = amd_iommu_domain_init,
3327 .domain_destroy = amd_iommu_domain_destroy,
3328 .attach_dev = amd_iommu_attach_device,
3329 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003330 .map = amd_iommu_map,
3331 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003332 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003333 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003334 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003335};
3336
Joerg Roedel0feae532009-08-26 15:26:30 +02003337/*****************************************************************************
3338 *
3339 * The next functions do a basic initialization of IOMMU for pass through
3340 * mode
3341 *
3342 * In passthrough mode the IOMMU is initialized and enabled but not used for
3343 * DMA-API translation.
3344 *
3345 *****************************************************************************/
3346
3347int __init amd_iommu_init_passthrough(void)
3348{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003349 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003350 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003351 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003352 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003353 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003354
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003355 ret = alloc_passthrough_domain();
3356 if (ret)
3357 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003358
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003359 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003360 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003361 continue;
3362
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003363 dev_data = get_dev_data(&dev->dev);
3364 dev_data->passthrough = true;
3365
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003366 devid = get_device_id(&dev->dev);
3367
Joerg Roedel15898bb2009-11-24 15:39:42 +01003368 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003369 if (!iommu)
3370 continue;
3371
Joerg Roedel15898bb2009-11-24 15:39:42 +01003372 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003373 }
3374
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003375 amd_iommu_stats_init();
3376
Joerg Roedel0feae532009-08-26 15:26:30 +02003377 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3378
3379 return 0;
3380}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003381
3382/* IOMMUv2 specific functions */
3383int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3384{
3385 return atomic_notifier_chain_register(&ppr_notifier, nb);
3386}
3387EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3388
3389int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3390{
3391 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3392}
3393EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003394
3395void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3396{
3397 struct protection_domain *domain = dom->priv;
3398 unsigned long flags;
3399
3400 spin_lock_irqsave(&domain->lock, flags);
3401
3402 /* Update data structure */
3403 domain->mode = PAGE_MODE_NONE;
3404 domain->updated = true;
3405
3406 /* Make changes visible to IOMMUs */
3407 update_domain(domain);
3408
3409 /* Page-table is not visible to IOMMU anymore, so free it */
3410 free_pagetable(domain);
3411
3412 spin_unlock_irqrestore(&domain->lock, flags);
3413}
3414EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003415
3416int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3417{
3418 struct protection_domain *domain = dom->priv;
3419 unsigned long flags;
3420 int levels, ret;
3421
3422 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3423 return -EINVAL;
3424
3425 /* Number of GCR3 table levels required */
3426 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3427 levels += 1;
3428
3429 if (levels > amd_iommu_max_glx_val)
3430 return -EINVAL;
3431
3432 spin_lock_irqsave(&domain->lock, flags);
3433
3434 /*
3435 * Save us all sanity checks whether devices already in the
3436 * domain support IOMMUv2. Just force that the domain has no
3437 * devices attached when it is switched into IOMMUv2 mode.
3438 */
3439 ret = -EBUSY;
3440 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3441 goto out;
3442
3443 ret = -ENOMEM;
3444 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3445 if (domain->gcr3_tbl == NULL)
3446 goto out;
3447
3448 domain->glx = levels;
3449 domain->flags |= PD_IOMMUV2_MASK;
3450 domain->updated = true;
3451
3452 update_domain(domain);
3453
3454 ret = 0;
3455
3456out:
3457 spin_unlock_irqrestore(&domain->lock, flags);
3458
3459 return ret;
3460}
3461EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003462
3463static int __flush_pasid(struct protection_domain *domain, int pasid,
3464 u64 address, bool size)
3465{
3466 struct iommu_dev_data *dev_data;
3467 struct iommu_cmd cmd;
3468 int i, ret;
3469
3470 if (!(domain->flags & PD_IOMMUV2_MASK))
3471 return -EINVAL;
3472
3473 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3474
3475 /*
3476 * IOMMU TLB needs to be flushed before Device TLB to
3477 * prevent device TLB refill from IOMMU TLB
3478 */
3479 for (i = 0; i < amd_iommus_present; ++i) {
3480 if (domain->dev_iommu[i] == 0)
3481 continue;
3482
3483 ret = iommu_queue_command(amd_iommus[i], &cmd);
3484 if (ret != 0)
3485 goto out;
3486 }
3487
3488 /* Wait until IOMMU TLB flushes are complete */
3489 domain_flush_complete(domain);
3490
3491 /* Now flush device TLBs */
3492 list_for_each_entry(dev_data, &domain->dev_list, list) {
3493 struct amd_iommu *iommu;
3494 int qdep;
3495
3496 BUG_ON(!dev_data->ats.enabled);
3497
3498 qdep = dev_data->ats.qdep;
3499 iommu = amd_iommu_rlookup_table[dev_data->devid];
3500
3501 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3502 qdep, address, size);
3503
3504 ret = iommu_queue_command(iommu, &cmd);
3505 if (ret != 0)
3506 goto out;
3507 }
3508
3509 /* Wait until all device TLBs are flushed */
3510 domain_flush_complete(domain);
3511
3512 ret = 0;
3513
3514out:
3515
3516 return ret;
3517}
3518
3519static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3520 u64 address)
3521{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003522 INC_STATS_COUNTER(invalidate_iotlb);
3523
Joerg Roedel22e266c2011-11-21 15:59:08 +01003524 return __flush_pasid(domain, pasid, address, false);
3525}
3526
3527int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3528 u64 address)
3529{
3530 struct protection_domain *domain = dom->priv;
3531 unsigned long flags;
3532 int ret;
3533
3534 spin_lock_irqsave(&domain->lock, flags);
3535 ret = __amd_iommu_flush_page(domain, pasid, address);
3536 spin_unlock_irqrestore(&domain->lock, flags);
3537
3538 return ret;
3539}
3540EXPORT_SYMBOL(amd_iommu_flush_page);
3541
3542static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3543{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003544 INC_STATS_COUNTER(invalidate_iotlb_all);
3545
Joerg Roedel22e266c2011-11-21 15:59:08 +01003546 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3547 true);
3548}
3549
3550int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3551{
3552 struct protection_domain *domain = dom->priv;
3553 unsigned long flags;
3554 int ret;
3555
3556 spin_lock_irqsave(&domain->lock, flags);
3557 ret = __amd_iommu_flush_tlb(domain, pasid);
3558 spin_unlock_irqrestore(&domain->lock, flags);
3559
3560 return ret;
3561}
3562EXPORT_SYMBOL(amd_iommu_flush_tlb);
3563
Joerg Roedelb16137b2011-11-21 16:50:23 +01003564static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3565{
3566 int index;
3567 u64 *pte;
3568
3569 while (true) {
3570
3571 index = (pasid >> (9 * level)) & 0x1ff;
3572 pte = &root[index];
3573
3574 if (level == 0)
3575 break;
3576
3577 if (!(*pte & GCR3_VALID)) {
3578 if (!alloc)
3579 return NULL;
3580
3581 root = (void *)get_zeroed_page(GFP_ATOMIC);
3582 if (root == NULL)
3583 return NULL;
3584
3585 *pte = __pa(root) | GCR3_VALID;
3586 }
3587
3588 root = __va(*pte & PAGE_MASK);
3589
3590 level -= 1;
3591 }
3592
3593 return pte;
3594}
3595
3596static int __set_gcr3(struct protection_domain *domain, int pasid,
3597 unsigned long cr3)
3598{
3599 u64 *pte;
3600
3601 if (domain->mode != PAGE_MODE_NONE)
3602 return -EINVAL;
3603
3604 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3605 if (pte == NULL)
3606 return -ENOMEM;
3607
3608 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3609
3610 return __amd_iommu_flush_tlb(domain, pasid);
3611}
3612
3613static int __clear_gcr3(struct protection_domain *domain, int pasid)
3614{
3615 u64 *pte;
3616
3617 if (domain->mode != PAGE_MODE_NONE)
3618 return -EINVAL;
3619
3620 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3621 if (pte == NULL)
3622 return 0;
3623
3624 *pte = 0;
3625
3626 return __amd_iommu_flush_tlb(domain, pasid);
3627}
3628
3629int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3630 unsigned long cr3)
3631{
3632 struct protection_domain *domain = dom->priv;
3633 unsigned long flags;
3634 int ret;
3635
3636 spin_lock_irqsave(&domain->lock, flags);
3637 ret = __set_gcr3(domain, pasid, cr3);
3638 spin_unlock_irqrestore(&domain->lock, flags);
3639
3640 return ret;
3641}
3642EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3643
3644int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3645{
3646 struct protection_domain *domain = dom->priv;
3647 unsigned long flags;
3648 int ret;
3649
3650 spin_lock_irqsave(&domain->lock, flags);
3651 ret = __clear_gcr3(domain, pasid);
3652 spin_unlock_irqrestore(&domain->lock, flags);
3653
3654 return ret;
3655}
3656EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003657
3658int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3659 int status, int tag)
3660{
3661 struct iommu_dev_data *dev_data;
3662 struct amd_iommu *iommu;
3663 struct iommu_cmd cmd;
3664
Joerg Roedel399be2f2011-12-01 16:53:47 +01003665 INC_STATS_COUNTER(complete_ppr);
3666
Joerg Roedelc99afa22011-11-21 18:19:25 +01003667 dev_data = get_dev_data(&pdev->dev);
3668 iommu = amd_iommu_rlookup_table[dev_data->devid];
3669
3670 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3671 tag, dev_data->pri_tlp);
3672
3673 return iommu_queue_command(iommu, &cmd);
3674}
3675EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003676
3677struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3678{
3679 struct protection_domain *domain;
3680
3681 domain = get_domain(&pdev->dev);
3682 if (IS_ERR(domain))
3683 return NULL;
3684
3685 /* Only return IOMMUv2 domains */
3686 if (!(domain->flags & PD_IOMMUV2_MASK))
3687 return NULL;
3688
3689 return domain->iommu_domain;
3690}
3691EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003692
3693void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3694{
3695 struct iommu_dev_data *dev_data;
3696
3697 if (!amd_iommu_v2_supported())
3698 return;
3699
3700 dev_data = get_dev_data(&pdev->dev);
3701 dev_data->errata |= (1 << erratum);
3702}
3703EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003704
3705int amd_iommu_device_info(struct pci_dev *pdev,
3706 struct amd_iommu_device_info *info)
3707{
3708 int max_pasids;
3709 int pos;
3710
3711 if (pdev == NULL || info == NULL)
3712 return -EINVAL;
3713
3714 if (!amd_iommu_v2_supported())
3715 return -EINVAL;
3716
3717 memset(info, 0, sizeof(*info));
3718
3719 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3720 if (pos)
3721 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3722
3723 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3724 if (pos)
3725 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3726
3727 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3728 if (pos) {
3729 int features;
3730
3731 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3732 max_pasids = min(max_pasids, (1 << 20));
3733
3734 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3735 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3736
3737 features = pci_pasid_features(pdev);
3738 if (features & PCI_PASID_CAP_EXEC)
3739 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3740 if (features & PCI_PASID_CAP_PRIV)
3741 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3742 }
3743
3744 return 0;
3745}
3746EXPORT_SYMBOL(amd_iommu_device_info);