blob: 895bb3f335c77f39481191e54739e8871674f7a1 [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
Magnus Damm0c13bf12008-08-11 15:13:24 +090098static int __init memchunk_setup(char *str)
99{
100 return 1; /* accept anything that begins with "memchunk." */
101}
102__setup("memchunk.", memchunk_setup);
103
104static void memchunk_cmdline_override(char *name, unsigned long *sizep)
105{
106 char *p = boot_command_line;
107 int k = strlen(name);
108
109 while ((p = strstr(p, "memchunk."))) {
110 p += 9; /* strlen("memchunk.") */
111 if (!strncmp(name, p, k) && p[k] == '=') {
112 p += k + 1;
113 *sizep = memparse(p, NULL);
114 pr_info("%s: forcing memory chunk size to 0x%08lx\n",
115 name, *sizep);
116 break;
117 }
118 }
119}
120
Magnus Damm1eca5c92008-07-16 19:02:54 +0900121int platform_resource_setup_memory(struct platform_device *pdev,
122 char *name, unsigned long memsize)
123{
124 struct resource *r;
125 dma_addr_t dma_handle;
126 void *buf;
127
128 r = pdev->resource + pdev->num_resources - 1;
129 if (r->flags) {
130 pr_warning("%s: unable to find empty space for resource\n",
131 name);
132 return -EINVAL;
133 }
134
Magnus Damm0c13bf12008-08-11 15:13:24 +0900135 memchunk_cmdline_override(name, &memsize);
136 if (!memsize)
137 return 0;
138
Magnus Damm1eca5c92008-07-16 19:02:54 +0900139 buf = dma_alloc_coherent(NULL, memsize, &dma_handle, GFP_KERNEL);
140 if (!buf) {
141 pr_warning("%s: unable to allocate memory\n", name);
142 return -ENOMEM;
143 }
144
145 memset(buf, 0, memsize);
146
147 r->flags = IORESOURCE_MEM;
148 r->start = dma_handle;
149 r->end = r->start + memsize - 1;
150 r->name = name;
151 return 0;
152}