forked from Minki/linux
511a1b8a4c
This reverts commit 48b25a3a71
.
That commit caused two regressions. The first is a BUG:
Jun 14 18:42:15 test-media kernel: [ 115.972299] BUG: unable to handle kernel NULL pointer dereference at 0000000000000100
Jun 14 18:42:15 test-media kernel: [ 115.972307] IP: [<ffffffff810d5cd0>] __lock_acquire+0x2f0/0x2070
Jun 14 18:42:15 test-media kernel: [ 115.972316] PGD 0
Jun 14 18:42:15 test-media kernel: [ 115.972318] Oops: 0000 [#1] PREEMPT SMP
Jun 14 18:42:15 test-media kernel: [ 115.972321] Modules linked in: vivid v4l2_dv_timings videobuf2_vmalloc videobuf2_memops videobuf2_core v4l2_common videodev media vmw_balloon vmw_vmci acpi_cpufreq processor button
Jun 14 18:42:15 test-media kernel: [ 115.972333] CPU: 0 PID: 1542 Comm: v4l2-ctl Not tainted 4.1.0-rc3-test-media #1190
Jun 14 18:42:15 test-media kernel: [ 115.972336] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/20/2014
Jun 14 18:42:15 test-media kernel: [ 115.972337] task: ffff880220ce4200 ti: ffff88021d16c000 task.ti: ffff88021d16c000
Jun 14 18:42:15 test-media kernel: [ 115.972339] RIP: 0010:[<ffffffff810d5cd0>] [<ffffffff810d5cd0>] __lock_acquire+0x2f0/0x2070
Jun 14 18:42:15 test-media kernel: [ 115.972342] RSP: 0018:ffff88021d16f9b8 EFLAGS: 00010002
Jun 14 18:42:15 test-media kernel: [ 115.972343] RAX: 0000000000000046 RBX: 0000000000000292 RCX: 0000000000000001
Jun 14 18:42:15 test-media kernel: [ 115.972345] RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000100
Jun 14 18:42:15 test-media kernel: [ 115.972346] RBP: ffff88021d16fa88 R08: 0000000000000001 R09: 0000000000000000
Jun 14 18:42:15 test-media kernel: [ 115.972347] R10: 0000000000000001 R11: 0000000000000000 R12: 0000000000000001
Jun 14 18:42:15 test-media kernel: [ 115.972348] R13: ffff880220ce4200 R14: 0000000000000100 R15: 0000000000000000
Jun 14 18:42:15 test-media kernel: [ 115.972350] FS: 00007f2441e7f740(0000) GS:ffff880236e00000(0000) knlGS:0000000000000000
Jun 14 18:42:15 test-media kernel: [ 115.972351] CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
Jun 14 18:42:15 test-media kernel: [ 115.972353] CR2: 0000000000000100 CR3: 0000000001e0b000 CR4: 00000000001406f0
Jun 14 18:42:15 test-media kernel: [ 115.972424] Stack:
Jun 14 18:42:15 test-media kernel: [ 115.972427] ffff88021d16fa98 ffffffff810d6543 0000000000000006 0000000000000246
Jun 14 18:42:15 test-media kernel: [ 115.972431] ffff88021d16fa08 ffffffff810d532d ffff880220ce4a78 ffff880200000000
Jun 14 18:42:15 test-media kernel: [ 115.972433] ffff880200000001 0000000000000000 0000000000000001 000000000093a4a0
Jun 14 18:42:15 test-media kernel: [ 115.972436] Call Trace:
Jun 14 18:42:15 test-media kernel: [ 115.972440] [<ffffffff810d6543>] ? __lock_acquire+0xb63/0x2070
Jun 14 18:42:15 test-media kernel: [ 115.972443] [<ffffffff810d532d>] ? mark_held_locks+0x6d/0xa0
Jun 14 18:42:15 test-media kernel: [ 115.972445] [<ffffffff810d37a8>] ? __lock_is_held+0x58/0x80
Jun 14 18:42:15 test-media kernel: [ 115.972447] [<ffffffff810d852c>] lock_acquire+0x6c/0xa0
Jun 14 18:42:15 test-media kernel: [ 115.972452] [<ffffffffa039f1f6>] ? vb2_vmalloc_put_userptr+0x36/0x110 [videobuf2_vmalloc]
Jun 14 18:42:15 test-media kernel: [ 115.972458] [<ffffffff819b1a92>] down_read+0x42/0x60
Jun 14 18:42:15 test-media kernel: [ 115.972460] [<ffffffffa039f1f6>] ? vb2_vmalloc_put_userptr+0x36/0x110 [videobuf2_vmalloc]
Jun 14 18:42:15 test-media kernel: [ 115.972463] [<ffffffff819af1b1>] ? mutex_lock_nested+0x2b1/0x560
Jun 14 18:42:15 test-media kernel: [ 115.972467] [<ffffffffa038fdc5>] ? vb2_queue_release+0x25/0x40 [videobuf2_core]
Jun 14 18:42:15 test-media kernel: [ 115.972469] [<ffffffffa039f1f6>] vb2_vmalloc_put_userptr+0x36/0x110 [videobuf2_vmalloc]
Jun 14 18:42:15 test-media kernel: [ 115.972472] [<ffffffffa038b626>] __vb2_queue_free+0x146/0x5e0 [videobuf2_core]
Jun 14 18:42:15 test-media kernel: [ 115.972475] [<ffffffffa038fdd3>] vb2_queue_release+0x33/0x40 [videobuf2_core]
Jun 14 18:42:15 test-media kernel: [ 115.972478] [<ffffffffa038fe75>] _vb2_fop_release+0x95/0xb0 [videobuf2_core]
Jun 14 18:42:15 test-media kernel: [ 115.972481] [<ffffffffa038feb9>] vb2_fop_release+0x29/0x50 [videobuf2_core]
Jun 14 18:42:15 test-media kernel: [ 115.972485] [<ffffffffa03ad372>] vivid_fop_release+0x92/0x230 [vivid]
Jun 14 18:42:15 test-media kernel: [ 115.972491] [<ffffffffa0358460>] v4l2_release+0x30/0x80 [videodev]
Jun 14 18:42:15 test-media kernel: [ 115.972496] [<ffffffff811a51d5>] __fput+0xe5/0x200
Jun 14 18:42:15 test-media kernel: [ 115.972498] [<ffffffff811a5339>] ____fput+0x9/0x10
Jun 14 18:42:15 test-media kernel: [ 115.972501] [<ffffffff810a9fa4>] task_work_run+0xc4/0xf0
Jun 14 18:42:15 test-media kernel: [ 115.972504] [<ffffffff8108c670>] do_exit+0x3a0/0xaf0
Jun 14 18:42:15 test-media kernel: [ 115.972507] [<ffffffff819b3a9b>] ? _raw_spin_unlock_irq+0x2b/0x60
Jun 14 18:42:15 test-media kernel: [ 115.972509] [<ffffffff8108e0ff>] do_group_exit+0x4f/0xe0
Jun 14 18:42:15 test-media kernel: [ 115.972511] [<ffffffff8109a170>] get_signal+0x200/0x8c0
Jun 14 18:42:15 test-media kernel: [ 115.972514] [<ffffffff819b14b5>] ? __mutex_unlock_slowpath+0xf5/0x240
Jun 14 18:42:15 test-media kernel: [ 115.972518] [<ffffffff81002593>] do_signal+0x23/0x820
Jun 14 18:42:15 test-media kernel: [ 115.972521] [<ffffffff819b1609>] ? mutex_unlock+0x9/0x10
Jun 14 18:42:15 test-media kernel: [ 115.972524] [<ffffffffa0358648>] ? v4l2_ioctl+0x78/0xf0 [videodev]
Jun 14 18:42:15 test-media kernel: [ 115.972526] [<ffffffff819b4653>] ? int_very_careful+0x5/0x46
Jun 14 18:42:15 test-media kernel: [ 115.972529] [<ffffffff810d54bd>] ? trace_hardirqs_on_caller+0x15d/0x200
Jun 14 18:42:15 test-media kernel: [ 115.972531] [<ffffffff81002de0>] do_notify_resume+0x50/0x60
Jun 14 18:42:15 test-media kernel: [ 115.972533] [<ffffffff819b46a6>] int_signal+0x12/0x17
Jun 14 18:42:15 test-media kernel: [ 115.972534] Code: ca 81 31 c0 e8 7a e2 8c 00 e8 aa 1d 8d 00 0f 1f 44 00 00 31 db 48 81 c4 a8 00 00 00 89 d8 5b 41 5c 41 5d 41 5e 41 5f 5d c3 66 90 <49> 81 3e 40 4e 02 82 b8 00 00 00 00 44 0f 44 e0 41 83 ff 01 0f
Jun 14 18:42:15 test-media kernel: [ 115.972567] RIP [<ffffffff810d5cd0>] __lock_acquire+0x2f0/0x2070
Jun 14 18:42:15 test-media kernel: [ 115.972569] RSP <ffff88021d16f9b8>
Jun 14 18:42:15 test-media kernel: [ 115.972570] CR2: 0000000000000100
Jun 14 18:42:15 test-media kernel: [ 115.972573] ---[ end trace 25595c2b8560cb57 ]---
Jun 14 18:42:15 test-media kernel: [ 115.972575] Fixing recursive fault but reboot is needed!
This can be reproduced by loading the vivid driver and running:
v4l2-ctl --stream-user
and pressing Ctrl-C. You may have to try a few times, but in my experience this BUG
is triggered quite quickly.
The second is a possible deadlock:
Jun 14 18:44:07 test-media kernel: [ 49.376650] ======================================================
Jun 14 18:44:07 test-media kernel: [ 49.376651] [ INFO: possible circular locking dependency detected ]
Jun 14 18:44:07 test-media kernel: [ 49.376653] 4.1.0-rc3-test-media #1190 Not tainted
Jun 14 18:44:07 test-media kernel: [ 49.376654] -------------------------------------------------------
Jun 14 18:44:07 test-media kernel: [ 49.376655] v4l2-compliance/1468 is trying to acquire lock:
Jun 14 18:44:07 test-media kernel: [ 49.376657] (&mm->mmap_sem){++++++}, at: [<ffffffffa03a81f6>] vb2_vmalloc_put_userptr+0x36/0x110 [videobuf2_vmalloc]
Jun 14 18:44:07 test-media kernel: [ 49.376665]
Jun 14 18:44:07 test-media kernel: [ 49.376665] but task is already holding lock:
Jun 14 18:44:07 test-media kernel: [ 49.376666] (&q->mmap_lock){+.+...}, at: [<ffffffffa0398dc5>] vb2_queue_release+0x25/0x40 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376670]
Jun 14 18:44:07 test-media kernel: [ 49.376670] which lock already depends on the new lock.
Jun 14 18:44:07 test-media kernel: [ 49.376670]
Jun 14 18:44:07 test-media kernel: [ 49.376671]
Jun 14 18:44:07 test-media kernel: [ 49.376671] the existing dependency chain (in reverse order) is:
Jun 14 18:44:07 test-media kernel: [ 49.376672]
Jun 14 18:44:07 test-media kernel: [ 49.376672] -> #1 (&q->mmap_lock){+.+...}:
Jun 14 18:44:07 test-media kernel: [ 49.376675] [<ffffffff810d852c>] lock_acquire+0x6c/0xa0
Jun 14 18:44:07 test-media kernel: [ 49.376682] [<ffffffff819aef5e>] mutex_lock_nested+0x5e/0x560
Jun 14 18:44:07 test-media kernel: [ 49.376689] [<ffffffffa03934a2>] vb2_mmap+0x232/0x350 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376691] [<ffffffffa0395a60>] vb2_fop_mmap+0x20/0x30 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376694] [<ffffffffa0361102>] v4l2_mmap+0x52/0x90 [videodev]
Jun 14 18:44:07 test-media kernel: [ 49.376698] [<ffffffff81177e33>] mmap_region+0x3b3/0x5e0
Jun 14 18:44:07 test-media kernel: [ 49.376701] [<ffffffff81178377>] do_mmap_pgoff+0x317/0x400
Jun 14 18:44:07 test-media kernel: [ 49.376703] [<ffffffff81165320>] vm_mmap_pgoff+0x90/0xc0
Jun 14 18:44:07 test-media kernel: [ 49.376708] [<ffffffff81176867>] SyS_mmap_pgoff+0x1d7/0x280
Jun 14 18:44:07 test-media kernel: [ 49.376709] [<ffffffff81007f8d>] SyS_mmap+0x1d/0x20
Jun 14 18:44:07 test-media kernel: [ 49.376714] [<ffffffff819b44ae>] system_call_fastpath+0x12/0x76
Jun 14 18:44:07 test-media kernel: [ 49.376716]
Jun 14 18:44:07 test-media kernel: [ 49.376716] -> #0 (&mm->mmap_sem){++++++}:
Jun 14 18:44:07 test-media kernel: [ 49.376718] [<ffffffff810d79b3>] __lock_acquire+0x1fd3/0x2070
Jun 14 18:44:07 test-media kernel: [ 49.376720] [<ffffffff810d852c>] lock_acquire+0x6c/0xa0
Jun 14 18:44:07 test-media kernel: [ 49.376721] [<ffffffff819b1a92>] down_read+0x42/0x60
Jun 14 18:44:07 test-media kernel: [ 49.376723] [<ffffffffa03a81f6>] vb2_vmalloc_put_userptr+0x36/0x110 [videobuf2_vmalloc]
Jun 14 18:44:07 test-media kernel: [ 49.376725] [<ffffffffa0394626>] __vb2_queue_free+0x146/0x5e0 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376727] [<ffffffffa0398dd3>] vb2_queue_release+0x33/0x40 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376729] [<ffffffffa0398e75>] _vb2_fop_release+0x95/0xb0 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376731] [<ffffffffa0398eb9>] vb2_fop_release+0x29/0x50 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376733] [<ffffffffa03b6372>] vivid_fop_release+0x92/0x230 [vivid]
Jun 14 18:44:07 test-media kernel: [ 49.376737] [<ffffffffa0361460>] v4l2_release+0x30/0x80 [videodev]
Jun 14 18:44:07 test-media kernel: [ 49.376739] [<ffffffff811a51d5>] __fput+0xe5/0x200
Jun 14 18:44:07 test-media kernel: [ 49.376744] [<ffffffff811a5339>] ____fput+0x9/0x10
Jun 14 18:44:07 test-media kernel: [ 49.376746] [<ffffffff810a9fa4>] task_work_run+0xc4/0xf0
Jun 14 18:44:07 test-media kernel: [ 49.376749] [<ffffffff81002dd1>] do_notify_resume+0x41/0x60
Jun 14 18:44:07 test-media kernel: [ 49.376752] [<ffffffff819b46a6>] int_signal+0x12/0x17
Jun 14 18:44:07 test-media kernel: [ 49.376754]
Jun 14 18:44:07 test-media kernel: [ 49.376754] other info that might help us debug this:
Jun 14 18:44:07 test-media kernel: [ 49.376754]
Jun 14 18:44:07 test-media kernel: [ 49.376755] Possible unsafe locking scenario:
Jun 14 18:44:07 test-media kernel: [ 49.376755]
Jun 14 18:44:07 test-media kernel: [ 49.376756] CPU0 CPU1
Jun 14 18:44:07 test-media kernel: [ 49.376757] ---- ----
Jun 14 18:44:07 test-media kernel: [ 49.376758] lock(&q->mmap_lock);
Jun 14 18:44:07 test-media kernel: [ 49.376759] lock(&mm->mmap_sem);
Jun 14 18:44:07 test-media kernel: [ 49.376760] lock(&q->mmap_lock);
Jun 14 18:44:07 test-media kernel: [ 49.376761] lock(&mm->mmap_sem);
Jun 14 18:44:07 test-media kernel: [ 49.376763]
Jun 14 18:44:07 test-media kernel: [ 49.376763] *** DEADLOCK ***
Jun 14 18:44:07 test-media kernel: [ 49.376763]
Jun 14 18:44:07 test-media kernel: [ 49.376764] 2 locks held by v4l2-compliance/1468:
Jun 14 18:44:07 test-media kernel: [ 49.376765] #0: (&dev->mutex#3){+.+.+.}, at: [<ffffffffa0398e0a>] _vb2_fop_release+0x2a/0xb0 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376770] #1: (&q->mmap_lock){+.+...}, at: [<ffffffffa0398dc5>] vb2_queue_release+0x25/0x40 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376773]
Jun 14 18:44:07 test-media kernel: [ 49.376773] stack backtrace:
Jun 14 18:44:07 test-media kernel: [ 49.376776] CPU: 2 PID: 1468 Comm: v4l2-compliance Not tainted 4.1.0-rc3-test-media #1190
Jun 14 18:44:07 test-media kernel: [ 49.376777] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/20/2014
Jun 14 18:44:07 test-media kernel: [ 49.376779] ffffffff8279e0b0 ffff88021d6f7ba8 ffffffff819a7aac 0000000000000011
Jun 14 18:44:07 test-media kernel: [ 49.376781] ffffffff8279e0b0 ffff88021d6f7bf8 ffffffff819a3964 ffff88021d6f7bd8
Jun 14 18:44:07 test-media kernel: [ 49.376783] ffff8800ac8aa100 0000000000000002 ffff8800ac8aa9a0 0000000000000002
Jun 14 18:44:07 test-media kernel: [ 49.376785] Call Trace:
Jun 14 18:44:07 test-media kernel: [ 49.376788] [<ffffffff819a7aac>] dump_stack+0x4f/0x7b
Jun 14 18:44:07 test-media kernel: [ 49.376792] [<ffffffff819a3964>] print_circular_bug+0x20f/0x251
Jun 14 18:44:07 test-media kernel: [ 49.376793] [<ffffffff810d79b3>] __lock_acquire+0x1fd3/0x2070
Jun 14 18:44:07 test-media kernel: [ 49.376795] [<ffffffff810d6543>] ? __lock_acquire+0xb63/0x2070
Jun 14 18:44:07 test-media kernel: [ 49.376797] [<ffffffff810d37a8>] ? __lock_is_held+0x58/0x80
Jun 14 18:44:07 test-media kernel: [ 49.376798] [<ffffffff810d852c>] lock_acquire+0x6c/0xa0
Jun 14 18:44:07 test-media kernel: [ 49.376800] [<ffffffffa03a81f6>] ? vb2_vmalloc_put_userptr+0x36/0x110 [videobuf2_vmalloc]
Jun 14 18:44:07 test-media kernel: [ 49.376802] [<ffffffff819b1a92>] down_read+0x42/0x60
Jun 14 18:44:07 test-media kernel: [ 49.376803] [<ffffffffa03a81f6>] ? vb2_vmalloc_put_userptr+0x36/0x110 [videobuf2_vmalloc]
Jun 14 18:44:07 test-media kernel: [ 49.376805] [<ffffffff819af1b1>] ? mutex_lock_nested+0x2b1/0x560
Jun 14 18:44:07 test-media kernel: [ 49.376807] [<ffffffffa0398dc5>] ? vb2_queue_release+0x25/0x40 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376808] [<ffffffffa03a81f6>] vb2_vmalloc_put_userptr+0x36/0x110 [videobuf2_vmalloc]
Jun 14 18:44:07 test-media kernel: [ 49.376810] [<ffffffffa0398e0a>] ? _vb2_fop_release+0x2a/0xb0 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376812] [<ffffffffa0394626>] __vb2_queue_free+0x146/0x5e0 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376814] [<ffffffffa0398dd3>] vb2_queue_release+0x33/0x40 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376816] [<ffffffffa0398e75>] _vb2_fop_release+0x95/0xb0 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376818] [<ffffffffa0398eb9>] vb2_fop_release+0x29/0x50 [videobuf2_core]
Jun 14 18:44:07 test-media kernel: [ 49.376820] [<ffffffffa03b6372>] vivid_fop_release+0x92/0x230 [vivid]
Jun 14 18:44:07 test-media kernel: [ 49.376822] [<ffffffffa0361460>] v4l2_release+0x30/0x80 [videodev]
Jun 14 18:44:07 test-media kernel: [ 49.376824] [<ffffffff811a51d5>] __fput+0xe5/0x200
Jun 14 18:44:07 test-media kernel: [ 49.376825] [<ffffffff819b4653>] ? int_very_careful+0x5/0x46
Jun 14 18:44:07 test-media kernel: [ 49.376827] [<ffffffff811a5339>] ____fput+0x9/0x10
Jun 14 18:44:07 test-media kernel: [ 49.376828] [<ffffffff810a9fa4>] task_work_run+0xc4/0xf0
Jun 14 18:44:07 test-media kernel: [ 49.376830] [<ffffffff81002dd1>] do_notify_resume+0x41/0x60
Jun 14 18:44:07 test-media kernel: [ 49.376832] [<ffffffff819b46a6>] int_signal+0x12/0x17
This can be triggered by loading the vivid module with the module option 'no_error_inj=1'
and running 'v4l2-compliance -s5'. Again, it may take a few attempts to trigger this
but for me it happens quite quickly.
Without this patch I cannot reproduce these two issues. So reverting is the best
solution for now.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
886 lines
20 KiB
C
886 lines
20 KiB
C
/*
|
|
* videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
|
|
*
|
|
* Copyright (C) 2010 Samsung Electronics
|
|
*
|
|
* Author: Pawel Osciak <pawel@osciak.com>
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#include <linux/dma-buf.h>
|
|
#include <linux/module.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <media/videobuf2-core.h>
|
|
#include <media/videobuf2-dma-contig.h>
|
|
#include <media/videobuf2-memops.h>
|
|
|
|
struct vb2_dc_conf {
|
|
struct device *dev;
|
|
};
|
|
|
|
struct vb2_dc_buf {
|
|
struct device *dev;
|
|
void *vaddr;
|
|
unsigned long size;
|
|
dma_addr_t dma_addr;
|
|
enum dma_data_direction dma_dir;
|
|
struct sg_table *dma_sgt;
|
|
|
|
/* MMAP related */
|
|
struct vb2_vmarea_handler handler;
|
|
atomic_t refcount;
|
|
struct sg_table *sgt_base;
|
|
|
|
/* USERPTR related */
|
|
struct vm_area_struct *vma;
|
|
|
|
/* DMABUF related */
|
|
struct dma_buf_attachment *db_attach;
|
|
};
|
|
|
|
/*********************************************/
|
|
/* scatterlist table functions */
|
|
/*********************************************/
|
|
|
|
|
|
static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
|
|
void (*cb)(struct page *pg))
|
|
{
|
|
struct scatterlist *s;
|
|
unsigned int i;
|
|
|
|
for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
|
|
struct page *page = sg_page(s);
|
|
unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
|
|
>> PAGE_SHIFT;
|
|
unsigned int j;
|
|
|
|
for (j = 0; j < n_pages; ++j, ++page)
|
|
cb(page);
|
|
}
|
|
}
|
|
|
|
static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
|
|
{
|
|
struct scatterlist *s;
|
|
dma_addr_t expected = sg_dma_address(sgt->sgl);
|
|
unsigned int i;
|
|
unsigned long size = 0;
|
|
|
|
for_each_sg(sgt->sgl, s, sgt->nents, i) {
|
|
if (sg_dma_address(s) != expected)
|
|
break;
|
|
expected = sg_dma_address(s) + sg_dma_len(s);
|
|
size += sg_dma_len(s);
|
|
}
|
|
return size;
|
|
}
|
|
|
|
/*********************************************/
|
|
/* callbacks for all buffers */
|
|
/*********************************************/
|
|
|
|
static void *vb2_dc_cookie(void *buf_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
|
|
return &buf->dma_addr;
|
|
}
|
|
|
|
static void *vb2_dc_vaddr(void *buf_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
|
|
if (!buf->vaddr && buf->db_attach)
|
|
buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
|
|
|
|
return buf->vaddr;
|
|
}
|
|
|
|
static unsigned int vb2_dc_num_users(void *buf_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
|
|
return atomic_read(&buf->refcount);
|
|
}
|
|
|
|
static void vb2_dc_prepare(void *buf_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
struct sg_table *sgt = buf->dma_sgt;
|
|
|
|
/* DMABUF exporter will flush the cache for us */
|
|
if (!sgt || buf->db_attach)
|
|
return;
|
|
|
|
dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
|
|
}
|
|
|
|
static void vb2_dc_finish(void *buf_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
struct sg_table *sgt = buf->dma_sgt;
|
|
|
|
/* DMABUF exporter will flush the cache for us */
|
|
if (!sgt || buf->db_attach)
|
|
return;
|
|
|
|
dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
|
|
}
|
|
|
|
/*********************************************/
|
|
/* callbacks for MMAP buffers */
|
|
/*********************************************/
|
|
|
|
static void vb2_dc_put(void *buf_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
|
|
if (!atomic_dec_and_test(&buf->refcount))
|
|
return;
|
|
|
|
if (buf->sgt_base) {
|
|
sg_free_table(buf->sgt_base);
|
|
kfree(buf->sgt_base);
|
|
}
|
|
dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
|
|
put_device(buf->dev);
|
|
kfree(buf);
|
|
}
|
|
|
|
static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size,
|
|
enum dma_data_direction dma_dir, gfp_t gfp_flags)
|
|
{
|
|
struct vb2_dc_conf *conf = alloc_ctx;
|
|
struct device *dev = conf->dev;
|
|
struct vb2_dc_buf *buf;
|
|
|
|
buf = kzalloc(sizeof *buf, GFP_KERNEL);
|
|
if (!buf)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr,
|
|
GFP_KERNEL | gfp_flags);
|
|
if (!buf->vaddr) {
|
|
dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
|
|
kfree(buf);
|
|
return ERR_PTR(-ENOMEM);
|
|
}
|
|
|
|
/* Prevent the device from being released while the buffer is used */
|
|
buf->dev = get_device(dev);
|
|
buf->size = size;
|
|
buf->dma_dir = dma_dir;
|
|
|
|
buf->handler.refcount = &buf->refcount;
|
|
buf->handler.put = vb2_dc_put;
|
|
buf->handler.arg = buf;
|
|
|
|
atomic_inc(&buf->refcount);
|
|
|
|
return buf;
|
|
}
|
|
|
|
static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
int ret;
|
|
|
|
if (!buf) {
|
|
printk(KERN_ERR "No buffer to map\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
/*
|
|
* dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
|
|
* map whole buffer
|
|
*/
|
|
vma->vm_pgoff = 0;
|
|
|
|
ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr,
|
|
buf->dma_addr, buf->size);
|
|
|
|
if (ret) {
|
|
pr_err("Remapping memory failed, error: %d\n", ret);
|
|
return ret;
|
|
}
|
|
|
|
vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
|
|
vma->vm_private_data = &buf->handler;
|
|
vma->vm_ops = &vb2_common_vm_ops;
|
|
|
|
vma->vm_ops->open(vma);
|
|
|
|
pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
|
|
__func__, (unsigned long)buf->dma_addr, vma->vm_start,
|
|
buf->size);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*********************************************/
|
|
/* DMABUF ops for exporters */
|
|
/*********************************************/
|
|
|
|
struct vb2_dc_attachment {
|
|
struct sg_table sgt;
|
|
enum dma_data_direction dma_dir;
|
|
};
|
|
|
|
static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
|
|
struct dma_buf_attachment *dbuf_attach)
|
|
{
|
|
struct vb2_dc_attachment *attach;
|
|
unsigned int i;
|
|
struct scatterlist *rd, *wr;
|
|
struct sg_table *sgt;
|
|
struct vb2_dc_buf *buf = dbuf->priv;
|
|
int ret;
|
|
|
|
attach = kzalloc(sizeof(*attach), GFP_KERNEL);
|
|
if (!attach)
|
|
return -ENOMEM;
|
|
|
|
sgt = &attach->sgt;
|
|
/* Copy the buf->base_sgt scatter list to the attachment, as we can't
|
|
* map the same scatter list to multiple attachments at the same time.
|
|
*/
|
|
ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
|
|
if (ret) {
|
|
kfree(attach);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
rd = buf->sgt_base->sgl;
|
|
wr = sgt->sgl;
|
|
for (i = 0; i < sgt->orig_nents; ++i) {
|
|
sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
|
|
rd = sg_next(rd);
|
|
wr = sg_next(wr);
|
|
}
|
|
|
|
attach->dma_dir = DMA_NONE;
|
|
dbuf_attach->priv = attach;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
|
|
struct dma_buf_attachment *db_attach)
|
|
{
|
|
struct vb2_dc_attachment *attach = db_attach->priv;
|
|
struct sg_table *sgt;
|
|
|
|
if (!attach)
|
|
return;
|
|
|
|
sgt = &attach->sgt;
|
|
|
|
/* release the scatterlist cache */
|
|
if (attach->dma_dir != DMA_NONE)
|
|
dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
|
|
attach->dma_dir);
|
|
sg_free_table(sgt);
|
|
kfree(attach);
|
|
db_attach->priv = NULL;
|
|
}
|
|
|
|
static struct sg_table *vb2_dc_dmabuf_ops_map(
|
|
struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
|
|
{
|
|
struct vb2_dc_attachment *attach = db_attach->priv;
|
|
/* stealing dmabuf mutex to serialize map/unmap operations */
|
|
struct mutex *lock = &db_attach->dmabuf->lock;
|
|
struct sg_table *sgt;
|
|
|
|
mutex_lock(lock);
|
|
|
|
sgt = &attach->sgt;
|
|
/* return previously mapped sg table */
|
|
if (attach->dma_dir == dma_dir) {
|
|
mutex_unlock(lock);
|
|
return sgt;
|
|
}
|
|
|
|
/* release any previous cache */
|
|
if (attach->dma_dir != DMA_NONE) {
|
|
dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
|
|
attach->dma_dir);
|
|
attach->dma_dir = DMA_NONE;
|
|
}
|
|
|
|
/* mapping to the client with new direction */
|
|
sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
|
|
dma_dir);
|
|
if (!sgt->nents) {
|
|
pr_err("failed to map scatterlist\n");
|
|
mutex_unlock(lock);
|
|
return ERR_PTR(-EIO);
|
|
}
|
|
|
|
attach->dma_dir = dma_dir;
|
|
|
|
mutex_unlock(lock);
|
|
|
|
return sgt;
|
|
}
|
|
|
|
static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
|
|
struct sg_table *sgt, enum dma_data_direction dma_dir)
|
|
{
|
|
/* nothing to be done here */
|
|
}
|
|
|
|
static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
|
|
{
|
|
/* drop reference obtained in vb2_dc_get_dmabuf */
|
|
vb2_dc_put(dbuf->priv);
|
|
}
|
|
|
|
static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
|
|
{
|
|
struct vb2_dc_buf *buf = dbuf->priv;
|
|
|
|
return buf->vaddr + pgnum * PAGE_SIZE;
|
|
}
|
|
|
|
static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
|
|
{
|
|
struct vb2_dc_buf *buf = dbuf->priv;
|
|
|
|
return buf->vaddr;
|
|
}
|
|
|
|
static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
|
|
struct vm_area_struct *vma)
|
|
{
|
|
return vb2_dc_mmap(dbuf->priv, vma);
|
|
}
|
|
|
|
static struct dma_buf_ops vb2_dc_dmabuf_ops = {
|
|
.attach = vb2_dc_dmabuf_ops_attach,
|
|
.detach = vb2_dc_dmabuf_ops_detach,
|
|
.map_dma_buf = vb2_dc_dmabuf_ops_map,
|
|
.unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
|
|
.kmap = vb2_dc_dmabuf_ops_kmap,
|
|
.kmap_atomic = vb2_dc_dmabuf_ops_kmap,
|
|
.vmap = vb2_dc_dmabuf_ops_vmap,
|
|
.mmap = vb2_dc_dmabuf_ops_mmap,
|
|
.release = vb2_dc_dmabuf_ops_release,
|
|
};
|
|
|
|
static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
|
|
{
|
|
int ret;
|
|
struct sg_table *sgt;
|
|
|
|
sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
|
|
if (!sgt) {
|
|
dev_err(buf->dev, "failed to alloc sg table\n");
|
|
return NULL;
|
|
}
|
|
|
|
ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr,
|
|
buf->size);
|
|
if (ret < 0) {
|
|
dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
|
|
kfree(sgt);
|
|
return NULL;
|
|
}
|
|
|
|
return sgt;
|
|
}
|
|
|
|
static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
struct dma_buf *dbuf;
|
|
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
|
|
|
|
exp_info.ops = &vb2_dc_dmabuf_ops;
|
|
exp_info.size = buf->size;
|
|
exp_info.flags = flags;
|
|
exp_info.priv = buf;
|
|
|
|
if (!buf->sgt_base)
|
|
buf->sgt_base = vb2_dc_get_base_sgt(buf);
|
|
|
|
if (WARN_ON(!buf->sgt_base))
|
|
return NULL;
|
|
|
|
dbuf = dma_buf_export(&exp_info);
|
|
if (IS_ERR(dbuf))
|
|
return NULL;
|
|
|
|
/* dmabuf keeps reference to vb2 buffer */
|
|
atomic_inc(&buf->refcount);
|
|
|
|
return dbuf;
|
|
}
|
|
|
|
/*********************************************/
|
|
/* callbacks for USERPTR buffers */
|
|
/*********************************************/
|
|
|
|
static inline int vma_is_io(struct vm_area_struct *vma)
|
|
{
|
|
return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
|
|
}
|
|
|
|
static int vb2_dc_get_user_pfn(unsigned long start, int n_pages,
|
|
struct vm_area_struct *vma, unsigned long *res)
|
|
{
|
|
unsigned long pfn, start_pfn, prev_pfn;
|
|
unsigned int i;
|
|
int ret;
|
|
|
|
if (!vma_is_io(vma))
|
|
return -EFAULT;
|
|
|
|
ret = follow_pfn(vma, start, &pfn);
|
|
if (ret)
|
|
return ret;
|
|
|
|
start_pfn = pfn;
|
|
start += PAGE_SIZE;
|
|
|
|
for (i = 1; i < n_pages; ++i, start += PAGE_SIZE) {
|
|
prev_pfn = pfn;
|
|
ret = follow_pfn(vma, start, &pfn);
|
|
|
|
if (ret) {
|
|
pr_err("no page for address %lu\n", start);
|
|
return ret;
|
|
}
|
|
if (pfn != prev_pfn + 1)
|
|
return -EINVAL;
|
|
}
|
|
|
|
*res = start_pfn;
|
|
return 0;
|
|
}
|
|
|
|
static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
|
|
int n_pages, struct vm_area_struct *vma,
|
|
enum dma_data_direction dma_dir)
|
|
{
|
|
if (vma_is_io(vma)) {
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
|
|
unsigned long pfn;
|
|
int ret = follow_pfn(vma, start, &pfn);
|
|
|
|
if (!pfn_valid(pfn))
|
|
return -EINVAL;
|
|
|
|
if (ret) {
|
|
pr_err("no page for address %lu\n", start);
|
|
return ret;
|
|
}
|
|
pages[i] = pfn_to_page(pfn);
|
|
}
|
|
} else {
|
|
int n;
|
|
|
|
n = get_user_pages(current, current->mm, start & PAGE_MASK,
|
|
n_pages, dma_dir == DMA_FROM_DEVICE, 1, pages, NULL);
|
|
/* negative error means that no page was pinned */
|
|
n = max(n, 0);
|
|
if (n != n_pages) {
|
|
pr_err("got only %d of %d user pages\n", n, n_pages);
|
|
while (n)
|
|
put_page(pages[--n]);
|
|
return -EFAULT;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void vb2_dc_put_dirty_page(struct page *page)
|
|
{
|
|
set_page_dirty_lock(page);
|
|
put_page(page);
|
|
}
|
|
|
|
static void vb2_dc_put_userptr(void *buf_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = buf_priv;
|
|
struct sg_table *sgt = buf->dma_sgt;
|
|
|
|
if (sgt) {
|
|
DEFINE_DMA_ATTRS(attrs);
|
|
|
|
dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
|
|
/*
|
|
* No need to sync to CPU, it's already synced to the CPU
|
|
* since the finish() memop will have been called before this.
|
|
*/
|
|
dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
|
|
buf->dma_dir, &attrs);
|
|
if (!vma_is_io(buf->vma))
|
|
vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
|
|
|
|
sg_free_table(sgt);
|
|
kfree(sgt);
|
|
}
|
|
vb2_put_vma(buf->vma);
|
|
kfree(buf);
|
|
}
|
|
|
|
/*
|
|
* For some kind of reserved memory there might be no struct page available,
|
|
* so all that can be done to support such 'pages' is to try to convert
|
|
* pfn to dma address or at the last resort just assume that
|
|
* dma address == physical address (like it has been assumed in earlier version
|
|
* of videobuf2-dma-contig
|
|
*/
|
|
|
|
#ifdef __arch_pfn_to_dma
|
|
static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
|
|
{
|
|
return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
|
|
}
|
|
#elif defined(__pfn_to_bus)
|
|
static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
|
|
{
|
|
return (dma_addr_t)__pfn_to_bus(pfn);
|
|
}
|
|
#elif defined(__pfn_to_phys)
|
|
static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
|
|
{
|
|
return (dma_addr_t)__pfn_to_phys(pfn);
|
|
}
|
|
#else
|
|
static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
|
|
{
|
|
/* really, we cannot do anything better at this point */
|
|
return (dma_addr_t)(pfn) << PAGE_SHIFT;
|
|
}
|
|
#endif
|
|
|
|
static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
|
|
unsigned long size, enum dma_data_direction dma_dir)
|
|
{
|
|
struct vb2_dc_conf *conf = alloc_ctx;
|
|
struct vb2_dc_buf *buf;
|
|
unsigned long start;
|
|
unsigned long end;
|
|
unsigned long offset;
|
|
struct page **pages;
|
|
int n_pages;
|
|
int ret = 0;
|
|
struct vm_area_struct *vma;
|
|
struct sg_table *sgt;
|
|
unsigned long contig_size;
|
|
unsigned long dma_align = dma_get_cache_alignment();
|
|
DEFINE_DMA_ATTRS(attrs);
|
|
|
|
dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
|
|
|
|
/* Only cache aligned DMA transfers are reliable */
|
|
if (!IS_ALIGNED(vaddr | size, dma_align)) {
|
|
pr_debug("user data must be aligned to %lu bytes\n", dma_align);
|
|
return ERR_PTR(-EINVAL);
|
|
}
|
|
|
|
if (!size) {
|
|
pr_debug("size is zero\n");
|
|
return ERR_PTR(-EINVAL);
|
|
}
|
|
|
|
buf = kzalloc(sizeof *buf, GFP_KERNEL);
|
|
if (!buf)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
buf->dev = conf->dev;
|
|
buf->dma_dir = dma_dir;
|
|
|
|
start = vaddr & PAGE_MASK;
|
|
offset = vaddr & ~PAGE_MASK;
|
|
end = PAGE_ALIGN(vaddr + size);
|
|
n_pages = (end - start) >> PAGE_SHIFT;
|
|
|
|
pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
|
|
if (!pages) {
|
|
ret = -ENOMEM;
|
|
pr_err("failed to allocate pages table\n");
|
|
goto fail_buf;
|
|
}
|
|
|
|
/* current->mm->mmap_sem is taken by videobuf2 core */
|
|
vma = find_vma(current->mm, vaddr);
|
|
if (!vma) {
|
|
pr_err("no vma for address %lu\n", vaddr);
|
|
ret = -EFAULT;
|
|
goto fail_pages;
|
|
}
|
|
|
|
if (vma->vm_end < vaddr + size) {
|
|
pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
|
|
ret = -EFAULT;
|
|
goto fail_pages;
|
|
}
|
|
|
|
buf->vma = vb2_get_vma(vma);
|
|
if (!buf->vma) {
|
|
pr_err("failed to copy vma\n");
|
|
ret = -ENOMEM;
|
|
goto fail_pages;
|
|
}
|
|
|
|
/* extract page list from userspace mapping */
|
|
ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, dma_dir);
|
|
if (ret) {
|
|
unsigned long pfn;
|
|
if (vb2_dc_get_user_pfn(start, n_pages, vma, &pfn) == 0) {
|
|
buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, pfn);
|
|
buf->size = size;
|
|
kfree(pages);
|
|
return buf;
|
|
}
|
|
|
|
pr_err("failed to get user pages\n");
|
|
goto fail_vma;
|
|
}
|
|
|
|
sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
|
|
if (!sgt) {
|
|
pr_err("failed to allocate sg table\n");
|
|
ret = -ENOMEM;
|
|
goto fail_get_user_pages;
|
|
}
|
|
|
|
ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
|
|
offset, size, GFP_KERNEL);
|
|
if (ret) {
|
|
pr_err("failed to initialize sg table\n");
|
|
goto fail_sgt;
|
|
}
|
|
|
|
/* pages are no longer needed */
|
|
kfree(pages);
|
|
pages = NULL;
|
|
|
|
/*
|
|
* No need to sync to the device, this will happen later when the
|
|
* prepare() memop is called.
|
|
*/
|
|
sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
|
|
buf->dma_dir, &attrs);
|
|
if (sgt->nents <= 0) {
|
|
pr_err("failed to map scatterlist\n");
|
|
ret = -EIO;
|
|
goto fail_sgt_init;
|
|
}
|
|
|
|
contig_size = vb2_dc_get_contiguous_size(sgt);
|
|
if (contig_size < size) {
|
|
pr_err("contiguous mapping is too small %lu/%lu\n",
|
|
contig_size, size);
|
|
ret = -EFAULT;
|
|
goto fail_map_sg;
|
|
}
|
|
|
|
buf->dma_addr = sg_dma_address(sgt->sgl);
|
|
buf->size = size;
|
|
buf->dma_sgt = sgt;
|
|
|
|
return buf;
|
|
|
|
fail_map_sg:
|
|
dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
|
|
buf->dma_dir, &attrs);
|
|
|
|
fail_sgt_init:
|
|
if (!vma_is_io(buf->vma))
|
|
vb2_dc_sgt_foreach_page(sgt, put_page);
|
|
sg_free_table(sgt);
|
|
|
|
fail_sgt:
|
|
kfree(sgt);
|
|
|
|
fail_get_user_pages:
|
|
if (pages && !vma_is_io(buf->vma))
|
|
while (n_pages)
|
|
put_page(pages[--n_pages]);
|
|
|
|
fail_vma:
|
|
vb2_put_vma(buf->vma);
|
|
|
|
fail_pages:
|
|
kfree(pages); /* kfree is NULL-proof */
|
|
|
|
fail_buf:
|
|
kfree(buf);
|
|
|
|
return ERR_PTR(ret);
|
|
}
|
|
|
|
/*********************************************/
|
|
/* callbacks for DMABUF buffers */
|
|
/*********************************************/
|
|
|
|
static int vb2_dc_map_dmabuf(void *mem_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = mem_priv;
|
|
struct sg_table *sgt;
|
|
unsigned long contig_size;
|
|
|
|
if (WARN_ON(!buf->db_attach)) {
|
|
pr_err("trying to pin a non attached buffer\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (WARN_ON(buf->dma_sgt)) {
|
|
pr_err("dmabuf buffer is already pinned\n");
|
|
return 0;
|
|
}
|
|
|
|
/* get the associated scatterlist for this buffer */
|
|
sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
|
|
if (IS_ERR(sgt)) {
|
|
pr_err("Error getting dmabuf scatterlist\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* checking if dmabuf is big enough to store contiguous chunk */
|
|
contig_size = vb2_dc_get_contiguous_size(sgt);
|
|
if (contig_size < buf->size) {
|
|
pr_err("contiguous chunk is too small %lu/%lu b\n",
|
|
contig_size, buf->size);
|
|
dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
|
|
return -EFAULT;
|
|
}
|
|
|
|
buf->dma_addr = sg_dma_address(sgt->sgl);
|
|
buf->dma_sgt = sgt;
|
|
buf->vaddr = NULL;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void vb2_dc_unmap_dmabuf(void *mem_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = mem_priv;
|
|
struct sg_table *sgt = buf->dma_sgt;
|
|
|
|
if (WARN_ON(!buf->db_attach)) {
|
|
pr_err("trying to unpin a not attached buffer\n");
|
|
return;
|
|
}
|
|
|
|
if (WARN_ON(!sgt)) {
|
|
pr_err("dmabuf buffer is already unpinned\n");
|
|
return;
|
|
}
|
|
|
|
if (buf->vaddr) {
|
|
dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
|
|
buf->vaddr = NULL;
|
|
}
|
|
dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
|
|
|
|
buf->dma_addr = 0;
|
|
buf->dma_sgt = NULL;
|
|
}
|
|
|
|
static void vb2_dc_detach_dmabuf(void *mem_priv)
|
|
{
|
|
struct vb2_dc_buf *buf = mem_priv;
|
|
|
|
/* if vb2 works correctly you should never detach mapped buffer */
|
|
if (WARN_ON(buf->dma_addr))
|
|
vb2_dc_unmap_dmabuf(buf);
|
|
|
|
/* detach this attachment */
|
|
dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
|
|
kfree(buf);
|
|
}
|
|
|
|
static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
|
|
unsigned long size, enum dma_data_direction dma_dir)
|
|
{
|
|
struct vb2_dc_conf *conf = alloc_ctx;
|
|
struct vb2_dc_buf *buf;
|
|
struct dma_buf_attachment *dba;
|
|
|
|
if (dbuf->size < size)
|
|
return ERR_PTR(-EFAULT);
|
|
|
|
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
|
|
if (!buf)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
buf->dev = conf->dev;
|
|
/* create attachment for the dmabuf with the user device */
|
|
dba = dma_buf_attach(dbuf, buf->dev);
|
|
if (IS_ERR(dba)) {
|
|
pr_err("failed to attach dmabuf\n");
|
|
kfree(buf);
|
|
return dba;
|
|
}
|
|
|
|
buf->dma_dir = dma_dir;
|
|
buf->size = size;
|
|
buf->db_attach = dba;
|
|
|
|
return buf;
|
|
}
|
|
|
|
/*********************************************/
|
|
/* DMA CONTIG exported functions */
|
|
/*********************************************/
|
|
|
|
const struct vb2_mem_ops vb2_dma_contig_memops = {
|
|
.alloc = vb2_dc_alloc,
|
|
.put = vb2_dc_put,
|
|
.get_dmabuf = vb2_dc_get_dmabuf,
|
|
.cookie = vb2_dc_cookie,
|
|
.vaddr = vb2_dc_vaddr,
|
|
.mmap = vb2_dc_mmap,
|
|
.get_userptr = vb2_dc_get_userptr,
|
|
.put_userptr = vb2_dc_put_userptr,
|
|
.prepare = vb2_dc_prepare,
|
|
.finish = vb2_dc_finish,
|
|
.map_dmabuf = vb2_dc_map_dmabuf,
|
|
.unmap_dmabuf = vb2_dc_unmap_dmabuf,
|
|
.attach_dmabuf = vb2_dc_attach_dmabuf,
|
|
.detach_dmabuf = vb2_dc_detach_dmabuf,
|
|
.num_users = vb2_dc_num_users,
|
|
};
|
|
EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
|
|
|
|
void *vb2_dma_contig_init_ctx(struct device *dev)
|
|
{
|
|
struct vb2_dc_conf *conf;
|
|
|
|
conf = kzalloc(sizeof *conf, GFP_KERNEL);
|
|
if (!conf)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
conf->dev = dev;
|
|
|
|
return conf;
|
|
}
|
|
EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
|
|
|
|
void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
|
|
{
|
|
if (!IS_ERR_OR_NULL(alloc_ctx))
|
|
kfree(alloc_ctx);
|
|
}
|
|
EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
|
|
|
|
MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
|
|
MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
|
|
MODULE_LICENSE("GPL");
|