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

xtensa: reimplement DMA API using common helpers

- keep existing functionality: don't handle attributes, don't support
high memory;
- implement scatterlist primitives (map/unmap/sync);
- enable DMA API debug.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>

+239 -184
+2
arch/xtensa/Kconfig
··· 14 14 select GENERIC_IRQ_SHOW 15 15 select GENERIC_PCI_IOMAP 16 16 select GENERIC_SCHED_CLOCK 17 + select HAVE_DMA_API_DEBUG 18 + select HAVE_DMA_ATTRS 17 19 select HAVE_FUNCTION_TRACER 18 20 select HAVE_IRQ_TIME_ACCOUNTING 19 21 select HAVE_OPROFILE
-1
arch/xtensa/include/asm/Kbuild
··· 2 2 generic-y += bug.h 3 3 generic-y += clkdev.h 4 4 generic-y += cputime.h 5 - generic-y += device.h 6 5 generic-y += div64.h 7 6 generic-y += emergency-restart.h 8 7 generic-y += errno.h
+19
arch/xtensa/include/asm/device.h
··· 1 + /* 2 + * Arch specific extensions to struct device 3 + * 4 + * This file is released under the GPLv2 5 + */ 6 + #ifndef _ASM_XTENSA_DEVICE_H 7 + #define _ASM_XTENSA_DEVICE_H 8 + 9 + struct dma_map_ops; 10 + 11 + struct dev_archdata { 12 + /* DMA operations on that device */ 13 + struct dma_map_ops *dma_ops; 14 + }; 15 + 16 + struct pdev_archdata { 17 + }; 18 + 19 + #endif /* _ASM_XTENSA_DEVICE_H */
+44 -152
arch/xtensa/include/asm/dma-mapping.h
··· 1 1 /* 2 - * include/asm-xtensa/dma-mapping.h 3 - * 4 2 * This file is subject to the terms and conditions of the GNU General Public 5 3 * License. See the file "COPYING" in the main directory of this archive 6 4 * for more details. 7 5 * 8 6 * Copyright (C) 2003 - 2005 Tensilica Inc. 7 + * Copyright (C) 2015 Cadence Design Systems Inc. 9 8 */ 10 9 11 10 #ifndef _XTENSA_DMA_MAPPING_H ··· 12 13 13 14 #include <asm/cache.h> 14 15 #include <asm/io.h> 16 + 17 + #include <asm-generic/dma-coherent.h> 18 + 15 19 #include <linux/mm.h> 16 20 #include <linux/scatterlist.h> 17 21 18 22 #define DMA_ERROR_CODE (~(dma_addr_t)0x0) 19 23 20 - /* 21 - * DMA-consistent mapping functions. 22 - */ 24 + extern struct dma_map_ops xtensa_dma_map_ops; 23 25 24 - extern void *consistent_alloc(int, size_t, dma_addr_t, unsigned long); 25 - extern void consistent_free(void*, size_t, dma_addr_t); 26 - extern void consistent_sync(void*, size_t, int); 27 - 28 - #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f) 29 - #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h) 30 - 31 - void *dma_alloc_coherent(struct device *dev, size_t size, 32 - dma_addr_t *dma_handle, gfp_t flag); 33 - 34 - void dma_free_coherent(struct device *dev, size_t size, 35 - void *vaddr, dma_addr_t dma_handle); 36 - 37 - static inline dma_addr_t 38 - dma_map_single(struct device *dev, void *ptr, size_t size, 39 - enum dma_data_direction direction) 26 + static inline struct dma_map_ops *get_dma_ops(struct device *dev) 40 27 { 41 - BUG_ON(direction == DMA_NONE); 42 - consistent_sync(ptr, size, direction); 43 - return virt_to_phys(ptr); 28 + if (dev && dev->archdata.dma_ops) 29 + return dev->archdata.dma_ops; 30 + else 31 + return &xtensa_dma_map_ops; 44 32 } 45 33 46 - static inline void 47 - dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size, 48 - enum dma_data_direction direction) 34 + #include <asm-generic/dma-mapping-common.h> 35 + 36 + #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL) 37 + #define dma_free_noncoherent(d, s, v, h) dma_free_attrs(d, s, v, h, NULL) 38 + #define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL) 39 + #define dma_free_coherent(d, s, c, h) dma_free_attrs(d, s, c, h, NULL) 40 + 41 + static inline void *dma_alloc_attrs(struct device *dev, size_t size, 42 + dma_addr_t *dma_handle, gfp_t gfp, 43 + struct dma_attrs *attrs) 49 44 { 50 - BUG_ON(direction == DMA_NONE); 45 + void *ret; 46 + struct dma_map_ops *ops = get_dma_ops(dev); 47 + 48 + if (dma_alloc_from_coherent(dev, size, dma_handle, &ret)) 49 + return ret; 50 + 51 + ret = ops->alloc(dev, size, dma_handle, gfp, attrs); 52 + debug_dma_alloc_coherent(dev, size, *dma_handle, ret); 53 + 54 + return ret; 51 55 } 52 56 53 - static inline int 54 - dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents, 55 - enum dma_data_direction direction) 57 + static inline void dma_free_attrs(struct device *dev, size_t size, 58 + void *vaddr, dma_addr_t dma_handle, 59 + struct dma_attrs *attrs) 56 60 { 57 - int i; 58 - struct scatterlist *sg; 61 + struct dma_map_ops *ops = get_dma_ops(dev); 59 62 60 - BUG_ON(direction == DMA_NONE); 63 + if (dma_release_from_coherent(dev, get_order(size), vaddr)) 64 + return; 61 65 62 - for_each_sg(sglist, sg, nents, i) { 63 - BUG_ON(!sg_page(sg)); 64 - 65 - sg->dma_address = sg_phys(sg); 66 - consistent_sync(sg_virt(sg), sg->length, direction); 67 - } 68 - 69 - return nents; 66 + ops->free(dev, size, vaddr, dma_handle, attrs); 67 + debug_dma_free_coherent(dev, size, vaddr, dma_handle); 70 68 } 71 69 72 - static inline dma_addr_t 73 - dma_map_page(struct device *dev, struct page *page, unsigned long offset, 74 - size_t size, enum dma_data_direction direction) 75 - { 76 - BUG_ON(direction == DMA_NONE); 77 - return (dma_addr_t)(page_to_pfn(page)) * PAGE_SIZE + offset; 78 - } 79 - 80 - static inline void 81 - dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size, 82 - enum dma_data_direction direction) 83 - { 84 - BUG_ON(direction == DMA_NONE); 85 - } 86 - 87 - 88 - static inline void 89 - dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries, 90 - enum dma_data_direction direction) 91 - { 92 - BUG_ON(direction == DMA_NONE); 93 - } 94 - 95 - static inline void 96 - dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size, 97 - enum dma_data_direction direction) 98 - { 99 - consistent_sync((void *)bus_to_virt(dma_handle), size, direction); 100 - } 101 - 102 - static inline void 103 - dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, 104 - size_t size, enum dma_data_direction direction) 105 - { 106 - consistent_sync((void *)bus_to_virt(dma_handle), size, direction); 107 - } 108 - 109 - static inline void 110 - dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle, 111 - unsigned long offset, size_t size, 112 - enum dma_data_direction direction) 113 - { 114 - 115 - consistent_sync((void *)bus_to_virt(dma_handle)+offset,size,direction); 116 - } 117 - 118 - static inline void 119 - dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle, 120 - unsigned long offset, size_t size, 121 - enum dma_data_direction direction) 122 - { 123 - 124 - consistent_sync((void *)bus_to_virt(dma_handle)+offset,size,direction); 125 - } 126 - static inline void 127 - dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sglist, int nelems, 128 - enum dma_data_direction dir) 129 - { 130 - int i; 131 - struct scatterlist *sg; 132 - 133 - for_each_sg(sglist, sg, nelems, i) 134 - consistent_sync(sg_virt(sg), sg->length, dir); 135 - } 136 - 137 - static inline void 138 - dma_sync_sg_for_device(struct device *dev, struct scatterlist *sglist, 139 - int nelems, enum dma_data_direction dir) 140 - { 141 - int i; 142 - struct scatterlist *sg; 143 - 144 - for_each_sg(sglist, sg, nelems, i) 145 - consistent_sync(sg_virt(sg), sg->length, dir); 146 - } 147 70 static inline int 148 71 dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 149 72 { 150 - return 0; 73 + struct dma_map_ops *ops = get_dma_ops(dev); 74 + 75 + debug_dma_mapping_error(dev, dma_addr); 76 + return ops->mapping_error(dev, dma_addr); 151 77 } 152 78 153 79 static inline int ··· 92 168 return 0; 93 169 } 94 170 95 - static inline void 96 - dma_cache_sync(struct device *dev, void *vaddr, size_t size, 97 - enum dma_data_direction direction) 98 - { 99 - consistent_sync(vaddr, size, direction); 100 - } 101 - 102 - /* Not supported for now */ 103 - static inline int dma_mmap_coherent(struct device *dev, 104 - struct vm_area_struct *vma, void *cpu_addr, 105 - dma_addr_t dma_addr, size_t size) 106 - { 107 - return -EINVAL; 108 - } 109 - 110 - static inline int dma_get_sgtable(struct device *dev, struct sg_table *sgt, 111 - void *cpu_addr, dma_addr_t dma_addr, 112 - size_t size) 113 - { 114 - return -EINVAL; 115 - } 116 - 117 - static inline void *dma_alloc_attrs(struct device *dev, size_t size, 118 - dma_addr_t *dma_handle, gfp_t flag, 119 - struct dma_attrs *attrs) 120 - { 121 - return NULL; 122 - } 123 - 124 - static inline void dma_free_attrs(struct device *dev, size_t size, 125 - void *vaddr, dma_addr_t dma_handle, 126 - struct dma_attrs *attrs) 127 - { 128 - } 171 + void dma_cache_sync(struct device *dev, void *vaddr, size_t size, 172 + enum dma_data_direction direction); 129 173 130 174 #endif /* _XTENSA_DMA_MAPPING_H */
+174 -31
arch/xtensa/kernel/pci-dma.c
··· 1 1 /* 2 - * arch/xtensa/kernel/pci-dma.c 3 - * 4 2 * DMA coherent memory allocation. 5 3 * 6 4 * This program is free software; you can redistribute it and/or modify it ··· 7 9 * option) any later version. 8 10 * 9 11 * Copyright (C) 2002 - 2005 Tensilica Inc. 12 + * Copyright (C) 2015 Cadence Design Systems Inc. 10 13 * 11 14 * Based on version for i386. 12 15 * ··· 24 25 #include <asm/io.h> 25 26 #include <asm/cacheflush.h> 26 27 28 + void dma_cache_sync(struct device *dev, void *vaddr, size_t size, 29 + enum dma_data_direction dir) 30 + { 31 + switch (dir) { 32 + case DMA_BIDIRECTIONAL: 33 + __flush_invalidate_dcache_range((unsigned long)vaddr, size); 34 + break; 35 + 36 + case DMA_FROM_DEVICE: 37 + __invalidate_dcache_range((unsigned long)vaddr, size); 38 + break; 39 + 40 + case DMA_TO_DEVICE: 41 + __flush_dcache_range((unsigned long)vaddr, size); 42 + break; 43 + 44 + case DMA_NONE: 45 + BUG(); 46 + break; 47 + } 48 + } 49 + EXPORT_SYMBOL(dma_cache_sync); 50 + 51 + static void xtensa_sync_single_for_cpu(struct device *dev, 52 + dma_addr_t dma_handle, size_t size, 53 + enum dma_data_direction dir) 54 + { 55 + void *vaddr; 56 + 57 + switch (dir) { 58 + case DMA_BIDIRECTIONAL: 59 + case DMA_FROM_DEVICE: 60 + vaddr = bus_to_virt(dma_handle); 61 + __invalidate_dcache_range((unsigned long)vaddr, size); 62 + break; 63 + 64 + case DMA_NONE: 65 + BUG(); 66 + break; 67 + 68 + default: 69 + break; 70 + } 71 + } 72 + 73 + static void xtensa_sync_single_for_device(struct device *dev, 74 + dma_addr_t dma_handle, size_t size, 75 + enum dma_data_direction dir) 76 + { 77 + void *vaddr; 78 + 79 + switch (dir) { 80 + case DMA_BIDIRECTIONAL: 81 + case DMA_TO_DEVICE: 82 + vaddr = bus_to_virt(dma_handle); 83 + __flush_dcache_range((unsigned long)vaddr, size); 84 + break; 85 + 86 + case DMA_NONE: 87 + BUG(); 88 + break; 89 + 90 + default: 91 + break; 92 + } 93 + } 94 + 95 + static void xtensa_sync_sg_for_cpu(struct device *dev, 96 + struct scatterlist *sg, int nents, 97 + enum dma_data_direction dir) 98 + { 99 + struct scatterlist *s; 100 + int i; 101 + 102 + for_each_sg(sg, s, nents, i) { 103 + xtensa_sync_single_for_cpu(dev, sg_dma_address(s), 104 + sg_dma_len(s), dir); 105 + } 106 + } 107 + 108 + static void xtensa_sync_sg_for_device(struct device *dev, 109 + struct scatterlist *sg, int nents, 110 + enum dma_data_direction dir) 111 + { 112 + struct scatterlist *s; 113 + int i; 114 + 115 + for_each_sg(sg, s, nents, i) { 116 + xtensa_sync_single_for_device(dev, sg_dma_address(s), 117 + sg_dma_len(s), dir); 118 + } 119 + } 120 + 27 121 /* 28 122 * Note: We assume that the full memory space is always mapped to 'kseg' 29 123 * Otherwise we have to use page attributes (not implemented). 30 124 */ 31 125 32 - void * 33 - dma_alloc_coherent(struct device *dev,size_t size,dma_addr_t *handle,gfp_t flag) 126 + static void *xtensa_dma_alloc(struct device *dev, size_t size, 127 + dma_addr_t *handle, gfp_t flag, 128 + struct dma_attrs *attrs) 34 129 { 35 130 unsigned long ret; 36 131 unsigned long uncached = 0; ··· 145 52 BUG_ON(ret < XCHAL_KSEG_CACHED_VADDR || 146 53 ret > XCHAL_KSEG_CACHED_VADDR + XCHAL_KSEG_SIZE - 1); 147 54 55 + uncached = ret + XCHAL_KSEG_BYPASS_VADDR - XCHAL_KSEG_CACHED_VADDR; 56 + *handle = virt_to_bus((void *)ret); 57 + __invalidate_dcache_range(ret, size); 148 58 149 - if (ret != 0) { 150 - memset((void*) ret, 0, size); 151 - uncached = ret+XCHAL_KSEG_BYPASS_VADDR-XCHAL_KSEG_CACHED_VADDR; 152 - *handle = virt_to_bus((void*)ret); 153 - __flush_invalidate_dcache_range(ret, size); 154 - } 155 - 156 - return (void*)uncached; 59 + return (void *)uncached; 157 60 } 158 - EXPORT_SYMBOL(dma_alloc_coherent); 159 61 160 - void dma_free_coherent(struct device *hwdev, size_t size, 161 - void *vaddr, dma_addr_t dma_handle) 62 + static void xtensa_dma_free(struct device *hwdev, size_t size, void *vaddr, 63 + dma_addr_t dma_handle, struct dma_attrs *attrs) 162 64 { 163 65 unsigned long addr = (unsigned long)vaddr + 164 66 XCHAL_KSEG_CACHED_VADDR - XCHAL_KSEG_BYPASS_VADDR; ··· 163 75 164 76 free_pages(addr, get_order(size)); 165 77 } 166 - EXPORT_SYMBOL(dma_free_coherent); 167 78 168 - 169 - void consistent_sync(void *vaddr, size_t size, int direction) 79 + static dma_addr_t xtensa_map_page(struct device *dev, struct page *page, 80 + unsigned long offset, size_t size, 81 + enum dma_data_direction dir, 82 + struct dma_attrs *attrs) 170 83 { 171 - switch (direction) { 172 - case PCI_DMA_NONE: 173 - BUG(); 174 - case PCI_DMA_FROMDEVICE: /* invalidate only */ 175 - __invalidate_dcache_range((unsigned long)vaddr, 176 - (unsigned long)size); 177 - break; 84 + dma_addr_t dma_handle = page_to_phys(page) + offset; 178 85 179 - case PCI_DMA_TODEVICE: /* writeback only */ 180 - case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */ 181 - __flush_invalidate_dcache_range((unsigned long)vaddr, 182 - (unsigned long)size); 183 - break; 86 + BUG_ON(PageHighMem(page)); 87 + xtensa_sync_single_for_device(dev, dma_handle, size, dir); 88 + return dma_handle; 89 + } 90 + 91 + static void xtensa_unmap_page(struct device *dev, dma_addr_t dma_handle, 92 + size_t size, enum dma_data_direction dir, 93 + struct dma_attrs *attrs) 94 + { 95 + xtensa_sync_single_for_cpu(dev, dma_handle, size, dir); 96 + } 97 + 98 + static int xtensa_map_sg(struct device *dev, struct scatterlist *sg, 99 + int nents, enum dma_data_direction dir, 100 + struct dma_attrs *attrs) 101 + { 102 + struct scatterlist *s; 103 + int i; 104 + 105 + for_each_sg(sg, s, nents, i) { 106 + s->dma_address = xtensa_map_page(dev, sg_page(s), s->offset, 107 + s->length, dir, attrs); 108 + } 109 + return nents; 110 + } 111 + 112 + static void xtensa_unmap_sg(struct device *dev, 113 + struct scatterlist *sg, int nents, 114 + enum dma_data_direction dir, 115 + struct dma_attrs *attrs) 116 + { 117 + struct scatterlist *s; 118 + int i; 119 + 120 + for_each_sg(sg, s, nents, i) { 121 + xtensa_unmap_page(dev, sg_dma_address(s), 122 + sg_dma_len(s), dir, attrs); 184 123 } 185 124 } 186 - EXPORT_SYMBOL(consistent_sync); 125 + 126 + int xtensa_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 127 + { 128 + return 0; 129 + } 130 + 131 + struct dma_map_ops xtensa_dma_map_ops = { 132 + .alloc = xtensa_dma_alloc, 133 + .free = xtensa_dma_free, 134 + .map_page = xtensa_map_page, 135 + .unmap_page = xtensa_unmap_page, 136 + .map_sg = xtensa_map_sg, 137 + .unmap_sg = xtensa_unmap_sg, 138 + .sync_single_for_cpu = xtensa_sync_single_for_cpu, 139 + .sync_single_for_device = xtensa_sync_single_for_device, 140 + .sync_sg_for_cpu = xtensa_sync_sg_for_cpu, 141 + .sync_sg_for_device = xtensa_sync_sg_for_device, 142 + .mapping_error = xtensa_dma_mapping_error, 143 + }; 144 + EXPORT_SYMBOL(xtensa_dma_map_ops); 145 + 146 + #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16) 147 + 148 + static int __init xtensa_dma_init(void) 149 + { 150 + dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); 151 + return 0; 152 + } 153 + fs_initcall(xtensa_dma_init);