blob: b1a231ea3dd772237bd21e6257677ae279b21584 [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
46#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070047extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070048#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010049#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010051#endif
Anders Delliene5bef532020-06-10 10:30:44 +010052
53// Dumb / generic drivers
54extern const struct backend backend_evdi;
55extern const struct backend backend_marvell;
56extern const struct backend backend_meson;
57extern const struct backend backend_nouveau;
58extern const struct backend backend_komeda;
59extern const struct backend backend_radeon;
60extern const struct backend backend_synaptics;
Gurchetan Singh73c141e2021-01-21 14:51:19 -080061extern const struct backend backend_virtgpu;
Anders Delliene5bef532020-06-10 10:30:44 +010062extern const struct backend backend_udl;
François-Denis Gonthiercea0b842020-05-22 18:02:24 -040063extern const struct backend backend_vkms;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070064
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070065static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070066{
67 drmVersionPtr drm_version;
68 unsigned int i;
69
70 drm_version = drmGetVersion(fd);
71
72 if (!drm_version)
73 return NULL;
74
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070075 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053076#ifdef DRV_AMDGPU
77 &backend_amdgpu,
78#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070079#ifdef DRV_EXYNOS
80 &backend_exynos,
81#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070082#ifdef DRV_I915
83 &backend_i915,
84#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070085#ifdef DRV_MEDIATEK
86 &backend_mediatek,
87#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053088#ifdef DRV_MSM
89 &backend_msm,
90#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070091#ifdef DRV_ROCKCHIP
92 &backend_rockchip,
93#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010094#ifdef DRV_VC4
95 &backend_vc4,
96#endif
Gurchetan Singh73c141e2021-01-21 14:51:19 -080097 &backend_evdi, &backend_marvell, &backend_meson, &backend_nouveau,
98 &backend_komeda, &backend_radeon, &backend_synaptics, &backend_virtgpu,
99 &backend_udl, &backend_virtgpu, &backend_vkms
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700100 };
101
David Stevens26fe6822020-03-09 12:23:42 +0000102 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
103 const struct backend *b = backend_list[i];
David Stevens26fe6822020-03-09 12:23:42 +0000104 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700105 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000106 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700107 }
David Stevens26fe6822020-03-09 12:23:42 +0000108 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700109
110 drmFreeVersion(drm_version);
111 return NULL;
112}
113
114struct driver *drv_create(int fd)
115{
116 struct driver *drv;
117 int ret;
118
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800119 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700120
121 if (!drv)
122 return NULL;
123
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500124 char *minigbm_debug;
125 minigbm_debug = getenv("MINIGBM_DEBUG");
126 drv->compression = (minigbm_debug == NULL) || (strcmp(minigbm_debug, "nocompression") != 0);
127
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700128 drv->fd = fd;
129 drv->backend = drv_get_backend(fd);
130
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700131 if (!drv->backend)
132 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700133
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800134 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700135 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700136
137 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700138 if (!drv->buffer_table)
139 goto free_lock;
140
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700141 drv->mappings = drv_array_init(sizeof(struct mapping));
142 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700143 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700144
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700145 drv->combos = drv_array_init(sizeof(struct combination));
146 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700147 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700148
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700149 if (drv->backend->init) {
150 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800151 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700152 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700153 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800154 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700155 }
156
157 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700158
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700159free_mappings:
160 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700161free_buffer_table:
162 drmHashDestroy(drv->buffer_table);
163free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800164 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700165free_driver:
166 free(drv);
167 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700168}
169
170void drv_destroy(struct driver *drv)
171{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800172 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700173
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700174 if (drv->backend->close)
175 drv->backend->close(drv);
176
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700177 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700178 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700179 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700180
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800181 pthread_mutex_unlock(&drv->driver_lock);
182 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700183
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700184 free(drv);
185}
186
187int drv_get_fd(struct driver *drv)
188{
189 return drv->fd;
190}
191
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800192const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700193{
194 return drv->backend->name;
195}
196
Gurchetan Singha1892b22017-09-28 16:40:52 -0700197struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700198{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800199 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700200
Gurchetan Singha1892b22017-09-28 16:40:52 -0700201 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700202 return 0;
203
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800204 best = NULL;
205 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700206 for (i = 0; i < drv_array_size(drv->combos); i++) {
207 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700208 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800209 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800210 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700211 }
212
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800213 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700214}
215
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700216struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000217 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700218{
219
220 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800221 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700222
223 if (!bo)
224 return NULL;
225
226 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700227 bo->meta.width = width;
228 bo->meta.height = height;
229 bo->meta.format = format;
230 bo->meta.use_flags = use_flags;
231 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000232 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700233
Gurchetan Singh298b7572019-09-19 09:55:18 -0700234 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700235 free(bo);
236 return NULL;
237 }
238
239 return bo;
240}
241
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800242struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700243 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700244{
245 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700246 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700247 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000248 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700249
David Stevens26fe6822020-03-09 12:23:42 +0000250 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
251 use_flags &= ~BO_USE_TEST_ALLOC;
252
253 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700254
255 if (!bo)
256 return NULL;
257
David Stevens26fe6822020-03-09 12:23:42 +0000258 ret = -EINVAL;
259 if (drv->backend->bo_compute_metadata) {
260 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
261 0);
262 if (!is_test_alloc && ret == 0)
263 ret = drv->backend->bo_create_from_metadata(bo);
264 } else if (!is_test_alloc) {
265 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
266 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700267
268 if (ret) {
269 free(bo);
270 return NULL;
271 }
272
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800273 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700274
Gurchetan Singh298b7572019-09-19 09:55:18 -0700275 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700276 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700277 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700278
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700279 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700280 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700281
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800282 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700283
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700284 return bo;
285}
286
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800287struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
288 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700289{
290 int ret;
291 size_t plane;
292 struct bo *bo;
293
David Stevens26fe6822020-03-09 12:23:42 +0000294 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700295 errno = ENOENT;
296 return NULL;
297 }
298
David Stevens26fe6822020-03-09 12:23:42 +0000299 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700300
301 if (!bo)
302 return NULL;
303
David Stevens26fe6822020-03-09 12:23:42 +0000304 ret = -EINVAL;
305 if (drv->backend->bo_compute_metadata) {
306 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
307 modifiers, count);
308 if (ret == 0)
309 ret = drv->backend->bo_create_from_metadata(bo);
310 } else {
311 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
312 count);
313 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700314
315 if (ret) {
316 free(bo);
317 return NULL;
318 }
319
320 pthread_mutex_lock(&drv->driver_lock);
321
Gurchetan Singh298b7572019-09-19 09:55:18 -0700322 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700323 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700324 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700325
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700326 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700327 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700328
329 pthread_mutex_unlock(&drv->driver_lock);
330
331 return bo;
332}
333
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700334void drv_bo_destroy(struct bo *bo)
335{
Jason Macnak0907d822019-11-12 10:53:05 -0800336 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700337 size_t plane;
338 uintptr_t total = 0;
339 struct driver *drv = bo->drv;
340
David Stevens26fe6822020-03-09 12:23:42 +0000341 if (!bo->is_test_buffer) {
342 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700343
David Stevens26fe6822020-03-09 12:23:42 +0000344 for (plane = 0; plane < bo->meta.num_planes; plane++)
345 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700346
David Stevens26fe6822020-03-09 12:23:42 +0000347 for (plane = 0; plane < bo->meta.num_planes; plane++)
348 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700349
David Stevens26fe6822020-03-09 12:23:42 +0000350 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700351
David Stevens26fe6822020-03-09 12:23:42 +0000352 if (total == 0) {
353 ret = drv_mapping_destroy(bo);
354 assert(ret == 0);
355 bo->drv->backend->bo_destroy(bo);
356 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900357 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700358
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700359 free(bo);
360}
361
362struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
363{
364 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700365 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700366 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700367 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700368
David Stevens26fe6822020-03-09 12:23:42 +0000369 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700370
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700371 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700372 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700373
Gurchetan Singh71611d62017-01-03 16:49:56 -0800374 ret = drv->backend->bo_import(bo, data);
375 if (ret) {
376 free(bo);
377 return NULL;
378 }
379
Gurchetan Singh298b7572019-09-19 09:55:18 -0700380 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530381 pthread_mutex_lock(&bo->drv->driver_lock);
382 drv_increment_reference_count(bo->drv, bo, plane);
383 pthread_mutex_unlock(&bo->drv->driver_lock);
384 }
385
Gurchetan Singh52155b42021-01-27 17:55:17 -0800386 bo->meta.format_modifier = data->format_modifier;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700387 for (plane = 0; plane < bo->meta.num_planes; plane++) {
388 bo->meta.strides[plane] = data->strides[plane];
389 bo->meta.offsets[plane] = data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700390
391 seek_end = lseek(data->fds[plane], 0, SEEK_END);
392 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700393 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700394 goto destroy_bo;
395 }
396
397 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700398 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
399 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700400 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700401 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700402
Gurchetan Singh298b7572019-09-19 09:55:18 -0700403 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700404 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700405 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800406 }
407
Gurchetan Singh298b7572019-09-19 09:55:18 -0700408 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700409 }
410
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700411 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700412
413destroy_bo:
414 drv_bo_destroy(bo);
415 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700416}
417
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800418void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
419 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700420{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700421 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700422 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700423 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700424
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800425 assert(rect->width >= 0);
426 assert(rect->height >= 0);
427 assert(rect->x + rect->width <= drv_bo_get_width(bo));
428 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700429 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900430 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700431 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700432
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800433 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000434 return MAP_FAILED;
David Stevens26fe6822020-03-09 12:23:42 +0000435
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800436 mapping.rect = *rect;
437 mapping.refcount = 1;
438
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800439 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700440
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700441 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
442 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
443 if (prior->vma->handle != bo->handles[plane].u32 ||
444 prior->vma->map_flags != map_flags)
445 continue;
446
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800447 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
448 rect->width != prior->rect.width || rect->height != prior->rect.height)
449 continue;
450
451 prior->refcount++;
452 *map_data = prior;
453 goto exact_match;
454 }
455
456 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
457 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
458 if (prior->vma->handle != bo->handles[plane].u32 ||
459 prior->vma->map_flags != map_flags)
460 continue;
461
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700462 prior->vma->refcount++;
463 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700464 goto success;
465 }
466
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700467 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700468 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800469 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700470 if (addr == MAP_FAILED) {
471 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700472 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800473 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700474 return MAP_FAILED;
475 }
476
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700477 mapping.vma->refcount = 1;
478 mapping.vma->addr = addr;
479 mapping.vma->handle = bo->handles[plane].u32;
480 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700481
482success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700483 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800484exact_match:
485 drv_bo_invalidate(bo, *map_data);
486 addr = (uint8_t *)((*map_data)->vma->addr);
487 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800488 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800489 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700490}
491
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700492int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700493{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700494 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700495 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700496
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800497 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700498
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800499 if (--mapping->refcount)
500 goto out;
501
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700502 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800503 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700504 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700505 }
506
507 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
508 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
509 drv_array_remove(bo->drv->mappings, i);
510 break;
511 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700512 }
513
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800514out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800515 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700516 return ret;
517}
518
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700519int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700520{
521 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700522
523 assert(mapping);
524 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800525 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700526 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700527
528 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700529 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700530
531 return ret;
532}
533
Jason Macnak1de7f662020-01-24 15:05:57 -0800534int drv_bo_flush(struct bo *bo, struct mapping *mapping)
535{
536 int ret = 0;
537
538 assert(mapping);
539 assert(mapping->vma);
540 assert(mapping->refcount > 0);
541 assert(mapping->vma->refcount > 0);
542
543 if (bo->drv->backend->bo_flush)
544 ret = bo->drv->backend->bo_flush(bo, mapping);
545
546 return ret;
547}
548
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700549int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700550{
551 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700552
553 assert(mapping);
554 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800555 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700556 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700557 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700558
559 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700560 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700561 else
562 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700563
564 return ret;
565}
566
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700567uint32_t drv_bo_get_width(struct bo *bo)
568{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700569 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700570}
571
572uint32_t drv_bo_get_height(struct bo *bo)
573{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700574 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700575}
576
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700577size_t drv_bo_get_num_planes(struct bo *bo)
578{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700579 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700580}
581
582union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
583{
584 return bo->handles[plane];
585}
586
587#ifndef DRM_RDWR
588#define DRM_RDWR O_RDWR
589#endif
590
591int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
592{
593
594 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700595 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700596
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800597 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000598 return -EINVAL;
David Stevens26fe6822020-03-09 12:23:42 +0000599
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800600 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700601
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700602 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
603 if (ret)
604 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
605
Jason Macnak166fe142021-01-29 07:50:34 -0800606 if (ret)
607 drv_log("Failed to get plane fd: %s\n", strerror(errno));
608
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700609 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700610}
611
612uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
613{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700614 assert(plane < bo->meta.num_planes);
615 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700616}
617
618uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
619{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700620 assert(plane < bo->meta.num_planes);
621 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700622}
623
624uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
625{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700626 assert(plane < bo->meta.num_planes);
627 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700628}
629
Gurchetan Singh52155b42021-01-27 17:55:17 -0800630uint64_t drv_bo_get_format_modifier(struct bo *bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700631{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800632 return bo->meta.format_modifier;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700633}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700634
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800635uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700636{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700637 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700638}
639
Jason Macnak1de7f662020-01-24 15:05:57 -0800640size_t drv_bo_get_total_size(struct bo *bo)
641{
642 return bo->meta.total_size;
643}
644
Gurchetan Singha1892b22017-09-28 16:40:52 -0700645uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700646{
647 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700648 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700649
650 return format;
651}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700652
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700653uint32_t drv_num_buffers_per_bo(struct bo *bo)
654{
655 uint32_t count = 0;
656 size_t plane, p;
657
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800658 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000659 return 0;
David Stevens26fe6822020-03-09 12:23:42 +0000660
Gurchetan Singh298b7572019-09-19 09:55:18 -0700661 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700662 for (p = 0; p < plane; p++)
663 if (bo->handles[p].u32 == bo->handles[plane].u32)
664 break;
665 if (p == plane)
666 count++;
667 }
668
669 return count;
670}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700671
672void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
673{
674 char buf[50];
675 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
676
677 va_list args;
678 va_start(args, format);
679#ifdef __ANDROID__
680 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
681#else
682 fprintf(stderr, "%s ", buf);
683 vfprintf(stderr, format, args);
684#endif
685 va_end(args);
686}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700687
688int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
689 uint32_t offsets[DRV_MAX_PLANES])
690{
691 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
692 strides[plane] = bo->meta.strides[plane];
693 offsets[plane] = bo->meta.offsets[plane];
694 }
695
696 if (bo->drv->backend->resource_info)
697 return bo->drv->backend->resource_info(bo, strides, offsets);
698
699 return 0;
700}