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/io-pgtable.h>
34 #include <linux/cc_platform.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]);
294 * This function checks if the driver got a valid device from the caller to
295 * avoid dereferencing invalid pointers.
297 static bool check_device(struct device *dev)
304 devid = get_device_id(dev);
308 /* Out of our scope? */
309 if (devid > amd_iommu_last_bdf)
312 if (amd_iommu_rlookup_table[devid] == NULL)
318 static int iommu_init_device(struct device *dev)
320 struct iommu_dev_data *dev_data;
323 if (dev_iommu_priv_get(dev))
326 devid = get_device_id(dev);
330 dev_data = find_dev_data(devid);
334 dev_data->pdev = setup_aliases(dev);
337 * By default we use passthrough mode for IOMMUv2 capable device.
338 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
339 * invalid address), we ignore the capability for the device so
340 * it'll be forced to go into translation mode.
342 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
343 dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
344 struct amd_iommu *iommu;
346 iommu = amd_iommu_rlookup_table[dev_data->devid];
347 dev_data->iommu_v2 = iommu->is_iommu_v2;
350 dev_iommu_priv_set(dev, dev_data);
355 static void iommu_ignore_device(struct device *dev)
359 devid = get_device_id(dev);
363 amd_iommu_rlookup_table[devid] = NULL;
364 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
369 static void amd_iommu_uninit_device(struct device *dev)
371 struct iommu_dev_data *dev_data;
373 dev_data = dev_iommu_priv_get(dev);
377 if (dev_data->domain)
380 dev_iommu_priv_set(dev, NULL);
383 * We keep dev_data around for unplugged devices and reuse it when the
384 * device is re-plugged - not doing so would introduce a ton of races.
388 /****************************************************************************
390 * Interrupt handling functions
392 ****************************************************************************/
394 static void dump_dte_entry(u16 devid)
398 for (i = 0; i < 4; ++i)
399 pr_err("DTE[%d]: %016llx\n", i,
400 amd_iommu_dev_table[devid].data[i]);
403 static void dump_command(unsigned long phys_addr)
405 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
408 for (i = 0; i < 4; ++i)
409 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
412 static void amd_iommu_report_rmp_hw_error(volatile u32 *event)
414 struct iommu_dev_data *dev_data = NULL;
415 int devid, vmg_tag, flags;
416 struct pci_dev *pdev;
419 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
420 vmg_tag = (event[1]) & 0xFFFF;
421 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
422 spa = ((u64)event[3] << 32) | (event[2] & 0xFFFFFFF8);
424 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
427 dev_data = dev_iommu_priv_get(&pdev->dev);
430 if (__ratelimit(&dev_data->rs)) {
431 pci_err(pdev, "Event logged [RMP_HW_ERROR vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
432 vmg_tag, spa, flags);
435 pr_err_ratelimited("Event logged [RMP_HW_ERROR device=%02x:%02x.%x, vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
436 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
437 vmg_tag, spa, flags);
444 static void amd_iommu_report_rmp_fault(volatile u32 *event)
446 struct iommu_dev_data *dev_data = NULL;
447 int devid, flags_rmp, vmg_tag, flags;
448 struct pci_dev *pdev;
451 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
452 flags_rmp = (event[0] >> EVENT_FLAGS_SHIFT) & 0xFF;
453 vmg_tag = (event[1]) & 0xFFFF;
454 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
455 gpa = ((u64)event[3] << 32) | event[2];
457 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
460 dev_data = dev_iommu_priv_get(&pdev->dev);
463 if (__ratelimit(&dev_data->rs)) {
464 pci_err(pdev, "Event logged [RMP_PAGE_FAULT vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
465 vmg_tag, gpa, flags_rmp, flags);
468 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",
469 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
470 vmg_tag, gpa, flags_rmp, flags);
477 static void amd_iommu_report_page_fault(u16 devid, u16 domain_id,
478 u64 address, int flags)
480 struct iommu_dev_data *dev_data = NULL;
481 struct pci_dev *pdev;
483 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
486 dev_data = dev_iommu_priv_get(&pdev->dev);
489 if (__ratelimit(&dev_data->rs)) {
490 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
491 domain_id, address, flags);
494 pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
495 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
496 domain_id, address, flags);
503 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
505 struct device *dev = iommu->iommu.dev;
506 int type, devid, flags, tag;
507 volatile u32 *event = __evt;
513 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
514 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
515 pasid = (event[0] & EVENT_DOMID_MASK_HI) |
516 (event[1] & EVENT_DOMID_MASK_LO);
517 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
518 address = (u64)(((u64)event[3]) << 32) | event[2];
521 /* Did we hit the erratum? */
522 if (++count == LOOP_TIMEOUT) {
523 pr_err("No event written to event log\n");
530 if (type == EVENT_TYPE_IO_FAULT) {
531 amd_iommu_report_page_fault(devid, pasid, address, flags);
536 case EVENT_TYPE_ILL_DEV:
537 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
538 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
539 pasid, address, flags);
540 dump_dte_entry(devid);
542 case EVENT_TYPE_DEV_TAB_ERR:
543 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
544 "address=0x%llx flags=0x%04x]\n",
545 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
548 case EVENT_TYPE_PAGE_TAB_ERR:
549 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
550 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
551 pasid, address, flags);
553 case EVENT_TYPE_ILL_CMD:
554 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
555 dump_command(address);
557 case EVENT_TYPE_CMD_HARD_ERR:
558 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
561 case EVENT_TYPE_IOTLB_INV_TO:
562 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%02x:%02x.%x address=0x%llx]\n",
563 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
566 case EVENT_TYPE_INV_DEV_REQ:
567 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
568 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
569 pasid, address, flags);
571 case EVENT_TYPE_RMP_FAULT:
572 amd_iommu_report_rmp_fault(event);
574 case EVENT_TYPE_RMP_HW_ERR:
575 amd_iommu_report_rmp_hw_error(event);
577 case EVENT_TYPE_INV_PPR_REQ:
578 pasid = PPR_PASID(*((u64 *)__evt));
579 tag = event[1] & 0x03FF;
580 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",
581 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
582 pasid, address, flags, tag);
585 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
586 event[0], event[1], event[2], event[3]);
589 memset(__evt, 0, 4 * sizeof(u32));
592 static void iommu_poll_events(struct amd_iommu *iommu)
596 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
597 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
599 while (head != tail) {
600 iommu_print_event(iommu, iommu->evt_buf + head);
601 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
604 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
607 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
609 struct amd_iommu_fault fault;
611 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
612 pr_err_ratelimited("Unknown PPR request received\n");
616 fault.address = raw[1];
617 fault.pasid = PPR_PASID(raw[0]);
618 fault.device_id = PPR_DEVID(raw[0]);
619 fault.tag = PPR_TAG(raw[0]);
620 fault.flags = PPR_FLAGS(raw[0]);
622 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
625 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
629 if (iommu->ppr_log == NULL)
632 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
633 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
635 while (head != tail) {
640 raw = (u64 *)(iommu->ppr_log + head);
643 * Hardware bug: Interrupt may arrive before the entry is
644 * written to memory. If this happens we need to wait for the
647 for (i = 0; i < LOOP_TIMEOUT; ++i) {
648 if (PPR_REQ_TYPE(raw[0]) != 0)
653 /* Avoid memcpy function-call overhead */
658 * To detect the hardware bug we need to clear the entry
661 raw[0] = raw[1] = 0UL;
663 /* Update head pointer of hardware ring-buffer */
664 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
665 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
667 /* Handle PPR entry */
668 iommu_handle_ppr_entry(iommu, entry);
670 /* Refresh ring-buffer information */
671 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
672 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
676 #ifdef CONFIG_IRQ_REMAP
677 static int (*iommu_ga_log_notifier)(u32);
679 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
681 iommu_ga_log_notifier = notifier;
685 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
687 static void iommu_poll_ga_log(struct amd_iommu *iommu)
689 u32 head, tail, cnt = 0;
691 if (iommu->ga_log == NULL)
694 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
695 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
697 while (head != tail) {
701 raw = (u64 *)(iommu->ga_log + head);
704 /* Avoid memcpy function-call overhead */
707 /* Update head pointer of hardware ring-buffer */
708 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
709 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
711 /* Handle GA entry */
712 switch (GA_REQ_TYPE(log_entry)) {
714 if (!iommu_ga_log_notifier)
717 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
718 __func__, GA_DEVID(log_entry),
721 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
722 pr_err("GA log notifier failed.\n");
731 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
733 if (!irq_remapping_enabled || !dev_is_pci(dev) ||
734 pci_dev_has_special_msi_domain(to_pci_dev(dev)))
737 dev_set_msi_domain(dev, iommu->msi_domain);
740 #else /* CONFIG_IRQ_REMAP */
742 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
743 #endif /* !CONFIG_IRQ_REMAP */
745 #define AMD_IOMMU_INT_MASK \
746 (MMIO_STATUS_EVT_INT_MASK | \
747 MMIO_STATUS_PPR_INT_MASK | \
748 MMIO_STATUS_GALOG_INT_MASK)
750 irqreturn_t amd_iommu_int_thread(int irq, void *data)
752 struct amd_iommu *iommu = (struct amd_iommu *) data;
753 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
755 while (status & AMD_IOMMU_INT_MASK) {
756 /* Enable EVT and PPR and GA interrupts again */
757 writel(AMD_IOMMU_INT_MASK,
758 iommu->mmio_base + MMIO_STATUS_OFFSET);
760 if (status & MMIO_STATUS_EVT_INT_MASK) {
761 pr_devel("Processing IOMMU Event Log\n");
762 iommu_poll_events(iommu);
765 if (status & MMIO_STATUS_PPR_INT_MASK) {
766 pr_devel("Processing IOMMU PPR Log\n");
767 iommu_poll_ppr_log(iommu);
770 #ifdef CONFIG_IRQ_REMAP
771 if (status & MMIO_STATUS_GALOG_INT_MASK) {
772 pr_devel("Processing IOMMU GA Log\n");
773 iommu_poll_ga_log(iommu);
778 * Hardware bug: ERBT1312
779 * When re-enabling interrupt (by writing 1
780 * to clear the bit), the hardware might also try to set
781 * the interrupt bit in the event status register.
782 * In this scenario, the bit will be set, and disable
783 * subsequent interrupts.
785 * Workaround: The IOMMU driver should read back the
786 * status register and check if the interrupt bits are cleared.
787 * If not, driver will need to go through the interrupt handler
788 * again and re-clear the bits
790 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
795 irqreturn_t amd_iommu_int_handler(int irq, void *data)
797 return IRQ_WAKE_THREAD;
800 /****************************************************************************
802 * IOMMU command queuing functions
804 ****************************************************************************/
806 static int wait_on_sem(struct amd_iommu *iommu, u64 data)
810 while (*iommu->cmd_sem != data && i < LOOP_TIMEOUT) {
815 if (i == LOOP_TIMEOUT) {
816 pr_alert("Completion-Wait loop timed out\n");
823 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
824 struct iommu_cmd *cmd)
829 /* Copy command to buffer */
830 tail = iommu->cmd_buf_tail;
831 target = iommu->cmd_buf + tail;
832 memcpy(target, cmd, sizeof(*cmd));
834 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
835 iommu->cmd_buf_tail = tail;
837 /* Tell the IOMMU about it */
838 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
841 static void build_completion_wait(struct iommu_cmd *cmd,
842 struct amd_iommu *iommu,
845 u64 paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
847 memset(cmd, 0, sizeof(*cmd));
848 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
849 cmd->data[1] = upper_32_bits(paddr);
851 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
854 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
856 memset(cmd, 0, sizeof(*cmd));
857 cmd->data[0] = devid;
858 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
862 * Builds an invalidation address which is suitable for one page or multiple
863 * pages. Sets the size bit (S) as needed is more than one page is flushed.
865 static inline u64 build_inv_address(u64 address, size_t size)
867 u64 pages, end, msb_diff;
869 pages = iommu_num_pages(address, size, PAGE_SIZE);
872 return address & PAGE_MASK;
874 end = address + size - 1;
877 * msb_diff would hold the index of the most significant bit that
878 * flipped between the start and end.
880 msb_diff = fls64(end ^ address) - 1;
883 * Bits 63:52 are sign extended. If for some reason bit 51 is different
884 * between the start and the end, invalidate everything.
886 if (unlikely(msb_diff > 51)) {
887 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
890 * The msb-bit must be clear on the address. Just set all the
893 address |= (1ull << msb_diff) - 1;
896 /* Clear bits 11:0 */
897 address &= PAGE_MASK;
899 /* Set the size bit - we flush more than one 4kb page */
900 return address | CMD_INV_IOMMU_PAGES_SIZE_MASK;
903 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
904 size_t size, u16 domid, int pde)
906 u64 inv_address = build_inv_address(address, size);
908 memset(cmd, 0, sizeof(*cmd));
909 cmd->data[1] |= domid;
910 cmd->data[2] = lower_32_bits(inv_address);
911 cmd->data[3] = upper_32_bits(inv_address);
912 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
913 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
914 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
917 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
918 u64 address, size_t size)
920 u64 inv_address = build_inv_address(address, size);
922 memset(cmd, 0, sizeof(*cmd));
923 cmd->data[0] = devid;
924 cmd->data[0] |= (qdep & 0xff) << 24;
925 cmd->data[1] = devid;
926 cmd->data[2] = lower_32_bits(inv_address);
927 cmd->data[3] = upper_32_bits(inv_address);
928 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
931 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
932 u64 address, bool size)
934 memset(cmd, 0, sizeof(*cmd));
936 address &= ~(0xfffULL);
938 cmd->data[0] = pasid;
939 cmd->data[1] = domid;
940 cmd->data[2] = lower_32_bits(address);
941 cmd->data[3] = upper_32_bits(address);
942 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
943 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
945 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
946 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
949 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid,
950 int qdep, u64 address, bool size)
952 memset(cmd, 0, sizeof(*cmd));
954 address &= ~(0xfffULL);
956 cmd->data[0] = devid;
957 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
958 cmd->data[0] |= (qdep & 0xff) << 24;
959 cmd->data[1] = devid;
960 cmd->data[1] |= (pasid & 0xff) << 16;
961 cmd->data[2] = lower_32_bits(address);
962 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
963 cmd->data[3] = upper_32_bits(address);
965 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
966 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
969 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
970 int status, int tag, bool gn)
972 memset(cmd, 0, sizeof(*cmd));
974 cmd->data[0] = devid;
976 cmd->data[1] = pasid;
977 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
979 cmd->data[3] = tag & 0x1ff;
980 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
982 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
985 static void build_inv_all(struct iommu_cmd *cmd)
987 memset(cmd, 0, sizeof(*cmd));
988 CMD_SET_TYPE(cmd, CMD_INV_ALL);
991 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
993 memset(cmd, 0, sizeof(*cmd));
994 cmd->data[0] = devid;
995 CMD_SET_TYPE(cmd, CMD_INV_IRT);
999 * Writes the command to the IOMMUs command buffer and informs the
1000 * hardware about the new command.
1002 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
1003 struct iommu_cmd *cmd,
1006 unsigned int count = 0;
1007 u32 left, next_tail;
1009 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1011 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
1014 /* Skip udelay() the first time around */
1016 if (count == LOOP_TIMEOUT) {
1017 pr_err("Command buffer timeout\n");
1024 /* Update head and recheck remaining space */
1025 iommu->cmd_buf_head = readl(iommu->mmio_base +
1026 MMIO_CMD_HEAD_OFFSET);
1031 copy_cmd_to_buffer(iommu, cmd);
1033 /* Do we need to make sure all commands are processed? */
1034 iommu->need_sync = sync;
1039 static int iommu_queue_command_sync(struct amd_iommu *iommu,
1040 struct iommu_cmd *cmd,
1043 unsigned long flags;
1046 raw_spin_lock_irqsave(&iommu->lock, flags);
1047 ret = __iommu_queue_command_sync(iommu, cmd, sync);
1048 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1053 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1055 return iommu_queue_command_sync(iommu, cmd, true);
1059 * This function queues a completion wait command into the command
1060 * buffer of an IOMMU
1062 static int iommu_completion_wait(struct amd_iommu *iommu)
1064 struct iommu_cmd cmd;
1065 unsigned long flags;
1069 if (!iommu->need_sync)
1072 raw_spin_lock_irqsave(&iommu->lock, flags);
1074 data = ++iommu->cmd_sem_val;
1075 build_completion_wait(&cmd, iommu, data);
1077 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1081 ret = wait_on_sem(iommu, data);
1084 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1089 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1091 struct iommu_cmd cmd;
1093 build_inv_dte(&cmd, devid);
1095 return iommu_queue_command(iommu, &cmd);
1098 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
1102 for (devid = 0; devid <= 0xffff; ++devid)
1103 iommu_flush_dte(iommu, devid);
1105 iommu_completion_wait(iommu);
1109 * This function uses heavy locking and may disable irqs for some time. But
1110 * this is no issue because it is only called during resume.
1112 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
1116 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1117 struct iommu_cmd cmd;
1118 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1120 iommu_queue_command(iommu, &cmd);
1123 iommu_completion_wait(iommu);
1126 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1128 struct iommu_cmd cmd;
1130 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1132 iommu_queue_command(iommu, &cmd);
1134 iommu_completion_wait(iommu);
1137 static void amd_iommu_flush_all(struct amd_iommu *iommu)
1139 struct iommu_cmd cmd;
1141 build_inv_all(&cmd);
1143 iommu_queue_command(iommu, &cmd);
1144 iommu_completion_wait(iommu);
1147 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1149 struct iommu_cmd cmd;
1151 build_inv_irt(&cmd, devid);
1153 iommu_queue_command(iommu, &cmd);
1156 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
1160 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1161 iommu_flush_irt(iommu, devid);
1163 iommu_completion_wait(iommu);
1166 void iommu_flush_all_caches(struct amd_iommu *iommu)
1168 if (iommu_feature(iommu, FEATURE_IA)) {
1169 amd_iommu_flush_all(iommu);
1171 amd_iommu_flush_dte_all(iommu);
1172 amd_iommu_flush_irt_all(iommu);
1173 amd_iommu_flush_tlb_all(iommu);
1178 * Command send function for flushing on-device TLB
1180 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1181 u64 address, size_t size)
1183 struct amd_iommu *iommu;
1184 struct iommu_cmd cmd;
1187 qdep = dev_data->ats.qdep;
1188 iommu = amd_iommu_rlookup_table[dev_data->devid];
1190 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1192 return iommu_queue_command(iommu, &cmd);
1195 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
1197 struct amd_iommu *iommu = data;
1199 return iommu_flush_dte(iommu, alias);
1203 * Command send function for invalidating a device table entry
1205 static int device_flush_dte(struct iommu_dev_data *dev_data)
1207 struct amd_iommu *iommu;
1211 iommu = amd_iommu_rlookup_table[dev_data->devid];
1214 ret = pci_for_each_dma_alias(dev_data->pdev,
1215 device_flush_dte_alias, iommu);
1217 ret = iommu_flush_dte(iommu, dev_data->devid);
1221 alias = amd_iommu_alias_table[dev_data->devid];
1222 if (alias != dev_data->devid) {
1223 ret = iommu_flush_dte(iommu, alias);
1228 if (dev_data->ats.enabled)
1229 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1235 * TLB invalidation function which is called from the mapping functions.
1236 * It invalidates a single PTE if the range to flush is within a single
1237 * page. Otherwise it flushes the whole TLB of the IOMMU.
1239 static void __domain_flush_pages(struct protection_domain *domain,
1240 u64 address, size_t size, int pde)
1242 struct iommu_dev_data *dev_data;
1243 struct iommu_cmd cmd;
1246 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1248 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1249 if (!domain->dev_iommu[i])
1253 * Devices of this domain are behind this IOMMU
1254 * We need a TLB flush
1256 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1259 list_for_each_entry(dev_data, &domain->dev_list, list) {
1261 if (!dev_data->ats.enabled)
1264 ret |= device_flush_iotlb(dev_data, address, size);
1270 static void domain_flush_pages(struct protection_domain *domain,
1271 u64 address, size_t size, int pde)
1273 if (likely(!amd_iommu_np_cache)) {
1274 __domain_flush_pages(domain, address, size, pde);
1279 * When NpCache is on, we infer that we run in a VM and use a vIOMMU.
1280 * In such setups it is best to avoid flushes of ranges which are not
1281 * naturally aligned, since it would lead to flushes of unmodified
1282 * PTEs. Such flushes would require the hypervisor to do more work than
1283 * necessary. Therefore, perform repeated flushes of aligned ranges
1284 * until you cover the range. Each iteration flushes the smaller
1285 * between the natural alignment of the address that we flush and the
1286 * greatest naturally aligned region that fits in the range.
1289 int addr_alignment = __ffs(address);
1290 int size_alignment = __fls(size);
1295 * size is always non-zero, but address might be zero, causing
1296 * addr_alignment to be negative. As the casting of the
1297 * argument in __ffs(address) to long might trim the high bits
1298 * of the address on x86-32, cast to long when doing the check.
1300 if (likely((unsigned long)address != 0))
1301 min_alignment = min(addr_alignment, size_alignment);
1303 min_alignment = size_alignment;
1305 flush_size = 1ul << min_alignment;
1307 __domain_flush_pages(domain, address, flush_size, pde);
1308 address += flush_size;
1313 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1314 void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain)
1316 domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1319 void amd_iommu_domain_flush_complete(struct protection_domain *domain)
1323 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1324 if (domain && !domain->dev_iommu[i])
1328 * Devices of this domain are behind this IOMMU
1329 * We need to wait for completion of all commands.
1331 iommu_completion_wait(amd_iommus[i]);
1335 /* Flush the not present cache if it exists */
1336 static void domain_flush_np_cache(struct protection_domain *domain,
1337 dma_addr_t iova, size_t size)
1339 if (unlikely(amd_iommu_np_cache)) {
1340 unsigned long flags;
1342 spin_lock_irqsave(&domain->lock, flags);
1343 domain_flush_pages(domain, iova, size, 1);
1344 amd_iommu_domain_flush_complete(domain);
1345 spin_unlock_irqrestore(&domain->lock, flags);
1351 * This function flushes the DTEs for all devices in domain
1353 static void domain_flush_devices(struct protection_domain *domain)
1355 struct iommu_dev_data *dev_data;
1357 list_for_each_entry(dev_data, &domain->dev_list, list)
1358 device_flush_dte(dev_data);
1361 /****************************************************************************
1363 * The next functions belong to the domain allocation. A domain is
1364 * allocated for every IOMMU as the default domain. If device isolation
1365 * is enabled, every device get its own domain. The most important thing
1366 * about domains is the page table mapping the DMA address space they
1369 ****************************************************************************/
1371 static u16 domain_id_alloc(void)
1375 spin_lock(&pd_bitmap_lock);
1376 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1378 if (id > 0 && id < MAX_DOMAIN_ID)
1379 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1382 spin_unlock(&pd_bitmap_lock);
1387 static void domain_id_free(int id)
1389 spin_lock(&pd_bitmap_lock);
1390 if (id > 0 && id < MAX_DOMAIN_ID)
1391 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1392 spin_unlock(&pd_bitmap_lock);
1395 static void free_gcr3_tbl_level1(u64 *tbl)
1400 for (i = 0; i < 512; ++i) {
1401 if (!(tbl[i] & GCR3_VALID))
1404 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1406 free_page((unsigned long)ptr);
1410 static void free_gcr3_tbl_level2(u64 *tbl)
1415 for (i = 0; i < 512; ++i) {
1416 if (!(tbl[i] & GCR3_VALID))
1419 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1421 free_gcr3_tbl_level1(ptr);
1425 static void free_gcr3_table(struct protection_domain *domain)
1427 if (domain->glx == 2)
1428 free_gcr3_tbl_level2(domain->gcr3_tbl);
1429 else if (domain->glx == 1)
1430 free_gcr3_tbl_level1(domain->gcr3_tbl);
1432 BUG_ON(domain->glx != 0);
1434 free_page((unsigned long)domain->gcr3_tbl);
1437 static void set_dte_entry(u16 devid, struct protection_domain *domain,
1444 if (domain->iop.mode != PAGE_MODE_NONE)
1445 pte_root = iommu_virt_to_phys(domain->iop.root);
1447 pte_root |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
1448 << DEV_ENTRY_MODE_SHIFT;
1449 pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV;
1451 flags = amd_iommu_dev_table[devid].data[1];
1454 flags |= DTE_FLAG_IOTLB;
1457 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1459 if (iommu_feature(iommu, FEATURE_EPHSUP))
1460 pte_root |= 1ULL << DEV_ENTRY_PPR;
1463 if (domain->flags & PD_IOMMUV2_MASK) {
1464 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
1465 u64 glx = domain->glx;
1468 pte_root |= DTE_FLAG_GV;
1469 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1471 /* First mask out possible old values for GCR3 table */
1472 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1475 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1478 /* Encode GCR3 table into DTE */
1479 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1482 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1485 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1489 flags &= ~DEV_DOMID_MASK;
1490 flags |= domain->id;
1492 old_domid = amd_iommu_dev_table[devid].data[1] & DEV_DOMID_MASK;
1493 amd_iommu_dev_table[devid].data[1] = flags;
1494 amd_iommu_dev_table[devid].data[0] = pte_root;
1497 * A kdump kernel might be replacing a domain ID that was copied from
1498 * the previous kernel--if so, it needs to flush the translation cache
1499 * entries for the old domain ID that is being overwritten
1502 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1504 amd_iommu_flush_tlb_domid(iommu, old_domid);
1508 static void clear_dte_entry(u16 devid)
1510 /* remove entry from the device table seen by the hardware */
1511 amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV;
1512 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
1514 amd_iommu_apply_erratum_63(devid);
1517 static void do_attach(struct iommu_dev_data *dev_data,
1518 struct protection_domain *domain)
1520 struct amd_iommu *iommu;
1523 iommu = amd_iommu_rlookup_table[dev_data->devid];
1524 ats = dev_data->ats.enabled;
1526 /* Update data structures */
1527 dev_data->domain = domain;
1528 list_add(&dev_data->list, &domain->dev_list);
1530 /* Do reference counting */
1531 domain->dev_iommu[iommu->index] += 1;
1532 domain->dev_cnt += 1;
1534 /* Update device table */
1535 set_dte_entry(dev_data->devid, domain,
1536 ats, dev_data->iommu_v2);
1537 clone_aliases(dev_data->pdev);
1539 device_flush_dte(dev_data);
1542 static void do_detach(struct iommu_dev_data *dev_data)
1544 struct protection_domain *domain = dev_data->domain;
1545 struct amd_iommu *iommu;
1547 iommu = amd_iommu_rlookup_table[dev_data->devid];
1549 /* Update data structures */
1550 dev_data->domain = NULL;
1551 list_del(&dev_data->list);
1552 clear_dte_entry(dev_data->devid);
1553 clone_aliases(dev_data->pdev);
1555 /* Flush the DTE entry */
1556 device_flush_dte(dev_data);
1559 amd_iommu_domain_flush_tlb_pde(domain);
1561 /* Wait for the flushes to finish */
1562 amd_iommu_domain_flush_complete(domain);
1564 /* decrease reference counters - needs to happen after the flushes */
1565 domain->dev_iommu[iommu->index] -= 1;
1566 domain->dev_cnt -= 1;
1569 static void pdev_iommuv2_disable(struct pci_dev *pdev)
1571 pci_disable_ats(pdev);
1572 pci_disable_pri(pdev);
1573 pci_disable_pasid(pdev);
1576 static int pdev_iommuv2_enable(struct pci_dev *pdev)
1580 /* Only allow access to user-accessible pages */
1581 ret = pci_enable_pasid(pdev, 0);
1585 /* First reset the PRI state of the device */
1586 ret = pci_reset_pri(pdev);
1591 /* FIXME: Hardcode number of outstanding requests for now */
1592 ret = pci_enable_pri(pdev, 32);
1596 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1603 pci_disable_pri(pdev);
1604 pci_disable_pasid(pdev);
1610 * If a device is not yet associated with a domain, this function makes the
1611 * device visible in the domain
1613 static int attach_device(struct device *dev,
1614 struct protection_domain *domain)
1616 struct iommu_dev_data *dev_data;
1617 struct pci_dev *pdev;
1618 unsigned long flags;
1621 spin_lock_irqsave(&domain->lock, flags);
1623 dev_data = dev_iommu_priv_get(dev);
1625 spin_lock(&dev_data->lock);
1628 if (dev_data->domain != NULL)
1631 if (!dev_is_pci(dev))
1632 goto skip_ats_check;
1634 pdev = to_pci_dev(dev);
1635 if (domain->flags & PD_IOMMUV2_MASK) {
1636 struct iommu_domain *def_domain = iommu_get_dma_domain(dev);
1639 if (def_domain->type != IOMMU_DOMAIN_IDENTITY)
1642 if (dev_data->iommu_v2) {
1643 if (pdev_iommuv2_enable(pdev) != 0)
1646 dev_data->ats.enabled = true;
1647 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1648 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
1650 } else if (amd_iommu_iotlb_sup &&
1651 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1652 dev_data->ats.enabled = true;
1653 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1659 do_attach(dev_data, domain);
1662 * We might boot into a crash-kernel here. The crashed kernel
1663 * left the caches in the IOMMU dirty. So we have to flush
1664 * here to evict all dirty stuff.
1666 amd_iommu_domain_flush_tlb_pde(domain);
1668 amd_iommu_domain_flush_complete(domain);
1671 spin_unlock(&dev_data->lock);
1673 spin_unlock_irqrestore(&domain->lock, flags);
1679 * Removes a device from a protection domain (with devtable_lock held)
1681 static void detach_device(struct device *dev)
1683 struct protection_domain *domain;
1684 struct iommu_dev_data *dev_data;
1685 unsigned long flags;
1687 dev_data = dev_iommu_priv_get(dev);
1688 domain = dev_data->domain;
1690 spin_lock_irqsave(&domain->lock, flags);
1692 spin_lock(&dev_data->lock);
1695 * First check if the device is still attached. It might already
1696 * be detached from its domain because the generic
1697 * iommu_detach_group code detached it and we try again here in
1698 * our alias handling.
1700 if (WARN_ON(!dev_data->domain))
1703 do_detach(dev_data);
1705 if (!dev_is_pci(dev))
1708 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
1709 pdev_iommuv2_disable(to_pci_dev(dev));
1710 else if (dev_data->ats.enabled)
1711 pci_disable_ats(to_pci_dev(dev));
1713 dev_data->ats.enabled = false;
1716 spin_unlock(&dev_data->lock);
1718 spin_unlock_irqrestore(&domain->lock, flags);
1721 static struct iommu_device *amd_iommu_probe_device(struct device *dev)
1723 struct iommu_device *iommu_dev;
1724 struct amd_iommu *iommu;
1727 if (!check_device(dev))
1728 return ERR_PTR(-ENODEV);
1730 devid = get_device_id(dev);
1731 iommu = amd_iommu_rlookup_table[devid];
1733 if (dev_iommu_priv_get(dev))
1734 return &iommu->iommu;
1736 ret = iommu_init_device(dev);
1738 if (ret != -ENOTSUPP)
1739 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
1740 iommu_dev = ERR_PTR(ret);
1741 iommu_ignore_device(dev);
1743 amd_iommu_set_pci_msi_domain(dev, iommu);
1744 iommu_dev = &iommu->iommu;
1747 iommu_completion_wait(iommu);
1752 static void amd_iommu_probe_finalize(struct device *dev)
1754 /* Domains are initialized for this device - have a look what we ended up with */
1755 set_dma_ops(dev, NULL);
1756 iommu_setup_dma_ops(dev, 0, U64_MAX);
1759 static void amd_iommu_release_device(struct device *dev)
1761 int devid = get_device_id(dev);
1762 struct amd_iommu *iommu;
1764 if (!check_device(dev))
1767 iommu = amd_iommu_rlookup_table[devid];
1769 amd_iommu_uninit_device(dev);
1770 iommu_completion_wait(iommu);
1773 static struct iommu_group *amd_iommu_device_group(struct device *dev)
1775 if (dev_is_pci(dev))
1776 return pci_device_group(dev);
1778 return acpihid_device_group(dev);
1781 /*****************************************************************************
1783 * The next functions belong to the dma_ops mapping/unmapping code.
1785 *****************************************************************************/
1787 static void update_device_table(struct protection_domain *domain)
1789 struct iommu_dev_data *dev_data;
1791 list_for_each_entry(dev_data, &domain->dev_list, list) {
1792 set_dte_entry(dev_data->devid, domain,
1793 dev_data->ats.enabled, dev_data->iommu_v2);
1794 clone_aliases(dev_data->pdev);
1798 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
1800 update_device_table(domain);
1801 domain_flush_devices(domain);
1804 void amd_iommu_domain_update(struct protection_domain *domain)
1806 /* Update device table */
1807 amd_iommu_update_and_flush_device_table(domain);
1809 /* Flush domain TLB(s) and wait for completion */
1810 amd_iommu_domain_flush_tlb_pde(domain);
1811 amd_iommu_domain_flush_complete(domain);
1814 static void __init amd_iommu_init_dma_ops(void)
1816 swiotlb = (iommu_default_passthrough() || sme_me_mask) ? 1 : 0;
1819 int __init amd_iommu_init_api(void)
1823 amd_iommu_init_dma_ops();
1825 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
1828 #ifdef CONFIG_ARM_AMBA
1829 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
1833 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
1840 /*****************************************************************************
1842 * The following functions belong to the exported interface of AMD IOMMU
1844 * This interface allows access to lower level functions of the IOMMU
1845 * like protection domain handling and assignement of devices to domains
1846 * which is not possible with the dma_ops interface.
1848 *****************************************************************************/
1850 static void cleanup_domain(struct protection_domain *domain)
1852 struct iommu_dev_data *entry;
1853 unsigned long flags;
1855 spin_lock_irqsave(&domain->lock, flags);
1857 while (!list_empty(&domain->dev_list)) {
1858 entry = list_first_entry(&domain->dev_list,
1859 struct iommu_dev_data, list);
1860 BUG_ON(!entry->domain);
1864 spin_unlock_irqrestore(&domain->lock, flags);
1867 static void protection_domain_free(struct protection_domain *domain)
1873 domain_id_free(domain->id);
1875 if (domain->iop.pgtbl_cfg.tlb)
1876 free_io_pgtable_ops(&domain->iop.iop.ops);
1881 static int protection_domain_init_v1(struct protection_domain *domain, int mode)
1883 u64 *pt_root = NULL;
1885 BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
1887 spin_lock_init(&domain->lock);
1888 domain->id = domain_id_alloc();
1891 INIT_LIST_HEAD(&domain->dev_list);
1893 if (mode != PAGE_MODE_NONE) {
1894 pt_root = (void *)get_zeroed_page(GFP_KERNEL);
1899 amd_iommu_domain_set_pgtable(domain, pt_root, mode);
1904 static struct protection_domain *protection_domain_alloc(unsigned int type)
1906 struct io_pgtable_ops *pgtbl_ops;
1907 struct protection_domain *domain;
1908 int pgtable = amd_iommu_pgtable;
1909 int mode = DEFAULT_PGTABLE_LEVEL;
1912 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
1917 * Force IOMMU v1 page table when iommu=pt and
1918 * when allocating domain for pass-through devices.
1920 if (type == IOMMU_DOMAIN_IDENTITY) {
1921 pgtable = AMD_IOMMU_V1;
1922 mode = PAGE_MODE_NONE;
1923 } else if (type == IOMMU_DOMAIN_UNMANAGED) {
1924 pgtable = AMD_IOMMU_V1;
1929 ret = protection_domain_init_v1(domain, mode);
1938 pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
1948 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
1950 struct protection_domain *domain;
1952 domain = protection_domain_alloc(type);
1956 domain->domain.geometry.aperture_start = 0;
1957 domain->domain.geometry.aperture_end = ~0ULL;
1958 domain->domain.geometry.force_aperture = true;
1960 return &domain->domain;
1963 static void amd_iommu_domain_free(struct iommu_domain *dom)
1965 struct protection_domain *domain;
1967 domain = to_pdomain(dom);
1969 if (domain->dev_cnt > 0)
1970 cleanup_domain(domain);
1972 BUG_ON(domain->dev_cnt != 0);
1977 if (domain->flags & PD_IOMMUV2_MASK)
1978 free_gcr3_table(domain);
1980 protection_domain_free(domain);
1983 static void amd_iommu_detach_device(struct iommu_domain *dom,
1986 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
1987 int devid = get_device_id(dev);
1988 struct amd_iommu *iommu;
1990 if (!check_device(dev))
1993 if (dev_data->domain != NULL)
1996 iommu = amd_iommu_rlookup_table[devid];
2000 #ifdef CONFIG_IRQ_REMAP
2001 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2002 (dom->type == IOMMU_DOMAIN_UNMANAGED))
2003 dev_data->use_vapic = 0;
2006 iommu_completion_wait(iommu);
2009 static int amd_iommu_attach_device(struct iommu_domain *dom,
2012 struct protection_domain *domain = to_pdomain(dom);
2013 struct iommu_dev_data *dev_data;
2014 struct amd_iommu *iommu;
2017 if (!check_device(dev))
2020 dev_data = dev_iommu_priv_get(dev);
2021 dev_data->defer_attach = false;
2023 iommu = amd_iommu_rlookup_table[dev_data->devid];
2027 if (dev_data->domain)
2030 ret = attach_device(dev, domain);
2032 #ifdef CONFIG_IRQ_REMAP
2033 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
2034 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
2035 dev_data->use_vapic = 1;
2037 dev_data->use_vapic = 0;
2041 iommu_completion_wait(iommu);
2046 static void amd_iommu_iotlb_sync_map(struct iommu_domain *dom,
2047 unsigned long iova, size_t size)
2049 struct protection_domain *domain = to_pdomain(dom);
2050 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2053 domain_flush_np_cache(domain, iova, size);
2056 static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2057 phys_addr_t paddr, size_t page_size, int iommu_prot,
2060 struct protection_domain *domain = to_pdomain(dom);
2061 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2065 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2066 (domain->iop.mode == PAGE_MODE_NONE))
2069 if (iommu_prot & IOMMU_READ)
2070 prot |= IOMMU_PROT_IR;
2071 if (iommu_prot & IOMMU_WRITE)
2072 prot |= IOMMU_PROT_IW;
2075 ret = ops->map(ops, iova, paddr, page_size, prot, gfp);
2080 static void amd_iommu_iotlb_gather_add_page(struct iommu_domain *domain,
2081 struct iommu_iotlb_gather *gather,
2082 unsigned long iova, size_t size)
2085 * AMD's IOMMU can flush as many pages as necessary in a single flush.
2086 * Unless we run in a virtual machine, which can be inferred according
2087 * to whether "non-present cache" is on, it is probably best to prefer
2088 * (potentially) too extensive TLB flushing (i.e., more misses) over
2089 * mutliple TLB flushes (i.e., more flushes). For virtual machines the
2090 * hypervisor needs to synchronize the host IOMMU PTEs with those of
2091 * the guest, and the trade-off is different: unnecessary TLB flushes
2092 * should be avoided.
2094 if (amd_iommu_np_cache &&
2095 iommu_iotlb_gather_is_disjoint(gather, iova, size))
2096 iommu_iotlb_sync(domain, gather);
2098 iommu_iotlb_gather_add_range(gather, iova, size);
2101 static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2103 struct iommu_iotlb_gather *gather)
2105 struct protection_domain *domain = to_pdomain(dom);
2106 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2109 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2110 (domain->iop.mode == PAGE_MODE_NONE))
2113 r = (ops->unmap) ? ops->unmap(ops, iova, page_size, gather) : 0;
2115 amd_iommu_iotlb_gather_add_page(dom, gather, iova, page_size);
2120 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2123 struct protection_domain *domain = to_pdomain(dom);
2124 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2126 return ops->iova_to_phys(ops, iova);
2129 static bool amd_iommu_capable(enum iommu_cap cap)
2132 case IOMMU_CAP_CACHE_COHERENCY:
2134 case IOMMU_CAP_INTR_REMAP:
2135 return (irq_remapping_enabled == 1);
2136 case IOMMU_CAP_NOEXEC:
2145 static void amd_iommu_get_resv_regions(struct device *dev,
2146 struct list_head *head)
2148 struct iommu_resv_region *region;
2149 struct unity_map_entry *entry;
2152 devid = get_device_id(dev);
2156 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
2160 if (devid < entry->devid_start || devid > entry->devid_end)
2163 type = IOMMU_RESV_DIRECT;
2164 length = entry->address_end - entry->address_start;
2165 if (entry->prot & IOMMU_PROT_IR)
2167 if (entry->prot & IOMMU_PROT_IW)
2168 prot |= IOMMU_WRITE;
2169 if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
2170 /* Exclusion range */
2171 type = IOMMU_RESV_RESERVED;
2173 region = iommu_alloc_resv_region(entry->address_start,
2174 length, prot, type);
2176 dev_err(dev, "Out of memory allocating dm-regions\n");
2179 list_add_tail(®ion->list, head);
2182 region = iommu_alloc_resv_region(MSI_RANGE_START,
2183 MSI_RANGE_END - MSI_RANGE_START + 1,
2187 list_add_tail(®ion->list, head);
2189 region = iommu_alloc_resv_region(HT_RANGE_START,
2190 HT_RANGE_END - HT_RANGE_START + 1,
2191 0, IOMMU_RESV_RESERVED);
2194 list_add_tail(®ion->list, head);
2197 bool amd_iommu_is_attach_deferred(struct iommu_domain *domain,
2200 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2202 return dev_data->defer_attach;
2204 EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred);
2206 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
2208 struct protection_domain *dom = to_pdomain(domain);
2209 unsigned long flags;
2211 spin_lock_irqsave(&dom->lock, flags);
2212 amd_iommu_domain_flush_tlb_pde(dom);
2213 amd_iommu_domain_flush_complete(dom);
2214 spin_unlock_irqrestore(&dom->lock, flags);
2217 static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
2218 struct iommu_iotlb_gather *gather)
2220 struct protection_domain *dom = to_pdomain(domain);
2221 unsigned long flags;
2223 spin_lock_irqsave(&dom->lock, flags);
2224 domain_flush_pages(dom, gather->start, gather->end - gather->start, 1);
2225 amd_iommu_domain_flush_complete(dom);
2226 spin_unlock_irqrestore(&dom->lock, flags);
2229 static int amd_iommu_def_domain_type(struct device *dev)
2231 struct iommu_dev_data *dev_data;
2233 dev_data = dev_iommu_priv_get(dev);
2238 * Do not identity map IOMMUv2 capable devices when memory encryption is
2239 * active, because some of those devices (AMD GPUs) don't have the
2240 * encryption bit in their DMA-mask and require remapping.
2242 if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT) && dev_data->iommu_v2)
2243 return IOMMU_DOMAIN_IDENTITY;
2248 const struct iommu_ops amd_iommu_ops = {
2249 .capable = amd_iommu_capable,
2250 .domain_alloc = amd_iommu_domain_alloc,
2251 .domain_free = amd_iommu_domain_free,
2252 .attach_dev = amd_iommu_attach_device,
2253 .detach_dev = amd_iommu_detach_device,
2254 .map = amd_iommu_map,
2255 .iotlb_sync_map = amd_iommu_iotlb_sync_map,
2256 .unmap = amd_iommu_unmap,
2257 .iova_to_phys = amd_iommu_iova_to_phys,
2258 .probe_device = amd_iommu_probe_device,
2259 .release_device = amd_iommu_release_device,
2260 .probe_finalize = amd_iommu_probe_finalize,
2261 .device_group = amd_iommu_device_group,
2262 .get_resv_regions = amd_iommu_get_resv_regions,
2263 .put_resv_regions = generic_iommu_put_resv_regions,
2264 .is_attach_deferred = amd_iommu_is_attach_deferred,
2265 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
2266 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2267 .iotlb_sync = amd_iommu_iotlb_sync,
2268 .def_domain_type = amd_iommu_def_domain_type,
2271 /*****************************************************************************
2273 * The next functions do a basic initialization of IOMMU for pass through
2276 * In passthrough mode the IOMMU is initialized and enabled but not used for
2277 * DMA-API translation.
2279 *****************************************************************************/
2281 /* IOMMUv2 specific functions */
2282 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2284 return atomic_notifier_chain_register(&ppr_notifier, nb);
2286 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2288 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2290 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2292 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
2294 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2296 struct protection_domain *domain = to_pdomain(dom);
2297 unsigned long flags;
2299 spin_lock_irqsave(&domain->lock, flags);
2301 if (domain->iop.pgtbl_cfg.tlb)
2302 free_io_pgtable_ops(&domain->iop.iop.ops);
2304 spin_unlock_irqrestore(&domain->lock, flags);
2306 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
2308 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
2310 struct protection_domain *domain = to_pdomain(dom);
2311 unsigned long flags;
2314 /* Number of GCR3 table levels required */
2315 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
2318 if (levels > amd_iommu_max_glx_val)
2321 spin_lock_irqsave(&domain->lock, flags);
2324 * Save us all sanity checks whether devices already in the
2325 * domain support IOMMUv2. Just force that the domain has no
2326 * devices attached when it is switched into IOMMUv2 mode.
2329 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
2333 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
2334 if (domain->gcr3_tbl == NULL)
2337 domain->glx = levels;
2338 domain->flags |= PD_IOMMUV2_MASK;
2340 amd_iommu_domain_update(domain);
2345 spin_unlock_irqrestore(&domain->lock, flags);
2349 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
2351 static int __flush_pasid(struct protection_domain *domain, u32 pasid,
2352 u64 address, bool size)
2354 struct iommu_dev_data *dev_data;
2355 struct iommu_cmd cmd;
2358 if (!(domain->flags & PD_IOMMUV2_MASK))
2361 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
2364 * IOMMU TLB needs to be flushed before Device TLB to
2365 * prevent device TLB refill from IOMMU TLB
2367 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
2368 if (domain->dev_iommu[i] == 0)
2371 ret = iommu_queue_command(amd_iommus[i], &cmd);
2376 /* Wait until IOMMU TLB flushes are complete */
2377 amd_iommu_domain_flush_complete(domain);
2379 /* Now flush device TLBs */
2380 list_for_each_entry(dev_data, &domain->dev_list, list) {
2381 struct amd_iommu *iommu;
2385 There might be non-IOMMUv2 capable devices in an IOMMUv2
2388 if (!dev_data->ats.enabled)
2391 qdep = dev_data->ats.qdep;
2392 iommu = amd_iommu_rlookup_table[dev_data->devid];
2394 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
2395 qdep, address, size);
2397 ret = iommu_queue_command(iommu, &cmd);
2402 /* Wait until all device TLBs are flushed */
2403 amd_iommu_domain_flush_complete(domain);
2412 static int __amd_iommu_flush_page(struct protection_domain *domain, u32 pasid,
2415 return __flush_pasid(domain, pasid, address, false);
2418 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid,
2421 struct protection_domain *domain = to_pdomain(dom);
2422 unsigned long flags;
2425 spin_lock_irqsave(&domain->lock, flags);
2426 ret = __amd_iommu_flush_page(domain, pasid, address);
2427 spin_unlock_irqrestore(&domain->lock, flags);
2431 EXPORT_SYMBOL(amd_iommu_flush_page);
2433 static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid)
2435 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
2439 int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid)
2441 struct protection_domain *domain = to_pdomain(dom);
2442 unsigned long flags;
2445 spin_lock_irqsave(&domain->lock, flags);
2446 ret = __amd_iommu_flush_tlb(domain, pasid);
2447 spin_unlock_irqrestore(&domain->lock, flags);
2451 EXPORT_SYMBOL(amd_iommu_flush_tlb);
2453 static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc)
2460 index = (pasid >> (9 * level)) & 0x1ff;
2466 if (!(*pte & GCR3_VALID)) {
2470 root = (void *)get_zeroed_page(GFP_ATOMIC);
2474 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
2477 root = iommu_phys_to_virt(*pte & PAGE_MASK);
2485 static int __set_gcr3(struct protection_domain *domain, u32 pasid,
2490 if (domain->iop.mode != PAGE_MODE_NONE)
2493 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
2497 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
2499 return __amd_iommu_flush_tlb(domain, pasid);
2502 static int __clear_gcr3(struct protection_domain *domain, u32 pasid)
2506 if (domain->iop.mode != PAGE_MODE_NONE)
2509 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
2515 return __amd_iommu_flush_tlb(domain, pasid);
2518 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
2521 struct protection_domain *domain = to_pdomain(dom);
2522 unsigned long flags;
2525 spin_lock_irqsave(&domain->lock, flags);
2526 ret = __set_gcr3(domain, pasid, cr3);
2527 spin_unlock_irqrestore(&domain->lock, flags);
2531 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
2533 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid)
2535 struct protection_domain *domain = to_pdomain(dom);
2536 unsigned long flags;
2539 spin_lock_irqsave(&domain->lock, flags);
2540 ret = __clear_gcr3(domain, pasid);
2541 spin_unlock_irqrestore(&domain->lock, flags);
2545 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
2547 int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
2548 int status, int tag)
2550 struct iommu_dev_data *dev_data;
2551 struct amd_iommu *iommu;
2552 struct iommu_cmd cmd;
2554 dev_data = dev_iommu_priv_get(&pdev->dev);
2555 iommu = amd_iommu_rlookup_table[dev_data->devid];
2557 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
2558 tag, dev_data->pri_tlp);
2560 return iommu_queue_command(iommu, &cmd);
2562 EXPORT_SYMBOL(amd_iommu_complete_ppr);
2564 int amd_iommu_device_info(struct pci_dev *pdev,
2565 struct amd_iommu_device_info *info)
2570 if (pdev == NULL || info == NULL)
2573 if (!amd_iommu_v2_supported())
2576 memset(info, 0, sizeof(*info));
2578 if (pci_ats_supported(pdev))
2579 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
2581 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
2583 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
2585 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
2589 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
2590 max_pasids = min(max_pasids, (1 << 20));
2592 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
2593 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
2595 features = pci_pasid_features(pdev);
2596 if (features & PCI_PASID_CAP_EXEC)
2597 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
2598 if (features & PCI_PASID_CAP_PRIV)
2599 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
2604 EXPORT_SYMBOL(amd_iommu_device_info);
2606 #ifdef CONFIG_IRQ_REMAP
2608 /*****************************************************************************
2610 * Interrupt Remapping Implementation
2612 *****************************************************************************/
2614 static struct irq_chip amd_ir_chip;
2615 static DEFINE_SPINLOCK(iommu_table_lock);
2617 static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
2621 dte = amd_iommu_dev_table[devid].data[2];
2622 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
2623 dte |= iommu_virt_to_phys(table->table);
2624 dte |= DTE_IRQ_REMAP_INTCTL;
2625 dte |= DTE_INTTABLEN;
2626 dte |= DTE_IRQ_REMAP_ENABLE;
2628 amd_iommu_dev_table[devid].data[2] = dte;
2631 static struct irq_remap_table *get_irq_table(u16 devid)
2633 struct irq_remap_table *table;
2635 if (WARN_ONCE(!amd_iommu_rlookup_table[devid],
2636 "%s: no iommu for devid %x\n", __func__, devid))
2639 table = irq_lookup_table[devid];
2640 if (WARN_ONCE(!table, "%s: no table for devid %x\n", __func__, devid))
2646 static struct irq_remap_table *__alloc_irq_table(void)
2648 struct irq_remap_table *table;
2650 table = kzalloc(sizeof(*table), GFP_KERNEL);
2654 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
2655 if (!table->table) {
2659 raw_spin_lock_init(&table->lock);
2661 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2662 memset(table->table, 0,
2663 MAX_IRQS_PER_TABLE * sizeof(u32));
2665 memset(table->table, 0,
2666 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
2670 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
2671 struct irq_remap_table *table)
2673 irq_lookup_table[devid] = table;
2674 set_dte_irq_entry(devid, table);
2675 iommu_flush_dte(iommu, devid);
2678 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
2681 struct irq_remap_table *table = data;
2683 irq_lookup_table[alias] = table;
2684 set_dte_irq_entry(alias, table);
2686 iommu_flush_dte(amd_iommu_rlookup_table[alias], alias);
2691 static struct irq_remap_table *alloc_irq_table(u16 devid, struct pci_dev *pdev)
2693 struct irq_remap_table *table = NULL;
2694 struct irq_remap_table *new_table = NULL;
2695 struct amd_iommu *iommu;
2696 unsigned long flags;
2699 spin_lock_irqsave(&iommu_table_lock, flags);
2701 iommu = amd_iommu_rlookup_table[devid];
2705 table = irq_lookup_table[devid];
2709 alias = amd_iommu_alias_table[devid];
2710 table = irq_lookup_table[alias];
2712 set_remap_table_entry(iommu, devid, table);
2715 spin_unlock_irqrestore(&iommu_table_lock, flags);
2717 /* Nothing there yet, allocate new irq remapping table */
2718 new_table = __alloc_irq_table();
2722 spin_lock_irqsave(&iommu_table_lock, flags);
2724 table = irq_lookup_table[devid];
2728 table = irq_lookup_table[alias];
2730 set_remap_table_entry(iommu, devid, table);
2738 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
2741 set_remap_table_entry(iommu, devid, table);
2744 set_remap_table_entry(iommu, alias, table);
2747 iommu_completion_wait(iommu);
2750 spin_unlock_irqrestore(&iommu_table_lock, flags);
2753 kmem_cache_free(amd_iommu_irq_cache, new_table->table);
2759 static int alloc_irq_index(u16 devid, int count, bool align,
2760 struct pci_dev *pdev)
2762 struct irq_remap_table *table;
2763 int index, c, alignment = 1;
2764 unsigned long flags;
2765 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
2770 table = alloc_irq_table(devid, pdev);
2775 alignment = roundup_pow_of_two(count);
2777 raw_spin_lock_irqsave(&table->lock, flags);
2779 /* Scan table for free entries */
2780 for (index = ALIGN(table->min_index, alignment), c = 0;
2781 index < MAX_IRQS_PER_TABLE;) {
2782 if (!iommu->irte_ops->is_allocated(table, index)) {
2786 index = ALIGN(index + 1, alignment);
2792 iommu->irte_ops->set_allocated(table, index - c + 1);
2804 raw_spin_unlock_irqrestore(&table->lock, flags);
2809 static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
2810 struct amd_ir_data *data)
2813 struct irq_remap_table *table;
2814 struct amd_iommu *iommu;
2815 unsigned long flags;
2816 struct irte_ga *entry;
2818 iommu = amd_iommu_rlookup_table[devid];
2822 table = get_irq_table(devid);
2826 raw_spin_lock_irqsave(&table->lock, flags);
2828 entry = (struct irte_ga *)table->table;
2829 entry = &entry[index];
2831 ret = cmpxchg_double(&entry->lo.val, &entry->hi.val,
2832 entry->lo.val, entry->hi.val,
2833 irte->lo.val, irte->hi.val);
2835 * We use cmpxchg16 to atomically update the 128-bit IRTE,
2836 * and it cannot be updated by the hardware or other processors
2837 * behind us, so the return value of cmpxchg16 should be the
2838 * same as the old value.
2845 raw_spin_unlock_irqrestore(&table->lock, flags);
2847 iommu_flush_irt(iommu, devid);
2848 iommu_completion_wait(iommu);
2853 static int modify_irte(u16 devid, int index, union irte *irte)
2855 struct irq_remap_table *table;
2856 struct amd_iommu *iommu;
2857 unsigned long flags;
2859 iommu = amd_iommu_rlookup_table[devid];
2863 table = get_irq_table(devid);
2867 raw_spin_lock_irqsave(&table->lock, flags);
2868 table->table[index] = irte->val;
2869 raw_spin_unlock_irqrestore(&table->lock, flags);
2871 iommu_flush_irt(iommu, devid);
2872 iommu_completion_wait(iommu);
2877 static void free_irte(u16 devid, int index)
2879 struct irq_remap_table *table;
2880 struct amd_iommu *iommu;
2881 unsigned long flags;
2883 iommu = amd_iommu_rlookup_table[devid];
2887 table = get_irq_table(devid);
2891 raw_spin_lock_irqsave(&table->lock, flags);
2892 iommu->irte_ops->clear_allocated(table, index);
2893 raw_spin_unlock_irqrestore(&table->lock, flags);
2895 iommu_flush_irt(iommu, devid);
2896 iommu_completion_wait(iommu);
2899 static void irte_prepare(void *entry,
2900 u32 delivery_mode, bool dest_mode,
2901 u8 vector, u32 dest_apicid, int devid)
2903 union irte *irte = (union irte *) entry;
2906 irte->fields.vector = vector;
2907 irte->fields.int_type = delivery_mode;
2908 irte->fields.destination = dest_apicid;
2909 irte->fields.dm = dest_mode;
2910 irte->fields.valid = 1;
2913 static void irte_ga_prepare(void *entry,
2914 u32 delivery_mode, bool dest_mode,
2915 u8 vector, u32 dest_apicid, int devid)
2917 struct irte_ga *irte = (struct irte_ga *) entry;
2921 irte->lo.fields_remap.int_type = delivery_mode;
2922 irte->lo.fields_remap.dm = dest_mode;
2923 irte->hi.fields.vector = vector;
2924 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
2925 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
2926 irte->lo.fields_remap.valid = 1;
2929 static void irte_activate(void *entry, u16 devid, u16 index)
2931 union irte *irte = (union irte *) entry;
2933 irte->fields.valid = 1;
2934 modify_irte(devid, index, irte);
2937 static void irte_ga_activate(void *entry, u16 devid, u16 index)
2939 struct irte_ga *irte = (struct irte_ga *) entry;
2941 irte->lo.fields_remap.valid = 1;
2942 modify_irte_ga(devid, index, irte, NULL);
2945 static void irte_deactivate(void *entry, u16 devid, u16 index)
2947 union irte *irte = (union irte *) entry;
2949 irte->fields.valid = 0;
2950 modify_irte(devid, index, irte);
2953 static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
2955 struct irte_ga *irte = (struct irte_ga *) entry;
2957 irte->lo.fields_remap.valid = 0;
2958 modify_irte_ga(devid, index, irte, NULL);
2961 static void irte_set_affinity(void *entry, u16 devid, u16 index,
2962 u8 vector, u32 dest_apicid)
2964 union irte *irte = (union irte *) entry;
2966 irte->fields.vector = vector;
2967 irte->fields.destination = dest_apicid;
2968 modify_irte(devid, index, irte);
2971 static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
2972 u8 vector, u32 dest_apicid)
2974 struct irte_ga *irte = (struct irte_ga *) entry;
2976 if (!irte->lo.fields_remap.guest_mode) {
2977 irte->hi.fields.vector = vector;
2978 irte->lo.fields_remap.destination =
2979 APICID_TO_IRTE_DEST_LO(dest_apicid);
2980 irte->hi.fields.destination =
2981 APICID_TO_IRTE_DEST_HI(dest_apicid);
2982 modify_irte_ga(devid, index, irte, NULL);
2986 #define IRTE_ALLOCATED (~1U)
2987 static void irte_set_allocated(struct irq_remap_table *table, int index)
2989 table->table[index] = IRTE_ALLOCATED;
2992 static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
2994 struct irte_ga *ptr = (struct irte_ga *)table->table;
2995 struct irte_ga *irte = &ptr[index];
2997 memset(&irte->lo.val, 0, sizeof(u64));
2998 memset(&irte->hi.val, 0, sizeof(u64));
2999 irte->hi.fields.vector = 0xff;
3002 static bool irte_is_allocated(struct irq_remap_table *table, int index)
3004 union irte *ptr = (union irte *)table->table;
3005 union irte *irte = &ptr[index];
3007 return irte->val != 0;
3010 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3012 struct irte_ga *ptr = (struct irte_ga *)table->table;
3013 struct irte_ga *irte = &ptr[index];
3015 return irte->hi.fields.vector != 0;
3018 static void irte_clear_allocated(struct irq_remap_table *table, int index)
3020 table->table[index] = 0;
3023 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3025 struct irte_ga *ptr = (struct irte_ga *)table->table;
3026 struct irte_ga *irte = &ptr[index];
3028 memset(&irte->lo.val, 0, sizeof(u64));
3029 memset(&irte->hi.val, 0, sizeof(u64));
3032 static int get_devid(struct irq_alloc_info *info)
3034 switch (info->type) {
3035 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3036 return get_ioapic_devid(info->devid);
3037 case X86_IRQ_ALLOC_TYPE_HPET:
3038 return get_hpet_devid(info->devid);
3039 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3040 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3041 return get_device_id(msi_desc_to_dev(info->desc));
3048 struct irq_remap_ops amd_iommu_irq_ops = {
3049 .prepare = amd_iommu_prepare,
3050 .enable = amd_iommu_enable,
3051 .disable = amd_iommu_disable,
3052 .reenable = amd_iommu_reenable,
3053 .enable_faulting = amd_iommu_enable_faulting,
3056 static void fill_msi_msg(struct msi_msg *msg, u32 index)
3059 msg->address_lo = 0;
3060 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
3061 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
3064 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3065 struct irq_cfg *irq_cfg,
3066 struct irq_alloc_info *info,
3067 int devid, int index, int sub_handle)
3069 struct irq_2_irte *irte_info = &data->irq_2_irte;
3070 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3075 data->irq_2_irte.devid = devid;
3076 data->irq_2_irte.index = index + sub_handle;
3077 iommu->irte_ops->prepare(data->entry, apic->delivery_mode,
3078 apic->dest_mode_logical, irq_cfg->vector,
3079 irq_cfg->dest_apicid, devid);
3081 switch (info->type) {
3082 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3083 case X86_IRQ_ALLOC_TYPE_HPET:
3084 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3085 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3086 fill_msi_msg(&data->msi_entry, irte_info->index);
3095 struct amd_irte_ops irte_32_ops = {
3096 .prepare = irte_prepare,
3097 .activate = irte_activate,
3098 .deactivate = irte_deactivate,
3099 .set_affinity = irte_set_affinity,
3100 .set_allocated = irte_set_allocated,
3101 .is_allocated = irte_is_allocated,
3102 .clear_allocated = irte_clear_allocated,
3105 struct amd_irte_ops irte_128_ops = {
3106 .prepare = irte_ga_prepare,
3107 .activate = irte_ga_activate,
3108 .deactivate = irte_ga_deactivate,
3109 .set_affinity = irte_ga_set_affinity,
3110 .set_allocated = irte_ga_set_allocated,
3111 .is_allocated = irte_ga_is_allocated,
3112 .clear_allocated = irte_ga_clear_allocated,
3115 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3116 unsigned int nr_irqs, void *arg)
3118 struct irq_alloc_info *info = arg;
3119 struct irq_data *irq_data;
3120 struct amd_ir_data *data = NULL;
3121 struct irq_cfg *cfg;
3127 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI &&
3128 info->type != X86_IRQ_ALLOC_TYPE_PCI_MSIX)
3132 * With IRQ remapping enabled, don't need contiguous CPU vectors
3133 * to support multiple MSI interrupts.
3135 if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
3136 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3138 devid = get_devid(info);
3142 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3146 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3147 struct irq_remap_table *table;
3148 struct amd_iommu *iommu;
3150 table = alloc_irq_table(devid, NULL);
3152 if (!table->min_index) {
3154 * Keep the first 32 indexes free for IOAPIC
3157 table->min_index = 32;
3158 iommu = amd_iommu_rlookup_table[devid];
3159 for (i = 0; i < 32; ++i)
3160 iommu->irte_ops->set_allocated(table, i);
3162 WARN_ON(table->min_index != 32);
3163 index = info->ioapic.pin;
3167 } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
3168 info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
3169 bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
3171 index = alloc_irq_index(devid, nr_irqs, align,
3172 msi_desc_to_pci_dev(info->desc));
3174 index = alloc_irq_index(devid, nr_irqs, false, NULL);
3178 pr_warn("Failed to allocate IRTE\n");
3180 goto out_free_parent;
3183 for (i = 0; i < nr_irqs; i++) {
3184 irq_data = irq_domain_get_irq_data(domain, virq + i);
3185 cfg = irq_data ? irqd_cfg(irq_data) : NULL;
3192 data = kzalloc(sizeof(*data), GFP_KERNEL);
3196 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3197 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
3199 data->entry = kzalloc(sizeof(struct irte_ga),
3206 irq_data->hwirq = (devid << 16) + i;
3207 irq_data->chip_data = data;
3208 irq_data->chip = &amd_ir_chip;
3209 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3210 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3216 for (i--; i >= 0; i--) {
3217 irq_data = irq_domain_get_irq_data(domain, virq + i);
3219 kfree(irq_data->chip_data);
3221 for (i = 0; i < nr_irqs; i++)
3222 free_irte(devid, index + i);
3224 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3228 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3229 unsigned int nr_irqs)
3231 struct irq_2_irte *irte_info;
3232 struct irq_data *irq_data;
3233 struct amd_ir_data *data;
3236 for (i = 0; i < nr_irqs; i++) {
3237 irq_data = irq_domain_get_irq_data(domain, virq + i);
3238 if (irq_data && irq_data->chip_data) {
3239 data = irq_data->chip_data;
3240 irte_info = &data->irq_2_irte;
3241 free_irte(irte_info->devid, irte_info->index);
3246 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3249 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3250 struct amd_ir_data *ir_data,
3251 struct irq_2_irte *irte_info,
3252 struct irq_cfg *cfg);
3254 static int irq_remapping_activate(struct irq_domain *domain,
3255 struct irq_data *irq_data, bool reserve)
3257 struct amd_ir_data *data = irq_data->chip_data;
3258 struct irq_2_irte *irte_info = &data->irq_2_irte;
3259 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3260 struct irq_cfg *cfg = irqd_cfg(irq_data);
3265 iommu->irte_ops->activate(data->entry, irte_info->devid,
3267 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
3271 static void irq_remapping_deactivate(struct irq_domain *domain,
3272 struct irq_data *irq_data)
3274 struct amd_ir_data *data = irq_data->chip_data;
3275 struct irq_2_irte *irte_info = &data->irq_2_irte;
3276 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3279 iommu->irte_ops->deactivate(data->entry, irte_info->devid,
3283 static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec,
3284 enum irq_domain_bus_token bus_token)
3286 struct amd_iommu *iommu;
3289 if (!amd_iommu_irq_remap)
3292 if (x86_fwspec_is_ioapic(fwspec))
3293 devid = get_ioapic_devid(fwspec->param[0]);
3294 else if (x86_fwspec_is_hpet(fwspec))
3295 devid = get_hpet_devid(fwspec->param[0]);
3300 iommu = amd_iommu_rlookup_table[devid];
3301 return iommu && iommu->ir_domain == d;
3304 static const struct irq_domain_ops amd_ir_domain_ops = {
3305 .select = irq_remapping_select,
3306 .alloc = irq_remapping_alloc,
3307 .free = irq_remapping_free,
3308 .activate = irq_remapping_activate,
3309 .deactivate = irq_remapping_deactivate,
3312 int amd_iommu_activate_guest_mode(void *data)
3314 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3315 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3318 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3319 !entry || entry->lo.fields_vapic.guest_mode)
3322 valid = entry->lo.fields_vapic.valid;
3327 entry->lo.fields_vapic.valid = valid;
3328 entry->lo.fields_vapic.guest_mode = 1;
3329 entry->lo.fields_vapic.ga_log_intr = 1;
3330 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
3331 entry->hi.fields.vector = ir_data->ga_vector;
3332 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
3334 return modify_irte_ga(ir_data->irq_2_irte.devid,
3335 ir_data->irq_2_irte.index, entry, ir_data);
3337 EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
3339 int amd_iommu_deactivate_guest_mode(void *data)
3341 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3342 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3343 struct irq_cfg *cfg = ir_data->cfg;
3346 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3347 !entry || !entry->lo.fields_vapic.guest_mode)
3350 valid = entry->lo.fields_remap.valid;
3355 entry->lo.fields_remap.valid = valid;
3356 entry->lo.fields_remap.dm = apic->dest_mode_logical;
3357 entry->lo.fields_remap.int_type = apic->delivery_mode;
3358 entry->hi.fields.vector = cfg->vector;
3359 entry->lo.fields_remap.destination =
3360 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
3361 entry->hi.fields.destination =
3362 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
3364 return modify_irte_ga(ir_data->irq_2_irte.devid,
3365 ir_data->irq_2_irte.index, entry, ir_data);
3367 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
3369 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
3372 struct amd_iommu *iommu;
3373 struct amd_iommu_pi_data *pi_data = vcpu_info;
3374 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
3375 struct amd_ir_data *ir_data = data->chip_data;
3376 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3377 struct iommu_dev_data *dev_data = search_dev_data(irte_info->devid);
3380 * This device has never been set up for guest mode.
3381 * we should not modify the IRTE
3383 if (!dev_data || !dev_data->use_vapic)
3386 ir_data->cfg = irqd_cfg(data);
3387 pi_data->ir_data = ir_data;
3390 * SVM tries to set up for VAPIC mode, but we are in
3391 * legacy mode. So, we force legacy mode instead.
3393 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3394 pr_debug("%s: Fall back to using intr legacy remap\n",
3396 pi_data->is_guest_mode = false;
3399 iommu = amd_iommu_rlookup_table[irte_info->devid];
3403 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
3404 if (pi_data->is_guest_mode) {
3405 ir_data->ga_root_ptr = (pi_data->base >> 12);
3406 ir_data->ga_vector = vcpu_pi_info->vector;
3407 ir_data->ga_tag = pi_data->ga_tag;
3408 ret = amd_iommu_activate_guest_mode(ir_data);
3410 ir_data->cached_ga_tag = pi_data->ga_tag;
3412 ret = amd_iommu_deactivate_guest_mode(ir_data);
3415 * This communicates the ga_tag back to the caller
3416 * so that it can do all the necessary clean up.
3419 ir_data->cached_ga_tag = 0;
3426 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3427 struct amd_ir_data *ir_data,
3428 struct irq_2_irte *irte_info,
3429 struct irq_cfg *cfg)
3433 * Atomically updates the IRTE with the new destination, vector
3434 * and flushes the interrupt entry cache.
3436 iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
3437 irte_info->index, cfg->vector,
3441 static int amd_ir_set_affinity(struct irq_data *data,
3442 const struct cpumask *mask, bool force)
3444 struct amd_ir_data *ir_data = data->chip_data;
3445 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3446 struct irq_cfg *cfg = irqd_cfg(data);
3447 struct irq_data *parent = data->parent_data;
3448 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3454 ret = parent->chip->irq_set_affinity(parent, mask, force);
3455 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3458 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
3460 * After this point, all the interrupts will start arriving
3461 * at the new destination. So, time to cleanup the previous
3462 * vector allocation.
3464 send_cleanup_vector(cfg);
3466 return IRQ_SET_MASK_OK_DONE;
3469 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3471 struct amd_ir_data *ir_data = irq_data->chip_data;
3473 *msg = ir_data->msi_entry;
3476 static struct irq_chip amd_ir_chip = {
3478 .irq_ack = apic_ack_irq,
3479 .irq_set_affinity = amd_ir_set_affinity,
3480 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
3481 .irq_compose_msi_msg = ir_compose_msi_msg,
3484 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3486 struct fwnode_handle *fn;
3488 fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
3491 iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
3492 if (!iommu->ir_domain) {
3493 irq_domain_free_fwnode(fn);
3497 iommu->ir_domain->parent = arch_get_ir_parent_domain();
3498 iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
3504 int amd_iommu_update_ga(int cpu, bool is_run, void *data)
3506 unsigned long flags;
3507 struct amd_iommu *iommu;
3508 struct irq_remap_table *table;
3509 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3510 int devid = ir_data->irq_2_irte.devid;
3511 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3512 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
3514 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3515 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
3518 iommu = amd_iommu_rlookup_table[devid];
3522 table = get_irq_table(devid);
3526 raw_spin_lock_irqsave(&table->lock, flags);
3528 if (ref->lo.fields_vapic.guest_mode) {
3530 ref->lo.fields_vapic.destination =
3531 APICID_TO_IRTE_DEST_LO(cpu);
3532 ref->hi.fields.destination =
3533 APICID_TO_IRTE_DEST_HI(cpu);
3535 ref->lo.fields_vapic.is_run = is_run;
3539 raw_spin_unlock_irqrestore(&table->lock, flags);
3541 iommu_flush_irt(iommu, devid);
3542 iommu_completion_wait(iommu);
3545 EXPORT_SYMBOL(amd_iommu_update_ga);