blob: 6d1cbdfc9b2a1af4a583460e8c4a5e5c80aabdfc [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 Roedel0feae532009-08-26 15:26:30 +020075/*
76 * Domain for untranslated devices - only allocated
77 * if iommu=pt passed on kernel cmd line.
78 */
79static struct protection_domain *pt_domain;
80
Joerg Roedel26961ef2008-12-03 17:00:17 +010081static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010082
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010083static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010084int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010085
Joerg Roedelac1534a2012-06-21 14:52:40 +020086static struct dma_map_ops amd_iommu_dma_ops;
87
Joerg Roedel431b2a22008-07-11 17:14:22 +020088/*
89 * general struct to manage commands send to an IOMMU
90 */
Joerg Roedeld6449532008-07-11 17:14:28 +020091struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020092 u32 data[4];
93};
94
Joerg Roedel04bfdd82009-09-02 16:00:23 +020095static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +010096static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -070097
Joerg Roedel15898bb2009-11-24 15:39:42 +010098/****************************************************************************
99 *
100 * Helper functions
101 *
102 ****************************************************************************/
103
Joerg Roedelf62dda62011-06-09 12:55:35 +0200104static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200105{
106 struct iommu_dev_data *dev_data;
107 unsigned long flags;
108
109 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
110 if (!dev_data)
111 return NULL;
112
Joerg Roedelf62dda62011-06-09 12:55:35 +0200113 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200114 atomic_set(&dev_data->bind, 0);
115
116 spin_lock_irqsave(&dev_data_list_lock, flags);
117 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
118 spin_unlock_irqrestore(&dev_data_list_lock, flags);
119
120 return dev_data;
121}
122
123static void free_dev_data(struct iommu_dev_data *dev_data)
124{
125 unsigned long flags;
126
127 spin_lock_irqsave(&dev_data_list_lock, flags);
128 list_del(&dev_data->dev_data_list);
129 spin_unlock_irqrestore(&dev_data_list_lock, flags);
130
131 kfree(dev_data);
132}
133
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200134static struct iommu_dev_data *search_dev_data(u16 devid)
135{
136 struct iommu_dev_data *dev_data;
137 unsigned long flags;
138
139 spin_lock_irqsave(&dev_data_list_lock, flags);
140 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
141 if (dev_data->devid == devid)
142 goto out_unlock;
143 }
144
145 dev_data = NULL;
146
147out_unlock:
148 spin_unlock_irqrestore(&dev_data_list_lock, flags);
149
150 return dev_data;
151}
152
153static struct iommu_dev_data *find_dev_data(u16 devid)
154{
155 struct iommu_dev_data *dev_data;
156
157 dev_data = search_dev_data(devid);
158
159 if (dev_data == NULL)
160 dev_data = alloc_dev_data(devid);
161
162 return dev_data;
163}
164
Joerg Roedel15898bb2009-11-24 15:39:42 +0100165static inline u16 get_device_id(struct device *dev)
166{
167 struct pci_dev *pdev = to_pci_dev(dev);
168
169 return calc_devid(pdev->bus->number, pdev->devfn);
170}
171
Joerg Roedel657cbb62009-11-23 15:26:46 +0100172static struct iommu_dev_data *get_dev_data(struct device *dev)
173{
174 return dev->archdata.iommu;
175}
176
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100177static bool pci_iommuv2_capable(struct pci_dev *pdev)
178{
179 static const int caps[] = {
180 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100181 PCI_EXT_CAP_ID_PRI,
182 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100183 };
184 int i, pos;
185
186 for (i = 0; i < 3; ++i) {
187 pos = pci_find_ext_capability(pdev, caps[i]);
188 if (pos == 0)
189 return false;
190 }
191
192 return true;
193}
194
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100195static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
196{
197 struct iommu_dev_data *dev_data;
198
199 dev_data = get_dev_data(&pdev->dev);
200
201 return dev_data->errata & (1 << erratum) ? true : false;
202}
203
Joerg Roedel71c70982009-11-24 16:43:06 +0100204/*
205 * In this function the list of preallocated protection domains is traversed to
206 * find the domain for a specific device
207 */
208static struct dma_ops_domain *find_protection_domain(u16 devid)
209{
210 struct dma_ops_domain *entry, *ret = NULL;
211 unsigned long flags;
212 u16 alias = amd_iommu_alias_table[devid];
213
214 if (list_empty(&iommu_pd_list))
215 return NULL;
216
217 spin_lock_irqsave(&iommu_pd_list_lock, flags);
218
219 list_for_each_entry(entry, &iommu_pd_list, list) {
220 if (entry->target_dev == devid ||
221 entry->target_dev == alias) {
222 ret = entry;
223 break;
224 }
225 }
226
227 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
228
229 return ret;
230}
231
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100232/*
233 * This function checks if the driver got a valid device from the caller to
234 * avoid dereferencing invalid pointers.
235 */
236static bool check_device(struct device *dev)
237{
238 u16 devid;
239
240 if (!dev || !dev->dma_mask)
241 return false;
242
243 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100244 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100245 return false;
246
247 devid = get_device_id(dev);
248
249 /* Out of our scope? */
250 if (devid > amd_iommu_last_bdf)
251 return false;
252
253 if (amd_iommu_rlookup_table[devid] == NULL)
254 return false;
255
256 return true;
257}
258
Alex Williamson664b6002012-05-30 14:19:31 -0600259static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
260{
261 pci_dev_put(*from);
262 *from = to;
263}
264
265#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
266
Joerg Roedel657cbb62009-11-23 15:26:46 +0100267static int iommu_init_device(struct device *dev)
268{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600269 struct pci_dev *dma_pdev, *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100270 struct iommu_dev_data *dev_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600271 struct iommu_group *group;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200272 u16 alias;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600273 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100274
275 if (dev->archdata.iommu)
276 return 0;
277
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200278 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100279 if (!dev_data)
280 return -ENOMEM;
281
Joerg Roedelf62dda62011-06-09 12:55:35 +0200282 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200283 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200284 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100285
Joerg Roedel71f77582011-06-09 19:03:15 +0200286 alias_data = find_dev_data(alias);
287 if (alias_data == NULL) {
288 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
289 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200290 free_dev_data(dev_data);
291 return -ENOTSUPP;
292 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200293 dev_data->alias_data = alias_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600294
295 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
296 } else
297 dma_pdev = pci_dev_get(pdev);
298
Alex Williamson664b6002012-05-30 14:19:31 -0600299 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
300
301 if (dma_pdev->multifunction &&
302 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
303 swap_pci_ref(&dma_pdev,
304 pci_get_slot(dma_pdev->bus,
305 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
306 0)));
307
308 while (!pci_is_root_bus(dma_pdev->bus)) {
309 if (pci_acs_path_enabled(dma_pdev->bus->self,
310 NULL, REQ_ACS_FLAGS))
311 break;
312
313 swap_pci_ref(&dma_pdev, pci_dev_get(dma_pdev->bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200314 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100315
Alex Williamson9dcd6132012-05-30 14:19:07 -0600316 group = iommu_group_get(&dma_pdev->dev);
317 pci_dev_put(dma_pdev);
318 if (!group) {
319 group = iommu_group_alloc();
320 if (IS_ERR(group))
321 return PTR_ERR(group);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100322 }
323
Alex Williamson9dcd6132012-05-30 14:19:07 -0600324 ret = iommu_group_add_device(group, dev);
325
326 iommu_group_put(group);
327
328 if (ret)
329 return ret;
330
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100331 if (pci_iommuv2_capable(pdev)) {
332 struct amd_iommu *iommu;
333
334 iommu = amd_iommu_rlookup_table[dev_data->devid];
335 dev_data->iommu_v2 = iommu->is_iommu_v2;
336 }
337
Joerg Roedel657cbb62009-11-23 15:26:46 +0100338 dev->archdata.iommu = dev_data;
339
Joerg Roedel657cbb62009-11-23 15:26:46 +0100340 return 0;
341}
342
Joerg Roedel26018872011-06-06 16:50:14 +0200343static void iommu_ignore_device(struct device *dev)
344{
345 u16 devid, alias;
346
347 devid = get_device_id(dev);
348 alias = amd_iommu_alias_table[devid];
349
350 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
351 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
352
353 amd_iommu_rlookup_table[devid] = NULL;
354 amd_iommu_rlookup_table[alias] = NULL;
355}
356
Joerg Roedel657cbb62009-11-23 15:26:46 +0100357static void iommu_uninit_device(struct device *dev)
358{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600359 iommu_group_remove_device(dev);
360
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200361 /*
362 * Nothing to do here - we keep dev_data around for unplugged devices
363 * and reuse it when the device is re-plugged - not doing so would
364 * introduce a ton of races.
365 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100366}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100367
368void __init amd_iommu_uninit_devices(void)
369{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200370 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100371 struct pci_dev *pdev = NULL;
372
373 for_each_pci_dev(pdev) {
374
375 if (!check_device(&pdev->dev))
376 continue;
377
378 iommu_uninit_device(&pdev->dev);
379 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200380
381 /* Free all of our dev_data structures */
382 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
383 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100384}
385
386int __init amd_iommu_init_devices(void)
387{
388 struct pci_dev *pdev = NULL;
389 int ret = 0;
390
391 for_each_pci_dev(pdev) {
392
393 if (!check_device(&pdev->dev))
394 continue;
395
396 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200397 if (ret == -ENOTSUPP)
398 iommu_ignore_device(&pdev->dev);
399 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100400 goto out_free;
401 }
402
403 return 0;
404
405out_free:
406
407 amd_iommu_uninit_devices();
408
409 return ret;
410}
Joerg Roedel7f265082008-12-12 13:50:21 +0100411#ifdef CONFIG_AMD_IOMMU_STATS
412
413/*
414 * Initialization code for statistics collection
415 */
416
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100417DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100418DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100419DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100420DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100421DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100422DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100423DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100424DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100425DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100426DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100427DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100428DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100429DECLARE_STATS_COUNTER(complete_ppr);
430DECLARE_STATS_COUNTER(invalidate_iotlb);
431DECLARE_STATS_COUNTER(invalidate_iotlb_all);
432DECLARE_STATS_COUNTER(pri_requests);
433
Joerg Roedel7f265082008-12-12 13:50:21 +0100434static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100435static struct dentry *de_fflush;
436
437static void amd_iommu_stats_add(struct __iommu_counter *cnt)
438{
439 if (stats_dir == NULL)
440 return;
441
442 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
443 &cnt->value);
444}
445
446static void amd_iommu_stats_init(void)
447{
448 stats_dir = debugfs_create_dir("amd-iommu", NULL);
449 if (stats_dir == NULL)
450 return;
451
Joerg Roedel7f265082008-12-12 13:50:21 +0100452 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300453 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100454
455 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100456 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100457 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100458 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100459 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100460 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100461 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100462 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100463 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100464 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100465 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100466 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100467 amd_iommu_stats_add(&complete_ppr);
468 amd_iommu_stats_add(&invalidate_iotlb);
469 amd_iommu_stats_add(&invalidate_iotlb_all);
470 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100471}
472
473#endif
474
Joerg Roedel431b2a22008-07-11 17:14:22 +0200475/****************************************************************************
476 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200477 * Interrupt handling functions
478 *
479 ****************************************************************************/
480
Joerg Roedele3e59872009-09-03 14:02:10 +0200481static void dump_dte_entry(u16 devid)
482{
483 int i;
484
Joerg Roedelee6c2862011-11-09 12:06:03 +0100485 for (i = 0; i < 4; ++i)
486 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200487 amd_iommu_dev_table[devid].data[i]);
488}
489
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200490static void dump_command(unsigned long phys_addr)
491{
492 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
493 int i;
494
495 for (i = 0; i < 4; ++i)
496 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
497}
498
Joerg Roedela345b232009-09-03 15:01:43 +0200499static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200500{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200501 int type, devid, domid, flags;
502 volatile u32 *event = __evt;
503 int count = 0;
504 u64 address;
505
506retry:
507 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
508 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
509 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
510 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
511 address = (u64)(((u64)event[3]) << 32) | event[2];
512
513 if (type == 0) {
514 /* Did we hit the erratum? */
515 if (++count == LOOP_TIMEOUT) {
516 pr_err("AMD-Vi: No event written to event log\n");
517 return;
518 }
519 udelay(1);
520 goto retry;
521 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200522
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200523 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200524
525 switch (type) {
526 case EVENT_TYPE_ILL_DEV:
527 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
528 "address=0x%016llx flags=0x%04x]\n",
529 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
530 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200531 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200532 break;
533 case EVENT_TYPE_IO_FAULT:
534 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
535 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
536 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
537 domid, address, flags);
538 break;
539 case EVENT_TYPE_DEV_TAB_ERR:
540 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
541 "address=0x%016llx flags=0x%04x]\n",
542 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
543 address, flags);
544 break;
545 case EVENT_TYPE_PAGE_TAB_ERR:
546 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
547 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
548 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
549 domid, address, flags);
550 break;
551 case EVENT_TYPE_ILL_CMD:
552 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200553 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200554 break;
555 case EVENT_TYPE_CMD_HARD_ERR:
556 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
557 "flags=0x%04x]\n", address, flags);
558 break;
559 case EVENT_TYPE_IOTLB_INV_TO:
560 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
561 "address=0x%016llx]\n",
562 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
563 address);
564 break;
565 case EVENT_TYPE_INV_DEV_REQ:
566 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
567 "address=0x%016llx flags=0x%04x]\n",
568 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
569 address, flags);
570 break;
571 default:
572 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
573 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200574
575 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200576}
577
578static void iommu_poll_events(struct amd_iommu *iommu)
579{
580 u32 head, tail;
581 unsigned long flags;
582
583 spin_lock_irqsave(&iommu->lock, flags);
584
585 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
586 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
587
588 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200589 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200590 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
591 }
592
593 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
594
595 spin_unlock_irqrestore(&iommu->lock, flags);
596}
597
Joerg Roedeleee53532012-06-01 15:20:23 +0200598static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100599{
600 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100601
Joerg Roedel399be2f2011-12-01 16:53:47 +0100602 INC_STATS_COUNTER(pri_requests);
603
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100604 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
605 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
606 return;
607 }
608
609 fault.address = raw[1];
610 fault.pasid = PPR_PASID(raw[0]);
611 fault.device_id = PPR_DEVID(raw[0]);
612 fault.tag = PPR_TAG(raw[0]);
613 fault.flags = PPR_FLAGS(raw[0]);
614
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100615 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
616}
617
618static void iommu_poll_ppr_log(struct amd_iommu *iommu)
619{
620 unsigned long flags;
621 u32 head, tail;
622
623 if (iommu->ppr_log == NULL)
624 return;
625
Joerg Roedeleee53532012-06-01 15:20:23 +0200626 /* enable ppr interrupts again */
627 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
628
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100629 spin_lock_irqsave(&iommu->lock, flags);
630
631 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
632 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
633
634 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200635 volatile u64 *raw;
636 u64 entry[2];
637 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100638
Joerg Roedeleee53532012-06-01 15:20:23 +0200639 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100640
Joerg Roedeleee53532012-06-01 15:20:23 +0200641 /*
642 * Hardware bug: Interrupt may arrive before the entry is
643 * written to memory. If this happens we need to wait for the
644 * entry to arrive.
645 */
646 for (i = 0; i < LOOP_TIMEOUT; ++i) {
647 if (PPR_REQ_TYPE(raw[0]) != 0)
648 break;
649 udelay(1);
650 }
651
652 /* Avoid memcpy function-call overhead */
653 entry[0] = raw[0];
654 entry[1] = raw[1];
655
656 /*
657 * To detect the hardware bug we need to clear the entry
658 * back to zero.
659 */
660 raw[0] = raw[1] = 0UL;
661
662 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100663 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
664 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200665
666 /*
667 * Release iommu->lock because ppr-handling might need to
668 * re-aquire it
669 */
670 spin_unlock_irqrestore(&iommu->lock, flags);
671
672 /* Handle PPR entry */
673 iommu_handle_ppr_entry(iommu, entry);
674
675 spin_lock_irqsave(&iommu->lock, flags);
676
677 /* Refresh ring-buffer information */
678 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100679 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
680 }
681
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100682 spin_unlock_irqrestore(&iommu->lock, flags);
683}
684
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200685irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200686{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200687 struct amd_iommu *iommu;
688
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100689 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200690 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100691 iommu_poll_ppr_log(iommu);
692 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200693
694 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200695}
696
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200697irqreturn_t amd_iommu_int_handler(int irq, void *data)
698{
699 return IRQ_WAKE_THREAD;
700}
701
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200702/****************************************************************************
703 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200704 * IOMMU command queuing functions
705 *
706 ****************************************************************************/
707
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200708static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200709{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200710 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200711
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200712 while (*sem == 0 && i < LOOP_TIMEOUT) {
713 udelay(1);
714 i += 1;
715 }
716
717 if (i == LOOP_TIMEOUT) {
718 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
719 return -EIO;
720 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200721
722 return 0;
723}
724
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200725static void copy_cmd_to_buffer(struct amd_iommu *iommu,
726 struct iommu_cmd *cmd,
727 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200728{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200729 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200730
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200731 target = iommu->cmd_buf + tail;
732 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200733
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200734 /* Copy command to buffer */
735 memcpy(target, cmd, sizeof(*cmd));
736
737 /* Tell the IOMMU about it */
738 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
739}
740
Joerg Roedel815b33f2011-04-06 17:26:49 +0200741static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200742{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200743 WARN_ON(address & 0x7ULL);
744
Joerg Roedelded46732011-04-06 10:53:48 +0200745 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200746 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
747 cmd->data[1] = upper_32_bits(__pa(address));
748 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200749 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
750}
751
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200752static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
753{
754 memset(cmd, 0, sizeof(*cmd));
755 cmd->data[0] = devid;
756 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
757}
758
Joerg Roedel11b64022011-04-06 11:49:28 +0200759static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
760 size_t size, u16 domid, int pde)
761{
762 u64 pages;
763 int s;
764
765 pages = iommu_num_pages(address, size, PAGE_SIZE);
766 s = 0;
767
768 if (pages > 1) {
769 /*
770 * If we have to flush more than one page, flush all
771 * TLB entries for this domain
772 */
773 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
774 s = 1;
775 }
776
777 address &= PAGE_MASK;
778
779 memset(cmd, 0, sizeof(*cmd));
780 cmd->data[1] |= domid;
781 cmd->data[2] = lower_32_bits(address);
782 cmd->data[3] = upper_32_bits(address);
783 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
784 if (s) /* size bit - we flush more than one 4kb page */
785 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
786 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
787 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
788}
789
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200790static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
791 u64 address, size_t size)
792{
793 u64 pages;
794 int s;
795
796 pages = iommu_num_pages(address, size, PAGE_SIZE);
797 s = 0;
798
799 if (pages > 1) {
800 /*
801 * If we have to flush more than one page, flush all
802 * TLB entries for this domain
803 */
804 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
805 s = 1;
806 }
807
808 address &= PAGE_MASK;
809
810 memset(cmd, 0, sizeof(*cmd));
811 cmd->data[0] = devid;
812 cmd->data[0] |= (qdep & 0xff) << 24;
813 cmd->data[1] = devid;
814 cmd->data[2] = lower_32_bits(address);
815 cmd->data[3] = upper_32_bits(address);
816 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
817 if (s)
818 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
819}
820
Joerg Roedel22e266c2011-11-21 15:59:08 +0100821static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
822 u64 address, bool size)
823{
824 memset(cmd, 0, sizeof(*cmd));
825
826 address &= ~(0xfffULL);
827
828 cmd->data[0] = pasid & PASID_MASK;
829 cmd->data[1] = domid;
830 cmd->data[2] = lower_32_bits(address);
831 cmd->data[3] = upper_32_bits(address);
832 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
833 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
834 if (size)
835 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
836 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
837}
838
839static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
840 int qdep, u64 address, bool size)
841{
842 memset(cmd, 0, sizeof(*cmd));
843
844 address &= ~(0xfffULL);
845
846 cmd->data[0] = devid;
847 cmd->data[0] |= (pasid & 0xff) << 16;
848 cmd->data[0] |= (qdep & 0xff) << 24;
849 cmd->data[1] = devid;
850 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
851 cmd->data[2] = lower_32_bits(address);
852 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
853 cmd->data[3] = upper_32_bits(address);
854 if (size)
855 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
856 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
857}
858
Joerg Roedelc99afa22011-11-21 18:19:25 +0100859static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
860 int status, int tag, bool gn)
861{
862 memset(cmd, 0, sizeof(*cmd));
863
864 cmd->data[0] = devid;
865 if (gn) {
866 cmd->data[1] = pasid & PASID_MASK;
867 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
868 }
869 cmd->data[3] = tag & 0x1ff;
870 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
871
872 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
873}
874
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200875static void build_inv_all(struct iommu_cmd *cmd)
876{
877 memset(cmd, 0, sizeof(*cmd));
878 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200879}
880
Joerg Roedel431b2a22008-07-11 17:14:22 +0200881/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200882 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200883 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200884 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200885static int iommu_queue_command_sync(struct amd_iommu *iommu,
886 struct iommu_cmd *cmd,
887 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200888{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200889 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200890 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200891
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200892 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100893
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200894again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200895 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200896
897 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
898 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
899 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
900 left = (head - next_tail) % iommu->cmd_buf_size;
901
902 if (left <= 2) {
903 struct iommu_cmd sync_cmd;
904 volatile u64 sem = 0;
905 int ret;
906
907 build_completion_wait(&sync_cmd, (u64)&sem);
908 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
909
910 spin_unlock_irqrestore(&iommu->lock, flags);
911
912 if ((ret = wait_on_sem(&sem)) != 0)
913 return ret;
914
915 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200916 }
917
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200918 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200919
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200920 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200921 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200922
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200923 spin_unlock_irqrestore(&iommu->lock, flags);
924
Joerg Roedel815b33f2011-04-06 17:26:49 +0200925 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100926}
927
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200928static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
929{
930 return iommu_queue_command_sync(iommu, cmd, true);
931}
932
Joerg Roedel8d201962008-12-02 20:34:41 +0100933/*
934 * This function queues a completion wait command into the command
935 * buffer of an IOMMU
936 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100937static int iommu_completion_wait(struct amd_iommu *iommu)
938{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200939 struct iommu_cmd cmd;
940 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200941 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100942
943 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200944 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100945
Joerg Roedel815b33f2011-04-06 17:26:49 +0200946 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100947
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200948 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100949 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200950 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100951
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200952 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200953}
954
Joerg Roedeld8c13082011-04-06 18:51:26 +0200955static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200956{
957 struct iommu_cmd cmd;
958
Joerg Roedeld8c13082011-04-06 18:51:26 +0200959 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200960
Joerg Roedeld8c13082011-04-06 18:51:26 +0200961 return iommu_queue_command(iommu, &cmd);
962}
963
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200964static void iommu_flush_dte_all(struct amd_iommu *iommu)
965{
966 u32 devid;
967
968 for (devid = 0; devid <= 0xffff; ++devid)
969 iommu_flush_dte(iommu, devid);
970
971 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200972}
973
974/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200975 * This function uses heavy locking and may disable irqs for some time. But
976 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200977 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200978static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200979{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200980 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200981
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200982 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
983 struct iommu_cmd cmd;
984 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
985 dom_id, 1);
986 iommu_queue_command(iommu, &cmd);
987 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200988
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200989 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200990}
991
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200992static void iommu_flush_all(struct amd_iommu *iommu)
993{
994 struct iommu_cmd cmd;
995
996 build_inv_all(&cmd);
997
998 iommu_queue_command(iommu, &cmd);
999 iommu_completion_wait(iommu);
1000}
1001
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001002void iommu_flush_all_caches(struct amd_iommu *iommu)
1003{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001004 if (iommu_feature(iommu, FEATURE_IA)) {
1005 iommu_flush_all(iommu);
1006 } else {
1007 iommu_flush_dte_all(iommu);
1008 iommu_flush_tlb_all(iommu);
1009 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001010}
1011
Joerg Roedel431b2a22008-07-11 17:14:22 +02001012/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001013 * Command send function for flushing on-device TLB
1014 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001015static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1016 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001017{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001018 struct amd_iommu *iommu;
1019 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001020 int qdep;
1021
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001022 qdep = dev_data->ats.qdep;
1023 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001024
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001025 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001026
1027 return iommu_queue_command(iommu, &cmd);
1028}
1029
1030/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001031 * Command send function for invalidating a device table entry
1032 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001033static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001034{
1035 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001036 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001037
Joerg Roedel6c542042011-06-09 17:07:31 +02001038 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001039
Joerg Roedelf62dda62011-06-09 12:55:35 +02001040 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001041 if (ret)
1042 return ret;
1043
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001044 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001045 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001046
1047 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001048}
1049
Joerg Roedel431b2a22008-07-11 17:14:22 +02001050/*
1051 * TLB invalidation function which is called from the mapping functions.
1052 * It invalidates a single PTE if the range to flush is within a single
1053 * page. Otherwise it flushes the whole TLB of the IOMMU.
1054 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001055static void __domain_flush_pages(struct protection_domain *domain,
1056 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001057{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001058 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001059 struct iommu_cmd cmd;
1060 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001061
Joerg Roedel11b64022011-04-06 11:49:28 +02001062 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001063
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001064 for (i = 0; i < amd_iommus_present; ++i) {
1065 if (!domain->dev_iommu[i])
1066 continue;
1067
1068 /*
1069 * Devices of this domain are behind this IOMMU
1070 * We need a TLB flush
1071 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001072 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001073 }
1074
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001075 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001076
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001077 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001078 continue;
1079
Joerg Roedel6c542042011-06-09 17:07:31 +02001080 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001081 }
1082
Joerg Roedel11b64022011-04-06 11:49:28 +02001083 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001084}
1085
Joerg Roedel17b124b2011-04-06 18:01:35 +02001086static void domain_flush_pages(struct protection_domain *domain,
1087 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001088{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001089 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001090}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001091
Joerg Roedel1c655772008-09-04 18:40:05 +02001092/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001093static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001094{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001095 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001096}
1097
Chris Wright42a49f92009-06-15 15:42:00 +02001098/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001099static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001100{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001101 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1102}
1103
1104static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001105{
1106 int i;
1107
1108 for (i = 0; i < amd_iommus_present; ++i) {
1109 if (!domain->dev_iommu[i])
1110 continue;
1111
1112 /*
1113 * Devices of this domain are behind this IOMMU
1114 * We need to wait for completion of all commands.
1115 */
1116 iommu_completion_wait(amd_iommus[i]);
1117 }
1118}
1119
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001120
Joerg Roedel43f49602008-12-02 21:01:12 +01001121/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001122 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001123 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001124static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001125{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001126 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001127
1128 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001129 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001130}
1131
Joerg Roedel431b2a22008-07-11 17:14:22 +02001132/****************************************************************************
1133 *
1134 * The functions below are used the create the page table mappings for
1135 * unity mapped regions.
1136 *
1137 ****************************************************************************/
1138
1139/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001140 * This function is used to add another level to an IO page table. Adding
1141 * another level increases the size of the address space by 9 bits to a size up
1142 * to 64 bits.
1143 */
1144static bool increase_address_space(struct protection_domain *domain,
1145 gfp_t gfp)
1146{
1147 u64 *pte;
1148
1149 if (domain->mode == PAGE_MODE_6_LEVEL)
1150 /* address space already 64 bit large */
1151 return false;
1152
1153 pte = (void *)get_zeroed_page(gfp);
1154 if (!pte)
1155 return false;
1156
1157 *pte = PM_LEVEL_PDE(domain->mode,
1158 virt_to_phys(domain->pt_root));
1159 domain->pt_root = pte;
1160 domain->mode += 1;
1161 domain->updated = true;
1162
1163 return true;
1164}
1165
1166static u64 *alloc_pte(struct protection_domain *domain,
1167 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001168 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001169 u64 **pte_page,
1170 gfp_t gfp)
1171{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001172 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001173 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001174
1175 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001176
1177 while (address > PM_LEVEL_SIZE(domain->mode))
1178 increase_address_space(domain, gfp);
1179
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001180 level = domain->mode - 1;
1181 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1182 address = PAGE_SIZE_ALIGN(address, page_size);
1183 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001184
1185 while (level > end_lvl) {
1186 if (!IOMMU_PTE_PRESENT(*pte)) {
1187 page = (u64 *)get_zeroed_page(gfp);
1188 if (!page)
1189 return NULL;
1190 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1191 }
1192
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001193 /* No level skipping support yet */
1194 if (PM_PTE_LEVEL(*pte) != level)
1195 return NULL;
1196
Joerg Roedel308973d2009-11-24 17:43:32 +01001197 level -= 1;
1198
1199 pte = IOMMU_PTE_PAGE(*pte);
1200
1201 if (pte_page && level == end_lvl)
1202 *pte_page = pte;
1203
1204 pte = &pte[PM_LEVEL_INDEX(level, address)];
1205 }
1206
1207 return pte;
1208}
1209
1210/*
1211 * This function checks if there is a PTE for a given dma address. If
1212 * there is one, it returns the pointer to it.
1213 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001214static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001215{
1216 int level;
1217 u64 *pte;
1218
Joerg Roedel24cd7722010-01-19 17:27:39 +01001219 if (address > PM_LEVEL_SIZE(domain->mode))
1220 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001221
Joerg Roedel24cd7722010-01-19 17:27:39 +01001222 level = domain->mode - 1;
1223 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1224
1225 while (level > 0) {
1226
1227 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001228 if (!IOMMU_PTE_PRESENT(*pte))
1229 return NULL;
1230
Joerg Roedel24cd7722010-01-19 17:27:39 +01001231 /* Large PTE */
1232 if (PM_PTE_LEVEL(*pte) == 0x07) {
1233 unsigned long pte_mask, __pte;
1234
1235 /*
1236 * If we have a series of large PTEs, make
1237 * sure to return a pointer to the first one.
1238 */
1239 pte_mask = PTE_PAGE_SIZE(*pte);
1240 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1241 __pte = ((unsigned long)pte) & pte_mask;
1242
1243 return (u64 *)__pte;
1244 }
1245
1246 /* No level skipping support yet */
1247 if (PM_PTE_LEVEL(*pte) != level)
1248 return NULL;
1249
Joerg Roedel308973d2009-11-24 17:43:32 +01001250 level -= 1;
1251
Joerg Roedel24cd7722010-01-19 17:27:39 +01001252 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001253 pte = IOMMU_PTE_PAGE(*pte);
1254 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001255 }
1256
1257 return pte;
1258}
1259
1260/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001261 * Generic mapping functions. It maps a physical address into a DMA
1262 * address space. It allocates the page table pages if necessary.
1263 * In the future it can be extended to a generic mapping function
1264 * supporting all features of AMD IOMMU page tables like level skipping
1265 * and full 64 bit address spaces.
1266 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001267static int iommu_map_page(struct protection_domain *dom,
1268 unsigned long bus_addr,
1269 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001270 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001271 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001272{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001273 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001274 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001275
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001276 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001277 return -EINVAL;
1278
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001279 bus_addr = PAGE_ALIGN(bus_addr);
1280 phys_addr = PAGE_ALIGN(phys_addr);
1281 count = PAGE_SIZE_PTE_COUNT(page_size);
1282 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001283
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001284 for (i = 0; i < count; ++i)
1285 if (IOMMU_PTE_PRESENT(pte[i]))
1286 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001287
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001288 if (page_size > PAGE_SIZE) {
1289 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1290 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1291 } else
1292 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1293
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001294 if (prot & IOMMU_PROT_IR)
1295 __pte |= IOMMU_PTE_IR;
1296 if (prot & IOMMU_PROT_IW)
1297 __pte |= IOMMU_PTE_IW;
1298
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001299 for (i = 0; i < count; ++i)
1300 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001301
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001302 update_domain(dom);
1303
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001304 return 0;
1305}
1306
Joerg Roedel24cd7722010-01-19 17:27:39 +01001307static unsigned long iommu_unmap_page(struct protection_domain *dom,
1308 unsigned long bus_addr,
1309 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001310{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001311 unsigned long long unmap_size, unmapped;
1312 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001313
Joerg Roedel24cd7722010-01-19 17:27:39 +01001314 BUG_ON(!is_power_of_2(page_size));
1315
1316 unmapped = 0;
1317
1318 while (unmapped < page_size) {
1319
1320 pte = fetch_pte(dom, bus_addr);
1321
1322 if (!pte) {
1323 /*
1324 * No PTE for this address
1325 * move forward in 4kb steps
1326 */
1327 unmap_size = PAGE_SIZE;
1328 } else if (PM_PTE_LEVEL(*pte) == 0) {
1329 /* 4kb PTE found for this address */
1330 unmap_size = PAGE_SIZE;
1331 *pte = 0ULL;
1332 } else {
1333 int count, i;
1334
1335 /* Large PTE found which maps this address */
1336 unmap_size = PTE_PAGE_SIZE(*pte);
1337 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1338 for (i = 0; i < count; i++)
1339 pte[i] = 0ULL;
1340 }
1341
1342 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1343 unmapped += unmap_size;
1344 }
1345
1346 BUG_ON(!is_power_of_2(unmapped));
1347
1348 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001349}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001350
Joerg Roedel431b2a22008-07-11 17:14:22 +02001351/*
1352 * This function checks if a specific unity mapping entry is needed for
1353 * this specific IOMMU.
1354 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001355static int iommu_for_unity_map(struct amd_iommu *iommu,
1356 struct unity_map_entry *entry)
1357{
1358 u16 bdf, i;
1359
1360 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1361 bdf = amd_iommu_alias_table[i];
1362 if (amd_iommu_rlookup_table[bdf] == iommu)
1363 return 1;
1364 }
1365
1366 return 0;
1367}
1368
Joerg Roedel431b2a22008-07-11 17:14:22 +02001369/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001370 * This function actually applies the mapping to the page table of the
1371 * dma_ops domain.
1372 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001373static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1374 struct unity_map_entry *e)
1375{
1376 u64 addr;
1377 int ret;
1378
1379 for (addr = e->address_start; addr < e->address_end;
1380 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001381 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001382 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001383 if (ret)
1384 return ret;
1385 /*
1386 * if unity mapping is in aperture range mark the page
1387 * as allocated in the aperture
1388 */
1389 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001390 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001391 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001392 }
1393
1394 return 0;
1395}
1396
Joerg Roedel431b2a22008-07-11 17:14:22 +02001397/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001398 * Init the unity mappings for a specific IOMMU in the system
1399 *
1400 * Basically iterates over all unity mapping entries and applies them to
1401 * the default domain DMA of that IOMMU if necessary.
1402 */
1403static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1404{
1405 struct unity_map_entry *entry;
1406 int ret;
1407
1408 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1409 if (!iommu_for_unity_map(iommu, entry))
1410 continue;
1411 ret = dma_ops_unity_map(iommu->default_dom, entry);
1412 if (ret)
1413 return ret;
1414 }
1415
1416 return 0;
1417}
1418
1419/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001420 * Inits the unity mappings required for a specific device
1421 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001422static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1423 u16 devid)
1424{
1425 struct unity_map_entry *e;
1426 int ret;
1427
1428 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1429 if (!(devid >= e->devid_start && devid <= e->devid_end))
1430 continue;
1431 ret = dma_ops_unity_map(dma_dom, e);
1432 if (ret)
1433 return ret;
1434 }
1435
1436 return 0;
1437}
1438
Joerg Roedel431b2a22008-07-11 17:14:22 +02001439/****************************************************************************
1440 *
1441 * The next functions belong to the address allocator for the dma_ops
1442 * interface functions. They work like the allocators in the other IOMMU
1443 * drivers. Its basically a bitmap which marks the allocated pages in
1444 * the aperture. Maybe it could be enhanced in the future to a more
1445 * efficient allocator.
1446 *
1447 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001448
Joerg Roedel431b2a22008-07-11 17:14:22 +02001449/*
Joerg Roedel384de722009-05-15 12:30:05 +02001450 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001451 *
1452 * called with domain->lock held
1453 */
Joerg Roedel384de722009-05-15 12:30:05 +02001454
Joerg Roedel9cabe892009-05-18 16:38:55 +02001455/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001456 * Used to reserve address ranges in the aperture (e.g. for exclusion
1457 * ranges.
1458 */
1459static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1460 unsigned long start_page,
1461 unsigned int pages)
1462{
1463 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1464
1465 if (start_page + pages > last_page)
1466 pages = last_page - start_page;
1467
1468 for (i = start_page; i < start_page + pages; ++i) {
1469 int index = i / APERTURE_RANGE_PAGES;
1470 int page = i % APERTURE_RANGE_PAGES;
1471 __set_bit(page, dom->aperture[index]->bitmap);
1472 }
1473}
1474
1475/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001476 * This function is used to add a new aperture range to an existing
1477 * aperture in case of dma_ops domain allocation or address allocation
1478 * failure.
1479 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001480static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001481 bool populate, gfp_t gfp)
1482{
1483 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001484 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001485 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001486
Joerg Roedelf5e97052009-05-22 12:31:53 +02001487#ifdef CONFIG_IOMMU_STRESS
1488 populate = false;
1489#endif
1490
Joerg Roedel9cabe892009-05-18 16:38:55 +02001491 if (index >= APERTURE_MAX_RANGES)
1492 return -ENOMEM;
1493
1494 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1495 if (!dma_dom->aperture[index])
1496 return -ENOMEM;
1497
1498 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1499 if (!dma_dom->aperture[index]->bitmap)
1500 goto out_free;
1501
1502 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1503
1504 if (populate) {
1505 unsigned long address = dma_dom->aperture_size;
1506 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1507 u64 *pte, *pte_page;
1508
1509 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001510 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001511 &pte_page, gfp);
1512 if (!pte)
1513 goto out_free;
1514
1515 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1516
1517 address += APERTURE_RANGE_SIZE / 64;
1518 }
1519 }
1520
Joerg Roedel17f5b562011-07-06 17:14:44 +02001521 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001522 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1523
Joerg Roedel17f5b562011-07-06 17:14:44 +02001524 /* Reserve address range used for MSI messages */
1525 if (old_size < MSI_ADDR_BASE_LO &&
1526 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1527 unsigned long spage;
1528 int pages;
1529
1530 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1531 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1532
1533 dma_ops_reserve_addresses(dma_dom, spage, pages);
1534 }
1535
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001536 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001537 for_each_iommu(iommu) {
1538 if (iommu->exclusion_start &&
1539 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1540 && iommu->exclusion_start < dma_dom->aperture_size) {
1541 unsigned long startpage;
1542 int pages = iommu_num_pages(iommu->exclusion_start,
1543 iommu->exclusion_length,
1544 PAGE_SIZE);
1545 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1546 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1547 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001548 }
1549
1550 /*
1551 * Check for areas already mapped as present in the new aperture
1552 * range and mark those pages as reserved in the allocator. Such
1553 * mappings may already exist as a result of requested unity
1554 * mappings for devices.
1555 */
1556 for (i = dma_dom->aperture[index]->offset;
1557 i < dma_dom->aperture_size;
1558 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001559 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001560 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1561 continue;
1562
Joerg Roedelfcd08612011-10-11 17:41:32 +02001563 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001564 }
1565
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001566 update_domain(&dma_dom->domain);
1567
Joerg Roedel9cabe892009-05-18 16:38:55 +02001568 return 0;
1569
1570out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001571 update_domain(&dma_dom->domain);
1572
Joerg Roedel9cabe892009-05-18 16:38:55 +02001573 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1574
1575 kfree(dma_dom->aperture[index]);
1576 dma_dom->aperture[index] = NULL;
1577
1578 return -ENOMEM;
1579}
1580
Joerg Roedel384de722009-05-15 12:30:05 +02001581static unsigned long dma_ops_area_alloc(struct device *dev,
1582 struct dma_ops_domain *dom,
1583 unsigned int pages,
1584 unsigned long align_mask,
1585 u64 dma_mask,
1586 unsigned long start)
1587{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001588 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001589 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1590 int i = start >> APERTURE_RANGE_SHIFT;
1591 unsigned long boundary_size;
1592 unsigned long address = -1;
1593 unsigned long limit;
1594
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001595 next_bit >>= PAGE_SHIFT;
1596
Joerg Roedel384de722009-05-15 12:30:05 +02001597 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1598 PAGE_SIZE) >> PAGE_SHIFT;
1599
1600 for (;i < max_index; ++i) {
1601 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1602
1603 if (dom->aperture[i]->offset >= dma_mask)
1604 break;
1605
1606 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1607 dma_mask >> PAGE_SHIFT);
1608
1609 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1610 limit, next_bit, pages, 0,
1611 boundary_size, align_mask);
1612 if (address != -1) {
1613 address = dom->aperture[i]->offset +
1614 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001615 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001616 break;
1617 }
1618
1619 next_bit = 0;
1620 }
1621
1622 return address;
1623}
1624
Joerg Roedeld3086442008-06-26 21:27:57 +02001625static unsigned long dma_ops_alloc_addresses(struct device *dev,
1626 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001627 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001628 unsigned long align_mask,
1629 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001630{
Joerg Roedeld3086442008-06-26 21:27:57 +02001631 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001632
Joerg Roedelfe16f082009-05-22 12:27:53 +02001633#ifdef CONFIG_IOMMU_STRESS
1634 dom->next_address = 0;
1635 dom->need_flush = true;
1636#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001637
Joerg Roedel384de722009-05-15 12:30:05 +02001638 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001639 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001640
Joerg Roedel1c655772008-09-04 18:40:05 +02001641 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001642 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001643 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1644 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001645 dom->need_flush = true;
1646 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001647
Joerg Roedel384de722009-05-15 12:30:05 +02001648 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001649 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001650
1651 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1652
1653 return address;
1654}
1655
Joerg Roedel431b2a22008-07-11 17:14:22 +02001656/*
1657 * The address free function.
1658 *
1659 * called with domain->lock held
1660 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001661static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1662 unsigned long address,
1663 unsigned int pages)
1664{
Joerg Roedel384de722009-05-15 12:30:05 +02001665 unsigned i = address >> APERTURE_RANGE_SHIFT;
1666 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001667
Joerg Roedel384de722009-05-15 12:30:05 +02001668 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1669
Joerg Roedel47bccd62009-05-22 12:40:54 +02001670#ifdef CONFIG_IOMMU_STRESS
1671 if (i < 4)
1672 return;
1673#endif
1674
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001675 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001676 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001677
1678 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001679
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001680 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001681
Joerg Roedeld3086442008-06-26 21:27:57 +02001682}
1683
Joerg Roedel431b2a22008-07-11 17:14:22 +02001684/****************************************************************************
1685 *
1686 * The next functions belong to the domain allocation. A domain is
1687 * allocated for every IOMMU as the default domain. If device isolation
1688 * is enabled, every device get its own domain. The most important thing
1689 * about domains is the page table mapping the DMA address space they
1690 * contain.
1691 *
1692 ****************************************************************************/
1693
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001694/*
1695 * This function adds a protection domain to the global protection domain list
1696 */
1697static void add_domain_to_list(struct protection_domain *domain)
1698{
1699 unsigned long flags;
1700
1701 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1702 list_add(&domain->list, &amd_iommu_pd_list);
1703 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1704}
1705
1706/*
1707 * This function removes a protection domain to the global
1708 * protection domain list
1709 */
1710static void del_domain_from_list(struct protection_domain *domain)
1711{
1712 unsigned long flags;
1713
1714 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1715 list_del(&domain->list);
1716 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1717}
1718
Joerg Roedelec487d12008-06-26 21:27:58 +02001719static u16 domain_id_alloc(void)
1720{
1721 unsigned long flags;
1722 int id;
1723
1724 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1725 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1726 BUG_ON(id == 0);
1727 if (id > 0 && id < MAX_DOMAIN_ID)
1728 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1729 else
1730 id = 0;
1731 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1732
1733 return id;
1734}
1735
Joerg Roedela2acfb72008-12-02 18:28:53 +01001736static void domain_id_free(int id)
1737{
1738 unsigned long flags;
1739
1740 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1741 if (id > 0 && id < MAX_DOMAIN_ID)
1742 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1743 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1744}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001745
Joerg Roedel86db2e52008-12-02 18:20:21 +01001746static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001747{
1748 int i, j;
1749 u64 *p1, *p2, *p3;
1750
Joerg Roedel86db2e52008-12-02 18:20:21 +01001751 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001752
1753 if (!p1)
1754 return;
1755
1756 for (i = 0; i < 512; ++i) {
1757 if (!IOMMU_PTE_PRESENT(p1[i]))
1758 continue;
1759
1760 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001761 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001762 if (!IOMMU_PTE_PRESENT(p2[j]))
1763 continue;
1764 p3 = IOMMU_PTE_PAGE(p2[j]);
1765 free_page((unsigned long)p3);
1766 }
1767
1768 free_page((unsigned long)p2);
1769 }
1770
1771 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001772
1773 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001774}
1775
Joerg Roedelb16137b2011-11-21 16:50:23 +01001776static void free_gcr3_tbl_level1(u64 *tbl)
1777{
1778 u64 *ptr;
1779 int i;
1780
1781 for (i = 0; i < 512; ++i) {
1782 if (!(tbl[i] & GCR3_VALID))
1783 continue;
1784
1785 ptr = __va(tbl[i] & PAGE_MASK);
1786
1787 free_page((unsigned long)ptr);
1788 }
1789}
1790
1791static void free_gcr3_tbl_level2(u64 *tbl)
1792{
1793 u64 *ptr;
1794 int i;
1795
1796 for (i = 0; i < 512; ++i) {
1797 if (!(tbl[i] & GCR3_VALID))
1798 continue;
1799
1800 ptr = __va(tbl[i] & PAGE_MASK);
1801
1802 free_gcr3_tbl_level1(ptr);
1803 }
1804}
1805
Joerg Roedel52815b72011-11-17 17:24:28 +01001806static void free_gcr3_table(struct protection_domain *domain)
1807{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001808 if (domain->glx == 2)
1809 free_gcr3_tbl_level2(domain->gcr3_tbl);
1810 else if (domain->glx == 1)
1811 free_gcr3_tbl_level1(domain->gcr3_tbl);
1812 else if (domain->glx != 0)
1813 BUG();
1814
Joerg Roedel52815b72011-11-17 17:24:28 +01001815 free_page((unsigned long)domain->gcr3_tbl);
1816}
1817
Joerg Roedel431b2a22008-07-11 17:14:22 +02001818/*
1819 * Free a domain, only used if something went wrong in the
1820 * allocation path and we need to free an already allocated page table
1821 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001822static void dma_ops_domain_free(struct dma_ops_domain *dom)
1823{
Joerg Roedel384de722009-05-15 12:30:05 +02001824 int i;
1825
Joerg Roedelec487d12008-06-26 21:27:58 +02001826 if (!dom)
1827 return;
1828
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001829 del_domain_from_list(&dom->domain);
1830
Joerg Roedel86db2e52008-12-02 18:20:21 +01001831 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001832
Joerg Roedel384de722009-05-15 12:30:05 +02001833 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1834 if (!dom->aperture[i])
1835 continue;
1836 free_page((unsigned long)dom->aperture[i]->bitmap);
1837 kfree(dom->aperture[i]);
1838 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001839
1840 kfree(dom);
1841}
1842
Joerg Roedel431b2a22008-07-11 17:14:22 +02001843/*
1844 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001845 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001846 * structures required for the dma_ops interface
1847 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001848static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001849{
1850 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001851
1852 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1853 if (!dma_dom)
1854 return NULL;
1855
1856 spin_lock_init(&dma_dom->domain.lock);
1857
1858 dma_dom->domain.id = domain_id_alloc();
1859 if (dma_dom->domain.id == 0)
1860 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001861 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001862 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001863 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001864 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001865 dma_dom->domain.priv = dma_dom;
1866 if (!dma_dom->domain.pt_root)
1867 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001868
Joerg Roedel1c655772008-09-04 18:40:05 +02001869 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001870 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001871
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001872 add_domain_to_list(&dma_dom->domain);
1873
Joerg Roedel576175c2009-11-23 19:08:46 +01001874 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001875 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001876
Joerg Roedel431b2a22008-07-11 17:14:22 +02001877 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001878 * mark the first page as allocated so we never return 0 as
1879 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001880 */
Joerg Roedel384de722009-05-15 12:30:05 +02001881 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001882 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001883
Joerg Roedelec487d12008-06-26 21:27:58 +02001884
1885 return dma_dom;
1886
1887free_dma_dom:
1888 dma_ops_domain_free(dma_dom);
1889
1890 return NULL;
1891}
1892
Joerg Roedel431b2a22008-07-11 17:14:22 +02001893/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001894 * little helper function to check whether a given protection domain is a
1895 * dma_ops domain
1896 */
1897static bool dma_ops_domain(struct protection_domain *domain)
1898{
1899 return domain->flags & PD_DMA_OPS_MASK;
1900}
1901
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001902static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001903{
Joerg Roedel132bd682011-11-17 14:18:46 +01001904 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001905 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001906
Joerg Roedel132bd682011-11-17 14:18:46 +01001907 if (domain->mode != PAGE_MODE_NONE)
1908 pte_root = virt_to_phys(domain->pt_root);
1909
Joerg Roedel38ddf412008-09-11 10:38:32 +02001910 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1911 << DEV_ENTRY_MODE_SHIFT;
1912 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001913
Joerg Roedelee6c2862011-11-09 12:06:03 +01001914 flags = amd_iommu_dev_table[devid].data[1];
1915
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001916 if (ats)
1917 flags |= DTE_FLAG_IOTLB;
1918
Joerg Roedel52815b72011-11-17 17:24:28 +01001919 if (domain->flags & PD_IOMMUV2_MASK) {
1920 u64 gcr3 = __pa(domain->gcr3_tbl);
1921 u64 glx = domain->glx;
1922 u64 tmp;
1923
1924 pte_root |= DTE_FLAG_GV;
1925 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1926
1927 /* First mask out possible old values for GCR3 table */
1928 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1929 flags &= ~tmp;
1930
1931 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1932 flags &= ~tmp;
1933
1934 /* Encode GCR3 table into DTE */
1935 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1936 pte_root |= tmp;
1937
1938 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1939 flags |= tmp;
1940
1941 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1942 flags |= tmp;
1943 }
1944
Joerg Roedelee6c2862011-11-09 12:06:03 +01001945 flags &= ~(0xffffUL);
1946 flags |= domain->id;
1947
1948 amd_iommu_dev_table[devid].data[1] = flags;
1949 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001950}
1951
Joerg Roedel15898bb2009-11-24 15:39:42 +01001952static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001953{
Joerg Roedel355bf552008-12-08 12:02:41 +01001954 /* remove entry from the device table seen by the hardware */
1955 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1956 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01001957
Joerg Roedelc5cca142009-10-09 18:31:20 +02001958 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001959}
1960
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001961static void do_attach(struct iommu_dev_data *dev_data,
1962 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001963{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001964 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001965 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001966
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001967 iommu = amd_iommu_rlookup_table[dev_data->devid];
1968 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001969
1970 /* Update data structures */
1971 dev_data->domain = domain;
1972 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001973 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001974
1975 /* Do reference counting */
1976 domain->dev_iommu[iommu->index] += 1;
1977 domain->dev_cnt += 1;
1978
1979 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001980 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001981}
1982
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001983static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001984{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001985 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001986
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001987 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001988
Joerg Roedelc4596112009-11-20 14:57:32 +01001989 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001990 dev_data->domain->dev_iommu[iommu->index] -= 1;
1991 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001992
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001993 /* Update data structures */
1994 dev_data->domain = NULL;
1995 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001996 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001997
1998 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001999 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002000}
2001
2002/*
2003 * If a device is not yet associated with a domain, this function does
2004 * assigns it visible for the hardware
2005 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002006static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002007 struct protection_domain *domain)
2008{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002009 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002010
Joerg Roedel15898bb2009-11-24 15:39:42 +01002011 /* lock domain */
2012 spin_lock(&domain->lock);
2013
Joerg Roedel71f77582011-06-09 19:03:15 +02002014 if (dev_data->alias_data != NULL) {
2015 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002016
Joerg Roedel2b02b092011-06-09 17:48:39 +02002017 /* Some sanity checks */
2018 ret = -EBUSY;
2019 if (alias_data->domain != NULL &&
2020 alias_data->domain != domain)
2021 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002022
Joerg Roedel2b02b092011-06-09 17:48:39 +02002023 if (dev_data->domain != NULL &&
2024 dev_data->domain != domain)
2025 goto out_unlock;
2026
2027 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002028 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002029 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002030
2031 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002032 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002033
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002034 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002035 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002036
Joerg Roedel24100052009-11-25 15:59:57 +01002037 atomic_inc(&dev_data->bind);
2038
Julia Lawall84fe6c12010-05-27 12:31:51 +02002039 ret = 0;
2040
2041out_unlock:
2042
Joerg Roedel355bf552008-12-08 12:02:41 +01002043 /* ready */
2044 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002045
Julia Lawall84fe6c12010-05-27 12:31:51 +02002046 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002047}
2048
Joerg Roedel52815b72011-11-17 17:24:28 +01002049
2050static void pdev_iommuv2_disable(struct pci_dev *pdev)
2051{
2052 pci_disable_ats(pdev);
2053 pci_disable_pri(pdev);
2054 pci_disable_pasid(pdev);
2055}
2056
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002057/* FIXME: Change generic reset-function to do the same */
2058static int pri_reset_while_enabled(struct pci_dev *pdev)
2059{
2060 u16 control;
2061 int pos;
2062
Joerg Roedel46277b72011-12-07 14:34:02 +01002063 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002064 if (!pos)
2065 return -EINVAL;
2066
Joerg Roedel46277b72011-12-07 14:34:02 +01002067 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2068 control |= PCI_PRI_CTRL_RESET;
2069 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002070
2071 return 0;
2072}
2073
Joerg Roedel52815b72011-11-17 17:24:28 +01002074static int pdev_iommuv2_enable(struct pci_dev *pdev)
2075{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002076 bool reset_enable;
2077 int reqs, ret;
2078
2079 /* FIXME: Hardcode number of outstanding requests for now */
2080 reqs = 32;
2081 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2082 reqs = 1;
2083 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002084
2085 /* Only allow access to user-accessible pages */
2086 ret = pci_enable_pasid(pdev, 0);
2087 if (ret)
2088 goto out_err;
2089
2090 /* First reset the PRI state of the device */
2091 ret = pci_reset_pri(pdev);
2092 if (ret)
2093 goto out_err;
2094
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002095 /* Enable PRI */
2096 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002097 if (ret)
2098 goto out_err;
2099
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002100 if (reset_enable) {
2101 ret = pri_reset_while_enabled(pdev);
2102 if (ret)
2103 goto out_err;
2104 }
2105
Joerg Roedel52815b72011-11-17 17:24:28 +01002106 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2107 if (ret)
2108 goto out_err;
2109
2110 return 0;
2111
2112out_err:
2113 pci_disable_pri(pdev);
2114 pci_disable_pasid(pdev);
2115
2116 return ret;
2117}
2118
Joerg Roedelc99afa22011-11-21 18:19:25 +01002119/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002120#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002121
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002122static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002123{
Joerg Roedela3b93122012-04-12 12:49:26 +02002124 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002125 int pos;
2126
Joerg Roedel46277b72011-12-07 14:34:02 +01002127 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002128 if (!pos)
2129 return false;
2130
Joerg Roedela3b93122012-04-12 12:49:26 +02002131 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002132
Joerg Roedela3b93122012-04-12 12:49:26 +02002133 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002134}
2135
Joerg Roedel15898bb2009-11-24 15:39:42 +01002136/*
2137 * If a device is not yet associated with a domain, this function does
2138 * assigns it visible for the hardware
2139 */
2140static int attach_device(struct device *dev,
2141 struct protection_domain *domain)
2142{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002143 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002144 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002145 unsigned long flags;
2146 int ret;
2147
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002148 dev_data = get_dev_data(dev);
2149
Joerg Roedel52815b72011-11-17 17:24:28 +01002150 if (domain->flags & PD_IOMMUV2_MASK) {
2151 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2152 return -EINVAL;
2153
2154 if (pdev_iommuv2_enable(pdev) != 0)
2155 return -EINVAL;
2156
2157 dev_data->ats.enabled = true;
2158 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002159 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002160 } else if (amd_iommu_iotlb_sup &&
2161 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002162 dev_data->ats.enabled = true;
2163 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2164 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002165
Joerg Roedel15898bb2009-11-24 15:39:42 +01002166 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002167 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002168 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2169
2170 /*
2171 * We might boot into a crash-kernel here. The crashed kernel
2172 * left the caches in the IOMMU dirty. So we have to flush
2173 * here to evict all dirty stuff.
2174 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002175 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002176
2177 return ret;
2178}
2179
2180/*
2181 * Removes a device from a protection domain (unlocked)
2182 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002183static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002184{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002185 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002186 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002187
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002188 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002189
Joerg Roedel2ca76272010-01-22 16:45:31 +01002190 domain = dev_data->domain;
2191
2192 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002193
Joerg Roedel71f77582011-06-09 19:03:15 +02002194 if (dev_data->alias_data != NULL) {
2195 struct iommu_dev_data *alias_data = dev_data->alias_data;
2196
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002197 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002198 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002199 }
2200
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002201 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002202 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002203
Joerg Roedel2ca76272010-01-22 16:45:31 +01002204 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002205
Joerg Roedel21129f72009-09-01 11:59:42 +02002206 /*
2207 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002208 * passthrough domain if it is detached from any other domain.
2209 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002210 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002211 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002212 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002213 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002214}
2215
2216/*
2217 * Removes a device from a protection domain (with devtable_lock held)
2218 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002219static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002220{
Joerg Roedel52815b72011-11-17 17:24:28 +01002221 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002222 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002223 unsigned long flags;
2224
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002225 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002226 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002227
Joerg Roedel355bf552008-12-08 12:02:41 +01002228 /* lock device table */
2229 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002230 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002231 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002232
Joerg Roedel52815b72011-11-17 17:24:28 +01002233 if (domain->flags & PD_IOMMUV2_MASK)
2234 pdev_iommuv2_disable(to_pci_dev(dev));
2235 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002236 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002237
2238 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002239}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002240
Joerg Roedel15898bb2009-11-24 15:39:42 +01002241/*
2242 * Find out the protection domain structure for a given PCI device. This
2243 * will give us the pointer to the page table root for example.
2244 */
2245static struct protection_domain *domain_for_device(struct device *dev)
2246{
Joerg Roedel71f77582011-06-09 19:03:15 +02002247 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002248 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002249 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002250
Joerg Roedel657cbb62009-11-23 15:26:46 +01002251 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002252
Joerg Roedel2b02b092011-06-09 17:48:39 +02002253 if (dev_data->domain)
2254 return dev_data->domain;
2255
Joerg Roedel71f77582011-06-09 19:03:15 +02002256 if (dev_data->alias_data != NULL) {
2257 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002258
2259 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2260 if (alias_data->domain != NULL) {
2261 __attach_device(dev_data, alias_data->domain);
2262 dom = alias_data->domain;
2263 }
2264 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002265 }
2266
Joerg Roedel15898bb2009-11-24 15:39:42 +01002267 return dom;
2268}
2269
Joerg Roedele275a2a2008-12-10 18:27:25 +01002270static int device_change_notifier(struct notifier_block *nb,
2271 unsigned long action, void *data)
2272{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002273 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002274 struct protection_domain *domain;
2275 struct iommu_dev_data *dev_data;
2276 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002277 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002278 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002279 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002280
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002281 if (!check_device(dev))
2282 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002283
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002284 devid = get_device_id(dev);
2285 iommu = amd_iommu_rlookup_table[devid];
2286 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002287
2288 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002289 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002290
2291 domain = domain_for_device(dev);
2292
Joerg Roedele275a2a2008-12-10 18:27:25 +01002293 if (!domain)
2294 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002295 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002296 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002297 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002298 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002299 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002300
2301 iommu_init_device(dev);
2302
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002303 /*
2304 * dev_data is still NULL and
2305 * got initialized in iommu_init_device
2306 */
2307 dev_data = get_dev_data(dev);
2308
2309 if (iommu_pass_through || dev_data->iommu_v2) {
2310 dev_data->passthrough = true;
2311 attach_device(dev, pt_domain);
2312 break;
2313 }
2314
Joerg Roedel657cbb62009-11-23 15:26:46 +01002315 domain = domain_for_device(dev);
2316
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002317 /* allocate a protection domain if a device is added */
2318 dma_domain = find_protection_domain(devid);
2319 if (dma_domain)
2320 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002321 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002322 if (!dma_domain)
2323 goto out;
2324 dma_domain->target_dev = devid;
2325
2326 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2327 list_add_tail(&dma_domain->list, &iommu_pd_list);
2328 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2329
Joerg Roedelac1534a2012-06-21 14:52:40 +02002330 dev_data = get_dev_data(dev);
2331
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002332 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002333
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002334 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002335 case BUS_NOTIFY_DEL_DEVICE:
2336
2337 iommu_uninit_device(dev);
2338
Joerg Roedele275a2a2008-12-10 18:27:25 +01002339 default:
2340 goto out;
2341 }
2342
Joerg Roedele275a2a2008-12-10 18:27:25 +01002343 iommu_completion_wait(iommu);
2344
2345out:
2346 return 0;
2347}
2348
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302349static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002350 .notifier_call = device_change_notifier,
2351};
Joerg Roedel355bf552008-12-08 12:02:41 +01002352
Joerg Roedel8638c492009-12-10 11:12:25 +01002353void amd_iommu_init_notifier(void)
2354{
2355 bus_register_notifier(&pci_bus_type, &device_nb);
2356}
2357
Joerg Roedel431b2a22008-07-11 17:14:22 +02002358/*****************************************************************************
2359 *
2360 * The next functions belong to the dma_ops mapping/unmapping code.
2361 *
2362 *****************************************************************************/
2363
2364/*
2365 * In the dma_ops path we only have the struct device. This function
2366 * finds the corresponding IOMMU, the protection domain and the
2367 * requestor id for a given device.
2368 * If the device is not yet associated with a domain this is also done
2369 * in this function.
2370 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002371static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002372{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002373 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002374 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002375 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002376
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002377 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002378 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002379
Joerg Roedel94f6d192009-11-24 16:40:02 +01002380 domain = domain_for_device(dev);
2381 if (domain != NULL && !dma_ops_domain(domain))
2382 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002383
Joerg Roedel94f6d192009-11-24 16:40:02 +01002384 if (domain != NULL)
2385 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002386
Joerg Roedel15898bb2009-11-24 15:39:42 +01002387 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002388 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002389 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002390 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2391 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002392 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002393 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002394
Joerg Roedel94f6d192009-11-24 16:40:02 +01002395 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002396}
2397
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002398static void update_device_table(struct protection_domain *domain)
2399{
Joerg Roedel492667d2009-11-27 13:25:47 +01002400 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002401
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002402 list_for_each_entry(dev_data, &domain->dev_list, list)
2403 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002404}
2405
2406static void update_domain(struct protection_domain *domain)
2407{
2408 if (!domain->updated)
2409 return;
2410
2411 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002412
2413 domain_flush_devices(domain);
2414 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002415
2416 domain->updated = false;
2417}
2418
Joerg Roedel431b2a22008-07-11 17:14:22 +02002419/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002420 * This function fetches the PTE for a given address in the aperture
2421 */
2422static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2423 unsigned long address)
2424{
Joerg Roedel384de722009-05-15 12:30:05 +02002425 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002426 u64 *pte, *pte_page;
2427
Joerg Roedel384de722009-05-15 12:30:05 +02002428 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2429 if (!aperture)
2430 return NULL;
2431
2432 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002433 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002434 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002435 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002436 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2437 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002438 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002439
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002440 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002441
2442 return pte;
2443}
2444
2445/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002446 * This is the generic map function. It maps one 4kb page at paddr to
2447 * the given address in the DMA address space for the domain.
2448 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002449static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002450 unsigned long address,
2451 phys_addr_t paddr,
2452 int direction)
2453{
2454 u64 *pte, __pte;
2455
2456 WARN_ON(address > dom->aperture_size);
2457
2458 paddr &= PAGE_MASK;
2459
Joerg Roedel8bda3092009-05-12 12:02:46 +02002460 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002461 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002462 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002463
2464 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2465
2466 if (direction == DMA_TO_DEVICE)
2467 __pte |= IOMMU_PTE_IR;
2468 else if (direction == DMA_FROM_DEVICE)
2469 __pte |= IOMMU_PTE_IW;
2470 else if (direction == DMA_BIDIRECTIONAL)
2471 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2472
2473 WARN_ON(*pte);
2474
2475 *pte = __pte;
2476
2477 return (dma_addr_t)address;
2478}
2479
Joerg Roedel431b2a22008-07-11 17:14:22 +02002480/*
2481 * The generic unmapping function for on page in the DMA address space.
2482 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002483static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002484 unsigned long address)
2485{
Joerg Roedel384de722009-05-15 12:30:05 +02002486 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002487 u64 *pte;
2488
2489 if (address >= dom->aperture_size)
2490 return;
2491
Joerg Roedel384de722009-05-15 12:30:05 +02002492 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2493 if (!aperture)
2494 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002495
Joerg Roedel384de722009-05-15 12:30:05 +02002496 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2497 if (!pte)
2498 return;
2499
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002500 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002501
2502 WARN_ON(!*pte);
2503
2504 *pte = 0ULL;
2505}
2506
Joerg Roedel431b2a22008-07-11 17:14:22 +02002507/*
2508 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002509 * contiguous memory region into DMA address space. It is used by all
2510 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002511 * Must be called with the domain lock held.
2512 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002513static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002514 struct dma_ops_domain *dma_dom,
2515 phys_addr_t paddr,
2516 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002517 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002518 bool align,
2519 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002520{
2521 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002522 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002523 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002524 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002525 int i;
2526
Joerg Roedele3c449f2008-10-15 22:02:11 -07002527 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002528 paddr &= PAGE_MASK;
2529
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002530 INC_STATS_COUNTER(total_map_requests);
2531
Joerg Roedelc1858972008-12-12 15:42:39 +01002532 if (pages > 1)
2533 INC_STATS_COUNTER(cross_page);
2534
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002535 if (align)
2536 align_mask = (1UL << get_order(size)) - 1;
2537
Joerg Roedel11b83882009-05-19 10:23:15 +02002538retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002539 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2540 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002541 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002542 /*
2543 * setting next_address here will let the address
2544 * allocator only scan the new allocated range in the
2545 * first run. This is a small optimization.
2546 */
2547 dma_dom->next_address = dma_dom->aperture_size;
2548
Joerg Roedel576175c2009-11-23 19:08:46 +01002549 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002550 goto out;
2551
2552 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002553 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002554 * allocation again
2555 */
2556 goto retry;
2557 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002558
2559 start = address;
2560 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002561 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002562 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002563 goto out_unmap;
2564
Joerg Roedelcb76c322008-06-26 21:28:00 +02002565 paddr += PAGE_SIZE;
2566 start += PAGE_SIZE;
2567 }
2568 address += offset;
2569
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002570 ADD_STATS_COUNTER(alloced_io_mem, size);
2571
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002572 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002573 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002574 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002575 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002576 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002577
Joerg Roedelcb76c322008-06-26 21:28:00 +02002578out:
2579 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002580
2581out_unmap:
2582
2583 for (--i; i >= 0; --i) {
2584 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002585 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002586 }
2587
2588 dma_ops_free_addresses(dma_dom, address, pages);
2589
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002590 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002591}
2592
Joerg Roedel431b2a22008-07-11 17:14:22 +02002593/*
2594 * Does the reverse of the __map_single function. Must be called with
2595 * the domain lock held too
2596 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002597static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002598 dma_addr_t dma_addr,
2599 size_t size,
2600 int dir)
2601{
Joerg Roedel04e04632010-09-23 16:12:48 +02002602 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002603 dma_addr_t i, start;
2604 unsigned int pages;
2605
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002606 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002607 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002608 return;
2609
Joerg Roedel04e04632010-09-23 16:12:48 +02002610 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002611 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002612 dma_addr &= PAGE_MASK;
2613 start = dma_addr;
2614
2615 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002616 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002617 start += PAGE_SIZE;
2618 }
2619
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002620 SUB_STATS_COUNTER(alloced_io_mem, size);
2621
Joerg Roedelcb76c322008-06-26 21:28:00 +02002622 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002623
Joerg Roedel80be3082008-11-06 14:59:05 +01002624 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002625 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002626 dma_dom->need_flush = false;
2627 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002628}
2629
Joerg Roedel431b2a22008-07-11 17:14:22 +02002630/*
2631 * The exported map_single function for dma_ops.
2632 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002633static dma_addr_t map_page(struct device *dev, struct page *page,
2634 unsigned long offset, size_t size,
2635 enum dma_data_direction dir,
2636 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002637{
2638 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002639 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002640 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002641 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002642 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002643
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002644 INC_STATS_COUNTER(cnt_map_single);
2645
Joerg Roedel94f6d192009-11-24 16:40:02 +01002646 domain = get_domain(dev);
2647 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002648 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002649 else if (IS_ERR(domain))
2650 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002651
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002652 dma_mask = *dev->dma_mask;
2653
Joerg Roedel4da70b92008-06-26 21:28:01 +02002654 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002655
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002656 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002657 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002658 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002659 goto out;
2660
Joerg Roedel17b124b2011-04-06 18:01:35 +02002661 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002662
2663out:
2664 spin_unlock_irqrestore(&domain->lock, flags);
2665
2666 return addr;
2667}
2668
Joerg Roedel431b2a22008-07-11 17:14:22 +02002669/*
2670 * The exported unmap_single function for dma_ops.
2671 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002672static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2673 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002674{
2675 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002676 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002677
Joerg Roedel146a6912008-12-12 15:07:12 +01002678 INC_STATS_COUNTER(cnt_unmap_single);
2679
Joerg Roedel94f6d192009-11-24 16:40:02 +01002680 domain = get_domain(dev);
2681 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002682 return;
2683
Joerg Roedel4da70b92008-06-26 21:28:01 +02002684 spin_lock_irqsave(&domain->lock, flags);
2685
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002686 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002687
Joerg Roedel17b124b2011-04-06 18:01:35 +02002688 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002689
2690 spin_unlock_irqrestore(&domain->lock, flags);
2691}
2692
Joerg Roedel431b2a22008-07-11 17:14:22 +02002693/*
2694 * This is a special map_sg function which is used if we should map a
2695 * device which is not handled by an AMD IOMMU in the system.
2696 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002697static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2698 int nelems, int dir)
2699{
2700 struct scatterlist *s;
2701 int i;
2702
2703 for_each_sg(sglist, s, nelems, i) {
2704 s->dma_address = (dma_addr_t)sg_phys(s);
2705 s->dma_length = s->length;
2706 }
2707
2708 return nelems;
2709}
2710
Joerg Roedel431b2a22008-07-11 17:14:22 +02002711/*
2712 * The exported map_sg function for dma_ops (handles scatter-gather
2713 * lists).
2714 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002715static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002716 int nelems, enum dma_data_direction dir,
2717 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002718{
2719 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002720 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002721 int i;
2722 struct scatterlist *s;
2723 phys_addr_t paddr;
2724 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002725 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002726
Joerg Roedeld03f0672008-12-12 15:09:48 +01002727 INC_STATS_COUNTER(cnt_map_sg);
2728
Joerg Roedel94f6d192009-11-24 16:40:02 +01002729 domain = get_domain(dev);
2730 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002731 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002732 else if (IS_ERR(domain))
2733 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002734
Joerg Roedel832a90c2008-09-18 15:54:23 +02002735 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002736
Joerg Roedel65b050a2008-06-26 21:28:02 +02002737 spin_lock_irqsave(&domain->lock, flags);
2738
2739 for_each_sg(sglist, s, nelems, i) {
2740 paddr = sg_phys(s);
2741
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002742 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002743 paddr, s->length, dir, false,
2744 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002745
2746 if (s->dma_address) {
2747 s->dma_length = s->length;
2748 mapped_elems++;
2749 } else
2750 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002751 }
2752
Joerg Roedel17b124b2011-04-06 18:01:35 +02002753 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002754
2755out:
2756 spin_unlock_irqrestore(&domain->lock, flags);
2757
2758 return mapped_elems;
2759unmap:
2760 for_each_sg(sglist, s, mapped_elems, i) {
2761 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002762 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002763 s->dma_length, dir);
2764 s->dma_address = s->dma_length = 0;
2765 }
2766
2767 mapped_elems = 0;
2768
2769 goto out;
2770}
2771
Joerg Roedel431b2a22008-07-11 17:14:22 +02002772/*
2773 * The exported map_sg function for dma_ops (handles scatter-gather
2774 * lists).
2775 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002776static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002777 int nelems, enum dma_data_direction dir,
2778 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002779{
2780 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002781 struct protection_domain *domain;
2782 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002783 int i;
2784
Joerg Roedel55877a62008-12-12 15:12:14 +01002785 INC_STATS_COUNTER(cnt_unmap_sg);
2786
Joerg Roedel94f6d192009-11-24 16:40:02 +01002787 domain = get_domain(dev);
2788 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002789 return;
2790
Joerg Roedel65b050a2008-06-26 21:28:02 +02002791 spin_lock_irqsave(&domain->lock, flags);
2792
2793 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002794 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002795 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002796 s->dma_address = s->dma_length = 0;
2797 }
2798
Joerg Roedel17b124b2011-04-06 18:01:35 +02002799 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002800
2801 spin_unlock_irqrestore(&domain->lock, flags);
2802}
2803
Joerg Roedel431b2a22008-07-11 17:14:22 +02002804/*
2805 * The exported alloc_coherent function for dma_ops.
2806 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002807static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002808 dma_addr_t *dma_addr, gfp_t flag,
2809 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002810{
2811 unsigned long flags;
2812 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002813 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002814 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002815 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002816
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002817 INC_STATS_COUNTER(cnt_alloc_coherent);
2818
Joerg Roedel94f6d192009-11-24 16:40:02 +01002819 domain = get_domain(dev);
2820 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002821 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2822 *dma_addr = __pa(virt_addr);
2823 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002824 } else if (IS_ERR(domain))
2825 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002826
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002827 dma_mask = dev->coherent_dma_mask;
2828 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2829 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002830
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002831 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2832 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302833 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002834
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002835 paddr = virt_to_phys(virt_addr);
2836
Joerg Roedel832a90c2008-09-18 15:54:23 +02002837 if (!dma_mask)
2838 dma_mask = *dev->dma_mask;
2839
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002840 spin_lock_irqsave(&domain->lock, flags);
2841
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002842 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002843 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002844
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002845 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002846 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002847 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002848 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002849
Joerg Roedel17b124b2011-04-06 18:01:35 +02002850 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002851
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002852 spin_unlock_irqrestore(&domain->lock, flags);
2853
2854 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002855
2856out_free:
2857
2858 free_pages((unsigned long)virt_addr, get_order(size));
2859
2860 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002861}
2862
Joerg Roedel431b2a22008-07-11 17:14:22 +02002863/*
2864 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002865 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002866static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002867 void *virt_addr, dma_addr_t dma_addr,
2868 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002869{
2870 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002871 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002872
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002873 INC_STATS_COUNTER(cnt_free_coherent);
2874
Joerg Roedel94f6d192009-11-24 16:40:02 +01002875 domain = get_domain(dev);
2876 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002877 goto free_mem;
2878
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002879 spin_lock_irqsave(&domain->lock, flags);
2880
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002881 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002882
Joerg Roedel17b124b2011-04-06 18:01:35 +02002883 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002884
2885 spin_unlock_irqrestore(&domain->lock, flags);
2886
2887free_mem:
2888 free_pages((unsigned long)virt_addr, get_order(size));
2889}
2890
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002891/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002892 * This function is called by the DMA layer to find out if we can handle a
2893 * particular device. It is part of the dma_ops.
2894 */
2895static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2896{
Joerg Roedel420aef82009-11-23 16:14:57 +01002897 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002898}
2899
2900/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002901 * The function for pre-allocating protection domains.
2902 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002903 * If the driver core informs the DMA layer if a driver grabs a device
2904 * we don't need to preallocate the protection domains anymore.
2905 * For now we have to.
2906 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01002907static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002908{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002909 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002910 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002911 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002912 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002913
Chris Wrightd18c69d2010-04-02 18:27:55 -07002914 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002915
2916 /* Do we handle this device? */
2917 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002918 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002919
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002920 dev_data = get_dev_data(&dev->dev);
2921 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2922 /* Make sure passthrough domain is allocated */
2923 alloc_passthrough_domain();
2924 dev_data->passthrough = true;
2925 attach_device(&dev->dev, pt_domain);
2926 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2927 dev_name(&dev->dev));
2928 }
2929
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002930 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002931 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002932 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002933
2934 devid = get_device_id(&dev->dev);
2935
Joerg Roedel87a64d52009-11-24 17:26:43 +01002936 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002937 if (!dma_dom)
2938 continue;
2939 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002940 dma_dom->target_dev = devid;
2941
Joerg Roedel15898bb2009-11-24 15:39:42 +01002942 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002943
Joerg Roedelbd60b732008-09-11 10:24:48 +02002944 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002945 }
2946}
2947
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002948static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002949 .alloc = alloc_coherent,
2950 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002951 .map_page = map_page,
2952 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002953 .map_sg = map_sg,
2954 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002955 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002956};
2957
Joerg Roedel27c21272011-05-30 15:56:24 +02002958static unsigned device_dma_ops_init(void)
2959{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002960 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002961 struct pci_dev *pdev = NULL;
2962 unsigned unhandled = 0;
2963
2964 for_each_pci_dev(pdev) {
2965 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01002966
2967 iommu_ignore_device(&pdev->dev);
2968
Joerg Roedel27c21272011-05-30 15:56:24 +02002969 unhandled += 1;
2970 continue;
2971 }
2972
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002973 dev_data = get_dev_data(&pdev->dev);
2974
2975 if (!dev_data->passthrough)
2976 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2977 else
2978 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02002979 }
2980
2981 return unhandled;
2982}
2983
Joerg Roedel431b2a22008-07-11 17:14:22 +02002984/*
2985 * The function which clues the AMD IOMMU driver into dma_ops.
2986 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002987
2988void __init amd_iommu_init_api(void)
2989{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02002990 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002991}
2992
Joerg Roedel6631ee92008-06-26 21:28:05 +02002993int __init amd_iommu_init_dma_ops(void)
2994{
2995 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002996 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002997
Joerg Roedel431b2a22008-07-11 17:14:22 +02002998 /*
2999 * first allocate a default protection domain for every IOMMU we
3000 * found in the system. Devices not assigned to any other
3001 * protection domain will be assigned to the default one.
3002 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003003 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003004 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003005 if (iommu->default_dom == NULL)
3006 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003007 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003008 ret = iommu_init_unity_mappings(iommu);
3009 if (ret)
3010 goto free_domains;
3011 }
3012
Joerg Roedel431b2a22008-07-11 17:14:22 +02003013 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003014 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003015 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003016 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003017
3018 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003019 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003020
Joerg Roedel431b2a22008-07-11 17:14:22 +02003021 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003022 unhandled = device_dma_ops_init();
3023 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3024 /* There are unhandled devices - initialize swiotlb for them */
3025 swiotlb = 1;
3026 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003027
Joerg Roedel7f265082008-12-12 13:50:21 +01003028 amd_iommu_stats_init();
3029
Joerg Roedel62410ee2012-06-12 16:42:43 +02003030 if (amd_iommu_unmap_flush)
3031 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3032 else
3033 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3034
Joerg Roedel6631ee92008-06-26 21:28:05 +02003035 return 0;
3036
3037free_domains:
3038
Joerg Roedel3bd22172009-05-04 15:06:20 +02003039 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003040 if (iommu->default_dom)
3041 dma_ops_domain_free(iommu->default_dom);
3042 }
3043
3044 return ret;
3045}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003046
3047/*****************************************************************************
3048 *
3049 * The following functions belong to the exported interface of AMD IOMMU
3050 *
3051 * This interface allows access to lower level functions of the IOMMU
3052 * like protection domain handling and assignement of devices to domains
3053 * which is not possible with the dma_ops interface.
3054 *
3055 *****************************************************************************/
3056
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003057static void cleanup_domain(struct protection_domain *domain)
3058{
Joerg Roedel492667d2009-11-27 13:25:47 +01003059 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003060 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003061
3062 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3063
Joerg Roedel492667d2009-11-27 13:25:47 +01003064 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003065 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003066 atomic_set(&dev_data->bind, 0);
3067 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003068
3069 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3070}
3071
Joerg Roedel26508152009-08-26 16:52:40 +02003072static void protection_domain_free(struct protection_domain *domain)
3073{
3074 if (!domain)
3075 return;
3076
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003077 del_domain_from_list(domain);
3078
Joerg Roedel26508152009-08-26 16:52:40 +02003079 if (domain->id)
3080 domain_id_free(domain->id);
3081
3082 kfree(domain);
3083}
3084
3085static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003086{
3087 struct protection_domain *domain;
3088
3089 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3090 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003091 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003092
3093 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003094 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003095 domain->id = domain_id_alloc();
3096 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003097 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003098 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003099
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003100 add_domain_to_list(domain);
3101
Joerg Roedel26508152009-08-26 16:52:40 +02003102 return domain;
3103
3104out_err:
3105 kfree(domain);
3106
3107 return NULL;
3108}
3109
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003110static int __init alloc_passthrough_domain(void)
3111{
3112 if (pt_domain != NULL)
3113 return 0;
3114
3115 /* allocate passthrough domain */
3116 pt_domain = protection_domain_alloc();
3117 if (!pt_domain)
3118 return -ENOMEM;
3119
3120 pt_domain->mode = PAGE_MODE_NONE;
3121
3122 return 0;
3123}
Joerg Roedel26508152009-08-26 16:52:40 +02003124static int amd_iommu_domain_init(struct iommu_domain *dom)
3125{
3126 struct protection_domain *domain;
3127
3128 domain = protection_domain_alloc();
3129 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003130 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003131
3132 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003133 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3134 if (!domain->pt_root)
3135 goto out_free;
3136
Joerg Roedelf3572db2011-11-23 12:36:25 +01003137 domain->iommu_domain = dom;
3138
Joerg Roedelc156e342008-12-02 18:13:27 +01003139 dom->priv = domain;
3140
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003141 dom->geometry.aperture_start = 0;
3142 dom->geometry.aperture_end = ~0ULL;
3143 dom->geometry.force_aperture = true;
3144
Joerg Roedelc156e342008-12-02 18:13:27 +01003145 return 0;
3146
3147out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003148 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003149
3150 return -ENOMEM;
3151}
3152
Joerg Roedel98383fc2008-12-02 18:34:12 +01003153static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3154{
3155 struct protection_domain *domain = dom->priv;
3156
3157 if (!domain)
3158 return;
3159
3160 if (domain->dev_cnt > 0)
3161 cleanup_domain(domain);
3162
3163 BUG_ON(domain->dev_cnt != 0);
3164
Joerg Roedel132bd682011-11-17 14:18:46 +01003165 if (domain->mode != PAGE_MODE_NONE)
3166 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003167
Joerg Roedel52815b72011-11-17 17:24:28 +01003168 if (domain->flags & PD_IOMMUV2_MASK)
3169 free_gcr3_table(domain);
3170
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003171 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003172
3173 dom->priv = NULL;
3174}
3175
Joerg Roedel684f2882008-12-08 12:07:44 +01003176static void amd_iommu_detach_device(struct iommu_domain *dom,
3177 struct device *dev)
3178{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003179 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003180 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003181 u16 devid;
3182
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003183 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003184 return;
3185
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003186 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003187
Joerg Roedel657cbb62009-11-23 15:26:46 +01003188 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003189 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003190
3191 iommu = amd_iommu_rlookup_table[devid];
3192 if (!iommu)
3193 return;
3194
Joerg Roedel684f2882008-12-08 12:07:44 +01003195 iommu_completion_wait(iommu);
3196}
3197
Joerg Roedel01106062008-12-02 19:34:11 +01003198static int amd_iommu_attach_device(struct iommu_domain *dom,
3199 struct device *dev)
3200{
3201 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003202 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003203 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003204 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003205
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003206 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003207 return -EINVAL;
3208
Joerg Roedel657cbb62009-11-23 15:26:46 +01003209 dev_data = dev->archdata.iommu;
3210
Joerg Roedelf62dda62011-06-09 12:55:35 +02003211 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003212 if (!iommu)
3213 return -EINVAL;
3214
Joerg Roedel657cbb62009-11-23 15:26:46 +01003215 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003216 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003217
Joerg Roedel15898bb2009-11-24 15:39:42 +01003218 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003219
3220 iommu_completion_wait(iommu);
3221
Joerg Roedel15898bb2009-11-24 15:39:42 +01003222 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003223}
3224
Joerg Roedel468e2362010-01-21 16:37:36 +01003225static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003226 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003227{
3228 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003229 int prot = 0;
3230 int ret;
3231
Joerg Roedel132bd682011-11-17 14:18:46 +01003232 if (domain->mode == PAGE_MODE_NONE)
3233 return -EINVAL;
3234
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003235 if (iommu_prot & IOMMU_READ)
3236 prot |= IOMMU_PROT_IR;
3237 if (iommu_prot & IOMMU_WRITE)
3238 prot |= IOMMU_PROT_IW;
3239
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003240 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003241 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003242 mutex_unlock(&domain->api_lock);
3243
Joerg Roedel795e74f72010-05-11 17:40:57 +02003244 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003245}
3246
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003247static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3248 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003249{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003250 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003251 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003252
Joerg Roedel132bd682011-11-17 14:18:46 +01003253 if (domain->mode == PAGE_MODE_NONE)
3254 return -EINVAL;
3255
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003256 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003257 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003258 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003259
Joerg Roedel17b124b2011-04-06 18:01:35 +02003260 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003261
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003262 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003263}
3264
Joerg Roedel645c4c82008-12-02 20:05:50 +01003265static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3266 unsigned long iova)
3267{
3268 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003269 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003270 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003271 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003272
Joerg Roedel132bd682011-11-17 14:18:46 +01003273 if (domain->mode == PAGE_MODE_NONE)
3274 return iova;
3275
Joerg Roedel24cd7722010-01-19 17:27:39 +01003276 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003277
Joerg Roedela6d41a42009-09-02 17:08:55 +02003278 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003279 return 0;
3280
Joerg Roedelf03152b2010-01-21 16:15:24 +01003281 if (PM_PTE_LEVEL(*pte) == 0)
3282 offset_mask = PAGE_SIZE - 1;
3283 else
3284 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3285
3286 __pte = *pte & PM_ADDR_MASK;
3287 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003288
3289 return paddr;
3290}
3291
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003292static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3293 unsigned long cap)
3294{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003295 switch (cap) {
3296 case IOMMU_CAP_CACHE_COHERENCY:
3297 return 1;
3298 }
3299
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003300 return 0;
3301}
3302
Joerg Roedel26961ef2008-12-03 17:00:17 +01003303static struct iommu_ops amd_iommu_ops = {
3304 .domain_init = amd_iommu_domain_init,
3305 .domain_destroy = amd_iommu_domain_destroy,
3306 .attach_dev = amd_iommu_attach_device,
3307 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003308 .map = amd_iommu_map,
3309 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003310 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003311 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003312 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003313};
3314
Joerg Roedel0feae532009-08-26 15:26:30 +02003315/*****************************************************************************
3316 *
3317 * The next functions do a basic initialization of IOMMU for pass through
3318 * mode
3319 *
3320 * In passthrough mode the IOMMU is initialized and enabled but not used for
3321 * DMA-API translation.
3322 *
3323 *****************************************************************************/
3324
3325int __init amd_iommu_init_passthrough(void)
3326{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003327 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003328 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003329 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003330 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003331 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003332
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003333 ret = alloc_passthrough_domain();
3334 if (ret)
3335 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003336
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003337 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003338 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003339 continue;
3340
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003341 dev_data = get_dev_data(&dev->dev);
3342 dev_data->passthrough = true;
3343
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003344 devid = get_device_id(&dev->dev);
3345
Joerg Roedel15898bb2009-11-24 15:39:42 +01003346 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003347 if (!iommu)
3348 continue;
3349
Joerg Roedel15898bb2009-11-24 15:39:42 +01003350 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003351 }
3352
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003353 amd_iommu_stats_init();
3354
Joerg Roedel0feae532009-08-26 15:26:30 +02003355 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3356
3357 return 0;
3358}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003359
3360/* IOMMUv2 specific functions */
3361int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3362{
3363 return atomic_notifier_chain_register(&ppr_notifier, nb);
3364}
3365EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3366
3367int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3368{
3369 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3370}
3371EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003372
3373void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3374{
3375 struct protection_domain *domain = dom->priv;
3376 unsigned long flags;
3377
3378 spin_lock_irqsave(&domain->lock, flags);
3379
3380 /* Update data structure */
3381 domain->mode = PAGE_MODE_NONE;
3382 domain->updated = true;
3383
3384 /* Make changes visible to IOMMUs */
3385 update_domain(domain);
3386
3387 /* Page-table is not visible to IOMMU anymore, so free it */
3388 free_pagetable(domain);
3389
3390 spin_unlock_irqrestore(&domain->lock, flags);
3391}
3392EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003393
3394int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3395{
3396 struct protection_domain *domain = dom->priv;
3397 unsigned long flags;
3398 int levels, ret;
3399
3400 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3401 return -EINVAL;
3402
3403 /* Number of GCR3 table levels required */
3404 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3405 levels += 1;
3406
3407 if (levels > amd_iommu_max_glx_val)
3408 return -EINVAL;
3409
3410 spin_lock_irqsave(&domain->lock, flags);
3411
3412 /*
3413 * Save us all sanity checks whether devices already in the
3414 * domain support IOMMUv2. Just force that the domain has no
3415 * devices attached when it is switched into IOMMUv2 mode.
3416 */
3417 ret = -EBUSY;
3418 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3419 goto out;
3420
3421 ret = -ENOMEM;
3422 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3423 if (domain->gcr3_tbl == NULL)
3424 goto out;
3425
3426 domain->glx = levels;
3427 domain->flags |= PD_IOMMUV2_MASK;
3428 domain->updated = true;
3429
3430 update_domain(domain);
3431
3432 ret = 0;
3433
3434out:
3435 spin_unlock_irqrestore(&domain->lock, flags);
3436
3437 return ret;
3438}
3439EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003440
3441static int __flush_pasid(struct protection_domain *domain, int pasid,
3442 u64 address, bool size)
3443{
3444 struct iommu_dev_data *dev_data;
3445 struct iommu_cmd cmd;
3446 int i, ret;
3447
3448 if (!(domain->flags & PD_IOMMUV2_MASK))
3449 return -EINVAL;
3450
3451 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3452
3453 /*
3454 * IOMMU TLB needs to be flushed before Device TLB to
3455 * prevent device TLB refill from IOMMU TLB
3456 */
3457 for (i = 0; i < amd_iommus_present; ++i) {
3458 if (domain->dev_iommu[i] == 0)
3459 continue;
3460
3461 ret = iommu_queue_command(amd_iommus[i], &cmd);
3462 if (ret != 0)
3463 goto out;
3464 }
3465
3466 /* Wait until IOMMU TLB flushes are complete */
3467 domain_flush_complete(domain);
3468
3469 /* Now flush device TLBs */
3470 list_for_each_entry(dev_data, &domain->dev_list, list) {
3471 struct amd_iommu *iommu;
3472 int qdep;
3473
3474 BUG_ON(!dev_data->ats.enabled);
3475
3476 qdep = dev_data->ats.qdep;
3477 iommu = amd_iommu_rlookup_table[dev_data->devid];
3478
3479 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3480 qdep, address, size);
3481
3482 ret = iommu_queue_command(iommu, &cmd);
3483 if (ret != 0)
3484 goto out;
3485 }
3486
3487 /* Wait until all device TLBs are flushed */
3488 domain_flush_complete(domain);
3489
3490 ret = 0;
3491
3492out:
3493
3494 return ret;
3495}
3496
3497static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3498 u64 address)
3499{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003500 INC_STATS_COUNTER(invalidate_iotlb);
3501
Joerg Roedel22e266c2011-11-21 15:59:08 +01003502 return __flush_pasid(domain, pasid, address, false);
3503}
3504
3505int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3506 u64 address)
3507{
3508 struct protection_domain *domain = dom->priv;
3509 unsigned long flags;
3510 int ret;
3511
3512 spin_lock_irqsave(&domain->lock, flags);
3513 ret = __amd_iommu_flush_page(domain, pasid, address);
3514 spin_unlock_irqrestore(&domain->lock, flags);
3515
3516 return ret;
3517}
3518EXPORT_SYMBOL(amd_iommu_flush_page);
3519
3520static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3521{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003522 INC_STATS_COUNTER(invalidate_iotlb_all);
3523
Joerg Roedel22e266c2011-11-21 15:59:08 +01003524 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3525 true);
3526}
3527
3528int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
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_tlb(domain, pasid);
3536 spin_unlock_irqrestore(&domain->lock, flags);
3537
3538 return ret;
3539}
3540EXPORT_SYMBOL(amd_iommu_flush_tlb);
3541
Joerg Roedelb16137b2011-11-21 16:50:23 +01003542static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3543{
3544 int index;
3545 u64 *pte;
3546
3547 while (true) {
3548
3549 index = (pasid >> (9 * level)) & 0x1ff;
3550 pte = &root[index];
3551
3552 if (level == 0)
3553 break;
3554
3555 if (!(*pte & GCR3_VALID)) {
3556 if (!alloc)
3557 return NULL;
3558
3559 root = (void *)get_zeroed_page(GFP_ATOMIC);
3560 if (root == NULL)
3561 return NULL;
3562
3563 *pte = __pa(root) | GCR3_VALID;
3564 }
3565
3566 root = __va(*pte & PAGE_MASK);
3567
3568 level -= 1;
3569 }
3570
3571 return pte;
3572}
3573
3574static int __set_gcr3(struct protection_domain *domain, int pasid,
3575 unsigned long cr3)
3576{
3577 u64 *pte;
3578
3579 if (domain->mode != PAGE_MODE_NONE)
3580 return -EINVAL;
3581
3582 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3583 if (pte == NULL)
3584 return -ENOMEM;
3585
3586 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3587
3588 return __amd_iommu_flush_tlb(domain, pasid);
3589}
3590
3591static int __clear_gcr3(struct protection_domain *domain, int pasid)
3592{
3593 u64 *pte;
3594
3595 if (domain->mode != PAGE_MODE_NONE)
3596 return -EINVAL;
3597
3598 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3599 if (pte == NULL)
3600 return 0;
3601
3602 *pte = 0;
3603
3604 return __amd_iommu_flush_tlb(domain, pasid);
3605}
3606
3607int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3608 unsigned long cr3)
3609{
3610 struct protection_domain *domain = dom->priv;
3611 unsigned long flags;
3612 int ret;
3613
3614 spin_lock_irqsave(&domain->lock, flags);
3615 ret = __set_gcr3(domain, pasid, cr3);
3616 spin_unlock_irqrestore(&domain->lock, flags);
3617
3618 return ret;
3619}
3620EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3621
3622int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3623{
3624 struct protection_domain *domain = dom->priv;
3625 unsigned long flags;
3626 int ret;
3627
3628 spin_lock_irqsave(&domain->lock, flags);
3629 ret = __clear_gcr3(domain, pasid);
3630 spin_unlock_irqrestore(&domain->lock, flags);
3631
3632 return ret;
3633}
3634EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003635
3636int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3637 int status, int tag)
3638{
3639 struct iommu_dev_data *dev_data;
3640 struct amd_iommu *iommu;
3641 struct iommu_cmd cmd;
3642
Joerg Roedel399be2f2011-12-01 16:53:47 +01003643 INC_STATS_COUNTER(complete_ppr);
3644
Joerg Roedelc99afa22011-11-21 18:19:25 +01003645 dev_data = get_dev_data(&pdev->dev);
3646 iommu = amd_iommu_rlookup_table[dev_data->devid];
3647
3648 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3649 tag, dev_data->pri_tlp);
3650
3651 return iommu_queue_command(iommu, &cmd);
3652}
3653EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003654
3655struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3656{
3657 struct protection_domain *domain;
3658
3659 domain = get_domain(&pdev->dev);
3660 if (IS_ERR(domain))
3661 return NULL;
3662
3663 /* Only return IOMMUv2 domains */
3664 if (!(domain->flags & PD_IOMMUV2_MASK))
3665 return NULL;
3666
3667 return domain->iommu_domain;
3668}
3669EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003670
3671void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3672{
3673 struct iommu_dev_data *dev_data;
3674
3675 if (!amd_iommu_v2_supported())
3676 return;
3677
3678 dev_data = get_dev_data(&pdev->dev);
3679 dev_data->errata |= (1 << erratum);
3680}
3681EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003682
3683int amd_iommu_device_info(struct pci_dev *pdev,
3684 struct amd_iommu_device_info *info)
3685{
3686 int max_pasids;
3687 int pos;
3688
3689 if (pdev == NULL || info == NULL)
3690 return -EINVAL;
3691
3692 if (!amd_iommu_v2_supported())
3693 return -EINVAL;
3694
3695 memset(info, 0, sizeof(*info));
3696
3697 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3698 if (pos)
3699 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3700
3701 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3702 if (pos)
3703 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3704
3705 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3706 if (pos) {
3707 int features;
3708
3709 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3710 max_pasids = min(max_pasids, (1 << 20));
3711
3712 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3713 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3714
3715 features = pci_pasid_features(pdev);
3716 if (features & PCI_PASID_CAP_EXEC)
3717 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3718 if (features & PCI_PASID_CAP_PRIV)
3719 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3720 }
3721
3722 return 0;
3723}
3724EXPORT_SYMBOL(amd_iommu_device_info);