x86: check range in reserve_early()
[linux-2.6/mini2440.git] / drivers / base / dma-mapping.c
blobca9186f70a695378c9bc9f5e7622d1d601cc6787
1 /*
2 * drivers/base/dma-mapping.c - arch-independent dma-mapping routines
4 * Copyright (c) 2006 SUSE Linux Products GmbH
5 * Copyright (c) 2006 Tejun Heo <teheo@suse.de>
7 * This file is released under the GPLv2.
8 */
10 #include <linux/dma-mapping.h>
13 * Managed DMA API
15 struct dma_devres {
16 size_t size;
17 void *vaddr;
18 dma_addr_t dma_handle;
21 static void dmam_coherent_release(struct device *dev, void *res)
23 struct dma_devres *this = res;
25 dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
28 static void dmam_noncoherent_release(struct device *dev, void *res)
30 struct dma_devres *this = res;
32 dma_free_noncoherent(dev, this->size, this->vaddr, this->dma_handle);
35 static int dmam_match(struct device *dev, void *res, void *match_data)
37 struct dma_devres *this = res, *match = match_data;
39 if (this->vaddr == match->vaddr) {
40 WARN_ON(this->size != match->size ||
41 this->dma_handle != match->dma_handle);
42 return 1;
44 return 0;
47 /**
48 * dmam_alloc_coherent - Managed dma_alloc_coherent()
49 * @dev: Device to allocate coherent memory for
50 * @size: Size of allocation
51 * @dma_handle: Out argument for allocated DMA handle
52 * @gfp: Allocation flags
54 * Managed dma_alloc_coherent(). Memory allocated using this function
55 * will be automatically released on driver detach.
57 * RETURNS:
58 * Pointer to allocated memory on success, NULL on failure.
60 void * dmam_alloc_coherent(struct device *dev, size_t size,
61 dma_addr_t *dma_handle, gfp_t gfp)
63 struct dma_devres *dr;
64 void *vaddr;
66 dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
67 if (!dr)
68 return NULL;
70 vaddr = dma_alloc_coherent(dev, size, dma_handle, gfp);
71 if (!vaddr) {
72 devres_free(dr);
73 return NULL;
76 dr->vaddr = vaddr;
77 dr->dma_handle = *dma_handle;
78 dr->size = size;
80 devres_add(dev, dr);
82 return vaddr;
84 EXPORT_SYMBOL(dmam_alloc_coherent);
86 /**
87 * dmam_free_coherent - Managed dma_free_coherent()
88 * @dev: Device to free coherent memory for
89 * @size: Size of allocation
90 * @vaddr: Virtual address of the memory to free
91 * @dma_handle: DMA handle of the memory to free
93 * Managed dma_free_coherent().
95 void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
96 dma_addr_t dma_handle)
98 struct dma_devres match_data = { size, vaddr, dma_handle };
100 dma_free_coherent(dev, size, vaddr, dma_handle);
101 WARN_ON(devres_destroy(dev, dmam_coherent_release, dmam_match,
102 &match_data));
104 EXPORT_SYMBOL(dmam_free_coherent);
107 * dmam_alloc_non_coherent - Managed dma_alloc_non_coherent()
108 * @dev: Device to allocate non_coherent memory for
109 * @size: Size of allocation
110 * @dma_handle: Out argument for allocated DMA handle
111 * @gfp: Allocation flags
113 * Managed dma_alloc_non_coherent(). Memory allocated using this
114 * function will be automatically released on driver detach.
116 * RETURNS:
117 * Pointer to allocated memory on success, NULL on failure.
119 void *dmam_alloc_noncoherent(struct device *dev, size_t size,
120 dma_addr_t *dma_handle, gfp_t gfp)
122 struct dma_devres *dr;
123 void *vaddr;
125 dr = devres_alloc(dmam_noncoherent_release, sizeof(*dr), gfp);
126 if (!dr)
127 return NULL;
129 vaddr = dma_alloc_noncoherent(dev, size, dma_handle, gfp);
130 if (!vaddr) {
131 devres_free(dr);
132 return NULL;
135 dr->vaddr = vaddr;
136 dr->dma_handle = *dma_handle;
137 dr->size = size;
139 devres_add(dev, dr);
141 return vaddr;
143 EXPORT_SYMBOL(dmam_alloc_noncoherent);
146 * dmam_free_coherent - Managed dma_free_noncoherent()
147 * @dev: Device to free noncoherent memory for
148 * @size: Size of allocation
149 * @vaddr: Virtual address of the memory to free
150 * @dma_handle: DMA handle of the memory to free
152 * Managed dma_free_noncoherent().
154 void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
155 dma_addr_t dma_handle)
157 struct dma_devres match_data = { size, vaddr, dma_handle };
159 dma_free_noncoherent(dev, size, vaddr, dma_handle);
160 WARN_ON(!devres_destroy(dev, dmam_noncoherent_release, dmam_match,
161 &match_data));
163 EXPORT_SYMBOL(dmam_free_noncoherent);
165 #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
167 static void dmam_coherent_decl_release(struct device *dev, void *res)
169 dma_release_declared_memory(dev);
173 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
174 * @dev: Device to declare coherent memory for
175 * @bus_addr: Bus address of coherent memory to be declared
176 * @device_addr: Device address of coherent memory to be declared
177 * @size: Size of coherent memory to be declared
178 * @flags: Flags
180 * Managed dma_declare_coherent_memory().
182 * RETURNS:
183 * 0 on success, -errno on failure.
185 int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
186 dma_addr_t device_addr, size_t size, int flags)
188 void *res;
189 int rc;
191 res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
192 if (!res)
193 return -ENOMEM;
195 rc = dma_declare_coherent_memory(dev, bus_addr, device_addr, size,
196 flags);
197 if (rc == 0)
198 devres_add(dev, res);
199 else
200 devres_free(res);
202 return rc;
204 EXPORT_SYMBOL(dmam_declare_coherent_memory);
207 * dmam_release_declared_memory - Managed dma_release_declared_memory().
208 * @dev: Device to release declared coherent memory for
210 * Managed dmam_release_declared_memory().
212 void dmam_release_declared_memory(struct device *dev)
214 WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
216 EXPORT_SYMBOL(dmam_release_declared_memory);
218 #endif