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

Merge commit 'tip/iommu-for-powerpc' into next

+292 -801
+18 -1
arch/ia64/include/asm/dma-mapping.h
··· 44 44 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h) 45 45 46 46 #define get_dma_ops(dev) platform_dma_get_ops(dev) 47 - #define flush_write_buffers() 48 47 49 48 #include <asm-generic/dma-mapping-common.h> 50 49 ··· 66 67 return -EIO; 67 68 *dev->dma_mask = mask; 68 69 return 0; 70 + } 71 + 72 + static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) 73 + { 74 + if (!dev->dma_mask) 75 + return 0; 76 + 77 + return addr + size <= *dev->dma_mask; 78 + } 79 + 80 + static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 81 + { 82 + return paddr; 83 + } 84 + 85 + static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 86 + { 87 + return daddr; 69 88 } 70 89 71 90 extern int dma_get_cache_alignment(void);
+23
arch/powerpc/include/asm/dma-mapping.h
··· 424 424 #endif 425 425 } 426 426 427 + static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) 428 + { 429 + struct dma_mapping_ops *ops = get_dma_ops(dev); 430 + 431 + if (ops->addr_needs_map && ops->addr_needs_map(dev, addr, size)) 432 + return 0; 433 + 434 + if (!dev->dma_mask) 435 + return 0; 436 + 437 + return addr + size <= *dev->dma_mask; 438 + } 439 + 440 + static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 441 + { 442 + return paddr + get_dma_direct_offset(dev); 443 + } 444 + 445 + static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 446 + { 447 + return daddr - get_dma_direct_offset(dev); 448 + } 449 + 427 450 #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f) 428 451 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h) 429 452 #ifdef CONFIG_NOT_COHERENT_CACHE
+1 -47
arch/powerpc/kernel/dma-swiotlb.c
··· 24 24 int swiotlb __read_mostly; 25 25 unsigned int ppc_swiotlb_enable; 26 26 27 - void *swiotlb_bus_to_virt(struct device *hwdev, dma_addr_t addr) 28 - { 29 - unsigned long pfn = PFN_DOWN(swiotlb_bus_to_phys(hwdev, addr)); 30 - void *pageaddr = page_address(pfn_to_page(pfn)); 31 - 32 - if (pageaddr != NULL) 33 - return pageaddr + (addr % PAGE_SIZE); 34 - return NULL; 35 - } 36 - 37 - dma_addr_t swiotlb_phys_to_bus(struct device *hwdev, phys_addr_t paddr) 38 - { 39 - return paddr + get_dma_direct_offset(hwdev); 40 - } 41 - 42 - phys_addr_t swiotlb_bus_to_phys(struct device *hwdev, dma_addr_t baddr) 43 - 44 - { 45 - return baddr - get_dma_direct_offset(hwdev); 46 - } 47 - 48 - /* 49 - * Determine if an address needs bounce buffering via swiotlb. 50 - * Going forward I expect the swiotlb code to generalize on using 51 - * a dma_ops->addr_needs_map, and this function will move from here to the 52 - * generic swiotlb code. 53 - */ 54 - int 55 - swiotlb_arch_address_needs_mapping(struct device *hwdev, dma_addr_t addr, 56 - size_t size) 57 - { 58 - struct dma_mapping_ops *dma_ops = get_dma_ops(hwdev); 59 - 60 - BUG_ON(!dma_ops); 61 - return dma_ops->addr_needs_map(hwdev, addr, size); 62 - } 63 - 64 27 /* 65 28 * Determine if an address is reachable by a pci device, or if we must bounce. 66 29 */ 67 30 static int 68 31 swiotlb_pci_addr_needs_map(struct device *hwdev, dma_addr_t addr, size_t size) 69 32 { 70 - u64 mask = dma_get_mask(hwdev); 71 33 dma_addr_t max; 72 34 struct pci_controller *hose; 73 35 struct pci_dev *pdev = to_pci_dev(hwdev); ··· 41 79 if ((addr + size > max) | (addr < hose->dma_window_base_cur)) 42 80 return 1; 43 81 44 - return !is_buffer_dma_capable(mask, addr, size); 82 + return 0; 45 83 } 46 - 47 - static int 48 - swiotlb_addr_needs_map(struct device *hwdev, dma_addr_t addr, size_t size) 49 - { 50 - return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size); 51 - } 52 - 53 84 54 85 /* 55 86 * At the moment, all platforms that use this code only require ··· 59 104 .dma_supported = swiotlb_dma_supported, 60 105 .map_page = swiotlb_map_page, 61 106 .unmap_page = swiotlb_unmap_page, 62 - .addr_needs_map = swiotlb_addr_needs_map, 63 107 .sync_single_range_for_cpu = swiotlb_sync_single_range_for_cpu, 64 108 .sync_single_range_for_device = swiotlb_sync_single_range_for_device, 65 109 .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
+2
arch/sparc/Kconfig
··· 25 25 select ARCH_WANT_OPTIONAL_GPIOLIB 26 26 select RTC_CLASS 27 27 select RTC_DRV_M48T59 28 + select HAVE_DMA_ATTRS 29 + select HAVE_DMA_API_DEBUG 28 30 29 31 config SPARC32 30 32 def_bool !64BIT
+23 -124
arch/sparc/include/asm/dma-mapping.h
··· 3 3 4 4 #include <linux/scatterlist.h> 5 5 #include <linux/mm.h> 6 + #include <linux/dma-debug.h> 6 7 7 8 #define DMA_ERROR_CODE (~(dma_addr_t)0x0) 8 9 ··· 14 13 #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h) 15 14 #define dma_is_consistent(d, h) (1) 16 15 17 - struct dma_ops { 18 - void *(*alloc_coherent)(struct device *dev, size_t size, 19 - dma_addr_t *dma_handle, gfp_t flag); 20 - void (*free_coherent)(struct device *dev, size_t size, 21 - void *cpu_addr, dma_addr_t dma_handle); 22 - dma_addr_t (*map_page)(struct device *dev, struct page *page, 23 - unsigned long offset, size_t size, 24 - enum dma_data_direction direction); 25 - void (*unmap_page)(struct device *dev, dma_addr_t dma_addr, 26 - size_t size, 27 - enum dma_data_direction direction); 28 - int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents, 29 - enum dma_data_direction direction); 30 - void (*unmap_sg)(struct device *dev, struct scatterlist *sg, 31 - int nhwentries, 32 - enum dma_data_direction direction); 33 - void (*sync_single_for_cpu)(struct device *dev, 34 - dma_addr_t dma_handle, size_t size, 35 - enum dma_data_direction direction); 36 - void (*sync_single_for_device)(struct device *dev, 37 - dma_addr_t dma_handle, size_t size, 38 - enum dma_data_direction direction); 39 - void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, 40 - int nelems, 41 - enum dma_data_direction direction); 42 - void (*sync_sg_for_device)(struct device *dev, 43 - struct scatterlist *sg, int nents, 44 - enum dma_data_direction dir); 45 - }; 46 - extern const struct dma_ops *dma_ops; 16 + extern struct dma_map_ops *dma_ops, pci32_dma_ops; 17 + extern struct bus_type pci_bus_type; 18 + 19 + static inline struct dma_map_ops *get_dma_ops(struct device *dev) 20 + { 21 + #if defined(CONFIG_SPARC32) && defined(CONFIG_PCI) 22 + if (dev->bus == &pci_bus_type) 23 + return &pci32_dma_ops; 24 + #endif 25 + return dma_ops; 26 + } 27 + 28 + #include <asm-generic/dma-mapping-common.h> 47 29 48 30 static inline void *dma_alloc_coherent(struct device *dev, size_t size, 49 31 dma_addr_t *dma_handle, gfp_t flag) 50 32 { 51 - return dma_ops->alloc_coherent(dev, size, dma_handle, flag); 33 + struct dma_map_ops *ops = get_dma_ops(dev); 34 + void *cpu_addr; 35 + 36 + cpu_addr = ops->alloc_coherent(dev, size, dma_handle, flag); 37 + debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr); 38 + return cpu_addr; 52 39 } 53 40 54 41 static inline void dma_free_coherent(struct device *dev, size_t size, 55 42 void *cpu_addr, dma_addr_t dma_handle) 56 43 { 57 - dma_ops->free_coherent(dev, size, cpu_addr, dma_handle); 58 - } 44 + struct dma_map_ops *ops = get_dma_ops(dev); 59 45 60 - static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr, 61 - size_t size, 62 - enum dma_data_direction direction) 63 - { 64 - return dma_ops->map_page(dev, virt_to_page(cpu_addr), 65 - (unsigned long)cpu_addr & ~PAGE_MASK, size, 66 - direction); 46 + debug_dma_free_coherent(dev, size, cpu_addr, dma_handle); 47 + ops->free_coherent(dev, size, cpu_addr, dma_handle); 67 48 } 68 - 69 - static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, 70 - size_t size, 71 - enum dma_data_direction direction) 72 - { 73 - dma_ops->unmap_page(dev, dma_addr, size, direction); 74 - } 75 - 76 - static inline dma_addr_t dma_map_page(struct device *dev, struct page *page, 77 - unsigned long offset, size_t size, 78 - enum dma_data_direction direction) 79 - { 80 - return dma_ops->map_page(dev, page, offset, size, direction); 81 - } 82 - 83 - static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address, 84 - size_t size, 85 - enum dma_data_direction direction) 86 - { 87 - dma_ops->unmap_page(dev, dma_address, size, direction); 88 - } 89 - 90 - static inline int dma_map_sg(struct device *dev, struct scatterlist *sg, 91 - int nents, enum dma_data_direction direction) 92 - { 93 - return dma_ops->map_sg(dev, sg, nents, direction); 94 - } 95 - 96 - static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg, 97 - int nents, enum dma_data_direction direction) 98 - { 99 - dma_ops->unmap_sg(dev, sg, nents, direction); 100 - } 101 - 102 - static inline void dma_sync_single_for_cpu(struct device *dev, 103 - dma_addr_t dma_handle, size_t size, 104 - enum dma_data_direction direction) 105 - { 106 - dma_ops->sync_single_for_cpu(dev, dma_handle, size, direction); 107 - } 108 - 109 - static inline void dma_sync_single_for_device(struct device *dev, 110 - dma_addr_t dma_handle, 111 - size_t size, 112 - enum dma_data_direction direction) 113 - { 114 - if (dma_ops->sync_single_for_device) 115 - dma_ops->sync_single_for_device(dev, dma_handle, size, 116 - direction); 117 - } 118 - 119 - static inline void dma_sync_sg_for_cpu(struct device *dev, 120 - struct scatterlist *sg, int nelems, 121 - enum dma_data_direction direction) 122 - { 123 - dma_ops->sync_sg_for_cpu(dev, sg, nelems, direction); 124 - } 125 - 126 - static inline void dma_sync_sg_for_device(struct device *dev, 127 - struct scatterlist *sg, int nelems, 128 - enum dma_data_direction direction) 129 - { 130 - if (dma_ops->sync_sg_for_device) 131 - dma_ops->sync_sg_for_device(dev, sg, nelems, direction); 132 - } 133 - 134 - static inline void dma_sync_single_range_for_cpu(struct device *dev, 135 - dma_addr_t dma_handle, 136 - unsigned long offset, 137 - size_t size, 138 - enum dma_data_direction dir) 139 - { 140 - dma_sync_single_for_cpu(dev, dma_handle+offset, size, dir); 141 - } 142 - 143 - static inline void dma_sync_single_range_for_device(struct device *dev, 144 - dma_addr_t dma_handle, 145 - unsigned long offset, 146 - size_t size, 147 - enum dma_data_direction dir) 148 - { 149 - dma_sync_single_for_device(dev, dma_handle+offset, size, dir); 150 - } 151 - 152 49 153 50 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 154 51 {
+3
arch/sparc/include/asm/pci.h
··· 5 5 #else 6 6 #include <asm/pci_32.h> 7 7 #endif 8 + 9 + #include <asm-generic/pci-dma-compat.h> 10 + 8 11 #endif
-105
arch/sparc/include/asm/pci_32.h
··· 31 31 */ 32 32 #define PCI_DMA_BUS_IS_PHYS (0) 33 33 34 - #include <asm/scatterlist.h> 35 - 36 34 struct pci_dev; 37 - 38 - /* Allocate and map kernel buffer using consistent mode DMA for a device. 39 - * hwdev should be valid struct pci_dev pointer for PCI devices. 40 - */ 41 - extern void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size, dma_addr_t *dma_handle); 42 - 43 - /* Free and unmap a consistent DMA buffer. 44 - * cpu_addr is what was returned from pci_alloc_consistent, 45 - * size must be the same as what as passed into pci_alloc_consistent, 46 - * and likewise dma_addr must be the same as what *dma_addrp was set to. 47 - * 48 - * References to the memory and mappings assosciated with cpu_addr/dma_addr 49 - * past this call are illegal. 50 - */ 51 - extern void pci_free_consistent(struct pci_dev *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle); 52 - 53 - /* Map a single buffer of the indicated size for DMA in streaming mode. 54 - * The 32-bit bus address to use is returned. 55 - * 56 - * Once the device is given the dma address, the device owns this memory 57 - * until either pci_unmap_single or pci_dma_sync_single_for_cpu is performed. 58 - */ 59 - extern dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size, int direction); 60 - 61 - /* Unmap a single streaming mode DMA translation. The dma_addr and size 62 - * must match what was provided for in a previous pci_map_single call. All 63 - * other usages are undefined. 64 - * 65 - * After this call, reads by the cpu to the buffer are guaranteed to see 66 - * whatever the device wrote there. 67 - */ 68 - extern void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t dma_addr, size_t size, int direction); 69 35 70 36 /* pci_unmap_{single,page} is not a nop, thus... */ 71 37 #define DECLARE_PCI_UNMAP_ADDR(ADDR_NAME) \ ··· 47 81 #define pci_unmap_len_set(PTR, LEN_NAME, VAL) \ 48 82 (((PTR)->LEN_NAME) = (VAL)) 49 83 50 - /* 51 - * Same as above, only with pages instead of mapped addresses. 52 - */ 53 - extern dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page, 54 - unsigned long offset, size_t size, int direction); 55 - extern void pci_unmap_page(struct pci_dev *hwdev, 56 - dma_addr_t dma_address, size_t size, int direction); 57 - 58 - /* Map a set of buffers described by scatterlist in streaming 59 - * mode for DMA. This is the scather-gather version of the 60 - * above pci_map_single interface. Here the scatter gather list 61 - * elements are each tagged with the appropriate dma address 62 - * and length. They are obtained via sg_dma_{address,length}(SG). 63 - * 64 - * NOTE: An implementation may be able to use a smaller number of 65 - * DMA address/length pairs than there are SG table elements. 66 - * (for example via virtual mapping capabilities) 67 - * The routine returns the number of addr/length pairs actually 68 - * used, at most nents. 69 - * 70 - * Device ownership issues as mentioned above for pci_map_single are 71 - * the same here. 72 - */ 73 - extern int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction); 74 - 75 - /* Unmap a set of streaming mode DMA translations. 76 - * Again, cpu read rules concerning calls here are the same as for 77 - * pci_unmap_single() above. 78 - */ 79 - extern void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nhwents, int direction); 80 - 81 - /* Make physical memory consistent for a single 82 - * streaming mode DMA translation after a transfer. 83 - * 84 - * If you perform a pci_map_single() but wish to interrogate the 85 - * buffer using the cpu, yet do not wish to teardown the PCI dma 86 - * mapping, you must call this function before doing so. At the 87 - * next point you give the PCI dma address back to the card, you 88 - * must first perform a pci_dma_sync_for_device, and then the device 89 - * again owns the buffer. 90 - */ 91 - extern void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t dma_handle, size_t size, int direction); 92 - extern void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t dma_handle, size_t size, int direction); 93 - 94 - /* Make physical memory consistent for a set of streaming 95 - * mode DMA translations after a transfer. 96 - * 97 - * The same as pci_dma_sync_single_* but for a scatter-gather list, 98 - * same rules and usage. 99 - */ 100 - extern void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg, int nelems, int direction); 101 - extern void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nelems, int direction); 102 - 103 - /* Return whether the given PCI device DMA address mask can 104 - * be supported properly. For example, if your device can 105 - * only drive the low 24-bits during PCI bus mastering, then 106 - * you would pass 0x00ffffff as the mask to this function. 107 - */ 108 - static inline int pci_dma_supported(struct pci_dev *hwdev, u64 mask) 109 - { 110 - return 1; 111 - } 112 - 113 84 #ifdef CONFIG_PCI 114 85 static inline void pci_dma_burst_advice(struct pci_dev *pdev, 115 86 enum pci_dma_burst_strategy *strat, ··· 56 153 *strategy_parameter = ~0UL; 57 154 } 58 155 #endif 59 - 60 - #define PCI_DMA_ERROR_CODE (~(dma_addr_t)0x0) 61 - 62 - static inline int pci_dma_mapping_error(struct pci_dev *pdev, 63 - dma_addr_t dma_addr) 64 - { 65 - return (dma_addr == PCI_DMA_ERROR_CODE); 66 - } 67 156 68 157 struct device_node; 69 158 extern struct device_node *pci_device_to_OF_node(struct pci_dev *pdev);
-88
arch/sparc/include/asm/pci_64.h
··· 35 35 */ 36 36 #define PCI_DMA_BUS_IS_PHYS (0) 37 37 38 - static inline void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, 39 - dma_addr_t *dma_handle) 40 - { 41 - return dma_alloc_coherent(&pdev->dev, size, dma_handle, GFP_ATOMIC); 42 - } 43 - 44 - static inline void pci_free_consistent(struct pci_dev *pdev, size_t size, 45 - void *vaddr, dma_addr_t dma_handle) 46 - { 47 - return dma_free_coherent(&pdev->dev, size, vaddr, dma_handle); 48 - } 49 - 50 - static inline dma_addr_t pci_map_single(struct pci_dev *pdev, void *ptr, 51 - size_t size, int direction) 52 - { 53 - return dma_map_single(&pdev->dev, ptr, size, 54 - (enum dma_data_direction) direction); 55 - } 56 - 57 - static inline void pci_unmap_single(struct pci_dev *pdev, dma_addr_t dma_addr, 58 - size_t size, int direction) 59 - { 60 - dma_unmap_single(&pdev->dev, dma_addr, size, 61 - (enum dma_data_direction) direction); 62 - } 63 - 64 - #define pci_map_page(dev, page, off, size, dir) \ 65 - pci_map_single(dev, (page_address(page) + (off)), size, dir) 66 - #define pci_unmap_page(dev,addr,sz,dir) \ 67 - pci_unmap_single(dev,addr,sz,dir) 68 - 69 38 /* pci_unmap_{single,page} is not a nop, thus... */ 70 39 #define DECLARE_PCI_UNMAP_ADDR(ADDR_NAME) \ 71 40 dma_addr_t ADDR_NAME; ··· 49 80 #define pci_unmap_len_set(PTR, LEN_NAME, VAL) \ 50 81 (((PTR)->LEN_NAME) = (VAL)) 51 82 52 - static inline int pci_map_sg(struct pci_dev *pdev, struct scatterlist *sg, 53 - int nents, int direction) 54 - { 55 - return dma_map_sg(&pdev->dev, sg, nents, 56 - (enum dma_data_direction) direction); 57 - } 58 - 59 - static inline void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sg, 60 - int nents, int direction) 61 - { 62 - dma_unmap_sg(&pdev->dev, sg, nents, 63 - (enum dma_data_direction) direction); 64 - } 65 - 66 - static inline void pci_dma_sync_single_for_cpu(struct pci_dev *pdev, 67 - dma_addr_t dma_handle, 68 - size_t size, int direction) 69 - { 70 - dma_sync_single_for_cpu(&pdev->dev, dma_handle, size, 71 - (enum dma_data_direction) direction); 72 - } 73 - 74 - static inline void pci_dma_sync_single_for_device(struct pci_dev *pdev, 75 - dma_addr_t dma_handle, 76 - size_t size, int direction) 77 - { 78 - /* No flushing needed to sync cpu writes to the device. */ 79 - } 80 - 81 - static inline void pci_dma_sync_sg_for_cpu(struct pci_dev *pdev, 82 - struct scatterlist *sg, 83 - int nents, int direction) 84 - { 85 - dma_sync_sg_for_cpu(&pdev->dev, sg, nents, 86 - (enum dma_data_direction) direction); 87 - } 88 - 89 - static inline void pci_dma_sync_sg_for_device(struct pci_dev *pdev, 90 - struct scatterlist *sg, 91 - int nelems, int direction) 92 - { 93 - /* No flushing needed to sync cpu writes to the device. */ 94 - } 95 - 96 - /* Return whether the given PCI device DMA address mask can 97 - * be supported properly. For example, if your device can 98 - * only drive the low 24-bits during PCI bus mastering, then 99 - * you would pass 0x00ffffff as the mask to this function. 100 - */ 101 - extern int pci_dma_supported(struct pci_dev *hwdev, u64 mask); 102 - 103 83 /* PCI IOMMU mapping bypass support. */ 104 84 105 85 /* PCI 64-bit addressing works for all slots on all controller ··· 57 139 */ 58 140 #define PCI64_REQUIRED_MASK (~(dma64_addr_t)0) 59 141 #define PCI64_ADDR_BASE 0xfffc000000000000UL 60 - 61 - static inline int pci_dma_mapping_error(struct pci_dev *pdev, 62 - dma_addr_t dma_addr) 63 - { 64 - return dma_mapping_error(&pdev->dev, dma_addr); 65 - } 66 142 67 143 #ifdef CONFIG_PCI 68 144 static inline void pci_dma_burst_advice(struct pci_dev *pdev,
+1 -1
arch/sparc/kernel/Makefile
··· 61 61 obj-$(CONFIG_SPARC32) += devres.o 62 62 devres-y := ../../../kernel/irq/devres.o 63 63 64 - obj-$(CONFIG_SPARC32) += dma.o 64 + obj-y += dma.o 65 65 66 66 obj-$(CONFIG_SPARC32_PCI) += pcic.o 67 67
+5 -170
arch/sparc/kernel/dma.c
··· 1 - /* dma.c: PCI and SBUS DMA accessors for 32-bit sparc. 2 - * 3 - * Copyright (C) 2008 David S. Miller <davem@davemloft.net> 4 - */ 5 - 6 1 #include <linux/kernel.h> 7 2 #include <linux/module.h> 8 3 #include <linux/dma-mapping.h> 9 - #include <linux/scatterlist.h> 10 - #include <linux/mm.h> 4 + #include <linux/dma-debug.h> 11 5 12 - #ifdef CONFIG_PCI 13 - #include <linux/pci.h> 14 - #endif 6 + #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 15) 15 7 16 - #include "dma.h" 17 - 18 - int dma_supported(struct device *dev, u64 mask) 8 + static int __init dma_init(void) 19 9 { 20 - #ifdef CONFIG_PCI 21 - if (dev->bus == &pci_bus_type) 22 - return pci_dma_supported(to_pci_dev(dev), mask); 23 - #endif 10 + dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); 24 11 return 0; 25 12 } 26 - EXPORT_SYMBOL(dma_supported); 27 - 28 - int dma_set_mask(struct device *dev, u64 dma_mask) 29 - { 30 - #ifdef CONFIG_PCI 31 - if (dev->bus == &pci_bus_type) 32 - return pci_set_dma_mask(to_pci_dev(dev), dma_mask); 33 - #endif 34 - return -EOPNOTSUPP; 35 - } 36 - EXPORT_SYMBOL(dma_set_mask); 37 - 38 - static void *dma32_alloc_coherent(struct device *dev, size_t size, 39 - dma_addr_t *dma_handle, gfp_t flag) 40 - { 41 - #ifdef CONFIG_PCI 42 - if (dev->bus == &pci_bus_type) 43 - return pci_alloc_consistent(to_pci_dev(dev), size, dma_handle); 44 - #endif 45 - return sbus_alloc_consistent(dev, size, dma_handle); 46 - } 47 - 48 - static void dma32_free_coherent(struct device *dev, size_t size, 49 - void *cpu_addr, dma_addr_t dma_handle) 50 - { 51 - #ifdef CONFIG_PCI 52 - if (dev->bus == &pci_bus_type) { 53 - pci_free_consistent(to_pci_dev(dev), size, 54 - cpu_addr, dma_handle); 55 - return; 56 - } 57 - #endif 58 - sbus_free_consistent(dev, size, cpu_addr, dma_handle); 59 - } 60 - 61 - static dma_addr_t dma32_map_page(struct device *dev, struct page *page, 62 - unsigned long offset, size_t size, 63 - enum dma_data_direction direction) 64 - { 65 - #ifdef CONFIG_PCI 66 - if (dev->bus == &pci_bus_type) 67 - return pci_map_page(to_pci_dev(dev), page, offset, 68 - size, (int)direction); 69 - #endif 70 - return sbus_map_single(dev, page_address(page) + offset, 71 - size, (int)direction); 72 - } 73 - 74 - static void dma32_unmap_page(struct device *dev, dma_addr_t dma_address, 75 - size_t size, enum dma_data_direction direction) 76 - { 77 - #ifdef CONFIG_PCI 78 - if (dev->bus == &pci_bus_type) { 79 - pci_unmap_page(to_pci_dev(dev), dma_address, 80 - size, (int)direction); 81 - return; 82 - } 83 - #endif 84 - sbus_unmap_single(dev, dma_address, size, (int)direction); 85 - } 86 - 87 - static int dma32_map_sg(struct device *dev, struct scatterlist *sg, 88 - int nents, enum dma_data_direction direction) 89 - { 90 - #ifdef CONFIG_PCI 91 - if (dev->bus == &pci_bus_type) 92 - return pci_map_sg(to_pci_dev(dev), sg, nents, (int)direction); 93 - #endif 94 - return sbus_map_sg(dev, sg, nents, direction); 95 - } 96 - 97 - void dma32_unmap_sg(struct device *dev, struct scatterlist *sg, 98 - int nents, enum dma_data_direction direction) 99 - { 100 - #ifdef CONFIG_PCI 101 - if (dev->bus == &pci_bus_type) { 102 - pci_unmap_sg(to_pci_dev(dev), sg, nents, (int)direction); 103 - return; 104 - } 105 - #endif 106 - sbus_unmap_sg(dev, sg, nents, (int)direction); 107 - } 108 - 109 - static void dma32_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, 110 - size_t size, 111 - enum dma_data_direction direction) 112 - { 113 - #ifdef CONFIG_PCI 114 - if (dev->bus == &pci_bus_type) { 115 - pci_dma_sync_single_for_cpu(to_pci_dev(dev), dma_handle, 116 - size, (int)direction); 117 - return; 118 - } 119 - #endif 120 - sbus_dma_sync_single_for_cpu(dev, dma_handle, size, (int) direction); 121 - } 122 - 123 - static void dma32_sync_single_for_device(struct device *dev, 124 - dma_addr_t dma_handle, size_t size, 125 - enum dma_data_direction direction) 126 - { 127 - #ifdef CONFIG_PCI 128 - if (dev->bus == &pci_bus_type) { 129 - pci_dma_sync_single_for_device(to_pci_dev(dev), dma_handle, 130 - size, (int)direction); 131 - return; 132 - } 133 - #endif 134 - sbus_dma_sync_single_for_device(dev, dma_handle, size, (int) direction); 135 - } 136 - 137 - static void dma32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, 138 - int nelems, enum dma_data_direction direction) 139 - { 140 - #ifdef CONFIG_PCI 141 - if (dev->bus == &pci_bus_type) { 142 - pci_dma_sync_sg_for_cpu(to_pci_dev(dev), sg, 143 - nelems, (int)direction); 144 - return; 145 - } 146 - #endif 147 - BUG(); 148 - } 149 - 150 - static void dma32_sync_sg_for_device(struct device *dev, 151 - struct scatterlist *sg, int nelems, 152 - enum dma_data_direction direction) 153 - { 154 - #ifdef CONFIG_PCI 155 - if (dev->bus == &pci_bus_type) { 156 - pci_dma_sync_sg_for_device(to_pci_dev(dev), sg, 157 - nelems, (int)direction); 158 - return; 159 - } 160 - #endif 161 - BUG(); 162 - } 163 - 164 - static const struct dma_ops dma32_dma_ops = { 165 - .alloc_coherent = dma32_alloc_coherent, 166 - .free_coherent = dma32_free_coherent, 167 - .map_page = dma32_map_page, 168 - .unmap_page = dma32_unmap_page, 169 - .map_sg = dma32_map_sg, 170 - .unmap_sg = dma32_unmap_sg, 171 - .sync_single_for_cpu = dma32_sync_single_for_cpu, 172 - .sync_single_for_device = dma32_sync_single_for_device, 173 - .sync_sg_for_cpu = dma32_sync_sg_for_cpu, 174 - .sync_sg_for_device = dma32_sync_sg_for_device, 175 - }; 176 - 177 - const struct dma_ops *dma_ops = &dma32_dma_ops; 178 - EXPORT_SYMBOL(dma_ops); 13 + fs_initcall(dma_init);
-14
arch/sparc/kernel/dma.h
··· 1 - void *sbus_alloc_consistent(struct device *dev, long len, u32 *dma_addrp); 2 - void sbus_free_consistent(struct device *dev, long n, void *p, u32 ba); 3 - dma_addr_t sbus_map_single(struct device *dev, void *va, 4 - size_t len, int direction); 5 - void sbus_unmap_single(struct device *dev, dma_addr_t ba, 6 - size_t n, int direction); 7 - int sbus_map_sg(struct device *dev, struct scatterlist *sg, 8 - int n, int direction); 9 - void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, 10 - int n, int direction); 11 - void sbus_dma_sync_single_for_cpu(struct device *dev, dma_addr_t ba, 12 - size_t size, int direction); 13 - void sbus_dma_sync_single_for_device(struct device *dev, dma_addr_t ba, 14 - size_t size, int direction);
+13 -7
arch/sparc/kernel/iommu.c
··· 353 353 354 354 static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page, 355 355 unsigned long offset, size_t sz, 356 - enum dma_data_direction direction) 356 + enum dma_data_direction direction, 357 + struct dma_attrs *attrs) 357 358 { 358 359 struct iommu *iommu; 359 360 struct strbuf *strbuf; ··· 475 474 } 476 475 477 476 static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr, 478 - size_t sz, enum dma_data_direction direction) 477 + size_t sz, enum dma_data_direction direction, 478 + struct dma_attrs *attrs) 479 479 { 480 480 struct iommu *iommu; 481 481 struct strbuf *strbuf; ··· 522 520 } 523 521 524 522 static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist, 525 - int nelems, enum dma_data_direction direction) 523 + int nelems, enum dma_data_direction direction, 524 + struct dma_attrs *attrs) 526 525 { 527 526 struct scatterlist *s, *outs, *segstart; 528 527 unsigned long flags, handle, prot, ctx; ··· 694 691 } 695 692 696 693 static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist, 697 - int nelems, enum dma_data_direction direction) 694 + int nelems, enum dma_data_direction direction, 695 + struct dma_attrs *attrs) 698 696 { 699 697 unsigned long flags, ctx; 700 698 struct scatterlist *sg; ··· 826 822 spin_unlock_irqrestore(&iommu->lock, flags); 827 823 } 828 824 829 - static const struct dma_ops sun4u_dma_ops = { 825 + static struct dma_map_ops sun4u_dma_ops = { 830 826 .alloc_coherent = dma_4u_alloc_coherent, 831 827 .free_coherent = dma_4u_free_coherent, 832 828 .map_page = dma_4u_map_page, ··· 837 833 .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu, 838 834 }; 839 835 840 - const struct dma_ops *dma_ops = &sun4u_dma_ops; 836 + struct dma_map_ops *dma_ops = &sun4u_dma_ops; 841 837 EXPORT_SYMBOL(dma_ops); 838 + 839 + extern int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask); 842 840 843 841 int dma_supported(struct device *dev, u64 device_mask) 844 842 { ··· 855 849 856 850 #ifdef CONFIG_PCI 857 851 if (dev->bus == &pci_bus_type) 858 - return pci_dma_supported(to_pci_dev(dev), device_mask); 852 + return pci64_dma_supported(to_pci_dev(dev), device_mask); 859 853 #endif 860 854 861 855 return 0;
+105 -85
arch/sparc/kernel/ioport.c
··· 48 48 #include <asm/iommu.h> 49 49 #include <asm/io-unit.h> 50 50 51 - #include "dma.h" 52 - 53 51 #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */ 54 52 55 53 static struct resource *_sparc_find_resource(struct resource *r, ··· 244 246 * Typically devices use them for control blocks. 245 247 * CPU may access them without any explicit flushing. 246 248 */ 247 - void *sbus_alloc_consistent(struct device *dev, long len, u32 *dma_addrp) 249 + static void *sbus_alloc_coherent(struct device *dev, size_t len, 250 + dma_addr_t *dma_addrp, gfp_t gfp) 248 251 { 249 252 struct of_device *op = to_of_device(dev); 250 253 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK; ··· 298 299 return NULL; 299 300 } 300 301 301 - void sbus_free_consistent(struct device *dev, long n, void *p, u32 ba) 302 + static void sbus_free_coherent(struct device *dev, size_t n, void *p, 303 + dma_addr_t ba) 302 304 { 303 305 struct resource *res; 304 306 struct page *pgv; ··· 317 317 318 318 n = (n + PAGE_SIZE-1) & PAGE_MASK; 319 319 if ((res->end-res->start)+1 != n) { 320 - printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n", 320 + printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n", 321 321 (long)((res->end-res->start)+1), n); 322 322 return; 323 323 } ··· 337 337 * CPU view of this memory may be inconsistent with 338 338 * a device view and explicit flushing is necessary. 339 339 */ 340 - dma_addr_t sbus_map_single(struct device *dev, void *va, size_t len, int direction) 340 + static dma_addr_t sbus_map_page(struct device *dev, struct page *page, 341 + unsigned long offset, size_t len, 342 + enum dma_data_direction dir, 343 + struct dma_attrs *attrs) 341 344 { 345 + void *va = page_address(page) + offset; 346 + 342 347 /* XXX why are some lengths signed, others unsigned? */ 343 348 if (len <= 0) { 344 349 return 0; ··· 355 350 return mmu_get_scsi_one(dev, va, len); 356 351 } 357 352 358 - void sbus_unmap_single(struct device *dev, dma_addr_t ba, size_t n, int direction) 353 + static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n, 354 + enum dma_data_direction dir, struct dma_attrs *attrs) 359 355 { 360 356 mmu_release_scsi_one(dev, ba, n); 361 357 } 362 358 363 - int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, int direction) 359 + static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, 360 + enum dma_data_direction dir, struct dma_attrs *attrs) 364 361 { 365 362 mmu_get_scsi_sgl(dev, sg, n); 366 363 ··· 373 366 return n; 374 367 } 375 368 376 - void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, int direction) 369 + static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, 370 + enum dma_data_direction dir, struct dma_attrs *attrs) 377 371 { 378 372 mmu_release_scsi_sgl(dev, sg, n); 379 373 } 380 374 381 - void sbus_dma_sync_single_for_cpu(struct device *dev, dma_addr_t ba, size_t size, int direction) 375 + static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, 376 + int n, enum dma_data_direction dir) 382 377 { 378 + BUG(); 383 379 } 384 380 385 - void sbus_dma_sync_single_for_device(struct device *dev, dma_addr_t ba, size_t size, int direction) 381 + static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg, 382 + int n, enum dma_data_direction dir) 386 383 { 384 + BUG(); 387 385 } 386 + 387 + struct dma_map_ops sbus_dma_ops = { 388 + .alloc_coherent = sbus_alloc_coherent, 389 + .free_coherent = sbus_free_coherent, 390 + .map_page = sbus_map_page, 391 + .unmap_page = sbus_unmap_page, 392 + .map_sg = sbus_map_sg, 393 + .unmap_sg = sbus_unmap_sg, 394 + .sync_sg_for_cpu = sbus_sync_sg_for_cpu, 395 + .sync_sg_for_device = sbus_sync_sg_for_device, 396 + }; 397 + 398 + struct dma_map_ops *dma_ops = &sbus_dma_ops; 399 + EXPORT_SYMBOL(dma_ops); 388 400 389 401 static int __init sparc_register_ioport(void) 390 402 { ··· 421 395 /* Allocate and map kernel buffer using consistent mode DMA for a device. 422 396 * hwdev should be valid struct pci_dev pointer for PCI devices. 423 397 */ 424 - void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba) 398 + static void *pci32_alloc_coherent(struct device *dev, size_t len, 399 + dma_addr_t *pba, gfp_t gfp) 425 400 { 426 401 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK; 427 402 unsigned long va; ··· 466 439 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */ 467 440 return (void *) res->start; 468 441 } 469 - EXPORT_SYMBOL(pci_alloc_consistent); 470 442 471 443 /* Free and unmap a consistent DMA buffer. 472 444 * cpu_addr is what was returned from pci_alloc_consistent, ··· 475 449 * References to the memory and mappings associated with cpu_addr/dma_addr 476 450 * past this call are illegal. 477 451 */ 478 - void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba) 452 + static void pci32_free_coherent(struct device *dev, size_t n, void *p, 453 + dma_addr_t ba) 479 454 { 480 455 struct resource *res; 481 456 unsigned long pgp; ··· 508 481 509 482 free_pages(pgp, get_order(n)); 510 483 } 511 - EXPORT_SYMBOL(pci_free_consistent); 512 - 513 - /* Map a single buffer of the indicated size for DMA in streaming mode. 514 - * The 32-bit bus address to use is returned. 515 - * 516 - * Once the device is given the dma address, the device owns this memory 517 - * until either pci_unmap_single or pci_dma_sync_single_* is performed. 518 - */ 519 - dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size, 520 - int direction) 521 - { 522 - BUG_ON(direction == PCI_DMA_NONE); 523 - /* IIep is write-through, not flushing. */ 524 - return virt_to_phys(ptr); 525 - } 526 - EXPORT_SYMBOL(pci_map_single); 527 - 528 - /* Unmap a single streaming mode DMA translation. The dma_addr and size 529 - * must match what was provided for in a previous pci_map_single call. All 530 - * other usages are undefined. 531 - * 532 - * After this call, reads by the cpu to the buffer are guaranteed to see 533 - * whatever the device wrote there. 534 - */ 535 - void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size, 536 - int direction) 537 - { 538 - BUG_ON(direction == PCI_DMA_NONE); 539 - if (direction != PCI_DMA_TODEVICE) { 540 - mmu_inval_dma_area((unsigned long)phys_to_virt(ba), 541 - (size + PAGE_SIZE-1) & PAGE_MASK); 542 - } 543 - } 544 - EXPORT_SYMBOL(pci_unmap_single); 545 484 546 485 /* 547 486 * Same as pci_map_single, but with pages. 548 487 */ 549 - dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page, 550 - unsigned long offset, size_t size, int direction) 488 + static dma_addr_t pci32_map_page(struct device *dev, struct page *page, 489 + unsigned long offset, size_t size, 490 + enum dma_data_direction dir, 491 + struct dma_attrs *attrs) 551 492 { 552 - BUG_ON(direction == PCI_DMA_NONE); 553 493 /* IIep is write-through, not flushing. */ 554 494 return page_to_phys(page) + offset; 555 495 } 556 - EXPORT_SYMBOL(pci_map_page); 557 - 558 - void pci_unmap_page(struct pci_dev *hwdev, 559 - dma_addr_t dma_address, size_t size, int direction) 560 - { 561 - BUG_ON(direction == PCI_DMA_NONE); 562 - /* mmu_inval_dma_area XXX */ 563 - } 564 - EXPORT_SYMBOL(pci_unmap_page); 565 496 566 497 /* Map a set of buffers described by scatterlist in streaming 567 498 * mode for DMA. This is the scather-gather version of the ··· 536 551 * Device ownership issues as mentioned above for pci_map_single are 537 552 * the same here. 538 553 */ 539 - int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, 540 - int direction) 554 + static int pci32_map_sg(struct device *device, struct scatterlist *sgl, 555 + int nents, enum dma_data_direction dir, 556 + struct dma_attrs *attrs) 541 557 { 542 558 struct scatterlist *sg; 543 559 int n; 544 560 545 - BUG_ON(direction == PCI_DMA_NONE); 546 561 /* IIep is write-through, not flushing. */ 547 562 for_each_sg(sgl, sg, nents, n) { 548 563 BUG_ON(page_address(sg_page(sg)) == NULL); ··· 551 566 } 552 567 return nents; 553 568 } 554 - EXPORT_SYMBOL(pci_map_sg); 555 569 556 570 /* Unmap a set of streaming mode DMA translations. 557 571 * Again, cpu read rules concerning calls here are the same as for 558 572 * pci_unmap_single() above. 559 573 */ 560 - void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, 561 - int direction) 574 + static void pci32_unmap_sg(struct device *dev, struct scatterlist *sgl, 575 + int nents, enum dma_data_direction dir, 576 + struct dma_attrs *attrs) 562 577 { 563 578 struct scatterlist *sg; 564 579 int n; 565 580 566 - BUG_ON(direction == PCI_DMA_NONE); 567 - if (direction != PCI_DMA_TODEVICE) { 581 + if (dir != PCI_DMA_TODEVICE) { 568 582 for_each_sg(sgl, sg, nents, n) { 569 583 BUG_ON(page_address(sg_page(sg)) == NULL); 570 584 mmu_inval_dma_area( ··· 572 588 } 573 589 } 574 590 } 575 - EXPORT_SYMBOL(pci_unmap_sg); 576 591 577 592 /* Make physical memory consistent for a single 578 593 * streaming mode DMA translation before or after a transfer. ··· 583 600 * must first perform a pci_dma_sync_for_device, and then the 584 601 * device again owns the buffer. 585 602 */ 586 - void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction) 603 + static void pci32_sync_single_for_cpu(struct device *dev, dma_addr_t ba, 604 + size_t size, enum dma_data_direction dir) 587 605 { 588 - BUG_ON(direction == PCI_DMA_NONE); 589 - if (direction != PCI_DMA_TODEVICE) { 606 + if (dir != PCI_DMA_TODEVICE) { 590 607 mmu_inval_dma_area((unsigned long)phys_to_virt(ba), 591 608 (size + PAGE_SIZE-1) & PAGE_MASK); 592 609 } 593 610 } 594 - EXPORT_SYMBOL(pci_dma_sync_single_for_cpu); 595 611 596 - void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction) 612 + static void pci32_sync_single_for_device(struct device *dev, dma_addr_t ba, 613 + size_t size, enum dma_data_direction dir) 597 614 { 598 - BUG_ON(direction == PCI_DMA_NONE); 599 - if (direction != PCI_DMA_TODEVICE) { 615 + if (dir != PCI_DMA_TODEVICE) { 600 616 mmu_inval_dma_area((unsigned long)phys_to_virt(ba), 601 617 (size + PAGE_SIZE-1) & PAGE_MASK); 602 618 } 603 619 } 604 - EXPORT_SYMBOL(pci_dma_sync_single_for_device); 605 620 606 621 /* Make physical memory consistent for a set of streaming 607 622 * mode DMA translations after a transfer. ··· 607 626 * The same as pci_dma_sync_single_* but for a scatter-gather list, 608 627 * same rules and usage. 609 628 */ 610 - void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction) 629 + static void pci32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, 630 + int nents, enum dma_data_direction dir) 611 631 { 612 632 struct scatterlist *sg; 613 633 int n; 614 634 615 - BUG_ON(direction == PCI_DMA_NONE); 616 - if (direction != PCI_DMA_TODEVICE) { 635 + if (dir != PCI_DMA_TODEVICE) { 617 636 for_each_sg(sgl, sg, nents, n) { 618 637 BUG_ON(page_address(sg_page(sg)) == NULL); 619 638 mmu_inval_dma_area( ··· 622 641 } 623 642 } 624 643 } 625 - EXPORT_SYMBOL(pci_dma_sync_sg_for_cpu); 626 644 627 - void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction) 645 + static void pci32_sync_sg_for_device(struct device *device, struct scatterlist *sgl, 646 + int nents, enum dma_data_direction dir) 628 647 { 629 648 struct scatterlist *sg; 630 649 int n; 631 650 632 - BUG_ON(direction == PCI_DMA_NONE); 633 - if (direction != PCI_DMA_TODEVICE) { 651 + if (dir != PCI_DMA_TODEVICE) { 634 652 for_each_sg(sgl, sg, nents, n) { 635 653 BUG_ON(page_address(sg_page(sg)) == NULL); 636 654 mmu_inval_dma_area( ··· 638 658 } 639 659 } 640 660 } 641 - EXPORT_SYMBOL(pci_dma_sync_sg_for_device); 661 + 662 + struct dma_map_ops pci32_dma_ops = { 663 + .alloc_coherent = pci32_alloc_coherent, 664 + .free_coherent = pci32_free_coherent, 665 + .map_page = pci32_map_page, 666 + .map_sg = pci32_map_sg, 667 + .unmap_sg = pci32_unmap_sg, 668 + .sync_single_for_cpu = pci32_sync_single_for_cpu, 669 + .sync_single_for_device = pci32_sync_single_for_device, 670 + .sync_sg_for_cpu = pci32_sync_sg_for_cpu, 671 + .sync_sg_for_device = pci32_sync_sg_for_device, 672 + }; 673 + EXPORT_SYMBOL(pci32_dma_ops); 674 + 642 675 #endif /* CONFIG_PCI */ 676 + 677 + /* 678 + * Return whether the given PCI device DMA address mask can be 679 + * supported properly. For example, if your device can only drive the 680 + * low 24-bits during PCI bus mastering, then you would pass 681 + * 0x00ffffff as the mask to this function. 682 + */ 683 + int dma_supported(struct device *dev, u64 mask) 684 + { 685 + #ifdef CONFIG_PCI 686 + if (dev->bus == &pci_bus_type) 687 + return 1; 688 + #endif 689 + return 0; 690 + } 691 + EXPORT_SYMBOL(dma_supported); 692 + 693 + int dma_set_mask(struct device *dev, u64 dma_mask) 694 + { 695 + #ifdef CONFIG_PCI 696 + if (dev->bus == &pci_bus_type) 697 + return pci_set_dma_mask(to_pci_dev(dev), dma_mask); 698 + #endif 699 + return -EOPNOTSUPP; 700 + } 701 + EXPORT_SYMBOL(dma_set_mask); 702 + 643 703 644 704 #ifdef CONFIG_PROC_FS 645 705
+1 -1
arch/sparc/kernel/pci.c
··· 1039 1039 pci_dev_put(ali_isa_bridge); 1040 1040 } 1041 1041 1042 - int pci_dma_supported(struct pci_dev *pdev, u64 device_mask) 1042 + int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask) 1043 1043 { 1044 1044 u64 dma_addr_mask; 1045 1045
+9 -21
arch/sparc/kernel/pci_sun4v.c
··· 232 232 233 233 static dma_addr_t dma_4v_map_page(struct device *dev, struct page *page, 234 234 unsigned long offset, size_t sz, 235 - enum dma_data_direction direction) 235 + enum dma_data_direction direction, 236 + struct dma_attrs *attrs) 236 237 { 237 238 struct iommu *iommu; 238 239 unsigned long flags, npages, oaddr; ··· 297 296 } 298 297 299 298 static void dma_4v_unmap_page(struct device *dev, dma_addr_t bus_addr, 300 - size_t sz, enum dma_data_direction direction) 299 + size_t sz, enum dma_data_direction direction, 300 + struct dma_attrs *attrs) 301 301 { 302 302 struct pci_pbm_info *pbm; 303 303 struct iommu *iommu; ··· 338 336 } 339 337 340 338 static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist, 341 - int nelems, enum dma_data_direction direction) 339 + int nelems, enum dma_data_direction direction, 340 + struct dma_attrs *attrs) 342 341 { 343 342 struct scatterlist *s, *outs, *segstart; 344 343 unsigned long flags, handle, prot; ··· 481 478 } 482 479 483 480 static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist, 484 - int nelems, enum dma_data_direction direction) 481 + int nelems, enum dma_data_direction direction, 482 + struct dma_attrs *attrs) 485 483 { 486 484 struct pci_pbm_info *pbm; 487 485 struct scatterlist *sg; ··· 525 521 spin_unlock_irqrestore(&iommu->lock, flags); 526 522 } 527 523 528 - static void dma_4v_sync_single_for_cpu(struct device *dev, 529 - dma_addr_t bus_addr, size_t sz, 530 - enum dma_data_direction direction) 531 - { 532 - /* Nothing to do... */ 533 - } 534 - 535 - static void dma_4v_sync_sg_for_cpu(struct device *dev, 536 - struct scatterlist *sglist, int nelems, 537 - enum dma_data_direction direction) 538 - { 539 - /* Nothing to do... */ 540 - } 541 - 542 - static const struct dma_ops sun4v_dma_ops = { 524 + static struct dma_map_ops sun4v_dma_ops = { 543 525 .alloc_coherent = dma_4v_alloc_coherent, 544 526 .free_coherent = dma_4v_free_coherent, 545 527 .map_page = dma_4v_map_page, 546 528 .unmap_page = dma_4v_unmap_page, 547 529 .map_sg = dma_4v_map_sg, 548 530 .unmap_sg = dma_4v_unmap_sg, 549 - .sync_single_for_cpu = dma_4v_sync_single_for_cpu, 550 - .sync_sg_for_cpu = dma_4v_sync_sg_for_cpu, 551 531 }; 552 532 553 533 static void __devinit pci_sun4v_scan_bus(struct pci_pbm_info *pbm,
+18
arch/x86/include/asm/dma-mapping.h
··· 55 55 extern void *dma_generic_alloc_coherent(struct device *dev, size_t size, 56 56 dma_addr_t *dma_addr, gfp_t flag); 57 57 58 + static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) 59 + { 60 + if (!dev->dma_mask) 61 + return 0; 62 + 63 + return addr + size <= *dev->dma_mask; 64 + } 65 + 66 + static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 67 + { 68 + return paddr; 69 + } 70 + 71 + static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 72 + { 73 + return daddr; 74 + } 75 + 58 76 static inline void 59 77 dma_cache_sync(struct device *dev, void *vaddr, size_t size, 60 78 enum dma_data_direction dir)
+1 -1
arch/x86/kernel/pci-dma.c
··· 147 147 return NULL; 148 148 149 149 addr = page_to_phys(page); 150 - if (!is_buffer_dma_capable(dma_mask, addr, size)) { 150 + if (addr + size > dma_mask) { 151 151 __free_pages(page, get_order(size)); 152 152 153 153 if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
+2 -3
arch/x86/kernel/pci-gart_64.c
··· 190 190 static inline int 191 191 need_iommu(struct device *dev, unsigned long addr, size_t size) 192 192 { 193 - return force_iommu || 194 - !is_buffer_dma_capable(*dev->dma_mask, addr, size); 193 + return force_iommu || !dma_capable(dev, addr, size); 195 194 } 196 195 197 196 static inline int 198 197 nonforced_iommu(struct device *dev, unsigned long addr, size_t size) 199 198 { 200 - return !is_buffer_dma_capable(*dev->dma_mask, addr, size); 199 + return !dma_capable(dev, addr, size); 201 200 } 202 201 203 202 /* Map a single continuous physical area into the IOMMU.
+23 -6
arch/x86/kernel/pci-nommu.c
··· 14 14 static int 15 15 check_addr(char *name, struct device *hwdev, dma_addr_t bus, size_t size) 16 16 { 17 - if (hwdev && !is_buffer_dma_capable(*hwdev->dma_mask, bus, size)) { 17 + if (hwdev && !dma_capable(hwdev, bus, size)) { 18 18 if (*hwdev->dma_mask >= DMA_BIT_MASK(32)) 19 19 printk(KERN_ERR 20 20 "nommu_%s: overflow %Lx+%zu of device mask %Lx\n", ··· 79 79 free_pages((unsigned long)vaddr, get_order(size)); 80 80 } 81 81 82 + static void nommu_sync_single_for_device(struct device *dev, 83 + dma_addr_t addr, size_t size, 84 + enum dma_data_direction dir) 85 + { 86 + flush_write_buffers(); 87 + } 88 + 89 + 90 + static void nommu_sync_sg_for_device(struct device *dev, 91 + struct scatterlist *sg, int nelems, 92 + enum dma_data_direction dir) 93 + { 94 + flush_write_buffers(); 95 + } 96 + 82 97 struct dma_map_ops nommu_dma_ops = { 83 - .alloc_coherent = dma_generic_alloc_coherent, 84 - .free_coherent = nommu_free_coherent, 85 - .map_sg = nommu_map_sg, 86 - .map_page = nommu_map_page, 87 - .is_phys = 1, 98 + .alloc_coherent = dma_generic_alloc_coherent, 99 + .free_coherent = nommu_free_coherent, 100 + .map_sg = nommu_map_sg, 101 + .map_page = nommu_map_page, 102 + .sync_single_for_device = nommu_sync_single_for_device, 103 + .sync_sg_for_device = nommu_sync_sg_for_device, 104 + .is_phys = 1, 88 105 }; 89 106 90 107 void __init no_iommu_init(void)
-25
arch/x86/kernel/pci-swiotlb.c
··· 13 13 14 14 int swiotlb __read_mostly; 15 15 16 - void * __init swiotlb_alloc_boot(size_t size, unsigned long nslabs) 17 - { 18 - return alloc_bootmem_low_pages(size); 19 - } 20 - 21 - void *swiotlb_alloc(unsigned order, unsigned long nslabs) 22 - { 23 - return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order); 24 - } 25 - 26 - dma_addr_t swiotlb_phys_to_bus(struct device *hwdev, phys_addr_t paddr) 27 - { 28 - return paddr; 29 - } 30 - 31 - phys_addr_t swiotlb_bus_to_phys(struct device *hwdev, dma_addr_t baddr) 32 - { 33 - return baddr; 34 - } 35 - 36 - int __weak swiotlb_arch_range_needs_mapping(phys_addr_t paddr, size_t size) 37 - { 38 - return 0; 39 - } 40 - 41 16 static void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size, 42 17 dma_addr_t *dma_handle, gfp_t flags) 43 18 {
-6
include/asm-generic/dma-mapping-common.h
··· 103 103 if (ops->sync_single_for_cpu) 104 104 ops->sync_single_for_cpu(dev, addr, size, dir); 105 105 debug_dma_sync_single_for_cpu(dev, addr, size, dir); 106 - flush_write_buffers(); 107 106 } 108 107 109 108 static inline void dma_sync_single_for_device(struct device *dev, ··· 115 116 if (ops->sync_single_for_device) 116 117 ops->sync_single_for_device(dev, addr, size, dir); 117 118 debug_dma_sync_single_for_device(dev, addr, size, dir); 118 - flush_write_buffers(); 119 119 } 120 120 121 121 static inline void dma_sync_single_range_for_cpu(struct device *dev, ··· 130 132 ops->sync_single_range_for_cpu(dev, addr, offset, size, dir); 131 133 debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir); 132 134 133 - flush_write_buffers(); 134 135 } else 135 136 dma_sync_single_for_cpu(dev, addr, size, dir); 136 137 } ··· 147 150 ops->sync_single_range_for_device(dev, addr, offset, size, dir); 148 151 debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir); 149 152 150 - flush_write_buffers(); 151 153 } else 152 154 dma_sync_single_for_device(dev, addr, size, dir); 153 155 } ··· 161 165 if (ops->sync_sg_for_cpu) 162 166 ops->sync_sg_for_cpu(dev, sg, nelems, dir); 163 167 debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir); 164 - flush_write_buffers(); 165 168 } 166 169 167 170 static inline void ··· 174 179 ops->sync_sg_for_device(dev, sg, nelems, dir); 175 180 debug_dma_sync_sg_for_device(dev, sg, nelems, dir); 176 181 177 - flush_write_buffers(); 178 182 } 179 183 180 184 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL)
-5
include/linux/dma-mapping.h
··· 98 98 return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE; 99 99 } 100 100 101 - static inline int is_buffer_dma_capable(u64 mask, dma_addr_t addr, size_t size) 102 - { 103 - return addr + size <= mask; 104 - } 105 - 106 101 #ifdef CONFIG_HAS_DMA 107 102 #include <asm/dma-mapping.h> 108 103 #else
-11
include/linux/swiotlb.h
··· 14 14 */ 15 15 #define IO_TLB_SEGSIZE 128 16 16 17 - 18 17 /* 19 18 * log of the size of each IO TLB slab. The number of slabs is command line 20 19 * controllable. ··· 22 23 23 24 extern void 24 25 swiotlb_init(void); 25 - 26 - extern void *swiotlb_alloc_boot(size_t bytes, unsigned long nslabs); 27 - extern void *swiotlb_alloc(unsigned order, unsigned long nslabs); 28 - 29 - extern dma_addr_t swiotlb_phys_to_bus(struct device *hwdev, 30 - phys_addr_t address); 31 - extern phys_addr_t swiotlb_bus_to_phys(struct device *hwdev, 32 - dma_addr_t address); 33 - 34 - extern int swiotlb_arch_range_needs_mapping(phys_addr_t paddr, size_t size); 35 26 36 27 extern void 37 28 *swiotlb_alloc_coherent(struct device *hwdev, size_t size,
+44 -80
lib/swiotlb.c
··· 114 114 __setup("swiotlb=", setup_io_tlb_npages); 115 115 /* make io_tlb_overflow tunable too? */ 116 116 117 - void * __weak __init swiotlb_alloc_boot(size_t size, unsigned long nslabs) 118 - { 119 - return alloc_bootmem_low_pages(size); 120 - } 121 - 122 - void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs) 123 - { 124 - return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order); 125 - } 126 - 127 - dma_addr_t __weak swiotlb_phys_to_bus(struct device *hwdev, phys_addr_t paddr) 128 - { 129 - return paddr; 130 - } 131 - 132 - phys_addr_t __weak swiotlb_bus_to_phys(struct device *hwdev, dma_addr_t baddr) 133 - { 134 - return baddr; 135 - } 136 - 117 + /* Note that this doesn't work with highmem page */ 137 118 static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev, 138 119 volatile void *address) 139 120 { 140 - return swiotlb_phys_to_bus(hwdev, virt_to_phys(address)); 141 - } 142 - 143 - void * __weak swiotlb_bus_to_virt(struct device *hwdev, dma_addr_t address) 144 - { 145 - return phys_to_virt(swiotlb_bus_to_phys(hwdev, address)); 146 - } 147 - 148 - int __weak swiotlb_arch_address_needs_mapping(struct device *hwdev, 149 - dma_addr_t addr, size_t size) 150 - { 151 - return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size); 152 - } 153 - 154 - int __weak swiotlb_arch_range_needs_mapping(phys_addr_t paddr, size_t size) 155 - { 156 - return 0; 121 + return phys_to_dma(hwdev, virt_to_phys(address)); 157 122 } 158 123 159 124 static void swiotlb_print_info(unsigned long bytes) ··· 154 189 /* 155 190 * Get IO TLB memory from the low pages 156 191 */ 157 - io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs); 192 + io_tlb_start = alloc_bootmem_low_pages(bytes); 158 193 if (!io_tlb_start) 159 194 panic("Cannot allocate SWIOTLB buffer"); 160 195 io_tlb_end = io_tlb_start + bytes; ··· 210 245 bytes = io_tlb_nslabs << IO_TLB_SHIFT; 211 246 212 247 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { 213 - io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs); 248 + io_tlb_start = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, 249 + order); 214 250 if (io_tlb_start) 215 251 break; 216 252 order--; ··· 281 315 return -ENOMEM; 282 316 } 283 317 284 - static inline int 285 - address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size) 318 + static int is_swiotlb_buffer(phys_addr_t paddr) 286 319 { 287 - return swiotlb_arch_address_needs_mapping(hwdev, addr, size); 288 - } 289 - 290 - static inline int range_needs_mapping(phys_addr_t paddr, size_t size) 291 - { 292 - return swiotlb_force || swiotlb_arch_range_needs_mapping(paddr, size); 293 - } 294 - 295 - static int is_swiotlb_buffer(char *addr) 296 - { 297 - return addr >= io_tlb_start && addr < io_tlb_end; 320 + return paddr >= virt_to_phys(io_tlb_start) && 321 + paddr < virt_to_phys(io_tlb_end); 298 322 } 299 323 300 324 /* ··· 517 561 dma_mask = hwdev->coherent_dma_mask; 518 562 519 563 ret = (void *)__get_free_pages(flags, order); 520 - if (ret && 521 - !is_buffer_dma_capable(dma_mask, swiotlb_virt_to_bus(hwdev, ret), 522 - size)) { 564 + if (ret && swiotlb_virt_to_bus(hwdev, ret) + size > dma_mask) { 523 565 /* 524 566 * The allocated memory isn't reachable by the device. 525 567 */ ··· 539 585 dev_addr = swiotlb_virt_to_bus(hwdev, ret); 540 586 541 587 /* Confirm address can be DMA'd by device */ 542 - if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) { 588 + if (dev_addr + size > dma_mask) { 543 589 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n", 544 590 (unsigned long long)dma_mask, 545 591 (unsigned long long)dev_addr); ··· 555 601 556 602 void 557 603 swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr, 558 - dma_addr_t dma_handle) 604 + dma_addr_t dev_addr) 559 605 { 606 + phys_addr_t paddr = dma_to_phys(hwdev, dev_addr); 607 + 560 608 WARN_ON(irqs_disabled()); 561 - if (!is_swiotlb_buffer(vaddr)) 562 - free_pages((unsigned long) vaddr, get_order(size)); 609 + if (!is_swiotlb_buffer(paddr)) 610 + free_pages((unsigned long)vaddr, get_order(size)); 563 611 else 564 612 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */ 565 613 do_unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE); ··· 581 625 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at " 582 626 "device %s\n", size, dev ? dev_name(dev) : "?"); 583 627 584 - if (size > io_tlb_overflow && do_panic) { 585 - if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) 586 - panic("DMA: Memory would be corrupted\n"); 587 - if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) 588 - panic("DMA: Random memory would be DMAed\n"); 589 - } 628 + if (size <= io_tlb_overflow || !do_panic) 629 + return; 630 + 631 + if (dir == DMA_BIDIRECTIONAL) 632 + panic("DMA: Random memory could be DMA accessed\n"); 633 + if (dir == DMA_FROM_DEVICE) 634 + panic("DMA: Random memory could be DMA written\n"); 635 + if (dir == DMA_TO_DEVICE) 636 + panic("DMA: Random memory could be DMA read\n"); 590 637 } 591 638 592 639 /* ··· 605 646 struct dma_attrs *attrs) 606 647 { 607 648 phys_addr_t phys = page_to_phys(page) + offset; 608 - dma_addr_t dev_addr = swiotlb_phys_to_bus(dev, phys); 649 + dma_addr_t dev_addr = phys_to_dma(dev, phys); 609 650 void *map; 610 651 611 652 BUG_ON(dir == DMA_NONE); ··· 614 655 * we can safely return the device addr and not worry about bounce 615 656 * buffering it. 616 657 */ 617 - if (!address_needs_mapping(dev, dev_addr, size) && 618 - !range_needs_mapping(phys, size)) 658 + if (dma_capable(dev, dev_addr, size) && !swiotlb_force) 619 659 return dev_addr; 620 660 621 661 /* ··· 631 673 /* 632 674 * Ensure that the address returned is DMA'ble 633 675 */ 634 - if (address_needs_mapping(dev, dev_addr, size)) 676 + if (!dma_capable(dev, dev_addr, size)) 635 677 panic("map_single: bounce buffer is not DMA'ble"); 636 678 637 679 return dev_addr; ··· 649 691 static void unmap_single(struct device *hwdev, dma_addr_t dev_addr, 650 692 size_t size, int dir) 651 693 { 652 - char *dma_addr = swiotlb_bus_to_virt(hwdev, dev_addr); 694 + phys_addr_t paddr = dma_to_phys(hwdev, dev_addr); 653 695 654 696 BUG_ON(dir == DMA_NONE); 655 697 656 - if (is_swiotlb_buffer(dma_addr)) { 657 - do_unmap_single(hwdev, dma_addr, size, dir); 698 + if (is_swiotlb_buffer(paddr)) { 699 + do_unmap_single(hwdev, phys_to_virt(paddr), size, dir); 658 700 return; 659 701 } 660 702 661 703 if (dir != DMA_FROM_DEVICE) 662 704 return; 663 705 664 - dma_mark_clean(dma_addr, size); 706 + /* 707 + * phys_to_virt doesn't work with hihgmem page but we could 708 + * call dma_mark_clean() with hihgmem page here. However, we 709 + * are fine since dma_mark_clean() is null on POWERPC. We can 710 + * make dma_mark_clean() take a physical address if necessary. 711 + */ 712 + dma_mark_clean(phys_to_virt(paddr), size); 665 713 } 666 714 667 715 void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr, ··· 692 728 swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr, 693 729 size_t size, int dir, int target) 694 730 { 695 - char *dma_addr = swiotlb_bus_to_virt(hwdev, dev_addr); 731 + phys_addr_t paddr = dma_to_phys(hwdev, dev_addr); 696 732 697 733 BUG_ON(dir == DMA_NONE); 698 734 699 - if (is_swiotlb_buffer(dma_addr)) { 700 - sync_single(hwdev, dma_addr, size, dir, target); 735 + if (is_swiotlb_buffer(paddr)) { 736 + sync_single(hwdev, phys_to_virt(paddr), size, dir, target); 701 737 return; 702 738 } 703 739 704 740 if (dir != DMA_FROM_DEVICE) 705 741 return; 706 742 707 - dma_mark_clean(dma_addr, size); 743 + dma_mark_clean(phys_to_virt(paddr), size); 708 744 } 709 745 710 746 void ··· 781 817 782 818 for_each_sg(sgl, sg, nelems, i) { 783 819 phys_addr_t paddr = sg_phys(sg); 784 - dma_addr_t dev_addr = swiotlb_phys_to_bus(hwdev, paddr); 820 + dma_addr_t dev_addr = phys_to_dma(hwdev, paddr); 785 821 786 - if (range_needs_mapping(paddr, sg->length) || 787 - address_needs_mapping(hwdev, dev_addr, sg->length)) { 822 + if (swiotlb_force || 823 + !dma_capable(hwdev, dev_addr, sg->length)) { 788 824 void *map = map_single(hwdev, sg_phys(sg), 789 825 sg->length, dir); 790 826 if (!map) {