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