blob: 312dd4dd47914e87e51c598091fa7b37f12e3f5c [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{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600281 struct pci_dev *dma_pdev, *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);
308 } else
309 dma_pdev = pci_dev_get(pdev);
310
Alex Williamson31fe9432012-08-04 12:09:03 -0600311 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600312 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
313
Alex Williamson31fe9432012-08-04 12:09:03 -0600314 /*
315 * If it's a multifunction device that does not support our
316 * required ACS flags, add to the same group as function 0.
317 */
Alex Williamson664b6002012-05-30 14:19:31 -0600318 if (dma_pdev->multifunction &&
319 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
320 swap_pci_ref(&dma_pdev,
321 pci_get_slot(dma_pdev->bus,
322 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
323 0)));
324
Alex Williamson31fe9432012-08-04 12:09:03 -0600325 /*
326 * Devices on the root bus go through the iommu. If that's not us,
327 * find the next upstream device and test ACS up to the root bus.
328 * Finding the next device may require skipping virtual buses.
329 */
Alex Williamson664b6002012-05-30 14:19:31 -0600330 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson31fe9432012-08-04 12:09:03 -0600331 struct pci_bus *bus = dma_pdev->bus;
332
333 while (!bus->self) {
334 if (!pci_is_root_bus(bus))
335 bus = bus->parent;
336 else
337 goto root_bus;
338 }
339
340 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600341 break;
342
Alex Williamson31fe9432012-08-04 12:09:03 -0600343 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200344 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100345
Alex Williamson31fe9432012-08-04 12:09:03 -0600346root_bus:
Alex Williamson9dcd6132012-05-30 14:19:07 -0600347 group = iommu_group_get(&dma_pdev->dev);
348 pci_dev_put(dma_pdev);
349 if (!group) {
350 group = iommu_group_alloc();
351 if (IS_ERR(group))
352 return PTR_ERR(group);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100353 }
354
Alex Williamson9dcd6132012-05-30 14:19:07 -0600355 ret = iommu_group_add_device(group, dev);
356
357 iommu_group_put(group);
358
359 if (ret)
360 return ret;
361
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100362 if (pci_iommuv2_capable(pdev)) {
363 struct amd_iommu *iommu;
364
365 iommu = amd_iommu_rlookup_table[dev_data->devid];
366 dev_data->iommu_v2 = iommu->is_iommu_v2;
367 }
368
Joerg Roedel657cbb62009-11-23 15:26:46 +0100369 dev->archdata.iommu = dev_data;
370
Joerg Roedel657cbb62009-11-23 15:26:46 +0100371 return 0;
372}
373
Joerg Roedel26018872011-06-06 16:50:14 +0200374static void iommu_ignore_device(struct device *dev)
375{
376 u16 devid, alias;
377
378 devid = get_device_id(dev);
379 alias = amd_iommu_alias_table[devid];
380
381 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
382 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
383
384 amd_iommu_rlookup_table[devid] = NULL;
385 amd_iommu_rlookup_table[alias] = NULL;
386}
387
Joerg Roedel657cbb62009-11-23 15:26:46 +0100388static void iommu_uninit_device(struct device *dev)
389{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600390 iommu_group_remove_device(dev);
391
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200392 /*
393 * Nothing to do here - we keep dev_data around for unplugged devices
394 * and reuse it when the device is re-plugged - not doing so would
395 * introduce a ton of races.
396 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100397}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100398
399void __init amd_iommu_uninit_devices(void)
400{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200401 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100402 struct pci_dev *pdev = NULL;
403
404 for_each_pci_dev(pdev) {
405
406 if (!check_device(&pdev->dev))
407 continue;
408
409 iommu_uninit_device(&pdev->dev);
410 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200411
412 /* Free all of our dev_data structures */
413 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
414 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100415}
416
417int __init amd_iommu_init_devices(void)
418{
419 struct pci_dev *pdev = NULL;
420 int ret = 0;
421
422 for_each_pci_dev(pdev) {
423
424 if (!check_device(&pdev->dev))
425 continue;
426
427 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200428 if (ret == -ENOTSUPP)
429 iommu_ignore_device(&pdev->dev);
430 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100431 goto out_free;
432 }
433
434 return 0;
435
436out_free:
437
438 amd_iommu_uninit_devices();
439
440 return ret;
441}
Joerg Roedel7f265082008-12-12 13:50:21 +0100442#ifdef CONFIG_AMD_IOMMU_STATS
443
444/*
445 * Initialization code for statistics collection
446 */
447
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100448DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100449DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100450DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100451DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100452DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100453DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100454DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100455DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100456DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100457DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100458DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100459DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100460DECLARE_STATS_COUNTER(complete_ppr);
461DECLARE_STATS_COUNTER(invalidate_iotlb);
462DECLARE_STATS_COUNTER(invalidate_iotlb_all);
463DECLARE_STATS_COUNTER(pri_requests);
464
Joerg Roedel7f265082008-12-12 13:50:21 +0100465static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100466static struct dentry *de_fflush;
467
468static void amd_iommu_stats_add(struct __iommu_counter *cnt)
469{
470 if (stats_dir == NULL)
471 return;
472
473 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
474 &cnt->value);
475}
476
477static void amd_iommu_stats_init(void)
478{
479 stats_dir = debugfs_create_dir("amd-iommu", NULL);
480 if (stats_dir == NULL)
481 return;
482
Joerg Roedel7f265082008-12-12 13:50:21 +0100483 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300484 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100485
486 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100487 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100488 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100489 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100490 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100491 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100492 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100493 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100494 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100495 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100496 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100497 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100498 amd_iommu_stats_add(&complete_ppr);
499 amd_iommu_stats_add(&invalidate_iotlb);
500 amd_iommu_stats_add(&invalidate_iotlb_all);
501 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100502}
503
504#endif
505
Joerg Roedel431b2a22008-07-11 17:14:22 +0200506/****************************************************************************
507 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200508 * Interrupt handling functions
509 *
510 ****************************************************************************/
511
Joerg Roedele3e59872009-09-03 14:02:10 +0200512static void dump_dte_entry(u16 devid)
513{
514 int i;
515
Joerg Roedelee6c2862011-11-09 12:06:03 +0100516 for (i = 0; i < 4; ++i)
517 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200518 amd_iommu_dev_table[devid].data[i]);
519}
520
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200521static void dump_command(unsigned long phys_addr)
522{
523 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
524 int i;
525
526 for (i = 0; i < 4; ++i)
527 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
528}
529
Joerg Roedela345b232009-09-03 15:01:43 +0200530static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200531{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200532 int type, devid, domid, flags;
533 volatile u32 *event = __evt;
534 int count = 0;
535 u64 address;
536
537retry:
538 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
539 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
540 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
541 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
542 address = (u64)(((u64)event[3]) << 32) | event[2];
543
544 if (type == 0) {
545 /* Did we hit the erratum? */
546 if (++count == LOOP_TIMEOUT) {
547 pr_err("AMD-Vi: No event written to event log\n");
548 return;
549 }
550 udelay(1);
551 goto retry;
552 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200553
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200554 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200555
556 switch (type) {
557 case EVENT_TYPE_ILL_DEV:
558 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
559 "address=0x%016llx flags=0x%04x]\n",
560 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
561 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200562 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200563 break;
564 case EVENT_TYPE_IO_FAULT:
565 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
566 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
567 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
568 domid, address, flags);
569 break;
570 case EVENT_TYPE_DEV_TAB_ERR:
571 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
572 "address=0x%016llx flags=0x%04x]\n",
573 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
574 address, flags);
575 break;
576 case EVENT_TYPE_PAGE_TAB_ERR:
577 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
578 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
579 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
580 domid, address, flags);
581 break;
582 case EVENT_TYPE_ILL_CMD:
583 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200584 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200585 break;
586 case EVENT_TYPE_CMD_HARD_ERR:
587 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
588 "flags=0x%04x]\n", address, flags);
589 break;
590 case EVENT_TYPE_IOTLB_INV_TO:
591 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
592 "address=0x%016llx]\n",
593 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
594 address);
595 break;
596 case EVENT_TYPE_INV_DEV_REQ:
597 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
598 "address=0x%016llx flags=0x%04x]\n",
599 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
600 address, flags);
601 break;
602 default:
603 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
604 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200605
606 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200607}
608
609static void iommu_poll_events(struct amd_iommu *iommu)
610{
611 u32 head, tail;
612 unsigned long flags;
613
614 spin_lock_irqsave(&iommu->lock, flags);
615
616 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
617 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
618
619 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200620 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200621 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
622 }
623
624 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
625
626 spin_unlock_irqrestore(&iommu->lock, flags);
627}
628
Joerg Roedeleee53532012-06-01 15:20:23 +0200629static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100630{
631 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100632
Joerg Roedel399be2f2011-12-01 16:53:47 +0100633 INC_STATS_COUNTER(pri_requests);
634
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100635 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
636 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
637 return;
638 }
639
640 fault.address = raw[1];
641 fault.pasid = PPR_PASID(raw[0]);
642 fault.device_id = PPR_DEVID(raw[0]);
643 fault.tag = PPR_TAG(raw[0]);
644 fault.flags = PPR_FLAGS(raw[0]);
645
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100646 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
647}
648
649static void iommu_poll_ppr_log(struct amd_iommu *iommu)
650{
651 unsigned long flags;
652 u32 head, tail;
653
654 if (iommu->ppr_log == NULL)
655 return;
656
Joerg Roedeleee53532012-06-01 15:20:23 +0200657 /* enable ppr interrupts again */
658 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
659
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100660 spin_lock_irqsave(&iommu->lock, flags);
661
662 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
663 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
664
665 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200666 volatile u64 *raw;
667 u64 entry[2];
668 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100669
Joerg Roedeleee53532012-06-01 15:20:23 +0200670 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100671
Joerg Roedeleee53532012-06-01 15:20:23 +0200672 /*
673 * Hardware bug: Interrupt may arrive before the entry is
674 * written to memory. If this happens we need to wait for the
675 * entry to arrive.
676 */
677 for (i = 0; i < LOOP_TIMEOUT; ++i) {
678 if (PPR_REQ_TYPE(raw[0]) != 0)
679 break;
680 udelay(1);
681 }
682
683 /* Avoid memcpy function-call overhead */
684 entry[0] = raw[0];
685 entry[1] = raw[1];
686
687 /*
688 * To detect the hardware bug we need to clear the entry
689 * back to zero.
690 */
691 raw[0] = raw[1] = 0UL;
692
693 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100694 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
695 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200696
697 /*
698 * Release iommu->lock because ppr-handling might need to
699 * re-aquire it
700 */
701 spin_unlock_irqrestore(&iommu->lock, flags);
702
703 /* Handle PPR entry */
704 iommu_handle_ppr_entry(iommu, entry);
705
706 spin_lock_irqsave(&iommu->lock, flags);
707
708 /* Refresh ring-buffer information */
709 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100710 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
711 }
712
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100713 spin_unlock_irqrestore(&iommu->lock, flags);
714}
715
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200716irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200717{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200718 struct amd_iommu *iommu;
719
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100720 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200721 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100722 iommu_poll_ppr_log(iommu);
723 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200724
725 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200726}
727
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200728irqreturn_t amd_iommu_int_handler(int irq, void *data)
729{
730 return IRQ_WAKE_THREAD;
731}
732
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200733/****************************************************************************
734 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200735 * IOMMU command queuing functions
736 *
737 ****************************************************************************/
738
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200739static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200740{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200741 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200742
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200743 while (*sem == 0 && i < LOOP_TIMEOUT) {
744 udelay(1);
745 i += 1;
746 }
747
748 if (i == LOOP_TIMEOUT) {
749 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
750 return -EIO;
751 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200752
753 return 0;
754}
755
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200756static void copy_cmd_to_buffer(struct amd_iommu *iommu,
757 struct iommu_cmd *cmd,
758 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200759{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200760 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200761
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200762 target = iommu->cmd_buf + tail;
763 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200764
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200765 /* Copy command to buffer */
766 memcpy(target, cmd, sizeof(*cmd));
767
768 /* Tell the IOMMU about it */
769 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
770}
771
Joerg Roedel815b33f2011-04-06 17:26:49 +0200772static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200773{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200774 WARN_ON(address & 0x7ULL);
775
Joerg Roedelded46732011-04-06 10:53:48 +0200776 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200777 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
778 cmd->data[1] = upper_32_bits(__pa(address));
779 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200780 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
781}
782
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200783static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
784{
785 memset(cmd, 0, sizeof(*cmd));
786 cmd->data[0] = devid;
787 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
788}
789
Joerg Roedel11b64022011-04-06 11:49:28 +0200790static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
791 size_t size, u16 domid, int pde)
792{
793 u64 pages;
794 int s;
795
796 pages = iommu_num_pages(address, size, PAGE_SIZE);
797 s = 0;
798
799 if (pages > 1) {
800 /*
801 * If we have to flush more than one page, flush all
802 * TLB entries for this domain
803 */
804 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
805 s = 1;
806 }
807
808 address &= PAGE_MASK;
809
810 memset(cmd, 0, sizeof(*cmd));
811 cmd->data[1] |= domid;
812 cmd->data[2] = lower_32_bits(address);
813 cmd->data[3] = upper_32_bits(address);
814 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
815 if (s) /* size bit - we flush more than one 4kb page */
816 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
817 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
818 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
819}
820
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200821static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
822 u64 address, size_t size)
823{
824 u64 pages;
825 int s;
826
827 pages = iommu_num_pages(address, size, PAGE_SIZE);
828 s = 0;
829
830 if (pages > 1) {
831 /*
832 * If we have to flush more than one page, flush all
833 * TLB entries for this domain
834 */
835 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
836 s = 1;
837 }
838
839 address &= PAGE_MASK;
840
841 memset(cmd, 0, sizeof(*cmd));
842 cmd->data[0] = devid;
843 cmd->data[0] |= (qdep & 0xff) << 24;
844 cmd->data[1] = devid;
845 cmd->data[2] = lower_32_bits(address);
846 cmd->data[3] = upper_32_bits(address);
847 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
848 if (s)
849 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
850}
851
Joerg Roedel22e266c2011-11-21 15:59:08 +0100852static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
853 u64 address, bool size)
854{
855 memset(cmd, 0, sizeof(*cmd));
856
857 address &= ~(0xfffULL);
858
859 cmd->data[0] = pasid & PASID_MASK;
860 cmd->data[1] = domid;
861 cmd->data[2] = lower_32_bits(address);
862 cmd->data[3] = upper_32_bits(address);
863 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
864 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
865 if (size)
866 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
867 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
868}
869
870static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
871 int qdep, u64 address, bool size)
872{
873 memset(cmd, 0, sizeof(*cmd));
874
875 address &= ~(0xfffULL);
876
877 cmd->data[0] = devid;
878 cmd->data[0] |= (pasid & 0xff) << 16;
879 cmd->data[0] |= (qdep & 0xff) << 24;
880 cmd->data[1] = devid;
881 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
882 cmd->data[2] = lower_32_bits(address);
883 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
884 cmd->data[3] = upper_32_bits(address);
885 if (size)
886 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
887 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
888}
889
Joerg Roedelc99afa22011-11-21 18:19:25 +0100890static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
891 int status, int tag, bool gn)
892{
893 memset(cmd, 0, sizeof(*cmd));
894
895 cmd->data[0] = devid;
896 if (gn) {
897 cmd->data[1] = pasid & PASID_MASK;
898 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
899 }
900 cmd->data[3] = tag & 0x1ff;
901 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
902
903 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
904}
905
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200906static void build_inv_all(struct iommu_cmd *cmd)
907{
908 memset(cmd, 0, sizeof(*cmd));
909 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200910}
911
Joerg Roedel7ef27982012-06-21 16:46:04 +0200912static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
913{
914 memset(cmd, 0, sizeof(*cmd));
915 cmd->data[0] = devid;
916 CMD_SET_TYPE(cmd, CMD_INV_IRT);
917}
918
Joerg Roedel431b2a22008-07-11 17:14:22 +0200919/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200920 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200921 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200922 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200923static int iommu_queue_command_sync(struct amd_iommu *iommu,
924 struct iommu_cmd *cmd,
925 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200926{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200927 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200928 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200929
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200930 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100931
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200932again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200933 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200934
935 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
936 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
937 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
938 left = (head - next_tail) % iommu->cmd_buf_size;
939
940 if (left <= 2) {
941 struct iommu_cmd sync_cmd;
942 volatile u64 sem = 0;
943 int ret;
944
945 build_completion_wait(&sync_cmd, (u64)&sem);
946 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
947
948 spin_unlock_irqrestore(&iommu->lock, flags);
949
950 if ((ret = wait_on_sem(&sem)) != 0)
951 return ret;
952
953 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200954 }
955
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200956 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200957
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200958 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200959 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200960
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200961 spin_unlock_irqrestore(&iommu->lock, flags);
962
Joerg Roedel815b33f2011-04-06 17:26:49 +0200963 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100964}
965
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200966static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
967{
968 return iommu_queue_command_sync(iommu, cmd, true);
969}
970
Joerg Roedel8d201962008-12-02 20:34:41 +0100971/*
972 * This function queues a completion wait command into the command
973 * buffer of an IOMMU
974 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100975static int iommu_completion_wait(struct amd_iommu *iommu)
976{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200977 struct iommu_cmd cmd;
978 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200979 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100980
981 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200982 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100983
Joerg Roedel815b33f2011-04-06 17:26:49 +0200984 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100985
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200986 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100987 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200988 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100989
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200990 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200991}
992
Joerg Roedeld8c13082011-04-06 18:51:26 +0200993static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200994{
995 struct iommu_cmd cmd;
996
Joerg Roedeld8c13082011-04-06 18:51:26 +0200997 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200998
Joerg Roedeld8c13082011-04-06 18:51:26 +0200999 return iommu_queue_command(iommu, &cmd);
1000}
1001
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001002static void iommu_flush_dte_all(struct amd_iommu *iommu)
1003{
1004 u32 devid;
1005
1006 for (devid = 0; devid <= 0xffff; ++devid)
1007 iommu_flush_dte(iommu, devid);
1008
1009 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001010}
1011
1012/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001013 * This function uses heavy locking and may disable irqs for some time. But
1014 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001015 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001016static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001017{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001018 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001019
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001020 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1021 struct iommu_cmd cmd;
1022 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1023 dom_id, 1);
1024 iommu_queue_command(iommu, &cmd);
1025 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001026
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001027 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001028}
1029
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001030static void iommu_flush_all(struct amd_iommu *iommu)
1031{
1032 struct iommu_cmd cmd;
1033
1034 build_inv_all(&cmd);
1035
1036 iommu_queue_command(iommu, &cmd);
1037 iommu_completion_wait(iommu);
1038}
1039
Joerg Roedel7ef27982012-06-21 16:46:04 +02001040static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1041{
1042 struct iommu_cmd cmd;
1043
1044 build_inv_irt(&cmd, devid);
1045
1046 iommu_queue_command(iommu, &cmd);
1047}
1048
1049static void iommu_flush_irt_all(struct amd_iommu *iommu)
1050{
1051 u32 devid;
1052
1053 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1054 iommu_flush_irt(iommu, devid);
1055
1056 iommu_completion_wait(iommu);
1057}
1058
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001059void iommu_flush_all_caches(struct amd_iommu *iommu)
1060{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001061 if (iommu_feature(iommu, FEATURE_IA)) {
1062 iommu_flush_all(iommu);
1063 } else {
1064 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001065 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001066 iommu_flush_tlb_all(iommu);
1067 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001068}
1069
Joerg Roedel431b2a22008-07-11 17:14:22 +02001070/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001071 * Command send function for flushing on-device TLB
1072 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001073static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1074 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001075{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001076 struct amd_iommu *iommu;
1077 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001078 int qdep;
1079
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001080 qdep = dev_data->ats.qdep;
1081 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001082
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001083 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001084
1085 return iommu_queue_command(iommu, &cmd);
1086}
1087
1088/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001089 * Command send function for invalidating a device table entry
1090 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001091static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001092{
1093 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001094 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001095
Joerg Roedel6c542042011-06-09 17:07:31 +02001096 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001097
Joerg Roedelf62dda62011-06-09 12:55:35 +02001098 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001099 if (ret)
1100 return ret;
1101
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001102 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001103 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001104
1105 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001106}
1107
Joerg Roedel431b2a22008-07-11 17:14:22 +02001108/*
1109 * TLB invalidation function which is called from the mapping functions.
1110 * It invalidates a single PTE if the range to flush is within a single
1111 * page. Otherwise it flushes the whole TLB of the IOMMU.
1112 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001113static void __domain_flush_pages(struct protection_domain *domain,
1114 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001115{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001116 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001117 struct iommu_cmd cmd;
1118 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001119
Joerg Roedel11b64022011-04-06 11:49:28 +02001120 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001121
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001122 for (i = 0; i < amd_iommus_present; ++i) {
1123 if (!domain->dev_iommu[i])
1124 continue;
1125
1126 /*
1127 * Devices of this domain are behind this IOMMU
1128 * We need a TLB flush
1129 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001130 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001131 }
1132
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001133 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001134
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001135 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001136 continue;
1137
Joerg Roedel6c542042011-06-09 17:07:31 +02001138 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001139 }
1140
Joerg Roedel11b64022011-04-06 11:49:28 +02001141 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001142}
1143
Joerg Roedel17b124b2011-04-06 18:01:35 +02001144static void domain_flush_pages(struct protection_domain *domain,
1145 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001146{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001147 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001148}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001149
Joerg Roedel1c655772008-09-04 18:40:05 +02001150/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001151static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001152{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001153 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001154}
1155
Chris Wright42a49f92009-06-15 15:42:00 +02001156/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001157static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001158{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001159 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1160}
1161
1162static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001163{
1164 int i;
1165
1166 for (i = 0; i < amd_iommus_present; ++i) {
1167 if (!domain->dev_iommu[i])
1168 continue;
1169
1170 /*
1171 * Devices of this domain are behind this IOMMU
1172 * We need to wait for completion of all commands.
1173 */
1174 iommu_completion_wait(amd_iommus[i]);
1175 }
1176}
1177
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001178
Joerg Roedel43f49602008-12-02 21:01:12 +01001179/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001180 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001181 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001182static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001183{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001184 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001185
1186 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001187 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001188}
1189
Joerg Roedel431b2a22008-07-11 17:14:22 +02001190/****************************************************************************
1191 *
1192 * The functions below are used the create the page table mappings for
1193 * unity mapped regions.
1194 *
1195 ****************************************************************************/
1196
1197/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001198 * This function is used to add another level to an IO page table. Adding
1199 * another level increases the size of the address space by 9 bits to a size up
1200 * to 64 bits.
1201 */
1202static bool increase_address_space(struct protection_domain *domain,
1203 gfp_t gfp)
1204{
1205 u64 *pte;
1206
1207 if (domain->mode == PAGE_MODE_6_LEVEL)
1208 /* address space already 64 bit large */
1209 return false;
1210
1211 pte = (void *)get_zeroed_page(gfp);
1212 if (!pte)
1213 return false;
1214
1215 *pte = PM_LEVEL_PDE(domain->mode,
1216 virt_to_phys(domain->pt_root));
1217 domain->pt_root = pte;
1218 domain->mode += 1;
1219 domain->updated = true;
1220
1221 return true;
1222}
1223
1224static u64 *alloc_pte(struct protection_domain *domain,
1225 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001226 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001227 u64 **pte_page,
1228 gfp_t gfp)
1229{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001230 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001231 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001232
1233 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001234
1235 while (address > PM_LEVEL_SIZE(domain->mode))
1236 increase_address_space(domain, gfp);
1237
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001238 level = domain->mode - 1;
1239 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1240 address = PAGE_SIZE_ALIGN(address, page_size);
1241 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001242
1243 while (level > end_lvl) {
1244 if (!IOMMU_PTE_PRESENT(*pte)) {
1245 page = (u64 *)get_zeroed_page(gfp);
1246 if (!page)
1247 return NULL;
1248 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1249 }
1250
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001251 /* No level skipping support yet */
1252 if (PM_PTE_LEVEL(*pte) != level)
1253 return NULL;
1254
Joerg Roedel308973d2009-11-24 17:43:32 +01001255 level -= 1;
1256
1257 pte = IOMMU_PTE_PAGE(*pte);
1258
1259 if (pte_page && level == end_lvl)
1260 *pte_page = pte;
1261
1262 pte = &pte[PM_LEVEL_INDEX(level, address)];
1263 }
1264
1265 return pte;
1266}
1267
1268/*
1269 * This function checks if there is a PTE for a given dma address. If
1270 * there is one, it returns the pointer to it.
1271 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001272static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001273{
1274 int level;
1275 u64 *pte;
1276
Joerg Roedel24cd7722010-01-19 17:27:39 +01001277 if (address > PM_LEVEL_SIZE(domain->mode))
1278 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001279
Joerg Roedel24cd7722010-01-19 17:27:39 +01001280 level = domain->mode - 1;
1281 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1282
1283 while (level > 0) {
1284
1285 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001286 if (!IOMMU_PTE_PRESENT(*pte))
1287 return NULL;
1288
Joerg Roedel24cd7722010-01-19 17:27:39 +01001289 /* Large PTE */
1290 if (PM_PTE_LEVEL(*pte) == 0x07) {
1291 unsigned long pte_mask, __pte;
1292
1293 /*
1294 * If we have a series of large PTEs, make
1295 * sure to return a pointer to the first one.
1296 */
1297 pte_mask = PTE_PAGE_SIZE(*pte);
1298 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1299 __pte = ((unsigned long)pte) & pte_mask;
1300
1301 return (u64 *)__pte;
1302 }
1303
1304 /* No level skipping support yet */
1305 if (PM_PTE_LEVEL(*pte) != level)
1306 return NULL;
1307
Joerg Roedel308973d2009-11-24 17:43:32 +01001308 level -= 1;
1309
Joerg Roedel24cd7722010-01-19 17:27:39 +01001310 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001311 pte = IOMMU_PTE_PAGE(*pte);
1312 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001313 }
1314
1315 return pte;
1316}
1317
1318/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001319 * Generic mapping functions. It maps a physical address into a DMA
1320 * address space. It allocates the page table pages if necessary.
1321 * In the future it can be extended to a generic mapping function
1322 * supporting all features of AMD IOMMU page tables like level skipping
1323 * and full 64 bit address spaces.
1324 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001325static int iommu_map_page(struct protection_domain *dom,
1326 unsigned long bus_addr,
1327 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001328 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001329 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001330{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001331 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001332 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001333
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001334 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001335 return -EINVAL;
1336
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001337 bus_addr = PAGE_ALIGN(bus_addr);
1338 phys_addr = PAGE_ALIGN(phys_addr);
1339 count = PAGE_SIZE_PTE_COUNT(page_size);
1340 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001341
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001342 for (i = 0; i < count; ++i)
1343 if (IOMMU_PTE_PRESENT(pte[i]))
1344 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001345
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001346 if (page_size > PAGE_SIZE) {
1347 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1348 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1349 } else
1350 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1351
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001352 if (prot & IOMMU_PROT_IR)
1353 __pte |= IOMMU_PTE_IR;
1354 if (prot & IOMMU_PROT_IW)
1355 __pte |= IOMMU_PTE_IW;
1356
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001357 for (i = 0; i < count; ++i)
1358 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001359
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001360 update_domain(dom);
1361
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001362 return 0;
1363}
1364
Joerg Roedel24cd7722010-01-19 17:27:39 +01001365static unsigned long iommu_unmap_page(struct protection_domain *dom,
1366 unsigned long bus_addr,
1367 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001368{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001369 unsigned long long unmap_size, unmapped;
1370 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001371
Joerg Roedel24cd7722010-01-19 17:27:39 +01001372 BUG_ON(!is_power_of_2(page_size));
1373
1374 unmapped = 0;
1375
1376 while (unmapped < page_size) {
1377
1378 pte = fetch_pte(dom, bus_addr);
1379
1380 if (!pte) {
1381 /*
1382 * No PTE for this address
1383 * move forward in 4kb steps
1384 */
1385 unmap_size = PAGE_SIZE;
1386 } else if (PM_PTE_LEVEL(*pte) == 0) {
1387 /* 4kb PTE found for this address */
1388 unmap_size = PAGE_SIZE;
1389 *pte = 0ULL;
1390 } else {
1391 int count, i;
1392
1393 /* Large PTE found which maps this address */
1394 unmap_size = PTE_PAGE_SIZE(*pte);
1395 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1396 for (i = 0; i < count; i++)
1397 pte[i] = 0ULL;
1398 }
1399
1400 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1401 unmapped += unmap_size;
1402 }
1403
1404 BUG_ON(!is_power_of_2(unmapped));
1405
1406 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001407}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001408
Joerg Roedel431b2a22008-07-11 17:14:22 +02001409/*
1410 * This function checks if a specific unity mapping entry is needed for
1411 * this specific IOMMU.
1412 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001413static int iommu_for_unity_map(struct amd_iommu *iommu,
1414 struct unity_map_entry *entry)
1415{
1416 u16 bdf, i;
1417
1418 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1419 bdf = amd_iommu_alias_table[i];
1420 if (amd_iommu_rlookup_table[bdf] == iommu)
1421 return 1;
1422 }
1423
1424 return 0;
1425}
1426
Joerg Roedel431b2a22008-07-11 17:14:22 +02001427/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001428 * This function actually applies the mapping to the page table of the
1429 * dma_ops domain.
1430 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001431static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1432 struct unity_map_entry *e)
1433{
1434 u64 addr;
1435 int ret;
1436
1437 for (addr = e->address_start; addr < e->address_end;
1438 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001439 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001440 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001441 if (ret)
1442 return ret;
1443 /*
1444 * if unity mapping is in aperture range mark the page
1445 * as allocated in the aperture
1446 */
1447 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001448 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001449 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001450 }
1451
1452 return 0;
1453}
1454
Joerg Roedel431b2a22008-07-11 17:14:22 +02001455/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001456 * Init the unity mappings for a specific IOMMU in the system
1457 *
1458 * Basically iterates over all unity mapping entries and applies them to
1459 * the default domain DMA of that IOMMU if necessary.
1460 */
1461static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1462{
1463 struct unity_map_entry *entry;
1464 int ret;
1465
1466 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1467 if (!iommu_for_unity_map(iommu, entry))
1468 continue;
1469 ret = dma_ops_unity_map(iommu->default_dom, entry);
1470 if (ret)
1471 return ret;
1472 }
1473
1474 return 0;
1475}
1476
1477/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001478 * Inits the unity mappings required for a specific device
1479 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001480static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1481 u16 devid)
1482{
1483 struct unity_map_entry *e;
1484 int ret;
1485
1486 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1487 if (!(devid >= e->devid_start && devid <= e->devid_end))
1488 continue;
1489 ret = dma_ops_unity_map(dma_dom, e);
1490 if (ret)
1491 return ret;
1492 }
1493
1494 return 0;
1495}
1496
Joerg Roedel431b2a22008-07-11 17:14:22 +02001497/****************************************************************************
1498 *
1499 * The next functions belong to the address allocator for the dma_ops
1500 * interface functions. They work like the allocators in the other IOMMU
1501 * drivers. Its basically a bitmap which marks the allocated pages in
1502 * the aperture. Maybe it could be enhanced in the future to a more
1503 * efficient allocator.
1504 *
1505 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001506
Joerg Roedel431b2a22008-07-11 17:14:22 +02001507/*
Joerg Roedel384de722009-05-15 12:30:05 +02001508 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001509 *
1510 * called with domain->lock held
1511 */
Joerg Roedel384de722009-05-15 12:30:05 +02001512
Joerg Roedel9cabe892009-05-18 16:38:55 +02001513/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001514 * Used to reserve address ranges in the aperture (e.g. for exclusion
1515 * ranges.
1516 */
1517static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1518 unsigned long start_page,
1519 unsigned int pages)
1520{
1521 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1522
1523 if (start_page + pages > last_page)
1524 pages = last_page - start_page;
1525
1526 for (i = start_page; i < start_page + pages; ++i) {
1527 int index = i / APERTURE_RANGE_PAGES;
1528 int page = i % APERTURE_RANGE_PAGES;
1529 __set_bit(page, dom->aperture[index]->bitmap);
1530 }
1531}
1532
1533/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001534 * This function is used to add a new aperture range to an existing
1535 * aperture in case of dma_ops domain allocation or address allocation
1536 * failure.
1537 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001538static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001539 bool populate, gfp_t gfp)
1540{
1541 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001542 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001543 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001544
Joerg Roedelf5e97052009-05-22 12:31:53 +02001545#ifdef CONFIG_IOMMU_STRESS
1546 populate = false;
1547#endif
1548
Joerg Roedel9cabe892009-05-18 16:38:55 +02001549 if (index >= APERTURE_MAX_RANGES)
1550 return -ENOMEM;
1551
1552 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1553 if (!dma_dom->aperture[index])
1554 return -ENOMEM;
1555
1556 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1557 if (!dma_dom->aperture[index]->bitmap)
1558 goto out_free;
1559
1560 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1561
1562 if (populate) {
1563 unsigned long address = dma_dom->aperture_size;
1564 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1565 u64 *pte, *pte_page;
1566
1567 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001568 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001569 &pte_page, gfp);
1570 if (!pte)
1571 goto out_free;
1572
1573 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1574
1575 address += APERTURE_RANGE_SIZE / 64;
1576 }
1577 }
1578
Joerg Roedel17f5b562011-07-06 17:14:44 +02001579 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001580 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1581
Joerg Roedel17f5b562011-07-06 17:14:44 +02001582 /* Reserve address range used for MSI messages */
1583 if (old_size < MSI_ADDR_BASE_LO &&
1584 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1585 unsigned long spage;
1586 int pages;
1587
1588 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1589 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1590
1591 dma_ops_reserve_addresses(dma_dom, spage, pages);
1592 }
1593
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001594 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001595 for_each_iommu(iommu) {
1596 if (iommu->exclusion_start &&
1597 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1598 && iommu->exclusion_start < dma_dom->aperture_size) {
1599 unsigned long startpage;
1600 int pages = iommu_num_pages(iommu->exclusion_start,
1601 iommu->exclusion_length,
1602 PAGE_SIZE);
1603 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1604 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1605 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001606 }
1607
1608 /*
1609 * Check for areas already mapped as present in the new aperture
1610 * range and mark those pages as reserved in the allocator. Such
1611 * mappings may already exist as a result of requested unity
1612 * mappings for devices.
1613 */
1614 for (i = dma_dom->aperture[index]->offset;
1615 i < dma_dom->aperture_size;
1616 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001617 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001618 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1619 continue;
1620
Joerg Roedelfcd08612011-10-11 17:41:32 +02001621 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001622 }
1623
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001624 update_domain(&dma_dom->domain);
1625
Joerg Roedel9cabe892009-05-18 16:38:55 +02001626 return 0;
1627
1628out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001629 update_domain(&dma_dom->domain);
1630
Joerg Roedel9cabe892009-05-18 16:38:55 +02001631 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1632
1633 kfree(dma_dom->aperture[index]);
1634 dma_dom->aperture[index] = NULL;
1635
1636 return -ENOMEM;
1637}
1638
Joerg Roedel384de722009-05-15 12:30:05 +02001639static unsigned long dma_ops_area_alloc(struct device *dev,
1640 struct dma_ops_domain *dom,
1641 unsigned int pages,
1642 unsigned long align_mask,
1643 u64 dma_mask,
1644 unsigned long start)
1645{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001646 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001647 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1648 int i = start >> APERTURE_RANGE_SHIFT;
1649 unsigned long boundary_size;
1650 unsigned long address = -1;
1651 unsigned long limit;
1652
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001653 next_bit >>= PAGE_SHIFT;
1654
Joerg Roedel384de722009-05-15 12:30:05 +02001655 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1656 PAGE_SIZE) >> PAGE_SHIFT;
1657
1658 for (;i < max_index; ++i) {
1659 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1660
1661 if (dom->aperture[i]->offset >= dma_mask)
1662 break;
1663
1664 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1665 dma_mask >> PAGE_SHIFT);
1666
1667 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1668 limit, next_bit, pages, 0,
1669 boundary_size, align_mask);
1670 if (address != -1) {
1671 address = dom->aperture[i]->offset +
1672 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001673 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001674 break;
1675 }
1676
1677 next_bit = 0;
1678 }
1679
1680 return address;
1681}
1682
Joerg Roedeld3086442008-06-26 21:27:57 +02001683static unsigned long dma_ops_alloc_addresses(struct device *dev,
1684 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001685 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001686 unsigned long align_mask,
1687 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001688{
Joerg Roedeld3086442008-06-26 21:27:57 +02001689 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001690
Joerg Roedelfe16f082009-05-22 12:27:53 +02001691#ifdef CONFIG_IOMMU_STRESS
1692 dom->next_address = 0;
1693 dom->need_flush = true;
1694#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001695
Joerg Roedel384de722009-05-15 12:30:05 +02001696 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001697 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001698
Joerg Roedel1c655772008-09-04 18:40:05 +02001699 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001700 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001701 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1702 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001703 dom->need_flush = true;
1704 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001705
Joerg Roedel384de722009-05-15 12:30:05 +02001706 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001707 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001708
1709 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1710
1711 return address;
1712}
1713
Joerg Roedel431b2a22008-07-11 17:14:22 +02001714/*
1715 * The address free function.
1716 *
1717 * called with domain->lock held
1718 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001719static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1720 unsigned long address,
1721 unsigned int pages)
1722{
Joerg Roedel384de722009-05-15 12:30:05 +02001723 unsigned i = address >> APERTURE_RANGE_SHIFT;
1724 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001725
Joerg Roedel384de722009-05-15 12:30:05 +02001726 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1727
Joerg Roedel47bccd62009-05-22 12:40:54 +02001728#ifdef CONFIG_IOMMU_STRESS
1729 if (i < 4)
1730 return;
1731#endif
1732
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001733 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001734 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001735
1736 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001737
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001738 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001739
Joerg Roedeld3086442008-06-26 21:27:57 +02001740}
1741
Joerg Roedel431b2a22008-07-11 17:14:22 +02001742/****************************************************************************
1743 *
1744 * The next functions belong to the domain allocation. A domain is
1745 * allocated for every IOMMU as the default domain. If device isolation
1746 * is enabled, every device get its own domain. The most important thing
1747 * about domains is the page table mapping the DMA address space they
1748 * contain.
1749 *
1750 ****************************************************************************/
1751
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001752/*
1753 * This function adds a protection domain to the global protection domain list
1754 */
1755static void add_domain_to_list(struct protection_domain *domain)
1756{
1757 unsigned long flags;
1758
1759 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1760 list_add(&domain->list, &amd_iommu_pd_list);
1761 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1762}
1763
1764/*
1765 * This function removes a protection domain to the global
1766 * protection domain list
1767 */
1768static void del_domain_from_list(struct protection_domain *domain)
1769{
1770 unsigned long flags;
1771
1772 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1773 list_del(&domain->list);
1774 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1775}
1776
Joerg Roedelec487d12008-06-26 21:27:58 +02001777static u16 domain_id_alloc(void)
1778{
1779 unsigned long flags;
1780 int id;
1781
1782 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1783 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1784 BUG_ON(id == 0);
1785 if (id > 0 && id < MAX_DOMAIN_ID)
1786 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1787 else
1788 id = 0;
1789 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1790
1791 return id;
1792}
1793
Joerg Roedela2acfb72008-12-02 18:28:53 +01001794static void domain_id_free(int id)
1795{
1796 unsigned long flags;
1797
1798 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1799 if (id > 0 && id < MAX_DOMAIN_ID)
1800 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1801 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1802}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001803
Joerg Roedel86db2e52008-12-02 18:20:21 +01001804static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001805{
1806 int i, j;
1807 u64 *p1, *p2, *p3;
1808
Joerg Roedel86db2e52008-12-02 18:20:21 +01001809 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001810
1811 if (!p1)
1812 return;
1813
1814 for (i = 0; i < 512; ++i) {
1815 if (!IOMMU_PTE_PRESENT(p1[i]))
1816 continue;
1817
1818 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001819 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001820 if (!IOMMU_PTE_PRESENT(p2[j]))
1821 continue;
1822 p3 = IOMMU_PTE_PAGE(p2[j]);
1823 free_page((unsigned long)p3);
1824 }
1825
1826 free_page((unsigned long)p2);
1827 }
1828
1829 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001830
1831 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001832}
1833
Joerg Roedelb16137b2011-11-21 16:50:23 +01001834static void free_gcr3_tbl_level1(u64 *tbl)
1835{
1836 u64 *ptr;
1837 int i;
1838
1839 for (i = 0; i < 512; ++i) {
1840 if (!(tbl[i] & GCR3_VALID))
1841 continue;
1842
1843 ptr = __va(tbl[i] & PAGE_MASK);
1844
1845 free_page((unsigned long)ptr);
1846 }
1847}
1848
1849static void free_gcr3_tbl_level2(u64 *tbl)
1850{
1851 u64 *ptr;
1852 int i;
1853
1854 for (i = 0; i < 512; ++i) {
1855 if (!(tbl[i] & GCR3_VALID))
1856 continue;
1857
1858 ptr = __va(tbl[i] & PAGE_MASK);
1859
1860 free_gcr3_tbl_level1(ptr);
1861 }
1862}
1863
Joerg Roedel52815b72011-11-17 17:24:28 +01001864static void free_gcr3_table(struct protection_domain *domain)
1865{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001866 if (domain->glx == 2)
1867 free_gcr3_tbl_level2(domain->gcr3_tbl);
1868 else if (domain->glx == 1)
1869 free_gcr3_tbl_level1(domain->gcr3_tbl);
1870 else if (domain->glx != 0)
1871 BUG();
1872
Joerg Roedel52815b72011-11-17 17:24:28 +01001873 free_page((unsigned long)domain->gcr3_tbl);
1874}
1875
Joerg Roedel431b2a22008-07-11 17:14:22 +02001876/*
1877 * Free a domain, only used if something went wrong in the
1878 * allocation path and we need to free an already allocated page table
1879 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001880static void dma_ops_domain_free(struct dma_ops_domain *dom)
1881{
Joerg Roedel384de722009-05-15 12:30:05 +02001882 int i;
1883
Joerg Roedelec487d12008-06-26 21:27:58 +02001884 if (!dom)
1885 return;
1886
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001887 del_domain_from_list(&dom->domain);
1888
Joerg Roedel86db2e52008-12-02 18:20:21 +01001889 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001890
Joerg Roedel384de722009-05-15 12:30:05 +02001891 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1892 if (!dom->aperture[i])
1893 continue;
1894 free_page((unsigned long)dom->aperture[i]->bitmap);
1895 kfree(dom->aperture[i]);
1896 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001897
1898 kfree(dom);
1899}
1900
Joerg Roedel431b2a22008-07-11 17:14:22 +02001901/*
1902 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001903 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001904 * structures required for the dma_ops interface
1905 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001906static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001907{
1908 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001909
1910 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1911 if (!dma_dom)
1912 return NULL;
1913
1914 spin_lock_init(&dma_dom->domain.lock);
1915
1916 dma_dom->domain.id = domain_id_alloc();
1917 if (dma_dom->domain.id == 0)
1918 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001919 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001920 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001921 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001922 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001923 dma_dom->domain.priv = dma_dom;
1924 if (!dma_dom->domain.pt_root)
1925 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001926
Joerg Roedel1c655772008-09-04 18:40:05 +02001927 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001928 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001929
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001930 add_domain_to_list(&dma_dom->domain);
1931
Joerg Roedel576175c2009-11-23 19:08:46 +01001932 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001933 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001934
Joerg Roedel431b2a22008-07-11 17:14:22 +02001935 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001936 * mark the first page as allocated so we never return 0 as
1937 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001938 */
Joerg Roedel384de722009-05-15 12:30:05 +02001939 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001940 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001941
Joerg Roedelec487d12008-06-26 21:27:58 +02001942
1943 return dma_dom;
1944
1945free_dma_dom:
1946 dma_ops_domain_free(dma_dom);
1947
1948 return NULL;
1949}
1950
Joerg Roedel431b2a22008-07-11 17:14:22 +02001951/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001952 * little helper function to check whether a given protection domain is a
1953 * dma_ops domain
1954 */
1955static bool dma_ops_domain(struct protection_domain *domain)
1956{
1957 return domain->flags & PD_DMA_OPS_MASK;
1958}
1959
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001960static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001961{
Joerg Roedel132bd682011-11-17 14:18:46 +01001962 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001963 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001964
Joerg Roedel132bd682011-11-17 14:18:46 +01001965 if (domain->mode != PAGE_MODE_NONE)
1966 pte_root = virt_to_phys(domain->pt_root);
1967
Joerg Roedel38ddf412008-09-11 10:38:32 +02001968 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1969 << DEV_ENTRY_MODE_SHIFT;
1970 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001971
Joerg Roedelee6c2862011-11-09 12:06:03 +01001972 flags = amd_iommu_dev_table[devid].data[1];
1973
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001974 if (ats)
1975 flags |= DTE_FLAG_IOTLB;
1976
Joerg Roedel52815b72011-11-17 17:24:28 +01001977 if (domain->flags & PD_IOMMUV2_MASK) {
1978 u64 gcr3 = __pa(domain->gcr3_tbl);
1979 u64 glx = domain->glx;
1980 u64 tmp;
1981
1982 pte_root |= DTE_FLAG_GV;
1983 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1984
1985 /* First mask out possible old values for GCR3 table */
1986 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1987 flags &= ~tmp;
1988
1989 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1990 flags &= ~tmp;
1991
1992 /* Encode GCR3 table into DTE */
1993 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1994 pte_root |= tmp;
1995
1996 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1997 flags |= tmp;
1998
1999 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2000 flags |= tmp;
2001 }
2002
Joerg Roedelee6c2862011-11-09 12:06:03 +01002003 flags &= ~(0xffffUL);
2004 flags |= domain->id;
2005
2006 amd_iommu_dev_table[devid].data[1] = flags;
2007 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002008}
2009
Joerg Roedel15898bb2009-11-24 15:39:42 +01002010static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002011{
Joerg Roedel355bf552008-12-08 12:02:41 +01002012 /* remove entry from the device table seen by the hardware */
2013 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2014 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002015
Joerg Roedelc5cca142009-10-09 18:31:20 +02002016 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002017}
2018
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002019static void do_attach(struct iommu_dev_data *dev_data,
2020 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002021{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002022 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002023 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002024
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002025 iommu = amd_iommu_rlookup_table[dev_data->devid];
2026 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002027
2028 /* Update data structures */
2029 dev_data->domain = domain;
2030 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002031 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002032
2033 /* Do reference counting */
2034 domain->dev_iommu[iommu->index] += 1;
2035 domain->dev_cnt += 1;
2036
2037 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002038 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002039}
2040
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002041static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002042{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002043 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002044
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002045 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002046
Joerg Roedelc4596112009-11-20 14:57:32 +01002047 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002048 dev_data->domain->dev_iommu[iommu->index] -= 1;
2049 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002050
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002051 /* Update data structures */
2052 dev_data->domain = NULL;
2053 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002054 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002055
2056 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002057 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002058}
2059
2060/*
2061 * If a device is not yet associated with a domain, this function does
2062 * assigns it visible for the hardware
2063 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002064static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002065 struct protection_domain *domain)
2066{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002067 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002068
Joerg Roedel15898bb2009-11-24 15:39:42 +01002069 /* lock domain */
2070 spin_lock(&domain->lock);
2071
Joerg Roedel71f77582011-06-09 19:03:15 +02002072 if (dev_data->alias_data != NULL) {
2073 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002074
Joerg Roedel2b02b092011-06-09 17:48:39 +02002075 /* Some sanity checks */
2076 ret = -EBUSY;
2077 if (alias_data->domain != NULL &&
2078 alias_data->domain != domain)
2079 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002080
Joerg Roedel2b02b092011-06-09 17:48:39 +02002081 if (dev_data->domain != NULL &&
2082 dev_data->domain != domain)
2083 goto out_unlock;
2084
2085 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002086 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002087 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002088
2089 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002090 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002091
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002092 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002093 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002094
Joerg Roedel24100052009-11-25 15:59:57 +01002095 atomic_inc(&dev_data->bind);
2096
Julia Lawall84fe6c12010-05-27 12:31:51 +02002097 ret = 0;
2098
2099out_unlock:
2100
Joerg Roedel355bf552008-12-08 12:02:41 +01002101 /* ready */
2102 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002103
Julia Lawall84fe6c12010-05-27 12:31:51 +02002104 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002105}
2106
Joerg Roedel52815b72011-11-17 17:24:28 +01002107
2108static void pdev_iommuv2_disable(struct pci_dev *pdev)
2109{
2110 pci_disable_ats(pdev);
2111 pci_disable_pri(pdev);
2112 pci_disable_pasid(pdev);
2113}
2114
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002115/* FIXME: Change generic reset-function to do the same */
2116static int pri_reset_while_enabled(struct pci_dev *pdev)
2117{
2118 u16 control;
2119 int pos;
2120
Joerg Roedel46277b72011-12-07 14:34:02 +01002121 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002122 if (!pos)
2123 return -EINVAL;
2124
Joerg Roedel46277b72011-12-07 14:34:02 +01002125 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2126 control |= PCI_PRI_CTRL_RESET;
2127 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002128
2129 return 0;
2130}
2131
Joerg Roedel52815b72011-11-17 17:24:28 +01002132static int pdev_iommuv2_enable(struct pci_dev *pdev)
2133{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002134 bool reset_enable;
2135 int reqs, ret;
2136
2137 /* FIXME: Hardcode number of outstanding requests for now */
2138 reqs = 32;
2139 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2140 reqs = 1;
2141 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002142
2143 /* Only allow access to user-accessible pages */
2144 ret = pci_enable_pasid(pdev, 0);
2145 if (ret)
2146 goto out_err;
2147
2148 /* First reset the PRI state of the device */
2149 ret = pci_reset_pri(pdev);
2150 if (ret)
2151 goto out_err;
2152
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002153 /* Enable PRI */
2154 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002155 if (ret)
2156 goto out_err;
2157
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002158 if (reset_enable) {
2159 ret = pri_reset_while_enabled(pdev);
2160 if (ret)
2161 goto out_err;
2162 }
2163
Joerg Roedel52815b72011-11-17 17:24:28 +01002164 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2165 if (ret)
2166 goto out_err;
2167
2168 return 0;
2169
2170out_err:
2171 pci_disable_pri(pdev);
2172 pci_disable_pasid(pdev);
2173
2174 return ret;
2175}
2176
Joerg Roedelc99afa22011-11-21 18:19:25 +01002177/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002178#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002179
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002180static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002181{
Joerg Roedela3b93122012-04-12 12:49:26 +02002182 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002183 int pos;
2184
Joerg Roedel46277b72011-12-07 14:34:02 +01002185 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002186 if (!pos)
2187 return false;
2188
Joerg Roedela3b93122012-04-12 12:49:26 +02002189 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002190
Joerg Roedela3b93122012-04-12 12:49:26 +02002191 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002192}
2193
Joerg Roedel15898bb2009-11-24 15:39:42 +01002194/*
2195 * If a device is not yet associated with a domain, this function does
2196 * assigns it visible for the hardware
2197 */
2198static int attach_device(struct device *dev,
2199 struct protection_domain *domain)
2200{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002201 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002202 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002203 unsigned long flags;
2204 int ret;
2205
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002206 dev_data = get_dev_data(dev);
2207
Joerg Roedel52815b72011-11-17 17:24:28 +01002208 if (domain->flags & PD_IOMMUV2_MASK) {
2209 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2210 return -EINVAL;
2211
2212 if (pdev_iommuv2_enable(pdev) != 0)
2213 return -EINVAL;
2214
2215 dev_data->ats.enabled = true;
2216 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002217 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002218 } else if (amd_iommu_iotlb_sup &&
2219 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002220 dev_data->ats.enabled = true;
2221 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2222 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002223
Joerg Roedel15898bb2009-11-24 15:39:42 +01002224 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002225 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002226 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2227
2228 /*
2229 * We might boot into a crash-kernel here. The crashed kernel
2230 * left the caches in the IOMMU dirty. So we have to flush
2231 * here to evict all dirty stuff.
2232 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002233 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002234
2235 return ret;
2236}
2237
2238/*
2239 * Removes a device from a protection domain (unlocked)
2240 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002241static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002242{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002243 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002244 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002245
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002246 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002247
Joerg Roedel2ca76272010-01-22 16:45:31 +01002248 domain = dev_data->domain;
2249
2250 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002251
Joerg Roedel71f77582011-06-09 19:03:15 +02002252 if (dev_data->alias_data != NULL) {
2253 struct iommu_dev_data *alias_data = dev_data->alias_data;
2254
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002255 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002256 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002257 }
2258
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002259 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002260 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002261
Joerg Roedel2ca76272010-01-22 16:45:31 +01002262 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002263
Joerg Roedel21129f72009-09-01 11:59:42 +02002264 /*
2265 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002266 * passthrough domain if it is detached from any other domain.
2267 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002268 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002269 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002270 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002271 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002272}
2273
2274/*
2275 * Removes a device from a protection domain (with devtable_lock held)
2276 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002277static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002278{
Joerg Roedel52815b72011-11-17 17:24:28 +01002279 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002280 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002281 unsigned long flags;
2282
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002283 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002284 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002285
Joerg Roedel355bf552008-12-08 12:02:41 +01002286 /* lock device table */
2287 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002288 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002289 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002290
Joerg Roedel52815b72011-11-17 17:24:28 +01002291 if (domain->flags & PD_IOMMUV2_MASK)
2292 pdev_iommuv2_disable(to_pci_dev(dev));
2293 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002294 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002295
2296 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002297}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002298
Joerg Roedel15898bb2009-11-24 15:39:42 +01002299/*
2300 * Find out the protection domain structure for a given PCI device. This
2301 * will give us the pointer to the page table root for example.
2302 */
2303static struct protection_domain *domain_for_device(struct device *dev)
2304{
Joerg Roedel71f77582011-06-09 19:03:15 +02002305 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002306 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002307 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002308
Joerg Roedel657cbb62009-11-23 15:26:46 +01002309 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002310
Joerg Roedel2b02b092011-06-09 17:48:39 +02002311 if (dev_data->domain)
2312 return dev_data->domain;
2313
Joerg Roedel71f77582011-06-09 19:03:15 +02002314 if (dev_data->alias_data != NULL) {
2315 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002316
2317 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2318 if (alias_data->domain != NULL) {
2319 __attach_device(dev_data, alias_data->domain);
2320 dom = alias_data->domain;
2321 }
2322 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002323 }
2324
Joerg Roedel15898bb2009-11-24 15:39:42 +01002325 return dom;
2326}
2327
Joerg Roedele275a2a2008-12-10 18:27:25 +01002328static int device_change_notifier(struct notifier_block *nb,
2329 unsigned long action, void *data)
2330{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002331 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002332 struct protection_domain *domain;
2333 struct iommu_dev_data *dev_data;
2334 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002335 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002336 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002337 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002338
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002339 if (!check_device(dev))
2340 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002341
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002342 devid = get_device_id(dev);
2343 iommu = amd_iommu_rlookup_table[devid];
2344 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002345
2346 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002347 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002348
2349 domain = domain_for_device(dev);
2350
Joerg Roedele275a2a2008-12-10 18:27:25 +01002351 if (!domain)
2352 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002353 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002354 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002355 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002356 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002357 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002358
2359 iommu_init_device(dev);
2360
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002361 /*
2362 * dev_data is still NULL and
2363 * got initialized in iommu_init_device
2364 */
2365 dev_data = get_dev_data(dev);
2366
2367 if (iommu_pass_through || dev_data->iommu_v2) {
2368 dev_data->passthrough = true;
2369 attach_device(dev, pt_domain);
2370 break;
2371 }
2372
Joerg Roedel657cbb62009-11-23 15:26:46 +01002373 domain = domain_for_device(dev);
2374
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002375 /* allocate a protection domain if a device is added */
2376 dma_domain = find_protection_domain(devid);
2377 if (dma_domain)
2378 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002379 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002380 if (!dma_domain)
2381 goto out;
2382 dma_domain->target_dev = devid;
2383
2384 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2385 list_add_tail(&dma_domain->list, &iommu_pd_list);
2386 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2387
Joerg Roedelac1534a2012-06-21 14:52:40 +02002388 dev_data = get_dev_data(dev);
2389
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002390 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002391
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002392 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002393 case BUS_NOTIFY_DEL_DEVICE:
2394
2395 iommu_uninit_device(dev);
2396
Joerg Roedele275a2a2008-12-10 18:27:25 +01002397 default:
2398 goto out;
2399 }
2400
Joerg Roedele275a2a2008-12-10 18:27:25 +01002401 iommu_completion_wait(iommu);
2402
2403out:
2404 return 0;
2405}
2406
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302407static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002408 .notifier_call = device_change_notifier,
2409};
Joerg Roedel355bf552008-12-08 12:02:41 +01002410
Joerg Roedel8638c492009-12-10 11:12:25 +01002411void amd_iommu_init_notifier(void)
2412{
2413 bus_register_notifier(&pci_bus_type, &device_nb);
2414}
2415
Joerg Roedel431b2a22008-07-11 17:14:22 +02002416/*****************************************************************************
2417 *
2418 * The next functions belong to the dma_ops mapping/unmapping code.
2419 *
2420 *****************************************************************************/
2421
2422/*
2423 * In the dma_ops path we only have the struct device. This function
2424 * finds the corresponding IOMMU, the protection domain and the
2425 * requestor id for a given device.
2426 * If the device is not yet associated with a domain this is also done
2427 * in this function.
2428 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002429static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002430{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002431 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002432 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002433 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002434
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002435 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002436 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002437
Joerg Roedel94f6d192009-11-24 16:40:02 +01002438 domain = domain_for_device(dev);
2439 if (domain != NULL && !dma_ops_domain(domain))
2440 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002441
Joerg Roedel94f6d192009-11-24 16:40:02 +01002442 if (domain != NULL)
2443 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002444
Joerg Roedel15898bb2009-11-24 15:39:42 +01002445 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002446 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002447 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002448 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2449 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002450 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002451 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002452
Joerg Roedel94f6d192009-11-24 16:40:02 +01002453 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002454}
2455
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002456static void update_device_table(struct protection_domain *domain)
2457{
Joerg Roedel492667d2009-11-27 13:25:47 +01002458 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002459
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002460 list_for_each_entry(dev_data, &domain->dev_list, list)
2461 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002462}
2463
2464static void update_domain(struct protection_domain *domain)
2465{
2466 if (!domain->updated)
2467 return;
2468
2469 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002470
2471 domain_flush_devices(domain);
2472 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002473
2474 domain->updated = false;
2475}
2476
Joerg Roedel431b2a22008-07-11 17:14:22 +02002477/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002478 * This function fetches the PTE for a given address in the aperture
2479 */
2480static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2481 unsigned long address)
2482{
Joerg Roedel384de722009-05-15 12:30:05 +02002483 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002484 u64 *pte, *pte_page;
2485
Joerg Roedel384de722009-05-15 12:30:05 +02002486 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2487 if (!aperture)
2488 return NULL;
2489
2490 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002491 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002492 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002493 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002494 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2495 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002496 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002497
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002498 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002499
2500 return pte;
2501}
2502
2503/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002504 * This is the generic map function. It maps one 4kb page at paddr to
2505 * the given address in the DMA address space for the domain.
2506 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002507static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002508 unsigned long address,
2509 phys_addr_t paddr,
2510 int direction)
2511{
2512 u64 *pte, __pte;
2513
2514 WARN_ON(address > dom->aperture_size);
2515
2516 paddr &= PAGE_MASK;
2517
Joerg Roedel8bda3092009-05-12 12:02:46 +02002518 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002519 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002520 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002521
2522 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2523
2524 if (direction == DMA_TO_DEVICE)
2525 __pte |= IOMMU_PTE_IR;
2526 else if (direction == DMA_FROM_DEVICE)
2527 __pte |= IOMMU_PTE_IW;
2528 else if (direction == DMA_BIDIRECTIONAL)
2529 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2530
2531 WARN_ON(*pte);
2532
2533 *pte = __pte;
2534
2535 return (dma_addr_t)address;
2536}
2537
Joerg Roedel431b2a22008-07-11 17:14:22 +02002538/*
2539 * The generic unmapping function for on page in the DMA address space.
2540 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002541static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002542 unsigned long address)
2543{
Joerg Roedel384de722009-05-15 12:30:05 +02002544 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002545 u64 *pte;
2546
2547 if (address >= dom->aperture_size)
2548 return;
2549
Joerg Roedel384de722009-05-15 12:30:05 +02002550 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2551 if (!aperture)
2552 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002553
Joerg Roedel384de722009-05-15 12:30:05 +02002554 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2555 if (!pte)
2556 return;
2557
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002558 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002559
2560 WARN_ON(!*pte);
2561
2562 *pte = 0ULL;
2563}
2564
Joerg Roedel431b2a22008-07-11 17:14:22 +02002565/*
2566 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002567 * contiguous memory region into DMA address space. It is used by all
2568 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002569 * Must be called with the domain lock held.
2570 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002571static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002572 struct dma_ops_domain *dma_dom,
2573 phys_addr_t paddr,
2574 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002575 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002576 bool align,
2577 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002578{
2579 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002580 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002581 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002582 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002583 int i;
2584
Joerg Roedele3c449f2008-10-15 22:02:11 -07002585 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002586 paddr &= PAGE_MASK;
2587
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002588 INC_STATS_COUNTER(total_map_requests);
2589
Joerg Roedelc1858972008-12-12 15:42:39 +01002590 if (pages > 1)
2591 INC_STATS_COUNTER(cross_page);
2592
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002593 if (align)
2594 align_mask = (1UL << get_order(size)) - 1;
2595
Joerg Roedel11b83882009-05-19 10:23:15 +02002596retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002597 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2598 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002599 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002600 /*
2601 * setting next_address here will let the address
2602 * allocator only scan the new allocated range in the
2603 * first run. This is a small optimization.
2604 */
2605 dma_dom->next_address = dma_dom->aperture_size;
2606
Joerg Roedel576175c2009-11-23 19:08:46 +01002607 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002608 goto out;
2609
2610 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002611 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002612 * allocation again
2613 */
2614 goto retry;
2615 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002616
2617 start = address;
2618 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002619 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002620 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002621 goto out_unmap;
2622
Joerg Roedelcb76c322008-06-26 21:28:00 +02002623 paddr += PAGE_SIZE;
2624 start += PAGE_SIZE;
2625 }
2626 address += offset;
2627
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002628 ADD_STATS_COUNTER(alloced_io_mem, size);
2629
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002630 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002631 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002632 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002633 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002634 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002635
Joerg Roedelcb76c322008-06-26 21:28:00 +02002636out:
2637 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002638
2639out_unmap:
2640
2641 for (--i; i >= 0; --i) {
2642 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002643 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002644 }
2645
2646 dma_ops_free_addresses(dma_dom, address, pages);
2647
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002648 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002649}
2650
Joerg Roedel431b2a22008-07-11 17:14:22 +02002651/*
2652 * Does the reverse of the __map_single function. Must be called with
2653 * the domain lock held too
2654 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002655static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002656 dma_addr_t dma_addr,
2657 size_t size,
2658 int dir)
2659{
Joerg Roedel04e04632010-09-23 16:12:48 +02002660 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002661 dma_addr_t i, start;
2662 unsigned int pages;
2663
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002664 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002665 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002666 return;
2667
Joerg Roedel04e04632010-09-23 16:12:48 +02002668 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002669 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002670 dma_addr &= PAGE_MASK;
2671 start = dma_addr;
2672
2673 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002674 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002675 start += PAGE_SIZE;
2676 }
2677
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002678 SUB_STATS_COUNTER(alloced_io_mem, size);
2679
Joerg Roedelcb76c322008-06-26 21:28:00 +02002680 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002681
Joerg Roedel80be3082008-11-06 14:59:05 +01002682 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002683 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002684 dma_dom->need_flush = false;
2685 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002686}
2687
Joerg Roedel431b2a22008-07-11 17:14:22 +02002688/*
2689 * The exported map_single function for dma_ops.
2690 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002691static dma_addr_t map_page(struct device *dev, struct page *page,
2692 unsigned long offset, size_t size,
2693 enum dma_data_direction dir,
2694 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002695{
2696 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002697 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002698 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002699 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002700 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002701
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002702 INC_STATS_COUNTER(cnt_map_single);
2703
Joerg Roedel94f6d192009-11-24 16:40:02 +01002704 domain = get_domain(dev);
2705 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002706 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002707 else if (IS_ERR(domain))
2708 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002709
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002710 dma_mask = *dev->dma_mask;
2711
Joerg Roedel4da70b92008-06-26 21:28:01 +02002712 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002713
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002714 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002715 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002716 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002717 goto out;
2718
Joerg Roedel17b124b2011-04-06 18:01:35 +02002719 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002720
2721out:
2722 spin_unlock_irqrestore(&domain->lock, flags);
2723
2724 return addr;
2725}
2726
Joerg Roedel431b2a22008-07-11 17:14:22 +02002727/*
2728 * The exported unmap_single function for dma_ops.
2729 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002730static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2731 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002732{
2733 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002734 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002735
Joerg Roedel146a6912008-12-12 15:07:12 +01002736 INC_STATS_COUNTER(cnt_unmap_single);
2737
Joerg Roedel94f6d192009-11-24 16:40:02 +01002738 domain = get_domain(dev);
2739 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002740 return;
2741
Joerg Roedel4da70b92008-06-26 21:28:01 +02002742 spin_lock_irqsave(&domain->lock, flags);
2743
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002744 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002745
Joerg Roedel17b124b2011-04-06 18:01:35 +02002746 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002747
2748 spin_unlock_irqrestore(&domain->lock, flags);
2749}
2750
Joerg Roedel431b2a22008-07-11 17:14:22 +02002751/*
2752 * This is a special map_sg function which is used if we should map a
2753 * device which is not handled by an AMD IOMMU in the system.
2754 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002755static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2756 int nelems, int dir)
2757{
2758 struct scatterlist *s;
2759 int i;
2760
2761 for_each_sg(sglist, s, nelems, i) {
2762 s->dma_address = (dma_addr_t)sg_phys(s);
2763 s->dma_length = s->length;
2764 }
2765
2766 return nelems;
2767}
2768
Joerg Roedel431b2a22008-07-11 17:14:22 +02002769/*
2770 * The exported map_sg function for dma_ops (handles scatter-gather
2771 * lists).
2772 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002773static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002774 int nelems, enum dma_data_direction dir,
2775 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002776{
2777 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002778 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002779 int i;
2780 struct scatterlist *s;
2781 phys_addr_t paddr;
2782 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002783 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002784
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002785 INC_STATS_COUNTER(cnt_map_sg);
2786
Joerg Roedel94f6d192009-11-24 16:40:02 +01002787 domain = get_domain(dev);
2788 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002789 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002790 else if (IS_ERR(domain))
2791 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002792
Joerg Roedel832a90c2008-09-18 15:54:23 +02002793 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002794
Joerg Roedel65b050a2008-06-26 21:28:02 +02002795 spin_lock_irqsave(&domain->lock, flags);
2796
2797 for_each_sg(sglist, s, nelems, i) {
2798 paddr = sg_phys(s);
2799
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002800 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002801 paddr, s->length, dir, false,
2802 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002803
2804 if (s->dma_address) {
2805 s->dma_length = s->length;
2806 mapped_elems++;
2807 } else
2808 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002809 }
2810
Joerg Roedel17b124b2011-04-06 18:01:35 +02002811 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002812
2813out:
2814 spin_unlock_irqrestore(&domain->lock, flags);
2815
2816 return mapped_elems;
2817unmap:
2818 for_each_sg(sglist, s, mapped_elems, i) {
2819 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002820 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002821 s->dma_length, dir);
2822 s->dma_address = s->dma_length = 0;
2823 }
2824
2825 mapped_elems = 0;
2826
2827 goto out;
2828}
2829
Joerg Roedel431b2a22008-07-11 17:14:22 +02002830/*
2831 * The exported map_sg function for dma_ops (handles scatter-gather
2832 * lists).
2833 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002834static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002835 int nelems, enum dma_data_direction dir,
2836 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002837{
2838 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002839 struct protection_domain *domain;
2840 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002841 int i;
2842
Joerg Roedel55877a62008-12-12 15:12:14 +01002843 INC_STATS_COUNTER(cnt_unmap_sg);
2844
Joerg Roedel94f6d192009-11-24 16:40:02 +01002845 domain = get_domain(dev);
2846 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002847 return;
2848
Joerg Roedel65b050a2008-06-26 21:28:02 +02002849 spin_lock_irqsave(&domain->lock, flags);
2850
2851 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002852 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002853 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002854 s->dma_address = s->dma_length = 0;
2855 }
2856
Joerg Roedel17b124b2011-04-06 18:01:35 +02002857 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002858
2859 spin_unlock_irqrestore(&domain->lock, flags);
2860}
2861
Joerg Roedel431b2a22008-07-11 17:14:22 +02002862/*
2863 * The exported alloc_coherent function for dma_ops.
2864 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002865static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002866 dma_addr_t *dma_addr, gfp_t flag,
2867 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002868{
2869 unsigned long flags;
2870 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002871 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002872 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002873 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002874
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002875 INC_STATS_COUNTER(cnt_alloc_coherent);
2876
Joerg Roedel94f6d192009-11-24 16:40:02 +01002877 domain = get_domain(dev);
2878 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002879 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2880 *dma_addr = __pa(virt_addr);
2881 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002882 } else if (IS_ERR(domain))
2883 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002884
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002885 dma_mask = dev->coherent_dma_mask;
2886 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2887 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002888
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002889 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2890 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302891 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002892
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002893 paddr = virt_to_phys(virt_addr);
2894
Joerg Roedel832a90c2008-09-18 15:54:23 +02002895 if (!dma_mask)
2896 dma_mask = *dev->dma_mask;
2897
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002898 spin_lock_irqsave(&domain->lock, flags);
2899
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002900 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002901 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002902
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002903 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002904 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002905 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002906 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002907
Joerg Roedel17b124b2011-04-06 18:01:35 +02002908 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002909
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002910 spin_unlock_irqrestore(&domain->lock, flags);
2911
2912 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002913
2914out_free:
2915
2916 free_pages((unsigned long)virt_addr, get_order(size));
2917
2918 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002919}
2920
Joerg Roedel431b2a22008-07-11 17:14:22 +02002921/*
2922 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002923 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002924static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002925 void *virt_addr, dma_addr_t dma_addr,
2926 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002927{
2928 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002929 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002930
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002931 INC_STATS_COUNTER(cnt_free_coherent);
2932
Joerg Roedel94f6d192009-11-24 16:40:02 +01002933 domain = get_domain(dev);
2934 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002935 goto free_mem;
2936
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002937 spin_lock_irqsave(&domain->lock, flags);
2938
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002939 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002940
Joerg Roedel17b124b2011-04-06 18:01:35 +02002941 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002942
2943 spin_unlock_irqrestore(&domain->lock, flags);
2944
2945free_mem:
2946 free_pages((unsigned long)virt_addr, get_order(size));
2947}
2948
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002949/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002950 * This function is called by the DMA layer to find out if we can handle a
2951 * particular device. It is part of the dma_ops.
2952 */
2953static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2954{
Joerg Roedel420aef82009-11-23 16:14:57 +01002955 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002956}
2957
2958/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002959 * The function for pre-allocating protection domains.
2960 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002961 * If the driver core informs the DMA layer if a driver grabs a device
2962 * we don't need to preallocate the protection domains anymore.
2963 * For now we have to.
2964 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01002965static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002966{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002967 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002968 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002969 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002970 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002971
Chris Wrightd18c69d2010-04-02 18:27:55 -07002972 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002973
2974 /* Do we handle this device? */
2975 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002976 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002977
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002978 dev_data = get_dev_data(&dev->dev);
2979 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2980 /* Make sure passthrough domain is allocated */
2981 alloc_passthrough_domain();
2982 dev_data->passthrough = true;
2983 attach_device(&dev->dev, pt_domain);
2984 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2985 dev_name(&dev->dev));
2986 }
2987
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002988 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002989 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002990 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002991
2992 devid = get_device_id(&dev->dev);
2993
Joerg Roedel87a64d52009-11-24 17:26:43 +01002994 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002995 if (!dma_dom)
2996 continue;
2997 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002998 dma_dom->target_dev = devid;
2999
Joerg Roedel15898bb2009-11-24 15:39:42 +01003000 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003001
Joerg Roedelbd60b732008-09-11 10:24:48 +02003002 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003003 }
3004}
3005
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003006static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003007 .alloc = alloc_coherent,
3008 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003009 .map_page = map_page,
3010 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003011 .map_sg = map_sg,
3012 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003013 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003014};
3015
Joerg Roedel27c21272011-05-30 15:56:24 +02003016static unsigned device_dma_ops_init(void)
3017{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003018 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003019 struct pci_dev *pdev = NULL;
3020 unsigned unhandled = 0;
3021
3022 for_each_pci_dev(pdev) {
3023 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003024
3025 iommu_ignore_device(&pdev->dev);
3026
Joerg Roedel27c21272011-05-30 15:56:24 +02003027 unhandled += 1;
3028 continue;
3029 }
3030
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003031 dev_data = get_dev_data(&pdev->dev);
3032
3033 if (!dev_data->passthrough)
3034 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3035 else
3036 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003037 }
3038
3039 return unhandled;
3040}
3041
Joerg Roedel431b2a22008-07-11 17:14:22 +02003042/*
3043 * The function which clues the AMD IOMMU driver into dma_ops.
3044 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003045
3046void __init amd_iommu_init_api(void)
3047{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003048 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003049}
3050
Joerg Roedel6631ee92008-06-26 21:28:05 +02003051int __init amd_iommu_init_dma_ops(void)
3052{
3053 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003054 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003055
Joerg Roedel431b2a22008-07-11 17:14:22 +02003056 /*
3057 * first allocate a default protection domain for every IOMMU we
3058 * found in the system. Devices not assigned to any other
3059 * protection domain will be assigned to the default one.
3060 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003061 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003062 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003063 if (iommu->default_dom == NULL)
3064 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003065 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003066 ret = iommu_init_unity_mappings(iommu);
3067 if (ret)
3068 goto free_domains;
3069 }
3070
Joerg Roedel431b2a22008-07-11 17:14:22 +02003071 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003072 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003073 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003074 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003075
3076 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003077 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003078
Joerg Roedel431b2a22008-07-11 17:14:22 +02003079 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003080 unhandled = device_dma_ops_init();
3081 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3082 /* There are unhandled devices - initialize swiotlb for them */
3083 swiotlb = 1;
3084 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003085
Joerg Roedel7f265082008-12-12 13:50:21 +01003086 amd_iommu_stats_init();
3087
Joerg Roedel62410ee2012-06-12 16:42:43 +02003088 if (amd_iommu_unmap_flush)
3089 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3090 else
3091 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3092
Joerg Roedel6631ee92008-06-26 21:28:05 +02003093 return 0;
3094
3095free_domains:
3096
Joerg Roedel3bd22172009-05-04 15:06:20 +02003097 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003098 if (iommu->default_dom)
3099 dma_ops_domain_free(iommu->default_dom);
3100 }
3101
3102 return ret;
3103}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003104
3105/*****************************************************************************
3106 *
3107 * The following functions belong to the exported interface of AMD IOMMU
3108 *
3109 * This interface allows access to lower level functions of the IOMMU
3110 * like protection domain handling and assignement of devices to domains
3111 * which is not possible with the dma_ops interface.
3112 *
3113 *****************************************************************************/
3114
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003115static void cleanup_domain(struct protection_domain *domain)
3116{
Joerg Roedel492667d2009-11-27 13:25:47 +01003117 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003118 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003119
3120 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3121
Joerg Roedel492667d2009-11-27 13:25:47 +01003122 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003123 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003124 atomic_set(&dev_data->bind, 0);
3125 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003126
3127 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3128}
3129
Joerg Roedel26508152009-08-26 16:52:40 +02003130static void protection_domain_free(struct protection_domain *domain)
3131{
3132 if (!domain)
3133 return;
3134
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003135 del_domain_from_list(domain);
3136
Joerg Roedel26508152009-08-26 16:52:40 +02003137 if (domain->id)
3138 domain_id_free(domain->id);
3139
3140 kfree(domain);
3141}
3142
3143static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003144{
3145 struct protection_domain *domain;
3146
3147 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3148 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003149 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003150
3151 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003152 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003153 domain->id = domain_id_alloc();
3154 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003155 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003156 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003157
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003158 add_domain_to_list(domain);
3159
Joerg Roedel26508152009-08-26 16:52:40 +02003160 return domain;
3161
3162out_err:
3163 kfree(domain);
3164
3165 return NULL;
3166}
3167
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003168static int __init alloc_passthrough_domain(void)
3169{
3170 if (pt_domain != NULL)
3171 return 0;
3172
3173 /* allocate passthrough domain */
3174 pt_domain = protection_domain_alloc();
3175 if (!pt_domain)
3176 return -ENOMEM;
3177
3178 pt_domain->mode = PAGE_MODE_NONE;
3179
3180 return 0;
3181}
Joerg Roedel26508152009-08-26 16:52:40 +02003182static int amd_iommu_domain_init(struct iommu_domain *dom)
3183{
3184 struct protection_domain *domain;
3185
3186 domain = protection_domain_alloc();
3187 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003188 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003189
3190 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003191 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3192 if (!domain->pt_root)
3193 goto out_free;
3194
Joerg Roedelf3572db2011-11-23 12:36:25 +01003195 domain->iommu_domain = dom;
3196
Joerg Roedelc156e342008-12-02 18:13:27 +01003197 dom->priv = domain;
3198
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003199 dom->geometry.aperture_start = 0;
3200 dom->geometry.aperture_end = ~0ULL;
3201 dom->geometry.force_aperture = true;
3202
Joerg Roedelc156e342008-12-02 18:13:27 +01003203 return 0;
3204
3205out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003206 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003207
3208 return -ENOMEM;
3209}
3210
Joerg Roedel98383fc2008-12-02 18:34:12 +01003211static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3212{
3213 struct protection_domain *domain = dom->priv;
3214
3215 if (!domain)
3216 return;
3217
3218 if (domain->dev_cnt > 0)
3219 cleanup_domain(domain);
3220
3221 BUG_ON(domain->dev_cnt != 0);
3222
Joerg Roedel132bd682011-11-17 14:18:46 +01003223 if (domain->mode != PAGE_MODE_NONE)
3224 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003225
Joerg Roedel52815b72011-11-17 17:24:28 +01003226 if (domain->flags & PD_IOMMUV2_MASK)
3227 free_gcr3_table(domain);
3228
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003229 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003230
3231 dom->priv = NULL;
3232}
3233
Joerg Roedel684f2882008-12-08 12:07:44 +01003234static void amd_iommu_detach_device(struct iommu_domain *dom,
3235 struct device *dev)
3236{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003237 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003238 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003239 u16 devid;
3240
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003241 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003242 return;
3243
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003244 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003245
Joerg Roedel657cbb62009-11-23 15:26:46 +01003246 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003247 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003248
3249 iommu = amd_iommu_rlookup_table[devid];
3250 if (!iommu)
3251 return;
3252
Joerg Roedel684f2882008-12-08 12:07:44 +01003253 iommu_completion_wait(iommu);
3254}
3255
Joerg Roedel01106062008-12-02 19:34:11 +01003256static int amd_iommu_attach_device(struct iommu_domain *dom,
3257 struct device *dev)
3258{
3259 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003260 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003261 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003262 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003263
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003264 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003265 return -EINVAL;
3266
Joerg Roedel657cbb62009-11-23 15:26:46 +01003267 dev_data = dev->archdata.iommu;
3268
Joerg Roedelf62dda62011-06-09 12:55:35 +02003269 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003270 if (!iommu)
3271 return -EINVAL;
3272
Joerg Roedel657cbb62009-11-23 15:26:46 +01003273 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003274 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003275
Joerg Roedel15898bb2009-11-24 15:39:42 +01003276 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003277
3278 iommu_completion_wait(iommu);
3279
Joerg Roedel15898bb2009-11-24 15:39:42 +01003280 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003281}
3282
Joerg Roedel468e2362010-01-21 16:37:36 +01003283static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003284 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003285{
3286 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003287 int prot = 0;
3288 int ret;
3289
Joerg Roedel132bd682011-11-17 14:18:46 +01003290 if (domain->mode == PAGE_MODE_NONE)
3291 return -EINVAL;
3292
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003293 if (iommu_prot & IOMMU_READ)
3294 prot |= IOMMU_PROT_IR;
3295 if (iommu_prot & IOMMU_WRITE)
3296 prot |= IOMMU_PROT_IW;
3297
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003298 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003299 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003300 mutex_unlock(&domain->api_lock);
3301
Joerg Roedel795e74f72010-05-11 17:40:57 +02003302 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003303}
3304
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003305static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3306 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003307{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003308 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003309 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003310
Joerg Roedel132bd682011-11-17 14:18:46 +01003311 if (domain->mode == PAGE_MODE_NONE)
3312 return -EINVAL;
3313
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003314 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003315 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003316 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003317
Joerg Roedel17b124b2011-04-06 18:01:35 +02003318 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003319
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003320 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003321}
3322
Joerg Roedel645c4c82008-12-02 20:05:50 +01003323static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3324 unsigned long iova)
3325{
3326 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003327 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003328 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003329 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003330
Joerg Roedel132bd682011-11-17 14:18:46 +01003331 if (domain->mode == PAGE_MODE_NONE)
3332 return iova;
3333
Joerg Roedel24cd7722010-01-19 17:27:39 +01003334 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003335
Joerg Roedela6d41a42009-09-02 17:08:55 +02003336 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003337 return 0;
3338
Joerg Roedelf03152b2010-01-21 16:15:24 +01003339 if (PM_PTE_LEVEL(*pte) == 0)
3340 offset_mask = PAGE_SIZE - 1;
3341 else
3342 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3343
3344 __pte = *pte & PM_ADDR_MASK;
3345 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003346
3347 return paddr;
3348}
3349
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003350static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3351 unsigned long cap)
3352{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003353 switch (cap) {
3354 case IOMMU_CAP_CACHE_COHERENCY:
3355 return 1;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003356 case IOMMU_CAP_INTR_REMAP:
3357 return irq_remapping_enabled;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003358 }
3359
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003360 return 0;
3361}
3362
Joerg Roedel26961ef2008-12-03 17:00:17 +01003363static struct iommu_ops amd_iommu_ops = {
3364 .domain_init = amd_iommu_domain_init,
3365 .domain_destroy = amd_iommu_domain_destroy,
3366 .attach_dev = amd_iommu_attach_device,
3367 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003368 .map = amd_iommu_map,
3369 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003370 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003371 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003372 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003373};
3374
Joerg Roedel0feae532009-08-26 15:26:30 +02003375/*****************************************************************************
3376 *
3377 * The next functions do a basic initialization of IOMMU for pass through
3378 * mode
3379 *
3380 * In passthrough mode the IOMMU is initialized and enabled but not used for
3381 * DMA-API translation.
3382 *
3383 *****************************************************************************/
3384
3385int __init amd_iommu_init_passthrough(void)
3386{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003387 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003388 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003389 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003390 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003391 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003392
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003393 ret = alloc_passthrough_domain();
3394 if (ret)
3395 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003396
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003397 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003398 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003399 continue;
3400
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003401 dev_data = get_dev_data(&dev->dev);
3402 dev_data->passthrough = true;
3403
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003404 devid = get_device_id(&dev->dev);
3405
Joerg Roedel15898bb2009-11-24 15:39:42 +01003406 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003407 if (!iommu)
3408 continue;
3409
Joerg Roedel15898bb2009-11-24 15:39:42 +01003410 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003411 }
3412
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003413 amd_iommu_stats_init();
3414
Joerg Roedel0feae532009-08-26 15:26:30 +02003415 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3416
3417 return 0;
3418}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003419
3420/* IOMMUv2 specific functions */
3421int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3422{
3423 return atomic_notifier_chain_register(&ppr_notifier, nb);
3424}
3425EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3426
3427int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3428{
3429 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3430}
3431EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003432
3433void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3434{
3435 struct protection_domain *domain = dom->priv;
3436 unsigned long flags;
3437
3438 spin_lock_irqsave(&domain->lock, flags);
3439
3440 /* Update data structure */
3441 domain->mode = PAGE_MODE_NONE;
3442 domain->updated = true;
3443
3444 /* Make changes visible to IOMMUs */
3445 update_domain(domain);
3446
3447 /* Page-table is not visible to IOMMU anymore, so free it */
3448 free_pagetable(domain);
3449
3450 spin_unlock_irqrestore(&domain->lock, flags);
3451}
3452EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003453
3454int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3455{
3456 struct protection_domain *domain = dom->priv;
3457 unsigned long flags;
3458 int levels, ret;
3459
3460 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3461 return -EINVAL;
3462
3463 /* Number of GCR3 table levels required */
3464 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3465 levels += 1;
3466
3467 if (levels > amd_iommu_max_glx_val)
3468 return -EINVAL;
3469
3470 spin_lock_irqsave(&domain->lock, flags);
3471
3472 /*
3473 * Save us all sanity checks whether devices already in the
3474 * domain support IOMMUv2. Just force that the domain has no
3475 * devices attached when it is switched into IOMMUv2 mode.
3476 */
3477 ret = -EBUSY;
3478 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3479 goto out;
3480
3481 ret = -ENOMEM;
3482 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3483 if (domain->gcr3_tbl == NULL)
3484 goto out;
3485
3486 domain->glx = levels;
3487 domain->flags |= PD_IOMMUV2_MASK;
3488 domain->updated = true;
3489
3490 update_domain(domain);
3491
3492 ret = 0;
3493
3494out:
3495 spin_unlock_irqrestore(&domain->lock, flags);
3496
3497 return ret;
3498}
3499EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003500
3501static int __flush_pasid(struct protection_domain *domain, int pasid,
3502 u64 address, bool size)
3503{
3504 struct iommu_dev_data *dev_data;
3505 struct iommu_cmd cmd;
3506 int i, ret;
3507
3508 if (!(domain->flags & PD_IOMMUV2_MASK))
3509 return -EINVAL;
3510
3511 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3512
3513 /*
3514 * IOMMU TLB needs to be flushed before Device TLB to
3515 * prevent device TLB refill from IOMMU TLB
3516 */
3517 for (i = 0; i < amd_iommus_present; ++i) {
3518 if (domain->dev_iommu[i] == 0)
3519 continue;
3520
3521 ret = iommu_queue_command(amd_iommus[i], &cmd);
3522 if (ret != 0)
3523 goto out;
3524 }
3525
3526 /* Wait until IOMMU TLB flushes are complete */
3527 domain_flush_complete(domain);
3528
3529 /* Now flush device TLBs */
3530 list_for_each_entry(dev_data, &domain->dev_list, list) {
3531 struct amd_iommu *iommu;
3532 int qdep;
3533
3534 BUG_ON(!dev_data->ats.enabled);
3535
3536 qdep = dev_data->ats.qdep;
3537 iommu = amd_iommu_rlookup_table[dev_data->devid];
3538
3539 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3540 qdep, address, size);
3541
3542 ret = iommu_queue_command(iommu, &cmd);
3543 if (ret != 0)
3544 goto out;
3545 }
3546
3547 /* Wait until all device TLBs are flushed */
3548 domain_flush_complete(domain);
3549
3550 ret = 0;
3551
3552out:
3553
3554 return ret;
3555}
3556
3557static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3558 u64 address)
3559{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003560 INC_STATS_COUNTER(invalidate_iotlb);
3561
Joerg Roedel22e266c2011-11-21 15:59:08 +01003562 return __flush_pasid(domain, pasid, address, false);
3563}
3564
3565int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3566 u64 address)
3567{
3568 struct protection_domain *domain = dom->priv;
3569 unsigned long flags;
3570 int ret;
3571
3572 spin_lock_irqsave(&domain->lock, flags);
3573 ret = __amd_iommu_flush_page(domain, pasid, address);
3574 spin_unlock_irqrestore(&domain->lock, flags);
3575
3576 return ret;
3577}
3578EXPORT_SYMBOL(amd_iommu_flush_page);
3579
3580static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3581{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003582 INC_STATS_COUNTER(invalidate_iotlb_all);
3583
Joerg Roedel22e266c2011-11-21 15:59:08 +01003584 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3585 true);
3586}
3587
3588int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3589{
3590 struct protection_domain *domain = dom->priv;
3591 unsigned long flags;
3592 int ret;
3593
3594 spin_lock_irqsave(&domain->lock, flags);
3595 ret = __amd_iommu_flush_tlb(domain, pasid);
3596 spin_unlock_irqrestore(&domain->lock, flags);
3597
3598 return ret;
3599}
3600EXPORT_SYMBOL(amd_iommu_flush_tlb);
3601
Joerg Roedelb16137b2011-11-21 16:50:23 +01003602static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3603{
3604 int index;
3605 u64 *pte;
3606
3607 while (true) {
3608
3609 index = (pasid >> (9 * level)) & 0x1ff;
3610 pte = &root[index];
3611
3612 if (level == 0)
3613 break;
3614
3615 if (!(*pte & GCR3_VALID)) {
3616 if (!alloc)
3617 return NULL;
3618
3619 root = (void *)get_zeroed_page(GFP_ATOMIC);
3620 if (root == NULL)
3621 return NULL;
3622
3623 *pte = __pa(root) | GCR3_VALID;
3624 }
3625
3626 root = __va(*pte & PAGE_MASK);
3627
3628 level -= 1;
3629 }
3630
3631 return pte;
3632}
3633
3634static int __set_gcr3(struct protection_domain *domain, int pasid,
3635 unsigned long cr3)
3636{
3637 u64 *pte;
3638
3639 if (domain->mode != PAGE_MODE_NONE)
3640 return -EINVAL;
3641
3642 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3643 if (pte == NULL)
3644 return -ENOMEM;
3645
3646 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3647
3648 return __amd_iommu_flush_tlb(domain, pasid);
3649}
3650
3651static int __clear_gcr3(struct protection_domain *domain, int pasid)
3652{
3653 u64 *pte;
3654
3655 if (domain->mode != PAGE_MODE_NONE)
3656 return -EINVAL;
3657
3658 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3659 if (pte == NULL)
3660 return 0;
3661
3662 *pte = 0;
3663
3664 return __amd_iommu_flush_tlb(domain, pasid);
3665}
3666
3667int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3668 unsigned long cr3)
3669{
3670 struct protection_domain *domain = dom->priv;
3671 unsigned long flags;
3672 int ret;
3673
3674 spin_lock_irqsave(&domain->lock, flags);
3675 ret = __set_gcr3(domain, pasid, cr3);
3676 spin_unlock_irqrestore(&domain->lock, flags);
3677
3678 return ret;
3679}
3680EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3681
3682int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3683{
3684 struct protection_domain *domain = dom->priv;
3685 unsigned long flags;
3686 int ret;
3687
3688 spin_lock_irqsave(&domain->lock, flags);
3689 ret = __clear_gcr3(domain, pasid);
3690 spin_unlock_irqrestore(&domain->lock, flags);
3691
3692 return ret;
3693}
3694EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003695
3696int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3697 int status, int tag)
3698{
3699 struct iommu_dev_data *dev_data;
3700 struct amd_iommu *iommu;
3701 struct iommu_cmd cmd;
3702
Joerg Roedel399be2f2011-12-01 16:53:47 +01003703 INC_STATS_COUNTER(complete_ppr);
3704
Joerg Roedelc99afa22011-11-21 18:19:25 +01003705 dev_data = get_dev_data(&pdev->dev);
3706 iommu = amd_iommu_rlookup_table[dev_data->devid];
3707
3708 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3709 tag, dev_data->pri_tlp);
3710
3711 return iommu_queue_command(iommu, &cmd);
3712}
3713EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003714
3715struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3716{
3717 struct protection_domain *domain;
3718
3719 domain = get_domain(&pdev->dev);
3720 if (IS_ERR(domain))
3721 return NULL;
3722
3723 /* Only return IOMMUv2 domains */
3724 if (!(domain->flags & PD_IOMMUV2_MASK))
3725 return NULL;
3726
3727 return domain->iommu_domain;
3728}
3729EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003730
3731void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3732{
3733 struct iommu_dev_data *dev_data;
3734
3735 if (!amd_iommu_v2_supported())
3736 return;
3737
3738 dev_data = get_dev_data(&pdev->dev);
3739 dev_data->errata |= (1 << erratum);
3740}
3741EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003742
3743int amd_iommu_device_info(struct pci_dev *pdev,
3744 struct amd_iommu_device_info *info)
3745{
3746 int max_pasids;
3747 int pos;
3748
3749 if (pdev == NULL || info == NULL)
3750 return -EINVAL;
3751
3752 if (!amd_iommu_v2_supported())
3753 return -EINVAL;
3754
3755 memset(info, 0, sizeof(*info));
3756
3757 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3758 if (pos)
3759 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3760
3761 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3762 if (pos)
3763 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3764
3765 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3766 if (pos) {
3767 int features;
3768
3769 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3770 max_pasids = min(max_pasids, (1 << 20));
3771
3772 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3773 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3774
3775 features = pci_pasid_features(pdev);
3776 if (features & PCI_PASID_CAP_EXEC)
3777 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3778 if (features & PCI_PASID_CAP_PRIV)
3779 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3780 }
3781
3782 return 0;
3783}
3784EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003785
3786#ifdef CONFIG_IRQ_REMAP
3787
3788/*****************************************************************************
3789 *
3790 * Interrupt Remapping Implementation
3791 *
3792 *****************************************************************************/
3793
3794union irte {
3795 u32 val;
3796 struct {
3797 u32 valid : 1,
3798 no_fault : 1,
3799 int_type : 3,
3800 rq_eoi : 1,
3801 dm : 1,
3802 rsvd_1 : 1,
3803 destination : 8,
3804 vector : 8,
3805 rsvd_2 : 8;
3806 } fields;
3807};
3808
3809#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3810#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3811#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3812#define DTE_IRQ_REMAP_ENABLE 1ULL
3813
3814static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3815{
3816 u64 dte;
3817
3818 dte = amd_iommu_dev_table[devid].data[2];
3819 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3820 dte |= virt_to_phys(table->table);
3821 dte |= DTE_IRQ_REMAP_INTCTL;
3822 dte |= DTE_IRQ_TABLE_LEN;
3823 dte |= DTE_IRQ_REMAP_ENABLE;
3824
3825 amd_iommu_dev_table[devid].data[2] = dte;
3826}
3827
3828#define IRTE_ALLOCATED (~1U)
3829
3830static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3831{
3832 struct irq_remap_table *table = NULL;
3833 struct amd_iommu *iommu;
3834 unsigned long flags;
3835 u16 alias;
3836
3837 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3838
3839 iommu = amd_iommu_rlookup_table[devid];
3840 if (!iommu)
3841 goto out_unlock;
3842
3843 table = irq_lookup_table[devid];
3844 if (table)
3845 goto out;
3846
3847 alias = amd_iommu_alias_table[devid];
3848 table = irq_lookup_table[alias];
3849 if (table) {
3850 irq_lookup_table[devid] = table;
3851 set_dte_irq_entry(devid, table);
3852 iommu_flush_dte(iommu, devid);
3853 goto out;
3854 }
3855
3856 /* Nothing there yet, allocate new irq remapping table */
3857 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3858 if (!table)
3859 goto out;
3860
3861 if (ioapic)
3862 /* Keep the first 32 indexes free for IOAPIC interrupts */
3863 table->min_index = 32;
3864
3865 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3866 if (!table->table) {
3867 kfree(table);
3868 goto out;
3869 }
3870
3871 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3872
3873 if (ioapic) {
3874 int i;
3875
3876 for (i = 0; i < 32; ++i)
3877 table->table[i] = IRTE_ALLOCATED;
3878 }
3879
3880 irq_lookup_table[devid] = table;
3881 set_dte_irq_entry(devid, table);
3882 iommu_flush_dte(iommu, devid);
3883 if (devid != alias) {
3884 irq_lookup_table[alias] = table;
3885 set_dte_irq_entry(devid, table);
3886 iommu_flush_dte(iommu, alias);
3887 }
3888
3889out:
3890 iommu_completion_wait(iommu);
3891
3892out_unlock:
3893 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3894
3895 return table;
3896}
3897
3898static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3899{
3900 struct irq_remap_table *table;
3901 unsigned long flags;
3902 int index, c;
3903
3904 table = get_irq_table(devid, false);
3905 if (!table)
3906 return -ENODEV;
3907
3908 spin_lock_irqsave(&table->lock, flags);
3909
3910 /* Scan table for free entries */
3911 for (c = 0, index = table->min_index;
3912 index < MAX_IRQS_PER_TABLE;
3913 ++index) {
3914 if (table->table[index] == 0)
3915 c += 1;
3916 else
3917 c = 0;
3918
3919 if (c == count) {
3920 struct irq_2_iommu *irte_info;
3921
3922 for (; c != 0; --c)
3923 table->table[index - c + 1] = IRTE_ALLOCATED;
3924
3925 index -= count - 1;
3926
3927 irte_info = &cfg->irq_2_iommu;
3928 irte_info->sub_handle = devid;
3929 irte_info->irte_index = index;
3930 irte_info->iommu = (void *)cfg;
3931
3932 goto out;
3933 }
3934 }
3935
3936 index = -ENOSPC;
3937
3938out:
3939 spin_unlock_irqrestore(&table->lock, flags);
3940
3941 return index;
3942}
3943
3944static int get_irte(u16 devid, int index, union irte *irte)
3945{
3946 struct irq_remap_table *table;
3947 unsigned long flags;
3948
3949 table = get_irq_table(devid, false);
3950 if (!table)
3951 return -ENOMEM;
3952
3953 spin_lock_irqsave(&table->lock, flags);
3954 irte->val = table->table[index];
3955 spin_unlock_irqrestore(&table->lock, flags);
3956
3957 return 0;
3958}
3959
3960static int modify_irte(u16 devid, int index, union irte irte)
3961{
3962 struct irq_remap_table *table;
3963 struct amd_iommu *iommu;
3964 unsigned long flags;
3965
3966 iommu = amd_iommu_rlookup_table[devid];
3967 if (iommu == NULL)
3968 return -EINVAL;
3969
3970 table = get_irq_table(devid, false);
3971 if (!table)
3972 return -ENOMEM;
3973
3974 spin_lock_irqsave(&table->lock, flags);
3975 table->table[index] = irte.val;
3976 spin_unlock_irqrestore(&table->lock, flags);
3977
3978 iommu_flush_irt(iommu, devid);
3979 iommu_completion_wait(iommu);
3980
3981 return 0;
3982}
3983
3984static void free_irte(u16 devid, int index)
3985{
3986 struct irq_remap_table *table;
3987 struct amd_iommu *iommu;
3988 unsigned long flags;
3989
3990 iommu = amd_iommu_rlookup_table[devid];
3991 if (iommu == NULL)
3992 return;
3993
3994 table = get_irq_table(devid, false);
3995 if (!table)
3996 return;
3997
3998 spin_lock_irqsave(&table->lock, flags);
3999 table->table[index] = 0;
4000 spin_unlock_irqrestore(&table->lock, flags);
4001
4002 iommu_flush_irt(iommu, devid);
4003 iommu_completion_wait(iommu);
4004}
4005
Joerg Roedel5527de72012-06-26 11:17:32 +02004006static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4007 unsigned int destination, int vector,
4008 struct io_apic_irq_attr *attr)
4009{
4010 struct irq_remap_table *table;
4011 struct irq_2_iommu *irte_info;
4012 struct irq_cfg *cfg;
4013 union irte irte;
4014 int ioapic_id;
4015 int index;
4016 int devid;
4017 int ret;
4018
4019 cfg = irq_get_chip_data(irq);
4020 if (!cfg)
4021 return -EINVAL;
4022
4023 irte_info = &cfg->irq_2_iommu;
4024 ioapic_id = mpc_ioapic_id(attr->ioapic);
4025 devid = get_ioapic_devid(ioapic_id);
4026
4027 if (devid < 0)
4028 return devid;
4029
4030 table = get_irq_table(devid, true);
4031 if (table == NULL)
4032 return -ENOMEM;
4033
4034 index = attr->ioapic_pin;
4035
4036 /* Setup IRQ remapping info */
4037 irte_info->sub_handle = devid;
4038 irte_info->irte_index = index;
4039 irte_info->iommu = (void *)cfg;
4040
4041 /* Setup IRTE for IOMMU */
4042 irte.val = 0;
4043 irte.fields.vector = vector;
4044 irte.fields.int_type = apic->irq_delivery_mode;
4045 irte.fields.destination = destination;
4046 irte.fields.dm = apic->irq_dest_mode;
4047 irte.fields.valid = 1;
4048
4049 ret = modify_irte(devid, index, irte);
4050 if (ret)
4051 return ret;
4052
4053 /* Setup IOAPIC entry */
4054 memset(entry, 0, sizeof(*entry));
4055
4056 entry->vector = index;
4057 entry->mask = 0;
4058 entry->trigger = attr->trigger;
4059 entry->polarity = attr->polarity;
4060
4061 /*
4062 * Mask level triggered irqs.
4063 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
4064 */
4065 if (attr->trigger)
4066 entry->mask = 1;
4067
4068 return 0;
4069}
4070
4071static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4072 bool force)
4073{
4074 struct irq_2_iommu *irte_info;
4075 unsigned int dest, irq;
4076 struct irq_cfg *cfg;
4077 union irte irte;
4078 int err;
4079
4080 if (!config_enabled(CONFIG_SMP))
4081 return -1;
4082
4083 cfg = data->chip_data;
4084 irq = data->irq;
4085 irte_info = &cfg->irq_2_iommu;
4086
4087 if (!cpumask_intersects(mask, cpu_online_mask))
4088 return -EINVAL;
4089
4090 if (get_irte(irte_info->sub_handle, irte_info->irte_index, &irte))
4091 return -EBUSY;
4092
4093 if (assign_irq_vector(irq, cfg, mask))
4094 return -EBUSY;
4095
4096 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4097 if (err) {
4098 if (assign_irq_vector(irq, cfg, data->affinity))
4099 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4100 return err;
4101 }
4102
4103 irte.fields.vector = cfg->vector;
4104 irte.fields.destination = dest;
4105
4106 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4107
4108 if (cfg->move_in_progress)
4109 send_cleanup_vector(cfg);
4110
4111 cpumask_copy(data->affinity, mask);
4112
4113 return 0;
4114}
4115
4116static int free_irq(int irq)
4117{
4118 struct irq_2_iommu *irte_info;
4119 struct irq_cfg *cfg;
4120
4121 cfg = irq_get_chip_data(irq);
4122 if (!cfg)
4123 return -EINVAL;
4124
4125 irte_info = &cfg->irq_2_iommu;
4126
4127 free_irte(irte_info->sub_handle, irte_info->irte_index);
4128
4129 return 0;
4130}
4131
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004132static void compose_msi_msg(struct pci_dev *pdev,
4133 unsigned int irq, unsigned int dest,
4134 struct msi_msg *msg, u8 hpet_id)
4135{
4136 struct irq_2_iommu *irte_info;
4137 struct irq_cfg *cfg;
4138 union irte irte;
4139
4140 cfg = irq_get_chip_data(irq);
4141 if (!cfg)
4142 return;
4143
4144 irte_info = &cfg->irq_2_iommu;
4145
4146 irte.val = 0;
4147 irte.fields.vector = cfg->vector;
4148 irte.fields.int_type = apic->irq_delivery_mode;
4149 irte.fields.destination = dest;
4150 irte.fields.dm = apic->irq_dest_mode;
4151 irte.fields.valid = 1;
4152
4153 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4154
4155 msg->address_hi = MSI_ADDR_BASE_HI;
4156 msg->address_lo = MSI_ADDR_BASE_LO;
4157 msg->data = irte_info->irte_index;
4158}
4159
4160static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4161{
4162 struct irq_cfg *cfg;
4163 int index;
4164 u16 devid;
4165
4166 if (!pdev)
4167 return -EINVAL;
4168
4169 cfg = irq_get_chip_data(irq);
4170 if (!cfg)
4171 return -EINVAL;
4172
4173 devid = get_device_id(&pdev->dev);
4174 index = alloc_irq_index(cfg, devid, nvec);
4175
4176 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4177}
4178
4179static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4180 int index, int offset)
4181{
4182 struct irq_2_iommu *irte_info;
4183 struct irq_cfg *cfg;
4184 u16 devid;
4185
4186 if (!pdev)
4187 return -EINVAL;
4188
4189 cfg = irq_get_chip_data(irq);
4190 if (!cfg)
4191 return -EINVAL;
4192
4193 if (index >= MAX_IRQS_PER_TABLE)
4194 return 0;
4195
4196 devid = get_device_id(&pdev->dev);
4197 irte_info = &cfg->irq_2_iommu;
4198
4199 irte_info->sub_handle = devid;
4200 irte_info->irte_index = index + offset;
4201 irte_info->iommu = (void *)cfg;
4202
4203 return 0;
4204}
4205
Joerg Roedeld9761952012-06-26 16:00:08 +02004206static int setup_hpet_msi(unsigned int irq, unsigned int id)
4207{
4208 struct irq_2_iommu *irte_info;
4209 struct irq_cfg *cfg;
4210 int index, devid;
4211
4212 cfg = irq_get_chip_data(irq);
4213 if (!cfg)
4214 return -EINVAL;
4215
4216 irte_info = &cfg->irq_2_iommu;
4217 devid = get_hpet_devid(id);
4218 if (devid < 0)
4219 return devid;
4220
4221 index = alloc_irq_index(cfg, devid, 1);
4222 if (index < 0)
4223 return index;
4224
4225 irte_info->sub_handle = devid;
4226 irte_info->irte_index = index;
4227 irte_info->iommu = (void *)cfg;
4228
4229 return 0;
4230}
4231
Joerg Roedel6b474b82012-06-26 16:46:04 +02004232struct irq_remap_ops amd_iommu_irq_ops = {
4233 .supported = amd_iommu_supported,
4234 .prepare = amd_iommu_prepare,
4235 .enable = amd_iommu_enable,
4236 .disable = amd_iommu_disable,
4237 .reenable = amd_iommu_reenable,
4238 .enable_faulting = amd_iommu_enable_faulting,
4239 .setup_ioapic_entry = setup_ioapic_entry,
4240 .set_affinity = set_affinity,
4241 .free_irq = free_irq,
4242 .compose_msi_msg = compose_msi_msg,
4243 .msi_alloc_irq = msi_alloc_irq,
4244 .msi_setup_irq = msi_setup_irq,
4245 .setup_hpet_msi = setup_hpet_msi,
4246};
Joerg Roedel2b324502012-06-21 16:29:10 +02004247#endif