Linux Audio

Check our new training course

Loading...
v3.15
  1/*
  2 * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
  3 *
  4 * Copyright (c) 2006  SUSE Linux Products GmbH
  5 * Copyright (c) 2006  Tejun Heo <teheo@suse.de>
  6 *
  7 * This file is released under the GPLv2.
  8 */
  9
 10#include <linux/dma-mapping.h>
 11#include <linux/export.h>
 12#include <linux/gfp.h>
 13#include <asm-generic/dma-coherent.h>
 14
 15/*
 16 * Managed DMA API
 17 */
 18struct dma_devres {
 19	size_t		size;
 20	void		*vaddr;
 21	dma_addr_t	dma_handle;
 22};
 23
 24static void dmam_coherent_release(struct device *dev, void *res)
 25{
 26	struct dma_devres *this = res;
 27
 28	dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
 29}
 30
 31static void dmam_noncoherent_release(struct device *dev, void *res)
 32{
 33	struct dma_devres *this = res;
 34
 35	dma_free_noncoherent(dev, this->size, this->vaddr, this->dma_handle);
 36}
 37
 38static int dmam_match(struct device *dev, void *res, void *match_data)
 39{
 40	struct dma_devres *this = res, *match = match_data;
 41
 42	if (this->vaddr == match->vaddr) {
 43		WARN_ON(this->size != match->size ||
 44			this->dma_handle != match->dma_handle);
 45		return 1;
 46	}
 47	return 0;
 48}
 49
 50/**
 51 * dmam_alloc_coherent - Managed dma_alloc_coherent()
 52 * @dev: Device to allocate coherent memory for
 53 * @size: Size of allocation
 54 * @dma_handle: Out argument for allocated DMA handle
 55 * @gfp: Allocation flags
 56 *
 57 * Managed dma_alloc_coherent().  Memory allocated using this function
 58 * will be automatically released on driver detach.
 59 *
 60 * RETURNS:
 61 * Pointer to allocated memory on success, NULL on failure.
 62 */
 63void * dmam_alloc_coherent(struct device *dev, size_t size,
 64			   dma_addr_t *dma_handle, gfp_t gfp)
 65{
 66	struct dma_devres *dr;
 67	void *vaddr;
 68
 69	dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
 70	if (!dr)
 71		return NULL;
 72
 73	vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
 74	if (!vaddr) {
 75		devres_free(dr);
 76		return NULL;
 77	}
 78
 79	dr->vaddr = vaddr;
 80	dr->dma_handle = *dma_handle;
 81	dr->size = size;
 82
 83	devres_add(dev, dr);
 84
 85	return vaddr;
 86}
 87EXPORT_SYMBOL(dmam_alloc_coherent);
 88
 89/**
 90 * dmam_free_coherent - Managed dma_free_coherent()
 91 * @dev: Device to free coherent memory for
 92 * @size: Size of allocation
 93 * @vaddr: Virtual address of the memory to free
 94 * @dma_handle: DMA handle of the memory to free
 95 *
 96 * Managed dma_free_coherent().
 97 */
 98void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
 99			dma_addr_t dma_handle)
