blob: f6fd0ec7ec04e66abcd6054cf5f8db4cf0e320bf [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Yuly Novikov96c7a3b2015-12-08 22:48:29 -05007#include <assert.h>
8#include <stdbool.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -07009#include <stdio.h>
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050010#include <stdlib.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070011#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070012#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070013#include <xf86drm.h>
Gurchetan Singh179687e2016-10-28 10:07:35 -070014#include <xf86drmMode.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070015
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include "drv_priv.h"
Lauri Peltonen904a8792015-01-17 13:57:51 +020017#include "helpers.h"
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050018#include "util.h"
19
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070020int drv_bpp_from_format(uint32_t format, size_t plane)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070021{
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -070022 assert(plane < drv_num_planes_from_format(format));
23
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070024 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080025 case DRM_FORMAT_BGR233:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080026 case DRM_FORMAT_C8:
27 case DRM_FORMAT_R8:
28 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080029 case DRM_FORMAT_YVU420:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070030 return 8;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070031
Gurchetan Singh2a119342016-11-02 10:40:51 -070032 /*
33 * NV12 is laid out as follows. Each letter represents a byte.
34 * Y plane:
35 * Y0_0, Y0_1, Y0_2, Y0_3, ..., Y0_N
36 * Y1_0, Y1_1, Y1_2, Y1_3, ..., Y1_N
37 * ...
38 * YM_0, YM_1, YM_2, YM_3, ..., YM_N
39 * CbCr plane:
40 * Cb01_01, Cr01_01, Cb01_23, Cr01_23, ..., Cb01_(N-1)N, Cr01_(N-1)N
41 * Cb23_01, Cr23_01, Cb23_23, Cr23_23, ..., Cb23_(N-1)N, Cr23_(N-1)N
42 * ...
43 * Cb(M-1)M_01, Cr(M-1)M_01, ..., Cb(M-1)M_(N-1)N, Cr(M-1)M_(N-1)N
44 *
45 * Pixel (0, 0) requires Y0_0, Cb01_01 and Cr01_01. Pixel (0, 1) requires
46 * Y0_1, Cb01_01 and Cr01_01. So for a single pixel, 2 bytes of luma data
47 * are required.
48 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080049 case DRM_FORMAT_NV12:
Gurchetan Singh2a119342016-11-02 10:40:51 -070050 return (plane == 0) ? 8 : 16;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -050051
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080052 case DRM_FORMAT_ABGR1555:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080053 case DRM_FORMAT_ABGR4444:
54 case DRM_FORMAT_ARGB1555:
55 case DRM_FORMAT_ARGB4444:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080056 case DRM_FORMAT_BGR565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080057 case DRM_FORMAT_BGRA4444:
58 case DRM_FORMAT_BGRA5551:
59 case DRM_FORMAT_BGRX4444:
60 case DRM_FORMAT_BGRX5551:
61 case DRM_FORMAT_GR88:
62 case DRM_FORMAT_RG88:
63 case DRM_FORMAT_RGB565:
64 case DRM_FORMAT_RGBA4444:
65 case DRM_FORMAT_RGBA5551:
66 case DRM_FORMAT_RGBX4444:
67 case DRM_FORMAT_RGBX5551:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080068 case DRM_FORMAT_UYVY:
69 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080070 case DRM_FORMAT_XBGR1555:
71 case DRM_FORMAT_XBGR4444:
72 case DRM_FORMAT_XRGB1555:
73 case DRM_FORMAT_XRGB4444:
74 case DRM_FORMAT_YUYV:
75 case DRM_FORMAT_YVYU:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070076 return 16;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070077
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080078 case DRM_FORMAT_BGR888:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080079 case DRM_FORMAT_RGB888:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070080 return 24;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070081
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080082 case DRM_FORMAT_ABGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080083 case DRM_FORMAT_ABGR8888:
84 case DRM_FORMAT_ARGB2101010:
85 case DRM_FORMAT_ARGB8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -080086 case DRM_FORMAT_AYUV:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -080087 case DRM_FORMAT_BGRA1010102:
88 case DRM_FORMAT_BGRA8888:
89 case DRM_FORMAT_BGRX1010102:
90 case DRM_FORMAT_BGRX8888:
91 case DRM_FORMAT_RGBA1010102:
92 case DRM_FORMAT_RGBA8888:
93 case DRM_FORMAT_RGBX1010102:
94 case DRM_FORMAT_RGBX8888:
95 case DRM_FORMAT_XBGR2101010:
96 case DRM_FORMAT_XBGR8888:
97 case DRM_FORMAT_XRGB2101010:
98 case DRM_FORMAT_XRGB8888:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -070099 return 32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700100 }
101
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700102 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700103 return 0;
104}
105
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700106/*
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700107 * This function fills in the buffer object given driver aligned dimensions
108 * and a format. This function assumes there is just one kernel buffer per
109 * buffer object.
110 */
111int drv_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800112 uint32_t format)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700113{
114
Gurchetan Singh2a119342016-11-02 10:40:51 -0700115 size_t p, num_planes;
116 uint32_t offset = 0;
117
118 num_planes = drv_num_planes_from_format(format);
119 assert(num_planes);
120
121 for (p = 0; p < num_planes; p++) {
122 bo->strides[p] = drv_stride_from_format(format, width, p);
123 bo->sizes[p] = drv_size_from_format(format, bo->strides[p],
124 height, p);
125 bo->offsets[p] = offset;
126 offset += bo->sizes[p];
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700127 }
128
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700129 bo->total_size = bo->offsets[bo->num_planes - 1] +
130 bo->sizes[bo->num_planes - 1];
131
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700132 return 0;
133}
134
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700135int drv_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800136 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700137{
138 struct drm_mode_create_dumb create_dumb;
139 int ret;
140
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500141 /* Only single-plane formats are supported */
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700142 assert(drv_num_planes_from_format(format) == 1);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500143
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700144 memset(&create_dumb, 0, sizeof(create_dumb));
145 create_dumb.height = height;
146 create_dumb.width = width;
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700147 create_dumb.bpp = drv_bpp_from_format(format, 0);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700148 create_dumb.flags = 0;
149
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700150 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700151 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700152 fprintf(stderr, "drv: DRM_IOCTL_MODE_CREATE_DUMB failed\n");
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700153 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700154 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700155
Gurchetan Singhbc17b1d2016-12-06 15:43:17 -0800156 bo->width = width;
157 bo->height = height;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500158 bo->handles[0].u32 = create_dumb.handle;
159 bo->offsets[0] = 0;
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700160 bo->total_size = bo->sizes[0] = create_dumb.size;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500161 bo->strides[0] = create_dumb.pitch;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700162
163 return 0;
164}
165
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700166int drv_dumb_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700167{
168 struct drm_mode_destroy_dumb destroy_dumb;
169 int ret;
170
171 memset(&destroy_dumb, 0, sizeof(destroy_dumb));
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500172 destroy_dumb.handle = bo->handles[0].u32;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700173
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700174 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700175 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700176 fprintf(stderr, "drv: DRM_IOCTL_MODE_DESTROY_DUMB failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500177 "(handle=%x)\n", bo->handles[0].u32);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700178 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700179 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700180
181 return 0;
182}
183
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700184int drv_gem_bo_destroy(struct bo *bo)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700185{
186 struct drm_gem_close gem_close;
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500187 int ret, error = 0;
188 size_t plane, i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700189
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500190 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700191 for (i = 0; i < plane; i++)
192 if (bo->handles[i].u32 == bo->handles[plane].u32)
193 break;
194 /* Make sure close hasn't already been called on this handle */
195 if (i != plane)
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500196 continue;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700197
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500198 memset(&gem_close, 0, sizeof(gem_close));
199 gem_close.handle = bo->handles[plane].u32;
200
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700201 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500202 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700203 fprintf(stderr, "drv: DRM_IOCTL_GEM_CLOSE failed "
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500204 "(handle=%x) error %d\n",
205 bo->handles[plane].u32, ret);
206 error = ret;
207 }
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700208 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700209
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500210 return error;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700211}
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700212
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700213void *drv_dumb_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700214{
215 int ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700216 size_t i;
Gurchetan Singhef920532016-08-12 16:38:25 -0700217 struct drm_mode_map_dumb map_dumb;
218
219 memset(&map_dumb, 0, sizeof(map_dumb));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700220 map_dumb.handle = bo->handles[plane].u32;
Gurchetan Singhef920532016-08-12 16:38:25 -0700221
222 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
223 if (ret) {
224 fprintf(stderr, "drv: DRM_IOCTL_MODE_MAP_DUMB failed \n");
225 return MAP_FAILED;
226 }
227
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700228 for (i = 0; i < bo->num_planes; i++)
229 if (bo->handles[i].u32 == bo->handles[plane].u32)
230 data->length += bo->sizes[i];
231
232 return mmap(0, data->length, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -0700233 bo->drv->fd, map_dumb.offset);
234}
235
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700236uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo,
237 size_t plane)
238{
239 void *count;
240 uintptr_t num = 0;
241
242 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, &count))
243 num = (uintptr_t) (count);
244
245 return num;
246}
247
248void drv_increment_reference_count(struct driver *drv, struct bo *bo,
249 size_t plane)
250{
251 uintptr_t num = drv_get_reference_count(drv, bo, plane);
252
253 /* If a value isn't in the table, drmHashDelete is a no-op */
254 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
255 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
256 (void *) (num + 1));
257}
258
259void drv_decrement_reference_count(struct driver *drv, struct bo *bo,
260 size_t plane)
261{
262 uintptr_t num = drv_get_reference_count(drv, bo, plane);
263
264 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
265
266 if (num > 0)
267 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
268 (void *) (num - 1));
269}
Gurchetan Singhef920532016-08-12 16:38:25 -0700270
Akshu Agrawal0337d9b2016-07-28 15:35:45 +0530271uint32_t drv_log_base2(uint32_t value)
272{
273 int ret = 0;
274
275 while (value >>= 1)
276 ++ret;
277
278 return ret;
279}
Gurchetan Singh179687e2016-10-28 10:07:35 -0700280
Gurchetan Singh0cefbe92016-12-01 13:36:04 -0800281/* Inserts a combination into list -- caller should have lock on driver. */
Gurchetan Singh179687e2016-10-28 10:07:35 -0700282void drv_insert_supported_combination(struct driver *drv, uint32_t format,
283 uint64_t usage, uint64_t modifier)
284{
Gurchetan Singh179687e2016-10-28 10:07:35 -0700285 struct combination_list_element *elem;
286
Gurchetan Singh0cefbe92016-12-01 13:36:04 -0800287 elem = calloc(1, sizeof(*elem));
288 elem->combination.format = format;
289 elem->combination.modifier = modifier;
290 elem->combination.usage = usage;
291 LIST_ADD(&elem->link, &drv->backend->combinations);
292}
293
294void drv_insert_combinations(struct driver *drv, struct supported_combination *combos,
295 uint32_t size)
296{
297 unsigned int i;
298
299 pthread_mutex_lock(&drv->driver_lock);
300
301 for (i = 0; i < size; i++)
302 drv_insert_supported_combination(drv, combos[i].format,
303 combos[i].usage,
304 combos[i].modifier);
305
306 pthread_mutex_unlock(&drv->driver_lock);
307}
308
309void drv_modify_supported_combination(struct driver *drv, uint32_t format,
310 uint64_t usage, uint64_t modifier)
311{
312 /*
313 * Attempts to add the specified usage to an existing {format, modifier}
314 * pair. If the pair is not present, a new combination is created.
315 */
316 int found = 0;
317
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800318 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700319
320 list_for_each_entry(struct combination_list_element,
321 elem, &drv->backend->combinations, link) {
322 if (elem->combination.format == format &&
323 elem->combination.modifier == modifier) {
324 elem->combination.usage |= usage;
325 found = 1;
326 }
327 }
328
Gurchetan Singh179687e2016-10-28 10:07:35 -0700329
Gurchetan Singh0cefbe92016-12-01 13:36:04 -0800330 if (!found)
331 drv_insert_supported_combination(drv, format, usage, modifier);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700332
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800333 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700334}
335
Gurchetan Singh179687e2016-10-28 10:07:35 -0700336int drv_add_kms_flags(struct driver *drv)
337{
338 int ret;
339 uint32_t i, j;
340 uint64_t flag, usage;
341 drmModePlanePtr plane;
342 drmModePropertyPtr prop;
343 drmModePlaneResPtr resources;
344 drmModeObjectPropertiesPtr props;
345
346 /*
347 * All current drivers can scanout XRGB8888/ARGB8888 as a primary plane.
348 * Some older kernel versions can only return overlay planes, so add the
349 * combination here. Note that the kernel disregards the alpha component
350 * of ARGB unless it's an overlay plane.
351 */
Gurchetan Singh0cefbe92016-12-01 13:36:04 -0800352 drv_modify_supported_combination(drv, DRM_FORMAT_XRGB8888,
Gurchetan Singh458976f2016-11-23 17:32:33 -0800353 BO_USE_SCANOUT, 0);
Gurchetan Singh0cefbe92016-12-01 13:36:04 -0800354 drv_modify_supported_combination(drv, DRM_FORMAT_ARGB8888,
Gurchetan Singh458976f2016-11-23 17:32:33 -0800355 BO_USE_SCANOUT, 0);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700356
357 /*
358 * The ability to return universal planes is only complete on
359 * ChromeOS kernel versions >= v3.18. The SET_CLIENT_CAP ioctl
360 * therefore might return an error code, so don't check it. If it
361 * fails, it'll just return the plane list as overlay planes, which is
362 * fine in our case (our drivers already have cursor bits set).
363 * modetest in libdrm does the same thing.
364 */
365 drmSetClientCap(drv->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
366
367 resources = drmModeGetPlaneResources(drv->fd);
368 if (!resources)
369 goto err;
370
371 for (i = 0; i < resources->count_planes; i++) {
372
373 plane = drmModeGetPlane(drv->fd, resources->planes[i]);
374
375 if (!plane)
376 goto err;
377
378 props = drmModeObjectGetProperties(drv->fd, plane->plane_id,
379 DRM_MODE_OBJECT_PLANE);
380 if (!props)
381 goto err;
382
383 for (j = 0; j < props->count_props; j++) {
384
385 prop = drmModeGetProperty(drv->fd, props->props[j]);
386 if (prop) {
387 if (strcmp(prop->name, "type") == 0) {
388 flag = props->prop_values[j];
389 }
390 drmModeFreeProperty(prop);
391 }
392 }
393
394 switch (flag) {
395 case DRM_PLANE_TYPE_OVERLAY:
396 case DRM_PLANE_TYPE_PRIMARY:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800397 usage = BO_USE_SCANOUT;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700398 break;
399 case DRM_PLANE_TYPE_CURSOR:
Gurchetan Singh458976f2016-11-23 17:32:33 -0800400 usage = BO_USE_CURSOR;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700401 break;
402 default:
403 assert(0);
404 }
405
406 for (j = 0; j < plane->count_formats; j++)
Gurchetan Singh0cefbe92016-12-01 13:36:04 -0800407 drv_modify_supported_combination(drv, plane->formats[j],
Gurchetan Singh179687e2016-10-28 10:07:35 -0700408 usage, 0);
409
410 drmModeFreeObjectProperties(props);
411 drmModeFreePlane(plane);
412
413 }
414
415 drmModeFreePlaneResources(resources);
416 return 0;
417
418err:
419 ret = -1;
420 return ret;
421}