iommu/amd: Split IOMMU group initialization
[linux-2.6-block.git] / drivers / iommu / 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
72e1dcc4 20#include <linux/ratelimit.h>
b6c02715 21#include <linux/pci.h>
cb41ed85 22#include <linux/pci-ats.h>
a66022c4 23#include <linux/bitmap.h>
5a0e3ad6 24#include <linux/slab.h>
7f26508b 25#include <linux/debugfs.h>
b6c02715 26#include <linux/scatterlist.h>
51491367 27#include <linux/dma-mapping.h>
b6c02715 28#include <linux/iommu-helper.h>
c156e347 29#include <linux/iommu.h>
815b33fd 30#include <linux/delay.h>
403f81d8 31#include <linux/amd-iommu.h>
72e1dcc4
JR
32#include <linux/notifier.h>
33#include <linux/export.h>
2b324506
JR
34#include <linux/irq.h>
35#include <linux/msi.h>
36#include <asm/irq_remapping.h>
37#include <asm/io_apic.h>
38#include <asm/apic.h>
39#include <asm/hw_irq.h>
17f5b569 40#include <asm/msidef.h>
b6c02715 41#include <asm/proto.h>
46a7fa27 42#include <asm/iommu.h>
1d9b16d1 43#include <asm/gart.h>
27c2127a 44#include <asm/dma.h>
403f81d8
JR
45
46#include "amd_iommu_proto.h"
47#include "amd_iommu_types.h"
6b474b82 48#include "irq_remapping.h"
b6c02715
JR
49
50#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
51
815b33fd 52#define LOOP_TIMEOUT 100000
136f78a1 53
aa3de9c0
OBC
54/*
55 * This bitmap is used to advertise the page sizes our hardware support
56 * to the IOMMU core, which will then use this information to split
57 * physically contiguous memory regions it is mapping into page sizes
58 * that we support.
59 *
60 * Traditionally the IOMMU core just handed us the mappings directly,
61 * after making sure the size is an order of a 4KiB page and that the
62 * mapping has natural alignment.
63 *
64 * To retain this behavior, we currently advertise that we support
65 * all page sizes that are an order of 4KiB.
66 *
67 * If at some point we'd like to utilize the IOMMU core's new behavior,
68 * we could change this to advertise the real page sizes we support.
69 */
70#define AMD_IOMMU_PGSIZES (~0xFFFUL)
71
b6c02715
JR
72static DEFINE_RWLOCK(amd_iommu_devtable_lock);
73
bd60b735
JR
74/* A list of preallocated protection domains */
75static LIST_HEAD(iommu_pd_list);
76static DEFINE_SPINLOCK(iommu_pd_list_lock);
77
8fa5f802
JR
78/* List of all available dev_data structures */
79static LIST_HEAD(dev_data_list);
80static DEFINE_SPINLOCK(dev_data_list_lock);
81
6efed63b
JR
82LIST_HEAD(ioapic_map);
83LIST_HEAD(hpet_map);
84
0feae533
JR
85/*
86 * Domain for untranslated devices - only allocated
87 * if iommu=pt passed on kernel cmd line.
88 */
89static struct protection_domain *pt_domain;
90
26961efe 91static struct iommu_ops amd_iommu_ops;
26961efe 92
72e1dcc4 93static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
52815b75 94int amd_iommu_max_glx_val = -1;
72e1dcc4 95
ac1534a5
JR
96static struct dma_map_ops amd_iommu_dma_ops;
97
431b2a20
JR
98/*
99 * general struct to manage commands send to an IOMMU
100 */
d6449536 101struct iommu_cmd {
b6c02715
JR
102 u32 data[4];
103};
104
05152a04
JR
105struct kmem_cache *amd_iommu_irq_cache;
106
04bfdd84 107static void update_domain(struct protection_domain *domain);
5abcdba4 108static int __init alloc_passthrough_domain(void);
c1eee67b 109
15898bbc
JR
110/****************************************************************************
111 *
112 * Helper functions
113 *
114 ****************************************************************************/
115
f62dda66 116static struct iommu_dev_data *alloc_dev_data(u16 devid)
8fa5f802
JR
117{
118 struct iommu_dev_data *dev_data;
119 unsigned long flags;
120
121 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
122 if (!dev_data)
123 return NULL;
124
f62dda66 125 dev_data->devid = devid;
8fa5f802
JR
126 atomic_set(&dev_data->bind, 0);
127
128 spin_lock_irqsave(&dev_data_list_lock, flags);
129 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
130 spin_unlock_irqrestore(&dev_data_list_lock, flags);
131
132 return dev_data;
133}
134
135static void free_dev_data(struct iommu_dev_data *dev_data)
136{
137 unsigned long flags;
138
139 spin_lock_irqsave(&dev_data_list_lock, flags);
140 list_del(&dev_data->dev_data_list);
141 spin_unlock_irqrestore(&dev_data_list_lock, flags);
142
143 kfree(dev_data);
144}
145
3b03bb74
JR
146static struct iommu_dev_data *search_dev_data(u16 devid)
147{
148 struct iommu_dev_data *dev_data;
149 unsigned long flags;
150
151 spin_lock_irqsave(&dev_data_list_lock, flags);
152 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
153 if (dev_data->devid == devid)
154 goto out_unlock;
155 }
156
157 dev_data = NULL;
158
159out_unlock:
160 spin_unlock_irqrestore(&dev_data_list_lock, flags);
161
162 return dev_data;
163}
164
165static struct iommu_dev_data *find_dev_data(u16 devid)
166{
167 struct iommu_dev_data *dev_data;
168
169 dev_data = search_dev_data(devid);
170
171 if (dev_data == NULL)
172 dev_data = alloc_dev_data(devid);
173
174 return dev_data;
175}
176
15898bbc
JR
177static inline u16 get_device_id(struct device *dev)
178{
179 struct pci_dev *pdev = to_pci_dev(dev);
180
181 return calc_devid(pdev->bus->number, pdev->devfn);
182}
183
657cbb6b
JR
184static struct iommu_dev_data *get_dev_data(struct device *dev)
185{
186 return dev->archdata.iommu;
187}
188
5abcdba4
JR
189static bool pci_iommuv2_capable(struct pci_dev *pdev)
190{
191 static const int caps[] = {
192 PCI_EXT_CAP_ID_ATS,
46277b75
JR
193 PCI_EXT_CAP_ID_PRI,
194 PCI_EXT_CAP_ID_PASID,
5abcdba4
JR
195 };
196 int i, pos;
197
198 for (i = 0; i < 3; ++i) {
199 pos = pci_find_ext_capability(pdev, caps[i]);
200 if (pos == 0)
201 return false;
202 }
203
204 return true;
205}
206
6a113ddc
JR
207static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
208{
209 struct iommu_dev_data *dev_data;
210
211 dev_data = get_dev_data(&pdev->dev);
212
213 return dev_data->errata & (1 << erratum) ? true : false;
214}
215
71c70984
JR
216/*
217 * In this function the list of preallocated protection domains is traversed to
218 * find the domain for a specific device
219 */
220static struct dma_ops_domain *find_protection_domain(u16 devid)
221{
222 struct dma_ops_domain *entry, *ret = NULL;
223 unsigned long flags;
224 u16 alias = amd_iommu_alias_table[devid];
225
226 if (list_empty(&iommu_pd_list))
227 return NULL;
228
229 spin_lock_irqsave(&iommu_pd_list_lock, flags);
230
231 list_for_each_entry(entry, &iommu_pd_list, list) {
232 if (entry->target_dev == devid ||
233 entry->target_dev == alias) {
234 ret = entry;
235 break;
236 }
237 }
238
239 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
240
241 return ret;
242}
243
98fc5a69
JR
244/*
245 * This function checks if the driver got a valid device from the caller to
246 * avoid dereferencing invalid pointers.
247 */
248static bool check_device(struct device *dev)
249{
250 u16 devid;
251
252 if (!dev || !dev->dma_mask)
253 return false;
254
255 /* No device or no PCI device */
339d3261 256 if (dev->bus != &pci_bus_type)
98fc5a69
JR
257 return false;
258
259 devid = get_device_id(dev);
260
261 /* Out of our scope? */
262 if (devid > amd_iommu_last_bdf)
263 return false;
264
265 if (amd_iommu_rlookup_table[devid] == NULL)
266 return false;
267
268 return true;
269}
270
664b6003
AW
271static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
272{
273 pci_dev_put(*from);
274 *from = to;
275}
276
277#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
278
eb9c9527 279static int init_iommu_group(struct device *dev)
657cbb6b
JR
280{
281 struct iommu_dev_data *dev_data;
9dcd6130 282 struct iommu_group *group;
eb9c9527 283 struct pci_dev *dma_pdev = NULL;
9dcd6130 284 int ret;
657cbb6b 285
eb9c9527
AW
286 group = iommu_group_get(dev);
287 if (group) {
288 iommu_group_put(group);
657cbb6b 289 return 0;
eb9c9527 290 }
657cbb6b 291
3b03bb74 292 dev_data = find_dev_data(get_device_id(dev));
657cbb6b
JR
293 if (!dev_data)
294 return -ENOMEM;
295
eb9c9527
AW
296 if (dev_data->alias_data) {
297 u16 alias;
9dcd6130 298
eb9c9527 299 alias = amd_iommu_alias_table[dev_data->devid];
9dcd6130 300 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
0774e392
JR
301 }
302
eb9c9527
AW
303 if (!dma_pdev)
304 dma_pdev = pci_dev_get(to_pci_dev(dev));
9dcd6130 305
31fe9435 306 /* Account for quirked devices */
664b6003
AW
307 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
308
31fe9435
AW
309 /*
310 * If it's a multifunction device that does not support our
311 * required ACS flags, add to the same group as function 0.
312 */
664b6003
AW
313 if (dma_pdev->multifunction &&
314 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
315 swap_pci_ref(&dma_pdev,
316 pci_get_slot(dma_pdev->bus,
317 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
318 0)));
319
31fe9435
AW
320 /*
321 * Devices on the root bus go through the iommu. If that's not us,
322 * find the next upstream device and test ACS up to the root bus.
323 * Finding the next device may require skipping virtual buses.
324 */
664b6003 325 while (!pci_is_root_bus(dma_pdev->bus)) {
31fe9435
AW
326 struct pci_bus *bus = dma_pdev->bus;
327
328 while (!bus->self) {
329 if (!pci_is_root_bus(bus))
330 bus = bus->parent;
331 else
332 goto root_bus;
333 }
334
335 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
664b6003
AW
336 break;
337
31fe9435 338 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
664b6003
AW
339 }
340
31fe9435 341root_bus:
9dcd6130
AW
342 group = iommu_group_get(&dma_pdev->dev);
343 pci_dev_put(dma_pdev);
344 if (!group) {
345 group = iommu_group_alloc();
346 if (IS_ERR(group))
347 return PTR_ERR(group);
26018874 348 }
657cbb6b 349
9dcd6130
AW
350 ret = iommu_group_add_device(group, dev);
351
352 iommu_group_put(group);
353
eb9c9527
AW
354 return ret;
355}
356
357static int iommu_init_device(struct device *dev)
358{
359 struct pci_dev *pdev = to_pci_dev(dev);
360 struct iommu_dev_data *dev_data;
361 u16 alias;
362 int ret;
363
364 if (dev->archdata.iommu)
365 return 0;
366
367 dev_data = find_dev_data(get_device_id(dev));
368 if (!dev_data)
369 return -ENOMEM;
370
371 alias = amd_iommu_alias_table[dev_data->devid];
372 if (alias != dev_data->devid) {
373 struct iommu_dev_data *alias_data;
374
375 alias_data = find_dev_data(alias);
376 if (alias_data == NULL) {
377 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
378 dev_name(dev));
379 free_dev_data(dev_data);
380 return -ENOTSUPP;
381 }
382 dev_data->alias_data = alias_data;
383 }
384
385 ret = init_iommu_group(dev);
9dcd6130
AW
386 if (ret)
387 return ret;
388
5abcdba4
JR
389 if (pci_iommuv2_capable(pdev)) {
390 struct amd_iommu *iommu;
391
392 iommu = amd_iommu_rlookup_table[dev_data->devid];
393 dev_data->iommu_v2 = iommu->is_iommu_v2;
394 }
395
657cbb6b
JR
396 dev->archdata.iommu = dev_data;
397
657cbb6b
JR
398 return 0;
399}
400
26018874
JR
401static void iommu_ignore_device(struct device *dev)
402{
403 u16 devid, alias;
404
405 devid = get_device_id(dev);
406 alias = amd_iommu_alias_table[devid];
407
408 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
409 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
410
411 amd_iommu_rlookup_table[devid] = NULL;
412 amd_iommu_rlookup_table[alias] = NULL;
413}
414
657cbb6b
JR
415static void iommu_uninit_device(struct device *dev)
416{
9dcd6130
AW
417 iommu_group_remove_device(dev);
418
8fa5f802
JR
419 /*
420 * Nothing to do here - we keep dev_data around for unplugged devices
421 * and reuse it when the device is re-plugged - not doing so would
422 * introduce a ton of races.
423 */
657cbb6b 424}
b7cc9554
JR
425
426void __init amd_iommu_uninit_devices(void)
427{
8fa5f802 428 struct iommu_dev_data *dev_data, *n;
b7cc9554
JR
429 struct pci_dev *pdev = NULL;
430
431 for_each_pci_dev(pdev) {
432
433 if (!check_device(&pdev->dev))
434 continue;
435
436 iommu_uninit_device(&pdev->dev);
437 }
8fa5f802
JR
438
439 /* Free all of our dev_data structures */
440 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
441 free_dev_data(dev_data);
b7cc9554
JR
442}
443
444int __init amd_iommu_init_devices(void)
445{
446 struct pci_dev *pdev = NULL;
447 int ret = 0;
448
449 for_each_pci_dev(pdev) {
450
451 if (!check_device(&pdev->dev))
452 continue;
453
454 ret = iommu_init_device(&pdev->dev);
26018874
JR
455 if (ret == -ENOTSUPP)
456 iommu_ignore_device(&pdev->dev);
457 else if (ret)
b7cc9554
JR
458 goto out_free;
459 }
460
461 return 0;
462
463out_free:
464
465 amd_iommu_uninit_devices();
466
467 return ret;
468}
7f26508b
JR
469#ifdef CONFIG_AMD_IOMMU_STATS
470
471/*
472 * Initialization code for statistics collection
473 */
474
da49f6df 475DECLARE_STATS_COUNTER(compl_wait);
0f2a86f2 476DECLARE_STATS_COUNTER(cnt_map_single);
146a6917 477DECLARE_STATS_COUNTER(cnt_unmap_single);
d03f067a 478DECLARE_STATS_COUNTER(cnt_map_sg);
55877a6b 479DECLARE_STATS_COUNTER(cnt_unmap_sg);
c8f0fb36 480DECLARE_STATS_COUNTER(cnt_alloc_coherent);
5d31ee7e 481DECLARE_STATS_COUNTER(cnt_free_coherent);
c1858976 482DECLARE_STATS_COUNTER(cross_page);
f57d98ae 483DECLARE_STATS_COUNTER(domain_flush_single);
18811f55 484DECLARE_STATS_COUNTER(domain_flush_all);
5774f7c5 485DECLARE_STATS_COUNTER(alloced_io_mem);
8ecaf8f1 486DECLARE_STATS_COUNTER(total_map_requests);
399be2f5
JR
487DECLARE_STATS_COUNTER(complete_ppr);
488DECLARE_STATS_COUNTER(invalidate_iotlb);
489DECLARE_STATS_COUNTER(invalidate_iotlb_all);
490DECLARE_STATS_COUNTER(pri_requests);
491
7f26508b 492static struct dentry *stats_dir;
7f26508b
JR
493static struct dentry *de_fflush;
494
495static void amd_iommu_stats_add(struct __iommu_counter *cnt)
496{
497 if (stats_dir == NULL)
498 return;
499
500 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
501 &cnt->value);
502}
503
504static void amd_iommu_stats_init(void)
505{
506 stats_dir = debugfs_create_dir("amd-iommu", NULL);
507 if (stats_dir == NULL)
508 return;
509
7f26508b 510 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
3775d481 511 &amd_iommu_unmap_flush);
da49f6df
JR
512
513 amd_iommu_stats_add(&compl_wait);
0f2a86f2 514 amd_iommu_stats_add(&cnt_map_single);
146a6917 515 amd_iommu_stats_add(&cnt_unmap_single);
d03f067a 516 amd_iommu_stats_add(&cnt_map_sg);
55877a6b 517 amd_iommu_stats_add(&cnt_unmap_sg);
c8f0fb36 518 amd_iommu_stats_add(&cnt_alloc_coherent);
5d31ee7e 519 amd_iommu_stats_add(&cnt_free_coherent);
c1858976 520 amd_iommu_stats_add(&cross_page);
f57d98ae 521 amd_iommu_stats_add(&domain_flush_single);
18811f55 522 amd_iommu_stats_add(&domain_flush_all);
5774f7c5 523 amd_iommu_stats_add(&alloced_io_mem);
8ecaf8f1 524 amd_iommu_stats_add(&total_map_requests);
399be2f5
JR
525 amd_iommu_stats_add(&complete_ppr);
526 amd_iommu_stats_add(&invalidate_iotlb);
527 amd_iommu_stats_add(&invalidate_iotlb_all);
528 amd_iommu_stats_add(&pri_requests);
7f26508b
JR
529}
530
531#endif
532
a80dc3e0
JR
533/****************************************************************************
534 *
535 * Interrupt handling functions
536 *
537 ****************************************************************************/
538
e3e59876
JR
539static void dump_dte_entry(u16 devid)
540{
541 int i;
542
ee6c2868
JR
543 for (i = 0; i < 4; ++i)
544 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
e3e59876
JR
545 amd_iommu_dev_table[devid].data[i]);
546}
547
945b4ac4
JR
548static void dump_command(unsigned long phys_addr)
549{
550 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
551 int i;
552
553 for (i = 0; i < 4; ++i)
554 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
555}
556
a345b23b 557static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
90008ee4 558{
3d06fca8
JR
559 int type, devid, domid, flags;
560 volatile u32 *event = __evt;
561 int count = 0;
562 u64 address;
563
564retry:
565 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
566 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
567 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
568 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
569 address = (u64)(((u64)event[3]) << 32) | event[2];
570
571 if (type == 0) {
572 /* Did we hit the erratum? */
573 if (++count == LOOP_TIMEOUT) {
574 pr_err("AMD-Vi: No event written to event log\n");
575 return;
576 }
577 udelay(1);
578 goto retry;
579 }
90008ee4 580
4c6f40d4 581 printk(KERN_ERR "AMD-Vi: Event logged [");
90008ee4
JR
582
583 switch (type) {
584 case EVENT_TYPE_ILL_DEV:
585 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
586 "address=0x%016llx flags=0x%04x]\n",
587 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
588 address, flags);
e3e59876 589 dump_dte_entry(devid);
90008ee4
JR
590 break;
591 case EVENT_TYPE_IO_FAULT:
592 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
593 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
594 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
595 domid, address, flags);
596 break;
597 case EVENT_TYPE_DEV_TAB_ERR:
598 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
599 "address=0x%016llx flags=0x%04x]\n",
600 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
601 address, flags);
602 break;
603 case EVENT_TYPE_PAGE_TAB_ERR:
604 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
605 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
606 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
607 domid, address, flags);
608 break;
609 case EVENT_TYPE_ILL_CMD:
610 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
945b4ac4 611 dump_command(address);
90008ee4
JR
612 break;
613 case EVENT_TYPE_CMD_HARD_ERR:
614 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
615 "flags=0x%04x]\n", address, flags);
616 break;
617 case EVENT_TYPE_IOTLB_INV_TO:
618 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
619 "address=0x%016llx]\n",
620 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
621 address);
622 break;
623 case EVENT_TYPE_INV_DEV_REQ:
624 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
625 "address=0x%016llx flags=0x%04x]\n",
626 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
627 address, flags);
628 break;
629 default:
630 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
631 }
3d06fca8
JR
632
633 memset(__evt, 0, 4 * sizeof(u32));
90008ee4
JR
634}
635
636static void iommu_poll_events(struct amd_iommu *iommu)
637{
638 u32 head, tail;
639 unsigned long flags;
640
641 spin_lock_irqsave(&iommu->lock, flags);
642
643 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
644 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
645
646 while (head != tail) {
a345b23b 647 iommu_print_event(iommu, iommu->evt_buf + head);
90008ee4
JR
648 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
649 }
650
651 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
652
653 spin_unlock_irqrestore(&iommu->lock, flags);
654}
655
eee53537 656static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
72e1dcc4
JR
657{
658 struct amd_iommu_fault fault;
72e1dcc4 659
399be2f5
JR
660 INC_STATS_COUNTER(pri_requests);
661
72e1dcc4
JR
662 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
663 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
664 return;
665 }
666
667 fault.address = raw[1];
668 fault.pasid = PPR_PASID(raw[0]);
669 fault.device_id = PPR_DEVID(raw[0]);
670 fault.tag = PPR_TAG(raw[0]);
671 fault.flags = PPR_FLAGS(raw[0]);
672
72e1dcc4
JR
673 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
674}
675
676static void iommu_poll_ppr_log(struct amd_iommu *iommu)
677{
678 unsigned long flags;
679 u32 head, tail;
680
681 if (iommu->ppr_log == NULL)
682 return;
683
eee53537
JR
684 /* enable ppr interrupts again */
685 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
686
72e1dcc4
JR
687 spin_lock_irqsave(&iommu->lock, flags);
688
689 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
690 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
691
692 while (head != tail) {
eee53537
JR
693 volatile u64 *raw;
694 u64 entry[2];
695 int i;
696
697 raw = (u64 *)(iommu->ppr_log + head);
698
699 /*
700 * Hardware bug: Interrupt may arrive before the entry is
701 * written to memory. If this happens we need to wait for the
702 * entry to arrive.
703 */
704 for (i = 0; i < LOOP_TIMEOUT; ++i) {
705 if (PPR_REQ_TYPE(raw[0]) != 0)
706 break;
707 udelay(1);
708 }
72e1dcc4 709
eee53537
JR
710 /* Avoid memcpy function-call overhead */
711 entry[0] = raw[0];
712 entry[1] = raw[1];
72e1dcc4 713
eee53537
JR
714 /*
715 * To detect the hardware bug we need to clear the entry
716 * back to zero.
717 */
718 raw[0] = raw[1] = 0UL;
719
720 /* Update head pointer of hardware ring-buffer */
72e1dcc4
JR
721 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
722 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
eee53537
JR
723
724 /*
725 * Release iommu->lock because ppr-handling might need to
df805abb 726 * re-acquire it
eee53537
JR
727 */
728 spin_unlock_irqrestore(&iommu->lock, flags);
729
730 /* Handle PPR entry */
731 iommu_handle_ppr_entry(iommu, entry);
732
733 spin_lock_irqsave(&iommu->lock, flags);
734
735 /* Refresh ring-buffer information */
736 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
72e1dcc4
JR
737 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
738 }
739
72e1dcc4
JR
740 spin_unlock_irqrestore(&iommu->lock, flags);
741}
742
72fe00f0 743irqreturn_t amd_iommu_int_thread(int irq, void *data)
a80dc3e0 744{
90008ee4
JR
745 struct amd_iommu *iommu;
746
72e1dcc4 747 for_each_iommu(iommu) {
90008ee4 748 iommu_poll_events(iommu);
72e1dcc4
JR
749 iommu_poll_ppr_log(iommu);
750 }
90008ee4
JR
751
752 return IRQ_HANDLED;
a80dc3e0
JR
753}
754
72fe00f0
JR
755irqreturn_t amd_iommu_int_handler(int irq, void *data)
756{
757 return IRQ_WAKE_THREAD;
758}
759
431b2a20
JR
760/****************************************************************************
761 *
762 * IOMMU command queuing functions
763 *
764 ****************************************************************************/
765
ac0ea6e9
JR
766static int wait_on_sem(volatile u64 *sem)
767{
768 int i = 0;
769
770 while (*sem == 0 && i < LOOP_TIMEOUT) {
771 udelay(1);
772 i += 1;
773 }
774
775 if (i == LOOP_TIMEOUT) {
776 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
777 return -EIO;
778 }
779
780 return 0;
781}
782
783static void copy_cmd_to_buffer(struct amd_iommu *iommu,
784 struct iommu_cmd *cmd,
785 u32 tail)
a19ae1ec 786{
a19ae1ec
JR
787 u8 *target;
788
8a7c5ef3 789 target = iommu->cmd_buf + tail;
ac0ea6e9
JR
790 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
791
792 /* Copy command to buffer */
793 memcpy(target, cmd, sizeof(*cmd));
794
795 /* Tell the IOMMU about it */
a19ae1ec 796 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
ac0ea6e9 797}
a19ae1ec 798
815b33fd 799static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
ded46737 800{
815b33fd
JR
801 WARN_ON(address & 0x7ULL);
802
ded46737 803 memset(cmd, 0, sizeof(*cmd));
815b33fd
JR
804 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
805 cmd->data[1] = upper_32_bits(__pa(address));
806 cmd->data[2] = 1;
ded46737
JR
807 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
808}
809
94fe79e2
JR
810static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
811{
812 memset(cmd, 0, sizeof(*cmd));
813 cmd->data[0] = devid;
814 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
815}
816
11b6402c
JR
817static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
818 size_t size, u16 domid, int pde)
819{
820 u64 pages;
821 int s;
822
823 pages = iommu_num_pages(address, size, PAGE_SIZE);
824 s = 0;
825
826 if (pages > 1) {
827 /*
828 * If we have to flush more than one page, flush all
829 * TLB entries for this domain
830 */
831 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
832 s = 1;
833 }
834
835 address &= PAGE_MASK;
836
837 memset(cmd, 0, sizeof(*cmd));
838 cmd->data[1] |= domid;
839 cmd->data[2] = lower_32_bits(address);
840 cmd->data[3] = upper_32_bits(address);
841 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
842 if (s) /* size bit - we flush more than one 4kb page */
843 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
df805abb 844 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
11b6402c
JR
845 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
846}
847
cb41ed85
JR
848static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
849 u64 address, size_t size)
850{
851 u64 pages;
852 int s;
853
854 pages = iommu_num_pages(address, size, PAGE_SIZE);
855 s = 0;
856
857 if (pages > 1) {
858 /*
859 * If we have to flush more than one page, flush all
860 * TLB entries for this domain
861 */
862 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
863 s = 1;
864 }
865
866 address &= PAGE_MASK;
867
868 memset(cmd, 0, sizeof(*cmd));
869 cmd->data[0] = devid;
870 cmd->data[0] |= (qdep & 0xff) << 24;
871 cmd->data[1] = devid;
872 cmd->data[2] = lower_32_bits(address);
873 cmd->data[3] = upper_32_bits(address);
874 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
875 if (s)
876 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
877}
878
22e266c7
JR
879static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
880 u64 address, bool size)
881{
882 memset(cmd, 0, sizeof(*cmd));
883
884 address &= ~(0xfffULL);
885
886 cmd->data[0] = pasid & PASID_MASK;
887 cmd->data[1] = domid;
888 cmd->data[2] = lower_32_bits(address);
889 cmd->data[3] = upper_32_bits(address);
890 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
891 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
892 if (size)
893 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
894 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
895}
896
897static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
898 int qdep, u64 address, bool size)
899{
900 memset(cmd, 0, sizeof(*cmd));
901
902 address &= ~(0xfffULL);
903
904 cmd->data[0] = devid;
905 cmd->data[0] |= (pasid & 0xff) << 16;
906 cmd->data[0] |= (qdep & 0xff) << 24;
907 cmd->data[1] = devid;
908 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
909 cmd->data[2] = lower_32_bits(address);
910 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
911 cmd->data[3] = upper_32_bits(address);
912 if (size)
913 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
914 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
915}
916
c99afa25
JR
917static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
918 int status, int tag, bool gn)
919{
920 memset(cmd, 0, sizeof(*cmd));
921
922 cmd->data[0] = devid;
923 if (gn) {
924 cmd->data[1] = pasid & PASID_MASK;
925 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
926 }
927 cmd->data[3] = tag & 0x1ff;
928 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
929
930 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
931}
932
58fc7f14
JR
933static void build_inv_all(struct iommu_cmd *cmd)
934{
935 memset(cmd, 0, sizeof(*cmd));
936 CMD_SET_TYPE(cmd, CMD_INV_ALL);
a19ae1ec
JR
937}
938
7ef2798d
JR
939static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
940{
941 memset(cmd, 0, sizeof(*cmd));
942 cmd->data[0] = devid;
943 CMD_SET_TYPE(cmd, CMD_INV_IRT);
944}
945
431b2a20 946/*
431b2a20 947 * Writes the command to the IOMMUs command buffer and informs the
ac0ea6e9 948 * hardware about the new command.
431b2a20 949 */
f1ca1512
JR
950static int iommu_queue_command_sync(struct amd_iommu *iommu,
951 struct iommu_cmd *cmd,
952 bool sync)
a19ae1ec 953{
ac0ea6e9 954 u32 left, tail, head, next_tail;
a19ae1ec 955 unsigned long flags;
a19ae1ec 956
549c90dc 957 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
ac0ea6e9
JR
958
959again:
a19ae1ec 960 spin_lock_irqsave(&iommu->lock, flags);
a19ae1ec 961
ac0ea6e9
JR
962 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
963 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
964 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
965 left = (head - next_tail) % iommu->cmd_buf_size;
a19ae1ec 966
ac0ea6e9
JR
967 if (left <= 2) {
968 struct iommu_cmd sync_cmd;
969 volatile u64 sem = 0;
970 int ret;
8d201968 971
ac0ea6e9
JR
972 build_completion_wait(&sync_cmd, (u64)&sem);
973 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
da49f6df 974
ac0ea6e9
JR
975 spin_unlock_irqrestore(&iommu->lock, flags);
976
977 if ((ret = wait_on_sem(&sem)) != 0)
978 return ret;
979
980 goto again;
8d201968
JR
981 }
982
ac0ea6e9
JR
983 copy_cmd_to_buffer(iommu, cmd, tail);
984
985 /* We need to sync now to make sure all commands are processed */
f1ca1512 986 iommu->need_sync = sync;
ac0ea6e9 987
a19ae1ec 988 spin_unlock_irqrestore(&iommu->lock, flags);
8d201968 989
815b33fd 990 return 0;
8d201968
JR
991}
992
f1ca1512
JR
993static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
994{
995 return iommu_queue_command_sync(iommu, cmd, true);
996}
997
8d201968
JR
998/*
999 * This function queues a completion wait command into the command
1000 * buffer of an IOMMU
1001 */
a19ae1ec 1002static int iommu_completion_wait(struct amd_iommu *iommu)
8d201968
JR
1003{
1004 struct iommu_cmd cmd;
815b33fd 1005 volatile u64 sem = 0;
ac0ea6e9 1006 int ret;
8d201968 1007
09ee17eb 1008 if (!iommu->need_sync)
815b33fd 1009 return 0;
09ee17eb 1010
815b33fd 1011 build_completion_wait(&cmd, (u64)&sem);
a19ae1ec 1012
f1ca1512 1013 ret = iommu_queue_command_sync(iommu, &cmd, false);
a19ae1ec 1014 if (ret)
815b33fd 1015 return ret;
8d201968 1016
ac0ea6e9 1017 return wait_on_sem(&sem);
8d201968
JR
1018}
1019
d8c13085 1020static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
a19ae1ec 1021{
d8c13085 1022 struct iommu_cmd cmd;
a19ae1ec 1023
d8c13085 1024 build_inv_dte(&cmd, devid);
7e4f88da 1025
d8c13085
JR
1026 return iommu_queue_command(iommu, &cmd);
1027}
09ee17eb 1028
7d0c5cc5
JR
1029static void iommu_flush_dte_all(struct amd_iommu *iommu)
1030{
1031 u32 devid;
09ee17eb 1032
7d0c5cc5
JR
1033 for (devid = 0; devid <= 0xffff; ++devid)
1034 iommu_flush_dte(iommu, devid);
a19ae1ec 1035
7d0c5cc5
JR
1036 iommu_completion_wait(iommu);
1037}
84df8175 1038
7d0c5cc5
JR
1039/*
1040 * This function uses heavy locking and may disable irqs for some time. But
1041 * this is no issue because it is only called during resume.
1042 */
1043static void iommu_flush_tlb_all(struct amd_iommu *iommu)
1044{
1045 u32 dom_id;
a19ae1ec 1046
7d0c5cc5
JR
1047 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1048 struct iommu_cmd cmd;
1049 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1050 dom_id, 1);
1051 iommu_queue_command(iommu, &cmd);
1052 }
8eed9833 1053
7d0c5cc5 1054 iommu_completion_wait(iommu);
a19ae1ec
JR
1055}
1056
58fc7f14 1057static void iommu_flush_all(struct amd_iommu *iommu)
0518a3a4 1058{
58fc7f14 1059 struct iommu_cmd cmd;
0518a3a4 1060
58fc7f14 1061 build_inv_all(&cmd);
0518a3a4 1062
58fc7f14
JR
1063 iommu_queue_command(iommu, &cmd);
1064 iommu_completion_wait(iommu);
1065}
1066
7ef2798d
JR
1067static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1068{
1069 struct iommu_cmd cmd;
1070
1071 build_inv_irt(&cmd, devid);
1072
1073 iommu_queue_command(iommu, &cmd);
1074}
1075
1076static void iommu_flush_irt_all(struct amd_iommu *iommu)
1077{
1078 u32 devid;
1079
1080 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1081 iommu_flush_irt(iommu, devid);
1082
1083 iommu_completion_wait(iommu);
1084}
1085
7d0c5cc5
JR
1086void iommu_flush_all_caches(struct amd_iommu *iommu)
1087{
58fc7f14
JR
1088 if (iommu_feature(iommu, FEATURE_IA)) {
1089 iommu_flush_all(iommu);
1090 } else {
1091 iommu_flush_dte_all(iommu);
7ef2798d 1092 iommu_flush_irt_all(iommu);
58fc7f14 1093 iommu_flush_tlb_all(iommu);
0518a3a4
JR
1094 }
1095}
1096
431b2a20 1097/*
cb41ed85 1098 * Command send function for flushing on-device TLB
431b2a20 1099 */
6c542047
JR
1100static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1101 u64 address, size_t size)
3fa43655
JR
1102{
1103 struct amd_iommu *iommu;
b00d3bcf 1104 struct iommu_cmd cmd;
cb41ed85 1105 int qdep;
3fa43655 1106
ea61cddb
JR
1107 qdep = dev_data->ats.qdep;
1108 iommu = amd_iommu_rlookup_table[dev_data->devid];
3fa43655 1109
ea61cddb 1110 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
b00d3bcf
JR
1111
1112 return iommu_queue_command(iommu, &cmd);
3fa43655
JR
1113}
1114
431b2a20 1115/*
431b2a20 1116 * Command send function for invalidating a device table entry
431b2a20 1117 */
6c542047 1118static int device_flush_dte(struct iommu_dev_data *dev_data)
a19ae1ec 1119{
3fa43655 1120 struct amd_iommu *iommu;
ee2fa743 1121 int ret;
a19ae1ec 1122
6c542047 1123 iommu = amd_iommu_rlookup_table[dev_data->devid];
a19ae1ec 1124
f62dda66 1125 ret = iommu_flush_dte(iommu, dev_data->devid);
cb41ed85
JR
1126 if (ret)
1127 return ret;
1128
ea61cddb 1129 if (dev_data->ats.enabled)
6c542047 1130 ret = device_flush_iotlb(dev_data, 0, ~0UL);
ee2fa743 1131
ee2fa743 1132 return ret;
a19ae1ec
JR
1133}
1134
431b2a20
JR
1135/*
1136 * TLB invalidation function which is called from the mapping functions.
1137 * It invalidates a single PTE if the range to flush is within a single
1138 * page. Otherwise it flushes the whole TLB of the IOMMU.
1139 */
17b124bf
JR
1140static void __domain_flush_pages(struct protection_domain *domain,
1141 u64 address, size_t size, int pde)
a19ae1ec 1142{
cb41ed85 1143 struct iommu_dev_data *dev_data;
11b6402c
JR
1144 struct iommu_cmd cmd;
1145 int ret = 0, i;
a19ae1ec 1146
11b6402c 1147 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
999ba417 1148
6de8ad9b
JR
1149 for (i = 0; i < amd_iommus_present; ++i) {
1150 if (!domain->dev_iommu[i])
1151 continue;
1152
1153 /*
1154 * Devices of this domain are behind this IOMMU
1155 * We need a TLB flush
1156 */
11b6402c 1157 ret |= iommu_queue_command(amd_iommus[i], &cmd);
6de8ad9b
JR
1158 }
1159
cb41ed85 1160 list_for_each_entry(dev_data, &domain->dev_list, list) {
cb41ed85 1161
ea61cddb 1162 if (!dev_data->ats.enabled)
cb41ed85
JR
1163 continue;
1164
6c542047 1165 ret |= device_flush_iotlb(dev_data, address, size);
cb41ed85
JR
1166 }
1167
11b6402c 1168 WARN_ON(ret);
6de8ad9b
JR
1169}
1170
17b124bf
JR
1171static void domain_flush_pages(struct protection_domain *domain,
1172 u64 address, size_t size)
6de8ad9b 1173{
17b124bf 1174 __domain_flush_pages(domain, address, size, 0);
a19ae1ec 1175}
b6c02715 1176
1c655773 1177/* Flush the whole IO/TLB for a given protection domain */
17b124bf 1178static void domain_flush_tlb(struct protection_domain *domain)
1c655773 1179{
17b124bf 1180 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
1c655773
JR
1181}
1182
42a49f96 1183/* Flush the whole IO/TLB for a given protection domain - including PDE */
17b124bf 1184static void domain_flush_tlb_pde(struct protection_domain *domain)
42a49f96 1185{
17b124bf 1186 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
42a49f96
CW
1187}
1188
17b124bf 1189static void domain_flush_complete(struct protection_domain *domain)
b00d3bcf 1190{
17b124bf 1191 int i;
18811f55 1192
17b124bf
JR
1193 for (i = 0; i < amd_iommus_present; ++i) {
1194 if (!domain->dev_iommu[i])
1195 continue;
bfd1be18 1196
17b124bf
JR
1197 /*
1198 * Devices of this domain are behind this IOMMU
1199 * We need to wait for completion of all commands.
1200 */
1201 iommu_completion_wait(amd_iommus[i]);
bfd1be18 1202 }
e394d72a
JR
1203}
1204
b00d3bcf 1205
09b42804 1206/*
b00d3bcf 1207 * This function flushes the DTEs for all devices in domain
09b42804 1208 */
17b124bf 1209static void domain_flush_devices(struct protection_domain *domain)
e394d72a 1210{
b00d3bcf 1211 struct iommu_dev_data *dev_data;
b26e81b8 1212
b00d3bcf 1213 list_for_each_entry(dev_data, &domain->dev_list, list)
6c542047 1214 device_flush_dte(dev_data);
a345b23b
JR
1215}
1216
431b2a20
JR
1217/****************************************************************************
1218 *
1219 * The functions below are used the create the page table mappings for
1220 * unity mapped regions.
1221 *
1222 ****************************************************************************/
1223
308973d3
JR
1224/*
1225 * This function is used to add another level to an IO page table. Adding
1226 * another level increases the size of the address space by 9 bits to a size up
1227 * to 64 bits.
1228 */
1229static bool increase_address_space(struct protection_domain *domain,
1230 gfp_t gfp)
1231{
1232 u64 *pte;
1233
1234 if (domain->mode == PAGE_MODE_6_LEVEL)
1235 /* address space already 64 bit large */
1236 return false;
1237
1238 pte = (void *)get_zeroed_page(gfp);
1239 if (!pte)
1240 return false;
1241
1242 *pte = PM_LEVEL_PDE(domain->mode,
1243 virt_to_phys(domain->pt_root));
1244 domain->pt_root = pte;
1245 domain->mode += 1;
1246 domain->updated = true;
1247
1248 return true;
1249}
1250
1251static u64 *alloc_pte(struct protection_domain *domain,
1252 unsigned long address,
cbb9d729 1253 unsigned long page_size,
308973d3
JR
1254 u64 **pte_page,
1255 gfp_t gfp)
1256{
cbb9d729 1257 int level, end_lvl;
308973d3 1258 u64 *pte, *page;
cbb9d729
JR
1259
1260 BUG_ON(!is_power_of_2(page_size));
308973d3
JR
1261
1262 while (address > PM_LEVEL_SIZE(domain->mode))
1263 increase_address_space(domain, gfp);
1264
cbb9d729
JR
1265 level = domain->mode - 1;
1266 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1267 address = PAGE_SIZE_ALIGN(address, page_size);
1268 end_lvl = PAGE_SIZE_LEVEL(page_size);
308973d3
JR
1269
1270 while (level > end_lvl) {
1271 if (!IOMMU_PTE_PRESENT(*pte)) {
1272 page = (u64 *)get_zeroed_page(gfp);
1273 if (!page)
1274 return NULL;
1275 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1276 }
1277
cbb9d729
JR
1278 /* No level skipping support yet */
1279 if (PM_PTE_LEVEL(*pte) != level)
1280 return NULL;
1281
308973d3
JR
1282 level -= 1;
1283
1284 pte = IOMMU_PTE_PAGE(*pte);
1285
1286 if (pte_page && level == end_lvl)
1287 *pte_page = pte;
1288
1289 pte = &pte[PM_LEVEL_INDEX(level, address)];
1290 }
1291
1292 return pte;
1293}
1294
1295/*
1296 * This function checks if there is a PTE for a given dma address. If
1297 * there is one, it returns the pointer to it.
1298 */
24cd7723 1299static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
308973d3
JR
1300{
1301 int level;
1302 u64 *pte;
1303
24cd7723
JR
1304 if (address > PM_LEVEL_SIZE(domain->mode))
1305 return NULL;
1306
1307 level = domain->mode - 1;
1308 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
308973d3 1309
24cd7723
JR
1310 while (level > 0) {
1311
1312 /* Not Present */
308973d3
JR
1313 if (!IOMMU_PTE_PRESENT(*pte))
1314 return NULL;
1315
24cd7723
JR
1316 /* Large PTE */
1317 if (PM_PTE_LEVEL(*pte) == 0x07) {
1318 unsigned long pte_mask, __pte;
1319
1320 /*
1321 * If we have a series of large PTEs, make
1322 * sure to return a pointer to the first one.
1323 */
1324 pte_mask = PTE_PAGE_SIZE(*pte);
1325 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1326 __pte = ((unsigned long)pte) & pte_mask;
1327
1328 return (u64 *)__pte;
1329 }
1330
1331 /* No level skipping support yet */
1332 if (PM_PTE_LEVEL(*pte) != level)
1333 return NULL;
1334
308973d3
JR
1335 level -= 1;
1336
24cd7723 1337 /* Walk to the next level */
308973d3
JR
1338 pte = IOMMU_PTE_PAGE(*pte);
1339 pte = &pte[PM_LEVEL_INDEX(level, address)];
308973d3
JR
1340 }
1341
1342 return pte;
1343}
1344
431b2a20
JR
1345/*
1346 * Generic mapping functions. It maps a physical address into a DMA
1347 * address space. It allocates the page table pages if necessary.
1348 * In the future it can be extended to a generic mapping function
1349 * supporting all features of AMD IOMMU page tables like level skipping
1350 * and full 64 bit address spaces.
1351 */
38e817fe
JR
1352static int iommu_map_page(struct protection_domain *dom,
1353 unsigned long bus_addr,
1354 unsigned long phys_addr,
abdc5eb3 1355 int prot,
cbb9d729 1356 unsigned long page_size)
bd0e5211 1357{
8bda3092 1358 u64 __pte, *pte;
cbb9d729 1359 int i, count;
abdc5eb3 1360
bad1cac2 1361 if (!(prot & IOMMU_PROT_MASK))
bd0e5211
JR
1362 return -EINVAL;
1363
cbb9d729
JR
1364 bus_addr = PAGE_ALIGN(bus_addr);
1365 phys_addr = PAGE_ALIGN(phys_addr);
1366 count = PAGE_SIZE_PTE_COUNT(page_size);
1367 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
1368
1369 for (i = 0; i < count; ++i)
1370 if (IOMMU_PTE_PRESENT(pte[i]))
1371 return -EBUSY;
bd0e5211 1372
cbb9d729
JR
1373 if (page_size > PAGE_SIZE) {
1374 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1375 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1376 } else
1377 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
bd0e5211 1378
bd0e5211
JR
1379 if (prot & IOMMU_PROT_IR)
1380 __pte |= IOMMU_PTE_IR;
1381 if (prot & IOMMU_PROT_IW)
1382 __pte |= IOMMU_PTE_IW;
1383
cbb9d729
JR
1384 for (i = 0; i < count; ++i)
1385 pte[i] = __pte;
bd0e5211 1386
04bfdd84
JR
1387 update_domain(dom);
1388
bd0e5211
JR
1389 return 0;
1390}
1391
24cd7723
JR
1392static unsigned long iommu_unmap_page(struct protection_domain *dom,
1393 unsigned long bus_addr,
1394 unsigned long page_size)
eb74ff6c 1395{
24cd7723
JR
1396 unsigned long long unmap_size, unmapped;
1397 u64 *pte;
1398
1399 BUG_ON(!is_power_of_2(page_size));
1400
1401 unmapped = 0;
eb74ff6c 1402
24cd7723
JR
1403 while (unmapped < page_size) {
1404
1405 pte = fetch_pte(dom, bus_addr);
1406
1407 if (!pte) {
1408 /*
1409 * No PTE for this address
1410 * move forward in 4kb steps
1411 */
1412 unmap_size = PAGE_SIZE;
1413 } else if (PM_PTE_LEVEL(*pte) == 0) {
1414 /* 4kb PTE found for this address */
1415 unmap_size = PAGE_SIZE;
1416 *pte = 0ULL;
1417 } else {
1418 int count, i;
1419
1420 /* Large PTE found which maps this address */
1421 unmap_size = PTE_PAGE_SIZE(*pte);
1422 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1423 for (i = 0; i < count; i++)
1424 pte[i] = 0ULL;
1425 }
1426
1427 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1428 unmapped += unmap_size;
1429 }
1430
1431 BUG_ON(!is_power_of_2(unmapped));
eb74ff6c 1432
24cd7723 1433 return unmapped;
eb74ff6c 1434}
eb74ff6c 1435
431b2a20
JR
1436/*
1437 * This function checks if a specific unity mapping entry is needed for
1438 * this specific IOMMU.
1439 */
bd0e5211
JR
1440static int iommu_for_unity_map(struct amd_iommu *iommu,
1441 struct unity_map_entry *entry)
1442{
1443 u16 bdf, i;
1444
1445 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1446 bdf = amd_iommu_alias_table[i];
1447 if (amd_iommu_rlookup_table[bdf] == iommu)
1448 return 1;
1449 }
1450
1451 return 0;
1452}
1453
431b2a20
JR
1454/*
1455 * This function actually applies the mapping to the page table of the
1456 * dma_ops domain.
1457 */
bd0e5211
JR
1458static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1459 struct unity_map_entry *e)
1460{
1461 u64 addr;
1462 int ret;
1463
1464 for (addr = e->address_start; addr < e->address_end;
1465 addr += PAGE_SIZE) {
abdc5eb3 1466 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
cbb9d729 1467 PAGE_SIZE);
bd0e5211
JR
1468 if (ret)
1469 return ret;
1470 /*
1471 * if unity mapping is in aperture range mark the page
1472 * as allocated in the aperture
1473 */
1474 if (addr < dma_dom->aperture_size)
c3239567 1475 __set_bit(addr >> PAGE_SHIFT,
384de729 1476 dma_dom->aperture[0]->bitmap);
bd0e5211
JR
1477 }
1478
1479 return 0;
1480}
1481
171e7b37
JR
1482/*
1483 * Init the unity mappings for a specific IOMMU in the system
1484 *
1485 * Basically iterates over all unity mapping entries and applies them to
1486 * the default domain DMA of that IOMMU if necessary.
1487 */
1488static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1489{
1490 struct unity_map_entry *entry;
1491 int ret;
1492
1493 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1494 if (!iommu_for_unity_map(iommu, entry))
1495 continue;
1496 ret = dma_ops_unity_map(iommu->default_dom, entry);
1497 if (ret)
1498 return ret;
1499 }
1500
1501 return 0;
1502}
1503
431b2a20
JR
1504/*
1505 * Inits the unity mappings required for a specific device
1506 */
bd0e5211
JR
1507static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1508 u16 devid)
1509{
1510 struct unity_map_entry *e;
1511 int ret;
1512
1513 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1514 if (!(devid >= e->devid_start && devid <= e->devid_end))
1515 continue;
1516 ret = dma_ops_unity_map(dma_dom, e);
1517 if (ret)
1518 return ret;
1519 }
1520
1521 return 0;
1522}
1523
431b2a20
JR
1524/****************************************************************************
1525 *
1526 * The next functions belong to the address allocator for the dma_ops
1527 * interface functions. They work like the allocators in the other IOMMU
1528 * drivers. Its basically a bitmap which marks the allocated pages in
1529 * the aperture. Maybe it could be enhanced in the future to a more
1530 * efficient allocator.
1531 *
1532 ****************************************************************************/
d3086444 1533
431b2a20 1534/*
384de729 1535 * The address allocator core functions.
431b2a20
JR
1536 *
1537 * called with domain->lock held
1538 */
384de729 1539
171e7b37
JR
1540/*
1541 * Used to reserve address ranges in the aperture (e.g. for exclusion
1542 * ranges.
1543 */
1544static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1545 unsigned long start_page,
1546 unsigned int pages)
1547{
1548 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1549
1550 if (start_page + pages > last_page)
1551 pages = last_page - start_page;
1552
1553 for (i = start_page; i < start_page + pages; ++i) {
1554 int index = i / APERTURE_RANGE_PAGES;
1555 int page = i % APERTURE_RANGE_PAGES;
1556 __set_bit(page, dom->aperture[index]->bitmap);
1557 }
1558}
1559
9cabe89b
JR
1560/*
1561 * This function is used to add a new aperture range to an existing
1562 * aperture in case of dma_ops domain allocation or address allocation
1563 * failure.
1564 */
576175c2 1565static int alloc_new_range(struct dma_ops_domain *dma_dom,
9cabe89b
JR
1566 bool populate, gfp_t gfp)
1567{
1568 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
576175c2 1569 struct amd_iommu *iommu;
17f5b569 1570 unsigned long i, old_size;
9cabe89b 1571
f5e9705c
JR
1572#ifdef CONFIG_IOMMU_STRESS
1573 populate = false;
1574#endif
1575
9cabe89b
JR
1576 if (index >= APERTURE_MAX_RANGES)
1577 return -ENOMEM;
1578
1579 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1580 if (!dma_dom->aperture[index])
1581 return -ENOMEM;
1582
1583 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1584 if (!dma_dom->aperture[index]->bitmap)
1585 goto out_free;
1586
1587 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1588
1589 if (populate) {
1590 unsigned long address = dma_dom->aperture_size;
1591 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1592 u64 *pte, *pte_page;
1593
1594 for (i = 0; i < num_ptes; ++i) {
cbb9d729 1595 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
9cabe89b
JR
1596 &pte_page, gfp);
1597 if (!pte)
1598 goto out_free;
1599
1600 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1601
1602 address += APERTURE_RANGE_SIZE / 64;
1603 }
1604 }
1605
17f5b569 1606 old_size = dma_dom->aperture_size;
9cabe89b
JR
1607 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1608
17f5b569
JR
1609 /* Reserve address range used for MSI messages */
1610 if (old_size < MSI_ADDR_BASE_LO &&
1611 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1612 unsigned long spage;
1613 int pages;
1614
1615 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1616 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1617
1618 dma_ops_reserve_addresses(dma_dom, spage, pages);
1619 }
1620
b595076a 1621 /* Initialize the exclusion range if necessary */
576175c2
JR
1622 for_each_iommu(iommu) {
1623 if (iommu->exclusion_start &&
1624 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1625 && iommu->exclusion_start < dma_dom->aperture_size) {
1626 unsigned long startpage;
1627 int pages = iommu_num_pages(iommu->exclusion_start,
1628 iommu->exclusion_length,
1629 PAGE_SIZE);
1630 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1631 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1632 }
00cd122a
JR
1633 }
1634
1635 /*
1636 * Check for areas already mapped as present in the new aperture
1637 * range and mark those pages as reserved in the allocator. Such
1638 * mappings may already exist as a result of requested unity
1639 * mappings for devices.
1640 */
1641 for (i = dma_dom->aperture[index]->offset;
1642 i < dma_dom->aperture_size;
1643 i += PAGE_SIZE) {
24cd7723 1644 u64 *pte = fetch_pte(&dma_dom->domain, i);
00cd122a
JR
1645 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1646 continue;
1647
fcd0861d 1648 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
00cd122a
JR
1649 }
1650
04bfdd84
JR
1651 update_domain(&dma_dom->domain);
1652
9cabe89b
JR
1653 return 0;
1654
1655out_free:
04bfdd84
JR
1656 update_domain(&dma_dom->domain);
1657
9cabe89b
JR
1658 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1659
1660 kfree(dma_dom->aperture[index]);
1661 dma_dom->aperture[index] = NULL;
1662
1663 return -ENOMEM;
1664}
1665
384de729
JR
1666static unsigned long dma_ops_area_alloc(struct device *dev,
1667 struct dma_ops_domain *dom,
1668 unsigned int pages,
1669 unsigned long align_mask,
1670 u64 dma_mask,
1671 unsigned long start)
1672{
803b8cb4 1673 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
384de729
JR
1674 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1675 int i = start >> APERTURE_RANGE_SHIFT;
1676 unsigned long boundary_size;
1677 unsigned long address = -1;
1678 unsigned long limit;
1679
803b8cb4
JR
1680 next_bit >>= PAGE_SHIFT;
1681
384de729
JR
1682 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1683 PAGE_SIZE) >> PAGE_SHIFT;
1684
1685 for (;i < max_index; ++i) {
1686 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1687
1688 if (dom->aperture[i]->offset >= dma_mask)
1689 break;
1690
1691 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1692 dma_mask >> PAGE_SHIFT);
1693
1694 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1695 limit, next_bit, pages, 0,
1696 boundary_size, align_mask);
1697 if (address != -1) {
1698 address = dom->aperture[i]->offset +
1699 (address << PAGE_SHIFT);
803b8cb4 1700 dom->next_address = address + (pages << PAGE_SHIFT);
384de729
JR
1701 break;
1702 }
1703
1704 next_bit = 0;
1705 }
1706
1707 return address;
1708}
1709
d3086444
JR
1710static unsigned long dma_ops_alloc_addresses(struct device *dev,
1711 struct dma_ops_domain *dom,
6d4f343f 1712 unsigned int pages,
832a90c3
JR
1713 unsigned long align_mask,
1714 u64 dma_mask)
d3086444 1715{
d3086444 1716 unsigned long address;
d3086444 1717
fe16f088
JR
1718#ifdef CONFIG_IOMMU_STRESS
1719 dom->next_address = 0;
1720 dom->need_flush = true;
1721#endif
d3086444 1722
384de729 1723 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
803b8cb4 1724 dma_mask, dom->next_address);
d3086444 1725
1c655773 1726 if (address == -1) {
803b8cb4 1727 dom->next_address = 0;
384de729
JR
1728 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1729 dma_mask, 0);
1c655773
JR
1730 dom->need_flush = true;
1731 }
d3086444 1732
384de729 1733 if (unlikely(address == -1))
8fd524b3 1734 address = DMA_ERROR_CODE;
d3086444
JR
1735
1736 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1737
1738 return address;
1739}
1740
431b2a20
JR
1741/*
1742 * The address free function.
1743 *
1744 * called with domain->lock held
1745 */
d3086444
JR
1746static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1747 unsigned long address,
1748 unsigned int pages)
1749{
384de729
JR
1750 unsigned i = address >> APERTURE_RANGE_SHIFT;
1751 struct aperture_range *range = dom->aperture[i];
80be308d 1752
384de729
JR
1753 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1754
47bccd6b
JR
1755#ifdef CONFIG_IOMMU_STRESS
1756 if (i < 4)
1757 return;
1758#endif
80be308d 1759
803b8cb4 1760 if (address >= dom->next_address)
80be308d 1761 dom->need_flush = true;
384de729
JR
1762
1763 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
803b8cb4 1764
a66022c4 1765 bitmap_clear(range->bitmap, address, pages);
384de729 1766
d3086444
JR
1767}
1768
431b2a20
JR
1769/****************************************************************************
1770 *
1771 * The next functions belong to the domain allocation. A domain is
1772 * allocated for every IOMMU as the default domain. If device isolation
1773 * is enabled, every device get its own domain. The most important thing
1774 * about domains is the page table mapping the DMA address space they
1775 * contain.
1776 *
1777 ****************************************************************************/
1778
aeb26f55
JR
1779/*
1780 * This function adds a protection domain to the global protection domain list
1781 */
1782static void add_domain_to_list(struct protection_domain *domain)
1783{
1784 unsigned long flags;
1785
1786 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1787 list_add(&domain->list, &amd_iommu_pd_list);
1788 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1789}
1790
1791/*
1792 * This function removes a protection domain to the global
1793 * protection domain list
1794 */
1795static void del_domain_from_list(struct protection_domain *domain)
1796{
1797 unsigned long flags;
1798
1799 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1800 list_del(&domain->list);
1801 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1802}
1803
ec487d1a
JR
1804static u16 domain_id_alloc(void)
1805{
1806 unsigned long flags;
1807 int id;
1808
1809 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1810 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1811 BUG_ON(id == 0);
1812 if (id > 0 && id < MAX_DOMAIN_ID)
1813 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1814 else
1815 id = 0;
1816 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1817
1818 return id;
1819}
1820
a2acfb75
JR
1821static void domain_id_free(int id)
1822{
1823 unsigned long flags;
1824
1825 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1826 if (id > 0 && id < MAX_DOMAIN_ID)
1827 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1828 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1829}
a2acfb75 1830
86db2e5d 1831static void free_pagetable(struct protection_domain *domain)
ec487d1a
JR
1832{
1833 int i, j;
1834 u64 *p1, *p2, *p3;
1835
86db2e5d 1836 p1 = domain->pt_root;
ec487d1a
JR
1837
1838 if (!p1)
1839 return;
1840
1841 for (i = 0; i < 512; ++i) {
1842 if (!IOMMU_PTE_PRESENT(p1[i]))
1843 continue;
1844
1845 p2 = IOMMU_PTE_PAGE(p1[i]);
3cc3d84b 1846 for (j = 0; j < 512; ++j) {
ec487d1a
JR
1847 if (!IOMMU_PTE_PRESENT(p2[j]))
1848 continue;
1849 p3 = IOMMU_PTE_PAGE(p2[j]);
1850 free_page((unsigned long)p3);
1851 }
1852
1853 free_page((unsigned long)p2);
1854 }
1855
1856 free_page((unsigned long)p1);
86db2e5d
JR
1857
1858 domain->pt_root = NULL;
ec487d1a
JR
1859}
1860
b16137b1
JR
1861static void free_gcr3_tbl_level1(u64 *tbl)
1862{
1863 u64 *ptr;
1864 int i;
1865
1866 for (i = 0; i < 512; ++i) {
1867 if (!(tbl[i] & GCR3_VALID))
1868 continue;
1869
1870 ptr = __va(tbl[i] & PAGE_MASK);
1871
1872 free_page((unsigned long)ptr);
1873 }
1874}
1875
1876static void free_gcr3_tbl_level2(u64 *tbl)
1877{
1878 u64 *ptr;
1879 int i;
1880
1881 for (i = 0; i < 512; ++i) {
1882 if (!(tbl[i] & GCR3_VALID))
1883 continue;
1884
1885 ptr = __va(tbl[i] & PAGE_MASK);
1886
1887 free_gcr3_tbl_level1(ptr);
1888 }
1889}
1890
52815b75
JR
1891static void free_gcr3_table(struct protection_domain *domain)
1892{
b16137b1
JR
1893 if (domain->glx == 2)
1894 free_gcr3_tbl_level2(domain->gcr3_tbl);
1895 else if (domain->glx == 1)
1896 free_gcr3_tbl_level1(domain->gcr3_tbl);
1897 else if (domain->glx != 0)
1898 BUG();
1899
52815b75
JR
1900 free_page((unsigned long)domain->gcr3_tbl);
1901}
1902
431b2a20
JR
1903/*
1904 * Free a domain, only used if something went wrong in the
1905 * allocation path and we need to free an already allocated page table
1906 */
ec487d1a
JR
1907static void dma_ops_domain_free(struct dma_ops_domain *dom)
1908{
384de729
JR
1909 int i;
1910
ec487d1a
JR
1911 if (!dom)
1912 return;
1913
aeb26f55
JR
1914 del_domain_from_list(&dom->domain);
1915
86db2e5d 1916 free_pagetable(&dom->domain);
ec487d1a 1917
384de729
JR
1918 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1919 if (!dom->aperture[i])
1920 continue;
1921 free_page((unsigned long)dom->aperture[i]->bitmap);
1922 kfree(dom->aperture[i]);
1923 }
ec487d1a
JR
1924
1925 kfree(dom);
1926}
1927
431b2a20
JR
1928/*
1929 * Allocates a new protection domain usable for the dma_ops functions.
b595076a 1930 * It also initializes the page table and the address allocator data
431b2a20
JR
1931 * structures required for the dma_ops interface
1932 */
87a64d52 1933static struct dma_ops_domain *dma_ops_domain_alloc(void)
ec487d1a
JR
1934{
1935 struct dma_ops_domain *dma_dom;
ec487d1a
JR
1936
1937 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1938 if (!dma_dom)
1939 return NULL;
1940
1941 spin_lock_init(&dma_dom->domain.lock);
1942
1943 dma_dom->domain.id = domain_id_alloc();
1944 if (dma_dom->domain.id == 0)
1945 goto free_dma_dom;
7c392cbe 1946 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
8f7a017c 1947 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
ec487d1a 1948 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
9fdb19d6 1949 dma_dom->domain.flags = PD_DMA_OPS_MASK;
ec487d1a
JR
1950 dma_dom->domain.priv = dma_dom;
1951 if (!dma_dom->domain.pt_root)
1952 goto free_dma_dom;
ec487d1a 1953
1c655773 1954 dma_dom->need_flush = false;
bd60b735 1955 dma_dom->target_dev = 0xffff;
1c655773 1956
aeb26f55
JR
1957 add_domain_to_list(&dma_dom->domain);
1958
576175c2 1959 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
ec487d1a 1960 goto free_dma_dom;
ec487d1a 1961
431b2a20 1962 /*
ec487d1a
JR
1963 * mark the first page as allocated so we never return 0 as
1964 * a valid dma-address. So we can use 0 as error value
431b2a20 1965 */
384de729 1966 dma_dom->aperture[0]->bitmap[0] = 1;
803b8cb4 1967 dma_dom->next_address = 0;
ec487d1a 1968
ec487d1a
JR
1969
1970 return dma_dom;
1971
1972free_dma_dom:
1973 dma_ops_domain_free(dma_dom);
1974
1975 return NULL;
1976}
1977
5b28df6f
JR
1978/*
1979 * little helper function to check whether a given protection domain is a
1980 * dma_ops domain
1981 */
1982static bool dma_ops_domain(struct protection_domain *domain)
1983{
1984 return domain->flags & PD_DMA_OPS_MASK;
1985}
1986
fd7b5535 1987static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
b20ac0d4 1988{
132bd68f 1989 u64 pte_root = 0;
ee6c2868 1990 u64 flags = 0;
863c74eb 1991
132bd68f
JR
1992 if (domain->mode != PAGE_MODE_NONE)
1993 pte_root = virt_to_phys(domain->pt_root);
1994
38ddf41b
JR
1995 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1996 << DEV_ENTRY_MODE_SHIFT;
1997 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
b20ac0d4 1998
ee6c2868
JR
1999 flags = amd_iommu_dev_table[devid].data[1];
2000
fd7b5535
JR
2001 if (ats)
2002 flags |= DTE_FLAG_IOTLB;
2003
52815b75
JR
2004 if (domain->flags & PD_IOMMUV2_MASK) {
2005 u64 gcr3 = __pa(domain->gcr3_tbl);
2006 u64 glx = domain->glx;
2007 u64 tmp;
2008
2009 pte_root |= DTE_FLAG_GV;
2010 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
2011
2012 /* First mask out possible old values for GCR3 table */
2013 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
2014 flags &= ~tmp;
2015
2016 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
2017 flags &= ~tmp;
2018
2019 /* Encode GCR3 table into DTE */
2020 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
2021 pte_root |= tmp;
2022
2023 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
2024 flags |= tmp;
2025
2026 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
2027 flags |= tmp;
2028 }
2029
ee6c2868
JR
2030 flags &= ~(0xffffUL);
2031 flags |= domain->id;
2032
2033 amd_iommu_dev_table[devid].data[1] = flags;
2034 amd_iommu_dev_table[devid].data[0] = pte_root;
15898bbc
JR
2035}
2036
2037static void clear_dte_entry(u16 devid)
2038{
15898bbc
JR
2039 /* remove entry from the device table seen by the hardware */
2040 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
2041 amd_iommu_dev_table[devid].data[1] = 0;
15898bbc
JR
2042
2043 amd_iommu_apply_erratum_63(devid);
7f760ddd
JR
2044}
2045
ec9e79ef
JR
2046static void do_attach(struct iommu_dev_data *dev_data,
2047 struct protection_domain *domain)
7f760ddd 2048{
7f760ddd 2049 struct amd_iommu *iommu;
ec9e79ef 2050 bool ats;
fd7b5535 2051
ec9e79ef
JR
2052 iommu = amd_iommu_rlookup_table[dev_data->devid];
2053 ats = dev_data->ats.enabled;
7f760ddd
JR
2054
2055 /* Update data structures */
2056 dev_data->domain = domain;
2057 list_add(&dev_data->list, &domain->dev_list);
f62dda66 2058 set_dte_entry(dev_data->devid, domain, ats);
7f760ddd
JR
2059
2060 /* Do reference counting */
2061 domain->dev_iommu[iommu->index] += 1;
2062 domain->dev_cnt += 1;
2063
2064 /* Flush the DTE entry */
6c542047 2065 device_flush_dte(dev_data);
7f760ddd
JR
2066}
2067
ec9e79ef 2068static void do_detach(struct iommu_dev_data *dev_data)
7f760ddd 2069{
7f760ddd 2070 struct amd_iommu *iommu;
7f760ddd 2071
ec9e79ef 2072 iommu = amd_iommu_rlookup_table[dev_data->devid];
15898bbc
JR
2073
2074 /* decrease reference counters */
7f760ddd
JR
2075 dev_data->domain->dev_iommu[iommu->index] -= 1;
2076 dev_data->domain->dev_cnt -= 1;
2077
2078 /* Update data structures */
2079 dev_data->domain = NULL;
2080 list_del(&dev_data->list);
f62dda66 2081 clear_dte_entry(dev_data->devid);
15898bbc 2082
7f760ddd 2083 /* Flush the DTE entry */
6c542047 2084 device_flush_dte(dev_data);
2b681faf
JR
2085}
2086
2087/*
2088 * If a device is not yet associated with a domain, this function does
2089 * assigns it visible for the hardware
2090 */
ec9e79ef 2091static int __attach_device(struct iommu_dev_data *dev_data,
15898bbc 2092 struct protection_domain *domain)
2b681faf 2093{
84fe6c19 2094 int ret;
657cbb6b 2095
2b681faf
JR
2096 /* lock domain */
2097 spin_lock(&domain->lock);
2098
71f77580
JR
2099 if (dev_data->alias_data != NULL) {
2100 struct iommu_dev_data *alias_data = dev_data->alias_data;
15898bbc 2101
2b02b091
JR
2102 /* Some sanity checks */
2103 ret = -EBUSY;
2104 if (alias_data->domain != NULL &&
2105 alias_data->domain != domain)
2106 goto out_unlock;
eba6ac60 2107
2b02b091
JR
2108 if (dev_data->domain != NULL &&
2109 dev_data->domain != domain)
2110 goto out_unlock;
15898bbc 2111
2b02b091 2112 /* Do real assignment */
7f760ddd 2113 if (alias_data->domain == NULL)
ec9e79ef 2114 do_attach(alias_data, domain);
24100055
JR
2115
2116 atomic_inc(&alias_data->bind);
657cbb6b 2117 }
15898bbc 2118
7f760ddd 2119 if (dev_data->domain == NULL)
ec9e79ef 2120 do_attach(dev_data, domain);
eba6ac60 2121
24100055
JR
2122 atomic_inc(&dev_data->bind);
2123
84fe6c19
JL
2124 ret = 0;
2125
2126out_unlock:
2127
eba6ac60
JR
2128 /* ready */
2129 spin_unlock(&domain->lock);
15898bbc 2130
84fe6c19 2131 return ret;
0feae533 2132}
b20ac0d4 2133
52815b75
JR
2134
2135static void pdev_iommuv2_disable(struct pci_dev *pdev)
2136{
2137 pci_disable_ats(pdev);
2138 pci_disable_pri(pdev);
2139 pci_disable_pasid(pdev);
2140}
2141
6a113ddc
JR
2142/* FIXME: Change generic reset-function to do the same */
2143static int pri_reset_while_enabled(struct pci_dev *pdev)
2144{
2145 u16 control;
2146 int pos;
2147
46277b75 2148 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
6a113ddc
JR
2149 if (!pos)
2150 return -EINVAL;
2151
46277b75
JR
2152 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2153 control |= PCI_PRI_CTRL_RESET;
2154 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
6a113ddc
JR
2155
2156 return 0;
2157}
2158
52815b75
JR
2159static int pdev_iommuv2_enable(struct pci_dev *pdev)
2160{
6a113ddc
JR
2161 bool reset_enable;
2162 int reqs, ret;
2163
2164 /* FIXME: Hardcode number of outstanding requests for now */
2165 reqs = 32;
2166 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2167 reqs = 1;
2168 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
52815b75
JR
2169
2170 /* Only allow access to user-accessible pages */
2171 ret = pci_enable_pasid(pdev, 0);
2172 if (ret)
2173 goto out_err;
2174
2175 /* First reset the PRI state of the device */
2176 ret = pci_reset_pri(pdev);
2177 if (ret)
2178 goto out_err;
2179
6a113ddc
JR
2180 /* Enable PRI */
2181 ret = pci_enable_pri(pdev, reqs);
52815b75
JR
2182 if (ret)
2183 goto out_err;
2184
6a113ddc
JR
2185 if (reset_enable) {
2186 ret = pri_reset_while_enabled(pdev);
2187 if (ret)
2188 goto out_err;
2189 }
2190
52815b75
JR
2191 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2192 if (ret)
2193 goto out_err;
2194
2195 return 0;
2196
2197out_err:
2198 pci_disable_pri(pdev);
2199 pci_disable_pasid(pdev);
2200
2201 return ret;
2202}
2203
c99afa25 2204/* FIXME: Move this to PCI code */
a3b93121 2205#define PCI_PRI_TLP_OFF (1 << 15)
c99afa25 2206
98f1ad25 2207static bool pci_pri_tlp_required(struct pci_dev *pdev)
c99afa25 2208{
a3b93121 2209 u16 status;
c99afa25
JR
2210 int pos;
2211
46277b75 2212 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
c99afa25
JR
2213 if (!pos)
2214 return false;
2215
a3b93121 2216 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
c99afa25 2217
a3b93121 2218 return (status & PCI_PRI_TLP_OFF) ? true : false;
c99afa25
JR
2219}
2220
407d733e 2221/*
df805abb 2222 * If a device is not yet associated with a domain, this function
407d733e
JR
2223 * assigns it visible for the hardware
2224 */
15898bbc
JR
2225static int attach_device(struct device *dev,
2226 struct protection_domain *domain)
0feae533 2227{
fd7b5535 2228 struct pci_dev *pdev = to_pci_dev(dev);
ea61cddb 2229 struct iommu_dev_data *dev_data;
eba6ac60 2230 unsigned long flags;
15898bbc 2231 int ret;
eba6ac60 2232
ea61cddb
JR
2233 dev_data = get_dev_data(dev);
2234
52815b75
JR
2235 if (domain->flags & PD_IOMMUV2_MASK) {
2236 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2237 return -EINVAL;
2238
2239 if (pdev_iommuv2_enable(pdev) != 0)
2240 return -EINVAL;
2241
2242 dev_data->ats.enabled = true;
2243 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
c99afa25 2244 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
52815b75
JR
2245 } else if (amd_iommu_iotlb_sup &&
2246 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
ea61cddb
JR
2247 dev_data->ats.enabled = true;
2248 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2249 }
fd7b5535 2250
eba6ac60 2251 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
ec9e79ef 2252 ret = __attach_device(dev_data, domain);
b20ac0d4
JR
2253 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2254
0feae533
JR
2255 /*
2256 * We might boot into a crash-kernel here. The crashed kernel
2257 * left the caches in the IOMMU dirty. So we have to flush
2258 * here to evict all dirty stuff.
2259 */
17b124bf 2260 domain_flush_tlb_pde(domain);
15898bbc
JR
2261
2262 return ret;
b20ac0d4
JR
2263}
2264
355bf553
JR
2265/*
2266 * Removes a device from a protection domain (unlocked)
2267 */
ec9e79ef 2268static void __detach_device(struct iommu_dev_data *dev_data)
355bf553 2269{
2ca76279 2270 struct protection_domain *domain;
7c392cbe 2271 unsigned long flags;
c4596114 2272
7f760ddd 2273 BUG_ON(!dev_data->domain);
355bf553 2274
2ca76279
JR
2275 domain = dev_data->domain;
2276
2277 spin_lock_irqsave(&domain->lock, flags);
24100055 2278
71f77580
JR
2279 if (dev_data->alias_data != NULL) {
2280 struct iommu_dev_data *alias_data = dev_data->alias_data;
2281
7f760ddd 2282 if (atomic_dec_and_test(&alias_data->bind))
ec9e79ef 2283 do_detach(alias_data);
24100055
JR
2284 }
2285
7f760ddd 2286 if (atomic_dec_and_test(&dev_data->bind))
ec9e79ef 2287 do_detach(dev_data);
7f760ddd 2288
2ca76279 2289 spin_unlock_irqrestore(&domain->lock, flags);
21129f78
JR
2290
2291 /*
2292 * If we run in passthrough mode the device must be assigned to the
d3ad9373
JR
2293 * passthrough domain if it is detached from any other domain.
2294 * Make sure we can deassign from the pt_domain itself.
21129f78 2295 */
5abcdba4 2296 if (dev_data->passthrough &&
d3ad9373 2297 (dev_data->domain == NULL && domain != pt_domain))
ec9e79ef 2298 __attach_device(dev_data, pt_domain);
355bf553
JR
2299}
2300
2301/*
2302 * Removes a device from a protection domain (with devtable_lock held)
2303 */
15898bbc 2304static void detach_device(struct device *dev)
355bf553 2305{
52815b75 2306 struct protection_domain *domain;
ea61cddb 2307 struct iommu_dev_data *dev_data;
355bf553
JR
2308 unsigned long flags;
2309
ec9e79ef 2310 dev_data = get_dev_data(dev);
52815b75 2311 domain = dev_data->domain;
ec9e79ef 2312
355bf553
JR
2313 /* lock device table */
2314 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
ec9e79ef 2315 __detach_device(dev_data);
355bf553 2316 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
fd7b5535 2317
52815b75
JR
2318 if (domain->flags & PD_IOMMUV2_MASK)
2319 pdev_iommuv2_disable(to_pci_dev(dev));
2320 else if (dev_data->ats.enabled)
ea61cddb 2321 pci_disable_ats(to_pci_dev(dev));
52815b75
JR
2322
2323 dev_data->ats.enabled = false;
355bf553 2324}
e275a2a0 2325
15898bbc
JR
2326/*
2327 * Find out the protection domain structure for a given PCI device. This
2328 * will give us the pointer to the page table root for example.
2329 */
2330static struct protection_domain *domain_for_device(struct device *dev)
2331{
71f77580 2332 struct iommu_dev_data *dev_data;
2b02b091 2333 struct protection_domain *dom = NULL;
15898bbc 2334 unsigned long flags;
15898bbc 2335
657cbb6b 2336 dev_data = get_dev_data(dev);
15898bbc 2337
2b02b091
JR
2338 if (dev_data->domain)
2339 return dev_data->domain;
15898bbc 2340
71f77580
JR
2341 if (dev_data->alias_data != NULL) {
2342 struct iommu_dev_data *alias_data = dev_data->alias_data;
2b02b091
JR
2343
2344 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2345 if (alias_data->domain != NULL) {
2346 __attach_device(dev_data, alias_data->domain);
2347 dom = alias_data->domain;
2348 }
2349 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2350 }
15898bbc
JR
2351
2352 return dom;
2353}
2354
e275a2a0
JR
2355static int device_change_notifier(struct notifier_block *nb,
2356 unsigned long action, void *data)
2357{
e275a2a0 2358 struct dma_ops_domain *dma_domain;
5abcdba4
JR
2359 struct protection_domain *domain;
2360 struct iommu_dev_data *dev_data;
2361 struct device *dev = data;
e275a2a0 2362 struct amd_iommu *iommu;
1ac4cbbc 2363 unsigned long flags;
5abcdba4 2364 u16 devid;
e275a2a0 2365
98fc5a69
JR
2366 if (!check_device(dev))
2367 return 0;
e275a2a0 2368
5abcdba4
JR
2369 devid = get_device_id(dev);
2370 iommu = amd_iommu_rlookup_table[devid];
2371 dev_data = get_dev_data(dev);
e275a2a0
JR
2372
2373 switch (action) {
c1eee67b 2374 case BUS_NOTIFY_UNBOUND_DRIVER:
657cbb6b
JR
2375
2376 domain = domain_for_device(dev);
2377
e275a2a0
JR
2378 if (!domain)
2379 goto out;
5abcdba4 2380 if (dev_data->passthrough)
a1ca331c 2381 break;
15898bbc 2382 detach_device(dev);
1ac4cbbc
JR
2383 break;
2384 case BUS_NOTIFY_ADD_DEVICE:
657cbb6b
JR
2385
2386 iommu_init_device(dev);
2387
2c9195e9
JR
2388 /*
2389 * dev_data is still NULL and
2390 * got initialized in iommu_init_device
2391 */
2392 dev_data = get_dev_data(dev);
2393
2394 if (iommu_pass_through || dev_data->iommu_v2) {
2395 dev_data->passthrough = true;
2396 attach_device(dev, pt_domain);
2397 break;
2398 }
2399
657cbb6b
JR
2400 domain = domain_for_device(dev);
2401
1ac4cbbc
JR
2402 /* allocate a protection domain if a device is added */
2403 dma_domain = find_protection_domain(devid);
2404 if (dma_domain)
2405 goto out;
87a64d52 2406 dma_domain = dma_ops_domain_alloc();
1ac4cbbc
JR
2407 if (!dma_domain)
2408 goto out;
2409 dma_domain->target_dev = devid;
2410
2411 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2412 list_add_tail(&dma_domain->list, &iommu_pd_list);
2413 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2414
ac1534a5
JR
2415 dev_data = get_dev_data(dev);
2416
2c9195e9 2417 dev->archdata.dma_ops = &amd_iommu_dma_ops;
ac1534a5 2418
e275a2a0 2419 break;
657cbb6b
JR
2420 case BUS_NOTIFY_DEL_DEVICE:
2421
2422 iommu_uninit_device(dev);
2423
e275a2a0
JR
2424 default:
2425 goto out;
2426 }
2427
e275a2a0
JR
2428 iommu_completion_wait(iommu);
2429
2430out:
2431 return 0;
2432}
2433
b25ae679 2434static struct notifier_block device_nb = {
e275a2a0
JR
2435 .notifier_call = device_change_notifier,
2436};
355bf553 2437
8638c491
JR
2438void amd_iommu_init_notifier(void)
2439{
2440 bus_register_notifier(&pci_bus_type, &device_nb);
2441}
2442
431b2a20
JR
2443/*****************************************************************************
2444 *
2445 * The next functions belong to the dma_ops mapping/unmapping code.
2446 *
2447 *****************************************************************************/
2448
2449/*
2450 * In the dma_ops path we only have the struct device. This function
2451 * finds the corresponding IOMMU, the protection domain and the
2452 * requestor id for a given device.
2453 * If the device is not yet associated with a domain this is also done
2454 * in this function.
2455 */
94f6d190 2456static struct protection_domain *get_domain(struct device *dev)
b20ac0d4 2457{
94f6d190 2458 struct protection_domain *domain;
b20ac0d4 2459 struct dma_ops_domain *dma_dom;
94f6d190 2460 u16 devid = get_device_id(dev);
b20ac0d4 2461
f99c0f1c 2462 if (!check_device(dev))
94f6d190 2463 return ERR_PTR(-EINVAL);
b20ac0d4 2464
94f6d190
JR
2465 domain = domain_for_device(dev);
2466 if (domain != NULL && !dma_ops_domain(domain))
2467 return ERR_PTR(-EBUSY);
f99c0f1c 2468
94f6d190
JR
2469 if (domain != NULL)
2470 return domain;
b20ac0d4 2471
df805abb 2472 /* Device not bound yet - bind it */
94f6d190 2473 dma_dom = find_protection_domain(devid);
15898bbc 2474 if (!dma_dom)
94f6d190
JR
2475 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2476 attach_device(dev, &dma_dom->domain);
15898bbc 2477 DUMP_printk("Using protection domain %d for device %s\n",
94f6d190 2478 dma_dom->domain.id, dev_name(dev));
f91ba190 2479
94f6d190 2480 return &dma_dom->domain;
b20ac0d4
JR
2481}
2482
04bfdd84
JR
2483static void update_device_table(struct protection_domain *domain)
2484{
492667da 2485 struct iommu_dev_data *dev_data;
04bfdd84 2486
ea61cddb
JR
2487 list_for_each_entry(dev_data, &domain->dev_list, list)
2488 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
04bfdd84
JR
2489}
2490
2491static void update_domain(struct protection_domain *domain)
2492{
2493 if (!domain->updated)
2494 return;
2495
2496 update_device_table(domain);
17b124bf
JR
2497
2498 domain_flush_devices(domain);
2499 domain_flush_tlb_pde(domain);
04bfdd84
JR
2500
2501 domain->updated = false;
2502}
2503
8bda3092
JR
2504/*
2505 * This function fetches the PTE for a given address in the aperture
2506 */
2507static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2508 unsigned long address)
2509{
384de729 2510 struct aperture_range *aperture;
8bda3092
JR
2511 u64 *pte, *pte_page;
2512
384de729
JR
2513 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2514 if (!aperture)
2515 return NULL;
2516
2517 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
8bda3092 2518 if (!pte) {
cbb9d729 2519 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
abdc5eb3 2520 GFP_ATOMIC);
384de729
JR
2521 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2522 } else
8c8c143c 2523 pte += PM_LEVEL_INDEX(0, address);
8bda3092 2524
04bfdd84 2525 update_domain(&dom->domain);
8bda3092
JR
2526
2527 return pte;
2528}
2529
431b2a20
JR
2530/*
2531 * This is the generic map function. It maps one 4kb page at paddr to
2532 * the given address in the DMA address space for the domain.
2533 */
680525e0 2534static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
cb76c322
JR
2535 unsigned long address,
2536 phys_addr_t paddr,
2537 int direction)
2538{
2539 u64 *pte, __pte;
2540
2541 WARN_ON(address > dom->aperture_size);
2542
2543 paddr &= PAGE_MASK;
2544
8bda3092 2545 pte = dma_ops_get_pte(dom, address);
53812c11 2546 if (!pte)
8fd524b3 2547 return DMA_ERROR_CODE;
cb76c322
JR
2548
2549 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2550
2551 if (direction == DMA_TO_DEVICE)
2552 __pte |= IOMMU_PTE_IR;
2553 else if (direction == DMA_FROM_DEVICE)
2554 __pte |= IOMMU_PTE_IW;
2555 else if (direction == DMA_BIDIRECTIONAL)
2556 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2557
2558 WARN_ON(*pte);
2559
2560 *pte = __pte;
2561
2562 return (dma_addr_t)address;
2563}
2564
431b2a20
JR
2565/*
2566 * The generic unmapping function for on page in the DMA address space.
2567 */
680525e0 2568static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
cb76c322
JR
2569 unsigned long address)
2570{
384de729 2571 struct aperture_range *aperture;
cb76c322
JR
2572 u64 *pte;
2573
2574 if (address >= dom->aperture_size)
2575 return;
2576
384de729
JR
2577 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2578 if (!aperture)
2579 return;
2580
2581 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2582 if (!pte)
2583 return;
cb76c322 2584
8c8c143c 2585 pte += PM_LEVEL_INDEX(0, address);
cb76c322
JR
2586
2587 WARN_ON(!*pte);
2588
2589 *pte = 0ULL;
2590}
2591
431b2a20
JR
2592/*
2593 * This function contains common code for mapping of a physically
24f81160
JR
2594 * contiguous memory region into DMA address space. It is used by all
2595 * mapping functions provided with this IOMMU driver.
431b2a20
JR
2596 * Must be called with the domain lock held.
2597 */
cb76c322 2598static dma_addr_t __map_single(struct device *dev,
cb76c322
JR
2599 struct dma_ops_domain *dma_dom,
2600 phys_addr_t paddr,
2601 size_t size,
6d4f343f 2602 int dir,
832a90c3
JR
2603 bool align,
2604 u64 dma_mask)
cb76c322
JR
2605{
2606 dma_addr_t offset = paddr & ~PAGE_MASK;
53812c11 2607 dma_addr_t address, start, ret;
cb76c322 2608 unsigned int pages;
6d4f343f 2609 unsigned long align_mask = 0;
cb76c322
JR
2610 int i;
2611
e3c449f5 2612 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
cb76c322
JR
2613 paddr &= PAGE_MASK;
2614
8ecaf8f1
JR
2615 INC_STATS_COUNTER(total_map_requests);
2616
c1858976
JR
2617 if (pages > 1)
2618 INC_STATS_COUNTER(cross_page);
2619
6d4f343f
JR
2620 if (align)
2621 align_mask = (1UL << get_order(size)) - 1;
2622
11b83888 2623retry:
832a90c3
JR
2624 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2625 dma_mask);
8fd524b3 2626 if (unlikely(address == DMA_ERROR_CODE)) {
11b83888
JR
2627 /*
2628 * setting next_address here will let the address
2629 * allocator only scan the new allocated range in the
2630 * first run. This is a small optimization.
2631 */
2632 dma_dom->next_address = dma_dom->aperture_size;
2633
576175c2 2634 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
11b83888
JR
2635 goto out;
2636
2637 /*
af901ca1 2638 * aperture was successfully enlarged by 128 MB, try
11b83888
JR
2639 * allocation again
2640 */
2641 goto retry;
2642 }
cb76c322
JR
2643
2644 start = address;
2645 for (i = 0; i < pages; ++i) {
680525e0 2646 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
8fd524b3 2647 if (ret == DMA_ERROR_CODE)
53812c11
JR
2648 goto out_unmap;
2649
cb76c322
JR
2650 paddr += PAGE_SIZE;
2651 start += PAGE_SIZE;
2652 }
2653 address += offset;
2654
5774f7c5
JR
2655 ADD_STATS_COUNTER(alloced_io_mem, size);
2656
afa9fdc2 2657 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
17b124bf 2658 domain_flush_tlb(&dma_dom->domain);
1c655773 2659 dma_dom->need_flush = false;
318afd41 2660 } else if (unlikely(amd_iommu_np_cache))
17b124bf 2661 domain_flush_pages(&dma_dom->domain, address, size);
270cab24 2662
cb76c322
JR
2663out:
2664 return address;
53812c11
JR
2665
2666out_unmap:
2667
2668 for (--i; i >= 0; --i) {
2669 start -= PAGE_SIZE;
680525e0 2670 dma_ops_domain_unmap(dma_dom, start);
53812c11
JR
2671 }
2672
2673 dma_ops_free_addresses(dma_dom, address, pages);
2674
8fd524b3 2675 return DMA_ERROR_CODE;
cb76c322
JR
2676}
2677
431b2a20
JR
2678/*
2679 * Does the reverse of the __map_single function. Must be called with
2680 * the domain lock held too
2681 */
cd8c82e8 2682static void __unmap_single(struct dma_ops_domain *dma_dom,
cb76c322
JR
2683 dma_addr_t dma_addr,
2684 size_t size,
2685 int dir)
2686{
04e0463e 2687 dma_addr_t flush_addr;
cb76c322
JR
2688 dma_addr_t i, start;
2689 unsigned int pages;
2690
8fd524b3 2691 if ((dma_addr == DMA_ERROR_CODE) ||
b8d9905d 2692 (dma_addr + size > dma_dom->aperture_size))
cb76c322
JR
2693 return;
2694
04e0463e 2695 flush_addr = dma_addr;
e3c449f5 2696 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
cb76c322
JR
2697 dma_addr &= PAGE_MASK;
2698 start = dma_addr;
2699
2700 for (i = 0; i < pages; ++i) {
680525e0 2701 dma_ops_domain_unmap(dma_dom, start);
cb76c322
JR
2702 start += PAGE_SIZE;
2703 }
2704
5774f7c5
JR
2705 SUB_STATS_COUNTER(alloced_io_mem, size);
2706
cb76c322 2707 dma_ops_free_addresses(dma_dom, dma_addr, pages);
270cab24 2708
80be308d 2709 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
17b124bf 2710 domain_flush_pages(&dma_dom->domain, flush_addr, size);
80be308d
JR
2711 dma_dom->need_flush = false;
2712 }
cb76c322
JR
2713}
2714
431b2a20
JR
2715/*
2716 * The exported map_single function for dma_ops.
2717 */
51491367
FT
2718static dma_addr_t map_page(struct device *dev, struct page *page,
2719 unsigned long offset, size_t size,
2720 enum dma_data_direction dir,
2721 struct dma_attrs *attrs)
4da70b9e
JR
2722{
2723 unsigned long flags;
4da70b9e 2724 struct protection_domain *domain;
4da70b9e 2725 dma_addr_t addr;
832a90c3 2726 u64 dma_mask;
51491367 2727 phys_addr_t paddr = page_to_phys(page) + offset;
4da70b9e 2728
0f2a86f2
JR
2729 INC_STATS_COUNTER(cnt_map_single);
2730
94f6d190
JR
2731 domain = get_domain(dev);
2732 if (PTR_ERR(domain) == -EINVAL)
4da70b9e 2733 return (dma_addr_t)paddr;
94f6d190
JR
2734 else if (IS_ERR(domain))
2735 return DMA_ERROR_CODE;
4da70b9e 2736
f99c0f1c
JR
2737 dma_mask = *dev->dma_mask;
2738
4da70b9e 2739 spin_lock_irqsave(&domain->lock, flags);
94f6d190 2740
cd8c82e8 2741 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
832a90c3 2742 dma_mask);
8fd524b3 2743 if (addr == DMA_ERROR_CODE)
4da70b9e
JR
2744 goto out;
2745
17b124bf 2746 domain_flush_complete(domain);
4da70b9e
JR
2747
2748out:
2749 spin_unlock_irqrestore(&domain->lock, flags);
2750
2751 return addr;
2752}
2753
431b2a20
JR
2754/*
2755 * The exported unmap_single function for dma_ops.
2756 */
51491367
FT
2757static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2758 enum dma_data_direction dir, struct dma_attrs *attrs)
4da70b9e
JR
2759{
2760 unsigned long flags;
4da70b9e 2761 struct protection_domain *domain;
4da70b9e 2762
146a6917
JR
2763 INC_STATS_COUNTER(cnt_unmap_single);
2764
94f6d190
JR
2765 domain = get_domain(dev);
2766 if (IS_ERR(domain))
5b28df6f
JR
2767 return;
2768
4da70b9e
JR
2769 spin_lock_irqsave(&domain->lock, flags);
2770
cd8c82e8 2771 __unmap_single(domain->priv, dma_addr, size, dir);
4da70b9e 2772
17b124bf 2773 domain_flush_complete(domain);
4da70b9e
JR
2774
2775 spin_unlock_irqrestore(&domain->lock, flags);
2776}
2777
431b2a20
JR
2778/*
2779 * This is a special map_sg function which is used if we should map a
2780 * device which is not handled by an AMD IOMMU in the system.
2781 */
65b050ad
JR
2782static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2783 int nelems, int dir)
2784{
2785 struct scatterlist *s;
2786 int i;
2787
2788 for_each_sg(sglist, s, nelems, i) {
2789 s->dma_address = (dma_addr_t)sg_phys(s);
2790 s->dma_length = s->length;
2791 }
2792
2793 return nelems;
2794}
2795
431b2a20
JR
2796/*
2797 * The exported map_sg function for dma_ops (handles scatter-gather
2798 * lists).
2799 */
65b050ad 2800static int map_sg(struct device *dev, struct scatterlist *sglist,
160c1d8e
FT
2801 int nelems, enum dma_data_direction dir,
2802 struct dma_attrs *attrs)
65b050ad
JR
2803{
2804 unsigned long flags;
65b050ad 2805 struct protection_domain *domain;
65b050ad
JR
2806 int i;
2807 struct scatterlist *s;
2808 phys_addr_t paddr;
2809 int mapped_elems = 0;
832a90c3 2810 u64 dma_mask;
65b050ad 2811
d03f067a
JR
2812 INC_STATS_COUNTER(cnt_map_sg);
2813
94f6d190
JR
2814 domain = get_domain(dev);
2815 if (PTR_ERR(domain) == -EINVAL)
f99c0f1c 2816 return map_sg_no_iommu(dev, sglist, nelems, dir);
94f6d190
JR
2817 else if (IS_ERR(domain))
2818 return 0;
dbcc112e 2819
832a90c3 2820 dma_mask = *dev->dma_mask;
65b050ad 2821
65b050ad
JR
2822 spin_lock_irqsave(&domain->lock, flags);
2823
2824 for_each_sg(sglist, s, nelems, i) {
2825 paddr = sg_phys(s);
2826
cd8c82e8 2827 s->dma_address = __map_single(dev, domain->priv,
832a90c3
JR
2828 paddr, s->length, dir, false,
2829 dma_mask);
65b050ad
JR
2830
2831 if (s->dma_address) {
2832 s->dma_length = s->length;
2833 mapped_elems++;
2834 } else
2835 goto unmap;
65b050ad
JR
2836 }
2837
17b124bf 2838 domain_flush_complete(domain);
65b050ad
JR
2839
2840out:
2841 spin_unlock_irqrestore(&domain->lock, flags);
2842
2843 return mapped_elems;
2844unmap:
2845 for_each_sg(sglist, s, mapped_elems, i) {
2846 if (s->dma_address)
cd8c82e8 2847 __unmap_single(domain->priv, s->dma_address,
65b050ad
JR
2848 s->dma_length, dir);
2849 s->dma_address = s->dma_length = 0;
2850 }
2851
2852 mapped_elems = 0;
2853
2854 goto out;
2855}
2856
431b2a20
JR
2857/*
2858 * The exported map_sg function for dma_ops (handles scatter-gather
2859 * lists).
2860 */
65b050ad 2861static void unmap_sg(struct device *dev, struct scatterlist *sglist,
160c1d8e
FT
2862 int nelems, enum dma_data_direction dir,
2863 struct dma_attrs *attrs)
65b050ad
JR
2864{
2865 unsigned long flags;
65b050ad
JR
2866 struct protection_domain *domain;
2867 struct scatterlist *s;
65b050ad
JR
2868 int i;
2869
55877a6b
JR
2870 INC_STATS_COUNTER(cnt_unmap_sg);
2871
94f6d190
JR
2872 domain = get_domain(dev);
2873 if (IS_ERR(domain))
5b28df6f
JR
2874 return;
2875
65b050ad
JR
2876 spin_lock_irqsave(&domain->lock, flags);
2877
2878 for_each_sg(sglist, s, nelems, i) {
cd8c82e8 2879 __unmap_single(domain->priv, s->dma_address,
65b050ad 2880 s->dma_length, dir);
65b050ad
JR
2881 s->dma_address = s->dma_length = 0;
2882 }
2883
17b124bf 2884 domain_flush_complete(domain);
65b050ad
JR
2885
2886 spin_unlock_irqrestore(&domain->lock, flags);
2887}
2888
431b2a20
JR
2889/*
2890 * The exported alloc_coherent function for dma_ops.
2891 */
5d8b53cf 2892static void *alloc_coherent(struct device *dev, size_t size,
baa676fc
AP
2893 dma_addr_t *dma_addr, gfp_t flag,
2894 struct dma_attrs *attrs)
5d8b53cf
JR
2895{
2896 unsigned long flags;
2897 void *virt_addr;
5d8b53cf 2898 struct protection_domain *domain;
5d8b53cf 2899 phys_addr_t paddr;
832a90c3 2900 u64 dma_mask = dev->coherent_dma_mask;
5d8b53cf 2901
c8f0fb36
JR
2902 INC_STATS_COUNTER(cnt_alloc_coherent);
2903
94f6d190
JR
2904 domain = get_domain(dev);
2905 if (PTR_ERR(domain) == -EINVAL) {
f99c0f1c
JR
2906 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2907 *dma_addr = __pa(virt_addr);
2908 return virt_addr;
94f6d190
JR
2909 } else if (IS_ERR(domain))
2910 return NULL;
5d8b53cf 2911
f99c0f1c
JR
2912 dma_mask = dev->coherent_dma_mask;
2913 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2914 flag |= __GFP_ZERO;
5d8b53cf
JR
2915
2916 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2917 if (!virt_addr)
b25ae679 2918 return NULL;
5d8b53cf 2919
5d8b53cf
JR
2920 paddr = virt_to_phys(virt_addr);
2921
832a90c3
JR
2922 if (!dma_mask)
2923 dma_mask = *dev->dma_mask;
2924
5d8b53cf
JR
2925 spin_lock_irqsave(&domain->lock, flags);
2926
cd8c82e8 2927 *dma_addr = __map_single(dev, domain->priv, paddr,
832a90c3 2928 size, DMA_BIDIRECTIONAL, true, dma_mask);
5d8b53cf 2929
8fd524b3 2930 if (*dma_addr == DMA_ERROR_CODE) {
367d04c4 2931 spin_unlock_irqrestore(&domain->lock, flags);
5b28df6f 2932 goto out_free;
367d04c4 2933 }
5d8b53cf 2934
17b124bf 2935 domain_flush_complete(domain);
5d8b53cf 2936
5d8b53cf
JR
2937 spin_unlock_irqrestore(&domain->lock, flags);
2938
2939 return virt_addr;
5b28df6f
JR
2940
2941out_free:
2942
2943 free_pages((unsigned long)virt_addr, get_order(size));
2944
2945 return NULL;
5d8b53cf
JR
2946}
2947
431b2a20
JR
2948/*
2949 * The exported free_coherent function for dma_ops.
431b2a20 2950 */
5d8b53cf 2951static void free_coherent(struct device *dev, size_t size,
baa676fc
AP
2952 void *virt_addr, dma_addr_t dma_addr,
2953 struct dma_attrs *attrs)
5d8b53cf
JR
2954{
2955 unsigned long flags;
5d8b53cf 2956 struct protection_domain *domain;
5d8b53cf 2957
5d31ee7e
JR
2958 INC_STATS_COUNTER(cnt_free_coherent);
2959
94f6d190
JR
2960 domain = get_domain(dev);
2961 if (IS_ERR(domain))
5b28df6f
JR
2962 goto free_mem;
2963
5d8b53cf
JR
2964 spin_lock_irqsave(&domain->lock, flags);
2965
cd8c82e8 2966 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
5d8b53cf 2967
17b124bf 2968 domain_flush_complete(domain);
5d8b53cf
JR
2969
2970 spin_unlock_irqrestore(&domain->lock, flags);
2971
2972free_mem:
2973 free_pages((unsigned long)virt_addr, get_order(size));
2974}
2975
b39ba6ad
JR
2976/*
2977 * This function is called by the DMA layer to find out if we can handle a
2978 * particular device. It is part of the dma_ops.
2979 */
2980static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2981{
420aef8a 2982 return check_device(dev);
b39ba6ad
JR
2983}
2984
c432f3df 2985/*
431b2a20
JR
2986 * The function for pre-allocating protection domains.
2987 *
c432f3df
JR
2988 * If the driver core informs the DMA layer if a driver grabs a device
2989 * we don't need to preallocate the protection domains anymore.
2990 * For now we have to.
2991 */
943bc7e1 2992static void __init prealloc_protection_domains(void)
c432f3df 2993{
5abcdba4 2994 struct iommu_dev_data *dev_data;
c432f3df 2995 struct dma_ops_domain *dma_dom;
5abcdba4 2996 struct pci_dev *dev = NULL;
98fc5a69 2997 u16 devid;
c432f3df 2998
d18c69d3 2999 for_each_pci_dev(dev) {
98fc5a69
JR
3000
3001 /* Do we handle this device? */
3002 if (!check_device(&dev->dev))
c432f3df 3003 continue;
98fc5a69 3004
5abcdba4
JR
3005 dev_data = get_dev_data(&dev->dev);
3006 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
3007 /* Make sure passthrough domain is allocated */
3008 alloc_passthrough_domain();
3009 dev_data->passthrough = true;
3010 attach_device(&dev->dev, pt_domain);
df805abb 3011 pr_info("AMD-Vi: Using passthrough domain for device %s\n",
5abcdba4
JR
3012 dev_name(&dev->dev));
3013 }
3014
98fc5a69 3015 /* Is there already any domain for it? */
15898bbc 3016 if (domain_for_device(&dev->dev))
c432f3df 3017 continue;
98fc5a69
JR
3018
3019 devid = get_device_id(&dev->dev);
3020
87a64d52 3021 dma_dom = dma_ops_domain_alloc();
c432f3df
JR
3022 if (!dma_dom)
3023 continue;
3024 init_unity_mappings_for_device(dma_dom, devid);
bd60b735
JR
3025 dma_dom->target_dev = devid;
3026
15898bbc 3027 attach_device(&dev->dev, &dma_dom->domain);
be831297 3028
bd60b735 3029 list_add_tail(&dma_dom->list, &iommu_pd_list);
c432f3df
JR
3030 }
3031}
3032
160c1d8e 3033static struct dma_map_ops amd_iommu_dma_ops = {
baa676fc
AP
3034 .alloc = alloc_coherent,
3035 .free = free_coherent,
51491367
FT
3036 .map_page = map_page,
3037 .unmap_page = unmap_page,
6631ee9d
JR
3038 .map_sg = map_sg,
3039 .unmap_sg = unmap_sg,
b39ba6ad 3040 .dma_supported = amd_iommu_dma_supported,
6631ee9d
JR
3041};
3042
27c2127a
JR
3043static unsigned device_dma_ops_init(void)
3044{
5abcdba4 3045 struct iommu_dev_data *dev_data;
27c2127a
JR
3046 struct pci_dev *pdev = NULL;
3047 unsigned unhandled = 0;
3048
3049 for_each_pci_dev(pdev) {
3050 if (!check_device(&pdev->dev)) {
af1be049
JR
3051
3052 iommu_ignore_device(&pdev->dev);
3053
27c2127a
JR
3054 unhandled += 1;
3055 continue;
3056 }
3057
5abcdba4
JR
3058 dev_data = get_dev_data(&pdev->dev);
3059
3060 if (!dev_data->passthrough)
3061 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3062 else
3063 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
27c2127a
JR
3064 }
3065
3066 return unhandled;
3067}
3068
431b2a20
JR
3069/*
3070 * The function which clues the AMD IOMMU driver into dma_ops.
3071 */
f5325094
JR
3072
3073void __init amd_iommu_init_api(void)
3074{
2cc21c42 3075 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
f5325094
JR
3076}
3077
6631ee9d
JR
3078int __init amd_iommu_init_dma_ops(void)
3079{
3080 struct amd_iommu *iommu;
27c2127a 3081 int ret, unhandled;
6631ee9d 3082
431b2a20
JR
3083 /*
3084 * first allocate a default protection domain for every IOMMU we
3085 * found in the system. Devices not assigned to any other
3086 * protection domain will be assigned to the default one.
3087 */
3bd22172 3088 for_each_iommu(iommu) {
87a64d52 3089 iommu->default_dom = dma_ops_domain_alloc();
6631ee9d
JR
3090 if (iommu->default_dom == NULL)
3091 return -ENOMEM;
e2dc14a2 3092 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
6631ee9d
JR
3093 ret = iommu_init_unity_mappings(iommu);
3094 if (ret)
3095 goto free_domains;
3096 }
3097
431b2a20 3098 /*
8793abeb 3099 * Pre-allocate the protection domains for each device.
431b2a20 3100 */
8793abeb 3101 prealloc_protection_domains();
6631ee9d
JR
3102
3103 iommu_detected = 1;
75f1cdf1 3104 swiotlb = 0;
6631ee9d 3105
431b2a20 3106 /* Make the driver finally visible to the drivers */
27c2127a
JR
3107 unhandled = device_dma_ops_init();
3108 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3109 /* There are unhandled devices - initialize swiotlb for them */
3110 swiotlb = 1;
3111 }
6631ee9d 3112
7f26508b
JR
3113 amd_iommu_stats_init();
3114
62410eeb
JR
3115 if (amd_iommu_unmap_flush)
3116 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3117 else
3118 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3119
6631ee9d
JR
3120 return 0;
3121
3122free_domains:
3123
3bd22172 3124 for_each_iommu(iommu) {
6631ee9d
JR
3125 if (iommu->default_dom)
3126 dma_ops_domain_free(iommu->default_dom);
3127 }
3128
3129 return ret;
3130}
6d98cd80
JR
3131
3132/*****************************************************************************
3133 *
3134 * The following functions belong to the exported interface of AMD IOMMU
3135 *
3136 * This interface allows access to lower level functions of the IOMMU
3137 * like protection domain handling and assignement of devices to domains
3138 * which is not possible with the dma_ops interface.
3139 *
3140 *****************************************************************************/
3141
6d98cd80
JR
3142static void cleanup_domain(struct protection_domain *domain)
3143{
492667da 3144 struct iommu_dev_data *dev_data, *next;
6d98cd80 3145 unsigned long flags;
6d98cd80
JR
3146
3147 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3148
492667da 3149 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
ec9e79ef 3150 __detach_device(dev_data);
492667da
JR
3151 atomic_set(&dev_data->bind, 0);
3152 }
6d98cd80
JR
3153
3154 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3155}
3156
2650815f
JR
3157static void protection_domain_free(struct protection_domain *domain)
3158{
3159 if (!domain)
3160 return;
3161
aeb26f55
JR
3162 del_domain_from_list(domain);
3163
2650815f
JR
3164 if (domain->id)
3165 domain_id_free(domain->id);
3166
3167 kfree(domain);
3168}
3169
3170static struct protection_domain *protection_domain_alloc(void)
c156e347
JR
3171{
3172 struct protection_domain *domain;
3173
3174 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3175 if (!domain)
2650815f 3176 return NULL;
c156e347
JR
3177
3178 spin_lock_init(&domain->lock);
5d214fe6 3179 mutex_init(&domain->api_lock);
c156e347
JR
3180 domain->id = domain_id_alloc();
3181 if (!domain->id)
2650815f 3182 goto out_err;
7c392cbe 3183 INIT_LIST_HEAD(&domain->dev_list);
2650815f 3184
aeb26f55
JR
3185 add_domain_to_list(domain);
3186
2650815f
JR
3187 return domain;
3188
3189out_err:
3190 kfree(domain);
3191
3192 return NULL;
3193}
3194
5abcdba4
JR
3195static int __init alloc_passthrough_domain(void)
3196{
3197 if (pt_domain != NULL)
3198 return 0;
3199
3200 /* allocate passthrough domain */
3201 pt_domain = protection_domain_alloc();
3202 if (!pt_domain)
3203 return -ENOMEM;
3204
3205 pt_domain->mode = PAGE_MODE_NONE;
3206
3207 return 0;
3208}
2650815f
JR
3209static int amd_iommu_domain_init(struct iommu_domain *dom)
3210{
3211 struct protection_domain *domain;
3212
3213 domain = protection_domain_alloc();
3214 if (!domain)
c156e347 3215 goto out_free;
2650815f
JR
3216
3217 domain->mode = PAGE_MODE_3_LEVEL;
c156e347
JR
3218 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3219 if (!domain->pt_root)
3220 goto out_free;
3221
f3572db8
JR
3222 domain->iommu_domain = dom;
3223
c156e347
JR
3224 dom->priv = domain;
3225
0ff64f80
JR
3226 dom->geometry.aperture_start = 0;
3227 dom->geometry.aperture_end = ~0ULL;
3228 dom->geometry.force_aperture = true;
3229
c156e347
JR
3230 return 0;
3231
3232out_free:
2650815f 3233 protection_domain_free(domain);
c156e347
JR
3234
3235 return -ENOMEM;
3236}
3237
98383fc3
JR
3238static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3239{
3240 struct protection_domain *domain = dom->priv;
3241
3242 if (!domain)
3243 return;
3244
3245 if (domain->dev_cnt > 0)
3246 cleanup_domain(domain);
3247
3248 BUG_ON(domain->dev_cnt != 0);
3249
132bd68f
JR
3250 if (domain->mode != PAGE_MODE_NONE)
3251 free_pagetable(domain);
98383fc3 3252
52815b75
JR
3253 if (domain->flags & PD_IOMMUV2_MASK)
3254 free_gcr3_table(domain);
3255
8b408fe4 3256 protection_domain_free(domain);
98383fc3
JR
3257
3258 dom->priv = NULL;
3259}
3260
684f2888
JR
3261static void amd_iommu_detach_device(struct iommu_domain *dom,
3262 struct device *dev)
3263{
657cbb6b 3264 struct iommu_dev_data *dev_data = dev->archdata.iommu;
684f2888 3265 struct amd_iommu *iommu;
684f2888
JR
3266 u16 devid;
3267
98fc5a69 3268 if (!check_device(dev))
684f2888
JR
3269 return;
3270
98fc5a69 3271 devid = get_device_id(dev);
684f2888 3272
657cbb6b 3273 if (dev_data->domain != NULL)
15898bbc 3274 detach_device(dev);
684f2888
JR
3275
3276 iommu = amd_iommu_rlookup_table[devid];
3277 if (!iommu)
3278 return;
3279
684f2888
JR
3280 iommu_completion_wait(iommu);
3281}
3282
01106066
JR
3283static int amd_iommu_attach_device(struct iommu_domain *dom,
3284 struct device *dev)
3285{
3286 struct protection_domain *domain = dom->priv;
657cbb6b 3287 struct iommu_dev_data *dev_data;
01106066 3288 struct amd_iommu *iommu;
15898bbc 3289 int ret;
01106066 3290
98fc5a69 3291 if (!check_device(dev))
01106066
JR
3292 return -EINVAL;
3293
657cbb6b
JR
3294 dev_data = dev->archdata.iommu;
3295
f62dda66 3296 iommu = amd_iommu_rlookup_table[dev_data->devid];
01106066
JR
3297 if (!iommu)
3298 return -EINVAL;
3299
657cbb6b 3300 if (dev_data->domain)
15898bbc 3301 detach_device(dev);
01106066 3302
15898bbc 3303 ret = attach_device(dev, domain);
01106066
JR
3304
3305 iommu_completion_wait(iommu);
3306
15898bbc 3307 return ret;
01106066
JR
3308}
3309
468e2366 3310static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
5009065d 3311 phys_addr_t paddr, size_t page_size, int iommu_prot)
c6229ca6
JR
3312{
3313 struct protection_domain *domain = dom->priv;
c6229ca6
JR
3314 int prot = 0;
3315 int ret;
3316
132bd68f
JR
3317 if (domain->mode == PAGE_MODE_NONE)
3318 return -EINVAL;
3319
c6229ca6
JR
3320 if (iommu_prot & IOMMU_READ)
3321 prot |= IOMMU_PROT_IR;
3322 if (iommu_prot & IOMMU_WRITE)
3323 prot |= IOMMU_PROT_IW;
3324
5d214fe6 3325 mutex_lock(&domain->api_lock);
795e74f7 3326 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
5d214fe6
JR
3327 mutex_unlock(&domain->api_lock);
3328
795e74f7 3329 return ret;
c6229ca6
JR
3330}
3331
5009065d
OBC
3332static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3333 size_t page_size)
eb74ff6c 3334{
eb74ff6c 3335 struct protection_domain *domain = dom->priv;
5009065d 3336 size_t unmap_size;
eb74ff6c 3337
132bd68f
JR
3338 if (domain->mode == PAGE_MODE_NONE)
3339 return -EINVAL;
3340
5d214fe6 3341 mutex_lock(&domain->api_lock);
468e2366 3342 unmap_size = iommu_unmap_page(domain, iova, page_size);
795e74f7 3343 mutex_unlock(&domain->api_lock);
eb74ff6c 3344
17b124bf 3345 domain_flush_tlb_pde(domain);
5d214fe6 3346
5009065d 3347 return unmap_size;
eb74ff6c
JR
3348}
3349
645c4c8d
JR
3350static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3351 unsigned long iova)
3352{
3353 struct protection_domain *domain = dom->priv;
f03152bb 3354 unsigned long offset_mask;
645c4c8d 3355 phys_addr_t paddr;
f03152bb 3356 u64 *pte, __pte;
645c4c8d 3357
132bd68f
JR
3358 if (domain->mode == PAGE_MODE_NONE)
3359 return iova;
3360
24cd7723 3361 pte = fetch_pte(domain, iova);
645c4c8d 3362
a6d41a40 3363 if (!pte || !IOMMU_PTE_PRESENT(*pte))
645c4c8d
JR
3364 return 0;
3365
f03152bb
JR
3366 if (PM_PTE_LEVEL(*pte) == 0)
3367 offset_mask = PAGE_SIZE - 1;
3368 else
3369 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3370
3371 __pte = *pte & PM_ADDR_MASK;
3372 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
645c4c8d
JR
3373
3374 return paddr;
3375}
3376
dbb9fd86
SY
3377static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3378 unsigned long cap)
3379{
80a506b8
JR
3380 switch (cap) {
3381 case IOMMU_CAP_CACHE_COHERENCY:
3382 return 1;
bdddadcb
JR
3383 case IOMMU_CAP_INTR_REMAP:
3384 return irq_remapping_enabled;
80a506b8
JR
3385 }
3386
dbb9fd86
SY
3387 return 0;
3388}
3389
26961efe
JR
3390static struct iommu_ops amd_iommu_ops = {
3391 .domain_init = amd_iommu_domain_init,
3392 .domain_destroy = amd_iommu_domain_destroy,
3393 .attach_dev = amd_iommu_attach_device,
3394 .detach_dev = amd_iommu_detach_device,
468e2366
JR
3395 .map = amd_iommu_map,
3396 .unmap = amd_iommu_unmap,
26961efe 3397 .iova_to_phys = amd_iommu_iova_to_phys,
dbb9fd86 3398 .domain_has_cap = amd_iommu_domain_has_cap,
aa3de9c0 3399 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
26961efe
JR
3400};
3401
0feae533
JR
3402/*****************************************************************************
3403 *
3404 * The next functions do a basic initialization of IOMMU for pass through
3405 * mode
3406 *
3407 * In passthrough mode the IOMMU is initialized and enabled but not used for
3408 * DMA-API translation.
3409 *
3410 *****************************************************************************/
3411
3412int __init amd_iommu_init_passthrough(void)
3413{
5abcdba4 3414 struct iommu_dev_data *dev_data;
0feae533 3415 struct pci_dev *dev = NULL;
5abcdba4 3416 struct amd_iommu *iommu;
15898bbc 3417 u16 devid;
5abcdba4 3418 int ret;
0feae533 3419
5abcdba4
JR
3420 ret = alloc_passthrough_domain();
3421 if (ret)
3422 return ret;
0feae533 3423
6c54aabd 3424 for_each_pci_dev(dev) {
98fc5a69 3425 if (!check_device(&dev->dev))
0feae533
JR
3426 continue;
3427
5abcdba4
JR
3428 dev_data = get_dev_data(&dev->dev);
3429 dev_data->passthrough = true;
3430
98fc5a69
JR
3431 devid = get_device_id(&dev->dev);
3432
15898bbc 3433 iommu = amd_iommu_rlookup_table[devid];
0feae533
JR
3434 if (!iommu)
3435 continue;
3436
15898bbc 3437 attach_device(&dev->dev, pt_domain);
0feae533
JR
3438 }
3439
2655d7a2
JR
3440 amd_iommu_stats_init();
3441
0feae533
JR
3442 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3443
3444 return 0;
3445}
72e1dcc4
JR
3446
3447/* IOMMUv2 specific functions */
3448int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3449{
3450 return atomic_notifier_chain_register(&ppr_notifier, nb);
3451}
3452EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3453
3454int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3455{
3456 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3457}
3458EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
132bd68f
JR
3459
3460void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3461{
3462 struct protection_domain *domain = dom->priv;
3463 unsigned long flags;
3464
3465 spin_lock_irqsave(&domain->lock, flags);
3466
3467 /* Update data structure */
3468 domain->mode = PAGE_MODE_NONE;
3469 domain->updated = true;
3470
3471 /* Make changes visible to IOMMUs */
3472 update_domain(domain);
3473
3474 /* Page-table is not visible to IOMMU anymore, so free it */
3475 free_pagetable(domain);
3476
3477 spin_unlock_irqrestore(&domain->lock, flags);
3478}
3479EXPORT_SYMBOL(amd_iommu_domain_direct_map);
52815b75
JR
3480
3481int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3482{
3483 struct protection_domain *domain = dom->priv;
3484 unsigned long flags;
3485 int levels, ret;
3486
3487 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3488 return -EINVAL;
3489
3490 /* Number of GCR3 table levels required */
3491 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3492 levels += 1;
3493
3494 if (levels > amd_iommu_max_glx_val)
3495 return -EINVAL;
3496
3497 spin_lock_irqsave(&domain->lock, flags);
3498
3499 /*
3500 * Save us all sanity checks whether devices already in the
3501 * domain support IOMMUv2. Just force that the domain has no
3502 * devices attached when it is switched into IOMMUv2 mode.
3503 */
3504 ret = -EBUSY;
3505 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3506 goto out;
3507
3508 ret = -ENOMEM;
3509 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3510 if (domain->gcr3_tbl == NULL)
3511 goto out;
3512
3513 domain->glx = levels;
3514 domain->flags |= PD_IOMMUV2_MASK;
3515 domain->updated = true;
3516
3517 update_domain(domain);
3518
3519 ret = 0;
3520
3521out:
3522 spin_unlock_irqrestore(&domain->lock, flags);
3523
3524 return ret;
3525}
3526EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
22e266c7
JR
3527
3528static int __flush_pasid(struct protection_domain *domain, int pasid,
3529 u64 address, bool size)
3530{
3531 struct iommu_dev_data *dev_data;
3532 struct iommu_cmd cmd;
3533 int i, ret;
3534
3535 if (!(domain->flags & PD_IOMMUV2_MASK))
3536 return -EINVAL;
3537
3538 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3539
3540 /*
3541 * IOMMU TLB needs to be flushed before Device TLB to
3542 * prevent device TLB refill from IOMMU TLB
3543 */
3544 for (i = 0; i < amd_iommus_present; ++i) {
3545 if (domain->dev_iommu[i] == 0)
3546 continue;
3547
3548 ret = iommu_queue_command(amd_iommus[i], &cmd);
3549 if (ret != 0)
3550 goto out;
3551 }
3552
3553 /* Wait until IOMMU TLB flushes are complete */
3554 domain_flush_complete(domain);
3555
3556 /* Now flush device TLBs */
3557 list_for_each_entry(dev_data, &domain->dev_list, list) {
3558 struct amd_iommu *iommu;
3559 int qdep;
3560
3561 BUG_ON(!dev_data->ats.enabled);
3562
3563 qdep = dev_data->ats.qdep;
3564 iommu = amd_iommu_rlookup_table[dev_data->devid];
3565
3566 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3567 qdep, address, size);
3568
3569 ret = iommu_queue_command(iommu, &cmd);
3570 if (ret != 0)
3571 goto out;
3572 }
3573
3574 /* Wait until all device TLBs are flushed */
3575 domain_flush_complete(domain);
3576
3577 ret = 0;
3578
3579out:
3580
3581 return ret;
3582}
3583
3584static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3585 u64 address)
3586{
399be2f5
JR
3587 INC_STATS_COUNTER(invalidate_iotlb);
3588
22e266c7
JR
3589 return __flush_pasid(domain, pasid, address, false);
3590}
3591
3592int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3593 u64 address)
3594{
3595 struct protection_domain *domain = dom->priv;
3596 unsigned long flags;
3597 int ret;
3598
3599 spin_lock_irqsave(&domain->lock, flags);
3600 ret = __amd_iommu_flush_page(domain, pasid, address);
3601 spin_unlock_irqrestore(&domain->lock, flags);
3602
3603 return ret;
3604}
3605EXPORT_SYMBOL(amd_iommu_flush_page);
3606
3607static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3608{
399be2f5
JR
3609 INC_STATS_COUNTER(invalidate_iotlb_all);
3610
22e266c7
JR
3611 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3612 true);
3613}
3614
3615int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3616{
3617 struct protection_domain *domain = dom->priv;
3618 unsigned long flags;
3619 int ret;
3620
3621 spin_lock_irqsave(&domain->lock, flags);
3622 ret = __amd_iommu_flush_tlb(domain, pasid);
3623 spin_unlock_irqrestore(&domain->lock, flags);
3624
3625 return ret;
3626}
3627EXPORT_SYMBOL(amd_iommu_flush_tlb);
3628
b16137b1
JR
3629static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3630{
3631 int index;
3632 u64 *pte;
3633
3634 while (true) {
3635
3636 index = (pasid >> (9 * level)) & 0x1ff;
3637 pte = &root[index];
3638
3639 if (level == 0)
3640 break;
3641
3642 if (!(*pte & GCR3_VALID)) {
3643 if (!alloc)
3644 return NULL;
3645
3646 root = (void *)get_zeroed_page(GFP_ATOMIC);
3647 if (root == NULL)
3648 return NULL;
3649
3650 *pte = __pa(root) | GCR3_VALID;
3651 }
3652
3653 root = __va(*pte & PAGE_MASK);
3654
3655 level -= 1;
3656 }
3657
3658 return pte;
3659}
3660
3661static int __set_gcr3(struct protection_domain *domain, int pasid,
3662 unsigned long cr3)
3663{
3664 u64 *pte;
3665
3666 if (domain->mode != PAGE_MODE_NONE)
3667 return -EINVAL;
3668
3669 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3670 if (pte == NULL)
3671 return -ENOMEM;
3672
3673 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3674
3675 return __amd_iommu_flush_tlb(domain, pasid);
3676}
3677
3678static int __clear_gcr3(struct protection_domain *domain, int pasid)
3679{
3680 u64 *pte;
3681
3682 if (domain->mode != PAGE_MODE_NONE)
3683 return -EINVAL;
3684
3685 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3686 if (pte == NULL)
3687 return 0;
3688
3689 *pte = 0;
3690
3691 return __amd_iommu_flush_tlb(domain, pasid);
3692}
3693
3694int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3695 unsigned long cr3)
3696{
3697 struct protection_domain *domain = dom->priv;
3698 unsigned long flags;
3699 int ret;
3700
3701 spin_lock_irqsave(&domain->lock, flags);
3702 ret = __set_gcr3(domain, pasid, cr3);
3703 spin_unlock_irqrestore(&domain->lock, flags);
3704
3705 return ret;
3706}
3707EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3708
3709int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3710{
3711 struct protection_domain *domain = dom->priv;
3712 unsigned long flags;
3713 int ret;
3714
3715 spin_lock_irqsave(&domain->lock, flags);
3716 ret = __clear_gcr3(domain, pasid);
3717 spin_unlock_irqrestore(&domain->lock, flags);
3718
3719 return ret;
3720}
3721EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
c99afa25
JR
3722
3723int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3724 int status, int tag)
3725{
3726 struct iommu_dev_data *dev_data;
3727 struct amd_iommu *iommu;
3728 struct iommu_cmd cmd;
3729
399be2f5
JR
3730 INC_STATS_COUNTER(complete_ppr);
3731
c99afa25
JR
3732 dev_data = get_dev_data(&pdev->dev);
3733 iommu = amd_iommu_rlookup_table[dev_data->devid];
3734
3735 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3736 tag, dev_data->pri_tlp);
3737
3738 return iommu_queue_command(iommu, &cmd);
3739}
3740EXPORT_SYMBOL(amd_iommu_complete_ppr);
f3572db8
JR
3741
3742struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3743{
3744 struct protection_domain *domain;
3745
3746 domain = get_domain(&pdev->dev);
3747 if (IS_ERR(domain))
3748 return NULL;
3749
3750 /* Only return IOMMUv2 domains */
3751 if (!(domain->flags & PD_IOMMUV2_MASK))
3752 return NULL;
3753
3754 return domain->iommu_domain;
3755}
3756EXPORT_SYMBOL(amd_iommu_get_v2_domain);
6a113ddc
JR
3757
3758void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3759{
3760 struct iommu_dev_data *dev_data;
3761
3762 if (!amd_iommu_v2_supported())
3763 return;
3764
3765 dev_data = get_dev_data(&pdev->dev);
3766 dev_data->errata |= (1 << erratum);
3767}
3768EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
52efdb89
JR
3769
3770int amd_iommu_device_info(struct pci_dev *pdev,
3771 struct amd_iommu_device_info *info)
3772{
3773 int max_pasids;
3774 int pos;
3775
3776 if (pdev == NULL || info == NULL)
3777 return -EINVAL;
3778
3779 if (!amd_iommu_v2_supported())
3780 return -EINVAL;
3781
3782 memset(info, 0, sizeof(*info));
3783
3784 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3785 if (pos)
3786 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3787
3788 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3789 if (pos)
3790 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3791
3792 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3793 if (pos) {
3794 int features;
3795
3796 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3797 max_pasids = min(max_pasids, (1 << 20));
3798
3799 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3800 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3801
3802 features = pci_pasid_features(pdev);
3803 if (features & PCI_PASID_CAP_EXEC)
3804 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3805 if (features & PCI_PASID_CAP_PRIV)
3806 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3807 }
3808
3809 return 0;
3810}
3811EXPORT_SYMBOL(amd_iommu_device_info);
2b324506
JR
3812
3813#ifdef CONFIG_IRQ_REMAP
3814
3815/*****************************************************************************
3816 *
3817 * Interrupt Remapping Implementation
3818 *
3819 *****************************************************************************/
3820
3821union irte {
3822 u32 val;
3823 struct {
3824 u32 valid : 1,
3825 no_fault : 1,
3826 int_type : 3,
3827 rq_eoi : 1,
3828 dm : 1,
3829 rsvd_1 : 1,
3830 destination : 8,
3831 vector : 8,
3832 rsvd_2 : 8;
3833 } fields;
3834};
3835
3836#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3837#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3838#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3839#define DTE_IRQ_REMAP_ENABLE 1ULL
3840
3841static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3842{
3843 u64 dte;
3844
3845 dte = amd_iommu_dev_table[devid].data[2];
3846 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3847 dte |= virt_to_phys(table->table);
3848 dte |= DTE_IRQ_REMAP_INTCTL;
3849 dte |= DTE_IRQ_TABLE_LEN;
3850 dte |= DTE_IRQ_REMAP_ENABLE;
3851
3852 amd_iommu_dev_table[devid].data[2] = dte;
3853}
3854
3855#define IRTE_ALLOCATED (~1U)
3856
3857static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3858{
3859 struct irq_remap_table *table = NULL;
3860 struct amd_iommu *iommu;
3861 unsigned long flags;
3862 u16 alias;
3863
3864 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3865
3866 iommu = amd_iommu_rlookup_table[devid];
3867 if (!iommu)
3868 goto out_unlock;
3869
3870 table = irq_lookup_table[devid];
3871 if (table)
3872 goto out;
3873
3874 alias = amd_iommu_alias_table[devid];
3875 table = irq_lookup_table[alias];
3876 if (table) {
3877 irq_lookup_table[devid] = table;
3878 set_dte_irq_entry(devid, table);
3879 iommu_flush_dte(iommu, devid);
3880 goto out;
3881 }
3882
3883 /* Nothing there yet, allocate new irq remapping table */
3884 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3885 if (!table)
3886 goto out;
3887
3888 if (ioapic)
3889 /* Keep the first 32 indexes free for IOAPIC interrupts */
3890 table->min_index = 32;
3891
3892 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3893 if (!table->table) {
3894 kfree(table);
821f0f68 3895 table = NULL;
2b324506
JR
3896 goto out;
3897 }
3898
3899 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3900
3901 if (ioapic) {
3902 int i;
3903
3904 for (i = 0; i < 32; ++i)
3905 table->table[i] = IRTE_ALLOCATED;
3906 }
3907
3908 irq_lookup_table[devid] = table;
3909 set_dte_irq_entry(devid, table);
3910 iommu_flush_dte(iommu, devid);
3911 if (devid != alias) {
3912 irq_lookup_table[alias] = table;
3913 set_dte_irq_entry(devid, table);
3914 iommu_flush_dte(iommu, alias);
3915 }
3916
3917out:
3918 iommu_completion_wait(iommu);
3919
3920out_unlock:
3921 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3922
3923 return table;
3924}
3925
3926static int alloc_irq_index(struct irq_cfg *cfg, u16 devid, int count)
3927{
3928 struct irq_remap_table *table;
3929 unsigned long flags;
3930 int index, c;
3931
3932 table = get_irq_table(devid, false);
3933 if (!table)
3934 return -ENODEV;
3935
3936 spin_lock_irqsave(&table->lock, flags);
3937
3938 /* Scan table for free entries */
3939 for (c = 0, index = table->min_index;
3940 index < MAX_IRQS_PER_TABLE;
3941 ++index) {
3942 if (table->table[index] == 0)
3943 c += 1;
3944 else
3945 c = 0;
3946
3947 if (c == count) {
3948 struct irq_2_iommu *irte_info;
3949
3950 for (; c != 0; --c)
3951 table->table[index - c + 1] = IRTE_ALLOCATED;
3952
3953 index -= count - 1;
3954
3955 irte_info = &cfg->irq_2_iommu;
3956 irte_info->sub_handle = devid;
3957 irte_info->irte_index = index;
3958 irte_info->iommu = (void *)cfg;
3959
3960 goto out;
3961 }
3962 }
3963
3964 index = -ENOSPC;
3965
3966out:
3967 spin_unlock_irqrestore(&table->lock, flags);
3968
3969 return index;
3970}
3971
3972static int get_irte(u16 devid, int index, union irte *irte)
3973{
3974 struct irq_remap_table *table;
3975 unsigned long flags;
3976
3977 table = get_irq_table(devid, false);
3978 if (!table)
3979 return -ENOMEM;
3980
3981 spin_lock_irqsave(&table->lock, flags);
3982 irte->val = table->table[index];
3983 spin_unlock_irqrestore(&table->lock, flags);
3984
3985 return 0;
3986}
3987
3988static int modify_irte(u16 devid, int index, union irte irte)
3989{
3990 struct irq_remap_table *table;
3991 struct amd_iommu *iommu;
3992 unsigned long flags;
3993
3994 iommu = amd_iommu_rlookup_table[devid];
3995 if (iommu == NULL)
3996 return -EINVAL;
3997
3998 table = get_irq_table(devid, false);
3999 if (!table)
4000 return -ENOMEM;
4001
4002 spin_lock_irqsave(&table->lock, flags);
4003 table->table[index] = irte.val;
4004 spin_unlock_irqrestore(&table->lock, flags);
4005
4006 iommu_flush_irt(iommu, devid);
4007 iommu_completion_wait(iommu);
4008
4009 return 0;
4010}
4011
4012static void free_irte(u16 devid, int index)
4013{
4014 struct irq_remap_table *table;
4015 struct amd_iommu *iommu;
4016 unsigned long flags;
4017
4018 iommu = amd_iommu_rlookup_table[devid];
4019 if (iommu == NULL)
4020 return;
4021
4022 table = get_irq_table(devid, false);
4023 if (!table)
4024 return;
4025
4026 spin_lock_irqsave(&table->lock, flags);
4027 table->table[index] = 0;
4028 spin_unlock_irqrestore(&table->lock, flags);
4029
4030 iommu_flush_irt(iommu, devid);
4031 iommu_completion_wait(iommu);
4032}
4033
5527de74
JR
4034static int setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
4035 unsigned int destination, int vector,
4036 struct io_apic_irq_attr *attr)
4037{
4038 struct irq_remap_table *table;
4039 struct irq_2_iommu *irte_info;
4040 struct irq_cfg *cfg;
4041 union irte irte;
4042 int ioapic_id;
4043 int index;
4044 int devid;
4045 int ret;
4046
4047 cfg = irq_get_chip_data(irq);
4048 if (!cfg)
4049 return -EINVAL;
4050
4051 irte_info = &cfg->irq_2_iommu;
4052 ioapic_id = mpc_ioapic_id(attr->ioapic);
4053 devid = get_ioapic_devid(ioapic_id);
4054
4055 if (devid < 0)
4056 return devid;
4057
4058 table = get_irq_table(devid, true);
4059 if (table == NULL)
4060 return -ENOMEM;
4061
4062 index = attr->ioapic_pin;
4063
4064 /* Setup IRQ remapping info */
4065 irte_info->sub_handle = devid;
4066 irte_info->irte_index = index;
4067 irte_info->iommu = (void *)cfg;
4068
4069 /* Setup IRTE for IOMMU */
4070 irte.val = 0;
4071 irte.fields.vector = vector;
4072 irte.fields.int_type = apic->irq_delivery_mode;
4073 irte.fields.destination = destination;
4074 irte.fields.dm = apic->irq_dest_mode;
4075 irte.fields.valid = 1;
4076
4077 ret = modify_irte(devid, index, irte);
4078 if (ret)
4079 return ret;
4080
4081 /* Setup IOAPIC entry */
4082 memset(entry, 0, sizeof(*entry));
4083
4084 entry->vector = index;
4085 entry->mask = 0;
4086 entry->trigger = attr->trigger;
4087 entry->polarity = attr->polarity;
4088
4089 /*
4090 * Mask level triggered irqs.
5527de74
JR
4091 */
4092 if (attr->trigger)
4093 entry->mask = 1;
4094
4095 return 0;
4096}
4097
4098static int set_affinity(struct irq_data *data, const struct cpumask *mask,
4099 bool force)
4100{
4101 struct irq_2_iommu *irte_info;
4102 unsigned int dest, irq;
4103 struct irq_cfg *cfg;
4104 union irte irte;
4105 int err;
4106
4107 if (!config_enabled(CONFIG_SMP))
4108 return -1;
4109
4110 cfg = data->chip_data;
4111 irq = data->irq;
4112 irte_info = &cfg->irq_2_iommu;
4113
4114 if (!cpumask_intersects(mask, cpu_online_mask))
4115 return -EINVAL;
4116
4117 if (get_irte(irte_info->sub_handle, irte_info->irte_index, &irte))
4118 return -EBUSY;
4119
4120 if (assign_irq_vector(irq, cfg, mask))
4121 return -EBUSY;
4122
4123 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
4124 if (err) {
4125 if (assign_irq_vector(irq, cfg, data->affinity))
4126 pr_err("AMD-Vi: Failed to recover vector for irq %d\n", irq);
4127 return err;
4128 }
4129
4130 irte.fields.vector = cfg->vector;
4131 irte.fields.destination = dest;
4132
4133 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4134
4135 if (cfg->move_in_progress)
4136 send_cleanup_vector(cfg);
4137
4138 cpumask_copy(data->affinity, mask);
4139
4140 return 0;
4141}
4142
4143static int free_irq(int irq)
4144{
4145 struct irq_2_iommu *irte_info;
4146 struct irq_cfg *cfg;
4147
4148 cfg = irq_get_chip_data(irq);
4149 if (!cfg)
4150 return -EINVAL;
4151
4152 irte_info = &cfg->irq_2_iommu;
4153
4154 free_irte(irte_info->sub_handle, irte_info->irte_index);
4155
4156 return 0;
4157}
4158
0b4d48cb
JR
4159static void compose_msi_msg(struct pci_dev *pdev,
4160 unsigned int irq, unsigned int dest,
4161 struct msi_msg *msg, u8 hpet_id)
4162{
4163 struct irq_2_iommu *irte_info;
4164 struct irq_cfg *cfg;
4165 union irte irte;
4166
4167 cfg = irq_get_chip_data(irq);
4168 if (!cfg)
4169 return;
4170
4171 irte_info = &cfg->irq_2_iommu;
4172
4173 irte.val = 0;
4174 irte.fields.vector = cfg->vector;
4175 irte.fields.int_type = apic->irq_delivery_mode;
4176 irte.fields.destination = dest;
4177 irte.fields.dm = apic->irq_dest_mode;
4178 irte.fields.valid = 1;
4179
4180 modify_irte(irte_info->sub_handle, irte_info->irte_index, irte);
4181
4182 msg->address_hi = MSI_ADDR_BASE_HI;
4183 msg->address_lo = MSI_ADDR_BASE_LO;
4184 msg->data = irte_info->irte_index;
4185}
4186
4187static int msi_alloc_irq(struct pci_dev *pdev, int irq, int nvec)
4188{
4189 struct irq_cfg *cfg;
4190 int index;
4191 u16 devid;
4192
4193 if (!pdev)
4194 return -EINVAL;
4195
4196 cfg = irq_get_chip_data(irq);
4197 if (!cfg)
4198 return -EINVAL;
4199
4200 devid = get_device_id(&pdev->dev);
4201 index = alloc_irq_index(cfg, devid, nvec);
4202
4203 return index < 0 ? MAX_IRQS_PER_TABLE : index;
4204}
4205
4206static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
4207 int index, int offset)
4208{
4209 struct irq_2_iommu *irte_info;
4210 struct irq_cfg *cfg;
4211 u16 devid;
4212
4213 if (!pdev)
4214 return -EINVAL;
4215
4216 cfg = irq_get_chip_data(irq);
4217 if (!cfg)
4218 return -EINVAL;
4219
4220 if (index >= MAX_IRQS_PER_TABLE)
4221 return 0;
4222
4223 devid = get_device_id(&pdev->dev);
4224 irte_info = &cfg->irq_2_iommu;
4225
4226 irte_info->sub_handle = devid;
4227 irte_info->irte_index = index + offset;
4228 irte_info->iommu = (void *)cfg;
4229
4230 return 0;
4231}
4232
d976195c
JR
4233static int setup_hpet_msi(unsigned int irq, unsigned int id)
4234{
4235 struct irq_2_iommu *irte_info;
4236 struct irq_cfg *cfg;
4237 int index, devid;
4238
4239 cfg = irq_get_chip_data(irq);
4240 if (!cfg)
4241 return -EINVAL;
4242
4243 irte_info = &cfg->irq_2_iommu;
4244 devid = get_hpet_devid(id);
4245 if (devid < 0)
4246 return devid;
4247
4248 index = alloc_irq_index(cfg, devid, 1);
4249 if (index < 0)
4250 return index;
4251
4252 irte_info->sub_handle = devid;
4253 irte_info->irte_index = index;
4254 irte_info->iommu = (void *)cfg;
4255
4256 return 0;
4257}
4258
6b474b82
JR
4259struct irq_remap_ops amd_iommu_irq_ops = {
4260 .supported = amd_iommu_supported,
4261 .prepare = amd_iommu_prepare,
4262 .enable = amd_iommu_enable,
4263 .disable = amd_iommu_disable,
4264 .reenable = amd_iommu_reenable,
4265 .enable_faulting = amd_iommu_enable_faulting,
4266 .setup_ioapic_entry = setup_ioapic_entry,
4267 .set_affinity = set_affinity,
4268 .free_irq = free_irq,
4269 .compose_msi_msg = compose_msi_msg,
4270 .msi_alloc_irq = msi_alloc_irq,
4271 .msi_setup_irq = msi_setup_irq,
4272 .setup_hpet_msi = setup_hpet_msi,
4273};
2b324506 4274#endif