2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* arch/sh/mm/consistent.c
|
|
|
|
*
|
2007-11-11 16:07:06 +08:00
|
|
|
* Copyright (C) 2004 - 2007 Paul Mundt
|
2005-04-17 06:20:36 +08: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.
|
|
|
|
*/
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
2006-09-27 14:13:36 +08:00
|
|
|
#include <asm/cacheflush.h>
|
|
|
|
#include <asm/addrspace.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/io.h>
|
|
|
|
|
2005-10-21 15:21:38 +08:00
|
|
|
void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *handle)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct page *page, *end, *free;
|
2007-11-11 16:07:06 +08:00
|
|
|
void *ret, *vp;
|
2005-04-17 06:20:36 +08:00
|
|
|
int order;
|
|
|
|
|
|
|
|
size = PAGE_ALIGN(size);
|
|
|
|
order = get_order(size);
|
|
|
|
|
|
|
|
page = alloc_pages(gfp, order);
|
|
|
|
if (!page)
|
|
|
|
return NULL;
|
2006-03-22 16:08:05 +08:00
|
|
|
split_page(page, order);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
ret = page_address(page);
|
|
|
|
*handle = virt_to_phys(ret);
|
|
|
|
|
2007-11-11 16:07:06 +08:00
|
|
|
vp = ioremap_nocache(*handle, size);
|
|
|
|
if (!vp) {
|
|
|
|
free_pages((unsigned long)ret, order);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(vp, 0, size);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* We must flush the cache before we pass it on to the device
|
|
|
|
*/
|
2007-11-11 16:07:06 +08:00
|
|
|
dma_cache_sync(NULL, ret, size, DMA_BIDIRECTIONAL);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
page = virt_to_page(ret);
|
|
|
|
free = page + (size >> PAGE_SHIFT);
|
|
|
|
end = page + (1 << order);
|
|
|
|
|
|
|
|
while (++page < end) {
|
|
|
|
/* Free any unused pages */
|
|
|
|
if (page >= free) {
|
|
|
|
__free_page(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-11 16:07:06 +08:00
|
|
|
return vp;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-11-11 16:07:06 +08:00
|
|
|
EXPORT_SYMBOL(consistent_alloc);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-11-11 16:07:06 +08:00
|
|
|
void consistent_free(void *vaddr, size_t size, dma_addr_t dma_handle)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-11-11 16:07:06 +08:00
|
|
|
struct page *page;
|
|
|
|
unsigned long addr;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-11-11 16:07:06 +08:00
|
|
|
addr = (unsigned long)phys_to_virt((unsigned long)dma_handle);
|
|
|
|
page = virt_to_page(addr);
|
|
|
|
|
|
|
|
free_pages(addr, get_order(size));
|
|
|
|
|
|
|
|
iounmap(vaddr);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-11-11 16:07:06 +08:00
|
|
|
EXPORT_SYMBOL(consistent_free);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
void consistent_sync(void *vaddr, size_t size, int direction)
|
|
|
|
{
|
2007-11-11 16:07:06 +08:00
|
|
|
#ifdef CONFIG_CPU_SH5
|
|
|
|
void *p1addr = vaddr;
|
|
|
|
#else
|
|
|
|
void *p1addr = (void*) P1SEGADDR((unsigned long)vaddr);
|
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
switch (direction) {
|
|
|
|
case DMA_FROM_DEVICE: /* invalidate only */
|
2007-10-17 14:29:42 +08:00
|
|
|
__flush_invalidate_region(p1addr, size);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
case DMA_TO_DEVICE: /* writeback only */
|
2007-10-17 14:29:42 +08:00
|
|
|
__flush_wback_region(p1addr, size);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
case DMA_BIDIRECTIONAL: /* writeback and invalidate */
|
2007-10-17 14:29:42 +08:00
|
|
|
__flush_purge_region(p1addr, size);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(consistent_sync);
|