blob: b744d38da345d1460264db9cff3bed81aba75e68 [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 Singh6423ecb2017-03-29 08:23:40 -0700149static int i915_align_dimensions(struct bo *bo, uint32_t tiling,
150 uint32_t *stride, uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700151{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700152 struct i915_device *i915 = bo->drv->priv;
153 uint32_t horizontal_alignment = 4;
154 uint32_t vertical_alignment = 4;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700155
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700156 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700157 default:
158 case I915_TILING_NONE:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700159 horizontal_alignment = 64;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700160 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800161
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700162 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700163 horizontal_alignment = 512;
164 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700165 break;
166
167 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700168 if (i915->gen == 3) {
169 horizontal_alignment = 512;
170 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700171 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700172 horizontal_alignment = 128;
173 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700174 }
175 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700176 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800177
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700178 *aligned_height = ALIGN(bo->height, vertical_alignment);
179 if (i915->gen > 3) {
180 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800181 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700182 while (*stride > horizontal_alignment)
183 horizontal_alignment <<= 1;
184
185 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800186 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800187
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700188 if (i915->gen <= 3 && *stride > 8192)
189 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800190
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700191 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700192}
193
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800194static int i915_init(struct driver *drv)
195{
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800196 struct i915_device *i915_dev;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800197 drm_i915_getparam_t get_param;
198 int device_id;
199 int ret;
200
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800201 i915_dev = calloc(1, sizeof(*i915_dev));
202 if (!i915_dev)
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800203 return -1;
204
205 memset(&get_param, 0, sizeof(get_param));
206 get_param.param = I915_PARAM_CHIPSET_ID;
207 get_param.value = &device_id;
208 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
209 if (ret) {
210 fprintf(stderr, "drv: DRM_IOCTL_I915_GETPARAM failed\n");
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800211 free(i915_dev);
212 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800213 }
214
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800215 i915_dev->gen = get_gen(device_id);
216 i915_dev->count = 0;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800217
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800218 i915_dev->mgr = drm_intel_bufmgr_gem_init(drv->fd, 16 * 1024);
219 if (!i915_dev->mgr) {
220 fprintf(stderr, "drv: drm_intel_bufmgr_gem_init failed\n");
221 free(i915_dev);
222 return -EINVAL;
223 }
224
225 drv->priv = i915_dev;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800226
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800227 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800228}
229
230static void i915_close(struct driver *drv)
231{
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800232 struct i915_device *i915_dev = drv->priv;
233 drm_intel_bufmgr_destroy(i915_dev->mgr);
234 free(i915_dev);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800235 drv->priv = NULL;
236}
237
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700238static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height,
239 uint32_t format, uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700240{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700241 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800242 size_t plane;
243 char name[20];
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700244 uint32_t stride;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800245 uint32_t tiling_mode;
246 struct i915_bo *i915_bo;
247
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700248 stride = drv_stride_from_format(format, width, 0);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800249 struct i915_device *i915_dev = (struct i915_device *)bo->drv->priv;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700250
Gurchetan Singh458976f2016-11-23 17:32:33 -0800251 if (flags & (BO_USE_CURSOR | BO_USE_LINEAR |
252 BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN))
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700253 tiling_mode = I915_TILING_NONE;
Gurchetan Singh458976f2016-11-23 17:32:33 -0800254 else if (flags & BO_USE_SCANOUT)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700255 tiling_mode = I915_TILING_X;
Gurchetan Singh6bab0c12016-10-13 19:08:48 -0700256 else
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700257 tiling_mode = I915_TILING_Y;
258
Gurchetan Singh507f5dd2017-03-16 13:14:30 -0700259 /*
260 * Align the Y plane to 128 bytes so the chroma planes would be aligned
261 * to 64 byte boundaries. This is an Intel HW requirement.
262 */
263 if (format == DRM_FORMAT_YVU420 ||
264 format == DRM_FORMAT_YVU420_ANDROID) {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700265 stride = ALIGN(stride, 128);
Gurchetan Singh507f5dd2017-03-16 13:14:30 -0700266 tiling_mode = I915_TILING_NONE;
267 }
268
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700269 ret = i915_align_dimensions(bo, tiling_mode, &stride, &height);
270 if (ret)
271 return ret;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800272
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700273 drv_bo_from_format(bo, stride, height, format);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800274
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800275 snprintf(name, sizeof(name), "i915-buffer-%u", i915_dev->count);
276 i915_dev->count++;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800277
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800278 i915_bo = calloc(1, sizeof(*i915_bo));
279 if (!i915_bo)
280 return -ENOMEM;
281
282 bo->priv = i915_bo;
283
284 i915_bo->ibos[0] = drm_intel_bo_alloc(i915_dev->mgr, name,
285 bo->total_size, 0);
286 if (!i915_bo->ibos[0]) {
287 fprintf(stderr, "drv: drm_intel_bo_alloc failed");
288 free(i915_bo);
289 bo->priv = NULL;
290 return -ENOMEM;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700291 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700292
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800293 for (plane = 0; plane < bo->num_planes; plane++) {
294 if (plane > 0)
295 drm_intel_bo_reference(i915_bo->ibos[0]);
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400296
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800297 bo->handles[plane].u32 = i915_bo->ibos[0]->handle;
298 i915_bo->ibos[plane] = i915_bo->ibos[0];
299 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700300
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800301 bo->tiling = tiling_mode;
302
303 ret = drm_intel_bo_set_tiling(i915_bo->ibos[0], &bo->tiling,
304 bo->strides[0]);
305
306 if (ret || bo->tiling != tiling_mode) {
307 fprintf(stderr, "drv: drm_intel_gem_bo_set_tiling failed "
308 "errno=%x, stride=%x\n", errno, bo->strides[0]);
309 /* Calls i915 bo destroy. */
310 bo->drv->backend->bo_destroy(bo);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700311 return -errno;
312 }
313
314 return 0;
315}
316
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800317static int i915_bo_destroy(struct bo *bo)
318{
319 size_t plane;
320 struct i915_bo *i915_bo = bo->priv;
321
322 for (plane = 0; plane < bo->num_planes; plane++)
323 drm_intel_bo_unreference(i915_bo->ibos[plane]);
324
325 free(i915_bo);
326 bo->priv = NULL;
327
328 return 0;
329}
330
331static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
332{
333 size_t plane;
334 uint32_t swizzling;
335 struct i915_bo *i915_bo;
336 struct i915_device *i915_dev = bo->drv->priv;
337
338 i915_bo = calloc(1, sizeof(*i915_bo));
339 if (!i915_bo)
340 return -ENOMEM;
341
342 bo->priv = i915_bo;
343
344 /*
345 * When self-importing, libdrm_intel increments the reference count
346 * on the drm_intel_bo. It also returns the same drm_intel_bo per GEM
347 * handle. Thus, we don't need to increase the reference count
348 * (i.e, drv_increment_reference_count) when importing with this
349 * backend.
350 */
351 for (plane = 0; plane < bo->num_planes; plane++) {
352
353 i915_bo->ibos[plane] = drm_intel_bo_gem_create_from_prime(i915_dev->mgr,
354 data->fds[plane], data->sizes[plane]);
355
356 if (!i915_bo->ibos[plane]) {
357 /*
358 * Need to call GEM close on planes that were opened,
359 * if any. Adjust the num_planes variable to be the
360 * plane that failed, so GEM close will be called on
361 * planes before that plane.
362 */
363 bo->num_planes = plane;
364 i915_bo_destroy(bo);
365 fprintf(stderr, "drv: i915: failed to import failed");
366 return -EINVAL;
367 }
368
369 bo->handles[plane].u32 = i915_bo->ibos[plane]->handle;
370 }
371
372 if (drm_intel_bo_get_tiling(i915_bo->ibos[0], &bo->tiling,
373 &swizzling)) {
374 fprintf(stderr, "drv: drm_intel_bo_get_tiling failed");
375 i915_bo_destroy(bo);
376 return -EINVAL;
377 }
378
379 return 0;
380}
381
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700382static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700383{
384 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800385 struct i915_bo *i915_bo = bo->priv;
Gurchetan Singhef920532016-08-12 16:38:25 -0700386
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800387 if (bo->tiling == I915_TILING_NONE)
388 /* TODO(gsingh): use bo_map flags to determine if we should
389 * enable writing.
390 */
391 ret = drm_intel_bo_map(i915_bo->ibos[0], 1);
392 else
393 ret = drm_intel_gem_bo_map_gtt(i915_bo->ibos[0]);
Gurchetan Singhef920532016-08-12 16:38:25 -0700394
Gurchetan Singhef920532016-08-12 16:38:25 -0700395 if (ret) {
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800396 fprintf(stderr, "drv: i915_bo_map failed.");
Gurchetan Singhef920532016-08-12 16:38:25 -0700397 return MAP_FAILED;
398 }
399
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800400 return i915_bo->ibos[0]->virtual;
401}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700402
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800403static int i915_bo_unmap(struct bo *bo, struct map_info *data)
404{
405 int ret;
406 struct i915_bo *i915_bo = bo->priv;
407
408 if (bo->tiling == I915_TILING_NONE)
409 ret = drm_intel_bo_unmap(i915_bo->ibos[0]);
410 else
411 ret = drm_intel_gem_bo_unmap_gtt(i915_bo->ibos[0]);
412
413 return ret;
Gurchetan Singhef920532016-08-12 16:38:25 -0700414}
415
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800416static uint32_t i915_resolve_format(uint32_t format)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700417{
418 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800419 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700420 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800421 return DRM_FORMAT_XBGR8888;
422 case DRM_FORMAT_FLEX_YCbCr_420_888:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800423 return DRM_FORMAT_YVU420_ANDROID;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700424 default:
425 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700426 }
427}
428
Gurchetan Singh179687e2016-10-28 10:07:35 -0700429struct backend backend_i915 =
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700430{
431 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700432 .init = i915_init,
433 .close = i915_close,
434 .bo_create = i915_bo_create,
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800435 .bo_destroy = i915_bo_destroy,
436 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700437 .bo_map = i915_bo_map,
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800438 .bo_unmap = i915_bo_unmap,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700439 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700440};
441
442#endif