blob: b65b377815ac6b58689889aea4da81b0260c0007 [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
277#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
278
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600279static int init_iommu_group(struct device *dev)
Joerg Roedel657cbb62009-11-23 15:26:46 +0100280{
281 struct iommu_dev_data *dev_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600282 struct iommu_group *group;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600283 struct pci_dev *dma_pdev = NULL;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600284 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100285
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600286 group = iommu_group_get(dev);
287 if (group) {
288 iommu_group_put(group);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100289 return 0;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600290 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100291
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200292 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100293 if (!dev_data)
294 return -ENOMEM;
295
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600296 if (dev_data->alias_data) {
297 u16 alias;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100298
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600299 alias = amd_iommu_alias_table[dev_data->devid];
Alex Williamson9dcd6132012-05-30 14:19:07 -0600300 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
Joerg Roedel0774e392012-09-28 16:14:44 +0200301 }
302
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600303 if (!dma_pdev)
304 dma_pdev = pci_dev_get(to_pci_dev(dev));
Alex Williamson9dcd6132012-05-30 14:19:07 -0600305
Alex Williamson31fe9432012-08-04 12:09:03 -0600306 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600307 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
308
Alex Williamson31fe9432012-08-04 12:09:03 -0600309 /*
310 * If it's a multifunction device that does not support our
311 * required ACS flags, add to the same group as function 0.
312 */
Alex Williamson664b6002012-05-30 14:19:31 -0600313 if (dma_pdev->multifunction &&
314 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
315 swap_pci_ref(&dma_pdev,
316 pci_get_slot(dma_pdev->bus,
317 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
318 0)));
319
Alex Williamson31fe9432012-08-04 12:09:03 -0600320 /*
321 * Devices on the root bus go through the iommu. If that's not us,
322 * find the next upstream device and test ACS up to the root bus.
323 * Finding the next device may require skipping virtual buses.
324 */
Alex Williamson664b6002012-05-30 14:19:31 -0600325 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson31fe9432012-08-04 12:09:03 -0600326 struct pci_bus *bus = dma_pdev->bus;
327
328 while (!bus->self) {
329 if (!pci_is_root_bus(bus))
330 bus = bus->parent;
331 else
332 goto root_bus;
333 }
334
335 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600336 break;
337
Alex Williamson31fe9432012-08-04 12:09:03 -0600338 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200339 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100340
Alex Williamson31fe9432012-08-04 12:09:03 -0600341root_bus:
Alex Williamson9dcd6132012-05-30 14:19:07 -0600342 group = iommu_group_get(&dma_pdev->dev);
343 pci_dev_put(dma_pdev);
344 if (!group) {
345 group = iommu_group_alloc();
346 if (IS_ERR(group))
347 return PTR_ERR(group);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100348 }
349
Alex Williamson9dcd6132012-05-30 14:19:07 -0600350 ret = iommu_group_add_device(group, dev);
351
352 iommu_group_put(group);
353
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600354 return ret;
355}
356
357static int iommu_init_device(struct device *dev)
358{
359 struct pci_dev *pdev = to_pci_dev(dev);
360 struct iommu_dev_data *dev_data;
361 u16 alias;
362 int ret;
363
364 if (dev->archdata.iommu)
365 return 0;
366
367 dev_data = find_dev_data(get_device_id(dev));
368 if (!dev_data)
369 return -ENOMEM;
370
371 alias = amd_iommu_alias_table[dev_data->devid];
372 if (alias != dev_data->devid) {
373 struct iommu_dev_data *alias_data;
374
375 alias_data = find_dev_data(alias);
376 if (alias_data == NULL) {
377 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
378 dev_name(dev));
379 free_dev_data(dev_data);
380 return -ENOTSUPP;
381 }
382 dev_data->alias_data = alias_data;
383 }
384
385 ret = init_iommu_group(dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600386 if (ret)
387 return ret;
388
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100389 if (pci_iommuv2_capable(pdev)) {
390 struct amd_iommu *iommu;
391
392 iommu = amd_iommu_rlookup_table[dev_data->devid];
393 dev_data->iommu_v2 = iommu->is_iommu_v2;
394 }
395
Joerg Roedel657cbb62009-11-23 15:26:46 +0100396 dev->archdata.iommu = dev_data;
397
Joerg Roedel657cbb62009-11-23 15:26:46 +0100398 return 0;
399}
400
Joerg Roedel26018872011-06-06 16:50:14 +0200401static void iommu_ignore_device(struct device *dev)
402{
403 u16 devid, alias;
404
405 devid = get_device_id(dev);
406 alias = amd_iommu_alias_table[devid];
407
408 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
409 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
410
411 amd_iommu_rlookup_table[devid] = NULL;
412 amd_iommu_rlookup_table[alias] = NULL;
413}
414
Joerg Roedel657cbb62009-11-23 15:26:46 +0100415static void iommu_uninit_device(struct device *dev)
416{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600417 iommu_group_remove_device(dev);
418
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200419 /*
420 * Nothing to do here - we keep dev_data around for unplugged devices
421 * and reuse it when the device is re-plugged - not doing so would
422 * introduce a ton of races.
423 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100424}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100425
426void __init amd_iommu_uninit_devices(void)
427{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200428 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100429 struct pci_dev *pdev = NULL;
430
431 for_each_pci_dev(pdev) {
432
433 if (!check_device(&pdev->dev))
434 continue;
435
436 iommu_uninit_device(&pdev->dev);
437 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200438
439 /* Free all of our dev_data structures */
440 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
441 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100442}
443
444int __init amd_iommu_init_devices(void)
445{
446 struct pci_dev *pdev = NULL;
447 int ret = 0;
448
449 for_each_pci_dev(pdev) {
450
451 if (!check_device(&pdev->dev))
452 continue;
453
454 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200455 if (ret == -ENOTSUPP)
456 iommu_ignore_device(&pdev->dev);
457 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100458 goto out_free;
459 }
460
461 return 0;
462
463out_free:
464
465 amd_iommu_uninit_devices();
466
467 return ret;
468}
Joerg Roedel7f265082008-12-12 13:50:21 +0100469#ifdef CONFIG_AMD_IOMMU_STATS
470
471/*
472 * Initialization code for statistics collection
473 */
474
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100475DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100476DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100477DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100478DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100479DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100480DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100481DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100482DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100483DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100484DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100485DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100486DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100487DECLARE_STATS_COUNTER(complete_ppr);
488DECLARE_STATS_COUNTER(invalidate_iotlb);
489DECLARE_STATS_COUNTER(invalidate_iotlb_all);
490DECLARE_STATS_COUNTER(pri_requests);
491
Joerg Roedel7f265082008-12-12 13:50:21 +0100492static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100493static struct dentry *de_fflush;
494
495static void amd_iommu_stats_add(struct __iommu_counter *cnt)
496{
497 if (stats_dir == NULL)
498 return;
499
500 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
501 &cnt->value);
502}
503
504static void amd_iommu_stats_init(void)
505{
506 stats_dir = debugfs_create_dir("amd-iommu", NULL);
507 if (stats_dir == NULL)
508 return;
509
Joerg Roedel7f265082008-12-12 13:50:21 +0100510 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300511 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100512
513 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100514 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100515 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100516 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100517 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100518 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100519 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100520 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100521 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100522 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100523 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100524 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100525 amd_iommu_stats_add(&complete_ppr);
526 amd_iommu_stats_add(&invalidate_iotlb);
527 amd_iommu_stats_add(&invalidate_iotlb_all);
528 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100529}
530
531#endif
532
Joerg Roedel431b2a22008-07-11 17:14:22 +0200533/****************************************************************************
534 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200535 * Interrupt handling functions
536 *
537 ****************************************************************************/
538
Joerg Roedele3e59872009-09-03 14:02:10 +0200539static void dump_dte_entry(u16 devid)
540{
541 int i;
542
Joerg Roedelee6c2862011-11-09 12:06:03 +0100543 for (i = 0; i < 4; ++i)
544 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200545 amd_iommu_dev_table[devid].data[i]);
546}
547
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200548static void dump_command(unsigned long phys_addr)
549{
550 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
551 int i;
552
553 for (i = 0; i < 4; ++i)
554 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
555}
556
Joerg Roedela345b232009-09-03 15:01:43 +0200557static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200558{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200559 int type, devid, domid, flags;
560 volatile u32 *event = __evt;
561 int count = 0;
562 u64 address;
563
564retry:
565 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
566 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
567 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
568 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
569 address = (u64)(((u64)event[3]) << 32) | event[2];
570
571 if (type == 0) {
572 /* Did we hit the erratum? */
573 if (++count == LOOP_TIMEOUT) {
574 pr_err("AMD-Vi: No event written to event log\n");
575 return;
576 }
577 udelay(1);
578 goto retry;
579 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200580
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200581 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200582
583 switch (type) {
584 case EVENT_TYPE_ILL_DEV:
585 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
586 "address=0x%016llx flags=0x%04x]\n",
587 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
588 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200589 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200590 break;
591 case EVENT_TYPE_IO_FAULT:
592 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
593 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
594 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
595 domid, address, flags);
596 break;
597 case EVENT_TYPE_DEV_TAB_ERR:
598 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
599 "address=0x%016llx flags=0x%04x]\n",
600 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
601 address, flags);
602 break;
603 case EVENT_TYPE_PAGE_TAB_ERR:
604 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
605 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
606 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
607 domid, address, flags);
608 break;
609 case EVENT_TYPE_ILL_CMD:
610 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200611 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200612 break;
613 case EVENT_TYPE_CMD_HARD_ERR:
614 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
615 "flags=0x%04x]\n", address, flags);
616 break;
617 case EVENT_TYPE_IOTLB_INV_TO:
618 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
619 "address=0x%016llx]\n",
620 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
621 address);
622 break;
623 case EVENT_TYPE_INV_DEV_REQ:
624 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
625 "address=0x%016llx flags=0x%04x]\n",
626 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
627 address, flags);
628 break;
629 default:
630 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
631 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200632
633 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200634}
635
636static void iommu_poll_events(struct amd_iommu *iommu)
637{
638 u32 head, tail;
639 unsigned long flags;
640
641 spin_lock_irqsave(&iommu->lock, flags);
642
643 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
644 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
645
646 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200647 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200648 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
649 }
650
651 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
652
653 spin_unlock_irqrestore(&iommu->lock, flags);
654}
655
Joerg Roedeleee53532012-06-01 15:20:23 +0200656static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100657{
658 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100659
Joerg Roedel399be2f2011-12-01 16:53:47 +0100660 INC_STATS_COUNTER(pri_requests);
661
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100662 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
663 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
664 return;
665 }
666
667 fault.address = raw[1];
668 fault.pasid = PPR_PASID(raw[0]);
669 fault.device_id = PPR_DEVID(raw[0]);
670 fault.tag = PPR_TAG(raw[0]);
671 fault.flags = PPR_FLAGS(raw[0]);
672
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100673 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
674}
675
676static void iommu_poll_ppr_log(struct amd_iommu *iommu)
677{
678 unsigned long flags;
679 u32 head, tail;
680
681 if (iommu->ppr_log == NULL)
682 return;
683
Joerg Roedeleee53532012-06-01 15:20:23 +0200684 /* enable ppr interrupts again */
685 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
686
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100687 spin_lock_irqsave(&iommu->lock, flags);
688
689 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
690 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
691
692 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200693 volatile u64 *raw;
694 u64 entry[2];
695 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100696
Joerg Roedeleee53532012-06-01 15:20:23 +0200697 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100698
Joerg Roedeleee53532012-06-01 15:20:23 +0200699 /*
700 * Hardware bug: Interrupt may arrive before the entry is
701 * written to memory. If this happens we need to wait for the
702 * entry to arrive.
703 */
704 for (i = 0; i < LOOP_TIMEOUT; ++i) {
705 if (PPR_REQ_TYPE(raw[0]) != 0)
706 break;
707 udelay(1);
708 }
709
710 /* Avoid memcpy function-call overhead */
711 entry[0] = raw[0];
712 entry[1] = raw[1];
713
714 /*
715 * To detect the hardware bug we need to clear the entry
716 * back to zero.
717 */
718 raw[0] = raw[1] = 0UL;
719
720 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100721 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
722 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200723
724 /*
725 * Release iommu->lock because ppr-handling might need to
Frank Arnolddf805ab2012-08-27 19:21:04 +0200726 * re-acquire it
Joerg Roedeleee53532012-06-01 15:20:23 +0200727 */
728 spin_unlock_irqrestore(&iommu->lock, flags);
729
730 /* Handle PPR entry */
731 iommu_handle_ppr_entry(iommu, entry);
732
733 spin_lock_irqsave(&iommu->lock, flags);
734
735 /* Refresh ring-buffer information */
736 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100737 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
738 }
739
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100740 spin_unlock_irqrestore(&iommu->lock, flags);
741}
742
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200743irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200744{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200745 struct amd_iommu *iommu;
746
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100747 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200748 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100749 iommu_poll_ppr_log(iommu);
750 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200751
752 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200753}
754
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200755irqreturn_t amd_iommu_int_handler(int irq, void *data)
756{
757 return IRQ_WAKE_THREAD;
758}
759
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200760/****************************************************************************
761 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200762 * IOMMU command queuing functions
763 *
764 ****************************************************************************/
765
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200766static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200767{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200768 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200769
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200770 while (*sem == 0 && i < LOOP_TIMEOUT) {
771 udelay(1);
772 i += 1;
773 }
774
775 if (i == LOOP_TIMEOUT) {
776 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
777 return -EIO;
778 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200779
780 return 0;
781}
782
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200783static void copy_cmd_to_buffer(struct amd_iommu *iommu,
784 struct iommu_cmd *cmd,
785 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200786{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200787 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200788
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200789 target = iommu->cmd_buf + tail;
790 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200791
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200792 /* Copy command to buffer */
793 memcpy(target, cmd, sizeof(*cmd));
794
795 /* Tell the IOMMU about it */
796 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
797}
798
Joerg Roedel815b33f2011-04-06 17:26:49 +0200799static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200800{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200801 WARN_ON(address & 0x7ULL);
802
Joerg Roedelded46732011-04-06 10:53:48 +0200803 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200804 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
805 cmd->data[1] = upper_32_bits(__pa(address));
806 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200807 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
808}
809
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200810static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
811{
812 memset(cmd, 0, sizeof(*cmd));
813 cmd->data[0] = devid;
814 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
815}
816
Joerg Roedel11b64022011-04-06 11:49:28 +0200817static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
818 size_t size, u16 domid, int pde)
819{
820 u64 pages;
821 int s;
822
823 pages = iommu_num_pages(address, size, PAGE_SIZE);
824 s = 0;
825
826 if (pages > 1) {
827 /*
828 * If we have to flush more than one page, flush all
829 * TLB entries for this domain
830 */
831 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
832 s = 1;
833 }
834
835 address &= PAGE_MASK;
836
837 memset(cmd, 0, sizeof(*cmd));
838 cmd->data[1] |= domid;
839 cmd->data[2] = lower_32_bits(address);
840 cmd->data[3] = upper_32_bits(address);
841 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
842 if (s) /* size bit - we flush more than one 4kb page */
843 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200844 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200845 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
846}
847
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200848static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
849 u64 address, size_t size)
850{
851 u64 pages;
852 int s;
853
854 pages = iommu_num_pages(address, size, PAGE_SIZE);
855 s = 0;
856
857 if (pages > 1) {
858 /*
859 * If we have to flush more than one page, flush all
860 * TLB entries for this domain
861 */
862 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
863 s = 1;
864 }
865
866 address &= PAGE_MASK;
867
868 memset(cmd, 0, sizeof(*cmd));
869 cmd->data[0] = devid;
870 cmd->data[0] |= (qdep & 0xff) << 24;
871 cmd->data[1] = devid;
872 cmd->data[2] = lower_32_bits(address);
873 cmd->data[3] = upper_32_bits(address);
874 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
875 if (s)
876 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
877}
878
Joerg Roedel22e266c2011-11-21 15:59:08 +0100879static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
880 u64 address, bool size)
881{
882 memset(cmd, 0, sizeof(*cmd));
883
884 address &= ~(0xfffULL);
885
886 cmd->data[0] = pasid & PASID_MASK;
887 cmd->data[1] = domid;
888 cmd->data[2] = lower_32_bits(address);
889 cmd->data[3] = upper_32_bits(address);
890 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
891 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
892 if (size)
893 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
894 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
895}
896
897static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
898 int qdep, u64 address, bool size)
899{
900 memset(cmd, 0, sizeof(*cmd));
901
902 address &= ~(0xfffULL);
903
904 cmd->data[0] = devid;
905 cmd->data[0] |= (pasid & 0xff) << 16;
906 cmd->data[0] |= (qdep & 0xff) << 24;
907 cmd->data[1] = devid;
908 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
909 cmd->data[2] = lower_32_bits(address);
910 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
911 cmd->data[3] = upper_32_bits(address);
912 if (size)
913 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
914 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
915}
916
Joerg Roedelc99afa22011-11-21 18:19:25 +0100917static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
918 int status, int tag, bool gn)
919{
920 memset(cmd, 0, sizeof(*cmd));
921
922 cmd->data[0] = devid;
923 if (gn) {
924 cmd->data[1] = pasid & PASID_MASK;
925 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
926 }
927 cmd->data[3] = tag & 0x1ff;
928 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
929
930 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
931}
932
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200933static void build_inv_all(struct iommu_cmd *cmd)
934{
935 memset(cmd, 0, sizeof(*cmd));
936 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200937}
938
Joerg Roedel7ef27982012-06-21 16:46:04 +0200939static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
940{
941 memset(cmd, 0, sizeof(*cmd));
942 cmd->data[0] = devid;
943 CMD_SET_TYPE(cmd, CMD_INV_IRT);
944}
945
Joerg Roedel431b2a22008-07-11 17:14:22 +0200946/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200947 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200948 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200949 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200950static int iommu_queue_command_sync(struct amd_iommu *iommu,
951 struct iommu_cmd *cmd,
952 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200953{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200954 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200955 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200956
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200957 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100958
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200959again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200960 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200961
962 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
963 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
964 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
965 left = (head - next_tail) % iommu->cmd_buf_size;
966
967 if (left <= 2) {
968 struct iommu_cmd sync_cmd;
969 volatile u64 sem = 0;
970 int ret;
971
972 build_completion_wait(&sync_cmd, (u64)&sem);
973 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
974
975 spin_unlock_irqrestore(&iommu->lock, flags);
976
977 if ((ret = wait_on_sem(&sem)) != 0)
978 return ret;
979
980 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200981 }
982
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200983 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200984
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200985 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200986 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200987
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200988 spin_unlock_irqrestore(&iommu->lock, flags);
989
Joerg Roedel815b33f2011-04-06 17:26:49 +0200990 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100991}
992
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200993static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
994{
995 return iommu_queue_command_sync(iommu, cmd, true);
996}
997
Joerg Roedel8d201962008-12-02 20:34:41 +0100998/*
999 * This function queues a completion wait command into the command
1000 * buffer of an IOMMU
1001 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001002static int iommu_completion_wait(struct amd_iommu *iommu)
1003{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001004 struct iommu_cmd cmd;
1005 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001006 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001007
1008 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001009 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001010
Joerg Roedel815b33f2011-04-06 17:26:49 +02001011 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001012
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001013 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001014 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001015 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001016
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001017 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001018}
1019
Joerg Roedeld8c13082011-04-06 18:51:26 +02001020static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001021{
1022 struct iommu_cmd cmd;
1023
Joerg Roedeld8c13082011-04-06 18:51:26 +02001024 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001025
Joerg Roedeld8c13082011-04-06 18:51:26 +02001026 return iommu_queue_command(iommu, &cmd);
1027}
1028
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001029static void iommu_flush_dte_all(struct amd_iommu *iommu)
1030{
1031 u32 devid;
1032
1033 for (devid = 0; devid <= 0xffff; ++devid)
1034 iommu_flush_dte(iommu, devid);
1035
1036 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001037}
1038
1039/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001040 * This function uses heavy locking and may disable irqs for some time. But
1041 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001042 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001043static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001044{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001045 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001046
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001047 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1048 struct iommu_cmd cmd;
1049 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1050 dom_id, 1);
1051 iommu_queue_command(iommu, &cmd);
1052 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001053
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001054 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001055}
1056
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001057static void iommu_flush_all(struct amd_iommu *iommu)
1058{
1059 struct iommu_cmd cmd;
1060
1061 build_inv_all(&cmd);
1062
1063 iommu_queue_command(iommu, &cmd);
1064 iommu_completion_wait(iommu);
1065}
1066
Joerg Roedel7ef27982012-06-21 16:46:04 +02001067static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1068{
1069 struct iommu_cmd cmd;
1070
1071 build_inv_irt(&cmd, devid);
1072
1073 iommu_queue_command(iommu, &cmd);
1074}
1075
1076static void iommu_flush_irt_all(struct amd_iommu *iommu)
1077{
1078 u32 devid;
1079
1080 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1081 iommu_flush_irt(iommu, devid);
1082
1083 iommu_completion_wait(iommu);
1084}
1085
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001086void iommu_flush_all_caches(struct amd_iommu *iommu)
1087{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001088 if (iommu_feature(iommu, FEATURE_IA)) {
1089 iommu_flush_all(iommu);
1090 } else {
1091 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001092 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001093 iommu_flush_tlb_all(iommu);
1094 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001095}
1096
Joerg Roedel431b2a22008-07-11 17:14:22 +02001097/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001098 * Command send function for flushing on-device TLB
1099 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001100static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1101 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001102{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001103 struct amd_iommu *iommu;
1104 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001105 int qdep;
1106
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001107 qdep = dev_data->ats.qdep;
1108 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001109
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001110 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001111
1112 return iommu_queue_command(iommu, &cmd);
1113}
1114
1115/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001116 * Command send function for invalidating a device table entry
1117 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001118static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001119{
1120 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001121 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001122
Joerg Roedel6c542042011-06-09 17:07:31 +02001123 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001124
Joerg Roedelf62dda62011-06-09 12:55:35 +02001125 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001126 if (ret)
1127 return ret;
1128
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001129 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001130 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001131
1132 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001133}
1134
Joerg Roedel431b2a22008-07-11 17:14:22 +02001135/*
1136 * TLB invalidation function which is called from the mapping functions.
1137 * It invalidates a single PTE if the range to flush is within a single
1138 * page. Otherwise it flushes the whole TLB of the IOMMU.
1139 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001140static void __domain_flush_pages(struct protection_domain *domain,
1141 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001142{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001143 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001144 struct iommu_cmd cmd;
1145 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001146
Joerg Roedel11b64022011-04-06 11:49:28 +02001147 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001148
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001149 for (i = 0; i < amd_iommus_present; ++i) {
1150 if (!domain->dev_iommu[i])
1151 continue;
1152
1153 /*
1154 * Devices of this domain are behind this IOMMU
1155 * We need a TLB flush
1156 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001157 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001158 }
1159
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001160 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001161
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001162 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001163 continue;
1164
Joerg Roedel6c542042011-06-09 17:07:31 +02001165 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001166 }
1167
Joerg Roedel11b64022011-04-06 11:49:28 +02001168 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001169}
1170
Joerg Roedel17b124b2011-04-06 18:01:35 +02001171static void domain_flush_pages(struct protection_domain *domain,
1172 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001173{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001174 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001175}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001176
Joerg Roedel1c655772008-09-04 18:40:05 +02001177/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001178static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001179{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001180 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001181}
1182
Chris Wright42a49f92009-06-15 15:42:00 +02001183/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001184static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001185{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001186 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1187}
1188
1189static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001190{
1191 int i;
1192
1193 for (i = 0; i < amd_iommus_present; ++i) {
1194 if (!domain->dev_iommu[i])
1195 continue;
1196
1197 /*
1198 * Devices of this domain are behind this IOMMU
1199 * We need to wait for completion of all commands.
1200 */
1201 iommu_completion_wait(amd_iommus[i]);
1202 }
1203}
1204
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001205
Joerg Roedel43f49602008-12-02 21:01:12 +01001206/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001207 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001208 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001209static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001210{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001211 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001212
1213 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001214 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001215}
1216
Joerg Roedel431b2a22008-07-11 17:14:22 +02001217/****************************************************************************
1218 *
1219 * The functions below are used the create the page table mappings for
1220 * unity mapped regions.
1221 *
1222 ****************************************************************************/
1223
1224/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001225 * This function is used to add another level to an IO page table. Adding
1226 * another level increases the size of the address space by 9 bits to a size up
1227 * to 64 bits.
1228 */
1229static bool increase_address_space(struct protection_domain *domain,
1230 gfp_t gfp)
1231{
1232 u64 *pte;
1233
1234 if (domain->mode == PAGE_MODE_6_LEVEL)
1235 /* address space already 64 bit large */
1236 return false;
1237
1238 pte = (void *)get_zeroed_page(gfp);
1239 if (!pte)
1240 return false;
1241
1242 *pte = PM_LEVEL_PDE(domain->mode,
1243 virt_to_phys(domain->pt_root));
1244 domain->pt_root = pte;
1245 domain->mode += 1;
1246 domain->updated = true;
1247
1248 return true;
1249}
1250
1251static u64 *alloc_pte(struct protection_domain *domain,
1252 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001253 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001254 u64 **pte_page,
1255 gfp_t gfp)
1256{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001257 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001258 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001259
1260 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001261
1262 while (address > PM_LEVEL_SIZE(domain->mode))
1263 increase_address_space(domain, gfp);
1264
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001265 level = domain->mode - 1;
1266 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1267 address = PAGE_SIZE_ALIGN(address, page_size);
1268 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001269
1270 while (level > end_lvl) {
1271 if (!IOMMU_PTE_PRESENT(*pte)) {
1272 page = (u64 *)get_zeroed_page(gfp);
1273 if (!page)
1274 return NULL;
1275 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1276 }
1277
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001278 /* No level skipping support yet */
1279 if (PM_PTE_LEVEL(*pte) != level)
1280 return NULL;
1281
Joerg Roedel308973d2009-11-24 17:43:32 +01001282 level -= 1;
1283
1284 pte = IOMMU_PTE_PAGE(*pte);
1285
1286 if (pte_page && level == end_lvl)
1287 *pte_page = pte;
1288
1289 pte = &pte[PM_LEVEL_INDEX(level, address)];
1290 }
1291
1292 return pte;
1293}
1294
1295/*
1296 * This function checks if there is a PTE for a given dma address. If
1297 * there is one, it returns the pointer to it.
1298 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001299static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001300{
1301 int level;
1302 u64 *pte;
1303
Joerg Roedel24cd7722010-01-19 17:27:39 +01001304 if (address > PM_LEVEL_SIZE(domain->mode))
1305 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001306
Joerg Roedel24cd7722010-01-19 17:27:39 +01001307 level = domain->mode - 1;
1308 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1309
1310 while (level > 0) {
1311
1312 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001313 if (!IOMMU_PTE_PRESENT(*pte))
1314 return NULL;
1315
Joerg Roedel24cd7722010-01-19 17:27:39 +01001316 /* Large PTE */
1317 if (PM_PTE_LEVEL(*pte) == 0x07) {
1318 unsigned long pte_mask, __pte;
1319
1320 /*
1321 * If we have a series of large PTEs, make
1322 * sure to return a pointer to the first one.
1323 */
1324 pte_mask = PTE_PAGE_SIZE(*pte);
1325 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1326 __pte = ((unsigned long)pte) & pte_mask;
1327
1328 return (u64 *)__pte;
1329 }
1330
1331 /* No level skipping support yet */
1332 if (PM_PTE_LEVEL(*pte) != level)
1333 return NULL;
1334
Joerg Roedel308973d2009-11-24 17:43:32 +01001335 level -= 1;
1336
Joerg Roedel24cd7722010-01-19 17:27:39 +01001337 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001338 pte = IOMMU_PTE_PAGE(*pte);
1339 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001340 }
1341
1342 return pte;
1343}
1344
1345/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001346 * Generic mapping functions. It maps a physical address into a DMA
1347 * address space. It allocates the page table pages if necessary.
1348 * In the future it can be extended to a generic mapping function
1349 * supporting all features of AMD IOMMU page tables like level skipping
1350 * and full 64 bit address spaces.
1351 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001352static int iommu_map_page(struct protection_domain *dom,
1353 unsigned long bus_addr,
1354 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001355 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001356 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001357{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001358 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001359 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001360
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001361 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001362 return -EINVAL;
1363
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001364 bus_addr = PAGE_ALIGN(bus_addr);
1365 phys_addr = PAGE_ALIGN(phys_addr);
1366 count = PAGE_SIZE_PTE_COUNT(page_size);
1367 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001368
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001369 for (i = 0; i < count; ++i)
1370 if (IOMMU_PTE_PRESENT(pte[i]))
1371 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001372
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001373 if (page_size > PAGE_SIZE) {
1374 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1375 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1376 } else
1377 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1378
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001379 if (prot & IOMMU_PROT_IR)
1380 __pte |= IOMMU_PTE_IR;
1381 if (prot & IOMMU_PROT_IW)
1382 __pte |= IOMMU_PTE_IW;
1383
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001384 for (i = 0; i < count; ++i)
1385 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001386
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001387 update_domain(dom);
1388
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001389 return 0;
1390}
1391
Joerg Roedel24cd7722010-01-19 17:27:39 +01001392static unsigned long iommu_unmap_page(struct protection_domain *dom,
1393 unsigned long bus_addr,
1394 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001395{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001396 unsigned long long unmap_size, unmapped;
1397 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001398
Joerg Roedel24cd7722010-01-19 17:27:39 +01001399 BUG_ON(!is_power_of_2(page_size));
1400
1401 unmapped = 0;
1402
1403 while (unmapped < page_size) {
1404
1405 pte = fetch_pte(dom, bus_addr);
1406
1407 if (!pte) {
1408 /*
1409 * No PTE for this address
1410 * move forward in 4kb steps
1411 */
1412 unmap_size = PAGE_SIZE;
1413 } else if (PM_PTE_LEVEL(*pte) == 0) {
1414 /* 4kb PTE found for this address */
1415 unmap_size = PAGE_SIZE;
1416 *pte = 0ULL;
1417 } else {
1418 int count, i;
1419
1420 /* Large PTE found which maps this address */
1421 unmap_size = PTE_PAGE_SIZE(*pte);
1422 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1423 for (i = 0; i < count; i++)
1424 pte[i] = 0ULL;
1425 }
1426
1427 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1428 unmapped += unmap_size;
1429 }
1430
1431 BUG_ON(!is_power_of_2(unmapped));
1432
1433 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001434}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001435
Joerg Roedel431b2a22008-07-11 17:14:22 +02001436/*
1437 * This function checks if a specific unity mapping entry is needed for
1438 * this specific IOMMU.
1439 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001440static int iommu_for_unity_map(struct amd_iommu *iommu,
1441 struct unity_map_entry *entry)
1442{
1443 u16 bdf, i;
1444
1445 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1446 bdf = amd_iommu_alias_table[i];
1447 if (amd_iommu_rlookup_table[bdf] == iommu)
1448 return 1;
1449 }
1450
1451 return 0;
1452}
1453
Joerg Roedel431b2a22008-07-11 17:14:22 +02001454/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001455 * This function actually applies the mapping to the page table of the
1456 * dma_ops domain.
1457 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001458static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1459 struct unity_map_entry *e)
1460{
1461 u64 addr;
1462 int ret;
1463
1464 for (addr = e->address_start; addr < e->address_end;
1465 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001466 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001467 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001468 if (ret)
1469 return ret;
1470 /*
1471 * if unity mapping is in aperture range mark the page
1472 * as allocated in the aperture
1473 */
1474 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001475 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001476 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001477 }
1478
1479 return 0;
1480}
1481
Joerg Roedel431b2a22008-07-11 17:14:22 +02001482/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001483 * Init the unity mappings for a specific IOMMU in the system
1484 *
1485 * Basically iterates over all unity mapping entries and applies them to
1486 * the default domain DMA of that IOMMU if necessary.
1487 */
1488static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1489{
1490 struct unity_map_entry *entry;
1491 int ret;
1492
1493 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1494 if (!iommu_for_unity_map(iommu, entry))
1495 continue;
1496 ret = dma_ops_unity_map(iommu->default_dom, entry);
1497 if (ret)
1498 return ret;
1499 }
1500
1501 return 0;
1502}
1503
1504/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001505 * Inits the unity mappings required for a specific device
1506 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001507static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1508 u16 devid)
1509{
1510 struct unity_map_entry *e;
1511 int ret;
1512
1513 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1514 if (!(devid >= e->devid_start && devid <= e->devid_end))
1515 continue;
1516 ret = dma_ops_unity_map(dma_dom, e);
1517 if (ret)
1518 return ret;
1519 }
1520
1521 return 0;
1522}
1523
Joerg Roedel431b2a22008-07-11 17:14:22 +02001524/****************************************************************************
1525 *
1526 * The next functions belong to the address allocator for the dma_ops
1527 * interface functions. They work like the allocators in the other IOMMU
1528 * drivers. Its basically a bitmap which marks the allocated pages in
1529 * the aperture. Maybe it could be enhanced in the future to a more
1530 * efficient allocator.
1531 *
1532 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001533
Joerg Roedel431b2a22008-07-11 17:14:22 +02001534/*
Joerg Roedel384de722009-05-15 12:30:05 +02001535 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001536 *
1537 * called with domain->lock held
1538 */
Joerg Roedel384de722009-05-15 12:30:05 +02001539
Joerg Roedel9cabe892009-05-18 16:38:55 +02001540/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001541 * Used to reserve address ranges in the aperture (e.g. for exclusion
1542 * ranges.
1543 */
1544static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1545 unsigned long start_page,
1546 unsigned int pages)
1547{
1548 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1549
1550 if (start_page + pages > last_page)
1551 pages = last_page - start_page;
1552
1553 for (i = start_page; i < start_page + pages; ++i) {
1554 int index = i / APERTURE_RANGE_PAGES;
1555 int page = i % APERTURE_RANGE_PAGES;
1556 __set_bit(page, dom->aperture[index]->bitmap);
1557 }
1558}
1559
1560/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001561 * This function is used to add a new aperture range to an existing
1562 * aperture in case of dma_ops domain allocation or address allocation
1563 * failure.
1564 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001565static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001566 bool populate, gfp_t gfp)
1567{
1568 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001569 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001570 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001571
Joerg Roedelf5e97052009-05-22 12:31:53 +02001572#ifdef CONFIG_IOMMU_STRESS
1573 populate = false;
1574#endif
1575
Joerg Roedel9cabe892009-05-18 16:38:55 +02001576 if (index >= APERTURE_MAX_RANGES)
1577 return -ENOMEM;
1578
1579 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1580 if (!dma_dom->aperture[index])
1581 return -ENOMEM;
1582
1583 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1584 if (!dma_dom->aperture[index]->bitmap)
1585 goto out_free;
1586
1587 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1588
1589 if (populate) {
1590 unsigned long address = dma_dom->aperture_size;
1591 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1592 u64 *pte, *pte_page;
1593
1594 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001595 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001596 &pte_page, gfp);
1597 if (!pte)
1598 goto out_free;
1599
1600 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1601
1602 address += APERTURE_RANGE_SIZE / 64;
1603 }
1604 }
1605
Joerg Roedel17f5b562011-07-06 17:14:44 +02001606 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001607 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1608
Joerg Roedel17f5b562011-07-06 17:14:44 +02001609 /* Reserve address range used for MSI messages */
1610 if (old_size < MSI_ADDR_BASE_LO &&
1611 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1612 unsigned long spage;
1613 int pages;
1614
1615 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1616 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1617
1618 dma_ops_reserve_addresses(dma_dom, spage, pages);
1619 }
1620
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001621 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001622 for_each_iommu(iommu) {
1623 if (iommu->exclusion_start &&
1624 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1625 && iommu->exclusion_start < dma_dom->aperture_size) {
1626 unsigned long startpage;
1627 int pages = iommu_num_pages(iommu->exclusion_start,
1628 iommu->exclusion_length,
1629 PAGE_SIZE);
1630 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1631 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1632 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001633 }
1634
1635 /*
1636 * Check for areas already mapped as present in the new aperture
1637 * range and mark those pages as reserved in the allocator. Such
1638 * mappings may already exist as a result of requested unity
1639 * mappings for devices.
1640 */
1641 for (i = dma_dom->aperture[index]->offset;
1642 i < dma_dom->aperture_size;
1643 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001644 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001645 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1646 continue;
1647
Joerg Roedelfcd08612011-10-11 17:41:32 +02001648 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001649 }
1650
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001651 update_domain(&dma_dom->domain);
1652
Joerg Roedel9cabe892009-05-18 16:38:55 +02001653 return 0;
1654
1655out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001656 update_domain(&dma_dom->domain);
1657
Joerg Roedel9cabe892009-05-18 16:38:55 +02001658 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1659
1660 kfree(dma_dom->aperture[index]);
1661 dma_dom->aperture[index] = NULL;
1662
1663 return -ENOMEM;
1664}
1665
Joerg Roedel384de722009-05-15 12:30:05 +02001666static unsigned long dma_ops_area_alloc(struct device *dev,
1667 struct dma_ops_domain *dom,
1668 unsigned int pages,
1669 unsigned long align_mask,
1670 u64 dma_mask,
1671 unsigned long start)
1672{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001673 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001674 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1675 int i = start >> APERTURE_RANGE_SHIFT;
1676 unsigned long boundary_size;
1677 unsigned long address = -1;
1678 unsigned long limit;
1679
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001680 next_bit >>= PAGE_SHIFT;
1681
Joerg Roedel384de722009-05-15 12:30:05 +02001682 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1683 PAGE_SIZE) >> PAGE_SHIFT;
1684
1685 for (;i < max_index; ++i) {
1686 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1687
1688 if (dom->aperture[i]->offset >= dma_mask)
1689 break;
1690
1691 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1692 dma_mask >> PAGE_SHIFT);
1693
1694 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1695 limit, next_bit, pages, 0,
1696 boundary_size, align_mask);
1697 if (address != -1) {
1698 address = dom->aperture[i]->offset +
1699 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001700 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001701 break;
1702 }
1703
1704 next_bit = 0;
1705 }
1706
1707 return address;
1708}
1709
Joerg Roedeld3086442008-06-26 21:27:57 +02001710static unsigned long dma_ops_alloc_addresses(struct device *dev,
1711 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001712 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001713 unsigned long align_mask,
1714 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001715{
Joerg Roedeld3086442008-06-26 21:27:57 +02001716 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001717
Joerg Roedelfe16f082009-05-22 12:27:53 +02001718#ifdef CONFIG_IOMMU_STRESS
1719 dom->next_address = 0;
1720 dom->need_flush = true;
1721#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001722
Joerg Roedel384de722009-05-15 12:30:05 +02001723 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001724 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001725
Joerg Roedel1c655772008-09-04 18:40:05 +02001726 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001727 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001728 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1729 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001730 dom->need_flush = true;
1731 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001732
Joerg Roedel384de722009-05-15 12:30:05 +02001733 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001734 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001735
1736 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1737
1738 return address;
1739}
1740
Joerg Roedel431b2a22008-07-11 17:14:22 +02001741/*
1742 * The address free function.
1743 *
1744 * called with domain->lock held
1745 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001746static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1747 unsigned long address,
1748 unsigned int pages)
1749{
Joerg Roedel384de722009-05-15 12:30:05 +02001750 unsigned i = address >> APERTURE_RANGE_SHIFT;
1751 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001752
Joerg Roedel384de722009-05-15 12:30:05 +02001753 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1754
Joerg Roedel47bccd62009-05-22 12:40:54 +02001755#ifdef CONFIG_IOMMU_STRESS
1756 if (i < 4)
1757 return;
1758#endif
1759
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001760 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001761 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001762
1763 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001764
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001765 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001766
Joerg Roedeld3086442008-06-26 21:27:57 +02001767}
1768
Joerg Roedel431b2a22008-07-11 17:14:22 +02001769/****************************************************************************
1770 *
1771 * The next functions belong to the domain allocation. A domain is
1772 * allocated for every IOMMU as the default domain. If device isolation
1773 * is enabled, every device get its own domain. The most important thing
1774 * about domains is the page table mapping the DMA address space they
1775 * contain.
1776 *
1777 ****************************************************************************/
1778
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001779/*
1780 * This function adds a protection domain to the global protection domain list
1781 */
1782static void add_domain_to_list(struct protection_domain *domain)
1783{
1784 unsigned long flags;
1785
1786 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1787 list_add(&domain->list, &amd_iommu_pd_list);
1788 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1789}
1790
1791/*
1792 * This function removes a protection domain to the global
1793 * protection domain list
1794 */
1795static void del_domain_from_list(struct protection_domain *domain)
1796{
1797 unsigned long flags;
1798
1799 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1800 list_del(&domain->list);
1801 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1802}
1803
Joerg Roedelec487d12008-06-26 21:27:58 +02001804static u16 domain_id_alloc(void)
1805{
1806 unsigned long flags;
1807 int id;
1808
1809 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1810 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1811 BUG_ON(id == 0);
1812 if (id > 0 && id < MAX_DOMAIN_ID)
1813 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1814 else
1815 id = 0;
1816 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1817
1818 return id;
1819}
1820
Joerg Roedela2acfb72008-12-02 18:28:53 +01001821static void domain_id_free(int id)
1822{
1823 unsigned long flags;
1824
1825 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1826 if (id > 0 && id < MAX_DOMAIN_ID)
1827 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1828 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1829}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001830
Joerg Roedel86db2e52008-12-02 18:20:21 +01001831static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001832{
1833 int i, j;
1834 u64 *p1, *p2, *p3;
1835
Joerg Roedel86db2e52008-12-02 18:20:21 +01001836 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001837
1838 if (!p1)
1839 return;
1840
1841 for (i = 0; i < 512; ++i) {
1842 if (!IOMMU_PTE_PRESENT(p1[i]))
1843 continue;
1844
1845 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001846 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001847 if (!IOMMU_PTE_PRESENT(p2[j]))
1848 continue;
1849 p3 = IOMMU_PTE_PAGE(p2[j]);
1850 free_page((unsigned long)p3);
1851 }
1852
1853 free_page((unsigned long)p2);
1854 }
1855
1856 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001857
1858 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001859}
1860
Joerg Roedelb16137b2011-11-21 16:50:23 +01001861static void free_gcr3_tbl_level1(u64 *tbl)
1862{
1863 u64 *ptr;
1864 int i;
1865
1866 for (i = 0; i < 512; ++i) {
1867 if (!(tbl[i] & GCR3_VALID))
1868 continue;
1869
1870 ptr = __va(tbl[i] & PAGE_MASK);
1871
1872 free_page((unsigned long)ptr);
1873 }
1874}
1875
1876static void free_gcr3_tbl_level2(u64 *tbl)
1877{
1878 u64 *ptr;
1879 int i;
1880
1881 for (i = 0; i < 512; ++i) {
1882 if (!(tbl[i] & GCR3_VALID))
1883 continue;
1884
1885 ptr = __va(tbl[i] & PAGE_MASK);
1886
1887 free_gcr3_tbl_level1(ptr);
1888 }
1889}
1890
Joerg Roedel52815b72011-11-17 17:24:28 +01001891static void free_gcr3_table(struct protection_domain *domain)
1892{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001893 if (domain->glx == 2)
1894 free_gcr3_tbl_level2(domain->gcr3_tbl);
1895 else if (domain->glx == 1)
1896 free_gcr3_tbl_level1(domain->gcr3_tbl);
1897 else if (domain->glx != 0)
1898 BUG();
1899
Joerg Roedel52815b72011-11-17 17:24:28 +01001900 free_page((unsigned long)domain->gcr3_tbl);
1901}
1902
Joerg Roedel431b2a22008-07-11 17:14:22 +02001903/*
1904 * Free a domain, only used if something went wrong in the
1905 * allocation path and we need to free an already allocated page table
1906 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001907static void dma_ops_domain_free(struct dma_ops_domain *dom)
1908{
Joerg Roedel384de722009-05-15 12:30:05 +02001909 int i;
1910
Joerg Roedelec487d12008-06-26 21:27:58 +02001911 if (!dom)
1912 return;
1913
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001914 del_domain_from_list(&dom->domain);
1915
Joerg Roedel86db2e52008-12-02 18:20:21 +01001916 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001917
Joerg Roedel384de722009-05-15 12:30:05 +02001918 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1919 if (!dom->aperture[i])
1920 continue;
1921 free_page((unsigned long)dom->aperture[i]->bitmap);
1922 kfree(dom->aperture[i]);
1923 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001924
1925 kfree(dom);
1926}
1927
Joerg Roedel431b2a22008-07-11 17:14:22 +02001928/*
1929 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001930 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001931 * structures required for the dma_ops interface
1932 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001933static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001934{
1935 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001936
1937 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1938 if (!dma_dom)
1939 return NULL;
1940
1941 spin_lock_init(&dma_dom->domain.lock);
1942
1943 dma_dom->domain.id = domain_id_alloc();
1944 if (dma_dom->domain.id == 0)
1945 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001946 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001947 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001948 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001949 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001950 dma_dom->domain.priv = dma_dom;
1951 if (!dma_dom->domain.pt_root)
1952 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001953
Joerg Roedel1c655772008-09-04 18:40:05 +02001954 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001955 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001956
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001957 add_domain_to_list(&dma_dom->domain);
1958
Joerg Roedel576175c2009-11-23 19:08:46 +01001959 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001960 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001961
Joerg Roedel431b2a22008-07-11 17:14:22 +02001962 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001963 * mark the first page as allocated so we never return 0 as
1964 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001965 */
Joerg Roedel384de722009-05-15 12:30:05 +02001966 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001967 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001968
Joerg Roedelec487d12008-06-26 21:27:58 +02001969
1970 return dma_dom;
1971
1972free_dma_dom:
1973 dma_ops_domain_free(dma_dom);
1974
1975 return NULL;
1976}
1977
Joerg Roedel431b2a22008-07-11 17:14:22 +02001978/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001979 * little helper function to check whether a given protection domain is a
1980 * dma_ops domain
1981 */
1982static bool dma_ops_domain(struct protection_domain *domain)
1983{
1984 return domain->flags & PD_DMA_OPS_MASK;
1985}
1986
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001987static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001988{
Joerg Roedel132bd682011-11-17 14:18:46 +01001989 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001990 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001991
Joerg Roedel132bd682011-11-17 14:18:46 +01001992 if (domain->mode != PAGE_MODE_NONE)
1993 pte_root = virt_to_phys(domain->pt_root);
1994
Joerg Roedel38ddf412008-09-11 10:38:32 +02001995 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1996 << DEV_ENTRY_MODE_SHIFT;
1997 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001998
Joerg Roedelee6c2862011-11-09 12:06:03 +01001999 flags = amd_iommu_dev_table[devid].data[1];
2000
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002001 if (ats)
2002 flags |= DTE_FLAG_IOTLB;
2003
Joerg Roedel52815b72011-11-17 17:24:28 +01002004 if (domain->flags & PD_IOMMUV2_MASK) {
2005 u64 gcr3 = __pa(domain->gcr3_tbl);
2006 u64 glx = domain->glx;
2007 u64 tmp;
2008
2009 pte_root |= DTE_FLAG_GV;
2010 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2011
2012 /* First mask out possible old values for GCR3 table */
2013 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2014 flags &= ~tmp;
2015
2016 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2017 flags &= ~tmp;
2018
2019 /* Encode GCR3 table into DTE */
2020 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2021 pte_root |= tmp;
2022
2023 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2024 flags |= tmp;
2025
2026 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2027 flags |= tmp;
2028 }
2029
Joerg Roedelee6c2862011-11-09 12:06:03 +01002030 flags &= ~(0xffffUL);
2031 flags |= domain->id;
2032
2033 amd_iommu_dev_table[devid].data[1] = flags;
2034 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002035}
2036
Joerg Roedel15898bb2009-11-24 15:39:42 +01002037static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002038{
Joerg Roedel355bf552008-12-08 12:02:41 +01002039 /* remove entry from the device table seen by the hardware */
2040 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2041 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002042
Joerg Roedelc5cca142009-10-09 18:31:20 +02002043 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002044}
2045
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002046static void do_attach(struct iommu_dev_data *dev_data,
2047 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002048{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002049 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002050 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002051
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002052 iommu = amd_iommu_rlookup_table[dev_data->devid];
2053 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002054
2055 /* Update data structures */
2056 dev_data->domain = domain;
2057 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002058 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002059
2060 /* Do reference counting */
2061 domain->dev_iommu[iommu->index] += 1;
2062 domain->dev_cnt += 1;
2063
2064 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002065 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002066}
2067
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002068static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002069{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002070 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002071
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002072 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002073
Joerg Roedelc4596112009-11-20 14:57:32 +01002074 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002075 dev_data->domain->dev_iommu[iommu->index] -= 1;
2076 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002077
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002078 /* Update data structures */
2079 dev_data->domain = NULL;
2080 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002081 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002082
2083 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002084 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002085}
2086
2087/*
2088 * If a device is not yet associated with a domain, this function does
2089 * assigns it visible for the hardware
2090 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002091static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002092 struct protection_domain *domain)
2093{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002094 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002095
Joerg Roedel15898bb2009-11-24 15:39:42 +01002096 /* lock domain */
2097 spin_lock(&domain->lock);
2098
Joerg Roedel71f77582011-06-09 19:03:15 +02002099 if (dev_data->alias_data != NULL) {
2100 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002101
Joerg Roedel2b02b092011-06-09 17:48:39 +02002102 /* Some sanity checks */
2103 ret = -EBUSY;
2104 if (alias_data->domain != NULL &&
2105 alias_data->domain != domain)
2106 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002107
Joerg Roedel2b02b092011-06-09 17:48:39 +02002108 if (dev_data->domain != NULL &&
2109 dev_data->domain != domain)
2110 goto out_unlock;
2111
2112 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002113 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002114 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002115
2116 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002117 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002118
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002119 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002120 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002121
Joerg Roedel24100052009-11-25 15:59:57 +01002122 atomic_inc(&dev_data->bind);
2123
Julia Lawall84fe6c12010-05-27 12:31:51 +02002124 ret = 0;
2125
2126out_unlock:
2127
Joerg Roedel355bf552008-12-08 12:02:41 +01002128 /* ready */
2129 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002130
Julia Lawall84fe6c12010-05-27 12:31:51 +02002131 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002132}
2133
Joerg Roedel52815b72011-11-17 17:24:28 +01002134
2135static void pdev_iommuv2_disable(struct pci_dev *pdev)
2136{
2137 pci_disable_ats(pdev);
2138 pci_disable_pri(pdev);
2139 pci_disable_pasid(pdev);
2140}
2141
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002142/* FIXME: Change generic reset-function to do the same */
2143static int pri_reset_while_enabled(struct pci_dev *pdev)
2144{
2145 u16 control;
2146 int pos;
2147
Joerg Roedel46277b72011-12-07 14:34:02 +01002148 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002149 if (!pos)
2150 return -EINVAL;
2151
Joerg Roedel46277b72011-12-07 14:34:02 +01002152 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2153 control |= PCI_PRI_CTRL_RESET;
2154 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002155
2156 return 0;
2157}
2158
Joerg Roedel52815b72011-11-17 17:24:28 +01002159static int pdev_iommuv2_enable(struct pci_dev *pdev)
2160{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002161 bool reset_enable;
2162 int reqs, ret;
2163
2164 /* FIXME: Hardcode number of outstanding requests for now */
2165 reqs = 32;
2166 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2167 reqs = 1;
2168 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002169
2170 /* Only allow access to user-accessible pages */
2171 ret = pci_enable_pasid(pdev, 0);
2172 if (ret)
2173 goto out_err;
2174
2175 /* First reset the PRI state of the device */
2176 ret = pci_reset_pri(pdev);
2177 if (ret)
2178 goto out_err;
2179
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002180 /* Enable PRI */
2181 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002182 if (ret)
2183 goto out_err;
2184
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002185 if (reset_enable) {
2186 ret = pri_reset_while_enabled(pdev);
2187 if (ret)
2188 goto out_err;
2189 }
2190
Joerg Roedel52815b72011-11-17 17:24:28 +01002191 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2192 if (ret)
2193 goto out_err;
2194
2195 return 0;
2196
2197out_err:
2198 pci_disable_pri(pdev);
2199 pci_disable_pasid(pdev);
2200
2201 return ret;
2202}
2203
Joerg Roedelc99afa22011-11-21 18:19:25 +01002204/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002205#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002206
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002207static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002208{
Joerg Roedela3b93122012-04-12 12:49:26 +02002209 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002210 int pos;
2211
Joerg Roedel46277b72011-12-07 14:34:02 +01002212 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002213 if (!pos)
2214 return false;
2215
Joerg Roedela3b93122012-04-12 12:49:26 +02002216 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002217
Joerg Roedela3b93122012-04-12 12:49:26 +02002218 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002219}
2220
Joerg Roedel15898bb2009-11-24 15:39:42 +01002221/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002222 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002223 * assigns it visible for the hardware
2224 */
2225static int attach_device(struct device *dev,
2226 struct protection_domain *domain)
2227{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002228 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002229 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002230 unsigned long flags;
2231 int ret;
2232
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002233 dev_data = get_dev_data(dev);
2234
Joerg Roedel52815b72011-11-17 17:24:28 +01002235 if (domain->flags & PD_IOMMUV2_MASK) {
2236 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2237 return -EINVAL;
2238
2239 if (pdev_iommuv2_enable(pdev) != 0)
2240 return -EINVAL;
2241
2242 dev_data->ats.enabled = true;
2243 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002244 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002245 } else if (amd_iommu_iotlb_sup &&
2246 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002247 dev_data->ats.enabled = true;
2248 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2249 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002250
Joerg Roedel15898bb2009-11-24 15:39:42 +01002251 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002252 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002253 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2254
2255 /*
2256 * We might boot into a crash-kernel here. The crashed kernel
2257 * left the caches in the IOMMU dirty. So we have to flush
2258 * here to evict all dirty stuff.
2259 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002260 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002261
2262 return ret;
2263}
2264
2265/*
2266 * Removes a device from a protection domain (unlocked)
2267 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002268static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002269{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002270 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002271 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002272
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002273 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002274
Joerg Roedel2ca76272010-01-22 16:45:31 +01002275 domain = dev_data->domain;
2276
2277 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002278
Joerg Roedel71f77582011-06-09 19:03:15 +02002279 if (dev_data->alias_data != NULL) {
2280 struct iommu_dev_data *alias_data = dev_data->alias_data;
2281
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002282 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002283 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002284 }
2285
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002286 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002287 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002288
Joerg Roedel2ca76272010-01-22 16:45:31 +01002289 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002290
Joerg Roedel21129f72009-09-01 11:59:42 +02002291 /*
2292 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002293 * passthrough domain if it is detached from any other domain.
2294 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002295 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002296 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002297 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002298 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002299}
2300
2301/*
2302 * Removes a device from a protection domain (with devtable_lock held)
2303 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002304static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002305{
Joerg Roedel52815b72011-11-17 17:24:28 +01002306 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002307 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002308 unsigned long flags;
2309
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002310 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002311 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002312
Joerg Roedel355bf552008-12-08 12:02:41 +01002313 /* lock device table */
2314 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002315 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002316 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002317
Joerg Roedel52815b72011-11-17 17:24:28 +01002318 if (domain->flags & PD_IOMMUV2_MASK)
2319 pdev_iommuv2_disable(to_pci_dev(dev));
2320 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002321 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002322
2323 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002324}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002325
Joerg Roedel15898bb2009-11-24 15:39:42 +01002326/*
2327 * Find out the protection domain structure for a given PCI device. This
2328 * will give us the pointer to the page table root for example.
2329 */
2330static struct protection_domain *domain_for_device(struct device *dev)
2331{
Joerg Roedel71f77582011-06-09 19:03:15 +02002332 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002333 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002334 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002335
Joerg Roedel657cbb62009-11-23 15:26:46 +01002336 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002337
Joerg Roedel2b02b092011-06-09 17:48:39 +02002338 if (dev_data->domain)
2339 return dev_data->domain;
2340
Joerg Roedel71f77582011-06-09 19:03:15 +02002341 if (dev_data->alias_data != NULL) {
2342 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002343
2344 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2345 if (alias_data->domain != NULL) {
2346 __attach_device(dev_data, alias_data->domain);
2347 dom = alias_data->domain;
2348 }
2349 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002350 }
2351
Joerg Roedel15898bb2009-11-24 15:39:42 +01002352 return dom;
2353}
2354
Joerg Roedele275a2a2008-12-10 18:27:25 +01002355static int device_change_notifier(struct notifier_block *nb,
2356 unsigned long action, void *data)
2357{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002358 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002359 struct protection_domain *domain;
2360 struct iommu_dev_data *dev_data;
2361 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002362 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002363 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002364 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002365
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002366 if (!check_device(dev))
2367 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002368
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002369 devid = get_device_id(dev);
2370 iommu = amd_iommu_rlookup_table[devid];
2371 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002372
2373 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002374 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002375
2376 domain = domain_for_device(dev);
2377
Joerg Roedele275a2a2008-12-10 18:27:25 +01002378 if (!domain)
2379 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002380 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002381 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002382 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002383 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002384 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002385
2386 iommu_init_device(dev);
2387
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002388 /*
2389 * dev_data is still NULL and
2390 * got initialized in iommu_init_device
2391 */
2392 dev_data = get_dev_data(dev);
2393
2394 if (iommu_pass_through || dev_data->iommu_v2) {
2395 dev_data->passthrough = true;
2396 attach_device(dev, pt_domain);
2397 break;
2398 }
2399
Joerg Roedel657cbb62009-11-23 15:26:46 +01002400 domain = domain_for_device(dev);
2401
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002402 /* allocate a protection domain if a device is added */
2403 dma_domain = find_protection_domain(devid);
2404 if (dma_domain)
2405 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002406 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002407 if (!dma_domain)
2408 goto out;
2409 dma_domain->target_dev = devid;
2410
2411 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2412 list_add_tail(&dma_domain->list, &iommu_pd_list);
2413 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2414
Joerg Roedelac1534a2012-06-21 14:52:40 +02002415 dev_data = get_dev_data(dev);
2416
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002417 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002418
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002419 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002420 case BUS_NOTIFY_DEL_DEVICE:
2421
2422 iommu_uninit_device(dev);
2423
Joerg Roedele275a2a2008-12-10 18:27:25 +01002424 default:
2425 goto out;
2426 }
2427
Joerg Roedele275a2a2008-12-10 18:27:25 +01002428 iommu_completion_wait(iommu);
2429
2430out:
2431 return 0;
2432}
2433
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302434static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002435 .notifier_call = device_change_notifier,
2436};
Joerg Roedel355bf552008-12-08 12:02:41 +01002437
Joerg Roedel8638c492009-12-10 11:12:25 +01002438void amd_iommu_init_notifier(void)
2439{
2440 bus_register_notifier(&pci_bus_type, &device_nb);
2441}
2442
Joerg Roedel431b2a22008-07-11 17:14:22 +02002443/*****************************************************************************
2444 *
2445 * The next functions belong to the dma_ops mapping/unmapping code.
2446 *
2447 *****************************************************************************/
2448
2449/*
2450 * In the dma_ops path we only have the struct device. This function
2451 * finds the corresponding IOMMU, the protection domain and the
2452 * requestor id for a given device.
2453 * If the device is not yet associated with a domain this is also done
2454 * in this function.
2455 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002456static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002457{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002458 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002459 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002460 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002461
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002462 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002463 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002464
Joerg Roedel94f6d192009-11-24 16:40:02 +01002465 domain = domain_for_device(dev);
2466 if (domain != NULL && !dma_ops_domain(domain))
2467 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002468
Joerg Roedel94f6d192009-11-24 16:40:02 +01002469 if (domain != NULL)
2470 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002471
Frank Arnolddf805ab2012-08-27 19:21:04 +02002472 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002473 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002474 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002475 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2476 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002477 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002478 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002479
Joerg Roedel94f6d192009-11-24 16:40:02 +01002480 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002481}
2482
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002483static void update_device_table(struct protection_domain *domain)
2484{
Joerg Roedel492667d2009-11-27 13:25:47 +01002485 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002486
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002487 list_for_each_entry(dev_data, &domain->dev_list, list)
2488 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002489}
2490
2491static void update_domain(struct protection_domain *domain)
2492{
2493 if (!domain->updated)
2494 return;
2495
2496 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002497
2498 domain_flush_devices(domain);
2499 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002500
2501 domain->updated = false;
2502}
2503
Joerg Roedel431b2a22008-07-11 17:14:22 +02002504/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002505 * This function fetches the PTE for a given address in the aperture
2506 */
2507static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2508 unsigned long address)
2509{
Joerg Roedel384de722009-05-15 12:30:05 +02002510 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002511 u64 *pte, *pte_page;
2512
Joerg Roedel384de722009-05-15 12:30:05 +02002513 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2514 if (!aperture)
2515 return NULL;
2516
2517 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002518 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002519 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002520 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002521 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2522 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002523 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002524
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002525 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002526
2527 return pte;
2528}
2529
2530/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002531 * This is the generic map function. It maps one 4kb page at paddr to
2532 * the given address in the DMA address space for the domain.
2533 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002534static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002535 unsigned long address,
2536 phys_addr_t paddr,
2537 int direction)
2538{
2539 u64 *pte, __pte;
2540
2541 WARN_ON(address > dom->aperture_size);
2542
2543 paddr &= PAGE_MASK;
2544
Joerg Roedel8bda3092009-05-12 12:02:46 +02002545 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002546 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002547 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002548
2549 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2550
2551 if (direction == DMA_TO_DEVICE)
2552 __pte |= IOMMU_PTE_IR;
2553 else if (direction == DMA_FROM_DEVICE)
2554 __pte |= IOMMU_PTE_IW;
2555 else if (direction == DMA_BIDIRECTIONAL)
2556 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2557
2558 WARN_ON(*pte);
2559
2560 *pte = __pte;
2561
2562 return (dma_addr_t)address;
2563}
2564
Joerg Roedel431b2a22008-07-11 17:14:22 +02002565/*
2566 * The generic unmapping function for on page in the DMA address space.
2567 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002568static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002569 unsigned long address)
2570{
Joerg Roedel384de722009-05-15 12:30:05 +02002571 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002572 u64 *pte;
2573
2574 if (address >= dom->aperture_size)
2575 return;
2576
Joerg Roedel384de722009-05-15 12:30:05 +02002577 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2578 if (!aperture)
2579 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002580
Joerg Roedel384de722009-05-15 12:30:05 +02002581 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2582 if (!pte)
2583 return;
2584
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002585 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002586
2587 WARN_ON(!*pte);
2588
2589 *pte = 0ULL;
2590}
2591
Joerg Roedel431b2a22008-07-11 17:14:22 +02002592/*
2593 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002594 * contiguous memory region into DMA address space. It is used by all
2595 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002596 * Must be called with the domain lock held.
2597 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002598static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002599 struct dma_ops_domain *dma_dom,
2600 phys_addr_t paddr,
2601 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002602 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002603 bool align,
2604 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002605{
2606 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002607 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002608 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002609 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002610 int i;
2611
Joerg Roedele3c449f2008-10-15 22:02:11 -07002612 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002613 paddr &= PAGE_MASK;
2614
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002615 INC_STATS_COUNTER(total_map_requests);
2616
Joerg Roedelc1858972008-12-12 15:42:39 +01002617 if (pages > 1)
2618 INC_STATS_COUNTER(cross_page);
2619
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002620 if (align)
2621 align_mask = (1UL << get_order(size)) - 1;
2622
Joerg Roedel11b83882009-05-19 10:23:15 +02002623retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002624 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2625 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002626 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002627 /*
2628 * setting next_address here will let the address
2629 * allocator only scan the new allocated range in the
2630 * first run. This is a small optimization.
2631 */
2632 dma_dom->next_address = dma_dom->aperture_size;
2633
Joerg Roedel576175c2009-11-23 19:08:46 +01002634 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002635 goto out;
2636
2637 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002638 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002639 * allocation again
2640 */
2641 goto retry;
2642 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002643
2644 start = address;
2645 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002646 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002647 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002648 goto out_unmap;
2649
Joerg Roedelcb76c322008-06-26 21:28:00 +02002650 paddr += PAGE_SIZE;
2651 start += PAGE_SIZE;
2652 }
2653 address += offset;
2654
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002655 ADD_STATS_COUNTER(alloced_io_mem, size);
2656
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002657 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002658 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002659 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002660 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002661 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002662
Joerg Roedelcb76c322008-06-26 21:28:00 +02002663out:
2664 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002665
2666out_unmap:
2667
2668 for (--i; i >= 0; --i) {
2669 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002670 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002671 }
2672
2673 dma_ops_free_addresses(dma_dom, address, pages);
2674
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002675 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002676}
2677
Joerg Roedel431b2a22008-07-11 17:14:22 +02002678/*
2679 * Does the reverse of the __map_single function. Must be called with
2680 * the domain lock held too
2681 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002682static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002683 dma_addr_t dma_addr,
2684 size_t size,
2685 int dir)
2686{
Joerg Roedel04e04632010-09-23 16:12:48 +02002687 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002688 dma_addr_t i, start;
2689 unsigned int pages;
2690
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002691 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002692 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002693 return;
2694
Joerg Roedel04e04632010-09-23 16:12:48 +02002695 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002696 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002697 dma_addr &= PAGE_MASK;
2698 start = dma_addr;
2699
2700 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002701 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002702 start += PAGE_SIZE;
2703 }
2704
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002705 SUB_STATS_COUNTER(alloced_io_mem, size);
2706
Joerg Roedelcb76c322008-06-26 21:28:00 +02002707 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002708
Joerg Roedel80be3082008-11-06 14:59:05 +01002709 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002710 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002711 dma_dom->need_flush = false;
2712 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002713}
2714
Joerg Roedel431b2a22008-07-11 17:14:22 +02002715/*
2716 * The exported map_single function for dma_ops.
2717 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002718static dma_addr_t map_page(struct device *dev, struct page *page,
2719 unsigned long offset, size_t size,
2720 enum dma_data_direction dir,
2721 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002722{
2723 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002724 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002725 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002726 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002727 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002728
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002729 INC_STATS_COUNTER(cnt_map_single);
2730
Joerg Roedel94f6d192009-11-24 16:40:02 +01002731 domain = get_domain(dev);
2732 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002733 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002734 else if (IS_ERR(domain))
2735 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002736
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002737 dma_mask = *dev->dma_mask;
2738
Joerg Roedel4da70b92008-06-26 21:28:01 +02002739 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002740
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002741 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002742 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002743 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002744 goto out;
2745
Joerg Roedel17b124b2011-04-06 18:01:35 +02002746 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002747
2748out:
2749 spin_unlock_irqrestore(&domain->lock, flags);
2750
2751 return addr;
2752}
2753
Joerg Roedel431b2a22008-07-11 17:14:22 +02002754/*
2755 * The exported unmap_single function for dma_ops.
2756 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002757static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2758 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002759{
2760 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002761 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002762
Joerg Roedel146a6912008-12-12 15:07:12 +01002763 INC_STATS_COUNTER(cnt_unmap_single);
2764
Joerg Roedel94f6d192009-11-24 16:40:02 +01002765 domain = get_domain(dev);
2766 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002767 return;
2768
Joerg Roedel4da70b92008-06-26 21:28:01 +02002769 spin_lock_irqsave(&domain->lock, flags);
2770
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002771 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002772
Joerg Roedel17b124b2011-04-06 18:01:35 +02002773 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002774
2775 spin_unlock_irqrestore(&domain->lock, flags);
2776}
2777
Joerg Roedel431b2a22008-07-11 17:14:22 +02002778/*
2779 * This is a special map_sg function which is used if we should map a
2780 * device which is not handled by an AMD IOMMU in the system.
2781 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002782static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2783 int nelems, int dir)
2784{
2785 struct scatterlist *s;
2786 int i;
2787
2788 for_each_sg(sglist, s, nelems, i) {
2789 s->dma_address = (dma_addr_t)sg_phys(s);
2790 s->dma_length = s->length;
2791 }
2792
2793 return nelems;
2794}
2795
Joerg Roedel431b2a22008-07-11 17:14:22 +02002796/*
2797 * The exported map_sg function for dma_ops (handles scatter-gather
2798 * lists).
2799 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002800static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002801 int nelems, enum dma_data_direction dir,
2802 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002803{
2804 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002805 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002806 int i;
2807 struct scatterlist *s;
2808 phys_addr_t paddr;
2809 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002810 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002811
Joerg Roedeld03f0672008-12-12 15:09:48 +01002812 INC_STATS_COUNTER(cnt_map_sg);
2813
Joerg Roedel94f6d192009-11-24 16:40:02 +01002814 domain = get_domain(dev);
2815 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002816 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002817 else if (IS_ERR(domain))
2818 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002819
Joerg Roedel832a90c2008-09-18 15:54:23 +02002820 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002821
Joerg Roedel65b050a2008-06-26 21:28:02 +02002822 spin_lock_irqsave(&domain->lock, flags);
2823
2824 for_each_sg(sglist, s, nelems, i) {
2825 paddr = sg_phys(s);
2826
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002827 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002828 paddr, s->length, dir, false,
2829 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002830
2831 if (s->dma_address) {
2832 s->dma_length = s->length;
2833 mapped_elems++;
2834 } else
2835 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002836 }
2837
Joerg Roedel17b124b2011-04-06 18:01:35 +02002838 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002839
2840out:
2841 spin_unlock_irqrestore(&domain->lock, flags);
2842
2843 return mapped_elems;
2844unmap:
2845 for_each_sg(sglist, s, mapped_elems, i) {
2846 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002847 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002848 s->dma_length, dir);
2849 s->dma_address = s->dma_length = 0;
2850 }
2851
2852 mapped_elems = 0;
2853
2854 goto out;
2855}
2856
Joerg Roedel431b2a22008-07-11 17:14:22 +02002857/*
2858 * The exported map_sg function for dma_ops (handles scatter-gather
2859 * lists).
2860 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002861static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002862 int nelems, enum dma_data_direction dir,
2863 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002864{
2865 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002866 struct protection_domain *domain;
2867 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002868 int i;
2869
Joerg Roedel55877a62008-12-12 15:12:14 +01002870 INC_STATS_COUNTER(cnt_unmap_sg);
2871
Joerg Roedel94f6d192009-11-24 16:40:02 +01002872 domain = get_domain(dev);
2873 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002874 return;
2875
Joerg Roedel65b050a2008-06-26 21:28:02 +02002876 spin_lock_irqsave(&domain->lock, flags);
2877
2878 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002879 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002880 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002881 s->dma_address = s->dma_length = 0;
2882 }
2883
Joerg Roedel17b124b2011-04-06 18:01:35 +02002884 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002885
2886 spin_unlock_irqrestore(&domain->lock, flags);
2887}
2888
Joerg Roedel431b2a22008-07-11 17:14:22 +02002889/*
2890 * The exported alloc_coherent function for dma_ops.
2891 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002892static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002893 dma_addr_t *dma_addr, gfp_t flag,
2894 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002895{
2896 unsigned long flags;
2897 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002898 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002899 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002900 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002901
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002902 INC_STATS_COUNTER(cnt_alloc_coherent);
2903
Joerg Roedel94f6d192009-11-24 16:40:02 +01002904 domain = get_domain(dev);
2905 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002906 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2907 *dma_addr = __pa(virt_addr);
2908 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002909 } else if (IS_ERR(domain))
2910 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002911
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002912 dma_mask = dev->coherent_dma_mask;
2913 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2914 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002915
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002916 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2917 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302918 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002919
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002920 paddr = virt_to_phys(virt_addr);
2921
Joerg Roedel832a90c2008-09-18 15:54:23 +02002922 if (!dma_mask)
2923 dma_mask = *dev->dma_mask;
2924
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002925 spin_lock_irqsave(&domain->lock, flags);
2926
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002927 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002928 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002929
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002930 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002931 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002932 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002933 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002934
Joerg Roedel17b124b2011-04-06 18:01:35 +02002935 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002936
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002937 spin_unlock_irqrestore(&domain->lock, flags);
2938
2939 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002940
2941out_free:
2942
2943 free_pages((unsigned long)virt_addr, get_order(size));
2944
2945 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002946}
2947
Joerg Roedel431b2a22008-07-11 17:14:22 +02002948/*
2949 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002950 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002951static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002952 void *virt_addr, dma_addr_t dma_addr,
2953 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002954{
2955 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002956 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002957
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002958 INC_STATS_COUNTER(cnt_free_coherent);
2959
Joerg Roedel94f6d192009-11-24 16:40:02 +01002960 domain = get_domain(dev);
2961 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002962 goto free_mem;
2963
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002964 spin_lock_irqsave(&domain->lock, flags);
2965
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002966 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002967
Joerg Roedel17b124b2011-04-06 18:01:35 +02002968 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002969
2970 spin_unlock_irqrestore(&domain->lock, flags);
2971
2972free_mem:
2973 free_pages((unsigned long)virt_addr, get_order(size));
2974}
2975
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002976/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002977 * This function is called by the DMA layer to find out if we can handle a
2978 * particular device. It is part of the dma_ops.
2979 */
2980static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2981{
Joerg Roedel420aef82009-11-23 16:14:57 +01002982 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002983}
2984
2985/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002986 * The function for pre-allocating protection domains.
2987 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002988 * If the driver core informs the DMA layer if a driver grabs a device
2989 * we don't need to preallocate the protection domains anymore.
2990 * For now we have to.
2991 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01002992static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002993{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002994 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002995 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002996 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002997 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002998
Chris Wrightd18c69d2010-04-02 18:27:55 -07002999 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003000
3001 /* Do we handle this device? */
3002 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003003 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003004
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003005 dev_data = get_dev_data(&dev->dev);
3006 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3007 /* Make sure passthrough domain is allocated */
3008 alloc_passthrough_domain();
3009 dev_data->passthrough = true;
3010 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003011 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003012 dev_name(&dev->dev));
3013 }
3014
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003015 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003016 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003017 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003018
3019 devid = get_device_id(&dev->dev);
3020
Joerg Roedel87a64d52009-11-24 17:26:43 +01003021 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003022 if (!dma_dom)
3023 continue;
3024 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003025 dma_dom->target_dev = devid;
3026
Joerg Roedel15898bb2009-11-24 15:39:42 +01003027 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003028
Joerg Roedelbd60b732008-09-11 10:24:48 +02003029 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003030 }
3031}
3032
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003033static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003034 .alloc = alloc_coherent,
3035 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003036 .map_page = map_page,
3037 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003038 .map_sg = map_sg,
3039 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003040 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003041};
3042
Joerg Roedel27c21272011-05-30 15:56:24 +02003043static unsigned device_dma_ops_init(void)
3044{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003045 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003046 struct pci_dev *pdev = NULL;
3047 unsigned unhandled = 0;
3048
3049 for_each_pci_dev(pdev) {
3050 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003051
3052 iommu_ignore_device(&pdev->dev);
3053
Joerg Roedel27c21272011-05-30 15:56:24 +02003054 unhandled += 1;
3055 continue;
3056 }
3057
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003058 dev_data = get_dev_data(&pdev->dev);
3059
3060 if (!dev_data->passthrough)
3061 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3062 else
3063 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003064 }
3065
3066 return unhandled;
3067}
3068
Joerg Roedel431b2a22008-07-11 17:14:22 +02003069/*
3070 * The function which clues the AMD IOMMU driver into dma_ops.
3071 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003072
3073void __init amd_iommu_init_api(void)
3074{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003075 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003076}
3077
Joerg Roedel6631ee92008-06-26 21:28:05 +02003078int __init amd_iommu_init_dma_ops(void)
3079{
3080 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003081 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003082
Joerg Roedel431b2a22008-07-11 17:14:22 +02003083 /*
3084 * first allocate a default protection domain for every IOMMU we
3085 * found in the system. Devices not assigned to any other
3086 * protection domain will be assigned to the default one.
3087 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003088 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003089 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003090 if (iommu->default_dom == NULL)
3091 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003092 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003093 ret = iommu_init_unity_mappings(iommu);
3094 if (ret)
3095 goto free_domains;
3096 }
3097
Joerg Roedel431b2a22008-07-11 17:14:22 +02003098 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003099 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003100 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003101 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003102
3103 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003104 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003105
Joerg Roedel431b2a22008-07-11 17:14:22 +02003106 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003107 unhandled = device_dma_ops_init();
3108 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3109 /* There are unhandled devices - initialize swiotlb for them */
3110 swiotlb = 1;
3111 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003112
Joerg Roedel7f265082008-12-12 13:50:21 +01003113 amd_iommu_stats_init();
3114
Joerg Roedel62410ee2012-06-12 16:42:43 +02003115 if (amd_iommu_unmap_flush)
3116 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3117 else
3118 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3119
Joerg Roedel6631ee92008-06-26 21:28:05 +02003120 return 0;
3121
3122free_domains:
3123
Joerg Roedel3bd22172009-05-04 15:06:20 +02003124 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003125 if (iommu->default_dom)
3126 dma_ops_domain_free(iommu->default_dom);
3127 }
3128
3129 return ret;
3130}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003131
3132/*****************************************************************************
3133 *
3134 * The following functions belong to the exported interface of AMD IOMMU
3135 *
3136 * This interface allows access to lower level functions of the IOMMU
3137 * like protection domain handling and assignement of devices to domains
3138 * which is not possible with the dma_ops interface.
3139 *
3140 *****************************************************************************/
3141
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003142static void cleanup_domain(struct protection_domain *domain)
3143{
Joerg Roedel492667d2009-11-27 13:25:47 +01003144 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003145 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003146
3147 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3148
Joerg Roedel492667d2009-11-27 13:25:47 +01003149 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003150 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003151 atomic_set(&dev_data->bind, 0);
3152 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003153
3154 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3155}
3156
Joerg Roedel26508152009-08-26 16:52:40 +02003157static void protection_domain_free(struct protection_domain *domain)
3158{
3159 if (!domain)
3160 return;
3161
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003162 del_domain_from_list(domain);
3163
Joerg Roedel26508152009-08-26 16:52:40 +02003164 if (domain->id)
3165 domain_id_free(domain->id);
3166
3167 kfree(domain);
3168}
3169
3170static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003171{
3172 struct protection_domain *domain;
3173
3174 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3175 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003176 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003177
3178 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003179 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003180 domain->id = domain_id_alloc();
3181 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003182 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003183 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003184
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003185 add_domain_to_list(domain);
3186
Joerg Roedel26508152009-08-26 16:52:40 +02003187 return domain;
3188
3189out_err:
3190 kfree(domain);
3191
3192 return NULL;
3193}
3194
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003195static int __init alloc_passthrough_domain(void)
3196{
3197 if (pt_domain != NULL)
3198 return 0;
3199
3200 /* allocate passthrough domain */
3201 pt_domain = protection_domain_alloc();
3202 if (!pt_domain)
3203 return -ENOMEM;
3204
3205 pt_domain->mode = PAGE_MODE_NONE;
3206
3207 return 0;
3208}
Joerg Roedel26508152009-08-26 16:52:40 +02003209static int amd_iommu_domain_init(struct iommu_domain *dom)
3210{
3211 struct protection_domain *domain;
3212
3213 domain = protection_domain_alloc();
3214 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003215 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003216
3217 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003218 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3219 if (!domain->pt_root)
3220 goto out_free;
3221
Joerg Roedelf3572db2011-11-23 12:36:25 +01003222 domain->iommu_domain = dom;
3223
Joerg Roedelc156e342008-12-02 18:13:27 +01003224 dom->priv = domain;
3225
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003226 dom->geometry.aperture_start = 0;
3227 dom->geometry.aperture_end = ~0ULL;
3228 dom->geometry.force_aperture = true;
3229
Joerg Roedelc156e342008-12-02 18:13:27 +01003230 return 0;
3231
3232out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003233 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003234
3235 return -ENOMEM;
3236}
3237
Joerg Roedel98383fc2008-12-02 18:34:12 +01003238static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3239{
3240 struct protection_domain *domain = dom->priv;
3241
3242 if (!domain)
3243 return;
3244
3245 if (domain->dev_cnt > 0)
3246 cleanup_domain(domain);
3247
3248 BUG_ON(domain->dev_cnt != 0);
3249
Joerg Roedel132bd682011-11-17 14:18:46 +01003250 if (domain->mode != PAGE_MODE_NONE)
3251 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003252
Joerg Roedel52815b72011-11-17 17:24:28 +01003253 if (domain->flags & PD_IOMMUV2_MASK)
3254 free_gcr3_table(domain);
3255
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003256 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003257
3258 dom->priv = NULL;
3259}
3260
Joerg Roedel684f2882008-12-08 12:07:44 +01003261static void amd_iommu_detach_device(struct iommu_domain *dom,
3262 struct device *dev)
3263{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003264 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003265 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003266 u16 devid;
3267
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003268 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003269 return;
3270
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003271 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003272
Joerg Roedel657cbb62009-11-23 15:26:46 +01003273 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003274 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003275
3276 iommu = amd_iommu_rlookup_table[devid];
3277 if (!iommu)
3278 return;
3279
Joerg Roedel684f2882008-12-08 12:07:44 +01003280 iommu_completion_wait(iommu);
3281}
3282
Joerg Roedel01106062008-12-02 19:34:11 +01003283static int amd_iommu_attach_device(struct iommu_domain *dom,
3284 struct device *dev)
3285{
3286 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003287 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003288 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003289 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003290
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003291 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003292 return -EINVAL;
3293
Joerg Roedel657cbb62009-11-23 15:26:46 +01003294 dev_data = dev->archdata.iommu;
3295
Joerg Roedelf62dda62011-06-09 12:55:35 +02003296 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003297 if (!iommu)
3298 return -EINVAL;
3299
Joerg Roedel657cbb62009-11-23 15:26:46 +01003300 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003301 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003302
Joerg Roedel15898bb2009-11-24 15:39:42 +01003303 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003304
3305 iommu_completion_wait(iommu);
3306
Joerg Roedel15898bb2009-11-24 15:39:42 +01003307 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003308}
3309
Joerg Roedel468e2362010-01-21 16:37:36 +01003310static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003311 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003312{
3313 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003314 int prot = 0;
3315 int ret;
3316
Joerg Roedel132bd682011-11-17 14:18:46 +01003317 if (domain->mode == PAGE_MODE_NONE)
3318 return -EINVAL;
3319
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003320 if (iommu_prot & IOMMU_READ)
3321 prot |= IOMMU_PROT_IR;
3322 if (iommu_prot & IOMMU_WRITE)
3323 prot |= IOMMU_PROT_IW;
3324
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003325 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003326 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003327 mutex_unlock(&domain->api_lock);
3328
Joerg Roedel795e74f72010-05-11 17:40:57 +02003329 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003330}
3331
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003332static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3333 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003334{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003335 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003336 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003337
Joerg Roedel132bd682011-11-17 14:18:46 +01003338 if (domain->mode == PAGE_MODE_NONE)
3339 return -EINVAL;
3340
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003341 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003342 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003343 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003344
Joerg Roedel17b124b2011-04-06 18:01:35 +02003345 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003346
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003347 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003348}
3349
Joerg Roedel645c4c82008-12-02 20:05:50 +01003350static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3351 unsigned long iova)
3352{
3353 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003354 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003355 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003356 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003357
Joerg Roedel132bd682011-11-17 14:18:46 +01003358 if (domain->mode == PAGE_MODE_NONE)
3359 return iova;
3360
Joerg Roedel24cd7722010-01-19 17:27:39 +01003361 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003362
Joerg Roedela6d41a42009-09-02 17:08:55 +02003363 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003364 return 0;
3365
Joerg Roedelf03152b2010-01-21 16:15:24 +01003366 if (PM_PTE_LEVEL(*pte) == 0)
3367 offset_mask = PAGE_SIZE - 1;
3368 else
3369 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3370
3371 __pte = *pte & PM_ADDR_MASK;
3372 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003373
3374 return paddr;
3375}
3376
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003377static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3378 unsigned long cap)
3379{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003380 switch (cap) {
3381 case IOMMU_CAP_CACHE_COHERENCY:
3382 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003383 case IOMMU_CAP_INTR_REMAP:
3384 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003385 }
3386
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003387 return 0;
3388}
3389
Joerg Roedel26961ef2008-12-03 17:00:17 +01003390static struct iommu_ops amd_iommu_ops = {
3391 .domain_init = amd_iommu_domain_init,
3392 .domain_destroy = amd_iommu_domain_destroy,
3393 .attach_dev = amd_iommu_attach_device,
3394 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003395 .map = amd_iommu_map,
3396 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003397 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003398 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003399 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003400};
3401
Joerg Roedel0feae532009-08-26 15:26:30 +02003402/*****************************************************************************
3403 *
3404 * The next functions do a basic initialization of IOMMU for pass through
3405 * mode
3406 *
3407 * In passthrough mode the IOMMU is initialized and enabled but not used for
3408 * DMA-API translation.
3409 *
3410 *****************************************************************************/
3411
3412int __init amd_iommu_init_passthrough(void)
3413{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003414 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003415 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003416 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003417 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003418 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003419
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003420 ret = alloc_passthrough_domain();
3421 if (ret)
3422 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003423
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003424 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003425 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003426 continue;
3427
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003428 dev_data = get_dev_data(&dev->dev);
3429 dev_data->passthrough = true;
3430
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003431 devid = get_device_id(&dev->dev);
3432
Joerg Roedel15898bb2009-11-24 15:39:42 +01003433 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003434 if (!iommu)
3435 continue;
3436
Joerg Roedel15898bb2009-11-24 15:39:42 +01003437 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003438 }
3439
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003440 amd_iommu_stats_init();
3441
Joerg Roedel0feae532009-08-26 15:26:30 +02003442 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3443
3444 return 0;
3445}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003446
3447/* IOMMUv2 specific functions */
3448int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3449{
3450 return atomic_notifier_chain_register(&ppr_notifier, nb);
3451}
3452EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3453
3454int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3455{
3456 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3457}
3458EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003459
3460void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3461{
3462 struct protection_domain *domain = dom->priv;
3463 unsigned long flags;
3464
3465 spin_lock_irqsave(&domain->lock, flags);
3466
3467 /* Update data structure */
3468 domain->mode = PAGE_MODE_NONE;
3469 domain->updated = true;
3470
3471 /* Make changes visible to IOMMUs */
3472 update_domain(domain);
3473
3474 /* Page-table is not visible to IOMMU anymore, so free it */
3475 free_pagetable(domain);
3476
3477 spin_unlock_irqrestore(&domain->lock, flags);
3478}
3479EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003480
3481int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3482{
3483 struct protection_domain *domain = dom->priv;
3484 unsigned long flags;
3485 int levels, ret;
3486
3487 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3488 return -EINVAL;
3489
3490 /* Number of GCR3 table levels required */
3491 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3492 levels += 1;
3493
3494 if (levels > amd_iommu_max_glx_val)
3495 return -EINVAL;
3496
3497 spin_lock_irqsave(&domain->lock, flags);
3498
3499 /*
3500 * Save us all sanity checks whether devices already in the
3501 * domain support IOMMUv2. Just force that the domain has no
3502 * devices attached when it is switched into IOMMUv2 mode.
3503 */
3504 ret = -EBUSY;
3505 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3506 goto out;
3507
3508 ret = -ENOMEM;
3509 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3510 if (domain->gcr3_tbl == NULL)
3511 goto out;
3512
3513 domain->glx = levels;
3514 domain->flags |= PD_IOMMUV2_MASK;
3515 domain->updated = true;
3516
3517 update_domain(domain);
3518
3519 ret = 0;
3520
3521out:
3522 spin_unlock_irqrestore(&domain->lock, flags);
3523
3524 return ret;
3525}
3526EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003527
3528static int __flush_pasid(struct protection_domain *domain, int pasid,
3529 u64 address, bool size)
3530{
3531 struct iommu_dev_data *dev_data;
3532 struct iommu_cmd cmd;
3533 int i, ret;
3534
3535 if (!(domain->flags & PD_IOMMUV2_MASK))
3536 return -EINVAL;
3537
3538 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3539
3540 /*
3541 * IOMMU TLB needs to be flushed before Device TLB to
3542 * prevent device TLB refill from IOMMU TLB
3543 */
3544 for (i = 0; i < amd_iommus_present; ++i) {
3545 if (domain->dev_iommu[i] == 0)
3546 continue;
3547
3548 ret = iommu_queue_command(amd_iommus[i], &cmd);
3549 if (ret != 0)
3550 goto out;
3551 }
3552
3553 /* Wait until IOMMU TLB flushes are complete */
3554 domain_flush_complete(domain);
3555
3556 /* Now flush device TLBs */
3557 list_for_each_entry(dev_data, &domain->dev_list, list) {
3558 struct amd_iommu *iommu;
3559 int qdep;
3560
3561 BUG_ON(!dev_data->ats.enabled);
3562
3563 qdep = dev_data->ats.qdep;
3564 iommu = amd_iommu_rlookup_table[dev_data->devid];
3565
3566 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3567 qdep, address, size);
3568
3569 ret = iommu_queue_command(iommu, &cmd);
3570 if (ret != 0)
3571 goto out;
3572 }
3573
3574 /* Wait until all device TLBs are flushed */
3575 domain_flush_complete(domain);
3576
3577 ret = 0;
3578
3579out:
3580
3581 return ret;
3582}
3583
3584static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3585 u64 address)
3586{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003587 INC_STATS_COUNTER(invalidate_iotlb);
3588
Joerg Roedel22e266c2011-11-21 15:59:08 +01003589 return __flush_pasid(domain, pasid, address, false);
3590}
3591
3592int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3593 u64 address)
3594{
3595 struct protection_domain *domain = dom->priv;
3596 unsigned long flags;
3597 int ret;
3598
3599 spin_lock_irqsave(&domain->lock, flags);
3600 ret = __amd_iommu_flush_page(domain, pasid, address);
3601 spin_unlock_irqrestore(&domain->lock, flags);
3602
3603 return ret;
3604}
3605EXPORT_SYMBOL(amd_iommu_flush_page);
3606
3607static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3608{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003609 INC_STATS_COUNTER(invalidate_iotlb_all);
3610
Joerg Roedel22e266c2011-11-21 15:59:08 +01003611 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3612 true);
3613}
3614
3615int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
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_tlb(domain, pasid);
3623 spin_unlock_irqrestore(&domain->lock, flags);
3624
3625 return ret;
3626}
3627EXPORT_SYMBOL(amd_iommu_flush_tlb);
3628
Joerg Roedelb16137b2011-11-21 16:50:23 +01003629static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3630{
3631 int index;
3632 u64 *pte;
3633
3634 while (true) {
3635
3636 index = (pasid >> (9 * level)) & 0x1ff;
3637 pte = &root[index];
3638
3639 if (level == 0)
3640 break;
3641
3642 if (!(*pte & GCR3_VALID)) {
3643 if (!alloc)
3644 return NULL;
3645
3646 root = (void *)get_zeroed_page(GFP_ATOMIC);
3647 if (root == NULL)
3648 return NULL;
3649
3650 *pte = __pa(root) | GCR3_VALID;
3651 }
3652
3653 root = __va(*pte & PAGE_MASK);
3654
3655 level -= 1;
3656 }
3657
3658 return pte;
3659}
3660
3661static int __set_gcr3(struct protection_domain *domain, int pasid,
3662 unsigned long cr3)
3663{
3664 u64 *pte;
3665
3666 if (domain->mode != PAGE_MODE_NONE)
3667 return -EINVAL;
3668
3669 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3670 if (pte == NULL)
3671 return -ENOMEM;
3672
3673 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3674
3675 return __amd_iommu_flush_tlb(domain, pasid);
3676}
3677
3678static int __clear_gcr3(struct protection_domain *domain, int pasid)
3679{
3680 u64 *pte;
3681
3682 if (domain->mode != PAGE_MODE_NONE)
3683 return -EINVAL;
3684
3685 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3686 if (pte == NULL)
3687 return 0;
3688
3689 *pte = 0;
3690
3691 return __amd_iommu_flush_tlb(domain, pasid);
3692}
3693
3694int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3695 unsigned long cr3)
3696{
3697 struct protection_domain *domain = dom->priv;
3698 unsigned long flags;
3699 int ret;
3700
3701 spin_lock_irqsave(&domain->lock, flags);
3702 ret = __set_gcr3(domain, pasid, cr3);
3703 spin_unlock_irqrestore(&domain->lock, flags);
3704
3705 return ret;
3706}
3707EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3708
3709int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3710{
3711 struct protection_domain *domain = dom->priv;
3712 unsigned long flags;
3713 int ret;
3714
3715 spin_lock_irqsave(&domain->lock, flags);
3716 ret = __clear_gcr3(domain, pasid);
3717 spin_unlock_irqrestore(&domain->lock, flags);
3718
3719 return ret;
3720}
3721EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003722
3723int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3724 int status, int tag)
3725{
3726 struct iommu_dev_data *dev_data;
3727 struct amd_iommu *iommu;
3728 struct iommu_cmd cmd;
3729
Joerg Roedel399be2f2011-12-01 16:53:47 +01003730 INC_STATS_COUNTER(complete_ppr);
3731
Joerg Roedelc99afa22011-11-21 18:19:25 +01003732 dev_data = get_dev_data(&pdev->dev);
3733 iommu = amd_iommu_rlookup_table[dev_data->devid];
3734
3735 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3736 tag, dev_data->pri_tlp);
3737
3738 return iommu_queue_command(iommu, &cmd);
3739}
3740EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003741
3742struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3743{
3744 struct protection_domain *domain;
3745
3746 domain = get_domain(&pdev->dev);
3747 if (IS_ERR(domain))
3748 return NULL;
3749
3750 /* Only return IOMMUv2 domains */
3751 if (!(domain->flags & PD_IOMMUV2_MASK))
3752 return NULL;
3753
3754 return domain->iommu_domain;
3755}
3756EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003757
3758void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3759{
3760 struct iommu_dev_data *dev_data;
3761
3762 if (!amd_iommu_v2_supported())
3763 return;
3764
3765 dev_data = get_dev_data(&pdev->dev);
3766 dev_data->errata |= (1 << erratum);
3767}
3768EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003769
3770int amd_iommu_device_info(struct pci_dev *pdev,
3771 struct amd_iommu_device_info *info)
3772{
3773 int max_pasids;
3774 int pos;
3775
3776 if (pdev == NULL || info == NULL)
3777 return -EINVAL;
3778
3779 if (!amd_iommu_v2_supported())
3780 return -EINVAL;
3781
3782 memset(info, 0, sizeof(*info));
3783
3784 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3785 if (pos)
3786 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3787
3788 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3789 if (pos)
3790 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3791
3792 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3793 if (pos) {
3794 int features;
3795
3796 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3797 max_pasids = min(max_pasids, (1 << 20));
3798
3799 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3800 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3801
3802 features = pci_pasid_features(pdev);
3803 if (features & PCI_PASID_CAP_EXEC)
3804 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3805 if (features & PCI_PASID_CAP_PRIV)
3806 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3807 }
3808
3809 return 0;
3810}
3811EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003812
3813#ifdef CONFIG_IRQ_REMAP
3814
3815/*****************************************************************************
3816 *
3817 * Interrupt Remapping Implementation
3818 *
3819 *****************************************************************************/
3820
3821union irte {
3822 u32 val;
3823 struct {
3824 u32 valid : 1,
3825 no_fault : 1,
3826 int_type : 3,
3827 rq_eoi : 1,
3828 dm : 1,
3829 rsvd_1 : 1,
3830 destination : 8,
3831 vector : 8,
3832 rsvd_2 : 8;
3833 } fields;
3834};
3835
3836#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3837#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3838#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3839#define DTE_IRQ_REMAP_ENABLE 1ULL
3840
3841static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3842{
3843 u64 dte;
3844
3845 dte = amd_iommu_dev_table[devid].data[2];
3846 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3847 dte |= virt_to_phys(table->table);
3848 dte |= DTE_IRQ_REMAP_INTCTL;
3849 dte |= DTE_IRQ_TABLE_LEN;
3850 dte |= DTE_IRQ_REMAP_ENABLE;
3851
3852 amd_iommu_dev_table[devid].data[2] = dte;
3853}
3854
3855#define IRTE_ALLOCATED (~1U)
3856
3857static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3858{
3859 struct irq_remap_table *table = NULL;
3860 struct amd_iommu *iommu;
3861 unsigned long flags;
3862 u16 alias;
3863
3864 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3865
3866 iommu = amd_iommu_rlookup_table[devid];
3867 if (!iommu)
3868 goto out_unlock;
3869
3870 table = irq_lookup_table[devid];
3871 if (table)
3872 goto out;
3873
3874 alias = amd_iommu_alias_table[devid];
3875 table = irq_lookup_table[alias];
3876 if (table) {
3877 irq_lookup_table[devid] = table;
3878 set_dte_irq_entry(devid, table);
3879 iommu_flush_dte(iommu, devid);
3880 goto out;
3881 }
3882
3883 /* Nothing there yet, allocate new irq remapping table */
3884 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3885 if (!table)
3886 goto out;
3887
3888 if (ioapic)
3889 /* Keep the first 32 indexes free for IOAPIC interrupts */
3890 table->min_index = 32;
3891
3892 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3893 if (!table->table) {
3894 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003895 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003896 goto out;
3897 }
3898
3899 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3900
3901 if (ioapic) {
3902 int i;
3903
3904 for (i = 0; i < 32; ++i)
3905 table->table[i] = IRTE_ALLOCATED;
3906 }
3907
3908 irq_lookup_table[devid] = table;
3909 set_dte_irq_entry(devid, table);
3910 iommu_flush_dte(iommu, devid);
3911 if (devid != alias) {
3912 irq_lookup_table[alias] = table;
3913 set_dte_irq_entry(devid, table);
3914 iommu_flush_dte(iommu, alias);
3915 }
3916
3917out:
3918 iommu_completion_wait(iommu);
3919
3920out_unlock:
3921 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3922
3923 return table;
3924}
3925
3926static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3927{
3928 struct irq_remap_table *table;
3929 unsigned long flags;
3930 int index, c;
3931
3932 table = get_irq_table(devid, false);
3933 if (!table)
3934 return -ENODEV;
3935
3936 spin_lock_irqsave(&table->lock, flags);
3937
3938 /* Scan table for free entries */
3939 for (c = 0, index = table->min_index;
3940 index < MAX_IRQS_PER_TABLE;
3941 ++index) {
3942 if (table->table[index] == 0)
3943 c += 1;
3944 else
3945 c = 0;
3946
3947 if (c == count) {
3948 struct irq_2_iommu *irte_info;
3949
3950 for (; c != 0; --c)
3951 table->table[index - c + 1] = IRTE_ALLOCATED;
3952
3953 index -= count - 1;
3954
3955 irte_info = &cfg->irq_2_iommu;
3956 irte_info->sub_handle = devid;
3957 irte_info->irte_index = index;
3958 irte_info->iommu = (void *)cfg;
3959
3960 goto out;
3961 }
3962 }
3963
3964 index = -ENOSPC;
3965
3966out:
3967 spin_unlock_irqrestore(&table->lock, flags);
3968
3969 return index;
3970}
3971
3972static int get_irte(u16 devid, int index, union irte *irte)
3973{
3974 struct irq_remap_table *table;
3975 unsigned long flags;
3976
3977 table = get_irq_table(devid, false);
3978 if (!table)
3979 return -ENOMEM;
3980
3981 spin_lock_irqsave(&table->lock, flags);
3982 irte->val = table->table[index];
3983 spin_unlock_irqrestore(&table->lock, flags);
3984
3985 return 0;
3986}
3987
3988static int modify_irte(u16 devid, int index, union irte irte)
3989{
3990 struct irq_remap_table *table;
3991 struct amd_iommu *iommu;
3992 unsigned long flags;
3993
3994 iommu = amd_iommu_rlookup_table[devid];
3995 if (iommu == NULL)
3996 return -EINVAL;
3997
3998 table = get_irq_table(devid, false);
3999 if (!table)
4000 return -ENOMEM;
4001
4002 spin_lock_irqsave(&table->lock, flags);
4003 table->table[index] = irte.val;
4004 spin_unlock_irqrestore(&table->lock, flags);
4005
4006 iommu_flush_irt(iommu, devid);
4007 iommu_completion_wait(iommu);
4008
4009 return 0;
4010}
4011
4012static void free_irte(u16 devid, int index)
4013{
4014 struct irq_remap_table *table;
4015 struct amd_iommu *iommu;
4016 unsigned long flags;
4017
4018 iommu = amd_iommu_rlookup_table[devid];
4019 if (iommu == NULL)
4020 return;
4021
4022 table = get_irq_table(devid, false);
4023 if (!table)
4024 return;
4025
4026 spin_lock_irqsave(&table->lock, flags);
4027 table->table[index] = 0;
4028 spin_unlock_irqrestore(&table->lock, flags);
4029
4030 iommu_flush_irt(iommu, devid);
4031 iommu_completion_wait(iommu);
4032}
4033
Joerg Roedel5527de72012-06-26 11:17:32 +02004034static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4035 unsigned int destination, int vector,
4036 struct io_apic_irq_attr *attr)
4037{
4038 struct irq_remap_table *table;
4039 struct irq_2_iommu *irte_info;
4040 struct irq_cfg *cfg;
4041 union irte irte;
4042 int ioapic_id;
4043 int index;
4044 int devid;
4045 int ret;
4046
4047 cfg = irq_get_chip_data(irq);
4048 if (!cfg)
4049 return -EINVAL;
4050
4051 irte_info = &cfg->irq_2_iommu;
4052 ioapic_id = mpc_ioapic_id(attr->ioapic);
4053 devid = get_ioapic_devid(ioapic_id);
4054
4055 if (devid < 0)
4056 return devid;
4057
4058 table = get_irq_table(devid, true);
4059 if (table == NULL)
4060 return -ENOMEM;
4061
4062 index = attr->ioapic_pin;
4063
4064 /* Setup IRQ remapping info */
4065 irte_info->sub_handle = devid;
4066 irte_info->irte_index = index;
4067 irte_info->iommu = (void *)cfg;
4068
4069 /* Setup IRTE for IOMMU */
4070 irte.val = 0;
4071 irte.fields.vector = vector;
4072 irte.fields.int_type = apic->irq_delivery_mode;
4073 irte.fields.destination = destination;
4074 irte.fields.dm = apic->irq_dest_mode;
4075 irte.fields.valid = 1;
4076
4077 ret = modify_irte(devid, index, irte);
4078 if (ret)
4079 return ret;
4080
4081 /* Setup IOAPIC entry */
4082 memset(entry, 0, sizeof(*entry));
4083
4084 entry->vector = index;
4085 entry->mask = 0;
4086 entry->trigger = attr->trigger;
4087 entry->polarity = attr->polarity;
4088
4089 /*
4090 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004091 */
4092 if (attr->trigger)
4093 entry->mask = 1;
4094
4095 return 0;
4096}
4097
4098static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4099 bool force)
4100{
4101 struct irq_2_iommu *irte_info;
4102 unsigned int dest, irq;
4103 struct irq_cfg *cfg;
4104 union irte irte;
4105 int err;
4106
4107 if (!config_enabled(CONFIG_SMP))
4108 return -1;
4109
4110 cfg = data->chip_data;
4111 irq = data->irq;
4112 irte_info = &cfg->irq_2_iommu;
4113
4114 if (!cpumask_intersects(mask, cpu_online_mask))
4115 return -EINVAL;
4116
4117 if (get_irte(irte_info->sub_handle, irte_info->irte_index, &irte))
4118 return -EBUSY;
4119
4120 if (assign_irq_vector(irq, cfg, mask))
4121 return -EBUSY;
4122
4123 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4124 if (err) {
4125 if (assign_irq_vector(irq, cfg, data->affinity))
4126 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4127 return err;
4128 }
4129
4130 irte.fields.vector = cfg->vector;
4131 irte.fields.destination = dest;
4132
4133 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4134
4135 if (cfg->move_in_progress)
4136 send_cleanup_vector(cfg);
4137
4138 cpumask_copy(data->affinity, mask);
4139
4140 return 0;
4141}
4142
4143static int free_irq(int irq)
4144{
4145 struct irq_2_iommu *irte_info;
4146 struct irq_cfg *cfg;
4147
4148 cfg = irq_get_chip_data(irq);
4149 if (!cfg)
4150 return -EINVAL;
4151
4152 irte_info = &cfg->irq_2_iommu;
4153
4154 free_irte(irte_info->sub_handle, irte_info->irte_index);
4155
4156 return 0;
4157}
4158
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004159static void compose_msi_msg(struct pci_dev *pdev,
4160 unsigned int irq, unsigned int dest,
4161 struct msi_msg *msg, u8 hpet_id)
4162{
4163 struct irq_2_iommu *irte_info;
4164 struct irq_cfg *cfg;
4165 union irte irte;
4166
4167 cfg = irq_get_chip_data(irq);
4168 if (!cfg)
4169 return;
4170
4171 irte_info = &cfg->irq_2_iommu;
4172
4173 irte.val = 0;
4174 irte.fields.vector = cfg->vector;
4175 irte.fields.int_type = apic->irq_delivery_mode;
4176 irte.fields.destination = dest;
4177 irte.fields.dm = apic->irq_dest_mode;
4178 irte.fields.valid = 1;
4179
4180 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4181
4182 msg->address_hi = MSI_ADDR_BASE_HI;
4183 msg->address_lo = MSI_ADDR_BASE_LO;
4184 msg->data = irte_info->irte_index;
4185}
4186
4187static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4188{
4189 struct irq_cfg *cfg;
4190 int index;
4191 u16 devid;
4192
4193 if (!pdev)
4194 return -EINVAL;
4195
4196 cfg = irq_get_chip_data(irq);
4197 if (!cfg)
4198 return -EINVAL;
4199
4200 devid = get_device_id(&pdev->dev);
4201 index = alloc_irq_index(cfg, devid, nvec);
4202
4203 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4204}
4205
4206static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4207 int index, int offset)
4208{
4209 struct irq_2_iommu *irte_info;
4210 struct irq_cfg *cfg;
4211 u16 devid;
4212
4213 if (!pdev)
4214 return -EINVAL;
4215
4216 cfg = irq_get_chip_data(irq);
4217 if (!cfg)
4218 return -EINVAL;
4219
4220 if (index >= MAX_IRQS_PER_TABLE)
4221 return 0;
4222
4223 devid = get_device_id(&pdev->dev);
4224 irte_info = &cfg->irq_2_iommu;
4225
4226 irte_info->sub_handle = devid;
4227 irte_info->irte_index = index + offset;
4228 irte_info->iommu = (void *)cfg;
4229
4230 return 0;
4231}
4232
Joerg Roedeld9761952012-06-26 16:00:08 +02004233static int setup_hpet_msi(unsigned int irq, unsigned int id)
4234{
4235 struct irq_2_iommu *irte_info;
4236 struct irq_cfg *cfg;
4237 int index, devid;
4238
4239 cfg = irq_get_chip_data(irq);
4240 if (!cfg)
4241 return -EINVAL;
4242
4243 irte_info = &cfg->irq_2_iommu;
4244 devid = get_hpet_devid(id);
4245 if (devid < 0)
4246 return devid;
4247
4248 index = alloc_irq_index(cfg, devid, 1);
4249 if (index < 0)
4250 return index;
4251
4252 irte_info->sub_handle = devid;
4253 irte_info->irte_index = index;
4254 irte_info->iommu = (void *)cfg;
4255
4256 return 0;
4257}
4258
Joerg Roedel6b474b82012-06-26 16:46:04 +02004259struct irq_remap_ops amd_iommu_irq_ops = {
4260 .supported = amd_iommu_supported,
4261 .prepare = amd_iommu_prepare,
4262 .enable = amd_iommu_enable,
4263 .disable = amd_iommu_disable,
4264 .reenable = amd_iommu_reenable,
4265 .enable_faulting = amd_iommu_enable_faulting,
4266 .setup_ioapic_entry = setup_ioapic_entry,
4267 .set_affinity = set_affinity,
4268 .free_irq = free_irq,
4269 .compose_msi_msg = compose_msi_msg,
4270 .msi_alloc_irq = msi_alloc_irq,
4271 .msi_setup_irq = msi_setup_irq,
4272 .setup_hpet_msi = setup_hpet_msi,
4273};
Joerg Roedel2b324502012-06-21 16:29:10 +02004274#endif