blob: 7fa97a5e3eaf69a14318250063f2da0c120149d1 [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 Roedel2b324502012-06-21 16:29:10 +020034#include <linux/irq.h>
35#include <linux/msi.h>
36#include <asm/irq_remapping.h>
37#include <asm/io_apic.h>
38#include <asm/apic.h>
39#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020040#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020041#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090042#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010043#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020044#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020045
46#include "amd_iommu_proto.h"
47#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020048#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020049
50#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
51
Joerg Roedel815b33f2011-04-06 17:26:49 +020052#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020053
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020054/*
55 * This bitmap is used to advertise the page sizes our hardware support
56 * to the IOMMU core, which will then use this information to split
57 * physically contiguous memory regions it is mapping into page sizes
58 * that we support.
59 *
60 * Traditionally the IOMMU core just handed us the mappings directly,
61 * after making sure the size is an order of a 4KiB page and that the
62 * mapping has natural alignment.
63 *
64 * To retain this behavior, we currently advertise that we support
65 * all page sizes that are an order of 4KiB.
66 *
67 * If at some point we'd like to utilize the IOMMU core's new behavior,
68 * we could change this to advertise the real page sizes we support.
69 */
70#define AMD_IOMMU_PGSIZES (~0xFFFUL)
71
Joerg Roedelb6c02712008-06-26 21:27:53 +020072static DEFINE_RWLOCK(amd_iommu_devtable_lock);
73
Joerg Roedelbd60b732008-09-11 10:24:48 +020074/* A list of preallocated protection domains */
75static LIST_HEAD(iommu_pd_list);
76static DEFINE_SPINLOCK(iommu_pd_list_lock);
77
Joerg Roedel8fa5f802011-06-09 12:24:45 +020078/* List of all available dev_data structures */
79static LIST_HEAD(dev_data_list);
80static DEFINE_SPINLOCK(dev_data_list_lock);
81
Joerg Roedel6efed632012-06-14 15:52:58 +020082LIST_HEAD(ioapic_map);
83LIST_HEAD(hpet_map);
84
Joerg Roedel0feae532009-08-26 15:26:30 +020085/*
86 * Domain for untranslated devices - only allocated
87 * if iommu=pt passed on kernel cmd line.
88 */
89static struct protection_domain *pt_domain;
90
Joerg Roedel26961ef2008-12-03 17:00:17 +010091static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010092
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010093static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010094int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010095
Joerg Roedelac1534a2012-06-21 14:52:40 +020096static struct dma_map_ops amd_iommu_dma_ops;
97
Joerg Roedel431b2a22008-07-11 17:14:22 +020098/*
99 * general struct to manage commands send to an IOMMU
100 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200101struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200102 u32 data[4];
103};
104
Joerg Roedel05152a02012-06-15 16:53:51 +0200105struct kmem_cache *amd_iommu_irq_cache;
106
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200107static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100108static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700109
Joerg Roedel15898bb2009-11-24 15:39:42 +0100110/****************************************************************************
111 *
112 * Helper functions
113 *
114 ****************************************************************************/
115
Joerg Roedelf62dda62011-06-09 12:55:35 +0200116static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200117{
118 struct iommu_dev_data *dev_data;
119 unsigned long flags;
120
121 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
122 if (!dev_data)
123 return NULL;
124
Joerg Roedelf62dda62011-06-09 12:55:35 +0200125 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200126 atomic_set(&dev_data->bind, 0);
127
128 spin_lock_irqsave(&dev_data_list_lock, flags);
129 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
130 spin_unlock_irqrestore(&dev_data_list_lock, flags);
131
132 return dev_data;
133}
134
135static void free_dev_data(struct iommu_dev_data *dev_data)
136{
137 unsigned long flags;
138
139 spin_lock_irqsave(&dev_data_list_lock, flags);
140 list_del(&dev_data->dev_data_list);
141 spin_unlock_irqrestore(&dev_data_list_lock, flags);
142
143 kfree(dev_data);
144}
145
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200146static struct iommu_dev_data *search_dev_data(u16 devid)
147{
148 struct iommu_dev_data *dev_data;
149 unsigned long flags;
150
151 spin_lock_irqsave(&dev_data_list_lock, flags);
152 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
153 if (dev_data->devid == devid)
154 goto out_unlock;
155 }
156
157 dev_data = NULL;
158
159out_unlock:
160 spin_unlock_irqrestore(&dev_data_list_lock, flags);
161
162 return dev_data;
163}
164
165static struct iommu_dev_data *find_dev_data(u16 devid)
166{
167 struct iommu_dev_data *dev_data;
168
169 dev_data = search_dev_data(devid);
170
171 if (dev_data == NULL)
172 dev_data = alloc_dev_data(devid);
173
174 return dev_data;
175}
176
Joerg Roedel15898bb2009-11-24 15:39:42 +0100177static inline u16 get_device_id(struct device *dev)
178{
179 struct pci_dev *pdev = to_pci_dev(dev);
180
181 return calc_devid(pdev->bus->number, pdev->devfn);
182}
183
Joerg Roedel657cbb62009-11-23 15:26:46 +0100184static struct iommu_dev_data *get_dev_data(struct device *dev)
185{
186 return dev->archdata.iommu;
187}
188
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100189static bool pci_iommuv2_capable(struct pci_dev *pdev)
190{
191 static const int caps[] = {
192 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100193 PCI_EXT_CAP_ID_PRI,
194 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100195 };
196 int i, pos;
197
198 for (i = 0; i < 3; ++i) {
199 pos = pci_find_ext_capability(pdev, caps[i]);
200 if (pos == 0)
201 return false;
202 }
203
204 return true;
205}
206
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100207static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
208{
209 struct iommu_dev_data *dev_data;
210
211 dev_data = get_dev_data(&pdev->dev);
212
213 return dev_data->errata & (1 << erratum) ? true : false;
214}
215
Joerg Roedel71c70982009-11-24 16:43:06 +0100216/*
217 * In this function the list of preallocated protection domains is traversed to
218 * find the domain for a specific device
219 */
220static struct dma_ops_domain *find_protection_domain(u16 devid)
221{
222 struct dma_ops_domain *entry, *ret = NULL;
223 unsigned long flags;
224 u16 alias = amd_iommu_alias_table[devid];
225
226 if (list_empty(&iommu_pd_list))
227 return NULL;
228
229 spin_lock_irqsave(&iommu_pd_list_lock, flags);
230
231 list_for_each_entry(entry, &iommu_pd_list, list) {
232 if (entry->target_dev == devid ||
233 entry->target_dev == alias) {
234 ret = entry;
235 break;
236 }
237 }
238
239 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
240
241 return ret;
242}
243
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100244/*
245 * This function checks if the driver got a valid device from the caller to
246 * avoid dereferencing invalid pointers.
247 */
248static bool check_device(struct device *dev)
249{
250 u16 devid;
251
252 if (!dev || !dev->dma_mask)
253 return false;
254
255 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100256 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100257 return false;
258
259 devid = get_device_id(dev);
260
261 /* Out of our scope? */
262 if (devid > amd_iommu_last_bdf)
263 return false;
264
265 if (amd_iommu_rlookup_table[devid] == NULL)
266 return false;
267
268 return true;
269}
270
Alex Williamson664b6002012-05-30 14:19:31 -0600271static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
272{
273 pci_dev_put(*from);
274 *from = to;
275}
276
Alex Williamson2bff6a52012-10-08 22:49:48 -0600277static struct pci_bus *find_hosted_bus(struct pci_bus *bus)
278{
279 while (!bus->self) {
280 if (!pci_is_root_bus(bus))
281 bus = bus->parent;
282 else
283 return ERR_PTR(-ENODEV);
284 }
285
286 return bus;
287}
288
Alex Williamson664b6002012-05-30 14:19:31 -0600289#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
290
Alex Williamson2851db22012-10-08 22:49:41 -0600291static struct pci_dev *get_isolation_root(struct pci_dev *pdev)
Joerg Roedel657cbb62009-11-23 15:26:46 +0100292{
Alex Williamson2851db22012-10-08 22:49:41 -0600293 struct pci_dev *dma_pdev = pdev;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600294
Alex Williamson31fe9432012-08-04 12:09:03 -0600295 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600296 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
297
Alex Williamson31fe9432012-08-04 12:09:03 -0600298 /*
299 * If it's a multifunction device that does not support our
300 * required ACS flags, add to the same group as function 0.
301 */
Alex Williamson664b6002012-05-30 14:19:31 -0600302 if (dma_pdev->multifunction &&
303 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
304 swap_pci_ref(&dma_pdev,
305 pci_get_slot(dma_pdev->bus,
306 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
307 0)));
308
Alex Williamson31fe9432012-08-04 12:09:03 -0600309 /*
310 * Devices on the root bus go through the iommu. If that's not us,
311 * find the next upstream device and test ACS up to the root bus.
312 * Finding the next device may require skipping virtual buses.
313 */
Alex Williamson664b6002012-05-30 14:19:31 -0600314 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson2bff6a52012-10-08 22:49:48 -0600315 struct pci_bus *bus = find_hosted_bus(dma_pdev->bus);
316 if (IS_ERR(bus))
317 break;
Alex Williamson31fe9432012-08-04 12:09:03 -0600318
319 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600320 break;
321
Alex Williamson31fe9432012-08-04 12:09:03 -0600322 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200323 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100324
Alex Williamson2851db22012-10-08 22:49:41 -0600325 return dma_pdev;
326}
327
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600328static int use_pdev_iommu_group(struct pci_dev *pdev, struct device *dev)
329{
330 struct iommu_group *group = iommu_group_get(&pdev->dev);
331 int ret;
332
333 if (!group) {
334 group = iommu_group_alloc();
335 if (IS_ERR(group))
336 return PTR_ERR(group);
337
338 WARN_ON(&pdev->dev != dev);
339 }
340
341 ret = iommu_group_add_device(group, dev);
342 iommu_group_put(group);
343 return ret;
344}
345
Alex Williamson2851db22012-10-08 22:49:41 -0600346static int init_iommu_group(struct device *dev)
347{
348 struct iommu_dev_data *dev_data;
349 struct iommu_group *group;
350 struct pci_dev *dma_pdev = NULL;
351 int ret;
352
353 group = iommu_group_get(dev);
354 if (group) {
355 iommu_group_put(group);
356 return 0;
357 }
358
359 dev_data = find_dev_data(get_device_id(dev));
360 if (!dev_data)
361 return -ENOMEM;
362
363 if (dev_data->alias_data) {
364 u16 alias;
365
366 alias = amd_iommu_alias_table[dev_data->devid];
367 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
368 }
369
370 if (!dma_pdev)
371 dma_pdev = pci_dev_get(to_pci_dev(dev));
372
373 dma_pdev = get_isolation_root(dma_pdev);
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600374 ret = use_pdev_iommu_group(dma_pdev, dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600375 pci_dev_put(dma_pdev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600376 return ret;
377}
378
379static int iommu_init_device(struct device *dev)
380{
381 struct pci_dev *pdev = to_pci_dev(dev);
382 struct iommu_dev_data *dev_data;
383 u16 alias;
384 int ret;
385
386 if (dev->archdata.iommu)
387 return 0;
388
389 dev_data = find_dev_data(get_device_id(dev));
390 if (!dev_data)
391 return -ENOMEM;
392
393 alias = amd_iommu_alias_table[dev_data->devid];
394 if (alias != dev_data->devid) {
395 struct iommu_dev_data *alias_data;
396
397 alias_data = find_dev_data(alias);
398 if (alias_data == NULL) {
399 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
400 dev_name(dev));
401 free_dev_data(dev_data);
402 return -ENOTSUPP;
403 }
404 dev_data->alias_data = alias_data;
405 }
406
407 ret = init_iommu_group(dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600408 if (ret)
409 return ret;
410
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100411 if (pci_iommuv2_capable(pdev)) {
412 struct amd_iommu *iommu;
413
414 iommu = amd_iommu_rlookup_table[dev_data->devid];
415 dev_data->iommu_v2 = iommu->is_iommu_v2;
416 }
417
Joerg Roedel657cbb62009-11-23 15:26:46 +0100418 dev->archdata.iommu = dev_data;
419
Joerg Roedel657cbb62009-11-23 15:26:46 +0100420 return 0;
421}
422
Joerg Roedel26018872011-06-06 16:50:14 +0200423static void iommu_ignore_device(struct device *dev)
424{
425 u16 devid, alias;
426
427 devid = get_device_id(dev);
428 alias = amd_iommu_alias_table[devid];
429
430 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
431 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
432
433 amd_iommu_rlookup_table[devid] = NULL;
434 amd_iommu_rlookup_table[alias] = NULL;
435}
436
Joerg Roedel657cbb62009-11-23 15:26:46 +0100437static void iommu_uninit_device(struct device *dev)
438{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600439 iommu_group_remove_device(dev);
440
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200441 /*
442 * Nothing to do here - we keep dev_data around for unplugged devices
443 * and reuse it when the device is re-plugged - not doing so would
444 * introduce a ton of races.
445 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100446}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100447
448void __init amd_iommu_uninit_devices(void)
449{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200450 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100451 struct pci_dev *pdev = NULL;
452
453 for_each_pci_dev(pdev) {
454
455 if (!check_device(&pdev->dev))
456 continue;
457
458 iommu_uninit_device(&pdev->dev);
459 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200460
461 /* Free all of our dev_data structures */
462 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
463 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100464}
465
466int __init amd_iommu_init_devices(void)
467{
468 struct pci_dev *pdev = NULL;
469 int ret = 0;
470
471 for_each_pci_dev(pdev) {
472
473 if (!check_device(&pdev->dev))
474 continue;
475
476 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200477 if (ret == -ENOTSUPP)
478 iommu_ignore_device(&pdev->dev);
479 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100480 goto out_free;
481 }
482
483 return 0;
484
485out_free:
486
487 amd_iommu_uninit_devices();
488
489 return ret;
490}
Joerg Roedel7f265082008-12-12 13:50:21 +0100491#ifdef CONFIG_AMD_IOMMU_STATS
492
493/*
494 * Initialization code for statistics collection
495 */
496
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100497DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100498DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100499DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100500DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100501DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100502DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100503DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100504DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100505DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100506DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100507DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100508DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100509DECLARE_STATS_COUNTER(complete_ppr);
510DECLARE_STATS_COUNTER(invalidate_iotlb);
511DECLARE_STATS_COUNTER(invalidate_iotlb_all);
512DECLARE_STATS_COUNTER(pri_requests);
513
Joerg Roedel7f265082008-12-12 13:50:21 +0100514static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100515static struct dentry *de_fflush;
516
517static void amd_iommu_stats_add(struct __iommu_counter *cnt)
518{
519 if (stats_dir == NULL)
520 return;
521
522 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
523 &cnt->value);
524}
525
526static void amd_iommu_stats_init(void)
527{
528 stats_dir = debugfs_create_dir("amd-iommu", NULL);
529 if (stats_dir == NULL)
530 return;
531
Joerg Roedel7f265082008-12-12 13:50:21 +0100532 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300533 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100534
535 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100536 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100537 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100538 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100539 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100540 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100541 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100542 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100543 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100544 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100545 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100546 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100547 amd_iommu_stats_add(&complete_ppr);
548 amd_iommu_stats_add(&invalidate_iotlb);
549 amd_iommu_stats_add(&invalidate_iotlb_all);
550 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100551}
552
553#endif
554
Joerg Roedel431b2a22008-07-11 17:14:22 +0200555/****************************************************************************
556 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200557 * Interrupt handling functions
558 *
559 ****************************************************************************/
560
Joerg Roedele3e59872009-09-03 14:02:10 +0200561static void dump_dte_entry(u16 devid)
562{
563 int i;
564
Joerg Roedelee6c2862011-11-09 12:06:03 +0100565 for (i = 0; i < 4; ++i)
566 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200567 amd_iommu_dev_table[devid].data[i]);
568}
569
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200570static void dump_command(unsigned long phys_addr)
571{
572 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
573 int i;
574
575 for (i = 0; i < 4; ++i)
576 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
577}
578
Joerg Roedela345b232009-09-03 15:01:43 +0200579static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200580{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200581 int type, devid, domid, flags;
582 volatile u32 *event = __evt;
583 int count = 0;
584 u64 address;
585
586retry:
587 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
588 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
589 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
590 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
591 address = (u64)(((u64)event[3]) << 32) | event[2];
592
593 if (type == 0) {
594 /* Did we hit the erratum? */
595 if (++count == LOOP_TIMEOUT) {
596 pr_err("AMD-Vi: No event written to event log\n");
597 return;
598 }
599 udelay(1);
600 goto retry;
601 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200602
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200603 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200604
605 switch (type) {
606 case EVENT_TYPE_ILL_DEV:
607 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
608 "address=0x%016llx flags=0x%04x]\n",
609 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
610 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200611 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200612 break;
613 case EVENT_TYPE_IO_FAULT:
614 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
615 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
616 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
617 domid, address, flags);
618 break;
619 case EVENT_TYPE_DEV_TAB_ERR:
620 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
621 "address=0x%016llx flags=0x%04x]\n",
622 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
623 address, flags);
624 break;
625 case EVENT_TYPE_PAGE_TAB_ERR:
626 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
627 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
628 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
629 domid, address, flags);
630 break;
631 case EVENT_TYPE_ILL_CMD:
632 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200633 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200634 break;
635 case EVENT_TYPE_CMD_HARD_ERR:
636 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
637 "flags=0x%04x]\n", address, flags);
638 break;
639 case EVENT_TYPE_IOTLB_INV_TO:
640 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
641 "address=0x%016llx]\n",
642 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
643 address);
644 break;
645 case EVENT_TYPE_INV_DEV_REQ:
646 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
647 "address=0x%016llx flags=0x%04x]\n",
648 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
649 address, flags);
650 break;
651 default:
652 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
653 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200654
655 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200656}
657
658static void iommu_poll_events(struct amd_iommu *iommu)
659{
660 u32 head, tail;
661 unsigned long flags;
662
663 spin_lock_irqsave(&iommu->lock, flags);
664
665 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
666 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
667
668 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200669 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200670 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
671 }
672
673 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
674
675 spin_unlock_irqrestore(&iommu->lock, flags);
676}
677
Joerg Roedeleee53532012-06-01 15:20:23 +0200678static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100679{
680 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100681
Joerg Roedel399be2f2011-12-01 16:53:47 +0100682 INC_STATS_COUNTER(pri_requests);
683
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100684 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
685 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
686 return;
687 }
688
689 fault.address = raw[1];
690 fault.pasid = PPR_PASID(raw[0]);
691 fault.device_id = PPR_DEVID(raw[0]);
692 fault.tag = PPR_TAG(raw[0]);
693 fault.flags = PPR_FLAGS(raw[0]);
694
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100695 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
696}
697
698static void iommu_poll_ppr_log(struct amd_iommu *iommu)
699{
700 unsigned long flags;
701 u32 head, tail;
702
703 if (iommu->ppr_log == NULL)
704 return;
705
Joerg Roedeleee53532012-06-01 15:20:23 +0200706 /* enable ppr interrupts again */
707 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
708
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100709 spin_lock_irqsave(&iommu->lock, flags);
710
711 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
712 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
713
714 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200715 volatile u64 *raw;
716 u64 entry[2];
717 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100718
Joerg Roedeleee53532012-06-01 15:20:23 +0200719 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100720
Joerg Roedeleee53532012-06-01 15:20:23 +0200721 /*
722 * Hardware bug: Interrupt may arrive before the entry is
723 * written to memory. If this happens we need to wait for the
724 * entry to arrive.
725 */
726 for (i = 0; i < LOOP_TIMEOUT; ++i) {
727 if (PPR_REQ_TYPE(raw[0]) != 0)
728 break;
729 udelay(1);
730 }
731
732 /* Avoid memcpy function-call overhead */
733 entry[0] = raw[0];
734 entry[1] = raw[1];
735
736 /*
737 * To detect the hardware bug we need to clear the entry
738 * back to zero.
739 */
740 raw[0] = raw[1] = 0UL;
741
742 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100743 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
744 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200745
746 /*
747 * Release iommu->lock because ppr-handling might need to
Frank Arnolddf805ab2012-08-27 19:21:04 +0200748 * re-acquire it
Joerg Roedeleee53532012-06-01 15:20:23 +0200749 */
750 spin_unlock_irqrestore(&iommu->lock, flags);
751
752 /* Handle PPR entry */
753 iommu_handle_ppr_entry(iommu, entry);
754
755 spin_lock_irqsave(&iommu->lock, flags);
756
757 /* Refresh ring-buffer information */
758 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100759 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
760 }
761
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100762 spin_unlock_irqrestore(&iommu->lock, flags);
763}
764
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200765irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200766{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200767 struct amd_iommu *iommu;
768
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100769 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200770 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100771 iommu_poll_ppr_log(iommu);
772 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200773
774 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200775}
776
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200777irqreturn_t amd_iommu_int_handler(int irq, void *data)
778{
779 return IRQ_WAKE_THREAD;
780}
781
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200782/****************************************************************************
783 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200784 * IOMMU command queuing functions
785 *
786 ****************************************************************************/
787
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200788static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200789{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200790 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200791
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200792 while (*sem == 0 && i < LOOP_TIMEOUT) {
793 udelay(1);
794 i += 1;
795 }
796
797 if (i == LOOP_TIMEOUT) {
798 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
799 return -EIO;
800 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200801
802 return 0;
803}
804
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200805static void copy_cmd_to_buffer(struct amd_iommu *iommu,
806 struct iommu_cmd *cmd,
807 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200808{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200809 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200810
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200811 target = iommu->cmd_buf + tail;
812 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200813
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200814 /* Copy command to buffer */
815 memcpy(target, cmd, sizeof(*cmd));
816
817 /* Tell the IOMMU about it */
818 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
819}
820
Joerg Roedel815b33f2011-04-06 17:26:49 +0200821static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200822{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200823 WARN_ON(address & 0x7ULL);
824
Joerg Roedelded46732011-04-06 10:53:48 +0200825 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200826 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
827 cmd->data[1] = upper_32_bits(__pa(address));
828 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200829 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
830}
831
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200832static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
833{
834 memset(cmd, 0, sizeof(*cmd));
835 cmd->data[0] = devid;
836 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
837}
838
Joerg Roedel11b64022011-04-06 11:49:28 +0200839static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
840 size_t size, u16 domid, int pde)
841{
842 u64 pages;
843 int s;
844
845 pages = iommu_num_pages(address, size, PAGE_SIZE);
846 s = 0;
847
848 if (pages > 1) {
849 /*
850 * If we have to flush more than one page, flush all
851 * TLB entries for this domain
852 */
853 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
854 s = 1;
855 }
856
857 address &= PAGE_MASK;
858
859 memset(cmd, 0, sizeof(*cmd));
860 cmd->data[1] |= domid;
861 cmd->data[2] = lower_32_bits(address);
862 cmd->data[3] = upper_32_bits(address);
863 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
864 if (s) /* size bit - we flush more than one 4kb page */
865 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200866 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200867 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
868}
869
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200870static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
871 u64 address, size_t size)
872{
873 u64 pages;
874 int s;
875
876 pages = iommu_num_pages(address, size, PAGE_SIZE);
877 s = 0;
878
879 if (pages > 1) {
880 /*
881 * If we have to flush more than one page, flush all
882 * TLB entries for this domain
883 */
884 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
885 s = 1;
886 }
887
888 address &= PAGE_MASK;
889
890 memset(cmd, 0, sizeof(*cmd));
891 cmd->data[0] = devid;
892 cmd->data[0] |= (qdep & 0xff) << 24;
893 cmd->data[1] = devid;
894 cmd->data[2] = lower_32_bits(address);
895 cmd->data[3] = upper_32_bits(address);
896 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
897 if (s)
898 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
899}
900
Joerg Roedel22e266c2011-11-21 15:59:08 +0100901static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
902 u64 address, bool size)
903{
904 memset(cmd, 0, sizeof(*cmd));
905
906 address &= ~(0xfffULL);
907
908 cmd->data[0] = pasid & PASID_MASK;
909 cmd->data[1] = domid;
910 cmd->data[2] = lower_32_bits(address);
911 cmd->data[3] = upper_32_bits(address);
912 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
913 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
914 if (size)
915 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
916 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
917}
918
919static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
920 int qdep, u64 address, bool size)
921{
922 memset(cmd, 0, sizeof(*cmd));
923
924 address &= ~(0xfffULL);
925
926 cmd->data[0] = devid;
927 cmd->data[0] |= (pasid & 0xff) << 16;
928 cmd->data[0] |= (qdep & 0xff) << 24;
929 cmd->data[1] = devid;
930 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
931 cmd->data[2] = lower_32_bits(address);
932 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
933 cmd->data[3] = upper_32_bits(address);
934 if (size)
935 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
936 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
937}
938
Joerg Roedelc99afa22011-11-21 18:19:25 +0100939static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
940 int status, int tag, bool gn)
941{
942 memset(cmd, 0, sizeof(*cmd));
943
944 cmd->data[0] = devid;
945 if (gn) {
946 cmd->data[1] = pasid & PASID_MASK;
947 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
948 }
949 cmd->data[3] = tag & 0x1ff;
950 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
951
952 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
953}
954
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200955static void build_inv_all(struct iommu_cmd *cmd)
956{
957 memset(cmd, 0, sizeof(*cmd));
958 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200959}
960
Joerg Roedel7ef27982012-06-21 16:46:04 +0200961static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
962{
963 memset(cmd, 0, sizeof(*cmd));
964 cmd->data[0] = devid;
965 CMD_SET_TYPE(cmd, CMD_INV_IRT);
966}
967
Joerg Roedel431b2a22008-07-11 17:14:22 +0200968/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200969 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200970 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200971 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200972static int iommu_queue_command_sync(struct amd_iommu *iommu,
973 struct iommu_cmd *cmd,
974 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200975{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200976 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200977 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200978
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200979 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100980
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200981again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200982 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200983
984 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
985 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
986 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
987 left = (head - next_tail) % iommu->cmd_buf_size;
988
989 if (left <= 2) {
990 struct iommu_cmd sync_cmd;
991 volatile u64 sem = 0;
992 int ret;
993
994 build_completion_wait(&sync_cmd, (u64)&sem);
995 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
996
997 spin_unlock_irqrestore(&iommu->lock, flags);
998
999 if ((ret = wait_on_sem(&sem)) != 0)
1000 return ret;
1001
1002 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001003 }
1004
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001005 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001006
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001007 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001008 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001009
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001010 spin_unlock_irqrestore(&iommu->lock, flags);
1011
Joerg Roedel815b33f2011-04-06 17:26:49 +02001012 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001013}
1014
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001015static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1016{
1017 return iommu_queue_command_sync(iommu, cmd, true);
1018}
1019
Joerg Roedel8d201962008-12-02 20:34:41 +01001020/*
1021 * This function queues a completion wait command into the command
1022 * buffer of an IOMMU
1023 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001024static int iommu_completion_wait(struct amd_iommu *iommu)
1025{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001026 struct iommu_cmd cmd;
1027 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001028 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001029
1030 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001031 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001032
Joerg Roedel815b33f2011-04-06 17:26:49 +02001033 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001034
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001035 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001036 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001037 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001038
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001039 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001040}
1041
Joerg Roedeld8c13082011-04-06 18:51:26 +02001042static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001043{
1044 struct iommu_cmd cmd;
1045
Joerg Roedeld8c13082011-04-06 18:51:26 +02001046 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001047
Joerg Roedeld8c13082011-04-06 18:51:26 +02001048 return iommu_queue_command(iommu, &cmd);
1049}
1050
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001051static void iommu_flush_dte_all(struct amd_iommu *iommu)
1052{
1053 u32 devid;
1054
1055 for (devid = 0; devid <= 0xffff; ++devid)
1056 iommu_flush_dte(iommu, devid);
1057
1058 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001059}
1060
1061/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001062 * This function uses heavy locking and may disable irqs for some time. But
1063 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001064 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001065static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001066{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001067 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001068
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001069 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1070 struct iommu_cmd cmd;
1071 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1072 dom_id, 1);
1073 iommu_queue_command(iommu, &cmd);
1074 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001075
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001076 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001077}
1078
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001079static void iommu_flush_all(struct amd_iommu *iommu)
1080{
1081 struct iommu_cmd cmd;
1082
1083 build_inv_all(&cmd);
1084
1085 iommu_queue_command(iommu, &cmd);
1086 iommu_completion_wait(iommu);
1087}
1088
Joerg Roedel7ef27982012-06-21 16:46:04 +02001089static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1090{
1091 struct iommu_cmd cmd;
1092
1093 build_inv_irt(&cmd, devid);
1094
1095 iommu_queue_command(iommu, &cmd);
1096}
1097
1098static void iommu_flush_irt_all(struct amd_iommu *iommu)
1099{
1100 u32 devid;
1101
1102 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1103 iommu_flush_irt(iommu, devid);
1104
1105 iommu_completion_wait(iommu);
1106}
1107
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001108void iommu_flush_all_caches(struct amd_iommu *iommu)
1109{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001110 if (iommu_feature(iommu, FEATURE_IA)) {
1111 iommu_flush_all(iommu);
1112 } else {
1113 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001114 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001115 iommu_flush_tlb_all(iommu);
1116 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001117}
1118
Joerg Roedel431b2a22008-07-11 17:14:22 +02001119/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001120 * Command send function for flushing on-device TLB
1121 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001122static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1123 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001124{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001125 struct amd_iommu *iommu;
1126 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001127 int qdep;
1128
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001129 qdep = dev_data->ats.qdep;
1130 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001131
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001132 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001133
1134 return iommu_queue_command(iommu, &cmd);
1135}
1136
1137/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001138 * Command send function for invalidating a device table entry
1139 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001140static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001141{
1142 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001143 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001144
Joerg Roedel6c542042011-06-09 17:07:31 +02001145 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001146
Joerg Roedelf62dda62011-06-09 12:55:35 +02001147 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001148 if (ret)
1149 return ret;
1150
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001151 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001152 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001153
1154 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001155}
1156
Joerg Roedel431b2a22008-07-11 17:14:22 +02001157/*
1158 * TLB invalidation function which is called from the mapping functions.
1159 * It invalidates a single PTE if the range to flush is within a single
1160 * page. Otherwise it flushes the whole TLB of the IOMMU.
1161 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001162static void __domain_flush_pages(struct protection_domain *domain,
1163 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001164{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001165 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001166 struct iommu_cmd cmd;
1167 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001168
Joerg Roedel11b64022011-04-06 11:49:28 +02001169 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001170
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001171 for (i = 0; i < amd_iommus_present; ++i) {
1172 if (!domain->dev_iommu[i])
1173 continue;
1174
1175 /*
1176 * Devices of this domain are behind this IOMMU
1177 * We need a TLB flush
1178 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001179 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001180 }
1181
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001182 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001183
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001184 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001185 continue;
1186
Joerg Roedel6c542042011-06-09 17:07:31 +02001187 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001188 }
1189
Joerg Roedel11b64022011-04-06 11:49:28 +02001190 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001191}
1192
Joerg Roedel17b124b2011-04-06 18:01:35 +02001193static void domain_flush_pages(struct protection_domain *domain,
1194 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001195{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001196 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001197}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001198
Joerg Roedel1c655772008-09-04 18:40:05 +02001199/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001200static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001201{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001202 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001203}
1204
Chris Wright42a49f92009-06-15 15:42:00 +02001205/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001206static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001207{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001208 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1209}
1210
1211static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001212{
1213 int i;
1214
1215 for (i = 0; i < amd_iommus_present; ++i) {
1216 if (!domain->dev_iommu[i])
1217 continue;
1218
1219 /*
1220 * Devices of this domain are behind this IOMMU
1221 * We need to wait for completion of all commands.
1222 */
1223 iommu_completion_wait(amd_iommus[i]);
1224 }
1225}
1226
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001227
Joerg Roedel43f49602008-12-02 21:01:12 +01001228/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001229 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001230 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001231static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001232{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001233 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001234
1235 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001236 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001237}
1238
Joerg Roedel431b2a22008-07-11 17:14:22 +02001239/****************************************************************************
1240 *
1241 * The functions below are used the create the page table mappings for
1242 * unity mapped regions.
1243 *
1244 ****************************************************************************/
1245
1246/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001247 * This function is used to add another level to an IO page table. Adding
1248 * another level increases the size of the address space by 9 bits to a size up
1249 * to 64 bits.
1250 */
1251static bool increase_address_space(struct protection_domain *domain,
1252 gfp_t gfp)
1253{
1254 u64 *pte;
1255
1256 if (domain->mode == PAGE_MODE_6_LEVEL)
1257 /* address space already 64 bit large */
1258 return false;
1259
1260 pte = (void *)get_zeroed_page(gfp);
1261 if (!pte)
1262 return false;
1263
1264 *pte = PM_LEVEL_PDE(domain->mode,
1265 virt_to_phys(domain->pt_root));
1266 domain->pt_root = pte;
1267 domain->mode += 1;
1268 domain->updated = true;
1269
1270 return true;
1271}
1272
1273static u64 *alloc_pte(struct protection_domain *domain,
1274 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001275 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001276 u64 **pte_page,
1277 gfp_t gfp)
1278{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001279 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001280 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001281
1282 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001283
1284 while (address > PM_LEVEL_SIZE(domain->mode))
1285 increase_address_space(domain, gfp);
1286
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001287 level = domain->mode - 1;
1288 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1289 address = PAGE_SIZE_ALIGN(address, page_size);
1290 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001291
1292 while (level > end_lvl) {
1293 if (!IOMMU_PTE_PRESENT(*pte)) {
1294 page = (u64 *)get_zeroed_page(gfp);
1295 if (!page)
1296 return NULL;
1297 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1298 }
1299
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001300 /* No level skipping support yet */
1301 if (PM_PTE_LEVEL(*pte) != level)
1302 return NULL;
1303
Joerg Roedel308973d2009-11-24 17:43:32 +01001304 level -= 1;
1305
1306 pte = IOMMU_PTE_PAGE(*pte);
1307
1308 if (pte_page && level == end_lvl)
1309 *pte_page = pte;
1310
1311 pte = &pte[PM_LEVEL_INDEX(level, address)];
1312 }
1313
1314 return pte;
1315}
1316
1317/*
1318 * This function checks if there is a PTE for a given dma address. If
1319 * there is one, it returns the pointer to it.
1320 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001321static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001322{
1323 int level;
1324 u64 *pte;
1325
Joerg Roedel24cd7722010-01-19 17:27:39 +01001326 if (address > PM_LEVEL_SIZE(domain->mode))
1327 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001328
Joerg Roedel24cd7722010-01-19 17:27:39 +01001329 level = domain->mode - 1;
1330 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1331
1332 while (level > 0) {
1333
1334 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001335 if (!IOMMU_PTE_PRESENT(*pte))
1336 return NULL;
1337
Joerg Roedel24cd7722010-01-19 17:27:39 +01001338 /* Large PTE */
1339 if (PM_PTE_LEVEL(*pte) == 0x07) {
1340 unsigned long pte_mask, __pte;
1341
1342 /*
1343 * If we have a series of large PTEs, make
1344 * sure to return a pointer to the first one.
1345 */
1346 pte_mask = PTE_PAGE_SIZE(*pte);
1347 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1348 __pte = ((unsigned long)pte) & pte_mask;
1349
1350 return (u64 *)__pte;
1351 }
1352
1353 /* No level skipping support yet */
1354 if (PM_PTE_LEVEL(*pte) != level)
1355 return NULL;
1356
Joerg Roedel308973d2009-11-24 17:43:32 +01001357 level -= 1;
1358
Joerg Roedel24cd7722010-01-19 17:27:39 +01001359 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001360 pte = IOMMU_PTE_PAGE(*pte);
1361 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001362 }
1363
1364 return pte;
1365}
1366
1367/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001368 * Generic mapping functions. It maps a physical address into a DMA
1369 * address space. It allocates the page table pages if necessary.
1370 * In the future it can be extended to a generic mapping function
1371 * supporting all features of AMD IOMMU page tables like level skipping
1372 * and full 64 bit address spaces.
1373 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001374static int iommu_map_page(struct protection_domain *dom,
1375 unsigned long bus_addr,
1376 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001377 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001378 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001379{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001380 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001381 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001382
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001383 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001384 return -EINVAL;
1385
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001386 bus_addr = PAGE_ALIGN(bus_addr);
1387 phys_addr = PAGE_ALIGN(phys_addr);
1388 count = PAGE_SIZE_PTE_COUNT(page_size);
1389 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001390
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001391 for (i = 0; i < count; ++i)
1392 if (IOMMU_PTE_PRESENT(pte[i]))
1393 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001394
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001395 if (page_size > PAGE_SIZE) {
1396 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1397 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1398 } else
1399 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1400
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001401 if (prot & IOMMU_PROT_IR)
1402 __pte |= IOMMU_PTE_IR;
1403 if (prot & IOMMU_PROT_IW)
1404 __pte |= IOMMU_PTE_IW;
1405
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001406 for (i = 0; i < count; ++i)
1407 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001408
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001409 update_domain(dom);
1410
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001411 return 0;
1412}
1413
Joerg Roedel24cd7722010-01-19 17:27:39 +01001414static unsigned long iommu_unmap_page(struct protection_domain *dom,
1415 unsigned long bus_addr,
1416 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001417{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001418 unsigned long long unmap_size, unmapped;
1419 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001420
Joerg Roedel24cd7722010-01-19 17:27:39 +01001421 BUG_ON(!is_power_of_2(page_size));
1422
1423 unmapped = 0;
1424
1425 while (unmapped < page_size) {
1426
1427 pte = fetch_pte(dom, bus_addr);
1428
1429 if (!pte) {
1430 /*
1431 * No PTE for this address
1432 * move forward in 4kb steps
1433 */
1434 unmap_size = PAGE_SIZE;
1435 } else if (PM_PTE_LEVEL(*pte) == 0) {
1436 /* 4kb PTE found for this address */
1437 unmap_size = PAGE_SIZE;
1438 *pte = 0ULL;
1439 } else {
1440 int count, i;
1441
1442 /* Large PTE found which maps this address */
1443 unmap_size = PTE_PAGE_SIZE(*pte);
1444 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1445 for (i = 0; i < count; i++)
1446 pte[i] = 0ULL;
1447 }
1448
1449 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1450 unmapped += unmap_size;
1451 }
1452
1453 BUG_ON(!is_power_of_2(unmapped));
1454
1455 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001456}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001457
Joerg Roedel431b2a22008-07-11 17:14:22 +02001458/*
1459 * This function checks if a specific unity mapping entry is needed for
1460 * this specific IOMMU.
1461 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001462static int iommu_for_unity_map(struct amd_iommu *iommu,
1463 struct unity_map_entry *entry)
1464{
1465 u16 bdf, i;
1466
1467 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1468 bdf = amd_iommu_alias_table[i];
1469 if (amd_iommu_rlookup_table[bdf] == iommu)
1470 return 1;
1471 }
1472
1473 return 0;
1474}
1475
Joerg Roedel431b2a22008-07-11 17:14:22 +02001476/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001477 * This function actually applies the mapping to the page table of the
1478 * dma_ops domain.
1479 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001480static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1481 struct unity_map_entry *e)
1482{
1483 u64 addr;
1484 int ret;
1485
1486 for (addr = e->address_start; addr < e->address_end;
1487 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001488 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001489 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001490 if (ret)
1491 return ret;
1492 /*
1493 * if unity mapping is in aperture range mark the page
1494 * as allocated in the aperture
1495 */
1496 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001497 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001498 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001499 }
1500
1501 return 0;
1502}
1503
Joerg Roedel431b2a22008-07-11 17:14:22 +02001504/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001505 * Init the unity mappings for a specific IOMMU in the system
1506 *
1507 * Basically iterates over all unity mapping entries and applies them to
1508 * the default domain DMA of that IOMMU if necessary.
1509 */
1510static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1511{
1512 struct unity_map_entry *entry;
1513 int ret;
1514
1515 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1516 if (!iommu_for_unity_map(iommu, entry))
1517 continue;
1518 ret = dma_ops_unity_map(iommu->default_dom, entry);
1519 if (ret)
1520 return ret;
1521 }
1522
1523 return 0;
1524}
1525
1526/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001527 * Inits the unity mappings required for a specific device
1528 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001529static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1530 u16 devid)
1531{
1532 struct unity_map_entry *e;
1533 int ret;
1534
1535 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1536 if (!(devid >= e->devid_start && devid <= e->devid_end))
1537 continue;
1538 ret = dma_ops_unity_map(dma_dom, e);
1539 if (ret)
1540 return ret;
1541 }
1542
1543 return 0;
1544}
1545
Joerg Roedel431b2a22008-07-11 17:14:22 +02001546/****************************************************************************
1547 *
1548 * The next functions belong to the address allocator for the dma_ops
1549 * interface functions. They work like the allocators in the other IOMMU
1550 * drivers. Its basically a bitmap which marks the allocated pages in
1551 * the aperture. Maybe it could be enhanced in the future to a more
1552 * efficient allocator.
1553 *
1554 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001555
Joerg Roedel431b2a22008-07-11 17:14:22 +02001556/*
Joerg Roedel384de722009-05-15 12:30:05 +02001557 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001558 *
1559 * called with domain->lock held
1560 */
Joerg Roedel384de722009-05-15 12:30:05 +02001561
Joerg Roedel9cabe892009-05-18 16:38:55 +02001562/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001563 * Used to reserve address ranges in the aperture (e.g. for exclusion
1564 * ranges.
1565 */
1566static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1567 unsigned long start_page,
1568 unsigned int pages)
1569{
1570 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1571
1572 if (start_page + pages > last_page)
1573 pages = last_page - start_page;
1574
1575 for (i = start_page; i < start_page + pages; ++i) {
1576 int index = i / APERTURE_RANGE_PAGES;
1577 int page = i % APERTURE_RANGE_PAGES;
1578 __set_bit(page, dom->aperture[index]->bitmap);
1579 }
1580}
1581
1582/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001583 * This function is used to add a new aperture range to an existing
1584 * aperture in case of dma_ops domain allocation or address allocation
1585 * failure.
1586 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001587static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001588 bool populate, gfp_t gfp)
1589{
1590 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001591 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001592 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001593
Joerg Roedelf5e97052009-05-22 12:31:53 +02001594#ifdef CONFIG_IOMMU_STRESS
1595 populate = false;
1596#endif
1597
Joerg Roedel9cabe892009-05-18 16:38:55 +02001598 if (index >= APERTURE_MAX_RANGES)
1599 return -ENOMEM;
1600
1601 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1602 if (!dma_dom->aperture[index])
1603 return -ENOMEM;
1604
1605 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1606 if (!dma_dom->aperture[index]->bitmap)
1607 goto out_free;
1608
1609 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1610
1611 if (populate) {
1612 unsigned long address = dma_dom->aperture_size;
1613 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1614 u64 *pte, *pte_page;
1615
1616 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001617 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001618 &pte_page, gfp);
1619 if (!pte)
1620 goto out_free;
1621
1622 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1623
1624 address += APERTURE_RANGE_SIZE / 64;
1625 }
1626 }
1627
Joerg Roedel17f5b562011-07-06 17:14:44 +02001628 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001629 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1630
Joerg Roedel17f5b562011-07-06 17:14:44 +02001631 /* Reserve address range used for MSI messages */
1632 if (old_size < MSI_ADDR_BASE_LO &&
1633 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1634 unsigned long spage;
1635 int pages;
1636
1637 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1638 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1639
1640 dma_ops_reserve_addresses(dma_dom, spage, pages);
1641 }
1642
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001643 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001644 for_each_iommu(iommu) {
1645 if (iommu->exclusion_start &&
1646 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1647 && iommu->exclusion_start < dma_dom->aperture_size) {
1648 unsigned long startpage;
1649 int pages = iommu_num_pages(iommu->exclusion_start,
1650 iommu->exclusion_length,
1651 PAGE_SIZE);
1652 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1653 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1654 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001655 }
1656
1657 /*
1658 * Check for areas already mapped as present in the new aperture
1659 * range and mark those pages as reserved in the allocator. Such
1660 * mappings may already exist as a result of requested unity
1661 * mappings for devices.
1662 */
1663 for (i = dma_dom->aperture[index]->offset;
1664 i < dma_dom->aperture_size;
1665 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001666 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001667 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1668 continue;
1669
Joerg Roedelfcd08612011-10-11 17:41:32 +02001670 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001671 }
1672
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001673 update_domain(&dma_dom->domain);
1674
Joerg Roedel9cabe892009-05-18 16:38:55 +02001675 return 0;
1676
1677out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001678 update_domain(&dma_dom->domain);
1679
Joerg Roedel9cabe892009-05-18 16:38:55 +02001680 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1681
1682 kfree(dma_dom->aperture[index]);
1683 dma_dom->aperture[index] = NULL;
1684
1685 return -ENOMEM;
1686}
1687
Joerg Roedel384de722009-05-15 12:30:05 +02001688static unsigned long dma_ops_area_alloc(struct device *dev,
1689 struct dma_ops_domain *dom,
1690 unsigned int pages,
1691 unsigned long align_mask,
1692 u64 dma_mask,
1693 unsigned long start)
1694{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001695 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001696 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1697 int i = start >> APERTURE_RANGE_SHIFT;
1698 unsigned long boundary_size;
1699 unsigned long address = -1;
1700 unsigned long limit;
1701
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001702 next_bit >>= PAGE_SHIFT;
1703
Joerg Roedel384de722009-05-15 12:30:05 +02001704 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1705 PAGE_SIZE) >> PAGE_SHIFT;
1706
1707 for (;i < max_index; ++i) {
1708 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1709
1710 if (dom->aperture[i]->offset >= dma_mask)
1711 break;
1712
1713 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1714 dma_mask >> PAGE_SHIFT);
1715
1716 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1717 limit, next_bit, pages, 0,
1718 boundary_size, align_mask);
1719 if (address != -1) {
1720 address = dom->aperture[i]->offset +
1721 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001722 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001723 break;
1724 }
1725
1726 next_bit = 0;
1727 }
1728
1729 return address;
1730}
1731
Joerg Roedeld3086442008-06-26 21:27:57 +02001732static unsigned long dma_ops_alloc_addresses(struct device *dev,
1733 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001734 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001735 unsigned long align_mask,
1736 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001737{
Joerg Roedeld3086442008-06-26 21:27:57 +02001738 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001739
Joerg Roedelfe16f082009-05-22 12:27:53 +02001740#ifdef CONFIG_IOMMU_STRESS
1741 dom->next_address = 0;
1742 dom->need_flush = true;
1743#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001744
Joerg Roedel384de722009-05-15 12:30:05 +02001745 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001746 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001747
Joerg Roedel1c655772008-09-04 18:40:05 +02001748 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001749 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001750 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1751 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001752 dom->need_flush = true;
1753 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001754
Joerg Roedel384de722009-05-15 12:30:05 +02001755 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001756 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001757
1758 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1759
1760 return address;
1761}
1762
Joerg Roedel431b2a22008-07-11 17:14:22 +02001763/*
1764 * The address free function.
1765 *
1766 * called with domain->lock held
1767 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001768static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1769 unsigned long address,
1770 unsigned int pages)
1771{
Joerg Roedel384de722009-05-15 12:30:05 +02001772 unsigned i = address >> APERTURE_RANGE_SHIFT;
1773 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001774
Joerg Roedel384de722009-05-15 12:30:05 +02001775 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1776
Joerg Roedel47bccd62009-05-22 12:40:54 +02001777#ifdef CONFIG_IOMMU_STRESS
1778 if (i < 4)
1779 return;
1780#endif
1781
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001782 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001783 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001784
1785 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001786
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001787 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001788
Joerg Roedeld3086442008-06-26 21:27:57 +02001789}
1790
Joerg Roedel431b2a22008-07-11 17:14:22 +02001791/****************************************************************************
1792 *
1793 * The next functions belong to the domain allocation. A domain is
1794 * allocated for every IOMMU as the default domain. If device isolation
1795 * is enabled, every device get its own domain. The most important thing
1796 * about domains is the page table mapping the DMA address space they
1797 * contain.
1798 *
1799 ****************************************************************************/
1800
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001801/*
1802 * This function adds a protection domain to the global protection domain list
1803 */
1804static void add_domain_to_list(struct protection_domain *domain)
1805{
1806 unsigned long flags;
1807
1808 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1809 list_add(&domain->list, &amd_iommu_pd_list);
1810 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1811}
1812
1813/*
1814 * This function removes a protection domain to the global
1815 * protection domain list
1816 */
1817static void del_domain_from_list(struct protection_domain *domain)
1818{
1819 unsigned long flags;
1820
1821 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1822 list_del(&domain->list);
1823 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1824}
1825
Joerg Roedelec487d12008-06-26 21:27:58 +02001826static u16 domain_id_alloc(void)
1827{
1828 unsigned long flags;
1829 int id;
1830
1831 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1832 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1833 BUG_ON(id == 0);
1834 if (id > 0 && id < MAX_DOMAIN_ID)
1835 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1836 else
1837 id = 0;
1838 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1839
1840 return id;
1841}
1842
Joerg Roedela2acfb72008-12-02 18:28:53 +01001843static void domain_id_free(int id)
1844{
1845 unsigned long flags;
1846
1847 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1848 if (id > 0 && id < MAX_DOMAIN_ID)
1849 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1850 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1851}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001852
Joerg Roedel86db2e52008-12-02 18:20:21 +01001853static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001854{
1855 int i, j;
1856 u64 *p1, *p2, *p3;
1857
Joerg Roedel86db2e52008-12-02 18:20:21 +01001858 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001859
1860 if (!p1)
1861 return;
1862
1863 for (i = 0; i < 512; ++i) {
1864 if (!IOMMU_PTE_PRESENT(p1[i]))
1865 continue;
1866
1867 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001868 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001869 if (!IOMMU_PTE_PRESENT(p2[j]))
1870 continue;
1871 p3 = IOMMU_PTE_PAGE(p2[j]);
1872 free_page((unsigned long)p3);
1873 }
1874
1875 free_page((unsigned long)p2);
1876 }
1877
1878 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001879
1880 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001881}
1882
Joerg Roedelb16137b2011-11-21 16:50:23 +01001883static void free_gcr3_tbl_level1(u64 *tbl)
1884{
1885 u64 *ptr;
1886 int i;
1887
1888 for (i = 0; i < 512; ++i) {
1889 if (!(tbl[i] & GCR3_VALID))
1890 continue;
1891
1892 ptr = __va(tbl[i] & PAGE_MASK);
1893
1894 free_page((unsigned long)ptr);
1895 }
1896}
1897
1898static void free_gcr3_tbl_level2(u64 *tbl)
1899{
1900 u64 *ptr;
1901 int i;
1902
1903 for (i = 0; i < 512; ++i) {
1904 if (!(tbl[i] & GCR3_VALID))
1905 continue;
1906
1907 ptr = __va(tbl[i] & PAGE_MASK);
1908
1909 free_gcr3_tbl_level1(ptr);
1910 }
1911}
1912
Joerg Roedel52815b72011-11-17 17:24:28 +01001913static void free_gcr3_table(struct protection_domain *domain)
1914{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001915 if (domain->glx == 2)
1916 free_gcr3_tbl_level2(domain->gcr3_tbl);
1917 else if (domain->glx == 1)
1918 free_gcr3_tbl_level1(domain->gcr3_tbl);
1919 else if (domain->glx != 0)
1920 BUG();
1921
Joerg Roedel52815b72011-11-17 17:24:28 +01001922 free_page((unsigned long)domain->gcr3_tbl);
1923}
1924
Joerg Roedel431b2a22008-07-11 17:14:22 +02001925/*
1926 * Free a domain, only used if something went wrong in the
1927 * allocation path and we need to free an already allocated page table
1928 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001929static void dma_ops_domain_free(struct dma_ops_domain *dom)
1930{
Joerg Roedel384de722009-05-15 12:30:05 +02001931 int i;
1932
Joerg Roedelec487d12008-06-26 21:27:58 +02001933 if (!dom)
1934 return;
1935
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001936 del_domain_from_list(&dom->domain);
1937
Joerg Roedel86db2e52008-12-02 18:20:21 +01001938 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001939
Joerg Roedel384de722009-05-15 12:30:05 +02001940 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1941 if (!dom->aperture[i])
1942 continue;
1943 free_page((unsigned long)dom->aperture[i]->bitmap);
1944 kfree(dom->aperture[i]);
1945 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001946
1947 kfree(dom);
1948}
1949
Joerg Roedel431b2a22008-07-11 17:14:22 +02001950/*
1951 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001952 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001953 * structures required for the dma_ops interface
1954 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001955static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001956{
1957 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001958
1959 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1960 if (!dma_dom)
1961 return NULL;
1962
1963 spin_lock_init(&dma_dom->domain.lock);
1964
1965 dma_dom->domain.id = domain_id_alloc();
1966 if (dma_dom->domain.id == 0)
1967 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001968 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001969 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001970 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001971 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001972 dma_dom->domain.priv = dma_dom;
1973 if (!dma_dom->domain.pt_root)
1974 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001975
Joerg Roedel1c655772008-09-04 18:40:05 +02001976 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001977 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001978
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001979 add_domain_to_list(&dma_dom->domain);
1980
Joerg Roedel576175c2009-11-23 19:08:46 +01001981 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001982 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001983
Joerg Roedel431b2a22008-07-11 17:14:22 +02001984 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001985 * mark the first page as allocated so we never return 0 as
1986 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001987 */
Joerg Roedel384de722009-05-15 12:30:05 +02001988 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001989 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001990
Joerg Roedelec487d12008-06-26 21:27:58 +02001991
1992 return dma_dom;
1993
1994free_dma_dom:
1995 dma_ops_domain_free(dma_dom);
1996
1997 return NULL;
1998}
1999
Joerg Roedel431b2a22008-07-11 17:14:22 +02002000/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002001 * little helper function to check whether a given protection domain is a
2002 * dma_ops domain
2003 */
2004static bool dma_ops_domain(struct protection_domain *domain)
2005{
2006 return domain->flags & PD_DMA_OPS_MASK;
2007}
2008
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002009static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002010{
Joerg Roedel132bd682011-11-17 14:18:46 +01002011 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002012 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002013
Joerg Roedel132bd682011-11-17 14:18:46 +01002014 if (domain->mode != PAGE_MODE_NONE)
2015 pte_root = virt_to_phys(domain->pt_root);
2016
Joerg Roedel38ddf412008-09-11 10:38:32 +02002017 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2018 << DEV_ENTRY_MODE_SHIFT;
2019 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002020
Joerg Roedelee6c2862011-11-09 12:06:03 +01002021 flags = amd_iommu_dev_table[devid].data[1];
2022
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002023 if (ats)
2024 flags |= DTE_FLAG_IOTLB;
2025
Joerg Roedel52815b72011-11-17 17:24:28 +01002026 if (domain->flags & PD_IOMMUV2_MASK) {
2027 u64 gcr3 = __pa(domain->gcr3_tbl);
2028 u64 glx = domain->glx;
2029 u64 tmp;
2030
2031 pte_root |= DTE_FLAG_GV;
2032 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2033
2034 /* First mask out possible old values for GCR3 table */
2035 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2036 flags &= ~tmp;
2037
2038 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2039 flags &= ~tmp;
2040
2041 /* Encode GCR3 table into DTE */
2042 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2043 pte_root |= tmp;
2044
2045 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2046 flags |= tmp;
2047
2048 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2049 flags |= tmp;
2050 }
2051
Joerg Roedelee6c2862011-11-09 12:06:03 +01002052 flags &= ~(0xffffUL);
2053 flags |= domain->id;
2054
2055 amd_iommu_dev_table[devid].data[1] = flags;
2056 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002057}
2058
Joerg Roedel15898bb2009-11-24 15:39:42 +01002059static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002060{
Joerg Roedel355bf552008-12-08 12:02:41 +01002061 /* remove entry from the device table seen by the hardware */
2062 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2063 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002064
Joerg Roedelc5cca142009-10-09 18:31:20 +02002065 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002066}
2067
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002068static void do_attach(struct iommu_dev_data *dev_data,
2069 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002070{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002071 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002072 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002073
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002074 iommu = amd_iommu_rlookup_table[dev_data->devid];
2075 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002076
2077 /* Update data structures */
2078 dev_data->domain = domain;
2079 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002080 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002081
2082 /* Do reference counting */
2083 domain->dev_iommu[iommu->index] += 1;
2084 domain->dev_cnt += 1;
2085
2086 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002087 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002088}
2089
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002090static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002091{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002092 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002093
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002094 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002095
Joerg Roedelc4596112009-11-20 14:57:32 +01002096 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002097 dev_data->domain->dev_iommu[iommu->index] -= 1;
2098 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002099
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002100 /* Update data structures */
2101 dev_data->domain = NULL;
2102 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002103 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002104
2105 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002106 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002107}
2108
2109/*
2110 * If a device is not yet associated with a domain, this function does
2111 * assigns it visible for the hardware
2112 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002113static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002114 struct protection_domain *domain)
2115{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002116 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002117
Joerg Roedel15898bb2009-11-24 15:39:42 +01002118 /* lock domain */
2119 spin_lock(&domain->lock);
2120
Joerg Roedel71f77582011-06-09 19:03:15 +02002121 if (dev_data->alias_data != NULL) {
2122 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002123
Joerg Roedel2b02b092011-06-09 17:48:39 +02002124 /* Some sanity checks */
2125 ret = -EBUSY;
2126 if (alias_data->domain != NULL &&
2127 alias_data->domain != domain)
2128 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002129
Joerg Roedel2b02b092011-06-09 17:48:39 +02002130 if (dev_data->domain != NULL &&
2131 dev_data->domain != domain)
2132 goto out_unlock;
2133
2134 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002135 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002136 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002137
2138 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002139 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002140
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002141 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002142 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002143
Joerg Roedel24100052009-11-25 15:59:57 +01002144 atomic_inc(&dev_data->bind);
2145
Julia Lawall84fe6c12010-05-27 12:31:51 +02002146 ret = 0;
2147
2148out_unlock:
2149
Joerg Roedel355bf552008-12-08 12:02:41 +01002150 /* ready */
2151 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002152
Julia Lawall84fe6c12010-05-27 12:31:51 +02002153 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002154}
2155
Joerg Roedel52815b72011-11-17 17:24:28 +01002156
2157static void pdev_iommuv2_disable(struct pci_dev *pdev)
2158{
2159 pci_disable_ats(pdev);
2160 pci_disable_pri(pdev);
2161 pci_disable_pasid(pdev);
2162}
2163
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002164/* FIXME: Change generic reset-function to do the same */
2165static int pri_reset_while_enabled(struct pci_dev *pdev)
2166{
2167 u16 control;
2168 int pos;
2169
Joerg Roedel46277b72011-12-07 14:34:02 +01002170 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002171 if (!pos)
2172 return -EINVAL;
2173
Joerg Roedel46277b72011-12-07 14:34:02 +01002174 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2175 control |= PCI_PRI_CTRL_RESET;
2176 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002177
2178 return 0;
2179}
2180
Joerg Roedel52815b72011-11-17 17:24:28 +01002181static int pdev_iommuv2_enable(struct pci_dev *pdev)
2182{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002183 bool reset_enable;
2184 int reqs, ret;
2185
2186 /* FIXME: Hardcode number of outstanding requests for now */
2187 reqs = 32;
2188 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2189 reqs = 1;
2190 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002191
2192 /* Only allow access to user-accessible pages */
2193 ret = pci_enable_pasid(pdev, 0);
2194 if (ret)
2195 goto out_err;
2196
2197 /* First reset the PRI state of the device */
2198 ret = pci_reset_pri(pdev);
2199 if (ret)
2200 goto out_err;
2201
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002202 /* Enable PRI */
2203 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002204 if (ret)
2205 goto out_err;
2206
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002207 if (reset_enable) {
2208 ret = pri_reset_while_enabled(pdev);
2209 if (ret)
2210 goto out_err;
2211 }
2212
Joerg Roedel52815b72011-11-17 17:24:28 +01002213 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2214 if (ret)
2215 goto out_err;
2216
2217 return 0;
2218
2219out_err:
2220 pci_disable_pri(pdev);
2221 pci_disable_pasid(pdev);
2222
2223 return ret;
2224}
2225
Joerg Roedelc99afa22011-11-21 18:19:25 +01002226/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002227#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002228
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002229static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002230{
Joerg Roedela3b93122012-04-12 12:49:26 +02002231 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002232 int pos;
2233
Joerg Roedel46277b72011-12-07 14:34:02 +01002234 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002235 if (!pos)
2236 return false;
2237
Joerg Roedela3b93122012-04-12 12:49:26 +02002238 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002239
Joerg Roedela3b93122012-04-12 12:49:26 +02002240 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002241}
2242
Joerg Roedel15898bb2009-11-24 15:39:42 +01002243/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002244 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002245 * assigns it visible for the hardware
2246 */
2247static int attach_device(struct device *dev,
2248 struct protection_domain *domain)
2249{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002250 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002251 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002252 unsigned long flags;
2253 int ret;
2254
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002255 dev_data = get_dev_data(dev);
2256
Joerg Roedel52815b72011-11-17 17:24:28 +01002257 if (domain->flags & PD_IOMMUV2_MASK) {
2258 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2259 return -EINVAL;
2260
2261 if (pdev_iommuv2_enable(pdev) != 0)
2262 return -EINVAL;
2263
2264 dev_data->ats.enabled = true;
2265 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002266 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002267 } else if (amd_iommu_iotlb_sup &&
2268 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002269 dev_data->ats.enabled = true;
2270 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2271 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002272
Joerg Roedel15898bb2009-11-24 15:39:42 +01002273 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002274 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002275 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2276
2277 /*
2278 * We might boot into a crash-kernel here. The crashed kernel
2279 * left the caches in the IOMMU dirty. So we have to flush
2280 * here to evict all dirty stuff.
2281 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002282 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002283
2284 return ret;
2285}
2286
2287/*
2288 * Removes a device from a protection domain (unlocked)
2289 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002290static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002291{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002292 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002293 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002294
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002295 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002296
Joerg Roedel2ca76272010-01-22 16:45:31 +01002297 domain = dev_data->domain;
2298
2299 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002300
Joerg Roedel71f77582011-06-09 19:03:15 +02002301 if (dev_data->alias_data != NULL) {
2302 struct iommu_dev_data *alias_data = dev_data->alias_data;
2303
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002304 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002305 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002306 }
2307
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002308 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002309 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002310
Joerg Roedel2ca76272010-01-22 16:45:31 +01002311 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002312
Joerg Roedel21129f72009-09-01 11:59:42 +02002313 /*
2314 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002315 * passthrough domain if it is detached from any other domain.
2316 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002317 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002318 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002319 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002320 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002321}
2322
2323/*
2324 * Removes a device from a protection domain (with devtable_lock held)
2325 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002326static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002327{
Joerg Roedel52815b72011-11-17 17:24:28 +01002328 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002329 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002330 unsigned long flags;
2331
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002332 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002333 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002334
Joerg Roedel355bf552008-12-08 12:02:41 +01002335 /* lock device table */
2336 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002337 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002338 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002339
Joerg Roedel52815b72011-11-17 17:24:28 +01002340 if (domain->flags & PD_IOMMUV2_MASK)
2341 pdev_iommuv2_disable(to_pci_dev(dev));
2342 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002343 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002344
2345 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002346}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002347
Joerg Roedel15898bb2009-11-24 15:39:42 +01002348/*
2349 * Find out the protection domain structure for a given PCI device. This
2350 * will give us the pointer to the page table root for example.
2351 */
2352static struct protection_domain *domain_for_device(struct device *dev)
2353{
Joerg Roedel71f77582011-06-09 19:03:15 +02002354 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002355 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002356 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002357
Joerg Roedel657cbb62009-11-23 15:26:46 +01002358 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002359
Joerg Roedel2b02b092011-06-09 17:48:39 +02002360 if (dev_data->domain)
2361 return dev_data->domain;
2362
Joerg Roedel71f77582011-06-09 19:03:15 +02002363 if (dev_data->alias_data != NULL) {
2364 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002365
2366 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2367 if (alias_data->domain != NULL) {
2368 __attach_device(dev_data, alias_data->domain);
2369 dom = alias_data->domain;
2370 }
2371 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002372 }
2373
Joerg Roedel15898bb2009-11-24 15:39:42 +01002374 return dom;
2375}
2376
Joerg Roedele275a2a2008-12-10 18:27:25 +01002377static int device_change_notifier(struct notifier_block *nb,
2378 unsigned long action, void *data)
2379{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002380 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002381 struct protection_domain *domain;
2382 struct iommu_dev_data *dev_data;
2383 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002384 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002385 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002386 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002387
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002388 if (!check_device(dev))
2389 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002390
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002391 devid = get_device_id(dev);
2392 iommu = amd_iommu_rlookup_table[devid];
2393 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002394
2395 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002396 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002397
2398 domain = domain_for_device(dev);
2399
Joerg Roedele275a2a2008-12-10 18:27:25 +01002400 if (!domain)
2401 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002402 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002403 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002404 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002405 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002406 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002407
2408 iommu_init_device(dev);
2409
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002410 /*
2411 * dev_data is still NULL and
2412 * got initialized in iommu_init_device
2413 */
2414 dev_data = get_dev_data(dev);
2415
2416 if (iommu_pass_through || dev_data->iommu_v2) {
2417 dev_data->passthrough = true;
2418 attach_device(dev, pt_domain);
2419 break;
2420 }
2421
Joerg Roedel657cbb62009-11-23 15:26:46 +01002422 domain = domain_for_device(dev);
2423
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002424 /* allocate a protection domain if a device is added */
2425 dma_domain = find_protection_domain(devid);
2426 if (dma_domain)
2427 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002428 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002429 if (!dma_domain)
2430 goto out;
2431 dma_domain->target_dev = devid;
2432
2433 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2434 list_add_tail(&dma_domain->list, &iommu_pd_list);
2435 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2436
Joerg Roedelac1534a2012-06-21 14:52:40 +02002437 dev_data = get_dev_data(dev);
2438
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002439 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002440
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002441 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002442 case BUS_NOTIFY_DEL_DEVICE:
2443
2444 iommu_uninit_device(dev);
2445
Joerg Roedele275a2a2008-12-10 18:27:25 +01002446 default:
2447 goto out;
2448 }
2449
Joerg Roedele275a2a2008-12-10 18:27:25 +01002450 iommu_completion_wait(iommu);
2451
2452out:
2453 return 0;
2454}
2455
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302456static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002457 .notifier_call = device_change_notifier,
2458};
Joerg Roedel355bf552008-12-08 12:02:41 +01002459
Joerg Roedel8638c492009-12-10 11:12:25 +01002460void amd_iommu_init_notifier(void)
2461{
2462 bus_register_notifier(&pci_bus_type, &device_nb);
2463}
2464
Joerg Roedel431b2a22008-07-11 17:14:22 +02002465/*****************************************************************************
2466 *
2467 * The next functions belong to the dma_ops mapping/unmapping code.
2468 *
2469 *****************************************************************************/
2470
2471/*
2472 * In the dma_ops path we only have the struct device. This function
2473 * finds the corresponding IOMMU, the protection domain and the
2474 * requestor id for a given device.
2475 * If the device is not yet associated with a domain this is also done
2476 * in this function.
2477 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002478static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002479{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002480 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002481 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002482 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002483
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002484 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002485 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002486
Joerg Roedel94f6d192009-11-24 16:40:02 +01002487 domain = domain_for_device(dev);
2488 if (domain != NULL && !dma_ops_domain(domain))
2489 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002490
Joerg Roedel94f6d192009-11-24 16:40:02 +01002491 if (domain != NULL)
2492 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002493
Frank Arnolddf805ab2012-08-27 19:21:04 +02002494 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002495 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002496 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002497 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2498 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002499 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002500 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002501
Joerg Roedel94f6d192009-11-24 16:40:02 +01002502 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002503}
2504
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002505static void update_device_table(struct protection_domain *domain)
2506{
Joerg Roedel492667d2009-11-27 13:25:47 +01002507 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002508
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002509 list_for_each_entry(dev_data, &domain->dev_list, list)
2510 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002511}
2512
2513static void update_domain(struct protection_domain *domain)
2514{
2515 if (!domain->updated)
2516 return;
2517
2518 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002519
2520 domain_flush_devices(domain);
2521 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002522
2523 domain->updated = false;
2524}
2525
Joerg Roedel431b2a22008-07-11 17:14:22 +02002526/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002527 * This function fetches the PTE for a given address in the aperture
2528 */
2529static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2530 unsigned long address)
2531{
Joerg Roedel384de722009-05-15 12:30:05 +02002532 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002533 u64 *pte, *pte_page;
2534
Joerg Roedel384de722009-05-15 12:30:05 +02002535 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2536 if (!aperture)
2537 return NULL;
2538
2539 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002540 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002541 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002542 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002543 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2544 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002545 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002546
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002547 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002548
2549 return pte;
2550}
2551
2552/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002553 * This is the generic map function. It maps one 4kb page at paddr to
2554 * the given address in the DMA address space for the domain.
2555 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002556static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002557 unsigned long address,
2558 phys_addr_t paddr,
2559 int direction)
2560{
2561 u64 *pte, __pte;
2562
2563 WARN_ON(address > dom->aperture_size);
2564
2565 paddr &= PAGE_MASK;
2566
Joerg Roedel8bda3092009-05-12 12:02:46 +02002567 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002568 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002569 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002570
2571 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2572
2573 if (direction == DMA_TO_DEVICE)
2574 __pte |= IOMMU_PTE_IR;
2575 else if (direction == DMA_FROM_DEVICE)
2576 __pte |= IOMMU_PTE_IW;
2577 else if (direction == DMA_BIDIRECTIONAL)
2578 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2579
2580 WARN_ON(*pte);
2581
2582 *pte = __pte;
2583
2584 return (dma_addr_t)address;
2585}
2586
Joerg Roedel431b2a22008-07-11 17:14:22 +02002587/*
2588 * The generic unmapping function for on page in the DMA address space.
2589 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002590static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002591 unsigned long address)
2592{
Joerg Roedel384de722009-05-15 12:30:05 +02002593 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002594 u64 *pte;
2595
2596 if (address >= dom->aperture_size)
2597 return;
2598
Joerg Roedel384de722009-05-15 12:30:05 +02002599 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2600 if (!aperture)
2601 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002602
Joerg Roedel384de722009-05-15 12:30:05 +02002603 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2604 if (!pte)
2605 return;
2606
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002607 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002608
2609 WARN_ON(!*pte);
2610
2611 *pte = 0ULL;
2612}
2613
Joerg Roedel431b2a22008-07-11 17:14:22 +02002614/*
2615 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002616 * contiguous memory region into DMA address space. It is used by all
2617 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002618 * Must be called with the domain lock held.
2619 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002620static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002621 struct dma_ops_domain *dma_dom,
2622 phys_addr_t paddr,
2623 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002624 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002625 bool align,
2626 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002627{
2628 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002629 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002630 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002631 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002632 int i;
2633
Joerg Roedele3c449f2008-10-15 22:02:11 -07002634 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002635 paddr &= PAGE_MASK;
2636
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002637 INC_STATS_COUNTER(total_map_requests);
2638
Joerg Roedelc1858972008-12-12 15:42:39 +01002639 if (pages > 1)
2640 INC_STATS_COUNTER(cross_page);
2641
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002642 if (align)
2643 align_mask = (1UL << get_order(size)) - 1;
2644
Joerg Roedel11b83882009-05-19 10:23:15 +02002645retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002646 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2647 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002648 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002649 /*
2650 * setting next_address here will let the address
2651 * allocator only scan the new allocated range in the
2652 * first run. This is a small optimization.
2653 */
2654 dma_dom->next_address = dma_dom->aperture_size;
2655
Joerg Roedel576175c2009-11-23 19:08:46 +01002656 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002657 goto out;
2658
2659 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002660 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002661 * allocation again
2662 */
2663 goto retry;
2664 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002665
2666 start = address;
2667 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002668 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002669 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002670 goto out_unmap;
2671
Joerg Roedelcb76c322008-06-26 21:28:00 +02002672 paddr += PAGE_SIZE;
2673 start += PAGE_SIZE;
2674 }
2675 address += offset;
2676
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002677 ADD_STATS_COUNTER(alloced_io_mem, size);
2678
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002679 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002680 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002681 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002682 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002683 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002684
Joerg Roedelcb76c322008-06-26 21:28:00 +02002685out:
2686 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002687
2688out_unmap:
2689
2690 for (--i; i >= 0; --i) {
2691 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002692 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002693 }
2694
2695 dma_ops_free_addresses(dma_dom, address, pages);
2696
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002697 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002698}
2699
Joerg Roedel431b2a22008-07-11 17:14:22 +02002700/*
2701 * Does the reverse of the __map_single function. Must be called with
2702 * the domain lock held too
2703 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002704static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002705 dma_addr_t dma_addr,
2706 size_t size,
2707 int dir)
2708{
Joerg Roedel04e04632010-09-23 16:12:48 +02002709 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002710 dma_addr_t i, start;
2711 unsigned int pages;
2712
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002713 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002714 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002715 return;
2716
Joerg Roedel04e04632010-09-23 16:12:48 +02002717 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002718 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002719 dma_addr &= PAGE_MASK;
2720 start = dma_addr;
2721
2722 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002723 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002724 start += PAGE_SIZE;
2725 }
2726
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002727 SUB_STATS_COUNTER(alloced_io_mem, size);
2728
Joerg Roedelcb76c322008-06-26 21:28:00 +02002729 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002730
Joerg Roedel80be3082008-11-06 14:59:05 +01002731 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002732 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002733 dma_dom->need_flush = false;
2734 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002735}
2736
Joerg Roedel431b2a22008-07-11 17:14:22 +02002737/*
2738 * The exported map_single function for dma_ops.
2739 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002740static dma_addr_t map_page(struct device *dev, struct page *page,
2741 unsigned long offset, size_t size,
2742 enum dma_data_direction dir,
2743 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002744{
2745 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002746 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002747 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002748 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002749 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002750
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002751 INC_STATS_COUNTER(cnt_map_single);
2752
Joerg Roedel94f6d192009-11-24 16:40:02 +01002753 domain = get_domain(dev);
2754 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002755 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002756 else if (IS_ERR(domain))
2757 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002758
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002759 dma_mask = *dev->dma_mask;
2760
Joerg Roedel4da70b92008-06-26 21:28:01 +02002761 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002762
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002763 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002764 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002765 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002766 goto out;
2767
Joerg Roedel17b124b2011-04-06 18:01:35 +02002768 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002769
2770out:
2771 spin_unlock_irqrestore(&domain->lock, flags);
2772
2773 return addr;
2774}
2775
Joerg Roedel431b2a22008-07-11 17:14:22 +02002776/*
2777 * The exported unmap_single function for dma_ops.
2778 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002779static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2780 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002781{
2782 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002783 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002784
Joerg Roedel146a6912008-12-12 15:07:12 +01002785 INC_STATS_COUNTER(cnt_unmap_single);
2786
Joerg Roedel94f6d192009-11-24 16:40:02 +01002787 domain = get_domain(dev);
2788 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002789 return;
2790
Joerg Roedel4da70b92008-06-26 21:28:01 +02002791 spin_lock_irqsave(&domain->lock, flags);
2792
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002793 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002794
Joerg Roedel17b124b2011-04-06 18:01:35 +02002795 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002796
2797 spin_unlock_irqrestore(&domain->lock, flags);
2798}
2799
Joerg Roedel431b2a22008-07-11 17:14:22 +02002800/*
2801 * This is a special map_sg function which is used if we should map a
2802 * device which is not handled by an AMD IOMMU in the system.
2803 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002804static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2805 int nelems, int dir)
2806{
2807 struct scatterlist *s;
2808 int i;
2809
2810 for_each_sg(sglist, s, nelems, i) {
2811 s->dma_address = (dma_addr_t)sg_phys(s);
2812 s->dma_length = s->length;
2813 }
2814
2815 return nelems;
2816}
2817
Joerg Roedel431b2a22008-07-11 17:14:22 +02002818/*
2819 * The exported map_sg function for dma_ops (handles scatter-gather
2820 * lists).
2821 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002822static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002823 int nelems, enum dma_data_direction dir,
2824 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002825{
2826 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002827 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002828 int i;
2829 struct scatterlist *s;
2830 phys_addr_t paddr;
2831 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002832 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002833
Joerg Roedeld03f0672008-12-12 15:09:48 +01002834 INC_STATS_COUNTER(cnt_map_sg);
2835
Joerg Roedel94f6d192009-11-24 16:40:02 +01002836 domain = get_domain(dev);
2837 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002838 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002839 else if (IS_ERR(domain))
2840 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002841
Joerg Roedel832a90c2008-09-18 15:54:23 +02002842 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002843
Joerg Roedel65b050a2008-06-26 21:28:02 +02002844 spin_lock_irqsave(&domain->lock, flags);
2845
2846 for_each_sg(sglist, s, nelems, i) {
2847 paddr = sg_phys(s);
2848
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002849 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002850 paddr, s->length, dir, false,
2851 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002852
2853 if (s->dma_address) {
2854 s->dma_length = s->length;
2855 mapped_elems++;
2856 } else
2857 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002858 }
2859
Joerg Roedel17b124b2011-04-06 18:01:35 +02002860 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002861
2862out:
2863 spin_unlock_irqrestore(&domain->lock, flags);
2864
2865 return mapped_elems;
2866unmap:
2867 for_each_sg(sglist, s, mapped_elems, i) {
2868 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002869 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002870 s->dma_length, dir);
2871 s->dma_address = s->dma_length = 0;
2872 }
2873
2874 mapped_elems = 0;
2875
2876 goto out;
2877}
2878
Joerg Roedel431b2a22008-07-11 17:14:22 +02002879/*
2880 * The exported map_sg function for dma_ops (handles scatter-gather
2881 * lists).
2882 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002883static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002884 int nelems, enum dma_data_direction dir,
2885 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002886{
2887 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002888 struct protection_domain *domain;
2889 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002890 int i;
2891
Joerg Roedel55877a62008-12-12 15:12:14 +01002892 INC_STATS_COUNTER(cnt_unmap_sg);
2893
Joerg Roedel94f6d192009-11-24 16:40:02 +01002894 domain = get_domain(dev);
2895 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002896 return;
2897
Joerg Roedel65b050a2008-06-26 21:28:02 +02002898 spin_lock_irqsave(&domain->lock, flags);
2899
2900 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002901 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002902 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002903 s->dma_address = s->dma_length = 0;
2904 }
2905
Joerg Roedel17b124b2011-04-06 18:01:35 +02002906 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002907
2908 spin_unlock_irqrestore(&domain->lock, flags);
2909}
2910
Joerg Roedel431b2a22008-07-11 17:14:22 +02002911/*
2912 * The exported alloc_coherent function for dma_ops.
2913 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002914static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002915 dma_addr_t *dma_addr, gfp_t flag,
2916 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002917{
2918 unsigned long flags;
2919 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002920 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002921 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002922 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002923
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002924 INC_STATS_COUNTER(cnt_alloc_coherent);
2925
Joerg Roedel94f6d192009-11-24 16:40:02 +01002926 domain = get_domain(dev);
2927 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002928 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2929 *dma_addr = __pa(virt_addr);
2930 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002931 } else if (IS_ERR(domain))
2932 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002933
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002934 dma_mask = dev->coherent_dma_mask;
2935 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2936 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002937
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002938 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2939 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302940 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002941
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002942 paddr = virt_to_phys(virt_addr);
2943
Joerg Roedel832a90c2008-09-18 15:54:23 +02002944 if (!dma_mask)
2945 dma_mask = *dev->dma_mask;
2946
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002947 spin_lock_irqsave(&domain->lock, flags);
2948
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002949 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002950 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002951
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002952 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002953 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002954 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002955 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002956
Joerg Roedel17b124b2011-04-06 18:01:35 +02002957 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002958
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002959 spin_unlock_irqrestore(&domain->lock, flags);
2960
2961 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002962
2963out_free:
2964
2965 free_pages((unsigned long)virt_addr, get_order(size));
2966
2967 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002968}
2969
Joerg Roedel431b2a22008-07-11 17:14:22 +02002970/*
2971 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002972 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002973static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002974 void *virt_addr, dma_addr_t dma_addr,
2975 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002976{
2977 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002978 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002979
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002980 INC_STATS_COUNTER(cnt_free_coherent);
2981
Joerg Roedel94f6d192009-11-24 16:40:02 +01002982 domain = get_domain(dev);
2983 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002984 goto free_mem;
2985
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002986 spin_lock_irqsave(&domain->lock, flags);
2987
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002988 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002989
Joerg Roedel17b124b2011-04-06 18:01:35 +02002990 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002991
2992 spin_unlock_irqrestore(&domain->lock, flags);
2993
2994free_mem:
2995 free_pages((unsigned long)virt_addr, get_order(size));
2996}
2997
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002998/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002999 * This function is called by the DMA layer to find out if we can handle a
3000 * particular device. It is part of the dma_ops.
3001 */
3002static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3003{
Joerg Roedel420aef82009-11-23 16:14:57 +01003004 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003005}
3006
3007/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003008 * The function for pre-allocating protection domains.
3009 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003010 * If the driver core informs the DMA layer if a driver grabs a device
3011 * we don't need to preallocate the protection domains anymore.
3012 * For now we have to.
3013 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003014static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003015{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003016 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003017 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003018 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003019 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003020
Chris Wrightd18c69d2010-04-02 18:27:55 -07003021 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003022
3023 /* Do we handle this device? */
3024 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003025 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003026
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003027 dev_data = get_dev_data(&dev->dev);
3028 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3029 /* Make sure passthrough domain is allocated */
3030 alloc_passthrough_domain();
3031 dev_data->passthrough = true;
3032 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003033 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003034 dev_name(&dev->dev));
3035 }
3036
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003037 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003038 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003039 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003040
3041 devid = get_device_id(&dev->dev);
3042
Joerg Roedel87a64d52009-11-24 17:26:43 +01003043 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003044 if (!dma_dom)
3045 continue;
3046 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003047 dma_dom->target_dev = devid;
3048
Joerg Roedel15898bb2009-11-24 15:39:42 +01003049 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003050
Joerg Roedelbd60b732008-09-11 10:24:48 +02003051 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003052 }
3053}
3054
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003055static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003056 .alloc = alloc_coherent,
3057 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003058 .map_page = map_page,
3059 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003060 .map_sg = map_sg,
3061 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003062 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003063};
3064
Joerg Roedel27c21272011-05-30 15:56:24 +02003065static unsigned device_dma_ops_init(void)
3066{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003067 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003068 struct pci_dev *pdev = NULL;
3069 unsigned unhandled = 0;
3070
3071 for_each_pci_dev(pdev) {
3072 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003073
3074 iommu_ignore_device(&pdev->dev);
3075
Joerg Roedel27c21272011-05-30 15:56:24 +02003076 unhandled += 1;
3077 continue;
3078 }
3079
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003080 dev_data = get_dev_data(&pdev->dev);
3081
3082 if (!dev_data->passthrough)
3083 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3084 else
3085 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003086 }
3087
3088 return unhandled;
3089}
3090
Joerg Roedel431b2a22008-07-11 17:14:22 +02003091/*
3092 * The function which clues the AMD IOMMU driver into dma_ops.
3093 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003094
3095void __init amd_iommu_init_api(void)
3096{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003097 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003098}
3099
Joerg Roedel6631ee92008-06-26 21:28:05 +02003100int __init amd_iommu_init_dma_ops(void)
3101{
3102 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003103 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003104
Joerg Roedel431b2a22008-07-11 17:14:22 +02003105 /*
3106 * first allocate a default protection domain for every IOMMU we
3107 * found in the system. Devices not assigned to any other
3108 * protection domain will be assigned to the default one.
3109 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003110 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003111 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003112 if (iommu->default_dom == NULL)
3113 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003114 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003115 ret = iommu_init_unity_mappings(iommu);
3116 if (ret)
3117 goto free_domains;
3118 }
3119
Joerg Roedel431b2a22008-07-11 17:14:22 +02003120 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003121 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003122 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003123 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003124
3125 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003126 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003127
Joerg Roedel431b2a22008-07-11 17:14:22 +02003128 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003129 unhandled = device_dma_ops_init();
3130 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3131 /* There are unhandled devices - initialize swiotlb for them */
3132 swiotlb = 1;
3133 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003134
Joerg Roedel7f265082008-12-12 13:50:21 +01003135 amd_iommu_stats_init();
3136
Joerg Roedel62410ee2012-06-12 16:42:43 +02003137 if (amd_iommu_unmap_flush)
3138 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3139 else
3140 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3141
Joerg Roedel6631ee92008-06-26 21:28:05 +02003142 return 0;
3143
3144free_domains:
3145
Joerg Roedel3bd22172009-05-04 15:06:20 +02003146 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003147 if (iommu->default_dom)
3148 dma_ops_domain_free(iommu->default_dom);
3149 }
3150
3151 return ret;
3152}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003153
3154/*****************************************************************************
3155 *
3156 * The following functions belong to the exported interface of AMD IOMMU
3157 *
3158 * This interface allows access to lower level functions of the IOMMU
3159 * like protection domain handling and assignement of devices to domains
3160 * which is not possible with the dma_ops interface.
3161 *
3162 *****************************************************************************/
3163
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003164static void cleanup_domain(struct protection_domain *domain)
3165{
Joerg Roedel492667d2009-11-27 13:25:47 +01003166 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003167 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003168
3169 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3170
Joerg Roedel492667d2009-11-27 13:25:47 +01003171 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003172 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003173 atomic_set(&dev_data->bind, 0);
3174 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003175
3176 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3177}
3178
Joerg Roedel26508152009-08-26 16:52:40 +02003179static void protection_domain_free(struct protection_domain *domain)
3180{
3181 if (!domain)
3182 return;
3183
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003184 del_domain_from_list(domain);
3185
Joerg Roedel26508152009-08-26 16:52:40 +02003186 if (domain->id)
3187 domain_id_free(domain->id);
3188
3189 kfree(domain);
3190}
3191
3192static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003193{
3194 struct protection_domain *domain;
3195
3196 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3197 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003198 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003199
3200 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003201 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003202 domain->id = domain_id_alloc();
3203 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003204 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003205 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003206
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003207 add_domain_to_list(domain);
3208
Joerg Roedel26508152009-08-26 16:52:40 +02003209 return domain;
3210
3211out_err:
3212 kfree(domain);
3213
3214 return NULL;
3215}
3216
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003217static int __init alloc_passthrough_domain(void)
3218{
3219 if (pt_domain != NULL)
3220 return 0;
3221
3222 /* allocate passthrough domain */
3223 pt_domain = protection_domain_alloc();
3224 if (!pt_domain)
3225 return -ENOMEM;
3226
3227 pt_domain->mode = PAGE_MODE_NONE;
3228
3229 return 0;
3230}
Joerg Roedel26508152009-08-26 16:52:40 +02003231static int amd_iommu_domain_init(struct iommu_domain *dom)
3232{
3233 struct protection_domain *domain;
3234
3235 domain = protection_domain_alloc();
3236 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003237 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003238
3239 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003240 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3241 if (!domain->pt_root)
3242 goto out_free;
3243
Joerg Roedelf3572db2011-11-23 12:36:25 +01003244 domain->iommu_domain = dom;
3245
Joerg Roedelc156e342008-12-02 18:13:27 +01003246 dom->priv = domain;
3247
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003248 dom->geometry.aperture_start = 0;
3249 dom->geometry.aperture_end = ~0ULL;
3250 dom->geometry.force_aperture = true;
3251
Joerg Roedelc156e342008-12-02 18:13:27 +01003252 return 0;
3253
3254out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003255 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003256
3257 return -ENOMEM;
3258}
3259
Joerg Roedel98383fc2008-12-02 18:34:12 +01003260static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3261{
3262 struct protection_domain *domain = dom->priv;
3263
3264 if (!domain)
3265 return;
3266
3267 if (domain->dev_cnt > 0)
3268 cleanup_domain(domain);
3269
3270 BUG_ON(domain->dev_cnt != 0);
3271
Joerg Roedel132bd682011-11-17 14:18:46 +01003272 if (domain->mode != PAGE_MODE_NONE)
3273 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003274
Joerg Roedel52815b72011-11-17 17:24:28 +01003275 if (domain->flags & PD_IOMMUV2_MASK)
3276 free_gcr3_table(domain);
3277
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003278 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003279
3280 dom->priv = NULL;
3281}
3282
Joerg Roedel684f2882008-12-08 12:07:44 +01003283static void amd_iommu_detach_device(struct iommu_domain *dom,
3284 struct device *dev)
3285{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003286 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003287 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003288 u16 devid;
3289
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003290 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003291 return;
3292
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003293 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003294
Joerg Roedel657cbb62009-11-23 15:26:46 +01003295 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003296 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003297
3298 iommu = amd_iommu_rlookup_table[devid];
3299 if (!iommu)
3300 return;
3301
Joerg Roedel684f2882008-12-08 12:07:44 +01003302 iommu_completion_wait(iommu);
3303}
3304
Joerg Roedel01106062008-12-02 19:34:11 +01003305static int amd_iommu_attach_device(struct iommu_domain *dom,
3306 struct device *dev)
3307{
3308 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003309 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003310 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003311 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003312
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003313 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003314 return -EINVAL;
3315
Joerg Roedel657cbb62009-11-23 15:26:46 +01003316 dev_data = dev->archdata.iommu;
3317
Joerg Roedelf62dda62011-06-09 12:55:35 +02003318 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003319 if (!iommu)
3320 return -EINVAL;
3321
Joerg Roedel657cbb62009-11-23 15:26:46 +01003322 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003323 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003324
Joerg Roedel15898bb2009-11-24 15:39:42 +01003325 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003326
3327 iommu_completion_wait(iommu);
3328
Joerg Roedel15898bb2009-11-24 15:39:42 +01003329 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003330}
3331
Joerg Roedel468e2362010-01-21 16:37:36 +01003332static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003333 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003334{
3335 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003336 int prot = 0;
3337 int ret;
3338
Joerg Roedel132bd682011-11-17 14:18:46 +01003339 if (domain->mode == PAGE_MODE_NONE)
3340 return -EINVAL;
3341
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003342 if (iommu_prot & IOMMU_READ)
3343 prot |= IOMMU_PROT_IR;
3344 if (iommu_prot & IOMMU_WRITE)
3345 prot |= IOMMU_PROT_IW;
3346
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003347 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003348 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003349 mutex_unlock(&domain->api_lock);
3350
Joerg Roedel795e74f72010-05-11 17:40:57 +02003351 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003352}
3353
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003354static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3355 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003356{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003357 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003358 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003359
Joerg Roedel132bd682011-11-17 14:18:46 +01003360 if (domain->mode == PAGE_MODE_NONE)
3361 return -EINVAL;
3362
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003363 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003364 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003365 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003366
Joerg Roedel17b124b2011-04-06 18:01:35 +02003367 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003368
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003369 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003370}
3371
Joerg Roedel645c4c82008-12-02 20:05:50 +01003372static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3373 unsigned long iova)
3374{
3375 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003376 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003377 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003378 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003379
Joerg Roedel132bd682011-11-17 14:18:46 +01003380 if (domain->mode == PAGE_MODE_NONE)
3381 return iova;
3382
Joerg Roedel24cd7722010-01-19 17:27:39 +01003383 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003384
Joerg Roedela6d41a42009-09-02 17:08:55 +02003385 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003386 return 0;
3387
Joerg Roedelf03152b2010-01-21 16:15:24 +01003388 if (PM_PTE_LEVEL(*pte) == 0)
3389 offset_mask = PAGE_SIZE - 1;
3390 else
3391 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3392
3393 __pte = *pte & PM_ADDR_MASK;
3394 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003395
3396 return paddr;
3397}
3398
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003399static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3400 unsigned long cap)
3401{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003402 switch (cap) {
3403 case IOMMU_CAP_CACHE_COHERENCY:
3404 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003405 case IOMMU_CAP_INTR_REMAP:
3406 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003407 }
3408
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003409 return 0;
3410}
3411
Joerg Roedel26961ef2008-12-03 17:00:17 +01003412static struct iommu_ops amd_iommu_ops = {
3413 .domain_init = amd_iommu_domain_init,
3414 .domain_destroy = amd_iommu_domain_destroy,
3415 .attach_dev = amd_iommu_attach_device,
3416 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003417 .map = amd_iommu_map,
3418 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003419 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003420 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003421 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003422};
3423
Joerg Roedel0feae532009-08-26 15:26:30 +02003424/*****************************************************************************
3425 *
3426 * The next functions do a basic initialization of IOMMU for pass through
3427 * mode
3428 *
3429 * In passthrough mode the IOMMU is initialized and enabled but not used for
3430 * DMA-API translation.
3431 *
3432 *****************************************************************************/
3433
3434int __init amd_iommu_init_passthrough(void)
3435{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003436 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003437 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003438 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003439 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003440 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003441
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003442 ret = alloc_passthrough_domain();
3443 if (ret)
3444 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003445
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003446 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003447 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003448 continue;
3449
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003450 dev_data = get_dev_data(&dev->dev);
3451 dev_data->passthrough = true;
3452
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003453 devid = get_device_id(&dev->dev);
3454
Joerg Roedel15898bb2009-11-24 15:39:42 +01003455 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003456 if (!iommu)
3457 continue;
3458
Joerg Roedel15898bb2009-11-24 15:39:42 +01003459 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003460 }
3461
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003462 amd_iommu_stats_init();
3463
Joerg Roedel0feae532009-08-26 15:26:30 +02003464 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3465
3466 return 0;
3467}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003468
3469/* IOMMUv2 specific functions */
3470int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3471{
3472 return atomic_notifier_chain_register(&ppr_notifier, nb);
3473}
3474EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3475
3476int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3477{
3478 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3479}
3480EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003481
3482void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3483{
3484 struct protection_domain *domain = dom->priv;
3485 unsigned long flags;
3486
3487 spin_lock_irqsave(&domain->lock, flags);
3488
3489 /* Update data structure */
3490 domain->mode = PAGE_MODE_NONE;
3491 domain->updated = true;
3492
3493 /* Make changes visible to IOMMUs */
3494 update_domain(domain);
3495
3496 /* Page-table is not visible to IOMMU anymore, so free it */
3497 free_pagetable(domain);
3498
3499 spin_unlock_irqrestore(&domain->lock, flags);
3500}
3501EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003502
3503int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3504{
3505 struct protection_domain *domain = dom->priv;
3506 unsigned long flags;
3507 int levels, ret;
3508
3509 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3510 return -EINVAL;
3511
3512 /* Number of GCR3 table levels required */
3513 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3514 levels += 1;
3515
3516 if (levels > amd_iommu_max_glx_val)
3517 return -EINVAL;
3518
3519 spin_lock_irqsave(&domain->lock, flags);
3520
3521 /*
3522 * Save us all sanity checks whether devices already in the
3523 * domain support IOMMUv2. Just force that the domain has no
3524 * devices attached when it is switched into IOMMUv2 mode.
3525 */
3526 ret = -EBUSY;
3527 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3528 goto out;
3529
3530 ret = -ENOMEM;
3531 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3532 if (domain->gcr3_tbl == NULL)
3533 goto out;
3534
3535 domain->glx = levels;
3536 domain->flags |= PD_IOMMUV2_MASK;
3537 domain->updated = true;
3538
3539 update_domain(domain);
3540
3541 ret = 0;
3542
3543out:
3544 spin_unlock_irqrestore(&domain->lock, flags);
3545
3546 return ret;
3547}
3548EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003549
3550static int __flush_pasid(struct protection_domain *domain, int pasid,
3551 u64 address, bool size)
3552{
3553 struct iommu_dev_data *dev_data;
3554 struct iommu_cmd cmd;
3555 int i, ret;
3556
3557 if (!(domain->flags & PD_IOMMUV2_MASK))
3558 return -EINVAL;
3559
3560 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3561
3562 /*
3563 * IOMMU TLB needs to be flushed before Device TLB to
3564 * prevent device TLB refill from IOMMU TLB
3565 */
3566 for (i = 0; i < amd_iommus_present; ++i) {
3567 if (domain->dev_iommu[i] == 0)
3568 continue;
3569
3570 ret = iommu_queue_command(amd_iommus[i], &cmd);
3571 if (ret != 0)
3572 goto out;
3573 }
3574
3575 /* Wait until IOMMU TLB flushes are complete */
3576 domain_flush_complete(domain);
3577
3578 /* Now flush device TLBs */
3579 list_for_each_entry(dev_data, &domain->dev_list, list) {
3580 struct amd_iommu *iommu;
3581 int qdep;
3582
3583 BUG_ON(!dev_data->ats.enabled);
3584
3585 qdep = dev_data->ats.qdep;
3586 iommu = amd_iommu_rlookup_table[dev_data->devid];
3587
3588 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3589 qdep, address, size);
3590
3591 ret = iommu_queue_command(iommu, &cmd);
3592 if (ret != 0)
3593 goto out;
3594 }
3595
3596 /* Wait until all device TLBs are flushed */
3597 domain_flush_complete(domain);
3598
3599 ret = 0;
3600
3601out:
3602
3603 return ret;
3604}
3605
3606static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3607 u64 address)
3608{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003609 INC_STATS_COUNTER(invalidate_iotlb);
3610
Joerg Roedel22e266c2011-11-21 15:59:08 +01003611 return __flush_pasid(domain, pasid, address, false);
3612}
3613
3614int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3615 u64 address)
3616{
3617 struct protection_domain *domain = dom->priv;
3618 unsigned long flags;
3619 int ret;
3620
3621 spin_lock_irqsave(&domain->lock, flags);
3622 ret = __amd_iommu_flush_page(domain, pasid, address);
3623 spin_unlock_irqrestore(&domain->lock, flags);
3624
3625 return ret;
3626}
3627EXPORT_SYMBOL(amd_iommu_flush_page);
3628
3629static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3630{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003631 INC_STATS_COUNTER(invalidate_iotlb_all);
3632
Joerg Roedel22e266c2011-11-21 15:59:08 +01003633 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3634 true);
3635}
3636
3637int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3638{
3639 struct protection_domain *domain = dom->priv;
3640 unsigned long flags;
3641 int ret;
3642
3643 spin_lock_irqsave(&domain->lock, flags);
3644 ret = __amd_iommu_flush_tlb(domain, pasid);
3645 spin_unlock_irqrestore(&domain->lock, flags);
3646
3647 return ret;
3648}
3649EXPORT_SYMBOL(amd_iommu_flush_tlb);
3650
Joerg Roedelb16137b2011-11-21 16:50:23 +01003651static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3652{
3653 int index;
3654 u64 *pte;
3655
3656 while (true) {
3657
3658 index = (pasid >> (9 * level)) & 0x1ff;
3659 pte = &root[index];
3660
3661 if (level == 0)
3662 break;
3663
3664 if (!(*pte & GCR3_VALID)) {
3665 if (!alloc)
3666 return NULL;
3667
3668 root = (void *)get_zeroed_page(GFP_ATOMIC);
3669 if (root == NULL)
3670 return NULL;
3671
3672 *pte = __pa(root) | GCR3_VALID;
3673 }
3674
3675 root = __va(*pte & PAGE_MASK);
3676
3677 level -= 1;
3678 }
3679
3680 return pte;
3681}
3682
3683static int __set_gcr3(struct protection_domain *domain, int pasid,
3684 unsigned long cr3)
3685{
3686 u64 *pte;
3687
3688 if (domain->mode != PAGE_MODE_NONE)
3689 return -EINVAL;
3690
3691 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3692 if (pte == NULL)
3693 return -ENOMEM;
3694
3695 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3696
3697 return __amd_iommu_flush_tlb(domain, pasid);
3698}
3699
3700static int __clear_gcr3(struct protection_domain *domain, int pasid)
3701{
3702 u64 *pte;
3703
3704 if (domain->mode != PAGE_MODE_NONE)
3705 return -EINVAL;
3706
3707 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3708 if (pte == NULL)
3709 return 0;
3710
3711 *pte = 0;
3712
3713 return __amd_iommu_flush_tlb(domain, pasid);
3714}
3715
3716int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3717 unsigned long cr3)
3718{
3719 struct protection_domain *domain = dom->priv;
3720 unsigned long flags;
3721 int ret;
3722
3723 spin_lock_irqsave(&domain->lock, flags);
3724 ret = __set_gcr3(domain, pasid, cr3);
3725 spin_unlock_irqrestore(&domain->lock, flags);
3726
3727 return ret;
3728}
3729EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3730
3731int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3732{
3733 struct protection_domain *domain = dom->priv;
3734 unsigned long flags;
3735 int ret;
3736
3737 spin_lock_irqsave(&domain->lock, flags);
3738 ret = __clear_gcr3(domain, pasid);
3739 spin_unlock_irqrestore(&domain->lock, flags);
3740
3741 return ret;
3742}
3743EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003744
3745int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3746 int status, int tag)
3747{
3748 struct iommu_dev_data *dev_data;
3749 struct amd_iommu *iommu;
3750 struct iommu_cmd cmd;
3751
Joerg Roedel399be2f2011-12-01 16:53:47 +01003752 INC_STATS_COUNTER(complete_ppr);
3753
Joerg Roedelc99afa22011-11-21 18:19:25 +01003754 dev_data = get_dev_data(&pdev->dev);
3755 iommu = amd_iommu_rlookup_table[dev_data->devid];
3756
3757 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3758 tag, dev_data->pri_tlp);
3759
3760 return iommu_queue_command(iommu, &cmd);
3761}
3762EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003763
3764struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3765{
3766 struct protection_domain *domain;
3767
3768 domain = get_domain(&pdev->dev);
3769 if (IS_ERR(domain))
3770 return NULL;
3771
3772 /* Only return IOMMUv2 domains */
3773 if (!(domain->flags & PD_IOMMUV2_MASK))
3774 return NULL;
3775
3776 return domain->iommu_domain;
3777}
3778EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003779
3780void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3781{
3782 struct iommu_dev_data *dev_data;
3783
3784 if (!amd_iommu_v2_supported())
3785 return;
3786
3787 dev_data = get_dev_data(&pdev->dev);
3788 dev_data->errata |= (1 << erratum);
3789}
3790EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003791
3792int amd_iommu_device_info(struct pci_dev *pdev,
3793 struct amd_iommu_device_info *info)
3794{
3795 int max_pasids;
3796 int pos;
3797
3798 if (pdev == NULL || info == NULL)
3799 return -EINVAL;
3800
3801 if (!amd_iommu_v2_supported())
3802 return -EINVAL;
3803
3804 memset(info, 0, sizeof(*info));
3805
3806 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3807 if (pos)
3808 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3809
3810 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3811 if (pos)
3812 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3813
3814 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3815 if (pos) {
3816 int features;
3817
3818 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3819 max_pasids = min(max_pasids, (1 << 20));
3820
3821 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3822 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3823
3824 features = pci_pasid_features(pdev);
3825 if (features & PCI_PASID_CAP_EXEC)
3826 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3827 if (features & PCI_PASID_CAP_PRIV)
3828 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3829 }
3830
3831 return 0;
3832}
3833EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003834
3835#ifdef CONFIG_IRQ_REMAP
3836
3837/*****************************************************************************
3838 *
3839 * Interrupt Remapping Implementation
3840 *
3841 *****************************************************************************/
3842
3843union irte {
3844 u32 val;
3845 struct {
3846 u32 valid : 1,
3847 no_fault : 1,
3848 int_type : 3,
3849 rq_eoi : 1,
3850 dm : 1,
3851 rsvd_1 : 1,
3852 destination : 8,
3853 vector : 8,
3854 rsvd_2 : 8;
3855 } fields;
3856};
3857
3858#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3859#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3860#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3861#define DTE_IRQ_REMAP_ENABLE 1ULL
3862
3863static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3864{
3865 u64 dte;
3866
3867 dte = amd_iommu_dev_table[devid].data[2];
3868 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3869 dte |= virt_to_phys(table->table);
3870 dte |= DTE_IRQ_REMAP_INTCTL;
3871 dte |= DTE_IRQ_TABLE_LEN;
3872 dte |= DTE_IRQ_REMAP_ENABLE;
3873
3874 amd_iommu_dev_table[devid].data[2] = dte;
3875}
3876
3877#define IRTE_ALLOCATED (~1U)
3878
3879static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3880{
3881 struct irq_remap_table *table = NULL;
3882 struct amd_iommu *iommu;
3883 unsigned long flags;
3884 u16 alias;
3885
3886 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3887
3888 iommu = amd_iommu_rlookup_table[devid];
3889 if (!iommu)
3890 goto out_unlock;
3891
3892 table = irq_lookup_table[devid];
3893 if (table)
3894 goto out;
3895
3896 alias = amd_iommu_alias_table[devid];
3897 table = irq_lookup_table[alias];
3898 if (table) {
3899 irq_lookup_table[devid] = table;
3900 set_dte_irq_entry(devid, table);
3901 iommu_flush_dte(iommu, devid);
3902 goto out;
3903 }
3904
3905 /* Nothing there yet, allocate new irq remapping table */
3906 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3907 if (!table)
3908 goto out;
3909
3910 if (ioapic)
3911 /* Keep the first 32 indexes free for IOAPIC interrupts */
3912 table->min_index = 32;
3913
3914 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3915 if (!table->table) {
3916 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003917 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003918 goto out;
3919 }
3920
3921 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3922
3923 if (ioapic) {
3924 int i;
3925
3926 for (i = 0; i < 32; ++i)
3927 table->table[i] = IRTE_ALLOCATED;
3928 }
3929
3930 irq_lookup_table[devid] = table;
3931 set_dte_irq_entry(devid, table);
3932 iommu_flush_dte(iommu, devid);
3933 if (devid != alias) {
3934 irq_lookup_table[alias] = table;
3935 set_dte_irq_entry(devid, table);
3936 iommu_flush_dte(iommu, alias);
3937 }
3938
3939out:
3940 iommu_completion_wait(iommu);
3941
3942out_unlock:
3943 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3944
3945 return table;
3946}
3947
3948static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3949{
3950 struct irq_remap_table *table;
3951 unsigned long flags;
3952 int index, c;
3953
3954 table = get_irq_table(devid, false);
3955 if (!table)
3956 return -ENODEV;
3957
3958 spin_lock_irqsave(&table->lock, flags);
3959
3960 /* Scan table for free entries */
3961 for (c = 0, index = table->min_index;
3962 index < MAX_IRQS_PER_TABLE;
3963 ++index) {
3964 if (table->table[index] == 0)
3965 c += 1;
3966 else
3967 c = 0;
3968
3969 if (c == count) {
3970 struct irq_2_iommu *irte_info;
3971
3972 for (; c != 0; --c)
3973 table->table[index - c + 1] = IRTE_ALLOCATED;
3974
3975 index -= count - 1;
3976
3977 irte_info = &cfg->irq_2_iommu;
3978 irte_info->sub_handle = devid;
3979 irte_info->irte_index = index;
3980 irte_info->iommu = (void *)cfg;
3981
3982 goto out;
3983 }
3984 }
3985
3986 index = -ENOSPC;
3987
3988out:
3989 spin_unlock_irqrestore(&table->lock, flags);
3990
3991 return index;
3992}
3993
3994static int get_irte(u16 devid, int index, union irte *irte)
3995{
3996 struct irq_remap_table *table;
3997 unsigned long flags;
3998
3999 table = get_irq_table(devid, false);
4000 if (!table)
4001 return -ENOMEM;
4002
4003 spin_lock_irqsave(&table->lock, flags);
4004 irte->val = table->table[index];
4005 spin_unlock_irqrestore(&table->lock, flags);
4006
4007 return 0;
4008}
4009
4010static int modify_irte(u16 devid, int index, union irte irte)
4011{
4012 struct irq_remap_table *table;
4013 struct amd_iommu *iommu;
4014 unsigned long flags;
4015
4016 iommu = amd_iommu_rlookup_table[devid];
4017 if (iommu == NULL)
4018 return -EINVAL;
4019
4020 table = get_irq_table(devid, false);
4021 if (!table)
4022 return -ENOMEM;
4023
4024 spin_lock_irqsave(&table->lock, flags);
4025 table->table[index] = irte.val;
4026 spin_unlock_irqrestore(&table->lock, flags);
4027
4028 iommu_flush_irt(iommu, devid);
4029 iommu_completion_wait(iommu);
4030
4031 return 0;
4032}
4033
4034static void free_irte(u16 devid, int index)
4035{
4036 struct irq_remap_table *table;
4037 struct amd_iommu *iommu;
4038 unsigned long flags;
4039
4040 iommu = amd_iommu_rlookup_table[devid];
4041 if (iommu == NULL)
4042 return;
4043
4044 table = get_irq_table(devid, false);
4045 if (!table)
4046 return;
4047
4048 spin_lock_irqsave(&table->lock, flags);
4049 table->table[index] = 0;
4050 spin_unlock_irqrestore(&table->lock, flags);
4051
4052 iommu_flush_irt(iommu, devid);
4053 iommu_completion_wait(iommu);
4054}
4055
Joerg Roedel5527de72012-06-26 11:17:32 +02004056static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4057 unsigned int destination, int vector,
4058 struct io_apic_irq_attr *attr)
4059{
4060 struct irq_remap_table *table;
4061 struct irq_2_iommu *irte_info;
4062 struct irq_cfg *cfg;
4063 union irte irte;
4064 int ioapic_id;
4065 int index;
4066 int devid;
4067 int ret;
4068
4069 cfg = irq_get_chip_data(irq);
4070 if (!cfg)
4071 return -EINVAL;
4072
4073 irte_info = &cfg->irq_2_iommu;
4074 ioapic_id = mpc_ioapic_id(attr->ioapic);
4075 devid = get_ioapic_devid(ioapic_id);
4076
4077 if (devid < 0)
4078 return devid;
4079
4080 table = get_irq_table(devid, true);
4081 if (table == NULL)
4082 return -ENOMEM;
4083
4084 index = attr->ioapic_pin;
4085
4086 /* Setup IRQ remapping info */
4087 irte_info->sub_handle = devid;
4088 irte_info->irte_index = index;
4089 irte_info->iommu = (void *)cfg;
4090
4091 /* Setup IRTE for IOMMU */
4092 irte.val = 0;
4093 irte.fields.vector = vector;
4094 irte.fields.int_type = apic->irq_delivery_mode;
4095 irte.fields.destination = destination;
4096 irte.fields.dm = apic->irq_dest_mode;
4097 irte.fields.valid = 1;
4098
4099 ret = modify_irte(devid, index, irte);
4100 if (ret)
4101 return ret;
4102
4103 /* Setup IOAPIC entry */
4104 memset(entry, 0, sizeof(*entry));
4105
4106 entry->vector = index;
4107 entry->mask = 0;
4108 entry->trigger = attr->trigger;
4109 entry->polarity = attr->polarity;
4110
4111 /*
4112 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004113 */
4114 if (attr->trigger)
4115 entry->mask = 1;
4116
4117 return 0;
4118}
4119
4120static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4121 bool force)
4122{
4123 struct irq_2_iommu *irte_info;
4124 unsigned int dest, irq;
4125 struct irq_cfg *cfg;
4126 union irte irte;
4127 int err;
4128
4129 if (!config_enabled(CONFIG_SMP))
4130 return -1;
4131
4132 cfg = data->chip_data;
4133 irq = data->irq;
4134 irte_info = &cfg->irq_2_iommu;
4135
4136 if (!cpumask_intersects(mask, cpu_online_mask))
4137 return -EINVAL;
4138
4139 if (get_irte(irte_info->sub_handle, irte_info->irte_index, &irte))
4140 return -EBUSY;
4141
4142 if (assign_irq_vector(irq, cfg, mask))
4143 return -EBUSY;
4144
4145 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4146 if (err) {
4147 if (assign_irq_vector(irq, cfg, data->affinity))
4148 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4149 return err;
4150 }
4151
4152 irte.fields.vector = cfg->vector;
4153 irte.fields.destination = dest;
4154
4155 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4156
4157 if (cfg->move_in_progress)
4158 send_cleanup_vector(cfg);
4159
4160 cpumask_copy(data->affinity, mask);
4161
4162 return 0;
4163}
4164
4165static int free_irq(int irq)
4166{
4167 struct irq_2_iommu *irte_info;
4168 struct irq_cfg *cfg;
4169
4170 cfg = irq_get_chip_data(irq);
4171 if (!cfg)
4172 return -EINVAL;
4173
4174 irte_info = &cfg->irq_2_iommu;
4175
4176 free_irte(irte_info->sub_handle, irte_info->irte_index);
4177
4178 return 0;
4179}
4180
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004181static void compose_msi_msg(struct pci_dev *pdev,
4182 unsigned int irq, unsigned int dest,
4183 struct msi_msg *msg, u8 hpet_id)
4184{
4185 struct irq_2_iommu *irte_info;
4186 struct irq_cfg *cfg;
4187 union irte irte;
4188
4189 cfg = irq_get_chip_data(irq);
4190 if (!cfg)
4191 return;
4192
4193 irte_info = &cfg->irq_2_iommu;
4194
4195 irte.val = 0;
4196 irte.fields.vector = cfg->vector;
4197 irte.fields.int_type = apic->irq_delivery_mode;
4198 irte.fields.destination = dest;
4199 irte.fields.dm = apic->irq_dest_mode;
4200 irte.fields.valid = 1;
4201
4202 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4203
4204 msg->address_hi = MSI_ADDR_BASE_HI;
4205 msg->address_lo = MSI_ADDR_BASE_LO;
4206 msg->data = irte_info->irte_index;
4207}
4208
4209static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4210{
4211 struct irq_cfg *cfg;
4212 int index;
4213 u16 devid;
4214
4215 if (!pdev)
4216 return -EINVAL;
4217
4218 cfg = irq_get_chip_data(irq);
4219 if (!cfg)
4220 return -EINVAL;
4221
4222 devid = get_device_id(&pdev->dev);
4223 index = alloc_irq_index(cfg, devid, nvec);
4224
4225 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4226}
4227
4228static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4229 int index, int offset)
4230{
4231 struct irq_2_iommu *irte_info;
4232 struct irq_cfg *cfg;
4233 u16 devid;
4234
4235 if (!pdev)
4236 return -EINVAL;
4237
4238 cfg = irq_get_chip_data(irq);
4239 if (!cfg)
4240 return -EINVAL;
4241
4242 if (index >= MAX_IRQS_PER_TABLE)
4243 return 0;
4244
4245 devid = get_device_id(&pdev->dev);
4246 irte_info = &cfg->irq_2_iommu;
4247
4248 irte_info->sub_handle = devid;
4249 irte_info->irte_index = index + offset;
4250 irte_info->iommu = (void *)cfg;
4251
4252 return 0;
4253}
4254
Joerg Roedeld9761952012-06-26 16:00:08 +02004255static int setup_hpet_msi(unsigned int irq, unsigned int id)
4256{
4257 struct irq_2_iommu *irte_info;
4258 struct irq_cfg *cfg;
4259 int index, devid;
4260
4261 cfg = irq_get_chip_data(irq);
4262 if (!cfg)
4263 return -EINVAL;
4264
4265 irte_info = &cfg->irq_2_iommu;
4266 devid = get_hpet_devid(id);
4267 if (devid < 0)
4268 return devid;
4269
4270 index = alloc_irq_index(cfg, devid, 1);
4271 if (index < 0)
4272 return index;
4273
4274 irte_info->sub_handle = devid;
4275 irte_info->irte_index = index;
4276 irte_info->iommu = (void *)cfg;
4277
4278 return 0;
4279}
4280
Joerg Roedel6b474b82012-06-26 16:46:04 +02004281struct irq_remap_ops amd_iommu_irq_ops = {
4282 .supported = amd_iommu_supported,
4283 .prepare = amd_iommu_prepare,
4284 .enable = amd_iommu_enable,
4285 .disable = amd_iommu_disable,
4286 .reenable = amd_iommu_reenable,
4287 .enable_faulting = amd_iommu_enable_faulting,
4288 .setup_ioapic_entry = setup_ioapic_entry,
4289 .set_affinity = set_affinity,
4290 .free_irq = free_irq,
4291 .compose_msi_msg = compose_msi_msg,
4292 .msi_alloc_irq = msi_alloc_irq,
4293 .msi_setup_irq = msi_setup_irq,
4294 .setup_hpet_msi = setup_hpet_msi,
4295};
Joerg Roedel2b324502012-06-21 16:29:10 +02004296#endif