blob: 5aa12eaabd21728f3a3b83caa894868ecd166379 [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 Roedelb6c02712008-06-26 21:27:53 +020031#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090032#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010033#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020034#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020035
36#include "amd_iommu_proto.h"
37#include "amd_iommu_types.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020038
39#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
40
Joerg Roedel815b33f2011-04-06 17:26:49 +020041#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020042
Joerg Roedelb6c02712008-06-26 21:27:53 +020043static DEFINE_RWLOCK(amd_iommu_devtable_lock);
44
Joerg Roedelbd60b732008-09-11 10:24:48 +020045/* A list of preallocated protection domains */
46static LIST_HEAD(iommu_pd_list);
47static DEFINE_SPINLOCK(iommu_pd_list_lock);
48
Joerg Roedel0feae532009-08-26 15:26:30 +020049/*
50 * Domain for untranslated devices - only allocated
51 * if iommu=pt passed on kernel cmd line.
52 */
53static struct protection_domain *pt_domain;
54
Joerg Roedel26961ef2008-12-03 17:00:17 +010055static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010056
Joerg Roedel431b2a22008-07-11 17:14:22 +020057/*
58 * general struct to manage commands send to an IOMMU
59 */
Joerg Roedeld6449532008-07-11 17:14:28 +020060struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020061 u32 data[4];
62};
63
Joerg Roedel04bfdd82009-09-02 16:00:23 +020064static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070065
Joerg Roedel15898bb2009-11-24 15:39:42 +010066/****************************************************************************
67 *
68 * Helper functions
69 *
70 ****************************************************************************/
71
72static inline u16 get_device_id(struct device *dev)
73{
74 struct pci_dev *pdev = to_pci_dev(dev);
75
76 return calc_devid(pdev->bus->number, pdev->devfn);
77}
78
Joerg Roedel657cbb62009-11-23 15:26:46 +010079static struct iommu_dev_data *get_dev_data(struct device *dev)
80{
81 return dev->archdata.iommu;
82}
83
Joerg Roedel71c70982009-11-24 16:43:06 +010084/*
85 * In this function the list of preallocated protection domains is traversed to
86 * find the domain for a specific device
87 */
88static struct dma_ops_domain *find_protection_domain(u16 devid)
89{
90 struct dma_ops_domain *entry, *ret = NULL;
91 unsigned long flags;
92 u16 alias = amd_iommu_alias_table[devid];
93
94 if (list_empty(&iommu_pd_list))
95 return NULL;
96
97 spin_lock_irqsave(&iommu_pd_list_lock, flags);
98
99 list_for_each_entry(entry, &iommu_pd_list, list) {
100 if (entry->target_dev == devid ||
101 entry->target_dev == alias) {
102 ret = entry;
103 break;
104 }
105 }
106
107 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
108
109 return ret;
110}
111
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100112/*
113 * This function checks if the driver got a valid device from the caller to
114 * avoid dereferencing invalid pointers.
115 */
116static bool check_device(struct device *dev)
117{
118 u16 devid;
119
120 if (!dev || !dev->dma_mask)
121 return false;
122
123 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100124 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100125 return false;
126
127 devid = get_device_id(dev);
128
129 /* Out of our scope? */
130 if (devid > amd_iommu_last_bdf)
131 return false;
132
133 if (amd_iommu_rlookup_table[devid] == NULL)
134 return false;
135
136 return true;
137}
138
Joerg Roedel657cbb62009-11-23 15:26:46 +0100139static int iommu_init_device(struct device *dev)
140{
141 struct iommu_dev_data *dev_data;
142 struct pci_dev *pdev;
143 u16 devid, alias;
144
145 if (dev->archdata.iommu)
146 return 0;
147
148 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
149 if (!dev_data)
150 return -ENOMEM;
151
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100152 dev_data->dev = dev;
153
Joerg Roedel657cbb62009-11-23 15:26:46 +0100154 devid = get_device_id(dev);
155 alias = amd_iommu_alias_table[devid];
156 pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
157 if (pdev)
158 dev_data->alias = &pdev->dev;
Joerg Roedel26018872011-06-06 16:50:14 +0200159 else {
160 kfree(dev_data);
161 return -ENOTSUPP;
162 }
Joerg Roedel657cbb62009-11-23 15:26:46 +0100163
Joerg Roedel24100052009-11-25 15:59:57 +0100164 atomic_set(&dev_data->bind, 0);
165
Joerg Roedel657cbb62009-11-23 15:26:46 +0100166 dev->archdata.iommu = dev_data;
167
168
169 return 0;
170}
171
Joerg Roedel26018872011-06-06 16:50:14 +0200172static void iommu_ignore_device(struct device *dev)
173{
174 u16 devid, alias;
175
176 devid = get_device_id(dev);
177 alias = amd_iommu_alias_table[devid];
178
179 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
180 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
181
182 amd_iommu_rlookup_table[devid] = NULL;
183 amd_iommu_rlookup_table[alias] = NULL;
184}
185
Joerg Roedel657cbb62009-11-23 15:26:46 +0100186static void iommu_uninit_device(struct device *dev)
187{
188 kfree(dev->archdata.iommu);
189}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100190
191void __init amd_iommu_uninit_devices(void)
192{
193 struct pci_dev *pdev = NULL;
194
195 for_each_pci_dev(pdev) {
196
197 if (!check_device(&pdev->dev))
198 continue;
199
200 iommu_uninit_device(&pdev->dev);
201 }
202}
203
204int __init amd_iommu_init_devices(void)
205{
206 struct pci_dev *pdev = NULL;
207 int ret = 0;
208
209 for_each_pci_dev(pdev) {
210
211 if (!check_device(&pdev->dev))
212 continue;
213
214 ret = iommu_init_device(&pdev->dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200215 if (ret == -ENOTSUPP)
216 iommu_ignore_device(&pdev->dev);
217 else if (ret)
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100218 goto out_free;
219 }
220
221 return 0;
222
223out_free:
224
225 amd_iommu_uninit_devices();
226
227 return ret;
228}
Joerg Roedel7f265082008-12-12 13:50:21 +0100229#ifdef CONFIG_AMD_IOMMU_STATS
230
231/*
232 * Initialization code for statistics collection
233 */
234
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100235DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100236DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100237DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100238DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100239DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100240DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100241DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100242DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100243DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100244DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100245DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100246DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100247
Joerg Roedel7f265082008-12-12 13:50:21 +0100248static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100249static struct dentry *de_fflush;
250
251static void amd_iommu_stats_add(struct __iommu_counter *cnt)
252{
253 if (stats_dir == NULL)
254 return;
255
256 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
257 &cnt->value);
258}
259
260static void amd_iommu_stats_init(void)
261{
262 stats_dir = debugfs_create_dir("amd-iommu", NULL);
263 if (stats_dir == NULL)
264 return;
265
Joerg Roedel7f265082008-12-12 13:50:21 +0100266 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
267 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100268
269 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100270 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100271 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f067a2008-12-12 15:09:48 +0100272 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100273 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100274 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100275 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100276 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100277 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100278 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100279 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100280 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100281}
282
283#endif
284
Joerg Roedel431b2a22008-07-11 17:14:22 +0200285/****************************************************************************
286 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200287 * Interrupt handling functions
288 *
289 ****************************************************************************/
290
Joerg Roedele3e59872009-09-03 14:02:10 +0200291static void dump_dte_entry(u16 devid)
292{
293 int i;
294
295 for (i = 0; i < 8; ++i)
296 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
297 amd_iommu_dev_table[devid].data[i]);
298}
299
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200300static void dump_command(unsigned long phys_addr)
301{
302 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
303 int i;
304
305 for (i = 0; i < 4; ++i)
306 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
307}
308
Joerg Roedela345b232009-09-03 15:01:43 +0200309static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200310{
311 u32 *event = __evt;
312 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
313 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
314 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
315 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
316 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
317
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200318 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200319
320 switch (type) {
321 case EVENT_TYPE_ILL_DEV:
322 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
323 "address=0x%016llx flags=0x%04x]\n",
324 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
325 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200326 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200327 break;
328 case EVENT_TYPE_IO_FAULT:
329 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
330 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
331 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
332 domid, address, flags);
333 break;
334 case EVENT_TYPE_DEV_TAB_ERR:
335 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
336 "address=0x%016llx flags=0x%04x]\n",
337 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
338 address, flags);
339 break;
340 case EVENT_TYPE_PAGE_TAB_ERR:
341 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
342 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
343 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
344 domid, address, flags);
345 break;
346 case EVENT_TYPE_ILL_CMD:
347 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200348 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200349 break;
350 case EVENT_TYPE_CMD_HARD_ERR:
351 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
352 "flags=0x%04x]\n", address, flags);
353 break;
354 case EVENT_TYPE_IOTLB_INV_TO:
355 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
356 "address=0x%016llx]\n",
357 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
358 address);
359 break;
360 case EVENT_TYPE_INV_DEV_REQ:
361 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
362 "address=0x%016llx flags=0x%04x]\n",
363 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
364 address, flags);
365 break;
366 default:
367 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
368 }
369}
370
371static void iommu_poll_events(struct amd_iommu *iommu)
372{
373 u32 head, tail;
374 unsigned long flags;
375
376 spin_lock_irqsave(&iommu->lock, flags);
377
378 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
379 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
380
381 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200382 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200383 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
384 }
385
386 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
387
388 spin_unlock_irqrestore(&iommu->lock, flags);
389}
390
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200391irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200392{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200393 struct amd_iommu *iommu;
394
Joerg Roedel3bd22172009-05-04 15:06:20 +0200395 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200396 iommu_poll_events(iommu);
397
398 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200399}
400
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200401irqreturn_t amd_iommu_int_handler(int irq, void *data)
402{
403 return IRQ_WAKE_THREAD;
404}
405
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200406/****************************************************************************
407 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200408 * IOMMU command queuing functions
409 *
410 ****************************************************************************/
411
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200412static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200413{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200414 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200415
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200416 while (*sem == 0 && i < LOOP_TIMEOUT) {
417 udelay(1);
418 i += 1;
419 }
420
421 if (i == LOOP_TIMEOUT) {
422 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
423 return -EIO;
424 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200425
426 return 0;
427}
428
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200429static void copy_cmd_to_buffer(struct amd_iommu *iommu,
430 struct iommu_cmd *cmd,
431 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200432{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200433 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200434
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200435 target = iommu->cmd_buf + tail;
436 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200437
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200438 /* Copy command to buffer */
439 memcpy(target, cmd, sizeof(*cmd));
440
441 /* Tell the IOMMU about it */
442 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
443}
444
Joerg Roedel815b33f2011-04-06 17:26:49 +0200445static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200446{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200447 WARN_ON(address & 0x7ULL);
448
Joerg Roedelded46732011-04-06 10:53:48 +0200449 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200450 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
451 cmd->data[1] = upper_32_bits(__pa(address));
452 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200453 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
454}
455
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200456static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
457{
458 memset(cmd, 0, sizeof(*cmd));
459 cmd->data[0] = devid;
460 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
461}
462
Joerg Roedel11b64022011-04-06 11:49:28 +0200463static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
464 size_t size, u16 domid, int pde)
465{
466 u64 pages;
467 int s;
468
469 pages = iommu_num_pages(address, size, PAGE_SIZE);
470 s = 0;
471
472 if (pages > 1) {
473 /*
474 * If we have to flush more than one page, flush all
475 * TLB entries for this domain
476 */
477 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
478 s = 1;
479 }
480
481 address &= PAGE_MASK;
482
483 memset(cmd, 0, sizeof(*cmd));
484 cmd->data[1] |= domid;
485 cmd->data[2] = lower_32_bits(address);
486 cmd->data[3] = upper_32_bits(address);
487 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
488 if (s) /* size bit - we flush more than one 4kb page */
489 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
490 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
491 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
492}
493
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200494static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
495 u64 address, size_t size)
496{
497 u64 pages;
498 int s;
499
500 pages = iommu_num_pages(address, size, PAGE_SIZE);
501 s = 0;
502
503 if (pages > 1) {
504 /*
505 * If we have to flush more than one page, flush all
506 * TLB entries for this domain
507 */
508 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
509 s = 1;
510 }
511
512 address &= PAGE_MASK;
513
514 memset(cmd, 0, sizeof(*cmd));
515 cmd->data[0] = devid;
516 cmd->data[0] |= (qdep & 0xff) << 24;
517 cmd->data[1] = devid;
518 cmd->data[2] = lower_32_bits(address);
519 cmd->data[3] = upper_32_bits(address);
520 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
521 if (s)
522 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
523}
524
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200525static void build_inv_all(struct iommu_cmd *cmd)
526{
527 memset(cmd, 0, sizeof(*cmd));
528 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200529}
530
Joerg Roedel431b2a22008-07-11 17:14:22 +0200531/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200532 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200533 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200534 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200535static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200536{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200537 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200538 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200539
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200540 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100541
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200542again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200543 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200544
545 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
546 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
547 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
548 left = (head - next_tail) % iommu->cmd_buf_size;
549
550 if (left <= 2) {
551 struct iommu_cmd sync_cmd;
552 volatile u64 sem = 0;
553 int ret;
554
555 build_completion_wait(&sync_cmd, (u64)&sem);
556 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
557
558 spin_unlock_irqrestore(&iommu->lock, flags);
559
560 if ((ret = wait_on_sem(&sem)) != 0)
561 return ret;
562
563 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200564 }
565
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200566 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200567
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200568 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200569 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200570
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200571 spin_unlock_irqrestore(&iommu->lock, flags);
572
Joerg Roedel815b33f2011-04-06 17:26:49 +0200573 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100574}
575
576/*
577 * This function queues a completion wait command into the command
578 * buffer of an IOMMU
579 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100580static int iommu_completion_wait(struct amd_iommu *iommu)
581{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200582 struct iommu_cmd cmd;
583 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200584 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100585
586 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200587 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100588
Joerg Roedel815b33f2011-04-06 17:26:49 +0200589 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100590
Joerg Roedel815b33f2011-04-06 17:26:49 +0200591 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100592 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200593 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100594
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200595 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200596}
597
Joerg Roedeld8c13082011-04-06 18:51:26 +0200598static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200599{
600 struct iommu_cmd cmd;
601
Joerg Roedeld8c13082011-04-06 18:51:26 +0200602 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200603
Joerg Roedeld8c13082011-04-06 18:51:26 +0200604 return iommu_queue_command(iommu, &cmd);
605}
606
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200607static void iommu_flush_dte_all(struct amd_iommu *iommu)
608{
609 u32 devid;
610
611 for (devid = 0; devid <= 0xffff; ++devid)
612 iommu_flush_dte(iommu, devid);
613
614 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200615}
616
617/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200618 * This function uses heavy locking and may disable irqs for some time. But
619 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200620 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200621static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200622{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200623 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200624
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200625 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
626 struct iommu_cmd cmd;
627 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
628 dom_id, 1);
629 iommu_queue_command(iommu, &cmd);
630 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200631
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200632 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200633}
634
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200635static void iommu_flush_all(struct amd_iommu *iommu)
636{
637 struct iommu_cmd cmd;
638
639 build_inv_all(&cmd);
640
641 iommu_queue_command(iommu, &cmd);
642 iommu_completion_wait(iommu);
643}
644
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200645void iommu_flush_all_caches(struct amd_iommu *iommu)
646{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200647 if (iommu_feature(iommu, FEATURE_IA)) {
648 iommu_flush_all(iommu);
649 } else {
650 iommu_flush_dte_all(iommu);
651 iommu_flush_tlb_all(iommu);
652 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200653}
654
Joerg Roedel431b2a22008-07-11 17:14:22 +0200655/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200656 * Command send function for flushing on-device TLB
657 */
658static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
659{
660 struct pci_dev *pdev = to_pci_dev(dev);
661 struct amd_iommu *iommu;
662 struct iommu_cmd cmd;
663 u16 devid;
664 int qdep;
665
666 qdep = pci_ats_queue_depth(pdev);
667 devid = get_device_id(dev);
668 iommu = amd_iommu_rlookup_table[devid];
669
670 build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
671
672 return iommu_queue_command(iommu, &cmd);
673}
674
675/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200676 * Command send function for invalidating a device table entry
677 */
Joerg Roedeld8c13082011-04-06 18:51:26 +0200678static int device_flush_dte(struct device *dev)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100679{
680 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200681 struct pci_dev *pdev;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100682 u16 devid;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200683 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100684
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200685 pdev = to_pci_dev(dev);
Joerg Roedel3fa43652009-11-26 15:04:38 +0100686 devid = get_device_id(dev);
687 iommu = amd_iommu_rlookup_table[devid];
688
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200689 ret = iommu_flush_dte(iommu, devid);
690 if (ret)
691 return ret;
692
693 if (pci_ats_enabled(pdev))
694 ret = device_flush_iotlb(dev, 0, ~0UL);
695
696 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100697}
698
Joerg Roedel431b2a22008-07-11 17:14:22 +0200699/*
700 * TLB invalidation function which is called from the mapping functions.
701 * It invalidates a single PTE if the range to flush is within a single
702 * page. Otherwise it flushes the whole TLB of the IOMMU.
703 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200704static void __domain_flush_pages(struct protection_domain *domain,
705 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200706{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200707 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200708 struct iommu_cmd cmd;
709 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200710
Joerg Roedel11b64022011-04-06 11:49:28 +0200711 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200712
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100713 for (i = 0; i < amd_iommus_present; ++i) {
714 if (!domain->dev_iommu[i])
715 continue;
716
717 /*
718 * Devices of this domain are behind this IOMMU
719 * We need a TLB flush
720 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200721 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100722 }
723
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200724 list_for_each_entry(dev_data, &domain->dev_list, list) {
725 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
726
727 if (!pci_ats_enabled(pdev))
728 continue;
729
730 ret |= device_flush_iotlb(dev_data->dev, address, size);
731 }
732
Joerg Roedel11b64022011-04-06 11:49:28 +0200733 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100734}
735
Joerg Roedel17b124b2011-04-06 18:01:35 +0200736static void domain_flush_pages(struct protection_domain *domain,
737 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100738{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200739 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200740}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200741
Joerg Roedel1c655772008-09-04 18:40:05 +0200742/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200743static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200744{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200745 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200746}
747
Chris Wright42a49f92009-06-15 15:42:00 +0200748/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200749static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200750{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200751 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
752}
753
754static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200755{
756 int i;
757
758 for (i = 0; i < amd_iommus_present; ++i) {
759 if (!domain->dev_iommu[i])
760 continue;
761
762 /*
763 * Devices of this domain are behind this IOMMU
764 * We need to wait for completion of all commands.
765 */
766 iommu_completion_wait(amd_iommus[i]);
767 }
768}
769
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100770
Joerg Roedel43f49602008-12-02 21:01:12 +0100771/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100772 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100773 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200774static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200775{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100776 struct iommu_dev_data *dev_data;
777 unsigned long flags;
778
779 spin_lock_irqsave(&domain->lock, flags);
780
781 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedeld8c13082011-04-06 18:51:26 +0200782 device_flush_dte(dev_data->dev);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100783
784 spin_unlock_irqrestore(&domain->lock, flags);
785}
786
Joerg Roedel431b2a22008-07-11 17:14:22 +0200787/****************************************************************************
788 *
789 * The functions below are used the create the page table mappings for
790 * unity mapped regions.
791 *
792 ****************************************************************************/
793
794/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100795 * This function is used to add another level to an IO page table. Adding
796 * another level increases the size of the address space by 9 bits to a size up
797 * to 64 bits.
798 */
799static bool increase_address_space(struct protection_domain *domain,
800 gfp_t gfp)
801{
802 u64 *pte;
803
804 if (domain->mode == PAGE_MODE_6_LEVEL)
805 /* address space already 64 bit large */
806 return false;
807
808 pte = (void *)get_zeroed_page(gfp);
809 if (!pte)
810 return false;
811
812 *pte = PM_LEVEL_PDE(domain->mode,
813 virt_to_phys(domain->pt_root));
814 domain->pt_root = pte;
815 domain->mode += 1;
816 domain->updated = true;
817
818 return true;
819}
820
821static u64 *alloc_pte(struct protection_domain *domain,
822 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100823 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100824 u64 **pte_page,
825 gfp_t gfp)
826{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100827 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100828 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100829
830 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100831
832 while (address > PM_LEVEL_SIZE(domain->mode))
833 increase_address_space(domain, gfp);
834
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100835 level = domain->mode - 1;
836 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
837 address = PAGE_SIZE_ALIGN(address, page_size);
838 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100839
840 while (level > end_lvl) {
841 if (!IOMMU_PTE_PRESENT(*pte)) {
842 page = (u64 *)get_zeroed_page(gfp);
843 if (!page)
844 return NULL;
845 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
846 }
847
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100848 /* No level skipping support yet */
849 if (PM_PTE_LEVEL(*pte) != level)
850 return NULL;
851
Joerg Roedel308973d2009-11-24 17:43:32 +0100852 level -= 1;
853
854 pte = IOMMU_PTE_PAGE(*pte);
855
856 if (pte_page && level == end_lvl)
857 *pte_page = pte;
858
859 pte = &pte[PM_LEVEL_INDEX(level, address)];
860 }
861
862 return pte;
863}
864
865/*
866 * This function checks if there is a PTE for a given dma address. If
867 * there is one, it returns the pointer to it.
868 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100869static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100870{
871 int level;
872 u64 *pte;
873
Joerg Roedel24cd7722010-01-19 17:27:39 +0100874 if (address > PM_LEVEL_SIZE(domain->mode))
875 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100876
Joerg Roedel24cd7722010-01-19 17:27:39 +0100877 level = domain->mode - 1;
878 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
879
880 while (level > 0) {
881
882 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100883 if (!IOMMU_PTE_PRESENT(*pte))
884 return NULL;
885
Joerg Roedel24cd7722010-01-19 17:27:39 +0100886 /* Large PTE */
887 if (PM_PTE_LEVEL(*pte) == 0x07) {
888 unsigned long pte_mask, __pte;
889
890 /*
891 * If we have a series of large PTEs, make
892 * sure to return a pointer to the first one.
893 */
894 pte_mask = PTE_PAGE_SIZE(*pte);
895 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
896 __pte = ((unsigned long)pte) & pte_mask;
897
898 return (u64 *)__pte;
899 }
900
901 /* No level skipping support yet */
902 if (PM_PTE_LEVEL(*pte) != level)
903 return NULL;
904
Joerg Roedel308973d2009-11-24 17:43:32 +0100905 level -= 1;
906
Joerg Roedel24cd7722010-01-19 17:27:39 +0100907 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100908 pte = IOMMU_PTE_PAGE(*pte);
909 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100910 }
911
912 return pte;
913}
914
915/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200916 * Generic mapping functions. It maps a physical address into a DMA
917 * address space. It allocates the page table pages if necessary.
918 * In the future it can be extended to a generic mapping function
919 * supporting all features of AMD IOMMU page tables like level skipping
920 * and full 64 bit address spaces.
921 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100922static int iommu_map_page(struct protection_domain *dom,
923 unsigned long bus_addr,
924 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200925 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100926 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200927{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200928 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100929 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200930
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200931 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200932 return -EINVAL;
933
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100934 bus_addr = PAGE_ALIGN(bus_addr);
935 phys_addr = PAGE_ALIGN(phys_addr);
936 count = PAGE_SIZE_PTE_COUNT(page_size);
937 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200938
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100939 for (i = 0; i < count; ++i)
940 if (IOMMU_PTE_PRESENT(pte[i]))
941 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200942
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100943 if (page_size > PAGE_SIZE) {
944 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
945 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
946 } else
947 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
948
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200949 if (prot & IOMMU_PROT_IR)
950 __pte |= IOMMU_PTE_IR;
951 if (prot & IOMMU_PROT_IW)
952 __pte |= IOMMU_PTE_IW;
953
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100954 for (i = 0; i < count; ++i)
955 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200956
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200957 update_domain(dom);
958
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200959 return 0;
960}
961
Joerg Roedel24cd7722010-01-19 17:27:39 +0100962static unsigned long iommu_unmap_page(struct protection_domain *dom,
963 unsigned long bus_addr,
964 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100965{
Joerg Roedel24cd7722010-01-19 17:27:39 +0100966 unsigned long long unmap_size, unmapped;
967 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100968
Joerg Roedel24cd7722010-01-19 17:27:39 +0100969 BUG_ON(!is_power_of_2(page_size));
970
971 unmapped = 0;
972
973 while (unmapped < page_size) {
974
975 pte = fetch_pte(dom, bus_addr);
976
977 if (!pte) {
978 /*
979 * No PTE for this address
980 * move forward in 4kb steps
981 */
982 unmap_size = PAGE_SIZE;
983 } else if (PM_PTE_LEVEL(*pte) == 0) {
984 /* 4kb PTE found for this address */
985 unmap_size = PAGE_SIZE;
986 *pte = 0ULL;
987 } else {
988 int count, i;
989
990 /* Large PTE found which maps this address */
991 unmap_size = PTE_PAGE_SIZE(*pte);
992 count = PAGE_SIZE_PTE_COUNT(unmap_size);
993 for (i = 0; i < count; i++)
994 pte[i] = 0ULL;
995 }
996
997 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
998 unmapped += unmap_size;
999 }
1000
1001 BUG_ON(!is_power_of_2(unmapped));
1002
1003 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001004}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001005
Joerg Roedel431b2a22008-07-11 17:14:22 +02001006/*
1007 * This function checks if a specific unity mapping entry is needed for
1008 * this specific IOMMU.
1009 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001010static int iommu_for_unity_map(struct amd_iommu *iommu,
1011 struct unity_map_entry *entry)
1012{
1013 u16 bdf, i;
1014
1015 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1016 bdf = amd_iommu_alias_table[i];
1017 if (amd_iommu_rlookup_table[bdf] == iommu)
1018 return 1;
1019 }
1020
1021 return 0;
1022}
1023
Joerg Roedel431b2a22008-07-11 17:14:22 +02001024/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001025 * This function actually applies the mapping to the page table of the
1026 * dma_ops domain.
1027 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001028static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1029 struct unity_map_entry *e)
1030{
1031 u64 addr;
1032 int ret;
1033
1034 for (addr = e->address_start; addr < e->address_end;
1035 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001036 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001037 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001038 if (ret)
1039 return ret;
1040 /*
1041 * if unity mapping is in aperture range mark the page
1042 * as allocated in the aperture
1043 */
1044 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001045 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001046 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001047 }
1048
1049 return 0;
1050}
1051
Joerg Roedel431b2a22008-07-11 17:14:22 +02001052/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001053 * Init the unity mappings for a specific IOMMU in the system
1054 *
1055 * Basically iterates over all unity mapping entries and applies them to
1056 * the default domain DMA of that IOMMU if necessary.
1057 */
1058static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1059{
1060 struct unity_map_entry *entry;
1061 int ret;
1062
1063 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1064 if (!iommu_for_unity_map(iommu, entry))
1065 continue;
1066 ret = dma_ops_unity_map(iommu->default_dom, entry);
1067 if (ret)
1068 return ret;
1069 }
1070
1071 return 0;
1072}
1073
1074/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001075 * Inits the unity mappings required for a specific device
1076 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001077static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1078 u16 devid)
1079{
1080 struct unity_map_entry *e;
1081 int ret;
1082
1083 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1084 if (!(devid >= e->devid_start && devid <= e->devid_end))
1085 continue;
1086 ret = dma_ops_unity_map(dma_dom, e);
1087 if (ret)
1088 return ret;
1089 }
1090
1091 return 0;
1092}
1093
Joerg Roedel431b2a22008-07-11 17:14:22 +02001094/****************************************************************************
1095 *
1096 * The next functions belong to the address allocator for the dma_ops
1097 * interface functions. They work like the allocators in the other IOMMU
1098 * drivers. Its basically a bitmap which marks the allocated pages in
1099 * the aperture. Maybe it could be enhanced in the future to a more
1100 * efficient allocator.
1101 *
1102 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001103
Joerg Roedel431b2a22008-07-11 17:14:22 +02001104/*
Joerg Roedel384de722009-05-15 12:30:05 +02001105 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001106 *
1107 * called with domain->lock held
1108 */
Joerg Roedel384de722009-05-15 12:30:05 +02001109
Joerg Roedel9cabe892009-05-18 16:38:55 +02001110/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001111 * Used to reserve address ranges in the aperture (e.g. for exclusion
1112 * ranges.
1113 */
1114static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1115 unsigned long start_page,
1116 unsigned int pages)
1117{
1118 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1119
1120 if (start_page + pages > last_page)
1121 pages = last_page - start_page;
1122
1123 for (i = start_page; i < start_page + pages; ++i) {
1124 int index = i / APERTURE_RANGE_PAGES;
1125 int page = i % APERTURE_RANGE_PAGES;
1126 __set_bit(page, dom->aperture[index]->bitmap);
1127 }
1128}
1129
1130/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001131 * This function is used to add a new aperture range to an existing
1132 * aperture in case of dma_ops domain allocation or address allocation
1133 * failure.
1134 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001135static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001136 bool populate, gfp_t gfp)
1137{
1138 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001139 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001140 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001141
Joerg Roedelf5e97052009-05-22 12:31:53 +02001142#ifdef CONFIG_IOMMU_STRESS
1143 populate = false;
1144#endif
1145
Joerg Roedel9cabe892009-05-18 16:38:55 +02001146 if (index >= APERTURE_MAX_RANGES)
1147 return -ENOMEM;
1148
1149 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1150 if (!dma_dom->aperture[index])
1151 return -ENOMEM;
1152
1153 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1154 if (!dma_dom->aperture[index]->bitmap)
1155 goto out_free;
1156
1157 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1158
1159 if (populate) {
1160 unsigned long address = dma_dom->aperture_size;
1161 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1162 u64 *pte, *pte_page;
1163
1164 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001165 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001166 &pte_page, gfp);
1167 if (!pte)
1168 goto out_free;
1169
1170 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1171
1172 address += APERTURE_RANGE_SIZE / 64;
1173 }
1174 }
1175
1176 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1177
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001178 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001179 for_each_iommu(iommu) {
1180 if (iommu->exclusion_start &&
1181 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1182 && iommu->exclusion_start < dma_dom->aperture_size) {
1183 unsigned long startpage;
1184 int pages = iommu_num_pages(iommu->exclusion_start,
1185 iommu->exclusion_length,
1186 PAGE_SIZE);
1187 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1188 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1189 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001190 }
1191
1192 /*
1193 * Check for areas already mapped as present in the new aperture
1194 * range and mark those pages as reserved in the allocator. Such
1195 * mappings may already exist as a result of requested unity
1196 * mappings for devices.
1197 */
1198 for (i = dma_dom->aperture[index]->offset;
1199 i < dma_dom->aperture_size;
1200 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001201 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001202 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1203 continue;
1204
1205 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1206 }
1207
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001208 update_domain(&dma_dom->domain);
1209
Joerg Roedel9cabe892009-05-18 16:38:55 +02001210 return 0;
1211
1212out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001213 update_domain(&dma_dom->domain);
1214
Joerg Roedel9cabe892009-05-18 16:38:55 +02001215 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1216
1217 kfree(dma_dom->aperture[index]);
1218 dma_dom->aperture[index] = NULL;
1219
1220 return -ENOMEM;
1221}
1222
Joerg Roedel384de722009-05-15 12:30:05 +02001223static unsigned long dma_ops_area_alloc(struct device *dev,
1224 struct dma_ops_domain *dom,
1225 unsigned int pages,
1226 unsigned long align_mask,
1227 u64 dma_mask,
1228 unsigned long start)
1229{
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001230 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001231 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1232 int i = start >> APERTURE_RANGE_SHIFT;
1233 unsigned long boundary_size;
1234 unsigned long address = -1;
1235 unsigned long limit;
1236
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001237 next_bit >>= PAGE_SHIFT;
1238
Joerg Roedel384de722009-05-15 12:30:05 +02001239 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1240 PAGE_SIZE) >> PAGE_SHIFT;
1241
1242 for (;i < max_index; ++i) {
1243 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1244
1245 if (dom->aperture[i]->offset >= dma_mask)
1246 break;
1247
1248 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1249 dma_mask >> PAGE_SHIFT);
1250
1251 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1252 limit, next_bit, pages, 0,
1253 boundary_size, align_mask);
1254 if (address != -1) {
1255 address = dom->aperture[i]->offset +
1256 (address << PAGE_SHIFT);
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001257 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001258 break;
1259 }
1260
1261 next_bit = 0;
1262 }
1263
1264 return address;
1265}
1266
Joerg Roedeld3086442008-06-26 21:27:57 +02001267static unsigned long dma_ops_alloc_addresses(struct device *dev,
1268 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001269 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001270 unsigned long align_mask,
1271 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001272{
Joerg Roedeld3086442008-06-26 21:27:57 +02001273 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001274
Joerg Roedelfe16f082009-05-22 12:27:53 +02001275#ifdef CONFIG_IOMMU_STRESS
1276 dom->next_address = 0;
1277 dom->need_flush = true;
1278#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001279
Joerg Roedel384de722009-05-15 12:30:05 +02001280 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001281 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001282
Joerg Roedel1c655772008-09-04 18:40:05 +02001283 if (address == -1) {
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001284 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001285 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1286 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001287 dom->need_flush = true;
1288 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001289
Joerg Roedel384de722009-05-15 12:30:05 +02001290 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001291 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001292
1293 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1294
1295 return address;
1296}
1297
Joerg Roedel431b2a22008-07-11 17:14:22 +02001298/*
1299 * The address free function.
1300 *
1301 * called with domain->lock held
1302 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001303static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1304 unsigned long address,
1305 unsigned int pages)
1306{
Joerg Roedel384de722009-05-15 12:30:05 +02001307 unsigned i = address >> APERTURE_RANGE_SHIFT;
1308 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001309
Joerg Roedel384de722009-05-15 12:30:05 +02001310 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1311
Joerg Roedel47bccd62009-05-22 12:40:54 +02001312#ifdef CONFIG_IOMMU_STRESS
1313 if (i < 4)
1314 return;
1315#endif
1316
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001317 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001318 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001319
1320 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001321
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001322 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001323
Joerg Roedeld3086442008-06-26 21:27:57 +02001324}
1325
Joerg Roedel431b2a22008-07-11 17:14:22 +02001326/****************************************************************************
1327 *
1328 * The next functions belong to the domain allocation. A domain is
1329 * allocated for every IOMMU as the default domain. If device isolation
1330 * is enabled, every device get its own domain. The most important thing
1331 * about domains is the page table mapping the DMA address space they
1332 * contain.
1333 *
1334 ****************************************************************************/
1335
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001336/*
1337 * This function adds a protection domain to the global protection domain list
1338 */
1339static void add_domain_to_list(struct protection_domain *domain)
1340{
1341 unsigned long flags;
1342
1343 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1344 list_add(&domain->list, &amd_iommu_pd_list);
1345 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1346}
1347
1348/*
1349 * This function removes a protection domain to the global
1350 * protection domain list
1351 */
1352static void del_domain_from_list(struct protection_domain *domain)
1353{
1354 unsigned long flags;
1355
1356 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1357 list_del(&domain->list);
1358 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1359}
1360
Joerg Roedelec487d12008-06-26 21:27:58 +02001361static u16 domain_id_alloc(void)
1362{
1363 unsigned long flags;
1364 int id;
1365
1366 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1367 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1368 BUG_ON(id == 0);
1369 if (id > 0 && id < MAX_DOMAIN_ID)
1370 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1371 else
1372 id = 0;
1373 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1374
1375 return id;
1376}
1377
Joerg Roedela2acfb72008-12-02 18:28:53 +01001378static void domain_id_free(int id)
1379{
1380 unsigned long flags;
1381
1382 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1383 if (id > 0 && id < MAX_DOMAIN_ID)
1384 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1385 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1386}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001387
Joerg Roedel86db2e52008-12-02 18:20:21 +01001388static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001389{
1390 int i, j;
1391 u64 *p1, *p2, *p3;
1392
Joerg Roedel86db2e52008-12-02 18:20:21 +01001393 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001394
1395 if (!p1)
1396 return;
1397
1398 for (i = 0; i < 512; ++i) {
1399 if (!IOMMU_PTE_PRESENT(p1[i]))
1400 continue;
1401
1402 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001403 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001404 if (!IOMMU_PTE_PRESENT(p2[j]))
1405 continue;
1406 p3 = IOMMU_PTE_PAGE(p2[j]);
1407 free_page((unsigned long)p3);
1408 }
1409
1410 free_page((unsigned long)p2);
1411 }
1412
1413 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001414
1415 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001416}
1417
Joerg Roedel431b2a22008-07-11 17:14:22 +02001418/*
1419 * Free a domain, only used if something went wrong in the
1420 * allocation path and we need to free an already allocated page table
1421 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001422static void dma_ops_domain_free(struct dma_ops_domain *dom)
1423{
Joerg Roedel384de722009-05-15 12:30:05 +02001424 int i;
1425
Joerg Roedelec487d12008-06-26 21:27:58 +02001426 if (!dom)
1427 return;
1428
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001429 del_domain_from_list(&dom->domain);
1430
Joerg Roedel86db2e52008-12-02 18:20:21 +01001431 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001432
Joerg Roedel384de722009-05-15 12:30:05 +02001433 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1434 if (!dom->aperture[i])
1435 continue;
1436 free_page((unsigned long)dom->aperture[i]->bitmap);
1437 kfree(dom->aperture[i]);
1438 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001439
1440 kfree(dom);
1441}
1442
Joerg Roedel431b2a22008-07-11 17:14:22 +02001443/*
1444 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001445 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001446 * structures required for the dma_ops interface
1447 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001448static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001449{
1450 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001451
1452 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1453 if (!dma_dom)
1454 return NULL;
1455
1456 spin_lock_init(&dma_dom->domain.lock);
1457
1458 dma_dom->domain.id = domain_id_alloc();
1459 if (dma_dom->domain.id == 0)
1460 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001461 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001462 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001463 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001464 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001465 dma_dom->domain.priv = dma_dom;
1466 if (!dma_dom->domain.pt_root)
1467 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001468
Joerg Roedel1c655772008-09-04 18:40:05 +02001469 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001470 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001471
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001472 add_domain_to_list(&dma_dom->domain);
1473
Joerg Roedel576175c2009-11-23 19:08:46 +01001474 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001475 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001476
Joerg Roedel431b2a22008-07-11 17:14:22 +02001477 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001478 * mark the first page as allocated so we never return 0 as
1479 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001480 */
Joerg Roedel384de722009-05-15 12:30:05 +02001481 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb42009-05-18 15:32:48 +02001482 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001483
Joerg Roedelec487d12008-06-26 21:27:58 +02001484
1485 return dma_dom;
1486
1487free_dma_dom:
1488 dma_ops_domain_free(dma_dom);
1489
1490 return NULL;
1491}
1492
Joerg Roedel431b2a22008-07-11 17:14:22 +02001493/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001494 * little helper function to check whether a given protection domain is a
1495 * dma_ops domain
1496 */
1497static bool dma_ops_domain(struct protection_domain *domain)
1498{
1499 return domain->flags & PD_DMA_OPS_MASK;
1500}
1501
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001502static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001503{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001504 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001505 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001506
Joerg Roedel38ddf412008-09-11 10:38:32 +02001507 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1508 << DEV_ENTRY_MODE_SHIFT;
1509 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001510
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001511 if (ats)
1512 flags |= DTE_FLAG_IOTLB;
1513
1514 amd_iommu_dev_table[devid].data[3] |= flags;
1515 amd_iommu_dev_table[devid].data[2] = domain->id;
1516 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1517 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001518}
1519
Joerg Roedel15898bb2009-11-24 15:39:42 +01001520static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001521{
Joerg Roedel355bf552008-12-08 12:02:41 +01001522 /* remove entry from the device table seen by the hardware */
1523 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1524 amd_iommu_dev_table[devid].data[1] = 0;
1525 amd_iommu_dev_table[devid].data[2] = 0;
1526
Joerg Roedelc5cca142009-10-09 18:31:20 +02001527 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001528}
1529
1530static void do_attach(struct device *dev, struct protection_domain *domain)
1531{
1532 struct iommu_dev_data *dev_data;
1533 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001534 struct pci_dev *pdev;
1535 bool ats = false;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001536 u16 devid;
1537
1538 devid = get_device_id(dev);
1539 iommu = amd_iommu_rlookup_table[devid];
1540 dev_data = get_dev_data(dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001541 pdev = to_pci_dev(dev);
1542
1543 if (amd_iommu_iotlb_sup)
1544 ats = pci_ats_enabled(pdev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001545
1546 /* Update data structures */
1547 dev_data->domain = domain;
1548 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001549 set_dte_entry(devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001550
1551 /* Do reference counting */
1552 domain->dev_iommu[iommu->index] += 1;
1553 domain->dev_cnt += 1;
1554
1555 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001556 device_flush_dte(dev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001557}
1558
1559static void do_detach(struct device *dev)
1560{
1561 struct iommu_dev_data *dev_data;
1562 struct amd_iommu *iommu;
1563 u16 devid;
1564
1565 devid = get_device_id(dev);
1566 iommu = amd_iommu_rlookup_table[devid];
1567 dev_data = get_dev_data(dev);
Joerg Roedelc5cca142009-10-09 18:31:20 +02001568
Joerg Roedelc4596112009-11-20 14:57:32 +01001569 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001570 dev_data->domain->dev_iommu[iommu->index] -= 1;
1571 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001572
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001573 /* Update data structures */
1574 dev_data->domain = NULL;
1575 list_del(&dev_data->list);
1576 clear_dte_entry(devid);
1577
1578 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001579 device_flush_dte(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001580}
1581
1582/*
1583 * If a device is not yet associated with a domain, this function does
1584 * assigns it visible for the hardware
1585 */
1586static int __attach_device(struct device *dev,
1587 struct protection_domain *domain)
1588{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001589 struct iommu_dev_data *dev_data, *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001590 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001591
Joerg Roedel657cbb62009-11-23 15:26:46 +01001592 dev_data = get_dev_data(dev);
1593 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001594
Joerg Roedel657cbb62009-11-23 15:26:46 +01001595 if (!alias_data)
1596 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001597
1598 /* lock domain */
1599 spin_lock(&domain->lock);
1600
1601 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001602 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001603 if (alias_data->domain != NULL &&
1604 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001605 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001606
Joerg Roedel657cbb62009-11-23 15:26:46 +01001607 if (dev_data->domain != NULL &&
1608 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001609 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001610
1611 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001612 if (dev_data->alias != dev) {
1613 alias_data = get_dev_data(dev_data->alias);
1614 if (alias_data->domain == NULL)
1615 do_attach(dev_data->alias, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001616
1617 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001618 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001619
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001620 if (dev_data->domain == NULL)
1621 do_attach(dev, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001622
Joerg Roedel24100052009-11-25 15:59:57 +01001623 atomic_inc(&dev_data->bind);
1624
Julia Lawall84fe6c12010-05-27 12:31:51 +02001625 ret = 0;
1626
1627out_unlock:
1628
Joerg Roedel355bf552008-12-08 12:02:41 +01001629 /* ready */
1630 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001631
Julia Lawall84fe6c12010-05-27 12:31:51 +02001632 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001633}
1634
1635/*
1636 * If a device is not yet associated with a domain, this function does
1637 * assigns it visible for the hardware
1638 */
1639static int attach_device(struct device *dev,
1640 struct protection_domain *domain)
1641{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001642 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001643 unsigned long flags;
1644 int ret;
1645
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001646 if (amd_iommu_iotlb_sup)
1647 pci_enable_ats(pdev, PAGE_SHIFT);
1648
Joerg Roedel15898bb2009-11-24 15:39:42 +01001649 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1650 ret = __attach_device(dev, domain);
1651 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1652
1653 /*
1654 * We might boot into a crash-kernel here. The crashed kernel
1655 * left the caches in the IOMMU dirty. So we have to flush
1656 * here to evict all dirty stuff.
1657 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001658 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001659
1660 return ret;
1661}
1662
1663/*
1664 * Removes a device from a protection domain (unlocked)
1665 */
1666static void __detach_device(struct device *dev)
1667{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001668 struct iommu_dev_data *dev_data = get_dev_data(dev);
Joerg Roedel24100052009-11-25 15:59:57 +01001669 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001670 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001671 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001672
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001673 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001674
Joerg Roedel2ca76272010-01-22 16:45:31 +01001675 domain = dev_data->domain;
1676
1677 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001678
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001679 if (dev_data->alias != dev) {
Joerg Roedel24100052009-11-25 15:59:57 +01001680 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001681 if (atomic_dec_and_test(&alias_data->bind))
1682 do_detach(dev_data->alias);
Joerg Roedel24100052009-11-25 15:59:57 +01001683 }
1684
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001685 if (atomic_dec_and_test(&dev_data->bind))
1686 do_detach(dev);
1687
Joerg Roedel2ca76272010-01-22 16:45:31 +01001688 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001689
Joerg Roedel21129f72009-09-01 11:59:42 +02001690 /*
1691 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001692 * passthrough domain if it is detached from any other domain.
1693 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001694 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001695 if (iommu_pass_through &&
1696 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedel15898bb2009-11-24 15:39:42 +01001697 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001698}
1699
1700/*
1701 * Removes a device from a protection domain (with devtable_lock held)
1702 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001703static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001704{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001705 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001706 unsigned long flags;
1707
1708 /* lock device table */
1709 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001710 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001711 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001712
1713 if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
1714 pci_disable_ats(pdev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001715}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001716
Joerg Roedel15898bb2009-11-24 15:39:42 +01001717/*
1718 * Find out the protection domain structure for a given PCI device. This
1719 * will give us the pointer to the page table root for example.
1720 */
1721static struct protection_domain *domain_for_device(struct device *dev)
1722{
1723 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001724 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001725 unsigned long flags;
Gustavo F. Padovan6ec5ff42011-05-20 16:13:00 -03001726 u16 devid;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001727
Joerg Roedel657cbb62009-11-23 15:26:46 +01001728 devid = get_device_id(dev);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001729 dev_data = get_dev_data(dev);
1730 alias_data = get_dev_data(dev_data->alias);
1731 if (!alias_data)
1732 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001733
1734 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001735 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001736 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001737 alias_data->domain != NULL) {
1738 __attach_device(dev, alias_data->domain);
1739 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001740 }
1741
1742 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1743
1744 return dom;
1745}
1746
Joerg Roedele275a2a2008-12-10 18:27:25 +01001747static int device_change_notifier(struct notifier_block *nb,
1748 unsigned long action, void *data)
1749{
1750 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001751 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001752 struct protection_domain *domain;
1753 struct dma_ops_domain *dma_domain;
1754 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001755 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001756
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001757 if (!check_device(dev))
1758 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001759
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001760 devid = get_device_id(dev);
1761 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001762
1763 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001764 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001765
1766 domain = domain_for_device(dev);
1767
Joerg Roedele275a2a2008-12-10 18:27:25 +01001768 if (!domain)
1769 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001770 if (iommu_pass_through)
1771 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001772 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001773 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001774 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001775
1776 iommu_init_device(dev);
1777
1778 domain = domain_for_device(dev);
1779
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001780 /* allocate a protection domain if a device is added */
1781 dma_domain = find_protection_domain(devid);
1782 if (dma_domain)
1783 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001784 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001785 if (!dma_domain)
1786 goto out;
1787 dma_domain->target_dev = devid;
1788
1789 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1790 list_add_tail(&dma_domain->list, &iommu_pd_list);
1791 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1792
1793 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001794 case BUS_NOTIFY_DEL_DEVICE:
1795
1796 iommu_uninit_device(dev);
1797
Joerg Roedele275a2a2008-12-10 18:27:25 +01001798 default:
1799 goto out;
1800 }
1801
Joerg Roedeld8c13082011-04-06 18:51:26 +02001802 device_flush_dte(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001803 iommu_completion_wait(iommu);
1804
1805out:
1806 return 0;
1807}
1808
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301809static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001810 .notifier_call = device_change_notifier,
1811};
Joerg Roedel355bf552008-12-08 12:02:41 +01001812
Joerg Roedel8638c492009-12-10 11:12:25 +01001813void amd_iommu_init_notifier(void)
1814{
1815 bus_register_notifier(&pci_bus_type, &device_nb);
1816}
1817
Joerg Roedel431b2a22008-07-11 17:14:22 +02001818/*****************************************************************************
1819 *
1820 * The next functions belong to the dma_ops mapping/unmapping code.
1821 *
1822 *****************************************************************************/
1823
1824/*
1825 * In the dma_ops path we only have the struct device. This function
1826 * finds the corresponding IOMMU, the protection domain and the
1827 * requestor id for a given device.
1828 * If the device is not yet associated with a domain this is also done
1829 * in this function.
1830 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001831static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001832{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001833 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001834 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001835 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001836
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001837 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001838 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001839
Joerg Roedel94f6d192009-11-24 16:40:02 +01001840 domain = domain_for_device(dev);
1841 if (domain != NULL && !dma_ops_domain(domain))
1842 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001843
Joerg Roedel94f6d192009-11-24 16:40:02 +01001844 if (domain != NULL)
1845 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001846
Joerg Roedel15898bb2009-11-24 15:39:42 +01001847 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001848 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001849 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001850 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1851 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001852 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001853 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001854
Joerg Roedel94f6d192009-11-24 16:40:02 +01001855 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001856}
1857
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001858static void update_device_table(struct protection_domain *domain)
1859{
Joerg Roedel492667d2009-11-27 13:25:47 +01001860 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001861
Joerg Roedel492667d2009-11-27 13:25:47 +01001862 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001863 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01001864 u16 devid = get_device_id(dev_data->dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001865 set_dte_entry(devid, domain, pci_ats_enabled(pdev));
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001866 }
1867}
1868
1869static void update_domain(struct protection_domain *domain)
1870{
1871 if (!domain->updated)
1872 return;
1873
1874 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001875
1876 domain_flush_devices(domain);
1877 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001878
1879 domain->updated = false;
1880}
1881
Joerg Roedel431b2a22008-07-11 17:14:22 +02001882/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001883 * This function fetches the PTE for a given address in the aperture
1884 */
1885static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1886 unsigned long address)
1887{
Joerg Roedel384de722009-05-15 12:30:05 +02001888 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001889 u64 *pte, *pte_page;
1890
Joerg Roedel384de722009-05-15 12:30:05 +02001891 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1892 if (!aperture)
1893 return NULL;
1894
1895 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001896 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001897 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001898 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001899 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1900 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001901 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001902
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001903 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001904
1905 return pte;
1906}
1907
1908/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001909 * This is the generic map function. It maps one 4kb page at paddr to
1910 * the given address in the DMA address space for the domain.
1911 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001912static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001913 unsigned long address,
1914 phys_addr_t paddr,
1915 int direction)
1916{
1917 u64 *pte, __pte;
1918
1919 WARN_ON(address > dom->aperture_size);
1920
1921 paddr &= PAGE_MASK;
1922
Joerg Roedel8bda3092009-05-12 12:02:46 +02001923 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001924 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001925 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001926
1927 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1928
1929 if (direction == DMA_TO_DEVICE)
1930 __pte |= IOMMU_PTE_IR;
1931 else if (direction == DMA_FROM_DEVICE)
1932 __pte |= IOMMU_PTE_IW;
1933 else if (direction == DMA_BIDIRECTIONAL)
1934 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1935
1936 WARN_ON(*pte);
1937
1938 *pte = __pte;
1939
1940 return (dma_addr_t)address;
1941}
1942
Joerg Roedel431b2a22008-07-11 17:14:22 +02001943/*
1944 * The generic unmapping function for on page in the DMA address space.
1945 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001946static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001947 unsigned long address)
1948{
Joerg Roedel384de722009-05-15 12:30:05 +02001949 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001950 u64 *pte;
1951
1952 if (address >= dom->aperture_size)
1953 return;
1954
Joerg Roedel384de722009-05-15 12:30:05 +02001955 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1956 if (!aperture)
1957 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001958
Joerg Roedel384de722009-05-15 12:30:05 +02001959 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1960 if (!pte)
1961 return;
1962
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001963 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001964
1965 WARN_ON(!*pte);
1966
1967 *pte = 0ULL;
1968}
1969
Joerg Roedel431b2a22008-07-11 17:14:22 +02001970/*
1971 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001972 * contiguous memory region into DMA address space. It is used by all
1973 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001974 * Must be called with the domain lock held.
1975 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001976static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001977 struct dma_ops_domain *dma_dom,
1978 phys_addr_t paddr,
1979 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001980 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001981 bool align,
1982 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001983{
1984 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02001985 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001986 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001987 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001988 int i;
1989
Joerg Roedele3c449f2008-10-15 22:02:11 -07001990 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001991 paddr &= PAGE_MASK;
1992
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01001993 INC_STATS_COUNTER(total_map_requests);
1994
Joerg Roedelc1858972008-12-12 15:42:39 +01001995 if (pages > 1)
1996 INC_STATS_COUNTER(cross_page);
1997
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001998 if (align)
1999 align_mask = (1UL << get_order(size)) - 1;
2000
Joerg Roedel11b83882009-05-19 10:23:15 +02002001retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02002002 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2003 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002004 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02002005 /*
2006 * setting next_address here will let the address
2007 * allocator only scan the new allocated range in the
2008 * first run. This is a small optimization.
2009 */
2010 dma_dom->next_address = dma_dom->aperture_size;
2011
Joerg Roedel576175c2009-11-23 19:08:46 +01002012 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02002013 goto out;
2014
2015 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002016 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02002017 * allocation again
2018 */
2019 goto retry;
2020 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002021
2022 start = address;
2023 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002024 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002025 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002026 goto out_unmap;
2027
Joerg Roedelcb76c322008-06-26 21:28:00 +02002028 paddr += PAGE_SIZE;
2029 start += PAGE_SIZE;
2030 }
2031 address += offset;
2032
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002033 ADD_STATS_COUNTER(alloced_io_mem, size);
2034
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002035 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002036 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002037 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002038 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002039 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002040
Joerg Roedelcb76c322008-06-26 21:28:00 +02002041out:
2042 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002043
2044out_unmap:
2045
2046 for (--i; i >= 0; --i) {
2047 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002048 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002049 }
2050
2051 dma_ops_free_addresses(dma_dom, address, pages);
2052
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002053 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002054}
2055
Joerg Roedel431b2a22008-07-11 17:14:22 +02002056/*
2057 * Does the reverse of the __map_single function. Must be called with
2058 * the domain lock held too
2059 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002060static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002061 dma_addr_t dma_addr,
2062 size_t size,
2063 int dir)
2064{
Joerg Roedel04e04632010-09-23 16:12:48 +02002065 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002066 dma_addr_t i, start;
2067 unsigned int pages;
2068
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002069 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002070 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002071 return;
2072
Joerg Roedel04e04632010-09-23 16:12:48 +02002073 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002074 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002075 dma_addr &= PAGE_MASK;
2076 start = dma_addr;
2077
2078 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002079 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002080 start += PAGE_SIZE;
2081 }
2082
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002083 SUB_STATS_COUNTER(alloced_io_mem, size);
2084
Joerg Roedelcb76c322008-06-26 21:28:00 +02002085 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002086
Joerg Roedel80be3082008-11-06 14:59:05 +01002087 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002088 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002089 dma_dom->need_flush = false;
2090 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002091}
2092
Joerg Roedel431b2a22008-07-11 17:14:22 +02002093/*
2094 * The exported map_single function for dma_ops.
2095 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002096static dma_addr_t map_page(struct device *dev, struct page *page,
2097 unsigned long offset, size_t size,
2098 enum dma_data_direction dir,
2099 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002100{
2101 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002102 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002103 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002104 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002105 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002106
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002107 INC_STATS_COUNTER(cnt_map_single);
2108
Joerg Roedel94f6d192009-11-24 16:40:02 +01002109 domain = get_domain(dev);
2110 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002111 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002112 else if (IS_ERR(domain))
2113 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002114
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002115 dma_mask = *dev->dma_mask;
2116
Joerg Roedel4da70b92008-06-26 21:28:01 +02002117 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002118
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002119 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002120 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002121 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002122 goto out;
2123
Joerg Roedel17b124b2011-04-06 18:01:35 +02002124 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002125
2126out:
2127 spin_unlock_irqrestore(&domain->lock, flags);
2128
2129 return addr;
2130}
2131
Joerg Roedel431b2a22008-07-11 17:14:22 +02002132/*
2133 * The exported unmap_single function for dma_ops.
2134 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002135static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2136 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002137{
2138 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002139 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002140
Joerg Roedel146a6912008-12-12 15:07:12 +01002141 INC_STATS_COUNTER(cnt_unmap_single);
2142
Joerg Roedel94f6d192009-11-24 16:40:02 +01002143 domain = get_domain(dev);
2144 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002145 return;
2146
Joerg Roedel4da70b92008-06-26 21:28:01 +02002147 spin_lock_irqsave(&domain->lock, flags);
2148
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002149 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002150
Joerg Roedel17b124b2011-04-06 18:01:35 +02002151 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002152
2153 spin_unlock_irqrestore(&domain->lock, flags);
2154}
2155
Joerg Roedel431b2a22008-07-11 17:14:22 +02002156/*
2157 * This is a special map_sg function which is used if we should map a
2158 * device which is not handled by an AMD IOMMU in the system.
2159 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002160static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2161 int nelems, int dir)
2162{
2163 struct scatterlist *s;
2164 int i;
2165
2166 for_each_sg(sglist, s, nelems, i) {
2167 s->dma_address = (dma_addr_t)sg_phys(s);
2168 s->dma_length = s->length;
2169 }
2170
2171 return nelems;
2172}
2173
Joerg Roedel431b2a22008-07-11 17:14:22 +02002174/*
2175 * The exported map_sg function for dma_ops (handles scatter-gather
2176 * lists).
2177 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002178static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002179 int nelems, enum dma_data_direction dir,
2180 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002181{
2182 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002183 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002184 int i;
2185 struct scatterlist *s;
2186 phys_addr_t paddr;
2187 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002188 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002189
Joerg Roedeld03f067a2008-12-12 15:09:48 +01002190 INC_STATS_COUNTER(cnt_map_sg);
2191
Joerg Roedel94f6d192009-11-24 16:40:02 +01002192 domain = get_domain(dev);
2193 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002194 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002195 else if (IS_ERR(domain))
2196 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002197
Joerg Roedel832a90c2008-09-18 15:54:23 +02002198 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002199
Joerg Roedel65b050a2008-06-26 21:28:02 +02002200 spin_lock_irqsave(&domain->lock, flags);
2201
2202 for_each_sg(sglist, s, nelems, i) {
2203 paddr = sg_phys(s);
2204
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002205 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002206 paddr, s->length, dir, false,
2207 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002208
2209 if (s->dma_address) {
2210 s->dma_length = s->length;
2211 mapped_elems++;
2212 } else
2213 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002214 }
2215
Joerg Roedel17b124b2011-04-06 18:01:35 +02002216 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002217
2218out:
2219 spin_unlock_irqrestore(&domain->lock, flags);
2220
2221 return mapped_elems;
2222unmap:
2223 for_each_sg(sglist, s, mapped_elems, i) {
2224 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002225 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002226 s->dma_length, dir);
2227 s->dma_address = s->dma_length = 0;
2228 }
2229
2230 mapped_elems = 0;
2231
2232 goto out;
2233}
2234
Joerg Roedel431b2a22008-07-11 17:14:22 +02002235/*
2236 * The exported map_sg function for dma_ops (handles scatter-gather
2237 * lists).
2238 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002239static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002240 int nelems, enum dma_data_direction dir,
2241 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002242{
2243 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002244 struct protection_domain *domain;
2245 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002246 int i;
2247
Joerg Roedel55877a62008-12-12 15:12:14 +01002248 INC_STATS_COUNTER(cnt_unmap_sg);
2249
Joerg Roedel94f6d192009-11-24 16:40:02 +01002250 domain = get_domain(dev);
2251 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002252 return;
2253
Joerg Roedel65b050a2008-06-26 21:28:02 +02002254 spin_lock_irqsave(&domain->lock, flags);
2255
2256 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002257 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002258 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002259 s->dma_address = s->dma_length = 0;
2260 }
2261
Joerg Roedel17b124b2011-04-06 18:01:35 +02002262 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002263
2264 spin_unlock_irqrestore(&domain->lock, flags);
2265}
2266
Joerg Roedel431b2a22008-07-11 17:14:22 +02002267/*
2268 * The exported alloc_coherent function for dma_ops.
2269 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002270static void *alloc_coherent(struct device *dev, size_t size,
2271 dma_addr_t *dma_addr, gfp_t flag)
2272{
2273 unsigned long flags;
2274 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002275 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002276 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002277 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002278
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002279 INC_STATS_COUNTER(cnt_alloc_coherent);
2280
Joerg Roedel94f6d192009-11-24 16:40:02 +01002281 domain = get_domain(dev);
2282 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002283 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2284 *dma_addr = __pa(virt_addr);
2285 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002286 } else if (IS_ERR(domain))
2287 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002288
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002289 dma_mask = dev->coherent_dma_mask;
2290 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2291 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002292
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002293 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2294 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302295 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002296
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002297 paddr = virt_to_phys(virt_addr);
2298
Joerg Roedel832a90c2008-09-18 15:54:23 +02002299 if (!dma_mask)
2300 dma_mask = *dev->dma_mask;
2301
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002302 spin_lock_irqsave(&domain->lock, flags);
2303
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002304 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002305 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002306
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002307 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002308 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002309 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002310 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002311
Joerg Roedel17b124b2011-04-06 18:01:35 +02002312 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002313
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002314 spin_unlock_irqrestore(&domain->lock, flags);
2315
2316 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002317
2318out_free:
2319
2320 free_pages((unsigned long)virt_addr, get_order(size));
2321
2322 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002323}
2324
Joerg Roedel431b2a22008-07-11 17:14:22 +02002325/*
2326 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002327 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002328static void free_coherent(struct device *dev, size_t size,
2329 void *virt_addr, dma_addr_t dma_addr)
2330{
2331 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002332 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002333
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002334 INC_STATS_COUNTER(cnt_free_coherent);
2335
Joerg Roedel94f6d192009-11-24 16:40:02 +01002336 domain = get_domain(dev);
2337 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002338 goto free_mem;
2339
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002340 spin_lock_irqsave(&domain->lock, flags);
2341
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002342 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002343
Joerg Roedel17b124b2011-04-06 18:01:35 +02002344 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002345
2346 spin_unlock_irqrestore(&domain->lock, flags);
2347
2348free_mem:
2349 free_pages((unsigned long)virt_addr, get_order(size));
2350}
2351
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002352/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002353 * This function is called by the DMA layer to find out if we can handle a
2354 * particular device. It is part of the dma_ops.
2355 */
2356static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2357{
Joerg Roedel420aef82009-11-23 16:14:57 +01002358 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002359}
2360
2361/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002362 * The function for pre-allocating protection domains.
2363 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002364 * If the driver core informs the DMA layer if a driver grabs a device
2365 * we don't need to preallocate the protection domains anymore.
2366 * For now we have to.
2367 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302368static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002369{
2370 struct pci_dev *dev = NULL;
2371 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002372 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002373
Chris Wrightd18c69d2010-04-02 18:27:55 -07002374 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002375
2376 /* Do we handle this device? */
2377 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002378 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002379
2380 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002381 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002382 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002383
2384 devid = get_device_id(&dev->dev);
2385
Joerg Roedel87a64d52009-11-24 17:26:43 +01002386 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002387 if (!dma_dom)
2388 continue;
2389 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002390 dma_dom->target_dev = devid;
2391
Joerg Roedel15898bb2009-11-24 15:39:42 +01002392 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002393
Joerg Roedelbd60b732008-09-11 10:24:48 +02002394 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002395 }
2396}
2397
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002398static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002399 .alloc_coherent = alloc_coherent,
2400 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002401 .map_page = map_page,
2402 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002403 .map_sg = map_sg,
2404 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002405 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002406};
2407
Joerg Roedel27c21272011-05-30 15:56:24 +02002408static unsigned device_dma_ops_init(void)
2409{
2410 struct pci_dev *pdev = NULL;
2411 unsigned unhandled = 0;
2412
2413 for_each_pci_dev(pdev) {
2414 if (!check_device(&pdev->dev)) {
2415 unhandled += 1;
2416 continue;
2417 }
2418
2419 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
2420 }
2421
2422 return unhandled;
2423}
2424
Joerg Roedel431b2a22008-07-11 17:14:22 +02002425/*
2426 * The function which clues the AMD IOMMU driver into dma_ops.
2427 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002428
2429void __init amd_iommu_init_api(void)
2430{
2431 register_iommu(&amd_iommu_ops);
2432}
2433
Joerg Roedel6631ee92008-06-26 21:28:05 +02002434int __init amd_iommu_init_dma_ops(void)
2435{
2436 struct amd_iommu *iommu;
Joerg Roedel27c21272011-05-30 15:56:24 +02002437 int ret, unhandled;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002438
Joerg Roedel431b2a22008-07-11 17:14:22 +02002439 /*
2440 * first allocate a default protection domain for every IOMMU we
2441 * found in the system. Devices not assigned to any other
2442 * protection domain will be assigned to the default one.
2443 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002444 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002445 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002446 if (iommu->default_dom == NULL)
2447 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002448 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002449 ret = iommu_init_unity_mappings(iommu);
2450 if (ret)
2451 goto free_domains;
2452 }
2453
Joerg Roedel431b2a22008-07-11 17:14:22 +02002454 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002455 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002456 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002457 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002458
2459 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002460 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002461
Joerg Roedel431b2a22008-07-11 17:14:22 +02002462 /* Make the driver finally visible to the drivers */
Joerg Roedel27c21272011-05-30 15:56:24 +02002463 unhandled = device_dma_ops_init();
2464 if (unhandled && max_pfn > MAX_DMA32_PFN) {
2465 /* There are unhandled devices - initialize swiotlb for them */
2466 swiotlb = 1;
2467 }
Joerg Roedel6631ee92008-06-26 21:28:05 +02002468
Joerg Roedel7f265082008-12-12 13:50:21 +01002469 amd_iommu_stats_init();
2470
Joerg Roedel6631ee92008-06-26 21:28:05 +02002471 return 0;
2472
2473free_domains:
2474
Joerg Roedel3bd22172009-05-04 15:06:20 +02002475 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002476 if (iommu->default_dom)
2477 dma_ops_domain_free(iommu->default_dom);
2478 }
2479
2480 return ret;
2481}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002482
2483/*****************************************************************************
2484 *
2485 * The following functions belong to the exported interface of AMD IOMMU
2486 *
2487 * This interface allows access to lower level functions of the IOMMU
2488 * like protection domain handling and assignement of devices to domains
2489 * which is not possible with the dma_ops interface.
2490 *
2491 *****************************************************************************/
2492
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002493static void cleanup_domain(struct protection_domain *domain)
2494{
Joerg Roedel492667d2009-11-27 13:25:47 +01002495 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002496 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002497
2498 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2499
Joerg Roedel492667d2009-11-27 13:25:47 +01002500 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2501 struct device *dev = dev_data->dev;
2502
Chris Wright04e856c2010-02-17 08:51:20 -08002503 __detach_device(dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01002504 atomic_set(&dev_data->bind, 0);
2505 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002506
2507 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2508}
2509
Joerg Roedel26508152009-08-26 16:52:40 +02002510static void protection_domain_free(struct protection_domain *domain)
2511{
2512 if (!domain)
2513 return;
2514
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002515 del_domain_from_list(domain);
2516
Joerg Roedel26508152009-08-26 16:52:40 +02002517 if (domain->id)
2518 domain_id_free(domain->id);
2519
2520 kfree(domain);
2521}
2522
2523static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002524{
2525 struct protection_domain *domain;
2526
2527 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2528 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002529 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002530
2531 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002532 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002533 domain->id = domain_id_alloc();
2534 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002535 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002536 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002537
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002538 add_domain_to_list(domain);
2539
Joerg Roedel26508152009-08-26 16:52:40 +02002540 return domain;
2541
2542out_err:
2543 kfree(domain);
2544
2545 return NULL;
2546}
2547
2548static int amd_iommu_domain_init(struct iommu_domain *dom)
2549{
2550 struct protection_domain *domain;
2551
2552 domain = protection_domain_alloc();
2553 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002554 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002555
2556 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002557 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2558 if (!domain->pt_root)
2559 goto out_free;
2560
2561 dom->priv = domain;
2562
2563 return 0;
2564
2565out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002566 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002567
2568 return -ENOMEM;
2569}
2570
Joerg Roedel98383fc2008-12-02 18:34:12 +01002571static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2572{
2573 struct protection_domain *domain = dom->priv;
2574
2575 if (!domain)
2576 return;
2577
2578 if (domain->dev_cnt > 0)
2579 cleanup_domain(domain);
2580
2581 BUG_ON(domain->dev_cnt != 0);
2582
2583 free_pagetable(domain);
2584
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002585 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002586
2587 dom->priv = NULL;
2588}
2589
Joerg Roedel684f2882008-12-08 12:07:44 +01002590static void amd_iommu_detach_device(struct iommu_domain *dom,
2591 struct device *dev)
2592{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002593 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002594 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002595 u16 devid;
2596
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002597 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002598 return;
2599
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002600 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002601
Joerg Roedel657cbb62009-11-23 15:26:46 +01002602 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002603 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002604
2605 iommu = amd_iommu_rlookup_table[devid];
2606 if (!iommu)
2607 return;
2608
Joerg Roedeld8c13082011-04-06 18:51:26 +02002609 device_flush_dte(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002610 iommu_completion_wait(iommu);
2611}
2612
Joerg Roedel01106062008-12-02 19:34:11 +01002613static int amd_iommu_attach_device(struct iommu_domain *dom,
2614 struct device *dev)
2615{
2616 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002617 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002618 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002619 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002620 u16 devid;
2621
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002622 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002623 return -EINVAL;
2624
Joerg Roedel657cbb62009-11-23 15:26:46 +01002625 dev_data = dev->archdata.iommu;
2626
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002627 devid = get_device_id(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002628
2629 iommu = amd_iommu_rlookup_table[devid];
2630 if (!iommu)
2631 return -EINVAL;
2632
Joerg Roedel657cbb62009-11-23 15:26:46 +01002633 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002634 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002635
Joerg Roedel15898bb2009-11-24 15:39:42 +01002636 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002637
2638 iommu_completion_wait(iommu);
2639
Joerg Roedel15898bb2009-11-24 15:39:42 +01002640 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002641}
2642
Joerg Roedel468e2362010-01-21 16:37:36 +01002643static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2644 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002645{
Joerg Roedel468e2362010-01-21 16:37:36 +01002646 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002647 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002648 int prot = 0;
2649 int ret;
2650
2651 if (iommu_prot & IOMMU_READ)
2652 prot |= IOMMU_PROT_IR;
2653 if (iommu_prot & IOMMU_WRITE)
2654 prot |= IOMMU_PROT_IW;
2655
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002656 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002657 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002658 mutex_unlock(&domain->api_lock);
2659
Joerg Roedel795e74f72010-05-11 17:40:57 +02002660 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002661}
2662
Joerg Roedel468e2362010-01-21 16:37:36 +01002663static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2664 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002665{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002666 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002667 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002668
Joerg Roedel468e2362010-01-21 16:37:36 +01002669 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002670
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002671 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002672 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002673 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002674
Joerg Roedel17b124b2011-04-06 18:01:35 +02002675 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002676
Joerg Roedel468e2362010-01-21 16:37:36 +01002677 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002678}
2679
Joerg Roedel645c4c82008-12-02 20:05:50 +01002680static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2681 unsigned long iova)
2682{
2683 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002684 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002685 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002686 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002687
Joerg Roedel24cd7722010-01-19 17:27:39 +01002688 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002689
Joerg Roedela6d41a42009-09-02 17:08:55 +02002690 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002691 return 0;
2692
Joerg Roedelf03152b2010-01-21 16:15:24 +01002693 if (PM_PTE_LEVEL(*pte) == 0)
2694 offset_mask = PAGE_SIZE - 1;
2695 else
2696 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2697
2698 __pte = *pte & PM_ADDR_MASK;
2699 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002700
2701 return paddr;
2702}
2703
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002704static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2705 unsigned long cap)
2706{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002707 switch (cap) {
2708 case IOMMU_CAP_CACHE_COHERENCY:
2709 return 1;
2710 }
2711
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002712 return 0;
2713}
2714
Joerg Roedel26961ef2008-12-03 17:00:17 +01002715static struct iommu_ops amd_iommu_ops = {
2716 .domain_init = amd_iommu_domain_init,
2717 .domain_destroy = amd_iommu_domain_destroy,
2718 .attach_dev = amd_iommu_attach_device,
2719 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002720 .map = amd_iommu_map,
2721 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002722 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002723 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002724};
2725
Joerg Roedel0feae532009-08-26 15:26:30 +02002726/*****************************************************************************
2727 *
2728 * The next functions do a basic initialization of IOMMU for pass through
2729 * mode
2730 *
2731 * In passthrough mode the IOMMU is initialized and enabled but not used for
2732 * DMA-API translation.
2733 *
2734 *****************************************************************************/
2735
2736int __init amd_iommu_init_passthrough(void)
2737{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002738 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002739 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002740 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002741
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002742 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002743 pt_domain = protection_domain_alloc();
2744 if (!pt_domain)
2745 return -ENOMEM;
2746
2747 pt_domain->mode |= PAGE_MODE_NONE;
2748
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002749 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002750 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002751 continue;
2752
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002753 devid = get_device_id(&dev->dev);
2754
Joerg Roedel15898bb2009-11-24 15:39:42 +01002755 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002756 if (!iommu)
2757 continue;
2758
Joerg Roedel15898bb2009-11-24 15:39:42 +01002759 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002760 }
2761
2762 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2763
2764 return 0;
2765}