blob: 55074cba20eba9012d6a310acef62b965728bcea [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedelb6c02712008-06-26 21:27:53 +02003 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020022#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010025#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020026#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090027#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020028#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010029#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020030#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020031#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010032#include <linux/notifier.h>
33#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020034#include <linux/irq.h>
35#include <linux/msi.h>
36#include <asm/irq_remapping.h>
37#include <asm/io_apic.h>
38#include <asm/apic.h>
39#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020040#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020041#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090042#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010043#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020044#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020045
46#include "amd_iommu_proto.h"
47#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020048#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020049
50#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
51
Joerg Roedel815b33f2011-04-06 17:26:49 +020052#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020053
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020054/*
55 * This bitmap is used to advertise the page sizes our hardware support
56 * to the IOMMU core, which will then use this information to split
57 * physically contiguous memory regions it is mapping into page sizes
58 * that we support.
59 *
60 * Traditionally the IOMMU core just handed us the mappings directly,
61 * after making sure the size is an order of a 4KiB page and that the
62 * mapping has natural alignment.
63 *
64 * To retain this behavior, we currently advertise that we support
65 * all page sizes that are an order of 4KiB.
66 *
67 * If at some point we'd like to utilize the IOMMU core's new behavior,
68 * we could change this to advertise the real page sizes we support.
69 */
70#define AMD_IOMMU_PGSIZES (~0xFFFUL)
71
Joerg Roedelb6c02712008-06-26 21:27:53 +020072static DEFINE_RWLOCK(amd_iommu_devtable_lock);
73
Joerg Roedelbd60b732008-09-11 10:24:48 +020074/* A list of preallocated protection domains */
75static LIST_HEAD(iommu_pd_list);
76static DEFINE_SPINLOCK(iommu_pd_list_lock);
77
Joerg Roedel8fa5f802011-06-09 12:24:45 +020078/* List of all available dev_data structures */
79static LIST_HEAD(dev_data_list);
80static DEFINE_SPINLOCK(dev_data_list_lock);
81
Joerg Roedel6efed632012-06-14 15:52:58 +020082LIST_HEAD(ioapic_map);
83LIST_HEAD(hpet_map);
84
Joerg Roedel0feae532009-08-26 15:26:30 +020085/*
86 * Domain for untranslated devices - only allocated
87 * if iommu=pt passed on kernel cmd line.
88 */
89static struct protection_domain *pt_domain;
90
Joerg Roedel26961ef2008-12-03 17:00:17 +010091static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010092
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010093static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010094int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010095
Joerg Roedelac1534a2012-06-21 14:52:40 +020096static struct dma_map_ops amd_iommu_dma_ops;
97
Joerg Roedel431b2a22008-07-11 17:14:22 +020098/*
99 * general struct to manage commands send to an IOMMU
100 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200101struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200102 u32 data[4];
103};
104
Joerg Roedel05152a02012-06-15 16:53:51 +0200105struct kmem_cache *amd_iommu_irq_cache;
106
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200107static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100108static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700109
Joerg Roedel15898bb2009-11-24 15:39:42 +0100110/****************************************************************************
111 *
112 * Helper functions
113 *
114 ****************************************************************************/
115
Joerg Roedelf62dda62011-06-09 12:55:35 +0200116static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200117{
118 struct iommu_dev_data *dev_data;
119 unsigned long flags;
120
121 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
122 if (!dev_data)
123 return NULL;
124
Joerg Roedelf62dda62011-06-09 12:55:35 +0200125 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200126 atomic_set(&dev_data->bind, 0);
127
128 spin_lock_irqsave(&dev_data_list_lock, flags);
129 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
130 spin_unlock_irqrestore(&dev_data_list_lock, flags);
131
132 return dev_data;
133}
134
135static void free_dev_data(struct iommu_dev_data *dev_data)
136{
137 unsigned long flags;
138
139 spin_lock_irqsave(&dev_data_list_lock, flags);
140 list_del(&dev_data->dev_data_list);
141 spin_unlock_irqrestore(&dev_data_list_lock, flags);
142
143 kfree(dev_data);
144}
145
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200146static struct iommu_dev_data *search_dev_data(u16 devid)
147{
148 struct iommu_dev_data *dev_data;
149 unsigned long flags;
150
151 spin_lock_irqsave(&dev_data_list_lock, flags);
152 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
153 if (dev_data->devid == devid)
154 goto out_unlock;
155 }
156
157 dev_data = NULL;
158
159out_unlock:
160 spin_unlock_irqrestore(&dev_data_list_lock, flags);
161
162 return dev_data;
163}
164
165static struct iommu_dev_data *find_dev_data(u16 devid)
166{
167 struct iommu_dev_data *dev_data;
168
169 dev_data = search_dev_data(devid);
170
171 if (dev_data == NULL)
172 dev_data = alloc_dev_data(devid);
173
174 return dev_data;
175}
176
Joerg Roedel15898bb2009-11-24 15:39:42 +0100177static inline u16 get_device_id(struct device *dev)
178{
179 struct pci_dev *pdev = to_pci_dev(dev);
180
181 return calc_devid(pdev->bus->number, pdev->devfn);
182}
183
Joerg Roedel657cbb62009-11-23 15:26:46 +0100184static struct iommu_dev_data *get_dev_data(struct device *dev)
185{
186 return dev->archdata.iommu;
187}
188
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100189static bool pci_iommuv2_capable(struct pci_dev *pdev)
190{
191 static const int caps[] = {
192 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100193 PCI_EXT_CAP_ID_PRI,
194 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100195 };
196 int i, pos;
197
198 for (i = 0; i < 3; ++i) {
199 pos = pci_find_ext_capability(pdev, caps[i]);
200 if (pos == 0)
201 return false;
202 }
203
204 return true;
205}
206
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100207static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
208{
209 struct iommu_dev_data *dev_data;
210
211 dev_data = get_dev_data(&pdev->dev);
212
213 return dev_data->errata & (1 << erratum) ? true : false;
214}
215
Joerg Roedel71c70982009-11-24 16:43:06 +0100216/*
217 * In this function the list of preallocated protection domains is traversed to
218 * find the domain for a specific device
219 */
220static struct dma_ops_domain *find_protection_domain(u16 devid)
221{
222 struct dma_ops_domain *entry, *ret = NULL;
223 unsigned long flags;
224 u16 alias = amd_iommu_alias_table[devid];
225
226 if (list_empty(&iommu_pd_list))
227 return NULL;
228
229 spin_lock_irqsave(&iommu_pd_list_lock, flags);
230
231 list_for_each_entry(entry, &iommu_pd_list, list) {
232 if (entry->target_dev == devid ||
233 entry->target_dev == alias) {
234 ret = entry;
235 break;
236 }
237 }
238
239 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
240
241 return ret;
242}
243
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100244/*
245 * This function checks if the driver got a valid device from the caller to
246 * avoid dereferencing invalid pointers.
247 */
248static bool check_device(struct device *dev)
249{
250 u16 devid;
251
252 if (!dev || !dev->dma_mask)
253 return false;
254
255 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100256 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100257 return false;
258
259 devid = get_device_id(dev);
260
261 /* Out of our scope? */
262 if (devid > amd_iommu_last_bdf)
263 return false;
264
265 if (amd_iommu_rlookup_table[devid] == NULL)
266 return false;
267
268 return true;
269}
270
Alex Williamson664b6002012-05-30 14:19:31 -0600271static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
272{
273 pci_dev_put(*from);
274 *from = to;
275}
276
277#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
278
Joerg Roedel657cbb62009-11-23 15:26:46 +0100279static int iommu_init_device(struct device *dev)
280{
Joerg Roedel0774e392012-09-28 16:14:44 +0200281 struct pci_dev *dma_pdev = NULL, *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100282 struct iommu_dev_data *dev_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600283 struct iommu_group *group;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200284 u16 alias;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600285 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100286
287 if (dev->archdata.iommu)
288 return 0;
289
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200290 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100291 if (!dev_data)
292 return -ENOMEM;
293
Joerg Roedelf62dda62011-06-09 12:55:35 +0200294 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200295 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200296 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100297
Joerg Roedel71f77582011-06-09 19:03:15 +0200298 alias_data = find_dev_data(alias);
299 if (alias_data == NULL) {
300 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
301 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200302 free_dev_data(dev_data);
303 return -ENOTSUPP;
304 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200305 dev_data->alias_data = alias_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600306
307 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
Joerg Roedel0774e392012-09-28 16:14:44 +0200308 }
309
310 if (dma_pdev == NULL)
Alex Williamson9dcd6132012-05-30 14:19:07 -0600311 dma_pdev = pci_dev_get(pdev);
312
Alex Williamson31fe9432012-08-04 12:09:03 -0600313 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600314 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
315
Alex Williamson31fe9432012-08-04 12:09:03 -0600316 /*
317 * If it's a multifunction device that does not support our
318 * required ACS flags, add to the same group as function 0.
319 */
Alex Williamson664b6002012-05-30 14:19:31 -0600320 if (dma_pdev->multifunction &&
321 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
322 swap_pci_ref(&dma_pdev,
323 pci_get_slot(dma_pdev->bus,
324 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
325 0)));
326
Alex Williamson31fe9432012-08-04 12:09:03 -0600327 /*
328 * Devices on the root bus go through the iommu. If that's not us,
329 * find the next upstream device and test ACS up to the root bus.
330 * Finding the next device may require skipping virtual buses.
331 */
Alex Williamson664b6002012-05-30 14:19:31 -0600332 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson31fe9432012-08-04 12:09:03 -0600333 struct pci_bus *bus = dma_pdev->bus;
334
335 while (!bus->self) {
336 if (!pci_is_root_bus(bus))
337 bus = bus->parent;
338 else
339 goto root_bus;
340 }
341
342 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600343 break;
344
Alex Williamson31fe9432012-08-04 12:09:03 -0600345 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200346 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100347
Alex Williamson31fe9432012-08-04 12:09:03 -0600348root_bus:
Alex Williamson9dcd6132012-05-30 14:19:07 -0600349 group = iommu_group_get(&dma_pdev->dev);
350 pci_dev_put(dma_pdev);
351 if (!group) {
352 group = iommu_group_alloc();
353 if (IS_ERR(group))
354 return PTR_ERR(group);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100355 }
356
Alex Williamson9dcd6132012-05-30 14:19:07 -0600357 ret = iommu_group_add_device(group, dev);
358
359 iommu_group_put(group);
360
361 if (ret)
362 return ret;
363
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100364 if (pci_iommuv2_capable(pdev)) {
365 struct amd_iommu *iommu;
366
367 iommu = amd_iommu_rlookup_table[dev_data->devid];
368 dev_data->iommu_v2 = iommu->is_iommu_v2;
369 }
370
Joerg Roedel657cbb62009-11-23 15:26:46 +0100371 dev->archdata.iommu = dev_data;
372
Joerg Roedel657cbb62009-11-23 15:26:46 +0100373 return 0;
374}
375
Joerg Roedel26018872011-06-06 16:50:14 +0200376static void iommu_ignore_device(struct device *dev)
377{
378 u16 devid, alias;
379
380 devid = get_device_id(dev);
381 alias = amd_iommu_alias_table[devid];
382
383 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
384 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
385
386 amd_iommu_rlookup_table[devid] = NULL;
387 amd_iommu_rlookup_table[alias] = NULL;
388}
389
Joerg Roedel657cbb62009-11-23 15:26:46 +0100390static void iommu_uninit_device(struct device *dev)
391{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600392 iommu_group_remove_device(dev);
393
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200394 /*
395 * Nothing to do here - we keep dev_data around for unplugged devices
396 * and reuse it when the device is re-plugged - not doing so would
397 * introduce a ton of races.
398 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100399}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100400
401void __init amd_iommu_uninit_devices(void)
402{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200403 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100404 struct pci_dev *pdev = NULL;
405
406 for_each_pci_dev(pdev) {
407
408 if (!check_device(&pdev->dev))
409 continue;
410
411 iommu_uninit_device(&pdev->dev);
412 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200413
414 /* Free all of our dev_data structures */
415 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
416 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100417}
418
419int __init amd_iommu_init_devices(void)
420{
421 struct pci_dev *pdev = NULL;
422 int ret = 0;
423
424 for_each_pci_dev(pdev) {
425
426 if (!check_device(&pdev->dev))
427 continue;
428
429 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200430 if (ret == -ENOTSUPP)
431 iommu_ignore_device(&pdev->dev);
432 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100433 goto out_free;
434 }
435
436 return 0;
437
438out_free:
439
440 amd_iommu_uninit_devices();
441
442 return ret;
443}
Joerg Roedel7f265082008-12-12 13:50:21 +0100444#ifdef CONFIG_AMD_IOMMU_STATS
445
446/*
447 * Initialization code for statistics collection
448 */
449
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100450DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100451DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100452DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100453DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100454DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100455DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100456DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100457DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100458DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100459DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100460DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100461DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100462DECLARE_STATS_COUNTER(complete_ppr);
463DECLARE_STATS_COUNTER(invalidate_iotlb);
464DECLARE_STATS_COUNTER(invalidate_iotlb_all);
465DECLARE_STATS_COUNTER(pri_requests);
466
Joerg Roedel7f265082008-12-12 13:50:21 +0100467static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100468static struct dentry *de_fflush;
469
470static void amd_iommu_stats_add(struct __iommu_counter *cnt)
471{
472 if (stats_dir == NULL)
473 return;
474
475 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
476 &cnt->value);
477}
478
479static void amd_iommu_stats_init(void)
480{
481 stats_dir = debugfs_create_dir("amd-iommu", NULL);
482 if (stats_dir == NULL)
483 return;
484
Joerg Roedel7f265082008-12-12 13:50:21 +0100485 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300486 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100487
488 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100489 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100490 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100491 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100492 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100493 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100494 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100495 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100496 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100497 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100498 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100499 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100500 amd_iommu_stats_add(&complete_ppr);
501 amd_iommu_stats_add(&invalidate_iotlb);
502 amd_iommu_stats_add(&invalidate_iotlb_all);
503 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100504}
505
506#endif
507
Joerg Roedel431b2a22008-07-11 17:14:22 +0200508/****************************************************************************
509 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200510 * Interrupt handling functions
511 *
512 ****************************************************************************/
513
Joerg Roedele3e59872009-09-03 14:02:10 +0200514static void dump_dte_entry(u16 devid)
515{
516 int i;
517
Joerg Roedelee6c2862011-11-09 12:06:03 +0100518 for (i = 0; i < 4; ++i)
519 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200520 amd_iommu_dev_table[devid].data[i]);
521}
522
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200523static void dump_command(unsigned long phys_addr)
524{
525 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
526 int i;
527
528 for (i = 0; i < 4; ++i)
529 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
530}
531
Joerg Roedela345b232009-09-03 15:01:43 +0200532static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200533{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200534 int type, devid, domid, flags;
535 volatile u32 *event = __evt;
536 int count = 0;
537 u64 address;
538
539retry:
540 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
541 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
542 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
543 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
544 address = (u64)(((u64)event[3]) << 32) | event[2];
545
546 if (type == 0) {
547 /* Did we hit the erratum? */
548 if (++count == LOOP_TIMEOUT) {
549 pr_err("AMD-Vi: No event written to event log\n");
550 return;
551 }
552 udelay(1);
553 goto retry;
554 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200555
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200556 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200557
558 switch (type) {
559 case EVENT_TYPE_ILL_DEV:
560 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
561 "address=0x%016llx flags=0x%04x]\n",
562 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
563 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200564 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200565 break;
566 case EVENT_TYPE_IO_FAULT:
567 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
568 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
569 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
570 domid, address, flags);
571 break;
572 case EVENT_TYPE_DEV_TAB_ERR:
573 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
574 "address=0x%016llx flags=0x%04x]\n",
575 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
576 address, flags);
577 break;
578 case EVENT_TYPE_PAGE_TAB_ERR:
579 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
580 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
581 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
582 domid, address, flags);
583 break;
584 case EVENT_TYPE_ILL_CMD:
585 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200586 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200587 break;
588 case EVENT_TYPE_CMD_HARD_ERR:
589 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
590 "flags=0x%04x]\n", address, flags);
591 break;
592 case EVENT_TYPE_IOTLB_INV_TO:
593 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
594 "address=0x%016llx]\n",
595 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
596 address);
597 break;
598 case EVENT_TYPE_INV_DEV_REQ:
599 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
600 "address=0x%016llx flags=0x%04x]\n",
601 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
602 address, flags);
603 break;
604 default:
605 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
606 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200607
608 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200609}
610
611static void iommu_poll_events(struct amd_iommu *iommu)
612{
613 u32 head, tail;
614 unsigned long flags;
615
616 spin_lock_irqsave(&iommu->lock, flags);
617
618 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
619 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
620
621 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200622 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200623 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
624 }
625
626 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
627
628 spin_unlock_irqrestore(&iommu->lock, flags);
629}
630
Joerg Roedeleee53532012-06-01 15:20:23 +0200631static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100632{
633 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100634
Joerg Roedel399be2f2011-12-01 16:53:47 +0100635 INC_STATS_COUNTER(pri_requests);
636
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100637 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
638 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
639 return;
640 }
641
642 fault.address = raw[1];
643 fault.pasid = PPR_PASID(raw[0]);
644 fault.device_id = PPR_DEVID(raw[0]);
645 fault.tag = PPR_TAG(raw[0]);
646 fault.flags = PPR_FLAGS(raw[0]);
647
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100648 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
649}
650
651static void iommu_poll_ppr_log(struct amd_iommu *iommu)
652{
653 unsigned long flags;
654 u32 head, tail;
655
656 if (iommu->ppr_log == NULL)
657 return;
658
Joerg Roedeleee53532012-06-01 15:20:23 +0200659 /* enable ppr interrupts again */
660 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
661
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100662 spin_lock_irqsave(&iommu->lock, flags);
663
664 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
665 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
666
667 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200668 volatile u64 *raw;
669 u64 entry[2];
670 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100671
Joerg Roedeleee53532012-06-01 15:20:23 +0200672 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100673
Joerg Roedeleee53532012-06-01 15:20:23 +0200674 /*
675 * Hardware bug: Interrupt may arrive before the entry is
676 * written to memory. If this happens we need to wait for the
677 * entry to arrive.
678 */
679 for (i = 0; i < LOOP_TIMEOUT; ++i) {
680 if (PPR_REQ_TYPE(raw[0]) != 0)
681 break;
682 udelay(1);
683 }
684
685 /* Avoid memcpy function-call overhead */
686 entry[0] = raw[0];
687 entry[1] = raw[1];
688
689 /*
690 * To detect the hardware bug we need to clear the entry
691 * back to zero.
692 */
693 raw[0] = raw[1] = 0UL;
694
695 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100696 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
697 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200698
699 /*
700 * Release iommu->lock because ppr-handling might need to
Frank Arnolddf805ab2012-08-27 19:21:04 +0200701 * re-acquire it
Joerg Roedeleee53532012-06-01 15:20:23 +0200702 */
703 spin_unlock_irqrestore(&iommu->lock, flags);
704
705 /* Handle PPR entry */
706 iommu_handle_ppr_entry(iommu, entry);
707
708 spin_lock_irqsave(&iommu->lock, flags);
709
710 /* Refresh ring-buffer information */
711 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100712 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
713 }
714
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100715 spin_unlock_irqrestore(&iommu->lock, flags);
716}
717
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200718irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200719{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200720 struct amd_iommu *iommu;
721
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100722 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200723 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100724 iommu_poll_ppr_log(iommu);
725 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200726
727 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200728}
729
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200730irqreturn_t amd_iommu_int_handler(int irq, void *data)
731{
732 return IRQ_WAKE_THREAD;
733}
734
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200735/****************************************************************************
736 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200737 * IOMMU command queuing functions
738 *
739 ****************************************************************************/
740
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200741static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200742{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200743 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200744
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200745 while (*sem == 0 && i < LOOP_TIMEOUT) {
746 udelay(1);
747 i += 1;
748 }
749
750 if (i == LOOP_TIMEOUT) {
751 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
752 return -EIO;
753 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200754
755 return 0;
756}
757
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200758static void copy_cmd_to_buffer(struct amd_iommu *iommu,
759 struct iommu_cmd *cmd,
760 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200761{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200762 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200763
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200764 target = iommu->cmd_buf + tail;
765 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200766
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200767 /* Copy command to buffer */
768 memcpy(target, cmd, sizeof(*cmd));
769
770 /* Tell the IOMMU about it */
771 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
772}
773
Joerg Roedel815b33f2011-04-06 17:26:49 +0200774static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200775{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200776 WARN_ON(address & 0x7ULL);
777
Joerg Roedelded46732011-04-06 10:53:48 +0200778 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200779 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
780 cmd->data[1] = upper_32_bits(__pa(address));
781 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200782 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
783}
784
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200785static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
786{
787 memset(cmd, 0, sizeof(*cmd));
788 cmd->data[0] = devid;
789 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
790}
791
Joerg Roedel11b64022011-04-06 11:49:28 +0200792static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
793 size_t size, u16 domid, int pde)
794{
795 u64 pages;
796 int s;
797
798 pages = iommu_num_pages(address, size, PAGE_SIZE);
799 s = 0;
800
801 if (pages > 1) {
802 /*
803 * If we have to flush more than one page, flush all
804 * TLB entries for this domain
805 */
806 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
807 s = 1;
808 }
809
810 address &= PAGE_MASK;
811
812 memset(cmd, 0, sizeof(*cmd));
813 cmd->data[1] |= domid;
814 cmd->data[2] = lower_32_bits(address);
815 cmd->data[3] = upper_32_bits(address);
816 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
817 if (s) /* size bit - we flush more than one 4kb page */
818 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200819 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200820 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
821}
822
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200823static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
824 u64 address, size_t size)
825{
826 u64 pages;
827 int s;
828
829 pages = iommu_num_pages(address, size, PAGE_SIZE);
830 s = 0;
831
832 if (pages > 1) {
833 /*
834 * If we have to flush more than one page, flush all
835 * TLB entries for this domain
836 */
837 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
838 s = 1;
839 }
840
841 address &= PAGE_MASK;
842
843 memset(cmd, 0, sizeof(*cmd));
844 cmd->data[0] = devid;
845 cmd->data[0] |= (qdep & 0xff) << 24;
846 cmd->data[1] = devid;
847 cmd->data[2] = lower_32_bits(address);
848 cmd->data[3] = upper_32_bits(address);
849 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
850 if (s)
851 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
852}
853
Joerg Roedel22e266c2011-11-21 15:59:08 +0100854static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
855 u64 address, bool size)
856{
857 memset(cmd, 0, sizeof(*cmd));
858
859 address &= ~(0xfffULL);
860
861 cmd->data[0] = pasid & PASID_MASK;
862 cmd->data[1] = domid;
863 cmd->data[2] = lower_32_bits(address);
864 cmd->data[3] = upper_32_bits(address);
865 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
866 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
867 if (size)
868 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
869 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
870}
871
872static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
873 int qdep, u64 address, bool size)
874{
875 memset(cmd, 0, sizeof(*cmd));
876
877 address &= ~(0xfffULL);
878
879 cmd->data[0] = devid;
880 cmd->data[0] |= (pasid & 0xff) << 16;
881 cmd->data[0] |= (qdep & 0xff) << 24;
882 cmd->data[1] = devid;
883 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
884 cmd->data[2] = lower_32_bits(address);
885 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
886 cmd->data[3] = upper_32_bits(address);
887 if (size)
888 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
889 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
890}
891
Joerg Roedelc99afa22011-11-21 18:19:25 +0100892static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
893 int status, int tag, bool gn)
894{
895 memset(cmd, 0, sizeof(*cmd));
896
897 cmd->data[0] = devid;
898 if (gn) {
899 cmd->data[1] = pasid & PASID_MASK;
900 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
901 }
902 cmd->data[3] = tag & 0x1ff;
903 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
904
905 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
906}
907
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200908static void build_inv_all(struct iommu_cmd *cmd)
909{
910 memset(cmd, 0, sizeof(*cmd));
911 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200912}
913
Joerg Roedel7ef27982012-06-21 16:46:04 +0200914static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
915{
916 memset(cmd, 0, sizeof(*cmd));
917 cmd->data[0] = devid;
918 CMD_SET_TYPE(cmd, CMD_INV_IRT);
919}
920
Joerg Roedel431b2a22008-07-11 17:14:22 +0200921/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200922 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200923 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200924 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200925static int iommu_queue_command_sync(struct amd_iommu *iommu,
926 struct iommu_cmd *cmd,
927 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200928{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200929 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200930 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200931
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200932 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100933
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200934again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200935 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200936
937 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
938 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
939 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
940 left = (head - next_tail) % iommu->cmd_buf_size;
941
942 if (left <= 2) {
943 struct iommu_cmd sync_cmd;
944 volatile u64 sem = 0;
945 int ret;
946
947 build_completion_wait(&sync_cmd, (u64)&sem);
948 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
949
950 spin_unlock_irqrestore(&iommu->lock, flags);
951
952 if ((ret = wait_on_sem(&sem)) != 0)
953 return ret;
954
955 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200956 }
957
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200958 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200959
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200960 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200961 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200962
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200963 spin_unlock_irqrestore(&iommu->lock, flags);
964
Joerg Roedel815b33f2011-04-06 17:26:49 +0200965 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100966}
967
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200968static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
969{
970 return iommu_queue_command_sync(iommu, cmd, true);
971}
972
Joerg Roedel8d201962008-12-02 20:34:41 +0100973/*
974 * This function queues a completion wait command into the command
975 * buffer of an IOMMU
976 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100977static int iommu_completion_wait(struct amd_iommu *iommu)
978{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200979 struct iommu_cmd cmd;
980 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200981 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100982
983 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200984 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100985
Joerg Roedel815b33f2011-04-06 17:26:49 +0200986 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100987
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200988 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100989 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200990 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100991
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200992 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200993}
994
Joerg Roedeld8c13082011-04-06 18:51:26 +0200995static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200996{
997 struct iommu_cmd cmd;
998
Joerg Roedeld8c13082011-04-06 18:51:26 +0200999 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001000
Joerg Roedeld8c13082011-04-06 18:51:26 +02001001 return iommu_queue_command(iommu, &cmd);
1002}
1003
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001004static void iommu_flush_dte_all(struct amd_iommu *iommu)
1005{
1006 u32 devid;
1007
1008 for (devid = 0; devid <= 0xffff; ++devid)
1009 iommu_flush_dte(iommu, devid);
1010
1011 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001012}
1013
1014/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001015 * This function uses heavy locking and may disable irqs for some time. But
1016 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001017 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001018static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001019{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001020 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001021
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001022 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1023 struct iommu_cmd cmd;
1024 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1025 dom_id, 1);
1026 iommu_queue_command(iommu, &cmd);
1027 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001028
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001029 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001030}
1031
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001032static void iommu_flush_all(struct amd_iommu *iommu)
1033{
1034 struct iommu_cmd cmd;
1035
1036 build_inv_all(&cmd);
1037
1038 iommu_queue_command(iommu, &cmd);
1039 iommu_completion_wait(iommu);
1040}
1041
Joerg Roedel7ef27982012-06-21 16:46:04 +02001042static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1043{
1044 struct iommu_cmd cmd;
1045
1046 build_inv_irt(&cmd, devid);
1047
1048 iommu_queue_command(iommu, &cmd);
1049}
1050
1051static void iommu_flush_irt_all(struct amd_iommu *iommu)
1052{
1053 u32 devid;
1054
1055 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1056 iommu_flush_irt(iommu, devid);
1057
1058 iommu_completion_wait(iommu);
1059}
1060
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001061void iommu_flush_all_caches(struct amd_iommu *iommu)
1062{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001063 if (iommu_feature(iommu, FEATURE_IA)) {
1064 iommu_flush_all(iommu);
1065 } else {
1066 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001067 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001068 iommu_flush_tlb_all(iommu);
1069 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001070}
1071
Joerg Roedel431b2a22008-07-11 17:14:22 +02001072/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001073 * Command send function for flushing on-device TLB
1074 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001075static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1076 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001077{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001078 struct amd_iommu *iommu;
1079 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001080 int qdep;
1081
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001082 qdep = dev_data->ats.qdep;
1083 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001084
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001085 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001086
1087 return iommu_queue_command(iommu, &cmd);
1088}
1089
1090/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001091 * Command send function for invalidating a device table entry
1092 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001093static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001094{
1095 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001096 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001097
Joerg Roedel6c542042011-06-09 17:07:31 +02001098 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001099
Joerg Roedelf62dda62011-06-09 12:55:35 +02001100 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001101 if (ret)
1102 return ret;
1103
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001104 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001105 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001106
1107 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001108}
1109
Joerg Roedel431b2a22008-07-11 17:14:22 +02001110/*
1111 * TLB invalidation function which is called from the mapping functions.
1112 * It invalidates a single PTE if the range to flush is within a single
1113 * page. Otherwise it flushes the whole TLB of the IOMMU.
1114 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001115static void __domain_flush_pages(struct protection_domain *domain,
1116 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001117{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001118 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001119 struct iommu_cmd cmd;
1120 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001121
Joerg Roedel11b64022011-04-06 11:49:28 +02001122 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001123
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001124 for (i = 0; i < amd_iommus_present; ++i) {
1125 if (!domain->dev_iommu[i])
1126 continue;
1127
1128 /*
1129 * Devices of this domain are behind this IOMMU
1130 * We need a TLB flush
1131 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001132 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001133 }
1134
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001135 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001136
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001137 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001138 continue;
1139
Joerg Roedel6c542042011-06-09 17:07:31 +02001140 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001141 }
1142
Joerg Roedel11b64022011-04-06 11:49:28 +02001143 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001144}
1145
Joerg Roedel17b124b2011-04-06 18:01:35 +02001146static void domain_flush_pages(struct protection_domain *domain,
1147 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001148{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001149 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001150}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001151
Joerg Roedel1c655772008-09-04 18:40:05 +02001152/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001153static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001154{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001155 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001156}
1157
Chris Wright42a49f92009-06-15 15:42:00 +02001158/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001159static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001160{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001161 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1162}
1163
1164static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001165{
1166 int i;
1167
1168 for (i = 0; i < amd_iommus_present; ++i) {
1169 if (!domain->dev_iommu[i])
1170 continue;
1171
1172 /*
1173 * Devices of this domain are behind this IOMMU
1174 * We need to wait for completion of all commands.
1175 */
1176 iommu_completion_wait(amd_iommus[i]);
1177 }
1178}
1179
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001180
Joerg Roedel43f49602008-12-02 21:01:12 +01001181/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001182 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001183 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001184static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001185{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001186 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001187
1188 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001189 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001190}
1191
Joerg Roedel431b2a22008-07-11 17:14:22 +02001192/****************************************************************************
1193 *
1194 * The functions below are used the create the page table mappings for
1195 * unity mapped regions.
1196 *
1197 ****************************************************************************/
1198
1199/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001200 * This function is used to add another level to an IO page table. Adding
1201 * another level increases the size of the address space by 9 bits to a size up
1202 * to 64 bits.
1203 */
1204static bool increase_address_space(struct protection_domain *domain,
1205 gfp_t gfp)
1206{
1207 u64 *pte;
1208
1209 if (domain->mode == PAGE_MODE_6_LEVEL)
1210 /* address space already 64 bit large */
1211 return false;
1212
1213 pte = (void *)get_zeroed_page(gfp);
1214 if (!pte)
1215 return false;
1216
1217 *pte = PM_LEVEL_PDE(domain->mode,
1218 virt_to_phys(domain->pt_root));
1219 domain->pt_root = pte;
1220 domain->mode += 1;
1221 domain->updated = true;
1222
1223 return true;
1224}
1225
1226static u64 *alloc_pte(struct protection_domain *domain,
1227 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001228 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001229 u64 **pte_page,
1230 gfp_t gfp)
1231{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001232 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001233 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001234
1235 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001236
1237 while (address > PM_LEVEL_SIZE(domain->mode))
1238 increase_address_space(domain, gfp);
1239
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001240 level = domain->mode - 1;
1241 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1242 address = PAGE_SIZE_ALIGN(address, page_size);
1243 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001244
1245 while (level > end_lvl) {
1246 if (!IOMMU_PTE_PRESENT(*pte)) {
1247 page = (u64 *)get_zeroed_page(gfp);
1248 if (!page)
1249 return NULL;
1250 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1251 }
1252
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001253 /* No level skipping support yet */
1254 if (PM_PTE_LEVEL(*pte) != level)
1255 return NULL;
1256
Joerg Roedel308973d2009-11-24 17:43:32 +01001257 level -= 1;
1258
1259 pte = IOMMU_PTE_PAGE(*pte);
1260
1261 if (pte_page && level == end_lvl)
1262 *pte_page = pte;
1263
1264 pte = &pte[PM_LEVEL_INDEX(level, address)];
1265 }
1266
1267 return pte;
1268}
1269
1270/*
1271 * This function checks if there is a PTE for a given dma address. If
1272 * there is one, it returns the pointer to it.
1273 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001274static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001275{
1276 int level;
1277 u64 *pte;
1278
Joerg Roedel24cd7722010-01-19 17:27:39 +01001279 if (address > PM_LEVEL_SIZE(domain->mode))
1280 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001281
Joerg Roedel24cd7722010-01-19 17:27:39 +01001282 level = domain->mode - 1;
1283 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1284
1285 while (level > 0) {
1286
1287 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001288 if (!IOMMU_PTE_PRESENT(*pte))
1289 return NULL;
1290
Joerg Roedel24cd7722010-01-19 17:27:39 +01001291 /* Large PTE */
1292 if (PM_PTE_LEVEL(*pte) == 0x07) {
1293 unsigned long pte_mask, __pte;
1294
1295 /*
1296 * If we have a series of large PTEs, make
1297 * sure to return a pointer to the first one.
1298 */
1299 pte_mask = PTE_PAGE_SIZE(*pte);
1300 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1301 __pte = ((unsigned long)pte) & pte_mask;
1302
1303 return (u64 *)__pte;
1304 }
1305
1306 /* No level skipping support yet */
1307 if (PM_PTE_LEVEL(*pte) != level)
1308 return NULL;
1309
Joerg Roedel308973d2009-11-24 17:43:32 +01001310 level -= 1;
1311
Joerg Roedel24cd7722010-01-19 17:27:39 +01001312 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001313 pte = IOMMU_PTE_PAGE(*pte);
1314 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001315 }
1316
1317 return pte;
1318}
1319
1320/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001321 * Generic mapping functions. It maps a physical address into a DMA
1322 * address space. It allocates the page table pages if necessary.
1323 * In the future it can be extended to a generic mapping function
1324 * supporting all features of AMD IOMMU page tables like level skipping
1325 * and full 64 bit address spaces.
1326 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001327static int iommu_map_page(struct protection_domain *dom,
1328 unsigned long bus_addr,
1329 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001330 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001331 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001332{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001333 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001334 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001335
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001336 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001337 return -EINVAL;
1338
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001339 bus_addr = PAGE_ALIGN(bus_addr);
1340 phys_addr = PAGE_ALIGN(phys_addr);
1341 count = PAGE_SIZE_PTE_COUNT(page_size);
1342 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001343
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001344 for (i = 0; i < count; ++i)
1345 if (IOMMU_PTE_PRESENT(pte[i]))
1346 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001347
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001348 if (page_size > PAGE_SIZE) {
1349 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1350 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1351 } else
1352 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1353
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001354 if (prot & IOMMU_PROT_IR)
1355 __pte |= IOMMU_PTE_IR;
1356 if (prot & IOMMU_PROT_IW)
1357 __pte |= IOMMU_PTE_IW;
1358
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001359 for (i = 0; i < count; ++i)
1360 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001361
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001362 update_domain(dom);
1363
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001364 return 0;
1365}
1366
Joerg Roedel24cd7722010-01-19 17:27:39 +01001367static unsigned long iommu_unmap_page(struct protection_domain *dom,
1368 unsigned long bus_addr,
1369 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001370{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001371 unsigned long long unmap_size, unmapped;
1372 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001373
Joerg Roedel24cd7722010-01-19 17:27:39 +01001374 BUG_ON(!is_power_of_2(page_size));
1375
1376 unmapped = 0;
1377
1378 while (unmapped < page_size) {
1379
1380 pte = fetch_pte(dom, bus_addr);
1381
1382 if (!pte) {
1383 /*
1384 * No PTE for this address
1385 * move forward in 4kb steps
1386 */
1387 unmap_size = PAGE_SIZE;
1388 } else if (PM_PTE_LEVEL(*pte) == 0) {
1389 /* 4kb PTE found for this address */
1390 unmap_size = PAGE_SIZE;
1391 *pte = 0ULL;
1392 } else {
1393 int count, i;
1394
1395 /* Large PTE found which maps this address */
1396 unmap_size = PTE_PAGE_SIZE(*pte);
1397 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1398 for (i = 0; i < count; i++)
1399 pte[i] = 0ULL;
1400 }
1401
1402 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1403 unmapped += unmap_size;
1404 }
1405
1406 BUG_ON(!is_power_of_2(unmapped));
1407
1408 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001409}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001410
Joerg Roedel431b2a22008-07-11 17:14:22 +02001411/*
1412 * This function checks if a specific unity mapping entry is needed for
1413 * this specific IOMMU.
1414 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001415static int iommu_for_unity_map(struct amd_iommu *iommu,
1416 struct unity_map_entry *entry)
1417{
1418 u16 bdf, i;
1419
1420 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1421 bdf = amd_iommu_alias_table[i];
1422 if (amd_iommu_rlookup_table[bdf] == iommu)
1423 return 1;
1424 }
1425
1426 return 0;
1427}
1428
Joerg Roedel431b2a22008-07-11 17:14:22 +02001429/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001430 * This function actually applies the mapping to the page table of the
1431 * dma_ops domain.
1432 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001433static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1434 struct unity_map_entry *e)
1435{
1436 u64 addr;
1437 int ret;
1438
1439 for (addr = e->address_start; addr < e->address_end;
1440 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001441 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001442 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001443 if (ret)
1444 return ret;
1445 /*
1446 * if unity mapping is in aperture range mark the page
1447 * as allocated in the aperture
1448 */
1449 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001450 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001451 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001452 }
1453
1454 return 0;
1455}
1456
Joerg Roedel431b2a22008-07-11 17:14:22 +02001457/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001458 * Init the unity mappings for a specific IOMMU in the system
1459 *
1460 * Basically iterates over all unity mapping entries and applies them to
1461 * the default domain DMA of that IOMMU if necessary.
1462 */
1463static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1464{
1465 struct unity_map_entry *entry;
1466 int ret;
1467
1468 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1469 if (!iommu_for_unity_map(iommu, entry))
1470 continue;
1471 ret = dma_ops_unity_map(iommu->default_dom, entry);
1472 if (ret)
1473 return ret;
1474 }
1475
1476 return 0;
1477}
1478
1479/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001480 * Inits the unity mappings required for a specific device
1481 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001482static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1483 u16 devid)
1484{
1485 struct unity_map_entry *e;
1486 int ret;
1487
1488 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1489 if (!(devid >= e->devid_start && devid <= e->devid_end))
1490 continue;
1491 ret = dma_ops_unity_map(dma_dom, e);
1492 if (ret)
1493 return ret;
1494 }
1495
1496 return 0;
1497}
1498
Joerg Roedel431b2a22008-07-11 17:14:22 +02001499/****************************************************************************
1500 *
1501 * The next functions belong to the address allocator for the dma_ops
1502 * interface functions. They work like the allocators in the other IOMMU
1503 * drivers. Its basically a bitmap which marks the allocated pages in
1504 * the aperture. Maybe it could be enhanced in the future to a more
1505 * efficient allocator.
1506 *
1507 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001508
Joerg Roedel431b2a22008-07-11 17:14:22 +02001509/*
Joerg Roedel384de722009-05-15 12:30:05 +02001510 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001511 *
1512 * called with domain->lock held
1513 */
Joerg Roedel384de722009-05-15 12:30:05 +02001514
Joerg Roedel9cabe892009-05-18 16:38:55 +02001515/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001516 * Used to reserve address ranges in the aperture (e.g. for exclusion
1517 * ranges.
1518 */
1519static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1520 unsigned long start_page,
1521 unsigned int pages)
1522{
1523 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1524
1525 if (start_page + pages > last_page)
1526 pages = last_page - start_page;
1527
1528 for (i = start_page; i < start_page + pages; ++i) {
1529 int index = i / APERTURE_RANGE_PAGES;
1530 int page = i % APERTURE_RANGE_PAGES;
1531 __set_bit(page, dom->aperture[index]->bitmap);
1532 }
1533}
1534
1535/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001536 * This function is used to add a new aperture range to an existing
1537 * aperture in case of dma_ops domain allocation or address allocation
1538 * failure.
1539 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001540static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001541 bool populate, gfp_t gfp)
1542{
1543 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001544 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001545 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001546
Joerg Roedelf5e97052009-05-22 12:31:53 +02001547#ifdef CONFIG_IOMMU_STRESS
1548 populate = false;
1549#endif
1550
Joerg Roedel9cabe892009-05-18 16:38:55 +02001551 if (index >= APERTURE_MAX_RANGES)
1552 return -ENOMEM;
1553
1554 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1555 if (!dma_dom->aperture[index])
1556 return -ENOMEM;
1557
1558 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1559 if (!dma_dom->aperture[index]->bitmap)
1560 goto out_free;
1561
1562 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1563
1564 if (populate) {
1565 unsigned long address = dma_dom->aperture_size;
1566 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1567 u64 *pte, *pte_page;
1568
1569 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001570 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001571 &pte_page, gfp);
1572 if (!pte)
1573 goto out_free;
1574
1575 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1576
1577 address += APERTURE_RANGE_SIZE / 64;
1578 }
1579 }
1580
Joerg Roedel17f5b562011-07-06 17:14:44 +02001581 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001582 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1583
Joerg Roedel17f5b562011-07-06 17:14:44 +02001584 /* Reserve address range used for MSI messages */
1585 if (old_size < MSI_ADDR_BASE_LO &&
1586 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1587 unsigned long spage;
1588 int pages;
1589
1590 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1591 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1592
1593 dma_ops_reserve_addresses(dma_dom, spage, pages);
1594 }
1595
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001596 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001597 for_each_iommu(iommu) {
1598 if (iommu->exclusion_start &&
1599 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1600 && iommu->exclusion_start < dma_dom->aperture_size) {
1601 unsigned long startpage;
1602 int pages = iommu_num_pages(iommu->exclusion_start,
1603 iommu->exclusion_length,
1604 PAGE_SIZE);
1605 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1606 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1607 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001608 }
1609
1610 /*
1611 * Check for areas already mapped as present in the new aperture
1612 * range and mark those pages as reserved in the allocator. Such
1613 * mappings may already exist as a result of requested unity
1614 * mappings for devices.
1615 */
1616 for (i = dma_dom->aperture[index]->offset;
1617 i < dma_dom->aperture_size;
1618 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001619 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001620 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1621 continue;
1622
Joerg Roedelfcd08612011-10-11 17:41:32 +02001623 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001624 }
1625
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001626 update_domain(&dma_dom->domain);
1627
Joerg Roedel9cabe892009-05-18 16:38:55 +02001628 return 0;
1629
1630out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001631 update_domain(&dma_dom->domain);
1632
Joerg Roedel9cabe892009-05-18 16:38:55 +02001633 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1634
1635 kfree(dma_dom->aperture[index]);
1636 dma_dom->aperture[index] = NULL;
1637
1638 return -ENOMEM;
1639}
1640
Joerg Roedel384de722009-05-15 12:30:05 +02001641static unsigned long dma_ops_area_alloc(struct device *dev,
1642 struct dma_ops_domain *dom,
1643 unsigned int pages,
1644 unsigned long align_mask,
1645 u64 dma_mask,
1646 unsigned long start)
1647{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001648 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001649 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1650 int i = start >> APERTURE_RANGE_SHIFT;
1651 unsigned long boundary_size;
1652 unsigned long address = -1;
1653 unsigned long limit;
1654
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001655 next_bit >>= PAGE_SHIFT;
1656
Joerg Roedel384de722009-05-15 12:30:05 +02001657 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1658 PAGE_SIZE) >> PAGE_SHIFT;
1659
1660 for (;i < max_index; ++i) {
1661 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1662
1663 if (dom->aperture[i]->offset >= dma_mask)
1664 break;
1665
1666 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1667 dma_mask >> PAGE_SHIFT);
1668
1669 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1670 limit, next_bit, pages, 0,
1671 boundary_size, align_mask);
1672 if (address != -1) {
1673 address = dom->aperture[i]->offset +
1674 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001675 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001676 break;
1677 }
1678
1679 next_bit = 0;
1680 }
1681
1682 return address;
1683}
1684
Joerg Roedeld3086442008-06-26 21:27:57 +02001685static unsigned long dma_ops_alloc_addresses(struct device *dev,
1686 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001687 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001688 unsigned long align_mask,
1689 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001690{
Joerg Roedeld3086442008-06-26 21:27:57 +02001691 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001692
Joerg Roedelfe16f082009-05-22 12:27:53 +02001693#ifdef CONFIG_IOMMU_STRESS
1694 dom->next_address = 0;
1695 dom->need_flush = true;
1696#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001697
Joerg Roedel384de722009-05-15 12:30:05 +02001698 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001699 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001700
Joerg Roedel1c655772008-09-04 18:40:05 +02001701 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001702 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001703 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1704 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001705 dom->need_flush = true;
1706 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001707
Joerg Roedel384de722009-05-15 12:30:05 +02001708 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001709 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001710
1711 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1712
1713 return address;
1714}
1715
Joerg Roedel431b2a22008-07-11 17:14:22 +02001716/*
1717 * The address free function.
1718 *
1719 * called with domain->lock held
1720 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001721static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1722 unsigned long address,
1723 unsigned int pages)
1724{
Joerg Roedel384de722009-05-15 12:30:05 +02001725 unsigned i = address >> APERTURE_RANGE_SHIFT;
1726 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001727
Joerg Roedel384de722009-05-15 12:30:05 +02001728 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1729
Joerg Roedel47bccd62009-05-22 12:40:54 +02001730#ifdef CONFIG_IOMMU_STRESS
1731 if (i < 4)
1732 return;
1733#endif
1734
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001735 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001736 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001737
1738 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001739
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001740 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001741
Joerg Roedeld3086442008-06-26 21:27:57 +02001742}
1743
Joerg Roedel431b2a22008-07-11 17:14:22 +02001744/****************************************************************************
1745 *
1746 * The next functions belong to the domain allocation. A domain is
1747 * allocated for every IOMMU as the default domain. If device isolation
1748 * is enabled, every device get its own domain. The most important thing
1749 * about domains is the page table mapping the DMA address space they
1750 * contain.
1751 *
1752 ****************************************************************************/
1753
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001754/*
1755 * This function adds a protection domain to the global protection domain list
1756 */
1757static void add_domain_to_list(struct protection_domain *domain)
1758{
1759 unsigned long flags;
1760
1761 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1762 list_add(&domain->list, &amd_iommu_pd_list);
1763 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1764}
1765
1766/*
1767 * This function removes a protection domain to the global
1768 * protection domain list
1769 */
1770static void del_domain_from_list(struct protection_domain *domain)
1771{
1772 unsigned long flags;
1773
1774 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1775 list_del(&domain->list);
1776 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1777}
1778
Joerg Roedelec487d12008-06-26 21:27:58 +02001779static u16 domain_id_alloc(void)
1780{
1781 unsigned long flags;
1782 int id;
1783
1784 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1785 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1786 BUG_ON(id == 0);
1787 if (id > 0 && id < MAX_DOMAIN_ID)
1788 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1789 else
1790 id = 0;
1791 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1792
1793 return id;
1794}
1795
Joerg Roedela2acfb72008-12-02 18:28:53 +01001796static void domain_id_free(int id)
1797{
1798 unsigned long flags;
1799
1800 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1801 if (id > 0 && id < MAX_DOMAIN_ID)
1802 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1803 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1804}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001805
Joerg Roedel86db2e52008-12-02 18:20:21 +01001806static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001807{
1808 int i, j;
1809 u64 *p1, *p2, *p3;
1810
Joerg Roedel86db2e52008-12-02 18:20:21 +01001811 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001812
1813 if (!p1)
1814 return;
1815
1816 for (i = 0; i < 512; ++i) {
1817 if (!IOMMU_PTE_PRESENT(p1[i]))
1818 continue;
1819
1820 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001821 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001822 if (!IOMMU_PTE_PRESENT(p2[j]))
1823 continue;
1824 p3 = IOMMU_PTE_PAGE(p2[j]);
1825 free_page((unsigned long)p3);
1826 }
1827
1828 free_page((unsigned long)p2);
1829 }
1830
1831 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001832
1833 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001834}
1835
Joerg Roedelb16137b2011-11-21 16:50:23 +01001836static void free_gcr3_tbl_level1(u64 *tbl)
1837{
1838 u64 *ptr;
1839 int i;
1840
1841 for (i = 0; i < 512; ++i) {
1842 if (!(tbl[i] & GCR3_VALID))
1843 continue;
1844
1845 ptr = __va(tbl[i] & PAGE_MASK);
1846
1847 free_page((unsigned long)ptr);
1848 }
1849}
1850
1851static void free_gcr3_tbl_level2(u64 *tbl)
1852{
1853 u64 *ptr;
1854 int i;
1855
1856 for (i = 0; i < 512; ++i) {
1857 if (!(tbl[i] & GCR3_VALID))
1858 continue;
1859
1860 ptr = __va(tbl[i] & PAGE_MASK);
1861
1862 free_gcr3_tbl_level1(ptr);
1863 }
1864}
1865
Joerg Roedel52815b72011-11-17 17:24:28 +01001866static void free_gcr3_table(struct protection_domain *domain)
1867{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001868 if (domain->glx == 2)
1869 free_gcr3_tbl_level2(domain->gcr3_tbl);
1870 else if (domain->glx == 1)
1871 free_gcr3_tbl_level1(domain->gcr3_tbl);
1872 else if (domain->glx != 0)
1873 BUG();
1874
Joerg Roedel52815b72011-11-17 17:24:28 +01001875 free_page((unsigned long)domain->gcr3_tbl);
1876}
1877
Joerg Roedel431b2a22008-07-11 17:14:22 +02001878/*
1879 * Free a domain, only used if something went wrong in the
1880 * allocation path and we need to free an already allocated page table
1881 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001882static void dma_ops_domain_free(struct dma_ops_domain *dom)
1883{
Joerg Roedel384de722009-05-15 12:30:05 +02001884 int i;
1885
Joerg Roedelec487d12008-06-26 21:27:58 +02001886 if (!dom)
1887 return;
1888
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001889 del_domain_from_list(&dom->domain);
1890
Joerg Roedel86db2e52008-12-02 18:20:21 +01001891 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001892
Joerg Roedel384de722009-05-15 12:30:05 +02001893 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1894 if (!dom->aperture[i])
1895 continue;
1896 free_page((unsigned long)dom->aperture[i]->bitmap);
1897 kfree(dom->aperture[i]);
1898 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001899
1900 kfree(dom);
1901}
1902
Joerg Roedel431b2a22008-07-11 17:14:22 +02001903/*
1904 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001905 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001906 * structures required for the dma_ops interface
1907 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001908static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001909{
1910 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001911
1912 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1913 if (!dma_dom)
1914 return NULL;
1915
1916 spin_lock_init(&dma_dom->domain.lock);
1917
1918 dma_dom->domain.id = domain_id_alloc();
1919 if (dma_dom->domain.id == 0)
1920 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001921 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001922 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001923 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001924 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001925 dma_dom->domain.priv = dma_dom;
1926 if (!dma_dom->domain.pt_root)
1927 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001928
Joerg Roedel1c655772008-09-04 18:40:05 +02001929 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001930 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001931
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001932 add_domain_to_list(&dma_dom->domain);
1933
Joerg Roedel576175c2009-11-23 19:08:46 +01001934 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001935 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001936
Joerg Roedel431b2a22008-07-11 17:14:22 +02001937 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001938 * mark the first page as allocated so we never return 0 as
1939 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001940 */
Joerg Roedel384de722009-05-15 12:30:05 +02001941 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001942 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001943
Joerg Roedelec487d12008-06-26 21:27:58 +02001944
1945 return dma_dom;
1946
1947free_dma_dom:
1948 dma_ops_domain_free(dma_dom);
1949
1950 return NULL;
1951}
1952
Joerg Roedel431b2a22008-07-11 17:14:22 +02001953/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001954 * little helper function to check whether a given protection domain is a
1955 * dma_ops domain
1956 */
1957static bool dma_ops_domain(struct protection_domain *domain)
1958{
1959 return domain->flags & PD_DMA_OPS_MASK;
1960}
1961
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001962static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001963{
Joerg Roedel132bd682011-11-17 14:18:46 +01001964 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001965 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001966
Joerg Roedel132bd682011-11-17 14:18:46 +01001967 if (domain->mode != PAGE_MODE_NONE)
1968 pte_root = virt_to_phys(domain->pt_root);
1969
Joerg Roedel38ddf412008-09-11 10:38:32 +02001970 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1971 << DEV_ENTRY_MODE_SHIFT;
1972 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001973
Joerg Roedelee6c2862011-11-09 12:06:03 +01001974 flags = amd_iommu_dev_table[devid].data[1];
1975
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001976 if (ats)
1977 flags |= DTE_FLAG_IOTLB;
1978
Joerg Roedel52815b72011-11-17 17:24:28 +01001979 if (domain->flags & PD_IOMMUV2_MASK) {
1980 u64 gcr3 = __pa(domain->gcr3_tbl);
1981 u64 glx = domain->glx;
1982 u64 tmp;
1983
1984 pte_root |= DTE_FLAG_GV;
1985 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1986
1987 /* First mask out possible old values for GCR3 table */
1988 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1989 flags &= ~tmp;
1990
1991 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1992 flags &= ~tmp;
1993
1994 /* Encode GCR3 table into DTE */
1995 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1996 pte_root |= tmp;
1997
1998 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1999 flags |= tmp;
2000
2001 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2002 flags |= tmp;
2003 }
2004
Joerg Roedelee6c2862011-11-09 12:06:03 +01002005 flags &= ~(0xffffUL);
2006 flags |= domain->id;
2007
2008 amd_iommu_dev_table[devid].data[1] = flags;
2009 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002010}
2011
Joerg Roedel15898bb2009-11-24 15:39:42 +01002012static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002013{
Joerg Roedel355bf552008-12-08 12:02:41 +01002014 /* remove entry from the device table seen by the hardware */
2015 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2016 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002017
Joerg Roedelc5cca142009-10-09 18:31:20 +02002018 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002019}
2020
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002021static void do_attach(struct iommu_dev_data *dev_data,
2022 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002023{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002024 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002025 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002026
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002027 iommu = amd_iommu_rlookup_table[dev_data->devid];
2028 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002029
2030 /* Update data structures */
2031 dev_data->domain = domain;
2032 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002033 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002034
2035 /* Do reference counting */
2036 domain->dev_iommu[iommu->index] += 1;
2037 domain->dev_cnt += 1;
2038
2039 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002040 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002041}
2042
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002043static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002044{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002045 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002046
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002047 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002048
Joerg Roedelc4596112009-11-20 14:57:32 +01002049 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002050 dev_data->domain->dev_iommu[iommu->index] -= 1;
2051 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002052
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002053 /* Update data structures */
2054 dev_data->domain = NULL;
2055 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002056 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002057
2058 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002059 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002060}
2061
2062/*
2063 * If a device is not yet associated with a domain, this function does
2064 * assigns it visible for the hardware
2065 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002066static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002067 struct protection_domain *domain)
2068{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002069 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002070
Joerg Roedel15898bb2009-11-24 15:39:42 +01002071 /* lock domain */
2072 spin_lock(&domain->lock);
2073
Joerg Roedel71f77582011-06-09 19:03:15 +02002074 if (dev_data->alias_data != NULL) {
2075 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002076
Joerg Roedel2b02b092011-06-09 17:48:39 +02002077 /* Some sanity checks */
2078 ret = -EBUSY;
2079 if (alias_data->domain != NULL &&
2080 alias_data->domain != domain)
2081 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002082
Joerg Roedel2b02b092011-06-09 17:48:39 +02002083 if (dev_data->domain != NULL &&
2084 dev_data->domain != domain)
2085 goto out_unlock;
2086
2087 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002088 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002089 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002090
2091 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002092 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002093
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002094 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002095 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002096
Joerg Roedel24100052009-11-25 15:59:57 +01002097 atomic_inc(&dev_data->bind);
2098
Julia Lawall84fe6c12010-05-27 12:31:51 +02002099 ret = 0;
2100
2101out_unlock:
2102
Joerg Roedel355bf552008-12-08 12:02:41 +01002103 /* ready */
2104 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002105
Julia Lawall84fe6c12010-05-27 12:31:51 +02002106 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002107}
2108
Joerg Roedel52815b72011-11-17 17:24:28 +01002109
2110static void pdev_iommuv2_disable(struct pci_dev *pdev)
2111{
2112 pci_disable_ats(pdev);
2113 pci_disable_pri(pdev);
2114 pci_disable_pasid(pdev);
2115}
2116
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002117/* FIXME: Change generic reset-function to do the same */
2118static int pri_reset_while_enabled(struct pci_dev *pdev)
2119{
2120 u16 control;
2121 int pos;
2122
Joerg Roedel46277b72011-12-07 14:34:02 +01002123 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002124 if (!pos)
2125 return -EINVAL;
2126
Joerg Roedel46277b72011-12-07 14:34:02 +01002127 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2128 control |= PCI_PRI_CTRL_RESET;
2129 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002130
2131 return 0;
2132}
2133
Joerg Roedel52815b72011-11-17 17:24:28 +01002134static int pdev_iommuv2_enable(struct pci_dev *pdev)
2135{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002136 bool reset_enable;
2137 int reqs, ret;
2138
2139 /* FIXME: Hardcode number of outstanding requests for now */
2140 reqs = 32;
2141 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2142 reqs = 1;
2143 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002144
2145 /* Only allow access to user-accessible pages */
2146 ret = pci_enable_pasid(pdev, 0);
2147 if (ret)
2148 goto out_err;
2149
2150 /* First reset the PRI state of the device */
2151 ret = pci_reset_pri(pdev);
2152 if (ret)
2153 goto out_err;
2154
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002155 /* Enable PRI */
2156 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002157 if (ret)
2158 goto out_err;
2159
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002160 if (reset_enable) {
2161 ret = pri_reset_while_enabled(pdev);
2162 if (ret)
2163 goto out_err;
2164 }
2165
Joerg Roedel52815b72011-11-17 17:24:28 +01002166 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2167 if (ret)
2168 goto out_err;
2169
2170 return 0;
2171
2172out_err:
2173 pci_disable_pri(pdev);
2174 pci_disable_pasid(pdev);
2175
2176 return ret;
2177}
2178
Joerg Roedelc99afa22011-11-21 18:19:25 +01002179/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002180#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002181
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002182static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002183{
Joerg Roedela3b93122012-04-12 12:49:26 +02002184 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002185 int pos;
2186
Joerg Roedel46277b72011-12-07 14:34:02 +01002187 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002188 if (!pos)
2189 return false;
2190
Joerg Roedela3b93122012-04-12 12:49:26 +02002191 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002192
Joerg Roedela3b93122012-04-12 12:49:26 +02002193 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002194}
2195
Joerg Roedel15898bb2009-11-24 15:39:42 +01002196/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002197 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002198 * assigns it visible for the hardware
2199 */
2200static int attach_device(struct device *dev,
2201 struct protection_domain *domain)
2202{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002203 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002204 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002205 unsigned long flags;
2206 int ret;
2207
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002208 dev_data = get_dev_data(dev);
2209
Joerg Roedel52815b72011-11-17 17:24:28 +01002210 if (domain->flags & PD_IOMMUV2_MASK) {
2211 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2212 return -EINVAL;
2213
2214 if (pdev_iommuv2_enable(pdev) != 0)
2215 return -EINVAL;
2216
2217 dev_data->ats.enabled = true;
2218 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002219 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002220 } else if (amd_iommu_iotlb_sup &&
2221 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002222 dev_data->ats.enabled = true;
2223 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2224 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002225
Joerg Roedel15898bb2009-11-24 15:39:42 +01002226 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002227 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002228 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2229
2230 /*
2231 * We might boot into a crash-kernel here. The crashed kernel
2232 * left the caches in the IOMMU dirty. So we have to flush
2233 * here to evict all dirty stuff.
2234 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002235 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002236
2237 return ret;
2238}
2239
2240/*
2241 * Removes a device from a protection domain (unlocked)
2242 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002243static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002244{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002245 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002246 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002247
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002248 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002249
Joerg Roedel2ca76272010-01-22 16:45:31 +01002250 domain = dev_data->domain;
2251
2252 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002253
Joerg Roedel71f77582011-06-09 19:03:15 +02002254 if (dev_data->alias_data != NULL) {
2255 struct iommu_dev_data *alias_data = dev_data->alias_data;
2256
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002257 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002258 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002259 }
2260
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002261 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002262 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002263
Joerg Roedel2ca76272010-01-22 16:45:31 +01002264 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002265
Joerg Roedel21129f72009-09-01 11:59:42 +02002266 /*
2267 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002268 * passthrough domain if it is detached from any other domain.
2269 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002270 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002271 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002272 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002273 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002274}
2275
2276/*
2277 * Removes a device from a protection domain (with devtable_lock held)
2278 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002279static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002280{
Joerg Roedel52815b72011-11-17 17:24:28 +01002281 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002282 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002283 unsigned long flags;
2284
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002285 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002286 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002287
Joerg Roedel355bf552008-12-08 12:02:41 +01002288 /* lock device table */
2289 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002290 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002291 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002292
Joerg Roedel52815b72011-11-17 17:24:28 +01002293 if (domain->flags & PD_IOMMUV2_MASK)
2294 pdev_iommuv2_disable(to_pci_dev(dev));
2295 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002296 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002297
2298 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002299}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002300
Joerg Roedel15898bb2009-11-24 15:39:42 +01002301/*
2302 * Find out the protection domain structure for a given PCI device. This
2303 * will give us the pointer to the page table root for example.
2304 */
2305static struct protection_domain *domain_for_device(struct device *dev)
2306{
Joerg Roedel71f77582011-06-09 19:03:15 +02002307 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002308 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002309 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002310
Joerg Roedel657cbb62009-11-23 15:26:46 +01002311 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002312
Joerg Roedel2b02b092011-06-09 17:48:39 +02002313 if (dev_data->domain)
2314 return dev_data->domain;
2315
Joerg Roedel71f77582011-06-09 19:03:15 +02002316 if (dev_data->alias_data != NULL) {
2317 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002318
2319 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2320 if (alias_data->domain != NULL) {
2321 __attach_device(dev_data, alias_data->domain);
2322 dom = alias_data->domain;
2323 }
2324 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002325 }
2326
Joerg Roedel15898bb2009-11-24 15:39:42 +01002327 return dom;
2328}
2329
Joerg Roedele275a2a2008-12-10 18:27:25 +01002330static int device_change_notifier(struct notifier_block *nb,
2331 unsigned long action, void *data)
2332{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002333 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002334 struct protection_domain *domain;
2335 struct iommu_dev_data *dev_data;
2336 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002337 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002338 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002339 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002340
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002341 if (!check_device(dev))
2342 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002343
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002344 devid = get_device_id(dev);
2345 iommu = amd_iommu_rlookup_table[devid];
2346 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002347
2348 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002349 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002350
2351 domain = domain_for_device(dev);
2352
Joerg Roedele275a2a2008-12-10 18:27:25 +01002353 if (!domain)
2354 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002355 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002356 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002357 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002358 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002359 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002360
2361 iommu_init_device(dev);
2362
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002363 /*
2364 * dev_data is still NULL and
2365 * got initialized in iommu_init_device
2366 */
2367 dev_data = get_dev_data(dev);
2368
2369 if (iommu_pass_through || dev_data->iommu_v2) {
2370 dev_data->passthrough = true;
2371 attach_device(dev, pt_domain);
2372 break;
2373 }
2374
Joerg Roedel657cbb62009-11-23 15:26:46 +01002375 domain = domain_for_device(dev);
2376
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002377 /* allocate a protection domain if a device is added */
2378 dma_domain = find_protection_domain(devid);
2379 if (dma_domain)
2380 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002381 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002382 if (!dma_domain)
2383 goto out;
2384 dma_domain->target_dev = devid;
2385
2386 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2387 list_add_tail(&dma_domain->list, &iommu_pd_list);
2388 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2389
Joerg Roedelac1534a2012-06-21 14:52:40 +02002390 dev_data = get_dev_data(dev);
2391
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002392 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002393
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002394 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002395 case BUS_NOTIFY_DEL_DEVICE:
2396
2397 iommu_uninit_device(dev);
2398
Joerg Roedele275a2a2008-12-10 18:27:25 +01002399 default:
2400 goto out;
2401 }
2402
Joerg Roedele275a2a2008-12-10 18:27:25 +01002403 iommu_completion_wait(iommu);
2404
2405out:
2406 return 0;
2407}
2408
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302409static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002410 .notifier_call = device_change_notifier,
2411};
Joerg Roedel355bf552008-12-08 12:02:41 +01002412
Joerg Roedel8638c492009-12-10 11:12:25 +01002413void amd_iommu_init_notifier(void)
2414{
2415 bus_register_notifier(&pci_bus_type, &device_nb);
2416}
2417
Joerg Roedel431b2a22008-07-11 17:14:22 +02002418/*****************************************************************************
2419 *
2420 * The next functions belong to the dma_ops mapping/unmapping code.
2421 *
2422 *****************************************************************************/
2423
2424/*
2425 * In the dma_ops path we only have the struct device. This function
2426 * finds the corresponding IOMMU, the protection domain and the
2427 * requestor id for a given device.
2428 * If the device is not yet associated with a domain this is also done
2429 * in this function.
2430 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002431static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002432{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002433 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002434 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002435 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002436
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002437 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002438 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002439
Joerg Roedel94f6d192009-11-24 16:40:02 +01002440 domain = domain_for_device(dev);
2441 if (domain != NULL && !dma_ops_domain(domain))
2442 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002443
Joerg Roedel94f6d192009-11-24 16:40:02 +01002444 if (domain != NULL)
2445 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002446
Frank Arnolddf805ab2012-08-27 19:21:04 +02002447 /* Device not bound yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002448 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002449 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002450 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2451 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002452 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002453 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002454
Joerg Roedel94f6d192009-11-24 16:40:02 +01002455 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002456}
2457
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002458static void update_device_table(struct protection_domain *domain)
2459{
Joerg Roedel492667d2009-11-27 13:25:47 +01002460 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002461
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002462 list_for_each_entry(dev_data, &domain->dev_list, list)
2463 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002464}
2465
2466static void update_domain(struct protection_domain *domain)
2467{
2468 if (!domain->updated)
2469 return;
2470
2471 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002472
2473 domain_flush_devices(domain);
2474 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002475
2476 domain->updated = false;
2477}
2478
Joerg Roedel431b2a22008-07-11 17:14:22 +02002479/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002480 * This function fetches the PTE for a given address in the aperture
2481 */
2482static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2483 unsigned long address)
2484{
Joerg Roedel384de722009-05-15 12:30:05 +02002485 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002486 u64 *pte, *pte_page;
2487
Joerg Roedel384de722009-05-15 12:30:05 +02002488 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2489 if (!aperture)
2490 return NULL;
2491
2492 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002493 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002494 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002495 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002496 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2497 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002498 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002499
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002500 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002501
2502 return pte;
2503}
2504
2505/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002506 * This is the generic map function. It maps one 4kb page at paddr to
2507 * the given address in the DMA address space for the domain.
2508 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002509static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002510 unsigned long address,
2511 phys_addr_t paddr,
2512 int direction)
2513{
2514 u64 *pte, __pte;
2515
2516 WARN_ON(address > dom->aperture_size);
2517
2518 paddr &= PAGE_MASK;
2519
Joerg Roedel8bda3092009-05-12 12:02:46 +02002520 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002521 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002522 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002523
2524 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2525
2526 if (direction == DMA_TO_DEVICE)
2527 __pte |= IOMMU_PTE_IR;
2528 else if (direction == DMA_FROM_DEVICE)
2529 __pte |= IOMMU_PTE_IW;
2530 else if (direction == DMA_BIDIRECTIONAL)
2531 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2532
2533 WARN_ON(*pte);
2534
2535 *pte = __pte;
2536
2537 return (dma_addr_t)address;
2538}
2539
Joerg Roedel431b2a22008-07-11 17:14:22 +02002540/*
2541 * The generic unmapping function for on page in the DMA address space.
2542 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002543static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002544 unsigned long address)
2545{
Joerg Roedel384de722009-05-15 12:30:05 +02002546 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002547 u64 *pte;
2548
2549 if (address >= dom->aperture_size)
2550 return;
2551
Joerg Roedel384de722009-05-15 12:30:05 +02002552 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2553 if (!aperture)
2554 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002555
Joerg Roedel384de722009-05-15 12:30:05 +02002556 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2557 if (!pte)
2558 return;
2559
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002560 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002561
2562 WARN_ON(!*pte);
2563
2564 *pte = 0ULL;
2565}
2566
Joerg Roedel431b2a22008-07-11 17:14:22 +02002567/*
2568 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002569 * contiguous memory region into DMA address space. It is used by all
2570 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002571 * Must be called with the domain lock held.
2572 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002573static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002574 struct dma_ops_domain *dma_dom,
2575 phys_addr_t paddr,
2576 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002577 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002578 bool align,
2579 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002580{
2581 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002582 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002583 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002584 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002585 int i;
2586
Joerg Roedele3c449f2008-10-15 22:02:11 -07002587 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002588 paddr &= PAGE_MASK;
2589
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002590 INC_STATS_COUNTER(total_map_requests);
2591
Joerg Roedelc1858972008-12-12 15:42:39 +01002592 if (pages > 1)
2593 INC_STATS_COUNTER(cross_page);
2594
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002595 if (align)
2596 align_mask = (1UL << get_order(size)) - 1;
2597
Joerg Roedel11b83882009-05-19 10:23:15 +02002598retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002599 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2600 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002601 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002602 /*
2603 * setting next_address here will let the address
2604 * allocator only scan the new allocated range in the
2605 * first run. This is a small optimization.
2606 */
2607 dma_dom->next_address = dma_dom->aperture_size;
2608
Joerg Roedel576175c2009-11-23 19:08:46 +01002609 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002610 goto out;
2611
2612 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002613 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002614 * allocation again
2615 */
2616 goto retry;
2617 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002618
2619 start = address;
2620 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002621 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002622 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002623 goto out_unmap;
2624
Joerg Roedelcb76c322008-06-26 21:28:00 +02002625 paddr += PAGE_SIZE;
2626 start += PAGE_SIZE;
2627 }
2628 address += offset;
2629
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002630 ADD_STATS_COUNTER(alloced_io_mem, size);
2631
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002632 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002633 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002634 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002635 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002636 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002637
Joerg Roedelcb76c322008-06-26 21:28:00 +02002638out:
2639 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002640
2641out_unmap:
2642
2643 for (--i; i >= 0; --i) {
2644 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002645 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002646 }
2647
2648 dma_ops_free_addresses(dma_dom, address, pages);
2649
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002650 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002651}
2652
Joerg Roedel431b2a22008-07-11 17:14:22 +02002653/*
2654 * Does the reverse of the __map_single function. Must be called with
2655 * the domain lock held too
2656 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002657static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002658 dma_addr_t dma_addr,
2659 size_t size,
2660 int dir)
2661{
Joerg Roedel04e04632010-09-23 16:12:48 +02002662 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002663 dma_addr_t i, start;
2664 unsigned int pages;
2665
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002666 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002667 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002668 return;
2669
Joerg Roedel04e04632010-09-23 16:12:48 +02002670 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002671 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002672 dma_addr &= PAGE_MASK;
2673 start = dma_addr;
2674
2675 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002676 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002677 start += PAGE_SIZE;
2678 }
2679
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002680 SUB_STATS_COUNTER(alloced_io_mem, size);
2681
Joerg Roedelcb76c322008-06-26 21:28:00 +02002682 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002683
Joerg Roedel80be3082008-11-06 14:59:05 +01002684 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002685 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002686 dma_dom->need_flush = false;
2687 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002688}
2689
Joerg Roedel431b2a22008-07-11 17:14:22 +02002690/*
2691 * The exported map_single function for dma_ops.
2692 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002693static dma_addr_t map_page(struct device *dev, struct page *page,
2694 unsigned long offset, size_t size,
2695 enum dma_data_direction dir,
2696 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002697{
2698 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002699 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002700 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002701 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002702 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002703
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002704 INC_STATS_COUNTER(cnt_map_single);
2705
Joerg Roedel94f6d192009-11-24 16:40:02 +01002706 domain = get_domain(dev);
2707 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002708 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002709 else if (IS_ERR(domain))
2710 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002711
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002712 dma_mask = *dev->dma_mask;
2713
Joerg Roedel4da70b92008-06-26 21:28:01 +02002714 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002715
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002716 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002717 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002718 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002719 goto out;
2720
Joerg Roedel17b124b2011-04-06 18:01:35 +02002721 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002722
2723out:
2724 spin_unlock_irqrestore(&domain->lock, flags);
2725
2726 return addr;
2727}
2728
Joerg Roedel431b2a22008-07-11 17:14:22 +02002729/*
2730 * The exported unmap_single function for dma_ops.
2731 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002732static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2733 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002734{
2735 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002736 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002737
Joerg Roedel146a6912008-12-12 15:07:12 +01002738 INC_STATS_COUNTER(cnt_unmap_single);
2739
Joerg Roedel94f6d192009-11-24 16:40:02 +01002740 domain = get_domain(dev);
2741 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002742 return;
2743
Joerg Roedel4da70b92008-06-26 21:28:01 +02002744 spin_lock_irqsave(&domain->lock, flags);
2745
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002746 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002747
Joerg Roedel17b124b2011-04-06 18:01:35 +02002748 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002749
2750 spin_unlock_irqrestore(&domain->lock, flags);
2751}
2752
Joerg Roedel431b2a22008-07-11 17:14:22 +02002753/*
2754 * This is a special map_sg function which is used if we should map a
2755 * device which is not handled by an AMD IOMMU in the system.
2756 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002757static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2758 int nelems, int dir)
2759{
2760 struct scatterlist *s;
2761 int i;
2762
2763 for_each_sg(sglist, s, nelems, i) {
2764 s->dma_address = (dma_addr_t)sg_phys(s);
2765 s->dma_length = s->length;
2766 }
2767
2768 return nelems;
2769}
2770
Joerg Roedel431b2a22008-07-11 17:14:22 +02002771/*
2772 * The exported map_sg function for dma_ops (handles scatter-gather
2773 * lists).
2774 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002775static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002776 int nelems, enum dma_data_direction dir,
2777 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002778{
2779 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002780 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002781 int i;
2782 struct scatterlist *s;
2783 phys_addr_t paddr;
2784 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002785 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002786
Joerg Roedeld03f0672008-12-12 15:09:48 +01002787 INC_STATS_COUNTER(cnt_map_sg);
2788
Joerg Roedel94f6d192009-11-24 16:40:02 +01002789 domain = get_domain(dev);
2790 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002791 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002792 else if (IS_ERR(domain))
2793 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002794
Joerg Roedel832a90c2008-09-18 15:54:23 +02002795 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002796
Joerg Roedel65b050a2008-06-26 21:28:02 +02002797 spin_lock_irqsave(&domain->lock, flags);
2798
2799 for_each_sg(sglist, s, nelems, i) {
2800 paddr = sg_phys(s);
2801
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002802 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002803 paddr, s->length, dir, false,
2804 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002805
2806 if (s->dma_address) {
2807 s->dma_length = s->length;
2808 mapped_elems++;
2809 } else
2810 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002811 }
2812
Joerg Roedel17b124b2011-04-06 18:01:35 +02002813 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002814
2815out:
2816 spin_unlock_irqrestore(&domain->lock, flags);
2817
2818 return mapped_elems;
2819unmap:
2820 for_each_sg(sglist, s, mapped_elems, i) {
2821 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002822 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002823 s->dma_length, dir);
2824 s->dma_address = s->dma_length = 0;
2825 }
2826
2827 mapped_elems = 0;
2828
2829 goto out;
2830}
2831
Joerg Roedel431b2a22008-07-11 17:14:22 +02002832/*
2833 * The exported map_sg function for dma_ops (handles scatter-gather
2834 * lists).
2835 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002836static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002837 int nelems, enum dma_data_direction dir,
2838 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002839{
2840 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002841 struct protection_domain *domain;
2842 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002843 int i;
2844
Joerg Roedel55877a62008-12-12 15:12:14 +01002845 INC_STATS_COUNTER(cnt_unmap_sg);
2846
Joerg Roedel94f6d192009-11-24 16:40:02 +01002847 domain = get_domain(dev);
2848 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002849 return;
2850
Joerg Roedel65b050a2008-06-26 21:28:02 +02002851 spin_lock_irqsave(&domain->lock, flags);
2852
2853 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002854 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002855 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002856 s->dma_address = s->dma_length = 0;
2857 }
2858
Joerg Roedel17b124b2011-04-06 18:01:35 +02002859 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002860
2861 spin_unlock_irqrestore(&domain->lock, flags);
2862}
2863
Joerg Roedel431b2a22008-07-11 17:14:22 +02002864/*
2865 * The exported alloc_coherent function for dma_ops.
2866 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002867static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002868 dma_addr_t *dma_addr, gfp_t flag,
2869 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002870{
2871 unsigned long flags;
2872 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002873 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002874 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002875 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002876
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002877 INC_STATS_COUNTER(cnt_alloc_coherent);
2878
Joerg Roedel94f6d192009-11-24 16:40:02 +01002879 domain = get_domain(dev);
2880 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002881 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2882 *dma_addr = __pa(virt_addr);
2883 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002884 } else if (IS_ERR(domain))
2885 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002886
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002887 dma_mask = dev->coherent_dma_mask;
2888 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2889 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002890
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002891 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2892 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302893 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002894
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002895 paddr = virt_to_phys(virt_addr);
2896
Joerg Roedel832a90c2008-09-18 15:54:23 +02002897 if (!dma_mask)
2898 dma_mask = *dev->dma_mask;
2899
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002900 spin_lock_irqsave(&domain->lock, flags);
2901
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002902 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002903 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002904
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002905 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002906 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002907 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002908 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002909
Joerg Roedel17b124b2011-04-06 18:01:35 +02002910 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002911
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002912 spin_unlock_irqrestore(&domain->lock, flags);
2913
2914 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002915
2916out_free:
2917
2918 free_pages((unsigned long)virt_addr, get_order(size));
2919
2920 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002921}
2922
Joerg Roedel431b2a22008-07-11 17:14:22 +02002923/*
2924 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002925 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002926static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002927 void *virt_addr, dma_addr_t dma_addr,
2928 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002929{
2930 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002931 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002932
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002933 INC_STATS_COUNTER(cnt_free_coherent);
2934
Joerg Roedel94f6d192009-11-24 16:40:02 +01002935 domain = get_domain(dev);
2936 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002937 goto free_mem;
2938
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002939 spin_lock_irqsave(&domain->lock, flags);
2940
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002941 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002942
Joerg Roedel17b124b2011-04-06 18:01:35 +02002943 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002944
2945 spin_unlock_irqrestore(&domain->lock, flags);
2946
2947free_mem:
2948 free_pages((unsigned long)virt_addr, get_order(size));
2949}
2950
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002951/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002952 * This function is called by the DMA layer to find out if we can handle a
2953 * particular device. It is part of the dma_ops.
2954 */
2955static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2956{
Joerg Roedel420aef82009-11-23 16:14:57 +01002957 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002958}
2959
2960/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002961 * The function for pre-allocating protection domains.
2962 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002963 * If the driver core informs the DMA layer if a driver grabs a device
2964 * we don't need to preallocate the protection domains anymore.
2965 * For now we have to.
2966 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01002967static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002968{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002969 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002970 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002971 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002972 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002973
Chris Wrightd18c69d2010-04-02 18:27:55 -07002974 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002975
2976 /* Do we handle this device? */
2977 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002978 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002979
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002980 dev_data = get_dev_data(&dev->dev);
2981 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2982 /* Make sure passthrough domain is allocated */
2983 alloc_passthrough_domain();
2984 dev_data->passthrough = true;
2985 attach_device(&dev->dev, pt_domain);
Frank Arnolddf805ab2012-08-27 19:21:04 +02002986 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002987 dev_name(&dev->dev));
2988 }
2989
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002990 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002991 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002992 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002993
2994 devid = get_device_id(&dev->dev);
2995
Joerg Roedel87a64d52009-11-24 17:26:43 +01002996 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002997 if (!dma_dom)
2998 continue;
2999 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02003000 dma_dom->target_dev = devid;
3001
Joerg Roedel15898bb2009-11-24 15:39:42 +01003002 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003003
Joerg Roedelbd60b732008-09-11 10:24:48 +02003004 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003005 }
3006}
3007
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003008static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003009 .alloc = alloc_coherent,
3010 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003011 .map_page = map_page,
3012 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003013 .map_sg = map_sg,
3014 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003015 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003016};
3017
Joerg Roedel27c21272011-05-30 15:56:24 +02003018static unsigned device_dma_ops_init(void)
3019{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003020 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003021 struct pci_dev *pdev = NULL;
3022 unsigned unhandled = 0;
3023
3024 for_each_pci_dev(pdev) {
3025 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003026
3027 iommu_ignore_device(&pdev->dev);
3028
Joerg Roedel27c21272011-05-30 15:56:24 +02003029 unhandled += 1;
3030 continue;
3031 }
3032
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003033 dev_data = get_dev_data(&pdev->dev);
3034
3035 if (!dev_data->passthrough)
3036 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3037 else
3038 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003039 }
3040
3041 return unhandled;
3042}
3043
Joerg Roedel431b2a22008-07-11 17:14:22 +02003044/*
3045 * The function which clues the AMD IOMMU driver into dma_ops.
3046 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003047
3048void __init amd_iommu_init_api(void)
3049{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003050 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003051}
3052
Joerg Roedel6631ee92008-06-26 21:28:05 +02003053int __init amd_iommu_init_dma_ops(void)
3054{
3055 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003056 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003057
Joerg Roedel431b2a22008-07-11 17:14:22 +02003058 /*
3059 * first allocate a default protection domain for every IOMMU we
3060 * found in the system. Devices not assigned to any other
3061 * protection domain will be assigned to the default one.
3062 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003063 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003064 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003065 if (iommu->default_dom == NULL)
3066 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003067 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003068 ret = iommu_init_unity_mappings(iommu);
3069 if (ret)
3070 goto free_domains;
3071 }
3072
Joerg Roedel431b2a22008-07-11 17:14:22 +02003073 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003074 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003075 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003076 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003077
3078 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003079 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003080
Joerg Roedel431b2a22008-07-11 17:14:22 +02003081 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003082 unhandled = device_dma_ops_init();
3083 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3084 /* There are unhandled devices - initialize swiotlb for them */
3085 swiotlb = 1;
3086 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003087
Joerg Roedel7f265082008-12-12 13:50:21 +01003088 amd_iommu_stats_init();
3089
Joerg Roedel62410ee2012-06-12 16:42:43 +02003090 if (amd_iommu_unmap_flush)
3091 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3092 else
3093 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3094
Joerg Roedel6631ee92008-06-26 21:28:05 +02003095 return 0;
3096
3097free_domains:
3098
Joerg Roedel3bd22172009-05-04 15:06:20 +02003099 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003100 if (iommu->default_dom)
3101 dma_ops_domain_free(iommu->default_dom);
3102 }
3103
3104 return ret;
3105}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003106
3107/*****************************************************************************
3108 *
3109 * The following functions belong to the exported interface of AMD IOMMU
3110 *
3111 * This interface allows access to lower level functions of the IOMMU
3112 * like protection domain handling and assignement of devices to domains
3113 * which is not possible with the dma_ops interface.
3114 *
3115 *****************************************************************************/
3116
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003117static void cleanup_domain(struct protection_domain *domain)
3118{
Joerg Roedel492667d2009-11-27 13:25:47 +01003119 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003120 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003121
3122 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3123
Joerg Roedel492667d2009-11-27 13:25:47 +01003124 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003125 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003126 atomic_set(&dev_data->bind, 0);
3127 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003128
3129 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3130}
3131
Joerg Roedel26508152009-08-26 16:52:40 +02003132static void protection_domain_free(struct protection_domain *domain)
3133{
3134 if (!domain)
3135 return;
3136
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003137 del_domain_from_list(domain);
3138
Joerg Roedel26508152009-08-26 16:52:40 +02003139 if (domain->id)
3140 domain_id_free(domain->id);
3141
3142 kfree(domain);
3143}
3144
3145static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003146{
3147 struct protection_domain *domain;
3148
3149 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3150 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003151 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003152
3153 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003154 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003155 domain->id = domain_id_alloc();
3156 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003157 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003158 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003159
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003160 add_domain_to_list(domain);
3161
Joerg Roedel26508152009-08-26 16:52:40 +02003162 return domain;
3163
3164out_err:
3165 kfree(domain);
3166
3167 return NULL;
3168}
3169
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003170static int __init alloc_passthrough_domain(void)
3171{
3172 if (pt_domain != NULL)
3173 return 0;
3174
3175 /* allocate passthrough domain */
3176 pt_domain = protection_domain_alloc();
3177 if (!pt_domain)
3178 return -ENOMEM;
3179
3180 pt_domain->mode = PAGE_MODE_NONE;
3181
3182 return 0;
3183}
Joerg Roedel26508152009-08-26 16:52:40 +02003184static int amd_iommu_domain_init(struct iommu_domain *dom)
3185{
3186 struct protection_domain *domain;
3187
3188 domain = protection_domain_alloc();
3189 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003190 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003191
3192 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003193 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3194 if (!domain->pt_root)
3195 goto out_free;
3196
Joerg Roedelf3572db2011-11-23 12:36:25 +01003197 domain->iommu_domain = dom;
3198
Joerg Roedelc156e342008-12-02 18:13:27 +01003199 dom->priv = domain;
3200
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003201 dom->geometry.aperture_start = 0;
3202 dom->geometry.aperture_end = ~0ULL;
3203 dom->geometry.force_aperture = true;
3204
Joerg Roedelc156e342008-12-02 18:13:27 +01003205 return 0;
3206
3207out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003208 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003209
3210 return -ENOMEM;
3211}
3212
Joerg Roedel98383fc2008-12-02 18:34:12 +01003213static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3214{
3215 struct protection_domain *domain = dom->priv;
3216
3217 if (!domain)
3218 return;
3219
3220 if (domain->dev_cnt > 0)
3221 cleanup_domain(domain);
3222
3223 BUG_ON(domain->dev_cnt != 0);
3224
Joerg Roedel132bd682011-11-17 14:18:46 +01003225 if (domain->mode != PAGE_MODE_NONE)
3226 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003227
Joerg Roedel52815b72011-11-17 17:24:28 +01003228 if (domain->flags & PD_IOMMUV2_MASK)
3229 free_gcr3_table(domain);
3230
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003231 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003232
3233 dom->priv = NULL;
3234}
3235
Joerg Roedel684f2882008-12-08 12:07:44 +01003236static void amd_iommu_detach_device(struct iommu_domain *dom,
3237 struct device *dev)
3238{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003239 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003240 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003241 u16 devid;
3242
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003243 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003244 return;
3245
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003246 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003247
Joerg Roedel657cbb62009-11-23 15:26:46 +01003248 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003249 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003250
3251 iommu = amd_iommu_rlookup_table[devid];
3252 if (!iommu)
3253 return;
3254
Joerg Roedel684f2882008-12-08 12:07:44 +01003255 iommu_completion_wait(iommu);
3256}
3257
Joerg Roedel01106062008-12-02 19:34:11 +01003258static int amd_iommu_attach_device(struct iommu_domain *dom,
3259 struct device *dev)
3260{
3261 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003262 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003263 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003264 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003265
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003266 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003267 return -EINVAL;
3268
Joerg Roedel657cbb62009-11-23 15:26:46 +01003269 dev_data = dev->archdata.iommu;
3270
Joerg Roedelf62dda62011-06-09 12:55:35 +02003271 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003272 if (!iommu)
3273 return -EINVAL;
3274
Joerg Roedel657cbb62009-11-23 15:26:46 +01003275 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003276 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003277
Joerg Roedel15898bb2009-11-24 15:39:42 +01003278 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003279
3280 iommu_completion_wait(iommu);
3281
Joerg Roedel15898bb2009-11-24 15:39:42 +01003282 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003283}
3284
Joerg Roedel468e2362010-01-21 16:37:36 +01003285static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003286 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003287{
3288 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003289 int prot = 0;
3290 int ret;
3291
Joerg Roedel132bd682011-11-17 14:18:46 +01003292 if (domain->mode == PAGE_MODE_NONE)
3293 return -EINVAL;
3294
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003295 if (iommu_prot & IOMMU_READ)
3296 prot |= IOMMU_PROT_IR;
3297 if (iommu_prot & IOMMU_WRITE)
3298 prot |= IOMMU_PROT_IW;
3299
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003300 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003301 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003302 mutex_unlock(&domain->api_lock);
3303
Joerg Roedel795e74f72010-05-11 17:40:57 +02003304 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003305}
3306
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003307static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3308 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003309{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003310 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003311 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003312
Joerg Roedel132bd682011-11-17 14:18:46 +01003313 if (domain->mode == PAGE_MODE_NONE)
3314 return -EINVAL;
3315
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003316 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003317 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003318 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003319
Joerg Roedel17b124b2011-04-06 18:01:35 +02003320 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003321
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003322 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003323}
3324
Joerg Roedel645c4c82008-12-02 20:05:50 +01003325static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3326 unsigned long iova)
3327{
3328 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003329 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003330 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003331 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003332
Joerg Roedel132bd682011-11-17 14:18:46 +01003333 if (domain->mode == PAGE_MODE_NONE)
3334 return iova;
3335
Joerg Roedel24cd7722010-01-19 17:27:39 +01003336 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003337
Joerg Roedela6d41a42009-09-02 17:08:55 +02003338 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003339 return 0;
3340
Joerg Roedelf03152b2010-01-21 16:15:24 +01003341 if (PM_PTE_LEVEL(*pte) == 0)
3342 offset_mask = PAGE_SIZE - 1;
3343 else
3344 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3345
3346 __pte = *pte & PM_ADDR_MASK;
3347 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003348
3349 return paddr;
3350}
3351
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003352static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3353 unsigned long cap)
3354{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003355 switch (cap) {
3356 case IOMMU_CAP_CACHE_COHERENCY:
3357 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003358 case IOMMU_CAP_INTR_REMAP:
3359 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003360 }
3361
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003362 return 0;
3363}
3364
Joerg Roedel26961ef2008-12-03 17:00:17 +01003365static struct iommu_ops amd_iommu_ops = {
3366 .domain_init = amd_iommu_domain_init,
3367 .domain_destroy = amd_iommu_domain_destroy,
3368 .attach_dev = amd_iommu_attach_device,
3369 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003370 .map = amd_iommu_map,
3371 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003372 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003373 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003374 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003375};
3376
Joerg Roedel0feae532009-08-26 15:26:30 +02003377/*****************************************************************************
3378 *
3379 * The next functions do a basic initialization of IOMMU for pass through
3380 * mode
3381 *
3382 * In passthrough mode the IOMMU is initialized and enabled but not used for
3383 * DMA-API translation.
3384 *
3385 *****************************************************************************/
3386
3387int __init amd_iommu_init_passthrough(void)
3388{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003389 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003390 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003391 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003392 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003393 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003394
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003395 ret = alloc_passthrough_domain();
3396 if (ret)
3397 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003398
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003399 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003400 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003401 continue;
3402
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003403 dev_data = get_dev_data(&dev->dev);
3404 dev_data->passthrough = true;
3405
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003406 devid = get_device_id(&dev->dev);
3407
Joerg Roedel15898bb2009-11-24 15:39:42 +01003408 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003409 if (!iommu)
3410 continue;
3411
Joerg Roedel15898bb2009-11-24 15:39:42 +01003412 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003413 }
3414
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003415 amd_iommu_stats_init();
3416
Joerg Roedel0feae532009-08-26 15:26:30 +02003417 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3418
3419 return 0;
3420}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003421
3422/* IOMMUv2 specific functions */
3423int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3424{
3425 return atomic_notifier_chain_register(&ppr_notifier, nb);
3426}
3427EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3428
3429int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3430{
3431 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3432}
3433EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003434
3435void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3436{
3437 struct protection_domain *domain = dom->priv;
3438 unsigned long flags;
3439
3440 spin_lock_irqsave(&domain->lock, flags);
3441
3442 /* Update data structure */
3443 domain->mode = PAGE_MODE_NONE;
3444 domain->updated = true;
3445
3446 /* Make changes visible to IOMMUs */
3447 update_domain(domain);
3448
3449 /* Page-table is not visible to IOMMU anymore, so free it */
3450 free_pagetable(domain);
3451
3452 spin_unlock_irqrestore(&domain->lock, flags);
3453}
3454EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003455
3456int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3457{
3458 struct protection_domain *domain = dom->priv;
3459 unsigned long flags;
3460 int levels, ret;
3461
3462 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3463 return -EINVAL;
3464
3465 /* Number of GCR3 table levels required */
3466 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3467 levels += 1;
3468
3469 if (levels > amd_iommu_max_glx_val)
3470 return -EINVAL;
3471
3472 spin_lock_irqsave(&domain->lock, flags);
3473
3474 /*
3475 * Save us all sanity checks whether devices already in the
3476 * domain support IOMMUv2. Just force that the domain has no
3477 * devices attached when it is switched into IOMMUv2 mode.
3478 */
3479 ret = -EBUSY;
3480 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3481 goto out;
3482
3483 ret = -ENOMEM;
3484 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3485 if (domain->gcr3_tbl == NULL)
3486 goto out;
3487
3488 domain->glx = levels;
3489 domain->flags |= PD_IOMMUV2_MASK;
3490 domain->updated = true;
3491
3492 update_domain(domain);
3493
3494 ret = 0;
3495
3496out:
3497 spin_unlock_irqrestore(&domain->lock, flags);
3498
3499 return ret;
3500}
3501EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003502
3503static int __flush_pasid(struct protection_domain *domain, int pasid,
3504 u64 address, bool size)
3505{
3506 struct iommu_dev_data *dev_data;
3507 struct iommu_cmd cmd;
3508 int i, ret;
3509
3510 if (!(domain->flags & PD_IOMMUV2_MASK))
3511 return -EINVAL;
3512
3513 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3514
3515 /*
3516 * IOMMU TLB needs to be flushed before Device TLB to
3517 * prevent device TLB refill from IOMMU TLB
3518 */
3519 for (i = 0; i < amd_iommus_present; ++i) {
3520 if (domain->dev_iommu[i] == 0)
3521 continue;
3522
3523 ret = iommu_queue_command(amd_iommus[i], &cmd);
3524 if (ret != 0)
3525 goto out;
3526 }
3527
3528 /* Wait until IOMMU TLB flushes are complete */
3529 domain_flush_complete(domain);
3530
3531 /* Now flush device TLBs */
3532 list_for_each_entry(dev_data, &domain->dev_list, list) {
3533 struct amd_iommu *iommu;
3534 int qdep;
3535
3536 BUG_ON(!dev_data->ats.enabled);
3537
3538 qdep = dev_data->ats.qdep;
3539 iommu = amd_iommu_rlookup_table[dev_data->devid];
3540
3541 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3542 qdep, address, size);
3543
3544 ret = iommu_queue_command(iommu, &cmd);
3545 if (ret != 0)
3546 goto out;
3547 }
3548
3549 /* Wait until all device TLBs are flushed */
3550 domain_flush_complete(domain);
3551
3552 ret = 0;
3553
3554out:
3555
3556 return ret;
3557}
3558
3559static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3560 u64 address)
3561{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003562 INC_STATS_COUNTER(invalidate_iotlb);
3563
Joerg Roedel22e266c2011-11-21 15:59:08 +01003564 return __flush_pasid(domain, pasid, address, false);
3565}
3566
3567int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3568 u64 address)
3569{
3570 struct protection_domain *domain = dom->priv;
3571 unsigned long flags;
3572 int ret;
3573
3574 spin_lock_irqsave(&domain->lock, flags);
3575 ret = __amd_iommu_flush_page(domain, pasid, address);
3576 spin_unlock_irqrestore(&domain->lock, flags);
3577
3578 return ret;
3579}
3580EXPORT_SYMBOL(amd_iommu_flush_page);
3581
3582static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3583{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003584 INC_STATS_COUNTER(invalidate_iotlb_all);
3585
Joerg Roedel22e266c2011-11-21 15:59:08 +01003586 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3587 true);
3588}
3589
3590int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3591{
3592 struct protection_domain *domain = dom->priv;
3593 unsigned long flags;
3594 int ret;
3595
3596 spin_lock_irqsave(&domain->lock, flags);
3597 ret = __amd_iommu_flush_tlb(domain, pasid);
3598 spin_unlock_irqrestore(&domain->lock, flags);
3599
3600 return ret;
3601}
3602EXPORT_SYMBOL(amd_iommu_flush_tlb);
3603
Joerg Roedelb16137b2011-11-21 16:50:23 +01003604static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3605{
3606 int index;
3607 u64 *pte;
3608
3609 while (true) {
3610
3611 index = (pasid >> (9 * level)) & 0x1ff;
3612 pte = &root[index];
3613
3614 if (level == 0)
3615 break;
3616
3617 if (!(*pte & GCR3_VALID)) {
3618 if (!alloc)
3619 return NULL;
3620
3621 root = (void *)get_zeroed_page(GFP_ATOMIC);
3622 if (root == NULL)
3623 return NULL;
3624
3625 *pte = __pa(root) | GCR3_VALID;
3626 }
3627
3628 root = __va(*pte & PAGE_MASK);
3629
3630 level -= 1;
3631 }
3632
3633 return pte;
3634}
3635
3636static int __set_gcr3(struct protection_domain *domain, int pasid,
3637 unsigned long cr3)
3638{
3639 u64 *pte;
3640
3641 if (domain->mode != PAGE_MODE_NONE)
3642 return -EINVAL;
3643
3644 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3645 if (pte == NULL)
3646 return -ENOMEM;
3647
3648 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3649
3650 return __amd_iommu_flush_tlb(domain, pasid);
3651}
3652
3653static int __clear_gcr3(struct protection_domain *domain, int pasid)
3654{
3655 u64 *pte;
3656
3657 if (domain->mode != PAGE_MODE_NONE)
3658 return -EINVAL;
3659
3660 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3661 if (pte == NULL)
3662 return 0;
3663
3664 *pte = 0;
3665
3666 return __amd_iommu_flush_tlb(domain, pasid);
3667}
3668
3669int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3670 unsigned long cr3)
3671{
3672 struct protection_domain *domain = dom->priv;
3673 unsigned long flags;
3674 int ret;
3675
3676 spin_lock_irqsave(&domain->lock, flags);
3677 ret = __set_gcr3(domain, pasid, cr3);
3678 spin_unlock_irqrestore(&domain->lock, flags);
3679
3680 return ret;
3681}
3682EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3683
3684int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3685{
3686 struct protection_domain *domain = dom->priv;
3687 unsigned long flags;
3688 int ret;
3689
3690 spin_lock_irqsave(&domain->lock, flags);
3691 ret = __clear_gcr3(domain, pasid);
3692 spin_unlock_irqrestore(&domain->lock, flags);
3693
3694 return ret;
3695}
3696EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003697
3698int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3699 int status, int tag)
3700{
3701 struct iommu_dev_data *dev_data;
3702 struct amd_iommu *iommu;
3703 struct iommu_cmd cmd;
3704
Joerg Roedel399be2f2011-12-01 16:53:47 +01003705 INC_STATS_COUNTER(complete_ppr);
3706
Joerg Roedelc99afa22011-11-21 18:19:25 +01003707 dev_data = get_dev_data(&pdev->dev);
3708 iommu = amd_iommu_rlookup_table[dev_data->devid];
3709
3710 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3711 tag, dev_data->pri_tlp);
3712
3713 return iommu_queue_command(iommu, &cmd);
3714}
3715EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003716
3717struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3718{
3719 struct protection_domain *domain;
3720
3721 domain = get_domain(&pdev->dev);
3722 if (IS_ERR(domain))
3723 return NULL;
3724
3725 /* Only return IOMMUv2 domains */
3726 if (!(domain->flags & PD_IOMMUV2_MASK))
3727 return NULL;
3728
3729 return domain->iommu_domain;
3730}
3731EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003732
3733void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3734{
3735 struct iommu_dev_data *dev_data;
3736
3737 if (!amd_iommu_v2_supported())
3738 return;
3739
3740 dev_data = get_dev_data(&pdev->dev);
3741 dev_data->errata |= (1 << erratum);
3742}
3743EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003744
3745int amd_iommu_device_info(struct pci_dev *pdev,
3746 struct amd_iommu_device_info *info)
3747{
3748 int max_pasids;
3749 int pos;
3750
3751 if (pdev == NULL || info == NULL)
3752 return -EINVAL;
3753
3754 if (!amd_iommu_v2_supported())
3755 return -EINVAL;
3756
3757 memset(info, 0, sizeof(*info));
3758
3759 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3760 if (pos)
3761 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3762
3763 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3764 if (pos)
3765 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3766
3767 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3768 if (pos) {
3769 int features;
3770
3771 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3772 max_pasids = min(max_pasids, (1 << 20));
3773
3774 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3775 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3776
3777 features = pci_pasid_features(pdev);
3778 if (features & PCI_PASID_CAP_EXEC)
3779 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3780 if (features & PCI_PASID_CAP_PRIV)
3781 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3782 }
3783
3784 return 0;
3785}
3786EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003787
3788#ifdef CONFIG_IRQ_REMAP
3789
3790/*****************************************************************************
3791 *
3792 * Interrupt Remapping Implementation
3793 *
3794 *****************************************************************************/
3795
3796union irte {
3797 u32 val;
3798 struct {
3799 u32 valid : 1,
3800 no_fault : 1,
3801 int_type : 3,
3802 rq_eoi : 1,
3803 dm : 1,
3804 rsvd_1 : 1,
3805 destination : 8,
3806 vector : 8,
3807 rsvd_2 : 8;
3808 } fields;
3809};
3810
3811#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3812#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3813#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3814#define DTE_IRQ_REMAP_ENABLE 1ULL
3815
3816static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3817{
3818 u64 dte;
3819
3820 dte = amd_iommu_dev_table[devid].data[2];
3821 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3822 dte |= virt_to_phys(table->table);
3823 dte |= DTE_IRQ_REMAP_INTCTL;
3824 dte |= DTE_IRQ_TABLE_LEN;
3825 dte |= DTE_IRQ_REMAP_ENABLE;
3826
3827 amd_iommu_dev_table[devid].data[2] = dte;
3828}
3829
3830#define IRTE_ALLOCATED (~1U)
3831
3832static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3833{
3834 struct irq_remap_table *table = NULL;
3835 struct amd_iommu *iommu;
3836 unsigned long flags;
3837 u16 alias;
3838
3839 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3840
3841 iommu = amd_iommu_rlookup_table[devid];
3842 if (!iommu)
3843 goto out_unlock;
3844
3845 table = irq_lookup_table[devid];
3846 if (table)
3847 goto out;
3848
3849 alias = amd_iommu_alias_table[devid];
3850 table = irq_lookup_table[alias];
3851 if (table) {
3852 irq_lookup_table[devid] = table;
3853 set_dte_irq_entry(devid, table);
3854 iommu_flush_dte(iommu, devid);
3855 goto out;
3856 }
3857
3858 /* Nothing there yet, allocate new irq remapping table */
3859 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3860 if (!table)
3861 goto out;
3862
3863 if (ioapic)
3864 /* Keep the first 32 indexes free for IOAPIC interrupts */
3865 table->min_index = 32;
3866
3867 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3868 if (!table->table) {
3869 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003870 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003871 goto out;
3872 }
3873
3874 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3875
3876 if (ioapic) {
3877 int i;
3878
3879 for (i = 0; i < 32; ++i)
3880 table->table[i] = IRTE_ALLOCATED;
3881 }
3882
3883 irq_lookup_table[devid] = table;
3884 set_dte_irq_entry(devid, table);
3885 iommu_flush_dte(iommu, devid);
3886 if (devid != alias) {
3887 irq_lookup_table[alias] = table;
3888 set_dte_irq_entry(devid, table);
3889 iommu_flush_dte(iommu, alias);
3890 }
3891
3892out:
3893 iommu_completion_wait(iommu);
3894
3895out_unlock:
3896 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3897
3898 return table;
3899}
3900
3901static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3902{
3903 struct irq_remap_table *table;
3904 unsigned long flags;
3905 int index, c;
3906
3907 table = get_irq_table(devid, false);
3908 if (!table)
3909 return -ENODEV;
3910
3911 spin_lock_irqsave(&table->lock, flags);
3912
3913 /* Scan table for free entries */
3914 for (c = 0, index = table->min_index;
3915 index < MAX_IRQS_PER_TABLE;
3916 ++index) {
3917 if (table->table[index] == 0)
3918 c += 1;
3919 else
3920 c = 0;
3921
3922 if (c == count) {
3923 struct irq_2_iommu *irte_info;
3924
3925 for (; c != 0; --c)
3926 table->table[index - c + 1] = IRTE_ALLOCATED;
3927
3928 index -= count - 1;
3929
3930 irte_info = &cfg->irq_2_iommu;
3931 irte_info->sub_handle = devid;
3932 irte_info->irte_index = index;
3933 irte_info->iommu = (void *)cfg;
3934
3935 goto out;
3936 }
3937 }
3938
3939 index = -ENOSPC;
3940
3941out:
3942 spin_unlock_irqrestore(&table->lock, flags);
3943
3944 return index;
3945}
3946
3947static int get_irte(u16 devid, int index, union irte *irte)
3948{
3949 struct irq_remap_table *table;
3950 unsigned long flags;
3951
3952 table = get_irq_table(devid, false);
3953 if (!table)
3954 return -ENOMEM;
3955
3956 spin_lock_irqsave(&table->lock, flags);
3957 irte->val = table->table[index];
3958 spin_unlock_irqrestore(&table->lock, flags);
3959
3960 return 0;
3961}
3962
3963static int modify_irte(u16 devid, int index, union irte irte)
3964{
3965 struct irq_remap_table *table;
3966 struct amd_iommu *iommu;
3967 unsigned long flags;
3968
3969 iommu = amd_iommu_rlookup_table[devid];
3970 if (iommu == NULL)
3971 return -EINVAL;
3972
3973 table = get_irq_table(devid, false);
3974 if (!table)
3975 return -ENOMEM;
3976
3977 spin_lock_irqsave(&table->lock, flags);
3978 table->table[index] = irte.val;
3979 spin_unlock_irqrestore(&table->lock, flags);
3980
3981 iommu_flush_irt(iommu, devid);
3982 iommu_completion_wait(iommu);
3983
3984 return 0;
3985}
3986
3987static void free_irte(u16 devid, int index)
3988{
3989 struct irq_remap_table *table;
3990 struct amd_iommu *iommu;
3991 unsigned long flags;
3992
3993 iommu = amd_iommu_rlookup_table[devid];
3994 if (iommu == NULL)
3995 return;
3996
3997 table = get_irq_table(devid, false);
3998 if (!table)
3999 return;
4000
4001 spin_lock_irqsave(&table->lock, flags);
4002 table->table[index] = 0;
4003 spin_unlock_irqrestore(&table->lock, flags);
4004
4005 iommu_flush_irt(iommu, devid);
4006 iommu_completion_wait(iommu);
4007}
4008
Joerg Roedel5527de72012-06-26 11:17:32 +02004009static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4010 unsigned int destination, int vector,
4011 struct io_apic_irq_attr *attr)
4012{
4013 struct irq_remap_table *table;
4014 struct irq_2_iommu *irte_info;
4015 struct irq_cfg *cfg;
4016 union irte irte;
4017 int ioapic_id;
4018 int index;
4019 int devid;
4020 int ret;
4021
4022 cfg = irq_get_chip_data(irq);
4023 if (!cfg)
4024 return -EINVAL;
4025
4026 irte_info = &cfg->irq_2_iommu;
4027 ioapic_id = mpc_ioapic_id(attr->ioapic);
4028 devid = get_ioapic_devid(ioapic_id);
4029
4030 if (devid < 0)
4031 return devid;
4032
4033 table = get_irq_table(devid, true);
4034 if (table == NULL)
4035 return -ENOMEM;
4036
4037 index = attr->ioapic_pin;
4038
4039 /* Setup IRQ remapping info */
4040 irte_info->sub_handle = devid;
4041 irte_info->irte_index = index;
4042 irte_info->iommu = (void *)cfg;
4043
4044 /* Setup IRTE for IOMMU */
4045 irte.val = 0;
4046 irte.fields.vector = vector;
4047 irte.fields.int_type = apic->irq_delivery_mode;
4048 irte.fields.destination = destination;
4049 irte.fields.dm = apic->irq_dest_mode;
4050 irte.fields.valid = 1;
4051
4052 ret = modify_irte(devid, index, irte);
4053 if (ret)
4054 return ret;
4055
4056 /* Setup IOAPIC entry */
4057 memset(entry, 0, sizeof(*entry));
4058
4059 entry->vector = index;
4060 entry->mask = 0;
4061 entry->trigger = attr->trigger;
4062 entry->polarity = attr->polarity;
4063
4064 /*
4065 * Mask level triggered irqs.
Joerg Roedel5527de72012-06-26 11:17:32 +02004066 */
4067 if (attr->trigger)
4068 entry->mask = 1;
4069
4070 return 0;
4071}
4072
4073static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4074 bool force)
4075{
4076 struct irq_2_iommu *irte_info;
4077 unsigned int dest, irq;
4078 struct irq_cfg *cfg;
4079 union irte irte;
4080 int err;
4081
4082 if (!config_enabled(CONFIG_SMP))
4083 return -1;
4084
4085 cfg = data->chip_data;
4086 irq = data->irq;
4087 irte_info = &cfg->irq_2_iommu;
4088
4089 if (!cpumask_intersects(mask, cpu_online_mask))
4090 return -EINVAL;
4091
4092 if (get_irte(irte_info->sub_handle, irte_info->irte_index, &irte))
4093 return -EBUSY;
4094
4095 if (assign_irq_vector(irq, cfg, mask))
4096 return -EBUSY;
4097
4098 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4099 if (err) {
4100 if (assign_irq_vector(irq, cfg, data->affinity))
4101 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4102 return err;
4103 }
4104
4105 irte.fields.vector = cfg->vector;
4106 irte.fields.destination = dest;
4107
4108 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4109
4110 if (cfg->move_in_progress)
4111 send_cleanup_vector(cfg);
4112
4113 cpumask_copy(data->affinity, mask);
4114
4115 return 0;
4116}
4117
4118static int free_irq(int irq)
4119{
4120 struct irq_2_iommu *irte_info;
4121 struct irq_cfg *cfg;
4122
4123 cfg = irq_get_chip_data(irq);
4124 if (!cfg)
4125 return -EINVAL;
4126
4127 irte_info = &cfg->irq_2_iommu;
4128
4129 free_irte(irte_info->sub_handle, irte_info->irte_index);
4130
4131 return 0;
4132}
4133
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004134static void compose_msi_msg(struct pci_dev *pdev,
4135 unsigned int irq, unsigned int dest,
4136 struct msi_msg *msg, u8 hpet_id)
4137{
4138 struct irq_2_iommu *irte_info;
4139 struct irq_cfg *cfg;
4140 union irte irte;
4141
4142 cfg = irq_get_chip_data(irq);
4143 if (!cfg)
4144 return;
4145
4146 irte_info = &cfg->irq_2_iommu;
4147
4148 irte.val = 0;
4149 irte.fields.vector = cfg->vector;
4150 irte.fields.int_type = apic->irq_delivery_mode;
4151 irte.fields.destination = dest;
4152 irte.fields.dm = apic->irq_dest_mode;
4153 irte.fields.valid = 1;
4154
4155 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4156
4157 msg->address_hi = MSI_ADDR_BASE_HI;
4158 msg->address_lo = MSI_ADDR_BASE_LO;
4159 msg->data = irte_info->irte_index;
4160}
4161
4162static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4163{
4164 struct irq_cfg *cfg;
4165 int index;
4166 u16 devid;
4167
4168 if (!pdev)
4169 return -EINVAL;
4170
4171 cfg = irq_get_chip_data(irq);
4172 if (!cfg)
4173 return -EINVAL;
4174
4175 devid = get_device_id(&pdev->dev);
4176 index = alloc_irq_index(cfg, devid, nvec);
4177
4178 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4179}
4180
4181static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4182 int index, int offset)
4183{
4184 struct irq_2_iommu *irte_info;
4185 struct irq_cfg *cfg;
4186 u16 devid;
4187
4188 if (!pdev)
4189 return -EINVAL;
4190
4191 cfg = irq_get_chip_data(irq);
4192 if (!cfg)
4193 return -EINVAL;
4194
4195 if (index >= MAX_IRQS_PER_TABLE)
4196 return 0;
4197
4198 devid = get_device_id(&pdev->dev);
4199 irte_info = &cfg->irq_2_iommu;
4200
4201 irte_info->sub_handle = devid;
4202 irte_info->irte_index = index + offset;
4203 irte_info->iommu = (void *)cfg;
4204
4205 return 0;
4206}
4207
Joerg Roedeld9761952012-06-26 16:00:08 +02004208static int setup_hpet_msi(unsigned int irq, unsigned int id)
4209{
4210 struct irq_2_iommu *irte_info;
4211 struct irq_cfg *cfg;
4212 int index, devid;
4213
4214 cfg = irq_get_chip_data(irq);
4215 if (!cfg)
4216 return -EINVAL;
4217
4218 irte_info = &cfg->irq_2_iommu;
4219 devid = get_hpet_devid(id);
4220 if (devid < 0)
4221 return devid;
4222
4223 index = alloc_irq_index(cfg, devid, 1);
4224 if (index < 0)
4225 return index;
4226
4227 irte_info->sub_handle = devid;
4228 irte_info->irte_index = index;
4229 irte_info->iommu = (void *)cfg;
4230
4231 return 0;
4232}
4233
Joerg Roedel6b474b82012-06-26 16:46:04 +02004234struct irq_remap_ops amd_iommu_irq_ops = {
4235 .supported = amd_iommu_supported,
4236 .prepare = amd_iommu_prepare,
4237 .enable = amd_iommu_enable,
4238 .disable = amd_iommu_disable,
4239 .reenable = amd_iommu_reenable,
4240 .enable_faulting = amd_iommu_enable_faulting,
4241 .setup_ioapic_entry = setup_ioapic_entry,
4242 .set_affinity = set_affinity,
4243 .free_irq = free_irq,
4244 .compose_msi_msg = compose_msi_msg,
4245 .msi_alloc_irq = msi_alloc_irq,
4246 .msi_setup_irq = msi_setup_irq,
4247 .setup_hpet_msi = setup_hpet_msi,
4248};
Joerg Roedel2b324502012-06-21 16:29:10 +02004249#endif