blob: 1f6fcab5c9309f5e52163aa6c02bed435bc5c3cb [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 Singh3e9d3832017-10-31 10:36:25 -070033extern const struct backend backend_gma500;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070034#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070035extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070036#endif
37#ifdef DRV_MARVELL
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070038extern const struct backend backend_marvell;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070039#endif
40#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070041extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070042#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070043extern const struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040044#ifdef DRV_RADEON
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070045extern const struct backend backend_radeon;
giri3f259512017-08-02 12:01:33 -040046#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070047#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070048extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070049#endif
50#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070051extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070052#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070053extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010054#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070055extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010056#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070057extern const struct backend backend_vgem;
58extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070059
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070060static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070061{
62 drmVersionPtr drm_version;
63 unsigned int i;
64
65 drm_version = drmGetVersion(fd);
66
67 if (!drm_version)
68 return NULL;
69
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070070 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053071#ifdef DRV_AMDGPU
72 &backend_amdgpu,
73#endif
Gurchetan Singh08052342018-01-11 15:42:36 -080074#ifdef DRV_AMLOGIC
75 &backend_amlogic,
76#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070077 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070078#ifdef DRV_EXYNOS
79 &backend_exynos,
80#endif
81 &backend_gma500,
82#ifdef DRV_I915
83 &backend_i915,
84#endif
85#ifdef DRV_MARVELL
86 &backend_marvell,
87#endif
88#ifdef DRV_MEDIATEK
89 &backend_mediatek,
90#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050091 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -040092#ifdef DRV_RADEON
93 &backend_radeon,
94#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070095#ifdef DRV_ROCKCHIP
96 &backend_rockchip,
97#endif
98#ifdef DRV_TEGRA
99 &backend_tegra,
100#endif
101 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +0100102#ifdef DRV_VC4
103 &backend_vc4,
104#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800105 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700106 };
107
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800108 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700109 if (!strcmp(drm_version->name, backend_list[i]->name)) {
110 drmFreeVersion(drm_version);
111 return backend_list[i];
112 }
113
114 drmFreeVersion(drm_version);
115 return NULL;
116}
117
118struct driver *drv_create(int fd)
119{
120 struct driver *drv;
121 int ret;
122
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800123 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700124
125 if (!drv)
126 return NULL;
127
128 drv->fd = fd;
129 drv->backend = drv_get_backend(fd);
130
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700131 if (!drv->backend)
132 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700133
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800134 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700135 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700136
137 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700138 if (!drv->buffer_table)
139 goto free_lock;
140
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700141 drv->mappings = drv_array_init(sizeof(struct mapping));
142 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700143 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700144
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700145 drv->combos = drv_array_init(sizeof(struct combination));
146 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700147 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700148
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700149 if (drv->backend->init) {
150 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800151 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700152 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700153 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800154 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700155 }
156
157 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700158
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700159free_mappings:
160 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700161free_buffer_table:
162 drmHashDestroy(drv->buffer_table);
163free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800164 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700165free_driver:
166 free(drv);
167 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700168}
169
170void drv_destroy(struct driver *drv)
171{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800172 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700173
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700174 if (drv->backend->close)
175 drv->backend->close(drv);
176
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700177 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700178 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700179 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700180
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800181 pthread_mutex_unlock(&drv->driver_lock);
182 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700183
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700184 free(drv);
185}
186
187int drv_get_fd(struct driver *drv)
188{
189 return drv->fd;
190}
191
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800192const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700193{
194 return drv->backend->name;
195}
196
Gurchetan Singha1892b22017-09-28 16:40:52 -0700197struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700198{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800199 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700200
Gurchetan Singha1892b22017-09-28 16:40:52 -0700201 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700202 return 0;
203
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800204 best = NULL;
205 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700206 for (i = 0; i < drv_array_size(drv->combos); i++) {
207 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700208 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800209 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800210 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700211 }
212
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800213 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700214}
215
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700216struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700217 uint64_t use_flags)
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;
227 bo->width = width;
228 bo->height = height;
229 bo->format = format;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700230 bo->use_flags = use_flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700231 bo->num_planes = drv_num_planes_from_format(format);
232
233 if (!bo->num_planes) {
234 free(bo);
235 return NULL;
236 }
237
238 return bo;
239}
240
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800241struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700242 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700243{
244 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700245 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700246 struct bo *bo;
247
Gurchetan Singha1892b22017-09-28 16:40:52 -0700248 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700249
250 if (!bo)
251 return NULL;
252
Gurchetan Singha1892b22017-09-28 16:40:52 -0700253 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700254
255 if (ret) {
256 free(bo);
257 return NULL;
258 }
259
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800260 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700261
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700262 for (plane = 0; plane < bo->num_planes; plane++) {
263 if (plane > 0)
264 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
265
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700266 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700267 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700268
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800269 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700270
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700271 return bo;
272}
273
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800274struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
275 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700276{
277 int ret;
278 size_t plane;
279 struct bo *bo;
280
281 if (!drv->backend->bo_create_with_modifiers) {
282 errno = ENOENT;
283 return NULL;
284 }
285
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700286 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700287
288 if (!bo)
289 return NULL;
290
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800291 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700292
293 if (ret) {
294 free(bo);
295 return NULL;
296 }
297
298 pthread_mutex_lock(&drv->driver_lock);
299
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700300 for (plane = 0; plane < bo->num_planes; plane++) {
301 if (plane > 0)
302 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
303
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700304 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700305 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700306
307 pthread_mutex_unlock(&drv->driver_lock);
308
309 return bo;
310}
311
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700312void drv_bo_destroy(struct bo *bo)
313{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700314 size_t plane;
315 uintptr_t total = 0;
316 struct driver *drv = bo->drv;
317
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800318 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700319
320 for (plane = 0; plane < bo->num_planes; plane++)
321 drv_decrement_reference_count(drv, bo, plane);
322
Gurchetan Singhde263f12017-01-24 13:30:06 -0800323 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700324 total += drv_get_reference_count(drv, bo, plane);
325
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800326 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700327
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900328 if (total == 0) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700329 assert(drv_mapping_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700330 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900331 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700332
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700333 free(bo);
334}
335
336struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
337{
338 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700339 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700340 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700341 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700342
Gurchetan Singha1892b22017-09-28 16:40:52 -0700343 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700344
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700345 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700346 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700347
Gurchetan Singh71611d62017-01-03 16:49:56 -0800348 ret = drv->backend->bo_import(bo, data);
349 if (ret) {
350 free(bo);
351 return NULL;
352 }
353
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700354 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700355 bo->strides[plane] = data->strides[plane];
356 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700357 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700358
359 seek_end = lseek(data->fds[plane], 0, SEEK_END);
360 if (seek_end == (off_t)(-1)) {
361 fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
362 goto destroy_bo;
363 }
364
365 lseek(data->fds[plane], 0, SEEK_SET);
366 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
367 bo->sizes[plane] = seek_end - data->offsets[plane];
368 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800369 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700370
371 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
372 fprintf(stderr, "drv: buffer size is too large.\n");
373 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800374 }
375
376 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700377 }
378
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700379 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700380
381destroy_bo:
382 drv_bo_destroy(bo);
383 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700384}
385
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800386void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
387 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700388{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700389 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700390 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700391 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700392
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800393 assert(rect->width >= 0);
394 assert(rect->height >= 0);
395 assert(rect->x + rect->width <= drv_bo_get_width(bo));
396 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700397 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900398 /* No CPU access for protected buffers. */
399 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700400
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700401 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800402 mapping.rect = *rect;
403 mapping.refcount = 1;
404
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800405 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700406
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700407 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
408 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
409 if (prior->vma->handle != bo->handles[plane].u32 ||
410 prior->vma->map_flags != map_flags)
411 continue;
412
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800413 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
414 rect->width != prior->rect.width || rect->height != prior->rect.height)
415 continue;
416
417 prior->refcount++;
418 *map_data = prior;
419 goto exact_match;
420 }
421
422 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
423 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
424 if (prior->vma->handle != bo->handles[plane].u32 ||
425 prior->vma->map_flags != map_flags)
426 continue;
427
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700428 prior->vma->refcount++;
429 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700430 goto success;
431 }
432
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700433 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800434 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700435 if (addr == MAP_FAILED) {
436 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700437 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800438 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700439 return MAP_FAILED;
440 }
441
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700442 mapping.vma->refcount = 1;
443 mapping.vma->addr = addr;
444 mapping.vma->handle = bo->handles[plane].u32;
445 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700446
447success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700448 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800449exact_match:
450 drv_bo_invalidate(bo, *map_data);
451 addr = (uint8_t *)((*map_data)->vma->addr);
452 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800453 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800454 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700455}
456
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700457int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700458{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700459 uint32_t i;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700460 int ret = drv_bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700461 if (ret)
462 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700463
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800464 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700465
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800466 if (--mapping->refcount)
467 goto out;
468
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700469 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800470 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700471 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700472 }
473
474 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
475 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
476 drv_array_remove(bo->drv->mappings, i);
477 break;
478 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700479 }
480
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800481out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800482 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700483 return ret;
484}
485
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700486int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700487{
488 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700489
490 assert(mapping);
491 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800492 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700493 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700494
495 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700496 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700497
498 return ret;
499}
500
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700501int drv_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700502{
503 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700504
505 assert(mapping);
506 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800507 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700508 assert(mapping->vma->refcount > 0);
Tomasz Figae0807b12017-08-04 12:50:03 +0900509 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700510
511 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700512 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700513
514 return ret;
515}
516
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700517uint32_t drv_bo_get_width(struct bo *bo)
518{
519 return bo->width;
520}
521
522uint32_t drv_bo_get_height(struct bo *bo)
523{
524 return bo->height;
525}
526
527uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
528{
529 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
530}
531
532size_t drv_bo_get_num_planes(struct bo *bo)
533{
534 return bo->num_planes;
535}
536
537union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
538{
539 return bo->handles[plane];
540}
541
542#ifndef DRM_RDWR
543#define DRM_RDWR O_RDWR
544#endif
545
546int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
547{
548
549 int ret, fd;
550 assert(plane < bo->num_planes);
551
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800552 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700553
554 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700555}
556
557uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
558{
559 assert(plane < bo->num_planes);
560 return bo->offsets[plane];
561}
562
563uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
564{
565 assert(plane < bo->num_planes);
566 return bo->sizes[plane];
567}
568
569uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
570{
571 assert(plane < bo->num_planes);
572 return bo->strides[plane];
573}
574
575uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
576{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800577 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700578 return bo->format_modifiers[plane];
579}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700580
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800581uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700582{
583 return bo->format;
584}
585
Gurchetan Singha1892b22017-09-28 16:40:52 -0700586uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700587{
588 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700589 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700590
591 return format;
592}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700593
Gurchetan Singh2a119342016-11-02 10:40:51 -0700594size_t drv_num_planes_from_format(uint32_t format)
595{
596 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800597 case DRM_FORMAT_ABGR1555:
598 case DRM_FORMAT_ABGR2101010:
599 case DRM_FORMAT_ABGR4444:
600 case DRM_FORMAT_ABGR8888:
601 case DRM_FORMAT_ARGB1555:
602 case DRM_FORMAT_ARGB2101010:
603 case DRM_FORMAT_ARGB4444:
604 case DRM_FORMAT_ARGB8888:
605 case DRM_FORMAT_AYUV:
606 case DRM_FORMAT_BGR233:
607 case DRM_FORMAT_BGR565:
608 case DRM_FORMAT_BGR888:
609 case DRM_FORMAT_BGRA1010102:
610 case DRM_FORMAT_BGRA4444:
611 case DRM_FORMAT_BGRA5551:
612 case DRM_FORMAT_BGRA8888:
613 case DRM_FORMAT_BGRX1010102:
614 case DRM_FORMAT_BGRX4444:
615 case DRM_FORMAT_BGRX5551:
616 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800617 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800618 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800619 case DRM_FORMAT_R8:
620 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800621 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800622 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800623 case DRM_FORMAT_RGB888:
624 case DRM_FORMAT_RGBA1010102:
625 case DRM_FORMAT_RGBA4444:
626 case DRM_FORMAT_RGBA5551:
627 case DRM_FORMAT_RGBA8888:
628 case DRM_FORMAT_RGBX1010102:
629 case DRM_FORMAT_RGBX4444:
630 case DRM_FORMAT_RGBX5551:
631 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800632 case DRM_FORMAT_UYVY:
633 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800634 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800635 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800636 case DRM_FORMAT_XBGR4444:
637 case DRM_FORMAT_XBGR8888:
638 case DRM_FORMAT_XRGB1555:
639 case DRM_FORMAT_XRGB2101010:
640 case DRM_FORMAT_XRGB4444:
641 case DRM_FORMAT_XRGB8888:
642 case DRM_FORMAT_YUYV:
643 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700644 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800645 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530646 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700647 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800648 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800649 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700650 return 3;
651 }
652
653 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
654 return 0;
655}
656
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700657uint32_t drv_num_buffers_per_bo(struct bo *bo)
658{
659 uint32_t count = 0;
660 size_t plane, p;
661
662 for (plane = 0; plane < bo->num_planes; plane++) {
663 for (p = 0; p < plane; p++)
664 if (bo->handles[p].u32 == bo->handles[plane].u32)
665 break;
666 if (p == plane)
667 count++;
668 }
669
670 return count;
671}