blob: 38c82d890ffda6a5cb332ad3b387bde0476dc551 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/sh/mm/consistent.c
3 *
4 * Copyright (C) 2004 Paul Mundt
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
10#include <linux/mm.h>
11#include <linux/dma-mapping.h>
Paul Mundt26ff6c12006-09-27 15:13:36 +090012#include <asm/cacheflush.h>
13#include <asm/addrspace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/io.h>
15
Al Viro6dae2c22005-10-21 03:21:38 -040016void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
18 struct page *page, *end, *free;
19 void *ret;
20 int order;
21
22 size = PAGE_ALIGN(size);
23 order = get_order(size);
24
25 page = alloc_pages(gfp, order);
26 if (!page)
27 return NULL;
Nick Piggin8dfcc9b2006-03-22 00:08:05 -080028 split_page(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 ret = page_address(page);
Paul Mundt833abf72006-10-10 18:33:10 +090031 memset(ret, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *handle = virt_to_phys(ret);
33
34 /*
35 * We must flush the cache before we pass it on to the device
36 */
37 dma_cache_wback_inv(ret, size);
38
39 page = virt_to_page(ret);
40 free = page + (size >> PAGE_SHIFT);
41 end = page + (1 << order);
42
43 while (++page < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 /* Free any unused pages */
45 if (page >= free) {
46 __free_page(page);
47 }
48 }
49
50 return P2SEGADDR(ret);
51}
52
53void consistent_free(void *vaddr, size_t size)
54{
55 unsigned long addr = P1SEGADDR((unsigned long)vaddr);
56 struct page *page=virt_to_page(addr);
57 int num_pages=(size+PAGE_SIZE-1) >> PAGE_SHIFT;
58 int i;
59
60 for(i=0;i<num_pages;i++) {
61 __free_page((page+i));
62 }
63}
64
65void consistent_sync(void *vaddr, size_t size, int direction)
66{
67 void * p1addr = (void*) P1SEGADDR((unsigned long)vaddr);
68
69 switch (direction) {
70 case DMA_FROM_DEVICE: /* invalidate only */
71 dma_cache_inv(p1addr, size);
72 break;
73 case DMA_TO_DEVICE: /* writeback only */
74 dma_cache_wback(p1addr, size);
75 break;
76 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
77 dma_cache_wback_inv(p1addr, size);
78 break;
79 default:
80 BUG();
81 }
82}
83
84EXPORT_SYMBOL(consistent_alloc);
85EXPORT_SYMBOL(consistent_free);
86EXPORT_SYMBOL(consistent_sync);
87