x86/amd-iommu: Fix 3 possible endless loops
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / kernel / amd_iommu.c
CommitLineData
b6c02715 1/*
5d0d7156 2 * Copyright (C) 2007-2010 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>
cb41ed85 21#include <linux/pci-ats.h>
a66022c4 22#include <linux/bitmap.h>
5a0e3ad6 23#include <linux/slab.h>
7f26508b 24#include <linux/debugfs.h>
b6c02715 25#include <linux/scatterlist.h>
51491367 26#include <linux/dma-mapping.h>
b6c02715 27#include <linux/iommu-helper.h>
c156e347 28#include <linux/iommu.h>
815b33fd 29#include <linux/delay.h>
b6c02715 30#include <asm/proto.h>
46a7fa27 31#include <asm/iommu.h>
1d9b16d1 32#include <asm/gart.h>
6a9401a7 33#include <asm/amd_iommu_proto.h>
b6c02715 34#include <asm/amd_iommu_types.h>
c6da992e 35#include <asm/amd_iommu.h>
b6c02715
JR
36
37#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
38
815b33fd 39#define LOOP_TIMEOUT 100000
136f78a1 40
b6c02715
JR
41static DEFINE_RWLOCK(amd_iommu_devtable_lock);
42
bd60b735
JR
43/* A list of preallocated protection domains */
44static LIST_HEAD(iommu_pd_list);
45static DEFINE_SPINLOCK(iommu_pd_list_lock);
46
0feae533
JR
47/*
48 * Domain for untranslated devices - only allocated
49 * if iommu=pt passed on kernel cmd line.
50 */
51static struct protection_domain *pt_domain;
52
26961efe 53static struct iommu_ops amd_iommu_ops;
26961efe 54
431b2a20
JR
55/*
56 * general struct to manage commands send to an IOMMU
57 */
d6449536 58struct iommu_cmd {
b6c02715
JR
59 u32 data[4];
60};
61
04bfdd84 62static void update_domain(struct protection_domain *domain);
c1eee67b 63
15898bbc
JR
64/****************************************************************************
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
657cbb6b
JR
77static struct iommu_dev_data *get_dev_data(struct device *dev)
78{
79 return dev->archdata.iommu;
80}
81
71c70984
JR
82/*
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
98fc5a69
JR
110/*
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 */
339d3261 122 if (dev->bus != &pci_bus_type)
98fc5a69
JR
123 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
657cbb6b
JR
137static 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
b00d3bcf
JR
150 dev_data->dev = dev;
151
657cbb6b
JR
152 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
24100055
JR
158 atomic_set(&dev_data->bind, 0);
159
657cbb6b
JR
160 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}
b7cc9554
JR
170
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}
7f26508b
JR
207#ifdef CONFIG_AMD_IOMMU_STATS
208
209/*
210 * Initialization code for statistics collection
211 */
212
da49f6df 213DECLARE_STATS_COUNTER(compl_wait);
0f2a86f2 214DECLARE_STATS_COUNTER(cnt_map_single);
146a6917 215DECLARE_STATS_COUNTER(cnt_unmap_single);
d03f067a 216DECLARE_STATS_COUNTER(cnt_map_sg);
55877a6b 217DECLARE_STATS_COUNTER(cnt_unmap_sg);
c8f0fb36 218DECLARE_STATS_COUNTER(cnt_alloc_coherent);
5d31ee7e 219DECLARE_STATS_COUNTER(cnt_free_coherent);
c1858976 220DECLARE_STATS_COUNTER(cross_page);
f57d98ae 221DECLARE_STATS_COUNTER(domain_flush_single);
18811f55 222DECLARE_STATS_COUNTER(domain_flush_all);
5774f7c5 223DECLARE_STATS_COUNTER(alloced_io_mem);
8ecaf8f1 224DECLARE_STATS_COUNTER(total_map_requests);
da49f6df 225
7f26508b 226static struct dentry *stats_dir;
7f26508b
JR
227static 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
7f26508b
JR
244 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
245 (u32 *)&amd_iommu_unmap_flush);
da49f6df
JR
246
247 amd_iommu_stats_add(&compl_wait);
0f2a86f2 248 amd_iommu_stats_add(&cnt_map_single);
146a6917 249 amd_iommu_stats_add(&cnt_unmap_single);
d03f067a 250 amd_iommu_stats_add(&cnt_map_sg);
55877a6b 251 amd_iommu_stats_add(&cnt_unmap_sg);
c8f0fb36 252 amd_iommu_stats_add(&cnt_alloc_coherent);
5d31ee7e 253 amd_iommu_stats_add(&cnt_free_coherent);
c1858976 254 amd_iommu_stats_add(&cross_page);
f57d98ae 255 amd_iommu_stats_add(&domain_flush_single);
18811f55 256 amd_iommu_stats_add(&domain_flush_all);
5774f7c5 257 amd_iommu_stats_add(&alloced_io_mem);
8ecaf8f1 258 amd_iommu_stats_add(&total_map_requests);
7f26508b
JR
259}
260
261#endif
262
a80dc3e0
JR
263/****************************************************************************
264 *
265 * Interrupt handling functions
266 *
267 ****************************************************************************/
268
e3e59876
JR
269static 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
945b4ac4
JR
278static 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
a345b23b 287static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
90008ee4
JR
288{
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
4c6f40d4 296 printk(KERN_ERR "AMD-Vi: Event logged [");
90008ee4
JR
297
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);
e3e59876 304 dump_dte_entry(devid);
90008ee4
JR
305 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);
945b4ac4 326 dump_command(address);
90008ee4
JR
327 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) {
a345b23b 360 iommu_print_event(iommu, iommu->evt_buf + head);
90008ee4
JR
361 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
72fe00f0 369irqreturn_t amd_iommu_int_thread(int irq, void *data)
a80dc3e0 370{
90008ee4
JR
371 struct amd_iommu *iommu;
372
3bd22172 373 for_each_iommu(iommu)
90008ee4
JR
374 iommu_poll_events(iommu);
375
376 return IRQ_HANDLED;
a80dc3e0
JR
377}
378
72fe00f0
JR
379irqreturn_t amd_iommu_int_handler(int irq, void *data)
380{
381 return IRQ_WAKE_THREAD;
382}
383
431b2a20
JR
384/****************************************************************************
385 *
386 * IOMMU command queuing functions
387 *
388 ****************************************************************************/
389
ac0ea6e9
JR
390static int wait_on_sem(volatile u64 *sem)
391{
392 int i = 0;
393
394 while (*sem == 0 && i < LOOP_TIMEOUT) {
395 udelay(1);
396 i += 1;
397 }
398
399 if (i == LOOP_TIMEOUT) {
400 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
401 return -EIO;
402 }
403
404 return 0;
405}
406
407static void copy_cmd_to_buffer(struct amd_iommu *iommu,
408 struct iommu_cmd *cmd,
409 u32 tail)
a19ae1ec 410{
a19ae1ec
JR
411 u8 *target;
412
8a7c5ef3 413 target = iommu->cmd_buf + tail;
ac0ea6e9
JR
414 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
415
416 /* Copy command to buffer */
417 memcpy(target, cmd, sizeof(*cmd));
418
419 /* Tell the IOMMU about it */
a19ae1ec 420 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
ac0ea6e9 421}
a19ae1ec 422
815b33fd 423static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
ded46737 424{
815b33fd
JR
425 WARN_ON(address & 0x7ULL);
426
ded46737 427 memset(cmd, 0, sizeof(*cmd));
815b33fd
JR
428 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
429 cmd->data[1] = upper_32_bits(__pa(address));
430 cmd->data[2] = 1;
ded46737
JR
431 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
432}
433
94fe79e2
JR
434static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
435{
436 memset(cmd, 0, sizeof(*cmd));
437 cmd->data[0] = devid;
438 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
439}
440
11b6402c
JR
441static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
442 size_t size, u16 domid, int pde)
443{
444 u64 pages;
445 int s;
446
447 pages = iommu_num_pages(address, size, PAGE_SIZE);
448 s = 0;
449
450 if (pages > 1) {
451 /*
452 * If we have to flush more than one page, flush all
453 * TLB entries for this domain
454 */
455 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
456 s = 1;
457 }
458
459 address &= PAGE_MASK;
460
461 memset(cmd, 0, sizeof(*cmd));
462 cmd->data[1] |= domid;
463 cmd->data[2] = lower_32_bits(address);
464 cmd->data[3] = upper_32_bits(address);
465 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
466 if (s) /* size bit - we flush more than one 4kb page */
467 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
468 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
469 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
470}
471
cb41ed85
JR
472static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
473 u64 address, size_t size)
474{
475 u64 pages;
476 int s;
477
478 pages = iommu_num_pages(address, size, PAGE_SIZE);
479 s = 0;
480
481 if (pages > 1) {
482 /*
483 * If we have to flush more than one page, flush all
484 * TLB entries for this domain
485 */
486 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
487 s = 1;
488 }
489
490 address &= PAGE_MASK;
491
492 memset(cmd, 0, sizeof(*cmd));
493 cmd->data[0] = devid;
494 cmd->data[0] |= (qdep & 0xff) << 24;
495 cmd->data[1] = devid;
496 cmd->data[2] = lower_32_bits(address);
497 cmd->data[3] = upper_32_bits(address);
498 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
499 if (s)
500 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
501}
502
58fc7f14
JR
503static void build_inv_all(struct iommu_cmd *cmd)
504{
505 memset(cmd, 0, sizeof(*cmd));
506 CMD_SET_TYPE(cmd, CMD_INV_ALL);
a19ae1ec
JR
507}
508
431b2a20 509/*
431b2a20 510 * Writes the command to the IOMMUs command buffer and informs the
ac0ea6e9 511 * hardware about the new command.
431b2a20 512 */
d6449536 513static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
a19ae1ec 514{
ac0ea6e9 515 u32 left, tail, head, next_tail;
a19ae1ec 516 unsigned long flags;
a19ae1ec 517
549c90dc 518 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
ac0ea6e9
JR
519
520again:
a19ae1ec 521 spin_lock_irqsave(&iommu->lock, flags);
a19ae1ec 522
ac0ea6e9
JR
523 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
524 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
525 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
526 left = (head - next_tail) % iommu->cmd_buf_size;
a19ae1ec 527
ac0ea6e9
JR
528 if (left <= 2) {
529 struct iommu_cmd sync_cmd;
530 volatile u64 sem = 0;
531 int ret;
8d201968 532
ac0ea6e9
JR
533 build_completion_wait(&sync_cmd, (u64)&sem);
534 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
da49f6df 535
ac0ea6e9
JR
536 spin_unlock_irqrestore(&iommu->lock, flags);
537
538 if ((ret = wait_on_sem(&sem)) != 0)
539 return ret;
540
541 goto again;
8d201968
JR
542 }
543
ac0ea6e9
JR
544 copy_cmd_to_buffer(iommu, cmd, tail);
545
546 /* We need to sync now to make sure all commands are processed */
815b33fd 547 iommu->need_sync = true;
ac0ea6e9 548
a19ae1ec 549 spin_unlock_irqrestore(&iommu->lock, flags);
8d201968 550
815b33fd 551 return 0;
8d201968
JR
552}
553
554/*
555 * This function queues a completion wait command into the command
556 * buffer of an IOMMU
557 */
a19ae1ec 558static int iommu_completion_wait(struct amd_iommu *iommu)
8d201968
JR
559{
560 struct iommu_cmd cmd;
815b33fd 561 volatile u64 sem = 0;
ac0ea6e9 562 int ret;
8d201968 563
09ee17eb 564 if (!iommu->need_sync)
815b33fd 565 return 0;
09ee17eb 566
815b33fd 567 build_completion_wait(&cmd, (u64)&sem);
a19ae1ec 568
815b33fd 569 ret = iommu_queue_command(iommu, &cmd);
a19ae1ec 570 if (ret)
815b33fd 571 return ret;
8d201968 572
ac0ea6e9 573 return wait_on_sem(&sem);
8d201968
JR
574}
575
d8c13085 576static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
a19ae1ec 577{
d8c13085 578 struct iommu_cmd cmd;
a19ae1ec 579
d8c13085 580 build_inv_dte(&cmd, devid);
7e4f88da 581
d8c13085
JR
582 return iommu_queue_command(iommu, &cmd);
583}
09ee17eb 584
7d0c5cc5
JR
585static void iommu_flush_dte_all(struct amd_iommu *iommu)
586{
587 u32 devid;
09ee17eb 588
7d0c5cc5
JR
589 for (devid = 0; devid <= 0xffff; ++devid)
590 iommu_flush_dte(iommu, devid);
a19ae1ec 591
7d0c5cc5
JR
592 iommu_completion_wait(iommu);
593}
84df8175 594
7d0c5cc5
JR
595/*
596 * This function uses heavy locking and may disable irqs for some time. But
597 * this is no issue because it is only called during resume.
598 */
599static void iommu_flush_tlb_all(struct amd_iommu *iommu)
600{
601 u32 dom_id;
a19ae1ec 602
7d0c5cc5
JR
603 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
604 struct iommu_cmd cmd;
605 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
606 dom_id, 1);
607 iommu_queue_command(iommu, &cmd);
608 }
8eed9833 609
7d0c5cc5 610 iommu_completion_wait(iommu);
a19ae1ec
JR
611}
612
58fc7f14 613static void iommu_flush_all(struct amd_iommu *iommu)
0518a3a4 614{
58fc7f14 615 struct iommu_cmd cmd;
0518a3a4 616
58fc7f14 617 build_inv_all(&cmd);
0518a3a4 618
58fc7f14
JR
619 iommu_queue_command(iommu, &cmd);
620 iommu_completion_wait(iommu);
621}
622
7d0c5cc5
JR
623void iommu_flush_all_caches(struct amd_iommu *iommu)
624{
58fc7f14
JR
625 if (iommu_feature(iommu, FEATURE_IA)) {
626 iommu_flush_all(iommu);
627 } else {
628 iommu_flush_dte_all(iommu);
629 iommu_flush_tlb_all(iommu);
0518a3a4
JR
630 }
631}
632
431b2a20 633/*
cb41ed85 634 * Command send function for flushing on-device TLB
431b2a20 635 */
cb41ed85 636static int device_flush_iotlb(struct device *dev, u64 address, size_t size)
3fa43655 637{
cb41ed85 638 struct pci_dev *pdev = to_pci_dev(dev);
3fa43655 639 struct amd_iommu *iommu;
b00d3bcf 640 struct iommu_cmd cmd;
3fa43655 641 u16 devid;
cb41ed85 642 int qdep;
3fa43655 643
cb41ed85 644 qdep = pci_ats_queue_depth(pdev);
3fa43655
JR
645 devid = get_device_id(dev);
646 iommu = amd_iommu_rlookup_table[devid];
647
cb41ed85 648 build_inv_iotlb_pages(&cmd, devid, qdep, address, size);
b00d3bcf
JR
649
650 return iommu_queue_command(iommu, &cmd);
3fa43655
JR
651}
652
431b2a20 653/*
431b2a20 654 * Command send function for invalidating a device table entry
431b2a20 655 */
d8c13085 656static int device_flush_dte(struct device *dev)
a19ae1ec 657{
3fa43655 658 struct amd_iommu *iommu;
cb41ed85 659 struct pci_dev *pdev;
3fa43655 660 u16 devid;
ee2fa743 661 int ret;
a19ae1ec 662
cb41ed85 663 pdev = to_pci_dev(dev);
3fa43655
JR
664 devid = get_device_id(dev);
665 iommu = amd_iommu_rlookup_table[devid];
a19ae1ec 666
cb41ed85
JR
667 ret = iommu_flush_dte(iommu, devid);
668 if (ret)
669 return ret;
670
671 if (pci_ats_enabled(pdev))
672 ret = device_flush_iotlb(dev, 0, ~0UL);
ee2fa743 673
ee2fa743 674 return ret;
a19ae1ec
JR
675}
676
431b2a20
JR
677/*
678 * TLB invalidation function which is called from the mapping functions.
679 * It invalidates a single PTE if the range to flush is within a single
680 * page. Otherwise it flushes the whole TLB of the IOMMU.
681 */
17b124bf
JR
682static void __domain_flush_pages(struct protection_domain *domain,
683 u64 address, size_t size, int pde)
a19ae1ec 684{
cb41ed85 685 struct iommu_dev_data *dev_data;
11b6402c
JR
686 struct iommu_cmd cmd;
687 int ret = 0, i;
a19ae1ec 688
11b6402c 689 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
999ba417 690
6de8ad9b
JR
691 for (i = 0; i < amd_iommus_present; ++i) {
692 if (!domain->dev_iommu[i])
693 continue;
694
695 /*
696 * Devices of this domain are behind this IOMMU
697 * We need a TLB flush
698 */
11b6402c 699 ret |= iommu_queue_command(amd_iommus[i], &cmd);
6de8ad9b
JR
700 }
701
cb41ed85
JR
702 list_for_each_entry(dev_data, &domain->dev_list, list) {
703 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
704
705 if (!pci_ats_enabled(pdev))
706 continue;
707
708 ret |= device_flush_iotlb(dev_data->dev, address, size);
709 }
710
11b6402c 711 WARN_ON(ret);
6de8ad9b
JR
712}
713
17b124bf
JR
714static void domain_flush_pages(struct protection_domain *domain,
715 u64 address, size_t size)
6de8ad9b 716{
17b124bf 717 __domain_flush_pages(domain, address, size, 0);
a19ae1ec 718}
b6c02715 719
1c655773 720/* Flush the whole IO/TLB for a given protection domain */
17b124bf 721static void domain_flush_tlb(struct protection_domain *domain)
1c655773 722{
17b124bf 723 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
1c655773
JR
724}
725
42a49f96 726/* Flush the whole IO/TLB for a given protection domain - including PDE */
17b124bf 727static void domain_flush_tlb_pde(struct protection_domain *domain)
42a49f96 728{
17b124bf 729 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
42a49f96
CW
730}
731
17b124bf 732static void domain_flush_complete(struct protection_domain *domain)
b00d3bcf 733{
17b124bf 734 int i;
18811f55 735
17b124bf
JR
736 for (i = 0; i < amd_iommus_present; ++i) {
737 if (!domain->dev_iommu[i])
738 continue;
bfd1be18 739
17b124bf
JR
740 /*
741 * Devices of this domain are behind this IOMMU
742 * We need to wait for completion of all commands.
743 */
744 iommu_completion_wait(amd_iommus[i]);
bfd1be18 745 }
e394d72a
JR
746}
747
b00d3bcf 748
09b42804 749/*
b00d3bcf 750 * This function flushes the DTEs for all devices in domain
09b42804 751 */
17b124bf 752static void domain_flush_devices(struct protection_domain *domain)
e394d72a 753{
b00d3bcf 754 struct iommu_dev_data *dev_data;
09b42804
JR
755 unsigned long flags;
756
b00d3bcf 757 spin_lock_irqsave(&domain->lock, flags);
b26e81b8 758
b00d3bcf 759 list_for_each_entry(dev_data, &domain->dev_list, list)
d8c13085 760 device_flush_dte(dev_data->dev);
b26e81b8 761
b00d3bcf 762 spin_unlock_irqrestore(&domain->lock, flags);
a345b23b
JR
763}
764
431b2a20
JR
765/****************************************************************************
766 *
767 * The functions below are used the create the page table mappings for
768 * unity mapped regions.
769 *
770 ****************************************************************************/
771
308973d3
JR
772/*
773 * This function is used to add another level to an IO page table. Adding
774 * another level increases the size of the address space by 9 bits to a size up
775 * to 64 bits.
776 */
777static bool increase_address_space(struct protection_domain *domain,
778 gfp_t gfp)
779{
780 u64 *pte;
781
782 if (domain->mode == PAGE_MODE_6_LEVEL)
783 /* address space already 64 bit large */
784 return false;
785
786 pte = (void *)get_zeroed_page(gfp);
787 if (!pte)
788 return false;
789
790 *pte = PM_LEVEL_PDE(domain->mode,
791 virt_to_phys(domain->pt_root));
792 domain->pt_root = pte;
793 domain->mode += 1;
794 domain->updated = true;
795
796 return true;
797}
798
799static u64 *alloc_pte(struct protection_domain *domain,
800 unsigned long address,
cbb9d729 801 unsigned long page_size,
308973d3
JR
802 u64 **pte_page,
803 gfp_t gfp)
804{
cbb9d729 805 int level, end_lvl;
308973d3 806 u64 *pte, *page;
cbb9d729
JR
807
808 BUG_ON(!is_power_of_2(page_size));
308973d3
JR
809
810 while (address > PM_LEVEL_SIZE(domain->mode))
811 increase_address_space(domain, gfp);
812
cbb9d729
JR
813 level = domain->mode - 1;
814 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
815 address = PAGE_SIZE_ALIGN(address, page_size);
816 end_lvl = PAGE_SIZE_LEVEL(page_size);
308973d3
JR
817
818 while (level > end_lvl) {
819 if (!IOMMU_PTE_PRESENT(*pte)) {
820 page = (u64 *)get_zeroed_page(gfp);
821 if (!page)
822 return NULL;
823 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
824 }
825
cbb9d729
JR
826 /* No level skipping support yet */
827 if (PM_PTE_LEVEL(*pte) != level)
828 return NULL;
829
308973d3
JR
830 level -= 1;
831
832 pte = IOMMU_PTE_PAGE(*pte);
833
834 if (pte_page && level == end_lvl)
835 *pte_page = pte;
836
837 pte = &pte[PM_LEVEL_INDEX(level, address)];
838 }
839
840 return pte;
841}
842
843/*
844 * This function checks if there is a PTE for a given dma address. If
845 * there is one, it returns the pointer to it.
846 */
24cd7723 847static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
308973d3
JR
848{
849 int level;
850 u64 *pte;
851
24cd7723
JR
852 if (address > PM_LEVEL_SIZE(domain->mode))
853 return NULL;
854
855 level = domain->mode - 1;
856 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
308973d3 857
24cd7723
JR
858 while (level > 0) {
859
860 /* Not Present */
308973d3
JR
861 if (!IOMMU_PTE_PRESENT(*pte))
862 return NULL;
863
24cd7723
JR
864 /* Large PTE */
865 if (PM_PTE_LEVEL(*pte) == 0x07) {
866 unsigned long pte_mask, __pte;
867
868 /*
869 * If we have a series of large PTEs, make
870 * sure to return a pointer to the first one.
871 */
872 pte_mask = PTE_PAGE_SIZE(*pte);
873 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
874 __pte = ((unsigned long)pte) & pte_mask;
875
876 return (u64 *)__pte;
877 }
878
879 /* No level skipping support yet */
880 if (PM_PTE_LEVEL(*pte) != level)
881 return NULL;
882
308973d3
JR
883 level -= 1;
884
24cd7723 885 /* Walk to the next level */
308973d3
JR
886 pte = IOMMU_PTE_PAGE(*pte);
887 pte = &pte[PM_LEVEL_INDEX(level, address)];
308973d3
JR
888 }
889
890 return pte;
891}
892
431b2a20
JR
893/*
894 * Generic mapping functions. It maps a physical address into a DMA
895 * address space. It allocates the page table pages if necessary.
896 * In the future it can be extended to a generic mapping function
897 * supporting all features of AMD IOMMU page tables like level skipping
898 * and full 64 bit address spaces.
899 */
38e817fe
JR
900static int iommu_map_page(struct protection_domain *dom,
901 unsigned long bus_addr,
902 unsigned long phys_addr,
abdc5eb3 903 int prot,
cbb9d729 904 unsigned long page_size)
bd0e5211 905{
8bda3092 906 u64 __pte, *pte;
cbb9d729 907 int i, count;
abdc5eb3 908
bad1cac2 909 if (!(prot & IOMMU_PROT_MASK))
bd0e5211
JR
910 return -EINVAL;
911
cbb9d729
JR
912 bus_addr = PAGE_ALIGN(bus_addr);
913 phys_addr = PAGE_ALIGN(phys_addr);
914 count = PAGE_SIZE_PTE_COUNT(page_size);
915 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
916
917 for (i = 0; i < count; ++i)
918 if (IOMMU_PTE_PRESENT(pte[i]))
919 return -EBUSY;
bd0e5211 920
cbb9d729
JR
921 if (page_size > PAGE_SIZE) {
922 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
923 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
924 } else
925 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
bd0e5211 926
bd0e5211
JR
927 if (prot & IOMMU_PROT_IR)
928 __pte |= IOMMU_PTE_IR;
929 if (prot & IOMMU_PROT_IW)
930 __pte |= IOMMU_PTE_IW;
931
cbb9d729
JR
932 for (i = 0; i < count; ++i)
933 pte[i] = __pte;
bd0e5211 934
04bfdd84
JR
935 update_domain(dom);
936
bd0e5211
JR
937 return 0;
938}
939
24cd7723
JR
940static unsigned long iommu_unmap_page(struct protection_domain *dom,
941 unsigned long bus_addr,
942 unsigned long page_size)
eb74ff6c 943{
24cd7723
JR
944 unsigned long long unmap_size, unmapped;
945 u64 *pte;
946
947 BUG_ON(!is_power_of_2(page_size));
948
949 unmapped = 0;
eb74ff6c 950
24cd7723
JR
951 while (unmapped < page_size) {
952
953 pte = fetch_pte(dom, bus_addr);
954
955 if (!pte) {
956 /*
957 * No PTE for this address
958 * move forward in 4kb steps
959 */
960 unmap_size = PAGE_SIZE;
961 } else if (PM_PTE_LEVEL(*pte) == 0) {
962 /* 4kb PTE found for this address */
963 unmap_size = PAGE_SIZE;
964 *pte = 0ULL;
965 } else {
966 int count, i;
967
968 /* Large PTE found which maps this address */
969 unmap_size = PTE_PAGE_SIZE(*pte);
970 count = PAGE_SIZE_PTE_COUNT(unmap_size);
971 for (i = 0; i < count; i++)
972 pte[i] = 0ULL;
973 }
974
975 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
976 unmapped += unmap_size;
977 }
978
979 BUG_ON(!is_power_of_2(unmapped));
eb74ff6c 980
24cd7723 981 return unmapped;
eb74ff6c 982}
eb74ff6c 983
431b2a20
JR
984/*
985 * This function checks if a specific unity mapping entry is needed for
986 * this specific IOMMU.
987 */
bd0e5211
JR
988static int iommu_for_unity_map(struct amd_iommu *iommu,
989 struct unity_map_entry *entry)
990{
991 u16 bdf, i;
992
993 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
994 bdf = amd_iommu_alias_table[i];
995 if (amd_iommu_rlookup_table[bdf] == iommu)
996 return 1;
997 }
998
999 return 0;
1000}
1001
431b2a20
JR
1002/*
1003 * This function actually applies the mapping to the page table of the
1004 * dma_ops domain.
1005 */
bd0e5211
JR
1006static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1007 struct unity_map_entry *e)
1008{
1009 u64 addr;
1010 int ret;
1011
1012 for (addr = e->address_start; addr < e->address_end;
1013 addr += PAGE_SIZE) {
abdc5eb3 1014 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
cbb9d729 1015 PAGE_SIZE);
bd0e5211
JR
1016 if (ret)
1017 return ret;
1018 /*
1019 * if unity mapping is in aperture range mark the page
1020 * as allocated in the aperture
1021 */
1022 if (addr < dma_dom->aperture_size)
c3239567 1023 __set_bit(addr >> PAGE_SHIFT,
384de729 1024 dma_dom->aperture[0]->bitmap);
bd0e5211
JR
1025 }
1026
1027 return 0;
1028}
1029
171e7b37
JR
1030/*
1031 * Init the unity mappings for a specific IOMMU in the system
1032 *
1033 * Basically iterates over all unity mapping entries and applies them to
1034 * the default domain DMA of that IOMMU if necessary.
1035 */
1036static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1037{
1038 struct unity_map_entry *entry;
1039 int ret;
1040
1041 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1042 if (!iommu_for_unity_map(iommu, entry))
1043 continue;
1044 ret = dma_ops_unity_map(iommu->default_dom, entry);
1045 if (ret)
1046 return ret;
1047 }
1048
1049 return 0;
1050}
1051
431b2a20
JR
1052/*
1053 * Inits the unity mappings required for a specific device
1054 */
bd0e5211
JR
1055static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1056 u16 devid)
1057{
1058 struct unity_map_entry *e;
1059 int ret;
1060
1061 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1062 if (!(devid >= e->devid_start && devid <= e->devid_end))
1063 continue;
1064 ret = dma_ops_unity_map(dma_dom, e);
1065 if (ret)
1066 return ret;
1067 }
1068
1069 return 0;
1070}
1071
431b2a20
JR
1072/****************************************************************************
1073 *
1074 * The next functions belong to the address allocator for the dma_ops
1075 * interface functions. They work like the allocators in the other IOMMU
1076 * drivers. Its basically a bitmap which marks the allocated pages in
1077 * the aperture. Maybe it could be enhanced in the future to a more
1078 * efficient allocator.
1079 *
1080 ****************************************************************************/
d3086444 1081
431b2a20 1082/*
384de729 1083 * The address allocator core functions.
431b2a20
JR
1084 *
1085 * called with domain->lock held
1086 */
384de729 1087
171e7b37
JR
1088/*
1089 * Used to reserve address ranges in the aperture (e.g. for exclusion
1090 * ranges.
1091 */
1092static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1093 unsigned long start_page,
1094 unsigned int pages)
1095{
1096 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1097
1098 if (start_page + pages > last_page)
1099 pages = last_page - start_page;
1100
1101 for (i = start_page; i < start_page + pages; ++i) {
1102 int index = i / APERTURE_RANGE_PAGES;
1103 int page = i % APERTURE_RANGE_PAGES;
1104 __set_bit(page, dom->aperture[index]->bitmap);
1105 }
1106}
1107
9cabe89b
JR
1108/*
1109 * This function is used to add a new aperture range to an existing
1110 * aperture in case of dma_ops domain allocation or address allocation
1111 * failure.
1112 */
576175c2 1113static int alloc_new_range(struct dma_ops_domain *dma_dom,
9cabe89b
JR
1114 bool populate, gfp_t gfp)
1115{
1116 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
576175c2 1117 struct amd_iommu *iommu;
d91afd15 1118 unsigned long i;
9cabe89b 1119
f5e9705c
JR
1120#ifdef CONFIG_IOMMU_STRESS
1121 populate = false;
1122#endif
1123
9cabe89b
JR
1124 if (index >= APERTURE_MAX_RANGES)
1125 return -ENOMEM;
1126
1127 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1128 if (!dma_dom->aperture[index])
1129 return -ENOMEM;
1130
1131 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1132 if (!dma_dom->aperture[index]->bitmap)
1133 goto out_free;
1134
1135 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1136
1137 if (populate) {
1138 unsigned long address = dma_dom->aperture_size;
1139 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1140 u64 *pte, *pte_page;
1141
1142 for (i = 0; i < num_ptes; ++i) {
cbb9d729 1143 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
9cabe89b
JR
1144 &pte_page, gfp);
1145 if (!pte)
1146 goto out_free;
1147
1148 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1149
1150 address += APERTURE_RANGE_SIZE / 64;
1151 }
1152 }
1153
1154 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1155
b595076a 1156 /* Initialize the exclusion range if necessary */
576175c2
JR
1157 for_each_iommu(iommu) {
1158 if (iommu->exclusion_start &&
1159 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1160 && iommu->exclusion_start < dma_dom->aperture_size) {
1161 unsigned long startpage;
1162 int pages = iommu_num_pages(iommu->exclusion_start,
1163 iommu->exclusion_length,
1164 PAGE_SIZE);
1165 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1166 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1167 }
00cd122a
JR
1168 }
1169
1170 /*
1171 * Check for areas already mapped as present in the new aperture
1172 * range and mark those pages as reserved in the allocator. Such
1173 * mappings may already exist as a result of requested unity
1174 * mappings for devices.
1175 */
1176 for (i = dma_dom->aperture[index]->offset;
1177 i < dma_dom->aperture_size;
1178 i += PAGE_SIZE) {
24cd7723 1179 u64 *pte = fetch_pte(&dma_dom->domain, i);
00cd122a
JR
1180 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1181 continue;
1182
1183 dma_ops_reserve_addresses(dma_dom, i << PAGE_SHIFT, 1);
1184 }
1185
04bfdd84
JR
1186 update_domain(&dma_dom->domain);
1187
9cabe89b
JR
1188 return 0;
1189
1190out_free:
04bfdd84
JR
1191 update_domain(&dma_dom->domain);
1192
9cabe89b
JR
1193 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1194
1195 kfree(dma_dom->aperture[index]);
1196 dma_dom->aperture[index] = NULL;
1197
1198 return -ENOMEM;
1199}
1200
384de729
JR
1201static unsigned long dma_ops_area_alloc(struct device *dev,
1202 struct dma_ops_domain *dom,
1203 unsigned int pages,
1204 unsigned long align_mask,
1205 u64 dma_mask,
1206 unsigned long start)
1207{
803b8cb4 1208 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
384de729
JR
1209 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1210 int i = start >> APERTURE_RANGE_SHIFT;
1211 unsigned long boundary_size;
1212 unsigned long address = -1;
1213 unsigned long limit;
1214
803b8cb4
JR
1215 next_bit >>= PAGE_SHIFT;
1216
384de729
JR
1217 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1218 PAGE_SIZE) >> PAGE_SHIFT;
1219
1220 for (;i < max_index; ++i) {
1221 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1222
1223 if (dom->aperture[i]->offset >= dma_mask)
1224 break;
1225
1226 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1227 dma_mask >> PAGE_SHIFT);
1228
1229 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1230 limit, next_bit, pages, 0,
1231 boundary_size, align_mask);
1232 if (address != -1) {
1233 address = dom->aperture[i]->offset +
1234 (address << PAGE_SHIFT);
803b8cb4 1235 dom->next_address = address + (pages << PAGE_SHIFT);
384de729
JR
1236 break;
1237 }
1238
1239 next_bit = 0;
1240 }
1241
1242 return address;
1243}
1244
d3086444
JR
1245static unsigned long dma_ops_alloc_addresses(struct device *dev,
1246 struct dma_ops_domain *dom,
6d4f343f 1247 unsigned int pages,
832a90c3
JR
1248 unsigned long align_mask,
1249 u64 dma_mask)
d3086444 1250{
d3086444 1251 unsigned long address;
d3086444 1252
fe16f088
JR
1253#ifdef CONFIG_IOMMU_STRESS
1254 dom->next_address = 0;
1255 dom->need_flush = true;
1256#endif
d3086444 1257
384de729 1258 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
803b8cb4 1259 dma_mask, dom->next_address);
d3086444 1260
1c655773 1261 if (address == -1) {
803b8cb4 1262 dom->next_address = 0;
384de729
JR
1263 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1264 dma_mask, 0);
1c655773
JR
1265 dom->need_flush = true;
1266 }
d3086444 1267
384de729 1268 if (unlikely(address == -1))
8fd524b3 1269 address = DMA_ERROR_CODE;
d3086444
JR
1270
1271 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1272
1273 return address;
1274}
1275
431b2a20
JR
1276/*
1277 * The address free function.
1278 *
1279 * called with domain->lock held
1280 */
d3086444
JR
1281static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1282 unsigned long address,
1283 unsigned int pages)
1284{
384de729
JR
1285 unsigned i = address >> APERTURE_RANGE_SHIFT;
1286 struct aperture_range *range = dom->aperture[i];
80be308d 1287
384de729
JR
1288 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1289
47bccd6b
JR
1290#ifdef CONFIG_IOMMU_STRESS
1291 if (i < 4)
1292 return;
1293#endif
80be308d 1294
803b8cb4 1295 if (address >= dom->next_address)
80be308d 1296 dom->need_flush = true;
384de729
JR
1297
1298 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
803b8cb4 1299
a66022c4 1300 bitmap_clear(range->bitmap, address, pages);
384de729 1301
d3086444
JR
1302}
1303
431b2a20
JR
1304/****************************************************************************
1305 *
1306 * The next functions belong to the domain allocation. A domain is
1307 * allocated for every IOMMU as the default domain. If device isolation
1308 * is enabled, every device get its own domain. The most important thing
1309 * about domains is the page table mapping the DMA address space they
1310 * contain.
1311 *
1312 ****************************************************************************/
1313
aeb26f55
JR
1314/*
1315 * This function adds a protection domain to the global protection domain list
1316 */
1317static void add_domain_to_list(struct protection_domain *domain)
1318{
1319 unsigned long flags;
1320
1321 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1322 list_add(&domain->list, &amd_iommu_pd_list);
1323 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1324}
1325
1326/*
1327 * This function removes a protection domain to the global
1328 * protection domain list
1329 */
1330static void del_domain_from_list(struct protection_domain *domain)
1331{
1332 unsigned long flags;
1333
1334 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1335 list_del(&domain->list);
1336 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1337}
1338
ec487d1a
JR
1339static u16 domain_id_alloc(void)
1340{
1341 unsigned long flags;
1342 int id;
1343
1344 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1345 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1346 BUG_ON(id == 0);
1347 if (id > 0 && id < MAX_DOMAIN_ID)
1348 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1349 else
1350 id = 0;
1351 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1352
1353 return id;
1354}
1355
a2acfb75
JR
1356static void domain_id_free(int id)
1357{
1358 unsigned long flags;
1359
1360 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1361 if (id > 0 && id < MAX_DOMAIN_ID)
1362 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1363 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1364}
a2acfb75 1365
86db2e5d 1366static void free_pagetable(struct protection_domain *domain)
ec487d1a
JR
1367{
1368 int i, j;
1369 u64 *p1, *p2, *p3;
1370
86db2e5d 1371 p1 = domain->pt_root;
ec487d1a
JR
1372
1373 if (!p1)
1374 return;
1375
1376 for (i = 0; i < 512; ++i) {
1377 if (!IOMMU_PTE_PRESENT(p1[i]))
1378 continue;
1379
1380 p2 = IOMMU_PTE_PAGE(p1[i]);
3cc3d84b 1381 for (j = 0; j < 512; ++j) {
ec487d1a
JR
1382 if (!IOMMU_PTE_PRESENT(p2[j]))
1383 continue;
1384 p3 = IOMMU_PTE_PAGE(p2[j]);
1385 free_page((unsigned long)p3);
1386 }
1387
1388 free_page((unsigned long)p2);
1389 }
1390
1391 free_page((unsigned long)p1);
86db2e5d
JR
1392
1393 domain->pt_root = NULL;
ec487d1a
JR
1394}
1395
431b2a20
JR
1396/*
1397 * Free a domain, only used if something went wrong in the
1398 * allocation path and we need to free an already allocated page table
1399 */
ec487d1a
JR
1400static void dma_ops_domain_free(struct dma_ops_domain *dom)
1401{
384de729
JR
1402 int i;
1403
ec487d1a
JR
1404 if (!dom)
1405 return;
1406
aeb26f55
JR
1407 del_domain_from_list(&dom->domain);
1408
86db2e5d 1409 free_pagetable(&dom->domain);
ec487d1a 1410
384de729
JR
1411 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1412 if (!dom->aperture[i])
1413 continue;
1414 free_page((unsigned long)dom->aperture[i]->bitmap);
1415 kfree(dom->aperture[i]);
1416 }
ec487d1a
JR
1417
1418 kfree(dom);
1419}
1420
431b2a20
JR
1421/*
1422 * Allocates a new protection domain usable for the dma_ops functions.
b595076a 1423 * It also initializes the page table and the address allocator data
431b2a20
JR
1424 * structures required for the dma_ops interface
1425 */
87a64d52 1426static struct dma_ops_domain *dma_ops_domain_alloc(void)
ec487d1a
JR
1427{
1428 struct dma_ops_domain *dma_dom;
ec487d1a
JR
1429
1430 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1431 if (!dma_dom)
1432 return NULL;
1433
1434 spin_lock_init(&dma_dom->domain.lock);
1435
1436 dma_dom->domain.id = domain_id_alloc();
1437 if (dma_dom->domain.id == 0)
1438 goto free_dma_dom;
7c392cbe 1439 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
8f7a017c 1440 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
ec487d1a 1441 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
9fdb19d6 1442 dma_dom->domain.flags = PD_DMA_OPS_MASK;
ec487d1a
JR
1443 dma_dom->domain.priv = dma_dom;
1444 if (!dma_dom->domain.pt_root)
1445 goto free_dma_dom;
ec487d1a 1446
1c655773 1447 dma_dom->need_flush = false;
bd60b735 1448 dma_dom->target_dev = 0xffff;
1c655773 1449
aeb26f55
JR
1450 add_domain_to_list(&dma_dom->domain);
1451
576175c2 1452 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
ec487d1a 1453 goto free_dma_dom;
ec487d1a 1454
431b2a20 1455 /*
ec487d1a
JR
1456 * mark the first page as allocated so we never return 0 as
1457 * a valid dma-address. So we can use 0 as error value
431b2a20 1458 */
384de729 1459 dma_dom->aperture[0]->bitmap[0] = 1;
803b8cb4 1460 dma_dom->next_address = 0;
ec487d1a 1461
ec487d1a
JR
1462
1463 return dma_dom;
1464
1465free_dma_dom:
1466 dma_ops_domain_free(dma_dom);
1467
1468 return NULL;
1469}
1470
5b28df6f
JR
1471/*
1472 * little helper function to check whether a given protection domain is a
1473 * dma_ops domain
1474 */
1475static bool dma_ops_domain(struct protection_domain *domain)
1476{
1477 return domain->flags & PD_DMA_OPS_MASK;
1478}
1479
fd7b5535 1480static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
b20ac0d4 1481{
b20ac0d4 1482 u64 pte_root = virt_to_phys(domain->pt_root);
fd7b5535 1483 u32 flags = 0;
863c74eb 1484
38ddf41b
JR
1485 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1486 << DEV_ENTRY_MODE_SHIFT;
1487 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
b20ac0d4 1488
fd7b5535
JR
1489 if (ats)
1490 flags |= DTE_FLAG_IOTLB;
1491
1492 amd_iommu_dev_table[devid].data[3] |= flags;
1493 amd_iommu_dev_table[devid].data[2] = domain->id;
1494 amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
1495 amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
15898bbc
JR
1496}
1497
1498static void clear_dte_entry(u16 devid)
1499{
15898bbc
JR
1500 /* remove entry from the device table seen by the hardware */
1501 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1502 amd_iommu_dev_table[devid].data[1] = 0;
1503 amd_iommu_dev_table[devid].data[2] = 0;
1504
1505 amd_iommu_apply_erratum_63(devid);
7f760ddd
JR
1506}
1507
1508static void do_attach(struct device *dev, struct protection_domain *domain)
1509{
1510 struct iommu_dev_data *dev_data;
1511 struct amd_iommu *iommu;
fd7b5535
JR
1512 struct pci_dev *pdev;
1513 bool ats = false;
7f760ddd
JR
1514 u16 devid;
1515
1516 devid = get_device_id(dev);
1517 iommu = amd_iommu_rlookup_table[devid];
1518 dev_data = get_dev_data(dev);
fd7b5535
JR
1519 pdev = to_pci_dev(dev);
1520
1521 if (amd_iommu_iotlb_sup)
1522 ats = pci_ats_enabled(pdev);
7f760ddd
JR
1523
1524 /* Update data structures */
1525 dev_data->domain = domain;
1526 list_add(&dev_data->list, &domain->dev_list);
fd7b5535 1527 set_dte_entry(devid, domain, ats);
7f760ddd
JR
1528
1529 /* Do reference counting */
1530 domain->dev_iommu[iommu->index] += 1;
1531 domain->dev_cnt += 1;
1532
1533 /* Flush the DTE entry */
d8c13085 1534 device_flush_dte(dev);
7f760ddd
JR
1535}
1536
1537static void do_detach(struct device *dev)
1538{
1539 struct iommu_dev_data *dev_data;
1540 struct amd_iommu *iommu;
1541 u16 devid;
1542
1543 devid = get_device_id(dev);
1544 iommu = amd_iommu_rlookup_table[devid];
1545 dev_data = get_dev_data(dev);
15898bbc
JR
1546
1547 /* decrease reference counters */
7f760ddd
JR
1548 dev_data->domain->dev_iommu[iommu->index] -= 1;
1549 dev_data->domain->dev_cnt -= 1;
1550
1551 /* Update data structures */
1552 dev_data->domain = NULL;
1553 list_del(&dev_data->list);
1554 clear_dte_entry(devid);
15898bbc 1555
7f760ddd 1556 /* Flush the DTE entry */
d8c13085 1557 device_flush_dte(dev);
2b681faf
JR
1558}
1559
1560/*
1561 * If a device is not yet associated with a domain, this function does
1562 * assigns it visible for the hardware
1563 */
15898bbc
JR
1564static int __attach_device(struct device *dev,
1565 struct protection_domain *domain)
2b681faf 1566{
657cbb6b 1567 struct iommu_dev_data *dev_data, *alias_data;
84fe6c19 1568 int ret;
657cbb6b 1569
657cbb6b
JR
1570 dev_data = get_dev_data(dev);
1571 alias_data = get_dev_data(dev_data->alias);
7f760ddd 1572
657cbb6b
JR
1573 if (!alias_data)
1574 return -EINVAL;
15898bbc 1575
2b681faf
JR
1576 /* lock domain */
1577 spin_lock(&domain->lock);
1578
15898bbc 1579 /* Some sanity checks */
84fe6c19 1580 ret = -EBUSY;
657cbb6b
JR
1581 if (alias_data->domain != NULL &&
1582 alias_data->domain != domain)
84fe6c19 1583 goto out_unlock;
eba6ac60 1584
657cbb6b
JR
1585 if (dev_data->domain != NULL &&
1586 dev_data->domain != domain)
84fe6c19 1587 goto out_unlock;
15898bbc
JR
1588
1589 /* Do real assignment */
7f760ddd
JR
1590 if (dev_data->alias != dev) {
1591 alias_data = get_dev_data(dev_data->alias);
1592 if (alias_data->domain == NULL)
1593 do_attach(dev_data->alias, domain);
24100055
JR
1594
1595 atomic_inc(&alias_data->bind);
657cbb6b 1596 }
15898bbc 1597
7f760ddd
JR
1598 if (dev_data->domain == NULL)
1599 do_attach(dev, domain);
eba6ac60 1600
24100055
JR
1601 atomic_inc(&dev_data->bind);
1602
84fe6c19
JL
1603 ret = 0;
1604
1605out_unlock:
1606
eba6ac60
JR
1607 /* ready */
1608 spin_unlock(&domain->lock);
15898bbc 1609
84fe6c19 1610 return ret;
0feae533 1611}
b20ac0d4 1612
407d733e
JR
1613/*
1614 * If a device is not yet associated with a domain, this function does
1615 * assigns it visible for the hardware
1616 */
15898bbc
JR
1617static int attach_device(struct device *dev,
1618 struct protection_domain *domain)
0feae533 1619{
fd7b5535 1620 struct pci_dev *pdev = to_pci_dev(dev);
eba6ac60 1621 unsigned long flags;
15898bbc 1622 int ret;
eba6ac60 1623
fd7b5535
JR
1624 if (amd_iommu_iotlb_sup)
1625 pci_enable_ats(pdev, PAGE_SHIFT);
1626
eba6ac60 1627 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
15898bbc 1628 ret = __attach_device(dev, domain);
b20ac0d4
JR
1629 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1630
0feae533
JR
1631 /*
1632 * We might boot into a crash-kernel here. The crashed kernel
1633 * left the caches in the IOMMU dirty. So we have to flush
1634 * here to evict all dirty stuff.
1635 */
17b124bf 1636 domain_flush_tlb_pde(domain);
15898bbc
JR
1637
1638 return ret;
b20ac0d4
JR
1639}
1640
355bf553
JR
1641/*
1642 * Removes a device from a protection domain (unlocked)
1643 */
15898bbc 1644static void __detach_device(struct device *dev)
355bf553 1645{
657cbb6b 1646 struct iommu_dev_data *dev_data = get_dev_data(dev);
24100055 1647 struct iommu_dev_data *alias_data;
2ca76279 1648 struct protection_domain *domain;
7c392cbe 1649 unsigned long flags;
c4596114 1650
7f760ddd 1651 BUG_ON(!dev_data->domain);
355bf553 1652
2ca76279
JR
1653 domain = dev_data->domain;
1654
1655 spin_lock_irqsave(&domain->lock, flags);
24100055 1656
7f760ddd 1657 if (dev_data->alias != dev) {
24100055 1658 alias_data = get_dev_data(dev_data->alias);
7f760ddd
JR
1659 if (atomic_dec_and_test(&alias_data->bind))
1660 do_detach(dev_data->alias);
24100055
JR
1661 }
1662
7f760ddd
JR
1663 if (atomic_dec_and_test(&dev_data->bind))
1664 do_detach(dev);
1665
2ca76279 1666 spin_unlock_irqrestore(&domain->lock, flags);
21129f78
JR
1667
1668 /*
1669 * If we run in passthrough mode the device must be assigned to the
d3ad9373
JR
1670 * passthrough domain if it is detached from any other domain.
1671 * Make sure we can deassign from the pt_domain itself.
21129f78 1672 */
d3ad9373
JR
1673 if (iommu_pass_through &&
1674 (dev_data->domain == NULL && domain != pt_domain))
15898bbc 1675 __attach_device(dev, pt_domain);
355bf553
JR
1676}
1677
1678/*
1679 * Removes a device from a protection domain (with devtable_lock held)
1680 */
15898bbc 1681static void detach_device(struct device *dev)
355bf553 1682{
fd7b5535 1683 struct pci_dev *pdev = to_pci_dev(dev);
355bf553
JR
1684 unsigned long flags;
1685
1686 /* lock device table */
1687 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
15898bbc 1688 __detach_device(dev);
355bf553 1689 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
fd7b5535
JR
1690
1691 if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev))
1692 pci_disable_ats(pdev);
355bf553 1693}
e275a2a0 1694
15898bbc
JR
1695/*
1696 * Find out the protection domain structure for a given PCI device. This
1697 * will give us the pointer to the page table root for example.
1698 */
1699static struct protection_domain *domain_for_device(struct device *dev)
1700{
1701 struct protection_domain *dom;
657cbb6b 1702 struct iommu_dev_data *dev_data, *alias_data;
15898bbc 1703 unsigned long flags;
6ec5ff4b 1704 u16 devid;
15898bbc 1705
657cbb6b 1706 devid = get_device_id(dev);
657cbb6b
JR
1707 dev_data = get_dev_data(dev);
1708 alias_data = get_dev_data(dev_data->alias);
1709 if (!alias_data)
1710 return NULL;
15898bbc
JR
1711
1712 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
657cbb6b 1713 dom = dev_data->domain;
15898bbc 1714 if (dom == NULL &&
657cbb6b
JR
1715 alias_data->domain != NULL) {
1716 __attach_device(dev, alias_data->domain);
1717 dom = alias_data->domain;
15898bbc
JR
1718 }
1719
1720 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1721
1722 return dom;
1723}
1724
e275a2a0
JR
1725static int device_change_notifier(struct notifier_block *nb,
1726 unsigned long action, void *data)
1727{
1728 struct device *dev = data;
98fc5a69 1729 u16 devid;
e275a2a0
JR
1730 struct protection_domain *domain;
1731 struct dma_ops_domain *dma_domain;
1732 struct amd_iommu *iommu;
1ac4cbbc 1733 unsigned long flags;
e275a2a0 1734
98fc5a69
JR
1735 if (!check_device(dev))
1736 return 0;
e275a2a0 1737
98fc5a69
JR
1738 devid = get_device_id(dev);
1739 iommu = amd_iommu_rlookup_table[devid];
e275a2a0
JR
1740
1741 switch (action) {
c1eee67b 1742 case BUS_NOTIFY_UNBOUND_DRIVER:
657cbb6b
JR
1743
1744 domain = domain_for_device(dev);
1745
e275a2a0
JR
1746 if (!domain)
1747 goto out;
a1ca331c
JR
1748 if (iommu_pass_through)
1749 break;
15898bbc 1750 detach_device(dev);
1ac4cbbc
JR
1751 break;
1752 case BUS_NOTIFY_ADD_DEVICE:
657cbb6b
JR
1753
1754 iommu_init_device(dev);
1755
1756 domain = domain_for_device(dev);
1757
1ac4cbbc
JR
1758 /* allocate a protection domain if a device is added */
1759 dma_domain = find_protection_domain(devid);
1760 if (dma_domain)
1761 goto out;
87a64d52 1762 dma_domain = dma_ops_domain_alloc();
1ac4cbbc
JR
1763 if (!dma_domain)
1764 goto out;
1765 dma_domain->target_dev = devid;
1766
1767 spin_lock_irqsave(&iommu_pd_list_lock, flags);
1768 list_add_tail(&dma_domain->list, &iommu_pd_list);
1769 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
1770
e275a2a0 1771 break;
657cbb6b
JR
1772 case BUS_NOTIFY_DEL_DEVICE:
1773
1774 iommu_uninit_device(dev);
1775
e275a2a0
JR
1776 default:
1777 goto out;
1778 }
1779
d8c13085 1780 device_flush_dte(dev);
e275a2a0
JR
1781 iommu_completion_wait(iommu);
1782
1783out:
1784 return 0;
1785}
1786
b25ae679 1787static struct notifier_block device_nb = {
e275a2a0
JR
1788 .notifier_call = device_change_notifier,
1789};
355bf553 1790
8638c491
JR
1791void amd_iommu_init_notifier(void)
1792{
1793 bus_register_notifier(&pci_bus_type, &device_nb);
1794}
1795
431b2a20
JR
1796/*****************************************************************************
1797 *
1798 * The next functions belong to the dma_ops mapping/unmapping code.
1799 *
1800 *****************************************************************************/
1801
1802/*
1803 * In the dma_ops path we only have the struct device. This function
1804 * finds the corresponding IOMMU, the protection domain and the
1805 * requestor id for a given device.
1806 * If the device is not yet associated with a domain this is also done
1807 * in this function.
1808 */
94f6d190 1809static struct protection_domain *get_domain(struct device *dev)
b20ac0d4 1810{
94f6d190 1811 struct protection_domain *domain;
b20ac0d4 1812 struct dma_ops_domain *dma_dom;
94f6d190 1813 u16 devid = get_device_id(dev);
b20ac0d4 1814
f99c0f1c 1815 if (!check_device(dev))
94f6d190 1816 return ERR_PTR(-EINVAL);
b20ac0d4 1817
94f6d190
JR
1818 domain = domain_for_device(dev);
1819 if (domain != NULL && !dma_ops_domain(domain))
1820 return ERR_PTR(-EBUSY);
f99c0f1c 1821
94f6d190
JR
1822 if (domain != NULL)
1823 return domain;
b20ac0d4 1824
15898bbc 1825 /* Device not bount yet - bind it */
94f6d190 1826 dma_dom = find_protection_domain(devid);
15898bbc 1827 if (!dma_dom)
94f6d190
JR
1828 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
1829 attach_device(dev, &dma_dom->domain);
15898bbc 1830 DUMP_printk("Using protection domain %d for device %s\n",
94f6d190 1831 dma_dom->domain.id, dev_name(dev));
f91ba190 1832
94f6d190 1833 return &dma_dom->domain;
b20ac0d4
JR
1834}
1835
04bfdd84
JR
1836static void update_device_table(struct protection_domain *domain)
1837{
492667da 1838 struct iommu_dev_data *dev_data;
04bfdd84 1839
492667da 1840 list_for_each_entry(dev_data, &domain->dev_list, list) {
fd7b5535 1841 struct pci_dev *pdev = to_pci_dev(dev_data->dev);
492667da 1842 u16 devid = get_device_id(dev_data->dev);
fd7b5535 1843 set_dte_entry(devid, domain, pci_ats_enabled(pdev));
04bfdd84
JR
1844 }
1845}
1846
1847static void update_domain(struct protection_domain *domain)
1848{
1849 if (!domain->updated)
1850 return;
1851
1852 update_device_table(domain);
17b124bf
JR
1853
1854 domain_flush_devices(domain);
1855 domain_flush_tlb_pde(domain);
04bfdd84
JR
1856
1857 domain->updated = false;
1858}
1859
8bda3092
JR
1860/*
1861 * This function fetches the PTE for a given address in the aperture
1862 */
1863static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
1864 unsigned long address)
1865{
384de729 1866 struct aperture_range *aperture;
8bda3092
JR
1867 u64 *pte, *pte_page;
1868
384de729
JR
1869 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1870 if (!aperture)
1871 return NULL;
1872
1873 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
8bda3092 1874 if (!pte) {
cbb9d729 1875 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
abdc5eb3 1876 GFP_ATOMIC);
384de729
JR
1877 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
1878 } else
8c8c143c 1879 pte += PM_LEVEL_INDEX(0, address);
8bda3092 1880
04bfdd84 1881 update_domain(&dom->domain);
8bda3092
JR
1882
1883 return pte;
1884}
1885
431b2a20
JR
1886/*
1887 * This is the generic map function. It maps one 4kb page at paddr to
1888 * the given address in the DMA address space for the domain.
1889 */
680525e0 1890static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
cb76c322
JR
1891 unsigned long address,
1892 phys_addr_t paddr,
1893 int direction)
1894{
1895 u64 *pte, __pte;
1896
1897 WARN_ON(address > dom->aperture_size);
1898
1899 paddr &= PAGE_MASK;
1900
8bda3092 1901 pte = dma_ops_get_pte(dom, address);
53812c11 1902 if (!pte)
8fd524b3 1903 return DMA_ERROR_CODE;
cb76c322
JR
1904
1905 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
1906
1907 if (direction == DMA_TO_DEVICE)
1908 __pte |= IOMMU_PTE_IR;
1909 else if (direction == DMA_FROM_DEVICE)
1910 __pte |= IOMMU_PTE_IW;
1911 else if (direction == DMA_BIDIRECTIONAL)
1912 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
1913
1914 WARN_ON(*pte);
1915
1916 *pte = __pte;
1917
1918 return (dma_addr_t)address;
1919}
1920
431b2a20
JR
1921/*
1922 * The generic unmapping function for on page in the DMA address space.
1923 */
680525e0 1924static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
cb76c322
JR
1925 unsigned long address)
1926{
384de729 1927 struct aperture_range *aperture;
cb76c322
JR
1928 u64 *pte;
1929
1930 if (address >= dom->aperture_size)
1931 return;
1932
384de729
JR
1933 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
1934 if (!aperture)
1935 return;
1936
1937 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
1938 if (!pte)
1939 return;
cb76c322 1940
8c8c143c 1941 pte += PM_LEVEL_INDEX(0, address);
cb76c322
JR
1942
1943 WARN_ON(!*pte);
1944
1945 *pte = 0ULL;
1946}
1947
431b2a20
JR
1948/*
1949 * This function contains common code for mapping of a physically
24f81160
JR
1950 * contiguous memory region into DMA address space. It is used by all
1951 * mapping functions provided with this IOMMU driver.
431b2a20
JR
1952 * Must be called with the domain lock held.
1953 */
cb76c322 1954static dma_addr_t __map_single(struct device *dev,
cb76c322
JR
1955 struct dma_ops_domain *dma_dom,
1956 phys_addr_t paddr,
1957 size_t size,
6d4f343f 1958 int dir,
832a90c3
JR
1959 bool align,
1960 u64 dma_mask)
cb76c322
JR
1961{
1962 dma_addr_t offset = paddr & ~PAGE_MASK;
53812c11 1963 dma_addr_t address, start, ret;
cb76c322 1964 unsigned int pages;
6d4f343f 1965 unsigned long align_mask = 0;
cb76c322
JR
1966 int i;
1967
e3c449f5 1968 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
cb76c322
JR
1969 paddr &= PAGE_MASK;
1970
8ecaf8f1
JR
1971 INC_STATS_COUNTER(total_map_requests);
1972
c1858976
JR
1973 if (pages > 1)
1974 INC_STATS_COUNTER(cross_page);
1975
6d4f343f
JR
1976 if (align)
1977 align_mask = (1UL << get_order(size)) - 1;
1978
11b83888 1979retry:
832a90c3
JR
1980 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
1981 dma_mask);
8fd524b3 1982 if (unlikely(address == DMA_ERROR_CODE)) {
11b83888
JR
1983 /*
1984 * setting next_address here will let the address
1985 * allocator only scan the new allocated range in the
1986 * first run. This is a small optimization.
1987 */
1988 dma_dom->next_address = dma_dom->aperture_size;
1989
576175c2 1990 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
11b83888
JR
1991 goto out;
1992
1993 /*
af901ca1 1994 * aperture was successfully enlarged by 128 MB, try
11b83888
JR
1995 * allocation again
1996 */
1997 goto retry;
1998 }
cb76c322
JR
1999
2000 start = address;
2001 for (i = 0; i < pages; ++i) {
680525e0 2002 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
8fd524b3 2003 if (ret == DMA_ERROR_CODE)
53812c11
JR
2004 goto out_unmap;
2005
cb76c322
JR
2006 paddr += PAGE_SIZE;
2007 start += PAGE_SIZE;
2008 }
2009 address += offset;
2010
5774f7c5
JR
2011 ADD_STATS_COUNTER(alloced_io_mem, size);
2012
afa9fdc2 2013 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
17b124bf 2014 domain_flush_tlb(&dma_dom->domain);
1c655773 2015 dma_dom->need_flush = false;
318afd41 2016 } else if (unlikely(amd_iommu_np_cache))
17b124bf 2017 domain_flush_pages(&dma_dom->domain, address, size);
270cab24 2018
cb76c322
JR
2019out:
2020 return address;
53812c11
JR
2021
2022out_unmap:
2023
2024 for (--i; i >= 0; --i) {
2025 start -= PAGE_SIZE;
680525e0 2026 dma_ops_domain_unmap(dma_dom, start);
53812c11
JR
2027 }
2028
2029 dma_ops_free_addresses(dma_dom, address, pages);
2030
8fd524b3 2031 return DMA_ERROR_CODE;
cb76c322
JR
2032}
2033
431b2a20
JR
2034/*
2035 * Does the reverse of the __map_single function. Must be called with
2036 * the domain lock held too
2037 */
cd8c82e8 2038static void __unmap_single(struct dma_ops_domain *dma_dom,
cb76c322
JR
2039 dma_addr_t dma_addr,
2040 size_t size,
2041 int dir)
2042{
04e0463e 2043 dma_addr_t flush_addr;
cb76c322
JR
2044 dma_addr_t i, start;
2045 unsigned int pages;
2046
8fd524b3 2047 if ((dma_addr == DMA_ERROR_CODE) ||
b8d9905d 2048 (dma_addr + size > dma_dom->aperture_size))
cb76c322
JR
2049 return;
2050
04e0463e 2051 flush_addr = dma_addr;
e3c449f5 2052 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
cb76c322
JR
2053 dma_addr &= PAGE_MASK;
2054 start = dma_addr;
2055
2056 for (i = 0; i < pages; ++i) {
680525e0 2057 dma_ops_domain_unmap(dma_dom, start);
cb76c322
JR
2058 start += PAGE_SIZE;
2059 }
2060
5774f7c5
JR
2061 SUB_STATS_COUNTER(alloced_io_mem, size);
2062
cb76c322 2063 dma_ops_free_addresses(dma_dom, dma_addr, pages);
270cab24 2064
80be308d 2065 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
17b124bf 2066 domain_flush_pages(&dma_dom->domain, flush_addr, size);
80be308d
JR
2067 dma_dom->need_flush = false;
2068 }
cb76c322
JR
2069}
2070
431b2a20
JR
2071/*
2072 * The exported map_single function for dma_ops.
2073 */
51491367
FT
2074static dma_addr_t map_page(struct device *dev, struct page *page,
2075 unsigned long offset, size_t size,
2076 enum dma_data_direction dir,
2077 struct dma_attrs *attrs)
4da70b9e
JR
2078{
2079 unsigned long flags;
4da70b9e 2080 struct protection_domain *domain;
4da70b9e 2081 dma_addr_t addr;
832a90c3 2082 u64 dma_mask;
51491367 2083 phys_addr_t paddr = page_to_phys(page) + offset;
4da70b9e 2084
0f2a86f2
JR
2085 INC_STATS_COUNTER(cnt_map_single);
2086
94f6d190
JR
2087 domain = get_domain(dev);
2088 if (PTR_ERR(domain) == -EINVAL)
4da70b9e 2089 return (dma_addr_t)paddr;
94f6d190
JR
2090 else if (IS_ERR(domain))
2091 return DMA_ERROR_CODE;
4da70b9e 2092
f99c0f1c
JR
2093 dma_mask = *dev->dma_mask;
2094
4da70b9e 2095 spin_lock_irqsave(&domain->lock, flags);
94f6d190 2096
cd8c82e8 2097 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
832a90c3 2098 dma_mask);
8fd524b3 2099 if (addr == DMA_ERROR_CODE)
4da70b9e
JR
2100 goto out;
2101
17b124bf 2102 domain_flush_complete(domain);
4da70b9e
JR
2103
2104out:
2105 spin_unlock_irqrestore(&domain->lock, flags);
2106
2107 return addr;
2108}
2109
431b2a20
JR
2110/*
2111 * The exported unmap_single function for dma_ops.
2112 */
51491367
FT
2113static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2114 enum dma_data_direction dir, struct dma_attrs *attrs)
4da70b9e
JR
2115{
2116 unsigned long flags;
4da70b9e 2117 struct protection_domain *domain;
4da70b9e 2118
146a6917
JR
2119 INC_STATS_COUNTER(cnt_unmap_single);
2120
94f6d190
JR
2121 domain = get_domain(dev);
2122 if (IS_ERR(domain))
5b28df6f
JR
2123 return;
2124
4da70b9e
JR
2125 spin_lock_irqsave(&domain->lock, flags);
2126
cd8c82e8 2127 __unmap_single(domain->priv, dma_addr, size, dir);
4da70b9e 2128
17b124bf 2129 domain_flush_complete(domain);
4da70b9e
JR
2130
2131 spin_unlock_irqrestore(&domain->lock, flags);
2132}
2133
431b2a20
JR
2134/*
2135 * This is a special map_sg function which is used if we should map a
2136 * device which is not handled by an AMD IOMMU in the system.
2137 */
65b050ad
JR
2138static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2139 int nelems, int dir)
2140{
2141 struct scatterlist *s;
2142 int i;
2143
2144 for_each_sg(sglist, s, nelems, i) {
2145 s->dma_address = (dma_addr_t)sg_phys(s);
2146 s->dma_length = s->length;
2147 }
2148
2149 return nelems;
2150}
2151
431b2a20
JR
2152/*
2153 * The exported map_sg function for dma_ops (handles scatter-gather
2154 * lists).
2155 */
65b050ad 2156static int map_sg(struct device *dev, struct scatterlist *sglist,
160c1d8e
FT
2157 int nelems, enum dma_data_direction dir,
2158 struct dma_attrs *attrs)
65b050ad
JR
2159{
2160 unsigned long flags;
65b050ad 2161 struct protection_domain *domain;
65b050ad
JR
2162 int i;
2163 struct scatterlist *s;
2164 phys_addr_t paddr;
2165 int mapped_elems = 0;
832a90c3 2166 u64 dma_mask;
65b050ad 2167
d03f067a
JR
2168 INC_STATS_COUNTER(cnt_map_sg);
2169
94f6d190
JR
2170 domain = get_domain(dev);
2171 if (PTR_ERR(domain) == -EINVAL)
f99c0f1c 2172 return map_sg_no_iommu(dev, sglist, nelems, dir);
94f6d190
JR
2173 else if (IS_ERR(domain))
2174 return 0;
dbcc112e 2175
832a90c3 2176 dma_mask = *dev->dma_mask;
65b050ad 2177
65b050ad
JR
2178 spin_lock_irqsave(&domain->lock, flags);
2179
2180 for_each_sg(sglist, s, nelems, i) {
2181 paddr = sg_phys(s);
2182
cd8c82e8 2183 s->dma_address = __map_single(dev, domain->priv,
832a90c3
JR
2184 paddr, s->length, dir, false,
2185 dma_mask);
65b050ad
JR
2186
2187 if (s->dma_address) {
2188 s->dma_length = s->length;
2189 mapped_elems++;
2190 } else
2191 goto unmap;
65b050ad
JR
2192 }
2193
17b124bf 2194 domain_flush_complete(domain);
65b050ad
JR
2195
2196out:
2197 spin_unlock_irqrestore(&domain->lock, flags);
2198
2199 return mapped_elems;
2200unmap:
2201 for_each_sg(sglist, s, mapped_elems, i) {
2202 if (s->dma_address)
cd8c82e8 2203 __unmap_single(domain->priv, s->dma_address,
65b050ad
JR
2204 s->dma_length, dir);
2205 s->dma_address = s->dma_length = 0;
2206 }
2207
2208 mapped_elems = 0;
2209
2210 goto out;
2211}
2212
431b2a20
JR
2213/*
2214 * The exported map_sg function for dma_ops (handles scatter-gather
2215 * lists).
2216 */
65b050ad 2217static void unmap_sg(struct device *dev, struct scatterlist *sglist,
160c1d8e
FT
2218 int nelems, enum dma_data_direction dir,
2219 struct dma_attrs *attrs)
65b050ad
JR
2220{
2221 unsigned long flags;
65b050ad
JR
2222 struct protection_domain *domain;
2223 struct scatterlist *s;
65b050ad
JR
2224 int i;
2225
55877a6b
JR
2226 INC_STATS_COUNTER(cnt_unmap_sg);
2227
94f6d190
JR
2228 domain = get_domain(dev);
2229 if (IS_ERR(domain))
5b28df6f
JR
2230 return;
2231
65b050ad
JR
2232 spin_lock_irqsave(&domain->lock, flags);
2233
2234 for_each_sg(sglist, s, nelems, i) {
cd8c82e8 2235 __unmap_single(domain->priv, s->dma_address,
65b050ad 2236 s->dma_length, dir);
65b050ad
JR
2237 s->dma_address = s->dma_length = 0;
2238 }
2239
17b124bf 2240 domain_flush_complete(domain);
65b050ad
JR
2241
2242 spin_unlock_irqrestore(&domain->lock, flags);
2243}
2244
431b2a20
JR
2245/*
2246 * The exported alloc_coherent function for dma_ops.
2247 */
5d8b53cf
JR
2248static void *alloc_coherent(struct device *dev, size_t size,
2249 dma_addr_t *dma_addr, gfp_t flag)
2250{
2251 unsigned long flags;
2252 void *virt_addr;
5d8b53cf 2253 struct protection_domain *domain;
5d8b53cf 2254 phys_addr_t paddr;
832a90c3 2255 u64 dma_mask = dev->coherent_dma_mask;
5d8b53cf 2256
c8f0fb36
JR
2257 INC_STATS_COUNTER(cnt_alloc_coherent);
2258
94f6d190
JR
2259 domain = get_domain(dev);
2260 if (PTR_ERR(domain) == -EINVAL) {
f99c0f1c
JR
2261 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2262 *dma_addr = __pa(virt_addr);
2263 return virt_addr;
94f6d190
JR
2264 } else if (IS_ERR(domain))
2265 return NULL;
5d8b53cf 2266
f99c0f1c
JR
2267 dma_mask = dev->coherent_dma_mask;
2268 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2269 flag |= __GFP_ZERO;
5d8b53cf
JR
2270
2271 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2272 if (!virt_addr)
b25ae679 2273 return NULL;
5d8b53cf 2274
5d8b53cf
JR
2275 paddr = virt_to_phys(virt_addr);
2276
832a90c3
JR
2277 if (!dma_mask)
2278 dma_mask = *dev->dma_mask;
2279
5d8b53cf
JR
2280 spin_lock_irqsave(&domain->lock, flags);
2281
cd8c82e8 2282 *dma_addr = __map_single(dev, domain->priv, paddr,
832a90c3 2283 size, DMA_BIDIRECTIONAL, true, dma_mask);
5d8b53cf 2284
8fd524b3 2285 if (*dma_addr == DMA_ERROR_CODE) {
367d04c4 2286 spin_unlock_irqrestore(&domain->lock, flags);
5b28df6f 2287 goto out_free;
367d04c4 2288 }
5d8b53cf 2289
17b124bf 2290 domain_flush_complete(domain);
5d8b53cf 2291
5d8b53cf
JR
2292 spin_unlock_irqrestore(&domain->lock, flags);
2293
2294 return virt_addr;
5b28df6f
JR
2295
2296out_free:
2297
2298 free_pages((unsigned long)virt_addr, get_order(size));
2299
2300 return NULL;
5d8b53cf
JR
2301}
2302
431b2a20
JR
2303/*
2304 * The exported free_coherent function for dma_ops.
431b2a20 2305 */
5d8b53cf
JR
2306static void free_coherent(struct device *dev, size_t size,
2307 void *virt_addr, dma_addr_t dma_addr)
2308{
2309 unsigned long flags;
5d8b53cf 2310 struct protection_domain *domain;
5d8b53cf 2311
5d31ee7e
JR
2312 INC_STATS_COUNTER(cnt_free_coherent);
2313
94f6d190
JR
2314 domain = get_domain(dev);
2315 if (IS_ERR(domain))
5b28df6f
JR
2316 goto free_mem;
2317
5d8b53cf
JR
2318 spin_lock_irqsave(&domain->lock, flags);
2319
cd8c82e8 2320 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
5d8b53cf 2321
17b124bf 2322 domain_flush_complete(domain);
5d8b53cf
JR
2323
2324 spin_unlock_irqrestore(&domain->lock, flags);
2325
2326free_mem:
2327 free_pages((unsigned long)virt_addr, get_order(size));
2328}
2329
b39ba6ad
JR
2330/*
2331 * This function is called by the DMA layer to find out if we can handle a
2332 * particular device. It is part of the dma_ops.
2333 */
2334static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2335{
420aef8a 2336 return check_device(dev);
b39ba6ad
JR
2337}
2338
c432f3df 2339/*
431b2a20
JR
2340 * The function for pre-allocating protection domains.
2341 *
c432f3df
JR
2342 * If the driver core informs the DMA layer if a driver grabs a device
2343 * we don't need to preallocate the protection domains anymore.
2344 * For now we have to.
2345 */
0e93dd88 2346static void prealloc_protection_domains(void)
c432f3df
JR
2347{
2348 struct pci_dev *dev = NULL;
2349 struct dma_ops_domain *dma_dom;
98fc5a69 2350 u16 devid;
c432f3df 2351
d18c69d3 2352 for_each_pci_dev(dev) {
98fc5a69
JR
2353
2354 /* Do we handle this device? */
2355 if (!check_device(&dev->dev))
c432f3df 2356 continue;
98fc5a69
JR
2357
2358 /* Is there already any domain for it? */
15898bbc 2359 if (domain_for_device(&dev->dev))
c432f3df 2360 continue;
98fc5a69
JR
2361
2362 devid = get_device_id(&dev->dev);
2363
87a64d52 2364 dma_dom = dma_ops_domain_alloc();
c432f3df
JR
2365 if (!dma_dom)
2366 continue;
2367 init_unity_mappings_for_device(dma_dom, devid);
bd60b735
JR
2368 dma_dom->target_dev = devid;
2369
15898bbc 2370 attach_device(&dev->dev, &dma_dom->domain);
be831297 2371
bd60b735 2372 list_add_tail(&dma_dom->list, &iommu_pd_list);
c432f3df
JR
2373 }
2374}
2375
160c1d8e 2376static struct dma_map_ops amd_iommu_dma_ops = {
6631ee9d
JR
2377 .alloc_coherent = alloc_coherent,
2378 .free_coherent = free_coherent,
51491367
FT
2379 .map_page = map_page,
2380 .unmap_page = unmap_page,
6631ee9d
JR
2381 .map_sg = map_sg,
2382 .unmap_sg = unmap_sg,
b39ba6ad 2383 .dma_supported = amd_iommu_dma_supported,
6631ee9d
JR
2384};
2385
431b2a20
JR
2386/*
2387 * The function which clues the AMD IOMMU driver into dma_ops.
2388 */
f5325094
JR
2389
2390void __init amd_iommu_init_api(void)
2391{
2392 register_iommu(&amd_iommu_ops);
2393}
2394
6631ee9d
JR
2395int __init amd_iommu_init_dma_ops(void)
2396{
2397 struct amd_iommu *iommu;
6631ee9d
JR
2398 int ret;
2399
431b2a20
JR
2400 /*
2401 * first allocate a default protection domain for every IOMMU we
2402 * found in the system. Devices not assigned to any other
2403 * protection domain will be assigned to the default one.
2404 */
3bd22172 2405 for_each_iommu(iommu) {
87a64d52 2406 iommu->default_dom = dma_ops_domain_alloc();
6631ee9d
JR
2407 if (iommu->default_dom == NULL)
2408 return -ENOMEM;
e2dc14a2 2409 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
6631ee9d
JR
2410 ret = iommu_init_unity_mappings(iommu);
2411 if (ret)
2412 goto free_domains;
2413 }
2414
431b2a20 2415 /*
8793abeb 2416 * Pre-allocate the protection domains for each device.
431b2a20 2417 */
8793abeb 2418 prealloc_protection_domains();
6631ee9d
JR
2419
2420 iommu_detected = 1;
75f1cdf1 2421 swiotlb = 0;
6631ee9d 2422
431b2a20 2423 /* Make the driver finally visible to the drivers */
6631ee9d
JR
2424 dma_ops = &amd_iommu_dma_ops;
2425
7f26508b
JR
2426 amd_iommu_stats_init();
2427
6631ee9d
JR
2428 return 0;
2429
2430free_domains:
2431
3bd22172 2432 for_each_iommu(iommu) {
6631ee9d
JR
2433 if (iommu->default_dom)
2434 dma_ops_domain_free(iommu->default_dom);
2435 }
2436
2437 return ret;
2438}
6d98cd80
JR
2439
2440/*****************************************************************************
2441 *
2442 * The following functions belong to the exported interface of AMD IOMMU
2443 *
2444 * This interface allows access to lower level functions of the IOMMU
2445 * like protection domain handling and assignement of devices to domains
2446 * which is not possible with the dma_ops interface.
2447 *
2448 *****************************************************************************/
2449
6d98cd80
JR
2450static void cleanup_domain(struct protection_domain *domain)
2451{
492667da 2452 struct iommu_dev_data *dev_data, *next;
6d98cd80 2453 unsigned long flags;
6d98cd80
JR
2454
2455 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2456
492667da
JR
2457 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
2458 struct device *dev = dev_data->dev;
2459
04e856c0 2460 __detach_device(dev);
492667da
JR
2461 atomic_set(&dev_data->bind, 0);
2462 }
6d98cd80
JR
2463
2464 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2465}
2466
2650815f
JR
2467static void protection_domain_free(struct protection_domain *domain)
2468{
2469 if (!domain)
2470 return;
2471
aeb26f55
JR
2472 del_domain_from_list(domain);
2473
2650815f
JR
2474 if (domain->id)
2475 domain_id_free(domain->id);
2476
2477 kfree(domain);
2478}
2479
2480static struct protection_domain *protection_domain_alloc(void)
c156e347
JR
2481{
2482 struct protection_domain *domain;
2483
2484 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2485 if (!domain)
2650815f 2486 return NULL;
c156e347
JR
2487
2488 spin_lock_init(&domain->lock);
5d214fe6 2489 mutex_init(&domain->api_lock);
c156e347
JR
2490 domain->id = domain_id_alloc();
2491 if (!domain->id)
2650815f 2492 goto out_err;
7c392cbe 2493 INIT_LIST_HEAD(&domain->dev_list);
2650815f 2494
aeb26f55
JR
2495 add_domain_to_list(domain);
2496
2650815f
JR
2497 return domain;
2498
2499out_err:
2500 kfree(domain);
2501
2502 return NULL;
2503}
2504
2505static int amd_iommu_domain_init(struct iommu_domain *dom)
2506{
2507 struct protection_domain *domain;
2508
2509 domain = protection_domain_alloc();
2510 if (!domain)
c156e347 2511 goto out_free;
2650815f
JR
2512
2513 domain->mode = PAGE_MODE_3_LEVEL;
c156e347
JR
2514 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2515 if (!domain->pt_root)
2516 goto out_free;
2517
2518 dom->priv = domain;
2519
2520 return 0;
2521
2522out_free:
2650815f 2523 protection_domain_free(domain);
c156e347
JR
2524
2525 return -ENOMEM;
2526}
2527
98383fc3
JR
2528static void amd_iommu_domain_destroy(struct iommu_domain *dom)
2529{
2530 struct protection_domain *domain = dom->priv;
2531
2532 if (!domain)
2533 return;
2534
2535 if (domain->dev_cnt > 0)
2536 cleanup_domain(domain);
2537
2538 BUG_ON(domain->dev_cnt != 0);
2539
2540 free_pagetable(domain);
2541
8b408fe4 2542 protection_domain_free(domain);
98383fc3
JR
2543
2544 dom->priv = NULL;
2545}
2546
684f2888
JR
2547static void amd_iommu_detach_device(struct iommu_domain *dom,
2548 struct device *dev)
2549{
657cbb6b 2550 struct iommu_dev_data *dev_data = dev->archdata.iommu;
684f2888 2551 struct amd_iommu *iommu;
684f2888
JR
2552 u16 devid;
2553
98fc5a69 2554 if (!check_device(dev))
684f2888
JR
2555 return;
2556
98fc5a69 2557 devid = get_device_id(dev);
684f2888 2558
657cbb6b 2559 if (dev_data->domain != NULL)
15898bbc 2560 detach_device(dev);
684f2888
JR
2561
2562 iommu = amd_iommu_rlookup_table[devid];
2563 if (!iommu)
2564 return;
2565
d8c13085 2566 device_flush_dte(dev);
684f2888
JR
2567 iommu_completion_wait(iommu);
2568}
2569
01106066
JR
2570static int amd_iommu_attach_device(struct iommu_domain *dom,
2571 struct device *dev)
2572{
2573 struct protection_domain *domain = dom->priv;
657cbb6b 2574 struct iommu_dev_data *dev_data;
01106066 2575 struct amd_iommu *iommu;
15898bbc 2576 int ret;
01106066
JR
2577 u16 devid;
2578
98fc5a69 2579 if (!check_device(dev))
01106066
JR
2580 return -EINVAL;
2581
657cbb6b
JR
2582 dev_data = dev->archdata.iommu;
2583
98fc5a69 2584 devid = get_device_id(dev);
01106066
JR
2585
2586 iommu = amd_iommu_rlookup_table[devid];
2587 if (!iommu)
2588 return -EINVAL;
2589
657cbb6b 2590 if (dev_data->domain)
15898bbc 2591 detach_device(dev);
01106066 2592
15898bbc 2593 ret = attach_device(dev, domain);
01106066
JR
2594
2595 iommu_completion_wait(iommu);
2596
15898bbc 2597 return ret;
01106066
JR
2598}
2599
468e2366
JR
2600static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2601 phys_addr_t paddr, int gfp_order, int iommu_prot)
c6229ca6 2602{
468e2366 2603 unsigned long page_size = 0x1000UL << gfp_order;
c6229ca6 2604 struct protection_domain *domain = dom->priv;
c6229ca6
JR
2605 int prot = 0;
2606 int ret;
2607
2608 if (iommu_prot & IOMMU_READ)
2609 prot |= IOMMU_PROT_IR;
2610 if (iommu_prot & IOMMU_WRITE)
2611 prot |= IOMMU_PROT_IW;
2612
5d214fe6 2613 mutex_lock(&domain->api_lock);
795e74f7 2614 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
5d214fe6
JR
2615 mutex_unlock(&domain->api_lock);
2616
795e74f7 2617 return ret;
c6229ca6
JR
2618}
2619
468e2366
JR
2620static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2621 int gfp_order)
eb74ff6c 2622{
eb74ff6c 2623 struct protection_domain *domain = dom->priv;
468e2366 2624 unsigned long page_size, unmap_size;
eb74ff6c 2625
468e2366 2626 page_size = 0x1000UL << gfp_order;
eb74ff6c 2627
5d214fe6 2628 mutex_lock(&domain->api_lock);
468e2366 2629 unmap_size = iommu_unmap_page(domain, iova, page_size);
795e74f7 2630 mutex_unlock(&domain->api_lock);
eb74ff6c 2631
17b124bf 2632 domain_flush_tlb_pde(domain);
5d214fe6 2633
468e2366 2634 return get_order(unmap_size);
eb74ff6c
JR
2635}
2636
645c4c8d
JR
2637static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2638 unsigned long iova)
2639{
2640 struct protection_domain *domain = dom->priv;
f03152bb 2641 unsigned long offset_mask;
645c4c8d 2642 phys_addr_t paddr;
f03152bb 2643 u64 *pte, __pte;
645c4c8d 2644
24cd7723 2645 pte = fetch_pte(domain, iova);
645c4c8d 2646
a6d41a40 2647 if (!pte || !IOMMU_PTE_PRESENT(*pte))
645c4c8d
JR
2648 return 0;
2649
f03152bb
JR
2650 if (PM_PTE_LEVEL(*pte) == 0)
2651 offset_mask = PAGE_SIZE - 1;
2652 else
2653 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
2654
2655 __pte = *pte & PM_ADDR_MASK;
2656 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
645c4c8d
JR
2657
2658 return paddr;
2659}
2660
dbb9fd86
SY
2661static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
2662 unsigned long cap)
2663{
80a506b8
JR
2664 switch (cap) {
2665 case IOMMU_CAP_CACHE_COHERENCY:
2666 return 1;
2667 }
2668
dbb9fd86
SY
2669 return 0;
2670}
2671
26961efe
JR
2672static struct iommu_ops amd_iommu_ops = {
2673 .domain_init = amd_iommu_domain_init,
2674 .domain_destroy = amd_iommu_domain_destroy,
2675 .attach_dev = amd_iommu_attach_device,
2676 .detach_dev = amd_iommu_detach_device,
468e2366
JR
2677 .map = amd_iommu_map,
2678 .unmap = amd_iommu_unmap,
26961efe 2679 .iova_to_phys = amd_iommu_iova_to_phys,
dbb9fd86 2680 .domain_has_cap = amd_iommu_domain_has_cap,
26961efe
JR
2681};
2682
0feae533
JR
2683/*****************************************************************************
2684 *
2685 * The next functions do a basic initialization of IOMMU for pass through
2686 * mode
2687 *
2688 * In passthrough mode the IOMMU is initialized and enabled but not used for
2689 * DMA-API translation.
2690 *
2691 *****************************************************************************/
2692
2693int __init amd_iommu_init_passthrough(void)
2694{
15898bbc 2695 struct amd_iommu *iommu;
0feae533 2696 struct pci_dev *dev = NULL;
15898bbc 2697 u16 devid;
0feae533 2698
af901ca1 2699 /* allocate passthrough domain */
0feae533
JR
2700 pt_domain = protection_domain_alloc();
2701 if (!pt_domain)
2702 return -ENOMEM;
2703
2704 pt_domain->mode |= PAGE_MODE_NONE;
2705
6c54aabd 2706 for_each_pci_dev(dev) {
98fc5a69 2707 if (!check_device(&dev->dev))
0feae533
JR
2708 continue;
2709
98fc5a69
JR
2710 devid = get_device_id(&dev->dev);
2711
15898bbc 2712 iommu = amd_iommu_rlookup_table[devid];
0feae533
JR
2713 if (!iommu)
2714 continue;
2715
15898bbc 2716 attach_device(&dev->dev, pt_domain);
0feae533
JR
2717 }
2718
2719 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
2720
2721 return 0;
2722}