blob: 98d74ab564b64114fd63a3f3819d7b957ab4a2da [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.
Joerg Roedel954e3dd2012-12-02 15:35:37 +010069 *
70 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020071 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010072#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020073
Joerg Roedelb6c02712008-06-26 21:27:53 +020074static DEFINE_RWLOCK(amd_iommu_devtable_lock);
75
Joerg Roedelbd60b732008-09-11 10:24:48 +020076/* A list of preallocated protection domains */
77static LIST_HEAD(iommu_pd_list);
78static DEFINE_SPINLOCK(iommu_pd_list_lock);
79
Joerg Roedel8fa5f802011-06-09 12:24:45 +020080/* List of all available dev_data structures */
81static LIST_HEAD(dev_data_list);
82static DEFINE_SPINLOCK(dev_data_list_lock);
83
Joerg Roedel6efed632012-06-14 15:52:58 +020084LIST_HEAD(ioapic_map);
85LIST_HEAD(hpet_map);
86
Joerg Roedel0feae532009-08-26 15:26:30 +020087/*
88 * Domain for untranslated devices - only allocated
89 * if iommu=pt passed on kernel cmd line.
90 */
91static struct protection_domain *pt_domain;
92
Joerg Roedel26961ef2008-12-03 17:00:17 +010093static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010094
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010095static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010096int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010097
Joerg Roedelac1534a2012-06-21 14:52:40 +020098static struct dma_map_ops amd_iommu_dma_ops;
99
Joerg Roedel431b2a22008-07-11 17:14:22 +0200100/*
101 * general struct to manage commands send to an IOMMU
102 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200103struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200104 u32 data[4];
105};
106
Joerg Roedel05152a02012-06-15 16:53:51 +0200107struct kmem_cache *amd_iommu_irq_cache;
108
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200109static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100110static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700111
Joerg Roedel15898bb2009-11-24 15:39:42 +0100112/****************************************************************************
113 *
114 * Helper functions
115 *
116 ****************************************************************************/
117
Joerg Roedelf62dda62011-06-09 12:55:35 +0200118static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200119{
120 struct iommu_dev_data *dev_data;
121 unsigned long flags;
122
123 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
124 if (!dev_data)
125 return NULL;
126
Joerg Roedelf62dda62011-06-09 12:55:35 +0200127 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200128 atomic_set(&dev_data->bind, 0);
129
130 spin_lock_irqsave(&dev_data_list_lock, flags);
131 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
132 spin_unlock_irqrestore(&dev_data_list_lock, flags);
133
134 return dev_data;
135}
136
137static void free_dev_data(struct iommu_dev_data *dev_data)
138{
139 unsigned long flags;
140
141 spin_lock_irqsave(&dev_data_list_lock, flags);
142 list_del(&dev_data->dev_data_list);
143 spin_unlock_irqrestore(&dev_data_list_lock, flags);
144
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600145 if (dev_data->group)
146 iommu_group_put(dev_data->group);
147
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200148 kfree(dev_data);
149}
150
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200151static struct iommu_dev_data *search_dev_data(u16 devid)
152{
153 struct iommu_dev_data *dev_data;
154 unsigned long flags;
155
156 spin_lock_irqsave(&dev_data_list_lock, flags);
157 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
158 if (dev_data->devid == devid)
159 goto out_unlock;
160 }
161
162 dev_data = NULL;
163
164out_unlock:
165 spin_unlock_irqrestore(&dev_data_list_lock, flags);
166
167 return dev_data;
168}
169
170static struct iommu_dev_data *find_dev_data(u16 devid)
171{
172 struct iommu_dev_data *dev_data;
173
174 dev_data = search_dev_data(devid);
175
176 if (dev_data == NULL)
177 dev_data = alloc_dev_data(devid);
178
179 return dev_data;
180}
181
Joerg Roedel15898bb2009-11-24 15:39:42 +0100182static inline u16 get_device_id(struct device *dev)
183{
184 struct pci_dev *pdev = to_pci_dev(dev);
185
186 return calc_devid(pdev->bus->number, pdev->devfn);
187}
188
Joerg Roedel657cbb62009-11-23 15:26:46 +0100189static struct iommu_dev_data *get_dev_data(struct device *dev)
190{
191 return dev->archdata.iommu;
192}
193
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100194static bool pci_iommuv2_capable(struct pci_dev *pdev)
195{
196 static const int caps[] = {
197 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100198 PCI_EXT_CAP_ID_PRI,
199 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100200 };
201 int i, pos;
202
203 for (i = 0; i < 3; ++i) {
204 pos = pci_find_ext_capability(pdev, caps[i]);
205 if (pos == 0)
206 return false;
207 }
208
209 return true;
210}
211
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100212static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
213{
214 struct iommu_dev_data *dev_data;
215
216 dev_data = get_dev_data(&pdev->dev);
217
218 return dev_data->errata & (1 << erratum) ? true : false;
219}
220
Joerg Roedel71c70982009-11-24 16:43:06 +0100221/*
222 * In this function the list of preallocated protection domains is traversed to
223 * find the domain for a specific device
224 */
225static struct dma_ops_domain *find_protection_domain(u16 devid)
226{
227 struct dma_ops_domain *entry, *ret = NULL;
228 unsigned long flags;
229 u16 alias = amd_iommu_alias_table[devid];
230
231 if (list_empty(&iommu_pd_list))
232 return NULL;
233
234 spin_lock_irqsave(&iommu_pd_list_lock, flags);
235
236 list_for_each_entry(entry, &iommu_pd_list, list) {
237 if (entry->target_dev == devid ||
238 entry->target_dev == alias) {
239 ret = entry;
240 break;
241 }
242 }
243
244 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
245
246 return ret;
247}
248
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100249/*
250 * This function checks if the driver got a valid device from the caller to
251 * avoid dereferencing invalid pointers.
252 */
253static bool check_device(struct device *dev)
254{
255 u16 devid;
256
257 if (!dev || !dev->dma_mask)
258 return false;
259
260 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100261 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100262 return false;
263
264 devid = get_device_id(dev);
265
266 /* Out of our scope? */
267 if (devid > amd_iommu_last_bdf)
268 return false;
269
270 if (amd_iommu_rlookup_table[devid] == NULL)
271 return false;
272
273 return true;
274}
275
Alex Williamson664b6002012-05-30 14:19:31 -0600276static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
277{
278 pci_dev_put(*from);
279 *from = to;
280}
281
Alex Williamson2bff6a52012-10-08 22:49:48 -0600282static struct pci_bus *find_hosted_bus(struct pci_bus *bus)
283{
284 while (!bus->self) {
285 if (!pci_is_root_bus(bus))
286 bus = bus->parent;
287 else
288 return ERR_PTR(-ENODEV);
289 }
290
291 return bus;
292}
293
Alex Williamson664b6002012-05-30 14:19:31 -0600294#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
295
Alex Williamson2851db22012-10-08 22:49:41 -0600296static struct pci_dev *get_isolation_root(struct pci_dev *pdev)
Joerg Roedel657cbb62009-11-23 15:26:46 +0100297{
Alex Williamson2851db22012-10-08 22:49:41 -0600298 struct pci_dev *dma_pdev = pdev;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600299
Alex Williamson31fe9432012-08-04 12:09:03 -0600300 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600301 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
302
Alex Williamson31fe9432012-08-04 12:09:03 -0600303 /*
304 * If it's a multifunction device that does not support our
305 * required ACS flags, add to the same group as function 0.
306 */
Alex Williamson664b6002012-05-30 14:19:31 -0600307 if (dma_pdev->multifunction &&
308 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
309 swap_pci_ref(&dma_pdev,
310 pci_get_slot(dma_pdev->bus,
311 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
312 0)));
313
Alex Williamson31fe9432012-08-04 12:09:03 -0600314 /*
315 * Devices on the root bus go through the iommu. If that's not us,
316 * find the next upstream device and test ACS up to the root bus.
317 * Finding the next device may require skipping virtual buses.
318 */
Alex Williamson664b6002012-05-30 14:19:31 -0600319 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson2bff6a52012-10-08 22:49:48 -0600320 struct pci_bus *bus = find_hosted_bus(dma_pdev->bus);
321 if (IS_ERR(bus))
322 break;
Alex Williamson31fe9432012-08-04 12:09:03 -0600323
324 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600325 break;
326
Alex Williamson31fe9432012-08-04 12:09:03 -0600327 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200328 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100329
Alex Williamson2851db22012-10-08 22:49:41 -0600330 return dma_pdev;
331}
332
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600333static int use_pdev_iommu_group(struct pci_dev *pdev, struct device *dev)
334{
335 struct iommu_group *group = iommu_group_get(&pdev->dev);
336 int ret;
337
338 if (!group) {
339 group = iommu_group_alloc();
340 if (IS_ERR(group))
341 return PTR_ERR(group);
342
343 WARN_ON(&pdev->dev != dev);
344 }
345
346 ret = iommu_group_add_device(group, dev);
347 iommu_group_put(group);
348 return ret;
349}
350
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600351static int use_dev_data_iommu_group(struct iommu_dev_data *dev_data,
352 struct device *dev)
353{
354 if (!dev_data->group) {
355 struct iommu_group *group = iommu_group_alloc();
356 if (IS_ERR(group))
357 return PTR_ERR(group);
358
359 dev_data->group = group;
360 }
361
362 return iommu_group_add_device(dev_data->group, dev);
363}
364
Alex Williamson2851db22012-10-08 22:49:41 -0600365static int init_iommu_group(struct device *dev)
366{
367 struct iommu_dev_data *dev_data;
368 struct iommu_group *group;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600369 struct pci_dev *dma_pdev;
Alex Williamson2851db22012-10-08 22:49:41 -0600370 int ret;
371
372 group = iommu_group_get(dev);
373 if (group) {
374 iommu_group_put(group);
375 return 0;
376 }
377
378 dev_data = find_dev_data(get_device_id(dev));
379 if (!dev_data)
380 return -ENOMEM;
381
382 if (dev_data->alias_data) {
383 u16 alias;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600384 struct pci_bus *bus;
Alex Williamson2851db22012-10-08 22:49:41 -0600385
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600386 if (dev_data->alias_data->group)
387 goto use_group;
388
389 /*
390 * If the alias device exists, it's effectively just a first
391 * level quirk for finding the DMA source.
392 */
Alex Williamson2851db22012-10-08 22:49:41 -0600393 alias = amd_iommu_alias_table[dev_data->devid];
394 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600395 if (dma_pdev) {
396 dma_pdev = get_isolation_root(dma_pdev);
397 goto use_pdev;
398 }
399
400 /*
401 * If the alias is virtual, try to find a parent device
402 * and test whether the IOMMU group is actualy rooted above
403 * the alias. Be careful to also test the parent device if
404 * we think the alias is the root of the group.
405 */
406 bus = pci_find_bus(0, alias >> 8);
407 if (!bus)
408 goto use_group;
409
410 bus = find_hosted_bus(bus);
411 if (IS_ERR(bus) || !bus->self)
412 goto use_group;
413
414 dma_pdev = get_isolation_root(pci_dev_get(bus->self));
415 if (dma_pdev != bus->self || (dma_pdev->multifunction &&
416 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)))
417 goto use_pdev;
418
419 pci_dev_put(dma_pdev);
420 goto use_group;
Alex Williamson2851db22012-10-08 22:49:41 -0600421 }
422
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600423 dma_pdev = get_isolation_root(pci_dev_get(to_pci_dev(dev)));
424use_pdev:
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600425 ret = use_pdev_iommu_group(dma_pdev, dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600426 pci_dev_put(dma_pdev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600427 return ret;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600428use_group:
429 return use_dev_data_iommu_group(dev_data->alias_data, dev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600430}
431
432static int iommu_init_device(struct device *dev)
433{
434 struct pci_dev *pdev = to_pci_dev(dev);
435 struct iommu_dev_data *dev_data;
436 u16 alias;
437 int ret;
438
439 if (dev->archdata.iommu)
440 return 0;
441
442 dev_data = find_dev_data(get_device_id(dev));
443 if (!dev_data)
444 return -ENOMEM;
445
446 alias = amd_iommu_alias_table[dev_data->devid];
447 if (alias != dev_data->devid) {
448 struct iommu_dev_data *alias_data;
449
450 alias_data = find_dev_data(alias);
451 if (alias_data == NULL) {
452 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
453 dev_name(dev));
454 free_dev_data(dev_data);
455 return -ENOTSUPP;
456 }
457 dev_data->alias_data = alias_data;
458 }
459
460 ret = init_iommu_group(dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600461 if (ret)
462 return ret;
463
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100464 if (pci_iommuv2_capable(pdev)) {
465 struct amd_iommu *iommu;
466
467 iommu = amd_iommu_rlookup_table[dev_data->devid];
468 dev_data->iommu_v2 = iommu->is_iommu_v2;
469 }
470
Joerg Roedel657cbb62009-11-23 15:26:46 +0100471 dev->archdata.iommu = dev_data;
472
Joerg Roedel657cbb62009-11-23 15:26:46 +0100473 return 0;
474}
475
Joerg Roedel26018872011-06-06 16:50:14 +0200476static void iommu_ignore_device(struct device *dev)
477{
478 u16 devid, alias;
479
480 devid = get_device_id(dev);
481 alias = amd_iommu_alias_table[devid];
482
483 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
484 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
485
486 amd_iommu_rlookup_table[devid] = NULL;
487 amd_iommu_rlookup_table[alias] = NULL;
488}
489
Joerg Roedel657cbb62009-11-23 15:26:46 +0100490static void iommu_uninit_device(struct device *dev)
491{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600492 iommu_group_remove_device(dev);
493
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200494 /*
495 * Nothing to do here - we keep dev_data around for unplugged devices
496 * and reuse it when the device is re-plugged - not doing so would
497 * introduce a ton of races.
498 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100499}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100500
501void __init amd_iommu_uninit_devices(void)
502{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200503 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100504 struct pci_dev *pdev = NULL;
505
506 for_each_pci_dev(pdev) {
507
508 if (!check_device(&pdev->dev))
509 continue;
510
511 iommu_uninit_device(&pdev->dev);
512 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200513
514 /* Free all of our dev_data structures */
515 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
516 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100517}
518
519int __init amd_iommu_init_devices(void)
520{
521 struct pci_dev *pdev = NULL;
522 int ret = 0;
523
524 for_each_pci_dev(pdev) {
525
526 if (!check_device(&pdev->dev))
527 continue;
528
529 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200530 if (ret == -ENOTSUPP)
531 iommu_ignore_device(&pdev->dev);
532 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100533 goto out_free;
534 }
535
536 return 0;
537
538out_free:
539
540 amd_iommu_uninit_devices();
541
542 return ret;
543}
Joerg Roedel7f265082008-12-12 13:50:21 +0100544#ifdef CONFIG_AMD_IOMMU_STATS
545
546/*
547 * Initialization code for statistics collection
548 */
549
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100550DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100551DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100552DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100553DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100554DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100555DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100556DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100557DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100558DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100559DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100560DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100561DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100562DECLARE_STATS_COUNTER(complete_ppr);
563DECLARE_STATS_COUNTER(invalidate_iotlb);
564DECLARE_STATS_COUNTER(invalidate_iotlb_all);
565DECLARE_STATS_COUNTER(pri_requests);
566
Joerg Roedel7f265082008-12-12 13:50:21 +0100567static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100568static struct dentry *de_fflush;
569
570static void amd_iommu_stats_add(struct __iommu_counter *cnt)
571{
572 if (stats_dir == NULL)
573 return;
574
575 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
576 &cnt->value);
577}
578
579static void amd_iommu_stats_init(void)
580{
581 stats_dir = debugfs_create_dir("amd-iommu", NULL);
582 if (stats_dir == NULL)
583 return;
584
Joerg Roedel7f265082008-12-12 13:50:21 +0100585 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300586 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100587
588 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100589 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100590 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100591 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100592 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100593 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100594 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100595 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100596 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100597 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100598 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100599 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100600 amd_iommu_stats_add(&complete_ppr);
601 amd_iommu_stats_add(&invalidate_iotlb);
602 amd_iommu_stats_add(&invalidate_iotlb_all);
603 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100604}
605
606#endif
607
Joerg Roedel431b2a22008-07-11 17:14:22 +0200608/****************************************************************************
609 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200610 * Interrupt handling functions
611 *
612 ****************************************************************************/
613
Joerg Roedele3e59872009-09-03 14:02:10 +0200614static void dump_dte_entry(u16 devid)
615{
616 int i;
617
Joerg Roedelee6c2862011-11-09 12:06:03 +0100618 for (i = 0; i < 4; ++i)
619 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200620 amd_iommu_dev_table[devid].data[i]);
621}
622
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200623static void dump_command(unsigned long phys_addr)
624{
625 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
626 int i;
627
628 for (i = 0; i < 4; ++i)
629 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
630}
631
Joerg Roedela345b232009-09-03 15:01:43 +0200632static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200633{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200634 int type, devid, domid, flags;
635 volatile u32 *event = __evt;
636 int count = 0;
637 u64 address;
638
639retry:
640 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
641 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
642 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
643 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
644 address = (u64)(((u64)event[3]) << 32) | event[2];
645
646 if (type == 0) {
647 /* Did we hit the erratum? */
648 if (++count == LOOP_TIMEOUT) {
649 pr_err("AMD-Vi: No event written to event log\n");
650 return;
651 }
652 udelay(1);
653 goto retry;
654 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200655
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200656 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200657
658 switch (type) {
659 case EVENT_TYPE_ILL_DEV:
660 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
661 "address=0x%016llx flags=0x%04x]\n",
662 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
663 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200664 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200665 break;
666 case EVENT_TYPE_IO_FAULT:
667 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
668 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
669 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
670 domid, address, flags);
671 break;
672 case EVENT_TYPE_DEV_TAB_ERR:
673 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
674 "address=0x%016llx flags=0x%04x]\n",
675 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
676 address, flags);
677 break;
678 case EVENT_TYPE_PAGE_TAB_ERR:
679 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
680 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
681 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
682 domid, address, flags);
683 break;
684 case EVENT_TYPE_ILL_CMD:
685 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200686 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200687 break;
688 case EVENT_TYPE_CMD_HARD_ERR:
689 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
690 "flags=0x%04x]\n", address, flags);
691 break;
692 case EVENT_TYPE_IOTLB_INV_TO:
693 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
694 "address=0x%016llx]\n",
695 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
696 address);
697 break;
698 case EVENT_TYPE_INV_DEV_REQ:
699 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
700 "address=0x%016llx flags=0x%04x]\n",
701 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
702 address, flags);
703 break;
704 default:
705 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
706 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200707
708 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200709}
710
711static void iommu_poll_events(struct amd_iommu *iommu)
712{
713 u32 head, tail;
714 unsigned long flags;
715
716 spin_lock_irqsave(&iommu->lock, flags);
717
718 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
719 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
720
721 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200722 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200723 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
724 }
725
726 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
727
728 spin_unlock_irqrestore(&iommu->lock, flags);
729}
730
Joerg Roedeleee53532012-06-01 15:20:23 +0200731static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100732{
733 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100734
Joerg Roedel399be2f2011-12-01 16:53:47 +0100735 INC_STATS_COUNTER(pri_requests);
736
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100737 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
738 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
739 return;
740 }
741
742 fault.address = raw[1];
743 fault.pasid = PPR_PASID(raw[0]);
744 fault.device_id = PPR_DEVID(raw[0]);
745 fault.tag = PPR_TAG(raw[0]);
746 fault.flags = PPR_FLAGS(raw[0]);
747
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100748 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
749}
750
751static void iommu_poll_ppr_log(struct amd_iommu *iommu)
752{
753 unsigned long flags;
754 u32 head, tail;
755
756 if (iommu->ppr_log == NULL)
757 return;
758
Joerg Roedeleee53532012-06-01 15:20:23 +0200759 /* enable ppr interrupts again */
760 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
761
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100762 spin_lock_irqsave(&iommu->lock, flags);
763
764 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
765 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
766
767 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200768 volatile u64 *raw;
769 u64 entry[2];
770 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100771
Joerg Roedeleee53532012-06-01 15:20:23 +0200772 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100773
Joerg Roedeleee53532012-06-01 15:20:23 +0200774 /*
775 * Hardware bug: Interrupt may arrive before the entry is
776 * written to memory. If this happens we need to wait for the
777 * entry to arrive.
778 */
779 for (i = 0; i < LOOP_TIMEOUT; ++i) {
780 if (PPR_REQ_TYPE(raw[0]) != 0)
781 break;
782 udelay(1);
783 }
784
785 /* Avoid memcpy function-call overhead */
786 entry[0] = raw[0];
787 entry[1] = raw[1];
788
789 /*
790 * To detect the hardware bug we need to clear the entry
791 * back to zero.
792 */
793 raw[0] = raw[1] = 0UL;
794
795 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100796 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
797 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200798
799 /*
800 * Release iommu->lock because ppr-handling might need to
Frank Arnolddf805ab2012-08-27 19:21:04 +0200801 * re-acquire it
Joerg Roedeleee53532012-06-01 15:20:23 +0200802 */
803 spin_unlock_irqrestore(&iommu->lock, flags);
804
805 /* Handle PPR entry */
806 iommu_handle_ppr_entry(iommu, entry);
807
808 spin_lock_irqsave(&iommu->lock, flags);
809
810 /* Refresh ring-buffer information */
811 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100812 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
813 }
814
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100815 spin_unlock_irqrestore(&iommu->lock, flags);
816}
817
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200818irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200819{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200820 struct amd_iommu *iommu;
821
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100822 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200823 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100824 iommu_poll_ppr_log(iommu);
825 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200826
827 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200828}
829
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200830irqreturn_t amd_iommu_int_handler(int irq, void *data)
831{
832 return IRQ_WAKE_THREAD;
833}
834
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200835/****************************************************************************
836 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200837 * IOMMU command queuing functions
838 *
839 ****************************************************************************/
840
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200841static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200842{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200843 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200844
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200845 while (*sem == 0 && i < LOOP_TIMEOUT) {
846 udelay(1);
847 i += 1;
848 }
849
850 if (i == LOOP_TIMEOUT) {
851 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
852 return -EIO;
853 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200854
855 return 0;
856}
857
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200858static void copy_cmd_to_buffer(struct amd_iommu *iommu,
859 struct iommu_cmd *cmd,
860 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200861{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200862 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200863
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200864 target = iommu->cmd_buf + tail;
865 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200866
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200867 /* Copy command to buffer */
868 memcpy(target, cmd, sizeof(*cmd));
869
870 /* Tell the IOMMU about it */
871 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
872}
873
Joerg Roedel815b33f2011-04-06 17:26:49 +0200874static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200875{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200876 WARN_ON(address & 0x7ULL);
877
Joerg Roedelded46732011-04-06 10:53:48 +0200878 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200879 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
880 cmd->data[1] = upper_32_bits(__pa(address));
881 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200882 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
883}
884
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200885static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
886{
887 memset(cmd, 0, sizeof(*cmd));
888 cmd->data[0] = devid;
889 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
890}
891
Joerg Roedel11b64022011-04-06 11:49:28 +0200892static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
893 size_t size, u16 domid, int pde)
894{
895 u64 pages;
896 int s;
897
898 pages = iommu_num_pages(address, size, PAGE_SIZE);
899 s = 0;
900
901 if (pages > 1) {
902 /*
903 * If we have to flush more than one page, flush all
904 * TLB entries for this domain
905 */
906 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
907 s = 1;
908 }
909
910 address &= PAGE_MASK;
911
912 memset(cmd, 0, sizeof(*cmd));
913 cmd->data[1] |= domid;
914 cmd->data[2] = lower_32_bits(address);
915 cmd->data[3] = upper_32_bits(address);
916 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
917 if (s) /* size bit - we flush more than one 4kb page */
918 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200919 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200920 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
921}
922
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200923static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
924 u64 address, size_t size)
925{
926 u64 pages;
927 int s;
928
929 pages = iommu_num_pages(address, size, PAGE_SIZE);
930 s = 0;
931
932 if (pages > 1) {
933 /*
934 * If we have to flush more than one page, flush all
935 * TLB entries for this domain
936 */
937 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
938 s = 1;
939 }
940
941 address &= PAGE_MASK;
942
943 memset(cmd, 0, sizeof(*cmd));
944 cmd->data[0] = devid;
945 cmd->data[0] |= (qdep & 0xff) << 24;
946 cmd->data[1] = devid;
947 cmd->data[2] = lower_32_bits(address);
948 cmd->data[3] = upper_32_bits(address);
949 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
950 if (s)
951 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
952}
953
Joerg Roedel22e266c2011-11-21 15:59:08 +0100954static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
955 u64 address, bool size)
956{
957 memset(cmd, 0, sizeof(*cmd));
958
959 address &= ~(0xfffULL);
960
961 cmd->data[0] = pasid & PASID_MASK;
962 cmd->data[1] = domid;
963 cmd->data[2] = lower_32_bits(address);
964 cmd->data[3] = upper_32_bits(address);
965 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
966 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
967 if (size)
968 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
969 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
970}
971
972static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
973 int qdep, u64 address, bool size)
974{
975 memset(cmd, 0, sizeof(*cmd));
976
977 address &= ~(0xfffULL);
978
979 cmd->data[0] = devid;
980 cmd->data[0] |= (pasid & 0xff) << 16;
981 cmd->data[0] |= (qdep & 0xff) << 24;
982 cmd->data[1] = devid;
983 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
984 cmd->data[2] = lower_32_bits(address);
985 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
986 cmd->data[3] = upper_32_bits(address);
987 if (size)
988 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
989 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
990}
991
Joerg Roedelc99afa22011-11-21 18:19:25 +0100992static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
993 int status, int tag, bool gn)
994{
995 memset(cmd, 0, sizeof(*cmd));
996
997 cmd->data[0] = devid;
998 if (gn) {
999 cmd->data[1] = pasid & PASID_MASK;
1000 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1001 }
1002 cmd->data[3] = tag & 0x1ff;
1003 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1004
1005 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1006}
1007
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001008static void build_inv_all(struct iommu_cmd *cmd)
1009{
1010 memset(cmd, 0, sizeof(*cmd));
1011 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001012}
1013
Joerg Roedel7ef27982012-06-21 16:46:04 +02001014static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1015{
1016 memset(cmd, 0, sizeof(*cmd));
1017 cmd->data[0] = devid;
1018 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1019}
1020
Joerg Roedel431b2a22008-07-11 17:14:22 +02001021/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001022 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001023 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001024 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001025static int iommu_queue_command_sync(struct amd_iommu *iommu,
1026 struct iommu_cmd *cmd,
1027 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001028{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001029 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +02001030 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001031
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001032 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +01001033
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001034again:
Joerg Roedel815b33f2011-04-06 17:26:49 +02001035 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001036
1037 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1038 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1039 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
1040 left = (head - next_tail) % iommu->cmd_buf_size;
1041
1042 if (left <= 2) {
1043 struct iommu_cmd sync_cmd;
1044 volatile u64 sem = 0;
1045 int ret;
1046
1047 build_completion_wait(&sync_cmd, (u64)&sem);
1048 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1049
1050 spin_unlock_irqrestore(&iommu->lock, flags);
1051
1052 if ((ret = wait_on_sem(&sem)) != 0)
1053 return ret;
1054
1055 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001056 }
1057
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001058 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001059
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001060 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001061 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001062
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001063 spin_unlock_irqrestore(&iommu->lock, flags);
1064
Joerg Roedel815b33f2011-04-06 17:26:49 +02001065 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001066}
1067
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001068static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1069{
1070 return iommu_queue_command_sync(iommu, cmd, true);
1071}
1072
Joerg Roedel8d201962008-12-02 20:34:41 +01001073/*
1074 * This function queues a completion wait command into the command
1075 * buffer of an IOMMU
1076 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001077static int iommu_completion_wait(struct amd_iommu *iommu)
1078{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001079 struct iommu_cmd cmd;
1080 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001081 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001082
1083 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001084 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001085
Joerg Roedel815b33f2011-04-06 17:26:49 +02001086 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001087
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001088 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001089 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001090 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001091
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001092 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001093}
1094
Joerg Roedeld8c13082011-04-06 18:51:26 +02001095static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001096{
1097 struct iommu_cmd cmd;
1098
Joerg Roedeld8c13082011-04-06 18:51:26 +02001099 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001100
Joerg Roedeld8c13082011-04-06 18:51:26 +02001101 return iommu_queue_command(iommu, &cmd);
1102}
1103
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001104static void iommu_flush_dte_all(struct amd_iommu *iommu)
1105{
1106 u32 devid;
1107
1108 for (devid = 0; devid <= 0xffff; ++devid)
1109 iommu_flush_dte(iommu, devid);
1110
1111 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001112}
1113
1114/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001115 * This function uses heavy locking and may disable irqs for some time. But
1116 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001117 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001118static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001119{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001120 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001121
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001122 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1123 struct iommu_cmd cmd;
1124 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1125 dom_id, 1);
1126 iommu_queue_command(iommu, &cmd);
1127 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001128
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001129 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001130}
1131
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001132static void iommu_flush_all(struct amd_iommu *iommu)
1133{
1134 struct iommu_cmd cmd;
1135
1136 build_inv_all(&cmd);
1137
1138 iommu_queue_command(iommu, &cmd);
1139 iommu_completion_wait(iommu);
1140}
1141
Joerg Roedel7ef27982012-06-21 16:46:04 +02001142static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1143{
1144 struct iommu_cmd cmd;
1145
1146 build_inv_irt(&cmd, devid);
1147
1148 iommu_queue_command(iommu, &cmd);
1149}
1150
1151static void iommu_flush_irt_all(struct amd_iommu *iommu)
1152{
1153 u32 devid;
1154
1155 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1156 iommu_flush_irt(iommu, devid);
1157
1158 iommu_completion_wait(iommu);
1159}
1160
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001161void iommu_flush_all_caches(struct amd_iommu *iommu)
1162{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001163 if (iommu_feature(iommu, FEATURE_IA)) {
1164 iommu_flush_all(iommu);
1165 } else {
1166 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001167 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001168 iommu_flush_tlb_all(iommu);
1169 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001170}
1171
Joerg Roedel431b2a22008-07-11 17:14:22 +02001172/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001173 * Command send function for flushing on-device TLB
1174 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001175static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1176 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001177{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001178 struct amd_iommu *iommu;
1179 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001180 int qdep;
1181
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001182 qdep = dev_data->ats.qdep;
1183 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001184
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001185 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001186
1187 return iommu_queue_command(iommu, &cmd);
1188}
1189
1190/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001191 * Command send function for invalidating a device table entry
1192 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001193static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001194{
1195 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001196 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001197
Joerg Roedel6c542042011-06-09 17:07:31 +02001198 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001199
Joerg Roedelf62dda62011-06-09 12:55:35 +02001200 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001201 if (ret)
1202 return ret;
1203
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001204 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001205 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001206
1207 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001208}
1209
Joerg Roedel431b2a22008-07-11 17:14:22 +02001210/*
1211 * TLB invalidation function which is called from the mapping functions.
1212 * It invalidates a single PTE if the range to flush is within a single
1213 * page. Otherwise it flushes the whole TLB of the IOMMU.
1214 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001215static void __domain_flush_pages(struct protection_domain *domain,
1216 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001217{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001218 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001219 struct iommu_cmd cmd;
1220 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001221
Joerg Roedel11b64022011-04-06 11:49:28 +02001222 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001223
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001224 for (i = 0; i < amd_iommus_present; ++i) {
1225 if (!domain->dev_iommu[i])
1226 continue;
1227
1228 /*
1229 * Devices of this domain are behind this IOMMU
1230 * We need a TLB flush
1231 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001232 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001233 }
1234
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001235 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001236
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001237 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001238 continue;
1239
Joerg Roedel6c542042011-06-09 17:07:31 +02001240 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001241 }
1242
Joerg Roedel11b64022011-04-06 11:49:28 +02001243 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001244}
1245
Joerg Roedel17b124b2011-04-06 18:01:35 +02001246static void domain_flush_pages(struct protection_domain *domain,
1247 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001248{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001249 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001250}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001251
Joerg Roedel1c655772008-09-04 18:40:05 +02001252/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001253static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001254{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001255 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001256}
1257
Chris Wright42a49f92009-06-15 15:42:00 +02001258/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001259static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001260{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001261 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1262}
1263
1264static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001265{
1266 int i;
1267
1268 for (i = 0; i < amd_iommus_present; ++i) {
1269 if (!domain->dev_iommu[i])
1270 continue;
1271
1272 /*
1273 * Devices of this domain are behind this IOMMU
1274 * We need to wait for completion of all commands.
1275 */
1276 iommu_completion_wait(amd_iommus[i]);
1277 }
1278}
1279
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001280
Joerg Roedel43f49602008-12-02 21:01:12 +01001281/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001282 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001283 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001284static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001285{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001286 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001287
1288 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001289 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001290}
1291
Joerg Roedel431b2a22008-07-11 17:14:22 +02001292/****************************************************************************
1293 *
1294 * The functions below are used the create the page table mappings for
1295 * unity mapped regions.
1296 *
1297 ****************************************************************************/
1298
1299/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001300 * This function is used to add another level to an IO page table. Adding
1301 * another level increases the size of the address space by 9 bits to a size up
1302 * to 64 bits.
1303 */
1304static bool increase_address_space(struct protection_domain *domain,
1305 gfp_t gfp)
1306{
1307 u64 *pte;
1308
1309 if (domain->mode == PAGE_MODE_6_LEVEL)
1310 /* address space already 64 bit large */
1311 return false;
1312
1313 pte = (void *)get_zeroed_page(gfp);
1314 if (!pte)
1315 return false;
1316
1317 *pte = PM_LEVEL_PDE(domain->mode,
1318 virt_to_phys(domain->pt_root));
1319 domain->pt_root = pte;
1320 domain->mode += 1;
1321 domain->updated = true;
1322
1323 return true;
1324}
1325
1326static u64 *alloc_pte(struct protection_domain *domain,
1327 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001328 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001329 u64 **pte_page,
1330 gfp_t gfp)
1331{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001332 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001333 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001334
1335 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001336
1337 while (address > PM_LEVEL_SIZE(domain->mode))
1338 increase_address_space(domain, gfp);
1339
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001340 level = domain->mode - 1;
1341 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1342 address = PAGE_SIZE_ALIGN(address, page_size);
1343 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001344
1345 while (level > end_lvl) {
1346 if (!IOMMU_PTE_PRESENT(*pte)) {
1347 page = (u64 *)get_zeroed_page(gfp);
1348 if (!page)
1349 return NULL;
1350 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1351 }
1352
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001353 /* No level skipping support yet */
1354 if (PM_PTE_LEVEL(*pte) != level)
1355 return NULL;
1356
Joerg Roedel308973d2009-11-24 17:43:32 +01001357 level -= 1;
1358
1359 pte = IOMMU_PTE_PAGE(*pte);
1360
1361 if (pte_page && level == end_lvl)
1362 *pte_page = pte;
1363
1364 pte = &pte[PM_LEVEL_INDEX(level, address)];
1365 }
1366
1367 return pte;
1368}
1369
1370/*
1371 * This function checks if there is a PTE for a given dma address. If
1372 * there is one, it returns the pointer to it.
1373 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001374static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001375{
1376 int level;
1377 u64 *pte;
1378
Joerg Roedel24cd7722010-01-19 17:27:39 +01001379 if (address > PM_LEVEL_SIZE(domain->mode))
1380 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001381
Joerg Roedel24cd7722010-01-19 17:27:39 +01001382 level = domain->mode - 1;
1383 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1384
1385 while (level > 0) {
1386
1387 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001388 if (!IOMMU_PTE_PRESENT(*pte))
1389 return NULL;
1390
Joerg Roedel24cd7722010-01-19 17:27:39 +01001391 /* Large PTE */
1392 if (PM_PTE_LEVEL(*pte) == 0x07) {
1393 unsigned long pte_mask, __pte;
1394
1395 /*
1396 * If we have a series of large PTEs, make
1397 * sure to return a pointer to the first one.
1398 */
1399 pte_mask = PTE_PAGE_SIZE(*pte);
1400 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1401 __pte = ((unsigned long)pte) & pte_mask;
1402
1403 return (u64 *)__pte;
1404 }
1405
1406 /* No level skipping support yet */
1407 if (PM_PTE_LEVEL(*pte) != level)
1408 return NULL;
1409
Joerg Roedel308973d2009-11-24 17:43:32 +01001410 level -= 1;
1411
Joerg Roedel24cd7722010-01-19 17:27:39 +01001412 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001413 pte = IOMMU_PTE_PAGE(*pte);
1414 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001415 }
1416
1417 return pte;
1418}
1419
1420/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001421 * Generic mapping functions. It maps a physical address into a DMA
1422 * address space. It allocates the page table pages if necessary.
1423 * In the future it can be extended to a generic mapping function
1424 * supporting all features of AMD IOMMU page tables like level skipping
1425 * and full 64 bit address spaces.
1426 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001427static int iommu_map_page(struct protection_domain *dom,
1428 unsigned long bus_addr,
1429 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001430 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001431 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001432{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001433 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001434 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001435
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001436 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001437 return -EINVAL;
1438
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001439 bus_addr = PAGE_ALIGN(bus_addr);
1440 phys_addr = PAGE_ALIGN(phys_addr);
1441 count = PAGE_SIZE_PTE_COUNT(page_size);
1442 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001443
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001444 for (i = 0; i < count; ++i)
1445 if (IOMMU_PTE_PRESENT(pte[i]))
1446 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001447
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001448 if (page_size > PAGE_SIZE) {
1449 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1450 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1451 } else
1452 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1453
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001454 if (prot & IOMMU_PROT_IR)
1455 __pte |= IOMMU_PTE_IR;
1456 if (prot & IOMMU_PROT_IW)
1457 __pte |= IOMMU_PTE_IW;
1458
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001459 for (i = 0; i < count; ++i)
1460 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001461
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001462 update_domain(dom);
1463
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001464 return 0;
1465}
1466
Joerg Roedel24cd7722010-01-19 17:27:39 +01001467static unsigned long iommu_unmap_page(struct protection_domain *dom,
1468 unsigned long bus_addr,
1469 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001470{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001471 unsigned long long unmap_size, unmapped;
1472 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001473
Joerg Roedel24cd7722010-01-19 17:27:39 +01001474 BUG_ON(!is_power_of_2(page_size));
1475
1476 unmapped = 0;
1477
1478 while (unmapped < page_size) {
1479
1480 pte = fetch_pte(dom, bus_addr);
1481
1482 if (!pte) {
1483 /*
1484 * No PTE for this address
1485 * move forward in 4kb steps
1486 */
1487 unmap_size = PAGE_SIZE;
1488 } else if (PM_PTE_LEVEL(*pte) == 0) {
1489 /* 4kb PTE found for this address */
1490 unmap_size = PAGE_SIZE;
1491 *pte = 0ULL;
1492 } else {
1493 int count, i;
1494
1495 /* Large PTE found which maps this address */
1496 unmap_size = PTE_PAGE_SIZE(*pte);
1497 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1498 for (i = 0; i < count; i++)
1499 pte[i] = 0ULL;
1500 }
1501
1502 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1503 unmapped += unmap_size;
1504 }
1505
1506 BUG_ON(!is_power_of_2(unmapped));
1507
1508 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001509}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001510
Joerg Roedel431b2a22008-07-11 17:14:22 +02001511/*
1512 * This function checks if a specific unity mapping entry is needed for
1513 * this specific IOMMU.
1514 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001515static int iommu_for_unity_map(struct amd_iommu *iommu,
1516 struct unity_map_entry *entry)
1517{
1518 u16 bdf, i;
1519
1520 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1521 bdf = amd_iommu_alias_table[i];
1522 if (amd_iommu_rlookup_table[bdf] == iommu)
1523 return 1;
1524 }
1525
1526 return 0;
1527}
1528
Joerg Roedel431b2a22008-07-11 17:14:22 +02001529/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001530 * This function actually applies the mapping to the page table of the
1531 * dma_ops domain.
1532 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001533static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1534 struct unity_map_entry *e)
1535{
1536 u64 addr;
1537 int ret;
1538
1539 for (addr = e->address_start; addr < e->address_end;
1540 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001541 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001542 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001543 if (ret)
1544 return ret;
1545 /*
1546 * if unity mapping is in aperture range mark the page
1547 * as allocated in the aperture
1548 */
1549 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001550 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001551 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001552 }
1553
1554 return 0;
1555}
1556
Joerg Roedel431b2a22008-07-11 17:14:22 +02001557/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001558 * Init the unity mappings for a specific IOMMU in the system
1559 *
1560 * Basically iterates over all unity mapping entries and applies them to
1561 * the default domain DMA of that IOMMU if necessary.
1562 */
1563static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1564{
1565 struct unity_map_entry *entry;
1566 int ret;
1567
1568 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1569 if (!iommu_for_unity_map(iommu, entry))
1570 continue;
1571 ret = dma_ops_unity_map(iommu->default_dom, entry);
1572 if (ret)
1573 return ret;
1574 }
1575
1576 return 0;
1577}
1578
1579/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001580 * Inits the unity mappings required for a specific device
1581 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001582static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1583 u16 devid)
1584{
1585 struct unity_map_entry *e;
1586 int ret;
1587
1588 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1589 if (!(devid >= e->devid_start && devid <= e->devid_end))
1590 continue;
1591 ret = dma_ops_unity_map(dma_dom, e);
1592 if (ret)
1593 return ret;
1594 }
1595
1596 return 0;
1597}
1598
Joerg Roedel431b2a22008-07-11 17:14:22 +02001599/****************************************************************************
1600 *
1601 * The next functions belong to the address allocator for the dma_ops
1602 * interface functions. They work like the allocators in the other IOMMU
1603 * drivers. Its basically a bitmap which marks the allocated pages in
1604 * the aperture. Maybe it could be enhanced in the future to a more
1605 * efficient allocator.
1606 *
1607 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001608
Joerg Roedel431b2a22008-07-11 17:14:22 +02001609/*
Joerg Roedel384de722009-05-15 12:30:05 +02001610 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001611 *
1612 * called with domain->lock held
1613 */
Joerg Roedel384de722009-05-15 12:30:05 +02001614
Joerg Roedel9cabe892009-05-18 16:38:55 +02001615/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001616 * Used to reserve address ranges in the aperture (e.g. for exclusion
1617 * ranges.
1618 */
1619static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1620 unsigned long start_page,
1621 unsigned int pages)
1622{
1623 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1624
1625 if (start_page + pages > last_page)
1626 pages = last_page - start_page;
1627
1628 for (i = start_page; i < start_page + pages; ++i) {
1629 int index = i / APERTURE_RANGE_PAGES;
1630 int page = i % APERTURE_RANGE_PAGES;
1631 __set_bit(page, dom->aperture[index]->bitmap);
1632 }
1633}
1634
1635/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001636 * This function is used to add a new aperture range to an existing
1637 * aperture in case of dma_ops domain allocation or address allocation
1638 * failure.
1639 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001640static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001641 bool populate, gfp_t gfp)
1642{
1643 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001644 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001645 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001646
Joerg Roedelf5e97052009-05-22 12:31:53 +02001647#ifdef CONFIG_IOMMU_STRESS
1648 populate = false;
1649#endif
1650
Joerg Roedel9cabe892009-05-18 16:38:55 +02001651 if (index >= APERTURE_MAX_RANGES)
1652 return -ENOMEM;
1653
1654 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1655 if (!dma_dom->aperture[index])
1656 return -ENOMEM;
1657
1658 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1659 if (!dma_dom->aperture[index]->bitmap)
1660 goto out_free;
1661
1662 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1663
1664 if (populate) {
1665 unsigned long address = dma_dom->aperture_size;
1666 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1667 u64 *pte, *pte_page;
1668
1669 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001670 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001671 &pte_page, gfp);
1672 if (!pte)
1673 goto out_free;
1674
1675 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1676
1677 address += APERTURE_RANGE_SIZE / 64;
1678 }
1679 }
1680
Joerg Roedel17f5b562011-07-06 17:14:44 +02001681 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001682 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1683
Joerg Roedel17f5b562011-07-06 17:14:44 +02001684 /* Reserve address range used for MSI messages */
1685 if (old_size < MSI_ADDR_BASE_LO &&
1686 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1687 unsigned long spage;
1688 int pages;
1689
1690 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1691 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1692
1693 dma_ops_reserve_addresses(dma_dom, spage, pages);
1694 }
1695
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001696 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001697 for_each_iommu(iommu) {
1698 if (iommu->exclusion_start &&
1699 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1700 && iommu->exclusion_start < dma_dom->aperture_size) {
1701 unsigned long startpage;
1702 int pages = iommu_num_pages(iommu->exclusion_start,
1703 iommu->exclusion_length,
1704 PAGE_SIZE);
1705 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1706 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1707 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001708 }
1709
1710 /*
1711 * Check for areas already mapped as present in the new aperture
1712 * range and mark those pages as reserved in the allocator. Such
1713 * mappings may already exist as a result of requested unity
1714 * mappings for devices.
1715 */
1716 for (i = dma_dom->aperture[index]->offset;
1717 i < dma_dom->aperture_size;
1718 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001719 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001720 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1721 continue;
1722
Joerg Roedelfcd08612011-10-11 17:41:32 +02001723 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001724 }
1725
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001726 update_domain(&dma_dom->domain);
1727
Joerg Roedel9cabe892009-05-18 16:38:55 +02001728 return 0;
1729
1730out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001731 update_domain(&dma_dom->domain);
1732
Joerg Roedel9cabe892009-05-18 16:38:55 +02001733 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1734
1735 kfree(dma_dom->aperture[index]);
1736 dma_dom->aperture[index] = NULL;
1737
1738 return -ENOMEM;
1739}
1740
Joerg Roedel384de722009-05-15 12:30:05 +02001741static unsigned long dma_ops_area_alloc(struct device *dev,
1742 struct dma_ops_domain *dom,
1743 unsigned int pages,
1744 unsigned long align_mask,
1745 u64 dma_mask,
1746 unsigned long start)
1747{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001748 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001749 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1750 int i = start >> APERTURE_RANGE_SHIFT;
1751 unsigned long boundary_size;
1752 unsigned long address = -1;
1753 unsigned long limit;
1754
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001755 next_bit >>= PAGE_SHIFT;
1756
Joerg Roedel384de722009-05-15 12:30:05 +02001757 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1758 PAGE_SIZE) >> PAGE_SHIFT;
1759
1760 for (;i < max_index; ++i) {
1761 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1762
1763 if (dom->aperture[i]->offset >= dma_mask)
1764 break;
1765
1766 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1767 dma_mask >> PAGE_SHIFT);
1768
1769 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1770 limit, next_bit, pages, 0,
1771 boundary_size, align_mask);
1772 if (address != -1) {
1773 address = dom->aperture[i]->offset +
1774 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001775 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001776 break;
1777 }
1778
1779 next_bit = 0;
1780 }
1781
1782 return address;
1783}
1784
Joerg Roedeld3086442008-06-26 21:27:57 +02001785static unsigned long dma_ops_alloc_addresses(struct device *dev,
1786 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001787 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001788 unsigned long align_mask,
1789 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001790{
Joerg Roedeld3086442008-06-26 21:27:57 +02001791 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001792
Joerg Roedelfe16f082009-05-22 12:27:53 +02001793#ifdef CONFIG_IOMMU_STRESS
1794 dom->next_address = 0;
1795 dom->need_flush = true;
1796#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001797
Joerg Roedel384de722009-05-15 12:30:05 +02001798 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001799 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001800
Joerg Roedel1c655772008-09-04 18:40:05 +02001801 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001802 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001803 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1804 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001805 dom->need_flush = true;
1806 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001807
Joerg Roedel384de722009-05-15 12:30:05 +02001808 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001809 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001810
1811 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1812
1813 return address;
1814}
1815
Joerg Roedel431b2a22008-07-11 17:14:22 +02001816/*
1817 * The address free function.
1818 *
1819 * called with domain->lock held
1820 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001821static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1822 unsigned long address,
1823 unsigned int pages)
1824{
Joerg Roedel384de722009-05-15 12:30:05 +02001825 unsigned i = address >> APERTURE_RANGE_SHIFT;
1826 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001827
Joerg Roedel384de722009-05-15 12:30:05 +02001828 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1829
Joerg Roedel47bccd62009-05-22 12:40:54 +02001830#ifdef CONFIG_IOMMU_STRESS
1831 if (i < 4)
1832 return;
1833#endif
1834
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001835 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001836 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001837
1838 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001839
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001840 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001841
Joerg Roedeld3086442008-06-26 21:27:57 +02001842}
1843
Joerg Roedel431b2a22008-07-11 17:14:22 +02001844/****************************************************************************
1845 *
1846 * The next functions belong to the domain allocation. A domain is
1847 * allocated for every IOMMU as the default domain. If device isolation
1848 * is enabled, every device get its own domain. The most important thing
1849 * about domains is the page table mapping the DMA address space they
1850 * contain.
1851 *
1852 ****************************************************************************/
1853
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001854/*
1855 * This function adds a protection domain to the global protection domain list
1856 */
1857static void add_domain_to_list(struct protection_domain *domain)
1858{
1859 unsigned long flags;
1860
1861 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1862 list_add(&domain->list, &amd_iommu_pd_list);
1863 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1864}
1865
1866/*
1867 * This function removes a protection domain to the global
1868 * protection domain list
1869 */
1870static void del_domain_from_list(struct protection_domain *domain)
1871{
1872 unsigned long flags;
1873
1874 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1875 list_del(&domain->list);
1876 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1877}
1878
Joerg Roedelec487d12008-06-26 21:27:58 +02001879static u16 domain_id_alloc(void)
1880{
1881 unsigned long flags;
1882 int id;
1883
1884 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1885 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1886 BUG_ON(id == 0);
1887 if (id > 0 && id < MAX_DOMAIN_ID)
1888 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1889 else
1890 id = 0;
1891 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1892
1893 return id;
1894}
1895
Joerg Roedela2acfb72008-12-02 18:28:53 +01001896static void domain_id_free(int id)
1897{
1898 unsigned long flags;
1899
1900 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1901 if (id > 0 && id < MAX_DOMAIN_ID)
1902 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1903 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1904}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001905
Joerg Roedel86db2e52008-12-02 18:20:21 +01001906static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001907{
1908 int i, j;
1909 u64 *p1, *p2, *p3;
1910
Joerg Roedel86db2e52008-12-02 18:20:21 +01001911 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001912
1913 if (!p1)
1914 return;
1915
1916 for (i = 0; i < 512; ++i) {
1917 if (!IOMMU_PTE_PRESENT(p1[i]))
1918 continue;
1919
1920 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001921 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001922 if (!IOMMU_PTE_PRESENT(p2[j]))
1923 continue;
1924 p3 = IOMMU_PTE_PAGE(p2[j]);
1925 free_page((unsigned long)p3);
1926 }
1927
1928 free_page((unsigned long)p2);
1929 }
1930
1931 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001932
1933 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001934}
1935
Joerg Roedelb16137b2011-11-21 16:50:23 +01001936static void free_gcr3_tbl_level1(u64 *tbl)
1937{
1938 u64 *ptr;
1939 int i;
1940
1941 for (i = 0; i < 512; ++i) {
1942 if (!(tbl[i] & GCR3_VALID))
1943 continue;
1944
1945 ptr = __va(tbl[i] & PAGE_MASK);
1946
1947 free_page((unsigned long)ptr);
1948 }
1949}
1950
1951static void free_gcr3_tbl_level2(u64 *tbl)
1952{
1953 u64 *ptr;
1954 int i;
1955
1956 for (i = 0; i < 512; ++i) {
1957 if (!(tbl[i] & GCR3_VALID))
1958 continue;
1959
1960 ptr = __va(tbl[i] & PAGE_MASK);
1961
1962 free_gcr3_tbl_level1(ptr);
1963 }
1964}
1965
Joerg Roedel52815b72011-11-17 17:24:28 +01001966static void free_gcr3_table(struct protection_domain *domain)
1967{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001968 if (domain->glx == 2)
1969 free_gcr3_tbl_level2(domain->gcr3_tbl);
1970 else if (domain->glx == 1)
1971 free_gcr3_tbl_level1(domain->gcr3_tbl);
1972 else if (domain->glx != 0)
1973 BUG();
1974
Joerg Roedel52815b72011-11-17 17:24:28 +01001975 free_page((unsigned long)domain->gcr3_tbl);
1976}
1977
Joerg Roedel431b2a22008-07-11 17:14:22 +02001978/*
1979 * Free a domain, only used if something went wrong in the
1980 * allocation path and we need to free an already allocated page table
1981 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001982static void dma_ops_domain_free(struct dma_ops_domain *dom)
1983{
Joerg Roedel384de722009-05-15 12:30:05 +02001984 int i;
1985
Joerg Roedelec487d12008-06-26 21:27:58 +02001986 if (!dom)
1987 return;
1988
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001989 del_domain_from_list(&dom->domain);
1990
Joerg Roedel86db2e52008-12-02 18:20:21 +01001991 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001992
Joerg Roedel384de722009-05-15 12:30:05 +02001993 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1994 if (!dom->aperture[i])
1995 continue;
1996 free_page((unsigned long)dom->aperture[i]->bitmap);
1997 kfree(dom->aperture[i]);
1998 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001999
2000 kfree(dom);
2001}
2002
Joerg Roedel431b2a22008-07-11 17:14:22 +02002003/*
2004 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002005 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02002006 * structures required for the dma_ops interface
2007 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01002008static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02002009{
2010 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002011
2012 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
2013 if (!dma_dom)
2014 return NULL;
2015
2016 spin_lock_init(&dma_dom->domain.lock);
2017
2018 dma_dom->domain.id = domain_id_alloc();
2019 if (dma_dom->domain.id == 0)
2020 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002021 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002022 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002023 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002024 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002025 dma_dom->domain.priv = dma_dom;
2026 if (!dma_dom->domain.pt_root)
2027 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002028
Joerg Roedel1c655772008-09-04 18:40:05 +02002029 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002030 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002031
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002032 add_domain_to_list(&dma_dom->domain);
2033
Joerg Roedel576175c2009-11-23 19:08:46 +01002034 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002035 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002036
Joerg Roedel431b2a22008-07-11 17:14:22 +02002037 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002038 * mark the first page as allocated so we never return 0 as
2039 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002040 */
Joerg Roedel384de722009-05-15 12:30:05 +02002041 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02002042 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002043
Joerg Roedelec487d12008-06-26 21:27:58 +02002044
2045 return dma_dom;
2046
2047free_dma_dom:
2048 dma_ops_domain_free(dma_dom);
2049
2050 return NULL;
2051}
2052
Joerg Roedel431b2a22008-07-11 17:14:22 +02002053/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002054 * little helper function to check whether a given protection domain is a
2055 * dma_ops domain
2056 */
2057static bool dma_ops_domain(struct protection_domain *domain)
2058{
2059 return domain->flags & PD_DMA_OPS_MASK;
2060}
2061
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002062static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002063{
Joerg Roedel132bd682011-11-17 14:18:46 +01002064 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002065 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002066
Joerg Roedel132bd682011-11-17 14:18:46 +01002067 if (domain->mode != PAGE_MODE_NONE)
2068 pte_root = virt_to_phys(domain->pt_root);
2069
Joerg Roedel38ddf412008-09-11 10:38:32 +02002070 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2071 << DEV_ENTRY_MODE_SHIFT;
2072 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002073
Joerg Roedelee6c2862011-11-09 12:06:03 +01002074 flags = amd_iommu_dev_table[devid].data[1];
2075
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002076 if (ats)
2077 flags |= DTE_FLAG_IOTLB;
2078
Joerg Roedel52815b72011-11-17 17:24:28 +01002079 if (domain->flags & PD_IOMMUV2_MASK) {
2080 u64 gcr3 = __pa(domain->gcr3_tbl);
2081 u64 glx = domain->glx;
2082 u64 tmp;
2083
2084 pte_root |= DTE_FLAG_GV;
2085 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2086
2087 /* First mask out possible old values for GCR3 table */
2088 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2089 flags &= ~tmp;
2090
2091 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2092 flags &= ~tmp;
2093
2094 /* Encode GCR3 table into DTE */
2095 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2096 pte_root |= tmp;
2097
2098 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2099 flags |= tmp;
2100
2101 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2102 flags |= tmp;
2103 }
2104
Joerg Roedelee6c2862011-11-09 12:06:03 +01002105 flags &= ~(0xffffUL);
2106 flags |= domain->id;
2107
2108 amd_iommu_dev_table[devid].data[1] = flags;
2109 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002110}
2111
Joerg Roedel15898bb2009-11-24 15:39:42 +01002112static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002113{
Joerg Roedel355bf552008-12-08 12:02:41 +01002114 /* remove entry from the device table seen by the hardware */
2115 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2116 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002117
Joerg Roedelc5cca142009-10-09 18:31:20 +02002118 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002119}
2120
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002121static void do_attach(struct iommu_dev_data *dev_data,
2122 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002123{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002124 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002125 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002126
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002127 iommu = amd_iommu_rlookup_table[dev_data->devid];
2128 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002129
2130 /* Update data structures */
2131 dev_data->domain = domain;
2132 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002133 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002134
2135 /* Do reference counting */
2136 domain->dev_iommu[iommu->index] += 1;
2137 domain->dev_cnt += 1;
2138
2139 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002140 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002141}
2142
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002143static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002144{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002145 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002146
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002147 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002148
Joerg Roedelc4596112009-11-20 14:57:32 +01002149 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002150 dev_data->domain->dev_iommu[iommu->index] -= 1;
2151 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002152
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002153 /* Update data structures */
2154 dev_data->domain = NULL;
2155 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002156 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002157
2158 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002159 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002160}
2161
2162/*
2163 * If a device is not yet associated with a domain, this function does
2164 * assigns it visible for the hardware
2165 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002166static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002167 struct protection_domain *domain)
2168{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002169 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002170
Joerg Roedel15898bb2009-11-24 15:39:42 +01002171 /* lock domain */
2172 spin_lock(&domain->lock);
2173
Joerg Roedel71f77582011-06-09 19:03:15 +02002174 if (dev_data->alias_data != NULL) {
2175 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002176
Joerg Roedel2b02b092011-06-09 17:48:39 +02002177 /* Some sanity checks */
2178 ret = -EBUSY;
2179 if (alias_data->domain != NULL &&
2180 alias_data->domain != domain)
2181 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002182
Joerg Roedel2b02b092011-06-09 17:48:39 +02002183 if (dev_data->domain != NULL &&
2184 dev_data->domain != domain)
2185 goto out_unlock;
2186
2187 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002188 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002189 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002190
2191 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002192 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002193
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002194 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002195 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002196
Joerg Roedel24100052009-11-25 15:59:57 +01002197 atomic_inc(&dev_data->bind);
2198
Julia Lawall84fe6c12010-05-27 12:31:51 +02002199 ret = 0;
2200
2201out_unlock:
2202
Joerg Roedel355bf552008-12-08 12:02:41 +01002203 /* ready */
2204 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002205
Julia Lawall84fe6c12010-05-27 12:31:51 +02002206 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002207}
2208
Joerg Roedel52815b72011-11-17 17:24:28 +01002209
2210static void pdev_iommuv2_disable(struct pci_dev *pdev)
2211{
2212 pci_disable_ats(pdev);
2213 pci_disable_pri(pdev);
2214 pci_disable_pasid(pdev);
2215}
2216
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002217/* FIXME: Change generic reset-function to do the same */
2218static int pri_reset_while_enabled(struct pci_dev *pdev)
2219{
2220 u16 control;
2221 int pos;
2222
Joerg Roedel46277b72011-12-07 14:34:02 +01002223 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002224 if (!pos)
2225 return -EINVAL;
2226
Joerg Roedel46277b72011-12-07 14:34:02 +01002227 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2228 control |= PCI_PRI_CTRL_RESET;
2229 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002230
2231 return 0;
2232}
2233
Joerg Roedel52815b72011-11-17 17:24:28 +01002234static int pdev_iommuv2_enable(struct pci_dev *pdev)
2235{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002236 bool reset_enable;
2237 int reqs, ret;
2238
2239 /* FIXME: Hardcode number of outstanding requests for now */
2240 reqs = 32;
2241 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2242 reqs = 1;
2243 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002244
2245 /* Only allow access to user-accessible pages */
2246 ret = pci_enable_pasid(pdev, 0);
2247 if (ret)
2248 goto out_err;
2249
2250 /* First reset the PRI state of the device */
2251 ret = pci_reset_pri(pdev);
2252 if (ret)
2253 goto out_err;
2254
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002255 /* Enable PRI */
2256 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002257 if (ret)
2258 goto out_err;
2259
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002260 if (reset_enable) {
2261 ret = pri_reset_while_enabled(pdev);
2262 if (ret)
2263 goto out_err;
2264 }
2265
Joerg Roedel52815b72011-11-17 17:24:28 +01002266 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2267 if (ret)
2268 goto out_err;
2269
2270 return 0;
2271
2272out_err:
2273 pci_disable_pri(pdev);
2274 pci_disable_pasid(pdev);
2275
2276 return ret;
2277}
2278
Joerg Roedelc99afa22011-11-21 18:19:25 +01002279/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002280#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002281
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002282static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002283{
Joerg Roedela3b93122012-04-12 12:49:26 +02002284 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002285 int pos;
2286
Joerg Roedel46277b72011-12-07 14:34:02 +01002287 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002288 if (!pos)
2289 return false;
2290
Joerg Roedela3b93122012-04-12 12:49:26 +02002291 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002292
Joerg Roedela3b93122012-04-12 12:49:26 +02002293 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002294}
2295
Joerg Roedel15898bb2009-11-24 15:39:42 +01002296/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002297 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002298 * assigns it visible for the hardware
2299 */
2300static int attach_device(struct device *dev,
2301 struct protection_domain *domain)
2302{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002303 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002304 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002305 unsigned long flags;
2306 int ret;
2307
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002308 dev_data = get_dev_data(dev);
2309
Joerg Roedel52815b72011-11-17 17:24:28 +01002310 if (domain->flags & PD_IOMMUV2_MASK) {
2311 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2312 return -EINVAL;
2313
2314 if (pdev_iommuv2_enable(pdev) != 0)
2315 return -EINVAL;
2316
2317 dev_data->ats.enabled = true;
2318 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002319 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002320 } else if (amd_iommu_iotlb_sup &&
2321 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002322 dev_data->ats.enabled = true;
2323 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2324 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002325
Joerg Roedel15898bb2009-11-24 15:39:42 +01002326 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002327 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002328 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2329
2330 /*
2331 * We might boot into a crash-kernel here. The crashed kernel
2332 * left the caches in the IOMMU dirty. So we have to flush
2333 * here to evict all dirty stuff.
2334 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002335 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002336
2337 return ret;
2338}
2339
2340/*
2341 * Removes a device from a protection domain (unlocked)
2342 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002343static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002344{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002345 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002346 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002347
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002348 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002349
Joerg Roedel2ca76272010-01-22 16:45:31 +01002350 domain = dev_data->domain;
2351
2352 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002353
Joerg Roedel71f77582011-06-09 19:03:15 +02002354 if (dev_data->alias_data != NULL) {
2355 struct iommu_dev_data *alias_data = dev_data->alias_data;
2356
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002357 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002358 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002359 }
2360
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002361 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002362 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002363
Joerg Roedel2ca76272010-01-22 16:45:31 +01002364 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002365
Joerg Roedel21129f72009-09-01 11:59:42 +02002366 /*
2367 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002368 * passthrough domain if it is detached from any other domain.
2369 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002370 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002371 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002372 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002373 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002374}
2375
2376/*
2377 * Removes a device from a protection domain (with devtable_lock held)
2378 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002379static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002380{
Joerg Roedel52815b72011-11-17 17:24:28 +01002381 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002382 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002383 unsigned long flags;
2384
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002385 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002386 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002387
Joerg Roedel355bf552008-12-08 12:02:41 +01002388 /* lock device table */
2389 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002390 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002391 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002392
Joerg Roedel52815b72011-11-17 17:24:28 +01002393 if (domain->flags & PD_IOMMUV2_MASK)
2394 pdev_iommuv2_disable(to_pci_dev(dev));
2395 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002396 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002397
2398 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002399}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002400
Joerg Roedel15898bb2009-11-24 15:39:42 +01002401/*
2402 * Find out the protection domain structure for a given PCI device. This
2403 * will give us the pointer to the page table root for example.
2404 */
2405static struct protection_domain *domain_for_device(struct device *dev)
2406{
Joerg Roedel71f77582011-06-09 19:03:15 +02002407 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002408 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002409 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002410
Joerg Roedel657cbb62009-11-23 15:26:46 +01002411 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002412
Joerg Roedel2b02b092011-06-09 17:48:39 +02002413 if (dev_data->domain)
2414 return dev_data->domain;
2415
Joerg Roedel71f77582011-06-09 19:03:15 +02002416 if (dev_data->alias_data != NULL) {
2417 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002418
2419 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2420 if (alias_data->domain != NULL) {
2421 __attach_device(dev_data, alias_data->domain);
2422 dom = alias_data->domain;
2423 }
2424 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002425 }
2426
Joerg Roedel15898bb2009-11-24 15:39:42 +01002427 return dom;
2428}
2429
Joerg Roedele275a2a2008-12-10 18:27:25 +01002430static int device_change_notifier(struct notifier_block *nb,
2431 unsigned long action, void *data)
2432{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002433 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002434 struct protection_domain *domain;
2435 struct iommu_dev_data *dev_data;
2436 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002437 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002438 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002439 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002440
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002441 if (!check_device(dev))
2442 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002443
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002444 devid = get_device_id(dev);
2445 iommu = amd_iommu_rlookup_table[devid];
2446 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002447
2448 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002449 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002450
2451 domain = domain_for_device(dev);
2452
Joerg Roedele275a2a2008-12-10 18:27:25 +01002453 if (!domain)
2454 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002455 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002456 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002457 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002458 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002459 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002460
2461 iommu_init_device(dev);
2462
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002463 /*
2464 * dev_data is still NULL and
2465 * got initialized in iommu_init_device
2466 */
2467 dev_data = get_dev_data(dev);
2468
2469 if (iommu_pass_through || dev_data->iommu_v2) {
2470 dev_data->passthrough = true;
2471 attach_device(dev, pt_domain);
2472 break;
2473 }
2474
Joerg Roedel657cbb62009-11-23 15:26:46 +01002475 domain = domain_for_device(dev);
2476
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002477 /* allocate a protection domain if a device is added */
2478 dma_domain = find_protection_domain(devid);
2479 if (dma_domain)
2480 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002481 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002482 if (!dma_domain)
2483 goto out;
2484 dma_domain->target_dev = devid;
2485
2486 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2487 list_add_tail(&dma_domain->list, &iommu_pd_list);
2488 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2489
Joerg Roedelac1534a2012-06-21 14:52:40 +02002490 dev_data = get_dev_data(dev);
2491
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002492 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002493
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002494 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002495 case BUS_NOTIFY_DEL_DEVICE:
2496
2497 iommu_uninit_device(dev);
2498
Joerg Roedele275a2a2008-12-10 18:27:25 +01002499 default:
2500 goto out;
2501 }
2502
Joerg Roedele275a2a2008-12-10 18:27:25 +01002503 iommu_completion_wait(iommu);
2504
2505out:
2506 return 0;
2507}
2508
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302509static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002510 .notifier_call = device_change_notifier,
2511};
Joerg Roedel355bf552008-12-08 12:02:41 +01002512
Joerg Roedel8638c492009-12-10 11:12:25 +01002513void amd_iommu_init_notifier(void)
2514{
2515 bus_register_notifier(&pci_bus_type, &device_nb);
2516}
2517
Joerg Roedel431b2a22008-07-11 17:14:22 +02002518/*****************************************************************************
2519 *
2520 * The next functions belong to the dma_ops mapping/unmapping code.
2521 *
2522 *****************************************************************************/
2523
2524/*
2525 * In the dma_ops path we only have the struct device. This function
2526 * finds the corresponding IOMMU, the protection domain and the
2527 * requestor id for a given device.
2528 * If the device is not yet associated with a domain this is also done
2529 * in this function.
2530 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002531static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002532{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002533 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002534 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002535 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002536
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002537 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002538 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002539
Joerg Roedel94f6d192009-11-24 16:40:02 +01002540 domain = domain_for_device(dev);
2541 if (domain != NULL && !dma_ops_domain(domain))
2542 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002543
Joerg Roedel94f6d192009-11-24 16:40:02 +01002544 if (domain != NULL)
2545 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002546
Frank Arnolddf805ab2012-08-27 19:21:04 +02002547 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002548 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002549 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002550 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2551 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002552 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002553 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002554
Joerg Roedel94f6d192009-11-24 16:40:02 +01002555 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002556}
2557
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002558static void update_device_table(struct protection_domain *domain)
2559{
Joerg Roedel492667d2009-11-27 13:25:47 +01002560 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002561
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002562 list_for_each_entry(dev_data, &domain->dev_list, list)
2563 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002564}
2565
2566static void update_domain(struct protection_domain *domain)
2567{
2568 if (!domain->updated)
2569 return;
2570
2571 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002572
2573 domain_flush_devices(domain);
2574 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002575
2576 domain->updated = false;
2577}
2578
Joerg Roedel431b2a22008-07-11 17:14:22 +02002579/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002580 * This function fetches the PTE for a given address in the aperture
2581 */
2582static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2583 unsigned long address)
2584{
Joerg Roedel384de722009-05-15 12:30:05 +02002585 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002586 u64 *pte, *pte_page;
2587
Joerg Roedel384de722009-05-15 12:30:05 +02002588 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2589 if (!aperture)
2590 return NULL;
2591
2592 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002593 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002594 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002595 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002596 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2597 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002598 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002599
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002600 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002601
2602 return pte;
2603}
2604
2605/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002606 * This is the generic map function. It maps one 4kb page at paddr to
2607 * the given address in the DMA address space for the domain.
2608 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002609static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002610 unsigned long address,
2611 phys_addr_t paddr,
2612 int direction)
2613{
2614 u64 *pte, __pte;
2615
2616 WARN_ON(address > dom->aperture_size);
2617
2618 paddr &= PAGE_MASK;
2619
Joerg Roedel8bda3092009-05-12 12:02:46 +02002620 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002621 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002622 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002623
2624 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2625
2626 if (direction == DMA_TO_DEVICE)
2627 __pte |= IOMMU_PTE_IR;
2628 else if (direction == DMA_FROM_DEVICE)
2629 __pte |= IOMMU_PTE_IW;
2630 else if (direction == DMA_BIDIRECTIONAL)
2631 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2632
2633 WARN_ON(*pte);
2634
2635 *pte = __pte;
2636
2637 return (dma_addr_t)address;
2638}
2639
Joerg Roedel431b2a22008-07-11 17:14:22 +02002640/*
2641 * The generic unmapping function for on page in the DMA address space.
2642 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002643static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002644 unsigned long address)
2645{
Joerg Roedel384de722009-05-15 12:30:05 +02002646 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002647 u64 *pte;
2648
2649 if (address >= dom->aperture_size)
2650 return;
2651
Joerg Roedel384de722009-05-15 12:30:05 +02002652 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2653 if (!aperture)
2654 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002655
Joerg Roedel384de722009-05-15 12:30:05 +02002656 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2657 if (!pte)
2658 return;
2659
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002660 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002661
2662 WARN_ON(!*pte);
2663
2664 *pte = 0ULL;
2665}
2666
Joerg Roedel431b2a22008-07-11 17:14:22 +02002667/*
2668 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002669 * contiguous memory region into DMA address space. It is used by all
2670 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002671 * Must be called with the domain lock held.
2672 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002673static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002674 struct dma_ops_domain *dma_dom,
2675 phys_addr_t paddr,
2676 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002677 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002678 bool align,
2679 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002680{
2681 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002682 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002683 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002684 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002685 int i;
2686
Joerg Roedele3c449f2008-10-15 22:02:11 -07002687 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002688 paddr &= PAGE_MASK;
2689
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002690 INC_STATS_COUNTER(total_map_requests);
2691
Joerg Roedelc1858972008-12-12 15:42:39 +01002692 if (pages > 1)
2693 INC_STATS_COUNTER(cross_page);
2694
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002695 if (align)
2696 align_mask = (1UL << get_order(size)) - 1;
2697
Joerg Roedel11b83882009-05-19 10:23:15 +02002698retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002699 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2700 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002701 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002702 /*
2703 * setting next_address here will let the address
2704 * allocator only scan the new allocated range in the
2705 * first run. This is a small optimization.
2706 */
2707 dma_dom->next_address = dma_dom->aperture_size;
2708
Joerg Roedel576175c2009-11-23 19:08:46 +01002709 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002710 goto out;
2711
2712 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002713 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002714 * allocation again
2715 */
2716 goto retry;
2717 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002718
2719 start = address;
2720 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002721 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002722 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002723 goto out_unmap;
2724
Joerg Roedelcb76c322008-06-26 21:28:00 +02002725 paddr += PAGE_SIZE;
2726 start += PAGE_SIZE;
2727 }
2728 address += offset;
2729
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002730 ADD_STATS_COUNTER(alloced_io_mem, size);
2731
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002732 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002733 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002734 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002735 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002736 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002737
Joerg Roedelcb76c322008-06-26 21:28:00 +02002738out:
2739 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002740
2741out_unmap:
2742
2743 for (--i; i >= 0; --i) {
2744 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002745 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002746 }
2747
2748 dma_ops_free_addresses(dma_dom, address, pages);
2749
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002750 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002751}
2752
Joerg Roedel431b2a22008-07-11 17:14:22 +02002753/*
2754 * Does the reverse of the __map_single function. Must be called with
2755 * the domain lock held too
2756 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002757static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002758 dma_addr_t dma_addr,
2759 size_t size,
2760 int dir)
2761{
Joerg Roedel04e04632010-09-23 16:12:48 +02002762 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002763 dma_addr_t i, start;
2764 unsigned int pages;
2765
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002766 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002767 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002768 return;
2769
Joerg Roedel04e04632010-09-23 16:12:48 +02002770 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002771 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002772 dma_addr &= PAGE_MASK;
2773 start = dma_addr;
2774
2775 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002776 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002777 start += PAGE_SIZE;
2778 }
2779
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002780 SUB_STATS_COUNTER(alloced_io_mem, size);
2781
Joerg Roedelcb76c322008-06-26 21:28:00 +02002782 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002783
Joerg Roedel80be3082008-11-06 14:59:05 +01002784 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002785 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002786 dma_dom->need_flush = false;
2787 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002788}
2789
Joerg Roedel431b2a22008-07-11 17:14:22 +02002790/*
2791 * The exported map_single function for dma_ops.
2792 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002793static dma_addr_t map_page(struct device *dev, struct page *page,
2794 unsigned long offset, size_t size,
2795 enum dma_data_direction dir,
2796 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002797{
2798 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002799 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002800 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002801 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002802 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002803
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002804 INC_STATS_COUNTER(cnt_map_single);
2805
Joerg Roedel94f6d192009-11-24 16:40:02 +01002806 domain = get_domain(dev);
2807 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002808 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002809 else if (IS_ERR(domain))
2810 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002811
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002812 dma_mask = *dev->dma_mask;
2813
Joerg Roedel4da70b92008-06-26 21:28:01 +02002814 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002815
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002816 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002817 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002818 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002819 goto out;
2820
Joerg Roedel17b124b2011-04-06 18:01:35 +02002821 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002822
2823out:
2824 spin_unlock_irqrestore(&domain->lock, flags);
2825
2826 return addr;
2827}
2828
Joerg Roedel431b2a22008-07-11 17:14:22 +02002829/*
2830 * The exported unmap_single function for dma_ops.
2831 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002832static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2833 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002834{
2835 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002836 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002837
Joerg Roedel146a6912008-12-12 15:07:12 +01002838 INC_STATS_COUNTER(cnt_unmap_single);
2839
Joerg Roedel94f6d192009-11-24 16:40:02 +01002840 domain = get_domain(dev);
2841 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002842 return;
2843
Joerg Roedel4da70b92008-06-26 21:28:01 +02002844 spin_lock_irqsave(&domain->lock, flags);
2845
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002846 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002847
Joerg Roedel17b124b2011-04-06 18:01:35 +02002848 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002849
2850 spin_unlock_irqrestore(&domain->lock, flags);
2851}
2852
Joerg Roedel431b2a22008-07-11 17:14:22 +02002853/*
2854 * This is a special map_sg function which is used if we should map a
2855 * device which is not handled by an AMD IOMMU in the system.
2856 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002857static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2858 int nelems, int dir)
2859{
2860 struct scatterlist *s;
2861 int i;
2862
2863 for_each_sg(sglist, s, nelems, i) {
2864 s->dma_address = (dma_addr_t)sg_phys(s);
2865 s->dma_length = s->length;
2866 }
2867
2868 return nelems;
2869}
2870
Joerg Roedel431b2a22008-07-11 17:14:22 +02002871/*
2872 * The exported map_sg function for dma_ops (handles scatter-gather
2873 * lists).
2874 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002875static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002876 int nelems, enum dma_data_direction dir,
2877 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002878{
2879 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002880 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002881 int i;
2882 struct scatterlist *s;
2883 phys_addr_t paddr;
2884 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002885 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002886
Joerg Roedeld03f0672008-12-12 15:09:48 +01002887 INC_STATS_COUNTER(cnt_map_sg);
2888
Joerg Roedel94f6d192009-11-24 16:40:02 +01002889 domain = get_domain(dev);
2890 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002891 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002892 else if (IS_ERR(domain))
2893 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002894
Joerg Roedel832a90c2008-09-18 15:54:23 +02002895 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002896
Joerg Roedel65b050a2008-06-26 21:28:02 +02002897 spin_lock_irqsave(&domain->lock, flags);
2898
2899 for_each_sg(sglist, s, nelems, i) {
2900 paddr = sg_phys(s);
2901
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002902 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002903 paddr, s->length, dir, false,
2904 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002905
2906 if (s->dma_address) {
2907 s->dma_length = s->length;
2908 mapped_elems++;
2909 } else
2910 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002911 }
2912
Joerg Roedel17b124b2011-04-06 18:01:35 +02002913 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002914
2915out:
2916 spin_unlock_irqrestore(&domain->lock, flags);
2917
2918 return mapped_elems;
2919unmap:
2920 for_each_sg(sglist, s, mapped_elems, i) {
2921 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002922 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002923 s->dma_length, dir);
2924 s->dma_address = s->dma_length = 0;
2925 }
2926
2927 mapped_elems = 0;
2928
2929 goto out;
2930}
2931
Joerg Roedel431b2a22008-07-11 17:14:22 +02002932/*
2933 * The exported map_sg function for dma_ops (handles scatter-gather
2934 * lists).
2935 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002936static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002937 int nelems, enum dma_data_direction dir,
2938 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002939{
2940 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002941 struct protection_domain *domain;
2942 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002943 int i;
2944
Joerg Roedel55877a62008-12-12 15:12:14 +01002945 INC_STATS_COUNTER(cnt_unmap_sg);
2946
Joerg Roedel94f6d192009-11-24 16:40:02 +01002947 domain = get_domain(dev);
2948 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002949 return;
2950
Joerg Roedel65b050a2008-06-26 21:28:02 +02002951 spin_lock_irqsave(&domain->lock, flags);
2952
2953 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002954 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002955 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002956 s->dma_address = s->dma_length = 0;
2957 }
2958
Joerg Roedel17b124b2011-04-06 18:01:35 +02002959 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002960
2961 spin_unlock_irqrestore(&domain->lock, flags);
2962}
2963
Joerg Roedel431b2a22008-07-11 17:14:22 +02002964/*
2965 * The exported alloc_coherent function for dma_ops.
2966 */
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02002967static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002968 dma_addr_t *dma_addr, gfp_t flag,
2969 struct dma_attrs *attrs)
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02002970{
2971 unsigned long flags;
2972 void *virt_addr;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02002973 struct protection_domain *domain;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02002974 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002975 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02002976
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002977 INC_STATS_COUNTER(cnt_alloc_coherent);
2978
Joerg Roedel94f6d192009-11-24 16:40:02 +01002979 domain = get_domain(dev);
2980 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002981 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2982 *dma_addr = __pa(virt_addr);
2983 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002984 } else if (IS_ERR(domain))
2985 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002986
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002987 dma_mask = dev->coherent_dma_mask;
2988 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2989 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002990
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02002991 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2992 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302993 return NULL;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02002994
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02002995 paddr = virt_to_phys(virt_addr);
2996
Joerg Roedel832a90c2008-09-18 15:54:23 +02002997 if (!dma_mask)
2998 dma_mask = *dev->dma_mask;
2999
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003000 spin_lock_irqsave(&domain->lock, flags);
3001
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003002 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02003003 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003004
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09003005 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02003006 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01003007 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02003008 }
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003009
Joerg Roedel17b124b2011-04-06 18:01:35 +02003010 domain_flush_complete(domain);
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003011
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003012 spin_unlock_irqrestore(&domain->lock, flags);
3013
3014 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01003015
3016out_free:
3017
3018 free_pages((unsigned long)virt_addr, get_order(size));
3019
3020 return NULL;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003021}
3022
Joerg Roedel431b2a22008-07-11 17:14:22 +02003023/*
3024 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003025 */
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003026static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003027 void *virt_addr, dma_addr_t dma_addr,
3028 struct dma_attrs *attrs)
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003029{
3030 unsigned long flags;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003031 struct protection_domain *domain;
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003032
Joerg Roedel5d31ee72008-12-12 15:16:38 +01003033 INC_STATS_COUNTER(cnt_free_coherent);
3034
Joerg Roedel94f6d192009-11-24 16:40:02 +01003035 domain = get_domain(dev);
3036 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01003037 goto free_mem;
3038
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003039 spin_lock_irqsave(&domain->lock, flags);
3040
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003041 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003042
Joerg Roedel17b124b2011-04-06 18:01:35 +02003043 domain_flush_complete(domain);
Joerg Roedel5d8b53cf32008-06-26 21:28:03 +02003044
3045 spin_unlock_irqrestore(&domain->lock, flags);
3046
3047free_mem:
3048 free_pages((unsigned long)virt_addr, get_order(size));
3049}
3050
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003051/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003052 * This function is called by the DMA layer to find out if we can handle a
3053 * particular device. It is part of the dma_ops.
3054 */
3055static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3056{
Joerg Roedel420aef82009-11-23 16:14:57 +01003057 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003058}
3059
3060/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003061 * The function for pre-allocating protection domains.
3062 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003063 * If the driver core informs the DMA layer if a driver grabs a device
3064 * we don't need to preallocate the protection domains anymore.
3065 * For now we have to.
3066 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003067static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003068{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003069 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003070 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003071 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003072 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003073
Chris Wrightd18c69d2010-04-02 18:27:55 -07003074 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003075
3076 /* Do we handle this device? */
3077 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003078 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003079
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003080 dev_data = get_dev_data(&dev->dev);
3081 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3082 /* Make sure passthrough domain is allocated */
3083 alloc_passthrough_domain();
3084 dev_data->passthrough = true;
3085 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003086 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003087 dev_name(&dev->dev));
3088 }
3089
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003090 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003091 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003092 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003093
3094 devid = get_device_id(&dev->dev);
3095
Joerg Roedel87a64d52009-11-24 17:26:43 +01003096 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003097 if (!dma_dom)
3098 continue;
3099 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003100 dma_dom->target_dev = devid;
3101
Joerg Roedel15898bb2009-11-24 15:39:42 +01003102 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003103
Joerg Roedelbd60b732008-09-11 10:24:48 +02003104 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003105 }
3106}
3107
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003108static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003109 .alloc = alloc_coherent,
3110 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003111 .map_page = map_page,
3112 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003113 .map_sg = map_sg,
3114 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003115 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003116};
3117
Joerg Roedel27c21272011-05-30 15:56:24 +02003118static unsigned device_dma_ops_init(void)
3119{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003120 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003121 struct pci_dev *pdev = NULL;
3122 unsigned unhandled = 0;
3123
3124 for_each_pci_dev(pdev) {
3125 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003126
3127 iommu_ignore_device(&pdev->dev);
3128
Joerg Roedel27c21272011-05-30 15:56:24 +02003129 unhandled += 1;
3130 continue;
3131 }
3132
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003133 dev_data = get_dev_data(&pdev->dev);
3134
3135 if (!dev_data->passthrough)
3136 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3137 else
3138 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003139 }
3140
3141 return unhandled;
3142}
3143
Joerg Roedel431b2a22008-07-11 17:14:22 +02003144/*
3145 * The function which clues the AMD IOMMU driver into dma_ops.
3146 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003147
3148void __init amd_iommu_init_api(void)
3149{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003150 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003151}
3152
Joerg Roedel6631ee92008-06-26 21:28:05 +02003153int __init amd_iommu_init_dma_ops(void)
3154{
3155 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003156 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003157
Joerg Roedel431b2a22008-07-11 17:14:22 +02003158 /*
3159 * first allocate a default protection domain for every IOMMU we
3160 * found in the system. Devices not assigned to any other
3161 * protection domain will be assigned to the default one.
3162 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003163 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003164 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003165 if (iommu->default_dom == NULL)
3166 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003167 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003168 ret = iommu_init_unity_mappings(iommu);
3169 if (ret)
3170 goto free_domains;
3171 }
3172
Joerg Roedel431b2a22008-07-11 17:14:22 +02003173 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003174 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003175 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003176 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003177
3178 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003179 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003180
Joerg Roedel431b2a22008-07-11 17:14:22 +02003181 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003182 unhandled = device_dma_ops_init();
3183 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3184 /* There are unhandled devices - initialize swiotlb for them */
3185 swiotlb = 1;
3186 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003187
Joerg Roedel7f265082008-12-12 13:50:21 +01003188 amd_iommu_stats_init();
3189
Joerg Roedel62410ee2012-06-12 16:42:43 +02003190 if (amd_iommu_unmap_flush)
3191 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3192 else
3193 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3194
Joerg Roedel6631ee92008-06-26 21:28:05 +02003195 return 0;
3196
3197free_domains:
3198
Joerg Roedel3bd22172009-05-04 15:06:20 +02003199 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003200 if (iommu->default_dom)
3201 dma_ops_domain_free(iommu->default_dom);
3202 }
3203
3204 return ret;
3205}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003206
3207/*****************************************************************************
3208 *
3209 * The following functions belong to the exported interface of AMD IOMMU
3210 *
3211 * This interface allows access to lower level functions of the IOMMU
3212 * like protection domain handling and assignement of devices to domains
3213 * which is not possible with the dma_ops interface.
3214 *
3215 *****************************************************************************/
3216
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003217static void cleanup_domain(struct protection_domain *domain)
3218{
Joerg Roedel492667d2009-11-27 13:25:47 +01003219 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003220 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003221
3222 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3223
Joerg Roedel492667d2009-11-27 13:25:47 +01003224 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003225 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003226 atomic_set(&dev_data->bind, 0);
3227 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003228
3229 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3230}
3231
Joerg Roedel26508152009-08-26 16:52:40 +02003232static void protection_domain_free(struct protection_domain *domain)
3233{
3234 if (!domain)
3235 return;
3236
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003237 del_domain_from_list(domain);
3238
Joerg Roedel26508152009-08-26 16:52:40 +02003239 if (domain->id)
3240 domain_id_free(domain->id);
3241
3242 kfree(domain);
3243}
3244
3245static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003246{
3247 struct protection_domain *domain;
3248
3249 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3250 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003251 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003252
3253 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003254 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003255 domain->id = domain_id_alloc();
3256 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003257 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003258 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003259
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003260 add_domain_to_list(domain);
3261
Joerg Roedel26508152009-08-26 16:52:40 +02003262 return domain;
3263
3264out_err:
3265 kfree(domain);
3266
3267 return NULL;
3268}
3269
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003270static int __init alloc_passthrough_domain(void)
3271{
3272 if (pt_domain != NULL)
3273 return 0;
3274
3275 /* allocate passthrough domain */
3276 pt_domain = protection_domain_alloc();
3277 if (!pt_domain)
3278 return -ENOMEM;
3279
3280 pt_domain->mode = PAGE_MODE_NONE;
3281
3282 return 0;
3283}
Joerg Roedel26508152009-08-26 16:52:40 +02003284static int amd_iommu_domain_init(struct iommu_domain *dom)
3285{
3286 struct protection_domain *domain;
3287
3288 domain = protection_domain_alloc();
3289 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003290 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003291
3292 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003293 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3294 if (!domain->pt_root)
3295 goto out_free;
3296
Joerg Roedelf3572db2011-11-23 12:36:25 +01003297 domain->iommu_domain = dom;
3298
Joerg Roedelc156e342008-12-02 18:13:27 +01003299 dom->priv = domain;
3300
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003301 dom->geometry.aperture_start = 0;
3302 dom->geometry.aperture_end = ~0ULL;
3303 dom->geometry.force_aperture = true;
3304
Joerg Roedelc156e342008-12-02 18:13:27 +01003305 return 0;
3306
3307out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003308 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003309
3310 return -ENOMEM;
3311}
3312
Joerg Roedel98383fc2008-12-02 18:34:12 +01003313static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3314{
3315 struct protection_domain *domain = dom->priv;
3316
3317 if (!domain)
3318 return;
3319
3320 if (domain->dev_cnt > 0)
3321 cleanup_domain(domain);
3322
3323 BUG_ON(domain->dev_cnt != 0);
3324
Joerg Roedel132bd682011-11-17 14:18:46 +01003325 if (domain->mode != PAGE_MODE_NONE)
3326 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003327
Joerg Roedel52815b72011-11-17 17:24:28 +01003328 if (domain->flags & PD_IOMMUV2_MASK)
3329 free_gcr3_table(domain);
3330
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003331 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003332
3333 dom->priv = NULL;
3334}
3335
Joerg Roedel684f2882008-12-08 12:07:44 +01003336static void amd_iommu_detach_device(struct iommu_domain *dom,
3337 struct device *dev)
3338{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003339 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003340 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003341 u16 devid;
3342
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003343 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003344 return;
3345
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003346 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003347
Joerg Roedel657cbb62009-11-23 15:26:46 +01003348 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003349 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003350
3351 iommu = amd_iommu_rlookup_table[devid];
3352 if (!iommu)
3353 return;
3354
Joerg Roedel684f2882008-12-08 12:07:44 +01003355 iommu_completion_wait(iommu);
3356}
3357
Joerg Roedel01106062008-12-02 19:34:11 +01003358static int amd_iommu_attach_device(struct iommu_domain *dom,
3359 struct device *dev)
3360{
3361 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003362 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003363 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003364 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003365
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003366 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003367 return -EINVAL;
3368
Joerg Roedel657cbb62009-11-23 15:26:46 +01003369 dev_data = dev->archdata.iommu;
3370
Joerg Roedelf62dda62011-06-09 12:55:35 +02003371 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003372 if (!iommu)
3373 return -EINVAL;
3374
Joerg Roedel657cbb62009-11-23 15:26:46 +01003375 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003376 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003377
Joerg Roedel15898bb2009-11-24 15:39:42 +01003378 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003379
3380 iommu_completion_wait(iommu);
3381
Joerg Roedel15898bb2009-11-24 15:39:42 +01003382 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003383}
3384
Joerg Roedel468e2362010-01-21 16:37:36 +01003385static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003386 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003387{
3388 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003389 int prot = 0;
3390 int ret;
3391
Joerg Roedel132bd682011-11-17 14:18:46 +01003392 if (domain->mode == PAGE_MODE_NONE)
3393 return -EINVAL;
3394
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003395 if (iommu_prot & IOMMU_READ)
3396 prot |= IOMMU_PROT_IR;
3397 if (iommu_prot & IOMMU_WRITE)
3398 prot |= IOMMU_PROT_IW;
3399
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003400 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003401 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003402 mutex_unlock(&domain->api_lock);
3403
Joerg Roedel795e74f2010-05-11 17:40:57 +02003404 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003405}
3406
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003407static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3408 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003409{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003410 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003411 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003412
Joerg Roedel132bd682011-11-17 14:18:46 +01003413 if (domain->mode == PAGE_MODE_NONE)
3414 return -EINVAL;
3415
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003416 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003417 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003418 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003419
Joerg Roedel17b124b2011-04-06 18:01:35 +02003420 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003421
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003422 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003423}
3424
Joerg Roedel645c4c82008-12-02 20:05:50 +01003425static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3426 unsigned long iova)
3427{
3428 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003429 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003430 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003431 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003432
Joerg Roedel132bd682011-11-17 14:18:46 +01003433 if (domain->mode == PAGE_MODE_NONE)
3434 return iova;
3435
Joerg Roedel24cd7722010-01-19 17:27:39 +01003436 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003437
Joerg Roedela6d41a42009-09-02 17:08:55 +02003438 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003439 return 0;
3440
Joerg Roedelf03152b2010-01-21 16:15:24 +01003441 if (PM_PTE_LEVEL(*pte) == 0)
3442 offset_mask = PAGE_SIZE - 1;
3443 else
3444 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3445
3446 __pte = *pte & PM_ADDR_MASK;
3447 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003448
3449 return paddr;
3450}
3451
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003452static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3453 unsigned long cap)
3454{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003455 switch (cap) {
3456 case IOMMU_CAP_CACHE_COHERENCY:
3457 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003458 case IOMMU_CAP_INTR_REMAP:
3459 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003460 }
3461
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003462 return 0;
3463}
3464
Joerg Roedel26961ef2008-12-03 17:00:17 +01003465static struct iommu_ops amd_iommu_ops = {
3466 .domain_init = amd_iommu_domain_init,
3467 .domain_destroy = amd_iommu_domain_destroy,
3468 .attach_dev = amd_iommu_attach_device,
3469 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003470 .map = amd_iommu_map,
3471 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003472 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003473 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003474 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003475};
3476
Joerg Roedel0feae532009-08-26 15:26:30 +02003477/*****************************************************************************
3478 *
3479 * The next functions do a basic initialization of IOMMU for pass through
3480 * mode
3481 *
3482 * In passthrough mode the IOMMU is initialized and enabled but not used for
3483 * DMA-API translation.
3484 *
3485 *****************************************************************************/
3486
3487int __init amd_iommu_init_passthrough(void)
3488{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003489 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003490 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003491 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003492 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003493 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003494
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003495 ret = alloc_passthrough_domain();
3496 if (ret)
3497 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003498
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003499 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003500 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003501 continue;
3502
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003503 dev_data = get_dev_data(&dev->dev);
3504 dev_data->passthrough = true;
3505
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003506 devid = get_device_id(&dev->dev);
3507
Joerg Roedel15898bb2009-11-24 15:39:42 +01003508 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003509 if (!iommu)
3510 continue;
3511
Joerg Roedel15898bb2009-11-24 15:39:42 +01003512 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003513 }
3514
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003515 amd_iommu_stats_init();
3516
Joerg Roedel0feae532009-08-26 15:26:30 +02003517 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3518
3519 return 0;
3520}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003521
3522/* IOMMUv2 specific functions */
3523int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3524{
3525 return atomic_notifier_chain_register(&ppr_notifier, nb);
3526}
3527EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3528
3529int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3530{
3531 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3532}
3533EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003534
3535void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3536{
3537 struct protection_domain *domain = dom->priv;
3538 unsigned long flags;
3539
3540 spin_lock_irqsave(&domain->lock, flags);
3541
3542 /* Update data structure */
3543 domain->mode = PAGE_MODE_NONE;
3544 domain->updated = true;
3545
3546 /* Make changes visible to IOMMUs */
3547 update_domain(domain);
3548
3549 /* Page-table is not visible to IOMMU anymore, so free it */
3550 free_pagetable(domain);
3551
3552 spin_unlock_irqrestore(&domain->lock, flags);
3553}
3554EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003555
3556int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3557{
3558 struct protection_domain *domain = dom->priv;
3559 unsigned long flags;
3560 int levels, ret;
3561
3562 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3563 return -EINVAL;
3564
3565 /* Number of GCR3 table levels required */
3566 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3567 levels += 1;
3568
3569 if (levels > amd_iommu_max_glx_val)
3570 return -EINVAL;
3571
3572 spin_lock_irqsave(&domain->lock, flags);
3573
3574 /*
3575 * Save us all sanity checks whether devices already in the
3576 * domain support IOMMUv2. Just force that the domain has no
3577 * devices attached when it is switched into IOMMUv2 mode.
3578 */
3579 ret = -EBUSY;
3580 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3581 goto out;
3582
3583 ret = -ENOMEM;
3584 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3585 if (domain->gcr3_tbl == NULL)
3586 goto out;
3587
3588 domain->glx = levels;
3589 domain->flags |= PD_IOMMUV2_MASK;
3590 domain->updated = true;
3591
3592 update_domain(domain);
3593
3594 ret = 0;
3595
3596out:
3597 spin_unlock_irqrestore(&domain->lock, flags);
3598
3599 return ret;
3600}
3601EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003602
3603static int __flush_pasid(struct protection_domain *domain, int pasid,
3604 u64 address, bool size)
3605{
3606 struct iommu_dev_data *dev_data;
3607 struct iommu_cmd cmd;
3608 int i, ret;
3609
3610 if (!(domain->flags & PD_IOMMUV2_MASK))
3611 return -EINVAL;
3612
3613 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3614
3615 /*
3616 * IOMMU TLB needs to be flushed before Device TLB to
3617 * prevent device TLB refill from IOMMU TLB
3618 */
3619 for (i = 0; i < amd_iommus_present; ++i) {
3620 if (domain->dev_iommu[i] == 0)
3621 continue;
3622
3623 ret = iommu_queue_command(amd_iommus[i], &cmd);
3624 if (ret != 0)
3625 goto out;
3626 }
3627
3628 /* Wait until IOMMU TLB flushes are complete */
3629 domain_flush_complete(domain);
3630
3631 /* Now flush device TLBs */
3632 list_for_each_entry(dev_data, &domain->dev_list, list) {
3633 struct amd_iommu *iommu;
3634 int qdep;
3635
3636 BUG_ON(!dev_data->ats.enabled);
3637
3638 qdep = dev_data->ats.qdep;
3639 iommu = amd_iommu_rlookup_table[dev_data->devid];
3640
3641 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3642 qdep, address, size);
3643
3644 ret = iommu_queue_command(iommu, &cmd);
3645 if (ret != 0)
3646 goto out;
3647 }
3648
3649 /* Wait until all device TLBs are flushed */
3650 domain_flush_complete(domain);
3651
3652 ret = 0;
3653
3654out:
3655
3656 return ret;
3657}
3658
3659static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3660 u64 address)
3661{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003662 INC_STATS_COUNTER(invalidate_iotlb);
3663
Joerg Roedel22e266c2011-11-21 15:59:08 +01003664 return __flush_pasid(domain, pasid, address, false);
3665}
3666
3667int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3668 u64 address)
3669{
3670 struct protection_domain *domain = dom->priv;
3671 unsigned long flags;
3672 int ret;
3673
3674 spin_lock_irqsave(&domain->lock, flags);
3675 ret = __amd_iommu_flush_page(domain, pasid, address);
3676 spin_unlock_irqrestore(&domain->lock, flags);
3677
3678 return ret;
3679}
3680EXPORT_SYMBOL(amd_iommu_flush_page);
3681
3682static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3683{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003684 INC_STATS_COUNTER(invalidate_iotlb_all);
3685
Joerg Roedel22e266c2011-11-21 15:59:08 +01003686 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3687 true);
3688}
3689
3690int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3691{
3692 struct protection_domain *domain = dom->priv;
3693 unsigned long flags;
3694 int ret;
3695
3696 spin_lock_irqsave(&domain->lock, flags);
3697 ret = __amd_iommu_flush_tlb(domain, pasid);
3698 spin_unlock_irqrestore(&domain->lock, flags);
3699
3700 return ret;
3701}
3702EXPORT_SYMBOL(amd_iommu_flush_tlb);
3703
Joerg Roedelb16137b2011-11-21 16:50:23 +01003704static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3705{
3706 int index;
3707 u64 *pte;
3708
3709 while (true) {
3710
3711 index = (pasid >> (9 * level)) & 0x1ff;
3712 pte = &root[index];
3713
3714 if (level == 0)
3715 break;
3716
3717 if (!(*pte & GCR3_VALID)) {
3718 if (!alloc)
3719 return NULL;
3720
3721 root = (void *)get_zeroed_page(GFP_ATOMIC);
3722 if (root == NULL)
3723 return NULL;
3724
3725 *pte = __pa(root) | GCR3_VALID;
3726 }
3727
3728 root = __va(*pte & PAGE_MASK);
3729
3730 level -= 1;
3731 }
3732
3733 return pte;
3734}
3735
3736static int __set_gcr3(struct protection_domain *domain, int pasid,
3737 unsigned long cr3)
3738{
3739 u64 *pte;
3740
3741 if (domain->mode != PAGE_MODE_NONE)
3742 return -EINVAL;
3743
3744 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3745 if (pte == NULL)
3746 return -ENOMEM;
3747
3748 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3749
3750 return __amd_iommu_flush_tlb(domain, pasid);
3751}
3752
3753static int __clear_gcr3(struct protection_domain *domain, int pasid)
3754{
3755 u64 *pte;
3756
3757 if (domain->mode != PAGE_MODE_NONE)
3758 return -EINVAL;
3759
3760 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3761 if (pte == NULL)
3762 return 0;
3763
3764 *pte = 0;
3765
3766 return __amd_iommu_flush_tlb(domain, pasid);
3767}
3768
3769int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3770 unsigned long cr3)
3771{
3772 struct protection_domain *domain = dom->priv;
3773 unsigned long flags;
3774 int ret;
3775
3776 spin_lock_irqsave(&domain->lock, flags);
3777 ret = __set_gcr3(domain, pasid, cr3);
3778 spin_unlock_irqrestore(&domain->lock, flags);
3779
3780 return ret;
3781}
3782EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3783
3784int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3785{
3786 struct protection_domain *domain = dom->priv;
3787 unsigned long flags;
3788 int ret;
3789
3790 spin_lock_irqsave(&domain->lock, flags);
3791 ret = __clear_gcr3(domain, pasid);
3792 spin_unlock_irqrestore(&domain->lock, flags);
3793
3794 return ret;
3795}
3796EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003797
3798int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3799 int status, int tag)
3800{
3801 struct iommu_dev_data *dev_data;
3802 struct amd_iommu *iommu;
3803 struct iommu_cmd cmd;
3804
Joerg Roedel399be2f2011-12-01 16:53:47 +01003805 INC_STATS_COUNTER(complete_ppr);
3806
Joerg Roedelc99afa22011-11-21 18:19:25 +01003807 dev_data = get_dev_data(&pdev->dev);
3808 iommu = amd_iommu_rlookup_table[dev_data->devid];
3809
3810 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3811 tag, dev_data->pri_tlp);
3812
3813 return iommu_queue_command(iommu, &cmd);
3814}
3815EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003816
3817struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3818{
3819 struct protection_domain *domain;
3820
3821 domain = get_domain(&pdev->dev);
3822 if (IS_ERR(domain))
3823 return NULL;
3824
3825 /* Only return IOMMUv2 domains */
3826 if (!(domain->flags & PD_IOMMUV2_MASK))
3827 return NULL;
3828
3829 return domain->iommu_domain;
3830}
3831EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003832
3833void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3834{
3835 struct iommu_dev_data *dev_data;
3836
3837 if (!amd_iommu_v2_supported())
3838 return;
3839
3840 dev_data = get_dev_data(&pdev->dev);
3841 dev_data->errata |= (1 << erratum);
3842}
3843EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003844
3845int amd_iommu_device_info(struct pci_dev *pdev,
3846 struct amd_iommu_device_info *info)
3847{
3848 int max_pasids;
3849 int pos;
3850
3851 if (pdev == NULL || info == NULL)
3852 return -EINVAL;
3853
3854 if (!amd_iommu_v2_supported())
3855 return -EINVAL;
3856
3857 memset(info, 0, sizeof(*info));
3858
3859 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3860 if (pos)
3861 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3862
3863 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3864 if (pos)
3865 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3866
3867 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3868 if (pos) {
3869 int features;
3870
3871 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3872 max_pasids = min(max_pasids, (1 << 20));
3873
3874 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3875 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3876
3877 features = pci_pasid_features(pdev);
3878 if (features & PCI_PASID_CAP_EXEC)
3879 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3880 if (features & PCI_PASID_CAP_PRIV)
3881 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3882 }
3883
3884 return 0;
3885}
3886EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003887
3888#ifdef CONFIG_IRQ_REMAP
3889
3890/*****************************************************************************
3891 *
3892 * Interrupt Remapping Implementation
3893 *
3894 *****************************************************************************/
3895
3896union irte {
3897 u32 val;
3898 struct {
3899 u32 valid : 1,
3900 no_fault : 1,
3901 int_type : 3,
3902 rq_eoi : 1,
3903 dm : 1,
3904 rsvd_1 : 1,
3905 destination : 8,
3906 vector : 8,
3907 rsvd_2 : 8;
3908 } fields;
3909};
3910
3911#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3912#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3913#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3914#define DTE_IRQ_REMAP_ENABLE 1ULL
3915
3916static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3917{
3918 u64 dte;
3919
3920 dte = amd_iommu_dev_table[devid].data[2];
3921 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3922 dte |= virt_to_phys(table->table);
3923 dte |= DTE_IRQ_REMAP_INTCTL;
3924 dte |= DTE_IRQ_TABLE_LEN;
3925 dte |= DTE_IRQ_REMAP_ENABLE;
3926
3927 amd_iommu_dev_table[devid].data[2] = dte;
3928}
3929
3930#define IRTE_ALLOCATED (~1U)
3931
3932static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3933{
3934 struct irq_remap_table *table = NULL;
3935 struct amd_iommu *iommu;
3936 unsigned long flags;
3937 u16 alias;
3938
3939 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3940
3941 iommu = amd_iommu_rlookup_table[devid];
3942 if (!iommu)
3943 goto out_unlock;
3944
3945 table = irq_lookup_table[devid];
3946 if (table)
3947 goto out;
3948
3949 alias = amd_iommu_alias_table[devid];
3950 table = irq_lookup_table[alias];
3951 if (table) {
3952 irq_lookup_table[devid] = table;
3953 set_dte_irq_entry(devid, table);
3954 iommu_flush_dte(iommu, devid);
3955 goto out;
3956 }
3957
3958 /* Nothing there yet, allocate new irq remapping table */
3959 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3960 if (!table)
3961 goto out;
3962
3963 if (ioapic)
3964 /* Keep the first 32 indexes free for IOAPIC interrupts */
3965 table->min_index = 32;
3966
3967 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3968 if (!table->table) {
3969 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003970 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003971 goto out;
3972 }
3973
3974 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3975
3976 if (ioapic) {
3977 int i;
3978
3979 for (i = 0; i < 32; ++i)
3980 table->table[i] = IRTE_ALLOCATED;
3981 }
3982
3983 irq_lookup_table[devid] = table;
3984 set_dte_irq_entry(devid, table);
3985 iommu_flush_dte(iommu, devid);
3986 if (devid != alias) {
3987 irq_lookup_table[alias] = table;
3988 set_dte_irq_entry(devid, table);
3989 iommu_flush_dte(iommu, alias);
3990 }
3991
3992out:
3993 iommu_completion_wait(iommu);
3994
3995out_unlock:
3996 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3997
3998 return table;
3999}
4000
4001static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
4002{
4003 struct irq_remap_table *table;
4004 unsigned long flags;
4005 int index, c;
4006
4007 table = get_irq_table(devid, false);
4008 if (!table)
4009 return -ENODEV;
4010
4011 spin_lock_irqsave(&table->lock, flags);
4012
4013 /* Scan table for free entries */
4014 for (c = 0, index = table->min_index;
4015 index < MAX_IRQS_PER_TABLE;
4016 ++index) {
4017 if (table->table[index] == 0)
4018 c += 1;
4019 else
4020 c = 0;
4021
4022 if (c == count) {
4023 struct irq_2_iommu *irte_info;
4024
4025 for (; c != 0; --c)
4026 table->table[index - c + 1] = IRTE_ALLOCATED;
4027
4028 index -= count - 1;
4029
4030 irte_info = &cfg->irq_2_iommu;
4031 irte_info->sub_handle = devid;
4032 irte_info->irte_index = index;
4033 irte_info->iommu = (void *)cfg;
4034
4035 goto out;
4036 }
4037 }
4038
4039 index = -ENOSPC;
4040
4041out:
4042 spin_unlock_irqrestore(&table->lock, flags);
4043
4044 return index;
4045}
4046
4047static int get_irte(u16 devid, int index, union irte *irte)
4048{
4049 struct irq_remap_table *table;
4050 unsigned long flags;
4051
4052 table = get_irq_table(devid, false);
4053 if (!table)
4054 return -ENOMEM;
4055
4056 spin_lock_irqsave(&table->lock, flags);
4057 irte->val = table->table[index];
4058 spin_unlock_irqrestore(&table->lock, flags);
4059
4060 return 0;
4061}
4062
4063static int modify_irte(u16 devid, int index, union irte irte)
4064{
4065 struct irq_remap_table *table;
4066 struct amd_iommu *iommu;
4067 unsigned long flags;
4068
4069 iommu = amd_iommu_rlookup_table[devid];
4070 if (iommu == NULL)
4071 return -EINVAL;
4072
4073 table = get_irq_table(devid, false);
4074 if (!table)
4075 return -ENOMEM;
4076
4077 spin_lock_irqsave(&table->lock, flags);
4078 table->table[index] = irte.val;
4079 spin_unlock_irqrestore(&table->lock, flags);
4080
4081 iommu_flush_irt(iommu, devid);
4082 iommu_completion_wait(iommu);
4083
4084 return 0;
4085}
4086
4087static void free_irte(u16 devid, int index)
4088{
4089 struct irq_remap_table *table;
4090 struct amd_iommu *iommu;
4091 unsigned long flags;
4092
4093 iommu = amd_iommu_rlookup_table[devid];
4094 if (iommu == NULL)
4095 return;
4096
4097 table = get_irq_table(devid, false);
4098 if (!table)
4099 return;
4100
4101 spin_lock_irqsave(&table->lock, flags);
4102 table->table[index] = 0;
4103 spin_unlock_irqrestore(&table->lock, flags);
4104
4105 iommu_flush_irt(iommu, devid);
4106 iommu_completion_wait(iommu);
4107}
4108
Joerg Roedel5527de72012-06-26 11:17:32 +02004109static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4110 unsigned int destination, int vector,
4111 struct io_apic_irq_attr *attr)
4112{
4113 struct irq_remap_table *table;
4114 struct irq_2_iommu *irte_info;
4115 struct irq_cfg *cfg;
4116 union irte irte;
4117 int ioapic_id;
4118 int index;
4119 int devid;
4120 int ret;
4121
4122 cfg = irq_get_chip_data(irq);
4123 if (!cfg)
4124 return -EINVAL;
4125
4126 irte_info = &cfg->irq_2_iommu;
4127 ioapic_id = mpc_ioapic_id(attr->ioapic);
4128 devid = get_ioapic_devid(ioapic_id);
4129
4130 if (devid < 0)
4131 return devid;
4132
4133 table = get_irq_table(devid, true);
4134 if (table == NULL)
4135 return -ENOMEM;
4136
4137 index = attr->ioapic_pin;
4138
4139 /* Setup IRQ remapping info */
4140 irte_info->sub_handle = devid;
4141 irte_info->irte_index = index;
4142 irte_info->iommu = (void *)cfg;
4143
4144 /* Setup IRTE for IOMMU */
4145 irte.val = 0;
4146 irte.fields.vector = vector;
4147 irte.fields.int_type = apic->irq_delivery_mode;
4148 irte.fields.destination = destination;
4149 irte.fields.dm = apic->irq_dest_mode;
4150 irte.fields.valid = 1;
4151
4152 ret = modify_irte(devid, index, irte);
4153 if (ret)
4154 return ret;
4155
4156 /* Setup IOAPIC entry */
4157 memset(entry, 0, sizeof(*entry));
4158
4159 entry->vector = index;
4160 entry->mask = 0;
4161 entry->trigger = attr->trigger;
4162 entry->polarity = attr->polarity;
4163
4164 /*
4165 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004166 */
4167 if (attr->trigger)
4168 entry->mask = 1;
4169
4170 return 0;
4171}
4172
4173static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4174 bool force)
4175{
4176 struct irq_2_iommu *irte_info;
4177 unsigned int dest, irq;
4178 struct irq_cfg *cfg;
4179 union irte irte;
4180 int err;
4181
4182 if (!config_enabled(CONFIG_SMP))
4183 return -1;
4184
4185 cfg = data->chip_data;
4186 irq = data->irq;
4187 irte_info = &cfg->irq_2_iommu;
4188
4189 if (!cpumask_intersects(mask, cpu_online_mask))
4190 return -EINVAL;
4191
4192 if (get_irte(irte_info->sub_handle, irte_info->irte_index, &irte))
4193 return -EBUSY;
4194
4195 if (assign_irq_vector(irq, cfg, mask))
4196 return -EBUSY;
4197
4198 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4199 if (err) {
4200 if (assign_irq_vector(irq, cfg, data->affinity))
4201 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4202 return err;
4203 }
4204
4205 irte.fields.vector = cfg->vector;
4206 irte.fields.destination = dest;
4207
4208 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4209
4210 if (cfg->move_in_progress)
4211 send_cleanup_vector(cfg);
4212
4213 cpumask_copy(data->affinity, mask);
4214
4215 return 0;
4216}
4217
4218static int free_irq(int irq)
4219{
4220 struct irq_2_iommu *irte_info;
4221 struct irq_cfg *cfg;
4222
4223 cfg = irq_get_chip_data(irq);
4224 if (!cfg)
4225 return -EINVAL;
4226
4227 irte_info = &cfg->irq_2_iommu;
4228
4229 free_irte(irte_info->sub_handle, irte_info->irte_index);
4230
4231 return 0;
4232}
4233
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004234static void compose_msi_msg(struct pci_dev *pdev,
4235 unsigned int irq, unsigned int dest,
4236 struct msi_msg *msg, u8 hpet_id)
4237{
4238 struct irq_2_iommu *irte_info;
4239 struct irq_cfg *cfg;
4240 union irte irte;
4241
4242 cfg = irq_get_chip_data(irq);
4243 if (!cfg)
4244 return;
4245
4246 irte_info = &cfg->irq_2_iommu;
4247
4248 irte.val = 0;
4249 irte.fields.vector = cfg->vector;
4250 irte.fields.int_type = apic->irq_delivery_mode;
4251 irte.fields.destination = dest;
4252 irte.fields.dm = apic->irq_dest_mode;
4253 irte.fields.valid = 1;
4254
4255 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4256
4257 msg->address_hi = MSI_ADDR_BASE_HI;
4258 msg->address_lo = MSI_ADDR_BASE_LO;
4259 msg->data = irte_info->irte_index;
4260}
4261
4262static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4263{
4264 struct irq_cfg *cfg;
4265 int index;
4266 u16 devid;
4267
4268 if (!pdev)
4269 return -EINVAL;
4270
4271 cfg = irq_get_chip_data(irq);
4272 if (!cfg)
4273 return -EINVAL;
4274
4275 devid = get_device_id(&pdev->dev);
4276 index = alloc_irq_index(cfg, devid, nvec);
4277
4278 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4279}
4280
4281static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4282 int index, int offset)
4283{
4284 struct irq_2_iommu *irte_info;
4285 struct irq_cfg *cfg;
4286 u16 devid;
4287
4288 if (!pdev)
4289 return -EINVAL;
4290
4291 cfg = irq_get_chip_data(irq);
4292 if (!cfg)
4293 return -EINVAL;
4294
4295 if (index >= MAX_IRQS_PER_TABLE)
4296 return 0;
4297
4298 devid = get_device_id(&pdev->dev);
4299 irte_info = &cfg->irq_2_iommu;
4300
4301 irte_info->sub_handle = devid;
4302 irte_info->irte_index = index + offset;
4303 irte_info->iommu = (void *)cfg;
4304
4305 return 0;
4306}
4307
Joerg Roedeld9761952012-06-26 16:00:08 +02004308static int setup_hpet_msi(unsigned int irq, unsigned int id)
4309{
4310 struct irq_2_iommu *irte_info;
4311 struct irq_cfg *cfg;
4312 int index, devid;
4313
4314 cfg = irq_get_chip_data(irq);
4315 if (!cfg)
4316 return -EINVAL;
4317
4318 irte_info = &cfg->irq_2_iommu;
4319 devid = get_hpet_devid(id);
4320 if (devid < 0)
4321 return devid;
4322
4323 index = alloc_irq_index(cfg, devid, 1);
4324 if (index < 0)
4325 return index;
4326
4327 irte_info->sub_handle = devid;
4328 irte_info->irte_index = index;
4329 irte_info->iommu = (void *)cfg;
4330
4331 return 0;
4332}
4333
Joerg Roedel6b474b82012-06-26 16:46:04 +02004334struct irq_remap_ops amd_iommu_irq_ops = {
4335 .supported = amd_iommu_supported,
4336 .prepare = amd_iommu_prepare,
4337 .enable = amd_iommu_enable,
4338 .disable = amd_iommu_disable,
4339 .reenable = amd_iommu_reenable,
4340 .enable_faulting = amd_iommu_enable_faulting,
4341 .setup_ioapic_entry = setup_ioapic_entry,
4342 .set_affinity = set_affinity,
4343 .free_irq = free_irq,
4344 .compose_msi_msg = compose_msi_msg,
4345 .msi_alloc_irq = msi_alloc_irq,
4346 .msi_setup_irq = msi_setup_irq,
4347 .setup_hpet_msi = setup_hpet_msi,
4348};
Joerg Roedel2b324502012-06-21 16:29:10 +02004349#endif