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 /* Device not attached to domain properly */
562 if (dev_data->domain == NULL) {
563 pr_err_ratelimited("Event logged [Device not attached to domain properly]\n");
564 pr_err_ratelimited(" device=%04x:%02x:%02x.%x domain=0x%04x\n",
565 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid),
566 PCI_FUNC(devid), domain_id);
570 if (!report_iommu_fault(&dev_data->domain->domain,
572 IS_WRITE_REQUEST(flags) ?
578 if (__ratelimit(&dev_data->rs)) {
579 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
580 domain_id, address, flags);
583 pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
584 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
585 domain_id, address, flags);
593 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
595 struct device *dev = iommu->iommu.dev;
596 int type, devid, flags, tag;
597 volatile u32 *event = __evt;
603 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
604 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
605 pasid = (event[0] & EVENT_DOMID_MASK_HI) |
606 (event[1] & EVENT_DOMID_MASK_LO);
607 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
608 address = (u64)(((u64)event[3]) << 32) | event[2];
611 /* Did we hit the erratum? */
612 if (++count == LOOP_TIMEOUT) {
613 pr_err("No event written to event log\n");
620 if (type == EVENT_TYPE_IO_FAULT) {
621 amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
626 case EVENT_TYPE_ILL_DEV:
627 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
628 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
629 pasid, address, flags);
630 dump_dte_entry(iommu, devid);
632 case EVENT_TYPE_DEV_TAB_ERR:
633 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x "
634 "address=0x%llx flags=0x%04x]\n",
635 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
638 case EVENT_TYPE_PAGE_TAB_ERR:
639 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
640 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
641 pasid, address, flags);
643 case EVENT_TYPE_ILL_CMD:
644 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
645 dump_command(address);
647 case EVENT_TYPE_CMD_HARD_ERR:
648 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
651 case EVENT_TYPE_IOTLB_INV_TO:
652 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%04x:%02x:%02x.%x address=0x%llx]\n",
653 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
656 case EVENT_TYPE_INV_DEV_REQ:
657 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
658 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
659 pasid, address, flags);
661 case EVENT_TYPE_RMP_FAULT:
662 amd_iommu_report_rmp_fault(iommu, event);
664 case EVENT_TYPE_RMP_HW_ERR:
665 amd_iommu_report_rmp_hw_error(iommu, event);
667 case EVENT_TYPE_INV_PPR_REQ:
668 pasid = PPR_PASID(*((u64 *)__evt));
669 tag = event[1] & 0x03FF;
670 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",
671 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
672 pasid, address, flags, tag);
675 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
676 event[0], event[1], event[2], event[3]);
680 * To detect the hardware errata 732 we need to clear the
681 * entry back to zero. This issue does not exist on SNP
682 * enabled system. Also this buffer is not writeable on
683 * SNP enabled system.
685 if (!amd_iommu_snp_en)
686 memset(__evt, 0, 4 * sizeof(u32));
689 static void iommu_poll_events(struct amd_iommu *iommu)
693 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
694 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
696 while (head != tail) {
697 iommu_print_event(iommu, iommu->evt_buf + head);
698 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
701 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
704 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
706 struct amd_iommu_fault fault;
708 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
709 pr_err_ratelimited("Unknown PPR request received\n");
713 fault.address = raw[1];
714 fault.pasid = PPR_PASID(raw[0]);
715 fault.sbdf = PCI_SEG_DEVID_TO_SBDF(iommu->pci_seg->id, PPR_DEVID(raw[0]));
716 fault.tag = PPR_TAG(raw[0]);
717 fault.flags = PPR_FLAGS(raw[0]);
719 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
722 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
726 if (iommu->ppr_log == NULL)
729 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
730 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
732 while (head != tail) {
737 raw = (u64 *)(iommu->ppr_log + head);
740 * Hardware bug: Interrupt may arrive before the entry is
741 * written to memory. If this happens we need to wait for the
744 for (i = 0; i < LOOP_TIMEOUT; ++i) {
745 if (PPR_REQ_TYPE(raw[0]) != 0)
750 /* Avoid memcpy function-call overhead */
755 * To detect the hardware errata 733 we need to clear the
756 * entry back to zero. This issue does not exist on SNP
757 * enabled system. Also this buffer is not writeable on
758 * SNP enabled system.
760 if (!amd_iommu_snp_en)
761 raw[0] = raw[1] = 0UL;
763 /* Update head pointer of hardware ring-buffer */
764 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
765 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
767 /* Handle PPR entry */
768 iommu_handle_ppr_entry(iommu, entry);
770 /* Refresh ring-buffer information */
771 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
772 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
776 #ifdef CONFIG_IRQ_REMAP
777 static int (*iommu_ga_log_notifier)(u32);
779 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
781 iommu_ga_log_notifier = notifier;
785 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
787 static void iommu_poll_ga_log(struct amd_iommu *iommu)
791 if (iommu->ga_log == NULL)
794 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
795 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
797 while (head != tail) {
801 raw = (u64 *)(iommu->ga_log + head);
803 /* Avoid memcpy function-call overhead */
806 /* Update head pointer of hardware ring-buffer */
807 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
808 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
810 /* Handle GA entry */
811 switch (GA_REQ_TYPE(log_entry)) {
813 if (!iommu_ga_log_notifier)
816 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
817 __func__, GA_DEVID(log_entry),
820 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
821 pr_err("GA log notifier failed.\n");
830 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
832 if (!irq_remapping_enabled || !dev_is_pci(dev) ||
833 !pci_dev_has_default_msi_parent_domain(to_pci_dev(dev)))
836 dev_set_msi_domain(dev, iommu->ir_domain);
839 #else /* CONFIG_IRQ_REMAP */
841 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
842 #endif /* !CONFIG_IRQ_REMAP */
844 #define AMD_IOMMU_INT_MASK \
845 (MMIO_STATUS_EVT_OVERFLOW_INT_MASK | \
846 MMIO_STATUS_EVT_INT_MASK | \
847 MMIO_STATUS_PPR_INT_MASK | \
848 MMIO_STATUS_GALOG_OVERFLOW_MASK | \
849 MMIO_STATUS_GALOG_INT_MASK)
851 irqreturn_t amd_iommu_int_thread(int irq, void *data)
853 struct amd_iommu *iommu = (struct amd_iommu *) data;
854 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
856 while (status & AMD_IOMMU_INT_MASK) {
857 /* Enable interrupt sources again */
858 writel(AMD_IOMMU_INT_MASK,
859 iommu->mmio_base + MMIO_STATUS_OFFSET);
861 if (status & MMIO_STATUS_EVT_INT_MASK) {
862 pr_devel("Processing IOMMU Event Log\n");
863 iommu_poll_events(iommu);
866 if (status & MMIO_STATUS_PPR_INT_MASK) {
867 pr_devel("Processing IOMMU PPR Log\n");
868 iommu_poll_ppr_log(iommu);
871 #ifdef CONFIG_IRQ_REMAP
872 if (status & (MMIO_STATUS_GALOG_INT_MASK |
873 MMIO_STATUS_GALOG_OVERFLOW_MASK)) {
874 pr_devel("Processing IOMMU GA Log\n");
875 iommu_poll_ga_log(iommu);
878 if (status & MMIO_STATUS_GALOG_OVERFLOW_MASK) {
879 pr_info_ratelimited("IOMMU GA Log overflow\n");
880 amd_iommu_restart_ga_log(iommu);
884 if (status & MMIO_STATUS_EVT_OVERFLOW_INT_MASK) {
885 pr_info_ratelimited("IOMMU event log overflow\n");
886 amd_iommu_restart_event_logging(iommu);
890 * Hardware bug: ERBT1312
891 * When re-enabling interrupt (by writing 1
892 * to clear the bit), the hardware might also try to set
893 * the interrupt bit in the event status register.
894 * In this scenario, the bit will be set, and disable
895 * subsequent interrupts.
897 * Workaround: The IOMMU driver should read back the
898 * status register and check if the interrupt bits are cleared.
899 * If not, driver will need to go through the interrupt handler
900 * again and re-clear the bits
902 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
907 irqreturn_t amd_iommu_int_handler(int irq, void *data)
909 return IRQ_WAKE_THREAD;
912 /****************************************************************************
914 * IOMMU command queuing functions
916 ****************************************************************************/
918 static int wait_on_sem(struct amd_iommu *iommu, u64 data)
922 while (*iommu->cmd_sem != data && i < LOOP_TIMEOUT) {
927 if (i == LOOP_TIMEOUT) {
928 pr_alert("Completion-Wait loop timed out\n");
935 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
936 struct iommu_cmd *cmd)
941 /* Copy command to buffer */
942 tail = iommu->cmd_buf_tail;
943 target = iommu->cmd_buf + tail;
944 memcpy(target, cmd, sizeof(*cmd));
946 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
947 iommu->cmd_buf_tail = tail;
949 /* Tell the IOMMU about it */
950 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
953 static void build_completion_wait(struct iommu_cmd *cmd,
954 struct amd_iommu *iommu,
957 u64 paddr = iommu_virt_to_phys((void *)iommu->cmd_sem);
959 memset(cmd, 0, sizeof(*cmd));
960 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
961 cmd->data[1] = upper_32_bits(paddr);
962 cmd->data[2] = lower_32_bits(data);
963 cmd->data[3] = upper_32_bits(data);
964 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
967 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
969 memset(cmd, 0, sizeof(*cmd));
970 cmd->data[0] = devid;
971 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
975 * Builds an invalidation address which is suitable for one page or multiple
976 * pages. Sets the size bit (S) as needed is more than one page is flushed.
978 static inline u64 build_inv_address(u64 address, size_t size)
980 u64 pages, end, msb_diff;
982 pages = iommu_num_pages(address, size, PAGE_SIZE);
985 return address & PAGE_MASK;
987 end = address + size - 1;
990 * msb_diff would hold the index of the most significant bit that
991 * flipped between the start and end.
993 msb_diff = fls64(end ^ address) - 1;
996 * Bits 63:52 are sign extended. If for some reason bit 51 is different
997 * between the start and the end, invalidate everything.
999 if (unlikely(msb_diff > 51)) {
1000 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
1003 * The msb-bit must be clear on the address. Just set all the
1006 address |= (1ull << msb_diff) - 1;
1009 /* Clear bits 11:0 */
1010 address &= PAGE_MASK;
1012 /* Set the size bit - we flush more than one 4kb page */
1013 return address | CMD_INV_IOMMU_PAGES_SIZE_MASK;
1016 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
1017 size_t size, u16 domid, int pde)
1019 u64 inv_address = build_inv_address(address, size);
1021 memset(cmd, 0, sizeof(*cmd));
1022 cmd->data[1] |= domid;
1023 cmd->data[2] = lower_32_bits(inv_address);
1024 cmd->data[3] = upper_32_bits(inv_address);
1025 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1026 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
1027 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1030 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
1031 u64 address, size_t size)
1033 u64 inv_address = build_inv_address(address, size);
1035 memset(cmd, 0, sizeof(*cmd));
1036 cmd->data[0] = devid;
1037 cmd->data[0] |= (qdep & 0xff) << 24;
1038 cmd->data[1] = devid;
1039 cmd->data[2] = lower_32_bits(inv_address);
1040 cmd->data[3] = upper_32_bits(inv_address);
1041 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1044 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
1045 u64 address, bool size)
1047 memset(cmd, 0, sizeof(*cmd));
1049 address &= ~(0xfffULL);
1051 cmd->data[0] = pasid;
1052 cmd->data[1] = domid;
1053 cmd->data[2] = lower_32_bits(address);
1054 cmd->data[3] = upper_32_bits(address);
1055 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1056 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1058 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1059 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1062 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid,
1063 int qdep, u64 address, bool size)
1065 memset(cmd, 0, sizeof(*cmd));
1067 address &= ~(0xfffULL);
1069 cmd->data[0] = devid;
1070 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
1071 cmd->data[0] |= (qdep & 0xff) << 24;
1072 cmd->data[1] = devid;
1073 cmd->data[1] |= (pasid & 0xff) << 16;
1074 cmd->data[2] = lower_32_bits(address);
1075 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1076 cmd->data[3] = upper_32_bits(address);
1078 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
1079 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
1082 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
1083 int status, int tag, bool gn)
1085 memset(cmd, 0, sizeof(*cmd));
1087 cmd->data[0] = devid;
1089 cmd->data[1] = pasid;
1090 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1092 cmd->data[3] = tag & 0x1ff;
1093 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1095 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1098 static void build_inv_all(struct iommu_cmd *cmd)
1100 memset(cmd, 0, sizeof(*cmd));
1101 CMD_SET_TYPE(cmd, CMD_INV_ALL);
1104 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1106 memset(cmd, 0, sizeof(*cmd));
1107 cmd->data[0] = devid;
1108 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1112 * Writes the command to the IOMMUs command buffer and informs the
1113 * hardware about the new command.
1115 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
1116 struct iommu_cmd *cmd,
1119 unsigned int count = 0;
1120 u32 left, next_tail;
1122 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1124 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
1127 /* Skip udelay() the first time around */
1129 if (count == LOOP_TIMEOUT) {
1130 pr_err("Command buffer timeout\n");
1137 /* Update head and recheck remaining space */
1138 iommu->cmd_buf_head = readl(iommu->mmio_base +
1139 MMIO_CMD_HEAD_OFFSET);
1144 copy_cmd_to_buffer(iommu, cmd);
1146 /* Do we need to make sure all commands are processed? */
1147 iommu->need_sync = sync;
1152 static int iommu_queue_command_sync(struct amd_iommu *iommu,
1153 struct iommu_cmd *cmd,
1156 unsigned long flags;
1159 raw_spin_lock_irqsave(&iommu->lock, flags);
1160 ret = __iommu_queue_command_sync(iommu, cmd, sync);
1161 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1166 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1168 return iommu_queue_command_sync(iommu, cmd, true);
1172 * This function queues a completion wait command into the command
1173 * buffer of an IOMMU
1175 static int iommu_completion_wait(struct amd_iommu *iommu)
1177 struct iommu_cmd cmd;
1178 unsigned long flags;
1182 if (!iommu->need_sync)
1185 raw_spin_lock_irqsave(&iommu->lock, flags);
1187 data = ++iommu->cmd_sem_val;
1188 build_completion_wait(&cmd, iommu, data);
1190 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1194 ret = wait_on_sem(iommu, data);
1197 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1202 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1204 struct iommu_cmd cmd;
1206 build_inv_dte(&cmd, devid);
1208 return iommu_queue_command(iommu, &cmd);
1211 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
1214 u16 last_bdf = iommu->pci_seg->last_bdf;
1216 for (devid = 0; devid <= last_bdf; ++devid)
1217 iommu_flush_dte(iommu, devid);
1219 iommu_completion_wait(iommu);
1223 * This function uses heavy locking and may disable irqs for some time. But
1224 * this is no issue because it is only called during resume.
1226 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
1229 u16 last_bdf = iommu->pci_seg->last_bdf;
1231 for (dom_id = 0; dom_id <= last_bdf; ++dom_id) {
1232 struct iommu_cmd cmd;
1233 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1235 iommu_queue_command(iommu, &cmd);
1238 iommu_completion_wait(iommu);
1241 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1243 struct iommu_cmd cmd;
1245 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1247 iommu_queue_command(iommu, &cmd);
1249 iommu_completion_wait(iommu);
1252 static void amd_iommu_flush_all(struct amd_iommu *iommu)
1254 struct iommu_cmd cmd;
1256 build_inv_all(&cmd);
1258 iommu_queue_command(iommu, &cmd);
1259 iommu_completion_wait(iommu);
1262 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1264 struct iommu_cmd cmd;
1266 build_inv_irt(&cmd, devid);
1268 iommu_queue_command(iommu, &cmd);
1271 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
1274 u16 last_bdf = iommu->pci_seg->last_bdf;
1276 for (devid = 0; devid <= last_bdf; devid++)
1277 iommu_flush_irt(iommu, devid);
1279 iommu_completion_wait(iommu);
1282 void iommu_flush_all_caches(struct amd_iommu *iommu)
1284 if (iommu_feature(iommu, FEATURE_IA)) {
1285 amd_iommu_flush_all(iommu);
1287 amd_iommu_flush_dte_all(iommu);
1288 amd_iommu_flush_irt_all(iommu);
1289 amd_iommu_flush_tlb_all(iommu);
1294 * Command send function for flushing on-device TLB
1296 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1297 u64 address, size_t size)
1299 struct amd_iommu *iommu;
1300 struct iommu_cmd cmd;
1303 qdep = dev_data->ats.qdep;
1304 iommu = rlookup_amd_iommu(dev_data->dev);
1308 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1310 return iommu_queue_command(iommu, &cmd);
1313 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
1315 struct amd_iommu *iommu = data;
1317 return iommu_flush_dte(iommu, alias);
1321 * Command send function for invalidating a device table entry
1323 static int device_flush_dte(struct iommu_dev_data *dev_data)
1325 struct amd_iommu *iommu;
1326 struct pci_dev *pdev = NULL;
1327 struct amd_iommu_pci_seg *pci_seg;
1331 iommu = rlookup_amd_iommu(dev_data->dev);
1335 if (dev_is_pci(dev_data->dev))
1336 pdev = to_pci_dev(dev_data->dev);
1339 ret = pci_for_each_dma_alias(pdev,
1340 device_flush_dte_alias, iommu);
1342 ret = iommu_flush_dte(iommu, dev_data->devid);
1346 pci_seg = iommu->pci_seg;
1347 alias = pci_seg->alias_table[dev_data->devid];
1348 if (alias != dev_data->devid) {
1349 ret = iommu_flush_dte(iommu, alias);
1354 if (dev_data->ats.enabled)
1355 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1361 * TLB invalidation function which is called from the mapping functions.
1362 * It invalidates a single PTE if the range to flush is within a single
1363 * page. Otherwise it flushes the whole TLB of the IOMMU.
1365 static void __domain_flush_pages(struct protection_domain *domain,
1366 u64 address, size_t size, int pde)
1368 struct iommu_dev_data *dev_data;
1369 struct iommu_cmd cmd;
1372 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1374 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1375 if (!domain->dev_iommu[i])
1379 * Devices of this domain are behind this IOMMU
1380 * We need a TLB flush
1382 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1385 list_for_each_entry(dev_data, &domain->dev_list, list) {
1387 if (!dev_data->ats.enabled)
1390 ret |= device_flush_iotlb(dev_data, address, size);
1396 static void domain_flush_pages(struct protection_domain *domain,
1397 u64 address, size_t size, int pde)
1399 if (likely(!amd_iommu_np_cache)) {
1400 __domain_flush_pages(domain, address, size, pde);
1405 * When NpCache is on, we infer that we run in a VM and use a vIOMMU.
1406 * In such setups it is best to avoid flushes of ranges which are not
1407 * naturally aligned, since it would lead to flushes of unmodified
1408 * PTEs. Such flushes would require the hypervisor to do more work than
1409 * necessary. Therefore, perform repeated flushes of aligned ranges
1410 * until you cover the range. Each iteration flushes the smaller
1411 * between the natural alignment of the address that we flush and the
1412 * greatest naturally aligned region that fits in the range.
1415 int addr_alignment = __ffs(address);
1416 int size_alignment = __fls(size);
1421 * size is always non-zero, but address might be zero, causing
1422 * addr_alignment to be negative. As the casting of the
1423 * argument in __ffs(address) to long might trim the high bits
1424 * of the address on x86-32, cast to long when doing the check.
1426 if (likely((unsigned long)address != 0))
1427 min_alignment = min(addr_alignment, size_alignment);
1429 min_alignment = size_alignment;
1431 flush_size = 1ul << min_alignment;
1433 __domain_flush_pages(domain, address, flush_size, pde);
1434 address += flush_size;
1439 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1440 void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain)
1442 domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1445 void amd_iommu_domain_flush_complete(struct protection_domain *domain)
1449 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1450 if (domain && !domain->dev_iommu[i])
1454 * Devices of this domain are behind this IOMMU
1455 * We need to wait for completion of all commands.
1457 iommu_completion_wait(amd_iommus[i]);
1461 /* Flush the not present cache if it exists */
1462 static void domain_flush_np_cache(struct protection_domain *domain,
1463 dma_addr_t iova, size_t size)
1465 if (unlikely(amd_iommu_np_cache)) {
1466 unsigned long flags;
1468 spin_lock_irqsave(&domain->lock, flags);
1469 domain_flush_pages(domain, iova, size, 1);
1470 amd_iommu_domain_flush_complete(domain);
1471 spin_unlock_irqrestore(&domain->lock, flags);
1477 * This function flushes the DTEs for all devices in domain
1479 static void domain_flush_devices(struct protection_domain *domain)
1481 struct iommu_dev_data *dev_data;
1483 list_for_each_entry(dev_data, &domain->dev_list, list)
1484 device_flush_dte(dev_data);
1487 /****************************************************************************
1489 * The next functions belong to the domain allocation. A domain is
1490 * allocated for every IOMMU as the default domain. If device isolation
1491 * is enabled, every device get its own domain. The most important thing
1492 * about domains is the page table mapping the DMA address space they
1495 ****************************************************************************/
1497 static u16 domain_id_alloc(void)
1501 spin_lock(&pd_bitmap_lock);
1502 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1504 if (id > 0 && id < MAX_DOMAIN_ID)
1505 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1508 spin_unlock(&pd_bitmap_lock);
1513 static void domain_id_free(int id)
1515 spin_lock(&pd_bitmap_lock);
1516 if (id > 0 && id < MAX_DOMAIN_ID)
1517 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1518 spin_unlock(&pd_bitmap_lock);
1521 static void free_gcr3_tbl_level1(u64 *tbl)
1526 for (i = 0; i < 512; ++i) {
1527 if (!(tbl[i] & GCR3_VALID))
1530 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1532 free_page((unsigned long)ptr);
1536 static void free_gcr3_tbl_level2(u64 *tbl)
1541 for (i = 0; i < 512; ++i) {
1542 if (!(tbl[i] & GCR3_VALID))
1545 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1547 free_gcr3_tbl_level1(ptr);
1551 static void free_gcr3_table(struct protection_domain *domain)
1553 if (domain->glx == 2)
1554 free_gcr3_tbl_level2(domain->gcr3_tbl);
1555 else if (domain->glx == 1)
1556 free_gcr3_tbl_level1(domain->gcr3_tbl);
1558 BUG_ON(domain->glx != 0);
1560 free_page((unsigned long)domain->gcr3_tbl);
1563 static void set_dte_entry(struct amd_iommu *iommu, u16 devid,
1564 struct protection_domain *domain, bool ats, bool ppr)
1569 struct dev_table_entry *dev_table = get_dev_table(iommu);
1571 if (domain->iop.mode != PAGE_MODE_NONE)
1572 pte_root = iommu_virt_to_phys(domain->iop.root);
1574 pte_root |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
1575 << DEV_ENTRY_MODE_SHIFT;
1577 pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V;
1580 * When SNP is enabled, Only set TV bit when IOMMU
1581 * page translation is in use.
1583 if (!amd_iommu_snp_en || (domain->id != 0))
1584 pte_root |= DTE_FLAG_TV;
1586 flags = dev_table[devid].data[1];
1589 flags |= DTE_FLAG_IOTLB;
1592 if (iommu_feature(iommu, FEATURE_EPHSUP))
1593 pte_root |= 1ULL << DEV_ENTRY_PPR;
1596 if (domain->flags & PD_IOMMUV2_MASK) {
1597 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
1598 u64 glx = domain->glx;
1601 pte_root |= DTE_FLAG_GV;
1602 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1604 /* First mask out possible old values for GCR3 table */
1605 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1608 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1611 /* Encode GCR3 table into DTE */
1612 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1615 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1618 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1621 if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL) {
1622 dev_table[devid].data[2] |=
1623 ((u64)GUEST_PGTABLE_5_LEVEL << DTE_GPT_LEVEL_SHIFT);
1626 if (domain->flags & PD_GIOV_MASK)
1627 pte_root |= DTE_FLAG_GIOV;
1630 flags &= ~DEV_DOMID_MASK;
1631 flags |= domain->id;
1633 old_domid = dev_table[devid].data[1] & DEV_DOMID_MASK;
1634 dev_table[devid].data[1] = flags;
1635 dev_table[devid].data[0] = pte_root;
1638 * A kdump kernel might be replacing a domain ID that was copied from
1639 * the previous kernel--if so, it needs to flush the translation cache
1640 * entries for the old domain ID that is being overwritten
1643 amd_iommu_flush_tlb_domid(iommu, old_domid);
1647 static void clear_dte_entry(struct amd_iommu *iommu, u16 devid)
1649 struct dev_table_entry *dev_table = get_dev_table(iommu);
1651 /* remove entry from the device table seen by the hardware */
1652 dev_table[devid].data[0] = DTE_FLAG_V;
1654 if (!amd_iommu_snp_en)
1655 dev_table[devid].data[0] |= DTE_FLAG_TV;
1657 dev_table[devid].data[1] &= DTE_FLAG_MASK;
1659 amd_iommu_apply_erratum_63(iommu, devid);
1662 static void do_attach(struct iommu_dev_data *dev_data,
1663 struct protection_domain *domain)
1665 struct amd_iommu *iommu;
1668 iommu = rlookup_amd_iommu(dev_data->dev);
1671 ats = dev_data->ats.enabled;
1673 /* Update data structures */
1674 dev_data->domain = domain;
1675 list_add(&dev_data->list, &domain->dev_list);
1677 /* Update NUMA Node ID */
1678 if (domain->nid == NUMA_NO_NODE)
1679 domain->nid = dev_to_node(dev_data->dev);
1681 /* Do reference counting */
1682 domain->dev_iommu[iommu->index] += 1;
1683 domain->dev_cnt += 1;
1685 /* Update device table */
1686 set_dte_entry(iommu, dev_data->devid, domain,
1687 ats, dev_data->iommu_v2);
1688 clone_aliases(iommu, dev_data->dev);
1690 device_flush_dte(dev_data);
1693 static void do_detach(struct iommu_dev_data *dev_data)
1695 struct protection_domain *domain = dev_data->domain;
1696 struct amd_iommu *iommu;
1698 iommu = rlookup_amd_iommu(dev_data->dev);
1702 /* Update data structures */
1703 dev_data->domain = NULL;
1704 list_del(&dev_data->list);
1705 clear_dte_entry(iommu, dev_data->devid);
1706 clone_aliases(iommu, dev_data->dev);
1708 /* Flush the DTE entry */
1709 device_flush_dte(dev_data);
1712 amd_iommu_domain_flush_tlb_pde(domain);
1714 /* Wait for the flushes to finish */
1715 amd_iommu_domain_flush_complete(domain);
1717 /* decrease reference counters - needs to happen after the flushes */
1718 domain->dev_iommu[iommu->index] -= 1;
1719 domain->dev_cnt -= 1;
1722 static void pdev_iommuv2_disable(struct pci_dev *pdev)
1724 pci_disable_ats(pdev);
1725 pci_disable_pri(pdev);
1726 pci_disable_pasid(pdev);
1729 static int pdev_pri_ats_enable(struct pci_dev *pdev)
1733 /* Only allow access to user-accessible pages */
1734 ret = pci_enable_pasid(pdev, 0);
1738 /* First reset the PRI state of the device */
1739 ret = pci_reset_pri(pdev);
1744 /* FIXME: Hardcode number of outstanding requests for now */
1745 ret = pci_enable_pri(pdev, 32);
1749 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1756 pci_disable_pri(pdev);
1759 pci_disable_pasid(pdev);
1765 * If a device is not yet associated with a domain, this function makes the
1766 * device visible in the domain
1768 static int attach_device(struct device *dev,
1769 struct protection_domain *domain)
1771 struct iommu_dev_data *dev_data;
1772 struct pci_dev *pdev;
1773 unsigned long flags;
1776 spin_lock_irqsave(&domain->lock, flags);
1778 dev_data = dev_iommu_priv_get(dev);
1780 spin_lock(&dev_data->lock);
1783 if (dev_data->domain != NULL)
1786 if (!dev_is_pci(dev))
1787 goto skip_ats_check;
1789 pdev = to_pci_dev(dev);
1790 if (domain->flags & PD_IOMMUV2_MASK) {
1791 struct iommu_domain *def_domain = iommu_get_dma_domain(dev);
1796 * In case of using AMD_IOMMU_V1 page table mode and the device
1797 * is enabling for PPR/ATS support (using v2 table),
1798 * we need to make sure that the domain type is identity map.
1800 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
1801 def_domain->type != IOMMU_DOMAIN_IDENTITY) {
1805 if (dev_data->iommu_v2) {
1806 if (pdev_pri_ats_enable(pdev) != 0)
1809 dev_data->ats.enabled = true;
1810 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1811 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
1813 } else if (amd_iommu_iotlb_sup &&
1814 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
1815 dev_data->ats.enabled = true;
1816 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1822 do_attach(dev_data, domain);
1825 * We might boot into a crash-kernel here. The crashed kernel
1826 * left the caches in the IOMMU dirty. So we have to flush
1827 * here to evict all dirty stuff.
1829 amd_iommu_domain_flush_tlb_pde(domain);
1831 amd_iommu_domain_flush_complete(domain);
1834 spin_unlock(&dev_data->lock);
1836 spin_unlock_irqrestore(&domain->lock, flags);
1842 * Removes a device from a protection domain (with devtable_lock held)
1844 static void detach_device(struct device *dev)
1846 struct protection_domain *domain;
1847 struct iommu_dev_data *dev_data;
1848 unsigned long flags;
1850 dev_data = dev_iommu_priv_get(dev);
1851 domain = dev_data->domain;
1853 spin_lock_irqsave(&domain->lock, flags);
1855 spin_lock(&dev_data->lock);
1858 * First check if the device is still attached. It might already
1859 * be detached from its domain because the generic
1860 * iommu_detach_group code detached it and we try again here in
1861 * our alias handling.
1863 if (WARN_ON(!dev_data->domain))
1866 do_detach(dev_data);
1868 if (!dev_is_pci(dev))
1871 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
1872 pdev_iommuv2_disable(to_pci_dev(dev));
1873 else if (dev_data->ats.enabled)
1874 pci_disable_ats(to_pci_dev(dev));
1876 dev_data->ats.enabled = false;
1879 spin_unlock(&dev_data->lock);
1881 spin_unlock_irqrestore(&domain->lock, flags);
1884 static struct iommu_device *amd_iommu_probe_device(struct device *dev)
1886 struct iommu_device *iommu_dev;
1887 struct amd_iommu *iommu;
1890 if (!check_device(dev))
1891 return ERR_PTR(-ENODEV);
1893 iommu = rlookup_amd_iommu(dev);
1895 return ERR_PTR(-ENODEV);
1897 /* Not registered yet? */
1898 if (!iommu->iommu.ops)
1899 return ERR_PTR(-ENODEV);
1901 if (dev_iommu_priv_get(dev))
1902 return &iommu->iommu;
1904 ret = iommu_init_device(iommu, dev);
1906 if (ret != -ENOTSUPP)
1907 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
1908 iommu_dev = ERR_PTR(ret);
1909 iommu_ignore_device(iommu, dev);
1911 amd_iommu_set_pci_msi_domain(dev, iommu);
1912 iommu_dev = &iommu->iommu;
1915 iommu_completion_wait(iommu);
1920 static void amd_iommu_probe_finalize(struct device *dev)
1922 /* Domains are initialized for this device - have a look what we ended up with */
1923 set_dma_ops(dev, NULL);
1924 iommu_setup_dma_ops(dev, 0, U64_MAX);
1927 static void amd_iommu_release_device(struct device *dev)
1929 struct amd_iommu *iommu;
1931 if (!check_device(dev))
1934 iommu = rlookup_amd_iommu(dev);
1938 amd_iommu_uninit_device(dev);
1939 iommu_completion_wait(iommu);
1942 static struct iommu_group *amd_iommu_device_group(struct device *dev)
1944 if (dev_is_pci(dev))
1945 return pci_device_group(dev);
1947 return acpihid_device_group(dev);
1950 /*****************************************************************************
1952 * The next functions belong to the dma_ops mapping/unmapping code.
1954 *****************************************************************************/
1956 static void update_device_table(struct protection_domain *domain)
1958 struct iommu_dev_data *dev_data;
1960 list_for_each_entry(dev_data, &domain->dev_list, list) {
1961 struct amd_iommu *iommu = rlookup_amd_iommu(dev_data->dev);
1965 set_dte_entry(iommu, dev_data->devid, domain,
1966 dev_data->ats.enabled, dev_data->iommu_v2);
1967 clone_aliases(iommu, dev_data->dev);
1971 void amd_iommu_update_and_flush_device_table(struct protection_domain *domain)
1973 update_device_table(domain);
1974 domain_flush_devices(domain);
1977 void amd_iommu_domain_update(struct protection_domain *domain)
1979 /* Update device table */
1980 amd_iommu_update_and_flush_device_table(domain);
1982 /* Flush domain TLB(s) and wait for completion */
1983 amd_iommu_domain_flush_tlb_pde(domain);
1984 amd_iommu_domain_flush_complete(domain);
1987 /*****************************************************************************
1989 * The following functions belong to the exported interface of AMD IOMMU
1991 * This interface allows access to lower level functions of the IOMMU
1992 * like protection domain handling and assignement of devices to domains
1993 * which is not possible with the dma_ops interface.
1995 *****************************************************************************/
1997 static void cleanup_domain(struct protection_domain *domain)
1999 struct iommu_dev_data *entry;
2000 unsigned long flags;
2002 spin_lock_irqsave(&domain->lock, flags);
2004 while (!list_empty(&domain->dev_list)) {
2005 entry = list_first_entry(&domain->dev_list,
2006 struct iommu_dev_data, list);
2007 BUG_ON(!entry->domain);
2011 spin_unlock_irqrestore(&domain->lock, flags);
2014 static void protection_domain_free(struct protection_domain *domain)
2019 if (domain->iop.pgtbl_cfg.tlb)
2020 free_io_pgtable_ops(&domain->iop.iop.ops);
2023 domain_id_free(domain->id);
2028 static int protection_domain_init_v1(struct protection_domain *domain, int mode)
2030 u64 *pt_root = NULL;
2032 BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
2034 spin_lock_init(&domain->lock);
2035 domain->id = domain_id_alloc();
2038 INIT_LIST_HEAD(&domain->dev_list);
2040 if (mode != PAGE_MODE_NONE) {
2041 pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2043 domain_id_free(domain->id);
2048 amd_iommu_domain_set_pgtable(domain, pt_root, mode);
2053 static int protection_domain_init_v2(struct protection_domain *domain)
2055 spin_lock_init(&domain->lock);
2056 domain->id = domain_id_alloc();
2059 INIT_LIST_HEAD(&domain->dev_list);
2061 domain->flags |= PD_GIOV_MASK;
2063 domain->domain.pgsize_bitmap = AMD_IOMMU_PGSIZES_V2;
2065 if (domain_enable_v2(domain, 1)) {
2066 domain_id_free(domain->id);
2073 static struct protection_domain *protection_domain_alloc(unsigned int type)
2075 struct io_pgtable_ops *pgtbl_ops;
2076 struct protection_domain *domain;
2078 int mode = DEFAULT_PGTABLE_LEVEL;
2081 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2086 * Force IOMMU v1 page table when iommu=pt and
2087 * when allocating domain for pass-through devices.
2089 if (type == IOMMU_DOMAIN_IDENTITY) {
2090 pgtable = AMD_IOMMU_V1;
2091 mode = PAGE_MODE_NONE;
2092 } else if (type == IOMMU_DOMAIN_UNMANAGED) {
2093 pgtable = AMD_IOMMU_V1;
2094 } else if (type == IOMMU_DOMAIN_DMA || type == IOMMU_DOMAIN_DMA_FQ) {
2095 pgtable = amd_iommu_pgtable;
2102 ret = protection_domain_init_v1(domain, mode);
2105 ret = protection_domain_init_v2(domain);
2114 /* No need to allocate io pgtable ops in passthrough mode */
2115 if (type == IOMMU_DOMAIN_IDENTITY)
2118 domain->nid = NUMA_NO_NODE;
2120 pgtbl_ops = alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl_cfg, domain);
2122 domain_id_free(domain->id);
2132 static inline u64 dma_max_address(void)
2134 if (amd_iommu_pgtable == AMD_IOMMU_V1)
2137 /* V2 with 4/5 level page table */
2138 return ((1ULL << PM_LEVEL_SHIFT(amd_iommu_gpt_level)) - 1);
2141 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2143 struct protection_domain *domain;
2146 * Since DTE[Mode]=0 is prohibited on SNP-enabled system,
2147 * default to use IOMMU_DOMAIN_DMA[_FQ].
2149 if (amd_iommu_snp_en && (type == IOMMU_DOMAIN_IDENTITY))
2152 domain = protection_domain_alloc(type);
2156 domain->domain.geometry.aperture_start = 0;
2157 domain->domain.geometry.aperture_end = dma_max_address();
2158 domain->domain.geometry.force_aperture = true;
2160 return &domain->domain;
2163 static void amd_iommu_domain_free(struct iommu_domain *dom)
2165 struct protection_domain *domain;
2167 domain = to_pdomain(dom);
2169 if (domain->dev_cnt > 0)
2170 cleanup_domain(domain);
2172 BUG_ON(domain->dev_cnt != 0);
2177 if (domain->flags & PD_IOMMUV2_MASK)
2178 free_gcr3_table(domain);
2180 protection_domain_free(domain);
2183 static int amd_iommu_attach_device(struct iommu_domain *dom,
2186 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2187 struct protection_domain *domain = to_pdomain(dom);
2188 struct amd_iommu *iommu = rlookup_amd_iommu(dev);
2192 * Skip attach device to domain if new domain is same as
2193 * devices current domain
2195 if (dev_data->domain == domain)
2198 dev_data->defer_attach = false;
2200 if (dev_data->domain)
2203 ret = attach_device(dev, domain);
2205 #ifdef CONFIG_IRQ_REMAP
2206 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
2207 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
2208 dev_data->use_vapic = 1;
2210 dev_data->use_vapic = 0;
2214 iommu_completion_wait(iommu);
2219 static void amd_iommu_iotlb_sync_map(struct iommu_domain *dom,
2220 unsigned long iova, size_t size)
2222 struct protection_domain *domain = to_pdomain(dom);
2223 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2226 domain_flush_np_cache(domain, iova, size);
2229 static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
2230 phys_addr_t paddr, size_t pgsize, size_t pgcount,
2231 int iommu_prot, gfp_t gfp, size_t *mapped)
2233 struct protection_domain *domain = to_pdomain(dom);
2234 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2238 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2239 (domain->iop.mode == PAGE_MODE_NONE))
2242 if (iommu_prot & IOMMU_READ)
2243 prot |= IOMMU_PROT_IR;
2244 if (iommu_prot & IOMMU_WRITE)
2245 prot |= IOMMU_PROT_IW;
2247 if (ops->map_pages) {
2248 ret = ops->map_pages(ops, iova, paddr, pgsize,
2249 pgcount, prot, gfp, mapped);
2255 static void amd_iommu_iotlb_gather_add_page(struct iommu_domain *domain,
2256 struct iommu_iotlb_gather *gather,
2257 unsigned long iova, size_t size)
2260 * AMD's IOMMU can flush as many pages as necessary in a single flush.
2261 * Unless we run in a virtual machine, which can be inferred according
2262 * to whether "non-present cache" is on, it is probably best to prefer
2263 * (potentially) too extensive TLB flushing (i.e., more misses) over
2264 * mutliple TLB flushes (i.e., more flushes). For virtual machines the
2265 * hypervisor needs to synchronize the host IOMMU PTEs with those of
2266 * the guest, and the trade-off is different: unnecessary TLB flushes
2267 * should be avoided.
2269 if (amd_iommu_np_cache &&
2270 iommu_iotlb_gather_is_disjoint(gather, iova, size))
2271 iommu_iotlb_sync(domain, gather);
2273 iommu_iotlb_gather_add_range(gather, iova, size);
2276 static size_t amd_iommu_unmap_pages(struct iommu_domain *dom, unsigned long iova,
2277 size_t pgsize, size_t pgcount,
2278 struct iommu_iotlb_gather *gather)
2280 struct protection_domain *domain = to_pdomain(dom);
2281 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2284 if ((amd_iommu_pgtable == AMD_IOMMU_V1) &&
2285 (domain->iop.mode == PAGE_MODE_NONE))
2288 r = (ops->unmap_pages) ? ops->unmap_pages(ops, iova, pgsize, pgcount, NULL) : 0;
2291 amd_iommu_iotlb_gather_add_page(dom, gather, iova, r);
2296 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2299 struct protection_domain *domain = to_pdomain(dom);
2300 struct io_pgtable_ops *ops = &domain->iop.iop.ops;
2302 return ops->iova_to_phys(ops, iova);
2305 static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap)
2308 case IOMMU_CAP_CACHE_COHERENCY:
2310 case IOMMU_CAP_NOEXEC:
2312 case IOMMU_CAP_PRE_BOOT_PROTECTION:
2313 return amdr_ivrs_remap_support;
2314 case IOMMU_CAP_ENFORCE_CACHE_COHERENCY:
2323 static void amd_iommu_get_resv_regions(struct device *dev,
2324 struct list_head *head)
2326 struct iommu_resv_region *region;
2327 struct unity_map_entry *entry;
2328 struct amd_iommu *iommu;
2329 struct amd_iommu_pci_seg *pci_seg;
2332 sbdf = get_device_sbdf_id(dev);
2336 devid = PCI_SBDF_TO_DEVID(sbdf);
2337 iommu = rlookup_amd_iommu(dev);
2340 pci_seg = iommu->pci_seg;
2342 list_for_each_entry(entry, &pci_seg->unity_map, list) {
2346 if (devid < entry->devid_start || devid > entry->devid_end)
2349 type = IOMMU_RESV_DIRECT;
2350 length = entry->address_end - entry->address_start;
2351 if (entry->prot & IOMMU_PROT_IR)
2353 if (entry->prot & IOMMU_PROT_IW)
2354 prot |= IOMMU_WRITE;
2355 if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
2356 /* Exclusion range */
2357 type = IOMMU_RESV_RESERVED;
2359 region = iommu_alloc_resv_region(entry->address_start,
2363 dev_err(dev, "Out of memory allocating dm-regions\n");
2366 list_add_tail(®ion->list, head);
2369 region = iommu_alloc_resv_region(MSI_RANGE_START,
2370 MSI_RANGE_END - MSI_RANGE_START + 1,
2371 0, IOMMU_RESV_MSI, GFP_KERNEL);
2374 list_add_tail(®ion->list, head);
2376 region = iommu_alloc_resv_region(HT_RANGE_START,
2377 HT_RANGE_END - HT_RANGE_START + 1,
2378 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
2381 list_add_tail(®ion->list, head);
2384 bool amd_iommu_is_attach_deferred(struct device *dev)
2386 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
2388 return dev_data->defer_attach;
2390 EXPORT_SYMBOL_GPL(amd_iommu_is_attach_deferred);
2392 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
2394 struct protection_domain *dom = to_pdomain(domain);
2395 unsigned long flags;
2397 spin_lock_irqsave(&dom->lock, flags);
2398 amd_iommu_domain_flush_tlb_pde(dom);
2399 amd_iommu_domain_flush_complete(dom);
2400 spin_unlock_irqrestore(&dom->lock, flags);
2403 static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
2404 struct iommu_iotlb_gather *gather)
2406 struct protection_domain *dom = to_pdomain(domain);
2407 unsigned long flags;
2409 spin_lock_irqsave(&dom->lock, flags);
2410 domain_flush_pages(dom, gather->start, gather->end - gather->start + 1, 1);
2411 amd_iommu_domain_flush_complete(dom);
2412 spin_unlock_irqrestore(&dom->lock, flags);
2415 static int amd_iommu_def_domain_type(struct device *dev)
2417 struct iommu_dev_data *dev_data;
2419 dev_data = dev_iommu_priv_get(dev);
2424 * Do not identity map IOMMUv2 capable devices when:
2425 * - memory encryption is active, because some of those devices
2426 * (AMD GPUs) don't have the encryption bit in their DMA-mask
2427 * and require remapping.
2428 * - SNP is enabled, because it prohibits DTE[Mode]=0.
2430 if (dev_data->iommu_v2 &&
2431 !cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
2432 !amd_iommu_snp_en) {
2433 return IOMMU_DOMAIN_IDENTITY;
2439 static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
2441 /* IOMMU_PTE_FC is always set */
2445 const struct iommu_ops amd_iommu_ops = {
2446 .capable = amd_iommu_capable,
2447 .domain_alloc = amd_iommu_domain_alloc,
2448 .probe_device = amd_iommu_probe_device,
2449 .release_device = amd_iommu_release_device,
2450 .probe_finalize = amd_iommu_probe_finalize,
2451 .device_group = amd_iommu_device_group,
2452 .get_resv_regions = amd_iommu_get_resv_regions,
2453 .is_attach_deferred = amd_iommu_is_attach_deferred,
2454 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
2455 .def_domain_type = amd_iommu_def_domain_type,
2456 .default_domain_ops = &(const struct iommu_domain_ops) {
2457 .attach_dev = amd_iommu_attach_device,
2458 .map_pages = amd_iommu_map_pages,
2459 .unmap_pages = amd_iommu_unmap_pages,
2460 .iotlb_sync_map = amd_iommu_iotlb_sync_map,
2461 .iova_to_phys = amd_iommu_iova_to_phys,
2462 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2463 .iotlb_sync = amd_iommu_iotlb_sync,
2464 .free = amd_iommu_domain_free,
2465 .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
2469 /*****************************************************************************
2471 * The next functions do a basic initialization of IOMMU for pass through
2474 * In passthrough mode the IOMMU is initialized and enabled but not used for
2475 * DMA-API translation.
2477 *****************************************************************************/
2479 /* IOMMUv2 specific functions */
2480 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2482 return atomic_notifier_chain_register(&ppr_notifier, nb);
2484 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2486 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2488 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2490 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
2492 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2494 struct protection_domain *domain = to_pdomain(dom);
2495 unsigned long flags;
2497 spin_lock_irqsave(&domain->lock, flags);
2499 if (domain->iop.pgtbl_cfg.tlb)
2500 free_io_pgtable_ops(&domain->iop.iop.ops);
2502 spin_unlock_irqrestore(&domain->lock, flags);
2504 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
2506 /* Note: This function expects iommu_domain->lock to be held prior calling the function. */
2507 static int domain_enable_v2(struct protection_domain *domain, int pasids)
2511 /* Number of GCR3 table levels required */
2512 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
2515 if (levels > amd_iommu_max_glx_val)
2518 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
2519 if (domain->gcr3_tbl == NULL)
2522 domain->glx = levels;
2523 domain->flags |= PD_IOMMUV2_MASK;
2525 amd_iommu_domain_update(domain);
2530 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
2532 struct protection_domain *pdom = to_pdomain(dom);
2533 unsigned long flags;
2536 spin_lock_irqsave(&pdom->lock, flags);
2539 * Save us all sanity checks whether devices already in the
2540 * domain support IOMMUv2. Just force that the domain has no
2541 * devices attached when it is switched into IOMMUv2 mode.
2544 if (pdom->dev_cnt > 0 || pdom->flags & PD_IOMMUV2_MASK)
2547 if (!pdom->gcr3_tbl)
2548 ret = domain_enable_v2(pdom, pasids);
2551 spin_unlock_irqrestore(&pdom->lock, flags);
2554 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
2556 static int __flush_pasid(struct protection_domain *domain, u32 pasid,
2557 u64 address, bool size)
2559 struct iommu_dev_data *dev_data;
2560 struct iommu_cmd cmd;
2563 if (!(domain->flags & PD_IOMMUV2_MASK))
2566 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
2569 * IOMMU TLB needs to be flushed before Device TLB to
2570 * prevent device TLB refill from IOMMU TLB
2572 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
2573 if (domain->dev_iommu[i] == 0)
2576 ret = iommu_queue_command(amd_iommus[i], &cmd);
2581 /* Wait until IOMMU TLB flushes are complete */
2582 amd_iommu_domain_flush_complete(domain);
2584 /* Now flush device TLBs */
2585 list_for_each_entry(dev_data, &domain->dev_list, list) {
2586 struct amd_iommu *iommu;
2590 There might be non-IOMMUv2 capable devices in an IOMMUv2
2593 if (!dev_data->ats.enabled)
2596 qdep = dev_data->ats.qdep;
2597 iommu = rlookup_amd_iommu(dev_data->dev);
2600 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
2601 qdep, address, size);
2603 ret = iommu_queue_command(iommu, &cmd);
2608 /* Wait until all device TLBs are flushed */
2609 amd_iommu_domain_flush_complete(domain);
2618 static int __amd_iommu_flush_page(struct protection_domain *domain, u32 pasid,
2621 return __flush_pasid(domain, pasid, address, false);
2624 int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid,
2627 struct protection_domain *domain = to_pdomain(dom);
2628 unsigned long flags;
2631 spin_lock_irqsave(&domain->lock, flags);
2632 ret = __amd_iommu_flush_page(domain, pasid, address);
2633 spin_unlock_irqrestore(&domain->lock, flags);
2637 EXPORT_SYMBOL(amd_iommu_flush_page);
2639 static int __amd_iommu_flush_tlb(struct protection_domain *domain, u32 pasid)
2641 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
2645 int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid)
2647 struct protection_domain *domain = to_pdomain(dom);
2648 unsigned long flags;
2651 spin_lock_irqsave(&domain->lock, flags);
2652 ret = __amd_iommu_flush_tlb(domain, pasid);
2653 spin_unlock_irqrestore(&domain->lock, flags);
2657 EXPORT_SYMBOL(amd_iommu_flush_tlb);
2659 static u64 *__get_gcr3_pte(u64 *root, int level, u32 pasid, bool alloc)
2666 index = (pasid >> (9 * level)) & 0x1ff;
2672 if (!(*pte & GCR3_VALID)) {
2676 root = (void *)get_zeroed_page(GFP_ATOMIC);
2680 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
2683 root = iommu_phys_to_virt(*pte & PAGE_MASK);
2691 static int __set_gcr3(struct protection_domain *domain, u32 pasid,
2696 if (domain->iop.mode != PAGE_MODE_NONE)
2699 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
2703 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
2705 return __amd_iommu_flush_tlb(domain, pasid);
2708 static int __clear_gcr3(struct protection_domain *domain, u32 pasid)
2712 if (domain->iop.mode != PAGE_MODE_NONE)
2715 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
2721 return __amd_iommu_flush_tlb(domain, pasid);
2724 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,
2727 struct protection_domain *domain = to_pdomain(dom);
2728 unsigned long flags;
2731 spin_lock_irqsave(&domain->lock, flags);
2732 ret = __set_gcr3(domain, pasid, cr3);
2733 spin_unlock_irqrestore(&domain->lock, flags);
2737 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
2739 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, u32 pasid)
2741 struct protection_domain *domain = to_pdomain(dom);
2742 unsigned long flags;
2745 spin_lock_irqsave(&domain->lock, flags);
2746 ret = __clear_gcr3(domain, pasid);
2747 spin_unlock_irqrestore(&domain->lock, flags);
2751 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
2753 int amd_iommu_complete_ppr(struct pci_dev *pdev, u32 pasid,
2754 int status, int tag)
2756 struct iommu_dev_data *dev_data;
2757 struct amd_iommu *iommu;
2758 struct iommu_cmd cmd;
2760 dev_data = dev_iommu_priv_get(&pdev->dev);
2761 iommu = rlookup_amd_iommu(&pdev->dev);
2765 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
2766 tag, dev_data->pri_tlp);
2768 return iommu_queue_command(iommu, &cmd);
2770 EXPORT_SYMBOL(amd_iommu_complete_ppr);
2772 int amd_iommu_device_info(struct pci_dev *pdev,
2773 struct amd_iommu_device_info *info)
2778 if (pdev == NULL || info == NULL)
2781 if (!amd_iommu_v2_supported())
2784 memset(info, 0, sizeof(*info));
2786 if (pci_ats_supported(pdev))
2787 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
2789 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
2791 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
2793 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
2797 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
2798 max_pasids = min(max_pasids, (1 << 20));
2800 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
2801 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
2803 features = pci_pasid_features(pdev);
2804 if (features & PCI_PASID_CAP_EXEC)
2805 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
2806 if (features & PCI_PASID_CAP_PRIV)
2807 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
2812 EXPORT_SYMBOL(amd_iommu_device_info);
2814 #ifdef CONFIG_IRQ_REMAP
2816 /*****************************************************************************
2818 * Interrupt Remapping Implementation
2820 *****************************************************************************/
2822 static struct irq_chip amd_ir_chip;
2823 static DEFINE_SPINLOCK(iommu_table_lock);
2825 static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid,
2826 struct irq_remap_table *table)
2829 struct dev_table_entry *dev_table = get_dev_table(iommu);
2831 dte = dev_table[devid].data[2];
2832 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
2833 dte |= iommu_virt_to_phys(table->table);
2834 dte |= DTE_IRQ_REMAP_INTCTL;
2835 dte |= DTE_INTTABLEN;
2836 dte |= DTE_IRQ_REMAP_ENABLE;
2838 dev_table[devid].data[2] = dte;
2841 static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 devid)
2843 struct irq_remap_table *table;
2844 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
2846 if (WARN_ONCE(!pci_seg->rlookup_table[devid],
2847 "%s: no iommu for devid %x:%x\n",
2848 __func__, pci_seg->id, devid))
2851 table = pci_seg->irq_lookup_table[devid];
2852 if (WARN_ONCE(!table, "%s: no table for devid %x:%x\n",
2853 __func__, pci_seg->id, devid))
2859 static struct irq_remap_table *__alloc_irq_table(void)
2861 struct irq_remap_table *table;
2863 table = kzalloc(sizeof(*table), GFP_KERNEL);
2867 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
2868 if (!table->table) {
2872 raw_spin_lock_init(&table->lock);
2874 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2875 memset(table->table, 0,
2876 MAX_IRQS_PER_TABLE * sizeof(u32));
2878 memset(table->table, 0,
2879 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
2883 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
2884 struct irq_remap_table *table)
2886 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
2888 pci_seg->irq_lookup_table[devid] = table;
2889 set_dte_irq_entry(iommu, devid, table);
2890 iommu_flush_dte(iommu, devid);
2893 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
2896 struct irq_remap_table *table = data;
2897 struct amd_iommu_pci_seg *pci_seg;
2898 struct amd_iommu *iommu = rlookup_amd_iommu(&pdev->dev);
2903 pci_seg = iommu->pci_seg;
2904 pci_seg->irq_lookup_table[alias] = table;
2905 set_dte_irq_entry(iommu, alias, table);
2906 iommu_flush_dte(pci_seg->rlookup_table[alias], alias);
2911 static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu,
2912 u16 devid, struct pci_dev *pdev)
2914 struct irq_remap_table *table = NULL;
2915 struct irq_remap_table *new_table = NULL;
2916 struct amd_iommu_pci_seg *pci_seg;
2917 unsigned long flags;
2920 spin_lock_irqsave(&iommu_table_lock, flags);
2922 pci_seg = iommu->pci_seg;
2923 table = pci_seg->irq_lookup_table[devid];
2927 alias = pci_seg->alias_table[devid];
2928 table = pci_seg->irq_lookup_table[alias];
2930 set_remap_table_entry(iommu, devid, table);
2933 spin_unlock_irqrestore(&iommu_table_lock, flags);
2935 /* Nothing there yet, allocate new irq remapping table */
2936 new_table = __alloc_irq_table();
2940 spin_lock_irqsave(&iommu_table_lock, flags);
2942 table = pci_seg->irq_lookup_table[devid];
2946 table = pci_seg->irq_lookup_table[alias];
2948 set_remap_table_entry(iommu, devid, table);
2956 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
2959 set_remap_table_entry(iommu, devid, table);
2962 set_remap_table_entry(iommu, alias, table);
2965 iommu_completion_wait(iommu);
2968 spin_unlock_irqrestore(&iommu_table_lock, flags);
2971 kmem_cache_free(amd_iommu_irq_cache, new_table->table);
2977 static int alloc_irq_index(struct amd_iommu *iommu, u16 devid, int count,
2978 bool align, struct pci_dev *pdev)
2980 struct irq_remap_table *table;
2981 int index, c, alignment = 1;
2982 unsigned long flags;
2984 table = alloc_irq_table(iommu, devid, pdev);
2989 alignment = roundup_pow_of_two(count);
2991 raw_spin_lock_irqsave(&table->lock, flags);
2993 /* Scan table for free entries */
2994 for (index = ALIGN(table->min_index, alignment), c = 0;
2995 index < MAX_IRQS_PER_TABLE;) {
2996 if (!iommu->irte_ops->is_allocated(table, index)) {
3000 index = ALIGN(index + 1, alignment);
3006 iommu->irte_ops->set_allocated(table, index - c + 1);
3018 raw_spin_unlock_irqrestore(&table->lock, flags);
3023 static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index,
3024 struct irte_ga *irte, struct amd_ir_data *data)
3027 struct irq_remap_table *table;
3028 unsigned long flags;
3029 struct irte_ga *entry;
3031 table = get_irq_table(iommu, devid);
3035 raw_spin_lock_irqsave(&table->lock, flags);
3037 entry = (struct irte_ga *)table->table;
3038 entry = &entry[index];
3040 ret = cmpxchg_double(&entry->lo.val, &entry->hi.val,
3041 entry->lo.val, entry->hi.val,
3042 irte->lo.val, irte->hi.val);
3044 * We use cmpxchg16 to atomically update the 128-bit IRTE,
3045 * and it cannot be updated by the hardware or other processors
3046 * behind us, so the return value of cmpxchg16 should be the
3047 * same as the old value.
3054 raw_spin_unlock_irqrestore(&table->lock, flags);
3056 iommu_flush_irt(iommu, devid);
3057 iommu_completion_wait(iommu);
3062 static int modify_irte(struct amd_iommu *iommu,
3063 u16 devid, int index, union irte *irte)
3065 struct irq_remap_table *table;
3066 unsigned long flags;
3068 table = get_irq_table(iommu, devid);
3072 raw_spin_lock_irqsave(&table->lock, flags);
3073 table->table[index] = irte->val;
3074 raw_spin_unlock_irqrestore(&table->lock, flags);
3076 iommu_flush_irt(iommu, devid);
3077 iommu_completion_wait(iommu);
3082 static void free_irte(struct amd_iommu *iommu, u16 devid, int index)
3084 struct irq_remap_table *table;
3085 unsigned long flags;
3087 table = get_irq_table(iommu, devid);
3091 raw_spin_lock_irqsave(&table->lock, flags);
3092 iommu->irte_ops->clear_allocated(table, index);
3093 raw_spin_unlock_irqrestore(&table->lock, flags);
3095 iommu_flush_irt(iommu, devid);
3096 iommu_completion_wait(iommu);
3099 static void irte_prepare(void *entry,
3100 u32 delivery_mode, bool dest_mode,
3101 u8 vector, u32 dest_apicid, int devid)
3103 union irte *irte = (union irte *) entry;
3106 irte->fields.vector = vector;
3107 irte->fields.int_type = delivery_mode;
3108 irte->fields.destination = dest_apicid;
3109 irte->fields.dm = dest_mode;
3110 irte->fields.valid = 1;
3113 static void irte_ga_prepare(void *entry,
3114 u32 delivery_mode, bool dest_mode,
3115 u8 vector, u32 dest_apicid, int devid)
3117 struct irte_ga *irte = (struct irte_ga *) entry;
3121 irte->lo.fields_remap.int_type = delivery_mode;
3122 irte->lo.fields_remap.dm = dest_mode;
3123 irte->hi.fields.vector = vector;
3124 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
3125 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
3126 irte->lo.fields_remap.valid = 1;
3129 static void irte_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3131 union irte *irte = (union irte *) entry;
3133 irte->fields.valid = 1;
3134 modify_irte(iommu, devid, index, irte);
3137 static void irte_ga_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3139 struct irte_ga *irte = (struct irte_ga *) entry;
3141 irte->lo.fields_remap.valid = 1;
3142 modify_irte_ga(iommu, devid, index, irte, NULL);
3145 static void irte_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3147 union irte *irte = (union irte *) entry;
3149 irte->fields.valid = 0;
3150 modify_irte(iommu, devid, index, irte);
3153 static void irte_ga_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
3155 struct irte_ga *irte = (struct irte_ga *) entry;
3157 irte->lo.fields_remap.valid = 0;
3158 modify_irte_ga(iommu, devid, index, irte, NULL);
3161 static void irte_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
3162 u8 vector, u32 dest_apicid)
3164 union irte *irte = (union irte *) entry;
3166 irte->fields.vector = vector;
3167 irte->fields.destination = dest_apicid;
3168 modify_irte(iommu, devid, index, irte);
3171 static void irte_ga_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
3172 u8 vector, u32 dest_apicid)
3174 struct irte_ga *irte = (struct irte_ga *) entry;
3176 if (!irte->lo.fields_remap.guest_mode) {
3177 irte->hi.fields.vector = vector;
3178 irte->lo.fields_remap.destination =
3179 APICID_TO_IRTE_DEST_LO(dest_apicid);
3180 irte->hi.fields.destination =
3181 APICID_TO_IRTE_DEST_HI(dest_apicid);
3182 modify_irte_ga(iommu, devid, index, irte, NULL);
3186 #define IRTE_ALLOCATED (~1U)
3187 static void irte_set_allocated(struct irq_remap_table *table, int index)
3189 table->table[index] = IRTE_ALLOCATED;
3192 static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3194 struct irte_ga *ptr = (struct irte_ga *)table->table;
3195 struct irte_ga *irte = &ptr[index];
3197 memset(&irte->lo.val, 0, sizeof(u64));
3198 memset(&irte->hi.val, 0, sizeof(u64));
3199 irte->hi.fields.vector = 0xff;
3202 static bool irte_is_allocated(struct irq_remap_table *table, int index)
3204 union irte *ptr = (union irte *)table->table;
3205 union irte *irte = &ptr[index];
3207 return irte->val != 0;
3210 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3212 struct irte_ga *ptr = (struct irte_ga *)table->table;
3213 struct irte_ga *irte = &ptr[index];
3215 return irte->hi.fields.vector != 0;
3218 static void irte_clear_allocated(struct irq_remap_table *table, int index)
3220 table->table[index] = 0;
3223 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3225 struct irte_ga *ptr = (struct irte_ga *)table->table;
3226 struct irte_ga *irte = &ptr[index];
3228 memset(&irte->lo.val, 0, sizeof(u64));
3229 memset(&irte->hi.val, 0, sizeof(u64));
3232 static int get_devid(struct irq_alloc_info *info)
3234 switch (info->type) {
3235 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3236 return get_ioapic_devid(info->devid);
3237 case X86_IRQ_ALLOC_TYPE_HPET:
3238 return get_hpet_devid(info->devid);
3239 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3240 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3241 return get_device_sbdf_id(msi_desc_to_dev(info->desc));
3248 struct irq_remap_ops amd_iommu_irq_ops = {
3249 .prepare = amd_iommu_prepare,
3250 .enable = amd_iommu_enable,
3251 .disable = amd_iommu_disable,
3252 .reenable = amd_iommu_reenable,
3253 .enable_faulting = amd_iommu_enable_faulting,
3256 static void fill_msi_msg(struct msi_msg *msg, u32 index)
3259 msg->address_lo = 0;
3260 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
3261 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
3264 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3265 struct irq_cfg *irq_cfg,
3266 struct irq_alloc_info *info,
3267 int devid, int index, int sub_handle)
3269 struct irq_2_irte *irte_info = &data->irq_2_irte;
3270 struct amd_iommu *iommu = data->iommu;
3275 data->irq_2_irte.devid = devid;
3276 data->irq_2_irte.index = index + sub_handle;
3277 iommu->irte_ops->prepare(data->entry, apic->delivery_mode,
3278 apic->dest_mode_logical, irq_cfg->vector,
3279 irq_cfg->dest_apicid, devid);
3281 switch (info->type) {
3282 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3283 case X86_IRQ_ALLOC_TYPE_HPET:
3284 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
3285 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
3286 fill_msi_msg(&data->msi_entry, irte_info->index);
3295 struct amd_irte_ops irte_32_ops = {
3296 .prepare = irte_prepare,
3297 .activate = irte_activate,
3298 .deactivate = irte_deactivate,
3299 .set_affinity = irte_set_affinity,
3300 .set_allocated = irte_set_allocated,
3301 .is_allocated = irte_is_allocated,
3302 .clear_allocated = irte_clear_allocated,
3305 struct amd_irte_ops irte_128_ops = {
3306 .prepare = irte_ga_prepare,
3307 .activate = irte_ga_activate,
3308 .deactivate = irte_ga_deactivate,
3309 .set_affinity = irte_ga_set_affinity,
3310 .set_allocated = irte_ga_set_allocated,
3311 .is_allocated = irte_ga_is_allocated,
3312 .clear_allocated = irte_ga_clear_allocated,
3315 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3316 unsigned int nr_irqs, void *arg)
3318 struct irq_alloc_info *info = arg;
3319 struct irq_data *irq_data;
3320 struct amd_ir_data *data = NULL;
3321 struct amd_iommu *iommu;
3322 struct irq_cfg *cfg;
3323 int i, ret, devid, seg, sbdf;
3328 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI)
3331 sbdf = get_devid(info);
3335 seg = PCI_SBDF_TO_SEGID(sbdf);
3336 devid = PCI_SBDF_TO_DEVID(sbdf);
3337 iommu = __rlookup_amd_iommu(seg, devid);
3341 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3345 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3346 struct irq_remap_table *table;
3348 table = alloc_irq_table(iommu, devid, NULL);
3350 if (!table->min_index) {
3352 * Keep the first 32 indexes free for IOAPIC
3355 table->min_index = 32;
3356 for (i = 0; i < 32; ++i)
3357 iommu->irte_ops->set_allocated(table, i);
3359 WARN_ON(table->min_index != 32);
3360 index = info->ioapic.pin;
3364 } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
3365 info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
3366 bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
3368 index = alloc_irq_index(iommu, devid, nr_irqs, align,
3369 msi_desc_to_pci_dev(info->desc));
3371 index = alloc_irq_index(iommu, devid, nr_irqs, false, NULL);
3375 pr_warn("Failed to allocate IRTE\n");
3377 goto out_free_parent;
3380 for (i = 0; i < nr_irqs; i++) {
3381 irq_data = irq_domain_get_irq_data(domain, virq + i);
3382 cfg = irq_data ? irqd_cfg(irq_data) : NULL;
3389 data = kzalloc(sizeof(*data), GFP_KERNEL);
3393 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3394 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
3396 data->entry = kzalloc(sizeof(struct irte_ga),
3403 data->iommu = iommu;
3404 irq_data->hwirq = (devid << 16) + i;
3405 irq_data->chip_data = data;
3406 irq_data->chip = &amd_ir_chip;
3407 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3408 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3414 for (i--; i >= 0; i--) {
3415 irq_data = irq_domain_get_irq_data(domain, virq + i);
3417 kfree(irq_data->chip_data);
3419 for (i = 0; i < nr_irqs; i++)
3420 free_irte(iommu, devid, index + i);
3422 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3426 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3427 unsigned int nr_irqs)
3429 struct irq_2_irte *irte_info;
3430 struct irq_data *irq_data;
3431 struct amd_ir_data *data;
3434 for (i = 0; i < nr_irqs; i++) {
3435 irq_data = irq_domain_get_irq_data(domain, virq + i);
3436 if (irq_data && irq_data->chip_data) {
3437 data = irq_data->chip_data;
3438 irte_info = &data->irq_2_irte;
3439 free_irte(data->iommu, irte_info->devid, irte_info->index);
3444 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3447 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3448 struct amd_ir_data *ir_data,
3449 struct irq_2_irte *irte_info,
3450 struct irq_cfg *cfg);
3452 static int irq_remapping_activate(struct irq_domain *domain,
3453 struct irq_data *irq_data, bool reserve)
3455 struct amd_ir_data *data = irq_data->chip_data;
3456 struct irq_2_irte *irte_info = &data->irq_2_irte;
3457 struct amd_iommu *iommu = data->iommu;
3458 struct irq_cfg *cfg = irqd_cfg(irq_data);
3463 iommu->irte_ops->activate(iommu, data->entry, irte_info->devid,
3465 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
3469 static void irq_remapping_deactivate(struct irq_domain *domain,
3470 struct irq_data *irq_data)
3472 struct amd_ir_data *data = irq_data->chip_data;
3473 struct irq_2_irte *irte_info = &data->irq_2_irte;
3474 struct amd_iommu *iommu = data->iommu;
3477 iommu->irte_ops->deactivate(iommu, data->entry, irte_info->devid,
3481 static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec,
3482 enum irq_domain_bus_token bus_token)
3484 struct amd_iommu *iommu;
3487 if (!amd_iommu_irq_remap)
3490 if (x86_fwspec_is_ioapic(fwspec))
3491 devid = get_ioapic_devid(fwspec->param[0]);
3492 else if (x86_fwspec_is_hpet(fwspec))
3493 devid = get_hpet_devid(fwspec->param[0]);
3497 iommu = __rlookup_amd_iommu((devid >> 16), (devid & 0xffff));
3499 return iommu && iommu->ir_domain == d;
3502 static const struct irq_domain_ops amd_ir_domain_ops = {
3503 .select = irq_remapping_select,
3504 .alloc = irq_remapping_alloc,
3505 .free = irq_remapping_free,
3506 .activate = irq_remapping_activate,
3507 .deactivate = irq_remapping_deactivate,
3510 int amd_iommu_activate_guest_mode(void *data)
3512 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3513 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3516 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) || !entry)
3519 valid = entry->lo.fields_vapic.valid;
3524 entry->lo.fields_vapic.valid = valid;
3525 entry->lo.fields_vapic.guest_mode = 1;
3526 entry->lo.fields_vapic.ga_log_intr = 1;
3527 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
3528 entry->hi.fields.vector = ir_data->ga_vector;
3529 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
3531 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
3532 ir_data->irq_2_irte.index, entry, ir_data);
3534 EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
3536 int amd_iommu_deactivate_guest_mode(void *data)
3538 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3539 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3540 struct irq_cfg *cfg = ir_data->cfg;
3543 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3544 !entry || !entry->lo.fields_vapic.guest_mode)
3547 valid = entry->lo.fields_remap.valid;
3552 entry->lo.fields_remap.valid = valid;
3553 entry->lo.fields_remap.dm = apic->dest_mode_logical;
3554 entry->lo.fields_remap.int_type = apic->delivery_mode;
3555 entry->hi.fields.vector = cfg->vector;
3556 entry->lo.fields_remap.destination =
3557 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
3558 entry->hi.fields.destination =
3559 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
3561 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
3562 ir_data->irq_2_irte.index, entry, ir_data);
3564 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
3566 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
3569 struct amd_iommu_pi_data *pi_data = vcpu_info;
3570 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
3571 struct amd_ir_data *ir_data = data->chip_data;
3572 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3573 struct iommu_dev_data *dev_data;
3575 if (ir_data->iommu == NULL)
3578 dev_data = search_dev_data(ir_data->iommu, irte_info->devid);
3581 * This device has never been set up for guest mode.
3582 * we should not modify the IRTE
3584 if (!dev_data || !dev_data->use_vapic)
3587 ir_data->cfg = irqd_cfg(data);
3588 pi_data->ir_data = ir_data;
3591 * SVM tries to set up for VAPIC mode, but we are in
3592 * legacy mode. So, we force legacy mode instead.
3594 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3595 pr_debug("%s: Fall back to using intr legacy remap\n",
3597 pi_data->is_guest_mode = false;
3600 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
3601 if (pi_data->is_guest_mode) {
3602 ir_data->ga_root_ptr = (pi_data->base >> 12);
3603 ir_data->ga_vector = vcpu_pi_info->vector;
3604 ir_data->ga_tag = pi_data->ga_tag;
3605 ret = amd_iommu_activate_guest_mode(ir_data);
3607 ir_data->cached_ga_tag = pi_data->ga_tag;
3609 ret = amd_iommu_deactivate_guest_mode(ir_data);
3612 * This communicates the ga_tag back to the caller
3613 * so that it can do all the necessary clean up.
3616 ir_data->cached_ga_tag = 0;
3623 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3624 struct amd_ir_data *ir_data,
3625 struct irq_2_irte *irte_info,
3626 struct irq_cfg *cfg)
3630 * Atomically updates the IRTE with the new destination, vector
3631 * and flushes the interrupt entry cache.
3633 iommu->irte_ops->set_affinity(iommu, ir_data->entry, irte_info->devid,
3634 irte_info->index, cfg->vector,
3638 static int amd_ir_set_affinity(struct irq_data *data,
3639 const struct cpumask *mask, bool force)
3641 struct amd_ir_data *ir_data = data->chip_data;
3642 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3643 struct irq_cfg *cfg = irqd_cfg(data);
3644 struct irq_data *parent = data->parent_data;
3645 struct amd_iommu *iommu = ir_data->iommu;
3651 ret = parent->chip->irq_set_affinity(parent, mask, force);
3652 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3655 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
3657 * After this point, all the interrupts will start arriving
3658 * at the new destination. So, time to cleanup the previous
3659 * vector allocation.
3661 send_cleanup_vector(cfg);
3663 return IRQ_SET_MASK_OK_DONE;
3666 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3668 struct amd_ir_data *ir_data = irq_data->chip_data;
3670 *msg = ir_data->msi_entry;
3673 static struct irq_chip amd_ir_chip = {
3675 .irq_ack = apic_ack_irq,
3676 .irq_set_affinity = amd_ir_set_affinity,
3677 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
3678 .irq_compose_msi_msg = ir_compose_msi_msg,
3681 static const struct msi_parent_ops amdvi_msi_parent_ops = {
3682 .supported_flags = X86_VECTOR_MSI_FLAGS_SUPPORTED |
3683 MSI_FLAG_MULTI_PCI_MSI |
3686 .init_dev_msi_info = msi_parent_init_dev_msi_info,
3689 static const struct msi_parent_ops virt_amdvi_msi_parent_ops = {
3690 .supported_flags = X86_VECTOR_MSI_FLAGS_SUPPORTED |
3691 MSI_FLAG_MULTI_PCI_MSI,
3693 .init_dev_msi_info = msi_parent_init_dev_msi_info,
3696 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3698 struct fwnode_handle *fn;
3700 fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
3703 iommu->ir_domain = irq_domain_create_hierarchy(arch_get_ir_parent_domain(), 0, 0,
3704 fn, &amd_ir_domain_ops, iommu);
3705 if (!iommu->ir_domain) {
3706 irq_domain_free_fwnode(fn);
3710 irq_domain_update_bus_token(iommu->ir_domain, DOMAIN_BUS_AMDVI);
3711 iommu->ir_domain->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT |
3712 IRQ_DOMAIN_FLAG_ISOLATED_MSI;
3714 if (amd_iommu_np_cache)
3715 iommu->ir_domain->msi_parent_ops = &virt_amdvi_msi_parent_ops;
3717 iommu->ir_domain->msi_parent_ops = &amdvi_msi_parent_ops;
3722 int amd_iommu_update_ga(int cpu, bool is_run, void *data)
3724 unsigned long flags;
3725 struct amd_iommu *iommu;
3726 struct irq_remap_table *table;
3727 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3728 int devid = ir_data->irq_2_irte.devid;
3729 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3730 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
3732 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3733 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
3736 iommu = ir_data->iommu;
3740 table = get_irq_table(iommu, devid);
3744 raw_spin_lock_irqsave(&table->lock, flags);
3746 if (ref->lo.fields_vapic.guest_mode) {
3748 ref->lo.fields_vapic.destination =
3749 APICID_TO_IRTE_DEST_LO(cpu);
3750 ref->hi.fields.destination =
3751 APICID_TO_IRTE_DEST_HI(cpu);
3753 ref->lo.fields_vapic.is_run = is_run;
3757 raw_spin_unlock_irqrestore(&table->lock, flags);
3759 iommu_flush_irt(iommu, devid);
3760 iommu_completion_wait(iommu);
3763 EXPORT_SYMBOL(amd_iommu_update_ga);