2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
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.
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.
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
20 #include <linux/ratelimit.h>
21 #include <linux/pci.h>
22 #include <linux/pci-ats.h>
23 #include <linux/bitmap.h>
24 #include <linux/slab.h>
25 #include <linux/debugfs.h>
26 #include <linux/scatterlist.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/iommu-helper.h>
29 #include <linux/iommu.h>
30 #include <linux/delay.h>
31 #include <linux/amd-iommu.h>
32 #include <linux/notifier.h>
33 #include <linux/export.h>
34 #include <asm/msidef.h>
35 #include <asm/proto.h>
36 #include <asm/iommu.h>
40 #include "amd_iommu_proto.h"
41 #include "amd_iommu_types.h"
43 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
45 #define LOOP_TIMEOUT 100000
48 * This bitmap is used to advertise the page sizes our hardware support
49 * to the IOMMU core, which will then use this information to split
50 * physically contiguous memory regions it is mapping into page sizes
53 * Traditionally the IOMMU core just handed us the mappings directly,
54 * after making sure the size is an order of a 4KiB page and that the
55 * mapping has natural alignment.
57 * To retain this behavior, we currently advertise that we support
58 * all page sizes that are an order of 4KiB.
60 * If at some point we'd like to utilize the IOMMU core's new behavior,
61 * we could change this to advertise the real page sizes we support.
63 #define AMD_IOMMU_PGSIZES (~0xFFFUL)
65 static DEFINE_RWLOCK(amd_iommu_devtable_lock);
67 /* A list of preallocated protection domains */
68 static LIST_HEAD(iommu_pd_list);
69 static DEFINE_SPINLOCK(iommu_pd_list_lock);
71 /* List of all available dev_data structures */
72 static LIST_HEAD(dev_data_list);
73 static DEFINE_SPINLOCK(dev_data_list_lock);
75 LIST_HEAD(ioapic_map);
79 * Domain for untranslated devices - only allocated
80 * if iommu=pt passed on kernel cmd line.
82 static struct protection_domain *pt_domain;
84 static struct iommu_ops amd_iommu_ops;
86 static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
87 int amd_iommu_max_glx_val = -1;
89 static struct dma_map_ops amd_iommu_dma_ops;
92 * general struct to manage commands send to an IOMMU
98 static void update_domain(struct protection_domain *domain);
99 static int __init alloc_passthrough_domain(void);
101 /****************************************************************************
105 ****************************************************************************/
107 static struct iommu_dev_data *alloc_dev_data(u16 devid)
109 struct iommu_dev_data *dev_data;
112 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
116 dev_data->devid = devid;
117 atomic_set(&dev_data->bind, 0);
119 spin_lock_irqsave(&dev_data_list_lock, flags);
120 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
121 spin_unlock_irqrestore(&dev_data_list_lock, flags);
126 static void free_dev_data(struct iommu_dev_data *dev_data)
130 spin_lock_irqsave(&dev_data_list_lock, flags);
131 list_del(&dev_data->dev_data_list);
132 spin_unlock_irqrestore(&dev_data_list_lock, flags);
137 static struct iommu_dev_data *search_dev_data(u16 devid)
139 struct iommu_dev_data *dev_data;
142 spin_lock_irqsave(&dev_data_list_lock, flags);
143 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
144 if (dev_data->devid == devid)
151 spin_unlock_irqrestore(&dev_data_list_lock, flags);
156 static struct iommu_dev_data *find_dev_data(u16 devid)
158 struct iommu_dev_data *dev_data;
160 dev_data = search_dev_data(devid);
162 if (dev_data == NULL)
163 dev_data = alloc_dev_data(devid);
168 static inline u16 get_device_id(struct device *dev)
170 struct pci_dev *pdev = to_pci_dev(dev);
172 return calc_devid(pdev->bus->number, pdev->devfn);
175 static struct iommu_dev_data *get_dev_data(struct device *dev)
177 return dev->archdata.iommu;
180 static bool pci_iommuv2_capable(struct pci_dev *pdev)
182 static const int caps[] = {
185 PCI_EXT_CAP_ID_PASID,
189 for (i = 0; i < 3; ++i) {
190 pos = pci_find_ext_capability(pdev, caps[i]);
198 static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
200 struct iommu_dev_data *dev_data;
202 dev_data = get_dev_data(&pdev->dev);
204 return dev_data->errata & (1 << erratum) ? true : false;
208 * In this function the list of preallocated protection domains is traversed to
209 * find the domain for a specific device
211 static struct dma_ops_domain *find_protection_domain(u16 devid)
213 struct dma_ops_domain *entry, *ret = NULL;
215 u16 alias = amd_iommu_alias_table[devid];
217 if (list_empty(&iommu_pd_list))
220 spin_lock_irqsave(&iommu_pd_list_lock, flags);
222 list_for_each_entry(entry, &iommu_pd_list, list) {
223 if (entry->target_dev == devid ||
224 entry->target_dev == alias) {
230 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
236 * This function checks if the driver got a valid device from the caller to
237 * avoid dereferencing invalid pointers.
239 static bool check_device(struct device *dev)
243 if (!dev || !dev->dma_mask)
246 /* No device or no PCI device */
247 if (dev->bus != &pci_bus_type)
250 devid = get_device_id(dev);
252 /* Out of our scope? */
253 if (devid > amd_iommu_last_bdf)
256 if (amd_iommu_rlookup_table[devid] == NULL)
262 static void swap_pci_ref(struct pci_dev **from, struct pci_dev *to)
268 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
270 static int iommu_init_device(struct device *dev)
272 struct pci_dev *dma_pdev, *pdev = to_pci_dev(dev);
273 struct iommu_dev_data *dev_data;
274 struct iommu_group *group;
278 if (dev->archdata.iommu)
281 dev_data = find_dev_data(get_device_id(dev));
285 alias = amd_iommu_alias_table[dev_data->devid];
286 if (alias != dev_data->devid) {
287 struct iommu_dev_data *alias_data;
289 alias_data = find_dev_data(alias);
290 if (alias_data == NULL) {
291 pr_err("AMD-Vi: Warning: Unhandled device %s\n",
293 free_dev_data(dev_data);
296 dev_data->alias_data = alias_data;
298 dma_pdev = pci_get_bus_and_slot(alias >> 8, alias & 0xff);
300 dma_pdev = pci_dev_get(pdev);
302 /* Account for quirked devices */
303 swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
306 * If it's a multifunction device that does not support our
307 * required ACS flags, add to the same group as function 0.
309 if (dma_pdev->multifunction &&
310 !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
311 swap_pci_ref(&dma_pdev,
312 pci_get_slot(dma_pdev->bus,
313 PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
317 * Devices on the root bus go through the iommu. If that's not us,
318 * find the next upstream device and test ACS up to the root bus.
319 * Finding the next device may require skipping virtual buses.
321 while (!pci_is_root_bus(dma_pdev->bus)) {
322 struct pci_bus *bus = dma_pdev->bus;
325 if (!pci_is_root_bus(bus))
331 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
334 swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
338 group = iommu_group_get(&dma_pdev->dev);
339 pci_dev_put(dma_pdev);
341 group = iommu_group_alloc();
343 return PTR_ERR(group);
346 ret = iommu_group_add_device(group, dev);
348 iommu_group_put(group);
353 if (pci_iommuv2_capable(pdev)) {
354 struct amd_iommu *iommu;
356 iommu = amd_iommu_rlookup_table[dev_data->devid];
357 dev_data->iommu_v2 = iommu->is_iommu_v2;
360 dev->archdata.iommu = dev_data;
365 static void iommu_ignore_device(struct device *dev)
369 devid = get_device_id(dev);
370 alias = amd_iommu_alias_table[devid];
372 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
373 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
375 amd_iommu_rlookup_table[devid] = NULL;
376 amd_iommu_rlookup_table[alias] = NULL;
379 static void iommu_uninit_device(struct device *dev)
381 iommu_group_remove_device(dev);
384 * Nothing to do here - we keep dev_data around for unplugged devices
385 * and reuse it when the device is re-plugged - not doing so would
386 * introduce a ton of races.
390 void __init amd_iommu_uninit_devices(void)
392 struct iommu_dev_data *dev_data, *n;
393 struct pci_dev *pdev = NULL;
395 for_each_pci_dev(pdev) {
397 if (!check_device(&pdev->dev))
400 iommu_uninit_device(&pdev->dev);
403 /* Free all of our dev_data structures */
404 list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
405 free_dev_data(dev_data);
408 int __init amd_iommu_init_devices(void)
410 struct pci_dev *pdev = NULL;
413 for_each_pci_dev(pdev) {
415 if (!check_device(&pdev->dev))
418 ret = iommu_init_device(&pdev->dev);
419 if (ret == -ENOTSUPP)
420 iommu_ignore_device(&pdev->dev);
429 amd_iommu_uninit_devices();
433 #ifdef CONFIG_AMD_IOMMU_STATS
436 * Initialization code for statistics collection
439 DECLARE_STATS_COUNTER(compl_wait);
440 DECLARE_STATS_COUNTER(cnt_map_single);
441 DECLARE_STATS_COUNTER(cnt_unmap_single);
442 DECLARE_STATS_COUNTER(cnt_map_sg);
443 DECLARE_STATS_COUNTER(cnt_unmap_sg);
444 DECLARE_STATS_COUNTER(cnt_alloc_coherent);
445 DECLARE_STATS_COUNTER(cnt_free_coherent);
446 DECLARE_STATS_COUNTER(cross_page);
447 DECLARE_STATS_COUNTER(domain_flush_single);
448 DECLARE_STATS_COUNTER(domain_flush_all);
449 DECLARE_STATS_COUNTER(alloced_io_mem);
450 DECLARE_STATS_COUNTER(total_map_requests);
451 DECLARE_STATS_COUNTER(complete_ppr);
452 DECLARE_STATS_COUNTER(invalidate_iotlb);
453 DECLARE_STATS_COUNTER(invalidate_iotlb_all);
454 DECLARE_STATS_COUNTER(pri_requests);
456 static struct dentry *stats_dir;
457 static struct dentry *de_fflush;
459 static void amd_iommu_stats_add(struct __iommu_counter *cnt)
461 if (stats_dir == NULL)
464 cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
468 static void amd_iommu_stats_init(void)
470 stats_dir = debugfs_create_dir("amd-iommu", NULL);
471 if (stats_dir == NULL)
474 de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
475 &amd_iommu_unmap_flush);
477 amd_iommu_stats_add(&compl_wait);
478 amd_iommu_stats_add(&cnt_map_single);
479 amd_iommu_stats_add(&cnt_unmap_single);
480 amd_iommu_stats_add(&cnt_map_sg);
481 amd_iommu_stats_add(&cnt_unmap_sg);
482 amd_iommu_stats_add(&cnt_alloc_coherent);
483 amd_iommu_stats_add(&cnt_free_coherent);
484 amd_iommu_stats_add(&cross_page);
485 amd_iommu_stats_add(&domain_flush_single);
486 amd_iommu_stats_add(&domain_flush_all);
487 amd_iommu_stats_add(&alloced_io_mem);
488 amd_iommu_stats_add(&total_map_requests);
489 amd_iommu_stats_add(&complete_ppr);
490 amd_iommu_stats_add(&invalidate_iotlb);
491 amd_iommu_stats_add(&invalidate_iotlb_all);
492 amd_iommu_stats_add(&pri_requests);
497 /****************************************************************************
499 * Interrupt handling functions
501 ****************************************************************************/
503 static void dump_dte_entry(u16 devid)
507 for (i = 0; i < 4; ++i)
508 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
509 amd_iommu_dev_table[devid].data[i]);
512 static void dump_command(unsigned long phys_addr)
514 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
517 for (i = 0; i < 4; ++i)
518 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
521 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
523 int type, devid, domid, flags;
524 volatile u32 *event = __evt;
529 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
530 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
531 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
532 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
533 address = (u64)(((u64)event[3]) << 32) | event[2];
536 /* Did we hit the erratum? */
537 if (++count == LOOP_TIMEOUT) {
538 pr_err("AMD-Vi: No event written to event log\n");
545 printk(KERN_ERR "AMD-Vi: Event logged [");
548 case EVENT_TYPE_ILL_DEV:
549 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
550 "address=0x%016llx flags=0x%04x]\n",
551 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
553 dump_dte_entry(devid);
555 case EVENT_TYPE_IO_FAULT:
556 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
557 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
558 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
559 domid, address, flags);
561 case EVENT_TYPE_DEV_TAB_ERR:
562 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
563 "address=0x%016llx flags=0x%04x]\n",
564 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
567 case EVENT_TYPE_PAGE_TAB_ERR:
568 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
569 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
570 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
571 domid, address, flags);
573 case EVENT_TYPE_ILL_CMD:
574 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
575 dump_command(address);
577 case EVENT_TYPE_CMD_HARD_ERR:
578 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
579 "flags=0x%04x]\n", address, flags);
581 case EVENT_TYPE_IOTLB_INV_TO:
582 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
583 "address=0x%016llx]\n",
584 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
587 case EVENT_TYPE_INV_DEV_REQ:
588 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
589 "address=0x%016llx flags=0x%04x]\n",
590 PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
594 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
597 memset(__evt, 0, 4 * sizeof(u32));
600 static void iommu_poll_events(struct amd_iommu *iommu)
605 spin_lock_irqsave(&iommu->lock, flags);
607 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
608 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
610 while (head != tail) {
611 iommu_print_event(iommu, iommu->evt_buf + head);
612 head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
615 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
617 spin_unlock_irqrestore(&iommu->lock, flags);
620 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
622 struct amd_iommu_fault fault;
624 INC_STATS_COUNTER(pri_requests);
626 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
627 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
631 fault.address = raw[1];
632 fault.pasid = PPR_PASID(raw[0]);
633 fault.device_id = PPR_DEVID(raw[0]);
634 fault.tag = PPR_TAG(raw[0]);
635 fault.flags = PPR_FLAGS(raw[0]);
637 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
640 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
645 if (iommu->ppr_log == NULL)
648 /* enable ppr interrupts again */
649 writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
651 spin_lock_irqsave(&iommu->lock, flags);
653 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
654 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
656 while (head != tail) {
661 raw = (u64 *)(iommu->ppr_log + head);
664 * Hardware bug: Interrupt may arrive before the entry is
665 * written to memory. If this happens we need to wait for the
668 for (i = 0; i < LOOP_TIMEOUT; ++i) {
669 if (PPR_REQ_TYPE(raw[0]) != 0)
674 /* Avoid memcpy function-call overhead */
679 * To detect the hardware bug we need to clear the entry
682 raw[0] = raw[1] = 0UL;
684 /* Update head pointer of hardware ring-buffer */
685 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
686 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
689 * Release iommu->lock because ppr-handling might need to
692 spin_unlock_irqrestore(&iommu->lock, flags);
694 /* Handle PPR entry */
695 iommu_handle_ppr_entry(iommu, entry);
697 spin_lock_irqsave(&iommu->lock, flags);
699 /* Refresh ring-buffer information */
700 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
701 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
704 spin_unlock_irqrestore(&iommu->lock, flags);
707 irqreturn_t amd_iommu_int_thread(int irq, void *data)
709 struct amd_iommu *iommu;
711 for_each_iommu(iommu) {
712 iommu_poll_events(iommu);
713 iommu_poll_ppr_log(iommu);
719 irqreturn_t amd_iommu_int_handler(int irq, void *data)
721 return IRQ_WAKE_THREAD;
724 /****************************************************************************
726 * IOMMU command queuing functions
728 ****************************************************************************/
730 static int wait_on_sem(volatile u64 *sem)
734 while (*sem == 0 && i < LOOP_TIMEOUT) {
739 if (i == LOOP_TIMEOUT) {
740 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
747 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
748 struct iommu_cmd *cmd,
753 target = iommu->cmd_buf + tail;
754 tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
756 /* Copy command to buffer */
757 memcpy(target, cmd, sizeof(*cmd));
759 /* Tell the IOMMU about it */
760 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
763 static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
765 WARN_ON(address & 0x7ULL);
767 memset(cmd, 0, sizeof(*cmd));
768 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
769 cmd->data[1] = upper_32_bits(__pa(address));
771 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
774 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
776 memset(cmd, 0, sizeof(*cmd));
777 cmd->data[0] = devid;
778 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
781 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
782 size_t size, u16 domid, int pde)
787 pages = iommu_num_pages(address, size, PAGE_SIZE);
792 * If we have to flush more than one page, flush all
793 * TLB entries for this domain
795 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
799 address &= PAGE_MASK;
801 memset(cmd, 0, sizeof(*cmd));
802 cmd->data[1] |= domid;
803 cmd->data[2] = lower_32_bits(address);
804 cmd->data[3] = upper_32_bits(address);
805 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
806 if (s) /* size bit - we flush more than one 4kb page */
807 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
808 if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
809 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
812 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
813 u64 address, size_t size)
818 pages = iommu_num_pages(address, size, PAGE_SIZE);
823 * If we have to flush more than one page, flush all
824 * TLB entries for this domain
826 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
830 address &= PAGE_MASK;
832 memset(cmd, 0, sizeof(*cmd));
833 cmd->data[0] = devid;
834 cmd->data[0] |= (qdep & 0xff) << 24;
835 cmd->data[1] = devid;
836 cmd->data[2] = lower_32_bits(address);
837 cmd->data[3] = upper_32_bits(address);
838 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
840 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
843 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
844 u64 address, bool size)
846 memset(cmd, 0, sizeof(*cmd));
848 address &= ~(0xfffULL);
850 cmd->data[0] = pasid & PASID_MASK;
851 cmd->data[1] = domid;
852 cmd->data[2] = lower_32_bits(address);
853 cmd->data[3] = upper_32_bits(address);
854 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
855 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
857 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
858 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
861 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
862 int qdep, u64 address, bool size)
864 memset(cmd, 0, sizeof(*cmd));
866 address &= ~(0xfffULL);
868 cmd->data[0] = devid;
869 cmd->data[0] |= (pasid & 0xff) << 16;
870 cmd->data[0] |= (qdep & 0xff) << 24;
871 cmd->data[1] = devid;
872 cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
873 cmd->data[2] = lower_32_bits(address);
874 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
875 cmd->data[3] = upper_32_bits(address);
877 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
878 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
881 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
882 int status, int tag, bool gn)
884 memset(cmd, 0, sizeof(*cmd));
886 cmd->data[0] = devid;
888 cmd->data[1] = pasid & PASID_MASK;
889 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
891 cmd->data[3] = tag & 0x1ff;
892 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
894 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
897 static void build_inv_all(struct iommu_cmd *cmd)
899 memset(cmd, 0, sizeof(*cmd));
900 CMD_SET_TYPE(cmd, CMD_INV_ALL);
904 * Writes the command to the IOMMUs command buffer and informs the
905 * hardware about the new command.
907 static int iommu_queue_command_sync(struct amd_iommu *iommu,
908 struct iommu_cmd *cmd,
911 u32 left, tail, head, next_tail;
914 WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
917 spin_lock_irqsave(&iommu->lock, flags);
919 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
920 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
921 next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
922 left = (head - next_tail) % iommu->cmd_buf_size;
925 struct iommu_cmd sync_cmd;
926 volatile u64 sem = 0;
929 build_completion_wait(&sync_cmd, (u64)&sem);
930 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
932 spin_unlock_irqrestore(&iommu->lock, flags);
934 if ((ret = wait_on_sem(&sem)) != 0)
940 copy_cmd_to_buffer(iommu, cmd, tail);
942 /* We need to sync now to make sure all commands are processed */
943 iommu->need_sync = sync;
945 spin_unlock_irqrestore(&iommu->lock, flags);
950 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
952 return iommu_queue_command_sync(iommu, cmd, true);
956 * This function queues a completion wait command into the command
959 static int iommu_completion_wait(struct amd_iommu *iommu)
961 struct iommu_cmd cmd;
962 volatile u64 sem = 0;
965 if (!iommu->need_sync)
968 build_completion_wait(&cmd, (u64)&sem);
970 ret = iommu_queue_command_sync(iommu, &cmd, false);
974 return wait_on_sem(&sem);
977 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
979 struct iommu_cmd cmd;
981 build_inv_dte(&cmd, devid);
983 return iommu_queue_command(iommu, &cmd);
986 static void iommu_flush_dte_all(struct amd_iommu *iommu)
990 for (devid = 0; devid <= 0xffff; ++devid)
991 iommu_flush_dte(iommu, devid);
993 iommu_completion_wait(iommu);
997 * This function uses heavy locking and may disable irqs for some time. But
998 * this is no issue because it is only called during resume.
1000 static void iommu_flush_tlb_all(struct amd_iommu *iommu)
1004 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1005 struct iommu_cmd cmd;
1006 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1008 iommu_queue_command(iommu, &cmd);
1011 iommu_completion_wait(iommu);
1014 static void iommu_flush_all(struct amd_iommu *iommu)
1016 struct iommu_cmd cmd;
1018 build_inv_all(&cmd);
1020 iommu_queue_command(iommu, &cmd);
1021 iommu_completion_wait(iommu);
1024 void iommu_flush_all_caches(struct amd_iommu *iommu)
1026 if (iommu_feature(iommu, FEATURE_IA)) {
1027 iommu_flush_all(iommu);
1029 iommu_flush_dte_all(iommu);
1030 iommu_flush_tlb_all(iommu);
1035 * Command send function for flushing on-device TLB
1037 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1038 u64 address, size_t size)
1040 struct amd_iommu *iommu;
1041 struct iommu_cmd cmd;
1044 qdep = dev_data->ats.qdep;
1045 iommu = amd_iommu_rlookup_table[dev_data->devid];
1047 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1049 return iommu_queue_command(iommu, &cmd);
1053 * Command send function for invalidating a device table entry
1055 static int device_flush_dte(struct iommu_dev_data *dev_data)
1057 struct amd_iommu *iommu;
1060 iommu = amd_iommu_rlookup_table[dev_data->devid];
1062 ret = iommu_flush_dte(iommu, dev_data->devid);
1066 if (dev_data->ats.enabled)
1067 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1073 * TLB invalidation function which is called from the mapping functions.
1074 * It invalidates a single PTE if the range to flush is within a single
1075 * page. Otherwise it flushes the whole TLB of the IOMMU.
1077 static void __domain_flush_pages(struct protection_domain *domain,
1078 u64 address, size_t size, int pde)
1080 struct iommu_dev_data *dev_data;
1081 struct iommu_cmd cmd;
1084 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1086 for (i = 0; i < amd_iommus_present; ++i) {
1087 if (!domain->dev_iommu[i])
1091 * Devices of this domain are behind this IOMMU
1092 * We need a TLB flush
1094 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1097 list_for_each_entry(dev_data, &domain->dev_list, list) {
1099 if (!dev_data->ats.enabled)
1102 ret |= device_flush_iotlb(dev_data, address, size);
1108 static void domain_flush_pages(struct protection_domain *domain,
1109 u64 address, size_t size)
1111 __domain_flush_pages(domain, address, size, 0);
1114 /* Flush the whole IO/TLB for a given protection domain */
1115 static void domain_flush_tlb(struct protection_domain *domain)
1117 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
1120 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1121 static void domain_flush_tlb_pde(struct protection_domain *domain)
1123 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1126 static void domain_flush_complete(struct protection_domain *domain)
1130 for (i = 0; i < amd_iommus_present; ++i) {
1131 if (!domain->dev_iommu[i])
1135 * Devices of this domain are behind this IOMMU
1136 * We need to wait for completion of all commands.
1138 iommu_completion_wait(amd_iommus[i]);
1144 * This function flushes the DTEs for all devices in domain
1146 static void domain_flush_devices(struct protection_domain *domain)
1148 struct iommu_dev_data *dev_data;
1150 list_for_each_entry(dev_data, &domain->dev_list, list)
1151 device_flush_dte(dev_data);
1154 /****************************************************************************
1156 * The functions below are used the create the page table mappings for
1157 * unity mapped regions.
1159 ****************************************************************************/
1162 * This function is used to add another level to an IO page table. Adding
1163 * another level increases the size of the address space by 9 bits to a size up
1166 static bool increase_address_space(struct protection_domain *domain,
1171 if (domain->mode == PAGE_MODE_6_LEVEL)
1172 /* address space already 64 bit large */
1175 pte = (void *)get_zeroed_page(gfp);
1179 *pte = PM_LEVEL_PDE(domain->mode,
1180 virt_to_phys(domain->pt_root));
1181 domain->pt_root = pte;
1183 domain->updated = true;
1188 static u64 *alloc_pte(struct protection_domain *domain,
1189 unsigned long address,
1190 unsigned long page_size,
1197 BUG_ON(!is_power_of_2(page_size));
1199 while (address > PM_LEVEL_SIZE(domain->mode))
1200 increase_address_space(domain, gfp);
1202 level = domain->mode - 1;
1203 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1204 address = PAGE_SIZE_ALIGN(address, page_size);
1205 end_lvl = PAGE_SIZE_LEVEL(page_size);
1207 while (level > end_lvl) {
1208 if (!IOMMU_PTE_PRESENT(*pte)) {
1209 page = (u64 *)get_zeroed_page(gfp);
1212 *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
1215 /* No level skipping support yet */
1216 if (PM_PTE_LEVEL(*pte) != level)
1221 pte = IOMMU_PTE_PAGE(*pte);
1223 if (pte_page && level == end_lvl)
1226 pte = &pte[PM_LEVEL_INDEX(level, address)];
1233 * This function checks if there is a PTE for a given dma address. If
1234 * there is one, it returns the pointer to it.
1236 static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
1241 if (address > PM_LEVEL_SIZE(domain->mode))
1244 level = domain->mode - 1;
1245 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1250 if (!IOMMU_PTE_PRESENT(*pte))
1254 if (PM_PTE_LEVEL(*pte) == 0x07) {
1255 unsigned long pte_mask, __pte;
1258 * If we have a series of large PTEs, make
1259 * sure to return a pointer to the first one.
1261 pte_mask = PTE_PAGE_SIZE(*pte);
1262 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1263 __pte = ((unsigned long)pte) & pte_mask;
1265 return (u64 *)__pte;
1268 /* No level skipping support yet */
1269 if (PM_PTE_LEVEL(*pte) != level)
1274 /* Walk to the next level */
1275 pte = IOMMU_PTE_PAGE(*pte);
1276 pte = &pte[PM_LEVEL_INDEX(level, address)];
1283 * Generic mapping functions. It maps a physical address into a DMA
1284 * address space. It allocates the page table pages if necessary.
1285 * In the future it can be extended to a generic mapping function
1286 * supporting all features of AMD IOMMU page tables like level skipping
1287 * and full 64 bit address spaces.
1289 static int iommu_map_page(struct protection_domain *dom,
1290 unsigned long bus_addr,
1291 unsigned long phys_addr,
1293 unsigned long page_size)
1298 if (!(prot & IOMMU_PROT_MASK))
1301 bus_addr = PAGE_ALIGN(bus_addr);
1302 phys_addr = PAGE_ALIGN(phys_addr);
1303 count = PAGE_SIZE_PTE_COUNT(page_size);
1304 pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
1306 for (i = 0; i < count; ++i)
1307 if (IOMMU_PTE_PRESENT(pte[i]))
1310 if (page_size > PAGE_SIZE) {
1311 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1312 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1314 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1316 if (prot & IOMMU_PROT_IR)
1317 __pte |= IOMMU_PTE_IR;
1318 if (prot & IOMMU_PROT_IW)
1319 __pte |= IOMMU_PTE_IW;
1321 for (i = 0; i < count; ++i)
1329 static unsigned long iommu_unmap_page(struct protection_domain *dom,
1330 unsigned long bus_addr,
1331 unsigned long page_size)
1333 unsigned long long unmap_size, unmapped;
1336 BUG_ON(!is_power_of_2(page_size));
1340 while (unmapped < page_size) {
1342 pte = fetch_pte(dom, bus_addr);
1346 * No PTE for this address
1347 * move forward in 4kb steps
1349 unmap_size = PAGE_SIZE;
1350 } else if (PM_PTE_LEVEL(*pte) == 0) {
1351 /* 4kb PTE found for this address */
1352 unmap_size = PAGE_SIZE;
1357 /* Large PTE found which maps this address */
1358 unmap_size = PTE_PAGE_SIZE(*pte);
1359 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1360 for (i = 0; i < count; i++)
1364 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1365 unmapped += unmap_size;
1368 BUG_ON(!is_power_of_2(unmapped));
1374 * This function checks if a specific unity mapping entry is needed for
1375 * this specific IOMMU.
1377 static int iommu_for_unity_map(struct amd_iommu *iommu,
1378 struct unity_map_entry *entry)
1382 for (i = entry->devid_start; i <= entry->devid_end; ++i) {
1383 bdf = amd_iommu_alias_table[i];
1384 if (amd_iommu_rlookup_table[bdf] == iommu)
1392 * This function actually applies the mapping to the page table of the
1395 static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
1396 struct unity_map_entry *e)
1401 for (addr = e->address_start; addr < e->address_end;
1402 addr += PAGE_SIZE) {
1403 ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
1408 * if unity mapping is in aperture range mark the page
1409 * as allocated in the aperture
1411 if (addr < dma_dom->aperture_size)
1412 __set_bit(addr >> PAGE_SHIFT,
1413 dma_dom->aperture[0]->bitmap);
1420 * Init the unity mappings for a specific IOMMU in the system
1422 * Basically iterates over all unity mapping entries and applies them to
1423 * the default domain DMA of that IOMMU if necessary.
1425 static int iommu_init_unity_mappings(struct amd_iommu *iommu)
1427 struct unity_map_entry *entry;
1430 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
1431 if (!iommu_for_unity_map(iommu, entry))
1433 ret = dma_ops_unity_map(iommu->default_dom, entry);
1442 * Inits the unity mappings required for a specific device
1444 static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
1447 struct unity_map_entry *e;
1450 list_for_each_entry(e, &amd_iommu_unity_map, list) {
1451 if (!(devid >= e->devid_start && devid <= e->devid_end))
1453 ret = dma_ops_unity_map(dma_dom, e);
1461 /****************************************************************************
1463 * The next functions belong to the address allocator for the dma_ops
1464 * interface functions. They work like the allocators in the other IOMMU
1465 * drivers. Its basically a bitmap which marks the allocated pages in
1466 * the aperture. Maybe it could be enhanced in the future to a more
1467 * efficient allocator.
1469 ****************************************************************************/
1472 * The address allocator core functions.
1474 * called with domain->lock held
1478 * Used to reserve address ranges in the aperture (e.g. for exclusion
1481 static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
1482 unsigned long start_page,
1485 unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
1487 if (start_page + pages > last_page)
1488 pages = last_page - start_page;
1490 for (i = start_page; i < start_page + pages; ++i) {
1491 int index = i / APERTURE_RANGE_PAGES;
1492 int page = i % APERTURE_RANGE_PAGES;
1493 __set_bit(page, dom->aperture[index]->bitmap);
1498 * This function is used to add a new aperture range to an existing
1499 * aperture in case of dma_ops domain allocation or address allocation
1502 static int alloc_new_range(struct dma_ops_domain *dma_dom,
1503 bool populate, gfp_t gfp)
1505 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
1506 struct amd_iommu *iommu;
1507 unsigned long i, old_size;
1509 #ifdef CONFIG_IOMMU_STRESS
1513 if (index >= APERTURE_MAX_RANGES)
1516 dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
1517 if (!dma_dom->aperture[index])
1520 dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
1521 if (!dma_dom->aperture[index]->bitmap)
1524 dma_dom->aperture[index]->offset = dma_dom->aperture_size;
1527 unsigned long address = dma_dom->aperture_size;
1528 int i, num_ptes = APERTURE_RANGE_PAGES / 512;
1529 u64 *pte, *pte_page;
1531 for (i = 0; i < num_ptes; ++i) {
1532 pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
1537 dma_dom->aperture[index]->pte_pages[i] = pte_page;
1539 address += APERTURE_RANGE_SIZE / 64;
1543 old_size = dma_dom->aperture_size;
1544 dma_dom->aperture_size += APERTURE_RANGE_SIZE;
1546 /* Reserve address range used for MSI messages */
1547 if (old_size < MSI_ADDR_BASE_LO &&
1548 dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
1549 unsigned long spage;
1552 pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
1553 spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
1555 dma_ops_reserve_addresses(dma_dom, spage, pages);
1558 /* Initialize the exclusion range if necessary */
1559 for_each_iommu(iommu) {
1560 if (iommu->exclusion_start &&
1561 iommu->exclusion_start >= dma_dom->aperture[index]->offset
1562 && iommu->exclusion_start < dma_dom->aperture_size) {
1563 unsigned long startpage;
1564 int pages = iommu_num_pages(iommu->exclusion_start,
1565 iommu->exclusion_length,
1567 startpage = iommu->exclusion_start >> PAGE_SHIFT;
1568 dma_ops_reserve_addresses(dma_dom, startpage, pages);
1573 * Check for areas already mapped as present in the new aperture
1574 * range and mark those pages as reserved in the allocator. Such
1575 * mappings may already exist as a result of requested unity
1576 * mappings for devices.
1578 for (i = dma_dom->aperture[index]->offset;
1579 i < dma_dom->aperture_size;
1581 u64 *pte = fetch_pte(&dma_dom->domain, i);
1582 if (!pte || !IOMMU_PTE_PRESENT(*pte))
1585 dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
1588 update_domain(&dma_dom->domain);
1593 update_domain(&dma_dom->domain);
1595 free_page((unsigned long)dma_dom->aperture[index]->bitmap);
1597 kfree(dma_dom->aperture[index]);
1598 dma_dom->aperture[index] = NULL;
1603 static unsigned long dma_ops_area_alloc(struct device *dev,
1604 struct dma_ops_domain *dom,
1606 unsigned long align_mask,
1608 unsigned long start)
1610 unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
1611 int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
1612 int i = start >> APERTURE_RANGE_SHIFT;
1613 unsigned long boundary_size;
1614 unsigned long address = -1;
1615 unsigned long limit;
1617 next_bit >>= PAGE_SHIFT;
1619 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
1620 PAGE_SIZE) >> PAGE_SHIFT;
1622 for (;i < max_index; ++i) {
1623 unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
1625 if (dom->aperture[i]->offset >= dma_mask)
1628 limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
1629 dma_mask >> PAGE_SHIFT);
1631 address = iommu_area_alloc(dom->aperture[i]->bitmap,
1632 limit, next_bit, pages, 0,
1633 boundary_size, align_mask);
1634 if (address != -1) {
1635 address = dom->aperture[i]->offset +
1636 (address << PAGE_SHIFT);
1637 dom->next_address = address + (pages << PAGE_SHIFT);
1647 static unsigned long dma_ops_alloc_addresses(struct device *dev,
1648 struct dma_ops_domain *dom,
1650 unsigned long align_mask,
1653 unsigned long address;
1655 #ifdef CONFIG_IOMMU_STRESS
1656 dom->next_address = 0;
1657 dom->need_flush = true;
1660 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1661 dma_mask, dom->next_address);
1663 if (address == -1) {
1664 dom->next_address = 0;
1665 address = dma_ops_area_alloc(dev, dom, pages, align_mask,
1667 dom->need_flush = true;
1670 if (unlikely(address == -1))
1671 address = DMA_ERROR_CODE;
1673 WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
1679 * The address free function.
1681 * called with domain->lock held
1683 static void dma_ops_free_addresses(struct dma_ops_domain *dom,
1684 unsigned long address,
1687 unsigned i = address >> APERTURE_RANGE_SHIFT;
1688 struct aperture_range *range = dom->aperture[i];
1690 BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
1692 #ifdef CONFIG_IOMMU_STRESS
1697 if (address >= dom->next_address)
1698 dom->need_flush = true;
1700 address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
1702 bitmap_clear(range->bitmap, address, pages);
1706 /****************************************************************************
1708 * The next functions belong to the domain allocation. A domain is
1709 * allocated for every IOMMU as the default domain. If device isolation
1710 * is enabled, every device get its own domain. The most important thing
1711 * about domains is the page table mapping the DMA address space they
1714 ****************************************************************************/
1717 * This function adds a protection domain to the global protection domain list
1719 static void add_domain_to_list(struct protection_domain *domain)
1721 unsigned long flags;
1723 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1724 list_add(&domain->list, &amd_iommu_pd_list);
1725 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1729 * This function removes a protection domain to the global
1730 * protection domain list
1732 static void del_domain_from_list(struct protection_domain *domain)
1734 unsigned long flags;
1736 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1737 list_del(&domain->list);
1738 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1741 static u16 domain_id_alloc(void)
1743 unsigned long flags;
1746 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1747 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1749 if (id > 0 && id < MAX_DOMAIN_ID)
1750 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1753 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1758 static void domain_id_free(int id)
1760 unsigned long flags;
1762 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1763 if (id > 0 && id < MAX_DOMAIN_ID)
1764 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1765 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1768 static void free_pagetable(struct protection_domain *domain)
1773 p1 = domain->pt_root;
1778 for (i = 0; i < 512; ++i) {
1779 if (!IOMMU_PTE_PRESENT(p1[i]))
1782 p2 = IOMMU_PTE_PAGE(p1[i]);
1783 for (j = 0; j < 512; ++j) {
1784 if (!IOMMU_PTE_PRESENT(p2[j]))
1786 p3 = IOMMU_PTE_PAGE(p2[j]);
1787 free_page((unsigned long)p3);
1790 free_page((unsigned long)p2);
1793 free_page((unsigned long)p1);
1795 domain->pt_root = NULL;
1798 static void free_gcr3_tbl_level1(u64 *tbl)
1803 for (i = 0; i < 512; ++i) {
1804 if (!(tbl[i] & GCR3_VALID))
1807 ptr = __va(tbl[i] & PAGE_MASK);
1809 free_page((unsigned long)ptr);
1813 static void free_gcr3_tbl_level2(u64 *tbl)
1818 for (i = 0; i < 512; ++i) {
1819 if (!(tbl[i] & GCR3_VALID))
1822 ptr = __va(tbl[i] & PAGE_MASK);
1824 free_gcr3_tbl_level1(ptr);
1828 static void free_gcr3_table(struct protection_domain *domain)
1830 if (domain->glx == 2)
1831 free_gcr3_tbl_level2(domain->gcr3_tbl);
1832 else if (domain->glx == 1)
1833 free_gcr3_tbl_level1(domain->gcr3_tbl);
1834 else if (domain->glx != 0)
1837 free_page((unsigned long)domain->gcr3_tbl);
1841 * Free a domain, only used if something went wrong in the
1842 * allocation path and we need to free an already allocated page table
1844 static void dma_ops_domain_free(struct dma_ops_domain *dom)
1851 del_domain_from_list(&dom->domain);
1853 free_pagetable(&dom->domain);
1855 for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
1856 if (!dom->aperture[i])
1858 free_page((unsigned long)dom->aperture[i]->bitmap);
1859 kfree(dom->aperture[i]);
1866 * Allocates a new protection domain usable for the dma_ops functions.
1867 * It also initializes the page table and the address allocator data
1868 * structures required for the dma_ops interface
1870 static struct dma_ops_domain *dma_ops_domain_alloc(void)
1872 struct dma_ops_domain *dma_dom;
1874 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1878 spin_lock_init(&dma_dom->domain.lock);
1880 dma_dom->domain.id = domain_id_alloc();
1881 if (dma_dom->domain.id == 0)
1883 INIT_LIST_HEAD(&dma_dom->domain.dev_list);
1884 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
1885 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
1886 dma_dom->domain.flags = PD_DMA_OPS_MASK;
1887 dma_dom->domain.priv = dma_dom;
1888 if (!dma_dom->domain.pt_root)
1891 dma_dom->need_flush = false;
1892 dma_dom->target_dev = 0xffff;
1894 add_domain_to_list(&dma_dom->domain);
1896 if (alloc_new_range(dma_dom, true, GFP_KERNEL))
1900 * mark the first page as allocated so we never return 0 as
1901 * a valid dma-address. So we can use 0 as error value
1903 dma_dom->aperture[0]->bitmap[0] = 1;
1904 dma_dom->next_address = 0;
1910 dma_ops_domain_free(dma_dom);
1916 * little helper function to check whether a given protection domain is a
1919 static bool dma_ops_domain(struct protection_domain *domain)
1921 return domain->flags & PD_DMA_OPS_MASK;
1924 static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
1929 if (domain->mode != PAGE_MODE_NONE)
1930 pte_root = virt_to_phys(domain->pt_root);
1932 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1933 << DEV_ENTRY_MODE_SHIFT;
1934 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
1936 flags = amd_iommu_dev_table[devid].data[1];
1939 flags |= DTE_FLAG_IOTLB;
1941 if (domain->flags & PD_IOMMUV2_MASK) {
1942 u64 gcr3 = __pa(domain->gcr3_tbl);
1943 u64 glx = domain->glx;
1946 pte_root |= DTE_FLAG_GV;
1947 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1949 /* First mask out possible old values for GCR3 table */
1950 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1953 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1956 /* Encode GCR3 table into DTE */
1957 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1960 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1963 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1967 flags &= ~(0xffffUL);
1968 flags |= domain->id;
1970 amd_iommu_dev_table[devid].data[1] = flags;
1971 amd_iommu_dev_table[devid].data[0] = pte_root;
1974 static void clear_dte_entry(u16 devid)
1976 /* remove entry from the device table seen by the hardware */
1977 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1978 amd_iommu_dev_table[devid].data[1] = 0;
1980 amd_iommu_apply_erratum_63(devid);
1983 static void do_attach(struct iommu_dev_data *dev_data,
1984 struct protection_domain *domain)
1986 struct amd_iommu *iommu;
1989 iommu = amd_iommu_rlookup_table[dev_data->devid];
1990 ats = dev_data->ats.enabled;
1992 /* Update data structures */
1993 dev_data->domain = domain;
1994 list_add(&dev_data->list, &domain->dev_list);
1995 set_dte_entry(dev_data->devid, domain, ats);
1997 /* Do reference counting */
1998 domain->dev_iommu[iommu->index] += 1;
1999 domain->dev_cnt += 1;
2001 /* Flush the DTE entry */
2002 device_flush_dte(dev_data);
2005 static void do_detach(struct iommu_dev_data *dev_data)
2007 struct amd_iommu *iommu;
2009 iommu = amd_iommu_rlookup_table[dev_data->devid];
2011 /* decrease reference counters */
2012 dev_data->domain->dev_iommu[iommu->index] -= 1;
2013 dev_data->domain->dev_cnt -= 1;
2015 /* Update data structures */
2016 dev_data->domain = NULL;
2017 list_del(&dev_data->list);
2018 clear_dte_entry(dev_data->devid);
2020 /* Flush the DTE entry */
2021 device_flush_dte(dev_data);
2025 * If a device is not yet associated with a domain, this function does
2026 * assigns it visible for the hardware
2028 static int __attach_device(struct iommu_dev_data *dev_data,
2029 struct protection_domain *domain)
2034 spin_lock(&domain->lock);
2036 if (dev_data->alias_data != NULL) {
2037 struct iommu_dev_data *alias_data = dev_data->alias_data;
2039 /* Some sanity checks */
2041 if (alias_data->domain != NULL &&
2042 alias_data->domain != domain)
2045 if (dev_data->domain != NULL &&
2046 dev_data->domain != domain)
2049 /* Do real assignment */
2050 if (alias_data->domain == NULL)
2051 do_attach(alias_data, domain);
2053 atomic_inc(&alias_data->bind);
2056 if (dev_data->domain == NULL)
2057 do_attach(dev_data, domain);
2059 atomic_inc(&dev_data->bind);
2066 spin_unlock(&domain->lock);
2072 static void pdev_iommuv2_disable(struct pci_dev *pdev)
2074 pci_disable_ats(pdev);
2075 pci_disable_pri(pdev);
2076 pci_disable_pasid(pdev);
2079 /* FIXME: Change generic reset-function to do the same */
2080 static int pri_reset_while_enabled(struct pci_dev *pdev)
2085 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
2089 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
2090 control |= PCI_PRI_CTRL_RESET;
2091 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
2096 static int pdev_iommuv2_enable(struct pci_dev *pdev)
2101 /* FIXME: Hardcode number of outstanding requests for now */
2103 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2105 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
2107 /* Only allow access to user-accessible pages */
2108 ret = pci_enable_pasid(pdev, 0);
2112 /* First reset the PRI state of the device */
2113 ret = pci_reset_pri(pdev);
2118 ret = pci_enable_pri(pdev, reqs);
2123 ret = pri_reset_while_enabled(pdev);
2128 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2135 pci_disable_pri(pdev);
2136 pci_disable_pasid(pdev);
2141 /* FIXME: Move this to PCI code */
2142 #define PCI_PRI_TLP_OFF (1 << 15)
2144 static bool pci_pri_tlp_required(struct pci_dev *pdev)
2149 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
2153 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
2155 return (status & PCI_PRI_TLP_OFF) ? true : false;
2159 * If a device is not yet associated with a domain, this function does
2160 * assigns it visible for the hardware
2162 static int attach_device(struct device *dev,
2163 struct protection_domain *domain)
2165 struct pci_dev *pdev = to_pci_dev(dev);
2166 struct iommu_dev_data *dev_data;
2167 unsigned long flags;
2170 dev_data = get_dev_data(dev);
2172 if (domain->flags & PD_IOMMUV2_MASK) {
2173 if (!dev_data->iommu_v2 || !dev_data->passthrough)
2176 if (pdev_iommuv2_enable(pdev) != 0)
2179 dev_data->ats.enabled = true;
2180 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2181 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2182 } else if (amd_iommu_iotlb_sup &&
2183 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
2184 dev_data->ats.enabled = true;
2185 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2188 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2189 ret = __attach_device(dev_data, domain);
2190 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2193 * We might boot into a crash-kernel here. The crashed kernel
2194 * left the caches in the IOMMU dirty. So we have to flush
2195 * here to evict all dirty stuff.
2197 domain_flush_tlb_pde(domain);
2203 * Removes a device from a protection domain (unlocked)
2205 static void __detach_device(struct iommu_dev_data *dev_data)
2207 struct protection_domain *domain;
2208 unsigned long flags;
2210 BUG_ON(!dev_data->domain);
2212 domain = dev_data->domain;
2214 spin_lock_irqsave(&domain->lock, flags);
2216 if (dev_data->alias_data != NULL) {
2217 struct iommu_dev_data *alias_data = dev_data->alias_data;
2219 if (atomic_dec_and_test(&alias_data->bind))
2220 do_detach(alias_data);
2223 if (atomic_dec_and_test(&dev_data->bind))
2224 do_detach(dev_data);
2226 spin_unlock_irqrestore(&domain->lock, flags);
2229 * If we run in passthrough mode the device must be assigned to the
2230 * passthrough domain if it is detached from any other domain.
2231 * Make sure we can deassign from the pt_domain itself.
2233 if (dev_data->passthrough &&
2234 (dev_data->domain == NULL && domain != pt_domain))
2235 __attach_device(dev_data, pt_domain);
2239 * Removes a device from a protection domain (with devtable_lock held)
2241 static void detach_device(struct device *dev)
2243 struct protection_domain *domain;
2244 struct iommu_dev_data *dev_data;
2245 unsigned long flags;
2247 dev_data = get_dev_data(dev);
2248 domain = dev_data->domain;
2250 /* lock device table */
2251 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2252 __detach_device(dev_data);
2253 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2255 if (domain->flags & PD_IOMMUV2_MASK)
2256 pdev_iommuv2_disable(to_pci_dev(dev));
2257 else if (dev_data->ats.enabled)
2258 pci_disable_ats(to_pci_dev(dev));
2260 dev_data->ats.enabled = false;
2264 * Find out the protection domain structure for a given PCI device. This
2265 * will give us the pointer to the page table root for example.
2267 static struct protection_domain *domain_for_device(struct device *dev)
2269 struct iommu_dev_data *dev_data;
2270 struct protection_domain *dom = NULL;
2271 unsigned long flags;
2273 dev_data = get_dev_data(dev);
2275 if (dev_data->domain)
2276 return dev_data->domain;
2278 if (dev_data->alias_data != NULL) {
2279 struct iommu_dev_data *alias_data = dev_data->alias_data;
2281 read_lock_irqsave(&amd_iommu_devtable_lock, flags);
2282 if (alias_data->domain != NULL) {
2283 __attach_device(dev_data, alias_data->domain);
2284 dom = alias_data->domain;
2286 read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2292 static int device_change_notifier(struct notifier_block *nb,
2293 unsigned long action, void *data)
2295 struct dma_ops_domain *dma_domain;
2296 struct protection_domain *domain;
2297 struct iommu_dev_data *dev_data;
2298 struct device *dev = data;
2299 struct amd_iommu *iommu;
2300 unsigned long flags;
2303 if (!check_device(dev))
2306 devid = get_device_id(dev);
2307 iommu = amd_iommu_rlookup_table[devid];
2308 dev_data = get_dev_data(dev);
2311 case BUS_NOTIFY_UNBOUND_DRIVER:
2313 domain = domain_for_device(dev);
2317 if (dev_data->passthrough)
2321 case BUS_NOTIFY_ADD_DEVICE:
2323 iommu_init_device(dev);
2326 * dev_data is still NULL and
2327 * got initialized in iommu_init_device
2329 dev_data = get_dev_data(dev);
2331 if (iommu_pass_through || dev_data->iommu_v2) {
2332 dev_data->passthrough = true;
2333 attach_device(dev, pt_domain);
2337 domain = domain_for_device(dev);
2339 /* allocate a protection domain if a device is added */
2340 dma_domain = find_protection_domain(devid);
2343 dma_domain = dma_ops_domain_alloc();
2346 dma_domain->target_dev = devid;
2348 spin_lock_irqsave(&iommu_pd_list_lock, flags);
2349 list_add_tail(&dma_domain->list, &iommu_pd_list);
2350 spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
2352 dev_data = get_dev_data(dev);
2354 dev->archdata.dma_ops = &amd_iommu_dma_ops;
2357 case BUS_NOTIFY_DEL_DEVICE:
2359 iommu_uninit_device(dev);
2365 iommu_completion_wait(iommu);
2371 static struct notifier_block device_nb = {
2372 .notifier_call = device_change_notifier,
2375 void amd_iommu_init_notifier(void)
2377 bus_register_notifier(&pci_bus_type, &device_nb);
2380 /*****************************************************************************
2382 * The next functions belong to the dma_ops mapping/unmapping code.
2384 *****************************************************************************/
2387 * In the dma_ops path we only have the struct device. This function
2388 * finds the corresponding IOMMU, the protection domain and the
2389 * requestor id for a given device.
2390 * If the device is not yet associated with a domain this is also done
2393 static struct protection_domain *get_domain(struct device *dev)
2395 struct protection_domain *domain;
2396 struct dma_ops_domain *dma_dom;
2397 u16 devid = get_device_id(dev);
2399 if (!check_device(dev))
2400 return ERR_PTR(-EINVAL);
2402 domain = domain_for_device(dev);
2403 if (domain != NULL && !dma_ops_domain(domain))
2404 return ERR_PTR(-EBUSY);
2409 /* Device not bount yet - bind it */
2410 dma_dom = find_protection_domain(devid);
2412 dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
2413 attach_device(dev, &dma_dom->domain);
2414 DUMP_printk("Using protection domain %d for device %s\n",
2415 dma_dom->domain.id, dev_name(dev));
2417 return &dma_dom->domain;
2420 static void update_device_table(struct protection_domain *domain)
2422 struct iommu_dev_data *dev_data;
2424 list_for_each_entry(dev_data, &domain->dev_list, list)
2425 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
2428 static void update_domain(struct protection_domain *domain)
2430 if (!domain->updated)
2433 update_device_table(domain);
2435 domain_flush_devices(domain);
2436 domain_flush_tlb_pde(domain);
2438 domain->updated = false;
2442 * This function fetches the PTE for a given address in the aperture
2444 static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
2445 unsigned long address)
2447 struct aperture_range *aperture;
2448 u64 *pte, *pte_page;
2450 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2454 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2456 pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
2458 aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
2460 pte += PM_LEVEL_INDEX(0, address);
2462 update_domain(&dom->domain);
2468 * This is the generic map function. It maps one 4kb page at paddr to
2469 * the given address in the DMA address space for the domain.
2471 static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
2472 unsigned long address,
2478 WARN_ON(address > dom->aperture_size);
2482 pte = dma_ops_get_pte(dom, address);
2484 return DMA_ERROR_CODE;
2486 __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
2488 if (direction == DMA_TO_DEVICE)
2489 __pte |= IOMMU_PTE_IR;
2490 else if (direction == DMA_FROM_DEVICE)
2491 __pte |= IOMMU_PTE_IW;
2492 else if (direction == DMA_BIDIRECTIONAL)
2493 __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
2499 return (dma_addr_t)address;
2503 * The generic unmapping function for on page in the DMA address space.
2505 static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
2506 unsigned long address)
2508 struct aperture_range *aperture;
2511 if (address >= dom->aperture_size)
2514 aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
2518 pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
2522 pte += PM_LEVEL_INDEX(0, address);
2530 * This function contains common code for mapping of a physically
2531 * contiguous memory region into DMA address space. It is used by all
2532 * mapping functions provided with this IOMMU driver.
2533 * Must be called with the domain lock held.
2535 static dma_addr_t __map_single(struct device *dev,
2536 struct dma_ops_domain *dma_dom,
2543 dma_addr_t offset = paddr & ~PAGE_MASK;
2544 dma_addr_t address, start, ret;
2546 unsigned long align_mask = 0;
2549 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
2552 INC_STATS_COUNTER(total_map_requests);
2555 INC_STATS_COUNTER(cross_page);
2558 align_mask = (1UL << get_order(size)) - 1;
2561 address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
2563 if (unlikely(address == DMA_ERROR_CODE)) {
2565 * setting next_address here will let the address
2566 * allocator only scan the new allocated range in the
2567 * first run. This is a small optimization.
2569 dma_dom->next_address = dma_dom->aperture_size;
2571 if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
2575 * aperture was successfully enlarged by 128 MB, try
2582 for (i = 0; i < pages; ++i) {
2583 ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
2584 if (ret == DMA_ERROR_CODE)
2592 ADD_STATS_COUNTER(alloced_io_mem, size);
2594 if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
2595 domain_flush_tlb(&dma_dom->domain);
2596 dma_dom->need_flush = false;
2597 } else if (unlikely(amd_iommu_np_cache))
2598 domain_flush_pages(&dma_dom->domain, address, size);
2605 for (--i; i >= 0; --i) {
2607 dma_ops_domain_unmap(dma_dom, start);
2610 dma_ops_free_addresses(dma_dom, address, pages);
2612 return DMA_ERROR_CODE;
2616 * Does the reverse of the __map_single function. Must be called with
2617 * the domain lock held too
2619 static void __unmap_single(struct dma_ops_domain *dma_dom,
2620 dma_addr_t dma_addr,
2624 dma_addr_t flush_addr;
2625 dma_addr_t i, start;
2628 if ((dma_addr == DMA_ERROR_CODE) ||
2629 (dma_addr + size > dma_dom->aperture_size))
2632 flush_addr = dma_addr;
2633 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
2634 dma_addr &= PAGE_MASK;
2637 for (i = 0; i < pages; ++i) {
2638 dma_ops_domain_unmap(dma_dom, start);
2642 SUB_STATS_COUNTER(alloced_io_mem, size);
2644 dma_ops_free_addresses(dma_dom, dma_addr, pages);
2646 if (amd_iommu_unmap_flush || dma_dom->need_flush) {
2647 domain_flush_pages(&dma_dom->domain, flush_addr, size);
2648 dma_dom->need_flush = false;
2653 * The exported map_single function for dma_ops.
2655 static dma_addr_t map_page(struct device *dev, struct page *page,
2656 unsigned long offset, size_t size,
2657 enum dma_data_direction dir,
2658 struct dma_attrs *attrs)
2660 unsigned long flags;
2661 struct protection_domain *domain;
2664 phys_addr_t paddr = page_to_phys(page) + offset;
2666 INC_STATS_COUNTER(cnt_map_single);
2668 domain = get_domain(dev);
2669 if (PTR_ERR(domain) == -EINVAL)
2670 return (dma_addr_t)paddr;
2671 else if (IS_ERR(domain))
2672 return DMA_ERROR_CODE;
2674 dma_mask = *dev->dma_mask;
2676 spin_lock_irqsave(&domain->lock, flags);
2678 addr = __map_single(dev, domain->priv, paddr, size, dir, false,
2680 if (addr == DMA_ERROR_CODE)
2683 domain_flush_complete(domain);
2686 spin_unlock_irqrestore(&domain->lock, flags);
2692 * The exported unmap_single function for dma_ops.
2694 static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2695 enum dma_data_direction dir, struct dma_attrs *attrs)
2697 unsigned long flags;
2698 struct protection_domain *domain;
2700 INC_STATS_COUNTER(cnt_unmap_single);
2702 domain = get_domain(dev);
2706 spin_lock_irqsave(&domain->lock, flags);
2708 __unmap_single(domain->priv, dma_addr, size, dir);
2710 domain_flush_complete(domain);
2712 spin_unlock_irqrestore(&domain->lock, flags);
2716 * This is a special map_sg function which is used if we should map a
2717 * device which is not handled by an AMD IOMMU in the system.
2719 static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
2720 int nelems, int dir)
2722 struct scatterlist *s;
2725 for_each_sg(sglist, s, nelems, i) {
2726 s->dma_address = (dma_addr_t)sg_phys(s);
2727 s->dma_length = s->length;
2734 * The exported map_sg function for dma_ops (handles scatter-gather
2737 static int map_sg(struct device *dev, struct scatterlist *sglist,
2738 int nelems, enum dma_data_direction dir,
2739 struct dma_attrs *attrs)
2741 unsigned long flags;
2742 struct protection_domain *domain;
2744 struct scatterlist *s;
2746 int mapped_elems = 0;
2749 INC_STATS_COUNTER(cnt_map_sg);
2751 domain = get_domain(dev);
2752 if (PTR_ERR(domain) == -EINVAL)
2753 return map_sg_no_iommu(dev, sglist, nelems, dir);
2754 else if (IS_ERR(domain))
2757 dma_mask = *dev->dma_mask;
2759 spin_lock_irqsave(&domain->lock, flags);
2761 for_each_sg(sglist, s, nelems, i) {
2764 s->dma_address = __map_single(dev, domain->priv,
2765 paddr, s->length, dir, false,
2768 if (s->dma_address) {
2769 s->dma_length = s->length;
2775 domain_flush_complete(domain);
2778 spin_unlock_irqrestore(&domain->lock, flags);
2780 return mapped_elems;
2782 for_each_sg(sglist, s, mapped_elems, i) {
2784 __unmap_single(domain->priv, s->dma_address,
2785 s->dma_length, dir);
2786 s->dma_address = s->dma_length = 0;
2795 * The exported map_sg function for dma_ops (handles scatter-gather
2798 static void unmap_sg(struct device *dev, struct scatterlist *sglist,
2799 int nelems, enum dma_data_direction dir,
2800 struct dma_attrs *attrs)
2802 unsigned long flags;
2803 struct protection_domain *domain;
2804 struct scatterlist *s;
2807 INC_STATS_COUNTER(cnt_unmap_sg);
2809 domain = get_domain(dev);
2813 spin_lock_irqsave(&domain->lock, flags);
2815 for_each_sg(sglist, s, nelems, i) {
2816 __unmap_single(domain->priv, s->dma_address,
2817 s->dma_length, dir);
2818 s->dma_address = s->dma_length = 0;
2821 domain_flush_complete(domain);
2823 spin_unlock_irqrestore(&domain->lock, flags);
2827 * The exported alloc_coherent function for dma_ops.
2829 static void *alloc_coherent(struct device *dev, size_t size,
2830 dma_addr_t *dma_addr, gfp_t flag,
2831 struct dma_attrs *attrs)
2833 unsigned long flags;
2835 struct protection_domain *domain;
2837 u64 dma_mask = dev->coherent_dma_mask;
2839 INC_STATS_COUNTER(cnt_alloc_coherent);
2841 domain = get_domain(dev);
2842 if (PTR_ERR(domain) == -EINVAL) {
2843 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2844 *dma_addr = __pa(virt_addr);
2846 } else if (IS_ERR(domain))
2849 dma_mask = dev->coherent_dma_mask;
2850 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2853 virt_addr = (void *)__get_free_pages(flag, get_order(size));
2857 paddr = virt_to_phys(virt_addr);
2860 dma_mask = *dev->dma_mask;
2862 spin_lock_irqsave(&domain->lock, flags);
2864 *dma_addr = __map_single(dev, domain->priv, paddr,
2865 size, DMA_BIDIRECTIONAL, true, dma_mask);
2867 if (*dma_addr == DMA_ERROR_CODE) {
2868 spin_unlock_irqrestore(&domain->lock, flags);
2872 domain_flush_complete(domain);
2874 spin_unlock_irqrestore(&domain->lock, flags);
2880 free_pages((unsigned long)virt_addr, get_order(size));
2886 * The exported free_coherent function for dma_ops.
2888 static void free_coherent(struct device *dev, size_t size,
2889 void *virt_addr, dma_addr_t dma_addr,
2890 struct dma_attrs *attrs)
2892 unsigned long flags;
2893 struct protection_domain *domain;
2895 INC_STATS_COUNTER(cnt_free_coherent);
2897 domain = get_domain(dev);
2901 spin_lock_irqsave(&domain->lock, flags);
2903 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
2905 domain_flush_complete(domain);
2907 spin_unlock_irqrestore(&domain->lock, flags);
2910 free_pages((unsigned long)virt_addr, get_order(size));
2914 * This function is called by the DMA layer to find out if we can handle a
2915 * particular device. It is part of the dma_ops.
2917 static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2919 return check_device(dev);
2923 * The function for pre-allocating protection domains.
2925 * If the driver core informs the DMA layer if a driver grabs a device
2926 * we don't need to preallocate the protection domains anymore.
2927 * For now we have to.
2929 static void __init prealloc_protection_domains(void)
2931 struct iommu_dev_data *dev_data;
2932 struct dma_ops_domain *dma_dom;
2933 struct pci_dev *dev = NULL;
2936 for_each_pci_dev(dev) {
2938 /* Do we handle this device? */
2939 if (!check_device(&dev->dev))
2942 dev_data = get_dev_data(&dev->dev);
2943 if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
2944 /* Make sure passthrough domain is allocated */
2945 alloc_passthrough_domain();
2946 dev_data->passthrough = true;
2947 attach_device(&dev->dev, pt_domain);
2948 pr_info("AMD-Vi: Using passthough domain for device %s\n",
2949 dev_name(&dev->dev));
2952 /* Is there already any domain for it? */
2953 if (domain_for_device(&dev->dev))
2956 devid = get_device_id(&dev->dev);
2958 dma_dom = dma_ops_domain_alloc();
2961 init_unity_mappings_for_device(dma_dom, devid);
2962 dma_dom->target_dev = devid;
2964 attach_device(&dev->dev, &dma_dom->domain);
2966 list_add_tail(&dma_dom->list, &iommu_pd_list);
2970 static struct dma_map_ops amd_iommu_dma_ops = {
2971 .alloc = alloc_coherent,
2972 .free = free_coherent,
2973 .map_page = map_page,
2974 .unmap_page = unmap_page,
2976 .unmap_sg = unmap_sg,
2977 .dma_supported = amd_iommu_dma_supported,
2980 static unsigned device_dma_ops_init(void)
2982 struct iommu_dev_data *dev_data;
2983 struct pci_dev *pdev = NULL;
2984 unsigned unhandled = 0;
2986 for_each_pci_dev(pdev) {
2987 if (!check_device(&pdev->dev)) {
2989 iommu_ignore_device(&pdev->dev);
2995 dev_data = get_dev_data(&pdev->dev);
2997 if (!dev_data->passthrough)
2998 pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
3000 pdev->dev.archdata.dma_ops = &nommu_dma_ops;
3007 * The function which clues the AMD IOMMU driver into dma_ops.
3010 void __init amd_iommu_init_api(void)
3012 bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
3015 int __init amd_iommu_init_dma_ops(void)
3017 struct amd_iommu *iommu;
3021 * first allocate a default protection domain for every IOMMU we
3022 * found in the system. Devices not assigned to any other
3023 * protection domain will be assigned to the default one.
3025 for_each_iommu(iommu) {
3026 iommu->default_dom = dma_ops_domain_alloc();
3027 if (iommu->default_dom == NULL)
3029 iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
3030 ret = iommu_init_unity_mappings(iommu);
3036 * Pre-allocate the protection domains for each device.
3038 prealloc_protection_domains();
3043 /* Make the driver finally visible to the drivers */
3044 unhandled = device_dma_ops_init();
3045 if (unhandled && max_pfn > MAX_DMA32_PFN) {
3046 /* There are unhandled devices - initialize swiotlb for them */
3050 amd_iommu_stats_init();
3052 if (amd_iommu_unmap_flush)
3053 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
3055 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
3061 for_each_iommu(iommu) {
3062 if (iommu->default_dom)
3063 dma_ops_domain_free(iommu->default_dom);
3069 /*****************************************************************************
3071 * The following functions belong to the exported interface of AMD IOMMU
3073 * This interface allows access to lower level functions of the IOMMU
3074 * like protection domain handling and assignement of devices to domains
3075 * which is not possible with the dma_ops interface.
3077 *****************************************************************************/
3079 static void cleanup_domain(struct protection_domain *domain)
3081 struct iommu_dev_data *dev_data, *next;
3082 unsigned long flags;
3084 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3086 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
3087 __detach_device(dev_data);
3088 atomic_set(&dev_data->bind, 0);
3091 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3094 static void protection_domain_free(struct protection_domain *domain)
3099 del_domain_from_list(domain);
3102 domain_id_free(domain->id);
3107 static struct protection_domain *protection_domain_alloc(void)
3109 struct protection_domain *domain;
3111 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
3115 spin_lock_init(&domain->lock);
3116 mutex_init(&domain->api_lock);
3117 domain->id = domain_id_alloc();
3120 INIT_LIST_HEAD(&domain->dev_list);
3122 add_domain_to_list(domain);
3132 static int __init alloc_passthrough_domain(void)
3134 if (pt_domain != NULL)
3137 /* allocate passthrough domain */
3138 pt_domain = protection_domain_alloc();
3142 pt_domain->mode = PAGE_MODE_NONE;
3146 static int amd_iommu_domain_init(struct iommu_domain *dom)
3148 struct protection_domain *domain;
3150 domain = protection_domain_alloc();
3154 domain->mode = PAGE_MODE_3_LEVEL;
3155 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
3156 if (!domain->pt_root)
3159 domain->iommu_domain = dom;
3163 dom->geometry.aperture_start = 0;
3164 dom->geometry.aperture_end = ~0ULL;
3165 dom->geometry.force_aperture = true;
3170 protection_domain_free(domain);
3175 static void amd_iommu_domain_destroy(struct iommu_domain *dom)
3177 struct protection_domain *domain = dom->priv;
3182 if (domain->dev_cnt > 0)
3183 cleanup_domain(domain);
3185 BUG_ON(domain->dev_cnt != 0);
3187 if (domain->mode != PAGE_MODE_NONE)
3188 free_pagetable(domain);
3190 if (domain->flags & PD_IOMMUV2_MASK)
3191 free_gcr3_table(domain);
3193 protection_domain_free(domain);
3198 static void amd_iommu_detach_device(struct iommu_domain *dom,
3201 struct iommu_dev_data *dev_data = dev->archdata.iommu;
3202 struct amd_iommu *iommu;
3205 if (!check_device(dev))
3208 devid = get_device_id(dev);
3210 if (dev_data->domain != NULL)
3213 iommu = amd_iommu_rlookup_table[devid];
3217 iommu_completion_wait(iommu);
3220 static int amd_iommu_attach_device(struct iommu_domain *dom,
3223 struct protection_domain *domain = dom->priv;
3224 struct iommu_dev_data *dev_data;
3225 struct amd_iommu *iommu;
3228 if (!check_device(dev))
3231 dev_data = dev->archdata.iommu;
3233 iommu = amd_iommu_rlookup_table[dev_data->devid];
3237 if (dev_data->domain)
3240 ret = attach_device(dev, domain);
3242 iommu_completion_wait(iommu);
3247 static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
3248 phys_addr_t paddr, size_t page_size, int iommu_prot)
3250 struct protection_domain *domain = dom->priv;
3254 if (domain->mode == PAGE_MODE_NONE)
3257 if (iommu_prot & IOMMU_READ)
3258 prot |= IOMMU_PROT_IR;
3259 if (iommu_prot & IOMMU_WRITE)
3260 prot |= IOMMU_PROT_IW;
3262 mutex_lock(&domain->api_lock);
3263 ret = iommu_map_page(domain, iova, paddr, prot, page_size);
3264 mutex_unlock(&domain->api_lock);
3269 static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3272 struct protection_domain *domain = dom->priv;
3275 if (domain->mode == PAGE_MODE_NONE)
3278 mutex_lock(&domain->api_lock);
3279 unmap_size = iommu_unmap_page(domain, iova, page_size);
3280 mutex_unlock(&domain->api_lock);
3282 domain_flush_tlb_pde(domain);
3287 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3290 struct protection_domain *domain = dom->priv;
3291 unsigned long offset_mask;
3295 if (domain->mode == PAGE_MODE_NONE)
3298 pte = fetch_pte(domain, iova);
3300 if (!pte || !IOMMU_PTE_PRESENT(*pte))
3303 if (PM_PTE_LEVEL(*pte) == 0)
3304 offset_mask = PAGE_SIZE - 1;
3306 offset_mask = PTE_PAGE_SIZE(*pte) - 1;
3308 __pte = *pte & PM_ADDR_MASK;
3309 paddr = (__pte & ~offset_mask) | (iova & offset_mask);
3314 static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
3318 case IOMMU_CAP_CACHE_COHERENCY:
3325 static struct iommu_ops amd_iommu_ops = {
3326 .domain_init = amd_iommu_domain_init,
3327 .domain_destroy = amd_iommu_domain_destroy,
3328 .attach_dev = amd_iommu_attach_device,
3329 .detach_dev = amd_iommu_detach_device,
3330 .map = amd_iommu_map,
3331 .unmap = amd_iommu_unmap,
3332 .iova_to_phys = amd_iommu_iova_to_phys,
3333 .domain_has_cap = amd_iommu_domain_has_cap,
3334 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
3337 /*****************************************************************************
3339 * The next functions do a basic initialization of IOMMU for pass through
3342 * In passthrough mode the IOMMU is initialized and enabled but not used for
3343 * DMA-API translation.
3345 *****************************************************************************/
3347 int __init amd_iommu_init_passthrough(void)
3349 struct iommu_dev_data *dev_data;
3350 struct pci_dev *dev = NULL;
3351 struct amd_iommu *iommu;
3355 ret = alloc_passthrough_domain();
3359 for_each_pci_dev(dev) {
3360 if (!check_device(&dev->dev))
3363 dev_data = get_dev_data(&dev->dev);
3364 dev_data->passthrough = true;
3366 devid = get_device_id(&dev->dev);
3368 iommu = amd_iommu_rlookup_table[devid];
3372 attach_device(&dev->dev, pt_domain);
3375 amd_iommu_stats_init();
3377 pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
3382 /* IOMMUv2 specific functions */
3383 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3385 return atomic_notifier_chain_register(&ppr_notifier, nb);
3387 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3389 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3391 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3393 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
3395 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3397 struct protection_domain *domain = dom->priv;
3398 unsigned long flags;
3400 spin_lock_irqsave(&domain->lock, flags);
3402 /* Update data structure */
3403 domain->mode = PAGE_MODE_NONE;
3404 domain->updated = true;
3406 /* Make changes visible to IOMMUs */
3407 update_domain(domain);
3409 /* Page-table is not visible to IOMMU anymore, so free it */
3410 free_pagetable(domain);
3412 spin_unlock_irqrestore(&domain->lock, flags);
3414 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
3416 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3418 struct protection_domain *domain = dom->priv;
3419 unsigned long flags;
3422 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3425 /* Number of GCR3 table levels required */
3426 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3429 if (levels > amd_iommu_max_glx_val)
3432 spin_lock_irqsave(&domain->lock, flags);
3435 * Save us all sanity checks whether devices already in the
3436 * domain support IOMMUv2. Just force that the domain has no
3437 * devices attached when it is switched into IOMMUv2 mode.
3440 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3444 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3445 if (domain->gcr3_tbl == NULL)
3448 domain->glx = levels;
3449 domain->flags |= PD_IOMMUV2_MASK;
3450 domain->updated = true;
3452 update_domain(domain);
3457 spin_unlock_irqrestore(&domain->lock, flags);
3461 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
3463 static int __flush_pasid(struct protection_domain *domain, int pasid,
3464 u64 address, bool size)
3466 struct iommu_dev_data *dev_data;
3467 struct iommu_cmd cmd;
3470 if (!(domain->flags & PD_IOMMUV2_MASK))
3473 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3476 * IOMMU TLB needs to be flushed before Device TLB to
3477 * prevent device TLB refill from IOMMU TLB
3479 for (i = 0; i < amd_iommus_present; ++i) {
3480 if (domain->dev_iommu[i] == 0)
3483 ret = iommu_queue_command(amd_iommus[i], &cmd);
3488 /* Wait until IOMMU TLB flushes are complete */
3489 domain_flush_complete(domain);
3491 /* Now flush device TLBs */
3492 list_for_each_entry(dev_data, &domain->dev_list, list) {
3493 struct amd_iommu *iommu;
3496 BUG_ON(!dev_data->ats.enabled);
3498 qdep = dev_data->ats.qdep;
3499 iommu = amd_iommu_rlookup_table[dev_data->devid];
3501 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3502 qdep, address, size);
3504 ret = iommu_queue_command(iommu, &cmd);
3509 /* Wait until all device TLBs are flushed */
3510 domain_flush_complete(domain);
3519 static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3522 INC_STATS_COUNTER(invalidate_iotlb);
3524 return __flush_pasid(domain, pasid, address, false);
3527 int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3530 struct protection_domain *domain = dom->priv;
3531 unsigned long flags;
3534 spin_lock_irqsave(&domain->lock, flags);
3535 ret = __amd_iommu_flush_page(domain, pasid, address);
3536 spin_unlock_irqrestore(&domain->lock, flags);
3540 EXPORT_SYMBOL(amd_iommu_flush_page);
3542 static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3544 INC_STATS_COUNTER(invalidate_iotlb_all);
3546 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3550 int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3552 struct protection_domain *domain = dom->priv;
3553 unsigned long flags;
3556 spin_lock_irqsave(&domain->lock, flags);
3557 ret = __amd_iommu_flush_tlb(domain, pasid);
3558 spin_unlock_irqrestore(&domain->lock, flags);
3562 EXPORT_SYMBOL(amd_iommu_flush_tlb);
3564 static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3571 index = (pasid >> (9 * level)) & 0x1ff;
3577 if (!(*pte & GCR3_VALID)) {
3581 root = (void *)get_zeroed_page(GFP_ATOMIC);
3585 *pte = __pa(root) | GCR3_VALID;
3588 root = __va(*pte & PAGE_MASK);
3596 static int __set_gcr3(struct protection_domain *domain, int pasid,
3601 if (domain->mode != PAGE_MODE_NONE)
3604 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3608 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3610 return __amd_iommu_flush_tlb(domain, pasid);
3613 static int __clear_gcr3(struct protection_domain *domain, int pasid)
3617 if (domain->mode != PAGE_MODE_NONE)
3620 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3626 return __amd_iommu_flush_tlb(domain, pasid);
3629 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3632 struct protection_domain *domain = dom->priv;
3633 unsigned long flags;
3636 spin_lock_irqsave(&domain->lock, flags);
3637 ret = __set_gcr3(domain, pasid, cr3);
3638 spin_unlock_irqrestore(&domain->lock, flags);
3642 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3644 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3646 struct protection_domain *domain = dom->priv;
3647 unsigned long flags;
3650 spin_lock_irqsave(&domain->lock, flags);
3651 ret = __clear_gcr3(domain, pasid);
3652 spin_unlock_irqrestore(&domain->lock, flags);
3656 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
3658 int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3659 int status, int tag)
3661 struct iommu_dev_data *dev_data;
3662 struct amd_iommu *iommu;
3663 struct iommu_cmd cmd;
3665 INC_STATS_COUNTER(complete_ppr);
3667 dev_data = get_dev_data(&pdev->dev);
3668 iommu = amd_iommu_rlookup_table[dev_data->devid];
3670 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3671 tag, dev_data->pri_tlp);
3673 return iommu_queue_command(iommu, &cmd);
3675 EXPORT_SYMBOL(amd_iommu_complete_ppr);
3677 struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3679 struct protection_domain *domain;
3681 domain = get_domain(&pdev->dev);
3685 /* Only return IOMMUv2 domains */
3686 if (!(domain->flags & PD_IOMMUV2_MASK))
3689 return domain->iommu_domain;
3691 EXPORT_SYMBOL(amd_iommu_get_v2_domain);
3693 void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3695 struct iommu_dev_data *dev_data;
3697 if (!amd_iommu_v2_supported())
3700 dev_data = get_dev_data(&pdev->dev);
3701 dev_data->errata |= (1 << erratum);
3703 EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
3705 int amd_iommu_device_info(struct pci_dev *pdev,
3706 struct amd_iommu_device_info *info)
3711 if (pdev == NULL || info == NULL)
3714 if (!amd_iommu_v2_supported())
3717 memset(info, 0, sizeof(*info));
3719 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3721 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3723 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3725 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3727 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3731 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3732 max_pasids = min(max_pasids, (1 << 20));
3734 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3735 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3737 features = pci_pasid_features(pdev);
3738 if (features & PCI_PASID_CAP_EXEC)
3739 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3740 if (features & PCI_PASID_CAP_PRIV)
3741 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3746 EXPORT_SYMBOL(amd_iommu_device_info);