blob: 22eabddfb01c789db91c84eac69d5627c14e6080 [file] [log] [blame]
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2016 The Chromium OS Authors. All rights reserved.
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07003 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6#include <assert.h>
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -07007#include <errno.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07008#include <fcntl.h>
Gurchetan Singh1647fbe2016-08-03 17:14:55 -07009#include <pthread.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070010#include <stdint.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070014#include <sys/mman.h>
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +080015#include <sys/types.h>
16#include <unistd.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include <xf86drm.h>
18
Alistair Strachan0cfaaa52018-03-19 14:03:23 -070019#ifdef __ANDROID__
20#include <cutils/log.h>
21#include <libgen.h>
22#endif
23
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070024#include "drv_priv.h"
25#include "helpers.h"
26#include "util.h"
27
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053028#ifdef DRV_AMDGPU
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070029extern const struct backend backend_amdgpu;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053030#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070031#ifdef DRV_EXYNOS
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070032extern const struct backend backend_exynos;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070033#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070034#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070035extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070036#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070037#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070038extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070039#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053040#ifdef DRV_MSM
41extern const struct backend backend_msm;
42#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070043#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070044extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070045#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010046#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070047extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010048#endif
Anders Delliene5bef532020-06-10 10:30:44 +010049
50// Dumb / generic drivers
51extern const struct backend backend_evdi;
52extern const struct backend backend_marvell;
53extern const struct backend backend_meson;
54extern const struct backend backend_nouveau;
55extern const struct backend backend_komeda;
56extern const struct backend backend_radeon;
57extern const struct backend backend_synaptics;
Gurchetan Singh73c141e2021-01-21 14:51:19 -080058extern const struct backend backend_virtgpu;
Anders Delliene5bef532020-06-10 10:30:44 +010059extern const struct backend backend_udl;
François-Denis Gonthiercea0b842020-05-22 18:02:24 -040060extern const struct backend backend_vkms;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070061
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070062static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070063{
64 drmVersionPtr drm_version;
65 unsigned int i;
66
67 drm_version = drmGetVersion(fd);
68
69 if (!drm_version)
70 return NULL;
71
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070072 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053073#ifdef DRV_AMDGPU
74 &backend_amdgpu,
75#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070076#ifdef DRV_EXYNOS
77 &backend_exynos,
78#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070079#ifdef DRV_I915
80 &backend_i915,
81#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070082#ifdef DRV_MEDIATEK
83 &backend_mediatek,
84#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053085#ifdef DRV_MSM
86 &backend_msm,
87#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070088#ifdef DRV_ROCKCHIP
89 &backend_rockchip,
90#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010091#ifdef DRV_VC4
92 &backend_vc4,
93#endif
Gurchetan Singh73c141e2021-01-21 14:51:19 -080094 &backend_evdi, &backend_marvell, &backend_meson, &backend_nouveau,
95 &backend_komeda, &backend_radeon, &backend_synaptics, &backend_virtgpu,
96 &backend_udl, &backend_virtgpu, &backend_vkms
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070097 };
98
David Stevens26fe6822020-03-09 12:23:42 +000099 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
100 const struct backend *b = backend_list[i];
David Stevens26fe6822020-03-09 12:23:42 +0000101 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700102 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000103 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700104 }
David Stevens26fe6822020-03-09 12:23:42 +0000105 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700106
107 drmFreeVersion(drm_version);
108 return NULL;
109}
110
111struct driver *drv_create(int fd)
112{
113 struct driver *drv;
114 int ret;
115
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800116 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700117
118 if (!drv)
119 return NULL;
120
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500121 char *minigbm_debug;
122 minigbm_debug = getenv("MINIGBM_DEBUG");
123 drv->compression = (minigbm_debug == NULL) || (strcmp(minigbm_debug, "nocompression") != 0);
124
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700125 drv->fd = fd;
126 drv->backend = drv_get_backend(fd);
127
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700128 if (!drv->backend)
129 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700130
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800131 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700132 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700133
134 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700135 if (!drv->buffer_table)
136 goto free_lock;
137
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700138 drv->mappings = drv_array_init(sizeof(struct mapping));
139 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700140 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700141
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700142 drv->combos = drv_array_init(sizeof(struct combination));
143 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700144 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700145
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700146 if (drv->backend->init) {
147 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800148 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700149 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700150 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800151 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700152 }
153
154 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700155
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700156free_mappings:
157 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700158free_buffer_table:
159 drmHashDestroy(drv->buffer_table);
160free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800161 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700162free_driver:
163 free(drv);
164 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700165}
166
167void drv_destroy(struct driver *drv)
168{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800169 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700170
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700171 if (drv->backend->close)
172 drv->backend->close(drv);
173
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700174 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700175 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700176 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700177
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800178 pthread_mutex_unlock(&drv->driver_lock);
179 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700180
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700181 free(drv);
182}
183
184int drv_get_fd(struct driver *drv)
185{
186 return drv->fd;
187}
188
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800189const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700190{
191 return drv->backend->name;
192}
193
Gurchetan Singha1892b22017-09-28 16:40:52 -0700194struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700195{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800196 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700197
Gurchetan Singha1892b22017-09-28 16:40:52 -0700198 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700199 return 0;
200
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800201 best = NULL;
202 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700203 for (i = 0; i < drv_array_size(drv->combos); i++) {
204 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700205 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800206 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800207 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700208 }
209
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800210 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700211}
212
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700213struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000214 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700215{
216
217 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800218 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700219
220 if (!bo)
221 return NULL;
222
223 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700224 bo->meta.width = width;
225 bo->meta.height = height;
226 bo->meta.format = format;
227 bo->meta.use_flags = use_flags;
228 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000229 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700230
Gurchetan Singh298b7572019-09-19 09:55:18 -0700231 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700232 free(bo);
Yiwei Zhang01b69742021-09-16 04:47:54 +0000233 errno = EINVAL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700234 return NULL;
235 }
236
237 return bo;
238}
239
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000240static int drv_bo_mapping_destroy(struct bo *bo)
241{
242 struct driver *drv = bo->drv;
243 uint32_t idx = 0;
244
245 /*
246 * This function is called right before the buffer is destroyed. It will free any mappings
247 * associated with the buffer.
248 */
249 pthread_mutex_lock(&drv->driver_lock);
250 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
251 while (idx < drv_array_size(drv->mappings)) {
252 struct mapping *mapping =
253 (struct mapping *)drv_array_at_idx(drv->mappings, idx);
254 if (mapping->vma->handle != bo->handles[plane].u32) {
255 idx++;
256 continue;
257 }
258
259 if (!--mapping->vma->refcount) {
260 int ret = drv->backend->bo_unmap(bo, mapping->vma);
261 if (ret) {
262 drv_log("munmap failed\n");
263 return ret;
264 }
265
266 free(mapping->vma);
267 }
268
269 /* This shrinks and shifts the array, so don't increment idx. */
270 drv_array_remove(drv->mappings, idx);
271 }
272 }
273 pthread_mutex_unlock(&drv->driver_lock);
274
275 return 0;
276}
277
278/*
279 * Acquire a reference on plane buffers of the bo.
280 */
281static void drv_bo_acquire(struct bo *bo)
282{
283 struct driver *drv = bo->drv;
284
285 pthread_mutex_lock(&drv->driver_lock);
286 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
287 uintptr_t num = 0;
288
289 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, (void **)&num))
290 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
291
292 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
293 }
294 pthread_mutex_unlock(&drv->driver_lock);
295}
296
297/*
298 * Release a reference on plane buffers of the bo. Return true when the bo has lost all its
299 * references. Otherwise, return false.
300 */
301static bool drv_bo_release(struct bo *bo)
302{
303 struct driver *drv = bo->drv;
304 bool unreferenced = true;
305
306 pthread_mutex_lock(&drv->driver_lock);
307 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
308 uintptr_t num = 0;
309
310 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, (void **)&num)) {
311 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
312 }
313
314 if (num > 1) {
315 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num - 1));
316 unreferenced = false;
317 }
318 }
319 pthread_mutex_unlock(&drv->driver_lock);
320
321 return unreferenced;
322}
323
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800324struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700325 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700326{
327 int ret;
328 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000329 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700330
David Stevens26fe6822020-03-09 12:23:42 +0000331 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
332 use_flags &= ~BO_USE_TEST_ALLOC;
333
334 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700335
336 if (!bo)
337 return NULL;
338
David Stevens26fe6822020-03-09 12:23:42 +0000339 ret = -EINVAL;
340 if (drv->backend->bo_compute_metadata) {
341 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
342 0);
343 if (!is_test_alloc && ret == 0)
344 ret = drv->backend->bo_create_from_metadata(bo);
345 } else if (!is_test_alloc) {
346 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
347 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700348
349 if (ret) {
Yiwei Zhang01b69742021-09-16 04:47:54 +0000350 errno = -ret;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700351 free(bo);
352 return NULL;
353 }
354
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000355 drv_bo_acquire(bo);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700356
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700357 return bo;
358}
359
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800360struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
361 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700362{
363 int ret;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700364 struct bo *bo;
365
David Stevens26fe6822020-03-09 12:23:42 +0000366 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700367 errno = ENOENT;
368 return NULL;
369 }
370
David Stevens26fe6822020-03-09 12:23:42 +0000371 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700372
373 if (!bo)
374 return NULL;
375
David Stevens26fe6822020-03-09 12:23:42 +0000376 ret = -EINVAL;
377 if (drv->backend->bo_compute_metadata) {
378 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
379 modifiers, count);
380 if (ret == 0)
381 ret = drv->backend->bo_create_from_metadata(bo);
382 } else {
383 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
384 count);
385 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700386
387 if (ret) {
388 free(bo);
389 return NULL;
390 }
391
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000392 drv_bo_acquire(bo);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700393
394 return bo;
395}
396
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700397void drv_bo_destroy(struct bo *bo)
398{
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000399 if (!bo->is_test_buffer && drv_bo_release(bo)) {
400 int ret = drv_bo_mapping_destroy(bo);
401 assert(ret == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700402
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000403 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900404 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700405
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700406 free(bo);
407}
408
409struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
410{
411 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700412 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700413 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700414 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700415
David Stevens26fe6822020-03-09 12:23:42 +0000416 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700417
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700418 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700419 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700420
Gurchetan Singh71611d62017-01-03 16:49:56 -0800421 ret = drv->backend->bo_import(bo, data);
422 if (ret) {
423 free(bo);
424 return NULL;
425 }
426
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000427 drv_bo_acquire(bo);
Satyajit Sahua047d412018-07-12 12:29:39 +0530428
Gurchetan Singh52155b42021-01-27 17:55:17 -0800429 bo->meta.format_modifier = data->format_modifier;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700430 for (plane = 0; plane < bo->meta.num_planes; plane++) {
431 bo->meta.strides[plane] = data->strides[plane];
432 bo->meta.offsets[plane] = data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700433
434 seek_end = lseek(data->fds[plane], 0, SEEK_END);
435 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700436 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700437 goto destroy_bo;
438 }
439
440 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700441 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
442 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700443 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700444 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700445
Gurchetan Singh298b7572019-09-19 09:55:18 -0700446 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700447 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700448 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800449 }
450
Gurchetan Singh298b7572019-09-19 09:55:18 -0700451 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700452 }
453
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700454 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700455
456destroy_bo:
457 drv_bo_destroy(bo);
458 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700459}
460
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800461void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
462 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700463{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700464 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700465 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700466 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700467
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800468 assert(rect->width >= 0);
469 assert(rect->height >= 0);
470 assert(rect->x + rect->width <= drv_bo_get_width(bo));
471 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700472 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900473 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700474 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700475
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800476 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000477 return MAP_FAILED;
David Stevens26fe6822020-03-09 12:23:42 +0000478
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800479 mapping.rect = *rect;
480 mapping.refcount = 1;
481
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800482 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700483
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700484 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
485 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
486 if (prior->vma->handle != bo->handles[plane].u32 ||
487 prior->vma->map_flags != map_flags)
488 continue;
489
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800490 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
491 rect->width != prior->rect.width || rect->height != prior->rect.height)
492 continue;
493
494 prior->refcount++;
495 *map_data = prior;
496 goto exact_match;
497 }
498
499 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
500 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
501 if (prior->vma->handle != bo->handles[plane].u32 ||
502 prior->vma->map_flags != map_flags)
503 continue;
504
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700505 prior->vma->refcount++;
506 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700507 goto success;
508 }
509
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700510 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700511 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800512 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700513 if (addr == MAP_FAILED) {
514 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700515 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800516 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700517 return MAP_FAILED;
518 }
519
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700520 mapping.vma->refcount = 1;
521 mapping.vma->addr = addr;
522 mapping.vma->handle = bo->handles[plane].u32;
523 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700524
525success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700526 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800527exact_match:
528 drv_bo_invalidate(bo, *map_data);
529 addr = (uint8_t *)((*map_data)->vma->addr);
530 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800531 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800532 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700533}
534
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700535int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700536{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700537 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700538 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700539
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800540 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700541
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800542 if (--mapping->refcount)
543 goto out;
544
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700545 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800546 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700547 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700548 }
549
550 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
551 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
552 drv_array_remove(bo->drv->mappings, i);
553 break;
554 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700555 }
556
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800557out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800558 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700559 return ret;
560}
561
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700562int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700563{
564 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700565
566 assert(mapping);
567 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800568 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700569 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700570
571 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700572 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700573
574 return ret;
575}
576
Jason Macnak1de7f662020-01-24 15:05:57 -0800577int drv_bo_flush(struct bo *bo, struct mapping *mapping)
578{
579 int ret = 0;
580
581 assert(mapping);
582 assert(mapping->vma);
583 assert(mapping->refcount > 0);
584 assert(mapping->vma->refcount > 0);
585
586 if (bo->drv->backend->bo_flush)
587 ret = bo->drv->backend->bo_flush(bo, mapping);
588
589 return ret;
590}
591
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700592int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700593{
594 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700595
596 assert(mapping);
597 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800598 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700599 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700600 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700601
602 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700603 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700604 else
605 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700606
607 return ret;
608}
609
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700610uint32_t drv_bo_get_width(struct bo *bo)
611{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700612 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700613}
614
615uint32_t drv_bo_get_height(struct bo *bo)
616{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700617 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700618}
619
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700620size_t drv_bo_get_num_planes(struct bo *bo)
621{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700622 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700623}
624
625union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
626{
627 return bo->handles[plane];
628}
629
630#ifndef DRM_RDWR
631#define DRM_RDWR O_RDWR
632#endif
633
634int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
635{
636
637 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700638 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700639
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800640 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000641 return -EINVAL;
David Stevens26fe6822020-03-09 12:23:42 +0000642
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800643 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700644
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700645 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
646 if (ret)
647 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
648
Jason Macnak166fe142021-01-29 07:50:34 -0800649 if (ret)
650 drv_log("Failed to get plane fd: %s\n", strerror(errno));
651
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700652 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700653}
654
655uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
656{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700657 assert(plane < bo->meta.num_planes);
658 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700659}
660
661uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
662{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700663 assert(plane < bo->meta.num_planes);
664 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700665}
666
667uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
668{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700669 assert(plane < bo->meta.num_planes);
670 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700671}
672
Gurchetan Singh52155b42021-01-27 17:55:17 -0800673uint64_t drv_bo_get_format_modifier(struct bo *bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700674{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800675 return bo->meta.format_modifier;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700676}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700677
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800678uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700679{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700680 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700681}
682
Yiwei Zhang1f9b9002021-09-15 21:28:51 +0000683uint32_t drv_bo_get_tiling(struct bo *bo)
684{
685 return bo->meta.tiling;
686}
687
688uint64_t drv_bo_get_use_flags(struct bo *bo)
689{
690 return bo->meta.use_flags;
691}
692
Jason Macnak1de7f662020-01-24 15:05:57 -0800693size_t drv_bo_get_total_size(struct bo *bo)
694{
695 return bo->meta.total_size;
696}
697
Gurchetan Singha1892b22017-09-28 16:40:52 -0700698uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700699{
700 if (drv->backend->resolve_format)
Yiwei Zhang9f390d92021-09-21 20:42:29 +0000701 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700702
703 return format;
704}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700705
Yiwei Zhangc1413ea2021-09-17 08:20:21 +0000706uint64_t drv_resolve_use_flags(struct driver *drv, uint32_t format, uint64_t use_flags)
707{
708 if (drv->backend->resolve_use_flags)
Yiwei Zhang9420ffe2021-09-24 06:24:30 +0000709 return drv->backend->resolve_use_flags(drv, format, use_flags);
Yiwei Zhangc1413ea2021-09-17 08:20:21 +0000710
711 return use_flags;
712}
713
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700714uint32_t drv_num_buffers_per_bo(struct bo *bo)
715{
716 uint32_t count = 0;
717 size_t plane, p;
718
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800719 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000720 return 0;
David Stevens26fe6822020-03-09 12:23:42 +0000721
Gurchetan Singh298b7572019-09-19 09:55:18 -0700722 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700723 for (p = 0; p < plane; p++)
724 if (bo->handles[p].u32 == bo->handles[plane].u32)
725 break;
726 if (p == plane)
727 count++;
728 }
729
730 return count;
731}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700732
733void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
734{
735 char buf[50];
736 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
737
738 va_list args;
739 va_start(args, format);
740#ifdef __ANDROID__
741 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
742#else
743 fprintf(stderr, "%s ", buf);
744 vfprintf(stderr, format, args);
745#endif
746 va_end(args);
747}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700748
749int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000750 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700751{
752 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
753 strides[plane] = bo->meta.strides[plane];
754 offsets[plane] = bo->meta.offsets[plane];
755 }
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000756 *format_modifier = bo->meta.format_modifier;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700757
758 if (bo->drv->backend->resource_info)
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000759 return bo->drv->backend->resource_info(bo, strides, offsets, format_modifier);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700760
761 return 0;
762}