2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <jroedel@suse.de>
4 * Leo Duran <leo.duran@amd.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/ratelimit.h>
21 #include <linux/pci.h>
22 #include <linux/acpi.h>
23 #include <linux/amba/bus.h>
24 #include <linux/platform_device.h>
25 #include <linux/pci-ats.h>
26 #include <linux/bitmap.h>
27 #include <linux/slab.h>
28 #include <linux/debugfs.h>
29 #include <linux/scatterlist.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/iommu-helper.h>
32 #include <linux/iommu.h>
33 #include <linux/delay.h>
34 #include <linux/amd-iommu.h>
35 #include <linux/notifier.h>
36 #include <linux/export.h>
37 #include <linux/irq.h>
38 #include <linux/msi.h>
39 #include <linux/dma-contiguous.h>
40 #include <linux/irqdomain.h>
41 #include <linux/percpu.h>
42 #include <linux/iova.h>
43 #include <asm/irq_remapping.h>
44 #include <asm/io_apic.h>
46 #include <asm/hw_irq.h>
47 #include <asm/msidef.h>
48 #include <asm/proto.h>
49 #include <asm/iommu.h>
53 #include "amd_iommu_proto.h"
54 #include "amd_iommu_types.h"
55 #include "irq_remapping.h"
57 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
59 #define LOOP_TIMEOUT 100000
61 /* IO virtual address start page frame number */
62 #define IOVA_START_PFN (1)
63 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
64 #define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
66 /* Reserved IOVA ranges */
67 #define MSI_RANGE_START (0xfee00000)
68 #define MSI_RANGE_END (0xfeefffff)
69 #define HT_RANGE_START (0xfd00000000ULL)
70 #define HT_RANGE_END (0xffffffffffULL)
73 * This bitmap is used to advertise the page sizes our hardware support
74 * to the IOMMU core, which will then use this information to split
75 * physically contiguous memory regions it is mapping into page sizes
78 * 512GB Pages are not supported due to a hardware bug
80 #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
82 static DEFINE_RWLOCK(amd_iommu_devtable_lock
);
84 /* List of all available dev_data structures */
85 static LIST_HEAD(dev_data_list
);
86 static DEFINE_SPINLOCK(dev_data_list_lock
);
88 LIST_HEAD(ioapic_map
);
90 LIST_HEAD(acpihid_map
);
92 #define FLUSH_QUEUE_SIZE 256
94 struct flush_queue_entry
{
95 unsigned long iova_pfn
;
97 struct dma_ops_domain
*dma_dom
;
103 struct flush_queue_entry
*entries
;
106 static DEFINE_PER_CPU(struct flush_queue
, flush_queue
);
108 static atomic_t queue_timer_on
;
109 static struct timer_list queue_timer
;
112 * Domain for untranslated devices - only allocated
113 * if iommu=pt passed on kernel cmd line.
115 const struct iommu_ops amd_iommu_ops
;
117 static ATOMIC_NOTIFIER_HEAD(ppr_notifier
);
118 int amd_iommu_max_glx_val
= -1;
120 static const struct dma_map_ops amd_iommu_dma_ops
;
123 * This struct contains device specific data for the IOMMU
125 struct iommu_dev_data
{
126 struct list_head list
; /* For domain->dev_list */
127 struct list_head dev_data_list
; /* For global dev_data_list */
128 struct protection_domain
*domain
; /* Domain the device is bound to */
129 u16 devid
; /* PCI Device ID */
130 u16 alias
; /* Alias Device ID */
131 bool iommu_v2
; /* Device can make use of IOMMUv2 */
132 bool passthrough
; /* Device is identity mapped */
136 } ats
; /* ATS state */
137 bool pri_tlp
; /* PASID TLB required for
139 u32 errata
; /* Bitmap for errata to apply */
140 bool use_vapic
; /* Enable device to use vapic mode */
142 struct ratelimit_state rs
; /* Ratelimit IOPF messages */
146 * general struct to manage commands send to an IOMMU
152 struct kmem_cache
*amd_iommu_irq_cache
;
154 static void update_domain(struct protection_domain
*domain
);
155 static int protection_domain_init(struct protection_domain
*domain
);
156 static void detach_device(struct device
*dev
);
159 * Data container for a dma_ops specific protection domain
161 struct dma_ops_domain
{
162 /* generic protection domain information */
163 struct protection_domain domain
;
166 struct iova_domain iovad
;
169 static struct iova_domain reserved_iova_ranges
;
170 static struct lock_class_key reserved_rbtree_key
;
172 /****************************************************************************
176 ****************************************************************************/
178 static inline int match_hid_uid(struct device
*dev
,
179 struct acpihid_map_entry
*entry
)
181 const char *hid
, *uid
;
183 hid
= acpi_device_hid(ACPI_COMPANION(dev
));
184 uid
= acpi_device_uid(ACPI_COMPANION(dev
));
190 return strcmp(hid
, entry
->hid
);
193 return strcmp(hid
, entry
->hid
);
195 return (strcmp(hid
, entry
->hid
) || strcmp(uid
, entry
->uid
));
198 static inline u16
get_pci_device_id(struct device
*dev
)
200 struct pci_dev
*pdev
= to_pci_dev(dev
);
202 return PCI_DEVID(pdev
->bus
->number
, pdev
->devfn
);
205 static inline int get_acpihid_device_id(struct device
*dev
,
206 struct acpihid_map_entry
**entry
)
208 struct acpihid_map_entry
*p
;
210 list_for_each_entry(p
, &acpihid_map
, list
) {
211 if (!match_hid_uid(dev
, p
)) {
220 static inline int get_device_id(struct device
*dev
)
225 devid
= get_pci_device_id(dev
);
227 devid
= get_acpihid_device_id(dev
, NULL
);
232 static struct protection_domain
*to_pdomain(struct iommu_domain
*dom
)
234 return container_of(dom
, struct protection_domain
, domain
);
237 static struct dma_ops_domain
* to_dma_ops_domain(struct protection_domain
*domain
)
239 BUG_ON(domain
->flags
!= PD_DMA_OPS_MASK
);
240 return container_of(domain
, struct dma_ops_domain
, domain
);
243 static struct iommu_dev_data
*alloc_dev_data(u16 devid
)
245 struct iommu_dev_data
*dev_data
;
248 dev_data
= kzalloc(sizeof(*dev_data
), GFP_KERNEL
);
252 dev_data
->devid
= devid
;
254 spin_lock_irqsave(&dev_data_list_lock
, flags
);
255 list_add_tail(&dev_data
->dev_data_list
, &dev_data_list
);
256 spin_unlock_irqrestore(&dev_data_list_lock
, flags
);
258 ratelimit_default_init(&dev_data
->rs
);
263 static struct iommu_dev_data
*search_dev_data(u16 devid
)
265 struct iommu_dev_data
*dev_data
;
268 spin_lock_irqsave(&dev_data_list_lock
, flags
);
269 list_for_each_entry(dev_data
, &dev_data_list
, dev_data_list
) {
270 if (dev_data
->devid
== devid
)
277 spin_unlock_irqrestore(&dev_data_list_lock
, flags
);
282 static int __last_alias(struct pci_dev
*pdev
, u16 alias
, void *data
)
284 *(u16
*)data
= alias
;
288 static u16
get_alias(struct device
*dev
)
290 struct pci_dev
*pdev
= to_pci_dev(dev
);
291 u16 devid
, ivrs_alias
, pci_alias
;
293 /* The callers make sure that get_device_id() does not fail here */
294 devid
= get_device_id(dev
);
295 ivrs_alias
= amd_iommu_alias_table
[devid
];
296 pci_for_each_dma_alias(pdev
, __last_alias
, &pci_alias
);
298 if (ivrs_alias
== pci_alias
)
304 * The IVRS is fairly reliable in telling us about aliases, but it
305 * can't know about every screwy device. If we don't have an IVRS
306 * reported alias, use the PCI reported alias. In that case we may
307 * still need to initialize the rlookup and dev_table entries if the
308 * alias is to a non-existent device.
310 if (ivrs_alias
== devid
) {
311 if (!amd_iommu_rlookup_table
[pci_alias
]) {
312 amd_iommu_rlookup_table
[pci_alias
] =
313 amd_iommu_rlookup_table
[devid
];
314 memcpy(amd_iommu_dev_table
[pci_alias
].data
,
315 amd_iommu_dev_table
[devid
].data
,
316 sizeof(amd_iommu_dev_table
[pci_alias
].data
));
322 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
323 "for device %s[%04x:%04x], kernel reported alias "
324 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias
), PCI_SLOT(ivrs_alias
),
325 PCI_FUNC(ivrs_alias
), dev_name(dev
), pdev
->vendor
, pdev
->device
,
326 PCI_BUS_NUM(pci_alias
), PCI_SLOT(pci_alias
),
327 PCI_FUNC(pci_alias
));
330 * If we don't have a PCI DMA alias and the IVRS alias is on the same
331 * bus, then the IVRS table may know about a quirk that we don't.
333 if (pci_alias
== devid
&&
334 PCI_BUS_NUM(ivrs_alias
) == pdev
->bus
->number
) {
335 pci_add_dma_alias(pdev
, ivrs_alias
& 0xff);
336 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
337 PCI_SLOT(ivrs_alias
), PCI_FUNC(ivrs_alias
),
344 static struct iommu_dev_data
*find_dev_data(u16 devid
)
346 struct iommu_dev_data
*dev_data
;
348 dev_data
= search_dev_data(devid
);
350 if (dev_data
== NULL
)
351 dev_data
= alloc_dev_data(devid
);
356 static struct iommu_dev_data
*get_dev_data(struct device
*dev
)
358 return dev
->archdata
.iommu
;
362 * Find or create an IOMMU group for a acpihid device.
364 static struct iommu_group
*acpihid_device_group(struct device
*dev
)
366 struct acpihid_map_entry
*p
, *entry
= NULL
;
369 devid
= get_acpihid_device_id(dev
, &entry
);
371 return ERR_PTR(devid
);
373 list_for_each_entry(p
, &acpihid_map
, list
) {
374 if ((devid
== p
->devid
) && p
->group
)
375 entry
->group
= p
->group
;
379 entry
->group
= generic_device_group(dev
);
381 iommu_group_ref_get(entry
->group
);
386 static bool pci_iommuv2_capable(struct pci_dev
*pdev
)
388 static const int caps
[] = {
391 PCI_EXT_CAP_ID_PASID
,
395 for (i
= 0; i
< 3; ++i
) {
396 pos
= pci_find_ext_capability(pdev
, caps
[i
]);
404 static bool pdev_pri_erratum(struct pci_dev
*pdev
, u32 erratum
)
406 struct iommu_dev_data
*dev_data
;
408 dev_data
= get_dev_data(&pdev
->dev
);
410 return dev_data
->errata
& (1 << erratum
) ? true : false;
414 * This function checks if the driver got a valid device from the caller to
415 * avoid dereferencing invalid pointers.
417 static bool check_device(struct device
*dev
)
421 if (!dev
|| !dev
->dma_mask
)
424 devid
= get_device_id(dev
);
428 /* Out of our scope? */
429 if (devid
> amd_iommu_last_bdf
)
432 if (amd_iommu_rlookup_table
[devid
] == NULL
)
438 static void init_iommu_group(struct device
*dev
)
440 struct iommu_group
*group
;
442 group
= iommu_group_get_for_dev(dev
);
446 iommu_group_put(group
);
449 static int iommu_init_device(struct device
*dev
)
451 struct iommu_dev_data
*dev_data
;
452 struct amd_iommu
*iommu
;
455 if (dev
->archdata
.iommu
)
458 devid
= get_device_id(dev
);
462 iommu
= amd_iommu_rlookup_table
[devid
];
464 dev_data
= find_dev_data(devid
);
468 dev_data
->alias
= get_alias(dev
);
470 if (dev_is_pci(dev
) && pci_iommuv2_capable(to_pci_dev(dev
))) {
471 struct amd_iommu
*iommu
;
473 iommu
= amd_iommu_rlookup_table
[dev_data
->devid
];
474 dev_data
->iommu_v2
= iommu
->is_iommu_v2
;
477 dev
->archdata
.iommu
= dev_data
;
479 iommu_device_link(&iommu
->iommu
, dev
);
484 static void iommu_ignore_device(struct device
*dev
)
489 devid
= get_device_id(dev
);
493 alias
= get_alias(dev
);
495 memset(&amd_iommu_dev_table
[devid
], 0, sizeof(struct dev_table_entry
));
496 memset(&amd_iommu_dev_table
[alias
], 0, sizeof(struct dev_table_entry
));
498 amd_iommu_rlookup_table
[devid
] = NULL
;
499 amd_iommu_rlookup_table
[alias
] = NULL
;
502 static void iommu_uninit_device(struct device
*dev
)
504 struct iommu_dev_data
*dev_data
;
505 struct amd_iommu
*iommu
;
508 devid
= get_device_id(dev
);
512 iommu
= amd_iommu_rlookup_table
[devid
];
514 dev_data
= search_dev_data(devid
);
518 if (dev_data
->domain
)
521 iommu_device_unlink(&iommu
->iommu
, dev
);
523 iommu_group_remove_device(dev
);
529 * We keep dev_data around for unplugged devices and reuse it when the
530 * device is re-plugged - not doing so would introduce a ton of races.
534 /****************************************************************************
536 * Interrupt handling functions
538 ****************************************************************************/
540 static void dump_dte_entry(u16 devid
)
544 for (i
= 0; i
< 4; ++i
)
545 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i
,
546 amd_iommu_dev_table
[devid
].data
[i
]);
549 static void dump_command(unsigned long phys_addr
)
551 struct iommu_cmd
*cmd
= phys_to_virt(phys_addr
);
554 for (i
= 0; i
< 4; ++i
)
555 pr_err("AMD-Vi: CMD[%d]: %08x\n", i
, cmd
->data
[i
]);
558 static void amd_iommu_report_page_fault(u16 devid
, u16 domain_id
,
559 u64 address
, int flags
)
561 struct iommu_dev_data
*dev_data
= NULL
;
562 struct pci_dev
*pdev
;
564 pdev
= pci_get_bus_and_slot(PCI_BUS_NUM(devid
), devid
& 0xff);
566 dev_data
= get_dev_data(&pdev
->dev
);
568 if (dev_data
&& __ratelimit(&dev_data
->rs
)) {
569 dev_err(&pdev
->dev
, "AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%016llx flags=0x%04x]\n",
570 domain_id
, address
, flags
);
571 } else if (printk_ratelimit()) {
572 pr_err("AMD-Vi: Event logged [IO_PAGE_FAULT device=%02x:%02x.%x domain=0x%04x address=0x%016llx flags=0x%04x]\n",
573 PCI_BUS_NUM(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
574 domain_id
, address
, flags
);
581 static void iommu_print_event(struct amd_iommu
*iommu
, void *__evt
)
583 int type
, devid
, domid
, flags
;
584 volatile u32
*event
= __evt
;
589 type
= (event
[1] >> EVENT_TYPE_SHIFT
) & EVENT_TYPE_MASK
;
590 devid
= (event
[0] >> EVENT_DEVID_SHIFT
) & EVENT_DEVID_MASK
;
591 domid
= (event
[1] >> EVENT_DOMID_SHIFT
) & EVENT_DOMID_MASK
;
592 flags
= (event
[1] >> EVENT_FLAGS_SHIFT
) & EVENT_FLAGS_MASK
;
593 address
= (u64
)(((u64
)event
[3]) << 32) | event
[2];
596 /* Did we hit the erratum? */
597 if (++count
== LOOP_TIMEOUT
) {
598 pr_err("AMD-Vi: No event written to event log\n");
605 if (type
== EVENT_TYPE_IO_FAULT
) {
606 amd_iommu_report_page_fault(devid
, domid
, address
, flags
);
609 printk(KERN_ERR
"AMD-Vi: Event logged [");
613 case EVENT_TYPE_ILL_DEV
:
614 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
615 "address=0x%016llx flags=0x%04x]\n",
616 PCI_BUS_NUM(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
618 dump_dte_entry(devid
);
620 case EVENT_TYPE_DEV_TAB_ERR
:
621 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
622 "address=0x%016llx flags=0x%04x]\n",
623 PCI_BUS_NUM(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
626 case EVENT_TYPE_PAGE_TAB_ERR
:
627 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
628 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
629 PCI_BUS_NUM(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
630 domid
, address
, flags
);
632 case EVENT_TYPE_ILL_CMD
:
633 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address
);
634 dump_command(address
);
636 case EVENT_TYPE_CMD_HARD_ERR
:
637 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
638 "flags=0x%04x]\n", address
, flags
);
640 case EVENT_TYPE_IOTLB_INV_TO
:
641 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
642 "address=0x%016llx]\n",
643 PCI_BUS_NUM(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
646 case EVENT_TYPE_INV_DEV_REQ
:
647 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
648 "address=0x%016llx flags=0x%04x]\n",
649 PCI_BUS_NUM(devid
), PCI_SLOT(devid
), PCI_FUNC(devid
),
653 printk(KERN_ERR
"UNKNOWN type=0x%02x]\n", type
);
656 memset(__evt
, 0, 4 * sizeof(u32
));
659 static void iommu_poll_events(struct amd_iommu
*iommu
)
663 head
= readl(iommu
->mmio_base
+ MMIO_EVT_HEAD_OFFSET
);
664 tail
= readl(iommu
->mmio_base
+ MMIO_EVT_TAIL_OFFSET
);
666 while (head
!= tail
) {
667 iommu_print_event(iommu
, iommu
->evt_buf
+ head
);
668 head
= (head
+ EVENT_ENTRY_SIZE
) % EVT_BUFFER_SIZE
;
671 writel(head
, iommu
->mmio_base
+ MMIO_EVT_HEAD_OFFSET
);
674 static void iommu_handle_ppr_entry(struct amd_iommu
*iommu
, u64
*raw
)
676 struct amd_iommu_fault fault
;
678 if (PPR_REQ_TYPE(raw
[0]) != PPR_REQ_FAULT
) {
679 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
683 fault
.address
= raw
[1];
684 fault
.pasid
= PPR_PASID(raw
[0]);
685 fault
.device_id
= PPR_DEVID(raw
[0]);
686 fault
.tag
= PPR_TAG(raw
[0]);
687 fault
.flags
= PPR_FLAGS(raw
[0]);
689 atomic_notifier_call_chain(&ppr_notifier
, 0, &fault
);
692 static void iommu_poll_ppr_log(struct amd_iommu
*iommu
)
696 if (iommu
->ppr_log
== NULL
)
699 head
= readl(iommu
->mmio_base
+ MMIO_PPR_HEAD_OFFSET
);
700 tail
= readl(iommu
->mmio_base
+ MMIO_PPR_TAIL_OFFSET
);
702 while (head
!= tail
) {
707 raw
= (u64
*)(iommu
->ppr_log
+ head
);
710 * Hardware bug: Interrupt may arrive before the entry is
711 * written to memory. If this happens we need to wait for the
714 for (i
= 0; i
< LOOP_TIMEOUT
; ++i
) {
715 if (PPR_REQ_TYPE(raw
[0]) != 0)
720 /* Avoid memcpy function-call overhead */
725 * To detect the hardware bug we need to clear the entry
728 raw
[0] = raw
[1] = 0UL;
730 /* Update head pointer of hardware ring-buffer */
731 head
= (head
+ PPR_ENTRY_SIZE
) % PPR_LOG_SIZE
;
732 writel(head
, iommu
->mmio_base
+ MMIO_PPR_HEAD_OFFSET
);
734 /* Handle PPR entry */
735 iommu_handle_ppr_entry(iommu
, entry
);
737 /* Refresh ring-buffer information */
738 head
= readl(iommu
->mmio_base
+ MMIO_PPR_HEAD_OFFSET
);
739 tail
= readl(iommu
->mmio_base
+ MMIO_PPR_TAIL_OFFSET
);
743 #ifdef CONFIG_IRQ_REMAP
744 static int (*iommu_ga_log_notifier
)(u32
);
746 int amd_iommu_register_ga_log_notifier(int (*notifier
)(u32
))
748 iommu_ga_log_notifier
= notifier
;
752 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier
);
754 static void iommu_poll_ga_log(struct amd_iommu
*iommu
)
756 u32 head
, tail
, cnt
= 0;
758 if (iommu
->ga_log
== NULL
)
761 head
= readl(iommu
->mmio_base
+ MMIO_GA_HEAD_OFFSET
);
762 tail
= readl(iommu
->mmio_base
+ MMIO_GA_TAIL_OFFSET
);
764 while (head
!= tail
) {
768 raw
= (u64
*)(iommu
->ga_log
+ head
);
771 /* Avoid memcpy function-call overhead */
774 /* Update head pointer of hardware ring-buffer */
775 head
= (head
+ GA_ENTRY_SIZE
) % GA_LOG_SIZE
;
776 writel(head
, iommu
->mmio_base
+ MMIO_GA_HEAD_OFFSET
);
778 /* Handle GA entry */
779 switch (GA_REQ_TYPE(log_entry
)) {
781 if (!iommu_ga_log_notifier
)
784 pr_debug("AMD-Vi: %s: devid=%#x, ga_tag=%#x\n",
785 __func__
, GA_DEVID(log_entry
),
788 if (iommu_ga_log_notifier(GA_TAG(log_entry
)) != 0)
789 pr_err("AMD-Vi: GA log notifier failed.\n");
796 #endif /* CONFIG_IRQ_REMAP */
798 #define AMD_IOMMU_INT_MASK \
799 (MMIO_STATUS_EVT_INT_MASK | \
800 MMIO_STATUS_PPR_INT_MASK | \
801 MMIO_STATUS_GALOG_INT_MASK)
803 irqreturn_t
amd_iommu_int_thread(int irq
, void *data
)
805 struct amd_iommu
*iommu
= (struct amd_iommu
*) data
;
806 u32 status
= readl(iommu
->mmio_base
+ MMIO_STATUS_OFFSET
);
808 while (status
& AMD_IOMMU_INT_MASK
) {
809 /* Enable EVT and PPR and GA interrupts again */
810 writel(AMD_IOMMU_INT_MASK
,
811 iommu
->mmio_base
+ MMIO_STATUS_OFFSET
);
813 if (status
& MMIO_STATUS_EVT_INT_MASK
) {
814 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
815 iommu_poll_events(iommu
);
818 if (status
& MMIO_STATUS_PPR_INT_MASK
) {
819 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
820 iommu_poll_ppr_log(iommu
);
823 #ifdef CONFIG_IRQ_REMAP
824 if (status
& MMIO_STATUS_GALOG_INT_MASK
) {
825 pr_devel("AMD-Vi: Processing IOMMU GA Log\n");
826 iommu_poll_ga_log(iommu
);
831 * Hardware bug: ERBT1312
832 * When re-enabling interrupt (by writing 1
833 * to clear the bit), the hardware might also try to set
834 * the interrupt bit in the event status register.
835 * In this scenario, the bit will be set, and disable
836 * subsequent interrupts.
838 * Workaround: The IOMMU driver should read back the
839 * status register and check if the interrupt bits are cleared.
840 * If not, driver will need to go through the interrupt handler
841 * again and re-clear the bits
843 status
= readl(iommu
->mmio_base
+ MMIO_STATUS_OFFSET
);
848 irqreturn_t
amd_iommu_int_handler(int irq
, void *data
)
850 return IRQ_WAKE_THREAD
;
853 /****************************************************************************
855 * IOMMU command queuing functions
857 ****************************************************************************/
859 static int wait_on_sem(volatile u64
*sem
)
863 while (*sem
== 0 && i
< LOOP_TIMEOUT
) {
868 if (i
== LOOP_TIMEOUT
) {
869 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
876 static void copy_cmd_to_buffer(struct amd_iommu
*iommu
,
877 struct iommu_cmd
*cmd
)
881 target
= iommu
->cmd_buf
+ iommu
->cmd_buf_tail
;
883 iommu
->cmd_buf_tail
+= sizeof(*cmd
);
884 iommu
->cmd_buf_tail
%= CMD_BUFFER_SIZE
;
886 /* Copy command to buffer */
887 memcpy(target
, cmd
, sizeof(*cmd
));
889 /* Tell the IOMMU about it */
890 writel(iommu
->cmd_buf_tail
, iommu
->mmio_base
+ MMIO_CMD_TAIL_OFFSET
);
893 static void build_completion_wait(struct iommu_cmd
*cmd
, u64 address
)
895 WARN_ON(address
& 0x7ULL
);
897 memset(cmd
, 0, sizeof(*cmd
));
898 cmd
->data
[0] = lower_32_bits(__pa(address
)) | CMD_COMPL_WAIT_STORE_MASK
;
899 cmd
->data
[1] = upper_32_bits(__pa(address
));
901 CMD_SET_TYPE(cmd
, CMD_COMPL_WAIT
);
904 static void build_inv_dte(struct iommu_cmd
*cmd
, u16 devid
)
906 memset(cmd
, 0, sizeof(*cmd
));
907 cmd
->data
[0] = devid
;
908 CMD_SET_TYPE(cmd
, CMD_INV_DEV_ENTRY
);
911 static void build_inv_iommu_pages(struct iommu_cmd
*cmd
, u64 address
,
912 size_t size
, u16 domid
, int pde
)
917 pages
= iommu_num_pages(address
, size
, PAGE_SIZE
);
922 * If we have to flush more than one page, flush all
923 * TLB entries for this domain
925 address
= CMD_INV_IOMMU_ALL_PAGES_ADDRESS
;
929 address
&= PAGE_MASK
;
931 memset(cmd
, 0, sizeof(*cmd
));
932 cmd
->data
[1] |= domid
;
933 cmd
->data
[2] = lower_32_bits(address
);
934 cmd
->data
[3] = upper_32_bits(address
);
935 CMD_SET_TYPE(cmd
, CMD_INV_IOMMU_PAGES
);
936 if (s
) /* size bit - we flush more than one 4kb page */
937 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK
;
938 if (pde
) /* PDE bit - we want to flush everything, not only the PTEs */
939 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK
;
942 static void build_inv_iotlb_pages(struct iommu_cmd
*cmd
, u16 devid
, int qdep
,
943 u64 address
, size_t size
)
948 pages
= iommu_num_pages(address
, size
, PAGE_SIZE
);
953 * If we have to flush more than one page, flush all
954 * TLB entries for this domain
956 address
= CMD_INV_IOMMU_ALL_PAGES_ADDRESS
;
960 address
&= PAGE_MASK
;
962 memset(cmd
, 0, sizeof(*cmd
));
963 cmd
->data
[0] = devid
;
964 cmd
->data
[0] |= (qdep
& 0xff) << 24;
965 cmd
->data
[1] = devid
;
966 cmd
->data
[2] = lower_32_bits(address
);
967 cmd
->data
[3] = upper_32_bits(address
);
968 CMD_SET_TYPE(cmd
, CMD_INV_IOTLB_PAGES
);
970 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK
;
973 static void build_inv_iommu_pasid(struct iommu_cmd
*cmd
, u16 domid
, int pasid
,
974 u64 address
, bool size
)
976 memset(cmd
, 0, sizeof(*cmd
));
978 address
&= ~(0xfffULL
);
980 cmd
->data
[0] = pasid
;
981 cmd
->data
[1] = domid
;
982 cmd
->data
[2] = lower_32_bits(address
);
983 cmd
->data
[3] = upper_32_bits(address
);
984 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK
;
985 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_GN_MASK
;
987 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK
;
988 CMD_SET_TYPE(cmd
, CMD_INV_IOMMU_PAGES
);
991 static void build_inv_iotlb_pasid(struct iommu_cmd
*cmd
, u16 devid
, int pasid
,
992 int qdep
, u64 address
, bool size
)
994 memset(cmd
, 0, sizeof(*cmd
));
996 address
&= ~(0xfffULL
);
998 cmd
->data
[0] = devid
;
999 cmd
->data
[0] |= ((pasid
>> 8) & 0xff) << 16;
1000 cmd
->data
[0] |= (qdep
& 0xff) << 24;
1001 cmd
->data
[1] = devid
;
1002 cmd
->data
[1] |= (pasid
& 0xff) << 16;
1003 cmd
->data
[2] = lower_32_bits(address
);
1004 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_GN_MASK
;
1005 cmd
->data
[3] = upper_32_bits(address
);
1007 cmd
->data
[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK
;
1008 CMD_SET_TYPE(cmd
, CMD_INV_IOTLB_PAGES
);
1011 static void build_complete_ppr(struct iommu_cmd
*cmd
, u16 devid
, int pasid
,
1012 int status
, int tag
, bool gn
)
1014 memset(cmd
, 0, sizeof(*cmd
));
1016 cmd
->data
[0] = devid
;
1018 cmd
->data
[1] = pasid
;
1019 cmd
->data
[2] = CMD_INV_IOMMU_PAGES_GN_MASK
;
1021 cmd
->data
[3] = tag
& 0x1ff;
1022 cmd
->data
[3] |= (status
& PPR_STATUS_MASK
) << PPR_STATUS_SHIFT
;
1024 CMD_SET_TYPE(cmd
, CMD_COMPLETE_PPR
);
1027 static void build_inv_all(struct iommu_cmd
*cmd
)
1029 memset(cmd
, 0, sizeof(*cmd
));
1030 CMD_SET_TYPE(cmd
, CMD_INV_ALL
);
1033 static void build_inv_irt(struct iommu_cmd
*cmd
, u16 devid
)
1035 memset(cmd
, 0, sizeof(*cmd
));
1036 cmd
->data
[0] = devid
;
1037 CMD_SET_TYPE(cmd
, CMD_INV_IRT
);
1041 * Writes the command to the IOMMUs command buffer and informs the
1042 * hardware about the new command.
1044 static int __iommu_queue_command_sync(struct amd_iommu
*iommu
,
1045 struct iommu_cmd
*cmd
,
1048 bool read_head
= true;
1049 u32 left
, next_tail
;
1051 next_tail
= (iommu
->cmd_buf_tail
+ sizeof(*cmd
)) % CMD_BUFFER_SIZE
;
1053 left
= (iommu
->cmd_buf_head
- next_tail
) % CMD_BUFFER_SIZE
;
1056 struct iommu_cmd sync_cmd
;
1060 /* Update head and recheck remaining space */
1061 iommu
->cmd_buf_head
= readl(iommu
->mmio_base
+
1062 MMIO_CMD_HEAD_OFFSET
);
1071 build_completion_wait(&sync_cmd
, (u64
)&iommu
->cmd_sem
);
1072 copy_cmd_to_buffer(iommu
, &sync_cmd
);
1074 if ((ret
= wait_on_sem(&iommu
->cmd_sem
)) != 0)
1080 copy_cmd_to_buffer(iommu
, cmd
);
1082 /* We need to sync now to make sure all commands are processed */
1083 iommu
->need_sync
= sync
;
1088 static int iommu_queue_command_sync(struct amd_iommu
*iommu
,
1089 struct iommu_cmd
*cmd
,
1092 unsigned long flags
;
1095 spin_lock_irqsave(&iommu
->lock
, flags
);
1096 ret
= __iommu_queue_command_sync(iommu
, cmd
, sync
);
1097 spin_unlock_irqrestore(&iommu
->lock
, flags
);
1102 static int iommu_queue_command(struct amd_iommu
*iommu
, struct iommu_cmd
*cmd
)
1104 return iommu_queue_command_sync(iommu
, cmd
, true);
1108 * This function queues a completion wait command into the command
1109 * buffer of an IOMMU
1111 static int iommu_completion_wait(struct amd_iommu
*iommu
)
1113 struct iommu_cmd cmd
;
1114 unsigned long flags
;
1117 if (!iommu
->need_sync
)
1121 build_completion_wait(&cmd
, (u64
)&iommu
->cmd_sem
);
1123 spin_lock_irqsave(&iommu
->lock
, flags
);
1127 ret
= __iommu_queue_command_sync(iommu
, &cmd
, false);
1131 ret
= wait_on_sem(&iommu
->cmd_sem
);
1134 spin_unlock_irqrestore(&iommu
->lock
, flags
);
1139 static int iommu_flush_dte(struct amd_iommu
*iommu
, u16 devid
)
1141 struct iommu_cmd cmd
;
1143 build_inv_dte(&cmd
, devid
);
1145 return iommu_queue_command(iommu
, &cmd
);
1148 static void iommu_flush_dte_all(struct amd_iommu
*iommu
)
1152 for (devid
= 0; devid
<= 0xffff; ++devid
)
1153 iommu_flush_dte(iommu
, devid
);
1155 iommu_completion_wait(iommu
);
1159 * This function uses heavy locking and may disable irqs for some time. But
1160 * this is no issue because it is only called during resume.
1162 static void iommu_flush_tlb_all(struct amd_iommu
*iommu
)
1166 for (dom_id
= 0; dom_id
<= 0xffff; ++dom_id
) {
1167 struct iommu_cmd cmd
;
1168 build_inv_iommu_pages(&cmd
, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS
,
1170 iommu_queue_command(iommu
, &cmd
);
1173 iommu_completion_wait(iommu
);
1176 static void iommu_flush_all(struct amd_iommu
*iommu
)
1178 struct iommu_cmd cmd
;
1180 build_inv_all(&cmd
);
1182 iommu_queue_command(iommu
, &cmd
);
1183 iommu_completion_wait(iommu
);
1186 static void iommu_flush_irt(struct amd_iommu
*iommu
, u16 devid
)
1188 struct iommu_cmd cmd
;
1190 build_inv_irt(&cmd
, devid
);
1192 iommu_queue_command(iommu
, &cmd
);
1195 static void iommu_flush_irt_all(struct amd_iommu
*iommu
)
1199 for (devid
= 0; devid
<= MAX_DEV_TABLE_ENTRIES
; devid
++)
1200 iommu_flush_irt(iommu
, devid
);
1202 iommu_completion_wait(iommu
);
1205 void iommu_flush_all_caches(struct amd_iommu
*iommu
)
1207 if (iommu_feature(iommu
, FEATURE_IA
)) {
1208 iommu_flush_all(iommu
);
1210 iommu_flush_dte_all(iommu
);
1211 iommu_flush_irt_all(iommu
);
1212 iommu_flush_tlb_all(iommu
);
1217 * Command send function for flushing on-device TLB
1219 static int device_flush_iotlb(struct iommu_dev_data
*dev_data
,
1220 u64 address
, size_t size
)
1222 struct amd_iommu
*iommu
;
1223 struct iommu_cmd cmd
;
1226 qdep
= dev_data
->ats
.qdep
;
1227 iommu
= amd_iommu_rlookup_table
[dev_data
->devid
];
1229 build_inv_iotlb_pages(&cmd
, dev_data
->devid
, qdep
, address
, size
);
1231 return iommu_queue_command(iommu
, &cmd
);
1235 * Command send function for invalidating a device table entry
1237 static int device_flush_dte(struct iommu_dev_data
*dev_data
)
1239 struct amd_iommu
*iommu
;
1243 iommu
= amd_iommu_rlookup_table
[dev_data
->devid
];
1244 alias
= dev_data
->alias
;
1246 ret
= iommu_flush_dte(iommu
, dev_data
->devid
);
1247 if (!ret
&& alias
!= dev_data
->devid
)
1248 ret
= iommu_flush_dte(iommu
, alias
);
1252 if (dev_data
->ats
.enabled
)
1253 ret
= device_flush_iotlb(dev_data
, 0, ~0UL);
1259 * TLB invalidation function which is called from the mapping functions.
1260 * It invalidates a single PTE if the range to flush is within a single
1261 * page. Otherwise it flushes the whole TLB of the IOMMU.
1263 static void __domain_flush_pages(struct protection_domain
*domain
,
1264 u64 address
, size_t size
, int pde
)
1266 struct iommu_dev_data
*dev_data
;
1267 struct iommu_cmd cmd
;
1270 build_inv_iommu_pages(&cmd
, address
, size
, domain
->id
, pde
);
1272 for (i
= 0; i
< amd_iommu_get_num_iommus(); ++i
) {
1273 if (!domain
->dev_iommu
[i
])
1277 * Devices of this domain are behind this IOMMU
1278 * We need a TLB flush
1280 ret
|= iommu_queue_command(amd_iommus
[i
], &cmd
);
1283 list_for_each_entry(dev_data
, &domain
->dev_list
, list
) {
1285 if (!dev_data
->ats
.enabled
)
1288 ret
|= device_flush_iotlb(dev_data
, address
, size
);
1294 static void domain_flush_pages(struct protection_domain
*domain
,
1295 u64 address
, size_t size
)
1297 __domain_flush_pages(domain
, address
, size
, 0);
1300 /* Flush the whole IO/TLB for a given protection domain */
1301 static void domain_flush_tlb(struct protection_domain
*domain
)
1303 __domain_flush_pages(domain
, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS
, 0);
1306 /* Flush the whole IO/TLB for a given protection domain - including PDE */
1307 static void domain_flush_tlb_pde(struct protection_domain
*domain
)
1309 __domain_flush_pages(domain
, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS
, 1);
1312 static void domain_flush_complete(struct protection_domain
*domain
)
1316 for (i
= 0; i
< amd_iommu_get_num_iommus(); ++i
) {
1317 if (domain
&& !domain
->dev_iommu
[i
])
1321 * Devices of this domain are behind this IOMMU
1322 * We need to wait for completion of all commands.
1324 iommu_completion_wait(amd_iommus
[i
]);
1330 * This function flushes the DTEs for all devices in domain
1332 static void domain_flush_devices(struct protection_domain
*domain
)
1334 struct iommu_dev_data
*dev_data
;
1336 list_for_each_entry(dev_data
, &domain
->dev_list
, list
)
1337 device_flush_dte(dev_data
);
1340 /****************************************************************************
1342 * The functions below are used the create the page table mappings for
1343 * unity mapped regions.
1345 ****************************************************************************/
1348 * This function is used to add another level to an IO page table. Adding
1349 * another level increases the size of the address space by 9 bits to a size up
1352 static bool increase_address_space(struct protection_domain
*domain
,
1357 if (domain
->mode
== PAGE_MODE_6_LEVEL
)
1358 /* address space already 64 bit large */
1361 pte
= (void *)get_zeroed_page(gfp
);
1365 *pte
= PM_LEVEL_PDE(domain
->mode
,
1366 virt_to_phys(domain
->pt_root
));
1367 domain
->pt_root
= pte
;
1369 domain
->updated
= true;
1374 static u64
*alloc_pte(struct protection_domain
*domain
,
1375 unsigned long address
,
1376 unsigned long page_size
,
1383 BUG_ON(!is_power_of_2(page_size
));
1385 while (address
> PM_LEVEL_SIZE(domain
->mode
))
1386 increase_address_space(domain
, gfp
);
1388 level
= domain
->mode
- 1;
1389 pte
= &domain
->pt_root
[PM_LEVEL_INDEX(level
, address
)];
1390 address
= PAGE_SIZE_ALIGN(address
, page_size
);
1391 end_lvl
= PAGE_SIZE_LEVEL(page_size
);
1393 while (level
> end_lvl
) {
1398 if (!IOMMU_PTE_PRESENT(__pte
)) {
1399 page
= (u64
*)get_zeroed_page(gfp
);
1403 __npte
= PM_LEVEL_PDE(level
, virt_to_phys(page
));
1405 /* pte could have been changed somewhere. */
1406 if (cmpxchg64(pte
, __pte
, __npte
) != __pte
) {
1407 free_page((unsigned long)page
);
1412 /* No level skipping support yet */
1413 if (PM_PTE_LEVEL(*pte
) != level
)
1418 pte
= IOMMU_PTE_PAGE(*pte
);
1420 if (pte_page
&& level
== end_lvl
)
1423 pte
= &pte
[PM_LEVEL_INDEX(level
, address
)];
1430 * This function checks if there is a PTE for a given dma address. If
1431 * there is one, it returns the pointer to it.
1433 static u64
*fetch_pte(struct protection_domain
*domain
,
1434 unsigned long address
,
1435 unsigned long *page_size
)
1440 if (address
> PM_LEVEL_SIZE(domain
->mode
))
1443 level
= domain
->mode
- 1;
1444 pte
= &domain
->pt_root
[PM_LEVEL_INDEX(level
, address
)];
1445 *page_size
= PTE_LEVEL_PAGE_SIZE(level
);
1450 if (!IOMMU_PTE_PRESENT(*pte
))
1454 if (PM_PTE_LEVEL(*pte
) == 7 ||
1455 PM_PTE_LEVEL(*pte
) == 0)
1458 /* No level skipping support yet */
1459 if (PM_PTE_LEVEL(*pte
) != level
)
1464 /* Walk to the next level */
1465 pte
= IOMMU_PTE_PAGE(*pte
);
1466 pte
= &pte
[PM_LEVEL_INDEX(level
, address
)];
1467 *page_size
= PTE_LEVEL_PAGE_SIZE(level
);
1470 if (PM_PTE_LEVEL(*pte
) == 0x07) {
1471 unsigned long pte_mask
;
1474 * If we have a series of large PTEs, make
1475 * sure to return a pointer to the first one.
1477 *page_size
= pte_mask
= PTE_PAGE_SIZE(*pte
);
1478 pte_mask
= ~((PAGE_SIZE_PTE_COUNT(pte_mask
) << 3) - 1);
1479 pte
= (u64
*)(((unsigned long)pte
) & pte_mask
);
1486 * Generic mapping functions. It maps a physical address into a DMA
1487 * address space. It allocates the page table pages if necessary.
1488 * In the future it can be extended to a generic mapping function
1489 * supporting all features of AMD IOMMU page tables like level skipping
1490 * and full 64 bit address spaces.
1492 static int iommu_map_page(struct protection_domain
*dom
,
1493 unsigned long bus_addr
,
1494 unsigned long phys_addr
,
1495 unsigned long page_size
,
1502 BUG_ON(!IS_ALIGNED(bus_addr
, page_size
));
1503 BUG_ON(!IS_ALIGNED(phys_addr
, page_size
));
1505 if (!(prot
& IOMMU_PROT_MASK
))
1508 count
= PAGE_SIZE_PTE_COUNT(page_size
);
1509 pte
= alloc_pte(dom
, bus_addr
, page_size
, NULL
, gfp
);
1514 for (i
= 0; i
< count
; ++i
)
1515 if (IOMMU_PTE_PRESENT(pte
[i
]))
1519 __pte
= PAGE_SIZE_PTE(phys_addr
, page_size
);
1520 __pte
|= PM_LEVEL_ENC(7) | IOMMU_PTE_P
| IOMMU_PTE_FC
;
1522 __pte
= phys_addr
| IOMMU_PTE_P
| IOMMU_PTE_FC
;
1524 if (prot
& IOMMU_PROT_IR
)
1525 __pte
|= IOMMU_PTE_IR
;
1526 if (prot
& IOMMU_PROT_IW
)
1527 __pte
|= IOMMU_PTE_IW
;
1529 for (i
= 0; i
< count
; ++i
)
1537 static unsigned long iommu_unmap_page(struct protection_domain
*dom
,
1538 unsigned long bus_addr
,
1539 unsigned long page_size
)
1541 unsigned long long unmapped
;
1542 unsigned long unmap_size
;
1545 BUG_ON(!is_power_of_2(page_size
));
1549 while (unmapped
< page_size
) {
1551 pte
= fetch_pte(dom
, bus_addr
, &unmap_size
);
1556 count
= PAGE_SIZE_PTE_COUNT(unmap_size
);
1557 for (i
= 0; i
< count
; i
++)
1561 bus_addr
= (bus_addr
& ~(unmap_size
- 1)) + unmap_size
;
1562 unmapped
+= unmap_size
;
1565 BUG_ON(unmapped
&& !is_power_of_2(unmapped
));
1570 /****************************************************************************
1572 * The next functions belong to the address allocator for the dma_ops
1573 * interface functions.
1575 ****************************************************************************/
1578 static unsigned long dma_ops_alloc_iova(struct device
*dev
,
1579 struct dma_ops_domain
*dma_dom
,
1580 unsigned int pages
, u64 dma_mask
)
1582 unsigned long pfn
= 0;
1584 pages
= __roundup_pow_of_two(pages
);
1586 if (dma_mask
> DMA_BIT_MASK(32))
1587 pfn
= alloc_iova_fast(&dma_dom
->iovad
, pages
,
1588 IOVA_PFN(DMA_BIT_MASK(32)));
1591 pfn
= alloc_iova_fast(&dma_dom
->iovad
, pages
, IOVA_PFN(dma_mask
));
1593 return (pfn
<< PAGE_SHIFT
);
1596 static void dma_ops_free_iova(struct dma_ops_domain
*dma_dom
,
1597 unsigned long address
,
1600 pages
= __roundup_pow_of_two(pages
);
1601 address
>>= PAGE_SHIFT
;
1603 free_iova_fast(&dma_dom
->iovad
, address
, pages
);
1606 /****************************************************************************
1608 * The next functions belong to the domain allocation. A domain is
1609 * allocated for every IOMMU as the default domain. If device isolation
1610 * is enabled, every device get its own domain. The most important thing
1611 * about domains is the page table mapping the DMA address space they
1614 ****************************************************************************/
1617 * This function adds a protection domain to the global protection domain list
1619 static void add_domain_to_list(struct protection_domain
*domain
)
1621 unsigned long flags
;
1623 spin_lock_irqsave(&amd_iommu_pd_lock
, flags
);
1624 list_add(&domain
->list
, &amd_iommu_pd_list
);
1625 spin_unlock_irqrestore(&amd_iommu_pd_lock
, flags
);
1629 * This function removes a protection domain to the global
1630 * protection domain list
1632 static void del_domain_from_list(struct protection_domain
*domain
)
1634 unsigned long flags
;
1636 spin_lock_irqsave(&amd_iommu_pd_lock
, flags
);
1637 list_del(&domain
->list
);
1638 spin_unlock_irqrestore(&amd_iommu_pd_lock
, flags
);
1641 static u16
domain_id_alloc(void)
1643 unsigned long flags
;
1646 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
1647 id
= find_first_zero_bit(amd_iommu_pd_alloc_bitmap
, MAX_DOMAIN_ID
);
1649 if (id
> 0 && id
< MAX_DOMAIN_ID
)
1650 __set_bit(id
, amd_iommu_pd_alloc_bitmap
);
1653 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
1658 static void domain_id_free(int id
)
1660 unsigned long flags
;
1662 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
1663 if (id
> 0 && id
< MAX_DOMAIN_ID
)
1664 __clear_bit(id
, amd_iommu_pd_alloc_bitmap
);
1665 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
1668 #define DEFINE_FREE_PT_FN(LVL, FN) \
1669 static void free_pt_##LVL (unsigned long __pt) \
1677 for (i = 0; i < 512; ++i) { \
1678 /* PTE present? */ \
1679 if (!IOMMU_PTE_PRESENT(pt[i])) \
1683 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1684 PM_PTE_LEVEL(pt[i]) == 7) \
1687 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1690 free_page((unsigned long)pt); \
1693 DEFINE_FREE_PT_FN(l2
, free_page
)
1694 DEFINE_FREE_PT_FN(l3
, free_pt_l2
)
1695 DEFINE_FREE_PT_FN(l4
, free_pt_l3
)
1696 DEFINE_FREE_PT_FN(l5
, free_pt_l4
)
1697 DEFINE_FREE_PT_FN(l6
, free_pt_l5
)
1699 static void free_pagetable(struct protection_domain
*domain
)
1701 unsigned long root
= (unsigned long)domain
->pt_root
;
1703 switch (domain
->mode
) {
1704 case PAGE_MODE_NONE
:
1706 case PAGE_MODE_1_LEVEL
:
1709 case PAGE_MODE_2_LEVEL
:
1712 case PAGE_MODE_3_LEVEL
:
1715 case PAGE_MODE_4_LEVEL
:
1718 case PAGE_MODE_5_LEVEL
:
1721 case PAGE_MODE_6_LEVEL
:
1729 static void free_gcr3_tbl_level1(u64
*tbl
)
1734 for (i
= 0; i
< 512; ++i
) {
1735 if (!(tbl
[i
] & GCR3_VALID
))
1738 ptr
= __va(tbl
[i
] & PAGE_MASK
);
1740 free_page((unsigned long)ptr
);
1744 static void free_gcr3_tbl_level2(u64
*tbl
)
1749 for (i
= 0; i
< 512; ++i
) {
1750 if (!(tbl
[i
] & GCR3_VALID
))
1753 ptr
= __va(tbl
[i
] & PAGE_MASK
);
1755 free_gcr3_tbl_level1(ptr
);
1759 static void free_gcr3_table(struct protection_domain
*domain
)
1761 if (domain
->glx
== 2)
1762 free_gcr3_tbl_level2(domain
->gcr3_tbl
);
1763 else if (domain
->glx
== 1)
1764 free_gcr3_tbl_level1(domain
->gcr3_tbl
);
1766 BUG_ON(domain
->glx
!= 0);
1768 free_page((unsigned long)domain
->gcr3_tbl
);
1772 * Free a domain, only used if something went wrong in the
1773 * allocation path and we need to free an already allocated page table
1775 static void dma_ops_domain_free(struct dma_ops_domain
*dom
)
1780 del_domain_from_list(&dom
->domain
);
1782 put_iova_domain(&dom
->iovad
);
1784 free_pagetable(&dom
->domain
);
1787 domain_id_free(dom
->domain
.id
);
1793 * Allocates a new protection domain usable for the dma_ops functions.
1794 * It also initializes the page table and the address allocator data
1795 * structures required for the dma_ops interface
1797 static struct dma_ops_domain
*dma_ops_domain_alloc(void)
1799 struct dma_ops_domain
*dma_dom
;
1801 dma_dom
= kzalloc(sizeof(struct dma_ops_domain
), GFP_KERNEL
);
1805 if (protection_domain_init(&dma_dom
->domain
))
1808 dma_dom
->domain
.mode
= PAGE_MODE_3_LEVEL
;
1809 dma_dom
->domain
.pt_root
= (void *)get_zeroed_page(GFP_KERNEL
);
1810 dma_dom
->domain
.flags
= PD_DMA_OPS_MASK
;
1811 if (!dma_dom
->domain
.pt_root
)
1814 init_iova_domain(&dma_dom
->iovad
, PAGE_SIZE
,
1815 IOVA_START_PFN
, DMA_32BIT_PFN
);
1817 /* Initialize reserved ranges */
1818 copy_reserved_iova(&reserved_iova_ranges
, &dma_dom
->iovad
);
1820 add_domain_to_list(&dma_dom
->domain
);
1825 dma_ops_domain_free(dma_dom
);
1831 * little helper function to check whether a given protection domain is a
1834 static bool dma_ops_domain(struct protection_domain
*domain
)
1836 return domain
->flags
& PD_DMA_OPS_MASK
;
1839 static void set_dte_entry(u16 devid
, struct protection_domain
*domain
, bool ats
)
1844 if (domain
->mode
!= PAGE_MODE_NONE
)
1845 pte_root
= virt_to_phys(domain
->pt_root
);
1847 pte_root
|= (domain
->mode
& DEV_ENTRY_MODE_MASK
)
1848 << DEV_ENTRY_MODE_SHIFT
;
1849 pte_root
|= IOMMU_PTE_IR
| IOMMU_PTE_IW
| IOMMU_PTE_P
| IOMMU_PTE_TV
;
1851 flags
= amd_iommu_dev_table
[devid
].data
[1];
1854 flags
|= DTE_FLAG_IOTLB
;
1856 if (domain
->flags
& PD_IOMMUV2_MASK
) {
1857 u64 gcr3
= __pa(domain
->gcr3_tbl
);
1858 u64 glx
= domain
->glx
;
1861 pte_root
|= DTE_FLAG_GV
;
1862 pte_root
|= (glx
& DTE_GLX_MASK
) << DTE_GLX_SHIFT
;
1864 /* First mask out possible old values for GCR3 table */
1865 tmp
= DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B
;
1868 tmp
= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C
;
1871 /* Encode GCR3 table into DTE */
1872 tmp
= DTE_GCR3_VAL_A(gcr3
) << DTE_GCR3_SHIFT_A
;
1875 tmp
= DTE_GCR3_VAL_B(gcr3
) << DTE_GCR3_SHIFT_B
;
1878 tmp
= DTE_GCR3_VAL_C(gcr3
) << DTE_GCR3_SHIFT_C
;
1882 flags
&= ~(0xffffUL
);
1883 flags
|= domain
->id
;
1885 amd_iommu_dev_table
[devid
].data
[1] = flags
;
1886 amd_iommu_dev_table
[devid
].data
[0] = pte_root
;
1889 static void clear_dte_entry(u16 devid
)
1891 /* remove entry from the device table seen by the hardware */
1892 amd_iommu_dev_table
[devid
].data
[0] = IOMMU_PTE_P
| IOMMU_PTE_TV
;
1893 amd_iommu_dev_table
[devid
].data
[1] &= DTE_FLAG_MASK
;
1895 amd_iommu_apply_erratum_63(devid
);
1898 static void do_attach(struct iommu_dev_data
*dev_data
,
1899 struct protection_domain
*domain
)
1901 struct amd_iommu
*iommu
;
1905 iommu
= amd_iommu_rlookup_table
[dev_data
->devid
];
1906 alias
= dev_data
->alias
;
1907 ats
= dev_data
->ats
.enabled
;
1909 /* Update data structures */
1910 dev_data
->domain
= domain
;
1911 list_add(&dev_data
->list
, &domain
->dev_list
);
1913 /* Do reference counting */
1914 domain
->dev_iommu
[iommu
->index
] += 1;
1915 domain
->dev_cnt
+= 1;
1917 /* Update device table */
1918 set_dte_entry(dev_data
->devid
, domain
, ats
);
1919 if (alias
!= dev_data
->devid
)
1920 set_dte_entry(alias
, domain
, ats
);
1922 device_flush_dte(dev_data
);
1925 static void do_detach(struct iommu_dev_data
*dev_data
)
1927 struct amd_iommu
*iommu
;
1931 * First check if the device is still attached. It might already
1932 * be detached from its domain because the generic
1933 * iommu_detach_group code detached it and we try again here in
1934 * our alias handling.
1936 if (!dev_data
->domain
)
1939 iommu
= amd_iommu_rlookup_table
[dev_data
->devid
];
1940 alias
= dev_data
->alias
;
1942 /* decrease reference counters */
1943 dev_data
->domain
->dev_iommu
[iommu
->index
] -= 1;
1944 dev_data
->domain
->dev_cnt
-= 1;
1946 /* Update data structures */
1947 dev_data
->domain
= NULL
;
1948 list_del(&dev_data
->list
);
1949 clear_dte_entry(dev_data
->devid
);
1950 if (alias
!= dev_data
->devid
)
1951 clear_dte_entry(alias
);
1953 /* Flush the DTE entry */
1954 device_flush_dte(dev_data
);
1958 * If a device is not yet associated with a domain, this function does
1959 * assigns it visible for the hardware
1961 static int __attach_device(struct iommu_dev_data
*dev_data
,
1962 struct protection_domain
*domain
)
1967 * Must be called with IRQs disabled. Warn here to detect early
1970 WARN_ON(!irqs_disabled());
1973 spin_lock(&domain
->lock
);
1976 if (dev_data
->domain
!= NULL
)
1979 /* Attach alias group root */
1980 do_attach(dev_data
, domain
);
1987 spin_unlock(&domain
->lock
);
1993 static void pdev_iommuv2_disable(struct pci_dev
*pdev
)
1995 pci_disable_ats(pdev
);
1996 pci_disable_pri(pdev
);
1997 pci_disable_pasid(pdev
);
2000 /* FIXME: Change generic reset-function to do the same */
2001 static int pri_reset_while_enabled(struct pci_dev
*pdev
)
2006 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
2010 pci_read_config_word(pdev
, pos
+ PCI_PRI_CTRL
, &control
);
2011 control
|= PCI_PRI_CTRL_RESET
;
2012 pci_write_config_word(pdev
, pos
+ PCI_PRI_CTRL
, control
);
2017 static int pdev_iommuv2_enable(struct pci_dev
*pdev
)
2022 /* FIXME: Hardcode number of outstanding requests for now */
2024 if (pdev_pri_erratum(pdev
, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE
))
2026 reset_enable
= pdev_pri_erratum(pdev
, AMD_PRI_DEV_ERRATUM_ENABLE_RESET
);
2028 /* Only allow access to user-accessible pages */
2029 ret
= pci_enable_pasid(pdev
, 0);
2033 /* First reset the PRI state of the device */
2034 ret
= pci_reset_pri(pdev
);
2039 ret
= pci_enable_pri(pdev
, reqs
);
2044 ret
= pri_reset_while_enabled(pdev
);
2049 ret
= pci_enable_ats(pdev
, PAGE_SHIFT
);
2056 pci_disable_pri(pdev
);
2057 pci_disable_pasid(pdev
);
2062 /* FIXME: Move this to PCI code */
2063 #define PCI_PRI_TLP_OFF (1 << 15)
2065 static bool pci_pri_tlp_required(struct pci_dev
*pdev
)
2070 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
2074 pci_read_config_word(pdev
, pos
+ PCI_PRI_STATUS
, &status
);
2076 return (status
& PCI_PRI_TLP_OFF
) ? true : false;
2080 * If a device is not yet associated with a domain, this function
2081 * assigns it visible for the hardware
2083 static int attach_device(struct device
*dev
,
2084 struct protection_domain
*domain
)
2086 struct pci_dev
*pdev
;
2087 struct iommu_dev_data
*dev_data
;
2088 unsigned long flags
;
2091 dev_data
= get_dev_data(dev
);
2093 if (!dev_is_pci(dev
))
2094 goto skip_ats_check
;
2096 pdev
= to_pci_dev(dev
);
2097 if (domain
->flags
& PD_IOMMUV2_MASK
) {
2098 if (!dev_data
->passthrough
)
2101 if (dev_data
->iommu_v2
) {
2102 if (pdev_iommuv2_enable(pdev
) != 0)
2105 dev_data
->ats
.enabled
= true;
2106 dev_data
->ats
.qdep
= pci_ats_queue_depth(pdev
);
2107 dev_data
->pri_tlp
= pci_pri_tlp_required(pdev
);
2109 } else if (amd_iommu_iotlb_sup
&&
2110 pci_enable_ats(pdev
, PAGE_SHIFT
) == 0) {
2111 dev_data
->ats
.enabled
= true;
2112 dev_data
->ats
.qdep
= pci_ats_queue_depth(pdev
);
2116 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
2117 ret
= __attach_device(dev_data
, domain
);
2118 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
2121 * We might boot into a crash-kernel here. The crashed kernel
2122 * left the caches in the IOMMU dirty. So we have to flush
2123 * here to evict all dirty stuff.
2125 domain_flush_tlb_pde(domain
);
2131 * Removes a device from a protection domain (unlocked)
2133 static void __detach_device(struct iommu_dev_data
*dev_data
)
2135 struct protection_domain
*domain
;
2138 * Must be called with IRQs disabled. Warn here to detect early
2141 WARN_ON(!irqs_disabled());
2143 if (WARN_ON(!dev_data
->domain
))
2146 domain
= dev_data
->domain
;
2148 spin_lock(&domain
->lock
);
2150 do_detach(dev_data
);
2152 spin_unlock(&domain
->lock
);
2156 * Removes a device from a protection domain (with devtable_lock held)
2158 static void detach_device(struct device
*dev
)
2160 struct protection_domain
*domain
;
2161 struct iommu_dev_data
*dev_data
;
2162 unsigned long flags
;
2164 dev_data
= get_dev_data(dev
);
2165 domain
= dev_data
->domain
;
2167 /* lock device table */
2168 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
2169 __detach_device(dev_data
);
2170 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
2172 if (!dev_is_pci(dev
))
2175 if (domain
->flags
& PD_IOMMUV2_MASK
&& dev_data
->iommu_v2
)
2176 pdev_iommuv2_disable(to_pci_dev(dev
));
2177 else if (dev_data
->ats
.enabled
)
2178 pci_disable_ats(to_pci_dev(dev
));
2180 dev_data
->ats
.enabled
= false;
2183 static int amd_iommu_add_device(struct device
*dev
)
2185 struct iommu_dev_data
*dev_data
;
2186 struct iommu_domain
*domain
;
2187 struct amd_iommu
*iommu
;
2190 if (!check_device(dev
) || get_dev_data(dev
))
2193 devid
= get_device_id(dev
);
2197 iommu
= amd_iommu_rlookup_table
[devid
];
2199 ret
= iommu_init_device(dev
);
2201 if (ret
!= -ENOTSUPP
)
2202 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2205 iommu_ignore_device(dev
);
2206 dev
->dma_ops
= &nommu_dma_ops
;
2209 init_iommu_group(dev
);
2211 dev_data
= get_dev_data(dev
);
2215 if (iommu_pass_through
|| dev_data
->iommu_v2
)
2216 iommu_request_dm_for_dev(dev
);
2218 /* Domains are initialized for this device - have a look what we ended up with */
2219 domain
= iommu_get_domain_for_dev(dev
);
2220 if (domain
->type
== IOMMU_DOMAIN_IDENTITY
)
2221 dev_data
->passthrough
= true;
2223 dev
->dma_ops
= &amd_iommu_dma_ops
;
2226 iommu_completion_wait(iommu
);
2231 static void amd_iommu_remove_device(struct device
*dev
)
2233 struct amd_iommu
*iommu
;
2236 if (!check_device(dev
))
2239 devid
= get_device_id(dev
);
2243 iommu
= amd_iommu_rlookup_table
[devid
];
2245 iommu_uninit_device(dev
);
2246 iommu_completion_wait(iommu
);
2249 static struct iommu_group
*amd_iommu_device_group(struct device
*dev
)
2251 if (dev_is_pci(dev
))
2252 return pci_device_group(dev
);
2254 return acpihid_device_group(dev
);
2257 /*****************************************************************************
2259 * The next functions belong to the dma_ops mapping/unmapping code.
2261 *****************************************************************************/
2263 static void __queue_flush(struct flush_queue
*queue
)
2265 struct protection_domain
*domain
;
2266 unsigned long flags
;
2269 /* First flush TLB of all known domains */
2270 spin_lock_irqsave(&amd_iommu_pd_lock
, flags
);
2271 list_for_each_entry(domain
, &amd_iommu_pd_list
, list
)
2272 domain_flush_tlb(domain
);
2273 spin_unlock_irqrestore(&amd_iommu_pd_lock
, flags
);
2275 /* Wait until flushes have completed */
2276 domain_flush_complete(NULL
);
2278 for (idx
= 0; idx
< queue
->next
; ++idx
) {
2279 struct flush_queue_entry
*entry
;
2281 entry
= queue
->entries
+ idx
;
2283 free_iova_fast(&entry
->dma_dom
->iovad
,
2287 /* Not really necessary, just to make sure we catch any bugs */
2288 entry
->dma_dom
= NULL
;
2294 static void queue_flush_all(void)
2298 for_each_possible_cpu(cpu
) {
2299 struct flush_queue
*queue
;
2300 unsigned long flags
;
2302 queue
= per_cpu_ptr(&flush_queue
, cpu
);
2303 spin_lock_irqsave(&queue
->lock
, flags
);
2304 if (queue
->next
> 0)
2305 __queue_flush(queue
);
2306 spin_unlock_irqrestore(&queue
->lock
, flags
);
2310 static void queue_flush_timeout(unsigned long unsused
)
2312 atomic_set(&queue_timer_on
, 0);
2316 static void queue_add(struct dma_ops_domain
*dma_dom
,
2317 unsigned long address
, unsigned long pages
)
2319 struct flush_queue_entry
*entry
;
2320 struct flush_queue
*queue
;
2321 unsigned long flags
;
2324 pages
= __roundup_pow_of_two(pages
);
2325 address
>>= PAGE_SHIFT
;
2327 queue
= get_cpu_ptr(&flush_queue
);
2328 spin_lock_irqsave(&queue
->lock
, flags
);
2330 if (queue
->next
== FLUSH_QUEUE_SIZE
)
2331 __queue_flush(queue
);
2333 idx
= queue
->next
++;
2334 entry
= queue
->entries
+ idx
;
2336 entry
->iova_pfn
= address
;
2337 entry
->pages
= pages
;
2338 entry
->dma_dom
= dma_dom
;
2340 spin_unlock_irqrestore(&queue
->lock
, flags
);
2342 if (atomic_cmpxchg(&queue_timer_on
, 0, 1) == 0)
2343 mod_timer(&queue_timer
, jiffies
+ msecs_to_jiffies(10));
2345 put_cpu_ptr(&flush_queue
);
2350 * In the dma_ops path we only have the struct device. This function
2351 * finds the corresponding IOMMU, the protection domain and the
2352 * requestor id for a given device.
2353 * If the device is not yet associated with a domain this is also done
2356 static struct protection_domain
*get_domain(struct device
*dev
)
2358 struct protection_domain
*domain
;
2360 if (!check_device(dev
))
2361 return ERR_PTR(-EINVAL
);
2363 domain
= get_dev_data(dev
)->domain
;
2364 if (!dma_ops_domain(domain
))
2365 return ERR_PTR(-EBUSY
);
2370 static void update_device_table(struct protection_domain
*domain
)
2372 struct iommu_dev_data
*dev_data
;
2374 list_for_each_entry(dev_data
, &domain
->dev_list
, list
) {
2375 set_dte_entry(dev_data
->devid
, domain
, dev_data
->ats
.enabled
);
2377 if (dev_data
->devid
== dev_data
->alias
)
2380 /* There is an alias, update device table entry for it */
2381 set_dte_entry(dev_data
->alias
, domain
, dev_data
->ats
.enabled
);
2385 static void update_domain(struct protection_domain
*domain
)
2387 if (!domain
->updated
)
2390 update_device_table(domain
);
2392 domain_flush_devices(domain
);
2393 domain_flush_tlb_pde(domain
);
2395 domain
->updated
= false;
2398 static int dir2prot(enum dma_data_direction direction
)
2400 if (direction
== DMA_TO_DEVICE
)
2401 return IOMMU_PROT_IR
;
2402 else if (direction
== DMA_FROM_DEVICE
)
2403 return IOMMU_PROT_IW
;
2404 else if (direction
== DMA_BIDIRECTIONAL
)
2405 return IOMMU_PROT_IW
| IOMMU_PROT_IR
;
2410 * This function contains common code for mapping of a physically
2411 * contiguous memory region into DMA address space. It is used by all
2412 * mapping functions provided with this IOMMU driver.
2413 * Must be called with the domain lock held.
2415 static dma_addr_t
__map_single(struct device
*dev
,
2416 struct dma_ops_domain
*dma_dom
,
2419 enum dma_data_direction direction
,
2422 dma_addr_t offset
= paddr
& ~PAGE_MASK
;
2423 dma_addr_t address
, start
, ret
;
2428 pages
= iommu_num_pages(paddr
, size
, PAGE_SIZE
);
2431 address
= dma_ops_alloc_iova(dev
, dma_dom
, pages
, dma_mask
);
2432 if (address
== DMA_ERROR_CODE
)
2435 prot
= dir2prot(direction
);
2438 for (i
= 0; i
< pages
; ++i
) {
2439 ret
= iommu_map_page(&dma_dom
->domain
, start
, paddr
,
2440 PAGE_SIZE
, prot
, GFP_ATOMIC
);
2449 if (unlikely(amd_iommu_np_cache
)) {
2450 domain_flush_pages(&dma_dom
->domain
, address
, size
);
2451 domain_flush_complete(&dma_dom
->domain
);
2459 for (--i
; i
>= 0; --i
) {
2461 iommu_unmap_page(&dma_dom
->domain
, start
, PAGE_SIZE
);
2464 domain_flush_tlb(&dma_dom
->domain
);
2465 domain_flush_complete(&dma_dom
->domain
);
2467 dma_ops_free_iova(dma_dom
, address
, pages
);
2469 return DMA_ERROR_CODE
;
2473 * Does the reverse of the __map_single function. Must be called with
2474 * the domain lock held too
2476 static void __unmap_single(struct dma_ops_domain
*dma_dom
,
2477 dma_addr_t dma_addr
,
2481 dma_addr_t flush_addr
;
2482 dma_addr_t i
, start
;
2485 flush_addr
= dma_addr
;
2486 pages
= iommu_num_pages(dma_addr
, size
, PAGE_SIZE
);
2487 dma_addr
&= PAGE_MASK
;
2490 for (i
= 0; i
< pages
; ++i
) {
2491 iommu_unmap_page(&dma_dom
->domain
, start
, PAGE_SIZE
);
2495 if (amd_iommu_unmap_flush
) {
2496 dma_ops_free_iova(dma_dom
, dma_addr
, pages
);
2497 domain_flush_tlb(&dma_dom
->domain
);
2498 domain_flush_complete(&dma_dom
->domain
);
2500 queue_add(dma_dom
, dma_addr
, pages
);
2505 * The exported map_single function for dma_ops.
2507 static dma_addr_t
map_page(struct device
*dev
, struct page
*page
,
2508 unsigned long offset
, size_t size
,
2509 enum dma_data_direction dir
,
2510 unsigned long attrs
)
2512 phys_addr_t paddr
= page_to_phys(page
) + offset
;
2513 struct protection_domain
*domain
;
2514 struct dma_ops_domain
*dma_dom
;
2517 domain
= get_domain(dev
);
2518 if (PTR_ERR(domain
) == -EINVAL
)
2519 return (dma_addr_t
)paddr
;
2520 else if (IS_ERR(domain
))
2521 return DMA_ERROR_CODE
;
2523 dma_mask
= *dev
->dma_mask
;
2524 dma_dom
= to_dma_ops_domain(domain
);
2526 return __map_single(dev
, dma_dom
, paddr
, size
, dir
, dma_mask
);
2530 * The exported unmap_single function for dma_ops.
2532 static void unmap_page(struct device
*dev
, dma_addr_t dma_addr
, size_t size
,
2533 enum dma_data_direction dir
, unsigned long attrs
)
2535 struct protection_domain
*domain
;
2536 struct dma_ops_domain
*dma_dom
;
2538 domain
= get_domain(dev
);
2542 dma_dom
= to_dma_ops_domain(domain
);
2544 __unmap_single(dma_dom
, dma_addr
, size
, dir
);
2547 static int sg_num_pages(struct device
*dev
,
2548 struct scatterlist
*sglist
,
2551 unsigned long mask
, boundary_size
;
2552 struct scatterlist
*s
;
2555 mask
= dma_get_seg_boundary(dev
);
2556 boundary_size
= mask
+ 1 ? ALIGN(mask
+ 1, PAGE_SIZE
) >> PAGE_SHIFT
:
2557 1UL << (BITS_PER_LONG
- PAGE_SHIFT
);
2559 for_each_sg(sglist
, s
, nelems
, i
) {
2562 s
->dma_address
= npages
<< PAGE_SHIFT
;
2563 p
= npages
% boundary_size
;
2564 n
= iommu_num_pages(sg_phys(s
), s
->length
, PAGE_SIZE
);
2565 if (p
+ n
> boundary_size
)
2566 npages
+= boundary_size
- p
;
2574 * The exported map_sg function for dma_ops (handles scatter-gather
2577 static int map_sg(struct device
*dev
, struct scatterlist
*sglist
,
2578 int nelems
, enum dma_data_direction direction
,
2579 unsigned long attrs
)
2581 int mapped_pages
= 0, npages
= 0, prot
= 0, i
;
2582 struct protection_domain
*domain
;
2583 struct dma_ops_domain
*dma_dom
;
2584 struct scatterlist
*s
;
2585 unsigned long address
;
2588 domain
= get_domain(dev
);
2592 dma_dom
= to_dma_ops_domain(domain
);
2593 dma_mask
= *dev
->dma_mask
;
2595 npages
= sg_num_pages(dev
, sglist
, nelems
);
2597 address
= dma_ops_alloc_iova(dev
, dma_dom
, npages
, dma_mask
);
2598 if (address
== DMA_ERROR_CODE
)
2601 prot
= dir2prot(direction
);
2603 /* Map all sg entries */
2604 for_each_sg(sglist
, s
, nelems
, i
) {
2605 int j
, pages
= iommu_num_pages(sg_phys(s
), s
->length
, PAGE_SIZE
);
2607 for (j
= 0; j
< pages
; ++j
) {
2608 unsigned long bus_addr
, phys_addr
;
2611 bus_addr
= address
+ s
->dma_address
+ (j
<< PAGE_SHIFT
);
2612 phys_addr
= (sg_phys(s
) & PAGE_MASK
) + (j
<< PAGE_SHIFT
);
2613 ret
= iommu_map_page(domain
, bus_addr
, phys_addr
, PAGE_SIZE
, prot
, GFP_ATOMIC
);
2621 /* Everything is mapped - write the right values into s->dma_address */
2622 for_each_sg(sglist
, s
, nelems
, i
) {
2623 s
->dma_address
+= address
+ s
->offset
;
2624 s
->dma_length
= s
->length
;
2630 pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
2631 dev_name(dev
), npages
);
2633 for_each_sg(sglist
, s
, nelems
, i
) {
2634 int j
, pages
= iommu_num_pages(sg_phys(s
), s
->length
, PAGE_SIZE
);
2636 for (j
= 0; j
< pages
; ++j
) {
2637 unsigned long bus_addr
;
2639 bus_addr
= address
+ s
->dma_address
+ (j
<< PAGE_SHIFT
);
2640 iommu_unmap_page(domain
, bus_addr
, PAGE_SIZE
);
2648 free_iova_fast(&dma_dom
->iovad
, address
, npages
);
2655 * The exported map_sg function for dma_ops (handles scatter-gather
2658 static void unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
2659 int nelems
, enum dma_data_direction dir
,
2660 unsigned long attrs
)
2662 struct protection_domain
*domain
;
2663 struct dma_ops_domain
*dma_dom
;
2664 unsigned long startaddr
;
2667 domain
= get_domain(dev
);
2671 startaddr
= sg_dma_address(sglist
) & PAGE_MASK
;
2672 dma_dom
= to_dma_ops_domain(domain
);
2673 npages
= sg_num_pages(dev
, sglist
, nelems
);
2675 __unmap_single(dma_dom
, startaddr
, npages
<< PAGE_SHIFT
, dir
);
2679 * The exported alloc_coherent function for dma_ops.
2681 static void *alloc_coherent(struct device
*dev
, size_t size
,
2682 dma_addr_t
*dma_addr
, gfp_t flag
,
2683 unsigned long attrs
)
2685 u64 dma_mask
= dev
->coherent_dma_mask
;
2686 struct protection_domain
*domain
;
2687 struct dma_ops_domain
*dma_dom
;
2690 domain
= get_domain(dev
);
2691 if (PTR_ERR(domain
) == -EINVAL
) {
2692 page
= alloc_pages(flag
, get_order(size
));
2693 *dma_addr
= page_to_phys(page
);
2694 return page_address(page
);
2695 } else if (IS_ERR(domain
))
2698 dma_dom
= to_dma_ops_domain(domain
);
2699 size
= PAGE_ALIGN(size
);
2700 dma_mask
= dev
->coherent_dma_mask
;
2701 flag
&= ~(__GFP_DMA
| __GFP_HIGHMEM
| __GFP_DMA32
);
2704 page
= alloc_pages(flag
| __GFP_NOWARN
, get_order(size
));
2706 if (!gfpflags_allow_blocking(flag
))
2709 page
= dma_alloc_from_contiguous(dev
, size
>> PAGE_SHIFT
,
2710 get_order(size
), flag
);
2716 dma_mask
= *dev
->dma_mask
;
2718 *dma_addr
= __map_single(dev
, dma_dom
, page_to_phys(page
),
2719 size
, DMA_BIDIRECTIONAL
, dma_mask
);
2721 if (*dma_addr
== DMA_ERROR_CODE
)
2724 return page_address(page
);
2728 if (!dma_release_from_contiguous(dev
, page
, size
>> PAGE_SHIFT
))
2729 __free_pages(page
, get_order(size
));
2735 * The exported free_coherent function for dma_ops.
2737 static void free_coherent(struct device
*dev
, size_t size
,
2738 void *virt_addr
, dma_addr_t dma_addr
,
2739 unsigned long attrs
)
2741 struct protection_domain
*domain
;
2742 struct dma_ops_domain
*dma_dom
;
2745 page
= virt_to_page(virt_addr
);
2746 size
= PAGE_ALIGN(size
);
2748 domain
= get_domain(dev
);
2752 dma_dom
= to_dma_ops_domain(domain
);
2754 __unmap_single(dma_dom
, dma_addr
, size
, DMA_BIDIRECTIONAL
);
2757 if (!dma_release_from_contiguous(dev
, page
, size
>> PAGE_SHIFT
))
2758 __free_pages(page
, get_order(size
));
2762 * This function is called by the DMA layer to find out if we can handle a
2763 * particular device. It is part of the dma_ops.
2765 static int amd_iommu_dma_supported(struct device
*dev
, u64 mask
)
2767 return check_device(dev
);
2770 static const struct dma_map_ops amd_iommu_dma_ops
= {
2771 .alloc
= alloc_coherent
,
2772 .free
= free_coherent
,
2773 .map_page
= map_page
,
2774 .unmap_page
= unmap_page
,
2776 .unmap_sg
= unmap_sg
,
2777 .dma_supported
= amd_iommu_dma_supported
,
2780 static int init_reserved_iova_ranges(void)
2782 struct pci_dev
*pdev
= NULL
;
2785 init_iova_domain(&reserved_iova_ranges
, PAGE_SIZE
,
2786 IOVA_START_PFN
, DMA_32BIT_PFN
);
2788 lockdep_set_class(&reserved_iova_ranges
.iova_rbtree_lock
,
2789 &reserved_rbtree_key
);
2791 /* MSI memory range */
2792 val
= reserve_iova(&reserved_iova_ranges
,
2793 IOVA_PFN(MSI_RANGE_START
), IOVA_PFN(MSI_RANGE_END
));
2795 pr_err("Reserving MSI range failed\n");
2799 /* HT memory range */
2800 val
= reserve_iova(&reserved_iova_ranges
,
2801 IOVA_PFN(HT_RANGE_START
), IOVA_PFN(HT_RANGE_END
));
2803 pr_err("Reserving HT range failed\n");
2808 * Memory used for PCI resources
2809 * FIXME: Check whether we can reserve the PCI-hole completly
2811 for_each_pci_dev(pdev
) {
2814 for (i
= 0; i
< PCI_NUM_RESOURCES
; ++i
) {
2815 struct resource
*r
= &pdev
->resource
[i
];
2817 if (!(r
->flags
& IORESOURCE_MEM
))
2820 val
= reserve_iova(&reserved_iova_ranges
,
2824 pr_err("Reserve pci-resource range failed\n");
2833 int __init
amd_iommu_init_api(void)
2835 int ret
, cpu
, err
= 0;
2837 ret
= iova_cache_get();
2841 ret
= init_reserved_iova_ranges();
2845 for_each_possible_cpu(cpu
) {
2846 struct flush_queue
*queue
= per_cpu_ptr(&flush_queue
, cpu
);
2848 queue
->entries
= kzalloc(FLUSH_QUEUE_SIZE
*
2849 sizeof(*queue
->entries
),
2851 if (!queue
->entries
)
2854 spin_lock_init(&queue
->lock
);
2857 err
= bus_set_iommu(&pci_bus_type
, &amd_iommu_ops
);
2860 #ifdef CONFIG_ARM_AMBA
2861 err
= bus_set_iommu(&amba_bustype
, &amd_iommu_ops
);
2865 err
= bus_set_iommu(&platform_bus_type
, &amd_iommu_ops
);
2871 for_each_possible_cpu(cpu
) {
2872 struct flush_queue
*queue
= per_cpu_ptr(&flush_queue
, cpu
);
2874 kfree(queue
->entries
);
2880 int __init
amd_iommu_init_dma_ops(void)
2882 setup_timer(&queue_timer
, queue_flush_timeout
, 0);
2883 atomic_set(&queue_timer_on
, 0);
2885 swiotlb
= iommu_pass_through
? 1 : 0;
2889 * In case we don't initialize SWIOTLB (actually the common case
2890 * when AMD IOMMU is enabled), make sure there are global
2891 * dma_ops set as a fall-back for devices not handled by this
2892 * driver (for example non-PCI devices).
2895 dma_ops
= &nommu_dma_ops
;
2897 if (amd_iommu_unmap_flush
)
2898 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2900 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2906 /*****************************************************************************
2908 * The following functions belong to the exported interface of AMD IOMMU
2910 * This interface allows access to lower level functions of the IOMMU
2911 * like protection domain handling and assignement of devices to domains
2912 * which is not possible with the dma_ops interface.
2914 *****************************************************************************/
2916 static void cleanup_domain(struct protection_domain
*domain
)
2918 struct iommu_dev_data
*entry
;
2919 unsigned long flags
;
2921 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
2923 while (!list_empty(&domain
->dev_list
)) {
2924 entry
= list_first_entry(&domain
->dev_list
,
2925 struct iommu_dev_data
, list
);
2926 __detach_device(entry
);
2929 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
2932 static void protection_domain_free(struct protection_domain
*domain
)
2937 del_domain_from_list(domain
);
2940 domain_id_free(domain
->id
);
2945 static int protection_domain_init(struct protection_domain
*domain
)
2947 spin_lock_init(&domain
->lock
);
2948 mutex_init(&domain
->api_lock
);
2949 domain
->id
= domain_id_alloc();
2952 INIT_LIST_HEAD(&domain
->dev_list
);
2957 static struct protection_domain
*protection_domain_alloc(void)
2959 struct protection_domain
*domain
;
2961 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
2965 if (protection_domain_init(domain
))
2968 add_domain_to_list(domain
);
2978 static struct iommu_domain
*amd_iommu_domain_alloc(unsigned type
)
2980 struct protection_domain
*pdomain
;
2981 struct dma_ops_domain
*dma_domain
;
2984 case IOMMU_DOMAIN_UNMANAGED
:
2985 pdomain
= protection_domain_alloc();
2989 pdomain
->mode
= PAGE_MODE_3_LEVEL
;
2990 pdomain
->pt_root
= (void *)get_zeroed_page(GFP_KERNEL
);
2991 if (!pdomain
->pt_root
) {
2992 protection_domain_free(pdomain
);
2996 pdomain
->domain
.geometry
.aperture_start
= 0;
2997 pdomain
->domain
.geometry
.aperture_end
= ~0ULL;
2998 pdomain
->domain
.geometry
.force_aperture
= true;
3001 case IOMMU_DOMAIN_DMA
:
3002 dma_domain
= dma_ops_domain_alloc();
3004 pr_err("AMD-Vi: Failed to allocate\n");
3007 pdomain
= &dma_domain
->domain
;
3009 case IOMMU_DOMAIN_IDENTITY
:
3010 pdomain
= protection_domain_alloc();
3014 pdomain
->mode
= PAGE_MODE_NONE
;
3020 return &pdomain
->domain
;
3023 static void amd_iommu_domain_free(struct iommu_domain
*dom
)
3025 struct protection_domain
*domain
;
3026 struct dma_ops_domain
*dma_dom
;
3028 domain
= to_pdomain(dom
);
3030 if (domain
->dev_cnt
> 0)
3031 cleanup_domain(domain
);
3033 BUG_ON(domain
->dev_cnt
!= 0);
3038 switch (dom
->type
) {
3039 case IOMMU_DOMAIN_DMA
:
3041 * First make sure the domain is no longer referenced from the
3046 /* Now release the domain */
3047 dma_dom
= to_dma_ops_domain(domain
);
3048 dma_ops_domain_free(dma_dom
);
3051 if (domain
->mode
!= PAGE_MODE_NONE
)
3052 free_pagetable(domain
);
3054 if (domain
->flags
& PD_IOMMUV2_MASK
)
3055 free_gcr3_table(domain
);
3057 protection_domain_free(domain
);
3062 static void amd_iommu_detach_device(struct iommu_domain
*dom
,
3065 struct iommu_dev_data
*dev_data
= dev
->archdata
.iommu
;
3066 struct amd_iommu
*iommu
;
3069 if (!check_device(dev
))
3072 devid
= get_device_id(dev
);
3076 if (dev_data
->domain
!= NULL
)
3079 iommu
= amd_iommu_rlookup_table
[devid
];
3083 #ifdef CONFIG_IRQ_REMAP
3084 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
) &&
3085 (dom
->type
== IOMMU_DOMAIN_UNMANAGED
))
3086 dev_data
->use_vapic
= 0;
3089 iommu_completion_wait(iommu
);
3092 static int amd_iommu_attach_device(struct iommu_domain
*dom
,
3095 struct protection_domain
*domain
= to_pdomain(dom
);
3096 struct iommu_dev_data
*dev_data
;
3097 struct amd_iommu
*iommu
;
3100 if (!check_device(dev
))
3103 dev_data
= dev
->archdata
.iommu
;
3105 iommu
= amd_iommu_rlookup_table
[dev_data
->devid
];
3109 if (dev_data
->domain
)
3112 ret
= attach_device(dev
, domain
);
3114 #ifdef CONFIG_IRQ_REMAP
3115 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
)) {
3116 if (dom
->type
== IOMMU_DOMAIN_UNMANAGED
)
3117 dev_data
->use_vapic
= 1;
3119 dev_data
->use_vapic
= 0;
3123 iommu_completion_wait(iommu
);
3128 static int amd_iommu_map(struct iommu_domain
*dom
, unsigned long iova
,
3129 phys_addr_t paddr
, size_t page_size
, int iommu_prot
)
3131 struct protection_domain
*domain
= to_pdomain(dom
);
3135 if (domain
->mode
== PAGE_MODE_NONE
)
3138 if (iommu_prot
& IOMMU_READ
)
3139 prot
|= IOMMU_PROT_IR
;
3140 if (iommu_prot
& IOMMU_WRITE
)
3141 prot
|= IOMMU_PROT_IW
;
3143 mutex_lock(&domain
->api_lock
);
3144 ret
= iommu_map_page(domain
, iova
, paddr
, page_size
, prot
, GFP_KERNEL
);
3145 mutex_unlock(&domain
->api_lock
);
3150 static size_t amd_iommu_unmap(struct iommu_domain
*dom
, unsigned long iova
,
3153 struct protection_domain
*domain
= to_pdomain(dom
);
3156 if (domain
->mode
== PAGE_MODE_NONE
)
3159 mutex_lock(&domain
->api_lock
);
3160 unmap_size
= iommu_unmap_page(domain
, iova
, page_size
);
3161 mutex_unlock(&domain
->api_lock
);
3163 domain_flush_tlb_pde(domain
);
3168 static phys_addr_t
amd_iommu_iova_to_phys(struct iommu_domain
*dom
,
3171 struct protection_domain
*domain
= to_pdomain(dom
);
3172 unsigned long offset_mask
, pte_pgsize
;
3175 if (domain
->mode
== PAGE_MODE_NONE
)
3178 pte
= fetch_pte(domain
, iova
, &pte_pgsize
);
3180 if (!pte
|| !IOMMU_PTE_PRESENT(*pte
))
3183 offset_mask
= pte_pgsize
- 1;
3184 __pte
= *pte
& PM_ADDR_MASK
;
3186 return (__pte
& ~offset_mask
) | (iova
& offset_mask
);
3189 static bool amd_iommu_capable(enum iommu_cap cap
)
3192 case IOMMU_CAP_CACHE_COHERENCY
:
3194 case IOMMU_CAP_INTR_REMAP
:
3195 return (irq_remapping_enabled
== 1);
3196 case IOMMU_CAP_NOEXEC
:
3203 static void amd_iommu_get_resv_regions(struct device
*dev
,
3204 struct list_head
*head
)
3206 struct iommu_resv_region
*region
;
3207 struct unity_map_entry
*entry
;
3210 devid
= get_device_id(dev
);
3214 list_for_each_entry(entry
, &amd_iommu_unity_map
, list
) {
3218 if (devid
< entry
->devid_start
|| devid
> entry
->devid_end
)
3221 length
= entry
->address_end
- entry
->address_start
;
3222 if (entry
->prot
& IOMMU_PROT_IR
)
3224 if (entry
->prot
& IOMMU_PROT_IW
)
3225 prot
|= IOMMU_WRITE
;
3227 region
= iommu_alloc_resv_region(entry
->address_start
,
3231 pr_err("Out of memory allocating dm-regions for %s\n",
3235 list_add_tail(®ion
->list
, head
);
3238 region
= iommu_alloc_resv_region(MSI_RANGE_START
,
3239 MSI_RANGE_END
- MSI_RANGE_START
+ 1,
3243 list_add_tail(®ion
->list
, head
);
3245 region
= iommu_alloc_resv_region(HT_RANGE_START
,
3246 HT_RANGE_END
- HT_RANGE_START
+ 1,
3247 0, IOMMU_RESV_RESERVED
);
3250 list_add_tail(®ion
->list
, head
);
3253 static void amd_iommu_put_resv_regions(struct device
*dev
,
3254 struct list_head
*head
)
3256 struct iommu_resv_region
*entry
, *next
;
3258 list_for_each_entry_safe(entry
, next
, head
, list
)
3262 static void amd_iommu_apply_resv_region(struct device
*dev
,
3263 struct iommu_domain
*domain
,
3264 struct iommu_resv_region
*region
)
3266 struct dma_ops_domain
*dma_dom
= to_dma_ops_domain(to_pdomain(domain
));
3267 unsigned long start
, end
;
3269 start
= IOVA_PFN(region
->start
);
3270 end
= IOVA_PFN(region
->start
+ region
->length
);
3272 WARN_ON_ONCE(reserve_iova(&dma_dom
->iovad
, start
, end
) == NULL
);
3275 const struct iommu_ops amd_iommu_ops
= {
3276 .capable
= amd_iommu_capable
,
3277 .domain_alloc
= amd_iommu_domain_alloc
,
3278 .domain_free
= amd_iommu_domain_free
,
3279 .attach_dev
= amd_iommu_attach_device
,
3280 .detach_dev
= amd_iommu_detach_device
,
3281 .map
= amd_iommu_map
,
3282 .unmap
= amd_iommu_unmap
,
3283 .map_sg
= default_iommu_map_sg
,
3284 .iova_to_phys
= amd_iommu_iova_to_phys
,
3285 .add_device
= amd_iommu_add_device
,
3286 .remove_device
= amd_iommu_remove_device
,
3287 .device_group
= amd_iommu_device_group
,
3288 .get_resv_regions
= amd_iommu_get_resv_regions
,
3289 .put_resv_regions
= amd_iommu_put_resv_regions
,
3290 .apply_resv_region
= amd_iommu_apply_resv_region
,
3291 .pgsize_bitmap
= AMD_IOMMU_PGSIZES
,
3294 /*****************************************************************************
3296 * The next functions do a basic initialization of IOMMU for pass through
3299 * In passthrough mode the IOMMU is initialized and enabled but not used for
3300 * DMA-API translation.
3302 *****************************************************************************/
3304 /* IOMMUv2 specific functions */
3305 int amd_iommu_register_ppr_notifier(struct notifier_block
*nb
)
3307 return atomic_notifier_chain_register(&ppr_notifier
, nb
);
3309 EXPORT_SYMBOL(amd_iommu_register_ppr_notifier
);
3311 int amd_iommu_unregister_ppr_notifier(struct notifier_block
*nb
)
3313 return atomic_notifier_chain_unregister(&ppr_notifier
, nb
);
3315 EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier
);
3317 void amd_iommu_domain_direct_map(struct iommu_domain
*dom
)
3319 struct protection_domain
*domain
= to_pdomain(dom
);
3320 unsigned long flags
;
3322 spin_lock_irqsave(&domain
->lock
, flags
);
3324 /* Update data structure */
3325 domain
->mode
= PAGE_MODE_NONE
;
3326 domain
->updated
= true;
3328 /* Make changes visible to IOMMUs */
3329 update_domain(domain
);
3331 /* Page-table is not visible to IOMMU anymore, so free it */
3332 free_pagetable(domain
);
3334 spin_unlock_irqrestore(&domain
->lock
, flags
);
3336 EXPORT_SYMBOL(amd_iommu_domain_direct_map
);
3338 int amd_iommu_domain_enable_v2(struct iommu_domain
*dom
, int pasids
)
3340 struct protection_domain
*domain
= to_pdomain(dom
);
3341 unsigned long flags
;
3344 if (pasids
<= 0 || pasids
> (PASID_MASK
+ 1))
3347 /* Number of GCR3 table levels required */
3348 for (levels
= 0; (pasids
- 1) & ~0x1ff; pasids
>>= 9)
3351 if (levels
> amd_iommu_max_glx_val
)
3354 spin_lock_irqsave(&domain
->lock
, flags
);
3357 * Save us all sanity checks whether devices already in the
3358 * domain support IOMMUv2. Just force that the domain has no
3359 * devices attached when it is switched into IOMMUv2 mode.
3362 if (domain
->dev_cnt
> 0 || domain
->flags
& PD_IOMMUV2_MASK
)
3366 domain
->gcr3_tbl
= (void *)get_zeroed_page(GFP_ATOMIC
);
3367 if (domain
->gcr3_tbl
== NULL
)
3370 domain
->glx
= levels
;
3371 domain
->flags
|= PD_IOMMUV2_MASK
;
3372 domain
->updated
= true;
3374 update_domain(domain
);
3379 spin_unlock_irqrestore(&domain
->lock
, flags
);
3383 EXPORT_SYMBOL(amd_iommu_domain_enable_v2
);
3385 static int __flush_pasid(struct protection_domain
*domain
, int pasid
,
3386 u64 address
, bool size
)
3388 struct iommu_dev_data
*dev_data
;
3389 struct iommu_cmd cmd
;
3392 if (!(domain
->flags
& PD_IOMMUV2_MASK
))
3395 build_inv_iommu_pasid(&cmd
, domain
->id
, pasid
, address
, size
);
3398 * IOMMU TLB needs to be flushed before Device TLB to
3399 * prevent device TLB refill from IOMMU TLB
3401 for (i
= 0; i
< amd_iommu_get_num_iommus(); ++i
) {
3402 if (domain
->dev_iommu
[i
] == 0)
3405 ret
= iommu_queue_command(amd_iommus
[i
], &cmd
);
3410 /* Wait until IOMMU TLB flushes are complete */
3411 domain_flush_complete(domain
);
3413 /* Now flush device TLBs */
3414 list_for_each_entry(dev_data
, &domain
->dev_list
, list
) {
3415 struct amd_iommu
*iommu
;
3419 There might be non-IOMMUv2 capable devices in an IOMMUv2
3422 if (!dev_data
->ats
.enabled
)
3425 qdep
= dev_data
->ats
.qdep
;
3426 iommu
= amd_iommu_rlookup_table
[dev_data
->devid
];
3428 build_inv_iotlb_pasid(&cmd
, dev_data
->devid
, pasid
,
3429 qdep
, address
, size
);
3431 ret
= iommu_queue_command(iommu
, &cmd
);
3436 /* Wait until all device TLBs are flushed */
3437 domain_flush_complete(domain
);
3446 static int __amd_iommu_flush_page(struct protection_domain
*domain
, int pasid
,
3449 return __flush_pasid(domain
, pasid
, address
, false);
3452 int amd_iommu_flush_page(struct iommu_domain
*dom
, int pasid
,
3455 struct protection_domain
*domain
= to_pdomain(dom
);
3456 unsigned long flags
;
3459 spin_lock_irqsave(&domain
->lock
, flags
);
3460 ret
= __amd_iommu_flush_page(domain
, pasid
, address
);
3461 spin_unlock_irqrestore(&domain
->lock
, flags
);
3465 EXPORT_SYMBOL(amd_iommu_flush_page
);
3467 static int __amd_iommu_flush_tlb(struct protection_domain
*domain
, int pasid
)
3469 return __flush_pasid(domain
, pasid
, CMD_INV_IOMMU_ALL_PAGES_ADDRESS
,
3473 int amd_iommu_flush_tlb(struct iommu_domain
*dom
, int pasid
)
3475 struct protection_domain
*domain
= to_pdomain(dom
);
3476 unsigned long flags
;
3479 spin_lock_irqsave(&domain
->lock
, flags
);
3480 ret
= __amd_iommu_flush_tlb(domain
, pasid
);
3481 spin_unlock_irqrestore(&domain
->lock
, flags
);
3485 EXPORT_SYMBOL(amd_iommu_flush_tlb
);
3487 static u64
*__get_gcr3_pte(u64
*root
, int level
, int pasid
, bool alloc
)
3494 index
= (pasid
>> (9 * level
)) & 0x1ff;
3500 if (!(*pte
& GCR3_VALID
)) {
3504 root
= (void *)get_zeroed_page(GFP_ATOMIC
);
3508 *pte
= __pa(root
) | GCR3_VALID
;
3511 root
= __va(*pte
& PAGE_MASK
);
3519 static int __set_gcr3(struct protection_domain
*domain
, int pasid
,
3524 if (domain
->mode
!= PAGE_MODE_NONE
)
3527 pte
= __get_gcr3_pte(domain
->gcr3_tbl
, domain
->glx
, pasid
, true);
3531 *pte
= (cr3
& PAGE_MASK
) | GCR3_VALID
;
3533 return __amd_iommu_flush_tlb(domain
, pasid
);
3536 static int __clear_gcr3(struct protection_domain
*domain
, int pasid
)
3540 if (domain
->mode
!= PAGE_MODE_NONE
)
3543 pte
= __get_gcr3_pte(domain
->gcr3_tbl
, domain
->glx
, pasid
, false);
3549 return __amd_iommu_flush_tlb(domain
, pasid
);
3552 int amd_iommu_domain_set_gcr3(struct iommu_domain
*dom
, int pasid
,
3555 struct protection_domain
*domain
= to_pdomain(dom
);
3556 unsigned long flags
;
3559 spin_lock_irqsave(&domain
->lock
, flags
);
3560 ret
= __set_gcr3(domain
, pasid
, cr3
);
3561 spin_unlock_irqrestore(&domain
->lock
, flags
);
3565 EXPORT_SYMBOL(amd_iommu_domain_set_gcr3
);
3567 int amd_iommu_domain_clear_gcr3(struct iommu_domain
*dom
, int pasid
)
3569 struct protection_domain
*domain
= to_pdomain(dom
);
3570 unsigned long flags
;
3573 spin_lock_irqsave(&domain
->lock
, flags
);
3574 ret
= __clear_gcr3(domain
, pasid
);
3575 spin_unlock_irqrestore(&domain
->lock
, flags
);
3579 EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3
);
3581 int amd_iommu_complete_ppr(struct pci_dev
*pdev
, int pasid
,
3582 int status
, int tag
)
3584 struct iommu_dev_data
*dev_data
;
3585 struct amd_iommu
*iommu
;
3586 struct iommu_cmd cmd
;
3588 dev_data
= get_dev_data(&pdev
->dev
);
3589 iommu
= amd_iommu_rlookup_table
[dev_data
->devid
];
3591 build_complete_ppr(&cmd
, dev_data
->devid
, pasid
, status
,
3592 tag
, dev_data
->pri_tlp
);
3594 return iommu_queue_command(iommu
, &cmd
);
3596 EXPORT_SYMBOL(amd_iommu_complete_ppr
);
3598 struct iommu_domain
*amd_iommu_get_v2_domain(struct pci_dev
*pdev
)
3600 struct protection_domain
*pdomain
;
3602 pdomain
= get_domain(&pdev
->dev
);
3603 if (IS_ERR(pdomain
))
3606 /* Only return IOMMUv2 domains */
3607 if (!(pdomain
->flags
& PD_IOMMUV2_MASK
))
3610 return &pdomain
->domain
;
3612 EXPORT_SYMBOL(amd_iommu_get_v2_domain
);
3614 void amd_iommu_enable_device_erratum(struct pci_dev
*pdev
, u32 erratum
)
3616 struct iommu_dev_data
*dev_data
;
3618 if (!amd_iommu_v2_supported())
3621 dev_data
= get_dev_data(&pdev
->dev
);
3622 dev_data
->errata
|= (1 << erratum
);
3624 EXPORT_SYMBOL(amd_iommu_enable_device_erratum
);
3626 int amd_iommu_device_info(struct pci_dev
*pdev
,
3627 struct amd_iommu_device_info
*info
)
3632 if (pdev
== NULL
|| info
== NULL
)
3635 if (!amd_iommu_v2_supported())
3638 memset(info
, 0, sizeof(*info
));
3640 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_ATS
);
3642 info
->flags
|= AMD_IOMMU_DEVICE_FLAG_ATS_SUP
;
3644 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PRI
);
3646 info
->flags
|= AMD_IOMMU_DEVICE_FLAG_PRI_SUP
;
3648 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_PASID
);
3652 max_pasids
= 1 << (9 * (amd_iommu_max_glx_val
+ 1));
3653 max_pasids
= min(max_pasids
, (1 << 20));
3655 info
->flags
|= AMD_IOMMU_DEVICE_FLAG_PASID_SUP
;
3656 info
->max_pasids
= min(pci_max_pasids(pdev
), max_pasids
);
3658 features
= pci_pasid_features(pdev
);
3659 if (features
& PCI_PASID_CAP_EXEC
)
3660 info
->flags
|= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP
;
3661 if (features
& PCI_PASID_CAP_PRIV
)
3662 info
->flags
|= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP
;
3667 EXPORT_SYMBOL(amd_iommu_device_info
);
3669 #ifdef CONFIG_IRQ_REMAP
3671 /*****************************************************************************
3673 * Interrupt Remapping Implementation
3675 *****************************************************************************/
3677 static struct irq_chip amd_ir_chip
;
3679 #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3680 #define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3681 #define DTE_IRQ_TABLE_LEN (8ULL << 1)
3682 #define DTE_IRQ_REMAP_ENABLE 1ULL
3684 static void set_dte_irq_entry(u16 devid
, struct irq_remap_table
*table
)
3688 dte
= amd_iommu_dev_table
[devid
].data
[2];
3689 dte
&= ~DTE_IRQ_PHYS_ADDR_MASK
;
3690 dte
|= virt_to_phys(table
->table
);
3691 dte
|= DTE_IRQ_REMAP_INTCTL
;
3692 dte
|= DTE_IRQ_TABLE_LEN
;
3693 dte
|= DTE_IRQ_REMAP_ENABLE
;
3695 amd_iommu_dev_table
[devid
].data
[2] = dte
;
3698 static struct irq_remap_table
*get_irq_table(u16 devid
, bool ioapic
)
3700 struct irq_remap_table
*table
= NULL
;
3701 struct amd_iommu
*iommu
;
3702 unsigned long flags
;
3705 write_lock_irqsave(&amd_iommu_devtable_lock
, flags
);
3707 iommu
= amd_iommu_rlookup_table
[devid
];
3711 table
= irq_lookup_table
[devid
];
3715 alias
= amd_iommu_alias_table
[devid
];
3716 table
= irq_lookup_table
[alias
];
3718 irq_lookup_table
[devid
] = table
;
3719 set_dte_irq_entry(devid
, table
);
3720 iommu_flush_dte(iommu
, devid
);
3724 /* Nothing there yet, allocate new irq remapping table */
3725 table
= kzalloc(sizeof(*table
), GFP_ATOMIC
);
3729 /* Initialize table spin-lock */
3730 spin_lock_init(&table
->lock
);
3733 /* Keep the first 32 indexes free for IOAPIC interrupts */
3734 table
->min_index
= 32;
3736 table
->table
= kmem_cache_alloc(amd_iommu_irq_cache
, GFP_ATOMIC
);
3737 if (!table
->table
) {
3743 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir
))
3744 memset(table
->table
, 0,
3745 MAX_IRQS_PER_TABLE
* sizeof(u32
));
3747 memset(table
->table
, 0,
3748 (MAX_IRQS_PER_TABLE
* (sizeof(u64
) * 2)));
3753 for (i
= 0; i
< 32; ++i
)
3754 iommu
->irte_ops
->set_allocated(table
, i
);
3757 irq_lookup_table
[devid
] = table
;
3758 set_dte_irq_entry(devid
, table
);
3759 iommu_flush_dte(iommu
, devid
);
3760 if (devid
!= alias
) {
3761 irq_lookup_table
[alias
] = table
;
3762 set_dte_irq_entry(alias
, table
);
3763 iommu_flush_dte(iommu
, alias
);
3767 iommu_completion_wait(iommu
);
3770 write_unlock_irqrestore(&amd_iommu_devtable_lock
, flags
);
3775 static int alloc_irq_index(u16 devid
, int count
)
3777 struct irq_remap_table
*table
;
3778 unsigned long flags
;
3780 struct amd_iommu
*iommu
= amd_iommu_rlookup_table
[devid
];
3785 table
= get_irq_table(devid
, false);
3789 spin_lock_irqsave(&table
->lock
, flags
);
3791 /* Scan table for free entries */
3792 for (c
= 0, index
= table
->min_index
;
3793 index
< MAX_IRQS_PER_TABLE
;
3795 if (!iommu
->irte_ops
->is_allocated(table
, index
))
3802 iommu
->irte_ops
->set_allocated(table
, index
- c
+ 1);
3812 spin_unlock_irqrestore(&table
->lock
, flags
);
3817 static int modify_irte_ga(u16 devid
, int index
, struct irte_ga
*irte
,
3818 struct amd_ir_data
*data
)
3820 struct irq_remap_table
*table
;
3821 struct amd_iommu
*iommu
;
3822 unsigned long flags
;
3823 struct irte_ga
*entry
;
3825 iommu
= amd_iommu_rlookup_table
[devid
];
3829 table
= get_irq_table(devid
, false);
3833 spin_lock_irqsave(&table
->lock
, flags
);
3835 entry
= (struct irte_ga
*)table
->table
;
3836 entry
= &entry
[index
];
3837 entry
->lo
.fields_remap
.valid
= 0;
3838 entry
->hi
.val
= irte
->hi
.val
;
3839 entry
->lo
.val
= irte
->lo
.val
;
3840 entry
->lo
.fields_remap
.valid
= 1;
3844 spin_unlock_irqrestore(&table
->lock
, flags
);
3846 iommu_flush_irt(iommu
, devid
);
3847 iommu_completion_wait(iommu
);
3852 static int modify_irte(u16 devid
, int index
, union irte
*irte
)
3854 struct irq_remap_table
*table
;
3855 struct amd_iommu
*iommu
;
3856 unsigned long flags
;
3858 iommu
= amd_iommu_rlookup_table
[devid
];
3862 table
= get_irq_table(devid
, false);
3866 spin_lock_irqsave(&table
->lock
, flags
);
3867 table
->table
[index
] = irte
->val
;
3868 spin_unlock_irqrestore(&table
->lock
, flags
);
3870 iommu_flush_irt(iommu
, devid
);
3871 iommu_completion_wait(iommu
);
3876 static void free_irte(u16 devid
, int index
)
3878 struct irq_remap_table
*table
;
3879 struct amd_iommu
*iommu
;
3880 unsigned long flags
;
3882 iommu
= amd_iommu_rlookup_table
[devid
];
3886 table
= get_irq_table(devid
, false);
3890 spin_lock_irqsave(&table
->lock
, flags
);
3891 iommu
->irte_ops
->clear_allocated(table
, index
);
3892 spin_unlock_irqrestore(&table
->lock
, flags
);
3894 iommu_flush_irt(iommu
, devid
);
3895 iommu_completion_wait(iommu
);
3898 static void irte_prepare(void *entry
,
3899 u32 delivery_mode
, u32 dest_mode
,
3900 u8 vector
, u32 dest_apicid
, int devid
)
3902 union irte
*irte
= (union irte
*) entry
;
3905 irte
->fields
.vector
= vector
;
3906 irte
->fields
.int_type
= delivery_mode
;
3907 irte
->fields
.destination
= dest_apicid
;
3908 irte
->fields
.dm
= dest_mode
;
3909 irte
->fields
.valid
= 1;
3912 static void irte_ga_prepare(void *entry
,
3913 u32 delivery_mode
, u32 dest_mode
,
3914 u8 vector
, u32 dest_apicid
, int devid
)
3916 struct irte_ga
*irte
= (struct irte_ga
*) entry
;
3917 struct iommu_dev_data
*dev_data
= search_dev_data(devid
);
3921 irte
->lo
.fields_remap
.guest_mode
= dev_data
? dev_data
->use_vapic
: 0;
3922 irte
->lo
.fields_remap
.int_type
= delivery_mode
;
3923 irte
->lo
.fields_remap
.dm
= dest_mode
;
3924 irte
->hi
.fields
.vector
= vector
;
3925 irte
->lo
.fields_remap
.destination
= dest_apicid
;
3926 irte
->lo
.fields_remap
.valid
= 1;
3929 static void irte_activate(void *entry
, u16 devid
, u16 index
)
3931 union irte
*irte
= (union irte
*) entry
;
3933 irte
->fields
.valid
= 1;
3934 modify_irte(devid
, index
, irte
);
3937 static void irte_ga_activate(void *entry
, u16 devid
, u16 index
)
3939 struct irte_ga
*irte
= (struct irte_ga
*) entry
;
3941 irte
->lo
.fields_remap
.valid
= 1;
3942 modify_irte_ga(devid
, index
, irte
, NULL
);
3945 static void irte_deactivate(void *entry
, u16 devid
, u16 index
)
3947 union irte
*irte
= (union irte
*) entry
;
3949 irte
->fields
.valid
= 0;
3950 modify_irte(devid
, index
, irte
);
3953 static void irte_ga_deactivate(void *entry
, u16 devid
, u16 index
)
3955 struct irte_ga
*irte
= (struct irte_ga
*) entry
;
3957 irte
->lo
.fields_remap
.valid
= 0;
3958 modify_irte_ga(devid
, index
, irte
, NULL
);
3961 static void irte_set_affinity(void *entry
, u16 devid
, u16 index
,
3962 u8 vector
, u32 dest_apicid
)
3964 union irte
*irte
= (union irte
*) entry
;
3966 irte
->fields
.vector
= vector
;
3967 irte
->fields
.destination
= dest_apicid
;
3968 modify_irte(devid
, index
, irte
);
3971 static void irte_ga_set_affinity(void *entry
, u16 devid
, u16 index
,
3972 u8 vector
, u32 dest_apicid
)
3974 struct irte_ga
*irte
= (struct irte_ga
*) entry
;
3975 struct iommu_dev_data
*dev_data
= search_dev_data(devid
);
3977 if (!dev_data
|| !dev_data
->use_vapic
) {
3978 irte
->hi
.fields
.vector
= vector
;
3979 irte
->lo
.fields_remap
.destination
= dest_apicid
;
3980 irte
->lo
.fields_remap
.guest_mode
= 0;
3981 modify_irte_ga(devid
, index
, irte
, NULL
);
3985 #define IRTE_ALLOCATED (~1U)
3986 static void irte_set_allocated(struct irq_remap_table
*table
, int index
)
3988 table
->table
[index
] = IRTE_ALLOCATED
;
3991 static void irte_ga_set_allocated(struct irq_remap_table
*table
, int index
)
3993 struct irte_ga
*ptr
= (struct irte_ga
*)table
->table
;
3994 struct irte_ga
*irte
= &ptr
[index
];
3996 memset(&irte
->lo
.val
, 0, sizeof(u64
));
3997 memset(&irte
->hi
.val
, 0, sizeof(u64
));
3998 irte
->hi
.fields
.vector
= 0xff;
4001 static bool irte_is_allocated(struct irq_remap_table
*table
, int index
)
4003 union irte
*ptr
= (union irte
*)table
->table
;
4004 union irte
*irte
= &ptr
[index
];
4006 return irte
->val
!= 0;
4009 static bool irte_ga_is_allocated(struct irq_remap_table
*table
, int index
)
4011 struct irte_ga
*ptr
= (struct irte_ga
*)table
->table
;
4012 struct irte_ga
*irte
= &ptr
[index
];
4014 return irte
->hi
.fields
.vector
!= 0;
4017 static void irte_clear_allocated(struct irq_remap_table
*table
, int index
)
4019 table
->table
[index
] = 0;
4022 static void irte_ga_clear_allocated(struct irq_remap_table
*table
, int index
)
4024 struct irte_ga
*ptr
= (struct irte_ga
*)table
->table
;
4025 struct irte_ga
*irte
= &ptr
[index
];
4027 memset(&irte
->lo
.val
, 0, sizeof(u64
));
4028 memset(&irte
->hi
.val
, 0, sizeof(u64
));
4031 static int get_devid(struct irq_alloc_info
*info
)
4035 switch (info
->type
) {
4036 case X86_IRQ_ALLOC_TYPE_IOAPIC
:
4037 devid
= get_ioapic_devid(info
->ioapic_id
);
4039 case X86_IRQ_ALLOC_TYPE_HPET
:
4040 devid
= get_hpet_devid(info
->hpet_id
);
4042 case X86_IRQ_ALLOC_TYPE_MSI
:
4043 case X86_IRQ_ALLOC_TYPE_MSIX
:
4044 devid
= get_device_id(&info
->msi_dev
->dev
);
4054 static struct irq_domain
*get_ir_irq_domain(struct irq_alloc_info
*info
)
4056 struct amd_iommu
*iommu
;
4062 devid
= get_devid(info
);
4064 iommu
= amd_iommu_rlookup_table
[devid
];
4066 return iommu
->ir_domain
;
4072 static struct irq_domain
*get_irq_domain(struct irq_alloc_info
*info
)
4074 struct amd_iommu
*iommu
;
4080 switch (info
->type
) {
4081 case X86_IRQ_ALLOC_TYPE_MSI
:
4082 case X86_IRQ_ALLOC_TYPE_MSIX
:
4083 devid
= get_device_id(&info
->msi_dev
->dev
);
4087 iommu
= amd_iommu_rlookup_table
[devid
];
4089 return iommu
->msi_domain
;
4098 struct irq_remap_ops amd_iommu_irq_ops
= {
4099 .prepare
= amd_iommu_prepare
,
4100 .enable
= amd_iommu_enable
,
4101 .disable
= amd_iommu_disable
,
4102 .reenable
= amd_iommu_reenable
,
4103 .enable_faulting
= amd_iommu_enable_faulting
,
4104 .get_ir_irq_domain
= get_ir_irq_domain
,
4105 .get_irq_domain
= get_irq_domain
,
4108 static void irq_remapping_prepare_irte(struct amd_ir_data
*data
,
4109 struct irq_cfg
*irq_cfg
,
4110 struct irq_alloc_info
*info
,
4111 int devid
, int index
, int sub_handle
)
4113 struct irq_2_irte
*irte_info
= &data
->irq_2_irte
;
4114 struct msi_msg
*msg
= &data
->msi_entry
;
4115 struct IO_APIC_route_entry
*entry
;
4116 struct amd_iommu
*iommu
= amd_iommu_rlookup_table
[devid
];
4121 data
->irq_2_irte
.devid
= devid
;
4122 data
->irq_2_irte
.index
= index
+ sub_handle
;
4123 iommu
->irte_ops
->prepare(data
->entry
, apic
->irq_delivery_mode
,
4124 apic
->irq_dest_mode
, irq_cfg
->vector
,
4125 irq_cfg
->dest_apicid
, devid
);
4127 switch (info
->type
) {
4128 case X86_IRQ_ALLOC_TYPE_IOAPIC
:
4129 /* Setup IOAPIC entry */
4130 entry
= info
->ioapic_entry
;
4131 info
->ioapic_entry
= NULL
;
4132 memset(entry
, 0, sizeof(*entry
));
4133 entry
->vector
= index
;
4135 entry
->trigger
= info
->ioapic_trigger
;
4136 entry
->polarity
= info
->ioapic_polarity
;
4137 /* Mask level triggered irqs. */
4138 if (info
->ioapic_trigger
)
4142 case X86_IRQ_ALLOC_TYPE_HPET
:
4143 case X86_IRQ_ALLOC_TYPE_MSI
:
4144 case X86_IRQ_ALLOC_TYPE_MSIX
:
4145 msg
->address_hi
= MSI_ADDR_BASE_HI
;
4146 msg
->address_lo
= MSI_ADDR_BASE_LO
;
4147 msg
->data
= irte_info
->index
;
4156 struct amd_irte_ops irte_32_ops
= {
4157 .prepare
= irte_prepare
,
4158 .activate
= irte_activate
,
4159 .deactivate
= irte_deactivate
,
4160 .set_affinity
= irte_set_affinity
,
4161 .set_allocated
= irte_set_allocated
,
4162 .is_allocated
= irte_is_allocated
,
4163 .clear_allocated
= irte_clear_allocated
,
4166 struct amd_irte_ops irte_128_ops
= {
4167 .prepare
= irte_ga_prepare
,
4168 .activate
= irte_ga_activate
,
4169 .deactivate
= irte_ga_deactivate
,
4170 .set_affinity
= irte_ga_set_affinity
,
4171 .set_allocated
= irte_ga_set_allocated
,
4172 .is_allocated
= irte_ga_is_allocated
,
4173 .clear_allocated
= irte_ga_clear_allocated
,
4176 static int irq_remapping_alloc(struct irq_domain
*domain
, unsigned int virq
,
4177 unsigned int nr_irqs
, void *arg
)
4179 struct irq_alloc_info
*info
= arg
;
4180 struct irq_data
*irq_data
;
4181 struct amd_ir_data
*data
= NULL
;
4182 struct irq_cfg
*cfg
;
4188 if (nr_irqs
> 1 && info
->type
!= X86_IRQ_ALLOC_TYPE_MSI
&&
4189 info
->type
!= X86_IRQ_ALLOC_TYPE_MSIX
)
4193 * With IRQ remapping enabled, don't need contiguous CPU vectors
4194 * to support multiple MSI interrupts.
4196 if (info
->type
== X86_IRQ_ALLOC_TYPE_MSI
)
4197 info
->flags
&= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS
;
4199 devid
= get_devid(info
);
4203 ret
= irq_domain_alloc_irqs_parent(domain
, virq
, nr_irqs
, arg
);
4207 if (info
->type
== X86_IRQ_ALLOC_TYPE_IOAPIC
) {
4208 if (get_irq_table(devid
, true))
4209 index
= info
->ioapic_pin
;
4213 index
= alloc_irq_index(devid
, nr_irqs
);
4216 pr_warn("Failed to allocate IRTE\n");
4218 goto out_free_parent
;
4221 for (i
= 0; i
< nr_irqs
; i
++) {
4222 irq_data
= irq_domain_get_irq_data(domain
, virq
+ i
);
4223 cfg
= irqd_cfg(irq_data
);
4224 if (!irq_data
|| !cfg
) {
4230 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
4234 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir
))
4235 data
->entry
= kzalloc(sizeof(union irte
), GFP_KERNEL
);
4237 data
->entry
= kzalloc(sizeof(struct irte_ga
),
4244 irq_data
->hwirq
= (devid
<< 16) + i
;
4245 irq_data
->chip_data
= data
;
4246 irq_data
->chip
= &amd_ir_chip
;
4247 irq_remapping_prepare_irte(data
, cfg
, info
, devid
, index
, i
);
4248 irq_set_status_flags(virq
+ i
, IRQ_MOVE_PCNTXT
);
4254 for (i
--; i
>= 0; i
--) {
4255 irq_data
= irq_domain_get_irq_data(domain
, virq
+ i
);
4257 kfree(irq_data
->chip_data
);
4259 for (i
= 0; i
< nr_irqs
; i
++)
4260 free_irte(devid
, index
+ i
);
4262 irq_domain_free_irqs_common(domain
, virq
, nr_irqs
);
4266 static void irq_remapping_free(struct irq_domain
*domain
, unsigned int virq
,
4267 unsigned int nr_irqs
)
4269 struct irq_2_irte
*irte_info
;
4270 struct irq_data
*irq_data
;
4271 struct amd_ir_data
*data
;
4274 for (i
= 0; i
< nr_irqs
; i
++) {
4275 irq_data
= irq_domain_get_irq_data(domain
, virq
+ i
);
4276 if (irq_data
&& irq_data
->chip_data
) {
4277 data
= irq_data
->chip_data
;
4278 irte_info
= &data
->irq_2_irte
;
4279 free_irte(irte_info
->devid
, irte_info
->index
);
4284 irq_domain_free_irqs_common(domain
, virq
, nr_irqs
);
4287 static void irq_remapping_activate(struct irq_domain
*domain
,
4288 struct irq_data
*irq_data
)
4290 struct amd_ir_data
*data
= irq_data
->chip_data
;
4291 struct irq_2_irte
*irte_info
= &data
->irq_2_irte
;
4292 struct amd_iommu
*iommu
= amd_iommu_rlookup_table
[irte_info
->devid
];
4295 iommu
->irte_ops
->activate(data
->entry
, irte_info
->devid
,
4299 static void irq_remapping_deactivate(struct irq_domain
*domain
,
4300 struct irq_data
*irq_data
)
4302 struct amd_ir_data
*data
= irq_data
->chip_data
;
4303 struct irq_2_irte
*irte_info
= &data
->irq_2_irte
;
4304 struct amd_iommu
*iommu
= amd_iommu_rlookup_table
[irte_info
->devid
];
4307 iommu
->irte_ops
->deactivate(data
->entry
, irte_info
->devid
,
4311 static const struct irq_domain_ops amd_ir_domain_ops
= {
4312 .alloc
= irq_remapping_alloc
,
4313 .free
= irq_remapping_free
,
4314 .activate
= irq_remapping_activate
,
4315 .deactivate
= irq_remapping_deactivate
,
4318 static int amd_ir_set_vcpu_affinity(struct irq_data
*data
, void *vcpu_info
)
4320 struct amd_iommu
*iommu
;
4321 struct amd_iommu_pi_data
*pi_data
= vcpu_info
;
4322 struct vcpu_data
*vcpu_pi_info
= pi_data
->vcpu_data
;
4323 struct amd_ir_data
*ir_data
= data
->chip_data
;
4324 struct irte_ga
*irte
= (struct irte_ga
*) ir_data
->entry
;
4325 struct irq_2_irte
*irte_info
= &ir_data
->irq_2_irte
;
4326 struct iommu_dev_data
*dev_data
= search_dev_data(irte_info
->devid
);
4329 * This device has never been set up for guest mode.
4330 * we should not modify the IRTE
4332 if (!dev_data
|| !dev_data
->use_vapic
)
4335 pi_data
->ir_data
= ir_data
;
4338 * SVM tries to set up for VAPIC mode, but we are in
4339 * legacy mode. So, we force legacy mode instead.
4341 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
)) {
4342 pr_debug("AMD-Vi: %s: Fall back to using intr legacy remap\n",
4344 pi_data
->is_guest_mode
= false;
4347 iommu
= amd_iommu_rlookup_table
[irte_info
->devid
];
4351 pi_data
->prev_ga_tag
= ir_data
->cached_ga_tag
;
4352 if (pi_data
->is_guest_mode
) {
4354 irte
->hi
.fields
.ga_root_ptr
= (pi_data
->base
>> 12);
4355 irte
->hi
.fields
.vector
= vcpu_pi_info
->vector
;
4356 irte
->lo
.fields_vapic
.guest_mode
= 1;
4357 irte
->lo
.fields_vapic
.ga_tag
= pi_data
->ga_tag
;
4359 ir_data
->cached_ga_tag
= pi_data
->ga_tag
;
4362 struct irq_cfg
*cfg
= irqd_cfg(data
);
4366 irte
->hi
.fields
.vector
= cfg
->vector
;
4367 irte
->lo
.fields_remap
.guest_mode
= 0;
4368 irte
->lo
.fields_remap
.destination
= cfg
->dest_apicid
;
4369 irte
->lo
.fields_remap
.int_type
= apic
->irq_delivery_mode
;
4370 irte
->lo
.fields_remap
.dm
= apic
->irq_dest_mode
;
4373 * This communicates the ga_tag back to the caller
4374 * so that it can do all the necessary clean up.
4376 ir_data
->cached_ga_tag
= 0;
4379 return modify_irte_ga(irte_info
->devid
, irte_info
->index
, irte
, ir_data
);
4382 static int amd_ir_set_affinity(struct irq_data
*data
,
4383 const struct cpumask
*mask
, bool force
)
4385 struct amd_ir_data
*ir_data
= data
->chip_data
;
4386 struct irq_2_irte
*irte_info
= &ir_data
->irq_2_irte
;
4387 struct irq_cfg
*cfg
= irqd_cfg(data
);
4388 struct irq_data
*parent
= data
->parent_data
;
4389 struct amd_iommu
*iommu
= amd_iommu_rlookup_table
[irte_info
->devid
];
4395 ret
= parent
->chip
->irq_set_affinity(parent
, mask
, force
);
4396 if (ret
< 0 || ret
== IRQ_SET_MASK_OK_DONE
)
4400 * Atomically updates the IRTE with the new destination, vector
4401 * and flushes the interrupt entry cache.
4403 iommu
->irte_ops
->set_affinity(ir_data
->entry
, irte_info
->devid
,
4404 irte_info
->index
, cfg
->vector
, cfg
->dest_apicid
);
4407 * After this point, all the interrupts will start arriving
4408 * at the new destination. So, time to cleanup the previous
4409 * vector allocation.
4411 send_cleanup_vector(cfg
);
4413 return IRQ_SET_MASK_OK_DONE
;
4416 static void ir_compose_msi_msg(struct irq_data
*irq_data
, struct msi_msg
*msg
)
4418 struct amd_ir_data
*ir_data
= irq_data
->chip_data
;
4420 *msg
= ir_data
->msi_entry
;
4423 static struct irq_chip amd_ir_chip
= {
4424 .irq_ack
= ir_ack_apic_edge
,
4425 .irq_set_affinity
= amd_ir_set_affinity
,
4426 .irq_set_vcpu_affinity
= amd_ir_set_vcpu_affinity
,
4427 .irq_compose_msi_msg
= ir_compose_msi_msg
,
4430 int amd_iommu_create_irq_domain(struct amd_iommu
*iommu
)
4432 iommu
->ir_domain
= irq_domain_add_tree(NULL
, &amd_ir_domain_ops
, iommu
);
4433 if (!iommu
->ir_domain
)
4436 iommu
->ir_domain
->parent
= arch_get_ir_parent_domain();
4437 iommu
->msi_domain
= arch_create_msi_irq_domain(iommu
->ir_domain
);
4442 int amd_iommu_update_ga(int cpu
, bool is_run
, void *data
)
4444 unsigned long flags
;
4445 struct amd_iommu
*iommu
;
4446 struct irq_remap_table
*irt
;
4447 struct amd_ir_data
*ir_data
= (struct amd_ir_data
*)data
;
4448 int devid
= ir_data
->irq_2_irte
.devid
;
4449 struct irte_ga
*entry
= (struct irte_ga
*) ir_data
->entry
;
4450 struct irte_ga
*ref
= (struct irte_ga
*) ir_data
->ref
;
4452 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
) ||
4453 !ref
|| !entry
|| !entry
->lo
.fields_vapic
.guest_mode
)
4456 iommu
= amd_iommu_rlookup_table
[devid
];
4460 irt
= get_irq_table(devid
, false);
4464 spin_lock_irqsave(&irt
->lock
, flags
);
4466 if (ref
->lo
.fields_vapic
.guest_mode
) {
4468 ref
->lo
.fields_vapic
.destination
= cpu
;
4469 ref
->lo
.fields_vapic
.is_run
= is_run
;
4473 spin_unlock_irqrestore(&irt
->lock
, flags
);
4475 iommu_flush_irt(iommu
, devid
);
4476 iommu_completion_wait(iommu
);
4479 EXPORT_SYMBOL(amd_iommu_update_ga
);