blob: 915a4ce1300b4ac4cf3eeeb931165000e50a7637 [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2014 The Chromium OS Authors. All rights reserved.
Stéphane Marchesin25a26062014-09-12 16:18:59 -07003 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07007#ifdef DRV_I915
Stéphane Marchesin25a26062014-09-12 16:18:59 -07008
9#include <errno.h>
Gurchetan Singh82a8eed2017-01-03 13:01:37 -080010#include <i915_drm.h>
11#include <intel_bufmgr.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070012#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070013#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070014#include <xf86drm.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070015
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include "drv_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070017#include "helpers.h"
18#include "util.h"
19
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080020static const uint32_t tileable_formats[] = {
21 DRM_FORMAT_ARGB1555, DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
22 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB1555,
23 DRM_FORMAT_XRGB8888, DRM_FORMAT_UYVY, DRM_FORMAT_YUYV
24};
25
26static const uint32_t linear_only_formats[] = {
Gurchetan Singh03f13562017-02-08 15:21:14 -080027 DRM_FORMAT_GR88, DRM_FORMAT_R8, DRM_FORMAT_YVU420,
28 DRM_FORMAT_YVU420_ANDROID
Gurchetan Singh179687e2016-10-28 10:07:35 -070029};
30
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070031struct i915_device
Stéphane Marchesin25a26062014-09-12 16:18:59 -070032{
33 int gen;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -080034 drm_intel_bufmgr *mgr;
35 uint32_t count;
36};
37
38struct i915_bo
39{
40 drm_intel_bo *ibos[DRV_MAX_PLANES];
Stéphane Marchesin25a26062014-09-12 16:18:59 -070041};
42
Stéphane Marchesin25a26062014-09-12 16:18:59 -070043static int get_gen(int device_id)
44{
Stéphane Marchesinec88e892015-11-03 16:14:59 -080045 const uint16_t gen3_ids[] = {0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
46 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011};
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070047 unsigned i;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070048 for(i = 0; i < ARRAY_SIZE(gen3_ids); i++)
49 if (gen3_ids[i] == device_id)
50 return 3;
51
52 return 4;
53}
54
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080055static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
56{
57 uint32_t i;
58 struct combination *combo;
59
60 /*
61 * Older hardware can't scanout Y-tiled formats. Newer devices can, and
62 * report this functionality via format modifiers.
63 */
64 for (i = 0; i < drv->backend->combos.size; i++) {
65 combo = &drv->backend->combos.data[i];
66 if (combo->format == item->format) {
67 if ((combo->metadata.tiling == I915_TILING_Y &&
68 item->modifier == I915_FORMAT_MOD_Y_TILED) ||
69 (combo->metadata.tiling == I915_TILING_X &&
70 item->modifier == I915_FORMAT_MOD_X_TILED)) {
71 combo->metadata.modifier = item->modifier;
72 combo->usage |= item->usage;
73 } else if (combo->metadata.tiling != I915_TILING_Y) {
74 combo->usage |= item->usage;
75 }
76 }
77 }
78
79 return 0;
80}
81
82static int i915_add_combinations(struct driver *drv)
83{
84 int ret;
85 uint32_t i, num_items;
86 struct kms_item *items;
87 struct format_metadata metadata;
88 uint64_t flags = BO_COMMON_USE_MASK;
89
90 metadata.tiling = I915_TILING_NONE;
91 metadata.priority = 1;
92 metadata.modifier = DRM_FORMAT_MOD_NONE;
93
94 ret = drv_add_combinations(drv, linear_only_formats,
95 ARRAY_SIZE(linear_only_formats), &metadata,
96 flags);
97 if (ret)
98 return ret;
99
100 ret = drv_add_combinations(drv, tileable_formats,
101 ARRAY_SIZE(tileable_formats), &metadata,
102 flags);
103 if (ret)
104 return ret;
105
106 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
107 BO_USE_CURSOR | BO_USE_SCANOUT);
108 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
109 BO_USE_CURSOR | BO_USE_SCANOUT);
110
111 flags &= ~BO_USE_SW_WRITE_OFTEN;
112 flags &= ~BO_USE_SW_READ_OFTEN;
113 flags &= ~BO_USE_LINEAR;
114
115 metadata.tiling = I915_TILING_X;
116 metadata.priority = 2;
117
118 ret = drv_add_combinations(drv, tileable_formats,
119 ARRAY_SIZE(tileable_formats), &metadata,
120 flags);
121 if (ret)
122 return ret;
123
124 metadata.tiling = I915_TILING_Y;
125 metadata.priority = 3;
126
127 ret = drv_add_combinations(drv, tileable_formats,
128 ARRAY_SIZE(tileable_formats), &metadata,
129 flags);
130 if (ret)
131 return ret;
132
133 items = drv_query_kms(drv, &num_items);
134 if (!items || !num_items)
135 return 0;
136
137 for (i = 0; i < num_items; i++) {
138 ret = i915_add_kms_item(drv, &items[i]);
139 if (ret) {
140 free(items);
141 return ret;
142 }
143 }
144
145 free(items);
146 return 0;
147}
148
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700149static void i915_align_dimensions(struct driver *drv, uint32_t tiling_mode,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800150 uint32_t *width, uint32_t *height, int bpp)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700151{
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800152 struct i915_device *i915_dev = (struct i915_device *)drv->priv;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800153 uint32_t width_alignment = 4, height_alignment = 4;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700154
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700155 switch (tiling_mode) {
156 default:
157 case I915_TILING_NONE:
158 width_alignment = 64 / bpp;
159 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800160
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700161 case I915_TILING_X:
162 width_alignment = 512 / bpp;
163 height_alignment = 8;
164 break;
165
166 case I915_TILING_Y:
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800167 if (i915_dev->gen == 3) {
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800168 width_alignment = 512 / bpp;
169 height_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700170 } else {
171 width_alignment = 128 / bpp;
172 height_alignment = 32;
173 }
174 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700175 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800176
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800177 if (i915_dev->gen > 3) {
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800178 *width = ALIGN(*width, width_alignment);
179 *height = ALIGN(*height, height_alignment);
180 } else {
181 uint32_t w;
Stéphane Marchesine3d7c1f2015-03-31 13:47:22 -0700182 for (w = width_alignment; w < *width; w <<= 1)
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800183 ;
184 *width = w;
185 *height = ALIGN(*height, height_alignment);
186 }
187}
188
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700189static int i915_verify_dimensions(struct driver *drv, uint32_t stride,
Stéphane Marchesinec88e892015-11-03 16:14:59 -0800190 uint32_t height)
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800191{
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800192 struct i915_device *i915_dev = (struct i915_device *)drv->priv;
193 if (i915_dev->gen <= 3 && stride > 8192)
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800194 return 0;
195
196 return 1;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700197}
198
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800199static int i915_init(struct driver *drv)
200{
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800201 struct i915_device *i915_dev;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800202 drm_i915_getparam_t get_param;
203 int device_id;
204 int ret;
205
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800206 i915_dev = calloc(1, sizeof(*i915_dev));
207 if (!i915_dev)
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800208 return -1;
209
210 memset(&get_param, 0, sizeof(get_param));
211 get_param.param = I915_PARAM_CHIPSET_ID;
212 get_param.value = &device_id;
213 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
214 if (ret) {
215 fprintf(stderr, "drv: DRM_IOCTL_I915_GETPARAM failed\n");
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800216 free(i915_dev);
217 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800218 }
219
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800220 i915_dev->gen = get_gen(device_id);
221 i915_dev->count = 0;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800222
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800223 i915_dev->mgr = drm_intel_bufmgr_gem_init(drv->fd, 16 * 1024);
224 if (!i915_dev->mgr) {
225 fprintf(stderr, "drv: drm_intel_bufmgr_gem_init failed\n");
226 free(i915_dev);
227 return -EINVAL;
228 }
229
230 drv->priv = i915_dev;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800231
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800232 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800233}
234
235static void i915_close(struct driver *drv)
236{
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800237 struct i915_device *i915_dev = drv->priv;
238 drm_intel_bufmgr_destroy(i915_dev->mgr);
239 free(i915_dev);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800240 drv->priv = NULL;
241}
242
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700243static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height,
244 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700245{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700246 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800247 size_t plane;
248 char name[20];
249 uint32_t tiling_mode;
250 struct i915_bo *i915_bo;
251
252 int bpp = drv_stride_from_format(format, 1, 0);
253 struct i915_device *i915_dev = (struct i915_device *)bo->drv->priv;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700254
Gurchetan Singh458976f2016-11-23 17:32:33 -0800255 if (flags & (BO_USE_CURSOR | BO_USE_LINEAR |
256 BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN))
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700257 tiling_mode = I915_TILING_NONE;
Gurchetan Singh458976f2016-11-23 17:32:33 -0800258 else if (flags & BO_USE_SCANOUT)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700259 tiling_mode = I915_TILING_X;
Gurchetan Singh6bab0c12016-10-13 19:08:48 -0700260 else
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700261 tiling_mode = I915_TILING_Y;
262
Gurchetan Singh507f5dd2017-03-16 13:14:30 -0700263 /*
264 * Align the Y plane to 128 bytes so the chroma planes would be aligned
265 * to 64 byte boundaries. This is an Intel HW requirement.
266 */
267 if (format == DRM_FORMAT_YVU420 ||
268 format == DRM_FORMAT_YVU420_ANDROID) {
269 width = ALIGN(width, 128);
270 tiling_mode = I915_TILING_NONE;
271 }
272
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800273 i915_align_dimensions(bo->drv, tiling_mode, &width, &height, bpp);
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700274 drv_bo_from_format(bo, width, height, format);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800275
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800276 if (!i915_verify_dimensions(bo->drv, bo->strides[0], height))
277 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800278
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800279 snprintf(name, sizeof(name), "i915-buffer-%u", i915_dev->count);
280 i915_dev->count++;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800281
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800282 i915_bo = calloc(1, sizeof(*i915_bo));
283 if (!i915_bo)
284 return -ENOMEM;
285
286 bo->priv = i915_bo;
287
288 i915_bo->ibos[0] = drm_intel_bo_alloc(i915_dev->mgr, name,
289 bo->total_size, 0);
290 if (!i915_bo->ibos[0]) {
291 fprintf(stderr, "drv: drm_intel_bo_alloc failed");
292 free(i915_bo);
293 bo->priv = NULL;
294 return -ENOMEM;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700295 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700296
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800297 for (plane = 0; plane < bo->num_planes; plane++) {
298 if (plane > 0)
299 drm_intel_bo_reference(i915_bo->ibos[0]);
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400300
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800301 bo->handles[plane].u32 = i915_bo->ibos[0]->handle;
302 i915_bo->ibos[plane] = i915_bo->ibos[0];
303 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700304
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800305 bo->tiling = tiling_mode;
306
307 ret = drm_intel_bo_set_tiling(i915_bo->ibos[0], &bo->tiling,
308 bo->strides[0]);
309
310 if (ret || bo->tiling != tiling_mode) {
311 fprintf(stderr, "drv: drm_intel_gem_bo_set_tiling failed "
312 "errno=%x, stride=%x\n", errno, bo->strides[0]);
313 /* Calls i915 bo destroy. */
314 bo->drv->backend->bo_destroy(bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700315 return -errno;
316 }
317
318 return 0;
319}
320
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800321static int i915_bo_destroy(struct bo *bo)
322{
323 size_t plane;
324 struct i915_bo *i915_bo = bo->priv;
325
326 for (plane = 0; plane < bo->num_planes; plane++)
327 drm_intel_bo_unreference(i915_bo->ibos[plane]);
328
329 free(i915_bo);
330 bo->priv = NULL;
331
332 return 0;
333}
334
335static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
336{
337 size_t plane;
338 uint32_t swizzling;
339 struct i915_bo *i915_bo;
340 struct i915_device *i915_dev = bo->drv->priv;
341
342 i915_bo = calloc(1, sizeof(*i915_bo));
343 if (!i915_bo)
344 return -ENOMEM;
345
346 bo->priv = i915_bo;
347
348 /*
349 * When self-importing, libdrm_intel increments the reference count
350 * on the drm_intel_bo. It also returns the same drm_intel_bo per GEM
351 * handle. Thus, we don't need to increase the reference count
352 * (i.e, drv_increment_reference_count) when importing with this
353 * backend.
354 */
355 for (plane = 0; plane < bo->num_planes; plane++) {
356
357 i915_bo->ibos[plane] = drm_intel_bo_gem_create_from_prime(i915_dev->mgr,
358 data->fds[plane], data->sizes[plane]);
359
360 if (!i915_bo->ibos[plane]) {
361 /*
362 * Need to call GEM close on planes that were opened,
363 * if any. Adjust the num_planes variable to be the
364 * plane that failed, so GEM close will be called on
365 * planes before that plane.
366 */
367 bo->num_planes = plane;
368 i915_bo_destroy(bo);
369 fprintf(stderr, "drv: i915: failed to import failed");
370 return -EINVAL;
371 }
372
373 bo->handles[plane].u32 = i915_bo->ibos[plane]->handle;
374 }
375
376 if (drm_intel_bo_get_tiling(i915_bo->ibos[0], &bo->tiling,
377 &swizzling)) {
378 fprintf(stderr, "drv: drm_intel_bo_get_tiling failed");
379 i915_bo_destroy(bo);
380 return -EINVAL;
381 }
382
383 return 0;
384}
385
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700386static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700387{
388 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800389 struct i915_bo *i915_bo = bo->priv;
Gurchetan Singhef920532016-08-12 16:38:25 -0700390
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800391 if (bo->tiling == I915_TILING_NONE)
392 /* TODO(gsingh): use bo_map flags to determine if we should
393 * enable writing.
394 */
395 ret = drm_intel_bo_map(i915_bo->ibos[0], 1);
396 else
397 ret = drm_intel_gem_bo_map_gtt(i915_bo->ibos[0]);
Gurchetan Singhef920532016-08-12 16:38:25 -0700398
Gurchetan Singhef920532016-08-12 16:38:25 -0700399 if (ret) {
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800400 fprintf(stderr, "drv: i915_bo_map failed.");
Gurchetan Singhef920532016-08-12 16:38:25 -0700401 return MAP_FAILED;
402 }
403
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800404 return i915_bo->ibos[0]->virtual;
405}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700406
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800407static int i915_bo_unmap(struct bo *bo, struct map_info *data)
408{
409 int ret;
410 struct i915_bo *i915_bo = bo->priv;
411
412 if (bo->tiling == I915_TILING_NONE)
413 ret = drm_intel_bo_unmap(i915_bo->ibos[0]);
414 else
415 ret = drm_intel_gem_bo_unmap_gtt(i915_bo->ibos[0]);
416
417 return ret;
Gurchetan Singhef920532016-08-12 16:38:25 -0700418}
419
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800420static uint32_t i915_resolve_format(uint32_t format)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700421{
422 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800423 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700424 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800425 return DRM_FORMAT_XBGR8888;
426 case DRM_FORMAT_FLEX_YCbCr_420_888:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800427 return DRM_FORMAT_YVU420_ANDROID;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700428 default:
429 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700430 }
431}
432
Gurchetan Singh179687e2016-10-28 10:07:35 -0700433struct backend backend_i915 =
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700434{
435 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700436 .init = i915_init,
437 .close = i915_close,
438 .bo_create = i915_bo_create,
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800439 .bo_destroy = i915_bo_destroy,
440 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700441 .bo_map = i915_bo_map,
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800442 .bo_unmap = i915_bo_unmap,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700443 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700444};
445
446#endif