blob: 25d7571dfc1c0c63b9808044e4e98ab0ccc050eb [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
Yijing Wangb82a2272013-12-05 19:42:41 +0800251 /* No PCI device */
252 if (!dev_is_pci(dev))
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
Alex Williamsonc1931092014-07-03 09:51:24 -0600430static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
431{
432 *(u16 *)data = alias;
433 return 0;
434}
435
436static u16 get_alias(struct device *dev)
437{
438 struct pci_dev *pdev = to_pci_dev(dev);
439 u16 devid, ivrs_alias, pci_alias;
440
441 devid = get_device_id(dev);
442 ivrs_alias = amd_iommu_alias_table[devid];
443 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
444
445 if (ivrs_alias == pci_alias)
446 return ivrs_alias;
447
448 /*
449 * DMA alias showdown
450 *
451 * The IVRS is fairly reliable in telling us about aliases, but it
452 * can't know about every screwy device. If we don't have an IVRS
453 * reported alias, use the PCI reported alias. In that case we may
454 * still need to initialize the rlookup and dev_table entries if the
455 * alias is to a non-existent device.
456 */
457 if (ivrs_alias == devid) {
458 if (!amd_iommu_rlookup_table[pci_alias]) {
459 amd_iommu_rlookup_table[pci_alias] =
460 amd_iommu_rlookup_table[devid];
461 memcpy(amd_iommu_dev_table[pci_alias].data,
462 amd_iommu_dev_table[devid].data,
463 sizeof(amd_iommu_dev_table[pci_alias].data));
464 }
465
466 return pci_alias;
467 }
468
469 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
470 "for device %s[%04x:%04x], kernel reported alias "
471 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
472 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
473 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
474 PCI_FUNC(pci_alias));
475
476 /*
477 * If we don't have a PCI DMA alias and the IVRS alias is on the same
478 * bus, then the IVRS table may know about a quirk that we don't.
479 */
480 if (pci_alias == devid &&
481 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
482 pdev->dev_flags |= PCI_DEV_FLAGS_DMA_ALIAS_DEVFN;
483 pdev->dma_alias_devfn = ivrs_alias & 0xff;
484 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
485 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
486 dev_name(dev));
487 }
488
489 return ivrs_alias;
490}
491
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600492static int iommu_init_device(struct device *dev)
493{
494 struct pci_dev *pdev = to_pci_dev(dev);
495 struct iommu_dev_data *dev_data;
496 u16 alias;
497 int ret;
498
499 if (dev->archdata.iommu)
500 return 0;
501
502 dev_data = find_dev_data(get_device_id(dev));
503 if (!dev_data)
504 return -ENOMEM;
505
Alex Williamsonc1931092014-07-03 09:51:24 -0600506 alias = get_alias(dev);
507
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600508 if (alias != dev_data->devid) {
509 struct iommu_dev_data *alias_data;
510
511 alias_data = find_dev_data(alias);
512 if (alias_data == NULL) {
513 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
514 dev_name(dev));
515 free_dev_data(dev_data);
516 return -ENOTSUPP;
517 }
518 dev_data->alias_data = alias_data;
519 }
520
521 ret = init_iommu_group(dev);
Radmila Kompováe644a012013-05-02 17:24:25 +0200522 if (ret) {
523 free_dev_data(dev_data);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600524 return ret;
Radmila Kompováe644a012013-05-02 17:24:25 +0200525 }
Alex Williamson9dcd6132012-05-30 14:19:07 -0600526
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100527 if (pci_iommuv2_capable(pdev)) {
528 struct amd_iommu *iommu;
529
530 iommu = amd_iommu_rlookup_table[dev_data->devid];
531 dev_data->iommu_v2 = iommu->is_iommu_v2;
532 }
533
Joerg Roedel657cbb62009-11-23 15:26:46 +0100534 dev->archdata.iommu = dev_data;
535
Joerg Roedel657cbb62009-11-23 15:26:46 +0100536 return 0;
537}
538
Joerg Roedel26018872011-06-06 16:50:14 +0200539static void iommu_ignore_device(struct device *dev)
540{
541 u16 devid, alias;
542
543 devid = get_device_id(dev);
544 alias = amd_iommu_alias_table[devid];
545
546 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
547 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
548
549 amd_iommu_rlookup_table[devid] = NULL;
550 amd_iommu_rlookup_table[alias] = NULL;
551}
552
Joerg Roedel657cbb62009-11-23 15:26:46 +0100553static void iommu_uninit_device(struct device *dev)
554{
Alex Williamsonc1931092014-07-03 09:51:24 -0600555 struct iommu_dev_data *dev_data = search_dev_data(get_device_id(dev));
556
557 if (!dev_data)
558 return;
559
Alex Williamson9dcd6132012-05-30 14:19:07 -0600560 iommu_group_remove_device(dev);
561
Alex Williamsonc1931092014-07-03 09:51:24 -0600562 /* Unlink from alias, it may change if another device is re-plugged */
563 dev_data->alias_data = NULL;
564
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200565 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600566 * We keep dev_data around for unplugged devices and reuse it when the
567 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200568 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100569}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100570
571void __init amd_iommu_uninit_devices(void)
572{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200573 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100574 struct pci_dev *pdev = NULL;
575
576 for_each_pci_dev(pdev) {
577
578 if (!check_device(&pdev->dev))
579 continue;
580
581 iommu_uninit_device(&pdev->dev);
582 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200583
584 /* Free all of our dev_data structures */
585 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
586 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100587}
588
589int __init amd_iommu_init_devices(void)
590{
591 struct pci_dev *pdev = NULL;
592 int ret = 0;
593
594 for_each_pci_dev(pdev) {
595
596 if (!check_device(&pdev->dev))
597 continue;
598
599 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200600 if (ret == -ENOTSUPP)
601 iommu_ignore_device(&pdev->dev);
602 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100603 goto out_free;
604 }
605
606 return 0;
607
608out_free:
609
610 amd_iommu_uninit_devices();
611
612 return ret;
613}
Joerg Roedel7f265082008-12-12 13:50:21 +0100614#ifdef CONFIG_AMD_IOMMU_STATS
615
616/*
617 * Initialization code for statistics collection
618 */
619
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100620DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100621DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100622DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100623DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100624DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100625DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100626DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100627DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100628DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100629DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100630DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100631DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100632DECLARE_STATS_COUNTER(complete_ppr);
633DECLARE_STATS_COUNTER(invalidate_iotlb);
634DECLARE_STATS_COUNTER(invalidate_iotlb_all);
635DECLARE_STATS_COUNTER(pri_requests);
636
Joerg Roedel7f265082008-12-12 13:50:21 +0100637static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100638static struct dentry *de_fflush;
639
640static void amd_iommu_stats_add(struct __iommu_counter *cnt)
641{
642 if (stats_dir == NULL)
643 return;
644
645 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
646 &cnt->value);
647}
648
649static void amd_iommu_stats_init(void)
650{
651 stats_dir = debugfs_create_dir("amd-iommu", NULL);
652 if (stats_dir == NULL)
653 return;
654
Joerg Roedel7f265082008-12-12 13:50:21 +0100655 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300656 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100657
658 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100659 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100660 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100661 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100662 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100663 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100664 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100665 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100666 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100667 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100668 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100669 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100670 amd_iommu_stats_add(&complete_ppr);
671 amd_iommu_stats_add(&invalidate_iotlb);
672 amd_iommu_stats_add(&invalidate_iotlb_all);
673 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100674}
675
676#endif
677
Joerg Roedel431b2a22008-07-11 17:14:22 +0200678/****************************************************************************
679 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200680 * Interrupt handling functions
681 *
682 ****************************************************************************/
683
Joerg Roedele3e59872009-09-03 14:02:10 +0200684static void dump_dte_entry(u16 devid)
685{
686 int i;
687
Joerg Roedelee6c2862011-11-09 12:06:03 +0100688 for (i = 0; i < 4; ++i)
689 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200690 amd_iommu_dev_table[devid].data[i]);
691}
692
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200693static void dump_command(unsigned long phys_addr)
694{
695 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
696 int i;
697
698 for (i = 0; i < 4; ++i)
699 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
700}
701
Joerg Roedela345b232009-09-03 15:01:43 +0200702static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200703{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200704 int type, devid, domid, flags;
705 volatile u32 *event = __evt;
706 int count = 0;
707 u64 address;
708
709retry:
710 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
711 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
712 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
713 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
714 address = (u64)(((u64)event[3]) << 32) | event[2];
715
716 if (type == 0) {
717 /* Did we hit the erratum? */
718 if (++count == LOOP_TIMEOUT) {
719 pr_err("AMD-Vi: No event written to event log\n");
720 return;
721 }
722 udelay(1);
723 goto retry;
724 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200725
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200726 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200727
728 switch (type) {
729 case EVENT_TYPE_ILL_DEV:
730 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
731 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700732 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200733 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200734 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200735 break;
736 case EVENT_TYPE_IO_FAULT:
737 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
738 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700739 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200740 domid, address, flags);
741 break;
742 case EVENT_TYPE_DEV_TAB_ERR:
743 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
744 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700745 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200746 address, flags);
747 break;
748 case EVENT_TYPE_PAGE_TAB_ERR:
749 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
750 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700751 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200752 domid, address, flags);
753 break;
754 case EVENT_TYPE_ILL_CMD:
755 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200756 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200757 break;
758 case EVENT_TYPE_CMD_HARD_ERR:
759 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
760 "flags=0x%04x]\n", address, flags);
761 break;
762 case EVENT_TYPE_IOTLB_INV_TO:
763 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
764 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700765 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200766 address);
767 break;
768 case EVENT_TYPE_INV_DEV_REQ:
769 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
770 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700771 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200772 address, flags);
773 break;
774 default:
775 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
776 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200777
778 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200779}
780
781static void iommu_poll_events(struct amd_iommu *iommu)
782{
783 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200784
785 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
786 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
787
788 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200789 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200790 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
791 }
792
793 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200794}
795
Joerg Roedeleee53532012-06-01 15:20:23 +0200796static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100797{
798 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100799
Joerg Roedel399be2f2011-12-01 16:53:47 +0100800 INC_STATS_COUNTER(pri_requests);
801
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100802 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
803 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
804 return;
805 }
806
807 fault.address = raw[1];
808 fault.pasid = PPR_PASID(raw[0]);
809 fault.device_id = PPR_DEVID(raw[0]);
810 fault.tag = PPR_TAG(raw[0]);
811 fault.flags = PPR_FLAGS(raw[0]);
812
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100813 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
814}
815
816static void iommu_poll_ppr_log(struct amd_iommu *iommu)
817{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100818 u32 head, tail;
819
820 if (iommu->ppr_log == NULL)
821 return;
822
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100823 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
824 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
825
826 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200827 volatile u64 *raw;
828 u64 entry[2];
829 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100830
Joerg Roedeleee53532012-06-01 15:20:23 +0200831 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100832
Joerg Roedeleee53532012-06-01 15:20:23 +0200833 /*
834 * Hardware bug: Interrupt may arrive before the entry is
835 * written to memory. If this happens we need to wait for the
836 * entry to arrive.
837 */
838 for (i = 0; i < LOOP_TIMEOUT; ++i) {
839 if (PPR_REQ_TYPE(raw[0]) != 0)
840 break;
841 udelay(1);
842 }
843
844 /* Avoid memcpy function-call overhead */
845 entry[0] = raw[0];
846 entry[1] = raw[1];
847
848 /*
849 * To detect the hardware bug we need to clear the entry
850 * back to zero.
851 */
852 raw[0] = raw[1] = 0UL;
853
854 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100855 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
856 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200857
Joerg Roedeleee53532012-06-01 15:20:23 +0200858 /* Handle PPR entry */
859 iommu_handle_ppr_entry(iommu, entry);
860
Joerg Roedeleee53532012-06-01 15:20:23 +0200861 /* Refresh ring-buffer information */
862 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100863 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
864 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100865}
866
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200867irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200868{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500869 struct amd_iommu *iommu = (struct amd_iommu *) data;
870 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200871
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500872 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
873 /* Enable EVT and PPR interrupts again */
874 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
875 iommu->mmio_base + MMIO_STATUS_OFFSET);
876
877 if (status & MMIO_STATUS_EVT_INT_MASK) {
878 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
879 iommu_poll_events(iommu);
880 }
881
882 if (status & MMIO_STATUS_PPR_INT_MASK) {
883 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
884 iommu_poll_ppr_log(iommu);
885 }
886
887 /*
888 * Hardware bug: ERBT1312
889 * When re-enabling interrupt (by writing 1
890 * to clear the bit), the hardware might also try to set
891 * the interrupt bit in the event status register.
892 * In this scenario, the bit will be set, and disable
893 * subsequent interrupts.
894 *
895 * Workaround: The IOMMU driver should read back the
896 * status register and check if the interrupt bits are cleared.
897 * If not, driver will need to go through the interrupt handler
898 * again and re-clear the bits
899 */
900 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100901 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200902 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200903}
904
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200905irqreturn_t amd_iommu_int_handler(int irq, void *data)
906{
907 return IRQ_WAKE_THREAD;
908}
909
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200910/****************************************************************************
911 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200912 * IOMMU command queuing functions
913 *
914 ****************************************************************************/
915
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200916static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200917{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200918 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200919
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200920 while (*sem == 0 && i < LOOP_TIMEOUT) {
921 udelay(1);
922 i += 1;
923 }
924
925 if (i == LOOP_TIMEOUT) {
926 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
927 return -EIO;
928 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200929
930 return 0;
931}
932
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200933static void copy_cmd_to_buffer(struct amd_iommu *iommu,
934 struct iommu_cmd *cmd,
935 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200936{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200937 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200938
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200939 target = iommu->cmd_buf + tail;
940 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200941
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200942 /* Copy command to buffer */
943 memcpy(target, cmd, sizeof(*cmd));
944
945 /* Tell the IOMMU about it */
946 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
947}
948
Joerg Roedel815b33f2011-04-06 17:26:49 +0200949static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200950{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200951 WARN_ON(address & 0x7ULL);
952
Joerg Roedelded46732011-04-06 10:53:48 +0200953 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200954 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
955 cmd->data[1] = upper_32_bits(__pa(address));
956 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200957 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
958}
959
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200960static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
961{
962 memset(cmd, 0, sizeof(*cmd));
963 cmd->data[0] = devid;
964 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
965}
966
Joerg Roedel11b64022011-04-06 11:49:28 +0200967static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
968 size_t size, u16 domid, int pde)
969{
970 u64 pages;
971 int s;
972
973 pages = iommu_num_pages(address, size, PAGE_SIZE);
974 s = 0;
975
976 if (pages > 1) {
977 /*
978 * If we have to flush more than one page, flush all
979 * TLB entries for this domain
980 */
981 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
982 s = 1;
983 }
984
985 address &= PAGE_MASK;
986
987 memset(cmd, 0, sizeof(*cmd));
988 cmd->data[1] |= domid;
989 cmd->data[2] = lower_32_bits(address);
990 cmd->data[3] = upper_32_bits(address);
991 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
992 if (s) /* size bit - we flush more than one 4kb page */
993 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200994 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200995 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
996}
997
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200998static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
999 u64 address, size_t size)
1000{
1001 u64 pages;
1002 int s;
1003
1004 pages = iommu_num_pages(address, size, PAGE_SIZE);
1005 s = 0;
1006
1007 if (pages > 1) {
1008 /*
1009 * If we have to flush more than one page, flush all
1010 * TLB entries for this domain
1011 */
1012 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
1013 s = 1;
1014 }
1015
1016 address &= PAGE_MASK;
1017
1018 memset(cmd, 0, sizeof(*cmd));
1019 cmd->data[0] = devid;
1020 cmd->data[0] |= (qdep & 0xff) << 24;
1021 cmd->data[1] = devid;
1022 cmd->data[2] = lower_32_bits(address);
1023 cmd->data[3] = upper_32_bits(address);
1024 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1025 if (s)
1026 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1027}
1028
Joerg Roedel22e266c2011-11-21 15:59:08 +01001029static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
1030 u64 address, bool size)
1031{
1032 memset(cmd, 0, sizeof(*cmd));
1033
1034 address &= ~(0xfffULL);
1035
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001036 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +01001037 cmd->data[1] = domid;
1038 cmd->data[2] = lower_32_bits(address);
1039 cmd->data[3] = upper_32_bits(address);
1040 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1041 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1042 if (size)
1043 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1044 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1045}
1046
1047static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
1048 int qdep, u64 address, bool size)
1049{
1050 memset(cmd, 0, sizeof(*cmd));
1051
1052 address &= ~(0xfffULL);
1053
1054 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -06001055 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +01001056 cmd->data[0] |= (qdep & 0xff) << 24;
1057 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -06001058 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +01001059 cmd->data[2] = lower_32_bits(address);
1060 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1061 cmd->data[3] = upper_32_bits(address);
1062 if (size)
1063 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1064 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1065}
1066
Joerg Roedelc99afa22011-11-21 18:19:25 +01001067static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
1068 int status, int tag, bool gn)
1069{
1070 memset(cmd, 0, sizeof(*cmd));
1071
1072 cmd->data[0] = devid;
1073 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001074 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001075 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1076 }
1077 cmd->data[3] = tag & 0x1ff;
1078 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1079
1080 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1081}
1082
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001083static void build_inv_all(struct iommu_cmd *cmd)
1084{
1085 memset(cmd, 0, sizeof(*cmd));
1086 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001087}
1088
Joerg Roedel7ef27982012-06-21 16:46:04 +02001089static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1090{
1091 memset(cmd, 0, sizeof(*cmd));
1092 cmd->data[0] = devid;
1093 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1094}
1095
Joerg Roedel431b2a22008-07-11 17:14:22 +02001096/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001097 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001098 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001099 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001100static int iommu_queue_command_sync(struct amd_iommu *iommu,
1101 struct iommu_cmd *cmd,
1102 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001103{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001104 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +02001105 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001106
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001107 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +01001108
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001109again:
Joerg Roedel815b33f2011-04-06 17:26:49 +02001110 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001111
1112 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1113 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1114 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
1115 left = (head - next_tail) % iommu->cmd_buf_size;
1116
1117 if (left <= 2) {
1118 struct iommu_cmd sync_cmd;
1119 volatile u64 sem = 0;
1120 int ret;
1121
1122 build_completion_wait(&sync_cmd, (u64)&sem);
1123 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1124
1125 spin_unlock_irqrestore(&iommu->lock, flags);
1126
1127 if ((ret = wait_on_sem(&sem)) != 0)
1128 return ret;
1129
1130 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001131 }
1132
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001133 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001134
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001135 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001136 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001137
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001138 spin_unlock_irqrestore(&iommu->lock, flags);
1139
Joerg Roedel815b33f2011-04-06 17:26:49 +02001140 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001141}
1142
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001143static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1144{
1145 return iommu_queue_command_sync(iommu, cmd, true);
1146}
1147
Joerg Roedel8d201962008-12-02 20:34:41 +01001148/*
1149 * This function queues a completion wait command into the command
1150 * buffer of an IOMMU
1151 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001152static int iommu_completion_wait(struct amd_iommu *iommu)
1153{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001154 struct iommu_cmd cmd;
1155 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001156 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001157
1158 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001159 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001160
Joerg Roedel815b33f2011-04-06 17:26:49 +02001161 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001162
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001163 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001164 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001165 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001166
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001167 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001168}
1169
Joerg Roedeld8c13082011-04-06 18:51:26 +02001170static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001171{
1172 struct iommu_cmd cmd;
1173
Joerg Roedeld8c13082011-04-06 18:51:26 +02001174 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001175
Joerg Roedeld8c13082011-04-06 18:51:26 +02001176 return iommu_queue_command(iommu, &cmd);
1177}
1178
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001179static void iommu_flush_dte_all(struct amd_iommu *iommu)
1180{
1181 u32 devid;
1182
1183 for (devid = 0; devid <= 0xffff; ++devid)
1184 iommu_flush_dte(iommu, devid);
1185
1186 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001187}
1188
1189/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001190 * This function uses heavy locking and may disable irqs for some time. But
1191 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001192 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001193static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001194{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001195 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001196
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001197 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1198 struct iommu_cmd cmd;
1199 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1200 dom_id, 1);
1201 iommu_queue_command(iommu, &cmd);
1202 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001203
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001204 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001205}
1206
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001207static void iommu_flush_all(struct amd_iommu *iommu)
1208{
1209 struct iommu_cmd cmd;
1210
1211 build_inv_all(&cmd);
1212
1213 iommu_queue_command(iommu, &cmd);
1214 iommu_completion_wait(iommu);
1215}
1216
Joerg Roedel7ef27982012-06-21 16:46:04 +02001217static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1218{
1219 struct iommu_cmd cmd;
1220
1221 build_inv_irt(&cmd, devid);
1222
1223 iommu_queue_command(iommu, &cmd);
1224}
1225
1226static void iommu_flush_irt_all(struct amd_iommu *iommu)
1227{
1228 u32 devid;
1229
1230 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1231 iommu_flush_irt(iommu, devid);
1232
1233 iommu_completion_wait(iommu);
1234}
1235
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001236void iommu_flush_all_caches(struct amd_iommu *iommu)
1237{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001238 if (iommu_feature(iommu, FEATURE_IA)) {
1239 iommu_flush_all(iommu);
1240 } else {
1241 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001242 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001243 iommu_flush_tlb_all(iommu);
1244 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001245}
1246
Joerg Roedel431b2a22008-07-11 17:14:22 +02001247/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001248 * Command send function for flushing on-device TLB
1249 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001250static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1251 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001252{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001253 struct amd_iommu *iommu;
1254 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001255 int qdep;
1256
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001257 qdep = dev_data->ats.qdep;
1258 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001259
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001260 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001261
1262 return iommu_queue_command(iommu, &cmd);
1263}
1264
1265/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001266 * Command send function for invalidating a device table entry
1267 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001268static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001269{
1270 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001271 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001272
Joerg Roedel6c542042011-06-09 17:07:31 +02001273 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001274
Joerg Roedelf62dda62011-06-09 12:55:35 +02001275 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001276 if (ret)
1277 return ret;
1278
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001279 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001280 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001281
1282 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001283}
1284
Joerg Roedel431b2a22008-07-11 17:14:22 +02001285/*
1286 * TLB invalidation function which is called from the mapping functions.
1287 * It invalidates a single PTE if the range to flush is within a single
1288 * page. Otherwise it flushes the whole TLB of the IOMMU.
1289 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001290static void __domain_flush_pages(struct protection_domain *domain,
1291 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001292{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001293 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001294 struct iommu_cmd cmd;
1295 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001296
Joerg Roedel11b64022011-04-06 11:49:28 +02001297 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001298
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001299 for (i = 0; i < amd_iommus_present; ++i) {
1300 if (!domain->dev_iommu[i])
1301 continue;
1302
1303 /*
1304 * Devices of this domain are behind this IOMMU
1305 * We need a TLB flush
1306 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001307 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001308 }
1309
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001310 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001311
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001312 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001313 continue;
1314
Joerg Roedel6c542042011-06-09 17:07:31 +02001315 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001316 }
1317
Joerg Roedel11b64022011-04-06 11:49:28 +02001318 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001319}
1320
Joerg Roedel17b124b2011-04-06 18:01:35 +02001321static void domain_flush_pages(struct protection_domain *domain,
1322 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001323{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001324 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001325}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001326
Joerg Roedel1c655772008-09-04 18:40:05 +02001327/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001328static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001329{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001330 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001331}
1332
Chris Wright42a49f92009-06-15 15:42:00 +02001333/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001334static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001335{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001336 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1337}
1338
1339static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001340{
1341 int i;
1342
1343 for (i = 0; i < amd_iommus_present; ++i) {
1344 if (!domain->dev_iommu[i])
1345 continue;
1346
1347 /*
1348 * Devices of this domain are behind this IOMMU
1349 * We need to wait for completion of all commands.
1350 */
1351 iommu_completion_wait(amd_iommus[i]);
1352 }
1353}
1354
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001355
Joerg Roedel43f49602008-12-02 21:01:12 +01001356/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001357 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001358 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001359static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001360{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001361 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001362
1363 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001364 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001365}
1366
Joerg Roedel431b2a22008-07-11 17:14:22 +02001367/****************************************************************************
1368 *
1369 * The functions below are used the create the page table mappings for
1370 * unity mapped regions.
1371 *
1372 ****************************************************************************/
1373
1374/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001375 * This function is used to add another level to an IO page table. Adding
1376 * another level increases the size of the address space by 9 bits to a size up
1377 * to 64 bits.
1378 */
1379static bool increase_address_space(struct protection_domain *domain,
1380 gfp_t gfp)
1381{
1382 u64 *pte;
1383
1384 if (domain->mode == PAGE_MODE_6_LEVEL)
1385 /* address space already 64 bit large */
1386 return false;
1387
1388 pte = (void *)get_zeroed_page(gfp);
1389 if (!pte)
1390 return false;
1391
1392 *pte = PM_LEVEL_PDE(domain->mode,
1393 virt_to_phys(domain->pt_root));
1394 domain->pt_root = pte;
1395 domain->mode += 1;
1396 domain->updated = true;
1397
1398 return true;
1399}
1400
1401static u64 *alloc_pte(struct protection_domain *domain,
1402 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001403 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001404 u64 **pte_page,
1405 gfp_t gfp)
1406{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001407 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001408 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001409
1410 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001411
1412 while (address > PM_LEVEL_SIZE(domain->mode))
1413 increase_address_space(domain, gfp);
1414
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001415 level = domain->mode - 1;
1416 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1417 address = PAGE_SIZE_ALIGN(address, page_size);
1418 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001419
1420 while (level > end_lvl) {
1421 if (!IOMMU_PTE_PRESENT(*pte)) {
1422 page = (u64 *)get_zeroed_page(gfp);
1423 if (!page)
1424 return NULL;
1425 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1426 }
1427
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001428 /* No level skipping support yet */
1429 if (PM_PTE_LEVEL(*pte) != level)
1430 return NULL;
1431
Joerg Roedel308973d2009-11-24 17:43:32 +01001432 level -= 1;
1433
1434 pte = IOMMU_PTE_PAGE(*pte);
1435
1436 if (pte_page && level == end_lvl)
1437 *pte_page = pte;
1438
1439 pte = &pte[PM_LEVEL_INDEX(level, address)];
1440 }
1441
1442 return pte;
1443}
1444
1445/*
1446 * This function checks if there is a PTE for a given dma address. If
1447 * there is one, it returns the pointer to it.
1448 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001449static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001450{
1451 int level;
1452 u64 *pte;
1453
Joerg Roedel24cd7722010-01-19 17:27:39 +01001454 if (address > PM_LEVEL_SIZE(domain->mode))
1455 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001456
Joerg Roedel24cd7722010-01-19 17:27:39 +01001457 level = domain->mode - 1;
1458 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1459
1460 while (level > 0) {
1461
1462 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001463 if (!IOMMU_PTE_PRESENT(*pte))
1464 return NULL;
1465
Joerg Roedel24cd7722010-01-19 17:27:39 +01001466 /* Large PTE */
1467 if (PM_PTE_LEVEL(*pte) == 0x07) {
1468 unsigned long pte_mask, __pte;
1469
1470 /*
1471 * If we have a series of large PTEs, make
1472 * sure to return a pointer to the first one.
1473 */
1474 pte_mask = PTE_PAGE_SIZE(*pte);
1475 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1476 __pte = ((unsigned long)pte) & pte_mask;
1477
1478 return (u64 *)__pte;
1479 }
1480
1481 /* No level skipping support yet */
1482 if (PM_PTE_LEVEL(*pte) != level)
1483 return NULL;
1484
Joerg Roedel308973d2009-11-24 17:43:32 +01001485 level -= 1;
1486
Joerg Roedel24cd7722010-01-19 17:27:39 +01001487 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001488 pte = IOMMU_PTE_PAGE(*pte);
1489 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001490 }
1491
1492 return pte;
1493}
1494
1495/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001496 * Generic mapping functions. It maps a physical address into a DMA
1497 * address space. It allocates the page table pages if necessary.
1498 * In the future it can be extended to a generic mapping function
1499 * supporting all features of AMD IOMMU page tables like level skipping
1500 * and full 64 bit address spaces.
1501 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001502static int iommu_map_page(struct protection_domain *dom,
1503 unsigned long bus_addr,
1504 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001505 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001506 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001507{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001508 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001509 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001510
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001511 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001512 return -EINVAL;
1513
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001514 bus_addr = PAGE_ALIGN(bus_addr);
1515 phys_addr = PAGE_ALIGN(phys_addr);
1516 count = PAGE_SIZE_PTE_COUNT(page_size);
1517 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001518
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001519 for (i = 0; i < count; ++i)
1520 if (IOMMU_PTE_PRESENT(pte[i]))
1521 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001522
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001523 if (page_size > PAGE_SIZE) {
1524 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1525 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1526 } else
1527 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1528
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001529 if (prot & IOMMU_PROT_IR)
1530 __pte |= IOMMU_PTE_IR;
1531 if (prot & IOMMU_PROT_IW)
1532 __pte |= IOMMU_PTE_IW;
1533
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001534 for (i = 0; i < count; ++i)
1535 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001536
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001537 update_domain(dom);
1538
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001539 return 0;
1540}
1541
Joerg Roedel24cd7722010-01-19 17:27:39 +01001542static unsigned long iommu_unmap_page(struct protection_domain *dom,
1543 unsigned long bus_addr,
1544 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001545{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001546 unsigned long long unmap_size, unmapped;
1547 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001548
Joerg Roedel24cd7722010-01-19 17:27:39 +01001549 BUG_ON(!is_power_of_2(page_size));
1550
1551 unmapped = 0;
1552
1553 while (unmapped < page_size) {
1554
1555 pte = fetch_pte(dom, bus_addr);
1556
1557 if (!pte) {
1558 /*
1559 * No PTE for this address
1560 * move forward in 4kb steps
1561 */
1562 unmap_size = PAGE_SIZE;
1563 } else if (PM_PTE_LEVEL(*pte) == 0) {
1564 /* 4kb PTE found for this address */
1565 unmap_size = PAGE_SIZE;
1566 *pte = 0ULL;
1567 } else {
1568 int count, i;
1569
1570 /* Large PTE found which maps this address */
1571 unmap_size = PTE_PAGE_SIZE(*pte);
Alex Williamson60d0ca32013-06-21 14:33:19 -06001572
1573 /* Only unmap from the first pte in the page */
1574 if ((unmap_size - 1) & bus_addr)
1575 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001576 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1577 for (i = 0; i < count; i++)
1578 pte[i] = 0ULL;
1579 }
1580
1581 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1582 unmapped += unmap_size;
1583 }
1584
Alex Williamson60d0ca32013-06-21 14:33:19 -06001585 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001586
1587 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001588}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001589
Joerg Roedel431b2a22008-07-11 17:14:22 +02001590/*
1591 * This function checks if a specific unity mapping entry is needed for
1592 * this specific IOMMU.
1593 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001594static int iommu_for_unity_map(struct amd_iommu *iommu,
1595 struct unity_map_entry *entry)
1596{
1597 u16 bdf, i;
1598
1599 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1600 bdf = amd_iommu_alias_table[i];
1601 if (amd_iommu_rlookup_table[bdf] == iommu)
1602 return 1;
1603 }
1604
1605 return 0;
1606}
1607
Joerg Roedel431b2a22008-07-11 17:14:22 +02001608/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001609 * This function actually applies the mapping to the page table of the
1610 * dma_ops domain.
1611 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001612static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1613 struct unity_map_entry *e)
1614{
1615 u64 addr;
1616 int ret;
1617
1618 for (addr = e->address_start; addr < e->address_end;
1619 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001620 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001621 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001622 if (ret)
1623 return ret;
1624 /*
1625 * if unity mapping is in aperture range mark the page
1626 * as allocated in the aperture
1627 */
1628 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001629 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001630 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001631 }
1632
1633 return 0;
1634}
1635
Joerg Roedel431b2a22008-07-11 17:14:22 +02001636/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001637 * Init the unity mappings for a specific IOMMU in the system
1638 *
1639 * Basically iterates over all unity mapping entries and applies them to
1640 * the default domain DMA of that IOMMU if necessary.
1641 */
1642static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1643{
1644 struct unity_map_entry *entry;
1645 int ret;
1646
1647 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1648 if (!iommu_for_unity_map(iommu, entry))
1649 continue;
1650 ret = dma_ops_unity_map(iommu->default_dom, entry);
1651 if (ret)
1652 return ret;
1653 }
1654
1655 return 0;
1656}
1657
1658/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001659 * Inits the unity mappings required for a specific device
1660 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001661static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1662 u16 devid)
1663{
1664 struct unity_map_entry *e;
1665 int ret;
1666
1667 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1668 if (!(devid >= e->devid_start && devid <= e->devid_end))
1669 continue;
1670 ret = dma_ops_unity_map(dma_dom, e);
1671 if (ret)
1672 return ret;
1673 }
1674
1675 return 0;
1676}
1677
Joerg Roedel431b2a22008-07-11 17:14:22 +02001678/****************************************************************************
1679 *
1680 * The next functions belong to the address allocator for the dma_ops
1681 * interface functions. They work like the allocators in the other IOMMU
1682 * drivers. Its basically a bitmap which marks the allocated pages in
1683 * the aperture. Maybe it could be enhanced in the future to a more
1684 * efficient allocator.
1685 *
1686 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001687
Joerg Roedel431b2a22008-07-11 17:14:22 +02001688/*
Joerg Roedel384de722009-05-15 12:30:05 +02001689 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001690 *
1691 * called with domain->lock held
1692 */
Joerg Roedel384de722009-05-15 12:30:05 +02001693
Joerg Roedel9cabe892009-05-18 16:38:55 +02001694/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001695 * Used to reserve address ranges in the aperture (e.g. for exclusion
1696 * ranges.
1697 */
1698static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1699 unsigned long start_page,
1700 unsigned int pages)
1701{
1702 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1703
1704 if (start_page + pages > last_page)
1705 pages = last_page - start_page;
1706
1707 for (i = start_page; i < start_page + pages; ++i) {
1708 int index = i / APERTURE_RANGE_PAGES;
1709 int page = i % APERTURE_RANGE_PAGES;
1710 __set_bit(page, dom->aperture[index]->bitmap);
1711 }
1712}
1713
1714/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001715 * This function is used to add a new aperture range to an existing
1716 * aperture in case of dma_ops domain allocation or address allocation
1717 * failure.
1718 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001719static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001720 bool populate, gfp_t gfp)
1721{
1722 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001723 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001724 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001725
Joerg Roedelf5e97052009-05-22 12:31:53 +02001726#ifdef CONFIG_IOMMU_STRESS
1727 populate = false;
1728#endif
1729
Joerg Roedel9cabe892009-05-18 16:38:55 +02001730 if (index >= APERTURE_MAX_RANGES)
1731 return -ENOMEM;
1732
1733 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1734 if (!dma_dom->aperture[index])
1735 return -ENOMEM;
1736
1737 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1738 if (!dma_dom->aperture[index]->bitmap)
1739 goto out_free;
1740
1741 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1742
1743 if (populate) {
1744 unsigned long address = dma_dom->aperture_size;
1745 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1746 u64 *pte, *pte_page;
1747
1748 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001749 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001750 &pte_page, gfp);
1751 if (!pte)
1752 goto out_free;
1753
1754 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1755
1756 address += APERTURE_RANGE_SIZE / 64;
1757 }
1758 }
1759
Joerg Roedel17f5b562011-07-06 17:14:44 +02001760 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001761 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1762
Joerg Roedel17f5b562011-07-06 17:14:44 +02001763 /* Reserve address range used for MSI messages */
1764 if (old_size < MSI_ADDR_BASE_LO &&
1765 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1766 unsigned long spage;
1767 int pages;
1768
1769 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1770 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1771
1772 dma_ops_reserve_addresses(dma_dom, spage, pages);
1773 }
1774
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001775 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001776 for_each_iommu(iommu) {
1777 if (iommu->exclusion_start &&
1778 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1779 && iommu->exclusion_start < dma_dom->aperture_size) {
1780 unsigned long startpage;
1781 int pages = iommu_num_pages(iommu->exclusion_start,
1782 iommu->exclusion_length,
1783 PAGE_SIZE);
1784 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1785 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1786 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001787 }
1788
1789 /*
1790 * Check for areas already mapped as present in the new aperture
1791 * range and mark those pages as reserved in the allocator. Such
1792 * mappings may already exist as a result of requested unity
1793 * mappings for devices.
1794 */
1795 for (i = dma_dom->aperture[index]->offset;
1796 i < dma_dom->aperture_size;
1797 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001798 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001799 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1800 continue;
1801
Joerg Roedelfcd08612011-10-11 17:41:32 +02001802 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001803 }
1804
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001805 update_domain(&dma_dom->domain);
1806
Joerg Roedel9cabe892009-05-18 16:38:55 +02001807 return 0;
1808
1809out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001810 update_domain(&dma_dom->domain);
1811
Joerg Roedel9cabe892009-05-18 16:38:55 +02001812 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1813
1814 kfree(dma_dom->aperture[index]);
1815 dma_dom->aperture[index] = NULL;
1816
1817 return -ENOMEM;
1818}
1819
Joerg Roedel384de722009-05-15 12:30:05 +02001820static unsigned long dma_ops_area_alloc(struct device *dev,
1821 struct dma_ops_domain *dom,
1822 unsigned int pages,
1823 unsigned long align_mask,
1824 u64 dma_mask,
1825 unsigned long start)
1826{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001827 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001828 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1829 int i = start >> APERTURE_RANGE_SHIFT;
1830 unsigned long boundary_size;
1831 unsigned long address = -1;
1832 unsigned long limit;
1833
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001834 next_bit >>= PAGE_SHIFT;
1835
Joerg Roedel384de722009-05-15 12:30:05 +02001836 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1837 PAGE_SIZE) >> PAGE_SHIFT;
1838
1839 for (;i < max_index; ++i) {
1840 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1841
1842 if (dom->aperture[i]->offset >= dma_mask)
1843 break;
1844
1845 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1846 dma_mask >> PAGE_SHIFT);
1847
1848 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1849 limit, next_bit, pages, 0,
1850 boundary_size, align_mask);
1851 if (address != -1) {
1852 address = dom->aperture[i]->offset +
1853 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001854 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001855 break;
1856 }
1857
1858 next_bit = 0;
1859 }
1860
1861 return address;
1862}
1863
Joerg Roedeld3086442008-06-26 21:27:57 +02001864static unsigned long dma_ops_alloc_addresses(struct device *dev,
1865 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001866 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001867 unsigned long align_mask,
1868 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001869{
Joerg Roedeld3086442008-06-26 21:27:57 +02001870 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001871
Joerg Roedelfe16f082009-05-22 12:27:53 +02001872#ifdef CONFIG_IOMMU_STRESS
1873 dom->next_address = 0;
1874 dom->need_flush = true;
1875#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001876
Joerg Roedel384de722009-05-15 12:30:05 +02001877 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001878 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001879
Joerg Roedel1c655772008-09-04 18:40:05 +02001880 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001881 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001882 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1883 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001884 dom->need_flush = true;
1885 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001886
Joerg Roedel384de722009-05-15 12:30:05 +02001887 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001888 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001889
1890 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1891
1892 return address;
1893}
1894
Joerg Roedel431b2a22008-07-11 17:14:22 +02001895/*
1896 * The address free function.
1897 *
1898 * called with domain->lock held
1899 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001900static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1901 unsigned long address,
1902 unsigned int pages)
1903{
Joerg Roedel384de722009-05-15 12:30:05 +02001904 unsigned i = address >> APERTURE_RANGE_SHIFT;
1905 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001906
Joerg Roedel384de722009-05-15 12:30:05 +02001907 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1908
Joerg Roedel47bccd62009-05-22 12:40:54 +02001909#ifdef CONFIG_IOMMU_STRESS
1910 if (i < 4)
1911 return;
1912#endif
1913
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001914 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001915 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001916
1917 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001918
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001919 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001920
Joerg Roedeld3086442008-06-26 21:27:57 +02001921}
1922
Joerg Roedel431b2a22008-07-11 17:14:22 +02001923/****************************************************************************
1924 *
1925 * The next functions belong to the domain allocation. A domain is
1926 * allocated for every IOMMU as the default domain. If device isolation
1927 * is enabled, every device get its own domain. The most important thing
1928 * about domains is the page table mapping the DMA address space they
1929 * contain.
1930 *
1931 ****************************************************************************/
1932
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001933/*
1934 * This function adds a protection domain to the global protection domain list
1935 */
1936static void add_domain_to_list(struct protection_domain *domain)
1937{
1938 unsigned long flags;
1939
1940 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1941 list_add(&domain->list, &amd_iommu_pd_list);
1942 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1943}
1944
1945/*
1946 * This function removes a protection domain to the global
1947 * protection domain list
1948 */
1949static void del_domain_from_list(struct protection_domain *domain)
1950{
1951 unsigned long flags;
1952
1953 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1954 list_del(&domain->list);
1955 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1956}
1957
Joerg Roedelec487d12008-06-26 21:27:58 +02001958static u16 domain_id_alloc(void)
1959{
1960 unsigned long flags;
1961 int id;
1962
1963 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1964 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1965 BUG_ON(id == 0);
1966 if (id > 0 && id < MAX_DOMAIN_ID)
1967 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1968 else
1969 id = 0;
1970 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1971
1972 return id;
1973}
1974
Joerg Roedela2acfb72008-12-02 18:28:53 +01001975static void domain_id_free(int id)
1976{
1977 unsigned long flags;
1978
1979 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1980 if (id > 0 && id < MAX_DOMAIN_ID)
1981 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1982 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1983}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001984
Joerg Roedel5c34c402013-06-20 20:22:58 +02001985#define DEFINE_FREE_PT_FN(LVL, FN) \
1986static void free_pt_##LVL (unsigned long __pt) \
1987{ \
1988 unsigned long p; \
1989 u64 *pt; \
1990 int i; \
1991 \
1992 pt = (u64 *)__pt; \
1993 \
1994 for (i = 0; i < 512; ++i) { \
1995 if (!IOMMU_PTE_PRESENT(pt[i])) \
1996 continue; \
1997 \
1998 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1999 FN(p); \
2000 } \
2001 free_page((unsigned long)pt); \
2002}
2003
2004DEFINE_FREE_PT_FN(l2, free_page)
2005DEFINE_FREE_PT_FN(l3, free_pt_l2)
2006DEFINE_FREE_PT_FN(l4, free_pt_l3)
2007DEFINE_FREE_PT_FN(l5, free_pt_l4)
2008DEFINE_FREE_PT_FN(l6, free_pt_l5)
2009
Joerg Roedel86db2e52008-12-02 18:20:21 +01002010static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02002011{
Joerg Roedel5c34c402013-06-20 20:22:58 +02002012 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02002013
Joerg Roedel5c34c402013-06-20 20:22:58 +02002014 switch (domain->mode) {
2015 case PAGE_MODE_NONE:
2016 break;
2017 case PAGE_MODE_1_LEVEL:
2018 free_page(root);
2019 break;
2020 case PAGE_MODE_2_LEVEL:
2021 free_pt_l2(root);
2022 break;
2023 case PAGE_MODE_3_LEVEL:
2024 free_pt_l3(root);
2025 break;
2026 case PAGE_MODE_4_LEVEL:
2027 free_pt_l4(root);
2028 break;
2029 case PAGE_MODE_5_LEVEL:
2030 free_pt_l5(root);
2031 break;
2032 case PAGE_MODE_6_LEVEL:
2033 free_pt_l6(root);
2034 break;
2035 default:
2036 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02002037 }
Joerg Roedelec487d12008-06-26 21:27:58 +02002038}
2039
Joerg Roedelb16137b2011-11-21 16:50:23 +01002040static void free_gcr3_tbl_level1(u64 *tbl)
2041{
2042 u64 *ptr;
2043 int i;
2044
2045 for (i = 0; i < 512; ++i) {
2046 if (!(tbl[i] & GCR3_VALID))
2047 continue;
2048
2049 ptr = __va(tbl[i] & PAGE_MASK);
2050
2051 free_page((unsigned long)ptr);
2052 }
2053}
2054
2055static void free_gcr3_tbl_level2(u64 *tbl)
2056{
2057 u64 *ptr;
2058 int i;
2059
2060 for (i = 0; i < 512; ++i) {
2061 if (!(tbl[i] & GCR3_VALID))
2062 continue;
2063
2064 ptr = __va(tbl[i] & PAGE_MASK);
2065
2066 free_gcr3_tbl_level1(ptr);
2067 }
2068}
2069
Joerg Roedel52815b72011-11-17 17:24:28 +01002070static void free_gcr3_table(struct protection_domain *domain)
2071{
Joerg Roedelb16137b2011-11-21 16:50:23 +01002072 if (domain->glx == 2)
2073 free_gcr3_tbl_level2(domain->gcr3_tbl);
2074 else if (domain->glx == 1)
2075 free_gcr3_tbl_level1(domain->gcr3_tbl);
2076 else if (domain->glx != 0)
2077 BUG();
2078
Joerg Roedel52815b72011-11-17 17:24:28 +01002079 free_page((unsigned long)domain->gcr3_tbl);
2080}
2081
Joerg Roedel431b2a22008-07-11 17:14:22 +02002082/*
2083 * Free a domain, only used if something went wrong in the
2084 * allocation path and we need to free an already allocated page table
2085 */
Joerg Roedelec487d12008-06-26 21:27:58 +02002086static void dma_ops_domain_free(struct dma_ops_domain *dom)
2087{
Joerg Roedel384de722009-05-15 12:30:05 +02002088 int i;
2089
Joerg Roedelec487d12008-06-26 21:27:58 +02002090 if (!dom)
2091 return;
2092
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002093 del_domain_from_list(&dom->domain);
2094
Joerg Roedel86db2e52008-12-02 18:20:21 +01002095 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02002096
Joerg Roedel384de722009-05-15 12:30:05 +02002097 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
2098 if (!dom->aperture[i])
2099 continue;
2100 free_page((unsigned long)dom->aperture[i]->bitmap);
2101 kfree(dom->aperture[i]);
2102 }
Joerg Roedelec487d12008-06-26 21:27:58 +02002103
2104 kfree(dom);
2105}
2106
Joerg Roedel431b2a22008-07-11 17:14:22 +02002107/*
2108 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002109 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02002110 * structures required for the dma_ops interface
2111 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01002112static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02002113{
2114 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002115
2116 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
2117 if (!dma_dom)
2118 return NULL;
2119
2120 spin_lock_init(&dma_dom->domain.lock);
2121
2122 dma_dom->domain.id = domain_id_alloc();
2123 if (dma_dom->domain.id == 0)
2124 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002125 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002126 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002127 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002128 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002129 dma_dom->domain.priv = dma_dom;
2130 if (!dma_dom->domain.pt_root)
2131 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002132
Joerg Roedel1c655772008-09-04 18:40:05 +02002133 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002134 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002135
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002136 add_domain_to_list(&dma_dom->domain);
2137
Joerg Roedel576175c2009-11-23 19:08:46 +01002138 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002139 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002140
Joerg Roedel431b2a22008-07-11 17:14:22 +02002141 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002142 * mark the first page as allocated so we never return 0 as
2143 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002144 */
Joerg Roedel384de722009-05-15 12:30:05 +02002145 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02002146 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002147
Joerg Roedelec487d12008-06-26 21:27:58 +02002148
2149 return dma_dom;
2150
2151free_dma_dom:
2152 dma_ops_domain_free(dma_dom);
2153
2154 return NULL;
2155}
2156
Joerg Roedel431b2a22008-07-11 17:14:22 +02002157/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002158 * little helper function to check whether a given protection domain is a
2159 * dma_ops domain
2160 */
2161static bool dma_ops_domain(struct protection_domain *domain)
2162{
2163 return domain->flags & PD_DMA_OPS_MASK;
2164}
2165
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002166static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002167{
Joerg Roedel132bd682011-11-17 14:18:46 +01002168 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002169 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002170
Joerg Roedel132bd682011-11-17 14:18:46 +01002171 if (domain->mode != PAGE_MODE_NONE)
2172 pte_root = virt_to_phys(domain->pt_root);
2173
Joerg Roedel38ddf412008-09-11 10:38:32 +02002174 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2175 << DEV_ENTRY_MODE_SHIFT;
2176 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002177
Joerg Roedelee6c2862011-11-09 12:06:03 +01002178 flags = amd_iommu_dev_table[devid].data[1];
2179
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002180 if (ats)
2181 flags |= DTE_FLAG_IOTLB;
2182
Joerg Roedel52815b72011-11-17 17:24:28 +01002183 if (domain->flags & PD_IOMMUV2_MASK) {
2184 u64 gcr3 = __pa(domain->gcr3_tbl);
2185 u64 glx = domain->glx;
2186 u64 tmp;
2187
2188 pte_root |= DTE_FLAG_GV;
2189 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2190
2191 /* First mask out possible old values for GCR3 table */
2192 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2193 flags &= ~tmp;
2194
2195 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2196 flags &= ~tmp;
2197
2198 /* Encode GCR3 table into DTE */
2199 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2200 pte_root |= tmp;
2201
2202 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2203 flags |= tmp;
2204
2205 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2206 flags |= tmp;
2207 }
2208
Joerg Roedelee6c2862011-11-09 12:06:03 +01002209 flags &= ~(0xffffUL);
2210 flags |= domain->id;
2211
2212 amd_iommu_dev_table[devid].data[1] = flags;
2213 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002214}
2215
Joerg Roedel15898bb2009-11-24 15:39:42 +01002216static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002217{
Joerg Roedel355bf552008-12-08 12:02:41 +01002218 /* remove entry from the device table seen by the hardware */
2219 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2220 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002221
Joerg Roedelc5cca142009-10-09 18:31:20 +02002222 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002223}
2224
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002225static void do_attach(struct iommu_dev_data *dev_data,
2226 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002227{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002228 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002229 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002230
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002231 iommu = amd_iommu_rlookup_table[dev_data->devid];
2232 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002233
2234 /* Update data structures */
2235 dev_data->domain = domain;
2236 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002237 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002238
2239 /* Do reference counting */
2240 domain->dev_iommu[iommu->index] += 1;
2241 domain->dev_cnt += 1;
2242
2243 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002244 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002245}
2246
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002247static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002248{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002249 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002250
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002251 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002252
Joerg Roedelc4596112009-11-20 14:57:32 +01002253 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002254 dev_data->domain->dev_iommu[iommu->index] -= 1;
2255 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002256
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002257 /* Update data structures */
2258 dev_data->domain = NULL;
2259 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002260 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002261
2262 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002263 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002264}
2265
2266/*
2267 * If a device is not yet associated with a domain, this function does
2268 * assigns it visible for the hardware
2269 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002270static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002271 struct protection_domain *domain)
2272{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002273 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002274
Joerg Roedel15898bb2009-11-24 15:39:42 +01002275 /* lock domain */
2276 spin_lock(&domain->lock);
2277
Joerg Roedel71f77582011-06-09 19:03:15 +02002278 if (dev_data->alias_data != NULL) {
2279 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002280
Joerg Roedel2b02b092011-06-09 17:48:39 +02002281 /* Some sanity checks */
2282 ret = -EBUSY;
2283 if (alias_data->domain != NULL &&
2284 alias_data->domain != domain)
2285 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002286
Joerg Roedel2b02b092011-06-09 17:48:39 +02002287 if (dev_data->domain != NULL &&
2288 dev_data->domain != domain)
2289 goto out_unlock;
2290
2291 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002292 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002293 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002294
2295 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002296 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002297
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002298 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002299 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002300
Joerg Roedel24100052009-11-25 15:59:57 +01002301 atomic_inc(&dev_data->bind);
2302
Julia Lawall84fe6c12010-05-27 12:31:51 +02002303 ret = 0;
2304
2305out_unlock:
2306
Joerg Roedel355bf552008-12-08 12:02:41 +01002307 /* ready */
2308 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002309
Julia Lawall84fe6c12010-05-27 12:31:51 +02002310 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002311}
2312
Joerg Roedel52815b72011-11-17 17:24:28 +01002313
2314static void pdev_iommuv2_disable(struct pci_dev *pdev)
2315{
2316 pci_disable_ats(pdev);
2317 pci_disable_pri(pdev);
2318 pci_disable_pasid(pdev);
2319}
2320
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002321/* FIXME: Change generic reset-function to do the same */
2322static int pri_reset_while_enabled(struct pci_dev *pdev)
2323{
2324 u16 control;
2325 int pos;
2326
Joerg Roedel46277b72011-12-07 14:34:02 +01002327 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002328 if (!pos)
2329 return -EINVAL;
2330
Joerg Roedel46277b72011-12-07 14:34:02 +01002331 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2332 control |= PCI_PRI_CTRL_RESET;
2333 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002334
2335 return 0;
2336}
2337
Joerg Roedel52815b72011-11-17 17:24:28 +01002338static int pdev_iommuv2_enable(struct pci_dev *pdev)
2339{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002340 bool reset_enable;
2341 int reqs, ret;
2342
2343 /* FIXME: Hardcode number of outstanding requests for now */
2344 reqs = 32;
2345 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2346 reqs = 1;
2347 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002348
2349 /* Only allow access to user-accessible pages */
2350 ret = pci_enable_pasid(pdev, 0);
2351 if (ret)
2352 goto out_err;
2353
2354 /* First reset the PRI state of the device */
2355 ret = pci_reset_pri(pdev);
2356 if (ret)
2357 goto out_err;
2358
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002359 /* Enable PRI */
2360 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002361 if (ret)
2362 goto out_err;
2363
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002364 if (reset_enable) {
2365 ret = pri_reset_while_enabled(pdev);
2366 if (ret)
2367 goto out_err;
2368 }
2369
Joerg Roedel52815b72011-11-17 17:24:28 +01002370 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2371 if (ret)
2372 goto out_err;
2373
2374 return 0;
2375
2376out_err:
2377 pci_disable_pri(pdev);
2378 pci_disable_pasid(pdev);
2379
2380 return ret;
2381}
2382
Joerg Roedelc99afa22011-11-21 18:19:25 +01002383/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002384#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002385
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002386static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002387{
Joerg Roedela3b93122012-04-12 12:49:26 +02002388 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002389 int pos;
2390
Joerg Roedel46277b72011-12-07 14:34:02 +01002391 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002392 if (!pos)
2393 return false;
2394
Joerg Roedela3b93122012-04-12 12:49:26 +02002395 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002396
Joerg Roedela3b93122012-04-12 12:49:26 +02002397 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002398}
2399
Joerg Roedel15898bb2009-11-24 15:39:42 +01002400/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002401 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002402 * assigns it visible for the hardware
2403 */
2404static int attach_device(struct device *dev,
2405 struct protection_domain *domain)
2406{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002407 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002408 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002409 unsigned long flags;
2410 int ret;
2411
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002412 dev_data = get_dev_data(dev);
2413
Joerg Roedel52815b72011-11-17 17:24:28 +01002414 if (domain->flags & PD_IOMMUV2_MASK) {
2415 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2416 return -EINVAL;
2417
2418 if (pdev_iommuv2_enable(pdev) != 0)
2419 return -EINVAL;
2420
2421 dev_data->ats.enabled = true;
2422 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002423 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002424 } else if (amd_iommu_iotlb_sup &&
2425 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002426 dev_data->ats.enabled = true;
2427 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2428 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002429
Joerg Roedel15898bb2009-11-24 15:39:42 +01002430 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002431 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002432 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2433
2434 /*
2435 * We might boot into a crash-kernel here. The crashed kernel
2436 * left the caches in the IOMMU dirty. So we have to flush
2437 * here to evict all dirty stuff.
2438 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002439 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002440
2441 return ret;
2442}
2443
2444/*
2445 * Removes a device from a protection domain (unlocked)
2446 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002447static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002448{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002449 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002450 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002451
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002452 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002453
Joerg Roedel2ca76272010-01-22 16:45:31 +01002454 domain = dev_data->domain;
2455
2456 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002457
Joerg Roedel71f77582011-06-09 19:03:15 +02002458 if (dev_data->alias_data != NULL) {
2459 struct iommu_dev_data *alias_data = dev_data->alias_data;
2460
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002461 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002462 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002463 }
2464
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002465 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002466 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002467
Joerg Roedel2ca76272010-01-22 16:45:31 +01002468 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002469
Joerg Roedel21129f72009-09-01 11:59:42 +02002470 /*
2471 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002472 * passthrough domain if it is detached from any other domain.
2473 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002474 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002475 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002476 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002477 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002478}
2479
2480/*
2481 * Removes a device from a protection domain (with devtable_lock held)
2482 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002483static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002484{
Joerg Roedel52815b72011-11-17 17:24:28 +01002485 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002486 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002487 unsigned long flags;
2488
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002489 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002490 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002491
Joerg Roedel355bf552008-12-08 12:02:41 +01002492 /* lock device table */
2493 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002494 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002495 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002496
Joerg Roedel52815b72011-11-17 17:24:28 +01002497 if (domain->flags & PD_IOMMUV2_MASK)
2498 pdev_iommuv2_disable(to_pci_dev(dev));
2499 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002500 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002501
2502 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002503}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002504
Joerg Roedel15898bb2009-11-24 15:39:42 +01002505/*
2506 * Find out the protection domain structure for a given PCI device. This
2507 * will give us the pointer to the page table root for example.
2508 */
2509static struct protection_domain *domain_for_device(struct device *dev)
2510{
Joerg Roedel71f77582011-06-09 19:03:15 +02002511 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002512 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002513 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002514
Joerg Roedel657cbb62009-11-23 15:26:46 +01002515 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002516
Joerg Roedel2b02b092011-06-09 17:48:39 +02002517 if (dev_data->domain)
2518 return dev_data->domain;
2519
Joerg Roedel71f77582011-06-09 19:03:15 +02002520 if (dev_data->alias_data != NULL) {
2521 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002522
2523 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2524 if (alias_data->domain != NULL) {
2525 __attach_device(dev_data, alias_data->domain);
2526 dom = alias_data->domain;
2527 }
2528 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002529 }
2530
Joerg Roedel15898bb2009-11-24 15:39:42 +01002531 return dom;
2532}
2533
Joerg Roedele275a2a2008-12-10 18:27:25 +01002534static int device_change_notifier(struct notifier_block *nb,
2535 unsigned long action, void *data)
2536{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002537 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002538 struct protection_domain *domain;
2539 struct iommu_dev_data *dev_data;
2540 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002541 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002542 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002543 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002544
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002545 if (!check_device(dev))
2546 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002547
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002548 devid = get_device_id(dev);
2549 iommu = amd_iommu_rlookup_table[devid];
2550 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002551
2552 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002553 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002554
2555 domain = domain_for_device(dev);
2556
Joerg Roedele275a2a2008-12-10 18:27:25 +01002557 if (!domain)
2558 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002559 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002560 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002561 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002562 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002563 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002564
2565 iommu_init_device(dev);
2566
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002567 /*
2568 * dev_data is still NULL and
2569 * got initialized in iommu_init_device
2570 */
2571 dev_data = get_dev_data(dev);
2572
2573 if (iommu_pass_through || dev_data->iommu_v2) {
2574 dev_data->passthrough = true;
2575 attach_device(dev, pt_domain);
2576 break;
2577 }
2578
Joerg Roedel657cbb62009-11-23 15:26:46 +01002579 domain = domain_for_device(dev);
2580
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002581 /* allocate a protection domain if a device is added */
2582 dma_domain = find_protection_domain(devid);
Joerg Roedelc2a28762013-03-26 22:48:23 +01002583 if (!dma_domain) {
2584 dma_domain = dma_ops_domain_alloc();
2585 if (!dma_domain)
2586 goto out;
2587 dma_domain->target_dev = devid;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002588
Joerg Roedelc2a28762013-03-26 22:48:23 +01002589 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2590 list_add_tail(&dma_domain->list, &iommu_pd_list);
2591 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2592 }
Joerg Roedelac1534a2012-06-21 14:52:40 +02002593
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002594 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002595
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002596 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002597 case BUS_NOTIFY_DEL_DEVICE:
2598
2599 iommu_uninit_device(dev);
2600
Joerg Roedele275a2a2008-12-10 18:27:25 +01002601 default:
2602 goto out;
2603 }
2604
Joerg Roedele275a2a2008-12-10 18:27:25 +01002605 iommu_completion_wait(iommu);
2606
2607out:
2608 return 0;
2609}
2610
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302611static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002612 .notifier_call = device_change_notifier,
2613};
Joerg Roedel355bf552008-12-08 12:02:41 +01002614
Joerg Roedel8638c492009-12-10 11:12:25 +01002615void amd_iommu_init_notifier(void)
2616{
2617 bus_register_notifier(&pci_bus_type, &device_nb);
2618}
2619
Joerg Roedel431b2a22008-07-11 17:14:22 +02002620/*****************************************************************************
2621 *
2622 * The next functions belong to the dma_ops mapping/unmapping code.
2623 *
2624 *****************************************************************************/
2625
2626/*
2627 * In the dma_ops path we only have the struct device. This function
2628 * finds the corresponding IOMMU, the protection domain and the
2629 * requestor id for a given device.
2630 * If the device is not yet associated with a domain this is also done
2631 * in this function.
2632 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002633static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002634{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002635 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002636 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002637 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002638
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002639 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002640 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002641
Joerg Roedel94f6d192009-11-24 16:40:02 +01002642 domain = domain_for_device(dev);
2643 if (domain != NULL && !dma_ops_domain(domain))
2644 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002645
Joerg Roedel94f6d192009-11-24 16:40:02 +01002646 if (domain != NULL)
2647 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002648
Frank Arnolddf805ab2012-08-27 19:21:04 +02002649 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002650 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002651 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002652 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2653 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002654 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002655 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002656
Joerg Roedel94f6d192009-11-24 16:40:02 +01002657 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002658}
2659
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002660static void update_device_table(struct protection_domain *domain)
2661{
Joerg Roedel492667d2009-11-27 13:25:47 +01002662 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002663
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002664 list_for_each_entry(dev_data, &domain->dev_list, list)
2665 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002666}
2667
2668static void update_domain(struct protection_domain *domain)
2669{
2670 if (!domain->updated)
2671 return;
2672
2673 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002674
2675 domain_flush_devices(domain);
2676 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002677
2678 domain->updated = false;
2679}
2680
Joerg Roedel431b2a22008-07-11 17:14:22 +02002681/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002682 * This function fetches the PTE for a given address in the aperture
2683 */
2684static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2685 unsigned long address)
2686{
Joerg Roedel384de722009-05-15 12:30:05 +02002687 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002688 u64 *pte, *pte_page;
2689
Joerg Roedel384de722009-05-15 12:30:05 +02002690 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2691 if (!aperture)
2692 return NULL;
2693
2694 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002695 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002696 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002697 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002698 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2699 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002700 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002701
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002702 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002703
2704 return pte;
2705}
2706
2707/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002708 * This is the generic map function. It maps one 4kb page at paddr to
2709 * the given address in the DMA address space for the domain.
2710 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002711static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002712 unsigned long address,
2713 phys_addr_t paddr,
2714 int direction)
2715{
2716 u64 *pte, __pte;
2717
2718 WARN_ON(address > dom->aperture_size);
2719
2720 paddr &= PAGE_MASK;
2721
Joerg Roedel8bda3092009-05-12 12:02:46 +02002722 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002723 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002724 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002725
2726 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2727
2728 if (direction == DMA_TO_DEVICE)
2729 __pte |= IOMMU_PTE_IR;
2730 else if (direction == DMA_FROM_DEVICE)
2731 __pte |= IOMMU_PTE_IW;
2732 else if (direction == DMA_BIDIRECTIONAL)
2733 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2734
2735 WARN_ON(*pte);
2736
2737 *pte = __pte;
2738
2739 return (dma_addr_t)address;
2740}
2741
Joerg Roedel431b2a22008-07-11 17:14:22 +02002742/*
2743 * The generic unmapping function for on page in the DMA address space.
2744 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002745static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002746 unsigned long address)
2747{
Joerg Roedel384de722009-05-15 12:30:05 +02002748 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002749 u64 *pte;
2750
2751 if (address >= dom->aperture_size)
2752 return;
2753
Joerg Roedel384de722009-05-15 12:30:05 +02002754 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2755 if (!aperture)
2756 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002757
Joerg Roedel384de722009-05-15 12:30:05 +02002758 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2759 if (!pte)
2760 return;
2761
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002762 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002763
2764 WARN_ON(!*pte);
2765
2766 *pte = 0ULL;
2767}
2768
Joerg Roedel431b2a22008-07-11 17:14:22 +02002769/*
2770 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002771 * contiguous memory region into DMA address space. It is used by all
2772 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002773 * Must be called with the domain lock held.
2774 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002775static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002776 struct dma_ops_domain *dma_dom,
2777 phys_addr_t paddr,
2778 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002779 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002780 bool align,
2781 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002782{
2783 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002784 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002785 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002786 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002787 int i;
2788
Joerg Roedele3c449f2008-10-15 22:02:11 -07002789 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002790 paddr &= PAGE_MASK;
2791
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002792 INC_STATS_COUNTER(total_map_requests);
2793
Joerg Roedelc1858972008-12-12 15:42:39 +01002794 if (pages > 1)
2795 INC_STATS_COUNTER(cross_page);
2796
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002797 if (align)
2798 align_mask = (1UL << get_order(size)) - 1;
2799
Joerg Roedel11b83882009-05-19 10:23:15 +02002800retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002801 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2802 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002803 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002804 /*
2805 * setting next_address here will let the address
2806 * allocator only scan the new allocated range in the
2807 * first run. This is a small optimization.
2808 */
2809 dma_dom->next_address = dma_dom->aperture_size;
2810
Joerg Roedel576175c2009-11-23 19:08:46 +01002811 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002812 goto out;
2813
2814 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002815 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002816 * allocation again
2817 */
2818 goto retry;
2819 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002820
2821 start = address;
2822 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002823 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002824 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002825 goto out_unmap;
2826
Joerg Roedelcb76c322008-06-26 21:28:00 +02002827 paddr += PAGE_SIZE;
2828 start += PAGE_SIZE;
2829 }
2830 address += offset;
2831
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002832 ADD_STATS_COUNTER(alloced_io_mem, size);
2833
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002834 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002835 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002836 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002837 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002838 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002839
Joerg Roedelcb76c322008-06-26 21:28:00 +02002840out:
2841 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002842
2843out_unmap:
2844
2845 for (--i; i >= 0; --i) {
2846 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002847 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002848 }
2849
2850 dma_ops_free_addresses(dma_dom, address, pages);
2851
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002852 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002853}
2854
Joerg Roedel431b2a22008-07-11 17:14:22 +02002855/*
2856 * Does the reverse of the __map_single function. Must be called with
2857 * the domain lock held too
2858 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002859static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002860 dma_addr_t dma_addr,
2861 size_t size,
2862 int dir)
2863{
Joerg Roedel04e04632010-09-23 16:12:48 +02002864 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002865 dma_addr_t i, start;
2866 unsigned int pages;
2867
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002868 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002869 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002870 return;
2871
Joerg Roedel04e04632010-09-23 16:12:48 +02002872 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002873 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002874 dma_addr &= PAGE_MASK;
2875 start = dma_addr;
2876
2877 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002878 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002879 start += PAGE_SIZE;
2880 }
2881
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002882 SUB_STATS_COUNTER(alloced_io_mem, size);
2883
Joerg Roedelcb76c322008-06-26 21:28:00 +02002884 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002885
Joerg Roedel80be3082008-11-06 14:59:05 +01002886 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002887 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002888 dma_dom->need_flush = false;
2889 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002890}
2891
Joerg Roedel431b2a22008-07-11 17:14:22 +02002892/*
2893 * The exported map_single function for dma_ops.
2894 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002895static dma_addr_t map_page(struct device *dev, struct page *page,
2896 unsigned long offset, size_t size,
2897 enum dma_data_direction dir,
2898 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002899{
2900 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002901 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002902 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002903 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002904 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002905
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002906 INC_STATS_COUNTER(cnt_map_single);
2907
Joerg Roedel94f6d192009-11-24 16:40:02 +01002908 domain = get_domain(dev);
2909 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002910 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002911 else if (IS_ERR(domain))
2912 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002913
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002914 dma_mask = *dev->dma_mask;
2915
Joerg Roedel4da70b92008-06-26 21:28:01 +02002916 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002917
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002918 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002919 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002920 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002921 goto out;
2922
Joerg Roedel17b124b2011-04-06 18:01:35 +02002923 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002924
2925out:
2926 spin_unlock_irqrestore(&domain->lock, flags);
2927
2928 return addr;
2929}
2930
Joerg Roedel431b2a22008-07-11 17:14:22 +02002931/*
2932 * The exported unmap_single function for dma_ops.
2933 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002934static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2935 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002936{
2937 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002938 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002939
Joerg Roedel146a6912008-12-12 15:07:12 +01002940 INC_STATS_COUNTER(cnt_unmap_single);
2941
Joerg Roedel94f6d192009-11-24 16:40:02 +01002942 domain = get_domain(dev);
2943 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002944 return;
2945
Joerg Roedel4da70b92008-06-26 21:28:01 +02002946 spin_lock_irqsave(&domain->lock, flags);
2947
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002948 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002949
Joerg Roedel17b124b2011-04-06 18:01:35 +02002950 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002951
2952 spin_unlock_irqrestore(&domain->lock, flags);
2953}
2954
Joerg Roedel431b2a22008-07-11 17:14:22 +02002955/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002956 * The exported map_sg function for dma_ops (handles scatter-gather
2957 * lists).
2958 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002959static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002960 int nelems, enum dma_data_direction dir,
2961 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002962{
2963 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002964 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002965 int i;
2966 struct scatterlist *s;
2967 phys_addr_t paddr;
2968 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002969 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002970
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002971 INC_STATS_COUNTER(cnt_map_sg);
2972
Joerg Roedel94f6d192009-11-24 16:40:02 +01002973 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002974 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002975 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002976
Joerg Roedel832a90c2008-09-18 15:54:23 +02002977 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002978
Joerg Roedel65b050a2008-06-26 21:28:02 +02002979 spin_lock_irqsave(&domain->lock, flags);
2980
2981 for_each_sg(sglist, s, nelems, i) {
2982 paddr = sg_phys(s);
2983
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002984 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002985 paddr, s->length, dir, false,
2986 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002987
2988 if (s->dma_address) {
2989 s->dma_length = s->length;
2990 mapped_elems++;
2991 } else
2992 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002993 }
2994
Joerg Roedel17b124b2011-04-06 18:01:35 +02002995 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002996
2997out:
2998 spin_unlock_irqrestore(&domain->lock, flags);
2999
3000 return mapped_elems;
3001unmap:
3002 for_each_sg(sglist, s, mapped_elems, i) {
3003 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003004 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02003005 s->dma_length, dir);
3006 s->dma_address = s->dma_length = 0;
3007 }
3008
3009 mapped_elems = 0;
3010
3011 goto out;
3012}
3013
Joerg Roedel431b2a22008-07-11 17:14:22 +02003014/*
3015 * The exported map_sg function for dma_ops (handles scatter-gather
3016 * lists).
3017 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02003018static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003019 int nelems, enum dma_data_direction dir,
3020 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02003021{
3022 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02003023 struct protection_domain *domain;
3024 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02003025 int i;
3026
Joerg Roedel55877a62008-12-12 15:12:14 +01003027 INC_STATS_COUNTER(cnt_unmap_sg);
3028
Joerg Roedel94f6d192009-11-24 16:40:02 +01003029 domain = get_domain(dev);
3030 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01003031 return;
3032
Joerg Roedel65b050a2008-06-26 21:28:02 +02003033 spin_lock_irqsave(&domain->lock, flags);
3034
3035 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003036 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02003037 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02003038 s->dma_address = s->dma_length = 0;
3039 }
3040
Joerg Roedel17b124b2011-04-06 18:01:35 +02003041 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02003042
3043 spin_unlock_irqrestore(&domain->lock, flags);
3044}
3045
Joerg Roedel431b2a22008-07-11 17:14:22 +02003046/*
3047 * The exported alloc_coherent function for dma_ops.
3048 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003049static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003050 dma_addr_t *dma_addr, gfp_t flag,
3051 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003052{
3053 unsigned long flags;
3054 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003055 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003056 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02003057 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003058
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01003059 INC_STATS_COUNTER(cnt_alloc_coherent);
3060
Joerg Roedel94f6d192009-11-24 16:40:02 +01003061 domain = get_domain(dev);
3062 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01003063 virt_addr = (void *)__get_free_pages(flag, get_order(size));
3064 *dma_addr = __pa(virt_addr);
3065 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01003066 } else if (IS_ERR(domain))
3067 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02003068
Joerg Roedelf99c0f12009-11-23 16:52:56 +01003069 dma_mask = dev->coherent_dma_mask;
3070 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
3071 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09003072
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003073 virt_addr = (void *)__get_free_pages(flag, get_order(size));
3074 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05303075 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003076
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003077 paddr = virt_to_phys(virt_addr);
3078
Joerg Roedel832a90c2008-09-18 15:54:23 +02003079 if (!dma_mask)
3080 dma_mask = *dev->dma_mask;
3081
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003082 spin_lock_irqsave(&domain->lock, flags);
3083
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003084 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02003085 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003086
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09003087 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02003088 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01003089 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02003090 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003091
Joerg Roedel17b124b2011-04-06 18:01:35 +02003092 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003093
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003094 spin_unlock_irqrestore(&domain->lock, flags);
3095
3096 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01003097
3098out_free:
3099
3100 free_pages((unsigned long)virt_addr, get_order(size));
3101
3102 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003103}
3104
Joerg Roedel431b2a22008-07-11 17:14:22 +02003105/*
3106 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003107 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003108static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003109 void *virt_addr, dma_addr_t dma_addr,
3110 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003111{
3112 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003113 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003114
Joerg Roedel5d31ee72008-12-12 15:16:38 +01003115 INC_STATS_COUNTER(cnt_free_coherent);
3116
Joerg Roedel94f6d192009-11-24 16:40:02 +01003117 domain = get_domain(dev);
3118 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01003119 goto free_mem;
3120
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003121 spin_lock_irqsave(&domain->lock, flags);
3122
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003123 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003124
Joerg Roedel17b124b2011-04-06 18:01:35 +02003125 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003126
3127 spin_unlock_irqrestore(&domain->lock, flags);
3128
3129free_mem:
3130 free_pages((unsigned long)virt_addr, get_order(size));
3131}
3132
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003133/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003134 * This function is called by the DMA layer to find out if we can handle a
3135 * particular device. It is part of the dma_ops.
3136 */
3137static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3138{
Joerg Roedel420aef82009-11-23 16:14:57 +01003139 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003140}
3141
3142/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003143 * The function for pre-allocating protection domains.
3144 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003145 * If the driver core informs the DMA layer if a driver grabs a device
3146 * we don't need to preallocate the protection domains anymore.
3147 * For now we have to.
3148 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003149static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003150{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003151 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003152 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003153 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003154 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003155
Chris Wrightd18c69d2010-04-02 18:27:55 -07003156 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003157
3158 /* Do we handle this device? */
3159 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003160 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003161
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003162 dev_data = get_dev_data(&dev->dev);
3163 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3164 /* Make sure passthrough domain is allocated */
3165 alloc_passthrough_domain();
3166 dev_data->passthrough = true;
3167 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003168 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003169 dev_name(&dev->dev));
3170 }
3171
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003172 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003173 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003174 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003175
3176 devid = get_device_id(&dev->dev);
3177
Joerg Roedel87a64d52009-11-24 17:26:43 +01003178 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003179 if (!dma_dom)
3180 continue;
3181 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003182 dma_dom->target_dev = devid;
3183
Joerg Roedel15898bb2009-11-24 15:39:42 +01003184 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003185
Joerg Roedelbd60b732008-09-11 10:24:48 +02003186 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003187 }
3188}
3189
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003190static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003191 .alloc = alloc_coherent,
3192 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003193 .map_page = map_page,
3194 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003195 .map_sg = map_sg,
3196 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003197 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003198};
3199
Joerg Roedel27c21272011-05-30 15:56:24 +02003200static unsigned device_dma_ops_init(void)
3201{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003202 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003203 struct pci_dev *pdev = NULL;
3204 unsigned unhandled = 0;
3205
3206 for_each_pci_dev(pdev) {
3207 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003208
3209 iommu_ignore_device(&pdev->dev);
3210
Joerg Roedel27c21272011-05-30 15:56:24 +02003211 unhandled += 1;
3212 continue;
3213 }
3214
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003215 dev_data = get_dev_data(&pdev->dev);
3216
3217 if (!dev_data->passthrough)
3218 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3219 else
3220 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003221 }
3222
3223 return unhandled;
3224}
3225
Joerg Roedel431b2a22008-07-11 17:14:22 +02003226/*
3227 * The function which clues the AMD IOMMU driver into dma_ops.
3228 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003229
3230void __init amd_iommu_init_api(void)
3231{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003232 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003233}
3234
Joerg Roedel6631ee92008-06-26 21:28:05 +02003235int __init amd_iommu_init_dma_ops(void)
3236{
3237 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003238 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003239
Joerg Roedel431b2a22008-07-11 17:14:22 +02003240 /*
3241 * first allocate a default protection domain for every IOMMU we
3242 * found in the system. Devices not assigned to any other
3243 * protection domain will be assigned to the default one.
3244 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003245 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003246 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003247 if (iommu->default_dom == NULL)
3248 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003249 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003250 ret = iommu_init_unity_mappings(iommu);
3251 if (ret)
3252 goto free_domains;
3253 }
3254
Joerg Roedel431b2a22008-07-11 17:14:22 +02003255 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003256 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003257 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003258 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003259
3260 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003261 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003262
Joerg Roedel431b2a22008-07-11 17:14:22 +02003263 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003264 unhandled = device_dma_ops_init();
3265 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3266 /* There are unhandled devices - initialize swiotlb for them */
3267 swiotlb = 1;
3268 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003269
Joerg Roedel7f265082008-12-12 13:50:21 +01003270 amd_iommu_stats_init();
3271
Joerg Roedel62410ee2012-06-12 16:42:43 +02003272 if (amd_iommu_unmap_flush)
3273 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3274 else
3275 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3276
Joerg Roedel6631ee92008-06-26 21:28:05 +02003277 return 0;
3278
3279free_domains:
3280
Joerg Roedel3bd22172009-05-04 15:06:20 +02003281 for_each_iommu(iommu) {
Cyril Roelandt91457df2013-02-12 05:01:50 +01003282 dma_ops_domain_free(iommu->default_dom);
Joerg Roedel6631ee92008-06-26 21:28:05 +02003283 }
3284
3285 return ret;
3286}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003287
3288/*****************************************************************************
3289 *
3290 * The following functions belong to the exported interface of AMD IOMMU
3291 *
3292 * This interface allows access to lower level functions of the IOMMU
3293 * like protection domain handling and assignement of devices to domains
3294 * which is not possible with the dma_ops interface.
3295 *
3296 *****************************************************************************/
3297
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003298static void cleanup_domain(struct protection_domain *domain)
3299{
Joerg Roedel492667d2009-11-27 13:25:47 +01003300 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003301 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003302
3303 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3304
Joerg Roedel492667d2009-11-27 13:25:47 +01003305 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003306 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003307 atomic_set(&dev_data->bind, 0);
3308 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003309
3310 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3311}
3312
Joerg Roedel26508152009-08-26 16:52:40 +02003313static void protection_domain_free(struct protection_domain *domain)
3314{
3315 if (!domain)
3316 return;
3317
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003318 del_domain_from_list(domain);
3319
Joerg Roedel26508152009-08-26 16:52:40 +02003320 if (domain->id)
3321 domain_id_free(domain->id);
3322
3323 kfree(domain);
3324}
3325
3326static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003327{
3328 struct protection_domain *domain;
3329
3330 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3331 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003332 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003333
3334 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003335 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003336 domain->id = domain_id_alloc();
3337 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003338 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003339 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003340
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003341 add_domain_to_list(domain);
3342
Joerg Roedel26508152009-08-26 16:52:40 +02003343 return domain;
3344
3345out_err:
3346 kfree(domain);
3347
3348 return NULL;
3349}
3350
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003351static int __init alloc_passthrough_domain(void)
3352{
3353 if (pt_domain != NULL)
3354 return 0;
3355
3356 /* allocate passthrough domain */
3357 pt_domain = protection_domain_alloc();
3358 if (!pt_domain)
3359 return -ENOMEM;
3360
3361 pt_domain->mode = PAGE_MODE_NONE;
3362
3363 return 0;
3364}
Joerg Roedel26508152009-08-26 16:52:40 +02003365static int amd_iommu_domain_init(struct iommu_domain *dom)
3366{
3367 struct protection_domain *domain;
3368
3369 domain = protection_domain_alloc();
3370 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003371 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003372
3373 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003374 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3375 if (!domain->pt_root)
3376 goto out_free;
3377
Joerg Roedelf3572db2011-11-23 12:36:25 +01003378 domain->iommu_domain = dom;
3379
Joerg Roedelc156e342008-12-02 18:13:27 +01003380 dom->priv = domain;
3381
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003382 dom->geometry.aperture_start = 0;
3383 dom->geometry.aperture_end = ~0ULL;
3384 dom->geometry.force_aperture = true;
3385
Joerg Roedelc156e342008-12-02 18:13:27 +01003386 return 0;
3387
3388out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003389 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003390
3391 return -ENOMEM;
3392}
3393
Joerg Roedel98383fc2008-12-02 18:34:12 +01003394static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3395{
3396 struct protection_domain *domain = dom->priv;
3397
3398 if (!domain)
3399 return;
3400
3401 if (domain->dev_cnt > 0)
3402 cleanup_domain(domain);
3403
3404 BUG_ON(domain->dev_cnt != 0);
3405
Joerg Roedel132bd682011-11-17 14:18:46 +01003406 if (domain->mode != PAGE_MODE_NONE)
3407 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003408
Joerg Roedel52815b72011-11-17 17:24:28 +01003409 if (domain->flags & PD_IOMMUV2_MASK)
3410 free_gcr3_table(domain);
3411
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003412 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003413
3414 dom->priv = NULL;
3415}
3416
Joerg Roedel684f2882008-12-08 12:07:44 +01003417static void amd_iommu_detach_device(struct iommu_domain *dom,
3418 struct device *dev)
3419{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003420 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003421 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003422 u16 devid;
3423
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003424 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003425 return;
3426
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003427 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003428
Joerg Roedel657cbb62009-11-23 15:26:46 +01003429 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003430 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003431
3432 iommu = amd_iommu_rlookup_table[devid];
3433 if (!iommu)
3434 return;
3435
Joerg Roedel684f2882008-12-08 12:07:44 +01003436 iommu_completion_wait(iommu);
3437}
3438
Joerg Roedel01106062008-12-02 19:34:11 +01003439static int amd_iommu_attach_device(struct iommu_domain *dom,
3440 struct device *dev)
3441{
3442 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003443 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003444 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003445 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003446
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003447 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003448 return -EINVAL;
3449
Joerg Roedel657cbb62009-11-23 15:26:46 +01003450 dev_data = dev->archdata.iommu;
3451
Joerg Roedelf62dda62011-06-09 12:55:35 +02003452 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003453 if (!iommu)
3454 return -EINVAL;
3455
Joerg Roedel657cbb62009-11-23 15:26:46 +01003456 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003457 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003458
Joerg Roedel15898bb2009-11-24 15:39:42 +01003459 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003460
3461 iommu_completion_wait(iommu);
3462
Joerg Roedel15898bb2009-11-24 15:39:42 +01003463 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003464}
3465
Joerg Roedel468e2362010-01-21 16:37:36 +01003466static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003467 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003468{
3469 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003470 int prot = 0;
3471 int ret;
3472
Joerg Roedel132bd682011-11-17 14:18:46 +01003473 if (domain->mode == PAGE_MODE_NONE)
3474 return -EINVAL;
3475
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003476 if (iommu_prot & IOMMU_READ)
3477 prot |= IOMMU_PROT_IR;
3478 if (iommu_prot & IOMMU_WRITE)
3479 prot |= IOMMU_PROT_IW;
3480
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003481 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003482 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003483 mutex_unlock(&domain->api_lock);
3484
Joerg Roedel795e74f72010-05-11 17:40:57 +02003485 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003486}
3487
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003488static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3489 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003490{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003491 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003492 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003493
Joerg Roedel132bd682011-11-17 14:18:46 +01003494 if (domain->mode == PAGE_MODE_NONE)
3495 return -EINVAL;
3496
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003497 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003498 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003499 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003500
Joerg Roedel17b124b2011-04-06 18:01:35 +02003501 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003502
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003503 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003504}
3505
Joerg Roedel645c4c82008-12-02 20:05:50 +01003506static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303507 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003508{
3509 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003510 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003511 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003512 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003513
Joerg Roedel132bd682011-11-17 14:18:46 +01003514 if (domain->mode == PAGE_MODE_NONE)
3515 return iova;
3516
Joerg Roedel24cd7722010-01-19 17:27:39 +01003517 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003518
Joerg Roedela6d41a42009-09-02 17:08:55 +02003519 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003520 return 0;
3521
Joerg Roedelf03152b2010-01-21 16:15:24 +01003522 if (PM_PTE_LEVEL(*pte) == 0)
3523 offset_mask = PAGE_SIZE - 1;
3524 else
3525 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3526
3527 __pte = *pte & PM_ADDR_MASK;
3528 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003529
3530 return paddr;
3531}
3532
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003533static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3534 unsigned long cap)
3535{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003536 switch (cap) {
3537 case IOMMU_CAP_CACHE_COHERENCY:
3538 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003539 case IOMMU_CAP_INTR_REMAP:
3540 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003541 }
3542
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003543 return 0;
3544}
3545
Joerg Roedel26961ef2008-12-03 17:00:17 +01003546static struct iommu_ops amd_iommu_ops = {
3547 .domain_init = amd_iommu_domain_init,
3548 .domain_destroy = amd_iommu_domain_destroy,
3549 .attach_dev = amd_iommu_attach_device,
3550 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003551 .map = amd_iommu_map,
3552 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003553 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003554 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003555 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003556};
3557
Joerg Roedel0feae532009-08-26 15:26:30 +02003558/*****************************************************************************
3559 *
3560 * The next functions do a basic initialization of IOMMU for pass through
3561 * mode
3562 *
3563 * In passthrough mode the IOMMU is initialized and enabled but not used for
3564 * DMA-API translation.
3565 *
3566 *****************************************************************************/
3567
3568int __init amd_iommu_init_passthrough(void)
3569{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003570 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003571 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003572 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003573
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003574 ret = alloc_passthrough_domain();
3575 if (ret)
3576 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003577
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003578 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003579 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003580 continue;
3581
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003582 dev_data = get_dev_data(&dev->dev);
3583 dev_data->passthrough = true;
3584
Joerg Roedel15898bb2009-11-24 15:39:42 +01003585 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003586 }
3587
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003588 amd_iommu_stats_init();
3589
Joerg Roedel0feae532009-08-26 15:26:30 +02003590 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3591
3592 return 0;
3593}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003594
3595/* IOMMUv2 specific functions */
3596int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3597{
3598 return atomic_notifier_chain_register(&ppr_notifier, nb);
3599}
3600EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3601
3602int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3603{
3604 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3605}
3606EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003607
3608void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3609{
3610 struct protection_domain *domain = dom->priv;
3611 unsigned long flags;
3612
3613 spin_lock_irqsave(&domain->lock, flags);
3614
3615 /* Update data structure */
3616 domain->mode = PAGE_MODE_NONE;
3617 domain->updated = true;
3618
3619 /* Make changes visible to IOMMUs */
3620 update_domain(domain);
3621
3622 /* Page-table is not visible to IOMMU anymore, so free it */
3623 free_pagetable(domain);
3624
3625 spin_unlock_irqrestore(&domain->lock, flags);
3626}
3627EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003628
3629int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3630{
3631 struct protection_domain *domain = dom->priv;
3632 unsigned long flags;
3633 int levels, ret;
3634
3635 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3636 return -EINVAL;
3637
3638 /* Number of GCR3 table levels required */
3639 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3640 levels += 1;
3641
3642 if (levels > amd_iommu_max_glx_val)
3643 return -EINVAL;
3644
3645 spin_lock_irqsave(&domain->lock, flags);
3646
3647 /*
3648 * Save us all sanity checks whether devices already in the
3649 * domain support IOMMUv2. Just force that the domain has no
3650 * devices attached when it is switched into IOMMUv2 mode.
3651 */
3652 ret = -EBUSY;
3653 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3654 goto out;
3655
3656 ret = -ENOMEM;
3657 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3658 if (domain->gcr3_tbl == NULL)
3659 goto out;
3660
3661 domain->glx = levels;
3662 domain->flags |= PD_IOMMUV2_MASK;
3663 domain->updated = true;
3664
3665 update_domain(domain);
3666
3667 ret = 0;
3668
3669out:
3670 spin_unlock_irqrestore(&domain->lock, flags);
3671
3672 return ret;
3673}
3674EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003675
3676static int __flush_pasid(struct protection_domain *domain, int pasid,
3677 u64 address, bool size)
3678{
3679 struct iommu_dev_data *dev_data;
3680 struct iommu_cmd cmd;
3681 int i, ret;
3682
3683 if (!(domain->flags & PD_IOMMUV2_MASK))
3684 return -EINVAL;
3685
3686 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3687
3688 /*
3689 * IOMMU TLB needs to be flushed before Device TLB to
3690 * prevent device TLB refill from IOMMU TLB
3691 */
3692 for (i = 0; i < amd_iommus_present; ++i) {
3693 if (domain->dev_iommu[i] == 0)
3694 continue;
3695
3696 ret = iommu_queue_command(amd_iommus[i], &cmd);
3697 if (ret != 0)
3698 goto out;
3699 }
3700
3701 /* Wait until IOMMU TLB flushes are complete */
3702 domain_flush_complete(domain);
3703
3704 /* Now flush device TLBs */
3705 list_for_each_entry(dev_data, &domain->dev_list, list) {
3706 struct amd_iommu *iommu;
3707 int qdep;
3708
3709 BUG_ON(!dev_data->ats.enabled);
3710
3711 qdep = dev_data->ats.qdep;
3712 iommu = amd_iommu_rlookup_table[dev_data->devid];
3713
3714 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3715 qdep, address, size);
3716
3717 ret = iommu_queue_command(iommu, &cmd);
3718 if (ret != 0)
3719 goto out;
3720 }
3721
3722 /* Wait until all device TLBs are flushed */
3723 domain_flush_complete(domain);
3724
3725 ret = 0;
3726
3727out:
3728
3729 return ret;
3730}
3731
3732static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3733 u64 address)
3734{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003735 INC_STATS_COUNTER(invalidate_iotlb);
3736
Joerg Roedel22e266c2011-11-21 15:59:08 +01003737 return __flush_pasid(domain, pasid, address, false);
3738}
3739
3740int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3741 u64 address)
3742{
3743 struct protection_domain *domain = dom->priv;
3744 unsigned long flags;
3745 int ret;
3746
3747 spin_lock_irqsave(&domain->lock, flags);
3748 ret = __amd_iommu_flush_page(domain, pasid, address);
3749 spin_unlock_irqrestore(&domain->lock, flags);
3750
3751 return ret;
3752}
3753EXPORT_SYMBOL(amd_iommu_flush_page);
3754
3755static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3756{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003757 INC_STATS_COUNTER(invalidate_iotlb_all);
3758
Joerg Roedel22e266c2011-11-21 15:59:08 +01003759 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3760 true);
3761}
3762
3763int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3764{
3765 struct protection_domain *domain = dom->priv;
3766 unsigned long flags;
3767 int ret;
3768
3769 spin_lock_irqsave(&domain->lock, flags);
3770 ret = __amd_iommu_flush_tlb(domain, pasid);
3771 spin_unlock_irqrestore(&domain->lock, flags);
3772
3773 return ret;
3774}
3775EXPORT_SYMBOL(amd_iommu_flush_tlb);
3776
Joerg Roedelb16137b2011-11-21 16:50:23 +01003777static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3778{
3779 int index;
3780 u64 *pte;
3781
3782 while (true) {
3783
3784 index = (pasid >> (9 * level)) & 0x1ff;
3785 pte = &root[index];
3786
3787 if (level == 0)
3788 break;
3789
3790 if (!(*pte & GCR3_VALID)) {
3791 if (!alloc)
3792 return NULL;
3793
3794 root = (void *)get_zeroed_page(GFP_ATOMIC);
3795 if (root == NULL)
3796 return NULL;
3797
3798 *pte = __pa(root) | GCR3_VALID;
3799 }
3800
3801 root = __va(*pte & PAGE_MASK);
3802
3803 level -= 1;
3804 }
3805
3806 return pte;
3807}
3808
3809static int __set_gcr3(struct protection_domain *domain, int pasid,
3810 unsigned long cr3)
3811{
3812 u64 *pte;
3813
3814 if (domain->mode != PAGE_MODE_NONE)
3815 return -EINVAL;
3816
3817 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3818 if (pte == NULL)
3819 return -ENOMEM;
3820
3821 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3822
3823 return __amd_iommu_flush_tlb(domain, pasid);
3824}
3825
3826static int __clear_gcr3(struct protection_domain *domain, int pasid)
3827{
3828 u64 *pte;
3829
3830 if (domain->mode != PAGE_MODE_NONE)
3831 return -EINVAL;
3832
3833 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3834 if (pte == NULL)
3835 return 0;
3836
3837 *pte = 0;
3838
3839 return __amd_iommu_flush_tlb(domain, pasid);
3840}
3841
3842int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3843 unsigned long cr3)
3844{
3845 struct protection_domain *domain = dom->priv;
3846 unsigned long flags;
3847 int ret;
3848
3849 spin_lock_irqsave(&domain->lock, flags);
3850 ret = __set_gcr3(domain, pasid, cr3);
3851 spin_unlock_irqrestore(&domain->lock, flags);
3852
3853 return ret;
3854}
3855EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3856
3857int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3858{
3859 struct protection_domain *domain = dom->priv;
3860 unsigned long flags;
3861 int ret;
3862
3863 spin_lock_irqsave(&domain->lock, flags);
3864 ret = __clear_gcr3(domain, pasid);
3865 spin_unlock_irqrestore(&domain->lock, flags);
3866
3867 return ret;
3868}
3869EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003870
3871int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3872 int status, int tag)
3873{
3874 struct iommu_dev_data *dev_data;
3875 struct amd_iommu *iommu;
3876 struct iommu_cmd cmd;
3877
Joerg Roedel399be2f2011-12-01 16:53:47 +01003878 INC_STATS_COUNTER(complete_ppr);
3879
Joerg Roedelc99afa22011-11-21 18:19:25 +01003880 dev_data = get_dev_data(&pdev->dev);
3881 iommu = amd_iommu_rlookup_table[dev_data->devid];
3882
3883 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3884 tag, dev_data->pri_tlp);
3885
3886 return iommu_queue_command(iommu, &cmd);
3887}
3888EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003889
3890struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3891{
3892 struct protection_domain *domain;
3893
3894 domain = get_domain(&pdev->dev);
3895 if (IS_ERR(domain))
3896 return NULL;
3897
3898 /* Only return IOMMUv2 domains */
3899 if (!(domain->flags & PD_IOMMUV2_MASK))
3900 return NULL;
3901
3902 return domain->iommu_domain;
3903}
3904EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003905
3906void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3907{
3908 struct iommu_dev_data *dev_data;
3909
3910 if (!amd_iommu_v2_supported())
3911 return;
3912
3913 dev_data = get_dev_data(&pdev->dev);
3914 dev_data->errata |= (1 << erratum);
3915}
3916EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003917
3918int amd_iommu_device_info(struct pci_dev *pdev,
3919 struct amd_iommu_device_info *info)
3920{
3921 int max_pasids;
3922 int pos;
3923
3924 if (pdev == NULL || info == NULL)
3925 return -EINVAL;
3926
3927 if (!amd_iommu_v2_supported())
3928 return -EINVAL;
3929
3930 memset(info, 0, sizeof(*info));
3931
3932 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3933 if (pos)
3934 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3935
3936 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3937 if (pos)
3938 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3939
3940 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3941 if (pos) {
3942 int features;
3943
3944 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3945 max_pasids = min(max_pasids, (1 << 20));
3946
3947 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3948 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3949
3950 features = pci_pasid_features(pdev);
3951 if (features & PCI_PASID_CAP_EXEC)
3952 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3953 if (features & PCI_PASID_CAP_PRIV)
3954 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3955 }
3956
3957 return 0;
3958}
3959EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003960
3961#ifdef CONFIG_IRQ_REMAP
3962
3963/*****************************************************************************
3964 *
3965 * Interrupt Remapping Implementation
3966 *
3967 *****************************************************************************/
3968
3969union irte {
3970 u32 val;
3971 struct {
3972 u32 valid : 1,
3973 no_fault : 1,
3974 int_type : 3,
3975 rq_eoi : 1,
3976 dm : 1,
3977 rsvd_1 : 1,
3978 destination : 8,
3979 vector : 8,
3980 rsvd_2 : 8;
3981 } fields;
3982};
3983
3984#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3985#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3986#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3987#define DTE_IRQ_REMAP_ENABLE 1ULL
3988
3989static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3990{
3991 u64 dte;
3992
3993 dte = amd_iommu_dev_table[devid].data[2];
3994 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3995 dte |= virt_to_phys(table->table);
3996 dte |= DTE_IRQ_REMAP_INTCTL;
3997 dte |= DTE_IRQ_TABLE_LEN;
3998 dte |= DTE_IRQ_REMAP_ENABLE;
3999
4000 amd_iommu_dev_table[devid].data[2] = dte;
4001}
4002
4003#define IRTE_ALLOCATED (~1U)
4004
4005static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
4006{
4007 struct irq_remap_table *table = NULL;
4008 struct amd_iommu *iommu;
4009 unsigned long flags;
4010 u16 alias;
4011
4012 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
4013
4014 iommu = amd_iommu_rlookup_table[devid];
4015 if (!iommu)
4016 goto out_unlock;
4017
4018 table = irq_lookup_table[devid];
4019 if (table)
4020 goto out;
4021
4022 alias = amd_iommu_alias_table[devid];
4023 table = irq_lookup_table[alias];
4024 if (table) {
4025 irq_lookup_table[devid] = table;
4026 set_dte_irq_entry(devid, table);
4027 iommu_flush_dte(iommu, devid);
4028 goto out;
4029 }
4030
4031 /* Nothing there yet, allocate new irq remapping table */
4032 table = kzalloc(sizeof(*table), GFP_ATOMIC);
4033 if (!table)
4034 goto out;
4035
Joerg Roedel197887f2013-04-09 21:14:08 +02004036 /* Initialize table spin-lock */
4037 spin_lock_init(&table->lock);
4038
Joerg Roedel2b324502012-06-21 16:29:10 +02004039 if (ioapic)
4040 /* Keep the first 32 indexes free for IOAPIC interrupts */
4041 table->min_index = 32;
4042
4043 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
4044 if (!table->table) {
4045 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03004046 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02004047 goto out;
4048 }
4049
4050 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
4051
4052 if (ioapic) {
4053 int i;
4054
4055 for (i = 0; i < 32; ++i)
4056 table->table[i] = IRTE_ALLOCATED;
4057 }
4058
4059 irq_lookup_table[devid] = table;
4060 set_dte_irq_entry(devid, table);
4061 iommu_flush_dte(iommu, devid);
4062 if (devid != alias) {
4063 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06004064 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02004065 iommu_flush_dte(iommu, alias);
4066 }
4067
4068out:
4069 iommu_completion_wait(iommu);
4070
4071out_unlock:
4072 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
4073
4074 return table;
4075}
4076
4077static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
4078{
4079 struct irq_remap_table *table;
4080 unsigned long flags;
4081 int index, c;
4082
4083 table = get_irq_table(devid, false);
4084 if (!table)
4085 return -ENODEV;
4086
4087 spin_lock_irqsave(&table->lock, flags);
4088
4089 /* Scan table for free entries */
4090 for (c = 0, index = table->min_index;
4091 index < MAX_IRQS_PER_TABLE;
4092 ++index) {
4093 if (table->table[index] == 0)
4094 c += 1;
4095 else
4096 c = 0;
4097
4098 if (c == count) {
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004099 struct irq_2_irte *irte_info;
Joerg Roedel2b324502012-06-21 16:29:10 +02004100
4101 for (; c != 0; --c)
4102 table->table[index - c + 1] = IRTE_ALLOCATED;
4103
4104 index -= count - 1;
4105
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004106 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004107 irte_info = &cfg->irq_2_irte;
4108 irte_info->devid = devid;
4109 irte_info->index = index;
Joerg Roedel2b324502012-06-21 16:29:10 +02004110
4111 goto out;
4112 }
4113 }
4114
4115 index = -ENOSPC;
4116
4117out:
4118 spin_unlock_irqrestore(&table->lock, flags);
4119
4120 return index;
4121}
4122
4123static int get_irte(u16 devid, int index, union irte *irte)
4124{
4125 struct irq_remap_table *table;
4126 unsigned long flags;
4127
4128 table = get_irq_table(devid, false);
4129 if (!table)
4130 return -ENOMEM;
4131
4132 spin_lock_irqsave(&table->lock, flags);
4133 irte->val = table->table[index];
4134 spin_unlock_irqrestore(&table->lock, flags);
4135
4136 return 0;
4137}
4138
4139static int modify_irte(u16 devid, int index, union irte irte)
4140{
4141 struct irq_remap_table *table;
4142 struct amd_iommu *iommu;
4143 unsigned long flags;
4144
4145 iommu = amd_iommu_rlookup_table[devid];
4146 if (iommu == NULL)
4147 return -EINVAL;
4148
4149 table = get_irq_table(devid, false);
4150 if (!table)
4151 return -ENOMEM;
4152
4153 spin_lock_irqsave(&table->lock, flags);
4154 table->table[index] = irte.val;
4155 spin_unlock_irqrestore(&table->lock, flags);
4156
4157 iommu_flush_irt(iommu, devid);
4158 iommu_completion_wait(iommu);
4159
4160 return 0;
4161}
4162
4163static void free_irte(u16 devid, int index)
4164{
4165 struct irq_remap_table *table;
4166 struct amd_iommu *iommu;
4167 unsigned long flags;
4168
4169 iommu = amd_iommu_rlookup_table[devid];
4170 if (iommu == NULL)
4171 return;
4172
4173 table = get_irq_table(devid, false);
4174 if (!table)
4175 return;
4176
4177 spin_lock_irqsave(&table->lock, flags);
4178 table->table[index] = 0;
4179 spin_unlock_irqrestore(&table->lock, flags);
4180
4181 iommu_flush_irt(iommu, devid);
4182 iommu_completion_wait(iommu);
4183}
4184
Joerg Roedel5527de72012-06-26 11:17:32 +02004185static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4186 unsigned int destination, int vector,
4187 struct io_apic_irq_attr *attr)
4188{
4189 struct irq_remap_table *table;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004190 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004191 struct irq_cfg *cfg;
4192 union irte irte;
4193 int ioapic_id;
4194 int index;
4195 int devid;
4196 int ret;
4197
4198 cfg = irq_get_chip_data(irq);
4199 if (!cfg)
4200 return -EINVAL;
4201
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004202 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004203 ioapic_id = mpc_ioapic_id(attr->ioapic);
4204 devid = get_ioapic_devid(ioapic_id);
4205
4206 if (devid < 0)
4207 return devid;
4208
4209 table = get_irq_table(devid, true);
4210 if (table == NULL)
4211 return -ENOMEM;
4212
4213 index = attr->ioapic_pin;
4214
4215 /* Setup IRQ remapping info */
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004216 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004217 irte_info->devid = devid;
4218 irte_info->index = index;
Joerg Roedel5527de72012-06-26 11:17:32 +02004219
4220 /* Setup IRTE for IOMMU */
4221 irte.val = 0;
4222 irte.fields.vector = vector;
4223 irte.fields.int_type = apic->irq_delivery_mode;
4224 irte.fields.destination = destination;
4225 irte.fields.dm = apic->irq_dest_mode;
4226 irte.fields.valid = 1;
4227
4228 ret = modify_irte(devid, index, irte);
4229 if (ret)
4230 return ret;
4231
4232 /* Setup IOAPIC entry */
4233 memset(entry, 0, sizeof(*entry));
4234
4235 entry->vector = index;
4236 entry->mask = 0;
4237 entry->trigger = attr->trigger;
4238 entry->polarity = attr->polarity;
4239
4240 /*
4241 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004242 */
4243 if (attr->trigger)
4244 entry->mask = 1;
4245
4246 return 0;
4247}
4248
4249static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4250 bool force)
4251{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004252 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004253 unsigned int dest, irq;
4254 struct irq_cfg *cfg;
4255 union irte irte;
4256 int err;
4257
4258 if (!config_enabled(CONFIG_SMP))
4259 return -1;
4260
4261 cfg = data->chip_data;
4262 irq = data->irq;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004263 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004264
4265 if (!cpumask_intersects(mask, cpu_online_mask))
4266 return -EINVAL;
4267
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004268 if (get_irte(irte_info->devid, irte_info->index, &irte))
Joerg Roedel5527de72012-06-26 11:17:32 +02004269 return -EBUSY;
4270
4271 if (assign_irq_vector(irq, cfg, mask))
4272 return -EBUSY;
4273
4274 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4275 if (err) {
4276 if (assign_irq_vector(irq, cfg, data->affinity))
4277 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4278 return err;
4279 }
4280
4281 irte.fields.vector = cfg->vector;
4282 irte.fields.destination = dest;
4283
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004284 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel5527de72012-06-26 11:17:32 +02004285
4286 if (cfg->move_in_progress)
4287 send_cleanup_vector(cfg);
4288
4289 cpumask_copy(data->affinity, mask);
4290
4291 return 0;
4292}
4293
4294static int free_irq(int irq)
4295{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004296 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004297 struct irq_cfg *cfg;
4298
4299 cfg = irq_get_chip_data(irq);
4300 if (!cfg)
4301 return -EINVAL;
4302
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004303 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004304
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004305 free_irte(irte_info->devid, irte_info->index);
Joerg Roedel5527de72012-06-26 11:17:32 +02004306
4307 return 0;
4308}
4309
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004310static void compose_msi_msg(struct pci_dev *pdev,
4311 unsigned int irq, unsigned int dest,
4312 struct msi_msg *msg, u8 hpet_id)
4313{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004314 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004315 struct irq_cfg *cfg;
4316 union irte irte;
4317
4318 cfg = irq_get_chip_data(irq);
4319 if (!cfg)
4320 return;
4321
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004322 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004323
4324 irte.val = 0;
4325 irte.fields.vector = cfg->vector;
4326 irte.fields.int_type = apic->irq_delivery_mode;
4327 irte.fields.destination = dest;
4328 irte.fields.dm = apic->irq_dest_mode;
4329 irte.fields.valid = 1;
4330
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004331 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004332
4333 msg->address_hi = MSI_ADDR_BASE_HI;
4334 msg->address_lo = MSI_ADDR_BASE_LO;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004335 msg->data = irte_info->index;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004336}
4337
4338static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4339{
4340 struct irq_cfg *cfg;
4341 int index;
4342 u16 devid;
4343
4344 if (!pdev)
4345 return -EINVAL;
4346
4347 cfg = irq_get_chip_data(irq);
4348 if (!cfg)
4349 return -EINVAL;
4350
4351 devid = get_device_id(&pdev->dev);
4352 index = alloc_irq_index(cfg, devid, nvec);
4353
4354 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4355}
4356
4357static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4358 int index, int offset)
4359{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004360 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004361 struct irq_cfg *cfg;
4362 u16 devid;
4363
4364 if (!pdev)
4365 return -EINVAL;
4366
4367 cfg = irq_get_chip_data(irq);
4368 if (!cfg)
4369 return -EINVAL;
4370
4371 if (index >= MAX_IRQS_PER_TABLE)
4372 return 0;
4373
4374 devid = get_device_id(&pdev->dev);
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004375 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004376
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004377 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004378 irte_info->devid = devid;
4379 irte_info->index = index + offset;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004380
4381 return 0;
4382}
4383
Joerg Roedeld9761952012-06-26 16:00:08 +02004384static int setup_hpet_msi(unsigned int irq, unsigned int id)
4385{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004386 struct irq_2_irte *irte_info;
Joerg Roedeld9761952012-06-26 16:00:08 +02004387 struct irq_cfg *cfg;
4388 int index, devid;
4389
4390 cfg = irq_get_chip_data(irq);
4391 if (!cfg)
4392 return -EINVAL;
4393
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004394 irte_info = &cfg->irq_2_irte;
Joerg Roedeld9761952012-06-26 16:00:08 +02004395 devid = get_hpet_devid(id);
4396 if (devid < 0)
4397 return devid;
4398
4399 index = alloc_irq_index(cfg, devid, 1);
4400 if (index < 0)
4401 return index;
4402
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004403 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004404 irte_info->devid = devid;
4405 irte_info->index = index;
Joerg Roedeld9761952012-06-26 16:00:08 +02004406
4407 return 0;
4408}
4409
Joerg Roedel6b474b82012-06-26 16:46:04 +02004410struct irq_remap_ops amd_iommu_irq_ops = {
4411 .supported = amd_iommu_supported,
4412 .prepare = amd_iommu_prepare,
4413 .enable = amd_iommu_enable,
4414 .disable = amd_iommu_disable,
4415 .reenable = amd_iommu_reenable,
4416 .enable_faulting = amd_iommu_enable_faulting,
4417 .setup_ioapic_entry = setup_ioapic_entry,
4418 .set_affinity = set_affinity,
4419 .free_irq = free_irq,
4420 .compose_msi_msg = compose_msi_msg,
4421 .msi_alloc_irq = msi_alloc_irq,
4422 .msi_setup_irq = msi_setup_irq,
4423 .setup_hpet_msi = setup_hpet_msi,
4424};
Joerg Roedel2b324502012-06-21 16:29:10 +02004425#endif