From a48aa3f48abb4b9445be958c4c83bc7a7c0b4e50 Mon Sep 17 00:00:00 2001
From: Mattijs Korpershoek <mkorpershoek@baylibre.com>
Date: Mon, 25 Nov 2024 10:43:39 +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>
Signed-off-by: Guillaume La Roque <glaroque@baylibre.com>
---
 components/DecodeInterface.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/DecodeInterface.cpp b/components/DecodeInterface.cpp
index 53689bd..13f0be4 100644
--- a/components/DecodeInterface.cpp
+++ b/components/DecodeInterface.cpp
@@ -315,7 +315,7 @@ DecodeInterface::DecodeInterface(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[] = {C2PlatformAllocatorStore::GRALLOC};
     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 1ad624c..e7cbffd 100644
--- a/plugin_store/V4L2PluginStore.cpp
+++ b/plugin_store/V4L2PluginStore.cpp
@@ -17,6 +17,7 @@
 #include <log/log.h>
 
 #include <v4l2_codec2/plugin_store/V4L2AllocatorId.h>
+#include <v4l2_codec2/plugin_store/C2CarveoutDmaBufAllocator.h>
 #include <v4l2_codec2/plugin_store/VendorAllocatorLoader.h>
 
 namespace android {
@@ -47,6 +48,9 @@ C2Allocator* createAllocator(C2Allocator::id_t allocatorId) {
         return allocatorLoader->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);
 }
@@ -98,6 +102,9 @@ C2BlockPool* createBlockPool(C2Allocator::id_t allocatorId, C2BlockPool::local_i
     case V4L2AllocatorId::SECURE_GRAPHIC:
         return new C2BufferQueueBlockPool(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 2353076..6fcf88e 100644
--- a/plugin_store/include/v4l2_codec2/plugin_store/V4L2AllocatorId.h
+++ b/plugin_store/include/v4l2_codec2/plugin_store/V4L2AllocatorId.h
@@ -14,6 +14,7 @@ namespace V4L2AllocatorId {
 enum : C2AllocatorStore::id_t {
     SECURE_LINEAR = C2PlatformAllocatorStore::PLATFORM_END,
     SECURE_GRAPHIC,
+    DMABUF_CARVEOUT,
 };
 
 }  // namespace V4L2AllocatorId
-- 
GitLab