summaryrefslogtreecommitdiff
path: root/drivers/misc/bmm_dmabuf.c
blob: abe204783dcb3009c4802cf46a62ac98065e448b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 *  bmm_dmabuf.c
 *
 *  CMA-backed dma_buf allocator
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */
#undef DEBUG

#include <linux/debugfs.h>
#include <linux/dma-buf.h>
#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <asm/page.h>

#define BMM_MINOR		94

#include "bmm_dmabuf.h"

struct bmm_block {
	struct dma_attrs attrs;
	void *cpu;
	size_t size;
	dma_addr_t paddr;

	struct list_head list;
	struct dma_buf *dmabuf;
};

static LIST_HEAD(bmm_used_blocks);
static DEFINE_MUTEX(bmm_mutex);	/* mutex for block list */

static void bmm_insert_ordered(struct bmm_block *new, struct list_head *head)
{
	struct bmm_block *p;

	if (list_empty(head)) {
		list_add(&new->list, head);
		return;
	}

	list_for_each_entry(p, head, list)
		if (p->paddr > new->paddr)
			break;

	list_add_tail(&new->list, &p->list);
}

static struct bmm_block *__bmm_malloc(size_t size, unsigned long attr,
	unsigned align)
{
	struct bmm_block *new;

	size = ALIGN(size, align);
	if (size == 0 || size > TASK_SIZE)
		return NULL;

	new = kmalloc(sizeof(*new), GFP_KERNEL);
	if (!new)
		return NULL;

	init_dma_attrs(&new->attrs);

	dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &new->attrs);
	dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, &new->attrs);
	if (attr & BMM_ATTR_WRITECOMBINE)
		dma_set_attr(DMA_ATTR_WRITE_COMBINE, &new->attrs);

	new->size = size;
	new->cpu = dma_alloc_attrs(NULL, size, &new->paddr,
				   GFP_KERNEL | __GFP_HIGHMEM,
				   &new->attrs);
	if (!new->cpu) {
		kfree(new);
		return NULL;
	}

	mutex_lock(&bmm_mutex);
	bmm_insert_ordered(new, &bmm_used_blocks);
	mutex_unlock(&bmm_mutex);

	return new;
}

static void __bmm_free(struct bmm_block *bmm)
{
	mutex_lock(&bmm_mutex);
	list_del(&bmm->list);
	mutex_unlock(&bmm_mutex);

	dma_free_attrs(NULL, bmm->size, bmm->cpu, bmm->paddr, &bmm->attrs);
	kfree(bmm);
}

static struct sg_table *
bmm_dmabuf_map(struct dma_buf_attachment *attach, enum dma_data_direction dir)
{
	struct bmm_block *bmm = attach->dmabuf->priv;
	struct sg_table *sgt;

	sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
	if (sgt) {
		if (sg_alloc_table(sgt, 1, GFP_KERNEL))
			goto err_free_sgt;

		sg_dma_address(sgt->sgl) = bmm->paddr;
		sg_dma_len(sgt->sgl) = bmm->size;
	}
	return sgt;

 err_free_sgt:
	kfree(sgt);
	return NULL;
}

static void
bmm_dmabuf_unmap(struct dma_buf_attachment *attach, struct sg_table *sgt,
	enum dma_data_direction dir)
{
	sg_free_table(sgt);
	kfree(sgt);
}

static void bmm_dmabuf_release(struct dma_buf *buf)
{
	struct bmm_block *bmm = buf->priv;

	bmm->dmabuf = NULL;
	__bmm_free(bmm);
}

static void *bmm_no_kmap(struct dma_buf *buf, unsigned long arg)
{
	return NULL;
}

static void bmm_no_kunmap(struct dma_buf *buf, unsigned long arg, void *addr)
{
}

static int bmm_dmabuf_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
{
	struct bmm_block *bmm = buf->priv;

	return dma_mmap_attrs(NULL, vma, bmm->cpu, bmm->paddr, bmm->size, &bmm->attrs);
}

static const struct dma_buf_ops bmm_dmabuf_ops = {
	.map_dma_buf	= bmm_dmabuf_map,
	.unmap_dma_buf	= bmm_dmabuf_unmap,
	.release	= bmm_dmabuf_release,
	.kmap_atomic	= bmm_no_kmap,
	.kunmap_atomic	= bmm_no_kunmap,
	.kmap		= bmm_no_kmap,
	.kunmap		= bmm_no_kunmap,
	.mmap		= bmm_dmabuf_mmap,
};

