Linux Audio

Check our new training course

Loading...
v5.9
 1/*
 2 * This file is subject to the terms and conditions of the GNU General Public
 3 * License.  See the file COPYING in the main directory of this archive
 4 * for more details.
 5 */
 6
 7#undef DEBUG
 8
 9#include <linux/dma-noncoherent.h>
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/platform_device.h>
13#include <linux/scatterlist.h>
14#include <linux/slab.h>
15#include <linux/vmalloc.h>
16#include <linux/export.h>
17
18#include <asm/cacheflush.h>
19
20#if defined(CONFIG_MMU) && !defined(CONFIG_COLDFIRE)
21void arch_dma_prep_coherent(struct page *page, size_t size)
 
 
22{
23	cache_push(page_to_phys(page), size);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24}
25
26pgprot_t pgprot_dmacoherent(pgprot_t prot)
 
27{
28	if (CPU_IS_040_OR_060) {
29		pgprot_val(prot) &= ~_PAGE_CACHE040;
30		pgprot_val(prot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S;
31	} else {
32		pgprot_val(prot) |= _PAGE_NOCACHE030;
33	}
34	return prot;
35}
 
36#else
37
38#include <asm/cacheflush.h>
39
40void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
41		gfp_t gfp, unsigned long attrs)
42{
43	void *ret;
 
 
44
45	if (dev == NULL || (*dev->dma_mask < 0xffffffff))
46		gfp |= GFP_DMA;
47	ret = (void *)__get_free_pages(gfp, get_order(size));
48
49	if (ret != NULL) {
50		memset(ret, 0, size);
51		*dma_handle = virt_to_phys(ret);
52	}
53	return ret;
54}
55
56void arch_dma_free(struct device *dev, size_t size, void *vaddr,
57		dma_addr_t dma_handle, unsigned long attrs)
58{
59	free_pages((unsigned long)vaddr, get_order(size));
60}
61
62#endif /* CONFIG_MMU && !CONFIG_COLDFIRE */
63
64void arch_sync_dma_for_device(phys_addr_t handle, size_t size,
65		enum dma_data_direction dir)
 
 
 
66{
67	switch (dir) {
68	case DMA_BIDIRECTIONAL:
69	case DMA_TO_DEVICE:
70		cache_push(handle, size);
71		break;
72	case DMA_FROM_DEVICE:
73		cache_clear(handle, size);
74		break;
75	default:
76		pr_err_ratelimited("dma_sync_single_for_device: unsupported dir %u\n",
77				   dir);
78		break;
79	}
80}
v3.15
  1/*
  2 * This file is subject to the terms and conditions of the GNU General Public
  3 * License.  See the file COPYING in the main directory of this archive
  4 * for more details.
  5 */
  6
  7#undef DEBUG
  8
  9#include <linux/dma-mapping.h>
 10#include <linux/device.h>
 11#include <linux/kernel.h>
 
 12#include <linux/scatterlist.h>
 13#include <linux/slab.h>
 14#include <linux/vmalloc.h>
 15#include <linux/export.h>
 16
 17#include <asm/pgalloc.h>
 18
 19#if defined(CONFIG_MMU) && !defined(CONFIG_COLDFIRE)
 20
 21void *dma_alloc_coherent(struct device *dev, size_t size,
 22			 dma_addr_t *handle, gfp_t flag)
 23{
 24	struct page *page, **map;
 25	pgprot_t pgprot;
 26	void *addr;
 27	int i, order;
 28
 29	pr_debug("dma_alloc_coherent: %d,%x\n", size, flag);
 30
 31	size = PAGE_ALIGN(size);
 32	order = get_order(size);
 33
 34	page = alloc_pages(flag, order);
 35	if (!page)
 36		return NULL;
 37
 38	*handle = page_to_phys(page);
 39	map = kmalloc(sizeof(struct page *) << order, flag & ~__GFP_DMA);
 40	if (!map) {
 41		__free_pages(page, order);
 42		return NULL;
 43	}
 44	split_page(page, order);
 45
 46	order = 1 << order;
 47	size >>= PAGE_SHIFT;
 48	map[0] = page;
 49	for (i = 1; i < size; i++)
 50		map[i] = page + i;
 51	for (; i < order; i++)
 52		__free_page(page + i);
 53	pgprot = __pgprot(_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_DIRTY);
 54	if (CPU_IS_040_OR_060)
 55		pgprot_val(pgprot) |= _PAGE_GLOBAL040 | _PAGE_NOCACHE_S;
 56	else
 57		pgprot_val(pgprot) |= _PAGE_NOCACHE030;
 58	addr = vmap(map, size, VM_MAP, pgprot);
 59	kfree(map);
 60
 61	return addr;
 62}
 63
 64void dma_free_coherent(struct device *dev, size_t size,
 65		       void *addr, dma_addr_t handle)
 66{
 67	pr_debug("dma_free_coherent: %p, %x\n", addr, handle);
 68	vfree(addr);
 
 
 
 
 
 69}
 70
 71#else
 72
 73#include <asm/cacheflush.h>
 74
 75void *dma_alloc_coherent(struct device *dev, size_t size,
 76			   dma_addr_t *dma_handle, gfp_t gfp)
 77{
 78	void *ret;
 79	/* ignore region specifiers */
 80	gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
 81
 82	if (dev == NULL || (*dev->dma_mask < 0xffffffff))
 83		gfp |= GFP_DMA;
 84	ret = (void *)__get_free_pages(gfp, get_order(size));
 85
 86	if (ret != NULL) {
 87		memset(ret, 0, size);
 88		*dma_handle = virt_to_phys(ret);
 89	}
 90	return ret;
 91}
 92
 93void dma_free_coherent(struct device *dev, size_t size,
 94			 void *vaddr, dma_addr_t dma_handle)
 95{
 96	free_pages((unsigned long)vaddr, get_order(size));
 97}
 98
 99#endif /* CONFIG_MMU && !CONFIG_COLDFIRE */
