blob: cb54a93553c487a255e8cb955843c8cbcbf7ac2b [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
135 drv->map_table = drmHashCreate();
136 if (!drv->map_table)
137 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700138
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800139 /* Start with a power of 2 number of allocations. */
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700140 drv->combos.allocations = 2;
141 drv->combos.size = 0;
142
143 drv->combos.data = calloc(drv->combos.allocations, sizeof(struct combination));
144 if (!drv->combos.data)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800145 goto free_map_table;
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) {
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700150 free(drv->combos.data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700151 goto free_map_table;
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
157free_map_table:
158 drmHashDestroy(drv->map_table);
159free_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 Singh1a31e602016-10-06 10:58:00 -0700176 drmHashDestroy(drv->map_table);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700177
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700178 free(drv->combos.data);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700179
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800180 pthread_mutex_unlock(&drv->driver_lock);
181 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700182
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700183 free(drv);
184}
185
186int drv_get_fd(struct driver *drv)
187{
188 return drv->fd;
189}
190
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800191const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700192{
193 return drv->backend->name;
194}
195
Gurchetan Singha1892b22017-09-28 16:40:52 -0700196struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700197{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800198 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700199
Gurchetan Singha1892b22017-09-28 16:40:52 -0700200 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700201 return 0;
202
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800203 best = NULL;
204 uint32_t i;
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700205 for (i = 0; i < drv->combos.size; i++) {
206 curr = &drv->combos.data[i];
Gurchetan Singha1892b22017-09-28 16:40:52 -0700207 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800208 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800209 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700210 }
211
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800212 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700213}
214
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700215struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700216 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700217{
218
219 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800220 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700221
222 if (!bo)
223 return NULL;
224
225 bo->drv = drv;
226 bo->width = width;
227 bo->height = height;
228 bo->format = format;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700229 bo->use_flags = use_flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700230 bo->num_planes = drv_num_planes_from_format(format);
231
232 if (!bo->num_planes) {
233 free(bo);
234 return NULL;
235 }
236
237 return bo;
238}
239
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800240struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700241 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700242{
243 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700244 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700245 struct bo *bo;
246
Gurchetan Singha1892b22017-09-28 16:40:52 -0700247 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700248
249 if (!bo)
250 return NULL;
251
Gurchetan Singha1892b22017-09-28 16:40:52 -0700252 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700253
254 if (ret) {
255 free(bo);
256 return NULL;
257 }
258
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800259 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700260
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700261 for (plane = 0; plane < bo->num_planes; plane++) {
262 if (plane > 0)
263 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
264
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700265 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700266 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700267
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800268 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700269
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700270 return bo;
271}
272
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800273struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
274 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700275{
276 int ret;
277 size_t plane;
278 struct bo *bo;
279
280 if (!drv->backend->bo_create_with_modifiers) {
281 errno = ENOENT;
282 return NULL;
283 }
284
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700285 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700286
287 if (!bo)
288 return NULL;
289
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800290 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700291
292 if (ret) {
293 free(bo);
294 return NULL;
295 }
296
297 pthread_mutex_lock(&drv->driver_lock);
298
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700299 for (plane = 0; plane < bo->num_planes; plane++) {
300 if (plane > 0)
301 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
302
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700303 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700304 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700305
306 pthread_mutex_unlock(&drv->driver_lock);
307
308 return bo;
309}
310
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700311void drv_bo_destroy(struct bo *bo)
312{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700313 size_t plane;
314 uintptr_t total = 0;
315 struct driver *drv = bo->drv;
316
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800317 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700318
319 for (plane = 0; plane < bo->num_planes; plane++)
320 drv_decrement_reference_count(drv, bo, plane);
321
Gurchetan Singhde263f12017-01-24 13:30:06 -0800322 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700323 total += drv_get_reference_count(drv, bo, plane);
324
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800325 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700326
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900327 if (total == 0) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700328 assert(drv_mapping_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700329 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900330 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700331
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700332 free(bo);
333}
334
335struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
336{
337 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700338 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700339 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700340 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700341
Gurchetan Singha1892b22017-09-28 16:40:52 -0700342 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700343
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700344 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700345 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700346
Gurchetan Singh71611d62017-01-03 16:49:56 -0800347 ret = drv->backend->bo_import(bo, data);
348 if (ret) {
349 free(bo);
350 return NULL;
351 }
352
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700353 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700354 bo->strides[plane] = data->strides[plane];
355 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700356 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700357
358 seek_end = lseek(data->fds[plane], 0, SEEK_END);
359 if (seek_end == (off_t)(-1)) {
360 fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
361 goto destroy_bo;
362 }
363
364 lseek(data->fds[plane], 0, SEEK_SET);
365 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
366 bo->sizes[plane] = seek_end - data->offsets[plane];
367 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800368 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700369
370 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
371 fprintf(stderr, "drv: buffer size is too large.\n");
372 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800373 }
374
375 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700376 }
377
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700378 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700379
380destroy_bo:
381 drv_bo_destroy(bo);
382 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700383}
384
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800385void *drv_bo_map(struct bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700386 uint32_t map_flags, struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700387{
388 void *ptr;
389 uint8_t *addr;
390 size_t offset;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700391 struct mapping *mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700392
393 assert(width > 0);
394 assert(height > 0);
395 assert(x + width <= drv_bo_get_width(bo));
396 assert(y + 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 Singh27dd4702016-11-23 17:27:52 -0800401 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700402
403 if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700404 mapping = (struct mapping *)ptr;
Gurchetan Singhe29a6402017-10-05 14:52:24 -0700405 /* TODO(gsingh): support mapping same buffer with different flags. */
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700406 assert(mapping->vma->map_flags == map_flags);
407 mapping->vma->refcount++;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700408 goto success;
409 }
410
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700411 mapping = calloc(1, sizeof(*mapping));
412 mapping->vma = calloc(1, sizeof(*mapping->vma));
413 addr = bo->drv->backend->bo_map(bo, mapping, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700414 if (addr == MAP_FAILED) {
415 *map_data = NULL;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700416 free(mapping->vma);
417 free(mapping);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800418 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700419 return MAP_FAILED;
420 }
421
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700422 mapping->vma->refcount = 1;
423 mapping->vma->addr = addr;
424 mapping->vma->handle = bo->handles[plane].u32;
425 mapping->vma->map_flags = map_flags;
426 drmHashInsert(bo->drv->map_table, bo->handles[plane].u32, (void *)mapping);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700427
428success:
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700429 drv_bo_invalidate(bo, mapping);
430 *map_data = mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700431 offset = drv_bo_get_plane_stride(bo, plane) * y;
432 offset += drv_stride_from_format(bo->format, x, plane);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700433 addr = (uint8_t *)mapping->vma->addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700434 addr += drv_bo_get_plane_offset(bo, plane) + offset;
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800435 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700436
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800437 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700438}
439
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700440int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700441{
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700442 int ret = drv_bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700443 if (ret)
444 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700445
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800446 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700447
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700448 if (!--mapping->vma->refcount) {
449 ret = bo->drv->backend->bo_unmap(bo, mapping);
450 drmHashDelete(bo->drv->map_table, mapping->vma->handle);
451 free(mapping->vma);
452 free(mapping);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700453 }
454
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800455 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700456
457 return ret;
458}
459
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700460int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700461{
462 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700463
464 assert(mapping);
465 assert(mapping->vma);
466 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700467
468 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700469 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700470
471 return ret;
472}
473
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700474int drv_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700475{
476 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700477
478 assert(mapping);
479 assert(mapping->vma);
480 assert(mapping->vma->refcount > 0);
Tomasz Figae0807b12017-08-04 12:50:03 +0900481 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700482
483 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700484 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700485
486 return ret;
487}
488
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700489uint32_t drv_bo_get_width(struct bo *bo)
490{
491 return bo->width;
492}
493
494uint32_t drv_bo_get_height(struct bo *bo)
495{
496 return bo->height;
497}
498
499uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
500{
501 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
502}
503
504size_t drv_bo_get_num_planes(struct bo *bo)
505{
506 return bo->num_planes;
507}
508
509union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
510{
511 return bo->handles[plane];
512}
513
514#ifndef DRM_RDWR
515#define DRM_RDWR O_RDWR
516#endif
517
518int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
519{
520
521 int ret, fd;
522 assert(plane < bo->num_planes);
523
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800524 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700525
526 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700527}
528
529uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
530{
531 assert(plane < bo->num_planes);
532 return bo->offsets[plane];
533}
534
535uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
536{
537 assert(plane < bo->num_planes);
538 return bo->sizes[plane];
539}
540
541uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
542{
543 assert(plane < bo->num_planes);
544 return bo->strides[plane];
545}
546
547uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
548{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800549 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700550 return bo->format_modifiers[plane];
551}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700552
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800553uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700554{
555 return bo->format;
556}
557
Gurchetan Singha1892b22017-09-28 16:40:52 -0700558uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700559{
560 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700561 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700562
563 return format;
564}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700565
Gurchetan Singh2a119342016-11-02 10:40:51 -0700566size_t drv_num_planes_from_format(uint32_t format)
567{
568 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800569 case DRM_FORMAT_ABGR1555:
570 case DRM_FORMAT_ABGR2101010:
571 case DRM_FORMAT_ABGR4444:
572 case DRM_FORMAT_ABGR8888:
573 case DRM_FORMAT_ARGB1555:
574 case DRM_FORMAT_ARGB2101010:
575 case DRM_FORMAT_ARGB4444:
576 case DRM_FORMAT_ARGB8888:
577 case DRM_FORMAT_AYUV:
578 case DRM_FORMAT_BGR233:
579 case DRM_FORMAT_BGR565:
580 case DRM_FORMAT_BGR888:
581 case DRM_FORMAT_BGRA1010102:
582 case DRM_FORMAT_BGRA4444:
583 case DRM_FORMAT_BGRA5551:
584 case DRM_FORMAT_BGRA8888:
585 case DRM_FORMAT_BGRX1010102:
586 case DRM_FORMAT_BGRX4444:
587 case DRM_FORMAT_BGRX5551:
588 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800589 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800590 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800591 case DRM_FORMAT_R8:
592 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800593 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800594 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800595 case DRM_FORMAT_RGB888:
596 case DRM_FORMAT_RGBA1010102:
597 case DRM_FORMAT_RGBA4444:
598 case DRM_FORMAT_RGBA5551:
599 case DRM_FORMAT_RGBA8888:
600 case DRM_FORMAT_RGBX1010102:
601 case DRM_FORMAT_RGBX4444:
602 case DRM_FORMAT_RGBX5551:
603 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800604 case DRM_FORMAT_UYVY:
605 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800606 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800607 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800608 case DRM_FORMAT_XBGR4444:
609 case DRM_FORMAT_XBGR8888:
610 case DRM_FORMAT_XRGB1555:
611 case DRM_FORMAT_XRGB2101010:
612 case DRM_FORMAT_XRGB4444:
613 case DRM_FORMAT_XRGB8888:
614 case DRM_FORMAT_YUYV:
615 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700616 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800617 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530618 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700619 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800620 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800621 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700622 return 3;
623 }
624
625 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
626 return 0;
627}
628
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700629uint32_t drv_num_buffers_per_bo(struct bo *bo)
630{
631 uint32_t count = 0;
632 size_t plane, p;
633
634 for (plane = 0; plane < bo->num_planes; plane++) {
635 for (p = 0; p < plane; p++)
636 if (bo->handles[p].u32 == bo->handles[plane].u32)
637 break;
638 if (p == plane)
639 count++;
640 }
641
642 return count;
643}