treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iommu / amd_iommu.c
blob7a6c056b9b9cc95daffb493f9b9775b9cb8c7094
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 * Leo Duran <leo.duran@amd.com>
6 */
8 #define pr_fmt(fmt) "AMD-Vi: " fmt
9 #define dev_fmt(fmt) pr_fmt(fmt)
11 #include <linux/ratelimit.h>
12 #include <linux/pci.h>
13 #include <linux/acpi.h>
14 #include <linux/amba/bus.h>
15 #include <linux/platform_device.h>
16 #include <linux/pci-ats.h>
17 #include <linux/bitmap.h>
18 #include <linux/slab.h>
19 #include <linux/debugfs.h>
20 #include <linux/scatterlist.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/dma-direct.h>
23 #include <linux/dma-iommu.h>
24 #include <linux/iommu-helper.h>
25 #include <linux/iommu.h>
26 #include <linux/delay.h>
27 #include <linux/amd-iommu.h>
28 #include <linux/notifier.h>
29 #include <linux/export.h>
30 #include <linux/irq.h>
31 #include <linux/msi.h>
32 #include <linux/dma-contiguous.h>
33 #include <linux/irqdomain.h>
34 #include <linux/percpu.h>
35 #include <linux/iova.h>
36 #include <asm/irq_remapping.h>
37 #include <asm/io_apic.h>
38 #include <asm/apic.h>
39 #include <asm/hw_irq.h>
40 #include <asm/msidef.h>
41 #include <asm/proto.h>
42 #include <asm/iommu.h>
43 #include <asm/gart.h>
44 #include <asm/dma.h>
46 #include "amd_iommu_proto.h"
47 #include "amd_iommu_types.h"
48 #include "irq_remapping.h"
50 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
52 #define LOOP_TIMEOUT 100000
54 /* IO virtual address start page frame number */
55 #define IOVA_START_PFN (1)
56 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
58 /* Reserved IOVA ranges */
59 #define MSI_RANGE_START (0xfee00000)
60 #define MSI_RANGE_END (0xfeefffff)
61 #define HT_RANGE_START (0xfd00000000ULL)
62 #define HT_RANGE_END (0xffffffffffULL)
65 * This bitmap is used to advertise the page sizes our hardware support
66 * to the IOMMU core, which will then use this information to split
67 * physically contiguous memory regions it is mapping into page sizes
68 * that we support.
70 * 512GB Pages are not supported due to a hardware bug
72 #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
74 static DEFINE_SPINLOCK(pd_bitmap_lock);
76 /* List of all available dev_data structures */
77 static LLIST_HEAD(dev_data_list);
79 LIST_HEAD(ioapic_map);
80 LIST_HEAD(hpet_map);
81 LIST_HEAD(acpihid_map);
84 * Domain for untranslated devices - only allocated
85 * if iommu=pt passed on kernel cmd line.
87 const struct iommu_ops amd_iommu_ops;
89 static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
90 int amd_iommu_max_glx_val = -1;
93 * general struct to manage commands send to an IOMMU
95 struct iommu_cmd {
96 u32 data[4];
99 struct kmem_cache *amd_iommu_irq_cache;
101 static void update_domain(struct protection_domain *domain);
102 static int protection_domain_init(struct protection_domain *domain);
103 static void detach_device(struct device *dev);
105 /****************************************************************************
107 * Helper functions
109 ****************************************************************************/
111 static inline u16 get_pci_device_id(struct device *dev)
113 struct pci_dev *pdev = to_pci_dev(dev);
115 return pci_dev_id(pdev);
118 static inline int get_acpihid_device_id(struct device *dev,
119 struct acpihid_map_entry **entry)
121 struct acpi_device *adev = ACPI_COMPANION(dev);
122 struct acpihid_map_entry *p;
124 if (!adev)
125 return -ENODEV;
127 list_for_each_entry(p, &acpihid_map, list) {
128 if (acpi_dev_hid_uid_match(adev, p->hid, p->uid)) {
129 if (entry)
130 *entry = p;
131 return p->devid;
134 return -EINVAL;
137 static inline int get_device_id(struct device *dev)
139 int devid;
141 if (dev_is_pci(dev))
142 devid = get_pci_device_id(dev);
143 else
144 devid = get_acpihid_device_id(dev, NULL);
146 return devid;
149 static struct protection_domain *to_pdomain(struct iommu_domain *dom)
151 return container_of(dom, struct protection_domain, domain);
154 static struct iommu_dev_data *alloc_dev_data(u16 devid)
156 struct iommu_dev_data *dev_data;
158 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
159 if (!dev_data)
160 return NULL;
162 spin_lock_init(&dev_data->lock);
163 dev_data->devid = devid;
164 ratelimit_default_init(&dev_data->rs);
166 llist_add(&dev_data->dev_data_list, &dev_data_list);
167 return dev_data;
170 static struct iommu_dev_data *search_dev_data(u16 devid)
172 struct iommu_dev_data *dev_data;
173 struct llist_node *node;
175 if (llist_empty(&dev_data_list))
176 return NULL;
178 node = dev_data_list.first;
179 llist_for_each_entry(dev_data, node, dev_data_list) {
180 if (dev_data->devid == devid)
181 return dev_data;
184 return NULL;
187 static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
189 u16 devid = pci_dev_id(pdev);
191 if (devid == alias)
192 return 0;
194 amd_iommu_rlookup_table[alias] =
195 amd_iommu_rlookup_table[devid];
196 memcpy(amd_iommu_dev_table[alias].data,
197 amd_iommu_dev_table[devid].data,
198 sizeof(amd_iommu_dev_table[alias].data));
200 return 0;
203 static void clone_aliases(struct pci_dev *pdev)
205 if (!pdev)
206 return;
209 * The IVRS alias stored in the alias table may not be
210 * part of the PCI DMA aliases if it's bus differs
211 * from the original device.
213 clone_alias(pdev, amd_iommu_alias_table[pci_dev_id(pdev)], NULL);
215 pci_for_each_dma_alias(pdev, clone_alias, NULL);
218 static struct pci_dev *setup_aliases(struct device *dev)
220 struct pci_dev *pdev = to_pci_dev(dev);
221 u16 ivrs_alias;
223 /* For ACPI HID devices, there are no aliases */
224 if (!dev_is_pci(dev))
225 return NULL;
228 * Add the IVRS alias to the pci aliases if it is on the same
229 * bus. The IVRS table may know about a quirk that we don't.
231 ivrs_alias = amd_iommu_alias_table[pci_dev_id(pdev)];
232 if (ivrs_alias != pci_dev_id(pdev) &&
233 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number)
234 pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1);
236 clone_aliases(pdev);
238 return pdev;
241 static struct iommu_dev_data *find_dev_data(u16 devid)
243 struct iommu_dev_data *dev_data;
244 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
246 dev_data = search_dev_data(devid);
248 if (dev_data == NULL) {
249 dev_data = alloc_dev_data(devid);
250 if (!dev_data)
251 return NULL;
253 if (translation_pre_enabled(iommu))
254 dev_data->defer_attach = true;
257 return dev_data;
260 struct iommu_dev_data *get_dev_data(struct device *dev)
262 return dev->archdata.iommu;
264 EXPORT_SYMBOL(get_dev_data);
267 * Find or create an IOMMU group for a acpihid device.
269 static struct iommu_group *acpihid_device_group(struct device *dev)
271 struct acpihid_map_entry *p, *entry = NULL;
272 int devid;
274 devid = get_acpihid_device_id(dev, &entry);
275 if (devid < 0)
276 return ERR_PTR(devid);
278 list_for_each_entry(p, &acpihid_map, list) {
279 if ((devid == p->devid) && p->group)
280 entry->group = p->group;
283 if (!entry->group)
284 entry->group = generic_device_group(dev);
285 else
286 iommu_group_ref_get(entry->group);
288 return entry->group;
291 static bool pci_iommuv2_capable(struct pci_dev *pdev)
293 static const int caps[] = {
294 PCI_EXT_CAP_ID_ATS,
295 PCI_EXT_CAP_ID_PRI,
296 PCI_EXT_CAP_ID_PASID,
298 int i, pos;
300 if (pci_ats_disabled())
301 return false;
303 for (i = 0; i < 3; ++i) {
304 pos = pci_find_ext_capability(pdev, caps[i]);
305 if (pos == 0)
306 return false;
309 return true;
312 static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
314 struct iommu_dev_data *dev_data;
316 dev_data = get_dev_data(&pdev->dev);
318 return dev_data->errata & (1 << erratum) ? true : false;
322 * This function checks if the driver got a valid device from the caller to
323 * avoid dereferencing invalid pointers.
325 static bool check_device(struct device *dev)
327 int devid;
329 if (!dev || !dev->dma_mask)
330 return false;
332 devid = get_device_id(dev);
333 if (devid < 0)
334 return false;
336 /* Out of our scope? */
337 if (devid > amd_iommu_last_bdf)
338 return false;
340 if (amd_iommu_rlookup_table[devid] == NULL)
341 return false;
343 return true;
346 static void init_iommu_group(struct device *dev)
348 struct iommu_group *group;
350 group = iommu_group_get_for_dev(dev);
351 if (IS_ERR(group))
352 return;
354 iommu_group_put(group);
357 static int iommu_init_device(struct device *dev)
359 struct iommu_dev_data *dev_data;
360 struct amd_iommu *iommu;
361 int devid;
363 if (dev->archdata.iommu)
364 return 0;
366 devid = get_device_id(dev);
367 if (devid < 0)
368 return devid;
370 iommu = amd_iommu_rlookup_table[devid];
372 dev_data = find_dev_data(devid);
373 if (!dev_data)
374 return -ENOMEM;
376 dev_data->pdev = setup_aliases(dev);
379 * By default we use passthrough mode for IOMMUv2 capable device.
380 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
381 * invalid address), we ignore the capability for the device so
382 * it'll be forced to go into translation mode.
384 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
385 dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
386 struct amd_iommu *iommu;
388 iommu = amd_iommu_rlookup_table[dev_data->devid];
389 dev_data->iommu_v2 = iommu->is_iommu_v2;
392 dev->archdata.iommu = dev_data;
394 iommu_device_link(&iommu->iommu, dev);
396 return 0;
399 static void iommu_ignore_device(struct device *dev)
401 int devid;
403 devid = get_device_id(dev);
404 if (devid < 0)
405 return;
407 amd_iommu_rlookup_table[devid] = NULL;
408 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
410 setup_aliases(dev);
413 static void iommu_uninit_device(struct device *dev)
415 struct iommu_dev_data *dev_data;
416 struct amd_iommu *iommu;
417 int devid;
419 devid = get_device_id(dev);
420 if (devid < 0)
421 return;
423 iommu = amd_iommu_rlookup_table[devid];
425 dev_data = search_dev_data(devid);
426 if (!dev_data)
427 return;
429 if (dev_data->domain)
430 detach_device(dev);
432 iommu_device_unlink(&iommu->iommu, dev);
434 iommu_group_remove_device(dev);
436 /* Remove dma-ops */
437 dev->dma_ops = NULL;
440 * We keep dev_data around for unplugged devices and reuse it when the
441 * device is re-plugged - not doing so would introduce a ton of races.
446 * Helper function to get the first pte of a large mapping
448 static u64 *first_pte_l7(u64 *pte, unsigned long *page_size,
449 unsigned long *count)
451 unsigned long pte_mask, pg_size, cnt;
452 u64 *fpte;
454 pg_size = PTE_PAGE_SIZE(*pte);
455 cnt = PAGE_SIZE_PTE_COUNT(pg_size);
456 pte_mask = ~((cnt << 3) - 1);
457 fpte = (u64 *)(((unsigned long)pte) & pte_mask);
459 if (page_size)
460 *page_size = pg_size;
462 if (count)
463 *count = cnt;
465 return fpte;
468 /****************************************************************************
470 * Interrupt handling functions
472 ****************************************************************************/
474 static void dump_dte_entry(u16 devid)
476 int i;
478 for (i = 0; i < 4; ++i)
479 pr_err("DTE[%d]: %016llx\n", i,
480 amd_iommu_dev_table[devid].data[i]);
483 static void dump_command(unsigned long phys_addr)
485 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
486 int i;
488 for (i = 0; i < 4; ++i)
489 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
492 static void amd_iommu_report_page_fault(u16 devid, u16 domain_id,
493 u64 address, int flags)
495 struct iommu_dev_data *dev_data = NULL;
496 struct pci_dev *pdev;
498 pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
499 devid & 0xff);
500 if (pdev)
501 dev_data = get_dev_data(&pdev->dev);
503 if (dev_data && __ratelimit(&dev_data->rs)) {
504 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
505 domain_id, address, flags);
506 } else if (printk_ratelimit()) {
507 pr_err("Event logged [IO_PAGE_FAULT device=%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
508 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
509 domain_id, address, flags);
512 if (pdev)
513 pci_dev_put(pdev);
516 static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
518 struct device *dev = iommu->iommu.dev;
519 int type, devid, pasid, flags, tag;
520 volatile u32 *event = __evt;
521 int count = 0;
522 u64 address;
524 retry:
525 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
526 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
527 pasid = (event[0] & EVENT_DOMID_MASK_HI) |
528 (event[1] & EVENT_DOMID_MASK_LO);
529 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
530 address = (u64)(((u64)event[3]) << 32) | event[2];
532 if (type == 0) {
533 /* Did we hit the erratum? */
534 if (++count == LOOP_TIMEOUT) {
535 pr_err("No event written to event log\n");
536 return;
538 udelay(1);
539 goto retry;
542 if (type == EVENT_TYPE_IO_FAULT) {
543 amd_iommu_report_page_fault(devid, pasid, address, flags);
544 return;
547 switch (type) {
548 case EVENT_TYPE_ILL_DEV:
549 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
550 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
551 pasid, address, flags);
552 dump_dte_entry(devid);
553 break;
554 case EVENT_TYPE_DEV_TAB_ERR:
555 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
556 "address=0x%llx flags=0x%04x]\n",
557 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
558 address, flags);
559 break;
560 case EVENT_TYPE_PAGE_TAB_ERR:
561 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
562 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
563 pasid, address, flags);
564 break;
565 case EVENT_TYPE_ILL_CMD:
566 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
567 dump_command(address);
568 break;
569 case EVENT_TYPE_CMD_HARD_ERR:
570 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
571 address, flags);
572 break;
573 case EVENT_TYPE_IOTLB_INV_TO:
574 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%02x:%02x.%x address=0x%llx]\n",
575 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
576 address);
577 break;
578 case EVENT_TYPE_INV_DEV_REQ:
579 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
580 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
581 pasid, address, flags);
582 break;
583 case EVENT_TYPE_INV_PPR_REQ:
584 pasid = PPR_PASID(*((u64 *)__evt));
585 tag = event[1] & 0x03FF;
586 dev_err(dev, "Event logged [INVALID_PPR_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n",
587 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
588 pasid, address, flags, tag);
589 break;
590 default:
591 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
592 event[0], event[1], event[2], event[3]);
595 memset(__evt, 0, 4 * sizeof(u32));
598 static void iommu_poll_events(struct amd_iommu *iommu)
600 u32 head, tail;
602 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
603 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
605 while (head != tail) {
606 iommu_print_event(iommu, iommu->evt_buf + head);
607 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
610 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
613 static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
615 struct amd_iommu_fault fault;
617 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
618 pr_err_ratelimited("Unknown PPR request received\n");
619 return;
622 fault.address = raw[1];
623 fault.pasid = PPR_PASID(raw[0]);
624 fault.device_id = PPR_DEVID(raw[0]);
625 fault.tag = PPR_TAG(raw[0]);
626 fault.flags = PPR_FLAGS(raw[0]);
628 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
631 static void iommu_poll_ppr_log(struct amd_iommu *iommu)
633 u32 head, tail;
635 if (iommu->ppr_log == NULL)
636 return;
638 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
639 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
641 while (head != tail) {
642 volatile u64 *raw;
643 u64 entry[2];
644 int i;
646 raw = (u64 *)(iommu->ppr_log + head);
649 * Hardware bug: Interrupt may arrive before the entry is
650 * written to memory. If this happens we need to wait for the
651 * entry to arrive.
653 for (i = 0; i < LOOP_TIMEOUT; ++i) {
654 if (PPR_REQ_TYPE(raw[0]) != 0)
655 break;
656 udelay(1);
659 /* Avoid memcpy function-call overhead */
660 entry[0] = raw[0];
661 entry[1] = raw[1];
664 * To detect the hardware bug we need to clear the entry
665 * back to zero.
667 raw[0] = raw[1] = 0UL;
669 /* Update head pointer of hardware ring-buffer */
670 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
671 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
673 /* Handle PPR entry */
674 iommu_handle_ppr_entry(iommu, entry);
676 /* Refresh ring-buffer information */
677 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
678 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
682 #ifdef CONFIG_IRQ_REMAP
683 static int (*iommu_ga_log_notifier)(u32);
685 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
687 iommu_ga_log_notifier = notifier;
689 return 0;
691 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
693 static void iommu_poll_ga_log(struct amd_iommu *iommu)
695 u32 head, tail, cnt = 0;
697 if (iommu->ga_log == NULL)
698 return;
700 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
701 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
703 while (head != tail) {
704 volatile u64 *raw;
705 u64 log_entry;
707 raw = (u64 *)(iommu->ga_log + head);
708 cnt++;
710 /* Avoid memcpy function-call overhead */
711 log_entry = *raw;
713 /* Update head pointer of hardware ring-buffer */
714 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
715 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
717 /* Handle GA entry */
718 switch (GA_REQ_TYPE(log_entry)) {
719 case GA_GUEST_NR:
720 if (!iommu_ga_log_notifier)
721 break;
723 pr_debug("%s: devid=%#x, ga_tag=%#x\n",
724 __func__, GA_DEVID(log_entry),
725 GA_TAG(log_entry));
727 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
728 pr_err("GA log notifier failed.\n");
729 break;
730 default:
731 break;
735 #endif /* CONFIG_IRQ_REMAP */
737 #define AMD_IOMMU_INT_MASK \
738 (MMIO_STATUS_EVT_INT_MASK | \
739 MMIO_STATUS_PPR_INT_MASK | \
740 MMIO_STATUS_GALOG_INT_MASK)
742 irqreturn_t amd_iommu_int_thread(int irq, void *data)
744 struct amd_iommu *iommu = (struct amd_iommu *) data;
745 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
747 while (status & AMD_IOMMU_INT_MASK) {
748 /* Enable EVT and PPR and GA interrupts again */
749 writel(AMD_IOMMU_INT_MASK,
750 iommu->mmio_base + MMIO_STATUS_OFFSET);
752 if (status & MMIO_STATUS_EVT_INT_MASK) {
753 pr_devel("Processing IOMMU Event Log\n");
754 iommu_poll_events(iommu);
757 if (status & MMIO_STATUS_PPR_INT_MASK) {
758 pr_devel("Processing IOMMU PPR Log\n");
759 iommu_poll_ppr_log(iommu);
762 #ifdef CONFIG_IRQ_REMAP
763 if (status & MMIO_STATUS_GALOG_INT_MASK) {
764 pr_devel("Processing IOMMU GA Log\n");
765 iommu_poll_ga_log(iommu);
767 #endif
770 * Hardware bug: ERBT1312
771 * When re-enabling interrupt (by writing 1
772 * to clear the bit), the hardware might also try to set
773 * the interrupt bit in the event status register.
774 * In this scenario, the bit will be set, and disable
775 * subsequent interrupts.
777 * Workaround: The IOMMU driver should read back the
778 * status register and check if the interrupt bits are cleared.
779 * If not, driver will need to go through the interrupt handler
780 * again and re-clear the bits
782 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
784 return IRQ_HANDLED;
787 irqreturn_t amd_iommu_int_handler(int irq, void *data)
789 return IRQ_WAKE_THREAD;
792 /****************************************************************************
794 * IOMMU command queuing functions
796 ****************************************************************************/
798 static int wait_on_sem(volatile u64 *sem)
800 int i = 0;
802 while (*sem == 0 && i < LOOP_TIMEOUT) {
803 udelay(1);
804 i += 1;
807 if (i == LOOP_TIMEOUT) {
808 pr_alert("Completion-Wait loop timed out\n");
809 return -EIO;
812 return 0;
815 static void copy_cmd_to_buffer(struct amd_iommu *iommu,
816 struct iommu_cmd *cmd)
818 u8 *target;
819 u32 tail;
821 /* Copy command to buffer */
822 tail = iommu->cmd_buf_tail;
823 target = iommu->cmd_buf + tail;
824 memcpy(target, cmd, sizeof(*cmd));
826 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
827 iommu->cmd_buf_tail = tail;
829 /* Tell the IOMMU about it */
830 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
833 static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
835 u64 paddr = iommu_virt_to_phys((void *)address);
837 WARN_ON(address & 0x7ULL);
839 memset(cmd, 0, sizeof(*cmd));
840 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
841 cmd->data[1] = upper_32_bits(paddr);
842 cmd->data[2] = 1;
843 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
846 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
848 memset(cmd, 0, sizeof(*cmd));
849 cmd->data[0] = devid;
850 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
853 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
854 size_t size, u16 domid, int pde)
856 u64 pages;
857 bool s;
859 pages = iommu_num_pages(address, size, PAGE_SIZE);
860 s = false;
862 if (pages > 1) {
864 * If we have to flush more than one page, flush all
865 * TLB entries for this domain
867 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
868 s = true;
871 address &= PAGE_MASK;
873 memset(cmd, 0, sizeof(*cmd));
874 cmd->data[1] |= domid;
875 cmd->data[2] = lower_32_bits(address);
876 cmd->data[3] = upper_32_bits(address);
877 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
878 if (s) /* size bit - we flush more than one 4kb page */
879 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
880 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
881 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
884 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
885 u64 address, size_t size)
887 u64 pages;
888 bool s;
890 pages = iommu_num_pages(address, size, PAGE_SIZE);
891 s = false;
893 if (pages > 1) {
895 * If we have to flush more than one page, flush all
896 * TLB entries for this domain
898 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
899 s = true;
902 address &= PAGE_MASK;
904 memset(cmd, 0, sizeof(*cmd));
905 cmd->data[0] = devid;
906 cmd->data[0] |= (qdep & 0xff) << 24;
907 cmd->data[1] = devid;
908 cmd->data[2] = lower_32_bits(address);
909 cmd->data[3] = upper_32_bits(address);
910 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
911 if (s)
912 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
915 static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
916 u64 address, bool size)
918 memset(cmd, 0, sizeof(*cmd));
920 address &= ~(0xfffULL);
922 cmd->data[0] = pasid;
923 cmd->data[1] = domid;
924 cmd->data[2] = lower_32_bits(address);
925 cmd->data[3] = upper_32_bits(address);
926 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
927 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
928 if (size)
929 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
930 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
933 static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
934 int qdep, u64 address, bool size)
936 memset(cmd, 0, sizeof(*cmd));
938 address &= ~(0xfffULL);
940 cmd->data[0] = devid;
941 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
942 cmd->data[0] |= (qdep & 0xff) << 24;
943 cmd->data[1] = devid;
944 cmd->data[1] |= (pasid & 0xff) << 16;
945 cmd->data[2] = lower_32_bits(address);
946 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
947 cmd->data[3] = upper_32_bits(address);
948 if (size)
949 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
950 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
953 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
954 int status, int tag, bool gn)
956 memset(cmd, 0, sizeof(*cmd));
958 cmd->data[0] = devid;
959 if (gn) {
960 cmd->data[1] = pasid;
961 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
963 cmd->data[3] = tag & 0x1ff;
964 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
966 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
969 static void build_inv_all(struct iommu_cmd *cmd)
971 memset(cmd, 0, sizeof(*cmd));
972 CMD_SET_TYPE(cmd, CMD_INV_ALL);
975 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
977 memset(cmd, 0, sizeof(*cmd));
978 cmd->data[0] = devid;
979 CMD_SET_TYPE(cmd, CMD_INV_IRT);
983 * Writes the command to the IOMMUs command buffer and informs the
984 * hardware about the new command.
986 static int __iommu_queue_command_sync(struct amd_iommu *iommu,
987 struct iommu_cmd *cmd,
988 bool sync)
990 unsigned int count = 0;
991 u32 left, next_tail;
993 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
994 again:
995 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
997 if (left <= 0x20) {
998 /* Skip udelay() the first time around */
999 if (count++) {
1000 if (count == LOOP_TIMEOUT) {
1001 pr_err("Command buffer timeout\n");
1002 return -EIO;
1005 udelay(1);
1008 /* Update head and recheck remaining space */
1009 iommu->cmd_buf_head = readl(iommu->mmio_base +
1010 MMIO_CMD_HEAD_OFFSET);
1012 goto again;
1015 copy_cmd_to_buffer(iommu, cmd);
1017 /* Do we need to make sure all commands are processed? */
1018 iommu->need_sync = sync;
1020 return 0;
1023 static int iommu_queue_command_sync(struct amd_iommu *iommu,
1024 struct iommu_cmd *cmd,
1025 bool sync)
1027 unsigned long flags;
1028 int ret;
1030 raw_spin_lock_irqsave(&iommu->lock, flags);
1031 ret = __iommu_queue_command_sync(iommu, cmd, sync);
1032 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1034 return ret;
1037 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1039 return iommu_queue_command_sync(iommu, cmd, true);
1043 * This function queues a completion wait command into the command
1044 * buffer of an IOMMU
1046 static int iommu_completion_wait(struct amd_iommu *iommu)
1048 struct iommu_cmd cmd;
1049 unsigned long flags;
1050 int ret;
1052 if (!iommu->need_sync)
1053 return 0;
1056 build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
1058 raw_spin_lock_irqsave(&iommu->lock, flags);
1060 iommu->cmd_sem = 0;
1062 ret = __iommu_queue_command_sync(iommu, &cmd, false);
1063 if (ret)
1064 goto out_unlock;
1066 ret = wait_on_sem(&iommu->cmd_sem);
1068 out_unlock:
1069 raw_spin_unlock_irqrestore(&iommu->lock, flags);
1071 return ret;
1074 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
1076 struct iommu_cmd cmd;
1078 build_inv_dte(&cmd, devid);
1080 return iommu_queue_command(iommu, &cmd);
1083 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
1085 u32 devid;
1087 for (devid = 0; devid <= 0xffff; ++devid)
1088 iommu_flush_dte(iommu, devid);
1090 iommu_completion_wait(iommu);
1094 * This function uses heavy locking and may disable irqs for some time. But
1095 * this is no issue because it is only called during resume.
1097 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
1099 u32 dom_id;
1101 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1102 struct iommu_cmd cmd;
1103 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1104 dom_id, 1);
1105 iommu_queue_command(iommu, &cmd);
1108 iommu_completion_wait(iommu);
1111 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
1113 struct iommu_cmd cmd;
1115 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1116 dom_id, 1);
1117 iommu_queue_command(iommu, &cmd);
1119 iommu_completion_wait(iommu);
1122 static void amd_iommu_flush_all(struct amd_iommu *iommu)
1124 struct iommu_cmd cmd;
1126 build_inv_all(&cmd);
1128 iommu_queue_command(iommu, &cmd);
1129 iommu_completion_wait(iommu);
1132 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1134 struct iommu_cmd cmd;
1136 build_inv_irt(&cmd, devid);
1138 iommu_queue_command(iommu, &cmd);
1141 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
1143 u32 devid;
1145 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1146 iommu_flush_irt(iommu, devid);
1148 iommu_completion_wait(iommu);
1151 void iommu_flush_all_caches(struct amd_iommu *iommu)
1153 if (iommu_feature(iommu, FEATURE_IA)) {
1154 amd_iommu_flush_all(iommu);
1155 } else {
1156 amd_iommu_flush_dte_all(iommu);
1157 amd_iommu_flush_irt_all(iommu);
1158 amd_iommu_flush_tlb_all(iommu);
1163 * Command send function for flushing on-device TLB
1165 static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1166 u64 address, size_t size)
1168 struct amd_iommu *iommu;
1169 struct iommu_cmd cmd;
1170 int qdep;
1172 qdep = dev_data->ats.qdep;
1173 iommu = amd_iommu_rlookup_table[dev_data->devid];
1175 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
1177 return iommu_queue_command(iommu, &cmd);
1180 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
1182 struct amd_iommu *iommu = data;
1184 return iommu_flush_dte(iommu, alias);
1188 * Command send function for invalidating a device table entry
1190 static int device_flush_dte(struct iommu_dev_data *dev_data)
1192 struct amd_iommu *iommu;
1193 u16 alias;
1194 int ret;
1196 iommu = amd_iommu_rlookup_table[dev_data->devid];
1198 if (dev_data->pdev)
1199 ret = pci_for_each_dma_alias(dev_data->pdev,
1200 device_flush_dte_alias, iommu);
1201 else
1202 ret = iommu_flush_dte(iommu, dev_data->devid);
1203 if (ret)
1204 return ret;
1206 alias = amd_iommu_alias_table[dev_data->devid];
1207 if (alias != dev_data->devid) {
1208 ret = iommu_flush_dte(iommu, alias);
1209 if (ret)
1210 return ret;
1213 if (dev_data->ats.enabled)
1214 ret = device_flush_iotlb(dev_data, 0, ~0UL);
1216 return ret;
1220 * TLB invalidation function which is called from the mapping functions.
1221 * It invalidates a single PTE if the range to flush is within a single
1222 * page. Otherwise it flushes the whole TLB of the IOMMU.
1224 static void __domain_flush_pages(struct protection_domain *domain,
1225 u64 address, size_t size, int pde)
1227 struct iommu_dev_data *dev_data;
1228 struct iommu_cmd cmd;
1229 int ret = 0, i;
1231 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
1233 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1234 if (!domain->dev_iommu[i])
1235 continue;
1238 * Devices of this domain are behind this IOMMU
1239 * We need a TLB flush
1241 ret |= iommu_queue_command(amd_iommus[i], &cmd);
1244 list_for_each_entry(dev_data, &domain->dev_list, list) {
1246 if (!dev_data->ats.enabled)
1247 continue;
1249 ret |= device_flush_iotlb(dev_data, address, size);
1252 WARN_ON(ret);
1255 static void domain_flush_pages(struct protection_domain *domain,
1256 u64 address, size_t size)
1258 __domain_flush_pages(domain, address, size, 0);
1261 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1262 static void domain_flush_tlb_pde(struct protection_domain *domain)
1264 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1267 static void domain_flush_complete(struct protection_domain *domain)
1269 int i;
1271 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
1272 if (domain && !domain->dev_iommu[i])
1273 continue;
1276 * Devices of this domain are behind this IOMMU
1277 * We need to wait for completion of all commands.
1279 iommu_completion_wait(amd_iommus[i]);
1283 /* Flush the not present cache if it exists */
1284 static void domain_flush_np_cache(struct protection_domain *domain,
1285 dma_addr_t iova, size_t size)
1287 if (unlikely(amd_iommu_np_cache)) {
1288 unsigned long flags;
1290 spin_lock_irqsave(&domain->lock, flags);
1291 domain_flush_pages(domain, iova, size);
1292 domain_flush_complete(domain);
1293 spin_unlock_irqrestore(&domain->lock, flags);
1299 * This function flushes the DTEs for all devices in domain
1301 static void domain_flush_devices(struct protection_domain *domain)
1303 struct iommu_dev_data *dev_data;
1305 list_for_each_entry(dev_data, &domain->dev_list, list)
1306 device_flush_dte(dev_data);
1309 /****************************************************************************
1311 * The functions below are used the create the page table mappings for
1312 * unity mapped regions.
1314 ****************************************************************************/
1316 static void free_page_list(struct page *freelist)
1318 while (freelist != NULL) {
1319 unsigned long p = (unsigned long)page_address(freelist);
1320 freelist = freelist->freelist;
1321 free_page(p);
1325 static struct page *free_pt_page(unsigned long pt, struct page *freelist)
1327 struct page *p = virt_to_page((void *)pt);
1329 p->freelist = freelist;
1331 return p;
1334 #define DEFINE_FREE_PT_FN(LVL, FN) \
1335 static struct page *free_pt_##LVL (unsigned long __pt, struct page *freelist) \
1337 unsigned long p; \
1338 u64 *pt; \
1339 int i; \
1341 pt = (u64 *)__pt; \
1343 for (i = 0; i < 512; ++i) { \
1344 /* PTE present? */ \
1345 if (!IOMMU_PTE_PRESENT(pt[i])) \
1346 continue; \
1348 /* Large PTE? */ \
1349 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1350 PM_PTE_LEVEL(pt[i]) == 7) \
1351 continue; \
1353 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1354 freelist = FN(p, freelist); \
1357 return free_pt_page((unsigned long)pt, freelist); \
1360 DEFINE_FREE_PT_FN(l2, free_pt_page)
1361 DEFINE_FREE_PT_FN(l3, free_pt_l2)
1362 DEFINE_FREE_PT_FN(l4, free_pt_l3)
1363 DEFINE_FREE_PT_FN(l5, free_pt_l4)
1364 DEFINE_FREE_PT_FN(l6, free_pt_l5)
1366 static struct page *free_sub_pt(unsigned long root, int mode,
1367 struct page *freelist)
1369 switch (mode) {
1370 case PAGE_MODE_NONE:
1371 case PAGE_MODE_7_LEVEL:
1372 break;
1373 case PAGE_MODE_1_LEVEL:
1374 freelist = free_pt_page(root, freelist);
1375 break;
1376 case PAGE_MODE_2_LEVEL:
1377 freelist = free_pt_l2(root, freelist);
1378 break;
1379 case PAGE_MODE_3_LEVEL:
1380 freelist = free_pt_l3(root, freelist);
1381 break;
1382 case PAGE_MODE_4_LEVEL:
1383 freelist = free_pt_l4(root, freelist);
1384 break;
1385 case PAGE_MODE_5_LEVEL:
1386 freelist = free_pt_l5(root, freelist);
1387 break;
1388 case PAGE_MODE_6_LEVEL:
1389 freelist = free_pt_l6(root, freelist);
1390 break;
1391 default:
1392 BUG();
1395 return freelist;
1398 static void free_pagetable(struct protection_domain *domain)
1400 unsigned long root = (unsigned long)domain->pt_root;
1401 struct page *freelist = NULL;
1403 BUG_ON(domain->mode < PAGE_MODE_NONE ||
1404 domain->mode > PAGE_MODE_6_LEVEL);
1406 freelist = free_sub_pt(root, domain->mode, freelist);
1408 free_page_list(freelist);
1412 * This function is used to add another level to an IO page table. Adding
1413 * another level increases the size of the address space by 9 bits to a size up
1414 * to 64 bits.
1416 static bool increase_address_space(struct protection_domain *domain,
1417 unsigned long address,
1418 gfp_t gfp)
1420 unsigned long flags;
1421 bool ret = false;
1422 u64 *pte;
1424 spin_lock_irqsave(&domain->lock, flags);
1426 if (address <= PM_LEVEL_SIZE(domain->mode) ||
1427 WARN_ON_ONCE(domain->mode == PAGE_MODE_6_LEVEL))
1428 goto out;
1430 pte = (void *)get_zeroed_page(gfp);
1431 if (!pte)
1432 goto out;
1434 *pte = PM_LEVEL_PDE(domain->mode,
1435 iommu_virt_to_phys(domain->pt_root));
1436 domain->pt_root = pte;
1437 domain->mode += 1;
1439 ret = true;
1441 out:
1442 spin_unlock_irqrestore(&domain->lock, flags);
1444 return ret;
1447 static u64 *alloc_pte(struct protection_domain *domain,
1448 unsigned long address,
1449 unsigned long page_size,
1450 u64 **pte_page,
1451 gfp_t gfp,
1452 bool *updated)
1454 int level, end_lvl;
1455 u64 *pte, *page;
1457 BUG_ON(!is_power_of_2(page_size));
1459 while (address > PM_LEVEL_SIZE(domain->mode))
1460 *updated = increase_address_space(domain, address, gfp) || *updated;
1462 level = domain->mode - 1;
1463 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1464 address = PAGE_SIZE_ALIGN(address, page_size);
1465 end_lvl = PAGE_SIZE_LEVEL(page_size);
1467 while (level > end_lvl) {
1468 u64 __pte, __npte;
1469 int pte_level;
1471 __pte = *pte;
1472 pte_level = PM_PTE_LEVEL(__pte);
1475 * If we replace a series of large PTEs, we need
1476 * to tear down all of them.
1478 if (IOMMU_PTE_PRESENT(__pte) &&
1479 pte_level == PAGE_MODE_7_LEVEL) {
1480 unsigned long count, i;
1481 u64 *lpte;
1483 lpte = first_pte_l7(pte, NULL, &count);
1486 * Unmap the replicated PTEs that still match the
1487 * original large mapping
1489 for (i = 0; i < count; ++i)
1490 cmpxchg64(&lpte[i], __pte, 0ULL);
1492 *updated = true;
1493 continue;
1496 if (!IOMMU_PTE_PRESENT(__pte) ||
1497 pte_level == PAGE_MODE_NONE) {
1498 page = (u64 *)get_zeroed_page(gfp);
1500 if (!page)
1501 return NULL;
1503 __npte = PM_LEVEL_PDE(level, iommu_virt_to_phys(page));
1505 /* pte could have been changed somewhere. */
1506 if (cmpxchg64(pte, __pte, __npte) != __pte)
1507 free_page((unsigned long)page);
1508 else if (IOMMU_PTE_PRESENT(__pte))
1509 *updated = true;
1511 continue;
1514 /* No level skipping support yet */
1515 if (pte_level != level)
1516 return NULL;
1518 level -= 1;
1520 pte = IOMMU_PTE_PAGE(__pte);
1522 if (pte_page && level == end_lvl)
1523 *pte_page = pte;
1525 pte = &pte[PM_LEVEL_INDEX(level, address)];
1528 return pte;
1532 * This function checks if there is a PTE for a given dma address. If
1533 * there is one, it returns the pointer to it.
1535 static u64 *fetch_pte(struct protection_domain *domain,
1536 unsigned long address,
1537 unsigned long *page_size)
1539 int level;
1540 u64 *pte;
1542 *page_size = 0;
1544 if (address > PM_LEVEL_SIZE(domain->mode))
1545 return NULL;
1547 level = domain->mode - 1;
1548 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1549 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1551 while (level > 0) {
1553 /* Not Present */
1554 if (!IOMMU_PTE_PRESENT(*pte))
1555 return NULL;
1557 /* Large PTE */
1558 if (PM_PTE_LEVEL(*pte) == 7 ||
1559 PM_PTE_LEVEL(*pte) == 0)
1560 break;
1562 /* No level skipping support yet */
1563 if (PM_PTE_LEVEL(*pte) != level)
1564 return NULL;
1566 level -= 1;
1568 /* Walk to the next level */
1569 pte = IOMMU_PTE_PAGE(*pte);
1570 pte = &pte[PM_LEVEL_INDEX(level, address)];
1571 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1575 * If we have a series of large PTEs, make
1576 * sure to return a pointer to the first one.
1578 if (PM_PTE_LEVEL(*pte) == PAGE_MODE_7_LEVEL)
1579 pte = first_pte_l7(pte, page_size, NULL);
1581 return pte;
1584 static struct page *free_clear_pte(u64 *pte, u64 pteval, struct page *freelist)
1586 unsigned long pt;
1587 int mode;
1589 while (cmpxchg64(pte, pteval, 0) != pteval) {
1590 pr_warn("AMD-Vi: IOMMU pte changed since we read it\n");
1591 pteval = *pte;
1594 if (!IOMMU_PTE_PRESENT(pteval))
1595 return freelist;
1597 pt = (unsigned long)IOMMU_PTE_PAGE(pteval);
1598 mode = IOMMU_PTE_MODE(pteval);
1600 return free_sub_pt(pt, mode, freelist);
1604 * Generic mapping functions. It maps a physical address into a DMA
1605 * address space. It allocates the page table pages if necessary.
1606 * In the future it can be extended to a generic mapping function
1607 * supporting all features of AMD IOMMU page tables like level skipping
1608 * and full 64 bit address spaces.
1610 static int iommu_map_page(struct protection_domain *dom,
1611 unsigned long bus_addr,
1612 unsigned long phys_addr,
1613 unsigned long page_size,
1614 int prot,
1615 gfp_t gfp)
1617 struct page *freelist = NULL;
1618 bool updated = false;
1619 u64 __pte, *pte;
1620 int ret, i, count;
1622 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1623 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1625 ret = -EINVAL;
1626 if (!(prot & IOMMU_PROT_MASK))
1627 goto out;
1629 count = PAGE_SIZE_PTE_COUNT(page_size);
1630 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp, &updated);
1632 ret = -ENOMEM;
1633 if (!pte)
1634 goto out;
1636 for (i = 0; i < count; ++i)
1637 freelist = free_clear_pte(&pte[i], pte[i], freelist);
1639 if (freelist != NULL)
1640 updated = true;
1642 if (count > 1) {
1643 __pte = PAGE_SIZE_PTE(__sme_set(phys_addr), page_size);
1644 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC;
1645 } else
1646 __pte = __sme_set(phys_addr) | IOMMU_PTE_PR | IOMMU_PTE_FC;
1648 if (prot & IOMMU_PROT_IR)
1649 __pte |= IOMMU_PTE_IR;
1650 if (prot & IOMMU_PROT_IW)
1651 __pte |= IOMMU_PTE_IW;
1653 for (i = 0; i < count; ++i)
1654 pte[i] = __pte;
1656 ret = 0;
1658 out:
1659 if (updated) {
1660 unsigned long flags;
1662 spin_lock_irqsave(&dom->lock, flags);
1663 update_domain(dom);
1664 spin_unlock_irqrestore(&dom->lock, flags);
1667 /* Everything flushed out, free pages now */
1668 free_page_list(freelist);
1670 return ret;
1673 static unsigned long iommu_unmap_page(struct protection_domain *dom,
1674 unsigned long bus_addr,
1675 unsigned long page_size)
1677 unsigned long long unmapped;
1678 unsigned long unmap_size;
1679 u64 *pte;
1681 BUG_ON(!is_power_of_2(page_size));
1683 unmapped = 0;
1685 while (unmapped < page_size) {
1687 pte = fetch_pte(dom, bus_addr, &unmap_size);
1689 if (pte) {
1690 int i, count;
1692 count = PAGE_SIZE_PTE_COUNT(unmap_size);
1693 for (i = 0; i < count; i++)
1694 pte[i] = 0ULL;
1697 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1698 unmapped += unmap_size;
1701 BUG_ON(unmapped && !is_power_of_2(unmapped));
1703 return unmapped;
1706 /****************************************************************************
1708 * The next functions belong to the domain allocation. A domain is
1709 * allocated for every IOMMU as the default domain. If device isolation
1710 * is enabled, every device get its own domain. The most important thing
1711 * about domains is the page table mapping the DMA address space they
1712 * contain.
1714 ****************************************************************************/
1716 static u16 domain_id_alloc(void)
1718 int id;
1720 spin_lock(&pd_bitmap_lock);
1721 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1722 BUG_ON(id == 0);
1723 if (id > 0 && id < MAX_DOMAIN_ID)
1724 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1725 else
1726 id = 0;
1727 spin_unlock(&pd_bitmap_lock);
1729 return id;
1732 static void domain_id_free(int id)
1734 spin_lock(&pd_bitmap_lock);
1735 if (id > 0 && id < MAX_DOMAIN_ID)
1736 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1737 spin_unlock(&pd_bitmap_lock);
1740 static void free_gcr3_tbl_level1(u64 *tbl)
1742 u64 *ptr;
1743 int i;
1745 for (i = 0; i < 512; ++i) {
1746 if (!(tbl[i] & GCR3_VALID))
1747 continue;
1749 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1751 free_page((unsigned long)ptr);
1755 static void free_gcr3_tbl_level2(u64 *tbl)
1757 u64 *ptr;
1758 int i;
1760 for (i = 0; i < 512; ++i) {
1761 if (!(tbl[i] & GCR3_VALID))
1762 continue;
1764 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
1766 free_gcr3_tbl_level1(ptr);
1770 static void free_gcr3_table(struct protection_domain *domain)
1772 if (domain->glx == 2)
1773 free_gcr3_tbl_level2(domain->gcr3_tbl);
1774 else if (domain->glx == 1)
1775 free_gcr3_tbl_level1(domain->gcr3_tbl);
1776 else
1777 BUG_ON(domain->glx != 0);
1779 free_page((unsigned long)domain->gcr3_tbl);
1783 * Free a domain, only used if something went wrong in the
1784 * allocation path and we need to free an already allocated page table
1786 static void dma_ops_domain_free(struct protection_domain *domain)
1788 if (!domain)
1789 return;
1791 iommu_put_dma_cookie(&domain->domain);
1793 free_pagetable(domain);
1795 if (domain->id)
1796 domain_id_free(domain->id);
1798 kfree(domain);
1802 * Allocates a new protection domain usable for the dma_ops functions.
1803 * It also initializes the page table and the address allocator data
1804 * structures required for the dma_ops interface
1806 static struct protection_domain *dma_ops_domain_alloc(void)
1808 struct protection_domain *domain;
1810 domain = kzalloc(sizeof(struct protection_domain), GFP_KERNEL);
1811 if (!domain)
1812 return NULL;
1814 if (protection_domain_init(domain))
1815 goto free_domain;
1817 domain->mode = PAGE_MODE_3_LEVEL;
1818 domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
1819 domain->flags = PD_DMA_OPS_MASK;
1820 if (!domain->pt_root)
1821 goto free_domain;
1823 if (iommu_get_dma_cookie(&domain->domain) == -ENOMEM)
1824 goto free_domain;
1826 return domain;
1828 free_domain:
1829 dma_ops_domain_free(domain);
1831 return NULL;
1835 * little helper function to check whether a given protection domain is a
1836 * dma_ops domain
1838 static bool dma_ops_domain(struct protection_domain *domain)
1840 return domain->flags & PD_DMA_OPS_MASK;
1843 static void set_dte_entry(u16 devid, struct protection_domain *domain,
1844 bool ats, bool ppr)
1846 u64 pte_root = 0;
1847 u64 flags = 0;
1848 u32 old_domid;
1850 if (domain->mode != PAGE_MODE_NONE)
1851 pte_root = iommu_virt_to_phys(domain->pt_root);
1853 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1854 << DEV_ENTRY_MODE_SHIFT;
1855 pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV;
1857 flags = amd_iommu_dev_table[devid].data[1];
1859 if (ats)
1860 flags |= DTE_FLAG_IOTLB;
1862 if (ppr) {
1863 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1865 if (iommu_feature(iommu, FEATURE_EPHSUP))
1866 pte_root |= 1ULL << DEV_ENTRY_PPR;
1869 if (domain->flags & PD_IOMMUV2_MASK) {
1870 u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
1871 u64 glx = domain->glx;
1872 u64 tmp;
1874 pte_root |= DTE_FLAG_GV;
1875 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1877 /* First mask out possible old values for GCR3 table */
1878 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1879 flags &= ~tmp;
1881 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1882 flags &= ~tmp;
1884 /* Encode GCR3 table into DTE */
1885 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1886 pte_root |= tmp;
1888 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1889 flags |= tmp;
1891 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1892 flags |= tmp;
1895 flags &= ~DEV_DOMID_MASK;
1896 flags |= domain->id;
1898 old_domid = amd_iommu_dev_table[devid].data[1] & DEV_DOMID_MASK;
1899 amd_iommu_dev_table[devid].data[1] = flags;
1900 amd_iommu_dev_table[devid].data[0] = pte_root;
1903 * A kdump kernel might be replacing a domain ID that was copied from
1904 * the previous kernel--if so, it needs to flush the translation cache
1905 * entries for the old domain ID that is being overwritten
1907 if (old_domid) {
1908 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1910 amd_iommu_flush_tlb_domid(iommu, old_domid);
1914 static void clear_dte_entry(u16 devid)
1916 /* remove entry from the device table seen by the hardware */
1917 amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV;
1918 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
1920 amd_iommu_apply_erratum_63(devid);
1923 static void do_attach(struct iommu_dev_data *dev_data,
1924 struct protection_domain *domain)
1926 struct amd_iommu *iommu;
1927 bool ats;
1929 iommu = amd_iommu_rlookup_table[dev_data->devid];
1930 ats = dev_data->ats.enabled;
1932 /* Update data structures */
1933 dev_data->domain = domain;
1934 list_add(&dev_data->list, &domain->dev_list);
1936 /* Do reference counting */
1937 domain->dev_iommu[iommu->index] += 1;
1938 domain->dev_cnt += 1;
1940 /* Update device table */
1941 set_dte_entry(dev_data->devid, domain, ats, dev_data->iommu_v2);
1942 clone_aliases(dev_data->pdev);
1944 device_flush_dte(dev_data);
1947 static void do_detach(struct iommu_dev_data *dev_data)
1949 struct protection_domain *domain = dev_data->domain;
1950 struct amd_iommu *iommu;
1952 iommu = amd_iommu_rlookup_table[dev_data->devid];
1954 /* Update data structures */
1955 dev_data->domain = NULL;
1956 list_del(&dev_data->list);
1957 clear_dte_entry(dev_data->devid);
1958 clone_aliases(dev_data->pdev);
1960 /* Flush the DTE entry */
1961 device_flush_dte(dev_data);
1963 /* Flush IOTLB */
1964 domain_flush_tlb_pde(domain);
1966 /* Wait for the flushes to finish */
1967 domain_flush_complete(domain);
1969 /* decrease reference counters - needs to happen after the flushes */
1970 domain->dev_iommu[iommu->index] -= 1;
1971 domain->dev_cnt -= 1;
1974 static void pdev_iommuv2_disable(struct pci_dev *pdev)
1976 pci_disable_ats(pdev);
1977 pci_disable_pri(pdev);
1978 pci_disable_pasid(pdev);
1981 /* FIXME: Change generic reset-function to do the same */
1982 static int pri_reset_while_enabled(struct pci_dev *pdev)
1984 u16 control;
1985 int pos;
1987 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
1988 if (!pos)
1989 return -EINVAL;
1991 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1992 control |= PCI_PRI_CTRL_RESET;
1993 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
1995 return 0;
1998 static int pdev_iommuv2_enable(struct pci_dev *pdev)
2000 bool reset_enable;
2001 int reqs, ret;
2003 /* FIXME: Hardcode number of outstanding requests for now */
2004 reqs = 32;
2005 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2006 reqs = 1;
2007 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
2009 /* Only allow access to user-accessible pages */
2010 ret = pci_enable_pasid(pdev, 0);
2011 if (ret)
2012 goto out_err;
2014 /* First reset the PRI state of the device */
2015 ret = pci_reset_pri(pdev);
2016 if (ret)
2017 goto out_err;
2019 /* Enable PRI */
2020 ret = pci_enable_pri(pdev, reqs);
2021 if (ret)
2022 goto out_err;
2024 if (reset_enable) {
2025 ret = pri_reset_while_enabled(pdev);
2026 if (ret)
2027 goto out_err;
2030 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2031 if (ret)
2032 goto out_err;
2034 return 0;
2036 out_err:
2037 pci_disable_pri(pdev);
2038 pci_disable_pasid(pdev);
2040 return ret;
2044 * If a device is not yet associated with a domain, this function makes the
2045 * device visible in the domain
2047 static int attach_device(struct device *dev,
2048 struct protection_domain *domain)
2050 struct pci_dev *pdev;
2051 struct iommu_dev_data *dev_data;
2052 unsigned long flags;
2053 int ret;
2055 spin_lock_irqsave(&domain->lock, flags);
2057 dev_data = get_dev_data(dev);
2059 spin_lock(&dev_data->lock);
2061 ret = -EBUSY;
2062 if (dev_data->domain != NULL)
2063 goto out;
2065 if (!dev_is_pci(dev))
2066 goto skip_ats_check;
2068 pdev = to_pci_dev(dev);
2069 if (domain->flags & PD_IOMMUV2_MASK) {
2070 ret = -EINVAL;
2071 if (!dev_data->passthrough)
2072 goto out;
2074 if (dev_data->iommu_v2) {
2075 if (pdev_iommuv2_enable(pdev) != 0)
2076 goto out;
2078 dev_data->ats.enabled = true;
2079 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2080 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
2082 } else if (amd_iommu_iotlb_sup &&
2083 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
2084 dev_data->ats.enabled = true;
2085 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2088 skip_ats_check:
2089 ret = 0;
2091 do_attach(dev_data, domain);
2094 * We might boot into a crash-kernel here. The crashed kernel
2095 * left the caches in the IOMMU dirty. So we have to flush
2096 * here to evict all dirty stuff.
2098 domain_flush_tlb_pde(domain);
2100 domain_flush_complete(domain);
2102 out:
2103 spin_unlock(&dev_data->lock);
2105 spin_unlock_irqrestore(&domain->lock, flags);
2107 return ret;
2111 * Removes a device from a protection domain (with devtable_lock held)
2113 static void detach_device(struct device *dev)
2115 struct protection_domain *domain;
2116 struct iommu_dev_data *dev_data;
2117 unsigned long flags;
2119 dev_data = get_dev_data(dev);
2120 domain = dev_data->domain;
2122 spin_lock_irqsave(&domain->lock, flags);
2124 spin_lock(&dev_data->lock);
2127 * First check if the device is still attached. It might already
2128 * be detached from its domain because the generic
2129 * iommu_detach_group code detached it and we try again here in
2130 * our alias handling.
2132 if (WARN_ON(!dev_data->domain))
2133 goto out;
2135 do_detach(dev_data);
2137 if (!dev_is_pci(dev))
2138 goto out;
2140 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
2141 pdev_iommuv2_disable(to_pci_dev(dev));
2142 else if (dev_data->ats.enabled)
2143 pci_disable_ats(to_pci_dev(dev));
2145 dev_data->ats.enabled = false;
2147 out:
2148 spin_unlock(&dev_data->lock);
2150 spin_unlock_irqrestore(&domain->lock, flags);
2153 static int amd_iommu_add_device(struct device *dev)
2155 struct iommu_dev_data *dev_data;
2156 struct iommu_domain *domain;
2157 struct amd_iommu *iommu;
2158 int ret, devid;
2160 if (!check_device(dev) || get_dev_data(dev))
2161 return 0;
2163 devid = get_device_id(dev);
2164 if (devid < 0)
2165 return devid;
2167 iommu = amd_iommu_rlookup_table[devid];
2169 ret = iommu_init_device(dev);
2170 if (ret) {
2171 if (ret != -ENOTSUPP)
2172 dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
2174 iommu_ignore_device(dev);
2175 dev->dma_ops = NULL;
2176 goto out;
2178 init_iommu_group(dev);
2180 dev_data = get_dev_data(dev);
2182 BUG_ON(!dev_data);
2184 if (dev_data->iommu_v2)
2185 iommu_request_dm_for_dev(dev);
2187 /* Domains are initialized for this device - have a look what we ended up with */
2188 domain = iommu_get_domain_for_dev(dev);
2189 if (domain->type == IOMMU_DOMAIN_IDENTITY)
2190 dev_data->passthrough = true;
2191 else if (domain->type == IOMMU_DOMAIN_DMA)
2192 iommu_setup_dma_ops(dev, IOVA_START_PFN << PAGE_SHIFT, 0);
2194 out:
2195 iommu_completion_wait(iommu);
2197 return 0;
2200 static void amd_iommu_remove_device(struct device *dev)
2202 struct amd_iommu *iommu;
2203 int devid;
2205 if (!check_device(dev))
2206 return;
2208 devid = get_device_id(dev);
2209 if (devid < 0)
2210 return;
2212 iommu = amd_iommu_rlookup_table[devid];
2214 iommu_uninit_device(dev);
2215 iommu_completion_wait(iommu);
2218 static struct iommu_group *amd_iommu_device_group(struct device *dev)
2220 if (dev_is_pci(dev))
2221 return pci_device_group(dev);
2223 return acpihid_device_group(dev);
2226 static int amd_iommu_domain_get_attr(struct iommu_domain *domain,
2227 enum iommu_attr attr, void *data)
2229 switch (domain->type) {
2230 case IOMMU_DOMAIN_UNMANAGED:
2231 return -ENODEV;
2232 case IOMMU_DOMAIN_DMA:
2233 switch (attr) {
2234 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
2235 *(int *)data = !amd_iommu_unmap_flush;
2236 return 0;
2237 default:
2238 return -ENODEV;
2240 break;
2241 default:
2242 return -EINVAL;
2246 /*****************************************************************************
2248 * The next functions belong to the dma_ops mapping/unmapping code.
2250 *****************************************************************************/
2252 static void update_device_table(struct protection_domain *domain)
2254 struct iommu_dev_data *dev_data;
2256 list_for_each_entry(dev_data, &domain->dev_list, list) {
2257 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled,
2258 dev_data->iommu_v2);
2259 clone_aliases(dev_data->pdev);
2263 static void update_domain(struct protection_domain *domain)
2265 update_device_table(domain);
2267 domain_flush_devices(domain);
2268 domain_flush_tlb_pde(domain);
2271 int __init amd_iommu_init_api(void)
2273 int ret, err = 0;
2275 ret = iova_cache_get();
2276 if (ret)
2277 return ret;
2279 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2280 if (err)
2281 return err;
2282 #ifdef CONFIG_ARM_AMBA
2283 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2284 if (err)
2285 return err;
2286 #endif
2287 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2288 if (err)
2289 return err;
2291 return 0;
2294 int __init amd_iommu_init_dma_ops(void)
2296 swiotlb = (iommu_default_passthrough() || sme_me_mask) ? 1 : 0;
2297 iommu_detected = 1;
2299 if (amd_iommu_unmap_flush)
2300 pr_info("IO/TLB flush on unmap enabled\n");
2301 else
2302 pr_info("Lazy IO/TLB flushing enabled\n");
2304 return 0;
2308 /*****************************************************************************
2310 * The following functions belong to the exported interface of AMD IOMMU
2312 * This interface allows access to lower level functions of the IOMMU
2313 * like protection domain handling and assignement of devices to domains
2314 * which is not possible with the dma_ops interface.
2316 *****************************************************************************/
2318 static void cleanup_domain(struct protection_domain *domain)
2320 struct iommu_dev_data *entry;
2321 unsigned long flags;
2323 spin_lock_irqsave(&domain->lock, flags);
2325 while (!list_empty(&domain->dev_list)) {
2326 entry = list_first_entry(&domain->dev_list,
2327 struct iommu_dev_data, list);
2328 BUG_ON(!entry->domain);
2329 do_detach(entry);
2332 spin_unlock_irqrestore(&domain->lock, flags);
2335 static void protection_domain_free(struct protection_domain *domain)
2337 if (!domain)
2338 return;
2340 if (domain->id)
2341 domain_id_free(domain->id);
2343 kfree(domain);
2346 static int protection_domain_init(struct protection_domain *domain)
2348 spin_lock_init(&domain->lock);
2349 domain->id = domain_id_alloc();
2350 if (!domain->id)
2351 return -ENOMEM;
2352 INIT_LIST_HEAD(&domain->dev_list);
2354 return 0;
2357 static struct protection_domain *protection_domain_alloc(void)
2359 struct protection_domain *domain;
2361 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2362 if (!domain)
2363 return NULL;
2365 if (protection_domain_init(domain))
2366 goto out_err;
2368 return domain;
2370 out_err:
2371 kfree(domain);
2373 return NULL;
2376 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2378 struct protection_domain *pdomain;
2380 switch (type) {
2381 case IOMMU_DOMAIN_UNMANAGED:
2382 pdomain = protection_domain_alloc();
2383 if (!pdomain)
2384 return NULL;
2386 pdomain->mode = PAGE_MODE_3_LEVEL;
2387 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2388 if (!pdomain->pt_root) {
2389 protection_domain_free(pdomain);
2390 return NULL;
2393 pdomain->domain.geometry.aperture_start = 0;
2394 pdomain->domain.geometry.aperture_end = ~0ULL;
2395 pdomain->domain.geometry.force_aperture = true;
2397 break;
2398 case IOMMU_DOMAIN_DMA:
2399 pdomain = dma_ops_domain_alloc();
2400 if (!pdomain) {
2401 pr_err("Failed to allocate\n");
2402 return NULL;
2404 break;
2405 case IOMMU_DOMAIN_IDENTITY:
2406 pdomain = protection_domain_alloc();
2407 if (!pdomain)
2408 return NULL;
2410 pdomain->mode = PAGE_MODE_NONE;
2411 break;
2412 default:
2413 return NULL;
2416 return &pdomain->domain;
2419 static void amd_iommu_domain_free(struct iommu_domain *dom)
2421 struct protection_domain *domain;
2423 domain = to_pdomain(dom);
2425 if (domain->dev_cnt > 0)
2426 cleanup_domain(domain);
2428 BUG_ON(domain->dev_cnt != 0);
2430 if (!dom)
2431 return;
2433 switch (dom->type) {
2434 case IOMMU_DOMAIN_DMA:
2435 /* Now release the domain */
2436 dma_ops_domain_free(domain);
2437 break;
2438 default:
2439 if (domain->mode != PAGE_MODE_NONE)
2440 free_pagetable(domain);
2442 if (domain->flags & PD_IOMMUV2_MASK)
2443 free_gcr3_table(domain);
2445 protection_domain_free(domain);
2446 break;
2450 static void amd_iommu_detach_device(struct iommu_domain *dom,
2451 struct device *dev)
2453 struct iommu_dev_data *dev_data = dev->archdata.iommu;
2454 struct amd_iommu *iommu;
2455 int devid;
2457 if (!check_device(dev))
2458 return;
2460 devid = get_device_id(dev);
2461 if (devid < 0)
2462 return;
2464 if (dev_data->domain != NULL)
2465 detach_device(dev);
2467 iommu = amd_iommu_rlookup_table[devid];
2468 if (!iommu)
2469 return;
2471 #ifdef CONFIG_IRQ_REMAP
2472 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2473 (dom->type == IOMMU_DOMAIN_UNMANAGED))
2474 dev_data->use_vapic = 0;
2475 #endif
2477 iommu_completion_wait(iommu);
2480 static int amd_iommu_attach_device(struct iommu_domain *dom,
2481 struct device *dev)
2483 struct protection_domain *domain = to_pdomain(dom);
2484 struct iommu_dev_data *dev_data;
2485 struct amd_iommu *iommu;
2486 int ret;
2488 if (!check_device(dev))
2489 return -EINVAL;
2491 dev_data = dev->archdata.iommu;
2492 dev_data->defer_attach = false;
2494 iommu = amd_iommu_rlookup_table[dev_data->devid];
2495 if (!iommu)
2496 return -EINVAL;
2498 if (dev_data->domain)
2499 detach_device(dev);
2501 ret = attach_device(dev, domain);
2503 #ifdef CONFIG_IRQ_REMAP
2504 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
2505 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
2506 dev_data->use_vapic = 1;
2507 else
2508 dev_data->use_vapic = 0;
2510 #endif
2512 iommu_completion_wait(iommu);
2514 return ret;
2517 static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
2518 phys_addr_t paddr, size_t page_size, int iommu_prot,
2519 gfp_t gfp)
2521 struct protection_domain *domain = to_pdomain(dom);
2522 int prot = 0;
2523 int ret;
2525 if (domain->mode == PAGE_MODE_NONE)
2526 return -EINVAL;
2528 if (iommu_prot & IOMMU_READ)
2529 prot |= IOMMU_PROT_IR;
2530 if (iommu_prot & IOMMU_WRITE)
2531 prot |= IOMMU_PROT_IW;
2533 ret = iommu_map_page(domain, iova, paddr, page_size, prot, gfp);
2535 domain_flush_np_cache(domain, iova, page_size);
2537 return ret;
2540 static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2541 size_t page_size,
2542 struct iommu_iotlb_gather *gather)
2544 struct protection_domain *domain = to_pdomain(dom);
2546 if (domain->mode == PAGE_MODE_NONE)
2547 return 0;
2549 return iommu_unmap_page(domain, iova, page_size);
2552 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
2553 dma_addr_t iova)
2555 struct protection_domain *domain = to_pdomain(dom);
2556 unsigned long offset_mask, pte_pgsize;
2557 u64 *pte, __pte;
2559 if (domain->mode == PAGE_MODE_NONE)
2560 return iova;
2562 pte = fetch_pte(domain, iova, &pte_pgsize);
2564 if (!pte || !IOMMU_PTE_PRESENT(*pte))
2565 return 0;
2567 offset_mask = pte_pgsize - 1;
2568 __pte = __sme_clr(*pte & PM_ADDR_MASK);
2570 return (__pte & ~offset_mask) | (iova & offset_mask);
2573 static bool amd_iommu_capable(enum iommu_cap cap)
2575 switch (cap) {
2576 case IOMMU_CAP_CACHE_COHERENCY:
2577 return true;
2578 case IOMMU_CAP_INTR_REMAP:
2579 return (irq_remapping_enabled == 1);
2580 case IOMMU_CAP_NOEXEC:
2581 return false;
2582 default:
2583 break;
2586 return false;
2589 static void amd_iommu_get_resv_regions(struct device *dev,
2590 struct list_head *head)
2592 struct iommu_resv_region *region;
2593 struct unity_map_entry *entry;
2594 int devid;
2596 devid = get_device_id(dev);
2597 if (devid < 0)
2598 return;
2600 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
2601 int type, prot = 0;
2602 size_t length;
2604 if (devid < entry->devid_start || devid > entry->devid_end)
2605 continue;
2607 type = IOMMU_RESV_DIRECT;
2608 length = entry->address_end - entry->address_start;
2609 if (entry->prot & IOMMU_PROT_IR)
2610 prot |= IOMMU_READ;
2611 if (entry->prot & IOMMU_PROT_IW)
2612 prot |= IOMMU_WRITE;
2613 if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
2614 /* Exclusion range */
2615 type = IOMMU_RESV_RESERVED;
2617 region = iommu_alloc_resv_region(entry->address_start,
2618 length, prot, type);
2619 if (!region) {
2620 dev_err(dev, "Out of memory allocating dm-regions\n");
2621 return;
2623 list_add_tail(&region->list, head);
2626 region = iommu_alloc_resv_region(MSI_RANGE_START,
2627 MSI_RANGE_END - MSI_RANGE_START + 1,
2628 0, IOMMU_RESV_MSI);
2629 if (!region)
2630 return;
2631 list_add_tail(&region->list, head);
2633 region = iommu_alloc_resv_region(HT_RANGE_START,
2634 HT_RANGE_END - HT_RANGE_START + 1,
2635 0, IOMMU_RESV_RESERVED);
2636 if (!region)
2637 return;
2638 list_add_tail(&region->list, head);
2641 static void amd_iommu_put_resv_regions(struct device *dev,
2642 struct list_head *head)
2644 struct iommu_resv_region *entry, *next;
2646 list_for_each_entry_safe(entry, next, head, list)
2647 kfree(entry);
2650 static bool amd_iommu_is_attach_deferred(struct iommu_domain *domain,
2651 struct device *dev)
2653 struct iommu_dev_data *dev_data = dev->archdata.iommu;
2654 return dev_data->defer_attach;
2657 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
2659 struct protection_domain *dom = to_pdomain(domain);
2660 unsigned long flags;
2662 spin_lock_irqsave(&dom->lock, flags);
2663 domain_flush_tlb_pde(dom);
2664 domain_flush_complete(dom);
2665 spin_unlock_irqrestore(&dom->lock, flags);
2668 static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
2669 struct iommu_iotlb_gather *gather)
2671 amd_iommu_flush_iotlb_all(domain);
2674 const struct iommu_ops amd_iommu_ops = {
2675 .capable = amd_iommu_capable,
2676 .domain_alloc = amd_iommu_domain_alloc,
2677 .domain_free = amd_iommu_domain_free,
2678 .attach_dev = amd_iommu_attach_device,
2679 .detach_dev = amd_iommu_detach_device,
2680 .map = amd_iommu_map,
2681 .unmap = amd_iommu_unmap,
2682 .iova_to_phys = amd_iommu_iova_to_phys,
2683 .add_device = amd_iommu_add_device,
2684 .remove_device = amd_iommu_remove_device,
2685 .device_group = amd_iommu_device_group,
2686 .domain_get_attr = amd_iommu_domain_get_attr,
2687 .get_resv_regions = amd_iommu_get_resv_regions,
2688 .put_resv_regions = amd_iommu_put_resv_regions,
2689 .is_attach_deferred = amd_iommu_is_attach_deferred,
2690 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
2691 .flush_iotlb_all = amd_iommu_flush_iotlb_all,
2692 .iotlb_sync = amd_iommu_iotlb_sync,
2695 /*****************************************************************************
2697 * The next functions do a basic initialization of IOMMU for pass through
2698 * mode
2700 * In passthrough mode the IOMMU is initialized and enabled but not used for
2701 * DMA-API translation.
2703 *****************************************************************************/
2705 /* IOMMUv2 specific functions */
2706 int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2708 return atomic_notifier_chain_register(&ppr_notifier, nb);
2710 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2712 int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2714 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2716 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
2718 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2720 struct protection_domain *domain = to_pdomain(dom);
2721 unsigned long flags;
2723 spin_lock_irqsave(&domain->lock, flags);
2725 /* Update data structure */
2726 domain->mode = PAGE_MODE_NONE;
2728 /* Make changes visible to IOMMUs */
2729 update_domain(domain);
2731 /* Page-table is not visible to IOMMU anymore, so free it */
2732 free_pagetable(domain);
2734 spin_unlock_irqrestore(&domain->lock, flags);
2736 EXPORT_SYMBOL(amd_iommu_domain_direct_map);
2738 int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
2740 struct protection_domain *domain = to_pdomain(dom);
2741 unsigned long flags;
2742 int levels, ret;
2744 if (pasids <= 0 || pasids > (PASID_MASK + 1))
2745 return -EINVAL;
2747 /* Number of GCR3 table levels required */
2748 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
2749 levels += 1;
2751 if (levels > amd_iommu_max_glx_val)
2752 return -EINVAL;
2754 spin_lock_irqsave(&domain->lock, flags);
2757 * Save us all sanity checks whether devices already in the
2758 * domain support IOMMUv2. Just force that the domain has no
2759 * devices attached when it is switched into IOMMUv2 mode.
2761 ret = -EBUSY;
2762 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
2763 goto out;
2765 ret = -ENOMEM;
2766 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
2767 if (domain->gcr3_tbl == NULL)
2768 goto out;
2770 domain->glx = levels;
2771 domain->flags |= PD_IOMMUV2_MASK;
2773 update_domain(domain);
2775 ret = 0;
2777 out:
2778 spin_unlock_irqrestore(&domain->lock, flags);
2780 return ret;
2782 EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
2784 static int __flush_pasid(struct protection_domain *domain, int pasid,
2785 u64 address, bool size)
2787 struct iommu_dev_data *dev_data;
2788 struct iommu_cmd cmd;
2789 int i, ret;
2791 if (!(domain->flags & PD_IOMMUV2_MASK))
2792 return -EINVAL;
2794 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
2797 * IOMMU TLB needs to be flushed before Device TLB to
2798 * prevent device TLB refill from IOMMU TLB
2800 for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
2801 if (domain->dev_iommu[i] == 0)
2802 continue;
2804 ret = iommu_queue_command(amd_iommus[i], &cmd);
2805 if (ret != 0)
2806 goto out;
2809 /* Wait until IOMMU TLB flushes are complete */
2810 domain_flush_complete(domain);
2812 /* Now flush device TLBs */
2813 list_for_each_entry(dev_data, &domain->dev_list, list) {
2814 struct amd_iommu *iommu;
2815 int qdep;
2818 There might be non-IOMMUv2 capable devices in an IOMMUv2
2819 * domain.
2821 if (!dev_data->ats.enabled)
2822 continue;
2824 qdep = dev_data->ats.qdep;
2825 iommu = amd_iommu_rlookup_table[dev_data->devid];
2827 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
2828 qdep, address, size);
2830 ret = iommu_queue_command(iommu, &cmd);
2831 if (ret != 0)
2832 goto out;
2835 /* Wait until all device TLBs are flushed */
2836 domain_flush_complete(domain);
2838 ret = 0;
2840 out:
2842 return ret;
2845 static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
2846 u64 address)
2848 return __flush_pasid(domain, pasid, address, false);
2851 int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
2852 u64 address)
2854 struct protection_domain *domain = to_pdomain(dom);
2855 unsigned long flags;
2856 int ret;
2858 spin_lock_irqsave(&domain->lock, flags);
2859 ret = __amd_iommu_flush_page(domain, pasid, address);
2860 spin_unlock_irqrestore(&domain->lock, flags);
2862 return ret;
2864 EXPORT_SYMBOL(amd_iommu_flush_page);
2866 static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
2868 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
2869 true);
2872 int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
2874 struct protection_domain *domain = to_pdomain(dom);
2875 unsigned long flags;
2876 int ret;
2878 spin_lock_irqsave(&domain->lock, flags);
2879 ret = __amd_iommu_flush_tlb(domain, pasid);
2880 spin_unlock_irqrestore(&domain->lock, flags);
2882 return ret;
2884 EXPORT_SYMBOL(amd_iommu_flush_tlb);
2886 static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
2888 int index;
2889 u64 *pte;
2891 while (true) {
2893 index = (pasid >> (9 * level)) & 0x1ff;
2894 pte = &root[index];
2896 if (level == 0)
2897 break;
2899 if (!(*pte & GCR3_VALID)) {
2900 if (!alloc)
2901 return NULL;
2903 root = (void *)get_zeroed_page(GFP_ATOMIC);
2904 if (root == NULL)
2905 return NULL;
2907 *pte = iommu_virt_to_phys(root) | GCR3_VALID;
2910 root = iommu_phys_to_virt(*pte & PAGE_MASK);
2912 level -= 1;
2915 return pte;
2918 static int __set_gcr3(struct protection_domain *domain, int pasid,
2919 unsigned long cr3)
2921 u64 *pte;
2923 if (domain->mode != PAGE_MODE_NONE)
2924 return -EINVAL;
2926 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
2927 if (pte == NULL)
2928 return -ENOMEM;
2930 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
2932 return __amd_iommu_flush_tlb(domain, pasid);
2935 static int __clear_gcr3(struct protection_domain *domain, int pasid)
2937 u64 *pte;
2939 if (domain->mode != PAGE_MODE_NONE)
2940 return -EINVAL;
2942 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
2943 if (pte == NULL)
2944 return 0;
2946 *pte = 0;
2948 return __amd_iommu_flush_tlb(domain, pasid);
2951 int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
2952 unsigned long cr3)
2954 struct protection_domain *domain = to_pdomain(dom);
2955 unsigned long flags;
2956 int ret;
2958 spin_lock_irqsave(&domain->lock, flags);
2959 ret = __set_gcr3(domain, pasid, cr3);
2960 spin_unlock_irqrestore(&domain->lock, flags);
2962 return ret;
2964 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
2966 int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
2968 struct protection_domain *domain = to_pdomain(dom);
2969 unsigned long flags;
2970 int ret;
2972 spin_lock_irqsave(&domain->lock, flags);
2973 ret = __clear_gcr3(domain, pasid);
2974 spin_unlock_irqrestore(&domain->lock, flags);
2976 return ret;
2978 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
2980 int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
2981 int status, int tag)
2983 struct iommu_dev_data *dev_data;
2984 struct amd_iommu *iommu;
2985 struct iommu_cmd cmd;
2987 dev_data = get_dev_data(&pdev->dev);
2988 iommu = amd_iommu_rlookup_table[dev_data->devid];
2990 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
2991 tag, dev_data->pri_tlp);
2993 return iommu_queue_command(iommu, &cmd);
2995 EXPORT_SYMBOL(amd_iommu_complete_ppr);
2997 struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
2999 struct protection_domain *pdomain;
3000 struct iommu_domain *io_domain;
3001 struct device *dev = &pdev->dev;
3003 if (!check_device(dev))
3004 return NULL;
3006 pdomain = get_dev_data(dev)->domain;
3007 if (pdomain == NULL && get_dev_data(dev)->defer_attach) {
3008 get_dev_data(dev)->defer_attach = false;
3009 io_domain = iommu_get_domain_for_dev(dev);
3010 pdomain = to_pdomain(io_domain);
3011 attach_device(dev, pdomain);
3013 if (pdomain == NULL)
3014 return NULL;
3016 if (!dma_ops_domain(pdomain))
3017 return NULL;
3019 /* Only return IOMMUv2 domains */
3020 if (!(pdomain->flags & PD_IOMMUV2_MASK))
3021 return NULL;
3023 return &pdomain->domain;
3025 EXPORT_SYMBOL(amd_iommu_get_v2_domain);
3027 void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3029 struct iommu_dev_data *dev_data;
3031 if (!amd_iommu_v2_supported())
3032 return;
3034 dev_data = get_dev_data(&pdev->dev);
3035 dev_data->errata |= (1 << erratum);
3037 EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
3039 int amd_iommu_device_info(struct pci_dev *pdev,
3040 struct amd_iommu_device_info *info)
3042 int max_pasids;
3043 int pos;
3045 if (pdev == NULL || info == NULL)
3046 return -EINVAL;
3048 if (!amd_iommu_v2_supported())
3049 return -EINVAL;
3051 memset(info, 0, sizeof(*info));
3053 if (!pci_ats_disabled()) {
3054 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3055 if (pos)
3056 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3059 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3060 if (pos)
3061 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3063 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3064 if (pos) {
3065 int features;
3067 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3068 max_pasids = min(max_pasids, (1 << 20));
3070 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3071 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3073 features = pci_pasid_features(pdev);
3074 if (features & PCI_PASID_CAP_EXEC)
3075 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3076 if (features & PCI_PASID_CAP_PRIV)
3077 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3080 return 0;
3082 EXPORT_SYMBOL(amd_iommu_device_info);
3084 #ifdef CONFIG_IRQ_REMAP
3086 /*****************************************************************************
3088 * Interrupt Remapping Implementation
3090 *****************************************************************************/
3092 static struct irq_chip amd_ir_chip;
3093 static DEFINE_SPINLOCK(iommu_table_lock);
3095 static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3097 u64 dte;
3099 dte = amd_iommu_dev_table[devid].data[2];
3100 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3101 dte |= iommu_virt_to_phys(table->table);
3102 dte |= DTE_IRQ_REMAP_INTCTL;
3103 dte |= DTE_IRQ_TABLE_LEN;
3104 dte |= DTE_IRQ_REMAP_ENABLE;
3106 amd_iommu_dev_table[devid].data[2] = dte;
3109 static struct irq_remap_table *get_irq_table(u16 devid)
3111 struct irq_remap_table *table;
3113 if (WARN_ONCE(!amd_iommu_rlookup_table[devid],
3114 "%s: no iommu for devid %x\n", __func__, devid))
3115 return NULL;
3117 table = irq_lookup_table[devid];
3118 if (WARN_ONCE(!table, "%s: no table for devid %x\n", __func__, devid))
3119 return NULL;
3121 return table;
3124 static struct irq_remap_table *__alloc_irq_table(void)
3126 struct irq_remap_table *table;
3128 table = kzalloc(sizeof(*table), GFP_KERNEL);
3129 if (!table)
3130 return NULL;
3132 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
3133 if (!table->table) {
3134 kfree(table);
3135 return NULL;
3137 raw_spin_lock_init(&table->lock);
3139 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3140 memset(table->table, 0,
3141 MAX_IRQS_PER_TABLE * sizeof(u32));
3142 else
3143 memset(table->table, 0,
3144 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
3145 return table;
3148 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
3149 struct irq_remap_table *table)
3151 irq_lookup_table[devid] = table;
3152 set_dte_irq_entry(devid, table);
3153 iommu_flush_dte(iommu, devid);
3156 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
3157 void *data)
3159 struct irq_remap_table *table = data;
3161 irq_lookup_table[alias] = table;
3162 set_dte_irq_entry(alias, table);
3164 iommu_flush_dte(amd_iommu_rlookup_table[alias], alias);
3166 return 0;
3169 static struct irq_remap_table *alloc_irq_table(u16 devid, struct pci_dev *pdev)
3171 struct irq_remap_table *table = NULL;
3172 struct irq_remap_table *new_table = NULL;
3173 struct amd_iommu *iommu;
3174 unsigned long flags;
3175 u16 alias;
3177 spin_lock_irqsave(&iommu_table_lock, flags);
3179 iommu = amd_iommu_rlookup_table[devid];
3180 if (!iommu)
3181 goto out_unlock;
3183 table = irq_lookup_table[devid];
3184 if (table)
3185 goto out_unlock;
3187 alias = amd_iommu_alias_table[devid];
3188 table = irq_lookup_table[alias];
3189 if (table) {
3190 set_remap_table_entry(iommu, devid, table);
3191 goto out_wait;
3193 spin_unlock_irqrestore(&iommu_table_lock, flags);
3195 /* Nothing there yet, allocate new irq remapping table */
3196 new_table = __alloc_irq_table();
3197 if (!new_table)
3198 return NULL;
3200 spin_lock_irqsave(&iommu_table_lock, flags);
3202 table = irq_lookup_table[devid];
3203 if (table)
3204 goto out_unlock;
3206 table = irq_lookup_table[alias];
3207 if (table) {
3208 set_remap_table_entry(iommu, devid, table);
3209 goto out_wait;
3212 table = new_table;
3213 new_table = NULL;
3215 if (pdev)
3216 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
3217 table);
3218 else
3219 set_remap_table_entry(iommu, devid, table);
3221 if (devid != alias)
3222 set_remap_table_entry(iommu, alias, table);
3224 out_wait:
3225 iommu_completion_wait(iommu);
3227 out_unlock:
3228 spin_unlock_irqrestore(&iommu_table_lock, flags);
3230 if (new_table) {
3231 kmem_cache_free(amd_iommu_irq_cache, new_table->table);
3232 kfree(new_table);
3234 return table;
3237 static int alloc_irq_index(u16 devid, int count, bool align,
3238 struct pci_dev *pdev)
3240 struct irq_remap_table *table;
3241 int index, c, alignment = 1;
3242 unsigned long flags;
3243 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3245 if (!iommu)
3246 return -ENODEV;
3248 table = alloc_irq_table(devid, pdev);
3249 if (!table)
3250 return -ENODEV;
3252 if (align)
3253 alignment = roundup_pow_of_two(count);
3255 raw_spin_lock_irqsave(&table->lock, flags);
3257 /* Scan table for free entries */
3258 for (index = ALIGN(table->min_index, alignment), c = 0;
3259 index < MAX_IRQS_PER_TABLE;) {
3260 if (!iommu->irte_ops->is_allocated(table, index)) {
3261 c += 1;
3262 } else {
3263 c = 0;
3264 index = ALIGN(index + 1, alignment);
3265 continue;
3268 if (c == count) {
3269 for (; c != 0; --c)
3270 iommu->irte_ops->set_allocated(table, index - c + 1);
3272 index -= count - 1;
3273 goto out;
3276 index++;
3279 index = -ENOSPC;
3281 out:
3282 raw_spin_unlock_irqrestore(&table->lock, flags);
3284 return index;
3287 static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
3288 struct amd_ir_data *data)
3290 struct irq_remap_table *table;
3291 struct amd_iommu *iommu;
3292 unsigned long flags;
3293 struct irte_ga *entry;
3295 iommu = amd_iommu_rlookup_table[devid];
3296 if (iommu == NULL)
3297 return -EINVAL;
3299 table = get_irq_table(devid);
3300 if (!table)
3301 return -ENOMEM;
3303 raw_spin_lock_irqsave(&table->lock, flags);
3305 entry = (struct irte_ga *)table->table;
3306 entry = &entry[index];
3307 entry->lo.fields_remap.valid = 0;
3308 entry->hi.val = irte->hi.val;
3309 entry->lo.val = irte->lo.val;
3310 entry->lo.fields_remap.valid = 1;
3311 if (data)
3312 data->ref = entry;
3314 raw_spin_unlock_irqrestore(&table->lock, flags);
3316 iommu_flush_irt(iommu, devid);
3317 iommu_completion_wait(iommu);
3319 return 0;
3322 static int modify_irte(u16 devid, int index, union irte *irte)
3324 struct irq_remap_table *table;
3325 struct amd_iommu *iommu;
3326 unsigned long flags;
3328 iommu = amd_iommu_rlookup_table[devid];
3329 if (iommu == NULL)
3330 return -EINVAL;
3332 table = get_irq_table(devid);
3333 if (!table)
3334 return -ENOMEM;
3336 raw_spin_lock_irqsave(&table->lock, flags);
3337 table->table[index] = irte->val;
3338 raw_spin_unlock_irqrestore(&table->lock, flags);
3340 iommu_flush_irt(iommu, devid);
3341 iommu_completion_wait(iommu);
3343 return 0;
3346 static void free_irte(u16 devid, int index)
3348 struct irq_remap_table *table;
3349 struct amd_iommu *iommu;
3350 unsigned long flags;
3352 iommu = amd_iommu_rlookup_table[devid];
3353 if (iommu == NULL)
3354 return;
3356 table = get_irq_table(devid);
3357 if (!table)
3358 return;
3360 raw_spin_lock_irqsave(&table->lock, flags);
3361 iommu->irte_ops->clear_allocated(table, index);
3362 raw_spin_unlock_irqrestore(&table->lock, flags);
3364 iommu_flush_irt(iommu, devid);
3365 iommu_completion_wait(iommu);
3368 static void irte_prepare(void *entry,
3369 u32 delivery_mode, u32 dest_mode,
3370 u8 vector, u32 dest_apicid, int devid)
3372 union irte *irte = (union irte *) entry;
3374 irte->val = 0;
3375 irte->fields.vector = vector;
3376 irte->fields.int_type = delivery_mode;
3377 irte->fields.destination = dest_apicid;
3378 irte->fields.dm = dest_mode;
3379 irte->fields.valid = 1;
3382 static void irte_ga_prepare(void *entry,
3383 u32 delivery_mode, u32 dest_mode,
3384 u8 vector, u32 dest_apicid, int devid)
3386 struct irte_ga *irte = (struct irte_ga *) entry;
3388 irte->lo.val = 0;
3389 irte->hi.val = 0;
3390 irte->lo.fields_remap.int_type = delivery_mode;
3391 irte->lo.fields_remap.dm = dest_mode;
3392 irte->hi.fields.vector = vector;
3393 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
3394 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
3395 irte->lo.fields_remap.valid = 1;
3398 static void irte_activate(void *entry, u16 devid, u16 index)
3400 union irte *irte = (union irte *) entry;
3402 irte->fields.valid = 1;
3403 modify_irte(devid, index, irte);
3406 static void irte_ga_activate(void *entry, u16 devid, u16 index)
3408 struct irte_ga *irte = (struct irte_ga *) entry;
3410 irte->lo.fields_remap.valid = 1;
3411 modify_irte_ga(devid, index, irte, NULL);
3414 static void irte_deactivate(void *entry, u16 devid, u16 index)
3416 union irte *irte = (union irte *) entry;
3418 irte->fields.valid = 0;
3419 modify_irte(devid, index, irte);
3422 static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
3424 struct irte_ga *irte = (struct irte_ga *) entry;
3426 irte->lo.fields_remap.valid = 0;
3427 modify_irte_ga(devid, index, irte, NULL);
3430 static void irte_set_affinity(void *entry, u16 devid, u16 index,
3431 u8 vector, u32 dest_apicid)
3433 union irte *irte = (union irte *) entry;
3435 irte->fields.vector = vector;
3436 irte->fields.destination = dest_apicid;
3437 modify_irte(devid, index, irte);
3440 static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
3441 u8 vector, u32 dest_apicid)
3443 struct irte_ga *irte = (struct irte_ga *) entry;
3445 if (!irte->lo.fields_remap.guest_mode) {
3446 irte->hi.fields.vector = vector;
3447 irte->lo.fields_remap.destination =
3448 APICID_TO_IRTE_DEST_LO(dest_apicid);
3449 irte->hi.fields.destination =
3450 APICID_TO_IRTE_DEST_HI(dest_apicid);
3451 modify_irte_ga(devid, index, irte, NULL);
3455 #define IRTE_ALLOCATED (~1U)
3456 static void irte_set_allocated(struct irq_remap_table *table, int index)
3458 table->table[index] = IRTE_ALLOCATED;
3461 static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3463 struct irte_ga *ptr = (struct irte_ga *)table->table;
3464 struct irte_ga *irte = &ptr[index];
3466 memset(&irte->lo.val, 0, sizeof(u64));
3467 memset(&irte->hi.val, 0, sizeof(u64));
3468 irte->hi.fields.vector = 0xff;
3471 static bool irte_is_allocated(struct irq_remap_table *table, int index)
3473 union irte *ptr = (union irte *)table->table;
3474 union irte *irte = &ptr[index];
3476 return irte->val != 0;
3479 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3481 struct irte_ga *ptr = (struct irte_ga *)table->table;
3482 struct irte_ga *irte = &ptr[index];
3484 return irte->hi.fields.vector != 0;
3487 static void irte_clear_allocated(struct irq_remap_table *table, int index)
3489 table->table[index] = 0;
3492 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3494 struct irte_ga *ptr = (struct irte_ga *)table->table;
3495 struct irte_ga *irte = &ptr[index];
3497 memset(&irte->lo.val, 0, sizeof(u64));
3498 memset(&irte->hi.val, 0, sizeof(u64));
3501 static int get_devid(struct irq_alloc_info *info)
3503 int devid = -1;
3505 switch (info->type) {
3506 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3507 devid = get_ioapic_devid(info->ioapic_id);
3508 break;
3509 case X86_IRQ_ALLOC_TYPE_HPET:
3510 devid = get_hpet_devid(info->hpet_id);
3511 break;
3512 case X86_IRQ_ALLOC_TYPE_MSI:
3513 case X86_IRQ_ALLOC_TYPE_MSIX:
3514 devid = get_device_id(&info->msi_dev->dev);
3515 break;
3516 default:
3517 BUG_ON(1);
3518 break;
3521 return devid;
3524 static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
3526 struct amd_iommu *iommu;
3527 int devid;
3529 if (!info)
3530 return NULL;
3532 devid = get_devid(info);
3533 if (devid >= 0) {
3534 iommu = amd_iommu_rlookup_table[devid];
3535 if (iommu)
3536 return iommu->ir_domain;
3539 return NULL;
3542 static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
3544 struct amd_iommu *iommu;
3545 int devid;
3547 if (!info)
3548 return NULL;
3550 switch (info->type) {
3551 case X86_IRQ_ALLOC_TYPE_MSI:
3552 case X86_IRQ_ALLOC_TYPE_MSIX:
3553 devid = get_device_id(&info->msi_dev->dev);
3554 if (devid < 0)
3555 return NULL;
3557 iommu = amd_iommu_rlookup_table[devid];
3558 if (iommu)
3559 return iommu->msi_domain;
3560 break;
3561 default:
3562 break;
3565 return NULL;
3568 struct irq_remap_ops amd_iommu_irq_ops = {
3569 .prepare = amd_iommu_prepare,
3570 .enable = amd_iommu_enable,
3571 .disable = amd_iommu_disable,
3572 .reenable = amd_iommu_reenable,
3573 .enable_faulting = amd_iommu_enable_faulting,
3574 .get_ir_irq_domain = get_ir_irq_domain,
3575 .get_irq_domain = get_irq_domain,
3578 static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3579 struct irq_cfg *irq_cfg,
3580 struct irq_alloc_info *info,
3581 int devid, int index, int sub_handle)
3583 struct irq_2_irte *irte_info = &data->irq_2_irte;
3584 struct msi_msg *msg = &data->msi_entry;
3585 struct IO_APIC_route_entry *entry;
3586 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3588 if (!iommu)
3589 return;
3591 data->irq_2_irte.devid = devid;
3592 data->irq_2_irte.index = index + sub_handle;
3593 iommu->irte_ops->prepare(data->entry, apic->irq_delivery_mode,
3594 apic->irq_dest_mode, irq_cfg->vector,
3595 irq_cfg->dest_apicid, devid);
3597 switch (info->type) {
3598 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3599 /* Setup IOAPIC entry */
3600 entry = info->ioapic_entry;
3601 info->ioapic_entry = NULL;
3602 memset(entry, 0, sizeof(*entry));
3603 entry->vector = index;
3604 entry->mask = 0;
3605 entry->trigger = info->ioapic_trigger;
3606 entry->polarity = info->ioapic_polarity;
3607 /* Mask level triggered irqs. */
3608 if (info->ioapic_trigger)
3609 entry->mask = 1;
3610 break;
3612 case X86_IRQ_ALLOC_TYPE_HPET:
3613 case X86_IRQ_ALLOC_TYPE_MSI:
3614 case X86_IRQ_ALLOC_TYPE_MSIX:
3615 msg->address_hi = MSI_ADDR_BASE_HI;
3616 msg->address_lo = MSI_ADDR_BASE_LO;
3617 msg->data = irte_info->index;
3618 break;
3620 default:
3621 BUG_ON(1);
3622 break;
3626 struct amd_irte_ops irte_32_ops = {
3627 .prepare = irte_prepare,
3628 .activate = irte_activate,
3629 .deactivate = irte_deactivate,
3630 .set_affinity = irte_set_affinity,
3631 .set_allocated = irte_set_allocated,
3632 .is_allocated = irte_is_allocated,
3633 .clear_allocated = irte_clear_allocated,
3636 struct amd_irte_ops irte_128_ops = {
3637 .prepare = irte_ga_prepare,
3638 .activate = irte_ga_activate,
3639 .deactivate = irte_ga_deactivate,
3640 .set_affinity = irte_ga_set_affinity,
3641 .set_allocated = irte_ga_set_allocated,
3642 .is_allocated = irte_ga_is_allocated,
3643 .clear_allocated = irte_ga_clear_allocated,
3646 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3647 unsigned int nr_irqs, void *arg)
3649 struct irq_alloc_info *info = arg;
3650 struct irq_data *irq_data;
3651 struct amd_ir_data *data = NULL;
3652 struct irq_cfg *cfg;
3653 int i, ret, devid;
3654 int index;
3656 if (!info)
3657 return -EINVAL;
3658 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3659 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3660 return -EINVAL;
3663 * With IRQ remapping enabled, don't need contiguous CPU vectors
3664 * to support multiple MSI interrupts.
3666 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3667 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3669 devid = get_devid(info);
3670 if (devid < 0)
3671 return -EINVAL;
3673 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3674 if (ret < 0)
3675 return ret;
3677 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3678 struct irq_remap_table *table;
3679 struct amd_iommu *iommu;
3681 table = alloc_irq_table(devid, NULL);
3682 if (table) {
3683 if (!table->min_index) {
3685 * Keep the first 32 indexes free for IOAPIC
3686 * interrupts.
3688 table->min_index = 32;
3689 iommu = amd_iommu_rlookup_table[devid];
3690 for (i = 0; i < 32; ++i)
3691 iommu->irte_ops->set_allocated(table, i);
3693 WARN_ON(table->min_index != 32);
3694 index = info->ioapic_pin;
3695 } else {
3696 index = -ENOMEM;
3698 } else if (info->type == X86_IRQ_ALLOC_TYPE_MSI ||
3699 info->type == X86_IRQ_ALLOC_TYPE_MSIX) {
3700 bool align = (info->type == X86_IRQ_ALLOC_TYPE_MSI);
3702 index = alloc_irq_index(devid, nr_irqs, align, info->msi_dev);
3703 } else {
3704 index = alloc_irq_index(devid, nr_irqs, false, NULL);
3707 if (index < 0) {
3708 pr_warn("Failed to allocate IRTE\n");
3709 ret = index;
3710 goto out_free_parent;
3713 for (i = 0; i < nr_irqs; i++) {
3714 irq_data = irq_domain_get_irq_data(domain, virq + i);
3715 cfg = irqd_cfg(irq_data);
3716 if (!irq_data || !cfg) {
3717 ret = -EINVAL;
3718 goto out_free_data;
3721 ret = -ENOMEM;
3722 data = kzalloc(sizeof(*data), GFP_KERNEL);
3723 if (!data)
3724 goto out_free_data;
3726 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3727 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
3728 else
3729 data->entry = kzalloc(sizeof(struct irte_ga),
3730 GFP_KERNEL);
3731 if (!data->entry) {
3732 kfree(data);
3733 goto out_free_data;
3736 irq_data->hwirq = (devid << 16) + i;
3737 irq_data->chip_data = data;
3738 irq_data->chip = &amd_ir_chip;
3739 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3740 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3743 return 0;
3745 out_free_data:
3746 for (i--; i >= 0; i--) {
3747 irq_data = irq_domain_get_irq_data(domain, virq + i);
3748 if (irq_data)
3749 kfree(irq_data->chip_data);
3751 for (i = 0; i < nr_irqs; i++)
3752 free_irte(devid, index + i);
3753 out_free_parent:
3754 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3755 return ret;
3758 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3759 unsigned int nr_irqs)
3761 struct irq_2_irte *irte_info;
3762 struct irq_data *irq_data;
3763 struct amd_ir_data *data;
3764 int i;
3766 for (i = 0; i < nr_irqs; i++) {
3767 irq_data = irq_domain_get_irq_data(domain, virq + i);
3768 if (irq_data && irq_data->chip_data) {
3769 data = irq_data->chip_data;
3770 irte_info = &data->irq_2_irte;
3771 free_irte(irte_info->devid, irte_info->index);
3772 kfree(data->entry);
3773 kfree(data);
3776 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3779 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3780 struct amd_ir_data *ir_data,
3781 struct irq_2_irte *irte_info,
3782 struct irq_cfg *cfg);
3784 static int irq_remapping_activate(struct irq_domain *domain,
3785 struct irq_data *irq_data, bool reserve)
3787 struct amd_ir_data *data = irq_data->chip_data;
3788 struct irq_2_irte *irte_info = &data->irq_2_irte;
3789 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3790 struct irq_cfg *cfg = irqd_cfg(irq_data);
3792 if (!iommu)
3793 return 0;
3795 iommu->irte_ops->activate(data->entry, irte_info->devid,
3796 irte_info->index);
3797 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
3798 return 0;
3801 static void irq_remapping_deactivate(struct irq_domain *domain,
3802 struct irq_data *irq_data)
3804 struct amd_ir_data *data = irq_data->chip_data;
3805 struct irq_2_irte *irte_info = &data->irq_2_irte;
3806 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3808 if (iommu)
3809 iommu->irte_ops->deactivate(data->entry, irte_info->devid,
3810 irte_info->index);
3813 static const struct irq_domain_ops amd_ir_domain_ops = {
3814 .alloc = irq_remapping_alloc,
3815 .free = irq_remapping_free,
3816 .activate = irq_remapping_activate,
3817 .deactivate = irq_remapping_deactivate,
3820 int amd_iommu_activate_guest_mode(void *data)
3822 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3823 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3825 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3826 !entry || entry->lo.fields_vapic.guest_mode)
3827 return 0;
3829 entry->lo.val = 0;
3830 entry->hi.val = 0;
3832 entry->lo.fields_vapic.guest_mode = 1;
3833 entry->lo.fields_vapic.ga_log_intr = 1;
3834 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
3835 entry->hi.fields.vector = ir_data->ga_vector;
3836 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
3838 return modify_irte_ga(ir_data->irq_2_irte.devid,
3839 ir_data->irq_2_irte.index, entry, NULL);
3841 EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
3843 int amd_iommu_deactivate_guest_mode(void *data)
3845 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
3846 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
3847 struct irq_cfg *cfg = ir_data->cfg;
3849 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
3850 !entry || !entry->lo.fields_vapic.guest_mode)
3851 return 0;
3853 entry->lo.val = 0;
3854 entry->hi.val = 0;
3856 entry->lo.fields_remap.dm = apic->irq_dest_mode;
3857 entry->lo.fields_remap.int_type = apic->irq_delivery_mode;
3858 entry->hi.fields.vector = cfg->vector;
3859 entry->lo.fields_remap.destination =
3860 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
3861 entry->hi.fields.destination =
3862 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
3864 return modify_irte_ga(ir_data->irq_2_irte.devid,
3865 ir_data->irq_2_irte.index, entry, NULL);
3867 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
3869 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
3871 int ret;
3872 struct amd_iommu *iommu;
3873 struct amd_iommu_pi_data *pi_data = vcpu_info;
3874 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
3875 struct amd_ir_data *ir_data = data->chip_data;
3876 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3877 struct iommu_dev_data *dev_data = search_dev_data(irte_info->devid);
3879 /* Note:
3880 * This device has never been set up for guest mode.
3881 * we should not modify the IRTE
3883 if (!dev_data || !dev_data->use_vapic)
3884 return 0;
3886 ir_data->cfg = irqd_cfg(data);
3887 pi_data->ir_data = ir_data;
3889 /* Note:
3890 * SVM tries to set up for VAPIC mode, but we are in
3891 * legacy mode. So, we force legacy mode instead.
3893 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3894 pr_debug("%s: Fall back to using intr legacy remap\n",
3895 __func__);
3896 pi_data->is_guest_mode = false;
3899 iommu = amd_iommu_rlookup_table[irte_info->devid];
3900 if (iommu == NULL)
3901 return -EINVAL;
3903 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
3904 if (pi_data->is_guest_mode) {
3905 ir_data->ga_root_ptr = (pi_data->base >> 12);
3906 ir_data->ga_vector = vcpu_pi_info->vector;
3907 ir_data->ga_tag = pi_data->ga_tag;
3908 ret = amd_iommu_activate_guest_mode(ir_data);
3909 if (!ret)
3910 ir_data->cached_ga_tag = pi_data->ga_tag;
3911 } else {
3912 ret = amd_iommu_deactivate_guest_mode(ir_data);
3915 * This communicates the ga_tag back to the caller
3916 * so that it can do all the necessary clean up.
3918 if (!ret)
3919 ir_data->cached_ga_tag = 0;
3922 return ret;
3926 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
3927 struct amd_ir_data *ir_data,
3928 struct irq_2_irte *irte_info,
3929 struct irq_cfg *cfg)
3933 * Atomically updates the IRTE with the new destination, vector
3934 * and flushes the interrupt entry cache.
3936 iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
3937 irte_info->index, cfg->vector,
3938 cfg->dest_apicid);
3941 static int amd_ir_set_affinity(struct irq_data *data,
3942 const struct cpumask *mask, bool force)
3944 struct amd_ir_data *ir_data = data->chip_data;
3945 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3946 struct irq_cfg *cfg = irqd_cfg(data);
3947 struct irq_data *parent = data->parent_data;
3948 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
3949 int ret;
3951 if (!iommu)
3952 return -ENODEV;
3954 ret = parent->chip->irq_set_affinity(parent, mask, force);
3955 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3956 return ret;
3958 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
3960 * After this point, all the interrupts will start arriving
3961 * at the new destination. So, time to cleanup the previous
3962 * vector allocation.
3964 send_cleanup_vector(cfg);
3966 return IRQ_SET_MASK_OK_DONE;
3969 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3971 struct amd_ir_data *ir_data = irq_data->chip_data;
3973 *msg = ir_data->msi_entry;
3976 static struct irq_chip amd_ir_chip = {
3977 .name = "AMD-IR",
3978 .irq_ack = apic_ack_irq,
3979 .irq_set_affinity = amd_ir_set_affinity,
3980 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
3981 .irq_compose_msi_msg = ir_compose_msi_msg,
3984 int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3986 struct fwnode_handle *fn;
3988 fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
3989 if (!fn)
3990 return -ENOMEM;
3991 iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
3992 irq_domain_free_fwnode(fn);
3993 if (!iommu->ir_domain)
3994 return -ENOMEM;
3996 iommu->ir_domain->parent = arch_get_ir_parent_domain();
3997 iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
3998 "AMD-IR-MSI",
3999 iommu->index);
4000 return 0;
4003 int amd_iommu_update_ga(int cpu, bool is_run, void *data)
4005 unsigned long flags;
4006 struct amd_iommu *iommu;
4007 struct irq_remap_table *table;
4008 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4009 int devid = ir_data->irq_2_irte.devid;
4010 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4011 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
4013 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
4014 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
4015 return 0;
4017 iommu = amd_iommu_rlookup_table[devid];
4018 if (!iommu)
4019 return -ENODEV;
4021 table = get_irq_table(devid);
4022 if (!table)
4023 return -ENODEV;
4025 raw_spin_lock_irqsave(&table->lock, flags);
4027 if (ref->lo.fields_vapic.guest_mode) {
4028 if (cpu >= 0) {
4029 ref->lo.fields_vapic.destination =
4030 APICID_TO_IRTE_DEST_LO(cpu);
4031 ref->hi.fields.destination =
4032 APICID_TO_IRTE_DEST_HI(cpu);
4034 ref->lo.fields_vapic.is_run = is_run;
4035 barrier();
4038 raw_spin_unlock_irqrestore(&table->lock, flags);
4040 iommu_flush_irt(iommu, devid);
4041 iommu_completion_wait(iommu);
4042 return 0;
4044 EXPORT_SYMBOL(amd_iommu_update_ga);
4045 #endif