100
101EXPORT_SYMBOL(dma_alloc_coherent);
102EXPORT_SYMBOL(dma_free_coherent);
103
104void dma_sync_single_for_device(struct device *dev, dma_addr_t handle,
105				size_t size, enum dma_data_direction dir)
106{
107	switch (dir) {
108	case DMA_BIDIRECTIONAL:
109	case DMA_TO_DEVICE:
110		cache_push(handle, size);
111		break;
112	case DMA_FROM_DEVICE:
113		cache_clear(handle, size);
114		break;
115	default:
116		if (printk_ratelimit())
117			printk("dma_sync_single_for_device: unsupported dir %u\n", dir);
118		break;
119	}
120}
121EXPORT_SYMBOL(dma_sync_single_for_device);
122
123void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
124			    enum dma_data_direction dir)
125{
126	int i;
127
128	for (i = 0; i < nents; sg++, i++)
129		dma_sync_single_for_device(dev, sg->dma_address, sg->length, dir);
130}
131EXPORT_SYMBOL(dma_sync_sg_for_device);
132
133dma_addr_t dma_map_single(struct device *dev, void *addr, size_t size,
134			  enum dma_data_direction dir)
135{
136	dma_addr_t handle = virt_to_bus(addr);
137
138	dma_sync_single_for_device(dev, handle, size, dir);
139	return handle;
140}
141EXPORT_SYMBOL(dma_map_single);
142
143dma_addr_t dma_map_page(struct device *dev, struct page *page,
144			unsigned long offset, size_t size,
145			enum dma_data_direction dir)
146{
147	dma_addr_t handle = page_to_phys(page) + offset;
148
149	dma_sync_single_for_device(dev, handle, size, dir);
150	return handle;
151}
152EXPORT_SYMBOL(dma_map_page);
153
154int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
155	       enum dma_data_direction dir)
156{
157	int i;
158
159	for (i = 0; i < nents; sg++, i++) {
160		sg->dma_address = sg_phys(sg);
161		dma_sync_single_for_device(dev, sg->dma_address, sg->length, dir);
162	}
163	return nents;
164}
165EXPORT_SYMBOL(dma_map_sg);