2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <jroedel@suse.de>
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/acpi.h>
23 #include <linux/amba/bus.h>
24 #include <linux/platform_device.h>
25 #include <linux/pci-ats.h>
26 #include <linux/bitmap.h>
27 #include <linux/slab.h>
28 #include <linux/debugfs.h>
29 #include <linux/scatterlist.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/iommu-helper.h>
32 #include <linux/iommu.h>
33 #include <linux/delay.h>
34 #include <linux/amd-iommu.h>
35 #include <linux/notifier.h>
36 #include <linux/export.h>
37 #include <linux/irq.h>
38 #include <linux/msi.h>
39 #include <linux/dma-contiguous.h>
40 #include <linux/irqdomain.h>
41 #include <linux/percpu.h>
42 #include <linux/iova.h>
43 #include <asm/irq_remapping.h>
44 #include <asm/io_apic.h>
46 #include <asm/hw_irq.h>
47 #include <asm/msidef.h>
48 #include <asm/proto.h>
49 #include <asm/iommu.h>
53 #include "amd_iommu_proto.h"
54 #include "amd_iommu_types.h"
55 #include "irq_remapping.h"
57 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
59 #define LOOP_TIMEOUT 100000
61 /* IO virtual address start page frame number */
62 #define IOVA_START_PFN (1)
63 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
64 #define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
66 /* Reserved IOVA ranges */
67 #define MSI_RANGE_START (0xfee00000)
68 #define MSI_RANGE_END (0xfeefffff)
69 #define HT_RANGE_START (0xfd00000000ULL)
70 #define HT_RANGE_END (0xffffffffffULL)
73 * This bitmap is used to advertise the page sizes our hardware support
74 * to the IOMMU core, which will then use this information to split
75 * physically contiguous memory regions it is mapping into page sizes
78 * 512GB Pages are not supported due to a hardware bug
80 #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
82 static DEFINE_RWLOCK(amd_iommu_devtable_lock);
84 /* List of all available dev_data structures */
85 static LIST_HEAD(dev_data_list);
86 static DEFINE_SPINLOCK(dev_data_list_lock);
88 LIST_HEAD(ioapic_map);
90 LIST_HEAD(acpihid_map);
92 #define FLUSH_QUEUE_SIZE 256
94 struct flush_queue_entry {
95 unsigned long iova_pfn;
97 struct dma_ops_domain *dma_dom;
103 struct flush_queue_entry *entries;
106 DEFINE_PER_CPU(struct flush_queue, flush_queue);
108 static atomic_t queue_timer_on;
109 static struct timer_list queue_timer;
112 * Domain for untranslated devices - only allocated
113 * if iommu=pt passed on kernel cmd line.
115 static const struct iommu_ops amd_iommu_ops;
117 static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
118 int amd_iommu_max_glx_val = -1;
120 static struct dma_map_ops amd_iommu_dma_ops;
123 * This struct contains device specific data for the IOMMU
125 struct iommu_dev_data {
126 struct list_head list; /* For domain->dev_list */
127 struct list_head dev_data_list; /* For global dev_data_list */
128 struct protection_domain *domain; /* Domain the device is bound to */
129 u16 devid; /* PCI Device ID */
130 u16 alias; /* Alias Device ID */
131 bool iommu_v2; /* Device can make use of IOMMUv2 */
132 bool passthrough; /* Device is identity mapped */
136 } ats; /* ATS state */
137 bool pri_tlp; /* PASID TLB required for
139 u32 errata; /* Bitmap for errata to apply */
143 * general struct to manage commands send to an IOMMU
149 struct kmem_cache *amd_iommu_irq_cache;
151 static void update_domain(struct protection_domain *domain);
152 static int protection_domain_init(struct protection_domain *domain);
153 static void detach_device(struct device *dev);
156 * Data container for a dma_ops specific protection domain
158 struct dma_ops_domain {
159 /* generic protection domain information */
160 struct protection_domain domain;
163 struct iova_domain iovad;
166 static struct iova_domain reserved_iova_ranges;
167 static struct lock_class_key reserved_rbtree_key;
169 /****************************************************************************
173 ****************************************************************************/
175 static inline int match_hid_uid(struct device *dev,
176 struct acpihid_map_entry *entry)
178 const char *hid, *uid;
180 hid = acpi_device_hid(ACPI_COMPANION(dev));
181 uid = acpi_device_uid(ACPI_COMPANION(dev));
187 return strcmp(hid, entry->hid);
190 return strcmp(hid, entry->hid);
192 return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
195 static inline u16 get_pci_device_id(struct device *dev)
197 struct pci_dev *pdev = to_pci_dev(dev);
199 return PCI_DEVID(pdev->bus->number, pdev->devfn);
202 static inline int get_acpihid_device_id(struct device *dev,
203 struct acpihid_map_entry **entry)
205 struct acpihid_map_entry *p;
207 list_for_each_entry(p, &acpihid_map, list) {
208 if (!match_hid_uid(dev, p)) {
217 static inline int get_device_id(struct device *dev)
222 devid = get_pci_device_id(dev);
224 devid = get_acpihid_device_id(dev, NULL);
229 static struct protection_domain *to_pdomain(struct iommu_domain *dom)
231 return container_of(dom, struct protection_domain, domain);
234 static struct dma_ops_domain* to_dma_ops_domain(struct protection_domain *domain)
236 BUG_ON(domain->flags != PD_DMA_OPS_MASK);
237 return container_of(domain, struct dma_ops_domain, domain);
240 static struct iommu_dev_data *alloc_dev_data(u16 devid)
242 struct iommu_dev_data *dev_data;
245 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
249 dev_data->devid = devid;
251 spin_lock_irqsave(&dev_data_list_lock, flags);
252 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
253 spin_unlock_irqrestore(&dev_data_list_lock, flags);
258 static struct iommu_dev_data *search_dev_data(u16 devid)
260 struct iommu_dev_data *dev_data;
263 spin_lock_irqsave(&dev_data_list_lock, flags);
264 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
265 if (dev_data->devid == devid)
272 spin_unlock_irqrestore(&dev_data_list_lock, flags);
277 static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
279 *(u16 *)data = alias;
283 static u16 get_alias(struct device *dev)
285 struct pci_dev *pdev = to_pci_dev(dev);
286 u16 devid, ivrs_alias, pci_alias;
288 /* The callers make sure that get_device_id() does not fail here */
289 devid = get_device_id(dev);
290 ivrs_alias = amd_iommu_alias_table[devid];
291 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
293 if (ivrs_alias == pci_alias)
299 * The IVRS is fairly reliable in telling us about aliases, but it
300 * can't know about every screwy device. If we don't have an IVRS
301 * reported alias, use the PCI reported alias. In that case we may
302 * still need to initialize the rlookup and dev_table entries if the
303 * alias is to a non-existent device.
305 if (ivrs_alias == devid) {
306 if (!amd_iommu_rlookup_table[pci_alias]) {
307 amd_iommu_rlookup_table[pci_alias] =
308 amd_iommu_rlookup_table[devid];
309 memcpy(amd_iommu_dev_table[pci_alias].data,
310 amd_iommu_dev_table[devid].data,
311 sizeof(amd_iommu_dev_table[pci_alias].data));
317 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
318 "for device %s[%04x:%04x], kernel reported alias "
319 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
320 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
321 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
322 PCI_FUNC(pci_alias));
325 * If we don't have a PCI DMA alias and the IVRS alias is on the same
326 * bus, then the IVRS table may know about a quirk that we don't.
328 if (pci_alias == devid &&
329 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
330 pci_add_dma_alias(pdev, ivrs_alias & 0xff);
331 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
332 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
339 static struct iommu_dev_data *find_dev_data(u16 devid)
341 struct iommu_dev_data *dev_data;
343 dev_data = search_dev_data(devid);
345 if (dev_data == NULL)
346 dev_data = alloc_dev_data(devid);
351 static struct iommu_dev_data *get_dev_data(struct device *dev)
353 return dev->archdata.iommu;
357 * Find or create an IOMMU group for a acpihid device.
359 static struct iommu_group *acpihid_device_group(struct device *dev)
361 struct acpihid_map_entry *p, *entry = NULL;
364 devid = get_acpihid_device_id(dev, &entry);
366 return ERR_PTR(devid);
368 list_for_each_entry(p, &acpihid_map, list) {
369 if ((devid == p->devid) && p->group)
370 entry->group = p->group;
374 entry->group = generic_device_group(dev);
379 static bool pci_iommuv2_capable(struct pci_dev *pdev)
381 static const int caps[] = {
384 PCI_EXT_CAP_ID_PASID,
388 for (i = 0; i < 3; ++i) {
389 pos = pci_find_ext_capability(pdev, caps[i]);
397 static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
399 struct iommu_dev_data *dev_data;
401 dev_data = get_dev_data(&pdev->dev);
403 return dev_data->errata & (1 << erratum) ? true : false;
407 * This function checks if the driver got a valid device from the caller to
408 * avoid dereferencing invalid pointers.
410 static bool check_device(struct device *dev)
414 if (!dev || !dev->dma_mask)
417 devid = get_device_id(dev);
421 /* Out of our scope? */
422 if (devid > amd_iommu_last_bdf)
425 if (amd_iommu_rlookup_table[devid] == NULL)
431 static void init_iommu_group(struct device *dev)
433 struct iommu_group *group;
435 group = iommu_group_get_for_dev(dev);
439 iommu_group_put(group);
442 static int iommu_init_device(struct device *dev)
444 struct iommu_dev_data *dev_data;
447 if (dev->archdata.iommu)
450 devid = get_device_id(dev);
454 dev_data = find_dev_data(devid);
458 dev_data->alias = get_alias(dev);
460 if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
461 struct amd_iommu *iommu;
463 iommu = amd_iommu_rlookup_table[dev_data->devid];
464 dev_data->iommu_v2 = iommu->is_iommu_v2;
467 dev->archdata.iommu = dev_data;
469 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
475 static void iommu_ignore_device(struct device *dev)
480 devid = get_device_id(dev);
484 alias = get_alias(dev);
486 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
487 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
489 amd_iommu_rlookup_table[devid] = NULL;
490 amd_iommu_rlookup_table[alias] = NULL;
493 static void iommu_uninit_device(struct device *dev)
496 struct iommu_dev_data *dev_data;
498 devid = get_device_id(dev);
502 dev_data = search_dev_data(devid);
506 if (dev_data->domain)
509 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
512 iommu_group_remove_device(dev);
515 dev->archdata.dma_ops = NULL;
518 * We keep dev_data around for unplugged devices and reuse it when the
519 * device is re-plugged - not doing so would introduce a ton of races.
523 /****************************************************************************
525 * Interrupt handling functions
527 ****************************************************************************/
529 static void dump_dte_entry(u16 devid)
533 for (i = 0; i < 4; ++i)
534 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
535 amd_iommu_dev_table[devid].data[i]);
538 static void dump_command(unsigned long phys_addr)
540 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
543 for (i = 0; i < 4; ++i)
544 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
547 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
549 int type, devid, domid, flags;
550 volatile u32 *event = __evt;
555 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
556 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
557 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
558 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
559 address = (u64)(((u64)event[3]) << 32) | event[2];
562 /* Did we hit the erratum? */
563 if (++count == LOOP_TIMEOUT) {
564 pr_err("AMD-Vi: No event written to event log\n");
571 printk(KERN_ERR "AMD-Vi: Event logged [");
574 case EVENT_TYPE_ILL_DEV:
575 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
576 "address=0x%016llx flags=0x%04x]\n",
577 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
579 dump_dte_entry(devid);
581 case EVENT_TYPE_IO_FAULT:
582 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
583 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
584 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
585 domid, address, flags);
587 case EVENT_TYPE_DEV_TAB_ERR:
588 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
589 "address=0x%016llx flags=0x%04x]\n",
590 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
593 case EVENT_TYPE_PAGE_TAB_ERR:
594 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
595 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
596 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
597 domid, address, flags);
599 case EVENT_TYPE_ILL_CMD:
600 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
601 dump_command(address);
603 case EVENT_TYPE_CMD_HARD_ERR:
604 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
605 "flags=0x%04x]\n", address, flags);
607 case EVENT_TYPE_IOTLB_INV_TO:
608 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
609 "address=0x%016llx]\n",
610 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
613 case EVENT_TYPE_INV_DEV_REQ:
614 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
615 "address=0x%016llx flags=0x%04x]\n",
616 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
620 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
623 memset(__evt, 0, 4 * sizeof(u32));
626 static void iommu_poll_events(struct amd_iommu *iommu)
630 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
631 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
633 while (head != tail) {
634 iommu_print_event(iommu, iommu->evt_buf + head);
635 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
638 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
641 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
643 struct amd_iommu_fault fault;
645 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
646 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
650 fault.address = raw[1];
651 fault.pasid = PPR_PASID(raw[0]);
652 fault.device_id = PPR_DEVID(raw[0]);
653 fault.tag = PPR_TAG(raw[0]);
654 fault.flags = PPR_FLAGS(raw[0]);
656 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
659 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
663 if (iommu->ppr_log == NULL)
666 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
667 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
669 while (head != tail) {
674 raw = (u64 *)(iommu->ppr_log + head);
677 * Hardware bug: Interrupt may arrive before the entry is
678 * written to memory. If this happens we need to wait for the
681 for (i = 0; i < LOOP_TIMEOUT; ++i) {
682 if (PPR_REQ_TYPE(raw[0]) != 0)
687 /* Avoid memcpy function-call overhead */
692 * To detect the hardware bug we need to clear the entry
695 raw[0] = raw[1] = 0UL;
697 /* Update head pointer of hardware ring-buffer */
698 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
699 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
701 /* Handle PPR entry */
702 iommu_handle_ppr_entry(iommu, entry);
704 /* Refresh ring-buffer information */
705 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
706 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
710 irqreturn_t amd_iommu_int_thread(int irq, void *data)
712 struct amd_iommu *iommu = (struct amd_iommu *) data;
713 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
715 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
716 /* Enable EVT and PPR interrupts again */
717 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
718 iommu->mmio_base + MMIO_STATUS_OFFSET);
720 if (status & MMIO_STATUS_EVT_INT_MASK) {
721 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
722 iommu_poll_events(iommu);
725 if (status & MMIO_STATUS_PPR_INT_MASK) {
726 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
727 iommu_poll_ppr_log(iommu);
731 * Hardware bug: ERBT1312
732 * When re-enabling interrupt (by writing 1
733 * to clear the bit), the hardware might also try to set
734 * the interrupt bit in the event status register.
735 * In this scenario, the bit will be set, and disable
736 * subsequent interrupts.
738 * Workaround: The IOMMU driver should read back the
739 * status register and check if the interrupt bits are cleared.
740 * If not, driver will need to go through the interrupt handler
741 * again and re-clear the bits
743 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
748 irqreturn_t amd_iommu_int_handler(int irq, void *data)
750 return IRQ_WAKE_THREAD;
753 /****************************************************************************
755 * IOMMU command queuing functions
757 ****************************************************************************/
759 static int wait_on_sem(volatile u64 *sem)
763 while (*sem == 0 && i < LOOP_TIMEOUT) {
768 if (i == LOOP_TIMEOUT) {
769 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
776 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
777 struct iommu_cmd *cmd,
782 target = iommu->cmd_buf + tail;
783 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
785 /* Copy command to buffer */
786 memcpy(target, cmd, sizeof(*cmd));
788 /* Tell the IOMMU about it */
789 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
792 static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
794 WARN_ON(address & 0x7ULL);
796 memset(cmd, 0, sizeof(*cmd));
797 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
798 cmd->data[1] = upper_32_bits(__pa(address));
800 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
803 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
805 memset(cmd, 0, sizeof(*cmd));
806 cmd->data[0] = devid;
807 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
810 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
811 size_t size, u16 domid, int pde)
816 pages = iommu_num_pages(address, size, PAGE_SIZE);
821 * If we have to flush more than one page, flush all
822 * TLB entries for this domain
824 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
828 address &= PAGE_MASK;
830 memset(cmd, 0, sizeof(*cmd));
831 cmd->data[1] |= domid;
832 cmd->data[2] = lower_32_bits(address);
833 cmd->data[3] = upper_32_bits(address);
834 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
835 if (s) /* size bit - we flush more than one 4kb page */
836 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
837 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
838 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
841 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
842 u64 address, size_t size)
847 pages = iommu_num_pages(address, size, PAGE_SIZE);
852 * If we have to flush more than one page, flush all
853 * TLB entries for this domain
855 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
859 address &= PAGE_MASK;
861 memset(cmd, 0, sizeof(*cmd));
862 cmd->data[0] = devid;
863 cmd->data[0] |= (qdep & 0xff) << 24;
864 cmd->data[1] = devid;
865 cmd->data[2] = lower_32_bits(address);
866 cmd->data[3] = upper_32_bits(address);
867 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
869 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
872 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
873 u64 address, bool size)
875 memset(cmd, 0, sizeof(*cmd));
877 address &= ~(0xfffULL);
879 cmd->data[0] = pasid;
880 cmd->data[1] = domid;
881 cmd->data[2] = lower_32_bits(address);
882 cmd->data[3] = upper_32_bits(address);
883 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
884 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
886 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
887 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
890 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
891 int qdep, u64 address, bool size)
893 memset(cmd, 0, sizeof(*cmd));
895 address &= ~(0xfffULL);
897 cmd->data[0] = devid;
898 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
899 cmd->data[0] |= (qdep & 0xff) << 24;
900 cmd->data[1] = devid;
901 cmd->data[1] |= (pasid & 0xff) << 16;
902 cmd->data[2] = lower_32_bits(address);
903 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
904 cmd->data[3] = upper_32_bits(address);
906 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
907 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
910 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
911 int status, int tag, bool gn)
913 memset(cmd, 0, sizeof(*cmd));
915 cmd->data[0] = devid;
917 cmd->data[1] = pasid;
918 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
920 cmd->data[3] = tag & 0x1ff;
921 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
923 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
926 static void build_inv_all(struct iommu_cmd *cmd)
928 memset(cmd, 0, sizeof(*cmd));
929 CMD_SET_TYPE(cmd, CMD_INV_ALL);
932 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
934 memset(cmd, 0, sizeof(*cmd));
935 cmd->data[0] = devid;
936 CMD_SET_TYPE(cmd, CMD_INV_IRT);
940 * Writes the command to the IOMMUs command buffer and informs the
941 * hardware about the new command.
943 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
944 struct iommu_cmd *cmd,
947 u32 left, tail, head, next_tail;
951 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
952 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
953 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
954 left = (head - next_tail) % CMD_BUFFER_SIZE;
957 struct iommu_cmd sync_cmd;
962 build_completion_wait(&sync_cmd, (u64)&iommu->cmd_sem);
963 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
965 if ((ret = wait_on_sem(&iommu->cmd_sem)) != 0)
971 copy_cmd_to_buffer(iommu, cmd, tail);
973 /* We need to sync now to make sure all commands are processed */
974 iommu->need_sync = sync;
979 static int iommu_queue_command_sync(struct amd_iommu *iommu,
980 struct iommu_cmd *cmd,
986 spin_lock_irqsave(&iommu->lock, flags);
987 ret = __iommu_queue_command_sync(iommu, cmd, sync);
988 spin_unlock_irqrestore(&iommu->lock, flags);
993 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
995 return iommu_queue_command_sync(iommu, cmd, true);
999 * This function queues a completion wait command into the command
1000 * buffer of an IOMMU
1002 static int iommu_completion_wait(struct amd_iommu *iommu)
1004 struct iommu_cmd cmd;
1005 unsigned long flags;
1008 if (!iommu->need_sync)
1012 build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
1014 spin_lock_irqsave(&iommu->lock, flags);
1018 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1022 ret = wait_on_sem(&iommu->cmd_sem);
1025 spin_unlock_irqrestore(&iommu->lock, flags);
1030 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1032 struct iommu_cmd cmd;
1034 build_inv_dte(&cmd, devid);
1036 return iommu_queue_command(iommu, &cmd);
1039 static void iommu_flush_dte_all(struct amd_iommu *iommu)
1043 for (devid = 0; devid <= 0xffff; ++devid)
1044 iommu_flush_dte(iommu, devid);
1046 iommu_completion_wait(iommu);
1050 * This function uses heavy locking and may disable irqs for some time. But
1051 * this is no issue because it is only called during resume.
1053 static void iommu_flush_tlb_all(struct amd_iommu *iommu)
1057 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1058 struct iommu_cmd cmd;
1059 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1061 iommu_queue_command(iommu, &cmd);
1064 iommu_completion_wait(iommu);
1067 static void iommu_flush_all(struct amd_iommu *iommu)
1069 struct iommu_cmd cmd;
1071 build_inv_all(&cmd);
1073 iommu_queue_command(iommu, &cmd);
1074 iommu_completion_wait(iommu);
1077 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1079 struct iommu_cmd cmd;
1081 build_inv_irt(&cmd, devid);
1083 iommu_queue_command(iommu, &cmd);
1086 static void iommu_flush_irt_all(struct amd_iommu *iommu)
1090 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1091 iommu_flush_irt(iommu, devid);
1093 iommu_completion_wait(iommu);
1096 void iommu_flush_all_caches(struct amd_iommu *iommu)
1098 if (iommu_feature(iommu, FEATURE_IA)) {
1099 iommu_flush_all(iommu);
1101 iommu_flush_dte_all(iommu);
1102 iommu_flush_irt_all(iommu);
1103 iommu_flush_tlb_all(iommu);
1108 * Command send function for flushing on-device TLB
1110 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1111 u64 address, size_t size)
1113 struct amd_iommu *iommu;
1114 struct iommu_cmd cmd;
1117 qdep = dev_data->ats.qdep;
1118 iommu = amd_iommu_rlookup_table[dev_data->devid];
1120 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1122 return iommu_queue_command(iommu, &cmd);
1126 * Command send function for invalidating a device table entry
1128 static int device_flush_dte(struct iommu_dev_data *dev_data)
1130 struct amd_iommu *iommu;
1134 iommu = amd_iommu_rlookup_table[dev_data->devid];
1135 alias = dev_data->alias;
1137 ret = iommu_flush_dte(iommu, dev_data->devid);
1138 if (!ret && alias != dev_data->devid)
1139 ret = iommu_flush_dte(iommu, alias);
1143 if (dev_data->ats.enabled)
1144 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1150 * TLB invalidation function which is called from the mapping functions.
1151 * It invalidates a single PTE if the range to flush is within a single
1152 * page. Otherwise it flushes the whole TLB of the IOMMU.
1154 static void __domain_flush_pages(struct protection_domain *domain,
1155 u64 address, size_t size, int pde)
1157 struct iommu_dev_data *dev_data;
1158 struct iommu_cmd cmd;
1161 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1163 for (i = 0; i < amd_iommus_present; ++i) {
1164 if (!domain->dev_iommu[i])
1168 * Devices of this domain are behind this IOMMU
1169 * We need a TLB flush
1171 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1174 list_for_each_entry(dev_data, &domain->dev_list, list) {
1176 if (!dev_data->ats.enabled)
1179 ret |= device_flush_iotlb(dev_data, address, size);
1185 static void domain_flush_pages(struct protection_domain *domain,
1186 u64 address, size_t size)
1188 __domain_flush_pages(domain, address, size, 0);
1191 /* Flush the whole IO/TLB for a given protection domain */
1192 static void domain_flush_tlb(struct protection_domain *domain)
1194 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
1197 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1198 static void domain_flush_tlb_pde(struct protection_domain *domain)
1200 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1203 static void domain_flush_complete(struct protection_domain *domain)
1207 for (i = 0; i < amd_iommus_present; ++i) {
1208 if (domain && !domain->dev_iommu[i])
1212 * Devices of this domain are behind this IOMMU
1213 * We need to wait for completion of all commands.
1215 iommu_completion_wait(amd_iommus[i]);
1221 * This function flushes the DTEs for all devices in domain
1223 static void domain_flush_devices(struct protection_domain *domain)
1225 struct iommu_dev_data *dev_data;
1227 list_for_each_entry(dev_data, &domain->dev_list, list)
1228 device_flush_dte(dev_data);
1231 /****************************************************************************
1233 * The functions below are used the create the page table mappings for
1234 * unity mapped regions.
1236 ****************************************************************************/
1239 * This function is used to add another level to an IO page table. Adding
1240 * another level increases the size of the address space by 9 bits to a size up
1243 static bool increase_address_space(struct protection_domain *domain,
1248 if (domain->mode == PAGE_MODE_6_LEVEL)
1249 /* address space already 64 bit large */
1252 pte = (void *)get_zeroed_page(gfp);
1256 *pte = PM_LEVEL_PDE(domain->mode,
1257 virt_to_phys(domain->pt_root));
1258 domain->pt_root = pte;
1260 domain->updated = true;
1265 static u64 *alloc_pte(struct protection_domain *domain,
1266 unsigned long address,
1267 unsigned long page_size,
1274 BUG_ON(!is_power_of_2(page_size));
1276 while (address > PM_LEVEL_SIZE(domain->mode))
1277 increase_address_space(domain, gfp);
1279 level = domain->mode - 1;
1280 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1281 address = PAGE_SIZE_ALIGN(address, page_size);
1282 end_lvl = PAGE_SIZE_LEVEL(page_size);
1284 while (level > end_lvl) {
1289 if (!IOMMU_PTE_PRESENT(__pte)) {
1290 page = (u64 *)get_zeroed_page(gfp);
1294 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1296 if (cmpxchg64(pte, __pte, __npte)) {
1297 free_page((unsigned long)page);
1302 /* No level skipping support yet */
1303 if (PM_PTE_LEVEL(*pte) != level)
1308 pte = IOMMU_PTE_PAGE(*pte);
1310 if (pte_page && level == end_lvl)
1313 pte = &pte[PM_LEVEL_INDEX(level, address)];
1320 * This function checks if there is a PTE for a given dma address. If
1321 * there is one, it returns the pointer to it.
1323 static u64 *fetch_pte(struct protection_domain *domain,
1324 unsigned long address,
1325 unsigned long *page_size)
1330 if (address > PM_LEVEL_SIZE(domain->mode))
1333 level = domain->mode - 1;
1334 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1335 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1340 if (!IOMMU_PTE_PRESENT(*pte))
1344 if (PM_PTE_LEVEL(*pte) == 7 ||
1345 PM_PTE_LEVEL(*pte) == 0)
1348 /* No level skipping support yet */
1349 if (PM_PTE_LEVEL(*pte) != level)
1354 /* Walk to the next level */
1355 pte = IOMMU_PTE_PAGE(*pte);
1356 pte = &pte[PM_LEVEL_INDEX(level, address)];
1357 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1360 if (PM_PTE_LEVEL(*pte) == 0x07) {
1361 unsigned long pte_mask;
1364 * If we have a series of large PTEs, make
1365 * sure to return a pointer to the first one.
1367 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1368 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1369 pte = (u64 *)(((unsigned long)pte) & pte_mask);
1376 * Generic mapping functions. It maps a physical address into a DMA
1377 * address space. It allocates the page table pages if necessary.
1378 * In the future it can be extended to a generic mapping function
1379 * supporting all features of AMD IOMMU page tables like level skipping
1380 * and full 64 bit address spaces.
1382 static int iommu_map_page(struct protection_domain *dom,
1383 unsigned long bus_addr,
1384 unsigned long phys_addr,
1385 unsigned long page_size,
1392 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1393 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1395 if (!(prot & IOMMU_PROT_MASK))
1398 count = PAGE_SIZE_PTE_COUNT(page_size);
1399 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
1404 for (i = 0; i < count; ++i)
1405 if (IOMMU_PTE_PRESENT(pte[i]))
1409 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1410 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1412 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1414 if (prot & IOMMU_PROT_IR)
1415 __pte |= IOMMU_PTE_IR;
1416 if (prot & IOMMU_PROT_IW)
1417 __pte |= IOMMU_PTE_IW;
1419 for (i = 0; i < count; ++i)
1427 static unsigned long iommu_unmap_page(struct protection_domain *dom,
1428 unsigned long bus_addr,
1429 unsigned long page_size)
1431 unsigned long long unmapped;
1432 unsigned long unmap_size;
1435 BUG_ON(!is_power_of_2(page_size));
1439 while (unmapped < page_size) {
1441 pte = fetch_pte(dom, bus_addr, &unmap_size);
1446 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1447 for (i = 0; i < count; i++)
1451 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1452 unmapped += unmap_size;
1455 BUG_ON(unmapped && !is_power_of_2(unmapped));
1460 /****************************************************************************
1462 * The next functions belong to the address allocator for the dma_ops
1463 * interface functions.
1465 ****************************************************************************/
1468 static unsigned long dma_ops_alloc_iova(struct device *dev,
1469 struct dma_ops_domain *dma_dom,
1470 unsigned int pages, u64 dma_mask)
1472 unsigned long pfn = 0;
1474 pages = __roundup_pow_of_two(pages);
1476 if (dma_mask > DMA_BIT_MASK(32))
1477 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
1478 IOVA_PFN(DMA_BIT_MASK(32)));
1481 pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask));
1483 return (pfn << PAGE_SHIFT);
1486 static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
1487 unsigned long address,
1490 pages = __roundup_pow_of_two(pages);
1491 address >>= PAGE_SHIFT;
1493 free_iova_fast(&dma_dom->iovad, address, pages);
1496 /****************************************************************************
1498 * The next functions belong to the domain allocation. A domain is
1499 * allocated for every IOMMU as the default domain. If device isolation
1500 * is enabled, every device get its own domain. The most important thing
1501 * about domains is the page table mapping the DMA address space they
1504 ****************************************************************************/
1507 * This function adds a protection domain to the global protection domain list
1509 static void add_domain_to_list(struct protection_domain *domain)
1511 unsigned long flags;
1513 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1514 list_add(&domain->list, &amd_iommu_pd_list);
1515 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1519 * This function removes a protection domain to the global
1520 * protection domain list
1522 static void del_domain_from_list(struct protection_domain *domain)
1524 unsigned long flags;
1526 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1527 list_del(&domain->list);
1528 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1531 static u16 domain_id_alloc(void)
1533 unsigned long flags;
1536 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1537 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1539 if (id > 0 && id < MAX_DOMAIN_ID)
1540 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1543 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1548 static void domain_id_free(int id)
1550 unsigned long flags;
1552 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1553 if (id > 0 && id < MAX_DOMAIN_ID)
1554 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1555 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1558 #define DEFINE_FREE_PT_FN(LVL, FN) \
1559 static void free_pt_##LVL (unsigned long __pt) \
1567 for (i = 0; i < 512; ++i) { \
1568 /* PTE present? */ \
1569 if (!IOMMU_PTE_PRESENT(pt[i])) \
1573 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1574 PM_PTE_LEVEL(pt[i]) == 7) \
1577 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1580 free_page((unsigned long)pt); \
1583 DEFINE_FREE_PT_FN(l2, free_page)
1584 DEFINE_FREE_PT_FN(l3, free_pt_l2)
1585 DEFINE_FREE_PT_FN(l4, free_pt_l3)
1586 DEFINE_FREE_PT_FN(l5, free_pt_l4)
1587 DEFINE_FREE_PT_FN(l6, free_pt_l5)
1589 static void free_pagetable(struct protection_domain *domain)
1591 unsigned long root = (unsigned long)domain->pt_root;
1593 switch (domain->mode) {
1594 case PAGE_MODE_NONE:
1596 case PAGE_MODE_1_LEVEL:
1599 case PAGE_MODE_2_LEVEL:
1602 case PAGE_MODE_3_LEVEL:
1605 case PAGE_MODE_4_LEVEL:
1608 case PAGE_MODE_5_LEVEL:
1611 case PAGE_MODE_6_LEVEL:
1619 static void free_gcr3_tbl_level1(u64 *tbl)
1624 for (i = 0; i < 512; ++i) {
1625 if (!(tbl[i] & GCR3_VALID))
1628 ptr = __va(tbl[i] & PAGE_MASK);
1630 free_page((unsigned long)ptr);
1634 static void free_gcr3_tbl_level2(u64 *tbl)
1639 for (i = 0; i < 512; ++i) {
1640 if (!(tbl[i] & GCR3_VALID))
1643 ptr = __va(tbl[i] & PAGE_MASK);
1645 free_gcr3_tbl_level1(ptr);
1649 static void free_gcr3_table(struct protection_domain *domain)
1651 if (domain->glx == 2)
1652 free_gcr3_tbl_level2(domain->gcr3_tbl);
1653 else if (domain->glx == 1)
1654 free_gcr3_tbl_level1(domain->gcr3_tbl);
1656 BUG_ON(domain->glx != 0);
1658 free_page((unsigned long)domain->gcr3_tbl);
1662 * Free a domain, only used if something went wrong in the
1663 * allocation path and we need to free an already allocated page table
1665 static void dma_ops_domain_free(struct dma_ops_domain *dom)
1670 del_domain_from_list(&dom->domain);
1672 put_iova_domain(&dom->iovad);
1674 free_pagetable(&dom->domain);
1680 * Allocates a new protection domain usable for the dma_ops functions.
1681 * It also initializes the page table and the address allocator data
1682 * structures required for the dma_ops interface
1684 static struct dma_ops_domain *dma_ops_domain_alloc(void)
1686 struct dma_ops_domain *dma_dom;
1688 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1692 if (protection_domain_init(&dma_dom->domain))
1695 dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
1696 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
1697 dma_dom->domain.flags = PD_DMA_OPS_MASK;
1698 if (!dma_dom->domain.pt_root)
1701 init_iova_domain(&dma_dom->iovad, PAGE_SIZE,
1702 IOVA_START_PFN, DMA_32BIT_PFN);
1704 /* Initialize reserved ranges */
1705 copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
1707 add_domain_to_list(&dma_dom->domain);
1712 dma_ops_domain_free(dma_dom);
1718 * little helper function to check whether a given protection domain is a
1721 static bool dma_ops_domain(struct protection_domain *domain)
1723 return domain->flags & PD_DMA_OPS_MASK;
1726 static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
1731 if (domain->mode != PAGE_MODE_NONE)
1732 pte_root = virt_to_phys(domain->pt_root);
1734 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1735 << DEV_ENTRY_MODE_SHIFT;
1736 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
1738 flags = amd_iommu_dev_table[devid].data[1];
1741 flags |= DTE_FLAG_IOTLB;
1743 if (domain->flags & PD_IOMMUV2_MASK) {
1744 u64 gcr3 = __pa(domain->gcr3_tbl);
1745 u64 glx = domain->glx;
1748 pte_root |= DTE_FLAG_GV;
1749 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1751 /* First mask out possible old values for GCR3 table */
1752 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1755 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1758 /* Encode GCR3 table into DTE */
1759 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1762 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1765 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1769 flags &= ~(0xffffUL);
1770 flags |= domain->id;
1772 amd_iommu_dev_table[devid].data[1] = flags;
1773 amd_iommu_dev_table[devid].data[0] = pte_root;
1776 static void clear_dte_entry(u16 devid)
1778 /* remove entry from the device table seen by the hardware */
1779 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1780 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
1782 amd_iommu_apply_erratum_63(devid);
1785 static void do_attach(struct iommu_dev_data *dev_data,
1786 struct protection_domain *domain)
1788 struct amd_iommu *iommu;
1792 iommu = amd_iommu_rlookup_table[dev_data->devid];
1793 alias = dev_data->alias;
1794 ats = dev_data->ats.enabled;
1796 /* Update data structures */
1797 dev_data->domain = domain;
1798 list_add(&dev_data->list, &domain->dev_list);
1800 /* Do reference counting */
1801 domain->dev_iommu[iommu->index] += 1;
1802 domain->dev_cnt += 1;
1804 /* Update device table */
1805 set_dte_entry(dev_data->devid, domain, ats);
1806 if (alias != dev_data->devid)
1807 set_dte_entry(alias, domain, ats);
1809 device_flush_dte(dev_data);
1812 static void do_detach(struct iommu_dev_data *dev_data)
1814 struct amd_iommu *iommu;
1818 * First check if the device is still attached. It might already
1819 * be detached from its domain because the generic
1820 * iommu_detach_group code detached it and we try again here in
1821 * our alias handling.
1823 if (!dev_data->domain)
1826 iommu = amd_iommu_rlookup_table[dev_data->devid];
1827 alias = dev_data->alias;
1829 /* decrease reference counters */
1830 dev_data->domain->dev_iommu[iommu->index] -= 1;
1831 dev_data->domain->dev_cnt -= 1;
1833 /* Update data structures */
1834 dev_data->domain = NULL;
1835 list_del(&dev_data->list);
1836 clear_dte_entry(dev_data->devid);
1837 if (alias != dev_data->devid)
1838 clear_dte_entry(alias);
1840 /* Flush the DTE entry */
1841 device_flush_dte(dev_data);
1845 * If a device is not yet associated with a domain, this function does
1846 * assigns it visible for the hardware
1848 static int __attach_device(struct iommu_dev_data *dev_data,
1849 struct protection_domain *domain)
1854 * Must be called with IRQs disabled. Warn here to detect early
1857 WARN_ON(!irqs_disabled());
1860 spin_lock(&domain->lock);
1863 if (dev_data->domain != NULL)
1866 /* Attach alias group root */
1867 do_attach(dev_data, domain);
1874 spin_unlock(&domain->lock);
1880 static void pdev_iommuv2_disable(struct pci_dev *pdev)
1882 pci_disable_ats(pdev);
1883 pci_disable_pri(pdev);
1884 pci_disable_pasid(pdev);
1887 /* FIXME: Change generic reset-function to do the same */
1888 static int pri_reset_while_enabled(struct pci_dev *pdev)
1893 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
1897 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1898 control |= PCI_PRI_CTRL_RESET;
1899 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
1904 static int pdev_iommuv2_enable(struct pci_dev *pdev)
1909 /* FIXME: Hardcode number of outstanding requests for now */
1911 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
1913 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
1915 /* Only allow access to user-accessible pages */
1916 ret = pci_enable_pasid(pdev, 0);
1920 /* First reset the PRI state of the device */
1921 ret = pci_reset_pri(pdev);
1926 ret = pci_enable_pri(pdev, reqs);
1931 ret = pri_reset_while_enabled(pdev);
1936 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1943 pci_disable_pri(pdev);
1944 pci_disable_pasid(pdev);
1949 /* FIXME: Move this to PCI code */
1950 #define PCI_PRI_TLP_OFF (1 << 15)
1952 static bool pci_pri_tlp_required(struct pci_dev *pdev)
1957 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
1961 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
1963 return (status & PCI_PRI_TLP_OFF) ? true : false;
1967 * If a device is not yet associated with a domain, this function
1968 * assigns it visible for the hardware
1970 static int attach_device(struct device *dev,
1971 struct protection_domain *domain)
1973 struct pci_dev *pdev;
1974 struct iommu_dev_data *dev_data;
1975 unsigned long flags;
1978 dev_data = get_dev_data(dev);
1980 if (!dev_is_pci(dev))
1981 goto skip_ats_check;
1983 pdev = to_pci_dev(dev);
1984 if (domain->flags & PD_IOMMUV2_MASK) {
1985 if (!dev_data->passthrough)
1988 if (dev_data->iommu_v2) {
1989 if (pdev_iommuv2_enable(pdev) != 0)
1992 dev_data->ats.enabled = true;
1993 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1994 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
1996 } else if (amd_iommu_iotlb_sup &&
1997 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1998 dev_data->ats.enabled = true;
1999 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2003 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2004 ret = __attach_device(dev_data, domain);
2005 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2008 * We might boot into a crash-kernel here. The crashed kernel
2009 * left the caches in the IOMMU dirty. So we have to flush
2010 * here to evict all dirty stuff.
2012 domain_flush_tlb_pde(domain);
2018 * Removes a device from a protection domain (unlocked)
2020 static void __detach_device(struct iommu_dev_data *dev_data)
2022 struct protection_domain *domain;
2025 * Must be called with IRQs disabled. Warn here to detect early
2028 WARN_ON(!irqs_disabled());
2030 if (WARN_ON(!dev_data->domain))
2033 domain = dev_data->domain;
2035 spin_lock(&domain->lock);
2037 do_detach(dev_data);
2039 spin_unlock(&domain->lock);
2043 * Removes a device from a protection domain (with devtable_lock held)
2045 static void detach_device(struct device *dev)
2047 struct protection_domain *domain;
2048 struct iommu_dev_data *dev_data;
2049 unsigned long flags;
2051 dev_data = get_dev_data(dev);
2052 domain = dev_data->domain;
2054 /* lock device table */
2055 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2056 __detach_device(dev_data);
2057 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2059 if (!dev_is_pci(dev))
2062 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
2063 pdev_iommuv2_disable(to_pci_dev(dev));
2064 else if (dev_data->ats.enabled)
2065 pci_disable_ats(to_pci_dev(dev));
2067 dev_data->ats.enabled = false;
2070 static int amd_iommu_add_device(struct device *dev)
2072 struct iommu_dev_data *dev_data;
2073 struct iommu_domain *domain;
2074 struct amd_iommu *iommu;
2077 if (!check_device(dev) || get_dev_data(dev))
2080 devid = get_device_id(dev);
2084 iommu = amd_iommu_rlookup_table[devid];
2086 ret = iommu_init_device(dev);
2088 if (ret != -ENOTSUPP)
2089 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2092 iommu_ignore_device(dev);
2093 dev->archdata.dma_ops = &nommu_dma_ops;
2096 init_iommu_group(dev);
2098 dev_data = get_dev_data(dev);
2102 if (iommu_pass_through || dev_data->iommu_v2)
2103 iommu_request_dm_for_dev(dev);
2105 /* Domains are initialized for this device - have a look what we ended up with */
2106 domain = iommu_get_domain_for_dev(dev);
2107 if (domain->type == IOMMU_DOMAIN_IDENTITY)
2108 dev_data->passthrough = true;
2110 dev->archdata.dma_ops = &amd_iommu_dma_ops;
2113 iommu_completion_wait(iommu);
2118 static void amd_iommu_remove_device(struct device *dev)
2120 struct amd_iommu *iommu;
2123 if (!check_device(dev))
2126 devid = get_device_id(dev);
2130 iommu = amd_iommu_rlookup_table[devid];
2132 iommu_uninit_device(dev);
2133 iommu_completion_wait(iommu);
2136 static struct iommu_group *amd_iommu_device_group(struct device *dev)
2138 if (dev_is_pci(dev))
2139 return pci_device_group(dev);
2141 return acpihid_device_group(dev);
2144 /*****************************************************************************
2146 * The next functions belong to the dma_ops mapping/unmapping code.
2148 *****************************************************************************/
2150 static void __queue_flush(struct flush_queue *queue)
2152 struct protection_domain *domain;
2153 unsigned long flags;
2156 /* First flush TLB of all known domains */
2157 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
2158 list_for_each_entry(domain, &amd_iommu_pd_list, list)
2159 domain_flush_tlb(domain);
2160 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
2162 /* Wait until flushes have completed */
2163 domain_flush_complete(NULL);
2165 for (idx = 0; idx < queue->next; ++idx) {
2166 struct flush_queue_entry *entry;
2168 entry = queue->entries + idx;
2170 free_iova_fast(&entry->dma_dom->iovad,
2174 /* Not really necessary, just to make sure we catch any bugs */
2175 entry->dma_dom = NULL;
2181 static void queue_flush_all(void)
2185 for_each_possible_cpu(cpu) {
2186 struct flush_queue *queue;
2187 unsigned long flags;
2189 queue = per_cpu_ptr(&flush_queue, cpu);
2190 spin_lock_irqsave(&queue->lock, flags);
2191 if (queue->next > 0)
2192 __queue_flush(queue);
2193 spin_unlock_irqrestore(&queue->lock, flags);
2197 static void queue_flush_timeout(unsigned long unsused)
2199 atomic_set(&queue_timer_on, 0);
2203 static void queue_add(struct dma_ops_domain *dma_dom,
2204 unsigned long address, unsigned long pages)
2206 struct flush_queue_entry *entry;
2207 struct flush_queue *queue;
2208 unsigned long flags;
2211 pages = __roundup_pow_of_two(pages);
2212 address >>= PAGE_SHIFT;
2214 queue = get_cpu_ptr(&flush_queue);
2215 spin_lock_irqsave(&queue->lock, flags);
2217 if (queue->next == FLUSH_QUEUE_SIZE)
2218 __queue_flush(queue);
2220 idx = queue->next++;
2221 entry = queue->entries + idx;
2223 entry->iova_pfn = address;
2224 entry->pages = pages;
2225 entry->dma_dom = dma_dom;
2227 spin_unlock_irqrestore(&queue->lock, flags);
2229 if (atomic_cmpxchg(&queue_timer_on, 0, 1) == 0)
2230 mod_timer(&queue_timer, jiffies + msecs_to_jiffies(10));
2232 put_cpu_ptr(&flush_queue);
2237 * In the dma_ops path we only have the struct device. This function
2238 * finds the corresponding IOMMU, the protection domain and the
2239 * requestor id for a given device.
2240 * If the device is not yet associated with a domain this is also done
2243 static struct protection_domain *get_domain(struct device *dev)
2245 struct protection_domain *domain;
2247 if (!check_device(dev))
2248 return ERR_PTR(-EINVAL);
2250 domain = get_dev_data(dev)->domain;
2251 if (!dma_ops_domain(domain))
2252 return ERR_PTR(-EBUSY);
2257 static void update_device_table(struct protection_domain *domain)
2259 struct iommu_dev_data *dev_data;
2261 list_for_each_entry(dev_data, &domain->dev_list, list) {
2262 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
2264 if (dev_data->devid == dev_data->alias)
2267 /* There is an alias, update device table entry for it */
2268 set_dte_entry(dev_data->alias, domain, dev_data->ats.enabled);
2272 static void update_domain(struct protection_domain *domain)
2274 if (!domain->updated)
2277 update_device_table(domain);
2279 domain_flush_devices(domain);
2280 domain_flush_tlb_pde(domain);
2282 domain->updated = false;
2285 static int dir2prot(enum dma_data_direction direction)
2287 if (direction == DMA_TO_DEVICE)
2288 return IOMMU_PROT_IR;
2289 else if (direction == DMA_FROM_DEVICE)
2290 return IOMMU_PROT_IW;
2291 else if (direction == DMA_BIDIRECTIONAL)
2292 return IOMMU_PROT_IW | IOMMU_PROT_IR;
2297 * This function contains common code for mapping of a physically
2298 * contiguous memory region into DMA address space. It is used by all
2299 * mapping functions provided with this IOMMU driver.
2300 * Must be called with the domain lock held.
2302 static dma_addr_t __map_single(struct device *dev,
2303 struct dma_ops_domain *dma_dom,
2306 enum dma_data_direction direction,
2309 dma_addr_t offset = paddr & ~PAGE_MASK;
2310 dma_addr_t address, start, ret;
2315 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
2318 address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
2319 if (address == DMA_ERROR_CODE)
2322 prot = dir2prot(direction);
2325 for (i = 0; i < pages; ++i) {
2326 ret = iommu_map_page(&dma_dom->domain, start, paddr,
2327 PAGE_SIZE, prot, GFP_ATOMIC);
2336 if (unlikely(amd_iommu_np_cache)) {
2337 domain_flush_pages(&dma_dom->domain, address, size);
2338 domain_flush_complete(&dma_dom->domain);
2346 for (--i; i >= 0; --i) {
2348 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
2351 domain_flush_tlb(&dma_dom->domain);
2352 domain_flush_complete(&dma_dom->domain);
2354 dma_ops_free_iova(dma_dom, address, pages);
2356 return DMA_ERROR_CODE;
2360 * Does the reverse of the __map_single function. Must be called with
2361 * the domain lock held too
2363 static void __unmap_single(struct dma_ops_domain *dma_dom,
2364 dma_addr_t dma_addr,
2368 dma_addr_t flush_addr;
2369 dma_addr_t i, start;
2372 flush_addr = dma_addr;
2373 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
2374 dma_addr &= PAGE_MASK;
2377 for (i = 0; i < pages; ++i) {
2378 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
2382 if (amd_iommu_unmap_flush) {
2383 dma_ops_free_iova(dma_dom, dma_addr, pages);
2384 domain_flush_tlb(&dma_dom->domain);
2385 domain_flush_complete(&dma_dom->domain);
2387 queue_add(dma_dom, dma_addr, pages);
2392 * The exported map_single function for dma_ops.
2394 static dma_addr_t map_page(struct device *dev, struct page *page,
2395 unsigned long offset, size_t size,
2396 enum dma_data_direction dir,
2397 unsigned long attrs)
2399 phys_addr_t paddr = page_to_phys(page) + offset;
2400 struct protection_domain *domain;
2401 struct dma_ops_domain *dma_dom;
2404 domain = get_domain(dev);
2405 if (PTR_ERR(domain) == -EINVAL)
2406 return (dma_addr_t)paddr;
2407 else if (IS_ERR(domain))
2408 return DMA_ERROR_CODE;
2410 dma_mask = *dev->dma_mask;
2411 dma_dom = to_dma_ops_domain(domain);
2413 return __map_single(dev, dma_dom, paddr, size, dir, dma_mask);
2417 * The exported unmap_single function for dma_ops.
2419 static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2420 enum dma_data_direction dir, unsigned long attrs)
2422 struct protection_domain *domain;
2423 struct dma_ops_domain *dma_dom;
2425 domain = get_domain(dev);
2429 dma_dom = to_dma_ops_domain(domain);
2431 __unmap_single(dma_dom, dma_addr, size, dir);
2434 static int sg_num_pages(struct device *dev,
2435 struct scatterlist *sglist,
2438 unsigned long mask, boundary_size;
2439 struct scatterlist *s;
2442 mask = dma_get_seg_boundary(dev);
2443 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
2444 1UL << (BITS_PER_LONG - PAGE_SHIFT);
2446 for_each_sg(sglist, s, nelems, i) {
2449 s->dma_address = npages << PAGE_SHIFT;
2450 p = npages % boundary_size;
2451 n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2452 if (p + n > boundary_size)
2453 npages += boundary_size - p;
2461 * The exported map_sg function for dma_ops (handles scatter-gather
2464 static int map_sg(struct device *dev, struct scatterlist *sglist,
2465 int nelems, enum dma_data_direction direction,
2466 unsigned long attrs)
2468 int mapped_pages = 0, npages = 0, prot = 0, i;
2469 struct protection_domain *domain;
2470 struct dma_ops_domain *dma_dom;
2471 struct scatterlist *s;
2472 unsigned long address;
2475 domain = get_domain(dev);
2479 dma_dom = to_dma_ops_domain(domain);
2480 dma_mask = *dev->dma_mask;
2482 npages = sg_num_pages(dev, sglist, nelems);
2484 address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
2485 if (address == DMA_ERROR_CODE)
2488 prot = dir2prot(direction);
2490 /* Map all sg entries */
2491 for_each_sg(sglist, s, nelems, i) {
2492 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2494 for (j = 0; j < pages; ++j) {
2495 unsigned long bus_addr, phys_addr;
2498 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2499 phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
2500 ret = iommu_map_page(domain, bus_addr, phys_addr, PAGE_SIZE, prot, GFP_ATOMIC);
2508 /* Everything is mapped - write the right values into s->dma_address */
2509 for_each_sg(sglist, s, nelems, i) {
2510 s->dma_address += address + s->offset;
2511 s->dma_length = s->length;
2517 pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
2518 dev_name(dev), npages);
2520 for_each_sg(sglist, s, nelems, i) {
2521 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2523 for (j = 0; j < pages; ++j) {
2524 unsigned long bus_addr;
2526 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2527 iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
2535 free_iova_fast(&dma_dom->iovad, address, npages);
2542 * The exported map_sg function for dma_ops (handles scatter-gather
2545 static void unmap_sg(struct device *dev, struct scatterlist *sglist,
2546 int nelems, enum dma_data_direction dir,
2547 unsigned long attrs)
2549 struct protection_domain *domain;
2550 struct dma_ops_domain *dma_dom;
2551 unsigned long startaddr;
2554 domain = get_domain(dev);
2558 startaddr = sg_dma_address(sglist) & PAGE_MASK;
2559 dma_dom = to_dma_ops_domain(domain);
2560 npages = sg_num_pages(dev, sglist, nelems);
2562 __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir);
2566 * The exported alloc_coherent function for dma_ops.
2568 static void *alloc_coherent(struct device *dev, size_t size,
2569 dma_addr_t *dma_addr, gfp_t flag,
2570 unsigned long attrs)
2572 u64 dma_mask = dev->coherent_dma_mask;
2573 struct protection_domain *domain;
2574 struct dma_ops_domain *dma_dom;
2577 domain = get_domain(dev);
2578 if (PTR_ERR(domain) == -EINVAL) {
2579 page = alloc_pages(flag, get_order(size));
2580 *dma_addr = page_to_phys(page);
2581 return page_address(page);
2582 } else if (IS_ERR(domain))
2585 dma_dom = to_dma_ops_domain(domain);
2586 size = PAGE_ALIGN(size);
2587 dma_mask = dev->coherent_dma_mask;
2588 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
2591 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2593 if (!gfpflags_allow_blocking(flag))
2596 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2603 dma_mask = *dev->dma_mask;
2605 *dma_addr = __map_single(dev, dma_dom, page_to_phys(page),
2606 size, DMA_BIDIRECTIONAL, dma_mask);
2608 if (*dma_addr == DMA_ERROR_CODE)
2611 return page_address(page);
2615 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2616 __free_pages(page, get_order(size));
2622 * The exported free_coherent function for dma_ops.
2624 static void free_coherent(struct device *dev, size_t size,
2625 void *virt_addr, dma_addr_t dma_addr,
2626 unsigned long attrs)
2628 struct protection_domain *domain;
2629 struct dma_ops_domain *dma_dom;
2632 page = virt_to_page(virt_addr);
2633 size = PAGE_ALIGN(size);
2635 domain = get_domain(dev);
2639 dma_dom = to_dma_ops_domain(domain);
2641 __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL);
2644 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2645 __free_pages(page, get_order(size));
2649 * This function is called by the DMA layer to find out if we can handle a
2650 * particular device. It is part of the dma_ops.
2652 static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2654 return check_device(dev);
2657 static struct dma_map_ops amd_iommu_dma_ops = {
2658 .alloc = alloc_coherent,
2659 .free = free_coherent,
2660 .map_page = map_page,
2661 .unmap_page = unmap_page,
2663 .unmap_sg = unmap_sg,
2664 .dma_supported = amd_iommu_dma_supported,
2667 static int init_reserved_iova_ranges(void)
2669 struct pci_dev *pdev = NULL;
2672 init_iova_domain(&reserved_iova_ranges, PAGE_SIZE,
2673 IOVA_START_PFN, DMA_32BIT_PFN);
2675 lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
2676 &reserved_rbtree_key);
2678 /* MSI memory range */
2679 val = reserve_iova(&reserved_iova_ranges,
2680 IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
2682 pr_err("Reserving MSI range failed\n");
2686 /* HT memory range */
2687 val = reserve_iova(&reserved_iova_ranges,
2688 IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
2690 pr_err("Reserving HT range failed\n");
2695 * Memory used for PCI resources
2696 * FIXME: Check whether we can reserve the PCI-hole completly
2698 for_each_pci_dev(pdev) {
2701 for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
2702 struct resource *r = &pdev->resource[i];
2704 if (!(r->flags & IORESOURCE_MEM))
2707 val = reserve_iova(&reserved_iova_ranges,
2711 pr_err("Reserve pci-resource range failed\n");
2720 int __init amd_iommu_init_api(void)
2722 int ret, cpu, err = 0;
2724 ret = iova_cache_get();
2728 ret = init_reserved_iova_ranges();
2732 for_each_possible_cpu(cpu) {
2733 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2735 queue->entries = kzalloc(FLUSH_QUEUE_SIZE *
2736 sizeof(*queue->entries),
2738 if (!queue->entries)
2741 spin_lock_init(&queue->lock);
2744 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2747 #ifdef CONFIG_ARM_AMBA
2748 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2752 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2758 for_each_possible_cpu(cpu) {
2759 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2761 kfree(queue->entries);
2767 int __init amd_iommu_init_dma_ops(void)
2769 setup_timer(&queue_timer, queue_flush_timeout, 0);
2770 atomic_set(&queue_timer_on, 0);
2772 swiotlb = iommu_pass_through ? 1 : 0;
2776 * In case we don't initialize SWIOTLB (actually the common case
2777 * when AMD IOMMU is enabled), make sure there are global
2778 * dma_ops set as a fall-back for devices not handled by this
2779 * driver (for example non-PCI devices).
2782 dma_ops = &nommu_dma_ops;
2784 if (amd_iommu_unmap_flush)
2785 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2787 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2793 /*****************************************************************************
2795 * The following functions belong to the exported interface of AMD IOMMU
2797 * This interface allows access to lower level functions of the IOMMU
2798 * like protection domain handling and assignement of devices to domains
2799 * which is not possible with the dma_ops interface.
2801 *****************************************************************************/
2803 static void cleanup_domain(struct protection_domain *domain)
2805 struct iommu_dev_data *entry;
2806 unsigned long flags;
2808 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2810 while (!list_empty(&domain->dev_list)) {
2811 entry = list_first_entry(&domain->dev_list,
2812 struct iommu_dev_data, list);
2813 __detach_device(entry);
2816 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2819 static void protection_domain_free(struct protection_domain *domain)
2824 del_domain_from_list(domain);
2827 domain_id_free(domain->id);
2832 static int protection_domain_init(struct protection_domain *domain)
2834 spin_lock_init(&domain->lock);
2835 mutex_init(&domain->api_lock);
2836 domain->id = domain_id_alloc();
2839 INIT_LIST_HEAD(&domain->dev_list);
2844 static struct protection_domain *protection_domain_alloc(void)
2846 struct protection_domain *domain;
2848 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2852 if (protection_domain_init(domain))
2855 add_domain_to_list(domain);
2865 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2867 struct protection_domain *pdomain;
2868 struct dma_ops_domain *dma_domain;
2871 case IOMMU_DOMAIN_UNMANAGED:
2872 pdomain = protection_domain_alloc();
2876 pdomain->mode = PAGE_MODE_3_LEVEL;
2877 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2878 if (!pdomain->pt_root) {
2879 protection_domain_free(pdomain);
2883 pdomain->domain.geometry.aperture_start = 0;
2884 pdomain->domain.geometry.aperture_end = ~0ULL;
2885 pdomain->domain.geometry.force_aperture = true;
2888 case IOMMU_DOMAIN_DMA:
2889 dma_domain = dma_ops_domain_alloc();
2891 pr_err("AMD-Vi: Failed to allocate\n");
2894 pdomain = &dma_domain->domain;
2896 case IOMMU_DOMAIN_IDENTITY:
2897 pdomain = protection_domain_alloc();
2901 pdomain->mode = PAGE_MODE_NONE;
2907 return &pdomain->domain;
2910 static void amd_iommu_domain_free(struct iommu_domain *dom)
2912 struct protection_domain *domain;
2913 struct dma_ops_domain *dma_dom;
2915 domain = to_pdomain(dom);
2917 if (domain->dev_cnt > 0)
2918 cleanup_domain(domain);
2920 BUG_ON(domain->dev_cnt != 0);
2925 switch (dom->type) {
2926 case IOMMU_DOMAIN_DMA:
2928 * First make sure the domain is no longer referenced from the
2933 /* Now release the domain */
2934 dma_dom = to_dma_ops_domain(domain);
2935 dma_ops_domain_free(dma_dom);
2938 if (domain->mode != PAGE_MODE_NONE)
2939 free_pagetable(domain);
2941 if (domain->flags & PD_IOMMUV2_MASK)
2942 free_gcr3_table(domain);
2944 protection_domain_free(domain);
2949 static void amd_iommu_detach_device(struct iommu_domain *dom,
2952 struct iommu_dev_data *dev_data = dev->archdata.iommu;
2953 struct amd_iommu *iommu;
2956 if (!check_device(dev))
2959 devid = get_device_id(dev);
2963 if (dev_data->domain != NULL)
2966 iommu = amd_iommu_rlookup_table[devid];
2970 iommu_completion_wait(iommu);
2973 static int amd_iommu_attach_device(struct iommu_domain *dom,
2976 struct protection_domain *domain = to_pdomain(dom);
2977 struct iommu_dev_data *dev_data;
2978 struct amd_iommu *iommu;
2981 if (!check_device(dev))
2984 dev_data = dev->archdata.iommu;
2986 iommu = amd_iommu_rlookup_table[dev_data->devid];
2990 if (dev_data->domain)
2993 ret = attach_device(dev, domain);
2995 iommu_completion_wait(iommu);
3000 static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
3001 phys_addr_t paddr, size_t page_size, int iommu_prot)
3003 struct protection_domain *domain = to_pdomain(dom);
3007 if (domain->mode == PAGE_MODE_NONE)
3010 if (iommu_prot & IOMMU_READ)
3011 prot |= IOMMU_PROT_IR;
3012 if (iommu_prot & IOMMU_WRITE)
3013 prot |= IOMMU_PROT_IW;
3015 mutex_lock(&domain->api_lock);
3016 ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
3017 mutex_unlock(&domain->api_lock);
3022 static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3025 struct protection_domain *domain = to_pdomain(dom);
3028 if (domain->mode == PAGE_MODE_NONE)
3031 mutex_lock(&domain->api_lock);
3032 unmap_size = iommu_unmap_page(domain, iova, page_size);
3033 mutex_unlock(&domain->api_lock);
3035 domain_flush_tlb_pde(domain);
3040 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
3043 struct protection_domain *domain = to_pdomain(dom);
3044 unsigned long offset_mask, pte_pgsize;
3047 if (domain->mode == PAGE_MODE_NONE)
3050 pte = fetch_pte(domain, iova, &pte_pgsize);
3052 if (!pte || !IOMMU_PTE_PRESENT(*pte))
3055 offset_mask = pte_pgsize - 1;
3056 __pte = *pte & PM_ADDR_MASK;
3058 return (__pte & ~offset_mask) | (iova & offset_mask);
3061 static bool amd_iommu_capable(enum iommu_cap cap)
3064 case IOMMU_CAP_CACHE_COHERENCY:
3066 case IOMMU_CAP_INTR_REMAP:
3067 return (irq_remapping_enabled == 1);
3068 case IOMMU_CAP_NOEXEC:
3075 static void amd_iommu_get_dm_regions(struct device *dev,
3076 struct list_head *head)
3078 struct unity_map_entry *entry;
3081 devid = get_device_id(dev);
3085 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3086 struct iommu_dm_region *region;
3088 if (devid < entry->devid_start || devid > entry->devid_end)
3091 region = kzalloc(sizeof(*region), GFP_KERNEL);
3093 pr_err("Out of memory allocating dm-regions for %s\n",
3098 region->start = entry->address_start;
3099 region->length = entry->address_end - entry->address_start;
3100 if (entry->prot & IOMMU_PROT_IR)
3101 region->prot |= IOMMU_READ;
3102 if (entry->prot & IOMMU_PROT_IW)
3103 region->prot |= IOMMU_WRITE;
3105 list_add_tail(®ion->list, head);
3109 static void amd_iommu_put_dm_regions(struct device *dev,
3110 struct list_head *head)
3112 struct iommu_dm_region *entry, *next;
3114 list_for_each_entry_safe(entry, next, head, list)
3118 static void amd_iommu_apply_dm_region(struct device *dev,
3119 struct iommu_domain *domain,
3120 struct iommu_dm_region *region)
3122 struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain));
3123 unsigned long start, end;
3125 start = IOVA_PFN(region->start);
3126 end = IOVA_PFN(region->start + region->length);
3128 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
3131 static const struct iommu_ops amd_iommu_ops = {
3132 .capable = amd_iommu_capable,
3133 .domain_alloc = amd_iommu_domain_alloc,
3134 .domain_free = amd_iommu_domain_free,
3135 .attach_dev = amd_iommu_attach_device,
3136 .detach_dev = amd_iommu_detach_device,
3137 .map = amd_iommu_map,
3138 .unmap = amd_iommu_unmap,
3139 .map_sg = default_iommu_map_sg,
3140 .iova_to_phys = amd_iommu_iova_to_phys,
3141 .add_device = amd_iommu_add_device,
3142 .remove_device = amd_iommu_remove_device,
3143 .device_group = amd_iommu_device_group,
3144 .get_dm_regions = amd_iommu_get_dm_regions,
3145 .put_dm_regions = amd_iommu_put_dm_regions,
3146 .apply_dm_region = amd_iommu_apply_dm_region,
3147 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
3150 /*****************************************************************************
3152 * The next functions do a basic initialization of IOMMU for pass through
3155 * In passthrough mode the IOMMU is initialized and enabled but not used for
3156 * DMA-API translation.
3158 *****************************************************************************/
3160 /* IOMMUv2 specific functions */
3161 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3163 return atomic_notifier_chain_register(&ppr_notifier, nb);
3165 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3167 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3169 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3171 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
3173 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3175 struct protection_domain *domain = to_pdomain(dom);
3176 unsigned long flags;
3178 spin_lock_irqsave(&domain->lock, flags);
3180 /* Update data structure */
3181 domain->mode = PAGE_MODE_NONE;
3182 domain->updated = true;
3184 /* Make changes visible to IOMMUs */
3185 update_domain(domain);
3187 /* Page-table is not visible to IOMMU anymore, so free it */
3188 free_pagetable(domain);
3190 spin_unlock_irqrestore(&domain->lock, flags);
3192 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
3194 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3196 struct protection_domain *domain = to_pdomain(dom);
3197 unsigned long flags;
3200 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3203 /* Number of GCR3 table levels required */
3204 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3207 if (levels > amd_iommu_max_glx_val)
3210 spin_lock_irqsave(&domain->lock, flags);
3213 * Save us all sanity checks whether devices already in the
3214 * domain support IOMMUv2. Just force that the domain has no
3215 * devices attached when it is switched into IOMMUv2 mode.
3218 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3222 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3223 if (domain->gcr3_tbl == NULL)
3226 domain->glx = levels;
3227 domain->flags |= PD_IOMMUV2_MASK;
3228 domain->updated = true;
3230 update_domain(domain);
3235 spin_unlock_irqrestore(&domain->lock, flags);
3239 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
3241 static int __flush_pasid(struct protection_domain *domain, int pasid,
3242 u64 address, bool size)
3244 struct iommu_dev_data *dev_data;
3245 struct iommu_cmd cmd;
3248 if (!(domain->flags & PD_IOMMUV2_MASK))
3251 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3254 * IOMMU TLB needs to be flushed before Device TLB to
3255 * prevent device TLB refill from IOMMU TLB
3257 for (i = 0; i < amd_iommus_present; ++i) {
3258 if (domain->dev_iommu[i] == 0)
3261 ret = iommu_queue_command(amd_iommus[i], &cmd);
3266 /* Wait until IOMMU TLB flushes are complete */
3267 domain_flush_complete(domain);
3269 /* Now flush device TLBs */
3270 list_for_each_entry(dev_data, &domain->dev_list, list) {
3271 struct amd_iommu *iommu;
3275 There might be non-IOMMUv2 capable devices in an IOMMUv2
3278 if (!dev_data->ats.enabled)
3281 qdep = dev_data->ats.qdep;
3282 iommu = amd_iommu_rlookup_table[dev_data->devid];
3284 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3285 qdep, address, size);
3287 ret = iommu_queue_command(iommu, &cmd);
3292 /* Wait until all device TLBs are flushed */
3293 domain_flush_complete(domain);
3302 static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3305 return __flush_pasid(domain, pasid, address, false);
3308 int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3311 struct protection_domain *domain = to_pdomain(dom);
3312 unsigned long flags;
3315 spin_lock_irqsave(&domain->lock, flags);
3316 ret = __amd_iommu_flush_page(domain, pasid, address);
3317 spin_unlock_irqrestore(&domain->lock, flags);
3321 EXPORT_SYMBOL(amd_iommu_flush_page);
3323 static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3325 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3329 int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3331 struct protection_domain *domain = to_pdomain(dom);
3332 unsigned long flags;
3335 spin_lock_irqsave(&domain->lock, flags);
3336 ret = __amd_iommu_flush_tlb(domain, pasid);
3337 spin_unlock_irqrestore(&domain->lock, flags);
3341 EXPORT_SYMBOL(amd_iommu_flush_tlb);
3343 static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3350 index = (pasid >> (9 * level)) & 0x1ff;
3356 if (!(*pte & GCR3_VALID)) {
3360 root = (void *)get_zeroed_page(GFP_ATOMIC);
3364 *pte = __pa(root) | GCR3_VALID;
3367 root = __va(*pte & PAGE_MASK);
3375 static int __set_gcr3(struct protection_domain *domain, int pasid,
3380 if (domain->mode != PAGE_MODE_NONE)
3383 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3387 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3389 return __amd_iommu_flush_tlb(domain, pasid);
3392 static int __clear_gcr3(struct protection_domain *domain, int pasid)
3396 if (domain->mode != PAGE_MODE_NONE)
3399 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3405 return __amd_iommu_flush_tlb(domain, pasid);
3408 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3411 struct protection_domain *domain = to_pdomain(dom);
3412 unsigned long flags;
3415 spin_lock_irqsave(&domain->lock, flags);
3416 ret = __set_gcr3(domain, pasid, cr3);
3417 spin_unlock_irqrestore(&domain->lock, flags);
3421 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3423 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3425 struct protection_domain *domain = to_pdomain(dom);
3426 unsigned long flags;
3429 spin_lock_irqsave(&domain->lock, flags);
3430 ret = __clear_gcr3(domain, pasid);
3431 spin_unlock_irqrestore(&domain->lock, flags);
3435 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
3437 int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3438 int status, int tag)
3440 struct iommu_dev_data *dev_data;
3441 struct amd_iommu *iommu;
3442 struct iommu_cmd cmd;
3444 dev_data = get_dev_data(&pdev->dev);
3445 iommu = amd_iommu_rlookup_table[dev_data->devid];
3447 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3448 tag, dev_data->pri_tlp);
3450 return iommu_queue_command(iommu, &cmd);
3452 EXPORT_SYMBOL(amd_iommu_complete_ppr);
3454 struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3456 struct protection_domain *pdomain;
3458 pdomain = get_domain(&pdev->dev);
3459 if (IS_ERR(pdomain))
3462 /* Only return IOMMUv2 domains */
3463 if (!(pdomain->flags & PD_IOMMUV2_MASK))
3466 return &pdomain->domain;
3468 EXPORT_SYMBOL(amd_iommu_get_v2_domain);
3470 void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3472 struct iommu_dev_data *dev_data;
3474 if (!amd_iommu_v2_supported())
3477 dev_data = get_dev_data(&pdev->dev);
3478 dev_data->errata |= (1 << erratum);
3480 EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
3482 int amd_iommu_device_info(struct pci_dev *pdev,
3483 struct amd_iommu_device_info *info)
3488 if (pdev == NULL || info == NULL)
3491 if (!amd_iommu_v2_supported())
3494 memset(info, 0, sizeof(*info));
3496 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3498 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3500 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3502 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3504 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3508 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3509 max_pasids = min(max_pasids, (1 << 20));
3511 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3512 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3514 features = pci_pasid_features(pdev);
3515 if (features & PCI_PASID_CAP_EXEC)
3516 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3517 if (features & PCI_PASID_CAP_PRIV)
3518 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3523 EXPORT_SYMBOL(amd_iommu_device_info);
3525 #ifdef CONFIG_IRQ_REMAP
3527 /*****************************************************************************
3529 * Interrupt Remapping Implementation
3531 *****************************************************************************/
3549 u16 devid; /* Device ID for IRTE table */
3550 u16 index; /* Index into IRTE table*/
3553 struct amd_ir_data {
3554 struct irq_2_irte irq_2_irte;
3555 union irte irte_entry;
3557 struct msi_msg msi_entry;
3561 static struct irq_chip amd_ir_chip;
3563 #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3564 #define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3565 #define DTE_IRQ_TABLE_LEN (8ULL << 1)
3566 #define DTE_IRQ_REMAP_ENABLE 1ULL
3568 static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3572 dte = amd_iommu_dev_table[devid].data[2];
3573 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3574 dte |= virt_to_phys(table->table);
3575 dte |= DTE_IRQ_REMAP_INTCTL;
3576 dte |= DTE_IRQ_TABLE_LEN;
3577 dte |= DTE_IRQ_REMAP_ENABLE;
3579 amd_iommu_dev_table[devid].data[2] = dte;
3582 #define IRTE_ALLOCATED (~1U)
3584 static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3586 struct irq_remap_table *table = NULL;
3587 struct amd_iommu *iommu;
3588 unsigned long flags;
3591 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3593 iommu = amd_iommu_rlookup_table[devid];
3597 table = irq_lookup_table[devid];
3601 alias = amd_iommu_alias_table[devid];
3602 table = irq_lookup_table[alias];
3604 irq_lookup_table[devid] = table;
3605 set_dte_irq_entry(devid, table);
3606 iommu_flush_dte(iommu, devid);
3610 /* Nothing there yet, allocate new irq remapping table */
3611 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3615 /* Initialize table spin-lock */
3616 spin_lock_init(&table->lock);
3619 /* Keep the first 32 indexes free for IOAPIC interrupts */
3620 table->min_index = 32;
3622 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3623 if (!table->table) {
3629 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3634 for (i = 0; i < 32; ++i)
3635 table->table[i] = IRTE_ALLOCATED;
3638 irq_lookup_table[devid] = table;
3639 set_dte_irq_entry(devid, table);
3640 iommu_flush_dte(iommu, devid);
3641 if (devid != alias) {
3642 irq_lookup_table[alias] = table;
3643 set_dte_irq_entry(alias, table);
3644 iommu_flush_dte(iommu, alias);
3648 iommu_completion_wait(iommu);
3651 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3656 static int alloc_irq_index(u16 devid, int count)
3658 struct irq_remap_table *table;
3659 unsigned long flags;
3662 table = get_irq_table(devid, false);
3666 spin_lock_irqsave(&table->lock, flags);
3668 /* Scan table for free entries */
3669 for (c = 0, index = table->min_index;
3670 index < MAX_IRQS_PER_TABLE;
3672 if (table->table[index] == 0)
3679 table->table[index - c + 1] = IRTE_ALLOCATED;
3689 spin_unlock_irqrestore(&table->lock, flags);
3694 static int modify_irte(u16 devid, int index, union irte irte)
3696 struct irq_remap_table *table;
3697 struct amd_iommu *iommu;
3698 unsigned long flags;
3700 iommu = amd_iommu_rlookup_table[devid];
3704 table = get_irq_table(devid, false);
3708 spin_lock_irqsave(&table->lock, flags);
3709 table->table[index] = irte.val;
3710 spin_unlock_irqrestore(&table->lock, flags);
3712 iommu_flush_irt(iommu, devid);
3713 iommu_completion_wait(iommu);
3718 static void free_irte(u16 devid, int index)
3720 struct irq_remap_table *table;
3721 struct amd_iommu *iommu;
3722 unsigned long flags;
3724 iommu = amd_iommu_rlookup_table[devid];
3728 table = get_irq_table(devid, false);
3732 spin_lock_irqsave(&table->lock, flags);
3733 table->table[index] = 0;
3734 spin_unlock_irqrestore(&table->lock, flags);
3736 iommu_flush_irt(iommu, devid);
3737 iommu_completion_wait(iommu);
3740 static int get_devid(struct irq_alloc_info *info)
3744 switch (info->type) {
3745 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3746 devid = get_ioapic_devid(info->ioapic_id);
3748 case X86_IRQ_ALLOC_TYPE_HPET:
3749 devid = get_hpet_devid(info->hpet_id);
3751 case X86_IRQ_ALLOC_TYPE_MSI:
3752 case X86_IRQ_ALLOC_TYPE_MSIX:
3753 devid = get_device_id(&info->msi_dev->dev);
3763 static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
3765 struct amd_iommu *iommu;
3771 devid = get_devid(info);
3773 iommu = amd_iommu_rlookup_table[devid];
3775 return iommu->ir_domain;
3781 static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
3783 struct amd_iommu *iommu;
3789 switch (info->type) {
3790 case X86_IRQ_ALLOC_TYPE_MSI:
3791 case X86_IRQ_ALLOC_TYPE_MSIX:
3792 devid = get_device_id(&info->msi_dev->dev);
3796 iommu = amd_iommu_rlookup_table[devid];
3798 return iommu->msi_domain;
3807 struct irq_remap_ops amd_iommu_irq_ops = {
3808 .prepare = amd_iommu_prepare,
3809 .enable = amd_iommu_enable,
3810 .disable = amd_iommu_disable,
3811 .reenable = amd_iommu_reenable,
3812 .enable_faulting = amd_iommu_enable_faulting,
3813 .get_ir_irq_domain = get_ir_irq_domain,
3814 .get_irq_domain = get_irq_domain,
3817 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3818 struct irq_cfg *irq_cfg,
3819 struct irq_alloc_info *info,
3820 int devid, int index, int sub_handle)
3822 struct irq_2_irte *irte_info = &data->irq_2_irte;
3823 struct msi_msg *msg = &data->msi_entry;
3824 union irte *irte = &data->irte_entry;
3825 struct IO_APIC_route_entry *entry;
3827 data->irq_2_irte.devid = devid;
3828 data->irq_2_irte.index = index + sub_handle;
3830 /* Setup IRTE for IOMMU */
3832 irte->fields.vector = irq_cfg->vector;
3833 irte->fields.int_type = apic->irq_delivery_mode;
3834 irte->fields.destination = irq_cfg->dest_apicid;
3835 irte->fields.dm = apic->irq_dest_mode;
3836 irte->fields.valid = 1;
3838 switch (info->type) {
3839 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3840 /* Setup IOAPIC entry */
3841 entry = info->ioapic_entry;
3842 info->ioapic_entry = NULL;
3843 memset(entry, 0, sizeof(*entry));
3844 entry->vector = index;
3846 entry->trigger = info->ioapic_trigger;
3847 entry->polarity = info->ioapic_polarity;
3848 /* Mask level triggered irqs. */
3849 if (info->ioapic_trigger)
3853 case X86_IRQ_ALLOC_TYPE_HPET:
3854 case X86_IRQ_ALLOC_TYPE_MSI:
3855 case X86_IRQ_ALLOC_TYPE_MSIX:
3856 msg->address_hi = MSI_ADDR_BASE_HI;
3857 msg->address_lo = MSI_ADDR_BASE_LO;
3858 msg->data = irte_info->index;
3867 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3868 unsigned int nr_irqs, void *arg)
3870 struct irq_alloc_info *info = arg;
3871 struct irq_data *irq_data;
3872 struct amd_ir_data *data;
3873 struct irq_cfg *cfg;
3879 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3880 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3884 * With IRQ remapping enabled, don't need contiguous CPU vectors
3885 * to support multiple MSI interrupts.
3887 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3888 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3890 devid = get_devid(info);
3894 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3898 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3899 if (get_irq_table(devid, true))
3900 index = info->ioapic_pin;
3904 index = alloc_irq_index(devid, nr_irqs);
3907 pr_warn("Failed to allocate IRTE\n");
3908 goto out_free_parent;
3911 for (i = 0; i < nr_irqs; i++) {
3912 irq_data = irq_domain_get_irq_data(domain, virq + i);
3913 cfg = irqd_cfg(irq_data);
3914 if (!irq_data || !cfg) {
3920 data = kzalloc(sizeof(*data), GFP_KERNEL);
3924 irq_data->hwirq = (devid << 16) + i;
3925 irq_data->chip_data = data;
3926 irq_data->chip = &amd_ir_chip;
3927 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3928 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3934 for (i--; i >= 0; i--) {
3935 irq_data = irq_domain_get_irq_data(domain, virq + i);
3937 kfree(irq_data->chip_data);
3939 for (i = 0; i < nr_irqs; i++)
3940 free_irte(devid, index + i);
3942 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3946 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3947 unsigned int nr_irqs)
3949 struct irq_2_irte *irte_info;
3950 struct irq_data *irq_data;
3951 struct amd_ir_data *data;
3954 for (i = 0; i < nr_irqs; i++) {
3955 irq_data = irq_domain_get_irq_data(domain, virq + i);
3956 if (irq_data && irq_data->chip_data) {
3957 data = irq_data->chip_data;
3958 irte_info = &data->irq_2_irte;
3959 free_irte(irte_info->devid, irte_info->index);
3963 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3966 static void irq_remapping_activate(struct irq_domain *domain,
3967 struct irq_data *irq_data)
3969 struct amd_ir_data *data = irq_data->chip_data;
3970 struct irq_2_irte *irte_info = &data->irq_2_irte;
3972 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3975 static void irq_remapping_deactivate(struct irq_domain *domain,
3976 struct irq_data *irq_data)
3978 struct amd_ir_data *data = irq_data->chip_data;
3979 struct irq_2_irte *irte_info = &data->irq_2_irte;
3983 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3986 static struct irq_domain_ops amd_ir_domain_ops = {
3987 .alloc = irq_remapping_alloc,
3988 .free = irq_remapping_free,
3989 .activate = irq_remapping_activate,
3990 .deactivate = irq_remapping_deactivate,
3993 static int amd_ir_set_affinity(struct irq_data *data,
3994 const struct cpumask *mask, bool force)
3996 struct amd_ir_data *ir_data = data->chip_data;
3997 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3998 struct irq_cfg *cfg = irqd_cfg(data);
3999 struct irq_data *parent = data->parent_data;
4002 ret = parent->chip->irq_set_affinity(parent, mask, force);
4003 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4007 * Atomically updates the IRTE with the new destination, vector
4008 * and flushes the interrupt entry cache.
4010 ir_data->irte_entry.fields.vector = cfg->vector;
4011 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
4012 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
4015 * After this point, all the interrupts will start arriving
4016 * at the new destination. So, time to cleanup the previous
4017 * vector allocation.
4019 send_cleanup_vector(cfg);
4021 return IRQ_SET_MASK_OK_DONE;
4024 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4026 struct amd_ir_data *ir_data = irq_data->chip_data;
4028 *msg = ir_data->msi_entry;
4031 static struct irq_chip amd_ir_chip = {
4032 .irq_ack = ir_ack_apic_edge,
4033 .irq_set_affinity = amd_ir_set_affinity,
4034 .irq_compose_msi_msg = ir_compose_msi_msg,
4037 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4039 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4040 if (!iommu->ir_domain)
4043 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4044 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);