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>
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
);
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.
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
;
66 dr
= devres_alloc(dmam_coherent_release
, sizeof(*dr
), gfp
);
70 vaddr
= dma_alloc_coherent(dev
, size
, dma_handle
, gfp
);
77 dr
->dma_handle
= *dma_handle
;
84 EXPORT_SYMBOL(dmam_alloc_coherent
);
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
,
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.
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
;
125 dr
= devres_alloc(dmam_noncoherent_release
, sizeof(*dr
), gfp
);
129 vaddr
= dma_alloc_noncoherent(dev
, size
, dma_handle
, gfp
);
136 dr
->dma_handle
= *dma_handle
;
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
,
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
180 * Managed dma_declare_coherent_memory().
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
)
191 res
= devres_alloc(dmam_coherent_decl_release
, 0, GFP_KERNEL
);
195 rc
= dma_declare_coherent_memory(dev
, bus_addr
, device_addr
, size
,
198 devres_add(dev
, res
);
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
);