blob: da22dc5e832b9edbeb81b23db82be1a5bb63d6a8 [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
Tomasz Figab92e4f82017-06-22 16:52:43 +090029static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_R8,
30 DRM_FORMAT_UYVY, DRM_FORMAT_YUYV };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070031
Tomasz Figab92e4f82017-06-22 16:52:43 +090032static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID,
33 DRM_FORMAT_NV12 };
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 */
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -070061 for (i = 0; i < drv_array_size(drv->combos); i++) {
62 combo = (struct combination *)drv_array_at_idx(drv->combos, i);
Tomasz Figae821cc22017-07-08 15:53:11 +090063 if (combo->format != item->format)
64 continue;
65
Gurchetan Singhd118a0e2018-01-12 23:31:50 +000066 if (item->modifier == DRM_FORMAT_MOD_LINEAR &&
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;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -070087 uint32_t i;
88 struct drv_array *kms_items;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080089 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 Singhd3001452017-11-03 17:18:36 -070099 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
100 &metadata, render_use_flags);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800101
Gurchetan Singhd3001452017-11-03 17:18:36 -0700102 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
103 &metadata, texture_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700104
Gurchetan Singhd3001452017-11-03 17:18:36 -0700105 drv_add_combinations(drv, tileable_texture_source_formats,
106 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
107 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800108
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800109 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
110 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800111
Tomasz Figad30c0a52017-07-05 17:50:18 +0900112 /* IPU3 camera ISP supports only NV12 output. */
113 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900114 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900115 /*
116 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
117 * from camera.
118 */
119 drv_modify_combination(drv, DRM_FORMAT_R8, &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
Gurchetan Singha1892b22017-09-28 16:40:52 -0700122 render_use_flags &= ~BO_USE_RENDERSCRIPT;
123 render_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
124 render_use_flags &= ~BO_USE_SW_READ_OFTEN;
125 render_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700126
Gurchetan Singha1892b22017-09-28 16:40:52 -0700127 texture_use_flags &= ~BO_USE_RENDERSCRIPT;
128 texture_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
129 texture_use_flags &= ~BO_USE_SW_READ_OFTEN;
130 texture_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800131
132 metadata.tiling = I915_TILING_X;
133 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900134 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800135
Gurchetan Singhd3001452017-11-03 17:18:36 -0700136 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
137 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700138
Gurchetan Singhd3001452017-11-03 17:18:36 -0700139 drv_add_combinations(drv, tileable_texture_source_formats,
140 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
141 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800142
143 metadata.tiling = I915_TILING_Y;
144 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900145 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800146
Gurchetan Singhd3001452017-11-03 17:18:36 -0700147 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
148 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700149
Gurchetan Singhd3001452017-11-03 17:18:36 -0700150 drv_add_combinations(drv, tileable_texture_source_formats,
151 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
152 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800153
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700154 kms_items = drv_query_kms(drv);
155 if (!kms_items)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800156 return 0;
157
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700158 for (i = 0; i < drv_array_size(kms_items); i++) {
159 ret = i915_add_kms_item(drv, (struct kms_item *)drv_array_at_idx(kms_items, i));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800160 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700161 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800162 return ret;
163 }
164 }
165
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700166 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800167 return 0;
168}
169
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800170static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
171 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700172{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700173 struct i915_device *i915 = bo->drv->priv;
174 uint32_t horizontal_alignment = 4;
175 uint32_t vertical_alignment = 4;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700176
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700177 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700178 default:
179 case I915_TILING_NONE:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700180 horizontal_alignment = 64;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700181 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800182
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700183 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700184 horizontal_alignment = 512;
185 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700186 break;
187
188 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700189 if (i915->gen == 3) {
190 horizontal_alignment = 512;
191 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800192 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700193 horizontal_alignment = 128;
194 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700195 }
196 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700197 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800198
Tomasz Figa33615a52017-07-29 15:37:58 +0900199 /*
200 * The alignment calculated above is based on the full size luma plane and to have chroma
201 * planes properly aligned with subsampled formats, we need to multiply luma alignment by
202 * subsampling factor.
203 */
204 switch (bo->format) {
205 case DRM_FORMAT_YVU420_ANDROID:
206 case DRM_FORMAT_YVU420:
207 horizontal_alignment *= 2;
Gurchetan Singh7dcdff12017-09-14 13:04:11 -0700208 /* Fall through */
Tomasz Figa33615a52017-07-29 15:37:58 +0900209 case DRM_FORMAT_NV12:
210 vertical_alignment *= 2;
211 break;
212 }
213
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700214 *aligned_height = ALIGN(bo->height, vertical_alignment);
215 if (i915->gen > 3) {
216 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800217 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700218 while (*stride > horizontal_alignment)
219 horizontal_alignment <<= 1;
220
221 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800222 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800223
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700224 if (i915->gen <= 3 && *stride > 8192)
225 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800226
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700227 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700228}
229
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800230static void i915_clflush(void *start, size_t size)
231{
232 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
233 void *end = (void *)((uintptr_t)start + size);
234
235 __builtin_ia32_mfence();
236 while (p < end) {
237 __builtin_ia32_clflush(p);
238 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
239 }
240}
241
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800242static int i915_init(struct driver *drv)
243{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800244 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800245 int device_id;
246 struct i915_device *i915;
247 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800248
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800249 i915 = calloc(1, sizeof(*i915));
250 if (!i915)
251 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800252
253 memset(&get_param, 0, sizeof(get_param));
254 get_param.param = I915_PARAM_CHIPSET_ID;
255 get_param.value = &device_id;
256 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
257 if (ret) {
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800258 fprintf(stderr, "drv: Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800259 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800260 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800261 }
262
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800263 i915->gen = i915_get_gen(device_id);
264
265 memset(&get_param, 0, sizeof(get_param));
266 get_param.param = I915_PARAM_HAS_LLC;
267 get_param.value = &i915->has_llc;
268 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
269 if (ret) {
270 fprintf(stderr, "drv: Failed to get I915_PARAM_HAS_LLC\n");
271 free(i915);
272 return -EINVAL;
273 }
274
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800275 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800276
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800277 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800278}
279
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700280static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
281 uint32_t format, uint64_t modifier)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700282{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700283 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800284 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700285 uint32_t stride;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800286 struct drm_i915_gem_create gem_create;
287 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700288
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700289 switch (modifier) {
290 case DRM_FORMAT_MOD_LINEAR:
291 bo->tiling = I915_TILING_NONE;
292 break;
293 case I915_FORMAT_MOD_X_TILED:
294 bo->tiling = I915_TILING_X;
295 break;
296 case I915_FORMAT_MOD_Y_TILED:
297 bo->tiling = I915_TILING_Y;
298 break;
299 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800300
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800301 bo->format_modifiers[0] = modifier;
302
Owen Linbbb69fd2017-06-05 14:33:08 +0800303 stride = drv_stride_from_format(format, width, 0);
Gurchetan Singh507f5dd2017-03-16 13:14:30 -0700304
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800305 ret = i915_align_dimensions(bo, bo->tiling, &stride, &height);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700306 if (ret)
307 return ret;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800308
Owen Linbbb69fd2017-06-05 14:33:08 +0800309 /*
Tomasz Figad846de62017-07-29 15:47:54 +0900310 * HAL_PIXEL_FORMAT_YV12 requires the buffer height not be aligned, but we need to keep
311 * total size as with aligned height to ensure enough padding space after each plane to
312 * satisfy GPU alignment requirements.
313 *
314 * We do it by first calling drv_bo_from_format() with aligned height and
315 * DRM_FORMAT_YVU420, which allows height alignment, saving the total size it calculates
316 * and then calling it again with requested parameters.
317 *
318 * This relies on the fact that i965 driver uses separate surfaces for each plane and
319 * contents of padding bytes is not affected, as it is only used to satisfy GPU cache
320 * requests.
321 *
322 * This is enforced by Mesa in src/intel/isl/isl_gen8.c, inside
323 * isl_gen8_choose_image_alignment_el(), which is used for GEN9 and GEN8.
Owen Linbbb69fd2017-06-05 14:33:08 +0800324 */
Tomasz Figad846de62017-07-29 15:47:54 +0900325 if (format == DRM_FORMAT_YVU420_ANDROID) {
326 uint32_t unaligned_height = bo->height;
327 size_t total_size;
Owen Linbbb69fd2017-06-05 14:33:08 +0800328
Tomasz Figad846de62017-07-29 15:47:54 +0900329 drv_bo_from_format(bo, stride, height, DRM_FORMAT_YVU420);
330 total_size = bo->total_size;
331 drv_bo_from_format(bo, stride, unaligned_height, format);
332 bo->total_size = total_size;
333 } else {
334 drv_bo_from_format(bo, stride, height, format);
335 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800336
Tomasz Figa581f3a52017-07-23 15:02:19 +0900337 /*
338 * Quoting Mesa ISL library:
339 *
340 * - For linear surfaces, additional padding of 64 bytes is required at
341 * the bottom of the surface. This is in addition to the padding
342 * required above.
343 */
344 if (bo->tiling == I915_TILING_NONE)
345 bo->total_size += 64;
346
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800347 memset(&gem_create, 0, sizeof(gem_create));
348 gem_create.size = bo->total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800349
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800350 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
351 if (ret) {
352 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n",
353 gem_create.size);
354 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700355 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700356
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800357 for (plane = 0; plane < bo->num_planes; plane++)
358 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400359
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800360 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
361 gem_set_tiling.handle = bo->handles[0].u32;
362 gem_set_tiling.tiling_mode = bo->tiling;
363 gem_set_tiling.stride = bo->strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700364
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800365 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
366 if (ret) {
367 struct drm_gem_close gem_close;
368 memset(&gem_close, 0, sizeof(gem_close));
369 gem_close.handle = bo->handles[0].u32;
370 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800371
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800372 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_TILING failed with %d", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700373 return -errno;
374 }
375
376 return 0;
377}
378
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700379static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
380 uint64_t use_flags)
381{
382 struct combination *combo;
383
384 combo = drv_get_combination(bo->drv, format, use_flags);
385 if (!combo)
386 return -EINVAL;
387
388 return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
389}
390
391static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
392 uint32_t format, const uint64_t *modifiers, uint32_t count)
393{
394 static const uint64_t modifier_order[] = {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800395 I915_FORMAT_MOD_Y_TILED,
396 I915_FORMAT_MOD_X_TILED,
397 DRM_FORMAT_MOD_LINEAR,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700398 };
399 uint64_t modifier;
400
401 modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
402
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700403 return i915_bo_create_for_modifier(bo, width, height, format, modifier);
404}
405
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800406static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800407{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800408 free(drv->priv);
409 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800410}
411
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800412static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
413{
414 int ret;
415 struct drm_i915_gem_get_tiling gem_get_tiling;
416
417 ret = drv_prime_bo_import(bo, data);
418 if (ret)
419 return ret;
420
421 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
422 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
423 gem_get_tiling.handle = bo->handles[0].u32;
424
425 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
426 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700427 drv_gem_bo_destroy(bo);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800428 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_GET_TILING failed.");
429 return ret;
430 }
431
432 bo->tiling = gem_get_tiling.tiling_mode;
433 return 0;
434}
435
Gurchetan Singhee43c302017-11-14 18:20:27 -0800436static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700437{
438 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800439 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700440
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800441 if (bo->tiling == I915_TILING_NONE) {
442 struct drm_i915_gem_mmap gem_map;
443 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700444
Gurchetan Singha1892b22017-09-28 16:40:52 -0700445 if ((bo->use_flags & BO_USE_SCANOUT) && !(bo->use_flags & BO_USE_RENDERSCRIPT))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700446 gem_map.flags = I915_MMAP_WC;
447
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800448 gem_map.handle = bo->handles[0].u32;
449 gem_map.offset = 0;
450 gem_map.size = bo->total_size;
451
452 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
453 if (ret) {
454 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP failed\n");
455 return MAP_FAILED;
456 }
457
458 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800459 } else {
460 struct drm_i915_gem_mmap_gtt gem_map;
461 memset(&gem_map, 0, sizeof(gem_map));
462
463 gem_map.handle = bo->handles[0].u32;
464
465 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
466 if (ret) {
467 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
468 return MAP_FAILED;
469 }
470
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700471 addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
472 gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800473 }
474
475 if (addr == MAP_FAILED) {
476 fprintf(stderr, "drv: i915 GEM mmap failed\n");
477 return addr;
478 }
479
Gurchetan Singhee43c302017-11-14 18:20:27 -0800480 vma->length = bo->total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800481 return addr;
482}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700483
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700484static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700485{
486 int ret;
487 struct drm_i915_gem_set_domain set_domain;
488
489 memset(&set_domain, 0, sizeof(set_domain));
490 set_domain.handle = bo->handles[0].u32;
491 if (bo->tiling == I915_TILING_NONE) {
492 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700493 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700494 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
495 } else {
496 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700497 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700498 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
499 }
500
501 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
502 if (ret) {
503 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
504 return ret;
505 }
506
507 return 0;
508}
509
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700510static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800511{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800512 struct i915_device *i915 = bo->drv->priv;
513 if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700514 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800515
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700516 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700517}
518
Gurchetan Singha1892b22017-09-28 16:40:52 -0700519static uint32_t i915_resolve_format(uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700520{
521 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800522 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900523 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700524 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900525 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700526 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800527 return DRM_FORMAT_XBGR8888;
528 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900529 /*
530 * KBL camera subsystem requires NV12. Our other use cases
531 * don't care:
532 * - Hardware video supports NV12,
533 * - USB Camera HALv3 supports NV12,
534 * - USB Camera HALv1 doesn't use this format.
535 * Moreover, NV12 is preferred for video, due to overlay
536 * support on SKL+.
537 */
538 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700539 default:
540 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700541 }
542}
543
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700544const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700545 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700546 .init = i915_init,
547 .close = i915_close,
548 .bo_create = i915_bo_create,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700549 .bo_create_with_modifiers = i915_bo_create_with_modifiers,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800550 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800551 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700552 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700553 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700554 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700555 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700556 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700557};
558
559#endif