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 #include <linux/pci.h>
30 #include <linux/dmar.h>
31 #include <linux/iova.h>
32 #include <linux/intel-iommu.h>
33 #include <linux/timer.h>
34 #include <linux/irq.h>
35 #include <linux/interrupt.h>
36 #include <linux/tboot.h>
37 #include <linux/dmi.h>
38 #include <linux/slab.h>
39 #include <asm/irq_remapping.h>
40 #include <asm/iommu_table.h>
42 #define PREFIX "DMAR: "
44 /* No locks are needed as DMA remapping hardware unit
45 * list is constructed at boot time and hotplug of
46 * these units are not supported by the architecture.
48 LIST_HEAD(dmar_drhd_units);
50 struct acpi_table_header * __initdata dmar_tbl;
51 static acpi_size dmar_tbl_size;
53 static void __init dmar_register_drhd_unit(struct dmar_drhd_unit *drhd)
56 * add INCLUDE_ALL at the tail, so scan the list will find it at
59 if (drhd->include_all)
60 list_add_tail(&drhd->list, &dmar_drhd_units);
62 list_add(&drhd->list, &dmar_drhd_units);
65 static int __init dmar_parse_one_dev_scope(struct acpi_dmar_device_scope *scope,
66 struct pci_dev **dev, u16 segment)
69 struct pci_dev *pdev = NULL;
70 struct acpi_dmar_pci_path *path;
73 bus = pci_find_bus(segment, scope->bus);
74 path = (struct acpi_dmar_pci_path *)(scope + 1);
75 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
76 / sizeof(struct acpi_dmar_pci_path);
82 * Some BIOSes list non-exist devices in DMAR table, just
87 PREFIX "Device scope bus [%d] not found\n",
91 pdev = pci_get_slot(bus, PCI_DEVFN(path->dev, path->fn));
93 printk(KERN_WARNING PREFIX
94 "Device scope device [%04x:%02x:%02x.%02x] not found\n",
95 segment, bus->number, path->dev, path->fn);
100 bus = pdev->subordinate;
103 printk(KERN_WARNING PREFIX
104 "Device scope device [%04x:%02x:%02x.%02x] not found\n",
105 segment, scope->bus, path->dev, path->fn);
109 if ((scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && \
110 pdev->subordinate) || (scope->entry_type == \
111 ACPI_DMAR_SCOPE_TYPE_BRIDGE && !pdev->subordinate)) {
113 printk(KERN_WARNING PREFIX
114 "Device scope type does not match for %s\n",
122 int __init dmar_parse_dev_scope(void *start, void *end, int *cnt,
123 struct pci_dev ***devices, u16 segment)
125 struct acpi_dmar_device_scope *scope;
131 while (start < end) {
133 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
134 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE)
136 else if (scope->entry_type != ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
137 printk(KERN_WARNING PREFIX
138 "Unsupported device scope\n");
140 start += scope->length;
145 *devices = kcalloc(*cnt, sizeof(struct pci_dev *), GFP_KERNEL);
151 while (start < end) {
153 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
154 scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE) {
155 ret = dmar_parse_one_dev_scope(scope,
156 &(*devices)[index], segment);
163 start += scope->length;
170 * dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition
171 * structure which uniquely represent one DMA remapping hardware unit
172 * present in the platform
175 dmar_parse_one_drhd(struct acpi_dmar_header *header)
177 struct acpi_dmar_hardware_unit *drhd;
178 struct dmar_drhd_unit *dmaru;
181 drhd = (struct acpi_dmar_hardware_unit *)header;
182 dmaru = kzalloc(sizeof(*dmaru), GFP_KERNEL);
187 dmaru->reg_base_addr = drhd->address;
188 dmaru->segment = drhd->segment;
189 dmaru->include_all = drhd->flags & 0x1; /* BIT0: INCLUDE_ALL */
191 ret = alloc_iommu(dmaru);
196 dmar_register_drhd_unit(dmaru);
200 static int __init dmar_parse_dev(struct dmar_drhd_unit *dmaru)
202 struct acpi_dmar_hardware_unit *drhd;
205 drhd = (struct acpi_dmar_hardware_unit *) dmaru->hdr;
207 if (dmaru->include_all)
210 ret = dmar_parse_dev_scope((void *)(drhd + 1),
211 ((void *)drhd) + drhd->header.length,
212 &dmaru->devices_cnt, &dmaru->devices,
215 list_del(&dmaru->list);
221 #ifdef CONFIG_ACPI_NUMA
223 dmar_parse_one_rhsa(struct acpi_dmar_header *header)
225 struct acpi_dmar_rhsa *rhsa;
226 struct dmar_drhd_unit *drhd;
228 rhsa = (struct acpi_dmar_rhsa *)header;
229 for_each_drhd_unit(drhd) {
230 if (drhd->reg_base_addr == rhsa->base_address) {
231 int node = acpi_map_pxm_to_node(rhsa->proximity_domain);
233 if (!node_online(node))
235 drhd->iommu->node = node;
240 1, TAINT_FIRMWARE_WORKAROUND,
241 "Your BIOS is broken; RHSA refers to non-existent DMAR unit at %llx\n"
242 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
244 dmi_get_system_info(DMI_BIOS_VENDOR),
245 dmi_get_system_info(DMI_BIOS_VERSION),
246 dmi_get_system_info(DMI_PRODUCT_VERSION));
253 dmar_table_print_dmar_entry(struct acpi_dmar_header *header)
255 struct acpi_dmar_hardware_unit *drhd;
256 struct acpi_dmar_reserved_memory *rmrr;
257 struct acpi_dmar_atsr *atsr;
258 struct acpi_dmar_rhsa *rhsa;
260 switch (header->type) {
261 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
262 drhd = container_of(header, struct acpi_dmar_hardware_unit,
264 printk (KERN_INFO PREFIX
265 "DRHD base: %#016Lx flags: %#x\n",
266 (unsigned long long)drhd->address, drhd->flags);
268 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
269 rmrr = container_of(header, struct acpi_dmar_reserved_memory,
271 printk (KERN_INFO PREFIX
272 "RMRR base: %#016Lx end: %#016Lx\n",
273 (unsigned long long)rmrr->base_address,
274 (unsigned long long)rmrr->end_address);
276 case ACPI_DMAR_TYPE_ATSR:
277 atsr = container_of(header, struct acpi_dmar_atsr, header);
278 printk(KERN_INFO PREFIX "ATSR flags: %#x\n", atsr->flags);
280 case ACPI_DMAR_HARDWARE_AFFINITY:
281 rhsa = container_of(header, struct acpi_dmar_rhsa, header);
282 printk(KERN_INFO PREFIX "RHSA base: %#016Lx proximity domain: %#x\n",
283 (unsigned long long)rhsa->base_address,
284 rhsa->proximity_domain);
290 * dmar_table_detect - checks to see if the platform supports DMAR devices
292 static int __init dmar_table_detect(void)
294 acpi_status status = AE_OK;
296 /* if we could find DMAR table, then there are DMAR devices */
297 status = acpi_get_table_with_size(ACPI_SIG_DMAR, 0,
298 (struct acpi_table_header **)&dmar_tbl,
301 if (ACPI_SUCCESS(status) && !dmar_tbl) {
302 printk (KERN_WARNING PREFIX "Unable to map DMAR\n");
303 status = AE_NOT_FOUND;
306 return (ACPI_SUCCESS(status) ? 1 : 0);
310 * parse_dmar_table - parses the DMA reporting table
313 parse_dmar_table(void)
315 struct acpi_table_dmar *dmar;
316 struct acpi_dmar_header *entry_header;
320 * Do it again, earlier dmar_tbl mapping could be mapped with
326 * ACPI tables may not be DMA protected by tboot, so use DMAR copy
327 * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
329 dmar_tbl = tboot_get_dmar_table(dmar_tbl);
331 dmar = (struct acpi_table_dmar *)dmar_tbl;
335 if (dmar->width < PAGE_SHIFT - 1) {
336 printk(KERN_WARNING PREFIX "Invalid DMAR haw\n");
340 printk (KERN_INFO PREFIX "Host address width %d\n",
343 entry_header = (struct acpi_dmar_header *)(dmar + 1);
344 while (((unsigned long)entry_header) <
345 (((unsigned long)dmar) + dmar_tbl->length)) {
346 /* Avoid looping forever on bad ACPI tables */
347 if (entry_header->length == 0) {
348 printk(KERN_WARNING PREFIX
349 "Invalid 0-length structure\n");
354 dmar_table_print_dmar_entry(entry_header);
356 switch (entry_header->type) {
357 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
358 ret = dmar_parse_one_drhd(entry_header);
360 case ACPI_DMAR_TYPE_RESERVED_MEMORY:
361 ret = dmar_parse_one_rmrr(entry_header);
363 case ACPI_DMAR_TYPE_ATSR:
364 ret = dmar_parse_one_atsr(entry_header);
366 case ACPI_DMAR_HARDWARE_AFFINITY:
367 #ifdef CONFIG_ACPI_NUMA
368 ret = dmar_parse_one_rhsa(entry_header);
372 printk(KERN_WARNING PREFIX
373 "Unknown DMAR structure type %d\n",
375 ret = 0; /* for forward compatibility */
381 entry_header = ((void *)entry_header + entry_header->length);
386 static int dmar_pci_device_match(struct pci_dev *devices[], int cnt,
392 for (index = 0; index < cnt; index++)
393 if (dev == devices[index])
396 /* Check our parent */
397 dev = dev->bus->self;
403 struct dmar_drhd_unit *
404 dmar_find_matched_drhd_unit(struct pci_dev *dev)
406 struct dmar_drhd_unit *dmaru = NULL;
407 struct acpi_dmar_hardware_unit *drhd;
409 dev = pci_physfn(dev);
411 list_for_each_entry(dmaru, &dmar_drhd_units, list) {
412 drhd = container_of(dmaru->hdr,
413 struct acpi_dmar_hardware_unit,
416 if (dmaru->include_all &&
417 drhd->segment == pci_domain_nr(dev->bus))
420 if (dmar_pci_device_match(dmaru->devices,
421 dmaru->devices_cnt, dev))
428 int __init dmar_dev_scope_init(void)
430 static int dmar_dev_scope_initialized;
431 struct dmar_drhd_unit *drhd, *drhd_n;
434 if (dmar_dev_scope_initialized)
435 return dmar_dev_scope_initialized;
437 if (list_empty(&dmar_drhd_units))
440 list_for_each_entry_safe(drhd, drhd_n, &dmar_drhd_units, list) {
441 ret = dmar_parse_dev(drhd);
446 ret = dmar_parse_rmrr_atsr_dev();
450 dmar_dev_scope_initialized = 1;
454 dmar_dev_scope_initialized = ret;
459 int __init dmar_table_init(void)
461 static int dmar_table_initialized;
464 if (dmar_table_initialized)
467 dmar_table_initialized = 1;
469 ret = parse_dmar_table();
472 printk(KERN_INFO PREFIX "parse DMAR table failure.\n");
476 if (list_empty(&dmar_drhd_units)) {
477 printk(KERN_INFO PREFIX "No DMAR devices found\n");
484 static void warn_invalid_dmar(u64 addr, const char *message)
487 1, TAINT_FIRMWARE_WORKAROUND,
488 "Your BIOS is broken; DMAR reported at address %llx%s!\n"
489 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
491 dmi_get_system_info(DMI_BIOS_VENDOR),
492 dmi_get_system_info(DMI_BIOS_VERSION),
493 dmi_get_system_info(DMI_PRODUCT_VERSION));
496 int __init check_zero_address(void)
498 struct acpi_table_dmar *dmar;
499 struct acpi_dmar_header *entry_header;
500 struct acpi_dmar_hardware_unit *drhd;
502 dmar = (struct acpi_table_dmar *)dmar_tbl;
503 entry_header = (struct acpi_dmar_header *)(dmar + 1);
505 while (((unsigned long)entry_header) <
506 (((unsigned long)dmar) + dmar_tbl->length)) {
507 /* Avoid looping forever on bad ACPI tables */
508 if (entry_header->length == 0) {
509 printk(KERN_WARNING PREFIX
510 "Invalid 0-length structure\n");
514 if (entry_header->type == ACPI_DMAR_TYPE_HARDWARE_UNIT) {
518 drhd = (void *)entry_header;
519 if (!drhd->address) {
520 warn_invalid_dmar(0, "");
524 addr = early_ioremap(drhd->address, VTD_PAGE_SIZE);
526 printk("IOMMU: can't validate: %llx\n", drhd->address);
529 cap = dmar_readq(addr + DMAR_CAP_REG);
530 ecap = dmar_readq(addr + DMAR_ECAP_REG);
531 early_iounmap(addr, VTD_PAGE_SIZE);
532 if (cap == (uint64_t)-1 && ecap == (uint64_t)-1) {
533 warn_invalid_dmar(drhd->address,
534 " returns all ones");
539 entry_header = ((void *)entry_header + entry_header->length);
547 int __init detect_intel_iommu(void)
551 ret = dmar_table_detect();
553 ret = check_zero_address();
555 struct acpi_table_dmar *dmar;
557 dmar = (struct acpi_table_dmar *) dmar_tbl;
559 if (ret && irq_remapping_enabled && cpu_has_x2apic &&
562 "Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
564 if (ret && !no_iommu && !iommu_detected && !dmar_disabled) {
566 /* Make sure ACS will be enabled */
572 x86_init.iommu.iommu_init = intel_iommu_init;
575 early_acpi_os_unmap_memory(dmar_tbl, dmar_tbl_size);
578 return ret ? 1 : -ENODEV;
582 int alloc_iommu(struct dmar_drhd_unit *drhd)
584 struct intel_iommu *iommu;
587 static int iommu_allocated = 0;
591 if (!drhd->reg_base_addr) {
592 warn_invalid_dmar(0, "");
596 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
600 iommu->seq_id = iommu_allocated++;
601 sprintf (iommu->name, "dmar%d", iommu->seq_id);
603 iommu->reg = ioremap(drhd->reg_base_addr, VTD_PAGE_SIZE);
605 printk(KERN_ERR "IOMMU: can't map the region\n");
608 iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
609 iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
611 if (iommu->cap == (uint64_t)-1 && iommu->ecap == (uint64_t)-1) {
612 warn_invalid_dmar(drhd->reg_base_addr, " returns all ones");
616 agaw = iommu_calculate_agaw(iommu);
619 "Cannot get a valid agaw for iommu (seq_id = %d)\n",
623 msagaw = iommu_calculate_max_sagaw(iommu);
626 "Cannot get a valid max agaw for iommu (seq_id = %d)\n",
631 iommu->msagaw = msagaw;
635 /* the registers might be more than one page */
636 map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
637 cap_max_fault_reg_offset(iommu->cap));
638 map_size = VTD_PAGE_ALIGN(map_size);
639 if (map_size > VTD_PAGE_SIZE) {
641 iommu->reg = ioremap(drhd->reg_base_addr, map_size);
643 printk(KERN_ERR "IOMMU: can't map the region\n");
648 ver = readl(iommu->reg + DMAR_VER_REG);
649 pr_info("IOMMU %d: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
651 (unsigned long long)drhd->reg_base_addr,
652 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver),
653 (unsigned long long)iommu->cap,
654 (unsigned long long)iommu->ecap);
656 raw_spin_lock_init(&iommu->register_lock);
668 void free_iommu(struct intel_iommu *iommu)
673 free_dmar_iommu(iommu);
681 * Reclaim all the submitted descriptors which have completed its work.
683 static inline void reclaim_free_desc(struct q_inval *qi)
685 while (qi->desc_status[qi->free_tail] == QI_DONE ||
686 qi->desc_status[qi->free_tail] == QI_ABORT) {
687 qi->desc_status[qi->free_tail] = QI_FREE;
688 qi->free_tail = (qi->free_tail + 1) % QI_LENGTH;
693 static int qi_check_fault(struct intel_iommu *iommu, int index)
697 struct q_inval *qi = iommu->qi;
698 int wait_index = (index + 1) % QI_LENGTH;
700 if (qi->desc_status[wait_index] == QI_ABORT)
703 fault = readl(iommu->reg + DMAR_FSTS_REG);
706 * If IQE happens, the head points to the descriptor associated
707 * with the error. No new descriptors are fetched until the IQE
710 if (fault & DMA_FSTS_IQE) {
711 head = readl(iommu->reg + DMAR_IQH_REG);
712 if ((head >> DMAR_IQ_SHIFT) == index) {
713 printk(KERN_ERR "VT-d detected invalid descriptor: "
714 "low=%llx, high=%llx\n",
715 (unsigned long long)qi->desc[index].low,
716 (unsigned long long)qi->desc[index].high);
717 memcpy(&qi->desc[index], &qi->desc[wait_index],
718 sizeof(struct qi_desc));
719 __iommu_flush_cache(iommu, &qi->desc[index],
720 sizeof(struct qi_desc));
721 writel(DMA_FSTS_IQE, iommu->reg + DMAR_FSTS_REG);
727 * If ITE happens, all pending wait_desc commands are aborted.
728 * No new descriptors are fetched until the ITE is cleared.
730 if (fault & DMA_FSTS_ITE) {
731 head = readl(iommu->reg + DMAR_IQH_REG);
732 head = ((head >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
734 tail = readl(iommu->reg + DMAR_IQT_REG);
735 tail = ((tail >> DMAR_IQ_SHIFT) - 1 + QI_LENGTH) % QI_LENGTH;
737 writel(DMA_FSTS_ITE, iommu->reg + DMAR_FSTS_REG);
740 if (qi->desc_status[head] == QI_IN_USE)
741 qi->desc_status[head] = QI_ABORT;
742 head = (head - 2 + QI_LENGTH) % QI_LENGTH;
743 } while (head != tail);
745 if (qi->desc_status[wait_index] == QI_ABORT)
749 if (fault & DMA_FSTS_ICE)
750 writel(DMA_FSTS_ICE, iommu->reg + DMAR_FSTS_REG);
756 * Submit the queued invalidation descriptor to the remapping
757 * hardware unit and wait for its completion.
759 int qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu)
762 struct q_inval *qi = iommu->qi;
763 struct qi_desc *hw, wait_desc;
764 int wait_index, index;
775 raw_spin_lock_irqsave(&qi->q_lock, flags);
776 while (qi->free_cnt < 3) {
777 raw_spin_unlock_irqrestore(&qi->q_lock, flags);
779 raw_spin_lock_irqsave(&qi->q_lock, flags);
782 index = qi->free_head;
783 wait_index = (index + 1) % QI_LENGTH;
785 qi->desc_status[index] = qi->desc_status[wait_index] = QI_IN_USE;
789 wait_desc.low = QI_IWD_STATUS_DATA(QI_DONE) |
790 QI_IWD_STATUS_WRITE | QI_IWD_TYPE;
791 wait_desc.high = virt_to_phys(&qi->desc_status[wait_index]);
793 hw[wait_index] = wait_desc;
795 __iommu_flush_cache(iommu, &hw[index], sizeof(struct qi_desc));
796 __iommu_flush_cache(iommu, &hw[wait_index], sizeof(struct qi_desc));
798 qi->free_head = (qi->free_head + 2) % QI_LENGTH;
802 * update the HW tail register indicating the presence of
805 writel(qi->free_head << DMAR_IQ_SHIFT, iommu->reg + DMAR_IQT_REG);
807 while (qi->desc_status[wait_index] != QI_DONE) {
809 * We will leave the interrupts disabled, to prevent interrupt
810 * context to queue another cmd while a cmd is already submitted
811 * and waiting for completion on this cpu. This is to avoid
812 * a deadlock where the interrupt context can wait indefinitely
813 * for free slots in the queue.
815 rc = qi_check_fault(iommu, index);
819 raw_spin_unlock(&qi->q_lock);
821 raw_spin_lock(&qi->q_lock);
824 qi->desc_status[index] = QI_DONE;
826 reclaim_free_desc(qi);
827 raw_spin_unlock_irqrestore(&qi->q_lock, flags);
836 * Flush the global interrupt entry cache.
838 void qi_global_iec(struct intel_iommu *iommu)
842 desc.low = QI_IEC_TYPE;
845 /* should never fail */
846 qi_submit_sync(&desc, iommu);
849 void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm,
854 desc.low = QI_CC_FM(fm) | QI_CC_SID(sid) | QI_CC_DID(did)
855 | QI_CC_GRAN(type) | QI_CC_TYPE;
858 qi_submit_sync(&desc, iommu);
861 void qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
862 unsigned int size_order, u64 type)
869 if (cap_write_drain(iommu->cap))
872 if (cap_read_drain(iommu->cap))
875 desc.low = QI_IOTLB_DID(did) | QI_IOTLB_DR(dr) | QI_IOTLB_DW(dw)
876 | QI_IOTLB_GRAN(type) | QI_IOTLB_TYPE;
877 desc.high = QI_IOTLB_ADDR(addr) | QI_IOTLB_IH(ih)
878 | QI_IOTLB_AM(size_order);
880 qi_submit_sync(&desc, iommu);
883 void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 qdep,
884 u64 addr, unsigned mask)
889 BUG_ON(addr & ((1 << (VTD_PAGE_SHIFT + mask)) - 1));
890 addr |= (1 << (VTD_PAGE_SHIFT + mask - 1)) - 1;
891 desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE;
893 desc.high = QI_DEV_IOTLB_ADDR(addr);
895 if (qdep >= QI_DEV_IOTLB_MAX_INVS)
898 desc.low = QI_DEV_IOTLB_SID(sid) | QI_DEV_IOTLB_QDEP(qdep) |
901 qi_submit_sync(&desc, iommu);
905 * Disable Queued Invalidation interface.
907 void dmar_disable_qi(struct intel_iommu *iommu)
911 cycles_t start_time = get_cycles();
913 if (!ecap_qis(iommu->ecap))
916 raw_spin_lock_irqsave(&iommu->register_lock, flags);
918 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
919 if (!(sts & DMA_GSTS_QIES))
923 * Give a chance to HW to complete the pending invalidation requests.
925 while ((readl(iommu->reg + DMAR_IQT_REG) !=
926 readl(iommu->reg + DMAR_IQH_REG)) &&
927 (DMAR_OPERATION_TIMEOUT > (get_cycles() - start_time)))
930 iommu->gcmd &= ~DMA_GCMD_QIE;
931 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
933 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl,
934 !(sts & DMA_GSTS_QIES), sts);
936 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
940 * Enable queued invalidation.
942 static void __dmar_enable_qi(struct intel_iommu *iommu)
946 struct q_inval *qi = iommu->qi;
948 qi->free_head = qi->free_tail = 0;
949 qi->free_cnt = QI_LENGTH;
951 raw_spin_lock_irqsave(&iommu->register_lock, flags);
953 /* write zero to the tail reg */
954 writel(0, iommu->reg + DMAR_IQT_REG);
956 dmar_writeq(iommu->reg + DMAR_IQA_REG, virt_to_phys(qi->desc));
958 iommu->gcmd |= DMA_GCMD_QIE;
959 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
961 /* Make sure hardware complete it */
962 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, readl, (sts & DMA_GSTS_QIES), sts);
964 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
968 * Enable Queued Invalidation interface. This is a must to support
969 * interrupt-remapping. Also used by DMA-remapping, which replaces
970 * register based IOTLB invalidation.
972 int dmar_enable_qi(struct intel_iommu *iommu)
975 struct page *desc_page;
977 if (!ecap_qis(iommu->ecap))
981 * queued invalidation is already setup and enabled.
986 iommu->qi = kmalloc(sizeof(*qi), GFP_ATOMIC);
993 desc_page = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO, 0);
1000 qi->desc = page_address(desc_page);
1002 qi->desc_status = kmalloc(QI_LENGTH * sizeof(int), GFP_ATOMIC);
1003 if (!qi->desc_status) {
1004 free_page((unsigned long) qi->desc);
1010 qi->free_head = qi->free_tail = 0;
1011 qi->free_cnt = QI_LENGTH;
1013 raw_spin_lock_init(&qi->q_lock);
1015 __dmar_enable_qi(iommu);
1020 /* iommu interrupt handling. Most stuff are MSI-like. */
1028 static const char *dma_remap_fault_reasons[] =
1031 "Present bit in root entry is clear",
1032 "Present bit in context entry is clear",
1033 "Invalid context entry",
1034 "Access beyond MGAW",
1035 "PTE Write access is not set",
1036 "PTE Read access is not set",
1037 "Next page table ptr is invalid",
1038 "Root table address invalid",
1039 "Context table ptr is invalid",
1040 "non-zero reserved fields in RTP",
1041 "non-zero reserved fields in CTP",
1042 "non-zero reserved fields in PTE",
1045 static const char *irq_remap_fault_reasons[] =
1047 "Detected reserved fields in the decoded interrupt-remapped request",
1048 "Interrupt index exceeded the interrupt-remapping table size",
1049 "Present field in the IRTE entry is clear",
1050 "Error accessing interrupt-remapping table pointed by IRTA_REG",
1051 "Detected reserved fields in the IRTE entry",
1052 "Blocked a compatibility format interrupt request",
1053 "Blocked an interrupt request due to source-id verification failure",
1056 #define MAX_FAULT_REASON_IDX (ARRAY_SIZE(fault_reason_strings) - 1)
1058 const char *dmar_get_fault_reason(u8 fault_reason, int *fault_type)
1060 if (fault_reason >= 0x20 && (fault_reason <= 0x20 +
1061 ARRAY_SIZE(irq_remap_fault_reasons))) {
1062 *fault_type = INTR_REMAP;
1063 return irq_remap_fault_reasons[fault_reason - 0x20];
1064 } else if (fault_reason < ARRAY_SIZE(dma_remap_fault_reasons)) {
1065 *fault_type = DMA_REMAP;
1066 return dma_remap_fault_reasons[fault_reason];
1068 *fault_type = UNKNOWN;
1073 void dmar_msi_unmask(struct irq_data *data)
1075 struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
1079 raw_spin_lock_irqsave(&iommu->register_lock, flag);
1080 writel(0, iommu->reg + DMAR_FECTL_REG);
1081 /* Read a reg to force flush the post write */
1082 readl(iommu->reg + DMAR_FECTL_REG);
1083 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
1086 void dmar_msi_mask(struct irq_data *data)
1089 struct intel_iommu *iommu = irq_data_get_irq_handler_data(data);
1092 raw_spin_lock_irqsave(&iommu->register_lock, flag);
1093 writel(DMA_FECTL_IM, iommu->reg + DMAR_FECTL_REG);
1094 /* Read a reg to force flush the post write */
1095 readl(iommu->reg + DMAR_FECTL_REG);
1096 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
1099 void dmar_msi_write(int irq, struct msi_msg *msg)
1101 struct intel_iommu *iommu = irq_get_handler_data(irq);
1104 raw_spin_lock_irqsave(&iommu->register_lock, flag);
1105 writel(msg->data, iommu->reg + DMAR_FEDATA_REG);
1106 writel(msg->address_lo, iommu->reg + DMAR_FEADDR_REG);
1107 writel(msg->address_hi, iommu->reg + DMAR_FEUADDR_REG);
1108 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
1111 void dmar_msi_read(int irq, struct msi_msg *msg)
1113 struct intel_iommu *iommu = irq_get_handler_data(irq);
1116 raw_spin_lock_irqsave(&iommu->register_lock, flag);
1117 msg->data = readl(iommu->reg + DMAR_FEDATA_REG);
1118 msg->address_lo = readl(iommu->reg + DMAR_FEADDR_REG);
1119 msg->address_hi = readl(iommu->reg + DMAR_FEUADDR_REG);
1120 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
1123 static int dmar_fault_do_one(struct intel_iommu *iommu, int type,
1124 u8 fault_reason, u16 source_id, unsigned long long addr)
1129 reason = dmar_get_fault_reason(fault_reason, &fault_type);
1131 if (fault_type == INTR_REMAP)
1132 printk(KERN_ERR "INTR-REMAP: Request device [[%02x:%02x.%d] "
1133 "fault index %llx\n"
1134 "INTR-REMAP:[fault reason %02d] %s\n",
1135 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1136 PCI_FUNC(source_id & 0xFF), addr >> 48,
1137 fault_reason, reason);
1140 "DMAR:[%s] Request device [%02x:%02x.%d] "
1141 "fault addr %llx \n"
1142 "DMAR:[fault reason %02d] %s\n",
1143 (type ? "DMA Read" : "DMA Write"),
1144 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1145 PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason);
1149 #define PRIMARY_FAULT_REG_LEN (16)
1150 irqreturn_t dmar_fault(int irq, void *dev_id)
1152 struct intel_iommu *iommu = dev_id;
1153 int reg, fault_index;
1157 raw_spin_lock_irqsave(&iommu->register_lock, flag);
1158 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1160 printk(KERN_ERR "DRHD: handling fault status reg %x\n",
1163 /* TBD: ignore advanced fault log currently */
1164 if (!(fault_status & DMA_FSTS_PPF))
1167 fault_index = dma_fsts_fault_record_index(fault_status);
1168 reg = cap_fault_reg_offset(iommu->cap);
1176 /* highest 32 bits */
1177 data = readl(iommu->reg + reg +
1178 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1179 if (!(data & DMA_FRCD_F))
1182 fault_reason = dma_frcd_fault_reason(data);
1183 type = dma_frcd_type(data);
1185 data = readl(iommu->reg + reg +
1186 fault_index * PRIMARY_FAULT_REG_LEN + 8);
1187 source_id = dma_frcd_source_id(data);
1189 guest_addr = dmar_readq(iommu->reg + reg +
1190 fault_index * PRIMARY_FAULT_REG_LEN);
1191 guest_addr = dma_frcd_page_addr(guest_addr);
1192 /* clear the fault */
1193 writel(DMA_FRCD_F, iommu->reg + reg +
1194 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1196 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
1198 dmar_fault_do_one(iommu, type, fault_reason,
1199 source_id, guest_addr);
1202 if (fault_index >= cap_num_fault_regs(iommu->cap))
1204 raw_spin_lock_irqsave(&iommu->register_lock, flag);
1207 /* clear all the other faults */
1208 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1209 writel(fault_status, iommu->reg + DMAR_FSTS_REG);
1211 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
1215 int dmar_set_interrupt(struct intel_iommu *iommu)
1220 * Check if the fault interrupt is already initialized.
1227 printk(KERN_ERR "IOMMU: no free vectors\n");
1231 irq_set_handler_data(irq, iommu);
1234 ret = arch_setup_dmar_msi(irq);
1236 irq_set_handler_data(irq, NULL);
1242 ret = request_irq(irq, dmar_fault, IRQF_NO_THREAD, iommu->name, iommu);
1244 printk(KERN_ERR "IOMMU: can't request irq\n");
1248 int __init enable_drhd_fault_handling(void)
1250 struct dmar_drhd_unit *drhd;
1253 * Enable fault control interrupt.
1255 for_each_drhd_unit(drhd) {
1257 struct intel_iommu *iommu = drhd->iommu;
1258 ret = dmar_set_interrupt(iommu);
1261 printk(KERN_ERR "DRHD %Lx: failed to enable fault, "
1262 " interrupt, ret %d\n",
1263 (unsigned long long)drhd->reg_base_addr, ret);
1268 * Clear any previous faults.
1270 dmar_fault(iommu->irq, iommu);
1277 * Re-enable Queued Invalidation interface.
1279 int dmar_reenable_qi(struct intel_iommu *iommu)
1281 if (!ecap_qis(iommu->ecap))
1288 * First disable queued invalidation.
1290 dmar_disable_qi(iommu);
1292 * Then enable queued invalidation again. Since there is no pending
1293 * invalidation requests now, it's safe to re-enable queued
1296 __dmar_enable_qi(iommu);
1302 * Check interrupt remapping support in DMAR table description.
1304 int __init dmar_ir_support(void)
1306 struct acpi_table_dmar *dmar;
1307 dmar = (struct acpi_table_dmar *)dmar_tbl;
1310 return dmar->flags & 0x1;
1312 IOMMU_INIT_POST(detect_intel_iommu);