2012-05-02 01:06:22 -06:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file COPYING in the main directory of this archive
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/export.h>
|
|
|
|
|
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
|
2012-06-26 05:02:54 -06:00
|
|
|
#if defined(CONFIG_MMU) && !defined(CONFIG_COLDFIRE)
|
2012-05-02 01:06:22 -06:00
|
|
|
|
2016-01-20 16:01:53 -07:00
|
|
|
static void *m68k_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
|
2016-08-03 14:46:00 -06:00
|
|
|
gfp_t flag, unsigned long attrs)
|
2012-05-02 01:06:22 -06:00
|
|
|
{
|
|
|
|
struct page *page, **map;
|
|
|
|
pgprot_t pgprot;
|
|
|
|
void *addr;
|
|
|
|
int i, order;
|
|
|
|
|
|
|
|
pr_debug("dma_alloc_coherent: %d,%x\n", size, flag);
|
|
|
|
|
|
|
|
size = PAGE_ALIGN(size);
|
|
|
|
order = get_order(size);
|
|
|
|
|
|
|
|
page = alloc_pages(flag, order);
|
|
|
|
if (!page)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*handle = page_to_phys(page);
|
|
|
|
map = kmalloc(sizeof(struct page *) << order, flag & ~__GFP_DMA);
|
|
|
|
if (!map) {
|
|
|
|
__free_pages(page, order);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
split_page(page, order);
|
|
|
|
|
|
|
|
order = 1 << order;
|
|
|
|
size >>= PAGE_SHIFT;
|
|
|
|
map[0] = page;
|
|
|
|
for (i = 1; i < size; i++)
|
|
|
|
map[i] = page + i;
|
|
|
|
for (; i < order; i++)
|
|
|
|
__free_page(page + i);
|
|
|
|
pgprot = __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY);
|
|
|
|
if (CPU_IS_040_OR_060)
|
|
|
|
pgprot_val(pgprot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S;
|
|
|
|
else
|
|
|
|
pgprot_val(pgprot) |= _PAGE_NOCACHE030;
|
|
|
|
addr = vmap(map, size, VM_MAP, pgprot);
|
|
|
|
kfree(map);
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2016-01-20 16:01:53 -07:00
|
|
|
static void m68k_dma_free(struct device *dev, size_t size, void *addr,
|
2016-08-03 14:46:00 -06:00
|
|
|
dma_addr_t handle, unsigned long attrs)
|
2012-05-02 01:06:22 -06:00
|
|
|
{
|
|
|
|
pr_debug("dma_free_coherent: %p, %x\n", addr, handle);
|
|
|
|
vfree(addr);
|
|
|
|
}
|
|
|
|
|
2011-03-21 21:39:27 -06:00
|
|
|
#else
|
2012-05-02 01:06:22 -06:00
|
|
|
|
|
|
|
#include <asm/cacheflush.h>
|
|
|
|
|
2016-01-20 16:01:53 -07:00
|
|
|
static void *m68k_dma_alloc(struct device *dev, size_t size,
|
2016-08-03 14:46:00 -06:00
|
|
|
dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
|
2012-05-02 01:06:22 -06:00
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
/* ignore region specifiers */
|
|
|
|
gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
|
|
|
|
|
|
|
|
if (dev == NULL || (*dev->dma_mask < 0xffffffff))
|
|
|
|
gfp |= GFP_DMA;
|
|
|
|
ret = (void *)__get_free_pages(gfp, get_order(size));
|
|
|
|
|
|
|
|
if (ret != NULL) {
|
|
|
|
memset(ret, 0, size);
|
|
|
|
*dma_handle = virt_to_phys(ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-20 16:01:53 -07:00
|
|
|
static void m68k_dma_free(struct device *dev, size_t size, void *vaddr,
|
2016-08-03 14:46:00 -06:00
|
|
|
dma_addr_t dma_handle, unsigned long attrs)
|
2012-05-02 01:06:22 -06:00
|
|
|
{
|
|
|
|
free_pages((unsigned long)vaddr, get_order(size));
|
|
|
|
}
|
|
|
|
|
2012-06-26 05:02:54 -06:00
|
|
|
#endif /* CONFIG_MMU && !CONFIG_COLDFIRE */
|
2012-05-02 01:06:22 -06:00
|
|
|
|
2016-01-20 16:01:53 -07:00
|
|
|
static void m68k_dma_sync_single_for_device(struct device *dev,
|
|
|
|
dma_addr_t handle, size_t size, enum dma_data_direction dir)
|
2012-05-02 01:06:22 -06:00
|
|
|
{
|
|
|
|
switch (dir) {
|
2012-07-09 21:50:58 -06:00
|
|
|
case DMA_BIDIRECTIONAL:
|
2012-05-02 01:06:22 -06:00
|
|
|
case DMA_TO_DEVICE:
|
|
|
|
cache_push(handle, size);
|
|
|
|
break;
|
|
|
|
case DMA_FROM_DEVICE:
|
|
|
|
cache_clear(handle, size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (printk_ratelimit())
|
|
|
|
printk("dma_sync_single_for_device: unsupported dir %u\n", dir);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 16:01:53 -07:00
|
|
|
static void m68k_dma_sync_sg_for_device(struct device *dev,
|
|
|
|
struct scatterlist *sglist, int nents, enum dma_data_direction dir)
|
2012-05-02 01:06:22 -06:00
|
|
|
{
|
|
|
|
int i;
|
2015-05-01 00:47:22 -06:00
|
|
|
struct scatterlist *sg;
|
2012-05-02 01:06:22 -06:00
|
|
|
|
2015-05-01 00:47:22 -06:00
|
|
|
for_each_sg(sglist, sg, nents, i) {
|
|
|
|
dma_sync_single_for_device(dev, sg->dma_address, sg->length,
|
|
|
|
dir);
|
|
|
|
}
|
2012-05-02 01:06:22 -06:00
|
|
|
}
|
|
|
|
|
2016-01-20 16:01:53 -07:00
|
|
|
static dma_addr_t m68k_dma_map_page(struct device *dev, struct page *page,
|
|
|
|
unsigned long offset, size_t size, enum dma_data_direction dir,
|
2016-08-03 14:46:00 -06:00
|
|
|
unsigned long attrs)
|
2012-05-02 01:06:22 -06:00
|
|
|
{
|
|
|
|
dma_addr_t handle = page_to_phys(page) + offset;
|
|
|
|
|
|
|
|
dma_sync_single_for_device(dev, handle, size, dir);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-01-20 16:01:53 -07:00
|
|
|
static int m68k_dma_map_sg(struct device *dev, struct scatterlist *sglist,
|
2016-08-03 14:46:00 -06:00
|
|
|
int nents, enum dma_data_direction dir, unsigned long attrs)
|
2012-05-02 01:06:22 -06:00
|
|
|
{
|
|
|
|
int i;
|
2015-05-01 00:47:22 -06:00
|
|
|
struct scatterlist *sg;
|
2012-05-02 01:06:22 -06:00
|
|
|
|
2015-05-01 00:47:22 -06:00
|
|
|
for_each_sg(sglist, sg, nents, i) {
|
2012-05-02 01:06:22 -06:00
|
|
|
sg->dma_address = sg_phys(sg);
|
2015-05-01 00:47:22 -06:00
|
|
|
dma_sync_single_for_device(dev, sg->dma_address, sg->length,
|
|
|
|
dir);
|
2012-05-02 01:06:22 -06:00
|
|
|
}
|
|
|
|
return nents;
|
|
|
|
}
|
2016-01-20 16:01:53 -07:00
|
|
|
|
|
|
|
struct dma_map_ops m68k_dma_ops = {
|
|
|
|
.alloc = m68k_dma_alloc,
|
|
|
|
.free = m68k_dma_free,
|
|
|
|
.map_page = m68k_dma_map_page,
|
|
|
|
.map_sg = m68k_dma_map_sg,
|
|
|
|
.sync_single_for_device = m68k_dma_sync_single_for_device,
|
|
|
|
.sync_sg_for_device = m68k_dma_sync_sg_for_device,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL(m68k_dma_ops);
|