blob: bccc0b5cdfbf0abbfe2f0d91a43c377e62669ee9 [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 Hondafd8b8ab2020-06-16 15:28:56 +090084
Hirokazu Honda3bd681c2020-06-23 17:52:20 +090085 /* NV12 format for camera, display, decoding and encoding. */
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000086 /* IPU3 camera ISP supports only NV12 output. */
David Stevens6116b312019-09-03 10:49:50 +090087 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Hirokazu Honda3bd681c2020-06-23 17:52:20 +090088 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
89 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +090090
Gurchetan Singh71bc6652018-09-17 17:42:05 -070091 /* Android CTS tests require this. */
92 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
93
Tomasz Figad30c0a52017-07-05 17:50:18 +090094 /*
95 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +090096 * from camera and input/output from hardware decoder/encoder.
Tomasz Figad30c0a52017-07-05 17:50:18 +090097 */
98 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +090099 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
100 BO_USE_HW_VIDEO_ENCODER);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900101
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000102 render = unset_flags(render, linear_mask);
103 scanout_and_render = unset_flags(scanout_and_render, linear_mask);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800104
105 metadata.tiling = I915_TILING_X;
106 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900107 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800108
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000109 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
110 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
111 &metadata, scanout_and_render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700112
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800113 metadata.tiling = I915_TILING_Y;
114 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900115 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800116
Gurchetan Singh8d884742020-03-24 13:48:54 -0700117 scanout_and_render =
118 unset_flags(scanout_and_render, BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000119/* Support y-tiled NV12 and P010 for libva */
120#ifdef I915_SCANOUT_Y_TILED
121 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
122 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT);
123#else
Gurchetan Singh86ddfdc2018-09-17 17:13:45 -0700124 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
125 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000126#endif
127 scanout_and_render = unset_flags(scanout_and_render, BO_USE_SCANOUT);
Miguel Casascdb25542019-07-18 13:07:30 -0400128 drv_add_combination(drv, DRM_FORMAT_P010, &metadata,
129 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700130
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000131 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
132 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
133 &metadata, scanout_and_render);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800134 return 0;
135}
136
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800137static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
138 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700139{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700140 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700141 uint32_t horizontal_alignment;
142 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700143
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700144 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700145 default:
146 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700147 /*
148 * The Intel GPU doesn't need any alignment in linear mode,
149 * but libva requires the allocation stride to be aligned to
150 * 16 bytes and height to 4 rows. Further, we round up the
151 * horizontal alignment so that row start on a cache line (64
152 * bytes).
153 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700154 horizontal_alignment = 64;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700155 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700156 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800157
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700158 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700159 horizontal_alignment = 512;
160 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700161 break;
162
163 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700164 if (i915->gen == 3) {
165 horizontal_alignment = 512;
166 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800167 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700168 horizontal_alignment = 128;
169 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700170 }
171 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700172 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800173
David Stevens793675a2019-09-25 11:17:48 +0900174 *aligned_height = ALIGN(*aligned_height, vertical_alignment);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700175 if (i915->gen > 3) {
176 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800177 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700178 while (*stride > horizontal_alignment)
179 horizontal_alignment <<= 1;
180
181 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800182 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800183
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700184 if (i915->gen <= 3 && *stride > 8192)
185 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800186
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700187 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700188}
189
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800190static void i915_clflush(void *start, size_t size)
191{
192 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
193 void *end = (void *)((uintptr_t)start + size);
194
195 __builtin_ia32_mfence();
196 while (p < end) {
197 __builtin_ia32_clflush(p);
198 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
199 }
200}
201
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800202static int i915_init(struct driver *drv)
203{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800204 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800205 int device_id;
206 struct i915_device *i915;
207 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800208
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800209 i915 = calloc(1, sizeof(*i915));
210 if (!i915)
211 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800212
213 memset(&get_param, 0, sizeof(get_param));
214 get_param.param = I915_PARAM_CHIPSET_ID;
215 get_param.value = &device_id;
216 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
217 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700218 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800219 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800220 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800221 }
222
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800223 i915->gen = i915_get_gen(device_id);
224
225 memset(&get_param, 0, sizeof(get_param));
226 get_param.param = I915_PARAM_HAS_LLC;
227 get_param.value = &i915->has_llc;
228 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
229 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700230 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800231 free(i915);
232 return -EINVAL;
233 }
234
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800235 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800236
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800237 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800238}
239
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700240static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
241{
242 uint32_t offset;
243 size_t plane;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800244 int ret, pagesize;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700245
246 offset = 0;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800247 pagesize = getpagesize();
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700248 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
249 uint32_t stride = drv_stride_from_format(format, width, plane);
250 uint32_t plane_height = drv_height_from_format(format, height, plane);
251
Gurchetan Singh298b7572019-09-19 09:55:18 -0700252 if (bo->meta.tiling != I915_TILING_NONE)
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800253 assert(IS_ALIGNED(offset, pagesize));
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700254
Gurchetan Singh298b7572019-09-19 09:55:18 -0700255 ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700256 if (ret)
257 return ret;
258
Gurchetan Singh298b7572019-09-19 09:55:18 -0700259 bo->meta.strides[plane] = stride;
260 bo->meta.sizes[plane] = stride * plane_height;
261 bo->meta.offsets[plane] = offset;
262 offset += bo->meta.sizes[plane];
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700263 }
264
Gurchetan Singh298b7572019-09-19 09:55:18 -0700265 bo->meta.total_size = ALIGN(offset, pagesize);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700266
267 return 0;
268}
269
David Stevens26fe6822020-03-09 12:23:42 +0000270static int i915_bo_compute_metadata(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
271 uint64_t use_flags, const uint64_t *modifiers, uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700272{
David Stevens26fe6822020-03-09 12:23:42 +0000273 static const uint64_t modifier_order[] = {
David Stevens26fe6822020-03-09 12:23:42 +0000274 I915_FORMAT_MOD_Y_TILED,
275 I915_FORMAT_MOD_X_TILED,
276 DRM_FORMAT_MOD_LINEAR,
277 };
278 uint64_t modifier;
Sean Paula9d3f772020-05-19 10:17:07 -0400279 struct i915_device *i915 = bo->drv->priv;
280 bool huge_bo = (i915->gen <= 11) && (width > 4096);
David Stevens26fe6822020-03-09 12:23:42 +0000281
282 if (modifiers) {
283 modifier =
284 drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
285 } else {
286 struct combination *combo = drv_get_combination(bo->drv, format, use_flags);
287 if (!combo)
288 return -EINVAL;
289 modifier = combo->metadata.modifier;
290 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700291
Sean Paula9d3f772020-05-19 10:17:07 -0400292 /*
293 * i915 only supports linear/x-tiled above 4096 wide
294 */
295 if (huge_bo && modifier != I915_FORMAT_MOD_X_TILED && modifier != DRM_FORMAT_MOD_LINEAR) {
296 uint32_t i;
297 for (i = 0; modifiers && i < count; i++) {
298 if (modifiers[i] == I915_FORMAT_MOD_X_TILED)
299 break;
300 }
301 if (i == count)
302 modifier = DRM_FORMAT_MOD_LINEAR;
303 else
304 modifier = I915_FORMAT_MOD_X_TILED;
305 }
306
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700307 switch (modifier) {
308 case DRM_FORMAT_MOD_LINEAR:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700309 bo->meta.tiling = I915_TILING_NONE;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700310 break;
311 case I915_FORMAT_MOD_X_TILED:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700312 bo->meta.tiling = I915_TILING_X;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700313 break;
314 case I915_FORMAT_MOD_Y_TILED:
Mark Yacoubc9565642020-02-07 11:02:22 -0500315 case I915_FORMAT_MOD_Y_TILED_CCS:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700316 bo->meta.tiling = I915_TILING_Y;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700317 break;
318 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800319
Gurchetan Singh298b7572019-09-19 09:55:18 -0700320 bo->meta.format_modifiers[0] = modifier;
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800321
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700322 if (format == DRM_FORMAT_YVU420_ANDROID) {
323 /*
324 * We only need to be able to use this as a linear texture,
325 * which doesn't put any HW restrictions on how we lay it
326 * out. The Android format does require the stride to be a
327 * multiple of 16 and expects the Cr and Cb stride to be
328 * ALIGN(Y_stride / 2, 16), which we can make happen by
329 * aligning to 32 bytes here.
330 */
331 uint32_t stride = ALIGN(width, 32);
332 drv_bo_from_format(bo, stride, height, format);
Mark Yacoubc9565642020-02-07 11:02:22 -0500333 } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
334 /*
335 * For compressed surfaces, we need a color control surface
336 * (CCS). Color compression is only supported for Y tiled
337 * surfaces, and for each 32x16 tiles in the main surface we
338 * need a tile in the control surface. Y tiles are 128 bytes
339 * wide and 32 lines tall and we use that to first compute the
340 * width and height in tiles of the main surface. stride and
341 * height are already multiples of 128 and 32, respectively:
342 */
343 uint32_t stride = drv_stride_from_format(format, width, 0);
344 uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128);
345 uint32_t height_in_tiles = DIV_ROUND_UP(height, 32);
346 uint32_t size = width_in_tiles * height_in_tiles * 4096;
347 uint32_t offset = 0;
348
349 bo->meta.strides[0] = width_in_tiles * 128;
350 bo->meta.sizes[0] = size;
351 bo->meta.offsets[0] = offset;
352 offset += size;
353
354 /*
355 * Now, compute the width and height in tiles of the control
356 * surface by dividing and rounding up.
357 */
358 uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32);
359 uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16);
360 uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096;
361
362 /*
363 * With stride and height aligned to y tiles, offset is
364 * already a multiple of 4096, which is the required alignment
365 * of the CCS.
366 */
367 bo->meta.strides[1] = ccs_width_in_tiles * 128;
368 bo->meta.sizes[1] = ccs_size;
369 bo->meta.offsets[1] = offset;
370 offset += ccs_size;
371
372 bo->meta.num_planes = 2;
373 bo->meta.total_size = offset;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700374 } else {
375 i915_bo_from_format(bo, width, height, format);
376 }
David Stevens26fe6822020-03-09 12:23:42 +0000377 return 0;
378}
379
380static int i915_bo_create_from_metadata(struct bo *bo)
381{
382 int ret;
383 size_t plane;
384 struct drm_i915_gem_create gem_create;
385 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800386
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800387 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700388 gem_create.size = bo->meta.total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800389
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800390 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
391 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700392 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700393 return -errno;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700394 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700395
Gurchetan Singh298b7572019-09-19 09:55:18 -0700396 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800397 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400398
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800399 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
400 gem_set_tiling.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700401 gem_set_tiling.tiling_mode = bo->meta.tiling;
402 gem_set_tiling.stride = bo->meta.strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700403
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800404 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
405 if (ret) {
406 struct drm_gem_close gem_close;
407 memset(&gem_close, 0, sizeof(gem_close));
408 gem_close.handle = bo->handles[0].u32;
409 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800410
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700411 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700412 return -errno;
413 }
414
415 return 0;
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);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700440 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800441 return ret;
442 }
443
Gurchetan Singh298b7572019-09-19 09:55:18 -0700444 bo->meta.tiling = gem_get_tiling.tiling_mode;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800445 return 0;
446}
447
Gurchetan Singhee43c302017-11-14 18:20:27 -0800448static void *i915_bo_map(struct bo *bo, struct vma *vma, 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
Mark Yacoubc9565642020-02-07 11:02:22 -0500453 if (bo->meta.format_modifiers[0] == I915_FORMAT_MOD_Y_TILED_CCS)
454 return MAP_FAILED;
455
Gurchetan Singh298b7572019-09-19 09:55:18 -0700456 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800457 struct drm_i915_gem_mmap gem_map;
458 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700459
Tomasz Figa39eb9512018-11-01 00:45:31 +0900460 /* TODO(b/118799155): We don't seem to have a good way to
461 * detect the use cases for which WC mapping is really needed.
462 * The current heuristic seems overly coarse and may be slowing
463 * down some other use cases unnecessarily.
464 *
465 * For now, care must be taken not to use WC mappings for
466 * Renderscript and camera use cases, as they're
467 * performance-sensitive. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700468 if ((bo->meta.use_flags & BO_USE_SCANOUT) &&
469 !(bo->meta.use_flags &
Tomasz Figa39eb9512018-11-01 00:45:31 +0900470 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700471 gem_map.flags = I915_MMAP_WC;
472
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800473 gem_map.handle = bo->handles[0].u32;
474 gem_map.offset = 0;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700475 gem_map.size = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800476
477 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
478 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700479 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800480 return MAP_FAILED;
481 }
482
483 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800484 } else {
485 struct drm_i915_gem_mmap_gtt gem_map;
486 memset(&gem_map, 0, sizeof(gem_map));
487
488 gem_map.handle = bo->handles[0].u32;
489
490 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
491 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700492 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800493 return MAP_FAILED;
494 }
495
Gurchetan Singh298b7572019-09-19 09:55:18 -0700496 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED,
497 bo->drv->fd, gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800498 }
499
500 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700501 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800502 return addr;
503 }
504
Gurchetan Singh298b7572019-09-19 09:55:18 -0700505 vma->length = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800506 return addr;
507}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700508
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700509static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700510{
511 int ret;
512 struct drm_i915_gem_set_domain set_domain;
513
514 memset(&set_domain, 0, sizeof(set_domain));
515 set_domain.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700516 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700517 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700518 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700519 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
520 } else {
521 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
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_GTT;
524 }
525
526 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
527 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700528 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700529 return ret;
530 }
531
532 return 0;
533}
534
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700535static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800536{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800537 struct i915_device *i915 = bo->drv->priv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700538 if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700539 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800540
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700541 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700542}
543
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700544static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700545{
546 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800547 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900548 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700549 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900550 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700551 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800552 return DRM_FORMAT_XBGR8888;
553 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900554 /*
555 * KBL camera subsystem requires NV12. Our other use cases
556 * don't care:
557 * - Hardware video supports NV12,
558 * - USB Camera HALv3 supports NV12,
559 * - USB Camera HALv1 doesn't use this format.
560 * Moreover, NV12 is preferred for video, due to overlay
561 * support on SKL+.
562 */
563 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700564 default:
565 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700566 }
567}
568
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700569const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700570 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700571 .init = i915_init,
572 .close = i915_close,
David Stevens26fe6822020-03-09 12:23:42 +0000573 .bo_compute_metadata = i915_bo_compute_metadata,
574 .bo_create_from_metadata = i915_bo_create_from_metadata,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800575 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800576 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700577 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700578 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700579 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700580 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700581 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700582};
583
584#endif