blob: c6f3c7e046840d4b6de11c9c3c06c91e25f14431 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedelb6c02712008-06-26 21:27:53 +02003 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020022#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010025#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090027#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010029#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020030#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020031#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010032#include <linux/notifier.h>
33#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020034#include <linux/irq.h>
35#include <linux/msi.h>
36#include <asm/irq_remapping.h>
37#include <asm/io_apic.h>
38#include <asm/apic.h>
39#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020040#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020041#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090042#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010043#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020044#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020045
46#include "amd_iommu_proto.h"
47#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020048#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020049
50#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
51
Joerg Roedel815b33f2011-04-06 17:26:49 +020052#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020053
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020054/*
55 * This bitmap is used to advertise the page sizes our hardware support
56 * to the IOMMU core, which will then use this information to split
57 * physically contiguous memory regions it is mapping into page sizes
58 * that we support.
59 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010060 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020061 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010062#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020063
Joerg Roedelb6c02712008-06-26 21:27:53 +020064static DEFINE_RWLOCK(amd_iommu_devtable_lock);
65
Joerg Roedelbd60b732008-09-11 10:24:48 +020066/* A list of preallocated protection domains */
67static LIST_HEAD(iommu_pd_list);
68static DEFINE_SPINLOCK(iommu_pd_list_lock);
69
Joerg Roedel8fa5f802011-06-09 12:24:45 +020070/* List of all available dev_data structures */
71static LIST_HEAD(dev_data_list);
72static DEFINE_SPINLOCK(dev_data_list_lock);
73
Joerg Roedel6efed632012-06-14 15:52:58 +020074LIST_HEAD(ioapic_map);
75LIST_HEAD(hpet_map);
76
Joerg Roedel0feae532009-08-26 15:26:30 +020077/*
78 * Domain for untranslated devices - only allocated
79 * if iommu=pt passed on kernel cmd line.
80 */
81static struct protection_domain *pt_domain;
82
Joerg Roedel26961ef2008-12-03 17:00:17 +010083static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010084
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010085static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010086int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010087
Joerg Roedelac1534a2012-06-21 14:52:40 +020088static struct dma_map_ops amd_iommu_dma_ops;
89
Joerg Roedel431b2a22008-07-11 17:14:22 +020090/*
91 * general struct to manage commands send to an IOMMU
92 */
Joerg Roedeld6449532008-07-11 17:14:28 +020093struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020094 u32 data[4];
95};
96
Joerg Roedel05152a02012-06-15 16:53:51 +020097struct kmem_cache *amd_iommu_irq_cache;
98
Joerg Roedel04bfdd82009-09-02 16:00:23 +020099static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100100static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700101
Joerg Roedel15898bb2009-11-24 15:39:42 +0100102/****************************************************************************
103 *
104 * Helper functions
105 *
106 ****************************************************************************/
107
Joerg Roedelf62dda62011-06-09 12:55:35 +0200108static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200109{
110 struct iommu_dev_data *dev_data;
111 unsigned long flags;
112
113 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
114 if (!dev_data)
115 return NULL;
116
Joerg Roedelf62dda62011-06-09 12:55:35 +0200117 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200118 atomic_set(&dev_data->bind, 0);
119
120 spin_lock_irqsave(&dev_data_list_lock, flags);
121 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
122 spin_unlock_irqrestore(&dev_data_list_lock, flags);
123
124 return dev_data;
125}
126
127static void free_dev_data(struct iommu_dev_data *dev_data)
128{
129 unsigned long flags;
130
131 spin_lock_irqsave(&dev_data_list_lock, flags);
132 list_del(&dev_data->dev_data_list);
133 spin_unlock_irqrestore(&dev_data_list_lock, flags);
134
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600135 if (dev_data->group)
136 iommu_group_put(dev_data->group);
137
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200138 kfree(dev_data);
139}
140
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200141static struct iommu_dev_data *search_dev_data(u16 devid)
142{
143 struct iommu_dev_data *dev_data;
144 unsigned long flags;
145
146 spin_lock_irqsave(&dev_data_list_lock, flags);
147 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
148 if (dev_data->devid == devid)
149 goto out_unlock;
150 }
151
152 dev_data = NULL;
153
154out_unlock:
155 spin_unlock_irqrestore(&dev_data_list_lock, flags);
156
157 return dev_data;
158}
159
160static struct iommu_dev_data *find_dev_data(u16 devid)
161{
162 struct iommu_dev_data *dev_data;
163
164 dev_data = search_dev_data(devid);
165
166 if (dev_data == NULL)
167 dev_data = alloc_dev_data(devid);
168
169 return dev_data;
170}
171
Joerg Roedel15898bb2009-11-24 15:39:42 +0100172static inline u16 get_device_id(struct device *dev)
173{
174 struct pci_dev *pdev = to_pci_dev(dev);
175
176 return calc_devid(pdev->bus->number, pdev->devfn);
177}
178
Joerg Roedel657cbb62009-11-23 15:26:46 +0100179static struct iommu_dev_data *get_dev_data(struct device *dev)
180{
181 return dev->archdata.iommu;
182}
183
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100184static bool pci_iommuv2_capable(struct pci_dev *pdev)
185{
186 static const int caps[] = {
187 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100188 PCI_EXT_CAP_ID_PRI,
189 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100190 };
191 int i, pos;
192
193 for (i = 0; i < 3; ++i) {
194 pos = pci_find_ext_capability(pdev, caps[i]);
195 if (pos == 0)
196 return false;
197 }
198
199 return true;
200}
201
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100202static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
203{
204 struct iommu_dev_data *dev_data;
205
206 dev_data = get_dev_data(&pdev->dev);
207
208 return dev_data->errata & (1 << erratum) ? true : false;
209}
210
Joerg Roedel71c70982009-11-24 16:43:06 +0100211/*
212 * In this function the list of preallocated protection domains is traversed to
213 * find the domain for a specific device
214 */
215static struct dma_ops_domain *find_protection_domain(u16 devid)
216{
217 struct dma_ops_domain *entry, *ret = NULL;
218 unsigned long flags;
219 u16 alias = amd_iommu_alias_table[devid];
220
221 if (list_empty(&iommu_pd_list))
222 return NULL;
223
224 spin_lock_irqsave(&iommu_pd_list_lock, flags);
225
226 list_for_each_entry(entry, &iommu_pd_list, list) {
227 if (entry->target_dev == devid ||
228 entry->target_dev == alias) {
229 ret = entry;
230 break;
231 }
232 }
233
234 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
235
236 return ret;
237}
238
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100239/*
240 * This function checks if the driver got a valid device from the caller to
241 * avoid dereferencing invalid pointers.
242 */
243static bool check_device(struct device *dev)
244{
245 u16 devid;
246
247 if (!dev || !dev->dma_mask)
248 return false;
249
250 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100251 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100252 return false;
253
254 devid = get_device_id(dev);
255
256 /* Out of our scope? */
257 if (devid > amd_iommu_last_bdf)
258 return false;
259
260 if (amd_iommu_rlookup_table[devid] == NULL)
261 return false;
262
263 return true;
264}
265
Alex Williamson664b6002012-05-30 14:19:31 -0600266static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
267{
268 pci_dev_put(*from);
269 *from = to;
270}
271
Alex Williamson2bff6a52012-10-08 22:49:48 -0600272static struct pci_bus *find_hosted_bus(struct pci_bus *bus)
273{
274 while (!bus->self) {
275 if (!pci_is_root_bus(bus))
276 bus = bus->parent;
277 else
278 return ERR_PTR(-ENODEV);
279 }
280
281 return bus;
282}
283
Alex Williamson664b6002012-05-30 14:19:31 -0600284#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
285
Alex Williamson2851db22012-10-08 22:49:41 -0600286static struct pci_dev *get_isolation_root(struct pci_dev *pdev)
Joerg Roedel657cbb62009-11-23 15:26:46 +0100287{
Alex Williamson2851db22012-10-08 22:49:41 -0600288 struct pci_dev *dma_pdev = pdev;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600289
Alex Williamson31fe9432012-08-04 12:09:03 -0600290 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600291 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
292
Alex Williamson31fe9432012-08-04 12:09:03 -0600293 /*
294 * If it's a multifunction device that does not support our
295 * required ACS flags, add to the same group as function 0.
296 */
Alex Williamson664b6002012-05-30 14:19:31 -0600297 if (dma_pdev->multifunction &&
298 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
299 swap_pci_ref(&dma_pdev,
300 pci_get_slot(dma_pdev->bus,
301 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
302 0)));
303
Alex Williamson31fe9432012-08-04 12:09:03 -0600304 /*
305 * Devices on the root bus go through the iommu. If that's not us,
306 * find the next upstream device and test ACS up to the root bus.
307 * Finding the next device may require skipping virtual buses.
308 */
Alex Williamson664b6002012-05-30 14:19:31 -0600309 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson2bff6a52012-10-08 22:49:48 -0600310 struct pci_bus *bus = find_hosted_bus(dma_pdev->bus);
311 if (IS_ERR(bus))
312 break;
Alex Williamson31fe9432012-08-04 12:09:03 -0600313
314 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600315 break;
316
Alex Williamson31fe9432012-08-04 12:09:03 -0600317 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200318 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100319
Alex Williamson2851db22012-10-08 22:49:41 -0600320 return dma_pdev;
321}
322
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600323static int use_pdev_iommu_group(struct pci_dev *pdev, struct device *dev)
324{
325 struct iommu_group *group = iommu_group_get(&pdev->dev);
326 int ret;
327
328 if (!group) {
329 group = iommu_group_alloc();
330 if (IS_ERR(group))
331 return PTR_ERR(group);
332
333 WARN_ON(&pdev->dev != dev);
334 }
335
336 ret = iommu_group_add_device(group, dev);
337 iommu_group_put(group);
338 return ret;
339}
340
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600341static int use_dev_data_iommu_group(struct iommu_dev_data *dev_data,
342 struct device *dev)
343{
344 if (!dev_data->group) {
345 struct iommu_group *group = iommu_group_alloc();
346 if (IS_ERR(group))
347 return PTR_ERR(group);
348
349 dev_data->group = group;
350 }
351
352 return iommu_group_add_device(dev_data->group, dev);
353}
354
Alex Williamson2851db22012-10-08 22:49:41 -0600355static int init_iommu_group(struct device *dev)
356{
357 struct iommu_dev_data *dev_data;
358 struct iommu_group *group;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600359 struct pci_dev *dma_pdev;
Alex Williamson2851db22012-10-08 22:49:41 -0600360 int ret;
361
362 group = iommu_group_get(dev);
363 if (group) {
364 iommu_group_put(group);
365 return 0;
366 }
367
368 dev_data = find_dev_data(get_device_id(dev));
369 if (!dev_data)
370 return -ENOMEM;
371
372 if (dev_data->alias_data) {
373 u16 alias;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600374 struct pci_bus *bus;
Alex Williamson2851db22012-10-08 22:49:41 -0600375
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600376 if (dev_data->alias_data->group)
377 goto use_group;
378
379 /*
380 * If the alias device exists, it's effectively just a first
381 * level quirk for finding the DMA source.
382 */
Alex Williamson2851db22012-10-08 22:49:41 -0600383 alias = amd_iommu_alias_table[dev_data->devid];
384 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600385 if (dma_pdev) {
386 dma_pdev = get_isolation_root(dma_pdev);
387 goto use_pdev;
388 }
389
390 /*
391 * If the alias is virtual, try to find a parent device
392 * and test whether the IOMMU group is actualy rooted above
393 * the alias. Be careful to also test the parent device if
394 * we think the alias is the root of the group.
395 */
396 bus = pci_find_bus(0, alias >> 8);
397 if (!bus)
398 goto use_group;
399
400 bus = find_hosted_bus(bus);
401 if (IS_ERR(bus) || !bus->self)
402 goto use_group;
403
404 dma_pdev = get_isolation_root(pci_dev_get(bus->self));
405 if (dma_pdev != bus->self || (dma_pdev->multifunction &&
406 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)))
407 goto use_pdev;
408
409 pci_dev_put(dma_pdev);
410 goto use_group;
Alex Williamson2851db22012-10-08 22:49:41 -0600411 }
412
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600413 dma_pdev = get_isolation_root(pci_dev_get(to_pci_dev(dev)));
414use_pdev:
Alex Williamsonce7ac4a2012-10-08 22:49:54 -0600415 ret = use_pdev_iommu_group(dma_pdev, dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600416 pci_dev_put(dma_pdev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600417 return ret;
Alex Williamson78bfa9f2012-10-08 22:50:00 -0600418use_group:
419 return use_dev_data_iommu_group(dev_data->alias_data, dev);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600420}
421
422static int iommu_init_device(struct device *dev)
423{
424 struct pci_dev *pdev = to_pci_dev(dev);
425 struct iommu_dev_data *dev_data;
426 u16 alias;
427 int ret;
428
429 if (dev->archdata.iommu)
430 return 0;
431
432 dev_data = find_dev_data(get_device_id(dev));
433 if (!dev_data)
434 return -ENOMEM;
435
436 alias = amd_iommu_alias_table[dev_data->devid];
437 if (alias != dev_data->devid) {
438 struct iommu_dev_data *alias_data;
439
440 alias_data = find_dev_data(alias);
441 if (alias_data == NULL) {
442 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
443 dev_name(dev));
444 free_dev_data(dev_data);
445 return -ENOTSUPP;
446 }
447 dev_data->alias_data = alias_data;
448 }
449
450 ret = init_iommu_group(dev);
Alex Williamson9dcd6132012-05-30 14:19:07 -0600451 if (ret)
452 return ret;
453
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100454 if (pci_iommuv2_capable(pdev)) {
455 struct amd_iommu *iommu;
456
457 iommu = amd_iommu_rlookup_table[dev_data->devid];
458 dev_data->iommu_v2 = iommu->is_iommu_v2;
459 }
460
Joerg Roedel657cbb62009-11-23 15:26:46 +0100461 dev->archdata.iommu = dev_data;
462
Joerg Roedel657cbb62009-11-23 15:26:46 +0100463 return 0;
464}
465
Joerg Roedel26018872011-06-06 16:50:14 +0200466static void iommu_ignore_device(struct device *dev)
467{
468 u16 devid, alias;
469
470 devid = get_device_id(dev);
471 alias = amd_iommu_alias_table[devid];
472
473 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
474 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
475
476 amd_iommu_rlookup_table[devid] = NULL;
477 amd_iommu_rlookup_table[alias] = NULL;
478}
479
Joerg Roedel657cbb62009-11-23 15:26:46 +0100480static void iommu_uninit_device(struct device *dev)
481{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600482 iommu_group_remove_device(dev);
483
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200484 /*
485 * Nothing to do here - we keep dev_data around for unplugged devices
486 * and reuse it when the device is re-plugged - not doing so would
487 * introduce a ton of races.
488 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100489}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100490
491void __init amd_iommu_uninit_devices(void)
492{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200493 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100494 struct pci_dev *pdev = NULL;
495
496 for_each_pci_dev(pdev) {
497
498 if (!check_device(&pdev->dev))
499 continue;
500
501 iommu_uninit_device(&pdev->dev);
502 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200503
504 /* Free all of our dev_data structures */
505 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
506 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100507}
508
509int __init amd_iommu_init_devices(void)
510{
511 struct pci_dev *pdev = NULL;
512 int ret = 0;
513
514 for_each_pci_dev(pdev) {
515
516 if (!check_device(&pdev->dev))
517 continue;
518
519 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200520 if (ret == -ENOTSUPP)
521 iommu_ignore_device(&pdev->dev);
522 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100523 goto out_free;
524 }
525
526 return 0;
527
528out_free:
529
530 amd_iommu_uninit_devices();
531
532 return ret;
533}
Joerg Roedel7f265082008-12-12 13:50:21 +0100534#ifdef CONFIG_AMD_IOMMU_STATS
535
536/*
537 * Initialization code for statistics collection
538 */
539
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100540DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100541DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100542DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100543DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100544DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100545DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100546DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100547DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100548DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100549DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100550DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100551DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100552DECLARE_STATS_COUNTER(complete_ppr);
553DECLARE_STATS_COUNTER(invalidate_iotlb);
554DECLARE_STATS_COUNTER(invalidate_iotlb_all);
555DECLARE_STATS_COUNTER(pri_requests);
556
Joerg Roedel7f265082008-12-12 13:50:21 +0100557static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100558static struct dentry *de_fflush;
559
560static void amd_iommu_stats_add(struct __iommu_counter *cnt)
561{
562 if (stats_dir == NULL)
563 return;
564
565 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
566 &cnt->value);
567}
568
569static void amd_iommu_stats_init(void)
570{
571 stats_dir = debugfs_create_dir("amd-iommu", NULL);
572 if (stats_dir == NULL)
573 return;
574
Joerg Roedel7f265082008-12-12 13:50:21 +0100575 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300576 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100577
578 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100579 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100580 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100581 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100582 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100583 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100584 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100585 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100586 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100587 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100588 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100589 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100590 amd_iommu_stats_add(&complete_ppr);
591 amd_iommu_stats_add(&invalidate_iotlb);
592 amd_iommu_stats_add(&invalidate_iotlb_all);
593 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100594}
595
596#endif
597
Joerg Roedel431b2a22008-07-11 17:14:22 +0200598/****************************************************************************
599 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200600 * Interrupt handling functions
601 *
602 ****************************************************************************/
603
Joerg Roedele3e59872009-09-03 14:02:10 +0200604static void dump_dte_entry(u16 devid)
605{
606 int i;
607
Joerg Roedelee6c2862011-11-09 12:06:03 +0100608 for (i = 0; i < 4; ++i)
609 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200610 amd_iommu_dev_table[devid].data[i]);
611}
612
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200613static void dump_command(unsigned long phys_addr)
614{
615 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
616 int i;
617
618 for (i = 0; i < 4; ++i)
619 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
620}
621
Joerg Roedela345b232009-09-03 15:01:43 +0200622static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200623{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200624 int type, devid, domid, flags;
625 volatile u32 *event = __evt;
626 int count = 0;
627 u64 address;
628
629retry:
630 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
631 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
632 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
633 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
634 address = (u64)(((u64)event[3]) << 32) | event[2];
635
636 if (type == 0) {
637 /* Did we hit the erratum? */
638 if (++count == LOOP_TIMEOUT) {
639 pr_err("AMD-Vi: No event written to event log\n");
640 return;
641 }
642 udelay(1);
643 goto retry;
644 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200645
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200646 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200647
648 switch (type) {
649 case EVENT_TYPE_ILL_DEV:
650 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
651 "address=0x%016llx flags=0x%04x]\n",
652 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
653 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200654 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200655 break;
656 case EVENT_TYPE_IO_FAULT:
657 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
658 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
659 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
660 domid, address, flags);
661 break;
662 case EVENT_TYPE_DEV_TAB_ERR:
663 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
664 "address=0x%016llx flags=0x%04x]\n",
665 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
666 address, flags);
667 break;
668 case EVENT_TYPE_PAGE_TAB_ERR:
669 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
670 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
671 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
672 domid, address, flags);
673 break;
674 case EVENT_TYPE_ILL_CMD:
675 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200676 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200677 break;
678 case EVENT_TYPE_CMD_HARD_ERR:
679 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
680 "flags=0x%04x]\n", address, flags);
681 break;
682 case EVENT_TYPE_IOTLB_INV_TO:
683 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
684 "address=0x%016llx]\n",
685 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
686 address);
687 break;
688 case EVENT_TYPE_INV_DEV_REQ:
689 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
690 "address=0x%016llx flags=0x%04x]\n",
691 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
692 address, flags);
693 break;
694 default:
695 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
696 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200697
698 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200699}
700
701static void iommu_poll_events(struct amd_iommu *iommu)
702{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500703 u32 head, tail;
Joerg Roedeld3263bc2013-04-18 17:55:04 +0200704
Joerg Roedel90008ee2008-09-09 16:41:05 +0200705 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
706 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
707
708 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200709 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200710 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
711 }
712
713 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200714}
715
Joerg Roedeleee53532012-06-01 15:20:23 +0200716static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100717{
718 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100719
Joerg Roedel399be2f2011-12-01 16:53:47 +0100720 INC_STATS_COUNTER(pri_requests);
721
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100722 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
723 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
724 return;
725 }
726
727 fault.address = raw[1];
728 fault.pasid = PPR_PASID(raw[0]);
729 fault.device_id = PPR_DEVID(raw[0]);
730 fault.tag = PPR_TAG(raw[0]);
731 fault.flags = PPR_FLAGS(raw[0]);
732
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100733 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
734}
735
736static void iommu_poll_ppr_log(struct amd_iommu *iommu)
737{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500738 u32 head, tail;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100739
740 if (iommu->ppr_log == NULL)
741 return;
742
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100743 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
744 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
745
746 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200747 volatile u64 *raw;
748 u64 entry[2];
749 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100750
Joerg Roedeleee53532012-06-01 15:20:23 +0200751 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100752
Joerg Roedeleee53532012-06-01 15:20:23 +0200753 /*
754 * Hardware bug: Interrupt may arrive before the entry is
755 * written to memory. If this happens we need to wait for the
756 * entry to arrive.
757 */
758 for (i = 0; i < LOOP_TIMEOUT; ++i) {
759 if (PPR_REQ_TYPE(raw[0]) != 0)
760 break;
761 udelay(1);
762 }
763
764 /* Avoid memcpy function-call overhead */
765 entry[0] = raw[0];
766 entry[1] = raw[1];
767
768 /*
769 * To detect the hardware bug we need to clear the entry
770 * back to zero.
771 */
772 raw[0] = raw[1] = 0UL;
773
774 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100775 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
776 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200777
Joerg Roedeleee53532012-06-01 15:20:23 +0200778 /* Handle PPR entry */
779 iommu_handle_ppr_entry(iommu, entry);
780
Joerg Roedeleee53532012-06-01 15:20:23 +0200781 /* Refresh ring-buffer information */
782 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100783 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
784 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100785}
786
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200787irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200788{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500789 struct amd_iommu *iommu = (struct amd_iommu *) data;
790 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200791
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500792 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
793 /* Enable EVT and PPR interrupts again */
794 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
795 iommu->mmio_base + MMIO_STATUS_OFFSET);
796
797 if (status & MMIO_STATUS_EVT_INT_MASK) {
798 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
799 iommu_poll_events(iommu);
800 }
801
802 if (status & MMIO_STATUS_PPR_INT_MASK) {
803 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
804 iommu_poll_ppr_log(iommu);
805 }
806
807 /*
808 * Hardware bug: ERBT1312
809 * When re-enabling interrupt (by writing 1
810 * to clear the bit), the hardware might also try to set
811 * the interrupt bit in the event status register.
812 * In this scenario, the bit will be set, and disable
813 * subsequent interrupts.
814 *
815 * Workaround: The IOMMU driver should read back the
816 * status register and check if the interrupt bits are cleared.
817 * If not, driver will need to go through the interrupt handler
818 * again and re-clear the bits
819 */
820 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100821 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200822 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200823}
824
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200825irqreturn_t amd_iommu_int_handler(int irq, void *data)
826{
827 return IRQ_WAKE_THREAD;
828}
829
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200830/****************************************************************************
831 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200832 * IOMMU command queuing functions
833 *
834 ****************************************************************************/
835
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200836static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200837{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200838 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200839
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200840 while (*sem == 0 && i < LOOP_TIMEOUT) {
841 udelay(1);
842 i += 1;
843 }
844
845 if (i == LOOP_TIMEOUT) {
846 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
847 return -EIO;
848 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200849
850 return 0;
851}
852
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200853static void copy_cmd_to_buffer(struct amd_iommu *iommu,
854 struct iommu_cmd *cmd,
855 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200856{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200857 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200858
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200859 target = iommu->cmd_buf + tail;
860 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200861
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200862 /* Copy command to buffer */
863 memcpy(target, cmd, sizeof(*cmd));
864
865 /* Tell the IOMMU about it */
866 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
867}
868
Joerg Roedel815b33f2011-04-06 17:26:49 +0200869static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200870{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200871 WARN_ON(address & 0x7ULL);
872
Joerg Roedelded46732011-04-06 10:53:48 +0200873 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200874 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
875 cmd->data[1] = upper_32_bits(__pa(address));
876 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200877 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
878}
879
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200880static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
881{
882 memset(cmd, 0, sizeof(*cmd));
883 cmd->data[0] = devid;
884 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
885}
886
Joerg Roedel11b64022011-04-06 11:49:28 +0200887static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
888 size_t size, u16 domid, int pde)
889{
890 u64 pages;
891 int s;
892
893 pages = iommu_num_pages(address, size, PAGE_SIZE);
894 s = 0;
895
896 if (pages > 1) {
897 /*
898 * If we have to flush more than one page, flush all
899 * TLB entries for this domain
900 */
901 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
902 s = 1;
903 }
904
905 address &= PAGE_MASK;
906
907 memset(cmd, 0, sizeof(*cmd));
908 cmd->data[1] |= domid;
909 cmd->data[2] = lower_32_bits(address);
910 cmd->data[3] = upper_32_bits(address);
911 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
912 if (s) /* size bit - we flush more than one 4kb page */
913 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200914 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200915 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
916}
917
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200918static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
919 u64 address, size_t size)
920{
921 u64 pages;
922 int s;
923
924 pages = iommu_num_pages(address, size, PAGE_SIZE);
925 s = 0;
926
927 if (pages > 1) {
928 /*
929 * If we have to flush more than one page, flush all
930 * TLB entries for this domain
931 */
932 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
933 s = 1;
934 }
935
936 address &= PAGE_MASK;
937
938 memset(cmd, 0, sizeof(*cmd));
939 cmd->data[0] = devid;
940 cmd->data[0] |= (qdep & 0xff) << 24;
941 cmd->data[1] = devid;
942 cmd->data[2] = lower_32_bits(address);
943 cmd->data[3] = upper_32_bits(address);
944 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
945 if (s)
946 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
947}
948
Joerg Roedel22e266c2011-11-21 15:59:08 +0100949static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
950 u64 address, bool size)
951{
952 memset(cmd, 0, sizeof(*cmd));
953
954 address &= ~(0xfffULL);
955
956 cmd->data[0] = pasid & PASID_MASK;
957 cmd->data[1] = domid;
958 cmd->data[2] = lower_32_bits(address);
959 cmd->data[3] = upper_32_bits(address);
960 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
961 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
962 if (size)
963 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
964 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
965}
966
967static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
968 int qdep, u64 address, bool size)
969{
970 memset(cmd, 0, sizeof(*cmd));
971
972 address &= ~(0xfffULL);
973
974 cmd->data[0] = devid;
975 cmd->data[0] |= (pasid & 0xff) << 16;
976 cmd->data[0] |= (qdep & 0xff) << 24;
977 cmd->data[1] = devid;
978 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
979 cmd->data[2] = lower_32_bits(address);
980 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
981 cmd->data[3] = upper_32_bits(address);
982 if (size)
983 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
984 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
985}
986
Joerg Roedelc99afa22011-11-21 18:19:25 +0100987static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
988 int status, int tag, bool gn)
989{
990 memset(cmd, 0, sizeof(*cmd));
991
992 cmd->data[0] = devid;
993 if (gn) {
994 cmd->data[1] = pasid & PASID_MASK;
995 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
996 }
997 cmd->data[3] = tag & 0x1ff;
998 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
999
1000 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1001}
1002
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001003static void build_inv_all(struct iommu_cmd *cmd)
1004{
1005 memset(cmd, 0, sizeof(*cmd));
1006 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001007}
1008
Joerg Roedel7ef27982012-06-21 16:46:04 +02001009static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1010{
1011 memset(cmd, 0, sizeof(*cmd));
1012 cmd->data[0] = devid;
1013 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1014}
1015
Joerg Roedel431b2a22008-07-11 17:14:22 +02001016/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001017 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001018 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001019 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001020static int iommu_queue_command_sync(struct amd_iommu *iommu,
1021 struct iommu_cmd *cmd,
1022 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001023{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001024 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +02001025 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001026
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001027 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +01001028
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001029again:
Joerg Roedel815b33f2011-04-06 17:26:49 +02001030 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001031
1032 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1033 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
1034 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
1035 left = (head - next_tail) % iommu->cmd_buf_size;
1036
1037 if (left <= 2) {
1038 struct iommu_cmd sync_cmd;
1039 volatile u64 sem = 0;
1040 int ret;
1041
1042 build_completion_wait(&sync_cmd, (u64)&sem);
1043 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1044
1045 spin_unlock_irqrestore(&iommu->lock, flags);
1046
1047 if ((ret = wait_on_sem(&sem)) != 0)
1048 return ret;
1049
1050 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001051 }
1052
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001053 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001054
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001055 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001056 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001057
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001058 spin_unlock_irqrestore(&iommu->lock, flags);
1059
Joerg Roedel815b33f2011-04-06 17:26:49 +02001060 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001061}
1062
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001063static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1064{
1065 return iommu_queue_command_sync(iommu, cmd, true);
1066}
1067
Joerg Roedel8d201962008-12-02 20:34:41 +01001068/*
1069 * This function queues a completion wait command into the command
1070 * buffer of an IOMMU
1071 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001072static int iommu_completion_wait(struct amd_iommu *iommu)
1073{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001074 struct iommu_cmd cmd;
1075 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001076 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001077
1078 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001079 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001080
Joerg Roedel815b33f2011-04-06 17:26:49 +02001081 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001082
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001083 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001084 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001085 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001086
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001087 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001088}
1089
Joerg Roedeld8c13082011-04-06 18:51:26 +02001090static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001091{
1092 struct iommu_cmd cmd;
1093
Joerg Roedeld8c13082011-04-06 18:51:26 +02001094 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001095
Joerg Roedeld8c13082011-04-06 18:51:26 +02001096 return iommu_queue_command(iommu, &cmd);
1097}
1098
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001099static void iommu_flush_dte_all(struct amd_iommu *iommu)
1100{
1101 u32 devid;
1102
1103 for (devid = 0; devid <= 0xffff; ++devid)
1104 iommu_flush_dte(iommu, devid);
1105
1106 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001107}
1108
1109/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001110 * This function uses heavy locking and may disable irqs for some time. But
1111 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001112 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001113static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001114{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001115 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001116
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001117 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1118 struct iommu_cmd cmd;
1119 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1120 dom_id, 1);
1121 iommu_queue_command(iommu, &cmd);
1122 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001123
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001124 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001125}
1126
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001127static void iommu_flush_all(struct amd_iommu *iommu)
1128{
1129 struct iommu_cmd cmd;
1130
1131 build_inv_all(&cmd);
1132
1133 iommu_queue_command(iommu, &cmd);
1134 iommu_completion_wait(iommu);
1135}
1136
Joerg Roedel7ef27982012-06-21 16:46:04 +02001137static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1138{
1139 struct iommu_cmd cmd;
1140
1141 build_inv_irt(&cmd, devid);
1142
1143 iommu_queue_command(iommu, &cmd);
1144}
1145
1146static void iommu_flush_irt_all(struct amd_iommu *iommu)
1147{
1148 u32 devid;
1149
1150 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1151 iommu_flush_irt(iommu, devid);
1152
1153 iommu_completion_wait(iommu);
1154}
1155
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001156void iommu_flush_all_caches(struct amd_iommu *iommu)
1157{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001158 if (iommu_feature(iommu, FEATURE_IA)) {
1159 iommu_flush_all(iommu);
1160 } else {
1161 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001162 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001163 iommu_flush_tlb_all(iommu);
1164 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001165}
1166
Joerg Roedel431b2a22008-07-11 17:14:22 +02001167/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001168 * Command send function for flushing on-device TLB
1169 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001170static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1171 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001172{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001173 struct amd_iommu *iommu;
1174 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001175 int qdep;
1176
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001177 qdep = dev_data->ats.qdep;
1178 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001179
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001180 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001181
1182 return iommu_queue_command(iommu, &cmd);
1183}
1184
1185/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001186 * Command send function for invalidating a device table entry
1187 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001188static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001189{
1190 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001191 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001192
Joerg Roedel6c542042011-06-09 17:07:31 +02001193 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001194
Joerg Roedelf62dda62011-06-09 12:55:35 +02001195 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001196 if (ret)
1197 return ret;
1198
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001199 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001200 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001201
1202 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001203}
1204
Joerg Roedel431b2a22008-07-11 17:14:22 +02001205/*
1206 * TLB invalidation function which is called from the mapping functions.
1207 * It invalidates a single PTE if the range to flush is within a single
1208 * page. Otherwise it flushes the whole TLB of the IOMMU.
1209 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001210static void __domain_flush_pages(struct protection_domain *domain,
1211 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001212{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001213 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001214 struct iommu_cmd cmd;
1215 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001216
Joerg Roedel11b64022011-04-06 11:49:28 +02001217 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001218
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001219 for (i = 0; i < amd_iommus_present; ++i) {
1220 if (!domain->dev_iommu[i])
1221 continue;
1222
1223 /*
1224 * Devices of this domain are behind this IOMMU
1225 * We need a TLB flush
1226 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001227 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001228 }
1229
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001230 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001231
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001232 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001233 continue;
1234
Joerg Roedel6c542042011-06-09 17:07:31 +02001235 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001236 }
1237
Joerg Roedel11b64022011-04-06 11:49:28 +02001238 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001239}
1240
Joerg Roedel17b124b2011-04-06 18:01:35 +02001241static void domain_flush_pages(struct protection_domain *domain,
1242 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001243{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001244 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001245}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001246
Joerg Roedel1c655772008-09-04 18:40:05 +02001247/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001248static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001249{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001250 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001251}
1252
Chris Wright42a49f92009-06-15 15:42:00 +02001253/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001254static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001255{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001256 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1257}
1258
1259static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001260{
1261 int i;
1262
1263 for (i = 0; i < amd_iommus_present; ++i) {
1264 if (!domain->dev_iommu[i])
1265 continue;
1266
1267 /*
1268 * Devices of this domain are behind this IOMMU
1269 * We need to wait for completion of all commands.
1270 */
1271 iommu_completion_wait(amd_iommus[i]);
1272 }
1273}
1274
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001275
Joerg Roedel43f49602008-12-02 21:01:12 +01001276/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001277 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001278 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001279static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001280{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001281 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001282
1283 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001284 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001285}
1286
Joerg Roedel431b2a22008-07-11 17:14:22 +02001287/****************************************************************************
1288 *
1289 * The functions below are used the create the page table mappings for
1290 * unity mapped regions.
1291 *
1292 ****************************************************************************/
1293
1294/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001295 * This function is used to add another level to an IO page table. Adding
1296 * another level increases the size of the address space by 9 bits to a size up
1297 * to 64 bits.
1298 */
1299static bool increase_address_space(struct protection_domain *domain,
1300 gfp_t gfp)
1301{
1302 u64 *pte;
1303
1304 if (domain->mode == PAGE_MODE_6_LEVEL)
1305 /* address space already 64 bit large */
1306 return false;
1307
1308 pte = (void *)get_zeroed_page(gfp);
1309 if (!pte)
1310 return false;
1311
1312 *pte = PM_LEVEL_PDE(domain->mode,
1313 virt_to_phys(domain->pt_root));
1314 domain->pt_root = pte;
1315 domain->mode += 1;
1316 domain->updated = true;
1317
1318 return true;
1319}
1320
1321static u64 *alloc_pte(struct protection_domain *domain,
1322 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001323 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001324 u64 **pte_page,
1325 gfp_t gfp)
1326{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001327 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001328 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001329
1330 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001331
1332 while (address > PM_LEVEL_SIZE(domain->mode))
1333 increase_address_space(domain, gfp);
1334
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001335 level = domain->mode - 1;
1336 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1337 address = PAGE_SIZE_ALIGN(address, page_size);
1338 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001339
1340 while (level > end_lvl) {
1341 if (!IOMMU_PTE_PRESENT(*pte)) {
1342 page = (u64 *)get_zeroed_page(gfp);
1343 if (!page)
1344 return NULL;
1345 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1346 }
1347
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001348 /* No level skipping support yet */
1349 if (PM_PTE_LEVEL(*pte) != level)
1350 return NULL;
1351
Joerg Roedel308973d2009-11-24 17:43:32 +01001352 level -= 1;
1353
1354 pte = IOMMU_PTE_PAGE(*pte);
1355
1356 if (pte_page && level == end_lvl)
1357 *pte_page = pte;
1358
1359 pte = &pte[PM_LEVEL_INDEX(level, address)];
1360 }
1361
1362 return pte;
1363}
1364
1365/*
1366 * This function checks if there is a PTE for a given dma address. If
1367 * there is one, it returns the pointer to it.
1368 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001369static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001370{
1371 int level;
1372 u64 *pte;
1373
Joerg Roedel24cd7722010-01-19 17:27:39 +01001374 if (address > PM_LEVEL_SIZE(domain->mode))
1375 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001376
Joerg Roedel24cd7722010-01-19 17:27:39 +01001377 level = domain->mode - 1;
1378 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1379
1380 while (level > 0) {
1381
1382 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001383 if (!IOMMU_PTE_PRESENT(*pte))
1384 return NULL;
1385
Joerg Roedel24cd7722010-01-19 17:27:39 +01001386 /* Large PTE */
1387 if (PM_PTE_LEVEL(*pte) == 0x07) {
1388 unsigned long pte_mask, __pte;
1389
1390 /*
1391 * If we have a series of large PTEs, make
1392 * sure to return a pointer to the first one.
1393 */
1394 pte_mask = PTE_PAGE_SIZE(*pte);
1395 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1396 __pte = ((unsigned long)pte) & pte_mask;
1397
1398 return (u64 *)__pte;
1399 }
1400
1401 /* No level skipping support yet */
1402 if (PM_PTE_LEVEL(*pte) != level)
1403 return NULL;
1404
Joerg Roedel308973d2009-11-24 17:43:32 +01001405 level -= 1;
1406
Joerg Roedel24cd7722010-01-19 17:27:39 +01001407 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001408 pte = IOMMU_PTE_PAGE(*pte);
1409 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001410 }
1411
1412 return pte;
1413}
1414
1415/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001416 * Generic mapping functions. It maps a physical address into a DMA
1417 * address space. It allocates the page table pages if necessary.
1418 * In the future it can be extended to a generic mapping function
1419 * supporting all features of AMD IOMMU page tables like level skipping
1420 * and full 64 bit address spaces.
1421 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001422static int iommu_map_page(struct protection_domain *dom,
1423 unsigned long bus_addr,
1424 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001425 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001426 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001427{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001428 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001429 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001430
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001431 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001432 return -EINVAL;
1433
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001434 bus_addr = PAGE_ALIGN(bus_addr);
1435 phys_addr = PAGE_ALIGN(phys_addr);
1436 count = PAGE_SIZE_PTE_COUNT(page_size);
1437 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001438
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001439 for (i = 0; i < count; ++i)
1440 if (IOMMU_PTE_PRESENT(pte[i]))
1441 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001442
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001443 if (page_size > PAGE_SIZE) {
1444 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1445 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1446 } else
1447 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1448
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001449 if (prot & IOMMU_PROT_IR)
1450 __pte |= IOMMU_PTE_IR;
1451 if (prot & IOMMU_PROT_IW)
1452 __pte |= IOMMU_PTE_IW;
1453
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001454 for (i = 0; i < count; ++i)
1455 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001456
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001457 update_domain(dom);
1458
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001459 return 0;
1460}
1461
Joerg Roedel24cd7722010-01-19 17:27:39 +01001462static unsigned long iommu_unmap_page(struct protection_domain *dom,
1463 unsigned long bus_addr,
1464 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001465{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001466 unsigned long long unmap_size, unmapped;
1467 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001468
Joerg Roedel24cd7722010-01-19 17:27:39 +01001469 BUG_ON(!is_power_of_2(page_size));
1470
1471 unmapped = 0;
1472
1473 while (unmapped < page_size) {
1474
1475 pte = fetch_pte(dom, bus_addr);
1476
1477 if (!pte) {
1478 /*
1479 * No PTE for this address
1480 * move forward in 4kb steps
1481 */
1482 unmap_size = PAGE_SIZE;
1483 } else if (PM_PTE_LEVEL(*pte) == 0) {
1484 /* 4kb PTE found for this address */
1485 unmap_size = PAGE_SIZE;
1486 *pte = 0ULL;
1487 } else {
1488 int count, i;
1489
1490 /* Large PTE found which maps this address */
1491 unmap_size = PTE_PAGE_SIZE(*pte);
1492 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1493 for (i = 0; i < count; i++)
1494 pte[i] = 0ULL;
1495 }
1496
1497 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1498 unmapped += unmap_size;
1499 }
1500
1501 BUG_ON(!is_power_of_2(unmapped));
1502
1503 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001504}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001505
Joerg Roedel431b2a22008-07-11 17:14:22 +02001506/*
1507 * This function checks if a specific unity mapping entry is needed for
1508 * this specific IOMMU.
1509 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001510static int iommu_for_unity_map(struct amd_iommu *iommu,
1511 struct unity_map_entry *entry)
1512{
1513 u16 bdf, i;
1514
1515 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1516 bdf = amd_iommu_alias_table[i];
1517 if (amd_iommu_rlookup_table[bdf] == iommu)
1518 return 1;
1519 }
1520
1521 return 0;
1522}
1523
Joerg Roedel431b2a22008-07-11 17:14:22 +02001524/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001525 * This function actually applies the mapping to the page table of the
1526 * dma_ops domain.
1527 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001528static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1529 struct unity_map_entry *e)
1530{
1531 u64 addr;
1532 int ret;
1533
1534 for (addr = e->address_start; addr < e->address_end;
1535 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001536 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001537 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001538 if (ret)
1539 return ret;
1540 /*
1541 * if unity mapping is in aperture range mark the page
1542 * as allocated in the aperture
1543 */
1544 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001545 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001546 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001547 }
1548
1549 return 0;
1550}
1551
Joerg Roedel431b2a22008-07-11 17:14:22 +02001552/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001553 * Init the unity mappings for a specific IOMMU in the system
1554 *
1555 * Basically iterates over all unity mapping entries and applies them to
1556 * the default domain DMA of that IOMMU if necessary.
1557 */
1558static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1559{
1560 struct unity_map_entry *entry;
1561 int ret;
1562
1563 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1564 if (!iommu_for_unity_map(iommu, entry))
1565 continue;
1566 ret = dma_ops_unity_map(iommu->default_dom, entry);
1567 if (ret)
1568 return ret;
1569 }
1570
1571 return 0;
1572}
1573
1574/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001575 * Inits the unity mappings required for a specific device
1576 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001577static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1578 u16 devid)
1579{
1580 struct unity_map_entry *e;
1581 int ret;
1582
1583 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1584 if (!(devid >= e->devid_start && devid <= e->devid_end))
1585 continue;
1586 ret = dma_ops_unity_map(dma_dom, e);
1587 if (ret)
1588 return ret;
1589 }
1590
1591 return 0;
1592}
1593
Joerg Roedel431b2a22008-07-11 17:14:22 +02001594/****************************************************************************
1595 *
1596 * The next functions belong to the address allocator for the dma_ops
1597 * interface functions. They work like the allocators in the other IOMMU
1598 * drivers. Its basically a bitmap which marks the allocated pages in
1599 * the aperture. Maybe it could be enhanced in the future to a more
1600 * efficient allocator.
1601 *
1602 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001603
Joerg Roedel431b2a22008-07-11 17:14:22 +02001604/*
Joerg Roedel384de722009-05-15 12:30:05 +02001605 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001606 *
1607 * called with domain->lock held
1608 */
Joerg Roedel384de722009-05-15 12:30:05 +02001609
Joerg Roedel9cabe892009-05-18 16:38:55 +02001610/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001611 * Used to reserve address ranges in the aperture (e.g. for exclusion
1612 * ranges.
1613 */
1614static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1615 unsigned long start_page,
1616 unsigned int pages)
1617{
1618 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1619
1620 if (start_page + pages > last_page)
1621 pages = last_page - start_page;
1622
1623 for (i = start_page; i < start_page + pages; ++i) {
1624 int index = i / APERTURE_RANGE_PAGES;
1625 int page = i % APERTURE_RANGE_PAGES;
1626 __set_bit(page, dom->aperture[index]->bitmap);
1627 }
1628}
1629
1630/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001631 * This function is used to add a new aperture range to an existing
1632 * aperture in case of dma_ops domain allocation or address allocation
1633 * failure.
1634 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001635static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001636 bool populate, gfp_t gfp)
1637{
1638 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001639 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001640 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001641
Joerg Roedelf5e97052009-05-22 12:31:53 +02001642#ifdef CONFIG_IOMMU_STRESS
1643 populate = false;
1644#endif
1645
Joerg Roedel9cabe892009-05-18 16:38:55 +02001646 if (index >= APERTURE_MAX_RANGES)
1647 return -ENOMEM;
1648
1649 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1650 if (!dma_dom->aperture[index])
1651 return -ENOMEM;
1652
1653 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1654 if (!dma_dom->aperture[index]->bitmap)
1655 goto out_free;
1656
1657 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1658
1659 if (populate) {
1660 unsigned long address = dma_dom->aperture_size;
1661 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1662 u64 *pte, *pte_page;
1663
1664 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001665 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001666 &pte_page, gfp);
1667 if (!pte)
1668 goto out_free;
1669
1670 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1671
1672 address += APERTURE_RANGE_SIZE / 64;
1673 }
1674 }
1675
Joerg Roedel17f5b562011-07-06 17:14:44 +02001676 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001677 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1678
Joerg Roedel17f5b562011-07-06 17:14:44 +02001679 /* Reserve address range used for MSI messages */
1680 if (old_size < MSI_ADDR_BASE_LO &&
1681 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1682 unsigned long spage;
1683 int pages;
1684
1685 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1686 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1687
1688 dma_ops_reserve_addresses(dma_dom, spage, pages);
1689 }
1690
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001691 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001692 for_each_iommu(iommu) {
1693 if (iommu->exclusion_start &&
1694 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1695 && iommu->exclusion_start < dma_dom->aperture_size) {
1696 unsigned long startpage;
1697 int pages = iommu_num_pages(iommu->exclusion_start,
1698 iommu->exclusion_length,
1699 PAGE_SIZE);
1700 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1701 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1702 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001703 }
1704
1705 /*
1706 * Check for areas already mapped as present in the new aperture
1707 * range and mark those pages as reserved in the allocator. Such
1708 * mappings may already exist as a result of requested unity
1709 * mappings for devices.
1710 */
1711 for (i = dma_dom->aperture[index]->offset;
1712 i < dma_dom->aperture_size;
1713 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001714 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001715 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1716 continue;
1717
Joerg Roedelfcd08612011-10-11 17:41:32 +02001718 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001719 }
1720
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001721 update_domain(&dma_dom->domain);
1722
Joerg Roedel9cabe892009-05-18 16:38:55 +02001723 return 0;
1724
1725out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001726 update_domain(&dma_dom->domain);
1727
Joerg Roedel9cabe892009-05-18 16:38:55 +02001728 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1729
1730 kfree(dma_dom->aperture[index]);
1731 dma_dom->aperture[index] = NULL;
1732
1733 return -ENOMEM;
1734}
1735
Joerg Roedel384de722009-05-15 12:30:05 +02001736static unsigned long dma_ops_area_alloc(struct device *dev,
1737 struct dma_ops_domain *dom,
1738 unsigned int pages,
1739 unsigned long align_mask,
1740 u64 dma_mask,
1741 unsigned long start)
1742{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001743 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001744 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1745 int i = start >> APERTURE_RANGE_SHIFT;
1746 unsigned long boundary_size;
1747 unsigned long address = -1;
1748 unsigned long limit;
1749
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001750 next_bit >>= PAGE_SHIFT;
1751
Joerg Roedel384de722009-05-15 12:30:05 +02001752 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1753 PAGE_SIZE) >> PAGE_SHIFT;
1754
1755 for (;i < max_index; ++i) {
1756 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1757
1758 if (dom->aperture[i]->offset >= dma_mask)
1759 break;
1760
1761 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1762 dma_mask >> PAGE_SHIFT);
1763
1764 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1765 limit, next_bit, pages, 0,
1766 boundary_size, align_mask);
1767 if (address != -1) {
1768 address = dom->aperture[i]->offset +
1769 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001770 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001771 break;
1772 }
1773
1774 next_bit = 0;
1775 }
1776
1777 return address;
1778}
1779
Joerg Roedeld3086442008-06-26 21:27:57 +02001780static unsigned long dma_ops_alloc_addresses(struct device *dev,
1781 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001782 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001783 unsigned long align_mask,
1784 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001785{
Joerg Roedeld3086442008-06-26 21:27:57 +02001786 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001787
Joerg Roedelfe16f082009-05-22 12:27:53 +02001788#ifdef CONFIG_IOMMU_STRESS
1789 dom->next_address = 0;
1790 dom->need_flush = true;
1791#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001792
Joerg Roedel384de722009-05-15 12:30:05 +02001793 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001794 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001795
Joerg Roedel1c655772008-09-04 18:40:05 +02001796 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001797 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001798 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1799 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001800 dom->need_flush = true;
1801 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001802
Joerg Roedel384de722009-05-15 12:30:05 +02001803 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001804 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001805
1806 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1807
1808 return address;
1809}
1810
Joerg Roedel431b2a22008-07-11 17:14:22 +02001811/*
1812 * The address free function.
1813 *
1814 * called with domain->lock held
1815 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001816static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1817 unsigned long address,
1818 unsigned int pages)
1819{
Joerg Roedel384de722009-05-15 12:30:05 +02001820 unsigned i = address >> APERTURE_RANGE_SHIFT;
1821 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001822
Joerg Roedel384de722009-05-15 12:30:05 +02001823 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1824
Joerg Roedel47bccd62009-05-22 12:40:54 +02001825#ifdef CONFIG_IOMMU_STRESS
1826 if (i < 4)
1827 return;
1828#endif
1829
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001830 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001831 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001832
1833 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001834
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001835 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001836
Joerg Roedeld3086442008-06-26 21:27:57 +02001837}
1838
Joerg Roedel431b2a22008-07-11 17:14:22 +02001839/****************************************************************************
1840 *
1841 * The next functions belong to the domain allocation. A domain is
1842 * allocated for every IOMMU as the default domain. If device isolation
1843 * is enabled, every device get its own domain. The most important thing
1844 * about domains is the page table mapping the DMA address space they
1845 * contain.
1846 *
1847 ****************************************************************************/
1848
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001849/*
1850 * This function adds a protection domain to the global protection domain list
1851 */
1852static void add_domain_to_list(struct protection_domain *domain)
1853{
1854 unsigned long flags;
1855
1856 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1857 list_add(&domain->list, &amd_iommu_pd_list);
1858 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1859}
1860
1861/*
1862 * This function removes a protection domain to the global
1863 * protection domain list
1864 */
1865static void del_domain_from_list(struct protection_domain *domain)
1866{
1867 unsigned long flags;
1868
1869 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1870 list_del(&domain->list);
1871 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1872}
1873
Joerg Roedelec487d12008-06-26 21:27:58 +02001874static u16 domain_id_alloc(void)
1875{
1876 unsigned long flags;
1877 int id;
1878
1879 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1880 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1881 BUG_ON(id == 0);
1882 if (id > 0 && id < MAX_DOMAIN_ID)
1883 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1884 else
1885 id = 0;
1886 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1887
1888 return id;
1889}
1890
Joerg Roedela2acfb72008-12-02 18:28:53 +01001891static void domain_id_free(int id)
1892{
1893 unsigned long flags;
1894
1895 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1896 if (id > 0 && id < MAX_DOMAIN_ID)
1897 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1898 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1899}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001900
Joerg Roedel86db2e52008-12-02 18:20:21 +01001901static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001902{
1903 int i, j;
1904 u64 *p1, *p2, *p3;
1905
Joerg Roedel86db2e52008-12-02 18:20:21 +01001906 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001907
1908 if (!p1)
1909 return;
1910
1911 for (i = 0; i < 512; ++i) {
1912 if (!IOMMU_PTE_PRESENT(p1[i]))
1913 continue;
1914
1915 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001916 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001917 if (!IOMMU_PTE_PRESENT(p2[j]))
1918 continue;
1919 p3 = IOMMU_PTE_PAGE(p2[j]);
1920 free_page((unsigned long)p3);
1921 }
1922
1923 free_page((unsigned long)p2);
1924 }
1925
1926 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001927
1928 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001929}
1930
Joerg Roedelb16137b2011-11-21 16:50:23 +01001931static void free_gcr3_tbl_level1(u64 *tbl)
1932{
1933 u64 *ptr;
1934 int i;
1935
1936 for (i = 0; i < 512; ++i) {
1937 if (!(tbl[i] & GCR3_VALID))
1938 continue;
1939
1940 ptr = __va(tbl[i] & PAGE_MASK);
1941
1942 free_page((unsigned long)ptr);
1943 }
1944}
1945
1946static void free_gcr3_tbl_level2(u64 *tbl)
1947{
1948 u64 *ptr;
1949 int i;
1950
1951 for (i = 0; i < 512; ++i) {
1952 if (!(tbl[i] & GCR3_VALID))
1953 continue;
1954
1955 ptr = __va(tbl[i] & PAGE_MASK);
1956
1957 free_gcr3_tbl_level1(ptr);
1958 }
1959}
1960
Joerg Roedel52815b72011-11-17 17:24:28 +01001961static void free_gcr3_table(struct protection_domain *domain)
1962{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001963 if (domain->glx == 2)
1964 free_gcr3_tbl_level2(domain->gcr3_tbl);
1965 else if (domain->glx == 1)
1966 free_gcr3_tbl_level1(domain->gcr3_tbl);
1967 else if (domain->glx != 0)
1968 BUG();
1969
Joerg Roedel52815b72011-11-17 17:24:28 +01001970 free_page((unsigned long)domain->gcr3_tbl);
1971}
1972
Joerg Roedel431b2a22008-07-11 17:14:22 +02001973/*
1974 * Free a domain, only used if something went wrong in the
1975 * allocation path and we need to free an already allocated page table
1976 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001977static void dma_ops_domain_free(struct dma_ops_domain *dom)
1978{
Joerg Roedel384de722009-05-15 12:30:05 +02001979 int i;
1980
Joerg Roedelec487d12008-06-26 21:27:58 +02001981 if (!dom)
1982 return;
1983
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001984 del_domain_from_list(&dom->domain);
1985
Joerg Roedel86db2e52008-12-02 18:20:21 +01001986 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001987
Joerg Roedel384de722009-05-15 12:30:05 +02001988 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1989 if (!dom->aperture[i])
1990 continue;
1991 free_page((unsigned long)dom->aperture[i]->bitmap);
1992 kfree(dom->aperture[i]);
1993 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001994
1995 kfree(dom);
1996}
1997
Joerg Roedel431b2a22008-07-11 17:14:22 +02001998/*
1999 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002000 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02002001 * structures required for the dma_ops interface
2002 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01002003static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02002004{
2005 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002006
2007 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
2008 if (!dma_dom)
2009 return NULL;
2010
2011 spin_lock_init(&dma_dom->domain.lock);
2012
2013 dma_dom->domain.id = domain_id_alloc();
2014 if (dma_dom->domain.id == 0)
2015 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002016 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02002017 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02002018 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01002019 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02002020 dma_dom->domain.priv = dma_dom;
2021 if (!dma_dom->domain.pt_root)
2022 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002023
Joerg Roedel1c655772008-09-04 18:40:05 +02002024 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02002025 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02002026
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002027 add_domain_to_list(&dma_dom->domain);
2028
Joerg Roedel576175c2009-11-23 19:08:46 +01002029 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02002030 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02002031
Joerg Roedel431b2a22008-07-11 17:14:22 +02002032 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02002033 * mark the first page as allocated so we never return 0 as
2034 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02002035 */
Joerg Roedel384de722009-05-15 12:30:05 +02002036 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02002037 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02002038
Joerg Roedelec487d12008-06-26 21:27:58 +02002039
2040 return dma_dom;
2041
2042free_dma_dom:
2043 dma_ops_domain_free(dma_dom);
2044
2045 return NULL;
2046}
2047
Joerg Roedel431b2a22008-07-11 17:14:22 +02002048/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01002049 * little helper function to check whether a given protection domain is a
2050 * dma_ops domain
2051 */
2052static bool dma_ops_domain(struct protection_domain *domain)
2053{
2054 return domain->flags & PD_DMA_OPS_MASK;
2055}
2056
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002057static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002058{
Joerg Roedel132bd682011-11-17 14:18:46 +01002059 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01002060 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01002061
Joerg Roedel132bd682011-11-17 14:18:46 +01002062 if (domain->mode != PAGE_MODE_NONE)
2063 pte_root = virt_to_phys(domain->pt_root);
2064
Joerg Roedel38ddf412008-09-11 10:38:32 +02002065 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
2066 << DEV_ENTRY_MODE_SHIFT;
2067 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002068
Joerg Roedelee6c2862011-11-09 12:06:03 +01002069 flags = amd_iommu_dev_table[devid].data[1];
2070
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002071 if (ats)
2072 flags |= DTE_FLAG_IOTLB;
2073
Joerg Roedel52815b72011-11-17 17:24:28 +01002074 if (domain->flags & PD_IOMMUV2_MASK) {
2075 u64 gcr3 = __pa(domain->gcr3_tbl);
2076 u64 glx = domain->glx;
2077 u64 tmp;
2078
2079 pte_root |= DTE_FLAG_GV;
2080 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2081
2082 /* First mask out possible old values for GCR3 table */
2083 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2084 flags &= ~tmp;
2085
2086 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2087 flags &= ~tmp;
2088
2089 /* Encode GCR3 table into DTE */
2090 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2091 pte_root |= tmp;
2092
2093 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2094 flags |= tmp;
2095
2096 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2097 flags |= tmp;
2098 }
2099
Joerg Roedelee6c2862011-11-09 12:06:03 +01002100 flags &= ~(0xffffUL);
2101 flags |= domain->id;
2102
2103 amd_iommu_dev_table[devid].data[1] = flags;
2104 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002105}
2106
Joerg Roedel15898bb2009-11-24 15:39:42 +01002107static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002108{
Joerg Roedel355bf552008-12-08 12:02:41 +01002109 /* remove entry from the device table seen by the hardware */
2110 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2111 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002112
Joerg Roedelc5cca142009-10-09 18:31:20 +02002113 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002114}
2115
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002116static void do_attach(struct iommu_dev_data *dev_data,
2117 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002118{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002119 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002120 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002121
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002122 iommu = amd_iommu_rlookup_table[dev_data->devid];
2123 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002124
2125 /* Update data structures */
2126 dev_data->domain = domain;
2127 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002128 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002129
2130 /* Do reference counting */
2131 domain->dev_iommu[iommu->index] += 1;
2132 domain->dev_cnt += 1;
2133
2134 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002135 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002136}
2137
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002138static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002139{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002140 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002141
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002142 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002143
Joerg Roedelc4596112009-11-20 14:57:32 +01002144 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002145 dev_data->domain->dev_iommu[iommu->index] -= 1;
2146 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002147
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002148 /* Update data structures */
2149 dev_data->domain = NULL;
2150 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002151 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002152
2153 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002154 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002155}
2156
2157/*
2158 * If a device is not yet associated with a domain, this function does
2159 * assigns it visible for the hardware
2160 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002161static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002162 struct protection_domain *domain)
2163{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002164 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002165
Joerg Roedel15898bb2009-11-24 15:39:42 +01002166 /* lock domain */
2167 spin_lock(&domain->lock);
2168
Joerg Roedel71f77582011-06-09 19:03:15 +02002169 if (dev_data->alias_data != NULL) {
2170 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002171
Joerg Roedel2b02b092011-06-09 17:48:39 +02002172 /* Some sanity checks */
2173 ret = -EBUSY;
2174 if (alias_data->domain != NULL &&
2175 alias_data->domain != domain)
2176 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002177
Joerg Roedel2b02b092011-06-09 17:48:39 +02002178 if (dev_data->domain != NULL &&
2179 dev_data->domain != domain)
2180 goto out_unlock;
2181
2182 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002183 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002184 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002185
2186 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002187 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002188
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002189 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002190 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002191
Joerg Roedel24100052009-11-25 15:59:57 +01002192 atomic_inc(&dev_data->bind);
2193
Julia Lawall84fe6c12010-05-27 12:31:51 +02002194 ret = 0;
2195
2196out_unlock:
2197
Joerg Roedel355bf552008-12-08 12:02:41 +01002198 /* ready */
2199 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002200
Julia Lawall84fe6c12010-05-27 12:31:51 +02002201 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002202}
2203
Joerg Roedel52815b72011-11-17 17:24:28 +01002204
2205static void pdev_iommuv2_disable(struct pci_dev *pdev)
2206{
2207 pci_disable_ats(pdev);
2208 pci_disable_pri(pdev);
2209 pci_disable_pasid(pdev);
2210}
2211
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002212/* FIXME: Change generic reset-function to do the same */
2213static int pri_reset_while_enabled(struct pci_dev *pdev)
2214{
2215 u16 control;
2216 int pos;
2217
Joerg Roedel46277b72011-12-07 14:34:02 +01002218 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002219 if (!pos)
2220 return -EINVAL;
2221
Joerg Roedel46277b72011-12-07 14:34:02 +01002222 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2223 control |= PCI_PRI_CTRL_RESET;
2224 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002225
2226 return 0;
2227}
2228
Joerg Roedel52815b72011-11-17 17:24:28 +01002229static int pdev_iommuv2_enable(struct pci_dev *pdev)
2230{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002231 bool reset_enable;
2232 int reqs, ret;
2233
2234 /* FIXME: Hardcode number of outstanding requests for now */
2235 reqs = 32;
2236 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2237 reqs = 1;
2238 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002239
2240 /* Only allow access to user-accessible pages */
2241 ret = pci_enable_pasid(pdev, 0);
2242 if (ret)
2243 goto out_err;
2244
2245 /* First reset the PRI state of the device */
2246 ret = pci_reset_pri(pdev);
2247 if (ret)
2248 goto out_err;
2249
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002250 /* Enable PRI */
2251 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002252 if (ret)
2253 goto out_err;
2254
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002255 if (reset_enable) {
2256 ret = pri_reset_while_enabled(pdev);
2257 if (ret)
2258 goto out_err;
2259 }
2260
Joerg Roedel52815b72011-11-17 17:24:28 +01002261 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2262 if (ret)
2263 goto out_err;
2264
2265 return 0;
2266
2267out_err:
2268 pci_disable_pri(pdev);
2269 pci_disable_pasid(pdev);
2270
2271 return ret;
2272}
2273
Joerg Roedelc99afa22011-11-21 18:19:25 +01002274/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002275#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002276
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002277static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002278{
Joerg Roedela3b93122012-04-12 12:49:26 +02002279 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002280 int pos;
2281
Joerg Roedel46277b72011-12-07 14:34:02 +01002282 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002283 if (!pos)
2284 return false;
2285
Joerg Roedela3b93122012-04-12 12:49:26 +02002286 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002287
Joerg Roedela3b93122012-04-12 12:49:26 +02002288 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002289}
2290
Joerg Roedel15898bb2009-11-24 15:39:42 +01002291/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002292 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002293 * assigns it visible for the hardware
2294 */
2295static int attach_device(struct device *dev,
2296 struct protection_domain *domain)
2297{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002298 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002299 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002300 unsigned long flags;
2301 int ret;
2302
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002303 dev_data = get_dev_data(dev);
2304
Joerg Roedel52815b72011-11-17 17:24:28 +01002305 if (domain->flags & PD_IOMMUV2_MASK) {
2306 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2307 return -EINVAL;
2308
2309 if (pdev_iommuv2_enable(pdev) != 0)
2310 return -EINVAL;
2311
2312 dev_data->ats.enabled = true;
2313 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002314 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002315 } else if (amd_iommu_iotlb_sup &&
2316 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002317 dev_data->ats.enabled = true;
2318 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2319 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002320
Joerg Roedel15898bb2009-11-24 15:39:42 +01002321 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002322 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002323 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2324
2325 /*
2326 * We might boot into a crash-kernel here. The crashed kernel
2327 * left the caches in the IOMMU dirty. So we have to flush
2328 * here to evict all dirty stuff.
2329 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002330 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002331
2332 return ret;
2333}
2334
2335/*
2336 * Removes a device from a protection domain (unlocked)
2337 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002338static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002339{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002340 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002341 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002342
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002343 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002344
Joerg Roedel2ca76272010-01-22 16:45:31 +01002345 domain = dev_data->domain;
2346
2347 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002348
Joerg Roedel71f77582011-06-09 19:03:15 +02002349 if (dev_data->alias_data != NULL) {
2350 struct iommu_dev_data *alias_data = dev_data->alias_data;
2351
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002352 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002353 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002354 }
2355
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002356 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002357 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002358
Joerg Roedel2ca76272010-01-22 16:45:31 +01002359 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002360
Joerg Roedel21129f72009-09-01 11:59:42 +02002361 /*
2362 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002363 * passthrough domain if it is detached from any other domain.
2364 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002365 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002366 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002367 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002368 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002369}
2370
2371/*
2372 * Removes a device from a protection domain (with devtable_lock held)
2373 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002374static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002375{
Joerg Roedel52815b72011-11-17 17:24:28 +01002376 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002377 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002378 unsigned long flags;
2379
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002380 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002381 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002382
Joerg Roedel355bf552008-12-08 12:02:41 +01002383 /* lock device table */
2384 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002385 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002386 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002387
Joerg Roedel52815b72011-11-17 17:24:28 +01002388 if (domain->flags & PD_IOMMUV2_MASK)
2389 pdev_iommuv2_disable(to_pci_dev(dev));
2390 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002391 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002392
2393 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002394}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002395
Joerg Roedel15898bb2009-11-24 15:39:42 +01002396/*
2397 * Find out the protection domain structure for a given PCI device. This
2398 * will give us the pointer to the page table root for example.
2399 */
2400static struct protection_domain *domain_for_device(struct device *dev)
2401{
Joerg Roedel71f77582011-06-09 19:03:15 +02002402 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002403 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002404 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002405
Joerg Roedel657cbb62009-11-23 15:26:46 +01002406 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002407
Joerg Roedel2b02b092011-06-09 17:48:39 +02002408 if (dev_data->domain)
2409 return dev_data->domain;
2410
Joerg Roedel71f77582011-06-09 19:03:15 +02002411 if (dev_data->alias_data != NULL) {
2412 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002413
2414 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2415 if (alias_data->domain != NULL) {
2416 __attach_device(dev_data, alias_data->domain);
2417 dom = alias_data->domain;
2418 }
2419 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002420 }
2421
Joerg Roedel15898bb2009-11-24 15:39:42 +01002422 return dom;
2423}
2424
Joerg Roedele275a2a2008-12-10 18:27:25 +01002425static int device_change_notifier(struct notifier_block *nb,
2426 unsigned long action, void *data)
2427{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002428 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002429 struct protection_domain *domain;
2430 struct iommu_dev_data *dev_data;
2431 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002432 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002433 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002434 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002435
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002436 if (!check_device(dev))
2437 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002438
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002439 devid = get_device_id(dev);
2440 iommu = amd_iommu_rlookup_table[devid];
2441 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002442
2443 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002444 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002445
2446 domain = domain_for_device(dev);
2447
Joerg Roedele275a2a2008-12-10 18:27:25 +01002448 if (!domain)
2449 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002450 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002451 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002452 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002453 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002454 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002455
2456 iommu_init_device(dev);
2457
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002458 /*
2459 * dev_data is still NULL and
2460 * got initialized in iommu_init_device
2461 */
2462 dev_data = get_dev_data(dev);
2463
2464 if (iommu_pass_through || dev_data->iommu_v2) {
2465 dev_data->passthrough = true;
2466 attach_device(dev, pt_domain);
2467 break;
2468 }
2469
Joerg Roedel657cbb62009-11-23 15:26:46 +01002470 domain = domain_for_device(dev);
2471
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002472 /* allocate a protection domain if a device is added */
2473 dma_domain = find_protection_domain(devid);
Joerg Roedelc2a28762013-03-26 22:48:23 +01002474 if (!dma_domain) {
2475 dma_domain = dma_ops_domain_alloc();
2476 if (!dma_domain)
2477 goto out;
2478 dma_domain->target_dev = devid;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002479
Joerg Roedelc2a28762013-03-26 22:48:23 +01002480 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2481 list_add_tail(&dma_domain->list, &iommu_pd_list);
2482 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2483 }
Joerg Roedelac1534a2012-06-21 14:52:40 +02002484
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002485 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002486
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002487 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002488 case BUS_NOTIFY_DEL_DEVICE:
2489
2490 iommu_uninit_device(dev);
2491
Joerg Roedele275a2a2008-12-10 18:27:25 +01002492 default:
2493 goto out;
2494 }
2495
Joerg Roedele275a2a2008-12-10 18:27:25 +01002496 iommu_completion_wait(iommu);
2497
2498out:
2499 return 0;
2500}
2501
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302502static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002503 .notifier_call = device_change_notifier,
2504};
Joerg Roedel355bf552008-12-08 12:02:41 +01002505
Joerg Roedel8638c492009-12-10 11:12:25 +01002506void amd_iommu_init_notifier(void)
2507{
2508 bus_register_notifier(&pci_bus_type, &device_nb);
2509}
2510
Joerg Roedel431b2a22008-07-11 17:14:22 +02002511/*****************************************************************************
2512 *
2513 * The next functions belong to the dma_ops mapping/unmapping code.
2514 *
2515 *****************************************************************************/
2516
2517/*
2518 * In the dma_ops path we only have the struct device. This function
2519 * finds the corresponding IOMMU, the protection domain and the
2520 * requestor id for a given device.
2521 * If the device is not yet associated with a domain this is also done
2522 * in this function.
2523 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002524static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002525{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002526 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002527 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002528 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002529
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002530 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002531 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002532
Joerg Roedel94f6d192009-11-24 16:40:02 +01002533 domain = domain_for_device(dev);
2534 if (domain != NULL && !dma_ops_domain(domain))
2535 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002536
Joerg Roedel94f6d192009-11-24 16:40:02 +01002537 if (domain != NULL)
2538 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002539
Frank Arnolddf805ab2012-08-27 19:21:04 +02002540 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002541 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002542 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002543 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2544 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002545 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002546 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002547
Joerg Roedel94f6d192009-11-24 16:40:02 +01002548 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002549}
2550
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002551static void update_device_table(struct protection_domain *domain)
2552{
Joerg Roedel492667d2009-11-27 13:25:47 +01002553 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002554
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002555 list_for_each_entry(dev_data, &domain->dev_list, list)
2556 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002557}
2558
2559static void update_domain(struct protection_domain *domain)
2560{
2561 if (!domain->updated)
2562 return;
2563
2564 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002565
2566 domain_flush_devices(domain);
2567 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002568
2569 domain->updated = false;
2570}
2571
Joerg Roedel431b2a22008-07-11 17:14:22 +02002572/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002573 * This function fetches the PTE for a given address in the aperture
2574 */
2575static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2576 unsigned long address)
2577{
Joerg Roedel384de722009-05-15 12:30:05 +02002578 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002579 u64 *pte, *pte_page;
2580
Joerg Roedel384de722009-05-15 12:30:05 +02002581 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2582 if (!aperture)
2583 return NULL;
2584
2585 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002586 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002587 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002588 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002589 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2590 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002591 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002592
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002593 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002594
2595 return pte;
2596}
2597
2598/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002599 * This is the generic map function. It maps one 4kb page at paddr to
2600 * the given address in the DMA address space for the domain.
2601 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002602static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002603 unsigned long address,
2604 phys_addr_t paddr,
2605 int direction)
2606{
2607 u64 *pte, __pte;
2608
2609 WARN_ON(address > dom->aperture_size);
2610
2611 paddr &= PAGE_MASK;
2612
Joerg Roedel8bda3092009-05-12 12:02:46 +02002613 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002614 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002615 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002616
2617 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2618
2619 if (direction == DMA_TO_DEVICE)
2620 __pte |= IOMMU_PTE_IR;
2621 else if (direction == DMA_FROM_DEVICE)
2622 __pte |= IOMMU_PTE_IW;
2623 else if (direction == DMA_BIDIRECTIONAL)
2624 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2625
2626 WARN_ON(*pte);
2627
2628 *pte = __pte;
2629
2630 return (dma_addr_t)address;
2631}
2632
Joerg Roedel431b2a22008-07-11 17:14:22 +02002633/*
2634 * The generic unmapping function for on page in the DMA address space.
2635 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002636static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002637 unsigned long address)
2638{
Joerg Roedel384de722009-05-15 12:30:05 +02002639 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002640 u64 *pte;
2641
2642 if (address >= dom->aperture_size)
2643 return;
2644
Joerg Roedel384de722009-05-15 12:30:05 +02002645 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2646 if (!aperture)
2647 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002648
Joerg Roedel384de722009-05-15 12:30:05 +02002649 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2650 if (!pte)
2651 return;
2652
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002653 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002654
2655 WARN_ON(!*pte);
2656
2657 *pte = 0ULL;
2658}
2659
Joerg Roedel431b2a22008-07-11 17:14:22 +02002660/*
2661 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002662 * contiguous memory region into DMA address space. It is used by all
2663 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002664 * Must be called with the domain lock held.
2665 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002666static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002667 struct dma_ops_domain *dma_dom,
2668 phys_addr_t paddr,
2669 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002670 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002671 bool align,
2672 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002673{
2674 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002675 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002676 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002677 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002678 int i;
2679
Joerg Roedele3c449f2008-10-15 22:02:11 -07002680 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002681 paddr &= PAGE_MASK;
2682
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002683 INC_STATS_COUNTER(total_map_requests);
2684
Joerg Roedelc1858972008-12-12 15:42:39 +01002685 if (pages > 1)
2686 INC_STATS_COUNTER(cross_page);
2687
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002688 if (align)
2689 align_mask = (1UL << get_order(size)) - 1;
2690
Joerg Roedel11b83882009-05-19 10:23:15 +02002691retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002692 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2693 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002694 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002695 /*
2696 * setting next_address here will let the address
2697 * allocator only scan the new allocated range in the
2698 * first run. This is a small optimization.
2699 */
2700 dma_dom->next_address = dma_dom->aperture_size;
2701
Joerg Roedel576175c2009-11-23 19:08:46 +01002702 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002703 goto out;
2704
2705 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002706 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002707 * allocation again
2708 */
2709 goto retry;
2710 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002711
2712 start = address;
2713 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002714 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002715 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002716 goto out_unmap;
2717
Joerg Roedelcb76c322008-06-26 21:28:00 +02002718 paddr += PAGE_SIZE;
2719 start += PAGE_SIZE;
2720 }
2721 address += offset;
2722
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002723 ADD_STATS_COUNTER(alloced_io_mem, size);
2724
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002725 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002726 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002727 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002728 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002729 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002730
Joerg Roedelcb76c322008-06-26 21:28:00 +02002731out:
2732 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002733
2734out_unmap:
2735
2736 for (--i; i >= 0; --i) {
2737 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002738 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002739 }
2740
2741 dma_ops_free_addresses(dma_dom, address, pages);
2742
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002743 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002744}
2745
Joerg Roedel431b2a22008-07-11 17:14:22 +02002746/*
2747 * Does the reverse of the __map_single function. Must be called with
2748 * the domain lock held too
2749 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002750static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002751 dma_addr_t dma_addr,
2752 size_t size,
2753 int dir)
2754{
Joerg Roedel04e04632010-09-23 16:12:48 +02002755 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002756 dma_addr_t i, start;
2757 unsigned int pages;
2758
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002759 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002760 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002761 return;
2762
Joerg Roedel04e04632010-09-23 16:12:48 +02002763 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002764 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002765 dma_addr &= PAGE_MASK;
2766 start = dma_addr;
2767
2768 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002769 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002770 start += PAGE_SIZE;
2771 }
2772
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002773 SUB_STATS_COUNTER(alloced_io_mem, size);
2774
Joerg Roedelcb76c322008-06-26 21:28:00 +02002775 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002776
Joerg Roedel80be3082008-11-06 14:59:05 +01002777 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002778 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002779 dma_dom->need_flush = false;
2780 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002781}
2782
Joerg Roedel431b2a22008-07-11 17:14:22 +02002783/*
2784 * The exported map_single function for dma_ops.
2785 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002786static dma_addr_t map_page(struct device *dev, struct page *page,
2787 unsigned long offset, size_t size,
2788 enum dma_data_direction dir,
2789 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002790{
2791 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002792 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002793 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002794 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002795 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002796
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002797 INC_STATS_COUNTER(cnt_map_single);
2798
Joerg Roedel94f6d192009-11-24 16:40:02 +01002799 domain = get_domain(dev);
2800 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002801 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002802 else if (IS_ERR(domain))
2803 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002804
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002805 dma_mask = *dev->dma_mask;
2806
Joerg Roedel4da70b92008-06-26 21:28:01 +02002807 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002808
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002809 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002810 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002811 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002812 goto out;
2813
Joerg Roedel17b124b2011-04-06 18:01:35 +02002814 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002815
2816out:
2817 spin_unlock_irqrestore(&domain->lock, flags);
2818
2819 return addr;
2820}
2821
Joerg Roedel431b2a22008-07-11 17:14:22 +02002822/*
2823 * The exported unmap_single function for dma_ops.
2824 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002825static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2826 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002827{
2828 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002829 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002830
Joerg Roedel146a6912008-12-12 15:07:12 +01002831 INC_STATS_COUNTER(cnt_unmap_single);
2832
Joerg Roedel94f6d192009-11-24 16:40:02 +01002833 domain = get_domain(dev);
2834 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002835 return;
2836
Joerg Roedel4da70b92008-06-26 21:28:01 +02002837 spin_lock_irqsave(&domain->lock, flags);
2838
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002839 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002840
Joerg Roedel17b124b2011-04-06 18:01:35 +02002841 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002842
2843 spin_unlock_irqrestore(&domain->lock, flags);
2844}
2845
Joerg Roedel431b2a22008-07-11 17:14:22 +02002846/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002847 * The exported map_sg function for dma_ops (handles scatter-gather
2848 * lists).
2849 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002850static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002851 int nelems, enum dma_data_direction dir,
2852 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002853{
2854 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002855 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002856 int i;
2857 struct scatterlist *s;
2858 phys_addr_t paddr;
2859 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002860 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002861
Joerg Roedeld03f0672008-12-12 15:09:48 +01002862 INC_STATS_COUNTER(cnt_map_sg);
2863
Joerg Roedel94f6d192009-11-24 16:40:02 +01002864 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002865 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002866 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002867
Joerg Roedel832a90c2008-09-18 15:54:23 +02002868 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002869
Joerg Roedel65b050a2008-06-26 21:28:02 +02002870 spin_lock_irqsave(&domain->lock, flags);
2871
2872 for_each_sg(sglist, s, nelems, i) {
2873 paddr = sg_phys(s);
2874
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002875 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002876 paddr, s->length, dir, false,
2877 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002878
2879 if (s->dma_address) {
2880 s->dma_length = s->length;
2881 mapped_elems++;
2882 } else
2883 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002884 }
2885
Joerg Roedel17b124b2011-04-06 18:01:35 +02002886 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002887
2888out:
2889 spin_unlock_irqrestore(&domain->lock, flags);
2890
2891 return mapped_elems;
2892unmap:
2893 for_each_sg(sglist, s, mapped_elems, i) {
2894 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002895 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002896 s->dma_length, dir);
2897 s->dma_address = s->dma_length = 0;
2898 }
2899
2900 mapped_elems = 0;
2901
2902 goto out;
2903}
2904
Joerg Roedel431b2a22008-07-11 17:14:22 +02002905/*
2906 * The exported map_sg function for dma_ops (handles scatter-gather
2907 * lists).
2908 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002909static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002910 int nelems, enum dma_data_direction dir,
2911 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002912{
2913 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002914 struct protection_domain *domain;
2915 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002916 int i;
2917
Joerg Roedel55877a62008-12-12 15:12:14 +01002918 INC_STATS_COUNTER(cnt_unmap_sg);
2919
Joerg Roedel94f6d192009-11-24 16:40:02 +01002920 domain = get_domain(dev);
2921 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002922 return;
2923
Joerg Roedel65b050a2008-06-26 21:28:02 +02002924 spin_lock_irqsave(&domain->lock, flags);
2925
2926 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002927 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002928 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002929 s->dma_address = s->dma_length = 0;
2930 }
2931
Joerg Roedel17b124b2011-04-06 18:01:35 +02002932 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002933
2934 spin_unlock_irqrestore(&domain->lock, flags);
2935}
2936
Joerg Roedel431b2a22008-07-11 17:14:22 +02002937/*
2938 * The exported alloc_coherent function for dma_ops.
2939 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002940static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002941 dma_addr_t *dma_addr, gfp_t flag,
2942 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002943{
2944 unsigned long flags;
2945 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002946 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002947 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002948 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002949
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002950 INC_STATS_COUNTER(cnt_alloc_coherent);
2951
Joerg Roedel94f6d192009-11-24 16:40:02 +01002952 domain = get_domain(dev);
2953 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002954 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2955 *dma_addr = __pa(virt_addr);
2956 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002957 } else if (IS_ERR(domain))
2958 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002959
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002960 dma_mask = dev->coherent_dma_mask;
2961 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2962 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002963
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002964 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2965 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302966 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002967
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002968 paddr = virt_to_phys(virt_addr);
2969
Joerg Roedel832a90c2008-09-18 15:54:23 +02002970 if (!dma_mask)
2971 dma_mask = *dev->dma_mask;
2972
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002973 spin_lock_irqsave(&domain->lock, flags);
2974
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002975 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002976 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002977
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002978 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002979 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002980 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002981 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002982
Joerg Roedel17b124b2011-04-06 18:01:35 +02002983 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002984
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002985 spin_unlock_irqrestore(&domain->lock, flags);
2986
2987 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002988
2989out_free:
2990
2991 free_pages((unsigned long)virt_addr, get_order(size));
2992
2993 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002994}
2995
Joerg Roedel431b2a22008-07-11 17:14:22 +02002996/*
2997 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002998 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002999static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003000 void *virt_addr, dma_addr_t dma_addr,
3001 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003002{
3003 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003004 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003005
Joerg Roedel5d31ee72008-12-12 15:16:38 +01003006 INC_STATS_COUNTER(cnt_free_coherent);
3007
Joerg Roedel94f6d192009-11-24 16:40:02 +01003008 domain = get_domain(dev);
3009 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01003010 goto free_mem;
3011
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003012 spin_lock_irqsave(&domain->lock, flags);
3013
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01003014 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003015
Joerg Roedel17b124b2011-04-06 18:01:35 +02003016 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02003017
3018 spin_unlock_irqrestore(&domain->lock, flags);
3019
3020free_mem:
3021 free_pages((unsigned long)virt_addr, get_order(size));
3022}
3023
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003024/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003025 * This function is called by the DMA layer to find out if we can handle a
3026 * particular device. It is part of the dma_ops.
3027 */
3028static int amd_iommu_dma_supported(struct device *dev, u64 mask)
3029{
Joerg Roedel420aef82009-11-23 16:14:57 +01003030 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003031}
3032
3033/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02003034 * The function for pre-allocating protection domains.
3035 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003036 * If the driver core informs the DMA layer if a driver grabs a device
3037 * we don't need to preallocate the protection domains anymore.
3038 * For now we have to.
3039 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01003040static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003041{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003042 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003043 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003044 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003045 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003046
Chris Wrightd18c69d2010-04-02 18:27:55 -07003047 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003048
3049 /* Do we handle this device? */
3050 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003051 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003052
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003053 dev_data = get_dev_data(&dev->dev);
3054 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3055 /* Make sure passthrough domain is allocated */
3056 alloc_passthrough_domain();
3057 dev_data->passthrough = true;
3058 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02003059 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003060 dev_name(&dev->dev));
3061 }
3062
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003063 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01003064 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003065 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003066
3067 devid = get_device_id(&dev->dev);
3068
Joerg Roedel87a64d52009-11-24 17:26:43 +01003069 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003070 if (!dma_dom)
3071 continue;
3072 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003073 dma_dom->target_dev = devid;
3074
Joerg Roedel15898bb2009-11-24 15:39:42 +01003075 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003076
Joerg Roedelbd60b732008-09-11 10:24:48 +02003077 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003078 }
3079}
3080
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003081static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003082 .alloc = alloc_coherent,
3083 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003084 .map_page = map_page,
3085 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003086 .map_sg = map_sg,
3087 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003088 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003089};
3090
Joerg Roedel27c21272011-05-30 15:56:24 +02003091static unsigned device_dma_ops_init(void)
3092{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003093 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003094 struct pci_dev *pdev = NULL;
3095 unsigned unhandled = 0;
3096
3097 for_each_pci_dev(pdev) {
3098 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003099
3100 iommu_ignore_device(&pdev->dev);
3101
Joerg Roedel27c21272011-05-30 15:56:24 +02003102 unhandled += 1;
3103 continue;
3104 }
3105
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003106 dev_data = get_dev_data(&pdev->dev);
3107
3108 if (!dev_data->passthrough)
3109 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3110 else
3111 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003112 }
3113
3114 return unhandled;
3115}
3116
Joerg Roedel431b2a22008-07-11 17:14:22 +02003117/*
3118 * The function which clues the AMD IOMMU driver into dma_ops.
3119 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003120
3121void __init amd_iommu_init_api(void)
3122{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003123 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003124}
3125
Joerg Roedel6631ee92008-06-26 21:28:05 +02003126int __init amd_iommu_init_dma_ops(void)
3127{
3128 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003129 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003130
Joerg Roedel431b2a22008-07-11 17:14:22 +02003131 /*
3132 * first allocate a default protection domain for every IOMMU we
3133 * found in the system. Devices not assigned to any other
3134 * protection domain will be assigned to the default one.
3135 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003136 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003137 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003138 if (iommu->default_dom == NULL)
3139 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003140 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003141 ret = iommu_init_unity_mappings(iommu);
3142 if (ret)
3143 goto free_domains;
3144 }
3145
Joerg Roedel431b2a22008-07-11 17:14:22 +02003146 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003147 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003148 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003149 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003150
3151 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003152 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003153
Joerg Roedel431b2a22008-07-11 17:14:22 +02003154 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003155 unhandled = device_dma_ops_init();
3156 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3157 /* There are unhandled devices - initialize swiotlb for them */
3158 swiotlb = 1;
3159 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003160
Joerg Roedel7f265082008-12-12 13:50:21 +01003161 amd_iommu_stats_init();
3162
Joerg Roedel62410ee2012-06-12 16:42:43 +02003163 if (amd_iommu_unmap_flush)
3164 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3165 else
3166 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3167
Joerg Roedel6631ee92008-06-26 21:28:05 +02003168 return 0;
3169
3170free_domains:
3171
Joerg Roedel3bd22172009-05-04 15:06:20 +02003172 for_each_iommu(iommu) {
Cyril Roelandt91457df2013-02-12 05:01:50 +01003173 dma_ops_domain_free(iommu->default_dom);
Joerg Roedel6631ee92008-06-26 21:28:05 +02003174 }
3175
3176 return ret;
3177}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003178
3179/*****************************************************************************
3180 *
3181 * The following functions belong to the exported interface of AMD IOMMU
3182 *
3183 * This interface allows access to lower level functions of the IOMMU
3184 * like protection domain handling and assignement of devices to domains
3185 * which is not possible with the dma_ops interface.
3186 *
3187 *****************************************************************************/
3188
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003189static void cleanup_domain(struct protection_domain *domain)
3190{
Joerg Roedel492667d2009-11-27 13:25:47 +01003191 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003192 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003193
3194 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3195
Joerg Roedel492667d2009-11-27 13:25:47 +01003196 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003197 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003198 atomic_set(&dev_data->bind, 0);
3199 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003200
3201 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3202}
3203
Joerg Roedel26508152009-08-26 16:52:40 +02003204static void protection_domain_free(struct protection_domain *domain)
3205{
3206 if (!domain)
3207 return;
3208
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003209 del_domain_from_list(domain);
3210
Joerg Roedel26508152009-08-26 16:52:40 +02003211 if (domain->id)
3212 domain_id_free(domain->id);
3213
3214 kfree(domain);
3215}
3216
3217static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003218{
3219 struct protection_domain *domain;
3220
3221 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3222 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003223 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003224
3225 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003226 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003227 domain->id = domain_id_alloc();
3228 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003229 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003230 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003231
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003232 add_domain_to_list(domain);
3233
Joerg Roedel26508152009-08-26 16:52:40 +02003234 return domain;
3235
3236out_err:
3237 kfree(domain);
3238
3239 return NULL;
3240}
3241
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003242static int __init alloc_passthrough_domain(void)
3243{
3244 if (pt_domain != NULL)
3245 return 0;
3246
3247 /* allocate passthrough domain */
3248 pt_domain = protection_domain_alloc();
3249 if (!pt_domain)
3250 return -ENOMEM;
3251
3252 pt_domain->mode = PAGE_MODE_NONE;
3253
3254 return 0;
3255}
Joerg Roedel26508152009-08-26 16:52:40 +02003256static int amd_iommu_domain_init(struct iommu_domain *dom)
3257{
3258 struct protection_domain *domain;
3259
3260 domain = protection_domain_alloc();
3261 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003262 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003263
3264 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003265 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3266 if (!domain->pt_root)
3267 goto out_free;
3268
Joerg Roedelf3572db2011-11-23 12:36:25 +01003269 domain->iommu_domain = dom;
3270
Joerg Roedelc156e342008-12-02 18:13:27 +01003271 dom->priv = domain;
3272
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003273 dom->geometry.aperture_start = 0;
3274 dom->geometry.aperture_end = ~0ULL;
3275 dom->geometry.force_aperture = true;
3276
Joerg Roedelc156e342008-12-02 18:13:27 +01003277 return 0;
3278
3279out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003280 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003281
3282 return -ENOMEM;
3283}
3284
Joerg Roedel98383fc2008-12-02 18:34:12 +01003285static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3286{
3287 struct protection_domain *domain = dom->priv;
3288
3289 if (!domain)
3290 return;
3291
3292 if (domain->dev_cnt > 0)
3293 cleanup_domain(domain);
3294
3295 BUG_ON(domain->dev_cnt != 0);
3296
Joerg Roedel132bd682011-11-17 14:18:46 +01003297 if (domain->mode != PAGE_MODE_NONE)
3298 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003299
Joerg Roedel52815b72011-11-17 17:24:28 +01003300 if (domain->flags & PD_IOMMUV2_MASK)
3301 free_gcr3_table(domain);
3302
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003303 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003304
3305 dom->priv = NULL;
3306}
3307
Joerg Roedel684f2882008-12-08 12:07:44 +01003308static void amd_iommu_detach_device(struct iommu_domain *dom,
3309 struct device *dev)
3310{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003311 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003312 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003313 u16 devid;
3314
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003315 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003316 return;
3317
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003318 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003319
Joerg Roedel657cbb62009-11-23 15:26:46 +01003320 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003321 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003322
3323 iommu = amd_iommu_rlookup_table[devid];
3324 if (!iommu)
3325 return;
3326
Joerg Roedel684f2882008-12-08 12:07:44 +01003327 iommu_completion_wait(iommu);
3328}
3329
Joerg Roedel01106062008-12-02 19:34:11 +01003330static int amd_iommu_attach_device(struct iommu_domain *dom,
3331 struct device *dev)
3332{
3333 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003334 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003335 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003336 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003337
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003338 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003339 return -EINVAL;
3340
Joerg Roedel657cbb62009-11-23 15:26:46 +01003341 dev_data = dev->archdata.iommu;
3342
Joerg Roedelf62dda62011-06-09 12:55:35 +02003343 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003344 if (!iommu)
3345 return -EINVAL;
3346
Joerg Roedel657cbb62009-11-23 15:26:46 +01003347 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003348 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003349
Joerg Roedel15898bb2009-11-24 15:39:42 +01003350 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003351
3352 iommu_completion_wait(iommu);
3353
Joerg Roedel15898bb2009-11-24 15:39:42 +01003354 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003355}
3356
Joerg Roedel468e2362010-01-21 16:37:36 +01003357static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003358 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003359{
3360 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003361 int prot = 0;
3362 int ret;
3363
Joerg Roedel132bd682011-11-17 14:18:46 +01003364 if (domain->mode == PAGE_MODE_NONE)
3365 return -EINVAL;
3366
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003367 if (iommu_prot & IOMMU_READ)
3368 prot |= IOMMU_PROT_IR;
3369 if (iommu_prot & IOMMU_WRITE)
3370 prot |= IOMMU_PROT_IW;
3371
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003372 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003373 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003374 mutex_unlock(&domain->api_lock);
3375
Joerg Roedel795e74f72010-05-11 17:40:57 +02003376 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003377}
3378
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003379static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3380 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003381{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003382 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003383 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003384
Joerg Roedel132bd682011-11-17 14:18:46 +01003385 if (domain->mode == PAGE_MODE_NONE)
3386 return -EINVAL;
3387
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003388 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003389 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003390 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003391
Joerg Roedel17b124b2011-04-06 18:01:35 +02003392 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003393
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003394 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003395}
3396
Joerg Roedel645c4c82008-12-02 20:05:50 +01003397static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3398 unsigned long iova)
3399{
3400 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003401 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003402 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003403 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003404
Joerg Roedel132bd682011-11-17 14:18:46 +01003405 if (domain->mode == PAGE_MODE_NONE)
3406 return iova;
3407
Joerg Roedel24cd7722010-01-19 17:27:39 +01003408 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003409
Joerg Roedela6d41a42009-09-02 17:08:55 +02003410 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003411 return 0;
3412
Joerg Roedelf03152b2010-01-21 16:15:24 +01003413 if (PM_PTE_LEVEL(*pte) == 0)
3414 offset_mask = PAGE_SIZE - 1;
3415 else
3416 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3417
3418 __pte = *pte & PM_ADDR_MASK;
3419 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003420
3421 return paddr;
3422}
3423
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003424static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3425 unsigned long cap)
3426{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003427 switch (cap) {
3428 case IOMMU_CAP_CACHE_COHERENCY:
3429 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003430 case IOMMU_CAP_INTR_REMAP:
3431 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003432 }
3433
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003434 return 0;
3435}
3436
Joerg Roedel26961ef2008-12-03 17:00:17 +01003437static struct iommu_ops amd_iommu_ops = {
3438 .domain_init = amd_iommu_domain_init,
3439 .domain_destroy = amd_iommu_domain_destroy,
3440 .attach_dev = amd_iommu_attach_device,
3441 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003442 .map = amd_iommu_map,
3443 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003444 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003445 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003446 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003447};
3448
Joerg Roedel0feae532009-08-26 15:26:30 +02003449/*****************************************************************************
3450 *
3451 * The next functions do a basic initialization of IOMMU for pass through
3452 * mode
3453 *
3454 * In passthrough mode the IOMMU is initialized and enabled but not used for
3455 * DMA-API translation.
3456 *
3457 *****************************************************************************/
3458
3459int __init amd_iommu_init_passthrough(void)
3460{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003461 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003462 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003463 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003464 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003465 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003466
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003467 ret = alloc_passthrough_domain();
3468 if (ret)
3469 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003470
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003471 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003472 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003473 continue;
3474
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003475 dev_data = get_dev_data(&dev->dev);
3476 dev_data->passthrough = true;
3477
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003478 devid = get_device_id(&dev->dev);
3479
Joerg Roedel15898bb2009-11-24 15:39:42 +01003480 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003481 if (!iommu)
3482 continue;
3483
Joerg Roedel15898bb2009-11-24 15:39:42 +01003484 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003485 }
3486
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003487 amd_iommu_stats_init();
3488
Joerg Roedel0feae532009-08-26 15:26:30 +02003489 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3490
3491 return 0;
3492}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003493
3494/* IOMMUv2 specific functions */
3495int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3496{
3497 return atomic_notifier_chain_register(&ppr_notifier, nb);
3498}
3499EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3500
3501int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3502{
3503 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3504}
3505EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003506
3507void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3508{
3509 struct protection_domain *domain = dom->priv;
3510 unsigned long flags;
3511
3512 spin_lock_irqsave(&domain->lock, flags);
3513
3514 /* Update data structure */
3515 domain->mode = PAGE_MODE_NONE;
3516 domain->updated = true;
3517
3518 /* Make changes visible to IOMMUs */
3519 update_domain(domain);
3520
3521 /* Page-table is not visible to IOMMU anymore, so free it */
3522 free_pagetable(domain);
3523
3524 spin_unlock_irqrestore(&domain->lock, flags);
3525}
3526EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003527
3528int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3529{
3530 struct protection_domain *domain = dom->priv;
3531 unsigned long flags;
3532 int levels, ret;
3533
3534 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3535 return -EINVAL;
3536
3537 /* Number of GCR3 table levels required */
3538 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3539 levels += 1;
3540
3541 if (levels > amd_iommu_max_glx_val)
3542 return -EINVAL;
3543
3544 spin_lock_irqsave(&domain->lock, flags);
3545
3546 /*
3547 * Save us all sanity checks whether devices already in the
3548 * domain support IOMMUv2. Just force that the domain has no
3549 * devices attached when it is switched into IOMMUv2 mode.
3550 */
3551 ret = -EBUSY;
3552 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3553 goto out;
3554
3555 ret = -ENOMEM;
3556 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3557 if (domain->gcr3_tbl == NULL)
3558 goto out;
3559
3560 domain->glx = levels;
3561 domain->flags |= PD_IOMMUV2_MASK;
3562 domain->updated = true;
3563
3564 update_domain(domain);
3565
3566 ret = 0;
3567
3568out:
3569 spin_unlock_irqrestore(&domain->lock, flags);
3570
3571 return ret;
3572}
3573EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003574
3575static int __flush_pasid(struct protection_domain *domain, int pasid,
3576 u64 address, bool size)
3577{
3578 struct iommu_dev_data *dev_data;
3579 struct iommu_cmd cmd;
3580 int i, ret;
3581
3582 if (!(domain->flags & PD_IOMMUV2_MASK))
3583 return -EINVAL;
3584
3585 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3586
3587 /*
3588 * IOMMU TLB needs to be flushed before Device TLB to
3589 * prevent device TLB refill from IOMMU TLB
3590 */
3591 for (i = 0; i < amd_iommus_present; ++i) {
3592 if (domain->dev_iommu[i] == 0)
3593 continue;
3594
3595 ret = iommu_queue_command(amd_iommus[i], &cmd);
3596 if (ret != 0)
3597 goto out;
3598 }
3599
3600 /* Wait until IOMMU TLB flushes are complete */
3601 domain_flush_complete(domain);
3602
3603 /* Now flush device TLBs */
3604 list_for_each_entry(dev_data, &domain->dev_list, list) {
3605 struct amd_iommu *iommu;
3606 int qdep;
3607
3608 BUG_ON(!dev_data->ats.enabled);
3609
3610 qdep = dev_data->ats.qdep;
3611 iommu = amd_iommu_rlookup_table[dev_data->devid];
3612
3613 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3614 qdep, address, size);
3615
3616 ret = iommu_queue_command(iommu, &cmd);
3617 if (ret != 0)
3618 goto out;
3619 }
3620
3621 /* Wait until all device TLBs are flushed */
3622 domain_flush_complete(domain);
3623
3624 ret = 0;
3625
3626out:
3627
3628 return ret;
3629}
3630
3631static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3632 u64 address)
3633{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003634 INC_STATS_COUNTER(invalidate_iotlb);
3635
Joerg Roedel22e266c2011-11-21 15:59:08 +01003636 return __flush_pasid(domain, pasid, address, false);
3637}
3638
3639int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3640 u64 address)
3641{
3642 struct protection_domain *domain = dom->priv;
3643 unsigned long flags;
3644 int ret;
3645
3646 spin_lock_irqsave(&domain->lock, flags);
3647 ret = __amd_iommu_flush_page(domain, pasid, address);
3648 spin_unlock_irqrestore(&domain->lock, flags);
3649
3650 return ret;
3651}
3652EXPORT_SYMBOL(amd_iommu_flush_page);
3653
3654static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3655{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003656 INC_STATS_COUNTER(invalidate_iotlb_all);
3657
Joerg Roedel22e266c2011-11-21 15:59:08 +01003658 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3659 true);
3660}
3661
3662int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3663{
3664 struct protection_domain *domain = dom->priv;
3665 unsigned long flags;
3666 int ret;
3667
3668 spin_lock_irqsave(&domain->lock, flags);
3669 ret = __amd_iommu_flush_tlb(domain, pasid);
3670 spin_unlock_irqrestore(&domain->lock, flags);
3671
3672 return ret;
3673}
3674EXPORT_SYMBOL(amd_iommu_flush_tlb);
3675
Joerg Roedelb16137b2011-11-21 16:50:23 +01003676static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3677{
3678 int index;
3679 u64 *pte;
3680
3681 while (true) {
3682
3683 index = (pasid >> (9 * level)) & 0x1ff;
3684 pte = &root[index];
3685
3686 if (level == 0)
3687 break;
3688
3689 if (!(*pte & GCR3_VALID)) {
3690 if (!alloc)
3691 return NULL;
3692
3693 root = (void *)get_zeroed_page(GFP_ATOMIC);
3694 if (root == NULL)
3695 return NULL;
3696
3697 *pte = __pa(root) | GCR3_VALID;
3698 }
3699
3700 root = __va(*pte & PAGE_MASK);
3701
3702 level -= 1;
3703 }
3704
3705 return pte;
3706}
3707
3708static int __set_gcr3(struct protection_domain *domain, int pasid,
3709 unsigned long cr3)
3710{
3711 u64 *pte;
3712
3713 if (domain->mode != PAGE_MODE_NONE)
3714 return -EINVAL;
3715
3716 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3717 if (pte == NULL)
3718 return -ENOMEM;
3719
3720 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3721
3722 return __amd_iommu_flush_tlb(domain, pasid);
3723}
3724
3725static int __clear_gcr3(struct protection_domain *domain, int pasid)
3726{
3727 u64 *pte;
3728
3729 if (domain->mode != PAGE_MODE_NONE)
3730 return -EINVAL;
3731
3732 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3733 if (pte == NULL)
3734 return 0;
3735
3736 *pte = 0;
3737
3738 return __amd_iommu_flush_tlb(domain, pasid);
3739}
3740
3741int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3742 unsigned long cr3)
3743{
3744 struct protection_domain *domain = dom->priv;
3745 unsigned long flags;
3746 int ret;
3747
3748 spin_lock_irqsave(&domain->lock, flags);
3749 ret = __set_gcr3(domain, pasid, cr3);
3750 spin_unlock_irqrestore(&domain->lock, flags);
3751
3752 return ret;
3753}
3754EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3755
3756int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3757{
3758 struct protection_domain *domain = dom->priv;
3759 unsigned long flags;
3760 int ret;
3761
3762 spin_lock_irqsave(&domain->lock, flags);
3763 ret = __clear_gcr3(domain, pasid);
3764 spin_unlock_irqrestore(&domain->lock, flags);
3765
3766 return ret;
3767}
3768EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003769
3770int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3771 int status, int tag)
3772{
3773 struct iommu_dev_data *dev_data;
3774 struct amd_iommu *iommu;
3775 struct iommu_cmd cmd;
3776
Joerg Roedel399be2f2011-12-01 16:53:47 +01003777 INC_STATS_COUNTER(complete_ppr);
3778
Joerg Roedelc99afa22011-11-21 18:19:25 +01003779 dev_data = get_dev_data(&pdev->dev);
3780 iommu = amd_iommu_rlookup_table[dev_data->devid];
3781
3782 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3783 tag, dev_data->pri_tlp);
3784
3785 return iommu_queue_command(iommu, &cmd);
3786}
3787EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003788
3789struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3790{
3791 struct protection_domain *domain;
3792
3793 domain = get_domain(&pdev->dev);
3794 if (IS_ERR(domain))
3795 return NULL;
3796
3797 /* Only return IOMMUv2 domains */
3798 if (!(domain->flags & PD_IOMMUV2_MASK))
3799 return NULL;
3800
3801 return domain->iommu_domain;
3802}
3803EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003804
3805void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3806{
3807 struct iommu_dev_data *dev_data;
3808
3809 if (!amd_iommu_v2_supported())
3810 return;
3811
3812 dev_data = get_dev_data(&pdev->dev);
3813 dev_data->errata |= (1 << erratum);
3814}
3815EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003816
3817int amd_iommu_device_info(struct pci_dev *pdev,
3818 struct amd_iommu_device_info *info)
3819{
3820 int max_pasids;
3821 int pos;
3822
3823 if (pdev == NULL || info == NULL)
3824 return -EINVAL;
3825
3826 if (!amd_iommu_v2_supported())
3827 return -EINVAL;
3828
3829 memset(info, 0, sizeof(*info));
3830
3831 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3832 if (pos)
3833 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3834
3835 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3836 if (pos)
3837 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3838
3839 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3840 if (pos) {
3841 int features;
3842
3843 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3844 max_pasids = min(max_pasids, (1 << 20));
3845
3846 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3847 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3848
3849 features = pci_pasid_features(pdev);
3850 if (features & PCI_PASID_CAP_EXEC)
3851 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3852 if (features & PCI_PASID_CAP_PRIV)
3853 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3854 }
3855
3856 return 0;
3857}
3858EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003859
3860#ifdef CONFIG_IRQ_REMAP
3861
3862/*****************************************************************************
3863 *
3864 * Interrupt Remapping Implementation
3865 *
3866 *****************************************************************************/
3867
3868union irte {
3869 u32 val;
3870 struct {
3871 u32 valid : 1,
3872 no_fault : 1,
3873 int_type : 3,
3874 rq_eoi : 1,
3875 dm : 1,
3876 rsvd_1 : 1,
3877 destination : 8,
3878 vector : 8,
3879 rsvd_2 : 8;
3880 } fields;
3881};
3882
3883#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3884#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3885#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3886#define DTE_IRQ_REMAP_ENABLE 1ULL
3887
3888static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3889{
3890 u64 dte;
3891
3892 dte = amd_iommu_dev_table[devid].data[2];
3893 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3894 dte |= virt_to_phys(table->table);
3895 dte |= DTE_IRQ_REMAP_INTCTL;
3896 dte |= DTE_IRQ_TABLE_LEN;
3897 dte |= DTE_IRQ_REMAP_ENABLE;
3898
3899 amd_iommu_dev_table[devid].data[2] = dte;
3900}
3901
3902#define IRTE_ALLOCATED (~1U)
3903
3904static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3905{
3906 struct irq_remap_table *table = NULL;
3907 struct amd_iommu *iommu;
3908 unsigned long flags;
3909 u16 alias;
3910
3911 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3912
3913 iommu = amd_iommu_rlookup_table[devid];
3914 if (!iommu)
3915 goto out_unlock;
3916
3917 table = irq_lookup_table[devid];
3918 if (table)
3919 goto out;
3920
3921 alias = amd_iommu_alias_table[devid];
3922 table = irq_lookup_table[alias];
3923 if (table) {
3924 irq_lookup_table[devid] = table;
3925 set_dte_irq_entry(devid, table);
3926 iommu_flush_dte(iommu, devid);
3927 goto out;
3928 }
3929
3930 /* Nothing there yet, allocate new irq remapping table */
3931 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3932 if (!table)
3933 goto out;
3934
Joerg Roedel197887f2013-04-09 21:14:08 +02003935 /* Initialize table spin-lock */
3936 spin_lock_init(&table->lock);
3937
Joerg Roedel2b324502012-06-21 16:29:10 +02003938 if (ioapic)
3939 /* Keep the first 32 indexes free for IOAPIC interrupts */
3940 table->min_index = 32;
3941
3942 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3943 if (!table->table) {
3944 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003945 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003946 goto out;
3947 }
3948
3949 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3950
3951 if (ioapic) {
3952 int i;
3953
3954 for (i = 0; i < 32; ++i)
3955 table->table[i] = IRTE_ALLOCATED;
3956 }
3957
3958 irq_lookup_table[devid] = table;
3959 set_dte_irq_entry(devid, table);
3960 iommu_flush_dte(iommu, devid);
3961 if (devid != alias) {
3962 irq_lookup_table[alias] = table;
3963 set_dte_irq_entry(devid, table);
3964 iommu_flush_dte(iommu, alias);
3965 }
3966
3967out:
3968 iommu_completion_wait(iommu);
3969
3970out_unlock:
3971 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3972
3973 return table;
3974}
3975
3976static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3977{
3978 struct irq_remap_table *table;
3979 unsigned long flags;
3980 int index, c;
3981
3982 table = get_irq_table(devid, false);
3983 if (!table)
3984 return -ENODEV;
3985
3986 spin_lock_irqsave(&table->lock, flags);
3987
3988 /* Scan table for free entries */
3989 for (c = 0, index = table->min_index;
3990 index < MAX_IRQS_PER_TABLE;
3991 ++index) {
3992 if (table->table[index] == 0)
3993 c += 1;
3994 else
3995 c = 0;
3996
3997 if (c == count) {
Joerg Roedel0dfedd62013-04-09 15:39:16 +02003998 struct irq_2_irte *irte_info;
Joerg Roedel2b324502012-06-21 16:29:10 +02003999
4000 for (; c != 0; --c)
4001 table->table[index - c + 1] = IRTE_ALLOCATED;
4002
4003 index -= count - 1;
4004
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004005 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004006 irte_info = &cfg->irq_2_irte;
4007 irte_info->devid = devid;
4008 irte_info->index = index;
Joerg Roedel2b324502012-06-21 16:29:10 +02004009
4010 goto out;
4011 }
4012 }
4013
4014 index = -ENOSPC;
4015
4016out:
4017 spin_unlock_irqrestore(&table->lock, flags);
4018
4019 return index;
4020}
4021
4022static int get_irte(u16 devid, int index, union irte *irte)
4023{
4024 struct irq_remap_table *table;
4025 unsigned long flags;
4026
4027 table = get_irq_table(devid, false);
4028 if (!table)
4029 return -ENOMEM;
4030
4031 spin_lock_irqsave(&table->lock, flags);
4032 irte->val = table->table[index];
4033 spin_unlock_irqrestore(&table->lock, flags);
4034
4035 return 0;
4036}
4037
4038static int modify_irte(u16 devid, int index, union irte irte)
4039{
4040 struct irq_remap_table *table;
4041 struct amd_iommu *iommu;
4042 unsigned long flags;
4043
4044 iommu = amd_iommu_rlookup_table[devid];
4045 if (iommu == NULL)
4046 return -EINVAL;
4047
4048 table = get_irq_table(devid, false);
4049 if (!table)
4050 return -ENOMEM;
4051
4052 spin_lock_irqsave(&table->lock, flags);
4053 table->table[index] = irte.val;
4054 spin_unlock_irqrestore(&table->lock, flags);
4055
4056 iommu_flush_irt(iommu, devid);
4057 iommu_completion_wait(iommu);
4058
4059 return 0;
4060}
4061
4062static void free_irte(u16 devid, int index)
4063{
4064 struct irq_remap_table *table;
4065 struct amd_iommu *iommu;
4066 unsigned long flags;
4067
4068 iommu = amd_iommu_rlookup_table[devid];
4069 if (iommu == NULL)
4070 return;
4071
4072 table = get_irq_table(devid, false);
4073 if (!table)
4074 return;
4075
4076 spin_lock_irqsave(&table->lock, flags);
4077 table->table[index] = 0;
4078 spin_unlock_irqrestore(&table->lock, flags);
4079
4080 iommu_flush_irt(iommu, devid);
4081 iommu_completion_wait(iommu);
4082}
4083
Joerg Roedel5527de72012-06-26 11:17:32 +02004084static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4085 unsigned int destination, int vector,
4086 struct io_apic_irq_attr *attr)
4087{
4088 struct irq_remap_table *table;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004089 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004090 struct irq_cfg *cfg;
4091 union irte irte;
4092 int ioapic_id;
4093 int index;
4094 int devid;
4095 int ret;
4096
4097 cfg = irq_get_chip_data(irq);
4098 if (!cfg)
4099 return -EINVAL;
4100
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004101 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004102 ioapic_id = mpc_ioapic_id(attr->ioapic);
4103 devid = get_ioapic_devid(ioapic_id);
4104
4105 if (devid < 0)
4106 return devid;
4107
4108 table = get_irq_table(devid, true);
4109 if (table == NULL)
4110 return -ENOMEM;
4111
4112 index = attr->ioapic_pin;
4113
4114 /* Setup IRQ remapping info */
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004115 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004116 irte_info->devid = devid;
4117 irte_info->index = index;
Joerg Roedel5527de72012-06-26 11:17:32 +02004118
4119 /* Setup IRTE for IOMMU */
4120 irte.val = 0;
4121 irte.fields.vector = vector;
4122 irte.fields.int_type = apic->irq_delivery_mode;
4123 irte.fields.destination = destination;
4124 irte.fields.dm = apic->irq_dest_mode;
4125 irte.fields.valid = 1;
4126
4127 ret = modify_irte(devid, index, irte);
4128 if (ret)
4129 return ret;
4130
4131 /* Setup IOAPIC entry */
4132 memset(entry, 0, sizeof(*entry));
4133
4134 entry->vector = index;
4135 entry->mask = 0;
4136 entry->trigger = attr->trigger;
4137 entry->polarity = attr->polarity;
4138
4139 /*
4140 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004141 */
4142 if (attr->trigger)
4143 entry->mask = 1;
4144
4145 return 0;
4146}
4147
4148static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4149 bool force)
4150{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004151 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004152 unsigned int dest, irq;
4153 struct irq_cfg *cfg;
4154 union irte irte;
4155 int err;
4156
4157 if (!config_enabled(CONFIG_SMP))
4158 return -1;
4159
4160 cfg = data->chip_data;
4161 irq = data->irq;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004162 irte_info = &cfg->irq_2_irte;
Joerg Roedel5527de72012-06-26 11:17:32 +02004163
4164 if (!cpumask_intersects(mask, cpu_online_mask))
4165 return -EINVAL;
4166
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004167 if (get_irte(irte_info->devid, irte_info->index, &irte))
Joerg Roedel5527de72012-06-26 11:17:32 +02004168 return -EBUSY;
4169
4170 if (assign_irq_vector(irq, cfg, mask))
4171 return -EBUSY;
4172
4173 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4174 if (err) {
4175 if (assign_irq_vector(irq, cfg, data->affinity))
4176 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4177 return err;
4178 }
4179
4180 irte.fields.vector = cfg->vector;
4181 irte.fields.destination = dest;
4182
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004183 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel5527de72012-06-26 11:17:32 +02004184
4185 if (cfg->move_in_progress)
4186 send_cleanup_vector(cfg);
4187
4188 cpumask_copy(data->affinity, mask);
4189
4190 return 0;
4191}
4192
4193static int free_irq(int irq)
4194{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004195 struct irq_2_irte *irte_info;
Joerg Roedel5527de72012-06-26 11:17:32 +02004196 struct irq_cfg *cfg;
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
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004204 free_irte(irte_info->devid, irte_info->index);
Joerg Roedel5527de72012-06-26 11:17:32 +02004205
4206 return 0;
4207}
4208
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004209static void compose_msi_msg(struct pci_dev *pdev,
4210 unsigned int irq, unsigned int dest,
4211 struct msi_msg *msg, u8 hpet_id)
4212{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004213 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004214 struct irq_cfg *cfg;
4215 union irte irte;
4216
4217 cfg = irq_get_chip_data(irq);
4218 if (!cfg)
4219 return;
4220
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004221 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004222
4223 irte.val = 0;
4224 irte.fields.vector = cfg->vector;
4225 irte.fields.int_type = apic->irq_delivery_mode;
4226 irte.fields.destination = dest;
4227 irte.fields.dm = apic->irq_dest_mode;
4228 irte.fields.valid = 1;
4229
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004230 modify_irte(irte_info->devid, irte_info->index, irte);
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004231
4232 msg->address_hi = MSI_ADDR_BASE_HI;
4233 msg->address_lo = MSI_ADDR_BASE_LO;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004234 msg->data = irte_info->index;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004235}
4236
4237static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4238{
4239 struct irq_cfg *cfg;
4240 int index;
4241 u16 devid;
4242
4243 if (!pdev)
4244 return -EINVAL;
4245
4246 cfg = irq_get_chip_data(irq);
4247 if (!cfg)
4248 return -EINVAL;
4249
4250 devid = get_device_id(&pdev->dev);
4251 index = alloc_irq_index(cfg, devid, nvec);
4252
4253 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4254}
4255
4256static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4257 int index, int offset)
4258{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004259 struct irq_2_irte *irte_info;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004260 struct irq_cfg *cfg;
4261 u16 devid;
4262
4263 if (!pdev)
4264 return -EINVAL;
4265
4266 cfg = irq_get_chip_data(irq);
4267 if (!cfg)
4268 return -EINVAL;
4269
4270 if (index >= MAX_IRQS_PER_TABLE)
4271 return 0;
4272
4273 devid = get_device_id(&pdev->dev);
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004274 irte_info = &cfg->irq_2_irte;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004275
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004276 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004277 irte_info->devid = devid;
4278 irte_info->index = index + offset;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004279
4280 return 0;
4281}
4282
Joerg Roedeld9761952012-06-26 16:00:08 +02004283static int setup_hpet_msi(unsigned int irq, unsigned int id)
4284{
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004285 struct irq_2_irte *irte_info;
Joerg Roedeld9761952012-06-26 16:00:08 +02004286 struct irq_cfg *cfg;
4287 int index, devid;
4288
4289 cfg = irq_get_chip_data(irq);
4290 if (!cfg)
4291 return -EINVAL;
4292
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004293 irte_info = &cfg->irq_2_irte;
Joerg Roedeld9761952012-06-26 16:00:08 +02004294 devid = get_hpet_devid(id);
4295 if (devid < 0)
4296 return devid;
4297
4298 index = alloc_irq_index(cfg, devid, 1);
4299 if (index < 0)
4300 return index;
4301
Joerg Roedel9b1b0e42012-09-26 12:44:45 +02004302 cfg->remapped = 1;
Joerg Roedel0dfedd62013-04-09 15:39:16 +02004303 irte_info->devid = devid;
4304 irte_info->index = index;
Joerg Roedeld9761952012-06-26 16:00:08 +02004305
4306 return 0;
4307}
4308
Joerg Roedel6b474b82012-06-26 16:46:04 +02004309struct irq_remap_ops amd_iommu_irq_ops = {
4310 .supported = amd_iommu_supported,
4311 .prepare = amd_iommu_prepare,
4312 .enable = amd_iommu_enable,
4313 .disable = amd_iommu_disable,
4314 .reenable = amd_iommu_reenable,
4315 .enable_faulting = amd_iommu_enable_faulting,
4316 .setup_ioapic_entry = setup_ioapic_entry,
4317 .set_affinity = set_affinity,
4318 .free_irq = free_irq,
4319 .compose_msi_msg = compose_msi_msg,
4320 .msi_alloc_irq = msi_alloc_irq,
4321 .msi_setup_irq = msi_setup_irq,
4322 .setup_hpet_msi = setup_hpet_msi,
4323};
Joerg Roedel2b324502012-06-21 16:29:10 +02004324#endif