blob: 3f6263823fe6c71b5619a19468841422e1f850cf [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
Kristian H. Kristensene8778f02018-04-04 14:21:41 -07009#include <assert.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070010#include <errno.h>
Gurchetan Singh82a8eed2017-01-03 13:01:37 -080011#include <i915_drm.h>
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070012#include <stdbool.h>
Gurchetan Singhcc015e82017-01-17 16:15:25 -080013#include <stdio.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070014#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070015#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070016#include <xf86drm.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070017
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070018#include "drv_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070019#include "helpers.h"
20#include "util.h"
21
Gurchetan Singh68af9c22017-01-18 13:48:11 -080022#define I915_CACHELINE_SIZE 64
23#define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
24
Gurchetan Singh767c5382018-05-05 00:42:12 +000025static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB1555,
Gurchetan Singhabe44f62018-06-06 17:01:51 -070026 DRM_FORMAT_ARGB8888, DRM_FORMAT_BGR888,
27 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR2101010,
28 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB1555,
29 DRM_FORMAT_XRGB2101010, DRM_FORMAT_XRGB8888 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080030
Tomasz Figab92e4f82017-06-22 16:52:43 +090031static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_R8,
32 DRM_FORMAT_UYVY, DRM_FORMAT_YUYV };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070033
Tomasz Figab92e4f82017-06-22 16:52:43 +090034static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID,
35 DRM_FORMAT_NV12 };
Gurchetan Singh179687e2016-10-28 10:07:35 -070036
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080037struct i915_device {
Gurchetan Singh68af9c22017-01-18 13:48:11 -080038 uint32_t gen;
39 int32_t has_llc;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070040};
41
Gurchetan Singh68af9c22017-01-18 13:48:11 -080042static uint32_t i915_get_gen(int device_id)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070043{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080044 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
45 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070046 unsigned i;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080047 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070048 if (gen3_ids[i] == device_id)
49 return 3;
50
51 return 4;
52}
53
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070054/*
55 * We allow allocation of ARGB formats for SCANOUT if the corresponding XRGB
56 * formats supports it. It's up to the caller (chrome ozone) to ultimately not
57 * scan out ARGB if the display controller only supports XRGB, but we'll allow
58 * the allocation of the bo here.
59 */
60static bool format_compatible(const struct combination *combo, uint32_t format)
61{
62 if (combo->format == format)
63 return true;
64
65 switch (format) {
66 case DRM_FORMAT_XRGB8888:
67 return combo->format == DRM_FORMAT_ARGB8888;
68 case DRM_FORMAT_XBGR8888:
69 return combo->format == DRM_FORMAT_ABGR8888;
70 case DRM_FORMAT_RGBX8888:
71 return combo->format == DRM_FORMAT_RGBA8888;
72 case DRM_FORMAT_BGRX8888:
73 return combo->format == DRM_FORMAT_BGRA8888;
74 default:
75 return false;
76 }
77}
78
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080079static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
80{
81 uint32_t i;
82 struct combination *combo;
83
84 /*
85 * Older hardware can't scanout Y-tiled formats. Newer devices can, and
86 * report this functionality via format modifiers.
87 */
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -070088 for (i = 0; i < drv_array_size(drv->combos); i++) {
89 combo = (struct combination *)drv_array_at_idx(drv->combos, i);
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070090 if (!format_compatible(combo, item->format))
Tomasz Figae821cc22017-07-08 15:53:11 +090091 continue;
92
Gurchetan Singhd118a0e2018-01-12 23:31:50 +000093 if (item->modifier == DRM_FORMAT_MOD_LINEAR &&
Tomasz Figae821cc22017-07-08 15:53:11 +090094 combo->metadata.tiling == I915_TILING_X) {
95 /*
96 * FIXME: drv_query_kms() does not report the available modifiers
97 * yet, but we know that all hardware can scanout from X-tiled
98 * buffers, so let's add this to our combinations, except for
99 * cursor, which must not be tiled.
100 */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700101 combo->use_flags |= item->use_flags & ~BO_USE_CURSOR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800102 }
Tomasz Figae821cc22017-07-08 15:53:11 +0900103
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700104 /* If we can scanout NV12, we support all tiling modes. */
105 if (item->format == DRM_FORMAT_NV12)
106 combo->use_flags |= item->use_flags;
107
Tomasz Figae821cc22017-07-08 15:53:11 +0900108 if (combo->metadata.modifier == item->modifier)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700109 combo->use_flags |= item->use_flags;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800110 }
111
112 return 0;
113}
114
115static int i915_add_combinations(struct driver *drv)
116{
117 int ret;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700118 uint32_t i;
119 struct drv_array *kms_items;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800120 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700121 uint64_t render_use_flags, texture_use_flags;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700122
Gurchetan Singha1892b22017-09-28 16:40:52 -0700123 render_use_flags = BO_USE_RENDER_MASK;
124 texture_use_flags = BO_USE_TEXTURE_MASK;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800125
126 metadata.tiling = I915_TILING_NONE;
127 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700128 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800129
Gurchetan Singhd3001452017-11-03 17:18:36 -0700130 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
131 &metadata, render_use_flags);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800132
Gurchetan Singhd3001452017-11-03 17:18:36 -0700133 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
134 &metadata, texture_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700135
Gurchetan Singhd3001452017-11-03 17:18:36 -0700136 drv_add_combinations(drv, tileable_texture_source_formats,
137 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
138 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800139
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800140 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
141 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800142
Tomasz Figad30c0a52017-07-05 17:50:18 +0900143 /* IPU3 camera ISP supports only NV12 output. */
144 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900145 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900146 /*
147 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
148 * from camera.
149 */
150 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900151 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900152
Gurchetan Singha1892b22017-09-28 16:40:52 -0700153 render_use_flags &= ~BO_USE_RENDERSCRIPT;
154 render_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
155 render_use_flags &= ~BO_USE_SW_READ_OFTEN;
156 render_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700157 render_use_flags &= ~BO_USE_PROTECTED;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700158
Gurchetan Singha1892b22017-09-28 16:40:52 -0700159 texture_use_flags &= ~BO_USE_RENDERSCRIPT;
160 texture_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
161 texture_use_flags &= ~BO_USE_SW_READ_OFTEN;
162 texture_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700163 texture_use_flags &= ~BO_USE_PROTECTED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800164
165 metadata.tiling = I915_TILING_X;
166 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900167 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800168
Gurchetan Singhd3001452017-11-03 17:18:36 -0700169 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
170 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700171
Gurchetan Singhd3001452017-11-03 17:18:36 -0700172 drv_add_combinations(drv, tileable_texture_source_formats,
173 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
174 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800175
176 metadata.tiling = I915_TILING_Y;
177 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900178 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800179
Gurchetan Singhd3001452017-11-03 17:18:36 -0700180 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
181 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700182
Gurchetan Singhd3001452017-11-03 17:18:36 -0700183 drv_add_combinations(drv, tileable_texture_source_formats,
184 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
185 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800186
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700187 /* Support y-tiled NV12 for libva */
188 const uint32_t nv12_format = DRM_FORMAT_NV12;
189 drv_add_combinations(drv, &nv12_format, 1, &metadata,
190 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
191
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700192 kms_items = drv_query_kms(drv);
193 if (!kms_items)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800194 return 0;
195
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700196 for (i = 0; i < drv_array_size(kms_items); i++) {
197 ret = i915_add_kms_item(drv, (struct kms_item *)drv_array_at_idx(kms_items, i));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800198 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700199 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800200 return ret;
201 }
202 }
203
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700204 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800205 return 0;
206}
207
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800208static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
209 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700210{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700211 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700212 uint32_t horizontal_alignment;
213 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700214
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700215 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700216 default:
217 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700218 /*
219 * The Intel GPU doesn't need any alignment in linear mode,
220 * but libva requires the allocation stride to be aligned to
221 * 16 bytes and height to 4 rows. Further, we round up the
222 * horizontal alignment so that row start on a cache line (64
223 * bytes).
224 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700225 horizontal_alignment = 64;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700226 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700227 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800228
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700229 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700230 horizontal_alignment = 512;
231 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700232 break;
233
234 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700235 if (i915->gen == 3) {
236 horizontal_alignment = 512;
237 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800238 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700239 horizontal_alignment = 128;
240 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700241 }
242 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700243 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800244
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700245 *aligned_height = ALIGN(bo->height, vertical_alignment);
246 if (i915->gen > 3) {
247 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800248 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700249 while (*stride > horizontal_alignment)
250 horizontal_alignment <<= 1;
251
252 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800253 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800254
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700255 if (i915->gen <= 3 && *stride > 8192)
256 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800257
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700258 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700259}
260
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800261static void i915_clflush(void *start, size_t size)
262{
263 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
264 void *end = (void *)((uintptr_t)start + size);
265
266 __builtin_ia32_mfence();
267 while (p < end) {
268 __builtin_ia32_clflush(p);
269 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
270 }
271}
272
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800273static int i915_init(struct driver *drv)
274{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800275 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800276 int device_id;
277 struct i915_device *i915;
278 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800279
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800280 i915 = calloc(1, sizeof(*i915));
281 if (!i915)
282 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800283
284 memset(&get_param, 0, sizeof(get_param));
285 get_param.param = I915_PARAM_CHIPSET_ID;
286 get_param.value = &device_id;
287 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
288 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700289 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800290 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800291 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800292 }
293
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800294 i915->gen = i915_get_gen(device_id);
295
296 memset(&get_param, 0, sizeof(get_param));
297 get_param.param = I915_PARAM_HAS_LLC;
298 get_param.value = &i915->has_llc;
299 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
300 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700301 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800302 free(i915);
303 return -EINVAL;
304 }
305
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800306 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800307
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800308 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800309}
310
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700311static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
312{
313 uint32_t offset;
314 size_t plane;
315 int ret;
316
317 offset = 0;
318 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
319 uint32_t stride = drv_stride_from_format(format, width, plane);
320 uint32_t plane_height = drv_height_from_format(format, height, plane);
321
322 if (bo->tiling != I915_TILING_NONE)
323 assert(IS_ALIGNED(offset, 4096));
324
325 ret = i915_align_dimensions(bo, bo->tiling, &stride, &plane_height);
326 if (ret)
327 return ret;
328
329 bo->strides[plane] = stride;
330 bo->sizes[plane] = stride * plane_height;
331 bo->offsets[plane] = offset;
332 offset += bo->sizes[plane];
333 }
334
335 bo->total_size = offset;
336
337 return 0;
338}
339
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700340static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
341 uint32_t format, uint64_t modifier)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700342{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700343 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800344 size_t plane;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800345 struct drm_i915_gem_create gem_create;
346 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700347
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700348 switch (modifier) {
349 case DRM_FORMAT_MOD_LINEAR:
350 bo->tiling = I915_TILING_NONE;
351 break;
352 case I915_FORMAT_MOD_X_TILED:
353 bo->tiling = I915_TILING_X;
354 break;
355 case I915_FORMAT_MOD_Y_TILED:
356 bo->tiling = I915_TILING_Y;
357 break;
358 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800359
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800360 bo->format_modifiers[0] = modifier;
361
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700362 if (format == DRM_FORMAT_YVU420_ANDROID) {
363 /*
364 * We only need to be able to use this as a linear texture,
365 * which doesn't put any HW restrictions on how we lay it
366 * out. The Android format does require the stride to be a
367 * multiple of 16 and expects the Cr and Cb stride to be
368 * ALIGN(Y_stride / 2, 16), which we can make happen by
369 * aligning to 32 bytes here.
370 */
371 uint32_t stride = ALIGN(width, 32);
372 drv_bo_from_format(bo, stride, height, format);
373 } else {
374 i915_bo_from_format(bo, width, height, format);
375 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800376
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800377 memset(&gem_create, 0, sizeof(gem_create));
378 gem_create.size = bo->total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800379
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800380 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
381 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700382 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800383 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700384 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700385
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800386 for (plane = 0; plane < bo->num_planes; plane++)
387 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400388
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800389 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
390 gem_set_tiling.handle = bo->handles[0].u32;
391 gem_set_tiling.tiling_mode = bo->tiling;
392 gem_set_tiling.stride = bo->strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700393
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800394 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
395 if (ret) {
396 struct drm_gem_close gem_close;
397 memset(&gem_close, 0, sizeof(gem_close));
398 gem_close.handle = bo->handles[0].u32;
399 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800400
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700401 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700402 return -errno;
403 }
404
405 return 0;
406}
407
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700408static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
409 uint64_t use_flags)
410{
411 struct combination *combo;
412
413 combo = drv_get_combination(bo->drv, format, use_flags);
414 if (!combo)
415 return -EINVAL;
416
417 return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
418}
419
420static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
421 uint32_t format, const uint64_t *modifiers, uint32_t count)
422{
423 static const uint64_t modifier_order[] = {
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700424 I915_FORMAT_MOD_Y_TILED,
425 I915_FORMAT_MOD_X_TILED,
426 DRM_FORMAT_MOD_LINEAR,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700427 };
428 uint64_t modifier;
429
430 modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
431
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700432 return i915_bo_create_for_modifier(bo, width, height, format, modifier);
433}
434
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800435static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800436{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800437 free(drv->priv);
438 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800439}
440
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800441static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
442{
443 int ret;
444 struct drm_i915_gem_get_tiling gem_get_tiling;
445
446 ret = drv_prime_bo_import(bo, data);
447 if (ret)
448 return ret;
449
450 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
451 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
452 gem_get_tiling.handle = bo->handles[0].u32;
453
454 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
455 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700456 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700457 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800458 return ret;
459 }
460
461 bo->tiling = gem_get_tiling.tiling_mode;
462 return 0;
463}
464
Gurchetan Singhee43c302017-11-14 18:20:27 -0800465static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700466{
467 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800468 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700469
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800470 if (bo->tiling == I915_TILING_NONE) {
471 struct drm_i915_gem_mmap gem_map;
472 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700473
Gurchetan Singha1892b22017-09-28 16:40:52 -0700474 if ((bo->use_flags & BO_USE_SCANOUT) && !(bo->use_flags & BO_USE_RENDERSCRIPT))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700475 gem_map.flags = I915_MMAP_WC;
476
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800477 gem_map.handle = bo->handles[0].u32;
478 gem_map.offset = 0;
479 gem_map.size = bo->total_size;
480
481 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
482 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700483 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800484 return MAP_FAILED;
485 }
486
487 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800488 } else {
489 struct drm_i915_gem_mmap_gtt gem_map;
490 memset(&gem_map, 0, sizeof(gem_map));
491
492 gem_map.handle = bo->handles[0].u32;
493
494 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
495 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700496 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800497 return MAP_FAILED;
498 }
499
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700500 addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
501 gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800502 }
503
504 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700505 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800506 return addr;
507 }
508
Gurchetan Singhee43c302017-11-14 18:20:27 -0800509 vma->length = bo->total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800510 return addr;
511}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700512
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700513static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700514{
515 int ret;
516 struct drm_i915_gem_set_domain set_domain;
517
518 memset(&set_domain, 0, sizeof(set_domain));
519 set_domain.handle = bo->handles[0].u32;
520 if (bo->tiling == I915_TILING_NONE) {
521 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700522 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700523 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
524 } else {
525 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700526 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700527 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
528 }
529
530 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
531 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700532 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700533 return ret;
534 }
535
536 return 0;
537}
538
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700539static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800540{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800541 struct i915_device *i915 = bo->drv->priv;
542 if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700543 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800544
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700545 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700546}
547
Gurchetan Singha1892b22017-09-28 16:40:52 -0700548static uint32_t i915_resolve_format(uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700549{
550 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800551 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900552 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700553 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900554 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700555 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800556 return DRM_FORMAT_XBGR8888;
557 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900558 /*
559 * KBL camera subsystem requires NV12. Our other use cases
560 * don't care:
561 * - Hardware video supports NV12,
562 * - USB Camera HALv3 supports NV12,
563 * - USB Camera HALv1 doesn't use this format.
564 * Moreover, NV12 is preferred for video, due to overlay
565 * support on SKL+.
566 */
567 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700568 default:
569 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700570 }
571}
572
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700573const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700574 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700575 .init = i915_init,
576 .close = i915_close,
577 .bo_create = i915_bo_create,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700578 .bo_create_with_modifiers = i915_bo_create_with_modifiers,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800579 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800580 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700581 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700582 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700583 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700584 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700585 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700586};
587
588#endif