blob: 08487ff55305242f03dff7adbe59917b13e0969e [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>
Gurchetan Singhcc35e692019-02-28 15:44:54 -080016#include <unistd.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070017#include <xf86drm.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070018
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070019#include "drv_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070020#include "helpers.h"
21#include "util.h"
22
Gurchetan Singh68af9c22017-01-18 13:48:11 -080023#define I915_CACHELINE_SIZE 64
24#define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
25
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000026static const uint32_t scanout_render_formats[] = { DRM_FORMAT_ABGR2101010, DRM_FORMAT_ABGR8888,
27 DRM_FORMAT_ARGB2101010, DRM_FORMAT_ARGB8888,
28 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR2101010,
29 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB2101010,
30 DRM_FORMAT_XRGB8888 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080031
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000032static const uint32_t render_formats[] = { DRM_FORMAT_ABGR16161616F };
33
34static const uint32_t texture_only_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12, DRM_FORMAT_P010,
35 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
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
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000054static uint64_t unset_flags(uint64_t current_flags, uint64_t mask)
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070055{
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000056 uint64_t value = current_flags & ~mask;
57 return value;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080058}
59
60static int i915_add_combinations(struct driver *drv)
61{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080062 struct format_metadata metadata;
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000063 uint64_t render, scanout_and_render, texture_only;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070064
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000065 scanout_and_render = BO_USE_RENDER_MASK | BO_USE_SCANOUT;
66 render = BO_USE_RENDER_MASK;
67 texture_only = BO_USE_TEXTURE_MASK;
68 uint64_t linear_mask = BO_USE_RENDERSCRIPT | BO_USE_LINEAR | BO_USE_PROTECTED |
69 BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080070
71 metadata.tiling = I915_TILING_NONE;
72 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -070073 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080074
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000075 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
76 &metadata, scanout_and_render);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080077
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000078 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070079
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000080 drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata,
81 texture_only);
82
83 drv_modify_linear_combinations(drv);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +090084 /*
85 * Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the
86 * Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future.
87 */
88 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_ENCODER);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000089 /* IPU3 camera ISP supports only NV12 output. */
David Stevens6116b312019-09-03 10:49:50 +090090 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000091 BO_USE_HW_VIDEO_ENCODER | BO_USE_HW_VIDEO_DECODER |
92 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +090093
Gurchetan Singh71bc6652018-09-17 17:42:05 -070094 /* Android CTS tests require this. */
95 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
96
Tomasz Figad30c0a52017-07-05 17:50:18 +090097 /*
98 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +090099 * from camera and input/output from hardware decoder/encoder.
Tomasz Figad30c0a52017-07-05 17:50:18 +0900100 */
101 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900102 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
103 BO_USE_HW_VIDEO_ENCODER);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900104
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000105 render = unset_flags(render, linear_mask);
106 scanout_and_render = unset_flags(scanout_and_render, linear_mask);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800107
108 metadata.tiling = I915_TILING_X;
109 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900110 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800111
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000112 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
113 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
114 &metadata, scanout_and_render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700115
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800116 metadata.tiling = I915_TILING_Y;
117 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900118 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800119
Gurchetan Singh8d884742020-03-24 13:48:54 -0700120 scanout_and_render =
121 unset_flags(scanout_and_render, BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000122/* Support y-tiled NV12 and P010 for libva */
123#ifdef I915_SCANOUT_Y_TILED
124 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
125 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT);
126#else
Gurchetan Singh86ddfdc2018-09-17 17:13:45 -0700127 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
128 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000129#endif
130 scanout_and_render = unset_flags(scanout_and_render, BO_USE_SCANOUT);
Miguel Casascdb25542019-07-18 13:07:30 -0400131 drv_add_combination(drv, DRM_FORMAT_P010, &metadata,
132 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700133
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000134 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
135 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
136 &metadata, scanout_and_render);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800137 return 0;
138}
139
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800140static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
141 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700142{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700143 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700144 uint32_t horizontal_alignment;
145 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700146
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700147 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700148 default:
149 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700150 /*
151 * The Intel GPU doesn't need any alignment in linear mode,
152 * but libva requires the allocation stride to be aligned to
153 * 16 bytes and height to 4 rows. Further, we round up the
154 * horizontal alignment so that row start on a cache line (64
155 * bytes).
156 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700157 horizontal_alignment = 64;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700158 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700159 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800160
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700161 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700162 horizontal_alignment = 512;
163 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700164 break;
165
166 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700167 if (i915->gen == 3) {
168 horizontal_alignment = 512;
169 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800170 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700171 horizontal_alignment = 128;
172 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700173 }
174 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700175 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800176
David Stevens793675a2019-09-25 11:17:48 +0900177 *aligned_height = ALIGN(*aligned_height, vertical_alignment);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700178 if (i915->gen > 3) {
179 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800180 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700181 while (*stride > horizontal_alignment)
182 horizontal_alignment <<= 1;
183
184 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800185 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800186
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700187 if (i915->gen <= 3 && *stride > 8192)
188 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800189
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700190 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700191}
192
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800193static void i915_clflush(void *start, size_t size)
194{
195 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
196 void *end = (void *)((uintptr_t)start + size);
197
198 __builtin_ia32_mfence();
199 while (p < end) {
200 __builtin_ia32_clflush(p);
201 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
202 }
203}
204
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800205static int i915_init(struct driver *drv)
206{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800207 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800208 int device_id;
209 struct i915_device *i915;
210 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800211
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800212 i915 = calloc(1, sizeof(*i915));
213 if (!i915)
214 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800215
216 memset(&get_param, 0, sizeof(get_param));
217 get_param.param = I915_PARAM_CHIPSET_ID;
218 get_param.value = &device_id;
219 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
220 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700221 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800222 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800223 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800224 }
225
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800226 i915->gen = i915_get_gen(device_id);
227
228 memset(&get_param, 0, sizeof(get_param));
229 get_param.param = I915_PARAM_HAS_LLC;
230 get_param.value = &i915->has_llc;
231 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
232 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700233 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800234 free(i915);
235 return -EINVAL;
236 }
237
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800238 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800239
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800240 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800241}
242
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700243static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
244{
245 uint32_t offset;
246 size_t plane;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800247 int ret, pagesize;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700248
249 offset = 0;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800250 pagesize = getpagesize();
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700251 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
252 uint32_t stride = drv_stride_from_format(format, width, plane);
253 uint32_t plane_height = drv_height_from_format(format, height, plane);
254
Gurchetan Singh298b7572019-09-19 09:55:18 -0700255 if (bo->meta.tiling != I915_TILING_NONE)
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800256 assert(IS_ALIGNED(offset, pagesize));
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700257
Gurchetan Singh298b7572019-09-19 09:55:18 -0700258 ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700259 if (ret)
260 return ret;
261
Gurchetan Singh298b7572019-09-19 09:55:18 -0700262 bo->meta.strides[plane] = stride;
263 bo->meta.sizes[plane] = stride * plane_height;
264 bo->meta.offsets[plane] = offset;
265 offset += bo->meta.sizes[plane];
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700266 }
267
Gurchetan Singh298b7572019-09-19 09:55:18 -0700268 bo->meta.total_size = ALIGN(offset, pagesize);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700269
270 return 0;
271}
272
David Stevens26fe6822020-03-09 12:23:42 +0000273static int i915_bo_compute_metadata(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
274 uint64_t use_flags, const uint64_t *modifiers, uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700275{
David Stevens26fe6822020-03-09 12:23:42 +0000276 static const uint64_t modifier_order[] = {
David Stevens26fe6822020-03-09 12:23:42 +0000277 I915_FORMAT_MOD_Y_TILED,
278 I915_FORMAT_MOD_X_TILED,
279 DRM_FORMAT_MOD_LINEAR,
280 };
281 uint64_t modifier;
Sean Paula9d3f772020-05-19 10:17:07 -0400282 struct i915_device *i915 = bo->drv->priv;
283 bool huge_bo = (i915->gen <= 11) && (width > 4096);
David Stevens26fe6822020-03-09 12:23:42 +0000284
285 if (modifiers) {
286 modifier =
287 drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
288 } else {
289 struct combination *combo = drv_get_combination(bo->drv, format, use_flags);
290 if (!combo)
291 return -EINVAL;
292 modifier = combo->metadata.modifier;
293 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700294
Sean Paula9d3f772020-05-19 10:17:07 -0400295 /*
296 * i915 only supports linear/x-tiled above 4096 wide
297 */
298 if (huge_bo && modifier != I915_FORMAT_MOD_X_TILED && modifier != DRM_FORMAT_MOD_LINEAR) {
299 uint32_t i;
300 for (i = 0; modifiers && i < count; i++) {
301 if (modifiers[i] == I915_FORMAT_MOD_X_TILED)
302 break;
303 }
304 if (i == count)
305 modifier = DRM_FORMAT_MOD_LINEAR;
306 else
307 modifier = I915_FORMAT_MOD_X_TILED;
308 }
309
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700310 switch (modifier) {
311 case DRM_FORMAT_MOD_LINEAR:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700312 bo->meta.tiling = I915_TILING_NONE;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700313 break;
314 case I915_FORMAT_MOD_X_TILED:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700315 bo->meta.tiling = I915_TILING_X;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700316 break;
317 case I915_FORMAT_MOD_Y_TILED:
Mark Yacoubc9565642020-02-07 11:02:22 -0500318 case I915_FORMAT_MOD_Y_TILED_CCS:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700319 bo->meta.tiling = I915_TILING_Y;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700320 break;
321 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800322
Gurchetan Singh298b7572019-09-19 09:55:18 -0700323 bo->meta.format_modifiers[0] = modifier;
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800324
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700325 if (format == DRM_FORMAT_YVU420_ANDROID) {
326 /*
327 * We only need to be able to use this as a linear texture,
328 * which doesn't put any HW restrictions on how we lay it
329 * out. The Android format does require the stride to be a
330 * multiple of 16 and expects the Cr and Cb stride to be
331 * ALIGN(Y_stride / 2, 16), which we can make happen by
332 * aligning to 32 bytes here.
333 */
334 uint32_t stride = ALIGN(width, 32);
335 drv_bo_from_format(bo, stride, height, format);
Mark Yacoubc9565642020-02-07 11:02:22 -0500336 } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
337 /*
338 * For compressed surfaces, we need a color control surface
339 * (CCS). Color compression is only supported for Y tiled
340 * surfaces, and for each 32x16 tiles in the main surface we
341 * need a tile in the control surface. Y tiles are 128 bytes
342 * wide and 32 lines tall and we use that to first compute the
343 * width and height in tiles of the main surface. stride and
344 * height are already multiples of 128 and 32, respectively:
345 */
346 uint32_t stride = drv_stride_from_format(format, width, 0);
347 uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128);
348 uint32_t height_in_tiles = DIV_ROUND_UP(height, 32);
349 uint32_t size = width_in_tiles * height_in_tiles * 4096;
350 uint32_t offset = 0;
351
352 bo->meta.strides[0] = width_in_tiles * 128;
353 bo->meta.sizes[0] = size;
354 bo->meta.offsets[0] = offset;
355 offset += size;
356
357 /*
358 * Now, compute the width and height in tiles of the control
359 * surface by dividing and rounding up.
360 */
361 uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32);
362 uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16);
363 uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096;
364
365 /*
366 * With stride and height aligned to y tiles, offset is
367 * already a multiple of 4096, which is the required alignment
368 * of the CCS.
369 */
370 bo->meta.strides[1] = ccs_width_in_tiles * 128;
371 bo->meta.sizes[1] = ccs_size;
372 bo->meta.offsets[1] = offset;
373 offset += ccs_size;
374
375 bo->meta.num_planes = 2;
376 bo->meta.total_size = offset;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700377 } else {
378 i915_bo_from_format(bo, width, height, format);
379 }
David Stevens26fe6822020-03-09 12:23:42 +0000380 return 0;
381}
382
383static int i915_bo_create_from_metadata(struct bo *bo)
384{
385 int ret;
386 size_t plane;
387 struct drm_i915_gem_create gem_create;
388 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800389
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800390 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700391 gem_create.size = bo->meta.total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800392
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800393 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
394 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700395 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700396 return -errno;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700397 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700398
Gurchetan Singh298b7572019-09-19 09:55:18 -0700399 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800400 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400401
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800402 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
403 gem_set_tiling.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700404 gem_set_tiling.tiling_mode = bo->meta.tiling;
405 gem_set_tiling.stride = bo->meta.strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700406
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800407 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
408 if (ret) {
409 struct drm_gem_close gem_close;
410 memset(&gem_close, 0, sizeof(gem_close));
411 gem_close.handle = bo->handles[0].u32;
412 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800413
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700414 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700415 return -errno;
416 }
417
418 return 0;
419}
420
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800421static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800422{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800423 free(drv->priv);
424 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800425}
426
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800427static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
428{
429 int ret;
430 struct drm_i915_gem_get_tiling gem_get_tiling;
431
432 ret = drv_prime_bo_import(bo, data);
433 if (ret)
434 return ret;
435
436 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
437 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
438 gem_get_tiling.handle = bo->handles[0].u32;
439
440 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
441 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700442 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700443 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800444 return ret;
445 }
446
Gurchetan Singh298b7572019-09-19 09:55:18 -0700447 bo->meta.tiling = gem_get_tiling.tiling_mode;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800448 return 0;
449}
450
Gurchetan Singhee43c302017-11-14 18:20:27 -0800451static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700452{
453 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800454 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700455
Mark Yacoubc9565642020-02-07 11:02:22 -0500456 if (bo->meta.format_modifiers[0] == I915_FORMAT_MOD_Y_TILED_CCS)
457 return MAP_FAILED;
458
Gurchetan Singh298b7572019-09-19 09:55:18 -0700459 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800460 struct drm_i915_gem_mmap gem_map;
461 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700462
Tomasz Figa39eb9512018-11-01 00:45:31 +0900463 /* TODO(b/118799155): We don't seem to have a good way to
464 * detect the use cases for which WC mapping is really needed.
465 * The current heuristic seems overly coarse and may be slowing
466 * down some other use cases unnecessarily.
467 *
468 * For now, care must be taken not to use WC mappings for
469 * Renderscript and camera use cases, as they're
470 * performance-sensitive. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700471 if ((bo->meta.use_flags & BO_USE_SCANOUT) &&
472 !(bo->meta.use_flags &
Tomasz Figa39eb9512018-11-01 00:45:31 +0900473 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700474 gem_map.flags = I915_MMAP_WC;
475
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800476 gem_map.handle = bo->handles[0].u32;
477 gem_map.offset = 0;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700478 gem_map.size = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800479
480 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
481 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700482 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800483 return MAP_FAILED;
484 }
485
486 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800487 } else {
488 struct drm_i915_gem_mmap_gtt gem_map;
489 memset(&gem_map, 0, sizeof(gem_map));
490
491 gem_map.handle = bo->handles[0].u32;
492
493 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
494 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700495 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800496 return MAP_FAILED;
497 }
498
Gurchetan Singh298b7572019-09-19 09:55:18 -0700499 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED,
500 bo->drv->fd, gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800501 }
502
503 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700504 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800505 return addr;
506 }
507
Gurchetan Singh298b7572019-09-19 09:55:18 -0700508 vma->length = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800509 return addr;
510}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700511
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700512static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700513{
514 int ret;
515 struct drm_i915_gem_set_domain set_domain;
516
517 memset(&set_domain, 0, sizeof(set_domain));
518 set_domain.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700519 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700520 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700521 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700522 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
523 } else {
524 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700525 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700526 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
527 }
528
529 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
530 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700531 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700532 return ret;
533 }
534
535 return 0;
536}
537
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700538static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800539{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800540 struct i915_device *i915 = bo->drv->priv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700541 if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700542 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800543
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700544 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700545}
546
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700547static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700548{
549 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800550 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900551 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700552 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900553 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700554 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800555 return DRM_FORMAT_XBGR8888;
556 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900557 /*
558 * KBL camera subsystem requires NV12. Our other use cases
559 * don't care:
560 * - Hardware video supports NV12,
561 * - USB Camera HALv3 supports NV12,
562 * - USB Camera HALv1 doesn't use this format.
563 * Moreover, NV12 is preferred for video, due to overlay
564 * support on SKL+.
565 */
566 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700567 default:
568 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700569 }
570}
571
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700572const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700573 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700574 .init = i915_init,
575 .close = i915_close,
David Stevens26fe6822020-03-09 12:23:42 +0000576 .bo_compute_metadata = i915_bo_compute_metadata,
577 .bo_create_from_metadata = i915_bo_create_from_metadata,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800578 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800579 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700580 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700581 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700582 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700583 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700584 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700585};
586
587#endif