1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2017, 2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
6 * Copyright (c) 2018 DisplayLink (UK) Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include "config.h"
31
32 #include <stdint.h>
33
34 #include <xf86drm.h>
35 #include <xf86drmMode.h>
36 #include <drm_fourcc.h>
37
38 #include <libweston/libweston.h>
39 #include <libweston/backend-drm.h>
40 #include <libweston/pixel-formats.h>
41 #include <libweston/linux-dmabuf.h>
42 #include "shared/helpers.h"
43 #include "drm-internal.h"
44 #include "linux-dmabuf.h"
45
46 static void
drm_fb_destroy(struct drm_fb * fb)47 drm_fb_destroy(struct drm_fb *fb)
48 {
49 if (fb->fb_id != 0)
50 drmModeRmFB(fb->fd, fb->fb_id);
51 weston_buffer_reference(&fb->buffer_ref, NULL);
52 weston_buffer_release_reference(&fb->buffer_release_ref, NULL);
53 free(fb);
54 }
55
56 static void
drm_fb_destroy_dumb(struct drm_fb * fb)57 drm_fb_destroy_dumb(struct drm_fb *fb)
58 {
59 struct drm_mode_destroy_dumb destroy_arg;
60
61 assert(fb->type == BUFFER_PIXMAN_DUMB);
62
63 if (fb->map && fb->size > 0)
64 munmap(fb->map, fb->size);
65
66 memset(&destroy_arg, 0, sizeof(destroy_arg));
67 destroy_arg.handle = fb->handles[0];
68 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
69
70 drm_fb_destroy(fb);
71 }
72
73 static int
drm_fb_addfb(struct drm_backend * b,struct drm_fb * fb)74 drm_fb_addfb(struct drm_backend *b, struct drm_fb *fb)
75 {
76 int ret = -EINVAL;
77 uint64_t mods[4] = { };
78 size_t i;
79
80 /* If we have a modifier set, we must only use the WithModifiers
81 * entrypoint; we cannot import it through legacy ioctls. */
82 if (b->fb_modifiers && fb->modifier != DRM_FORMAT_MOD_INVALID) {
83 /* KMS demands that if a modifier is set, it must be the same
84 * for all planes. */
85 for (i = 0; i < ARRAY_LENGTH(mods) && fb->handles[i]; i++)
86 mods[i] = fb->modifier;
87 ret = drmModeAddFB2WithModifiers(fb->fd, fb->width, fb->height,
88 fb->format->format,
89 fb->handles, fb->strides,
90 fb->offsets, mods, &fb->fb_id,
91 DRM_MODE_FB_MODIFIERS);
92 return ret;
93 }
94
95 ret = drmModeAddFB2(fb->fd, fb->width, fb->height, fb->format->format,
96 fb->handles, fb->strides, fb->offsets, &fb->fb_id,
97 0);
98 if (ret == 0)
99 return 0;
100
101 /* Legacy AddFB can't always infer the format from depth/bpp alone, so
102 * check if our format is one of the lucky ones. */
103 if (!fb->format->depth || !fb->format->bpp)
104 return ret;
105
106 /* Cannot fall back to AddFB for multi-planar formats either. */
107 if (fb->handles[1] || fb->handles[2] || fb->handles[3])
108 return ret;
109
110 ret = drmModeAddFB(fb->fd, fb->width, fb->height,
111 fb->format->depth, fb->format->bpp,
112 fb->strides[0], fb->handles[0], &fb->fb_id);
113 return ret;
114 }
115
116 struct drm_fb *
drm_fb_create_dumb(struct drm_backend * b,int width,int height,uint32_t format)117 drm_fb_create_dumb(struct drm_backend *b, int width, int height,
118 uint32_t format)
119 {
120 struct drm_fb *fb;
121 int ret;
122
123 struct drm_mode_create_dumb create_arg;
124 struct drm_mode_destroy_dumb destroy_arg;
125 struct drm_mode_map_dumb map_arg;
126
127 fb = zalloc(sizeof *fb);
128 if (!fb)
129 return NULL;
130 fb->refcnt = 1;
131
132 fb->format = pixel_format_get_info(format);
133 if (!fb->format) {
134 weston_log("failed to look up format 0x%lx\n",
135 (unsigned long) format);
136 goto err_fb;
137 }
138
139 if (!fb->format->depth || !fb->format->bpp) {
140 weston_log("format 0x%lx is not compatible with dumb buffers\n",
141 (unsigned long) format);
142 goto err_fb;
143 }
144
145 memset(&create_arg, 0, sizeof create_arg);
146 create_arg.bpp = fb->format->bpp;
147 create_arg.width = width;
148 create_arg.height = height;
149
150 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
151 if (ret)
152 goto err_fb;
153
154 fb->type = BUFFER_PIXMAN_DUMB;
155 fb->modifier = DRM_FORMAT_MOD_INVALID;
156 fb->handles[0] = create_arg.handle;
157 fb->strides[0] = create_arg.pitch;
158 fb->num_planes = 1;
159 fb->size = create_arg.size;
160 fb->width = width;
161 fb->height = height;
162 fb->fd = b->drm.fd;
163
164 if (drm_fb_addfb(b, fb) != 0) {
165 weston_log("failed to create kms fb: %s\n", strerror(errno));
166 goto err_bo;
167 }
168
169 memset(&map_arg, 0, sizeof map_arg);
170 map_arg.handle = fb->handles[0];
171 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
172 if (ret)
173 goto err_add_fb;
174
175 if (b->use_tde) {
176 fb->map = mmap(NULL, fb->size, PROT_READ | PROT_WRITE,
177 MAP_SHARED, b->drm.fd, map_arg.offset);
178 weston_log("use mmap");
179 } else {
180 fb->map = mmap64(NULL, fb->size, PROT_READ | PROT_WRITE,
181 MAP_SHARED, b->drm.fd, map_arg.offset);
182 weston_log("use mmap64");
183 }
184
185 if (fb->map == MAP_FAILED)
186 goto err_add_fb;
187
188 return fb;
189
190 err_add_fb:
191 drmModeRmFB(b->drm.fd, fb->fb_id);
192 err_bo:
193 memset(&destroy_arg, 0, sizeof(destroy_arg));
194 destroy_arg.handle = create_arg.handle;
195 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
196 err_fb:
197 free(fb);
198 return NULL;
199 }
200
201 struct drm_fb *
drm_fb_ref(struct drm_fb * fb)202 drm_fb_ref(struct drm_fb *fb)
203 {
204 fb->refcnt++;
205 return fb;
206 }
207
208 #ifdef BUILD_DRM_GBM
209 static void
drm_fb_destroy_gbm(struct gbm_bo * bo,void * data)210 drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
211 {
212 struct drm_fb *fb = data;
213
214 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
215 fb->type == BUFFER_CURSOR);
216 drm_fb_destroy(fb);
217 }
218
219 static void
drm_fb_destroy_dmabuf(struct drm_fb * fb)220 drm_fb_destroy_dmabuf(struct drm_fb *fb)
221 {
222 /* We deliberately do not close the GEM handles here; GBM manages
223 * their lifetime through the BO. */
224 if (fb->bo)
225 gbm_bo_destroy(fb->bo);
226 drm_fb_destroy(fb);
227 }
228
229 static struct drm_fb *
drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer * dmabuf,struct drm_backend * backend,bool is_opaque)230 drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
231 struct drm_backend *backend, bool is_opaque)
232 {
233 struct drm_fb *fb;
234 struct gbm_import_fd_data import_legacy = {
235 .width = dmabuf->attributes.width,
236 .height = dmabuf->attributes.height,
237 .format = dmabuf->attributes.format,
238 .stride = dmabuf->attributes.stride[0],
239 .fd = dmabuf->attributes.fd[0],
240 };
241 #ifdef HAVE_GBM_FD_IMPORT
242 struct gbm_import_fd_modifier_data import_mod = {
243 .width = dmabuf->attributes.width,
244 .height = dmabuf->attributes.height,
245 .format = dmabuf->attributes.format,
246 .num_fds = dmabuf->attributes.n_planes,
247 .modifier = dmabuf->attributes.modifier[0],
248 };
249 #endif /* HAVE_GBM_FD_IMPORT */
250
251 int i;
252
253 /* XXX: TODO:
254 *
255 * Currently the buffer is rejected if any dmabuf attribute
256 * flag is set. This keeps us from passing an inverted /
257 * interlaced / bottom-first buffer (or any other type that may
258 * be added in the future) through to an overlay. Ultimately,
259 * these types of buffers should be handled through buffer
260 * transforms and not as spot-checks requiring specific
261 * knowledge. */
262 if (dmabuf->attributes.flags)
263 return NULL;
264
265 fb = zalloc(sizeof *fb);
266 if (fb == NULL)
267 return NULL;
268
269 fb->refcnt = 1;
270 fb->type = BUFFER_DMABUF;
271
272 #ifdef HAVE_GBM_FD_IMPORT
273 static_assert(ARRAY_LENGTH(import_mod.fds) ==
274 ARRAY_LENGTH(dmabuf->attributes.fd),
275 "GBM and linux_dmabuf FD size must match");
276 static_assert(sizeof(import_mod.fds) == sizeof(dmabuf->attributes.fd),
277 "GBM and linux_dmabuf FD size must match");
278 memcpy(import_mod.fds, dmabuf->attributes.fd, sizeof(import_mod.fds));
279
280 static_assert(ARRAY_LENGTH(import_mod.strides) ==
281 ARRAY_LENGTH(dmabuf->attributes.stride),
282 "GBM and linux_dmabuf stride size must match");
283 static_assert(sizeof(import_mod.strides) ==
284 sizeof(dmabuf->attributes.stride),
285 "GBM and linux_dmabuf stride size must match");
286 memcpy(import_mod.strides, dmabuf->attributes.stride,
287 sizeof(import_mod.strides));
288
289 static_assert(ARRAY_LENGTH(import_mod.offsets) ==
290 ARRAY_LENGTH(dmabuf->attributes.offset),
291 "GBM and linux_dmabuf offset size must match");
292 static_assert(sizeof(import_mod.offsets) ==
293 sizeof(dmabuf->attributes.offset),
294 "GBM and linux_dmabuf offset size must match");
295 memcpy(import_mod.offsets, dmabuf->attributes.offset,
296 sizeof(import_mod.offsets));
297 #endif /* NOT HAVE_GBM_FD_IMPORT */
298
299 /* The legacy FD-import path does not allow us to supply modifiers,
300 * multiple planes, or buffer offsets. */
301 if (dmabuf->attributes.modifier[0] != DRM_FORMAT_MOD_INVALID ||
302 dmabuf->attributes.n_planes > 1 ||
303 dmabuf->attributes.offset[0] > 0) {
304 #ifdef HAVE_GBM_FD_IMPORT
305 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER,
306 &import_mod,
307 GBM_BO_USE_SCANOUT);
308 #else /* NOT HAVE_GBM_FD_IMPORT */
309 drm_debug(backend, "\t\t\t[dmabuf] Unsupported use of modifiers.\n");
310 goto err_free;
311 #endif /* NOT HAVE_GBM_FD_IMPORT */
312 } else {
313 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD,
314 &import_legacy,
315 GBM_BO_USE_SCANOUT);
316 }
317
318 if (!fb->bo)
319 goto err_free;
320
321 fb->width = dmabuf->attributes.width;
322 fb->height = dmabuf->attributes.height;
323 fb->modifier = dmabuf->attributes.modifier[0];
324 fb->size = 0;
325 fb->fd = backend->drm.fd;
326
327 static_assert(ARRAY_LENGTH(fb->strides) ==
328 ARRAY_LENGTH(dmabuf->attributes.stride),
329 "drm_fb and dmabuf stride size must match");
330 static_assert(sizeof(fb->strides) == sizeof(dmabuf->attributes.stride),
331 "drm_fb and dmabuf stride size must match");
332 memcpy(fb->strides, dmabuf->attributes.stride, sizeof(fb->strides));
333 static_assert(ARRAY_LENGTH(fb->offsets) ==
334 ARRAY_LENGTH(dmabuf->attributes.offset),
335 "drm_fb and dmabuf offset size must match");
336 static_assert(sizeof(fb->offsets) == sizeof(dmabuf->attributes.offset),
337 "drm_fb and dmabuf offset size must match");
338 memcpy(fb->offsets, dmabuf->attributes.offset, sizeof(fb->offsets));
339
340 fb->format = pixel_format_get_info(dmabuf->attributes.format);
341 if (!fb->format) {
342 weston_log("couldn't look up format info for 0x%lx\n",
343 (unsigned long) dmabuf->attributes.format);
344 goto err_free;
345 }
346
347 if (is_opaque)
348 fb->format = pixel_format_get_opaque_substitute(fb->format);
349
350 if (backend->min_width > fb->width ||
351 fb->width > backend->max_width ||
352 backend->min_height > fb->height ||
353 fb->height > backend->max_height) {
354 weston_log("bo geometry out of bounds\n");
355 goto err_free;
356 }
357
358 #ifdef HAVE_GBM_MODIFIERS
359 fb->num_planes = dmabuf->attributes.n_planes;
360 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
361 union gbm_bo_handle handle;
362
363 handle = gbm_bo_get_handle_for_plane(fb->bo, i);
364 if (handle.s32 == -1)
365 goto err_free;
366 fb->handles[i] = handle.u32;
367 }
368 #else /* NOT HAVE_GBM_MODIFIERS */
369 {
370 union gbm_bo_handle handle;
371
372 fb->num_planes = 1;
373
374 handle = gbm_bo_get_handle(fb->bo);
375
376 if (handle.s32 == -1)
377 goto err_free;
378 fb->handles[0] = handle.u32;
379 }
380 #endif /* NOT HAVE_GBM_MODIFIERS */
381
382
383 if (drm_fb_addfb(backend, fb) != 0)
384 goto err_free;
385
386 return fb;
387
388 err_free:
389 drm_fb_destroy_dmabuf(fb);
390 return NULL;
391 }
392
393 struct drm_fb *
drm_fb_get_from_bo(struct gbm_bo * bo,struct drm_backend * backend,bool is_opaque,enum drm_fb_type type)394 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
395 bool is_opaque, enum drm_fb_type type)
396 {
397 struct drm_fb *fb = gbm_bo_get_user_data(bo);
398 #ifdef HAVE_GBM_MODIFIERS
399 int i;
400 #endif
401
402 if (fb) {
403 assert(fb->type == type);
404 return drm_fb_ref(fb);
405 }
406
407 fb = zalloc(sizeof *fb);
408 if (fb == NULL)
409 return NULL;
410
411 fb->type = type;
412 fb->refcnt = 1;
413 fb->bo = bo;
414 fb->fd = backend->drm.fd;
415
416 fb->width = gbm_bo_get_width(bo);
417 fb->height = gbm_bo_get_height(bo);
418 fb->format = pixel_format_get_info(gbm_bo_get_format(bo));
419 fb->size = 0;
420
421 #ifdef HAVE_GBM_MODIFIERS
422 fb->modifier = gbm_bo_get_modifier(bo);
423 fb->num_planes = gbm_bo_get_plane_count(bo);
424 for (i = 0; i < fb->num_planes; i++) {
425 fb->strides[i] = gbm_bo_get_stride_for_plane(bo, i);
426 fb->handles[i] = gbm_bo_get_handle_for_plane(bo, i).u32;
427 fb->offsets[i] = gbm_bo_get_offset(bo, i);
428 }
429 #else
430 fb->num_planes = 1;
431 fb->strides[0] = gbm_bo_get_stride(bo);
432 fb->handles[0] = gbm_bo_get_handle(bo).u32;
433 fb->modifier = DRM_FORMAT_MOD_INVALID;
434 #endif
435
436 if (!fb->format) {
437 weston_log("couldn't look up format 0x%lx\n",
438 (unsigned long) gbm_bo_get_format(bo));
439 goto err_free;
440 }
441
442 /* We can scanout an ARGB buffer if the surface's opaque region covers
443 * the whole output, but we have to use XRGB as the KMS format code. */
444 if (is_opaque)
445 fb->format = pixel_format_get_opaque_substitute(fb->format);
446
447 if (backend->min_width > fb->width ||
448 fb->width > backend->max_width ||
449 backend->min_height > fb->height ||
450 fb->height > backend->max_height) {
451 weston_log("bo geometry out of bounds\n");
452 goto err_free;
453 }
454
455 if (drm_fb_addfb(backend, fb) != 0) {
456 if (type == BUFFER_GBM_SURFACE)
457 weston_log("failed to create kms fb: %s\n",
458 strerror(errno));
459 goto err_free;
460 }
461
462 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
463
464 return fb;
465
466 err_free:
467 free(fb);
468 return NULL;
469 }
470
471 static void
drm_fb_set_buffer(struct drm_fb * fb,struct weston_buffer * buffer,struct weston_buffer_release * buffer_release)472 drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer,
473 struct weston_buffer_release *buffer_release)
474 {
475 assert(fb->buffer_ref.buffer == NULL);
476 assert(fb->type == BUFFER_CLIENT || fb->type == BUFFER_DMABUF);
477 weston_buffer_reference(&fb->buffer_ref, buffer);
478 weston_buffer_release_reference(&fb->buffer_release_ref,
479 buffer_release);
480 }
481 #endif
482
483 void
drm_fb_unref(struct drm_fb * fb)484 drm_fb_unref(struct drm_fb *fb)
485 {
486 if (!fb)
487 return;
488
489 assert(fb->refcnt > 0);
490 if (--fb->refcnt > 0)
491 return;
492
493 switch (fb->type) {
494 case BUFFER_PIXMAN_DUMB:
495 drm_fb_destroy_dumb(fb);
496 break;
497 #ifdef BUILD_DRM_GBM
498 case BUFFER_CURSOR:
499 case BUFFER_CLIENT:
500 gbm_bo_destroy(fb->bo);
501 break;
502 case BUFFER_GBM_SURFACE:
503 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
504 break;
505 case BUFFER_DMABUF:
506 drm_fb_destroy_dmabuf(fb);
507 break;
508 #endif
509 default:
510 assert(NULL);
511 break;
512 }
513 }
514
515 #ifdef BUILD_DRM_GBM
516 bool
drm_can_scanout_dmabuf(struct weston_compositor * ec,struct linux_dmabuf_buffer * dmabuf)517 drm_can_scanout_dmabuf(struct weston_compositor *ec,
518 struct linux_dmabuf_buffer *dmabuf)
519 {
520 struct drm_fb *fb;
521 struct drm_backend *b = to_drm_backend(ec);
522 bool ret = false;
523
524 fb = drm_fb_get_from_dmabuf(dmabuf, b, true);
525 if (fb)
526 ret = true;
527
528 drm_fb_unref(fb);
529 drm_debug(b, "[dmabuf] dmabuf %p, import test %s\n", dmabuf,
530 ret ? "succeeded" : "failed");
531 return ret;
532 }
533
534 struct drm_fb *
drm_fb_get_from_view(struct drm_output_state * state,struct weston_view * ev)535 drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev)
536 {
537 struct drm_output *output = state->output;
538 struct drm_backend *b = to_drm_backend(output->base.compositor);
539 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
540 bool is_opaque = weston_view_is_opaque(ev, &ev->transform.boundingbox);
541 struct linux_dmabuf_buffer *dmabuf;
542 struct drm_fb *fb;
543
544 if (ev->alpha != 1.0f)
545 return NULL;
546
547 if (!drm_view_transform_supported(ev, &output->base))
548 return NULL;
549
550 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
551 ev->surface->desired_protection > output->base.current_protection)
552 return NULL;
553
554 if (!buffer)
555 return NULL;
556
557 if (wl_shm_buffer_get(buffer->resource))
558 return NULL;
559
560 /* GBM is used for dmabuf import as well as from client wl_buffer. */
561 if (!b->gbm)
562 return NULL;
563
564 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
565 if (dmabuf) {
566 fb = drm_fb_get_from_dmabuf(dmabuf, b, is_opaque);
567 if (!fb)
568 return NULL;
569 } else {
570 struct gbm_bo *bo;
571
572 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
573 buffer->resource, GBM_BO_USE_SCANOUT);
574 if (!bo)
575 return NULL;
576
577 fb = drm_fb_get_from_bo(bo, b, is_opaque, BUFFER_CLIENT);
578 if (!fb) {
579 gbm_bo_destroy(bo);
580 return NULL;
581 }
582 }
583
584 drm_debug(b, "\t\t\t[view] view %p format: %s\n",
585 ev, fb->format->drm_format_name);
586 drm_fb_set_buffer(fb, buffer,
587 ev->surface->buffer_release_ref.buffer_release);
588 return fb;
589 }
590 #endif
591