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

Merge git://git.infradead.org/iommu-2.6

* git://git.infradead.org/iommu-2.6:
intel-iommu: Fix address wrap on 32-bit kernel.
intel-iommu: Enable DMAR on 32-bit kernel.
intel-iommu: fix PCI device detach from virtual machine
intel-iommu: VT-d page table to support snooping control bit
iommu: Add domain_has_cap iommu_ops
intel-iommu: Snooping control support

Fixed trivial conflicts in arch/x86/Kconfig and drivers/pci/intel-iommu.c

+127 -22
+2 -2
arch/x86/Kconfig
··· 1837 1837 1838 1838 config DMAR 1839 1839 bool "Support for DMA Remapping Devices (EXPERIMENTAL)" 1840 - depends on X86_64 && PCI_MSI && ACPI && EXPERIMENTAL 1841 - ---help--- 1840 + depends on PCI_MSI && ACPI && EXPERIMENTAL 1841 + help 1842 1842 DMA remapping (DMAR) devices support enables independent address 1843 1843 translations for Direct Memory Access (DMA) from devices. 1844 1844 These DMA remapping devices are reported via ACPI tables
+7
arch/x86/kernel/amd_iommu.c
··· 1928 1928 return paddr; 1929 1929 } 1930 1930 1931 + static int amd_iommu_domain_has_cap(struct iommu_domain *domain, 1932 + unsigned long cap) 1933 + { 1934 + return 0; 1935 + } 1936 + 1931 1937 static struct iommu_ops amd_iommu_ops = { 1932 1938 .domain_init = amd_iommu_domain_init, 1933 1939 .domain_destroy = amd_iommu_domain_destroy, ··· 1942 1936 .map = amd_iommu_map_range, 1943 1937 .unmap = amd_iommu_unmap_range, 1944 1938 .iova_to_phys = amd_iommu_iova_to_phys, 1939 + .domain_has_cap = amd_iommu_domain_has_cap, 1945 1940 }; 1946 1941
+7
drivers/base/iommu.c
··· 98 98 return iommu_ops->iova_to_phys(domain, iova); 99 99 } 100 100 EXPORT_SYMBOL_GPL(iommu_iova_to_phys); 101 + 102 + int iommu_domain_has_cap(struct iommu_domain *domain, 103 + unsigned long cap) 104 + { 105 + return iommu_ops->domain_has_cap(domain, cap); 106 + } 107 + EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
+96 -19
drivers/pci/intel-iommu.c
··· 164 164 * 1: writable 165 165 * 2-6: reserved 166 166 * 7: super page 167 - * 8-11: available 167 + * 8-10: available 168 + * 11: snoop behavior 168 169 * 12-63: Host physcial address 169 170 */ 170 171 struct dma_pte { ··· 185 184 static inline void dma_set_pte_writable(struct dma_pte *pte) 186 185 { 187 186 pte->val |= DMA_PTE_WRITE; 187 + } 188 + 189 + static inline void dma_set_pte_snp(struct dma_pte *pte) 190 + { 191 + pte->val |= DMA_PTE_SNP; 188 192 } 189 193 190 194 static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot) ··· 237 231 int flags; /* flags to find out type of domain */ 238 232 239 233 int iommu_coherency;/* indicate coherency of iommu access */ 234 + int iommu_snooping; /* indicate snooping control feature*/ 240 235 int iommu_count; /* reference count of iommu */ 241 236 spinlock_t iommu_lock; /* protect iommu set in domain */ 242 237 u64 max_addr; /* maximum mapped address */ ··· 428 421 return g_iommus[iommu_id]; 429 422 } 430 423 431 - /* "Coherency" capability may be different across iommus */ 432 424 static void domain_update_iommu_coherency(struct dmar_domain *domain) 433 425 { 434 426 int i; ··· 442 436 } 443 437 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1); 444 438 } 439 + } 440 + 441 + static void domain_update_iommu_snooping(struct dmar_domain *domain) 442 + { 443 + int i; 444 + 445 + domain->iommu_snooping = 1; 446 + 447 + i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus); 448 + for (; i < g_num_of_iommus; ) { 449 + if (!ecap_sc_support(g_iommus[i]->ecap)) { 450 + domain->iommu_snooping = 0; 451 + break; 452 + } 453 + i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1); 454 + } 455 + } 456 + 457 + /* Some capabilities may be different across iommus */ 458 + static void domain_update_iommu_cap(struct dmar_domain *domain) 459 + { 460 + domain_update_iommu_coherency(domain); 461 + domain_update_iommu_snooping(domain); 445 462 } 446 463 447 464 static struct intel_iommu *device_to_iommu(u8 bus, u8 devfn) ··· 718 689 static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end) 719 690 { 720 691 int addr_width = agaw_to_width(domain->agaw); 692 + int npages; 721 693 722 694 start &= (((u64)1) << addr_width) - 1; 723 695 end &= (((u64)1) << addr_width) - 1; 724 696 /* in case it's partial page */ 725 697 start = PAGE_ALIGN(start); 726 698 end &= PAGE_MASK; 699 + npages = (end - start) / VTD_PAGE_SIZE; 727 700 728 701 /* we don't need lock here, nobody else touches the iova range */ 729 - while (start < end) { 702 + while (npages--) { 730 703 dma_pte_clear_one(domain, start); 731 704 start += VTD_PAGE_SIZE; 732 705 } ··· 1272 1241 else 1273 1242 domain->iommu_coherency = 0; 1274 1243 1244 + if (ecap_sc_support(iommu->ecap)) 1245 + domain->iommu_snooping = 1; 1246 + else 1247 + domain->iommu_snooping = 0; 1248 + 1275 1249 domain->iommu_count = 1; 1276 1250 1277 1251 /* always allocate the top pgd */ ··· 1405 1369 spin_lock_irqsave(&domain->iommu_lock, flags); 1406 1370 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) { 1407 1371 domain->iommu_count++; 1408 - domain_update_iommu_coherency(domain); 1372 + domain_update_iommu_cap(domain); 1409 1373 } 1410 1374 spin_unlock_irqrestore(&domain->iommu_lock, flags); 1411 1375 return 0; ··· 1505 1469 BUG_ON(dma_pte_addr(pte)); 1506 1470 dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT); 1507 1471 dma_set_pte_prot(pte, prot); 1472 + if (prot & DMA_PTE_SNP) 1473 + dma_set_pte_snp(pte); 1508 1474 domain_flush_cache(domain, pte, sizeof(*pte)); 1509 1475 start_pfn++; 1510 1476 index++; ··· 2157 2119 error: 2158 2120 if (iova) 2159 2121 __free_iova(&domain->iovad, iova); 2160 - printk(KERN_ERR"Device %s request: %lx@%llx dir %d --- failed\n", 2122 + printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n", 2161 2123 pci_name(pdev), size, (unsigned long long)paddr, dir); 2162 2124 return 0; 2163 2125 } ··· 2256 2218 start_addr = iova->pfn_lo << PAGE_SHIFT; 2257 2219 size = aligned_size((u64)dev_addr, size); 2258 2220 2259 - pr_debug("Device %s unmapping: %lx@%llx\n", 2221 + pr_debug("Device %s unmapping: %zx@%llx\n", 2260 2222 pci_name(pdev), size, (unsigned long long)start_addr); 2261 2223 2262 2224 /* clear the whole page */ ··· 2320 2282 free_pages((unsigned long)vaddr, order); 2321 2283 } 2322 2284 2323 - #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg))) 2324 - 2325 2285 static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist, 2326 2286 int nelems, enum dma_data_direction dir, 2327 2287 struct dma_attrs *attrs) ··· 2330 2294 unsigned long start_addr; 2331 2295 struct iova *iova; 2332 2296 size_t size = 0; 2333 - void *addr; 2297 + phys_addr_t addr; 2334 2298 struct scatterlist *sg; 2335 2299 struct intel_iommu *iommu; 2336 2300 ··· 2346 2310 if (!iova) 2347 2311 return; 2348 2312 for_each_sg(sglist, sg, nelems, i) { 2349 - addr = SG_ENT_VIRT_ADDRESS(sg); 2313 + addr = page_to_phys(sg_page(sg)) + sg->offset; 2350 2314 size += aligned_size((u64)addr, sg->length); 2351 2315 } 2352 2316 ··· 2373 2337 2374 2338 for_each_sg(sglist, sg, nelems, i) { 2375 2339 BUG_ON(!sg_page(sg)); 2376 - sg->dma_address = virt_to_bus(SG_ENT_VIRT_ADDRESS(sg)); 2340 + sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset; 2377 2341 sg->dma_length = sg->length; 2378 2342 } 2379 2343 return nelems; ··· 2382 2346 static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems, 2383 2347 enum dma_data_direction dir, struct dma_attrs *attrs) 2384 2348 { 2385 - void *addr; 2349 + phys_addr_t addr; 2386 2350 int i; 2387 2351 struct pci_dev *pdev = to_pci_dev(hwdev); 2388 2352 struct dmar_domain *domain; ··· 2406 2370 iommu = domain_get_iommu(domain); 2407 2371 2408 2372 for_each_sg(sglist, sg, nelems, i) { 2409 - addr = SG_ENT_VIRT_ADDRESS(sg); 2410 - addr = (void *)virt_to_phys(addr); 2373 + addr = page_to_phys(sg_page(sg)) + sg->offset; 2411 2374 size += aligned_size((u64)addr, sg->length); 2412 2375 } 2413 2376 ··· 2429 2394 start_addr = iova->pfn_lo << PAGE_SHIFT; 2430 2395 offset = 0; 2431 2396 for_each_sg(sglist, sg, nelems, i) { 2432 - addr = SG_ENT_VIRT_ADDRESS(sg); 2433 - addr = (void *)virt_to_phys(addr); 2397 + addr = page_to_phys(sg_page(sg)) + sg->offset; 2434 2398 size = aligned_size((u64)addr, sg->length); 2435 2399 ret = domain_page_mapping(domain, start_addr + offset, 2436 2400 ((u64)addr) & PAGE_MASK, ··· 2662 2628 return 0; 2663 2629 } 2664 2630 2631 + static void iommu_detach_dependent_devices(struct intel_iommu *iommu, 2632 + struct pci_dev *pdev) 2633 + { 2634 + struct pci_dev *tmp, *parent; 2635 + 2636 + if (!iommu || !pdev) 2637 + return; 2638 + 2639 + /* dependent device detach */ 2640 + tmp = pci_find_upstream_pcie_bridge(pdev); 2641 + /* Secondary interface's bus number and devfn 0 */ 2642 + if (tmp) { 2643 + parent = pdev->bus->self; 2644 + while (parent != tmp) { 2645 + iommu_detach_dev(iommu, parent->bus->number, 2646 + parent->devfn); 2647 + parent = parent->bus->self; 2648 + } 2649 + if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */ 2650 + iommu_detach_dev(iommu, 2651 + tmp->subordinate->number, 0); 2652 + else /* this is a legacy PCI bridge */ 2653 + iommu_detach_dev(iommu, 2654 + tmp->bus->number, tmp->devfn); 2655 + } 2656 + } 2657 + 2665 2658 static void vm_domain_remove_one_dev_info(struct dmar_domain *domain, 2666 2659 struct pci_dev *pdev) 2667 2660 { ··· 2714 2653 spin_unlock_irqrestore(&device_domain_lock, flags); 2715 2654 2716 2655 iommu_detach_dev(iommu, info->bus, info->devfn); 2656 + iommu_detach_dependent_devices(iommu, pdev); 2717 2657 free_devinfo_mem(info); 2718 2658 2719 2659 spin_lock_irqsave(&device_domain_lock, flags); ··· 2738 2676 spin_lock_irqsave(&domain->iommu_lock, tmp_flags); 2739 2677 clear_bit(iommu->seq_id, &domain->iommu_bmp); 2740 2678 domain->iommu_count--; 2741 - domain_update_iommu_coherency(domain); 2679 + domain_update_iommu_cap(domain); 2742 2680 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags); 2743 2681 } 2744 2682 ··· 2764 2702 2765 2703 iommu = device_to_iommu(info->bus, info->devfn); 2766 2704 iommu_detach_dev(iommu, info->bus, info->devfn); 2705 + iommu_detach_dependent_devices(iommu, info->dev); 2767 2706 2768 2707 /* clear this iommu in iommu_bmp, update iommu count 2769 - * and coherency 2708 + * and capabilities 2770 2709 */ 2771 2710 spin_lock_irqsave(&domain->iommu_lock, flags2); 2772 2711 if (test_and_clear_bit(iommu->seq_id, 2773 2712 &domain->iommu_bmp)) { 2774 2713 domain->iommu_count--; 2775 - domain_update_iommu_coherency(domain); 2714 + domain_update_iommu_cap(domain); 2776 2715 } 2777 2716 spin_unlock_irqrestore(&domain->iommu_lock, flags2); 2778 2717 ··· 2996 2933 prot |= DMA_PTE_READ; 2997 2934 if (iommu_prot & IOMMU_WRITE) 2998 2935 prot |= DMA_PTE_WRITE; 2936 + if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping) 2937 + prot |= DMA_PTE_SNP; 2999 2938 3000 2939 max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size); 3001 2940 if (dmar_domain->max_addr < max_addr) { ··· 3051 2986 return phys; 3052 2987 } 3053 2988 2989 + static int intel_iommu_domain_has_cap(struct iommu_domain *domain, 2990 + unsigned long cap) 2991 + { 2992 + struct dmar_domain *dmar_domain = domain->priv; 2993 + 2994 + if (cap == IOMMU_CAP_CACHE_COHERENCY) 2995 + return dmar_domain->iommu_snooping; 2996 + 2997 + return 0; 2998 + } 2999 + 3054 3000 static struct iommu_ops intel_iommu_ops = { 3055 3001 .domain_init = intel_iommu_domain_init, 3056 3002 .domain_destroy = intel_iommu_domain_destroy, ··· 3070 2994 .map = intel_iommu_map_range, 3071 2995 .unmap = intel_iommu_unmap_range, 3072 2996 .iova_to_phys = intel_iommu_iova_to_phys, 2997 + .domain_has_cap = intel_iommu_domain_has_cap, 3073 2998 }; 3074 2999 3075 3000 static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
+1
include/linux/dma_remapping.h
··· 11 11 12 12 #define DMA_PTE_READ (1) 13 13 #define DMA_PTE_WRITE (2) 14 + #define DMA_PTE_SNP (1 << 11) 14 15 15 16 struct intel_iommu; 16 17 struct dmar_domain;
+1 -1
include/linux/intel-iommu.h
··· 123 123 #define ecap_eim_support(e) ((e >> 4) & 0x1) 124 124 #define ecap_ir_support(e) ((e >> 3) & 0x1) 125 125 #define ecap_max_handle_mask(e) ((e >> 20) & 0xf) 126 - 126 + #define ecap_sc_support(e) ((e >> 7) & 0x1) /* Snooping Control */ 127 127 128 128 /* IOTLB_REG */ 129 129 #define DMA_TLB_FLUSH_GRANU_OFFSET 60
+13
include/linux/iommu.h
··· 21 21 22 22 #define IOMMU_READ (1) 23 23 #define IOMMU_WRITE (2) 24 + #define IOMMU_CACHE (4) /* DMA cache coherency */ 24 25 25 26 struct device; 26 27 27 28 struct iommu_domain { 28 29 void *priv; 29 30 }; 31 + 32 + #define IOMMU_CAP_CACHE_COHERENCY 0x1 30 33 31 34 struct iommu_ops { 32 35 int (*domain_init)(struct iommu_domain *domain); ··· 42 39 size_t size); 43 40 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, 44 41 unsigned long iova); 42 + int (*domain_has_cap)(struct iommu_domain *domain, 43 + unsigned long cap); 45 44 }; 46 45 47 46 #ifdef CONFIG_IOMMU_API ··· 62 57 size_t size); 63 58 extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, 64 59 unsigned long iova); 60 + extern int iommu_domain_has_cap(struct iommu_domain *domain, 61 + unsigned long cap); 65 62 66 63 #else /* CONFIG_IOMMU_API */ 67 64 ··· 110 103 111 104 static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, 112 105 unsigned long iova) 106 + { 107 + return 0; 108 + } 109 + 110 + static inline int domain_has_cap(struct iommu_domain *domain, 111 + unsigned long cap) 113 112 { 114 113 return 0; 115 114 }