summaryrefslogtreecommitdiffstats
path: root/meta-rcar-gen3-adas/recipes-kernel/kernel-module-cmemdrv/files/cmemdrv.c
blob: a5d4363554105971b795b52af3cb55f6e798fbf1 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*************************************************************************/ /*
 Memory Driver (kernel module)

 Copyright (C) 2015 - 2017 Renesas Electronics Corporation

 License        Dual MIT/GPLv2

 The contents of this file are subject to the MIT license as set out below.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 Alternatively, the contents of this file may be used under the terms of
 the GNU General Public License Version 2 ("GPL") in which case the provisions
 of GPL are applicable instead of those above.

 If you wish to allow use of your version of this file only under the terms of
 GPL, and not to allow others to use your version of this file under the terms
 of the MIT license, indicate your decision by deleting the provisions above
 and replace them with the notice and other provisions required by GPL as set
 out in the file called "GPL-COPYING" included in this distribution. If you do
 not delete the provisions above, a recipient may use your version of this file
 under the terms of either the MIT license or GPL.

 This License is also included in this distribution in the file called
 "MIT-COPYING".

 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 GPLv2:
 If you wish to use this file under the terms of GPL, following terms are
 effective.

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/ /*************************************************************************/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/of_device.h>

#define MAX_AREA_NUM 4
#define DEFAULT_AREA_SIZE (16 * 1024 * 1024)

#define PARAM_SET     1
#define M_LOCK        3
#define M_UNLOCK      4
#define GET_PHYS_ADDR 5
#define M_ALLOCATE    6
#define M_UNALLOCATE  7
#define TRY_CONV      8

#define IOCTL_FROM_IMP_TO_CPU 0
#define IOCTL_FROM_CPU_TO_IMP 1

struct mem_area_data {
	struct device *dev;
	size_t size;
	void *virt_ptr;
	dma_addr_t phys_addr;
};

struct mem_access_data {
	struct mem_area_data* area;
	int start_offset;
	int offset;
	int width;
	int height;
	int stride;
	int locked;
	int tl;
};

static unsigned int bsize_count;
static unsigned long bsize[MAX_AREA_NUM];
module_param_array(bsize, ulong, &bsize_count, S_IRUGO);
static int cached = 1;
module_param(cached, int, S_IRUGO);

#ifdef CFG_USE_FPGA
static unsigned long cfg_bsize = 0;
module_param(cfg_bsize, ulong, S_IRUGO);
#endif

static unsigned int cmem_major = 88;		// 0:auto
module_param(cmem_major, uint, S_IRUGO);

static struct class *cmem_class = NULL;
static struct mem_area_data *cmem_areas[MAX_AREA_NUM];

static int cv_v_to_p(unsigned long vaddr, unsigned long *paddr)
{
	struct vm_area_struct *vma;
	unsigned long start, offset;
	unsigned long pfn;
	int ret;

	*paddr = 0;

	vma = find_vma(current->active_mm, vaddr);
	if (vma == NULL)
		return -EINVAL;

	start = vaddr & PAGE_MASK;
	ret = follow_pfn(vma, start, &pfn);
	if (ret < 0)
		return ret;

	offset = offset_in_page(vaddr);
	*paddr = (pfn << PAGE_SHIFT) + offset;

	return 0;
}

static void do_tl(u64 *src, u64 *dst, unsigned int height, unsigned int step)
{
	unsigned int i;
	int j;
	unsigned int yaddr;
	u64 *isrc, *idst, *isrcp, *idstp;

	for (j = 0; j < height ; j += 2) {
		yaddr = (j & ~0x1f) * step | ((j & 0x1f) << 7);
		isrc = src + (yaddr >> 3);
		idst = dst + (j * step >> 3);
		for (i = 0; i < step >> 3; i += 16) {
			idstp = idst + i;
			isrcp = isrc + (i << 5);
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;

			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;

			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;

			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;

			idstp = idst + i + (step >> 3);
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;

			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;

			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;

			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;
			*idstp++ = *isrcp++;

			//idst[i] = isrc[((i >> 4) << 9) + (i & 0xf)];
		}
	}
}