static int bmm_dmabuf_alloc(struct bmm_fd_alloc *alloc)
{
	struct bmm_block *bmm;
	struct dma_buf *buf;
	int ret;

	if (alloc->align > PAGE_SIZE)
		return -EINVAL;

	bmm = __bmm_malloc(alloc->size, alloc->attr, PAGE_SIZE);
	if (!bmm)
		return -ENOMEM;

	buf = dma_buf_export(bmm, &bmm_dmabuf_ops, bmm->size, O_RDWR);
	if (IS_ERR(buf)) {
		__bmm_free(bmm);
		return PTR_ERR(buf);
	}

	bmm->dmabuf = buf;

	/* Consume the refcount on the bmm */
	ret = dma_buf_fd(buf, O_CLOEXEC);
	if (ret >= 0)
		alloc->fd = ret;

	return ret < 0 ? ret : 0;
}

static struct dma_buf *bmm_block_get(int fd)
{
	struct dma_buf *buf = dma_buf_get(fd);

	if (!IS_ERR(buf) && buf->ops != &bmm_dmabuf_ops) {
		dma_buf_put(buf);
		buf = ERR_PTR(-EINVAL);
	}
	return buf;
}

static int bmm_dmabuf_flush(struct bmm_fd_flush *flush)
{
	struct dma_buf *buf = bmm_block_get(flush->fd);
	struct bmm_block *bmm;

	if (IS_ERR(buf))
		return PTR_ERR(buf);

	bmm = buf->priv;

	dma_buf_put(buf);

	return -EINVAL;
}

union ioctl_arg {
	struct bmm_fd_alloc alloc;
	struct bmm_fd_flush flush;
};

static long bmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	void __user *u_io = (void __user *)arg;
	union ioctl_arg io;
	int ret;

	if (_IOC_SIZE(cmd) > sizeof(io))
		return -EINVAL;

	memset(&io, 0, sizeof(io));
	if (cmd & IOC_IN && copy_from_user(&io, u_io, _IOC_SIZE(cmd))) {
		pr_debug("bmm_ioctl() error in copy_from_user()\n");
		return -EFAULT;
	}

	pr_debug("bmm_ioctl(cmd=0x%08x, arg=0x%08lx)\n", cmd, arg);

	switch (cmd) {
	case BMM_FD_ALLOC:
		ret = bmm_dmabuf_alloc(&io.alloc);
		break;

	case BMM_FD_FLUSH:
		ret = bmm_dmabuf_flush(&io.flush);
		break;

	default:
		ret = -ENOTTY;
		break;
	}

	if (ret == 0 && cmd & IOC_OUT) {
		if (copy_to_user(u_io, &io, _IOC_SIZE(cmd))) {
			pr_debug("bmm_ioctl() error in copy_to_user()\n");
			return -EFAULT;
		}
	}

	return ret;
}

static const struct file_operations bmm_fops = {
	.owner		= THIS_MODULE,
	.unlocked_ioctl	= bmm_ioctl,
};

static struct miscdevice bmm_misc = {
	.minor	= BMM_MINOR,
	.name	= "bmm",
	.fops	= &bmm_fops,
};

#ifdef CONFIG_DEBUG_FS
static struct dentry *bmm_dentry;

static int bmm_debugfs_show(struct seq_file *m, void *data)
{
	struct bmm_block *bmm;

	list_for_each_entry(bmm, &bmm_used_blocks, list) {
		seq_printf(m, "0x%08lx-0x%08lx used %p %p\n",
			   (unsigned long)bmm->paddr,
			   (unsigned long)bmm->paddr + bmm->size - 1,
			   bmm->dmabuf, bmm->dmabuf ? bmm->dmabuf->file : NULL);
	}
	return 0;
}

static int bmm_debugfs_open(struct inode *inode, struct file *file)
{
	return single_open(file, bmm_debugfs_show, NULL);
}

static const struct file_operations bmm_debugfs_fops = {
	.owner		= THIS_MODULE,
	.open		= bmm_debugfs_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static void bmm_debugfs_init(void)
{
	bmm_dentry = debugfs_create_file("bmm", S_IRUSR, NULL, NULL,
		&bmm_debugfs_fops);
}

static void bmm_debugfs_exit(void)
{
	debugfs_remove(bmm_dentry);
}
#else
#define bmm_debugfs_init()
#define bmm_debugfs_exit()
#endif

static int bmm_probe(struct platform_device *pdev)
{
	return 0;
}

static struct platform_driver bmm_driver = {
	.driver = {
		.name	= "bmm",
		.owner	= THIS_MODULE,
	},
	.probe	= bmm_probe,
};
MODULE_ALIAS(PLATFORM_MODULE_PREFIX "bmm");

static int __init bmm_init(void)
{
	int ret;

	bmm_debugfs_init();

	ret = platform_driver_register(&bmm_driver);
	if (ret)
		return ret;

	ret = misc_register(&bmm_misc);
	if (ret)
		platform_driver_unregister(&bmm_driver);

	return ret;
}

static void __exit bmm_exit(void)
{
	bmm_debugfs_exit();
	misc_deregister(&bmm_misc);
	platform_driver_unregister(&bmm_driver);
}
module_init(bmm_init);
module_exit(bmm_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("CMA-backed dma_buf Management Module");