blob: bcf48156868b54ee7403c5966984fdd9de346736 [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 Roedelb6c02712008-06-26 21:27:53 +020048
49#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
50
Joerg Roedel815b33f2011-04-06 17:26:49 +020051#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020052
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020053/*
54 * This bitmap is used to advertise the page sizes our hardware support
55 * to the IOMMU core, which will then use this information to split
56 * physically contiguous memory regions it is mapping into page sizes
57 * that we support.
58 *
59 * Traditionally the IOMMU core just handed us the mappings directly,
60 * after making sure the size is an order of a 4KiB page and that the
61 * mapping has natural alignment.
62 *
63 * To retain this behavior, we currently advertise that we support
64 * all page sizes that are an order of 4KiB.
65 *
66 * If at some point we'd like to utilize the IOMMU core's new behavior,
67 * we could change this to advertise the real page sizes we support.
68 */
69#define AMD_IOMMU_PGSIZES (~0xFFFUL)
70
Joerg Roedelb6c02712008-06-26 21:27:53 +020071static DEFINE_RWLOCK(amd_iommu_devtable_lock);
72
Joerg Roedelbd60b732008-09-11 10:24:48 +020073/* A list of preallocated protection domains */
74static LIST_HEAD(iommu_pd_list);
75static DEFINE_SPINLOCK(iommu_pd_list_lock);
76
Joerg Roedel8fa5f802011-06-09 12:24:45 +020077/* List of all available dev_data structures */
78static LIST_HEAD(dev_data_list);
79static DEFINE_SPINLOCK(dev_data_list_lock);
80
Joerg Roedel6efed632012-06-14 15:52:58 +020081LIST_HEAD(ioapic_map);
82LIST_HEAD(hpet_map);
83
Joerg Roedel0feae532009-08-26 15:26:30 +020084/*
85 * Domain for untranslated devices - only allocated
86 * if iommu=pt passed on kernel cmd line.
87 */
88static struct protection_domain *pt_domain;
89
Joerg Roedel26961ef2008-12-03 17:00:17 +010090static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010091
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010092static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010093int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010094
Joerg Roedelac1534a2012-06-21 14:52:40 +020095static struct dma_map_ops amd_iommu_dma_ops;
96
Joerg Roedel431b2a22008-07-11 17:14:22 +020097/*
98 * general struct to manage commands send to an IOMMU
99 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200100struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200101 u32 data[4];
102};
103
Joerg Roedel05152a02012-06-15 16:53:51 +0200104struct kmem_cache *amd_iommu_irq_cache;
105
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200106static void update_domain(struct protection_domain *domain);
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100107static int __init alloc_passthrough_domain(void);
Chris Wrightc1eee672009-05-21 00:56:58 -0700108
Joerg Roedel15898bb2009-11-24 15:39:42 +0100109/****************************************************************************
110 *
111 * Helper functions
112 *
113 ****************************************************************************/
114
Joerg Roedelf62dda62011-06-09 12:55:35 +0200115static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200116{
117 struct iommu_dev_data *dev_data;
118 unsigned long flags;
119
120 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
121 if (!dev_data)
122 return NULL;
123
Joerg Roedelf62dda62011-06-09 12:55:35 +0200124 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200125 atomic_set(&dev_data->bind, 0);
126
127 spin_lock_irqsave(&dev_data_list_lock, flags);
128 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
129 spin_unlock_irqrestore(&dev_data_list_lock, flags);
130
131 return dev_data;
132}
133
134static void free_dev_data(struct iommu_dev_data *dev_data)
135{
136 unsigned long flags;
137
138 spin_lock_irqsave(&dev_data_list_lock, flags);
139 list_del(&dev_data->dev_data_list);
140 spin_unlock_irqrestore(&dev_data_list_lock, flags);
141
142 kfree(dev_data);
143}
144
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200145static struct iommu_dev_data *search_dev_data(u16 devid)
146{
147 struct iommu_dev_data *dev_data;
148 unsigned long flags;
149
150 spin_lock_irqsave(&dev_data_list_lock, flags);
151 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
152 if (dev_data->devid == devid)
153 goto out_unlock;
154 }
155
156 dev_data = NULL;
157
158out_unlock:
159 spin_unlock_irqrestore(&dev_data_list_lock, flags);
160
161 return dev_data;
162}
163
164static struct iommu_dev_data *find_dev_data(u16 devid)
165{
166 struct iommu_dev_data *dev_data;
167
168 dev_data = search_dev_data(devid);
169
170 if (dev_data == NULL)
171 dev_data = alloc_dev_data(devid);
172
173 return dev_data;
174}
175
Joerg Roedel15898bb2009-11-24 15:39:42 +0100176static inline u16 get_device_id(struct device *dev)
177{
178 struct pci_dev *pdev = to_pci_dev(dev);
179
180 return calc_devid(pdev->bus->number, pdev->devfn);
181}
182
Joerg Roedel657cbb62009-11-23 15:26:46 +0100183static struct iommu_dev_data *get_dev_data(struct device *dev)
184{
185 return dev->archdata.iommu;
186}
187
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100188static bool pci_iommuv2_capable(struct pci_dev *pdev)
189{
190 static const int caps[] = {
191 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100192 PCI_EXT_CAP_ID_PRI,
193 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100194 };
195 int i, pos;
196
197 for (i = 0; i < 3; ++i) {
198 pos = pci_find_ext_capability(pdev, caps[i]);
199 if (pos == 0)
200 return false;
201 }
202
203 return true;
204}
205
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100206static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
207{
208 struct iommu_dev_data *dev_data;
209
210 dev_data = get_dev_data(&pdev->dev);
211
212 return dev_data->errata & (1 << erratum) ? true : false;
213}
214
Joerg Roedel71c70982009-11-24 16:43:06 +0100215/*
216 * In this function the list of preallocated protection domains is traversed to
217 * find the domain for a specific device
218 */
219static struct dma_ops_domain *find_protection_domain(u16 devid)
220{
221 struct dma_ops_domain *entry, *ret = NULL;
222 unsigned long flags;
223 u16 alias = amd_iommu_alias_table[devid];
224
225 if (list_empty(&iommu_pd_list))
226 return NULL;
227
228 spin_lock_irqsave(&iommu_pd_list_lock, flags);
229
230 list_for_each_entry(entry, &iommu_pd_list, list) {
231 if (entry->target_dev == devid ||
232 entry->target_dev == alias) {
233 ret = entry;
234 break;
235 }
236 }
237
238 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
239
240 return ret;
241}
242
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100243/*
244 * This function checks if the driver got a valid device from the caller to
245 * avoid dereferencing invalid pointers.
246 */
247static bool check_device(struct device *dev)
248{
249 u16 devid;
250
251 if (!dev || !dev->dma_mask)
252 return false;
253
254 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100255 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100256 return false;
257
258 devid = get_device_id(dev);
259
260 /* Out of our scope? */
261 if (devid > amd_iommu_last_bdf)
262 return false;
263
264 if (amd_iommu_rlookup_table[devid] == NULL)
265 return false;
266
267 return true;
268}
269
Alex Williamson664b6002012-05-30 14:19:31 -0600270static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
271{
272 pci_dev_put(*from);
273 *from = to;
274}
275
276#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
277
Joerg Roedel657cbb62009-11-23 15:26:46 +0100278static int iommu_init_device(struct device *dev)
279{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600280 struct pci_dev *dma_pdev, *pdev = to_pci_dev(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100281 struct iommu_dev_data *dev_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600282 struct iommu_group *group;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200283 u16 alias;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600284 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100285
286 if (dev->archdata.iommu)
287 return 0;
288
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200289 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100290 if (!dev_data)
291 return -ENOMEM;
292
Joerg Roedelf62dda62011-06-09 12:55:35 +0200293 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200294 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200295 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100296
Joerg Roedel71f77582011-06-09 19:03:15 +0200297 alias_data = find_dev_data(alias);
298 if (alias_data == NULL) {
299 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
300 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200301 free_dev_data(dev_data);
302 return -ENOTSUPP;
303 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200304 dev_data->alias_data = alias_data;
Alex Williamson9dcd6132012-05-30 14:19:07 -0600305
306 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
307 } else
308 dma_pdev = pci_dev_get(pdev);
309
Alex Williamson31fe9432012-08-04 12:09:03 -0600310 /* Account for quirked devices */
Alex Williamson664b6002012-05-30 14:19:31 -0600311 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
312
Alex Williamson31fe9432012-08-04 12:09:03 -0600313 /*
314 * If it's a multifunction device that does not support our
315 * required ACS flags, add to the same group as function 0.
316 */
Alex Williamson664b6002012-05-30 14:19:31 -0600317 if (dma_pdev->multifunction &&
318 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
319 swap_pci_ref(&dma_pdev,
320 pci_get_slot(dma_pdev->bus,
321 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
322 0)));
323
Alex Williamson31fe9432012-08-04 12:09:03 -0600324 /*
325 * Devices on the root bus go through the iommu. If that's not us,
326 * find the next upstream device and test ACS up to the root bus.
327 * Finding the next device may require skipping virtual buses.
328 */
Alex Williamson664b6002012-05-30 14:19:31 -0600329 while (!pci_is_root_bus(dma_pdev->bus)) {
Alex Williamson31fe9432012-08-04 12:09:03 -0600330 struct pci_bus *bus = dma_pdev->bus;
331
332 while (!bus->self) {
333 if (!pci_is_root_bus(bus))
334 bus = bus->parent;
335 else
336 goto root_bus;
337 }
338
339 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
Alex Williamson664b6002012-05-30 14:19:31 -0600340 break;
341
Alex Williamson31fe9432012-08-04 12:09:03 -0600342 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
Joerg Roedel26018872011-06-06 16:50:14 +0200343 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100344
Alex Williamson31fe9432012-08-04 12:09:03 -0600345root_bus:
Alex Williamson9dcd6132012-05-30 14:19:07 -0600346 group = iommu_group_get(&dma_pdev->dev);
347 pci_dev_put(dma_pdev);
348 if (!group) {
349 group = iommu_group_alloc();
350 if (IS_ERR(group))
351 return PTR_ERR(group);
Joerg Roedel657cbb62009-11-23 15:26:46 +0100352 }
353
Alex Williamson9dcd6132012-05-30 14:19:07 -0600354 ret = iommu_group_add_device(group, dev);
355
356 iommu_group_put(group);
357
358 if (ret)
359 return ret;
360
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100361 if (pci_iommuv2_capable(pdev)) {
362 struct amd_iommu *iommu;
363
364 iommu = amd_iommu_rlookup_table[dev_data->devid];
365 dev_data->iommu_v2 = iommu->is_iommu_v2;
366 }
367
Joerg Roedel657cbb62009-11-23 15:26:46 +0100368 dev->archdata.iommu = dev_data;
369
Joerg Roedel657cbb62009-11-23 15:26:46 +0100370 return 0;
371}
372
Joerg Roedel26018872011-06-06 16:50:14 +0200373static void iommu_ignore_device(struct device *dev)
374{
375 u16 devid, alias;
376
377 devid = get_device_id(dev);
378 alias = amd_iommu_alias_table[devid];
379
380 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
381 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
382
383 amd_iommu_rlookup_table[devid] = NULL;
384 amd_iommu_rlookup_table[alias] = NULL;
385}
386
Joerg Roedel657cbb62009-11-23 15:26:46 +0100387static void iommu_uninit_device(struct device *dev)
388{
Alex Williamson9dcd6132012-05-30 14:19:07 -0600389 iommu_group_remove_device(dev);
390
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200391 /*
392 * Nothing to do here - we keep dev_data around for unplugged devices
393 * and reuse it when the device is re-plugged - not doing so would
394 * introduce a ton of races.
395 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100396}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100397
398void __init amd_iommu_uninit_devices(void)
399{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200400 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100401 struct pci_dev *pdev = NULL;
402
403 for_each_pci_dev(pdev) {
404
405 if (!check_device(&pdev->dev))
406 continue;
407
408 iommu_uninit_device(&pdev->dev);
409 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200410
411 /* Free all of our dev_data structures */
412 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
413 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100414}
415
416int __init amd_iommu_init_devices(void)
417{
418 struct pci_dev *pdev = NULL;
419 int ret = 0;
420
421 for_each_pci_dev(pdev) {
422
423 if (!check_device(&pdev->dev))
424 continue;
425
426 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200427 if (ret == -ENOTSUPP)
428 iommu_ignore_device(&pdev->dev);
429 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100430 goto out_free;
431 }
432
433 return 0;
434
435out_free:
436
437 amd_iommu_uninit_devices();
438
439 return ret;
440}
Joerg Roedel7f265082008-12-12 13:50:21 +0100441#ifdef CONFIG_AMD_IOMMU_STATS
442
443/*
444 * Initialization code for statistics collection
445 */
446
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100447DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100448DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100449DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100450DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100451DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100452DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100453DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100454DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100455DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100456DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100457DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100458DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100459DECLARE_STATS_COUNTER(complete_ppr);
460DECLARE_STATS_COUNTER(invalidate_iotlb);
461DECLARE_STATS_COUNTER(invalidate_iotlb_all);
462DECLARE_STATS_COUNTER(pri_requests);
463
Joerg Roedel7f265082008-12-12 13:50:21 +0100464static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100465static struct dentry *de_fflush;
466
467static void amd_iommu_stats_add(struct __iommu_counter *cnt)
468{
469 if (stats_dir == NULL)
470 return;
471
472 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
473 &cnt->value);
474}
475
476static void amd_iommu_stats_init(void)
477{
478 stats_dir = debugfs_create_dir("amd-iommu", NULL);
479 if (stats_dir == NULL)
480 return;
481
Joerg Roedel7f265082008-12-12 13:50:21 +0100482 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
Dan Carpenter3775d482012-06-27 12:09:18 +0300483 &amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100484
485 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100486 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100487 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100488 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100489 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100490 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100491 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100492 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100493 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100494 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100495 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100496 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel399be2f2011-12-01 16:53:47 +0100497 amd_iommu_stats_add(&complete_ppr);
498 amd_iommu_stats_add(&invalidate_iotlb);
499 amd_iommu_stats_add(&invalidate_iotlb_all);
500 amd_iommu_stats_add(&pri_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100501}
502
503#endif
504
Joerg Roedel431b2a22008-07-11 17:14:22 +0200505/****************************************************************************
506 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200507 * Interrupt handling functions
508 *
509 ****************************************************************************/
510
Joerg Roedele3e59872009-09-03 14:02:10 +0200511static void dump_dte_entry(u16 devid)
512{
513 int i;
514
Joerg Roedelee6c2862011-11-09 12:06:03 +0100515 for (i = 0; i < 4; ++i)
516 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200517 amd_iommu_dev_table[devid].data[i]);
518}
519
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200520static void dump_command(unsigned long phys_addr)
521{
522 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
523 int i;
524
525 for (i = 0; i < 4; ++i)
526 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
527}
528
Joerg Roedela345b232009-09-03 15:01:43 +0200529static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200530{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200531 int type, devid, domid, flags;
532 volatile u32 *event = __evt;
533 int count = 0;
534 u64 address;
535
536retry:
537 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
538 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
539 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
540 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
541 address = (u64)(((u64)event[3]) << 32) | event[2];
542
543 if (type == 0) {
544 /* Did we hit the erratum? */
545 if (++count == LOOP_TIMEOUT) {
546 pr_err("AMD-Vi: No event written to event log\n");
547 return;
548 }
549 udelay(1);
550 goto retry;
551 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200552
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200553 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200554
555 switch (type) {
556 case EVENT_TYPE_ILL_DEV:
557 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
558 "address=0x%016llx flags=0x%04x]\n",
559 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
560 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200561 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200562 break;
563 case EVENT_TYPE_IO_FAULT:
564 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
565 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
566 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
567 domid, address, flags);
568 break;
569 case EVENT_TYPE_DEV_TAB_ERR:
570 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
571 "address=0x%016llx flags=0x%04x]\n",
572 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
573 address, flags);
574 break;
575 case EVENT_TYPE_PAGE_TAB_ERR:
576 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
577 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
578 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
579 domid, address, flags);
580 break;
581 case EVENT_TYPE_ILL_CMD:
582 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200583 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200584 break;
585 case EVENT_TYPE_CMD_HARD_ERR:
586 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
587 "flags=0x%04x]\n", address, flags);
588 break;
589 case EVENT_TYPE_IOTLB_INV_TO:
590 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
591 "address=0x%016llx]\n",
592 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
593 address);
594 break;
595 case EVENT_TYPE_INV_DEV_REQ:
596 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
597 "address=0x%016llx flags=0x%04x]\n",
598 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
599 address, flags);
600 break;
601 default:
602 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
603 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200604
605 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200606}
607
608static void iommu_poll_events(struct amd_iommu *iommu)
609{
610 u32 head, tail;
611 unsigned long flags;
612
613 spin_lock_irqsave(&iommu->lock, flags);
614
615 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
616 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
617
618 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200619 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200620 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
621 }
622
623 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
624
625 spin_unlock_irqrestore(&iommu->lock, flags);
626}
627
Joerg Roedeleee53532012-06-01 15:20:23 +0200628static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100629{
630 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100631
Joerg Roedel399be2f2011-12-01 16:53:47 +0100632 INC_STATS_COUNTER(pri_requests);
633
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100634 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
635 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
636 return;
637 }
638
639 fault.address = raw[1];
640 fault.pasid = PPR_PASID(raw[0]);
641 fault.device_id = PPR_DEVID(raw[0]);
642 fault.tag = PPR_TAG(raw[0]);
643 fault.flags = PPR_FLAGS(raw[0]);
644
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100645 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
646}
647
648static void iommu_poll_ppr_log(struct amd_iommu *iommu)
649{
650 unsigned long flags;
651 u32 head, tail;
652
653 if (iommu->ppr_log == NULL)
654 return;
655
Joerg Roedeleee53532012-06-01 15:20:23 +0200656 /* enable ppr interrupts again */
657 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
658
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100659 spin_lock_irqsave(&iommu->lock, flags);
660
661 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
662 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
663
664 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200665 volatile u64 *raw;
666 u64 entry[2];
667 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100668
Joerg Roedeleee53532012-06-01 15:20:23 +0200669 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100670
Joerg Roedeleee53532012-06-01 15:20:23 +0200671 /*
672 * Hardware bug: Interrupt may arrive before the entry is
673 * written to memory. If this happens we need to wait for the
674 * entry to arrive.
675 */
676 for (i = 0; i < LOOP_TIMEOUT; ++i) {
677 if (PPR_REQ_TYPE(raw[0]) != 0)
678 break;
679 udelay(1);
680 }
681
682 /* Avoid memcpy function-call overhead */
683 entry[0] = raw[0];
684 entry[1] = raw[1];
685
686 /*
687 * To detect the hardware bug we need to clear the entry
688 * back to zero.
689 */
690 raw[0] = raw[1] = 0UL;
691
692 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100693 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
694 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200695
696 /*
697 * Release iommu->lock because ppr-handling might need to
698 * re-aquire it
699 */
700 spin_unlock_irqrestore(&iommu->lock, flags);
701
702 /* Handle PPR entry */
703 iommu_handle_ppr_entry(iommu, entry);
704
705 spin_lock_irqsave(&iommu->lock, flags);
706
707 /* Refresh ring-buffer information */
708 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100709 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
710 }
711
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100712 spin_unlock_irqrestore(&iommu->lock, flags);
713}
714
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200715irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200716{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200717 struct amd_iommu *iommu;
718
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100719 for_each_iommu(iommu) {
Joerg Roedel90008ee2008-09-09 16:41:05 +0200720 iommu_poll_events(iommu);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100721 iommu_poll_ppr_log(iommu);
722 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200723
724 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200725}
726
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200727irqreturn_t amd_iommu_int_handler(int irq, void *data)
728{
729 return IRQ_WAKE_THREAD;
730}
731
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200732/****************************************************************************
733 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200734 * IOMMU command queuing functions
735 *
736 ****************************************************************************/
737
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200738static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200739{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200740 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200741
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200742 while (*sem == 0 && i < LOOP_TIMEOUT) {
743 udelay(1);
744 i += 1;
745 }
746
747 if (i == LOOP_TIMEOUT) {
748 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
749 return -EIO;
750 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200751
752 return 0;
753}
754
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200755static void copy_cmd_to_buffer(struct amd_iommu *iommu,
756 struct iommu_cmd *cmd,
757 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200758{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200759 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200760
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200761 target = iommu->cmd_buf + tail;
762 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200763
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200764 /* Copy command to buffer */
765 memcpy(target, cmd, sizeof(*cmd));
766
767 /* Tell the IOMMU about it */
768 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
769}
770
Joerg Roedel815b33f2011-04-06 17:26:49 +0200771static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200772{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200773 WARN_ON(address & 0x7ULL);
774
Joerg Roedelded46732011-04-06 10:53:48 +0200775 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200776 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
777 cmd->data[1] = upper_32_bits(__pa(address));
778 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200779 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
780}
781
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200782static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
783{
784 memset(cmd, 0, sizeof(*cmd));
785 cmd->data[0] = devid;
786 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
787}
788
Joerg Roedel11b64022011-04-06 11:49:28 +0200789static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
790 size_t size, u16 domid, int pde)
791{
792 u64 pages;
793 int s;
794
795 pages = iommu_num_pages(address, size, PAGE_SIZE);
796 s = 0;
797
798 if (pages > 1) {
799 /*
800 * If we have to flush more than one page, flush all
801 * TLB entries for this domain
802 */
803 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
804 s = 1;
805 }
806
807 address &= PAGE_MASK;
808
809 memset(cmd, 0, sizeof(*cmd));
810 cmd->data[1] |= domid;
811 cmd->data[2] = lower_32_bits(address);
812 cmd->data[3] = upper_32_bits(address);
813 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
814 if (s) /* size bit - we flush more than one 4kb page */
815 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
816 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
817 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
818}
819
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200820static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
821 u64 address, size_t size)
822{
823 u64 pages;
824 int s;
825
826 pages = iommu_num_pages(address, size, PAGE_SIZE);
827 s = 0;
828
829 if (pages > 1) {
830 /*
831 * If we have to flush more than one page, flush all
832 * TLB entries for this domain
833 */
834 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
835 s = 1;
836 }
837
838 address &= PAGE_MASK;
839
840 memset(cmd, 0, sizeof(*cmd));
841 cmd->data[0] = devid;
842 cmd->data[0] |= (qdep & 0xff) << 24;
843 cmd->data[1] = devid;
844 cmd->data[2] = lower_32_bits(address);
845 cmd->data[3] = upper_32_bits(address);
846 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
847 if (s)
848 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
849}
850
Joerg Roedel22e266c2011-11-21 15:59:08 +0100851static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
852 u64 address, bool size)
853{
854 memset(cmd, 0, sizeof(*cmd));
855
856 address &= ~(0xfffULL);
857
858 cmd->data[0] = pasid & PASID_MASK;
859 cmd->data[1] = domid;
860 cmd->data[2] = lower_32_bits(address);
861 cmd->data[3] = upper_32_bits(address);
862 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
863 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
864 if (size)
865 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
866 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
867}
868
869static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
870 int qdep, u64 address, bool size)
871{
872 memset(cmd, 0, sizeof(*cmd));
873
874 address &= ~(0xfffULL);
875
876 cmd->data[0] = devid;
877 cmd->data[0] |= (pasid & 0xff) << 16;
878 cmd->data[0] |= (qdep & 0xff) << 24;
879 cmd->data[1] = devid;
880 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
881 cmd->data[2] = lower_32_bits(address);
882 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
883 cmd->data[3] = upper_32_bits(address);
884 if (size)
885 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
886 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
887}
888
Joerg Roedelc99afa22011-11-21 18:19:25 +0100889static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
890 int status, int tag, bool gn)
891{
892 memset(cmd, 0, sizeof(*cmd));
893
894 cmd->data[0] = devid;
895 if (gn) {
896 cmd->data[1] = pasid & PASID_MASK;
897 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
898 }
899 cmd->data[3] = tag & 0x1ff;
900 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
901
902 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
903}
904
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200905static void build_inv_all(struct iommu_cmd *cmd)
906{
907 memset(cmd, 0, sizeof(*cmd));
908 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200909}
910
Joerg Roedel7ef27982012-06-21 16:46:04 +0200911static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
912{
913 memset(cmd, 0, sizeof(*cmd));
914 cmd->data[0] = devid;
915 CMD_SET_TYPE(cmd, CMD_INV_IRT);
916}
917
Joerg Roedel431b2a22008-07-11 17:14:22 +0200918/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200919 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200920 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200921 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200922static int iommu_queue_command_sync(struct amd_iommu *iommu,
923 struct iommu_cmd *cmd,
924 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200925{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200926 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200927 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200928
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200929 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100930
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200931again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200932 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200933
934 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
935 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
936 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
937 left = (head - next_tail) % iommu->cmd_buf_size;
938
939 if (left <= 2) {
940 struct iommu_cmd sync_cmd;
941 volatile u64 sem = 0;
942 int ret;
943
944 build_completion_wait(&sync_cmd, (u64)&sem);
945 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
946
947 spin_unlock_irqrestore(&iommu->lock, flags);
948
949 if ((ret = wait_on_sem(&sem)) != 0)
950 return ret;
951
952 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200953 }
954
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200955 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200956
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200957 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200958 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200959
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200960 spin_unlock_irqrestore(&iommu->lock, flags);
961
Joerg Roedel815b33f2011-04-06 17:26:49 +0200962 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100963}
964
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200965static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
966{
967 return iommu_queue_command_sync(iommu, cmd, true);
968}
969
Joerg Roedel8d201962008-12-02 20:34:41 +0100970/*
971 * This function queues a completion wait command into the command
972 * buffer of an IOMMU
973 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100974static int iommu_completion_wait(struct amd_iommu *iommu)
975{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200976 struct iommu_cmd cmd;
977 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200978 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100979
980 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200981 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100982
Joerg Roedel815b33f2011-04-06 17:26:49 +0200983 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100984
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200985 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100986 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200987 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100988
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200989 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200990}
991
Joerg Roedeld8c13082011-04-06 18:51:26 +0200992static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200993{
994 struct iommu_cmd cmd;
995
Joerg Roedeld8c13082011-04-06 18:51:26 +0200996 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200997
Joerg Roedeld8c13082011-04-06 18:51:26 +0200998 return iommu_queue_command(iommu, &cmd);
999}
1000
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001001static void iommu_flush_dte_all(struct amd_iommu *iommu)
1002{
1003 u32 devid;
1004
1005 for (devid = 0; devid <= 0xffff; ++devid)
1006 iommu_flush_dte(iommu, devid);
1007
1008 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001009}
1010
1011/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001012 * This function uses heavy locking and may disable irqs for some time. But
1013 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001014 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001015static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001016{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001017 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001018
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001019 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1020 struct iommu_cmd cmd;
1021 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1022 dom_id, 1);
1023 iommu_queue_command(iommu, &cmd);
1024 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001025
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001026 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001027}
1028
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001029static void iommu_flush_all(struct amd_iommu *iommu)
1030{
1031 struct iommu_cmd cmd;
1032
1033 build_inv_all(&cmd);
1034
1035 iommu_queue_command(iommu, &cmd);
1036 iommu_completion_wait(iommu);
1037}
1038
Joerg Roedel7ef27982012-06-21 16:46:04 +02001039static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1040{
1041 struct iommu_cmd cmd;
1042
1043 build_inv_irt(&cmd, devid);
1044
1045 iommu_queue_command(iommu, &cmd);
1046}
1047
1048static void iommu_flush_irt_all(struct amd_iommu *iommu)
1049{
1050 u32 devid;
1051
1052 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1053 iommu_flush_irt(iommu, devid);
1054
1055 iommu_completion_wait(iommu);
1056}
1057
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001058void iommu_flush_all_caches(struct amd_iommu *iommu)
1059{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001060 if (iommu_feature(iommu, FEATURE_IA)) {
1061 iommu_flush_all(iommu);
1062 } else {
1063 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001064 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001065 iommu_flush_tlb_all(iommu);
1066 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001067}
1068
Joerg Roedel431b2a22008-07-11 17:14:22 +02001069/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001070 * Command send function for flushing on-device TLB
1071 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001072static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1073 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001074{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001075 struct amd_iommu *iommu;
1076 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001077 int qdep;
1078
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001079 qdep = dev_data->ats.qdep;
1080 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001081
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001082 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001083
1084 return iommu_queue_command(iommu, &cmd);
1085}
1086
1087/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001088 * Command send function for invalidating a device table entry
1089 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001090static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001091{
1092 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001093 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001094
Joerg Roedel6c542042011-06-09 17:07:31 +02001095 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +01001096
Joerg Roedelf62dda62011-06-09 12:55:35 +02001097 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001098 if (ret)
1099 return ret;
1100
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001101 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001102 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001103
1104 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001105}
1106
Joerg Roedel431b2a22008-07-11 17:14:22 +02001107/*
1108 * TLB invalidation function which is called from the mapping functions.
1109 * It invalidates a single PTE if the range to flush is within a single
1110 * page. Otherwise it flushes the whole TLB of the IOMMU.
1111 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001112static void __domain_flush_pages(struct protection_domain *domain,
1113 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001114{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001115 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001116 struct iommu_cmd cmd;
1117 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001118
Joerg Roedel11b64022011-04-06 11:49:28 +02001119 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001120
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001121 for (i = 0; i < amd_iommus_present; ++i) {
1122 if (!domain->dev_iommu[i])
1123 continue;
1124
1125 /*
1126 * Devices of this domain are behind this IOMMU
1127 * We need a TLB flush
1128 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001129 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001130 }
1131
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001132 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001133
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001134 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001135 continue;
1136
Joerg Roedel6c542042011-06-09 17:07:31 +02001137 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001138 }
1139
Joerg Roedel11b64022011-04-06 11:49:28 +02001140 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001141}
1142
Joerg Roedel17b124b2011-04-06 18:01:35 +02001143static void domain_flush_pages(struct protection_domain *domain,
1144 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001145{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001146 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001147}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001148
Joerg Roedel1c655772008-09-04 18:40:05 +02001149/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001150static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001151{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001152 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001153}
1154
Chris Wright42a49f92009-06-15 15:42:00 +02001155/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001156static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001157{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001158 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1159}
1160
1161static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001162{
1163 int i;
1164
1165 for (i = 0; i < amd_iommus_present; ++i) {
1166 if (!domain->dev_iommu[i])
1167 continue;
1168
1169 /*
1170 * Devices of this domain are behind this IOMMU
1171 * We need to wait for completion of all commands.
1172 */
1173 iommu_completion_wait(amd_iommus[i]);
1174 }
1175}
1176
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001177
Joerg Roedel43f49602008-12-02 21:01:12 +01001178/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001179 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001180 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001181static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001182{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001183 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001184
1185 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001186 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001187}
1188
Joerg Roedel431b2a22008-07-11 17:14:22 +02001189/****************************************************************************
1190 *
1191 * The functions below are used the create the page table mappings for
1192 * unity mapped regions.
1193 *
1194 ****************************************************************************/
1195
1196/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001197 * This function is used to add another level to an IO page table. Adding
1198 * another level increases the size of the address space by 9 bits to a size up
1199 * to 64 bits.
1200 */
1201static bool increase_address_space(struct protection_domain *domain,
1202 gfp_t gfp)
1203{
1204 u64 *pte;
1205
1206 if (domain->mode == PAGE_MODE_6_LEVEL)
1207 /* address space already 64 bit large */
1208 return false;
1209
1210 pte = (void *)get_zeroed_page(gfp);
1211 if (!pte)
1212 return false;
1213
1214 *pte = PM_LEVEL_PDE(domain->mode,
1215 virt_to_phys(domain->pt_root));
1216 domain->pt_root = pte;
1217 domain->mode += 1;
1218 domain->updated = true;
1219
1220 return true;
1221}
1222
1223static u64 *alloc_pte(struct protection_domain *domain,
1224 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001225 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001226 u64 **pte_page,
1227 gfp_t gfp)
1228{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001229 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001230 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001231
1232 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001233
1234 while (address > PM_LEVEL_SIZE(domain->mode))
1235 increase_address_space(domain, gfp);
1236
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001237 level = domain->mode - 1;
1238 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1239 address = PAGE_SIZE_ALIGN(address, page_size);
1240 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001241
1242 while (level > end_lvl) {
1243 if (!IOMMU_PTE_PRESENT(*pte)) {
1244 page = (u64 *)get_zeroed_page(gfp);
1245 if (!page)
1246 return NULL;
1247 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1248 }
1249
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001250 /* No level skipping support yet */
1251 if (PM_PTE_LEVEL(*pte) != level)
1252 return NULL;
1253
Joerg Roedel308973d2009-11-24 17:43:32 +01001254 level -= 1;
1255
1256 pte = IOMMU_PTE_PAGE(*pte);
1257
1258 if (pte_page && level == end_lvl)
1259 *pte_page = pte;
1260
1261 pte = &pte[PM_LEVEL_INDEX(level, address)];
1262 }
1263
1264 return pte;
1265}
1266
1267/*
1268 * This function checks if there is a PTE for a given dma address. If
1269 * there is one, it returns the pointer to it.
1270 */
Joerg Roedel24cd7722010-01-19 17:27:39 +01001271static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +01001272{
1273 int level;
1274 u64 *pte;
1275
Joerg Roedel24cd7722010-01-19 17:27:39 +01001276 if (address > PM_LEVEL_SIZE(domain->mode))
1277 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001278
Joerg Roedel24cd7722010-01-19 17:27:39 +01001279 level = domain->mode - 1;
1280 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1281
1282 while (level > 0) {
1283
1284 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001285 if (!IOMMU_PTE_PRESENT(*pte))
1286 return NULL;
1287
Joerg Roedel24cd7722010-01-19 17:27:39 +01001288 /* Large PTE */
1289 if (PM_PTE_LEVEL(*pte) == 0x07) {
1290 unsigned long pte_mask, __pte;
1291
1292 /*
1293 * If we have a series of large PTEs, make
1294 * sure to return a pointer to the first one.
1295 */
1296 pte_mask = PTE_PAGE_SIZE(*pte);
1297 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1298 __pte = ((unsigned long)pte) & pte_mask;
1299
1300 return (u64 *)__pte;
1301 }
1302
1303 /* No level skipping support yet */
1304 if (PM_PTE_LEVEL(*pte) != level)
1305 return NULL;
1306
Joerg Roedel308973d2009-11-24 17:43:32 +01001307 level -= 1;
1308
Joerg Roedel24cd7722010-01-19 17:27:39 +01001309 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +01001310 pte = IOMMU_PTE_PAGE(*pte);
1311 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +01001312 }
1313
1314 return pte;
1315}
1316
1317/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001318 * Generic mapping functions. It maps a physical address into a DMA
1319 * address space. It allocates the page table pages if necessary.
1320 * In the future it can be extended to a generic mapping function
1321 * supporting all features of AMD IOMMU page tables like level skipping
1322 * and full 64 bit address spaces.
1323 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001324static int iommu_map_page(struct protection_domain *dom,
1325 unsigned long bus_addr,
1326 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001327 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001328 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001329{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001330 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001331 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001332
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001333 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001334 return -EINVAL;
1335
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001336 bus_addr = PAGE_ALIGN(bus_addr);
1337 phys_addr = PAGE_ALIGN(phys_addr);
1338 count = PAGE_SIZE_PTE_COUNT(page_size);
1339 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001340
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001341 for (i = 0; i < count; ++i)
1342 if (IOMMU_PTE_PRESENT(pte[i]))
1343 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001344
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001345 if (page_size > PAGE_SIZE) {
1346 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1347 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1348 } else
1349 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1350
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001351 if (prot & IOMMU_PROT_IR)
1352 __pte |= IOMMU_PTE_IR;
1353 if (prot & IOMMU_PROT_IW)
1354 __pte |= IOMMU_PTE_IW;
1355
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001356 for (i = 0; i < count; ++i)
1357 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001358
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001359 update_domain(dom);
1360
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001361 return 0;
1362}
1363
Joerg Roedel24cd7722010-01-19 17:27:39 +01001364static unsigned long iommu_unmap_page(struct protection_domain *dom,
1365 unsigned long bus_addr,
1366 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001367{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001368 unsigned long long unmap_size, unmapped;
1369 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001370
Joerg Roedel24cd7722010-01-19 17:27:39 +01001371 BUG_ON(!is_power_of_2(page_size));
1372
1373 unmapped = 0;
1374
1375 while (unmapped < page_size) {
1376
1377 pte = fetch_pte(dom, bus_addr);
1378
1379 if (!pte) {
1380 /*
1381 * No PTE for this address
1382 * move forward in 4kb steps
1383 */
1384 unmap_size = PAGE_SIZE;
1385 } else if (PM_PTE_LEVEL(*pte) == 0) {
1386 /* 4kb PTE found for this address */
1387 unmap_size = PAGE_SIZE;
1388 *pte = 0ULL;
1389 } else {
1390 int count, i;
1391
1392 /* Large PTE found which maps this address */
1393 unmap_size = PTE_PAGE_SIZE(*pte);
1394 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1395 for (i = 0; i < count; i++)
1396 pte[i] = 0ULL;
1397 }
1398
1399 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1400 unmapped += unmap_size;
1401 }
1402
1403 BUG_ON(!is_power_of_2(unmapped));
1404
1405 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001406}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001407
Joerg Roedel431b2a22008-07-11 17:14:22 +02001408/*
1409 * This function checks if a specific unity mapping entry is needed for
1410 * this specific IOMMU.
1411 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001412static int iommu_for_unity_map(struct amd_iommu *iommu,
1413 struct unity_map_entry *entry)
1414{
1415 u16 bdf, i;
1416
1417 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1418 bdf = amd_iommu_alias_table[i];
1419 if (amd_iommu_rlookup_table[bdf] == iommu)
1420 return 1;
1421 }
1422
1423 return 0;
1424}
1425
Joerg Roedel431b2a22008-07-11 17:14:22 +02001426/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001427 * This function actually applies the mapping to the page table of the
1428 * dma_ops domain.
1429 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001430static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1431 struct unity_map_entry *e)
1432{
1433 u64 addr;
1434 int ret;
1435
1436 for (addr = e->address_start; addr < e->address_end;
1437 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001438 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001439 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001440 if (ret)
1441 return ret;
1442 /*
1443 * if unity mapping is in aperture range mark the page
1444 * as allocated in the aperture
1445 */
1446 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001447 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001448 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001449 }
1450
1451 return 0;
1452}
1453
Joerg Roedel431b2a22008-07-11 17:14:22 +02001454/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001455 * Init the unity mappings for a specific IOMMU in the system
1456 *
1457 * Basically iterates over all unity mapping entries and applies them to
1458 * the default domain DMA of that IOMMU if necessary.
1459 */
1460static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1461{
1462 struct unity_map_entry *entry;
1463 int ret;
1464
1465 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1466 if (!iommu_for_unity_map(iommu, entry))
1467 continue;
1468 ret = dma_ops_unity_map(iommu->default_dom, entry);
1469 if (ret)
1470 return ret;
1471 }
1472
1473 return 0;
1474}
1475
1476/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001477 * Inits the unity mappings required for a specific device
1478 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001479static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1480 u16 devid)
1481{
1482 struct unity_map_entry *e;
1483 int ret;
1484
1485 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1486 if (!(devid >= e->devid_start && devid <= e->devid_end))
1487 continue;
1488 ret = dma_ops_unity_map(dma_dom, e);
1489 if (ret)
1490 return ret;
1491 }
1492
1493 return 0;
1494}
1495
Joerg Roedel431b2a22008-07-11 17:14:22 +02001496/****************************************************************************
1497 *
1498 * The next functions belong to the address allocator for the dma_ops
1499 * interface functions. They work like the allocators in the other IOMMU
1500 * drivers. Its basically a bitmap which marks the allocated pages in
1501 * the aperture. Maybe it could be enhanced in the future to a more
1502 * efficient allocator.
1503 *
1504 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001505
Joerg Roedel431b2a22008-07-11 17:14:22 +02001506/*
Joerg Roedel384de722009-05-15 12:30:05 +02001507 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001508 *
1509 * called with domain->lock held
1510 */
Joerg Roedel384de722009-05-15 12:30:05 +02001511
Joerg Roedel9cabe892009-05-18 16:38:55 +02001512/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001513 * Used to reserve address ranges in the aperture (e.g. for exclusion
1514 * ranges.
1515 */
1516static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1517 unsigned long start_page,
1518 unsigned int pages)
1519{
1520 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1521
1522 if (start_page + pages > last_page)
1523 pages = last_page - start_page;
1524
1525 for (i = start_page; i < start_page + pages; ++i) {
1526 int index = i / APERTURE_RANGE_PAGES;
1527 int page = i % APERTURE_RANGE_PAGES;
1528 __set_bit(page, dom->aperture[index]->bitmap);
1529 }
1530}
1531
1532/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001533 * This function is used to add a new aperture range to an existing
1534 * aperture in case of dma_ops domain allocation or address allocation
1535 * failure.
1536 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001537static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001538 bool populate, gfp_t gfp)
1539{
1540 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001541 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001542 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001543
Joerg Roedelf5e97052009-05-22 12:31:53 +02001544#ifdef CONFIG_IOMMU_STRESS
1545 populate = false;
1546#endif
1547
Joerg Roedel9cabe892009-05-18 16:38:55 +02001548 if (index >= APERTURE_MAX_RANGES)
1549 return -ENOMEM;
1550
1551 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1552 if (!dma_dom->aperture[index])
1553 return -ENOMEM;
1554
1555 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1556 if (!dma_dom->aperture[index]->bitmap)
1557 goto out_free;
1558
1559 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1560
1561 if (populate) {
1562 unsigned long address = dma_dom->aperture_size;
1563 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1564 u64 *pte, *pte_page;
1565
1566 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001567 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001568 &pte_page, gfp);
1569 if (!pte)
1570 goto out_free;
1571
1572 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1573
1574 address += APERTURE_RANGE_SIZE / 64;
1575 }
1576 }
1577
Joerg Roedel17f5b562011-07-06 17:14:44 +02001578 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001579 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1580
Joerg Roedel17f5b562011-07-06 17:14:44 +02001581 /* Reserve address range used for MSI messages */
1582 if (old_size < MSI_ADDR_BASE_LO &&
1583 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1584 unsigned long spage;
1585 int pages;
1586
1587 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1588 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1589
1590 dma_ops_reserve_addresses(dma_dom, spage, pages);
1591 }
1592
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001593 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001594 for_each_iommu(iommu) {
1595 if (iommu->exclusion_start &&
1596 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1597 && iommu->exclusion_start < dma_dom->aperture_size) {
1598 unsigned long startpage;
1599 int pages = iommu_num_pages(iommu->exclusion_start,
1600 iommu->exclusion_length,
1601 PAGE_SIZE);
1602 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1603 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1604 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001605 }
1606
1607 /*
1608 * Check for areas already mapped as present in the new aperture
1609 * range and mark those pages as reserved in the allocator. Such
1610 * mappings may already exist as a result of requested unity
1611 * mappings for devices.
1612 */
1613 for (i = dma_dom->aperture[index]->offset;
1614 i < dma_dom->aperture_size;
1615 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001616 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001617 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1618 continue;
1619
Joerg Roedelfcd08612011-10-11 17:41:32 +02001620 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001621 }
1622
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001623 update_domain(&dma_dom->domain);
1624
Joerg Roedel9cabe892009-05-18 16:38:55 +02001625 return 0;
1626
1627out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001628 update_domain(&dma_dom->domain);
1629
Joerg Roedel9cabe892009-05-18 16:38:55 +02001630 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1631
1632 kfree(dma_dom->aperture[index]);
1633 dma_dom->aperture[index] = NULL;
1634
1635 return -ENOMEM;
1636}
1637
Joerg Roedel384de722009-05-15 12:30:05 +02001638static unsigned long dma_ops_area_alloc(struct device *dev,
1639 struct dma_ops_domain *dom,
1640 unsigned int pages,
1641 unsigned long align_mask,
1642 u64 dma_mask,
1643 unsigned long start)
1644{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001645 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001646 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1647 int i = start >> APERTURE_RANGE_SHIFT;
1648 unsigned long boundary_size;
1649 unsigned long address = -1;
1650 unsigned long limit;
1651
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001652 next_bit >>= PAGE_SHIFT;
1653
Joerg Roedel384de722009-05-15 12:30:05 +02001654 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1655 PAGE_SIZE) >> PAGE_SHIFT;
1656
1657 for (;i < max_index; ++i) {
1658 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1659
1660 if (dom->aperture[i]->offset >= dma_mask)
1661 break;
1662
1663 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1664 dma_mask >> PAGE_SHIFT);
1665
1666 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1667 limit, next_bit, pages, 0,
1668 boundary_size, align_mask);
1669 if (address != -1) {
1670 address = dom->aperture[i]->offset +
1671 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001672 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001673 break;
1674 }
1675
1676 next_bit = 0;
1677 }
1678
1679 return address;
1680}
1681
Joerg Roedeld3086442008-06-26 21:27:57 +02001682static unsigned long dma_ops_alloc_addresses(struct device *dev,
1683 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001684 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001685 unsigned long align_mask,
1686 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001687{
Joerg Roedeld3086442008-06-26 21:27:57 +02001688 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001689
Joerg Roedelfe16f082009-05-22 12:27:53 +02001690#ifdef CONFIG_IOMMU_STRESS
1691 dom->next_address = 0;
1692 dom->need_flush = true;
1693#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001694
Joerg Roedel384de722009-05-15 12:30:05 +02001695 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001696 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001697
Joerg Roedel1c655772008-09-04 18:40:05 +02001698 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001699 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001700 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1701 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001702 dom->need_flush = true;
1703 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001704
Joerg Roedel384de722009-05-15 12:30:05 +02001705 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001706 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001707
1708 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1709
1710 return address;
1711}
1712
Joerg Roedel431b2a22008-07-11 17:14:22 +02001713/*
1714 * The address free function.
1715 *
1716 * called with domain->lock held
1717 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001718static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1719 unsigned long address,
1720 unsigned int pages)
1721{
Joerg Roedel384de722009-05-15 12:30:05 +02001722 unsigned i = address >> APERTURE_RANGE_SHIFT;
1723 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001724
Joerg Roedel384de722009-05-15 12:30:05 +02001725 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1726
Joerg Roedel47bccd62009-05-22 12:40:54 +02001727#ifdef CONFIG_IOMMU_STRESS
1728 if (i < 4)
1729 return;
1730#endif
1731
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001732 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001733 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001734
1735 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001736
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001737 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001738
Joerg Roedeld3086442008-06-26 21:27:57 +02001739}
1740
Joerg Roedel431b2a22008-07-11 17:14:22 +02001741/****************************************************************************
1742 *
1743 * The next functions belong to the domain allocation. A domain is
1744 * allocated for every IOMMU as the default domain. If device isolation
1745 * is enabled, every device get its own domain. The most important thing
1746 * about domains is the page table mapping the DMA address space they
1747 * contain.
1748 *
1749 ****************************************************************************/
1750
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001751/*
1752 * This function adds a protection domain to the global protection domain list
1753 */
1754static void add_domain_to_list(struct protection_domain *domain)
1755{
1756 unsigned long flags;
1757
1758 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1759 list_add(&domain->list, &amd_iommu_pd_list);
1760 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1761}
1762
1763/*
1764 * This function removes a protection domain to the global
1765 * protection domain list
1766 */
1767static void del_domain_from_list(struct protection_domain *domain)
1768{
1769 unsigned long flags;
1770
1771 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1772 list_del(&domain->list);
1773 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1774}
1775
Joerg Roedelec487d12008-06-26 21:27:58 +02001776static u16 domain_id_alloc(void)
1777{
1778 unsigned long flags;
1779 int id;
1780
1781 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1782 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1783 BUG_ON(id == 0);
1784 if (id > 0 && id < MAX_DOMAIN_ID)
1785 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1786 else
1787 id = 0;
1788 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1789
1790 return id;
1791}
1792
Joerg Roedela2acfb72008-12-02 18:28:53 +01001793static void domain_id_free(int id)
1794{
1795 unsigned long flags;
1796
1797 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1798 if (id > 0 && id < MAX_DOMAIN_ID)
1799 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1800 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1801}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001802
Joerg Roedel86db2e52008-12-02 18:20:21 +01001803static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001804{
1805 int i, j;
1806 u64 *p1, *p2, *p3;
1807
Joerg Roedel86db2e52008-12-02 18:20:21 +01001808 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001809
1810 if (!p1)
1811 return;
1812
1813 for (i = 0; i < 512; ++i) {
1814 if (!IOMMU_PTE_PRESENT(p1[i]))
1815 continue;
1816
1817 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001818 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001819 if (!IOMMU_PTE_PRESENT(p2[j]))
1820 continue;
1821 p3 = IOMMU_PTE_PAGE(p2[j]);
1822 free_page((unsigned long)p3);
1823 }
1824
1825 free_page((unsigned long)p2);
1826 }
1827
1828 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001829
1830 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001831}
1832
Joerg Roedelb16137b2011-11-21 16:50:23 +01001833static void free_gcr3_tbl_level1(u64 *tbl)
1834{
1835 u64 *ptr;
1836 int i;
1837
1838 for (i = 0; i < 512; ++i) {
1839 if (!(tbl[i] & GCR3_VALID))
1840 continue;
1841
1842 ptr = __va(tbl[i] & PAGE_MASK);
1843
1844 free_page((unsigned long)ptr);
1845 }
1846}
1847
1848static void free_gcr3_tbl_level2(u64 *tbl)
1849{
1850 u64 *ptr;
1851 int i;
1852
1853 for (i = 0; i < 512; ++i) {
1854 if (!(tbl[i] & GCR3_VALID))
1855 continue;
1856
1857 ptr = __va(tbl[i] & PAGE_MASK);
1858
1859 free_gcr3_tbl_level1(ptr);
1860 }
1861}
1862
Joerg Roedel52815b72011-11-17 17:24:28 +01001863static void free_gcr3_table(struct protection_domain *domain)
1864{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001865 if (domain->glx == 2)
1866 free_gcr3_tbl_level2(domain->gcr3_tbl);
1867 else if (domain->glx == 1)
1868 free_gcr3_tbl_level1(domain->gcr3_tbl);
1869 else if (domain->glx != 0)
1870 BUG();
1871
Joerg Roedel52815b72011-11-17 17:24:28 +01001872 free_page((unsigned long)domain->gcr3_tbl);
1873}
1874
Joerg Roedel431b2a22008-07-11 17:14:22 +02001875/*
1876 * Free a domain, only used if something went wrong in the
1877 * allocation path and we need to free an already allocated page table
1878 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001879static void dma_ops_domain_free(struct dma_ops_domain *dom)
1880{
Joerg Roedel384de722009-05-15 12:30:05 +02001881 int i;
1882
Joerg Roedelec487d12008-06-26 21:27:58 +02001883 if (!dom)
1884 return;
1885
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001886 del_domain_from_list(&dom->domain);
1887
Joerg Roedel86db2e52008-12-02 18:20:21 +01001888 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001889
Joerg Roedel384de722009-05-15 12:30:05 +02001890 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1891 if (!dom->aperture[i])
1892 continue;
1893 free_page((unsigned long)dom->aperture[i]->bitmap);
1894 kfree(dom->aperture[i]);
1895 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001896
1897 kfree(dom);
1898}
1899
Joerg Roedel431b2a22008-07-11 17:14:22 +02001900/*
1901 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001902 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001903 * structures required for the dma_ops interface
1904 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001905static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001906{
1907 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001908
1909 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1910 if (!dma_dom)
1911 return NULL;
1912
1913 spin_lock_init(&dma_dom->domain.lock);
1914
1915 dma_dom->domain.id = domain_id_alloc();
1916 if (dma_dom->domain.id == 0)
1917 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001918 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001919 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001920 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001921 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001922 dma_dom->domain.priv = dma_dom;
1923 if (!dma_dom->domain.pt_root)
1924 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001925
Joerg Roedel1c655772008-09-04 18:40:05 +02001926 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001927 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001928
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001929 add_domain_to_list(&dma_dom->domain);
1930
Joerg Roedel576175c2009-11-23 19:08:46 +01001931 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001932 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001933
Joerg Roedel431b2a22008-07-11 17:14:22 +02001934 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001935 * mark the first page as allocated so we never return 0 as
1936 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001937 */
Joerg Roedel384de722009-05-15 12:30:05 +02001938 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001939 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001940
Joerg Roedelec487d12008-06-26 21:27:58 +02001941
1942 return dma_dom;
1943
1944free_dma_dom:
1945 dma_ops_domain_free(dma_dom);
1946
1947 return NULL;
1948}
1949
Joerg Roedel431b2a22008-07-11 17:14:22 +02001950/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001951 * little helper function to check whether a given protection domain is a
1952 * dma_ops domain
1953 */
1954static bool dma_ops_domain(struct protection_domain *domain)
1955{
1956 return domain->flags & PD_DMA_OPS_MASK;
1957}
1958
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001959static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001960{
Joerg Roedel132bd682011-11-17 14:18:46 +01001961 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001962 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001963
Joerg Roedel132bd682011-11-17 14:18:46 +01001964 if (domain->mode != PAGE_MODE_NONE)
1965 pte_root = virt_to_phys(domain->pt_root);
1966
Joerg Roedel38ddf412008-09-11 10:38:32 +02001967 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1968 << DEV_ENTRY_MODE_SHIFT;
1969 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001970
Joerg Roedelee6c2862011-11-09 12:06:03 +01001971 flags = amd_iommu_dev_table[devid].data[1];
1972
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001973 if (ats)
1974 flags |= DTE_FLAG_IOTLB;
1975
Joerg Roedel52815b72011-11-17 17:24:28 +01001976 if (domain->flags & PD_IOMMUV2_MASK) {
1977 u64 gcr3 = __pa(domain->gcr3_tbl);
1978 u64 glx = domain->glx;
1979 u64 tmp;
1980
1981 pte_root |= DTE_FLAG_GV;
1982 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1983
1984 /* First mask out possible old values for GCR3 table */
1985 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1986 flags &= ~tmp;
1987
1988 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1989 flags &= ~tmp;
1990
1991 /* Encode GCR3 table into DTE */
1992 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1993 pte_root |= tmp;
1994
1995 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1996 flags |= tmp;
1997
1998 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1999 flags |= tmp;
2000 }
2001
Joerg Roedelee6c2862011-11-09 12:06:03 +01002002 flags &= ~(0xffffUL);
2003 flags |= domain->id;
2004
2005 amd_iommu_dev_table[devid].data[1] = flags;
2006 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002007}
2008
Joerg Roedel15898bb2009-11-24 15:39:42 +01002009static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01002010{
Joerg Roedel355bf552008-12-08 12:02:41 +01002011 /* remove entry from the device table seen by the hardware */
2012 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2013 amd_iommu_dev_table[devid].data[1] = 0;
Joerg Roedel355bf552008-12-08 12:02:41 +01002014
Joerg Roedelc5cca142009-10-09 18:31:20 +02002015 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002016}
2017
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002018static void do_attach(struct iommu_dev_data *dev_data,
2019 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002020{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002021 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002022 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002023
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002024 iommu = amd_iommu_rlookup_table[dev_data->devid];
2025 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002026
2027 /* Update data structures */
2028 dev_data->domain = domain;
2029 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002030 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002031
2032 /* Do reference counting */
2033 domain->dev_iommu[iommu->index] += 1;
2034 domain->dev_cnt += 1;
2035
2036 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002037 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002038}
2039
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002040static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002041{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002042 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002043
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002044 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02002045
Joerg Roedelc4596112009-11-20 14:57:32 +01002046 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002047 dev_data->domain->dev_iommu[iommu->index] -= 1;
2048 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01002049
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002050 /* Update data structures */
2051 dev_data->domain = NULL;
2052 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02002053 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002054
2055 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02002056 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002057}
2058
2059/*
2060 * If a device is not yet associated with a domain, this function does
2061 * assigns it visible for the hardware
2062 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002063static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01002064 struct protection_domain *domain)
2065{
Julia Lawall84fe6c12010-05-27 12:31:51 +02002066 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002067
Joerg Roedel15898bb2009-11-24 15:39:42 +01002068 /* lock domain */
2069 spin_lock(&domain->lock);
2070
Joerg Roedel71f77582011-06-09 19:03:15 +02002071 if (dev_data->alias_data != NULL) {
2072 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002073
Joerg Roedel2b02b092011-06-09 17:48:39 +02002074 /* Some sanity checks */
2075 ret = -EBUSY;
2076 if (alias_data->domain != NULL &&
2077 alias_data->domain != domain)
2078 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002079
Joerg Roedel2b02b092011-06-09 17:48:39 +02002080 if (dev_data->domain != NULL &&
2081 dev_data->domain != domain)
2082 goto out_unlock;
2083
2084 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002085 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002086 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01002087
2088 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002089 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01002090
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002091 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002092 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002093
Joerg Roedel24100052009-11-25 15:59:57 +01002094 atomic_inc(&dev_data->bind);
2095
Julia Lawall84fe6c12010-05-27 12:31:51 +02002096 ret = 0;
2097
2098out_unlock:
2099
Joerg Roedel355bf552008-12-08 12:02:41 +01002100 /* ready */
2101 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02002102
Julia Lawall84fe6c12010-05-27 12:31:51 +02002103 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002104}
2105
Joerg Roedel52815b72011-11-17 17:24:28 +01002106
2107static void pdev_iommuv2_disable(struct pci_dev *pdev)
2108{
2109 pci_disable_ats(pdev);
2110 pci_disable_pri(pdev);
2111 pci_disable_pasid(pdev);
2112}
2113
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002114/* FIXME: Change generic reset-function to do the same */
2115static int pri_reset_while_enabled(struct pci_dev *pdev)
2116{
2117 u16 control;
2118 int pos;
2119
Joerg Roedel46277b72011-12-07 14:34:02 +01002120 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002121 if (!pos)
2122 return -EINVAL;
2123
Joerg Roedel46277b72011-12-07 14:34:02 +01002124 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2125 control |= PCI_PRI_CTRL_RESET;
2126 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002127
2128 return 0;
2129}
2130
Joerg Roedel52815b72011-11-17 17:24:28 +01002131static int pdev_iommuv2_enable(struct pci_dev *pdev)
2132{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002133 bool reset_enable;
2134 int reqs, ret;
2135
2136 /* FIXME: Hardcode number of outstanding requests for now */
2137 reqs = 32;
2138 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2139 reqs = 1;
2140 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002141
2142 /* Only allow access to user-accessible pages */
2143 ret = pci_enable_pasid(pdev, 0);
2144 if (ret)
2145 goto out_err;
2146
2147 /* First reset the PRI state of the device */
2148 ret = pci_reset_pri(pdev);
2149 if (ret)
2150 goto out_err;
2151
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002152 /* Enable PRI */
2153 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002154 if (ret)
2155 goto out_err;
2156
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002157 if (reset_enable) {
2158 ret = pri_reset_while_enabled(pdev);
2159 if (ret)
2160 goto out_err;
2161 }
2162
Joerg Roedel52815b72011-11-17 17:24:28 +01002163 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2164 if (ret)
2165 goto out_err;
2166
2167 return 0;
2168
2169out_err:
2170 pci_disable_pri(pdev);
2171 pci_disable_pasid(pdev);
2172
2173 return ret;
2174}
2175
Joerg Roedelc99afa22011-11-21 18:19:25 +01002176/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002177#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002178
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002179static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002180{
Joerg Roedela3b93122012-04-12 12:49:26 +02002181 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002182 int pos;
2183
Joerg Roedel46277b72011-12-07 14:34:02 +01002184 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002185 if (!pos)
2186 return false;
2187
Joerg Roedela3b93122012-04-12 12:49:26 +02002188 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002189
Joerg Roedela3b93122012-04-12 12:49:26 +02002190 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002191}
2192
Joerg Roedel15898bb2009-11-24 15:39:42 +01002193/*
2194 * If a device is not yet associated with a domain, this function does
2195 * assigns it visible for the hardware
2196 */
2197static int attach_device(struct device *dev,
2198 struct protection_domain *domain)
2199{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002200 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002201 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002202 unsigned long flags;
2203 int ret;
2204
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002205 dev_data = get_dev_data(dev);
2206
Joerg Roedel52815b72011-11-17 17:24:28 +01002207 if (domain->flags & PD_IOMMUV2_MASK) {
2208 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2209 return -EINVAL;
2210
2211 if (pdev_iommuv2_enable(pdev) != 0)
2212 return -EINVAL;
2213
2214 dev_data->ats.enabled = true;
2215 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002216 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002217 } else if (amd_iommu_iotlb_sup &&
2218 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002219 dev_data->ats.enabled = true;
2220 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2221 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002222
Joerg Roedel15898bb2009-11-24 15:39:42 +01002223 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002224 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002225 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2226
2227 /*
2228 * We might boot into a crash-kernel here. The crashed kernel
2229 * left the caches in the IOMMU dirty. So we have to flush
2230 * here to evict all dirty stuff.
2231 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002232 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002233
2234 return ret;
2235}
2236
2237/*
2238 * Removes a device from a protection domain (unlocked)
2239 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002240static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002241{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002242 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002243 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002244
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002245 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002246
Joerg Roedel2ca76272010-01-22 16:45:31 +01002247 domain = dev_data->domain;
2248
2249 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01002250
Joerg Roedel71f77582011-06-09 19:03:15 +02002251 if (dev_data->alias_data != NULL) {
2252 struct iommu_dev_data *alias_data = dev_data->alias_data;
2253
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002254 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002255 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01002256 }
2257
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002258 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002259 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01002260
Joerg Roedel2ca76272010-01-22 16:45:31 +01002261 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002262
Joerg Roedel21129f72009-09-01 11:59:42 +02002263 /*
2264 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002265 * passthrough domain if it is detached from any other domain.
2266 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02002267 */
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002268 if (dev_data->passthrough &&
Joerg Roedeld3ad9372010-01-22 17:55:27 +01002269 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002270 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01002271}
2272
2273/*
2274 * Removes a device from a protection domain (with devtable_lock held)
2275 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002276static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002277{
Joerg Roedel52815b72011-11-17 17:24:28 +01002278 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002279 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002280 unsigned long flags;
2281
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002282 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002283 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002284
Joerg Roedel355bf552008-12-08 12:02:41 +01002285 /* lock device table */
2286 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002287 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002288 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002289
Joerg Roedel52815b72011-11-17 17:24:28 +01002290 if (domain->flags & PD_IOMMUV2_MASK)
2291 pdev_iommuv2_disable(to_pci_dev(dev));
2292 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002293 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002294
2295 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002296}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002297
Joerg Roedel15898bb2009-11-24 15:39:42 +01002298/*
2299 * Find out the protection domain structure for a given PCI device. This
2300 * will give us the pointer to the page table root for example.
2301 */
2302static struct protection_domain *domain_for_device(struct device *dev)
2303{
Joerg Roedel71f77582011-06-09 19:03:15 +02002304 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002305 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002306 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002307
Joerg Roedel657cbb62009-11-23 15:26:46 +01002308 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002309
Joerg Roedel2b02b092011-06-09 17:48:39 +02002310 if (dev_data->domain)
2311 return dev_data->domain;
2312
Joerg Roedel71f77582011-06-09 19:03:15 +02002313 if (dev_data->alias_data != NULL) {
2314 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02002315
2316 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2317 if (alias_data->domain != NULL) {
2318 __attach_device(dev_data, alias_data->domain);
2319 dom = alias_data->domain;
2320 }
2321 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002322 }
2323
Joerg Roedel15898bb2009-11-24 15:39:42 +01002324 return dom;
2325}
2326
Joerg Roedele275a2a2008-12-10 18:27:25 +01002327static int device_change_notifier(struct notifier_block *nb,
2328 unsigned long action, void *data)
2329{
Joerg Roedele275a2a2008-12-10 18:27:25 +01002330 struct dma_ops_domain *dma_domain;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002331 struct protection_domain *domain;
2332 struct iommu_dev_data *dev_data;
2333 struct device *dev = data;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002334 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002335 unsigned long flags;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002336 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002337
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002338 if (!check_device(dev))
2339 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002340
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002341 devid = get_device_id(dev);
2342 iommu = amd_iommu_rlookup_table[devid];
2343 dev_data = get_dev_data(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002344
2345 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07002346 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002347
2348 domain = domain_for_device(dev);
2349
Joerg Roedele275a2a2008-12-10 18:27:25 +01002350 if (!domain)
2351 goto out;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002352 if (dev_data->passthrough)
Joerg Roedela1ca3312009-09-01 12:22:22 +02002353 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002354 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002355 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002356 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01002357
2358 iommu_init_device(dev);
2359
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002360 /*
2361 * dev_data is still NULL and
2362 * got initialized in iommu_init_device
2363 */
2364 dev_data = get_dev_data(dev);
2365
2366 if (iommu_pass_through || dev_data->iommu_v2) {
2367 dev_data->passthrough = true;
2368 attach_device(dev, pt_domain);
2369 break;
2370 }
2371
Joerg Roedel657cbb62009-11-23 15:26:46 +01002372 domain = domain_for_device(dev);
2373
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002374 /* allocate a protection domain if a device is added */
2375 dma_domain = find_protection_domain(devid);
2376 if (dma_domain)
2377 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01002378 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002379 if (!dma_domain)
2380 goto out;
2381 dma_domain->target_dev = devid;
2382
2383 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2384 list_add_tail(&dma_domain->list, &iommu_pd_list);
2385 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2386
Joerg Roedelac1534a2012-06-21 14:52:40 +02002387 dev_data = get_dev_data(dev);
2388
Joerg Roedel2c9195e2012-07-19 13:42:54 +02002389 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedelac1534a2012-06-21 14:52:40 +02002390
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01002391 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002392 case BUS_NOTIFY_DEL_DEVICE:
2393
2394 iommu_uninit_device(dev);
2395
Joerg Roedele275a2a2008-12-10 18:27:25 +01002396 default:
2397 goto out;
2398 }
2399
Joerg Roedele275a2a2008-12-10 18:27:25 +01002400 iommu_completion_wait(iommu);
2401
2402out:
2403 return 0;
2404}
2405
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302406static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01002407 .notifier_call = device_change_notifier,
2408};
Joerg Roedel355bf552008-12-08 12:02:41 +01002409
Joerg Roedel8638c492009-12-10 11:12:25 +01002410void amd_iommu_init_notifier(void)
2411{
2412 bus_register_notifier(&pci_bus_type, &device_nb);
2413}
2414
Joerg Roedel431b2a22008-07-11 17:14:22 +02002415/*****************************************************************************
2416 *
2417 * The next functions belong to the dma_ops mapping/unmapping code.
2418 *
2419 *****************************************************************************/
2420
2421/*
2422 * In the dma_ops path we only have the struct device. This function
2423 * finds the corresponding IOMMU, the protection domain and the
2424 * requestor id for a given device.
2425 * If the device is not yet associated with a domain this is also done
2426 * in this function.
2427 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002428static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002429{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002430 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002431 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002432 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002433
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002434 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002435 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002436
Joerg Roedel94f6d192009-11-24 16:40:02 +01002437 domain = domain_for_device(dev);
2438 if (domain != NULL && !dma_ops_domain(domain))
2439 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002440
Joerg Roedel94f6d192009-11-24 16:40:02 +01002441 if (domain != NULL)
2442 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002443
Joerg Roedel15898bb2009-11-24 15:39:42 +01002444 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002445 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002446 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01002447 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2448 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002449 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01002450 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01002451
Joerg Roedel94f6d192009-11-24 16:40:02 +01002452 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002453}
2454
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002455static void update_device_table(struct protection_domain *domain)
2456{
Joerg Roedel492667d2009-11-27 13:25:47 +01002457 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002458
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002459 list_for_each_entry(dev_data, &domain->dev_list, list)
2460 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002461}
2462
2463static void update_domain(struct protection_domain *domain)
2464{
2465 if (!domain->updated)
2466 return;
2467
2468 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002469
2470 domain_flush_devices(domain);
2471 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002472
2473 domain->updated = false;
2474}
2475
Joerg Roedel431b2a22008-07-11 17:14:22 +02002476/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02002477 * This function fetches the PTE for a given address in the aperture
2478 */
2479static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2480 unsigned long address)
2481{
Joerg Roedel384de722009-05-15 12:30:05 +02002482 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02002483 u64 *pte, *pte_page;
2484
Joerg Roedel384de722009-05-15 12:30:05 +02002485 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2486 if (!aperture)
2487 return NULL;
2488
2489 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02002490 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01002491 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02002492 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02002493 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2494 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002495 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002496
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002497 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02002498
2499 return pte;
2500}
2501
2502/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002503 * This is the generic map function. It maps one 4kb page at paddr to
2504 * the given address in the DMA address space for the domain.
2505 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002506static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002507 unsigned long address,
2508 phys_addr_t paddr,
2509 int direction)
2510{
2511 u64 *pte, __pte;
2512
2513 WARN_ON(address > dom->aperture_size);
2514
2515 paddr &= PAGE_MASK;
2516
Joerg Roedel8bda3092009-05-12 12:02:46 +02002517 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002518 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002519 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002520
2521 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2522
2523 if (direction == DMA_TO_DEVICE)
2524 __pte |= IOMMU_PTE_IR;
2525 else if (direction == DMA_FROM_DEVICE)
2526 __pte |= IOMMU_PTE_IW;
2527 else if (direction == DMA_BIDIRECTIONAL)
2528 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2529
2530 WARN_ON(*pte);
2531
2532 *pte = __pte;
2533
2534 return (dma_addr_t)address;
2535}
2536
Joerg Roedel431b2a22008-07-11 17:14:22 +02002537/*
2538 * The generic unmapping function for on page in the DMA address space.
2539 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002540static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002541 unsigned long address)
2542{
Joerg Roedel384de722009-05-15 12:30:05 +02002543 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002544 u64 *pte;
2545
2546 if (address >= dom->aperture_size)
2547 return;
2548
Joerg Roedel384de722009-05-15 12:30:05 +02002549 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2550 if (!aperture)
2551 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002552
Joerg Roedel384de722009-05-15 12:30:05 +02002553 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2554 if (!pte)
2555 return;
2556
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002557 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002558
2559 WARN_ON(!*pte);
2560
2561 *pte = 0ULL;
2562}
2563
Joerg Roedel431b2a22008-07-11 17:14:22 +02002564/*
2565 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002566 * contiguous memory region into DMA address space. It is used by all
2567 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002568 * Must be called with the domain lock held.
2569 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002570static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002571 struct dma_ops_domain *dma_dom,
2572 phys_addr_t paddr,
2573 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002574 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002575 bool align,
2576 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002577{
2578 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002579 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002580 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002581 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002582 int i;
2583
Joerg Roedele3c449f2008-10-15 22:02:11 -07002584 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002585 paddr &= PAGE_MASK;
2586
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002587 INC_STATS_COUNTER(total_map_requests);
2588
Joerg Roedelc1858972008-12-12 15:42:39 +01002589 if (pages > 1)
2590 INC_STATS_COUNTER(cross_page);
2591
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002592 if (align)
2593 align_mask = (1UL << get_order(size)) - 1;
2594
Joerg Roedel11b83882009-05-19 10:23:15 +02002595retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002596 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2597 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002598 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002599 /*
2600 * setting next_address here will let the address
2601 * allocator only scan the new allocated range in the
2602 * first run. This is a small optimization.
2603 */
2604 dma_dom->next_address = dma_dom->aperture_size;
2605
Joerg Roedel576175c2009-11-23 19:08:46 +01002606 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002607 goto out;
2608
2609 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002610 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002611 * allocation again
2612 */
2613 goto retry;
2614 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002615
2616 start = address;
2617 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002618 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002619 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002620 goto out_unmap;
2621
Joerg Roedelcb76c322008-06-26 21:28:00 +02002622 paddr += PAGE_SIZE;
2623 start += PAGE_SIZE;
2624 }
2625 address += offset;
2626
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002627 ADD_STATS_COUNTER(alloced_io_mem, size);
2628
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002629 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002630 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002631 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002632 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002633 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002634
Joerg Roedelcb76c322008-06-26 21:28:00 +02002635out:
2636 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002637
2638out_unmap:
2639
2640 for (--i; i >= 0; --i) {
2641 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002642 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002643 }
2644
2645 dma_ops_free_addresses(dma_dom, address, pages);
2646
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002647 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002648}
2649
Joerg Roedel431b2a22008-07-11 17:14:22 +02002650/*
2651 * Does the reverse of the __map_single function. Must be called with
2652 * the domain lock held too
2653 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002654static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002655 dma_addr_t dma_addr,
2656 size_t size,
2657 int dir)
2658{
Joerg Roedel04e04632010-09-23 16:12:48 +02002659 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002660 dma_addr_t i, start;
2661 unsigned int pages;
2662
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002663 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002664 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002665 return;
2666
Joerg Roedel04e04632010-09-23 16:12:48 +02002667 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002668 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002669 dma_addr &= PAGE_MASK;
2670 start = dma_addr;
2671
2672 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002673 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002674 start += PAGE_SIZE;
2675 }
2676
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002677 SUB_STATS_COUNTER(alloced_io_mem, size);
2678
Joerg Roedelcb76c322008-06-26 21:28:00 +02002679 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002680
Joerg Roedel80be3082008-11-06 14:59:05 +01002681 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002682 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002683 dma_dom->need_flush = false;
2684 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002685}
2686
Joerg Roedel431b2a22008-07-11 17:14:22 +02002687/*
2688 * The exported map_single function for dma_ops.
2689 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002690static dma_addr_t map_page(struct device *dev, struct page *page,
2691 unsigned long offset, size_t size,
2692 enum dma_data_direction dir,
2693 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002694{
2695 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002696 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002697 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002698 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002699 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002700
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002701 INC_STATS_COUNTER(cnt_map_single);
2702
Joerg Roedel94f6d192009-11-24 16:40:02 +01002703 domain = get_domain(dev);
2704 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002705 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002706 else if (IS_ERR(domain))
2707 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002708
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002709 dma_mask = *dev->dma_mask;
2710
Joerg Roedel4da70b92008-06-26 21:28:01 +02002711 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002712
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002713 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002714 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002715 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002716 goto out;
2717
Joerg Roedel17b124b2011-04-06 18:01:35 +02002718 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002719
2720out:
2721 spin_unlock_irqrestore(&domain->lock, flags);
2722
2723 return addr;
2724}
2725
Joerg Roedel431b2a22008-07-11 17:14:22 +02002726/*
2727 * The exported unmap_single function for dma_ops.
2728 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002729static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2730 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002731{
2732 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002733 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002734
Joerg Roedel146a6912008-12-12 15:07:12 +01002735 INC_STATS_COUNTER(cnt_unmap_single);
2736
Joerg Roedel94f6d192009-11-24 16:40:02 +01002737 domain = get_domain(dev);
2738 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002739 return;
2740
Joerg Roedel4da70b92008-06-26 21:28:01 +02002741 spin_lock_irqsave(&domain->lock, flags);
2742
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002743 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002744
Joerg Roedel17b124b2011-04-06 18:01:35 +02002745 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002746
2747 spin_unlock_irqrestore(&domain->lock, flags);
2748}
2749
Joerg Roedel431b2a22008-07-11 17:14:22 +02002750/*
2751 * This is a special map_sg function which is used if we should map a
2752 * device which is not handled by an AMD IOMMU in the system.
2753 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002754static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2755 int nelems, int dir)
2756{
2757 struct scatterlist *s;
2758 int i;
2759
2760 for_each_sg(sglist, s, nelems, i) {
2761 s->dma_address = (dma_addr_t)sg_phys(s);
2762 s->dma_length = s->length;
2763 }
2764
2765 return nelems;
2766}
2767
Joerg Roedel431b2a22008-07-11 17:14:22 +02002768/*
2769 * The exported map_sg function for dma_ops (handles scatter-gather
2770 * lists).
2771 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002772static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002773 int nelems, enum dma_data_direction dir,
2774 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002775{
2776 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002777 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002778 int i;
2779 struct scatterlist *s;
2780 phys_addr_t paddr;
2781 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002782 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002783
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002784 INC_STATS_COUNTER(cnt_map_sg);
2785
Joerg Roedel94f6d192009-11-24 16:40:02 +01002786 domain = get_domain(dev);
2787 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002788 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002789 else if (IS_ERR(domain))
2790 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002791
Joerg Roedel832a90c2008-09-18 15:54:23 +02002792 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002793
Joerg Roedel65b050a2008-06-26 21:28:02 +02002794 spin_lock_irqsave(&domain->lock, flags);
2795
2796 for_each_sg(sglist, s, nelems, i) {
2797 paddr = sg_phys(s);
2798
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002799 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002800 paddr, s->length, dir, false,
2801 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002802
2803 if (s->dma_address) {
2804 s->dma_length = s->length;
2805 mapped_elems++;
2806 } else
2807 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002808 }
2809
Joerg Roedel17b124b2011-04-06 18:01:35 +02002810 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002811
2812out:
2813 spin_unlock_irqrestore(&domain->lock, flags);
2814
2815 return mapped_elems;
2816unmap:
2817 for_each_sg(sglist, s, mapped_elems, i) {
2818 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002819 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002820 s->dma_length, dir);
2821 s->dma_address = s->dma_length = 0;
2822 }
2823
2824 mapped_elems = 0;
2825
2826 goto out;
2827}
2828
Joerg Roedel431b2a22008-07-11 17:14:22 +02002829/*
2830 * The exported map_sg function for dma_ops (handles scatter-gather
2831 * lists).
2832 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002833static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002834 int nelems, enum dma_data_direction dir,
2835 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002836{
2837 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002838 struct protection_domain *domain;
2839 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002840 int i;
2841
Joerg Roedel55877a62008-12-12 15:12:14 +01002842 INC_STATS_COUNTER(cnt_unmap_sg);
2843
Joerg Roedel94f6d192009-11-24 16:40:02 +01002844 domain = get_domain(dev);
2845 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002846 return;
2847
Joerg Roedel65b050a2008-06-26 21:28:02 +02002848 spin_lock_irqsave(&domain->lock, flags);
2849
2850 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002851 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002852 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002853 s->dma_address = s->dma_length = 0;
2854 }
2855
Joerg Roedel17b124b2011-04-06 18:01:35 +02002856 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002857
2858 spin_unlock_irqrestore(&domain->lock, flags);
2859}
2860
Joerg Roedel431b2a22008-07-11 17:14:22 +02002861/*
2862 * The exported alloc_coherent function for dma_ops.
2863 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002864static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002865 dma_addr_t *dma_addr, gfp_t flag,
2866 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002867{
2868 unsigned long flags;
2869 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002870 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002871 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002872 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002873
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002874 INC_STATS_COUNTER(cnt_alloc_coherent);
2875
Joerg Roedel94f6d192009-11-24 16:40:02 +01002876 domain = get_domain(dev);
2877 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002878 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2879 *dma_addr = __pa(virt_addr);
2880 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002881 } else if (IS_ERR(domain))
2882 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002883
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002884 dma_mask = dev->coherent_dma_mask;
2885 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2886 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002887
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002888 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2889 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302890 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002891
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002892 paddr = virt_to_phys(virt_addr);
2893
Joerg Roedel832a90c2008-09-18 15:54:23 +02002894 if (!dma_mask)
2895 dma_mask = *dev->dma_mask;
2896
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002897 spin_lock_irqsave(&domain->lock, flags);
2898
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002899 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002900 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002901
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002902 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002903 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002904 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002905 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002906
Joerg Roedel17b124b2011-04-06 18:01:35 +02002907 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002908
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002909 spin_unlock_irqrestore(&domain->lock, flags);
2910
2911 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002912
2913out_free:
2914
2915 free_pages((unsigned long)virt_addr, get_order(size));
2916
2917 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002918}
2919
Joerg Roedel431b2a22008-07-11 17:14:22 +02002920/*
2921 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002922 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002923static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002924 void *virt_addr, dma_addr_t dma_addr,
2925 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002926{
2927 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002928 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002929
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002930 INC_STATS_COUNTER(cnt_free_coherent);
2931
Joerg Roedel94f6d192009-11-24 16:40:02 +01002932 domain = get_domain(dev);
2933 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002934 goto free_mem;
2935
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002936 spin_lock_irqsave(&domain->lock, flags);
2937
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002938 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002939
Joerg Roedel17b124b2011-04-06 18:01:35 +02002940 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002941
2942 spin_unlock_irqrestore(&domain->lock, flags);
2943
2944free_mem:
2945 free_pages((unsigned long)virt_addr, get_order(size));
2946}
2947
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002948/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002949 * This function is called by the DMA layer to find out if we can handle a
2950 * particular device. It is part of the dma_ops.
2951 */
2952static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2953{
Joerg Roedel420aef82009-11-23 16:14:57 +01002954 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002955}
2956
2957/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002958 * The function for pre-allocating protection domains.
2959 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002960 * If the driver core informs the DMA layer if a driver grabs a device
2961 * we don't need to preallocate the protection domains anymore.
2962 * For now we have to.
2963 */
Steffen Persvold943bc7e2012-03-15 12:16:28 +01002964static void __init prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002965{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002966 struct iommu_dev_data *dev_data;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002967 struct dma_ops_domain *dma_dom;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002968 struct pci_dev *dev = NULL;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002969 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002970
Chris Wrightd18c69d2010-04-02 18:27:55 -07002971 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002972
2973 /* Do we handle this device? */
2974 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002975 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002976
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002977 dev_data = get_dev_data(&dev->dev);
2978 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2979 /* Make sure passthrough domain is allocated */
2980 alloc_passthrough_domain();
2981 dev_data->passthrough = true;
2982 attach_device(&dev->dev, pt_domain);
2983 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2984 dev_name(&dev->dev));
2985 }
2986
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002987 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002988 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002989 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002990
2991 devid = get_device_id(&dev->dev);
2992
Joerg Roedel87a64d52009-11-24 17:26:43 +01002993 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002994 if (!dma_dom)
2995 continue;
2996 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002997 dma_dom->target_dev = devid;
2998
Joerg Roedel15898bb2009-11-24 15:39:42 +01002999 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01003000
Joerg Roedelbd60b732008-09-11 10:24:48 +02003001 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02003002 }
3003}
3004
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003005static struct dma_map_ops amd_iommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003006 .alloc = alloc_coherent,
3007 .free = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09003008 .map_page = map_page,
3009 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003010 .map_sg = map_sg,
3011 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02003012 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02003013};
3014
Joerg Roedel27c21272011-05-30 15:56:24 +02003015static unsigned device_dma_ops_init(void)
3016{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003017 struct iommu_dev_data *dev_data;
Joerg Roedel27c21272011-05-30 15:56:24 +02003018 struct pci_dev *pdev = NULL;
3019 unsigned unhandled = 0;
3020
3021 for_each_pci_dev(pdev) {
3022 if (!check_device(&pdev->dev)) {
Joerg Roedelaf1be042012-01-18 14:03:11 +01003023
3024 iommu_ignore_device(&pdev->dev);
3025
Joerg Roedel27c21272011-05-30 15:56:24 +02003026 unhandled += 1;
3027 continue;
3028 }
3029
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003030 dev_data = get_dev_data(&pdev->dev);
3031
3032 if (!dev_data->passthrough)
3033 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3034 else
3035 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
Joerg Roedel27c21272011-05-30 15:56:24 +02003036 }
3037
3038 return unhandled;
3039}
3040
Joerg Roedel431b2a22008-07-11 17:14:22 +02003041/*
3042 * The function which clues the AMD IOMMU driver into dma_ops.
3043 */
Joerg Roedelf5325092010-01-22 17:44:35 +01003044
3045void __init amd_iommu_init_api(void)
3046{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02003047 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01003048}
3049
Joerg Roedel6631ee92008-06-26 21:28:05 +02003050int __init amd_iommu_init_dma_ops(void)
3051{
3052 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02003053 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003054
Joerg Roedel431b2a22008-07-11 17:14:22 +02003055 /*
3056 * first allocate a default protection domain for every IOMMU we
3057 * found in the system. Devices not assigned to any other
3058 * protection domain will be assigned to the default one.
3059 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02003060 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01003061 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003062 if (iommu->default_dom == NULL)
3063 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01003064 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003065 ret = iommu_init_unity_mappings(iommu);
3066 if (ret)
3067 goto free_domains;
3068 }
3069
Joerg Roedel431b2a22008-07-11 17:14:22 +02003070 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01003071 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02003072 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01003073 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02003074
3075 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003076 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02003077
Joerg Roedel431b2a22008-07-11 17:14:22 +02003078 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02003079 unhandled = device_dma_ops_init();
3080 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3081 /* There are unhandled devices - initialize swiotlb for them */
3082 swiotlb = 1;
3083 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02003084
Joerg Roedel7f265082008-12-12 13:50:21 +01003085 amd_iommu_stats_init();
3086
Joerg Roedel62410ee2012-06-12 16:42:43 +02003087 if (amd_iommu_unmap_flush)
3088 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3089 else
3090 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3091
Joerg Roedel6631ee92008-06-26 21:28:05 +02003092 return 0;
3093
3094free_domains:
3095
Joerg Roedel3bd22172009-05-04 15:06:20 +02003096 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02003097 if (iommu->default_dom)
3098 dma_ops_domain_free(iommu->default_dom);
3099 }
3100
3101 return ret;
3102}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003103
3104/*****************************************************************************
3105 *
3106 * The following functions belong to the exported interface of AMD IOMMU
3107 *
3108 * This interface allows access to lower level functions of the IOMMU
3109 * like protection domain handling and assignement of devices to domains
3110 * which is not possible with the dma_ops interface.
3111 *
3112 *****************************************************************************/
3113
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003114static void cleanup_domain(struct protection_domain *domain)
3115{
Joerg Roedel492667d2009-11-27 13:25:47 +01003116 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003117 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003118
3119 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3120
Joerg Roedel492667d2009-11-27 13:25:47 +01003121 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02003122 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01003123 atomic_set(&dev_data->bind, 0);
3124 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01003125
3126 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3127}
3128
Joerg Roedel26508152009-08-26 16:52:40 +02003129static void protection_domain_free(struct protection_domain *domain)
3130{
3131 if (!domain)
3132 return;
3133
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003134 del_domain_from_list(domain);
3135
Joerg Roedel26508152009-08-26 16:52:40 +02003136 if (domain->id)
3137 domain_id_free(domain->id);
3138
3139 kfree(domain);
3140}
3141
3142static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01003143{
3144 struct protection_domain *domain;
3145
3146 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3147 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02003148 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003149
3150 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003151 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01003152 domain->id = domain_id_alloc();
3153 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02003154 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01003155 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02003156
Joerg Roedelaeb26f52009-11-20 16:44:01 +01003157 add_domain_to_list(domain);
3158
Joerg Roedel26508152009-08-26 16:52:40 +02003159 return domain;
3160
3161out_err:
3162 kfree(domain);
3163
3164 return NULL;
3165}
3166
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003167static int __init alloc_passthrough_domain(void)
3168{
3169 if (pt_domain != NULL)
3170 return 0;
3171
3172 /* allocate passthrough domain */
3173 pt_domain = protection_domain_alloc();
3174 if (!pt_domain)
3175 return -ENOMEM;
3176
3177 pt_domain->mode = PAGE_MODE_NONE;
3178
3179 return 0;
3180}
Joerg Roedel26508152009-08-26 16:52:40 +02003181static int amd_iommu_domain_init(struct iommu_domain *dom)
3182{
3183 struct protection_domain *domain;
3184
3185 domain = protection_domain_alloc();
3186 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01003187 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02003188
3189 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01003190 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3191 if (!domain->pt_root)
3192 goto out_free;
3193
Joerg Roedelf3572db2011-11-23 12:36:25 +01003194 domain->iommu_domain = dom;
3195
Joerg Roedelc156e342008-12-02 18:13:27 +01003196 dom->priv = domain;
3197
Joerg Roedel0ff64f82012-01-26 19:40:53 +01003198 dom->geometry.aperture_start = 0;
3199 dom->geometry.aperture_end = ~0ULL;
3200 dom->geometry.force_aperture = true;
3201
Joerg Roedelc156e342008-12-02 18:13:27 +01003202 return 0;
3203
3204out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02003205 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01003206
3207 return -ENOMEM;
3208}
3209
Joerg Roedel98383fc2008-12-02 18:34:12 +01003210static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3211{
3212 struct protection_domain *domain = dom->priv;
3213
3214 if (!domain)
3215 return;
3216
3217 if (domain->dev_cnt > 0)
3218 cleanup_domain(domain);
3219
3220 BUG_ON(domain->dev_cnt != 0);
3221
Joerg Roedel132bd682011-11-17 14:18:46 +01003222 if (domain->mode != PAGE_MODE_NONE)
3223 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003224
Joerg Roedel52815b72011-11-17 17:24:28 +01003225 if (domain->flags & PD_IOMMUV2_MASK)
3226 free_gcr3_table(domain);
3227
Joerg Roedel8b408fe2010-03-08 14:20:07 +01003228 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01003229
3230 dom->priv = NULL;
3231}
3232
Joerg Roedel684f2882008-12-08 12:07:44 +01003233static void amd_iommu_detach_device(struct iommu_domain *dom,
3234 struct device *dev)
3235{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003236 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003237 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003238 u16 devid;
3239
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003240 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003241 return;
3242
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003243 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003244
Joerg Roedel657cbb62009-11-23 15:26:46 +01003245 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003246 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003247
3248 iommu = amd_iommu_rlookup_table[devid];
3249 if (!iommu)
3250 return;
3251
Joerg Roedel684f2882008-12-08 12:07:44 +01003252 iommu_completion_wait(iommu);
3253}
3254
Joerg Roedel01106062008-12-02 19:34:11 +01003255static int amd_iommu_attach_device(struct iommu_domain *dom,
3256 struct device *dev)
3257{
3258 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01003259 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003260 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003261 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003262
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003263 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003264 return -EINVAL;
3265
Joerg Roedel657cbb62009-11-23 15:26:46 +01003266 dev_data = dev->archdata.iommu;
3267
Joerg Roedelf62dda62011-06-09 12:55:35 +02003268 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003269 if (!iommu)
3270 return -EINVAL;
3271
Joerg Roedel657cbb62009-11-23 15:26:46 +01003272 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003273 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003274
Joerg Roedel15898bb2009-11-24 15:39:42 +01003275 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003276
3277 iommu_completion_wait(iommu);
3278
Joerg Roedel15898bb2009-11-24 15:39:42 +01003279 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003280}
3281
Joerg Roedel468e2362010-01-21 16:37:36 +01003282static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003283 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003284{
3285 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003286 int prot = 0;
3287 int ret;
3288
Joerg Roedel132bd682011-11-17 14:18:46 +01003289 if (domain->mode == PAGE_MODE_NONE)
3290 return -EINVAL;
3291
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003292 if (iommu_prot & IOMMU_READ)
3293 prot |= IOMMU_PROT_IR;
3294 if (iommu_prot & IOMMU_WRITE)
3295 prot |= IOMMU_PROT_IW;
3296
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003297 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003298 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003299 mutex_unlock(&domain->api_lock);
3300
Joerg Roedel795e74f72010-05-11 17:40:57 +02003301 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003302}
3303
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003304static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3305 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003306{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003307 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003308 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003309
Joerg Roedel132bd682011-11-17 14:18:46 +01003310 if (domain->mode == PAGE_MODE_NONE)
3311 return -EINVAL;
3312
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003313 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003314 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003315 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003316
Joerg Roedel17b124b2011-04-06 18:01:35 +02003317 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003318
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003319 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003320}
3321
Joerg Roedel645c4c82008-12-02 20:05:50 +01003322static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3323 unsigned long iova)
3324{
3325 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003326 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003327 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003328 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003329
Joerg Roedel132bd682011-11-17 14:18:46 +01003330 if (domain->mode == PAGE_MODE_NONE)
3331 return iova;
3332
Joerg Roedel24cd7722010-01-19 17:27:39 +01003333 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003334
Joerg Roedela6d41a42009-09-02 17:08:55 +02003335 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003336 return 0;
3337
Joerg Roedelf03152b2010-01-21 16:15:24 +01003338 if (PM_PTE_LEVEL(*pte) == 0)
3339 offset_mask = PAGE_SIZE - 1;
3340 else
3341 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3342
3343 __pte = *pte & PM_ADDR_MASK;
3344 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003345
3346 return paddr;
3347}
3348
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003349static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3350 unsigned long cap)
3351{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003352 switch (cap) {
3353 case IOMMU_CAP_CACHE_COHERENCY:
3354 return 1;
3355 }
3356
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003357 return 0;
3358}
3359
Joerg Roedel26961ef2008-12-03 17:00:17 +01003360static struct iommu_ops amd_iommu_ops = {
3361 .domain_init = amd_iommu_domain_init,
3362 .domain_destroy = amd_iommu_domain_destroy,
3363 .attach_dev = amd_iommu_attach_device,
3364 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003365 .map = amd_iommu_map,
3366 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003367 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003368 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003369 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003370};
3371
Joerg Roedel0feae532009-08-26 15:26:30 +02003372/*****************************************************************************
3373 *
3374 * The next functions do a basic initialization of IOMMU for pass through
3375 * mode
3376 *
3377 * In passthrough mode the IOMMU is initialized and enabled but not used for
3378 * DMA-API translation.
3379 *
3380 *****************************************************************************/
3381
3382int __init amd_iommu_init_passthrough(void)
3383{
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003384 struct iommu_dev_data *dev_data;
Joerg Roedel0feae532009-08-26 15:26:30 +02003385 struct pci_dev *dev = NULL;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003386 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003387 u16 devid;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003388 int ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003389
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003390 ret = alloc_passthrough_domain();
3391 if (ret)
3392 return ret;
Joerg Roedel0feae532009-08-26 15:26:30 +02003393
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04003394 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003395 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02003396 continue;
3397
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003398 dev_data = get_dev_data(&dev->dev);
3399 dev_data->passthrough = true;
3400
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003401 devid = get_device_id(&dev->dev);
3402
Joerg Roedel15898bb2009-11-24 15:39:42 +01003403 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02003404 if (!iommu)
3405 continue;
3406
Joerg Roedel15898bb2009-11-24 15:39:42 +01003407 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02003408 }
3409
Joerg Roedel2655d7a2011-12-22 12:35:38 +01003410 amd_iommu_stats_init();
3411
Joerg Roedel0feae532009-08-26 15:26:30 +02003412 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3413
3414 return 0;
3415}
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003416
3417/* IOMMUv2 specific functions */
3418int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3419{
3420 return atomic_notifier_chain_register(&ppr_notifier, nb);
3421}
3422EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3423
3424int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3425{
3426 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3427}
3428EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003429
3430void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3431{
3432 struct protection_domain *domain = dom->priv;
3433 unsigned long flags;
3434
3435 spin_lock_irqsave(&domain->lock, flags);
3436
3437 /* Update data structure */
3438 domain->mode = PAGE_MODE_NONE;
3439 domain->updated = true;
3440
3441 /* Make changes visible to IOMMUs */
3442 update_domain(domain);
3443
3444 /* Page-table is not visible to IOMMU anymore, so free it */
3445 free_pagetable(domain);
3446
3447 spin_unlock_irqrestore(&domain->lock, flags);
3448}
3449EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003450
3451int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3452{
3453 struct protection_domain *domain = dom->priv;
3454 unsigned long flags;
3455 int levels, ret;
3456
3457 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3458 return -EINVAL;
3459
3460 /* Number of GCR3 table levels required */
3461 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3462 levels += 1;
3463
3464 if (levels > amd_iommu_max_glx_val)
3465 return -EINVAL;
3466
3467 spin_lock_irqsave(&domain->lock, flags);
3468
3469 /*
3470 * Save us all sanity checks whether devices already in the
3471 * domain support IOMMUv2. Just force that the domain has no
3472 * devices attached when it is switched into IOMMUv2 mode.
3473 */
3474 ret = -EBUSY;
3475 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3476 goto out;
3477
3478 ret = -ENOMEM;
3479 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3480 if (domain->gcr3_tbl == NULL)
3481 goto out;
3482
3483 domain->glx = levels;
3484 domain->flags |= PD_IOMMUV2_MASK;
3485 domain->updated = true;
3486
3487 update_domain(domain);
3488
3489 ret = 0;
3490
3491out:
3492 spin_unlock_irqrestore(&domain->lock, flags);
3493
3494 return ret;
3495}
3496EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003497
3498static int __flush_pasid(struct protection_domain *domain, int pasid,
3499 u64 address, bool size)
3500{
3501 struct iommu_dev_data *dev_data;
3502 struct iommu_cmd cmd;
3503 int i, ret;
3504
3505 if (!(domain->flags & PD_IOMMUV2_MASK))
3506 return -EINVAL;
3507
3508 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3509
3510 /*
3511 * IOMMU TLB needs to be flushed before Device TLB to
3512 * prevent device TLB refill from IOMMU TLB
3513 */
3514 for (i = 0; i < amd_iommus_present; ++i) {
3515 if (domain->dev_iommu[i] == 0)
3516 continue;
3517
3518 ret = iommu_queue_command(amd_iommus[i], &cmd);
3519 if (ret != 0)
3520 goto out;
3521 }
3522
3523 /* Wait until IOMMU TLB flushes are complete */
3524 domain_flush_complete(domain);
3525
3526 /* Now flush device TLBs */
3527 list_for_each_entry(dev_data, &domain->dev_list, list) {
3528 struct amd_iommu *iommu;
3529 int qdep;
3530
3531 BUG_ON(!dev_data->ats.enabled);
3532
3533 qdep = dev_data->ats.qdep;
3534 iommu = amd_iommu_rlookup_table[dev_data->devid];
3535
3536 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3537 qdep, address, size);
3538
3539 ret = iommu_queue_command(iommu, &cmd);
3540 if (ret != 0)
3541 goto out;
3542 }
3543
3544 /* Wait until all device TLBs are flushed */
3545 domain_flush_complete(domain);
3546
3547 ret = 0;
3548
3549out:
3550
3551 return ret;
3552}
3553
3554static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3555 u64 address)
3556{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003557 INC_STATS_COUNTER(invalidate_iotlb);
3558
Joerg Roedel22e266c2011-11-21 15:59:08 +01003559 return __flush_pasid(domain, pasid, address, false);
3560}
3561
3562int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3563 u64 address)
3564{
3565 struct protection_domain *domain = dom->priv;
3566 unsigned long flags;
3567 int ret;
3568
3569 spin_lock_irqsave(&domain->lock, flags);
3570 ret = __amd_iommu_flush_page(domain, pasid, address);
3571 spin_unlock_irqrestore(&domain->lock, flags);
3572
3573 return ret;
3574}
3575EXPORT_SYMBOL(amd_iommu_flush_page);
3576
3577static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3578{
Joerg Roedel399be2f2011-12-01 16:53:47 +01003579 INC_STATS_COUNTER(invalidate_iotlb_all);
3580
Joerg Roedel22e266c2011-11-21 15:59:08 +01003581 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3582 true);
3583}
3584
3585int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3586{
3587 struct protection_domain *domain = dom->priv;
3588 unsigned long flags;
3589 int ret;
3590
3591 spin_lock_irqsave(&domain->lock, flags);
3592 ret = __amd_iommu_flush_tlb(domain, pasid);
3593 spin_unlock_irqrestore(&domain->lock, flags);
3594
3595 return ret;
3596}
3597EXPORT_SYMBOL(amd_iommu_flush_tlb);
3598
Joerg Roedelb16137b2011-11-21 16:50:23 +01003599static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3600{
3601 int index;
3602 u64 *pte;
3603
3604 while (true) {
3605
3606 index = (pasid >> (9 * level)) & 0x1ff;
3607 pte = &root[index];
3608
3609 if (level == 0)
3610 break;
3611
3612 if (!(*pte & GCR3_VALID)) {
3613 if (!alloc)
3614 return NULL;
3615
3616 root = (void *)get_zeroed_page(GFP_ATOMIC);
3617 if (root == NULL)
3618 return NULL;
3619
3620 *pte = __pa(root) | GCR3_VALID;
3621 }
3622
3623 root = __va(*pte & PAGE_MASK);
3624
3625 level -= 1;
3626 }
3627
3628 return pte;
3629}
3630
3631static int __set_gcr3(struct protection_domain *domain, int pasid,
3632 unsigned long cr3)
3633{
3634 u64 *pte;
3635
3636 if (domain->mode != PAGE_MODE_NONE)
3637 return -EINVAL;
3638
3639 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3640 if (pte == NULL)
3641 return -ENOMEM;
3642
3643 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3644
3645 return __amd_iommu_flush_tlb(domain, pasid);
3646}
3647
3648static int __clear_gcr3(struct protection_domain *domain, int pasid)
3649{
3650 u64 *pte;
3651
3652 if (domain->mode != PAGE_MODE_NONE)
3653 return -EINVAL;
3654
3655 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3656 if (pte == NULL)
3657 return 0;
3658
3659 *pte = 0;
3660
3661 return __amd_iommu_flush_tlb(domain, pasid);
3662}
3663
3664int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3665 unsigned long cr3)
3666{
3667 struct protection_domain *domain = dom->priv;
3668 unsigned long flags;
3669 int ret;
3670
3671 spin_lock_irqsave(&domain->lock, flags);
3672 ret = __set_gcr3(domain, pasid, cr3);
3673 spin_unlock_irqrestore(&domain->lock, flags);
3674
3675 return ret;
3676}
3677EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3678
3679int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3680{
3681 struct protection_domain *domain = dom->priv;
3682 unsigned long flags;
3683 int ret;
3684
3685 spin_lock_irqsave(&domain->lock, flags);
3686 ret = __clear_gcr3(domain, pasid);
3687 spin_unlock_irqrestore(&domain->lock, flags);
3688
3689 return ret;
3690}
3691EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003692
3693int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3694 int status, int tag)
3695{
3696 struct iommu_dev_data *dev_data;
3697 struct amd_iommu *iommu;
3698 struct iommu_cmd cmd;
3699
Joerg Roedel399be2f2011-12-01 16:53:47 +01003700 INC_STATS_COUNTER(complete_ppr);
3701
Joerg Roedelc99afa22011-11-21 18:19:25 +01003702 dev_data = get_dev_data(&pdev->dev);
3703 iommu = amd_iommu_rlookup_table[dev_data->devid];
3704
3705 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3706 tag, dev_data->pri_tlp);
3707
3708 return iommu_queue_command(iommu, &cmd);
3709}
3710EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003711
3712struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3713{
3714 struct protection_domain *domain;
3715
3716 domain = get_domain(&pdev->dev);
3717 if (IS_ERR(domain))
3718 return NULL;
3719
3720 /* Only return IOMMUv2 domains */
3721 if (!(domain->flags & PD_IOMMUV2_MASK))
3722 return NULL;
3723
3724 return domain->iommu_domain;
3725}
3726EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003727
3728void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3729{
3730 struct iommu_dev_data *dev_data;
3731
3732 if (!amd_iommu_v2_supported())
3733 return;
3734
3735 dev_data = get_dev_data(&pdev->dev);
3736 dev_data->errata |= (1 << erratum);
3737}
3738EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003739
3740int amd_iommu_device_info(struct pci_dev *pdev,
3741 struct amd_iommu_device_info *info)
3742{
3743 int max_pasids;
3744 int pos;
3745
3746 if (pdev == NULL || info == NULL)
3747 return -EINVAL;
3748
3749 if (!amd_iommu_v2_supported())
3750 return -EINVAL;
3751
3752 memset(info, 0, sizeof(*info));
3753
3754 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3755 if (pos)
3756 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3757
3758 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3759 if (pos)
3760 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3761
3762 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3763 if (pos) {
3764 int features;
3765
3766 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3767 max_pasids = min(max_pasids, (1 << 20));
3768
3769 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3770 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3771
3772 features = pci_pasid_features(pdev);
3773 if (features & PCI_PASID_CAP_EXEC)
3774 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3775 if (features & PCI_PASID_CAP_PRIV)
3776 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3777 }
3778
3779 return 0;
3780}
3781EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003782
3783#ifdef CONFIG_IRQ_REMAP
3784
3785/*****************************************************************************
3786 *
3787 * Interrupt Remapping Implementation
3788 *
3789 *****************************************************************************/
3790
3791union irte {
3792 u32 val;
3793 struct {
3794 u32 valid : 1,
3795 no_fault : 1,
3796 int_type : 3,
3797 rq_eoi : 1,
3798 dm : 1,
3799 rsvd_1 : 1,
3800 destination : 8,
3801 vector : 8,
3802 rsvd_2 : 8;
3803 } fields;
3804};
3805
3806#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3807#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3808#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3809#define DTE_IRQ_REMAP_ENABLE 1ULL
3810
3811static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3812{
3813 u64 dte;
3814
3815 dte = amd_iommu_dev_table[devid].data[2];
3816 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3817 dte |= virt_to_phys(table->table);
3818 dte |= DTE_IRQ_REMAP_INTCTL;
3819 dte |= DTE_IRQ_TABLE_LEN;
3820 dte |= DTE_IRQ_REMAP_ENABLE;
3821
3822 amd_iommu_dev_table[devid].data[2] = dte;
3823}
3824
3825#define IRTE_ALLOCATED (~1U)
3826
3827static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3828{
3829 struct irq_remap_table *table = NULL;
3830 struct amd_iommu *iommu;
3831 unsigned long flags;
3832 u16 alias;
3833
3834 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3835
3836 iommu = amd_iommu_rlookup_table[devid];
3837 if (!iommu)
3838 goto out_unlock;
3839
3840 table = irq_lookup_table[devid];
3841 if (table)
3842 goto out;
3843
3844 alias = amd_iommu_alias_table[devid];
3845 table = irq_lookup_table[alias];
3846 if (table) {
3847 irq_lookup_table[devid] = table;
3848 set_dte_irq_entry(devid, table);
3849 iommu_flush_dte(iommu, devid);
3850 goto out;
3851 }
3852
3853 /* Nothing there yet, allocate new irq remapping table */
3854 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3855 if (!table)
3856 goto out;
3857
3858 if (ioapic)
3859 /* Keep the first 32 indexes free for IOAPIC interrupts */
3860 table->min_index = 32;
3861
3862 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3863 if (!table->table) {
3864 kfree(table);
3865 goto out;
3866 }
3867
3868 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3869
3870 if (ioapic) {
3871 int i;
3872
3873 for (i = 0; i < 32; ++i)
3874 table->table[i] = IRTE_ALLOCATED;
3875 }
3876
3877 irq_lookup_table[devid] = table;
3878 set_dte_irq_entry(devid, table);
3879 iommu_flush_dte(iommu, devid);
3880 if (devid != alias) {
3881 irq_lookup_table[alias] = table;
3882 set_dte_irq_entry(devid, table);
3883 iommu_flush_dte(iommu, alias);
3884 }
3885
3886out:
3887 iommu_completion_wait(iommu);
3888
3889out_unlock:
3890 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3891
3892 return table;
3893}
3894
3895static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3896{
3897 struct irq_remap_table *table;
3898 unsigned long flags;
3899 int index, c;
3900
3901 table = get_irq_table(devid, false);
3902 if (!table)
3903 return -ENODEV;
3904
3905 spin_lock_irqsave(&table->lock, flags);
3906
3907 /* Scan table for free entries */
3908 for (c = 0, index = table->min_index;
3909 index < MAX_IRQS_PER_TABLE;
3910 ++index) {
3911 if (table->table[index] == 0)
3912 c += 1;
3913 else
3914 c = 0;
3915
3916 if (c == count) {
3917 struct irq_2_iommu *irte_info;
3918
3919 for (; c != 0; --c)
3920 table->table[index - c + 1] = IRTE_ALLOCATED;
3921
3922 index -= count - 1;
3923
3924 irte_info = &cfg->irq_2_iommu;
3925 irte_info->sub_handle = devid;
3926 irte_info->irte_index = index;
3927 irte_info->iommu = (void *)cfg;
3928
3929 goto out;
3930 }
3931 }
3932
3933 index = -ENOSPC;
3934
3935out:
3936 spin_unlock_irqrestore(&table->lock, flags);
3937
3938 return index;
3939}
3940
3941static int get_irte(u16 devid, int index, union irte *irte)
3942{
3943 struct irq_remap_table *table;
3944 unsigned long flags;
3945
3946 table = get_irq_table(devid, false);
3947 if (!table)
3948 return -ENOMEM;
3949
3950 spin_lock_irqsave(&table->lock, flags);
3951 irte->val = table->table[index];
3952 spin_unlock_irqrestore(&table->lock, flags);
3953
3954 return 0;
3955}
3956
3957static int modify_irte(u16 devid, int index, union irte irte)
3958{
3959 struct irq_remap_table *table;
3960 struct amd_iommu *iommu;
3961 unsigned long flags;
3962
3963 iommu = amd_iommu_rlookup_table[devid];
3964 if (iommu == NULL)
3965 return -EINVAL;
3966
3967 table = get_irq_table(devid, false);
3968 if (!table)
3969 return -ENOMEM;
3970
3971 spin_lock_irqsave(&table->lock, flags);
3972 table->table[index] = irte.val;
3973 spin_unlock_irqrestore(&table->lock, flags);
3974
3975 iommu_flush_irt(iommu, devid);
3976 iommu_completion_wait(iommu);
3977
3978 return 0;
3979}
3980
3981static void free_irte(u16 devid, int index)
3982{
3983 struct irq_remap_table *table;
3984 struct amd_iommu *iommu;
3985 unsigned long flags;
3986
3987 iommu = amd_iommu_rlookup_table[devid];
3988 if (iommu == NULL)
3989 return;
3990
3991 table = get_irq_table(devid, false);
3992 if (!table)
3993 return;
3994
3995 spin_lock_irqsave(&table->lock, flags);
3996 table->table[index] = 0;
3997 spin_unlock_irqrestore(&table->lock, flags);
3998
3999 iommu_flush_irt(iommu, devid);
4000 iommu_completion_wait(iommu);
4001}
4002
Joerg Roedel5527de72012-06-26 11:17:32 +02004003static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4004 unsigned int destination, int vector,
4005 struct io_apic_irq_attr *attr)
4006{
4007 struct irq_remap_table *table;
4008 struct irq_2_iommu *irte_info;
4009 struct irq_cfg *cfg;
4010 union irte irte;
4011 int ioapic_id;
4012 int index;
4013 int devid;
4014 int ret;
4015
4016 cfg = irq_get_chip_data(irq);
4017 if (!cfg)
4018 return -EINVAL;
4019
4020 irte_info = &cfg->irq_2_iommu;
4021 ioapic_id = mpc_ioapic_id(attr->ioapic);
4022 devid = get_ioapic_devid(ioapic_id);
4023
4024 if (devid < 0)
4025 return devid;
4026
4027 table = get_irq_table(devid, true);
4028 if (table == NULL)
4029 return -ENOMEM;
4030
4031 index = attr->ioapic_pin;
4032
4033 /* Setup IRQ remapping info */
4034 irte_info->sub_handle = devid;
4035 irte_info->irte_index = index;
4036 irte_info->iommu = (void *)cfg;
4037
4038 /* Setup IRTE for IOMMU */
4039 irte.val = 0;
4040 irte.fields.vector = vector;
4041 irte.fields.int_type = apic->irq_delivery_mode;
4042 irte.fields.destination = destination;
4043 irte.fields.dm = apic->irq_dest_mode;
4044 irte.fields.valid = 1;
4045
4046 ret = modify_irte(devid, index, irte);
4047 if (ret)
4048 return ret;
4049
4050 /* Setup IOAPIC entry */
4051 memset(entry, 0, sizeof(*entry));
4052
4053 entry->vector = index;
4054 entry->mask = 0;
4055 entry->trigger = attr->trigger;
4056 entry->polarity = attr->polarity;
4057
4058 /*
4059 * Mask level triggered irqs.
4060 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
4061 */
4062 if (attr->trigger)
4063 entry->mask = 1;
4064
4065 return 0;
4066}
4067
4068static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4069 bool force)
4070{
4071 struct irq_2_iommu *irte_info;
4072 unsigned int dest, irq;
4073 struct irq_cfg *cfg;
4074 union irte irte;
4075 int err;
4076
4077 if (!config_enabled(CONFIG_SMP))
4078 return -1;
4079
4080 cfg = data->chip_data;
4081 irq = data->irq;
4082 irte_info = &cfg->irq_2_iommu;
4083
4084 if (!cpumask_intersects(mask, cpu_online_mask))
4085 return -EINVAL;
4086
4087 if (get_irte(irte_info->sub_handle, irte_info->irte_index, &irte))
4088 return -EBUSY;
4089
4090 if (assign_irq_vector(irq, cfg, mask))
4091 return -EBUSY;
4092
4093 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4094 if (err) {
4095 if (assign_irq_vector(irq, cfg, data->affinity))
4096 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4097 return err;
4098 }
4099
4100 irte.fields.vector = cfg->vector;
4101 irte.fields.destination = dest;
4102
4103 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4104
4105 if (cfg->move_in_progress)
4106 send_cleanup_vector(cfg);
4107
4108 cpumask_copy(data->affinity, mask);
4109
4110 return 0;
4111}
4112
4113static int free_irq(int irq)
4114{
4115 struct irq_2_iommu *irte_info;
4116 struct irq_cfg *cfg;
4117
4118 cfg = irq_get_chip_data(irq);
4119 if (!cfg)
4120 return -EINVAL;
4121
4122 irte_info = &cfg->irq_2_iommu;
4123
4124 free_irte(irte_info->sub_handle, irte_info->irte_index);
4125
4126 return 0;
4127}
4128
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004129static void compose_msi_msg(struct pci_dev *pdev,
4130 unsigned int irq, unsigned int dest,
4131 struct msi_msg *msg, u8 hpet_id)
4132{
4133 struct irq_2_iommu *irte_info;
4134 struct irq_cfg *cfg;
4135 union irte irte;
4136
4137 cfg = irq_get_chip_data(irq);
4138 if (!cfg)
4139 return;
4140
4141 irte_info = &cfg->irq_2_iommu;
4142
4143 irte.val = 0;
4144 irte.fields.vector = cfg->vector;
4145 irte.fields.int_type = apic->irq_delivery_mode;
4146 irte.fields.destination = dest;
4147 irte.fields.dm = apic->irq_dest_mode;
4148 irte.fields.valid = 1;
4149
4150 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4151
4152 msg->address_hi = MSI_ADDR_BASE_HI;
4153 msg->address_lo = MSI_ADDR_BASE_LO;
4154 msg->data = irte_info->irte_index;
4155}
4156
4157static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4158{
4159 struct irq_cfg *cfg;
4160 int index;
4161 u16 devid;
4162
4163 if (!pdev)
4164 return -EINVAL;
4165
4166 cfg = irq_get_chip_data(irq);
4167 if (!cfg)
4168 return -EINVAL;
4169
4170 devid = get_device_id(&pdev->dev);
4171 index = alloc_irq_index(cfg, devid, nvec);
4172
4173 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4174}
4175
4176static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4177 int index, int offset)
4178{
4179 struct irq_2_iommu *irte_info;
4180 struct irq_cfg *cfg;
4181 u16 devid;
4182
4183 if (!pdev)
4184 return -EINVAL;
4185
4186 cfg = irq_get_chip_data(irq);
4187 if (!cfg)
4188 return -EINVAL;
4189
4190 if (index >= MAX_IRQS_PER_TABLE)
4191 return 0;
4192
4193 devid = get_device_id(&pdev->dev);
4194 irte_info = &cfg->irq_2_iommu;
4195
4196 irte_info->sub_handle = devid;
4197 irte_info->irte_index = index + offset;
4198 irte_info->iommu = (void *)cfg;
4199
4200 return 0;
4201}
4202
Joerg Roedel2b324502012-06-21 16:29:10 +02004203#endif