blob: dc5dddafe5c24a148d28736b9cfe2f9f2ef8c97d [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 Roedelb6c02712008-06-26 21:27:53 +020030#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090031#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010032#include <asm/gart.h>
Joerg Roedel6a9401a2009-11-20 13:22:21 +010033#include <asm/amd_iommu_proto.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020034#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020035#include <asm/amd_iommu.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020036
37#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
38
Joerg Roedel815b33f2011-04-06 17:26:49 +020039#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020040
Joerg Roedelb6c02712008-06-26 21:27:53 +020041static DEFINE_RWLOCK(amd_iommu_devtable_lock);
42
Joerg Roedelbd60b732008-09-11 10:24:48 +020043/* A list of preallocated protection domains */
44static LIST_HEAD(iommu_pd_list);
45static DEFINE_SPINLOCK(iommu_pd_list_lock);
46
Joerg Roedel0feae532009-08-26 15:26:30 +020047/*
48 * Domain for untranslated devices - only allocated
49 * if iommu=pt passed on kernel cmd line.
50 */
51static struct protection_domain *pt_domain;
52
Joerg Roedel26961ef2008-12-03 17:00:17 +010053static struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010054
Joerg Roedel431b2a22008-07-11 17:14:22 +020055/*
56 * general struct to manage commands send to an IOMMU
57 */
Joerg Roedeld6449532008-07-11 17:14:28 +020058struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +020059 u32 data[4];
60};
61
Joerg Roedel04bfdd82009-09-02 16:00:23 +020062static void update_domain(struct protection_domain *domain);
Chris Wrightc1eee672009-05-21 00:56:58 -070063
Joerg Roedel15898bb2009-11-24 15:39:42 +010064/****************************************************************************
65 *
66 * Helper functions
67 *
68 ****************************************************************************/
69
70static inline u16 get_device_id(struct device *dev)
71{
72 struct pci_dev *pdev = to_pci_dev(dev);
73
74 return calc_devid(pdev->bus->number, pdev->devfn);
75}
76
Joerg Roedel657cbb62009-11-23 15:26:46 +010077static struct iommu_dev_data *get_dev_data(struct device *dev)
78{
79 return dev->archdata.iommu;
80}
81
Joerg Roedel71c70982009-11-24 16:43:06 +010082/*
83 * In this function the list of preallocated protection domains is traversed to
84 * find the domain for a specific device
85 */
86static struct dma_ops_domain *find_protection_domain(u16 devid)
87{
88 struct dma_ops_domain *entry, *ret = NULL;
89 unsigned long flags;
90 u16 alias = amd_iommu_alias_table[devid];
91
92 if (list_empty(&iommu_pd_list))
93 return NULL;
94
95 spin_lock_irqsave(&iommu_pd_list_lock, flags);
96
97 list_for_each_entry(entry, &iommu_pd_list, list) {
98 if (entry->target_dev == devid ||
99 entry->target_dev == alias) {
100 ret = entry;
101 break;
102 }
103 }
104
105 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
106
107 return ret;
108}
109
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100110/*
111 * This function checks if the driver got a valid device from the caller to
112 * avoid dereferencing invalid pointers.
113 */
114static bool check_device(struct device *dev)
115{
116 u16 devid;
117
118 if (!dev || !dev->dma_mask)
119 return false;
120
121 /* No device or no PCI device */
Julia Lawall339d3262010-02-06 09:42:39 +0100122 if (dev->bus != &pci_bus_type)
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100123 return false;
124
125 devid = get_device_id(dev);
126
127 /* Out of our scope? */
128 if (devid > amd_iommu_last_bdf)
129 return false;
130
131 if (amd_iommu_rlookup_table[devid] == NULL)
132 return false;
133
134 return true;
135}
136
Joerg Roedel657cbb62009-11-23 15:26:46 +0100137static int iommu_init_device(struct device *dev)
138{
139 struct iommu_dev_data *dev_data;
140 struct pci_dev *pdev;
141 u16 devid, alias;
142
143 if (dev->archdata.iommu)
144 return 0;
145
146 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
147 if (!dev_data)
148 return -ENOMEM;
149
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100150 dev_data->dev = dev;
151
Joerg Roedel657cbb62009-11-23 15:26:46 +0100152 devid = get_device_id(dev);
153 alias = amd_iommu_alias_table[devid];
154 pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff);
155 if (pdev)
156 dev_data->alias = &pdev->dev;
157
Joerg Roedel24100052009-11-25 15:59:57 +0100158 atomic_set(&dev_data->bind, 0);
159
Joerg Roedel657cbb62009-11-23 15:26:46 +0100160 dev->archdata.iommu = dev_data;
161
162
163 return 0;
164}
165
166static void iommu_uninit_device(struct device *dev)
167{
168 kfree(dev->archdata.iommu);
169}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100170
171void __init amd_iommu_uninit_devices(void)
172{
173 struct pci_dev *pdev = NULL;
174
175 for_each_pci_dev(pdev) {
176
177 if (!check_device(&pdev->dev))
178 continue;
179
180 iommu_uninit_device(&pdev->dev);
181 }
182}
183
184int __init amd_iommu_init_devices(void)
185{
186 struct pci_dev *pdev = NULL;
187 int ret = 0;
188
189 for_each_pci_dev(pdev) {
190
191 if (!check_device(&pdev->dev))
192 continue;
193
194 ret = iommu_init_device(&pdev->dev);
195 if (ret)
196 goto out_free;
197 }
198
199 return 0;
200
201out_free:
202
203 amd_iommu_uninit_devices();
204
205 return ret;
206}
Joerg Roedel7f265082008-12-12 13:50:21 +0100207#ifdef CONFIG_AMD_IOMMU_STATS
208
209/*
210 * Initialization code for statistics collection
211 */
212
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100213DECLARE_STATS_COUNTER(compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100214DECLARE_STATS_COUNTER(cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100215DECLARE_STATS_COUNTER(cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100216DECLARE_STATS_COUNTER(cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100217DECLARE_STATS_COUNTER(cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100218DECLARE_STATS_COUNTER(cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100219DECLARE_STATS_COUNTER(cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100220DECLARE_STATS_COUNTER(cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100221DECLARE_STATS_COUNTER(domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100222DECLARE_STATS_COUNTER(domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100223DECLARE_STATS_COUNTER(alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100224DECLARE_STATS_COUNTER(total_map_requests);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100225
Joerg Roedel7f265082008-12-12 13:50:21 +0100226static struct dentry *stats_dir;
Joerg Roedel7f265082008-12-12 13:50:21 +0100227static struct dentry *de_fflush;
228
229static void amd_iommu_stats_add(struct __iommu_counter *cnt)
230{
231 if (stats_dir == NULL)
232 return;
233
234 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
235 &cnt->value);
236}
237
238static void amd_iommu_stats_init(void)
239{
240 stats_dir = debugfs_create_dir("amd-iommu", NULL);
241 if (stats_dir == NULL)
242 return;
243
Joerg Roedel7f265082008-12-12 13:50:21 +0100244 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
245 (u32 *)&amd_iommu_unmap_flush);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100246
247 amd_iommu_stats_add(&compl_wait);
Joerg Roedel0f2a86f2008-12-12 15:05:16 +0100248 amd_iommu_stats_add(&cnt_map_single);
Joerg Roedel146a6912008-12-12 15:07:12 +0100249 amd_iommu_stats_add(&cnt_unmap_single);
Joerg Roedeld03f0672008-12-12 15:09:48 +0100250 amd_iommu_stats_add(&cnt_map_sg);
Joerg Roedel55877a62008-12-12 15:12:14 +0100251 amd_iommu_stats_add(&cnt_unmap_sg);
Joerg Roedelc8f0fb32008-12-12 15:14:21 +0100252 amd_iommu_stats_add(&cnt_alloc_coherent);
Joerg Roedel5d31ee72008-12-12 15:16:38 +0100253 amd_iommu_stats_add(&cnt_free_coherent);
Joerg Roedelc1858972008-12-12 15:42:39 +0100254 amd_iommu_stats_add(&cross_page);
Joerg Roedelf57d98a2008-12-12 15:46:29 +0100255 amd_iommu_stats_add(&domain_flush_single);
Joerg Roedel18811f52008-12-12 15:48:28 +0100256 amd_iommu_stats_add(&domain_flush_all);
Joerg Roedel5774f7c2008-12-12 15:57:30 +0100257 amd_iommu_stats_add(&alloced_io_mem);
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +0100258 amd_iommu_stats_add(&total_map_requests);
Joerg Roedel7f265082008-12-12 13:50:21 +0100259}
260
261#endif
262
Joerg Roedel431b2a22008-07-11 17:14:22 +0200263/****************************************************************************
264 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200265 * Interrupt handling functions
266 *
267 ****************************************************************************/
268
Joerg Roedele3e59872009-09-03 14:02:10 +0200269static void dump_dte_entry(u16 devid)
270{
271 int i;
272
273 for (i = 0; i < 8; ++i)
274 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
275 amd_iommu_dev_table[devid].data[i]);
276}
277
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200278static void dump_command(unsigned long phys_addr)
279{
280 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
281 int i;
282
283 for (i = 0; i < 4; ++i)
284 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
285}
286
Joerg Roedela345b232009-09-03 15:01:43 +0200287static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200288{
289 u32 *event = __evt;
290 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
291 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
292 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
293 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
294 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
295
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200296 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200297
298 switch (type) {
299 case EVENT_TYPE_ILL_DEV:
300 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
301 "address=0x%016llx flags=0x%04x]\n",
302 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
303 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200304 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200305 break;
306 case EVENT_TYPE_IO_FAULT:
307 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
308 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
309 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
310 domid, address, flags);
311 break;
312 case EVENT_TYPE_DEV_TAB_ERR:
313 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
314 "address=0x%016llx flags=0x%04x]\n",
315 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
316 address, flags);
317 break;
318 case EVENT_TYPE_PAGE_TAB_ERR:
319 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
320 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
321 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
322 domid, address, flags);
323 break;
324 case EVENT_TYPE_ILL_CMD:
325 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200326 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200327 break;
328 case EVENT_TYPE_CMD_HARD_ERR:
329 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
330 "flags=0x%04x]\n", address, flags);
331 break;
332 case EVENT_TYPE_IOTLB_INV_TO:
333 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
334 "address=0x%016llx]\n",
335 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
336 address);
337 break;
338 case EVENT_TYPE_INV_DEV_REQ:
339 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
340 "address=0x%016llx flags=0x%04x]\n",
341 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
342 address, flags);
343 break;
344 default:
345 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
346 }
347}
348
349static void iommu_poll_events(struct amd_iommu *iommu)
350{
351 u32 head, tail;
352 unsigned long flags;
353
354 spin_lock_irqsave(&iommu->lock, flags);
355
356 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
357 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
358
359 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200360 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200361 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
362 }
363
364 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
365
366 spin_unlock_irqrestore(&iommu->lock, flags);
367}
368
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200369irqreturn_t amd_iommu_int_handler(int irq, void *data)
370{
Joerg Roedel90008ee2008-09-09 16:41:05 +0200371 struct amd_iommu *iommu;
372
Joerg Roedel3bd22172009-05-04 15:06:20 +0200373 for_each_iommu(iommu)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200374 iommu_poll_events(iommu);
375
376 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200377}
378
379/****************************************************************************
380 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200381 * IOMMU command queuing functions
382 *
383 ****************************************************************************/
384
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200385static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200386{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200387 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200388
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200389 while (*sem == 0 && i < LOOP_TIMEOUT) {
390 udelay(1);
391 i += 1;
392 }
393
394 if (i == LOOP_TIMEOUT) {
395 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
396 return -EIO;
397 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200398
399 return 0;
400}
401
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200402static void copy_cmd_to_buffer(struct amd_iommu *iommu,
403 struct iommu_cmd *cmd,
404 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200405{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200406 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200407
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200408 target = iommu->cmd_buf + tail;
409 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200410
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200411 /* Copy command to buffer */
412 memcpy(target, cmd, sizeof(*cmd));
413
414 /* Tell the IOMMU about it */
415 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
416}
417
Joerg Roedel815b33f2011-04-06 17:26:49 +0200418static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200419{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200420 WARN_ON(address & 0x7ULL);
421
Joerg Roedelded46732011-04-06 10:53:48 +0200422 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200423 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
424 cmd->data[1] = upper_32_bits(__pa(address));
425 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200426 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
427}
428
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200429static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
430{
431 memset(cmd, 0, sizeof(*cmd));
432 cmd->data[0] = devid;
433 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
434}
435
Joerg Roedel11b64022011-04-06 11:49:28 +0200436static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
437 size_t size, u16 domid, int pde)
438{
439 u64 pages;
440 int s;
441
442 pages = iommu_num_pages(address, size, PAGE_SIZE);
443 s = 0;
444
445 if (pages > 1) {
446 /*
447 * If we have to flush more than one page, flush all
448 * TLB entries for this domain
449 */
450 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
451 s = 1;
452 }
453
454 address &= PAGE_MASK;
455
456 memset(cmd, 0, sizeof(*cmd));
457 cmd->data[1] |= domid;
458 cmd->data[2] = lower_32_bits(address);
459 cmd->data[3] = upper_32_bits(address);
460 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
461 if (s) /* size bit - we flush more than one 4kb page */
462 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
463 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
464 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
465}
466
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200467static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
468 u64 address, size_t size)
469{
470 u64 pages;
471 int s;
472
473 pages = iommu_num_pages(address, size, PAGE_SIZE);
474 s = 0;
475
476 if (pages > 1) {
477 /*
478 * If we have to flush more than one page, flush all
479 * TLB entries for this domain
480 */
481 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
482 s = 1;
483 }
484
485 address &= PAGE_MASK;
486
487 memset(cmd, 0, sizeof(*cmd));
488 cmd->data[0] = devid;
489 cmd->data[0] |= (qdep & 0xff) << 24;
490 cmd->data[1] = devid;
491 cmd->data[2] = lower_32_bits(address);
492 cmd->data[3] = upper_32_bits(address);
493 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
494 if (s)
495 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
496}
497
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200498static void build_inv_all(struct iommu_cmd *cmd)
499{
500 memset(cmd, 0, sizeof(*cmd));
501 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200502}
503
Joerg Roedel431b2a22008-07-11 17:14:22 +0200504/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200505 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200506 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200507 */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200508static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200509{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200510 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200511 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200512
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200513 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
Joerg Roedelda49f6d2008-12-12 14:59:58 +0100514
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200515again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200516 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200517
518 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
519 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
520 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
521 left = (head - next_tail) % iommu->cmd_buf_size;
522
523 if (left <= 2) {
524 struct iommu_cmd sync_cmd;
525 volatile u64 sem = 0;
526 int ret;
527
528 build_completion_wait(&sync_cmd, (u64)&sem);
529 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
530
531 spin_unlock_irqrestore(&iommu->lock, flags);
532
533 if ((ret = wait_on_sem(&sem)) != 0)
534 return ret;
535
536 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200537 }
538
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200539 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200540
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200541 /* We need to sync now to make sure all commands are processed */
Joerg Roedel815b33f2011-04-06 17:26:49 +0200542 iommu->need_sync = true;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200543
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200544 spin_unlock_irqrestore(&iommu->lock, flags);
545
Joerg Roedel815b33f2011-04-06 17:26:49 +0200546 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100547}
548
549/*
550 * This function queues a completion wait command into the command
551 * buffer of an IOMMU
552 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100553static int iommu_completion_wait(struct amd_iommu *iommu)
554{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200555 struct iommu_cmd cmd;
556 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200557 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100558
559 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200560 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100561
Joerg Roedel815b33f2011-04-06 17:26:49 +0200562 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100563
Joerg Roedel815b33f2011-04-06 17:26:49 +0200564 ret = iommu_queue_command(iommu, &cmd);
Joerg Roedel8d201962008-12-02 20:34:41 +0100565 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200566 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100567
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200568 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200569}
570
Joerg Roedeld8c13082011-04-06 18:51:26 +0200571static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200572{
573 struct iommu_cmd cmd;
574
Joerg Roedeld8c13082011-04-06 18:51:26 +0200575 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200576
Joerg Roedeld8c13082011-04-06 18:51:26 +0200577 return iommu_queue_command(iommu, &cmd);
578}
579
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200580static void iommu_flush_dte_all(struct amd_iommu *iommu)
581{
582 u32 devid;
583
584 for (devid = 0; devid <= 0xffff; ++devid)
585 iommu_flush_dte(iommu, devid);
586
587 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200588}
589
590/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200591 * This function uses heavy locking and may disable irqs for some time. But
592 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200593 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200594static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200595{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200596 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200597
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200598 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
599 struct iommu_cmd cmd;
600 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
601 dom_id, 1);
602 iommu_queue_command(iommu, &cmd);
603 }
Joerg Roedel431b2a22008-07-11 17:14:22 +0200604
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200605 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200606}
607
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200608static void iommu_flush_all(struct amd_iommu *iommu)
609{
610 struct iommu_cmd cmd;
611
612 build_inv_all(&cmd);
613
614 iommu_queue_command(iommu, &cmd);
615 iommu_completion_wait(iommu);
616}
617
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200618void iommu_flush_all_caches(struct amd_iommu *iommu)
619{
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200620 if (iommu_feature(iommu, FEATURE_IA)) {
621 iommu_flush_all(iommu);
622 } else {
623 iommu_flush_dte_all(iommu);
624 iommu_flush_tlb_all(iommu);
625 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200626}
627
Joerg Roedel431b2a22008-07-11 17:14:22 +0200628/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200629 * Command send function for flushing on-device TLB
630 */
631static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
632{
633 struct pci_dev *pdev = to_pci_dev(dev);
634 struct amd_iommu *iommu;
635 struct iommu_cmd cmd;
636 u16 devid;
637 int qdep;
638
639 qdep = pci_ats_queue_depth(pdev);
640 devid = get_device_id(dev);
641 iommu = amd_iommu_rlookup_table[devid];
642
643 build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
644
645 return iommu_queue_command(iommu, &cmd);
646}
647
648/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200649 * Command send function for invalidating a device table entry
650 */
Joerg Roedeld8c13082011-04-06 18:51:26 +0200651static int device_flush_dte(struct device *dev)
Joerg Roedel3fa43652009-11-26 15:04:38 +0100652{
653 struct amd_iommu *iommu;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200654 struct pci_dev *pdev;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100655 u16 devid;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200656 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100657
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200658 pdev = to_pci_dev(dev);
Joerg Roedel3fa43652009-11-26 15:04:38 +0100659 devid = get_device_id(dev);
660 iommu = amd_iommu_rlookup_table[devid];
661
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200662 ret = iommu_flush_dte(iommu, devid);
663 if (ret)
664 return ret;
665
666 if (pci_ats_enabled(pdev))
667 ret = device_flush_iotlb(dev, 0, ~0UL);
668
669 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +0100670}
671
Joerg Roedel431b2a22008-07-11 17:14:22 +0200672/*
673 * TLB invalidation function which is called from the mapping functions.
674 * It invalidates a single PTE if the range to flush is within a single
675 * page. Otherwise it flushes the whole TLB of the IOMMU.
676 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200677static void __domain_flush_pages(struct protection_domain *domain,
678 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200679{
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200680 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +0200681 struct iommu_cmd cmd;
682 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200683
Joerg Roedel11b64022011-04-06 11:49:28 +0200684 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +0200685
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100686 for (i = 0; i < amd_iommus_present; ++i) {
687 if (!domain->dev_iommu[i])
688 continue;
689
690 /*
691 * Devices of this domain are behind this IOMMU
692 * We need a TLB flush
693 */
Joerg Roedel11b64022011-04-06 11:49:28 +0200694 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100695 }
696
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200697 list_for_each_entry(dev_data, &domain->dev_list, list) {
698 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
699
700 if (!pci_ats_enabled(pdev))
701 continue;
702
703 ret |= device_flush_iotlb(dev_data->dev, address, size);
704 }
705
Joerg Roedel11b64022011-04-06 11:49:28 +0200706 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100707}
708
Joerg Roedel17b124b2011-04-06 18:01:35 +0200709static void domain_flush_pages(struct protection_domain *domain,
710 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +0100711{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200712 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200713}
Joerg Roedelb6c02712008-06-26 21:27:53 +0200714
Joerg Roedel1c655772008-09-04 18:40:05 +0200715/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200716static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +0200717{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200718 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +0200719}
720
Chris Wright42a49f92009-06-15 15:42:00 +0200721/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200722static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +0200723{
Joerg Roedel17b124b2011-04-06 18:01:35 +0200724 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
725}
726
727static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +0200728{
729 int i;
730
731 for (i = 0; i < amd_iommus_present; ++i) {
732 if (!domain->dev_iommu[i])
733 continue;
734
735 /*
736 * Devices of this domain are behind this IOMMU
737 * We need to wait for completion of all commands.
738 */
739 iommu_completion_wait(amd_iommus[i]);
740 }
741}
742
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100743
Joerg Roedel43f49602008-12-02 21:01:12 +0100744/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100745 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +0100746 */
Joerg Roedel17b124b2011-04-06 18:01:35 +0200747static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +0200748{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100749 struct iommu_dev_data *dev_data;
750 unsigned long flags;
751
752 spin_lock_irqsave(&domain->lock, flags);
753
754 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedeld8c13082011-04-06 18:51:26 +0200755 device_flush_dte(dev_data->dev);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +0100756
757 spin_unlock_irqrestore(&domain->lock, flags);
758}
759
Joerg Roedel431b2a22008-07-11 17:14:22 +0200760/****************************************************************************
761 *
762 * The functions below are used the create the page table mappings for
763 * unity mapped regions.
764 *
765 ****************************************************************************/
766
767/*
Joerg Roedel308973d2009-11-24 17:43:32 +0100768 * This function is used to add another level to an IO page table. Adding
769 * another level increases the size of the address space by 9 bits to a size up
770 * to 64 bits.
771 */
772static bool increase_address_space(struct protection_domain *domain,
773 gfp_t gfp)
774{
775 u64 *pte;
776
777 if (domain->mode == PAGE_MODE_6_LEVEL)
778 /* address space already 64 bit large */
779 return false;
780
781 pte = (void *)get_zeroed_page(gfp);
782 if (!pte)
783 return false;
784
785 *pte = PM_LEVEL_PDE(domain->mode,
786 virt_to_phys(domain->pt_root));
787 domain->pt_root = pte;
788 domain->mode += 1;
789 domain->updated = true;
790
791 return true;
792}
793
794static u64 *alloc_pte(struct protection_domain *domain,
795 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100796 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +0100797 u64 **pte_page,
798 gfp_t gfp)
799{
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100800 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +0100801 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100802
803 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +0100804
805 while (address > PM_LEVEL_SIZE(domain->mode))
806 increase_address_space(domain, gfp);
807
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100808 level = domain->mode - 1;
809 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
810 address = PAGE_SIZE_ALIGN(address, page_size);
811 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +0100812
813 while (level > end_lvl) {
814 if (!IOMMU_PTE_PRESENT(*pte)) {
815 page = (u64 *)get_zeroed_page(gfp);
816 if (!page)
817 return NULL;
818 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
819 }
820
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100821 /* No level skipping support yet */
822 if (PM_PTE_LEVEL(*pte) != level)
823 return NULL;
824
Joerg Roedel308973d2009-11-24 17:43:32 +0100825 level -= 1;
826
827 pte = IOMMU_PTE_PAGE(*pte);
828
829 if (pte_page && level == end_lvl)
830 *pte_page = pte;
831
832 pte = &pte[PM_LEVEL_INDEX(level, address)];
833 }
834
835 return pte;
836}
837
838/*
839 * This function checks if there is a PTE for a given dma address. If
840 * there is one, it returns the pointer to it.
841 */
Joerg Roedel24cd7722010-01-19 17:27:39 +0100842static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
Joerg Roedel308973d2009-11-24 17:43:32 +0100843{
844 int level;
845 u64 *pte;
846
Joerg Roedel24cd7722010-01-19 17:27:39 +0100847 if (address > PM_LEVEL_SIZE(domain->mode))
848 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +0100849
Joerg Roedel24cd7722010-01-19 17:27:39 +0100850 level = domain->mode - 1;
851 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
852
853 while (level > 0) {
854
855 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +0100856 if (!IOMMU_PTE_PRESENT(*pte))
857 return NULL;
858
Joerg Roedel24cd7722010-01-19 17:27:39 +0100859 /* Large PTE */
860 if (PM_PTE_LEVEL(*pte) == 0x07) {
861 unsigned long pte_mask, __pte;
862
863 /*
864 * If we have a series of large PTEs, make
865 * sure to return a pointer to the first one.
866 */
867 pte_mask = PTE_PAGE_SIZE(*pte);
868 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
869 __pte = ((unsigned long)pte) & pte_mask;
870
871 return (u64 *)__pte;
872 }
873
874 /* No level skipping support yet */
875 if (PM_PTE_LEVEL(*pte) != level)
876 return NULL;
877
Joerg Roedel308973d2009-11-24 17:43:32 +0100878 level -= 1;
879
Joerg Roedel24cd7722010-01-19 17:27:39 +0100880 /* Walk to the next level */
Joerg Roedel308973d2009-11-24 17:43:32 +0100881 pte = IOMMU_PTE_PAGE(*pte);
882 pte = &pte[PM_LEVEL_INDEX(level, address)];
Joerg Roedel308973d2009-11-24 17:43:32 +0100883 }
884
885 return pte;
886}
887
888/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200889 * Generic mapping functions. It maps a physical address into a DMA
890 * address space. It allocates the page table pages if necessary.
891 * In the future it can be extended to a generic mapping function
892 * supporting all features of AMD IOMMU page tables like level skipping
893 * and full 64 bit address spaces.
894 */
Joerg Roedel38e817f2008-12-02 17:27:52 +0100895static int iommu_map_page(struct protection_domain *dom,
896 unsigned long bus_addr,
897 unsigned long phys_addr,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200898 int prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100899 unsigned long page_size)
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200900{
Joerg Roedel8bda3092009-05-12 12:02:46 +0200901 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100902 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +0200903
Joerg Roedelbad1cac2009-09-02 16:52:23 +0200904 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200905 return -EINVAL;
906
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100907 bus_addr = PAGE_ALIGN(bus_addr);
908 phys_addr = PAGE_ALIGN(phys_addr);
909 count = PAGE_SIZE_PTE_COUNT(page_size);
910 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200911
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100912 for (i = 0; i < count; ++i)
913 if (IOMMU_PTE_PRESENT(pte[i]))
914 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200915
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100916 if (page_size > PAGE_SIZE) {
917 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
918 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
919 } else
920 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
921
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200922 if (prot & IOMMU_PROT_IR)
923 __pte |= IOMMU_PTE_IR;
924 if (prot & IOMMU_PROT_IW)
925 __pte |= IOMMU_PTE_IW;
926
Joerg Roedelcbb9d722010-01-15 14:41:15 +0100927 for (i = 0; i < count; ++i)
928 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200929
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200930 update_domain(dom);
931
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200932 return 0;
933}
934
Joerg Roedel24cd7722010-01-19 17:27:39 +0100935static unsigned long iommu_unmap_page(struct protection_domain *dom,
936 unsigned long bus_addr,
937 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100938{
Joerg Roedel24cd7722010-01-19 17:27:39 +0100939 unsigned long long unmap_size, unmapped;
940 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100941
Joerg Roedel24cd7722010-01-19 17:27:39 +0100942 BUG_ON(!is_power_of_2(page_size));
943
944 unmapped = 0;
945
946 while (unmapped < page_size) {
947
948 pte = fetch_pte(dom, bus_addr);
949
950 if (!pte) {
951 /*
952 * No PTE for this address
953 * move forward in 4kb steps
954 */
955 unmap_size = PAGE_SIZE;
956 } else if (PM_PTE_LEVEL(*pte) == 0) {
957 /* 4kb PTE found for this address */
958 unmap_size = PAGE_SIZE;
959 *pte = 0ULL;
960 } else {
961 int count, i;
962
963 /* Large PTE found which maps this address */
964 unmap_size = PTE_PAGE_SIZE(*pte);
965 count = PAGE_SIZE_PTE_COUNT(unmap_size);
966 for (i = 0; i < count; i++)
967 pte[i] = 0ULL;
968 }
969
970 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
971 unmapped += unmap_size;
972 }
973
974 BUG_ON(!is_power_of_2(unmapped));
975
976 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100977}
Joerg Roedeleb74ff62008-12-02 19:59:10 +0100978
Joerg Roedel431b2a22008-07-11 17:14:22 +0200979/*
980 * This function checks if a specific unity mapping entry is needed for
981 * this specific IOMMU.
982 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +0200983static int iommu_for_unity_map(struct amd_iommu *iommu,
984 struct unity_map_entry *entry)
985{
986 u16 bdf, i;
987
988 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
989 bdf = amd_iommu_alias_table[i];
990 if (amd_iommu_rlookup_table[bdf] == iommu)
991 return 1;
992 }
993
994 return 0;
995}
996
Joerg Roedel431b2a22008-07-11 17:14:22 +0200997/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200998 * This function actually applies the mapping to the page table of the
999 * dma_ops domain.
1000 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001001static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1002 struct unity_map_entry *e)
1003{
1004 u64 addr;
1005 int ret;
1006
1007 for (addr = e->address_start; addr < e->address_end;
1008 addr += PAGE_SIZE) {
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001009 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001010 PAGE_SIZE);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001011 if (ret)
1012 return ret;
1013 /*
1014 * if unity mapping is in aperture range mark the page
1015 * as allocated in the aperture
1016 */
1017 if (addr < dma_dom->aperture_size)
Joerg Roedelc3239562009-05-12 10:56:44 +02001018 __set_bit(addr >> PAGE_SHIFT,
Joerg Roedel384de722009-05-15 12:30:05 +02001019 dma_dom->aperture[0]->bitmap);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001020 }
1021
1022 return 0;
1023}
1024
Joerg Roedel431b2a22008-07-11 17:14:22 +02001025/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001026 * Init the unity mappings for a specific IOMMU in the system
1027 *
1028 * Basically iterates over all unity mapping entries and applies them to
1029 * the default domain DMA of that IOMMU if necessary.
1030 */
1031static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1032{
1033 struct unity_map_entry *entry;
1034 int ret;
1035
1036 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1037 if (!iommu_for_unity_map(iommu, entry))
1038 continue;
1039 ret = dma_ops_unity_map(iommu->default_dom, entry);
1040 if (ret)
1041 return ret;
1042 }
1043
1044 return 0;
1045}
1046
1047/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001048 * Inits the unity mappings required for a specific device
1049 */
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001050static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1051 u16 devid)
1052{
1053 struct unity_map_entry *e;
1054 int ret;
1055
1056 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1057 if (!(devid >= e->devid_start && devid <= e->devid_end))
1058 continue;
1059 ret = dma_ops_unity_map(dma_dom, e);
1060 if (ret)
1061 return ret;
1062 }
1063
1064 return 0;
1065}
1066
Joerg Roedel431b2a22008-07-11 17:14:22 +02001067/****************************************************************************
1068 *
1069 * The next functions belong to the address allocator for the dma_ops
1070 * interface functions. They work like the allocators in the other IOMMU
1071 * drivers. Its basically a bitmap which marks the allocated pages in
1072 * the aperture. Maybe it could be enhanced in the future to a more
1073 * efficient allocator.
1074 *
1075 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001076
Joerg Roedel431b2a22008-07-11 17:14:22 +02001077/*
Joerg Roedel384de722009-05-15 12:30:05 +02001078 * The address allocator core functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001079 *
1080 * called with domain->lock held
1081 */
Joerg Roedel384de722009-05-15 12:30:05 +02001082
Joerg Roedel9cabe892009-05-18 16:38:55 +02001083/*
Joerg Roedel171e7b32009-11-24 17:47:56 +01001084 * Used to reserve address ranges in the aperture (e.g. for exclusion
1085 * ranges.
1086 */
1087static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1088 unsigned long start_page,
1089 unsigned int pages)
1090{
1091 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1092
1093 if (start_page + pages > last_page)
1094 pages = last_page - start_page;
1095
1096 for (i = start_page; i < start_page + pages; ++i) {
1097 int index = i / APERTURE_RANGE_PAGES;
1098 int page = i % APERTURE_RANGE_PAGES;
1099 __set_bit(page, dom->aperture[index]->bitmap);
1100 }
1101}
1102
1103/*
Joerg Roedel9cabe892009-05-18 16:38:55 +02001104 * This function is used to add a new aperture range to an existing
1105 * aperture in case of dma_ops domain allocation or address allocation
1106 * failure.
1107 */
Joerg Roedel576175c2009-11-23 19:08:46 +01001108static int alloc_new_range(struct dma_ops_domain *dma_dom,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001109 bool populate, gfp_t gfp)
1110{
1111 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
Joerg Roedel576175c2009-11-23 19:08:46 +01001112 struct amd_iommu *iommu;
Joerg Roedeld91afd12010-01-22 16:40:20 +01001113 unsigned long i;
Joerg Roedel9cabe892009-05-18 16:38:55 +02001114
Joerg Roedelf5e97052009-05-22 12:31:53 +02001115#ifdef CONFIG_IOMMU_STRESS
1116 populate = false;
1117#endif
1118
Joerg Roedel9cabe892009-05-18 16:38:55 +02001119 if (index >= APERTURE_MAX_RANGES)
1120 return -ENOMEM;
1121
1122 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1123 if (!dma_dom->aperture[index])
1124 return -ENOMEM;
1125
1126 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1127 if (!dma_dom->aperture[index]->bitmap)
1128 goto out_free;
1129
1130 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1131
1132 if (populate) {
1133 unsigned long address = dma_dom->aperture_size;
1134 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1135 u64 *pte, *pte_page;
1136
1137 for (i = 0; i < num_ptes; ++i) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001138 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
Joerg Roedel9cabe892009-05-18 16:38:55 +02001139 &pte_page, gfp);
1140 if (!pte)
1141 goto out_free;
1142
1143 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1144
1145 address += APERTURE_RANGE_SIZE / 64;
1146 }
1147 }
1148
1149 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1150
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001151 /* Initialize the exclusion range if necessary */
Joerg Roedel576175c2009-11-23 19:08:46 +01001152 for_each_iommu(iommu) {
1153 if (iommu->exclusion_start &&
1154 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1155 && iommu->exclusion_start < dma_dom->aperture_size) {
1156 unsigned long startpage;
1157 int pages = iommu_num_pages(iommu->exclusion_start,
1158 iommu->exclusion_length,
1159 PAGE_SIZE);
1160 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1161 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1162 }
Joerg Roedel00cd1222009-05-19 09:52:40 +02001163 }
1164
1165 /*
1166 * Check for areas already mapped as present in the new aperture
1167 * range and mark those pages as reserved in the allocator. Such
1168 * mappings may already exist as a result of requested unity
1169 * mappings for devices.
1170 */
1171 for (i = dma_dom->aperture[index]->offset;
1172 i < dma_dom->aperture_size;
1173 i += PAGE_SIZE) {
Joerg Roedel24cd7722010-01-19 17:27:39 +01001174 u64 *pte = fetch_pte(&dma_dom->domain, i);
Joerg Roedel00cd1222009-05-19 09:52:40 +02001175 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1176 continue;
1177
1178 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1179 }
1180
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001181 update_domain(&dma_dom->domain);
1182
Joerg Roedel9cabe892009-05-18 16:38:55 +02001183 return 0;
1184
1185out_free:
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001186 update_domain(&dma_dom->domain);
1187
Joerg Roedel9cabe892009-05-18 16:38:55 +02001188 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1189
1190 kfree(dma_dom->aperture[index]);
1191 dma_dom->aperture[index] = NULL;
1192
1193 return -ENOMEM;
1194}
1195
Joerg Roedel384de722009-05-15 12:30:05 +02001196static unsigned long dma_ops_area_alloc(struct device *dev,
1197 struct dma_ops_domain *dom,
1198 unsigned int pages,
1199 unsigned long align_mask,
1200 u64 dma_mask,
1201 unsigned long start)
1202{
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001203 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
Joerg Roedel384de722009-05-15 12:30:05 +02001204 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1205 int i = start >> APERTURE_RANGE_SHIFT;
1206 unsigned long boundary_size;
1207 unsigned long address = -1;
1208 unsigned long limit;
1209
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001210 next_bit >>= PAGE_SHIFT;
1211
Joerg Roedel384de722009-05-15 12:30:05 +02001212 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1213 PAGE_SIZE) >> PAGE_SHIFT;
1214
1215 for (;i < max_index; ++i) {
1216 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1217
1218 if (dom->aperture[i]->offset >= dma_mask)
1219 break;
1220
1221 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1222 dma_mask >> PAGE_SHIFT);
1223
1224 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1225 limit, next_bit, pages, 0,
1226 boundary_size, align_mask);
1227 if (address != -1) {
1228 address = dom->aperture[i]->offset +
1229 (address << PAGE_SHIFT);
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001230 dom->next_address = address + (pages << PAGE_SHIFT);
Joerg Roedel384de722009-05-15 12:30:05 +02001231 break;
1232 }
1233
1234 next_bit = 0;
1235 }
1236
1237 return address;
1238}
1239
Joerg Roedeld3086442008-06-26 21:27:57 +02001240static unsigned long dma_ops_alloc_addresses(struct device *dev,
1241 struct dma_ops_domain *dom,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001242 unsigned int pages,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001243 unsigned long align_mask,
1244 u64 dma_mask)
Joerg Roedeld3086442008-06-26 21:27:57 +02001245{
Joerg Roedeld3086442008-06-26 21:27:57 +02001246 unsigned long address;
Joerg Roedeld3086442008-06-26 21:27:57 +02001247
Joerg Roedelfe16f082009-05-22 12:27:53 +02001248#ifdef CONFIG_IOMMU_STRESS
1249 dom->next_address = 0;
1250 dom->need_flush = true;
1251#endif
Joerg Roedeld3086442008-06-26 21:27:57 +02001252
Joerg Roedel384de722009-05-15 12:30:05 +02001253 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001254 dma_mask, dom->next_address);
Joerg Roedeld3086442008-06-26 21:27:57 +02001255
Joerg Roedel1c655772008-09-04 18:40:05 +02001256 if (address == -1) {
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001257 dom->next_address = 0;
Joerg Roedel384de722009-05-15 12:30:05 +02001258 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1259 dma_mask, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001260 dom->need_flush = true;
1261 }
Joerg Roedeld3086442008-06-26 21:27:57 +02001262
Joerg Roedel384de722009-05-15 12:30:05 +02001263 if (unlikely(address == -1))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001264 address = DMA_ERROR_CODE;
Joerg Roedeld3086442008-06-26 21:27:57 +02001265
1266 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1267
1268 return address;
1269}
1270
Joerg Roedel431b2a22008-07-11 17:14:22 +02001271/*
1272 * The address free function.
1273 *
1274 * called with domain->lock held
1275 */
Joerg Roedeld3086442008-06-26 21:27:57 +02001276static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1277 unsigned long address,
1278 unsigned int pages)
1279{
Joerg Roedel384de722009-05-15 12:30:05 +02001280 unsigned i = address >> APERTURE_RANGE_SHIFT;
1281 struct aperture_range *range = dom->aperture[i];
Joerg Roedel80be3082008-11-06 14:59:05 +01001282
Joerg Roedel384de722009-05-15 12:30:05 +02001283 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1284
Joerg Roedel47bccd62009-05-22 12:40:54 +02001285#ifdef CONFIG_IOMMU_STRESS
1286 if (i < 4)
1287 return;
1288#endif
1289
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001290 if (address >= dom->next_address)
Joerg Roedel80be3082008-11-06 14:59:05 +01001291 dom->need_flush = true;
Joerg Roedel384de722009-05-15 12:30:05 +02001292
1293 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001294
Akinobu Mitaa66022c2009-12-15 16:48:28 -08001295 bitmap_clear(range->bitmap, address, pages);
Joerg Roedel384de722009-05-15 12:30:05 +02001296
Joerg Roedeld3086442008-06-26 21:27:57 +02001297}
1298
Joerg Roedel431b2a22008-07-11 17:14:22 +02001299/****************************************************************************
1300 *
1301 * The next functions belong to the domain allocation. A domain is
1302 * allocated for every IOMMU as the default domain. If device isolation
1303 * is enabled, every device get its own domain. The most important thing
1304 * about domains is the page table mapping the DMA address space they
1305 * contain.
1306 *
1307 ****************************************************************************/
1308
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001309/*
1310 * This function adds a protection domain to the global protection domain list
1311 */
1312static void add_domain_to_list(struct protection_domain *domain)
1313{
1314 unsigned long flags;
1315
1316 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1317 list_add(&domain->list, &amd_iommu_pd_list);
1318 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1319}
1320
1321/*
1322 * This function removes a protection domain to the global
1323 * protection domain list
1324 */
1325static void del_domain_from_list(struct protection_domain *domain)
1326{
1327 unsigned long flags;
1328
1329 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1330 list_del(&domain->list);
1331 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1332}
1333
Joerg Roedelec487d12008-06-26 21:27:58 +02001334static u16 domain_id_alloc(void)
1335{
1336 unsigned long flags;
1337 int id;
1338
1339 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1340 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1341 BUG_ON(id == 0);
1342 if (id > 0 && id < MAX_DOMAIN_ID)
1343 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1344 else
1345 id = 0;
1346 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1347
1348 return id;
1349}
1350
Joerg Roedela2acfb72008-12-02 18:28:53 +01001351static void domain_id_free(int id)
1352{
1353 unsigned long flags;
1354
1355 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1356 if (id > 0 && id < MAX_DOMAIN_ID)
1357 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1358 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1359}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001360
Joerg Roedel86db2e52008-12-02 18:20:21 +01001361static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001362{
1363 int i, j;
1364 u64 *p1, *p2, *p3;
1365
Joerg Roedel86db2e52008-12-02 18:20:21 +01001366 p1 = domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001367
1368 if (!p1)
1369 return;
1370
1371 for (i = 0; i < 512; ++i) {
1372 if (!IOMMU_PTE_PRESENT(p1[i]))
1373 continue;
1374
1375 p2 = IOMMU_PTE_PAGE(p1[i]);
Joerg Roedel3cc3d842008-12-04 16:44:31 +01001376 for (j = 0; j < 512; ++j) {
Joerg Roedelec487d12008-06-26 21:27:58 +02001377 if (!IOMMU_PTE_PRESENT(p2[j]))
1378 continue;
1379 p3 = IOMMU_PTE_PAGE(p2[j]);
1380 free_page((unsigned long)p3);
1381 }
1382
1383 free_page((unsigned long)p2);
1384 }
1385
1386 free_page((unsigned long)p1);
Joerg Roedel86db2e52008-12-02 18:20:21 +01001387
1388 domain->pt_root = NULL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001389}
1390
Joerg Roedel431b2a22008-07-11 17:14:22 +02001391/*
1392 * Free a domain, only used if something went wrong in the
1393 * allocation path and we need to free an already allocated page table
1394 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001395static void dma_ops_domain_free(struct dma_ops_domain *dom)
1396{
Joerg Roedel384de722009-05-15 12:30:05 +02001397 int i;
1398
Joerg Roedelec487d12008-06-26 21:27:58 +02001399 if (!dom)
1400 return;
1401
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001402 del_domain_from_list(&dom->domain);
1403
Joerg Roedel86db2e52008-12-02 18:20:21 +01001404 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001405
Joerg Roedel384de722009-05-15 12:30:05 +02001406 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1407 if (!dom->aperture[i])
1408 continue;
1409 free_page((unsigned long)dom->aperture[i]->bitmap);
1410 kfree(dom->aperture[i]);
1411 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001412
1413 kfree(dom);
1414}
1415
Joerg Roedel431b2a22008-07-11 17:14:22 +02001416/*
1417 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001418 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001419 * structures required for the dma_ops interface
1420 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001421static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001422{
1423 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001424
1425 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1426 if (!dma_dom)
1427 return NULL;
1428
1429 spin_lock_init(&dma_dom->domain.lock);
1430
1431 dma_dom->domain.id = domain_id_alloc();
1432 if (dma_dom->domain.id == 0)
1433 goto free_dma_dom;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001434 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001435 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001436 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001437 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001438 dma_dom->domain.priv = dma_dom;
1439 if (!dma_dom->domain.pt_root)
1440 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001441
Joerg Roedel1c655772008-09-04 18:40:05 +02001442 dma_dom->need_flush = false;
Joerg Roedelbd60b732008-09-11 10:24:48 +02001443 dma_dom->target_dev = 0xffff;
Joerg Roedel1c655772008-09-04 18:40:05 +02001444
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001445 add_domain_to_list(&dma_dom->domain);
1446
Joerg Roedel576175c2009-11-23 19:08:46 +01001447 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
Joerg Roedelec487d12008-06-26 21:27:58 +02001448 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001449
Joerg Roedel431b2a22008-07-11 17:14:22 +02001450 /*
Joerg Roedelec487d12008-06-26 21:27:58 +02001451 * mark the first page as allocated so we never return 0 as
1452 * a valid dma-address. So we can use 0 as error value
Joerg Roedel431b2a22008-07-11 17:14:22 +02001453 */
Joerg Roedel384de722009-05-15 12:30:05 +02001454 dma_dom->aperture[0]->bitmap[0] = 1;
Joerg Roedel803b8cb2009-05-18 15:32:48 +02001455 dma_dom->next_address = 0;
Joerg Roedelec487d12008-06-26 21:27:58 +02001456
Joerg Roedelec487d12008-06-26 21:27:58 +02001457
1458 return dma_dom;
1459
1460free_dma_dom:
1461 dma_ops_domain_free(dma_dom);
1462
1463 return NULL;
1464}
1465
Joerg Roedel431b2a22008-07-11 17:14:22 +02001466/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001467 * little helper function to check whether a given protection domain is a
1468 * dma_ops domain
1469 */
1470static bool dma_ops_domain(struct protection_domain *domain)
1471{
1472 return domain->flags & PD_DMA_OPS_MASK;
1473}
1474
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001475static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001476{
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001477 u64 pte_root = virt_to_phys(domain->pt_root);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001478 u32 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001479
Joerg Roedel38ddf412008-09-11 10:38:32 +02001480 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1481 << DEV_ENTRY_MODE_SHIFT;
1482 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001483
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001484 if (ats)
1485 flags |= DTE_FLAG_IOTLB;
1486
1487 amd_iommu_dev_table[devid].data[3] |= flags;
1488 amd_iommu_dev_table[devid].data[2] = domain->id;
1489 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1490 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001491}
1492
Joerg Roedel15898bb2009-11-24 15:39:42 +01001493static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001494{
Joerg Roedel355bf552008-12-08 12:02:41 +01001495 /* remove entry from the device table seen by the hardware */
1496 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1497 amd_iommu_dev_table[devid].data[1] = 0;
1498 amd_iommu_dev_table[devid].data[2] = 0;
1499
Joerg Roedelc5cca142009-10-09 18:31:20 +02001500 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001501}
1502
1503static void do_attach(struct device *dev, struct protection_domain *domain)
1504{
1505 struct iommu_dev_data *dev_data;
1506 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001507 struct pci_dev *pdev;
1508 bool ats = false;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001509 u16 devid;
1510
1511 devid = get_device_id(dev);
1512 iommu = amd_iommu_rlookup_table[devid];
1513 dev_data = get_dev_data(dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001514 pdev = to_pci_dev(dev);
1515
1516 if (amd_iommu_iotlb_sup)
1517 ats = pci_ats_enabled(pdev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001518
1519 /* Update data structures */
1520 dev_data->domain = domain;
1521 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001522 set_dte_entry(devid, domain, ats);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001523
1524 /* Do reference counting */
1525 domain->dev_iommu[iommu->index] += 1;
1526 domain->dev_cnt += 1;
1527
1528 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001529 device_flush_dte(dev);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001530}
1531
1532static void do_detach(struct device *dev)
1533{
1534 struct iommu_dev_data *dev_data;
1535 struct amd_iommu *iommu;
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001536 struct pci_dev *pdev;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001537 u16 devid;
1538
1539 devid = get_device_id(dev);
1540 iommu = amd_iommu_rlookup_table[devid];
1541 dev_data = get_dev_data(dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001542 pdev = to_pci_dev(dev);
Joerg Roedelc5cca142009-10-09 18:31:20 +02001543
Joerg Roedelc4596112009-11-20 14:57:32 +01001544 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001545 dev_data->domain->dev_iommu[iommu->index] -= 1;
1546 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001547
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001548 /* Update data structures */
1549 dev_data->domain = NULL;
1550 list_del(&dev_data->list);
1551 clear_dte_entry(devid);
1552
1553 /* Flush the DTE entry */
Joerg Roedeld8c13082011-04-06 18:51:26 +02001554 device_flush_dte(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001555}
1556
1557/*
1558 * If a device is not yet associated with a domain, this function does
1559 * assigns it visible for the hardware
1560 */
1561static int __attach_device(struct device *dev,
1562 struct protection_domain *domain)
1563{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001564 struct iommu_dev_data *dev_data, *alias_data;
Julia Lawall84fe6c12010-05-27 12:31:51 +02001565 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001566
Joerg Roedel657cbb62009-11-23 15:26:46 +01001567 dev_data = get_dev_data(dev);
1568 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001569
Joerg Roedel657cbb62009-11-23 15:26:46 +01001570 if (!alias_data)
1571 return -EINVAL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001572
1573 /* lock domain */
1574 spin_lock(&domain->lock);
1575
1576 /* Some sanity checks */
Julia Lawall84fe6c12010-05-27 12:31:51 +02001577 ret = -EBUSY;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001578 if (alias_data->domain != NULL &&
1579 alias_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001580 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001581
Joerg Roedel657cbb62009-11-23 15:26:46 +01001582 if (dev_data->domain != NULL &&
1583 dev_data->domain != domain)
Julia Lawall84fe6c12010-05-27 12:31:51 +02001584 goto out_unlock;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001585
1586 /* Do real assignment */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001587 if (dev_data->alias != dev) {
1588 alias_data = get_dev_data(dev_data->alias);
1589 if (alias_data->domain == NULL)
1590 do_attach(dev_data->alias, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001591
1592 atomic_inc(&alias_data->bind);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001593 }
Joerg Roedel15898bb2009-11-24 15:39:42 +01001594
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001595 if (dev_data->domain == NULL)
1596 do_attach(dev, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001597
Joerg Roedel24100052009-11-25 15:59:57 +01001598 atomic_inc(&dev_data->bind);
1599
Julia Lawall84fe6c12010-05-27 12:31:51 +02001600 ret = 0;
1601
1602out_unlock:
1603
Joerg Roedel355bf552008-12-08 12:02:41 +01001604 /* ready */
1605 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001606
Julia Lawall84fe6c12010-05-27 12:31:51 +02001607 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001608}
1609
1610/*
1611 * If a device is not yet associated with a domain, this function does
1612 * assigns it visible for the hardware
1613 */
1614static int attach_device(struct device *dev,
1615 struct protection_domain *domain)
1616{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001617 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001618 unsigned long flags;
1619 int ret;
1620
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001621 if (amd_iommu_iotlb_sup)
1622 pci_enable_ats(pdev, PAGE_SHIFT);
1623
Joerg Roedel15898bb2009-11-24 15:39:42 +01001624 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1625 ret = __attach_device(dev, domain);
1626 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1627
1628 /*
1629 * We might boot into a crash-kernel here. The crashed kernel
1630 * left the caches in the IOMMU dirty. So we have to flush
1631 * here to evict all dirty stuff.
1632 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001633 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001634
1635 return ret;
1636}
1637
1638/*
1639 * Removes a device from a protection domain (unlocked)
1640 */
1641static void __detach_device(struct device *dev)
1642{
Joerg Roedel657cbb62009-11-23 15:26:46 +01001643 struct iommu_dev_data *dev_data = get_dev_data(dev);
Joerg Roedel24100052009-11-25 15:59:57 +01001644 struct iommu_dev_data *alias_data;
Joerg Roedel2ca76272010-01-22 16:45:31 +01001645 struct protection_domain *domain;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01001646 unsigned long flags;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001647
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001648 BUG_ON(!dev_data->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001649
Joerg Roedel2ca76272010-01-22 16:45:31 +01001650 domain = dev_data->domain;
1651
1652 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel24100052009-11-25 15:59:57 +01001653
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001654 if (dev_data->alias != dev) {
Joerg Roedel24100052009-11-25 15:59:57 +01001655 alias_data = get_dev_data(dev_data->alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001656 if (atomic_dec_and_test(&alias_data->bind))
1657 do_detach(dev_data->alias);
Joerg Roedel24100052009-11-25 15:59:57 +01001658 }
1659
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001660 if (atomic_dec_and_test(&dev_data->bind))
1661 do_detach(dev);
1662
Joerg Roedel2ca76272010-01-22 16:45:31 +01001663 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001664
Joerg Roedel21129f72009-09-01 11:59:42 +02001665 /*
1666 * If we run in passthrough mode the device must be assigned to the
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001667 * passthrough domain if it is detached from any other domain.
1668 * Make sure we can deassign from the pt_domain itself.
Joerg Roedel21129f72009-09-01 11:59:42 +02001669 */
Joerg Roedeld3ad9372010-01-22 17:55:27 +01001670 if (iommu_pass_through &&
1671 (dev_data->domain == NULL && domain != pt_domain))
Joerg Roedel15898bb2009-11-24 15:39:42 +01001672 __attach_device(dev, pt_domain);
Joerg Roedel355bf552008-12-08 12:02:41 +01001673}
1674
1675/*
1676 * Removes a device from a protection domain (with devtable_lock held)
1677 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01001678static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01001679{
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001680 struct pci_dev *pdev = to_pci_dev(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001681 unsigned long flags;
1682
1683 /* lock device table */
1684 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001685 __detach_device(dev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001686 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001687
1688 if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
1689 pci_disable_ats(pdev);
Joerg Roedel355bf552008-12-08 12:02:41 +01001690}
Joerg Roedele275a2a2008-12-10 18:27:25 +01001691
Joerg Roedel15898bb2009-11-24 15:39:42 +01001692/*
1693 * Find out the protection domain structure for a given PCI device. This
1694 * will give us the pointer to the page table root for example.
1695 */
1696static struct protection_domain *domain_for_device(struct device *dev)
1697{
1698 struct protection_domain *dom;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001699 struct iommu_dev_data *dev_data, *alias_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001700 unsigned long flags;
1701 u16 devid, alias;
1702
Joerg Roedel657cbb62009-11-23 15:26:46 +01001703 devid = get_device_id(dev);
1704 alias = amd_iommu_alias_table[devid];
1705 dev_data = get_dev_data(dev);
1706 alias_data = get_dev_data(dev_data->alias);
1707 if (!alias_data)
1708 return NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001709
1710 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedel657cbb62009-11-23 15:26:46 +01001711 dom = dev_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001712 if (dom == NULL &&
Joerg Roedel657cbb62009-11-23 15:26:46 +01001713 alias_data->domain != NULL) {
1714 __attach_device(dev, alias_data->domain);
1715 dom = alias_data->domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001716 }
1717
1718 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1719
1720 return dom;
1721}
1722
Joerg Roedele275a2a2008-12-10 18:27:25 +01001723static int device_change_notifier(struct notifier_block *nb,
1724 unsigned long action, void *data)
1725{
1726 struct device *dev = data;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001727 u16 devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001728 struct protection_domain *domain;
1729 struct dma_ops_domain *dma_domain;
1730 struct amd_iommu *iommu;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001731 unsigned long flags;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001732
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001733 if (!check_device(dev))
1734 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01001735
Joerg Roedel98fc5a62009-11-24 17:19:23 +01001736 devid = get_device_id(dev);
1737 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01001738
1739 switch (action) {
Chris Wrightc1eee672009-05-21 00:56:58 -07001740 case BUS_NOTIFY_UNBOUND_DRIVER:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001741
1742 domain = domain_for_device(dev);
1743
Joerg Roedele275a2a2008-12-10 18:27:25 +01001744 if (!domain)
1745 goto out;
Joerg Roedela1ca3312009-09-01 12:22:22 +02001746 if (iommu_pass_through)
1747 break;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001748 detach_device(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001749 break;
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001750 case BUS_NOTIFY_ADD_DEVICE:
Joerg Roedel657cbb62009-11-23 15:26:46 +01001751
1752 iommu_init_device(dev);
1753
1754 domain = domain_for_device(dev);
1755
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001756 /* allocate a protection domain if a device is added */
1757 dma_domain = find_protection_domain(devid);
1758 if (dma_domain)
1759 goto out;
Joerg Roedel87a64d52009-11-24 17:26:43 +01001760 dma_domain = dma_ops_domain_alloc();
Joerg Roedel1ac4cbb2008-12-10 19:33:26 +01001761 if (!dma_domain)
1762 goto out;
1763 dma_domain->target_dev = devid;
1764
1765 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1766 list_add_tail(&dma_domain->list, &iommu_pd_list);
1767 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1768
1769 break;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001770 case BUS_NOTIFY_DEL_DEVICE:
1771
1772 iommu_uninit_device(dev);
1773
Joerg Roedele275a2a2008-12-10 18:27:25 +01001774 default:
1775 goto out;
1776 }
1777
Joerg Roedeld8c13082011-04-06 18:51:26 +02001778 device_flush_dte(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01001779 iommu_completion_wait(iommu);
1780
1781out:
1782 return 0;
1783}
1784
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05301785static struct notifier_block device_nb = {
Joerg Roedele275a2a2008-12-10 18:27:25 +01001786 .notifier_call = device_change_notifier,
1787};
Joerg Roedel355bf552008-12-08 12:02:41 +01001788
Joerg Roedel8638c492009-12-10 11:12:25 +01001789void amd_iommu_init_notifier(void)
1790{
1791 bus_register_notifier(&pci_bus_type, &device_nb);
1792}
1793
Joerg Roedel431b2a22008-07-11 17:14:22 +02001794/*****************************************************************************
1795 *
1796 * The next functions belong to the dma_ops mapping/unmapping code.
1797 *
1798 *****************************************************************************/
1799
1800/*
1801 * In the dma_ops path we only have the struct device. This function
1802 * finds the corresponding IOMMU, the protection domain and the
1803 * requestor id for a given device.
1804 * If the device is not yet associated with a domain this is also done
1805 * in this function.
1806 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001807static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001808{
Joerg Roedel94f6d192009-11-24 16:40:02 +01001809 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001810 struct dma_ops_domain *dma_dom;
Joerg Roedel94f6d192009-11-24 16:40:02 +01001811 u16 devid = get_device_id(dev);
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001812
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001813 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01001814 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02001815
Joerg Roedel94f6d192009-11-24 16:40:02 +01001816 domain = domain_for_device(dev);
1817 if (domain != NULL && !dma_ops_domain(domain))
1818 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01001819
Joerg Roedel94f6d192009-11-24 16:40:02 +01001820 if (domain != NULL)
1821 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001822
Joerg Roedel15898bb2009-11-24 15:39:42 +01001823 /* Device not bount yet - bind it */
Joerg Roedel94f6d192009-11-24 16:40:02 +01001824 dma_dom = find_protection_domain(devid);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001825 if (!dma_dom)
Joerg Roedel94f6d192009-11-24 16:40:02 +01001826 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1827 attach_device(dev, &dma_dom->domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001828 DUMP_printk("Using protection domain %d for device %s\n",
Joerg Roedel94f6d192009-11-24 16:40:02 +01001829 dma_dom->domain.id, dev_name(dev));
Joerg Roedelf91ba192008-11-25 12:56:12 +01001830
Joerg Roedel94f6d192009-11-24 16:40:02 +01001831 return &dma_dom->domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001832}
1833
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001834static void update_device_table(struct protection_domain *domain)
1835{
Joerg Roedel492667d2009-11-27 13:25:47 +01001836 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001837
Joerg Roedel492667d2009-11-27 13:25:47 +01001838 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001839 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01001840 u16 devid = get_device_id(dev_data->dev);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001841 set_dte_entry(devid, domain, pci_ats_enabled(pdev));
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001842 }
1843}
1844
1845static void update_domain(struct protection_domain *domain)
1846{
1847 if (!domain->updated)
1848 return;
1849
1850 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02001851
1852 domain_flush_devices(domain);
1853 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001854
1855 domain->updated = false;
1856}
1857
Joerg Roedel431b2a22008-07-11 17:14:22 +02001858/*
Joerg Roedel8bda3092009-05-12 12:02:46 +02001859 * This function fetches the PTE for a given address in the aperture
1860 */
1861static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1862 unsigned long address)
1863{
Joerg Roedel384de722009-05-15 12:30:05 +02001864 struct aperture_range *aperture;
Joerg Roedel8bda3092009-05-12 12:02:46 +02001865 u64 *pte, *pte_page;
1866
Joerg Roedel384de722009-05-15 12:30:05 +02001867 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1868 if (!aperture)
1869 return NULL;
1870
1871 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
Joerg Roedel8bda3092009-05-12 12:02:46 +02001872 if (!pte) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001873 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001874 GFP_ATOMIC);
Joerg Roedel384de722009-05-15 12:30:05 +02001875 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1876 } else
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001877 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001878
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001879 update_domain(&dom->domain);
Joerg Roedel8bda3092009-05-12 12:02:46 +02001880
1881 return pte;
1882}
1883
1884/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001885 * This is the generic map function. It maps one 4kb page at paddr to
1886 * the given address in the DMA address space for the domain.
1887 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001888static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001889 unsigned long address,
1890 phys_addr_t paddr,
1891 int direction)
1892{
1893 u64 *pte, __pte;
1894
1895 WARN_ON(address > dom->aperture_size);
1896
1897 paddr &= PAGE_MASK;
1898
Joerg Roedel8bda3092009-05-12 12:02:46 +02001899 pte = dma_ops_get_pte(dom, address);
Joerg Roedel53812c12009-05-12 12:17:38 +02001900 if (!pte)
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001901 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001902
1903 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1904
1905 if (direction == DMA_TO_DEVICE)
1906 __pte |= IOMMU_PTE_IR;
1907 else if (direction == DMA_FROM_DEVICE)
1908 __pte |= IOMMU_PTE_IW;
1909 else if (direction == DMA_BIDIRECTIONAL)
1910 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1911
1912 WARN_ON(*pte);
1913
1914 *pte = __pte;
1915
1916 return (dma_addr_t)address;
1917}
1918
Joerg Roedel431b2a22008-07-11 17:14:22 +02001919/*
1920 * The generic unmapping function for on page in the DMA address space.
1921 */
Joerg Roedel680525e2009-11-23 18:44:42 +01001922static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001923 unsigned long address)
1924{
Joerg Roedel384de722009-05-15 12:30:05 +02001925 struct aperture_range *aperture;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001926 u64 *pte;
1927
1928 if (address >= dom->aperture_size)
1929 return;
1930
Joerg Roedel384de722009-05-15 12:30:05 +02001931 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1932 if (!aperture)
1933 return;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001934
Joerg Roedel384de722009-05-15 12:30:05 +02001935 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1936 if (!pte)
1937 return;
1938
Joerg Roedel8c8c1432009-09-02 17:30:00 +02001939 pte += PM_LEVEL_INDEX(0, address);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001940
1941 WARN_ON(!*pte);
1942
1943 *pte = 0ULL;
1944}
1945
Joerg Roedel431b2a22008-07-11 17:14:22 +02001946/*
1947 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01001948 * contiguous memory region into DMA address space. It is used by all
1949 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001950 * Must be called with the domain lock held.
1951 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02001952static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02001953 struct dma_ops_domain *dma_dom,
1954 phys_addr_t paddr,
1955 size_t size,
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001956 int dir,
Joerg Roedel832a90c2008-09-18 15:54:23 +02001957 bool align,
1958 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02001959{
1960 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02001961 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001962 unsigned int pages;
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001963 unsigned long align_mask = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02001964 int i;
1965
Joerg Roedele3c449f2008-10-15 22:02:11 -07001966 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02001967 paddr &= PAGE_MASK;
1968
Joerg Roedel8ecaf8f2008-12-12 16:13:04 +01001969 INC_STATS_COUNTER(total_map_requests);
1970
Joerg Roedelc1858972008-12-12 15:42:39 +01001971 if (pages > 1)
1972 INC_STATS_COUNTER(cross_page);
1973
Joerg Roedel6d4f3432008-09-04 19:18:02 +02001974 if (align)
1975 align_mask = (1UL << get_order(size)) - 1;
1976
Joerg Roedel11b83882009-05-19 10:23:15 +02001977retry:
Joerg Roedel832a90c2008-09-18 15:54:23 +02001978 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1979 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09001980 if (unlikely(address == DMA_ERROR_CODE)) {
Joerg Roedel11b83882009-05-19 10:23:15 +02001981 /*
1982 * setting next_address here will let the address
1983 * allocator only scan the new allocated range in the
1984 * first run. This is a small optimization.
1985 */
1986 dma_dom->next_address = dma_dom->aperture_size;
1987
Joerg Roedel576175c2009-11-23 19:08:46 +01001988 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
Joerg Roedel11b83882009-05-19 10:23:15 +02001989 goto out;
1990
1991 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001992 * aperture was successfully enlarged by 128 MB, try
Joerg Roedel11b83882009-05-19 10:23:15 +02001993 * allocation again
1994 */
1995 goto retry;
1996 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02001997
1998 start = address;
1999 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002000 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002001 if (ret == DMA_ERROR_CODE)
Joerg Roedel53812c12009-05-12 12:17:38 +02002002 goto out_unmap;
2003
Joerg Roedelcb76c322008-06-26 21:28:00 +02002004 paddr += PAGE_SIZE;
2005 start += PAGE_SIZE;
2006 }
2007 address += offset;
2008
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002009 ADD_STATS_COUNTER(alloced_io_mem, size);
2010
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002011 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002012 domain_flush_tlb(&dma_dom->domain);
Joerg Roedel1c655772008-09-04 18:40:05 +02002013 dma_dom->need_flush = false;
Joerg Roedel318afd42009-11-23 18:32:38 +01002014 } else if (unlikely(amd_iommu_np_cache))
Joerg Roedel17b124b2011-04-06 18:01:35 +02002015 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedel270cab242008-09-04 15:49:46 +02002016
Joerg Roedelcb76c322008-06-26 21:28:00 +02002017out:
2018 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002019
2020out_unmap:
2021
2022 for (--i; i >= 0; --i) {
2023 start -= PAGE_SIZE;
Joerg Roedel680525e2009-11-23 18:44:42 +01002024 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedel53812c12009-05-12 12:17:38 +02002025 }
2026
2027 dma_ops_free_addresses(dma_dom, address, pages);
2028
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002029 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002030}
2031
Joerg Roedel431b2a22008-07-11 17:14:22 +02002032/*
2033 * Does the reverse of the __map_single function. Must be called with
2034 * the domain lock held too
2035 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002036static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002037 dma_addr_t dma_addr,
2038 size_t size,
2039 int dir)
2040{
Joerg Roedel04e04632010-09-23 16:12:48 +02002041 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002042 dma_addr_t i, start;
2043 unsigned int pages;
2044
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002045 if ((dma_addr == DMA_ERROR_CODE) ||
Joerg Roedelb8d99052008-12-08 14:40:26 +01002046 (dma_addr + size > dma_dom->aperture_size))
Joerg Roedelcb76c322008-06-26 21:28:00 +02002047 return;
2048
Joerg Roedel04e04632010-09-23 16:12:48 +02002049 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002050 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002051 dma_addr &= PAGE_MASK;
2052 start = dma_addr;
2053
2054 for (i = 0; i < pages; ++i) {
Joerg Roedel680525e2009-11-23 18:44:42 +01002055 dma_ops_domain_unmap(dma_dom, start);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002056 start += PAGE_SIZE;
2057 }
2058
Joerg Roedel5774f7c2008-12-12 15:57:30 +01002059 SUB_STATS_COUNTER(alloced_io_mem, size);
2060
Joerg Roedelcb76c322008-06-26 21:28:00 +02002061 dma_ops_free_addresses(dma_dom, dma_addr, pages);
Joerg Roedel270cab242008-09-04 15:49:46 +02002062
Joerg Roedel80be3082008-11-06 14:59:05 +01002063 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002064 domain_flush_pages(&dma_dom->domain, flush_addr, size);
Joerg Roedel80be3082008-11-06 14:59:05 +01002065 dma_dom->need_flush = false;
2066 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002067}
2068
Joerg Roedel431b2a22008-07-11 17:14:22 +02002069/*
2070 * The exported map_single function for dma_ops.
2071 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002072static dma_addr_t map_page(struct device *dev, struct page *page,
2073 unsigned long offset, size_t size,
2074 enum dma_data_direction dir,
2075 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002076{
2077 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002078 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002079 dma_addr_t addr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002080 u64 dma_mask;
FUJITA Tomonori51491362009-01-05 23:47:25 +09002081 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002082
Joerg Roedel0f2a86f2008-12-12 15:05:16 +01002083 INC_STATS_COUNTER(cnt_map_single);
2084
Joerg Roedel94f6d192009-11-24 16:40:02 +01002085 domain = get_domain(dev);
2086 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002087 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002088 else if (IS_ERR(domain))
2089 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002090
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002091 dma_mask = *dev->dma_mask;
2092
Joerg Roedel4da70b92008-06-26 21:28:01 +02002093 spin_lock_irqsave(&domain->lock, flags);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002094
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002095 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002096 dma_mask);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002097 if (addr == DMA_ERROR_CODE)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002098 goto out;
2099
Joerg Roedel17b124b2011-04-06 18:01:35 +02002100 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002101
2102out:
2103 spin_unlock_irqrestore(&domain->lock, flags);
2104
2105 return addr;
2106}
2107
Joerg Roedel431b2a22008-07-11 17:14:22 +02002108/*
2109 * The exported unmap_single function for dma_ops.
2110 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002111static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2112 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002113{
2114 unsigned long flags;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002115 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002116
Joerg Roedel146a6912008-12-12 15:07:12 +01002117 INC_STATS_COUNTER(cnt_unmap_single);
2118
Joerg Roedel94f6d192009-11-24 16:40:02 +01002119 domain = get_domain(dev);
2120 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002121 return;
2122
Joerg Roedel4da70b92008-06-26 21:28:01 +02002123 spin_lock_irqsave(&domain->lock, flags);
2124
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002125 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002126
Joerg Roedel17b124b2011-04-06 18:01:35 +02002127 domain_flush_complete(domain);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002128
2129 spin_unlock_irqrestore(&domain->lock, flags);
2130}
2131
Joerg Roedel431b2a22008-07-11 17:14:22 +02002132/*
2133 * This is a special map_sg function which is used if we should map a
2134 * device which is not handled by an AMD IOMMU in the system.
2135 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002136static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2137 int nelems, int dir)
2138{
2139 struct scatterlist *s;
2140 int i;
2141
2142 for_each_sg(sglist, s, nelems, i) {
2143 s->dma_address = (dma_addr_t)sg_phys(s);
2144 s->dma_length = s->length;
2145 }
2146
2147 return nelems;
2148}
2149
Joerg Roedel431b2a22008-07-11 17:14:22 +02002150/*
2151 * The exported map_sg function for dma_ops (handles scatter-gather
2152 * lists).
2153 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002154static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002155 int nelems, enum dma_data_direction dir,
2156 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002157{
2158 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002159 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002160 int i;
2161 struct scatterlist *s;
2162 phys_addr_t paddr;
2163 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002164 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002165
Joerg Roedeld03f0672008-12-12 15:09:48 +01002166 INC_STATS_COUNTER(cnt_map_sg);
2167
Joerg Roedel94f6d192009-11-24 16:40:02 +01002168 domain = get_domain(dev);
2169 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002170 return map_sg_no_iommu(dev, sglist, nelems, dir);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002171 else if (IS_ERR(domain))
2172 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002173
Joerg Roedel832a90c2008-09-18 15:54:23 +02002174 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002175
Joerg Roedel65b050a2008-06-26 21:28:02 +02002176 spin_lock_irqsave(&domain->lock, flags);
2177
2178 for_each_sg(sglist, s, nelems, i) {
2179 paddr = sg_phys(s);
2180
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002181 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002182 paddr, s->length, dir, false,
2183 dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002184
2185 if (s->dma_address) {
2186 s->dma_length = s->length;
2187 mapped_elems++;
2188 } else
2189 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002190 }
2191
Joerg Roedel17b124b2011-04-06 18:01:35 +02002192 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002193
2194out:
2195 spin_unlock_irqrestore(&domain->lock, flags);
2196
2197 return mapped_elems;
2198unmap:
2199 for_each_sg(sglist, s, mapped_elems, i) {
2200 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002201 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002202 s->dma_length, dir);
2203 s->dma_address = s->dma_length = 0;
2204 }
2205
2206 mapped_elems = 0;
2207
2208 goto out;
2209}
2210
Joerg Roedel431b2a22008-07-11 17:14:22 +02002211/*
2212 * The exported map_sg function for dma_ops (handles scatter-gather
2213 * lists).
2214 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002215static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002216 int nelems, enum dma_data_direction dir,
2217 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002218{
2219 unsigned long flags;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002220 struct protection_domain *domain;
2221 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002222 int i;
2223
Joerg Roedel55877a62008-12-12 15:12:14 +01002224 INC_STATS_COUNTER(cnt_unmap_sg);
2225
Joerg Roedel94f6d192009-11-24 16:40:02 +01002226 domain = get_domain(dev);
2227 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002228 return;
2229
Joerg Roedel65b050a2008-06-26 21:28:02 +02002230 spin_lock_irqsave(&domain->lock, flags);
2231
2232 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002233 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002234 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002235 s->dma_address = s->dma_length = 0;
2236 }
2237
Joerg Roedel17b124b2011-04-06 18:01:35 +02002238 domain_flush_complete(domain);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002239
2240 spin_unlock_irqrestore(&domain->lock, flags);
2241}
2242
Joerg Roedel431b2a22008-07-11 17:14:22 +02002243/*
2244 * The exported alloc_coherent function for dma_ops.
2245 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002246static void *alloc_coherent(struct device *dev, size_t size,
2247 dma_addr_t *dma_addr, gfp_t flag)
2248{
2249 unsigned long flags;
2250 void *virt_addr;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002251 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002252 phys_addr_t paddr;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002253 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002254
Joerg Roedelc8f0fb32008-12-12 15:14:21 +01002255 INC_STATS_COUNTER(cnt_alloc_coherent);
2256
Joerg Roedel94f6d192009-11-24 16:40:02 +01002257 domain = get_domain(dev);
2258 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002259 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2260 *dma_addr = __pa(virt_addr);
2261 return virt_addr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002262 } else if (IS_ERR(domain))
2263 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002264
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002265 dma_mask = dev->coherent_dma_mask;
2266 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2267 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002268
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002269 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2270 if (!virt_addr)
Jaswinder Singh Rajputb25ae672009-07-01 19:53:14 +05302271 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002272
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002273 paddr = virt_to_phys(virt_addr);
2274
Joerg Roedel832a90c2008-09-18 15:54:23 +02002275 if (!dma_mask)
2276 dma_mask = *dev->dma_mask;
2277
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002278 spin_lock_irqsave(&domain->lock, flags);
2279
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002280 *dma_addr = __map_single(dev, domain->priv, paddr,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002281 size, DMA_BIDIRECTIONAL, true, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002282
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002283 if (*dma_addr == DMA_ERROR_CODE) {
Jiri Slaby367d04c2009-05-28 09:54:48 +02002284 spin_unlock_irqrestore(&domain->lock, flags);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002285 goto out_free;
Jiri Slaby367d04c2009-05-28 09:54:48 +02002286 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002287
Joerg Roedel17b124b2011-04-06 18:01:35 +02002288 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002289
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002290 spin_unlock_irqrestore(&domain->lock, flags);
2291
2292 return virt_addr;
Joerg Roedel5b28df62008-12-02 17:49:42 +01002293
2294out_free:
2295
2296 free_pages((unsigned long)virt_addr, get_order(size));
2297
2298 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002299}
2300
Joerg Roedel431b2a22008-07-11 17:14:22 +02002301/*
2302 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002303 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002304static void free_coherent(struct device *dev, size_t size,
2305 void *virt_addr, dma_addr_t dma_addr)
2306{
2307 unsigned long flags;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002308 struct protection_domain *domain;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002309
Joerg Roedel5d31ee72008-12-12 15:16:38 +01002310 INC_STATS_COUNTER(cnt_free_coherent);
2311
Joerg Roedel94f6d192009-11-24 16:40:02 +01002312 domain = get_domain(dev);
2313 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002314 goto free_mem;
2315
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002316 spin_lock_irqsave(&domain->lock, flags);
2317
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002318 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002319
Joerg Roedel17b124b2011-04-06 18:01:35 +02002320 domain_flush_complete(domain);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002321
2322 spin_unlock_irqrestore(&domain->lock, flags);
2323
2324free_mem:
2325 free_pages((unsigned long)virt_addr, get_order(size));
2326}
2327
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002328/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002329 * This function is called by the DMA layer to find out if we can handle a
2330 * particular device. It is part of the dma_ops.
2331 */
2332static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2333{
Joerg Roedel420aef82009-11-23 16:14:57 +01002334 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002335}
2336
2337/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002338 * The function for pre-allocating protection domains.
2339 *
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002340 * If the driver core informs the DMA layer if a driver grabs a device
2341 * we don't need to preallocate the protection domains anymore.
2342 * For now we have to.
2343 */
Jaswinder Singh Rajput0e93dd82008-12-29 21:45:22 +05302344static void prealloc_protection_domains(void)
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002345{
2346 struct pci_dev *dev = NULL;
2347 struct dma_ops_domain *dma_dom;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002348 u16 devid;
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002349
Chris Wrightd18c69d2010-04-02 18:27:55 -07002350 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002351
2352 /* Do we handle this device? */
2353 if (!check_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002354 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002355
2356 /* Is there already any domain for it? */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002357 if (domain_for_device(&dev->dev))
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002358 continue;
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002359
2360 devid = get_device_id(&dev->dev);
2361
Joerg Roedel87a64d52009-11-24 17:26:43 +01002362 dma_dom = dma_ops_domain_alloc();
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002363 if (!dma_dom)
2364 continue;
2365 init_unity_mappings_for_device(dma_dom, devid);
Joerg Roedelbd60b732008-09-11 10:24:48 +02002366 dma_dom->target_dev = devid;
2367
Joerg Roedel15898bb2009-11-24 15:39:42 +01002368 attach_device(&dev->dev, &dma_dom->domain);
Joerg Roedelbe831292009-11-23 12:50:00 +01002369
Joerg Roedelbd60b732008-09-11 10:24:48 +02002370 list_add_tail(&dma_dom->list, &iommu_pd_list);
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002371 }
2372}
2373
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002374static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002375 .alloc_coherent = alloc_coherent,
2376 .free_coherent = free_coherent,
FUJITA Tomonori51491362009-01-05 23:47:25 +09002377 .map_page = map_page,
2378 .unmap_page = unmap_page,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002379 .map_sg = map_sg,
2380 .unmap_sg = unmap_sg,
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002381 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002382};
2383
Joerg Roedel431b2a22008-07-11 17:14:22 +02002384/*
2385 * The function which clues the AMD IOMMU driver into dma_ops.
2386 */
Joerg Roedelf5325092010-01-22 17:44:35 +01002387
2388void __init amd_iommu_init_api(void)
2389{
2390 register_iommu(&amd_iommu_ops);
2391}
2392
Joerg Roedel6631ee92008-06-26 21:28:05 +02002393int __init amd_iommu_init_dma_ops(void)
2394{
2395 struct amd_iommu *iommu;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002396 int ret;
2397
Joerg Roedel431b2a22008-07-11 17:14:22 +02002398 /*
2399 * first allocate a default protection domain for every IOMMU we
2400 * found in the system. Devices not assigned to any other
2401 * protection domain will be assigned to the default one.
2402 */
Joerg Roedel3bd22172009-05-04 15:06:20 +02002403 for_each_iommu(iommu) {
Joerg Roedel87a64d52009-11-24 17:26:43 +01002404 iommu->default_dom = dma_ops_domain_alloc();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002405 if (iommu->default_dom == NULL)
2406 return -ENOMEM;
Joerg Roedele2dc14a2008-12-10 18:48:59 +01002407 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002408 ret = iommu_init_unity_mappings(iommu);
2409 if (ret)
2410 goto free_domains;
2411 }
2412
Joerg Roedel431b2a22008-07-11 17:14:22 +02002413 /*
Joerg Roedel8793abe2009-11-27 11:40:33 +01002414 * Pre-allocate the protection domains for each device.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002415 */
Joerg Roedel8793abe2009-11-27 11:40:33 +01002416 prealloc_protection_domains();
Joerg Roedel6631ee92008-06-26 21:28:05 +02002417
2418 iommu_detected = 1;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002419 swiotlb = 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002420
Joerg Roedel431b2a22008-07-11 17:14:22 +02002421 /* Make the driver finally visible to the drivers */
Joerg Roedel6631ee92008-06-26 21:28:05 +02002422 dma_ops = &amd_iommu_dma_ops;
2423
Joerg Roedel7f265082008-12-12 13:50:21 +01002424 amd_iommu_stats_init();
2425
Joerg Roedel6631ee92008-06-26 21:28:05 +02002426 return 0;
2427
2428free_domains:
2429
Joerg Roedel3bd22172009-05-04 15:06:20 +02002430 for_each_iommu(iommu) {
Joerg Roedel6631ee92008-06-26 21:28:05 +02002431 if (iommu->default_dom)
2432 dma_ops_domain_free(iommu->default_dom);
2433 }
2434
2435 return ret;
2436}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002437
2438/*****************************************************************************
2439 *
2440 * The following functions belong to the exported interface of AMD IOMMU
2441 *
2442 * This interface allows access to lower level functions of the IOMMU
2443 * like protection domain handling and assignement of devices to domains
2444 * which is not possible with the dma_ops interface.
2445 *
2446 *****************************************************************************/
2447
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002448static void cleanup_domain(struct protection_domain *domain)
2449{
Joerg Roedel492667d2009-11-27 13:25:47 +01002450 struct iommu_dev_data *dev_data, *next;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002451 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002452
2453 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2454
Joerg Roedel492667d2009-11-27 13:25:47 +01002455 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2456 struct device *dev = dev_data->dev;
2457
Chris Wright04e856c2010-02-17 08:51:20 -08002458 __detach_device(dev);
Joerg Roedel492667d2009-11-27 13:25:47 +01002459 atomic_set(&dev_data->bind, 0);
2460 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002461
2462 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2463}
2464
Joerg Roedel26508152009-08-26 16:52:40 +02002465static void protection_domain_free(struct protection_domain *domain)
2466{
2467 if (!domain)
2468 return;
2469
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002470 del_domain_from_list(domain);
2471
Joerg Roedel26508152009-08-26 16:52:40 +02002472 if (domain->id)
2473 domain_id_free(domain->id);
2474
2475 kfree(domain);
2476}
2477
2478static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002479{
2480 struct protection_domain *domain;
2481
2482 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2483 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002484 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002485
2486 spin_lock_init(&domain->lock);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002487 mutex_init(&domain->api_lock);
Joerg Roedelc156e342008-12-02 18:13:27 +01002488 domain->id = domain_id_alloc();
2489 if (!domain->id)
Joerg Roedel26508152009-08-26 16:52:40 +02002490 goto out_err;
Joerg Roedel7c392cb2009-11-26 11:13:32 +01002491 INIT_LIST_HEAD(&domain->dev_list);
Joerg Roedel26508152009-08-26 16:52:40 +02002492
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002493 add_domain_to_list(domain);
2494
Joerg Roedel26508152009-08-26 16:52:40 +02002495 return domain;
2496
2497out_err:
2498 kfree(domain);
2499
2500 return NULL;
2501}
2502
2503static int amd_iommu_domain_init(struct iommu_domain *dom)
2504{
2505 struct protection_domain *domain;
2506
2507 domain = protection_domain_alloc();
2508 if (!domain)
Joerg Roedelc156e342008-12-02 18:13:27 +01002509 goto out_free;
Joerg Roedel26508152009-08-26 16:52:40 +02002510
2511 domain->mode = PAGE_MODE_3_LEVEL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002512 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2513 if (!domain->pt_root)
2514 goto out_free;
2515
2516 dom->priv = domain;
2517
2518 return 0;
2519
2520out_free:
Joerg Roedel26508152009-08-26 16:52:40 +02002521 protection_domain_free(domain);
Joerg Roedelc156e342008-12-02 18:13:27 +01002522
2523 return -ENOMEM;
2524}
2525
Joerg Roedel98383fc2008-12-02 18:34:12 +01002526static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2527{
2528 struct protection_domain *domain = dom->priv;
2529
2530 if (!domain)
2531 return;
2532
2533 if (domain->dev_cnt > 0)
2534 cleanup_domain(domain);
2535
2536 BUG_ON(domain->dev_cnt != 0);
2537
2538 free_pagetable(domain);
2539
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002540 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002541
2542 dom->priv = NULL;
2543}
2544
Joerg Roedel684f2882008-12-08 12:07:44 +01002545static void amd_iommu_detach_device(struct iommu_domain *dom,
2546 struct device *dev)
2547{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002548 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002549 struct amd_iommu *iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002550 u16 devid;
2551
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002552 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002553 return;
2554
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002555 devid = get_device_id(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002556
Joerg Roedel657cbb62009-11-23 15:26:46 +01002557 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002558 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002559
2560 iommu = amd_iommu_rlookup_table[devid];
2561 if (!iommu)
2562 return;
2563
Joerg Roedeld8c13082011-04-06 18:51:26 +02002564 device_flush_dte(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002565 iommu_completion_wait(iommu);
2566}
2567
Joerg Roedel01106062008-12-02 19:34:11 +01002568static int amd_iommu_attach_device(struct iommu_domain *dom,
2569 struct device *dev)
2570{
2571 struct protection_domain *domain = dom->priv;
Joerg Roedel657cbb62009-11-23 15:26:46 +01002572 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002573 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002574 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002575 u16 devid;
2576
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002577 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002578 return -EINVAL;
2579
Joerg Roedel657cbb62009-11-23 15:26:46 +01002580 dev_data = dev->archdata.iommu;
2581
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002582 devid = get_device_id(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002583
2584 iommu = amd_iommu_rlookup_table[devid];
2585 if (!iommu)
2586 return -EINVAL;
2587
Joerg Roedel657cbb62009-11-23 15:26:46 +01002588 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002589 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002590
Joerg Roedel15898bb2009-11-24 15:39:42 +01002591 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002592
2593 iommu_completion_wait(iommu);
2594
Joerg Roedel15898bb2009-11-24 15:39:42 +01002595 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002596}
2597
Joerg Roedel468e2362010-01-21 16:37:36 +01002598static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2599 phys_addr_t paddr, int gfp_order, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002600{
Joerg Roedel468e2362010-01-21 16:37:36 +01002601 unsigned long page_size = 0x1000UL << gfp_order;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002602 struct protection_domain *domain = dom->priv;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002603 int prot = 0;
2604 int ret;
2605
2606 if (iommu_prot & IOMMU_READ)
2607 prot |= IOMMU_PROT_IR;
2608 if (iommu_prot & IOMMU_WRITE)
2609 prot |= IOMMU_PROT_IW;
2610
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002611 mutex_lock(&domain->api_lock);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002612 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002613 mutex_unlock(&domain->api_lock);
2614
Joerg Roedel795e74f2010-05-11 17:40:57 +02002615 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002616}
2617
Joerg Roedel468e2362010-01-21 16:37:36 +01002618static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2619 int gfp_order)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002620{
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002621 struct protection_domain *domain = dom->priv;
Joerg Roedel468e2362010-01-21 16:37:36 +01002622 unsigned long page_size, unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002623
Joerg Roedel468e2362010-01-21 16:37:36 +01002624 page_size = 0x1000UL << gfp_order;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002625
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002626 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002627 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02002628 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002629
Joerg Roedel17b124b2011-04-06 18:01:35 +02002630 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002631
Joerg Roedel468e2362010-01-21 16:37:36 +01002632 return get_order(unmap_size);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002633}
2634
Joerg Roedel645c4c82008-12-02 20:05:50 +01002635static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2636 unsigned long iova)
2637{
2638 struct protection_domain *domain = dom->priv;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002639 unsigned long offset_mask;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002640 phys_addr_t paddr;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002641 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002642
Joerg Roedel24cd7722010-01-19 17:27:39 +01002643 pte = fetch_pte(domain, iova);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002644
Joerg Roedela6d41a42009-09-02 17:08:55 +02002645 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002646 return 0;
2647
Joerg Roedelf03152b2010-01-21 16:15:24 +01002648 if (PM_PTE_LEVEL(*pte) == 0)
2649 offset_mask = PAGE_SIZE - 1;
2650 else
2651 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2652
2653 __pte = *pte & PM_ADDR_MASK;
2654 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002655
2656 return paddr;
2657}
2658
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002659static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2660 unsigned long cap)
2661{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002662 switch (cap) {
2663 case IOMMU_CAP_CACHE_COHERENCY:
2664 return 1;
2665 }
2666
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002667 return 0;
2668}
2669
Joerg Roedel26961ef2008-12-03 17:00:17 +01002670static struct iommu_ops amd_iommu_ops = {
2671 .domain_init = amd_iommu_domain_init,
2672 .domain_destroy = amd_iommu_domain_destroy,
2673 .attach_dev = amd_iommu_attach_device,
2674 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002675 .map = amd_iommu_map,
2676 .unmap = amd_iommu_unmap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002677 .iova_to_phys = amd_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002678 .domain_has_cap = amd_iommu_domain_has_cap,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002679};
2680
Joerg Roedel0feae532009-08-26 15:26:30 +02002681/*****************************************************************************
2682 *
2683 * The next functions do a basic initialization of IOMMU for pass through
2684 * mode
2685 *
2686 * In passthrough mode the IOMMU is initialized and enabled but not used for
2687 * DMA-API translation.
2688 *
2689 *****************************************************************************/
2690
2691int __init amd_iommu_init_passthrough(void)
2692{
Joerg Roedel15898bb2009-11-24 15:39:42 +01002693 struct amd_iommu *iommu;
Joerg Roedel0feae532009-08-26 15:26:30 +02002694 struct pci_dev *dev = NULL;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002695 u16 devid;
Joerg Roedel0feae532009-08-26 15:26:30 +02002696
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002697 /* allocate passthrough domain */
Joerg Roedel0feae532009-08-26 15:26:30 +02002698 pt_domain = protection_domain_alloc();
2699 if (!pt_domain)
2700 return -ENOMEM;
2701
2702 pt_domain->mode |= PAGE_MODE_NONE;
2703
Kulikov Vasiliy6c54aab2010-07-03 12:03:51 -04002704 for_each_pci_dev(dev) {
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002705 if (!check_device(&dev->dev))
Joerg Roedel0feae532009-08-26 15:26:30 +02002706 continue;
2707
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002708 devid = get_device_id(&dev->dev);
2709
Joerg Roedel15898bb2009-11-24 15:39:42 +01002710 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedel0feae532009-08-26 15:26:30 +02002711 if (!iommu)
2712 continue;
2713
Joerg Roedel15898bb2009-11-24 15:39:42 +01002714 attach_device(&dev->dev, pt_domain);
Joerg Roedel0feae532009-08-26 15:26:30 +02002715 }
2716
2717 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2718
2719 return 0;
2720}