From 64021a2ec150204437cf606f053528c36a46b5ea Mon Sep 17 00:00:00 2001
From: Mattijs Korpershoek <mkorpershoek@baylibre.com>
Date: Mon, 25 Mar 2024 19:12:44 +0100
Subject: [PATCH] TI: add DMABUF_CARVEOUT custom allocator support for decoder

In 48 bits mode, the wave5 video decoder/encoder has a hardware limitation:

For each memory access, the upper 16 bits of the 48 bit address *must* be
identical.

In order to enforce this addressing limitation, we can use a different dmabuf
heap, named carveout[1].

Add support for the carveout heap allocator in plugin_store and make sure that
this allocator is used by default for all input buffers.

Note: the output allocator still uses gralloc.
This requires a custom gralloc implementation which uses the carveout heap
whenever the GRALLOC_USAGE_HW_VIDEO_{DECODER,ENCODER} flags are used.

To tell the c2 framework that this allocator is available, make sure to set the
c2-poolmask property:

    debug.stagefright.c2-poolmask=0x1f50000

(DMABUF_CARVEOUT is bit 24)

[1] https://git.ti.com/cgit/ti-linux-kernel/ti-linux-kernel/tree/drivers/dma-buf/heaps/carveout-heap.c?h=ti-linux-6.1.y
Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
---
 components/V4L2DecodeInterface.cpp                         | 2 +-
 plugin_store/C2CarveoutDmaBufAllocator.cpp                 | 2 +-
 plugin_store/V4L2PluginStore.cpp                           | 7 +++++++
 .../include/v4l2_codec2/plugin_store/V4L2AllocatorId.h     | 1 +
 4 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/components/V4L2DecodeInterface.cpp b/components/V4L2DecodeInterface.cpp
index d22f77b..6e7d393 100644
--- a/components/V4L2DecodeInterface.cpp
+++ b/components/V4L2DecodeInterface.cpp
@@ -265,7 +265,7 @@ V4L2DecodeInterface::V4L2DecodeInterface(const std::string& name,
 
     bool secureMode = name.find(".secure") != std::string::npos;
     const C2Allocator::id_t inputAllocators[] = {secureMode ? V4L2AllocatorId::SECURE_LINEAR
-                                                            : C2AllocatorStore::DEFAULT_LINEAR};
+                                                            : V4L2AllocatorId::DMABUF_CARVEOUT};
 
     const C2Allocator::id_t outputAllocators[] = {V4L2AllocatorId::V4L2_BUFFERPOOL};
     const C2Allocator::id_t surfaceAllocator =
diff --git a/plugin_store/C2CarveoutDmaBufAllocator.cpp b/plugin_store/C2CarveoutDmaBufAllocator.cpp
index 3e0e5e6..89b37cb 100644
--- a/plugin_store/C2CarveoutDmaBufAllocator.cpp
+++ b/plugin_store/C2CarveoutDmaBufAllocator.cpp
@@ -348,7 +348,7 @@ c2_status_t C2CarveoutDmaBufAllocator::mapUsage(C2MemoryUsage usage, size_t capa
                 !(usage.expected & (C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE)))
                 *heap_name = "system-uncached";
             else
-                *heap_name = "system";
+                *heap_name = "carveout_video";
             *flags = 0;
             res = C2_NO_INIT;
         }
diff --git a/plugin_store/V4L2PluginStore.cpp b/plugin_store/V4L2PluginStore.cpp
index 2d53c5f..040ddce 100644
--- a/plugin_store/V4L2PluginStore.cpp
+++ b/plugin_store/V4L2PluginStore.cpp
@@ -17,6 +17,7 @@
 
 #include <v4l2_codec2/plugin_store/C2VdaBqBlockPool.h>
 #include <v4l2_codec2/plugin_store/C2VdaPooledBlockPool.h>
+#include <v4l2_codec2/plugin_store/C2CarveoutDmaBufAllocator.h>
 #include <v4l2_codec2/plugin_store/V4L2AllocatorId.h>
 #include <v4l2_codec2/plugin_store/VendorAllocatorLoader.h>
 
@@ -32,6 +33,9 @@ C2Allocator* createAllocator(C2Allocator::id_t allocatorId) {
         return sAllocatorLoader->createAllocator(allocatorId);
     }
 
+    if (allocatorId == V4L2AllocatorId::DMABUF_CARVEOUT)
+        return new C2CarveoutDmaBufAllocator(allocatorId);
+
     ALOGI("%s(): Fallback to create C2AllocatorGralloc(id=%u)", __func__, allocatorId);
     return new C2AllocatorGralloc(allocatorId, true);
 }
@@ -79,6 +83,9 @@ C2BlockPool* createBlockPool(C2Allocator::id_t allocatorId, C2BlockPool::local_i
     case V4L2AllocatorId::SECURE_GRAPHIC:
         return new C2VdaBqBlockPool(allocator, poolId);
 
+    case V4L2AllocatorId::DMABUF_CARVEOUT:
+        return new C2PooledBlockPool(allocator, poolId);
+
     default:
         ALOGE("%s(): Unknown allocator id=%u", __func__, allocatorId);
         return nullptr;
diff --git a/plugin_store/include/v4l2_codec2/plugin_store/V4L2AllocatorId.h b/plugin_store/include/v4l2_codec2/plugin_store/V4L2AllocatorId.h
index 0808963..0106eda 100644
--- a/plugin_store/include/v4l2_codec2/plugin_store/V4L2AllocatorId.h
+++ b/plugin_store/include/v4l2_codec2/plugin_store/V4L2AllocatorId.h
@@ -16,6 +16,7 @@ enum : C2AllocatorStore::id_t {
     V4L2_BUFFERPOOL,
     SECURE_LINEAR,
     SECURE_GRAPHIC,
+    DMABUF_CARVEOUT,
 };
 
 }  // namespace V4L2AllocatorId
-- 
GitLab