2 * Copyright (c) 2006, Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 * Copyright (C) 2006-2008 Intel Corporation
18 * Author: Ashok Raj <ashok.raj@intel.com>
19 * Author: Shaohua Li <shaohua.li@intel.com>
20 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
22 * This file implements early detection/parsing of Remapping Devices
23 * reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI
26 * These routines are used by both DMA-remapping and Interrupt-remapping
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt /* has to precede printk.h */
31 #include <linux/pci.h>
32 #include <linux/dmar.h>
33 #include <linux/iova.h>
34 #include <linux/intel-iommu.h>
35 #include <linux/timer.h>
36 #include <linux/irq.h>
37 #include <linux/interrupt.h>
38 #include <linux/tboot.h>
39 #include <linux/dmi.h>
40 #include <linux/slab.h>
41 #include <linux/iommu.h>
42 #include <asm/irq_remapping.h>
43 #include <asm/iommu_table.h>
45 #include "irq_remapping.h"
49 * 1) The hotplug framework guarentees that DMAR unit will be hot-added
50 * before IO devices managed by that unit.
51 * 2) The hotplug framework guarantees that DMAR unit will be hot-removed
52 * after IO devices managed by that unit.
53 * 3) Hotplug events are rare.
55 * Locking rules for DMA and interrupt remapping related global data structures:
56 * 1) Use dmar_global_lock in process context
57 * 2) Use RCU in interrupt context
59 DECLARE_RWSEM(dmar_global_lock
);
60 LIST_HEAD(dmar_drhd_units
);
62 struct acpi_table_header
* __initdata dmar_tbl
;
63 static acpi_size dmar_tbl_size
;
64 static int dmar_dev_scope_status
= 1;
66 static int alloc_iommu(struct dmar_drhd_unit
*drhd
);
67 static void free_iommu(struct intel_iommu
*iommu
);
69 static void __init
dmar_register_drhd_unit(struct dmar_drhd_unit
*drhd
)
72 * add INCLUDE_ALL at the tail, so scan the list will find it at
75 if (drhd
->include_all
)
76 list_add_tail_rcu(&drhd
->list
, &dmar_drhd_units
);
78 list_add_rcu(&drhd
->list
, &dmar_drhd_units
);
81 void *dmar_alloc_dev_scope(void *start
, void *end
, int *cnt
)
83 struct acpi_dmar_device_scope
*scope
;
88 if (scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_NAMESPACE
||
89 scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_ENDPOINT
||
90 scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_BRIDGE
)
92 else if (scope
->entry_type
!= ACPI_DMAR_SCOPE_TYPE_IOAPIC
&&
93 scope
->entry_type
!= ACPI_DMAR_SCOPE_TYPE_HPET
) {
94 pr_warn("Unsupported device scope\n");
96 start
+= scope
->length
;
101 return kcalloc(*cnt
, sizeof(struct dmar_dev_scope
), GFP_KERNEL
);
104 void dmar_free_dev_scope(struct dmar_dev_scope
**devices
, int *cnt
)
107 struct device
*tmp_dev
;
109 if (*devices
&& *cnt
) {
110 for_each_active_dev_scope(*devices
, *cnt
, i
, tmp_dev
)
119 /* Optimize out kzalloc()/kfree() for normal cases */
120 static char dmar_pci_notify_info_buf
[64];
122 static struct dmar_pci_notify_info
*
123 dmar_alloc_pci_notify_info(struct pci_dev
*dev
, unsigned long event
)
128 struct dmar_pci_notify_info
*info
;
130 BUG_ON(dev
->is_virtfn
);
132 /* Only generate path[] for device addition event */
133 if (event
== BUS_NOTIFY_ADD_DEVICE
)
134 for (tmp
= dev
; tmp
; tmp
= tmp
->bus
->self
)
137 size
= sizeof(*info
) + level
* sizeof(struct acpi_dmar_pci_path
);
138 if (size
<= sizeof(dmar_pci_notify_info_buf
)) {
139 info
= (struct dmar_pci_notify_info
*)dmar_pci_notify_info_buf
;
141 info
= kzalloc(size
, GFP_KERNEL
);
143 pr_warn("Out of memory when allocating notify_info "
144 "for %s.\n", pci_name(dev
));
145 if (dmar_dev_scope_status
== 0)
146 dmar_dev_scope_status
= -ENOMEM
;
153 info
->seg
= pci_domain_nr(dev
->bus
);
155 if (event
== BUS_NOTIFY_ADD_DEVICE
) {
156 for (tmp
= dev
; tmp
; tmp
= tmp
->bus
->self
) {
158 info
->path
[level
].bus
= tmp
->bus
->number
;
159 info
->path
[level
].device
= PCI_SLOT(tmp
->devfn
);
160 info
->path
[level
].function
= PCI_FUNC(tmp
->devfn
);
161 if (pci_is_root_bus(tmp
->bus
))
162 info
->bus
= tmp
->bus
->number
;
169 static inline void dmar_free_pci_notify_info(struct dmar_pci_notify_info
*info
)
171 if ((void *)info
!= dmar_pci_notify_info_buf
)
175 static bool dmar_match_pci_path(struct dmar_pci_notify_info
*info
, int bus
,
176 struct acpi_dmar_pci_path
*path
, int count
)
180 if (info
->bus
!= bus
)
182 if (info
->level
!= count
)
185 for (i
= 0; i
< count
; i
++) {
186 if (path
[i
].device
!= info
->path
[i
].device
||
187 path
[i
].function
!= info
->path
[i
].function
)
199 if (bus
== info
->path
[i
].bus
&&
200 path
[0].device
== info
->path
[i
].device
&&
201 path
[0].function
== info
->path
[i
].function
) {
202 pr_info(FW_BUG
"RMRR entry for device %02x:%02x.%x is broken - applying workaround\n",
203 bus
, path
[0].device
, path
[0].function
);
210 /* Return: > 0 if match found, 0 if no match found, < 0 if error happens */
211 int dmar_insert_dev_scope(struct dmar_pci_notify_info
*info
,
212 void *start
, void*end
, u16 segment
,
213 struct dmar_dev_scope
*devices
,
217 struct device
*tmp
, *dev
= &info
->dev
->dev
;
218 struct acpi_dmar_device_scope
*scope
;
219 struct acpi_dmar_pci_path
*path
;
221 if (segment
!= info
->seg
)
224 for (; start
< end
; start
+= scope
->length
) {
226 if (scope
->entry_type
!= ACPI_DMAR_SCOPE_TYPE_ENDPOINT
&&
227 scope
->entry_type
!= ACPI_DMAR_SCOPE_TYPE_BRIDGE
)
230 path
= (struct acpi_dmar_pci_path
*)(scope
+ 1);
231 level
= (scope
->length
- sizeof(*scope
)) / sizeof(*path
);
232 if (!dmar_match_pci_path(info
, scope
->bus
, path
, level
))
235 if ((scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_ENDPOINT
) ^
236 (info
->dev
->hdr_type
== PCI_HEADER_TYPE_NORMAL
)) {
237 pr_warn("Device scope type does not match for %s\n",
238 pci_name(info
->dev
));
242 for_each_dev_scope(devices
, devices_cnt
, i
, tmp
)
244 devices
[i
].bus
= info
->dev
->bus
->number
;
245 devices
[i
].devfn
= info
->dev
->devfn
;
246 rcu_assign_pointer(devices
[i
].dev
,
250 BUG_ON(i
>= devices_cnt
);
256 int dmar_remove_dev_scope(struct dmar_pci_notify_info
*info
, u16 segment
,
257 struct dmar_dev_scope
*devices
, int count
)
262 if (info
->seg
!= segment
)
265 for_each_active_dev_scope(devices
, count
, index
, tmp
)
266 if (tmp
== &info
->dev
->dev
) {
267 RCU_INIT_POINTER(devices
[index
].dev
, NULL
);
276 static int dmar_pci_bus_add_dev(struct dmar_pci_notify_info
*info
)
279 struct dmar_drhd_unit
*dmaru
;
280 struct acpi_dmar_hardware_unit
*drhd
;
282 for_each_drhd_unit(dmaru
) {
283 if (dmaru
->include_all
)
286 drhd
= container_of(dmaru
->hdr
,
287 struct acpi_dmar_hardware_unit
, header
);
288 ret
= dmar_insert_dev_scope(info
, (void *)(drhd
+ 1),
289 ((void *)drhd
) + drhd
->header
.length
,
291 dmaru
->devices
, dmaru
->devices_cnt
);
296 ret
= dmar_iommu_notify_scope_dev(info
);
297 if (ret
< 0 && dmar_dev_scope_status
== 0)
298 dmar_dev_scope_status
= ret
;
303 static void dmar_pci_bus_del_dev(struct dmar_pci_notify_info
*info
)
305 struct dmar_drhd_unit
*dmaru
;
307 for_each_drhd_unit(dmaru
)
308 if (dmar_remove_dev_scope(info
, dmaru
->segment
,
309 dmaru
->devices
, dmaru
->devices_cnt
))
311 dmar_iommu_notify_scope_dev(info
);
314 static int dmar_pci_bus_notifier(struct notifier_block
*nb
,
315 unsigned long action
, void *data
)
317 struct pci_dev
*pdev
= to_pci_dev(data
);
318 struct dmar_pci_notify_info
*info
;
320 /* Only care about add/remove events for physical functions */
323 if (action
!= BUS_NOTIFY_ADD_DEVICE
&& action
!= BUS_NOTIFY_DEL_DEVICE
)
326 info
= dmar_alloc_pci_notify_info(pdev
, action
);
330 down_write(&dmar_global_lock
);
331 if (action
== BUS_NOTIFY_ADD_DEVICE
)
332 dmar_pci_bus_add_dev(info
);
333 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
334 dmar_pci_bus_del_dev(info
);
335 up_write(&dmar_global_lock
);
337 dmar_free_pci_notify_info(info
);
342 static struct notifier_block dmar_pci_bus_nb
= {
343 .notifier_call
= dmar_pci_bus_notifier
,
348 * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
349 * structure which uniquely represent one DMA remapping hardware unit
350 * present in the platform
353 dmar_parse_one_drhd(struct acpi_dmar_header
*header
)
355 struct acpi_dmar_hardware_unit
*drhd
;
356 struct dmar_drhd_unit
*dmaru
;
359 drhd
= (struct acpi_dmar_hardware_unit
*)header
;
360 dmaru
= kzalloc(sizeof(*dmaru
), GFP_KERNEL
);
365 dmaru
->reg_base_addr
= drhd
->address
;
366 dmaru
->segment
= drhd
->segment
;
367 dmaru
->include_all
= drhd
->flags
& 0x1; /* BIT0: INCLUDE_ALL */
368 dmaru
->devices
= dmar_alloc_dev_scope((void *)(drhd
+ 1),
369 ((void *)drhd
) + drhd
->header
.length
,
370 &dmaru
->devices_cnt
);
371 if (dmaru
->devices_cnt
&& dmaru
->devices
== NULL
) {
376 ret
= alloc_iommu(dmaru
);
378 dmar_free_dev_scope(&dmaru
->devices
,
379 &dmaru
->devices_cnt
);
383 dmar_register_drhd_unit(dmaru
);
387 static void dmar_free_drhd(struct dmar_drhd_unit
*dmaru
)
389 if (dmaru
->devices
&& dmaru
->devices_cnt
)
390 dmar_free_dev_scope(&dmaru
->devices
, &dmaru
->devices_cnt
);
392 free_iommu(dmaru
->iommu
);
396 static int __init
dmar_parse_one_andd(struct acpi_dmar_header
*header
)
398 struct acpi_dmar_andd
*andd
= (void *)header
;
400 /* Check for NUL termination within the designated length */
401 if (strnlen(andd
->device_name
, header
->length
- 8) == header
->length
- 8) {
402 WARN_TAINT(1, TAINT_FIRMWARE_WORKAROUND
,
403 "Your BIOS is broken; ANDD object name is not NUL-terminated\n"
404 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
405 dmi_get_system_info(DMI_BIOS_VENDOR
),
406 dmi_get_system_info(DMI_BIOS_VERSION
),
407 dmi_get_system_info(DMI_PRODUCT_VERSION
));
410 pr_info("ANDD device: %x name: %s\n", andd
->device_number
,
416 #ifdef CONFIG_ACPI_NUMA
418 dmar_parse_one_rhsa(struct acpi_dmar_header
*header
)
420 struct acpi_dmar_rhsa
*rhsa
;
421 struct dmar_drhd_unit
*drhd
;
423 rhsa
= (struct acpi_dmar_rhsa
*)header
;
424 for_each_drhd_unit(drhd
) {
425 if (drhd
->reg_base_addr
== rhsa
->base_address
) {
426 int node
= acpi_map_pxm_to_node(rhsa
->proximity_domain
);
428 if (!node_online(node
))
430 drhd
->iommu
->node
= node
;
435 1, TAINT_FIRMWARE_WORKAROUND
,
436 "Your BIOS is broken; RHSA refers to non-existent DMAR unit at %llx\n"
437 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
439 dmi_get_system_info(DMI_BIOS_VENDOR
),
440 dmi_get_system_info(DMI_BIOS_VERSION
),
441 dmi_get_system_info(DMI_PRODUCT_VERSION
));
448 dmar_table_print_dmar_entry(struct acpi_dmar_header
*header
)
450 struct acpi_dmar_hardware_unit
*drhd
;
451 struct acpi_dmar_reserved_memory
*rmrr
;
452 struct acpi_dmar_atsr
*atsr
;
453 struct acpi_dmar_rhsa
*rhsa
;
455 switch (header
->type
) {
456 case ACPI_DMAR_TYPE_HARDWARE_UNIT
:
457 drhd
= container_of(header
, struct acpi_dmar_hardware_unit
,
459 pr_info("DRHD base: %#016Lx flags: %#x\n",
460 (unsigned long long)drhd
->address
, drhd
->flags
);
462 case ACPI_DMAR_TYPE_RESERVED_MEMORY
:
463 rmrr
= container_of(header
, struct acpi_dmar_reserved_memory
,
465 pr_info("RMRR base: %#016Lx end: %#016Lx\n",
466 (unsigned long long)rmrr
->base_address
,
467 (unsigned long long)rmrr
->end_address
);
469 case ACPI_DMAR_TYPE_ROOT_ATS
:
470 atsr
= container_of(header
, struct acpi_dmar_atsr
, header
);
471 pr_info("ATSR flags: %#x\n", atsr
->flags
);
473 case ACPI_DMAR_TYPE_HARDWARE_AFFINITY
:
474 rhsa
= container_of(header
, struct acpi_dmar_rhsa
, header
);
475 pr_info("RHSA base: %#016Lx proximity domain: %#x\n",
476 (unsigned long long)rhsa
->base_address
,
477 rhsa
->proximity_domain
);
479 case ACPI_DMAR_TYPE_NAMESPACE
:
480 /* We don't print this here because we need to sanity-check
481 it first. So print it in dmar_parse_one_andd() instead. */
487 * dmar_table_detect - checks to see if the platform supports DMAR devices
489 static int __init
dmar_table_detect(void)
491 acpi_status status
= AE_OK
;
493 /* if we could find DMAR table, then there are DMAR devices */
494 status
= acpi_get_table_with_size(ACPI_SIG_DMAR
, 0,
495 (struct acpi_table_header
**)&dmar_tbl
,
498 if (ACPI_SUCCESS(status
) && !dmar_tbl
) {
499 pr_warn("Unable to map DMAR\n");
500 status
= AE_NOT_FOUND
;
503 return (ACPI_SUCCESS(status
) ? 1 : 0);
507 * parse_dmar_table - parses the DMA reporting table
510 parse_dmar_table(void)
512 struct acpi_table_dmar
*dmar
;
513 struct acpi_dmar_header
*entry_header
;
518 * Do it again, earlier dmar_tbl mapping could be mapped with
524 * ACPI tables may not be DMA protected by tboot, so use DMAR copy
525 * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
527 dmar_tbl
= tboot_get_dmar_table(dmar_tbl
);
529 dmar
= (struct acpi_table_dmar
*)dmar_tbl
;
533 if (dmar
->width
< PAGE_SHIFT
- 1) {
534 pr_warn("Invalid DMAR haw\n");
538 pr_info("Host address width %d\n", dmar
->width
+ 1);
540 entry_header
= (struct acpi_dmar_header
*)(dmar
+ 1);
541 while (((unsigned long)entry_header
) <
542 (((unsigned long)dmar
) + dmar_tbl
->length
)) {
543 /* Avoid looping forever on bad ACPI tables */
544 if (entry_header
->length
== 0) {
545 pr_warn("Invalid 0-length structure\n");
550 dmar_table_print_dmar_entry(entry_header
);
552 switch (entry_header
->type
) {
553 case ACPI_DMAR_TYPE_HARDWARE_UNIT
:
555 ret
= dmar_parse_one_drhd(entry_header
);
557 case ACPI_DMAR_TYPE_RESERVED_MEMORY
:
558 ret
= dmar_parse_one_rmrr(entry_header
);
560 case ACPI_DMAR_TYPE_ROOT_ATS
:
561 ret
= dmar_parse_one_atsr(entry_header
);
563 case ACPI_DMAR_TYPE_HARDWARE_AFFINITY
:
564 #ifdef CONFIG_ACPI_NUMA
565 ret
= dmar_parse_one_rhsa(entry_header
);
568 case ACPI_DMAR_TYPE_NAMESPACE
:
569 ret
= dmar_parse_one_andd(entry_header
);
572 pr_warn("Unknown DMAR structure type %d\n",
574 ret
= 0; /* for forward compatibility */
580 entry_header
= ((void *)entry_header
+ entry_header
->length
);
583 pr_warn(FW_BUG
"No DRHD structure found in DMAR table\n");
587 static int dmar_pci_device_match(struct dmar_dev_scope devices
[],
588 int cnt
, struct pci_dev
*dev
)
594 for_each_active_dev_scope(devices
, cnt
, index
, tmp
)
595 if (dev_is_pci(tmp
) && dev
== to_pci_dev(tmp
))
598 /* Check our parent */
599 dev
= dev
->bus
->self
;
605 struct dmar_drhd_unit
*
606 dmar_find_matched_drhd_unit(struct pci_dev
*dev
)
608 struct dmar_drhd_unit
*dmaru
;
609 struct acpi_dmar_hardware_unit
*drhd
;
611 dev
= pci_physfn(dev
);
614 for_each_drhd_unit(dmaru
) {
615 drhd
= container_of(dmaru
->hdr
,
616 struct acpi_dmar_hardware_unit
,
619 if (dmaru
->include_all
&&
620 drhd
->segment
== pci_domain_nr(dev
->bus
))
623 if (dmar_pci_device_match(dmaru
->devices
,
624 dmaru
->devices_cnt
, dev
))
634 static void __init
dmar_acpi_insert_dev_scope(u8 device_number
,
635 struct acpi_device
*adev
)
637 struct dmar_drhd_unit
*dmaru
;
638 struct acpi_dmar_hardware_unit
*drhd
;
639 struct acpi_dmar_device_scope
*scope
;
642 struct acpi_dmar_pci_path
*path
;
644 for_each_drhd_unit(dmaru
) {
645 drhd
= container_of(dmaru
->hdr
,
646 struct acpi_dmar_hardware_unit
,
649 for (scope
= (void *)(drhd
+ 1);
650 (unsigned long)scope
< ((unsigned long)drhd
) + drhd
->header
.length
;
651 scope
= ((void *)scope
) + scope
->length
) {
652 if (scope
->entry_type
!= ACPI_DMAR_SCOPE_TYPE_NAMESPACE
)
654 if (scope
->enumeration_id
!= device_number
)
657 path
= (void *)(scope
+ 1);
658 pr_info("ACPI device \"%s\" under DMAR at %llx as %02x:%02x.%d\n",
659 dev_name(&adev
->dev
), dmaru
->reg_base_addr
,
660 scope
->bus
, path
->device
, path
->function
);
661 for_each_dev_scope(dmaru
->devices
, dmaru
->devices_cnt
, i
, tmp
)
663 dmaru
->devices
[i
].bus
= scope
->bus
;
664 dmaru
->devices
[i
].devfn
= PCI_DEVFN(path
->device
,
666 rcu_assign_pointer(dmaru
->devices
[i
].dev
,
667 get_device(&adev
->dev
));
670 BUG_ON(i
>= dmaru
->devices_cnt
);
673 pr_warn("No IOMMU scope found for ANDD enumeration ID %d (%s)\n",
674 device_number
, dev_name(&adev
->dev
));
677 static int __init
dmar_acpi_dev_scope_init(void)
679 struct acpi_dmar_andd
*andd
;
681 if (dmar_tbl
== NULL
)
684 for (andd
= (void *)dmar_tbl
+ sizeof(struct acpi_table_dmar
);
685 ((unsigned long)andd
) < ((unsigned long)dmar_tbl
) + dmar_tbl
->length
;
686 andd
= ((void *)andd
) + andd
->header
.length
) {
687 if (andd
->header
.type
== ACPI_DMAR_TYPE_NAMESPACE
) {
689 struct acpi_device
*adev
;
691 if (!ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT
,
694 pr_err("Failed to find handle for ACPI object %s\n",
698 acpi_bus_get_device(h
, &adev
);
700 pr_err("Failed to get device for ACPI object %s\n",
704 dmar_acpi_insert_dev_scope(andd
->device_number
, adev
);
710 int __init
dmar_dev_scope_init(void)
712 struct pci_dev
*dev
= NULL
;
713 struct dmar_pci_notify_info
*info
;
715 if (dmar_dev_scope_status
!= 1)
716 return dmar_dev_scope_status
;
718 if (list_empty(&dmar_drhd_units
)) {
719 dmar_dev_scope_status
= -ENODEV
;
721 dmar_dev_scope_status
= 0;
723 dmar_acpi_dev_scope_init();
725 for_each_pci_dev(dev
) {
729 info
= dmar_alloc_pci_notify_info(dev
,
730 BUS_NOTIFY_ADD_DEVICE
);
732 return dmar_dev_scope_status
;
734 dmar_pci_bus_add_dev(info
);
735 dmar_free_pci_notify_info(info
);
739 bus_register_notifier(&pci_bus_type
, &dmar_pci_bus_nb
);
742 return dmar_dev_scope_status
;
746 int __init
dmar_table_init(void)
748 static int dmar_table_initialized
;
751 if (dmar_table_initialized
== 0) {
752 ret
= parse_dmar_table();
755 pr_info("parse DMAR table failure.\n");
756 } else if (list_empty(&dmar_drhd_units
)) {
757 pr_info("No DMAR devices found\n");
762 dmar_table_initialized
= ret
;
764 dmar_table_initialized
= 1;
767 return dmar_table_initialized
< 0 ? dmar_table_initialized
: 0;
770 static void warn_invalid_dmar(u64 addr
, const char *message
)
773 1, TAINT_FIRMWARE_WORKAROUND
,
774 "Your BIOS is broken; DMAR reported at address %llx%s!\n"
775 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
777 dmi_get_system_info(DMI_BIOS_VENDOR
),
778 dmi_get_system_info(DMI_BIOS_VERSION
),
779 dmi_get_system_info(DMI_PRODUCT_VERSION
));
782 static int __init
check_zero_address(void)
784 struct acpi_table_dmar
*dmar
;
785 struct acpi_dmar_header
*entry_header
;
786 struct acpi_dmar_hardware_unit
*drhd
;
788 dmar
= (struct acpi_table_dmar
*)dmar_tbl
;
789 entry_header
= (struct acpi_dmar_header
*)(dmar
+ 1);
791 while (((unsigned long)entry_header
) <
792 (((unsigned long)dmar
) + dmar_tbl
->length
)) {
793 /* Avoid looping forever on bad ACPI tables */
794 if (entry_header
->length
== 0) {
795 pr_warn("Invalid 0-length structure\n");
799 if (entry_header
->type
== ACPI_DMAR_TYPE_HARDWARE_UNIT
) {
803 drhd
= (void *)entry_header
;
804 if (!drhd
->address
) {
805 warn_invalid_dmar(0, "");
809 addr
= early_ioremap(drhd
->address
, VTD_PAGE_SIZE
);
811 printk("IOMMU: can't validate: %llx\n", drhd
->address
);
814 cap
= dmar_readq(addr
+ DMAR_CAP_REG
);
815 ecap
= dmar_readq(addr
+ DMAR_ECAP_REG
);
816 early_iounmap(addr
, VTD_PAGE_SIZE
);
817 if (cap
== (uint64_t)-1 && ecap
== (uint64_t)-1) {
818 warn_invalid_dmar(drhd
->address
,
819 " returns all ones");
824 entry_header
= ((void *)entry_header
+ entry_header
->length
);
832 int __init
detect_intel_iommu(void)
836 down_write(&dmar_global_lock
);
837 ret
= dmar_table_detect();
839 ret
= check_zero_address();
841 if (ret
&& !no_iommu
&& !iommu_detected
&& !dmar_disabled
) {
843 /* Make sure ACS will be enabled */
849 x86_init
.iommu
.iommu_init
= intel_iommu_init
;
852 early_acpi_os_unmap_memory((void __iomem
*)dmar_tbl
, dmar_tbl_size
);
854 up_write(&dmar_global_lock
);
856 return ret
? 1 : -ENODEV
;
860 static void unmap_iommu(struct intel_iommu
*iommu
)
863 release_mem_region(iommu
->reg_phys
, iommu
->reg_size
);
867 * map_iommu: map the iommu's registers
868 * @iommu: the iommu to map
869 * @phys_addr: the physical address of the base resgister
871 * Memory map the iommu's registers. Start w/ a single page, and
872 * possibly expand if that turns out to be insufficent.
874 static int map_iommu(struct intel_iommu
*iommu
, u64 phys_addr
)
878 iommu
->reg_phys
= phys_addr
;
879 iommu
->reg_size
= VTD_PAGE_SIZE
;
881 if (!request_mem_region(iommu
->reg_phys
, iommu
->reg_size
, iommu
->name
)) {
882 pr_err("IOMMU: can't reserve memory\n");
887 iommu
->reg
= ioremap(iommu
->reg_phys
, iommu
->reg_size
);
889 pr_err("IOMMU: can't map the region\n");
894 iommu
->cap
= dmar_readq(iommu
->reg
+ DMAR_CAP_REG
);
895 iommu
->ecap
= dmar_readq(iommu
->reg
+ DMAR_ECAP_REG
);
897 if (iommu
->cap
== (uint64_t)-1 && iommu
->ecap
== (uint64_t)-1) {
899 warn_invalid_dmar(phys_addr
, " returns all ones");
903 /* the registers might be more than one page */
904 map_size
= max_t(int, ecap_max_iotlb_offset(iommu
->ecap
),
905 cap_max_fault_reg_offset(iommu
->cap
));
906 map_size
= VTD_PAGE_ALIGN(map_size
);
907 if (map_size
> iommu
->reg_size
) {
909 release_mem_region(iommu
->reg_phys
, iommu
->reg_size
);
910 iommu
->reg_size
= map_size
;
911 if (!request_mem_region(iommu
->reg_phys
, iommu
->reg_size
,
913 pr_err("IOMMU: can't reserve memory\n");
917 iommu
->reg
= ioremap(iommu
->reg_phys
, iommu
->reg_size
);
919 pr_err("IOMMU: can't map the region\n");
930 release_mem_region(iommu
->reg_phys
, iommu
->reg_size
);
935 static int alloc_iommu(struct dmar_drhd_unit
*drhd
)
937 struct intel_iommu
*iommu
;
939 static int iommu_allocated
= 0;
944 if (!drhd
->reg_base_addr
) {
945 warn_invalid_dmar(0, "");
949 iommu
= kzalloc(sizeof(*iommu
), GFP_KERNEL
);
953 iommu
->seq_id
= iommu_allocated
++;
954 sprintf (iommu
->name
, "dmar%d", iommu
->seq_id
);
956 err
= map_iommu(iommu
, drhd
->reg_base_addr
);
958 pr_err("IOMMU: failed to map %s\n", iommu
->name
);
963 agaw
= iommu_calculate_agaw(iommu
);
965 pr_err("Cannot get a valid agaw for iommu (seq_id = %d)\n",
969 msagaw
= iommu_calculate_max_sagaw(iommu
);
971 pr_err("Cannot get a valid max agaw for iommu (seq_id = %d)\n",
976 iommu
->msagaw
= msagaw
;
977 iommu
->segment
= drhd
->segment
;
981 ver
= readl(iommu
->reg
+ DMAR_VER_REG
);
982 pr_info("IOMMU %d: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
984 (unsigned long long)drhd
->reg_base_addr
,
985 DMAR_VER_MAJOR(ver
), DMAR_VER_MINOR(ver
),
986 (unsigned long long)iommu
->cap
,
987 (unsigned long long)iommu
->ecap
);
989 /* Reflect status in gcmd */
990 sts
= readl(iommu
->reg
+ DMAR_GSTS_REG
);
991 if (sts
& DMA_GSTS_IRES
)
992 iommu
->gcmd
|= DMA_GCMD_IRE
;
993 if (sts
& DMA_GSTS_TES
)
994 iommu
->gcmd
|= DMA_GCMD_TE
;
995 if (sts
& DMA_GSTS_QIES
)
996 iommu
->gcmd
|= DMA_GCMD_QIE
;
998 raw_spin_lock_init(&iommu
->register_lock
);
1000 drhd
->iommu
= iommu
;
1002 if (intel_iommu_enabled
)
1003 iommu
->iommu_dev
= iommu_device_create(NULL
, iommu
,
1016 static void free_iommu(struct intel_iommu
*iommu
)
1018 iommu_device_destroy(iommu
->iommu_dev
);
1021 free_irq(iommu
->irq
, iommu
);
1022 irq_set_handler_data(iommu
->irq
, NULL
);
1023 dmar_free_hwirq(iommu
->irq
);
1027 free_page((unsigned long)iommu
->qi
->desc
);
1028 kfree(iommu
->qi
->desc_status
);
1039 * Reclaim all the submitted descriptors which have completed its work.
1041 static inline void reclaim_free_desc(struct q_inval
*qi
)
1043 while (qi
->desc_status
[qi
->free_tail
] == QI_DONE
||
1044 qi
->desc_status
[qi
->free_tail
] == QI_ABORT
) {
1045 qi
->desc_status
[qi
->free_tail
] = QI_FREE
;
1046 qi
->free_tail
= (qi
->free_tail
+ 1) % QI_LENGTH
;
1051 static int qi_check_fault(struct intel_iommu
*iommu
, int index
)
1055 struct q_inval
*qi
= iommu
->qi
;
1056 int wait_index
= (index
+ 1) % QI_LENGTH
;
1058 if (qi
->desc_status
[wait_index
] == QI_ABORT
)
1061 fault
= readl(iommu
->reg
+ DMAR_FSTS_REG
);
1064 * If IQE happens, the head points to the descriptor associated
1065 * with the error. No new descriptors are fetched until the IQE
1068 if (fault
& DMA_FSTS_IQE
) {
1069 head
= readl(iommu
->reg
+ DMAR_IQH_REG
);
1070 if ((head
>> DMAR_IQ_SHIFT
) == index
) {
1071 pr_err("VT-d detected invalid descriptor: "
1072 "low=%llx, high=%llx\n",
1073 (unsigned long long)qi
->desc
[index
].low
,
1074 (unsigned long long)qi
->desc
[index
].high
);
1075 memcpy(&qi
->desc
[index
], &qi
->desc
[wait_index
],
1076 sizeof(struct qi_desc
));
1077 __iommu_flush_cache(iommu
, &qi
->desc
[index
],
1078 sizeof(struct qi_desc
));
1079 writel(DMA_FSTS_IQE
, iommu
->reg
+ DMAR_FSTS_REG
);
1085 * If ITE happens, all pending wait_desc commands are aborted.
1086 * No new descriptors are fetched until the ITE is cleared.
1088 if (fault
& DMA_FSTS_ITE
) {
1089 head
= readl(iommu
->reg
+ DMAR_IQH_REG
);
1090 head
= ((head
>> DMAR_IQ_SHIFT
) - 1 + QI_LENGTH
) % QI_LENGTH
;
1092 tail
= readl(iommu
->reg
+ DMAR_IQT_REG
);
1093 tail
= ((tail
>> DMAR_IQ_SHIFT
) - 1 + QI_LENGTH
) % QI_LENGTH
;
1095 writel(DMA_FSTS_ITE
, iommu
->reg
+ DMAR_FSTS_REG
);
1098 if (qi
->desc_status
[head
] == QI_IN_USE
)
1099 qi
->desc_status
[head
] = QI_ABORT
;
1100 head
= (head
- 2 + QI_LENGTH
) % QI_LENGTH
;
1101 } while (head
!= tail
);
1103 if (qi
->desc_status
[wait_index
] == QI_ABORT
)
1107 if (fault
& DMA_FSTS_ICE
)
1108 writel(DMA_FSTS_ICE
, iommu
->reg
+ DMAR_FSTS_REG
);
1114 * Submit the queued invalidation descriptor to the remapping
1115 * hardware unit and wait for its completion.
1117 int qi_submit_sync(struct qi_desc
*desc
, struct intel_iommu
*iommu
)
1120 struct q_inval
*qi
= iommu
->qi
;
1121 struct qi_desc
*hw
, wait_desc
;
1122 int wait_index
, index
;
1123 unsigned long flags
;
1133 raw_spin_lock_irqsave(&qi
->q_lock
, flags
);
1134 while (qi
->free_cnt
< 3) {
1135 raw_spin_unlock_irqrestore(&qi
->q_lock
, flags
);
1137 raw_spin_lock_irqsave(&qi
->q_lock
, flags
);
1140 index
= qi
->free_head
;
1141 wait_index
= (index
+ 1) % QI_LENGTH
;
1143 qi
->desc_status
[index
] = qi
->desc_status
[wait_index
] = QI_IN_USE
;
1147 wait_desc
.low
= QI_IWD_STATUS_DATA(QI_DONE
) |
1148 QI_IWD_STATUS_WRITE
| QI_IWD_TYPE
;
1149 wait_desc
.high
= virt_to_phys(&qi
->desc_status
[wait_index
]);
1151 hw
[wait_index
] = wait_desc
;
1153 __iommu_flush_cache(iommu
, &hw
[index
], sizeof(struct qi_desc
));
1154 __iommu_flush_cache(iommu
, &hw
[wait_index
], sizeof(struct qi_desc
));
1156 qi
->free_head
= (qi
->free_head
+ 2) % QI_LENGTH
;
1160 * update the HW tail register indicating the presence of
1163 writel(qi
->free_head
<< DMAR_IQ_SHIFT
, iommu
->reg
+ DMAR_IQT_REG
);
1165 while (qi
->desc_status
[wait_index
] != QI_DONE
) {
1167 * We will leave the interrupts disabled, to prevent interrupt
1168 * context to queue another cmd while a cmd is already submitted
1169 * and waiting for completion on this cpu. This is to avoid
1170 * a deadlock where the interrupt context can wait indefinitely
1171 * for free slots in the queue.
1173 rc
= qi_check_fault(iommu
, index
);
1177 raw_spin_unlock(&qi
->q_lock
);
1179 raw_spin_lock(&qi
->q_lock
);
1182 qi
->desc_status
[index
] = QI_DONE
;
1184 reclaim_free_desc(qi
);
1185 raw_spin_unlock_irqrestore(&qi
->q_lock
, flags
);
1194 * Flush the global interrupt entry cache.
1196 void qi_global_iec(struct intel_iommu
*iommu
)
1198 struct qi_desc desc
;
1200 desc
.low
= QI_IEC_TYPE
;
1203 /* should never fail */
1204 qi_submit_sync(&desc
, iommu
);
1207 void qi_flush_context(struct intel_iommu
*iommu
, u16 did
, u16 sid
, u8 fm
,
1210 struct qi_desc desc
;
1212 desc
.low
= QI_CC_FM(fm
) | QI_CC_SID(sid
) | QI_CC_DID(did
)
1213 | QI_CC_GRAN(type
) | QI_CC_TYPE
;
1216 qi_submit_sync(&desc
, iommu
);
1219 void qi_flush_iotlb(struct intel_iommu
*iommu
, u16 did
, u64 addr
,
1220 unsigned int size_order
, u64 type
)
1224 struct qi_desc desc
;
1227 if (cap_write_drain(iommu
->cap
))
1230 if (cap_read_drain(iommu
->cap
))
1233 desc
.low
= QI_IOTLB_DID(did
) | QI_IOTLB_DR(dr
) | QI_IOTLB_DW(dw
)
1234 | QI_IOTLB_GRAN(type
) | QI_IOTLB_TYPE
;
1235 desc
.high
= QI_IOTLB_ADDR(addr
) | QI_IOTLB_IH(ih
)
1236 | QI_IOTLB_AM(size_order
);
1238 qi_submit_sync(&desc
, iommu
);
1241 void qi_flush_dev_iotlb(struct intel_iommu
*iommu
, u16 sid
, u16 qdep
,
1242 u64 addr
, unsigned mask
)
1244 struct qi_desc desc
;
1247 BUG_ON(addr
& ((1 << (VTD_PAGE_SHIFT
+ mask
)) - 1));
1248 addr
|= (1 << (VTD_PAGE_SHIFT
+ mask
- 1)) - 1;
1249 desc
.high
= QI_DEV_IOTLB_ADDR(addr
) | QI_DEV_IOTLB_SIZE
;
1251 desc
.high
= QI_DEV_IOTLB_ADDR(addr
);
1253 if (qdep
>= QI_DEV_IOTLB_MAX_INVS
)
1256 desc
.low
= QI_DEV_IOTLB_SID(sid
) | QI_DEV_IOTLB_QDEP(qdep
) |
1259 qi_submit_sync(&desc
, iommu
);
1263 * Disable Queued Invalidation interface.
1265 void dmar_disable_qi(struct intel_iommu
*iommu
)
1267 unsigned long flags
;
1269 cycles_t start_time
= get_cycles();
1271 if (!ecap_qis(iommu
->ecap
))
1274 raw_spin_lock_irqsave(&iommu
->register_lock
, flags
);
1276 sts
= dmar_readq(iommu
->reg
+ DMAR_GSTS_REG
);
1277 if (!(sts
& DMA_GSTS_QIES
))
1281 * Give a chance to HW to complete the pending invalidation requests.
1283 while ((readl(iommu
->reg
+ DMAR_IQT_REG
) !=
1284 readl(iommu
->reg
+ DMAR_IQH_REG
)) &&
1285 (DMAR_OPERATION_TIMEOUT
> (get_cycles() - start_time
)))
1288 iommu
->gcmd
&= ~DMA_GCMD_QIE
;
1289 writel(iommu
->gcmd
, iommu
->reg
+ DMAR_GCMD_REG
);
1291 IOMMU_WAIT_OP(iommu
, DMAR_GSTS_REG
, readl
,
1292 !(sts
& DMA_GSTS_QIES
), sts
);
1294 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flags
);
1298 * Enable queued invalidation.
1300 static void __dmar_enable_qi(struct intel_iommu
*iommu
)
1303 unsigned long flags
;
1304 struct q_inval
*qi
= iommu
->qi
;
1306 qi
->free_head
= qi
->free_tail
= 0;
1307 qi
->free_cnt
= QI_LENGTH
;
1309 raw_spin_lock_irqsave(&iommu
->register_lock
, flags
);
1311 /* write zero to the tail reg */
1312 writel(0, iommu
->reg
+ DMAR_IQT_REG
);
1314 dmar_writeq(iommu
->reg
+ DMAR_IQA_REG
, virt_to_phys(qi
->desc
));
1316 iommu
->gcmd
|= DMA_GCMD_QIE
;
1317 writel(iommu
->gcmd
, iommu
->reg
+ DMAR_GCMD_REG
);
1319 /* Make sure hardware complete it */
1320 IOMMU_WAIT_OP(iommu
, DMAR_GSTS_REG
, readl
, (sts
& DMA_GSTS_QIES
), sts
);
1322 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flags
);
1326 * Enable Queued Invalidation interface. This is a must to support
1327 * interrupt-remapping. Also used by DMA-remapping, which replaces
1328 * register based IOTLB invalidation.
1330 int dmar_enable_qi(struct intel_iommu
*iommu
)
1333 struct page
*desc_page
;
1335 if (!ecap_qis(iommu
->ecap
))
1339 * queued invalidation is already setup and enabled.
1344 iommu
->qi
= kmalloc(sizeof(*qi
), GFP_ATOMIC
);
1351 desc_page
= alloc_pages_node(iommu
->node
, GFP_ATOMIC
| __GFP_ZERO
, 0);
1358 qi
->desc
= page_address(desc_page
);
1360 qi
->desc_status
= kzalloc(QI_LENGTH
* sizeof(int), GFP_ATOMIC
);
1361 if (!qi
->desc_status
) {
1362 free_page((unsigned long) qi
->desc
);
1368 raw_spin_lock_init(&qi
->q_lock
);
1370 __dmar_enable_qi(iommu
);
1375 /* iommu interrupt handling. Most stuff are MSI-like. */
1383 static const char *dma_remap_fault_reasons
[] =
1386 "Present bit in root entry is clear",
1387 "Present bit in context entry is clear",
1388 "Invalid context entry",
1389 "Access beyond MGAW",
1390 "PTE Write access is not set",
1391 "PTE Read access is not set",
1392 "Next page table ptr is invalid",
1393 "Root table address invalid",
1394 "Context table ptr is invalid",
1395 "non-zero reserved fields in RTP",
1396 "non-zero reserved fields in CTP",
1397 "non-zero reserved fields in PTE",
1398 "PCE for translation request specifies blocking",
1401 static const char *irq_remap_fault_reasons
[] =
1403 "Detected reserved fields in the decoded interrupt-remapped request",
1404 "Interrupt index exceeded the interrupt-remapping table size",
1405 "Present field in the IRTE entry is clear",
1406 "Error accessing interrupt-remapping table pointed by IRTA_REG",
1407 "Detected reserved fields in the IRTE entry",
1408 "Blocked a compatibility format interrupt request",
1409 "Blocked an interrupt request due to source-id verification failure",
1412 static const char *dmar_get_fault_reason(u8 fault_reason
, int *fault_type
)
1414 if (fault_reason
>= 0x20 && (fault_reason
- 0x20 <
1415 ARRAY_SIZE(irq_remap_fault_reasons
))) {
1416 *fault_type
= INTR_REMAP
;
1417 return irq_remap_fault_reasons
[fault_reason
- 0x20];
1418 } else if (fault_reason
< ARRAY_SIZE(dma_remap_fault_reasons
)) {
1419 *fault_type
= DMA_REMAP
;
1420 return dma_remap_fault_reasons
[fault_reason
];
1422 *fault_type
= UNKNOWN
;
1427 void dmar_msi_unmask(struct irq_data
*data
)
1429 struct intel_iommu
*iommu
= irq_data_get_irq_handler_data(data
);
1433 raw_spin_lock_irqsave(&iommu
->register_lock
, flag
);
1434 writel(0, iommu
->reg
+ DMAR_FECTL_REG
);
1435 /* Read a reg to force flush the post write */
1436 readl(iommu
->reg
+ DMAR_FECTL_REG
);
1437 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flag
);
1440 void dmar_msi_mask(struct irq_data
*data
)
1443 struct intel_iommu
*iommu
= irq_data_get_irq_handler_data(data
);
1446 raw_spin_lock_irqsave(&iommu
->register_lock
, flag
);
1447 writel(DMA_FECTL_IM
, iommu
->reg
+ DMAR_FECTL_REG
);
1448 /* Read a reg to force flush the post write */
1449 readl(iommu
->reg
+ DMAR_FECTL_REG
);
1450 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flag
);
1453 void dmar_msi_write(int irq
, struct msi_msg
*msg
)
1455 struct intel_iommu
*iommu
= irq_get_handler_data(irq
);
1458 raw_spin_lock_irqsave(&iommu
->register_lock
, flag
);
1459 writel(msg
->data
, iommu
->reg
+ DMAR_FEDATA_REG
);
1460 writel(msg
->address_lo
, iommu
->reg
+ DMAR_FEADDR_REG
);
1461 writel(msg
->address_hi
, iommu
->reg
+ DMAR_FEUADDR_REG
);
1462 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flag
);
1465 void dmar_msi_read(int irq
, struct msi_msg
*msg
)
1467 struct intel_iommu
*iommu
= irq_get_handler_data(irq
);
1470 raw_spin_lock_irqsave(&iommu
->register_lock
, flag
);
1471 msg
->data
= readl(iommu
->reg
+ DMAR_FEDATA_REG
);
1472 msg
->address_lo
= readl(iommu
->reg
+ DMAR_FEADDR_REG
);
1473 msg
->address_hi
= readl(iommu
->reg
+ DMAR_FEUADDR_REG
);
1474 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flag
);
1477 static int dmar_fault_do_one(struct intel_iommu
*iommu
, int type
,
1478 u8 fault_reason
, u16 source_id
, unsigned long long addr
)
1483 reason
= dmar_get_fault_reason(fault_reason
, &fault_type
);
1485 if (fault_type
== INTR_REMAP
)
1486 pr_err("INTR-REMAP: Request device [[%02x:%02x.%d] "
1487 "fault index %llx\n"
1488 "INTR-REMAP:[fault reason %02d] %s\n",
1489 (source_id
>> 8), PCI_SLOT(source_id
& 0xFF),
1490 PCI_FUNC(source_id
& 0xFF), addr
>> 48,
1491 fault_reason
, reason
);
1493 pr_err("DMAR:[%s] Request device [%02x:%02x.%d] "
1494 "fault addr %llx \n"
1495 "DMAR:[fault reason %02d] %s\n",
1496 (type
? "DMA Read" : "DMA Write"),
1497 (source_id
>> 8), PCI_SLOT(source_id
& 0xFF),
1498 PCI_FUNC(source_id
& 0xFF), addr
, fault_reason
, reason
);
1502 #define PRIMARY_FAULT_REG_LEN (16)
1503 irqreturn_t
dmar_fault(int irq
, void *dev_id
)
1505 struct intel_iommu
*iommu
= dev_id
;
1506 int reg
, fault_index
;
1510 raw_spin_lock_irqsave(&iommu
->register_lock
, flag
);
1511 fault_status
= readl(iommu
->reg
+ DMAR_FSTS_REG
);
1513 pr_err("DRHD: handling fault status reg %x\n", fault_status
);
1515 /* TBD: ignore advanced fault log currently */
1516 if (!(fault_status
& DMA_FSTS_PPF
))
1519 fault_index
= dma_fsts_fault_record_index(fault_status
);
1520 reg
= cap_fault_reg_offset(iommu
->cap
);
1528 /* highest 32 bits */
1529 data
= readl(iommu
->reg
+ reg
+
1530 fault_index
* PRIMARY_FAULT_REG_LEN
+ 12);
1531 if (!(data
& DMA_FRCD_F
))
1534 fault_reason
= dma_frcd_fault_reason(data
);
1535 type
= dma_frcd_type(data
);
1537 data
= readl(iommu
->reg
+ reg
+
1538 fault_index
* PRIMARY_FAULT_REG_LEN
+ 8);
1539 source_id
= dma_frcd_source_id(data
);
1541 guest_addr
= dmar_readq(iommu
->reg
+ reg
+
1542 fault_index
* PRIMARY_FAULT_REG_LEN
);
1543 guest_addr
= dma_frcd_page_addr(guest_addr
);
1544 /* clear the fault */
1545 writel(DMA_FRCD_F
, iommu
->reg
+ reg
+
1546 fault_index
* PRIMARY_FAULT_REG_LEN
+ 12);
1548 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flag
);
1550 dmar_fault_do_one(iommu
, type
, fault_reason
,
1551 source_id
, guest_addr
);
1554 if (fault_index
>= cap_num_fault_regs(iommu
->cap
))
1556 raw_spin_lock_irqsave(&iommu
->register_lock
, flag
);
1559 writel(DMA_FSTS_PFO
| DMA_FSTS_PPF
, iommu
->reg
+ DMAR_FSTS_REG
);
1562 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flag
);
1566 int dmar_set_interrupt(struct intel_iommu
*iommu
)
1571 * Check if the fault interrupt is already initialized.
1576 irq
= dmar_alloc_hwirq();
1578 pr_err("IOMMU: no free vectors\n");
1582 irq_set_handler_data(irq
, iommu
);
1585 ret
= arch_setup_dmar_msi(irq
);
1587 irq_set_handler_data(irq
, NULL
);
1589 dmar_free_hwirq(irq
);
1593 ret
= request_irq(irq
, dmar_fault
, IRQF_NO_THREAD
, iommu
->name
, iommu
);
1595 pr_err("IOMMU: can't request irq\n");
1599 int __init
enable_drhd_fault_handling(void)
1601 struct dmar_drhd_unit
*drhd
;
1602 struct intel_iommu
*iommu
;
1605 * Enable fault control interrupt.
1607 for_each_iommu(iommu
, drhd
) {
1609 int ret
= dmar_set_interrupt(iommu
);
1612 pr_err("DRHD %Lx: failed to enable fault, interrupt, ret %d\n",
1613 (unsigned long long)drhd
->reg_base_addr
, ret
);
1618 * Clear any previous faults.
1620 dmar_fault(iommu
->irq
, iommu
);
1621 fault_status
= readl(iommu
->reg
+ DMAR_FSTS_REG
);
1622 writel(fault_status
, iommu
->reg
+ DMAR_FSTS_REG
);
1629 * Re-enable Queued Invalidation interface.
1631 int dmar_reenable_qi(struct intel_iommu
*iommu
)
1633 if (!ecap_qis(iommu
->ecap
))
1640 * First disable queued invalidation.
1642 dmar_disable_qi(iommu
);
1644 * Then enable queued invalidation again. Since there is no pending
1645 * invalidation requests now, it's safe to re-enable queued
1648 __dmar_enable_qi(iommu
);
1654 * Check interrupt remapping support in DMAR table description.
1656 int __init
dmar_ir_support(void)
1658 struct acpi_table_dmar
*dmar
;
1659 dmar
= (struct acpi_table_dmar
*)dmar_tbl
;
1662 return dmar
->flags
& 0x1;
1665 static int __init
dmar_free_unused_resources(void)
1667 struct dmar_drhd_unit
*dmaru
, *dmaru_n
;
1669 /* DMAR units are in use */
1670 if (irq_remapping_enabled
|| intel_iommu_enabled
)
1673 if (dmar_dev_scope_status
!= 1 && !list_empty(&dmar_drhd_units
))
1674 bus_unregister_notifier(&pci_bus_type
, &dmar_pci_bus_nb
);
1676 down_write(&dmar_global_lock
);
1677 list_for_each_entry_safe(dmaru
, dmaru_n
, &dmar_drhd_units
, list
) {
1678 list_del(&dmaru
->list
);
1679 dmar_free_drhd(dmaru
);
1681 up_write(&dmar_global_lock
);
1686 late_initcall(dmar_free_unused_resources
);
1687 IOMMU_INIT_POST(detect_intel_iommu
);