summaryrefstree
path: root/kernel/drivers/media/platform/exynos/fimc-is2/fimc-is-mem.c
blob: fae17e22adfd2de68a63f2242d4c57860833ff87 (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
/*
 * Samsung Exynos5 SoC series FIMC-IS driver
 *
 * exynos5 fimc-is core functions
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd
 *
 * 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.
 */

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <video/videonode.h>
#include <media/exynos_mc.h>
#include <asm/cacheflush.h>
#include <asm/pgtable.h>
#include <linux/firmware.h>
#include <linux/dma-mapping.h>
#include <linux/scatterlist.h>
#include <linux/videodev2.h>
#include <linux/videodev2_exynos_camera.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
#include <linux/of.h>

#include "fimc-is-core.h"
#include "fimc-is-cmd.h"
#include "fimc-is-regs.h"
#include "fimc-is-err.h"

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
#include <linux/exynos_iovmm.h>
#else
#include <plat/iovmm.h>
#endif

#if defined(CONFIG_VIDEOBUF2_ION)
/* fimc-is vb2 buffer operations */
static inline ulong fimc_is_vb2_ion_plane_kvaddr(
		struct fimc_is_vb2_buf *vbuf, u32 plane)

{
	return (ulong)vb2_plane_vaddr(&vbuf->vb.vb2_buf, plane);
}

static inline ulong fimc_is_vb2_ion_plane_cookie(
		struct fimc_is_vb2_buf *vbuf, u32 plane)
{
	return (ulong)vb2_plane_cookie(&vbuf->vb.vb2_buf, plane);
}

static dma_addr_t fimc_is_vb2_ion_plane_dvaddr(
		struct fimc_is_vb2_buf *vbuf, u32 plane)

{
	dma_addr_t dva = 0;

	WARN_ON(vb2_ion_dma_address(vb2_plane_cookie(&vbuf->vb.vb2_buf, plane), &dva) != 0);

	return (ulong)dva;
}

static void fimc_is_vb2_ion_plane_prepare(struct fimc_is_vb2_buf *vbuf,
		u32 plane, bool exact)
{
	struct vb2_buffer *vb = &vbuf->vb.vb2_buf;
	enum dma_data_direction dir;
	unsigned long size;
	u32 spare;

	/* skip meta plane */
	spare = vb->num_planes - 1;
	if (plane == spare)
		return;

	dir = V4L2_TYPE_IS_OUTPUT(vb->type) ?
		DMA_TO_DEVICE : DMA_FROM_DEVICE;

	size = exact ?
		vb2_get_plane_payload(vb, plane) : vb2_plane_size(vb, plane);

	vb2_ion_sync_for_device((void *)vb2_plane_cookie(vb, plane), 0,
			size, dir);
}

static void fimc_is_vb2_ion_plane_finish(struct fimc_is_vb2_buf *vbuf,
		u32 plane, bool exact)
{
	struct vb2_buffer *vb = &vbuf->vb.vb2_buf;
	enum dma_data_direction dir;
	unsigned long size;
	u32 spare;

	/* skip meta plane */
	spare = vb->num_planes - 1;
	if (plane == spare)
		return;

	dir = V4L2_TYPE_IS_OUTPUT(vb->type) ?
		DMA_TO_DEVICE : DMA_FROM_DEVICE;

	size = exact ?
		vb2_get_plane_payload(vb, plane) : vb2_plane_size(vb, plane);

	vb2_ion_sync_for_cpu((void *)vb2_plane_cookie(vb, plane), 0,
			size, dir);
}

static void fimc_is_vb2_ion_buf_prepare(struct fimc_is_vb2_buf *vbuf, bool exact)
{
	struct vb2_buffer *vb = &vbuf->vb.vb2_buf;
	enum dma_data_direction dir;
	unsigned long size;
	u32 plane;
	u32 spare;

	dir = V4L2_TYPE_IS_OUTPUT(vb->type) ?
		DMA_TO_DEVICE : DMA_FROM_DEVICE;

	/* skip meta plane */
	spare = vb->num_planes - 1;

	for (plane = 0; plane < spare; plane++) {
		size = exact ?
			vb2_get_plane_payload(vb, plane) : vb2_plane_size(vb, plane);

		vb2_ion_sync_for_device((void *)vb2_plane_cookie(vb, plane), 0,
				size, dir);
	}
}

static void fimc_is_vb2_ion_buf_finish(struct fimc_is_vb2_buf *vbuf, bool exact)
{
	struct vb2_buffer *vb = &vbuf->vb.vb2_buf;
	enum dma_data_direction dir;
	unsigned long size;
	u32 plane;
	u32 spare;

	dir = V4L2_TYPE_IS_OUTPUT(vb->type) ?
		DMA_TO_DEVICE : DMA_FROM_DEVICE;

	/* skip meta plane */
	spare = vb->num_planes - 1;

	for (plane = 0; plane < spare; plane++) {
		size = exact ?
			vb2_get_plane_payload(vb, plane) : vb2_plane_size(vb, plane);

		vb2_ion_sync_for_cpu((void *)vb2_plane_cookie(vb, plane), 0,
				size, dir);
	}
}

const struct fimc_is_vb2_buf_ops fimc_is_vb2_buf_ops_ion = {
	.plane_kvaddr	= fimc_is_vb2_ion_plane_kvaddr,
	.plane_cookie	= fimc_is_vb2_ion_plane_cookie,
	.plane_dvaddr	= fimc_is_vb2_ion_plane_dvaddr,
	.plane_prepare	= fimc_is_vb2_ion_plane_prepare,
	.plane_finish	= fimc_is_vb2_ion_plane_finish,
	.buf_prepare	= fimc_is_vb2_ion_buf_prepare,
	.buf_finish	= fimc_is_vb2_ion_buf_finish,
};

/* fimc-is private buffer operations */
static void fimc_is_vb2_ion_free(struct fimc_is_priv_buf *pbuf)
{
	vb2_ion_private_free(pbuf->cookie);
	kfree(pbuf);
}

static ulong fimc_is_vb2_ion_kvaddr(struct fimc_is_priv_buf *pbuf)
{
	void *kva;

	if (!pbuf)
		return 0;

	kva = vb2_ion_private_vaddr(pbuf->cookie);
	if (IS_ERR_OR_NULL(kva)) {
		err("could not get kernel addr for @%p: %ld",
			pbuf->cookie, PTR_ERR(kva));
		return 0;
	}

	return (ulong)kva;
}

static dma_addr_t fimc_is_vb2_ion_dvaddr(struct fimc_is_priv_buf *pbuf)
{
	dma_addr_t dva = 0;

	if (!pbuf)
		return 0;

	WARN_ON(vb2_ion_dma_address((void *)pbuf->cookie, &dva) != 0);

	return (ulong)dva;
}

static phys_addr_t fimc_is_vb2_ion_phaddr(struct fimc_is_priv_buf *pbuf)
{
	/* PHCONTIG option is not supported in ION */
	return 0;
}

static void fimc_is_vb2_ion_sync_for_device(struct fimc_is_priv_buf *pbuf,
		off_t offset, size_t size, enum dma_data_direction dir)
{
	vb2_ion_sync_for_device(pbuf->cookie, offset, size, dir);
}

static void fimc_is_vb2_ion_sync_for_cpu(struct fimc_is_priv_buf *pbuf,
		off_t offset, size_t size, enum dma_data_direction dir)
{
	vb2_ion_sync_for_cpu(pbuf->cookie, offset, size, dir);
}

const struct fimc_is_priv_buf_ops fimc_is_priv_buf_ops_ion = {
	.free			= fimc_is_vb2_ion_free,
	.kvaddr			= fimc_is_vb2_ion_kvaddr,
	.dvaddr			= fimc_is_vb2_ion_dvaddr,
	.phaddr			= fimc_is_vb2_ion_phaddr,
	.sync_for_device	= fimc_is_vb2_ion_sync_for_device,
	.sync_for_cpu		= fimc_is_vb2_ion_sync_for_cpu,
};

/* fimc-is memory operations */
static void *fimc_is_vb2_ion_init(struct platform_device *pdev,
		long flag)
{
	return vb2_ion_create_context(&pdev->dev, SZ_4K,
			VB2ION_CTX_IOMMU |
			flag);
}

static struct fimc_is_priv_buf *fimc_is_vb2_ion_alloc(void *ctx,
		size_t size, size_t align)
{
	struct fimc_is_priv_buf *buf;
	int ret = 0;

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

	buf->cookie = vb2_ion_private_alloc(ctx, size);
	if (IS_ERR(buf->cookie)) {
		err("failed to allocate a private buffer, size: %zd", size);
		ret = PTR_ERR(buf->cookie);
		goto err_priv_alloc;
	}

	buf->size = size;
	buf->align = align;
	buf->ctx = ctx;
	buf->ops = &fimc_is_priv_buf_ops_ion;

	return buf;

err_priv_alloc:
	kfree(buf);

	return ERR_PTR(ret);
}

static int fimc_is_vb2_ion_resume(void *ctx)
{
	if (ctx)
		return vb2_ion_attach_iommu(ctx);

	return -ENOENT;
}

static void fimc_is_vb2_ion_suspend(void *ctx)
{
	if (ctx)
		vb2_ion_detach_iommu(ctx);
}

const struct fimc_is_mem_ops fimc_is_mem_ops_ion = {
	.init			= fimc_is_vb2_ion_init,
	.cleanup		= vb2_ion_destroy_context,
	.resume			= fimc_is_vb2_ion_resume,
	.suspend		= fimc_is_vb2_ion_suspend,
	.set_cached		= vb2_ion_set_cached,
	.set_alignment		= vb2_ion_set_alignment,
	.alloc			= fimc_is_vb2_ion_alloc,
};
#endif


/* fimc-is private buffer operations */
static void fimc_is_vb2_km_free(struct fimc_is_priv_buf *pbuf)
{
	kfree(pbuf->kvaddr);
	kfree(pbuf);
}

static ulong fimc_is_vb2_km_kvaddr(struct fimc_is_priv_buf *pbuf)
{
	if (!pbuf)
		return 0;

	return (ulong)pbuf->kvaddr;
}

static phys_addr_t fimc_is_vb2_km_phaddr(struct fimc_is_priv_buf *pbuf)
{
	phys_addr_t pa = 0;

	if (!pbuf)
		return 0;

	pa = virt_to_phys(pbuf->kvaddr);

	return pa;
}
const struct fimc_is_priv_buf_ops fimc_is_priv_buf_ops_km = {
	.free			= fimc_is_vb2_km_free,
	.kvaddr			= fimc_is_vb2_km_kvaddr,
	.phaddr			= fimc_is_vb2_km_phaddr,
};

static struct fimc_is_priv_buf *fimc_is_kmalloc(size_t size, size_t align)
{
	struct fimc_is_priv_buf *buf = NULL;
	int ret = 0;

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

	buf->kvaddr = kzalloc(DEBUG_REGION_SIZE + 0x10, GFP_KERNEL);
	if (!buf->kvaddr) {
		ret = -ENOMEM;
		goto err_priv_alloc;
	}

	buf->size = size;
	buf->align = align;
	buf->ops = &fimc_is_priv_buf_ops_km;

	return buf;

err_priv_alloc:
	kfree(buf);

	return ERR_PTR(ret);
}

int fimc_is_mem_init(struct fimc_is_mem *mem, struct platform_device *pdev)
{
#if defined(CONFIG_VIDEOBUF2_ION)
	mem->fimc_is_mem_ops = &fimc_is_mem_ops_ion;
	mem->vb2_mem_ops = &vb2_ion_memops;
	mem->fimc_is_vb2_buf_ops = &fimc_is_vb2_buf_ops_ion;
	mem->kmalloc = &fimc_is_kmalloc;
#endif

	mem->default_ctx = CALL_PTR_MEMOP(mem, init, pdev, VB2ION_CTX_VMCONTIG);
	if (IS_ERR_OR_NULL(mem->default_ctx)) {
		if (IS_ERR(mem->default_ctx))
			return PTR_ERR(mem->default_ctx);
		else
			return -EINVAL;
	}

	return 0;
}