blob: 5a02d0776a66a9a21f57bcd2867cae613a60eb2e [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
Alex Williamsonc14d2692013-05-30 12:39:18 -0600290 * required ACS flags, add to the same group as lowest numbered
291 * function that also does not suport the required ACS flags.
Alex Williamson31fe9432012-08-04 12:09:03 -0600292 */
Alex Williamson664b6002012-05-30 14:19:31 -0600293 if (dma_pdev->multifunction &&
Alex Williamsonc14d2692013-05-30 12:39:18 -0600294 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)) {
295 u8 i, slot = PCI_SLOT(dma_pdev->devfn);
296
297 for (i = 0; i < 8; i++) {
298 struct pci_dev *tmp;
299
300 tmp = pci_get_slot(dma_pdev->bus, PCI_DEVFN(slot, i));
301 if (!tmp)
302 continue;
303
304 if (!pci_acs_enabled(tmp, REQ_ACS_FLAGS)) {
305 swap_pci_ref(&dma_pdev, tmp);
306 break;
307 }
308 pci_dev_put(tmp);
309 }
310 }
Alex Williamson664b6002012-05-30 14:19:31 -0600311
Alex Williamson31fe9432012-08-04 12:09:03 -0600312 /*
313 * Devices on the root bus go through the iommu. If that's not us,
314 * find the next upstream device and test ACS up to the root bus.
315 * Finding the next device may require skipping virtual buses.
316 */
Alex Williamson664b6002012-05-30 14:19:31 -0600317 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson2bff6a52012-10-08 22:49:48 -0600318 struct pci_bus *bus = find_hosted_bus(dma_pdev->bus);
319 if (IS_ERR(bus))
320 break;
Alex Williamson31fe9432012-08-04 12:09:03 -0600321
322 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600323 break;
324
Alex Williamson31fe9432012-08-04 12:09:03 -0600325 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200326 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100327
Alex Williamson2851db22012-10-08 22:49:41 -0600328 return dma_pdev;
329}
330
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600331static int use_pdev_iommu_group(struct pci_dev *pdev, struct device *dev)
332{
333 struct iommu_group *group = iommu_group_get(&pdev->dev);
334 int ret;
335
336 if (!group) {
337 group = iommu_group_alloc();
338 if (IS_ERR(group))
339 return PTR_ERR(group);
340
341 WARN_ON(&pdev->dev != dev);
342 }
343
344 ret = iommu_group_add_device(group, dev);
345 iommu_group_put(group);
346 return ret;
347}
348
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600349static int use_dev_data_iommu_group(struct iommu_dev_data *dev_data,
350 struct device *dev)
351{
352 if (!dev_data->group) {
353 struct iommu_group *group = iommu_group_alloc();
354 if (IS_ERR(group))
355 return PTR_ERR(group);
356
357 dev_data->group = group;
358 }
359
360 return iommu_group_add_device(dev_data->group, dev);
361}
362
Alex Williamson2851db22012-10-08 22:49:41 -0600363static int init_iommu_group(struct device *dev)
364{
365 struct iommu_dev_data *dev_data;
366 struct iommu_group *group;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600367 struct pci_dev *dma_pdev;
Alex Williamson2851db22012-10-08 22:49:41 -0600368 int ret;
369
370 group = iommu_group_get(dev);
371 if (group) {
372 iommu_group_put(group);
373 return 0;
374 }
375
376 dev_data = find_dev_data(get_device_id(dev));
377 if (!dev_data)
378 return -ENOMEM;
379
380 if (dev_data->alias_data) {
381 u16 alias;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600382 struct pci_bus *bus;
Alex Williamson2851db22012-10-08 22:49:41 -0600383
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600384 if (dev_data->alias_data->group)
385 goto use_group;
386
387 /*
388 * If the alias device exists, it's effectively just a first
389 * level quirk for finding the DMA source.
390 */
Alex Williamson2851db22012-10-08 22:49:41 -0600391 alias = amd_iommu_alias_table[dev_data->devid];
392 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600393 if (dma_pdev) {
394 dma_pdev = get_isolation_root(dma_pdev);
395 goto use_pdev;
396 }
397
398 /*
399 * If the alias is virtual, try to find a parent device
400 * and test whether the IOMMU group is actualy rooted above
401 * the alias. Be careful to also test the parent device if
402 * we think the alias is the root of the group.
403 */
404 bus = pci_find_bus(0, alias >> 8);
405 if (!bus)
406 goto use_group;
407
408 bus = find_hosted_bus(bus);
409 if (IS_ERR(bus) || !bus->self)
410 goto use_group;
411
412 dma_pdev = get_isolation_root(pci_dev_get(bus->self));
413 if (dma_pdev != bus->self || (dma_pdev->multifunction &&
414 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)))
415 goto use_pdev;
416
417 pci_dev_put(dma_pdev);
418 goto use_group;
Alex Williamson2851db22012-10-08 22:49:41 -0600419 }
420
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600421 dma_pdev = get_isolation_root(pci_dev_get(to_pci_dev(dev)));
422use_pdev:
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600423 ret = use_pdev_iommu_group(dma_pdev, dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600424 pci_dev_put(dma_pdev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600425 return ret;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600426use_group:
427 return use_dev_data_iommu_group(dev_data->alias_data, dev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600428}
429
430static int iommu_init_device(struct device *dev)
431{
432 struct pci_dev *pdev = to_pci_dev(dev);
433 struct iommu_dev_data *dev_data;
434 u16 alias;
435 int ret;
436
437 if (dev->archdata.iommu)
438 return 0;
439
440 dev_data = find_dev_data(get_device_id(dev));
441 if (!dev_data)
442 return -ENOMEM;
443
444 alias = amd_iommu_alias_table[dev_data->devid];
445 if (alias != dev_data->devid) {
446 struct iommu_dev_data *alias_data;
447
448 alias_data = find_dev_data(alias);
449 if (alias_data == NULL) {
450 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
451 dev_name(dev));
452 free_dev_data(dev_data);
453 return -ENOTSUPP;
454 }
455 dev_data->alias_data = alias_data;
456 }
457
458 ret = init_iommu_group(dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600459 if (ret)
460 return ret;
461
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100462 if (pci_iommuv2_capable(pdev)) {
463 struct amd_iommu *iommu;
464
465 iommu = amd_iommu_rlookup_table[dev_data->devid];
466 dev_data->iommu_v2 = iommu->is_iommu_v2;
467 }
468
Joerg Roedel657cbb62009-11-23 15:26:46 +0100469 dev->archdata.iommu = dev_data;
470
Joerg Roedel657cbb62009-11-23 15:26:46 +0100471 return 0;
472}
473
Joerg Roedel26018872011-06-06 16:50:14 +0200474static void iommu_ignore_device(struct device *dev)
475{
476 u16 devid, alias;
477
478 devid = get_device_id(dev);
479 alias = amd_iommu_alias_table[devid];
480
481 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
482 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
483
484 amd_iommu_rlookup_table[devid] = NULL;
485 amd_iommu_rlookup_table[alias] = NULL;
486}
487
Joerg Roedel657cbb62009-11-23 15:26:46 +0100488static void iommu_uninit_device(struct device *dev)
489{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600490 iommu_group_remove_device(dev);
491
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200492 /*
493 * Nothing to do here - we keep dev_data around for unplugged devices
494 * and reuse it when the device is re-plugged - not doing so would
495 * introduce a ton of races.
496 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100497}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100498
499void __init amd_iommu_uninit_devices(void)
500{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200501 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100502 struct pci_dev *pdev = NULL;
503
504 for_each_pci_dev(pdev) {
505
506 if (!check_device(&pdev->dev))
507 continue;
508
509 iommu_uninit_device(&pdev->dev);
510 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200511
512 /* Free all of our dev_data structures */
513 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
514 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100515}
516
517int __init amd_iommu_init_devices(void)
518{
519 struct pci_dev *pdev = NULL;
520 int ret = 0;
521
522 for_each_pci_dev(pdev) {
523
524 if (!check_device(&pdev->dev))
525 continue;
526
527 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200528 if (ret == -ENOTSUPP)
529 iommu_ignore_device(&pdev->dev);
530 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100531 goto out_free;
532 }
533
534 return 0;
535
536out_free:
537
538 amd_iommu_uninit_devices();
539
540 return ret;
541}
Joerg Roedel7f265082008-12-12 13:50:21 +0100542#ifdef CONFIG_AMD_IOMMU_STATS
543
544/*
545 * Initialization code for statistics collection
546 */
547
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100548DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100549DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100550DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100551DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100552DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100553DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100554DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100555DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100556DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100557DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100558DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100559DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100560DECLARE_STATS_COUNTER(complete_ppr);
561DECLARE_STATS_COUNTER(invalidate_iotlb);
562DECLARE_STATS_COUNTER(invalidate_iotlb_all);
563DECLARE_STATS_COUNTER(pri_requests);
564
Joerg Roedel7f265082008-12-12 13:50:21 +0100565static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100566static struct dentry *de_fflush;
567
568static void amd_iommu_stats_add(struct __iommu_counter *cnt)
569{
570 if (stats_dir == NULL)
571 return;
572
573 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
574 &cnt->value);
575}
576
577static void amd_iommu_stats_init(void)
578{
579 stats_dir = debugfs_create_dir("amd-iommu", NULL);
580 if (stats_dir == NULL)
581 return;
582
Joerg Roedel7f265082008-12-12 13:50:21 +0100583 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300584 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100585
586 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100587 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100588 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100589 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100590 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100591 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100592 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100593 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100594 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100595 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100596 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100597 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100598 amd_iommu_stats_add(&complete_ppr);
599 amd_iommu_stats_add(&invalidate_iotlb);
600 amd_iommu_stats_add(&invalidate_iotlb_all);
601 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100602}
603
604#endif
605
Joerg Roedel431b2a22008-07-11 17:14:22 +0200606/****************************************************************************
607 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200608 * Interrupt handling functions
609 *
610 ****************************************************************************/
611
Joerg Roedele3e59872009-09-03 14:02:10 +0200612static void dump_dte_entry(u16 devid)
613{
614 int i;
615
Joerg Roedelee6c2862011-11-09 12:06:03 +0100616 for (i = 0; i < 4; ++i)
617 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200618 amd_iommu_dev_table[devid].data[i]);
619}
620
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200621static void dump_command(unsigned long phys_addr)
622{
623 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
624 int i;
625
626 for (i = 0; i < 4; ++i)
627 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
628}
629
Joerg Roedela345b232009-09-03 15:01:43 +0200630static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200631{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200632 int type, devid, domid, flags;
633 volatile u32 *event = __evt;
634 int count = 0;
635 u64 address;
636
637retry:
638 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
639 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
640 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
641 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
642 address = (u64)(((u64)event[3]) << 32) | event[2];
643
644 if (type == 0) {
645 /* Did we hit the erratum? */
646 if (++count == LOOP_TIMEOUT) {
647 pr_err("AMD-Vi: No event written to event log\n");
648 return;
649 }
650 udelay(1);
651 goto retry;
652 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200653
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200654 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200655
656 switch (type) {
657 case EVENT_TYPE_ILL_DEV:
658 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
659 "address=0x%016llx flags=0x%04x]\n",
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);
Joerg Roedele3e59872009-09-03 14:02:10 +0200662 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200663 break;
664 case EVENT_TYPE_IO_FAULT:
665 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
666 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700667 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200668 domid, address, flags);
669 break;
670 case EVENT_TYPE_DEV_TAB_ERR:
671 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
672 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700673 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200674 address, flags);
675 break;
676 case EVENT_TYPE_PAGE_TAB_ERR:
677 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
678 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700679 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200680 domid, address, flags);
681 break;
682 case EVENT_TYPE_ILL_CMD:
683 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200684 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200685 break;
686 case EVENT_TYPE_CMD_HARD_ERR:
687 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
688 "flags=0x%04x]\n", address, flags);
689 break;
690 case EVENT_TYPE_IOTLB_INV_TO:
691 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
692 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700693 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200694 address);
695 break;
696 case EVENT_TYPE_INV_DEV_REQ:
697 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
698 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700699 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200700 address, flags);
701 break;
702 default:
703 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
704 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200705
706 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200707}
708
709static void iommu_poll_events(struct amd_iommu *iommu)
710{
711 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200712
713 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
714 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
715
716 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200717 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200718 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
719 }
720
721 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200722}
723
Joerg Roedeleee53532012-06-01 15:20:23 +0200724static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100725{
726 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100727
Joerg Roedel399be2f2011-12-01 16:53:47 +0100728 INC_STATS_COUNTER(pri_requests);
729
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100730 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
731 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
732 return;
733 }
734
735 fault.address = raw[1];
736 fault.pasid = PPR_PASID(raw[0]);
737 fault.device_id = PPR_DEVID(raw[0]);
738 fault.tag = PPR_TAG(raw[0]);
739 fault.flags = PPR_FLAGS(raw[0]);
740
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100741 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
742}
743
744static void iommu_poll_ppr_log(struct amd_iommu *iommu)
745{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100746 u32 head, tail;
747
748 if (iommu->ppr_log == NULL)
749 return;
750
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100751 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
752 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
753
754 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200755 volatile u64 *raw;
756 u64 entry[2];
757 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100758
Joerg Roedeleee53532012-06-01 15:20:23 +0200759 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100760
Joerg Roedeleee53532012-06-01 15:20:23 +0200761 /*
762 * Hardware bug: Interrupt may arrive before the entry is
763 * written to memory. If this happens we need to wait for the
764 * entry to arrive.
765 */
766 for (i = 0; i < LOOP_TIMEOUT; ++i) {
767 if (PPR_REQ_TYPE(raw[0]) != 0)
768 break;
769 udelay(1);
770 }
771
772 /* Avoid memcpy function-call overhead */
773 entry[0] = raw[0];
774 entry[1] = raw[1];
775
776 /*
777 * To detect the hardware bug we need to clear the entry
778 * back to zero.
779 */
780 raw[0] = raw[1] = 0UL;
781
782 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100783 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
784 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200785
Joerg Roedeleee53532012-06-01 15:20:23 +0200786 /* Handle PPR entry */
787 iommu_handle_ppr_entry(iommu, entry);
788
Joerg Roedeleee53532012-06-01 15:20:23 +0200789 /* Refresh ring-buffer information */
790 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100791 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
792 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100793}
794
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200795irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200796{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500797 struct amd_iommu *iommu = (struct amd_iommu *) data;
798 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200799
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500800 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
801 /* Enable EVT and PPR interrupts again */
802 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
803 iommu->mmio_base + MMIO_STATUS_OFFSET);
804
805 if (status & MMIO_STATUS_EVT_INT_MASK) {
806 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
807 iommu_poll_events(iommu);
808 }
809
810 if (status & MMIO_STATUS_PPR_INT_MASK) {
811 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
812 iommu_poll_ppr_log(iommu);
813 }
814
815 /*
816 * Hardware bug: ERBT1312
817 * When re-enabling interrupt (by writing 1
818 * to clear the bit), the hardware might also try to set
819 * the interrupt bit in the event status register.
820 * In this scenario, the bit will be set, and disable
821 * subsequent interrupts.
822 *
823 * Workaround: The IOMMU driver should read back the
824 * status register and check if the interrupt bits are cleared.
825 * If not, driver will need to go through the interrupt handler
826 * again and re-clear the bits
827 */
828 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100829 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200830 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200831}
832
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200833irqreturn_t amd_iommu_int_handler(int irq, void *data)
834{
835 return IRQ_WAKE_THREAD;
836}
837
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200838/****************************************************************************
839 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200840 * IOMMU command queuing functions
841 *
842 ****************************************************************************/
843
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200844static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200845{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200846 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200847
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200848 while (*sem == 0 && i < LOOP_TIMEOUT) {
849 udelay(1);
850 i += 1;
851 }
852
853 if (i == LOOP_TIMEOUT) {
854 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
855 return -EIO;
856 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200857
858 return 0;
859}
860
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200861static void copy_cmd_to_buffer(struct amd_iommu *iommu,
862 struct iommu_cmd *cmd,
863 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200864{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200865 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200866
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200867 target = iommu->cmd_buf + tail;
868 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200869
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200870 /* Copy command to buffer */
871 memcpy(target, cmd, sizeof(*cmd));
872
873 /* Tell the IOMMU about it */
874 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
875}
876
Joerg Roedel815b33f2011-04-06 17:26:49 +0200877static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200878{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200879 WARN_ON(address & 0x7ULL);
880
Joerg Roedelded46732011-04-06 10:53:48 +0200881 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200882 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
883 cmd->data[1] = upper_32_bits(__pa(address));
884 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200885 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
886}
887
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200888static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
889{
890 memset(cmd, 0, sizeof(*cmd));
891 cmd->data[0] = devid;
892 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
893}
894
Joerg Roedel11b64022011-04-06 11:49:28 +0200895static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
896 size_t size, u16 domid, int pde)
897{
898 u64 pages;
899 int s;
900
901 pages = iommu_num_pages(address, size, PAGE_SIZE);
902 s = 0;
903
904 if (pages > 1) {
905 /*
906 * If we have to flush more than one page, flush all
907 * TLB entries for this domain
908 */
909 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
910 s = 1;
911 }
912
913 address &= PAGE_MASK;
914
915 memset(cmd, 0, sizeof(*cmd));
916 cmd->data[1] |= domid;
917 cmd->data[2] = lower_32_bits(address);
918 cmd->data[3] = upper_32_bits(address);
919 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
920 if (s) /* size bit - we flush more than one 4kb page */
921 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200922 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200923 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
924}
925
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200926static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
927 u64 address, size_t size)
928{
929 u64 pages;
930 int s;
931
932 pages = iommu_num_pages(address, size, PAGE_SIZE);
933 s = 0;
934
935 if (pages > 1) {
936 /*
937 * If we have to flush more than one page, flush all
938 * TLB entries for this domain
939 */
940 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
941 s = 1;
942 }
943
944 address &= PAGE_MASK;
945
946 memset(cmd, 0, sizeof(*cmd));
947 cmd->data[0] = devid;
948 cmd->data[0] |= (qdep & 0xff) << 24;
949 cmd->data[1] = devid;
950 cmd->data[2] = lower_32_bits(address);
951 cmd->data[3] = upper_32_bits(address);
952 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
953 if (s)
954 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
955}
956
Joerg Roedel22e266c2011-11-21 15:59:08 +0100957static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
958 u64 address, bool size)
959{
960 memset(cmd, 0, sizeof(*cmd));
961
962 address &= ~(0xfffULL);
963
964 cmd->data[0] = pasid & PASID_MASK;
965 cmd->data[1] = domid;
966 cmd->data[2] = lower_32_bits(address);
967 cmd->data[3] = upper_32_bits(address);
968 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
969 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
970 if (size)
971 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
972 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
973}
974
975static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
976 int qdep, u64 address, bool size)
977{
978 memset(cmd, 0, sizeof(*cmd));
979
980 address &= ~(0xfffULL);
981
982 cmd->data[0] = devid;
983 cmd->data[0] |= (pasid & 0xff) << 16;
984 cmd->data[0] |= (qdep & 0xff) << 24;
985 cmd->data[1] = devid;
986 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
987 cmd->data[2] = lower_32_bits(address);
988 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
989 cmd->data[3] = upper_32_bits(address);
990 if (size)
991 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
992 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
993}
994
Joerg Roedelc99afa22011-11-21 18:19:25 +0100995static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
996 int status, int tag, bool gn)
997{
998 memset(cmd, 0, sizeof(*cmd));
999
1000 cmd->data[0] = devid;
1001 if (gn) {
1002 cmd->data[1] = pasid & PASID_MASK;
1003 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1004 }
1005 cmd->data[3] = tag & 0x1ff;
1006 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1007
1008 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1009}
1010
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001011static void build_inv_all(struct iommu_cmd *cmd)
1012{
1013 memset(cmd, 0, sizeof(*cmd));
1014 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001015}
1016
Joerg Roedel7ef27982012-06-21 16:46:04 +02001017static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1018{
1019 memset(cmd, 0, sizeof(*cmd));
1020 cmd->data[0] = devid;
1021 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1022}
1023
Joerg Roedel431b2a22008-07-11 17:14:22 +02001024/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001025 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001026 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001027 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001028static int iommu_queue_command_sync(struct amd_iommu *iommu,
1029 struct iommu_cmd *cmd,
1030 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001031{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001032 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +02001033 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001034
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001035 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +01001036
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001037again:
Joerg Roedel815b33f2011-04-06 17:26:49 +02001038 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001039
1040 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1041 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1042 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
1043 left = (head - next_tail) % iommu->cmd_buf_size;
1044
1045 if (left <= 2) {
1046 struct iommu_cmd sync_cmd;
1047 volatile u64 sem = 0;
1048 int ret;
1049
1050 build_completion_wait(&sync_cmd, (u64)&sem);
1051 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1052
1053 spin_unlock_irqrestore(&iommu->lock, flags);
1054
1055 if ((ret = wait_on_sem(&sem)) != 0)
1056 return ret;
1057
1058 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001059 }
1060
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001061 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001062
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001063 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001064 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001065
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001066 spin_unlock_irqrestore(&iommu->lock, flags);
1067
Joerg Roedel815b33f2011-04-06 17:26:49 +02001068 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001069}
1070
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001071static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1072{
1073 return iommu_queue_command_sync(iommu, cmd, true);
1074}
1075
Joerg Roedel8d201962008-12-02 20:34:41 +01001076/*
1077 * This function queues a completion wait command into the command
1078 * buffer of an IOMMU
1079 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001080static int iommu_completion_wait(struct amd_iommu *iommu)
1081{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001082 struct iommu_cmd cmd;
1083 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001084 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001085
1086 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001087 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001088
Joerg Roedel815b33f2011-04-06 17:26:49 +02001089 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001090
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001091 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001092 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001093 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001094
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001095 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001096}
1097
Joerg Roedeld8c13082011-04-06 18:51:26 +02001098static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001099{
1100 struct iommu_cmd cmd;
1101
Joerg Roedeld8c13082011-04-06 18:51:26 +02001102 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001103
Joerg Roedeld8c13082011-04-06 18:51:26 +02001104 return iommu_queue_command(iommu, &cmd);
1105}
1106
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001107static void iommu_flush_dte_all(struct amd_iommu *iommu)
1108{
1109 u32 devid;
1110
1111 for (devid = 0; devid <= 0xffff; ++devid)
1112 iommu_flush_dte(iommu, devid);
1113
1114 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001115}
1116
1117/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001118 * This function uses heavy locking and may disable irqs for some time. But
1119 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001120 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001121static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001122{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001123 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001124
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001125 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1126 struct iommu_cmd cmd;
1127 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1128 dom_id, 1);
1129 iommu_queue_command(iommu, &cmd);
1130 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001131
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001132 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001133}
1134
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001135static void iommu_flush_all(struct amd_iommu *iommu)
1136{
1137 struct iommu_cmd cmd;
1138
1139 build_inv_all(&cmd);
1140
1141 iommu_queue_command(iommu, &cmd);
1142 iommu_completion_wait(iommu);
1143}
1144
Joerg Roedel7ef27982012-06-21 16:46:04 +02001145static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1146{
1147 struct iommu_cmd cmd;
1148
1149 build_inv_irt(&cmd, devid);
1150
1151 iommu_queue_command(iommu, &cmd);
1152}
1153
1154static void iommu_flush_irt_all(struct amd_iommu *iommu)
1155{
1156 u32 devid;
1157
1158 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1159 iommu_flush_irt(iommu, devid);
1160
1161 iommu_completion_wait(iommu);
1162}
1163
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001164void iommu_flush_all_caches(struct amd_iommu *iommu)
1165{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001166 if (iommu_feature(iommu, FEATURE_IA)) {
1167 iommu_flush_all(iommu);
1168 } else {
1169 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001170 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001171 iommu_flush_tlb_all(iommu);
1172 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001173}
1174
Joerg Roedel431b2a22008-07-11 17:14:22 +02001175/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001176 * Command send function for flushing on-device TLB
1177 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001178static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1179 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001180{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001181 struct amd_iommu *iommu;
1182 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001183 int qdep;
1184
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001185 qdep = dev_data->ats.qdep;
1186 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001187
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001188 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001189
1190 return iommu_queue_command(iommu, &cmd);
1191}
1192
1193/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001194 * Command send function for invalidating a device table entry
1195 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001196static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001197{
1198 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001199 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001200
Joerg Roedel6c542042011-06-09 17:07:31 +02001201 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001202
Joerg Roedelf62dda62011-06-09 12:55:35 +02001203 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001204 if (ret)
1205 return ret;
1206
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001207 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001208 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001209
1210 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001211}
1212
Joerg Roedel431b2a22008-07-11 17:14:22 +02001213/*
1214 * TLB invalidation function which is called from the mapping functions.
1215 * It invalidates a single PTE if the range to flush is within a single
1216 * page. Otherwise it flushes the whole TLB of the IOMMU.
1217 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001218static void __domain_flush_pages(struct protection_domain *domain,
1219 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001220{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001221 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001222 struct iommu_cmd cmd;
1223 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001224
Joerg Roedel11b64022011-04-06 11:49:28 +02001225 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001226
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001227 for (i = 0; i < amd_iommus_present; ++i) {
1228 if (!domain->dev_iommu[i])
1229 continue;
1230
1231 /*
1232 * Devices of this domain are behind this IOMMU
1233 * We need a TLB flush
1234 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001235 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001236 }
1237
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001238 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001239
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001240 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001241 continue;
1242
Joerg Roedel6c542042011-06-09 17:07:31 +02001243 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001244 }
1245
Joerg Roedel11b64022011-04-06 11:49:28 +02001246 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001247}
1248
Joerg Roedel17b124b2011-04-06 18:01:35 +02001249static void domain_flush_pages(struct protection_domain *domain,
1250 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001251{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001252 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001253}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001254
Joerg Roedel1c655772008-09-04 18:40:05 +02001255/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001256static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001257{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001258 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001259}
1260
Chris Wright42a49f92009-06-15 15:42:00 +02001261/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001262static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001263{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001264 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1265}
1266
1267static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001268{
1269 int i;
1270
1271 for (i = 0; i < amd_iommus_present; ++i) {
1272 if (!domain->dev_iommu[i])
1273 continue;
1274
1275 /*
1276 * Devices of this domain are behind this IOMMU
1277 * We need to wait for completion of all commands.
1278 */
1279 iommu_completion_wait(amd_iommus[i]);
1280 }
1281}
1282
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001283
Joerg Roedel43f49602008-12-02 21:01:12 +01001284/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001285 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001286 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001287static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001288{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001289 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001290
1291 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001292 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001293}
1294
Joerg Roedel431b2a22008-07-11 17:14:22 +02001295/****************************************************************************
1296 *
1297 * The functions below are used the create the page table mappings for
1298 * unity mapped regions.
1299 *
1300 ****************************************************************************/
1301
1302/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001303 * This function is used to add another level to an IO page table. Adding
1304 * another level increases the size of the address space by 9 bits to a size up
1305 * to 64 bits.
1306 */
1307static bool increase_address_space(struct protection_domain *domain,
1308 gfp_t gfp)
1309{
1310 u64 *pte;
1311
1312 if (domain->mode == PAGE_MODE_6_LEVEL)
1313 /* address space already 64 bit large */
1314 return false;
1315
1316 pte = (void *)get_zeroed_page(gfp);
1317 if (!pte)
1318 return false;
1319
1320 *pte = PM_LEVEL_PDE(domain->mode,
1321 virt_to_phys(domain->pt_root));
1322 domain->pt_root = pte;
1323 domain->mode += 1;
1324 domain->updated = true;
1325
1326 return true;
1327}
1328
1329static u64 *alloc_pte(struct protection_domain *domain,
1330 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001331 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001332 u64 **pte_page,
1333 gfp_t gfp)
1334{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001335 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001336 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001337
1338 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001339
1340 while (address > PM_LEVEL_SIZE(domain->mode))
1341 increase_address_space(domain, gfp);
1342
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001343 level = domain->mode - 1;
1344 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1345 address = PAGE_SIZE_ALIGN(address, page_size);
1346 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001347
1348 while (level > end_lvl) {
1349 if (!IOMMU_PTE_PRESENT(*pte)) {
1350 page = (u64 *)get_zeroed_page(gfp);
1351 if (!page)
1352 return NULL;
1353 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1354 }
1355
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001356 /* No level skipping support yet */
1357 if (PM_PTE_LEVEL(*pte) != level)
1358 return NULL;
1359
Joerg Roedel308973d2009-11-24 17:43:32 +01001360 level -= 1;
1361
1362 pte = IOMMU_PTE_PAGE(*pte);
1363
1364 if (pte_page && level == end_lvl)
1365 *pte_page = pte;
1366
1367 pte = &pte[PM_LEVEL_INDEX(level, address)];
1368 }
1369
1370 return pte;
1371}
1372
1373/*
1374 * This function checks if there is a PTE for a given dma address. If
1375 * there is one, it returns the pointer to it.
1376 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001377static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001378{
1379 int level;
1380 u64 *pte;
1381
Joerg Roedel24cd7722010-01-19 17:27:39 +01001382 if (address > PM_LEVEL_SIZE(domain->mode))
1383 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001384
Joerg Roedel24cd7722010-01-19 17:27:39 +01001385 level = domain->mode - 1;
1386 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1387
1388 while (level > 0) {
1389
1390 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001391 if (!IOMMU_PTE_PRESENT(*pte))
1392 return NULL;
1393
Joerg Roedel24cd7722010-01-19 17:27:39 +01001394 /* Large PTE */
1395 if (PM_PTE_LEVEL(*pte) == 0x07) {
1396 unsigned long pte_mask, __pte;
1397
1398 /*
1399 * If we have a series of large PTEs, make
1400 * sure to return a pointer to the first one.
1401 */
1402 pte_mask = PTE_PAGE_SIZE(*pte);
1403 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1404 __pte = ((unsigned long)pte) & pte_mask;
1405
1406 return (u64 *)__pte;
1407 }
1408
1409 /* No level skipping support yet */
1410 if (PM_PTE_LEVEL(*pte) != level)
1411 return NULL;
1412
Joerg Roedel308973d2009-11-24 17:43:32 +01001413 level -= 1;
1414
Joerg Roedel24cd7722010-01-19 17:27:39 +01001415 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001416 pte = IOMMU_PTE_PAGE(*pte);
1417 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001418 }
1419
1420 return pte;
1421}
1422
1423/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001424 * Generic mapping functions. It maps a physical address into a DMA
1425 * address space. It allocates the page table pages if necessary.
1426 * In the future it can be extended to a generic mapping function
1427 * supporting all features of AMD IOMMU page tables like level skipping
1428 * and full 64 bit address spaces.
1429 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001430static int iommu_map_page(struct protection_domain *dom,
1431 unsigned long bus_addr,
1432 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001433 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001434 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001435{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001436 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001437 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001438
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001439 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001440 return -EINVAL;
1441
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001442 bus_addr = PAGE_ALIGN(bus_addr);
1443 phys_addr = PAGE_ALIGN(phys_addr);
1444 count = PAGE_SIZE_PTE_COUNT(page_size);
1445 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001446
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001447 for (i = 0; i < count; ++i)
1448 if (IOMMU_PTE_PRESENT(pte[i]))
1449 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001450
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001451 if (page_size > PAGE_SIZE) {
1452 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1453 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1454 } else
1455 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1456
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001457 if (prot & IOMMU_PROT_IR)
1458 __pte |= IOMMU_PTE_IR;
1459 if (prot & IOMMU_PROT_IW)
1460 __pte |= IOMMU_PTE_IW;
1461
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001462 for (i = 0; i < count; ++i)
1463 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001464
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001465 update_domain(dom);
1466
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001467 return 0;
1468}
1469
Joerg Roedel24cd7722010-01-19 17:27:39 +01001470static unsigned long iommu_unmap_page(struct protection_domain *dom,
1471 unsigned long bus_addr,
1472 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001473{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001474 unsigned long long unmap_size, unmapped;
1475 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001476
Joerg Roedel24cd7722010-01-19 17:27:39 +01001477 BUG_ON(!is_power_of_2(page_size));
1478
1479 unmapped = 0;
1480
1481 while (unmapped < page_size) {
1482
1483 pte = fetch_pte(dom, bus_addr);
1484
1485 if (!pte) {
1486 /*
1487 * No PTE for this address
1488 * move forward in 4kb steps
1489 */
1490 unmap_size = PAGE_SIZE;
1491 } else if (PM_PTE_LEVEL(*pte) == 0) {
1492 /* 4kb PTE found for this address */
1493 unmap_size = PAGE_SIZE;
1494 *pte = 0ULL;
1495 } else {
1496 int count, i;
1497
1498 /* Large PTE found which maps this address */
1499 unmap_size = PTE_PAGE_SIZE(*pte);
1500 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1501 for (i = 0; i < count; i++)
1502 pte[i] = 0ULL;
1503 }
1504
1505 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1506 unmapped += unmap_size;
1507 }
1508
1509 BUG_ON(!is_power_of_2(unmapped));
1510
1511 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001512}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001513
Joerg Roedel431b2a22008-07-11 17:14:22 +02001514/*
1515 * This function checks if a specific unity mapping entry is needed for
1516 * this specific IOMMU.
1517 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001518static int iommu_for_unity_map(struct amd_iommu *iommu,
1519 struct unity_map_entry *entry)
1520{
1521 u16 bdf, i;
1522
1523 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1524 bdf = amd_iommu_alias_table[i];
1525 if (amd_iommu_rlookup_table[bdf] == iommu)
1526 return 1;
1527 }
1528
1529 return 0;
1530}
1531
Joerg Roedel431b2a22008-07-11 17:14:22 +02001532/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001533 * This function actually applies the mapping to the page table of the
1534 * dma_ops domain.
1535 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001536static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1537 struct unity_map_entry *e)
1538{
1539 u64 addr;
1540 int ret;
1541
1542 for (addr = e->address_start; addr < e->address_end;
1543 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001544 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001545 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001546 if (ret)
1547 return ret;
1548 /*
1549 * if unity mapping is in aperture range mark the page
1550 * as allocated in the aperture
1551 */
1552 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001553 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001554 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001555 }
1556
1557 return 0;
1558}
1559
Joerg Roedel431b2a22008-07-11 17:14:22 +02001560/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001561 * Init the unity mappings for a specific IOMMU in the system
1562 *
1563 * Basically iterates over all unity mapping entries and applies them to
1564 * the default domain DMA of that IOMMU if necessary.
1565 */
1566static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1567{
1568 struct unity_map_entry *entry;
1569 int ret;
1570
1571 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1572 if (!iommu_for_unity_map(iommu, entry))
1573 continue;
1574 ret = dma_ops_unity_map(iommu->default_dom, entry);
1575 if (ret)
1576 return ret;
1577 }
1578
1579 return 0;
1580}
1581
1582/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001583 * Inits the unity mappings required for a specific device
1584 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001585static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1586 u16 devid)
1587{
1588 struct unity_map_entry *e;
1589 int ret;
1590
1591 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1592 if (!(devid >= e->devid_start && devid <= e->devid_end))
1593 continue;
1594 ret = dma_ops_unity_map(dma_dom, e);
1595 if (ret)
1596 return ret;
1597 }
1598
1599 return 0;
1600}
1601
Joerg Roedel431b2a22008-07-11 17:14:22 +02001602/****************************************************************************
1603 *
1604 * The next functions belong to the address allocator for the dma_ops
1605 * interface functions. They work like the allocators in the other IOMMU
1606 * drivers. Its basically a bitmap which marks the allocated pages in
1607 * the aperture. Maybe it could be enhanced in the future to a more
1608 * efficient allocator.
1609 *
1610 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001611
Joerg Roedel431b2a22008-07-11 17:14:22 +02001612/*
Joerg Roedel384de722009-05-15 12:30:05 +02001613 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001614 *
1615 * called with domain->lock held
1616 */
Joerg Roedel384de722009-05-15 12:30:05 +02001617
Joerg Roedel9cabe892009-05-18 16:38:55 +02001618/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001619 * Used to reserve address ranges in the aperture (e.g. for exclusion
1620 * ranges.
1621 */
1622static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1623 unsigned long start_page,
1624 unsigned int pages)
1625{
1626 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1627
1628 if (start_page + pages > last_page)
1629 pages = last_page - start_page;
1630
1631 for (i = start_page; i < start_page + pages; ++i) {
1632 int index = i / APERTURE_RANGE_PAGES;
1633 int page = i % APERTURE_RANGE_PAGES;
1634 __set_bit(page, dom->aperture[index]->bitmap);
1635 }
1636}
1637
1638/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001639 * This function is used to add a new aperture range to an existing
1640 * aperture in case of dma_ops domain allocation or address allocation
1641 * failure.
1642 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001643static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001644 bool populate, gfp_t gfp)
1645{
1646 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001647 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001648 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001649
Joerg Roedelf5e97052009-05-22 12:31:53 +02001650#ifdef CONFIG_IOMMU_STRESS
1651 populate = false;
1652#endif
1653
Joerg Roedel9cabe892009-05-18 16:38:55 +02001654 if (index >= APERTURE_MAX_RANGES)
1655 return -ENOMEM;
1656
1657 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1658 if (!dma_dom->aperture[index])
1659 return -ENOMEM;
1660
1661 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1662 if (!dma_dom->aperture[index]->bitmap)
1663 goto out_free;
1664
1665 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1666
1667 if (populate) {
1668 unsigned long address = dma_dom->aperture_size;
1669 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1670 u64 *pte, *pte_page;
1671
1672 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001673 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001674 &pte_page, gfp);
1675 if (!pte)
1676 goto out_free;
1677
1678 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1679
1680 address += APERTURE_RANGE_SIZE / 64;
1681 }
1682 }
1683
Joerg Roedel17f5b562011-07-06 17:14:44 +02001684 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001685 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1686
Joerg Roedel17f5b562011-07-06 17:14:44 +02001687 /* Reserve address range used for MSI messages */
1688 if (old_size < MSI_ADDR_BASE_LO &&
1689 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1690 unsigned long spage;
1691 int pages;
1692
1693 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1694 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1695
1696 dma_ops_reserve_addresses(dma_dom, spage, pages);
1697 }
1698
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001699 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001700 for_each_iommu(iommu) {
1701 if (iommu->exclusion_start &&
1702 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1703 && iommu->exclusion_start < dma_dom->aperture_size) {
1704 unsigned long startpage;
1705 int pages = iommu_num_pages(iommu->exclusion_start,
1706 iommu->exclusion_length,
1707 PAGE_SIZE);
1708 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1709 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1710 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001711 }
1712
1713 /*
1714 * Check for areas already mapped as present in the new aperture
1715 * range and mark those pages as reserved in the allocator. Such
1716 * mappings may already exist as a result of requested unity
1717 * mappings for devices.
1718 */
1719 for (i = dma_dom->aperture[index]->offset;
1720 i < dma_dom->aperture_size;
1721 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001722 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001723 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1724 continue;
1725
Joerg Roedelfcd08612011-10-11 17:41:32 +02001726 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001727 }
1728
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001729 update_domain(&dma_dom->domain);
1730
Joerg Roedel9cabe892009-05-18 16:38:55 +02001731 return 0;
1732
1733out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001734 update_domain(&dma_dom->domain);
1735
Joerg Roedel9cabe892009-05-18 16:38:55 +02001736 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1737
1738 kfree(dma_dom->aperture[index]);
1739 dma_dom->aperture[index] = NULL;
1740
1741 return -ENOMEM;
1742}
1743
Joerg Roedel384de722009-05-15 12:30:05 +02001744static unsigned long dma_ops_area_alloc(struct device *dev,
1745 struct dma_ops_domain *dom,
1746 unsigned int pages,
1747 unsigned long align_mask,
1748 u64 dma_mask,
1749 unsigned long start)
1750{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001751 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001752 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1753 int i = start >> APERTURE_RANGE_SHIFT;
1754 unsigned long boundary_size;
1755 unsigned long address = -1;
1756 unsigned long limit;
1757
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001758 next_bit >>= PAGE_SHIFT;
1759
Joerg Roedel384de722009-05-15 12:30:05 +02001760 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1761 PAGE_SIZE) >> PAGE_SHIFT;
1762
1763 for (;i < max_index; ++i) {
1764 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1765
1766 if (dom->aperture[i]->offset >= dma_mask)
1767 break;
1768
1769 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1770 dma_mask >> PAGE_SHIFT);
1771
1772 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1773 limit, next_bit, pages, 0,
1774 boundary_size, align_mask);
1775 if (address != -1) {
1776 address = dom->aperture[i]->offset +
1777 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001778 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001779 break;
1780 }
1781
1782 next_bit = 0;
1783 }
1784
1785 return address;
1786}
1787
Joerg Roedeld3086442008-06-26 21:27:57 +02001788static unsigned long dma_ops_alloc_addresses(struct device *dev,
1789 struct dma_ops_domain *dom,
Joerg Roedel6d4f343f2008-09-04 19:18:02 +02001790 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001791 unsigned long align_mask,
1792 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001793{
Joerg Roedeld3086442008-06-26 21:27:57 +02001794 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001795
Joerg Roedelfe16f082009-05-22 12:27:53 +02001796#ifdef CONFIG_IOMMU_STRESS
1797 dom->next_address = 0;
1798 dom->need_flush = true;
1799#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001800
Joerg Roedel384de722009-05-15 12:30:05 +02001801 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001802 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001803
Joerg Roedel1c655772008-09-04 18:40:05 +02001804 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001805 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001806 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1807 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001808 dom->need_flush = true;
1809 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001810
Joerg Roedel384de722009-05-15 12:30:05 +02001811 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001812 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001813
1814 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1815
1816 return address;
1817}
1818
Joerg Roedel431b2a22008-07-11 17:14:22 +02001819/*
1820 * The address free function.
1821 *
1822 * called with domain->lock held
1823 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001824static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1825 unsigned long address,
1826 unsigned int pages)
1827{
Joerg Roedel384de722009-05-15 12:30:05 +02001828 unsigned i = address >> APERTURE_RANGE_SHIFT;
1829 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001830
Joerg Roedel384de722009-05-15 12:30:05 +02001831 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1832
Joerg Roedel47bccd62009-05-22 12:40:54 +02001833#ifdef CONFIG_IOMMU_STRESS
1834 if (i < 4)
1835 return;
1836#endif
1837
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001838 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001839 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001840
1841 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001842
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001843 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001844
Joerg Roedeld3086442008-06-26 21:27:57 +02001845}
1846
Joerg Roedel431b2a22008-07-11 17:14:22 +02001847/****************************************************************************
1848 *
1849 * The next functions belong to the domain allocation. A domain is
1850 * allocated for every IOMMU as the default domain. If device isolation
1851 * is enabled, every device get its own domain. The most important thing
1852 * about domains is the page table mapping the DMA address space they
1853 * contain.
1854 *
1855 ****************************************************************************/
1856
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001857/*
1858 * This function adds a protection domain to the global protection domain list
1859 */
1860static void add_domain_to_list(struct protection_domain *domain)
1861{
1862 unsigned long flags;
1863
1864 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1865 list_add(&domain->list, &amd_iommu_pd_list);
1866 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1867}
1868
1869/*
1870 * This function removes a protection domain to the global
1871 * protection domain list
1872 */
1873static void del_domain_from_list(struct protection_domain *domain)
1874{
1875 unsigned long flags;
1876
1877 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1878 list_del(&domain->list);
1879 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1880}
1881
Joerg Roedelec487d12008-06-26 21:27:58 +02001882static u16 domain_id_alloc(void)
1883{
1884 unsigned long flags;
1885 int id;
1886
1887 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1888 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1889 BUG_ON(id == 0);
1890 if (id > 0 && id < MAX_DOMAIN_ID)
1891 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1892 else
1893 id = 0;
1894 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1895
1896 return id;
1897}
1898
Joerg Roedela2acfb72008-12-02 18:28:53 +01001899static void domain_id_free(int id)
1900{
1901 unsigned long flags;
1902
1903 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1904 if (id > 0 && id < MAX_DOMAIN_ID)
1905 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1906 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1907}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001908
Joerg Roedel86db2e52008-12-02 18:20:21 +01001909static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001910{
1911 int i, j;
1912 u64 *p1, *p2, *p3;
1913
Joerg Roedel86db2e52008-12-02 18:20:21 +01001914 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001915
1916 if (!p1)
1917 return;
1918
1919 for (i = 0; i < 512; ++i) {
1920 if (!IOMMU_PTE_PRESENT(p1[i]))
1921 continue;
1922
1923 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001924 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001925 if (!IOMMU_PTE_PRESENT(p2[j]))
1926 continue;
1927 p3 = IOMMU_PTE_PAGE(p2[j]);
1928 free_page((unsigned long)p3);
1929 }
1930
1931 free_page((unsigned long)p2);
1932 }
1933
1934 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001935
1936 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001937}
1938
Joerg Roedelb16137b2011-11-21 16:50:23 +01001939static void free_gcr3_tbl_level1(u64 *tbl)
1940{
1941 u64 *ptr;
1942 int i;
1943
1944 for (i = 0; i < 512; ++i) {
1945 if (!(tbl[i] & GCR3_VALID))
1946 continue;
1947
1948 ptr = __va(tbl[i] & PAGE_MASK);
1949
1950 free_page((unsigned long)ptr);
1951 }
1952}
1953
1954static void free_gcr3_tbl_level2(u64 *tbl)
1955{
1956 u64 *ptr;
1957 int i;
1958
1959 for (i = 0; i < 512; ++i) {
1960 if (!(tbl[i] & GCR3_VALID))
1961 continue;
1962
1963 ptr = __va(tbl[i] & PAGE_MASK);
1964
1965 free_gcr3_tbl_level1(ptr);
1966 }
1967}
1968
Joerg Roedel52815b72011-11-17 17:24:28 +01001969static void free_gcr3_table(struct protection_domain *domain)
1970{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001971 if (domain->glx == 2)
1972 free_gcr3_tbl_level2(domain->gcr3_tbl);
1973 else if (domain->glx == 1)
1974 free_gcr3_tbl_level1(domain->gcr3_tbl);
1975 else if (domain->glx != 0)
1976 BUG();
1977
Joerg Roedel52815b72011-11-17 17:24:28 +01001978 free_page((unsigned long)domain->gcr3_tbl);
1979}
1980
Joerg Roedel431b2a22008-07-11 17:14:22 +02001981/*
1982 * Free a domain, only used if something went wrong in the
1983 * allocation path and we need to free an already allocated page table
1984 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001985static void dma_ops_domain_free(struct dma_ops_domain *dom)
1986{
Joerg Roedel384de722009-05-15 12:30:05 +02001987 int i;
1988
Joerg Roedelec487d12008-06-26 21:27:58 +02001989 if (!dom)
1990 return;
1991
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001992 del_domain_from_list(&dom->domain);
1993
Joerg Roedel86db2e52008-12-02 18:20:21 +01001994 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001995
Joerg Roedel384de722009-05-15 12:30:05 +02001996 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1997 if (!dom->aperture[i])
1998 continue;
1999 free_page((unsigned long)dom->aperture[i]->bitmap);
2000 kfree(dom->aperture[i]);
2001 }
Joerg Roedelec487d12008-06-26 21:27:58 +02002002
2003 kfree(dom);
2004}
2005
Joerg Roedel431b2a22008-07-11 17:14:22 +02002006/*
2007 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002008 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02002009 * structures required for the dma_ops interface
2010 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01002011static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02002012{
2013 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002014
2015 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
2016 if (!dma_dom)
2017 return NULL;
2018
2019 spin_lock_init(&dma_dom->domain.lock);
2020
2021 dma_dom->domain.id = domain_id_alloc();
2022 if (dma_dom->domain.id == 0)
2023 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002024 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002025 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002026 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002027 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002028 dma_dom->domain.priv = dma_dom;
2029 if (!dma_dom->domain.pt_root)
2030 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002031
Joerg Roedel1c655772008-09-04 18:40:05 +02002032 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002033 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002034
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002035 add_domain_to_list(&dma_dom->domain);
2036
Joerg Roedel576175c2009-11-23 19:08:46 +01002037 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002038 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002039
Joerg Roedel431b2a22008-07-11 17:14:22 +02002040 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002041 * mark the first page as allocated so we never return 0 as
2042 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002043 */
Joerg Roedel384de722009-05-15 12:30:05 +02002044 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02002045 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002046
Joerg Roedelec487d12008-06-26 21:27:58 +02002047
2048 return dma_dom;
2049
2050free_dma_dom:
2051 dma_ops_domain_free(dma_dom);
2052
2053 return NULL;
2054}
2055
Joerg Roedel431b2a22008-07-11 17:14:22 +02002056/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002057 * little helper function to check whether a given protection domain is a
2058 * dma_ops domain
2059 */
2060static bool dma_ops_domain(struct protection_domain *domain)
2061{
2062 return domain->flags & PD_DMA_OPS_MASK;
2063}
2064
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002065static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002066{
Joerg Roedel132bd682011-11-17 14:18:46 +01002067 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002068 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002069
Joerg Roedel132bd682011-11-17 14:18:46 +01002070 if (domain->mode != PAGE_MODE_NONE)
2071 pte_root = virt_to_phys(domain->pt_root);
2072
Joerg Roedel38ddf412008-09-11 10:38:32 +02002073 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2074 << DEV_ENTRY_MODE_SHIFT;
2075 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002076
Joerg Roedelee6c2862011-11-09 12:06:03 +01002077 flags = amd_iommu_dev_table[devid].data[1];
2078
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002079 if (ats)
2080 flags |= DTE_FLAG_IOTLB;
2081
Joerg Roedel52815b72011-11-17 17:24:28 +01002082 if (domain->flags & PD_IOMMUV2_MASK) {
2083 u64 gcr3 = __pa(domain->gcr3_tbl);
2084 u64 glx = domain->glx;
2085 u64 tmp;
2086
2087 pte_root |= DTE_FLAG_GV;
2088 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2089
2090 /* First mask out possible old values for GCR3 table */
2091 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2092 flags &= ~tmp;
2093
2094 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2095 flags &= ~tmp;
2096
2097 /* Encode GCR3 table into DTE */
2098 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2099 pte_root |= tmp;
2100
2101 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2102 flags |= tmp;
2103
2104 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2105 flags |= tmp;
2106 }
2107
Joerg Roedelee6c2862011-11-09 12:06:03 +01002108 flags &= ~(0xffffUL);
2109 flags |= domain->id;
2110
2111 amd_iommu_dev_table[devid].data[1] = flags;
2112 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002113}
2114
Joerg Roedel15898bb2009-11-24 15:39:42 +01002115static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002116{
Joerg Roedel355bf552008-12-08 12:02:41 +01002117 /* remove entry from the device table seen by the hardware */
2118 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2119 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002120
Joerg Roedelc5cca142009-10-09 18:31:20 +02002121 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002122}
2123
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002124static void do_attach(struct iommu_dev_data *dev_data,
2125 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002126{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002127 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002128 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002129
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002130 iommu = amd_iommu_rlookup_table[dev_data->devid];
2131 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002132
2133 /* Update data structures */
2134 dev_data->domain = domain;
2135 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002136 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002137
2138 /* Do reference counting */
2139 domain->dev_iommu[iommu->index] += 1;
2140 domain->dev_cnt += 1;
2141
2142 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002143 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002144}
2145
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002146static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002147{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002148 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002149
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002150 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002151
Joerg Roedelc4596112009-11-20 14:57:32 +01002152 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002153 dev_data->domain->dev_iommu[iommu->index] -= 1;
2154 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002155
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002156 /* Update data structures */
2157 dev_data->domain = NULL;
2158 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002159 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002160
2161 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002162 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002163}
2164
2165/*
2166 * If a device is not yet associated with a domain, this function does
2167 * assigns it visible for the hardware
2168 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002169static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002170 struct protection_domain *domain)
2171{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002172 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002173
Joerg Roedel15898bb2009-11-24 15:39:42 +01002174 /* lock domain */
2175 spin_lock(&domain->lock);
2176
Joerg Roedel71f77582011-06-09 19:03:15 +02002177 if (dev_data->alias_data != NULL) {
2178 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002179
Joerg Roedel2b02b092011-06-09 17:48:39 +02002180 /* Some sanity checks */
2181 ret = -EBUSY;
2182 if (alias_data->domain != NULL &&
2183 alias_data->domain != domain)
2184 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002185
Joerg Roedel2b02b092011-06-09 17:48:39 +02002186 if (dev_data->domain != NULL &&
2187 dev_data->domain != domain)
2188 goto out_unlock;
2189
2190 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002191 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002192 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002193
2194 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002195 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002196
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002197 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002198 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002199
Joerg Roedel24100052009-11-25 15:59:57 +01002200 atomic_inc(&dev_data->bind);
2201
Julia Lawall84fe6c12010-05-27 12:31:51 +02002202 ret = 0;
2203
2204out_unlock:
2205
Joerg Roedel355bf552008-12-08 12:02:41 +01002206 /* ready */
2207 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002208
Julia Lawall84fe6c12010-05-27 12:31:51 +02002209 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002210}
2211
Joerg Roedel52815b72011-11-17 17:24:28 +01002212
2213static void pdev_iommuv2_disable(struct pci_dev *pdev)
2214{
2215 pci_disable_ats(pdev);
2216 pci_disable_pri(pdev);
2217 pci_disable_pasid(pdev);
2218}
2219
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002220/* FIXME: Change generic reset-function to do the same */
2221static int pri_reset_while_enabled(struct pci_dev *pdev)
2222{
2223 u16 control;
2224 int pos;
2225
Joerg Roedel46277b72011-12-07 14:34:02 +01002226 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002227 if (!pos)
2228 return -EINVAL;
2229
Joerg Roedel46277b72011-12-07 14:34:02 +01002230 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2231 control |= PCI_PRI_CTRL_RESET;
2232 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002233
2234 return 0;
2235}
2236
Joerg Roedel52815b72011-11-17 17:24:28 +01002237static int pdev_iommuv2_enable(struct pci_dev *pdev)
2238{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002239 bool reset_enable;
2240 int reqs, ret;
2241
2242 /* FIXME: Hardcode number of outstanding requests for now */
2243 reqs = 32;
2244 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2245 reqs = 1;
2246 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002247
2248 /* Only allow access to user-accessible pages */
2249 ret = pci_enable_pasid(pdev, 0);
2250 if (ret)
2251 goto out_err;
2252
2253 /* First reset the PRI state of the device */
2254 ret = pci_reset_pri(pdev);
2255 if (ret)
2256 goto out_err;
2257
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002258 /* Enable PRI */
2259 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002260 if (ret)
2261 goto out_err;
2262
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002263 if (reset_enable) {
2264 ret = pri_reset_while_enabled(pdev);
2265 if (ret)
2266 goto out_err;
2267 }
2268
Joerg Roedel52815b72011-11-17 17:24:28 +01002269 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2270 if (ret)
2271 goto out_err;
2272
2273 return 0;
2274
2275out_err:
2276 pci_disable_pri(pdev);
2277 pci_disable_pasid(pdev);
2278
2279 return ret;
2280}
2281
Joerg Roedelc99afa22011-11-21 18:19:25 +01002282/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002283#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002284
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002285static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002286{
Joerg Roedela3b93122012-04-12 12:49:26 +02002287 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002288 int pos;
2289
Joerg Roedel46277b72011-12-07 14:34:02 +01002290 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002291 if (!pos)
2292 return false;
2293
Joerg Roedela3b93122012-04-12 12:49:26 +02002294 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002295
Joerg Roedela3b93122012-04-12 12:49:26 +02002296 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002297}
2298
Joerg Roedel15898bb2009-11-24 15:39:42 +01002299/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002300 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002301 * assigns it visible for the hardware
2302 */
2303static int attach_device(struct device *dev,
2304 struct protection_domain *domain)
2305{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002306 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002307 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002308 unsigned long flags;
2309 int ret;
2310
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002311 dev_data = get_dev_data(dev);
2312
Joerg Roedel52815b72011-11-17 17:24:28 +01002313 if (domain->flags & PD_IOMMUV2_MASK) {
2314 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2315 return -EINVAL;
2316
2317 if (pdev_iommuv2_enable(pdev) != 0)
2318 return -EINVAL;
2319
2320 dev_data->ats.enabled = true;
2321 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002322 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002323 } else if (amd_iommu_iotlb_sup &&
2324 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002325 dev_data->ats.enabled = true;
2326 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2327 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002328
Joerg Roedel15898bb2009-11-24 15:39:42 +01002329 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002330 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002331 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2332
2333 /*
2334 * We might boot into a crash-kernel here. The crashed kernel
2335 * left the caches in the IOMMU dirty. So we have to flush
2336 * here to evict all dirty stuff.
2337 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002338 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002339
2340 return ret;
2341}
2342
2343/*
2344 * Removes a device from a protection domain (unlocked)
2345 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002346static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002347{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002348 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002349 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002350
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002351 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002352
Joerg Roedel2ca76272010-01-22 16:45:31 +01002353 domain = dev_data->domain;
2354
2355 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002356
Joerg Roedel71f77582011-06-09 19:03:15 +02002357 if (dev_data->alias_data != NULL) {
2358 struct iommu_dev_data *alias_data = dev_data->alias_data;
2359
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002360 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002361 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002362 }
2363
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002364 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002365 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002366
Joerg Roedel2ca76272010-01-22 16:45:31 +01002367 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002368
Joerg Roedel21129f72009-09-01 11:59:42 +02002369 /*
2370 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002371 * passthrough domain if it is detached from any other domain.
2372 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002373 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002374 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002375 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002376 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002377}
2378
2379/*
2380 * Removes a device from a protection domain (with devtable_lock held)
2381 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002382static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002383{
Joerg Roedel52815b72011-11-17 17:24:28 +01002384 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002385 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002386 unsigned long flags;
2387
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002388 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002389 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002390
Joerg Roedel355bf552008-12-08 12:02:41 +01002391 /* lock device table */
2392 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002393 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002394 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002395
Joerg Roedel52815b72011-11-17 17:24:28 +01002396 if (domain->flags & PD_IOMMUV2_MASK)
2397 pdev_iommuv2_disable(to_pci_dev(dev));
2398 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002399 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002400
2401 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002402}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002403
Joerg Roedel15898bb2009-11-24 15:39:42 +01002404/*
2405 * Find out the protection domain structure for a given PCI device. This
2406 * will give us the pointer to the page table root for example.
2407 */
2408static struct protection_domain *domain_for_device(struct device *dev)
2409{
Joerg Roedel71f77582011-06-09 19:03:15 +02002410 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002411 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002412 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002413
Joerg Roedel657cbb62009-11-23 15:26:46 +01002414 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002415
Joerg Roedel2b02b092011-06-09 17:48:39 +02002416 if (dev_data->domain)
2417 return dev_data->domain;
2418
Joerg Roedel71f77582011-06-09 19:03:15 +02002419 if (dev_data->alias_data != NULL) {
2420 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002421
2422 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2423 if (alias_data->domain != NULL) {
2424 __attach_device(dev_data, alias_data->domain);
2425 dom = alias_data->domain;
2426 }
2427 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002428 }
2429
Joerg Roedel15898bb2009-11-24 15:39:42 +01002430 return dom;
2431}
2432
Joerg Roedele275a2a2008-12-10 18:27:25 +01002433static int device_change_notifier(struct notifier_block *nb,
2434 unsigned long action, void *data)
2435{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002436 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002437 struct protection_domain *domain;
2438 struct iommu_dev_data *dev_data;
2439 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002440 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002441 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002442 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002443
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002444 if (!check_device(dev))
2445 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002446
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002447 devid = get_device_id(dev);
2448 iommu = amd_iommu_rlookup_table[devid];
2449 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002450
2451 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002452 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002453
2454 domain = domain_for_device(dev);
2455
Joerg Roedele275a2a2008-12-10 18:27:25 +01002456 if (!domain)
2457 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002458 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002459 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002460 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002461 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002462 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002463
2464 iommu_init_device(dev);
2465
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002466 /*
2467 * dev_data is still NULL and
2468 * got initialized in iommu_init_device
2469 */
2470 dev_data = get_dev_data(dev);
2471
2472 if (iommu_pass_through || dev_data->iommu_v2) {
2473 dev_data->passthrough = true;
2474 attach_device(dev, pt_domain);
2475 break;
2476 }
2477
Joerg Roedel657cbb62009-11-23 15:26:46 +01002478 domain = domain_for_device(dev);
2479
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002480 /* allocate a protection domain if a device is added */
2481 dma_domain = find_protection_domain(devid);
Joerg Roedelc2a28762013-03-26 22:48:23 +01002482 if (!dma_domain) {
2483 dma_domain = dma_ops_domain_alloc();
2484 if (!dma_domain)
2485 goto out;
2486 dma_domain->target_dev = devid;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002487
Joerg Roedelc2a28762013-03-26 22:48:23 +01002488 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2489 list_add_tail(&dma_domain->list, &iommu_pd_list);
2490 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2491 }
Joerg Roedelac1534a2012-06-21 14:52:40 +02002492
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002493 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002494
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002495 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002496 case BUS_NOTIFY_DEL_DEVICE:
2497
2498 iommu_uninit_device(dev);
2499
Joerg Roedele275a2a2008-12-10 18:27:25 +01002500 default:
2501 goto out;
2502 }
2503
Joerg Roedele275a2a2008-12-10 18:27:25 +01002504 iommu_completion_wait(iommu);
2505
2506out:
2507 return 0;
2508}
2509
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302510static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002511 .notifier_call = device_change_notifier,
2512};
Joerg Roedel355bf552008-12-08 12:02:41 +01002513
Joerg Roedel8638c492009-12-10 11:12:25 +01002514void amd_iommu_init_notifier(void)
2515{
2516 bus_register_notifier(&pci_bus_type, &device_nb);
2517}
2518
Joerg Roedel431b2a22008-07-11 17:14:22 +02002519/*****************************************************************************
2520 *
2521 * The next functions belong to the dma_ops mapping/unmapping code.
2522 *
2523 *****************************************************************************/
2524
2525/*
2526 * In the dma_ops path we only have the struct device. This function
2527 * finds the corresponding IOMMU, the protection domain and the
2528 * requestor id for a given device.
2529 * If the device is not yet associated with a domain this is also done
2530 * in this function.
2531 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002532static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002533{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002534 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002535 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002536 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002537
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002538 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002539 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002540
Joerg Roedel94f6d192009-11-24 16:40:02 +01002541 domain = domain_for_device(dev);
2542 if (domain != NULL && !dma_ops_domain(domain))
2543 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002544
Joerg Roedel94f6d192009-11-24 16:40:02 +01002545 if (domain != NULL)
2546 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002547
Frank Arnolddf805ab2012-08-27 19:21:04 +02002548 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002549 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002550 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002551 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2552 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002553 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002554 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002555
Joerg Roedel94f6d192009-11-24 16:40:02 +01002556 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002557}
2558
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002559static void update_device_table(struct protection_domain *domain)
2560{
Joerg Roedel492667d2009-11-27 13:25:47 +01002561 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002562
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002563 list_for_each_entry(dev_data, &domain->dev_list, list)
2564 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002565}
2566
2567static void update_domain(struct protection_domain *domain)
2568{
2569 if (!domain->updated)
2570 return;
2571
2572 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002573
2574 domain_flush_devices(domain);
2575 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002576
2577 domain->updated = false;
2578}
2579
Joerg Roedel431b2a22008-07-11 17:14:22 +02002580/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002581 * This function fetches the PTE for a given address in the aperture
2582 */
2583static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2584 unsigned long address)
2585{
Joerg Roedel384de722009-05-15 12:30:05 +02002586 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002587 u64 *pte, *pte_page;
2588
Joerg Roedel384de722009-05-15 12:30:05 +02002589 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2590 if (!aperture)
2591 return NULL;
2592
2593 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002594 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002595 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002596 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002597 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2598 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002599 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002600
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002601 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002602
2603 return pte;
2604}
2605
2606/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002607 * This is the generic map function. It maps one 4kb page at paddr to
2608 * the given address in the DMA address space for the domain.
2609 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002610static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002611 unsigned long address,
2612 phys_addr_t paddr,
2613 int direction)
2614{
2615 u64 *pte, __pte;
2616
2617 WARN_ON(address > dom->aperture_size);
2618
2619 paddr &= PAGE_MASK;
2620
Joerg Roedel8bda3092009-05-12 12:02:46 +02002621 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002622 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002623 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002624
2625 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2626
2627 if (direction == DMA_TO_DEVICE)
2628 __pte |= IOMMU_PTE_IR;
2629 else if (direction == DMA_FROM_DEVICE)
2630 __pte |= IOMMU_PTE_IW;
2631 else if (direction == DMA_BIDIRECTIONAL)
2632 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2633
2634 WARN_ON(*pte);
2635
2636 *pte = __pte;
2637
2638 return (dma_addr_t)address;
2639}
2640
Joerg Roedel431b2a22008-07-11 17:14:22 +02002641/*
2642 * The generic unmapping function for on page in the DMA address space.
2643 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002644static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002645 unsigned long address)
2646{
Joerg Roedel384de722009-05-15 12:30:05 +02002647 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002648 u64 *pte;
2649
2650 if (address >= dom->aperture_size)
2651 return;
2652
Joerg Roedel384de722009-05-15 12:30:05 +02002653 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2654 if (!aperture)
2655 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002656
Joerg Roedel384de722009-05-15 12:30:05 +02002657 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2658 if (!pte)
2659 return;
2660
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002661 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002662
2663 WARN_ON(!*pte);
2664
2665 *pte = 0ULL;
2666}
2667
Joerg Roedel431b2a22008-07-11 17:14:22 +02002668/*
2669 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002670 * contiguous memory region into DMA address space. It is used by all
2671 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002672 * Must be called with the domain lock held.
2673 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002674static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002675 struct dma_ops_domain *dma_dom,
2676 phys_addr_t paddr,
2677 size_t size,
Joerg Roedel6d4f343f2008-09-04 19:18:02 +02002678 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002679 bool align,
2680 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002681{
2682 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002683 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002684 unsigned int pages;
Joerg Roedel6d4f343f2008-09-04 19:18:02 +02002685 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002686 int i;
2687
Joerg Roedele3c449f2008-10-15 22:02:11 -07002688 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002689 paddr &= PAGE_MASK;
2690
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002691 INC_STATS_COUNTER(total_map_requests);
2692
Joerg Roedelc1858972008-12-12 15:42:39 +01002693 if (pages > 1)
2694 INC_STATS_COUNTER(cross_page);
2695
Joerg Roedel6d4f343f2008-09-04 19:18:02 +02002696 if (align)
2697 align_mask = (1UL << get_order(size)) - 1;
2698
Joerg Roedel11b83882009-05-19 10:23:15 +02002699retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002700 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2701 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002702 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002703 /*
2704 * setting next_address here will let the address
2705 * allocator only scan the new allocated range in the
2706 * first run. This is a small optimization.
2707 */
2708 dma_dom->next_address = dma_dom->aperture_size;
2709
Joerg Roedel576175c2009-11-23 19:08:46 +01002710 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002711 goto out;
2712
2713 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002714 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002715 * allocation again
2716 */
2717 goto retry;
2718 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002719
2720 start = address;
2721 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002722 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002723 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002724 goto out_unmap;
2725
Joerg Roedelcb76c322008-06-26 21:28:00 +02002726 paddr += PAGE_SIZE;
2727 start += PAGE_SIZE;
2728 }
2729 address += offset;
2730
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002731 ADD_STATS_COUNTER(alloced_io_mem, size);
2732
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002733 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002734 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002735 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002736 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002737 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002738
Joerg Roedelcb76c322008-06-26 21:28:00 +02002739out:
2740 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002741
2742out_unmap:
2743
2744 for (--i; i >= 0; --i) {
2745 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002746 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002747 }
2748
2749 dma_ops_free_addresses(dma_dom, address, pages);
2750
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002751 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002752}
2753
Joerg Roedel431b2a22008-07-11 17:14:22 +02002754/*
2755 * Does the reverse of the __map_single function. Must be called with
2756 * the domain lock held too
2757 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002758static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002759 dma_addr_t dma_addr,
2760 size_t size,
2761 int dir)
2762{
Joerg Roedel04e04632010-09-23 16:12:48 +02002763 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002764 dma_addr_t i, start;
2765 unsigned int pages;
2766
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002767 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002768 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002769 return;
2770
Joerg Roedel04e04632010-09-23 16:12:48 +02002771 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002772 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002773 dma_addr &= PAGE_MASK;
2774 start = dma_addr;
2775
2776 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002777 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002778 start += PAGE_SIZE;
2779 }
2780
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002781 SUB_STATS_COUNTER(alloced_io_mem, size);
2782
Joerg Roedelcb76c322008-06-26 21:28:00 +02002783 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002784
Joerg Roedel80be3082008-11-06 14:59:05 +01002785 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002786 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002787 dma_dom->need_flush = false;
2788 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002789}
2790
Joerg Roedel431b2a22008-07-11 17:14:22 +02002791/*
2792 * The exported map_single function for dma_ops.
2793 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002794static dma_addr_t map_page(struct device *dev, struct page *page,
2795 unsigned long offset, size_t size,
2796 enum dma_data_direction dir,
2797 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002798{
2799 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002800 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002801 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002802 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002803 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002804
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002805 INC_STATS_COUNTER(cnt_map_single);
2806
Joerg Roedel94f6d192009-11-24 16:40:02 +01002807 domain = get_domain(dev);
2808 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002809 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002810 else if (IS_ERR(domain))
2811 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002812
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002813 dma_mask = *dev->dma_mask;
2814
Joerg Roedel4da70b92008-06-26 21:28:01 +02002815 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002816
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002817 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002818 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002819 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002820 goto out;
2821
Joerg Roedel17b124b2011-04-06 18:01:35 +02002822 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002823
2824out:
2825 spin_unlock_irqrestore(&domain->lock, flags);
2826
2827 return addr;
2828}
2829
Joerg Roedel431b2a22008-07-11 17:14:22 +02002830/*
2831 * The exported unmap_single function for dma_ops.
2832 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002833static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2834 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002835{
2836 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002837 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002838
Joerg Roedel146a6912008-12-12 15:07:12 +01002839 INC_STATS_COUNTER(cnt_unmap_single);
2840
Joerg Roedel94f6d192009-11-24 16:40:02 +01002841 domain = get_domain(dev);
2842 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002843 return;
2844
Joerg Roedel4da70b92008-06-26 21:28:01 +02002845 spin_lock_irqsave(&domain->lock, flags);
2846
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002847 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002848
Joerg Roedel17b124b2011-04-06 18:01:35 +02002849 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002850
2851 spin_unlock_irqrestore(&domain->lock, flags);
2852}
2853
Joerg Roedel431b2a22008-07-11 17:14:22 +02002854/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002855 * The exported map_sg function for dma_ops (handles scatter-gather
2856 * lists).
2857 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002858static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002859 int nelems, enum dma_data_direction dir,
2860 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002861{
2862 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002863 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002864 int i;
2865 struct scatterlist *s;
2866 phys_addr_t paddr;
2867 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002868 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002869
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002870 INC_STATS_COUNTER(cnt_map_sg);
2871
Joerg Roedel94f6d192009-11-24 16:40:02 +01002872 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002873 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002874 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002875
Joerg Roedel832a90c2008-09-18 15:54:23 +02002876 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002877
Joerg Roedel65b050a2008-06-26 21:28:02 +02002878 spin_lock_irqsave(&domain->lock, flags);
2879
2880 for_each_sg(sglist, s, nelems, i) {
2881 paddr = sg_phys(s);
2882
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002883 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002884 paddr, s->length, dir, false,
2885 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002886
2887 if (s->dma_address) {
2888 s->dma_length = s->length;
2889 mapped_elems++;
2890 } else
2891 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002892 }
2893
Joerg Roedel17b124b2011-04-06 18:01:35 +02002894 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002895
2896out:
2897 spin_unlock_irqrestore(&domain->lock, flags);
2898
2899 return mapped_elems;
2900unmap:
2901 for_each_sg(sglist, s, mapped_elems, i) {
2902 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002903 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002904 s->dma_length, dir);
2905 s->dma_address = s->dma_length = 0;
2906 }
2907
2908 mapped_elems = 0;
2909
2910 goto out;
2911}
2912
Joerg Roedel431b2a22008-07-11 17:14:22 +02002913/*
2914 * The exported map_sg function for dma_ops (handles scatter-gather
2915 * lists).
2916 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002917static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002918 int nelems, enum dma_data_direction dir,
2919 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002920{
2921 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002922 struct protection_domain *domain;
2923 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002924 int i;
2925
Joerg Roedel55877a62008-12-12 15:12:14 +01002926 INC_STATS_COUNTER(cnt_unmap_sg);
2927
Joerg Roedel94f6d192009-11-24 16:40:02 +01002928 domain = get_domain(dev);
2929 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002930 return;
2931
Joerg Roedel65b050a2008-06-26 21:28:02 +02002932 spin_lock_irqsave(&domain->lock, flags);
2933
2934 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002935 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002936 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002937 s->dma_address = s->dma_length = 0;
2938 }
2939
Joerg Roedel17b124b2011-04-06 18:01:35 +02002940 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002941
2942 spin_unlock_irqrestore(&domain->lock, flags);
2943}
2944
Joerg Roedel431b2a22008-07-11 17:14:22 +02002945/*
2946 * The exported alloc_coherent function for dma_ops.
2947 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002948static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002949 dma_addr_t *dma_addr, gfp_t flag,
2950 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002951{
2952 unsigned long flags;
2953 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002954 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002955 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002956 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002957
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002958 INC_STATS_COUNTER(cnt_alloc_coherent);
2959
Joerg Roedel94f6d192009-11-24 16:40:02 +01002960 domain = get_domain(dev);
2961 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002962 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2963 *dma_addr = __pa(virt_addr);
2964 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002965 } else if (IS_ERR(domain))
2966 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002967
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002968 dma_mask = dev->coherent_dma_mask;
2969 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2970 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002971
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002972 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2973 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302974 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002975
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002976 paddr = virt_to_phys(virt_addr);
2977
Joerg Roedel832a90c2008-09-18 15:54:23 +02002978 if (!dma_mask)
2979 dma_mask = *dev->dma_mask;
2980
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002981 spin_lock_irqsave(&domain->lock, flags);
2982
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002983 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002984 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002985
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002986 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002987 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002988 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002989 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002990
Joerg Roedel17b124b2011-04-06 18:01:35 +02002991 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002992
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002993 spin_unlock_irqrestore(&domain->lock, flags);
2994
2995 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002996
2997out_free:
2998
2999 free_pages((unsigned long)virt_addr, get_order(size));
3000
3001 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003002}
3003
Joerg Roedel431b2a22008-07-11 17:14:22 +02003004/*
3005 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003006 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003007static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003008 void *virt_addr, dma_addr_t dma_addr,
3009 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003010{
3011 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003012 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003013
Joerg Roedel5d31ee72008-12-12 15:16:38 +01003014 INC_STATS_COUNTER(cnt_free_coherent);
3015
Joerg Roedel94f6d192009-11-24 16:40:02 +01003016 domain = get_domain(dev);
3017 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01003018 goto free_mem;
3019
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003020 spin_lock_irqsave(&domain->lock, flags);
3021
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003022 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003023
Joerg Roedel17b124b2011-04-06 18:01:35 +02003024 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003025
3026 spin_unlock_irqrestore(&domain->lock, flags);
3027
3028free_mem:
3029 free_pages((unsigned long)virt_addr, get_order(size));
3030}
3031
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003032/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003033 * This function is called by the DMA layer to find out if we can handle a
3034 * particular device. It is part of the dma_ops.
3035 */
3036static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3037{
Joerg Roedel420aef82009-11-23 16:14:57 +01003038 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003039}
3040
3041/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003042 * The function for pre-allocating protection domains.
3043 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003044 * If the driver core informs the DMA layer if a driver grabs a device
3045 * we don't need to preallocate the protection domains anymore.
3046 * For now we have to.
3047 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003048static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003049{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003050 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003051 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003052 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003053 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003054
Chris Wrightd18c69d2010-04-02 18:27:55 -07003055 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003056
3057 /* Do we handle this device? */
3058 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003059 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003060
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003061 dev_data = get_dev_data(&dev->dev);
3062 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3063 /* Make sure passthrough domain is allocated */
3064 alloc_passthrough_domain();
3065 dev_data->passthrough = true;
3066 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003067 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003068 dev_name(&dev->dev));
3069 }
3070
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003071 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003072 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003073 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003074
3075 devid = get_device_id(&dev->dev);
3076
Joerg Roedel87a64d52009-11-24 17:26:43 +01003077 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003078 if (!dma_dom)
3079 continue;
3080 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003081 dma_dom->target_dev = devid;
3082
Joerg Roedel15898bb2009-11-24 15:39:42 +01003083 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003084
Joerg Roedelbd60b732008-09-11 10:24:48 +02003085 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003086 }
3087}
3088
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003089static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003090 .alloc = alloc_coherent,
3091 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003092 .map_page = map_page,
3093 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003094 .map_sg = map_sg,
3095 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003096 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003097};
3098
Joerg Roedel27c21272011-05-30 15:56:24 +02003099static unsigned device_dma_ops_init(void)
3100{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003101 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003102 struct pci_dev *pdev = NULL;
3103 unsigned unhandled = 0;
3104
3105 for_each_pci_dev(pdev) {
3106 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003107
3108 iommu_ignore_device(&pdev->dev);
3109
Joerg Roedel27c21272011-05-30 15:56:24 +02003110 unhandled += 1;
3111 continue;
3112 }
3113
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003114 dev_data = get_dev_data(&pdev->dev);
3115
3116 if (!dev_data->passthrough)
3117 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3118 else
3119 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003120 }
3121
3122 return unhandled;
3123}
3124
Joerg Roedel431b2a22008-07-11 17:14:22 +02003125/*
3126 * The function which clues the AMD IOMMU driver into dma_ops.
3127 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003128
3129void __init amd_iommu_init_api(void)
3130{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003131 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003132}
3133
Joerg Roedel6631ee92008-06-26 21:28:05 +02003134int __init amd_iommu_init_dma_ops(void)
3135{
3136 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003137 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003138
Joerg Roedel431b2a22008-07-11 17:14:22 +02003139 /*
3140 * first allocate a default protection domain for every IOMMU we
3141 * found in the system. Devices not assigned to any other
3142 * protection domain will be assigned to the default one.
3143 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003144 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003145 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003146 if (iommu->default_dom == NULL)
3147 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003148 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003149 ret = iommu_init_unity_mappings(iommu);
3150 if (ret)
3151 goto free_domains;
3152 }
3153
Joerg Roedel431b2a22008-07-11 17:14:22 +02003154 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003155 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003156 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003157 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003158
3159 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003160 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003161
Joerg Roedel431b2a22008-07-11 17:14:22 +02003162 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003163 unhandled = device_dma_ops_init();
3164 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3165 /* There are unhandled devices - initialize swiotlb for them */
3166 swiotlb = 1;
3167 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003168
Joerg Roedel7f265082008-12-12 13:50:21 +01003169 amd_iommu_stats_init();
3170
Joerg Roedel62410ee2012-06-12 16:42:43 +02003171 if (amd_iommu_unmap_flush)
3172 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3173 else
3174 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3175
Joerg Roedel6631ee92008-06-26 21:28:05 +02003176 return 0;
3177
3178free_domains:
3179
Joerg Roedel3bd22172009-05-04 15:06:20 +02003180 for_each_iommu(iommu) {
Cyril Roelandt91457df2013-02-12 05:01:50 +01003181 dma_ops_domain_free(iommu->default_dom);
Joerg Roedel6631ee92008-06-26 21:28:05 +02003182 }
3183
3184 return ret;
3185}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003186
3187/*****************************************************************************
3188 *
3189 * The following functions belong to the exported interface of AMD IOMMU
3190 *
3191 * This interface allows access to lower level functions of the IOMMU
3192 * like protection domain handling and assignement of devices to domains
3193 * which is not possible with the dma_ops interface.
3194 *
3195 *****************************************************************************/
3196
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003197static void cleanup_domain(struct protection_domain *domain)
3198{
Joerg Roedel492667d2009-11-27 13:25:47 +01003199 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003200 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003201
3202 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3203
Joerg Roedel492667d2009-11-27 13:25:47 +01003204 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003205 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003206 atomic_set(&dev_data->bind, 0);
3207 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003208
3209 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3210}
3211
Joerg Roedel26508152009-08-26 16:52:40 +02003212static void protection_domain_free(struct protection_domain *domain)
3213{
3214 if (!domain)
3215 return;
3216
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003217 del_domain_from_list(domain);
3218
Joerg Roedel26508152009-08-26 16:52:40 +02003219 if (domain->id)
3220 domain_id_free(domain->id);
3221
3222 kfree(domain);
3223}
3224
3225static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003226{
3227 struct protection_domain *domain;
3228
3229 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3230 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003231 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003232
3233 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003234 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003235 domain->id = domain_id_alloc();
3236 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003237 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003238 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003239
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003240 add_domain_to_list(domain);
3241
Joerg Roedel26508152009-08-26 16:52:40 +02003242 return domain;
3243
3244out_err:
3245 kfree(domain);
3246
3247 return NULL;
3248}
3249
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003250static int __init alloc_passthrough_domain(void)
3251{
3252 if (pt_domain != NULL)
3253 return 0;
3254
3255 /* allocate passthrough domain */
3256 pt_domain = protection_domain_alloc();
3257 if (!pt_domain)
3258 return -ENOMEM;
3259
3260 pt_domain->mode = PAGE_MODE_NONE;
3261
3262 return 0;
3263}
Joerg Roedel26508152009-08-26 16:52:40 +02003264static int amd_iommu_domain_init(struct iommu_domain *dom)
3265{
3266 struct protection_domain *domain;
3267
3268 domain = protection_domain_alloc();
3269 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003270 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003271
3272 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003273 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3274 if (!domain->pt_root)
3275 goto out_free;
3276
Joerg Roedelf3572db2011-11-23 12:36:25 +01003277 domain->iommu_domain = dom;
3278
Joerg Roedelc156e342008-12-02 18:13:27 +01003279 dom->priv = domain;
3280
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003281 dom->geometry.aperture_start = 0;
3282 dom->geometry.aperture_end = ~0ULL;
3283 dom->geometry.force_aperture = true;
3284
Joerg Roedelc156e342008-12-02 18:13:27 +01003285 return 0;
3286
3287out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003288 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003289
3290 return -ENOMEM;
3291}
3292
Joerg Roedel98383fc2008-12-02 18:34:12 +01003293static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3294{
3295 struct protection_domain *domain = dom->priv;
3296
3297 if (!domain)
3298 return;
3299
3300 if (domain->dev_cnt > 0)
3301 cleanup_domain(domain);
3302
3303 BUG_ON(domain->dev_cnt != 0);
3304
Joerg Roedel132bd682011-11-17 14:18:46 +01003305 if (domain->mode != PAGE_MODE_NONE)
3306 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003307
Joerg Roedel52815b72011-11-17 17:24:28 +01003308 if (domain->flags & PD_IOMMUV2_MASK)
3309 free_gcr3_table(domain);
3310
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003311 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003312
3313 dom->priv = NULL;
3314}
3315
Joerg Roedel684f2882008-12-08 12:07:44 +01003316static void amd_iommu_detach_device(struct iommu_domain *dom,
3317 struct device *dev)
3318{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003319 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003320 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003321 u16 devid;
3322
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003323 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003324 return;
3325
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003326 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003327
Joerg Roedel657cbb62009-11-23 15:26:46 +01003328 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003329 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003330
3331 iommu = amd_iommu_rlookup_table[devid];
3332 if (!iommu)
3333 return;
3334
Joerg Roedel684f2882008-12-08 12:07:44 +01003335 iommu_completion_wait(iommu);
3336}
3337
Joerg Roedel01106062008-12-02 19:34:11 +01003338static int amd_iommu_attach_device(struct iommu_domain *dom,
3339 struct device *dev)
3340{
3341 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003342 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003343 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003344 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003345
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003346 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003347 return -EINVAL;
3348
Joerg Roedel657cbb62009-11-23 15:26:46 +01003349 dev_data = dev->archdata.iommu;
3350
Joerg Roedelf62dda62011-06-09 12:55:35 +02003351 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003352 if (!iommu)
3353 return -EINVAL;
3354
Joerg Roedel657cbb62009-11-23 15:26:46 +01003355 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003356 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003357
Joerg Roedel15898bb2009-11-24 15:39:42 +01003358 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003359
3360 iommu_completion_wait(iommu);
3361
Joerg Roedel15898bb2009-11-24 15:39:42 +01003362 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003363}
3364
Joerg Roedel468e2362010-01-21 16:37:36 +01003365static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003366 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003367{
3368 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003369 int prot = 0;
3370 int ret;
3371
Joerg Roedel132bd682011-11-17 14:18:46 +01003372 if (domain->mode == PAGE_MODE_NONE)
3373 return -EINVAL;
3374
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003375 if (iommu_prot & IOMMU_READ)
3376 prot |= IOMMU_PROT_IR;
3377 if (iommu_prot & IOMMU_WRITE)
3378 prot |= IOMMU_PROT_IW;
3379
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003380 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003381 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003382 mutex_unlock(&domain->api_lock);
3383
Joerg Roedel795e74f72010-05-11 17:40:57 +02003384 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003385}
3386
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003387static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3388 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003389{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003390 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003391 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003392
Joerg Roedel132bd682011-11-17 14:18:46 +01003393 if (domain->mode == PAGE_MODE_NONE)
3394 return -EINVAL;
3395
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003396 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003397 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003398 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003399
Joerg Roedel17b124b2011-04-06 18:01:35 +02003400 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003401
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003402 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003403}
3404
Joerg Roedel645c4c82008-12-02 20:05:50 +01003405static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303406 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003407{
3408 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003409 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003410 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003411 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003412
Joerg Roedel132bd682011-11-17 14:18:46 +01003413 if (domain->mode == PAGE_MODE_NONE)
3414 return iova;
3415
Joerg Roedel24cd7722010-01-19 17:27:39 +01003416 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003417
Joerg Roedela6d41a42009-09-02 17:08:55 +02003418 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003419 return 0;
3420
Joerg Roedelf03152b2010-01-21 16:15:24 +01003421 if (PM_PTE_LEVEL(*pte) == 0)
3422 offset_mask = PAGE_SIZE - 1;
3423 else
3424 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3425
3426 __pte = *pte & PM_ADDR_MASK;
3427 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003428
3429 return paddr;
3430}
3431
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003432static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3433 unsigned long cap)
3434{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003435 switch (cap) {
3436 case IOMMU_CAP_CACHE_COHERENCY:
3437 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003438 case IOMMU_CAP_INTR_REMAP:
3439 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003440 }
3441
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003442 return 0;
3443}
3444
Joerg Roedel26961ef2008-12-03 17:00:17 +01003445static struct iommu_ops amd_iommu_ops = {
3446 .domain_init = amd_iommu_domain_init,
3447 .domain_destroy = amd_iommu_domain_destroy,
3448 .attach_dev = amd_iommu_attach_device,
3449 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003450 .map = amd_iommu_map,
3451 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003452 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003453 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003454 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003455};
3456
Joerg Roedel0feae532009-08-26 15:26:30 +02003457/*****************************************************************************
3458 *
3459 * The next functions do a basic initialization of IOMMU for pass through
3460 * mode
3461 *
3462 * In passthrough mode the IOMMU is initialized and enabled but not used for
3463 * DMA-API translation.
3464 *
3465 *****************************************************************************/
3466
3467int __init amd_iommu_init_passthrough(void)
3468{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003469 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003470 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003471 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003472 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003473 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003474
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003475 ret = alloc_passthrough_domain();
3476 if (ret)
3477 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003478
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003479 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003480 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003481 continue;
3482
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003483 dev_data = get_dev_data(&dev->dev);
3484 dev_data->passthrough = true;
3485
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003486 devid = get_device_id(&dev->dev);
3487
Joerg Roedel15898bb2009-11-24 15:39:42 +01003488 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003489 if (!iommu)
3490 continue;
3491
Joerg Roedel15898bb2009-11-24 15:39:42 +01003492 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003493 }
3494
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003495 amd_iommu_stats_init();
3496
Joerg Roedel0feae532009-08-26 15:26:30 +02003497 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3498
3499 return 0;
3500}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003501
3502/* IOMMUv2 specific functions */
3503int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3504{
3505 return atomic_notifier_chain_register(&ppr_notifier, nb);
3506}
3507EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3508
3509int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3510{
3511 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3512}
3513EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003514
3515void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3516{
3517 struct protection_domain *domain = dom->priv;
3518 unsigned long flags;
3519
3520 spin_lock_irqsave(&domain->lock, flags);
3521
3522 /* Update data structure */
3523 domain->mode = PAGE_MODE_NONE;
3524 domain->updated = true;
3525
3526 /* Make changes visible to IOMMUs */
3527 update_domain(domain);
3528
3529 /* Page-table is not visible to IOMMU anymore, so free it */
3530 free_pagetable(domain);
3531
3532 spin_unlock_irqrestore(&domain->lock, flags);
3533}
3534EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003535
3536int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3537{
3538 struct protection_domain *domain = dom->priv;
3539 unsigned long flags;
3540 int levels, ret;
3541
3542 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3543 return -EINVAL;
3544
3545 /* Number of GCR3 table levels required */
3546 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3547 levels += 1;
3548
3549 if (levels > amd_iommu_max_glx_val)
3550 return -EINVAL;
3551
3552 spin_lock_irqsave(&domain->lock, flags);
3553
3554 /*
3555 * Save us all sanity checks whether devices already in the
3556 * domain support IOMMUv2. Just force that the domain has no
3557 * devices attached when it is switched into IOMMUv2 mode.
3558 */
3559 ret = -EBUSY;
3560 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3561 goto out;
3562
3563 ret = -ENOMEM;
3564 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3565 if (domain->gcr3_tbl == NULL)
3566 goto out;
3567
3568 domain->glx = levels;
3569 domain->flags |= PD_IOMMUV2_MASK;
3570 domain->updated = true;
3571
3572 update_domain(domain);
3573
3574 ret = 0;
3575
3576out:
3577 spin_unlock_irqrestore(&domain->lock, flags);
3578
3579 return ret;
3580}
3581EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003582
3583static int __flush_pasid(struct protection_domain *domain, int pasid,
3584 u64 address, bool size)
3585{
3586 struct iommu_dev_data *dev_data;
3587 struct iommu_cmd cmd;
3588 int i, ret;
3589
3590 if (!(domain->flags & PD_IOMMUV2_MASK))
3591 return -EINVAL;
3592
3593 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3594
3595 /*
3596 * IOMMU TLB needs to be flushed before Device TLB to
3597 * prevent device TLB refill from IOMMU TLB
3598 */
3599 for (i = 0; i < amd_iommus_present; ++i) {
3600 if (domain->dev_iommu[i] == 0)
3601 continue;
3602
3603 ret = iommu_queue_command(amd_iommus[i], &cmd);
3604 if (ret != 0)
3605 goto out;
3606 }
3607
3608 /* Wait until IOMMU TLB flushes are complete */
3609 domain_flush_complete(domain);
3610
3611 /* Now flush device TLBs */
3612 list_for_each_entry(dev_data, &domain->dev_list, list) {
3613 struct amd_iommu *iommu;
3614 int qdep;
3615
3616 BUG_ON(!dev_data->ats.enabled);
3617
3618 qdep = dev_data->ats.qdep;
3619 iommu = amd_iommu_rlookup_table[dev_data->devid];
3620
3621 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3622 qdep, address, size);
3623
3624 ret = iommu_queue_command(iommu, &cmd);
3625 if (ret != 0)
3626 goto out;
3627 }
3628
3629 /* Wait until all device TLBs are flushed */
3630 domain_flush_complete(domain);
3631
3632 ret = 0;
3633
3634out:
3635
3636 return ret;
3637}
3638
3639static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3640 u64 address)
3641{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003642 INC_STATS_COUNTER(invalidate_iotlb);
3643
Joerg Roedel22e266c2011-11-21 15:59:08 +01003644 return __flush_pasid(domain, pasid, address, false);
3645}
3646
3647int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3648 u64 address)
3649{
3650 struct protection_domain *domain = dom->priv;
3651 unsigned long flags;
3652 int ret;
3653
3654 spin_lock_irqsave(&domain->lock, flags);
3655 ret = __amd_iommu_flush_page(domain, pasid, address);
3656 spin_unlock_irqrestore(&domain->lock, flags);
3657
3658 return ret;
3659}
3660EXPORT_SYMBOL(amd_iommu_flush_page);
3661
3662static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3663{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003664 INC_STATS_COUNTER(invalidate_iotlb_all);
3665
Joerg Roedel22e266c2011-11-21 15:59:08 +01003666 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3667 true);
3668}
3669
3670int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3671{
3672 struct protection_domain *domain = dom->priv;
3673 unsigned long flags;
3674 int ret;
3675
3676 spin_lock_irqsave(&domain->lock, flags);
3677 ret = __amd_iommu_flush_tlb(domain, pasid);
3678 spin_unlock_irqrestore(&domain->lock, flags);
3679
3680 return ret;
3681}
3682EXPORT_SYMBOL(amd_iommu_flush_tlb);
3683
Joerg Roedelb16137b2011-11-21 16:50:23 +01003684static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3685{
3686 int index;
3687 u64 *pte;
3688
3689 while (true) {
3690
3691 index = (pasid >> (9 * level)) & 0x1ff;
3692 pte = &root[index];
3693
3694 if (level == 0)
3695 break;
3696
3697 if (!(*pte & GCR3_VALID)) {
3698 if (!alloc)
3699 return NULL;
3700
3701 root = (void *)get_zeroed_page(GFP_ATOMIC);
3702 if (root == NULL)
3703 return NULL;
3704
3705 *pte = __pa(root) | GCR3_VALID;
3706 }
3707
3708 root = __va(*pte & PAGE_MASK);
3709
3710 level -= 1;
3711 }
3712
3713 return pte;
3714}
3715
3716static int __set_gcr3(struct protection_domain *domain, int pasid,
3717 unsigned long cr3)
3718{
3719 u64 *pte;
3720
3721 if (domain->mode != PAGE_MODE_NONE)
3722 return -EINVAL;
3723
3724 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3725 if (pte == NULL)
3726 return -ENOMEM;
3727
3728 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3729
3730 return __amd_iommu_flush_tlb(domain, pasid);
3731}
3732
3733static int __clear_gcr3(struct protection_domain *domain, int pasid)
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, false);
3741 if (pte == NULL)
3742 return 0;
3743
3744 *pte = 0;
3745
3746 return __amd_iommu_flush_tlb(domain, pasid);
3747}
3748
3749int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3750 unsigned long cr3)
3751{
3752 struct protection_domain *domain = dom->priv;
3753 unsigned long flags;
3754 int ret;
3755
3756 spin_lock_irqsave(&domain->lock, flags);
3757 ret = __set_gcr3(domain, pasid, cr3);
3758 spin_unlock_irqrestore(&domain->lock, flags);
3759
3760 return ret;
3761}
3762EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3763
3764int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3765{
3766 struct protection_domain *domain = dom->priv;
3767 unsigned long flags;
3768 int ret;
3769
3770 spin_lock_irqsave(&domain->lock, flags);
3771 ret = __clear_gcr3(domain, pasid);
3772 spin_unlock_irqrestore(&domain->lock, flags);
3773
3774 return ret;
3775}
3776EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003777
3778int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3779 int status, int tag)
3780{
3781 struct iommu_dev_data *dev_data;
3782 struct amd_iommu *iommu;
3783 struct iommu_cmd cmd;
3784
Joerg Roedel399be2f2011-12-01 16:53:47 +01003785 INC_STATS_COUNTER(complete_ppr);
3786
Joerg Roedelc99afa22011-11-21 18:19:25 +01003787 dev_data = get_dev_data(&pdev->dev);
3788 iommu = amd_iommu_rlookup_table[dev_data->devid];
3789
3790 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3791 tag, dev_data->pri_tlp);
3792
3793 return iommu_queue_command(iommu, &cmd);
3794}
3795EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003796
3797struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3798{
3799 struct protection_domain *domain;
3800
3801 domain = get_domain(&pdev->dev);
3802 if (IS_ERR(domain))
3803 return NULL;
3804
3805 /* Only return IOMMUv2 domains */
3806 if (!(domain->flags & PD_IOMMUV2_MASK))
3807 return NULL;
3808
3809 return domain->iommu_domain;
3810}
3811EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003812
3813void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3814{
3815 struct iommu_dev_data *dev_data;
3816
3817 if (!amd_iommu_v2_supported())
3818 return;
3819
3820 dev_data = get_dev_data(&pdev->dev);
3821 dev_data->errata |= (1 << erratum);
3822}
3823EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003824
3825int amd_iommu_device_info(struct pci_dev *pdev,
3826 struct amd_iommu_device_info *info)
3827{
3828 int max_pasids;
3829 int pos;
3830
3831 if (pdev == NULL || info == NULL)
3832 return -EINVAL;
3833
3834 if (!amd_iommu_v2_supported())
3835 return -EINVAL;
3836
3837 memset(info, 0, sizeof(*info));
3838
3839 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3840 if (pos)
3841 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3842
3843 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3844 if (pos)
3845 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3846
3847 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3848 if (pos) {
3849 int features;
3850
3851 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3852 max_pasids = min(max_pasids, (1 << 20));
3853
3854 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3855 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3856
3857 features = pci_pasid_features(pdev);
3858 if (features & PCI_PASID_CAP_EXEC)
3859 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3860 if (features & PCI_PASID_CAP_PRIV)
3861 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3862 }
3863
3864 return 0;
3865}
3866EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003867
3868#ifdef CONFIG_IRQ_REMAP
3869
3870/*****************************************************************************
3871 *
3872 * Interrupt Remapping Implementation
3873 *
3874 *****************************************************************************/
3875
3876union irte {
3877 u32 val;
3878 struct {
3879 u32 valid : 1,
3880 no_fault : 1,
3881 int_type : 3,
3882 rq_eoi : 1,
3883 dm : 1,
3884 rsvd_1 : 1,
3885 destination : 8,
3886 vector : 8,
3887 rsvd_2 : 8;
3888 } fields;
3889};
3890
3891#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3892#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3893#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3894#define DTE_IRQ_REMAP_ENABLE 1ULL
3895
3896static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3897{
3898 u64 dte;
3899
3900 dte = amd_iommu_dev_table[devid].data[2];
3901 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3902 dte |= virt_to_phys(table->table);
3903 dte |= DTE_IRQ_REMAP_INTCTL;
3904 dte |= DTE_IRQ_TABLE_LEN;
3905 dte |= DTE_IRQ_REMAP_ENABLE;
3906
3907 amd_iommu_dev_table[devid].data[2] = dte;
3908}
3909
3910#define IRTE_ALLOCATED (~1U)
3911
3912static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3913{
3914 struct irq_remap_table *table = NULL;
3915 struct amd_iommu *iommu;
3916 unsigned long flags;
3917 u16 alias;
3918
3919 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3920
3921 iommu = amd_iommu_rlookup_table[devid];
3922 if (!iommu)
3923 goto out_unlock;
3924
3925 table = irq_lookup_table[devid];
3926 if (table)
3927 goto out;
3928
3929 alias = amd_iommu_alias_table[devid];
3930 table = irq_lookup_table[alias];
3931 if (table) {
3932 irq_lookup_table[devid] = table;
3933 set_dte_irq_entry(devid, table);
3934 iommu_flush_dte(iommu, devid);
3935 goto out;
3936 }
3937
3938 /* Nothing there yet, allocate new irq remapping table */
3939 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3940 if (!table)
3941 goto out;
3942
Joerg Roedel197887f2013-04-09 21:14:08 +02003943 /* Initialize table spin-lock */
3944 spin_lock_init(&table->lock);
3945
Joerg Roedel2b324502012-06-21 16:29:10 +02003946 if (ioapic)
3947 /* Keep the first 32 indexes free for IOAPIC interrupts */
3948 table->min_index = 32;
3949
3950 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3951 if (!table->table) {
3952 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003953 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003954 goto out;
3955 }
3956
3957 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3958
3959 if (ioapic) {
3960 int i;
3961
3962 for (i = 0; i < 32; ++i)
3963 table->table[i] = IRTE_ALLOCATED;
3964 }
3965
3966 irq_lookup_table[devid] = table;
3967 set_dte_irq_entry(devid, table);
3968 iommu_flush_dte(iommu, devid);
3969 if (devid != alias) {
3970 irq_lookup_table[alias] = table;
3971 set_dte_irq_entry(devid, table);
3972 iommu_flush_dte(iommu, alias);
3973 }
3974
3975out:
3976 iommu_completion_wait(iommu);
3977
3978out_unlock:
3979 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3980
3981 return table;
3982}
3983
3984static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3985{
3986 struct irq_remap_table *table;
3987 unsigned long flags;
3988 int index, c;
3989
3990 table = get_irq_table(devid, false);
3991 if (!table)
3992 return -ENODEV;
3993
3994 spin_lock_irqsave(&table->lock, flags);
3995
3996 /* Scan table for free entries */
3997 for (c = 0, index = table->min_index;
3998 index < MAX_IRQS_PER_TABLE;
3999 ++index) {
4000 if (table->table[index] == 0)
4001 c += 1;
4002 else
4003 c = 0;
4004
4005 if (c == count) {
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004006 struct irq_2_irte *irte_info;
Joerg Roedel2b324502012-06-21 16:29:10 +02004007
4008 for (; c != 0; --c)
4009 table->table[index - c + 1] = IRTE_ALLOCATED;
4010
4011 index -= count - 1;
4012
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004013 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004014 irte_info = &cfg->irq_2_irte;
4015 irte_info->devid = devid;
4016 irte_info->index = index;
Joerg Roedel2b324502012-06-21 16:29:10 +02004017
4018 goto out;
4019 }
4020 }
4021
4022 index = -ENOSPC;
4023
4024out:
4025 spin_unlock_irqrestore(&table->lock, flags);
4026
4027 return index;
4028}
4029
4030static int get_irte(u16 devid, int index, union irte *irte)
4031{
4032 struct irq_remap_table *table;
4033 unsigned long flags;
4034
4035 table = get_irq_table(devid, false);
4036 if (!table)
4037 return -ENOMEM;
4038
4039 spin_lock_irqsave(&table->lock, flags);
4040 irte->val = table->table[index];
4041 spin_unlock_irqrestore(&table->lock, flags);
4042
4043 return 0;
4044}
4045
4046static int modify_irte(u16 devid, int index, union irte irte)
4047{
4048 struct irq_remap_table *table;
4049 struct amd_iommu *iommu;
4050 unsigned long flags;
4051
4052 iommu = amd_iommu_rlookup_table[devid];
4053 if (iommu == NULL)
4054 return -EINVAL;
4055
4056 table = get_irq_table(devid, false);
4057 if (!table)
4058 return -ENOMEM;
4059
4060 spin_lock_irqsave(&table->lock, flags);
4061 table->table[index] = irte.val;
4062 spin_unlock_irqrestore(&table->lock, flags);
4063
4064 iommu_flush_irt(iommu, devid);
4065 iommu_completion_wait(iommu);
4066
4067 return 0;
4068}
4069
4070static void free_irte(u16 devid, int index)
4071{
4072 struct irq_remap_table *table;
4073 struct amd_iommu *iommu;
4074 unsigned long flags;
4075
4076 iommu = amd_iommu_rlookup_table[devid];
4077 if (iommu == NULL)
4078 return;
4079
4080 table = get_irq_table(devid, false);
4081 if (!table)
4082 return;
4083
4084 spin_lock_irqsave(&table->lock, flags);
4085 table->table[index] = 0;
4086 spin_unlock_irqrestore(&table->lock, flags);
4087
4088 iommu_flush_irt(iommu, devid);
4089 iommu_completion_wait(iommu);
4090}
4091
Joerg Roedel5527de72012-06-26 11:17:32 +02004092static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4093 unsigned int destination, int vector,
4094 struct io_apic_irq_attr *attr)
4095{
4096 struct irq_remap_table *table;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004097 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004098 struct irq_cfg *cfg;
4099 union irte irte;
4100 int ioapic_id;
4101 int index;
4102 int devid;
4103 int ret;
4104
4105 cfg = irq_get_chip_data(irq);
4106 if (!cfg)
4107 return -EINVAL;
4108
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004109 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004110 ioapic_id = mpc_ioapic_id(attr->ioapic);
4111 devid = get_ioapic_devid(ioapic_id);
4112
4113 if (devid < 0)
4114 return devid;
4115
4116 table = get_irq_table(devid, true);
4117 if (table == NULL)
4118 return -ENOMEM;
4119
4120 index = attr->ioapic_pin;
4121
4122 /* Setup IRQ remapping info */
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004123 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004124 irte_info->devid = devid;
4125 irte_info->index = index;
Joerg Roedel5527de72012-06-26 11:17:32 +02004126
4127 /* Setup IRTE for IOMMU */
4128 irte.val = 0;
4129 irte.fields.vector = vector;
4130 irte.fields.int_type = apic->irq_delivery_mode;
4131 irte.fields.destination = destination;
4132 irte.fields.dm = apic->irq_dest_mode;
4133 irte.fields.valid = 1;
4134
4135 ret = modify_irte(devid, index, irte);
4136 if (ret)
4137 return ret;
4138
4139 /* Setup IOAPIC entry */
4140 memset(entry, 0, sizeof(*entry));
4141
4142 entry->vector = index;
4143 entry->mask = 0;
4144 entry->trigger = attr->trigger;
4145 entry->polarity = attr->polarity;
4146
4147 /*
4148 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004149 */
4150 if (attr->trigger)
4151 entry->mask = 1;
4152
4153 return 0;
4154}
4155
4156static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4157 bool force)
4158{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004159 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004160 unsigned int dest, irq;
4161 struct irq_cfg *cfg;
4162 union irte irte;
4163 int err;
4164
4165 if (!config_enabled(CONFIG_SMP))
4166 return -1;
4167
4168 cfg = data->chip_data;
4169 irq = data->irq;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004170 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004171
4172 if (!cpumask_intersects(mask, cpu_online_mask))
4173 return -EINVAL;
4174
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004175 if (get_irte(irte_info->devid, irte_info->index, &irte))
Joerg Roedel5527de72012-06-26 11:17:32 +02004176 return -EBUSY;
4177
4178 if (assign_irq_vector(irq, cfg, mask))
4179 return -EBUSY;
4180
4181 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4182 if (err) {
4183 if (assign_irq_vector(irq, cfg, data->affinity))
4184 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4185 return err;
4186 }
4187
4188 irte.fields.vector = cfg->vector;
4189 irte.fields.destination = dest;
4190
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004191 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel5527de72012-06-26 11:17:32 +02004192
4193 if (cfg->move_in_progress)
4194 send_cleanup_vector(cfg);
4195
4196 cpumask_copy(data->affinity, mask);
4197
4198 return 0;
4199}
4200
4201static int free_irq(int irq)
4202{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004203 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004204 struct irq_cfg *cfg;
4205
4206 cfg = irq_get_chip_data(irq);
4207 if (!cfg)
4208 return -EINVAL;
4209
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004210 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004211
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004212 free_irte(irte_info->devid, irte_info->index);
Joerg Roedel5527de72012-06-26 11:17:32 +02004213
4214 return 0;
4215}
4216
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004217static void compose_msi_msg(struct pci_dev *pdev,
4218 unsigned int irq, unsigned int dest,
4219 struct msi_msg *msg, u8 hpet_id)
4220{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004221 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004222 struct irq_cfg *cfg;
4223 union irte irte;
4224
4225 cfg = irq_get_chip_data(irq);
4226 if (!cfg)
4227 return;
4228
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004229 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004230
4231 irte.val = 0;
4232 irte.fields.vector = cfg->vector;
4233 irte.fields.int_type = apic->irq_delivery_mode;
4234 irte.fields.destination = dest;
4235 irte.fields.dm = apic->irq_dest_mode;
4236 irte.fields.valid = 1;
4237
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004238 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004239
4240 msg->address_hi = MSI_ADDR_BASE_HI;
4241 msg->address_lo = MSI_ADDR_BASE_LO;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004242 msg->data = irte_info->index;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004243}
4244
4245static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4246{
4247 struct irq_cfg *cfg;
4248 int index;
4249 u16 devid;
4250
4251 if (!pdev)
4252 return -EINVAL;
4253
4254 cfg = irq_get_chip_data(irq);
4255 if (!cfg)
4256 return -EINVAL;
4257
4258 devid = get_device_id(&pdev->dev);
4259 index = alloc_irq_index(cfg, devid, nvec);
4260
4261 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4262}
4263
4264static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4265 int index, int offset)
4266{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004267 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004268 struct irq_cfg *cfg;
4269 u16 devid;
4270
4271 if (!pdev)
4272 return -EINVAL;
4273
4274 cfg = irq_get_chip_data(irq);
4275 if (!cfg)
4276 return -EINVAL;
4277
4278 if (index >= MAX_IRQS_PER_TABLE)
4279 return 0;
4280
4281 devid = get_device_id(&pdev->dev);
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004282 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004283
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004284 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004285 irte_info->devid = devid;
4286 irte_info->index = index + offset;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004287
4288 return 0;
4289}
4290
Joerg Roedeld9761952012-06-26 16:00:08 +02004291static int setup_hpet_msi(unsigned int irq, unsigned int id)
4292{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004293 struct irq_2_irte *irte_info;
Joerg Roedeld9761952012-06-26 16:00:08 +02004294 struct irq_cfg *cfg;
4295 int index, devid;
4296
4297 cfg = irq_get_chip_data(irq);
4298 if (!cfg)
4299 return -EINVAL;
4300
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004301 irte_info = &cfg->irq_2_irte;
Joerg Roedeld9761952012-06-26 16:00:08 +02004302 devid = get_hpet_devid(id);
4303 if (devid < 0)
4304 return devid;
4305
4306 index = alloc_irq_index(cfg, devid, 1);
4307 if (index < 0)
4308 return index;
4309
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004310 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004311 irte_info->devid = devid;
4312 irte_info->index = index;
Joerg Roedeld9761952012-06-26 16:00:08 +02004313
4314 return 0;
4315}
4316
Joerg Roedel6b474b82012-06-26 16:46:04 +02004317struct irq_remap_ops amd_iommu_irq_ops = {
4318 .supported = amd_iommu_supported,
4319 .prepare = amd_iommu_prepare,
4320 .enable = amd_iommu_enable,
4321 .disable = amd_iommu_disable,
4322 .reenable = amd_iommu_reenable,
4323 .enable_faulting = amd_iommu_enable_faulting,
4324 .setup_ioapic_entry = setup_ioapic_entry,
4325 .set_affinity = set_affinity,
4326 .free_irq = free_irq,
4327 .compose_msi_msg = compose_msi_msg,
4328 .msi_alloc_irq = msi_alloc_irq,
4329 .msi_setup_irq = msi_setup_irq,
4330 .setup_hpet_msi = setup_hpet_msi,
4331};
Joerg Roedel2b324502012-06-21 16:29:10 +02004332#endif