1 /* 2 * This file is subject to the terms and conditions of the GNU General Public 3 * License. See the file COPYING in the main directory of this archive 4 * for more details. 5 */ 6 7 #undef DEBUG 8 9 #include <linux/dma-mapping.h> 10 #include <linux/device.h> 11 #include <linux/kernel.h> 12 #include <linux/platform_device.h> 13 #include <linux/scatterlist.h> 14 #include <linux/slab.h> 15 #include <linux/vmalloc.h> 16 #include <linux/export.h> 17 18 #include <asm/pgalloc.h> 19 20 #if defined(CONFIG_MMU) && !defined(CONFIG_COLDFIRE) 21 22 static void *m68k_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, 23 gfp_t flag, unsigned long attrs) 24 { 25 struct page *page, **map; 26 pgprot_t pgprot; 27 void *addr; 28 int i, order; 29 30 pr_debug("dma_alloc_coherent: %d,%x\n", size, flag); 31 32 size = PAGE_ALIGN(size); 33 order = get_order(size); 34 35 page = alloc_pages(flag, order); 36 if (!page) 37 return NULL; 38 39 *handle = page_to_phys(page); 40 map = kmalloc(sizeof(struct page *) << order, flag & ~__GFP_DMA); 41 if (!map) { 42 __free_pages(page, order); 43 return NULL; 44 } 45 split_page(page, order); 46 47 order = 1 << order; 48 size >>= PAGE_SHIFT; 49 map[0] = page; 50 for (i = 1; i < size; i++) 51 map[i] = page + i; 52 for (; i < order; i++) 53 __free_page(page + i); 54 pgprot = __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY); 55 if (CPU_IS_040_OR_060) 56 pgprot_val(pgprot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S; 57 else 58 pgprot_val(pgprot) |= _PAGE_NOCACHE030; 59 addr = vmap(map, size, VM_MAP, pgprot); 60 kfree(map); 61 62 return addr; 63 } 64 65 static void m68k_dma_free(struct device *dev, size_t size, void *addr, 66 dma_addr_t handle, unsigned long attrs) 67 { 68 pr_debug("dma_free_coherent: %p, %x\n", addr, handle); 69 vfree(addr); 70 } 71 72 #else 73 74 #include <asm/cacheflush.h> 75 76 static void *m68k_dma_alloc(struct device *dev, size_t size, 77 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) 78 { 79 void *ret; 80 81 if (dev == NULL || (*dev->dma_mask < 0xffffffff)) 82 gfp |= GFP_DMA; 83 ret = (void *)__get_free_pages(gfp, get_order(size)); 84 85 if (ret != NULL) { 86 memset(ret, 0, size); 87 *dma_handle = virt_to_phys(ret); 88 } 89 return ret; 90 } 91 92 static void m68k_dma_free(struct device *dev, size_t size, void *vaddr, 93 dma_addr_t dma_handle, unsigned long attrs) 94 { 95 free_pages((unsigned long)vaddr, get_order(size)); 96 } 97 98 #endif /* CONFIG_MMU && !CONFIG_COLDFIRE */ 99 100 static void m68k_dma_sync_single_for_device(struct device *dev, 101 dma_addr_t handle, size_t size, enum dma_data_direction dir) 102 { 103 switch (dir) { 104 case DMA_BIDIRECTIONAL: 105 case DMA_TO_DEVICE: 106 cache_push(handle, size); 107 break; 108 case DMA_FROM_DEVICE: 109 cache_clear(handle, size); 110 break; 111 default: 112 pr_err_ratelimited("dma_sync_single_for_device: unsupported dir %u\n", 113 dir); 114 break; 115 } 116 } 117 118 static void m68k_dma_sync_sg_for_device(struct device *dev, 119 struct scatterlist *sglist, int nents, enum dma_data_direction dir) 120 { 121 int i; 122 struct scatterlist *sg; 123 124 for_each_sg(sglist, sg, nents, i) { 125 dma_sync_single_for_device(dev, sg->dma_address, sg->length, 126 dir); 127 } 128 } 129 130 static dma_addr_t m68k_dma_map_page(struct device *dev, struct page *page, 131 unsigned long offset, size_t size, enum dma_data_direction dir, 132 unsigned long attrs) 133 { 134 dma_addr_t handle = page_to_phys(page) + offset; 135 136 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) 137 dma_sync_single_for_device(dev, handle, size, dir); 138 139 return handle; 140 } 141 142 static int m68k_dma_map_sg(struct device *dev, struct scatterlist *sglist, 143 int nents, enum dma_data_direction dir, unsigned long attrs) 144 { 145 int i; 146 struct scatterlist *sg; 147 148 for_each_sg(sglist, sg, nents, i) { 149 sg->dma_address = sg_phys(sg); 150 151 if (attrs & DMA_ATTR_SKIP_CPU_SYNC) 152 continue; 153 154 dma_sync_single_for_device(dev, sg->dma_address, sg->length, 155 dir); 156 } 157 return nents; 158 } 159 160 const struct dma_map_ops m68k_dma_ops = { 161 .alloc = m68k_dma_alloc, 162 .free = m68k_dma_free, 163 .map_page = m68k_dma_map_page, 164 .map_sg = m68k_dma_map_sg, 165 .sync_single_for_device = m68k_dma_sync_single_for_device, 166 .sync_sg_for_device = m68k_dma_sync_sg_for_device, 167 }; 168 EXPORT_SYMBOL(m68k_dma_ops); 169 170 void arch_setup_pdev_archdata(struct platform_device *pdev) 171 { 172 if (pdev->dev.coherent_dma_mask == DMA_MASK_NONE && 173 pdev->dev.dma_mask == NULL) { 174 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); 175 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; 176 } 177 } 178