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