blob: 2dc2484a8f87ec093fcfae1d88ac5f1fd57b8058 [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>
Gurchetan Singhcc015e82017-01-17 16:15:25 -080011#include <stdio.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 Singh68af9c22017-01-18 13:48:11 -080020#define I915_CACHELINE_SIZE 64
21#define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
22
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070023static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB1555,
24 DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
Daniele Castagna7a2df902017-10-18 16:15:44 -040025 DRM_FORMAT_XBGR2101010, DRM_FORMAT_XBGR8888,
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070026 DRM_FORMAT_XRGB1555, DRM_FORMAT_XRGB2101010,
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070027 DRM_FORMAT_XRGB8888 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080028
Dongseong Hwang750e0b92017-06-07 15:17:25 -070029static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_NV12,
30 DRM_FORMAT_R8, DRM_FORMAT_UYVY,
31 DRM_FORMAT_YUYV };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070032
33static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
Gurchetan Singh179687e2016-10-28 10:07:35 -070034
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080035struct i915_device {
Gurchetan Singh68af9c22017-01-18 13:48:11 -080036 uint32_t gen;
37 int32_t has_llc;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070038};
39
Gurchetan Singh68af9c22017-01-18 13:48:11 -080040static uint32_t i915_get_gen(int device_id)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070041{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080042 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
43 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070044 unsigned i;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080045 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070046 if (gen3_ids[i] == device_id)
47 return 3;
48
49 return 4;
50}
51
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080052static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
53{
54 uint32_t i;
55 struct combination *combo;
56
57 /*
58 * Older hardware can't scanout Y-tiled formats. Newer devices can, and
59 * report this functionality via format modifiers.
60 */
Ege Mihmanli96b7d462017-09-19 20:13:26 -070061 for (i = 0; i < drv->combos.size; i++) {
62 combo = &drv->combos.data[i];
Tomasz Figae821cc22017-07-08 15:53:11 +090063 if (combo->format != item->format)
64 continue;
65
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -070066 if (item->modifier == DRM_FORMAT_MOD_INVALID &&
Tomasz Figae821cc22017-07-08 15:53:11 +090067 combo->metadata.tiling == I915_TILING_X) {
68 /*
69 * FIXME: drv_query_kms() does not report the available modifiers
70 * yet, but we know that all hardware can scanout from X-tiled
71 * buffers, so let's add this to our combinations, except for
72 * cursor, which must not be tiled.
73 */
Gurchetan Singha1892b22017-09-28 16:40:52 -070074 combo->use_flags |= item->use_flags & ~BO_USE_CURSOR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080075 }
Tomasz Figae821cc22017-07-08 15:53:11 +090076
77 if (combo->metadata.modifier == item->modifier)
Gurchetan Singha1892b22017-09-28 16:40:52 -070078 combo->use_flags |= item->use_flags;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080079 }
80
81 return 0;
82}
83
84static int i915_add_combinations(struct driver *drv)
85{
86 int ret;
87 uint32_t i, num_items;
88 struct kms_item *items;
89 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -070090 uint64_t render_use_flags, texture_use_flags;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070091
Gurchetan Singha1892b22017-09-28 16:40:52 -070092 render_use_flags = BO_USE_RENDER_MASK;
93 texture_use_flags = BO_USE_TEXTURE_MASK;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080094
95 metadata.tiling = I915_TILING_NONE;
96 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -070097 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080098
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070099 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
Gurchetan Singha1892b22017-09-28 16:40:52 -0700100 &metadata, render_use_flags);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800101 if (ret)
102 return ret;
103
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700104 ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
Gurchetan Singha1892b22017-09-28 16:40:52 -0700105 &metadata, texture_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700106 if (ret)
107 return ret;
108
109 ret = drv_add_combinations(drv, tileable_texture_source_formats,
Dongseong Hwang3c5be5a2017-06-14 10:47:11 -0700110 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700111 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800112 if (ret)
113 return ret;
114
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800115 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
116 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800117
Tomasz Figad30c0a52017-07-05 17:50:18 +0900118 /* IPU3 camera ISP supports only NV12 output. */
119 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900120 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900121 /*
122 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
123 * from camera.
124 */
125 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900126 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900127
Gurchetan Singha1892b22017-09-28 16:40:52 -0700128 render_use_flags &= ~BO_USE_RENDERSCRIPT;
129 render_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
130 render_use_flags &= ~BO_USE_SW_READ_OFTEN;
131 render_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700132
Gurchetan Singha1892b22017-09-28 16:40:52 -0700133 texture_use_flags &= ~BO_USE_RENDERSCRIPT;
134 texture_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
135 texture_use_flags &= ~BO_USE_SW_READ_OFTEN;
136 texture_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800137
138 metadata.tiling = I915_TILING_X;
139 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900140 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800141
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700142 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
Gurchetan Singha1892b22017-09-28 16:40:52 -0700143 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700144 if (ret)
145 return ret;
146
147 ret = drv_add_combinations(drv, tileable_texture_source_formats,
148 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700149 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800150 if (ret)
151 return ret;
152
153 metadata.tiling = I915_TILING_Y;
154 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900155 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800156
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700157 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
Gurchetan Singha1892b22017-09-28 16:40:52 -0700158 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700159 if (ret)
160 return ret;
161
162 ret = drv_add_combinations(drv, tileable_texture_source_formats,
163 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700164 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800165 if (ret)
166 return ret;
167
168 items = drv_query_kms(drv, &num_items);
169 if (!items || !num_items)
170 return 0;
171
172 for (i = 0; i < num_items; i++) {
173 ret = i915_add_kms_item(drv, &items[i]);
174 if (ret) {
175 free(items);
176 return ret;
177 }
178 }
179
180 free(items);
181 return 0;
182}
183
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800184static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
185 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700186{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700187 struct i915_device *i915 = bo->drv->priv;
188 uint32_t horizontal_alignment = 4;
189 uint32_t vertical_alignment = 4;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700190
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700191 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700192 default:
193 case I915_TILING_NONE:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700194 horizontal_alignment = 64;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700195 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800196
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700197 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700198 horizontal_alignment = 512;
199 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700200 break;
201
202 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700203 if (i915->gen == 3) {
204 horizontal_alignment = 512;
205 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800206 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700207 horizontal_alignment = 128;
208 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700209 }
210 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700211 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800212
Tomasz Figa33615a52017-07-29 15:37:58 +0900213 /*
214 * The alignment calculated above is based on the full size luma plane and to have chroma
215 * planes properly aligned with subsampled formats, we need to multiply luma alignment by
216 * subsampling factor.
217 */
218 switch (bo->format) {
219 case DRM_FORMAT_YVU420_ANDROID:
220 case DRM_FORMAT_YVU420:
221 horizontal_alignment *= 2;
Gurchetan Singh7dcdff12017-09-14 13:04:11 -0700222 /* Fall through */
Tomasz Figa33615a52017-07-29 15:37:58 +0900223 case DRM_FORMAT_NV12:
224 vertical_alignment *= 2;
225 break;
226 }
227
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700228 *aligned_height = ALIGN(bo->height, vertical_alignment);
229 if (i915->gen > 3) {
230 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800231 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700232 while (*stride > horizontal_alignment)
233 horizontal_alignment <<= 1;
234
235 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800236 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800237
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700238 if (i915->gen <= 3 && *stride > 8192)
239 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800240
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700241 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700242}
243
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800244static void i915_clflush(void *start, size_t size)
245{
246 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
247 void *end = (void *)((uintptr_t)start + size);
248
249 __builtin_ia32_mfence();
250 while (p < end) {
251 __builtin_ia32_clflush(p);
252 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
253 }
254}
255
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800256static int i915_init(struct driver *drv)
257{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800258 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800259 int device_id;
260 struct i915_device *i915;
261 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800262
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800263 i915 = calloc(1, sizeof(*i915));
264 if (!i915)
265 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800266
267 memset(&get_param, 0, sizeof(get_param));
268 get_param.param = I915_PARAM_CHIPSET_ID;
269 get_param.value = &device_id;
270 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
271 if (ret) {
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800272 fprintf(stderr, "drv: Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800273 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800274 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800275 }
276
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800277 i915->gen = i915_get_gen(device_id);
278
279 memset(&get_param, 0, sizeof(get_param));
280 get_param.param = I915_PARAM_HAS_LLC;
281 get_param.value = &i915->has_llc;
282 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
283 if (ret) {
284 fprintf(stderr, "drv: Failed to get I915_PARAM_HAS_LLC\n");
285 free(i915);
286 return -EINVAL;
287 }
288
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800289 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800290
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800291 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800292}
293
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700294static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
295 uint32_t format, uint64_t modifier)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700296{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700297 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800298 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700299 uint32_t stride;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800300 struct drm_i915_gem_create gem_create;
301 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700302
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700303 switch (modifier) {
304 case DRM_FORMAT_MOD_LINEAR:
305 bo->tiling = I915_TILING_NONE;
306 break;
307 case I915_FORMAT_MOD_X_TILED:
308 bo->tiling = I915_TILING_X;
309 break;
310 case I915_FORMAT_MOD_Y_TILED:
311 bo->tiling = I915_TILING_Y;
312 break;
313 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800314
315 stride = drv_stride_from_format(format, width, 0);
Gurchetan Singh507f5dd2017-03-16 13:14:30 -0700316
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800317 ret = i915_align_dimensions(bo, bo->tiling, &stride, &height);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700318 if (ret)
319 return ret;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800320
Owen Linbbb69fd2017-06-05 14:33:08 +0800321 /*
Tomasz Figad846de62017-07-29 15:47:54 +0900322 * HAL_PIXEL_FORMAT_YV12 requires the buffer height not be aligned, but we need to keep
323 * total size as with aligned height to ensure enough padding space after each plane to
324 * satisfy GPU alignment requirements.
325 *
326 * We do it by first calling drv_bo_from_format() with aligned height and
327 * DRM_FORMAT_YVU420, which allows height alignment, saving the total size it calculates
328 * and then calling it again with requested parameters.
329 *
330 * This relies on the fact that i965 driver uses separate surfaces for each plane and
331 * contents of padding bytes is not affected, as it is only used to satisfy GPU cache
332 * requests.
333 *
334 * This is enforced by Mesa in src/intel/isl/isl_gen8.c, inside
335 * isl_gen8_choose_image_alignment_el(), which is used for GEN9 and GEN8.
Owen Linbbb69fd2017-06-05 14:33:08 +0800336 */
Tomasz Figad846de62017-07-29 15:47:54 +0900337 if (format == DRM_FORMAT_YVU420_ANDROID) {
338 uint32_t unaligned_height = bo->height;
339 size_t total_size;
Owen Linbbb69fd2017-06-05 14:33:08 +0800340
Tomasz Figad846de62017-07-29 15:47:54 +0900341 drv_bo_from_format(bo, stride, height, DRM_FORMAT_YVU420);
342 total_size = bo->total_size;
343 drv_bo_from_format(bo, stride, unaligned_height, format);
344 bo->total_size = total_size;
345 } else {
346 drv_bo_from_format(bo, stride, height, format);
347 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800348
Tomasz Figa581f3a52017-07-23 15:02:19 +0900349 /*
350 * Quoting Mesa ISL library:
351 *
352 * - For linear surfaces, additional padding of 64 bytes is required at
353 * the bottom of the surface. This is in addition to the padding
354 * required above.
355 */
356 if (bo->tiling == I915_TILING_NONE)
357 bo->total_size += 64;
358
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800359 memset(&gem_create, 0, sizeof(gem_create));
360 gem_create.size = bo->total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800361
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800362 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
363 if (ret) {
364 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n",
365 gem_create.size);
366 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700367 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700368
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800369 for (plane = 0; plane < bo->num_planes; plane++)
370 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400371
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800372 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
373 gem_set_tiling.handle = bo->handles[0].u32;
374 gem_set_tiling.tiling_mode = bo->tiling;
375 gem_set_tiling.stride = bo->strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700376
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800377 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
378 if (ret) {
379 struct drm_gem_close gem_close;
380 memset(&gem_close, 0, sizeof(gem_close));
381 gem_close.handle = bo->handles[0].u32;
382 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800383
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800384 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_TILING failed with %d", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700385 return -errno;
386 }
387
388 return 0;
389}
390
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700391static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
392 uint64_t use_flags)
393{
394 struct combination *combo;
395
396 combo = drv_get_combination(bo->drv, format, use_flags);
397 if (!combo)
398 return -EINVAL;
399
400 return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
401}
402
403static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
404 uint32_t format, const uint64_t *modifiers, uint32_t count)
405{
406 static const uint64_t modifier_order[] = {
407 I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED, DRM_FORMAT_MOD_LINEAR,
408 };
409 uint64_t modifier;
410
411 modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
412
413 bo->format_modifiers[0] = modifier;
414
415 return i915_bo_create_for_modifier(bo, width, height, format, modifier);
416}
417
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800418static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800419{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800420 free(drv->priv);
421 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800422}
423
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800424static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
425{
426 int ret;
427 struct drm_i915_gem_get_tiling gem_get_tiling;
428
429 ret = drv_prime_bo_import(bo, data);
430 if (ret)
431 return ret;
432
433 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
434 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
435 gem_get_tiling.handle = bo->handles[0].u32;
436
437 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
438 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700439 drv_gem_bo_destroy(bo);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800440 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_GET_TILING failed.");
441 return ret;
442 }
443
444 bo->tiling = gem_get_tiling.tiling_mode;
445 return 0;
446}
447
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700448static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700449{
450 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800451 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700452
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800453 if (bo->tiling == I915_TILING_NONE) {
454 struct drm_i915_gem_mmap gem_map;
455 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700456
Gurchetan Singha1892b22017-09-28 16:40:52 -0700457 if ((bo->use_flags & BO_USE_SCANOUT) && !(bo->use_flags & BO_USE_RENDERSCRIPT))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700458 gem_map.flags = I915_MMAP_WC;
459
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800460 gem_map.handle = bo->handles[0].u32;
461 gem_map.offset = 0;
462 gem_map.size = bo->total_size;
463
464 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
465 if (ret) {
466 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP failed\n");
467 return MAP_FAILED;
468 }
469
470 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800471 } else {
472 struct drm_i915_gem_mmap_gtt gem_map;
473 memset(&gem_map, 0, sizeof(gem_map));
474
475 gem_map.handle = bo->handles[0].u32;
476
477 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
478 if (ret) {
479 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
480 return MAP_FAILED;
481 }
482
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700483 addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
484 gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800485 }
486
487 if (addr == MAP_FAILED) {
488 fprintf(stderr, "drv: i915 GEM mmap failed\n");
489 return addr;
490 }
491
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800492 data->length = bo->total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800493 return addr;
494}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700495
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700496static int i915_bo_invalidate(struct bo *bo, struct map_info *data)
497{
498 int ret;
499 struct drm_i915_gem_set_domain set_domain;
500
501 memset(&set_domain, 0, sizeof(set_domain));
502 set_domain.handle = bo->handles[0].u32;
503 if (bo->tiling == I915_TILING_NONE) {
504 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
505 if (data->map_flags & BO_MAP_WRITE)
506 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
507 } else {
508 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
509 if (data->map_flags & BO_MAP_WRITE)
510 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
511 }
512
513 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
514 if (ret) {
515 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
516 return ret;
517 }
518
519 return 0;
520}
521
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700522static int i915_bo_flush(struct bo *bo, struct map_info *data)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800523{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800524 struct i915_device *i915 = bo->drv->priv;
525 if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
526 i915_clflush(data->addr, data->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800527
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700528 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700529}
530
Gurchetan Singha1892b22017-09-28 16:40:52 -0700531static uint32_t i915_resolve_format(uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700532{
533 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800534 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900535 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700536 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900537 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700538 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800539 return DRM_FORMAT_XBGR8888;
540 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900541 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700542 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900543 return DRM_FORMAT_NV12;
Owen Linbbb69fd2017-06-05 14:33:08 +0800544 return DRM_FORMAT_YVU420;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700545 default:
546 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700547 }
548}
549
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700550const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700551 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700552 .init = i915_init,
553 .close = i915_close,
554 .bo_create = i915_bo_create,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700555 .bo_create_with_modifiers = i915_bo_create_with_modifiers,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800556 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800557 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700558 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700559 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700560 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700561 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700562 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700563};
564
565#endif