about summary refs log tree commit diff
path: root/users/flokli/ipu6-softisp/libcamera/0002-libcamera-internal-Move-dma_heaps.-h-cpp-to-common-d.patch
blob: 450a0a21f19aa57750703ded019be9227d5085f9 (plain) (blame)
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
From 96e50c6a43352a9cb81d558fea27e580f2b26585 Mon Sep 17 00:00:00 2001
From: Andrey Konovalov <andrey.konovalov@linaro.org>
Date: Mon, 11 Mar 2024 15:15:06 +0100
Subject: [PATCH 02/21] libcamera: internal: Move dma_heaps.[h, cpp] to common
 directories

DmaHeap class is useful outside the RPi pipeline handler too.

Move dma_heaps.h and dma_heaps.cpp to common directories. Update
the build files and RPi vc4 pipeline handler accordingly.

Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> # sc8280xp Lenovo x13s
Tested-by: Pavel Machek <pavel@ucw.cz>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrey Konovalov <andrey.konovalov@linaro.org>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
---
 .../libcamera/internal}/dma_heaps.h           |   4 -
 include/libcamera/internal/meson.build        |   1 +
 src/libcamera/dma_heaps.cpp                   | 127 ++++++++++++++++++
 src/libcamera/meson.build                     |   1 +
 src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp  |  90 -------------
 src/libcamera/pipeline/rpi/vc4/meson.build    |   1 -
 src/libcamera/pipeline/rpi/vc4/vc4.cpp        |   5 +-
 7 files changed, 131 insertions(+), 98 deletions(-)
 rename {src/libcamera/pipeline/rpi/vc4 => include/libcamera/internal}/dma_heaps.h (92%)
 create mode 100644 src/libcamera/dma_heaps.cpp
 delete mode 100644 src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp

diff --git a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h b/include/libcamera/internal/dma_heaps.h
similarity index 92%
rename from src/libcamera/pipeline/rpi/vc4/dma_heaps.h
rename to include/libcamera/internal/dma_heaps.h
index 0a4a8d86..cff8f140 100644
--- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h
+++ b/include/libcamera/internal/dma_heaps.h
@@ -13,8 +13,6 @@
 
 namespace libcamera {
 
-namespace RPi {
-
 class DmaHeap
 {
 public:
@@ -27,6 +25,4 @@ private:
 	UniqueFD dmaHeapHandle_;
 };
 
-} /* namespace RPi */
-
 } /* namespace libcamera */
diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build
index 7f1f3440..33eb0fb3 100644
--- a/include/libcamera/internal/meson.build
+++ b/include/libcamera/internal/meson.build
@@ -25,6 +25,7 @@ libcamera_internal_headers = files([
     'device_enumerator.h',
     'device_enumerator_sysfs.h',
     'device_enumerator_udev.h',
+    'dma_heaps.h',
     'formats.h',
     'framebuffer.h',
     'ipa_manager.h',
diff --git a/src/libcamera/dma_heaps.cpp b/src/libcamera/dma_heaps.cpp
new file mode 100644
index 00000000..38ef175a
--- /dev/null
+++ b/src/libcamera/dma_heaps.cpp
@@ -0,0 +1,127 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2020, Raspberry Pi Ltd
+ *
+ * dma_heaps.h - Helper class for dma-heap allocations.
+ */
+
+#include "libcamera/internal/dma_heaps.h"
+
+#include <array>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include <linux/dma-buf.h>
+#include <linux/dma-heap.h>
+
+#include <libcamera/base/log.h>
+
+/**
+ * \file dma_heaps.cpp
+ * \brief CMA dma-heap allocator
+ */
+
+/*
+ * /dev/dma_heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma
+ * to only have to worry about importing.
+ *
+ * Annoyingly, should the cma heap size be specified on the kernel command line
+ * instead of DT, the heap gets named "reserved" instead.
+ */
+static constexpr std::array<const char *, 2> heapNames = {
+	"/dev/dma_heap/linux,cma",
+	"/dev/dma_heap/reserved"
+};
+
+namespace libcamera {
+
+LOG_DEFINE_CATEGORY(DmaHeap)
+
+/**
+ * \class DmaHeap
+ * \brief Helper class for CMA dma-heap allocations
+ */
+
+/**
+ * \brief Construct a DmaHeap that owns a CMA dma-heap file descriptor
+ *
+ * Goes through the internal list of possible names of the CMA dma-heap devices
+ * until a CMA dma-heap device is successfully opened. If it fails to open any
+ * dma-heap device, an invalid DmaHeap object is constructed. A valid DmaHeap
+ * object owns a wrapped dma-heap file descriptor.
+ *
+ * Please check the new DmaHeap object with \ref DmaHeap::isValid before using it.
+ */
+DmaHeap::DmaHeap()
+{
+	for (const char *name : heapNames) {
+		int ret = ::open(name, O_RDWR | O_CLOEXEC, 0);
+		if (ret < 0) {
+			ret = errno;
+			LOG(DmaHeap, Debug)
+				<< "Failed to open " << name << ": "
+				<< strerror(ret);
+			continue;
+		}
+
+		dmaHeapHandle_ = UniqueFD(ret);
+		break;
+	}
+
+	if (!dmaHeapHandle_.isValid())
+		LOG(DmaHeap, Error) << "Could not open any dmaHeap device";
+}
+
+/**
+ * \brief Destroy the DmaHeap instance
+ *
+ * Destroying a DmaHeap instance which owns a wrapped dma-heap file descriptor
+ * closes the descriptor automatically.
+ */
+DmaHeap::~DmaHeap() = default;
+
+/**
+ * \fn DmaHeap::isValid()
+ * \brief Check if the DmaHeap instance is valid
+ * \return True if the DmaHeap is valid, false otherwise
+ */
+
+/**
+ * \brief Allocate a dma-buf from the DmaHeap
+ * \param [in] name The name to set for the allocated buffer
+ * \param [in] size The size of the buffer to allocate
+ * \return The \ref UniqueFD of the allocated buffer
+ *
+ * Allocates a dma-buf with read/write access.
+ * If the allocation fails returns invalid UniqueFD.
+ */
+UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
+{
+	int ret;
+
+	if (!name)
+		return {};
+
+	struct dma_heap_allocation_data alloc = {};
+
+	alloc.len = size;
+	alloc.fd_flags = O_CLOEXEC | O_RDWR;
+
+	ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);
+	if (ret < 0) {
+		LOG(DmaHeap, Error) << "dmaHeap allocation failure for " << name;
+		return {};
+	}
+
+	UniqueFD allocFd(alloc.fd);
+	ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);
+	if (ret < 0) {
+		LOG(DmaHeap, Error) << "dmaHeap naming failure for " << name;
+		return {};
+	}
+
+	return allocFd;
+}
+
+} /* namespace libcamera */
diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build
index 45f63e93..3c5e43df 100644
--- a/src/libcamera/meson.build
+++ b/src/libcamera/meson.build
@@ -17,6 +17,7 @@ libcamera_sources = files([
     'delayed_controls.cpp',
     'device_enumerator.cpp',
     'device_enumerator_sysfs.cpp',
+    'dma_heaps.cpp',
     'fence.cpp',
     'formats.cpp',
     'framebuffer.cpp',
diff --git a/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp b/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp
deleted file mode 100644
index 317b1fc1..00000000
--- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-/*
- * Copyright (C) 2020, Raspberry Pi Ltd
- *
- * dma_heaps.h - Helper class for dma-heap allocations.
- */
-
-#include "dma_heaps.h"
-
-#include <array>
-#include <fcntl.h>
-#include <linux/dma-buf.h>
-#include <linux/dma-heap.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
-
-#include <libcamera/base/log.h>
-
-/*
- * /dev/dma-heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma
- * to only have to worry about importing.
- *
- * Annoyingly, should the cma heap size be specified on the kernel command line
- * instead of DT, the heap gets named "reserved" instead.
- */
-static constexpr std::array<const char *, 2> heapNames = {
-	"/dev/dma_heap/linux,cma",
-	"/dev/dma_heap/reserved"
-};
-
-namespace libcamera {
-
-LOG_DECLARE_CATEGORY(RPI)
-
-namespace RPi {
-
-DmaHeap::DmaHeap()
-{
-	for (const char *name : heapNames) {
-		int ret = ::open(name, O_RDWR | O_CLOEXEC, 0);
-		if (ret < 0) {
-			ret = errno;
-			LOG(RPI, Debug) << "Failed to open " << name << ": "
-					<< strerror(ret);
-			continue;
-		}
-
-		dmaHeapHandle_ = UniqueFD(ret);
-		break;
-	}
-
-	if (!dmaHeapHandle_.isValid())
-		LOG(RPI, Error) << "Could not open any dmaHeap device";
-}
-
-DmaHeap::~DmaHeap() = default;
-
-UniqueFD DmaHeap::alloc(const char *name, std::size_t size)
-{
-	int ret;
-
-	if (!name)
-		return {};
-
-	struct dma_heap_allocation_data alloc = {};
-
-	alloc.len = size;
-	alloc.fd_flags = O_CLOEXEC | O_RDWR;
-
-	ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc);
-	if (ret < 0) {
-		LOG(RPI, Error) << "dmaHeap allocation failure for "
-				<< name;
-		return {};
-	}
-
-	UniqueFD allocFd(alloc.fd);
-	ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name);
-	if (ret < 0) {
-		LOG(RPI, Error) << "dmaHeap naming failure for "
-				<< name;
-		return {};
-	}
-
-	return allocFd;
-}
-
-} /* namespace RPi */
-
-} /* namespace libcamera */
diff --git a/src/libcamera/pipeline/rpi/vc4/meson.build b/src/libcamera/pipeline/rpi/vc4/meson.build
index cdb049c5..386e2296 100644
--- a/src/libcamera/pipeline/rpi/vc4/meson.build
+++ b/src/libcamera/pipeline/rpi/vc4/meson.build
@@ -1,7 +1,6 @@
 # SPDX-License-Identifier: CC0-1.0
 
 libcamera_sources += files([
-    'dma_heaps.cpp',
     'vc4.cpp',
 ])
 
diff --git a/src/libcamera/pipeline/rpi/vc4/vc4.cpp b/src/libcamera/pipeline/rpi/vc4/vc4.cpp
index 26102ea7..3a42e75e 100644
--- a/src/libcamera/pipeline/rpi/vc4/vc4.cpp
+++ b/src/libcamera/pipeline/rpi/vc4/vc4.cpp
@@ -12,12 +12,11 @@
 #include <libcamera/formats.h>
 
 #include "libcamera/internal/device_enumerator.h"
+#include "libcamera/internal/dma_heaps.h"
 
 #include "../common/pipeline_base.h"
 #include "../common/rpi_stream.h"
 
-#include "dma_heaps.h"
-
 using namespace std::chrono_literals;
 
 namespace libcamera {
@@ -87,7 +86,7 @@ public:
 	RPi::Device<Isp, 4> isp_;
 
 	/* DMAHEAP allocation helper. */
-	RPi::DmaHeap dmaHeap_;
+	DmaHeap dmaHeap_;
 	SharedFD lsTable_;
 
 	struct Config {
-- 
2.43.2