]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/amd_iommu.c
x86/amd-iommu: Move find_protection_domain to helper functions
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / amd_iommu.c
CommitLineData
b6c02715 1/*
bf3118c1 2 * Copyright (C) 2007-2009 Advanced Micro Devices, Inc.
b6c02715
JR
3 * 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>
21#include <linux/gfp.h>
22#include <linux/bitops.h>
7f26508b 23#include <linux/debugfs.h>
b6c02715 24#include <linux/scatterlist.h>
51491367 25#include <linux/dma-mapping.h>
b6c02715 26#include <linux/iommu-helper.h>
c156e347 27#include <linux/iommu.h>
b6c02715 28#include <asm/proto.h>
46a7fa27 29#include <asm/iommu.h>
1d9b16d1 30#include <asm/gart.h>
6a9401a7 31#include <asm/amd_iommu_proto.h>
b6c02715 32#include <asm/amd_iommu_types.h>
c6da992e 33#include <asm/amd_iommu.h>
b6c02715
JR
34
35#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
36
136f78a1
JR
37#define EXIT_LOOP_COUNT 10000000
38
b6c02715
JR
39static DEFINE_RWLOCK(amd_iommu_devtable_lock);
40
bd60b735
JR
41/* A list of preallocated protection domains */
42static LIST_HEAD(iommu_pd_list);
43static DEFINE_SPINLOCK(iommu_pd_list_lock);
44
0feae533
JR
45/*
46 * Domain for untranslated devices - only allocated
47 * if iommu=pt passed on kernel cmd line.
48 */
49static struct protection_domain *pt_domain;
50
26961efe 51static struct iommu_ops amd_iommu_ops;
26961efe 52
431b2a20
JR
53/*
54 * general struct to manage commands send to an IOMMU
55 */
d6449536 56struct iommu_cmd {
b6c02715
JR
57 u32 data[4];
58};
59
bd0e5211
JR
60static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
61 struct unity_map_entry *e);
8bc3e127 62static u64 *alloc_pte(struct protection_domain *domain,
abdc5eb3
JR
63 unsigned long address, int end_lvl,
64 u64 **pte_page, gfp_t gfp);
00cd122a
JR
65static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
66 unsigned long start_page,
67 unsigned int pages);
a345b23b 68static void reset_iommu_command_buffer(struct amd_iommu *iommu);
9355a081 69static u64 *fetch_pte(struct protection_domain *domain,
a6b256b4 70 unsigned long address, int map_size);
04bfdd84 71static void update_domain(struct protection_domain *domain);
c1eee67b 72
15898bbc
JR
73/****************************************************************************
74 *
75 * Helper functions
76 *
77 ****************************************************************************/
78
79static inline u16 get_device_id(struct device *dev)
80{
81 struct pci_dev *pdev = to_pci_dev(dev);
82
83 return calc_devid(pdev->bus->number, pdev->devfn);
84}
85
71c70984
JR
86/*
87 * In this function the list of preallocated protection domains is traversed to
88 * find the domain for a specific device
89 */
90static struct dma_ops_domain *find_protection_domain(u16 devid)
91{
92 struct dma_ops_domain *entry, *ret = NULL;
93 unsigned long flags;
94 u16 alias = amd_iommu_alias_table[devid];
95
96 if (list_empty(&iommu_pd_list))
97 return NULL;
98
99 spin_lock_irqsave(&iommu_pd_list_lock, flags);
100
101 list_for_each_entry(entry, &iommu_pd_list, list) {
102 if (entry->target_dev == devid ||
103 entry->target_dev == alias) {
104 ret = entry;
105 break;
106 }
107 }
108
109 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
110
111 return ret;
112}
113
7f26508b
JR
114#ifdef CONFIG_AMD_IOMMU_STATS
115
116/*
117 * Initialization code for statistics collection
118 */
119
da49f6df 120DECLARE_STATS_COUNTER(compl_wait);
0f2a86f2 121DECLARE_STATS_COUNTER(cnt_map_single);
146a6917 122DECLARE_STATS_COUNTER(cnt_unmap_single);
d03f067a 123DECLARE_STATS_COUNTER(cnt_map_sg);
55877a6b 124DECLARE_STATS_COUNTER(cnt_unmap_sg);
c8f0fb36 125DECLARE_STATS_COUNTER(cnt_alloc_coherent);
5d31ee7e 126DECLARE_STATS_COUNTER(cnt_free_coherent);
c1858976 127DECLARE_STATS_COUNTER(cross_page);
f57d98ae 128DECLARE_STATS_COUNTER(domain_flush_single);
18811f55 129DECLARE_STATS_COUNTER(domain_flush_all);
5774f7c5 130DECLARE_STATS_COUNTER(alloced_io_mem);
8ecaf8f1 131DECLARE_STATS_COUNTER(total_map_requests);
da49f6df 132
7f26508b
JR
133static struct dentry *stats_dir;
134static struct dentry *de_isolate;
135static struct dentry *de_fflush;
136
137static void amd_iommu_stats_add(struct __iommu_counter *cnt)
138{
139 if (stats_dir == NULL)
140 return;
141
142 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
143 &cnt->value);
144}
145
146static void amd_iommu_stats_init(void)
147{
148 stats_dir = debugfs_create_dir("amd-iommu", NULL);
149 if (stats_dir == NULL)
150 return;
151
152 de_isolate = debugfs_create_bool("isolation", 0444, stats_dir,
153 (u32 *)&amd_iommu_isolate);
154
155 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
156 (u32 *)&amd_iommu_unmap_flush);
da49f6df
JR
157
158 amd_iommu_stats_add(&compl_wait);
0f2a86f2 159 amd_iommu_stats_add(&cnt_map_single);
146a6917 160 amd_iommu_stats_add(&cnt_unmap_single);
d03f067a 161 amd_iommu_stats_add(&cnt_map_sg);
55877a6b 162 amd_iommu_stats_add(&cnt_unmap_sg);
c8f0fb36 163 amd_iommu_stats_add(&cnt_alloc_coherent);
5d31ee7e 164 amd_iommu_stats_add(&cnt_free_coherent);
c1858976 165 amd_iommu_stats_add(&cross_page);
f57d98ae 166 amd_iommu_stats_add(&domain_flush_single);
18811f55 167 amd_iommu_stats_add(&domain_flush_all);
5774f7c5 168 amd_iommu_stats_add(&alloced_io_mem);
8ecaf8f1 169 amd_iommu_stats_add(&total_map_requests);
7f26508b
JR
170}
171
172#endif
173
a80dc3e0
JR
174/****************************************************************************
175 *
176 * Interrupt handling functions
177 *
178 ****************************************************************************/
179
e3e59876
JR
180static void dump_dte_entry(u16 devid)
181{
182 int i;
183
184 for (i = 0; i < 8; ++i)
185 pr_err("AMD-Vi: DTE[%d]: %08x\n", i,
186 amd_iommu_dev_table[devid].data[i]);
187}
188
945b4ac4
JR
189static void dump_command(unsigned long phys_addr)
190{
191 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
192 int i;
193
194 for (i = 0; i < 4; ++i)
195 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
196}
197
a345b23b 198static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
90008ee4
JR
199{
200 u32 *event = __evt;
201 int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
202 int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
203 int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
204 int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
205 u64 address = (u64)(((u64)event[3]) << 32) | event[2];
206
4c6f40d4 207 printk(KERN_ERR "AMD-Vi: Event logged [");
90008ee4
JR
208
209 switch (type) {
210 case EVENT_TYPE_ILL_DEV:
211 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
212 "address=0x%016llx flags=0x%04x]\n",
213 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
214 address, flags);
e3e59876 215 dump_dte_entry(devid);
90008ee4
JR
216 break;
217 case EVENT_TYPE_IO_FAULT:
218 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
219 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
220 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
221 domid, address, flags);
222 break;
223 case EVENT_TYPE_DEV_TAB_ERR:
224 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
225 "address=0x%016llx flags=0x%04x]\n",
226 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
227 address, flags);
228 break;
229 case EVENT_TYPE_PAGE_TAB_ERR:
230 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
231 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
232 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
233 domid, address, flags);
234 break;
235 case EVENT_TYPE_ILL_CMD:
236 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
a345b23b 237 reset_iommu_command_buffer(iommu);
945b4ac4 238 dump_command(address);
90008ee4
JR
239 break;
240 case EVENT_TYPE_CMD_HARD_ERR:
241 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
242 "flags=0x%04x]\n", address, flags);
243 break;
244 case EVENT_TYPE_IOTLB_INV_TO:
245 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
246 "address=0x%016llx]\n",
247 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
248 address);
249 break;
250 case EVENT_TYPE_INV_DEV_REQ:
251 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
252 "address=0x%016llx flags=0x%04x]\n",
253 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
254 address, flags);
255 break;
256 default:
257 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
258 }
259}
260
261static void iommu_poll_events(struct amd_iommu *iommu)
262{
263 u32 head, tail;
264 unsigned long flags;
265
266 spin_lock_irqsave(&iommu->lock, flags);
267
268 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
269 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
270
271 while (head != tail) {
a345b23b 272 iommu_print_event(iommu, iommu->evt_buf + head);
90008ee4
JR
273 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
274 }
275
276 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
277
278 spin_unlock_irqrestore(&iommu->lock, flags);
279}
280
a80dc3e0
JR
281irqreturn_t amd_iommu_int_handler(int irq, void *data)
282{
90008ee4
JR
283 struct amd_iommu *iommu;
284
3bd22172 285 for_each_iommu(iommu)
90008ee4
JR
286 iommu_poll_events(iommu);
287
288 return IRQ_HANDLED;
a80dc3e0
JR
289}
290
431b2a20
JR
291/****************************************************************************
292 *
293 * IOMMU command queuing functions
294 *
295 ****************************************************************************/
296
297/*
298 * Writes the command to the IOMMUs command buffer and informs the
299 * hardware about the new command. Must be called with iommu->lock held.
300 */
d6449536 301static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
a19ae1ec
JR
302{
303 u32 tail, head;
304 u8 *target;
305
306 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
8a7c5ef3 307 target = iommu->cmd_buf + tail;
a19ae1ec
JR
308 memcpy_toio(target, cmd, sizeof(*cmd));
309 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
310 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
311 if (tail == head)
312 return -ENOMEM;
313 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
314
315 return 0;
316}
317
431b2a20
JR
318/*
319 * General queuing function for commands. Takes iommu->lock and calls
320 * __iommu_queue_command().
321 */
d6449536 322static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
a19ae1ec
JR
323{
324 unsigned long flags;
325 int ret;
326
327 spin_lock_irqsave(&iommu->lock, flags);
328 ret = __iommu_queue_command(iommu, cmd);
09ee17eb 329 if (!ret)
0cfd7aa9 330 iommu->need_sync = true;
a19ae1ec
JR
331 spin_unlock_irqrestore(&iommu->lock, flags);
332
333 return ret;
334}
335
8d201968
JR
336/*
337 * This function waits until an IOMMU has completed a completion
338 * wait command
339 */
340static void __iommu_wait_for_completion(struct amd_iommu *iommu)
341{
342 int ready = 0;
343 unsigned status = 0;
344 unsigned long i = 0;
345
da49f6df
JR
346 INC_STATS_COUNTER(compl_wait);
347
8d201968
JR
348 while (!ready && (i < EXIT_LOOP_COUNT)) {
349 ++i;
350 /* wait for the bit to become one */
351 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
352 ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
353 }
354
355 /* set bit back to zero */
356 status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
357 writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
358
6a1eddd2
JR
359 if (unlikely(i == EXIT_LOOP_COUNT)) {
360 spin_unlock(&iommu->lock);
361 reset_iommu_command_buffer(iommu);
362 spin_lock(&iommu->lock);
363 }
8d201968
JR
364}
365
366/*
367 * This function queues a completion wait command into the command
368 * buffer of an IOMMU
369 */
370static int __iommu_completion_wait(struct amd_iommu *iommu)
371{
372 struct iommu_cmd cmd;
373
374 memset(&cmd, 0, sizeof(cmd));
375 cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
376 CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
377
378 return __iommu_queue_command(iommu, &cmd);
379}
380
431b2a20
JR
381/*
382 * This function is called whenever we need to ensure that the IOMMU has
383 * completed execution of all commands we sent. It sends a
384 * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
385 * us about that by writing a value to a physical address we pass with
386 * the command.
387 */
a19ae1ec
JR
388static int iommu_completion_wait(struct amd_iommu *iommu)
389{
8d201968
JR
390 int ret = 0;
391 unsigned long flags;
a19ae1ec 392
7e4f88da
JR
393 spin_lock_irqsave(&iommu->lock, flags);
394
09ee17eb
JR
395 if (!iommu->need_sync)
396 goto out;
397
8d201968 398 ret = __iommu_completion_wait(iommu);
09ee17eb 399
0cfd7aa9 400 iommu->need_sync = false;
a19ae1ec
JR
401
402 if (ret)
7e4f88da 403 goto out;
a19ae1ec 404
8d201968 405 __iommu_wait_for_completion(iommu);
84df8175 406
7e4f88da
JR
407out:
408 spin_unlock_irqrestore(&iommu->lock, flags);
a19ae1ec
JR
409
410 return 0;
411}
412
0518a3a4
JR
413static void iommu_flush_complete(struct protection_domain *domain)
414{
415 int i;
416
417 for (i = 0; i < amd_iommus_present; ++i) {
418 if (!domain->dev_iommu[i])
419 continue;
420
421 /*
422 * Devices of this domain are behind this IOMMU
423 * We need to wait for completion of all commands.
424 */
425 iommu_completion_wait(amd_iommus[i]);
426 }
427}
428
431b2a20
JR
429/*
430 * Command send function for invalidating a device table entry
431 */
a19ae1ec
JR
432static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
433{
d6449536 434 struct iommu_cmd cmd;
ee2fa743 435 int ret;
a19ae1ec
JR
436
437 BUG_ON(iommu == NULL);
438
439 memset(&cmd, 0, sizeof(cmd));
440 CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
441 cmd.data[0] = devid;
442
ee2fa743
JR
443 ret = iommu_queue_command(iommu, &cmd);
444
ee2fa743 445 return ret;
a19ae1ec
JR
446}
447
237b6f33
JR
448static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
449 u16 domid, int pde, int s)
450{
451 memset(cmd, 0, sizeof(*cmd));
452 address &= PAGE_MASK;
453 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
454 cmd->data[1] |= domid;
455 cmd->data[2] = lower_32_bits(address);
456 cmd->data[3] = upper_32_bits(address);
457 if (s) /* size bit - we flush more than one 4kb page */
458 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
459 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
460 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
461}
462
431b2a20
JR
463/*
464 * Generic command send function for invalidaing TLB entries
465 */
a19ae1ec
JR
466static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
467 u64 address, u16 domid, int pde, int s)
468{
d6449536 469 struct iommu_cmd cmd;
ee2fa743 470 int ret;
a19ae1ec 471
237b6f33 472 __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
a19ae1ec 473
ee2fa743
JR
474 ret = iommu_queue_command(iommu, &cmd);
475
ee2fa743 476 return ret;
a19ae1ec
JR
477}
478
431b2a20
JR
479/*
480 * TLB invalidation function which is called from the mapping functions.
481 * It invalidates a single PTE if the range to flush is within a single
482 * page. Otherwise it flushes the whole TLB of the IOMMU.
483 */
6de8ad9b
JR
484static void __iommu_flush_pages(struct protection_domain *domain,
485 u64 address, size_t size, int pde)
a19ae1ec 486{
6de8ad9b 487 int s = 0, i;
dcd1e92e 488 unsigned long pages = iommu_num_pages(address, size, PAGE_SIZE);
a19ae1ec
JR
489
490 address &= PAGE_MASK;
491
999ba417
JR
492 if (pages > 1) {
493 /*
494 * If we have to flush more than one page, flush all
495 * TLB entries for this domain
496 */
497 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
498 s = 1;
a19ae1ec
JR
499 }
500
999ba417 501
6de8ad9b
JR
502 for (i = 0; i < amd_iommus_present; ++i) {
503 if (!domain->dev_iommu[i])
504 continue;
505
506 /*
507 * Devices of this domain are behind this IOMMU
508 * We need a TLB flush
509 */
510 iommu_queue_inv_iommu_pages(amd_iommus[i], address,
511 domain->id, pde, s);
512 }
513
514 return;
515}
516
517static void iommu_flush_pages(struct protection_domain *domain,
518 u64 address, size_t size)
519{
520 __iommu_flush_pages(domain, address, size, 0);
a19ae1ec 521}
b6c02715 522
1c655773 523/* Flush the whole IO/TLB for a given protection domain */
dcd1e92e 524static void iommu_flush_tlb(struct protection_domain *domain)
1c655773 525{
dcd1e92e 526 __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
1c655773
JR
527}
528
42a49f96 529/* Flush the whole IO/TLB for a given protection domain - including PDE */
dcd1e92e 530static void iommu_flush_tlb_pde(struct protection_domain *domain)
42a49f96 531{
dcd1e92e 532 __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
42a49f96
CW
533}
534
43f49609 535/*
09b42804 536 * This function flushes all domains that have devices on the given IOMMU
43f49609 537 */
09b42804 538static void flush_all_domains_on_iommu(struct amd_iommu *iommu)
43f49609 539{
09b42804
JR
540 u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
541 struct protection_domain *domain;
e394d72a 542 unsigned long flags;
18811f55 543
09b42804 544 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
bfd1be18 545
09b42804
JR
546 list_for_each_entry(domain, &amd_iommu_pd_list, list) {
547 if (domain->dev_iommu[iommu->index] == 0)
bfd1be18 548 continue;
09b42804
JR
549
550 spin_lock(&domain->lock);
551 iommu_queue_inv_iommu_pages(iommu, address, domain->id, 1, 1);
552 iommu_flush_complete(domain);
553 spin_unlock(&domain->lock);
bfd1be18 554 }
e394d72a 555
09b42804 556 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
e394d72a
JR
557}
558
09b42804
JR
559/*
560 * This function uses heavy locking and may disable irqs for some time. But
561 * this is no issue because it is only called during resume.
562 */
bfd1be18 563void amd_iommu_flush_all_domains(void)
e394d72a 564{
e3306664 565 struct protection_domain *domain;
09b42804
JR
566 unsigned long flags;
567
568 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
e394d72a 569
e3306664 570 list_for_each_entry(domain, &amd_iommu_pd_list, list) {
09b42804 571 spin_lock(&domain->lock);
e3306664
JR
572 iommu_flush_tlb_pde(domain);
573 iommu_flush_complete(domain);
09b42804 574 spin_unlock(&domain->lock);
e3306664 575 }
09b42804
JR
576
577 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
bfd1be18
JR
578}
579
d586d785 580static void flush_all_devices_for_iommu(struct amd_iommu *iommu)
bfd1be18
JR
581{
582 int i;
583
d586d785
JR
584 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
585 if (iommu != amd_iommu_rlookup_table[i])
bfd1be18 586 continue;
d586d785
JR
587
588 iommu_queue_inv_dev_entry(iommu, i);
589 iommu_completion_wait(iommu);
bfd1be18
JR
590 }
591}
592
6a0dbcbe 593static void flush_devices_by_domain(struct protection_domain *domain)
7d7a110c
JR
594{
595 struct amd_iommu *iommu;
596 int i;
597
598 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
6a0dbcbe
JR
599 if ((domain == NULL && amd_iommu_pd_table[i] == NULL) ||
600 (amd_iommu_pd_table[i] != domain))
7d7a110c
JR
601 continue;
602
603 iommu = amd_iommu_rlookup_table[i];
604 if (!iommu)
605 continue;
606
607 iommu_queue_inv_dev_entry(iommu, i);
608 iommu_completion_wait(iommu);
609 }
610}
611
a345b23b
JR
612static void reset_iommu_command_buffer(struct amd_iommu *iommu)
613{
614 pr_err("AMD-Vi: Resetting IOMMU command buffer\n");
615
b26e81b8
JR
616 if (iommu->reset_in_progress)
617 panic("AMD-Vi: ILLEGAL_COMMAND_ERROR while resetting command buffer\n");
618
619 iommu->reset_in_progress = true;
620
a345b23b
JR
621 amd_iommu_reset_cmd_buffer(iommu);
622 flush_all_devices_for_iommu(iommu);
623 flush_all_domains_on_iommu(iommu);
b26e81b8
JR
624
625 iommu->reset_in_progress = false;
a345b23b
JR
626}
627
6a0dbcbe
JR
628void amd_iommu_flush_all_devices(void)
629{
630 flush_devices_by_domain(NULL);
631}
632
431b2a20
JR
633/****************************************************************************
634 *
635 * The functions below are used the create the page table mappings for
636 * unity mapped regions.
637 *
638 ****************************************************************************/
639
640/*
641 * Generic mapping functions. It maps a physical address into a DMA
642 * address space. It allocates the page table pages if necessary.
643 * In the future it can be extended to a generic mapping function
644 * supporting all features of AMD IOMMU page tables like level skipping
645 * and full 64 bit address spaces.
646 */
38e817fe
JR
647static int iommu_map_page(struct protection_domain *dom,
648 unsigned long bus_addr,
649 unsigned long phys_addr,
abdc5eb3
JR
650 int prot,
651 int map_size)
bd0e5211 652{
8bda3092 653 u64 __pte, *pte;
bd0e5211
JR
654
655 bus_addr = PAGE_ALIGN(bus_addr);
bb9d4ff8 656 phys_addr = PAGE_ALIGN(phys_addr);
bd0e5211 657
abdc5eb3
JR
658 BUG_ON(!PM_ALIGNED(map_size, bus_addr));
659 BUG_ON(!PM_ALIGNED(map_size, phys_addr));
660
bad1cac2 661 if (!(prot & IOMMU_PROT_MASK))
bd0e5211
JR
662 return -EINVAL;
663
abdc5eb3 664 pte = alloc_pte(dom, bus_addr, map_size, NULL, GFP_KERNEL);
bd0e5211
JR
665
666 if (IOMMU_PTE_PRESENT(*pte))
667 return -EBUSY;
668
669 __pte = phys_addr | IOMMU_PTE_P;
670 if (prot & IOMMU_PROT_IR)
671 __pte |= IOMMU_PTE_IR;
672 if (prot & IOMMU_PROT_IW)
673 __pte |= IOMMU_PTE_IW;
674
675 *pte = __pte;
676
04bfdd84
JR
677 update_domain(dom);
678
bd0e5211
JR
679 return 0;
680}
681
eb74ff6c 682static void iommu_unmap_page(struct protection_domain *dom,
a6b256b4 683 unsigned long bus_addr, int map_size)
eb74ff6c 684{
a6b256b4 685 u64 *pte = fetch_pte(dom, bus_addr, map_size);
eb74ff6c 686
38a76eee
JR
687 if (pte)
688 *pte = 0;
eb74ff6c 689}
eb74ff6c 690
431b2a20
JR
691/*
692 * This function checks if a specific unity mapping entry is needed for
693 * this specific IOMMU.
694 */
bd0e5211
JR
695static int iommu_for_unity_map(struct amd_iommu *iommu,
696 struct unity_map_entry *entry)
697{
698 u16 bdf, i;
699
700 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
701 bdf = amd_iommu_alias_table[i];
702 if (amd_iommu_rlookup_table[bdf] == iommu)
703 return 1;
704 }
705
706 return 0;
707}
708
431b2a20
JR
709/*
710 * Init the unity mappings for a specific IOMMU in the system
711 *
712 * Basically iterates over all unity mapping entries and applies them to
713 * the default domain DMA of that IOMMU if necessary.
714 */
bd0e5211
JR
715static int iommu_init_unity_mappings(struct amd_iommu *iommu)
716{
717 struct unity_map_entry *entry;
718 int ret;
719
720 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
721 if (!iommu_for_unity_map(iommu, entry))
722 continue;
723 ret = dma_ops_unity_map(iommu->default_dom, entry);
724 if (ret)
725 return ret;
726 }
727
728 return 0;
729}
730
431b2a20
JR
731/*
732 * This function actually applies the mapping to the page table of the
733 * dma_ops domain.
734 */
bd0e5211
JR
735static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
736 struct unity_map_entry *e)
737{
738 u64 addr;
739 int ret;
740
741 for (addr = e->address_start; addr < e->address_end;
742 addr += PAGE_SIZE) {
abdc5eb3
JR
743 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
744 PM_MAP_4k);
bd0e5211
JR
745 if (ret)
746 return ret;
747 /*
748 * if unity mapping is in aperture range mark the page
749 * as allocated in the aperture
750 */
751 if (addr < dma_dom->aperture_size)
c3239567 752 __set_bit(addr >> PAGE_SHIFT,
384de729 753 dma_dom->aperture[0]->bitmap);
bd0e5211
JR
754 }
755
756 return 0;
757}
758
431b2a20
JR
759/*
760 * Inits the unity mappings required for a specific device
761 */
bd0e5211
JR
762static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
763 u16 devid)
764{
765 struct unity_map_entry *e;
766 int ret;
767
768 list_for_each_entry(e, &amd_iommu_unity_map, list) {
769 if (!(devid >= e->devid_start && devid <= e->devid_end))
770 continue;
771 ret = dma_ops_unity_map(dma_dom, e);
772 if (ret)
773 return ret;
774 }
775
776 return 0;
777}
778
431b2a20
JR
779/****************************************************************************
780 *
781 * The next functions belong to the address allocator for the dma_ops
782 * interface functions. They work like the allocators in the other IOMMU
783 * drivers. Its basically a bitmap which marks the allocated pages in
784 * the aperture. Maybe it could be enhanced in the future to a more
785 * efficient allocator.
786 *
787 ****************************************************************************/
d3086444 788
431b2a20 789/*
384de729 790 * The address allocator core functions.
431b2a20
JR
791 *
792 * called with domain->lock held
793 */
384de729 794
00cd122a
JR
795/*
796 * This function checks if there is a PTE for a given dma address. If
797 * there is one, it returns the pointer to it.
798 */
9355a081 799static u64 *fetch_pte(struct protection_domain *domain,
a6b256b4 800 unsigned long address, int map_size)
00cd122a 801{
9355a081 802 int level;
00cd122a
JR
803 u64 *pte;
804
9355a081
JR
805 level = domain->mode - 1;
806 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
00cd122a 807
a6b256b4 808 while (level > map_size) {
9355a081
JR
809 if (!IOMMU_PTE_PRESENT(*pte))
810 return NULL;
00cd122a 811
9355a081 812 level -= 1;
00cd122a 813
9355a081
JR
814 pte = IOMMU_PTE_PAGE(*pte);
815 pte = &pte[PM_LEVEL_INDEX(level, address)];
00cd122a 816
a6b256b4
JR
817 if ((PM_PTE_LEVEL(*pte) == 0) && level != map_size) {
818 pte = NULL;
819 break;
820 }
9355a081 821 }
00cd122a
JR
822
823 return pte;
824}
825
9cabe89b
JR
826/*
827 * This function is used to add a new aperture range to an existing
828 * aperture in case of dma_ops domain allocation or address allocation
829 * failure.
830 */
576175c2 831static int alloc_new_range(struct dma_ops_domain *dma_dom,
9cabe89b
JR
832 bool populate, gfp_t gfp)
833{
834 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
576175c2 835 struct amd_iommu *iommu;
00cd122a 836 int i;
9cabe89b 837
f5e9705c
JR
838#ifdef CONFIG_IOMMU_STRESS
839 populate = false;
840#endif
841
9cabe89b
JR
842 if (index >= APERTURE_MAX_RANGES)
843 return -ENOMEM;
844
845 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
846 if (!dma_dom->aperture[index])
847 return -ENOMEM;
848
849 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
850 if (!dma_dom->aperture[index]->bitmap)
851 goto out_free;
852
853 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
854
855 if (populate) {
856 unsigned long address = dma_dom->aperture_size;
857 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
858 u64 *pte, *pte_page;
859
860 for (i = 0; i < num_ptes; ++i) {
abdc5eb3 861 pte = alloc_pte(&dma_dom->domain, address, PM_MAP_4k,
9cabe89b
JR
862 &pte_page, gfp);
863 if (!pte)
864 goto out_free;
865
866 dma_dom->aperture[index]->pte_pages[i] = pte_page;
867
868 address += APERTURE_RANGE_SIZE / 64;
869 }
870 }
871
872 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
873
00cd122a 874 /* Intialize the exclusion range if necessary */
576175c2
JR
875 for_each_iommu(iommu) {
876 if (iommu->exclusion_start &&
877 iommu->exclusion_start >= dma_dom->aperture[index]->offset
878 && iommu->exclusion_start < dma_dom->aperture_size) {
879 unsigned long startpage;
880 int pages = iommu_num_pages(iommu->exclusion_start,
881 iommu->exclusion_length,
882 PAGE_SIZE);
883 startpage = iommu->exclusion_start >> PAGE_SHIFT;
884 dma_ops_reserve_addresses(dma_dom, startpage, pages);
885 }
00cd122a
JR
886 }
887
888 /*
889 * Check for areas already mapped as present in the new aperture
890 * range and mark those pages as reserved in the allocator. Such
891 * mappings may already exist as a result of requested unity
892 * mappings for devices.
893 */
894 for (i = dma_dom->aperture[index]->offset;
895 i < dma_dom->aperture_size;
896 i += PAGE_SIZE) {
a6b256b4 897 u64 *pte = fetch_pte(&dma_dom->domain, i, PM_MAP_4k);
00cd122a
JR
898 if (!pte || !IOMMU_PTE_PRESENT(*pte))
899 continue;
900
901 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
902 }
903
04bfdd84
JR
904 update_domain(&dma_dom->domain);
905
9cabe89b
JR
906 return 0;
907
908out_free:
04bfdd84
JR
909 update_domain(&dma_dom->domain);
910
9cabe89b
JR
911 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
912
913 kfree(dma_dom->aperture[index]);
914 dma_dom->aperture[index] = NULL;
915
916 return -ENOMEM;
917}
918
384de729
JR
919static unsigned long dma_ops_area_alloc(struct device *dev,
920 struct dma_ops_domain *dom,
921 unsigned int pages,
922 unsigned long align_mask,
923 u64 dma_mask,
924 unsigned long start)
925{
803b8cb4 926 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
384de729
JR
927 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
928 int i = start >> APERTURE_RANGE_SHIFT;
929 unsigned long boundary_size;
930 unsigned long address = -1;
931 unsigned long limit;
932
803b8cb4
JR
933 next_bit >>= PAGE_SHIFT;
934
384de729
JR
935 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
936 PAGE_SIZE) >> PAGE_SHIFT;
937
938 for (;i < max_index; ++i) {
939 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
940
941 if (dom->aperture[i]->offset >= dma_mask)
942 break;
943
944 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
945 dma_mask >> PAGE_SHIFT);
946
947 address = iommu_area_alloc(dom->aperture[i]->bitmap,
948 limit, next_bit, pages, 0,
949 boundary_size, align_mask);
950 if (address != -1) {
951 address = dom->aperture[i]->offset +
952 (address << PAGE_SHIFT);
803b8cb4 953 dom->next_address = address + (pages << PAGE_SHIFT);
384de729
JR
954 break;
955 }
956
957 next_bit = 0;
958 }
959
960 return address;
961}
962
d3086444
JR
963static unsigned long dma_ops_alloc_addresses(struct device *dev,
964 struct dma_ops_domain *dom,
6d4f343f 965 unsigned int pages,
832a90c3
JR
966 unsigned long align_mask,
967 u64 dma_mask)
d3086444 968{
d3086444 969 unsigned long address;
d3086444 970
fe16f088
JR
971#ifdef CONFIG_IOMMU_STRESS
972 dom->next_address = 0;
973 dom->need_flush = true;
974#endif
d3086444 975
384de729 976 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
803b8cb4 977 dma_mask, dom->next_address);
d3086444 978
1c655773 979 if (address == -1) {
803b8cb4 980 dom->next_address = 0;
384de729
JR
981 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
982 dma_mask, 0);
1c655773
JR
983 dom->need_flush = true;
984 }
d3086444 985
384de729 986 if (unlikely(address == -1))
8fd524b3 987 address = DMA_ERROR_CODE;
d3086444
JR
988
989 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
990
991 return address;
992}
993
431b2a20
JR
994/*
995 * The address free function.
996 *
997 * called with domain->lock held
998 */
d3086444
JR
999static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1000 unsigned long address,
1001 unsigned int pages)
1002{
384de729
JR
1003 unsigned i = address >> APERTURE_RANGE_SHIFT;
1004 struct aperture_range *range = dom->aperture[i];
80be308d 1005
384de729
JR
1006 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1007
47bccd6b
JR
1008#ifdef CONFIG_IOMMU_STRESS
1009 if (i < 4)
1010 return;
1011#endif
80be308d 1012
803b8cb4 1013 if (address >= dom->next_address)
80be308d 1014 dom->need_flush = true;
384de729
JR
1015
1016 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
803b8cb4 1017
384de729
JR
1018 iommu_area_free(range->bitmap, address, pages);
1019
d3086444
JR
1020}
1021
431b2a20
JR
1022/****************************************************************************
1023 *
1024 * The next functions belong to the domain allocation. A domain is
1025 * allocated for every IOMMU as the default domain. If device isolation
1026 * is enabled, every device get its own domain. The most important thing
1027 * about domains is the page table mapping the DMA address space they
1028 * contain.
1029 *
1030 ****************************************************************************/
1031
aeb26f55
JR
1032/*
1033 * This function adds a protection domain to the global protection domain list
1034 */
1035static void add_domain_to_list(struct protection_domain *domain)
1036{
1037 unsigned long flags;
1038
1039 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1040 list_add(&domain->list, &amd_iommu_pd_list);
1041 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1042}
1043
1044/*
1045 * This function removes a protection domain to the global
1046 * protection domain list
1047 */
1048static void del_domain_from_list(struct protection_domain *domain)
1049{
1050 unsigned long flags;
1051
1052 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1053 list_del(&domain->list);
1054 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1055}
1056
ec487d1a
JR
1057static u16 domain_id_alloc(void)
1058{
1059 unsigned long flags;
1060 int id;
1061
1062 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1063 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1064 BUG_ON(id == 0);
1065 if (id > 0 && id < MAX_DOMAIN_ID)
1066 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1067 else
1068 id = 0;
1069 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1070
1071 return id;
1072}
1073
a2acfb75
JR
1074static void domain_id_free(int id)
1075{
1076 unsigned long flags;
1077
1078 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1079 if (id > 0 && id < MAX_DOMAIN_ID)
1080 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1081 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1082}
a2acfb75 1083
431b2a20
JR
1084/*
1085 * Used to reserve address ranges in the aperture (e.g. for exclusion
1086 * ranges.
1087 */
ec487d1a
JR
1088static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1089 unsigned long start_page,
1090 unsigned int pages)
1091{
384de729 1092 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
ec487d1a
JR
1093
1094 if (start_page + pages > last_page)
1095 pages = last_page - start_page;
1096
384de729
JR
1097 for (i = start_page; i < start_page + pages; ++i) {
1098 int index = i / APERTURE_RANGE_PAGES;
1099 int page = i % APERTURE_RANGE_PAGES;
1100 __set_bit(page, dom->aperture[index]->bitmap);
1101 }
ec487d1a
JR
1102}
1103
86db2e5d 1104static void free_pagetable(struct protection_domain *domain)
ec487d1a
JR
1105{
1106 int i, j;
1107 u64 *p1, *p2, *p3;
1108
86db2e5d 1109 p1 = domain->pt_root;
ec487d1a
JR
1110
1111 if (!p1)
1112 return;
1113
1114 for (i = 0; i < 512; ++i) {
1115 if (!IOMMU_PTE_PRESENT(p1[i]))
1116 continue;
1117
1118 p2 = IOMMU_PTE_PAGE(p1[i]);
3cc3d84b 1119 for (j = 0; j < 512; ++j) {
ec487d1a
JR
1120 if (!IOMMU_PTE_PRESENT(p2[j]))
1121 continue;
1122 p3 = IOMMU_PTE_PAGE(p2[j]);
1123 free_page((unsigned long)p3);
1124 }
1125
1126 free_page((unsigned long)p2);
1127 }
1128
1129 free_page((unsigned long)p1);
86db2e5d
JR
1130
1131 domain->pt_root = NULL;
ec487d1a
JR
1132}
1133
431b2a20
JR
1134/*
1135 * Free a domain, only used if something went wrong in the
1136 * allocation path and we need to free an already allocated page table
1137 */
ec487d1a
JR
1138static void dma_ops_domain_free(struct dma_ops_domain *dom)
1139{
384de729
JR
1140 int i;
1141
ec487d1a
JR
1142 if (!dom)
1143 return;
1144
aeb26f55
JR
1145 del_domain_from_list(&dom->domain);
1146
86db2e5d 1147 free_pagetable(&dom->domain);
ec487d1a 1148
384de729
JR
1149 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1150 if (!dom->aperture[i])
1151 continue;
1152 free_page((unsigned long)dom->aperture[i]->bitmap);
1153 kfree(dom->aperture[i]);
1154 }
ec487d1a
JR
1155
1156 kfree(dom);
1157}
1158
431b2a20
JR
1159/*
1160 * Allocates a new protection domain usable for the dma_ops functions.
1161 * It also intializes the page table and the address allocator data
1162 * structures required for the dma_ops interface
1163 */
d9cfed92 1164static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu)
ec487d1a
JR
1165{
1166 struct dma_ops_domain *dma_dom;
ec487d1a
JR
1167
1168 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1169 if (!dma_dom)
1170 return NULL;
1171
1172 spin_lock_init(&dma_dom->domain.lock);
1173
1174 dma_dom->domain.id = domain_id_alloc();
1175 if (dma_dom->domain.id == 0)
1176 goto free_dma_dom;
8f7a017c 1177 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
ec487d1a 1178 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
9fdb19d6 1179 dma_dom->domain.flags = PD_DMA_OPS_MASK;
ec487d1a
JR
1180 dma_dom->domain.priv = dma_dom;
1181 if (!dma_dom->domain.pt_root)
1182 goto free_dma_dom;
ec487d1a 1183
1c655773 1184 dma_dom->need_flush = false;
bd60b735 1185 dma_dom->target_dev = 0xffff;
1c655773 1186
aeb26f55
JR
1187 add_domain_to_list(&dma_dom->domain);
1188
576175c2 1189 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
ec487d1a 1190 goto free_dma_dom;
ec487d1a 1191
431b2a20 1192 /*
ec487d1a
JR
1193 * mark the first page as allocated so we never return 0 as
1194 * a valid dma-address. So we can use 0 as error value
431b2a20 1195 */
384de729 1196 dma_dom->aperture[0]->bitmap[0] = 1;
803b8cb4 1197 dma_dom->next_address = 0;
ec487d1a 1198
ec487d1a
JR
1199
1200 return dma_dom;
1201
1202free_dma_dom:
1203 dma_ops_domain_free(dma_dom);
1204
1205 return NULL;
1206}
1207
5b28df6f
JR
1208/*
1209 * little helper function to check whether a given protection domain is a
1210 * dma_ops domain
1211 */
1212static bool dma_ops_domain(struct protection_domain *domain)
1213{
1214 return domain->flags & PD_DMA_OPS_MASK;
1215}
1216
407d733e 1217static void set_dte_entry(u16 devid, struct protection_domain *domain)
b20ac0d4 1218{
15898bbc 1219 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
b20ac0d4 1220 u64 pte_root = virt_to_phys(domain->pt_root);
863c74eb 1221
15898bbc
JR
1222 BUG_ON(amd_iommu_pd_table[devid] != NULL);
1223
38ddf41b
JR
1224 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1225 << DEV_ENTRY_MODE_SHIFT;
1226 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
b20ac0d4 1227
b20ac0d4 1228 amd_iommu_dev_table[devid].data[2] = domain->id;
aa879fff
JR
1229 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1230 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
b20ac0d4
JR
1231
1232 amd_iommu_pd_table[devid] = domain;
15898bbc
JR
1233
1234 /* Do reference counting */
1235 domain->dev_iommu[iommu->index] += 1;
1236 domain->dev_cnt += 1;
1237
1238 /* Flush the changes DTE entry */
1239 iommu_queue_inv_dev_entry(iommu, devid);
1240}
1241
1242static void clear_dte_entry(u16 devid)
1243{
1244 struct protection_domain *domain = amd_iommu_pd_table[devid];
1245 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1246
1247 BUG_ON(domain == NULL);
1248
1249 /* remove domain from the lookup table */
1250 amd_iommu_pd_table[devid] = NULL;
1251
1252 /* remove entry from the device table seen by the hardware */
1253 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1254 amd_iommu_dev_table[devid].data[1] = 0;
1255 amd_iommu_dev_table[devid].data[2] = 0;
1256
1257 amd_iommu_apply_erratum_63(devid);
1258
1259 /* decrease reference counters */
1260 domain->dev_iommu[iommu->index] -= 1;
1261 domain->dev_cnt -= 1;
1262
1263 iommu_queue_inv_dev_entry(iommu, devid);
2b681faf
JR
1264}
1265
1266/*
1267 * If a device is not yet associated with a domain, this function does
1268 * assigns it visible for the hardware
1269 */
15898bbc
JR
1270static int __attach_device(struct device *dev,
1271 struct protection_domain *domain)
2b681faf 1272{
15898bbc
JR
1273 u16 devid = get_device_id(dev);
1274 u16 alias = amd_iommu_alias_table[devid];
1275
2b681faf
JR
1276 /* lock domain */
1277 spin_lock(&domain->lock);
1278
15898bbc
JR
1279 /* Some sanity checks */
1280 if (amd_iommu_pd_table[alias] != NULL &&
1281 amd_iommu_pd_table[alias] != domain)
1282 return -EBUSY;
eba6ac60 1283
15898bbc
JR
1284 if (amd_iommu_pd_table[devid] != NULL &&
1285 amd_iommu_pd_table[devid] != domain)
1286 return -EBUSY;
1287
1288 /* Do real assignment */
1289 if (alias != devid &&
1290 amd_iommu_pd_table[alias] == NULL)
1291 set_dte_entry(alias, domain);
1292
1293 if (amd_iommu_pd_table[devid] == NULL)
1294 set_dte_entry(devid, domain);
eba6ac60
JR
1295
1296 /* ready */
1297 spin_unlock(&domain->lock);
15898bbc
JR
1298
1299 return 0;
0feae533 1300}
b20ac0d4 1301
407d733e
JR
1302/*
1303 * If a device is not yet associated with a domain, this function does
1304 * assigns it visible for the hardware
1305 */
15898bbc
JR
1306static int attach_device(struct device *dev,
1307 struct protection_domain *domain)
0feae533 1308{
eba6ac60 1309 unsigned long flags;
15898bbc 1310 int ret;
eba6ac60
JR
1311
1312 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
15898bbc 1313 ret = __attach_device(dev, domain);
b20ac0d4
JR
1314 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1315
0feae533
JR
1316 /*
1317 * We might boot into a crash-kernel here. The crashed kernel
1318 * left the caches in the IOMMU dirty. So we have to flush
1319 * here to evict all dirty stuff.
1320 */
dcd1e92e 1321 iommu_flush_tlb_pde(domain);
15898bbc
JR
1322
1323 return ret;
b20ac0d4
JR
1324}
1325
355bf553
JR
1326/*
1327 * Removes a device from a protection domain (unlocked)
1328 */
15898bbc 1329static void __detach_device(struct device *dev)
355bf553 1330{
15898bbc 1331 u16 devid = get_device_id(dev);
c4596114
JR
1332 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1333
1334 BUG_ON(!iommu);
355bf553 1335
15898bbc 1336 clear_dte_entry(devid);
21129f78
JR
1337
1338 /*
1339 * If we run in passthrough mode the device must be assigned to the
1340 * passthrough domain if it is detached from any other domain
1341 */
15898bbc
JR
1342 if (iommu_pass_through)
1343 __attach_device(dev, pt_domain);
355bf553
JR
1344}
1345
1346/*
1347 * Removes a device from a protection domain (with devtable_lock held)
1348 */
15898bbc 1349static void detach_device(struct device *dev)
355bf553
JR
1350{
1351 unsigned long flags;
1352
1353 /* lock device table */
1354 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
15898bbc 1355 __detach_device(dev);
355bf553
JR
1356 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1357}
e275a2a0 1358
15898bbc
JR
1359/*
1360 * Find out the protection domain structure for a given PCI device. This
1361 * will give us the pointer to the page table root for example.
1362 */
1363static struct protection_domain *domain_for_device(struct device *dev)
1364{
1365 struct protection_domain *dom;
1366 unsigned long flags;
1367 u16 devid, alias;
1368
1369 devid = get_device_id(dev);
1370 alias = amd_iommu_alias_table[devid];
1371
1372 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
1373 dom = amd_iommu_pd_table[devid];
1374 if (dom == NULL &&
1375 amd_iommu_pd_table[alias] != NULL) {
1376 __attach_device(dev, amd_iommu_pd_table[alias]);
1377 dom = amd_iommu_pd_table[devid];
1378 }
1379
1380 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1381
1382 return dom;
1383}
1384
e275a2a0
JR
1385static int device_change_notifier(struct notifier_block *nb,
1386 unsigned long action, void *data)
1387{
1388 struct device *dev = data;
1389 struct pci_dev *pdev = to_pci_dev(dev);
1390 u16 devid = calc_devid(pdev->bus->number, pdev->devfn);
1391 struct protection_domain *domain;
1392 struct dma_ops_domain *dma_domain;
1393 struct amd_iommu *iommu;
1ac4cbbc 1394 unsigned long flags;
e275a2a0
JR
1395
1396 if (devid > amd_iommu_last_bdf)
1397 goto out;
1398
1399 devid = amd_iommu_alias_table[devid];
1400
1401 iommu = amd_iommu_rlookup_table[devid];
1402 if (iommu == NULL)
1403 goto out;
1404
15898bbc 1405 domain = domain_for_device(dev);
e275a2a0
JR
1406
1407 if (domain && !dma_ops_domain(domain))
1408 WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
1409 "to a non-dma-ops domain\n", dev_name(dev));
1410
1411 switch (action) {
c1eee67b 1412 case BUS_NOTIFY_UNBOUND_DRIVER:
e275a2a0
JR
1413 if (!domain)
1414 goto out;
a1ca331c
JR
1415 if (iommu_pass_through)
1416 break;
15898bbc 1417 detach_device(dev);
1ac4cbbc
JR
1418 break;
1419 case BUS_NOTIFY_ADD_DEVICE:
1420 /* allocate a protection domain if a device is added */
1421 dma_domain = find_protection_domain(devid);
1422 if (dma_domain)
1423 goto out;
d9cfed92 1424 dma_domain = dma_ops_domain_alloc(iommu);
1ac4cbbc
JR
1425 if (!dma_domain)
1426 goto out;
1427 dma_domain->target_dev = devid;
1428
1429 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1430 list_add_tail(&dma_domain->list, &iommu_pd_list);
1431 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1432
e275a2a0
JR
1433 break;
1434 default:
1435 goto out;
1436 }
1437
1438 iommu_queue_inv_dev_entry(iommu, devid);
1439 iommu_completion_wait(iommu);
1440
1441out:
1442 return 0;
1443}
1444
b25ae679 1445static struct notifier_block device_nb = {
e275a2a0
JR
1446 .notifier_call = device_change_notifier,
1447};
355bf553 1448
431b2a20
JR
1449/*****************************************************************************
1450 *
1451 * The next functions belong to the dma_ops mapping/unmapping code.
1452 *
1453 *****************************************************************************/
1454
dbcc112e
JR
1455/*
1456 * This function checks if the driver got a valid device from the caller to
1457 * avoid dereferencing invalid pointers.
1458 */
1459static bool check_device(struct device *dev)
1460{
420aef8a
JR
1461 u16 bdf;
1462 struct pci_dev *pcidev;
1463
dbcc112e
JR
1464 if (!dev || !dev->dma_mask)
1465 return false;
1466
420aef8a
JR
1467 /* No device or no PCI device */
1468 if (!dev || dev->bus != &pci_bus_type)
1469 return false;
1470
1471 pcidev = to_pci_dev(dev);
1472
1473 bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
1474
1475 /* Out of our scope? */
1476 if (bdf > amd_iommu_last_bdf)
1477 return false;
1478
1479 if (amd_iommu_rlookup_table[bdf] == NULL)
1480 return false;
1481
dbcc112e
JR
1482 return true;
1483}
1484
431b2a20
JR
1485/*
1486 * In the dma_ops path we only have the struct device. This function
1487 * finds the corresponding IOMMU, the protection domain and the
1488 * requestor id for a given device.
1489 * If the device is not yet associated with a domain this is also done
1490 * in this function.
1491 */
94f6d190 1492static struct protection_domain *get_domain(struct device *dev)
b20ac0d4 1493{
94f6d190 1494 struct protection_domain *domain;
b20ac0d4 1495 struct dma_ops_domain *dma_dom;
94f6d190 1496 u16 devid = get_device_id(dev);
b20ac0d4 1497
f99c0f1c 1498 if (!check_device(dev))
94f6d190 1499 return ERR_PTR(-EINVAL);
b20ac0d4 1500
94f6d190
JR
1501 domain = domain_for_device(dev);
1502 if (domain != NULL && !dma_ops_domain(domain))
1503 return ERR_PTR(-EBUSY);
f99c0f1c 1504
94f6d190
JR
1505 if (domain != NULL)
1506 return domain;
b20ac0d4 1507
15898bbc 1508 /* Device not bount yet - bind it */
94f6d190 1509 dma_dom = find_protection_domain(devid);
15898bbc 1510 if (!dma_dom)
94f6d190
JR
1511 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1512 attach_device(dev, &dma_dom->domain);
15898bbc 1513 DUMP_printk("Using protection domain %d for device %s\n",
94f6d190 1514 dma_dom->domain.id, dev_name(dev));
f91ba190 1515
94f6d190 1516 return &dma_dom->domain;
b20ac0d4
JR
1517}
1518
04bfdd84
JR
1519static void update_device_table(struct protection_domain *domain)
1520{
2b681faf 1521 unsigned long flags;
04bfdd84
JR
1522 int i;
1523
1524 for (i = 0; i <= amd_iommu_last_bdf; ++i) {
1525 if (amd_iommu_pd_table[i] != domain)
1526 continue;
2b681faf 1527 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
04bfdd84 1528 set_dte_entry(i, domain);
2b681faf 1529 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
04bfdd84
JR
1530 }
1531}
1532
1533static void update_domain(struct protection_domain *domain)
1534{
1535 if (!domain->updated)
1536 return;
1537
1538 update_device_table(domain);
1539 flush_devices_by_domain(domain);
601367d7 1540 iommu_flush_tlb_pde(domain);
04bfdd84
JR
1541
1542 domain->updated = false;
1543}
1544
8bda3092 1545/*
50020fb6
JR
1546 * This function is used to add another level to an IO page table. Adding
1547 * another level increases the size of the address space by 9 bits to a size up
1548 * to 64 bits.
8bda3092 1549 */
50020fb6
JR
1550static bool increase_address_space(struct protection_domain *domain,
1551 gfp_t gfp)
1552{
1553 u64 *pte;
1554
1555 if (domain->mode == PAGE_MODE_6_LEVEL)
1556 /* address space already 64 bit large */
1557 return false;
1558
1559 pte = (void *)get_zeroed_page(gfp);
1560 if (!pte)
1561 return false;
1562
1563 *pte = PM_LEVEL_PDE(domain->mode,
1564 virt_to_phys(domain->pt_root));
1565 domain->pt_root = pte;
1566 domain->mode += 1;
1567 domain->updated = true;
1568
1569 return true;
1570}
1571
8bc3e127 1572static u64 *alloc_pte(struct protection_domain *domain,
abdc5eb3
JR
1573 unsigned long address,
1574 int end_lvl,
1575 u64 **pte_page,
1576 gfp_t gfp)
8bda3092
JR
1577{
1578 u64 *pte, *page;
8bc3e127 1579 int level;
8bda3092 1580
8bc3e127
JR
1581 while (address > PM_LEVEL_SIZE(domain->mode))
1582 increase_address_space(domain, gfp);
8bda3092 1583
8bc3e127
JR
1584 level = domain->mode - 1;
1585 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
8bda3092 1586
abdc5eb3 1587 while (level > end_lvl) {
8bc3e127
JR
1588 if (!IOMMU_PTE_PRESENT(*pte)) {
1589 page = (u64 *)get_zeroed_page(gfp);
1590 if (!page)
1591 return NULL;
1592 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1593 }
8bda3092 1594
8bc3e127 1595 level -= 1;
8bda3092 1596
8bc3e127 1597 pte = IOMMU_PTE_PAGE(*pte);
8bda3092 1598
abdc5eb3 1599 if (pte_page && level == end_lvl)
8bc3e127 1600 *pte_page = pte;
8bda3092 1601
8bc3e127
JR
1602 pte = &pte[PM_LEVEL_INDEX(level, address)];
1603 }
8bda3092
JR
1604
1605 return pte;
1606}
1607
1608/*
1609 * This function fetches the PTE for a given address in the aperture
1610 */
1611static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1612 unsigned long address)
1613{
384de729 1614 struct aperture_range *aperture;
8bda3092
JR
1615 u64 *pte, *pte_page;
1616
384de729
JR
1617 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1618 if (!aperture)
1619 return NULL;
1620
1621 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
8bda3092 1622 if (!pte) {
abdc5eb3
JR
1623 pte = alloc_pte(&dom->domain, address, PM_MAP_4k, &pte_page,
1624 GFP_ATOMIC);
384de729
JR
1625 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1626 } else
8c8c143c 1627 pte += PM_LEVEL_INDEX(0, address);
8bda3092 1628
04bfdd84 1629 update_domain(&dom->domain);
8bda3092
JR
1630
1631 return pte;
1632}
1633
431b2a20
JR
1634/*
1635 * This is the generic map function. It maps one 4kb page at paddr to
1636 * the given address in the DMA address space for the domain.
1637 */
680525e0 1638static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
cb76c322
JR
1639 unsigned long address,
1640 phys_addr_t paddr,
1641 int direction)
1642{
1643 u64 *pte, __pte;
1644
1645 WARN_ON(address > dom->aperture_size);
1646
1647 paddr &= PAGE_MASK;
1648
8bda3092 1649 pte = dma_ops_get_pte(dom, address);
53812c11 1650 if (!pte)
8fd524b3 1651 return DMA_ERROR_CODE;
cb76c322
JR
1652
1653 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1654
1655 if (direction == DMA_TO_DEVICE)
1656 __pte |= IOMMU_PTE_IR;
1657 else if (direction == DMA_FROM_DEVICE)
1658 __pte |= IOMMU_PTE_IW;
1659 else if (direction == DMA_BIDIRECTIONAL)
1660 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1661
1662 WARN_ON(*pte);
1663
1664 *pte = __pte;
1665
1666 return (dma_addr_t)address;
1667}
1668
431b2a20
JR
1669/*
1670 * The generic unmapping function for on page in the DMA address space.
1671 */
680525e0 1672static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
cb76c322
JR
1673 unsigned long address)
1674{
384de729 1675 struct aperture_range *aperture;
cb76c322
JR
1676 u64 *pte;
1677
1678 if (address >= dom->aperture_size)
1679 return;
1680
384de729
JR
1681 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1682 if (!aperture)
1683 return;
1684
1685 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1686 if (!pte)
1687 return;
cb76c322 1688
8c8c143c 1689 pte += PM_LEVEL_INDEX(0, address);
cb76c322
JR
1690
1691 WARN_ON(!*pte);
1692
1693 *pte = 0ULL;
1694}
1695
431b2a20
JR
1696/*
1697 * This function contains common code for mapping of a physically
24f81160
JR
1698 * contiguous memory region into DMA address space. It is used by all
1699 * mapping functions provided with this IOMMU driver.
431b2a20
JR
1700 * Must be called with the domain lock held.
1701 */
cb76c322 1702static dma_addr_t __map_single(struct device *dev,
cb76c322
JR
1703 struct dma_ops_domain *dma_dom,
1704 phys_addr_t paddr,
1705 size_t size,
6d4f343f 1706 int dir,
832a90c3
JR
1707 bool align,
1708 u64 dma_mask)
cb76c322
JR
1709{
1710 dma_addr_t offset = paddr & ~PAGE_MASK;
53812c11 1711 dma_addr_t address, start, ret;
cb76c322 1712 unsigned int pages;
6d4f343f 1713 unsigned long align_mask = 0;
cb76c322
JR
1714 int i;
1715
e3c449f5 1716 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
cb76c322
JR
1717 paddr &= PAGE_MASK;
1718
8ecaf8f1
JR
1719 INC_STATS_COUNTER(total_map_requests);
1720
c1858976
JR
1721 if (pages > 1)
1722 INC_STATS_COUNTER(cross_page);
1723
6d4f343f
JR
1724 if (align)
1725 align_mask = (1UL << get_order(size)) - 1;
1726
11b83888 1727retry:
832a90c3
JR
1728 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1729 dma_mask);
8fd524b3 1730 if (unlikely(address == DMA_ERROR_CODE)) {
11b83888
JR
1731 /*
1732 * setting next_address here will let the address
1733 * allocator only scan the new allocated range in the
1734 * first run. This is a small optimization.
1735 */
1736 dma_dom->next_address = dma_dom->aperture_size;
1737
576175c2 1738 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
11b83888
JR
1739 goto out;
1740
1741 /*
1742 * aperture was sucessfully enlarged by 128 MB, try
1743 * allocation again
1744 */
1745 goto retry;
1746 }
cb76c322
JR
1747
1748 start = address;
1749 for (i = 0; i < pages; ++i) {
680525e0 1750 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
8fd524b3 1751 if (ret == DMA_ERROR_CODE)
53812c11
JR
1752 goto out_unmap;
1753
cb76c322
JR
1754 paddr += PAGE_SIZE;
1755 start += PAGE_SIZE;
1756 }
1757 address += offset;
1758
5774f7c5
JR
1759 ADD_STATS_COUNTER(alloced_io_mem, size);
1760
afa9fdc2 1761 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
dcd1e92e 1762 iommu_flush_tlb(&dma_dom->domain);
1c655773 1763 dma_dom->need_flush = false;
318afd41 1764 } else if (unlikely(amd_iommu_np_cache))
6de8ad9b 1765 iommu_flush_pages(&dma_dom->domain, address, size);
270cab24 1766
cb76c322
JR
1767out:
1768 return address;
53812c11
JR
1769
1770out_unmap:
1771
1772 for (--i; i >= 0; --i) {
1773 start -= PAGE_SIZE;
680525e0 1774 dma_ops_domain_unmap(dma_dom, start);
53812c11
JR
1775 }
1776
1777 dma_ops_free_addresses(dma_dom, address, pages);
1778
8fd524b3 1779 return DMA_ERROR_CODE;
cb76c322
JR
1780}
1781
431b2a20
JR
1782/*
1783 * Does the reverse of the __map_single function. Must be called with
1784 * the domain lock held too
1785 */
cd8c82e8 1786static void __unmap_single(struct dma_ops_domain *dma_dom,
cb76c322
JR
1787 dma_addr_t dma_addr,
1788 size_t size,
1789 int dir)
1790{
1791 dma_addr_t i, start;
1792 unsigned int pages;
1793
8fd524b3 1794 if ((dma_addr == DMA_ERROR_CODE) ||
b8d9905d 1795 (dma_addr + size > dma_dom->aperture_size))
cb76c322
JR
1796 return;
1797
e3c449f5 1798 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
cb76c322
JR
1799 dma_addr &= PAGE_MASK;
1800 start = dma_addr;
1801
1802 for (i = 0; i < pages; ++i) {
680525e0 1803 dma_ops_domain_unmap(dma_dom, start);
cb76c322
JR
1804 start += PAGE_SIZE;
1805 }
1806
5774f7c5
JR
1807 SUB_STATS_COUNTER(alloced_io_mem, size);
1808
cb76c322 1809 dma_ops_free_addresses(dma_dom, dma_addr, pages);
270cab24 1810
80be308d 1811 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
6de8ad9b 1812 iommu_flush_pages(&dma_dom->domain, dma_addr, size);
80be308d
JR
1813 dma_dom->need_flush = false;
1814 }
cb76c322
JR
1815}
1816
431b2a20
JR
1817/*
1818 * The exported map_single function for dma_ops.
1819 */
51491367
FT
1820static dma_addr_t map_page(struct device *dev, struct page *page,
1821 unsigned long offset, size_t size,
1822 enum dma_data_direction dir,
1823 struct dma_attrs *attrs)
4da70b9e
JR
1824{
1825 unsigned long flags;
4da70b9e 1826 struct protection_domain *domain;
4da70b9e 1827 dma_addr_t addr;
832a90c3 1828 u64 dma_mask;
51491367 1829 phys_addr_t paddr = page_to_phys(page) + offset;
4da70b9e 1830
0f2a86f2
JR
1831 INC_STATS_COUNTER(cnt_map_single);
1832
94f6d190
JR
1833 domain = get_domain(dev);
1834 if (PTR_ERR(domain) == -EINVAL)
4da70b9e 1835 return (dma_addr_t)paddr;
94f6d190
JR
1836 else if (IS_ERR(domain))
1837 return DMA_ERROR_CODE;
4da70b9e 1838
f99c0f1c
JR
1839 dma_mask = *dev->dma_mask;
1840
4da70b9e 1841 spin_lock_irqsave(&domain->lock, flags);
94f6d190 1842
cd8c82e8 1843 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
832a90c3 1844 dma_mask);
8fd524b3 1845 if (addr == DMA_ERROR_CODE)
4da70b9e
JR
1846 goto out;
1847
0518a3a4 1848 iommu_flush_complete(domain);
4da70b9e
JR
1849
1850out:
1851 spin_unlock_irqrestore(&domain->lock, flags);
1852
1853 return addr;
1854}
1855
431b2a20
JR
1856/*
1857 * The exported unmap_single function for dma_ops.
1858 */
51491367
FT
1859static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
1860 enum dma_data_direction dir, struct dma_attrs *attrs)
4da70b9e
JR
1861{
1862 unsigned long flags;
4da70b9e 1863 struct protection_domain *domain;
4da70b9e 1864
146a6917
JR
1865 INC_STATS_COUNTER(cnt_unmap_single);
1866
94f6d190
JR
1867 domain = get_domain(dev);
1868 if (IS_ERR(domain))
5b28df6f
JR
1869 return;
1870
4da70b9e
JR
1871 spin_lock_irqsave(&domain->lock, flags);
1872
cd8c82e8 1873 __unmap_single(domain->priv, dma_addr, size, dir);
4da70b9e 1874
0518a3a4 1875 iommu_flush_complete(domain);
4da70b9e
JR
1876
1877 spin_unlock_irqrestore(&domain->lock, flags);
1878}
1879
431b2a20
JR
1880/*
1881 * This is a special map_sg function which is used if we should map a
1882 * device which is not handled by an AMD IOMMU in the system.
1883 */
65b050ad
JR
1884static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
1885 int nelems, int dir)
1886{
1887 struct scatterlist *s;
1888 int i;
1889
1890 for_each_sg(sglist, s, nelems, i) {
1891 s->dma_address = (dma_addr_t)sg_phys(s);
1892 s->dma_length = s->length;
1893 }
1894
1895 return nelems;
1896}
1897
431b2a20
JR
1898/*
1899 * The exported map_sg function for dma_ops (handles scatter-gather
1900 * lists).
1901 */
65b050ad 1902static int map_sg(struct device *dev, struct scatterlist *sglist,
160c1d8e
FT
1903 int nelems, enum dma_data_direction dir,
1904 struct dma_attrs *attrs)
65b050ad
JR
1905{
1906 unsigned long flags;
65b050ad 1907 struct protection_domain *domain;
65b050ad
JR
1908 int i;
1909 struct scatterlist *s;
1910 phys_addr_t paddr;
1911 int mapped_elems = 0;
832a90c3 1912 u64 dma_mask;
65b050ad 1913
d03f067a
JR
1914 INC_STATS_COUNTER(cnt_map_sg);
1915
94f6d190
JR
1916 domain = get_domain(dev);
1917 if (PTR_ERR(domain) == -EINVAL)
f99c0f1c 1918 return map_sg_no_iommu(dev, sglist, nelems, dir);
94f6d190
JR
1919 else if (IS_ERR(domain))
1920 return 0;
dbcc112e 1921
832a90c3 1922 dma_mask = *dev->dma_mask;
65b050ad 1923
65b050ad
JR
1924 spin_lock_irqsave(&domain->lock, flags);
1925
1926 for_each_sg(sglist, s, nelems, i) {
1927 paddr = sg_phys(s);
1928
cd8c82e8 1929 s->dma_address = __map_single(dev, domain->priv,
832a90c3
JR
1930 paddr, s->length, dir, false,
1931 dma_mask);
65b050ad
JR
1932
1933 if (s->dma_address) {
1934 s->dma_length = s->length;
1935 mapped_elems++;
1936 } else
1937 goto unmap;
65b050ad
JR
1938 }
1939
0518a3a4 1940 iommu_flush_complete(domain);
65b050ad
JR
1941
1942out:
1943 spin_unlock_irqrestore(&domain->lock, flags);
1944
1945 return mapped_elems;
1946unmap:
1947 for_each_sg(sglist, s, mapped_elems, i) {
1948 if (s->dma_address)
cd8c82e8 1949 __unmap_single(domain->priv, s->dma_address,
65b050ad
JR
1950 s->dma_length, dir);
1951 s->dma_address = s->dma_length = 0;
1952 }
1953
1954 mapped_elems = 0;
1955
1956 goto out;
1957}
1958
431b2a20
JR
1959/*
1960 * The exported map_sg function for dma_ops (handles scatter-gather
1961 * lists).
1962 */
65b050ad 1963static void unmap_sg(struct device *dev, struct scatterlist *sglist,
160c1d8e
FT
1964 int nelems, enum dma_data_direction dir,
1965 struct dma_attrs *attrs)
65b050ad
JR
1966{
1967 unsigned long flags;
65b050ad
JR
1968 struct protection_domain *domain;
1969 struct scatterlist *s;
65b050ad
JR
1970 int i;
1971
55877a6b
JR
1972 INC_STATS_COUNTER(cnt_unmap_sg);
1973
94f6d190
JR
1974 domain = get_domain(dev);
1975 if (IS_ERR(domain))
5b28df6f
JR
1976 return;
1977
65b050ad
JR
1978 spin_lock_irqsave(&domain->lock, flags);
1979
1980 for_each_sg(sglist, s, nelems, i) {
cd8c82e8 1981 __unmap_single(domain->priv, s->dma_address,
65b050ad 1982 s->dma_length, dir);
65b050ad
JR
1983 s->dma_address = s->dma_length = 0;
1984 }
1985
0518a3a4 1986 iommu_flush_complete(domain);
65b050ad
JR
1987
1988 spin_unlock_irqrestore(&domain->lock, flags);
1989}
1990
431b2a20
JR
1991/*
1992 * The exported alloc_coherent function for dma_ops.
1993 */
5d8b53cf
JR
1994static void *alloc_coherent(struct device *dev, size_t size,
1995 dma_addr_t *dma_addr, gfp_t flag)
1996{
1997 unsigned long flags;
1998 void *virt_addr;
5d8b53cf 1999 struct protection_domain *domain;
5d8b53cf 2000 phys_addr_t paddr;
832a90c3 2001 u64 dma_mask = dev->coherent_dma_mask;
5d8b53cf 2002
c8f0fb36
JR
2003 INC_STATS_COUNTER(cnt_alloc_coherent);
2004
94f6d190
JR
2005 domain = get_domain(dev);
2006 if (PTR_ERR(domain) == -EINVAL) {
f99c0f1c
JR
2007 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2008 *dma_addr = __pa(virt_addr);
2009 return virt_addr;
94f6d190
JR
2010 } else if (IS_ERR(domain))
2011 return NULL;
5d8b53cf 2012
f99c0f1c
JR
2013 dma_mask = dev->coherent_dma_mask;
2014 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2015 flag |= __GFP_ZERO;
5d8b53cf
JR
2016
2017 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2018 if (!virt_addr)
b25ae679 2019 return NULL;
5d8b53cf 2020
5d8b53cf
JR
2021 paddr = virt_to_phys(virt_addr);
2022
832a90c3
JR
2023 if (!dma_mask)
2024 dma_mask = *dev->dma_mask;
2025
5d8b53cf
JR
2026 spin_lock_irqsave(&domain->lock, flags);
2027
cd8c82e8 2028 *dma_addr = __map_single(dev, domain->priv, paddr,
832a90c3 2029 size, DMA_BIDIRECTIONAL, true, dma_mask);
5d8b53cf 2030
8fd524b3 2031 if (*dma_addr == DMA_ERROR_CODE) {
367d04c4 2032 spin_unlock_irqrestore(&domain->lock, flags);
5b28df6f 2033 goto out_free;
367d04c4 2034 }
5d8b53cf 2035
0518a3a4 2036 iommu_flush_complete(domain);
5d8b53cf 2037
5d8b53cf
JR
2038 spin_unlock_irqrestore(&domain->lock, flags);
2039
2040 return virt_addr;
5b28df6f
JR
2041
2042out_free:
2043
2044 free_pages((unsigned long)virt_addr, get_order(size));
2045
2046 return NULL;
5d8b53cf
JR
2047}
2048
431b2a20
JR
2049/*
2050 * The exported free_coherent function for dma_ops.
431b2a20 2051 */
5d8b53cf
JR
2052static void free_coherent(struct device *dev, size_t size,
2053 void *virt_addr, dma_addr_t dma_addr)
2054{
2055 unsigned long flags;
5d8b53cf 2056 struct protection_domain *domain;
5d8b53cf 2057
5d31ee7e
JR
2058 INC_STATS_COUNTER(cnt_free_coherent);
2059
94f6d190
JR
2060 domain = get_domain(dev);
2061 if (IS_ERR(domain))
5b28df6f
JR
2062 goto free_mem;
2063
5d8b53cf
JR
2064 spin_lock_irqsave(&domain->lock, flags);
2065
cd8c82e8 2066 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
5d8b53cf 2067
0518a3a4 2068 iommu_flush_complete(domain);
5d8b53cf
JR
2069
2070 spin_unlock_irqrestore(&domain->lock, flags);
2071
2072free_mem:
2073 free_pages((unsigned long)virt_addr, get_order(size));
2074}
2075
b39ba6ad
JR
2076/*
2077 * This function is called by the DMA layer to find out if we can handle a
2078 * particular device. It is part of the dma_ops.
2079 */
2080static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2081{
420aef8a 2082 return check_device(dev);
b39ba6ad
JR
2083}
2084
c432f3df 2085/*
431b2a20
JR
2086 * The function for pre-allocating protection domains.
2087 *
c432f3df
JR
2088 * If the driver core informs the DMA layer if a driver grabs a device
2089 * we don't need to preallocate the protection domains anymore.
2090 * For now we have to.
2091 */
0e93dd88 2092static void prealloc_protection_domains(void)
c432f3df
JR
2093{
2094 struct pci_dev *dev = NULL;
2095 struct dma_ops_domain *dma_dom;
2096 struct amd_iommu *iommu;
be831297 2097 u16 devid, __devid;
c432f3df
JR
2098
2099 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
be831297 2100 __devid = devid = calc_devid(dev->bus->number, dev->devfn);
3a61ec38 2101 if (devid > amd_iommu_last_bdf)
c432f3df
JR
2102 continue;
2103 devid = amd_iommu_alias_table[devid];
15898bbc 2104 if (domain_for_device(&dev->dev))
c432f3df
JR
2105 continue;
2106 iommu = amd_iommu_rlookup_table[devid];
2107 if (!iommu)
2108 continue;
d9cfed92 2109 dma_dom = dma_ops_domain_alloc(iommu);
c432f3df
JR
2110 if (!dma_dom)
2111 continue;
2112 init_unity_mappings_for_device(dma_dom, devid);
bd60b735
JR
2113 dma_dom->target_dev = devid;
2114
15898bbc 2115 attach_device(&dev->dev, &dma_dom->domain);
be831297 2116
bd60b735 2117 list_add_tail(&dma_dom->list, &iommu_pd_list);
c432f3df
JR
2118 }
2119}
2120
160c1d8e 2121static struct dma_map_ops amd_iommu_dma_ops = {
6631ee9d
JR
2122 .alloc_coherent = alloc_coherent,
2123 .free_coherent = free_coherent,
51491367
FT
2124 .map_page = map_page,
2125 .unmap_page = unmap_page,
6631ee9d
JR
2126 .map_sg = map_sg,
2127 .unmap_sg = unmap_sg,
b39ba6ad 2128 .dma_supported = amd_iommu_dma_supported,
6631ee9d
JR
2129};
2130
431b2a20
JR
2131/*
2132 * The function which clues the AMD IOMMU driver into dma_ops.
2133 */
6631ee9d
JR
2134int __init amd_iommu_init_dma_ops(void)
2135{
2136 struct amd_iommu *iommu;
6631ee9d
JR
2137 int ret;
2138
431b2a20
JR
2139 /*
2140 * first allocate a default protection domain for every IOMMU we
2141 * found in the system. Devices not assigned to any other
2142 * protection domain will be assigned to the default one.
2143 */
3bd22172 2144 for_each_iommu(iommu) {
d9cfed92 2145 iommu->default_dom = dma_ops_domain_alloc(iommu);
6631ee9d
JR
2146 if (iommu->default_dom == NULL)
2147 return -ENOMEM;
e2dc14a2 2148 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
6631ee9d
JR
2149 ret = iommu_init_unity_mappings(iommu);
2150 if (ret)
2151 goto free_domains;
2152 }
2153
431b2a20
JR
2154 /*
2155 * If device isolation is enabled, pre-allocate the protection
2156 * domains for each device.
2157 */
6631ee9d
JR
2158 if (amd_iommu_isolate)
2159 prealloc_protection_domains();
2160
2161 iommu_detected = 1;
75f1cdf1 2162 swiotlb = 0;
92af4e29 2163#ifdef CONFIG_GART_IOMMU
6631ee9d
JR
2164 gart_iommu_aperture_disabled = 1;
2165 gart_iommu_aperture = 0;
92af4e29 2166#endif
6631ee9d 2167
431b2a20 2168 /* Make the driver finally visible to the drivers */
6631ee9d
JR
2169 dma_ops = &amd_iommu_dma_ops;
2170
26961efe 2171 register_iommu(&amd_iommu_ops);
26961efe 2172
e275a2a0
JR
2173 bus_register_notifier(&pci_bus_type, &device_nb);
2174
7f26508b
JR
2175 amd_iommu_stats_init();
2176
6631ee9d
JR
2177 return 0;
2178
2179free_domains:
2180
3bd22172 2181 for_each_iommu(iommu) {
6631ee9d
JR
2182 if (iommu->default_dom)
2183 dma_ops_domain_free(iommu->default_dom);
2184 }
2185
2186 return ret;
2187}
6d98cd80
JR
2188
2189/*****************************************************************************
2190 *
2191 * The following functions belong to the exported interface of AMD IOMMU
2192 *
2193 * This interface allows access to lower level functions of the IOMMU
2194 * like protection domain handling and assignement of devices to domains
2195 * which is not possible with the dma_ops interface.
2196 *
2197 *****************************************************************************/
2198
6d98cd80
JR
2199static void cleanup_domain(struct protection_domain *domain)
2200{
2201 unsigned long flags;
2202 u16 devid;
2203
2204 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2205
2206 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2207 if (amd_iommu_pd_table[devid] == domain)
15898bbc 2208 clear_dte_entry(devid);
6d98cd80
JR
2209
2210 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2211}
2212
2650815f
JR
2213static void protection_domain_free(struct protection_domain *domain)
2214{
2215 if (!domain)
2216 return;
2217
aeb26f55
JR
2218 del_domain_from_list(domain);
2219
2650815f
JR
2220 if (domain->id)
2221 domain_id_free(domain->id);
2222
2223 kfree(domain);
2224}
2225
2226static struct protection_domain *protection_domain_alloc(void)
c156e347
JR
2227{
2228 struct protection_domain *domain;
2229
2230 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2231 if (!domain)
2650815f 2232 return NULL;
c156e347
JR
2233
2234 spin_lock_init(&domain->lock);
c156e347
JR
2235 domain->id = domain_id_alloc();
2236 if (!domain->id)
2650815f
JR
2237 goto out_err;
2238
aeb26f55
JR
2239 add_domain_to_list(domain);
2240
2650815f
JR
2241 return domain;
2242
2243out_err:
2244 kfree(domain);
2245
2246 return NULL;
2247}
2248
2249static int amd_iommu_domain_init(struct iommu_domain *dom)
2250{
2251 struct protection_domain *domain;
2252
2253 domain = protection_domain_alloc();
2254 if (!domain)
c156e347 2255 goto out_free;
2650815f
JR
2256
2257 domain->mode = PAGE_MODE_3_LEVEL;
c156e347
JR
2258 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2259 if (!domain->pt_root)
2260 goto out_free;
2261
2262 dom->priv = domain;
2263
2264 return 0;
2265
2266out_free:
2650815f 2267 protection_domain_free(domain);
c156e347
JR
2268
2269 return -ENOMEM;
2270}
2271
98383fc3
JR
2272static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2273{
2274 struct protection_domain *domain = dom->priv;
2275
2276 if (!domain)
2277 return;
2278
2279 if (domain->dev_cnt > 0)
2280 cleanup_domain(domain);
2281
2282 BUG_ON(domain->dev_cnt != 0);
2283
2284 free_pagetable(domain);
2285
2286 domain_id_free(domain->id);
2287
2288 kfree(domain);
2289
2290 dom->priv = NULL;
2291}
2292
684f2888
JR
2293static void amd_iommu_detach_device(struct iommu_domain *dom,
2294 struct device *dev)
2295{
684f2888
JR
2296 struct amd_iommu *iommu;
2297 struct pci_dev *pdev;
2298 u16 devid;
2299
2300 if (dev->bus != &pci_bus_type)
2301 return;
2302
2303 pdev = to_pci_dev(dev);
2304
2305 devid = calc_devid(pdev->bus->number, pdev->devfn);
2306
2307 if (devid > 0)
15898bbc 2308 detach_device(dev);
684f2888
JR
2309
2310 iommu = amd_iommu_rlookup_table[devid];
2311 if (!iommu)
2312 return;
2313
2314 iommu_queue_inv_dev_entry(iommu, devid);
2315 iommu_completion_wait(iommu);
2316}
2317
01106066
JR
2318static int amd_iommu_attach_device(struct iommu_domain *dom,
2319 struct device *dev)
2320{
2321 struct protection_domain *domain = dom->priv;
2322 struct protection_domain *old_domain;
2323 struct amd_iommu *iommu;
2324 struct pci_dev *pdev;
15898bbc 2325 int ret;
01106066
JR
2326 u16 devid;
2327
2328 if (dev->bus != &pci_bus_type)
2329 return -EINVAL;
2330
2331 pdev = to_pci_dev(dev);
2332
2333 devid = calc_devid(pdev->bus->number, pdev->devfn);
2334
2335 if (devid >= amd_iommu_last_bdf ||
2336 devid != amd_iommu_alias_table[devid])
2337 return -EINVAL;
2338
2339 iommu = amd_iommu_rlookup_table[devid];
2340 if (!iommu)
2341 return -EINVAL;
2342
15898bbc 2343 old_domain = amd_iommu_pd_table[devid];
01106066 2344 if (old_domain)
15898bbc 2345 detach_device(dev);
01106066 2346
15898bbc 2347 ret = attach_device(dev, domain);
01106066
JR
2348
2349 iommu_completion_wait(iommu);
2350
15898bbc 2351 return ret;
01106066
JR
2352}
2353
c6229ca6
JR
2354static int amd_iommu_map_range(struct iommu_domain *dom,
2355 unsigned long iova, phys_addr_t paddr,
2356 size_t size, int iommu_prot)
2357{
2358 struct protection_domain *domain = dom->priv;
2359 unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
2360 int prot = 0;
2361 int ret;
2362
2363 if (iommu_prot & IOMMU_READ)
2364 prot |= IOMMU_PROT_IR;
2365 if (iommu_prot & IOMMU_WRITE)
2366 prot |= IOMMU_PROT_IW;
2367
2368 iova &= PAGE_MASK;
2369 paddr &= PAGE_MASK;
2370
2371 for (i = 0; i < npages; ++i) {
abdc5eb3 2372 ret = iommu_map_page(domain, iova, paddr, prot, PM_MAP_4k);
c6229ca6
JR
2373 if (ret)
2374 return ret;
2375
2376 iova += PAGE_SIZE;
2377 paddr += PAGE_SIZE;
2378 }
2379
2380 return 0;
2381}
2382
eb74ff6c
JR
2383static void amd_iommu_unmap_range(struct iommu_domain *dom,
2384 unsigned long iova, size_t size)
2385{
2386
2387 struct protection_domain *domain = dom->priv;
2388 unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
2389
2390 iova &= PAGE_MASK;
2391
2392 for (i = 0; i < npages; ++i) {
a6b256b4 2393 iommu_unmap_page(domain, iova, PM_MAP_4k);
eb74ff6c
JR
2394 iova += PAGE_SIZE;
2395 }
2396
601367d7 2397 iommu_flush_tlb_pde(domain);
eb74ff6c
JR
2398}
2399
645c4c8d
JR
2400static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2401 unsigned long iova)
2402{
2403 struct protection_domain *domain = dom->priv;
2404 unsigned long offset = iova & ~PAGE_MASK;
2405 phys_addr_t paddr;
2406 u64 *pte;
2407
a6b256b4 2408 pte = fetch_pte(domain, iova, PM_MAP_4k);
645c4c8d 2409
a6d41a40 2410 if (!pte || !IOMMU_PTE_PRESENT(*pte))
645c4c8d
JR
2411 return 0;
2412
2413 paddr = *pte & IOMMU_PAGE_MASK;
2414 paddr |= offset;
2415
2416 return paddr;
2417}
2418
dbb9fd86
SY
2419static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2420 unsigned long cap)
2421{
2422 return 0;
2423}
2424
26961efe
JR
2425static struct iommu_ops amd_iommu_ops = {
2426 .domain_init = amd_iommu_domain_init,
2427 .domain_destroy = amd_iommu_domain_destroy,
2428 .attach_dev = amd_iommu_attach_device,
2429 .detach_dev = amd_iommu_detach_device,
2430 .map = amd_iommu_map_range,
2431 .unmap = amd_iommu_unmap_range,
2432 .iova_to_phys = amd_iommu_iova_to_phys,
dbb9fd86 2433 .domain_has_cap = amd_iommu_domain_has_cap,
26961efe
JR
2434};
2435
0feae533
JR
2436/*****************************************************************************
2437 *
2438 * The next functions do a basic initialization of IOMMU for pass through
2439 * mode
2440 *
2441 * In passthrough mode the IOMMU is initialized and enabled but not used for
2442 * DMA-API translation.
2443 *
2444 *****************************************************************************/
2445
2446int __init amd_iommu_init_passthrough(void)
2447{
15898bbc 2448 struct amd_iommu *iommu;
0feae533 2449 struct pci_dev *dev = NULL;
15898bbc 2450 u16 devid;
0feae533
JR
2451
2452 /* allocate passthroug domain */
2453 pt_domain = protection_domain_alloc();
2454 if (!pt_domain)
2455 return -ENOMEM;
2456
2457 pt_domain->mode |= PAGE_MODE_NONE;
2458
2459 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
0feae533
JR
2460
2461 devid = calc_devid(dev->bus->number, dev->devfn);
2462 if (devid > amd_iommu_last_bdf)
2463 continue;
2464
15898bbc 2465 iommu = amd_iommu_rlookup_table[devid];
0feae533
JR
2466 if (!iommu)
2467 continue;
2468
15898bbc 2469 attach_device(&dev->dev, pt_domain);
0feae533
JR
2470 }
2471
2472 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2473
2474 return 0;
2475}