blob: cb63cc5d94d71d70802a0bbea55575b5b3da5f34 [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
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600143 if (dev_data->group)
144 iommu_group_put(dev_data->group);
145
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200146 kfree(dev_data);
147}
148
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200149static struct iommu_dev_data *search_dev_data(u16 devid)
150{
151 struct iommu_dev_data *dev_data;
152 unsigned long flags;
153
154 spin_lock_irqsave(&dev_data_list_lock, flags);
155 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
156 if (dev_data->devid == devid)
157 goto out_unlock;
158 }
159
160 dev_data = NULL;
161
162out_unlock:
163 spin_unlock_irqrestore(&dev_data_list_lock, flags);
164
165 return dev_data;
166}
167
168static struct iommu_dev_data *find_dev_data(u16 devid)
169{
170 struct iommu_dev_data *dev_data;
171
172 dev_data = search_dev_data(devid);
173
174 if (dev_data == NULL)
175 dev_data = alloc_dev_data(devid);
176
177 return dev_data;
178}
179
Joerg Roedel15898bb2009-11-24 15:39:42 +0100180static inline u16 get_device_id(struct device *dev)
181{
182 struct pci_dev *pdev = to_pci_dev(dev);
183
184 return calc_devid(pdev->bus->number, pdev->devfn);
185}
186
Joerg Roedel657cbb62009-11-23 15:26:46 +0100187static struct iommu_dev_data *get_dev_data(struct device *dev)
188{
189 return dev->archdata.iommu;
190}
191
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100192static bool pci_iommuv2_capable(struct pci_dev *pdev)
193{
194 static const int caps[] = {
195 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100196 PCI_EXT_CAP_ID_PRI,
197 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100198 };
199 int i, pos;
200
201 for (i = 0; i < 3; ++i) {
202 pos = pci_find_ext_capability(pdev, caps[i]);
203 if (pos == 0)
204 return false;
205 }
206
207 return true;
208}
209
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100210static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
211{
212 struct iommu_dev_data *dev_data;
213
214 dev_data = get_dev_data(&pdev->dev);
215
216 return dev_data->errata & (1 << erratum) ? true : false;
217}
218
Joerg Roedel71c70982009-11-24 16:43:06 +0100219/*
220 * In this function the list of preallocated protection domains is traversed to
221 * find the domain for a specific device
222 */
223static struct dma_ops_domain *find_protection_domain(u16 devid)
224{
225 struct dma_ops_domain *entry, *ret = NULL;
226 unsigned long flags;
227 u16 alias = amd_iommu_alias_table[devid];
228
229 if (list_empty(&iommu_pd_list))
230 return NULL;
231
232 spin_lock_irqsave(&iommu_pd_list_lock, flags);
233
234 list_for_each_entry(entry, &iommu_pd_list, list) {
235 if (entry->target_dev == devid ||
236 entry->target_dev == alias) {
237 ret = entry;
238 break;
239 }
240 }
241
242 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
243
244 return ret;
245}
246
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100247/*
248 * This function checks if the driver got a valid device from the caller to
249 * avoid dereferencing invalid pointers.
250 */
251static bool check_device(struct device *dev)
252{
253 u16 devid;
254
255 if (!dev || !dev->dma_mask)
256 return false;
257
258 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100259 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100260 return false;
261
262 devid = get_device_id(dev);
263
264 /* Out of our scope? */
265 if (devid > amd_iommu_last_bdf)
266 return false;
267
268 if (amd_iommu_rlookup_table[devid] == NULL)
269 return false;
270
271 return true;
272}
273
Alex Williamson664b6002012-05-30 14:19:31 -0600274static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
275{
276 pci_dev_put(*from);
277 *from = to;
278}
279
Alex Williamson2bff6a52012-10-08 22:49:48 -0600280static struct pci_bus *find_hosted_bus(struct pci_bus *bus)
281{
282 while (!bus->self) {
283 if (!pci_is_root_bus(bus))
284 bus = bus->parent;
285 else
286 return ERR_PTR(-ENODEV);
287 }
288
289 return bus;
290}
291
Alex Williamson664b6002012-05-30 14:19:31 -0600292#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
293
Alex Williamson2851db22012-10-08 22:49:41 -0600294static struct pci_dev *get_isolation_root(struct pci_dev *pdev)
Joerg Roedel657cbb62009-11-23 15:26:46 +0100295{
Alex Williamson2851db22012-10-08 22:49:41 -0600296 struct pci_dev *dma_pdev = pdev;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600297
Alex Williamson31fe9432012-08-04 12:09:03 -0600298 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600299 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
300
Alex Williamson31fe9432012-08-04 12:09:03 -0600301 /*
302 * If it's a multifunction device that does not support our
303 * required ACS flags, add to the same group as function 0.
304 */
Alex Williamson664b6002012-05-30 14:19:31 -0600305 if (dma_pdev->multifunction &&
306 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
307 swap_pci_ref(&dma_pdev,
308 pci_get_slot(dma_pdev->bus,
309 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
310 0)));
311
Alex Williamson31fe9432012-08-04 12:09:03 -0600312 /*
313 * Devices on the root bus go through the iommu. If that's not us,
314 * find the next upstream device and test ACS up to the root bus.
315 * Finding the next device may require skipping virtual buses.
316 */
Alex Williamson664b6002012-05-30 14:19:31 -0600317 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson2bff6a52012-10-08 22:49:48 -0600318 struct pci_bus *bus = find_hosted_bus(dma_pdev->bus);
319 if (IS_ERR(bus))
320 break;
Alex Williamson31fe9432012-08-04 12:09:03 -0600321
322 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600323 break;
324
Alex Williamson31fe9432012-08-04 12:09:03 -0600325 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200326 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100327
Alex Williamson2851db22012-10-08 22:49:41 -0600328 return dma_pdev;
329}
330
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600331static int use_pdev_iommu_group(struct pci_dev *pdev, struct device *dev)
332{
333 struct iommu_group *group = iommu_group_get(&pdev->dev);
334 int ret;
335
336 if (!group) {
337 group = iommu_group_alloc();
338 if (IS_ERR(group))
339 return PTR_ERR(group);
340
341 WARN_ON(&pdev->dev != dev);
342 }
343
344 ret = iommu_group_add_device(group, dev);
345 iommu_group_put(group);
346 return ret;
347}
348
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600349static int use_dev_data_iommu_group(struct iommu_dev_data *dev_data,
350 struct device *dev)
351{
352 if (!dev_data->group) {
353 struct iommu_group *group = iommu_group_alloc();
354 if (IS_ERR(group))
355 return PTR_ERR(group);
356
357 dev_data->group = group;
358 }
359
360 return iommu_group_add_device(dev_data->group, dev);
361}
362
Alex Williamson2851db22012-10-08 22:49:41 -0600363static int init_iommu_group(struct device *dev)
364{
365 struct iommu_dev_data *dev_data;
366 struct iommu_group *group;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600367 struct pci_dev *dma_pdev;
Alex Williamson2851db22012-10-08 22:49:41 -0600368 int ret;
369
370 group = iommu_group_get(dev);
371 if (group) {
372 iommu_group_put(group);
373 return 0;
374 }
375
376 dev_data = find_dev_data(get_device_id(dev));
377 if (!dev_data)
378 return -ENOMEM;
379
380 if (dev_data->alias_data) {
381 u16 alias;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600382 struct pci_bus *bus;
Alex Williamson2851db22012-10-08 22:49:41 -0600383
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600384 if (dev_data->alias_data->group)
385 goto use_group;
386
387 /*
388 * If the alias device exists, it's effectively just a first
389 * level quirk for finding the DMA source.
390 */
Alex Williamson2851db22012-10-08 22:49:41 -0600391 alias = amd_iommu_alias_table[dev_data->devid];
392 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600393 if (dma_pdev) {
394 dma_pdev = get_isolation_root(dma_pdev);
395 goto use_pdev;
396 }
397
398 /*
399 * If the alias is virtual, try to find a parent device
400 * and test whether the IOMMU group is actualy rooted above
401 * the alias. Be careful to also test the parent device if
402 * we think the alias is the root of the group.
403 */
404 bus = pci_find_bus(0, alias >> 8);
405 if (!bus)
406 goto use_group;
407
408 bus = find_hosted_bus(bus);
409 if (IS_ERR(bus) || !bus->self)
410 goto use_group;
411
412 dma_pdev = get_isolation_root(pci_dev_get(bus->self));
413 if (dma_pdev != bus->self || (dma_pdev->multifunction &&
414 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)))
415 goto use_pdev;
416
417 pci_dev_put(dma_pdev);
418 goto use_group;
Alex Williamson2851db22012-10-08 22:49:41 -0600419 }
420
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600421 dma_pdev = get_isolation_root(pci_dev_get(to_pci_dev(dev)));
422use_pdev:
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600423 ret = use_pdev_iommu_group(dma_pdev, dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600424 pci_dev_put(dma_pdev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600425 return ret;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600426use_group:
427 return use_dev_data_iommu_group(dev_data->alias_data, dev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600428}
429
430static int iommu_init_device(struct device *dev)
431{
432 struct pci_dev *pdev = to_pci_dev(dev);
433 struct iommu_dev_data *dev_data;
434 u16 alias;
435 int ret;
436
437 if (dev->archdata.iommu)
438 return 0;
439
440 dev_data = find_dev_data(get_device_id(dev));
441 if (!dev_data)
442 return -ENOMEM;
443
444 alias = amd_iommu_alias_table[dev_data->devid];
445 if (alias != dev_data->devid) {
446 struct iommu_dev_data *alias_data;
447
448 alias_data = find_dev_data(alias);
449 if (alias_data == NULL) {
450 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
451 dev_name(dev));
452 free_dev_data(dev_data);
453 return -ENOTSUPP;
454 }
455 dev_data->alias_data = alias_data;
456 }
457
458 ret = init_iommu_group(dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600459 if (ret)
460 return ret;
461
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100462 if (pci_iommuv2_capable(pdev)) {
463 struct amd_iommu *iommu;
464
465 iommu = amd_iommu_rlookup_table[dev_data->devid];
466 dev_data->iommu_v2 = iommu->is_iommu_v2;
467 }
468
Joerg Roedel657cbb62009-11-23 15:26:46 +0100469 dev->archdata.iommu = dev_data;
470
Joerg Roedel657cbb62009-11-23 15:26:46 +0100471 return 0;
472}
473
Joerg Roedel26018872011-06-06 16:50:14 +0200474static void iommu_ignore_device(struct device *dev)
475{
476 u16 devid, alias;
477
478 devid = get_device_id(dev);
479 alias = amd_iommu_alias_table[devid];
480
481 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
482 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
483
484 amd_iommu_rlookup_table[devid] = NULL;
485 amd_iommu_rlookup_table[alias] = NULL;
486}
487
Joerg Roedel657cbb62009-11-23 15:26:46 +0100488static void iommu_uninit_device(struct device *dev)
489{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600490 iommu_group_remove_device(dev);
491
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200492 /*
493 * Nothing to do here - we keep dev_data around for unplugged devices
494 * and reuse it when the device is re-plugged - not doing so would
495 * introduce a ton of races.
496 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100497}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100498
499void __init amd_iommu_uninit_devices(void)
500{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200501 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100502 struct pci_dev *pdev = NULL;
503
504 for_each_pci_dev(pdev) {
505
506 if (!check_device(&pdev->dev))
507 continue;
508
509 iommu_uninit_device(&pdev->dev);
510 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200511
512 /* Free all of our dev_data structures */
513 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
514 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100515}
516
517int __init amd_iommu_init_devices(void)
518{
519 struct pci_dev *pdev = NULL;
520 int ret = 0;
521
522 for_each_pci_dev(pdev) {
523
524 if (!check_device(&pdev->dev))
525 continue;
526
527 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200528 if (ret == -ENOTSUPP)
529 iommu_ignore_device(&pdev->dev);
530 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100531 goto out_free;
532 }
533
534 return 0;
535
536out_free:
537
538 amd_iommu_uninit_devices();
539
540 return ret;
541}
Joerg Roedel7f265082008-12-12 13:50:21 +0100542#ifdef CONFIG_AMD_IOMMU_STATS
543
544/*
545 * Initialization code for statistics collection
546 */
547
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100548DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100549DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100550DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100551DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100552DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100553DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100554DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100555DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100556DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100557DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100558DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100559DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100560DECLARE_STATS_COUNTER(complete_ppr);
561DECLARE_STATS_COUNTER(invalidate_iotlb);
562DECLARE_STATS_COUNTER(invalidate_iotlb_all);
563DECLARE_STATS_COUNTER(pri_requests);
564
Joerg Roedel7f265082008-12-12 13:50:21 +0100565static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100566static struct dentry *de_fflush;
567
568static void amd_iommu_stats_add(struct __iommu_counter *cnt)
569{
570 if (stats_dir == NULL)
571 return;
572
573 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
574 &cnt->value);
575}
576
577static void amd_iommu_stats_init(void)
578{
579 stats_dir = debugfs_create_dir("amd-iommu", NULL);
580 if (stats_dir == NULL)
581 return;
582
Joerg Roedel7f265082008-12-12 13:50:21 +0100583 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300584 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100585
586 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100587 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100588 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100589 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100590 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100591 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100592 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100593 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100594 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100595 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100596 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100597 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100598 amd_iommu_stats_add(&complete_ppr);
599 amd_iommu_stats_add(&invalidate_iotlb);
600 amd_iommu_stats_add(&invalidate_iotlb_all);
601 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100602}
603
604#endif
605
Joerg Roedel431b2a22008-07-11 17:14:22 +0200606/****************************************************************************
607 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200608 * Interrupt handling functions
609 *
610 ****************************************************************************/
611
Joerg Roedele3e59872009-09-03 14:02:10 +0200612static void dump_dte_entry(u16 devid)
613{
614 int i;
615
Joerg Roedelee6c2862011-11-09 12:06:03 +0100616 for (i = 0; i < 4; ++i)
617 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200618 amd_iommu_dev_table[devid].data[i]);
619}
620
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200621static void dump_command(unsigned long phys_addr)
622{
623 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
624 int i;
625
626 for (i = 0; i < 4; ++i)
627 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
628}
629
Joerg Roedela345b232009-09-03 15:01:43 +0200630static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200631{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200632 int type, devid, domid, flags;
633 volatile u32 *event = __evt;
634 int count = 0;
635 u64 address;
636
637retry:
638 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
639 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
640 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
641 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
642 address = (u64)(((u64)event[3]) << 32) | event[2];
643
644 if (type == 0) {
645 /* Did we hit the erratum? */
646 if (++count == LOOP_TIMEOUT) {
647 pr_err("AMD-Vi: No event written to event log\n");
648 return;
649 }
650 udelay(1);
651 goto retry;
652 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200653
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200654 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200655
656 switch (type) {
657 case EVENT_TYPE_ILL_DEV:
658 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
659 "address=0x%016llx flags=0x%04x]\n",
660 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
661 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200662 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200663 break;
664 case EVENT_TYPE_IO_FAULT:
665 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
666 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
667 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
668 domid, address, flags);
669 break;
670 case EVENT_TYPE_DEV_TAB_ERR:
671 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
672 "address=0x%016llx flags=0x%04x]\n",
673 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
674 address, flags);
675 break;
676 case EVENT_TYPE_PAGE_TAB_ERR:
677 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
678 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
679 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
680 domid, address, flags);
681 break;
682 case EVENT_TYPE_ILL_CMD:
683 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200684 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200685 break;
686 case EVENT_TYPE_CMD_HARD_ERR:
687 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
688 "flags=0x%04x]\n", address, flags);
689 break;
690 case EVENT_TYPE_IOTLB_INV_TO:
691 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
692 "address=0x%016llx]\n",
693 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
694 address);
695 break;
696 case EVENT_TYPE_INV_DEV_REQ:
697 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
698 "address=0x%016llx flags=0x%04x]\n",
699 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
700 address, flags);
701 break;
702 default:
703 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
704 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200705
706 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200707}
708
709static void iommu_poll_events(struct amd_iommu *iommu)
710{
711 u32 head, tail;
712 unsigned long flags;
713
714 spin_lock_irqsave(&iommu->lock, flags);
715
716 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
717 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
718
719 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200720 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200721 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
722 }
723
724 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
725
726 spin_unlock_irqrestore(&iommu->lock, flags);
727}
728
Joerg Roedeleee53532012-06-01 15:20:23 +0200729static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100730{
731 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100732
Joerg Roedel399be2f2011-12-01 16:53:47 +0100733 INC_STATS_COUNTER(pri_requests);
734
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100735 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
736 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
737 return;
738 }
739
740 fault.address = raw[1];
741 fault.pasid = PPR_PASID(raw[0]);
742 fault.device_id = PPR_DEVID(raw[0]);
743 fault.tag = PPR_TAG(raw[0]);
744 fault.flags = PPR_FLAGS(raw[0]);
745
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100746 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
747}
748
749static void iommu_poll_ppr_log(struct amd_iommu *iommu)
750{
751 unsigned long flags;
752 u32 head, tail;
753
754 if (iommu->ppr_log == NULL)
755 return;
756
Joerg Roedeleee53532012-06-01 15:20:23 +0200757 /* enable ppr interrupts again */
758 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
759
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100760 spin_lock_irqsave(&iommu->lock, flags);
761
762 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
763 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
764
765 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200766 volatile u64 *raw;
767 u64 entry[2];
768 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100769
Joerg Roedeleee53532012-06-01 15:20:23 +0200770 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100771
Joerg Roedeleee53532012-06-01 15:20:23 +0200772 /*
773 * Hardware bug: Interrupt may arrive before the entry is
774 * written to memory. If this happens we need to wait for the
775 * entry to arrive.
776 */
777 for (i = 0; i < LOOP_TIMEOUT; ++i) {
778 if (PPR_REQ_TYPE(raw[0]) != 0)
779 break;
780 udelay(1);
781 }
782
783 /* Avoid memcpy function-call overhead */
784 entry[0] = raw[0];
785 entry[1] = raw[1];
786
787 /*
788 * To detect the hardware bug we need to clear the entry
789 * back to zero.
790 */
791 raw[0] = raw[1] = 0UL;
792
793 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100794 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
795 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200796
797 /*
798 * Release iommu->lock because ppr-handling might need to
Frank Arnolddf805ab2012-08-27 19:21:04 +0200799 * re-acquire it
Joerg Roedeleee53532012-06-01 15:20:23 +0200800 */
801 spin_unlock_irqrestore(&iommu->lock, flags);
802
803 /* Handle PPR entry */
804 iommu_handle_ppr_entry(iommu, entry);
805
806 spin_lock_irqsave(&iommu->lock, flags);
807
808 /* Refresh ring-buffer information */
809 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100810 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
811 }
812
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100813 spin_unlock_irqrestore(&iommu->lock, flags);
814}
815
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200816irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200817{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200818 struct amd_iommu *iommu;
819
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100820 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200821 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100822 iommu_poll_ppr_log(iommu);
823 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200824
825 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200826}
827
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200828irqreturn_t amd_iommu_int_handler(int irq, void *data)
829{
830 return IRQ_WAKE_THREAD;
831}
832
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200833/****************************************************************************
834 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200835 * IOMMU command queuing functions
836 *
837 ****************************************************************************/
838
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200839static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200840{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200841 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200842
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200843 while (*sem == 0 && i < LOOP_TIMEOUT) {
844 udelay(1);
845 i += 1;
846 }
847
848 if (i == LOOP_TIMEOUT) {
849 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
850 return -EIO;
851 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200852
853 return 0;
854}
855
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200856static void copy_cmd_to_buffer(struct amd_iommu *iommu,
857 struct iommu_cmd *cmd,
858 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200859{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200860 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200861
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200862 target = iommu->cmd_buf + tail;
863 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200864
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200865 /* Copy command to buffer */
866 memcpy(target, cmd, sizeof(*cmd));
867
868 /* Tell the IOMMU about it */
869 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
870}
871
Joerg Roedel815b33f2011-04-06 17:26:49 +0200872static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200873{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200874 WARN_ON(address & 0x7ULL);
875
Joerg Roedelded46732011-04-06 10:53:48 +0200876 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200877 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
878 cmd->data[1] = upper_32_bits(__pa(address));
879 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200880 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
881}
882
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200883static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
884{
885 memset(cmd, 0, sizeof(*cmd));
886 cmd->data[0] = devid;
887 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
888}
889
Joerg Roedel11b64022011-04-06 11:49:28 +0200890static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
891 size_t size, u16 domid, int pde)
892{
893 u64 pages;
894 int s;
895
896 pages = iommu_num_pages(address, size, PAGE_SIZE);
897 s = 0;
898
899 if (pages > 1) {
900 /*
901 * If we have to flush more than one page, flush all
902 * TLB entries for this domain
903 */
904 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
905 s = 1;
906 }
907
908 address &= PAGE_MASK;
909
910 memset(cmd, 0, sizeof(*cmd));
911 cmd->data[1] |= domid;
912 cmd->data[2] = lower_32_bits(address);
913 cmd->data[3] = upper_32_bits(address);
914 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
915 if (s) /* size bit - we flush more than one 4kb page */
916 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200917 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200918 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
919}
920
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200921static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
922 u64 address, size_t size)
923{
924 u64 pages;
925 int s;
926
927 pages = iommu_num_pages(address, size, PAGE_SIZE);
928 s = 0;
929
930 if (pages > 1) {
931 /*
932 * If we have to flush more than one page, flush all
933 * TLB entries for this domain
934 */
935 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
936 s = 1;
937 }
938
939 address &= PAGE_MASK;
940
941 memset(cmd, 0, sizeof(*cmd));
942 cmd->data[0] = devid;
943 cmd->data[0] |= (qdep & 0xff) << 24;
944 cmd->data[1] = devid;
945 cmd->data[2] = lower_32_bits(address);
946 cmd->data[3] = upper_32_bits(address);
947 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
948 if (s)
949 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
950}
951
Joerg Roedel22e266c2011-11-21 15:59:08 +0100952static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
953 u64 address, bool size)
954{
955 memset(cmd, 0, sizeof(*cmd));
956
957 address &= ~(0xfffULL);
958
959 cmd->data[0] = pasid & PASID_MASK;
960 cmd->data[1] = domid;
961 cmd->data[2] = lower_32_bits(address);
962 cmd->data[3] = upper_32_bits(address);
963 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
964 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
965 if (size)
966 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
967 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
968}
969
970static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
971 int qdep, u64 address, bool size)
972{
973 memset(cmd, 0, sizeof(*cmd));
974
975 address &= ~(0xfffULL);
976
977 cmd->data[0] = devid;
978 cmd->data[0] |= (pasid & 0xff) << 16;
979 cmd->data[0] |= (qdep & 0xff) << 24;
980 cmd->data[1] = devid;
981 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
982 cmd->data[2] = lower_32_bits(address);
983 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
984 cmd->data[3] = upper_32_bits(address);
985 if (size)
986 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
987 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
988}
989
Joerg Roedelc99afa22011-11-21 18:19:25 +0100990static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
991 int status, int tag, bool gn)
992{
993 memset(cmd, 0, sizeof(*cmd));
994
995 cmd->data[0] = devid;
996 if (gn) {
997 cmd->data[1] = pasid & PASID_MASK;
998 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
999 }
1000 cmd->data[3] = tag & 0x1ff;
1001 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1002
1003 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1004}
1005
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001006static void build_inv_all(struct iommu_cmd *cmd)
1007{
1008 memset(cmd, 0, sizeof(*cmd));
1009 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001010}
1011
Joerg Roedel7ef27982012-06-21 16:46:04 +02001012static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1013{
1014 memset(cmd, 0, sizeof(*cmd));
1015 cmd->data[0] = devid;
1016 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1017}
1018
Joerg Roedel431b2a22008-07-11 17:14:22 +02001019/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001020 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001021 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001022 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001023static int iommu_queue_command_sync(struct amd_iommu *iommu,
1024 struct iommu_cmd *cmd,
1025 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001026{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001027 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +02001028 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001029
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001030 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +01001031
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001032again:
Joerg Roedel815b33f2011-04-06 17:26:49 +02001033 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001034
1035 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1036 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1037 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
1038 left = (head - next_tail) % iommu->cmd_buf_size;
1039
1040 if (left <= 2) {
1041 struct iommu_cmd sync_cmd;
1042 volatile u64 sem = 0;
1043 int ret;
1044
1045 build_completion_wait(&sync_cmd, (u64)&sem);
1046 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1047
1048 spin_unlock_irqrestore(&iommu->lock, flags);
1049
1050 if ((ret = wait_on_sem(&sem)) != 0)
1051 return ret;
1052
1053 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001054 }
1055
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001056 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001057
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001058 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001059 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001060
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001061 spin_unlock_irqrestore(&iommu->lock, flags);
1062
Joerg Roedel815b33f2011-04-06 17:26:49 +02001063 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001064}
1065
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001066static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1067{
1068 return iommu_queue_command_sync(iommu, cmd, true);
1069}
1070
Joerg Roedel8d201962008-12-02 20:34:41 +01001071/*
1072 * This function queues a completion wait command into the command
1073 * buffer of an IOMMU
1074 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001075static int iommu_completion_wait(struct amd_iommu *iommu)
1076{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001077 struct iommu_cmd cmd;
1078 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001079 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001080
1081 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001082 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001083
Joerg Roedel815b33f2011-04-06 17:26:49 +02001084 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001085
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001086 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001087 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001088 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001089
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001090 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001091}
1092
Joerg Roedeld8c13082011-04-06 18:51:26 +02001093static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001094{
1095 struct iommu_cmd cmd;
1096
Joerg Roedeld8c13082011-04-06 18:51:26 +02001097 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001098
Joerg Roedeld8c13082011-04-06 18:51:26 +02001099 return iommu_queue_command(iommu, &cmd);
1100}
1101
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001102static void iommu_flush_dte_all(struct amd_iommu *iommu)
1103{
1104 u32 devid;
1105
1106 for (devid = 0; devid <= 0xffff; ++devid)
1107 iommu_flush_dte(iommu, devid);
1108
1109 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001110}
1111
1112/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001113 * This function uses heavy locking and may disable irqs for some time. But
1114 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001115 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001116static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001117{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001118 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001119
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001120 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1121 struct iommu_cmd cmd;
1122 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1123 dom_id, 1);
1124 iommu_queue_command(iommu, &cmd);
1125 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001126
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001127 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001128}
1129
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001130static void iommu_flush_all(struct amd_iommu *iommu)
1131{
1132 struct iommu_cmd cmd;
1133
1134 build_inv_all(&cmd);
1135
1136 iommu_queue_command(iommu, &cmd);
1137 iommu_completion_wait(iommu);
1138}
1139
Joerg Roedel7ef27982012-06-21 16:46:04 +02001140static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1141{
1142 struct iommu_cmd cmd;
1143
1144 build_inv_irt(&cmd, devid);
1145
1146 iommu_queue_command(iommu, &cmd);
1147}
1148
1149static void iommu_flush_irt_all(struct amd_iommu *iommu)
1150{
1151 u32 devid;
1152
1153 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1154 iommu_flush_irt(iommu, devid);
1155
1156 iommu_completion_wait(iommu);
1157}
1158
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001159void iommu_flush_all_caches(struct amd_iommu *iommu)
1160{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001161 if (iommu_feature(iommu, FEATURE_IA)) {
1162 iommu_flush_all(iommu);
1163 } else {
1164 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001165 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001166 iommu_flush_tlb_all(iommu);
1167 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001168}
1169
Joerg Roedel431b2a22008-07-11 17:14:22 +02001170/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001171 * Command send function for flushing on-device TLB
1172 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001173static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1174 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001175{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001176 struct amd_iommu *iommu;
1177 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001178 int qdep;
1179
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001180 qdep = dev_data->ats.qdep;
1181 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001182
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001183 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001184
1185 return iommu_queue_command(iommu, &cmd);
1186}
1187
1188/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001189 * Command send function for invalidating a device table entry
1190 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001191static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001192{
1193 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001194 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001195
Joerg Roedel6c542042011-06-09 17:07:31 +02001196 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001197
Joerg Roedelf62dda62011-06-09 12:55:35 +02001198 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001199 if (ret)
1200 return ret;
1201
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001202 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001203 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001204
1205 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001206}
1207
Joerg Roedel431b2a22008-07-11 17:14:22 +02001208/*
1209 * TLB invalidation function which is called from the mapping functions.
1210 * It invalidates a single PTE if the range to flush is within a single
1211 * page. Otherwise it flushes the whole TLB of the IOMMU.
1212 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001213static void __domain_flush_pages(struct protection_domain *domain,
1214 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001215{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001216 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001217 struct iommu_cmd cmd;
1218 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001219
Joerg Roedel11b64022011-04-06 11:49:28 +02001220 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001221
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001222 for (i = 0; i < amd_iommus_present; ++i) {
1223 if (!domain->dev_iommu[i])
1224 continue;
1225
1226 /*
1227 * Devices of this domain are behind this IOMMU
1228 * We need a TLB flush
1229 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001230 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001231 }
1232
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001233 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001234
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001235 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001236 continue;
1237
Joerg Roedel6c542042011-06-09 17:07:31 +02001238 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001239 }
1240
Joerg Roedel11b64022011-04-06 11:49:28 +02001241 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001242}
1243
Joerg Roedel17b124b2011-04-06 18:01:35 +02001244static void domain_flush_pages(struct protection_domain *domain,
1245 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001246{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001247 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001248}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001249
Joerg Roedel1c655772008-09-04 18:40:05 +02001250/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001251static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001252{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001253 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001254}
1255
Chris Wright42a49f92009-06-15 15:42:00 +02001256/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001257static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001258{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001259 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1260}
1261
1262static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001263{
1264 int i;
1265
1266 for (i = 0; i < amd_iommus_present; ++i) {
1267 if (!domain->dev_iommu[i])
1268 continue;
1269
1270 /*
1271 * Devices of this domain are behind this IOMMU
1272 * We need to wait for completion of all commands.
1273 */
1274 iommu_completion_wait(amd_iommus[i]);
1275 }
1276}
1277
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001278
Joerg Roedel43f49602008-12-02 21:01:12 +01001279/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001280 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001281 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001282static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001283{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001284 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001285
1286 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001287 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001288}
1289
Joerg Roedel431b2a22008-07-11 17:14:22 +02001290/****************************************************************************
1291 *
1292 * The functions below are used the create the page table mappings for
1293 * unity mapped regions.
1294 *
1295 ****************************************************************************/
1296
1297/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001298 * This function is used to add another level to an IO page table. Adding
1299 * another level increases the size of the address space by 9 bits to a size up
1300 * to 64 bits.
1301 */
1302static bool increase_address_space(struct protection_domain *domain,
1303 gfp_t gfp)
1304{
1305 u64 *pte;
1306
1307 if (domain->mode == PAGE_MODE_6_LEVEL)
1308 /* address space already 64 bit large */
1309 return false;
1310
1311 pte = (void *)get_zeroed_page(gfp);
1312 if (!pte)
1313 return false;
1314
1315 *pte = PM_LEVEL_PDE(domain->mode,
1316 virt_to_phys(domain->pt_root));
1317 domain->pt_root = pte;
1318 domain->mode += 1;
1319 domain->updated = true;
1320
1321 return true;
1322}
1323
1324static u64 *alloc_pte(struct protection_domain *domain,
1325 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001326 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001327 u64 **pte_page,
1328 gfp_t gfp)
1329{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001330 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001331 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001332
1333 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001334
1335 while (address > PM_LEVEL_SIZE(domain->mode))
1336 increase_address_space(domain, gfp);
1337
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001338 level = domain->mode - 1;
1339 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1340 address = PAGE_SIZE_ALIGN(address, page_size);
1341 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001342
1343 while (level > end_lvl) {
1344 if (!IOMMU_PTE_PRESENT(*pte)) {
1345 page = (u64 *)get_zeroed_page(gfp);
1346 if (!page)
1347 return NULL;
1348 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1349 }
1350
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001351 /* No level skipping support yet */
1352 if (PM_PTE_LEVEL(*pte) != level)
1353 return NULL;
1354
Joerg Roedel308973d2009-11-24 17:43:32 +01001355 level -= 1;
1356
1357 pte = IOMMU_PTE_PAGE(*pte);
1358
1359 if (pte_page && level == end_lvl)
1360 *pte_page = pte;
1361
1362 pte = &pte[PM_LEVEL_INDEX(level, address)];
1363 }
1364
1365 return pte;
1366}
1367
1368/*
1369 * This function checks if there is a PTE for a given dma address. If
1370 * there is one, it returns the pointer to it.
1371 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001372static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001373{
1374 int level;
1375 u64 *pte;
1376
Joerg Roedel24cd7722010-01-19 17:27:39 +01001377 if (address > PM_LEVEL_SIZE(domain->mode))
1378 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001379
Joerg Roedel24cd7722010-01-19 17:27:39 +01001380 level = domain->mode - 1;
1381 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1382
1383 while (level > 0) {
1384
1385 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001386 if (!IOMMU_PTE_PRESENT(*pte))
1387 return NULL;
1388
Joerg Roedel24cd7722010-01-19 17:27:39 +01001389 /* Large PTE */
1390 if (PM_PTE_LEVEL(*pte) == 0x07) {
1391 unsigned long pte_mask, __pte;
1392
1393 /*
1394 * If we have a series of large PTEs, make
1395 * sure to return a pointer to the first one.
1396 */
1397 pte_mask = PTE_PAGE_SIZE(*pte);
1398 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1399 __pte = ((unsigned long)pte) & pte_mask;
1400
1401 return (u64 *)__pte;
1402 }
1403
1404 /* No level skipping support yet */
1405 if (PM_PTE_LEVEL(*pte) != level)
1406 return NULL;
1407
Joerg Roedel308973d2009-11-24 17:43:32 +01001408 level -= 1;
1409
Joerg Roedel24cd7722010-01-19 17:27:39 +01001410 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001411 pte = IOMMU_PTE_PAGE(*pte);
1412 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001413 }
1414
1415 return pte;
1416}
1417
1418/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001419 * Generic mapping functions. It maps a physical address into a DMA
1420 * address space. It allocates the page table pages if necessary.
1421 * In the future it can be extended to a generic mapping function
1422 * supporting all features of AMD IOMMU page tables like level skipping
1423 * and full 64 bit address spaces.
1424 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001425static int iommu_map_page(struct protection_domain *dom,
1426 unsigned long bus_addr,
1427 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001428 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001429 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001430{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001431 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001432 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001433
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001434 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001435 return -EINVAL;
1436
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001437 bus_addr = PAGE_ALIGN(bus_addr);
1438 phys_addr = PAGE_ALIGN(phys_addr);
1439 count = PAGE_SIZE_PTE_COUNT(page_size);
1440 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001441
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001442 for (i = 0; i < count; ++i)
1443 if (IOMMU_PTE_PRESENT(pte[i]))
1444 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001445
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001446 if (page_size > PAGE_SIZE) {
1447 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1448 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1449 } else
1450 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1451
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001452 if (prot & IOMMU_PROT_IR)
1453 __pte |= IOMMU_PTE_IR;
1454 if (prot & IOMMU_PROT_IW)
1455 __pte |= IOMMU_PTE_IW;
1456
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001457 for (i = 0; i < count; ++i)
1458 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001459
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001460 update_domain(dom);
1461
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001462 return 0;
1463}
1464
Joerg Roedel24cd7722010-01-19 17:27:39 +01001465static unsigned long iommu_unmap_page(struct protection_domain *dom,
1466 unsigned long bus_addr,
1467 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001468{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001469 unsigned long long unmap_size, unmapped;
1470 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001471
Joerg Roedel24cd7722010-01-19 17:27:39 +01001472 BUG_ON(!is_power_of_2(page_size));
1473
1474 unmapped = 0;
1475
1476 while (unmapped < page_size) {
1477
1478 pte = fetch_pte(dom, bus_addr);
1479
1480 if (!pte) {
1481 /*
1482 * No PTE for this address
1483 * move forward in 4kb steps
1484 */
1485 unmap_size = PAGE_SIZE;
1486 } else if (PM_PTE_LEVEL(*pte) == 0) {
1487 /* 4kb PTE found for this address */
1488 unmap_size = PAGE_SIZE;
1489 *pte = 0ULL;
1490 } else {
1491 int count, i;
1492
1493 /* Large PTE found which maps this address */
1494 unmap_size = PTE_PAGE_SIZE(*pte);
1495 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1496 for (i = 0; i < count; i++)
1497 pte[i] = 0ULL;
1498 }
1499
1500 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1501 unmapped += unmap_size;
1502 }
1503
1504 BUG_ON(!is_power_of_2(unmapped));
1505
1506 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001507}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001508
Joerg Roedel431b2a22008-07-11 17:14:22 +02001509/*
1510 * This function checks if a specific unity mapping entry is needed for
1511 * this specific IOMMU.
1512 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001513static int iommu_for_unity_map(struct amd_iommu *iommu,
1514 struct unity_map_entry *entry)
1515{
1516 u16 bdf, i;
1517
1518 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1519 bdf = amd_iommu_alias_table[i];
1520 if (amd_iommu_rlookup_table[bdf] == iommu)
1521 return 1;
1522 }
1523
1524 return 0;
1525}
1526
Joerg Roedel431b2a22008-07-11 17:14:22 +02001527/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001528 * This function actually applies the mapping to the page table of the
1529 * dma_ops domain.
1530 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001531static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1532 struct unity_map_entry *e)
1533{
1534 u64 addr;
1535 int ret;
1536
1537 for (addr = e->address_start; addr < e->address_end;
1538 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001539 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001540 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001541 if (ret)
1542 return ret;
1543 /*
1544 * if unity mapping is in aperture range mark the page
1545 * as allocated in the aperture
1546 */
1547 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001548 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001549 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001550 }
1551
1552 return 0;
1553}
1554
Joerg Roedel431b2a22008-07-11 17:14:22 +02001555/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001556 * Init the unity mappings for a specific IOMMU in the system
1557 *
1558 * Basically iterates over all unity mapping entries and applies them to
1559 * the default domain DMA of that IOMMU if necessary.
1560 */
1561static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1562{
1563 struct unity_map_entry *entry;
1564 int ret;
1565
1566 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1567 if (!iommu_for_unity_map(iommu, entry))
1568 continue;
1569 ret = dma_ops_unity_map(iommu->default_dom, entry);
1570 if (ret)
1571 return ret;
1572 }
1573
1574 return 0;
1575}
1576
1577/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001578 * Inits the unity mappings required for a specific device
1579 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001580static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1581 u16 devid)
1582{
1583 struct unity_map_entry *e;
1584 int ret;
1585
1586 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1587 if (!(devid >= e->devid_start && devid <= e->devid_end))
1588 continue;
1589 ret = dma_ops_unity_map(dma_dom, e);
1590 if (ret)
1591 return ret;
1592 }
1593
1594 return 0;
1595}
1596
Joerg Roedel431b2a22008-07-11 17:14:22 +02001597/****************************************************************************
1598 *
1599 * The next functions belong to the address allocator for the dma_ops
1600 * interface functions. They work like the allocators in the other IOMMU
1601 * drivers. Its basically a bitmap which marks the allocated pages in
1602 * the aperture. Maybe it could be enhanced in the future to a more
1603 * efficient allocator.
1604 *
1605 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001606
Joerg Roedel431b2a22008-07-11 17:14:22 +02001607/*
Joerg Roedel384de722009-05-15 12:30:05 +02001608 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001609 *
1610 * called with domain->lock held
1611 */
Joerg Roedel384de722009-05-15 12:30:05 +02001612
Joerg Roedel9cabe892009-05-18 16:38:55 +02001613/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001614 * Used to reserve address ranges in the aperture (e.g. for exclusion
1615 * ranges.
1616 */
1617static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1618 unsigned long start_page,
1619 unsigned int pages)
1620{
1621 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1622
1623 if (start_page + pages > last_page)
1624 pages = last_page - start_page;
1625
1626 for (i = start_page; i < start_page + pages; ++i) {
1627 int index = i / APERTURE_RANGE_PAGES;
1628 int page = i % APERTURE_RANGE_PAGES;
1629 __set_bit(page, dom->aperture[index]->bitmap);
1630 }
1631}
1632
1633/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001634 * This function is used to add a new aperture range to an existing
1635 * aperture in case of dma_ops domain allocation or address allocation
1636 * failure.
1637 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001638static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001639 bool populate, gfp_t gfp)
1640{
1641 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001642 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001643 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001644
Joerg Roedelf5e97052009-05-22 12:31:53 +02001645#ifdef CONFIG_IOMMU_STRESS
1646 populate = false;
1647#endif
1648
Joerg Roedel9cabe892009-05-18 16:38:55 +02001649 if (index >= APERTURE_MAX_RANGES)
1650 return -ENOMEM;
1651
1652 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1653 if (!dma_dom->aperture[index])
1654 return -ENOMEM;
1655
1656 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1657 if (!dma_dom->aperture[index]->bitmap)
1658 goto out_free;
1659
1660 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1661
1662 if (populate) {
1663 unsigned long address = dma_dom->aperture_size;
1664 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1665 u64 *pte, *pte_page;
1666
1667 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001668 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001669 &pte_page, gfp);
1670 if (!pte)
1671 goto out_free;
1672
1673 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1674
1675 address += APERTURE_RANGE_SIZE / 64;
1676 }
1677 }
1678
Joerg Roedel17f5b562011-07-06 17:14:44 +02001679 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001680 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1681
Joerg Roedel17f5b562011-07-06 17:14:44 +02001682 /* Reserve address range used for MSI messages */
1683 if (old_size < MSI_ADDR_BASE_LO &&
1684 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1685 unsigned long spage;
1686 int pages;
1687
1688 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1689 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1690
1691 dma_ops_reserve_addresses(dma_dom, spage, pages);
1692 }
1693
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001694 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001695 for_each_iommu(iommu) {
1696 if (iommu->exclusion_start &&
1697 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1698 && iommu->exclusion_start < dma_dom->aperture_size) {
1699 unsigned long startpage;
1700 int pages = iommu_num_pages(iommu->exclusion_start,
1701 iommu->exclusion_length,
1702 PAGE_SIZE);
1703 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1704 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1705 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001706 }
1707
1708 /*
1709 * Check for areas already mapped as present in the new aperture
1710 * range and mark those pages as reserved in the allocator. Such
1711 * mappings may already exist as a result of requested unity
1712 * mappings for devices.
1713 */
1714 for (i = dma_dom->aperture[index]->offset;
1715 i < dma_dom->aperture_size;
1716 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001717 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001718 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1719 continue;
1720
Joerg Roedelfcd08612011-10-11 17:41:32 +02001721 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001722 }
1723
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001724 update_domain(&dma_dom->domain);
1725
Joerg Roedel9cabe892009-05-18 16:38:55 +02001726 return 0;
1727
1728out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001729 update_domain(&dma_dom->domain);
1730
Joerg Roedel9cabe892009-05-18 16:38:55 +02001731 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1732
1733 kfree(dma_dom->aperture[index]);
1734 dma_dom->aperture[index] = NULL;
1735
1736 return -ENOMEM;
1737}
1738
Joerg Roedel384de722009-05-15 12:30:05 +02001739static unsigned long dma_ops_area_alloc(struct device *dev,
1740 struct dma_ops_domain *dom,
1741 unsigned int pages,
1742 unsigned long align_mask,
1743 u64 dma_mask,
1744 unsigned long start)
1745{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001746 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001747 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1748 int i = start >> APERTURE_RANGE_SHIFT;
1749 unsigned long boundary_size;
1750 unsigned long address = -1;
1751 unsigned long limit;
1752
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001753 next_bit >>= PAGE_SHIFT;
1754
Joerg Roedel384de722009-05-15 12:30:05 +02001755 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1756 PAGE_SIZE) >> PAGE_SHIFT;
1757
1758 for (;i < max_index; ++i) {
1759 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1760
1761 if (dom->aperture[i]->offset >= dma_mask)
1762 break;
1763
1764 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1765 dma_mask >> PAGE_SHIFT);
1766
1767 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1768 limit, next_bit, pages, 0,
1769 boundary_size, align_mask);
1770 if (address != -1) {
1771 address = dom->aperture[i]->offset +
1772 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001773 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001774 break;
1775 }
1776
1777 next_bit = 0;
1778 }
1779
1780 return address;
1781}
1782
Joerg Roedeld3086442008-06-26 21:27:57 +02001783static unsigned long dma_ops_alloc_addresses(struct device *dev,
1784 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001785 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001786 unsigned long align_mask,
1787 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001788{
Joerg Roedeld3086442008-06-26 21:27:57 +02001789 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001790
Joerg Roedelfe16f082009-05-22 12:27:53 +02001791#ifdef CONFIG_IOMMU_STRESS
1792 dom->next_address = 0;
1793 dom->need_flush = true;
1794#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001795
Joerg Roedel384de722009-05-15 12:30:05 +02001796 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001797 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001798
Joerg Roedel1c655772008-09-04 18:40:05 +02001799 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001800 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001801 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1802 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001803 dom->need_flush = true;
1804 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001805
Joerg Roedel384de722009-05-15 12:30:05 +02001806 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001807 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001808
1809 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1810
1811 return address;
1812}
1813
Joerg Roedel431b2a22008-07-11 17:14:22 +02001814/*
1815 * The address free function.
1816 *
1817 * called with domain->lock held
1818 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001819static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1820 unsigned long address,
1821 unsigned int pages)
1822{
Joerg Roedel384de722009-05-15 12:30:05 +02001823 unsigned i = address >> APERTURE_RANGE_SHIFT;
1824 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001825
Joerg Roedel384de722009-05-15 12:30:05 +02001826 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1827
Joerg Roedel47bccd62009-05-22 12:40:54 +02001828#ifdef CONFIG_IOMMU_STRESS
1829 if (i < 4)
1830 return;
1831#endif
1832
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001833 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001834 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001835
1836 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001837
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001838 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001839
Joerg Roedeld3086442008-06-26 21:27:57 +02001840}
1841
Joerg Roedel431b2a22008-07-11 17:14:22 +02001842/****************************************************************************
1843 *
1844 * The next functions belong to the domain allocation. A domain is
1845 * allocated for every IOMMU as the default domain. If device isolation
1846 * is enabled, every device get its own domain. The most important thing
1847 * about domains is the page table mapping the DMA address space they
1848 * contain.
1849 *
1850 ****************************************************************************/
1851
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001852/*
1853 * This function adds a protection domain to the global protection domain list
1854 */
1855static void add_domain_to_list(struct protection_domain *domain)
1856{
1857 unsigned long flags;
1858
1859 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1860 list_add(&domain->list, &amd_iommu_pd_list);
1861 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1862}
1863
1864/*
1865 * This function removes a protection domain to the global
1866 * protection domain list
1867 */
1868static void del_domain_from_list(struct protection_domain *domain)
1869{
1870 unsigned long flags;
1871
1872 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1873 list_del(&domain->list);
1874 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1875}
1876
Joerg Roedelec487d12008-06-26 21:27:58 +02001877static u16 domain_id_alloc(void)
1878{
1879 unsigned long flags;
1880 int id;
1881
1882 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1883 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1884 BUG_ON(id == 0);
1885 if (id > 0 && id < MAX_DOMAIN_ID)
1886 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1887 else
1888 id = 0;
1889 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1890
1891 return id;
1892}
1893
Joerg Roedela2acfb72008-12-02 18:28:53 +01001894static void domain_id_free(int id)
1895{
1896 unsigned long flags;
1897
1898 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1899 if (id > 0 && id < MAX_DOMAIN_ID)
1900 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1901 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1902}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001903
Joerg Roedel86db2e52008-12-02 18:20:21 +01001904static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001905{
1906 int i, j;
1907 u64 *p1, *p2, *p3;
1908
Joerg Roedel86db2e52008-12-02 18:20:21 +01001909 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001910
1911 if (!p1)
1912 return;
1913
1914 for (i = 0; i < 512; ++i) {
1915 if (!IOMMU_PTE_PRESENT(p1[i]))
1916 continue;
1917
1918 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001919 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001920 if (!IOMMU_PTE_PRESENT(p2[j]))
1921 continue;
1922 p3 = IOMMU_PTE_PAGE(p2[j]);
1923 free_page((unsigned long)p3);
1924 }
1925
1926 free_page((unsigned long)p2);
1927 }
1928
1929 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001930
1931 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001932}
1933
Joerg Roedelb16137b2011-11-21 16:50:23 +01001934static void free_gcr3_tbl_level1(u64 *tbl)
1935{
1936 u64 *ptr;
1937 int i;
1938
1939 for (i = 0; i < 512; ++i) {
1940 if (!(tbl[i] & GCR3_VALID))
1941 continue;
1942
1943 ptr = __va(tbl[i] & PAGE_MASK);
1944
1945 free_page((unsigned long)ptr);
1946 }
1947}
1948
1949static void free_gcr3_tbl_level2(u64 *tbl)
1950{
1951 u64 *ptr;
1952 int i;
1953
1954 for (i = 0; i < 512; ++i) {
1955 if (!(tbl[i] & GCR3_VALID))
1956 continue;
1957
1958 ptr = __va(tbl[i] & PAGE_MASK);
1959
1960 free_gcr3_tbl_level1(ptr);
1961 }
1962}
1963
Joerg Roedel52815b72011-11-17 17:24:28 +01001964static void free_gcr3_table(struct protection_domain *domain)
1965{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001966 if (domain->glx == 2)
1967 free_gcr3_tbl_level2(domain->gcr3_tbl);
1968 else if (domain->glx == 1)
1969 free_gcr3_tbl_level1(domain->gcr3_tbl);
1970 else if (domain->glx != 0)
1971 BUG();
1972
Joerg Roedel52815b72011-11-17 17:24:28 +01001973 free_page((unsigned long)domain->gcr3_tbl);
1974}
1975
Joerg Roedel431b2a22008-07-11 17:14:22 +02001976/*
1977 * Free a domain, only used if something went wrong in the
1978 * allocation path and we need to free an already allocated page table
1979 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001980static void dma_ops_domain_free(struct dma_ops_domain *dom)
1981{
Joerg Roedel384de722009-05-15 12:30:05 +02001982 int i;
1983
Joerg Roedelec487d12008-06-26 21:27:58 +02001984 if (!dom)
1985 return;
1986
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001987 del_domain_from_list(&dom->domain);
1988
Joerg Roedel86db2e52008-12-02 18:20:21 +01001989 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001990
Joerg Roedel384de722009-05-15 12:30:05 +02001991 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1992 if (!dom->aperture[i])
1993 continue;
1994 free_page((unsigned long)dom->aperture[i]->bitmap);
1995 kfree(dom->aperture[i]);
1996 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001997
1998 kfree(dom);
1999}
2000
Joerg Roedel431b2a22008-07-11 17:14:22 +02002001/*
2002 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002003 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02002004 * structures required for the dma_ops interface
2005 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01002006static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02002007{
2008 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002009
2010 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
2011 if (!dma_dom)
2012 return NULL;
2013
2014 spin_lock_init(&dma_dom->domain.lock);
2015
2016 dma_dom->domain.id = domain_id_alloc();
2017 if (dma_dom->domain.id == 0)
2018 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002019 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002020 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002021 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002022 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002023 dma_dom->domain.priv = dma_dom;
2024 if (!dma_dom->domain.pt_root)
2025 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002026
Joerg Roedel1c655772008-09-04 18:40:05 +02002027 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002028 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002029
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002030 add_domain_to_list(&dma_dom->domain);
2031
Joerg Roedel576175c2009-11-23 19:08:46 +01002032 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002033 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002034
Joerg Roedel431b2a22008-07-11 17:14:22 +02002035 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002036 * mark the first page as allocated so we never return 0 as
2037 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002038 */
Joerg Roedel384de722009-05-15 12:30:05 +02002039 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02002040 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002041
Joerg Roedelec487d12008-06-26 21:27:58 +02002042
2043 return dma_dom;
2044
2045free_dma_dom:
2046 dma_ops_domain_free(dma_dom);
2047
2048 return NULL;
2049}
2050
Joerg Roedel431b2a22008-07-11 17:14:22 +02002051/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002052 * little helper function to check whether a given protection domain is a
2053 * dma_ops domain
2054 */
2055static bool dma_ops_domain(struct protection_domain *domain)
2056{
2057 return domain->flags & PD_DMA_OPS_MASK;
2058}
2059
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002060static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002061{
Joerg Roedel132bd682011-11-17 14:18:46 +01002062 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002063 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002064
Joerg Roedel132bd682011-11-17 14:18:46 +01002065 if (domain->mode != PAGE_MODE_NONE)
2066 pte_root = virt_to_phys(domain->pt_root);
2067
Joerg Roedel38ddf412008-09-11 10:38:32 +02002068 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2069 << DEV_ENTRY_MODE_SHIFT;
2070 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002071
Joerg Roedelee6c2862011-11-09 12:06:03 +01002072 flags = amd_iommu_dev_table[devid].data[1];
2073
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002074 if (ats)
2075 flags |= DTE_FLAG_IOTLB;
2076
Joerg Roedel52815b72011-11-17 17:24:28 +01002077 if (domain->flags & PD_IOMMUV2_MASK) {
2078 u64 gcr3 = __pa(domain->gcr3_tbl);
2079 u64 glx = domain->glx;
2080 u64 tmp;
2081
2082 pte_root |= DTE_FLAG_GV;
2083 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2084
2085 /* First mask out possible old values for GCR3 table */
2086 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2087 flags &= ~tmp;
2088
2089 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2090 flags &= ~tmp;
2091
2092 /* Encode GCR3 table into DTE */
2093 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2094 pte_root |= tmp;
2095
2096 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2097 flags |= tmp;
2098
2099 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2100 flags |= tmp;
2101 }
2102
Joerg Roedelee6c2862011-11-09 12:06:03 +01002103 flags &= ~(0xffffUL);
2104 flags |= domain->id;
2105
2106 amd_iommu_dev_table[devid].data[1] = flags;
2107 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002108}
2109
Joerg Roedel15898bb2009-11-24 15:39:42 +01002110static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002111{
Joerg Roedel355bf552008-12-08 12:02:41 +01002112 /* remove entry from the device table seen by the hardware */
2113 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2114 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002115
Joerg Roedelc5cca142009-10-09 18:31:20 +02002116 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002117}
2118
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002119static void do_attach(struct iommu_dev_data *dev_data,
2120 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002121{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002122 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002123 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002124
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002125 iommu = amd_iommu_rlookup_table[dev_data->devid];
2126 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002127
2128 /* Update data structures */
2129 dev_data->domain = domain;
2130 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002131 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002132
2133 /* Do reference counting */
2134 domain->dev_iommu[iommu->index] += 1;
2135 domain->dev_cnt += 1;
2136
2137 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002138 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002139}
2140
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002141static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002142{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002143 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002144
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002145 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002146
Joerg Roedelc4596112009-11-20 14:57:32 +01002147 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002148 dev_data->domain->dev_iommu[iommu->index] -= 1;
2149 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002150
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002151 /* Update data structures */
2152 dev_data->domain = NULL;
2153 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002154 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002155
2156 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002157 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002158}
2159
2160/*
2161 * If a device is not yet associated with a domain, this function does
2162 * assigns it visible for the hardware
2163 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002164static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002165 struct protection_domain *domain)
2166{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002167 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002168
Joerg Roedel15898bb2009-11-24 15:39:42 +01002169 /* lock domain */
2170 spin_lock(&domain->lock);
2171
Joerg Roedel71f77582011-06-09 19:03:15 +02002172 if (dev_data->alias_data != NULL) {
2173 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002174
Joerg Roedel2b02b092011-06-09 17:48:39 +02002175 /* Some sanity checks */
2176 ret = -EBUSY;
2177 if (alias_data->domain != NULL &&
2178 alias_data->domain != domain)
2179 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002180
Joerg Roedel2b02b092011-06-09 17:48:39 +02002181 if (dev_data->domain != NULL &&
2182 dev_data->domain != domain)
2183 goto out_unlock;
2184
2185 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002186 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002187 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002188
2189 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002190 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002191
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002192 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002193 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002194
Joerg Roedel24100052009-11-25 15:59:57 +01002195 atomic_inc(&dev_data->bind);
2196
Julia Lawall84fe6c12010-05-27 12:31:51 +02002197 ret = 0;
2198
2199out_unlock:
2200
Joerg Roedel355bf552008-12-08 12:02:41 +01002201 /* ready */
2202 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002203
Julia Lawall84fe6c12010-05-27 12:31:51 +02002204 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002205}
2206
Joerg Roedel52815b72011-11-17 17:24:28 +01002207
2208static void pdev_iommuv2_disable(struct pci_dev *pdev)
2209{
2210 pci_disable_ats(pdev);
2211 pci_disable_pri(pdev);
2212 pci_disable_pasid(pdev);
2213}
2214
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002215/* FIXME: Change generic reset-function to do the same */
2216static int pri_reset_while_enabled(struct pci_dev *pdev)
2217{
2218 u16 control;
2219 int pos;
2220
Joerg Roedel46277b72011-12-07 14:34:02 +01002221 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002222 if (!pos)
2223 return -EINVAL;
2224
Joerg Roedel46277b72011-12-07 14:34:02 +01002225 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2226 control |= PCI_PRI_CTRL_RESET;
2227 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002228
2229 return 0;
2230}
2231
Joerg Roedel52815b72011-11-17 17:24:28 +01002232static int pdev_iommuv2_enable(struct pci_dev *pdev)
2233{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002234 bool reset_enable;
2235 int reqs, ret;
2236
2237 /* FIXME: Hardcode number of outstanding requests for now */
2238 reqs = 32;
2239 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2240 reqs = 1;
2241 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002242
2243 /* Only allow access to user-accessible pages */
2244 ret = pci_enable_pasid(pdev, 0);
2245 if (ret)
2246 goto out_err;
2247
2248 /* First reset the PRI state of the device */
2249 ret = pci_reset_pri(pdev);
2250 if (ret)
2251 goto out_err;
2252
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002253 /* Enable PRI */
2254 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002255 if (ret)
2256 goto out_err;
2257
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002258 if (reset_enable) {
2259 ret = pri_reset_while_enabled(pdev);
2260 if (ret)
2261 goto out_err;
2262 }
2263
Joerg Roedel52815b72011-11-17 17:24:28 +01002264 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2265 if (ret)
2266 goto out_err;
2267
2268 return 0;
2269
2270out_err:
2271 pci_disable_pri(pdev);
2272 pci_disable_pasid(pdev);
2273
2274 return ret;
2275}
2276
Joerg Roedelc99afa22011-11-21 18:19:25 +01002277/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002278#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002279
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002280static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002281{
Joerg Roedela3b93122012-04-12 12:49:26 +02002282 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002283 int pos;
2284
Joerg Roedel46277b72011-12-07 14:34:02 +01002285 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002286 if (!pos)
2287 return false;
2288
Joerg Roedela3b93122012-04-12 12:49:26 +02002289 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002290
Joerg Roedela3b93122012-04-12 12:49:26 +02002291 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002292}
2293
Joerg Roedel15898bb2009-11-24 15:39:42 +01002294/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002295 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002296 * assigns it visible for the hardware
2297 */
2298static int attach_device(struct device *dev,
2299 struct protection_domain *domain)
2300{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002301 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002302 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002303 unsigned long flags;
2304 int ret;
2305
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002306 dev_data = get_dev_data(dev);
2307
Joerg Roedel52815b72011-11-17 17:24:28 +01002308 if (domain->flags & PD_IOMMUV2_MASK) {
2309 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2310 return -EINVAL;
2311
2312 if (pdev_iommuv2_enable(pdev) != 0)
2313 return -EINVAL;
2314
2315 dev_data->ats.enabled = true;
2316 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002317 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002318 } else if (amd_iommu_iotlb_sup &&
2319 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002320 dev_data->ats.enabled = true;
2321 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2322 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002323
Joerg Roedel15898bb2009-11-24 15:39:42 +01002324 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002325 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002326 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2327
2328 /*
2329 * We might boot into a crash-kernel here. The crashed kernel
2330 * left the caches in the IOMMU dirty. So we have to flush
2331 * here to evict all dirty stuff.
2332 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002333 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002334
2335 return ret;
2336}
2337
2338/*
2339 * Removes a device from a protection domain (unlocked)
2340 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002341static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002342{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002343 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002344 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002345
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002346 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002347
Joerg Roedel2ca76272010-01-22 16:45:31 +01002348 domain = dev_data->domain;
2349
2350 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002351
Joerg Roedel71f77582011-06-09 19:03:15 +02002352 if (dev_data->alias_data != NULL) {
2353 struct iommu_dev_data *alias_data = dev_data->alias_data;
2354
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002355 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002356 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002357 }
2358
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002359 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002360 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002361
Joerg Roedel2ca76272010-01-22 16:45:31 +01002362 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002363
Joerg Roedel21129f72009-09-01 11:59:42 +02002364 /*
2365 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002366 * passthrough domain if it is detached from any other domain.
2367 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002368 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002369 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002370 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002371 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002372}
2373
2374/*
2375 * Removes a device from a protection domain (with devtable_lock held)
2376 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002377static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002378{
Joerg Roedel52815b72011-11-17 17:24:28 +01002379 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002380 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002381 unsigned long flags;
2382
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002383 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002384 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002385
Joerg Roedel355bf552008-12-08 12:02:41 +01002386 /* lock device table */
2387 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002388 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002389 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002390
Joerg Roedel52815b72011-11-17 17:24:28 +01002391 if (domain->flags & PD_IOMMUV2_MASK)
2392 pdev_iommuv2_disable(to_pci_dev(dev));
2393 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002394 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002395
2396 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002397}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002398
Joerg Roedel15898bb2009-11-24 15:39:42 +01002399/*
2400 * Find out the protection domain structure for a given PCI device. This
2401 * will give us the pointer to the page table root for example.
2402 */
2403static struct protection_domain *domain_for_device(struct device *dev)
2404{
Joerg Roedel71f77582011-06-09 19:03:15 +02002405 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002406 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002407 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002408
Joerg Roedel657cbb62009-11-23 15:26:46 +01002409 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002410
Joerg Roedel2b02b092011-06-09 17:48:39 +02002411 if (dev_data->domain)
2412 return dev_data->domain;
2413
Joerg Roedel71f77582011-06-09 19:03:15 +02002414 if (dev_data->alias_data != NULL) {
2415 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002416
2417 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2418 if (alias_data->domain != NULL) {
2419 __attach_device(dev_data, alias_data->domain);
2420 dom = alias_data->domain;
2421 }
2422 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002423 }
2424
Joerg Roedel15898bb2009-11-24 15:39:42 +01002425 return dom;
2426}
2427
Joerg Roedele275a2a2008-12-10 18:27:25 +01002428static int device_change_notifier(struct notifier_block *nb,
2429 unsigned long action, void *data)
2430{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002431 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002432 struct protection_domain *domain;
2433 struct iommu_dev_data *dev_data;
2434 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002435 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002436 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002437 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002438
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002439 if (!check_device(dev))
2440 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002441
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002442 devid = get_device_id(dev);
2443 iommu = amd_iommu_rlookup_table[devid];
2444 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002445
2446 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002447 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002448
2449 domain = domain_for_device(dev);
2450
Joerg Roedele275a2a2008-12-10 18:27:25 +01002451 if (!domain)
2452 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002453 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002454 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002455 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002456 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002457 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002458
2459 iommu_init_device(dev);
2460
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002461 /*
2462 * dev_data is still NULL and
2463 * got initialized in iommu_init_device
2464 */
2465 dev_data = get_dev_data(dev);
2466
2467 if (iommu_pass_through || dev_data->iommu_v2) {
2468 dev_data->passthrough = true;
2469 attach_device(dev, pt_domain);
2470 break;
2471 }
2472
Joerg Roedel657cbb62009-11-23 15:26:46 +01002473 domain = domain_for_device(dev);
2474
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002475 /* allocate a protection domain if a device is added */
2476 dma_domain = find_protection_domain(devid);
2477 if (dma_domain)
2478 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002479 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002480 if (!dma_domain)
2481 goto out;
2482 dma_domain->target_dev = devid;
2483
2484 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2485 list_add_tail(&dma_domain->list, &iommu_pd_list);
2486 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2487
Joerg Roedelac1534a2012-06-21 14:52:40 +02002488 dev_data = get_dev_data(dev);
2489
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002490 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002491
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002492 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002493 case BUS_NOTIFY_DEL_DEVICE:
2494
2495 iommu_uninit_device(dev);
2496
Joerg Roedele275a2a2008-12-10 18:27:25 +01002497 default:
2498 goto out;
2499 }
2500
Joerg Roedele275a2a2008-12-10 18:27:25 +01002501 iommu_completion_wait(iommu);
2502
2503out:
2504 return 0;
2505}
2506
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302507static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002508 .notifier_call = device_change_notifier,
2509};
Joerg Roedel355bf552008-12-08 12:02:41 +01002510
Joerg Roedel8638c492009-12-10 11:12:25 +01002511void amd_iommu_init_notifier(void)
2512{
2513 bus_register_notifier(&pci_bus_type, &device_nb);
2514}
2515
Joerg Roedel431b2a22008-07-11 17:14:22 +02002516/*****************************************************************************
2517 *
2518 * The next functions belong to the dma_ops mapping/unmapping code.
2519 *
2520 *****************************************************************************/
2521
2522/*
2523 * In the dma_ops path we only have the struct device. This function
2524 * finds the corresponding IOMMU, the protection domain and the
2525 * requestor id for a given device.
2526 * If the device is not yet associated with a domain this is also done
2527 * in this function.
2528 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002529static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002530{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002531 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002532 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002533 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002534
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002535 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002536 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002537
Joerg Roedel94f6d192009-11-24 16:40:02 +01002538 domain = domain_for_device(dev);
2539 if (domain != NULL && !dma_ops_domain(domain))
2540 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002541
Joerg Roedel94f6d192009-11-24 16:40:02 +01002542 if (domain != NULL)
2543 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002544
Frank Arnolddf805ab2012-08-27 19:21:04 +02002545 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002546 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002547 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002548 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2549 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002550 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002551 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002552
Joerg Roedel94f6d192009-11-24 16:40:02 +01002553 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002554}
2555
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002556static void update_device_table(struct protection_domain *domain)
2557{
Joerg Roedel492667d2009-11-27 13:25:47 +01002558 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002559
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002560 list_for_each_entry(dev_data, &domain->dev_list, list)
2561 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002562}
2563
2564static void update_domain(struct protection_domain *domain)
2565{
2566 if (!domain->updated)
2567 return;
2568
2569 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002570
2571 domain_flush_devices(domain);
2572 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002573
2574 domain->updated = false;
2575}
2576
Joerg Roedel431b2a22008-07-11 17:14:22 +02002577/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002578 * This function fetches the PTE for a given address in the aperture
2579 */
2580static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2581 unsigned long address)
2582{
Joerg Roedel384de722009-05-15 12:30:05 +02002583 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002584 u64 *pte, *pte_page;
2585
Joerg Roedel384de722009-05-15 12:30:05 +02002586 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2587 if (!aperture)
2588 return NULL;
2589
2590 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002591 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002592 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002593 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002594 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2595 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002596 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002597
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002598 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002599
2600 return pte;
2601}
2602
2603/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002604 * This is the generic map function. It maps one 4kb page at paddr to
2605 * the given address in the DMA address space for the domain.
2606 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002607static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002608 unsigned long address,
2609 phys_addr_t paddr,
2610 int direction)
2611{
2612 u64 *pte, __pte;
2613
2614 WARN_ON(address > dom->aperture_size);
2615
2616 paddr &= PAGE_MASK;
2617
Joerg Roedel8bda3092009-05-12 12:02:46 +02002618 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002619 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002620 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002621
2622 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2623
2624 if (direction == DMA_TO_DEVICE)
2625 __pte |= IOMMU_PTE_IR;
2626 else if (direction == DMA_FROM_DEVICE)
2627 __pte |= IOMMU_PTE_IW;
2628 else if (direction == DMA_BIDIRECTIONAL)
2629 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2630
2631 WARN_ON(*pte);
2632
2633 *pte = __pte;
2634
2635 return (dma_addr_t)address;
2636}
2637
Joerg Roedel431b2a22008-07-11 17:14:22 +02002638/*
2639 * The generic unmapping function for on page in the DMA address space.
2640 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002641static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002642 unsigned long address)
2643{
Joerg Roedel384de722009-05-15 12:30:05 +02002644 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002645 u64 *pte;
2646
2647 if (address >= dom->aperture_size)
2648 return;
2649
Joerg Roedel384de722009-05-15 12:30:05 +02002650 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2651 if (!aperture)
2652 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002653
Joerg Roedel384de722009-05-15 12:30:05 +02002654 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2655 if (!pte)
2656 return;
2657
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002658 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002659
2660 WARN_ON(!*pte);
2661
2662 *pte = 0ULL;
2663}
2664
Joerg Roedel431b2a22008-07-11 17:14:22 +02002665/*
2666 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002667 * contiguous memory region into DMA address space. It is used by all
2668 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002669 * Must be called with the domain lock held.
2670 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002671static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002672 struct dma_ops_domain *dma_dom,
2673 phys_addr_t paddr,
2674 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002675 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002676 bool align,
2677 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002678{
2679 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002680 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002681 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002682 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002683 int i;
2684
Joerg Roedele3c449f2008-10-15 22:02:11 -07002685 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002686 paddr &= PAGE_MASK;
2687
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002688 INC_STATS_COUNTER(total_map_requests);
2689
Joerg Roedelc1858972008-12-12 15:42:39 +01002690 if (pages > 1)
2691 INC_STATS_COUNTER(cross_page);
2692
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002693 if (align)
2694 align_mask = (1UL << get_order(size)) - 1;
2695
Joerg Roedel11b83882009-05-19 10:23:15 +02002696retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002697 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2698 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002699 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002700 /*
2701 * setting next_address here will let the address
2702 * allocator only scan the new allocated range in the
2703 * first run. This is a small optimization.
2704 */
2705 dma_dom->next_address = dma_dom->aperture_size;
2706
Joerg Roedel576175c2009-11-23 19:08:46 +01002707 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002708 goto out;
2709
2710 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002711 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002712 * allocation again
2713 */
2714 goto retry;
2715 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002716
2717 start = address;
2718 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002719 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002720 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002721 goto out_unmap;
2722
Joerg Roedelcb76c322008-06-26 21:28:00 +02002723 paddr += PAGE_SIZE;
2724 start += PAGE_SIZE;
2725 }
2726 address += offset;
2727
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002728 ADD_STATS_COUNTER(alloced_io_mem, size);
2729
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002730 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002731 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002732 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002733 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002734 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002735
Joerg Roedelcb76c322008-06-26 21:28:00 +02002736out:
2737 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002738
2739out_unmap:
2740
2741 for (--i; i >= 0; --i) {
2742 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002743 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002744 }
2745
2746 dma_ops_free_addresses(dma_dom, address, pages);
2747
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002748 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002749}
2750
Joerg Roedel431b2a22008-07-11 17:14:22 +02002751/*
2752 * Does the reverse of the __map_single function. Must be called with
2753 * the domain lock held too
2754 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002755static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002756 dma_addr_t dma_addr,
2757 size_t size,
2758 int dir)
2759{
Joerg Roedel04e04632010-09-23 16:12:48 +02002760 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002761 dma_addr_t i, start;
2762 unsigned int pages;
2763
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002764 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002765 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002766 return;
2767
Joerg Roedel04e04632010-09-23 16:12:48 +02002768 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002769 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002770 dma_addr &= PAGE_MASK;
2771 start = dma_addr;
2772
2773 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002774 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002775 start += PAGE_SIZE;
2776 }
2777
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002778 SUB_STATS_COUNTER(alloced_io_mem, size);
2779
Joerg Roedelcb76c322008-06-26 21:28:00 +02002780 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002781
Joerg Roedel80be3082008-11-06 14:59:05 +01002782 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002783 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002784 dma_dom->need_flush = false;
2785 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002786}
2787
Joerg Roedel431b2a22008-07-11 17:14:22 +02002788/*
2789 * The exported map_single function for dma_ops.
2790 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002791static dma_addr_t map_page(struct device *dev, struct page *page,
2792 unsigned long offset, size_t size,
2793 enum dma_data_direction dir,
2794 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002795{
2796 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002797 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002798 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002799 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002800 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002801
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002802 INC_STATS_COUNTER(cnt_map_single);
2803
Joerg Roedel94f6d192009-11-24 16:40:02 +01002804 domain = get_domain(dev);
2805 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002806 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002807 else if (IS_ERR(domain))
2808 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002809
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002810 dma_mask = *dev->dma_mask;
2811
Joerg Roedel4da70b92008-06-26 21:28:01 +02002812 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002813
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002814 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002815 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002816 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002817 goto out;
2818
Joerg Roedel17b124b2011-04-06 18:01:35 +02002819 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002820
2821out:
2822 spin_unlock_irqrestore(&domain->lock, flags);
2823
2824 return addr;
2825}
2826
Joerg Roedel431b2a22008-07-11 17:14:22 +02002827/*
2828 * The exported unmap_single function for dma_ops.
2829 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002830static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2831 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002832{
2833 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002834 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002835
Joerg Roedel146a6912008-12-12 15:07:12 +01002836 INC_STATS_COUNTER(cnt_unmap_single);
2837
Joerg Roedel94f6d192009-11-24 16:40:02 +01002838 domain = get_domain(dev);
2839 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002840 return;
2841
Joerg Roedel4da70b92008-06-26 21:28:01 +02002842 spin_lock_irqsave(&domain->lock, flags);
2843
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002844 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002845
Joerg Roedel17b124b2011-04-06 18:01:35 +02002846 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002847
2848 spin_unlock_irqrestore(&domain->lock, flags);
2849}
2850
Joerg Roedel431b2a22008-07-11 17:14:22 +02002851/*
2852 * This is a special map_sg function which is used if we should map a
2853 * device which is not handled by an AMD IOMMU in the system.
2854 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002855static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2856 int nelems, int dir)
2857{
2858 struct scatterlist *s;
2859 int i;
2860
2861 for_each_sg(sglist, s, nelems, i) {
2862 s->dma_address = (dma_addr_t)sg_phys(s);
2863 s->dma_length = s->length;
2864 }
2865
2866 return nelems;
2867}
2868
Joerg Roedel431b2a22008-07-11 17:14:22 +02002869/*
2870 * The exported map_sg function for dma_ops (handles scatter-gather
2871 * lists).
2872 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002873static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002874 int nelems, enum dma_data_direction dir,
2875 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002876{
2877 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002878 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002879 int i;
2880 struct scatterlist *s;
2881 phys_addr_t paddr;
2882 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002883 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002884
Joerg Roedeld03f0672008-12-12 15:09:48 +01002885 INC_STATS_COUNTER(cnt_map_sg);
2886
Joerg Roedel94f6d192009-11-24 16:40:02 +01002887 domain = get_domain(dev);
2888 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002889 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002890 else if (IS_ERR(domain))
2891 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002892
Joerg Roedel832a90c2008-09-18 15:54:23 +02002893 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002894
Joerg Roedel65b050a2008-06-26 21:28:02 +02002895 spin_lock_irqsave(&domain->lock, flags);
2896
2897 for_each_sg(sglist, s, nelems, i) {
2898 paddr = sg_phys(s);
2899
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002900 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002901 paddr, s->length, dir, false,
2902 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002903
2904 if (s->dma_address) {
2905 s->dma_length = s->length;
2906 mapped_elems++;
2907 } else
2908 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002909 }
2910
Joerg Roedel17b124b2011-04-06 18:01:35 +02002911 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002912
2913out:
2914 spin_unlock_irqrestore(&domain->lock, flags);
2915
2916 return mapped_elems;
2917unmap:
2918 for_each_sg(sglist, s, mapped_elems, i) {
2919 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002920 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002921 s->dma_length, dir);
2922 s->dma_address = s->dma_length = 0;
2923 }
2924
2925 mapped_elems = 0;
2926
2927 goto out;
2928}
2929
Joerg Roedel431b2a22008-07-11 17:14:22 +02002930/*
2931 * The exported map_sg function for dma_ops (handles scatter-gather
2932 * lists).
2933 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002934static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002935 int nelems, enum dma_data_direction dir,
2936 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002937{
2938 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002939 struct protection_domain *domain;
2940 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002941 int i;
2942
Joerg Roedel55877a62008-12-12 15:12:14 +01002943 INC_STATS_COUNTER(cnt_unmap_sg);
2944
Joerg Roedel94f6d192009-11-24 16:40:02 +01002945 domain = get_domain(dev);
2946 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002947 return;
2948
Joerg Roedel65b050a2008-06-26 21:28:02 +02002949 spin_lock_irqsave(&domain->lock, flags);
2950
2951 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002952 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002953 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002954 s->dma_address = s->dma_length = 0;
2955 }
2956
Joerg Roedel17b124b2011-04-06 18:01:35 +02002957 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002958
2959 spin_unlock_irqrestore(&domain->lock, flags);
2960}
2961
Joerg Roedel431b2a22008-07-11 17:14:22 +02002962/*
2963 * The exported alloc_coherent function for dma_ops.
2964 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002965static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002966 dma_addr_t *dma_addr, gfp_t flag,
2967 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002968{
2969 unsigned long flags;
2970 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002971 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002972 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002973 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002974
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002975 INC_STATS_COUNTER(cnt_alloc_coherent);
2976
Joerg Roedel94f6d192009-11-24 16:40:02 +01002977 domain = get_domain(dev);
2978 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002979 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2980 *dma_addr = __pa(virt_addr);
2981 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002982 } else if (IS_ERR(domain))
2983 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002984
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002985 dma_mask = dev->coherent_dma_mask;
2986 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2987 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002988
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002989 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2990 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302991 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002992
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002993 paddr = virt_to_phys(virt_addr);
2994
Joerg Roedel832a90c2008-09-18 15:54:23 +02002995 if (!dma_mask)
2996 dma_mask = *dev->dma_mask;
2997
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002998 spin_lock_irqsave(&domain->lock, flags);
2999
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003000 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02003001 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003002
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09003003 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02003004 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01003005 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02003006 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003007
Joerg Roedel17b124b2011-04-06 18:01:35 +02003008 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003009
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003010 spin_unlock_irqrestore(&domain->lock, flags);
3011
3012 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01003013
3014out_free:
3015
3016 free_pages((unsigned long)virt_addr, get_order(size));
3017
3018 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003019}
3020
Joerg Roedel431b2a22008-07-11 17:14:22 +02003021/*
3022 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003023 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003024static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003025 void *virt_addr, dma_addr_t dma_addr,
3026 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003027{
3028 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003029 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003030
Joerg Roedel5d31ee72008-12-12 15:16:38 +01003031 INC_STATS_COUNTER(cnt_free_coherent);
3032
Joerg Roedel94f6d192009-11-24 16:40:02 +01003033 domain = get_domain(dev);
3034 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01003035 goto free_mem;
3036
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003037 spin_lock_irqsave(&domain->lock, flags);
3038
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003039 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003040
Joerg Roedel17b124b2011-04-06 18:01:35 +02003041 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003042
3043 spin_unlock_irqrestore(&domain->lock, flags);
3044
3045free_mem:
3046 free_pages((unsigned long)virt_addr, get_order(size));
3047}
3048
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003049/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003050 * This function is called by the DMA layer to find out if we can handle a
3051 * particular device. It is part of the dma_ops.
3052 */
3053static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3054{
Joerg Roedel420aef82009-11-23 16:14:57 +01003055 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003056}
3057
3058/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003059 * The function for pre-allocating protection domains.
3060 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003061 * If the driver core informs the DMA layer if a driver grabs a device
3062 * we don't need to preallocate the protection domains anymore.
3063 * For now we have to.
3064 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003065static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003066{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003067 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003068 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003069 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003070 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003071
Chris Wrightd18c69d2010-04-02 18:27:55 -07003072 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003073
3074 /* Do we handle this device? */
3075 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003076 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003077
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003078 dev_data = get_dev_data(&dev->dev);
3079 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3080 /* Make sure passthrough domain is allocated */
3081 alloc_passthrough_domain();
3082 dev_data->passthrough = true;
3083 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003084 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003085 dev_name(&dev->dev));
3086 }
3087
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003088 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003089 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003090 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003091
3092 devid = get_device_id(&dev->dev);
3093
Joerg Roedel87a64d52009-11-24 17:26:43 +01003094 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003095 if (!dma_dom)
3096 continue;
3097 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003098 dma_dom->target_dev = devid;
3099
Joerg Roedel15898bb2009-11-24 15:39:42 +01003100 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003101
Joerg Roedelbd60b732008-09-11 10:24:48 +02003102 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003103 }
3104}
3105
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003106static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003107 .alloc = alloc_coherent,
3108 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003109 .map_page = map_page,
3110 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003111 .map_sg = map_sg,
3112 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003113 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003114};
3115
Joerg Roedel27c21272011-05-30 15:56:24 +02003116static unsigned device_dma_ops_init(void)
3117{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003118 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003119 struct pci_dev *pdev = NULL;
3120 unsigned unhandled = 0;
3121
3122 for_each_pci_dev(pdev) {
3123 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003124
3125 iommu_ignore_device(&pdev->dev);
3126
Joerg Roedel27c21272011-05-30 15:56:24 +02003127 unhandled += 1;
3128 continue;
3129 }
3130
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003131 dev_data = get_dev_data(&pdev->dev);
3132
3133 if (!dev_data->passthrough)
3134 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3135 else
3136 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003137 }
3138
3139 return unhandled;
3140}
3141
Joerg Roedel431b2a22008-07-11 17:14:22 +02003142/*
3143 * The function which clues the AMD IOMMU driver into dma_ops.
3144 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003145
3146void __init amd_iommu_init_api(void)
3147{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003148 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003149}
3150
Joerg Roedel6631ee92008-06-26 21:28:05 +02003151int __init amd_iommu_init_dma_ops(void)
3152{
3153 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003154 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003155
Joerg Roedel431b2a22008-07-11 17:14:22 +02003156 /*
3157 * first allocate a default protection domain for every IOMMU we
3158 * found in the system. Devices not assigned to any other
3159 * protection domain will be assigned to the default one.
3160 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003161 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003162 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003163 if (iommu->default_dom == NULL)
3164 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003165 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003166 ret = iommu_init_unity_mappings(iommu);
3167 if (ret)
3168 goto free_domains;
3169 }
3170
Joerg Roedel431b2a22008-07-11 17:14:22 +02003171 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003172 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003173 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003174 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003175
3176 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003177 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003178
Joerg Roedel431b2a22008-07-11 17:14:22 +02003179 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003180 unhandled = device_dma_ops_init();
3181 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3182 /* There are unhandled devices - initialize swiotlb for them */
3183 swiotlb = 1;
3184 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003185
Joerg Roedel7f265082008-12-12 13:50:21 +01003186 amd_iommu_stats_init();
3187
Joerg Roedel62410ee2012-06-12 16:42:43 +02003188 if (amd_iommu_unmap_flush)
3189 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3190 else
3191 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3192
Joerg Roedel6631ee92008-06-26 21:28:05 +02003193 return 0;
3194
3195free_domains:
3196
Joerg Roedel3bd22172009-05-04 15:06:20 +02003197 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003198 if (iommu->default_dom)
3199 dma_ops_domain_free(iommu->default_dom);
3200 }
3201
3202 return ret;
3203}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003204
3205/*****************************************************************************
3206 *
3207 * The following functions belong to the exported interface of AMD IOMMU
3208 *
3209 * This interface allows access to lower level functions of the IOMMU
3210 * like protection domain handling and assignement of devices to domains
3211 * which is not possible with the dma_ops interface.
3212 *
3213 *****************************************************************************/
3214
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003215static void cleanup_domain(struct protection_domain *domain)
3216{
Joerg Roedel492667d2009-11-27 13:25:47 +01003217 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003218 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003219
3220 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3221
Joerg Roedel492667d2009-11-27 13:25:47 +01003222 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003223 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003224 atomic_set(&dev_data->bind, 0);
3225 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003226
3227 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3228}
3229
Joerg Roedel26508152009-08-26 16:52:40 +02003230static void protection_domain_free(struct protection_domain *domain)
3231{
3232 if (!domain)
3233 return;
3234
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003235 del_domain_from_list(domain);
3236
Joerg Roedel26508152009-08-26 16:52:40 +02003237 if (domain->id)
3238 domain_id_free(domain->id);
3239
3240 kfree(domain);
3241}
3242
3243static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003244{
3245 struct protection_domain *domain;
3246
3247 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3248 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003249 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003250
3251 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003252 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003253 domain->id = domain_id_alloc();
3254 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003255 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003256 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003257
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003258 add_domain_to_list(domain);
3259
Joerg Roedel26508152009-08-26 16:52:40 +02003260 return domain;
3261
3262out_err:
3263 kfree(domain);
3264
3265 return NULL;
3266}
3267
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003268static int __init alloc_passthrough_domain(void)
3269{
3270 if (pt_domain != NULL)
3271 return 0;
3272
3273 /* allocate passthrough domain */
3274 pt_domain = protection_domain_alloc();
3275 if (!pt_domain)
3276 return -ENOMEM;
3277
3278 pt_domain->mode = PAGE_MODE_NONE;
3279
3280 return 0;
3281}
Joerg Roedel26508152009-08-26 16:52:40 +02003282static int amd_iommu_domain_init(struct iommu_domain *dom)
3283{
3284 struct protection_domain *domain;
3285
3286 domain = protection_domain_alloc();
3287 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003288 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003289
3290 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003291 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3292 if (!domain->pt_root)
3293 goto out_free;
3294
Joerg Roedelf3572db2011-11-23 12:36:25 +01003295 domain->iommu_domain = dom;
3296
Joerg Roedelc156e342008-12-02 18:13:27 +01003297 dom->priv = domain;
3298
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003299 dom->geometry.aperture_start = 0;
3300 dom->geometry.aperture_end = ~0ULL;
3301 dom->geometry.force_aperture = true;
3302
Joerg Roedelc156e342008-12-02 18:13:27 +01003303 return 0;
3304
3305out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003306 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003307
3308 return -ENOMEM;
3309}
3310
Joerg Roedel98383fc2008-12-02 18:34:12 +01003311static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3312{
3313 struct protection_domain *domain = dom->priv;
3314
3315 if (!domain)
3316 return;
3317
3318 if (domain->dev_cnt > 0)
3319 cleanup_domain(domain);
3320
3321 BUG_ON(domain->dev_cnt != 0);
3322
Joerg Roedel132bd682011-11-17 14:18:46 +01003323 if (domain->mode != PAGE_MODE_NONE)
3324 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003325
Joerg Roedel52815b72011-11-17 17:24:28 +01003326 if (domain->flags & PD_IOMMUV2_MASK)
3327 free_gcr3_table(domain);
3328
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003329 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003330
3331 dom->priv = NULL;
3332}
3333
Joerg Roedel684f2882008-12-08 12:07:44 +01003334static void amd_iommu_detach_device(struct iommu_domain *dom,
3335 struct device *dev)
3336{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003337 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003338 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003339 u16 devid;
3340
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003341 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003342 return;
3343
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003344 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003345
Joerg Roedel657cbb62009-11-23 15:26:46 +01003346 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003347 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003348
3349 iommu = amd_iommu_rlookup_table[devid];
3350 if (!iommu)
3351 return;
3352
Joerg Roedel684f2882008-12-08 12:07:44 +01003353 iommu_completion_wait(iommu);
3354}
3355
Joerg Roedel01106062008-12-02 19:34:11 +01003356static int amd_iommu_attach_device(struct iommu_domain *dom,
3357 struct device *dev)
3358{
3359 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003360 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003361 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003362 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003363
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003364 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003365 return -EINVAL;
3366
Joerg Roedel657cbb62009-11-23 15:26:46 +01003367 dev_data = dev->archdata.iommu;
3368
Joerg Roedelf62dda62011-06-09 12:55:35 +02003369 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003370 if (!iommu)
3371 return -EINVAL;
3372
Joerg Roedel657cbb62009-11-23 15:26:46 +01003373 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003374 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003375
Joerg Roedel15898bb2009-11-24 15:39:42 +01003376 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003377
3378 iommu_completion_wait(iommu);
3379
Joerg Roedel15898bb2009-11-24 15:39:42 +01003380 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003381}
3382
Joerg Roedel468e2362010-01-21 16:37:36 +01003383static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003384 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003385{
3386 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003387 int prot = 0;
3388 int ret;
3389
Joerg Roedel132bd682011-11-17 14:18:46 +01003390 if (domain->mode == PAGE_MODE_NONE)
3391 return -EINVAL;
3392
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003393 if (iommu_prot & IOMMU_READ)
3394 prot |= IOMMU_PROT_IR;
3395 if (iommu_prot & IOMMU_WRITE)
3396 prot |= IOMMU_PROT_IW;
3397
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003398 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003399 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003400 mutex_unlock(&domain->api_lock);
3401
Joerg Roedel795e74f2010-05-11 17:40:57 +02003402 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003403}
3404
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003405static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3406 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003407{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003408 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003409 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003410
Joerg Roedel132bd682011-11-17 14:18:46 +01003411 if (domain->mode == PAGE_MODE_NONE)
3412 return -EINVAL;
3413
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003414 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003415 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003416 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003417
Joerg Roedel17b124b2011-04-06 18:01:35 +02003418 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003419
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003420 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003421}
3422
Joerg Roedel645c4c82008-12-02 20:05:50 +01003423static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3424 unsigned long iova)
3425{
3426 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003427 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003428 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003429 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003430
Joerg Roedel132bd682011-11-17 14:18:46 +01003431 if (domain->mode == PAGE_MODE_NONE)
3432 return iova;
3433
Joerg Roedel24cd7722010-01-19 17:27:39 +01003434 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003435
Joerg Roedela6d41a42009-09-02 17:08:55 +02003436 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003437 return 0;
3438
Joerg Roedelf03152b2010-01-21 16:15:24 +01003439 if (PM_PTE_LEVEL(*pte) == 0)
3440 offset_mask = PAGE_SIZE - 1;
3441 else
3442 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3443
3444 __pte = *pte & PM_ADDR_MASK;
3445 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003446
3447 return paddr;
3448}
3449
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003450static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3451 unsigned long cap)
3452{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003453 switch (cap) {
3454 case IOMMU_CAP_CACHE_COHERENCY:
3455 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003456 case IOMMU_CAP_INTR_REMAP:
3457 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003458 }
3459
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003460 return 0;
3461}
3462
Joerg Roedel26961ef2008-12-03 17:00:17 +01003463static struct iommu_ops amd_iommu_ops = {
3464 .domain_init = amd_iommu_domain_init,
3465 .domain_destroy = amd_iommu_domain_destroy,
3466 .attach_dev = amd_iommu_attach_device,
3467 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003468 .map = amd_iommu_map,
3469 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003470 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003471 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003472 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003473};
3474
Joerg Roedel0feae532009-08-26 15:26:30 +02003475/*****************************************************************************
3476 *
3477 * The next functions do a basic initialization of IOMMU for pass through
3478 * mode
3479 *
3480 * In passthrough mode the IOMMU is initialized and enabled but not used for
3481 * DMA-API translation.
3482 *
3483 *****************************************************************************/
3484
3485int __init amd_iommu_init_passthrough(void)
3486{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003487 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003488 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003489 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003490 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003491 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003492
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003493 ret = alloc_passthrough_domain();
3494 if (ret)
3495 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003496
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003497 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003498 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003499 continue;
3500
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003501 dev_data = get_dev_data(&dev->dev);
3502 dev_data->passthrough = true;
3503
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003504 devid = get_device_id(&dev->dev);
3505
Joerg Roedel15898bb2009-11-24 15:39:42 +01003506 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003507 if (!iommu)
3508 continue;
3509
Joerg Roedel15898bb2009-11-24 15:39:42 +01003510 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003511 }
3512
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003513 amd_iommu_stats_init();
3514
Joerg Roedel0feae532009-08-26 15:26:30 +02003515 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3516
3517 return 0;
3518}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003519
3520/* IOMMUv2 specific functions */
3521int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3522{
3523 return atomic_notifier_chain_register(&ppr_notifier, nb);
3524}
3525EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3526
3527int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3528{
3529 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3530}
3531EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003532
3533void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3534{
3535 struct protection_domain *domain = dom->priv;
3536 unsigned long flags;
3537
3538 spin_lock_irqsave(&domain->lock, flags);
3539
3540 /* Update data structure */
3541 domain->mode = PAGE_MODE_NONE;
3542 domain->updated = true;
3543
3544 /* Make changes visible to IOMMUs */
3545 update_domain(domain);
3546
3547 /* Page-table is not visible to IOMMU anymore, so free it */
3548 free_pagetable(domain);
3549
3550 spin_unlock_irqrestore(&domain->lock, flags);
3551}
3552EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003553
3554int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3555{
3556 struct protection_domain *domain = dom->priv;
3557 unsigned long flags;
3558 int levels, ret;
3559
3560 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3561 return -EINVAL;
3562
3563 /* Number of GCR3 table levels required */
3564 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3565 levels += 1;
3566
3567 if (levels > amd_iommu_max_glx_val)
3568 return -EINVAL;
3569
3570 spin_lock_irqsave(&domain->lock, flags);
3571
3572 /*
3573 * Save us all sanity checks whether devices already in the
3574 * domain support IOMMUv2. Just force that the domain has no
3575 * devices attached when it is switched into IOMMUv2 mode.
3576 */
3577 ret = -EBUSY;
3578 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3579 goto out;
3580
3581 ret = -ENOMEM;
3582 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3583 if (domain->gcr3_tbl == NULL)
3584 goto out;
3585
3586 domain->glx = levels;
3587 domain->flags |= PD_IOMMUV2_MASK;
3588 domain->updated = true;
3589
3590 update_domain(domain);
3591
3592 ret = 0;
3593
3594out:
3595 spin_unlock_irqrestore(&domain->lock, flags);
3596
3597 return ret;
3598}
3599EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003600
3601static int __flush_pasid(struct protection_domain *domain, int pasid,
3602 u64 address, bool size)
3603{
3604 struct iommu_dev_data *dev_data;
3605 struct iommu_cmd cmd;
3606 int i, ret;
3607
3608 if (!(domain->flags & PD_IOMMUV2_MASK))
3609 return -EINVAL;
3610
3611 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3612
3613 /*
3614 * IOMMU TLB needs to be flushed before Device TLB to
3615 * prevent device TLB refill from IOMMU TLB
3616 */
3617 for (i = 0; i < amd_iommus_present; ++i) {
3618 if (domain->dev_iommu[i] == 0)
3619 continue;
3620
3621 ret = iommu_queue_command(amd_iommus[i], &cmd);
3622 if (ret != 0)
3623 goto out;
3624 }
3625
3626 /* Wait until IOMMU TLB flushes are complete */
3627 domain_flush_complete(domain);
3628
3629 /* Now flush device TLBs */
3630 list_for_each_entry(dev_data, &domain->dev_list, list) {
3631 struct amd_iommu *iommu;
3632 int qdep;
3633
3634 BUG_ON(!dev_data->ats.enabled);
3635
3636 qdep = dev_data->ats.qdep;
3637 iommu = amd_iommu_rlookup_table[dev_data->devid];
3638
3639 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3640 qdep, address, size);
3641
3642 ret = iommu_queue_command(iommu, &cmd);
3643 if (ret != 0)
3644 goto out;
3645 }
3646
3647 /* Wait until all device TLBs are flushed */
3648 domain_flush_complete(domain);
3649
3650 ret = 0;
3651
3652out:
3653
3654 return ret;
3655}
3656
3657static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3658 u64 address)
3659{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003660 INC_STATS_COUNTER(invalidate_iotlb);
3661
Joerg Roedel22e266c2011-11-21 15:59:08 +01003662 return __flush_pasid(domain, pasid, address, false);
3663}
3664
3665int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3666 u64 address)
3667{
3668 struct protection_domain *domain = dom->priv;
3669 unsigned long flags;
3670 int ret;
3671
3672 spin_lock_irqsave(&domain->lock, flags);
3673 ret = __amd_iommu_flush_page(domain, pasid, address);
3674 spin_unlock_irqrestore(&domain->lock, flags);
3675
3676 return ret;
3677}
3678EXPORT_SYMBOL(amd_iommu_flush_page);
3679
3680static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3681{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003682 INC_STATS_COUNTER(invalidate_iotlb_all);
3683
Joerg Roedel22e266c2011-11-21 15:59:08 +01003684 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3685 true);
3686}
3687
3688int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3689{
3690 struct protection_domain *domain = dom->priv;
3691 unsigned long flags;
3692 int ret;
3693
3694 spin_lock_irqsave(&domain->lock, flags);
3695 ret = __amd_iommu_flush_tlb(domain, pasid);
3696 spin_unlock_irqrestore(&domain->lock, flags);
3697
3698 return ret;
3699}
3700EXPORT_SYMBOL(amd_iommu_flush_tlb);
3701
Joerg Roedelb16137b2011-11-21 16:50:23 +01003702static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3703{
3704 int index;
3705 u64 *pte;
3706
3707 while (true) {
3708
3709 index = (pasid >> (9 * level)) & 0x1ff;
3710 pte = &root[index];
3711
3712 if (level == 0)
3713 break;
3714
3715 if (!(*pte & GCR3_VALID)) {
3716 if (!alloc)
3717 return NULL;
3718
3719 root = (void *)get_zeroed_page(GFP_ATOMIC);
3720 if (root == NULL)
3721 return NULL;
3722
3723 *pte = __pa(root) | GCR3_VALID;
3724 }
3725
3726 root = __va(*pte & PAGE_MASK);
3727
3728 level -= 1;
3729 }
3730
3731 return pte;
3732}
3733
3734static int __set_gcr3(struct protection_domain *domain, int pasid,
3735 unsigned long cr3)
3736{
3737 u64 *pte;
3738
3739 if (domain->mode != PAGE_MODE_NONE)
3740 return -EINVAL;
3741
3742 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3743 if (pte == NULL)
3744 return -ENOMEM;
3745
3746 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3747
3748 return __amd_iommu_flush_tlb(domain, pasid);
3749}
3750
3751static int __clear_gcr3(struct protection_domain *domain, int pasid)
3752{
3753 u64 *pte;
3754
3755 if (domain->mode != PAGE_MODE_NONE)
3756 return -EINVAL;
3757
3758 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3759 if (pte == NULL)
3760 return 0;
3761
3762 *pte = 0;
3763
3764 return __amd_iommu_flush_tlb(domain, pasid);
3765}
3766
3767int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3768 unsigned long cr3)
3769{
3770 struct protection_domain *domain = dom->priv;
3771 unsigned long flags;
3772 int ret;
3773
3774 spin_lock_irqsave(&domain->lock, flags);
3775 ret = __set_gcr3(domain, pasid, cr3);
3776 spin_unlock_irqrestore(&domain->lock, flags);
3777
3778 return ret;
3779}
3780EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3781
3782int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3783{
3784 struct protection_domain *domain = dom->priv;
3785 unsigned long flags;
3786 int ret;
3787
3788 spin_lock_irqsave(&domain->lock, flags);
3789 ret = __clear_gcr3(domain, pasid);
3790 spin_unlock_irqrestore(&domain->lock, flags);
3791
3792 return ret;
3793}
3794EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003795
3796int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3797 int status, int tag)
3798{
3799 struct iommu_dev_data *dev_data;
3800 struct amd_iommu *iommu;
3801 struct iommu_cmd cmd;
3802
Joerg Roedel399be2f2011-12-01 16:53:47 +01003803 INC_STATS_COUNTER(complete_ppr);
3804
Joerg Roedelc99afa22011-11-21 18:19:25 +01003805 dev_data = get_dev_data(&pdev->dev);
3806 iommu = amd_iommu_rlookup_table[dev_data->devid];
3807
3808 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3809 tag, dev_data->pri_tlp);
3810
3811 return iommu_queue_command(iommu, &cmd);
3812}
3813EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003814
3815struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3816{
3817 struct protection_domain *domain;
3818
3819 domain = get_domain(&pdev->dev);
3820 if (IS_ERR(domain))
3821 return NULL;
3822
3823 /* Only return IOMMUv2 domains */
3824 if (!(domain->flags & PD_IOMMUV2_MASK))
3825 return NULL;
3826
3827 return domain->iommu_domain;
3828}
3829EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003830
3831void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3832{
3833 struct iommu_dev_data *dev_data;
3834
3835 if (!amd_iommu_v2_supported())
3836 return;
3837
3838 dev_data = get_dev_data(&pdev->dev);
3839 dev_data->errata |= (1 << erratum);
3840}
3841EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003842
3843int amd_iommu_device_info(struct pci_dev *pdev,
3844 struct amd_iommu_device_info *info)
3845{
3846 int max_pasids;
3847 int pos;
3848
3849 if (pdev == NULL || info == NULL)
3850 return -EINVAL;
3851
3852 if (!amd_iommu_v2_supported())
3853 return -EINVAL;
3854
3855 memset(info, 0, sizeof(*info));
3856
3857 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3858 if (pos)
3859 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3860
3861 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3862 if (pos)
3863 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3864
3865 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3866 if (pos) {
3867 int features;
3868
3869 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3870 max_pasids = min(max_pasids, (1 << 20));
3871
3872 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3873 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3874
3875 features = pci_pasid_features(pdev);
3876 if (features & PCI_PASID_CAP_EXEC)
3877 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3878 if (features & PCI_PASID_CAP_PRIV)
3879 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3880 }
3881
3882 return 0;
3883}
3884EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003885
3886#ifdef CONFIG_IRQ_REMAP
3887
3888/*****************************************************************************
3889 *
3890 * Interrupt Remapping Implementation
3891 *
3892 *****************************************************************************/
3893
3894union irte {
3895 u32 val;
3896 struct {
3897 u32 valid : 1,
3898 no_fault : 1,
3899 int_type : 3,
3900 rq_eoi : 1,
3901 dm : 1,
3902 rsvd_1 : 1,
3903 destination : 8,
3904 vector : 8,
3905 rsvd_2 : 8;
3906 } fields;
3907};
3908
3909#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3910#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3911#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3912#define DTE_IRQ_REMAP_ENABLE 1ULL
3913
3914static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3915{
3916 u64 dte;
3917
3918 dte = amd_iommu_dev_table[devid].data[2];
3919 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3920 dte |= virt_to_phys(table->table);
3921 dte |= DTE_IRQ_REMAP_INTCTL;
3922 dte |= DTE_IRQ_TABLE_LEN;
3923 dte |= DTE_IRQ_REMAP_ENABLE;
3924
3925 amd_iommu_dev_table[devid].data[2] = dte;
3926}
3927
3928#define IRTE_ALLOCATED (~1U)
3929
3930static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3931{
3932 struct irq_remap_table *table = NULL;
3933 struct amd_iommu *iommu;
3934 unsigned long flags;
3935 u16 alias;
3936
3937 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3938
3939 iommu = amd_iommu_rlookup_table[devid];
3940 if (!iommu)
3941 goto out_unlock;
3942
3943 table = irq_lookup_table[devid];
3944 if (table)
3945 goto out;
3946
3947 alias = amd_iommu_alias_table[devid];
3948 table = irq_lookup_table[alias];
3949 if (table) {
3950 irq_lookup_table[devid] = table;
3951 set_dte_irq_entry(devid, table);
3952 iommu_flush_dte(iommu, devid);
3953 goto out;
3954 }
3955
3956 /* Nothing there yet, allocate new irq remapping table */
3957 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3958 if (!table)
3959 goto out;
3960
3961 if (ioapic)
3962 /* Keep the first 32 indexes free for IOAPIC interrupts */
3963 table->min_index = 32;
3964
3965 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3966 if (!table->table) {
3967 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003968 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003969 goto out;
3970 }
3971
3972 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3973
3974 if (ioapic) {
3975 int i;
3976
3977 for (i = 0; i < 32; ++i)
3978 table->table[i] = IRTE_ALLOCATED;
3979 }
3980
3981 irq_lookup_table[devid] = table;
3982 set_dte_irq_entry(devid, table);
3983 iommu_flush_dte(iommu, devid);
3984 if (devid != alias) {
3985 irq_lookup_table[alias] = table;
3986 set_dte_irq_entry(devid, table);
3987 iommu_flush_dte(iommu, alias);
3988 }
3989
3990out:
3991 iommu_completion_wait(iommu);
3992
3993out_unlock:
3994 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3995
3996 return table;
3997}
3998
3999static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
4000{
4001 struct irq_remap_table *table;
4002 unsigned long flags;
4003 int index, c;
4004
4005 table = get_irq_table(devid, false);
4006 if (!table)
4007 return -ENODEV;
4008
4009 spin_lock_irqsave(&table->lock, flags);
4010
4011 /* Scan table for free entries */
4012 for (c = 0, index = table->min_index;
4013 index < MAX_IRQS_PER_TABLE;
4014 ++index) {
4015 if (table->table[index] == 0)
4016 c += 1;
4017 else
4018 c = 0;
4019
4020 if (c == count) {
4021 struct irq_2_iommu *irte_info;
4022
4023 for (; c != 0; --c)
4024 table->table[index - c + 1] = IRTE_ALLOCATED;
4025
4026 index -= count - 1;
4027
4028 irte_info = &cfg->irq_2_iommu;
4029 irte_info->sub_handle = devid;
4030 irte_info->irte_index = index;
4031 irte_info->iommu = (void *)cfg;
4032
4033 goto out;
4034 }
4035 }
4036
4037 index = -ENOSPC;
4038
4039out:
4040 spin_unlock_irqrestore(&table->lock, flags);
4041
4042 return index;
4043}
4044
4045static int get_irte(u16 devid, int index, union irte *irte)
4046{
4047 struct irq_remap_table *table;
4048 unsigned long flags;
4049
4050 table = get_irq_table(devid, false);
4051 if (!table)
4052 return -ENOMEM;
4053
4054 spin_lock_irqsave(&table->lock, flags);
4055 irte->val = table->table[index];
4056 spin_unlock_irqrestore(&table->lock, flags);
4057
4058 return 0;
4059}
4060
4061static int modify_irte(u16 devid, int index, union irte irte)
4062{
4063 struct irq_remap_table *table;
4064 struct amd_iommu *iommu;
4065 unsigned long flags;
4066
4067 iommu = amd_iommu_rlookup_table[devid];
4068 if (iommu == NULL)
4069 return -EINVAL;
4070
4071 table = get_irq_table(devid, false);
4072 if (!table)
4073 return -ENOMEM;
4074
4075 spin_lock_irqsave(&table->lock, flags);
4076 table->table[index] = irte.val;
4077 spin_unlock_irqrestore(&table->lock, flags);
4078
4079 iommu_flush_irt(iommu, devid);
4080 iommu_completion_wait(iommu);
4081
4082 return 0;
4083}
4084
4085static void free_irte(u16 devid, int index)
4086{
4087 struct irq_remap_table *table;
4088 struct amd_iommu *iommu;
4089 unsigned long flags;
4090
4091 iommu = amd_iommu_rlookup_table[devid];
4092 if (iommu == NULL)
4093 return;
4094
4095 table = get_irq_table(devid, false);
4096 if (!table)
4097 return;
4098
4099 spin_lock_irqsave(&table->lock, flags);
4100 table->table[index] = 0;
4101 spin_unlock_irqrestore(&table->lock, flags);
4102
4103 iommu_flush_irt(iommu, devid);
4104 iommu_completion_wait(iommu);
4105}
4106
Joerg Roedel5527de72012-06-26 11:17:32 +02004107static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4108 unsigned int destination, int vector,
4109 struct io_apic_irq_attr *attr)
4110{
4111 struct irq_remap_table *table;
4112 struct irq_2_iommu *irte_info;
4113 struct irq_cfg *cfg;
4114 union irte irte;
4115 int ioapic_id;
4116 int index;
4117 int devid;
4118 int ret;
4119
4120 cfg = irq_get_chip_data(irq);
4121 if (!cfg)
4122 return -EINVAL;
4123
4124 irte_info = &cfg->irq_2_iommu;
4125 ioapic_id = mpc_ioapic_id(attr->ioapic);
4126 devid = get_ioapic_devid(ioapic_id);
4127
4128 if (devid < 0)
4129 return devid;
4130
4131 table = get_irq_table(devid, true);
4132 if (table == NULL)
4133 return -ENOMEM;
4134
4135 index = attr->ioapic_pin;
4136
4137 /* Setup IRQ remapping info */
4138 irte_info->sub_handle = devid;
4139 irte_info->irte_index = index;
4140 irte_info->iommu = (void *)cfg;
4141
4142 /* Setup IRTE for IOMMU */
4143 irte.val = 0;
4144 irte.fields.vector = vector;
4145 irte.fields.int_type = apic->irq_delivery_mode;
4146 irte.fields.destination = destination;
4147 irte.fields.dm = apic->irq_dest_mode;
4148 irte.fields.valid = 1;
4149
4150 ret = modify_irte(devid, index, irte);
4151 if (ret)
4152 return ret;
4153
4154 /* Setup IOAPIC entry */
4155 memset(entry, 0, sizeof(*entry));
4156
4157 entry->vector = index;
4158 entry->mask = 0;
4159 entry->trigger = attr->trigger;
4160 entry->polarity = attr->polarity;
4161
4162 /*
4163 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004164 */
4165 if (attr->trigger)
4166 entry->mask = 1;
4167
4168 return 0;
4169}
4170
4171static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4172 bool force)
4173{
4174 struct irq_2_iommu *irte_info;
4175 unsigned int dest, irq;
4176 struct irq_cfg *cfg;
4177 union irte irte;
4178 int err;
4179
4180 if (!config_enabled(CONFIG_SMP))
4181 return -1;
4182
4183 cfg = data->chip_data;
4184 irq = data->irq;
4185 irte_info = &cfg->irq_2_iommu;
4186
4187 if (!cpumask_intersects(mask, cpu_online_mask))
4188 return -EINVAL;
4189
4190 if (get_irte(irte_info->sub_handle, irte_info->irte_index, &irte))
4191 return -EBUSY;
4192
4193 if (assign_irq_vector(irq, cfg, mask))
4194 return -EBUSY;
4195
4196 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4197 if (err) {
4198 if (assign_irq_vector(irq, cfg, data->affinity))
4199 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4200 return err;
4201 }
4202
4203 irte.fields.vector = cfg->vector;
4204 irte.fields.destination = dest;
4205
4206 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4207
4208 if (cfg->move_in_progress)
4209 send_cleanup_vector(cfg);
4210
4211 cpumask_copy(data->affinity, mask);
4212
4213 return 0;
4214}
4215
4216static int free_irq(int irq)
4217{
4218 struct irq_2_iommu *irte_info;
4219 struct irq_cfg *cfg;
4220
4221 cfg = irq_get_chip_data(irq);
4222 if (!cfg)
4223 return -EINVAL;
4224
4225 irte_info = &cfg->irq_2_iommu;
4226
4227 free_irte(irte_info->sub_handle, irte_info->irte_index);
4228
4229 return 0;
4230}
4231
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004232static void compose_msi_msg(struct pci_dev *pdev,
4233 unsigned int irq, unsigned int dest,
4234 struct msi_msg *msg, u8 hpet_id)
4235{
4236 struct irq_2_iommu *irte_info;
4237 struct irq_cfg *cfg;
4238 union irte irte;
4239
4240 cfg = irq_get_chip_data(irq);
4241 if (!cfg)
4242 return;
4243
4244 irte_info = &cfg->irq_2_iommu;
4245
4246 irte.val = 0;
4247 irte.fields.vector = cfg->vector;
4248 irte.fields.int_type = apic->irq_delivery_mode;
4249 irte.fields.destination = dest;
4250 irte.fields.dm = apic->irq_dest_mode;
4251 irte.fields.valid = 1;
4252
4253 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4254
4255 msg->address_hi = MSI_ADDR_BASE_HI;
4256 msg->address_lo = MSI_ADDR_BASE_LO;
4257 msg->data = irte_info->irte_index;
4258}
4259
4260static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4261{
4262 struct irq_cfg *cfg;
4263 int index;
4264 u16 devid;
4265
4266 if (!pdev)
4267 return -EINVAL;
4268
4269 cfg = irq_get_chip_data(irq);
4270 if (!cfg)
4271 return -EINVAL;
4272
4273 devid = get_device_id(&pdev->dev);
4274 index = alloc_irq_index(cfg, devid, nvec);
4275
4276 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4277}
4278
4279static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4280 int index, int offset)
4281{
4282 struct irq_2_iommu *irte_info;
4283 struct irq_cfg *cfg;
4284 u16 devid;
4285
4286 if (!pdev)
4287 return -EINVAL;
4288
4289 cfg = irq_get_chip_data(irq);
4290 if (!cfg)
4291 return -EINVAL;
4292
4293 if (index >= MAX_IRQS_PER_TABLE)
4294 return 0;
4295
4296 devid = get_device_id(&pdev->dev);
4297 irte_info = &cfg->irq_2_iommu;
4298
4299 irte_info->sub_handle = devid;
4300 irte_info->irte_index = index + offset;
4301 irte_info->iommu = (void *)cfg;
4302
4303 return 0;
4304}
4305
Joerg Roedeld9761952012-06-26 16:00:08 +02004306static int setup_hpet_msi(unsigned int irq, unsigned int id)
4307{
4308 struct irq_2_iommu *irte_info;
4309 struct irq_cfg *cfg;
4310 int index, devid;
4311
4312 cfg = irq_get_chip_data(irq);
4313 if (!cfg)
4314 return -EINVAL;
4315
4316 irte_info = &cfg->irq_2_iommu;
4317 devid = get_hpet_devid(id);
4318 if (devid < 0)
4319 return devid;
4320
4321 index = alloc_irq_index(cfg, devid, 1);
4322 if (index < 0)
4323 return index;
4324
4325 irte_info->sub_handle = devid;
4326 irte_info->irte_index = index;
4327 irte_info->iommu = (void *)cfg;
4328
4329 return 0;
4330}
4331
Joerg Roedel6b474b82012-06-26 16:46:04 +02004332struct irq_remap_ops amd_iommu_irq_ops = {
4333 .supported = amd_iommu_supported,
4334 .prepare = amd_iommu_prepare,
4335 .enable = amd_iommu_enable,
4336 .disable = amd_iommu_disable,
4337 .reenable = amd_iommu_reenable,
4338 .enable_faulting = amd_iommu_enable_faulting,
4339 .setup_ioapic_entry = setup_ioapic_entry,
4340 .set_affinity = set_affinity,
4341 .free_irq = free_irq,
4342 .compose_msi_msg = compose_msi_msg,
4343 .msi_alloc_irq = msi_alloc_irq,
4344 .msi_setup_irq = msi_setup_irq,
4345 .setup_hpet_msi = setup_hpet_msi,
4346};
Joerg Roedel2b324502012-06-21 16:29:10 +02004347#endif