blob: 20c95da7aa77cffd0d1500e862f504dacbf6fd74 [file] [log] [blame]
Pawel Osciak1a758d42010-10-11 10:59:36 -03001/*
2 * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciak1a758d42010-10-11 10:59:36 -03007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/dma-mapping.h>
16
17#include <media/videobuf2-core.h>
H Hartley Sweetend0df3c32012-04-24 19:08:12 -030018#include <media/videobuf2-dma-contig.h>
Pawel Osciak1a758d42010-10-11 10:59:36 -030019#include <media/videobuf2-memops.h>
20
21struct vb2_dc_conf {
22 struct device *dev;
23};
24
25struct vb2_dc_buf {
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -030026 struct device *dev;
Pawel Osciak1a758d42010-10-11 10:59:36 -030027 void *vaddr;
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -030028 dma_addr_t dma_addr;
Pawel Osciak1a758d42010-10-11 10:59:36 -030029 unsigned long size;
30 struct vm_area_struct *vma;
31 atomic_t refcount;
32 struct vb2_vmarea_handler handler;
33};
34
Laurent Pinchartf7f129c2012-06-14 10:37:39 -030035static void vb2_dc_put(void *buf_priv);
Pawel Osciak1a758d42010-10-11 10:59:36 -030036
Laurent Pinchartf7f129c2012-06-14 10:37:39 -030037static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
Pawel Osciak1a758d42010-10-11 10:59:36 -030038{
39 struct vb2_dc_conf *conf = alloc_ctx;
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -030040 struct device *dev = conf->dev;
Pawel Osciak1a758d42010-10-11 10:59:36 -030041 struct vb2_dc_buf *buf;
42
43 buf = kzalloc(sizeof *buf, GFP_KERNEL);
44 if (!buf)
45 return ERR_PTR(-ENOMEM);
46
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -030047 buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
Pawel Osciak1a758d42010-10-11 10:59:36 -030048 if (!buf->vaddr) {
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -030049 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
Pawel Osciak1a758d42010-10-11 10:59:36 -030050 kfree(buf);
51 return ERR_PTR(-ENOMEM);
52 }
53
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -030054 buf->dev = dev;
Pawel Osciak1a758d42010-10-11 10:59:36 -030055 buf->size = size;
56
57 buf->handler.refcount = &buf->refcount;
Laurent Pinchartf7f129c2012-06-14 10:37:39 -030058 buf->handler.put = vb2_dc_put;
Pawel Osciak1a758d42010-10-11 10:59:36 -030059 buf->handler.arg = buf;
60
61 atomic_inc(&buf->refcount);
62
63 return buf;
64}
65
Laurent Pinchartf7f129c2012-06-14 10:37:39 -030066static void vb2_dc_put(void *buf_priv)
Pawel Osciak1a758d42010-10-11 10:59:36 -030067{
68 struct vb2_dc_buf *buf = buf_priv;
69
70 if (atomic_dec_and_test(&buf->refcount)) {
Tomasz Stanislawski72f86bf2012-06-14 10:37:40 -030071 dma_free_coherent(buf->dev, buf->size, buf->vaddr,
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -030072 buf->dma_addr);
Pawel Osciak1a758d42010-10-11 10:59:36 -030073 kfree(buf);
74 }
75}
76
Laurent Pinchartf7f129c2012-06-14 10:37:39 -030077static void *vb2_dc_cookie(void *buf_priv)
Pawel Osciak1a758d42010-10-11 10:59:36 -030078{
79 struct vb2_dc_buf *buf = buf_priv;
80
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -030081 return &buf->dma_addr;
Pawel Osciak1a758d42010-10-11 10:59:36 -030082}
83
Laurent Pinchartf7f129c2012-06-14 10:37:39 -030084static void *vb2_dc_vaddr(void *buf_priv)
Pawel Osciak1a758d42010-10-11 10:59:36 -030085{
86 struct vb2_dc_buf *buf = buf_priv;
87 if (!buf)
H Hartley Sweeten121b3dd2012-04-24 19:12:48 -030088 return NULL;
Pawel Osciak1a758d42010-10-11 10:59:36 -030089
90 return buf->vaddr;
91}
92
Laurent Pinchartf7f129c2012-06-14 10:37:39 -030093static unsigned int vb2_dc_num_users(void *buf_priv)
Pawel Osciak1a758d42010-10-11 10:59:36 -030094{
95 struct vb2_dc_buf *buf = buf_priv;
96
97 return atomic_read(&buf->refcount);
98}
99
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300100static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
Pawel Osciak1a758d42010-10-11 10:59:36 -0300101{
102 struct vb2_dc_buf *buf = buf_priv;
103
104 if (!buf) {
105 printk(KERN_ERR "No buffer to map\n");
106 return -EINVAL;
107 }
108
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300109 return vb2_mmap_pfn_range(vma, buf->dma_addr, buf->size,
Pawel Osciak1a758d42010-10-11 10:59:36 -0300110 &vb2_common_vm_ops, &buf->handler);
111}
112
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300113static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
Pawel Osciak1a758d42010-10-11 10:59:36 -0300114 unsigned long size, int write)
115{
116 struct vb2_dc_buf *buf;
117 struct vm_area_struct *vma;
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300118 dma_addr_t dma_addr = 0;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300119 int ret;
120
121 buf = kzalloc(sizeof *buf, GFP_KERNEL);
122 if (!buf)
123 return ERR_PTR(-ENOMEM);
124
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300125 ret = vb2_get_contig_userptr(vaddr, size, &vma, &dma_addr);
Pawel Osciak1a758d42010-10-11 10:59:36 -0300126 if (ret) {
127 printk(KERN_ERR "Failed acquiring VMA for vaddr 0x%08lx\n",
128 vaddr);
129 kfree(buf);
130 return ERR_PTR(ret);
131 }
132
133 buf->size = size;
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300134 buf->dma_addr = dma_addr;
Pawel Osciak1a758d42010-10-11 10:59:36 -0300135 buf->vma = vma;
136
137 return buf;
138}
139
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300140static void vb2_dc_put_userptr(void *mem_priv)
Pawel Osciak1a758d42010-10-11 10:59:36 -0300141{
142 struct vb2_dc_buf *buf = mem_priv;
143
144 if (!buf)
145 return;
146
147 vb2_put_vma(buf->vma);
148 kfree(buf);
149}
150
151const struct vb2_mem_ops vb2_dma_contig_memops = {
Laurent Pinchartf7f129c2012-06-14 10:37:39 -0300152 .alloc = vb2_dc_alloc,
153 .put = vb2_dc_put,
154 .cookie = vb2_dc_cookie,
155 .vaddr = vb2_dc_vaddr,
156 .mmap = vb2_dc_mmap,
157 .get_userptr = vb2_dc_get_userptr,
158 .put_userptr = vb2_dc_put_userptr,
159 .num_users = vb2_dc_num_users,
Pawel Osciak1a758d42010-10-11 10:59:36 -0300160};
161EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
162
163void *vb2_dma_contig_init_ctx(struct device *dev)
164{
165 struct vb2_dc_conf *conf;
166
167 conf = kzalloc(sizeof *conf, GFP_KERNEL);
168 if (!conf)
169 return ERR_PTR(-ENOMEM);
170
171 conf->dev = dev;
172
173 return conf;
174}
175EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
176
177void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
178{
179 kfree(alloc_ctx);
180}
181EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
182
183MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
Pawel Osciak95072082011-03-13 15:23:32 -0300184MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
Pawel Osciak1a758d42010-10-11 10:59:36 -0300185MODULE_LICENSE("GPL");