100{
101	struct dma_devres match_data = { size, vaddr, dma_handle };
102
103	dma_free_coherent(dev, size, vaddr, dma_handle);
104	WARN_ON(devres_destroy(dev, dmam_coherent_release, dmam_match,
105			       &match_data));
106}
107EXPORT_SYMBOL(dmam_free_coherent);
108
109/**
110 * dmam_alloc_non_coherent - Managed dma_alloc_non_coherent()
111 * @dev: Device to allocate non_coherent memory for
112 * @size: Size of allocation
113 * @dma_handle: Out argument for allocated DMA handle
114 * @gfp: Allocation flags
115 *
116 * Managed dma_alloc_non_coherent().  Memory allocated using this
117 * function will be automatically released on driver detach.
118 *
119 * RETURNS:
120 * Pointer to allocated memory on success, NULL on failure.
121 */
122void *dmam_alloc_noncoherent(struct device *dev, size_t size,
123			     dma_addr_t *dma_handle, gfp_t gfp)
124{
125	struct dma_devres *dr;
126	void *vaddr;
127
128	dr = devres_alloc(dmam_noncoherent_release, sizeof(*dr), gfp);
129	if (!dr)
130		return NULL;
131
132	vaddr = dma_alloc_noncoherent(dev, size, dma_handle, gfp);
133	if (!vaddr) {
134		devres_free(dr);
135		return NULL;
136	}
137
138	dr->vaddr = vaddr;
139	dr->dma_handle = *dma_handle;
140	dr->size = size;
141
142	devres_add(dev, dr);
143
144	return vaddr;
145}
146EXPORT_SYMBOL(dmam_alloc_noncoherent);
147
148/**
149 * dmam_free_coherent - Managed dma_free_noncoherent()
150 * @dev: Device to free noncoherent memory for
151 * @size: Size of allocation
152 * @vaddr: Virtual address of the memory to free
153 * @dma_handle: DMA handle of the memory to free
154 *
155 * Managed dma_free_noncoherent().
156 */
157void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
158			   dma_addr_t dma_handle)
159{
160	struct dma_devres match_data = { size, vaddr, dma_handle };
161
162	dma_free_noncoherent(dev, size, vaddr, dma_handle);
163	WARN_ON(!devres_destroy(dev, dmam_noncoherent_release, dmam_match,
164				&match_data));
165}
166EXPORT_SYMBOL(dmam_free_noncoherent);
167
168#ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
169
170static void dmam_coherent_decl_release(struct device *dev, void *res)
171{
172	dma_release_declared_memory(dev);
173}
174
175/**
176 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
177 * @dev: Device to declare coherent memory for
178 * @bus_addr: Bus address of coherent memory to be declared
179 * @device_addr: Device address of coherent memory to be declared
180 * @size: Size of coherent memory to be declared
181 * @flags: Flags
182 *
183 * Managed dma_declare_coherent_memory().
184 *
185 * RETURNS:
186 * 0 on success, -errno on failure.
187 */
188int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
189				 dma_addr_t device_addr, size_t size, int flags)
190{
191	void *res;
192	int rc;
193
194	res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
195	if (!res)
196		return -ENOMEM;
197
198	rc = dma_declare_coherent_memory(dev, bus_addr, device_addr, size,
199					 flags);
200	if (rc == 0)
201		devres_add(dev, res);
202	else
203		devres_free(res);
204
205	return rc;
206}
207EXPORT_SYMBOL(dmam_declare_coherent_memory);
208
209/**
210 * dmam_release_declared_memory - Managed dma_release_declared_memory().
211 * @dev: Device to release declared coherent memory for
212 *
213 * Managed dmam_release_declared_memory().
214 */
215void dmam_release_declared_memory(struct device *dev)
216{
217	WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
218}
219EXPORT_SYMBOL(dmam_release_declared_memory);
220
221#endif
222
223/*
224 * Create scatter-list for the already allocated DMA buffer.
225 */
226int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
227		 void *cpu_addr, dma_addr_t handle, size_t size)
228{
229	struct page *page = virt_to_page(cpu_addr);
230	int ret;
231
232	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
233	if (unlikely(ret))
234		return ret;
235
236	sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
237	return 0;
238}
239EXPORT_SYMBOL(dma_common_get_sgtable);
240
241/*
242 * Create userspace mapping for the DMA-coherent memory.
243 */
244int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
245		    void *cpu_addr, dma_addr_t dma_addr, size_t size)
246{
247	int ret = -ENXIO;
248#ifdef CONFIG_MMU
249	unsigned long user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
250	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
251	unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
252	unsigned long off = vma->vm_pgoff;
253
254	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
255
256	if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
257		return ret;
258
259	if (off < count && user_count <= (count - off)) {
260		ret = remap_pfn_range(vma, vma->vm_start,
261				      pfn + off,
262				      user_count << PAGE_SHIFT,
263				      vma->vm_page_prot);
264	}
265#endif	/* CONFIG_MMU */
266
267	return ret;
268}
269EXPORT_SYMBOL(dma_common_mmap);
v3.1
  1/*
  2 * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
  3 *
  4 * Copyright (c) 2006  SUSE Linux Products GmbH
  5 * Copyright (c) 2006  Tejun Heo <teheo@suse.de>
  6 *
  7 * This file is released under the GPLv2.
  8 */
  9
 10#include <linux/dma-mapping.h>
 
 11#include <linux/gfp.h>
 
 12
 13/*
 14 * Managed DMA API
 15 */
 16struct dma_devres {
 17	size_t		size;
 18	void		*vaddr;
 19	dma_addr_t	dma_handle;
 20};
 21
 22static void dmam_coherent_release(struct device *dev, void *res)
 23{
 24	struct dma_devres *this = res;
 25
 26	dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
 27}
 28
 29static void dmam_noncoherent_release(struct device *dev, void *res)
 30{
 31	struct dma_devres *this = res;
 32
 33	dma_free_noncoherent(dev, this->size, this->vaddr, this->dma_handle);
 34}
 35
 36static int dmam_match(struct device *dev, void *res, void *match_data)
 37{
 38	struct dma_devres *this = res, *match = match_data;
 39
 40	if (this->vaddr == match->vaddr) {
 41		WARN_ON(this->size != match->size ||
 42			this->dma_handle != match->dma_handle);
 43		return 1;
 44	}
 45	return 0;
 46}
 47
 48/**
 49 * dmam_alloc_coherent - Managed dma_alloc_coherent()
 50 * @dev: Device to allocate coherent memory for
 51 * @size: Size of allocation
 52 * @dma_handle: Out argument for allocated DMA handle
 53 * @gfp: Allocation flags
 54 *
 55 * Managed dma_alloc_coherent().  Memory allocated using this function
 56 * will be automatically released on driver detach.
 57 *
 58 * RETURNS:
 59 * Pointer to allocated memory on success, NULL on failure.
 60 */
 61void * dmam_alloc_coherent(struct device *dev, size_t size,
 62			   dma_addr_t *dma_handle, gfp_t gfp)
 63{
 64	struct dma_devres *dr;
 65	void *vaddr;
 66
 67	dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
 68	if (!dr)
 69		return NULL;
 70
 71	vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
 72	if (!vaddr) {
 73		devres_free(dr);
 74		return NULL;
 75	}
 76
 77	dr->vaddr = vaddr;
 78	dr->dma_handle = *dma_handle;
 79	dr->size = size;
 80
 81	devres_add(dev, dr);
 82
 83	return vaddr;
 84}
 85EXPORT_SYMBOL(dmam_alloc_coherent);
 86
 87/**
 88 * dmam_free_coherent - Managed dma_free_coherent()
 89 * @dev: Device to free coherent memory for
 90 * @size: Size of allocation
 91 * @vaddr: Virtual address of the memory to free
 92 * @dma_handle: DMA handle of the memory to free
 93 *
 94 * Managed dma_free_coherent().
 95 */
 96void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
 97			dma_addr_t dma_handle)
 98{
 99	struct dma_devres match_data = { size, vaddr, dma_handle };
100
101	dma_free_coherent(dev, size, vaddr, dma_handle);
102	WARN_ON(devres_destroy(dev, dmam_coherent_release, dmam_match,
103			       &match_data));
104}
105EXPORT_SYMBOL(dmam_free_coherent);
106
107/**
108 * dmam_alloc_non_coherent - Managed dma_alloc_non_coherent()
109 * @dev: Device to allocate non_coherent memory for
110 * @size: Size of allocation
111 * @dma_handle: Out argument for allocated DMA handle
112 * @gfp: Allocation flags
113 *
114 * Managed dma_alloc_non_coherent().  Memory allocated using this
115 * function will be automatically released on driver detach.
116 *
117 * RETURNS:
118 * Pointer to allocated memory on success, NULL on failure.
119 */
120void *dmam_alloc_noncoherent(struct device *dev, size_t size,
121			     dma_addr_t *dma_handle, gfp_t gfp)
122{
123	struct dma_devres *dr;
124	void *vaddr;
125
126	dr = devres_alloc(dmam_noncoherent_release, sizeof(*dr), gfp);
127	if (!dr)
128		return NULL;
129
130	vaddr = dma_alloc_noncoherent(dev, size, dma_handle, gfp);
131	if (!vaddr) {
132		devres_free(dr);
133		return NULL;
134	}
135
136	dr->vaddr = vaddr;
137	dr->dma_handle = *dma_handle;
138	dr->size = size;
139
140	devres_add(dev, dr);
141
142	return vaddr;
143}
144EXPORT_SYMBOL(dmam_alloc_noncoherent);
145
146/**
147 * dmam_free_coherent - Managed dma_free_noncoherent()
148 * @dev: Device to free noncoherent memory for
149 * @size: Size of allocation
150 * @vaddr: Virtual address of the memory to free
151 * @dma_handle: DMA handle of the memory to free
152 *
153 * Managed dma_free_noncoherent().
154 */
155void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
156			   dma_addr_t dma_handle)
157{
158	struct dma_devres match_data = { size, vaddr, dma_handle };
159
160	dma_free_noncoherent(dev, size, vaddr, dma_handle);
161	WARN_ON(!devres_destroy(dev, dmam_noncoherent_release, dmam_match,
162				&match_data));
163}
164EXPORT_SYMBOL(dmam_free_noncoherent);
165
166#ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
167
168static void dmam_coherent_decl_release(struct device *dev, void *res)
169{
170	dma_release_declared_memory(dev);
171}
172
173/**
174 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
175 * @dev: Device to declare coherent memory for
176 * @bus_addr: Bus address of coherent memory to be declared
177 * @device_addr: Device address of coherent memory to be declared
178 * @size: Size of coherent memory to be declared
179 * @flags: Flags
180 *
181 * Managed dma_declare_coherent_memory().
182 *
183 * RETURNS:
184 * 0 on success, -errno on failure.
185 */
186int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
187				 dma_addr_t device_addr, size_t size, int flags)
188{
189	void *res;
190	int rc;
191
192	res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
193	if (!res)
194		return -ENOMEM;
195
196	rc = dma_declare_coherent_memory(dev, bus_addr, device_addr, size,
197					 flags);
198	if (rc == 0)
199		devres_add(dev, res);
200	else
201		devres_free(res);
202
203	return rc;
204}
205EXPORT_SYMBOL(dmam_declare_coherent_memory);
206
207/**
208 * dmam_release_declared_memory - Managed dma_release_declared_memory().
209 * @dev: Device to release declared coherent memory for
210 *
211 * Managed dmam_release_declared_memory().
212 */
213void dmam_release_declared_memory(struct device *dev)
214{
215	WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
216}
217EXPORT_SYMBOL(dmam_release_declared_memory);
218
219#endif