xref: /linux/arch/sh/mm/consistent.c (revision 856e7c4b619af622d56b3b454f7bec32a170ac99)
1 /*
2  * arch/sh/mm/consistent.c
3  *
4  * Copyright (C) 2004 - 2007  Paul Mundt
5  *
6  * Declared coherent memory functions based on arch/x86/kernel/pci-dma_32.c
7  *
8  * 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>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/dma-debug.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/gfp.h>
20 #include <asm/cacheflush.h>
21 #include <asm/addrspace.h>
22 
23 const struct dma_map_ops *dma_ops;
24 EXPORT_SYMBOL(dma_ops);
25 
26 void *dma_generic_alloc_coherent(struct device *dev, size_t size,
27 				 dma_addr_t *dma_handle, gfp_t gfp,
28 				 unsigned long attrs)
29 {
30 	void *ret, *ret_nocache;
31 	int order = get_order(size);
32 
33 	gfp |= __GFP_ZERO;
34 
35 	ret = (void *)__get_free_pages(gfp, order);
36 	if (!ret)
37 		return NULL;
38 
39 	/*
40 	 * Pages from the page allocator may have data present in
41 	 * cache. So flush the cache before using uncached memory.
42 	 */
43 	sh_sync_dma_for_device(ret, size, DMA_BIDIRECTIONAL);
44 
45 	ret_nocache = (void __force *)ioremap_nocache(virt_to_phys(ret), size);
46 	if (!ret_nocache) {
47 		free_pages((unsigned long)ret, order);
48 		return NULL;
49 	}
50 
51 	split_page(pfn_to_page(virt_to_phys(ret) >> PAGE_SHIFT), order);
52 
53 	*dma_handle = virt_to_phys(ret);
54 	if (!WARN_ON(!dev))
55 		*dma_handle -= PFN_PHYS(dev->dma_pfn_offset);
56 
57 	return ret_nocache;
58 }
59 
60 void dma_generic_free_coherent(struct device *dev, size_t size,
61 			       void *vaddr, dma_addr_t dma_handle,
62 			       unsigned long attrs)
63 {
64 	int order = get_order(size);
65 	unsigned long pfn = dma_handle >> PAGE_SHIFT;
66 	int k;
67 
68 	if (!WARN_ON(!dev))
69 		pfn += dev->dma_pfn_offset;
70 
71 	for (k = 0; k < (1 << order); k++)
72 		__free_pages(pfn_to_page(pfn + k), 0);
73 
74 	iounmap(vaddr);
75 }
76 
77 void sh_sync_dma_for_device(void *vaddr, size_t size,
78 		    enum dma_data_direction direction)
79 {
80 	void *addr;
81 
82 	addr = __in_29bit_mode() ?
83 	       (void *)CAC_ADDR((unsigned long)vaddr) : vaddr;
84 
85 	switch (direction) {
86 	case DMA_FROM_DEVICE:		/* invalidate only */
87 		__flush_invalidate_region(addr, size);
88 		break;
89 	case DMA_TO_DEVICE:		/* writeback only */
90 		__flush_wback_region(addr, size);
91 		break;
92 	case DMA_BIDIRECTIONAL:		/* writeback and invalidate */
93 		__flush_purge_region(addr, size);
94 		break;
95 	default:
96 		BUG();
97 	}
98 }
99 EXPORT_SYMBOL(sh_sync_dma_for_device);
100 
101 static int __init memchunk_setup(char *str)
102 {
103 	return 1; /* accept anything that begins with "memchunk." */
104 }
105 __setup("memchunk.", memchunk_setup);
106 
107 static void __init memchunk_cmdline_override(char *name, unsigned long *sizep)
108 {
109 	char *p = boot_command_line;
110 	int k = strlen(name);
111 
112 	while ((p = strstr(p, "memchunk."))) {
113 		p += 9; /* strlen("memchunk.") */
114 		if (!strncmp(name, p, k) && p[k] == '=') {
115 			p += k + 1;
116 			*sizep = memparse(p, NULL);
117 			pr_info("%s: forcing memory chunk size to 0x%08lx\n",
118 				name, *sizep);
119 			break;
120 		}
121 	}
122 }
123 
124 int __init platform_resource_setup_memory(struct platform_device *pdev,
125 					  char *name, unsigned long memsize)
126 {
127 	struct resource *r;
128 	dma_addr_t dma_handle;
129 	void *buf;
130 
131 	r = pdev->resource + pdev->num_resources - 1;
132 	if (r->flags) {
133 		pr_warning("%s: unable to find empty space for resource\n",
134 			name);
135 		return -EINVAL;
136 	}
137 
138 	memchunk_cmdline_override(name, &memsize);
139 	if (!memsize)
140 		return 0;
141 
142 	buf = dma_alloc_coherent(&pdev->dev, memsize, &dma_handle, GFP_KERNEL);
143 	if (!buf) {
144 		pr_warning("%s: unable to allocate memory\n", name);
145 		return -ENOMEM;
146 	}
147 
148 	memset(buf, 0, memsize);
149 
150 	r->flags = IORESOURCE_MEM;
151 	r->start = dma_handle;
152 	r->end = r->start + memsize - 1;
153 	r->name = name;
154 	return 0;
155 }
156