static ssize_t dev_read(struct file *filep, char *buf, size_t len, loff_t *ppos)
{
	size_t count;
	struct mem_access_data *p = filep->private_data;

	if (p->tl) {
		do_tl( (u64 *)(p->area->virt_ptr + p->offset + p->start_offset),
		       (u64 *)buf, p->height, p->stride );
		count = len;
	} else {
		count = copy_to_user(buf, p->area->virt_ptr + p->offset + p->start_offset, len);
	}

	return count;
}

static ssize_t dev_write(struct file *filep, const char *buf, size_t len, loff_t *ppos)
{
	size_t count;
	struct mem_access_data *p = filep->private_data;

	count = copy_from_user(p->area->virt_ptr + p->offset + p->start_offset, buf, len);

	return count;
}

static long dev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
{
	struct mem_access_data *p = filep->private_data;
	struct device *dev = p->area->dev;
	u32 offset, size, dir;

	switch (cmd) {
	case PARAM_SET :
		p->width  = ((unsigned int *)arg)[0];
		p->height = ((unsigned int *)arg)[1];
		p->stride = ((unsigned int *)arg)[2];
		p->tl     = ((unsigned int *)arg)[3];
		p->offset = ((unsigned int *)arg)[4];
		break;
	case M_ALLOCATE :
		break;
	case M_LOCK :
		if (cached) {
			offset = ((unsigned int *)arg)[0];
			size   = ((unsigned int *)arg)[1];
			dir    = ((unsigned int *)arg)[2];
			if (dir == IOCTL_FROM_IMP_TO_CPU)
				dma_sync_single_for_device(dev, p->area->phys_addr + p->start_offset + offset, size, DMA_FROM_DEVICE);
			else
				dma_sync_single_for_device(dev, p->area->phys_addr + p->start_offset + offset, size, DMA_TO_DEVICE);
		}
		break;
	case M_UNLOCK :
		if (cached) {
			offset = ((unsigned int *)arg)[0];
			size   = ((unsigned int *)arg)[1];
			dir    = ((unsigned int *)arg)[2];
			if (dir == IOCTL_FROM_IMP_TO_CPU)
				dma_sync_single_for_cpu(dev, p->area->phys_addr + p->start_offset + offset, size, DMA_FROM_DEVICE);
			else
				dma_sync_single_for_cpu(dev, p->area->phys_addr + p->start_offset + offset, size, DMA_TO_DEVICE);
		}
		break;
	case M_UNALLOCATE :
		break;
	case GET_PHYS_ADDR :
		*((unsigned int *)arg) = p->area->phys_addr;
		break;
	case TRY_CONV :
		cv_v_to_p( ( (unsigned long *)arg )[0], (unsigned long *)arg + 1);
		break;
	default:
		dev_warn(dev, "## unknown ioctl command %d\n", cmd);
		return -EINVAL;
	}

	return 0;
}

static int dev_mmap(struct file *filep, struct vm_area_struct *vma)
{
	struct mem_access_data *p = filep->private_data;
	unsigned long off;
	unsigned long start;

	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
		return -EINVAL;

	off = vma->vm_pgoff << PAGE_SHIFT;
	start = p->area->phys_addr;

	if ((vma->vm_end - vma->vm_start + off) > p->area->size)
		return -EINVAL;

	off += start;
	vma->vm_pgoff = off >> PAGE_SHIFT;
	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
	if (!cached)
		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

	return remap_pfn_range( vma,
	                        vma->vm_start,
	                        vma->vm_pgoff,
	                        vma->vm_end - vma->vm_start,
	                        vma->vm_page_prot ) ;
}

static int dev_open(struct inode *inode, struct file *filep)
{
	struct mem_access_data *mem_access_p;
	int minor = iminor(inode);
	struct mem_area_data* area = cmem_areas[minor];

	mem_access_p = kzalloc(sizeof(*mem_access_p), GFP_KERNEL);
	if (!mem_access_p)
		return -ENOMEM;

	dev_dbg(area->dev, "Device Open\n");
	mem_access_p->area = area;
	filep->private_data = mem_access_p;

	return 0;
}

