blob: acaec035822c97fee6fe70c393dbfb26ea16665e [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
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800240struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700241 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700242{
243 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700244 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700245 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000246 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700247
David Stevens26fe6822020-03-09 12:23:42 +0000248 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
249 use_flags &= ~BO_USE_TEST_ALLOC;
250
251 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700252
253 if (!bo)
254 return NULL;
255
David Stevens26fe6822020-03-09 12:23:42 +0000256 ret = -EINVAL;
257 if (drv->backend->bo_compute_metadata) {
258 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
259 0);
260 if (!is_test_alloc && ret == 0)
261 ret = drv->backend->bo_create_from_metadata(bo);
262 } else if (!is_test_alloc) {
263 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
264 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700265
266 if (ret) {
Yiwei Zhang01b69742021-09-16 04:47:54 +0000267 errno = -ret;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700268 free(bo);
269 return NULL;
270 }
271
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800272 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700273
Gurchetan Singh298b7572019-09-19 09:55:18 -0700274 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700275 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700276 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700277
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700278 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700279 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700280
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800281 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700282
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700283 return bo;
284}
285
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800286struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
287 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700288{
289 int ret;
290 size_t plane;
291 struct bo *bo;
292
David Stevens26fe6822020-03-09 12:23:42 +0000293 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700294 errno = ENOENT;
295 return NULL;
296 }
297
David Stevens26fe6822020-03-09 12:23:42 +0000298 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700299
300 if (!bo)
301 return NULL;
302
David Stevens26fe6822020-03-09 12:23:42 +0000303 ret = -EINVAL;
304 if (drv->backend->bo_compute_metadata) {
305 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
306 modifiers, count);
307 if (ret == 0)
308 ret = drv->backend->bo_create_from_metadata(bo);
309 } else {
310 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
311 count);
312 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700313
314 if (ret) {
315 free(bo);
316 return NULL;
317 }
318
319 pthread_mutex_lock(&drv->driver_lock);
320
Gurchetan Singh298b7572019-09-19 09:55:18 -0700321 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700322 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700323 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700324
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700325 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700326 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700327
328 pthread_mutex_unlock(&drv->driver_lock);
329
330 return bo;
331}
332
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700333void drv_bo_destroy(struct bo *bo)
334{
Jason Macnak0907d822019-11-12 10:53:05 -0800335 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700336 size_t plane;
337 uintptr_t total = 0;
338 struct driver *drv = bo->drv;
339
David Stevens26fe6822020-03-09 12:23:42 +0000340 if (!bo->is_test_buffer) {
341 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700342
David Stevens26fe6822020-03-09 12:23:42 +0000343 for (plane = 0; plane < bo->meta.num_planes; plane++)
344 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700345
David Stevens26fe6822020-03-09 12:23:42 +0000346 for (plane = 0; plane < bo->meta.num_planes; plane++)
347 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700348
David Stevens26fe6822020-03-09 12:23:42 +0000349 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700350
David Stevens26fe6822020-03-09 12:23:42 +0000351 if (total == 0) {
352 ret = drv_mapping_destroy(bo);
353 assert(ret == 0);
354 bo->drv->backend->bo_destroy(bo);
355 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900356 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700357
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700358 free(bo);
359}
360
361struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
362{
363 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700364 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700365 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700366 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700367
David Stevens26fe6822020-03-09 12:23:42 +0000368 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700369
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700370 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700371 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700372
Gurchetan Singh71611d62017-01-03 16:49:56 -0800373 ret = drv->backend->bo_import(bo, data);
374 if (ret) {
375 free(bo);
376 return NULL;
377 }
378
Gurchetan Singh298b7572019-09-19 09:55:18 -0700379 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530380 pthread_mutex_lock(&bo->drv->driver_lock);
381 drv_increment_reference_count(bo->drv, bo, plane);
382 pthread_mutex_unlock(&bo->drv->driver_lock);
383 }
384
Gurchetan Singh52155b42021-01-27 17:55:17 -0800385 bo->meta.format_modifier = data->format_modifier;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700386 for (plane = 0; plane < bo->meta.num_planes; plane++) {
387 bo->meta.strides[plane] = data->strides[plane];
388 bo->meta.offsets[plane] = data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700389
390 seek_end = lseek(data->fds[plane], 0, SEEK_END);
391 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700392 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700393 goto destroy_bo;
394 }
395
396 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700397 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
398 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700399 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700400 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700401
Gurchetan Singh298b7572019-09-19 09:55:18 -0700402 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700403 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700404 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800405 }
406
Gurchetan Singh298b7572019-09-19 09:55:18 -0700407 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700408 }
409
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700410 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700411
412destroy_bo:
413 drv_bo_destroy(bo);
414 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700415}
416
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800417void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
418 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700419{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700420 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700421 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700422 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700423
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800424 assert(rect->width >= 0);
425 assert(rect->height >= 0);
426 assert(rect->x + rect->width <= drv_bo_get_width(bo));
427 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700428 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900429 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700430 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700431
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800432 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000433 return MAP_FAILED;
David Stevens26fe6822020-03-09 12:23:42 +0000434
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800435 mapping.rect = *rect;
436 mapping.refcount = 1;
437
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800438 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700439
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700440 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
441 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
442 if (prior->vma->handle != bo->handles[plane].u32 ||
443 prior->vma->map_flags != map_flags)
444 continue;
445
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800446 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
447 rect->width != prior->rect.width || rect->height != prior->rect.height)
448 continue;
449
450 prior->refcount++;
451 *map_data = prior;
452 goto exact_match;
453 }
454
455 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
456 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
457 if (prior->vma->handle != bo->handles[plane].u32 ||
458 prior->vma->map_flags != map_flags)
459 continue;
460
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700461 prior->vma->refcount++;
462 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700463 goto success;
464 }
465
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700466 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700467 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800468 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700469 if (addr == MAP_FAILED) {
470 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700471 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800472 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700473 return MAP_FAILED;
474 }
475
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700476 mapping.vma->refcount = 1;
477 mapping.vma->addr = addr;
478 mapping.vma->handle = bo->handles[plane].u32;
479 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700480
481success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700482 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800483exact_match:
484 drv_bo_invalidate(bo, *map_data);
485 addr = (uint8_t *)((*map_data)->vma->addr);
486 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800487 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800488 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700489}
490
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700491int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700492{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700493 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700494 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700495
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800496 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700497
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800498 if (--mapping->refcount)
499 goto out;
500
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700501 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800502 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700503 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700504 }
505
506 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
507 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
508 drv_array_remove(bo->drv->mappings, i);
509 break;
510 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700511 }
512
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800513out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800514 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700515 return ret;
516}
517
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700518int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700519{
520 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700521
522 assert(mapping);
523 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800524 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700525 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700526
527 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700528 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700529
530 return ret;
531}
532
Jason Macnak1de7f662020-01-24 15:05:57 -0800533int drv_bo_flush(struct bo *bo, struct mapping *mapping)
534{
535 int ret = 0;
536
537 assert(mapping);
538 assert(mapping->vma);
539 assert(mapping->refcount > 0);
540 assert(mapping->vma->refcount > 0);
541
542 if (bo->drv->backend->bo_flush)
543 ret = bo->drv->backend->bo_flush(bo, mapping);
544
545 return ret;
546}
547
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700548int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700549{
550 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700551
552 assert(mapping);
553 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800554 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700555 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700556 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700557
558 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700559 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700560 else
561 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700562
563 return ret;
564}
565
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700566uint32_t drv_bo_get_width(struct bo *bo)
567{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700568 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700569}
570
571uint32_t drv_bo_get_height(struct bo *bo)
572{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700573 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700574}
575
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700576size_t drv_bo_get_num_planes(struct bo *bo)
577{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700578 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700579}
580
581union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
582{
583 return bo->handles[plane];
584}
585
586#ifndef DRM_RDWR
587#define DRM_RDWR O_RDWR
588#endif
589
590int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
591{
592
593 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700594 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700595
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800596 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000597 return -EINVAL;
David Stevens26fe6822020-03-09 12:23:42 +0000598
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800599 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700600
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700601 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
602 if (ret)
603 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
604
Jason Macnak166fe142021-01-29 07:50:34 -0800605 if (ret)
606 drv_log("Failed to get plane fd: %s\n", strerror(errno));
607
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700608 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700609}
610
611uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
612{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700613 assert(plane < bo->meta.num_planes);
614 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700615}
616
617uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
618{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700619 assert(plane < bo->meta.num_planes);
620 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700621}
622
623uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
624{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700625 assert(plane < bo->meta.num_planes);
626 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700627}
628
Gurchetan Singh52155b42021-01-27 17:55:17 -0800629uint64_t drv_bo_get_format_modifier(struct bo *bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700630{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800631 return bo->meta.format_modifier;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700632}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700633
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800634uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700635{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700636 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700637}
638
Yiwei Zhang1f9b9002021-09-15 21:28:51 +0000639uint32_t drv_bo_get_tiling(struct bo *bo)
640{
641 return bo->meta.tiling;
642}
643
644uint64_t drv_bo_get_use_flags(struct bo *bo)
645{
646 return bo->meta.use_flags;
647}
648
Jason Macnak1de7f662020-01-24 15:05:57 -0800649size_t drv_bo_get_total_size(struct bo *bo)
650{
651 return bo->meta.total_size;
652}
653
Gurchetan Singha1892b22017-09-28 16:40:52 -0700654uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700655{
656 if (drv->backend->resolve_format)
Yiwei Zhang9f390d92021-09-21 20:42:29 +0000657 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700658
659 return format;
660}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700661
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700662uint32_t drv_num_buffers_per_bo(struct bo *bo)
663{
664 uint32_t count = 0;
665 size_t plane, p;
666
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800667 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000668 return 0;
David Stevens26fe6822020-03-09 12:23:42 +0000669
Gurchetan Singh298b7572019-09-19 09:55:18 -0700670 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700671 for (p = 0; p < plane; p++)
672 if (bo->handles[p].u32 == bo->handles[plane].u32)
673 break;
674 if (p == plane)
675 count++;
676 }
677
678 return count;
679}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700680
681void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
682{
683 char buf[50];
684 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
685
686 va_list args;
687 va_start(args, format);
688#ifdef __ANDROID__
689 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
690#else
691 fprintf(stderr, "%s ", buf);
692 vfprintf(stderr, format, args);
693#endif
694 va_end(args);
695}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700696
697int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000698 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700699{
700 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
701 strides[plane] = bo->meta.strides[plane];
702 offsets[plane] = bo->meta.offsets[plane];
703 }
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000704 *format_modifier = bo->meta.format_modifier;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700705
706 if (bo->drv->backend->resource_info)
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000707 return bo->drv->backend->resource_info(bo, strides, offsets, format_modifier);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700708
709 return 0;
710}