1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 * Leo Duran <leo.duran@amd.com>
8 #define pr_fmt(fmt) "AMD-Vi: " fmt
9 #define dev_fmt(fmt) pr_fmt(fmt)
11 #include <linux/ratelimit.h>
12 #include <linux/pci.h>
13 #include <linux/acpi.h>
14 #include <linux/amba/bus.h>
15 #include <linux/platform_device.h>
16 #include <linux/pci-ats.h>
17 #include <linux/bitmap.h>
18 #include <linux/slab.h>
19 #include <linux/debugfs.h>
20 #include <linux/scatterlist.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/dma-direct.h>
23 #include <linux/dma-iommu.h>
24 #include <linux/iommu-helper.h>
25 #include <linux/delay.h>
26 #include <linux/amd-iommu.h>
27 #include <linux/notifier.h>
28 #include <linux/export.h>
29 #include <linux/irq.h>
30 #include <linux/msi.h>
31 #include <linux/dma-contiguous.h>
32 #include <linux/irqdomain.h>
33 #include <linux/percpu.h>
34 #include <linux/iova.h>
35 #include <asm/irq_remapping.h>
36 #include <asm/io_apic.h>
38 #include <asm/hw_irq.h>
39 #include <asm/msidef.h>
40 #include <asm/proto.h>
41 #include <asm/iommu.h>
45 #include "amd_iommu.h"
46 #include "../irq_remapping.h"
48 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
50 #define LOOP_TIMEOUT 100000
52 /* IO virtual address start page frame number */
53 #define IOVA_START_PFN (1)
54 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
56 /* Reserved IOVA ranges */
57 #define MSI_RANGE_START (0xfee00000)
58 #define MSI_RANGE_END (0xfeefffff)
59 #define HT_RANGE_START (0xfd00000000ULL)
60 #define HT_RANGE_END (0xffffffffffULL)
63 * This bitmap is used to advertise the page sizes our hardware support
64 * to the IOMMU core, which will then use this information to split
65 * physically contiguous memory regions it is mapping into page sizes
68 * 512GB Pages are not supported due to a hardware bug
70 #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
72 #define DEFAULT_PGTABLE_LEVEL PAGE_MODE_3_LEVEL
74 static DEFINE_SPINLOCK(pd_bitmap_lock);
76 /* List of all available dev_data structures */
77 static LLIST_HEAD(dev_data_list);
79 LIST_HEAD(ioapic_map);
81 LIST_HEAD(acpihid_map);
84 * Domain for untranslated devices - only allocated
85 * if iommu=pt passed on kernel cmd line.
87 const struct iommu_ops amd_iommu_ops;
89 static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
90 int amd_iommu_max_glx_val = -1;
93 * general struct to manage commands send to an IOMMU
99 struct kmem_cache *amd_iommu_irq_cache;
101 static void update_domain(struct protection_domain *domain);
102 static void detach_device(struct device *dev);
103 static void update_and_flush_device_table(struct protection_domain *domain,
104 struct domain_pgtable *pgtable);
106 /****************************************************************************
110 ****************************************************************************/
112 static inline u16 get_pci_device_id(struct device *dev)
114 struct pci_dev *pdev = to_pci_dev(dev);
116 return pci_dev_id(pdev);
119 static inline int get_acpihid_device_id(struct device *dev,
120 struct acpihid_map_entry **entry)
122 struct acpi_device *adev = ACPI_COMPANION(dev);
123 struct acpihid_map_entry *p;
128 list_for_each_entry(p, &acpihid_map, list) {
129 if (acpi_dev_hid_uid_match(adev, p->hid,
130 p->uid[0] ? p->uid : NULL)) {
139 static inline int get_device_id(struct device *dev)
144 devid = get_pci_device_id(dev);
146 devid = get_acpihid_device_id(dev, NULL);
151 static struct protection_domain *to_pdomain(struct iommu_domain *dom)
153 return container_of(dom, struct protection_domain, domain);
156 static void amd_iommu_domain_get_pgtable(struct protection_domain *domain,
157 struct domain_pgtable *pgtable)
159 u64 pt_root = atomic64_read(&domain->pt_root);
161 pgtable->root = (u64 *)(pt_root & PAGE_MASK);
162 pgtable->mode = pt_root & 7; /* lowest 3 bits encode pgtable mode */
165 static void amd_iommu_domain_set_pt_root(struct protection_domain *domain, u64 root)
167 atomic64_set(&domain->pt_root, root);
170 static void amd_iommu_domain_clr_pt_root(struct protection_domain *domain)
172 amd_iommu_domain_set_pt_root(domain, 0);
175 static void amd_iommu_domain_set_pgtable(struct protection_domain *domain,
180 /* lowest 3 bits encode pgtable mode */
182 pt_root |= (u64)root;
184 amd_iommu_domain_set_pt_root(domain, pt_root);
187 static struct iommu_dev_data *alloc_dev_data(u16 devid)
189 struct iommu_dev_data *dev_data;
191 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
195 spin_lock_init(&dev_data->lock);
196 dev_data->devid = devid;
197 ratelimit_default_init(&dev_data->rs);
199 llist_add(&dev_data->dev_data_list, &dev_data_list);
203 static struct iommu_dev_data *search_dev_data(u16 devid)
205 struct iommu_dev_data *dev_data;
206 struct llist_node *node;
208 if (llist_empty(&dev_data_list))
211 node = dev_data_list.first;
212 llist_for_each_entry(dev_data, node, dev_data_list) {
213 if (dev_data->devid == devid)
220 static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
222 u16 devid = pci_dev_id(pdev);
227 amd_iommu_rlookup_table[alias] =
228 amd_iommu_rlookup_table[devid];
229 memcpy(amd_iommu_dev_table[alias].data,
230 amd_iommu_dev_table[devid].data,
231 sizeof(amd_iommu_dev_table[alias].data));
236 static void clone_aliases(struct pci_dev *pdev)
242 * The IVRS alias stored in the alias table may not be
243 * part of the PCI DMA aliases if it's bus differs
244 * from the original device.
246 clone_alias(pdev, amd_iommu_alias_table[pci_dev_id(pdev)], NULL);
248 pci_for_each_dma_alias(pdev, clone_alias, NULL);
251 static struct pci_dev *setup_aliases(struct device *dev)
253 struct pci_dev *pdev = to_pci_dev(dev);
256 /* For ACPI HID devices, there are no aliases */
257 if (!dev_is_pci(dev))
261 * Add the IVRS alias to the pci aliases if it is on the same
262 * bus. The IVRS table may know about a quirk that we don't.
264 ivrs_alias = amd_iommu_alias_table[pci_dev_id(pdev)];
265 if (ivrs_alias != pci_dev_id(pdev) &&
266 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number)
267 pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1);
274 static struct iommu_dev_data *find_dev_data(u16 devid)
276 struct iommu_dev_data *dev_data;
277 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
279 dev_data = search_dev_data(devid);
281 if (dev_data == NULL) {
282 dev_data = alloc_dev_data(devid);
286 if (translation_pre_enabled(iommu))
287 dev_data->defer_attach = true;
294 * Find or create an IOMMU group for a acpihid device.
296 static struct iommu_group *acpihid_device_group(struct device *dev)
298 struct acpihid_map_entry *p, *entry = NULL;
301 devid = get_acpihid_device_id(dev, &entry);
303 return ERR_PTR(devid);
305 list_for_each_entry(p, &acpihid_map, list) {
306 if ((devid == p->devid) && p->group)
307 entry->group = p->group;
311 entry->group = generic_device_group(dev);
313 iommu_group_ref_get(entry->group);
318 static bool pci_iommuv2_capable(struct pci_dev *pdev)
320 static const int caps[] = {
322 PCI_EXT_CAP_ID_PASID,
326 if (!pci_ats_supported(pdev))
329 for (i = 0; i < 2; ++i) {
330 pos = pci_find_ext_capability(pdev, caps[i]);
338 static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
340 struct iommu_dev_data *dev_data;
342 dev_data = dev_iommu_priv_get(&pdev->dev);
344 return dev_data->errata & (1 << erratum) ? true : false;
348 * This function checks if the driver got a valid device from the caller to
349 * avoid dereferencing invalid pointers.
351 static bool check_device(struct device *dev)
358 devid = get_device_id(dev);
362 /* Out of our scope? */
363 if (devid > amd_iommu_last_bdf)
366 if (amd_iommu_rlookup_table[devid] == NULL)
372 static int iommu_init_device(struct device *dev)
374 struct iommu_dev_data *dev_data;
377 if (dev_iommu_priv_get(dev))
380 devid = get_device_id(dev);
384 dev_data = find_dev_data(devid);
388 dev_data->pdev = setup_aliases(dev);
391 * By default we use passthrough mode for IOMMUv2 capable device.
392 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
393 * invalid address), we ignore the capability for the device so
394 * it'll be forced to go into translation mode.
396 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
397 dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
398 struct amd_iommu *iommu;
400 iommu = amd_iommu_rlookup_table[dev_data->devid];
401 dev_data->iommu_v2 = iommu->is_iommu_v2;
404 dev_iommu_priv_set(dev, dev_data);
409 static void iommu_ignore_device(struct device *dev)
413 devid = get_device_id(dev);
417 amd_iommu_rlookup_table[devid] = NULL;
418 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
423 static void amd_iommu_uninit_device(struct device *dev)
425 struct iommu_dev_data *dev_data;
427 dev_data = dev_iommu_priv_get(dev);
431 if (dev_data->domain)
434 dev_iommu_priv_set(dev, NULL);
437 * We keep dev_data around for unplugged devices and reuse it when the
438 * device is re-plugged - not doing so would introduce a ton of races.
443 * Helper function to get the first pte of a large mapping
445 static u64 *first_pte_l7(u64 *pte, unsigned long *page_size,
446 unsigned long *count)
448 unsigned long pte_mask, pg_size, cnt;
451 pg_size = PTE_PAGE_SIZE(*pte);
452 cnt = PAGE_SIZE_PTE_COUNT(pg_size);
453 pte_mask = ~((cnt << 3) - 1);
454 fpte = (u64 *)(((unsigned long)pte) & pte_mask);
457 *page_size = pg_size;
465 /****************************************************************************
467 * Interrupt handling functions
469 ****************************************************************************/
471 static void dump_dte_entry(u16 devid)
475 for (i = 0; i < 4; ++i)
476 pr_err("DTE[%d]: %016llx\n", i,
477 amd_iommu_dev_table[devid].data[i]);
480 static void dump_command(unsigned long phys_addr)
482 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
485 for (i = 0; i < 4; ++i)
486 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
489 static void amd_iommu_report_page_fault(u16 devid, u16 domain_id,
490 u64 address, int flags)
492 struct iommu_dev_data *dev_data = NULL;
493 struct pci_dev *pdev;
495 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
498 dev_data = dev_iommu_priv_get(&pdev->dev);
500 if (dev_data && __ratelimit(&dev_data->rs)) {
501 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
502 domain_id, address, flags);
503 } else if (printk_ratelimit()) {
504 pr_err("Event logged [IO_PAGE_FAULT device=%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
505 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
506 domain_id, address, flags);
513 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
515 struct device *dev = iommu->iommu.dev;
516 int type, devid, flags, tag;
517 volatile u32 *event = __evt;
523 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
524 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
525 pasid = (event[0] & EVENT_DOMID_MASK_HI) |
526 (event[1] & EVENT_DOMID_MASK_LO);
527 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
528 address = (u64)(((u64)event[3]) << 32) | event[2];
531 /* Did we hit the erratum? */
532 if (++count == LOOP_TIMEOUT) {
533 pr_err("No event written to event log\n");
540 if (type == EVENT_TYPE_IO_FAULT) {
541 amd_iommu_report_page_fault(devid, pasid, address, flags);
546 case EVENT_TYPE_ILL_DEV:
547 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
548 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
549 pasid, address, flags);
550 dump_dte_entry(devid);
552 case EVENT_TYPE_DEV_TAB_ERR:
553 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
554 "address=0x%llx flags=0x%04x]\n",
555 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
558 case EVENT_TYPE_PAGE_TAB_ERR:
559 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
560 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
561 pasid, address, flags);
563 case EVENT_TYPE_ILL_CMD:
564 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
565 dump_command(address);
567 case EVENT_TYPE_CMD_HARD_ERR:
568 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
571 case EVENT_TYPE_IOTLB_INV_TO:
572 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%02x:%02x.%x address=0x%llx]\n",
573 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
576 case EVENT_TYPE_INV_DEV_REQ:
577 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
578 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
579 pasid, address, flags);
581 case EVENT_TYPE_INV_PPR_REQ:
582 pasid = PPR_PASID(*((u64 *)__evt));
583 tag = event[1] & 0x03FF;
584 dev_err(dev, "Event logged [INVALID_PPR_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n",
585 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
586 pasid, address, flags, tag);
589 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
590 event[0], event[1], event[2], event[3]);
593 memset(__evt, 0, 4 * sizeof(u32));
596 static void iommu_poll_events(struct amd_iommu *iommu)
600 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
601 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
603 while (head != tail) {
604 iommu_print_event(iommu, iommu->evt_buf + head);
605 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
608 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
611 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
613 struct amd_iommu_fault fault;
615 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
616 pr_err_ratelimited("Unknown PPR request received\n");
620 fault.address = raw[1];
621 fault.pasid = PPR_PASID(raw[0]);
622 fault.device_id = PPR_DEVID(raw[0]);
623 fault.tag = PPR_TAG(raw[0]);
624 fault.flags = PPR_FLAGS(raw[0]);
626 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
629 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
633 if (iommu->ppr_log == NULL)
636 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
637 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
639 while (head != tail) {
644 raw = (u64 *)(iommu->ppr_log + head);
647 * Hardware bug: Interrupt may arrive before the entry is
648 * written to memory. If this happens we need to wait for the
651 for (i = 0; i < LOOP_TIMEOUT; ++i) {
652 if (PPR_REQ_TYPE(raw[0]) != 0)
657 /* Avoid memcpy function-call overhead */
662 * To detect the hardware bug we need to clear the entry
665 raw[0] = raw[1] = 0UL;
667 /* Update head pointer of hardware ring-buffer */
668 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
669 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
671 /* Handle PPR entry */
672 iommu_handle_ppr_entry(iommu, entry);
674 /* Refresh ring-buffer information */
675 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
676 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
680 #ifdef CONFIG_IRQ_REMAP
681 static int (*iommu_ga_log_notifier)(u32);
683 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
685 iommu_ga_log_notifier = notifier;
689 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
691 static void iommu_poll_ga_log(struct amd_iommu *iommu)
693 u32 head, tail, cnt = 0;
695 if (iommu->ga_log == NULL)
698 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
699 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
701 while (head != tail) {
705 raw = (u64 *)(iommu->ga_log + head);
708 /* Avoid memcpy function-call overhead */
711 /* Update head pointer of hardware ring-buffer */
712 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
713 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
715 /* Handle GA entry */
716 switch (GA_REQ_TYPE(log_entry)) {
718 if (!iommu_ga_log_notifier)
721 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
722 __func__, GA_DEVID(log_entry),
725 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
726 pr_err("GA log notifier failed.\n");
735 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
737 if (!irq_remapping_enabled || !dev_is_pci(dev) ||
738 pci_dev_has_special_msi_domain(to_pci_dev(dev)))
741 dev_set_msi_domain(dev, iommu->msi_domain);
744 #else /* CONFIG_IRQ_REMAP */
746 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
747 #endif /* !CONFIG_IRQ_REMAP */
749 #define AMD_IOMMU_INT_MASK \
750 (MMIO_STATUS_EVT_INT_MASK | \
751 MMIO_STATUS_PPR_INT_MASK | \
752 MMIO_STATUS_GALOG_INT_MASK)
754 irqreturn_t amd_iommu_int_thread(int irq, void *data)
756 struct amd_iommu *iommu = (struct amd_iommu *) data;
757 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
759 while (status & AMD_IOMMU_INT_MASK) {
760 /* Enable EVT and PPR and GA interrupts again */
761 writel(AMD_IOMMU_INT_MASK,
762 iommu->mmio_base + MMIO_STATUS_OFFSET);
764 if (status & MMIO_STATUS_EVT_INT_MASK) {
765 pr_devel("Processing IOMMU Event Log\n");
766 iommu_poll_events(iommu);
769 if (status & MMIO_STATUS_PPR_INT_MASK) {
770 pr_devel("Processing IOMMU PPR Log\n");
771 iommu_poll_ppr_log(iommu);
774 #ifdef CONFIG_IRQ_REMAP
775 if (status & MMIO_STATUS_GALOG_INT_MASK) {
776 pr_devel("Processing IOMMU GA Log\n");
777 iommu_poll_ga_log(iommu);
782 * Hardware bug: ERBT1312
783 * When re-enabling interrupt (by writing 1
784 * to clear the bit), the hardware might also try to set
785 * the interrupt bit in the event status register.
786 * In this scenario, the bit will be set, and disable
787 * subsequent interrupts.
789 * Workaround: The IOMMU driver should read back the
790 * status register and check if the interrupt bits are cleared.
791 * If not, driver will need to go through the interrupt handler
792 * again and re-clear the bits
794 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
799 irqreturn_t amd_iommu_int_handler(int irq, void *data)
801 return IRQ_WAKE_THREAD;
804 /****************************************************************************
806 * IOMMU command queuing functions
808 ****************************************************************************/
810 static int wait_on_sem(volatile u64 *sem)
814 while (*sem == 0 && i < LOOP_TIMEOUT) {
819 if (i == LOOP_TIMEOUT) {
820 pr_alert("Completion-Wait loop timed out\n");
827 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
828 struct iommu_cmd *cmd)
833 /* Copy command to buffer */
834 tail = iommu->cmd_buf_tail;
835 target = iommu->cmd_buf + tail;
836 memcpy(target, cmd, sizeof(*cmd));
838 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
839 iommu->cmd_buf_tail = tail;
841 /* Tell the IOMMU about it */
842 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
845 static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
847 u64 paddr = iommu_virt_to_phys((void *)address);
849 WARN_ON(address & 0x7ULL);
851 memset(cmd, 0, sizeof(*cmd));
852 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
853 cmd->data[1] = upper_32_bits(paddr);
855 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
858 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
860 memset(cmd, 0, sizeof(*cmd));
861 cmd->data[0] = devid;
862 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
865 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
866 size_t size, u16 domid, int pde)
871 pages = iommu_num_pages(address, size, PAGE_SIZE);
876 * If we have to flush more than one page, flush all
877 * TLB entries for this domain
879 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
883 address &= PAGE_MASK;
885 memset(cmd, 0, sizeof(*cmd));
886 cmd->data[1] |= domid;
887 cmd->data[2] = lower_32_bits(address);
888 cmd->data[3] = upper_32_bits(address);
889 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
890 if (s) /* size bit - we flush more than one 4kb page */
891 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
892 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
893 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
896 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
897 u64 address, size_t size)
902 pages = iommu_num_pages(address, size, PAGE_SIZE);
907 * If we have to flush more than one page, flush all
908 * TLB entries for this domain
910 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
914 address &= PAGE_MASK;
916 memset(cmd, 0, sizeof(*cmd));
917 cmd->data[0] = devid;
918 cmd->data[0] |= (qdep & 0xff) << 24;
919 cmd->data[1] = devid;
920 cmd->data[2] = lower_32_bits(address);
921 cmd->data[3] = upper_32_bits(address);
922 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
924 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
927 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
928 u64 address, bool size)
930 memset(cmd, 0, sizeof(*cmd));
932 address &= ~(0xfffULL);
934 cmd->data[0] = pasid;
935 cmd->data[1] = domid;
936 cmd->data[2] = lower_32_bits(address);
937 cmd->data[3] = upper_32_bits(address);
938 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
939 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
941 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
942 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
945 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid,
946 int qdep, u64 address, bool size)
948 memset(cmd, 0, sizeof(*cmd));
950 address &= ~(0xfffULL);
952 cmd->data[0] = devid;
953 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
954 cmd->data[0] |= (qdep & 0xff) << 24;
955 cmd->data[1] = devid;
956 cmd->data[1] |= (pasid & 0xff) << 16;
957 cmd->data[2] = lower_32_bits(address);
958 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
959 cmd->data[3] = upper_32_bits(address);
961 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
962 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
965 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
966 int status, int tag, bool gn)
968 memset(cmd, 0, sizeof(*cmd));
970 cmd->data[0] = devid;
972 cmd->data[1] = pasid;
973 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
975 cmd->data[3] = tag & 0x1ff;
976 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
978 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
981 static void build_inv_all(struct iommu_cmd *cmd)
983 memset(cmd, 0, sizeof(*cmd));
984 CMD_SET_TYPE(cmd, CMD_INV_ALL);
987 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
989 memset(cmd, 0, sizeof(*cmd));
990 cmd->data[0] = devid;
991 CMD_SET_TYPE(cmd, CMD_INV_IRT);
995 * Writes the command to the IOMMUs command buffer and informs the
996 * hardware about the new command.
998 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
999 struct iommu_cmd *cmd,
1002 unsigned int count = 0;
1003 u32 left, next_tail;
1005 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1007 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
1010 /* Skip udelay() the first time around */
1012 if (count == LOOP_TIMEOUT) {
1013 pr_err("Command buffer timeout\n");
1020 /* Update head and recheck remaining space */
1021 iommu->cmd_buf_head = readl(iommu->mmio_base +
1022 MMIO_CMD_HEAD_OFFSET);
1027 copy_cmd_to_buffer(iommu, cmd);
1029 /* Do we need to make sure all commands are processed? */
1030 iommu->need_sync = sync;
1035 static int iommu_queue_command_sync(struct amd_iommu *iommu,
1036 struct iommu_cmd *cmd,
1039 unsigned long flags;
1042 raw_spin_lock_irqsave(&iommu->lock, flags);
1043 ret = __iommu_queue_command_sync(iommu, cmd, sync);
1044 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1049 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1051 return iommu_queue_command_sync(iommu, cmd, true);
1055 * This function queues a completion wait command into the command
1056 * buffer of an IOMMU
1058 static int iommu_completion_wait(struct amd_iommu *iommu)
1060 struct iommu_cmd cmd;
1061 unsigned long flags;
1064 if (!iommu->need_sync)
1068 build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
1070 raw_spin_lock_irqsave(&iommu->lock, flags);
1074 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1078 ret = wait_on_sem(&iommu->cmd_sem);
1081 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1086 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1088 struct iommu_cmd cmd;
1090 build_inv_dte(&cmd, devid);
1092 return iommu_queue_command(iommu, &cmd);
1095 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
1099 for (devid = 0; devid <= 0xffff; ++devid)
1100 iommu_flush_dte(iommu, devid);
1102 iommu_completion_wait(iommu);
1106 * This function uses heavy locking and may disable irqs for some time. But
1107 * this is no issue because it is only called during resume.
1109 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
1113 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1114 struct iommu_cmd cmd;
1115 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1117 iommu_queue_command(iommu, &cmd);
1120 iommu_completion_wait(iommu);
1123 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1125 struct iommu_cmd cmd;
1127 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1129 iommu_queue_command(iommu, &cmd);
1131 iommu_completion_wait(iommu);
1134 static void amd_iommu_flush_all(struct amd_iommu *iommu)
1136 struct iommu_cmd cmd;
1138 build_inv_all(&cmd);
1140 iommu_queue_command(iommu, &cmd);
1141 iommu_completion_wait(iommu);
1144 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1146 struct iommu_cmd cmd;
1148 build_inv_irt(&cmd, devid);
1150 iommu_queue_command(iommu, &cmd);
1153 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
1157 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1158 iommu_flush_irt(iommu, devid);
1160 iommu_completion_wait(iommu);
1163 void iommu_flush_all_caches(struct amd_iommu *iommu)
1165 if (iommu_feature(iommu, FEATURE_IA)) {
1166 amd_iommu_flush_all(iommu);
1168 amd_iommu_flush_dte_all(iommu);
1169 amd_iommu_flush_irt_all(iommu);
1170 amd_iommu_flush_tlb_all(iommu);
1175 * Command send function for flushing on-device TLB
1177 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1178 u64 address, size_t size)
1180 struct amd_iommu *iommu;
1181 struct iommu_cmd cmd;
1184 qdep = dev_data->ats.qdep;
1185 iommu = amd_iommu_rlookup_table[dev_data->devid];
1187 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1189 return iommu_queue_command(iommu, &cmd);
1192 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
1194 struct amd_iommu *iommu = data;
1196 return iommu_flush_dte(iommu, alias);
1200 * Command send function for invalidating a device table entry
1202 static int device_flush_dte(struct iommu_dev_data *dev_data)
1204 struct amd_iommu *iommu;
1208 iommu = amd_iommu_rlookup_table[dev_data->devid];
1211 ret = pci_for_each_dma_alias(dev_data->pdev,
1212 device_flush_dte_alias, iommu);
1214 ret = iommu_flush_dte(iommu, dev_data->devid);
1218 alias = amd_iommu_alias_table[dev_data->devid];
1219 if (alias != dev_data->devid) {
1220 ret = iommu_flush_dte(iommu, alias);
1225 if (dev_data->ats.enabled)
1226 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1232 * TLB invalidation function which is called from the mapping functions.
1233 * It invalidates a single PTE if the range to flush is within a single
1234 * page. Otherwise it flushes the whole TLB of the IOMMU.
1236 static void __domain_flush_pages(struct protection_domain *domain,
1237 u64 address, size_t size, int pde)
1239 struct iommu_dev_data *dev_data;
1240 struct iommu_cmd cmd;
1243 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1245 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1246 if (!domain->dev_iommu[i])
1250 * Devices of this domain are behind this IOMMU
1251 * We need a TLB flush
1253 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1256 list_for_each_entry(dev_data, &domain->dev_list, list) {
1258 if (!dev_data->ats.enabled)
1261 ret |= device_flush_iotlb(dev_data, address, size);
1267 static void domain_flush_pages(struct protection_domain *domain,
1268 u64 address, size_t size)
1270 __domain_flush_pages(domain, address, size, 0);
1273 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1274 static void domain_flush_tlb_pde(struct protection_domain *domain)
1276 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1279 static void domain_flush_complete(struct protection_domain *domain)
1283 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1284 if (domain && !domain->dev_iommu[i])
1288 * Devices of this domain are behind this IOMMU
1289 * We need to wait for completion of all commands.
1291 iommu_completion_wait(amd_iommus[i]);
1295 /* Flush the not present cache if it exists */
1296 static void domain_flush_np_cache(struct protection_domain *domain,
1297 dma_addr_t iova, size_t size)
1299 if (unlikely(amd_iommu_np_cache)) {
1300 unsigned long flags;
1302 spin_lock_irqsave(&domain->lock, flags);
1303 domain_flush_pages(domain, iova, size);
1304 domain_flush_complete(domain);
1305 spin_unlock_irqrestore(&domain->lock, flags);
1311 * This function flushes the DTEs for all devices in domain
1313 static void domain_flush_devices(struct protection_domain *domain)
1315 struct iommu_dev_data *dev_data;
1317 list_for_each_entry(dev_data, &domain->dev_list, list)
1318 device_flush_dte(dev_data);
1321 /****************************************************************************
1323 * The functions below are used the create the page table mappings for
1324 * unity mapped regions.
1326 ****************************************************************************/
1328 static void free_page_list(struct page *freelist)
1330 while (freelist != NULL) {
1331 unsigned long p = (unsigned long)page_address(freelist);
1332 freelist = freelist->freelist;
1337 static struct page *free_pt_page(unsigned long pt, struct page *freelist)
1339 struct page *p = virt_to_page((void *)pt);
1341 p->freelist = freelist;
1346 #define DEFINE_FREE_PT_FN(LVL, FN) \
1347 static struct page *free_pt_##LVL (unsigned long __pt, struct page *freelist) \
1355 for (i = 0; i < 512; ++i) { \
1356 /* PTE present? */ \
1357 if (!IOMMU_PTE_PRESENT(pt[i])) \
1361 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1362 PM_PTE_LEVEL(pt[i]) == 7) \
1365 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1366 freelist = FN(p, freelist); \
1369 return free_pt_page((unsigned long)pt, freelist); \
1372 DEFINE_FREE_PT_FN(l2, free_pt_page)
1373 DEFINE_FREE_PT_FN(l3, free_pt_l2)
1374 DEFINE_FREE_PT_FN(l4, free_pt_l3)
1375 DEFINE_FREE_PT_FN(l5, free_pt_l4)
1376 DEFINE_FREE_PT_FN(l6, free_pt_l5)
1378 static struct page *free_sub_pt(unsigned long root, int mode,
1379 struct page *freelist)
1382 case PAGE_MODE_NONE:
1383 case PAGE_MODE_7_LEVEL:
1385 case PAGE_MODE_1_LEVEL:
1386 freelist = free_pt_page(root, freelist);
1388 case PAGE_MODE_2_LEVEL:
1389 freelist = free_pt_l2(root, freelist);
1391 case PAGE_MODE_3_LEVEL:
1392 freelist = free_pt_l3(root, freelist);
1394 case PAGE_MODE_4_LEVEL:
1395 freelist = free_pt_l4(root, freelist);
1397 case PAGE_MODE_5_LEVEL:
1398 freelist = free_pt_l5(root, freelist);
1400 case PAGE_MODE_6_LEVEL:
1401 freelist = free_pt_l6(root, freelist);
1410 static void free_pagetable(struct domain_pgtable *pgtable)
1412 struct page *freelist = NULL;
1415 if (pgtable->mode == PAGE_MODE_NONE)
1418 BUG_ON(pgtable->mode < PAGE_MODE_NONE ||
1419 pgtable->mode > PAGE_MODE_6_LEVEL);
1421 root = (unsigned long)pgtable->root;
1422 freelist = free_sub_pt(root, pgtable->mode, freelist);
1424 free_page_list(freelist);
1428 * This function is used to add another level to an IO page table. Adding
1429 * another level increases the size of the address space by 9 bits to a size up
1432 static bool increase_address_space(struct protection_domain *domain,
1433 unsigned long address,
1436 struct domain_pgtable pgtable;
1437 unsigned long flags;
1441 spin_lock_irqsave(&domain->lock, flags);
1443 amd_iommu_domain_get_pgtable(domain, &pgtable);
1445 if (address <= PM_LEVEL_SIZE(pgtable.mode))
1449 if (WARN_ON_ONCE(pgtable.mode == PAGE_MODE_6_LEVEL))
1452 pte = (void *)get_zeroed_page(gfp);
1456 *pte = PM_LEVEL_PDE(pgtable.mode, iommu_virt_to_phys(pgtable.root));
1460 update_and_flush_device_table(domain, &pgtable);
1461 domain_flush_complete(domain);
1464 * Device Table needs to be updated and flushed before the new root can
1467 amd_iommu_domain_set_pgtable(domain, pte, pgtable.mode);
1472 spin_unlock_irqrestore(&domain->lock, flags);
1477 static u64 *alloc_pte(struct protection_domain *domain,
1478 unsigned long address,
1479 unsigned long page_size,
1484 struct domain_pgtable pgtable;
1488 BUG_ON(!is_power_of_2(page_size));
1490 amd_iommu_domain_get_pgtable(domain, &pgtable);
1492 while (address > PM_LEVEL_SIZE(pgtable.mode)) {
1494 * Return an error if there is no memory to update the
1497 if (!increase_address_space(domain, address, gfp))
1500 /* Read new values to check if update was successful */
1501 amd_iommu_domain_get_pgtable(domain, &pgtable);
1505 level = pgtable.mode - 1;
1506 pte = &pgtable.root[PM_LEVEL_INDEX(level, address)];
1507 address = PAGE_SIZE_ALIGN(address, page_size);
1508 end_lvl = PAGE_SIZE_LEVEL(page_size);
1510 while (level > end_lvl) {
1515 pte_level = PM_PTE_LEVEL(__pte);
1518 * If we replace a series of large PTEs, we need
1519 * to tear down all of them.
1521 if (IOMMU_PTE_PRESENT(__pte) &&
1522 pte_level == PAGE_MODE_7_LEVEL) {
1523 unsigned long count, i;
1526 lpte = first_pte_l7(pte, NULL, &count);
1529 * Unmap the replicated PTEs that still match the
1530 * original large mapping
1532 for (i = 0; i < count; ++i)
1533 cmpxchg64(&lpte[i], __pte, 0ULL);
1539 if (!IOMMU_PTE_PRESENT(__pte) ||
1540 pte_level == PAGE_MODE_NONE) {
1541 page = (u64 *)get_zeroed_page(gfp);
1546 __npte = PM_LEVEL_PDE(level, iommu_virt_to_phys(page));
1548 /* pte could have been changed somewhere. */
1549 if (cmpxchg64(pte, __pte, __npte) != __pte)
1550 free_page((unsigned long)page);
1551 else if (IOMMU_PTE_PRESENT(__pte))
1557 /* No level skipping support yet */
1558 if (pte_level != level)
1563 pte = IOMMU_PTE_PAGE(__pte);
1565 if (pte_page && level == end_lvl)
1568 pte = &pte[PM_LEVEL_INDEX(level, address)];
1575 * This function checks if there is a PTE for a given dma address. If
1576 * there is one, it returns the pointer to it.
1578 static u64 *fetch_pte(struct protection_domain *domain,
1579 unsigned long address,
1580 unsigned long *page_size)
1582 struct domain_pgtable pgtable;
1588 amd_iommu_domain_get_pgtable(domain, &pgtable);
1590 if (address > PM_LEVEL_SIZE(pgtable.mode))
1593 level = pgtable.mode - 1;
1594 pte = &pgtable.root[PM_LEVEL_INDEX(level, address)];
1595 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1600 if (!IOMMU_PTE_PRESENT(*pte))
1604 if (PM_PTE_LEVEL(*pte) == 7 ||
1605 PM_PTE_LEVEL(*pte) == 0)
1608 /* No level skipping support yet */
1609 if (PM_PTE_LEVEL(*pte) != level)
1614 /* Walk to the next level */
1615 pte = IOMMU_PTE_PAGE(*pte);
1616 pte = &pte[PM_LEVEL_INDEX(level, address)];
1617 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1621 * If we have a series of large PTEs, make
1622 * sure to return a pointer to the first one.
1624 if (PM_PTE_LEVEL(*pte) == PAGE_MODE_7_LEVEL)
1625 pte = first_pte_l7(pte, page_size, NULL);
1630 static struct page *free_clear_pte(u64 *pte, u64 pteval, struct page *freelist)
1635 while (cmpxchg64(pte, pteval, 0) != pteval) {
1636 pr_warn("AMD-Vi: IOMMU pte changed since we read it\n");
1640 if (!IOMMU_PTE_PRESENT(pteval))
1643 pt = (unsigned long)IOMMU_PTE_PAGE(pteval);
1644 mode = IOMMU_PTE_MODE(pteval);
1646 return free_sub_pt(pt, mode, freelist);
1650 * Generic mapping functions. It maps a physical address into a DMA
1651 * address space. It allocates the page table pages if necessary.
1652 * In the future it can be extended to a generic mapping function
1653 * supporting all features of AMD IOMMU page tables like level skipping
1654 * and full 64 bit address spaces.
1656 static int iommu_map_page(struct protection_domain *dom,
1657 unsigned long bus_addr,
1658 unsigned long phys_addr,
1659 unsigned long page_size,
1663 struct page *freelist = NULL;
1664 bool updated = false;
1668 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1669 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1672 if (!(prot & IOMMU_PROT_MASK))
1675 count = PAGE_SIZE_PTE_COUNT(page_size);
1676 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp, &updated);
1682 for (i = 0; i < count; ++i)
1683 freelist = free_clear_pte(&pte[i], pte[i], freelist);
1685 if (freelist != NULL)
1689 __pte = PAGE_SIZE_PTE(__sme_set(phys_addr), page_size);
1690 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC;
1692 __pte = __sme_set(phys_addr) | IOMMU_PTE_PR | IOMMU_PTE_FC;
1694 if (prot & IOMMU_PROT_IR)
1695 __pte |= IOMMU_PTE_IR;
1696 if (prot & IOMMU_PROT_IW)
1697 __pte |= IOMMU_PTE_IW;
1699 for (i = 0; i < count; ++i)
1706 unsigned long flags;
1708 spin_lock_irqsave(&dom->lock, flags);
1710 * Flush domain TLB(s) and wait for completion. Any Device-Table
1711 * Updates and flushing already happened in
1712 * increase_address_space().
1714 domain_flush_tlb_pde(dom);
1715 domain_flush_complete(dom);
1716 spin_unlock_irqrestore(&dom->lock, flags);
1719 /* Everything flushed out, free pages now */
1720 free_page_list(freelist);
1725 static unsigned long iommu_unmap_page(struct protection_domain *dom,
1726 unsigned long bus_addr,
1727 unsigned long page_size)
1729 unsigned long long unmapped;
1730 unsigned long unmap_size;
1733 BUG_ON(!is_power_of_2(page_size));
1737 while (unmapped < page_size) {
1739 pte = fetch_pte(dom, bus_addr, &unmap_size);
1744 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1745 for (i = 0; i < count; i++)
1749 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1750 unmapped += unmap_size;
1753 BUG_ON(unmapped && !is_power_of_2(unmapped));
1758 /****************************************************************************
1760 * The next functions belong to the domain allocation. A domain is
1761 * allocated for every IOMMU as the default domain. If device isolation
1762 * is enabled, every device get its own domain. The most important thing
1763 * about domains is the page table mapping the DMA address space they
1766 ****************************************************************************/
1768 static u16 domain_id_alloc(void)
1772 spin_lock(&pd_bitmap_lock);
1773 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1775 if (id > 0 && id < MAX_DOMAIN_ID)
1776 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1779 spin_unlock(&pd_bitmap_lock);
1784 static void domain_id_free(int id)
1786 spin_lock(&pd_bitmap_lock);
1787 if (id > 0 && id < MAX_DOMAIN_ID)
1788 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1789 spin_unlock(&pd_bitmap_lock);
1792 static void free_gcr3_tbl_level1(u64 *tbl)
1797 for (i = 0; i < 512; ++i) {
1798 if (!(tbl[i] & GCR3_VALID))
1801 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1803 free_page((unsigned long)ptr);
1807 static void free_gcr3_tbl_level2(u64 *tbl)
1812 for (i = 0; i < 512; ++i) {
1813 if (!(tbl[i] & GCR3_VALID))
1816 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1818 free_gcr3_tbl_level1(ptr);
1822 static void free_gcr3_table(struct protection_domain *domain)
1824 if (domain->glx == 2)
1825 free_gcr3_tbl_level2(domain->gcr3_tbl);
1826 else if (domain->glx == 1)
1827 free_gcr3_tbl_level1(domain->gcr3_tbl);
1829 BUG_ON(domain->glx != 0);
1831 free_page((unsigned long)domain->gcr3_tbl);
1834 static void set_dte_entry(u16 devid, struct protection_domain *domain,
1835 struct domain_pgtable *pgtable,
1842 if (pgtable->mode != PAGE_MODE_NONE)
1843 pte_root = iommu_virt_to_phys(pgtable->root);
1845 pte_root |= (pgtable->mode & DEV_ENTRY_MODE_MASK)
1846 << DEV_ENTRY_MODE_SHIFT;
1847 pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV;
1849 flags = amd_iommu_dev_table[devid].data[1];
1852 flags |= DTE_FLAG_IOTLB;
1855 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1857 if (iommu_feature(iommu, FEATURE_EPHSUP))
1858 pte_root |= 1ULL << DEV_ENTRY_PPR;
1861 if (domain->flags & PD_IOMMUV2_MASK) {
1862 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
1863 u64 glx = domain->glx;
1866 pte_root |= DTE_FLAG_GV;
1867 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1869 /* First mask out possible old values for GCR3 table */
1870 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1873 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1876 /* Encode GCR3 table into DTE */
1877 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1880 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1883 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1887 flags &= ~DEV_DOMID_MASK;
1888 flags |= domain->id;
1890 old_domid = amd_iommu_dev_table[devid].data[1] & DEV_DOMID_MASK;
1891 amd_iommu_dev_table[devid].data[1] = flags;
1892 amd_iommu_dev_table[devid].data[0] = pte_root;
1895 * A kdump kernel might be replacing a domain ID that was copied from
1896 * the previous kernel--if so, it needs to flush the translation cache
1897 * entries for the old domain ID that is being overwritten
1900 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1902 amd_iommu_flush_tlb_domid(iommu, old_domid);
1906 static void clear_dte_entry(u16 devid)
1908 /* remove entry from the device table seen by the hardware */
1909 amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV;
1910 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
1912 amd_iommu_apply_erratum_63(devid);
1915 static void do_attach(struct iommu_dev_data *dev_data,
1916 struct protection_domain *domain)
1918 struct domain_pgtable pgtable;
1919 struct amd_iommu *iommu;
1922 iommu = amd_iommu_rlookup_table[dev_data->devid];
1923 ats = dev_data->ats.enabled;
1925 /* Update data structures */
1926 dev_data->domain = domain;
1927 list_add(&dev_data->list, &domain->dev_list);
1929 /* Do reference counting */
1930 domain->dev_iommu[iommu->index] += 1;
1931 domain->dev_cnt += 1;
1933 /* Update device table */
1934 amd_iommu_domain_get_pgtable(domain, &pgtable);
1935 set_dte_entry(dev_data->devid, domain, &pgtable,
1936 ats, dev_data->iommu_v2);
1937 clone_aliases(dev_data->pdev);
1939 device_flush_dte(dev_data);
1942 static void do_detach(struct iommu_dev_data *dev_data)
1944 struct protection_domain *domain = dev_data->domain;
1945 struct amd_iommu *iommu;
1947 iommu = amd_iommu_rlookup_table[dev_data->devid];
1949 /* Update data structures */
1950 dev_data->domain = NULL;
1951 list_del(&dev_data->list);
1952 clear_dte_entry(dev_data->devid);
1953 clone_aliases(dev_data->pdev);
1955 /* Flush the DTE entry */
1956 device_flush_dte(dev_data);
1959 domain_flush_tlb_pde(domain);
1961 /* Wait for the flushes to finish */
1962 domain_flush_complete(domain);
1964 /* decrease reference counters - needs to happen after the flushes */
1965 domain->dev_iommu[iommu->index] -= 1;
1966 domain->dev_cnt -= 1;
1969 static void pdev_iommuv2_disable(struct pci_dev *pdev)
1971 pci_disable_ats(pdev);
1972 pci_disable_pri(pdev);
1973 pci_disable_pasid(pdev);
1976 /* FIXME: Change generic reset-function to do the same */
1977 static int pri_reset_while_enabled(struct pci_dev *pdev)
1982 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
1986 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1987 control |= PCI_PRI_CTRL_RESET;
1988 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
1993 static int pdev_iommuv2_enable(struct pci_dev *pdev)
1998 /* FIXME: Hardcode number of outstanding requests for now */
2000 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2002 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
2004 /* Only allow access to user-accessible pages */
2005 ret = pci_enable_pasid(pdev, 0);
2009 /* First reset the PRI state of the device */
2010 ret = pci_reset_pri(pdev);
2015 ret = pci_enable_pri(pdev, reqs);
2020 ret = pri_reset_while_enabled(pdev);
2025 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2032 pci_disable_pri(pdev);
2033 pci_disable_pasid(pdev);
2039 * If a device is not yet associated with a domain, this function makes the
2040 * device visible in the domain
2042 static int attach_device(struct device *dev,
2043 struct protection_domain *domain)
2045 struct iommu_dev_data *dev_data;
2046 struct pci_dev *pdev;
2047 unsigned long flags;
2050 spin_lock_irqsave(&domain->lock, flags);
2052 dev_data = dev_iommu_priv_get(dev);
2054 spin_lock(&dev_data->lock);
2057 if (dev_data->domain != NULL)
2060 if (!dev_is_pci(dev))
2061 goto skip_ats_check;
2063 pdev = to_pci_dev(dev);
2064 if (domain->flags & PD_IOMMUV2_MASK) {
2065 struct iommu_domain *def_domain = iommu_get_dma_domain(dev);
2068 if (def_domain->type != IOMMU_DOMAIN_IDENTITY)
2071 if (dev_data->iommu_v2) {
2072 if (pdev_iommuv2_enable(pdev) != 0)
2075 dev_data->ats.enabled = true;
2076 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2077 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
2079 } else if (amd_iommu_iotlb_sup &&
2080 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
2081 dev_data->ats.enabled = true;
2082 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2088 do_attach(dev_data, domain);
2091 * We might boot into a crash-kernel here. The crashed kernel
2092 * left the caches in the IOMMU dirty. So we have to flush
2093 * here to evict all dirty stuff.
2095 domain_flush_tlb_pde(domain);
2097 domain_flush_complete(domain);
2100 spin_unlock(&dev_data->lock);
2102 spin_unlock_irqrestore(&domain->lock, flags);
2108 * Removes a device from a protection domain (with devtable_lock held)
2110 static void detach_device(struct device *dev)
2112 struct protection_domain *domain;
2113 struct iommu_dev_data *dev_data;
2114 unsigned long flags;
2116 dev_data = dev_iommu_priv_get(dev);
2117 domain = dev_data->domain;
2119 spin_lock_irqsave(&domain->lock, flags);
2121 spin_lock(&dev_data->lock);
2124 * First check if the device is still attached. It might already
2125 * be detached from its domain because the generic
2126 * iommu_detach_group code detached it and we try again here in
2127 * our alias handling.
2129 if (WARN_ON(!dev_data->domain))
2132 do_detach(dev_data);
2134 if (!dev_is_pci(dev))
2137 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
2138 pdev_iommuv2_disable(to_pci_dev(dev));
2139 else if (dev_data->ats.enabled)
2140 pci_disable_ats(to_pci_dev(dev));
2142 dev_data->ats.enabled = false;
2145 spin_unlock(&dev_data->lock);
2147 spin_unlock_irqrestore(&domain->lock, flags);
2150 static struct iommu_device *amd_iommu_probe_device(struct device *dev)
2152 struct iommu_device *iommu_dev;
2153 struct amd_iommu *iommu;
2156 if (!check_device(dev))
2157 return ERR_PTR(-ENODEV);
2159 devid = get_device_id(dev);
2161 return ERR_PTR(devid);
2163 iommu = amd_iommu_rlookup_table[devid];
2165 if (dev_iommu_priv_get(dev))
2166 return &iommu->iommu;
2168 ret = iommu_init_device(dev);
2170 if (ret != -ENOTSUPP)
2171 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
2172 iommu_dev = ERR_PTR(ret);
2173 iommu_ignore_device(dev);
2175 amd_iommu_set_pci_msi_domain(dev, iommu);
2176 iommu_dev = &iommu->iommu;
2179 iommu_completion_wait(iommu);
2184 static void amd_iommu_probe_finalize(struct device *dev)
2186 struct iommu_domain *domain;
2188 /* Domains are initialized for this device - have a look what we ended up with */
2189 domain = iommu_get_domain_for_dev(dev);
2190 if (domain->type == IOMMU_DOMAIN_DMA)
2191 iommu_setup_dma_ops(dev, IOVA_START_PFN << PAGE_SHIFT, 0);
2194 static void amd_iommu_release_device(struct device *dev)
2196 int devid = get_device_id(dev);
2197 struct amd_iommu *iommu;
2199 if (!check_device(dev))
2202 iommu = amd_iommu_rlookup_table[devid];
2204 amd_iommu_uninit_device(dev);
2205 iommu_completion_wait(iommu);
2208 static struct iommu_group *amd_iommu_device_group(struct device *dev)
2210 if (dev_is_pci(dev))
2211 return pci_device_group(dev);
2213 return acpihid_device_group(dev);
2216 static int amd_iommu_domain_get_attr(struct iommu_domain *domain,
2217 enum iommu_attr attr, void *data)
2219 switch (domain->type) {
2220 case IOMMU_DOMAIN_UNMANAGED:
2222 case IOMMU_DOMAIN_DMA:
2224 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
2225 *(int *)data = !amd_iommu_unmap_flush;
2236 /*****************************************************************************
2238 * The next functions belong to the dma_ops mapping/unmapping code.
2240 *****************************************************************************/
2242 static void update_device_table(struct protection_domain *domain,
2243 struct domain_pgtable *pgtable)
2245 struct iommu_dev_data *dev_data;
2247 list_for_each_entry(dev_data, &domain->dev_list, list) {
2248 set_dte_entry(dev_data->devid, domain, pgtable,
2249 dev_data->ats.enabled, dev_data->iommu_v2);
2250 clone_aliases(dev_data->pdev);
2254 static void update_and_flush_device_table(struct protection_domain *domain,
2255 struct domain_pgtable *pgtable)
2257 update_device_table(domain, pgtable);
2258 domain_flush_devices(domain);
2261 static void update_domain(struct protection_domain *domain)
2263 struct domain_pgtable pgtable;
2265 /* Update device table */
2266 amd_iommu_domain_get_pgtable(domain, &pgtable);
2267 update_and_flush_device_table(domain, &pgtable);
2269 /* Flush domain TLB(s) and wait for completion */
2270 domain_flush_tlb_pde(domain);
2271 domain_flush_complete(domain);
2274 int __init amd_iommu_init_api(void)
2278 ret = iova_cache_get();
2282 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2285 #ifdef CONFIG_ARM_AMBA
2286 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2290 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2297 int __init amd_iommu_init_dma_ops(void)
2299 swiotlb = (iommu_default_passthrough() || sme_me_mask) ? 1 : 0;
2301 if (amd_iommu_unmap_flush)
2302 pr_info("IO/TLB flush on unmap enabled\n");
2304 pr_info("Lazy IO/TLB flushing enabled\n");
2310 /*****************************************************************************
2312 * The following functions belong to the exported interface of AMD IOMMU
2314 * This interface allows access to lower level functions of the IOMMU
2315 * like protection domain handling and assignement of devices to domains
2316 * which is not possible with the dma_ops interface.
2318 *****************************************************************************/
2320 static void cleanup_domain(struct protection_domain *domain)
2322 struct iommu_dev_data *entry;
2323 unsigned long flags;
2325 spin_lock_irqsave(&domain->lock, flags);
2327 while (!list_empty(&domain->dev_list)) {
2328 entry = list_first_entry(&domain->dev_list,
2329 struct iommu_dev_data, list);
2330 BUG_ON(!entry->domain);
2334 spin_unlock_irqrestore(&domain->lock, flags);
2337 static void protection_domain_free(struct protection_domain *domain)
2339 struct domain_pgtable pgtable;
2345 domain_id_free(domain->id);
2347 amd_iommu_domain_get_pgtable(domain, &pgtable);
2348 amd_iommu_domain_clr_pt_root(domain);
2349 free_pagetable(&pgtable);
2354 static int protection_domain_init(struct protection_domain *domain, int mode)
2356 u64 *pt_root = NULL;
2358 BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
2360 spin_lock_init(&domain->lock);
2361 domain->id = domain_id_alloc();
2364 INIT_LIST_HEAD(&domain->dev_list);
2366 if (mode != PAGE_MODE_NONE) {
2367 pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2372 amd_iommu_domain_set_pgtable(domain, pt_root, mode);
2377 static struct protection_domain *protection_domain_alloc(int mode)
2379 struct protection_domain *domain;
2381 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2385 if (protection_domain_init(domain, mode))
2396 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2398 struct protection_domain *domain;
2399 int mode = DEFAULT_PGTABLE_LEVEL;
2401 if (type == IOMMU_DOMAIN_IDENTITY)
2402 mode = PAGE_MODE_NONE;
2404 domain = protection_domain_alloc(mode);
2408 domain->domain.geometry.aperture_start = 0;
2409 domain->domain.geometry.aperture_end = ~0ULL;
2410 domain->domain.geometry.force_aperture = true;
2412 if (type == IOMMU_DOMAIN_DMA &&
2413 iommu_get_dma_cookie(&domain->domain) == -ENOMEM)
2416 return &domain->domain;
2419 protection_domain_free(domain);
2424 static void amd_iommu_domain_free(struct iommu_domain *dom)
2426 struct protection_domain *domain;
2428 domain = to_pdomain(dom);
2430 if (domain->dev_cnt > 0)
2431 cleanup_domain(domain);
2433 BUG_ON(domain->dev_cnt != 0);
2438 if (dom->type == IOMMU_DOMAIN_DMA)
2439 iommu_put_dma_cookie(&domain->domain);
2441 if (domain->flags & PD_IOMMUV2_MASK)
2442 free_gcr3_table(domain);
2444 protection_domain_free(domain);
2447 static void amd_iommu_detach_device(struct iommu_domain *dom,
2450 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2451 struct amd_iommu *iommu;
2454 if (!check_device(dev))
2457 devid = get_device_id(dev);
2461 if (dev_data->domain != NULL)
2464 iommu = amd_iommu_rlookup_table[devid];
2468 #ifdef CONFIG_IRQ_REMAP
2469 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2470 (dom->type == IOMMU_DOMAIN_UNMANAGED))
2471 dev_data->use_vapic = 0;
2474 iommu_completion_wait(iommu);
2477 static int amd_iommu_attach_device(struct iommu_domain *dom,
2480 struct protection_domain *domain = to_pdomain(dom);
2481 struct iommu_dev_data *dev_data;
2482 struct amd_iommu *iommu;
2485 if (!check_device(dev))
2488 dev_data = dev_iommu_priv_get(dev);
2489 dev_data->defer_attach = false;
2491 iommu = amd_iommu_rlookup_table[dev_data->devid];
2495 if (dev_data->domain)
2498 ret = attach_device(dev, domain);
2500 #ifdef CONFIG_IRQ_REMAP
2501 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
2502 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
2503 dev_data->use_vapic = 1;
2505 dev_data->use_vapic = 0;
2509 iommu_completion_wait(iommu);
2514 static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2515 phys_addr_t paddr, size_t page_size, int iommu_prot,
2518 struct protection_domain *domain = to_pdomain(dom);
2519 struct domain_pgtable pgtable;
2523 amd_iommu_domain_get_pgtable(domain, &pgtable);
2524 if (pgtable.mode == PAGE_MODE_NONE)
2527 if (iommu_prot & IOMMU_READ)
2528 prot |= IOMMU_PROT_IR;
2529 if (iommu_prot & IOMMU_WRITE)
2530 prot |= IOMMU_PROT_IW;
2532 ret = iommu_map_page(domain, iova, paddr, page_size, prot, gfp);
2534 domain_flush_np_cache(domain, iova, page_size);
2539 static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2541 struct iommu_iotlb_gather *gather)
2543 struct protection_domain *domain = to_pdomain(dom);
2544 struct domain_pgtable pgtable;
2546 amd_iommu_domain_get_pgtable(domain, &pgtable);
2547 if (pgtable.mode == PAGE_MODE_NONE)
2550 return iommu_unmap_page(domain, iova, page_size);
2553 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2556 struct protection_domain *domain = to_pdomain(dom);
2557 unsigned long offset_mask, pte_pgsize;
2558 struct domain_pgtable pgtable;
2561 amd_iommu_domain_get_pgtable(domain, &pgtable);
2562 if (pgtable.mode == PAGE_MODE_NONE)
2565 pte = fetch_pte(domain, iova, &pte_pgsize);
2567 if (!pte || !IOMMU_PTE_PRESENT(*pte))
2570 offset_mask = pte_pgsize - 1;
2571 __pte = __sme_clr(*pte & PM_ADDR_MASK);
2573 return (__pte & ~offset_mask) | (iova & offset_mask);
2576 static bool amd_iommu_capable(enum iommu_cap cap)
2579 case IOMMU_CAP_CACHE_COHERENCY:
2581 case IOMMU_CAP_INTR_REMAP:
2582 return (irq_remapping_enabled == 1);
2583 case IOMMU_CAP_NOEXEC:
2592 static void amd_iommu_get_resv_regions(struct device *dev,
2593 struct list_head *head)
2595 struct iommu_resv_region *region;
2596 struct unity_map_entry *entry;
2599 devid = get_device_id(dev);
2603 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
2607 if (devid < entry->devid_start || devid > entry->devid_end)
2610 type = IOMMU_RESV_DIRECT;
2611 length = entry->address_end - entry->address_start;
2612 if (entry->prot & IOMMU_PROT_IR)
2614 if (entry->prot & IOMMU_PROT_IW)
2615 prot |= IOMMU_WRITE;
2616 if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
2617 /* Exclusion range */
2618 type = IOMMU_RESV_RESERVED;
2620 region = iommu_alloc_resv_region(entry->address_start,
2621 length, prot, type);
2623 dev_err(dev, "Out of memory allocating dm-regions\n");
2626 list_add_tail(®ion->list, head);
2629 region = iommu_alloc_resv_region(MSI_RANGE_START,
2630 MSI_RANGE_END - MSI_RANGE_START + 1,
2634 list_add_tail(®ion->list, head);
2636 region = iommu_alloc_resv_region(HT_RANGE_START,
2637 HT_RANGE_END - HT_RANGE_START + 1,
2638 0, IOMMU_RESV_RESERVED);
2641 list_add_tail(®ion->list, head);
2644 bool amd_iommu_is_attach_deferred(struct iommu_domain *domain,
2647 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2649 return dev_data->defer_attach;
2651 EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred);
2653 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
2655 struct protection_domain *dom = to_pdomain(domain);
2656 unsigned long flags;
2658 spin_lock_irqsave(&dom->lock, flags);
2659 domain_flush_tlb_pde(dom);
2660 domain_flush_complete(dom);
2661 spin_unlock_irqrestore(&dom->lock, flags);
2664 static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
2665 struct iommu_iotlb_gather *gather)
2667 amd_iommu_flush_iotlb_all(domain);
2670 static int amd_iommu_def_domain_type(struct device *dev)
2672 struct iommu_dev_data *dev_data;
2674 dev_data = dev_iommu_priv_get(dev);
2679 * Do not identity map IOMMUv2 capable devices when memory encryption is
2680 * active, because some of those devices (AMD GPUs) don't have the
2681 * encryption bit in their DMA-mask and require remapping.
2683 if (!mem_encrypt_active() && dev_data->iommu_v2)
2684 return IOMMU_DOMAIN_IDENTITY;
2689 const struct iommu_ops amd_iommu_ops = {
2690 .capable = amd_iommu_capable,
2691 .domain_alloc = amd_iommu_domain_alloc,
2692 .domain_free = amd_iommu_domain_free,
2693 .attach_dev = amd_iommu_attach_device,
2694 .detach_dev = amd_iommu_detach_device,
2695 .map = amd_iommu_map,
2696 .unmap = amd_iommu_unmap,
2697 .iova_to_phys = amd_iommu_iova_to_phys,
2698 .probe_device = amd_iommu_probe_device,
2699 .release_device = amd_iommu_release_device,
2700 .probe_finalize = amd_iommu_probe_finalize,
2701 .device_group = amd_iommu_device_group,
2702 .domain_get_attr = amd_iommu_domain_get_attr,
2703 .get_resv_regions = amd_iommu_get_resv_regions,
2704 .put_resv_regions = generic_iommu_put_resv_regions,
2705 .is_attach_deferred = amd_iommu_is_attach_deferred,
2706 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
2707 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2708 .iotlb_sync = amd_iommu_iotlb_sync,
2709 .def_domain_type = amd_iommu_def_domain_type,
2712 /*****************************************************************************
2714 * The next functions do a basic initialization of IOMMU for pass through
2717 * In passthrough mode the IOMMU is initialized and enabled but not used for
2718 * DMA-API translation.
2720 *****************************************************************************/
2722 /* IOMMUv2 specific functions */
2723 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2725 return atomic_notifier_chain_register(&ppr_notifier, nb);
2727 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2729 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2731 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2733 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
2735 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2737 struct protection_domain *domain = to_pdomain(dom);
2738 struct domain_pgtable pgtable;
2739 unsigned long flags;
2741 spin_lock_irqsave(&domain->lock, flags);
2743 /* First save pgtable configuration*/
2744 amd_iommu_domain_get_pgtable(domain, &pgtable);
2746 /* Remove page-table from domain */
2747 amd_iommu_domain_clr_pt_root(domain);
2749 /* Make changes visible to IOMMUs */
2750 update_domain(domain);
2752 /* Page-table is not visible to IOMMU anymore, so free it */
2753 free_pagetable(&pgtable);
2755 spin_unlock_irqrestore(&domain->lock, flags);
2757 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
2759 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
2761 struct protection_domain *domain = to_pdomain(dom);
2762 unsigned long flags;
2765 if (pasids <= 0 || pasids > (PASID_MASK + 1))
2768 /* Number of GCR3 table levels required */
2769 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
2772 if (levels > amd_iommu_max_glx_val)
2775 spin_lock_irqsave(&domain->lock, flags);
2778 * Save us all sanity checks whether devices already in the
2779 * domain support IOMMUv2. Just force that the domain has no
2780 * devices attached when it is switched into IOMMUv2 mode.
2783 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
2787 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
2788 if (domain->gcr3_tbl == NULL)
2791 domain->glx = levels;
2792 domain->flags |= PD_IOMMUV2_MASK;
2794 update_domain(domain);
2799 spin_unlock_irqrestore(&domain->lock, flags);
2803 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
2805 static int __flush_pasid(struct protection_domain *domain, u32 pasid,
2806 u64 address, bool size)
2808 struct iommu_dev_data *dev_data;
2809 struct iommu_cmd cmd;
2812 if (!(domain->flags & PD_IOMMUV2_MASK))
2815 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
2818 * IOMMU TLB needs to be flushed before Device TLB to
2819 * prevent device TLB refill from IOMMU TLB
2821 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
2822 if (domain->dev_iommu[i] == 0)
2825 ret = iommu_queue_command(amd_iommus[i], &cmd);
2830 /* Wait until IOMMU TLB flushes are complete */
2831 domain_flush_complete(domain);
2833 /* Now flush device TLBs */
2834 list_for_each_entry(dev_data, &domain->dev_list, list) {
2835 struct amd_iommu *iommu;
2839 There might be non-IOMMUv2 capable devices in an IOMMUv2
2842 if (!dev_data->ats.enabled)
2845 qdep = dev_data->ats.qdep;
2846 iommu = amd_iommu_rlookup_table[dev_data->devid];
2848 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
2849 qdep, address, size);
2851 ret = iommu_queue_command(iommu, &cmd);
2856 /* Wait until all device TLBs are flushed */
2857 domain_flush_complete(domain);
2866 static int __amd_iommu_flush_page(struct protection_domain *domain, u32 pasid,
2869 return __flush_pasid(domain, pasid, address, false);
2872 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid,
2875 struct protection_domain *domain = to_pdomain(dom);
2876 unsigned long flags;
2879 spin_lock_irqsave(&domain->lock, flags);
2880 ret = __amd_iommu_flush_page(domain, pasid, address);
2881 spin_unlock_irqrestore(&domain->lock, flags);
2885 EXPORT_SYMBOL(amd_iommu_flush_page);
2887 static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid)
2889 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
2893 int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid)
2895 struct protection_domain *domain = to_pdomain(dom);
2896 unsigned long flags;
2899 spin_lock_irqsave(&domain->lock, flags);
2900 ret = __amd_iommu_flush_tlb(domain, pasid);
2901 spin_unlock_irqrestore(&domain->lock, flags);
2905 EXPORT_SYMBOL(amd_iommu_flush_tlb);
2907 static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc)
2914 index = (pasid >> (9 * level)) & 0x1ff;
2920 if (!(*pte & GCR3_VALID)) {
2924 root = (void *)get_zeroed_page(GFP_ATOMIC);
2928 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
2931 root = iommu_phys_to_virt(*pte & PAGE_MASK);
2939 static int __set_gcr3(struct protection_domain *domain, u32 pasid,
2942 struct domain_pgtable pgtable;
2945 amd_iommu_domain_get_pgtable(domain, &pgtable);
2946 if (pgtable.mode != PAGE_MODE_NONE)
2949 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
2953 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
2955 return __amd_iommu_flush_tlb(domain, pasid);
2958 static int __clear_gcr3(struct protection_domain *domain, u32 pasid)
2960 struct domain_pgtable pgtable;
2963 amd_iommu_domain_get_pgtable(domain, &pgtable);
2964 if (pgtable.mode != PAGE_MODE_NONE)
2967 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
2973 return __amd_iommu_flush_tlb(domain, pasid);
2976 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
2979 struct protection_domain *domain = to_pdomain(dom);
2980 unsigned long flags;
2983 spin_lock_irqsave(&domain->lock, flags);
2984 ret = __set_gcr3(domain, pasid, cr3);
2985 spin_unlock_irqrestore(&domain->lock, flags);
2989 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
2991 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid)
2993 struct protection_domain *domain = to_pdomain(dom);
2994 unsigned long flags;
2997 spin_lock_irqsave(&domain->lock, flags);
2998 ret = __clear_gcr3(domain, pasid);
2999 spin_unlock_irqrestore(&domain->lock, flags);
3003 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
3005 int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
3006 int status, int tag)
3008 struct iommu_dev_data *dev_data;
3009 struct amd_iommu *iommu;
3010 struct iommu_cmd cmd;
3012 dev_data = dev_iommu_priv_get(&pdev->dev);
3013 iommu = amd_iommu_rlookup_table[dev_data->devid];
3015 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3016 tag, dev_data->pri_tlp);
3018 return iommu_queue_command(iommu, &cmd);
3020 EXPORT_SYMBOL(amd_iommu_complete_ppr);
3022 struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3024 struct protection_domain *pdomain;
3025 struct iommu_dev_data *dev_data;
3026 struct device *dev = &pdev->dev;
3027 struct iommu_domain *io_domain;
3029 if (!check_device(dev))
3032 dev_data = dev_iommu_priv_get(&pdev->dev);
3033 pdomain = dev_data->domain;
3034 io_domain = iommu_get_domain_for_dev(dev);
3036 if (pdomain == NULL && dev_data->defer_attach) {
3037 dev_data->defer_attach = false;
3038 pdomain = to_pdomain(io_domain);
3039 attach_device(dev, pdomain);
3042 if (pdomain == NULL)
3045 if (io_domain->type != IOMMU_DOMAIN_DMA)
3048 /* Only return IOMMUv2 domains */
3049 if (!(pdomain->flags & PD_IOMMUV2_MASK))
3052 return &pdomain->domain;
3054 EXPORT_SYMBOL(amd_iommu_get_v2_domain);
3056 void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3058 struct iommu_dev_data *dev_data;
3060 if (!amd_iommu_v2_supported())
3063 dev_data = dev_iommu_priv_get(&pdev->dev);
3064 dev_data->errata |= (1 << erratum);
3066 EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
3068 int amd_iommu_device_info(struct pci_dev *pdev,
3069 struct amd_iommu_device_info *info)
3074 if (pdev == NULL || info == NULL)
3077 if (!amd_iommu_v2_supported())
3080 memset(info, 0, sizeof(*info));
3082 if (pci_ats_supported(pdev))
3083 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3085 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3087 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3089 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3093 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3094 max_pasids = min(max_pasids, (1 << 20));
3096 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3097 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3099 features = pci_pasid_features(pdev);
3100 if (features & PCI_PASID_CAP_EXEC)
3101 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3102 if (features & PCI_PASID_CAP_PRIV)
3103 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3108 EXPORT_SYMBOL(amd_iommu_device_info);
3110 #ifdef CONFIG_IRQ_REMAP
3112 /*****************************************************************************
3114 * Interrupt Remapping Implementation
3116 *****************************************************************************/
3118 static struct irq_chip amd_ir_chip;
3119 static DEFINE_SPINLOCK(iommu_table_lock);
3121 static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3125 dte = amd_iommu_dev_table[devid].data[2];
3126 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3127 dte |= iommu_virt_to_phys(table->table);
3128 dte |= DTE_IRQ_REMAP_INTCTL;
3129 dte |= DTE_IRQ_TABLE_LEN;
3130 dte |= DTE_IRQ_REMAP_ENABLE;
3132 amd_iommu_dev_table[devid].data[2] = dte;
3135 static struct irq_remap_table *get_irq_table(u16 devid)
3137 struct irq_remap_table *table;
3139 if (WARN_ONCE(!amd_iommu_rlookup_table[devid],
3140 "%s: no iommu for devid %x\n", __func__, devid))
3143 table = irq_lookup_table[devid];
3144 if (WARN_ONCE(!table, "%s: no table for devid %x\n", __func__, devid))
3150 static struct irq_remap_table *__alloc_irq_table(void)
3152 struct irq_remap_table *table;
3154 table = kzalloc(sizeof(*table), GFP_KERNEL);
3158 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
3159 if (!table->table) {
3163 raw_spin_lock_init(&table->lock);
3165 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3166 memset(table->table, 0,
3167 MAX_IRQS_PER_TABLE * sizeof(u32));
3169 memset(table->table, 0,
3170 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
3174 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
3175 struct irq_remap_table *table)
3177 irq_lookup_table[devid] = table;
3178 set_dte_irq_entry(devid, table);
3179 iommu_flush_dte(iommu, devid);
3182 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
3185 struct irq_remap_table *table = data;
3187 irq_lookup_table[alias] = table;
3188 set_dte_irq_entry(alias, table);
3190 iommu_flush_dte(amd_iommu_rlookup_table[alias], alias);
3195 static struct irq_remap_table *alloc_irq_table(u16 devid, struct pci_dev *pdev)
3197 struct irq_remap_table *table = NULL;
3198 struct irq_remap_table *new_table = NULL;
3199 struct amd_iommu *iommu;
3200 unsigned long flags;
3203 spin_lock_irqsave(&iommu_table_lock, flags);
3205 iommu = amd_iommu_rlookup_table[devid];
3209 table = irq_lookup_table[devid];
3213 alias = amd_iommu_alias_table[devid];
3214 table = irq_lookup_table[alias];
3216 set_remap_table_entry(iommu, devid, table);
3219 spin_unlock_irqrestore(&iommu_table_lock, flags);
3221 /* Nothing there yet, allocate new irq remapping table */
3222 new_table = __alloc_irq_table();
3226 spin_lock_irqsave(&iommu_table_lock, flags);
3228 table = irq_lookup_table[devid];
3232 table = irq_lookup_table[alias];
3234 set_remap_table_entry(iommu, devid, table);
3242 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
3245 set_remap_table_entry(iommu, devid, table);
3248 set_remap_table_entry(iommu, alias, table);
3251 iommu_completion_wait(iommu);
3254 spin_unlock_irqrestore(&iommu_table_lock, flags);
3257 kmem_cache_free(amd_iommu_irq_cache, new_table->table);
3263 static int alloc_irq_index(u16 devid, int count, bool align,
3264 struct pci_dev *pdev)
3266 struct irq_remap_table *table;
3267 int index, c, alignment = 1;
3268 unsigned long flags;
3269 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3274 table = alloc_irq_table(devid, pdev);
3279 alignment = roundup_pow_of_two(count);
3281 raw_spin_lock_irqsave(&table->lock, flags);
3283 /* Scan table for free entries */
3284 for (index = ALIGN(table->min_index, alignment), c = 0;
3285 index < MAX_IRQS_PER_TABLE;) {
3286 if (!iommu->irte_ops->is_allocated(table, index)) {
3290 index = ALIGN(index + 1, alignment);
3296 iommu->irte_ops->set_allocated(table, index - c + 1);
3308 raw_spin_unlock_irqrestore(&table->lock, flags);
3313 static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
3314 struct amd_ir_data *data)
3317 struct irq_remap_table *table;
3318 struct amd_iommu *iommu;
3319 unsigned long flags;
3320 struct irte_ga *entry;
3322 iommu = amd_iommu_rlookup_table[devid];
3326 table = get_irq_table(devid);
3330 raw_spin_lock_irqsave(&table->lock, flags);
3332 entry = (struct irte_ga *)table->table;
3333 entry = &entry[index];
3335 ret = cmpxchg_double(&entry->lo.val, &entry->hi.val,
3336 entry->lo.val, entry->hi.val,
3337 irte->lo.val, irte->hi.val);
3339 * We use cmpxchg16 to atomically update the 128-bit IRTE,
3340 * and it cannot be updated by the hardware or other processors
3341 * behind us, so the return value of cmpxchg16 should be the
3342 * same as the old value.
3349 raw_spin_unlock_irqrestore(&table->lock, flags);
3351 iommu_flush_irt(iommu, devid);
3352 iommu_completion_wait(iommu);
3357 static int modify_irte(u16 devid, int index, union irte *irte)
3359 struct irq_remap_table *table;
3360 struct amd_iommu *iommu;
3361 unsigned long flags;
3363 iommu = amd_iommu_rlookup_table[devid];
3367 table = get_irq_table(devid);
3371 raw_spin_lock_irqsave(&table->lock, flags);
3372 table->table[index] = irte->val;
3373 raw_spin_unlock_irqrestore(&table->lock, flags);
3375 iommu_flush_irt(iommu, devid);
3376 iommu_completion_wait(iommu);
3381 static void free_irte(u16 devid, int index)
3383 struct irq_remap_table *table;
3384 struct amd_iommu *iommu;
3385 unsigned long flags;
3387 iommu = amd_iommu_rlookup_table[devid];
3391 table = get_irq_table(devid);
3395 raw_spin_lock_irqsave(&table->lock, flags);
3396 iommu->irte_ops->clear_allocated(table, index);
3397 raw_spin_unlock_irqrestore(&table->lock, flags);
3399 iommu_flush_irt(iommu, devid);
3400 iommu_completion_wait(iommu);
3403 static void irte_prepare(void *entry,
3404 u32 delivery_mode, u32 dest_mode,
3405 u8 vector, u32 dest_apicid, int devid)
3407 union irte *irte = (union irte *) entry;
3410 irte->fields.vector = vector;
3411 irte->fields.int_type = delivery_mode;
3412 irte->fields.destination = dest_apicid;
3413 irte->fields.dm = dest_mode;
3414 irte->fields.valid = 1;
3417 static void irte_ga_prepare(void *entry,
3418 u32 delivery_mode, u32 dest_mode,
3419 u8 vector, u32 dest_apicid, int devid)
3421 struct irte_ga *irte = (struct irte_ga *) entry;
3425 irte->lo.fields_remap.int_type = delivery_mode;
3426 irte->lo.fields_remap.dm = dest_mode;
3427 irte->hi.fields.vector = vector;
3428 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
3429 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
3430 irte->lo.fields_remap.valid = 1;
3433 static void irte_activate(void *entry, u16 devid, u16 index)
3435 union irte *irte = (union irte *) entry;
3437 irte->fields.valid = 1;
3438 modify_irte(devid, index, irte);
3441 static void irte_ga_activate(void *entry, u16 devid, u16 index)
3443 struct irte_ga *irte = (struct irte_ga *) entry;
3445 irte->lo.fields_remap.valid = 1;
3446 modify_irte_ga(devid, index, irte, NULL);
3449 static void irte_deactivate(void *entry, u16 devid, u16 index)
3451 union irte *irte = (union irte *) entry;
3453 irte->fields.valid = 0;
3454 modify_irte(devid, index, irte);
3457 static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
3459 struct irte_ga *irte = (struct irte_ga *) entry;
3461 irte->lo.fields_remap.valid = 0;
3462 modify_irte_ga(devid, index, irte, NULL);
3465 static void irte_set_affinity(void *entry, u16 devid, u16 index,
3466 u8 vector, u32 dest_apicid)
3468 union irte *irte = (union irte *) entry;
3470 irte->fields.vector = vector;
3471 irte->fields.destination = dest_apicid;
3472 modify_irte(devid, index, irte);
3475 static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
3476 u8 vector, u32 dest_apicid)
3478 struct irte_ga *irte = (struct irte_ga *) entry;
3480 if (!irte->lo.fields_remap.guest_mode) {
3481 irte->hi.fields.vector = vector;
3482 irte->lo.fields_remap.destination =
3483 APICID_TO_IRTE_DEST_LO(dest_apicid);
3484 irte->hi.fields.destination =
3485 APICID_TO_IRTE_DEST_HI(dest_apicid);
3486 modify_irte_ga(devid, index, irte, NULL);
3490 #define IRTE_ALLOCATED (~1U)
3491 static void irte_set_allocated(struct irq_remap_table *table, int index)
3493 table->table[index] = IRTE_ALLOCATED;
3496 static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3498 struct irte_ga *ptr = (struct irte_ga *)table->table;
3499 struct irte_ga *irte = &ptr[index];
3501 memset(&irte->lo.val, 0, sizeof(u64));
3502 memset(&irte->hi.val, 0, sizeof(u64));
3503 irte->hi.fields.vector = 0xff;
3506 static bool irte_is_allocated(struct irq_remap_table *table, int index)
3508 union irte *ptr = (union irte *)table->table;
3509 union irte *irte = &ptr[index];
3511 return irte->val != 0;
3514 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3516 struct irte_ga *ptr = (struct irte_ga *)table->table;
3517 struct irte_ga *irte = &ptr[index];
3519 return irte->hi.fields.vector != 0;
3522 static void irte_clear_allocated(struct irq_remap_table *table, int index)
3524 table->table[index] = 0;
3527 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3529 struct irte_ga *ptr = (struct irte_ga *)table->table;
3530 struct irte_ga *irte = &ptr[index];
3532 memset(&irte->lo.val, 0, sizeof(u64));
3533 memset(&irte->hi.val, 0, sizeof(u64));
3536 static int get_devid(struct irq_alloc_info *info)
3538 switch (info->type) {
3539 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3540 case X86_IRQ_ALLOC_TYPE_IOAPIC_GET_PARENT:
3541 return get_ioapic_devid(info->devid);
3542 case X86_IRQ_ALLOC_TYPE_HPET:
3543 case X86_IRQ_ALLOC_TYPE_HPET_GET_PARENT:
3544 return get_hpet_devid(info->devid);
3545 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3546 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3547 return get_device_id(msi_desc_to_dev(info->desc));
3554 static struct irq_domain *get_irq_domain_for_devid(struct irq_alloc_info *info,
3557 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3562 switch (info->type) {
3563 case X86_IRQ_ALLOC_TYPE_IOAPIC_GET_PARENT:
3564 case X86_IRQ_ALLOC_TYPE_HPET_GET_PARENT:
3565 return iommu->ir_domain;
3572 static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
3579 devid = get_devid(info);
3582 return get_irq_domain_for_devid(info, devid);
3585 struct irq_remap_ops amd_iommu_irq_ops = {
3586 .prepare = amd_iommu_prepare,
3587 .enable = amd_iommu_enable,
3588 .disable = amd_iommu_disable,
3589 .reenable = amd_iommu_reenable,
3590 .enable_faulting = amd_iommu_enable_faulting,
3591 .get_irq_domain = get_irq_domain,
3594 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3595 struct irq_cfg *irq_cfg,
3596 struct irq_alloc_info *info,
3597 int devid, int index, int sub_handle)
3599 struct irq_2_irte *irte_info = &data->irq_2_irte;
3600 struct msi_msg *msg = &data->msi_entry;
3601 struct IO_APIC_route_entry *entry;
3602 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3607 data->irq_2_irte.devid = devid;
3608 data->irq_2_irte.index = index + sub_handle;
3609 iommu->irte_ops->prepare(data->entry, apic->irq_delivery_mode,
3610 apic->irq_dest_mode, irq_cfg->vector,
3611 irq_cfg->dest_apicid, devid);
3613 switch (info->type) {
3614 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3615 /* Setup IOAPIC entry */
3616 entry = info->ioapic.entry;
3617 info->ioapic.entry = NULL;
3618 memset(entry, 0, sizeof(*entry));
3619 entry->vector = index;
3621 entry->trigger = info->ioapic.trigger;
3622 entry->polarity = info->ioapic.polarity;
3623 /* Mask level triggered irqs. */
3624 if (info->ioapic.trigger)
3628 case X86_IRQ_ALLOC_TYPE_HPET:
3629 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3630 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3631 msg->address_hi = MSI_ADDR_BASE_HI;
3632 msg->address_lo = MSI_ADDR_BASE_LO;
3633 msg->data = irte_info->index;
3642 struct amd_irte_ops irte_32_ops = {
3643 .prepare = irte_prepare,
3644 .activate = irte_activate,
3645 .deactivate = irte_deactivate,
3646 .set_affinity = irte_set_affinity,
3647 .set_allocated = irte_set_allocated,
3648 .is_allocated = irte_is_allocated,
3649 .clear_allocated = irte_clear_allocated,
3652 struct amd_irte_ops irte_128_ops = {
3653 .prepare = irte_ga_prepare,
3654 .activate = irte_ga_activate,
3655 .deactivate = irte_ga_deactivate,
3656 .set_affinity = irte_ga_set_affinity,
3657 .set_allocated = irte_ga_set_allocated,
3658 .is_allocated = irte_ga_is_allocated,
3659 .clear_allocated = irte_ga_clear_allocated,
3662 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3663 unsigned int nr_irqs, void *arg)
3665 struct irq_alloc_info *info = arg;
3666 struct irq_data *irq_data;
3667 struct amd_ir_data *data = NULL;
3668 struct irq_cfg *cfg;
3674 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI &&
3675 info->type != X86_IRQ_ALLOC_TYPE_PCI_MSIX)
3679 * With IRQ remapping enabled, don't need contiguous CPU vectors
3680 * to support multiple MSI interrupts.
3682 if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
3683 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3685 devid = get_devid(info);
3689 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3693 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3694 struct irq_remap_table *table;
3695 struct amd_iommu *iommu;
3697 table = alloc_irq_table(devid, NULL);
3699 if (!table->min_index) {
3701 * Keep the first 32 indexes free for IOAPIC
3704 table->min_index = 32;
3705 iommu = amd_iommu_rlookup_table[devid];
3706 for (i = 0; i < 32; ++i)
3707 iommu->irte_ops->set_allocated(table, i);
3709 WARN_ON(table->min_index != 32);
3710 index = info->ioapic.pin;
3714 } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
3715 info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
3716 bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
3718 index = alloc_irq_index(devid, nr_irqs, align,
3719 msi_desc_to_pci_dev(info->desc));
3721 index = alloc_irq_index(devid, nr_irqs, false, NULL);
3725 pr_warn("Failed to allocate IRTE\n");
3727 goto out_free_parent;
3730 for (i = 0; i < nr_irqs; i++) {
3731 irq_data = irq_domain_get_irq_data(domain, virq + i);
3732 cfg = irq_data ? irqd_cfg(irq_data) : NULL;
3739 data = kzalloc(sizeof(*data), GFP_KERNEL);
3743 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3744 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
3746 data->entry = kzalloc(sizeof(struct irte_ga),
3753 irq_data->hwirq = (devid << 16) + i;
3754 irq_data->chip_data = data;
3755 irq_data->chip = &amd_ir_chip;
3756 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3757 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3763 for (i--; i >= 0; i--) {
3764 irq_data = irq_domain_get_irq_data(domain, virq + i);
3766 kfree(irq_data->chip_data);
3768 for (i = 0; i < nr_irqs; i++)
3769 free_irte(devid, index + i);
3771 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3775 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3776 unsigned int nr_irqs)
3778 struct irq_2_irte *irte_info;
3779 struct irq_data *irq_data;
3780 struct amd_ir_data *data;
3783 for (i = 0; i < nr_irqs; i++) {
3784 irq_data = irq_domain_get_irq_data(domain, virq + i);
3785 if (irq_data && irq_data->chip_data) {
3786 data = irq_data->chip_data;
3787 irte_info = &data->irq_2_irte;
3788 free_irte(irte_info->devid, irte_info->index);
3793 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3796 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3797 struct amd_ir_data *ir_data,
3798 struct irq_2_irte *irte_info,
3799 struct irq_cfg *cfg);
3801 static int irq_remapping_activate(struct irq_domain *domain,
3802 struct irq_data *irq_data, bool reserve)
3804 struct amd_ir_data *data = irq_data->chip_data;
3805 struct irq_2_irte *irte_info = &data->irq_2_irte;
3806 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3807 struct irq_cfg *cfg = irqd_cfg(irq_data);
3812 iommu->irte_ops->activate(data->entry, irte_info->devid,
3814 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
3818 static void irq_remapping_deactivate(struct irq_domain *domain,
3819 struct irq_data *irq_data)
3821 struct amd_ir_data *data = irq_data->chip_data;
3822 struct irq_2_irte *irte_info = &data->irq_2_irte;
3823 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3826 iommu->irte_ops->deactivate(data->entry, irte_info->devid,
3830 static const struct irq_domain_ops amd_ir_domain_ops = {
3831 .alloc = irq_remapping_alloc,
3832 .free = irq_remapping_free,
3833 .activate = irq_remapping_activate,
3834 .deactivate = irq_remapping_deactivate,
3837 int amd_iommu_activate_guest_mode(void *data)
3839 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3840 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3843 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3844 !entry || entry->lo.fields_vapic.guest_mode)
3847 valid = entry->lo.fields_vapic.valid;
3852 entry->lo.fields_vapic.valid = valid;
3853 entry->lo.fields_vapic.guest_mode = 1;
3854 entry->lo.fields_vapic.ga_log_intr = 1;
3855 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
3856 entry->hi.fields.vector = ir_data->ga_vector;
3857 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
3859 return modify_irte_ga(ir_data->irq_2_irte.devid,
3860 ir_data->irq_2_irte.index, entry, ir_data);
3862 EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
3864 int amd_iommu_deactivate_guest_mode(void *data)
3866 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3867 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3868 struct irq_cfg *cfg = ir_data->cfg;
3871 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3872 !entry || !entry->lo.fields_vapic.guest_mode)
3875 valid = entry->lo.fields_remap.valid;
3880 entry->lo.fields_remap.valid = valid;
3881 entry->lo.fields_remap.dm = apic->irq_dest_mode;
3882 entry->lo.fields_remap.int_type = apic->irq_delivery_mode;
3883 entry->hi.fields.vector = cfg->vector;
3884 entry->lo.fields_remap.destination =
3885 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
3886 entry->hi.fields.destination =
3887 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
3889 return modify_irte_ga(ir_data->irq_2_irte.devid,
3890 ir_data->irq_2_irte.index, entry, ir_data);
3892 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
3894 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
3897 struct amd_iommu *iommu;
3898 struct amd_iommu_pi_data *pi_data = vcpu_info;
3899 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
3900 struct amd_ir_data *ir_data = data->chip_data;
3901 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3902 struct iommu_dev_data *dev_data = search_dev_data(irte_info->devid);
3905 * This device has never been set up for guest mode.
3906 * we should not modify the IRTE
3908 if (!dev_data || !dev_data->use_vapic)
3911 ir_data->cfg = irqd_cfg(data);
3912 pi_data->ir_data = ir_data;
3915 * SVM tries to set up for VAPIC mode, but we are in
3916 * legacy mode. So, we force legacy mode instead.
3918 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3919 pr_debug("%s: Fall back to using intr legacy remap\n",
3921 pi_data->is_guest_mode = false;
3924 iommu = amd_iommu_rlookup_table[irte_info->devid];
3928 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
3929 if (pi_data->is_guest_mode) {
3930 ir_data->ga_root_ptr = (pi_data->base >> 12);
3931 ir_data->ga_vector = vcpu_pi_info->vector;
3932 ir_data->ga_tag = pi_data->ga_tag;
3933 ret = amd_iommu_activate_guest_mode(ir_data);
3935 ir_data->cached_ga_tag = pi_data->ga_tag;
3937 ret = amd_iommu_deactivate_guest_mode(ir_data);
3940 * This communicates the ga_tag back to the caller
3941 * so that it can do all the necessary clean up.
3944 ir_data->cached_ga_tag = 0;
3951 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3952 struct amd_ir_data *ir_data,
3953 struct irq_2_irte *irte_info,
3954 struct irq_cfg *cfg)
3958 * Atomically updates the IRTE with the new destination, vector
3959 * and flushes the interrupt entry cache.
3961 iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
3962 irte_info->index, cfg->vector,
3966 static int amd_ir_set_affinity(struct irq_data *data,
3967 const struct cpumask *mask, bool force)
3969 struct amd_ir_data *ir_data = data->chip_data;
3970 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3971 struct irq_cfg *cfg = irqd_cfg(data);
3972 struct irq_data *parent = data->parent_data;
3973 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3979 ret = parent->chip->irq_set_affinity(parent, mask, force);
3980 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3983 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
3985 * After this point, all the interrupts will start arriving
3986 * at the new destination. So, time to cleanup the previous
3987 * vector allocation.
3989 send_cleanup_vector(cfg);
3991 return IRQ_SET_MASK_OK_DONE;
3994 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3996 struct amd_ir_data *ir_data = irq_data->chip_data;
3998 *msg = ir_data->msi_entry;
4001 static struct irq_chip amd_ir_chip = {
4003 .irq_ack = apic_ack_irq,
4004 .irq_set_affinity = amd_ir_set_affinity,
4005 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
4006 .irq_compose_msi_msg = ir_compose_msi_msg,
4009 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4011 struct fwnode_handle *fn;
4013 fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
4016 iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
4017 if (!iommu->ir_domain) {
4018 irq_domain_free_fwnode(fn);
4022 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4023 iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
4029 int amd_iommu_update_ga(int cpu, bool is_run, void *data)
4031 unsigned long flags;
4032 struct amd_iommu *iommu;
4033 struct irq_remap_table *table;
4034 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4035 int devid = ir_data->irq_2_irte.devid;
4036 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4037 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
4039 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
4040 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
4043 iommu = amd_iommu_rlookup_table[devid];
4047 table = get_irq_table(devid);
4051 raw_spin_lock_irqsave(&table->lock, flags);
4053 if (ref->lo.fields_vapic.guest_mode) {
4055 ref->lo.fields_vapic.destination =
4056 APICID_TO_IRTE_DEST_LO(cpu);
4057 ref->hi.fields.destination =
4058 APICID_TO_IRTE_DEST_HI(cpu);
4060 ref->lo.fields_vapic.is_run = is_run;
4064 raw_spin_unlock_irqrestore(&table->lock, flags);
4066 iommu_flush_irt(iommu, devid);
4067 iommu_completion_wait(iommu);
4070 EXPORT_SYMBOL(amd_iommu_update_ga);