static int dev_rls(struct inode *inode, struct file *filep)
{
	struct mem_access_data *mem_access_p = filep->private_data;
	kfree(mem_access_p);
	return 0;
}

static struct file_operations fops = {
	.owner          = THIS_MODULE,
	.read           = dev_read,
	.write          = dev_write,
	.unlocked_ioctl = dev_ioctl,
	.compat_ioctl   = dev_ioctl,
	.mmap           = dev_mmap,
	.open           = dev_open,
	.release        = dev_rls,
};

static int cmemdrv_create_device(dev_t devt, size_t size)
{
	int ret;
	struct mem_area_data *area;
	struct device *dev;
	void *virt_b_ptr;
	dma_addr_t phy_b_addr;

	dev = device_create(cmem_class, NULL, devt, NULL, "cmem%d", MINOR(devt));
	if (IS_ERR(dev)) {
		pr_err("cmem: unable to create device cmem%d\n", MINOR(devt));
		return PTR_ERR(dev);
	}

	area = devm_kzalloc(dev, sizeof(*area), GFP_KERNEL);
	if (!area) {
		ret = -ENOMEM;
		goto err;
	}
	area->dev = dev;

#ifdef CONFIG_ARM64
	{
		struct device_node *np;
		np = of_find_compatible_node(NULL, NULL, "shared-dma-pool");
		of_dma_configure(dev, np);
	}
#endif

	virt_b_ptr = dmam_alloc_coherent(dev, size + PAGE_SIZE, &phy_b_addr, GFP_KERNEL);
	if (!virt_b_ptr) {
		dev_err(dev, "Memory allocation failed.. (size:0x%zx)\n", size);
		ret = -ENOMEM;
		goto err;
	}

	area->virt_ptr = PTR_ALIGN(virt_b_ptr, PAGE_SIZE);
	area->phys_addr = phy_b_addr + (area->virt_ptr - virt_b_ptr);
	area->size = size;
	cmem_areas[MINOR(devt)] = area;
	dev_notice(dev, "Memory allocated.. 0x%08lx (size:0x%zx)\n", (unsigned long)area->phys_addr, size);

	return 0;

err:
	device_destroy(cmem_class, devt);
	return ret;
}

static int __init cmemdrv_init(void)
{
	int i = 0;
	int ret;

	if (bsize_count == 0)
		bsize[bsize_count++] = DEFAULT_AREA_SIZE;	/* add default area */

#ifdef CFG_USE_FPGA
	if (cfg_bsize && bsize_count < MAX_AREA_NUM)
		bsize[bsize_count++] = cfg_bsize;	/* for compatibility */
#endif

	ret = register_chrdev(cmem_major, "CMem", &fops);
	if (ret < 0) {
		pr_err("cmem: unable to get major %d\n", cmem_major);
		return ret;
	}
	if (cmem_major == 0)
		cmem_major = ret;

	cmem_class = class_create(THIS_MODULE, "cmem");
	if (IS_ERR(cmem_class)) {
		pr_err("cmem: unable to create class\n");
		ret = PTR_ERR(cmem_class);
		goto err1;
	}

	for (i = 0; i < bsize_count; i++) {
		ret = cmemdrv_create_device(MKDEV(cmem_major, i), bsize[i]);
		if (ret < 0)
			goto err2;
	}

	return 0;

err2:
	for (i--; i >= 0; i--)
		device_destroy(cmem_class, MKDEV(cmem_major, i));

	class_destroy(cmem_class);
err1:
	unregister_chrdev(cmem_major, "CMem");
	return ret;
}

static void __exit cmemdrv_exit(void)
{
	int i;
	for (i = 0; i < bsize_count; i++)
		device_destroy(cmem_class, MKDEV(cmem_major, i));

	class_destroy(cmem_class);
	unregister_chrdev(cmem_major, "CMem");
}

module_init(cmemdrv_init)
module_exit(cmemdrv_exit)

MODULE_AUTHOR("RenesasElectronicsCorp.");
MODULE_DESCRIPTION("Userspace I/O driver for image memory");
MODULE_LICENSE("GPL v2");