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/pci-ats.h>
15 #include <linux/bitmap.h>
16 #include <linux/slab.h>
17 #include <linux/debugfs.h>
18 #include <linux/scatterlist.h>
19 #include <linux/dma-map-ops.h>
20 #include <linux/dma-direct.h>
21 #include <linux/iommu-helper.h>
22 #include <linux/delay.h>
23 #include <linux/amd-iommu.h>
24 #include <linux/notifier.h>
25 #include <linux/export.h>
26 #include <linux/irq.h>
27 #include <linux/msi.h>
28 #include <linux/irqdomain.h>
29 #include <linux/percpu.h>
30 #include <linux/io-pgtable.h>
31 #include <linux/cc_platform.h>
32 #include <asm/irq_remapping.h>
33 #include <asm/io_apic.h>
35 #include <asm/hw_irq.h>
36 #include <asm/proto.h>
37 #include <asm/iommu.h>
41 #include "amd_iommu.h"
42 #include "../dma-iommu.h"
43 #include "../irq_remapping.h"
45 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
47 #define LOOP_TIMEOUT 100000
49 /* IO virtual address start page frame number */
50 #define IOVA_START_PFN (1)
51 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
53 /* Reserved IOVA ranges */
54 #define MSI_RANGE_START (0xfee00000)
55 #define MSI_RANGE_END (0xfeefffff)
56 #define HT_RANGE_START (0xfd00000000ULL)
57 #define HT_RANGE_END (0xffffffffffULL)
59 #define DEFAULT_PGTABLE_LEVEL PAGE_MODE_3_LEVEL
61 static DEFINE_SPINLOCK(pd_bitmap_lock);
63 LIST_HEAD(ioapic_map);
65 LIST_HEAD(acpihid_map);
67 const struct iommu_ops amd_iommu_ops;
69 static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
70 int amd_iommu_max_glx_val = -1;
73 * general struct to manage commands send to an IOMMU
79 struct kmem_cache *amd_iommu_irq_cache;
81 static void detach_device(struct device *dev);
82 static int domain_enable_v2(struct protection_domain *domain, int pasids);
84 /****************************************************************************
88 ****************************************************************************/
90 static inline int get_acpihid_device_id(struct device *dev,
91 struct acpihid_map_entry **entry)
93 struct acpi_device *adev = ACPI_COMPANION(dev);
94 struct acpihid_map_entry *p;
99 list_for_each_entry(p, &acpihid_map, list) {
100 if (acpi_dev_hid_uid_match(adev, p->hid,
101 p->uid[0] ? p->uid : NULL)) {
110 static inline int get_device_sbdf_id(struct device *dev)
115 sbdf = get_pci_sbdf_id(to_pci_dev(dev));
117 sbdf = get_acpihid_device_id(dev, NULL);
122 struct dev_table_entry *get_dev_table(struct amd_iommu *iommu)
124 struct dev_table_entry *dev_table;
125 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
127 BUG_ON(pci_seg == NULL);
128 dev_table = pci_seg->dev_table;
129 BUG_ON(dev_table == NULL);
134 static inline u16 get_device_segment(struct device *dev)
138 if (dev_is_pci(dev)) {
139 struct pci_dev *pdev = to_pci_dev(dev);
141 seg = pci_domain_nr(pdev->bus);
143 u32 devid = get_acpihid_device_id(dev, NULL);
145 seg = PCI_SBDF_TO_SEGID(devid);
151 /* Writes the specific IOMMU for a device into the PCI segment rlookup table */
152 void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid)
154 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
156 pci_seg->rlookup_table[devid] = iommu;
159 static struct amd_iommu *__rlookup_amd_iommu(u16 seg, u16 devid)
161 struct amd_iommu_pci_seg *pci_seg;
163 for_each_pci_segment(pci_seg) {
164 if (pci_seg->id == seg)
165 return pci_seg->rlookup_table[devid];
170 static struct amd_iommu *rlookup_amd_iommu(struct device *dev)
172 u16 seg = get_device_segment(dev);
173 int devid = get_device_sbdf_id(dev);
177 return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid));
180 static struct protection_domain *to_pdomain(struct iommu_domain *dom)
182 return container_of(dom, struct protection_domain, domain);
185 static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
187 struct iommu_dev_data *dev_data;
188 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
190 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
194 spin_lock_init(&dev_data->lock);
195 dev_data->devid = devid;
196 ratelimit_default_init(&dev_data->rs);
198 llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list);
202 static struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid)
204 struct iommu_dev_data *dev_data;
205 struct llist_node *node;
206 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
208 if (llist_empty(&pci_seg->dev_data_list))
211 node = pci_seg->dev_data_list.first;
212 llist_for_each_entry(dev_data, node, dev_data_list) {
213 if (dev_data->devid == devid)
220 static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
222 struct amd_iommu *iommu;
223 struct dev_table_entry *dev_table;
224 u16 devid = pci_dev_id(pdev);
229 iommu = rlookup_amd_iommu(&pdev->dev);
233 amd_iommu_set_rlookup_table(iommu, alias);
234 dev_table = get_dev_table(iommu);
235 memcpy(dev_table[alias].data,
236 dev_table[devid].data,
237 sizeof(dev_table[alias].data));
242 static void clone_aliases(struct amd_iommu *iommu, struct device *dev)
244 struct pci_dev *pdev;
246 if (!dev_is_pci(dev))
248 pdev = to_pci_dev(dev);
251 * The IVRS alias stored in the alias table may not be
252 * part of the PCI DMA aliases if it's bus differs
253 * from the original device.
255 clone_alias(pdev, iommu->pci_seg->alias_table[pci_dev_id(pdev)], NULL);
257 pci_for_each_dma_alias(pdev, clone_alias, NULL);
260 static void setup_aliases(struct amd_iommu *iommu, struct device *dev)
262 struct pci_dev *pdev = to_pci_dev(dev);
263 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
266 /* For ACPI HID devices, there are no aliases */
267 if (!dev_is_pci(dev))
271 * Add the IVRS alias to the pci aliases if it is on the same
272 * bus. The IVRS table may know about a quirk that we don't.
274 ivrs_alias = pci_seg->alias_table[pci_dev_id(pdev)];
275 if (ivrs_alias != pci_dev_id(pdev) &&
276 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number)
277 pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1);
279 clone_aliases(iommu, dev);
282 static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid)
284 struct iommu_dev_data *dev_data;
286 dev_data = search_dev_data(iommu, devid);
288 if (dev_data == NULL) {
289 dev_data = alloc_dev_data(iommu, devid);
293 if (translation_pre_enabled(iommu))
294 dev_data->defer_attach = true;
301 * Find or create an IOMMU group for a acpihid device.
303 static struct iommu_group *acpihid_device_group(struct device *dev)
305 struct acpihid_map_entry *p, *entry = NULL;
308 devid = get_acpihid_device_id(dev, &entry);
310 return ERR_PTR(devid);
312 list_for_each_entry(p, &acpihid_map, list) {
313 if ((devid == p->devid) && p->group)
314 entry->group = p->group;
318 entry->group = generic_device_group(dev);
320 iommu_group_ref_get(entry->group);
325 static bool pci_iommuv2_capable(struct pci_dev *pdev)
327 static const int caps[] = {
329 PCI_EXT_CAP_ID_PASID,
333 if (!pci_ats_supported(pdev))
336 for (i = 0; i < 2; ++i) {
337 pos = pci_find_ext_capability(pdev, caps[i]);
346 * This function checks if the driver got a valid device from the caller to
347 * avoid dereferencing invalid pointers.
349 static bool check_device(struct device *dev)
351 struct amd_iommu_pci_seg *pci_seg;
352 struct amd_iommu *iommu;
358 sbdf = get_device_sbdf_id(dev);
361 devid = PCI_SBDF_TO_DEVID(sbdf);
363 iommu = rlookup_amd_iommu(dev);
367 /* Out of our scope? */
368 pci_seg = iommu->pci_seg;
369 if (devid > pci_seg->last_bdf)
375 static int iommu_init_device(struct amd_iommu *iommu, struct device *dev)
377 struct iommu_dev_data *dev_data;
380 if (dev_iommu_priv_get(dev))
383 sbdf = get_device_sbdf_id(dev);
387 devid = PCI_SBDF_TO_DEVID(sbdf);
388 dev_data = find_dev_data(iommu, devid);
393 setup_aliases(iommu, dev);
396 * By default we use passthrough mode for IOMMUv2 capable device.
397 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
398 * invalid address), we ignore the capability for the device so
399 * it'll be forced to go into translation mode.
401 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
402 dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
403 dev_data->iommu_v2 = iommu->is_iommu_v2;
406 dev_iommu_priv_set(dev, dev_data);
411 static void iommu_ignore_device(struct amd_iommu *iommu, struct device *dev)
413 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
414 struct dev_table_entry *dev_table = get_dev_table(iommu);
417 sbdf = get_device_sbdf_id(dev);
421 devid = PCI_SBDF_TO_DEVID(sbdf);
422 pci_seg->rlookup_table[devid] = NULL;
423 memset(&dev_table[devid], 0, sizeof(struct dev_table_entry));
425 setup_aliases(iommu, dev);
428 static void amd_iommu_uninit_device(struct device *dev)
430 struct iommu_dev_data *dev_data;
432 dev_data = dev_iommu_priv_get(dev);
436 if (dev_data->domain)
439 dev_iommu_priv_set(dev, NULL);
442 * We keep dev_data around for unplugged devices and reuse it when the
443 * device is re-plugged - not doing so would introduce a ton of races.
447 /****************************************************************************
449 * Interrupt handling functions
451 ****************************************************************************/
453 static void dump_dte_entry(struct amd_iommu *iommu, u16 devid)
456 struct dev_table_entry *dev_table = get_dev_table(iommu);
458 for (i = 0; i < 4; ++i)
459 pr_err("DTE[%d]: %016llx\n", i, dev_table[devid].data[i]);
462 static void dump_command(unsigned long phys_addr)
464 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
467 for (i = 0; i < 4; ++i)
468 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
471 static void amd_iommu_report_rmp_hw_error(struct amd_iommu *iommu, volatile u32 *event)
473 struct iommu_dev_data *dev_data = NULL;
474 int devid, vmg_tag, flags;
475 struct pci_dev *pdev;
478 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
479 vmg_tag = (event[1]) & 0xFFFF;
480 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
481 spa = ((u64)event[3] << 32) | (event[2] & 0xFFFFFFF8);
483 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, 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 [RMP_HW_ERROR vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
491 vmg_tag, spa, flags);
494 pr_err_ratelimited("Event logged [RMP_HW_ERROR device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
495 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
496 vmg_tag, spa, flags);
503 static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *event)
505 struct iommu_dev_data *dev_data = NULL;
506 int devid, flags_rmp, vmg_tag, flags;
507 struct pci_dev *pdev;
510 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
511 flags_rmp = (event[0] >> EVENT_FLAGS_SHIFT) & 0xFF;
512 vmg_tag = (event[1]) & 0xFFFF;
513 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
514 gpa = ((u64)event[3] << 32) | event[2];
516 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
519 dev_data = dev_iommu_priv_get(&pdev->dev);
522 if (__ratelimit(&dev_data->rs)) {
523 pci_err(pdev, "Event logged [RMP_PAGE_FAULT vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
524 vmg_tag, gpa, flags_rmp, flags);
527 pr_err_ratelimited("Event logged [RMP_PAGE_FAULT device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
528 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
529 vmg_tag, gpa, flags_rmp, flags);
536 #define IS_IOMMU_MEM_TRANSACTION(flags) \
537 (((flags) & EVENT_FLAG_I) == 0)
539 #define IS_WRITE_REQUEST(flags) \
540 ((flags) & EVENT_FLAG_RW)
542 static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
543 u16 devid, u16 domain_id,
544 u64 address, int flags)
546 struct iommu_dev_data *dev_data = NULL;
547 struct pci_dev *pdev;
549 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
552 dev_data = dev_iommu_priv_get(&pdev->dev);
556 * If this is a DMA fault (for which the I(nterrupt)
557 * bit will be unset), allow report_iommu_fault() to
558 * prevent logging it.
560 if (IS_IOMMU_MEM_TRANSACTION(flags)) {
561 if (!report_iommu_fault(&dev_data->domain->domain,
563 IS_WRITE_REQUEST(flags) ?
569 if (__ratelimit(&dev_data->rs)) {
570 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
571 domain_id, address, flags);
574 pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
575 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
576 domain_id, address, flags);
584 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
586 struct device *dev = iommu->iommu.dev;
587 int type, devid, flags, tag;
588 volatile u32 *event = __evt;
594 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
595 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
596 pasid = (event[0] & EVENT_DOMID_MASK_HI) |
597 (event[1] & EVENT_DOMID_MASK_LO);
598 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
599 address = (u64)(((u64)event[3]) << 32) | event[2];
602 /* Did we hit the erratum? */
603 if (++count == LOOP_TIMEOUT) {
604 pr_err("No event written to event log\n");
611 if (type == EVENT_TYPE_IO_FAULT) {
612 amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
617 case EVENT_TYPE_ILL_DEV:
618 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
619 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
620 pasid, address, flags);
621 dump_dte_entry(iommu, devid);
623 case EVENT_TYPE_DEV_TAB_ERR:
624 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x "
625 "address=0x%llx flags=0x%04x]\n",
626 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
629 case EVENT_TYPE_PAGE_TAB_ERR:
630 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
631 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
632 pasid, address, flags);
634 case EVENT_TYPE_ILL_CMD:
635 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
636 dump_command(address);
638 case EVENT_TYPE_CMD_HARD_ERR:
639 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
642 case EVENT_TYPE_IOTLB_INV_TO:
643 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%04x:%02x:%02x.%x address=0x%llx]\n",
644 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
647 case EVENT_TYPE_INV_DEV_REQ:
648 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
649 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
650 pasid, address, flags);
652 case EVENT_TYPE_RMP_FAULT:
653 amd_iommu_report_rmp_fault(iommu, event);
655 case EVENT_TYPE_RMP_HW_ERR:
656 amd_iommu_report_rmp_hw_error(iommu, event);
658 case EVENT_TYPE_INV_PPR_REQ:
659 pasid = PPR_PASID(*((u64 *)__evt));
660 tag = event[1] & 0x03FF;
661 dev_err(dev, "Event logged [INVALID_PPR_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n",
662 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
663 pasid, address, flags, tag);
666 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
667 event[0], event[1], event[2], event[3]);
670 memset(__evt, 0, 4 * sizeof(u32));
673 static void iommu_poll_events(struct amd_iommu *iommu)
677 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
678 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
680 while (head != tail) {
681 iommu_print_event(iommu, iommu->evt_buf + head);
682 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
685 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
688 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
690 struct amd_iommu_fault fault;
692 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
693 pr_err_ratelimited("Unknown PPR request received\n");
697 fault.address = raw[1];
698 fault.pasid = PPR_PASID(raw[0]);
699 fault.sbdf = PCI_SEG_DEVID_TO_SBDF(iommu->pci_seg->id, PPR_DEVID(raw[0]));
700 fault.tag = PPR_TAG(raw[0]);
701 fault.flags = PPR_FLAGS(raw[0]);
703 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
706 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
710 if (iommu->ppr_log == NULL)
713 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
714 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
716 while (head != tail) {
721 raw = (u64 *)(iommu->ppr_log + head);
724 * Hardware bug: Interrupt may arrive before the entry is
725 * written to memory. If this happens we need to wait for the
728 for (i = 0; i < LOOP_TIMEOUT; ++i) {
729 if (PPR_REQ_TYPE(raw[0]) != 0)
734 /* Avoid memcpy function-call overhead */
739 * To detect the hardware bug we need to clear the entry
742 raw[0] = raw[1] = 0UL;
744 /* Update head pointer of hardware ring-buffer */
745 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
746 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
748 /* Handle PPR entry */
749 iommu_handle_ppr_entry(iommu, entry);
751 /* Refresh ring-buffer information */
752 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
753 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
757 #ifdef CONFIG_IRQ_REMAP
758 static int (*iommu_ga_log_notifier)(u32);
760 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
762 iommu_ga_log_notifier = notifier;
766 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
768 static void iommu_poll_ga_log(struct amd_iommu *iommu)
770 u32 head, tail, cnt = 0;
772 if (iommu->ga_log == NULL)
775 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
776 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
778 while (head != tail) {
782 raw = (u64 *)(iommu->ga_log + head);
785 /* Avoid memcpy function-call overhead */
788 /* Update head pointer of hardware ring-buffer */
789 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
790 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
792 /* Handle GA entry */
793 switch (GA_REQ_TYPE(log_entry)) {
795 if (!iommu_ga_log_notifier)
798 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
799 __func__, GA_DEVID(log_entry),
802 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
803 pr_err("GA log notifier failed.\n");
812 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
814 if (!irq_remapping_enabled || !dev_is_pci(dev) ||
815 pci_dev_has_special_msi_domain(to_pci_dev(dev)))
818 dev_set_msi_domain(dev, iommu->msi_domain);
821 #else /* CONFIG_IRQ_REMAP */
823 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
824 #endif /* !CONFIG_IRQ_REMAP */
826 #define AMD_IOMMU_INT_MASK \
827 (MMIO_STATUS_EVT_OVERFLOW_INT_MASK | \
828 MMIO_STATUS_EVT_INT_MASK | \
829 MMIO_STATUS_PPR_INT_MASK | \
830 MMIO_STATUS_GALOG_INT_MASK)
832 irqreturn_t amd_iommu_int_thread(int irq, void *data)
834 struct amd_iommu *iommu = (struct amd_iommu *) data;
835 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
837 while (status & AMD_IOMMU_INT_MASK) {
838 /* Enable interrupt sources again */
839 writel(AMD_IOMMU_INT_MASK,
840 iommu->mmio_base + MMIO_STATUS_OFFSET);
842 if (status & MMIO_STATUS_EVT_INT_MASK) {
843 pr_devel("Processing IOMMU Event Log\n");
844 iommu_poll_events(iommu);
847 if (status & MMIO_STATUS_PPR_INT_MASK) {
848 pr_devel("Processing IOMMU PPR Log\n");
849 iommu_poll_ppr_log(iommu);
852 #ifdef CONFIG_IRQ_REMAP
853 if (status & MMIO_STATUS_GALOG_INT_MASK) {
854 pr_devel("Processing IOMMU GA Log\n");
855 iommu_poll_ga_log(iommu);
859 if (status & MMIO_STATUS_EVT_OVERFLOW_INT_MASK) {
860 pr_info_ratelimited("IOMMU event log overflow\n");
861 amd_iommu_restart_event_logging(iommu);
865 * Hardware bug: ERBT1312
866 * When re-enabling interrupt (by writing 1
867 * to clear the bit), the hardware might also try to set
868 * the interrupt bit in the event status register.
869 * In this scenario, the bit will be set, and disable
870 * subsequent interrupts.
872 * Workaround: The IOMMU driver should read back the
873 * status register and check if the interrupt bits are cleared.
874 * If not, driver will need to go through the interrupt handler
875 * again and re-clear the bits
877 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
882 irqreturn_t amd_iommu_int_handler(int irq, void *data)
884 return IRQ_WAKE_THREAD;
887 /****************************************************************************
889 * IOMMU command queuing functions
891 ****************************************************************************/
893 static int wait_on_sem(struct amd_iommu *iommu, u64 data)
897 while (*iommu->cmd_sem != data && i < LOOP_TIMEOUT) {
902 if (i == LOOP_TIMEOUT) {
903 pr_alert("Completion-Wait loop timed out\n");
910 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
911 struct iommu_cmd *cmd)
916 /* Copy command to buffer */
917 tail = iommu->cmd_buf_tail;
918 target = iommu->cmd_buf + tail;
919 memcpy(target, cmd, sizeof(*cmd));
921 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
922 iommu->cmd_buf_tail = tail;
924 /* Tell the IOMMU about it */
925 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
928 static void build_completion_wait(struct iommu_cmd *cmd,
929 struct amd_iommu *iommu,
932 u64 paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
934 memset(cmd, 0, sizeof(*cmd));
935 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
936 cmd->data[1] = upper_32_bits(paddr);
937 cmd->data[2] = lower_32_bits(data);
938 cmd->data[3] = upper_32_bits(data);
939 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
942 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
944 memset(cmd, 0, sizeof(*cmd));
945 cmd->data[0] = devid;
946 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
950 * Builds an invalidation address which is suitable for one page or multiple
951 * pages. Sets the size bit (S) as needed is more than one page is flushed.
953 static inline u64 build_inv_address(u64 address, size_t size)
955 u64 pages, end, msb_diff;
957 pages = iommu_num_pages(address, size, PAGE_SIZE);
960 return address & PAGE_MASK;
962 end = address + size - 1;
965 * msb_diff would hold the index of the most significant bit that
966 * flipped between the start and end.
968 msb_diff = fls64(end ^ address) - 1;
971 * Bits 63:52 are sign extended. If for some reason bit 51 is different
972 * between the start and the end, invalidate everything.
974 if (unlikely(msb_diff > 51)) {
975 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
978 * The msb-bit must be clear on the address. Just set all the
981 address |= (1ull << msb_diff) - 1;
984 /* Clear bits 11:0 */
985 address &= PAGE_MASK;
987 /* Set the size bit - we flush more than one 4kb page */
988 return address | CMD_INV_IOMMU_PAGES_SIZE_MASK;
991 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
992 size_t size, u16 domid, int pde)
994 u64 inv_address = build_inv_address(address, size);
996 memset(cmd, 0, sizeof(*cmd));
997 cmd->data[1] |= domid;
998 cmd->data[2] = lower_32_bits(inv_address);
999 cmd->data[3] = upper_32_bits(inv_address);
1000 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1001 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
1002 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1005 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
1006 u64 address, size_t size)
1008 u64 inv_address = build_inv_address(address, size);
1010 memset(cmd, 0, sizeof(*cmd));
1011 cmd->data[0] = devid;
1012 cmd->data[0] |= (qdep & 0xff) << 24;
1013 cmd->data[1] = devid;
1014 cmd->data[2] = lower_32_bits(inv_address);
1015 cmd->data[3] = upper_32_bits(inv_address);
1016 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1019 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
1020 u64 address, bool size)
1022 memset(cmd, 0, sizeof(*cmd));
1024 address &= ~(0xfffULL);
1026 cmd->data[0] = pasid;
1027 cmd->data[1] = domid;
1028 cmd->data[2] = lower_32_bits(address);
1029 cmd->data[3] = upper_32_bits(address);
1030 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1031 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1033 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1034 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1037 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid,
1038 int qdep, u64 address, bool size)
1040 memset(cmd, 0, sizeof(*cmd));
1042 address &= ~(0xfffULL);
1044 cmd->data[0] = devid;
1045 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
1046 cmd->data[0] |= (qdep & 0xff) << 24;
1047 cmd->data[1] = devid;
1048 cmd->data[1] |= (pasid & 0xff) << 16;
1049 cmd->data[2] = lower_32_bits(address);
1050 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1051 cmd->data[3] = upper_32_bits(address);
1053 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1054 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1057 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
1058 int status, int tag, bool gn)
1060 memset(cmd, 0, sizeof(*cmd));
1062 cmd->data[0] = devid;
1064 cmd->data[1] = pasid;
1065 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1067 cmd->data[3] = tag & 0x1ff;
1068 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1070 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1073 static void build_inv_all(struct iommu_cmd *cmd)
1075 memset(cmd, 0, sizeof(*cmd));
1076 CMD_SET_TYPE(cmd, CMD_INV_ALL);
1079 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1081 memset(cmd, 0, sizeof(*cmd));
1082 cmd->data[0] = devid;
1083 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1087 * Writes the command to the IOMMUs command buffer and informs the
1088 * hardware about the new command.
1090 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
1091 struct iommu_cmd *cmd,
1094 unsigned int count = 0;
1095 u32 left, next_tail;
1097 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1099 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
1102 /* Skip udelay() the first time around */
1104 if (count == LOOP_TIMEOUT) {
1105 pr_err("Command buffer timeout\n");
1112 /* Update head and recheck remaining space */
1113 iommu->cmd_buf_head = readl(iommu->mmio_base +
1114 MMIO_CMD_HEAD_OFFSET);
1119 copy_cmd_to_buffer(iommu, cmd);
1121 /* Do we need to make sure all commands are processed? */
1122 iommu->need_sync = sync;
1127 static int iommu_queue_command_sync(struct amd_iommu *iommu,
1128 struct iommu_cmd *cmd,
1131 unsigned long flags;
1134 raw_spin_lock_irqsave(&iommu->lock, flags);
1135 ret = __iommu_queue_command_sync(iommu, cmd, sync);
1136 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1141 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1143 return iommu_queue_command_sync(iommu, cmd, true);
1147 * This function queues a completion wait command into the command
1148 * buffer of an IOMMU
1150 static int iommu_completion_wait(struct amd_iommu *iommu)
1152 struct iommu_cmd cmd;
1153 unsigned long flags;
1157 if (!iommu->need_sync)
1160 raw_spin_lock_irqsave(&iommu->lock, flags);
1162 data = ++iommu->cmd_sem_val;
1163 build_completion_wait(&cmd, iommu, data);
1165 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1169 ret = wait_on_sem(iommu, data);
1172 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1177 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1179 struct iommu_cmd cmd;
1181 build_inv_dte(&cmd, devid);
1183 return iommu_queue_command(iommu, &cmd);
1186 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
1189 u16 last_bdf = iommu->pci_seg->last_bdf;
1191 for (devid = 0; devid <= last_bdf; ++devid)
1192 iommu_flush_dte(iommu, devid);
1194 iommu_completion_wait(iommu);
1198 * This function uses heavy locking and may disable irqs for some time. But
1199 * this is no issue because it is only called during resume.
1201 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
1204 u16 last_bdf = iommu->pci_seg->last_bdf;
1206 for (dom_id = 0; dom_id <= last_bdf; ++dom_id) {
1207 struct iommu_cmd cmd;
1208 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1210 iommu_queue_command(iommu, &cmd);
1213 iommu_completion_wait(iommu);
1216 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1218 struct iommu_cmd cmd;
1220 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1222 iommu_queue_command(iommu, &cmd);
1224 iommu_completion_wait(iommu);
1227 static void amd_iommu_flush_all(struct amd_iommu *iommu)
1229 struct iommu_cmd cmd;
1231 build_inv_all(&cmd);
1233 iommu_queue_command(iommu, &cmd);
1234 iommu_completion_wait(iommu);
1237 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1239 struct iommu_cmd cmd;
1241 build_inv_irt(&cmd, devid);
1243 iommu_queue_command(iommu, &cmd);
1246 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
1249 u16 last_bdf = iommu->pci_seg->last_bdf;
1251 for (devid = 0; devid <= last_bdf; devid++)
1252 iommu_flush_irt(iommu, devid);
1254 iommu_completion_wait(iommu);
1257 void iommu_flush_all_caches(struct amd_iommu *iommu)
1259 if (iommu_feature(iommu, FEATURE_IA)) {
1260 amd_iommu_flush_all(iommu);
1262 amd_iommu_flush_dte_all(iommu);
1263 amd_iommu_flush_irt_all(iommu);
1264 amd_iommu_flush_tlb_all(iommu);
1269 * Command send function for flushing on-device TLB
1271 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1272 u64 address, size_t size)
1274 struct amd_iommu *iommu;
1275 struct iommu_cmd cmd;
1278 qdep = dev_data->ats.qdep;
1279 iommu = rlookup_amd_iommu(dev_data->dev);
1283 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1285 return iommu_queue_command(iommu, &cmd);
1288 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
1290 struct amd_iommu *iommu = data;
1292 return iommu_flush_dte(iommu, alias);
1296 * Command send function for invalidating a device table entry
1298 static int device_flush_dte(struct iommu_dev_data *dev_data)
1300 struct amd_iommu *iommu;
1301 struct pci_dev *pdev = NULL;
1302 struct amd_iommu_pci_seg *pci_seg;
1306 iommu = rlookup_amd_iommu(dev_data->dev);
1310 if (dev_is_pci(dev_data->dev))
1311 pdev = to_pci_dev(dev_data->dev);
1314 ret = pci_for_each_dma_alias(pdev,
1315 device_flush_dte_alias, iommu);
1317 ret = iommu_flush_dte(iommu, dev_data->devid);
1321 pci_seg = iommu->pci_seg;
1322 alias = pci_seg->alias_table[dev_data->devid];
1323 if (alias != dev_data->devid) {
1324 ret = iommu_flush_dte(iommu, alias);
1329 if (dev_data->ats.enabled)
1330 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1336 * TLB invalidation function which is called from the mapping functions.
1337 * It invalidates a single PTE if the range to flush is within a single
1338 * page. Otherwise it flushes the whole TLB of the IOMMU.
1340 static void __domain_flush_pages(struct protection_domain *domain,
1341 u64 address, size_t size, int pde)
1343 struct iommu_dev_data *dev_data;
1344 struct iommu_cmd cmd;
1347 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1349 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1350 if (!domain->dev_iommu[i])
1354 * Devices of this domain are behind this IOMMU
1355 * We need a TLB flush
1357 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1360 list_for_each_entry(dev_data, &domain->dev_list, list) {
1362 if (!dev_data->ats.enabled)
1365 ret |= device_flush_iotlb(dev_data, address, size);
1371 static void domain_flush_pages(struct protection_domain *domain,
1372 u64 address, size_t size, int pde)
1374 if (likely(!amd_iommu_np_cache)) {
1375 __domain_flush_pages(domain, address, size, pde);
1380 * When NpCache is on, we infer that we run in a VM and use a vIOMMU.
1381 * In such setups it is best to avoid flushes of ranges which are not
1382 * naturally aligned, since it would lead to flushes of unmodified
1383 * PTEs. Such flushes would require the hypervisor to do more work than
1384 * necessary. Therefore, perform repeated flushes of aligned ranges
1385 * until you cover the range. Each iteration flushes the smaller
1386 * between the natural alignment of the address that we flush and the
1387 * greatest naturally aligned region that fits in the range.
1390 int addr_alignment = __ffs(address);
1391 int size_alignment = __fls(size);
1396 * size is always non-zero, but address might be zero, causing
1397 * addr_alignment to be negative. As the casting of the
1398 * argument in __ffs(address) to long might trim the high bits
1399 * of the address on x86-32, cast to long when doing the check.
1401 if (likely((unsigned long)address != 0))
1402 min_alignment = min(addr_alignment, size_alignment);
1404 min_alignment = size_alignment;
1406 flush_size = 1ul << min_alignment;
1408 __domain_flush_pages(domain, address, flush_size, pde);
1409 address += flush_size;
1414 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1415 void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain)
1417 domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1420 void amd_iommu_domain_flush_complete(struct protection_domain *domain)
1424 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1425 if (domain && !domain->dev_iommu[i])
1429 * Devices of this domain are behind this IOMMU
1430 * We need to wait for completion of all commands.
1432 iommu_completion_wait(amd_iommus[i]);
1436 /* Flush the not present cache if it exists */
1437 static void domain_flush_np_cache(struct protection_domain *domain,
1438 dma_addr_t iova, size_t size)
1440 if (unlikely(amd_iommu_np_cache)) {
1441 unsigned long flags;
1443 spin_lock_irqsave(&domain->lock, flags);
1444 domain_flush_pages(domain, iova, size, 1);
1445 amd_iommu_domain_flush_complete(domain);
1446 spin_unlock_irqrestore(&domain->lock, flags);
1452 * This function flushes the DTEs for all devices in domain
1454 static void domain_flush_devices(struct protection_domain *domain)
1456 struct iommu_dev_data *dev_data;
1458 list_for_each_entry(dev_data, &domain->dev_list, list)
1459 device_flush_dte(dev_data);
1462 /****************************************************************************
1464 * The next functions belong to the domain allocation. A domain is
1465 * allocated for every IOMMU as the default domain. If device isolation
1466 * is enabled, every device get its own domain. The most important thing
1467 * about domains is the page table mapping the DMA address space they
1470 ****************************************************************************/
1472 static u16 domain_id_alloc(void)
1476 spin_lock(&pd_bitmap_lock);
1477 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1479 if (id > 0 && id < MAX_DOMAIN_ID)
1480 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1483 spin_unlock(&pd_bitmap_lock);
1488 static void domain_id_free(int id)
1490 spin_lock(&pd_bitmap_lock);
1491 if (id > 0 && id < MAX_DOMAIN_ID)
1492 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1493 spin_unlock(&pd_bitmap_lock);
1496 static void free_gcr3_tbl_level1(u64 *tbl)
1501 for (i = 0; i < 512; ++i) {
1502 if (!(tbl[i] & GCR3_VALID))
1505 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1507 free_page((unsigned long)ptr);
1511 static void free_gcr3_tbl_level2(u64 *tbl)
1516 for (i = 0; i < 512; ++i) {
1517 if (!(tbl[i] & GCR3_VALID))
1520 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1522 free_gcr3_tbl_level1(ptr);
1526 static void free_gcr3_table(struct protection_domain *domain)
1528 if (domain->glx == 2)
1529 free_gcr3_tbl_level2(domain->gcr3_tbl);
1530 else if (domain->glx == 1)
1531 free_gcr3_tbl_level1(domain->gcr3_tbl);
1533 BUG_ON(domain->glx != 0);
1535 free_page((unsigned long)domain->gcr3_tbl);
1538 static void set_dte_entry(struct amd_iommu *iommu, u16 devid,
1539 struct protection_domain *domain, bool ats, bool ppr)
1544 struct dev_table_entry *dev_table = get_dev_table(iommu);
1546 if (domain->iop.mode != PAGE_MODE_NONE)
1547 pte_root = iommu_virt_to_phys(domain->iop.root);
1549 pte_root |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
1550 << DEV_ENTRY_MODE_SHIFT;
1552 pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V;
1555 * When SNP is enabled, Only set TV bit when IOMMU
1556 * page translation is in use.
1558 if (!amd_iommu_snp_en || (domain->id != 0))
1559 pte_root |= DTE_FLAG_TV;
1561 flags = dev_table[devid].data[1];
1564 flags |= DTE_FLAG_IOTLB;
1567 if (iommu_feature(iommu, FEATURE_EPHSUP))
1568 pte_root |= 1ULL << DEV_ENTRY_PPR;
1571 if (domain->flags & PD_IOMMUV2_MASK) {
1572 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
1573 u64 glx = domain->glx;
1576 pte_root |= DTE_FLAG_GV;
1577 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1579 /* First mask out possible old values for GCR3 table */
1580 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1583 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1586 /* Encode GCR3 table into DTE */
1587 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1590 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1593 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1596 if (domain->flags & PD_GIOV_MASK)
1597 pte_root |= DTE_FLAG_GIOV;
1600 flags &= ~DEV_DOMID_MASK;
1601 flags |= domain->id;
1603 old_domid = dev_table[devid].data[1] & DEV_DOMID_MASK;
1604 dev_table[devid].data[1] = flags;
1605 dev_table[devid].data[0] = pte_root;
1608 * A kdump kernel might be replacing a domain ID that was copied from
1609 * the previous kernel--if so, it needs to flush the translation cache
1610 * entries for the old domain ID that is being overwritten
1613 amd_iommu_flush_tlb_domid(iommu, old_domid);
1617 static void clear_dte_entry(struct amd_iommu *iommu, u16 devid)
1619 struct dev_table_entry *dev_table = get_dev_table(iommu);
1621 /* remove entry from the device table seen by the hardware */
1622 dev_table[devid].data[0] = DTE_FLAG_V;
1624 if (!amd_iommu_snp_en)
1625 dev_table[devid].data[0] |= DTE_FLAG_TV;
1627 dev_table[devid].data[1] &= DTE_FLAG_MASK;
1629 amd_iommu_apply_erratum_63(iommu, devid);
1632 static void do_attach(struct iommu_dev_data *dev_data,
1633 struct protection_domain *domain)
1635 struct amd_iommu *iommu;
1638 iommu = rlookup_amd_iommu(dev_data->dev);
1641 ats = dev_data->ats.enabled;
1643 /* Update data structures */
1644 dev_data->domain = domain;
1645 list_add(&dev_data->list, &domain->dev_list);
1647 /* Do reference counting */
1648 domain->dev_iommu[iommu->index] += 1;
1649 domain->dev_cnt += 1;
1651 /* Override supported page sizes */
1652 if (domain->flags & PD_GIOV_MASK)
1653 domain->domain.pgsize_bitmap = AMD_IOMMU_PGSIZES_V2;
1655 /* Update device table */
1656 set_dte_entry(iommu, dev_data->devid, domain,
1657 ats, dev_data->iommu_v2);
1658 clone_aliases(iommu, dev_data->dev);
1660 device_flush_dte(dev_data);
1663 static void do_detach(struct iommu_dev_data *dev_data)
1665 struct protection_domain *domain = dev_data->domain;
1666 struct amd_iommu *iommu;
1668 iommu = rlookup_amd_iommu(dev_data->dev);
1672 /* Update data structures */
1673 dev_data->domain = NULL;
1674 list_del(&dev_data->list);
1675 clear_dte_entry(iommu, dev_data->devid);
1676 clone_aliases(iommu, dev_data->dev);
1678 /* Flush the DTE entry */
1679 device_flush_dte(dev_data);
1682 amd_iommu_domain_flush_tlb_pde(domain);
1684 /* Wait for the flushes to finish */
1685 amd_iommu_domain_flush_complete(domain);
1687 /* decrease reference counters - needs to happen after the flushes */
1688 domain->dev_iommu[iommu->index] -= 1;
1689 domain->dev_cnt -= 1;
1692 static void pdev_iommuv2_disable(struct pci_dev *pdev)
1694 pci_disable_ats(pdev);
1695 pci_disable_pri(pdev);
1696 pci_disable_pasid(pdev);
1699 static int pdev_pri_ats_enable(struct pci_dev *pdev)
1703 /* Only allow access to user-accessible pages */
1704 ret = pci_enable_pasid(pdev, 0);
1708 /* First reset the PRI state of the device */
1709 ret = pci_reset_pri(pdev);
1714 /* FIXME: Hardcode number of outstanding requests for now */
1715 ret = pci_enable_pri(pdev, 32);
1719 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1726 pci_disable_pri(pdev);
1727 pci_disable_pasid(pdev);
1733 * If a device is not yet associated with a domain, this function makes the
1734 * device visible in the domain
1736 static int attach_device(struct device *dev,
1737 struct protection_domain *domain)
1739 struct iommu_dev_data *dev_data;
1740 struct pci_dev *pdev;
1741 unsigned long flags;
1744 spin_lock_irqsave(&domain->lock, flags);
1746 dev_data = dev_iommu_priv_get(dev);
1748 spin_lock(&dev_data->lock);
1751 if (dev_data->domain != NULL)
1754 if (!dev_is_pci(dev))
1755 goto skip_ats_check;
1757 pdev = to_pci_dev(dev);
1758 if (domain->flags & PD_IOMMUV2_MASK) {
1759 struct iommu_domain *def_domain = iommu_get_dma_domain(dev);
1764 * In case of using AMD_IOMMU_V1 page table mode and the device
1765 * is enabling for PPR/ATS support (using v2 table),
1766 * we need to make sure that the domain type is identity map.
1768 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
1769 def_domain->type != IOMMU_DOMAIN_IDENTITY) {
1773 if (dev_data->iommu_v2) {
1774 if (pdev_pri_ats_enable(pdev) != 0)
1777 dev_data->ats.enabled = true;
1778 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1779 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
1781 } else if (amd_iommu_iotlb_sup &&
1782 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1783 dev_data->ats.enabled = true;
1784 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1790 do_attach(dev_data, domain);
1793 * We might boot into a crash-kernel here. The crashed kernel
1794 * left the caches in the IOMMU dirty. So we have to flush
1795 * here to evict all dirty stuff.
1797 amd_iommu_domain_flush_tlb_pde(domain);
1799 amd_iommu_domain_flush_complete(domain);
1802 spin_unlock(&dev_data->lock);
1804 spin_unlock_irqrestore(&domain->lock, flags);
1810 * Removes a device from a protection domain (with devtable_lock held)
1812 static void detach_device(struct device *dev)
1814 struct protection_domain *domain;
1815 struct iommu_dev_data *dev_data;
1816 unsigned long flags;
1818 dev_data = dev_iommu_priv_get(dev);
1819 domain = dev_data->domain;
1821 spin_lock_irqsave(&domain->lock, flags);
1823 spin_lock(&dev_data->lock);
1826 * First check if the device is still attached. It might already
1827 * be detached from its domain because the generic
1828 * iommu_detach_group code detached it and we try again here in
1829 * our alias handling.
1831 if (WARN_ON(!dev_data->domain))
1834 do_detach(dev_data);
1836 if (!dev_is_pci(dev))
1839 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
1840 pdev_iommuv2_disable(to_pci_dev(dev));
1841 else if (dev_data->ats.enabled)
1842 pci_disable_ats(to_pci_dev(dev));
1844 dev_data->ats.enabled = false;
1847 spin_unlock(&dev_data->lock);
1849 spin_unlock_irqrestore(&domain->lock, flags);
1852 static struct iommu_device *amd_iommu_probe_device(struct device *dev)
1854 struct iommu_device *iommu_dev;
1855 struct amd_iommu *iommu;
1858 if (!check_device(dev))
1859 return ERR_PTR(-ENODEV);
1861 iommu = rlookup_amd_iommu(dev);
1863 return ERR_PTR(-ENODEV);
1865 /* Not registered yet? */
1866 if (!iommu->iommu.ops)
1867 return ERR_PTR(-ENODEV);
1869 if (dev_iommu_priv_get(dev))
1870 return &iommu->iommu;
1872 ret = iommu_init_device(iommu, dev);
1874 if (ret != -ENOTSUPP)
1875 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
1876 iommu_dev = ERR_PTR(ret);
1877 iommu_ignore_device(iommu, dev);
1879 amd_iommu_set_pci_msi_domain(dev, iommu);
1880 iommu_dev = &iommu->iommu;
1883 iommu_completion_wait(iommu);
1888 static void amd_iommu_probe_finalize(struct device *dev)
1890 /* Domains are initialized for this device - have a look what we ended up with */
1891 set_dma_ops(dev, NULL);
1892 iommu_setup_dma_ops(dev, 0, U64_MAX);
1895 static void amd_iommu_release_device(struct device *dev)
1897 struct amd_iommu *iommu;
1899 if (!check_device(dev))
1902 iommu = rlookup_amd_iommu(dev);
1906 amd_iommu_uninit_device(dev);
1907 iommu_completion_wait(iommu);
1910 static struct iommu_group *amd_iommu_device_group(struct device *dev)
1912 if (dev_is_pci(dev))
1913 return pci_device_group(dev);
1915 return acpihid_device_group(dev);
1918 /*****************************************************************************
1920 * The next functions belong to the dma_ops mapping/unmapping code.
1922 *****************************************************************************/
1924 static void update_device_table(struct protection_domain *domain)
1926 struct iommu_dev_data *dev_data;
1928 list_for_each_entry(dev_data, &domain->dev_list, list) {
1929 struct amd_iommu *iommu = rlookup_amd_iommu(dev_data->dev);
1933 set_dte_entry(iommu, dev_data->devid, domain,
1934 dev_data->ats.enabled, dev_data->iommu_v2);
1935 clone_aliases(iommu, dev_data->dev);
1939 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
1941 update_device_table(domain);
1942 domain_flush_devices(domain);
1945 void amd_iommu_domain_update(struct protection_domain *domain)
1947 /* Update device table */
1948 amd_iommu_update_and_flush_device_table(domain);
1950 /* Flush domain TLB(s) and wait for completion */
1951 amd_iommu_domain_flush_tlb_pde(domain);
1952 amd_iommu_domain_flush_complete(domain);
1955 /*****************************************************************************
1957 * The following functions belong to the exported interface of AMD IOMMU
1959 * This interface allows access to lower level functions of the IOMMU
1960 * like protection domain handling and assignement of devices to domains
1961 * which is not possible with the dma_ops interface.
1963 *****************************************************************************/
1965 static void cleanup_domain(struct protection_domain *domain)
1967 struct iommu_dev_data *entry;
1968 unsigned long flags;
1970 spin_lock_irqsave(&domain->lock, flags);
1972 while (!list_empty(&domain->dev_list)) {
1973 entry = list_first_entry(&domain->dev_list,
1974 struct iommu_dev_data, list);
1975 BUG_ON(!entry->domain);
1979 spin_unlock_irqrestore(&domain->lock, flags);
1982 static void protection_domain_free(struct protection_domain *domain)
1987 if (domain->iop.pgtbl_cfg.tlb)
1988 free_io_pgtable_ops(&domain->iop.iop.ops);
1991 domain_id_free(domain->id);
1996 static int protection_domain_init_v1(struct protection_domain *domain, int mode)
1998 u64 *pt_root = NULL;
2000 BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
2002 spin_lock_init(&domain->lock);
2003 domain->id = domain_id_alloc();
2006 INIT_LIST_HEAD(&domain->dev_list);
2008 if (mode != PAGE_MODE_NONE) {
2009 pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2011 domain_id_free(domain->id);
2016 amd_iommu_domain_set_pgtable(domain, pt_root, mode);
2021 static int protection_domain_init_v2(struct protection_domain *domain)
2023 spin_lock_init(&domain->lock);
2024 domain->id = domain_id_alloc();
2027 INIT_LIST_HEAD(&domain->dev_list);
2029 domain->flags |= PD_GIOV_MASK;
2031 if (domain_enable_v2(domain, 1)) {
2032 domain_id_free(domain->id);
2039 static struct protection_domain *protection_domain_alloc(unsigned int type)
2041 struct io_pgtable_ops *pgtbl_ops;
2042 struct protection_domain *domain;
2043 int pgtable = amd_iommu_pgtable;
2044 int mode = DEFAULT_PGTABLE_LEVEL;
2047 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2052 * Force IOMMU v1 page table when iommu=pt and
2053 * when allocating domain for pass-through devices.
2055 if (type == IOMMU_DOMAIN_IDENTITY) {
2056 pgtable = AMD_IOMMU_V1;
2057 mode = PAGE_MODE_NONE;
2058 } else if (type == IOMMU_DOMAIN_UNMANAGED) {
2059 pgtable = AMD_IOMMU_V1;
2064 ret = protection_domain_init_v1(domain, mode);
2067 ret = protection_domain_init_v2(domain);
2076 pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
2078 domain_id_free(domain->id);
2088 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2090 struct protection_domain *domain;
2093 * Since DTE[Mode]=0 is prohibited on SNP-enabled system,
2094 * default to use IOMMU_DOMAIN_DMA[_FQ].
2096 if (amd_iommu_snp_en && (type == IOMMU_DOMAIN_IDENTITY))
2099 domain = protection_domain_alloc(type);
2103 domain->domain.geometry.aperture_start = 0;
2104 domain->domain.geometry.aperture_end = ~0ULL;
2105 domain->domain.geometry.force_aperture = true;
2107 return &domain->domain;
2110 static void amd_iommu_domain_free(struct iommu_domain *dom)
2112 struct protection_domain *domain;
2114 domain = to_pdomain(dom);
2116 if (domain->dev_cnt > 0)
2117 cleanup_domain(domain);
2119 BUG_ON(domain->dev_cnt != 0);
2124 if (domain->flags & PD_IOMMUV2_MASK)
2125 free_gcr3_table(domain);
2127 protection_domain_free(domain);
2130 static void amd_iommu_detach_device(struct iommu_domain *dom,
2133 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2134 struct amd_iommu *iommu;
2136 if (!check_device(dev))
2139 if (dev_data->domain != NULL)
2142 iommu = rlookup_amd_iommu(dev);
2146 #ifdef CONFIG_IRQ_REMAP
2147 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2148 (dom->type == IOMMU_DOMAIN_UNMANAGED))
2149 dev_data->use_vapic = 0;
2152 iommu_completion_wait(iommu);
2155 static int amd_iommu_attach_device(struct iommu_domain *dom,
2158 struct protection_domain *domain = to_pdomain(dom);
2159 struct iommu_dev_data *dev_data;
2160 struct amd_iommu *iommu;
2163 if (!check_device(dev))
2166 dev_data = dev_iommu_priv_get(dev);
2167 dev_data->defer_attach = false;
2169 iommu = rlookup_amd_iommu(dev);
2173 if (dev_data->domain)
2176 ret = attach_device(dev, domain);
2178 #ifdef CONFIG_IRQ_REMAP
2179 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
2180 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
2181 dev_data->use_vapic = 1;
2183 dev_data->use_vapic = 0;
2187 iommu_completion_wait(iommu);
2192 static void amd_iommu_iotlb_sync_map(struct iommu_domain *dom,
2193 unsigned long iova, size_t size)
2195 struct protection_domain *domain = to_pdomain(dom);
2196 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2199 domain_flush_np_cache(domain, iova, size);
2202 static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
2203 phys_addr_t paddr, size_t pgsize, size_t pgcount,
2204 int iommu_prot, gfp_t gfp, size_t *mapped)
2206 struct protection_domain *domain = to_pdomain(dom);
2207 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2211 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2212 (domain->iop.mode == PAGE_MODE_NONE))
2215 if (iommu_prot & IOMMU_READ)
2216 prot |= IOMMU_PROT_IR;
2217 if (iommu_prot & IOMMU_WRITE)
2218 prot |= IOMMU_PROT_IW;
2220 if (ops->map_pages) {
2221 ret = ops->map_pages(ops, iova, paddr, pgsize,
2222 pgcount, prot, gfp, mapped);
2228 static void amd_iommu_iotlb_gather_add_page(struct iommu_domain *domain,
2229 struct iommu_iotlb_gather *gather,
2230 unsigned long iova, size_t size)
2233 * AMD's IOMMU can flush as many pages as necessary in a single flush.
2234 * Unless we run in a virtual machine, which can be inferred according
2235 * to whether "non-present cache" is on, it is probably best to prefer
2236 * (potentially) too extensive TLB flushing (i.e., more misses) over
2237 * mutliple TLB flushes (i.e., more flushes). For virtual machines the
2238 * hypervisor needs to synchronize the host IOMMU PTEs with those of
2239 * the guest, and the trade-off is different: unnecessary TLB flushes
2240 * should be avoided.
2242 if (amd_iommu_np_cache &&
2243 iommu_iotlb_gather_is_disjoint(gather, iova, size))
2244 iommu_iotlb_sync(domain, gather);
2246 iommu_iotlb_gather_add_range(gather, iova, size);
2249 static size_t amd_iommu_unmap_pages(struct iommu_domain *dom, unsigned long iova,
2250 size_t pgsize, size_t pgcount,
2251 struct iommu_iotlb_gather *gather)
2253 struct protection_domain *domain = to_pdomain(dom);
2254 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2257 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2258 (domain->iop.mode == PAGE_MODE_NONE))
2261 r = (ops->unmap_pages) ? ops->unmap_pages(ops, iova, pgsize, pgcount, NULL) : 0;
2264 amd_iommu_iotlb_gather_add_page(dom, gather, iova, r);
2269 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2272 struct protection_domain *domain = to_pdomain(dom);
2273 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2275 return ops->iova_to_phys(ops, iova);
2278 static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap)
2281 case IOMMU_CAP_CACHE_COHERENCY:
2283 case IOMMU_CAP_INTR_REMAP:
2284 return (irq_remapping_enabled == 1);
2285 case IOMMU_CAP_NOEXEC:
2287 case IOMMU_CAP_PRE_BOOT_PROTECTION:
2288 return amdr_ivrs_remap_support;
2296 static void amd_iommu_get_resv_regions(struct device *dev,
2297 struct list_head *head)
2299 struct iommu_resv_region *region;
2300 struct unity_map_entry *entry;
2301 struct amd_iommu *iommu;
2302 struct amd_iommu_pci_seg *pci_seg;
2305 sbdf = get_device_sbdf_id(dev);
2309 devid = PCI_SBDF_TO_DEVID(sbdf);
2310 iommu = rlookup_amd_iommu(dev);
2313 pci_seg = iommu->pci_seg;
2315 list_for_each_entry(entry, &pci_seg->unity_map, list) {
2319 if (devid < entry->devid_start || devid > entry->devid_end)
2322 type = IOMMU_RESV_DIRECT;
2323 length = entry->address_end - entry->address_start;
2324 if (entry->prot & IOMMU_PROT_IR)
2326 if (entry->prot & IOMMU_PROT_IW)
2327 prot |= IOMMU_WRITE;
2328 if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
2329 /* Exclusion range */
2330 type = IOMMU_RESV_RESERVED;
2332 region = iommu_alloc_resv_region(entry->address_start,
2333 length, prot, type);
2335 dev_err(dev, "Out of memory allocating dm-regions\n");
2338 list_add_tail(®ion->list, head);
2341 region = iommu_alloc_resv_region(MSI_RANGE_START,
2342 MSI_RANGE_END - MSI_RANGE_START + 1,
2346 list_add_tail(®ion->list, head);
2348 region = iommu_alloc_resv_region(HT_RANGE_START,
2349 HT_RANGE_END - HT_RANGE_START + 1,
2350 0, IOMMU_RESV_RESERVED);
2353 list_add_tail(®ion->list, head);
2356 bool amd_iommu_is_attach_deferred(struct device *dev)
2358 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2360 return dev_data->defer_attach;
2362 EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred);
2364 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
2366 struct protection_domain *dom = to_pdomain(domain);
2367 unsigned long flags;
2369 spin_lock_irqsave(&dom->lock, flags);
2370 amd_iommu_domain_flush_tlb_pde(dom);
2371 amd_iommu_domain_flush_complete(dom);
2372 spin_unlock_irqrestore(&dom->lock, flags);
2375 static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
2376 struct iommu_iotlb_gather *gather)
2378 struct protection_domain *dom = to_pdomain(domain);
2379 unsigned long flags;
2381 spin_lock_irqsave(&dom->lock, flags);
2382 domain_flush_pages(dom, gather->start, gather->end - gather->start, 1);
2383 amd_iommu_domain_flush_complete(dom);
2384 spin_unlock_irqrestore(&dom->lock, flags);
2387 static int amd_iommu_def_domain_type(struct device *dev)
2389 struct iommu_dev_data *dev_data;
2391 dev_data = dev_iommu_priv_get(dev);
2396 * Do not identity map IOMMUv2 capable devices when memory encryption is
2397 * active, because some of those devices (AMD GPUs) don't have the
2398 * encryption bit in their DMA-mask and require remapping.
2400 if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT) && dev_data->iommu_v2)
2401 return IOMMU_DOMAIN_IDENTITY;
2406 static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
2408 /* IOMMU_PTE_FC is always set */
2412 const struct iommu_ops amd_iommu_ops = {
2413 .capable = amd_iommu_capable,
2414 .domain_alloc = amd_iommu_domain_alloc,
2415 .probe_device = amd_iommu_probe_device,
2416 .release_device = amd_iommu_release_device,
2417 .probe_finalize = amd_iommu_probe_finalize,
2418 .device_group = amd_iommu_device_group,
2419 .get_resv_regions = amd_iommu_get_resv_regions,
2420 .is_attach_deferred = amd_iommu_is_attach_deferred,
2421 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
2422 .def_domain_type = amd_iommu_def_domain_type,
2423 .default_domain_ops = &(const struct iommu_domain_ops) {
2424 .attach_dev = amd_iommu_attach_device,
2425 .detach_dev = amd_iommu_detach_device,
2426 .map_pages = amd_iommu_map_pages,
2427 .unmap_pages = amd_iommu_unmap_pages,
2428 .iotlb_sync_map = amd_iommu_iotlb_sync_map,
2429 .iova_to_phys = amd_iommu_iova_to_phys,
2430 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2431 .iotlb_sync = amd_iommu_iotlb_sync,
2432 .free = amd_iommu_domain_free,
2433 .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
2437 /*****************************************************************************
2439 * The next functions do a basic initialization of IOMMU for pass through
2442 * In passthrough mode the IOMMU is initialized and enabled but not used for
2443 * DMA-API translation.
2445 *****************************************************************************/
2447 /* IOMMUv2 specific functions */
2448 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2450 return atomic_notifier_chain_register(&ppr_notifier, nb);
2452 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2454 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2456 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2458 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
2460 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2462 struct protection_domain *domain = to_pdomain(dom);
2463 unsigned long flags;
2465 spin_lock_irqsave(&domain->lock, flags);
2467 if (domain->iop.pgtbl_cfg.tlb)
2468 free_io_pgtable_ops(&domain->iop.iop.ops);
2470 spin_unlock_irqrestore(&domain->lock, flags);
2472 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
2474 /* Note: This function expects iommu_domain->lock to be held prior calling the function. */
2475 static int domain_enable_v2(struct protection_domain *domain, int pasids)
2479 /* Number of GCR3 table levels required */
2480 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
2483 if (levels > amd_iommu_max_glx_val)
2486 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
2487 if (domain->gcr3_tbl == NULL)
2490 domain->glx = levels;
2491 domain->flags |= PD_IOMMUV2_MASK;
2493 amd_iommu_domain_update(domain);
2498 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
2500 struct protection_domain *pdom = to_pdomain(dom);
2501 unsigned long flags;
2504 spin_lock_irqsave(&pdom->lock, flags);
2507 * Save us all sanity checks whether devices already in the
2508 * domain support IOMMUv2. Just force that the domain has no
2509 * devices attached when it is switched into IOMMUv2 mode.
2512 if (pdom->dev_cnt > 0 || pdom->flags & PD_IOMMUV2_MASK)
2515 if (!pdom->gcr3_tbl)
2516 ret = domain_enable_v2(pdom, pasids);
2519 spin_unlock_irqrestore(&pdom->lock, flags);
2522 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
2524 static int __flush_pasid(struct protection_domain *domain, u32 pasid,
2525 u64 address, bool size)
2527 struct iommu_dev_data *dev_data;
2528 struct iommu_cmd cmd;
2531 if (!(domain->flags & PD_IOMMUV2_MASK))
2534 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
2537 * IOMMU TLB needs to be flushed before Device TLB to
2538 * prevent device TLB refill from IOMMU TLB
2540 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
2541 if (domain->dev_iommu[i] == 0)
2544 ret = iommu_queue_command(amd_iommus[i], &cmd);
2549 /* Wait until IOMMU TLB flushes are complete */
2550 amd_iommu_domain_flush_complete(domain);
2552 /* Now flush device TLBs */
2553 list_for_each_entry(dev_data, &domain->dev_list, list) {
2554 struct amd_iommu *iommu;
2558 There might be non-IOMMUv2 capable devices in an IOMMUv2
2561 if (!dev_data->ats.enabled)
2564 qdep = dev_data->ats.qdep;
2565 iommu = rlookup_amd_iommu(dev_data->dev);
2568 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
2569 qdep, address, size);
2571 ret = iommu_queue_command(iommu, &cmd);
2576 /* Wait until all device TLBs are flushed */
2577 amd_iommu_domain_flush_complete(domain);
2586 static int __amd_iommu_flush_page(struct protection_domain *domain, u32 pasid,
2589 return __flush_pasid(domain, pasid, address, false);
2592 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid,
2595 struct protection_domain *domain = to_pdomain(dom);
2596 unsigned long flags;
2599 spin_lock_irqsave(&domain->lock, flags);
2600 ret = __amd_iommu_flush_page(domain, pasid, address);
2601 spin_unlock_irqrestore(&domain->lock, flags);
2605 EXPORT_SYMBOL(amd_iommu_flush_page);
2607 static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid)
2609 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
2613 int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid)
2615 struct protection_domain *domain = to_pdomain(dom);
2616 unsigned long flags;
2619 spin_lock_irqsave(&domain->lock, flags);
2620 ret = __amd_iommu_flush_tlb(domain, pasid);
2621 spin_unlock_irqrestore(&domain->lock, flags);
2625 EXPORT_SYMBOL(amd_iommu_flush_tlb);
2627 static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc)
2634 index = (pasid >> (9 * level)) & 0x1ff;
2640 if (!(*pte & GCR3_VALID)) {
2644 root = (void *)get_zeroed_page(GFP_ATOMIC);
2648 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
2651 root = iommu_phys_to_virt(*pte & PAGE_MASK);
2659 static int __set_gcr3(struct protection_domain *domain, u32 pasid,
2664 if (domain->iop.mode != PAGE_MODE_NONE)
2667 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
2671 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
2673 return __amd_iommu_flush_tlb(domain, pasid);
2676 static int __clear_gcr3(struct protection_domain *domain, u32 pasid)
2680 if (domain->iop.mode != PAGE_MODE_NONE)
2683 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
2689 return __amd_iommu_flush_tlb(domain, pasid);
2692 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
2695 struct protection_domain *domain = to_pdomain(dom);
2696 unsigned long flags;
2699 spin_lock_irqsave(&domain->lock, flags);
2700 ret = __set_gcr3(domain, pasid, cr3);
2701 spin_unlock_irqrestore(&domain->lock, flags);
2705 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
2707 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid)
2709 struct protection_domain *domain = to_pdomain(dom);
2710 unsigned long flags;
2713 spin_lock_irqsave(&domain->lock, flags);
2714 ret = __clear_gcr3(domain, pasid);
2715 spin_unlock_irqrestore(&domain->lock, flags);
2719 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
2721 int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
2722 int status, int tag)
2724 struct iommu_dev_data *dev_data;
2725 struct amd_iommu *iommu;
2726 struct iommu_cmd cmd;
2728 dev_data = dev_iommu_priv_get(&pdev->dev);
2729 iommu = rlookup_amd_iommu(&pdev->dev);
2733 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
2734 tag, dev_data->pri_tlp);
2736 return iommu_queue_command(iommu, &cmd);
2738 EXPORT_SYMBOL(amd_iommu_complete_ppr);
2740 int amd_iommu_device_info(struct pci_dev *pdev,
2741 struct amd_iommu_device_info *info)
2746 if (pdev == NULL || info == NULL)
2749 if (!amd_iommu_v2_supported())
2752 memset(info, 0, sizeof(*info));
2754 if (pci_ats_supported(pdev))
2755 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
2757 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
2759 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
2761 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
2765 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
2766 max_pasids = min(max_pasids, (1 << 20));
2768 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
2769 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
2771 features = pci_pasid_features(pdev);
2772 if (features & PCI_PASID_CAP_EXEC)
2773 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
2774 if (features & PCI_PASID_CAP_PRIV)
2775 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
2780 EXPORT_SYMBOL(amd_iommu_device_info);
2782 #ifdef CONFIG_IRQ_REMAP
2784 /*****************************************************************************
2786 * Interrupt Remapping Implementation
2788 *****************************************************************************/
2790 static struct irq_chip amd_ir_chip;
2791 static DEFINE_SPINLOCK(iommu_table_lock);
2793 static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid,
2794 struct irq_remap_table *table)
2797 struct dev_table_entry *dev_table = get_dev_table(iommu);
2799 dte = dev_table[devid].data[2];
2800 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
2801 dte |= iommu_virt_to_phys(table->table);
2802 dte |= DTE_IRQ_REMAP_INTCTL;
2803 dte |= DTE_INTTABLEN;
2804 dte |= DTE_IRQ_REMAP_ENABLE;
2806 dev_table[devid].data[2] = dte;
2809 static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 devid)
2811 struct irq_remap_table *table;
2812 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
2814 if (WARN_ONCE(!pci_seg->rlookup_table[devid],
2815 "%s: no iommu for devid %x:%x\n",
2816 __func__, pci_seg->id, devid))
2819 table = pci_seg->irq_lookup_table[devid];
2820 if (WARN_ONCE(!table, "%s: no table for devid %x:%x\n",
2821 __func__, pci_seg->id, devid))
2827 static struct irq_remap_table *__alloc_irq_table(void)
2829 struct irq_remap_table *table;
2831 table = kzalloc(sizeof(*table), GFP_KERNEL);
2835 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
2836 if (!table->table) {
2840 raw_spin_lock_init(&table->lock);
2842 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2843 memset(table->table, 0,
2844 MAX_IRQS_PER_TABLE * sizeof(u32));
2846 memset(table->table, 0,
2847 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
2851 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
2852 struct irq_remap_table *table)
2854 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
2856 pci_seg->irq_lookup_table[devid] = table;
2857 set_dte_irq_entry(iommu, devid, table);
2858 iommu_flush_dte(iommu, devid);
2861 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
2864 struct irq_remap_table *table = data;
2865 struct amd_iommu_pci_seg *pci_seg;
2866 struct amd_iommu *iommu = rlookup_amd_iommu(&pdev->dev);
2871 pci_seg = iommu->pci_seg;
2872 pci_seg->irq_lookup_table[alias] = table;
2873 set_dte_irq_entry(iommu, alias, table);
2874 iommu_flush_dte(pci_seg->rlookup_table[alias], alias);
2879 static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu,
2880 u16 devid, struct pci_dev *pdev)
2882 struct irq_remap_table *table = NULL;
2883 struct irq_remap_table *new_table = NULL;
2884 struct amd_iommu_pci_seg *pci_seg;
2885 unsigned long flags;
2888 spin_lock_irqsave(&iommu_table_lock, flags);
2890 pci_seg = iommu->pci_seg;
2891 table = pci_seg->irq_lookup_table[devid];
2895 alias = pci_seg->alias_table[devid];
2896 table = pci_seg->irq_lookup_table[alias];
2898 set_remap_table_entry(iommu, devid, table);
2901 spin_unlock_irqrestore(&iommu_table_lock, flags);
2903 /* Nothing there yet, allocate new irq remapping table */
2904 new_table = __alloc_irq_table();
2908 spin_lock_irqsave(&iommu_table_lock, flags);
2910 table = pci_seg->irq_lookup_table[devid];
2914 table = pci_seg->irq_lookup_table[alias];
2916 set_remap_table_entry(iommu, devid, table);
2924 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
2927 set_remap_table_entry(iommu, devid, table);
2930 set_remap_table_entry(iommu, alias, table);
2933 iommu_completion_wait(iommu);
2936 spin_unlock_irqrestore(&iommu_table_lock, flags);
2939 kmem_cache_free(amd_iommu_irq_cache, new_table->table);
2945 static int alloc_irq_index(struct amd_iommu *iommu, u16 devid, int count,
2946 bool align, struct pci_dev *pdev)
2948 struct irq_remap_table *table;
2949 int index, c, alignment = 1;
2950 unsigned long flags;
2952 table = alloc_irq_table(iommu, devid, pdev);
2957 alignment = roundup_pow_of_two(count);
2959 raw_spin_lock_irqsave(&table->lock, flags);
2961 /* Scan table for free entries */
2962 for (index = ALIGN(table->min_index, alignment), c = 0;
2963 index < MAX_IRQS_PER_TABLE;) {
2964 if (!iommu->irte_ops->is_allocated(table, index)) {
2968 index = ALIGN(index + 1, alignment);
2974 iommu->irte_ops->set_allocated(table, index - c + 1);
2986 raw_spin_unlock_irqrestore(&table->lock, flags);
2991 static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index,
2992 struct irte_ga *irte, struct amd_ir_data *data)
2995 struct irq_remap_table *table;
2996 unsigned long flags;
2997 struct irte_ga *entry;
2999 table = get_irq_table(iommu, devid);
3003 raw_spin_lock_irqsave(&table->lock, flags);
3005 entry = (struct irte_ga *)table->table;
3006 entry = &entry[index];
3008 ret = cmpxchg_double(&entry->lo.val, &entry->hi.val,
3009 entry->lo.val, entry->hi.val,
3010 irte->lo.val, irte->hi.val);
3012 * We use cmpxchg16 to atomically update the 128-bit IRTE,
3013 * and it cannot be updated by the hardware or other processors
3014 * behind us, so the return value of cmpxchg16 should be the
3015 * same as the old value.
3022 raw_spin_unlock_irqrestore(&table->lock, flags);
3024 iommu_flush_irt(iommu, devid);
3025 iommu_completion_wait(iommu);
3030 static int modify_irte(struct amd_iommu *iommu,
3031 u16 devid, int index, union irte *irte)
3033 struct irq_remap_table *table;
3034 unsigned long flags;
3036 table = get_irq_table(iommu, devid);
3040 raw_spin_lock_irqsave(&table->lock, flags);
3041 table->table[index] = irte->val;
3042 raw_spin_unlock_irqrestore(&table->lock, flags);
3044 iommu_flush_irt(iommu, devid);
3045 iommu_completion_wait(iommu);
3050 static void free_irte(struct amd_iommu *iommu, u16 devid, int index)
3052 struct irq_remap_table *table;
3053 unsigned long flags;
3055 table = get_irq_table(iommu, devid);
3059 raw_spin_lock_irqsave(&table->lock, flags);
3060 iommu->irte_ops->clear_allocated(table, index);
3061 raw_spin_unlock_irqrestore(&table->lock, flags);
3063 iommu_flush_irt(iommu, devid);
3064 iommu_completion_wait(iommu);
3067 static void irte_prepare(void *entry,
3068 u32 delivery_mode, bool dest_mode,
3069 u8 vector, u32 dest_apicid, int devid)
3071 union irte *irte = (union irte *) entry;
3074 irte->fields.vector = vector;
3075 irte->fields.int_type = delivery_mode;
3076 irte->fields.destination = dest_apicid;
3077 irte->fields.dm = dest_mode;
3078 irte->fields.valid = 1;
3081 static void irte_ga_prepare(void *entry,
3082 u32 delivery_mode, bool dest_mode,
3083 u8 vector, u32 dest_apicid, int devid)
3085 struct irte_ga *irte = (struct irte_ga *) entry;
3089 irte->lo.fields_remap.int_type = delivery_mode;
3090 irte->lo.fields_remap.dm = dest_mode;
3091 irte->hi.fields.vector = vector;
3092 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
3093 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
3094 irte->lo.fields_remap.valid = 1;
3097 static void irte_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3099 union irte *irte = (union irte *) entry;
3101 irte->fields.valid = 1;
3102 modify_irte(iommu, devid, index, irte);
3105 static void irte_ga_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3107 struct irte_ga *irte = (struct irte_ga *) entry;
3109 irte->lo.fields_remap.valid = 1;
3110 modify_irte_ga(iommu, devid, index, irte, NULL);
3113 static void irte_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3115 union irte *irte = (union irte *) entry;
3117 irte->fields.valid = 0;
3118 modify_irte(iommu, devid, index, irte);
3121 static void irte_ga_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3123 struct irte_ga *irte = (struct irte_ga *) entry;
3125 irte->lo.fields_remap.valid = 0;
3126 modify_irte_ga(iommu, devid, index, irte, NULL);
3129 static void irte_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
3130 u8 vector, u32 dest_apicid)
3132 union irte *irte = (union irte *) entry;
3134 irte->fields.vector = vector;
3135 irte->fields.destination = dest_apicid;
3136 modify_irte(iommu, devid, index, irte);
3139 static void irte_ga_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
3140 u8 vector, u32 dest_apicid)
3142 struct irte_ga *irte = (struct irte_ga *) entry;
3144 if (!irte->lo.fields_remap.guest_mode) {
3145 irte->hi.fields.vector = vector;
3146 irte->lo.fields_remap.destination =
3147 APICID_TO_IRTE_DEST_LO(dest_apicid);
3148 irte->hi.fields.destination =
3149 APICID_TO_IRTE_DEST_HI(dest_apicid);
3150 modify_irte_ga(iommu, devid, index, irte, NULL);
3154 #define IRTE_ALLOCATED (~1U)
3155 static void irte_set_allocated(struct irq_remap_table *table, int index)
3157 table->table[index] = IRTE_ALLOCATED;
3160 static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3162 struct irte_ga *ptr = (struct irte_ga *)table->table;
3163 struct irte_ga *irte = &ptr[index];
3165 memset(&irte->lo.val, 0, sizeof(u64));
3166 memset(&irte->hi.val, 0, sizeof(u64));
3167 irte->hi.fields.vector = 0xff;
3170 static bool irte_is_allocated(struct irq_remap_table *table, int index)
3172 union irte *ptr = (union irte *)table->table;
3173 union irte *irte = &ptr[index];
3175 return irte->val != 0;
3178 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3180 struct irte_ga *ptr = (struct irte_ga *)table->table;
3181 struct irte_ga *irte = &ptr[index];
3183 return irte->hi.fields.vector != 0;
3186 static void irte_clear_allocated(struct irq_remap_table *table, int index)
3188 table->table[index] = 0;
3191 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3193 struct irte_ga *ptr = (struct irte_ga *)table->table;
3194 struct irte_ga *irte = &ptr[index];
3196 memset(&irte->lo.val, 0, sizeof(u64));
3197 memset(&irte->hi.val, 0, sizeof(u64));
3200 static int get_devid(struct irq_alloc_info *info)
3202 switch (info->type) {
3203 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3204 return get_ioapic_devid(info->devid);
3205 case X86_IRQ_ALLOC_TYPE_HPET:
3206 return get_hpet_devid(info->devid);
3207 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3208 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3209 return get_device_sbdf_id(msi_desc_to_dev(info->desc));
3216 struct irq_remap_ops amd_iommu_irq_ops = {
3217 .prepare = amd_iommu_prepare,
3218 .enable = amd_iommu_enable,
3219 .disable = amd_iommu_disable,
3220 .reenable = amd_iommu_reenable,
3221 .enable_faulting = amd_iommu_enable_faulting,
3224 static void fill_msi_msg(struct msi_msg *msg, u32 index)
3227 msg->address_lo = 0;
3228 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
3229 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
3232 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3233 struct irq_cfg *irq_cfg,
3234 struct irq_alloc_info *info,
3235 int devid, int index, int sub_handle)
3237 struct irq_2_irte *irte_info = &data->irq_2_irte;
3238 struct amd_iommu *iommu = data->iommu;
3243 data->irq_2_irte.devid = devid;
3244 data->irq_2_irte.index = index + sub_handle;
3245 iommu->irte_ops->prepare(data->entry, apic->delivery_mode,
3246 apic->dest_mode_logical, irq_cfg->vector,
3247 irq_cfg->dest_apicid, devid);
3249 switch (info->type) {
3250 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3251 case X86_IRQ_ALLOC_TYPE_HPET:
3252 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3253 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3254 fill_msi_msg(&data->msi_entry, irte_info->index);
3263 struct amd_irte_ops irte_32_ops = {
3264 .prepare = irte_prepare,
3265 .activate = irte_activate,
3266 .deactivate = irte_deactivate,
3267 .set_affinity = irte_set_affinity,
3268 .set_allocated = irte_set_allocated,
3269 .is_allocated = irte_is_allocated,
3270 .clear_allocated = irte_clear_allocated,
3273 struct amd_irte_ops irte_128_ops = {
3274 .prepare = irte_ga_prepare,
3275 .activate = irte_ga_activate,
3276 .deactivate = irte_ga_deactivate,
3277 .set_affinity = irte_ga_set_affinity,
3278 .set_allocated = irte_ga_set_allocated,
3279 .is_allocated = irte_ga_is_allocated,
3280 .clear_allocated = irte_ga_clear_allocated,
3283 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3284 unsigned int nr_irqs, void *arg)
3286 struct irq_alloc_info *info = arg;
3287 struct irq_data *irq_data;
3288 struct amd_ir_data *data = NULL;
3289 struct amd_iommu *iommu;
3290 struct irq_cfg *cfg;
3291 int i, ret, devid, seg, sbdf;
3296 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI &&
3297 info->type != X86_IRQ_ALLOC_TYPE_PCI_MSIX)
3301 * With IRQ remapping enabled, don't need contiguous CPU vectors
3302 * to support multiple MSI interrupts.
3304 if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
3305 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3307 sbdf = get_devid(info);
3311 seg = PCI_SBDF_TO_SEGID(sbdf);
3312 devid = PCI_SBDF_TO_DEVID(sbdf);
3313 iommu = __rlookup_amd_iommu(seg, devid);
3317 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3321 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3322 struct irq_remap_table *table;
3324 table = alloc_irq_table(iommu, devid, NULL);
3326 if (!table->min_index) {
3328 * Keep the first 32 indexes free for IOAPIC
3331 table->min_index = 32;
3332 for (i = 0; i < 32; ++i)
3333 iommu->irte_ops->set_allocated(table, i);
3335 WARN_ON(table->min_index != 32);
3336 index = info->ioapic.pin;
3340 } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
3341 info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
3342 bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
3344 index = alloc_irq_index(iommu, devid, nr_irqs, align,
3345 msi_desc_to_pci_dev(info->desc));
3347 index = alloc_irq_index(iommu, devid, nr_irqs, false, NULL);
3351 pr_warn("Failed to allocate IRTE\n");
3353 goto out_free_parent;
3356 for (i = 0; i < nr_irqs; i++) {
3357 irq_data = irq_domain_get_irq_data(domain, virq + i);
3358 cfg = irq_data ? irqd_cfg(irq_data) : NULL;
3365 data = kzalloc(sizeof(*data), GFP_KERNEL);
3369 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3370 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
3372 data->entry = kzalloc(sizeof(struct irte_ga),
3379 data->iommu = iommu;
3380 irq_data->hwirq = (devid << 16) + i;
3381 irq_data->chip_data = data;
3382 irq_data->chip = &amd_ir_chip;
3383 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3384 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3390 for (i--; i >= 0; i--) {
3391 irq_data = irq_domain_get_irq_data(domain, virq + i);
3393 kfree(irq_data->chip_data);
3395 for (i = 0; i < nr_irqs; i++)
3396 free_irte(iommu, devid, index + i);
3398 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3402 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3403 unsigned int nr_irqs)
3405 struct irq_2_irte *irte_info;
3406 struct irq_data *irq_data;
3407 struct amd_ir_data *data;
3410 for (i = 0; i < nr_irqs; i++) {
3411 irq_data = irq_domain_get_irq_data(domain, virq + i);
3412 if (irq_data && irq_data->chip_data) {
3413 data = irq_data->chip_data;
3414 irte_info = &data->irq_2_irte;
3415 free_irte(data->iommu, irte_info->devid, irte_info->index);
3420 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3423 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3424 struct amd_ir_data *ir_data,
3425 struct irq_2_irte *irte_info,
3426 struct irq_cfg *cfg);
3428 static int irq_remapping_activate(struct irq_domain *domain,
3429 struct irq_data *irq_data, bool reserve)
3431 struct amd_ir_data *data = irq_data->chip_data;
3432 struct irq_2_irte *irte_info = &data->irq_2_irte;
3433 struct amd_iommu *iommu = data->iommu;
3434 struct irq_cfg *cfg = irqd_cfg(irq_data);
3439 iommu->irte_ops->activate(iommu, data->entry, irte_info->devid,
3441 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
3445 static void irq_remapping_deactivate(struct irq_domain *domain,
3446 struct irq_data *irq_data)
3448 struct amd_ir_data *data = irq_data->chip_data;
3449 struct irq_2_irte *irte_info = &data->irq_2_irte;
3450 struct amd_iommu *iommu = data->iommu;
3453 iommu->irte_ops->deactivate(iommu, data->entry, irte_info->devid,
3457 static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec,
3458 enum irq_domain_bus_token bus_token)
3460 struct amd_iommu *iommu;
3463 if (!amd_iommu_irq_remap)
3466 if (x86_fwspec_is_ioapic(fwspec))
3467 devid = get_ioapic_devid(fwspec->param[0]);
3468 else if (x86_fwspec_is_hpet(fwspec))
3469 devid = get_hpet_devid(fwspec->param[0]);
3473 iommu = __rlookup_amd_iommu((devid >> 16), (devid & 0xffff));
3475 return iommu && iommu->ir_domain == d;
3478 static const struct irq_domain_ops amd_ir_domain_ops = {
3479 .select = irq_remapping_select,
3480 .alloc = irq_remapping_alloc,
3481 .free = irq_remapping_free,
3482 .activate = irq_remapping_activate,
3483 .deactivate = irq_remapping_deactivate,
3486 int amd_iommu_activate_guest_mode(void *data)
3488 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3489 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3492 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3493 !entry || entry->lo.fields_vapic.guest_mode)
3496 valid = entry->lo.fields_vapic.valid;
3501 entry->lo.fields_vapic.valid = valid;
3502 entry->lo.fields_vapic.guest_mode = 1;
3503 entry->lo.fields_vapic.ga_log_intr = 1;
3504 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
3505 entry->hi.fields.vector = ir_data->ga_vector;
3506 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
3508 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
3509 ir_data->irq_2_irte.index, entry, ir_data);
3511 EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
3513 int amd_iommu_deactivate_guest_mode(void *data)
3515 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3516 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3517 struct irq_cfg *cfg = ir_data->cfg;
3520 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3521 !entry || !entry->lo.fields_vapic.guest_mode)
3524 valid = entry->lo.fields_remap.valid;
3529 entry->lo.fields_remap.valid = valid;
3530 entry->lo.fields_remap.dm = apic->dest_mode_logical;
3531 entry->lo.fields_remap.int_type = apic->delivery_mode;
3532 entry->hi.fields.vector = cfg->vector;
3533 entry->lo.fields_remap.destination =
3534 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
3535 entry->hi.fields.destination =
3536 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
3538 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
3539 ir_data->irq_2_irte.index, entry, ir_data);
3541 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
3543 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
3546 struct amd_iommu_pi_data *pi_data = vcpu_info;
3547 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
3548 struct amd_ir_data *ir_data = data->chip_data;
3549 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3550 struct iommu_dev_data *dev_data;
3552 if (ir_data->iommu == NULL)
3555 dev_data = search_dev_data(ir_data->iommu, irte_info->devid);
3558 * This device has never been set up for guest mode.
3559 * we should not modify the IRTE
3561 if (!dev_data || !dev_data->use_vapic)
3564 ir_data->cfg = irqd_cfg(data);
3565 pi_data->ir_data = ir_data;
3568 * SVM tries to set up for VAPIC mode, but we are in
3569 * legacy mode. So, we force legacy mode instead.
3571 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3572 pr_debug("%s: Fall back to using intr legacy remap\n",
3574 pi_data->is_guest_mode = false;
3577 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
3578 if (pi_data->is_guest_mode) {
3579 ir_data->ga_root_ptr = (pi_data->base >> 12);
3580 ir_data->ga_vector = vcpu_pi_info->vector;
3581 ir_data->ga_tag = pi_data->ga_tag;
3582 ret = amd_iommu_activate_guest_mode(ir_data);
3584 ir_data->cached_ga_tag = pi_data->ga_tag;
3586 ret = amd_iommu_deactivate_guest_mode(ir_data);
3589 * This communicates the ga_tag back to the caller
3590 * so that it can do all the necessary clean up.
3593 ir_data->cached_ga_tag = 0;
3600 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3601 struct amd_ir_data *ir_data,
3602 struct irq_2_irte *irte_info,
3603 struct irq_cfg *cfg)
3607 * Atomically updates the IRTE with the new destination, vector
3608 * and flushes the interrupt entry cache.
3610 iommu->irte_ops->set_affinity(iommu, ir_data->entry, irte_info->devid,
3611 irte_info->index, cfg->vector,
3615 static int amd_ir_set_affinity(struct irq_data *data,
3616 const struct cpumask *mask, bool force)
3618 struct amd_ir_data *ir_data = data->chip_data;
3619 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3620 struct irq_cfg *cfg = irqd_cfg(data);
3621 struct irq_data *parent = data->parent_data;
3622 struct amd_iommu *iommu = ir_data->iommu;
3628 ret = parent->chip->irq_set_affinity(parent, mask, force);
3629 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3632 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
3634 * After this point, all the interrupts will start arriving
3635 * at the new destination. So, time to cleanup the previous
3636 * vector allocation.
3638 send_cleanup_vector(cfg);
3640 return IRQ_SET_MASK_OK_DONE;
3643 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3645 struct amd_ir_data *ir_data = irq_data->chip_data;
3647 *msg = ir_data->msi_entry;
3650 static struct irq_chip amd_ir_chip = {
3652 .irq_ack = apic_ack_irq,
3653 .irq_set_affinity = amd_ir_set_affinity,
3654 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
3655 .irq_compose_msi_msg = ir_compose_msi_msg,
3658 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3660 struct fwnode_handle *fn;
3662 fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
3665 iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
3666 if (!iommu->ir_domain) {
3667 irq_domain_free_fwnode(fn);
3671 iommu->ir_domain->parent = arch_get_ir_parent_domain();
3672 iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
3678 int amd_iommu_update_ga(int cpu, bool is_run, void *data)
3680 unsigned long flags;
3681 struct amd_iommu *iommu;
3682 struct irq_remap_table *table;
3683 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3684 int devid = ir_data->irq_2_irte.devid;
3685 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3686 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
3688 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3689 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
3692 iommu = ir_data->iommu;
3696 table = get_irq_table(iommu, devid);
3700 raw_spin_lock_irqsave(&table->lock, flags);
3702 if (ref->lo.fields_vapic.guest_mode) {
3704 ref->lo.fields_vapic.destination =
3705 APICID_TO_IRTE_DEST_LO(cpu);
3706 ref->hi.fields.destination =
3707 APICID_TO_IRTE_DEST_HI(cpu);
3709 ref->lo.fields_vapic.is_run = is_run;
3713 raw_spin_unlock_irqrestore(&table->lock, flags);
3715 iommu_flush_irt(iommu, devid);
3716 iommu_completion_wait(iommu);
3719 EXPORT_SYMBOL(amd_iommu_update_ga);