blob: a3f79c495a411ea85b87604ab473fcef6b962558 [file] [log] [blame]
Sumit Semwald15bd7e2011-12-26 14:53:15 +05301/*
2 * Framework for buffer objects that can be shared across devices/subsystems.
3 *
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Author: Sumit Semwal <sumit.semwal@ti.com>
6 *
7 * Many thanks to linaro-mm-sig list, and specially
8 * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
9 * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
10 * refining of this idea.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25#include <linux/fs.h>
26#include <linux/slab.h>
27#include <linux/dma-buf.h>
28#include <linux/anon_inodes.h>
29#include <linux/export.h>
30
31static inline int is_dma_buf_file(struct file *);
32
33static int dma_buf_release(struct inode *inode, struct file *file)
34{
35 struct dma_buf *dmabuf;
36
37 if (!is_dma_buf_file(file))
38 return -EINVAL;
39
40 dmabuf = file->private_data;
41
42 dmabuf->ops->release(dmabuf);
43 kfree(dmabuf);
44 return 0;
45}
46
Daniel Vetter4c785132012-04-24 14:38:52 +053047static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
48{
49 struct dma_buf *dmabuf;
50
51 if (!is_dma_buf_file(file))
52 return -EINVAL;
53
54 dmabuf = file->private_data;
55
56 /* check for overflowing the buffer's size */
57 if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
58 dmabuf->size >> PAGE_SHIFT)
59 return -EINVAL;
60
61 return dmabuf->ops->mmap(dmabuf, vma);
62}
63
Sumit Semwald15bd7e2011-12-26 14:53:15 +053064static const struct file_operations dma_buf_fops = {
65 .release = dma_buf_release,
Daniel Vetter4c785132012-04-24 14:38:52 +053066 .mmap = dma_buf_mmap_internal,
Sumit Semwald15bd7e2011-12-26 14:53:15 +053067};
68
69/*
70 * is_dma_buf_file - Check if struct file* is associated with dma_buf
71 */
72static inline int is_dma_buf_file(struct file *file)
73{
74 return file->f_op == &dma_buf_fops;
75}
76
77/**
78 * dma_buf_export - Creates a new dma_buf, and associates an anon file
79 * with this buffer, so it can be exported.
80 * Also connect the allocator specific data and ops to the buffer.
81 *
82 * @priv: [in] Attach private data of allocator to this buffer
83 * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
84 * @size: [in] Size of the buffer
85 * @flags: [in] mode flags for the file.
86 *
87 * Returns, on success, a newly created dma_buf object, which wraps the
88 * supplied private data and operations for dma_buf_ops. On either missing
89 * ops, or error in allocating struct dma_buf, will return negative error.
90 *
91 */
Laurent Pinchart53757642012-01-26 12:27:22 +010092struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
Sumit Semwald15bd7e2011-12-26 14:53:15 +053093 size_t size, int flags)
94{
95 struct dma_buf *dmabuf;
96 struct file *file;
97
98 if (WARN_ON(!priv || !ops
99 || !ops->map_dma_buf
100 || !ops->unmap_dma_buf
Daniel Vetterfc130202012-03-20 00:02:37 +0100101 || !ops->release
102 || !ops->kmap_atomic
Daniel Vetter4c785132012-04-24 14:38:52 +0530103 || !ops->kmap
104 || !ops->mmap)) {
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530105 return ERR_PTR(-EINVAL);
106 }
107
108 dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
109 if (dmabuf == NULL)
110 return ERR_PTR(-ENOMEM);
111
112 dmabuf->priv = priv;
113 dmabuf->ops = ops;
114 dmabuf->size = size;
115
116 file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
117
118 dmabuf->file = file;
119
120 mutex_init(&dmabuf->lock);
121 INIT_LIST_HEAD(&dmabuf->attachments);
122
123 return dmabuf;
124}
125EXPORT_SYMBOL_GPL(dma_buf_export);
126
127
128/**
129 * dma_buf_fd - returns a file descriptor for the given dma_buf
130 * @dmabuf: [in] pointer to dma_buf for which fd is required.
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000131 * @flags: [in] flags to give to fd
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530132 *
133 * On success, returns an associated 'fd'. Else, returns error.
134 */
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000135int dma_buf_fd(struct dma_buf *dmabuf, int flags)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530136{
137 int error, fd;
138
139 if (!dmabuf || !dmabuf->file)
140 return -EINVAL;
141
Dave Airlie55c1c4c2012-03-16 10:34:02 +0000142 error = get_unused_fd_flags(flags);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530143 if (error < 0)
144 return error;
145 fd = error;
146
147 fd_install(fd, dmabuf->file);
148
149 return fd;
150}
151EXPORT_SYMBOL_GPL(dma_buf_fd);
152
153/**
154 * dma_buf_get - returns the dma_buf structure related to an fd
155 * @fd: [in] fd associated with the dma_buf to be returned
156 *
157 * On success, returns the dma_buf structure associated with an fd; uses
158 * file's refcounting done by fget to increase refcount. returns ERR_PTR
159 * otherwise.
160 */
161struct dma_buf *dma_buf_get(int fd)
162{
163 struct file *file;
164
165 file = fget(fd);
166
167 if (!file)
168 return ERR_PTR(-EBADF);
169
170 if (!is_dma_buf_file(file)) {
171 fput(file);
172 return ERR_PTR(-EINVAL);
173 }
174
175 return file->private_data;
176}
177EXPORT_SYMBOL_GPL(dma_buf_get);
178
179/**
180 * dma_buf_put - decreases refcount of the buffer
181 * @dmabuf: [in] buffer to reduce refcount of
182 *
183 * Uses file's refcounting done implicitly by fput()
184 */
185void dma_buf_put(struct dma_buf *dmabuf)
186{
187 if (WARN_ON(!dmabuf || !dmabuf->file))
188 return;
189
190 fput(dmabuf->file);
191}
192EXPORT_SYMBOL_GPL(dma_buf_put);
193
194/**
195 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
196 * calls attach() of dma_buf_ops to allow device-specific attach functionality
197 * @dmabuf: [in] buffer to attach device to.
198 * @dev: [in] device to be attached.
199 *
200 * Returns struct dma_buf_attachment * for this attachment; may return negative
201 * error codes.
202 *
203 */
204struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
205 struct device *dev)
206{
207 struct dma_buf_attachment *attach;
208 int ret;
209
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100210 if (WARN_ON(!dmabuf || !dev))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530211 return ERR_PTR(-EINVAL);
212
213 attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
214 if (attach == NULL)
Laurent Pincharta9fbc3b2012-01-26 12:27:24 +0100215 return ERR_PTR(-ENOMEM);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530216
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530217 attach->dev = dev;
218 attach->dmabuf = dmabuf;
Laurent Pinchart2ed92012012-01-26 12:27:25 +0100219
220 mutex_lock(&dmabuf->lock);
221
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530222 if (dmabuf->ops->attach) {
223 ret = dmabuf->ops->attach(dmabuf, dev, attach);
224 if (ret)
225 goto err_attach;
226 }
227 list_add(&attach->node, &dmabuf->attachments);
228
229 mutex_unlock(&dmabuf->lock);
230 return attach;
231
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530232err_attach:
233 kfree(attach);
234 mutex_unlock(&dmabuf->lock);
235 return ERR_PTR(ret);
236}
237EXPORT_SYMBOL_GPL(dma_buf_attach);
238
239/**
240 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
241 * optionally calls detach() of dma_buf_ops for device-specific detach
242 * @dmabuf: [in] buffer to detach from.
243 * @attach: [in] attachment to be detached; is free'd after this call.
244 *
245 */
246void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
247{
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100248 if (WARN_ON(!dmabuf || !attach))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530249 return;
250
251 mutex_lock(&dmabuf->lock);
252 list_del(&attach->node);
253 if (dmabuf->ops->detach)
254 dmabuf->ops->detach(dmabuf, attach);
255
256 mutex_unlock(&dmabuf->lock);
257 kfree(attach);
258}
259EXPORT_SYMBOL_GPL(dma_buf_detach);
260
261/**
262 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
263 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
264 * dma_buf_ops.
265 * @attach: [in] attachment whose scatterlist is to be returned
266 * @direction: [in] direction of DMA transfer
267 *
268 * Returns sg_table containing the scatterlist to be returned; may return NULL
269 * or ERR_PTR.
270 *
271 */
272struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
273 enum dma_data_direction direction)
274{
275 struct sg_table *sg_table = ERR_PTR(-EINVAL);
276
277 might_sleep();
278
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100279 if (WARN_ON(!attach || !attach->dmabuf))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530280 return ERR_PTR(-EINVAL);
281
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100282 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530283
284 return sg_table;
285}
286EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
287
288/**
289 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
290 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
291 * dma_buf_ops.
292 * @attach: [in] attachment to unmap buffer from
293 * @sg_table: [in] scatterlist info of the buffer to unmap
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530294 * @direction: [in] direction of DMA transfer
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530295 *
296 */
297void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530298 struct sg_table *sg_table,
299 enum dma_data_direction direction)
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530300{
Rob Clarkb6fa0cd2012-09-28 09:29:43 +0200301 might_sleep();
302
Laurent Pinchartd1aa06a2012-01-26 12:27:23 +0100303 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530304 return;
305
Sumit Semwal33ea2dc2012-01-27 15:09:27 +0530306 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
307 direction);
Sumit Semwald15bd7e2011-12-26 14:53:15 +0530308}
309EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
Daniel Vetterfc130202012-03-20 00:02:37 +0100310
311
312/**
313 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
314 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
315 * preparations. Coherency is only guaranteed in the specified range for the
316 * specified access direction.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700317 * @dmabuf: [in] buffer to prepare cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +0100318 * @start: [in] start of range for cpu access.
319 * @len: [in] length of range for cpu access.
320 * @direction: [in] length of range for cpu access.
321 *
322 * Can return negative error values, returns 0 on success.
323 */
324int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
325 enum dma_data_direction direction)
326{
327 int ret = 0;
328
329 if (WARN_ON(!dmabuf))
330 return -EINVAL;
331
332 if (dmabuf->ops->begin_cpu_access)
333 ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
334
335 return ret;
336}
337EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
338
339/**
340 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
341 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
342 * actions. Coherency is only guaranteed in the specified range for the
343 * specified access direction.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700344 * @dmabuf: [in] buffer to complete cpu access for.
Daniel Vetterfc130202012-03-20 00:02:37 +0100345 * @start: [in] start of range for cpu access.
346 * @len: [in] length of range for cpu access.
347 * @direction: [in] length of range for cpu access.
348 *
349 * This call must always succeed.
350 */
351void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
352 enum dma_data_direction direction)
353{
354 WARN_ON(!dmabuf);
355
356 if (dmabuf->ops->end_cpu_access)
357 dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
358}
359EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
360
361/**
362 * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
363 * space. The same restrictions as for kmap_atomic and friends apply.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700364 * @dmabuf: [in] buffer to map page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100365 * @page_num: [in] page in PAGE_SIZE units to map.
366 *
367 * This call must always succeed, any necessary preparations that might fail
368 * need to be done in begin_cpu_access.
369 */
370void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
371{
372 WARN_ON(!dmabuf);
373
374 return dmabuf->ops->kmap_atomic(dmabuf, page_num);
375}
376EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
377
378/**
379 * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700380 * @dmabuf: [in] buffer to unmap page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100381 * @page_num: [in] page in PAGE_SIZE units to unmap.
382 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
383 *
384 * This call must always succeed.
385 */
386void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
387 void *vaddr)
388{
389 WARN_ON(!dmabuf);
390
391 if (dmabuf->ops->kunmap_atomic)
392 dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
393}
394EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
395
396/**
397 * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
398 * same restrictions as for kmap and friends apply.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700399 * @dmabuf: [in] buffer to map page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100400 * @page_num: [in] page in PAGE_SIZE units to map.
401 *
402 * This call must always succeed, any necessary preparations that might fail
403 * need to be done in begin_cpu_access.
404 */
405void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
406{
407 WARN_ON(!dmabuf);
408
409 return dmabuf->ops->kmap(dmabuf, page_num);
410}
411EXPORT_SYMBOL_GPL(dma_buf_kmap);
412
413/**
414 * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
Randy Dunlapefb4df822012-04-17 17:03:30 -0700415 * @dmabuf: [in] buffer to unmap page from.
Daniel Vetterfc130202012-03-20 00:02:37 +0100416 * @page_num: [in] page in PAGE_SIZE units to unmap.
417 * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
418 *
419 * This call must always succeed.
420 */
421void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
422 void *vaddr)
423{
424 WARN_ON(!dmabuf);
425
426 if (dmabuf->ops->kunmap)
427 dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
428}
429EXPORT_SYMBOL_GPL(dma_buf_kunmap);
Daniel Vetter4c785132012-04-24 14:38:52 +0530430
431
432/**
433 * dma_buf_mmap - Setup up a userspace mmap with the given vma
Sumit Semwal12c47272012-05-23 15:27:40 +0530434 * @dmabuf: [in] buffer that should back the vma
Daniel Vetter4c785132012-04-24 14:38:52 +0530435 * @vma: [in] vma for the mmap
436 * @pgoff: [in] offset in pages where this mmap should start within the
437 * dma-buf buffer.
438 *
439 * This function adjusts the passed in vma so that it points at the file of the
440 * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
441 * checking on the size of the vma. Then it calls the exporters mmap function to
442 * set up the mapping.
443 *
444 * Can return negative error values, returns 0 on success.
445 */
446int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
447 unsigned long pgoff)
448{
449 if (WARN_ON(!dmabuf || !vma))
450 return -EINVAL;
451
452 /* check for offset overflow */
453 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
454 return -EOVERFLOW;
455
456 /* check for overflowing the buffer's size */
457 if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
458 dmabuf->size >> PAGE_SHIFT)
459 return -EINVAL;
460
461 /* readjust the vma */
462 if (vma->vm_file)
463 fput(vma->vm_file);
464
Al Virocb0942b2012-08-27 14:48:26 -0400465 vma->vm_file = get_file(dmabuf->file);
Daniel Vetter4c785132012-04-24 14:38:52 +0530466
467 vma->vm_pgoff = pgoff;
468
469 return dmabuf->ops->mmap(dmabuf, vma);
470}
471EXPORT_SYMBOL_GPL(dma_buf_mmap);
Dave Airlie98f86c92012-05-20 12:33:56 +0530472
473/**
Sumit Semwal12c47272012-05-23 15:27:40 +0530474 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
475 * address space. Same restrictions as for vmap and friends apply.
476 * @dmabuf: [in] buffer to vmap
Dave Airlie98f86c92012-05-20 12:33:56 +0530477 *
478 * This call may fail due to lack of virtual mapping address space.
479 * These calls are optional in drivers. The intended use for them
480 * is for mapping objects linear in kernel space for high use objects.
481 * Please attempt to use kmap/kunmap before thinking about these interfaces.
482 */
483void *dma_buf_vmap(struct dma_buf *dmabuf)
484{
485 if (WARN_ON(!dmabuf))
486 return NULL;
487
488 if (dmabuf->ops->vmap)
489 return dmabuf->ops->vmap(dmabuf);
490 return NULL;
491}
492EXPORT_SYMBOL_GPL(dma_buf_vmap);
493
494/**
495 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
Sumit Semwal12c47272012-05-23 15:27:40 +0530496 * @dmabuf: [in] buffer to vunmap
Randy Dunlap6e7b4a52012-06-09 15:02:59 -0700497 * @vaddr: [in] vmap to vunmap
Dave Airlie98f86c92012-05-20 12:33:56 +0530498 */
499void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
500{
501 if (WARN_ON(!dmabuf))
502 return;
503
504 if (dmabuf->ops->vunmap)
505 dmabuf->ops->vunmap(dmabuf, vaddr);
506}
507EXPORT_SYMBOL_GPL(dma_buf_vunmap);