blob: 4b029c17f8d009263ca0aef0a10fdd62d89a5113 [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"
Varun Sethi61e015a2013-04-23 10:05:24 +053049#include "pci.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020050
51#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
52
Joerg Roedel815b33f2011-04-06 17:26:49 +020053#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020054
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020055/*
56 * This bitmap is used to advertise the page sizes our hardware support
57 * to the IOMMU core, which will then use this information to split
58 * physically contiguous memory regions it is mapping into page sizes
59 * that we support.
60 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010061 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020062 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010063#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020064
Joerg Roedelb6c02712008-06-26 21:27:53 +020065static DEFINE_RWLOCK(amd_iommu_devtable_lock);
66
Joerg Roedelbd60b732008-09-11 10:24:48 +020067/* A list of preallocated protection domains */
68static LIST_HEAD(iommu_pd_list);
69static DEFINE_SPINLOCK(iommu_pd_list_lock);
70
Joerg Roedel8fa5f802011-06-09 12:24:45 +020071/* List of all available dev_data structures */
72static LIST_HEAD(dev_data_list);
73static DEFINE_SPINLOCK(dev_data_list_lock);
74
Joerg Roedel6efed632012-06-14 15:52:58 +020075LIST_HEAD(ioapic_map);
76LIST_HEAD(hpet_map);
77
Joerg Roedel0feae532009-08-26 15:26:30 +020078/*
79 * Domain for untranslated devices - only allocated
80 * if iommu=pt passed on kernel cmd line.
81 */
82static struct protection_domain *pt_domain;
83
Joerg Roedel26961ef2008-12-03 17:00:17 +010084static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010085
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010086static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010087int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010088
Joerg Roedelac1534a2012-06-21 14:52:40 +020089static struct dma_map_ops amd_iommu_dma_ops;
90
Joerg Roedel431b2a22008-07-11 17:14:22 +020091/*
92 * general struct to manage commands send to an IOMMU
93 */
Joerg Roedeld6449532008-07-11 17:14:28 +020094struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020095 u32 data[4];
96};
97
Joerg Roedel05152a02012-06-15 16:53:51 +020098struct kmem_cache *amd_iommu_irq_cache;
99
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200100static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100101static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700102
Joerg Roedel15898bb2009-11-24 15:39:42 +0100103/****************************************************************************
104 *
105 * Helper functions
106 *
107 ****************************************************************************/
108
Joerg Roedelf62dda62011-06-09 12:55:35 +0200109static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200110{
111 struct iommu_dev_data *dev_data;
112 unsigned long flags;
113
114 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
115 if (!dev_data)
116 return NULL;
117
Joerg Roedelf62dda62011-06-09 12:55:35 +0200118 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200119 atomic_set(&dev_data->bind, 0);
120
121 spin_lock_irqsave(&dev_data_list_lock, flags);
122 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
123 spin_unlock_irqrestore(&dev_data_list_lock, flags);
124
125 return dev_data;
126}
127
128static void free_dev_data(struct iommu_dev_data *dev_data)
129{
130 unsigned long flags;
131
132 spin_lock_irqsave(&dev_data_list_lock, flags);
133 list_del(&dev_data->dev_data_list);
134 spin_unlock_irqrestore(&dev_data_list_lock, flags);
135
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600136 if (dev_data->group)
137 iommu_group_put(dev_data->group);
138
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200139 kfree(dev_data);
140}
141
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200142static struct iommu_dev_data *search_dev_data(u16 devid)
143{
144 struct iommu_dev_data *dev_data;
145 unsigned long flags;
146
147 spin_lock_irqsave(&dev_data_list_lock, flags);
148 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
149 if (dev_data->devid == devid)
150 goto out_unlock;
151 }
152
153 dev_data = NULL;
154
155out_unlock:
156 spin_unlock_irqrestore(&dev_data_list_lock, flags);
157
158 return dev_data;
159}
160
161static struct iommu_dev_data *find_dev_data(u16 devid)
162{
163 struct iommu_dev_data *dev_data;
164
165 dev_data = search_dev_data(devid);
166
167 if (dev_data == NULL)
168 dev_data = alloc_dev_data(devid);
169
170 return dev_data;
171}
172
Joerg Roedel15898bb2009-11-24 15:39:42 +0100173static inline u16 get_device_id(struct device *dev)
174{
175 struct pci_dev *pdev = to_pci_dev(dev);
176
Shuah Khan6f2729b2013-02-27 17:07:30 -0700177 return PCI_DEVID(pdev->bus->number, pdev->devfn);
Joerg Roedel15898bb2009-11-24 15:39:42 +0100178}
179
Joerg Roedel657cbb62009-11-23 15:26:46 +0100180static struct iommu_dev_data *get_dev_data(struct device *dev)
181{
182 return dev->archdata.iommu;
183}
184
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100185static bool pci_iommuv2_capable(struct pci_dev *pdev)
186{
187 static const int caps[] = {
188 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100189 PCI_EXT_CAP_ID_PRI,
190 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100191 };
192 int i, pos;
193
194 for (i = 0; i < 3; ++i) {
195 pos = pci_find_ext_capability(pdev, caps[i]);
196 if (pos == 0)
197 return false;
198 }
199
200 return true;
201}
202
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100203static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
204{
205 struct iommu_dev_data *dev_data;
206
207 dev_data = get_dev_data(&pdev->dev);
208
209 return dev_data->errata & (1 << erratum) ? true : false;
210}
211
Joerg Roedel71c70982009-11-24 16:43:06 +0100212/*
213 * In this function the list of preallocated protection domains is traversed to
214 * find the domain for a specific device
215 */
216static struct dma_ops_domain *find_protection_domain(u16 devid)
217{
218 struct dma_ops_domain *entry, *ret = NULL;
219 unsigned long flags;
220 u16 alias = amd_iommu_alias_table[devid];
221
222 if (list_empty(&iommu_pd_list))
223 return NULL;
224
225 spin_lock_irqsave(&iommu_pd_list_lock, flags);
226
227 list_for_each_entry(entry, &iommu_pd_list, list) {
228 if (entry->target_dev == devid ||
229 entry->target_dev == alias) {
230 ret = entry;
231 break;
232 }
233 }
234
235 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
236
237 return ret;
238}
239
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100240/*
241 * This function checks if the driver got a valid device from the caller to
242 * avoid dereferencing invalid pointers.
243 */
244static bool check_device(struct device *dev)
245{
246 u16 devid;
247
248 if (!dev || !dev->dma_mask)
249 return false;
250
251 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100252 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100253 return false;
254
255 devid = get_device_id(dev);
256
257 /* Out of our scope? */
258 if (devid > amd_iommu_last_bdf)
259 return false;
260
261 if (amd_iommu_rlookup_table[devid] == NULL)
262 return false;
263
264 return true;
265}
266
Alex Williamson2bff6a52012-10-08 22:49:48 -0600267static struct pci_bus *find_hosted_bus(struct pci_bus *bus)
268{
269 while (!bus->self) {
270 if (!pci_is_root_bus(bus))
271 bus = bus->parent;
272 else
273 return ERR_PTR(-ENODEV);
274 }
275
276 return bus;
277}
278
Alex Williamson664b6002012-05-30 14:19:31 -0600279#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
280
Alex Williamson2851db22012-10-08 22:49:41 -0600281static struct pci_dev *get_isolation_root(struct pci_dev *pdev)
Joerg Roedel657cbb62009-11-23 15:26:46 +0100282{
Alex Williamson2851db22012-10-08 22:49:41 -0600283 struct pci_dev *dma_pdev = pdev;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600284
Alex Williamson31fe9432012-08-04 12:09:03 -0600285 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600286 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
287
Alex Williamson31fe9432012-08-04 12:09:03 -0600288 /*
289 * If it's a multifunction device that does not support our
290 * required ACS flags, add to the same group as function 0.
291 */
Alex Williamson664b6002012-05-30 14:19:31 -0600292 if (dma_pdev->multifunction &&
293 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
294 swap_pci_ref(&dma_pdev,
295 pci_get_slot(dma_pdev->bus,
296 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
297 0)));
298
Alex Williamson31fe9432012-08-04 12:09:03 -0600299 /*
300 * Devices on the root bus go through the iommu. If that's not us,
301 * find the next upstream device and test ACS up to the root bus.
302 * Finding the next device may require skipping virtual buses.
303 */
Alex Williamson664b6002012-05-30 14:19:31 -0600304 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson2bff6a52012-10-08 22:49:48 -0600305 struct pci_bus *bus = find_hosted_bus(dma_pdev->bus);
306 if (IS_ERR(bus))
307 break;
Alex Williamson31fe9432012-08-04 12:09:03 -0600308
309 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600310 break;
311
Alex Williamson31fe9432012-08-04 12:09:03 -0600312 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200313 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100314
Alex Williamson2851db22012-10-08 22:49:41 -0600315 return dma_pdev;
316}
317
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600318static int use_pdev_iommu_group(struct pci_dev *pdev, struct device *dev)
319{
320 struct iommu_group *group = iommu_group_get(&pdev->dev);
321 int ret;
322
323 if (!group) {
324 group = iommu_group_alloc();
325 if (IS_ERR(group))
326 return PTR_ERR(group);
327
328 WARN_ON(&pdev->dev != dev);
329 }
330
331 ret = iommu_group_add_device(group, dev);
332 iommu_group_put(group);
333 return ret;
334}
335
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600336static int use_dev_data_iommu_group(struct iommu_dev_data *dev_data,
337 struct device *dev)
338{
339 if (!dev_data->group) {
340 struct iommu_group *group = iommu_group_alloc();
341 if (IS_ERR(group))
342 return PTR_ERR(group);
343
344 dev_data->group = group;
345 }
346
347 return iommu_group_add_device(dev_data->group, dev);
348}
349
Alex Williamson2851db22012-10-08 22:49:41 -0600350static int init_iommu_group(struct device *dev)
351{
352 struct iommu_dev_data *dev_data;
353 struct iommu_group *group;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600354 struct pci_dev *dma_pdev;
Alex Williamson2851db22012-10-08 22:49:41 -0600355 int ret;
356
357 group = iommu_group_get(dev);
358 if (group) {
359 iommu_group_put(group);
360 return 0;
361 }
362
363 dev_data = find_dev_data(get_device_id(dev));
364 if (!dev_data)
365 return -ENOMEM;
366
367 if (dev_data->alias_data) {
368 u16 alias;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600369 struct pci_bus *bus;
Alex Williamson2851db22012-10-08 22:49:41 -0600370
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600371 if (dev_data->alias_data->group)
372 goto use_group;
373
374 /*
375 * If the alias device exists, it's effectively just a first
376 * level quirk for finding the DMA source.
377 */
Alex Williamson2851db22012-10-08 22:49:41 -0600378 alias = amd_iommu_alias_table[dev_data->devid];
379 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600380 if (dma_pdev) {
381 dma_pdev = get_isolation_root(dma_pdev);
382 goto use_pdev;
383 }
384
385 /*
386 * If the alias is virtual, try to find a parent device
387 * and test whether the IOMMU group is actualy rooted above
388 * the alias. Be careful to also test the parent device if
389 * we think the alias is the root of the group.
390 */
391 bus = pci_find_bus(0, alias >> 8);
392 if (!bus)
393 goto use_group;
394
395 bus = find_hosted_bus(bus);
396 if (IS_ERR(bus) || !bus->self)
397 goto use_group;
398
399 dma_pdev = get_isolation_root(pci_dev_get(bus->self));
400 if (dma_pdev != bus->self || (dma_pdev->multifunction &&
401 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)))
402 goto use_pdev;
403
404 pci_dev_put(dma_pdev);
405 goto use_group;
Alex Williamson2851db22012-10-08 22:49:41 -0600406 }
407
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600408 dma_pdev = get_isolation_root(pci_dev_get(to_pci_dev(dev)));
409use_pdev:
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600410 ret = use_pdev_iommu_group(dma_pdev, dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600411 pci_dev_put(dma_pdev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600412 return ret;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600413use_group:
414 return use_dev_data_iommu_group(dev_data->alias_data, dev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600415}
416
417static int iommu_init_device(struct device *dev)
418{
419 struct pci_dev *pdev = to_pci_dev(dev);
420 struct iommu_dev_data *dev_data;
421 u16 alias;
422 int ret;
423
424 if (dev->archdata.iommu)
425 return 0;
426
427 dev_data = find_dev_data(get_device_id(dev));
428 if (!dev_data)
429 return -ENOMEM;
430
431 alias = amd_iommu_alias_table[dev_data->devid];
432 if (alias != dev_data->devid) {
433 struct iommu_dev_data *alias_data;
434
435 alias_data = find_dev_data(alias);
436 if (alias_data == NULL) {
437 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
438 dev_name(dev));
439 free_dev_data(dev_data);
440 return -ENOTSUPP;
441 }
442 dev_data->alias_data = alias_data;
443 }
444
445 ret = init_iommu_group(dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600446 if (ret)
447 return ret;
448
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100449 if (pci_iommuv2_capable(pdev)) {
450 struct amd_iommu *iommu;
451
452 iommu = amd_iommu_rlookup_table[dev_data->devid];
453 dev_data->iommu_v2 = iommu->is_iommu_v2;
454 }
455
Joerg Roedel657cbb62009-11-23 15:26:46 +0100456 dev->archdata.iommu = dev_data;
457
Joerg Roedel657cbb62009-11-23 15:26:46 +0100458 return 0;
459}
460
Joerg Roedel26018872011-06-06 16:50:14 +0200461static void iommu_ignore_device(struct device *dev)
462{
463 u16 devid, alias;
464
465 devid = get_device_id(dev);
466 alias = amd_iommu_alias_table[devid];
467
468 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
469 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
470
471 amd_iommu_rlookup_table[devid] = NULL;
472 amd_iommu_rlookup_table[alias] = NULL;
473}
474
Joerg Roedel657cbb62009-11-23 15:26:46 +0100475static void iommu_uninit_device(struct device *dev)
476{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600477 iommu_group_remove_device(dev);
478
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200479 /*
480 * Nothing to do here - we keep dev_data around for unplugged devices
481 * and reuse it when the device is re-plugged - not doing so would
482 * introduce a ton of races.
483 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100484}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100485
486void __init amd_iommu_uninit_devices(void)
487{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200488 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100489 struct pci_dev *pdev = NULL;
490
491 for_each_pci_dev(pdev) {
492
493 if (!check_device(&pdev->dev))
494 continue;
495
496 iommu_uninit_device(&pdev->dev);
497 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200498
499 /* Free all of our dev_data structures */
500 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
501 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100502}
503
504int __init amd_iommu_init_devices(void)
505{
506 struct pci_dev *pdev = NULL;
507 int ret = 0;
508
509 for_each_pci_dev(pdev) {
510
511 if (!check_device(&pdev->dev))
512 continue;
513
514 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200515 if (ret == -ENOTSUPP)
516 iommu_ignore_device(&pdev->dev);
517 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100518 goto out_free;
519 }
520
521 return 0;
522
523out_free:
524
525 amd_iommu_uninit_devices();
526
527 return ret;
528}
Joerg Roedel7f265082008-12-12 13:50:21 +0100529#ifdef CONFIG_AMD_IOMMU_STATS
530
531/*
532 * Initialization code for statistics collection
533 */
534
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100535DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100536DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100537DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100538DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100539DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100540DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100541DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100542DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100543DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100544DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100545DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100546DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100547DECLARE_STATS_COUNTER(complete_ppr);
548DECLARE_STATS_COUNTER(invalidate_iotlb);
549DECLARE_STATS_COUNTER(invalidate_iotlb_all);
550DECLARE_STATS_COUNTER(pri_requests);
551
Joerg Roedel7f265082008-12-12 13:50:21 +0100552static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100553static struct dentry *de_fflush;
554
555static void amd_iommu_stats_add(struct __iommu_counter *cnt)
556{
557 if (stats_dir == NULL)
558 return;
559
560 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
561 &cnt->value);
562}
563
564static void amd_iommu_stats_init(void)
565{
566 stats_dir = debugfs_create_dir("amd-iommu", NULL);
567 if (stats_dir == NULL)
568 return;
569
Joerg Roedel7f265082008-12-12 13:50:21 +0100570 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300571 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100572
573 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100574 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100575 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100576 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100577 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100578 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100579 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100580 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100581 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100582 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100583 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100584 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100585 amd_iommu_stats_add(&complete_ppr);
586 amd_iommu_stats_add(&invalidate_iotlb);
587 amd_iommu_stats_add(&invalidate_iotlb_all);
588 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100589}
590
591#endif
592
Joerg Roedel431b2a22008-07-11 17:14:22 +0200593/****************************************************************************
594 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200595 * Interrupt handling functions
596 *
597 ****************************************************************************/
598
Joerg Roedele3e59872009-09-03 14:02:10 +0200599static void dump_dte_entry(u16 devid)
600{
601 int i;
602
Joerg Roedelee6c2862011-11-09 12:06:03 +0100603 for (i = 0; i < 4; ++i)
604 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200605 amd_iommu_dev_table[devid].data[i]);
606}
607
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200608static void dump_command(unsigned long phys_addr)
609{
610 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
611 int i;
612
613 for (i = 0; i < 4; ++i)
614 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
615}
616
Joerg Roedela345b232009-09-03 15:01:43 +0200617static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200618{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200619 int type, devid, domid, flags;
620 volatile u32 *event = __evt;
621 int count = 0;
622 u64 address;
623
624retry:
625 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
626 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
627 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
628 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
629 address = (u64)(((u64)event[3]) << 32) | event[2];
630
631 if (type == 0) {
632 /* Did we hit the erratum? */
633 if (++count == LOOP_TIMEOUT) {
634 pr_err("AMD-Vi: No event written to event log\n");
635 return;
636 }
637 udelay(1);
638 goto retry;
639 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200640
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200641 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200642
643 switch (type) {
644 case EVENT_TYPE_ILL_DEV:
645 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
646 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700647 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200648 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200649 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200650 break;
651 case EVENT_TYPE_IO_FAULT:
652 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
653 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700654 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200655 domid, address, flags);
656 break;
657 case EVENT_TYPE_DEV_TAB_ERR:
658 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
659 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700660 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200661 address, flags);
662 break;
663 case EVENT_TYPE_PAGE_TAB_ERR:
664 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
665 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700666 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200667 domid, address, flags);
668 break;
669 case EVENT_TYPE_ILL_CMD:
670 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200671 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200672 break;
673 case EVENT_TYPE_CMD_HARD_ERR:
674 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
675 "flags=0x%04x]\n", address, flags);
676 break;
677 case EVENT_TYPE_IOTLB_INV_TO:
678 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
679 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700680 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200681 address);
682 break;
683 case EVENT_TYPE_INV_DEV_REQ:
684 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
685 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700686 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200687 address, flags);
688 break;
689 default:
690 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
691 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200692
693 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200694}
695
696static void iommu_poll_events(struct amd_iommu *iommu)
697{
698 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200699
700 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
701 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
702
703 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200704 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200705 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
706 }
707
708 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200709}
710
Joerg Roedeleee53532012-06-01 15:20:23 +0200711static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100712{
713 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100714
Joerg Roedel399be2f2011-12-01 16:53:47 +0100715 INC_STATS_COUNTER(pri_requests);
716
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100717 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
718 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
719 return;
720 }
721
722 fault.address = raw[1];
723 fault.pasid = PPR_PASID(raw[0]);
724 fault.device_id = PPR_DEVID(raw[0]);
725 fault.tag = PPR_TAG(raw[0]);
726 fault.flags = PPR_FLAGS(raw[0]);
727
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100728 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
729}
730
731static void iommu_poll_ppr_log(struct amd_iommu *iommu)
732{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100733 u32 head, tail;
734
735 if (iommu->ppr_log == NULL)
736 return;
737
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100738 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
739 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
740
741 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200742 volatile u64 *raw;
743 u64 entry[2];
744 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100745
Joerg Roedeleee53532012-06-01 15:20:23 +0200746 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100747
Joerg Roedeleee53532012-06-01 15:20:23 +0200748 /*
749 * Hardware bug: Interrupt may arrive before the entry is
750 * written to memory. If this happens we need to wait for the
751 * entry to arrive.
752 */
753 for (i = 0; i < LOOP_TIMEOUT; ++i) {
754 if (PPR_REQ_TYPE(raw[0]) != 0)
755 break;
756 udelay(1);
757 }
758
759 /* Avoid memcpy function-call overhead */
760 entry[0] = raw[0];
761 entry[1] = raw[1];
762
763 /*
764 * To detect the hardware bug we need to clear the entry
765 * back to zero.
766 */
767 raw[0] = raw[1] = 0UL;
768
769 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100770 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
771 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200772
Joerg Roedeleee53532012-06-01 15:20:23 +0200773 /* Handle PPR entry */
774 iommu_handle_ppr_entry(iommu, entry);
775
Joerg Roedeleee53532012-06-01 15:20:23 +0200776 /* Refresh ring-buffer information */
777 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100778 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
779 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100780}
781
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200782irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200783{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500784 struct amd_iommu *iommu = (struct amd_iommu *) data;
785 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200786
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500787 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
788 /* Enable EVT and PPR interrupts again */
789 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
790 iommu->mmio_base + MMIO_STATUS_OFFSET);
791
792 if (status & MMIO_STATUS_EVT_INT_MASK) {
793 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
794 iommu_poll_events(iommu);
795 }
796
797 if (status & MMIO_STATUS_PPR_INT_MASK) {
798 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
799 iommu_poll_ppr_log(iommu);
800 }
801
802 /*
803 * Hardware bug: ERBT1312
804 * When re-enabling interrupt (by writing 1
805 * to clear the bit), the hardware might also try to set
806 * the interrupt bit in the event status register.
807 * In this scenario, the bit will be set, and disable
808 * subsequent interrupts.
809 *
810 * Workaround: The IOMMU driver should read back the
811 * status register and check if the interrupt bits are cleared.
812 * If not, driver will need to go through the interrupt handler
813 * again and re-clear the bits
814 */
815 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100816 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200817 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200818}
819
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200820irqreturn_t amd_iommu_int_handler(int irq, void *data)
821{
822 return IRQ_WAKE_THREAD;
823}
824
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200825/****************************************************************************
826 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200827 * IOMMU command queuing functions
828 *
829 ****************************************************************************/
830
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200831static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200832{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200833 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200834
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200835 while (*sem == 0 && i < LOOP_TIMEOUT) {
836 udelay(1);
837 i += 1;
838 }
839
840 if (i == LOOP_TIMEOUT) {
841 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
842 return -EIO;
843 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200844
845 return 0;
846}
847
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200848static void copy_cmd_to_buffer(struct amd_iommu *iommu,
849 struct iommu_cmd *cmd,
850 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200851{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200852 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200853
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200854 target = iommu->cmd_buf + tail;
855 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200856
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200857 /* Copy command to buffer */
858 memcpy(target, cmd, sizeof(*cmd));
859
860 /* Tell the IOMMU about it */
861 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
862}
863
Joerg Roedel815b33f2011-04-06 17:26:49 +0200864static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200865{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200866 WARN_ON(address & 0x7ULL);
867
Joerg Roedelded46732011-04-06 10:53:48 +0200868 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200869 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
870 cmd->data[1] = upper_32_bits(__pa(address));
871 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200872 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
873}
874
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200875static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
876{
877 memset(cmd, 0, sizeof(*cmd));
878 cmd->data[0] = devid;
879 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
880}
881
Joerg Roedel11b64022011-04-06 11:49:28 +0200882static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
883 size_t size, u16 domid, int pde)
884{
885 u64 pages;
886 int s;
887
888 pages = iommu_num_pages(address, size, PAGE_SIZE);
889 s = 0;
890
891 if (pages > 1) {
892 /*
893 * If we have to flush more than one page, flush all
894 * TLB entries for this domain
895 */
896 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
897 s = 1;
898 }
899
900 address &= PAGE_MASK;
901
902 memset(cmd, 0, sizeof(*cmd));
903 cmd->data[1] |= domid;
904 cmd->data[2] = lower_32_bits(address);
905 cmd->data[3] = upper_32_bits(address);
906 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
907 if (s) /* size bit - we flush more than one 4kb page */
908 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200909 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200910 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
911}
912
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200913static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
914 u64 address, size_t size)
915{
916 u64 pages;
917 int s;
918
919 pages = iommu_num_pages(address, size, PAGE_SIZE);
920 s = 0;
921
922 if (pages > 1) {
923 /*
924 * If we have to flush more than one page, flush all
925 * TLB entries for this domain
926 */
927 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
928 s = 1;
929 }
930
931 address &= PAGE_MASK;
932
933 memset(cmd, 0, sizeof(*cmd));
934 cmd->data[0] = devid;
935 cmd->data[0] |= (qdep & 0xff) << 24;
936 cmd->data[1] = devid;
937 cmd->data[2] = lower_32_bits(address);
938 cmd->data[3] = upper_32_bits(address);
939 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
940 if (s)
941 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
942}
943
Joerg Roedel22e266c2011-11-21 15:59:08 +0100944static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
945 u64 address, bool size)
946{
947 memset(cmd, 0, sizeof(*cmd));
948
949 address &= ~(0xfffULL);
950
951 cmd->data[0] = pasid & PASID_MASK;
952 cmd->data[1] = domid;
953 cmd->data[2] = lower_32_bits(address);
954 cmd->data[3] = upper_32_bits(address);
955 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
956 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
957 if (size)
958 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
959 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
960}
961
962static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
963 int qdep, u64 address, bool size)
964{
965 memset(cmd, 0, sizeof(*cmd));
966
967 address &= ~(0xfffULL);
968
969 cmd->data[0] = devid;
970 cmd->data[0] |= (pasid & 0xff) << 16;
971 cmd->data[0] |= (qdep & 0xff) << 24;
972 cmd->data[1] = devid;
973 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
974 cmd->data[2] = lower_32_bits(address);
975 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
976 cmd->data[3] = upper_32_bits(address);
977 if (size)
978 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
979 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
980}
981
Joerg Roedelc99afa22011-11-21 18:19:25 +0100982static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
983 int status, int tag, bool gn)
984{
985 memset(cmd, 0, sizeof(*cmd));
986
987 cmd->data[0] = devid;
988 if (gn) {
989 cmd->data[1] = pasid & PASID_MASK;
990 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
991 }
992 cmd->data[3] = tag & 0x1ff;
993 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
994
995 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
996}
997
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200998static void build_inv_all(struct iommu_cmd *cmd)
999{
1000 memset(cmd, 0, sizeof(*cmd));
1001 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001002}
1003
Joerg Roedel7ef27982012-06-21 16:46:04 +02001004static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1005{
1006 memset(cmd, 0, sizeof(*cmd));
1007 cmd->data[0] = devid;
1008 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1009}
1010
Joerg Roedel431b2a22008-07-11 17:14:22 +02001011/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001012 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001013 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001014 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001015static int iommu_queue_command_sync(struct amd_iommu *iommu,
1016 struct iommu_cmd *cmd,
1017 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001018{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001019 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +02001020 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001021
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001022 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +01001023
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001024again:
Joerg Roedel815b33f2011-04-06 17:26:49 +02001025 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001026
1027 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1028 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1029 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
1030 left = (head - next_tail) % iommu->cmd_buf_size;
1031
1032 if (left <= 2) {
1033 struct iommu_cmd sync_cmd;
1034 volatile u64 sem = 0;
1035 int ret;
1036
1037 build_completion_wait(&sync_cmd, (u64)&sem);
1038 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1039
1040 spin_unlock_irqrestore(&iommu->lock, flags);
1041
1042 if ((ret = wait_on_sem(&sem)) != 0)
1043 return ret;
1044
1045 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001046 }
1047
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001048 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001049
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001050 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001051 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001052
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001053 spin_unlock_irqrestore(&iommu->lock, flags);
1054
Joerg Roedel815b33f2011-04-06 17:26:49 +02001055 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001056}
1057
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001058static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1059{
1060 return iommu_queue_command_sync(iommu, cmd, true);
1061}
1062
Joerg Roedel8d201962008-12-02 20:34:41 +01001063/*
1064 * This function queues a completion wait command into the command
1065 * buffer of an IOMMU
1066 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001067static int iommu_completion_wait(struct amd_iommu *iommu)
1068{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001069 struct iommu_cmd cmd;
1070 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001071 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001072
1073 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001074 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001075
Joerg Roedel815b33f2011-04-06 17:26:49 +02001076 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001077
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001078 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001079 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001080 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001081
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001082 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001083}
1084
Joerg Roedeld8c13082011-04-06 18:51:26 +02001085static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001086{
1087 struct iommu_cmd cmd;
1088
Joerg Roedeld8c13082011-04-06 18:51:26 +02001089 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001090
Joerg Roedeld8c13082011-04-06 18:51:26 +02001091 return iommu_queue_command(iommu, &cmd);
1092}
1093
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001094static void iommu_flush_dte_all(struct amd_iommu *iommu)
1095{
1096 u32 devid;
1097
1098 for (devid = 0; devid <= 0xffff; ++devid)
1099 iommu_flush_dte(iommu, devid);
1100
1101 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001102}
1103
1104/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001105 * This function uses heavy locking and may disable irqs for some time. But
1106 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001107 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001108static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001109{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001110 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001111
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001112 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1113 struct iommu_cmd cmd;
1114 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1115 dom_id, 1);
1116 iommu_queue_command(iommu, &cmd);
1117 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001118
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001119 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001120}
1121
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001122static void iommu_flush_all(struct amd_iommu *iommu)
1123{
1124 struct iommu_cmd cmd;
1125
1126 build_inv_all(&cmd);
1127
1128 iommu_queue_command(iommu, &cmd);
1129 iommu_completion_wait(iommu);
1130}
1131
Joerg Roedel7ef27982012-06-21 16:46:04 +02001132static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1133{
1134 struct iommu_cmd cmd;
1135
1136 build_inv_irt(&cmd, devid);
1137
1138 iommu_queue_command(iommu, &cmd);
1139}
1140
1141static void iommu_flush_irt_all(struct amd_iommu *iommu)
1142{
1143 u32 devid;
1144
1145 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1146 iommu_flush_irt(iommu, devid);
1147
1148 iommu_completion_wait(iommu);
1149}
1150
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001151void iommu_flush_all_caches(struct amd_iommu *iommu)
1152{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001153 if (iommu_feature(iommu, FEATURE_IA)) {
1154 iommu_flush_all(iommu);
1155 } else {
1156 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001157 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001158 iommu_flush_tlb_all(iommu);
1159 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001160}
1161
Joerg Roedel431b2a22008-07-11 17:14:22 +02001162/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001163 * Command send function for flushing on-device TLB
1164 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001165static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1166 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001167{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001168 struct amd_iommu *iommu;
1169 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001170 int qdep;
1171
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001172 qdep = dev_data->ats.qdep;
1173 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001174
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001175 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001176
1177 return iommu_queue_command(iommu, &cmd);
1178}
1179
1180/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001181 * Command send function for invalidating a device table entry
1182 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001183static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001184{
1185 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001186 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001187
Joerg Roedel6c542042011-06-09 17:07:31 +02001188 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001189
Joerg Roedelf62dda62011-06-09 12:55:35 +02001190 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001191 if (ret)
1192 return ret;
1193
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001194 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001195 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001196
1197 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001198}
1199
Joerg Roedel431b2a22008-07-11 17:14:22 +02001200/*
1201 * TLB invalidation function which is called from the mapping functions.
1202 * It invalidates a single PTE if the range to flush is within a single
1203 * page. Otherwise it flushes the whole TLB of the IOMMU.
1204 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001205static void __domain_flush_pages(struct protection_domain *domain,
1206 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001207{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001208 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001209 struct iommu_cmd cmd;
1210 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001211
Joerg Roedel11b64022011-04-06 11:49:28 +02001212 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001213
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001214 for (i = 0; i < amd_iommus_present; ++i) {
1215 if (!domain->dev_iommu[i])
1216 continue;
1217
1218 /*
1219 * Devices of this domain are behind this IOMMU
1220 * We need a TLB flush
1221 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001222 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001223 }
1224
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001225 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001226
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001227 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001228 continue;
1229
Joerg Roedel6c542042011-06-09 17:07:31 +02001230 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001231 }
1232
Joerg Roedel11b64022011-04-06 11:49:28 +02001233 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001234}
1235
Joerg Roedel17b124b2011-04-06 18:01:35 +02001236static void domain_flush_pages(struct protection_domain *domain,
1237 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001238{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001239 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001240}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001241
Joerg Roedel1c655772008-09-04 18:40:05 +02001242/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001243static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001244{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001245 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001246}
1247
Chris Wright42a49f92009-06-15 15:42:00 +02001248/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001249static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001250{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001251 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1252}
1253
1254static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001255{
1256 int i;
1257
1258 for (i = 0; i < amd_iommus_present; ++i) {
1259 if (!domain->dev_iommu[i])
1260 continue;
1261
1262 /*
1263 * Devices of this domain are behind this IOMMU
1264 * We need to wait for completion of all commands.
1265 */
1266 iommu_completion_wait(amd_iommus[i]);
1267 }
1268}
1269
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001270
Joerg Roedel43f49602008-12-02 21:01:12 +01001271/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001272 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001273 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001274static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001275{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001276 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001277
1278 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001279 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001280}
1281
Joerg Roedel431b2a22008-07-11 17:14:22 +02001282/****************************************************************************
1283 *
1284 * The functions below are used the create the page table mappings for
1285 * unity mapped regions.
1286 *
1287 ****************************************************************************/
1288
1289/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001290 * This function is used to add another level to an IO page table. Adding
1291 * another level increases the size of the address space by 9 bits to a size up
1292 * to 64 bits.
1293 */
1294static bool increase_address_space(struct protection_domain *domain,
1295 gfp_t gfp)
1296{
1297 u64 *pte;
1298
1299 if (domain->mode == PAGE_MODE_6_LEVEL)
1300 /* address space already 64 bit large */
1301 return false;
1302
1303 pte = (void *)get_zeroed_page(gfp);
1304 if (!pte)
1305 return false;
1306
1307 *pte = PM_LEVEL_PDE(domain->mode,
1308 virt_to_phys(domain->pt_root));
1309 domain->pt_root = pte;
1310 domain->mode += 1;
1311 domain->updated = true;
1312
1313 return true;
1314}
1315
1316static u64 *alloc_pte(struct protection_domain *domain,
1317 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001318 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001319 u64 **pte_page,
1320 gfp_t gfp)
1321{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001322 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001323 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001324
1325 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001326
1327 while (address > PM_LEVEL_SIZE(domain->mode))
1328 increase_address_space(domain, gfp);
1329
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001330 level = domain->mode - 1;
1331 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1332 address = PAGE_SIZE_ALIGN(address, page_size);
1333 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001334
1335 while (level > end_lvl) {
1336 if (!IOMMU_PTE_PRESENT(*pte)) {
1337 page = (u64 *)get_zeroed_page(gfp);
1338 if (!page)
1339 return NULL;
1340 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1341 }
1342
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001343 /* No level skipping support yet */
1344 if (PM_PTE_LEVEL(*pte) != level)
1345 return NULL;
1346
Joerg Roedel308973d2009-11-24 17:43:32 +01001347 level -= 1;
1348
1349 pte = IOMMU_PTE_PAGE(*pte);
1350
1351 if (pte_page && level == end_lvl)
1352 *pte_page = pte;
1353
1354 pte = &pte[PM_LEVEL_INDEX(level, address)];
1355 }
1356
1357 return pte;
1358}
1359
1360/*
1361 * This function checks if there is a PTE for a given dma address. If
1362 * there is one, it returns the pointer to it.
1363 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001364static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001365{
1366 int level;
1367 u64 *pte;
1368
Joerg Roedel24cd7722010-01-19 17:27:39 +01001369 if (address > PM_LEVEL_SIZE(domain->mode))
1370 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001371
Joerg Roedel24cd7722010-01-19 17:27:39 +01001372 level = domain->mode - 1;
1373 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1374
1375 while (level > 0) {
1376
1377 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001378 if (!IOMMU_PTE_PRESENT(*pte))
1379 return NULL;
1380
Joerg Roedel24cd7722010-01-19 17:27:39 +01001381 /* Large PTE */
1382 if (PM_PTE_LEVEL(*pte) == 0x07) {
1383 unsigned long pte_mask, __pte;
1384
1385 /*
1386 * If we have a series of large PTEs, make
1387 * sure to return a pointer to the first one.
1388 */
1389 pte_mask = PTE_PAGE_SIZE(*pte);
1390 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1391 __pte = ((unsigned long)pte) & pte_mask;
1392
1393 return (u64 *)__pte;
1394 }
1395
1396 /* No level skipping support yet */
1397 if (PM_PTE_LEVEL(*pte) != level)
1398 return NULL;
1399
Joerg Roedel308973d2009-11-24 17:43:32 +01001400 level -= 1;
1401
Joerg Roedel24cd7722010-01-19 17:27:39 +01001402 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001403 pte = IOMMU_PTE_PAGE(*pte);
1404 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001405 }
1406
1407 return pte;
1408}
1409
1410/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001411 * Generic mapping functions. It maps a physical address into a DMA
1412 * address space. It allocates the page table pages if necessary.
1413 * In the future it can be extended to a generic mapping function
1414 * supporting all features of AMD IOMMU page tables like level skipping
1415 * and full 64 bit address spaces.
1416 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001417static int iommu_map_page(struct protection_domain *dom,
1418 unsigned long bus_addr,
1419 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001420 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001421 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001422{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001423 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001424 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001425
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001426 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001427 return -EINVAL;
1428
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001429 bus_addr = PAGE_ALIGN(bus_addr);
1430 phys_addr = PAGE_ALIGN(phys_addr);
1431 count = PAGE_SIZE_PTE_COUNT(page_size);
1432 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001433
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001434 for (i = 0; i < count; ++i)
1435 if (IOMMU_PTE_PRESENT(pte[i]))
1436 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001437
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001438 if (page_size > PAGE_SIZE) {
1439 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1440 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1441 } else
1442 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1443
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001444 if (prot & IOMMU_PROT_IR)
1445 __pte |= IOMMU_PTE_IR;
1446 if (prot & IOMMU_PROT_IW)
1447 __pte |= IOMMU_PTE_IW;
1448
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001449 for (i = 0; i < count; ++i)
1450 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001451
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001452 update_domain(dom);
1453
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001454 return 0;
1455}
1456
Joerg Roedel24cd7722010-01-19 17:27:39 +01001457static unsigned long iommu_unmap_page(struct protection_domain *dom,
1458 unsigned long bus_addr,
1459 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001460{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001461 unsigned long long unmap_size, unmapped;
1462 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001463
Joerg Roedel24cd7722010-01-19 17:27:39 +01001464 BUG_ON(!is_power_of_2(page_size));
1465
1466 unmapped = 0;
1467
1468 while (unmapped < page_size) {
1469
1470 pte = fetch_pte(dom, bus_addr);
1471
1472 if (!pte) {
1473 /*
1474 * No PTE for this address
1475 * move forward in 4kb steps
1476 */
1477 unmap_size = PAGE_SIZE;
1478 } else if (PM_PTE_LEVEL(*pte) == 0) {
1479 /* 4kb PTE found for this address */
1480 unmap_size = PAGE_SIZE;
1481 *pte = 0ULL;
1482 } else {
1483 int count, i;
1484
1485 /* Large PTE found which maps this address */
1486 unmap_size = PTE_PAGE_SIZE(*pte);
Alex Williamson60d0ca32013-06-21 14:33:19 -06001487
1488 /* Only unmap from the first pte in the page */
1489 if ((unmap_size - 1) & bus_addr)
1490 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001491 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1492 for (i = 0; i < count; i++)
1493 pte[i] = 0ULL;
1494 }
1495
1496 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1497 unmapped += unmap_size;
1498 }
1499
Alex Williamson60d0ca32013-06-21 14:33:19 -06001500 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001501
1502 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001503}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001504
Joerg Roedel431b2a22008-07-11 17:14:22 +02001505/*
1506 * This function checks if a specific unity mapping entry is needed for
1507 * this specific IOMMU.
1508 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001509static int iommu_for_unity_map(struct amd_iommu *iommu,
1510 struct unity_map_entry *entry)
1511{
1512 u16 bdf, i;
1513
1514 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1515 bdf = amd_iommu_alias_table[i];
1516 if (amd_iommu_rlookup_table[bdf] == iommu)
1517 return 1;
1518 }
1519
1520 return 0;
1521}
1522
Joerg Roedel431b2a22008-07-11 17:14:22 +02001523/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001524 * This function actually applies the mapping to the page table of the
1525 * dma_ops domain.
1526 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001527static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1528 struct unity_map_entry *e)
1529{
1530 u64 addr;
1531 int ret;
1532
1533 for (addr = e->address_start; addr < e->address_end;
1534 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001535 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001536 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001537 if (ret)
1538 return ret;
1539 /*
1540 * if unity mapping is in aperture range mark the page
1541 * as allocated in the aperture
1542 */
1543 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001544 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001545 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001546 }
1547
1548 return 0;
1549}
1550
Joerg Roedel431b2a22008-07-11 17:14:22 +02001551/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001552 * Init the unity mappings for a specific IOMMU in the system
1553 *
1554 * Basically iterates over all unity mapping entries and applies them to
1555 * the default domain DMA of that IOMMU if necessary.
1556 */
1557static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1558{
1559 struct unity_map_entry *entry;
1560 int ret;
1561
1562 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1563 if (!iommu_for_unity_map(iommu, entry))
1564 continue;
1565 ret = dma_ops_unity_map(iommu->default_dom, entry);
1566 if (ret)
1567 return ret;
1568 }
1569
1570 return 0;
1571}
1572
1573/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001574 * Inits the unity mappings required for a specific device
1575 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001576static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1577 u16 devid)
1578{
1579 struct unity_map_entry *e;
1580 int ret;
1581
1582 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1583 if (!(devid >= e->devid_start && devid <= e->devid_end))
1584 continue;
1585 ret = dma_ops_unity_map(dma_dom, e);
1586 if (ret)
1587 return ret;
1588 }
1589
1590 return 0;
1591}
1592
Joerg Roedel431b2a22008-07-11 17:14:22 +02001593/****************************************************************************
1594 *
1595 * The next functions belong to the address allocator for the dma_ops
1596 * interface functions. They work like the allocators in the other IOMMU
1597 * drivers. Its basically a bitmap which marks the allocated pages in
1598 * the aperture. Maybe it could be enhanced in the future to a more
1599 * efficient allocator.
1600 *
1601 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001602
Joerg Roedel431b2a22008-07-11 17:14:22 +02001603/*
Joerg Roedel384de722009-05-15 12:30:05 +02001604 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001605 *
1606 * called with domain->lock held
1607 */
Joerg Roedel384de722009-05-15 12:30:05 +02001608
Joerg Roedel9cabe892009-05-18 16:38:55 +02001609/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001610 * Used to reserve address ranges in the aperture (e.g. for exclusion
1611 * ranges.
1612 */
1613static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1614 unsigned long start_page,
1615 unsigned int pages)
1616{
1617 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1618
1619 if (start_page + pages > last_page)
1620 pages = last_page - start_page;
1621
1622 for (i = start_page; i < start_page + pages; ++i) {
1623 int index = i / APERTURE_RANGE_PAGES;
1624 int page = i % APERTURE_RANGE_PAGES;
1625 __set_bit(page, dom->aperture[index]->bitmap);
1626 }
1627}
1628
1629/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001630 * This function is used to add a new aperture range to an existing
1631 * aperture in case of dma_ops domain allocation or address allocation
1632 * failure.
1633 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001634static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001635 bool populate, gfp_t gfp)
1636{
1637 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001638 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001639 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001640
Joerg Roedelf5e97052009-05-22 12:31:53 +02001641#ifdef CONFIG_IOMMU_STRESS
1642 populate = false;
1643#endif
1644
Joerg Roedel9cabe892009-05-18 16:38:55 +02001645 if (index >= APERTURE_MAX_RANGES)
1646 return -ENOMEM;
1647
1648 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1649 if (!dma_dom->aperture[index])
1650 return -ENOMEM;
1651
1652 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1653 if (!dma_dom->aperture[index]->bitmap)
1654 goto out_free;
1655
1656 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1657
1658 if (populate) {
1659 unsigned long address = dma_dom->aperture_size;
1660 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1661 u64 *pte, *pte_page;
1662
1663 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001664 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001665 &pte_page, gfp);
1666 if (!pte)
1667 goto out_free;
1668
1669 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1670
1671 address += APERTURE_RANGE_SIZE / 64;
1672 }
1673 }
1674
Joerg Roedel17f5b562011-07-06 17:14:44 +02001675 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001676 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1677
Joerg Roedel17f5b562011-07-06 17:14:44 +02001678 /* Reserve address range used for MSI messages */
1679 if (old_size < MSI_ADDR_BASE_LO &&
1680 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1681 unsigned long spage;
1682 int pages;
1683
1684 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1685 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1686
1687 dma_ops_reserve_addresses(dma_dom, spage, pages);
1688 }
1689
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001690 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001691 for_each_iommu(iommu) {
1692 if (iommu->exclusion_start &&
1693 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1694 && iommu->exclusion_start < dma_dom->aperture_size) {
1695 unsigned long startpage;
1696 int pages = iommu_num_pages(iommu->exclusion_start,
1697 iommu->exclusion_length,
1698 PAGE_SIZE);
1699 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1700 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1701 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001702 }
1703
1704 /*
1705 * Check for areas already mapped as present in the new aperture
1706 * range and mark those pages as reserved in the allocator. Such
1707 * mappings may already exist as a result of requested unity
1708 * mappings for devices.
1709 */
1710 for (i = dma_dom->aperture[index]->offset;
1711 i < dma_dom->aperture_size;
1712 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001713 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001714 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1715 continue;
1716
Joerg Roedelfcd08612011-10-11 17:41:32 +02001717 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001718 }
1719
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001720 update_domain(&dma_dom->domain);
1721
Joerg Roedel9cabe892009-05-18 16:38:55 +02001722 return 0;
1723
1724out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001725 update_domain(&dma_dom->domain);
1726
Joerg Roedel9cabe892009-05-18 16:38:55 +02001727 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1728
1729 kfree(dma_dom->aperture[index]);
1730 dma_dom->aperture[index] = NULL;
1731
1732 return -ENOMEM;
1733}
1734
Joerg Roedel384de722009-05-15 12:30:05 +02001735static unsigned long dma_ops_area_alloc(struct device *dev,
1736 struct dma_ops_domain *dom,
1737 unsigned int pages,
1738 unsigned long align_mask,
1739 u64 dma_mask,
1740 unsigned long start)
1741{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001742 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001743 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1744 int i = start >> APERTURE_RANGE_SHIFT;
1745 unsigned long boundary_size;
1746 unsigned long address = -1;
1747 unsigned long limit;
1748
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001749 next_bit >>= PAGE_SHIFT;
1750
Joerg Roedel384de722009-05-15 12:30:05 +02001751 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1752 PAGE_SIZE) >> PAGE_SHIFT;
1753
1754 for (;i < max_index; ++i) {
1755 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1756
1757 if (dom->aperture[i]->offset >= dma_mask)
1758 break;
1759
1760 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1761 dma_mask >> PAGE_SHIFT);
1762
1763 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1764 limit, next_bit, pages, 0,
1765 boundary_size, align_mask);
1766 if (address != -1) {
1767 address = dom->aperture[i]->offset +
1768 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001769 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001770 break;
1771 }
1772
1773 next_bit = 0;
1774 }
1775
1776 return address;
1777}
1778
Joerg Roedeld3086442008-06-26 21:27:57 +02001779static unsigned long dma_ops_alloc_addresses(struct device *dev,
1780 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001781 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001782 unsigned long align_mask,
1783 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001784{
Joerg Roedeld3086442008-06-26 21:27:57 +02001785 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001786
Joerg Roedelfe16f082009-05-22 12:27:53 +02001787#ifdef CONFIG_IOMMU_STRESS
1788 dom->next_address = 0;
1789 dom->need_flush = true;
1790#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001791
Joerg Roedel384de722009-05-15 12:30:05 +02001792 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001793 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001794
Joerg Roedel1c655772008-09-04 18:40:05 +02001795 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001796 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001797 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1798 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001799 dom->need_flush = true;
1800 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001801
Joerg Roedel384de722009-05-15 12:30:05 +02001802 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001803 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001804
1805 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1806
1807 return address;
1808}
1809
Joerg Roedel431b2a22008-07-11 17:14:22 +02001810/*
1811 * The address free function.
1812 *
1813 * called with domain->lock held
1814 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001815static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1816 unsigned long address,
1817 unsigned int pages)
1818{
Joerg Roedel384de722009-05-15 12:30:05 +02001819 unsigned i = address >> APERTURE_RANGE_SHIFT;
1820 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001821
Joerg Roedel384de722009-05-15 12:30:05 +02001822 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1823
Joerg Roedel47bccd62009-05-22 12:40:54 +02001824#ifdef CONFIG_IOMMU_STRESS
1825 if (i < 4)
1826 return;
1827#endif
1828
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001829 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001830 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001831
1832 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001833
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001834 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001835
Joerg Roedeld3086442008-06-26 21:27:57 +02001836}
1837
Joerg Roedel431b2a22008-07-11 17:14:22 +02001838/****************************************************************************
1839 *
1840 * The next functions belong to the domain allocation. A domain is
1841 * allocated for every IOMMU as the default domain. If device isolation
1842 * is enabled, every device get its own domain. The most important thing
1843 * about domains is the page table mapping the DMA address space they
1844 * contain.
1845 *
1846 ****************************************************************************/
1847
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001848/*
1849 * This function adds a protection domain to the global protection domain list
1850 */
1851static void add_domain_to_list(struct protection_domain *domain)
1852{
1853 unsigned long flags;
1854
1855 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1856 list_add(&domain->list, &amd_iommu_pd_list);
1857 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1858}
1859
1860/*
1861 * This function removes a protection domain to the global
1862 * protection domain list
1863 */
1864static void del_domain_from_list(struct protection_domain *domain)
1865{
1866 unsigned long flags;
1867
1868 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1869 list_del(&domain->list);
1870 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1871}
1872
Joerg Roedelec487d12008-06-26 21:27:58 +02001873static u16 domain_id_alloc(void)
1874{
1875 unsigned long flags;
1876 int id;
1877
1878 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1879 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1880 BUG_ON(id == 0);
1881 if (id > 0 && id < MAX_DOMAIN_ID)
1882 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1883 else
1884 id = 0;
1885 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1886
1887 return id;
1888}
1889
Joerg Roedela2acfb72008-12-02 18:28:53 +01001890static void domain_id_free(int id)
1891{
1892 unsigned long flags;
1893
1894 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1895 if (id > 0 && id < MAX_DOMAIN_ID)
1896 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1897 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1898}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001899
Joerg Roedel5c34c402013-06-20 20:22:58 +02001900#define DEFINE_FREE_PT_FN(LVL, FN) \
1901static void free_pt_##LVL (unsigned long __pt) \
1902{ \
1903 unsigned long p; \
1904 u64 *pt; \
1905 int i; \
1906 \
1907 pt = (u64 *)__pt; \
1908 \
1909 for (i = 0; i < 512; ++i) { \
1910 if (!IOMMU_PTE_PRESENT(pt[i])) \
1911 continue; \
1912 \
1913 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1914 FN(p); \
1915 } \
1916 free_page((unsigned long)pt); \
1917}
1918
1919DEFINE_FREE_PT_FN(l2, free_page)
1920DEFINE_FREE_PT_FN(l3, free_pt_l2)
1921DEFINE_FREE_PT_FN(l4, free_pt_l3)
1922DEFINE_FREE_PT_FN(l5, free_pt_l4)
1923DEFINE_FREE_PT_FN(l6, free_pt_l5)
1924
Joerg Roedel86db2e52008-12-02 18:20:21 +01001925static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001926{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001927 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001928
Joerg Roedel5c34c402013-06-20 20:22:58 +02001929 switch (domain->mode) {
1930 case PAGE_MODE_NONE:
1931 break;
1932 case PAGE_MODE_1_LEVEL:
1933 free_page(root);
1934 break;
1935 case PAGE_MODE_2_LEVEL:
1936 free_pt_l2(root);
1937 break;
1938 case PAGE_MODE_3_LEVEL:
1939 free_pt_l3(root);
1940 break;
1941 case PAGE_MODE_4_LEVEL:
1942 free_pt_l4(root);
1943 break;
1944 case PAGE_MODE_5_LEVEL:
1945 free_pt_l5(root);
1946 break;
1947 case PAGE_MODE_6_LEVEL:
1948 free_pt_l6(root);
1949 break;
1950 default:
1951 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001952 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001953}
1954
Joerg Roedelb16137b2011-11-21 16:50:23 +01001955static void free_gcr3_tbl_level1(u64 *tbl)
1956{
1957 u64 *ptr;
1958 int i;
1959
1960 for (i = 0; i < 512; ++i) {
1961 if (!(tbl[i] & GCR3_VALID))
1962 continue;
1963
1964 ptr = __va(tbl[i] & PAGE_MASK);
1965
1966 free_page((unsigned long)ptr);
1967 }
1968}
1969
1970static void free_gcr3_tbl_level2(u64 *tbl)
1971{
1972 u64 *ptr;
1973 int i;
1974
1975 for (i = 0; i < 512; ++i) {
1976 if (!(tbl[i] & GCR3_VALID))
1977 continue;
1978
1979 ptr = __va(tbl[i] & PAGE_MASK);
1980
1981 free_gcr3_tbl_level1(ptr);
1982 }
1983}
1984
Joerg Roedel52815b72011-11-17 17:24:28 +01001985static void free_gcr3_table(struct protection_domain *domain)
1986{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001987 if (domain->glx == 2)
1988 free_gcr3_tbl_level2(domain->gcr3_tbl);
1989 else if (domain->glx == 1)
1990 free_gcr3_tbl_level1(domain->gcr3_tbl);
1991 else if (domain->glx != 0)
1992 BUG();
1993
Joerg Roedel52815b72011-11-17 17:24:28 +01001994 free_page((unsigned long)domain->gcr3_tbl);
1995}
1996
Joerg Roedel431b2a22008-07-11 17:14:22 +02001997/*
1998 * Free a domain, only used if something went wrong in the
1999 * allocation path and we need to free an already allocated page table
2000 */
Joerg Roedelec487d12008-06-26 21:27:58 +02002001static void dma_ops_domain_free(struct dma_ops_domain *dom)
2002{
Joerg Roedel384de722009-05-15 12:30:05 +02002003 int i;
2004
Joerg Roedelec487d12008-06-26 21:27:58 +02002005 if (!dom)
2006 return;
2007
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002008 del_domain_from_list(&dom->domain);
2009
Joerg Roedel86db2e52008-12-02 18:20:21 +01002010 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02002011
Joerg Roedel384de722009-05-15 12:30:05 +02002012 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
2013 if (!dom->aperture[i])
2014 continue;
2015 free_page((unsigned long)dom->aperture[i]->bitmap);
2016 kfree(dom->aperture[i]);
2017 }
Joerg Roedelec487d12008-06-26 21:27:58 +02002018
2019 kfree(dom);
2020}
2021
Joerg Roedel431b2a22008-07-11 17:14:22 +02002022/*
2023 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002024 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02002025 * structures required for the dma_ops interface
2026 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01002027static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02002028{
2029 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002030
2031 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
2032 if (!dma_dom)
2033 return NULL;
2034
2035 spin_lock_init(&dma_dom->domain.lock);
2036
2037 dma_dom->domain.id = domain_id_alloc();
2038 if (dma_dom->domain.id == 0)
2039 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002040 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002041 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002042 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002043 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002044 dma_dom->domain.priv = dma_dom;
2045 if (!dma_dom->domain.pt_root)
2046 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002047
Joerg Roedel1c655772008-09-04 18:40:05 +02002048 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002049 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002050
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002051 add_domain_to_list(&dma_dom->domain);
2052
Joerg Roedel576175c2009-11-23 19:08:46 +01002053 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002054 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002055
Joerg Roedel431b2a22008-07-11 17:14:22 +02002056 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002057 * mark the first page as allocated so we never return 0 as
2058 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002059 */
Joerg Roedel384de722009-05-15 12:30:05 +02002060 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02002061 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002062
Joerg Roedelec487d12008-06-26 21:27:58 +02002063
2064 return dma_dom;
2065
2066free_dma_dom:
2067 dma_ops_domain_free(dma_dom);
2068
2069 return NULL;
2070}
2071
Joerg Roedel431b2a22008-07-11 17:14:22 +02002072/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002073 * little helper function to check whether a given protection domain is a
2074 * dma_ops domain
2075 */
2076static bool dma_ops_domain(struct protection_domain *domain)
2077{
2078 return domain->flags & PD_DMA_OPS_MASK;
2079}
2080
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002081static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002082{
Joerg Roedel132bd682011-11-17 14:18:46 +01002083 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002084 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002085
Joerg Roedel132bd682011-11-17 14:18:46 +01002086 if (domain->mode != PAGE_MODE_NONE)
2087 pte_root = virt_to_phys(domain->pt_root);
2088
Joerg Roedel38ddf412008-09-11 10:38:32 +02002089 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2090 << DEV_ENTRY_MODE_SHIFT;
2091 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002092
Joerg Roedelee6c2862011-11-09 12:06:03 +01002093 flags = amd_iommu_dev_table[devid].data[1];
2094
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002095 if (ats)
2096 flags |= DTE_FLAG_IOTLB;
2097
Joerg Roedel52815b72011-11-17 17:24:28 +01002098 if (domain->flags & PD_IOMMUV2_MASK) {
2099 u64 gcr3 = __pa(domain->gcr3_tbl);
2100 u64 glx = domain->glx;
2101 u64 tmp;
2102
2103 pte_root |= DTE_FLAG_GV;
2104 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2105
2106 /* First mask out possible old values for GCR3 table */
2107 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2108 flags &= ~tmp;
2109
2110 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2111 flags &= ~tmp;
2112
2113 /* Encode GCR3 table into DTE */
2114 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2115 pte_root |= tmp;
2116
2117 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2118 flags |= tmp;
2119
2120 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2121 flags |= tmp;
2122 }
2123
Joerg Roedelee6c2862011-11-09 12:06:03 +01002124 flags &= ~(0xffffUL);
2125 flags |= domain->id;
2126
2127 amd_iommu_dev_table[devid].data[1] = flags;
2128 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002129}
2130
Joerg Roedel15898bb2009-11-24 15:39:42 +01002131static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002132{
Joerg Roedel355bf552008-12-08 12:02:41 +01002133 /* remove entry from the device table seen by the hardware */
2134 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2135 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002136
Joerg Roedelc5cca142009-10-09 18:31:20 +02002137 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002138}
2139
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002140static void do_attach(struct iommu_dev_data *dev_data,
2141 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002142{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002143 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002144 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002145
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002146 iommu = amd_iommu_rlookup_table[dev_data->devid];
2147 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002148
2149 /* Update data structures */
2150 dev_data->domain = domain;
2151 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002152 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002153
2154 /* Do reference counting */
2155 domain->dev_iommu[iommu->index] += 1;
2156 domain->dev_cnt += 1;
2157
2158 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002159 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002160}
2161
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002162static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002163{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002164 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002165
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002166 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002167
Joerg Roedelc4596112009-11-20 14:57:32 +01002168 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002169 dev_data->domain->dev_iommu[iommu->index] -= 1;
2170 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002171
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002172 /* Update data structures */
2173 dev_data->domain = NULL;
2174 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002175 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002176
2177 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002178 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002179}
2180
2181/*
2182 * If a device is not yet associated with a domain, this function does
2183 * assigns it visible for the hardware
2184 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002185static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002186 struct protection_domain *domain)
2187{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002188 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002189
Joerg Roedel15898bb2009-11-24 15:39:42 +01002190 /* lock domain */
2191 spin_lock(&domain->lock);
2192
Joerg Roedel71f77582011-06-09 19:03:15 +02002193 if (dev_data->alias_data != NULL) {
2194 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002195
Joerg Roedel2b02b092011-06-09 17:48:39 +02002196 /* Some sanity checks */
2197 ret = -EBUSY;
2198 if (alias_data->domain != NULL &&
2199 alias_data->domain != domain)
2200 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002201
Joerg Roedel2b02b092011-06-09 17:48:39 +02002202 if (dev_data->domain != NULL &&
2203 dev_data->domain != domain)
2204 goto out_unlock;
2205
2206 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002207 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002208 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002209
2210 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002211 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002212
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002213 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002214 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002215
Joerg Roedel24100052009-11-25 15:59:57 +01002216 atomic_inc(&dev_data->bind);
2217
Julia Lawall84fe6c12010-05-27 12:31:51 +02002218 ret = 0;
2219
2220out_unlock:
2221
Joerg Roedel355bf552008-12-08 12:02:41 +01002222 /* ready */
2223 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002224
Julia Lawall84fe6c12010-05-27 12:31:51 +02002225 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002226}
2227
Joerg Roedel52815b72011-11-17 17:24:28 +01002228
2229static void pdev_iommuv2_disable(struct pci_dev *pdev)
2230{
2231 pci_disable_ats(pdev);
2232 pci_disable_pri(pdev);
2233 pci_disable_pasid(pdev);
2234}
2235
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002236/* FIXME: Change generic reset-function to do the same */
2237static int pri_reset_while_enabled(struct pci_dev *pdev)
2238{
2239 u16 control;
2240 int pos;
2241
Joerg Roedel46277b72011-12-07 14:34:02 +01002242 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002243 if (!pos)
2244 return -EINVAL;
2245
Joerg Roedel46277b72011-12-07 14:34:02 +01002246 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2247 control |= PCI_PRI_CTRL_RESET;
2248 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002249
2250 return 0;
2251}
2252
Joerg Roedel52815b72011-11-17 17:24:28 +01002253static int pdev_iommuv2_enable(struct pci_dev *pdev)
2254{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002255 bool reset_enable;
2256 int reqs, ret;
2257
2258 /* FIXME: Hardcode number of outstanding requests for now */
2259 reqs = 32;
2260 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2261 reqs = 1;
2262 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002263
2264 /* Only allow access to user-accessible pages */
2265 ret = pci_enable_pasid(pdev, 0);
2266 if (ret)
2267 goto out_err;
2268
2269 /* First reset the PRI state of the device */
2270 ret = pci_reset_pri(pdev);
2271 if (ret)
2272 goto out_err;
2273
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002274 /* Enable PRI */
2275 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002276 if (ret)
2277 goto out_err;
2278
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002279 if (reset_enable) {
2280 ret = pri_reset_while_enabled(pdev);
2281 if (ret)
2282 goto out_err;
2283 }
2284
Joerg Roedel52815b72011-11-17 17:24:28 +01002285 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2286 if (ret)
2287 goto out_err;
2288
2289 return 0;
2290
2291out_err:
2292 pci_disable_pri(pdev);
2293 pci_disable_pasid(pdev);
2294
2295 return ret;
2296}
2297
Joerg Roedelc99afa22011-11-21 18:19:25 +01002298/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002299#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002300
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002301static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002302{
Joerg Roedela3b93122012-04-12 12:49:26 +02002303 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002304 int pos;
2305
Joerg Roedel46277b72011-12-07 14:34:02 +01002306 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002307 if (!pos)
2308 return false;
2309
Joerg Roedela3b93122012-04-12 12:49:26 +02002310 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002311
Joerg Roedela3b93122012-04-12 12:49:26 +02002312 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002313}
2314
Joerg Roedel15898bb2009-11-24 15:39:42 +01002315/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002316 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002317 * assigns it visible for the hardware
2318 */
2319static int attach_device(struct device *dev,
2320 struct protection_domain *domain)
2321{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002322 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002323 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002324 unsigned long flags;
2325 int ret;
2326
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002327 dev_data = get_dev_data(dev);
2328
Joerg Roedel52815b72011-11-17 17:24:28 +01002329 if (domain->flags & PD_IOMMUV2_MASK) {
2330 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2331 return -EINVAL;
2332
2333 if (pdev_iommuv2_enable(pdev) != 0)
2334 return -EINVAL;
2335
2336 dev_data->ats.enabled = true;
2337 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002338 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002339 } else if (amd_iommu_iotlb_sup &&
2340 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002341 dev_data->ats.enabled = true;
2342 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2343 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002344
Joerg Roedel15898bb2009-11-24 15:39:42 +01002345 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002346 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002347 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2348
2349 /*
2350 * We might boot into a crash-kernel here. The crashed kernel
2351 * left the caches in the IOMMU dirty. So we have to flush
2352 * here to evict all dirty stuff.
2353 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002354 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002355
2356 return ret;
2357}
2358
2359/*
2360 * Removes a device from a protection domain (unlocked)
2361 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002362static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002363{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002364 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002365 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002366
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002367 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002368
Joerg Roedel2ca76272010-01-22 16:45:31 +01002369 domain = dev_data->domain;
2370
2371 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002372
Joerg Roedel71f77582011-06-09 19:03:15 +02002373 if (dev_data->alias_data != NULL) {
2374 struct iommu_dev_data *alias_data = dev_data->alias_data;
2375
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002376 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002377 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002378 }
2379
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002380 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002381 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002382
Joerg Roedel2ca76272010-01-22 16:45:31 +01002383 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002384
Joerg Roedel21129f72009-09-01 11:59:42 +02002385 /*
2386 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002387 * passthrough domain if it is detached from any other domain.
2388 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002389 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002390 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002391 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002392 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002393}
2394
2395/*
2396 * Removes a device from a protection domain (with devtable_lock held)
2397 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002398static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002399{
Joerg Roedel52815b72011-11-17 17:24:28 +01002400 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002401 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002402 unsigned long flags;
2403
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002404 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002405 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002406
Joerg Roedel355bf552008-12-08 12:02:41 +01002407 /* lock device table */
2408 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002409 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002410 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002411
Joerg Roedel52815b72011-11-17 17:24:28 +01002412 if (domain->flags & PD_IOMMUV2_MASK)
2413 pdev_iommuv2_disable(to_pci_dev(dev));
2414 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002415 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002416
2417 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002418}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002419
Joerg Roedel15898bb2009-11-24 15:39:42 +01002420/*
2421 * Find out the protection domain structure for a given PCI device. This
2422 * will give us the pointer to the page table root for example.
2423 */
2424static struct protection_domain *domain_for_device(struct device *dev)
2425{
Joerg Roedel71f77582011-06-09 19:03:15 +02002426 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002427 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002428 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002429
Joerg Roedel657cbb62009-11-23 15:26:46 +01002430 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002431
Joerg Roedel2b02b092011-06-09 17:48:39 +02002432 if (dev_data->domain)
2433 return dev_data->domain;
2434
Joerg Roedel71f77582011-06-09 19:03:15 +02002435 if (dev_data->alias_data != NULL) {
2436 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002437
2438 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2439 if (alias_data->domain != NULL) {
2440 __attach_device(dev_data, alias_data->domain);
2441 dom = alias_data->domain;
2442 }
2443 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002444 }
2445
Joerg Roedel15898bb2009-11-24 15:39:42 +01002446 return dom;
2447}
2448
Joerg Roedele275a2a2008-12-10 18:27:25 +01002449static int device_change_notifier(struct notifier_block *nb,
2450 unsigned long action, void *data)
2451{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002452 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002453 struct protection_domain *domain;
2454 struct iommu_dev_data *dev_data;
2455 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002456 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002457 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002458 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002459
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002460 if (!check_device(dev))
2461 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002462
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002463 devid = get_device_id(dev);
2464 iommu = amd_iommu_rlookup_table[devid];
2465 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002466
2467 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002468 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002469
2470 domain = domain_for_device(dev);
2471
Joerg Roedele275a2a2008-12-10 18:27:25 +01002472 if (!domain)
2473 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002474 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002475 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002476 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002477 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002478 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002479
2480 iommu_init_device(dev);
2481
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002482 /*
2483 * dev_data is still NULL and
2484 * got initialized in iommu_init_device
2485 */
2486 dev_data = get_dev_data(dev);
2487
2488 if (iommu_pass_through || dev_data->iommu_v2) {
2489 dev_data->passthrough = true;
2490 attach_device(dev, pt_domain);
2491 break;
2492 }
2493
Joerg Roedel657cbb62009-11-23 15:26:46 +01002494 domain = domain_for_device(dev);
2495
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002496 /* allocate a protection domain if a device is added */
2497 dma_domain = find_protection_domain(devid);
Joerg Roedelc2a28762013-03-26 22:48:23 +01002498 if (!dma_domain) {
2499 dma_domain = dma_ops_domain_alloc();
2500 if (!dma_domain)
2501 goto out;
2502 dma_domain->target_dev = devid;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002503
Joerg Roedelc2a28762013-03-26 22:48:23 +01002504 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2505 list_add_tail(&dma_domain->list, &iommu_pd_list);
2506 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2507 }
Joerg Roedelac1534a2012-06-21 14:52:40 +02002508
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002509 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002510
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002511 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002512 case BUS_NOTIFY_DEL_DEVICE:
2513
2514 iommu_uninit_device(dev);
2515
Joerg Roedele275a2a2008-12-10 18:27:25 +01002516 default:
2517 goto out;
2518 }
2519
Joerg Roedele275a2a2008-12-10 18:27:25 +01002520 iommu_completion_wait(iommu);
2521
2522out:
2523 return 0;
2524}
2525
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302526static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002527 .notifier_call = device_change_notifier,
2528};
Joerg Roedel355bf552008-12-08 12:02:41 +01002529
Joerg Roedel8638c492009-12-10 11:12:25 +01002530void amd_iommu_init_notifier(void)
2531{
2532 bus_register_notifier(&pci_bus_type, &device_nb);
2533}
2534
Joerg Roedel431b2a22008-07-11 17:14:22 +02002535/*****************************************************************************
2536 *
2537 * The next functions belong to the dma_ops mapping/unmapping code.
2538 *
2539 *****************************************************************************/
2540
2541/*
2542 * In the dma_ops path we only have the struct device. This function
2543 * finds the corresponding IOMMU, the protection domain and the
2544 * requestor id for a given device.
2545 * If the device is not yet associated with a domain this is also done
2546 * in this function.
2547 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002548static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002549{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002550 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002551 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002552 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002553
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002554 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002555 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002556
Joerg Roedel94f6d192009-11-24 16:40:02 +01002557 domain = domain_for_device(dev);
2558 if (domain != NULL && !dma_ops_domain(domain))
2559 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002560
Joerg Roedel94f6d192009-11-24 16:40:02 +01002561 if (domain != NULL)
2562 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002563
Frank Arnolddf805ab2012-08-27 19:21:04 +02002564 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002565 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002566 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002567 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2568 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002569 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002570 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002571
Joerg Roedel94f6d192009-11-24 16:40:02 +01002572 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002573}
2574
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002575static void update_device_table(struct protection_domain *domain)
2576{
Joerg Roedel492667d2009-11-27 13:25:47 +01002577 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002578
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002579 list_for_each_entry(dev_data, &domain->dev_list, list)
2580 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002581}
2582
2583static void update_domain(struct protection_domain *domain)
2584{
2585 if (!domain->updated)
2586 return;
2587
2588 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002589
2590 domain_flush_devices(domain);
2591 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002592
2593 domain->updated = false;
2594}
2595
Joerg Roedel431b2a22008-07-11 17:14:22 +02002596/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002597 * This function fetches the PTE for a given address in the aperture
2598 */
2599static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2600 unsigned long address)
2601{
Joerg Roedel384de722009-05-15 12:30:05 +02002602 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002603 u64 *pte, *pte_page;
2604
Joerg Roedel384de722009-05-15 12:30:05 +02002605 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2606 if (!aperture)
2607 return NULL;
2608
2609 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002610 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002611 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002612 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002613 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2614 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002615 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002616
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002617 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002618
2619 return pte;
2620}
2621
2622/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002623 * This is the generic map function. It maps one 4kb page at paddr to
2624 * the given address in the DMA address space for the domain.
2625 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002626static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002627 unsigned long address,
2628 phys_addr_t paddr,
2629 int direction)
2630{
2631 u64 *pte, __pte;
2632
2633 WARN_ON(address > dom->aperture_size);
2634
2635 paddr &= PAGE_MASK;
2636
Joerg Roedel8bda3092009-05-12 12:02:46 +02002637 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002638 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002639 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002640
2641 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2642
2643 if (direction == DMA_TO_DEVICE)
2644 __pte |= IOMMU_PTE_IR;
2645 else if (direction == DMA_FROM_DEVICE)
2646 __pte |= IOMMU_PTE_IW;
2647 else if (direction == DMA_BIDIRECTIONAL)
2648 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2649
2650 WARN_ON(*pte);
2651
2652 *pte = __pte;
2653
2654 return (dma_addr_t)address;
2655}
2656
Joerg Roedel431b2a22008-07-11 17:14:22 +02002657/*
2658 * The generic unmapping function for on page in the DMA address space.
2659 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002660static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002661 unsigned long address)
2662{
Joerg Roedel384de722009-05-15 12:30:05 +02002663 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002664 u64 *pte;
2665
2666 if (address >= dom->aperture_size)
2667 return;
2668
Joerg Roedel384de722009-05-15 12:30:05 +02002669 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2670 if (!aperture)
2671 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002672
Joerg Roedel384de722009-05-15 12:30:05 +02002673 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2674 if (!pte)
2675 return;
2676
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002677 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002678
2679 WARN_ON(!*pte);
2680
2681 *pte = 0ULL;
2682}
2683
Joerg Roedel431b2a22008-07-11 17:14:22 +02002684/*
2685 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002686 * contiguous memory region into DMA address space. It is used by all
2687 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002688 * Must be called with the domain lock held.
2689 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002690static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002691 struct dma_ops_domain *dma_dom,
2692 phys_addr_t paddr,
2693 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002694 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002695 bool align,
2696 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002697{
2698 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002699 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002700 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002701 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002702 int i;
2703
Joerg Roedele3c449f2008-10-15 22:02:11 -07002704 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002705 paddr &= PAGE_MASK;
2706
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002707 INC_STATS_COUNTER(total_map_requests);
2708
Joerg Roedelc1858972008-12-12 15:42:39 +01002709 if (pages > 1)
2710 INC_STATS_COUNTER(cross_page);
2711
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002712 if (align)
2713 align_mask = (1UL << get_order(size)) - 1;
2714
Joerg Roedel11b83882009-05-19 10:23:15 +02002715retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002716 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2717 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002718 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002719 /*
2720 * setting next_address here will let the address
2721 * allocator only scan the new allocated range in the
2722 * first run. This is a small optimization.
2723 */
2724 dma_dom->next_address = dma_dom->aperture_size;
2725
Joerg Roedel576175c2009-11-23 19:08:46 +01002726 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002727 goto out;
2728
2729 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002730 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002731 * allocation again
2732 */
2733 goto retry;
2734 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002735
2736 start = address;
2737 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002738 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002739 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002740 goto out_unmap;
2741
Joerg Roedelcb76c322008-06-26 21:28:00 +02002742 paddr += PAGE_SIZE;
2743 start += PAGE_SIZE;
2744 }
2745 address += offset;
2746
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002747 ADD_STATS_COUNTER(alloced_io_mem, size);
2748
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002749 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002750 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002751 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002752 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002753 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002754
Joerg Roedelcb76c322008-06-26 21:28:00 +02002755out:
2756 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002757
2758out_unmap:
2759
2760 for (--i; i >= 0; --i) {
2761 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002762 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002763 }
2764
2765 dma_ops_free_addresses(dma_dom, address, pages);
2766
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002767 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002768}
2769
Joerg Roedel431b2a22008-07-11 17:14:22 +02002770/*
2771 * Does the reverse of the __map_single function. Must be called with
2772 * the domain lock held too
2773 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002774static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002775 dma_addr_t dma_addr,
2776 size_t size,
2777 int dir)
2778{
Joerg Roedel04e04632010-09-23 16:12:48 +02002779 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002780 dma_addr_t i, start;
2781 unsigned int pages;
2782
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002783 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002784 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002785 return;
2786
Joerg Roedel04e04632010-09-23 16:12:48 +02002787 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002788 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002789 dma_addr &= PAGE_MASK;
2790 start = dma_addr;
2791
2792 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002793 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002794 start += PAGE_SIZE;
2795 }
2796
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002797 SUB_STATS_COUNTER(alloced_io_mem, size);
2798
Joerg Roedelcb76c322008-06-26 21:28:00 +02002799 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002800
Joerg Roedel80be3082008-11-06 14:59:05 +01002801 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002802 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002803 dma_dom->need_flush = false;
2804 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002805}
2806
Joerg Roedel431b2a22008-07-11 17:14:22 +02002807/*
2808 * The exported map_single function for dma_ops.
2809 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002810static dma_addr_t map_page(struct device *dev, struct page *page,
2811 unsigned long offset, size_t size,
2812 enum dma_data_direction dir,
2813 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002814{
2815 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002816 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002817 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002818 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002819 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002820
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002821 INC_STATS_COUNTER(cnt_map_single);
2822
Joerg Roedel94f6d192009-11-24 16:40:02 +01002823 domain = get_domain(dev);
2824 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002825 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002826 else if (IS_ERR(domain))
2827 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002828
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002829 dma_mask = *dev->dma_mask;
2830
Joerg Roedel4da70b92008-06-26 21:28:01 +02002831 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002832
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002833 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002834 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002835 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002836 goto out;
2837
Joerg Roedel17b124b2011-04-06 18:01:35 +02002838 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002839
2840out:
2841 spin_unlock_irqrestore(&domain->lock, flags);
2842
2843 return addr;
2844}
2845
Joerg Roedel431b2a22008-07-11 17:14:22 +02002846/*
2847 * The exported unmap_single function for dma_ops.
2848 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002849static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2850 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002851{
2852 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002853 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002854
Joerg Roedel146a6912008-12-12 15:07:12 +01002855 INC_STATS_COUNTER(cnt_unmap_single);
2856
Joerg Roedel94f6d192009-11-24 16:40:02 +01002857 domain = get_domain(dev);
2858 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002859 return;
2860
Joerg Roedel4da70b92008-06-26 21:28:01 +02002861 spin_lock_irqsave(&domain->lock, flags);
2862
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002863 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002864
Joerg Roedel17b124b2011-04-06 18:01:35 +02002865 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002866
2867 spin_unlock_irqrestore(&domain->lock, flags);
2868}
2869
Joerg Roedel431b2a22008-07-11 17:14:22 +02002870/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002871 * The exported map_sg function for dma_ops (handles scatter-gather
2872 * lists).
2873 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002874static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002875 int nelems, enum dma_data_direction dir,
2876 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002877{
2878 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002879 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002880 int i;
2881 struct scatterlist *s;
2882 phys_addr_t paddr;
2883 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002884 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002885
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002886 INC_STATS_COUNTER(cnt_map_sg);
2887
Joerg Roedel94f6d192009-11-24 16:40:02 +01002888 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002889 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002890 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002891
Joerg Roedel832a90c2008-09-18 15:54:23 +02002892 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002893
Joerg Roedel65b050a2008-06-26 21:28:02 +02002894 spin_lock_irqsave(&domain->lock, flags);
2895
2896 for_each_sg(sglist, s, nelems, i) {
2897 paddr = sg_phys(s);
2898
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002899 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002900 paddr, s->length, dir, false,
2901 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002902
2903 if (s->dma_address) {
2904 s->dma_length = s->length;
2905 mapped_elems++;
2906 } else
2907 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002908 }
2909
Joerg Roedel17b124b2011-04-06 18:01:35 +02002910 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002911
2912out:
2913 spin_unlock_irqrestore(&domain->lock, flags);
2914
2915 return mapped_elems;
2916unmap:
2917 for_each_sg(sglist, s, mapped_elems, i) {
2918 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002919 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002920 s->dma_length, dir);
2921 s->dma_address = s->dma_length = 0;
2922 }
2923
2924 mapped_elems = 0;
2925
2926 goto out;
2927}
2928
Joerg Roedel431b2a22008-07-11 17:14:22 +02002929/*
2930 * The exported map_sg function for dma_ops (handles scatter-gather
2931 * lists).
2932 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002933static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002934 int nelems, enum dma_data_direction dir,
2935 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002936{
2937 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002938 struct protection_domain *domain;
2939 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002940 int i;
2941
Joerg Roedel55877a62008-12-12 15:12:14 +01002942 INC_STATS_COUNTER(cnt_unmap_sg);
2943
Joerg Roedel94f6d192009-11-24 16:40:02 +01002944 domain = get_domain(dev);
2945 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002946 return;
2947
Joerg Roedel65b050a2008-06-26 21:28:02 +02002948 spin_lock_irqsave(&domain->lock, flags);
2949
2950 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002951 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002952 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002953 s->dma_address = s->dma_length = 0;
2954 }
2955
Joerg Roedel17b124b2011-04-06 18:01:35 +02002956 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002957
2958 spin_unlock_irqrestore(&domain->lock, flags);
2959}
2960
Joerg Roedel431b2a22008-07-11 17:14:22 +02002961/*
2962 * The exported alloc_coherent function for dma_ops.
2963 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002964static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002965 dma_addr_t *dma_addr, gfp_t flag,
2966 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002967{
2968 unsigned long flags;
2969 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002970 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002971 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002972 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002973
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002974 INC_STATS_COUNTER(cnt_alloc_coherent);
2975
Joerg Roedel94f6d192009-11-24 16:40:02 +01002976 domain = get_domain(dev);
2977 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002978 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2979 *dma_addr = __pa(virt_addr);
2980 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002981 } else if (IS_ERR(domain))
2982 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002983
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002984 dma_mask = dev->coherent_dma_mask;
2985 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2986 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002987
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002988 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2989 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302990 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002991
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002992 paddr = virt_to_phys(virt_addr);
2993
Joerg Roedel832a90c2008-09-18 15:54:23 +02002994 if (!dma_mask)
2995 dma_mask = *dev->dma_mask;
2996
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002997 spin_lock_irqsave(&domain->lock, flags);
2998
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002999 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02003000 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003001
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09003002 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02003003 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01003004 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02003005 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003006
Joerg Roedel17b124b2011-04-06 18:01:35 +02003007 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003008
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003009 spin_unlock_irqrestore(&domain->lock, flags);
3010
3011 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01003012
3013out_free:
3014
3015 free_pages((unsigned long)virt_addr, get_order(size));
3016
3017 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003018}
3019
Joerg Roedel431b2a22008-07-11 17:14:22 +02003020/*
3021 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003022 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003023static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003024 void *virt_addr, dma_addr_t dma_addr,
3025 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003026{
3027 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003028 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003029
Joerg Roedel5d31ee72008-12-12 15:16:38 +01003030 INC_STATS_COUNTER(cnt_free_coherent);
3031
Joerg Roedel94f6d192009-11-24 16:40:02 +01003032 domain = get_domain(dev);
3033 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01003034 goto free_mem;
3035
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003036 spin_lock_irqsave(&domain->lock, flags);
3037
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003038 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003039
Joerg Roedel17b124b2011-04-06 18:01:35 +02003040 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003041
3042 spin_unlock_irqrestore(&domain->lock, flags);
3043
3044free_mem:
3045 free_pages((unsigned long)virt_addr, get_order(size));
3046}
3047
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003048/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003049 * This function is called by the DMA layer to find out if we can handle a
3050 * particular device. It is part of the dma_ops.
3051 */
3052static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3053{
Joerg Roedel420aef82009-11-23 16:14:57 +01003054 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003055}
3056
3057/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003058 * The function for pre-allocating protection domains.
3059 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003060 * If the driver core informs the DMA layer if a driver grabs a device
3061 * we don't need to preallocate the protection domains anymore.
3062 * For now we have to.
3063 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003064static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003065{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003066 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003067 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003068 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003069 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003070
Chris Wrightd18c69d2010-04-02 18:27:55 -07003071 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003072
3073 /* Do we handle this device? */
3074 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003075 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003076
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003077 dev_data = get_dev_data(&dev->dev);
3078 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3079 /* Make sure passthrough domain is allocated */
3080 alloc_passthrough_domain();
3081 dev_data->passthrough = true;
3082 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003083 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003084 dev_name(&dev->dev));
3085 }
3086
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003087 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003088 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003089 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003090
3091 devid = get_device_id(&dev->dev);
3092
Joerg Roedel87a64d52009-11-24 17:26:43 +01003093 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003094 if (!dma_dom)
3095 continue;
3096 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003097 dma_dom->target_dev = devid;
3098
Joerg Roedel15898bb2009-11-24 15:39:42 +01003099 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003100
Joerg Roedelbd60b732008-09-11 10:24:48 +02003101 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003102 }
3103}
3104
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003105static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003106 .alloc = alloc_coherent,
3107 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003108 .map_page = map_page,
3109 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003110 .map_sg = map_sg,
3111 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003112 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003113};
3114
Joerg Roedel27c21272011-05-30 15:56:24 +02003115static unsigned device_dma_ops_init(void)
3116{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003117 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003118 struct pci_dev *pdev = NULL;
3119 unsigned unhandled = 0;
3120
3121 for_each_pci_dev(pdev) {
3122 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003123
3124 iommu_ignore_device(&pdev->dev);
3125
Joerg Roedel27c21272011-05-30 15:56:24 +02003126 unhandled += 1;
3127 continue;
3128 }
3129
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003130 dev_data = get_dev_data(&pdev->dev);
3131
3132 if (!dev_data->passthrough)
3133 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3134 else
3135 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003136 }
3137
3138 return unhandled;
3139}
3140
Joerg Roedel431b2a22008-07-11 17:14:22 +02003141/*
3142 * The function which clues the AMD IOMMU driver into dma_ops.
3143 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003144
3145void __init amd_iommu_init_api(void)
3146{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003147 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003148}
3149
Joerg Roedel6631ee92008-06-26 21:28:05 +02003150int __init amd_iommu_init_dma_ops(void)
3151{
3152 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003153 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003154
Joerg Roedel431b2a22008-07-11 17:14:22 +02003155 /*
3156 * first allocate a default protection domain for every IOMMU we
3157 * found in the system. Devices not assigned to any other
3158 * protection domain will be assigned to the default one.
3159 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003160 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003161 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003162 if (iommu->default_dom == NULL)
3163 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003164 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003165 ret = iommu_init_unity_mappings(iommu);
3166 if (ret)
3167 goto free_domains;
3168 }
3169
Joerg Roedel431b2a22008-07-11 17:14:22 +02003170 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003171 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003172 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003173 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003174
3175 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003176 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003177
Joerg Roedel431b2a22008-07-11 17:14:22 +02003178 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003179 unhandled = device_dma_ops_init();
3180 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3181 /* There are unhandled devices - initialize swiotlb for them */
3182 swiotlb = 1;
3183 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003184
Joerg Roedel7f265082008-12-12 13:50:21 +01003185 amd_iommu_stats_init();
3186
Joerg Roedel62410ee2012-06-12 16:42:43 +02003187 if (amd_iommu_unmap_flush)
3188 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3189 else
3190 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3191
Joerg Roedel6631ee92008-06-26 21:28:05 +02003192 return 0;
3193
3194free_domains:
3195
Joerg Roedel3bd22172009-05-04 15:06:20 +02003196 for_each_iommu(iommu) {
Cyril Roelandt91457df2013-02-12 05:01:50 +01003197 dma_ops_domain_free(iommu->default_dom);
Joerg Roedel6631ee92008-06-26 21:28:05 +02003198 }
3199
3200 return ret;
3201}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003202
3203/*****************************************************************************
3204 *
3205 * The following functions belong to the exported interface of AMD IOMMU
3206 *
3207 * This interface allows access to lower level functions of the IOMMU
3208 * like protection domain handling and assignement of devices to domains
3209 * which is not possible with the dma_ops interface.
3210 *
3211 *****************************************************************************/
3212
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003213static void cleanup_domain(struct protection_domain *domain)
3214{
Joerg Roedel492667d2009-11-27 13:25:47 +01003215 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003216 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003217
3218 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3219
Joerg Roedel492667d2009-11-27 13:25:47 +01003220 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003221 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003222 atomic_set(&dev_data->bind, 0);
3223 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003224
3225 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3226}
3227
Joerg Roedel26508152009-08-26 16:52:40 +02003228static void protection_domain_free(struct protection_domain *domain)
3229{
3230 if (!domain)
3231 return;
3232
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003233 del_domain_from_list(domain);
3234
Joerg Roedel26508152009-08-26 16:52:40 +02003235 if (domain->id)
3236 domain_id_free(domain->id);
3237
3238 kfree(domain);
3239}
3240
3241static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003242{
3243 struct protection_domain *domain;
3244
3245 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3246 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003247 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003248
3249 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003250 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003251 domain->id = domain_id_alloc();
3252 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003253 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003254 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003255
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003256 add_domain_to_list(domain);
3257
Joerg Roedel26508152009-08-26 16:52:40 +02003258 return domain;
3259
3260out_err:
3261 kfree(domain);
3262
3263 return NULL;
3264}
3265
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003266static int __init alloc_passthrough_domain(void)
3267{
3268 if (pt_domain != NULL)
3269 return 0;
3270
3271 /* allocate passthrough domain */
3272 pt_domain = protection_domain_alloc();
3273 if (!pt_domain)
3274 return -ENOMEM;
3275
3276 pt_domain->mode = PAGE_MODE_NONE;
3277
3278 return 0;
3279}
Joerg Roedel26508152009-08-26 16:52:40 +02003280static int amd_iommu_domain_init(struct iommu_domain *dom)
3281{
3282 struct protection_domain *domain;
3283
3284 domain = protection_domain_alloc();
3285 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003286 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003287
3288 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003289 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3290 if (!domain->pt_root)
3291 goto out_free;
3292
Joerg Roedelf3572db2011-11-23 12:36:25 +01003293 domain->iommu_domain = dom;
3294
Joerg Roedelc156e342008-12-02 18:13:27 +01003295 dom->priv = domain;
3296
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003297 dom->geometry.aperture_start = 0;
3298 dom->geometry.aperture_end = ~0ULL;
3299 dom->geometry.force_aperture = true;
3300
Joerg Roedelc156e342008-12-02 18:13:27 +01003301 return 0;
3302
3303out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003304 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003305
3306 return -ENOMEM;
3307}
3308
Joerg Roedel98383fc2008-12-02 18:34:12 +01003309static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3310{
3311 struct protection_domain *domain = dom->priv;
3312
3313 if (!domain)
3314 return;
3315
3316 if (domain->dev_cnt > 0)
3317 cleanup_domain(domain);
3318
3319 BUG_ON(domain->dev_cnt != 0);
3320
Joerg Roedel132bd682011-11-17 14:18:46 +01003321 if (domain->mode != PAGE_MODE_NONE)
3322 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003323
Joerg Roedel52815b72011-11-17 17:24:28 +01003324 if (domain->flags & PD_IOMMUV2_MASK)
3325 free_gcr3_table(domain);
3326
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003327 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003328
3329 dom->priv = NULL;
3330}
3331
Joerg Roedel684f2882008-12-08 12:07:44 +01003332static void amd_iommu_detach_device(struct iommu_domain *dom,
3333 struct device *dev)
3334{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003335 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003336 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003337 u16 devid;
3338
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003339 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003340 return;
3341
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003342 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003343
Joerg Roedel657cbb62009-11-23 15:26:46 +01003344 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003345 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003346
3347 iommu = amd_iommu_rlookup_table[devid];
3348 if (!iommu)
3349 return;
3350
Joerg Roedel684f2882008-12-08 12:07:44 +01003351 iommu_completion_wait(iommu);
3352}
3353
Joerg Roedel01106062008-12-02 19:34:11 +01003354static int amd_iommu_attach_device(struct iommu_domain *dom,
3355 struct device *dev)
3356{
3357 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003358 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003359 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003360 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003361
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003362 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003363 return -EINVAL;
3364
Joerg Roedel657cbb62009-11-23 15:26:46 +01003365 dev_data = dev->archdata.iommu;
3366
Joerg Roedelf62dda62011-06-09 12:55:35 +02003367 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003368 if (!iommu)
3369 return -EINVAL;
3370
Joerg Roedel657cbb62009-11-23 15:26:46 +01003371 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003372 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003373
Joerg Roedel15898bb2009-11-24 15:39:42 +01003374 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003375
3376 iommu_completion_wait(iommu);
3377
Joerg Roedel15898bb2009-11-24 15:39:42 +01003378 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003379}
3380
Joerg Roedel468e2362010-01-21 16:37:36 +01003381static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003382 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003383{
3384 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003385 int prot = 0;
3386 int ret;
3387
Joerg Roedel132bd682011-11-17 14:18:46 +01003388 if (domain->mode == PAGE_MODE_NONE)
3389 return -EINVAL;
3390
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003391 if (iommu_prot & IOMMU_READ)
3392 prot |= IOMMU_PROT_IR;
3393 if (iommu_prot & IOMMU_WRITE)
3394 prot |= IOMMU_PROT_IW;
3395
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003396 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003397 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003398 mutex_unlock(&domain->api_lock);
3399
Joerg Roedel795e74f72010-05-11 17:40:57 +02003400 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003401}
3402
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003403static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3404 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003405{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003406 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003407 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003408
Joerg Roedel132bd682011-11-17 14:18:46 +01003409 if (domain->mode == PAGE_MODE_NONE)
3410 return -EINVAL;
3411
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003412 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003413 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003414 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003415
Joerg Roedel17b124b2011-04-06 18:01:35 +02003416 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003417
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003418 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003419}
3420
Joerg Roedel645c4c82008-12-02 20:05:50 +01003421static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303422 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003423{
3424 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003425 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003426 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003427 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003428
Joerg Roedel132bd682011-11-17 14:18:46 +01003429 if (domain->mode == PAGE_MODE_NONE)
3430 return iova;
3431
Joerg Roedel24cd7722010-01-19 17:27:39 +01003432 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003433
Joerg Roedela6d41a42009-09-02 17:08:55 +02003434 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003435 return 0;
3436
Joerg Roedelf03152b2010-01-21 16:15:24 +01003437 if (PM_PTE_LEVEL(*pte) == 0)
3438 offset_mask = PAGE_SIZE - 1;
3439 else
3440 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3441
3442 __pte = *pte & PM_ADDR_MASK;
3443 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003444
3445 return paddr;
3446}
3447
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003448static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3449 unsigned long cap)
3450{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003451 switch (cap) {
3452 case IOMMU_CAP_CACHE_COHERENCY:
3453 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003454 case IOMMU_CAP_INTR_REMAP:
3455 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003456 }
3457
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003458 return 0;
3459}
3460
Joerg Roedel26961ef2008-12-03 17:00:17 +01003461static struct iommu_ops amd_iommu_ops = {
3462 .domain_init = amd_iommu_domain_init,
3463 .domain_destroy = amd_iommu_domain_destroy,
3464 .attach_dev = amd_iommu_attach_device,
3465 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003466 .map = amd_iommu_map,
3467 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003468 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003469 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003470 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003471};
3472
Joerg Roedel0feae532009-08-26 15:26:30 +02003473/*****************************************************************************
3474 *
3475 * The next functions do a basic initialization of IOMMU for pass through
3476 * mode
3477 *
3478 * In passthrough mode the IOMMU is initialized and enabled but not used for
3479 * DMA-API translation.
3480 *
3481 *****************************************************************************/
3482
3483int __init amd_iommu_init_passthrough(void)
3484{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003485 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003486 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003487 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003488 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003489 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003490
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003491 ret = alloc_passthrough_domain();
3492 if (ret)
3493 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003494
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003495 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003496 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003497 continue;
3498
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003499 dev_data = get_dev_data(&dev->dev);
3500 dev_data->passthrough = true;
3501
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003502 devid = get_device_id(&dev->dev);
3503
Joerg Roedel15898bb2009-11-24 15:39:42 +01003504 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003505 if (!iommu)
3506 continue;
3507
Joerg Roedel15898bb2009-11-24 15:39:42 +01003508 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003509 }
3510
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003511 amd_iommu_stats_init();
3512
Joerg Roedel0feae532009-08-26 15:26:30 +02003513 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3514
3515 return 0;
3516}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003517
3518/* IOMMUv2 specific functions */
3519int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3520{
3521 return atomic_notifier_chain_register(&ppr_notifier, nb);
3522}
3523EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3524
3525int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3526{
3527 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3528}
3529EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003530
3531void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3532{
3533 struct protection_domain *domain = dom->priv;
3534 unsigned long flags;
3535
3536 spin_lock_irqsave(&domain->lock, flags);
3537
3538 /* Update data structure */
3539 domain->mode = PAGE_MODE_NONE;
3540 domain->updated = true;
3541
3542 /* Make changes visible to IOMMUs */
3543 update_domain(domain);
3544
3545 /* Page-table is not visible to IOMMU anymore, so free it */
3546 free_pagetable(domain);
3547
3548 spin_unlock_irqrestore(&domain->lock, flags);
3549}
3550EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003551
3552int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3553{
3554 struct protection_domain *domain = dom->priv;
3555 unsigned long flags;
3556 int levels, ret;
3557
3558 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3559 return -EINVAL;
3560
3561 /* Number of GCR3 table levels required */
3562 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3563 levels += 1;
3564
3565 if (levels > amd_iommu_max_glx_val)
3566 return -EINVAL;
3567
3568 spin_lock_irqsave(&domain->lock, flags);
3569
3570 /*
3571 * Save us all sanity checks whether devices already in the
3572 * domain support IOMMUv2. Just force that the domain has no
3573 * devices attached when it is switched into IOMMUv2 mode.
3574 */
3575 ret = -EBUSY;
3576 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3577 goto out;
3578
3579 ret = -ENOMEM;
3580 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3581 if (domain->gcr3_tbl == NULL)
3582 goto out;
3583
3584 domain->glx = levels;
3585 domain->flags |= PD_IOMMUV2_MASK;
3586 domain->updated = true;
3587
3588 update_domain(domain);
3589
3590 ret = 0;
3591
3592out:
3593 spin_unlock_irqrestore(&domain->lock, flags);
3594
3595 return ret;
3596}
3597EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003598
3599static int __flush_pasid(struct protection_domain *domain, int pasid,
3600 u64 address, bool size)
3601{
3602 struct iommu_dev_data *dev_data;
3603 struct iommu_cmd cmd;
3604 int i, ret;
3605
3606 if (!(domain->flags & PD_IOMMUV2_MASK))
3607 return -EINVAL;
3608
3609 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3610
3611 /*
3612 * IOMMU TLB needs to be flushed before Device TLB to
3613 * prevent device TLB refill from IOMMU TLB
3614 */
3615 for (i = 0; i < amd_iommus_present; ++i) {
3616 if (domain->dev_iommu[i] == 0)
3617 continue;
3618
3619 ret = iommu_queue_command(amd_iommus[i], &cmd);
3620 if (ret != 0)
3621 goto out;
3622 }
3623
3624 /* Wait until IOMMU TLB flushes are complete */
3625 domain_flush_complete(domain);
3626
3627 /* Now flush device TLBs */
3628 list_for_each_entry(dev_data, &domain->dev_list, list) {
3629 struct amd_iommu *iommu;
3630 int qdep;
3631
3632 BUG_ON(!dev_data->ats.enabled);
3633
3634 qdep = dev_data->ats.qdep;
3635 iommu = amd_iommu_rlookup_table[dev_data->devid];
3636
3637 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3638 qdep, address, size);
3639
3640 ret = iommu_queue_command(iommu, &cmd);
3641 if (ret != 0)
3642 goto out;
3643 }
3644
3645 /* Wait until all device TLBs are flushed */
3646 domain_flush_complete(domain);
3647
3648 ret = 0;
3649
3650out:
3651
3652 return ret;
3653}
3654
3655static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3656 u64 address)
3657{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003658 INC_STATS_COUNTER(invalidate_iotlb);
3659
Joerg Roedel22e266c2011-11-21 15:59:08 +01003660 return __flush_pasid(domain, pasid, address, false);
3661}
3662
3663int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3664 u64 address)
3665{
3666 struct protection_domain *domain = dom->priv;
3667 unsigned long flags;
3668 int ret;
3669
3670 spin_lock_irqsave(&domain->lock, flags);
3671 ret = __amd_iommu_flush_page(domain, pasid, address);
3672 spin_unlock_irqrestore(&domain->lock, flags);
3673
3674 return ret;
3675}
3676EXPORT_SYMBOL(amd_iommu_flush_page);
3677
3678static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3679{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003680 INC_STATS_COUNTER(invalidate_iotlb_all);
3681
Joerg Roedel22e266c2011-11-21 15:59:08 +01003682 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3683 true);
3684}
3685
3686int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3687{
3688 struct protection_domain *domain = dom->priv;
3689 unsigned long flags;
3690 int ret;
3691
3692 spin_lock_irqsave(&domain->lock, flags);
3693 ret = __amd_iommu_flush_tlb(domain, pasid);
3694 spin_unlock_irqrestore(&domain->lock, flags);
3695
3696 return ret;
3697}
3698EXPORT_SYMBOL(amd_iommu_flush_tlb);
3699
Joerg Roedelb16137b2011-11-21 16:50:23 +01003700static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3701{
3702 int index;
3703 u64 *pte;
3704
3705 while (true) {
3706
3707 index = (pasid >> (9 * level)) & 0x1ff;
3708 pte = &root[index];
3709
3710 if (level == 0)
3711 break;
3712
3713 if (!(*pte & GCR3_VALID)) {
3714 if (!alloc)
3715 return NULL;
3716
3717 root = (void *)get_zeroed_page(GFP_ATOMIC);
3718 if (root == NULL)
3719 return NULL;
3720
3721 *pte = __pa(root) | GCR3_VALID;
3722 }
3723
3724 root = __va(*pte & PAGE_MASK);
3725
3726 level -= 1;
3727 }
3728
3729 return pte;
3730}
3731
3732static int __set_gcr3(struct protection_domain *domain, int pasid,
3733 unsigned long cr3)
3734{
3735 u64 *pte;
3736
3737 if (domain->mode != PAGE_MODE_NONE)
3738 return -EINVAL;
3739
3740 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3741 if (pte == NULL)
3742 return -ENOMEM;
3743
3744 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3745
3746 return __amd_iommu_flush_tlb(domain, pasid);
3747}
3748
3749static int __clear_gcr3(struct protection_domain *domain, int pasid)
3750{
3751 u64 *pte;
3752
3753 if (domain->mode != PAGE_MODE_NONE)
3754 return -EINVAL;
3755
3756 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3757 if (pte == NULL)
3758 return 0;
3759
3760 *pte = 0;
3761
3762 return __amd_iommu_flush_tlb(domain, pasid);
3763}
3764
3765int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3766 unsigned long cr3)
3767{
3768 struct protection_domain *domain = dom->priv;
3769 unsigned long flags;
3770 int ret;
3771
3772 spin_lock_irqsave(&domain->lock, flags);
3773 ret = __set_gcr3(domain, pasid, cr3);
3774 spin_unlock_irqrestore(&domain->lock, flags);
3775
3776 return ret;
3777}
3778EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3779
3780int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3781{
3782 struct protection_domain *domain = dom->priv;
3783 unsigned long flags;
3784 int ret;
3785
3786 spin_lock_irqsave(&domain->lock, flags);
3787 ret = __clear_gcr3(domain, pasid);
3788 spin_unlock_irqrestore(&domain->lock, flags);
3789
3790 return ret;
3791}
3792EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003793
3794int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3795 int status, int tag)
3796{
3797 struct iommu_dev_data *dev_data;
3798 struct amd_iommu *iommu;
3799 struct iommu_cmd cmd;
3800
Joerg Roedel399be2f2011-12-01 16:53:47 +01003801 INC_STATS_COUNTER(complete_ppr);
3802
Joerg Roedelc99afa22011-11-21 18:19:25 +01003803 dev_data = get_dev_data(&pdev->dev);
3804 iommu = amd_iommu_rlookup_table[dev_data->devid];
3805
3806 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3807 tag, dev_data->pri_tlp);
3808
3809 return iommu_queue_command(iommu, &cmd);
3810}
3811EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003812
3813struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3814{
3815 struct protection_domain *domain;
3816
3817 domain = get_domain(&pdev->dev);
3818 if (IS_ERR(domain))
3819 return NULL;
3820
3821 /* Only return IOMMUv2 domains */
3822 if (!(domain->flags & PD_IOMMUV2_MASK))
3823 return NULL;
3824
3825 return domain->iommu_domain;
3826}
3827EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003828
3829void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3830{
3831 struct iommu_dev_data *dev_data;
3832
3833 if (!amd_iommu_v2_supported())
3834 return;
3835
3836 dev_data = get_dev_data(&pdev->dev);
3837 dev_data->errata |= (1 << erratum);
3838}
3839EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003840
3841int amd_iommu_device_info(struct pci_dev *pdev,
3842 struct amd_iommu_device_info *info)
3843{
3844 int max_pasids;
3845 int pos;
3846
3847 if (pdev == NULL || info == NULL)
3848 return -EINVAL;
3849
3850 if (!amd_iommu_v2_supported())
3851 return -EINVAL;
3852
3853 memset(info, 0, sizeof(*info));
3854
3855 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3856 if (pos)
3857 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3858
3859 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3860 if (pos)
3861 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3862
3863 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3864 if (pos) {
3865 int features;
3866
3867 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3868 max_pasids = min(max_pasids, (1 << 20));
3869
3870 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3871 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3872
3873 features = pci_pasid_features(pdev);
3874 if (features & PCI_PASID_CAP_EXEC)
3875 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3876 if (features & PCI_PASID_CAP_PRIV)
3877 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3878 }
3879
3880 return 0;
3881}
3882EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003883
3884#ifdef CONFIG_IRQ_REMAP
3885
3886/*****************************************************************************
3887 *
3888 * Interrupt Remapping Implementation
3889 *
3890 *****************************************************************************/
3891
3892union irte {
3893 u32 val;
3894 struct {
3895 u32 valid : 1,
3896 no_fault : 1,
3897 int_type : 3,
3898 rq_eoi : 1,
3899 dm : 1,
3900 rsvd_1 : 1,
3901 destination : 8,
3902 vector : 8,
3903 rsvd_2 : 8;
3904 } fields;
3905};
3906
3907#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3908#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3909#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3910#define DTE_IRQ_REMAP_ENABLE 1ULL
3911
3912static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3913{
3914 u64 dte;
3915
3916 dte = amd_iommu_dev_table[devid].data[2];
3917 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3918 dte |= virt_to_phys(table->table);
3919 dte |= DTE_IRQ_REMAP_INTCTL;
3920 dte |= DTE_IRQ_TABLE_LEN;
3921 dte |= DTE_IRQ_REMAP_ENABLE;
3922
3923 amd_iommu_dev_table[devid].data[2] = dte;
3924}
3925
3926#define IRTE_ALLOCATED (~1U)
3927
3928static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3929{
3930 struct irq_remap_table *table = NULL;
3931 struct amd_iommu *iommu;
3932 unsigned long flags;
3933 u16 alias;
3934
3935 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3936
3937 iommu = amd_iommu_rlookup_table[devid];
3938 if (!iommu)
3939 goto out_unlock;
3940
3941 table = irq_lookup_table[devid];
3942 if (table)
3943 goto out;
3944
3945 alias = amd_iommu_alias_table[devid];
3946 table = irq_lookup_table[alias];
3947 if (table) {
3948 irq_lookup_table[devid] = table;
3949 set_dte_irq_entry(devid, table);
3950 iommu_flush_dte(iommu, devid);
3951 goto out;
3952 }
3953
3954 /* Nothing there yet, allocate new irq remapping table */
3955 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3956 if (!table)
3957 goto out;
3958
Joerg Roedel197887f2013-04-09 21:14:08 +02003959 /* Initialize table spin-lock */
3960 spin_lock_init(&table->lock);
3961
Joerg Roedel2b324502012-06-21 16:29:10 +02003962 if (ioapic)
3963 /* Keep the first 32 indexes free for IOAPIC interrupts */
3964 table->min_index = 32;
3965
3966 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3967 if (!table->table) {
3968 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003969 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003970 goto out;
3971 }
3972
3973 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3974
3975 if (ioapic) {
3976 int i;
3977
3978 for (i = 0; i < 32; ++i)
3979 table->table[i] = IRTE_ALLOCATED;
3980 }
3981
3982 irq_lookup_table[devid] = table;
3983 set_dte_irq_entry(devid, table);
3984 iommu_flush_dte(iommu, devid);
3985 if (devid != alias) {
3986 irq_lookup_table[alias] = table;
3987 set_dte_irq_entry(devid, table);
3988 iommu_flush_dte(iommu, alias);
3989 }
3990
3991out:
3992 iommu_completion_wait(iommu);
3993
3994out_unlock:
3995 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3996
3997 return table;
3998}
3999
4000static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
4001{
4002 struct irq_remap_table *table;
4003 unsigned long flags;
4004 int index, c;
4005
4006 table = get_irq_table(devid, false);
4007 if (!table)
4008 return -ENODEV;
4009
4010 spin_lock_irqsave(&table->lock, flags);
4011
4012 /* Scan table for free entries */
4013 for (c = 0, index = table->min_index;
4014 index < MAX_IRQS_PER_TABLE;
4015 ++index) {
4016 if (table->table[index] == 0)
4017 c += 1;
4018 else
4019 c = 0;
4020
4021 if (c == count) {
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004022 struct irq_2_irte *irte_info;
Joerg Roedel2b324502012-06-21 16:29:10 +02004023
4024 for (; c != 0; --c)
4025 table->table[index - c + 1] = IRTE_ALLOCATED;
4026
4027 index -= count - 1;
4028
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004029 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004030 irte_info = &cfg->irq_2_irte;
4031 irte_info->devid = devid;
4032 irte_info->index = index;
Joerg Roedel2b324502012-06-21 16:29:10 +02004033
4034 goto out;
4035 }
4036 }
4037
4038 index = -ENOSPC;
4039
4040out:
4041 spin_unlock_irqrestore(&table->lock, flags);
4042
4043 return index;
4044}
4045
4046static int get_irte(u16 devid, int index, union irte *irte)
4047{
4048 struct irq_remap_table *table;
4049 unsigned long flags;
4050
4051 table = get_irq_table(devid, false);
4052 if (!table)
4053 return -ENOMEM;
4054
4055 spin_lock_irqsave(&table->lock, flags);
4056 irte->val = table->table[index];
4057 spin_unlock_irqrestore(&table->lock, flags);
4058
4059 return 0;
4060}
4061
4062static int modify_irte(u16 devid, int index, union irte irte)
4063{
4064 struct irq_remap_table *table;
4065 struct amd_iommu *iommu;
4066 unsigned long flags;
4067
4068 iommu = amd_iommu_rlookup_table[devid];
4069 if (iommu == NULL)
4070 return -EINVAL;
4071
4072 table = get_irq_table(devid, false);
4073 if (!table)
4074 return -ENOMEM;
4075
4076 spin_lock_irqsave(&table->lock, flags);
4077 table->table[index] = irte.val;
4078 spin_unlock_irqrestore(&table->lock, flags);
4079
4080 iommu_flush_irt(iommu, devid);
4081 iommu_completion_wait(iommu);
4082
4083 return 0;
4084}
4085
4086static void free_irte(u16 devid, int index)
4087{
4088 struct irq_remap_table *table;
4089 struct amd_iommu *iommu;
4090 unsigned long flags;
4091
4092 iommu = amd_iommu_rlookup_table[devid];
4093 if (iommu == NULL)
4094 return;
4095
4096 table = get_irq_table(devid, false);
4097 if (!table)
4098 return;
4099
4100 spin_lock_irqsave(&table->lock, flags);
4101 table->table[index] = 0;
4102 spin_unlock_irqrestore(&table->lock, flags);
4103
4104 iommu_flush_irt(iommu, devid);
4105 iommu_completion_wait(iommu);
4106}
4107
Joerg Roedel5527de72012-06-26 11:17:32 +02004108static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4109 unsigned int destination, int vector,
4110 struct io_apic_irq_attr *attr)
4111{
4112 struct irq_remap_table *table;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004113 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004114 struct irq_cfg *cfg;
4115 union irte irte;
4116 int ioapic_id;
4117 int index;
4118 int devid;
4119 int ret;
4120
4121 cfg = irq_get_chip_data(irq);
4122 if (!cfg)
4123 return -EINVAL;
4124
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004125 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004126 ioapic_id = mpc_ioapic_id(attr->ioapic);
4127 devid = get_ioapic_devid(ioapic_id);
4128
4129 if (devid < 0)
4130 return devid;
4131
4132 table = get_irq_table(devid, true);
4133 if (table == NULL)
4134 return -ENOMEM;
4135
4136 index = attr->ioapic_pin;
4137
4138 /* Setup IRQ remapping info */
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004139 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004140 irte_info->devid = devid;
4141 irte_info->index = index;
Joerg Roedel5527de72012-06-26 11:17:32 +02004142
4143 /* Setup IRTE for IOMMU */
4144 irte.val = 0;
4145 irte.fields.vector = vector;
4146 irte.fields.int_type = apic->irq_delivery_mode;
4147 irte.fields.destination = destination;
4148 irte.fields.dm = apic->irq_dest_mode;
4149 irte.fields.valid = 1;
4150
4151 ret = modify_irte(devid, index, irte);
4152 if (ret)
4153 return ret;
4154
4155 /* Setup IOAPIC entry */
4156 memset(entry, 0, sizeof(*entry));
4157
4158 entry->vector = index;
4159 entry->mask = 0;
4160 entry->trigger = attr->trigger;
4161 entry->polarity = attr->polarity;
4162
4163 /*
4164 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004165 */
4166 if (attr->trigger)
4167 entry->mask = 1;
4168
4169 return 0;
4170}
4171
4172static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4173 bool force)
4174{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004175 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004176 unsigned int dest, irq;
4177 struct irq_cfg *cfg;
4178 union irte irte;
4179 int err;
4180
4181 if (!config_enabled(CONFIG_SMP))
4182 return -1;
4183
4184 cfg = data->chip_data;
4185 irq = data->irq;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004186 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004187
4188 if (!cpumask_intersects(mask, cpu_online_mask))
4189 return -EINVAL;
4190
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004191 if (get_irte(irte_info->devid, irte_info->index, &irte))
Joerg Roedel5527de72012-06-26 11:17:32 +02004192 return -EBUSY;
4193
4194 if (assign_irq_vector(irq, cfg, mask))
4195 return -EBUSY;
4196
4197 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4198 if (err) {
4199 if (assign_irq_vector(irq, cfg, data->affinity))
4200 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4201 return err;
4202 }
4203
4204 irte.fields.vector = cfg->vector;
4205 irte.fields.destination = dest;
4206
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004207 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel5527de72012-06-26 11:17:32 +02004208
4209 if (cfg->move_in_progress)
4210 send_cleanup_vector(cfg);
4211
4212 cpumask_copy(data->affinity, mask);
4213
4214 return 0;
4215}
4216
4217static int free_irq(int irq)
4218{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004219 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004220 struct irq_cfg *cfg;
4221
4222 cfg = irq_get_chip_data(irq);
4223 if (!cfg)
4224 return -EINVAL;
4225
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004226 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004227
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004228 free_irte(irte_info->devid, irte_info->index);
Joerg Roedel5527de72012-06-26 11:17:32 +02004229
4230 return 0;
4231}
4232
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004233static void compose_msi_msg(struct pci_dev *pdev,
4234 unsigned int irq, unsigned int dest,
4235 struct msi_msg *msg, u8 hpet_id)
4236{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004237 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004238 struct irq_cfg *cfg;
4239 union irte irte;
4240
4241 cfg = irq_get_chip_data(irq);
4242 if (!cfg)
4243 return;
4244
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004245 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004246
4247 irte.val = 0;
4248 irte.fields.vector = cfg->vector;
4249 irte.fields.int_type = apic->irq_delivery_mode;
4250 irte.fields.destination = dest;
4251 irte.fields.dm = apic->irq_dest_mode;
4252 irte.fields.valid = 1;
4253
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004254 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004255
4256 msg->address_hi = MSI_ADDR_BASE_HI;
4257 msg->address_lo = MSI_ADDR_BASE_LO;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004258 msg->data = irte_info->index;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004259}
4260
4261static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4262{
4263 struct irq_cfg *cfg;
4264 int index;
4265 u16 devid;
4266
4267 if (!pdev)
4268 return -EINVAL;
4269
4270 cfg = irq_get_chip_data(irq);
4271 if (!cfg)
4272 return -EINVAL;
4273
4274 devid = get_device_id(&pdev->dev);
4275 index = alloc_irq_index(cfg, devid, nvec);
4276
4277 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4278}
4279
4280static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4281 int index, int offset)
4282{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004283 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004284 struct irq_cfg *cfg;
4285 u16 devid;
4286
4287 if (!pdev)
4288 return -EINVAL;
4289
4290 cfg = irq_get_chip_data(irq);
4291 if (!cfg)
4292 return -EINVAL;
4293
4294 if (index >= MAX_IRQS_PER_TABLE)
4295 return 0;
4296
4297 devid = get_device_id(&pdev->dev);
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004298 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004299
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004300 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004301 irte_info->devid = devid;
4302 irte_info->index = index + offset;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004303
4304 return 0;
4305}
4306
Joerg Roedeld9761952012-06-26 16:00:08 +02004307static int setup_hpet_msi(unsigned int irq, unsigned int id)
4308{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004309 struct irq_2_irte *irte_info;
Joerg Roedeld9761952012-06-26 16:00:08 +02004310 struct irq_cfg *cfg;
4311 int index, devid;
4312
4313 cfg = irq_get_chip_data(irq);
4314 if (!cfg)
4315 return -EINVAL;
4316
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004317 irte_info = &cfg->irq_2_irte;
Joerg Roedeld9761952012-06-26 16:00:08 +02004318 devid = get_hpet_devid(id);
4319 if (devid < 0)
4320 return devid;
4321
4322 index = alloc_irq_index(cfg, devid, 1);
4323 if (index < 0)
4324 return index;
4325
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004326 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004327 irte_info->devid = devid;
4328 irte_info->index = index;
Joerg Roedeld9761952012-06-26 16:00:08 +02004329
4330 return 0;
4331}
4332
Joerg Roedel6b474b82012-06-26 16:46:04 +02004333struct irq_remap_ops amd_iommu_irq_ops = {
4334 .supported = amd_iommu_supported,
4335 .prepare = amd_iommu_prepare,
4336 .enable = amd_iommu_enable,
4337 .disable = amd_iommu_disable,
4338 .reenable = amd_iommu_reenable,
4339 .enable_faulting = amd_iommu_enable_faulting,
4340 .setup_ioapic_entry = setup_ioapic_entry,
4341 .set_affinity = set_affinity,
4342 .free_irq = free_irq,
4343 .compose_msi_msg = compose_msi_msg,
4344 .msi_alloc_irq = msi_alloc_irq,
4345 .msi_setup_irq = msi_setup_irq,
4346 .setup_hpet_msi = setup_hpet_msi,
4347};
Joerg Roedel2b324502012-06-21 16:29:10 +02004348#endif