blob: 9f8ea3ada4dbe845d6ff5dde86267471022e8afc [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 +090019void *dma_alloc_coherent(struct device *dev, size_t size,
20 dma_addr_t *dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Magnus Damm2a3eeba2008-01-25 12:42:48 +090022 void *ret, *ret_nocache;
Magnus Dammf93e97e2008-01-24 18:35:10 +090023 int order = get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Dmitry Baryshkov9de90ac2008-07-18 13:30:31 +040025 if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
26 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Magnus Dammf93e97e2008-01-24 18:35:10 +090028 ret = (void *)__get_free_pages(gfp, order);
Magnus Damm2a3eeba2008-01-25 12:42:48 +090029 if (!ret)
30 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Magnus Damm2a3eeba2008-01-25 12:42:48 +090032 memset(ret, 0, size);
33 /*
34 * Pages from the page allocator may have data present in
35 * cache. So flush the cache before using uncached memory.
36 */
37 dma_cache_sync(dev, ret, size, DMA_BIDIRECTIONAL);
38
Paul Mundtfa439722008-09-04 18:53:58 +090039 ret_nocache = (void __force *)ioremap_nocache(virt_to_phys(ret), size);
Magnus Damm2a3eeba2008-01-25 12:42:48 +090040 if (!ret_nocache) {
41 free_pages((unsigned long)ret, order);
42 return NULL;
Magnus Dammf93e97e2008-01-24 18:35:10 +090043 }
Magnus Damm2a3eeba2008-01-25 12:42:48 +090044
45 *dma_handle = virt_to_phys(ret);
46 return ret_nocache;
Magnus Dammf93e97e2008-01-24 18:35:10 +090047}
48EXPORT_SYMBOL(dma_alloc_coherent);
49
50void dma_free_coherent(struct device *dev, size_t size,
51 void *vaddr, dma_addr_t dma_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Magnus Dammf93e97e2008-01-24 18:35:10 +090053 int order = get_order(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dmitry Baryshkov9de90ac2008-07-18 13:30:31 +040055 if (!dma_release_from_coherent(dev, order, vaddr)) {
Magnus Dammf93e97e2008-01-24 18:35:10 +090056 WARN_ON(irqs_disabled()); /* for portability */
Magnus Damm2a3eeba2008-01-25 12:42:48 +090057 free_pages((unsigned long)phys_to_virt(dma_handle), order);
58 iounmap(vaddr);
Magnus Dammf93e97e2008-01-24 18:35:10 +090059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
Magnus Dammf93e97e2008-01-24 18:35:10 +090061EXPORT_SYMBOL(dma_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Magnus Dammf93e97e2008-01-24 18:35:10 +090063void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
64 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Paul Mundt8a7bcf02007-11-11 17:07:06 +090066#ifdef CONFIG_CPU_SH5
67 void *p1addr = vaddr;
68#else
69 void *p1addr = (void*) P1SEGADDR((unsigned long)vaddr);
70#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 switch (direction) {
73 case DMA_FROM_DEVICE: /* invalidate only */
Ralf Baechle622a9ed2007-10-16 23:29:42 -070074 __flush_invalidate_region(p1addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 break;
76 case DMA_TO_DEVICE: /* writeback only */
Ralf Baechle622a9ed2007-10-16 23:29:42 -070077 __flush_wback_region(p1addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
Ralf Baechle622a9ed2007-10-16 23:29:42 -070080 __flush_purge_region(p1addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 break;
82 default:
83 BUG();
84 }
85}
Magnus Dammf93e97e2008-01-24 18:35:10 +090086EXPORT_SYMBOL(dma_cache_sync);
Magnus Damm1eca5c92008-07-16 19:02:54 +090087
Magnus Damm0c13bf12008-08-11 15:13:24 +090088static int __init memchunk_setup(char *str)
89{
90 return 1; /* accept anything that begins with "memchunk." */
91}
92__setup("memchunk.", memchunk_setup);
93
Magnus Dammc773d8a2008-08-27 18:21:29 +090094static void __init memchunk_cmdline_override(char *name, unsigned long *sizep)
Magnus Damm0c13bf12008-08-11 15:13:24 +090095{
96 char *p = boot_command_line;
97 int k = strlen(name);
98
99 while ((p = strstr(p, "memchunk."))) {
100 p += 9; /* strlen("memchunk.") */
101 if (!strncmp(name, p, k) && p[k] == '=') {
102 p += k + 1;
103 *sizep = memparse(p, NULL);
104 pr_info("%s: forcing memory chunk size to 0x%08lx\n",
105 name, *sizep);
106 break;
107 }
108 }
109}
110
Magnus Dammc773d8a2008-08-27 18:21:29 +0900111int __init platform_resource_setup_memory(struct platform_device *pdev,
112 char *name, unsigned long memsize)
Magnus Damm1eca5c92008-07-16 19:02:54 +0900113{
114 struct resource *r;
115 dma_addr_t dma_handle;
116 void *buf;
117
118 r = pdev->resource + pdev->num_resources - 1;
119 if (r->flags) {
120 pr_warning("%s: unable to find empty space for resource\n",
121 name);
122 return -EINVAL;
123 }
124
Magnus Damm0c13bf12008-08-11 15:13:24 +0900125 memchunk_cmdline_override(name, &memsize);
126 if (!memsize)
127 return 0;
128
Magnus Damm1eca5c92008-07-16 19:02:54 +0900129 buf = dma_alloc_coherent(NULL, memsize, &dma_handle, GFP_KERNEL);
130 if (!buf) {
131 pr_warning("%s: unable to allocate memory\n", name);
132 return -ENOMEM;
133 }
134
135 memset(buf, 0, memsize);
136
137 r->flags = IORESOURCE_MEM;
138 r->start = dma_handle;
139 r->end = r->start + memsize - 1;
140 r->name = name;
141 return 0;
142}