blob: c0536eda58e9d0e106efa5ed8efa5cfc38b7928f [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 Roedelb9e619e2012-06-21 14:52:40 +020086static struct dma_map_ops amd_iommu_dma_ops;
87
Joerg Roedel431b2a22008-07-11 17:14:22 +020088/*
89 * general struct to manage commands send to an IOMMU
90 */
Joerg Roedeld6449532008-07-11 17:14:28 +020091struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020092 u32 data[4];
93};
94
Joerg Roedel04bfdd82009-09-02 16:00:23 +020095static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +010096static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -070097
Joerg Roedel15898bb2009-11-24 15:39:42 +010098/****************************************************************************
99 *
100 * Helper functions
101 *
102 ****************************************************************************/
103
Joerg Roedelf62dda62011-06-09 12:55:35 +0200104static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200105{
106 struct iommu_dev_data *dev_data;
107 unsigned long flags;
108
109 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
110 if (!dev_data)
111 return NULL;
112
Joerg Roedelf62dda62011-06-09 12:55:35 +0200113 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200114 atomic_set(&dev_data->bind, 0);
115
116 spin_lock_irqsave(&dev_data_list_lock, flags);
117 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
118 spin_unlock_irqrestore(&dev_data_list_lock, flags);
119
120 return dev_data;
121}
122
123static void free_dev_data(struct iommu_dev_data *dev_data)
124{
125 unsigned long flags;
126
127 spin_lock_irqsave(&dev_data_list_lock, flags);
128 list_del(&dev_data->dev_data_list);
129 spin_unlock_irqrestore(&dev_data_list_lock, flags);
130
131 kfree(dev_data);
132}
133
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200134static struct iommu_dev_data *search_dev_data(u16 devid)
135{
136 struct iommu_dev_data *dev_data;
137 unsigned long flags;
138
139 spin_lock_irqsave(&dev_data_list_lock, flags);
140 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
141 if (dev_data->devid == devid)
142 goto out_unlock;
143 }
144
145 dev_data = NULL;
146
147out_unlock:
148 spin_unlock_irqrestore(&dev_data_list_lock, flags);
149
150 return dev_data;
151}
152
153static struct iommu_dev_data *find_dev_data(u16 devid)
154{
155 struct iommu_dev_data *dev_data;
156
157 dev_data = search_dev_data(devid);
158
159 if (dev_data == NULL)
160 dev_data = alloc_dev_data(devid);
161
162 return dev_data;
163}
164
Joerg Roedel15898bb2009-11-24 15:39:42 +0100165static inline u16 get_device_id(struct device *dev)
166{
167 struct pci_dev *pdev = to_pci_dev(dev);
168
169 return calc_devid(pdev->bus->number, pdev->devfn);
170}
171
Joerg Roedel657cbb62009-11-23 15:26:46 +0100172static struct iommu_dev_data *get_dev_data(struct device *dev)
173{
174 return dev->archdata.iommu;
175}
176
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100177static bool pci_iommuv2_capable(struct pci_dev *pdev)
178{
179 static const int caps[] = {
180 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100181 PCI_EXT_CAP_ID_PRI,
182 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100183 };
184 int i, pos;
185
186 for (i = 0; i < 3; ++i) {
187 pos = pci_find_ext_capability(pdev, caps[i]);
188 if (pos == 0)
189 return false;
190 }
191
192 return true;
193}
194
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100195static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
196{
197 struct iommu_dev_data *dev_data;
198
199 dev_data = get_dev_data(&pdev->dev);
200
201 return dev_data->errata & (1 << erratum) ? true : false;
202}
203
Joerg Roedel71c70982009-11-24 16:43:06 +0100204/*
205 * In this function the list of preallocated protection domains is traversed to
206 * find the domain for a specific device
207 */
208static struct dma_ops_domain *find_protection_domain(u16 devid)
209{
210 struct dma_ops_domain *entry, *ret = NULL;
211 unsigned long flags;
212 u16 alias = amd_iommu_alias_table[devid];
213
214 if (list_empty(&iommu_pd_list))
215 return NULL;
216
217 spin_lock_irqsave(&iommu_pd_list_lock, flags);
218
219 list_for_each_entry(entry, &iommu_pd_list, list) {
220 if (entry->target_dev == devid ||
221 entry->target_dev == alias) {
222 ret = entry;
223 break;
224 }
225 }
226
227 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
228
229 return ret;
230}
231
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100232/*
233 * This function checks if the driver got a valid device from the caller to
234 * avoid dereferencing invalid pointers.
235 */
236static bool check_device(struct device *dev)
237{
238 u16 devid;
239
240 if (!dev || !dev->dma_mask)
241 return false;
242
243 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100244 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100245 return false;
246
247 devid = get_device_id(dev);
248
249 /* Out of our scope? */
250 if (devid > amd_iommu_last_bdf)
251 return false;
252
253 if (amd_iommu_rlookup_table[devid] == NULL)
254 return false;
255
256 return true;
257}
258
Joerg Roedel657cbb62009-11-23 15:26:46 +0100259static int iommu_init_device(struct device *dev)
260{
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100261 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100262 struct iommu_dev_data *dev_data;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200263 u16 alias;
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;
Joerg Roedel26018872011-06-06 16:50:14 +0200284 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100285
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100286 if (pci_iommuv2_capable(pdev)) {
287 struct amd_iommu *iommu;
288
289 iommu = amd_iommu_rlookup_table[dev_data->devid];
290 dev_data->iommu_v2 = iommu->is_iommu_v2;
291 }
292
Joerg Roedel657cbb62009-11-23 15:26:46 +0100293 dev->archdata.iommu = dev_data;
294
Joerg Roedel657cbb62009-11-23 15:26:46 +0100295 return 0;
296}
297
Joerg Roedel26018872011-06-06 16:50:14 +0200298static void iommu_ignore_device(struct device *dev)
299{
300 u16 devid, alias;
301
302 devid = get_device_id(dev);
303 alias = amd_iommu_alias_table[devid];
304
305 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
306 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
307
308 amd_iommu_rlookup_table[devid] = NULL;
309 amd_iommu_rlookup_table[alias] = NULL;
310}
311
Joerg Roedel657cbb62009-11-23 15:26:46 +0100312static void iommu_uninit_device(struct device *dev)
313{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200314 /*
315 * Nothing to do here - we keep dev_data around for unplugged devices
316 * and reuse it when the device is re-plugged - not doing so would
317 * introduce a ton of races.
318 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100319}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100320
321void __init amd_iommu_uninit_devices(void)
322{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200323 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100324 struct pci_dev *pdev = NULL;
325
326 for_each_pci_dev(pdev) {
327
328 if (!check_device(&pdev->dev))
329 continue;
330
331 iommu_uninit_device(&pdev->dev);
332 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200333
334 /* Free all of our dev_data structures */
335 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
336 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100337}
338
339int __init amd_iommu_init_devices(void)
340{
341 struct pci_dev *pdev = NULL;
342 int ret = 0;
343
344 for_each_pci_dev(pdev) {
345
346 if (!check_device(&pdev->dev))
347 continue;
348
349 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200350 if (ret == -ENOTSUPP)
351 iommu_ignore_device(&pdev->dev);
352 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100353 goto out_free;
354 }
355
356 return 0;
357
358out_free:
359
360 amd_iommu_uninit_devices();
361
362 return ret;
363}
Joerg Roedel7f265082008-12-12 13:50:21 +0100364#ifdef CONFIG_AMD_IOMMU_STATS
365
366/*
367 * Initialization code for statistics collection
368 */
369
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100370DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100371DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100372DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100373DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100374DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100375DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100376DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100377DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100378DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100379DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100380DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100381DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100382DECLARE_STATS_COUNTER(complete_ppr);
383DECLARE_STATS_COUNTER(invalidate_iotlb);
384DECLARE_STATS_COUNTER(invalidate_iotlb_all);
385DECLARE_STATS_COUNTER(pri_requests);
386
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100387
Joerg Roedel7f265082008-12-12 13:50:21 +0100388static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100389static struct dentry *de_fflush;
390
391static void amd_iommu_stats_add(struct __iommu_counter *cnt)
392{
393 if (stats_dir == NULL)
394 return;
395
396 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
397 &cnt->value);
398}
399
400static void amd_iommu_stats_init(void)
401{
402 stats_dir = debugfs_create_dir("amd-iommu", NULL);
403 if (stats_dir == NULL)
404 return;
405
Joerg Roedel7f265082008-12-12 13:50:21 +0100406 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
407 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100408
409 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100410 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100411 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100412 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100413 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100414 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100415 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100416 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100417 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100418 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100419 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100420 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100421 amd_iommu_stats_add(&complete_ppr);
422 amd_iommu_stats_add(&invalidate_iotlb);
423 amd_iommu_stats_add(&invalidate_iotlb_all);
424 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100425}
426
427#endif
428
Joerg Roedel431b2a22008-07-11 17:14:22 +0200429/****************************************************************************
430 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200431 * Interrupt handling functions
432 *
433 ****************************************************************************/
434
Joerg Roedele3e59872009-09-03 14:02:10 +0200435static void dump_dte_entry(u16 devid)
436{
437 int i;
438
Joerg Roedelee6c2862011-11-09 12:06:03 +0100439 for (i = 0; i < 4; ++i)
440 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200441 amd_iommu_dev_table[devid].data[i]);
442}
443
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200444static void dump_command(unsigned long phys_addr)
445{
446 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
447 int i;
448
449 for (i = 0; i < 4; ++i)
450 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
451}
452
Joerg Roedela345b232009-09-03 15:01:43 +0200453static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200454{
Joerg Roedelc2fcf3a2012-04-12 14:12:00 +0200455 int type, devid, domid, flags;
456 volatile u32 *event = __evt;
457 int count = 0;
458 u64 address;
459
460retry:
461 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
462 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
463 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
464 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
465 address = (u64)(((u64)event[3]) << 32) | event[2];
466
467 if (type == 0) {
468 /* Did we hit the erratum? */
469 if (++count == LOOP_TIMEOUT) {
470 pr_err("AMD-Vi: No event written to event log\n");
471 return;
472 }
473 udelay(1);
474 goto retry;
475 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200476
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200477 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200478
479 switch (type) {
480 case EVENT_TYPE_ILL_DEV:
481 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
482 "address=0x%016llx flags=0x%04x]\n",
483 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
484 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200485 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200486 break;
487 case EVENT_TYPE_IO_FAULT:
488 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
489 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
490 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
491 domid, address, flags);
492 break;
493 case EVENT_TYPE_DEV_TAB_ERR:
494 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
495 "address=0x%016llx flags=0x%04x]\n",
496 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
497 address, flags);
498 break;
499 case EVENT_TYPE_PAGE_TAB_ERR:
500 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
501 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
502 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
503 domid, address, flags);
504 break;
505 case EVENT_TYPE_ILL_CMD:
506 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200507 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200508 break;
509 case EVENT_TYPE_CMD_HARD_ERR:
510 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
511 "flags=0x%04x]\n", address, flags);
512 break;
513 case EVENT_TYPE_IOTLB_INV_TO:
514 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
515 "address=0x%016llx]\n",
516 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
517 address);
518 break;
519 case EVENT_TYPE_INV_DEV_REQ:
520 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
521 "address=0x%016llx flags=0x%04x]\n",
522 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
523 address, flags);
524 break;
525 default:
526 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
527 }
Joerg Roedelc2fcf3a2012-04-12 14:12:00 +0200528
529 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200530}
531
532static void iommu_poll_events(struct amd_iommu *iommu)
533{
Joerg Roedel52462d22013-04-18 17:55:04 +0200534 u32 head, tail, status;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200535 unsigned long flags;
536
537 spin_lock_irqsave(&iommu->lock, flags);
538
Joerg Roedel52462d22013-04-18 17:55:04 +0200539 /* enable event interrupts again */
540 do {
541 /*
542 * Workaround for Erratum ERBT1312
543 * Clearing the EVT_INT bit may race in the hardware, so read
544 * it again and make sure it was really cleared
545 */
546 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
547 writel(MMIO_STATUS_EVT_INT_MASK,
548 iommu->mmio_base + MMIO_STATUS_OFFSET);
549 } while (status & MMIO_STATUS_EVT_INT_MASK);
550
Joerg Roedel90008ee2008-09-09 16:41:05 +0200551 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
552 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
553
554 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200555 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200556 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
557 }
558
559 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
560
561 spin_unlock_irqrestore(&iommu->lock, flags);
562}
563
Joerg Roedel3e743872012-06-01 15:20:23 +0200564static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100565{
566 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100567
Joerg Roedel399be2f2011-12-01 16:53:47 +0100568 INC_STATS_COUNTER(pri_requests);
569
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100570 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
571 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
572 return;
573 }
574
575 fault.address = raw[1];
576 fault.pasid = PPR_PASID(raw[0]);
577 fault.device_id = PPR_DEVID(raw[0]);
578 fault.tag = PPR_TAG(raw[0]);
579 fault.flags = PPR_FLAGS(raw[0]);
580
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100581 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
582}
583
584static void iommu_poll_ppr_log(struct amd_iommu *iommu)
585{
586 unsigned long flags;
Joerg Roedel52462d22013-04-18 17:55:04 +0200587 u32 head, tail, status;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100588
589 if (iommu->ppr_log == NULL)
590 return;
591
592 spin_lock_irqsave(&iommu->lock, flags);
593
Joerg Roedel52462d22013-04-18 17:55:04 +0200594 /* enable ppr interrupts again */
595 do {
596 /*
597 * Workaround for Erratum ERBT1312
598 * Clearing the PPR_INT bit may race in the hardware, so read
599 * it again and make sure it was really cleared
600 */
601 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
602 writel(MMIO_STATUS_PPR_INT_MASK,
603 iommu->mmio_base + MMIO_STATUS_OFFSET);
604 } while (status & MMIO_STATUS_PPR_INT_MASK);
605
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100606 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
607 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
608
609 while (head != tail) {
Joerg Roedel3e743872012-06-01 15:20:23 +0200610 volatile u64 *raw;
611 u64 entry[2];
612 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100613
Joerg Roedel3e743872012-06-01 15:20:23 +0200614 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100615
Joerg Roedel3e743872012-06-01 15:20:23 +0200616 /*
617 * Hardware bug: Interrupt may arrive before the entry is
618 * written to memory. If this happens we need to wait for the
619 * entry to arrive.
620 */
621 for (i = 0; i < LOOP_TIMEOUT; ++i) {
622 if (PPR_REQ_TYPE(raw[0]) != 0)
623 break;
624 udelay(1);
625 }
626
627 /* Avoid memcpy function-call overhead */
628 entry[0] = raw[0];
629 entry[1] = raw[1];
630
631 /*
632 * To detect the hardware bug we need to clear the entry
633 * back to zero.
634 */
635 raw[0] = raw[1] = 0UL;
636
637 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100638 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
639 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel3e743872012-06-01 15:20:23 +0200640
641 /*
642 * Release iommu->lock because ppr-handling might need to
643 * re-aquire it
644 */
645 spin_unlock_irqrestore(&iommu->lock, flags);
646
647 /* Handle PPR entry */
648 iommu_handle_ppr_entry(iommu, entry);
649
650 spin_lock_irqsave(&iommu->lock, flags);
651
652 /* Refresh ring-buffer information */
653 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100654 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
655 }
656
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100657 spin_unlock_irqrestore(&iommu->lock, flags);
658}
659
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200660irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200661{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200662 struct amd_iommu *iommu;
663
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100664 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200665 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100666 iommu_poll_ppr_log(iommu);
667 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200668
669 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200670}
671
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200672irqreturn_t amd_iommu_int_handler(int irq, void *data)
673{
674 return IRQ_WAKE_THREAD;
675}
676
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200677/****************************************************************************
678 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200679 * IOMMU command queuing functions
680 *
681 ****************************************************************************/
682
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200683static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200684{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200685 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200686
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200687 while (*sem == 0 && i < LOOP_TIMEOUT) {
688 udelay(1);
689 i += 1;
690 }
691
692 if (i == LOOP_TIMEOUT) {
693 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
694 return -EIO;
695 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200696
697 return 0;
698}
699
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200700static void copy_cmd_to_buffer(struct amd_iommu *iommu,
701 struct iommu_cmd *cmd,
702 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200703{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200704 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200705
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200706 target = iommu->cmd_buf + tail;
707 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200708
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200709 /* Copy command to buffer */
710 memcpy(target, cmd, sizeof(*cmd));
711
712 /* Tell the IOMMU about it */
713 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
714}
715
Joerg Roedel815b33f2011-04-06 17:26:49 +0200716static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200717{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200718 WARN_ON(address & 0x7ULL);
719
Joerg Roedelded46732011-04-06 10:53:48 +0200720 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200721 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
722 cmd->data[1] = upper_32_bits(__pa(address));
723 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200724 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
725}
726
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200727static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
728{
729 memset(cmd, 0, sizeof(*cmd));
730 cmd->data[0] = devid;
731 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
732}
733
Joerg Roedel11b64022011-04-06 11:49:28 +0200734static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
735 size_t size, u16 domid, int pde)
736{
737 u64 pages;
738 int s;
739
740 pages = iommu_num_pages(address, size, PAGE_SIZE);
741 s = 0;
742
743 if (pages > 1) {
744 /*
745 * If we have to flush more than one page, flush all
746 * TLB entries for this domain
747 */
748 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
749 s = 1;
750 }
751
752 address &= PAGE_MASK;
753
754 memset(cmd, 0, sizeof(*cmd));
755 cmd->data[1] |= domid;
756 cmd->data[2] = lower_32_bits(address);
757 cmd->data[3] = upper_32_bits(address);
758 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
759 if (s) /* size bit - we flush more than one 4kb page */
760 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
761 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
762 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
763}
764
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200765static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
766 u64 address, size_t size)
767{
768 u64 pages;
769 int s;
770
771 pages = iommu_num_pages(address, size, PAGE_SIZE);
772 s = 0;
773
774 if (pages > 1) {
775 /*
776 * If we have to flush more than one page, flush all
777 * TLB entries for this domain
778 */
779 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
780 s = 1;
781 }
782
783 address &= PAGE_MASK;
784
785 memset(cmd, 0, sizeof(*cmd));
786 cmd->data[0] = devid;
787 cmd->data[0] |= (qdep & 0xff) << 24;
788 cmd->data[1] = devid;
789 cmd->data[2] = lower_32_bits(address);
790 cmd->data[3] = upper_32_bits(address);
791 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
792 if (s)
793 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
794}
795
Joerg Roedel22e266c2011-11-21 15:59:08 +0100796static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
797 u64 address, bool size)
798{
799 memset(cmd, 0, sizeof(*cmd));
800
801 address &= ~(0xfffULL);
802
803 cmd->data[0] = pasid & PASID_MASK;
804 cmd->data[1] = domid;
805 cmd->data[2] = lower_32_bits(address);
806 cmd->data[3] = upper_32_bits(address);
807 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
808 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
809 if (size)
810 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
811 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
812}
813
814static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
815 int qdep, u64 address, bool size)
816{
817 memset(cmd, 0, sizeof(*cmd));
818
819 address &= ~(0xfffULL);
820
821 cmd->data[0] = devid;
822 cmd->data[0] |= (pasid & 0xff) << 16;
823 cmd->data[0] |= (qdep & 0xff) << 24;
824 cmd->data[1] = devid;
825 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
826 cmd->data[2] = lower_32_bits(address);
827 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
828 cmd->data[3] = upper_32_bits(address);
829 if (size)
830 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
831 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
832}
833
Joerg Roedelc99afa22011-11-21 18:19:25 +0100834static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
835 int status, int tag, bool gn)
836{
837 memset(cmd, 0, sizeof(*cmd));
838
839 cmd->data[0] = devid;
840 if (gn) {
841 cmd->data[1] = pasid & PASID_MASK;
842 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
843 }
844 cmd->data[3] = tag & 0x1ff;
845 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
846
847 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
848}
849
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200850static void build_inv_all(struct iommu_cmd *cmd)
851{
852 memset(cmd, 0, sizeof(*cmd));
853 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200854}
855
Joerg Roedel431b2a22008-07-11 17:14:22 +0200856/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200857 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200858 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200859 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200860static int iommu_queue_command_sync(struct amd_iommu *iommu,
861 struct iommu_cmd *cmd,
862 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200863{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200864 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200865 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200866
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200867 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100868
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200869again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200870 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200871
872 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
873 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
874 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
875 left = (head - next_tail) % iommu->cmd_buf_size;
876
877 if (left <= 2) {
878 struct iommu_cmd sync_cmd;
879 volatile u64 sem = 0;
880 int ret;
881
882 build_completion_wait(&sync_cmd, (u64)&sem);
883 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
884
885 spin_unlock_irqrestore(&iommu->lock, flags);
886
887 if ((ret = wait_on_sem(&sem)) != 0)
888 return ret;
889
890 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200891 }
892
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200893 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200894
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200895 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200896 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200897
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200898 spin_unlock_irqrestore(&iommu->lock, flags);
899
Joerg Roedel815b33f2011-04-06 17:26:49 +0200900 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100901}
902
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200903static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
904{
905 return iommu_queue_command_sync(iommu, cmd, true);
906}
907
Joerg Roedel8d201962008-12-02 20:34:41 +0100908/*
909 * This function queues a completion wait command into the command
910 * buffer of an IOMMU
911 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100912static int iommu_completion_wait(struct amd_iommu *iommu)
913{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200914 struct iommu_cmd cmd;
915 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200916 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100917
918 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200919 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100920
Joerg Roedel815b33f2011-04-06 17:26:49 +0200921 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100922
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200923 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100924 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200925 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100926
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200927 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200928}
929
Joerg Roedeld8c13082011-04-06 18:51:26 +0200930static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200931{
932 struct iommu_cmd cmd;
933
Joerg Roedeld8c13082011-04-06 18:51:26 +0200934 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200935
Joerg Roedeld8c13082011-04-06 18:51:26 +0200936 return iommu_queue_command(iommu, &cmd);
937}
938
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200939static void iommu_flush_dte_all(struct amd_iommu *iommu)
940{
941 u32 devid;
942
943 for (devid = 0; devid <= 0xffff; ++devid)
944 iommu_flush_dte(iommu, devid);
945
946 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200947}
948
949/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200950 * This function uses heavy locking and may disable irqs for some time. But
951 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200952 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200953static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200954{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200955 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200956
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200957 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
958 struct iommu_cmd cmd;
959 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
960 dom_id, 1);
961 iommu_queue_command(iommu, &cmd);
962 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200963
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200964 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200965}
966
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200967static void iommu_flush_all(struct amd_iommu *iommu)
968{
969 struct iommu_cmd cmd;
970
971 build_inv_all(&cmd);
972
973 iommu_queue_command(iommu, &cmd);
974 iommu_completion_wait(iommu);
975}
976
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200977void iommu_flush_all_caches(struct amd_iommu *iommu)
978{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200979 if (iommu_feature(iommu, FEATURE_IA)) {
980 iommu_flush_all(iommu);
981 } else {
982 iommu_flush_dte_all(iommu);
983 iommu_flush_tlb_all(iommu);
984 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200985}
986
Joerg Roedel431b2a22008-07-11 17:14:22 +0200987/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200988 * Command send function for flushing on-device TLB
989 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200990static int device_flush_iotlb(struct iommu_dev_data *dev_data,
991 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200992{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200993 struct amd_iommu *iommu;
994 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200995 int qdep;
996
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200997 qdep = dev_data->ats.qdep;
998 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200999
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001000 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001001
1002 return iommu_queue_command(iommu, &cmd);
1003}
1004
1005/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001006 * Command send function for invalidating a device table entry
1007 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001008static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001009{
1010 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001011 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001012
Joerg Roedel6c542042011-06-09 17:07:31 +02001013 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001014
Joerg Roedelf62dda62011-06-09 12:55:35 +02001015 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001016 if (ret)
1017 return ret;
1018
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001019 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001020 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001021
1022 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001023}
1024
Joerg Roedel431b2a22008-07-11 17:14:22 +02001025/*
1026 * TLB invalidation function which is called from the mapping functions.
1027 * It invalidates a single PTE if the range to flush is within a single
1028 * page. Otherwise it flushes the whole TLB of the IOMMU.
1029 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001030static void __domain_flush_pages(struct protection_domain *domain,
1031 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001032{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001033 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001034 struct iommu_cmd cmd;
1035 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001036
Joerg Roedel11b64022011-04-06 11:49:28 +02001037 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001038
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001039 for (i = 0; i < amd_iommus_present; ++i) {
1040 if (!domain->dev_iommu[i])
1041 continue;
1042
1043 /*
1044 * Devices of this domain are behind this IOMMU
1045 * We need a TLB flush
1046 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001047 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001048 }
1049
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001050 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001051
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001052 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001053 continue;
1054
Joerg Roedel6c542042011-06-09 17:07:31 +02001055 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001056 }
1057
Joerg Roedel11b64022011-04-06 11:49:28 +02001058 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001059}
1060
Joerg Roedel17b124b2011-04-06 18:01:35 +02001061static void domain_flush_pages(struct protection_domain *domain,
1062 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001063{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001064 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001065}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001066
Joerg Roedel1c655772008-09-04 18:40:05 +02001067/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001068static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001069{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001070 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001071}
1072
Chris Wright42a49f92009-06-15 15:42:00 +02001073/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001074static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001075{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001076 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1077}
1078
1079static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001080{
1081 int i;
1082
1083 for (i = 0; i < amd_iommus_present; ++i) {
1084 if (!domain->dev_iommu[i])
1085 continue;
1086
1087 /*
1088 * Devices of this domain are behind this IOMMU
1089 * We need to wait for completion of all commands.
1090 */
1091 iommu_completion_wait(amd_iommus[i]);
1092 }
1093}
1094
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001095
Joerg Roedel43f49602008-12-02 21:01:12 +01001096/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001097 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001098 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001099static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001100{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001101 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001102
1103 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001104 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001105}
1106
Joerg Roedel431b2a22008-07-11 17:14:22 +02001107/****************************************************************************
1108 *
1109 * The functions below are used the create the page table mappings for
1110 * unity mapped regions.
1111 *
1112 ****************************************************************************/
1113
1114/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001115 * This function is used to add another level to an IO page table. Adding
1116 * another level increases the size of the address space by 9 bits to a size up
1117 * to 64 bits.
1118 */
1119static bool increase_address_space(struct protection_domain *domain,
1120 gfp_t gfp)
1121{
1122 u64 *pte;
1123
1124 if (domain->mode == PAGE_MODE_6_LEVEL)
1125 /* address space already 64 bit large */
1126 return false;
1127
1128 pte = (void *)get_zeroed_page(gfp);
1129 if (!pte)
1130 return false;
1131
1132 *pte = PM_LEVEL_PDE(domain->mode,
1133 virt_to_phys(domain->pt_root));
1134 domain->pt_root = pte;
1135 domain->mode += 1;
1136 domain->updated = true;
1137
1138 return true;
1139}
1140
1141static u64 *alloc_pte(struct protection_domain *domain,
1142 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001143 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001144 u64 **pte_page,
1145 gfp_t gfp)
1146{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001147 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001148 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001149
1150 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001151
1152 while (address > PM_LEVEL_SIZE(domain->mode))
1153 increase_address_space(domain, gfp);
1154
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001155 level = domain->mode - 1;
1156 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1157 address = PAGE_SIZE_ALIGN(address, page_size);
1158 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001159
1160 while (level > end_lvl) {
1161 if (!IOMMU_PTE_PRESENT(*pte)) {
1162 page = (u64 *)get_zeroed_page(gfp);
1163 if (!page)
1164 return NULL;
1165 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1166 }
1167
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001168 /* No level skipping support yet */
1169 if (PM_PTE_LEVEL(*pte) != level)
1170 return NULL;
1171
Joerg Roedel308973d2009-11-24 17:43:32 +01001172 level -= 1;
1173
1174 pte = IOMMU_PTE_PAGE(*pte);
1175
1176 if (pte_page && level == end_lvl)
1177 *pte_page = pte;
1178
1179 pte = &pte[PM_LEVEL_INDEX(level, address)];
1180 }
1181
1182 return pte;
1183}
1184
1185/*
1186 * This function checks if there is a PTE for a given dma address. If
1187 * there is one, it returns the pointer to it.
1188 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001189static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001190{
1191 int level;
1192 u64 *pte;
1193
Joerg Roedel24cd7722010-01-19 17:27:39 +01001194 if (address > PM_LEVEL_SIZE(domain->mode))
1195 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001196
Joerg Roedel24cd7722010-01-19 17:27:39 +01001197 level = domain->mode - 1;
1198 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1199
1200 while (level > 0) {
1201
1202 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001203 if (!IOMMU_PTE_PRESENT(*pte))
1204 return NULL;
1205
Joerg Roedel24cd7722010-01-19 17:27:39 +01001206 /* Large PTE */
1207 if (PM_PTE_LEVEL(*pte) == 0x07) {
1208 unsigned long pte_mask, __pte;
1209
1210 /*
1211 * If we have a series of large PTEs, make
1212 * sure to return a pointer to the first one.
1213 */
1214 pte_mask = PTE_PAGE_SIZE(*pte);
1215 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1216 __pte = ((unsigned long)pte) & pte_mask;
1217
1218 return (u64 *)__pte;
1219 }
1220
1221 /* No level skipping support yet */
1222 if (PM_PTE_LEVEL(*pte) != level)
1223 return NULL;
1224
Joerg Roedel308973d2009-11-24 17:43:32 +01001225 level -= 1;
1226
Joerg Roedel24cd7722010-01-19 17:27:39 +01001227 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001228 pte = IOMMU_PTE_PAGE(*pte);
1229 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001230 }
1231
1232 return pte;
1233}
1234
1235/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001236 * Generic mapping functions. It maps a physical address into a DMA
1237 * address space. It allocates the page table pages if necessary.
1238 * In the future it can be extended to a generic mapping function
1239 * supporting all features of AMD IOMMU page tables like level skipping
1240 * and full 64 bit address spaces.
1241 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001242static int iommu_map_page(struct protection_domain *dom,
1243 unsigned long bus_addr,
1244 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001245 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001246 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001247{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001248 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001249 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001250
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001251 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001252 return -EINVAL;
1253
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001254 bus_addr = PAGE_ALIGN(bus_addr);
1255 phys_addr = PAGE_ALIGN(phys_addr);
1256 count = PAGE_SIZE_PTE_COUNT(page_size);
1257 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001258
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001259 for (i = 0; i < count; ++i)
1260 if (IOMMU_PTE_PRESENT(pte[i]))
1261 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001262
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001263 if (page_size > PAGE_SIZE) {
1264 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1265 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1266 } else
1267 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1268
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001269 if (prot & IOMMU_PROT_IR)
1270 __pte |= IOMMU_PTE_IR;
1271 if (prot & IOMMU_PROT_IW)
1272 __pte |= IOMMU_PTE_IW;
1273
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001274 for (i = 0; i < count; ++i)
1275 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001276
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001277 update_domain(dom);
1278
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001279 return 0;
1280}
1281
Joerg Roedel24cd7722010-01-19 17:27:39 +01001282static unsigned long iommu_unmap_page(struct protection_domain *dom,
1283 unsigned long bus_addr,
1284 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001285{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001286 unsigned long long unmap_size, unmapped;
1287 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001288
Joerg Roedel24cd7722010-01-19 17:27:39 +01001289 BUG_ON(!is_power_of_2(page_size));
1290
1291 unmapped = 0;
1292
1293 while (unmapped < page_size) {
1294
1295 pte = fetch_pte(dom, bus_addr);
1296
1297 if (!pte) {
1298 /*
1299 * No PTE for this address
1300 * move forward in 4kb steps
1301 */
1302 unmap_size = PAGE_SIZE;
1303 } else if (PM_PTE_LEVEL(*pte) == 0) {
1304 /* 4kb PTE found for this address */
1305 unmap_size = PAGE_SIZE;
1306 *pte = 0ULL;
1307 } else {
1308 int count, i;
1309
1310 /* Large PTE found which maps this address */
1311 unmap_size = PTE_PAGE_SIZE(*pte);
Alex Williamsonca9c5a92013-06-21 14:33:19 -06001312
1313 /* Only unmap from the first pte in the page */
1314 if ((unmap_size - 1) & bus_addr)
1315 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001316 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1317 for (i = 0; i < count; i++)
1318 pte[i] = 0ULL;
1319 }
1320
1321 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1322 unmapped += unmap_size;
1323 }
1324
Alex Williamsonca9c5a92013-06-21 14:33:19 -06001325 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001326
1327 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001328}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001329
Joerg Roedel431b2a22008-07-11 17:14:22 +02001330/*
1331 * This function checks if a specific unity mapping entry is needed for
1332 * this specific IOMMU.
1333 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001334static int iommu_for_unity_map(struct amd_iommu *iommu,
1335 struct unity_map_entry *entry)
1336{
1337 u16 bdf, i;
1338
1339 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1340 bdf = amd_iommu_alias_table[i];
1341 if (amd_iommu_rlookup_table[bdf] == iommu)
1342 return 1;
1343 }
1344
1345 return 0;
1346}
1347
Joerg Roedel431b2a22008-07-11 17:14:22 +02001348/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001349 * This function actually applies the mapping to the page table of the
1350 * dma_ops domain.
1351 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001352static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1353 struct unity_map_entry *e)
1354{
1355 u64 addr;
1356 int ret;
1357
1358 for (addr = e->address_start; addr < e->address_end;
1359 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001360 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001361 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001362 if (ret)
1363 return ret;
1364 /*
1365 * if unity mapping is in aperture range mark the page
1366 * as allocated in the aperture
1367 */
1368 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001369 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001370 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001371 }
1372
1373 return 0;
1374}
1375
Joerg Roedel431b2a22008-07-11 17:14:22 +02001376/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001377 * Init the unity mappings for a specific IOMMU in the system
1378 *
1379 * Basically iterates over all unity mapping entries and applies them to
1380 * the default domain DMA of that IOMMU if necessary.
1381 */
1382static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1383{
1384 struct unity_map_entry *entry;
1385 int ret;
1386
1387 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1388 if (!iommu_for_unity_map(iommu, entry))
1389 continue;
1390 ret = dma_ops_unity_map(iommu->default_dom, entry);
1391 if (ret)
1392 return ret;
1393 }
1394
1395 return 0;
1396}
1397
1398/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001399 * Inits the unity mappings required for a specific device
1400 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001401static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1402 u16 devid)
1403{
1404 struct unity_map_entry *e;
1405 int ret;
1406
1407 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1408 if (!(devid >= e->devid_start && devid <= e->devid_end))
1409 continue;
1410 ret = dma_ops_unity_map(dma_dom, e);
1411 if (ret)
1412 return ret;
1413 }
1414
1415 return 0;
1416}
1417
Joerg Roedel431b2a22008-07-11 17:14:22 +02001418/****************************************************************************
1419 *
1420 * The next functions belong to the address allocator for the dma_ops
1421 * interface functions. They work like the allocators in the other IOMMU
1422 * drivers. Its basically a bitmap which marks the allocated pages in
1423 * the aperture. Maybe it could be enhanced in the future to a more
1424 * efficient allocator.
1425 *
1426 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001427
Joerg Roedel431b2a22008-07-11 17:14:22 +02001428/*
Joerg Roedel384de722009-05-15 12:30:05 +02001429 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001430 *
1431 * called with domain->lock held
1432 */
Joerg Roedel384de722009-05-15 12:30:05 +02001433
Joerg Roedel9cabe892009-05-18 16:38:55 +02001434/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001435 * Used to reserve address ranges in the aperture (e.g. for exclusion
1436 * ranges.
1437 */
1438static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1439 unsigned long start_page,
1440 unsigned int pages)
1441{
1442 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1443
1444 if (start_page + pages > last_page)
1445 pages = last_page - start_page;
1446
1447 for (i = start_page; i < start_page + pages; ++i) {
1448 int index = i / APERTURE_RANGE_PAGES;
1449 int page = i % APERTURE_RANGE_PAGES;
1450 __set_bit(page, dom->aperture[index]->bitmap);
1451 }
1452}
1453
1454/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001455 * This function is used to add a new aperture range to an existing
1456 * aperture in case of dma_ops domain allocation or address allocation
1457 * failure.
1458 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001459static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001460 bool populate, gfp_t gfp)
1461{
1462 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001463 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001464 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001465
Joerg Roedelf5e97052009-05-22 12:31:53 +02001466#ifdef CONFIG_IOMMU_STRESS
1467 populate = false;
1468#endif
1469
Joerg Roedel9cabe892009-05-18 16:38:55 +02001470 if (index >= APERTURE_MAX_RANGES)
1471 return -ENOMEM;
1472
1473 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1474 if (!dma_dom->aperture[index])
1475 return -ENOMEM;
1476
1477 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1478 if (!dma_dom->aperture[index]->bitmap)
1479 goto out_free;
1480
1481 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1482
1483 if (populate) {
1484 unsigned long address = dma_dom->aperture_size;
1485 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1486 u64 *pte, *pte_page;
1487
1488 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001489 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001490 &pte_page, gfp);
1491 if (!pte)
1492 goto out_free;
1493
1494 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1495
1496 address += APERTURE_RANGE_SIZE / 64;
1497 }
1498 }
1499
Joerg Roedel17f5b562011-07-06 17:14:44 +02001500 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001501 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1502
Joerg Roedel17f5b562011-07-06 17:14:44 +02001503 /* Reserve address range used for MSI messages */
1504 if (old_size < MSI_ADDR_BASE_LO &&
1505 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1506 unsigned long spage;
1507 int pages;
1508
1509 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1510 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1511
1512 dma_ops_reserve_addresses(dma_dom, spage, pages);
1513 }
1514
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001515 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001516 for_each_iommu(iommu) {
1517 if (iommu->exclusion_start &&
1518 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1519 && iommu->exclusion_start < dma_dom->aperture_size) {
1520 unsigned long startpage;
1521 int pages = iommu_num_pages(iommu->exclusion_start,
1522 iommu->exclusion_length,
1523 PAGE_SIZE);
1524 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1525 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1526 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001527 }
1528
1529 /*
1530 * Check for areas already mapped as present in the new aperture
1531 * range and mark those pages as reserved in the allocator. Such
1532 * mappings may already exist as a result of requested unity
1533 * mappings for devices.
1534 */
1535 for (i = dma_dom->aperture[index]->offset;
1536 i < dma_dom->aperture_size;
1537 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001538 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001539 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1540 continue;
1541
Joerg Roedelfcd08612011-10-11 17:41:32 +02001542 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001543 }
1544
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001545 update_domain(&dma_dom->domain);
1546
Joerg Roedel9cabe892009-05-18 16:38:55 +02001547 return 0;
1548
1549out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001550 update_domain(&dma_dom->domain);
1551
Joerg Roedel9cabe892009-05-18 16:38:55 +02001552 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1553
1554 kfree(dma_dom->aperture[index]);
1555 dma_dom->aperture[index] = NULL;
1556
1557 return -ENOMEM;
1558}
1559
Joerg Roedel384de722009-05-15 12:30:05 +02001560static unsigned long dma_ops_area_alloc(struct device *dev,
1561 struct dma_ops_domain *dom,
1562 unsigned int pages,
1563 unsigned long align_mask,
1564 u64 dma_mask,
1565 unsigned long start)
1566{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001567 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001568 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1569 int i = start >> APERTURE_RANGE_SHIFT;
1570 unsigned long boundary_size;
1571 unsigned long address = -1;
1572 unsigned long limit;
1573
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001574 next_bit >>= PAGE_SHIFT;
1575
Joerg Roedel384de722009-05-15 12:30:05 +02001576 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1577 PAGE_SIZE) >> PAGE_SHIFT;
1578
1579 for (;i < max_index; ++i) {
1580 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1581
1582 if (dom->aperture[i]->offset >= dma_mask)
1583 break;
1584
1585 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1586 dma_mask >> PAGE_SHIFT);
1587
1588 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1589 limit, next_bit, pages, 0,
1590 boundary_size, align_mask);
1591 if (address != -1) {
1592 address = dom->aperture[i]->offset +
1593 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001594 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001595 break;
1596 }
1597
1598 next_bit = 0;
1599 }
1600
1601 return address;
1602}
1603
Joerg Roedeld3086442008-06-26 21:27:57 +02001604static unsigned long dma_ops_alloc_addresses(struct device *dev,
1605 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001606 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001607 unsigned long align_mask,
1608 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001609{
Joerg Roedeld3086442008-06-26 21:27:57 +02001610 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001611
Joerg Roedelfe16f082009-05-22 12:27:53 +02001612#ifdef CONFIG_IOMMU_STRESS
1613 dom->next_address = 0;
1614 dom->need_flush = true;
1615#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001616
Joerg Roedel384de722009-05-15 12:30:05 +02001617 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001618 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001619
Joerg Roedel1c655772008-09-04 18:40:05 +02001620 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001621 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001622 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1623 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001624 dom->need_flush = true;
1625 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001626
Joerg Roedel384de722009-05-15 12:30:05 +02001627 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001628 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001629
1630 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1631
1632 return address;
1633}
1634
Joerg Roedel431b2a22008-07-11 17:14:22 +02001635/*
1636 * The address free function.
1637 *
1638 * called with domain->lock held
1639 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001640static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1641 unsigned long address,
1642 unsigned int pages)
1643{
Joerg Roedel384de722009-05-15 12:30:05 +02001644 unsigned i = address >> APERTURE_RANGE_SHIFT;
1645 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001646
Joerg Roedel384de722009-05-15 12:30:05 +02001647 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1648
Joerg Roedel47bccd62009-05-22 12:40:54 +02001649#ifdef CONFIG_IOMMU_STRESS
1650 if (i < 4)
1651 return;
1652#endif
1653
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001654 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001655 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001656
1657 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001658
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001659 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001660
Joerg Roedeld3086442008-06-26 21:27:57 +02001661}
1662
Joerg Roedel431b2a22008-07-11 17:14:22 +02001663/****************************************************************************
1664 *
1665 * The next functions belong to the domain allocation. A domain is
1666 * allocated for every IOMMU as the default domain. If device isolation
1667 * is enabled, every device get its own domain. The most important thing
1668 * about domains is the page table mapping the DMA address space they
1669 * contain.
1670 *
1671 ****************************************************************************/
1672
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001673/*
1674 * This function adds a protection domain to the global protection domain list
1675 */
1676static void add_domain_to_list(struct protection_domain *domain)
1677{
1678 unsigned long flags;
1679
1680 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1681 list_add(&domain->list, &amd_iommu_pd_list);
1682 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1683}
1684
1685/*
1686 * This function removes a protection domain to the global
1687 * protection domain list
1688 */
1689static void del_domain_from_list(struct protection_domain *domain)
1690{
1691 unsigned long flags;
1692
1693 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1694 list_del(&domain->list);
1695 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1696}
1697
Joerg Roedelec487d12008-06-26 21:27:58 +02001698static u16 domain_id_alloc(void)
1699{
1700 unsigned long flags;
1701 int id;
1702
1703 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1704 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1705 BUG_ON(id == 0);
1706 if (id > 0 && id < MAX_DOMAIN_ID)
1707 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1708 else
1709 id = 0;
1710 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1711
1712 return id;
1713}
1714
Joerg Roedela2acfb72008-12-02 18:28:53 +01001715static void domain_id_free(int id)
1716{
1717 unsigned long flags;
1718
1719 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1720 if (id > 0 && id < MAX_DOMAIN_ID)
1721 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1722 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1723}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001724
Joerg Roedel86db2e52008-12-02 18:20:21 +01001725static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001726{
1727 int i, j;
1728 u64 *p1, *p2, *p3;
1729
Joerg Roedel86db2e52008-12-02 18:20:21 +01001730 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001731
1732 if (!p1)
1733 return;
1734
1735 for (i = 0; i < 512; ++i) {
1736 if (!IOMMU_PTE_PRESENT(p1[i]))
1737 continue;
1738
1739 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001740 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001741 if (!IOMMU_PTE_PRESENT(p2[j]))
1742 continue;
1743 p3 = IOMMU_PTE_PAGE(p2[j]);
1744 free_page((unsigned long)p3);
1745 }
1746
1747 free_page((unsigned long)p2);
1748 }
1749
1750 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001751
1752 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001753}
1754
Joerg Roedelb16137b2011-11-21 16:50:23 +01001755static void free_gcr3_tbl_level1(u64 *tbl)
1756{
1757 u64 *ptr;
1758 int i;
1759
1760 for (i = 0; i < 512; ++i) {
1761 if (!(tbl[i] & GCR3_VALID))
1762 continue;
1763
1764 ptr = __va(tbl[i] & PAGE_MASK);
1765
1766 free_page((unsigned long)ptr);
1767 }
1768}
1769
1770static void free_gcr3_tbl_level2(u64 *tbl)
1771{
1772 u64 *ptr;
1773 int i;
1774
1775 for (i = 0; i < 512; ++i) {
1776 if (!(tbl[i] & GCR3_VALID))
1777 continue;
1778
1779 ptr = __va(tbl[i] & PAGE_MASK);
1780
1781 free_gcr3_tbl_level1(ptr);
1782 }
1783}
1784
Joerg Roedel52815b72011-11-17 17:24:28 +01001785static void free_gcr3_table(struct protection_domain *domain)
1786{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001787 if (domain->glx == 2)
1788 free_gcr3_tbl_level2(domain->gcr3_tbl);
1789 else if (domain->glx == 1)
1790 free_gcr3_tbl_level1(domain->gcr3_tbl);
1791 else if (domain->glx != 0)
1792 BUG();
1793
Joerg Roedel52815b72011-11-17 17:24:28 +01001794 free_page((unsigned long)domain->gcr3_tbl);
1795}
1796
Joerg Roedel431b2a22008-07-11 17:14:22 +02001797/*
1798 * Free a domain, only used if something went wrong in the
1799 * allocation path and we need to free an already allocated page table
1800 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001801static void dma_ops_domain_free(struct dma_ops_domain *dom)
1802{
Joerg Roedel384de722009-05-15 12:30:05 +02001803 int i;
1804
Joerg Roedelec487d12008-06-26 21:27:58 +02001805 if (!dom)
1806 return;
1807
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001808 del_domain_from_list(&dom->domain);
1809
Joerg Roedel86db2e52008-12-02 18:20:21 +01001810 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001811
Joerg Roedel384de722009-05-15 12:30:05 +02001812 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1813 if (!dom->aperture[i])
1814 continue;
1815 free_page((unsigned long)dom->aperture[i]->bitmap);
1816 kfree(dom->aperture[i]);
1817 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001818
1819 kfree(dom);
1820}
1821
Joerg Roedel431b2a22008-07-11 17:14:22 +02001822/*
1823 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001824 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001825 * structures required for the dma_ops interface
1826 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001827static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001828{
1829 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001830
1831 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1832 if (!dma_dom)
1833 return NULL;
1834
1835 spin_lock_init(&dma_dom->domain.lock);
1836
1837 dma_dom->domain.id = domain_id_alloc();
1838 if (dma_dom->domain.id == 0)
1839 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001840 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001841 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001842 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001843 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001844 dma_dom->domain.priv = dma_dom;
1845 if (!dma_dom->domain.pt_root)
1846 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001847
Joerg Roedel1c655772008-09-04 18:40:05 +02001848 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001849 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001850
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001851 add_domain_to_list(&dma_dom->domain);
1852
Joerg Roedel576175c2009-11-23 19:08:46 +01001853 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001854 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001855
Joerg Roedel431b2a22008-07-11 17:14:22 +02001856 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001857 * mark the first page as allocated so we never return 0 as
1858 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001859 */
Joerg Roedel384de722009-05-15 12:30:05 +02001860 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001861 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001862
Joerg Roedelec487d12008-06-26 21:27:58 +02001863
1864 return dma_dom;
1865
1866free_dma_dom:
1867 dma_ops_domain_free(dma_dom);
1868
1869 return NULL;
1870}
1871
Joerg Roedel431b2a22008-07-11 17:14:22 +02001872/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001873 * little helper function to check whether a given protection domain is a
1874 * dma_ops domain
1875 */
1876static bool dma_ops_domain(struct protection_domain *domain)
1877{
1878 return domain->flags & PD_DMA_OPS_MASK;
1879}
1880
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001881static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001882{
Joerg Roedel132bd682011-11-17 14:18:46 +01001883 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001884 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001885
Joerg Roedel132bd682011-11-17 14:18:46 +01001886 if (domain->mode != PAGE_MODE_NONE)
1887 pte_root = virt_to_phys(domain->pt_root);
1888
Joerg Roedel38ddf412008-09-11 10:38:32 +02001889 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1890 << DEV_ENTRY_MODE_SHIFT;
1891 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001892
Joerg Roedelee6c2862011-11-09 12:06:03 +01001893 flags = amd_iommu_dev_table[devid].data[1];
1894
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001895 if (ats)
1896 flags |= DTE_FLAG_IOTLB;
1897
Joerg Roedel52815b72011-11-17 17:24:28 +01001898 if (domain->flags & PD_IOMMUV2_MASK) {
1899 u64 gcr3 = __pa(domain->gcr3_tbl);
1900 u64 glx = domain->glx;
1901 u64 tmp;
1902
1903 pte_root |= DTE_FLAG_GV;
1904 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1905
1906 /* First mask out possible old values for GCR3 table */
1907 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1908 flags &= ~tmp;
1909
1910 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1911 flags &= ~tmp;
1912
1913 /* Encode GCR3 table into DTE */
1914 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1915 pte_root |= tmp;
1916
1917 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1918 flags |= tmp;
1919
1920 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1921 flags |= tmp;
1922 }
1923
Joerg Roedelee6c2862011-11-09 12:06:03 +01001924 flags &= ~(0xffffUL);
1925 flags |= domain->id;
1926
1927 amd_iommu_dev_table[devid].data[1] = flags;
1928 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001929}
1930
Joerg Roedel15898bb2009-11-24 15:39:42 +01001931static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001932{
Joerg Roedel355bf552008-12-08 12:02:41 +01001933 /* remove entry from the device table seen by the hardware */
1934 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1935 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01001936
Joerg Roedelc5cca142009-10-09 18:31:20 +02001937 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001938}
1939
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001940static void do_attach(struct iommu_dev_data *dev_data,
1941 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001942{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001943 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001944 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001945
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001946 iommu = amd_iommu_rlookup_table[dev_data->devid];
1947 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001948
1949 /* Update data structures */
1950 dev_data->domain = domain;
1951 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001952 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001953
1954 /* Do reference counting */
1955 domain->dev_iommu[iommu->index] += 1;
1956 domain->dev_cnt += 1;
1957
1958 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001959 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001960}
1961
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001962static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001963{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001964 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001965
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001966 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001967
Joerg Roedelc4596112009-11-20 14:57:32 +01001968 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001969 dev_data->domain->dev_iommu[iommu->index] -= 1;
1970 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001971
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001972 /* Update data structures */
1973 dev_data->domain = NULL;
1974 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001975 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001976
1977 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001978 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001979}
1980
1981/*
1982 * If a device is not yet associated with a domain, this function does
1983 * assigns it visible for the hardware
1984 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001985static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001986 struct protection_domain *domain)
1987{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001988 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001989
Joerg Roedel15898bb2009-11-24 15:39:42 +01001990 /* lock domain */
1991 spin_lock(&domain->lock);
1992
Joerg Roedel71f77582011-06-09 19:03:15 +02001993 if (dev_data->alias_data != NULL) {
1994 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001995
Joerg Roedel2b02b092011-06-09 17:48:39 +02001996 /* Some sanity checks */
1997 ret = -EBUSY;
1998 if (alias_data->domain != NULL &&
1999 alias_data->domain != domain)
2000 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002001
Joerg Roedel2b02b092011-06-09 17:48:39 +02002002 if (dev_data->domain != NULL &&
2003 dev_data->domain != domain)
2004 goto out_unlock;
2005
2006 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002007 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002008 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002009
2010 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002011 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002012
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002013 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002014 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002015
Joerg Roedel24100052009-11-25 15:59:57 +01002016 atomic_inc(&dev_data->bind);
2017
Julia Lawall84fe6c12010-05-27 12:31:51 +02002018 ret = 0;
2019
2020out_unlock:
2021
Joerg Roedel355bf552008-12-08 12:02:41 +01002022 /* ready */
2023 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002024
Julia Lawall84fe6c12010-05-27 12:31:51 +02002025 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002026}
2027
Joerg Roedel52815b72011-11-17 17:24:28 +01002028
2029static void pdev_iommuv2_disable(struct pci_dev *pdev)
2030{
2031 pci_disable_ats(pdev);
2032 pci_disable_pri(pdev);
2033 pci_disable_pasid(pdev);
2034}
2035
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002036/* FIXME: Change generic reset-function to do the same */
2037static int pri_reset_while_enabled(struct pci_dev *pdev)
2038{
2039 u16 control;
2040 int pos;
2041
Joerg Roedel46277b72011-12-07 14:34:02 +01002042 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002043 if (!pos)
2044 return -EINVAL;
2045
Joerg Roedel46277b72011-12-07 14:34:02 +01002046 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2047 control |= PCI_PRI_CTRL_RESET;
2048 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002049
2050 return 0;
2051}
2052
Joerg Roedel52815b72011-11-17 17:24:28 +01002053static int pdev_iommuv2_enable(struct pci_dev *pdev)
2054{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002055 bool reset_enable;
2056 int reqs, ret;
2057
2058 /* FIXME: Hardcode number of outstanding requests for now */
2059 reqs = 32;
2060 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2061 reqs = 1;
2062 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002063
2064 /* Only allow access to user-accessible pages */
2065 ret = pci_enable_pasid(pdev, 0);
2066 if (ret)
2067 goto out_err;
2068
2069 /* First reset the PRI state of the device */
2070 ret = pci_reset_pri(pdev);
2071 if (ret)
2072 goto out_err;
2073
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002074 /* Enable PRI */
2075 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002076 if (ret)
2077 goto out_err;
2078
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002079 if (reset_enable) {
2080 ret = pri_reset_while_enabled(pdev);
2081 if (ret)
2082 goto out_err;
2083 }
2084
Joerg Roedel52815b72011-11-17 17:24:28 +01002085 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2086 if (ret)
2087 goto out_err;
2088
2089 return 0;
2090
2091out_err:
2092 pci_disable_pri(pdev);
2093 pci_disable_pasid(pdev);
2094
2095 return ret;
2096}
2097
Joerg Roedelc99afa22011-11-21 18:19:25 +01002098/* FIXME: Move this to PCI code */
Joerg Roedelf9a07f32012-04-12 12:49:26 +02002099#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002100
2101bool pci_pri_tlp_required(struct pci_dev *pdev)
2102{
Joerg Roedelf9a07f32012-04-12 12:49:26 +02002103 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002104 int pos;
2105
Joerg Roedel46277b72011-12-07 14:34:02 +01002106 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002107 if (!pos)
2108 return false;
2109
Joerg Roedelf9a07f32012-04-12 12:49:26 +02002110 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002111
Joerg Roedelf9a07f32012-04-12 12:49:26 +02002112 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002113}
2114
Joerg Roedel15898bb2009-11-24 15:39:42 +01002115/*
2116 * If a device is not yet associated with a domain, this function does
2117 * assigns it visible for the hardware
2118 */
2119static int attach_device(struct device *dev,
2120 struct protection_domain *domain)
2121{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002122 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002123 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002124 unsigned long flags;
2125 int ret;
2126
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002127 dev_data = get_dev_data(dev);
2128
Joerg Roedel52815b72011-11-17 17:24:28 +01002129 if (domain->flags & PD_IOMMUV2_MASK) {
2130 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2131 return -EINVAL;
2132
2133 if (pdev_iommuv2_enable(pdev) != 0)
2134 return -EINVAL;
2135
2136 dev_data->ats.enabled = true;
2137 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002138 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002139 } else if (amd_iommu_iotlb_sup &&
2140 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002141 dev_data->ats.enabled = true;
2142 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2143 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002144
Joerg Roedel15898bb2009-11-24 15:39:42 +01002145 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002146 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002147 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2148
2149 /*
2150 * We might boot into a crash-kernel here. The crashed kernel
2151 * left the caches in the IOMMU dirty. So we have to flush
2152 * here to evict all dirty stuff.
2153 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002154 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002155
2156 return ret;
2157}
2158
2159/*
2160 * Removes a device from a protection domain (unlocked)
2161 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002162static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002163{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002164 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002165 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002166
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002167 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002168
Joerg Roedel2ca76272010-01-22 16:45:31 +01002169 domain = dev_data->domain;
2170
2171 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002172
Joerg Roedel71f77582011-06-09 19:03:15 +02002173 if (dev_data->alias_data != NULL) {
2174 struct iommu_dev_data *alias_data = dev_data->alias_data;
2175
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002176 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002177 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002178 }
2179
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002180 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002181 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002182
Joerg Roedel2ca76272010-01-22 16:45:31 +01002183 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002184
Joerg Roedel21129f72009-09-01 11:59:42 +02002185 /*
2186 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002187 * passthrough domain if it is detached from any other domain.
2188 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002189 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002190 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002191 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002192 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002193}
2194
2195/*
2196 * Removes a device from a protection domain (with devtable_lock held)
2197 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002198static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002199{
Joerg Roedel52815b72011-11-17 17:24:28 +01002200 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002201 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002202 unsigned long flags;
2203
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002204 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002205 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002206
Joerg Roedel355bf552008-12-08 12:02:41 +01002207 /* lock device table */
2208 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002209 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002210 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002211
Joerg Roedel52815b72011-11-17 17:24:28 +01002212 if (domain->flags & PD_IOMMUV2_MASK)
2213 pdev_iommuv2_disable(to_pci_dev(dev));
2214 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002215 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002216
2217 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002218}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002219
Joerg Roedel15898bb2009-11-24 15:39:42 +01002220/*
2221 * Find out the protection domain structure for a given PCI device. This
2222 * will give us the pointer to the page table root for example.
2223 */
2224static struct protection_domain *domain_for_device(struct device *dev)
2225{
Joerg Roedel71f77582011-06-09 19:03:15 +02002226 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002227 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002228 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002229
Joerg Roedel657cbb62009-11-23 15:26:46 +01002230 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002231
Joerg Roedel2b02b092011-06-09 17:48:39 +02002232 if (dev_data->domain)
2233 return dev_data->domain;
2234
Joerg Roedel71f77582011-06-09 19:03:15 +02002235 if (dev_data->alias_data != NULL) {
2236 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002237
2238 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2239 if (alias_data->domain != NULL) {
2240 __attach_device(dev_data, alias_data->domain);
2241 dom = alias_data->domain;
2242 }
2243 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002244 }
2245
Joerg Roedel15898bb2009-11-24 15:39:42 +01002246 return dom;
2247}
2248
Joerg Roedele275a2a2008-12-10 18:27:25 +01002249static int device_change_notifier(struct notifier_block *nb,
2250 unsigned long action, void *data)
2251{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002252 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002253 struct protection_domain *domain;
2254 struct iommu_dev_data *dev_data;
2255 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002256 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002257 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002258 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002259
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002260 if (!check_device(dev))
2261 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002262
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002263 devid = get_device_id(dev);
2264 iommu = amd_iommu_rlookup_table[devid];
2265 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002266
2267 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002268 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002269
2270 domain = domain_for_device(dev);
2271
Joerg Roedele275a2a2008-12-10 18:27:25 +01002272 if (!domain)
2273 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002274 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002275 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002276 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002277 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002278 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002279
2280 iommu_init_device(dev);
2281
Joerg Roedel06e619c2012-07-19 13:42:54 +02002282 /*
2283 * dev_data is still NULL and
2284 * got initialized in iommu_init_device
2285 */
2286 dev_data = get_dev_data(dev);
2287
2288 if (iommu_pass_through || dev_data->iommu_v2) {
2289 dev_data->passthrough = true;
2290 attach_device(dev, pt_domain);
2291 break;
2292 }
2293
Joerg Roedel657cbb62009-11-23 15:26:46 +01002294 domain = domain_for_device(dev);
2295
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002296 /* allocate a protection domain if a device is added */
2297 dma_domain = find_protection_domain(devid);
Joerg Roedel2c0260b2013-03-26 22:48:23 +01002298 if (!dma_domain) {
2299 dma_domain = dma_ops_domain_alloc();
2300 if (!dma_domain)
2301 goto out;
2302 dma_domain->target_dev = devid;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002303
Joerg Roedel2c0260b2013-03-26 22:48:23 +01002304 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2305 list_add_tail(&dma_domain->list, &iommu_pd_list);
2306 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2307 }
Joerg Roedelb9e619e2012-06-21 14:52:40 +02002308
Joerg Roedel06e619c2012-07-19 13:42:54 +02002309 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelb9e619e2012-06-21 14:52:40 +02002310
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002311 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002312 case BUS_NOTIFY_DEL_DEVICE:
2313
2314 iommu_uninit_device(dev);
2315
Joerg Roedele275a2a2008-12-10 18:27:25 +01002316 default:
2317 goto out;
2318 }
2319
Joerg Roedele275a2a2008-12-10 18:27:25 +01002320 iommu_completion_wait(iommu);
2321
2322out:
2323 return 0;
2324}
2325
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302326static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002327 .notifier_call = device_change_notifier,
2328};
Joerg Roedel355bf552008-12-08 12:02:41 +01002329
Joerg Roedel8638c492009-12-10 11:12:25 +01002330void amd_iommu_init_notifier(void)
2331{
2332 bus_register_notifier(&pci_bus_type, &device_nb);
2333}
2334
Joerg Roedel431b2a22008-07-11 17:14:22 +02002335/*****************************************************************************
2336 *
2337 * The next functions belong to the dma_ops mapping/unmapping code.
2338 *
2339 *****************************************************************************/
2340
2341/*
2342 * In the dma_ops path we only have the struct device. This function
2343 * finds the corresponding IOMMU, the protection domain and the
2344 * requestor id for a given device.
2345 * If the device is not yet associated with a domain this is also done
2346 * in this function.
2347 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002348static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002349{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002350 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002351 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002352 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002353
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002354 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002355 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002356
Joerg Roedel94f6d192009-11-24 16:40:02 +01002357 domain = domain_for_device(dev);
2358 if (domain != NULL && !dma_ops_domain(domain))
2359 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002360
Joerg Roedel94f6d192009-11-24 16:40:02 +01002361 if (domain != NULL)
2362 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002363
Joerg Roedel15898bb2009-11-24 15:39:42 +01002364 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002365 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002366 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002367 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2368 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002369 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002370 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002371
Joerg Roedel94f6d192009-11-24 16:40:02 +01002372 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002373}
2374
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002375static void update_device_table(struct protection_domain *domain)
2376{
Joerg Roedel492667d2009-11-27 13:25:47 +01002377 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002378
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002379 list_for_each_entry(dev_data, &domain->dev_list, list)
2380 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002381}
2382
2383static void update_domain(struct protection_domain *domain)
2384{
2385 if (!domain->updated)
2386 return;
2387
2388 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002389
2390 domain_flush_devices(domain);
2391 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002392
2393 domain->updated = false;
2394}
2395
Joerg Roedel431b2a22008-07-11 17:14:22 +02002396/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002397 * This function fetches the PTE for a given address in the aperture
2398 */
2399static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2400 unsigned long address)
2401{
Joerg Roedel384de722009-05-15 12:30:05 +02002402 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002403 u64 *pte, *pte_page;
2404
Joerg Roedel384de722009-05-15 12:30:05 +02002405 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2406 if (!aperture)
2407 return NULL;
2408
2409 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002410 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002411 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002412 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002413 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2414 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002415 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002416
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002417 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002418
2419 return pte;
2420}
2421
2422/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002423 * This is the generic map function. It maps one 4kb page at paddr to
2424 * the given address in the DMA address space for the domain.
2425 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002426static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002427 unsigned long address,
2428 phys_addr_t paddr,
2429 int direction)
2430{
2431 u64 *pte, __pte;
2432
2433 WARN_ON(address > dom->aperture_size);
2434
2435 paddr &= PAGE_MASK;
2436
Joerg Roedel8bda3092009-05-12 12:02:46 +02002437 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002438 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002439 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002440
2441 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2442
2443 if (direction == DMA_TO_DEVICE)
2444 __pte |= IOMMU_PTE_IR;
2445 else if (direction == DMA_FROM_DEVICE)
2446 __pte |= IOMMU_PTE_IW;
2447 else if (direction == DMA_BIDIRECTIONAL)
2448 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2449
2450 WARN_ON(*pte);
2451
2452 *pte = __pte;
2453
2454 return (dma_addr_t)address;
2455}
2456
Joerg Roedel431b2a22008-07-11 17:14:22 +02002457/*
2458 * The generic unmapping function for on page in the DMA address space.
2459 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002460static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002461 unsigned long address)
2462{
Joerg Roedel384de722009-05-15 12:30:05 +02002463 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002464 u64 *pte;
2465
2466 if (address >= dom->aperture_size)
2467 return;
2468
Joerg Roedel384de722009-05-15 12:30:05 +02002469 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2470 if (!aperture)
2471 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002472
Joerg Roedel384de722009-05-15 12:30:05 +02002473 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2474 if (!pte)
2475 return;
2476
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002477 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002478
2479 WARN_ON(!*pte);
2480
2481 *pte = 0ULL;
2482}
2483
Joerg Roedel431b2a22008-07-11 17:14:22 +02002484/*
2485 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002486 * contiguous memory region into DMA address space. It is used by all
2487 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002488 * Must be called with the domain lock held.
2489 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002490static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002491 struct dma_ops_domain *dma_dom,
2492 phys_addr_t paddr,
2493 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002494 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002495 bool align,
2496 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002497{
2498 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002499 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002500 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002501 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002502 int i;
2503
Joerg Roedele3c449f2008-10-15 22:02:11 -07002504 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002505 paddr &= PAGE_MASK;
2506
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002507 INC_STATS_COUNTER(total_map_requests);
2508
Joerg Roedelc1858972008-12-12 15:42:39 +01002509 if (pages > 1)
2510 INC_STATS_COUNTER(cross_page);
2511
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002512 if (align)
2513 align_mask = (1UL << get_order(size)) - 1;
2514
Joerg Roedel11b83882009-05-19 10:23:15 +02002515retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002516 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2517 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002518 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002519 /*
2520 * setting next_address here will let the address
2521 * allocator only scan the new allocated range in the
2522 * first run. This is a small optimization.
2523 */
2524 dma_dom->next_address = dma_dom->aperture_size;
2525
Joerg Roedel576175c2009-11-23 19:08:46 +01002526 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002527 goto out;
2528
2529 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002530 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002531 * allocation again
2532 */
2533 goto retry;
2534 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002535
2536 start = address;
2537 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002538 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002539 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002540 goto out_unmap;
2541
Joerg Roedelcb76c322008-06-26 21:28:00 +02002542 paddr += PAGE_SIZE;
2543 start += PAGE_SIZE;
2544 }
2545 address += offset;
2546
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002547 ADD_STATS_COUNTER(alloced_io_mem, size);
2548
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002549 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002550 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002551 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002552 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002553 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002554
Joerg Roedelcb76c322008-06-26 21:28:00 +02002555out:
2556 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002557
2558out_unmap:
2559
2560 for (--i; i >= 0; --i) {
2561 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002562 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002563 }
2564
2565 dma_ops_free_addresses(dma_dom, address, pages);
2566
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002567 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002568}
2569
Joerg Roedel431b2a22008-07-11 17:14:22 +02002570/*
2571 * Does the reverse of the __map_single function. Must be called with
2572 * the domain lock held too
2573 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002574static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002575 dma_addr_t dma_addr,
2576 size_t size,
2577 int dir)
2578{
Joerg Roedel04e04632010-09-23 16:12:48 +02002579 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002580 dma_addr_t i, start;
2581 unsigned int pages;
2582
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002583 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002584 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002585 return;
2586
Joerg Roedel04e04632010-09-23 16:12:48 +02002587 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002588 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002589 dma_addr &= PAGE_MASK;
2590 start = dma_addr;
2591
2592 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002593 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002594 start += PAGE_SIZE;
2595 }
2596
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002597 SUB_STATS_COUNTER(alloced_io_mem, size);
2598
Joerg Roedelcb76c322008-06-26 21:28:00 +02002599 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002600
Joerg Roedel80be3082008-11-06 14:59:05 +01002601 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002602 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002603 dma_dom->need_flush = false;
2604 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002605}
2606
Joerg Roedel431b2a22008-07-11 17:14:22 +02002607/*
2608 * The exported map_single function for dma_ops.
2609 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002610static dma_addr_t map_page(struct device *dev, struct page *page,
2611 unsigned long offset, size_t size,
2612 enum dma_data_direction dir,
2613 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002614{
2615 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002616 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002617 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002618 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002619 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002620
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002621 INC_STATS_COUNTER(cnt_map_single);
2622
Joerg Roedel94f6d192009-11-24 16:40:02 +01002623 domain = get_domain(dev);
2624 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002625 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002626 else if (IS_ERR(domain))
2627 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002628
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002629 dma_mask = *dev->dma_mask;
2630
Joerg Roedel4da70b92008-06-26 21:28:01 +02002631 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002632
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002633 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002634 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002635 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002636 goto out;
2637
Joerg Roedel17b124b2011-04-06 18:01:35 +02002638 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002639
2640out:
2641 spin_unlock_irqrestore(&domain->lock, flags);
2642
2643 return addr;
2644}
2645
Joerg Roedel431b2a22008-07-11 17:14:22 +02002646/*
2647 * The exported unmap_single function for dma_ops.
2648 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002649static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2650 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002651{
2652 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002653 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002654
Joerg Roedel146a6912008-12-12 15:07:12 +01002655 INC_STATS_COUNTER(cnt_unmap_single);
2656
Joerg Roedel94f6d192009-11-24 16:40:02 +01002657 domain = get_domain(dev);
2658 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002659 return;
2660
Joerg Roedel4da70b92008-06-26 21:28:01 +02002661 spin_lock_irqsave(&domain->lock, flags);
2662
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002663 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002664
Joerg Roedel17b124b2011-04-06 18:01:35 +02002665 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002666
2667 spin_unlock_irqrestore(&domain->lock, flags);
2668}
2669
Joerg Roedel431b2a22008-07-11 17:14:22 +02002670/*
2671 * This is a special map_sg function which is used if we should map a
2672 * device which is not handled by an AMD IOMMU in the system.
2673 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002674static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2675 int nelems, int dir)
2676{
2677 struct scatterlist *s;
2678 int i;
2679
2680 for_each_sg(sglist, s, nelems, i) {
2681 s->dma_address = (dma_addr_t)sg_phys(s);
2682 s->dma_length = s->length;
2683 }
2684
2685 return nelems;
2686}
2687
Joerg Roedel431b2a22008-07-11 17:14:22 +02002688/*
2689 * The exported map_sg function for dma_ops (handles scatter-gather
2690 * lists).
2691 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002692static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002693 int nelems, enum dma_data_direction dir,
2694 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002695{
2696 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002697 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002698 int i;
2699 struct scatterlist *s;
2700 phys_addr_t paddr;
2701 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002702 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002703
Joerg Roedeld03f0672008-12-12 15:09:48 +01002704 INC_STATS_COUNTER(cnt_map_sg);
2705
Joerg Roedel94f6d192009-11-24 16:40:02 +01002706 domain = get_domain(dev);
2707 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002708 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002709 else if (IS_ERR(domain))
2710 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002711
Joerg Roedel832a90c2008-09-18 15:54:23 +02002712 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002713
Joerg Roedel65b050a2008-06-26 21:28:02 +02002714 spin_lock_irqsave(&domain->lock, flags);
2715
2716 for_each_sg(sglist, s, nelems, i) {
2717 paddr = sg_phys(s);
2718
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002719 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002720 paddr, s->length, dir, false,
2721 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002722
2723 if (s->dma_address) {
2724 s->dma_length = s->length;
2725 mapped_elems++;
2726 } else
2727 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002728 }
2729
Joerg Roedel17b124b2011-04-06 18:01:35 +02002730 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002731
2732out:
2733 spin_unlock_irqrestore(&domain->lock, flags);
2734
2735 return mapped_elems;
2736unmap:
2737 for_each_sg(sglist, s, mapped_elems, i) {
2738 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002739 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002740 s->dma_length, dir);
2741 s->dma_address = s->dma_length = 0;
2742 }
2743
2744 mapped_elems = 0;
2745
2746 goto out;
2747}
2748
Joerg Roedel431b2a22008-07-11 17:14:22 +02002749/*
2750 * The exported map_sg function for dma_ops (handles scatter-gather
2751 * lists).
2752 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002753static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002754 int nelems, enum dma_data_direction dir,
2755 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002756{
2757 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002758 struct protection_domain *domain;
2759 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002760 int i;
2761
Joerg Roedel55877a62008-12-12 15:12:14 +01002762 INC_STATS_COUNTER(cnt_unmap_sg);
2763
Joerg Roedel94f6d192009-11-24 16:40:02 +01002764 domain = get_domain(dev);
2765 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002766 return;
2767
Joerg Roedel65b050a2008-06-26 21:28:02 +02002768 spin_lock_irqsave(&domain->lock, flags);
2769
2770 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002771 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002772 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002773 s->dma_address = s->dma_length = 0;
2774 }
2775
Joerg Roedel17b124b2011-04-06 18:01:35 +02002776 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002777
2778 spin_unlock_irqrestore(&domain->lock, flags);
2779}
2780
Joerg Roedel431b2a22008-07-11 17:14:22 +02002781/*
2782 * The exported alloc_coherent function for dma_ops.
2783 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002784static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002785 dma_addr_t *dma_addr, gfp_t flag,
2786 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002787{
2788 unsigned long flags;
2789 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002790 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002791 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002792 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002793
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002794 INC_STATS_COUNTER(cnt_alloc_coherent);
2795
Joerg Roedel94f6d192009-11-24 16:40:02 +01002796 domain = get_domain(dev);
2797 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002798 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2799 *dma_addr = __pa(virt_addr);
2800 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002801 } else if (IS_ERR(domain))
2802 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002803
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002804 dma_mask = dev->coherent_dma_mask;
2805 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2806 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002807
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002808 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2809 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302810 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002811
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002812 paddr = virt_to_phys(virt_addr);
2813
Joerg Roedel832a90c2008-09-18 15:54:23 +02002814 if (!dma_mask)
2815 dma_mask = *dev->dma_mask;
2816
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002817 spin_lock_irqsave(&domain->lock, flags);
2818
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002819 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002820 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002821
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002822 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002823 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002824 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002825 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002826
Joerg Roedel17b124b2011-04-06 18:01:35 +02002827 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002828
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002829 spin_unlock_irqrestore(&domain->lock, flags);
2830
2831 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002832
2833out_free:
2834
2835 free_pages((unsigned long)virt_addr, get_order(size));
2836
2837 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002838}
2839
Joerg Roedel431b2a22008-07-11 17:14:22 +02002840/*
2841 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002842 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002843static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002844 void *virt_addr, dma_addr_t dma_addr,
2845 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002846{
2847 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002848 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002849
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002850 INC_STATS_COUNTER(cnt_free_coherent);
2851
Joerg Roedel94f6d192009-11-24 16:40:02 +01002852 domain = get_domain(dev);
2853 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002854 goto free_mem;
2855
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002856 spin_lock_irqsave(&domain->lock, flags);
2857
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002858 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002859
Joerg Roedel17b124b2011-04-06 18:01:35 +02002860 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002861
2862 spin_unlock_irqrestore(&domain->lock, flags);
2863
2864free_mem:
2865 free_pages((unsigned long)virt_addr, get_order(size));
2866}
2867
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002868/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002869 * This function is called by the DMA layer to find out if we can handle a
2870 * particular device. It is part of the dma_ops.
2871 */
2872static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2873{
Joerg Roedel420aef82009-11-23 16:14:57 +01002874 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002875}
2876
2877/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002878 * The function for pre-allocating protection domains.
2879 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002880 * If the driver core informs the DMA layer if a driver grabs a device
2881 * we don't need to preallocate the protection domains anymore.
2882 * For now we have to.
2883 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01002884static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002885{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002886 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002887 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002888 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002889 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002890
Chris Wrightd18c69d2010-04-02 18:27:55 -07002891 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002892
2893 /* Do we handle this device? */
2894 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002895 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002896
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002897 dev_data = get_dev_data(&dev->dev);
2898 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2899 /* Make sure passthrough domain is allocated */
2900 alloc_passthrough_domain();
2901 dev_data->passthrough = true;
2902 attach_device(&dev->dev, pt_domain);
2903 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2904 dev_name(&dev->dev));
2905 }
2906
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002907 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002908 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002909 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002910
2911 devid = get_device_id(&dev->dev);
2912
Joerg Roedel87a64d52009-11-24 17:26:43 +01002913 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002914 if (!dma_dom)
2915 continue;
2916 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002917 dma_dom->target_dev = devid;
2918
Joerg Roedel15898bb2009-11-24 15:39:42 +01002919 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002920
Joerg Roedelbd60b732008-09-11 10:24:48 +02002921 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002922 }
2923}
2924
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002925static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002926 .alloc = alloc_coherent,
2927 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002928 .map_page = map_page,
2929 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002930 .map_sg = map_sg,
2931 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002932 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002933};
2934
Joerg Roedel27c21272011-05-30 15:56:24 +02002935static unsigned device_dma_ops_init(void)
2936{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002937 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02002938 struct pci_dev *pdev = NULL;
2939 unsigned unhandled = 0;
2940
2941 for_each_pci_dev(pdev) {
2942 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01002943
2944 iommu_ignore_device(&pdev->dev);
2945
Joerg Roedel27c21272011-05-30 15:56:24 +02002946 unhandled += 1;
2947 continue;
2948 }
2949
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002950 dev_data = get_dev_data(&pdev->dev);
2951
2952 if (!dev_data->passthrough)
2953 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2954 else
2955 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02002956 }
2957
2958 return unhandled;
2959}
2960
Joerg Roedel431b2a22008-07-11 17:14:22 +02002961/*
2962 * The function which clues the AMD IOMMU driver into dma_ops.
2963 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002964
2965void __init amd_iommu_init_api(void)
2966{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02002967 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002968}
2969
Joerg Roedel6631ee92008-06-26 21:28:05 +02002970int __init amd_iommu_init_dma_ops(void)
2971{
2972 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002973 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002974
Joerg Roedel431b2a22008-07-11 17:14:22 +02002975 /*
2976 * first allocate a default protection domain for every IOMMU we
2977 * found in the system. Devices not assigned to any other
2978 * protection domain will be assigned to the default one.
2979 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002980 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002981 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002982 if (iommu->default_dom == NULL)
2983 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002984 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002985 ret = iommu_init_unity_mappings(iommu);
2986 if (ret)
2987 goto free_domains;
2988 }
2989
Joerg Roedel431b2a22008-07-11 17:14:22 +02002990 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002991 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002992 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002993 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002994
2995 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002996 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002997
Joerg Roedel431b2a22008-07-11 17:14:22 +02002998 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002999 unhandled = device_dma_ops_init();
3000 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3001 /* There are unhandled devices - initialize swiotlb for them */
3002 swiotlb = 1;
3003 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003004
Joerg Roedel7f265082008-12-12 13:50:21 +01003005 amd_iommu_stats_init();
3006
Joerg Roedel6631ee92008-06-26 21:28:05 +02003007 return 0;
3008
3009free_domains:
3010
Joerg Roedel3bd22172009-05-04 15:06:20 +02003011 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003012 if (iommu->default_dom)
3013 dma_ops_domain_free(iommu->default_dom);
3014 }
3015
3016 return ret;
3017}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003018
3019/*****************************************************************************
3020 *
3021 * The following functions belong to the exported interface of AMD IOMMU
3022 *
3023 * This interface allows access to lower level functions of the IOMMU
3024 * like protection domain handling and assignement of devices to domains
3025 * which is not possible with the dma_ops interface.
3026 *
3027 *****************************************************************************/
3028
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003029static void cleanup_domain(struct protection_domain *domain)
3030{
Joerg Roedel492667d2009-11-27 13:25:47 +01003031 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003032 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003033
3034 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3035
Joerg Roedel492667d2009-11-27 13:25:47 +01003036 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003037 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003038 atomic_set(&dev_data->bind, 0);
3039 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003040
3041 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3042}
3043
Joerg Roedel26508152009-08-26 16:52:40 +02003044static void protection_domain_free(struct protection_domain *domain)
3045{
3046 if (!domain)
3047 return;
3048
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003049 del_domain_from_list(domain);
3050
Joerg Roedel26508152009-08-26 16:52:40 +02003051 if (domain->id)
3052 domain_id_free(domain->id);
3053
3054 kfree(domain);
3055}
3056
3057static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003058{
3059 struct protection_domain *domain;
3060
3061 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3062 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003063 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003064
3065 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003066 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003067 domain->id = domain_id_alloc();
3068 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003069 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003070 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003071
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003072 add_domain_to_list(domain);
3073
Joerg Roedel26508152009-08-26 16:52:40 +02003074 return domain;
3075
3076out_err:
3077 kfree(domain);
3078
3079 return NULL;
3080}
3081
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003082static int __init alloc_passthrough_domain(void)
3083{
3084 if (pt_domain != NULL)
3085 return 0;
3086
3087 /* allocate passthrough domain */
3088 pt_domain = protection_domain_alloc();
3089 if (!pt_domain)
3090 return -ENOMEM;
3091
3092 pt_domain->mode = PAGE_MODE_NONE;
3093
3094 return 0;
3095}
Joerg Roedel26508152009-08-26 16:52:40 +02003096static int amd_iommu_domain_init(struct iommu_domain *dom)
3097{
3098 struct protection_domain *domain;
3099
3100 domain = protection_domain_alloc();
3101 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003102 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003103
3104 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003105 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3106 if (!domain->pt_root)
3107 goto out_free;
3108
Joerg Roedelf3572db2011-11-23 12:36:25 +01003109 domain->iommu_domain = dom;
3110
Joerg Roedelc156e342008-12-02 18:13:27 +01003111 dom->priv = domain;
3112
3113 return 0;
3114
3115out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003116 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003117
3118 return -ENOMEM;
3119}
3120
Joerg Roedel98383fc2008-12-02 18:34:12 +01003121static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3122{
3123 struct protection_domain *domain = dom->priv;
3124
3125 if (!domain)
3126 return;
3127
3128 if (domain->dev_cnt > 0)
3129 cleanup_domain(domain);
3130
3131 BUG_ON(domain->dev_cnt != 0);
3132
Joerg Roedel132bd682011-11-17 14:18:46 +01003133 if (domain->mode != PAGE_MODE_NONE)
3134 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003135
Joerg Roedel52815b72011-11-17 17:24:28 +01003136 if (domain->flags & PD_IOMMUV2_MASK)
3137 free_gcr3_table(domain);
3138
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003139 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003140
3141 dom->priv = NULL;
3142}
3143
Joerg Roedel684f2882008-12-08 12:07:44 +01003144static void amd_iommu_detach_device(struct iommu_domain *dom,
3145 struct device *dev)
3146{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003147 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003148 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003149 u16 devid;
3150
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003151 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003152 return;
3153
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003154 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003155
Joerg Roedel657cbb62009-11-23 15:26:46 +01003156 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003157 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003158
3159 iommu = amd_iommu_rlookup_table[devid];
3160 if (!iommu)
3161 return;
3162
Joerg Roedel684f2882008-12-08 12:07:44 +01003163 iommu_completion_wait(iommu);
3164}
3165
Joerg Roedel01106062008-12-02 19:34:11 +01003166static int amd_iommu_attach_device(struct iommu_domain *dom,
3167 struct device *dev)
3168{
3169 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003170 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003171 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003172 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003173
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003174 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003175 return -EINVAL;
3176
Joerg Roedel657cbb62009-11-23 15:26:46 +01003177 dev_data = dev->archdata.iommu;
3178
Joerg Roedelf62dda62011-06-09 12:55:35 +02003179 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003180 if (!iommu)
3181 return -EINVAL;
3182
Joerg Roedel657cbb62009-11-23 15:26:46 +01003183 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003184 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003185
Joerg Roedel15898bb2009-11-24 15:39:42 +01003186 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003187
3188 iommu_completion_wait(iommu);
3189
Joerg Roedel15898bb2009-11-24 15:39:42 +01003190 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003191}
3192
Joerg Roedel468e2362010-01-21 16:37:36 +01003193static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003194 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003195{
3196 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003197 int prot = 0;
3198 int ret;
3199
Joerg Roedel132bd682011-11-17 14:18:46 +01003200 if (domain->mode == PAGE_MODE_NONE)
3201 return -EINVAL;
3202
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003203 if (iommu_prot & IOMMU_READ)
3204 prot |= IOMMU_PROT_IR;
3205 if (iommu_prot & IOMMU_WRITE)
3206 prot |= IOMMU_PROT_IW;
3207
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003208 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003209 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003210 mutex_unlock(&domain->api_lock);
3211
Joerg Roedel795e74f2010-05-11 17:40:57 +02003212 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003213}
3214
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003215static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3216 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003217{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003218 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003219 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003220
Joerg Roedel132bd682011-11-17 14:18:46 +01003221 if (domain->mode == PAGE_MODE_NONE)
3222 return -EINVAL;
3223
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003224 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003225 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003226 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003227
Joerg Roedel17b124b2011-04-06 18:01:35 +02003228 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003229
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003230 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003231}
3232
Joerg Roedel645c4c82008-12-02 20:05:50 +01003233static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3234 unsigned long iova)
3235{
3236 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003237 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003238 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003239 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003240
Joerg Roedel132bd682011-11-17 14:18:46 +01003241 if (domain->mode == PAGE_MODE_NONE)
3242 return iova;
3243
Joerg Roedel24cd7722010-01-19 17:27:39 +01003244 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003245
Joerg Roedela6d41a42009-09-02 17:08:55 +02003246 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003247 return 0;
3248
Joerg Roedelf03152b2010-01-21 16:15:24 +01003249 if (PM_PTE_LEVEL(*pte) == 0)
3250 offset_mask = PAGE_SIZE - 1;
3251 else
3252 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3253
3254 __pte = *pte & PM_ADDR_MASK;
3255 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003256
3257 return paddr;
3258}
3259
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003260static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3261 unsigned long cap)
3262{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003263 switch (cap) {
3264 case IOMMU_CAP_CACHE_COHERENCY:
3265 return 1;
3266 }
3267
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003268 return 0;
3269}
3270
Alex Williamson8fbdce62011-10-21 15:56:18 -04003271static int amd_iommu_device_group(struct device *dev, unsigned int *groupid)
3272{
3273 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Alex Williamsonbcb71ab2011-10-21 15:56:24 -04003274 struct pci_dev *pdev = to_pci_dev(dev);
3275 u16 devid;
Alex Williamson8fbdce62011-10-21 15:56:18 -04003276
3277 if (!dev_data)
3278 return -ENODEV;
3279
Alex Williamsonbcb71ab2011-10-21 15:56:24 -04003280 if (pdev->is_virtfn || !iommu_group_mf)
3281 devid = dev_data->devid;
3282 else
3283 devid = calc_devid(pdev->bus->number,
3284 PCI_DEVFN(PCI_SLOT(pdev->devfn), 0));
3285
3286 *groupid = amd_iommu_alias_table[devid];
Alex Williamson8fbdce62011-10-21 15:56:18 -04003287
3288 return 0;
3289}
3290
Joerg Roedel26961ef2008-12-03 17:00:17 +01003291static struct iommu_ops amd_iommu_ops = {
3292 .domain_init = amd_iommu_domain_init,
3293 .domain_destroy = amd_iommu_domain_destroy,
3294 .attach_dev = amd_iommu_attach_device,
3295 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003296 .map = amd_iommu_map,
3297 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003298 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003299 .domain_has_cap = amd_iommu_domain_has_cap,
Alex Williamson8fbdce62011-10-21 15:56:18 -04003300 .device_group = amd_iommu_device_group,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003301 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003302};
3303
Joerg Roedel0feae532009-08-26 15:26:30 +02003304/*****************************************************************************
3305 *
3306 * The next functions do a basic initialization of IOMMU for pass through
3307 * mode
3308 *
3309 * In passthrough mode the IOMMU is initialized and enabled but not used for
3310 * DMA-API translation.
3311 *
3312 *****************************************************************************/
3313
3314int __init amd_iommu_init_passthrough(void)
3315{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003316 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003317 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003318 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003319 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003320 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003321
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003322 ret = alloc_passthrough_domain();
3323 if (ret)
3324 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003325
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003326 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003327 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003328 continue;
3329
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003330 dev_data = get_dev_data(&dev->dev);
3331 dev_data->passthrough = true;
3332
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003333 devid = get_device_id(&dev->dev);
3334
Joerg Roedel15898bb2009-11-24 15:39:42 +01003335 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003336 if (!iommu)
3337 continue;
3338
Joerg Roedel15898bb2009-11-24 15:39:42 +01003339 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003340 }
3341
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003342 amd_iommu_stats_init();
3343
Joerg Roedel0feae532009-08-26 15:26:30 +02003344 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3345
3346 return 0;
3347}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003348
3349/* IOMMUv2 specific functions */
3350int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3351{
3352 return atomic_notifier_chain_register(&ppr_notifier, nb);
3353}
3354EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3355
3356int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3357{
3358 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3359}
3360EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003361
3362void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3363{
3364 struct protection_domain *domain = dom->priv;
3365 unsigned long flags;
3366
3367 spin_lock_irqsave(&domain->lock, flags);
3368
3369 /* Update data structure */
3370 domain->mode = PAGE_MODE_NONE;
3371 domain->updated = true;
3372
3373 /* Make changes visible to IOMMUs */
3374 update_domain(domain);
3375
3376 /* Page-table is not visible to IOMMU anymore, so free it */
3377 free_pagetable(domain);
3378
3379 spin_unlock_irqrestore(&domain->lock, flags);
3380}
3381EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003382
3383int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3384{
3385 struct protection_domain *domain = dom->priv;
3386 unsigned long flags;
3387 int levels, ret;
3388
3389 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3390 return -EINVAL;
3391
3392 /* Number of GCR3 table levels required */
3393 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3394 levels += 1;
3395
3396 if (levels > amd_iommu_max_glx_val)
3397 return -EINVAL;
3398
3399 spin_lock_irqsave(&domain->lock, flags);
3400
3401 /*
3402 * Save us all sanity checks whether devices already in the
3403 * domain support IOMMUv2. Just force that the domain has no
3404 * devices attached when it is switched into IOMMUv2 mode.
3405 */
3406 ret = -EBUSY;
3407 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3408 goto out;
3409
3410 ret = -ENOMEM;
3411 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3412 if (domain->gcr3_tbl == NULL)
3413 goto out;
3414
3415 domain->glx = levels;
3416 domain->flags |= PD_IOMMUV2_MASK;
3417 domain->updated = true;
3418
3419 update_domain(domain);
3420
3421 ret = 0;
3422
3423out:
3424 spin_unlock_irqrestore(&domain->lock, flags);
3425
3426 return ret;
3427}
3428EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003429
3430static int __flush_pasid(struct protection_domain *domain, int pasid,
3431 u64 address, bool size)
3432{
3433 struct iommu_dev_data *dev_data;
3434 struct iommu_cmd cmd;
3435 int i, ret;
3436
3437 if (!(domain->flags & PD_IOMMUV2_MASK))
3438 return -EINVAL;
3439
3440 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3441
3442 /*
3443 * IOMMU TLB needs to be flushed before Device TLB to
3444 * prevent device TLB refill from IOMMU TLB
3445 */
3446 for (i = 0; i < amd_iommus_present; ++i) {
3447 if (domain->dev_iommu[i] == 0)
3448 continue;
3449
3450 ret = iommu_queue_command(amd_iommus[i], &cmd);
3451 if (ret != 0)
3452 goto out;
3453 }
3454
3455 /* Wait until IOMMU TLB flushes are complete */
3456 domain_flush_complete(domain);
3457
3458 /* Now flush device TLBs */
3459 list_for_each_entry(dev_data, &domain->dev_list, list) {
3460 struct amd_iommu *iommu;
3461 int qdep;
3462
3463 BUG_ON(!dev_data->ats.enabled);
3464
3465 qdep = dev_data->ats.qdep;
3466 iommu = amd_iommu_rlookup_table[dev_data->devid];
3467
3468 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3469 qdep, address, size);
3470
3471 ret = iommu_queue_command(iommu, &cmd);
3472 if (ret != 0)
3473 goto out;
3474 }
3475
3476 /* Wait until all device TLBs are flushed */
3477 domain_flush_complete(domain);
3478
3479 ret = 0;
3480
3481out:
3482
3483 return ret;
3484}
3485
3486static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3487 u64 address)
3488{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003489 INC_STATS_COUNTER(invalidate_iotlb);
3490
Joerg Roedel22e266c2011-11-21 15:59:08 +01003491 return __flush_pasid(domain, pasid, address, false);
3492}
3493
3494int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3495 u64 address)
3496{
3497 struct protection_domain *domain = dom->priv;
3498 unsigned long flags;
3499 int ret;
3500
3501 spin_lock_irqsave(&domain->lock, flags);
3502 ret = __amd_iommu_flush_page(domain, pasid, address);
3503 spin_unlock_irqrestore(&domain->lock, flags);
3504
3505 return ret;
3506}
3507EXPORT_SYMBOL(amd_iommu_flush_page);
3508
3509static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3510{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003511 INC_STATS_COUNTER(invalidate_iotlb_all);
3512
Joerg Roedel22e266c2011-11-21 15:59:08 +01003513 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3514 true);
3515}
3516
3517int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3518{
3519 struct protection_domain *domain = dom->priv;
3520 unsigned long flags;
3521 int ret;
3522
3523 spin_lock_irqsave(&domain->lock, flags);
3524 ret = __amd_iommu_flush_tlb(domain, pasid);
3525 spin_unlock_irqrestore(&domain->lock, flags);
3526
3527 return ret;
3528}
3529EXPORT_SYMBOL(amd_iommu_flush_tlb);
3530
Joerg Roedelb16137b2011-11-21 16:50:23 +01003531static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3532{
3533 int index;
3534 u64 *pte;
3535
3536 while (true) {
3537
3538 index = (pasid >> (9 * level)) & 0x1ff;
3539 pte = &root[index];
3540
3541 if (level == 0)
3542 break;
3543
3544 if (!(*pte & GCR3_VALID)) {
3545 if (!alloc)
3546 return NULL;
3547
3548 root = (void *)get_zeroed_page(GFP_ATOMIC);
3549 if (root == NULL)
3550 return NULL;
3551
3552 *pte = __pa(root) | GCR3_VALID;
3553 }
3554
3555 root = __va(*pte & PAGE_MASK);
3556
3557 level -= 1;
3558 }
3559
3560 return pte;
3561}
3562
3563static int __set_gcr3(struct protection_domain *domain, int pasid,
3564 unsigned long cr3)
3565{
3566 u64 *pte;
3567
3568 if (domain->mode != PAGE_MODE_NONE)
3569 return -EINVAL;
3570
3571 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3572 if (pte == NULL)
3573 return -ENOMEM;
3574
3575 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3576
3577 return __amd_iommu_flush_tlb(domain, pasid);
3578}
3579
3580static int __clear_gcr3(struct protection_domain *domain, int pasid)
3581{
3582 u64 *pte;
3583
3584 if (domain->mode != PAGE_MODE_NONE)
3585 return -EINVAL;
3586
3587 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3588 if (pte == NULL)
3589 return 0;
3590
3591 *pte = 0;
3592
3593 return __amd_iommu_flush_tlb(domain, pasid);
3594}
3595
3596int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3597 unsigned long cr3)
3598{
3599 struct protection_domain *domain = dom->priv;
3600 unsigned long flags;
3601 int ret;
3602
3603 spin_lock_irqsave(&domain->lock, flags);
3604 ret = __set_gcr3(domain, pasid, cr3);
3605 spin_unlock_irqrestore(&domain->lock, flags);
3606
3607 return ret;
3608}
3609EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3610
3611int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3612{
3613 struct protection_domain *domain = dom->priv;
3614 unsigned long flags;
3615 int ret;
3616
3617 spin_lock_irqsave(&domain->lock, flags);
3618 ret = __clear_gcr3(domain, pasid);
3619 spin_unlock_irqrestore(&domain->lock, flags);
3620
3621 return ret;
3622}
3623EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003624
3625int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3626 int status, int tag)
3627{
3628 struct iommu_dev_data *dev_data;
3629 struct amd_iommu *iommu;
3630 struct iommu_cmd cmd;
3631
Joerg Roedel399be2f2011-12-01 16:53:47 +01003632 INC_STATS_COUNTER(complete_ppr);
3633
Joerg Roedelc99afa22011-11-21 18:19:25 +01003634 dev_data = get_dev_data(&pdev->dev);
3635 iommu = amd_iommu_rlookup_table[dev_data->devid];
3636
3637 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3638 tag, dev_data->pri_tlp);
3639
3640 return iommu_queue_command(iommu, &cmd);
3641}
3642EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003643
3644struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3645{
3646 struct protection_domain *domain;
3647
3648 domain = get_domain(&pdev->dev);
3649 if (IS_ERR(domain))
3650 return NULL;
3651
3652 /* Only return IOMMUv2 domains */
3653 if (!(domain->flags & PD_IOMMUV2_MASK))
3654 return NULL;
3655
3656 return domain->iommu_domain;
3657}
3658EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003659
3660void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3661{
3662 struct iommu_dev_data *dev_data;
3663
3664 if (!amd_iommu_v2_supported())
3665 return;
3666
3667 dev_data = get_dev_data(&pdev->dev);
3668 dev_data->errata |= (1 << erratum);
3669}
3670EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003671
3672int amd_iommu_device_info(struct pci_dev *pdev,
3673 struct amd_iommu_device_info *info)
3674{
3675 int max_pasids;
3676 int pos;
3677
3678 if (pdev == NULL || info == NULL)
3679 return -EINVAL;
3680
3681 if (!amd_iommu_v2_supported())
3682 return -EINVAL;
3683
3684 memset(info, 0, sizeof(*info));
3685
3686 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3687 if (pos)
3688 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3689
3690 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3691 if (pos)
3692 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3693
3694 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3695 if (pos) {
3696 int features;
3697
3698 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3699 max_pasids = min(max_pasids, (1 << 20));
3700
3701 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3702 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3703
3704 features = pci_pasid_features(pdev);
3705 if (features & PCI_PASID_CAP_EXEC)
3706 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3707 if (features & PCI_PASID_CAP_PRIV)
3708 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3709 }
3710
3711 return 0;
3712}
3713EXPORT_SYMBOL(amd_iommu_device_info);