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-map-ops.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/irqdomain.h>
32 #include <linux/percpu.h>
33 #include <linux/iova.h>
34 #include <linux/io-pgtable.h>
35 #include <asm/irq_remapping.h>
36 #include <asm/io_apic.h>
38 #include <asm/hw_irq.h>
39 #include <asm/proto.h>
40 #include <asm/iommu.h>
44 #include "amd_iommu.h"
45 #include "../irq_remapping.h"
47 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
49 #define LOOP_TIMEOUT 100000
51 /* IO virtual address start page frame number */
52 #define IOVA_START_PFN (1)
53 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
55 /* Reserved IOVA ranges */
56 #define MSI_RANGE_START (0xfee00000)
57 #define MSI_RANGE_END (0xfeefffff)
58 #define HT_RANGE_START (0xfd00000000ULL)
59 #define HT_RANGE_END (0xffffffffffULL)
61 #define DEFAULT_PGTABLE_LEVEL PAGE_MODE_3_LEVEL
63 static DEFINE_SPINLOCK(pd_bitmap_lock);
65 /* List of all available dev_data structures */
66 static LLIST_HEAD(dev_data_list);
68 LIST_HEAD(ioapic_map);
70 LIST_HEAD(acpihid_map);
73 * Domain for untranslated devices - only allocated
74 * if iommu=pt passed on kernel cmd line.
76 const struct iommu_ops amd_iommu_ops;
78 static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
79 int amd_iommu_max_glx_val = -1;
82 * general struct to manage commands send to an IOMMU
88 struct kmem_cache *amd_iommu_irq_cache;
90 static void detach_device(struct device *dev);
92 /****************************************************************************
96 ****************************************************************************/
98 static inline u16 get_pci_device_id(struct device *dev)
100 struct pci_dev *pdev = to_pci_dev(dev);
102 return pci_dev_id(pdev);
105 static inline int get_acpihid_device_id(struct device *dev,
106 struct acpihid_map_entry **entry)
108 struct acpi_device *adev = ACPI_COMPANION(dev);
109 struct acpihid_map_entry *p;
114 list_for_each_entry(p, &acpihid_map, list) {
115 if (acpi_dev_hid_uid_match(adev, p->hid,
116 p->uid[0] ? p->uid : NULL)) {
125 static inline int get_device_id(struct device *dev)
130 devid = get_pci_device_id(dev);
132 devid = get_acpihid_device_id(dev, NULL);
137 static struct protection_domain *to_pdomain(struct iommu_domain *dom)
139 return container_of(dom, struct protection_domain, domain);
142 static struct iommu_dev_data *alloc_dev_data(u16 devid)
144 struct iommu_dev_data *dev_data;
146 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
150 spin_lock_init(&dev_data->lock);
151 dev_data->devid = devid;
152 ratelimit_default_init(&dev_data->rs);
154 llist_add(&dev_data->dev_data_list, &dev_data_list);
158 static struct iommu_dev_data *search_dev_data(u16 devid)
160 struct iommu_dev_data *dev_data;
161 struct llist_node *node;
163 if (llist_empty(&dev_data_list))
166 node = dev_data_list.first;
167 llist_for_each_entry(dev_data, node, dev_data_list) {
168 if (dev_data->devid == devid)
175 static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
177 u16 devid = pci_dev_id(pdev);
182 amd_iommu_rlookup_table[alias] =
183 amd_iommu_rlookup_table[devid];
184 memcpy(amd_iommu_dev_table[alias].data,
185 amd_iommu_dev_table[devid].data,
186 sizeof(amd_iommu_dev_table[alias].data));
191 static void clone_aliases(struct pci_dev *pdev)
197 * The IVRS alias stored in the alias table may not be
198 * part of the PCI DMA aliases if it's bus differs
199 * from the original device.
201 clone_alias(pdev, amd_iommu_alias_table[pci_dev_id(pdev)], NULL);
203 pci_for_each_dma_alias(pdev, clone_alias, NULL);
206 static struct pci_dev *setup_aliases(struct device *dev)
208 struct pci_dev *pdev = to_pci_dev(dev);
211 /* For ACPI HID devices, there are no aliases */
212 if (!dev_is_pci(dev))
216 * Add the IVRS alias to the pci aliases if it is on the same
217 * bus. The IVRS table may know about a quirk that we don't.
219 ivrs_alias = amd_iommu_alias_table[pci_dev_id(pdev)];
220 if (ivrs_alias != pci_dev_id(pdev) &&
221 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number)
222 pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1);
229 static struct iommu_dev_data *find_dev_data(u16 devid)
231 struct iommu_dev_data *dev_data;
232 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
234 dev_data = search_dev_data(devid);
236 if (dev_data == NULL) {
237 dev_data = alloc_dev_data(devid);
241 if (translation_pre_enabled(iommu))
242 dev_data->defer_attach = true;
249 * Find or create an IOMMU group for a acpihid device.
251 static struct iommu_group *acpihid_device_group(struct device *dev)
253 struct acpihid_map_entry *p, *entry = NULL;
256 devid = get_acpihid_device_id(dev, &entry);
258 return ERR_PTR(devid);
260 list_for_each_entry(p, &acpihid_map, list) {
261 if ((devid == p->devid) && p->group)
262 entry->group = p->group;
266 entry->group = generic_device_group(dev);
268 iommu_group_ref_get(entry->group);
273 static bool pci_iommuv2_capable(struct pci_dev *pdev)
275 static const int caps[] = {
277 PCI_EXT_CAP_ID_PASID,
281 if (!pci_ats_supported(pdev))
284 for (i = 0; i < 2; ++i) {
285 pos = pci_find_ext_capability(pdev, caps[i]);
293 static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
295 struct iommu_dev_data *dev_data;
297 dev_data = dev_iommu_priv_get(&pdev->dev);
299 return dev_data->errata & (1 << erratum) ? true : false;
303 * This function checks if the driver got a valid device from the caller to
304 * avoid dereferencing invalid pointers.
306 static bool check_device(struct device *dev)
313 devid = get_device_id(dev);
317 /* Out of our scope? */
318 if (devid > amd_iommu_last_bdf)
321 if (amd_iommu_rlookup_table[devid] == NULL)
327 static int iommu_init_device(struct device *dev)
329 struct iommu_dev_data *dev_data;
332 if (dev_iommu_priv_get(dev))
335 devid = get_device_id(dev);
339 dev_data = find_dev_data(devid);
343 dev_data->pdev = setup_aliases(dev);
346 * By default we use passthrough mode for IOMMUv2 capable device.
347 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
348 * invalid address), we ignore the capability for the device so
349 * it'll be forced to go into translation mode.
351 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
352 dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
353 struct amd_iommu *iommu;
355 iommu = amd_iommu_rlookup_table[dev_data->devid];
356 dev_data->iommu_v2 = iommu->is_iommu_v2;
359 dev_iommu_priv_set(dev, dev_data);
364 static void iommu_ignore_device(struct device *dev)
368 devid = get_device_id(dev);
372 amd_iommu_rlookup_table[devid] = NULL;
373 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
378 static void amd_iommu_uninit_device(struct device *dev)
380 struct iommu_dev_data *dev_data;
382 dev_data = dev_iommu_priv_get(dev);
386 if (dev_data->domain)
389 dev_iommu_priv_set(dev, NULL);
392 * We keep dev_data around for unplugged devices and reuse it when the
393 * device is re-plugged - not doing so would introduce a ton of races.
397 /****************************************************************************
399 * Interrupt handling functions
401 ****************************************************************************/
403 static void dump_dte_entry(u16 devid)
407 for (i = 0; i < 4; ++i)
408 pr_err("DTE[%d]: %016llx\n", i,
409 amd_iommu_dev_table[devid].data[i]);
412 static void dump_command(unsigned long phys_addr)
414 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
417 for (i = 0; i < 4; ++i)
418 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
421 static void amd_iommu_report_rmp_hw_error(volatile u32 *event)
423 struct iommu_dev_data *dev_data = NULL;
424 int devid, vmg_tag, flags;
425 struct pci_dev *pdev;
428 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
429 vmg_tag = (event[1]) & 0xFFFF;
430 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
431 spa = ((u64)event[3] << 32) | (event[2] & 0xFFFFFFF8);
433 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
436 dev_data = dev_iommu_priv_get(&pdev->dev);
438 if (dev_data && __ratelimit(&dev_data->rs)) {
439 pci_err(pdev, "Event logged [RMP_HW_ERROR vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
440 vmg_tag, spa, flags);
442 pr_err_ratelimited("Event logged [RMP_HW_ERROR device=%02x:%02x.%x, vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
443 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
444 vmg_tag, spa, flags);
451 static void amd_iommu_report_rmp_fault(volatile u32 *event)
453 struct iommu_dev_data *dev_data = NULL;
454 int devid, flags_rmp, vmg_tag, flags;
455 struct pci_dev *pdev;
458 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
459 flags_rmp = (event[0] >> EVENT_FLAGS_SHIFT) & 0xFF;
460 vmg_tag = (event[1]) & 0xFFFF;
461 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
462 gpa = ((u64)event[3] << 32) | event[2];
464 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
467 dev_data = dev_iommu_priv_get(&pdev->dev);
469 if (dev_data && __ratelimit(&dev_data->rs)) {
470 pci_err(pdev, "Event logged [RMP_PAGE_FAULT vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
471 vmg_tag, gpa, flags_rmp, flags);
473 pr_err_ratelimited("Event logged [RMP_PAGE_FAULT device=%02x:%02x.%x, vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
474 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
475 vmg_tag, gpa, flags_rmp, flags);
482 static void amd_iommu_report_page_fault(u16 devid, u16 domain_id,
483 u64 address, int flags)
485 struct iommu_dev_data *dev_data = NULL;
486 struct pci_dev *pdev;
488 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
491 dev_data = dev_iommu_priv_get(&pdev->dev);
493 if (dev_data && __ratelimit(&dev_data->rs)) {
494 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
495 domain_id, address, flags);
496 } else if (printk_ratelimit()) {
497 pr_err("Event logged [IO_PAGE_FAULT device=%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
498 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
499 domain_id, address, flags);
506 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
508 struct device *dev = iommu->iommu.dev;
509 int type, devid, flags, tag;
510 volatile u32 *event = __evt;
516 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
517 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
518 pasid = (event[0] & EVENT_DOMID_MASK_HI) |
519 (event[1] & EVENT_DOMID_MASK_LO);
520 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
521 address = (u64)(((u64)event[3]) << 32) | event[2];
524 /* Did we hit the erratum? */
525 if (++count == LOOP_TIMEOUT) {
526 pr_err("No event written to event log\n");
533 if (type == EVENT_TYPE_IO_FAULT) {
534 amd_iommu_report_page_fault(devid, pasid, address, flags);
539 case EVENT_TYPE_ILL_DEV:
540 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
541 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
542 pasid, address, flags);
543 dump_dte_entry(devid);
545 case EVENT_TYPE_DEV_TAB_ERR:
546 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
547 "address=0x%llx flags=0x%04x]\n",
548 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
551 case EVENT_TYPE_PAGE_TAB_ERR:
552 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
553 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
554 pasid, address, flags);
556 case EVENT_TYPE_ILL_CMD:
557 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
558 dump_command(address);
560 case EVENT_TYPE_CMD_HARD_ERR:
561 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
564 case EVENT_TYPE_IOTLB_INV_TO:
565 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%02x:%02x.%x address=0x%llx]\n",
566 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
569 case EVENT_TYPE_INV_DEV_REQ:
570 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
571 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
572 pasid, address, flags);
574 case EVENT_TYPE_RMP_FAULT:
575 amd_iommu_report_rmp_fault(event);
577 case EVENT_TYPE_RMP_HW_ERR:
578 amd_iommu_report_rmp_hw_error(event);
580 case EVENT_TYPE_INV_PPR_REQ:
581 pasid = PPR_PASID(*((u64 *)__evt));
582 tag = event[1] & 0x03FF;
583 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",
584 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
585 pasid, address, flags, tag);
588 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
589 event[0], event[1], event[2], event[3]);
592 memset(__evt, 0, 4 * sizeof(u32));
595 static void iommu_poll_events(struct amd_iommu *iommu)
599 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
600 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
602 while (head != tail) {
603 iommu_print_event(iommu, iommu->evt_buf + head);
604 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
607 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
610 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
612 struct amd_iommu_fault fault;
614 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
615 pr_err_ratelimited("Unknown PPR request received\n");
619 fault.address = raw[1];
620 fault.pasid = PPR_PASID(raw[0]);
621 fault.device_id = PPR_DEVID(raw[0]);
622 fault.tag = PPR_TAG(raw[0]);
623 fault.flags = PPR_FLAGS(raw[0]);
625 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
628 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
632 if (iommu->ppr_log == NULL)
635 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
636 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
638 while (head != tail) {
643 raw = (u64 *)(iommu->ppr_log + head);
646 * Hardware bug: Interrupt may arrive before the entry is
647 * written to memory. If this happens we need to wait for the
650 for (i = 0; i < LOOP_TIMEOUT; ++i) {
651 if (PPR_REQ_TYPE(raw[0]) != 0)
656 /* Avoid memcpy function-call overhead */
661 * To detect the hardware bug we need to clear the entry
664 raw[0] = raw[1] = 0UL;
666 /* Update head pointer of hardware ring-buffer */
667 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
668 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
670 /* Handle PPR entry */
671 iommu_handle_ppr_entry(iommu, entry);
673 /* Refresh ring-buffer information */
674 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
675 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
679 #ifdef CONFIG_IRQ_REMAP
680 static int (*iommu_ga_log_notifier)(u32);
682 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
684 iommu_ga_log_notifier = notifier;
688 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
690 static void iommu_poll_ga_log(struct amd_iommu *iommu)
692 u32 head, tail, cnt = 0;
694 if (iommu->ga_log == NULL)
697 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
698 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
700 while (head != tail) {
704 raw = (u64 *)(iommu->ga_log + head);
707 /* Avoid memcpy function-call overhead */
710 /* Update head pointer of hardware ring-buffer */
711 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
712 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
714 /* Handle GA entry */
715 switch (GA_REQ_TYPE(log_entry)) {
717 if (!iommu_ga_log_notifier)
720 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
721 __func__, GA_DEVID(log_entry),
724 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
725 pr_err("GA log notifier failed.\n");
734 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
736 if (!irq_remapping_enabled || !dev_is_pci(dev) ||
737 pci_dev_has_special_msi_domain(to_pci_dev(dev)))
740 dev_set_msi_domain(dev, iommu->msi_domain);
743 #else /* CONFIG_IRQ_REMAP */
745 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
746 #endif /* !CONFIG_IRQ_REMAP */
748 #define AMD_IOMMU_INT_MASK \
749 (MMIO_STATUS_EVT_INT_MASK | \
750 MMIO_STATUS_PPR_INT_MASK | \
751 MMIO_STATUS_GALOG_INT_MASK)
753 irqreturn_t amd_iommu_int_thread(int irq, void *data)
755 struct amd_iommu *iommu = (struct amd_iommu *) data;
756 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
758 while (status & AMD_IOMMU_INT_MASK) {
759 /* Enable EVT and PPR and GA interrupts again */
760 writel(AMD_IOMMU_INT_MASK,
761 iommu->mmio_base + MMIO_STATUS_OFFSET);
763 if (status & MMIO_STATUS_EVT_INT_MASK) {
764 pr_devel("Processing IOMMU Event Log\n");
765 iommu_poll_events(iommu);
768 if (status & MMIO_STATUS_PPR_INT_MASK) {
769 pr_devel("Processing IOMMU PPR Log\n");
770 iommu_poll_ppr_log(iommu);
773 #ifdef CONFIG_IRQ_REMAP
774 if (status & MMIO_STATUS_GALOG_INT_MASK) {
775 pr_devel("Processing IOMMU GA Log\n");
776 iommu_poll_ga_log(iommu);
781 * Hardware bug: ERBT1312
782 * When re-enabling interrupt (by writing 1
783 * to clear the bit), the hardware might also try to set
784 * the interrupt bit in the event status register.
785 * In this scenario, the bit will be set, and disable
786 * subsequent interrupts.
788 * Workaround: The IOMMU driver should read back the
789 * status register and check if the interrupt bits are cleared.
790 * If not, driver will need to go through the interrupt handler
791 * again and re-clear the bits
793 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
798 irqreturn_t amd_iommu_int_handler(int irq, void *data)
800 return IRQ_WAKE_THREAD;
803 /****************************************************************************
805 * IOMMU command queuing functions
807 ****************************************************************************/
809 static int wait_on_sem(struct amd_iommu *iommu, u64 data)
813 while (*iommu->cmd_sem != data && i < LOOP_TIMEOUT) {
818 if (i == LOOP_TIMEOUT) {
819 pr_alert("Completion-Wait loop timed out\n");
826 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
827 struct iommu_cmd *cmd)
832 /* Copy command to buffer */
833 tail = iommu->cmd_buf_tail;
834 target = iommu->cmd_buf + tail;
835 memcpy(target, cmd, sizeof(*cmd));
837 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
838 iommu->cmd_buf_tail = tail;
840 /* Tell the IOMMU about it */
841 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
844 static void build_completion_wait(struct iommu_cmd *cmd,
845 struct amd_iommu *iommu,
848 u64 paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
850 memset(cmd, 0, sizeof(*cmd));
851 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
852 cmd->data[1] = upper_32_bits(paddr);
854 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
857 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
859 memset(cmd, 0, sizeof(*cmd));
860 cmd->data[0] = devid;
861 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
864 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
865 size_t size, u16 domid, int pde)
870 pages = iommu_num_pages(address, size, PAGE_SIZE);
875 * If we have to flush more than one page, flush all
876 * TLB entries for this domain
878 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
882 address &= PAGE_MASK;
884 memset(cmd, 0, sizeof(*cmd));
885 cmd->data[1] |= domid;
886 cmd->data[2] = lower_32_bits(address);
887 cmd->data[3] = upper_32_bits(address);
888 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
889 if (s) /* size bit - we flush more than one 4kb page */
890 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
891 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
892 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
895 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
896 u64 address, size_t size)
901 pages = iommu_num_pages(address, size, PAGE_SIZE);
906 * If we have to flush more than one page, flush all
907 * TLB entries for this domain
909 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
913 address &= PAGE_MASK;
915 memset(cmd, 0, sizeof(*cmd));
916 cmd->data[0] = devid;
917 cmd->data[0] |= (qdep & 0xff) << 24;
918 cmd->data[1] = devid;
919 cmd->data[2] = lower_32_bits(address);
920 cmd->data[3] = upper_32_bits(address);
921 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
923 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
926 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
927 u64 address, bool size)
929 memset(cmd, 0, sizeof(*cmd));
931 address &= ~(0xfffULL);
933 cmd->data[0] = pasid;
934 cmd->data[1] = domid;
935 cmd->data[2] = lower_32_bits(address);
936 cmd->data[3] = upper_32_bits(address);
937 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
938 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
940 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
941 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
944 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid,
945 int qdep, u64 address, bool size)
947 memset(cmd, 0, sizeof(*cmd));
949 address &= ~(0xfffULL);
951 cmd->data[0] = devid;
952 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
953 cmd->data[0] |= (qdep & 0xff) << 24;
954 cmd->data[1] = devid;
955 cmd->data[1] |= (pasid & 0xff) << 16;
956 cmd->data[2] = lower_32_bits(address);
957 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
958 cmd->data[3] = upper_32_bits(address);
960 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
961 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
964 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
965 int status, int tag, bool gn)
967 memset(cmd, 0, sizeof(*cmd));
969 cmd->data[0] = devid;
971 cmd->data[1] = pasid;
972 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
974 cmd->data[3] = tag & 0x1ff;
975 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
977 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
980 static void build_inv_all(struct iommu_cmd *cmd)
982 memset(cmd, 0, sizeof(*cmd));
983 CMD_SET_TYPE(cmd, CMD_INV_ALL);
986 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
988 memset(cmd, 0, sizeof(*cmd));
989 cmd->data[0] = devid;
990 CMD_SET_TYPE(cmd, CMD_INV_IRT);
994 * Writes the command to the IOMMUs command buffer and informs the
995 * hardware about the new command.
997 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
998 struct iommu_cmd *cmd,
1001 unsigned int count = 0;
1002 u32 left, next_tail;
1004 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1006 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
1009 /* Skip udelay() the first time around */
1011 if (count == LOOP_TIMEOUT) {
1012 pr_err("Command buffer timeout\n");
1019 /* Update head and recheck remaining space */
1020 iommu->cmd_buf_head = readl(iommu->mmio_base +
1021 MMIO_CMD_HEAD_OFFSET);
1026 copy_cmd_to_buffer(iommu, cmd);
1028 /* Do we need to make sure all commands are processed? */
1029 iommu->need_sync = sync;
1034 static int iommu_queue_command_sync(struct amd_iommu *iommu,
1035 struct iommu_cmd *cmd,
1038 unsigned long flags;
1041 raw_spin_lock_irqsave(&iommu->lock, flags);
1042 ret = __iommu_queue_command_sync(iommu, cmd, sync);
1043 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1048 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1050 return iommu_queue_command_sync(iommu, cmd, true);
1054 * This function queues a completion wait command into the command
1055 * buffer of an IOMMU
1057 static int iommu_completion_wait(struct amd_iommu *iommu)
1059 struct iommu_cmd cmd;
1060 unsigned long flags;
1064 if (!iommu->need_sync)
1067 raw_spin_lock_irqsave(&iommu->lock, flags);
1069 data = ++iommu->cmd_sem_val;
1070 build_completion_wait(&cmd, iommu, data);
1072 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1076 ret = wait_on_sem(iommu, data);
1079 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1084 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1086 struct iommu_cmd cmd;
1088 build_inv_dte(&cmd, devid);
1090 return iommu_queue_command(iommu, &cmd);
1093 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
1097 for (devid = 0; devid <= 0xffff; ++devid)
1098 iommu_flush_dte(iommu, devid);
1100 iommu_completion_wait(iommu);
1104 * This function uses heavy locking and may disable irqs for some time. But
1105 * this is no issue because it is only called during resume.
1107 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
1111 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1112 struct iommu_cmd cmd;
1113 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1115 iommu_queue_command(iommu, &cmd);
1118 iommu_completion_wait(iommu);
1121 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1123 struct iommu_cmd cmd;
1125 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1127 iommu_queue_command(iommu, &cmd);
1129 iommu_completion_wait(iommu);
1132 static void amd_iommu_flush_all(struct amd_iommu *iommu)
1134 struct iommu_cmd cmd;
1136 build_inv_all(&cmd);
1138 iommu_queue_command(iommu, &cmd);
1139 iommu_completion_wait(iommu);
1142 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1144 struct iommu_cmd cmd;
1146 build_inv_irt(&cmd, devid);
1148 iommu_queue_command(iommu, &cmd);
1151 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
1155 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1156 iommu_flush_irt(iommu, devid);
1158 iommu_completion_wait(iommu);
1161 void iommu_flush_all_caches(struct amd_iommu *iommu)
1163 if (iommu_feature(iommu, FEATURE_IA)) {
1164 amd_iommu_flush_all(iommu);
1166 amd_iommu_flush_dte_all(iommu);
1167 amd_iommu_flush_irt_all(iommu);
1168 amd_iommu_flush_tlb_all(iommu);
1173 * Command send function for flushing on-device TLB
1175 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1176 u64 address, size_t size)
1178 struct amd_iommu *iommu;
1179 struct iommu_cmd cmd;
1182 qdep = dev_data->ats.qdep;
1183 iommu = amd_iommu_rlookup_table[dev_data->devid];
1185 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1187 return iommu_queue_command(iommu, &cmd);
1190 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
1192 struct amd_iommu *iommu = data;
1194 return iommu_flush_dte(iommu, alias);
1198 * Command send function for invalidating a device table entry
1200 static int device_flush_dte(struct iommu_dev_data *dev_data)
1202 struct amd_iommu *iommu;
1206 iommu = amd_iommu_rlookup_table[dev_data->devid];
1209 ret = pci_for_each_dma_alias(dev_data->pdev,
1210 device_flush_dte_alias, iommu);
1212 ret = iommu_flush_dte(iommu, dev_data->devid);
1216 alias = amd_iommu_alias_table[dev_data->devid];
1217 if (alias != dev_data->devid) {
1218 ret = iommu_flush_dte(iommu, alias);
1223 if (dev_data->ats.enabled)
1224 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1230 * TLB invalidation function which is called from the mapping functions.
1231 * It invalidates a single PTE if the range to flush is within a single
1232 * page. Otherwise it flushes the whole TLB of the IOMMU.
1234 static void __domain_flush_pages(struct protection_domain *domain,
1235 u64 address, size_t size, int pde)
1237 struct iommu_dev_data *dev_data;
1238 struct iommu_cmd cmd;
1241 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1243 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1244 if (!domain->dev_iommu[i])
1248 * Devices of this domain are behind this IOMMU
1249 * We need a TLB flush
1251 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1254 list_for_each_entry(dev_data, &domain->dev_list, list) {
1256 if (!dev_data->ats.enabled)
1259 ret |= device_flush_iotlb(dev_data, address, size);
1265 static void domain_flush_pages(struct protection_domain *domain,
1266 u64 address, size_t size)
1268 __domain_flush_pages(domain, address, size, 0);
1271 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1272 void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain)
1274 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1277 void amd_iommu_domain_flush_complete(struct protection_domain *domain)
1281 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1282 if (domain && !domain->dev_iommu[i])
1286 * Devices of this domain are behind this IOMMU
1287 * We need to wait for completion of all commands.
1289 iommu_completion_wait(amd_iommus[i]);
1293 /* Flush the not present cache if it exists */
1294 static void domain_flush_np_cache(struct protection_domain *domain,
1295 dma_addr_t iova, size_t size)
1297 if (unlikely(amd_iommu_np_cache)) {
1298 unsigned long flags;
1300 spin_lock_irqsave(&domain->lock, flags);
1301 domain_flush_pages(domain, iova, size);
1302 amd_iommu_domain_flush_complete(domain);
1303 spin_unlock_irqrestore(&domain->lock, flags);
1309 * This function flushes the DTEs for all devices in domain
1311 static void domain_flush_devices(struct protection_domain *domain)
1313 struct iommu_dev_data *dev_data;
1315 list_for_each_entry(dev_data, &domain->dev_list, list)
1316 device_flush_dte(dev_data);
1319 /****************************************************************************
1321 * The next functions belong to the domain allocation. A domain is
1322 * allocated for every IOMMU as the default domain. If device isolation
1323 * is enabled, every device get its own domain. The most important thing
1324 * about domains is the page table mapping the DMA address space they
1327 ****************************************************************************/
1329 static u16 domain_id_alloc(void)
1333 spin_lock(&pd_bitmap_lock);
1334 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1336 if (id > 0 && id < MAX_DOMAIN_ID)
1337 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1340 spin_unlock(&pd_bitmap_lock);
1345 static void domain_id_free(int id)
1347 spin_lock(&pd_bitmap_lock);
1348 if (id > 0 && id < MAX_DOMAIN_ID)
1349 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1350 spin_unlock(&pd_bitmap_lock);
1353 static void free_gcr3_tbl_level1(u64 *tbl)
1358 for (i = 0; i < 512; ++i) {
1359 if (!(tbl[i] & GCR3_VALID))
1362 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1364 free_page((unsigned long)ptr);
1368 static void free_gcr3_tbl_level2(u64 *tbl)
1373 for (i = 0; i < 512; ++i) {
1374 if (!(tbl[i] & GCR3_VALID))
1377 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1379 free_gcr3_tbl_level1(ptr);
1383 static void free_gcr3_table(struct protection_domain *domain)
1385 if (domain->glx == 2)
1386 free_gcr3_tbl_level2(domain->gcr3_tbl);
1387 else if (domain->glx == 1)
1388 free_gcr3_tbl_level1(domain->gcr3_tbl);
1390 BUG_ON(domain->glx != 0);
1392 free_page((unsigned long)domain->gcr3_tbl);
1395 static void set_dte_entry(u16 devid, struct protection_domain *domain,
1402 if (domain->iop.mode != PAGE_MODE_NONE)
1403 pte_root = iommu_virt_to_phys(domain->iop.root);
1405 pte_root |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
1406 << DEV_ENTRY_MODE_SHIFT;
1407 pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV;
1409 flags = amd_iommu_dev_table[devid].data[1];
1412 flags |= DTE_FLAG_IOTLB;
1415 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1417 if (iommu_feature(iommu, FEATURE_EPHSUP))
1418 pte_root |= 1ULL << DEV_ENTRY_PPR;
1421 if (domain->flags & PD_IOMMUV2_MASK) {
1422 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
1423 u64 glx = domain->glx;
1426 pte_root |= DTE_FLAG_GV;
1427 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1429 /* First mask out possible old values for GCR3 table */
1430 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1433 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1436 /* Encode GCR3 table into DTE */
1437 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1440 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1443 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1447 flags &= ~DEV_DOMID_MASK;
1448 flags |= domain->id;
1450 old_domid = amd_iommu_dev_table[devid].data[1] & DEV_DOMID_MASK;
1451 amd_iommu_dev_table[devid].data[1] = flags;
1452 amd_iommu_dev_table[devid].data[0] = pte_root;
1455 * A kdump kernel might be replacing a domain ID that was copied from
1456 * the previous kernel--if so, it needs to flush the translation cache
1457 * entries for the old domain ID that is being overwritten
1460 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1462 amd_iommu_flush_tlb_domid(iommu, old_domid);
1466 static void clear_dte_entry(u16 devid)
1468 /* remove entry from the device table seen by the hardware */
1469 amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV;
1470 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
1472 amd_iommu_apply_erratum_63(devid);
1475 static void do_attach(struct iommu_dev_data *dev_data,
1476 struct protection_domain *domain)
1478 struct amd_iommu *iommu;
1481 iommu = amd_iommu_rlookup_table[dev_data->devid];
1482 ats = dev_data->ats.enabled;
1484 /* Update data structures */
1485 dev_data->domain = domain;
1486 list_add(&dev_data->list, &domain->dev_list);
1488 /* Do reference counting */
1489 domain->dev_iommu[iommu->index] += 1;
1490 domain->dev_cnt += 1;
1492 /* Update device table */
1493 set_dte_entry(dev_data->devid, domain,
1494 ats, dev_data->iommu_v2);
1495 clone_aliases(dev_data->pdev);
1497 device_flush_dte(dev_data);
1500 static void do_detach(struct iommu_dev_data *dev_data)
1502 struct protection_domain *domain = dev_data->domain;
1503 struct amd_iommu *iommu;
1505 iommu = amd_iommu_rlookup_table[dev_data->devid];
1507 /* Update data structures */
1508 dev_data->domain = NULL;
1509 list_del(&dev_data->list);
1510 clear_dte_entry(dev_data->devid);
1511 clone_aliases(dev_data->pdev);
1513 /* Flush the DTE entry */
1514 device_flush_dte(dev_data);
1517 amd_iommu_domain_flush_tlb_pde(domain);
1519 /* Wait for the flushes to finish */
1520 amd_iommu_domain_flush_complete(domain);
1522 /* decrease reference counters - needs to happen after the flushes */
1523 domain->dev_iommu[iommu->index] -= 1;
1524 domain->dev_cnt -= 1;
1527 static void pdev_iommuv2_disable(struct pci_dev *pdev)
1529 pci_disable_ats(pdev);
1530 pci_disable_pri(pdev);
1531 pci_disable_pasid(pdev);
1534 /* FIXME: Change generic reset-function to do the same */
1535 static int pri_reset_while_enabled(struct pci_dev *pdev)
1540 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
1544 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1545 control |= PCI_PRI_CTRL_RESET;
1546 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
1551 static int pdev_iommuv2_enable(struct pci_dev *pdev)
1556 /* FIXME: Hardcode number of outstanding requests for now */
1558 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
1560 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
1562 /* Only allow access to user-accessible pages */
1563 ret = pci_enable_pasid(pdev, 0);
1567 /* First reset the PRI state of the device */
1568 ret = pci_reset_pri(pdev);
1573 ret = pci_enable_pri(pdev, reqs);
1578 ret = pri_reset_while_enabled(pdev);
1583 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1590 pci_disable_pri(pdev);
1591 pci_disable_pasid(pdev);
1597 * If a device is not yet associated with a domain, this function makes the
1598 * device visible in the domain
1600 static int attach_device(struct device *dev,
1601 struct protection_domain *domain)
1603 struct iommu_dev_data *dev_data;
1604 struct pci_dev *pdev;
1605 unsigned long flags;
1608 spin_lock_irqsave(&domain->lock, flags);
1610 dev_data = dev_iommu_priv_get(dev);
1612 spin_lock(&dev_data->lock);
1615 if (dev_data->domain != NULL)
1618 if (!dev_is_pci(dev))
1619 goto skip_ats_check;
1621 pdev = to_pci_dev(dev);
1622 if (domain->flags & PD_IOMMUV2_MASK) {
1623 struct iommu_domain *def_domain = iommu_get_dma_domain(dev);
1626 if (def_domain->type != IOMMU_DOMAIN_IDENTITY)
1629 if (dev_data->iommu_v2) {
1630 if (pdev_iommuv2_enable(pdev) != 0)
1633 dev_data->ats.enabled = true;
1634 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1635 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
1637 } else if (amd_iommu_iotlb_sup &&
1638 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1639 dev_data->ats.enabled = true;
1640 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1646 do_attach(dev_data, domain);
1649 * We might boot into a crash-kernel here. The crashed kernel
1650 * left the caches in the IOMMU dirty. So we have to flush
1651 * here to evict all dirty stuff.
1653 amd_iommu_domain_flush_tlb_pde(domain);
1655 amd_iommu_domain_flush_complete(domain);
1658 spin_unlock(&dev_data->lock);
1660 spin_unlock_irqrestore(&domain->lock, flags);
1666 * Removes a device from a protection domain (with devtable_lock held)
1668 static void detach_device(struct device *dev)
1670 struct protection_domain *domain;
1671 struct iommu_dev_data *dev_data;
1672 unsigned long flags;
1674 dev_data = dev_iommu_priv_get(dev);
1675 domain = dev_data->domain;
1677 spin_lock_irqsave(&domain->lock, flags);
1679 spin_lock(&dev_data->lock);
1682 * First check if the device is still attached. It might already
1683 * be detached from its domain because the generic
1684 * iommu_detach_group code detached it and we try again here in
1685 * our alias handling.
1687 if (WARN_ON(!dev_data->domain))
1690 do_detach(dev_data);
1692 if (!dev_is_pci(dev))
1695 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
1696 pdev_iommuv2_disable(to_pci_dev(dev));
1697 else if (dev_data->ats.enabled)
1698 pci_disable_ats(to_pci_dev(dev));
1700 dev_data->ats.enabled = false;
1703 spin_unlock(&dev_data->lock);
1705 spin_unlock_irqrestore(&domain->lock, flags);
1708 static struct iommu_device *amd_iommu_probe_device(struct device *dev)
1710 struct iommu_device *iommu_dev;
1711 struct amd_iommu *iommu;
1714 if (!check_device(dev))
1715 return ERR_PTR(-ENODEV);
1717 devid = get_device_id(dev);
1719 return ERR_PTR(devid);
1721 iommu = amd_iommu_rlookup_table[devid];
1723 if (dev_iommu_priv_get(dev))
1724 return &iommu->iommu;
1726 ret = iommu_init_device(dev);
1728 if (ret != -ENOTSUPP)
1729 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
1730 iommu_dev = ERR_PTR(ret);
1731 iommu_ignore_device(dev);
1733 amd_iommu_set_pci_msi_domain(dev, iommu);
1734 iommu_dev = &iommu->iommu;
1737 iommu_completion_wait(iommu);
1742 static void amd_iommu_probe_finalize(struct device *dev)
1744 struct iommu_domain *domain;
1746 /* Domains are initialized for this device - have a look what we ended up with */
1747 domain = iommu_get_domain_for_dev(dev);
1748 if (domain->type == IOMMU_DOMAIN_DMA)
1749 iommu_setup_dma_ops(dev, IOVA_START_PFN << PAGE_SHIFT, 0);
1752 static void amd_iommu_release_device(struct device *dev)
1754 int devid = get_device_id(dev);
1755 struct amd_iommu *iommu;
1757 if (!check_device(dev))
1760 iommu = amd_iommu_rlookup_table[devid];
1762 amd_iommu_uninit_device(dev);
1763 iommu_completion_wait(iommu);
1766 static struct iommu_group *amd_iommu_device_group(struct device *dev)
1768 if (dev_is_pci(dev))
1769 return pci_device_group(dev);
1771 return acpihid_device_group(dev);
1774 static int amd_iommu_domain_get_attr(struct iommu_domain *domain,
1775 enum iommu_attr attr, void *data)
1777 switch (domain->type) {
1778 case IOMMU_DOMAIN_UNMANAGED:
1780 case IOMMU_DOMAIN_DMA:
1782 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1783 *(int *)data = !amd_iommu_unmap_flush;
1794 /*****************************************************************************
1796 * The next functions belong to the dma_ops mapping/unmapping code.
1798 *****************************************************************************/
1800 static void update_device_table(struct protection_domain *domain)
1802 struct iommu_dev_data *dev_data;
1804 list_for_each_entry(dev_data, &domain->dev_list, list) {
1805 set_dte_entry(dev_data->devid, domain,
1806 dev_data->ats.enabled, dev_data->iommu_v2);
1807 clone_aliases(dev_data->pdev);
1811 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
1813 update_device_table(domain);
1814 domain_flush_devices(domain);
1817 void amd_iommu_domain_update(struct protection_domain *domain)
1819 /* Update device table */
1820 amd_iommu_update_and_flush_device_table(domain);
1822 /* Flush domain TLB(s) and wait for completion */
1823 amd_iommu_domain_flush_tlb_pde(domain);
1824 amd_iommu_domain_flush_complete(domain);
1827 int __init amd_iommu_init_api(void)
1831 ret = iova_cache_get();
1835 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
1838 #ifdef CONFIG_ARM_AMBA
1839 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
1843 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
1850 int __init amd_iommu_init_dma_ops(void)
1852 swiotlb = (iommu_default_passthrough() || sme_me_mask) ? 1 : 0;
1854 if (amd_iommu_unmap_flush)
1855 pr_info("IO/TLB flush on unmap enabled\n");
1857 pr_info("Lazy IO/TLB flushing enabled\n");
1863 /*****************************************************************************
1865 * The following functions belong to the exported interface of AMD IOMMU
1867 * This interface allows access to lower level functions of the IOMMU
1868 * like protection domain handling and assignement of devices to domains
1869 * which is not possible with the dma_ops interface.
1871 *****************************************************************************/
1873 static void cleanup_domain(struct protection_domain *domain)
1875 struct iommu_dev_data *entry;
1876 unsigned long flags;
1878 spin_lock_irqsave(&domain->lock, flags);
1880 while (!list_empty(&domain->dev_list)) {
1881 entry = list_first_entry(&domain->dev_list,
1882 struct iommu_dev_data, list);
1883 BUG_ON(!entry->domain);
1887 spin_unlock_irqrestore(&domain->lock, flags);
1890 static void protection_domain_free(struct protection_domain *domain)
1896 domain_id_free(domain->id);
1898 if (domain->iop.pgtbl_cfg.tlb)
1899 free_io_pgtable_ops(&domain->iop.iop.ops);
1904 static int protection_domain_init_v1(struct protection_domain *domain, int mode)
1906 u64 *pt_root = NULL;
1908 BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
1910 spin_lock_init(&domain->lock);
1911 domain->id = domain_id_alloc();
1914 INIT_LIST_HEAD(&domain->dev_list);
1916 if (mode != PAGE_MODE_NONE) {
1917 pt_root = (void *)get_zeroed_page(GFP_KERNEL);
1922 amd_iommu_domain_set_pgtable(domain, pt_root, mode);
1927 static struct protection_domain *protection_domain_alloc(unsigned int type)
1929 struct io_pgtable_ops *pgtbl_ops;
1930 struct protection_domain *domain;
1931 int pgtable = amd_iommu_pgtable;
1932 int mode = DEFAULT_PGTABLE_LEVEL;
1935 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
1940 * Force IOMMU v1 page table when iommu=pt and
1941 * when allocating domain for pass-through devices.
1943 if (type == IOMMU_DOMAIN_IDENTITY) {
1944 pgtable = AMD_IOMMU_V1;
1945 mode = PAGE_MODE_NONE;
1946 } else if (type == IOMMU_DOMAIN_UNMANAGED) {
1947 pgtable = AMD_IOMMU_V1;
1952 ret = protection_domain_init_v1(domain, mode);
1961 pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
1971 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
1973 struct protection_domain *domain;
1975 domain = protection_domain_alloc(type);
1979 domain->domain.geometry.aperture_start = 0;
1980 domain->domain.geometry.aperture_end = ~0ULL;
1981 domain->domain.geometry.force_aperture = true;
1983 if (type == IOMMU_DOMAIN_DMA &&
1984 iommu_get_dma_cookie(&domain->domain) == -ENOMEM)
1987 return &domain->domain;
1990 protection_domain_free(domain);
1995 static void amd_iommu_domain_free(struct iommu_domain *dom)
1997 struct protection_domain *domain;
1999 domain = to_pdomain(dom);
2001 if (domain->dev_cnt > 0)
2002 cleanup_domain(domain);
2004 BUG_ON(domain->dev_cnt != 0);
2009 if (dom->type == IOMMU_DOMAIN_DMA)
2010 iommu_put_dma_cookie(&domain->domain);
2012 if (domain->flags & PD_IOMMUV2_MASK)
2013 free_gcr3_table(domain);
2015 protection_domain_free(domain);
2018 static void amd_iommu_detach_device(struct iommu_domain *dom,
2021 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2022 struct amd_iommu *iommu;
2025 if (!check_device(dev))
2028 devid = get_device_id(dev);
2032 if (dev_data->domain != NULL)
2035 iommu = amd_iommu_rlookup_table[devid];
2039 #ifdef CONFIG_IRQ_REMAP
2040 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2041 (dom->type == IOMMU_DOMAIN_UNMANAGED))
2042 dev_data->use_vapic = 0;
2045 iommu_completion_wait(iommu);
2048 static int amd_iommu_attach_device(struct iommu_domain *dom,
2051 struct protection_domain *domain = to_pdomain(dom);
2052 struct iommu_dev_data *dev_data;
2053 struct amd_iommu *iommu;
2056 if (!check_device(dev))
2059 dev_data = dev_iommu_priv_get(dev);
2060 dev_data->defer_attach = false;
2062 iommu = amd_iommu_rlookup_table[dev_data->devid];
2066 if (dev_data->domain)
2069 ret = attach_device(dev, domain);
2071 #ifdef CONFIG_IRQ_REMAP
2072 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
2073 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
2074 dev_data->use_vapic = 1;
2076 dev_data->use_vapic = 0;
2080 iommu_completion_wait(iommu);
2085 static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2086 phys_addr_t paddr, size_t page_size, int iommu_prot,
2089 struct protection_domain *domain = to_pdomain(dom);
2090 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2094 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2095 (domain->iop.mode == PAGE_MODE_NONE))
2098 if (iommu_prot & IOMMU_READ)
2099 prot |= IOMMU_PROT_IR;
2100 if (iommu_prot & IOMMU_WRITE)
2101 prot |= IOMMU_PROT_IW;
2104 ret = ops->map(ops, iova, paddr, page_size, prot, gfp);
2105 domain_flush_np_cache(domain, iova, page_size);
2111 static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2113 struct iommu_iotlb_gather *gather)
2115 struct protection_domain *domain = to_pdomain(dom);
2116 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2118 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2119 (domain->iop.mode == PAGE_MODE_NONE))
2122 return (ops->unmap) ? ops->unmap(ops, iova, page_size, gather) : 0;
2125 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2128 struct protection_domain *domain = to_pdomain(dom);
2129 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2131 return ops->iova_to_phys(ops, iova);
2134 static bool amd_iommu_capable(enum iommu_cap cap)
2137 case IOMMU_CAP_CACHE_COHERENCY:
2139 case IOMMU_CAP_INTR_REMAP:
2140 return (irq_remapping_enabled == 1);
2141 case IOMMU_CAP_NOEXEC:
2150 static void amd_iommu_get_resv_regions(struct device *dev,
2151 struct list_head *head)
2153 struct iommu_resv_region *region;
2154 struct unity_map_entry *entry;
2157 devid = get_device_id(dev);
2161 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
2165 if (devid < entry->devid_start || devid > entry->devid_end)
2168 type = IOMMU_RESV_DIRECT;
2169 length = entry->address_end - entry->address_start;
2170 if (entry->prot & IOMMU_PROT_IR)
2172 if (entry->prot & IOMMU_PROT_IW)
2173 prot |= IOMMU_WRITE;
2174 if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
2175 /* Exclusion range */
2176 type = IOMMU_RESV_RESERVED;
2178 region = iommu_alloc_resv_region(entry->address_start,
2179 length, prot, type);
2181 dev_err(dev, "Out of memory allocating dm-regions\n");
2184 list_add_tail(®ion->list, head);
2187 region = iommu_alloc_resv_region(MSI_RANGE_START,
2188 MSI_RANGE_END - MSI_RANGE_START + 1,
2192 list_add_tail(®ion->list, head);
2194 region = iommu_alloc_resv_region(HT_RANGE_START,
2195 HT_RANGE_END - HT_RANGE_START + 1,
2196 0, IOMMU_RESV_RESERVED);
2199 list_add_tail(®ion->list, head);
2202 bool amd_iommu_is_attach_deferred(struct iommu_domain *domain,
2205 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2207 return dev_data->defer_attach;
2209 EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred);
2211 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
2213 struct protection_domain *dom = to_pdomain(domain);
2214 unsigned long flags;
2216 spin_lock_irqsave(&dom->lock, flags);
2217 amd_iommu_domain_flush_tlb_pde(dom);
2218 amd_iommu_domain_flush_complete(dom);
2219 spin_unlock_irqrestore(&dom->lock, flags);
2222 static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
2223 struct iommu_iotlb_gather *gather)
2225 amd_iommu_flush_iotlb_all(domain);
2228 static int amd_iommu_def_domain_type(struct device *dev)
2230 struct iommu_dev_data *dev_data;
2232 dev_data = dev_iommu_priv_get(dev);
2237 * Do not identity map IOMMUv2 capable devices when memory encryption is
2238 * active, because some of those devices (AMD GPUs) don't have the
2239 * encryption bit in their DMA-mask and require remapping.
2241 if (!mem_encrypt_active() && dev_data->iommu_v2)
2242 return IOMMU_DOMAIN_IDENTITY;
2247 const struct iommu_ops amd_iommu_ops = {
2248 .capable = amd_iommu_capable,
2249 .domain_alloc = amd_iommu_domain_alloc,
2250 .domain_free = amd_iommu_domain_free,
2251 .attach_dev = amd_iommu_attach_device,
2252 .detach_dev = amd_iommu_detach_device,
2253 .map = amd_iommu_map,
2254 .unmap = amd_iommu_unmap,
2255 .iova_to_phys = amd_iommu_iova_to_phys,
2256 .probe_device = amd_iommu_probe_device,
2257 .release_device = amd_iommu_release_device,
2258 .probe_finalize = amd_iommu_probe_finalize,
2259 .device_group = amd_iommu_device_group,
2260 .domain_get_attr = amd_iommu_domain_get_attr,
2261 .get_resv_regions = amd_iommu_get_resv_regions,
2262 .put_resv_regions = generic_iommu_put_resv_regions,
2263 .is_attach_deferred = amd_iommu_is_attach_deferred,
2264 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
2265 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2266 .iotlb_sync = amd_iommu_iotlb_sync,
2267 .def_domain_type = amd_iommu_def_domain_type,
2270 /*****************************************************************************
2272 * The next functions do a basic initialization of IOMMU for pass through
2275 * In passthrough mode the IOMMU is initialized and enabled but not used for
2276 * DMA-API translation.
2278 *****************************************************************************/
2280 /* IOMMUv2 specific functions */
2281 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2283 return atomic_notifier_chain_register(&ppr_notifier, nb);
2285 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2287 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2289 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2291 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
2293 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2295 struct protection_domain *domain = to_pdomain(dom);
2296 unsigned long flags;
2298 spin_lock_irqsave(&domain->lock, flags);
2300 if (domain->iop.pgtbl_cfg.tlb)
2301 free_io_pgtable_ops(&domain->iop.iop.ops);
2303 spin_unlock_irqrestore(&domain->lock, flags);
2305 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
2307 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
2309 struct protection_domain *domain = to_pdomain(dom);
2310 unsigned long flags;
2313 if (pasids <= 0 || pasids > (PASID_MASK + 1))
2316 /* Number of GCR3 table levels required */
2317 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
2320 if (levels > amd_iommu_max_glx_val)
2323 spin_lock_irqsave(&domain->lock, flags);
2326 * Save us all sanity checks whether devices already in the
2327 * domain support IOMMUv2. Just force that the domain has no
2328 * devices attached when it is switched into IOMMUv2 mode.
2331 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
2335 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
2336 if (domain->gcr3_tbl == NULL)
2339 domain->glx = levels;
2340 domain->flags |= PD_IOMMUV2_MASK;
2342 amd_iommu_domain_update(domain);
2347 spin_unlock_irqrestore(&domain->lock, flags);
2351 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
2353 static int __flush_pasid(struct protection_domain *domain, u32 pasid,
2354 u64 address, bool size)
2356 struct iommu_dev_data *dev_data;
2357 struct iommu_cmd cmd;
2360 if (!(domain->flags & PD_IOMMUV2_MASK))
2363 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
2366 * IOMMU TLB needs to be flushed before Device TLB to
2367 * prevent device TLB refill from IOMMU TLB
2369 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
2370 if (domain->dev_iommu[i] == 0)
2373 ret = iommu_queue_command(amd_iommus[i], &cmd);
2378 /* Wait until IOMMU TLB flushes are complete */
2379 amd_iommu_domain_flush_complete(domain);
2381 /* Now flush device TLBs */
2382 list_for_each_entry(dev_data, &domain->dev_list, list) {
2383 struct amd_iommu *iommu;
2387 There might be non-IOMMUv2 capable devices in an IOMMUv2
2390 if (!dev_data->ats.enabled)
2393 qdep = dev_data->ats.qdep;
2394 iommu = amd_iommu_rlookup_table[dev_data->devid];
2396 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
2397 qdep, address, size);
2399 ret = iommu_queue_command(iommu, &cmd);
2404 /* Wait until all device TLBs are flushed */
2405 amd_iommu_domain_flush_complete(domain);
2414 static int __amd_iommu_flush_page(struct protection_domain *domain, u32 pasid,
2417 return __flush_pasid(domain, pasid, address, false);
2420 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid,
2423 struct protection_domain *domain = to_pdomain(dom);
2424 unsigned long flags;
2427 spin_lock_irqsave(&domain->lock, flags);
2428 ret = __amd_iommu_flush_page(domain, pasid, address);
2429 spin_unlock_irqrestore(&domain->lock, flags);
2433 EXPORT_SYMBOL(amd_iommu_flush_page);
2435 static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid)
2437 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
2441 int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid)
2443 struct protection_domain *domain = to_pdomain(dom);
2444 unsigned long flags;
2447 spin_lock_irqsave(&domain->lock, flags);
2448 ret = __amd_iommu_flush_tlb(domain, pasid);
2449 spin_unlock_irqrestore(&domain->lock, flags);
2453 EXPORT_SYMBOL(amd_iommu_flush_tlb);
2455 static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc)
2462 index = (pasid >> (9 * level)) & 0x1ff;
2468 if (!(*pte & GCR3_VALID)) {
2472 root = (void *)get_zeroed_page(GFP_ATOMIC);
2476 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
2479 root = iommu_phys_to_virt(*pte & PAGE_MASK);
2487 static int __set_gcr3(struct protection_domain *domain, u32 pasid,
2492 if (domain->iop.mode != PAGE_MODE_NONE)
2495 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
2499 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
2501 return __amd_iommu_flush_tlb(domain, pasid);
2504 static int __clear_gcr3(struct protection_domain *domain, u32 pasid)
2508 if (domain->iop.mode != PAGE_MODE_NONE)
2511 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
2517 return __amd_iommu_flush_tlb(domain, pasid);
2520 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
2523 struct protection_domain *domain = to_pdomain(dom);
2524 unsigned long flags;
2527 spin_lock_irqsave(&domain->lock, flags);
2528 ret = __set_gcr3(domain, pasid, cr3);
2529 spin_unlock_irqrestore(&domain->lock, flags);
2533 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
2535 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid)
2537 struct protection_domain *domain = to_pdomain(dom);
2538 unsigned long flags;
2541 spin_lock_irqsave(&domain->lock, flags);
2542 ret = __clear_gcr3(domain, pasid);
2543 spin_unlock_irqrestore(&domain->lock, flags);
2547 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
2549 int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
2550 int status, int tag)
2552 struct iommu_dev_data *dev_data;
2553 struct amd_iommu *iommu;
2554 struct iommu_cmd cmd;
2556 dev_data = dev_iommu_priv_get(&pdev->dev);
2557 iommu = amd_iommu_rlookup_table[dev_data->devid];
2559 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
2560 tag, dev_data->pri_tlp);
2562 return iommu_queue_command(iommu, &cmd);
2564 EXPORT_SYMBOL(amd_iommu_complete_ppr);
2566 struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
2568 struct protection_domain *pdomain;
2569 struct iommu_dev_data *dev_data;
2570 struct device *dev = &pdev->dev;
2571 struct iommu_domain *io_domain;
2573 if (!check_device(dev))
2576 dev_data = dev_iommu_priv_get(&pdev->dev);
2577 pdomain = dev_data->domain;
2578 io_domain = iommu_get_domain_for_dev(dev);
2580 if (pdomain == NULL && dev_data->defer_attach) {
2581 dev_data->defer_attach = false;
2582 pdomain = to_pdomain(io_domain);
2583 attach_device(dev, pdomain);
2586 if (pdomain == NULL)
2589 if (io_domain->type != IOMMU_DOMAIN_DMA)
2592 /* Only return IOMMUv2 domains */
2593 if (!(pdomain->flags & PD_IOMMUV2_MASK))
2596 return &pdomain->domain;
2598 EXPORT_SYMBOL(amd_iommu_get_v2_domain);
2600 void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
2602 struct iommu_dev_data *dev_data;
2604 if (!amd_iommu_v2_supported())
2607 dev_data = dev_iommu_priv_get(&pdev->dev);
2608 dev_data->errata |= (1 << erratum);
2610 EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
2612 int amd_iommu_device_info(struct pci_dev *pdev,
2613 struct amd_iommu_device_info *info)
2618 if (pdev == NULL || info == NULL)
2621 if (!amd_iommu_v2_supported())
2624 memset(info, 0, sizeof(*info));
2626 if (pci_ats_supported(pdev))
2627 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
2629 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
2631 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
2633 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
2637 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
2638 max_pasids = min(max_pasids, (1 << 20));
2640 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
2641 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
2643 features = pci_pasid_features(pdev);
2644 if (features & PCI_PASID_CAP_EXEC)
2645 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
2646 if (features & PCI_PASID_CAP_PRIV)
2647 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
2652 EXPORT_SYMBOL(amd_iommu_device_info);
2654 #ifdef CONFIG_IRQ_REMAP
2656 /*****************************************************************************
2658 * Interrupt Remapping Implementation
2660 *****************************************************************************/
2662 static struct irq_chip amd_ir_chip;
2663 static DEFINE_SPINLOCK(iommu_table_lock);
2665 static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
2669 dte = amd_iommu_dev_table[devid].data[2];
2670 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
2671 dte |= iommu_virt_to_phys(table->table);
2672 dte |= DTE_IRQ_REMAP_INTCTL;
2673 dte |= DTE_INTTABLEN;
2674 dte |= DTE_IRQ_REMAP_ENABLE;
2676 amd_iommu_dev_table[devid].data[2] = dte;
2679 static struct irq_remap_table *get_irq_table(u16 devid)
2681 struct irq_remap_table *table;
2683 if (WARN_ONCE(!amd_iommu_rlookup_table[devid],
2684 "%s: no iommu for devid %x\n", __func__, devid))
2687 table = irq_lookup_table[devid];
2688 if (WARN_ONCE(!table, "%s: no table for devid %x\n", __func__, devid))
2694 static struct irq_remap_table *__alloc_irq_table(void)
2696 struct irq_remap_table *table;
2698 table = kzalloc(sizeof(*table), GFP_KERNEL);
2702 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
2703 if (!table->table) {
2707 raw_spin_lock_init(&table->lock);
2709 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2710 memset(table->table, 0,
2711 MAX_IRQS_PER_TABLE * sizeof(u32));
2713 memset(table->table, 0,
2714 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
2718 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
2719 struct irq_remap_table *table)
2721 irq_lookup_table[devid] = table;
2722 set_dte_irq_entry(devid, table);
2723 iommu_flush_dte(iommu, devid);
2726 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
2729 struct irq_remap_table *table = data;
2731 irq_lookup_table[alias] = table;
2732 set_dte_irq_entry(alias, table);
2734 iommu_flush_dte(amd_iommu_rlookup_table[alias], alias);
2739 static struct irq_remap_table *alloc_irq_table(u16 devid, struct pci_dev *pdev)
2741 struct irq_remap_table *table = NULL;
2742 struct irq_remap_table *new_table = NULL;
2743 struct amd_iommu *iommu;
2744 unsigned long flags;
2747 spin_lock_irqsave(&iommu_table_lock, flags);
2749 iommu = amd_iommu_rlookup_table[devid];
2753 table = irq_lookup_table[devid];
2757 alias = amd_iommu_alias_table[devid];
2758 table = irq_lookup_table[alias];
2760 set_remap_table_entry(iommu, devid, table);
2763 spin_unlock_irqrestore(&iommu_table_lock, flags);
2765 /* Nothing there yet, allocate new irq remapping table */
2766 new_table = __alloc_irq_table();
2770 spin_lock_irqsave(&iommu_table_lock, flags);
2772 table = irq_lookup_table[devid];
2776 table = irq_lookup_table[alias];
2778 set_remap_table_entry(iommu, devid, table);
2786 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
2789 set_remap_table_entry(iommu, devid, table);
2792 set_remap_table_entry(iommu, alias, table);
2795 iommu_completion_wait(iommu);
2798 spin_unlock_irqrestore(&iommu_table_lock, flags);
2801 kmem_cache_free(amd_iommu_irq_cache, new_table->table);
2807 static int alloc_irq_index(u16 devid, int count, bool align,
2808 struct pci_dev *pdev)
2810 struct irq_remap_table *table;
2811 int index, c, alignment = 1;
2812 unsigned long flags;
2813 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
2818 table = alloc_irq_table(devid, pdev);
2823 alignment = roundup_pow_of_two(count);
2825 raw_spin_lock_irqsave(&table->lock, flags);
2827 /* Scan table for free entries */
2828 for (index = ALIGN(table->min_index, alignment), c = 0;
2829 index < MAX_IRQS_PER_TABLE;) {
2830 if (!iommu->irte_ops->is_allocated(table, index)) {
2834 index = ALIGN(index + 1, alignment);
2840 iommu->irte_ops->set_allocated(table, index - c + 1);
2852 raw_spin_unlock_irqrestore(&table->lock, flags);
2857 static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
2858 struct amd_ir_data *data)
2861 struct irq_remap_table *table;
2862 struct amd_iommu *iommu;
2863 unsigned long flags;
2864 struct irte_ga *entry;
2866 iommu = amd_iommu_rlookup_table[devid];
2870 table = get_irq_table(devid);
2874 raw_spin_lock_irqsave(&table->lock, flags);
2876 entry = (struct irte_ga *)table->table;
2877 entry = &entry[index];
2879 ret = cmpxchg_double(&entry->lo.val, &entry->hi.val,
2880 entry->lo.val, entry->hi.val,
2881 irte->lo.val, irte->hi.val);
2883 * We use cmpxchg16 to atomically update the 128-bit IRTE,
2884 * and it cannot be updated by the hardware or other processors
2885 * behind us, so the return value of cmpxchg16 should be the
2886 * same as the old value.
2893 raw_spin_unlock_irqrestore(&table->lock, flags);
2895 iommu_flush_irt(iommu, devid);
2896 iommu_completion_wait(iommu);
2901 static int modify_irte(u16 devid, int index, union irte *irte)
2903 struct irq_remap_table *table;
2904 struct amd_iommu *iommu;
2905 unsigned long flags;
2907 iommu = amd_iommu_rlookup_table[devid];
2911 table = get_irq_table(devid);
2915 raw_spin_lock_irqsave(&table->lock, flags);
2916 table->table[index] = irte->val;
2917 raw_spin_unlock_irqrestore(&table->lock, flags);
2919 iommu_flush_irt(iommu, devid);
2920 iommu_completion_wait(iommu);
2925 static void free_irte(u16 devid, int index)
2927 struct irq_remap_table *table;
2928 struct amd_iommu *iommu;
2929 unsigned long flags;
2931 iommu = amd_iommu_rlookup_table[devid];
2935 table = get_irq_table(devid);
2939 raw_spin_lock_irqsave(&table->lock, flags);
2940 iommu->irte_ops->clear_allocated(table, index);
2941 raw_spin_unlock_irqrestore(&table->lock, flags);
2943 iommu_flush_irt(iommu, devid);
2944 iommu_completion_wait(iommu);
2947 static void irte_prepare(void *entry,
2948 u32 delivery_mode, bool dest_mode,
2949 u8 vector, u32 dest_apicid, int devid)
2951 union irte *irte = (union irte *) entry;
2954 irte->fields.vector = vector;
2955 irte->fields.int_type = delivery_mode;
2956 irte->fields.destination = dest_apicid;
2957 irte->fields.dm = dest_mode;
2958 irte->fields.valid = 1;
2961 static void irte_ga_prepare(void *entry,
2962 u32 delivery_mode, bool dest_mode,
2963 u8 vector, u32 dest_apicid, int devid)
2965 struct irte_ga *irte = (struct irte_ga *) entry;
2969 irte->lo.fields_remap.int_type = delivery_mode;
2970 irte->lo.fields_remap.dm = dest_mode;
2971 irte->hi.fields.vector = vector;
2972 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
2973 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
2974 irte->lo.fields_remap.valid = 1;
2977 static void irte_activate(void *entry, u16 devid, u16 index)
2979 union irte *irte = (union irte *) entry;
2981 irte->fields.valid = 1;
2982 modify_irte(devid, index, irte);
2985 static void irte_ga_activate(void *entry, u16 devid, u16 index)
2987 struct irte_ga *irte = (struct irte_ga *) entry;
2989 irte->lo.fields_remap.valid = 1;
2990 modify_irte_ga(devid, index, irte, NULL);
2993 static void irte_deactivate(void *entry, u16 devid, u16 index)
2995 union irte *irte = (union irte *) entry;
2997 irte->fields.valid = 0;
2998 modify_irte(devid, index, irte);
3001 static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
3003 struct irte_ga *irte = (struct irte_ga *) entry;
3005 irte->lo.fields_remap.valid = 0;
3006 modify_irte_ga(devid, index, irte, NULL);
3009 static void irte_set_affinity(void *entry, u16 devid, u16 index,
3010 u8 vector, u32 dest_apicid)
3012 union irte *irte = (union irte *) entry;
3014 irte->fields.vector = vector;
3015 irte->fields.destination = dest_apicid;
3016 modify_irte(devid, index, irte);
3019 static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
3020 u8 vector, u32 dest_apicid)
3022 struct irte_ga *irte = (struct irte_ga *) entry;
3024 if (!irte->lo.fields_remap.guest_mode) {
3025 irte->hi.fields.vector = vector;
3026 irte->lo.fields_remap.destination =
3027 APICID_TO_IRTE_DEST_LO(dest_apicid);
3028 irte->hi.fields.destination =
3029 APICID_TO_IRTE_DEST_HI(dest_apicid);
3030 modify_irte_ga(devid, index, irte, NULL);
3034 #define IRTE_ALLOCATED (~1U)
3035 static void irte_set_allocated(struct irq_remap_table *table, int index)
3037 table->table[index] = IRTE_ALLOCATED;
3040 static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3042 struct irte_ga *ptr = (struct irte_ga *)table->table;
3043 struct irte_ga *irte = &ptr[index];
3045 memset(&irte->lo.val, 0, sizeof(u64));
3046 memset(&irte->hi.val, 0, sizeof(u64));
3047 irte->hi.fields.vector = 0xff;
3050 static bool irte_is_allocated(struct irq_remap_table *table, int index)
3052 union irte *ptr = (union irte *)table->table;
3053 union irte *irte = &ptr[index];
3055 return irte->val != 0;
3058 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3060 struct irte_ga *ptr = (struct irte_ga *)table->table;
3061 struct irte_ga *irte = &ptr[index];
3063 return irte->hi.fields.vector != 0;
3066 static void irte_clear_allocated(struct irq_remap_table *table, int index)
3068 table->table[index] = 0;
3071 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3073 struct irte_ga *ptr = (struct irte_ga *)table->table;
3074 struct irte_ga *irte = &ptr[index];
3076 memset(&irte->lo.val, 0, sizeof(u64));
3077 memset(&irte->hi.val, 0, sizeof(u64));
3080 static int get_devid(struct irq_alloc_info *info)
3082 switch (info->type) {
3083 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3084 return get_ioapic_devid(info->devid);
3085 case X86_IRQ_ALLOC_TYPE_HPET:
3086 return get_hpet_devid(info->devid);
3087 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3088 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3089 return get_device_id(msi_desc_to_dev(info->desc));
3096 struct irq_remap_ops amd_iommu_irq_ops = {
3097 .prepare = amd_iommu_prepare,
3098 .enable = amd_iommu_enable,
3099 .disable = amd_iommu_disable,
3100 .reenable = amd_iommu_reenable,
3101 .enable_faulting = amd_iommu_enable_faulting,
3104 static void fill_msi_msg(struct msi_msg *msg, u32 index)
3107 msg->address_lo = 0;
3108 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
3109 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
3112 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3113 struct irq_cfg *irq_cfg,
3114 struct irq_alloc_info *info,
3115 int devid, int index, int sub_handle)
3117 struct irq_2_irte *irte_info = &data->irq_2_irte;
3118 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3123 data->irq_2_irte.devid = devid;
3124 data->irq_2_irte.index = index + sub_handle;
3125 iommu->irte_ops->prepare(data->entry, apic->delivery_mode,
3126 apic->dest_mode_logical, irq_cfg->vector,
3127 irq_cfg->dest_apicid, devid);
3129 switch (info->type) {
3130 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3131 case X86_IRQ_ALLOC_TYPE_HPET:
3132 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3133 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3134 fill_msi_msg(&data->msi_entry, irte_info->index);
3143 struct amd_irte_ops irte_32_ops = {
3144 .prepare = irte_prepare,
3145 .activate = irte_activate,
3146 .deactivate = irte_deactivate,
3147 .set_affinity = irte_set_affinity,
3148 .set_allocated = irte_set_allocated,
3149 .is_allocated = irte_is_allocated,
3150 .clear_allocated = irte_clear_allocated,
3153 struct amd_irte_ops irte_128_ops = {
3154 .prepare = irte_ga_prepare,
3155 .activate = irte_ga_activate,
3156 .deactivate = irte_ga_deactivate,
3157 .set_affinity = irte_ga_set_affinity,
3158 .set_allocated = irte_ga_set_allocated,
3159 .is_allocated = irte_ga_is_allocated,
3160 .clear_allocated = irte_ga_clear_allocated,
3163 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3164 unsigned int nr_irqs, void *arg)
3166 struct irq_alloc_info *info = arg;
3167 struct irq_data *irq_data;
3168 struct amd_ir_data *data = NULL;
3169 struct irq_cfg *cfg;
3175 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI &&
3176 info->type != X86_IRQ_ALLOC_TYPE_PCI_MSIX)
3180 * With IRQ remapping enabled, don't need contiguous CPU vectors
3181 * to support multiple MSI interrupts.
3183 if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
3184 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3186 devid = get_devid(info);
3190 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3194 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3195 struct irq_remap_table *table;
3196 struct amd_iommu *iommu;
3198 table = alloc_irq_table(devid, NULL);
3200 if (!table->min_index) {
3202 * Keep the first 32 indexes free for IOAPIC
3205 table->min_index = 32;
3206 iommu = amd_iommu_rlookup_table[devid];
3207 for (i = 0; i < 32; ++i)
3208 iommu->irte_ops->set_allocated(table, i);
3210 WARN_ON(table->min_index != 32);
3211 index = info->ioapic.pin;
3215 } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
3216 info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
3217 bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
3219 index = alloc_irq_index(devid, nr_irqs, align,
3220 msi_desc_to_pci_dev(info->desc));
3222 index = alloc_irq_index(devid, nr_irqs, false, NULL);
3226 pr_warn("Failed to allocate IRTE\n");
3228 goto out_free_parent;
3231 for (i = 0; i < nr_irqs; i++) {
3232 irq_data = irq_domain_get_irq_data(domain, virq + i);
3233 cfg = irq_data ? irqd_cfg(irq_data) : NULL;
3240 data = kzalloc(sizeof(*data), GFP_KERNEL);
3244 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3245 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
3247 data->entry = kzalloc(sizeof(struct irte_ga),
3254 irq_data->hwirq = (devid << 16) + i;
3255 irq_data->chip_data = data;
3256 irq_data->chip = &amd_ir_chip;
3257 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3258 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3264 for (i--; i >= 0; i--) {
3265 irq_data = irq_domain_get_irq_data(domain, virq + i);
3267 kfree(irq_data->chip_data);
3269 for (i = 0; i < nr_irqs; i++)
3270 free_irte(devid, index + i);
3272 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3276 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3277 unsigned int nr_irqs)
3279 struct irq_2_irte *irte_info;
3280 struct irq_data *irq_data;
3281 struct amd_ir_data *data;
3284 for (i = 0; i < nr_irqs; i++) {
3285 irq_data = irq_domain_get_irq_data(domain, virq + i);
3286 if (irq_data && irq_data->chip_data) {
3287 data = irq_data->chip_data;
3288 irte_info = &data->irq_2_irte;
3289 free_irte(irte_info->devid, irte_info->index);
3294 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3297 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3298 struct amd_ir_data *ir_data,
3299 struct irq_2_irte *irte_info,
3300 struct irq_cfg *cfg);
3302 static int irq_remapping_activate(struct irq_domain *domain,
3303 struct irq_data *irq_data, bool reserve)
3305 struct amd_ir_data *data = irq_data->chip_data;
3306 struct irq_2_irte *irte_info = &data->irq_2_irte;
3307 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3308 struct irq_cfg *cfg = irqd_cfg(irq_data);
3313 iommu->irte_ops->activate(data->entry, irte_info->devid,
3315 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
3319 static void irq_remapping_deactivate(struct irq_domain *domain,
3320 struct irq_data *irq_data)
3322 struct amd_ir_data *data = irq_data->chip_data;
3323 struct irq_2_irte *irte_info = &data->irq_2_irte;
3324 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3327 iommu->irte_ops->deactivate(data->entry, irte_info->devid,
3331 static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec,
3332 enum irq_domain_bus_token bus_token)
3334 struct amd_iommu *iommu;
3337 if (!amd_iommu_irq_remap)
3340 if (x86_fwspec_is_ioapic(fwspec))
3341 devid = get_ioapic_devid(fwspec->param[0]);
3342 else if (x86_fwspec_is_hpet(fwspec))
3343 devid = get_hpet_devid(fwspec->param[0]);
3348 iommu = amd_iommu_rlookup_table[devid];
3349 return iommu && iommu->ir_domain == d;
3352 static const struct irq_domain_ops amd_ir_domain_ops = {
3353 .select = irq_remapping_select,
3354 .alloc = irq_remapping_alloc,
3355 .free = irq_remapping_free,
3356 .activate = irq_remapping_activate,
3357 .deactivate = irq_remapping_deactivate,
3360 int amd_iommu_activate_guest_mode(void *data)
3362 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3363 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3366 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3367 !entry || entry->lo.fields_vapic.guest_mode)
3370 valid = entry->lo.fields_vapic.valid;
3375 entry->lo.fields_vapic.valid = valid;
3376 entry->lo.fields_vapic.guest_mode = 1;
3377 entry->lo.fields_vapic.ga_log_intr = 1;
3378 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
3379 entry->hi.fields.vector = ir_data->ga_vector;
3380 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
3382 return modify_irte_ga(ir_data->irq_2_irte.devid,
3383 ir_data->irq_2_irte.index, entry, ir_data);
3385 EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
3387 int amd_iommu_deactivate_guest_mode(void *data)
3389 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3390 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3391 struct irq_cfg *cfg = ir_data->cfg;
3394 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3395 !entry || !entry->lo.fields_vapic.guest_mode)
3398 valid = entry->lo.fields_remap.valid;
3403 entry->lo.fields_remap.valid = valid;
3404 entry->lo.fields_remap.dm = apic->dest_mode_logical;
3405 entry->lo.fields_remap.int_type = apic->delivery_mode;
3406 entry->hi.fields.vector = cfg->vector;
3407 entry->lo.fields_remap.destination =
3408 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
3409 entry->hi.fields.destination =
3410 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
3412 return modify_irte_ga(ir_data->irq_2_irte.devid,
3413 ir_data->irq_2_irte.index, entry, ir_data);
3415 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
3417 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
3420 struct amd_iommu *iommu;
3421 struct amd_iommu_pi_data *pi_data = vcpu_info;
3422 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
3423 struct amd_ir_data *ir_data = data->chip_data;
3424 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3425 struct iommu_dev_data *dev_data = search_dev_data(irte_info->devid);
3428 * This device has never been set up for guest mode.
3429 * we should not modify the IRTE
3431 if (!dev_data || !dev_data->use_vapic)
3434 ir_data->cfg = irqd_cfg(data);
3435 pi_data->ir_data = ir_data;
3438 * SVM tries to set up for VAPIC mode, but we are in
3439 * legacy mode. So, we force legacy mode instead.
3441 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3442 pr_debug("%s: Fall back to using intr legacy remap\n",
3444 pi_data->is_guest_mode = false;
3447 iommu = amd_iommu_rlookup_table[irte_info->devid];
3451 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
3452 if (pi_data->is_guest_mode) {
3453 ir_data->ga_root_ptr = (pi_data->base >> 12);
3454 ir_data->ga_vector = vcpu_pi_info->vector;
3455 ir_data->ga_tag = pi_data->ga_tag;
3456 ret = amd_iommu_activate_guest_mode(ir_data);
3458 ir_data->cached_ga_tag = pi_data->ga_tag;
3460 ret = amd_iommu_deactivate_guest_mode(ir_data);
3463 * This communicates the ga_tag back to the caller
3464 * so that it can do all the necessary clean up.
3467 ir_data->cached_ga_tag = 0;
3474 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3475 struct amd_ir_data *ir_data,
3476 struct irq_2_irte *irte_info,
3477 struct irq_cfg *cfg)
3481 * Atomically updates the IRTE with the new destination, vector
3482 * and flushes the interrupt entry cache.
3484 iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
3485 irte_info->index, cfg->vector,
3489 static int amd_ir_set_affinity(struct irq_data *data,
3490 const struct cpumask *mask, bool force)
3492 struct amd_ir_data *ir_data = data->chip_data;
3493 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3494 struct irq_cfg *cfg = irqd_cfg(data);
3495 struct irq_data *parent = data->parent_data;
3496 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3502 ret = parent->chip->irq_set_affinity(parent, mask, force);
3503 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3506 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
3508 * After this point, all the interrupts will start arriving
3509 * at the new destination. So, time to cleanup the previous
3510 * vector allocation.
3512 send_cleanup_vector(cfg);
3514 return IRQ_SET_MASK_OK_DONE;
3517 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3519 struct amd_ir_data *ir_data = irq_data->chip_data;
3521 *msg = ir_data->msi_entry;
3524 static struct irq_chip amd_ir_chip = {
3526 .irq_ack = apic_ack_irq,
3527 .irq_set_affinity = amd_ir_set_affinity,
3528 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
3529 .irq_compose_msi_msg = ir_compose_msi_msg,
3532 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3534 struct fwnode_handle *fn;
3536 fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
3539 iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
3540 if (!iommu->ir_domain) {
3541 irq_domain_free_fwnode(fn);
3545 iommu->ir_domain->parent = arch_get_ir_parent_domain();
3546 iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
3552 int amd_iommu_update_ga(int cpu, bool is_run, void *data)
3554 unsigned long flags;
3555 struct amd_iommu *iommu;
3556 struct irq_remap_table *table;
3557 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3558 int devid = ir_data->irq_2_irte.devid;
3559 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3560 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
3562 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3563 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
3566 iommu = amd_iommu_rlookup_table[devid];
3570 table = get_irq_table(devid);
3574 raw_spin_lock_irqsave(&table->lock, flags);
3576 if (ref->lo.fields_vapic.guest_mode) {
3578 ref->lo.fields_vapic.destination =
3579 APICID_TO_IRTE_DEST_LO(cpu);
3580 ref->hi.fields.destination =
3581 APICID_TO_IRTE_DEST_HI(cpu);
3583 ref->lo.fields_vapic.is_run = is_run;
3587 raw_spin_unlock_irqrestore(&table->lock, flags);
3589 iommu_flush_irt(iommu, devid);
3590 iommu_completion_wait(iommu);
3593 EXPORT_SYMBOL(amd_iommu_update_ga);