blob: 5b6a1169b6c5605d6b59f7d703f7dfbc12f1e2e0 [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 Singh3e9d3832017-10-31 10:36:25 -070026extern const struct backend backend_evdi;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070027#ifdef DRV_EXYNOS
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070028extern const struct backend backend_exynos;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070029#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070030extern const struct backend backend_gma500;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070031#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070032extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070033#endif
34#ifdef DRV_MARVELL
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070035extern const struct backend backend_marvell;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070036#endif
37#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
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070040extern const struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040041#ifdef DRV_RADEON
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070042extern const struct backend backend_radeon;
giri3f259512017-08-02 12:01:33 -040043#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070044#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070045extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070046#endif
47#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070048extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070049#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010051#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070052extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010053#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070054extern const struct backend backend_vgem;
55extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070056
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070057static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070058{
59 drmVersionPtr drm_version;
60 unsigned int i;
61
62 drm_version = drmGetVersion(fd);
63
64 if (!drm_version)
65 return NULL;
66
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070067 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053068#ifdef DRV_AMDGPU
69 &backend_amdgpu,
70#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070071 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070072#ifdef DRV_EXYNOS
73 &backend_exynos,
74#endif
75 &backend_gma500,
76#ifdef DRV_I915
77 &backend_i915,
78#endif
79#ifdef DRV_MARVELL
80 &backend_marvell,
81#endif
82#ifdef DRV_MEDIATEK
83 &backend_mediatek,
84#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050085 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -040086#ifdef DRV_RADEON
87 &backend_radeon,
88#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070089#ifdef DRV_ROCKCHIP
90 &backend_rockchip,
91#endif
92#ifdef DRV_TEGRA
93 &backend_tegra,
94#endif
95 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +010096#ifdef DRV_VC4
97 &backend_vc4,
98#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080099 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700100 };
101
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800102 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700103 if (!strcmp(drm_version->name, backend_list[i]->name)) {
104 drmFreeVersion(drm_version);
105 return backend_list[i];
106 }
107
108 drmFreeVersion(drm_version);
109 return NULL;
110}
111
112struct driver *drv_create(int fd)
113{
114 struct driver *drv;
115 int ret;
116
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800117 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700118
119 if (!drv)
120 return NULL;
121
122 drv->fd = fd;
123 drv->backend = drv_get_backend(fd);
124
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700125 if (!drv->backend)
126 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700127
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800128 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700129 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700130
131 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700132 if (!drv->buffer_table)
133 goto free_lock;
134
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700135 drv->mappings = drv_array_init(sizeof(struct mapping));
136 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700137 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700138
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700139 drv->combos = drv_array_init(sizeof(struct combination));
140 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700141 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700142
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700143 if (drv->backend->init) {
144 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800145 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700146 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700147 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800148 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700149 }
150
151 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700152
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700153free_mappings:
154 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700155free_buffer_table:
156 drmHashDestroy(drv->buffer_table);
157free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800158 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700159free_driver:
160 free(drv);
161 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700162}
163
164void drv_destroy(struct driver *drv)
165{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800166 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700167
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700168 if (drv->backend->close)
169 drv->backend->close(drv);
170
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700171 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700172 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700173 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700174
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800175 pthread_mutex_unlock(&drv->driver_lock);
176 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700177
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700178 free(drv);
179}
180
181int drv_get_fd(struct driver *drv)
182{
183 return drv->fd;
184}
185
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800186const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700187{
188 return drv->backend->name;
189}
190
Gurchetan Singha1892b22017-09-28 16:40:52 -0700191struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700192{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800193 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700194
Gurchetan Singha1892b22017-09-28 16:40:52 -0700195 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700196 return 0;
197
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800198 best = NULL;
199 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700200 for (i = 0; i < drv_array_size(drv->combos); i++) {
201 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700202 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800203 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800204 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700205 }
206
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800207 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700208}
209
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700210struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700211 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700212{
213
214 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800215 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700216
217 if (!bo)
218 return NULL;
219
220 bo->drv = drv;
221 bo->width = width;
222 bo->height = height;
223 bo->format = format;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700224 bo->use_flags = use_flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700225 bo->num_planes = drv_num_planes_from_format(format);
226
227 if (!bo->num_planes) {
228 free(bo);
229 return NULL;
230 }
231
232 return bo;
233}
234
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800235struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700236 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700237{
238 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700239 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700240 struct bo *bo;
241
Gurchetan Singha1892b22017-09-28 16:40:52 -0700242 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700243
244 if (!bo)
245 return NULL;
246
Gurchetan Singha1892b22017-09-28 16:40:52 -0700247 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700248
249 if (ret) {
250 free(bo);
251 return NULL;
252 }
253
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800254 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700255
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700256 for (plane = 0; plane < bo->num_planes; plane++) {
257 if (plane > 0)
258 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
259
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700260 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700261 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700262
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800263 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700264
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700265 return bo;
266}
267
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800268struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
269 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700270{
271 int ret;
272 size_t plane;
273 struct bo *bo;
274
275 if (!drv->backend->bo_create_with_modifiers) {
276 errno = ENOENT;
277 return NULL;
278 }
279
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700280 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700281
282 if (!bo)
283 return NULL;
284
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800285 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700286
287 if (ret) {
288 free(bo);
289 return NULL;
290 }
291
292 pthread_mutex_lock(&drv->driver_lock);
293
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700294 for (plane = 0; plane < bo->num_planes; plane++) {
295 if (plane > 0)
296 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
297
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700298 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700299 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700300
301 pthread_mutex_unlock(&drv->driver_lock);
302
303 return bo;
304}
305
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700306void drv_bo_destroy(struct bo *bo)
307{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700308 size_t plane;
309 uintptr_t total = 0;
310 struct driver *drv = bo->drv;
311
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800312 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700313
314 for (plane = 0; plane < bo->num_planes; plane++)
315 drv_decrement_reference_count(drv, bo, plane);
316
Gurchetan Singhde263f12017-01-24 13:30:06 -0800317 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700318 total += drv_get_reference_count(drv, bo, plane);
319
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800320 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700321
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900322 if (total == 0) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700323 assert(drv_mapping_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700324 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900325 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700326
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700327 free(bo);
328}
329
330struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
331{
332 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700333 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700334 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700335 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700336
Gurchetan Singha1892b22017-09-28 16:40:52 -0700337 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700338
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700339 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700340 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700341
Gurchetan Singh71611d62017-01-03 16:49:56 -0800342 ret = drv->backend->bo_import(bo, data);
343 if (ret) {
344 free(bo);
345 return NULL;
346 }
347
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700348 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700349 bo->strides[plane] = data->strides[plane];
350 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700351 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700352
353 seek_end = lseek(data->fds[plane], 0, SEEK_END);
354 if (seek_end == (off_t)(-1)) {
355 fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
356 goto destroy_bo;
357 }
358
359 lseek(data->fds[plane], 0, SEEK_SET);
360 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
361 bo->sizes[plane] = seek_end - data->offsets[plane];
362 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800363 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700364
365 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
366 fprintf(stderr, "drv: buffer size is too large.\n");
367 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800368 }
369
370 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700371 }
372
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700373 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700374
375destroy_bo:
376 drv_bo_destroy(bo);
377 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700378}
379
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800380void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
381 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700382{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700383 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700384 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700385 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700386
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800387 assert(rect->width >= 0);
388 assert(rect->height >= 0);
389 assert(rect->x + rect->width <= drv_bo_get_width(bo));
390 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700391 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900392 /* No CPU access for protected buffers. */
393 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700394
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700395 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800396 mapping.rect = *rect;
397 mapping.refcount = 1;
398
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800399 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700400
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700401 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
402 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
403 if (prior->vma->handle != bo->handles[plane].u32 ||
404 prior->vma->map_flags != map_flags)
405 continue;
406
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800407 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
408 rect->width != prior->rect.width || rect->height != prior->rect.height)
409 continue;
410
411 prior->refcount++;
412 *map_data = prior;
413 goto exact_match;
414 }
415
416 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
417 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
418 if (prior->vma->handle != bo->handles[plane].u32 ||
419 prior->vma->map_flags != map_flags)
420 continue;
421
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700422 prior->vma->refcount++;
423 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700424 goto success;
425 }
426
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700427 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800428 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700429 if (addr == MAP_FAILED) {
430 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700431 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800432 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700433 return MAP_FAILED;
434 }
435
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700436 mapping.vma->refcount = 1;
437 mapping.vma->addr = addr;
438 mapping.vma->handle = bo->handles[plane].u32;
439 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700440
441success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700442 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800443exact_match:
444 drv_bo_invalidate(bo, *map_data);
445 addr = (uint8_t *)((*map_data)->vma->addr);
446 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800447 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800448 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700449}
450
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700451int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700452{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700453 uint32_t i;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700454 int ret = drv_bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700455 if (ret)
456 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700457
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800458 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700459
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800460 if (--mapping->refcount)
461 goto out;
462
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700463 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800464 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700465 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700466 }
467
468 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
469 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
470 drv_array_remove(bo->drv->mappings, i);
471 break;
472 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700473 }
474
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800475out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800476 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700477 return ret;
478}
479
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700480int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700481{
482 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700483
484 assert(mapping);
485 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800486 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700487 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700488
489 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700490 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700491
492 return ret;
493}
494
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700495int drv_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700496{
497 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700498
499 assert(mapping);
500 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800501 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700502 assert(mapping->vma->refcount > 0);
Tomasz Figae0807b12017-08-04 12:50:03 +0900503 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700504
505 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700506 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700507
508 return ret;
509}
510
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700511uint32_t drv_bo_get_width(struct bo *bo)
512{
513 return bo->width;
514}
515
516uint32_t drv_bo_get_height(struct bo *bo)
517{
518 return bo->height;
519}
520
521uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
522{
523 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
524}
525
526size_t drv_bo_get_num_planes(struct bo *bo)
527{
528 return bo->num_planes;
529}
530
531union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
532{
533 return bo->handles[plane];
534}
535
536#ifndef DRM_RDWR
537#define DRM_RDWR O_RDWR
538#endif
539
540int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
541{
542
543 int ret, fd;
544 assert(plane < bo->num_planes);
545
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800546 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700547
548 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700549}
550
551uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
552{
553 assert(plane < bo->num_planes);
554 return bo->offsets[plane];
555}
556
557uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
558{
559 assert(plane < bo->num_planes);
560 return bo->sizes[plane];
561}
562
563uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
564{
565 assert(plane < bo->num_planes);
566 return bo->strides[plane];
567}
568
569uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
570{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800571 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700572 return bo->format_modifiers[plane];
573}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700574
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800575uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700576{
577 return bo->format;
578}
579
Gurchetan Singha1892b22017-09-28 16:40:52 -0700580uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700581{
582 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700583 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700584
585 return format;
586}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700587
Gurchetan Singh2a119342016-11-02 10:40:51 -0700588size_t drv_num_planes_from_format(uint32_t format)
589{
590 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800591 case DRM_FORMAT_ABGR1555:
592 case DRM_FORMAT_ABGR2101010:
593 case DRM_FORMAT_ABGR4444:
594 case DRM_FORMAT_ABGR8888:
595 case DRM_FORMAT_ARGB1555:
596 case DRM_FORMAT_ARGB2101010:
597 case DRM_FORMAT_ARGB4444:
598 case DRM_FORMAT_ARGB8888:
599 case DRM_FORMAT_AYUV:
600 case DRM_FORMAT_BGR233:
601 case DRM_FORMAT_BGR565:
602 case DRM_FORMAT_BGR888:
603 case DRM_FORMAT_BGRA1010102:
604 case DRM_FORMAT_BGRA4444:
605 case DRM_FORMAT_BGRA5551:
606 case DRM_FORMAT_BGRA8888:
607 case DRM_FORMAT_BGRX1010102:
608 case DRM_FORMAT_BGRX4444:
609 case DRM_FORMAT_BGRX5551:
610 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800611 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800612 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800613 case DRM_FORMAT_R8:
614 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800615 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800616 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800617 case DRM_FORMAT_RGB888:
618 case DRM_FORMAT_RGBA1010102:
619 case DRM_FORMAT_RGBA4444:
620 case DRM_FORMAT_RGBA5551:
621 case DRM_FORMAT_RGBA8888:
622 case DRM_FORMAT_RGBX1010102:
623 case DRM_FORMAT_RGBX4444:
624 case DRM_FORMAT_RGBX5551:
625 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800626 case DRM_FORMAT_UYVY:
627 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800628 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800629 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800630 case DRM_FORMAT_XBGR4444:
631 case DRM_FORMAT_XBGR8888:
632 case DRM_FORMAT_XRGB1555:
633 case DRM_FORMAT_XRGB2101010:
634 case DRM_FORMAT_XRGB4444:
635 case DRM_FORMAT_XRGB8888:
636 case DRM_FORMAT_YUYV:
637 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700638 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800639 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530640 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700641 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800642 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800643 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700644 return 3;
645 }
646
647 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
648 return 0;
649}
650
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700651uint32_t drv_num_buffers_per_bo(struct bo *bo)
652{
653 uint32_t count = 0;
654 size_t plane, p;
655
656 for (plane = 0; plane < bo->num_planes; plane++) {
657 for (p = 0; p < plane; p++)
658 if (bo->handles[p].u32 == bo->handles[plane].u32)
659 break;
660 if (p == plane)
661 count++;
662 }
663
664 return count;
665}