blob: 341573821864e48b0715a02d7712a0dbcbc84d91 [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
20#include <linux/pci.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020021#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080022#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010024#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020025#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090026#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020027#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010028#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020029#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020030#include <linux/amd-iommu.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020031#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020032#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090033#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010034#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020035#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020036
37#include "amd_iommu_proto.h"
38#include "amd_iommu_types.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020039
40#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
41
Joerg Roedel815b33f2011-04-06 17:26:49 +020042#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020043
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020044/*
45 * This bitmap is used to advertise the page sizes our hardware support
46 * to the IOMMU core, which will then use this information to split
47 * physically contiguous memory regions it is mapping into page sizes
48 * that we support.
49 *
50 * Traditionally the IOMMU core just handed us the mappings directly,
51 * after making sure the size is an order of a 4KiB page and that the
52 * mapping has natural alignment.
53 *
54 * To retain this behavior, we currently advertise that we support
55 * all page sizes that are an order of 4KiB.
56 *
57 * If at some point we'd like to utilize the IOMMU core's new behavior,
58 * we could change this to advertise the real page sizes we support.
59 */
60#define AMD_IOMMU_PGSIZES (~0xFFFUL)
61
Joerg Roedelb6c02712008-06-26 21:27:53 +020062static DEFINE_RWLOCK(amd_iommu_devtable_lock);
63
Joerg Roedelbd60b732008-09-11 10:24:48 +020064/* A list of preallocated protection domains */
65static LIST_HEAD(iommu_pd_list);
66static DEFINE_SPINLOCK(iommu_pd_list_lock);
67
Joerg Roedel8fa5f802011-06-09 12:24:45 +020068/* List of all available dev_data structures */
69static LIST_HEAD(dev_data_list);
70static DEFINE_SPINLOCK(dev_data_list_lock);
71
Joerg Roedel0feae532009-08-26 15:26:30 +020072/*
73 * Domain for untranslated devices - only allocated
74 * if iommu=pt passed on kernel cmd line.
75 */
76static struct protection_domain *pt_domain;
77
Joerg Roedel26961ef2008-12-03 17:00:17 +010078static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010079
Joerg Roedel431b2a22008-07-11 17:14:22 +020080/*
81 * general struct to manage commands send to an IOMMU
82 */
Joerg Roedeld6449532008-07-11 17:14:28 +020083struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020084 u32 data[4];
85};
86
Joerg Roedel04bfdd82009-09-02 16:00:23 +020087static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070088
Joerg Roedel15898bb2009-11-24 15:39:42 +010089/****************************************************************************
90 *
91 * Helper functions
92 *
93 ****************************************************************************/
94
Joerg Roedelf62dda62011-06-09 12:55:35 +020095static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +020096{
97 struct iommu_dev_data *dev_data;
98 unsigned long flags;
99
100 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
101 if (!dev_data)
102 return NULL;
103
Joerg Roedelf62dda62011-06-09 12:55:35 +0200104 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200105 atomic_set(&dev_data->bind, 0);
106
107 spin_lock_irqsave(&dev_data_list_lock, flags);
108 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
109 spin_unlock_irqrestore(&dev_data_list_lock, flags);
110
111 return dev_data;
112}
113
114static void free_dev_data(struct iommu_dev_data *dev_data)
115{
116 unsigned long flags;
117
118 spin_lock_irqsave(&dev_data_list_lock, flags);
119 list_del(&dev_data->dev_data_list);
120 spin_unlock_irqrestore(&dev_data_list_lock, flags);
121
122 kfree(dev_data);
123}
124
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200125static struct iommu_dev_data *search_dev_data(u16 devid)
126{
127 struct iommu_dev_data *dev_data;
128 unsigned long flags;
129
130 spin_lock_irqsave(&dev_data_list_lock, flags);
131 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
132 if (dev_data->devid == devid)
133 goto out_unlock;
134 }
135
136 dev_data = NULL;
137
138out_unlock:
139 spin_unlock_irqrestore(&dev_data_list_lock, flags);
140
141 return dev_data;
142}
143
144static struct iommu_dev_data *find_dev_data(u16 devid)
145{
146 struct iommu_dev_data *dev_data;
147
148 dev_data = search_dev_data(devid);
149
150 if (dev_data == NULL)
151 dev_data = alloc_dev_data(devid);
152
153 return dev_data;
154}
155
Joerg Roedel15898bb2009-11-24 15:39:42 +0100156static inline u16 get_device_id(struct device *dev)
157{
158 struct pci_dev *pdev = to_pci_dev(dev);
159
160 return calc_devid(pdev->bus->number, pdev->devfn);
161}
162
Joerg Roedel657cbb62009-11-23 15:26:46 +0100163static struct iommu_dev_data *get_dev_data(struct device *dev)
164{
165 return dev->archdata.iommu;
166}
167
Joerg Roedel71c70982009-11-24 16:43:06 +0100168/*
169 * In this function the list of preallocated protection domains is traversed to
170 * find the domain for a specific device
171 */
172static struct dma_ops_domain *find_protection_domain(u16 devid)
173{
174 struct dma_ops_domain *entry, *ret = NULL;
175 unsigned long flags;
176 u16 alias = amd_iommu_alias_table[devid];
177
178 if (list_empty(&iommu_pd_list))
179 return NULL;
180
181 spin_lock_irqsave(&iommu_pd_list_lock, flags);
182
183 list_for_each_entry(entry, &iommu_pd_list, list) {
184 if (entry->target_dev == devid ||
185 entry->target_dev == alias) {
186 ret = entry;
187 break;
188 }
189 }
190
191 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
192
193 return ret;
194}
195
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100196/*
197 * This function checks if the driver got a valid device from the caller to
198 * avoid dereferencing invalid pointers.
199 */
200static bool check_device(struct device *dev)
201{
202 u16 devid;
203
204 if (!dev || !dev->dma_mask)
205 return false;
206
207 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100208 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100209 return false;
210
211 devid = get_device_id(dev);
212
213 /* Out of our scope? */
214 if (devid > amd_iommu_last_bdf)
215 return false;
216
217 if (amd_iommu_rlookup_table[devid] == NULL)
218 return false;
219
220 return true;
221}
222
Joerg Roedel657cbb62009-11-23 15:26:46 +0100223static int iommu_init_device(struct device *dev)
224{
225 struct iommu_dev_data *dev_data;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200226 u16 alias;
Joerg Roedel657cbb62009-11-23 15:26:46 +0100227
228 if (dev->archdata.iommu)
229 return 0;
230
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200231 dev_data = find_dev_data(get_device_id(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +0100232 if (!dev_data)
233 return -ENOMEM;
234
Joerg Roedelf62dda62011-06-09 12:55:35 +0200235 alias = amd_iommu_alias_table[dev_data->devid];
Joerg Roedel2b02b092011-06-09 17:48:39 +0200236 if (alias != dev_data->devid) {
Joerg Roedel71f77582011-06-09 19:03:15 +0200237 struct iommu_dev_data *alias_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100238
Joerg Roedel71f77582011-06-09 19:03:15 +0200239 alias_data = find_dev_data(alias);
240 if (alias_data == NULL) {
241 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
242 dev_name(dev));
Joerg Roedel2b02b092011-06-09 17:48:39 +0200243 free_dev_data(dev_data);
244 return -ENOTSUPP;
245 }
Joerg Roedel71f77582011-06-09 19:03:15 +0200246 dev_data->alias_data = alias_data;
Joerg Roedel26018872011-06-06 16:50:14 +0200247 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100248
249 dev->archdata.iommu = dev_data;
250
Joerg Roedel657cbb62009-11-23 15:26:46 +0100251 return 0;
252}
253
Joerg Roedel26018872011-06-06 16:50:14 +0200254static void iommu_ignore_device(struct device *dev)
255{
256 u16 devid, alias;
257
258 devid = get_device_id(dev);
259 alias = amd_iommu_alias_table[devid];
260
261 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
262 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
263
264 amd_iommu_rlookup_table[devid] = NULL;
265 amd_iommu_rlookup_table[alias] = NULL;
266}
267
Joerg Roedel657cbb62009-11-23 15:26:46 +0100268static void iommu_uninit_device(struct device *dev)
269{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200270 /*
271 * Nothing to do here - we keep dev_data around for unplugged devices
272 * and reuse it when the device is re-plugged - not doing so would
273 * introduce a ton of races.
274 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100275}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100276
277void __init amd_iommu_uninit_devices(void)
278{
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200279 struct iommu_dev_data *dev_data, *n;
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100280 struct pci_dev *pdev = NULL;
281
282 for_each_pci_dev(pdev) {
283
284 if (!check_device(&pdev->dev))
285 continue;
286
287 iommu_uninit_device(&pdev->dev);
288 }
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200289
290 /* Free all of our dev_data structures */
291 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
292 free_dev_data(dev_data);
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100293}
294
295int __init amd_iommu_init_devices(void)
296{
297 struct pci_dev *pdev = NULL;
298 int ret = 0;
299
300 for_each_pci_dev(pdev) {
301
302 if (!check_device(&pdev->dev))
303 continue;
304
305 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200306 if (ret == -ENOTSUPP)
307 iommu_ignore_device(&pdev->dev);
308 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100309 goto out_free;
310 }
311
312 return 0;
313
314out_free:
315
316 amd_iommu_uninit_devices();
317
318 return ret;
319}
Joerg Roedel7f265082008-12-12 13:50:21 +0100320#ifdef CONFIG_AMD_IOMMU_STATS
321
322/*
323 * Initialization code for statistics collection
324 */
325
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100326DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100327DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100328DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100329DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100330DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100331DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100332DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100333DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100334DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100335DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100336DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100337DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100338
Joerg Roedel7f265082008-12-12 13:50:21 +0100339static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100340static struct dentry *de_fflush;
341
342static void amd_iommu_stats_add(struct __iommu_counter *cnt)
343{
344 if (stats_dir == NULL)
345 return;
346
347 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
348 &cnt->value);
349}
350
351static void amd_iommu_stats_init(void)
352{
353 stats_dir = debugfs_create_dir("amd-iommu", NULL);
354 if (stats_dir == NULL)
355 return;
356
Joerg Roedel7f265082008-12-12 13:50:21 +0100357 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
358 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100359
360 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100361 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100362 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100363 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100364 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100365 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100366 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100367 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100368 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100369 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100370 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100371 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100372}
373
374#endif
375
Joerg Roedel431b2a22008-07-11 17:14:22 +0200376/****************************************************************************
377 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200378 * Interrupt handling functions
379 *
380 ****************************************************************************/
381
Joerg Roedele3e59872009-09-03 14:02:10 +0200382static void dump_dte_entry(u16 devid)
383{
384 int i;
385
386 for (i = 0; i < 8; ++i)
387 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
388 amd_iommu_dev_table[devid].data[i]);
389}
390
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200391static void dump_command(unsigned long phys_addr)
392{
393 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
394 int i;
395
396 for (i = 0; i < 4; ++i)
397 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
398}
399
Joerg Roedela345b232009-09-03 15:01:43 +0200400static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200401{
402 u32 *event = __evt;
403 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
404 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
405 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
406 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
407 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
408
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200409 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200410
411 switch (type) {
412 case EVENT_TYPE_ILL_DEV:
413 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
414 "address=0x%016llx flags=0x%04x]\n",
415 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
416 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200417 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200418 break;
419 case EVENT_TYPE_IO_FAULT:
420 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
421 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
422 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
423 domid, address, flags);
424 break;
425 case EVENT_TYPE_DEV_TAB_ERR:
426 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
427 "address=0x%016llx flags=0x%04x]\n",
428 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
429 address, flags);
430 break;
431 case EVENT_TYPE_PAGE_TAB_ERR:
432 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
433 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
434 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
435 domid, address, flags);
436 break;
437 case EVENT_TYPE_ILL_CMD:
438 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200439 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200440 break;
441 case EVENT_TYPE_CMD_HARD_ERR:
442 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
443 "flags=0x%04x]\n", address, flags);
444 break;
445 case EVENT_TYPE_IOTLB_INV_TO:
446 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
447 "address=0x%016llx]\n",
448 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
449 address);
450 break;
451 case EVENT_TYPE_INV_DEV_REQ:
452 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
453 "address=0x%016llx flags=0x%04x]\n",
454 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
455 address, flags);
456 break;
457 default:
458 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
459 }
460}
461
462static void iommu_poll_events(struct amd_iommu *iommu)
463{
464 u32 head, tail;
465 unsigned long flags;
466
467 spin_lock_irqsave(&iommu->lock, flags);
468
469 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
470 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
471
472 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200473 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200474 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
475 }
476
477 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
478
479 spin_unlock_irqrestore(&iommu->lock, flags);
480}
481
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200482irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200483{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200484 struct amd_iommu *iommu;
485
Joerg Roedel3bd22172009-05-04 15:06:20 +0200486 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200487 iommu_poll_events(iommu);
488
489 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200490}
491
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200492irqreturn_t amd_iommu_int_handler(int irq, void *data)
493{
494 return IRQ_WAKE_THREAD;
495}
496
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200497/****************************************************************************
498 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200499 * IOMMU command queuing functions
500 *
501 ****************************************************************************/
502
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200503static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200504{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200505 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200506
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200507 while (*sem == 0 && i < LOOP_TIMEOUT) {
508 udelay(1);
509 i += 1;
510 }
511
512 if (i == LOOP_TIMEOUT) {
513 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
514 return -EIO;
515 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200516
517 return 0;
518}
519
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200520static void copy_cmd_to_buffer(struct amd_iommu *iommu,
521 struct iommu_cmd *cmd,
522 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200523{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200524 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200525
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200526 target = iommu->cmd_buf + tail;
527 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200528
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200529 /* Copy command to buffer */
530 memcpy(target, cmd, sizeof(*cmd));
531
532 /* Tell the IOMMU about it */
533 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
534}
535
Joerg Roedel815b33f2011-04-06 17:26:49 +0200536static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200537{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200538 WARN_ON(address & 0x7ULL);
539
Joerg Roedelded46732011-04-06 10:53:48 +0200540 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200541 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
542 cmd->data[1] = upper_32_bits(__pa(address));
543 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200544 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
545}
546
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200547static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
548{
549 memset(cmd, 0, sizeof(*cmd));
550 cmd->data[0] = devid;
551 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
552}
553
Joerg Roedel11b64022011-04-06 11:49:28 +0200554static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
555 size_t size, u16 domid, int pde)
556{
557 u64 pages;
558 int s;
559
560 pages = iommu_num_pages(address, size, PAGE_SIZE);
561 s = 0;
562
563 if (pages > 1) {
564 /*
565 * If we have to flush more than one page, flush all
566 * TLB entries for this domain
567 */
568 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
569 s = 1;
570 }
571
572 address &= PAGE_MASK;
573
574 memset(cmd, 0, sizeof(*cmd));
575 cmd->data[1] |= domid;
576 cmd->data[2] = lower_32_bits(address);
577 cmd->data[3] = upper_32_bits(address);
578 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
579 if (s) /* size bit - we flush more than one 4kb page */
580 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
581 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
582 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
583}
584
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200585static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
586 u64 address, size_t size)
587{
588 u64 pages;
589 int s;
590
591 pages = iommu_num_pages(address, size, PAGE_SIZE);
592 s = 0;
593
594 if (pages > 1) {
595 /*
596 * If we have to flush more than one page, flush all
597 * TLB entries for this domain
598 */
599 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
600 s = 1;
601 }
602
603 address &= PAGE_MASK;
604
605 memset(cmd, 0, sizeof(*cmd));
606 cmd->data[0] = devid;
607 cmd->data[0] |= (qdep & 0xff) << 24;
608 cmd->data[1] = devid;
609 cmd->data[2] = lower_32_bits(address);
610 cmd->data[3] = upper_32_bits(address);
611 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
612 if (s)
613 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
614}
615
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200616static void build_inv_all(struct iommu_cmd *cmd)
617{
618 memset(cmd, 0, sizeof(*cmd));
619 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200620}
621
Joerg Roedel431b2a22008-07-11 17:14:22 +0200622/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200623 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200624 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200625 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200626static int iommu_queue_command_sync(struct amd_iommu *iommu,
627 struct iommu_cmd *cmd,
628 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200629{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200630 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200631 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200632
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200633 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100634
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200635again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200636 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200637
638 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
639 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
640 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
641 left = (head - next_tail) % iommu->cmd_buf_size;
642
643 if (left <= 2) {
644 struct iommu_cmd sync_cmd;
645 volatile u64 sem = 0;
646 int ret;
647
648 build_completion_wait(&sync_cmd, (u64)&sem);
649 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
650
651 spin_unlock_irqrestore(&iommu->lock, flags);
652
653 if ((ret = wait_on_sem(&sem)) != 0)
654 return ret;
655
656 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200657 }
658
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200659 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200660
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200661 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200662 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200663
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200664 spin_unlock_irqrestore(&iommu->lock, flags);
665
Joerg Roedel815b33f2011-04-06 17:26:49 +0200666 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100667}
668
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200669static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
670{
671 return iommu_queue_command_sync(iommu, cmd, true);
672}
673
Joerg Roedel8d201962008-12-02 20:34:41 +0100674/*
675 * This function queues a completion wait command into the command
676 * buffer of an IOMMU
677 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100678static int iommu_completion_wait(struct amd_iommu *iommu)
679{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200680 struct iommu_cmd cmd;
681 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200682 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100683
684 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200685 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100686
Joerg Roedel815b33f2011-04-06 17:26:49 +0200687 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100688
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200689 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100690 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200691 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100692
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200693 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200694}
695
Joerg Roedeld8c13082011-04-06 18:51:26 +0200696static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200697{
698 struct iommu_cmd cmd;
699
Joerg Roedeld8c13082011-04-06 18:51:26 +0200700 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200701
Joerg Roedeld8c13082011-04-06 18:51:26 +0200702 return iommu_queue_command(iommu, &cmd);
703}
704
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200705static void iommu_flush_dte_all(struct amd_iommu *iommu)
706{
707 u32 devid;
708
709 for (devid = 0; devid <= 0xffff; ++devid)
710 iommu_flush_dte(iommu, devid);
711
712 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200713}
714
715/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200716 * This function uses heavy locking and may disable irqs for some time. But
717 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200718 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200719static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200720{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200721 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200722
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200723 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
724 struct iommu_cmd cmd;
725 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
726 dom_id, 1);
727 iommu_queue_command(iommu, &cmd);
728 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200729
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200730 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200731}
732
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200733static void iommu_flush_all(struct amd_iommu *iommu)
734{
735 struct iommu_cmd cmd;
736
737 build_inv_all(&cmd);
738
739 iommu_queue_command(iommu, &cmd);
740 iommu_completion_wait(iommu);
741}
742
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200743void iommu_flush_all_caches(struct amd_iommu *iommu)
744{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200745 if (iommu_feature(iommu, FEATURE_IA)) {
746 iommu_flush_all(iommu);
747 } else {
748 iommu_flush_dte_all(iommu);
749 iommu_flush_tlb_all(iommu);
750 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200751}
752
Joerg Roedel431b2a22008-07-11 17:14:22 +0200753/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200754 * Command send function for flushing on-device TLB
755 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200756static int device_flush_iotlb(struct iommu_dev_data *dev_data,
757 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200758{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200759 struct amd_iommu *iommu;
760 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200761 int qdep;
762
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200763 qdep = dev_data->ats.qdep;
764 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200765
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200766 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200767
768 return iommu_queue_command(iommu, &cmd);
769}
770
771/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200772 * Command send function for invalidating a device table entry
773 */
Joerg Roedel6c542042011-06-09 17:07:31 +0200774static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100775{
776 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200777 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100778
Joerg Roedel6c542042011-06-09 17:07:31 +0200779 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel3fa43652009-11-26 15:04:38 +0100780
Joerg Roedelf62dda62011-06-09 12:55:35 +0200781 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200782 if (ret)
783 return ret;
784
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200785 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +0200786 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200787
788 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100789}
790
Joerg Roedel431b2a22008-07-11 17:14:22 +0200791/*
792 * TLB invalidation function which is called from the mapping functions.
793 * It invalidates a single PTE if the range to flush is within a single
794 * page. Otherwise it flushes the whole TLB of the IOMMU.
795 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200796static void __domain_flush_pages(struct protection_domain *domain,
797 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200798{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200799 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200800 struct iommu_cmd cmd;
801 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200802
Joerg Roedel11b64022011-04-06 11:49:28 +0200803 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200804
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100805 for (i = 0; i < amd_iommus_present; ++i) {
806 if (!domain->dev_iommu[i])
807 continue;
808
809 /*
810 * Devices of this domain are behind this IOMMU
811 * We need a TLB flush
812 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200813 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100814 }
815
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200816 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200817
Joerg Roedelea61cdd2011-06-09 12:56:30 +0200818 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200819 continue;
820
Joerg Roedel6c542042011-06-09 17:07:31 +0200821 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200822 }
823
Joerg Roedel11b64022011-04-06 11:49:28 +0200824 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100825}
826
Joerg Roedel17b124b2011-04-06 18:01:35 +0200827static void domain_flush_pages(struct protection_domain *domain,
828 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100829{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200830 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200831}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200832
Joerg Roedel1c655772008-09-04 18:40:05 +0200833/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200834static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200835{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200836 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200837}
838
Chris Wright42a49f92009-06-15 15:42:00 +0200839/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200840static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200841{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200842 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
843}
844
845static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200846{
847 int i;
848
849 for (i = 0; i < amd_iommus_present; ++i) {
850 if (!domain->dev_iommu[i])
851 continue;
852
853 /*
854 * Devices of this domain are behind this IOMMU
855 * We need to wait for completion of all commands.
856 */
857 iommu_completion_wait(amd_iommus[i]);
858 }
859}
860
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100861
Joerg Roedel43f49602008-12-02 21:01:12 +0100862/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100863 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100864 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200865static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200866{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100867 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100868
869 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +0200870 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100871}
872
Joerg Roedel431b2a22008-07-11 17:14:22 +0200873/****************************************************************************
874 *
875 * The functions below are used the create the page table mappings for
876 * unity mapped regions.
877 *
878 ****************************************************************************/
879
880/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100881 * This function is used to add another level to an IO page table. Adding
882 * another level increases the size of the address space by 9 bits to a size up
883 * to 64 bits.
884 */
885static bool increase_address_space(struct protection_domain *domain,
886 gfp_t gfp)
887{
888 u64 *pte;
889
890 if (domain->mode == PAGE_MODE_6_LEVEL)
891 /* address space already 64 bit large */
892 return false;
893
894 pte = (void *)get_zeroed_page(gfp);
895 if (!pte)
896 return false;
897
898 *pte = PM_LEVEL_PDE(domain->mode,
899 virt_to_phys(domain->pt_root));
900 domain->pt_root = pte;
901 domain->mode += 1;
902 domain->updated = true;
903
904 return true;
905}
906
907static u64 *alloc_pte(struct protection_domain *domain,
908 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100909 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100910 u64 **pte_page,
911 gfp_t gfp)
912{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100913 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100914 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100915
916 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100917
918 while (address > PM_LEVEL_SIZE(domain->mode))
919 increase_address_space(domain, gfp);
920
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100921 level = domain->mode - 1;
922 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
923 address = PAGE_SIZE_ALIGN(address, page_size);
924 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100925
926 while (level > end_lvl) {
927 if (!IOMMU_PTE_PRESENT(*pte)) {
928 page = (u64 *)get_zeroed_page(gfp);
929 if (!page)
930 return NULL;
931 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
932 }
933
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100934 /* No level skipping support yet */
935 if (PM_PTE_LEVEL(*pte) != level)
936 return NULL;
937
Joerg Roedel308973d2009-11-24 17:43:32 +0100938 level -= 1;
939
940 pte = IOMMU_PTE_PAGE(*pte);
941
942 if (pte_page && level == end_lvl)
943 *pte_page = pte;
944
945 pte = &pte[PM_LEVEL_INDEX(level, address)];
946 }
947
948 return pte;
949}
950
951/*
952 * This function checks if there is a PTE for a given dma address. If
953 * there is one, it returns the pointer to it.
954 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100955static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100956{
957 int level;
958 u64 *pte;
959
Joerg Roedel24cd7722010-01-19 17:27:39 +0100960 if (address > PM_LEVEL_SIZE(domain->mode))
961 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100962
Joerg Roedel24cd7722010-01-19 17:27:39 +0100963 level = domain->mode - 1;
964 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
965
966 while (level > 0) {
967
968 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100969 if (!IOMMU_PTE_PRESENT(*pte))
970 return NULL;
971
Joerg Roedel24cd7722010-01-19 17:27:39 +0100972 /* Large PTE */
973 if (PM_PTE_LEVEL(*pte) == 0x07) {
974 unsigned long pte_mask, __pte;
975
976 /*
977 * If we have a series of large PTEs, make
978 * sure to return a pointer to the first one.
979 */
980 pte_mask = PTE_PAGE_SIZE(*pte);
981 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
982 __pte = ((unsigned long)pte) & pte_mask;
983
984 return (u64 *)__pte;
985 }
986
987 /* No level skipping support yet */
988 if (PM_PTE_LEVEL(*pte) != level)
989 return NULL;
990
Joerg Roedel308973d2009-11-24 17:43:32 +0100991 level -= 1;
992
Joerg Roedel24cd7722010-01-19 17:27:39 +0100993 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100994 pte = IOMMU_PTE_PAGE(*pte);
995 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100996 }
997
998 return pte;
999}
1000
1001/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001002 * Generic mapping functions. It maps a physical address into a DMA
1003 * address space. It allocates the page table pages if necessary.
1004 * In the future it can be extended to a generic mapping function
1005 * supporting all features of AMD IOMMU page tables like level skipping
1006 * and full 64 bit address spaces.
1007 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001008static int iommu_map_page(struct protection_domain *dom,
1009 unsigned long bus_addr,
1010 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001011 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001012 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001013{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001014 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001015 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001016
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001017 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001018 return -EINVAL;
1019
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001020 bus_addr = PAGE_ALIGN(bus_addr);
1021 phys_addr = PAGE_ALIGN(phys_addr);
1022 count = PAGE_SIZE_PTE_COUNT(page_size);
1023 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001024
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001025 for (i = 0; i < count; ++i)
1026 if (IOMMU_PTE_PRESENT(pte[i]))
1027 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001028
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001029 if (page_size > PAGE_SIZE) {
1030 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1031 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1032 } else
1033 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1034
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001035 if (prot & IOMMU_PROT_IR)
1036 __pte |= IOMMU_PTE_IR;
1037 if (prot & IOMMU_PROT_IW)
1038 __pte |= IOMMU_PTE_IW;
1039
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001040 for (i = 0; i < count; ++i)
1041 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001042
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001043 update_domain(dom);
1044
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001045 return 0;
1046}
1047
Joerg Roedel24cd7722010-01-19 17:27:39 +01001048static unsigned long iommu_unmap_page(struct protection_domain *dom,
1049 unsigned long bus_addr,
1050 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001051{
Joerg Roedel24cd7722010-01-19 17:27:39 +01001052 unsigned long long unmap_size, unmapped;
1053 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001054
Joerg Roedel24cd7722010-01-19 17:27:39 +01001055 BUG_ON(!is_power_of_2(page_size));
1056
1057 unmapped = 0;
1058
1059 while (unmapped < page_size) {
1060
1061 pte = fetch_pte(dom, bus_addr);
1062
1063 if (!pte) {
1064 /*
1065 * No PTE for this address
1066 * move forward in 4kb steps
1067 */
1068 unmap_size = PAGE_SIZE;
1069 } else if (PM_PTE_LEVEL(*pte) == 0) {
1070 /* 4kb PTE found for this address */
1071 unmap_size = PAGE_SIZE;
1072 *pte = 0ULL;
1073 } else {
1074 int count, i;
1075
1076 /* Large PTE found which maps this address */
1077 unmap_size = PTE_PAGE_SIZE(*pte);
1078 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1079 for (i = 0; i < count; i++)
1080 pte[i] = 0ULL;
1081 }
1082
1083 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1084 unmapped += unmap_size;
1085 }
1086
1087 BUG_ON(!is_power_of_2(unmapped));
1088
1089 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001090}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001091
Joerg Roedel431b2a22008-07-11 17:14:22 +02001092/*
1093 * This function checks if a specific unity mapping entry is needed for
1094 * this specific IOMMU.
1095 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001096static int iommu_for_unity_map(struct amd_iommu *iommu,
1097 struct unity_map_entry *entry)
1098{
1099 u16 bdf, i;
1100
1101 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1102 bdf = amd_iommu_alias_table[i];
1103 if (amd_iommu_rlookup_table[bdf] == iommu)
1104 return 1;
1105 }
1106
1107 return 0;
1108}
1109
Joerg Roedel431b2a22008-07-11 17:14:22 +02001110/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001111 * This function actually applies the mapping to the page table of the
1112 * dma_ops domain.
1113 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001114static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1115 struct unity_map_entry *e)
1116{
1117 u64 addr;
1118 int ret;
1119
1120 for (addr = e->address_start; addr < e->address_end;
1121 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001122 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001123 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001124 if (ret)
1125 return ret;
1126 /*
1127 * if unity mapping is in aperture range mark the page
1128 * as allocated in the aperture
1129 */
1130 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001131 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001132 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001133 }
1134
1135 return 0;
1136}
1137
Joerg Roedel431b2a22008-07-11 17:14:22 +02001138/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001139 * Init the unity mappings for a specific IOMMU in the system
1140 *
1141 * Basically iterates over all unity mapping entries and applies them to
1142 * the default domain DMA of that IOMMU if necessary.
1143 */
1144static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1145{
1146 struct unity_map_entry *entry;
1147 int ret;
1148
1149 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1150 if (!iommu_for_unity_map(iommu, entry))
1151 continue;
1152 ret = dma_ops_unity_map(iommu->default_dom, entry);
1153 if (ret)
1154 return ret;
1155 }
1156
1157 return 0;
1158}
1159
1160/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001161 * Inits the unity mappings required for a specific device
1162 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001163static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1164 u16 devid)
1165{
1166 struct unity_map_entry *e;
1167 int ret;
1168
1169 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1170 if (!(devid >= e->devid_start && devid <= e->devid_end))
1171 continue;
1172 ret = dma_ops_unity_map(dma_dom, e);
1173 if (ret)
1174 return ret;
1175 }
1176
1177 return 0;
1178}
1179
Joerg Roedel431b2a22008-07-11 17:14:22 +02001180/****************************************************************************
1181 *
1182 * The next functions belong to the address allocator for the dma_ops
1183 * interface functions. They work like the allocators in the other IOMMU
1184 * drivers. Its basically a bitmap which marks the allocated pages in
1185 * the aperture. Maybe it could be enhanced in the future to a more
1186 * efficient allocator.
1187 *
1188 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001189
Joerg Roedel431b2a22008-07-11 17:14:22 +02001190/*
Joerg Roedel384de722009-05-15 12:30:05 +02001191 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001192 *
1193 * called with domain->lock held
1194 */
Joerg Roedel384de722009-05-15 12:30:05 +02001195
Joerg Roedel9cabe892009-05-18 16:38:55 +02001196/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001197 * Used to reserve address ranges in the aperture (e.g. for exclusion
1198 * ranges.
1199 */
1200static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1201 unsigned long start_page,
1202 unsigned int pages)
1203{
1204 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1205
1206 if (start_page + pages > last_page)
1207 pages = last_page - start_page;
1208
1209 for (i = start_page; i < start_page + pages; ++i) {
1210 int index = i / APERTURE_RANGE_PAGES;
1211 int page = i % APERTURE_RANGE_PAGES;
1212 __set_bit(page, dom->aperture[index]->bitmap);
1213 }
1214}
1215
1216/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001217 * This function is used to add a new aperture range to an existing
1218 * aperture in case of dma_ops domain allocation or address allocation
1219 * failure.
1220 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001221static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001222 bool populate, gfp_t gfp)
1223{
1224 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001225 struct amd_iommu *iommu;
Joerg Roedel17f5b562011-07-06 17:14:44 +02001226 unsigned long i, old_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001227
Joerg Roedelf5e97052009-05-22 12:31:53 +02001228#ifdef CONFIG_IOMMU_STRESS
1229 populate = false;
1230#endif
1231
Joerg Roedel9cabe892009-05-18 16:38:55 +02001232 if (index >= APERTURE_MAX_RANGES)
1233 return -ENOMEM;
1234
1235 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1236 if (!dma_dom->aperture[index])
1237 return -ENOMEM;
1238
1239 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1240 if (!dma_dom->aperture[index]->bitmap)
1241 goto out_free;
1242
1243 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1244
1245 if (populate) {
1246 unsigned long address = dma_dom->aperture_size;
1247 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1248 u64 *pte, *pte_page;
1249
1250 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001251 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001252 &pte_page, gfp);
1253 if (!pte)
1254 goto out_free;
1255
1256 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1257
1258 address += APERTURE_RANGE_SIZE / 64;
1259 }
1260 }
1261
Joerg Roedel17f5b562011-07-06 17:14:44 +02001262 old_size = dma_dom->aperture_size;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001263 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1264
Joerg Roedel17f5b562011-07-06 17:14:44 +02001265 /* Reserve address range used for MSI messages */
1266 if (old_size < MSI_ADDR_BASE_LO &&
1267 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1268 unsigned long spage;
1269 int pages;
1270
1271 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1272 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1273
1274 dma_ops_reserve_addresses(dma_dom, spage, pages);
1275 }
1276
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001277 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001278 for_each_iommu(iommu) {
1279 if (iommu->exclusion_start &&
1280 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1281 && iommu->exclusion_start < dma_dom->aperture_size) {
1282 unsigned long startpage;
1283 int pages = iommu_num_pages(iommu->exclusion_start,
1284 iommu->exclusion_length,
1285 PAGE_SIZE);
1286 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1287 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1288 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001289 }
1290
1291 /*
1292 * Check for areas already mapped as present in the new aperture
1293 * range and mark those pages as reserved in the allocator. Such
1294 * mappings may already exist as a result of requested unity
1295 * mappings for devices.
1296 */
1297 for (i = dma_dom->aperture[index]->offset;
1298 i < dma_dom->aperture_size;
1299 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001300 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001301 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1302 continue;
1303
Joerg Roedelfcd08612011-10-11 17:41:32 +02001304 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001305 }
1306
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001307 update_domain(&dma_dom->domain);
1308
Joerg Roedel9cabe892009-05-18 16:38:55 +02001309 return 0;
1310
1311out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001312 update_domain(&dma_dom->domain);
1313
Joerg Roedel9cabe892009-05-18 16:38:55 +02001314 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1315
1316 kfree(dma_dom->aperture[index]);
1317 dma_dom->aperture[index] = NULL;
1318
1319 return -ENOMEM;
1320}
1321
Joerg Roedel384de722009-05-15 12:30:05 +02001322static unsigned long dma_ops_area_alloc(struct device *dev,
1323 struct dma_ops_domain *dom,
1324 unsigned int pages,
1325 unsigned long align_mask,
1326 u64 dma_mask,
1327 unsigned long start)
1328{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001329 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001330 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1331 int i = start >> APERTURE_RANGE_SHIFT;
1332 unsigned long boundary_size;
1333 unsigned long address = -1;
1334 unsigned long limit;
1335
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001336 next_bit >>= PAGE_SHIFT;
1337
Joerg Roedel384de722009-05-15 12:30:05 +02001338 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1339 PAGE_SIZE) >> PAGE_SHIFT;
1340
1341 for (;i < max_index; ++i) {
1342 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1343
1344 if (dom->aperture[i]->offset >= dma_mask)
1345 break;
1346
1347 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1348 dma_mask >> PAGE_SHIFT);
1349
1350 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1351 limit, next_bit, pages, 0,
1352 boundary_size, align_mask);
1353 if (address != -1) {
1354 address = dom->aperture[i]->offset +
1355 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001356 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001357 break;
1358 }
1359
1360 next_bit = 0;
1361 }
1362
1363 return address;
1364}
1365
Joerg Roedeld3086442008-06-26 21:27:57 +02001366static unsigned long dma_ops_alloc_addresses(struct device *dev,
1367 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001368 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001369 unsigned long align_mask,
1370 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001371{
Joerg Roedeld3086442008-06-26 21:27:57 +02001372 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001373
Joerg Roedelfe16f082009-05-22 12:27:53 +02001374#ifdef CONFIG_IOMMU_STRESS
1375 dom->next_address = 0;
1376 dom->need_flush = true;
1377#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001378
Joerg Roedel384de722009-05-15 12:30:05 +02001379 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001380 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001381
Joerg Roedel1c655772008-09-04 18:40:05 +02001382 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001383 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001384 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1385 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001386 dom->need_flush = true;
1387 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001388
Joerg Roedel384de722009-05-15 12:30:05 +02001389 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001390 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001391
1392 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1393
1394 return address;
1395}
1396
Joerg Roedel431b2a22008-07-11 17:14:22 +02001397/*
1398 * The address free function.
1399 *
1400 * called with domain->lock held
1401 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001402static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1403 unsigned long address,
1404 unsigned int pages)
1405{
Joerg Roedel384de722009-05-15 12:30:05 +02001406 unsigned i = address >> APERTURE_RANGE_SHIFT;
1407 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001408
Joerg Roedel384de722009-05-15 12:30:05 +02001409 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1410
Joerg Roedel47bccd62009-05-22 12:40:54 +02001411#ifdef CONFIG_IOMMU_STRESS
1412 if (i < 4)
1413 return;
1414#endif
1415
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001416 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001417 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001418
1419 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001420
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001421 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001422
Joerg Roedeld3086442008-06-26 21:27:57 +02001423}
1424
Joerg Roedel431b2a22008-07-11 17:14:22 +02001425/****************************************************************************
1426 *
1427 * The next functions belong to the domain allocation. A domain is
1428 * allocated for every IOMMU as the default domain. If device isolation
1429 * is enabled, every device get its own domain. The most important thing
1430 * about domains is the page table mapping the DMA address space they
1431 * contain.
1432 *
1433 ****************************************************************************/
1434
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001435/*
1436 * This function adds a protection domain to the global protection domain list
1437 */
1438static void add_domain_to_list(struct protection_domain *domain)
1439{
1440 unsigned long flags;
1441
1442 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1443 list_add(&domain->list, &amd_iommu_pd_list);
1444 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1445}
1446
1447/*
1448 * This function removes a protection domain to the global
1449 * protection domain list
1450 */
1451static void del_domain_from_list(struct protection_domain *domain)
1452{
1453 unsigned long flags;
1454
1455 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1456 list_del(&domain->list);
1457 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1458}
1459
Joerg Roedelec487d12008-06-26 21:27:58 +02001460static u16 domain_id_alloc(void)
1461{
1462 unsigned long flags;
1463 int id;
1464
1465 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1466 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1467 BUG_ON(id == 0);
1468 if (id > 0 && id < MAX_DOMAIN_ID)
1469 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1470 else
1471 id = 0;
1472 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1473
1474 return id;
1475}
1476
Joerg Roedela2acfb72008-12-02 18:28:53 +01001477static void domain_id_free(int id)
1478{
1479 unsigned long flags;
1480
1481 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1482 if (id > 0 && id < MAX_DOMAIN_ID)
1483 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1484 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1485}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001486
Joerg Roedel86db2e52008-12-02 18:20:21 +01001487static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001488{
1489 int i, j;
1490 u64 *p1, *p2, *p3;
1491
Joerg Roedel86db2e52008-12-02 18:20:21 +01001492 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001493
1494 if (!p1)
1495 return;
1496
1497 for (i = 0; i < 512; ++i) {
1498 if (!IOMMU_PTE_PRESENT(p1[i]))
1499 continue;
1500
1501 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001502 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001503 if (!IOMMU_PTE_PRESENT(p2[j]))
1504 continue;
1505 p3 = IOMMU_PTE_PAGE(p2[j]);
1506 free_page((unsigned long)p3);
1507 }
1508
1509 free_page((unsigned long)p2);
1510 }
1511
1512 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001513
1514 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001515}
1516
Joerg Roedel431b2a22008-07-11 17:14:22 +02001517/*
1518 * Free a domain, only used if something went wrong in the
1519 * allocation path and we need to free an already allocated page table
1520 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001521static void dma_ops_domain_free(struct dma_ops_domain *dom)
1522{
Joerg Roedel384de722009-05-15 12:30:05 +02001523 int i;
1524
Joerg Roedelec487d12008-06-26 21:27:58 +02001525 if (!dom)
1526 return;
1527
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001528 del_domain_from_list(&dom->domain);
1529
Joerg Roedel86db2e52008-12-02 18:20:21 +01001530 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001531
Joerg Roedel384de722009-05-15 12:30:05 +02001532 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1533 if (!dom->aperture[i])
1534 continue;
1535 free_page((unsigned long)dom->aperture[i]->bitmap);
1536 kfree(dom->aperture[i]);
1537 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001538
1539 kfree(dom);
1540}
1541
Joerg Roedel431b2a22008-07-11 17:14:22 +02001542/*
1543 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001544 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001545 * structures required for the dma_ops interface
1546 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001547static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001548{
1549 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001550
1551 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1552 if (!dma_dom)
1553 return NULL;
1554
1555 spin_lock_init(&dma_dom->domain.lock);
1556
1557 dma_dom->domain.id = domain_id_alloc();
1558 if (dma_dom->domain.id == 0)
1559 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001560 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001561 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001562 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001563 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001564 dma_dom->domain.priv = dma_dom;
1565 if (!dma_dom->domain.pt_root)
1566 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001567
Joerg Roedel1c655772008-09-04 18:40:05 +02001568 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001569 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001570
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001571 add_domain_to_list(&dma_dom->domain);
1572
Joerg Roedel576175c2009-11-23 19:08:46 +01001573 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001574 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001575
Joerg Roedel431b2a22008-07-11 17:14:22 +02001576 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001577 * mark the first page as allocated so we never return 0 as
1578 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001579 */
Joerg Roedel384de722009-05-15 12:30:05 +02001580 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001581 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001582
Joerg Roedelec487d12008-06-26 21:27:58 +02001583
1584 return dma_dom;
1585
1586free_dma_dom:
1587 dma_ops_domain_free(dma_dom);
1588
1589 return NULL;
1590}
1591
Joerg Roedel431b2a22008-07-11 17:14:22 +02001592/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001593 * little helper function to check whether a given protection domain is a
1594 * dma_ops domain
1595 */
1596static bool dma_ops_domain(struct protection_domain *domain)
1597{
1598 return domain->flags & PD_DMA_OPS_MASK;
1599}
1600
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001601static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001602{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001603 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001604 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001605
Joerg Roedel38ddf412008-09-11 10:38:32 +02001606 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1607 << DEV_ENTRY_MODE_SHIFT;
1608 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001609
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001610 if (ats)
1611 flags |= DTE_FLAG_IOTLB;
1612
1613 amd_iommu_dev_table[devid].data[3] |= flags;
1614 amd_iommu_dev_table[devid].data[2] = domain->id;
1615 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1616 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001617}
1618
Joerg Roedel15898bb2009-11-24 15:39:42 +01001619static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001620{
Joerg Roedel355bf552008-12-08 12:02:41 +01001621 /* remove entry from the device table seen by the hardware */
1622 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1623 amd_iommu_dev_table[devid].data[1] = 0;
1624 amd_iommu_dev_table[devid].data[2] = 0;
1625
Joerg Roedelc5cca142009-10-09 18:31:20 +02001626 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001627}
1628
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001629static void do_attach(struct iommu_dev_data *dev_data,
1630 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001631{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001632 struct amd_iommu *iommu;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001633 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001634
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001635 iommu = amd_iommu_rlookup_table[dev_data->devid];
1636 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001637
1638 /* Update data structures */
1639 dev_data->domain = domain;
1640 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001641 set_dte_entry(dev_data->devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001642
1643 /* Do reference counting */
1644 domain->dev_iommu[iommu->index] += 1;
1645 domain->dev_cnt += 1;
1646
1647 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001648 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001649}
1650
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001651static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001652{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001653 struct amd_iommu *iommu;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001654
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001655 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelc5cca142009-10-09 18:31:20 +02001656
Joerg Roedelc4596112009-11-20 14:57:32 +01001657 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001658 dev_data->domain->dev_iommu[iommu->index] -= 1;
1659 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001660
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001661 /* Update data structures */
1662 dev_data->domain = NULL;
1663 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001664 clear_dte_entry(dev_data->devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001665
1666 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001667 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001668}
1669
1670/*
1671 * If a device is not yet associated with a domain, this function does
1672 * assigns it visible for the hardware
1673 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001674static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001675 struct protection_domain *domain)
1676{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001677 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001678
Joerg Roedel15898bb2009-11-24 15:39:42 +01001679 /* lock domain */
1680 spin_lock(&domain->lock);
1681
Joerg Roedel71f77582011-06-09 19:03:15 +02001682 if (dev_data->alias_data != NULL) {
1683 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001684
Joerg Roedel2b02b092011-06-09 17:48:39 +02001685 /* Some sanity checks */
1686 ret = -EBUSY;
1687 if (alias_data->domain != NULL &&
1688 alias_data->domain != domain)
1689 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001690
Joerg Roedel2b02b092011-06-09 17:48:39 +02001691 if (dev_data->domain != NULL &&
1692 dev_data->domain != domain)
1693 goto out_unlock;
1694
1695 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001696 if (alias_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001697 do_attach(alias_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001698
1699 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001700 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001701
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001702 if (dev_data->domain == NULL)
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001703 do_attach(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001704
Joerg Roedel24100052009-11-25 15:59:57 +01001705 atomic_inc(&dev_data->bind);
1706
Julia Lawall84fe6c12010-05-27 12:31:51 +02001707 ret = 0;
1708
1709out_unlock:
1710
Joerg Roedel355bf552008-12-08 12:02:41 +01001711 /* ready */
1712 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001713
Julia Lawall84fe6c12010-05-27 12:31:51 +02001714 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001715}
1716
1717/*
1718 * If a device is not yet associated with a domain, this function does
1719 * assigns it visible for the hardware
1720 */
1721static int attach_device(struct device *dev,
1722 struct protection_domain *domain)
1723{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001724 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001725 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001726 unsigned long flags;
1727 int ret;
1728
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001729 dev_data = get_dev_data(dev);
1730
1731 if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1732 dev_data->ats.enabled = true;
1733 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1734 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001735
Joerg Roedel15898bb2009-11-24 15:39:42 +01001736 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001737 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001738 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1739
1740 /*
1741 * We might boot into a crash-kernel here. The crashed kernel
1742 * left the caches in the IOMMU dirty. So we have to flush
1743 * here to evict all dirty stuff.
1744 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001745 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001746
1747 return ret;
1748}
1749
1750/*
1751 * Removes a device from a protection domain (unlocked)
1752 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001753static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001754{
Joerg Roedel2ca76272010-01-22 16:45:31 +01001755 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001756 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001757
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001758 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001759
Joerg Roedel2ca76272010-01-22 16:45:31 +01001760 domain = dev_data->domain;
1761
1762 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001763
Joerg Roedel71f77582011-06-09 19:03:15 +02001764 if (dev_data->alias_data != NULL) {
1765 struct iommu_dev_data *alias_data = dev_data->alias_data;
1766
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001767 if (atomic_dec_and_test(&alias_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001768 do_detach(alias_data);
Joerg Roedel24100052009-11-25 15:59:57 +01001769 }
1770
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001771 if (atomic_dec_and_test(&dev_data->bind))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001772 do_detach(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001773
Joerg Roedel2ca76272010-01-22 16:45:31 +01001774 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001775
Joerg Roedel21129f72009-09-01 11:59:42 +02001776 /*
1777 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001778 * passthrough domain if it is detached from any other domain.
1779 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001780 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001781 if (iommu_pass_through &&
1782 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001783 __attach_device(dev_data, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001784}
1785
1786/*
1787 * Removes a device from a protection domain (with devtable_lock held)
1788 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001789static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001790{
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001791 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01001792 unsigned long flags;
1793
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001794 dev_data = get_dev_data(dev);
1795
Joerg Roedel355bf552008-12-08 12:02:41 +01001796 /* lock device table */
1797 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001798 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01001799 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001800
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001801 if (dev_data->ats.enabled) {
1802 pci_disable_ats(to_pci_dev(dev));
1803 dev_data->ats.enabled = false;
1804 }
Joerg Roedel355bf552008-12-08 12:02:41 +01001805}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001806
Joerg Roedel15898bb2009-11-24 15:39:42 +01001807/*
1808 * Find out the protection domain structure for a given PCI device. This
1809 * will give us the pointer to the page table root for example.
1810 */
1811static struct protection_domain *domain_for_device(struct device *dev)
1812{
Joerg Roedel71f77582011-06-09 19:03:15 +02001813 struct iommu_dev_data *dev_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001814 struct protection_domain *dom = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001815 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001816
Joerg Roedel657cbb62009-11-23 15:26:46 +01001817 dev_data = get_dev_data(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001818
Joerg Roedel2b02b092011-06-09 17:48:39 +02001819 if (dev_data->domain)
1820 return dev_data->domain;
1821
Joerg Roedel71f77582011-06-09 19:03:15 +02001822 if (dev_data->alias_data != NULL) {
1823 struct iommu_dev_data *alias_data = dev_data->alias_data;
Joerg Roedel2b02b092011-06-09 17:48:39 +02001824
1825 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1826 if (alias_data->domain != NULL) {
1827 __attach_device(dev_data, alias_data->domain);
1828 dom = alias_data->domain;
1829 }
1830 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001831 }
1832
Joerg Roedel15898bb2009-11-24 15:39:42 +01001833 return dom;
1834}
1835
Joerg Roedele275a2a2008-12-10 18:27:25 +01001836static int device_change_notifier(struct notifier_block *nb,
1837 unsigned long action, void *data)
1838{
1839 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001840 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001841 struct protection_domain *domain;
1842 struct dma_ops_domain *dma_domain;
1843 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001844 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001845
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001846 if (!check_device(dev))
1847 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001848
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001849 devid = get_device_id(dev);
1850 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001851
1852 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001853 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001854
1855 domain = domain_for_device(dev);
1856
Joerg Roedele275a2a2008-12-10 18:27:25 +01001857 if (!domain)
1858 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001859 if (iommu_pass_through)
1860 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001861 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001862 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001863 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001864
1865 iommu_init_device(dev);
1866
1867 domain = domain_for_device(dev);
1868
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001869 /* allocate a protection domain if a device is added */
1870 dma_domain = find_protection_domain(devid);
1871 if (dma_domain)
1872 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001873 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001874 if (!dma_domain)
1875 goto out;
1876 dma_domain->target_dev = devid;
1877
1878 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1879 list_add_tail(&dma_domain->list, &iommu_pd_list);
1880 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1881
1882 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001883 case BUS_NOTIFY_DEL_DEVICE:
1884
1885 iommu_uninit_device(dev);
1886
Joerg Roedele275a2a2008-12-10 18:27:25 +01001887 default:
1888 goto out;
1889 }
1890
Joerg Roedele275a2a2008-12-10 18:27:25 +01001891 iommu_completion_wait(iommu);
1892
1893out:
1894 return 0;
1895}
1896
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301897static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001898 .notifier_call = device_change_notifier,
1899};
Joerg Roedel355bf552008-12-08 12:02:41 +01001900
Joerg Roedel8638c492009-12-10 11:12:25 +01001901void amd_iommu_init_notifier(void)
1902{
1903 bus_register_notifier(&pci_bus_type, &device_nb);
1904}
1905
Joerg Roedel431b2a22008-07-11 17:14:22 +02001906/*****************************************************************************
1907 *
1908 * The next functions belong to the dma_ops mapping/unmapping code.
1909 *
1910 *****************************************************************************/
1911
1912/*
1913 * In the dma_ops path we only have the struct device. This function
1914 * finds the corresponding IOMMU, the protection domain and the
1915 * requestor id for a given device.
1916 * If the device is not yet associated with a domain this is also done
1917 * in this function.
1918 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001919static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001920{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001921 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001922 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001923 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001924
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001925 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001926 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001927
Joerg Roedel94f6d192009-11-24 16:40:02 +01001928 domain = domain_for_device(dev);
1929 if (domain != NULL && !dma_ops_domain(domain))
1930 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001931
Joerg Roedel94f6d192009-11-24 16:40:02 +01001932 if (domain != NULL)
1933 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001934
Joerg Roedel15898bb2009-11-24 15:39:42 +01001935 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001936 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001937 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001938 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1939 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001940 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001941 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001942
Joerg Roedel94f6d192009-11-24 16:40:02 +01001943 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001944}
1945
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001946static void update_device_table(struct protection_domain *domain)
1947{
Joerg Roedel492667d2009-11-27 13:25:47 +01001948 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001949
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001950 list_for_each_entry(dev_data, &domain->dev_list, list)
1951 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001952}
1953
1954static void update_domain(struct protection_domain *domain)
1955{
1956 if (!domain->updated)
1957 return;
1958
1959 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001960
1961 domain_flush_devices(domain);
1962 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001963
1964 domain->updated = false;
1965}
1966
Joerg Roedel431b2a22008-07-11 17:14:22 +02001967/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001968 * This function fetches the PTE for a given address in the aperture
1969 */
1970static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1971 unsigned long address)
1972{
Joerg Roedel384de722009-05-15 12:30:05 +02001973 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001974 u64 *pte, *pte_page;
1975
Joerg Roedel384de722009-05-15 12:30:05 +02001976 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1977 if (!aperture)
1978 return NULL;
1979
1980 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001981 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001982 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001983 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001984 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1985 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001986 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001987
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001988 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001989
1990 return pte;
1991}
1992
1993/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001994 * This is the generic map function. It maps one 4kb page at paddr to
1995 * the given address in the DMA address space for the domain.
1996 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001997static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001998 unsigned long address,
1999 phys_addr_t paddr,
2000 int direction)
2001{
2002 u64 *pte, __pte;
2003
2004 WARN_ON(address > dom->aperture_size);
2005
2006 paddr &= PAGE_MASK;
2007
Joerg Roedel8bda3092009-05-12 12:02:46 +02002008 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02002009 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002010 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002011
2012 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2013
2014 if (direction == DMA_TO_DEVICE)
2015 __pte |= IOMMU_PTE_IR;
2016 else if (direction == DMA_FROM_DEVICE)
2017 __pte |= IOMMU_PTE_IW;
2018 else if (direction == DMA_BIDIRECTIONAL)
2019 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2020
2021 WARN_ON(*pte);
2022
2023 *pte = __pte;
2024
2025 return (dma_addr_t)address;
2026}
2027
Joerg Roedel431b2a22008-07-11 17:14:22 +02002028/*
2029 * The generic unmapping function for on page in the DMA address space.
2030 */
Joerg Roedel680525e2009-11-23 18:44:42 +01002031static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002032 unsigned long address)
2033{
Joerg Roedel384de722009-05-15 12:30:05 +02002034 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002035 u64 *pte;
2036
2037 if (address >= dom->aperture_size)
2038 return;
2039
Joerg Roedel384de722009-05-15 12:30:05 +02002040 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2041 if (!aperture)
2042 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002043
Joerg Roedel384de722009-05-15 12:30:05 +02002044 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2045 if (!pte)
2046 return;
2047
Joerg Roedel8c8c1432009-09-02 17:30:00 +02002048 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002049
2050 WARN_ON(!*pte);
2051
2052 *pte = 0ULL;
2053}
2054
Joerg Roedel431b2a22008-07-11 17:14:22 +02002055/*
2056 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002057 * contiguous memory region into DMA address space. It is used by all
2058 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002059 * Must be called with the domain lock held.
2060 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002061static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002062 struct dma_ops_domain *dma_dom,
2063 phys_addr_t paddr,
2064 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002065 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002066 bool align,
2067 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002068{
2069 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002070 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002071 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002072 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002073 int i;
2074
Joerg Roedele3c449f2008-10-15 22:02:11 -07002075 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002076 paddr &= PAGE_MASK;
2077
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01002078 INC_STATS_COUNTER(total_map_requests);
2079
Joerg Roedelc1858972008-12-12 15:42:39 +01002080 if (pages > 1)
2081 INC_STATS_COUNTER(cross_page);
2082
Joerg Roedel6d4f3432008-09-04 19:18:02 +02002083 if (align)
2084 align_mask = (1UL << get_order(size)) - 1;
2085
Joerg Roedel11b83882009-05-19 10:23:15 +02002086retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002087 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2088 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002089 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002090 /*
2091 * setting next_address here will let the address
2092 * allocator only scan the new allocated range in the
2093 * first run. This is a small optimization.
2094 */
2095 dma_dom->next_address = dma_dom->aperture_size;
2096
Joerg Roedel576175c2009-11-23 19:08:46 +01002097 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002098 goto out;
2099
2100 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002101 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002102 * allocation again
2103 */
2104 goto retry;
2105 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002106
2107 start = address;
2108 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002109 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002110 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002111 goto out_unmap;
2112
Joerg Roedelcb76c322008-06-26 21:28:00 +02002113 paddr += PAGE_SIZE;
2114 start += PAGE_SIZE;
2115 }
2116 address += offset;
2117
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002118 ADD_STATS_COUNTER(alloced_io_mem, size);
2119
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002120 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002121 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002122 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002123 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002124 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002125
Joerg Roedelcb76c322008-06-26 21:28:00 +02002126out:
2127 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002128
2129out_unmap:
2130
2131 for (--i; i >= 0; --i) {
2132 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002133 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002134 }
2135
2136 dma_ops_free_addresses(dma_dom, address, pages);
2137
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002138 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002139}
2140
Joerg Roedel431b2a22008-07-11 17:14:22 +02002141/*
2142 * Does the reverse of the __map_single function. Must be called with
2143 * the domain lock held too
2144 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002145static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002146 dma_addr_t dma_addr,
2147 size_t size,
2148 int dir)
2149{
Joerg Roedel04e04632010-09-23 16:12:48 +02002150 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002151 dma_addr_t i, start;
2152 unsigned int pages;
2153
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002154 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002155 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002156 return;
2157
Joerg Roedel04e04632010-09-23 16:12:48 +02002158 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002159 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002160 dma_addr &= PAGE_MASK;
2161 start = dma_addr;
2162
2163 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002164 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002165 start += PAGE_SIZE;
2166 }
2167
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002168 SUB_STATS_COUNTER(alloced_io_mem, size);
2169
Joerg Roedelcb76c322008-06-26 21:28:00 +02002170 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002171
Joerg Roedel80be3082008-11-06 14:59:05 +01002172 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002173 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002174 dma_dom->need_flush = false;
2175 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002176}
2177
Joerg Roedel431b2a22008-07-11 17:14:22 +02002178/*
2179 * The exported map_single function for dma_ops.
2180 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002181static dma_addr_t map_page(struct device *dev, struct page *page,
2182 unsigned long offset, size_t size,
2183 enum dma_data_direction dir,
2184 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002185{
2186 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002187 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002188 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002189 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002190 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002191
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002192 INC_STATS_COUNTER(cnt_map_single);
2193
Joerg Roedel94f6d192009-11-24 16:40:02 +01002194 domain = get_domain(dev);
2195 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002196 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002197 else if (IS_ERR(domain))
2198 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002199
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002200 dma_mask = *dev->dma_mask;
2201
Joerg Roedel4da70b92008-06-26 21:28:01 +02002202 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002203
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002204 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002205 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002206 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002207 goto out;
2208
Joerg Roedel17b124b2011-04-06 18:01:35 +02002209 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002210
2211out:
2212 spin_unlock_irqrestore(&domain->lock, flags);
2213
2214 return addr;
2215}
2216
Joerg Roedel431b2a22008-07-11 17:14:22 +02002217/*
2218 * The exported unmap_single function for dma_ops.
2219 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002220static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2221 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002222{
2223 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002224 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002225
Joerg Roedel146a6912008-12-12 15:07:12 +01002226 INC_STATS_COUNTER(cnt_unmap_single);
2227
Joerg Roedel94f6d192009-11-24 16:40:02 +01002228 domain = get_domain(dev);
2229 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002230 return;
2231
Joerg Roedel4da70b92008-06-26 21:28:01 +02002232 spin_lock_irqsave(&domain->lock, flags);
2233
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002234 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002235
Joerg Roedel17b124b2011-04-06 18:01:35 +02002236 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002237
2238 spin_unlock_irqrestore(&domain->lock, flags);
2239}
2240
Joerg Roedel431b2a22008-07-11 17:14:22 +02002241/*
2242 * This is a special map_sg function which is used if we should map a
2243 * device which is not handled by an AMD IOMMU in the system.
2244 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002245static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2246 int nelems, int dir)
2247{
2248 struct scatterlist *s;
2249 int i;
2250
2251 for_each_sg(sglist, s, nelems, i) {
2252 s->dma_address = (dma_addr_t)sg_phys(s);
2253 s->dma_length = s->length;
2254 }
2255
2256 return nelems;
2257}
2258
Joerg Roedel431b2a22008-07-11 17:14:22 +02002259/*
2260 * The exported map_sg function for dma_ops (handles scatter-gather
2261 * lists).
2262 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002263static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002264 int nelems, enum dma_data_direction dir,
2265 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002266{
2267 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002268 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002269 int i;
2270 struct scatterlist *s;
2271 phys_addr_t paddr;
2272 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002273 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002274
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002275 INC_STATS_COUNTER(cnt_map_sg);
2276
Joerg Roedel94f6d192009-11-24 16:40:02 +01002277 domain = get_domain(dev);
2278 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002279 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002280 else if (IS_ERR(domain))
2281 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002282
Joerg Roedel832a90c2008-09-18 15:54:23 +02002283 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002284
Joerg Roedel65b050a2008-06-26 21:28:02 +02002285 spin_lock_irqsave(&domain->lock, flags);
2286
2287 for_each_sg(sglist, s, nelems, i) {
2288 paddr = sg_phys(s);
2289
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002290 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002291 paddr, s->length, dir, false,
2292 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002293
2294 if (s->dma_address) {
2295 s->dma_length = s->length;
2296 mapped_elems++;
2297 } else
2298 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002299 }
2300
Joerg Roedel17b124b2011-04-06 18:01:35 +02002301 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002302
2303out:
2304 spin_unlock_irqrestore(&domain->lock, flags);
2305
2306 return mapped_elems;
2307unmap:
2308 for_each_sg(sglist, s, mapped_elems, i) {
2309 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002310 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002311 s->dma_length, dir);
2312 s->dma_address = s->dma_length = 0;
2313 }
2314
2315 mapped_elems = 0;
2316
2317 goto out;
2318}
2319
Joerg Roedel431b2a22008-07-11 17:14:22 +02002320/*
2321 * The exported map_sg function for dma_ops (handles scatter-gather
2322 * lists).
2323 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002324static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002325 int nelems, enum dma_data_direction dir,
2326 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002327{
2328 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002329 struct protection_domain *domain;
2330 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002331 int i;
2332
Joerg Roedel55877a62008-12-12 15:12:14 +01002333 INC_STATS_COUNTER(cnt_unmap_sg);
2334
Joerg Roedel94f6d192009-11-24 16:40:02 +01002335 domain = get_domain(dev);
2336 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002337 return;
2338
Joerg Roedel65b050a2008-06-26 21:28:02 +02002339 spin_lock_irqsave(&domain->lock, flags);
2340
2341 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002342 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002343 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002344 s->dma_address = s->dma_length = 0;
2345 }
2346
Joerg Roedel17b124b2011-04-06 18:01:35 +02002347 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002348
2349 spin_unlock_irqrestore(&domain->lock, flags);
2350}
2351
Joerg Roedel431b2a22008-07-11 17:14:22 +02002352/*
2353 * The exported alloc_coherent function for dma_ops.
2354 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002355static void *alloc_coherent(struct device *dev, size_t size,
2356 dma_addr_t *dma_addr, gfp_t flag)
2357{
2358 unsigned long flags;
2359 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002360 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002361 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002362 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002363
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002364 INC_STATS_COUNTER(cnt_alloc_coherent);
2365
Joerg Roedel94f6d192009-11-24 16:40:02 +01002366 domain = get_domain(dev);
2367 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002368 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2369 *dma_addr = __pa(virt_addr);
2370 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002371 } else if (IS_ERR(domain))
2372 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002373
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002374 dma_mask = dev->coherent_dma_mask;
2375 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2376 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002377
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002378 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2379 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302380 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002381
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002382 paddr = virt_to_phys(virt_addr);
2383
Joerg Roedel832a90c2008-09-18 15:54:23 +02002384 if (!dma_mask)
2385 dma_mask = *dev->dma_mask;
2386
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002387 spin_lock_irqsave(&domain->lock, flags);
2388
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002389 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002390 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002391
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002392 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002393 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002394 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002395 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002396
Joerg Roedel17b124b2011-04-06 18:01:35 +02002397 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002398
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002399 spin_unlock_irqrestore(&domain->lock, flags);
2400
2401 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002402
2403out_free:
2404
2405 free_pages((unsigned long)virt_addr, get_order(size));
2406
2407 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002408}
2409
Joerg Roedel431b2a22008-07-11 17:14:22 +02002410/*
2411 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002412 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002413static void free_coherent(struct device *dev, size_t size,
2414 void *virt_addr, dma_addr_t dma_addr)
2415{
2416 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002417 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002418
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002419 INC_STATS_COUNTER(cnt_free_coherent);
2420
Joerg Roedel94f6d192009-11-24 16:40:02 +01002421 domain = get_domain(dev);
2422 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002423 goto free_mem;
2424
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002425 spin_lock_irqsave(&domain->lock, flags);
2426
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002427 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002428
Joerg Roedel17b124b2011-04-06 18:01:35 +02002429 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002430
2431 spin_unlock_irqrestore(&domain->lock, flags);
2432
2433free_mem:
2434 free_pages((unsigned long)virt_addr, get_order(size));
2435}
2436
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002437/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002438 * This function is called by the DMA layer to find out if we can handle a
2439 * particular device. It is part of the dma_ops.
2440 */
2441static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2442{
Joerg Roedel420aef82009-11-23 16:14:57 +01002443 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002444}
2445
2446/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002447 * The function for pre-allocating protection domains.
2448 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002449 * If the driver core informs the DMA layer if a driver grabs a device
2450 * we don't need to preallocate the protection domains anymore.
2451 * For now we have to.
2452 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302453static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002454{
2455 struct pci_dev *dev = NULL;
2456 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002457 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002458
Chris Wrightd18c69d2010-04-02 18:27:55 -07002459 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002460
2461 /* Do we handle this device? */
2462 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002463 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002464
2465 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002466 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002467 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002468
2469 devid = get_device_id(&dev->dev);
2470
Joerg Roedel87a64d52009-11-24 17:26:43 +01002471 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002472 if (!dma_dom)
2473 continue;
2474 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002475 dma_dom->target_dev = devid;
2476
Joerg Roedel15898bb2009-11-24 15:39:42 +01002477 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002478
Joerg Roedelbd60b732008-09-11 10:24:48 +02002479 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002480 }
2481}
2482
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002483static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002484 .alloc_coherent = alloc_coherent,
2485 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002486 .map_page = map_page,
2487 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002488 .map_sg = map_sg,
2489 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002490 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002491};
2492
Joerg Roedel27c21272011-05-30 15:56:24 +02002493static unsigned device_dma_ops_init(void)
2494{
2495 struct pci_dev *pdev = NULL;
2496 unsigned unhandled = 0;
2497
2498 for_each_pci_dev(pdev) {
2499 if (!check_device(&pdev->dev)) {
2500 unhandled += 1;
2501 continue;
2502 }
2503
2504 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2505 }
2506
2507 return unhandled;
2508}
2509
Joerg Roedel431b2a22008-07-11 17:14:22 +02002510/*
2511 * The function which clues the AMD IOMMU driver into dma_ops.
2512 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002513
2514void __init amd_iommu_init_api(void)
2515{
Joerg Roedel2cc21c42011-09-06 17:56:07 +02002516 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
Joerg Roedelf5325092010-01-22 17:44:35 +01002517}
2518
Joerg Roedel6631ee92008-06-26 21:28:05 +02002519int __init amd_iommu_init_dma_ops(void)
2520{
2521 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002522 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002523
Joerg Roedel431b2a22008-07-11 17:14:22 +02002524 /*
2525 * first allocate a default protection domain for every IOMMU we
2526 * found in the system. Devices not assigned to any other
2527 * protection domain will be assigned to the default one.
2528 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002529 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002530 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002531 if (iommu->default_dom == NULL)
2532 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002533 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002534 ret = iommu_init_unity_mappings(iommu);
2535 if (ret)
2536 goto free_domains;
2537 }
2538
Joerg Roedel431b2a22008-07-11 17:14:22 +02002539 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002540 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002541 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002542 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002543
2544 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002545 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002546
Joerg Roedel431b2a22008-07-11 17:14:22 +02002547 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002548 unhandled = device_dma_ops_init();
2549 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2550 /* There are unhandled devices - initialize swiotlb for them */
2551 swiotlb = 1;
2552 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002553
Joerg Roedel7f265082008-12-12 13:50:21 +01002554 amd_iommu_stats_init();
2555
Joerg Roedel6631ee92008-06-26 21:28:05 +02002556 return 0;
2557
2558free_domains:
2559
Joerg Roedel3bd22172009-05-04 15:06:20 +02002560 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002561 if (iommu->default_dom)
2562 dma_ops_domain_free(iommu->default_dom);
2563 }
2564
2565 return ret;
2566}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002567
2568/*****************************************************************************
2569 *
2570 * The following functions belong to the exported interface of AMD IOMMU
2571 *
2572 * This interface allows access to lower level functions of the IOMMU
2573 * like protection domain handling and assignement of devices to domains
2574 * which is not possible with the dma_ops interface.
2575 *
2576 *****************************************************************************/
2577
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002578static void cleanup_domain(struct protection_domain *domain)
2579{
Joerg Roedel492667d2009-11-27 13:25:47 +01002580 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002581 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002582
2583 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2584
Joerg Roedel492667d2009-11-27 13:25:47 +01002585 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002586 __detach_device(dev_data);
Joerg Roedel492667d2009-11-27 13:25:47 +01002587 atomic_set(&dev_data->bind, 0);
2588 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002589
2590 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2591}
2592
Joerg Roedel26508152009-08-26 16:52:40 +02002593static void protection_domain_free(struct protection_domain *domain)
2594{
2595 if (!domain)
2596 return;
2597
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002598 del_domain_from_list(domain);
2599
Joerg Roedel26508152009-08-26 16:52:40 +02002600 if (domain->id)
2601 domain_id_free(domain->id);
2602
2603 kfree(domain);
2604}
2605
2606static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002607{
2608 struct protection_domain *domain;
2609
2610 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2611 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002612 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002613
2614 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002615 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002616 domain->id = domain_id_alloc();
2617 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002618 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002619 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002620
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002621 add_domain_to_list(domain);
2622
Joerg Roedel26508152009-08-26 16:52:40 +02002623 return domain;
2624
2625out_err:
2626 kfree(domain);
2627
2628 return NULL;
2629}
2630
2631static int amd_iommu_domain_init(struct iommu_domain *dom)
2632{
2633 struct protection_domain *domain;
2634
2635 domain = protection_domain_alloc();
2636 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002637 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002638
2639 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002640 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2641 if (!domain->pt_root)
2642 goto out_free;
2643
2644 dom->priv = domain;
2645
2646 return 0;
2647
2648out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002649 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002650
2651 return -ENOMEM;
2652}
2653
Joerg Roedel98383fc2008-12-02 18:34:12 +01002654static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2655{
2656 struct protection_domain *domain = dom->priv;
2657
2658 if (!domain)
2659 return;
2660
2661 if (domain->dev_cnt > 0)
2662 cleanup_domain(domain);
2663
2664 BUG_ON(domain->dev_cnt != 0);
2665
2666 free_pagetable(domain);
2667
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002668 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002669
2670 dom->priv = NULL;
2671}
2672
Joerg Roedel684f2882008-12-08 12:07:44 +01002673static void amd_iommu_detach_device(struct iommu_domain *dom,
2674 struct device *dev)
2675{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002676 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002677 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002678 u16 devid;
2679
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002680 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002681 return;
2682
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002683 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002684
Joerg Roedel657cbb62009-11-23 15:26:46 +01002685 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002686 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002687
2688 iommu = amd_iommu_rlookup_table[devid];
2689 if (!iommu)
2690 return;
2691
Joerg Roedel684f2882008-12-08 12:07:44 +01002692 iommu_completion_wait(iommu);
2693}
2694
Joerg Roedel01106062008-12-02 19:34:11 +01002695static int amd_iommu_attach_device(struct iommu_domain *dom,
2696 struct device *dev)
2697{
2698 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002699 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002700 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002701 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002702
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002703 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002704 return -EINVAL;
2705
Joerg Roedel657cbb62009-11-23 15:26:46 +01002706 dev_data = dev->archdata.iommu;
2707
Joerg Roedelf62dda62011-06-09 12:55:35 +02002708 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002709 if (!iommu)
2710 return -EINVAL;
2711
Joerg Roedel657cbb62009-11-23 15:26:46 +01002712 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002713 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002714
Joerg Roedel15898bb2009-11-24 15:39:42 +01002715 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002716
2717 iommu_completion_wait(iommu);
2718
Joerg Roedel15898bb2009-11-24 15:39:42 +01002719 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002720}
2721
Joerg Roedel468e2362010-01-21 16:37:36 +01002722static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002723 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002724{
2725 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002726 int prot = 0;
2727 int ret;
2728
2729 if (iommu_prot & IOMMU_READ)
2730 prot |= IOMMU_PROT_IR;
2731 if (iommu_prot & IOMMU_WRITE)
2732 prot |= IOMMU_PROT_IW;
2733
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002734 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002735 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002736 mutex_unlock(&domain->api_lock);
2737
Joerg Roedel795e74f72010-05-11 17:40:57 +02002738 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002739}
2740
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002741static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2742 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002743{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002744 struct protection_domain *domain = dom->priv;
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002745 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002746
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002747 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002748 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002749 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002750
Joerg Roedel17b124b2011-04-06 18:01:35 +02002751 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002752
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002753 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002754}
2755
Joerg Roedel645c4c82008-12-02 20:05:50 +01002756static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2757 unsigned long iova)
2758{
2759 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002760 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002761 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002762 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002763
Joerg Roedel24cd7722010-01-19 17:27:39 +01002764 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002765
Joerg Roedela6d41a42009-09-02 17:08:55 +02002766 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002767 return 0;
2768
Joerg Roedelf03152b2010-01-21 16:15:24 +01002769 if (PM_PTE_LEVEL(*pte) == 0)
2770 offset_mask = PAGE_SIZE - 1;
2771 else
2772 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2773
2774 __pte = *pte & PM_ADDR_MASK;
2775 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002776
2777 return paddr;
2778}
2779
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002780static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2781 unsigned long cap)
2782{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002783 switch (cap) {
2784 case IOMMU_CAP_CACHE_COHERENCY:
2785 return 1;
2786 }
2787
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002788 return 0;
2789}
2790
Joerg Roedel26961ef2008-12-03 17:00:17 +01002791static struct iommu_ops amd_iommu_ops = {
2792 .domain_init = amd_iommu_domain_init,
2793 .domain_destroy = amd_iommu_domain_destroy,
2794 .attach_dev = amd_iommu_attach_device,
2795 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002796 .map = amd_iommu_map,
2797 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002798 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002799 .domain_has_cap = amd_iommu_domain_has_cap,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02002800 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002801};
2802
Joerg Roedel0feae532009-08-26 15:26:30 +02002803/*****************************************************************************
2804 *
2805 * The next functions do a basic initialization of IOMMU for pass through
2806 * mode
2807 *
2808 * In passthrough mode the IOMMU is initialized and enabled but not used for
2809 * DMA-API translation.
2810 *
2811 *****************************************************************************/
2812
2813int __init amd_iommu_init_passthrough(void)
2814{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002815 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002816 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002817 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002818
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002819 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002820 pt_domain = protection_domain_alloc();
2821 if (!pt_domain)
2822 return -ENOMEM;
2823
2824 pt_domain->mode |= PAGE_MODE_NONE;
2825
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002826 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002827 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002828 continue;
2829
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002830 devid = get_device_id(&dev->dev);
2831
Joerg Roedel15898bb2009-11-24 15:39:42 +01002832 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002833 if (!iommu)
2834 continue;
2835
Joerg Roedel15898bb2009-11-24 15:39:42 +01002836 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002837 }
2838
2839 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2840
2841 return 0;
2842}