blob: 7e12873696519ac6aa41f9f7f8a858789f3fe700 [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
19#include "drv_priv.h"
20#include "helpers.h"
21#include "util.h"
22
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053023#ifdef DRV_AMDGPU
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070024extern const struct backend backend_amdgpu;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053025#endif
Gurchetan Singh218edbe2018-01-12 15:22:53 -080026#ifdef DRV_AMLOGIC
27extern const struct backend backend_amlogic;
28#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070029extern const struct backend backend_evdi;
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
36#ifdef DRV_MARVELL
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070037extern const struct backend backend_marvell;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070038#endif
39#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070040extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070041#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070042extern const struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040043#ifdef DRV_RADEON
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070044extern const struct backend backend_radeon;
giri3f259512017-08-02 12:01:33 -040045#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070046#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070047extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070048#endif
49#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070051#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070052extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010053#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070054extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010055#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070056extern const struct backend backend_vgem;
57extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070058
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070059static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070060{
61 drmVersionPtr drm_version;
62 unsigned int i;
63
64 drm_version = drmGetVersion(fd);
65
66 if (!drm_version)
67 return NULL;
68
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070069 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053070#ifdef DRV_AMDGPU
71 &backend_amdgpu,
72#endif
Gurchetan Singh08052342018-01-11 15:42:36 -080073#ifdef DRV_AMLOGIC
74 &backend_amlogic,
75#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070076 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070077#ifdef DRV_EXYNOS
78 &backend_exynos,
79#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070080#ifdef DRV_I915
81 &backend_i915,
82#endif
83#ifdef DRV_MARVELL
84 &backend_marvell,
85#endif
86#ifdef DRV_MEDIATEK
87 &backend_mediatek,
88#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050089 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -040090#ifdef DRV_RADEON
91 &backend_radeon,
92#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070093#ifdef DRV_ROCKCHIP
94 &backend_rockchip,
95#endif
96#ifdef DRV_TEGRA
97 &backend_tegra,
98#endif
99 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +0100100#ifdef DRV_VC4
101 &backend_vc4,
102#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800103 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700104 };
105
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800106 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700107 if (!strcmp(drm_version->name, backend_list[i]->name)) {
108 drmFreeVersion(drm_version);
109 return backend_list[i];
110 }
111
112 drmFreeVersion(drm_version);
113 return NULL;
114}
115
116struct driver *drv_create(int fd)
117{
118 struct driver *drv;
119 int ret;
120
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800121 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700122
123 if (!drv)
124 return NULL;
125
126 drv->fd = fd;
127 drv->backend = drv_get_backend(fd);
128
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700129 if (!drv->backend)
130 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700131
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800132 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700133 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700134
135 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700136 if (!drv->buffer_table)
137 goto free_lock;
138
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700139 drv->mappings = drv_array_init(sizeof(struct mapping));
140 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700141 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700142
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700143 drv->combos = drv_array_init(sizeof(struct combination));
144 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700145 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700146
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700147 if (drv->backend->init) {
148 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800149 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700150 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700151 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800152 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700153 }
154
155 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700156
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700157free_mappings:
158 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700159free_buffer_table:
160 drmHashDestroy(drv->buffer_table);
161free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800162 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700163free_driver:
164 free(drv);
165 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700166}
167
168void drv_destroy(struct driver *drv)
169{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800170 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700171
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700172 if (drv->backend->close)
173 drv->backend->close(drv);
174
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700175 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700176 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700177 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700178
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800179 pthread_mutex_unlock(&drv->driver_lock);
180 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700181
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700182 free(drv);
183}
184
185int drv_get_fd(struct driver *drv)
186{
187 return drv->fd;
188}
189
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800190const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700191{
192 return drv->backend->name;
193}
194
Gurchetan Singha1892b22017-09-28 16:40:52 -0700195struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700196{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800197 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700198
Gurchetan Singha1892b22017-09-28 16:40:52 -0700199 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700200 return 0;
201
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800202 best = NULL;
203 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700204 for (i = 0; i < drv_array_size(drv->combos); i++) {
205 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700206 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800207 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800208 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700209 }
210
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800211 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700212}
213
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700214struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700215 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700216{
217
218 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800219 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700220
221 if (!bo)
222 return NULL;
223
224 bo->drv = drv;
225 bo->width = width;
226 bo->height = height;
227 bo->format = format;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700228 bo->use_flags = use_flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700229 bo->num_planes = drv_num_planes_from_format(format);
230
231 if (!bo->num_planes) {
232 free(bo);
233 return NULL;
234 }
235
236 return bo;
237}
238
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800239struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700240 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700241{
242 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700243 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700244 struct bo *bo;
245
Gurchetan Singha1892b22017-09-28 16:40:52 -0700246 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700247
248 if (!bo)
249 return NULL;
250
Gurchetan Singha1892b22017-09-28 16:40:52 -0700251 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700252
253 if (ret) {
254 free(bo);
255 return NULL;
256 }
257
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800258 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700259
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700260 for (plane = 0; plane < bo->num_planes; plane++) {
261 if (plane > 0)
262 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
263
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700264 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700265 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700266
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800267 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700268
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700269 return bo;
270}
271
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800272struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
273 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700274{
275 int ret;
276 size_t plane;
277 struct bo *bo;
278
279 if (!drv->backend->bo_create_with_modifiers) {
280 errno = ENOENT;
281 return NULL;
282 }
283
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700284 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700285
286 if (!bo)
287 return NULL;
288
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800289 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700290
291 if (ret) {
292 free(bo);
293 return NULL;
294 }
295
296 pthread_mutex_lock(&drv->driver_lock);
297
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700298 for (plane = 0; plane < bo->num_planes; plane++) {
299 if (plane > 0)
300 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
301
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700302 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700303 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700304
305 pthread_mutex_unlock(&drv->driver_lock);
306
307 return bo;
308}
309
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700310void drv_bo_destroy(struct bo *bo)
311{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700312 size_t plane;
313 uintptr_t total = 0;
314 struct driver *drv = bo->drv;
315
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800316 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700317
318 for (plane = 0; plane < bo->num_planes; plane++)
319 drv_decrement_reference_count(drv, bo, plane);
320
Gurchetan Singhde263f12017-01-24 13:30:06 -0800321 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700322 total += drv_get_reference_count(drv, bo, plane);
323
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800324 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700325
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900326 if (total == 0) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700327 assert(drv_mapping_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700328 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900329 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700330
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700331 free(bo);
332}
333
334struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
335{
336 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700337 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700338 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700339 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700340
Gurchetan Singha1892b22017-09-28 16:40:52 -0700341 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700342
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700343 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700344 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700345
Gurchetan Singh71611d62017-01-03 16:49:56 -0800346 ret = drv->backend->bo_import(bo, data);
347 if (ret) {
348 free(bo);
349 return NULL;
350 }
351
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700352 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700353 bo->strides[plane] = data->strides[plane];
354 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700355 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700356
357 seek_end = lseek(data->fds[plane], 0, SEEK_END);
358 if (seek_end == (off_t)(-1)) {
359 fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
360 goto destroy_bo;
361 }
362
363 lseek(data->fds[plane], 0, SEEK_SET);
364 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
365 bo->sizes[plane] = seek_end - data->offsets[plane];
366 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800367 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700368
369 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
370 fprintf(stderr, "drv: buffer size is too large.\n");
371 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800372 }
373
374 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700375 }
376
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700377 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700378
379destroy_bo:
380 drv_bo_destroy(bo);
381 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700382}
383
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800384void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
385 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700386{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700387 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700388 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700389 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700390
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800391 assert(rect->width >= 0);
392 assert(rect->height >= 0);
393 assert(rect->x + rect->width <= drv_bo_get_width(bo));
394 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700395 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900396 /* No CPU access for protected buffers. */
397 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700398
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700399 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800400 mapping.rect = *rect;
401 mapping.refcount = 1;
402
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800403 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700404
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700405 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
406 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
407 if (prior->vma->handle != bo->handles[plane].u32 ||
408 prior->vma->map_flags != map_flags)
409 continue;
410
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800411 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
412 rect->width != prior->rect.width || rect->height != prior->rect.height)
413 continue;
414
415 prior->refcount++;
416 *map_data = prior;
417 goto exact_match;
418 }
419
420 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
421 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
422 if (prior->vma->handle != bo->handles[plane].u32 ||
423 prior->vma->map_flags != map_flags)
424 continue;
425
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700426 prior->vma->refcount++;
427 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700428 goto success;
429 }
430
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700431 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800432 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700433 if (addr == MAP_FAILED) {
434 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700435 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800436 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700437 return MAP_FAILED;
438 }
439
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700440 mapping.vma->refcount = 1;
441 mapping.vma->addr = addr;
442 mapping.vma->handle = bo->handles[plane].u32;
443 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700444
445success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700446 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800447exact_match:
448 drv_bo_invalidate(bo, *map_data);
449 addr = (uint8_t *)((*map_data)->vma->addr);
450 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800451 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800452 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700453}
454
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700455int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700456{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700457 uint32_t i;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700458 int ret = drv_bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700459 if (ret)
460 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700461
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800462 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700463
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800464 if (--mapping->refcount)
465 goto out;
466
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700467 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800468 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700469 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700470 }
471
472 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
473 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
474 drv_array_remove(bo->drv->mappings, i);
475 break;
476 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700477 }
478
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800479out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800480 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700481 return ret;
482}
483
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700484int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700485{
486 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700487
488 assert(mapping);
489 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800490 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700491 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700492
493 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700494 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700495
496 return ret;
497}
498
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700499int drv_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700500{
501 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700502
503 assert(mapping);
504 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800505 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700506 assert(mapping->vma->refcount > 0);
Tomasz Figae0807b12017-08-04 12:50:03 +0900507 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700508
509 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700510 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700511
512 return ret;
513}
514
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700515uint32_t drv_bo_get_width(struct bo *bo)
516{
517 return bo->width;
518}
519
520uint32_t drv_bo_get_height(struct bo *bo)
521{
522 return bo->height;
523}
524
525uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
526{
527 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
528}
529
530size_t drv_bo_get_num_planes(struct bo *bo)
531{
532 return bo->num_planes;
533}
534
535union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
536{
537 return bo->handles[plane];
538}
539
540#ifndef DRM_RDWR
541#define DRM_RDWR O_RDWR
542#endif
543
544int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
545{
546
547 int ret, fd;
548 assert(plane < bo->num_planes);
549
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800550 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700551
552 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700553}
554
555uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
556{
557 assert(plane < bo->num_planes);
558 return bo->offsets[plane];
559}
560
561uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
562{
563 assert(plane < bo->num_planes);
564 return bo->sizes[plane];
565}
566
567uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
568{
569 assert(plane < bo->num_planes);
570 return bo->strides[plane];
571}
572
573uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
574{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800575 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700576 return bo->format_modifiers[plane];
577}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700578
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800579uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700580{
581 return bo->format;
582}
583
Gurchetan Singha1892b22017-09-28 16:40:52 -0700584uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700585{
586 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700587 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700588
589 return format;
590}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700591
Gurchetan Singh2a119342016-11-02 10:40:51 -0700592size_t drv_num_planes_from_format(uint32_t format)
593{
594 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800595 case DRM_FORMAT_ABGR1555:
596 case DRM_FORMAT_ABGR2101010:
597 case DRM_FORMAT_ABGR4444:
598 case DRM_FORMAT_ABGR8888:
599 case DRM_FORMAT_ARGB1555:
600 case DRM_FORMAT_ARGB2101010:
601 case DRM_FORMAT_ARGB4444:
602 case DRM_FORMAT_ARGB8888:
603 case DRM_FORMAT_AYUV:
604 case DRM_FORMAT_BGR233:
605 case DRM_FORMAT_BGR565:
606 case DRM_FORMAT_BGR888:
607 case DRM_FORMAT_BGRA1010102:
608 case DRM_FORMAT_BGRA4444:
609 case DRM_FORMAT_BGRA5551:
610 case DRM_FORMAT_BGRA8888:
611 case DRM_FORMAT_BGRX1010102:
612 case DRM_FORMAT_BGRX4444:
613 case DRM_FORMAT_BGRX5551:
614 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800615 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800616 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800617 case DRM_FORMAT_R8:
618 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800619 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800620 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800621 case DRM_FORMAT_RGB888:
622 case DRM_FORMAT_RGBA1010102:
623 case DRM_FORMAT_RGBA4444:
624 case DRM_FORMAT_RGBA5551:
625 case DRM_FORMAT_RGBA8888:
626 case DRM_FORMAT_RGBX1010102:
627 case DRM_FORMAT_RGBX4444:
628 case DRM_FORMAT_RGBX5551:
629 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800630 case DRM_FORMAT_UYVY:
631 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800632 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800633 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800634 case DRM_FORMAT_XBGR4444:
635 case DRM_FORMAT_XBGR8888:
636 case DRM_FORMAT_XRGB1555:
637 case DRM_FORMAT_XRGB2101010:
638 case DRM_FORMAT_XRGB4444:
639 case DRM_FORMAT_XRGB8888:
640 case DRM_FORMAT_YUYV:
641 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700642 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800643 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530644 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700645 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800646 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800647 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700648 return 3;
649 }
650
651 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
652 return 0;
653}
654
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700655uint32_t drv_num_buffers_per_bo(struct bo *bo)
656{
657 uint32_t count = 0;
658 size_t plane, p;
659
660 for (plane = 0; plane < bo->num_planes; plane++) {
661 for (p = 0; p < plane; p++)
662 if (bo->handles[p].u32 == bo->handles[plane].u32)
663 break;
664 if (p == plane)
665 count++;
666 }
667
668 return count;
669}