Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'iommu-3.1' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu into core/iommu

+332 -267
-19
arch/arm/mach-msm/Kconfig
··· 148 148 149 149 endmenu 150 150 151 - config MSM_IOMMU 152 - bool "MSM IOMMU Support" 153 - depends on ARCH_MSM8X60 || ARCH_MSM8960 154 - select IOMMU_API 155 - default n 156 - help 157 - Support for the IOMMUs found on certain Qualcomm SOCs. 158 - These IOMMUs allow virtualization of the address space used by most 159 - cores within the multimedia subsystem. 160 - 161 - If unsure, say N here. 162 - 163 - config IOMMU_PGTABLES_L2 164 - def_bool y 165 - depends on MSM_IOMMU && MMU && SMP && CPU_DCACHE_DISABLE=n 166 - 167 151 config MSM_DEBUG_UART 168 152 int 169 153 default 1 if MSM_DEBUG_UART1 ··· 187 203 bool 188 204 189 205 config MSM_V2_TLMM 190 - bool 191 - 192 - config IOMMU_API 193 206 bool 194 207 195 208 config MSM_SCM
+1 -1
arch/arm/mach-msm/Makefile
··· 3 3 obj-$(CONFIG_DEBUG_FS) += clock-debug.o 4 4 5 5 obj-$(CONFIG_MSM_VIC) += irq-vic.o 6 - obj-$(CONFIG_MSM_IOMMU) += iommu.o iommu_dev.o devices-iommu.o 6 + obj-$(CONFIG_MSM_IOMMU) += devices-iommu.o 7 7 8 8 obj-$(CONFIG_ARCH_MSM7X00A) += dma.o irq.o acpuclock-arm11.o 9 9 obj-$(CONFIG_ARCH_MSM7X30) += dma.o
arch/arm/mach-msm/iommu.c drivers/iommu/msm_iommu.c
arch/arm/mach-msm/iommu_dev.c drivers/iommu/msm_iommu_dev.c
-24
arch/ia64/Kconfig
··· 627 627 628 628 source "drivers/pcmcia/Kconfig" 629 629 630 - config DMAR 631 - bool "Support for DMA Remapping Devices (EXPERIMENTAL)" 632 - depends on IA64_GENERIC && ACPI && EXPERIMENTAL 633 - help 634 - DMA remapping (DMAR) devices support enables independent address 635 - translations for Direct Memory Access (DMA) from devices. 636 - These DMA remapping devices are reported via ACPI tables 637 - and include PCI device scope covered by these DMA 638 - remapping devices. 639 - 640 - config DMAR_DEFAULT_ON 641 - def_bool y 642 - prompt "Enable DMA Remapping Devices by default" 643 - depends on DMAR 644 - help 645 - Selecting this option will enable a DMAR device at boot time if 646 - one is found. If this option is not selected, DMAR support can 647 - be enabled by passing intel_iommu=on to the kernel. It is 648 - recommended you say N here while the DMAR code remains 649 - experimental. 650 - 651 630 endmenu 652 631 653 632 endif ··· 660 681 661 682 config IOMMU_HELPER 662 683 def_bool (IA64_HP_ZX1 || IA64_HP_ZX1_SWIOTLB || IA64_GENERIC || SWIOTLB) 663 - 664 - config IOMMU_API 665 - def_bool (DMAR)
-79
arch/x86/Kconfig
··· 680 680 Calgary anyway, pass 'iommu=calgary' on the kernel command line. 681 681 If unsure, say Y. 682 682 683 - config AMD_IOMMU 684 - bool "AMD IOMMU support" 685 - select SWIOTLB 686 - select PCI_MSI 687 - select PCI_IOV 688 - depends on X86_64 && PCI && ACPI 689 - ---help--- 690 - With this option you can enable support for AMD IOMMU hardware in 691 - your system. An IOMMU is a hardware component which provides 692 - remapping of DMA memory accesses from devices. With an AMD IOMMU you 693 - can isolate the the DMA memory of different devices and protect the 694 - system from misbehaving device drivers or hardware. 695 - 696 - You can find out if your system has an AMD IOMMU if you look into 697 - your BIOS for an option to enable it or if you have an IVRS ACPI 698 - table. 699 - 700 - config AMD_IOMMU_STATS 701 - bool "Export AMD IOMMU statistics to debugfs" 702 - depends on AMD_IOMMU 703 - select DEBUG_FS 704 - ---help--- 705 - This option enables code in the AMD IOMMU driver to collect various 706 - statistics about whats happening in the driver and exports that 707 - information to userspace via debugfs. 708 - If unsure, say N. 709 - 710 683 # need this always selected by IOMMU for the VIA workaround 711 684 config SWIOTLB 712 685 def_bool y if X86_64 ··· 692 719 693 720 config IOMMU_HELPER 694 721 def_bool (CALGARY_IOMMU || GART_IOMMU || SWIOTLB || AMD_IOMMU) 695 - 696 - config IOMMU_API 697 - def_bool (AMD_IOMMU || DMAR) 698 722 699 723 config MAXSMP 700 724 bool "Enable Maximum number of SMP Processors and NUMA Nodes" ··· 1911 1941 is known to be incomplete. 1912 1942 1913 1943 You should say N unless you know you need this. 1914 - 1915 - config DMAR 1916 - bool "Support for DMA Remapping Devices (EXPERIMENTAL)" 1917 - depends on PCI_MSI && ACPI && EXPERIMENTAL 1918 - help 1919 - DMA remapping (DMAR) devices support enables independent address 1920 - translations for Direct Memory Access (DMA) from devices. 1921 - These DMA remapping devices are reported via ACPI tables 1922 - and include PCI device scope covered by these DMA 1923 - remapping devices. 1924 - 1925 - config DMAR_DEFAULT_ON 1926 - def_bool y 1927 - prompt "Enable DMA Remapping Devices by default" 1928 - depends on DMAR 1929 - help 1930 - Selecting this option will enable a DMAR device at boot time if 1931 - one is found. If this option is not selected, DMAR support can 1932 - be enabled by passing intel_iommu=on to the kernel. It is 1933 - recommended you say N here while the DMAR code remains 1934 - experimental. 1935 - 1936 - config DMAR_BROKEN_GFX_WA 1937 - bool "Workaround broken graphics drivers (going away soon)" 1938 - depends on DMAR && BROKEN 1939 - ---help--- 1940 - Current Graphics drivers tend to use physical address 1941 - for DMA and avoid using DMA APIs. Setting this config 1942 - option permits the IOMMU driver to set a unity map for 1943 - all the OS-visible memory. Hence the driver can continue 1944 - to use physical addresses for DMA, at least until this 1945 - option is removed in the 2.6.32 kernel. 1946 - 1947 - config DMAR_FLOPPY_WA 1948 - def_bool y 1949 - depends on DMAR 1950 - ---help--- 1951 - Floppy disk drivers are known to bypass DMA API calls 1952 - thereby failing to work when IOMMU is enabled. This 1953 - workaround will setup a 1:1 mapping for the first 1954 - 16MiB to make floppy (an ISA device) work. 1955 - 1956 - config INTR_REMAP 1957 - bool "Support for Interrupt Remapping (EXPERIMENTAL)" 1958 - depends on X86_64 && X86_IO_APIC && PCI_MSI && ACPI && EXPERIMENTAL 1959 - ---help--- 1960 - Supports Interrupt remapping for IO-APIC and MSI devices. 1961 - To use x2apic mode in the CPU's which support x2APIC enhancements or 1962 - to support platforms with CPU's having > 8 bit APIC ID, say Y. 1963 1944 1964 1945 source "drivers/pci/pcie/Kconfig" 1965 1946
arch/x86/include/asm/amd_iommu.h include/linux/amd-iommu.h
+1 -1
arch/x86/include/asm/amd_iommu_proto.h drivers/iommu/amd_iommu_proto.h
··· 19 19 #ifndef _ASM_X86_AMD_IOMMU_PROTO_H 20 20 #define _ASM_X86_AMD_IOMMU_PROTO_H 21 21 22 - #include <asm/amd_iommu_types.h> 22 + #include "amd_iommu_types.h" 23 23 24 24 extern int amd_iommu_init_dma_ops(void); 25 25 extern int amd_iommu_init_passthrough(void);
+7 -2
arch/x86/include/asm/amd_iommu_types.h drivers/iommu/amd_iommu_types.h
··· 310 310 */ 311 311 struct iommu_dev_data { 312 312 struct list_head list; /* For domain->dev_list */ 313 - struct device *dev; /* Device this data belong to */ 314 - struct device *alias; /* The Alias Device */ 313 + struct list_head dev_data_list; /* For global dev_data_list */ 314 + struct iommu_dev_data *alias_data;/* The alias dev_data */ 315 315 struct protection_domain *domain; /* Domain the device is bound to */ 316 316 atomic_t bind; /* Domain attach reverent count */ 317 + u16 devid; /* PCI Device ID */ 318 + struct { 319 + bool enabled; 320 + int qdep; 321 + } ats; /* ATS state */ 317 322 }; 318 323 319 324 /*
-1
arch/x86/kernel/Makefile
··· 123 123 124 124 obj-$(CONFIG_GART_IOMMU) += amd_gart_64.o aperture_64.o 125 125 obj-$(CONFIG_CALGARY_IOMMU) += pci-calgary_64.o tce_64.o 126 - obj-$(CONFIG_AMD_IOMMU) += amd_iommu_init.o amd_iommu.o 127 126 128 127 obj-$(CONFIG_PCI_MMCONFIG) += mmconf-fam10h_64.o 129 128 obj-y += vsmp_64.o
+187 -127
arch/x86/kernel/amd_iommu.c drivers/iommu/amd_iommu.c
··· 27 27 #include <linux/iommu-helper.h> 28 28 #include <linux/iommu.h> 29 29 #include <linux/delay.h> 30 + #include <linux/amd-iommu.h> 31 + #include <asm/msidef.h> 30 32 #include <asm/proto.h> 31 33 #include <asm/iommu.h> 32 34 #include <asm/gart.h> 33 35 #include <asm/dma.h> 34 - #include <asm/amd_iommu_proto.h> 35 - #include <asm/amd_iommu_types.h> 36 - #include <asm/amd_iommu.h> 36 + 37 + #include "amd_iommu_proto.h" 38 + #include "amd_iommu_types.h" 37 39 38 40 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) 39 41 ··· 46 44 /* A list of preallocated protection domains */ 47 45 static LIST_HEAD(iommu_pd_list); 48 46 static DEFINE_SPINLOCK(iommu_pd_list_lock); 47 + 48 + /* List of all available dev_data structures */ 49 + static LIST_HEAD(dev_data_list); 50 + static DEFINE_SPINLOCK(dev_data_list_lock); 49 51 50 52 /* 51 53 * Domain for untranslated devices - only allocated ··· 73 67 * Helper functions 74 68 * 75 69 ****************************************************************************/ 70 + 71 + static struct iommu_dev_data *alloc_dev_data(u16 devid) 72 + { 73 + struct iommu_dev_data *dev_data; 74 + unsigned long flags; 75 + 76 + dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL); 77 + if (!dev_data) 78 + return NULL; 79 + 80 + dev_data->devid = devid; 81 + atomic_set(&dev_data->bind, 0); 82 + 83 + spin_lock_irqsave(&dev_data_list_lock, flags); 84 + list_add_tail(&dev_data->dev_data_list, &dev_data_list); 85 + spin_unlock_irqrestore(&dev_data_list_lock, flags); 86 + 87 + return dev_data; 88 + } 89 + 90 + static void free_dev_data(struct iommu_dev_data *dev_data) 91 + { 92 + unsigned long flags; 93 + 94 + spin_lock_irqsave(&dev_data_list_lock, flags); 95 + list_del(&dev_data->dev_data_list); 96 + spin_unlock_irqrestore(&dev_data_list_lock, flags); 97 + 98 + kfree(dev_data); 99 + } 100 + 101 + static struct iommu_dev_data *search_dev_data(u16 devid) 102 + { 103 + struct iommu_dev_data *dev_data; 104 + unsigned long flags; 105 + 106 + spin_lock_irqsave(&dev_data_list_lock, flags); 107 + list_for_each_entry(dev_data, &dev_data_list, dev_data_list) { 108 + if (dev_data->devid == devid) 109 + goto out_unlock; 110 + } 111 + 112 + dev_data = NULL; 113 + 114 + out_unlock: 115 + spin_unlock_irqrestore(&dev_data_list_lock, flags); 116 + 117 + return dev_data; 118 + } 119 + 120 + static struct iommu_dev_data *find_dev_data(u16 devid) 121 + { 122 + struct iommu_dev_data *dev_data; 123 + 124 + dev_data = search_dev_data(devid); 125 + 126 + if (dev_data == NULL) 127 + dev_data = alloc_dev_data(devid); 128 + 129 + return dev_data; 130 + } 76 131 77 132 static inline u16 get_device_id(struct device *dev) 78 133 { ··· 205 138 static int iommu_init_device(struct device *dev) 206 139 { 207 140 struct iommu_dev_data *dev_data; 208 - struct pci_dev *pdev; 209 - u16 devid, alias; 141 + u16 alias; 210 142 211 143 if (dev->archdata.iommu) 212 144 return 0; 213 145 214 - dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL); 146 + dev_data = find_dev_data(get_device_id(dev)); 215 147 if (!dev_data) 216 148 return -ENOMEM; 217 149 218 - dev_data->dev = dev; 150 + alias = amd_iommu_alias_table[dev_data->devid]; 151 + if (alias != dev_data->devid) { 152 + struct iommu_dev_data *alias_data; 219 153 220 - devid = get_device_id(dev); 221 - alias = amd_iommu_alias_table[devid]; 222 - pdev = pci_get_bus_and_slot(PCI_BUS(alias), alias & 0xff); 223 - if (pdev) 224 - dev_data->alias = &pdev->dev; 225 - else { 226 - kfree(dev_data); 227 - return -ENOTSUPP; 154 + alias_data = find_dev_data(alias); 155 + if (alias_data == NULL) { 156 + pr_err("AMD-Vi: Warning: Unhandled device %s\n", 157 + dev_name(dev)); 158 + free_dev_data(dev_data); 159 + return -ENOTSUPP; 160 + } 161 + dev_data->alias_data = alias_data; 228 162 } 229 163 230 - atomic_set(&dev_data->bind, 0); 231 - 232 164 dev->archdata.iommu = dev_data; 233 - 234 165 235 166 return 0; 236 167 } ··· 249 184 250 185 static void iommu_uninit_device(struct device *dev) 251 186 { 252 - kfree(dev->archdata.iommu); 187 + /* 188 + * Nothing to do here - we keep dev_data around for unplugged devices 189 + * and reuse it when the device is re-plugged - not doing so would 190 + * introduce a ton of races. 191 + */ 253 192 } 254 193 255 194 void __init amd_iommu_uninit_devices(void) 256 195 { 196 + struct iommu_dev_data *dev_data, *n; 257 197 struct pci_dev *pdev = NULL; 258 198 259 199 for_each_pci_dev(pdev) { ··· 268 198 269 199 iommu_uninit_device(&pdev->dev); 270 200 } 201 + 202 + /* Free all of our dev_data structures */ 203 + list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list) 204 + free_dev_data(dev_data); 271 205 } 272 206 273 207 int __init amd_iommu_init_devices(void) ··· 728 654 /* 729 655 * Command send function for flushing on-device TLB 730 656 */ 731 - static int device_flush_iotlb(struct device *dev, u64 address, size_t size) 657 + static int device_flush_iotlb(struct iommu_dev_data *dev_data, 658 + u64 address, size_t size) 732 659 { 733 - struct pci_dev *pdev = to_pci_dev(dev); 734 660 struct amd_iommu *iommu; 735 661 struct iommu_cmd cmd; 736 - u16 devid; 737 662 int qdep; 738 663 739 - qdep = pci_ats_queue_depth(pdev); 740 - devid = get_device_id(dev); 741 - iommu = amd_iommu_rlookup_table[devid]; 664 + qdep = dev_data->ats.qdep; 665 + iommu = amd_iommu_rlookup_table[dev_data->devid]; 742 666 743 - build_inv_iotlb_pages(&cmd, devid, qdep, address, size); 667 + build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size); 744 668 745 669 return iommu_queue_command(iommu, &cmd); 746 670 } ··· 746 674 /* 747 675 * Command send function for invalidating a device table entry 748 676 */ 749 - static int device_flush_dte(struct device *dev) 677 + static int device_flush_dte(struct iommu_dev_data *dev_data) 750 678 { 751 679 struct amd_iommu *iommu; 752 - struct pci_dev *pdev; 753 - u16 devid; 754 680 int ret; 755 681 756 - pdev = to_pci_dev(dev); 757 - devid = get_device_id(dev); 758 - iommu = amd_iommu_rlookup_table[devid]; 682 + iommu = amd_iommu_rlookup_table[dev_data->devid]; 759 683 760 - ret = iommu_flush_dte(iommu, devid); 684 + ret = iommu_flush_dte(iommu, dev_data->devid); 761 685 if (ret) 762 686 return ret; 763 687 764 - if (pci_ats_enabled(pdev)) 765 - ret = device_flush_iotlb(dev, 0, ~0UL); 688 + if (dev_data->ats.enabled) 689 + ret = device_flush_iotlb(dev_data, 0, ~0UL); 766 690 767 691 return ret; 768 692 } ··· 789 721 } 790 722 791 723 list_for_each_entry(dev_data, &domain->dev_list, list) { 792 - struct pci_dev *pdev = to_pci_dev(dev_data->dev); 793 724 794 - if (!pci_ats_enabled(pdev)) 725 + if (!dev_data->ats.enabled) 795 726 continue; 796 727 797 - ret |= device_flush_iotlb(dev_data->dev, address, size); 728 + ret |= device_flush_iotlb(dev_data, address, size); 798 729 } 799 730 800 731 WARN_ON(ret); ··· 845 778 spin_lock_irqsave(&domain->lock, flags); 846 779 847 780 list_for_each_entry(dev_data, &domain->dev_list, list) 848 - device_flush_dte(dev_data->dev); 781 + device_flush_dte(dev_data); 849 782 850 783 spin_unlock_irqrestore(&domain->lock, flags); 851 784 } ··· 1203 1136 { 1204 1137 int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT; 1205 1138 struct amd_iommu *iommu; 1206 - unsigned long i; 1139 + unsigned long i, old_size; 1207 1140 1208 1141 #ifdef CONFIG_IOMMU_STRESS 1209 1142 populate = false; ··· 1239 1172 } 1240 1173 } 1241 1174 1175 + old_size = dma_dom->aperture_size; 1242 1176 dma_dom->aperture_size += APERTURE_RANGE_SIZE; 1177 + 1178 + /* Reserve address range used for MSI messages */ 1179 + if (old_size < MSI_ADDR_BASE_LO && 1180 + dma_dom->aperture_size > MSI_ADDR_BASE_LO) { 1181 + unsigned long spage; 1182 + int pages; 1183 + 1184 + pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE); 1185 + spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT; 1186 + 1187 + dma_ops_reserve_addresses(dma_dom, spage, pages); 1188 + } 1243 1189 1244 1190 /* Initialize the exclusion range if necessary */ 1245 1191 for_each_iommu(iommu) { ··· 1606 1526 amd_iommu_apply_erratum_63(devid); 1607 1527 } 1608 1528 1609 - static void do_attach(struct device *dev, struct protection_domain *domain) 1529 + static void do_attach(struct iommu_dev_data *dev_data, 1530 + struct protection_domain *domain) 1610 1531 { 1611 - struct iommu_dev_data *dev_data; 1612 1532 struct amd_iommu *iommu; 1613 - struct pci_dev *pdev; 1614 - bool ats = false; 1615 - u16 devid; 1533 + bool ats; 1616 1534 1617 - devid = get_device_id(dev); 1618 - iommu = amd_iommu_rlookup_table[devid]; 1619 - dev_data = get_dev_data(dev); 1620 - pdev = to_pci_dev(dev); 1621 - 1622 - if (amd_iommu_iotlb_sup) 1623 - ats = pci_ats_enabled(pdev); 1535 + iommu = amd_iommu_rlookup_table[dev_data->devid]; 1536 + ats = dev_data->ats.enabled; 1624 1537 1625 1538 /* Update data structures */ 1626 1539 dev_data->domain = domain; 1627 1540 list_add(&dev_data->list, &domain->dev_list); 1628 - set_dte_entry(devid, domain, ats); 1541 + set_dte_entry(dev_data->devid, domain, ats); 1629 1542 1630 1543 /* Do reference counting */ 1631 1544 domain->dev_iommu[iommu->index] += 1; 1632 1545 domain->dev_cnt += 1; 1633 1546 1634 1547 /* Flush the DTE entry */ 1635 - device_flush_dte(dev); 1548 + device_flush_dte(dev_data); 1636 1549 } 1637 1550 1638 - static void do_detach(struct device *dev) 1551 + static void do_detach(struct iommu_dev_data *dev_data) 1639 1552 { 1640 - struct iommu_dev_data *dev_data; 1641 1553 struct amd_iommu *iommu; 1642 - u16 devid; 1643 1554 1644 - devid = get_device_id(dev); 1645 - iommu = amd_iommu_rlookup_table[devid]; 1646 - dev_data = get_dev_data(dev); 1555 + iommu = amd_iommu_rlookup_table[dev_data->devid]; 1647 1556 1648 1557 /* decrease reference counters */ 1649 1558 dev_data->domain->dev_iommu[iommu->index] -= 1; ··· 1641 1572 /* Update data structures */ 1642 1573 dev_data->domain = NULL; 1643 1574 list_del(&dev_data->list); 1644 - clear_dte_entry(devid); 1575 + clear_dte_entry(dev_data->devid); 1645 1576 1646 1577 /* Flush the DTE entry */ 1647 - device_flush_dte(dev); 1578 + device_flush_dte(dev_data); 1648 1579 } 1649 1580 1650 1581 /* 1651 1582 * If a device is not yet associated with a domain, this function does 1652 1583 * assigns it visible for the hardware 1653 1584 */ 1654 - static int __attach_device(struct device *dev, 1585 + static int __attach_device(struct iommu_dev_data *dev_data, 1655 1586 struct protection_domain *domain) 1656 1587 { 1657 - struct iommu_dev_data *dev_data, *alias_data; 1658 1588 int ret; 1659 - 1660 - dev_data = get_dev_data(dev); 1661 - alias_data = get_dev_data(dev_data->alias); 1662 - 1663 - if (!alias_data) 1664 - return -EINVAL; 1665 1589 1666 1590 /* lock domain */ 1667 1591 spin_lock(&domain->lock); 1668 1592 1669 - /* Some sanity checks */ 1670 - ret = -EBUSY; 1671 - if (alias_data->domain != NULL && 1672 - alias_data->domain != domain) 1673 - goto out_unlock; 1593 + if (dev_data->alias_data != NULL) { 1594 + struct iommu_dev_data *alias_data = dev_data->alias_data; 1674 1595 1675 - if (dev_data->domain != NULL && 1676 - dev_data->domain != domain) 1677 - goto out_unlock; 1596 + /* Some sanity checks */ 1597 + ret = -EBUSY; 1598 + if (alias_data->domain != NULL && 1599 + alias_data->domain != domain) 1600 + goto out_unlock; 1678 1601 1679 - /* Do real assignment */ 1680 - if (dev_data->alias != dev) { 1681 - alias_data = get_dev_data(dev_data->alias); 1602 + if (dev_data->domain != NULL && 1603 + dev_data->domain != domain) 1604 + goto out_unlock; 1605 + 1606 + /* Do real assignment */ 1682 1607 if (alias_data->domain == NULL) 1683 - do_attach(dev_data->alias, domain); 1608 + do_attach(alias_data, domain); 1684 1609 1685 1610 atomic_inc(&alias_data->bind); 1686 1611 } 1687 1612 1688 1613 if (dev_data->domain == NULL) 1689 - do_attach(dev, domain); 1614 + do_attach(dev_data, domain); 1690 1615 1691 1616 atomic_inc(&dev_data->bind); 1692 1617 ··· 1702 1639 struct protection_domain *domain) 1703 1640 { 1704 1641 struct pci_dev *pdev = to_pci_dev(dev); 1642 + struct iommu_dev_data *dev_data; 1705 1643 unsigned long flags; 1706 1644 int ret; 1707 1645 1708 - if (amd_iommu_iotlb_sup) 1709 - pci_enable_ats(pdev, PAGE_SHIFT); 1646 + dev_data = get_dev_data(dev); 1647 + 1648 + if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) { 1649 + dev_data->ats.enabled = true; 1650 + dev_data->ats.qdep = pci_ats_queue_depth(pdev); 1651 + } 1710 1652 1711 1653 write_lock_irqsave(&amd_iommu_devtable_lock, flags); 1712 - ret = __attach_device(dev, domain); 1654 + ret = __attach_device(dev_data, domain); 1713 1655 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); 1714 1656 1715 1657 /* ··· 1730 1662 /* 1731 1663 * Removes a device from a protection domain (unlocked) 1732 1664 */ 1733 - static void __detach_device(struct device *dev) 1665 + static void __detach_device(struct iommu_dev_data *dev_data) 1734 1666 { 1735 - struct iommu_dev_data *dev_data = get_dev_data(dev); 1736 - struct iommu_dev_data *alias_data; 1737 1667 struct protection_domain *domain; 1738 1668 unsigned long flags; 1739 1669 ··· 1741 1675 1742 1676 spin_lock_irqsave(&domain->lock, flags); 1743 1677 1744 - if (dev_data->alias != dev) { 1745 - alias_data = get_dev_data(dev_data->alias); 1678 + if (dev_data->alias_data != NULL) { 1679 + struct iommu_dev_data *alias_data = dev_data->alias_data; 1680 + 1746 1681 if (atomic_dec_and_test(&alias_data->bind)) 1747 - do_detach(dev_data->alias); 1682 + do_detach(alias_data); 1748 1683 } 1749 1684 1750 1685 if (atomic_dec_and_test(&dev_data->bind)) 1751 - do_detach(dev); 1686 + do_detach(dev_data); 1752 1687 1753 1688 spin_unlock_irqrestore(&domain->lock, flags); 1754 1689 ··· 1760 1693 */ 1761 1694 if (iommu_pass_through && 1762 1695 (dev_data->domain == NULL && domain != pt_domain)) 1763 - __attach_device(dev, pt_domain); 1696 + __attach_device(dev_data, pt_domain); 1764 1697 } 1765 1698 1766 1699 /* ··· 1768 1701 */ 1769 1702 static void detach_device(struct device *dev) 1770 1703 { 1771 - struct pci_dev *pdev = to_pci_dev(dev); 1704 + struct iommu_dev_data *dev_data; 1772 1705 unsigned long flags; 1706 + 1707 + dev_data = get_dev_data(dev); 1773 1708 1774 1709 /* lock device table */ 1775 1710 write_lock_irqsave(&amd_iommu_devtable_lock, flags); 1776 - __detach_device(dev); 1711 + __detach_device(dev_data); 1777 1712 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); 1778 1713 1779 - if (amd_iommu_iotlb_sup && pci_ats_enabled(pdev)) 1780 - pci_disable_ats(pdev); 1714 + if (dev_data->ats.enabled) { 1715 + pci_disable_ats(to_pci_dev(dev)); 1716 + dev_data->ats.enabled = false; 1717 + } 1781 1718 } 1782 1719 1783 1720 /* ··· 1790 1719 */ 1791 1720 static struct protection_domain *domain_for_device(struct device *dev) 1792 1721 { 1793 - struct protection_domain *dom; 1794 - struct iommu_dev_data *dev_data, *alias_data; 1722 + struct iommu_dev_data *dev_data; 1723 + struct protection_domain *dom = NULL; 1795 1724 unsigned long flags; 1796 - u16 devid; 1797 1725 1798 - devid = get_device_id(dev); 1799 1726 dev_data = get_dev_data(dev); 1800 - alias_data = get_dev_data(dev_data->alias); 1801 - if (!alias_data) 1802 - return NULL; 1803 1727 1804 - read_lock_irqsave(&amd_iommu_devtable_lock, flags); 1805 - dom = dev_data->domain; 1806 - if (dom == NULL && 1807 - alias_data->domain != NULL) { 1808 - __attach_device(dev, alias_data->domain); 1809 - dom = alias_data->domain; 1728 + if (dev_data->domain) 1729 + return dev_data->domain; 1730 + 1731 + if (dev_data->alias_data != NULL) { 1732 + struct iommu_dev_data *alias_data = dev_data->alias_data; 1733 + 1734 + read_lock_irqsave(&amd_iommu_devtable_lock, flags); 1735 + if (alias_data->domain != NULL) { 1736 + __attach_device(dev_data, alias_data->domain); 1737 + dom = alias_data->domain; 1738 + } 1739 + read_unlock_irqrestore(&amd_iommu_devtable_lock, flags); 1810 1740 } 1811 - 1812 - read_unlock_irqrestore(&amd_iommu_devtable_lock, flags); 1813 1741 1814 1742 return dom; 1815 1743 } ··· 1868 1798 goto out; 1869 1799 } 1870 1800 1871 - device_flush_dte(dev); 1872 1801 iommu_completion_wait(iommu); 1873 1802 1874 1803 out: ··· 1927 1858 { 1928 1859 struct iommu_dev_data *dev_data; 1929 1860 1930 - list_for_each_entry(dev_data, &domain->dev_list, list) { 1931 - struct pci_dev *pdev = to_pci_dev(dev_data->dev); 1932 - u16 devid = get_device_id(dev_data->dev); 1933 - set_dte_entry(devid, domain, pci_ats_enabled(pdev)); 1934 - } 1861 + list_for_each_entry(dev_data, &domain->dev_list, list) 1862 + set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled); 1935 1863 } 1936 1864 1937 1865 static void update_domain(struct protection_domain *domain) ··· 2563 2497 write_lock_irqsave(&amd_iommu_devtable_lock, flags); 2564 2498 2565 2499 list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) { 2566 - struct device *dev = dev_data->dev; 2567 - 2568 - __detach_device(dev); 2500 + __detach_device(dev_data); 2569 2501 atomic_set(&dev_data->bind, 0); 2570 2502 } 2571 2503 ··· 2669 2605 if (!iommu) 2670 2606 return; 2671 2607 2672 - device_flush_dte(dev); 2673 2608 iommu_completion_wait(iommu); 2674 2609 } 2675 2610 ··· 2679 2616 struct iommu_dev_data *dev_data; 2680 2617 struct amd_iommu *iommu; 2681 2618 int ret; 2682 - u16 devid; 2683 2619 2684 2620 if (!check_device(dev)) 2685 2621 return -EINVAL; 2686 2622 2687 2623 dev_data = dev->archdata.iommu; 2688 2624 2689 - devid = get_device_id(dev); 2690 - 2691 - iommu = amd_iommu_rlookup_table[devid]; 2625 + iommu = amd_iommu_rlookup_table[dev_data->devid]; 2692 2626 if (!iommu) 2693 2627 return -EINVAL; 2694 2628
+5 -3
arch/x86/kernel/amd_iommu_init.c drivers/iommu/amd_iommu_init.c
··· 24 24 #include <linux/syscore_ops.h> 25 25 #include <linux/interrupt.h> 26 26 #include <linux/msi.h> 27 + #include <linux/amd-iommu.h> 27 28 #include <asm/pci-direct.h> 28 - #include <asm/amd_iommu_proto.h> 29 - #include <asm/amd_iommu_types.h> 30 - #include <asm/amd_iommu.h> 31 29 #include <asm/iommu.h> 32 30 #include <asm/gart.h> 33 31 #include <asm/x86_init.h> 34 32 #include <asm/iommu_table.h> 33 + 34 + #include "amd_iommu_proto.h" 35 + #include "amd_iommu_types.h" 36 + 35 37 /* 36 38 * definitions for the ACPI scanning code 37 39 */
+2
drivers/Kconfig
··· 126 126 127 127 source "drivers/clocksource/Kconfig" 128 128 129 + source "drivers/iommu/Kconfig" 130 + 129 131 endmenu
+1
drivers/Makefile
··· 122 122 obj-y += clk/ 123 123 124 124 obj-$(CONFIG_HWSPINLOCK) += hwspinlock/ 125 + obj-$(CONFIG_IOMMU_SUPPORT) += iommu/
-1
drivers/base/Makefile
··· 13 13 obj-$(CONFIG_NUMA) += node.o 14 14 obj-$(CONFIG_MEMORY_HOTPLUG_SPARSE) += memory.o 15 15 obj-$(CONFIG_SMP) += topology.o 16 - obj-$(CONFIG_IOMMU_API) += iommu.o 17 16 ifeq ($(CONFIG_SYSFS),y) 18 17 obj-$(CONFIG_MODULES) += module.o 19 18 endif
drivers/base/iommu.c drivers/iommu/iommu.c
+110
drivers/iommu/Kconfig
··· 1 + # IOMMU_API always gets selected by whoever wants it. 2 + config IOMMU_API 3 + bool 4 + 5 + menuconfig IOMMU_SUPPORT 6 + bool "IOMMU Hardware Support" 7 + default y 8 + ---help--- 9 + Say Y here if you want to compile device drivers for IO Memory 10 + Management Units into the kernel. These devices usually allow to 11 + remap DMA requests and/or remap interrupts from other devices on the 12 + system. 13 + 14 + if IOMMU_SUPPORT 15 + 16 + # MSM IOMMU support 17 + config MSM_IOMMU 18 + bool "MSM IOMMU Support" 19 + depends on ARCH_MSM8X60 || ARCH_MSM8960 20 + select IOMMU_API 21 + help 22 + Support for the IOMMUs found on certain Qualcomm SOCs. 23 + These IOMMUs allow virtualization of the address space used by most 24 + cores within the multimedia subsystem. 25 + 26 + If unsure, say N here. 27 + 28 + config IOMMU_PGTABLES_L2 29 + def_bool y 30 + depends on MSM_IOMMU && MMU && SMP && CPU_DCACHE_DISABLE=n 31 + 32 + # AMD IOMMU support 33 + config AMD_IOMMU 34 + bool "AMD IOMMU support" 35 + select SWIOTLB 36 + select PCI_MSI 37 + select PCI_IOV 38 + select IOMMU_API 39 + depends on X86_64 && PCI && ACPI 40 + ---help--- 41 + With this option you can enable support for AMD IOMMU hardware in 42 + your system. An IOMMU is a hardware component which provides 43 + remapping of DMA memory accesses from devices. With an AMD IOMMU you 44 + can isolate the the DMA memory of different devices and protect the 45 + system from misbehaving device drivers or hardware. 46 + 47 + You can find out if your system has an AMD IOMMU if you look into 48 + your BIOS for an option to enable it or if you have an IVRS ACPI 49 + table. 50 + 51 + config AMD_IOMMU_STATS 52 + bool "Export AMD IOMMU statistics to debugfs" 53 + depends on AMD_IOMMU 54 + select DEBUG_FS 55 + ---help--- 56 + This option enables code in the AMD IOMMU driver to collect various 57 + statistics about whats happening in the driver and exports that 58 + information to userspace via debugfs. 59 + If unsure, say N. 60 + 61 + # Intel IOMMU support 62 + config DMAR 63 + bool "Support for DMA Remapping Devices" 64 + depends on PCI_MSI && ACPI && (X86 || IA64_GENERIC) 65 + select IOMMU_API 66 + help 67 + DMA remapping (DMAR) devices support enables independent address 68 + translations for Direct Memory Access (DMA) from devices. 69 + These DMA remapping devices are reported via ACPI tables 70 + and include PCI device scope covered by these DMA 71 + remapping devices. 72 + 73 + config DMAR_DEFAULT_ON 74 + def_bool y 75 + prompt "Enable DMA Remapping Devices by default" 76 + depends on DMAR 77 + help 78 + Selecting this option will enable a DMAR device at boot time if 79 + one is found. If this option is not selected, DMAR support can 80 + be enabled by passing intel_iommu=on to the kernel. 81 + 82 + config DMAR_BROKEN_GFX_WA 83 + bool "Workaround broken graphics drivers (going away soon)" 84 + depends on DMAR && BROKEN && X86 85 + ---help--- 86 + Current Graphics drivers tend to use physical address 87 + for DMA and avoid using DMA APIs. Setting this config 88 + option permits the IOMMU driver to set a unity map for 89 + all the OS-visible memory. Hence the driver can continue 90 + to use physical addresses for DMA, at least until this 91 + option is removed in the 2.6.32 kernel. 92 + 93 + config DMAR_FLOPPY_WA 94 + def_bool y 95 + depends on DMAR && X86 96 + ---help--- 97 + Floppy disk drivers are known to bypass DMA API calls 98 + thereby failing to work when IOMMU is enabled. This 99 + workaround will setup a 1:1 mapping for the first 100 + 16MiB to make floppy (an ISA device) work. 101 + 102 + config INTR_REMAP 103 + bool "Support for Interrupt Remapping (EXPERIMENTAL)" 104 + depends on X86_64 && X86_IO_APIC && PCI_MSI && ACPI && EXPERIMENTAL 105 + ---help--- 106 + Supports Interrupt remapping for IO-APIC and MSI devices. 107 + To use x2apic mode in the CPU's which support x2APIC enhancements or 108 + to support platforms with CPU's having > 8 bit APIC ID, say Y. 109 + 110 + endif # IOMMU_SUPPORT
+5
drivers/iommu/Makefile
··· 1 + obj-$(CONFIG_IOMMU_API) += iommu.o 2 + obj-$(CONFIG_MSM_IOMMU) += msm_iommu.o msm_iommu_dev.o 3 + obj-$(CONFIG_AMD_IOMMU) += amd_iommu.o amd_iommu_init.o 4 + obj-$(CONFIG_DMAR) += dmar.o iova.o intel-iommu.o 5 + obj-$(CONFIG_INTR_REMAP) += dmar.o intr_remapping.o
-5
drivers/pci/Makefile
··· 29 29 # Build the Hypertransport interrupt support 30 30 obj-$(CONFIG_HT_IRQ) += htirq.o 31 31 32 - # Build Intel IOMMU support 33 - obj-$(CONFIG_DMAR) += dmar.o iova.o intel-iommu.o 34 - 35 - obj-$(CONFIG_INTR_REMAP) += dmar.o intr_remapping.o 36 - 37 32 obj-$(CONFIG_PCI_IOV) += iov.o 38 33 39 34 #
drivers/pci/dmar.c drivers/iommu/dmar.c
-1
drivers/pci/intel-iommu.c drivers/iommu/intel-iommu.c
··· 42 42 #include <linux/pci-ats.h> 43 43 #include <asm/cacheflush.h> 44 44 #include <asm/iommu.h> 45 - #include "pci.h" 46 45 47 46 #define ROOT_SIZE VTD_PAGE_SIZE 48 47 #define CONTEXT_SIZE VTD_PAGE_SIZE
-1
drivers/pci/intr_remapping.c drivers/iommu/intr_remapping.c
··· 13 13 #include "intr_remapping.h" 14 14 #include <acpi/acpi.h> 15 15 #include <asm/pci-direct.h> 16 - #include "pci.h" 17 16 18 17 static struct ioapic_scope ir_ioapic[MAX_IO_APICS]; 19 18 static struct hpet_scope ir_hpet[MAX_HPET_TBS];
drivers/pci/intr_remapping.h drivers/iommu/intr_remapping.h
drivers/pci/iova.c drivers/iommu/iova.c
-2
drivers/pci/pci.h
··· 184 184 return NULL; 185 185 } 186 186 187 - struct pci_dev *pci_find_upstream_pcie_bridge(struct pci_dev *pdev); 188 - 189 187 /* PCI slot sysfs helper code */ 190 188 #define to_pci_slot(s) container_of(s, struct pci_slot, kobj) 191 189
+2
include/linux/iommu.h
··· 19 19 #ifndef __LINUX_IOMMU_H 20 20 #define __LINUX_IOMMU_H 21 21 22 + #include <linux/errno.h> 23 + 22 24 #define IOMMU_READ (1) 23 25 #define IOMMU_WRITE (2) 24 26 #define IOMMU_CACHE (4) /* DMA cache coherency */
+11
include/linux/pci.h
··· 1589 1589 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off, 1590 1590 unsigned int len, const char *kw); 1591 1591 1592 + /** 1593 + * pci_find_upstream_pcie_bridge - find upstream PCIe-to-PCI bridge of a device 1594 + * @pdev: the PCI device 1595 + * 1596 + * if the device is PCIE, return NULL 1597 + * if the device isn't connected to a PCIe bridge (that is its parent is a 1598 + * legacy PCI bridge and the bridge is directly connected to bus 0), return its 1599 + * parent 1600 + */ 1601 + struct pci_dev *pci_find_upstream_pcie_bridge(struct pci_dev *pdev); 1602 + 1592 1603 #endif /* __KERNEL__ */ 1593 1604 #endif /* LINUX_PCI_H */