blob: ea979d3ec9545a73dfe22179afb7afddf14a18b2 [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>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070012#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070013#include <sys/mman.h>
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +080014#include <sys/types.h>
15#include <unistd.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include <xf86drm.h>
17
Alistair Strachan0cfaaa52018-03-19 14:03:23 -070018#ifdef __ANDROID__
19#include <cutils/log.h>
20#include <libgen.h>
21#endif
22
Yiwei Zhangb7a64442021-09-30 05:13:10 +000023#include "drv_helpers.h"
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070024#include "drv_priv.h"
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070025#include "util.h"
26
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053027#ifdef DRV_AMDGPU
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070028extern const struct backend backend_amdgpu;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053029#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070030#ifdef DRV_EXYNOS
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070031extern const struct backend backend_exynos;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070032#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070033#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070034extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070035#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070036#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070037extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070038#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053039#ifdef DRV_MSM
40extern const struct backend backend_msm;
41#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070042#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070043extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070044#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010045#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070046extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010047#endif
Anders Delliene5bef532020-06-10 10:30:44 +010048
49// Dumb / generic drivers
50extern const struct backend backend_evdi;
51extern const struct backend backend_marvell;
52extern const struct backend backend_meson;
53extern const struct backend backend_nouveau;
54extern const struct backend backend_komeda;
55extern const struct backend backend_radeon;
56extern const struct backend backend_synaptics;
Gurchetan Singh73c141e2021-01-21 14:51:19 -080057extern const struct backend backend_virtgpu;
Anders Delliene5bef532020-06-10 10:30:44 +010058extern const struct backend backend_udl;
François-Denis Gonthiercea0b842020-05-22 18:02:24 -040059extern const struct backend backend_vkms;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070060
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070061static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070062{
63 drmVersionPtr drm_version;
64 unsigned int i;
65
66 drm_version = drmGetVersion(fd);
67
68 if (!drm_version)
69 return NULL;
70
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070071 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053072#ifdef DRV_AMDGPU
73 &backend_amdgpu,
74#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070075#ifdef DRV_EXYNOS
76 &backend_exynos,
77#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070078#ifdef DRV_I915
79 &backend_i915,
80#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070081#ifdef DRV_MEDIATEK
82 &backend_mediatek,
83#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053084#ifdef DRV_MSM
85 &backend_msm,
86#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070087#ifdef DRV_ROCKCHIP
88 &backend_rockchip,
89#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010090#ifdef DRV_VC4
91 &backend_vc4,
92#endif
Gurchetan Singh73c141e2021-01-21 14:51:19 -080093 &backend_evdi, &backend_marvell, &backend_meson, &backend_nouveau,
94 &backend_komeda, &backend_radeon, &backend_synaptics, &backend_virtgpu,
95 &backend_udl, &backend_virtgpu, &backend_vkms
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070096 };
97
David Stevens26fe6822020-03-09 12:23:42 +000098 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
99 const struct backend *b = backend_list[i];
David Stevens26fe6822020-03-09 12:23:42 +0000100 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700101 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000102 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700103 }
David Stevens26fe6822020-03-09 12:23:42 +0000104 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700105
106 drmFreeVersion(drm_version);
107 return NULL;
108}
109
110struct driver *drv_create(int fd)
111{
112 struct driver *drv;
113 int ret;
114
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800115 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700116
117 if (!drv)
118 return NULL;
119
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500120 char *minigbm_debug;
121 minigbm_debug = getenv("MINIGBM_DEBUG");
122 drv->compression = (minigbm_debug == NULL) || (strcmp(minigbm_debug, "nocompression") != 0);
123
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700124 drv->fd = fd;
125 drv->backend = drv_get_backend(fd);
126
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700127 if (!drv->backend)
128 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700129
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000130 if (pthread_mutex_init(&drv->buffer_table_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700131 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700132
133 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700134 if (!drv->buffer_table)
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000135 goto free_buffer_table_lock;
136
137 if (pthread_mutex_init(&drv->mappings_lock, NULL))
138 goto free_buffer_table;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700139
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700140 drv->mappings = drv_array_init(sizeof(struct mapping));
141 if (!drv->mappings)
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000142 goto free_mappings_lock;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700143
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700144 drv->combos = drv_array_init(sizeof(struct combination));
145 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700146 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700147
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700148 if (drv->backend->init) {
149 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800150 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700151 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700152 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800153 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700154 }
155
156 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700157
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700158free_mappings:
159 drv_array_destroy(drv->mappings);
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000160free_mappings_lock:
161 pthread_mutex_destroy(&drv->mappings_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700162free_buffer_table:
163 drmHashDestroy(drv->buffer_table);
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000164free_buffer_table_lock:
165 pthread_mutex_destroy(&drv->buffer_table_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700166free_driver:
167 free(drv);
168 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700169}
170
171void drv_destroy(struct driver *drv)
172{
173 if (drv->backend->close)
174 drv->backend->close(drv);
175
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700176 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700177
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000178 drv_array_destroy(drv->mappings);
179 pthread_mutex_destroy(&drv->mappings_lock);
180
181 drmHashDestroy(drv->buffer_table);
182 pthread_mutex_destroy(&drv->buffer_table_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);
Yiwei Zhang01b69742021-09-16 04:47:54 +0000236 errno = EINVAL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700237 return NULL;
238 }
239
240 return bo;
241}
242
Jason Macnak04c8f512021-09-29 11:38:00 -0700243static void drv_bo_mapping_destroy(struct bo *bo)
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000244{
245 struct driver *drv = bo->drv;
246 uint32_t idx = 0;
247
248 /*
249 * This function is called right before the buffer is destroyed. It will free any mappings
250 * associated with the buffer.
251 */
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000252 pthread_mutex_lock(&drv->mappings_lock);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000253 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
254 while (idx < drv_array_size(drv->mappings)) {
255 struct mapping *mapping =
256 (struct mapping *)drv_array_at_idx(drv->mappings, idx);
257 if (mapping->vma->handle != bo->handles[plane].u32) {
258 idx++;
259 continue;
260 }
261
262 if (!--mapping->vma->refcount) {
263 int ret = drv->backend->bo_unmap(bo, mapping->vma);
264 if (ret) {
Yiwei Zhang8bc35bf2021-10-04 21:36:23 +0000265 pthread_mutex_unlock(&drv->mappings_lock);
266 assert(ret);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000267 drv_log("munmap failed\n");
Jason Macnak04c8f512021-09-29 11:38:00 -0700268 return;
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000269 }
270
271 free(mapping->vma);
272 }
273
274 /* This shrinks and shifts the array, so don't increment idx. */
275 drv_array_remove(drv->mappings, idx);
276 }
277 }
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000278 pthread_mutex_unlock(&drv->mappings_lock);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000279}
280
281/*
282 * Acquire a reference on plane buffers of the bo.
283 */
284static void drv_bo_acquire(struct bo *bo)
285{
286 struct driver *drv = bo->drv;
287
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000288 pthread_mutex_lock(&drv->buffer_table_lock);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000289 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
290 uintptr_t num = 0;
291
292 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, (void **)&num))
293 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
294
295 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
296 }
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000297 pthread_mutex_unlock(&drv->buffer_table_lock);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000298}
299
300/*
301 * Release a reference on plane buffers of the bo. Return true when the bo has lost all its
302 * references. Otherwise, return false.
303 */
304static bool drv_bo_release(struct bo *bo)
305{
306 struct driver *drv = bo->drv;
Yiwei Zhang8bc35bf2021-10-04 21:36:23 +0000307 uintptr_t num;
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000308
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000309 pthread_mutex_lock(&drv->buffer_table_lock);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000310 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000311 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, (void **)&num)) {
312 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000313
Yiwei Zhang8bc35bf2021-10-04 21:36:23 +0000314 if (num > 1) {
315 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
316 (void *)(num - 1));
317 }
318 }
319 }
320
321 /* The same buffer can back multiple planes with different offsets. */
322 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
323 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, (void **)&num)) {
324 /* num is positive if found in the hashmap. */
325 pthread_mutex_unlock(&drv->buffer_table_lock);
326 return false;
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000327 }
328 }
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000329 pthread_mutex_unlock(&drv->buffer_table_lock);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000330
Yiwei Zhang8bc35bf2021-10-04 21:36:23 +0000331 return true;
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000332}
333
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800334struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700335 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700336{
337 int ret;
338 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000339 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700340
David Stevens26fe6822020-03-09 12:23:42 +0000341 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
342 use_flags &= ~BO_USE_TEST_ALLOC;
343
344 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700345
346 if (!bo)
347 return NULL;
348
David Stevens26fe6822020-03-09 12:23:42 +0000349 ret = -EINVAL;
350 if (drv->backend->bo_compute_metadata) {
351 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
352 0);
353 if (!is_test_alloc && ret == 0)
354 ret = drv->backend->bo_create_from_metadata(bo);
355 } else if (!is_test_alloc) {
356 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
357 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700358
359 if (ret) {
Yiwei Zhang01b69742021-09-16 04:47:54 +0000360 errno = -ret;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700361 free(bo);
362 return NULL;
363 }
364
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000365 drv_bo_acquire(bo);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700366
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700367 return bo;
368}
369
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800370struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
371 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700372{
373 int ret;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700374 struct bo *bo;
375
David Stevens26fe6822020-03-09 12:23:42 +0000376 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700377 errno = ENOENT;
378 return NULL;
379 }
380
David Stevens26fe6822020-03-09 12:23:42 +0000381 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700382
383 if (!bo)
384 return NULL;
385
David Stevens26fe6822020-03-09 12:23:42 +0000386 ret = -EINVAL;
387 if (drv->backend->bo_compute_metadata) {
388 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
389 modifiers, count);
390 if (ret == 0)
391 ret = drv->backend->bo_create_from_metadata(bo);
392 } else {
393 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
394 count);
395 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700396
397 if (ret) {
398 free(bo);
399 return NULL;
400 }
401
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000402 drv_bo_acquire(bo);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700403
404 return bo;
405}
406
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700407void drv_bo_destroy(struct bo *bo)
408{
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000409 if (!bo->is_test_buffer && drv_bo_release(bo)) {
Jason Macnak04c8f512021-09-29 11:38:00 -0700410 drv_bo_mapping_destroy(bo);
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000411 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900412 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700413
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700414 free(bo);
415}
416
417struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
418{
419 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700420 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700421 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700422 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700423
David Stevens26fe6822020-03-09 12:23:42 +0000424 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700425
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700426 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700427 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700428
Gurchetan Singh71611d62017-01-03 16:49:56 -0800429 ret = drv->backend->bo_import(bo, data);
430 if (ret) {
431 free(bo);
432 return NULL;
433 }
434
Yiwei Zhang7fae5d02021-09-24 21:54:20 +0000435 drv_bo_acquire(bo);
Satyajit Sahua047d412018-07-12 12:29:39 +0530436
Gurchetan Singh52155b42021-01-27 17:55:17 -0800437 bo->meta.format_modifier = data->format_modifier;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700438 for (plane = 0; plane < bo->meta.num_planes; plane++) {
439 bo->meta.strides[plane] = data->strides[plane];
440 bo->meta.offsets[plane] = data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700441
442 seek_end = lseek(data->fds[plane], 0, SEEK_END);
443 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700444 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700445 goto destroy_bo;
446 }
447
448 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700449 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
450 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700451 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700452 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700453
Gurchetan Singh298b7572019-09-19 09:55:18 -0700454 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700455 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700456 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800457 }
458
Gurchetan Singh298b7572019-09-19 09:55:18 -0700459 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700460 }
461
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700462 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700463
464destroy_bo:
465 drv_bo_destroy(bo);
466 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700467}
468
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800469void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
470 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700471{
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000472 struct driver *drv = bo->drv;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700473 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700474 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700475 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700476
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800477 assert(rect->width >= 0);
478 assert(rect->height >= 0);
479 assert(rect->x + rect->width <= drv_bo_get_width(bo));
480 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700481 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900482 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700483 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700484
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800485 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000486 return MAP_FAILED;
David Stevens26fe6822020-03-09 12:23:42 +0000487
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800488 mapping.rect = *rect;
489 mapping.refcount = 1;
490
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000491 pthread_mutex_lock(&drv->mappings_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700492
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000493 for (i = 0; i < drv_array_size(drv->mappings); i++) {
494 struct mapping *prior = (struct mapping *)drv_array_at_idx(drv->mappings, i);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700495 if (prior->vma->handle != bo->handles[plane].u32 ||
496 prior->vma->map_flags != map_flags)
497 continue;
498
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800499 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
500 rect->width != prior->rect.width || rect->height != prior->rect.height)
501 continue;
502
503 prior->refcount++;
504 *map_data = prior;
505 goto exact_match;
506 }
507
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000508 for (i = 0; i < drv_array_size(drv->mappings); i++) {
509 struct mapping *prior = (struct mapping *)drv_array_at_idx(drv->mappings, i);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800510 if (prior->vma->handle != bo->handles[plane].u32 ||
511 prior->vma->map_flags != map_flags)
512 continue;
513
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700514 prior->vma->refcount++;
515 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700516 goto success;
517 }
518
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700519 mapping.vma = calloc(1, sizeof(*mapping.vma));
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000520 if (!mapping.vma) {
521 *map_data = NULL;
522 pthread_mutex_unlock(&drv->mappings_lock);
523 return MAP_FAILED;
524 }
525
Gurchetan Singh298b7572019-09-19 09:55:18 -0700526 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000527 addr = drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700528 if (addr == MAP_FAILED) {
529 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700530 free(mapping.vma);
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000531 pthread_mutex_unlock(&drv->mappings_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700532 return MAP_FAILED;
533 }
534
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700535 mapping.vma->refcount = 1;
536 mapping.vma->addr = addr;
537 mapping.vma->handle = bo->handles[plane].u32;
538 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700539
540success:
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000541 *map_data = drv_array_append(drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800542exact_match:
543 drv_bo_invalidate(bo, *map_data);
544 addr = (uint8_t *)((*map_data)->vma->addr);
545 addr += drv_bo_get_plane_offset(bo, plane);
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000546 pthread_mutex_unlock(&drv->mappings_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800547 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700548}
549
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700550int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700551{
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000552 struct driver *drv = bo->drv;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700553 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700554 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700555
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000556 pthread_mutex_lock(&drv->mappings_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700557
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800558 if (--mapping->refcount)
559 goto out;
560
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700561 if (!--mapping->vma->refcount) {
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000562 ret = drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700563 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700564 }
565
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000566 for (i = 0; i < drv_array_size(drv->mappings); i++) {
567 if (mapping == (struct mapping *)drv_array_at_idx(drv->mappings, i)) {
568 drv_array_remove(drv->mappings, i);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700569 break;
570 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700571 }
572
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800573out:
Yiwei Zhang84236dd2021-09-27 20:18:58 +0000574 pthread_mutex_unlock(&drv->mappings_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700575 return ret;
576}
577
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700578int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700579{
580 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700581
582 assert(mapping);
583 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800584 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700585 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700586
587 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700588 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700589
590 return ret;
591}
592
Jason Macnak1de7f662020-01-24 15:05:57 -0800593int drv_bo_flush(struct bo *bo, struct mapping *mapping)
594{
595 int ret = 0;
596
597 assert(mapping);
598 assert(mapping->vma);
599 assert(mapping->refcount > 0);
600 assert(mapping->vma->refcount > 0);
601
602 if (bo->drv->backend->bo_flush)
603 ret = bo->drv->backend->bo_flush(bo, mapping);
604
605 return ret;
606}
607
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700608int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700609{
610 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700611
612 assert(mapping);
613 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800614 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700615 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700616 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700617
618 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700619 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700620 else
621 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700622
623 return ret;
624}
625
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700626uint32_t drv_bo_get_width(struct bo *bo)
627{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700628 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700629}
630
631uint32_t drv_bo_get_height(struct bo *bo)
632{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700633 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700634}
635
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700636size_t drv_bo_get_num_planes(struct bo *bo)
637{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700638 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700639}
640
641union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
642{
643 return bo->handles[plane];
644}
645
646#ifndef DRM_RDWR
647#define DRM_RDWR O_RDWR
648#endif
649
650int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
651{
652
653 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700654 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700655
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800656 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000657 return -EINVAL;
David Stevens26fe6822020-03-09 12:23:42 +0000658
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800659 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700660
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700661 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
662 if (ret)
663 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
664
Jason Macnak166fe142021-01-29 07:50:34 -0800665 if (ret)
666 drv_log("Failed to get plane fd: %s\n", strerror(errno));
667
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700668 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700669}
670
671uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
672{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700673 assert(plane < bo->meta.num_planes);
674 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700675}
676
677uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
678{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700679 assert(plane < bo->meta.num_planes);
680 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700681}
682
683uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
684{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700685 assert(plane < bo->meta.num_planes);
686 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700687}
688
Gurchetan Singh52155b42021-01-27 17:55:17 -0800689uint64_t drv_bo_get_format_modifier(struct bo *bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700690{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800691 return bo->meta.format_modifier;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700692}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700693
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800694uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700695{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700696 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700697}
698
Yiwei Zhang1f9b9002021-09-15 21:28:51 +0000699uint32_t drv_bo_get_tiling(struct bo *bo)
700{
701 return bo->meta.tiling;
702}
703
704uint64_t drv_bo_get_use_flags(struct bo *bo)
705{
706 return bo->meta.use_flags;
707}
708
Jason Macnak1de7f662020-01-24 15:05:57 -0800709size_t drv_bo_get_total_size(struct bo *bo)
710{
711 return bo->meta.total_size;
712}
713
Yiwei Zhangb7a64442021-09-30 05:13:10 +0000714/*
715 * Map internal fourcc codes back to standard fourcc codes.
716 */
717uint32_t drv_get_standard_fourcc(uint32_t fourcc_internal)
718{
719 return (fourcc_internal == DRM_FORMAT_YVU420_ANDROID) ? DRM_FORMAT_YVU420 : fourcc_internal;
720}
721
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000722void drv_resolve_format_and_use_flags(struct driver *drv, uint32_t format, uint64_t use_flags,
723 uint32_t *out_format, uint64_t *out_use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700724{
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000725 assert(drv->backend->resolve_format_and_use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700726
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000727 drv->backend->resolve_format_and_use_flags(drv, format, use_flags, out_format,
728 out_use_flags);
Yiwei Zhangc1413ea2021-09-17 08:20:21 +0000729}
730
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700731uint32_t drv_num_buffers_per_bo(struct bo *bo)
732{
733 uint32_t count = 0;
734 size_t plane, p;
735
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800736 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000737 return 0;
David Stevens26fe6822020-03-09 12:23:42 +0000738
Gurchetan Singh298b7572019-09-19 09:55:18 -0700739 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700740 for (p = 0; p < plane; p++)
741 if (bo->handles[p].u32 == bo->handles[plane].u32)
742 break;
743 if (p == plane)
744 count++;
745 }
746
747 return count;
748}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700749
750void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
751{
752 char buf[50];
753 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
754
755 va_list args;
756 va_start(args, format);
757#ifdef __ANDROID__
758 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
759#else
760 fprintf(stderr, "%s ", buf);
761 vfprintf(stderr, format, args);
762#endif
763 va_end(args);
764}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700765
766int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000767 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700768{
769 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
770 strides[plane] = bo->meta.strides[plane];
771 offsets[plane] = bo->meta.offsets[plane];
772 }
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000773 *format_modifier = bo->meta.format_modifier;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700774
775 if (bo->drv->backend->resource_info)
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000776 return bo->drv->backend->resource_info(bo, strides, offsets, format_modifier);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700777
778 return 0;
779}
Jason Macnak336fd052021-09-29 11:10:06 -0700780
781uint32_t drv_get_max_texture_2d_size(struct driver *drv)
782{
783 if (drv->backend->get_max_texture_2d_size)
784 return drv->backend->get_max_texture_2d_size(drv);
785
786 return UINT32_MAX;
787}