blob: b2ce014401b5516ed8ae51c4ca98cf5d0c4a026e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/sh/mm/consistent.c
3 *
Paul Mundt8a7bcf02007-11-11 17:07:06 +09004 * Copyright (C) 2004 - 2007 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Magnus Dammf93e97e2008-01-24 18:35:10 +09006 * Declared coherent memory functions based on arch/x86/kernel/pci-dma_32.c
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
11 */
12#include <linux/mm.h>
Magnus Damm1eca5c92008-07-16 19:02:54 +090013#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/dma-mapping.h>
Paul Mundt26ff6c12006-09-27 15:13:36 +090015#include <asm/cacheflush.h>
16#include <asm/addrspace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/io.h>
18
Magnus Dammf93e97e2008-01-24 18:35:10 +090019struct dma_coherent_mem {
20 void *virt_base;
21 u32 device_base;
22 int size;
23 int flags;
24 unsigned long *bitmap;
25};
26
27void *dma_alloc_coherent(struct device *dev, size_t size,
28 dma_addr_t *dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Magnus Damm2a3eeba2008-01-25 12:42:48 +090030 void *ret, *ret_nocache;
Magnus Dammf93e97e2008-01-24 18:35:10 +090031 int order = get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Dmitry Baryshkov9de90ac2008-07-18 13:30:31 +040033 if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
34 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Magnus Dammf93e97e2008-01-24 18:35:10 +090036 ret = (void *)__get_free_pages(gfp, order);
Magnus Damm2a3eeba2008-01-25 12:42:48 +090037 if (!ret)
38 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Magnus Damm2a3eeba2008-01-25 12:42:48 +090040 memset(ret, 0, size);
41 /*
42 * Pages from the page allocator may have data present in
43 * cache. So flush the cache before using uncached memory.
44 */
45 dma_cache_sync(dev, ret, size, DMA_BIDIRECTIONAL);
46
47 ret_nocache = ioremap_nocache(virt_to_phys(ret), size);
48 if (!ret_nocache) {
49 free_pages((unsigned long)ret, order);
50 return NULL;
Magnus Dammf93e97e2008-01-24 18:35:10 +090051 }
Magnus Damm2a3eeba2008-01-25 12:42:48 +090052
53 *dma_handle = virt_to_phys(ret);
54 return ret_nocache;
Magnus Dammf93e97e2008-01-24 18:35:10 +090055}
56EXPORT_SYMBOL(dma_alloc_coherent);
57
58void dma_free_coherent(struct device *dev, size_t size,
59 void *vaddr, dma_addr_t dma_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Magnus Dammf93e97e2008-01-24 18:35:10 +090061 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
62 int order = get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Dmitry Baryshkov9de90ac2008-07-18 13:30:31 +040064 if (!dma_release_from_coherent(dev, order, vaddr)) {
Magnus Dammf93e97e2008-01-24 18:35:10 +090065 WARN_ON(irqs_disabled()); /* for portability */
66 BUG_ON(mem && mem->flags & DMA_MEMORY_EXCLUSIVE);
Magnus Damm2a3eeba2008-01-25 12:42:48 +090067 free_pages((unsigned long)phys_to_virt(dma_handle), order);
68 iounmap(vaddr);
Magnus Dammf93e97e2008-01-24 18:35:10 +090069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
Magnus Dammf93e97e2008-01-24 18:35:10 +090071EXPORT_SYMBOL(dma_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Magnus Dammf93e97e2008-01-24 18:35:10 +090073void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
74 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Paul Mundt8a7bcf02007-11-11 17:07:06 +090076#ifdef CONFIG_CPU_SH5
77 void *p1addr = vaddr;
78#else
79 void *p1addr = (void*) P1SEGADDR((unsigned long)vaddr);
80#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 switch (direction) {
83 case DMA_FROM_DEVICE: /* invalidate only */
Ralf Baechle622a9ed2007-10-16 23:29:42 -070084 __flush_invalidate_region(p1addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 break;
86 case DMA_TO_DEVICE: /* writeback only */
Ralf Baechle622a9ed2007-10-16 23:29:42 -070087 __flush_wback_region(p1addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 break;
89 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
Ralf Baechle622a9ed2007-10-16 23:29:42 -070090 __flush_purge_region(p1addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 break;
92 default:
93 BUG();
94 }
95}
Magnus Dammf93e97e2008-01-24 18:35:10 +090096EXPORT_SYMBOL(dma_cache_sync);
Magnus Damm1eca5c92008-07-16 19:02:54 +090097
98int platform_resource_setup_memory(struct platform_device *pdev,
99 char *name, unsigned long memsize)
100{
101 struct resource *r;
102 dma_addr_t dma_handle;
103 void *buf;
104
105 r = pdev->resource + pdev->num_resources - 1;
106 if (r->flags) {
107 pr_warning("%s: unable to find empty space for resource\n",
108 name);
109 return -EINVAL;
110 }
111
112 buf = dma_alloc_coherent(NULL, memsize, &dma_handle, GFP_KERNEL);
113 if (!buf) {
114 pr_warning("%s: unable to allocate memory\n", name);
115 return -ENOMEM;
116 }
117
118 memset(buf, 0, memsize);
119
120 r->flags = IORESOURCE_MEM;
121 r->start = dma_handle;
122 r->end = r->start + memsize - 1;
123 r->name = name;
124 return 0;
125}