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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
* 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 <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 <linux/dma-buf.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);
}
clear_bit(FIMC_IS_VBUF_CACHE_INVALIDATE, &vbuf->cache_state);
}
static dma_addr_t fimc_is_bufcon_map(struct fimc_is_vb2_buf *vbuf,
struct device *dev,
int idx,
struct dma_buf *dmabuf)
{
int ret = 0;
vbuf->bufs[idx] = dmabuf;
vbuf->attachment[idx] = dma_buf_attach(dmabuf, dev);
if (IS_ERR(vbuf->attachment[idx])) {
ret = PTR_ERR(vbuf->attachment[idx]);
pr_err("%s: Failed to dma_buf_attach %d\n", __func__, idx);
goto err_attach;
}
vbuf->sgt[idx] = dma_buf_map_attachment(vbuf->attachment[idx], DMA_BIDIRECTIONAL);
if (IS_ERR(vbuf->sgt[idx])) {
ret = PTR_ERR(vbuf->sgt[idx]);
pr_err("%s: Failed to dma_buf_map_attachment %d\n", __func__, idx);
goto err_map_dmabuf;
}
vbuf->dva[idx] = ion_iovmm_map(vbuf->attachment[idx], 0, 0, DMA_BIDIRECTIONAL, 0);
if (IS_ERR_VALUE(vbuf->dva[idx])) {
pr_err("%s: Failed to ion_iovmm_map %d\n", __func__, idx);
ret = -ENOMEM;
goto err_ion_map_io;
}
return vbuf->dva[idx];
err_ion_map_io:
dma_buf_unmap_attachment(vbuf->attachment[idx], vbuf->sgt[idx], DMA_BIDIRECTIONAL);
err_map_dmabuf:
dma_buf_detach(dmabuf, vbuf->attachment[idx]);
err_attach:
vbuf->bufs[idx] = NULL;
return 0;
}
static void fimc_is_bufcon_unmap(struct fimc_is_vb2_buf *vbuf,
int idx)
{
if (!vbuf->bufs[idx])
return;
ion_iovmm_unmap(vbuf->attachment[idx], vbuf->dva[idx]);
vbuf->dva[idx] = 0;
dma_buf_unmap_attachment(vbuf->attachment[idx], vbuf->sgt[idx], DMA_BIDIRECTIONAL);
dma_buf_detach(vbuf->bufs[idx], vbuf->attachment[idx]);
vbuf->bufs[idx] = NULL;
}
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,
.bufcon_map = fimc_is_bufcon_map,
.bufcon_unmap = fimc_is_bufcon_unmap,
};
/* fimc-is private buffer operations */
static void fimc_is_ion_free(struct fimc_is_priv_buf *pbuf)
{
struct fimc_is_ion_ctx *alloc_ctx;
alloc_ctx = pbuf->ctx;
mutex_lock(&alloc_ctx->lock);
if (pbuf->cookie.ioaddr)
ion_iovmm_unmap(pbuf->attachment, pbuf->cookie.ioaddr);
mutex_unlock(&alloc_ctx->lock);
dma_buf_unmap_attachment(pbuf->attachment, pbuf->cookie.sgt,
DMA_BIDIRECTIONAL);
dma_buf_detach(pbuf->dma_buf, pbuf->attachment);
dma_buf_put(pbuf->dma_buf);
if (pbuf->kva)
ion_unmap_kernel(alloc_ctx->client, pbuf->handle);
ion_free(alloc_ctx->client, pbuf->handle);
vfree(pbuf);
}
static ulong fimc_is_ion_kvaddr(struct fimc_is_priv_buf *pbuf)
{
struct fimc_is_ion_ctx *alloc_ctx;
if (!pbuf)
return 0;
alloc_ctx = pbuf->ctx;
if (!pbuf->kva) {
pbuf->kva = ion_map_kernel(alloc_ctx->client, pbuf->handle);
if (IS_ERR_OR_NULL(pbuf->kva))
pbuf->kva = NULL;
pbuf->kva += pbuf->cookie.offset;
}
return (ulong)pbuf->kva;
}
static dma_addr_t fimc_is_ion_dvaddr(struct fimc_is_priv_buf *pbuf)
{
dma_addr_t dva = 0;
if (!pbuf)
return -EINVAL;
if (pbuf->cookie.ioaddr == 0)
return -EINVAL;
dva = pbuf->cookie.ioaddr;
return (ulong)dva;
}
static phys_addr_t fimc_is_ion_phaddr(struct fimc_is_priv_buf *pbuf)
{
/* PHCONTIG option is not supported in ION */
return 0;
}
static void fimc_is_ion_sync_for_device(struct fimc_is_priv_buf *pbuf,
off_t offset, size_t size, enum dma_data_direction dir)
{
struct fimc_is_ion_ctx *alloc_ctx = pbuf->ctx;
if (pbuf->kva) {
FIMC_BUG_VOID((offset < 0) || (offset > pbuf->size));
FIMC_BUG_VOID((offset + size) < size);
FIMC_BUG_VOID((size > pbuf->size) || ((offset + size) > pbuf->size));
exynos_ion_sync_vaddr_for_device(alloc_ctx->dev,
pbuf->dma_buf, pbuf->kva, size, offset, dir);
}
}
static void fimc_is_ion_sync_for_cpu(struct fimc_is_priv_buf *pbuf,
off_t offset, size_t size, enum dma_data_direction dir)
{
struct fimc_is_ion_ctx *alloc_ctx = pbuf->ctx;
if (pbuf->kva) {
FIMC_BUG_VOID((offset < 0) || (offset > pbuf->size));
FIMC_BUG_VOID((offset + size) < size);
FIMC_BUG_VOID((size > pbuf->size) || ((offset + size) > pbuf->size));
exynos_ion_sync_vaddr_for_cpu(alloc_ctx->dev,
pbuf->dma_buf, pbuf->kva, size, offset, dir);
}
}
const struct fimc_is_priv_buf_ops fimc_is_priv_buf_ops_ion = {
.free = fimc_is_ion_free,
.kvaddr = fimc_is_ion_kvaddr,
.dvaddr = fimc_is_ion_dvaddr,
.phaddr = fimc_is_ion_phaddr,
.sync_for_device = fimc_is_ion_sync_for_device,
.sync_for_cpu = fimc_is_ion_sync_for_cpu,
};
/* fimc-is memory operations */
static void *fimc_is_ion_init(struct platform_device *pdev)
{
struct fimc_is_ion_ctx *ctx;
ctx = vzalloc(sizeof(*ctx));
if (!ctx)
return ERR_PTR(-ENOMEM);
ctx->dev = &pdev->dev;
ctx->client = exynos_ion_client_create(dev_name(&pdev->dev));
if (IS_ERR(ctx->client)) {
void *retp = ctx->client;
kfree(ctx);
return retp;
}
ctx->alignment = SZ_4K;
ctx->flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
mutex_init(&ctx->lock);
return ctx;
}
static void fimc_is_ion_deinit(void *ctx)
{
struct fimc_is_ion_ctx *alloc_ctx = ctx;
mutex_destroy(&alloc_ctx->lock);
ion_client_destroy(alloc_ctx->client);
vfree(alloc_ctx);
}
static struct fimc_is_priv_buf *fimc_is_ion_alloc(void *ctx,
size_t size, size_t align)
{
struct fimc_is_ion_ctx *alloc_ctx = ctx;
struct fimc_is_priv_buf *buf;
int heapflags = EXYNOS_ION_HEAP_SYSTEM_MASK;
int ret = 0;
buf = vzalloc(sizeof(*buf));
if (!buf)
return ERR_PTR(-ENOMEM);
size = PAGE_ALIGN(size);
buf->handle = ion_alloc(alloc_ctx->client, size, alloc_ctx->alignment,
heapflags, alloc_ctx->flags);
if (IS_ERR(buf->handle)) {
ret = -ENOMEM;
goto err_alloc;
}
buf->dma_buf = ion_share_dma_buf(alloc_ctx->client, buf->handle);
if (IS_ERR(buf->dma_buf)) {
ret = PTR_ERR(buf->dma_buf);
goto err_share;
}
buf->attachment = dma_buf_attach(buf->dma_buf, alloc_ctx->dev);
if (IS_ERR(buf->attachment)) {
ret = PTR_ERR(buf->attachment);
goto err_attach;
}
buf->cookie.sgt = dma_buf_map_attachment(
buf->attachment, DMA_BIDIRECTIONAL);
if (IS_ERR(buf->cookie.sgt)) {
ret = PTR_ERR(buf->cookie.sgt);
goto err_map_dmabuf;
}
buf->ctx = alloc_ctx;
buf->size = size;
buf->direction = DMA_BIDIRECTIONAL;
buf->ops = &fimc_is_priv_buf_ops_ion;
mutex_lock(&alloc_ctx->lock);
buf->cookie.ioaddr = ion_iovmm_map(buf->attachment, 0,
buf->size, buf->direction, 0);
if (IS_ERR_VALUE(buf->cookie.ioaddr)) {
ret = (int)buf->cookie.ioaddr;
mutex_unlock(&alloc_ctx->lock);
goto err_ion_map_io;
}
mutex_unlock(&alloc_ctx->lock);
return buf;
err_ion_map_io:
if (buf->kva)
ion_unmap_kernel(alloc_ctx->client, buf->handle);
dma_buf_unmap_attachment(buf->attachment, buf->cookie.sgt,
DMA_BIDIRECTIONAL);
err_map_dmabuf:
dma_buf_detach(buf->dma_buf, buf->attachment);
err_attach:
dma_buf_put(buf->dma_buf);
err_share:
ion_free(alloc_ctx->client, buf->handle);
err_alloc:
vfree(buf);
pr_err("%s: Error occured while allocating\n", __func__);
return ERR_PTR(ret);
}
static int fimc_is_ion_resume(void *ctx)
{
struct fimc_is_ion_ctx *alloc_ctx = ctx;
int ret = 0;
if (!alloc_ctx)
return -ENOENT;
mutex_lock(&alloc_ctx->lock);
if (alloc_ctx->iommu_active_cnt == 0)
ret = iovmm_activate(alloc_ctx->dev);
if (!ret)
alloc_ctx->iommu_active_cnt++;
mutex_unlock(&alloc_ctx->lock);
return ret;
}
static void fimc_is_ion_suspend(void *ctx)
{
struct fimc_is_ion_ctx *alloc_ctx = ctx;
if (!alloc_ctx)
return;
mutex_lock(&alloc_ctx->lock);
FIMC_BUG_VOID(alloc_ctx->iommu_active_cnt == 0);
if (--alloc_ctx->iommu_active_cnt == 0)
iovmm_deactivate(alloc_ctx->dev);
mutex_unlock(&alloc_ctx->lock);
}
const struct fimc_is_mem_ops fimc_is_mem_ops_ion = {
.init = fimc_is_ion_init,
.cleanup = fimc_is_ion_deinit,
.resume = fimc_is_ion_resume,
.suspend = fimc_is_ion_suspend,
.alloc = fimc_is_ion_alloc,
};
#endif
/* fimc-is private buffer operations */
static void fimc_is_km_free(struct fimc_is_priv_buf *pbuf)
{
kfree(pbuf->kvaddr);
kfree(pbuf);
}
static ulong fimc_is_km_kvaddr(struct fimc_is_priv_buf *pbuf)
{
if (!pbuf)
return 0;
return (ulong)pbuf->kvaddr;
}
static phys_addr_t fimc_is_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_km_free,
.kvaddr = fimc_is_km_kvaddr,
.phaddr = fimc_is_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);
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;
}
|