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.
10 #include <linux/dma-mapping.h>
11 #include <linux/export.h>
12 #include <linux/gfp.h>
20 dma_addr_t dma_handle
;
23 static void dmam_coherent_release(struct device
*dev
, void *res
)
25 struct dma_devres
*this = res
;
27 dma_free_coherent(dev
, this->size
, this->vaddr
, this->dma_handle
);
30 static void dmam_noncoherent_release(struct device
*dev
, void *res
)
32 struct dma_devres
*this = res
;
34 dma_free_noncoherent(dev
, this->size
, this->vaddr
, this->dma_handle
);
37 static int dmam_match(struct device
*dev
, void *res
, void *match_data
)
39 struct dma_devres
*this = res
, *match
= match_data
;
41 if (this->vaddr
== match
->vaddr
) {
42 WARN_ON(this->size
!= match
->size
||
43 this->dma_handle
!= match
->dma_handle
);
50 * dmam_alloc_coherent - Managed dma_alloc_coherent()
51 * @dev: Device to allocate coherent memory for
52 * @size: Size of allocation
53 * @dma_handle: Out argument for allocated DMA handle
54 * @gfp: Allocation flags
56 * Managed dma_alloc_coherent(). Memory allocated using this function
57 * will be automatically released on driver detach.
60 * Pointer to allocated memory on success, NULL on failure.
62 void * dmam_alloc_coherent(struct device
*dev
, size_t size
,
63 dma_addr_t
*dma_handle
, gfp_t gfp
)
65 struct dma_devres
*dr
;
68 dr
= devres_alloc(dmam_coherent_release
, sizeof(*dr
), gfp
);
72 vaddr
= dma_alloc_coherent(dev
, size
, dma_handle
, gfp
);
79 dr
->dma_handle
= *dma_handle
;
86 EXPORT_SYMBOL(dmam_alloc_coherent
);
89 * dmam_free_coherent - Managed dma_free_coherent()
90 * @dev: Device to free coherent memory for
91 * @size: Size of allocation
92 * @vaddr: Virtual address of the memory to free
93 * @dma_handle: DMA handle of the memory to free
95 * Managed dma_free_coherent().
97 void dmam_free_coherent(struct device
*dev
, size_t size
, void *vaddr
,
98 dma_addr_t dma_handle
)
100 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
102 dma_free_coherent(dev
, size
, vaddr
, dma_handle
);
103 WARN_ON(devres_destroy(dev
, dmam_coherent_release
, dmam_match
,
106 EXPORT_SYMBOL(dmam_free_coherent
);
109 * dmam_alloc_non_coherent - Managed dma_alloc_non_coherent()
110 * @dev: Device to allocate non_coherent memory for
111 * @size: Size of allocation
112 * @dma_handle: Out argument for allocated DMA handle
113 * @gfp: Allocation flags
115 * Managed dma_alloc_non_coherent(). Memory allocated using this
116 * function will be automatically released on driver detach.
119 * Pointer to allocated memory on success, NULL on failure.
121 void *dmam_alloc_noncoherent(struct device
*dev
, size_t size
,
122 dma_addr_t
*dma_handle
, gfp_t gfp
)
124 struct dma_devres
*dr
;
127 dr
= devres_alloc(dmam_noncoherent_release
, sizeof(*dr
), gfp
);
131 vaddr
= dma_alloc_noncoherent(dev
, size
, dma_handle
, gfp
);
138 dr
->dma_handle
= *dma_handle
;
145 EXPORT_SYMBOL(dmam_alloc_noncoherent
);
148 * dmam_free_coherent - Managed dma_free_noncoherent()
149 * @dev: Device to free noncoherent memory for
150 * @size: Size of allocation
151 * @vaddr: Virtual address of the memory to free
152 * @dma_handle: DMA handle of the memory to free
154 * Managed dma_free_noncoherent().
156 void dmam_free_noncoherent(struct device
*dev
, size_t size
, void *vaddr
,
157 dma_addr_t dma_handle
)
159 struct dma_devres match_data
= { size
, vaddr
, dma_handle
};
161 dma_free_noncoherent(dev
, size
, vaddr
, dma_handle
);
162 WARN_ON(!devres_destroy(dev
, dmam_noncoherent_release
, dmam_match
,
165 EXPORT_SYMBOL(dmam_free_noncoherent
);
167 #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
169 static void dmam_coherent_decl_release(struct device
*dev
, void *res
)
171 dma_release_declared_memory(dev
);
175 * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
176 * @dev: Device to declare coherent memory for
177 * @bus_addr: Bus address of coherent memory to be declared
178 * @device_addr: Device address of coherent memory to be declared
179 * @size: Size of coherent memory to be declared
182 * Managed dma_declare_coherent_memory().
185 * 0 on success, -errno on failure.
187 int dmam_declare_coherent_memory(struct device
*dev
, dma_addr_t bus_addr
,
188 dma_addr_t device_addr
, size_t size
, int flags
)
193 res
= devres_alloc(dmam_coherent_decl_release
, 0, GFP_KERNEL
);
197 rc
= dma_declare_coherent_memory(dev
, bus_addr
, device_addr
, size
,
200 devres_add(dev
, res
);
206 EXPORT_SYMBOL(dmam_declare_coherent_memory
);
209 * dmam_release_declared_memory - Managed dma_release_declared_memory().
210 * @dev: Device to release declared coherent memory for
212 * Managed dmam_release_declared_memory().
214 void dmam_release_declared_memory(struct device
*dev
)
216 WARN_ON(devres_destroy(dev
, dmam_coherent_decl_release
, NULL
, NULL
));
218 EXPORT_SYMBOL(dmam_release_declared_memory
);