blob: 60ea92065d31a0380ec233b8e9255f00071d1fbf [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 Roedel431b2a22008-07-11 17:14:22 +020086/*
87 * general struct to manage commands send to an IOMMU
88 */
Joerg Roedeld6449532008-07-11 17:14:28 +020089struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020090 u32 data[4];
91};
92
Joerg Roedel04bfdd82009-09-02 16:00:23 +020093static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +010094static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -070095
Joerg Roedel15898bb2009-11-24 15:39:42 +010096/****************************************************************************
97 *
98 * Helper functions
99 *
100 ****************************************************************************/
101
Joerg Roedelf62dda62011-06-09 12:55:35 +0200102static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200103{
104 struct iommu_dev_data *dev_data;
105 unsigned long flags;
106
107 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
108 if (!dev_data)
109 return NULL;
110
Joerg Roedelf62dda62011-06-09 12:55:35 +0200111 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200112 atomic_set(&dev_data->bind, 0);
113
114 spin_lock_irqsave(&dev_data_list_lock, flags);
115 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
116 spin_unlock_irqrestore(&dev_data_list_lock, flags);
117
118 return dev_data;
119}
120
121static void free_dev_data(struct iommu_dev_data *dev_data)
122{
123 unsigned long flags;
124
125 spin_lock_irqsave(&dev_data_list_lock, flags);
126 list_del(&dev_data->dev_data_list);
127 spin_unlock_irqrestore(&dev_data_list_lock, flags);
128
129 kfree(dev_data);
130}
131
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200132static struct iommu_dev_data *search_dev_data(u16 devid)
133{
134 struct iommu_dev_data *dev_data;
135 unsigned long flags;
136
137 spin_lock_irqsave(&dev_data_list_lock, flags);
138 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
139 if (dev_data->devid == devid)
140 goto out_unlock;
141 }
142
143 dev_data = NULL;
144
145out_unlock:
146 spin_unlock_irqrestore(&dev_data_list_lock, flags);
147
148 return dev_data;
149}
150
151static struct iommu_dev_data *find_dev_data(u16 devid)
152{
153 struct iommu_dev_data *dev_data;
154
155 dev_data = search_dev_data(devid);
156
157 if (dev_data == NULL)
158 dev_data = alloc_dev_data(devid);
159
160 return dev_data;
161}
162
Joerg Roedel15898bb2009-11-24 15:39:42 +0100163static inline u16 get_device_id(struct device *dev)
164{
165 struct pci_dev *pdev = to_pci_dev(dev);
166
167 return calc_devid(pdev->bus->number, pdev->devfn);
168}
169
Joerg Roedel657cbb62009-11-23 15:26:46 +0100170static struct iommu_dev_data *get_dev_data(struct device *dev)
171{
172 return dev->archdata.iommu;
173}
174
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100175static bool pci_iommuv2_capable(struct pci_dev *pdev)
176{
177 static const int caps[] = {
178 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100179 PCI_EXT_CAP_ID_PRI,
180 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100181 };
182 int i, pos;
183
184 for (i = 0; i < 3; ++i) {
185 pos = pci_find_ext_capability(pdev, caps[i]);
186 if (pos == 0)
187 return false;
188 }
189
190 return true;
191}
192
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100193static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
194{
195 struct iommu_dev_data *dev_data;
196
197 dev_data = get_dev_data(&pdev->dev);
198
199 return dev_data->errata & (1 << erratum) ? true : false;
200}
201
Joerg Roedel71c70982009-11-24 16:43:06 +0100202/*
203 * In this function the list of preallocated protection domains is traversed to
204 * find the domain for a specific device
205 */
206static struct dma_ops_domain *find_protection_domain(u16 devid)
207{
208 struct dma_ops_domain *entry, *ret = NULL;
209 unsigned long flags;
210 u16 alias = amd_iommu_alias_table[devid];
211
212 if (list_empty(&iommu_pd_list))
213 return NULL;
214
215 spin_lock_irqsave(&iommu_pd_list_lock, flags);
216
217 list_for_each_entry(entry, &iommu_pd_list, list) {
218 if (entry->target_dev == devid ||
219 entry->target_dev == alias) {
220 ret = entry;
221 break;
222 }
223 }
224
225 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
226
227 return ret;
228}
229
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100230/*
231 * This function checks if the driver got a valid device from the caller to
232 * avoid dereferencing invalid pointers.
233 */
234static bool check_device(struct device *dev)
235{
236 u16 devid;
237
238 if (!dev || !dev->dma_mask)
239 return false;
240
241 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100242 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100243 return false;
244
245 devid = get_device_id(dev);
246
247 /* Out of our scope? */
248 if (devid > amd_iommu_last_bdf)
249 return false;
250
251 if (amd_iommu_rlookup_table[devid] == NULL)
252 return false;
253
254 return true;
255}
256
Joerg Roedel657cbb62009-11-23 15:26:46 +0100257static int iommu_init_device(struct device *dev)
258{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600259 struct pci_dev *dma_pdev, *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100260 struct iommu_dev_data *dev_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600261 struct iommu_group *group;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200262 u16 alias;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600263 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100264
265 if (dev->archdata.iommu)
266 return 0;
267
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200268 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100269 if (!dev_data)
270 return -ENOMEM;
271
Joerg Roedelf62dda62011-06-09 12:55:35 +0200272 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200273 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200274 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100275
Joerg Roedel71f77582011-06-09 19:03:15 +0200276 alias_data = find_dev_data(alias);
277 if (alias_data == NULL) {
278 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
279 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200280 free_dev_data(dev_data);
281 return -ENOTSUPP;
282 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200283 dev_data->alias_data = alias_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600284
285 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
286 } else
287 dma_pdev = pci_dev_get(pdev);
288
289 group = iommu_group_get(&dma_pdev->dev);
290 pci_dev_put(dma_pdev);
291 if (!group) {
292 group = iommu_group_alloc();
293 if (IS_ERR(group))
294 return PTR_ERR(group);
Joerg Roedel26018872011-06-06 16:50:14 +0200295 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100296
Alex Williamson9dcd6132012-05-30 14:19:07 -0600297 ret = iommu_group_add_device(group, dev);
298
299 iommu_group_put(group);
300
301 if (ret)
302 return ret;
303
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100304 if (pci_iommuv2_capable(pdev)) {
305 struct amd_iommu *iommu;
306
307 iommu = amd_iommu_rlookup_table[dev_data->devid];
308 dev_data->iommu_v2 = iommu->is_iommu_v2;
309 }
310
Joerg Roedel657cbb62009-11-23 15:26:46 +0100311 dev->archdata.iommu = dev_data;
312
Joerg Roedel657cbb62009-11-23 15:26:46 +0100313 return 0;
314}
315
Joerg Roedel26018872011-06-06 16:50:14 +0200316static void iommu_ignore_device(struct device *dev)
317{
318 u16 devid, alias;
319
320 devid = get_device_id(dev);
321 alias = amd_iommu_alias_table[devid];
322
323 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
324 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
325
326 amd_iommu_rlookup_table[devid] = NULL;
327 amd_iommu_rlookup_table[alias] = NULL;
328}
329
Joerg Roedel657cbb62009-11-23 15:26:46 +0100330static void iommu_uninit_device(struct device *dev)
331{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600332 iommu_group_remove_device(dev);
333
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200334 /*
335 * Nothing to do here - we keep dev_data around for unplugged devices
336 * and reuse it when the device is re-plugged - not doing so would
337 * introduce a ton of races.
338 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100339}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100340
341void __init amd_iommu_uninit_devices(void)
342{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200343 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100344 struct pci_dev *pdev = NULL;
345
346 for_each_pci_dev(pdev) {
347
348 if (!check_device(&pdev->dev))
349 continue;
350
351 iommu_uninit_device(&pdev->dev);
352 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200353
354 /* Free all of our dev_data structures */
355 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
356 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100357}
358
359int __init amd_iommu_init_devices(void)
360{
361 struct pci_dev *pdev = NULL;
362 int ret = 0;
363
364 for_each_pci_dev(pdev) {
365
366 if (!check_device(&pdev->dev))
367 continue;
368
369 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200370 if (ret == -ENOTSUPP)
371 iommu_ignore_device(&pdev->dev);
372 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100373 goto out_free;
374 }
375
376 return 0;
377
378out_free:
379
380 amd_iommu_uninit_devices();
381
382 return ret;
383}
Joerg Roedel7f265082008-12-12 13:50:21 +0100384#ifdef CONFIG_AMD_IOMMU_STATS
385
386/*
387 * Initialization code for statistics collection
388 */
389
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100390DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100391DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100392DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100393DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100394DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100395DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100396DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100397DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100398DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100399DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100400DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100401DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100402DECLARE_STATS_COUNTER(complete_ppr);
403DECLARE_STATS_COUNTER(invalidate_iotlb);
404DECLARE_STATS_COUNTER(invalidate_iotlb_all);
405DECLARE_STATS_COUNTER(pri_requests);
406
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100407
Joerg Roedel7f265082008-12-12 13:50:21 +0100408static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100409static struct dentry *de_fflush;
410
411static void amd_iommu_stats_add(struct __iommu_counter *cnt)
412{
413 if (stats_dir == NULL)
414 return;
415
416 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
417 &cnt->value);
418}
419
420static void amd_iommu_stats_init(void)
421{
422 stats_dir = debugfs_create_dir("amd-iommu", NULL);
423 if (stats_dir == NULL)
424 return;
425
Joerg Roedel7f265082008-12-12 13:50:21 +0100426 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
427 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100428
429 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100430 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100431 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100432 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100433 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100434 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100435 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100436 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100437 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100438 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100439 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100440 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100441 amd_iommu_stats_add(&complete_ppr);
442 amd_iommu_stats_add(&invalidate_iotlb);
443 amd_iommu_stats_add(&invalidate_iotlb_all);
444 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100445}
446
447#endif
448
Joerg Roedel431b2a22008-07-11 17:14:22 +0200449/****************************************************************************
450 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200451 * Interrupt handling functions
452 *
453 ****************************************************************************/
454
Joerg Roedele3e59872009-09-03 14:02:10 +0200455static void dump_dte_entry(u16 devid)
456{
457 int i;
458
Joerg Roedelee6c2862011-11-09 12:06:03 +0100459 for (i = 0; i < 4; ++i)
460 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200461 amd_iommu_dev_table[devid].data[i]);
462}
463
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200464static void dump_command(unsigned long phys_addr)
465{
466 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
467 int i;
468
469 for (i = 0; i < 4; ++i)
470 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
471}
472
Joerg Roedela345b232009-09-03 15:01:43 +0200473static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200474{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200475 int type, devid, domid, flags;
476 volatile u32 *event = __evt;
477 int count = 0;
478 u64 address;
479
480retry:
481 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
482 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
483 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
484 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
485 address = (u64)(((u64)event[3]) << 32) | event[2];
486
487 if (type == 0) {
488 /* Did we hit the erratum? */
489 if (++count == LOOP_TIMEOUT) {
490 pr_err("AMD-Vi: No event written to event log\n");
491 return;
492 }
493 udelay(1);
494 goto retry;
495 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200496
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200497 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200498
499 switch (type) {
500 case EVENT_TYPE_ILL_DEV:
501 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
502 "address=0x%016llx flags=0x%04x]\n",
503 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
504 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200505 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200506 break;
507 case EVENT_TYPE_IO_FAULT:
508 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
509 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
510 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
511 domid, address, flags);
512 break;
513 case EVENT_TYPE_DEV_TAB_ERR:
514 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
515 "address=0x%016llx flags=0x%04x]\n",
516 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
517 address, flags);
518 break;
519 case EVENT_TYPE_PAGE_TAB_ERR:
520 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
521 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
522 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
523 domid, address, flags);
524 break;
525 case EVENT_TYPE_ILL_CMD:
526 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200527 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200528 break;
529 case EVENT_TYPE_CMD_HARD_ERR:
530 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
531 "flags=0x%04x]\n", address, flags);
532 break;
533 case EVENT_TYPE_IOTLB_INV_TO:
534 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
535 "address=0x%016llx]\n",
536 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
537 address);
538 break;
539 case EVENT_TYPE_INV_DEV_REQ:
540 printk("INVALID_DEVICE_REQUEST 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 default:
546 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
547 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200548
549 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200550}
551
552static void iommu_poll_events(struct amd_iommu *iommu)
553{
554 u32 head, tail;
555 unsigned long flags;
556
557 spin_lock_irqsave(&iommu->lock, flags);
558
559 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
560 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
561
562 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200563 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200564 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
565 }
566
567 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
568
569 spin_unlock_irqrestore(&iommu->lock, flags);
570}
571
Joerg Roedeleee53532012-06-01 15:20:23 +0200572static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100573{
574 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100575
Joerg Roedel399be2f2011-12-01 16:53:47 +0100576 INC_STATS_COUNTER(pri_requests);
577
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100578 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
579 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
580 return;
581 }
582
583 fault.address = raw[1];
584 fault.pasid = PPR_PASID(raw[0]);
585 fault.device_id = PPR_DEVID(raw[0]);
586 fault.tag = PPR_TAG(raw[0]);
587 fault.flags = PPR_FLAGS(raw[0]);
588
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100589 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
590}
591
592static void iommu_poll_ppr_log(struct amd_iommu *iommu)
593{
594 unsigned long flags;
595 u32 head, tail;
596
597 if (iommu->ppr_log == NULL)
598 return;
599
Joerg Roedeleee53532012-06-01 15:20:23 +0200600 /* enable ppr interrupts again */
601 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
602
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100603 spin_lock_irqsave(&iommu->lock, flags);
604
605 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
606 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
607
608 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200609 volatile u64 *raw;
610 u64 entry[2];
611 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100612
Joerg Roedeleee53532012-06-01 15:20:23 +0200613 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100614
Joerg Roedeleee53532012-06-01 15:20:23 +0200615 /*
616 * Hardware bug: Interrupt may arrive before the entry is
617 * written to memory. If this happens we need to wait for the
618 * entry to arrive.
619 */
620 for (i = 0; i < LOOP_TIMEOUT; ++i) {
621 if (PPR_REQ_TYPE(raw[0]) != 0)
622 break;
623 udelay(1);
624 }
625
626 /* Avoid memcpy function-call overhead */
627 entry[0] = raw[0];
628 entry[1] = raw[1];
629
630 /*
631 * To detect the hardware bug we need to clear the entry
632 * back to zero.
633 */
634 raw[0] = raw[1] = 0UL;
635
636 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100637 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
638 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200639
640 /*
641 * Release iommu->lock because ppr-handling might need to
642 * re-aquire it
643 */
644 spin_unlock_irqrestore(&iommu->lock, flags);
645
646 /* Handle PPR entry */
647 iommu_handle_ppr_entry(iommu, entry);
648
649 spin_lock_irqsave(&iommu->lock, flags);
650
651 /* Refresh ring-buffer information */
652 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100653 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
654 }
655
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100656 spin_unlock_irqrestore(&iommu->lock, flags);
657}
658
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200659irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200660{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200661 struct amd_iommu *iommu;
662
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100663 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200664 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100665 iommu_poll_ppr_log(iommu);
666 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200667
668 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200669}
670
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200671irqreturn_t amd_iommu_int_handler(int irq, void *data)
672{
673 return IRQ_WAKE_THREAD;
674}
675
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200676/****************************************************************************
677 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200678 * IOMMU command queuing functions
679 *
680 ****************************************************************************/
681
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200682static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200683{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200684 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200685
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200686 while (*sem == 0 && i < LOOP_TIMEOUT) {
687 udelay(1);
688 i += 1;
689 }
690
691 if (i == LOOP_TIMEOUT) {
692 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
693 return -EIO;
694 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200695
696 return 0;
697}
698
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200699static void copy_cmd_to_buffer(struct amd_iommu *iommu,
700 struct iommu_cmd *cmd,
701 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200702{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200703 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200704
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200705 target = iommu->cmd_buf + tail;
706 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200707
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200708 /* Copy command to buffer */
709 memcpy(target, cmd, sizeof(*cmd));
710
711 /* Tell the IOMMU about it */
712 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
713}
714
Joerg Roedel815b33f2011-04-06 17:26:49 +0200715static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200716{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200717 WARN_ON(address & 0x7ULL);
718
Joerg Roedelded46732011-04-06 10:53:48 +0200719 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200720 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
721 cmd->data[1] = upper_32_bits(__pa(address));
722 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200723 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
724}
725
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200726static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
727{
728 memset(cmd, 0, sizeof(*cmd));
729 cmd->data[0] = devid;
730 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
731}
732
Joerg Roedel11b64022011-04-06 11:49:28 +0200733static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
734 size_t size, u16 domid, int pde)
735{
736 u64 pages;
737 int s;
738
739 pages = iommu_num_pages(address, size, PAGE_SIZE);
740 s = 0;
741
742 if (pages > 1) {
743 /*
744 * If we have to flush more than one page, flush all
745 * TLB entries for this domain
746 */
747 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
748 s = 1;
749 }
750
751 address &= PAGE_MASK;
752
753 memset(cmd, 0, sizeof(*cmd));
754 cmd->data[1] |= domid;
755 cmd->data[2] = lower_32_bits(address);
756 cmd->data[3] = upper_32_bits(address);
757 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
758 if (s) /* size bit - we flush more than one 4kb page */
759 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
760 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
761 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
762}
763
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200764static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
765 u64 address, size_t size)
766{
767 u64 pages;
768 int s;
769
770 pages = iommu_num_pages(address, size, PAGE_SIZE);
771 s = 0;
772
773 if (pages > 1) {
774 /*
775 * If we have to flush more than one page, flush all
776 * TLB entries for this domain
777 */
778 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
779 s = 1;
780 }
781
782 address &= PAGE_MASK;
783
784 memset(cmd, 0, sizeof(*cmd));
785 cmd->data[0] = devid;
786 cmd->data[0] |= (qdep & 0xff) << 24;
787 cmd->data[1] = devid;
788 cmd->data[2] = lower_32_bits(address);
789 cmd->data[3] = upper_32_bits(address);
790 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
791 if (s)
792 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
793}
794
Joerg Roedel22e266c2011-11-21 15:59:08 +0100795static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
796 u64 address, bool size)
797{
798 memset(cmd, 0, sizeof(*cmd));
799
800 address &= ~(0xfffULL);
801
802 cmd->data[0] = pasid & PASID_MASK;
803 cmd->data[1] = domid;
804 cmd->data[2] = lower_32_bits(address);
805 cmd->data[3] = upper_32_bits(address);
806 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
807 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
808 if (size)
809 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
810 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
811}
812
813static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
814 int qdep, u64 address, bool size)
815{
816 memset(cmd, 0, sizeof(*cmd));
817
818 address &= ~(0xfffULL);
819
820 cmd->data[0] = devid;
821 cmd->data[0] |= (pasid & 0xff) << 16;
822 cmd->data[0] |= (qdep & 0xff) << 24;
823 cmd->data[1] = devid;
824 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
825 cmd->data[2] = lower_32_bits(address);
826 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
827 cmd->data[3] = upper_32_bits(address);
828 if (size)
829 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
830 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
831}
832
Joerg Roedelc99afa22011-11-21 18:19:25 +0100833static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
834 int status, int tag, bool gn)
835{
836 memset(cmd, 0, sizeof(*cmd));
837
838 cmd->data[0] = devid;
839 if (gn) {
840 cmd->data[1] = pasid & PASID_MASK;
841 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
842 }
843 cmd->data[3] = tag & 0x1ff;
844 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
845
846 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
847}
848
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200849static void build_inv_all(struct iommu_cmd *cmd)
850{
851 memset(cmd, 0, sizeof(*cmd));
852 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200853}
854
Joerg Roedel431b2a22008-07-11 17:14:22 +0200855/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200856 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200857 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200858 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200859static int iommu_queue_command_sync(struct amd_iommu *iommu,
860 struct iommu_cmd *cmd,
861 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200862{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200863 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200864 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200865
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200866 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100867
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200868again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200869 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200870
871 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
872 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
873 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
874 left = (head - next_tail) % iommu->cmd_buf_size;
875
876 if (left <= 2) {
877 struct iommu_cmd sync_cmd;
878 volatile u64 sem = 0;
879 int ret;
880
881 build_completion_wait(&sync_cmd, (u64)&sem);
882 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
883
884 spin_unlock_irqrestore(&iommu->lock, flags);
885
886 if ((ret = wait_on_sem(&sem)) != 0)
887 return ret;
888
889 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200890 }
891
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200892 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200893
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200894 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200895 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200896
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200897 spin_unlock_irqrestore(&iommu->lock, flags);
898
Joerg Roedel815b33f2011-04-06 17:26:49 +0200899 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100900}
901
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200902static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
903{
904 return iommu_queue_command_sync(iommu, cmd, true);
905}
906
Joerg Roedel8d201962008-12-02 20:34:41 +0100907/*
908 * This function queues a completion wait command into the command
909 * buffer of an IOMMU
910 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100911static int iommu_completion_wait(struct amd_iommu *iommu)
912{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200913 struct iommu_cmd cmd;
914 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200915 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100916
917 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200918 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100919
Joerg Roedel815b33f2011-04-06 17:26:49 +0200920 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100921
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200922 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100923 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200924 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100925
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200926 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200927}
928
Joerg Roedeld8c13082011-04-06 18:51:26 +0200929static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200930{
931 struct iommu_cmd cmd;
932
Joerg Roedeld8c13082011-04-06 18:51:26 +0200933 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200934
Joerg Roedeld8c13082011-04-06 18:51:26 +0200935 return iommu_queue_command(iommu, &cmd);
936}
937
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200938static void iommu_flush_dte_all(struct amd_iommu *iommu)
939{
940 u32 devid;
941
942 for (devid = 0; devid <= 0xffff; ++devid)
943 iommu_flush_dte(iommu, devid);
944
945 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200946}
947
948/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200949 * This function uses heavy locking and may disable irqs for some time. But
950 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200951 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200952static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200953{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200954 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200955
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200956 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
957 struct iommu_cmd cmd;
958 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
959 dom_id, 1);
960 iommu_queue_command(iommu, &cmd);
961 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200962
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200963 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200964}
965
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200966static void iommu_flush_all(struct amd_iommu *iommu)
967{
968 struct iommu_cmd cmd;
969
970 build_inv_all(&cmd);
971
972 iommu_queue_command(iommu, &cmd);
973 iommu_completion_wait(iommu);
974}
975
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200976void iommu_flush_all_caches(struct amd_iommu *iommu)
977{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200978 if (iommu_feature(iommu, FEATURE_IA)) {
979 iommu_flush_all(iommu);
980 } else {
981 iommu_flush_dte_all(iommu);
982 iommu_flush_tlb_all(iommu);
983 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200984}
985
Joerg Roedel431b2a22008-07-11 17:14:22 +0200986/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200987 * Command send function for flushing on-device TLB
988 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200989static int device_flush_iotlb(struct iommu_dev_data *dev_data,
990 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200991{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200992 struct amd_iommu *iommu;
993 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200994 int qdep;
995
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200996 qdep = dev_data->ats.qdep;
997 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200998
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200999 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001000
1001 return iommu_queue_command(iommu, &cmd);
1002}
1003
1004/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001005 * Command send function for invalidating a device table entry
1006 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001007static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001008{
1009 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001010 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001011
Joerg Roedel6c542042011-06-09 17:07:31 +02001012 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001013
Joerg Roedelf62dda62011-06-09 12:55:35 +02001014 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001015 if (ret)
1016 return ret;
1017
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001018 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001019 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001020
1021 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001022}
1023
Joerg Roedel431b2a22008-07-11 17:14:22 +02001024/*
1025 * TLB invalidation function which is called from the mapping functions.
1026 * It invalidates a single PTE if the range to flush is within a single
1027 * page. Otherwise it flushes the whole TLB of the IOMMU.
1028 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001029static void __domain_flush_pages(struct protection_domain *domain,
1030 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001031{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001032 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001033 struct iommu_cmd cmd;
1034 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001035
Joerg Roedel11b64022011-04-06 11:49:28 +02001036 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001037
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001038 for (i = 0; i < amd_iommus_present; ++i) {
1039 if (!domain->dev_iommu[i])
1040 continue;
1041
1042 /*
1043 * Devices of this domain are behind this IOMMU
1044 * We need a TLB flush
1045 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001046 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001047 }
1048
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001049 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001050
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001051 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001052 continue;
1053
Joerg Roedel6c542042011-06-09 17:07:31 +02001054 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001055 }
1056
Joerg Roedel11b64022011-04-06 11:49:28 +02001057 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001058}
1059
Joerg Roedel17b124b2011-04-06 18:01:35 +02001060static void domain_flush_pages(struct protection_domain *domain,
1061 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001062{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001063 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001064}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001065
Joerg Roedel1c655772008-09-04 18:40:05 +02001066/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001067static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001068{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001069 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001070}
1071
Chris Wright42a49f92009-06-15 15:42:00 +02001072/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001073static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001074{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001075 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1076}
1077
1078static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001079{
1080 int i;
1081
1082 for (i = 0; i < amd_iommus_present; ++i) {
1083 if (!domain->dev_iommu[i])
1084 continue;
1085
1086 /*
1087 * Devices of this domain are behind this IOMMU
1088 * We need to wait for completion of all commands.
1089 */
1090 iommu_completion_wait(amd_iommus[i]);
1091 }
1092}
1093
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001094
Joerg Roedel43f49602008-12-02 21:01:12 +01001095/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001096 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001097 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001098static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001099{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001100 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001101
1102 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001103 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001104}
1105
Joerg Roedel431b2a22008-07-11 17:14:22 +02001106/****************************************************************************
1107 *
1108 * The functions below are used the create the page table mappings for
1109 * unity mapped regions.
1110 *
1111 ****************************************************************************/
1112
1113/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001114 * This function is used to add another level to an IO page table. Adding
1115 * another level increases the size of the address space by 9 bits to a size up
1116 * to 64 bits.
1117 */
1118static bool increase_address_space(struct protection_domain *domain,
1119 gfp_t gfp)
1120{
1121 u64 *pte;
1122
1123 if (domain->mode == PAGE_MODE_6_LEVEL)
1124 /* address space already 64 bit large */
1125 return false;
1126
1127 pte = (void *)get_zeroed_page(gfp);
1128 if (!pte)
1129 return false;
1130
1131 *pte = PM_LEVEL_PDE(domain->mode,
1132 virt_to_phys(domain->pt_root));
1133 domain->pt_root = pte;
1134 domain->mode += 1;
1135 domain->updated = true;
1136
1137 return true;
1138}
1139
1140static u64 *alloc_pte(struct protection_domain *domain,
1141 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001142 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001143 u64 **pte_page,
1144 gfp_t gfp)
1145{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001146 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001147 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001148
1149 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001150
1151 while (address > PM_LEVEL_SIZE(domain->mode))
1152 increase_address_space(domain, gfp);
1153
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001154 level = domain->mode - 1;
1155 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1156 address = PAGE_SIZE_ALIGN(address, page_size);
1157 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001158
1159 while (level > end_lvl) {
1160 if (!IOMMU_PTE_PRESENT(*pte)) {
1161 page = (u64 *)get_zeroed_page(gfp);
1162 if (!page)
1163 return NULL;
1164 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1165 }
1166
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001167 /* No level skipping support yet */
1168 if (PM_PTE_LEVEL(*pte) != level)
1169 return NULL;
1170
Joerg Roedel308973d2009-11-24 17:43:32 +01001171 level -= 1;
1172
1173 pte = IOMMU_PTE_PAGE(*pte);
1174
1175 if (pte_page && level == end_lvl)
1176 *pte_page = pte;
1177
1178 pte = &pte[PM_LEVEL_INDEX(level, address)];
1179 }
1180
1181 return pte;
1182}
1183
1184/*
1185 * This function checks if there is a PTE for a given dma address. If
1186 * there is one, it returns the pointer to it.
1187 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001188static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001189{
1190 int level;
1191 u64 *pte;
1192
Joerg Roedel24cd7722010-01-19 17:27:39 +01001193 if (address > PM_LEVEL_SIZE(domain->mode))
1194 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001195
Joerg Roedel24cd7722010-01-19 17:27:39 +01001196 level = domain->mode - 1;
1197 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1198
1199 while (level > 0) {
1200
1201 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001202 if (!IOMMU_PTE_PRESENT(*pte))
1203 return NULL;
1204
Joerg Roedel24cd7722010-01-19 17:27:39 +01001205 /* Large PTE */
1206 if (PM_PTE_LEVEL(*pte) == 0x07) {
1207 unsigned long pte_mask, __pte;
1208
1209 /*
1210 * If we have a series of large PTEs, make
1211 * sure to return a pointer to the first one.
1212 */
1213 pte_mask = PTE_PAGE_SIZE(*pte);
1214 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1215 __pte = ((unsigned long)pte) & pte_mask;
1216
1217 return (u64 *)__pte;
1218 }
1219
1220 /* No level skipping support yet */
1221 if (PM_PTE_LEVEL(*pte) != level)
1222 return NULL;
1223
Joerg Roedel308973d2009-11-24 17:43:32 +01001224 level -= 1;
1225
Joerg Roedel24cd7722010-01-19 17:27:39 +01001226 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001227 pte = IOMMU_PTE_PAGE(*pte);
1228 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001229 }
1230
1231 return pte;
1232}
1233
1234/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001235 * Generic mapping functions. It maps a physical address into a DMA
1236 * address space. It allocates the page table pages if necessary.
1237 * In the future it can be extended to a generic mapping function
1238 * supporting all features of AMD IOMMU page tables like level skipping
1239 * and full 64 bit address spaces.
1240 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001241static int iommu_map_page(struct protection_domain *dom,
1242 unsigned long bus_addr,
1243 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001244 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001245 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001246{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001247 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001248 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001249
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001250 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001251 return -EINVAL;
1252
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001253 bus_addr = PAGE_ALIGN(bus_addr);
1254 phys_addr = PAGE_ALIGN(phys_addr);
1255 count = PAGE_SIZE_PTE_COUNT(page_size);
1256 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001257
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001258 for (i = 0; i < count; ++i)
1259 if (IOMMU_PTE_PRESENT(pte[i]))
1260 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001261
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001262 if (page_size > PAGE_SIZE) {
1263 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1264 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1265 } else
1266 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1267
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001268 if (prot & IOMMU_PROT_IR)
1269 __pte |= IOMMU_PTE_IR;
1270 if (prot & IOMMU_PROT_IW)
1271 __pte |= IOMMU_PTE_IW;
1272
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001273 for (i = 0; i < count; ++i)
1274 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001275
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001276 update_domain(dom);
1277
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001278 return 0;
1279}
1280
Joerg Roedel24cd7722010-01-19 17:27:39 +01001281static unsigned long iommu_unmap_page(struct protection_domain *dom,
1282 unsigned long bus_addr,
1283 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001284{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001285 unsigned long long unmap_size, unmapped;
1286 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001287
Joerg Roedel24cd7722010-01-19 17:27:39 +01001288 BUG_ON(!is_power_of_2(page_size));
1289
1290 unmapped = 0;
1291
1292 while (unmapped < page_size) {
1293
1294 pte = fetch_pte(dom, bus_addr);
1295
1296 if (!pte) {
1297 /*
1298 * No PTE for this address
1299 * move forward in 4kb steps
1300 */
1301 unmap_size = PAGE_SIZE;
1302 } else if (PM_PTE_LEVEL(*pte) == 0) {
1303 /* 4kb PTE found for this address */
1304 unmap_size = PAGE_SIZE;
1305 *pte = 0ULL;
1306 } else {
1307 int count, i;
1308
1309 /* Large PTE found which maps this address */
1310 unmap_size = PTE_PAGE_SIZE(*pte);
1311 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1312 for (i = 0; i < count; i++)
1313 pte[i] = 0ULL;
1314 }
1315
1316 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1317 unmapped += unmap_size;
1318 }
1319
1320 BUG_ON(!is_power_of_2(unmapped));
1321
1322 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001323}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001324
Joerg Roedel431b2a22008-07-11 17:14:22 +02001325/*
1326 * This function checks if a specific unity mapping entry is needed for
1327 * this specific IOMMU.
1328 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001329static int iommu_for_unity_map(struct amd_iommu *iommu,
1330 struct unity_map_entry *entry)
1331{
1332 u16 bdf, i;
1333
1334 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1335 bdf = amd_iommu_alias_table[i];
1336 if (amd_iommu_rlookup_table[bdf] == iommu)
1337 return 1;
1338 }
1339
1340 return 0;
1341}
1342
Joerg Roedel431b2a22008-07-11 17:14:22 +02001343/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001344 * This function actually applies the mapping to the page table of the
1345 * dma_ops domain.
1346 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001347static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1348 struct unity_map_entry *e)
1349{
1350 u64 addr;
1351 int ret;
1352
1353 for (addr = e->address_start; addr < e->address_end;
1354 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001355 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001356 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001357 if (ret)
1358 return ret;
1359 /*
1360 * if unity mapping is in aperture range mark the page
1361 * as allocated in the aperture
1362 */
1363 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001364 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001365 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001366 }
1367
1368 return 0;
1369}
1370
Joerg Roedel431b2a22008-07-11 17:14:22 +02001371/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001372 * Init the unity mappings for a specific IOMMU in the system
1373 *
1374 * Basically iterates over all unity mapping entries and applies them to
1375 * the default domain DMA of that IOMMU if necessary.
1376 */
1377static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1378{
1379 struct unity_map_entry *entry;
1380 int ret;
1381
1382 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1383 if (!iommu_for_unity_map(iommu, entry))
1384 continue;
1385 ret = dma_ops_unity_map(iommu->default_dom, entry);
1386 if (ret)
1387 return ret;
1388 }
1389
1390 return 0;
1391}
1392
1393/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001394 * Inits the unity mappings required for a specific device
1395 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001396static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1397 u16 devid)
1398{
1399 struct unity_map_entry *e;
1400 int ret;
1401
1402 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1403 if (!(devid >= e->devid_start && devid <= e->devid_end))
1404 continue;
1405 ret = dma_ops_unity_map(dma_dom, e);
1406 if (ret)
1407 return ret;
1408 }
1409
1410 return 0;
1411}
1412
Joerg Roedel431b2a22008-07-11 17:14:22 +02001413/****************************************************************************
1414 *
1415 * The next functions belong to the address allocator for the dma_ops
1416 * interface functions. They work like the allocators in the other IOMMU
1417 * drivers. Its basically a bitmap which marks the allocated pages in
1418 * the aperture. Maybe it could be enhanced in the future to a more
1419 * efficient allocator.
1420 *
1421 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001422
Joerg Roedel431b2a22008-07-11 17:14:22 +02001423/*
Joerg Roedel384de722009-05-15 12:30:05 +02001424 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001425 *
1426 * called with domain->lock held
1427 */
Joerg Roedel384de722009-05-15 12:30:05 +02001428
Joerg Roedel9cabe892009-05-18 16:38:55 +02001429/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001430 * Used to reserve address ranges in the aperture (e.g. for exclusion
1431 * ranges.
1432 */
1433static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1434 unsigned long start_page,
1435 unsigned int pages)
1436{
1437 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1438
1439 if (start_page + pages > last_page)
1440 pages = last_page - start_page;
1441
1442 for (i = start_page; i < start_page + pages; ++i) {
1443 int index = i / APERTURE_RANGE_PAGES;
1444 int page = i % APERTURE_RANGE_PAGES;
1445 __set_bit(page, dom->aperture[index]->bitmap);
1446 }
1447}
1448
1449/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001450 * This function is used to add a new aperture range to an existing
1451 * aperture in case of dma_ops domain allocation or address allocation
1452 * failure.
1453 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001454static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001455 bool populate, gfp_t gfp)
1456{
1457 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001458 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001459 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001460
Joerg Roedelf5e97052009-05-22 12:31:53 +02001461#ifdef CONFIG_IOMMU_STRESS
1462 populate = false;
1463#endif
1464
Joerg Roedel9cabe892009-05-18 16:38:55 +02001465 if (index >= APERTURE_MAX_RANGES)
1466 return -ENOMEM;
1467
1468 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1469 if (!dma_dom->aperture[index])
1470 return -ENOMEM;
1471
1472 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1473 if (!dma_dom->aperture[index]->bitmap)
1474 goto out_free;
1475
1476 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1477
1478 if (populate) {
1479 unsigned long address = dma_dom->aperture_size;
1480 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1481 u64 *pte, *pte_page;
1482
1483 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001484 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001485 &pte_page, gfp);
1486 if (!pte)
1487 goto out_free;
1488
1489 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1490
1491 address += APERTURE_RANGE_SIZE / 64;
1492 }
1493 }
1494
Joerg Roedel17f5b562011-07-06 17:14:44 +02001495 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001496 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1497
Joerg Roedel17f5b562011-07-06 17:14:44 +02001498 /* Reserve address range used for MSI messages */
1499 if (old_size < MSI_ADDR_BASE_LO &&
1500 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1501 unsigned long spage;
1502 int pages;
1503
1504 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1505 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1506
1507 dma_ops_reserve_addresses(dma_dom, spage, pages);
1508 }
1509
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001510 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001511 for_each_iommu(iommu) {
1512 if (iommu->exclusion_start &&
1513 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1514 && iommu->exclusion_start < dma_dom->aperture_size) {
1515 unsigned long startpage;
1516 int pages = iommu_num_pages(iommu->exclusion_start,
1517 iommu->exclusion_length,
1518 PAGE_SIZE);
1519 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1520 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1521 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001522 }
1523
1524 /*
1525 * Check for areas already mapped as present in the new aperture
1526 * range and mark those pages as reserved in the allocator. Such
1527 * mappings may already exist as a result of requested unity
1528 * mappings for devices.
1529 */
1530 for (i = dma_dom->aperture[index]->offset;
1531 i < dma_dom->aperture_size;
1532 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001533 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001534 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1535 continue;
1536
Joerg Roedelfcd08612011-10-11 17:41:32 +02001537 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001538 }
1539
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001540 update_domain(&dma_dom->domain);
1541
Joerg Roedel9cabe892009-05-18 16:38:55 +02001542 return 0;
1543
1544out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001545 update_domain(&dma_dom->domain);
1546
Joerg Roedel9cabe892009-05-18 16:38:55 +02001547 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1548
1549 kfree(dma_dom->aperture[index]);
1550 dma_dom->aperture[index] = NULL;
1551
1552 return -ENOMEM;
1553}
1554
Joerg Roedel384de722009-05-15 12:30:05 +02001555static unsigned long dma_ops_area_alloc(struct device *dev,
1556 struct dma_ops_domain *dom,
1557 unsigned int pages,
1558 unsigned long align_mask,
1559 u64 dma_mask,
1560 unsigned long start)
1561{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001562 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001563 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1564 int i = start >> APERTURE_RANGE_SHIFT;
1565 unsigned long boundary_size;
1566 unsigned long address = -1;
1567 unsigned long limit;
1568
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001569 next_bit >>= PAGE_SHIFT;
1570
Joerg Roedel384de722009-05-15 12:30:05 +02001571 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1572 PAGE_SIZE) >> PAGE_SHIFT;
1573
1574 for (;i < max_index; ++i) {
1575 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1576
1577 if (dom->aperture[i]->offset >= dma_mask)
1578 break;
1579
1580 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1581 dma_mask >> PAGE_SHIFT);
1582
1583 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1584 limit, next_bit, pages, 0,
1585 boundary_size, align_mask);
1586 if (address != -1) {
1587 address = dom->aperture[i]->offset +
1588 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001589 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001590 break;
1591 }
1592
1593 next_bit = 0;
1594 }
1595
1596 return address;
1597}
1598
Joerg Roedeld3086442008-06-26 21:27:57 +02001599static unsigned long dma_ops_alloc_addresses(struct device *dev,
1600 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001601 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001602 unsigned long align_mask,
1603 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001604{
Joerg Roedeld3086442008-06-26 21:27:57 +02001605 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001606
Joerg Roedelfe16f082009-05-22 12:27:53 +02001607#ifdef CONFIG_IOMMU_STRESS
1608 dom->next_address = 0;
1609 dom->need_flush = true;
1610#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001611
Joerg Roedel384de722009-05-15 12:30:05 +02001612 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001613 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001614
Joerg Roedel1c655772008-09-04 18:40:05 +02001615 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001616 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001617 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1618 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001619 dom->need_flush = true;
1620 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001621
Joerg Roedel384de722009-05-15 12:30:05 +02001622 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001623 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001624
1625 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1626
1627 return address;
1628}
1629
Joerg Roedel431b2a22008-07-11 17:14:22 +02001630/*
1631 * The address free function.
1632 *
1633 * called with domain->lock held
1634 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001635static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1636 unsigned long address,
1637 unsigned int pages)
1638{
Joerg Roedel384de722009-05-15 12:30:05 +02001639 unsigned i = address >> APERTURE_RANGE_SHIFT;
1640 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001641
Joerg Roedel384de722009-05-15 12:30:05 +02001642 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1643
Joerg Roedel47bccd62009-05-22 12:40:54 +02001644#ifdef CONFIG_IOMMU_STRESS
1645 if (i < 4)
1646 return;
1647#endif
1648
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001649 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001650 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001651
1652 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001653
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001654 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001655
Joerg Roedeld3086442008-06-26 21:27:57 +02001656}
1657
Joerg Roedel431b2a22008-07-11 17:14:22 +02001658/****************************************************************************
1659 *
1660 * The next functions belong to the domain allocation. A domain is
1661 * allocated for every IOMMU as the default domain. If device isolation
1662 * is enabled, every device get its own domain. The most important thing
1663 * about domains is the page table mapping the DMA address space they
1664 * contain.
1665 *
1666 ****************************************************************************/
1667
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001668/*
1669 * This function adds a protection domain to the global protection domain list
1670 */
1671static void add_domain_to_list(struct protection_domain *domain)
1672{
1673 unsigned long flags;
1674
1675 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1676 list_add(&domain->list, &amd_iommu_pd_list);
1677 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1678}
1679
1680/*
1681 * This function removes a protection domain to the global
1682 * protection domain list
1683 */
1684static void del_domain_from_list(struct protection_domain *domain)
1685{
1686 unsigned long flags;
1687
1688 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1689 list_del(&domain->list);
1690 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1691}
1692
Joerg Roedelec487d12008-06-26 21:27:58 +02001693static u16 domain_id_alloc(void)
1694{
1695 unsigned long flags;
1696 int id;
1697
1698 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1699 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1700 BUG_ON(id == 0);
1701 if (id > 0 && id < MAX_DOMAIN_ID)
1702 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1703 else
1704 id = 0;
1705 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1706
1707 return id;
1708}
1709
Joerg Roedela2acfb72008-12-02 18:28:53 +01001710static void domain_id_free(int id)
1711{
1712 unsigned long flags;
1713
1714 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1715 if (id > 0 && id < MAX_DOMAIN_ID)
1716 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1717 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1718}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001719
Joerg Roedel86db2e52008-12-02 18:20:21 +01001720static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001721{
1722 int i, j;
1723 u64 *p1, *p2, *p3;
1724
Joerg Roedel86db2e52008-12-02 18:20:21 +01001725 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001726
1727 if (!p1)
1728 return;
1729
1730 for (i = 0; i < 512; ++i) {
1731 if (!IOMMU_PTE_PRESENT(p1[i]))
1732 continue;
1733
1734 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001735 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001736 if (!IOMMU_PTE_PRESENT(p2[j]))
1737 continue;
1738 p3 = IOMMU_PTE_PAGE(p2[j]);
1739 free_page((unsigned long)p3);
1740 }
1741
1742 free_page((unsigned long)p2);
1743 }
1744
1745 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001746
1747 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001748}
1749
Joerg Roedelb16137b2011-11-21 16:50:23 +01001750static void free_gcr3_tbl_level1(u64 *tbl)
1751{
1752 u64 *ptr;
1753 int i;
1754
1755 for (i = 0; i < 512; ++i) {
1756 if (!(tbl[i] & GCR3_VALID))
1757 continue;
1758
1759 ptr = __va(tbl[i] & PAGE_MASK);
1760
1761 free_page((unsigned long)ptr);
1762 }
1763}
1764
1765static void free_gcr3_tbl_level2(u64 *tbl)
1766{
1767 u64 *ptr;
1768 int i;
1769
1770 for (i = 0; i < 512; ++i) {
1771 if (!(tbl[i] & GCR3_VALID))
1772 continue;
1773
1774 ptr = __va(tbl[i] & PAGE_MASK);
1775
1776 free_gcr3_tbl_level1(ptr);
1777 }
1778}
1779
Joerg Roedel52815b72011-11-17 17:24:28 +01001780static void free_gcr3_table(struct protection_domain *domain)
1781{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001782 if (domain->glx == 2)
1783 free_gcr3_tbl_level2(domain->gcr3_tbl);
1784 else if (domain->glx == 1)
1785 free_gcr3_tbl_level1(domain->gcr3_tbl);
1786 else if (domain->glx != 0)
1787 BUG();
1788
Joerg Roedel52815b72011-11-17 17:24:28 +01001789 free_page((unsigned long)domain->gcr3_tbl);
1790}
1791
Joerg Roedel431b2a22008-07-11 17:14:22 +02001792/*
1793 * Free a domain, only used if something went wrong in the
1794 * allocation path and we need to free an already allocated page table
1795 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001796static void dma_ops_domain_free(struct dma_ops_domain *dom)
1797{
Joerg Roedel384de722009-05-15 12:30:05 +02001798 int i;
1799
Joerg Roedelec487d12008-06-26 21:27:58 +02001800 if (!dom)
1801 return;
1802
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001803 del_domain_from_list(&dom->domain);
1804
Joerg Roedel86db2e52008-12-02 18:20:21 +01001805 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001806
Joerg Roedel384de722009-05-15 12:30:05 +02001807 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1808 if (!dom->aperture[i])
1809 continue;
1810 free_page((unsigned long)dom->aperture[i]->bitmap);
1811 kfree(dom->aperture[i]);
1812 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001813
1814 kfree(dom);
1815}
1816
Joerg Roedel431b2a22008-07-11 17:14:22 +02001817/*
1818 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001819 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001820 * structures required for the dma_ops interface
1821 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001822static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001823{
1824 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001825
1826 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1827 if (!dma_dom)
1828 return NULL;
1829
1830 spin_lock_init(&dma_dom->domain.lock);
1831
1832 dma_dom->domain.id = domain_id_alloc();
1833 if (dma_dom->domain.id == 0)
1834 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001835 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001836 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001837 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001838 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001839 dma_dom->domain.priv = dma_dom;
1840 if (!dma_dom->domain.pt_root)
1841 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001842
Joerg Roedel1c655772008-09-04 18:40:05 +02001843 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001844 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001845
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001846 add_domain_to_list(&dma_dom->domain);
1847
Joerg Roedel576175c2009-11-23 19:08:46 +01001848 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001849 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001850
Joerg Roedel431b2a22008-07-11 17:14:22 +02001851 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001852 * mark the first page as allocated so we never return 0 as
1853 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001854 */
Joerg Roedel384de722009-05-15 12:30:05 +02001855 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001856 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001857
Joerg Roedelec487d12008-06-26 21:27:58 +02001858
1859 return dma_dom;
1860
1861free_dma_dom:
1862 dma_ops_domain_free(dma_dom);
1863
1864 return NULL;
1865}
1866
Joerg Roedel431b2a22008-07-11 17:14:22 +02001867/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001868 * little helper function to check whether a given protection domain is a
1869 * dma_ops domain
1870 */
1871static bool dma_ops_domain(struct protection_domain *domain)
1872{
1873 return domain->flags & PD_DMA_OPS_MASK;
1874}
1875
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001876static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001877{
Joerg Roedel132bd682011-11-17 14:18:46 +01001878 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001879 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001880
Joerg Roedel132bd682011-11-17 14:18:46 +01001881 if (domain->mode != PAGE_MODE_NONE)
1882 pte_root = virt_to_phys(domain->pt_root);
1883
Joerg Roedel38ddf412008-09-11 10:38:32 +02001884 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1885 << DEV_ENTRY_MODE_SHIFT;
1886 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001887
Joerg Roedelee6c2862011-11-09 12:06:03 +01001888 flags = amd_iommu_dev_table[devid].data[1];
1889
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001890 if (ats)
1891 flags |= DTE_FLAG_IOTLB;
1892
Joerg Roedel52815b72011-11-17 17:24:28 +01001893 if (domain->flags & PD_IOMMUV2_MASK) {
1894 u64 gcr3 = __pa(domain->gcr3_tbl);
1895 u64 glx = domain->glx;
1896 u64 tmp;
1897
1898 pte_root |= DTE_FLAG_GV;
1899 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1900
1901 /* First mask out possible old values for GCR3 table */
1902 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1903 flags &= ~tmp;
1904
1905 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1906 flags &= ~tmp;
1907
1908 /* Encode GCR3 table into DTE */
1909 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1910 pte_root |= tmp;
1911
1912 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1913 flags |= tmp;
1914
1915 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1916 flags |= tmp;
1917 }
1918
Joerg Roedelee6c2862011-11-09 12:06:03 +01001919 flags &= ~(0xffffUL);
1920 flags |= domain->id;
1921
1922 amd_iommu_dev_table[devid].data[1] = flags;
1923 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001924}
1925
Joerg Roedel15898bb2009-11-24 15:39:42 +01001926static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001927{
Joerg Roedel355bf552008-12-08 12:02:41 +01001928 /* remove entry from the device table seen by the hardware */
1929 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1930 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01001931
Joerg Roedelc5cca142009-10-09 18:31:20 +02001932 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001933}
1934
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001935static void do_attach(struct iommu_dev_data *dev_data,
1936 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001937{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001938 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001939 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001940
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001941 iommu = amd_iommu_rlookup_table[dev_data->devid];
1942 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001943
1944 /* Update data structures */
1945 dev_data->domain = domain;
1946 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001947 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001948
1949 /* Do reference counting */
1950 domain->dev_iommu[iommu->index] += 1;
1951 domain->dev_cnt += 1;
1952
1953 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001954 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001955}
1956
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001957static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001958{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001959 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001960
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001961 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001962
Joerg Roedelc4596112009-11-20 14:57:32 +01001963 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001964 dev_data->domain->dev_iommu[iommu->index] -= 1;
1965 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001966
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001967 /* Update data structures */
1968 dev_data->domain = NULL;
1969 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001970 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001971
1972 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001973 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001974}
1975
1976/*
1977 * If a device is not yet associated with a domain, this function does
1978 * assigns it visible for the hardware
1979 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001980static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001981 struct protection_domain *domain)
1982{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001983 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001984
Joerg Roedel15898bb2009-11-24 15:39:42 +01001985 /* lock domain */
1986 spin_lock(&domain->lock);
1987
Joerg Roedel71f77582011-06-09 19:03:15 +02001988 if (dev_data->alias_data != NULL) {
1989 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001990
Joerg Roedel2b02b092011-06-09 17:48:39 +02001991 /* Some sanity checks */
1992 ret = -EBUSY;
1993 if (alias_data->domain != NULL &&
1994 alias_data->domain != domain)
1995 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001996
Joerg Roedel2b02b092011-06-09 17:48:39 +02001997 if (dev_data->domain != NULL &&
1998 dev_data->domain != domain)
1999 goto out_unlock;
2000
2001 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002002 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002003 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002004
2005 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002006 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002007
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002008 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002009 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002010
Joerg Roedel24100052009-11-25 15:59:57 +01002011 atomic_inc(&dev_data->bind);
2012
Julia Lawall84fe6c12010-05-27 12:31:51 +02002013 ret = 0;
2014
2015out_unlock:
2016
Joerg Roedel355bf552008-12-08 12:02:41 +01002017 /* ready */
2018 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002019
Julia Lawall84fe6c12010-05-27 12:31:51 +02002020 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002021}
2022
Joerg Roedel52815b72011-11-17 17:24:28 +01002023
2024static void pdev_iommuv2_disable(struct pci_dev *pdev)
2025{
2026 pci_disable_ats(pdev);
2027 pci_disable_pri(pdev);
2028 pci_disable_pasid(pdev);
2029}
2030
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002031/* FIXME: Change generic reset-function to do the same */
2032static int pri_reset_while_enabled(struct pci_dev *pdev)
2033{
2034 u16 control;
2035 int pos;
2036
Joerg Roedel46277b72011-12-07 14:34:02 +01002037 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002038 if (!pos)
2039 return -EINVAL;
2040
Joerg Roedel46277b72011-12-07 14:34:02 +01002041 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2042 control |= PCI_PRI_CTRL_RESET;
2043 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002044
2045 return 0;
2046}
2047
Joerg Roedel52815b72011-11-17 17:24:28 +01002048static int pdev_iommuv2_enable(struct pci_dev *pdev)
2049{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002050 bool reset_enable;
2051 int reqs, ret;
2052
2053 /* FIXME: Hardcode number of outstanding requests for now */
2054 reqs = 32;
2055 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2056 reqs = 1;
2057 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002058
2059 /* Only allow access to user-accessible pages */
2060 ret = pci_enable_pasid(pdev, 0);
2061 if (ret)
2062 goto out_err;
2063
2064 /* First reset the PRI state of the device */
2065 ret = pci_reset_pri(pdev);
2066 if (ret)
2067 goto out_err;
2068
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002069 /* Enable PRI */
2070 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002071 if (ret)
2072 goto out_err;
2073
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002074 if (reset_enable) {
2075 ret = pri_reset_while_enabled(pdev);
2076 if (ret)
2077 goto out_err;
2078 }
2079
Joerg Roedel52815b72011-11-17 17:24:28 +01002080 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2081 if (ret)
2082 goto out_err;
2083
2084 return 0;
2085
2086out_err:
2087 pci_disable_pri(pdev);
2088 pci_disable_pasid(pdev);
2089
2090 return ret;
2091}
2092
Joerg Roedelc99afa22011-11-21 18:19:25 +01002093/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002094#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002095
2096bool pci_pri_tlp_required(struct pci_dev *pdev)
2097{
Joerg Roedela3b93122012-04-12 12:49:26 +02002098 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002099 int pos;
2100
Joerg Roedel46277b72011-12-07 14:34:02 +01002101 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002102 if (!pos)
2103 return false;
2104
Joerg Roedela3b93122012-04-12 12:49:26 +02002105 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002106
Joerg Roedela3b93122012-04-12 12:49:26 +02002107 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002108}
2109
Joerg Roedel15898bb2009-11-24 15:39:42 +01002110/*
2111 * If a device is not yet associated with a domain, this function does
2112 * assigns it visible for the hardware
2113 */
2114static int attach_device(struct device *dev,
2115 struct protection_domain *domain)
2116{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002117 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002118 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002119 unsigned long flags;
2120 int ret;
2121
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002122 dev_data = get_dev_data(dev);
2123
Joerg Roedel52815b72011-11-17 17:24:28 +01002124 if (domain->flags & PD_IOMMUV2_MASK) {
2125 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2126 return -EINVAL;
2127
2128 if (pdev_iommuv2_enable(pdev) != 0)
2129 return -EINVAL;
2130
2131 dev_data->ats.enabled = true;
2132 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002133 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002134 } else if (amd_iommu_iotlb_sup &&
2135 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002136 dev_data->ats.enabled = true;
2137 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2138 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002139
Joerg Roedel15898bb2009-11-24 15:39:42 +01002140 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002141 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002142 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2143
2144 /*
2145 * We might boot into a crash-kernel here. The crashed kernel
2146 * left the caches in the IOMMU dirty. So we have to flush
2147 * here to evict all dirty stuff.
2148 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002149 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002150
2151 return ret;
2152}
2153
2154/*
2155 * Removes a device from a protection domain (unlocked)
2156 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002157static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002158{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002159 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002160 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002161
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002162 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002163
Joerg Roedel2ca76272010-01-22 16:45:31 +01002164 domain = dev_data->domain;
2165
2166 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002167
Joerg Roedel71f77582011-06-09 19:03:15 +02002168 if (dev_data->alias_data != NULL) {
2169 struct iommu_dev_data *alias_data = dev_data->alias_data;
2170
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002171 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002172 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002173 }
2174
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002175 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002176 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002177
Joerg Roedel2ca76272010-01-22 16:45:31 +01002178 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002179
Joerg Roedel21129f72009-09-01 11:59:42 +02002180 /*
2181 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002182 * passthrough domain if it is detached from any other domain.
2183 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002184 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002185 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002186 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002187 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002188}
2189
2190/*
2191 * Removes a device from a protection domain (with devtable_lock held)
2192 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002193static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002194{
Joerg Roedel52815b72011-11-17 17:24:28 +01002195 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002196 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002197 unsigned long flags;
2198
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002199 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002200 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002201
Joerg Roedel355bf552008-12-08 12:02:41 +01002202 /* lock device table */
2203 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002204 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002205 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002206
Joerg Roedel52815b72011-11-17 17:24:28 +01002207 if (domain->flags & PD_IOMMUV2_MASK)
2208 pdev_iommuv2_disable(to_pci_dev(dev));
2209 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002210 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002211
2212 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002213}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002214
Joerg Roedel15898bb2009-11-24 15:39:42 +01002215/*
2216 * Find out the protection domain structure for a given PCI device. This
2217 * will give us the pointer to the page table root for example.
2218 */
2219static struct protection_domain *domain_for_device(struct device *dev)
2220{
Joerg Roedel71f77582011-06-09 19:03:15 +02002221 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002222 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002223 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002224
Joerg Roedel657cbb62009-11-23 15:26:46 +01002225 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002226
Joerg Roedel2b02b092011-06-09 17:48:39 +02002227 if (dev_data->domain)
2228 return dev_data->domain;
2229
Joerg Roedel71f77582011-06-09 19:03:15 +02002230 if (dev_data->alias_data != NULL) {
2231 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002232
2233 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2234 if (alias_data->domain != NULL) {
2235 __attach_device(dev_data, alias_data->domain);
2236 dom = alias_data->domain;
2237 }
2238 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002239 }
2240
Joerg Roedel15898bb2009-11-24 15:39:42 +01002241 return dom;
2242}
2243
Joerg Roedele275a2a2008-12-10 18:27:25 +01002244static int device_change_notifier(struct notifier_block *nb,
2245 unsigned long action, void *data)
2246{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002247 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002248 struct protection_domain *domain;
2249 struct iommu_dev_data *dev_data;
2250 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002251 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002252 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002253 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002254
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002255 if (!check_device(dev))
2256 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002257
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002258 devid = get_device_id(dev);
2259 iommu = amd_iommu_rlookup_table[devid];
2260 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002261
2262 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002263 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002264
2265 domain = domain_for_device(dev);
2266
Joerg Roedele275a2a2008-12-10 18:27:25 +01002267 if (!domain)
2268 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002269 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002270 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002271 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002272 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002273 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002274
2275 iommu_init_device(dev);
2276
2277 domain = domain_for_device(dev);
2278
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002279 /* allocate a protection domain if a device is added */
2280 dma_domain = find_protection_domain(devid);
2281 if (dma_domain)
2282 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002283 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002284 if (!dma_domain)
2285 goto out;
2286 dma_domain->target_dev = devid;
2287
2288 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2289 list_add_tail(&dma_domain->list, &iommu_pd_list);
2290 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2291
2292 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002293 case BUS_NOTIFY_DEL_DEVICE:
2294
2295 iommu_uninit_device(dev);
2296
Joerg Roedele275a2a2008-12-10 18:27:25 +01002297 default:
2298 goto out;
2299 }
2300
Joerg Roedele275a2a2008-12-10 18:27:25 +01002301 iommu_completion_wait(iommu);
2302
2303out:
2304 return 0;
2305}
2306
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302307static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002308 .notifier_call = device_change_notifier,
2309};
Joerg Roedel355bf552008-12-08 12:02:41 +01002310
Joerg Roedel8638c492009-12-10 11:12:25 +01002311void amd_iommu_init_notifier(void)
2312{
2313 bus_register_notifier(&pci_bus_type, &device_nb);
2314}
2315
Joerg Roedel431b2a22008-07-11 17:14:22 +02002316/*****************************************************************************
2317 *
2318 * The next functions belong to the dma_ops mapping/unmapping code.
2319 *
2320 *****************************************************************************/
2321
2322/*
2323 * In the dma_ops path we only have the struct device. This function
2324 * finds the corresponding IOMMU, the protection domain and the
2325 * requestor id for a given device.
2326 * If the device is not yet associated with a domain this is also done
2327 * in this function.
2328 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002329static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002330{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002331 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002332 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002333 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002334
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002335 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002336 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002337
Joerg Roedel94f6d192009-11-24 16:40:02 +01002338 domain = domain_for_device(dev);
2339 if (domain != NULL && !dma_ops_domain(domain))
2340 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002341
Joerg Roedel94f6d192009-11-24 16:40:02 +01002342 if (domain != NULL)
2343 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002344
Joerg Roedel15898bb2009-11-24 15:39:42 +01002345 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002346 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002347 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002348 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2349 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002350 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002351 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002352
Joerg Roedel94f6d192009-11-24 16:40:02 +01002353 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002354}
2355
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002356static void update_device_table(struct protection_domain *domain)
2357{
Joerg Roedel492667d2009-11-27 13:25:47 +01002358 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002359
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002360 list_for_each_entry(dev_data, &domain->dev_list, list)
2361 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002362}
2363
2364static void update_domain(struct protection_domain *domain)
2365{
2366 if (!domain->updated)
2367 return;
2368
2369 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002370
2371 domain_flush_devices(domain);
2372 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002373
2374 domain->updated = false;
2375}
2376
Joerg Roedel431b2a22008-07-11 17:14:22 +02002377/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002378 * This function fetches the PTE for a given address in the aperture
2379 */
2380static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2381 unsigned long address)
2382{
Joerg Roedel384de722009-05-15 12:30:05 +02002383 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002384 u64 *pte, *pte_page;
2385
Joerg Roedel384de722009-05-15 12:30:05 +02002386 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2387 if (!aperture)
2388 return NULL;
2389
2390 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002391 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002392 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002393 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002394 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2395 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002396 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002397
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002398 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002399
2400 return pte;
2401}
2402
2403/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002404 * This is the generic map function. It maps one 4kb page at paddr to
2405 * the given address in the DMA address space for the domain.
2406 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002407static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002408 unsigned long address,
2409 phys_addr_t paddr,
2410 int direction)
2411{
2412 u64 *pte, __pte;
2413
2414 WARN_ON(address > dom->aperture_size);
2415
2416 paddr &= PAGE_MASK;
2417
Joerg Roedel8bda3092009-05-12 12:02:46 +02002418 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002419 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002420 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002421
2422 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2423
2424 if (direction == DMA_TO_DEVICE)
2425 __pte |= IOMMU_PTE_IR;
2426 else if (direction == DMA_FROM_DEVICE)
2427 __pte |= IOMMU_PTE_IW;
2428 else if (direction == DMA_BIDIRECTIONAL)
2429 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2430
2431 WARN_ON(*pte);
2432
2433 *pte = __pte;
2434
2435 return (dma_addr_t)address;
2436}
2437
Joerg Roedel431b2a22008-07-11 17:14:22 +02002438/*
2439 * The generic unmapping function for on page in the DMA address space.
2440 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002441static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002442 unsigned long address)
2443{
Joerg Roedel384de722009-05-15 12:30:05 +02002444 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002445 u64 *pte;
2446
2447 if (address >= dom->aperture_size)
2448 return;
2449
Joerg Roedel384de722009-05-15 12:30:05 +02002450 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2451 if (!aperture)
2452 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002453
Joerg Roedel384de722009-05-15 12:30:05 +02002454 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2455 if (!pte)
2456 return;
2457
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002458 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002459
2460 WARN_ON(!*pte);
2461
2462 *pte = 0ULL;
2463}
2464
Joerg Roedel431b2a22008-07-11 17:14:22 +02002465/*
2466 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002467 * contiguous memory region into DMA address space. It is used by all
2468 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002469 * Must be called with the domain lock held.
2470 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002471static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002472 struct dma_ops_domain *dma_dom,
2473 phys_addr_t paddr,
2474 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002475 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002476 bool align,
2477 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002478{
2479 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002480 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002481 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002482 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002483 int i;
2484
Joerg Roedele3c449f2008-10-15 22:02:11 -07002485 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002486 paddr &= PAGE_MASK;
2487
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002488 INC_STATS_COUNTER(total_map_requests);
2489
Joerg Roedelc1858972008-12-12 15:42:39 +01002490 if (pages > 1)
2491 INC_STATS_COUNTER(cross_page);
2492
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002493 if (align)
2494 align_mask = (1UL << get_order(size)) - 1;
2495
Joerg Roedel11b83882009-05-19 10:23:15 +02002496retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002497 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2498 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002499 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002500 /*
2501 * setting next_address here will let the address
2502 * allocator only scan the new allocated range in the
2503 * first run. This is a small optimization.
2504 */
2505 dma_dom->next_address = dma_dom->aperture_size;
2506
Joerg Roedel576175c2009-11-23 19:08:46 +01002507 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002508 goto out;
2509
2510 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002511 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002512 * allocation again
2513 */
2514 goto retry;
2515 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002516
2517 start = address;
2518 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002519 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002520 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002521 goto out_unmap;
2522
Joerg Roedelcb76c322008-06-26 21:28:00 +02002523 paddr += PAGE_SIZE;
2524 start += PAGE_SIZE;
2525 }
2526 address += offset;
2527
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002528 ADD_STATS_COUNTER(alloced_io_mem, size);
2529
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002530 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002531 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002532 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002533 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002534 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002535
Joerg Roedelcb76c322008-06-26 21:28:00 +02002536out:
2537 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002538
2539out_unmap:
2540
2541 for (--i; i >= 0; --i) {
2542 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002543 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002544 }
2545
2546 dma_ops_free_addresses(dma_dom, address, pages);
2547
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002548 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002549}
2550
Joerg Roedel431b2a22008-07-11 17:14:22 +02002551/*
2552 * Does the reverse of the __map_single function. Must be called with
2553 * the domain lock held too
2554 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002555static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002556 dma_addr_t dma_addr,
2557 size_t size,
2558 int dir)
2559{
Joerg Roedel04e04632010-09-23 16:12:48 +02002560 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002561 dma_addr_t i, start;
2562 unsigned int pages;
2563
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002564 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002565 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002566 return;
2567
Joerg Roedel04e04632010-09-23 16:12:48 +02002568 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002569 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002570 dma_addr &= PAGE_MASK;
2571 start = dma_addr;
2572
2573 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002574 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002575 start += PAGE_SIZE;
2576 }
2577
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002578 SUB_STATS_COUNTER(alloced_io_mem, size);
2579
Joerg Roedelcb76c322008-06-26 21:28:00 +02002580 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002581
Joerg Roedel80be3082008-11-06 14:59:05 +01002582 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002583 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002584 dma_dom->need_flush = false;
2585 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002586}
2587
Joerg Roedel431b2a22008-07-11 17:14:22 +02002588/*
2589 * The exported map_single function for dma_ops.
2590 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002591static dma_addr_t map_page(struct device *dev, struct page *page,
2592 unsigned long offset, size_t size,
2593 enum dma_data_direction dir,
2594 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002595{
2596 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002597 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002598 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002599 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002600 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002601
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002602 INC_STATS_COUNTER(cnt_map_single);
2603
Joerg Roedel94f6d192009-11-24 16:40:02 +01002604 domain = get_domain(dev);
2605 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002606 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002607 else if (IS_ERR(domain))
2608 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002609
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002610 dma_mask = *dev->dma_mask;
2611
Joerg Roedel4da70b92008-06-26 21:28:01 +02002612 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002613
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002614 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002615 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002616 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002617 goto out;
2618
Joerg Roedel17b124b2011-04-06 18:01:35 +02002619 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002620
2621out:
2622 spin_unlock_irqrestore(&domain->lock, flags);
2623
2624 return addr;
2625}
2626
Joerg Roedel431b2a22008-07-11 17:14:22 +02002627/*
2628 * The exported unmap_single function for dma_ops.
2629 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002630static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2631 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002632{
2633 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002634 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002635
Joerg Roedel146a6912008-12-12 15:07:12 +01002636 INC_STATS_COUNTER(cnt_unmap_single);
2637
Joerg Roedel94f6d192009-11-24 16:40:02 +01002638 domain = get_domain(dev);
2639 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002640 return;
2641
Joerg Roedel4da70b92008-06-26 21:28:01 +02002642 spin_lock_irqsave(&domain->lock, flags);
2643
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002644 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002645
Joerg Roedel17b124b2011-04-06 18:01:35 +02002646 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002647
2648 spin_unlock_irqrestore(&domain->lock, flags);
2649}
2650
Joerg Roedel431b2a22008-07-11 17:14:22 +02002651/*
2652 * This is a special map_sg function which is used if we should map a
2653 * device which is not handled by an AMD IOMMU in the system.
2654 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002655static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2656 int nelems, int dir)
2657{
2658 struct scatterlist *s;
2659 int i;
2660
2661 for_each_sg(sglist, s, nelems, i) {
2662 s->dma_address = (dma_addr_t)sg_phys(s);
2663 s->dma_length = s->length;
2664 }
2665
2666 return nelems;
2667}
2668
Joerg Roedel431b2a22008-07-11 17:14:22 +02002669/*
2670 * The exported map_sg function for dma_ops (handles scatter-gather
2671 * lists).
2672 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002673static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002674 int nelems, enum dma_data_direction dir,
2675 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002676{
2677 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002678 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002679 int i;
2680 struct scatterlist *s;
2681 phys_addr_t paddr;
2682 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002683 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002684
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002685 INC_STATS_COUNTER(cnt_map_sg);
2686
Joerg Roedel94f6d192009-11-24 16:40:02 +01002687 domain = get_domain(dev);
2688 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002689 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002690 else if (IS_ERR(domain))
2691 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002692
Joerg Roedel832a90c2008-09-18 15:54:23 +02002693 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002694
Joerg Roedel65b050a2008-06-26 21:28:02 +02002695 spin_lock_irqsave(&domain->lock, flags);
2696
2697 for_each_sg(sglist, s, nelems, i) {
2698 paddr = sg_phys(s);
2699
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002700 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002701 paddr, s->length, dir, false,
2702 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002703
2704 if (s->dma_address) {
2705 s->dma_length = s->length;
2706 mapped_elems++;
2707 } else
2708 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002709 }
2710
Joerg Roedel17b124b2011-04-06 18:01:35 +02002711 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002712
2713out:
2714 spin_unlock_irqrestore(&domain->lock, flags);
2715
2716 return mapped_elems;
2717unmap:
2718 for_each_sg(sglist, s, mapped_elems, i) {
2719 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002720 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002721 s->dma_length, dir);
2722 s->dma_address = s->dma_length = 0;
2723 }
2724
2725 mapped_elems = 0;
2726
2727 goto out;
2728}
2729
Joerg Roedel431b2a22008-07-11 17:14:22 +02002730/*
2731 * The exported map_sg function for dma_ops (handles scatter-gather
2732 * lists).
2733 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002734static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002735 int nelems, enum dma_data_direction dir,
2736 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002737{
2738 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002739 struct protection_domain *domain;
2740 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002741 int i;
2742
Joerg Roedel55877a62008-12-12 15:12:14 +01002743 INC_STATS_COUNTER(cnt_unmap_sg);
2744
Joerg Roedel94f6d192009-11-24 16:40:02 +01002745 domain = get_domain(dev);
2746 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002747 return;
2748
Joerg Roedel65b050a2008-06-26 21:28:02 +02002749 spin_lock_irqsave(&domain->lock, flags);
2750
2751 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002752 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002753 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002754 s->dma_address = s->dma_length = 0;
2755 }
2756
Joerg Roedel17b124b2011-04-06 18:01:35 +02002757 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002758
2759 spin_unlock_irqrestore(&domain->lock, flags);
2760}
2761
Joerg Roedel431b2a22008-07-11 17:14:22 +02002762/*
2763 * The exported alloc_coherent function for dma_ops.
2764 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002765static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002766 dma_addr_t *dma_addr, gfp_t flag,
2767 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002768{
2769 unsigned long flags;
2770 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002771 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002772 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002773 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002774
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002775 INC_STATS_COUNTER(cnt_alloc_coherent);
2776
Joerg Roedel94f6d192009-11-24 16:40:02 +01002777 domain = get_domain(dev);
2778 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002779 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2780 *dma_addr = __pa(virt_addr);
2781 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002782 } else if (IS_ERR(domain))
2783 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002784
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002785 dma_mask = dev->coherent_dma_mask;
2786 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2787 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002788
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002789 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2790 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302791 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002792
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002793 paddr = virt_to_phys(virt_addr);
2794
Joerg Roedel832a90c2008-09-18 15:54:23 +02002795 if (!dma_mask)
2796 dma_mask = *dev->dma_mask;
2797
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002798 spin_lock_irqsave(&domain->lock, flags);
2799
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002800 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002801 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002802
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002803 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002804 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002805 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002806 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002807
Joerg Roedel17b124b2011-04-06 18:01:35 +02002808 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002809
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002810 spin_unlock_irqrestore(&domain->lock, flags);
2811
2812 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002813
2814out_free:
2815
2816 free_pages((unsigned long)virt_addr, get_order(size));
2817
2818 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002819}
2820
Joerg Roedel431b2a22008-07-11 17:14:22 +02002821/*
2822 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002823 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002824static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002825 void *virt_addr, dma_addr_t dma_addr,
2826 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002827{
2828 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002829 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002830
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002831 INC_STATS_COUNTER(cnt_free_coherent);
2832
Joerg Roedel94f6d192009-11-24 16:40:02 +01002833 domain = get_domain(dev);
2834 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002835 goto free_mem;
2836
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002837 spin_lock_irqsave(&domain->lock, flags);
2838
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002839 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002840
Joerg Roedel17b124b2011-04-06 18:01:35 +02002841 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002842
2843 spin_unlock_irqrestore(&domain->lock, flags);
2844
2845free_mem:
2846 free_pages((unsigned long)virt_addr, get_order(size));
2847}
2848
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002849/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002850 * This function is called by the DMA layer to find out if we can handle a
2851 * particular device. It is part of the dma_ops.
2852 */
2853static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2854{
Joerg Roedel420aef82009-11-23 16:14:57 +01002855 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002856}
2857
2858/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002859 * The function for pre-allocating protection domains.
2860 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002861 * If the driver core informs the DMA layer if a driver grabs a device
2862 * we don't need to preallocate the protection domains anymore.
2863 * For now we have to.
2864 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01002865static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002866{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002867 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002868 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002869 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002870 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002871
Chris Wrightd18c69d2010-04-02 18:27:55 -07002872 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002873
2874 /* Do we handle this device? */
2875 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002876 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002877
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002878 dev_data = get_dev_data(&dev->dev);
2879 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2880 /* Make sure passthrough domain is allocated */
2881 alloc_passthrough_domain();
2882 dev_data->passthrough = true;
2883 attach_device(&dev->dev, pt_domain);
2884 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2885 dev_name(&dev->dev));
2886 }
2887
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002888 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002889 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002890 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002891
2892 devid = get_device_id(&dev->dev);
2893
Joerg Roedel87a64d52009-11-24 17:26:43 +01002894 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002895 if (!dma_dom)
2896 continue;
2897 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002898 dma_dom->target_dev = devid;
2899
Joerg Roedel15898bb2009-11-24 15:39:42 +01002900 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002901
Joerg Roedelbd60b732008-09-11 10:24:48 +02002902 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002903 }
2904}
2905
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002906static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002907 .alloc = alloc_coherent,
2908 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002909 .map_page = map_page,
2910 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002911 .map_sg = map_sg,
2912 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002913 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002914};
2915
Joerg Roedel27c21272011-05-30 15:56:24 +02002916static unsigned device_dma_ops_init(void)
2917{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002918 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002919 struct pci_dev *pdev = NULL;
2920 unsigned unhandled = 0;
2921
2922 for_each_pci_dev(pdev) {
2923 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01002924
2925 iommu_ignore_device(&pdev->dev);
2926
Joerg Roedel27c21272011-05-30 15:56:24 +02002927 unhandled += 1;
2928 continue;
2929 }
2930
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002931 dev_data = get_dev_data(&pdev->dev);
2932
2933 if (!dev_data->passthrough)
2934 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2935 else
2936 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02002937 }
2938
2939 return unhandled;
2940}
2941
Joerg Roedel431b2a22008-07-11 17:14:22 +02002942/*
2943 * The function which clues the AMD IOMMU driver into dma_ops.
2944 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002945
2946void __init amd_iommu_init_api(void)
2947{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02002948 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002949}
2950
Joerg Roedel6631ee92008-06-26 21:28:05 +02002951int __init amd_iommu_init_dma_ops(void)
2952{
2953 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002954 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002955
Joerg Roedel431b2a22008-07-11 17:14:22 +02002956 /*
2957 * first allocate a default protection domain for every IOMMU we
2958 * found in the system. Devices not assigned to any other
2959 * protection domain will be assigned to the default one.
2960 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002961 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002962 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002963 if (iommu->default_dom == NULL)
2964 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002965 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002966 ret = iommu_init_unity_mappings(iommu);
2967 if (ret)
2968 goto free_domains;
2969 }
2970
Joerg Roedel431b2a22008-07-11 17:14:22 +02002971 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002972 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002973 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002974 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002975
2976 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002977 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002978
Joerg Roedel431b2a22008-07-11 17:14:22 +02002979 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002980 unhandled = device_dma_ops_init();
2981 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2982 /* There are unhandled devices - initialize swiotlb for them */
2983 swiotlb = 1;
2984 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002985
Joerg Roedel7f265082008-12-12 13:50:21 +01002986 amd_iommu_stats_init();
2987
Joerg Roedel6631ee92008-06-26 21:28:05 +02002988 return 0;
2989
2990free_domains:
2991
Joerg Roedel3bd22172009-05-04 15:06:20 +02002992 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002993 if (iommu->default_dom)
2994 dma_ops_domain_free(iommu->default_dom);
2995 }
2996
2997 return ret;
2998}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002999
3000/*****************************************************************************
3001 *
3002 * The following functions belong to the exported interface of AMD IOMMU
3003 *
3004 * This interface allows access to lower level functions of the IOMMU
3005 * like protection domain handling and assignement of devices to domains
3006 * which is not possible with the dma_ops interface.
3007 *
3008 *****************************************************************************/
3009
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003010static void cleanup_domain(struct protection_domain *domain)
3011{
Joerg Roedel492667d2009-11-27 13:25:47 +01003012 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003013 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003014
3015 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3016
Joerg Roedel492667d2009-11-27 13:25:47 +01003017 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003018 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003019 atomic_set(&dev_data->bind, 0);
3020 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003021
3022 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3023}
3024
Joerg Roedel26508152009-08-26 16:52:40 +02003025static void protection_domain_free(struct protection_domain *domain)
3026{
3027 if (!domain)
3028 return;
3029
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003030 del_domain_from_list(domain);
3031
Joerg Roedel26508152009-08-26 16:52:40 +02003032 if (domain->id)
3033 domain_id_free(domain->id);
3034
3035 kfree(domain);
3036}
3037
3038static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003039{
3040 struct protection_domain *domain;
3041
3042 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3043 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003044 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003045
3046 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003047 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003048 domain->id = domain_id_alloc();
3049 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003050 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003051 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003052
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003053 add_domain_to_list(domain);
3054
Joerg Roedel26508152009-08-26 16:52:40 +02003055 return domain;
3056
3057out_err:
3058 kfree(domain);
3059
3060 return NULL;
3061}
3062
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003063static int __init alloc_passthrough_domain(void)
3064{
3065 if (pt_domain != NULL)
3066 return 0;
3067
3068 /* allocate passthrough domain */
3069 pt_domain = protection_domain_alloc();
3070 if (!pt_domain)
3071 return -ENOMEM;
3072
3073 pt_domain->mode = PAGE_MODE_NONE;
3074
3075 return 0;
3076}
Joerg Roedel26508152009-08-26 16:52:40 +02003077static int amd_iommu_domain_init(struct iommu_domain *dom)
3078{
3079 struct protection_domain *domain;
3080
3081 domain = protection_domain_alloc();
3082 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003083 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003084
3085 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003086 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3087 if (!domain->pt_root)
3088 goto out_free;
3089
Joerg Roedelf3572db2011-11-23 12:36:25 +01003090 domain->iommu_domain = dom;
3091
Joerg Roedelc156e342008-12-02 18:13:27 +01003092 dom->priv = domain;
3093
3094 return 0;
3095
3096out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003097 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003098
3099 return -ENOMEM;
3100}
3101
Joerg Roedel98383fc2008-12-02 18:34:12 +01003102static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3103{
3104 struct protection_domain *domain = dom->priv;
3105
3106 if (!domain)
3107 return;
3108
3109 if (domain->dev_cnt > 0)
3110 cleanup_domain(domain);
3111
3112 BUG_ON(domain->dev_cnt != 0);
3113
Joerg Roedel132bd682011-11-17 14:18:46 +01003114 if (domain->mode != PAGE_MODE_NONE)
3115 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003116
Joerg Roedel52815b72011-11-17 17:24:28 +01003117 if (domain->flags & PD_IOMMUV2_MASK)
3118 free_gcr3_table(domain);
3119
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003120 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003121
3122 dom->priv = NULL;
3123}
3124
Joerg Roedel684f2882008-12-08 12:07:44 +01003125static void amd_iommu_detach_device(struct iommu_domain *dom,
3126 struct device *dev)
3127{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003128 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003129 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003130 u16 devid;
3131
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003132 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003133 return;
3134
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003135 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003136
Joerg Roedel657cbb62009-11-23 15:26:46 +01003137 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003138 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003139
3140 iommu = amd_iommu_rlookup_table[devid];
3141 if (!iommu)
3142 return;
3143
Joerg Roedel684f2882008-12-08 12:07:44 +01003144 iommu_completion_wait(iommu);
3145}
3146
Joerg Roedel01106062008-12-02 19:34:11 +01003147static int amd_iommu_attach_device(struct iommu_domain *dom,
3148 struct device *dev)
3149{
3150 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003151 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003152 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003153 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003154
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003155 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003156 return -EINVAL;
3157
Joerg Roedel657cbb62009-11-23 15:26:46 +01003158 dev_data = dev->archdata.iommu;
3159
Joerg Roedelf62dda62011-06-09 12:55:35 +02003160 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003161 if (!iommu)
3162 return -EINVAL;
3163
Joerg Roedel657cbb62009-11-23 15:26:46 +01003164 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003165 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003166
Joerg Roedel15898bb2009-11-24 15:39:42 +01003167 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003168
3169 iommu_completion_wait(iommu);
3170
Joerg Roedel15898bb2009-11-24 15:39:42 +01003171 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003172}
3173
Joerg Roedel468e2362010-01-21 16:37:36 +01003174static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003175 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003176{
3177 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003178 int prot = 0;
3179 int ret;
3180
Joerg Roedel132bd682011-11-17 14:18:46 +01003181 if (domain->mode == PAGE_MODE_NONE)
3182 return -EINVAL;
3183
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003184 if (iommu_prot & IOMMU_READ)
3185 prot |= IOMMU_PROT_IR;
3186 if (iommu_prot & IOMMU_WRITE)
3187 prot |= IOMMU_PROT_IW;
3188
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003189 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003190 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003191 mutex_unlock(&domain->api_lock);
3192
Joerg Roedel795e74f72010-05-11 17:40:57 +02003193 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003194}
3195
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003196static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3197 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003198{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003199 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003200 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003201
Joerg Roedel132bd682011-11-17 14:18:46 +01003202 if (domain->mode == PAGE_MODE_NONE)
3203 return -EINVAL;
3204
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003205 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003206 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003207 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003208
Joerg Roedel17b124b2011-04-06 18:01:35 +02003209 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003210
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003211 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003212}
3213
Joerg Roedel645c4c82008-12-02 20:05:50 +01003214static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3215 unsigned long iova)
3216{
3217 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003218 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003219 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003220 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003221
Joerg Roedel132bd682011-11-17 14:18:46 +01003222 if (domain->mode == PAGE_MODE_NONE)
3223 return iova;
3224
Joerg Roedel24cd7722010-01-19 17:27:39 +01003225 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003226
Joerg Roedela6d41a42009-09-02 17:08:55 +02003227 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003228 return 0;
3229
Joerg Roedelf03152b2010-01-21 16:15:24 +01003230 if (PM_PTE_LEVEL(*pte) == 0)
3231 offset_mask = PAGE_SIZE - 1;
3232 else
3233 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3234
3235 __pte = *pte & PM_ADDR_MASK;
3236 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003237
3238 return paddr;
3239}
3240
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003241static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3242 unsigned long cap)
3243{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003244 switch (cap) {
3245 case IOMMU_CAP_CACHE_COHERENCY:
3246 return 1;
3247 }
3248
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003249 return 0;
3250}
3251
Joerg Roedel26961ef2008-12-03 17:00:17 +01003252static struct iommu_ops amd_iommu_ops = {
3253 .domain_init = amd_iommu_domain_init,
3254 .domain_destroy = amd_iommu_domain_destroy,
3255 .attach_dev = amd_iommu_attach_device,
3256 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003257 .map = amd_iommu_map,
3258 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003259 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003260 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003261 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003262};
3263
Joerg Roedel0feae532009-08-26 15:26:30 +02003264/*****************************************************************************
3265 *
3266 * The next functions do a basic initialization of IOMMU for pass through
3267 * mode
3268 *
3269 * In passthrough mode the IOMMU is initialized and enabled but not used for
3270 * DMA-API translation.
3271 *
3272 *****************************************************************************/
3273
3274int __init amd_iommu_init_passthrough(void)
3275{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003276 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003277 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003278 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003279 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003280 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003281
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003282 ret = alloc_passthrough_domain();
3283 if (ret)
3284 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003285
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003286 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003287 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003288 continue;
3289
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003290 dev_data = get_dev_data(&dev->dev);
3291 dev_data->passthrough = true;
3292
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003293 devid = get_device_id(&dev->dev);
3294
Joerg Roedel15898bb2009-11-24 15:39:42 +01003295 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003296 if (!iommu)
3297 continue;
3298
Joerg Roedel15898bb2009-11-24 15:39:42 +01003299 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003300 }
3301
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003302 amd_iommu_stats_init();
3303
Joerg Roedel0feae532009-08-26 15:26:30 +02003304 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3305
3306 return 0;
3307}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003308
3309/* IOMMUv2 specific functions */
3310int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3311{
3312 return atomic_notifier_chain_register(&ppr_notifier, nb);
3313}
3314EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3315
3316int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3317{
3318 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3319}
3320EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003321
3322void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3323{
3324 struct protection_domain *domain = dom->priv;
3325 unsigned long flags;
3326
3327 spin_lock_irqsave(&domain->lock, flags);
3328
3329 /* Update data structure */
3330 domain->mode = PAGE_MODE_NONE;
3331 domain->updated = true;
3332
3333 /* Make changes visible to IOMMUs */
3334 update_domain(domain);
3335
3336 /* Page-table is not visible to IOMMU anymore, so free it */
3337 free_pagetable(domain);
3338
3339 spin_unlock_irqrestore(&domain->lock, flags);
3340}
3341EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003342
3343int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3344{
3345 struct protection_domain *domain = dom->priv;
3346 unsigned long flags;
3347 int levels, ret;
3348
3349 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3350 return -EINVAL;
3351
3352 /* Number of GCR3 table levels required */
3353 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3354 levels += 1;
3355
3356 if (levels > amd_iommu_max_glx_val)
3357 return -EINVAL;
3358
3359 spin_lock_irqsave(&domain->lock, flags);
3360
3361 /*
3362 * Save us all sanity checks whether devices already in the
3363 * domain support IOMMUv2. Just force that the domain has no
3364 * devices attached when it is switched into IOMMUv2 mode.
3365 */
3366 ret = -EBUSY;
3367 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3368 goto out;
3369
3370 ret = -ENOMEM;
3371 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3372 if (domain->gcr3_tbl == NULL)
3373 goto out;
3374
3375 domain->glx = levels;
3376 domain->flags |= PD_IOMMUV2_MASK;
3377 domain->updated = true;
3378
3379 update_domain(domain);
3380
3381 ret = 0;
3382
3383out:
3384 spin_unlock_irqrestore(&domain->lock, flags);
3385
3386 return ret;
3387}
3388EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003389
3390static int __flush_pasid(struct protection_domain *domain, int pasid,
3391 u64 address, bool size)
3392{
3393 struct iommu_dev_data *dev_data;
3394 struct iommu_cmd cmd;
3395 int i, ret;
3396
3397 if (!(domain->flags & PD_IOMMUV2_MASK))
3398 return -EINVAL;
3399
3400 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3401
3402 /*
3403 * IOMMU TLB needs to be flushed before Device TLB to
3404 * prevent device TLB refill from IOMMU TLB
3405 */
3406 for (i = 0; i < amd_iommus_present; ++i) {
3407 if (domain->dev_iommu[i] == 0)
3408 continue;
3409
3410 ret = iommu_queue_command(amd_iommus[i], &cmd);
3411 if (ret != 0)
3412 goto out;
3413 }
3414
3415 /* Wait until IOMMU TLB flushes are complete */
3416 domain_flush_complete(domain);
3417
3418 /* Now flush device TLBs */
3419 list_for_each_entry(dev_data, &domain->dev_list, list) {
3420 struct amd_iommu *iommu;
3421 int qdep;
3422
3423 BUG_ON(!dev_data->ats.enabled);
3424
3425 qdep = dev_data->ats.qdep;
3426 iommu = amd_iommu_rlookup_table[dev_data->devid];
3427
3428 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3429 qdep, address, size);
3430
3431 ret = iommu_queue_command(iommu, &cmd);
3432 if (ret != 0)
3433 goto out;
3434 }
3435
3436 /* Wait until all device TLBs are flushed */
3437 domain_flush_complete(domain);
3438
3439 ret = 0;
3440
3441out:
3442
3443 return ret;
3444}
3445
3446static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3447 u64 address)
3448{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003449 INC_STATS_COUNTER(invalidate_iotlb);
3450
Joerg Roedel22e266c2011-11-21 15:59:08 +01003451 return __flush_pasid(domain, pasid, address, false);
3452}
3453
3454int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3455 u64 address)
3456{
3457 struct protection_domain *domain = dom->priv;
3458 unsigned long flags;
3459 int ret;
3460
3461 spin_lock_irqsave(&domain->lock, flags);
3462 ret = __amd_iommu_flush_page(domain, pasid, address);
3463 spin_unlock_irqrestore(&domain->lock, flags);
3464
3465 return ret;
3466}
3467EXPORT_SYMBOL(amd_iommu_flush_page);
3468
3469static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3470{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003471 INC_STATS_COUNTER(invalidate_iotlb_all);
3472
Joerg Roedel22e266c2011-11-21 15:59:08 +01003473 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3474 true);
3475}
3476
3477int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3478{
3479 struct protection_domain *domain = dom->priv;
3480 unsigned long flags;
3481 int ret;
3482
3483 spin_lock_irqsave(&domain->lock, flags);
3484 ret = __amd_iommu_flush_tlb(domain, pasid);
3485 spin_unlock_irqrestore(&domain->lock, flags);
3486
3487 return ret;
3488}
3489EXPORT_SYMBOL(amd_iommu_flush_tlb);
3490
Joerg Roedelb16137b2011-11-21 16:50:23 +01003491static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3492{
3493 int index;
3494 u64 *pte;
3495
3496 while (true) {
3497
3498 index = (pasid >> (9 * level)) & 0x1ff;
3499 pte = &root[index];
3500
3501 if (level == 0)
3502 break;
3503
3504 if (!(*pte & GCR3_VALID)) {
3505 if (!alloc)
3506 return NULL;
3507
3508 root = (void *)get_zeroed_page(GFP_ATOMIC);
3509 if (root == NULL)
3510 return NULL;
3511
3512 *pte = __pa(root) | GCR3_VALID;
3513 }
3514
3515 root = __va(*pte & PAGE_MASK);
3516
3517 level -= 1;
3518 }
3519
3520 return pte;
3521}
3522
3523static int __set_gcr3(struct protection_domain *domain, int pasid,
3524 unsigned long cr3)
3525{
3526 u64 *pte;
3527
3528 if (domain->mode != PAGE_MODE_NONE)
3529 return -EINVAL;
3530
3531 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3532 if (pte == NULL)
3533 return -ENOMEM;
3534
3535 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3536
3537 return __amd_iommu_flush_tlb(domain, pasid);
3538}
3539
3540static int __clear_gcr3(struct protection_domain *domain, int pasid)
3541{
3542 u64 *pte;
3543
3544 if (domain->mode != PAGE_MODE_NONE)
3545 return -EINVAL;
3546
3547 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3548 if (pte == NULL)
3549 return 0;
3550
3551 *pte = 0;
3552
3553 return __amd_iommu_flush_tlb(domain, pasid);
3554}
3555
3556int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3557 unsigned long cr3)
3558{
3559 struct protection_domain *domain = dom->priv;
3560 unsigned long flags;
3561 int ret;
3562
3563 spin_lock_irqsave(&domain->lock, flags);
3564 ret = __set_gcr3(domain, pasid, cr3);
3565 spin_unlock_irqrestore(&domain->lock, flags);
3566
3567 return ret;
3568}
3569EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3570
3571int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3572{
3573 struct protection_domain *domain = dom->priv;
3574 unsigned long flags;
3575 int ret;
3576
3577 spin_lock_irqsave(&domain->lock, flags);
3578 ret = __clear_gcr3(domain, pasid);
3579 spin_unlock_irqrestore(&domain->lock, flags);
3580
3581 return ret;
3582}
3583EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003584
3585int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3586 int status, int tag)
3587{
3588 struct iommu_dev_data *dev_data;
3589 struct amd_iommu *iommu;
3590 struct iommu_cmd cmd;
3591
Joerg Roedel399be2f2011-12-01 16:53:47 +01003592 INC_STATS_COUNTER(complete_ppr);
3593
Joerg Roedelc99afa22011-11-21 18:19:25 +01003594 dev_data = get_dev_data(&pdev->dev);
3595 iommu = amd_iommu_rlookup_table[dev_data->devid];
3596
3597 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3598 tag, dev_data->pri_tlp);
3599
3600 return iommu_queue_command(iommu, &cmd);
3601}
3602EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003603
3604struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3605{
3606 struct protection_domain *domain;
3607
3608 domain = get_domain(&pdev->dev);
3609 if (IS_ERR(domain))
3610 return NULL;
3611
3612 /* Only return IOMMUv2 domains */
3613 if (!(domain->flags & PD_IOMMUV2_MASK))
3614 return NULL;
3615
3616 return domain->iommu_domain;
3617}
3618EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003619
3620void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3621{
3622 struct iommu_dev_data *dev_data;
3623
3624 if (!amd_iommu_v2_supported())
3625 return;
3626
3627 dev_data = get_dev_data(&pdev->dev);
3628 dev_data->errata |= (1 << erratum);
3629}
3630EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003631
3632int amd_iommu_device_info(struct pci_dev *pdev,
3633 struct amd_iommu_device_info *info)
3634{
3635 int max_pasids;
3636 int pos;
3637
3638 if (pdev == NULL || info == NULL)
3639 return -EINVAL;
3640
3641 if (!amd_iommu_v2_supported())
3642 return -EINVAL;
3643
3644 memset(info, 0, sizeof(*info));
3645
3646 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3647 if (pos)
3648 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3649
3650 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3651 if (pos)
3652 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3653
3654 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3655 if (pos) {
3656 int features;
3657
3658 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3659 max_pasids = min(max_pasids, (1 << 20));
3660
3661 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3662 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3663
3664 features = pci_pasid_features(pdev);
3665 if (features & PCI_PASID_CAP_EXEC)
3666 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3667 if (features & PCI_PASID_CAP_PRIV)
3668 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3669 }
3670
3671 return 0;
3672}
3673EXPORT_SYMBOL(amd_iommu_device_info);