blob: 92fd5b19da91ad462bfd6ea082154583610fab3c [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
99 * from camera.
100 */
101 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900102 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900103
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000104 render = unset_flags(render, linear_mask);
105 scanout_and_render = unset_flags(scanout_and_render, linear_mask);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800106
107 metadata.tiling = I915_TILING_X;
108 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900109 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800110
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000111 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
112 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
113 &metadata, scanout_and_render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700114
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800115 metadata.tiling = I915_TILING_Y;
116 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900117 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800118
Gurchetan Singh8d884742020-03-24 13:48:54 -0700119 scanout_and_render =
120 unset_flags(scanout_and_render, BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000121/* Support y-tiled NV12 and P010 for libva */
122#ifdef I915_SCANOUT_Y_TILED
123 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
124 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT);
125#else
Gurchetan Singh86ddfdc2018-09-17 17:13:45 -0700126 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
127 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000128#endif
129 scanout_and_render = unset_flags(scanout_and_render, BO_USE_SCANOUT);
Miguel Casascdb25542019-07-18 13:07:30 -0400130 drv_add_combination(drv, DRM_FORMAT_P010, &metadata,
131 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700132
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000133 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
134 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
135 &metadata, scanout_and_render);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800136 return 0;
137}
138
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800139static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
140 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700141{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700142 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700143 uint32_t horizontal_alignment;
144 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700145
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700146 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700147 default:
148 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700149 /*
150 * The Intel GPU doesn't need any alignment in linear mode,
151 * but libva requires the allocation stride to be aligned to
152 * 16 bytes and height to 4 rows. Further, we round up the
153 * horizontal alignment so that row start on a cache line (64
154 * bytes).
155 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700156 horizontal_alignment = 64;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700157 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700158 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800159
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700160 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700161 horizontal_alignment = 512;
162 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700163 break;
164
165 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700166 if (i915->gen == 3) {
167 horizontal_alignment = 512;
168 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800169 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700170 horizontal_alignment = 128;
171 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700172 }
173 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700174 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800175
David Stevens793675a2019-09-25 11:17:48 +0900176 *aligned_height = ALIGN(*aligned_height, vertical_alignment);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700177 if (i915->gen > 3) {
178 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800179 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700180 while (*stride > horizontal_alignment)
181 horizontal_alignment <<= 1;
182
183 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800184 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800185
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700186 if (i915->gen <= 3 && *stride > 8192)
187 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800188
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700189 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700190}
191
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800192static void i915_clflush(void *start, size_t size)
193{
194 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
195 void *end = (void *)((uintptr_t)start + size);
196
197 __builtin_ia32_mfence();
198 while (p < end) {
199 __builtin_ia32_clflush(p);
200 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
201 }
202}
203
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800204static int i915_init(struct driver *drv)
205{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800206 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800207 int device_id;
208 struct i915_device *i915;
209 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800210
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800211 i915 = calloc(1, sizeof(*i915));
212 if (!i915)
213 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800214
215 memset(&get_param, 0, sizeof(get_param));
216 get_param.param = I915_PARAM_CHIPSET_ID;
217 get_param.value = &device_id;
218 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
219 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700220 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800221 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800222 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800223 }
224
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800225 i915->gen = i915_get_gen(device_id);
226
227 memset(&get_param, 0, sizeof(get_param));
228 get_param.param = I915_PARAM_HAS_LLC;
229 get_param.value = &i915->has_llc;
230 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
231 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700232 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800233 free(i915);
234 return -EINVAL;
235 }
236
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800237 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800238
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800239 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800240}
241
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700242static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
243{
244 uint32_t offset;
245 size_t plane;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800246 int ret, pagesize;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700247
248 offset = 0;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800249 pagesize = getpagesize();
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700250 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
251 uint32_t stride = drv_stride_from_format(format, width, plane);
252 uint32_t plane_height = drv_height_from_format(format, height, plane);
253
Gurchetan Singh298b7572019-09-19 09:55:18 -0700254 if (bo->meta.tiling != I915_TILING_NONE)
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800255 assert(IS_ALIGNED(offset, pagesize));
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700256
Gurchetan Singh298b7572019-09-19 09:55:18 -0700257 ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700258 if (ret)
259 return ret;
260
Gurchetan Singh298b7572019-09-19 09:55:18 -0700261 bo->meta.strides[plane] = stride;
262 bo->meta.sizes[plane] = stride * plane_height;
263 bo->meta.offsets[plane] = offset;
264 offset += bo->meta.sizes[plane];
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700265 }
266
Gurchetan Singh298b7572019-09-19 09:55:18 -0700267 bo->meta.total_size = ALIGN(offset, pagesize);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700268
269 return 0;
270}
271
David Stevens26fe6822020-03-09 12:23:42 +0000272static int i915_bo_compute_metadata(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
273 uint64_t use_flags, const uint64_t *modifiers, uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700274{
David Stevens26fe6822020-03-09 12:23:42 +0000275 static const uint64_t modifier_order[] = {
David Stevens26fe6822020-03-09 12:23:42 +0000276 I915_FORMAT_MOD_Y_TILED,
277 I915_FORMAT_MOD_X_TILED,
278 DRM_FORMAT_MOD_LINEAR,
279 };
280 uint64_t modifier;
281
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
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700292 switch (modifier) {
293 case DRM_FORMAT_MOD_LINEAR:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700294 bo->meta.tiling = I915_TILING_NONE;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700295 break;
296 case I915_FORMAT_MOD_X_TILED:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700297 bo->meta.tiling = I915_TILING_X;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700298 break;
299 case I915_FORMAT_MOD_Y_TILED:
Mark Yacoubc9565642020-02-07 11:02:22 -0500300 case I915_FORMAT_MOD_Y_TILED_CCS:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700301 bo->meta.tiling = I915_TILING_Y;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700302 break;
303 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800304
Gurchetan Singh298b7572019-09-19 09:55:18 -0700305 bo->meta.format_modifiers[0] = modifier;
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800306
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700307 if (format == DRM_FORMAT_YVU420_ANDROID) {
308 /*
309 * We only need to be able to use this as a linear texture,
310 * which doesn't put any HW restrictions on how we lay it
311 * out. The Android format does require the stride to be a
312 * multiple of 16 and expects the Cr and Cb stride to be
313 * ALIGN(Y_stride / 2, 16), which we can make happen by
314 * aligning to 32 bytes here.
315 */
316 uint32_t stride = ALIGN(width, 32);
317 drv_bo_from_format(bo, stride, height, format);
Mark Yacoubc9565642020-02-07 11:02:22 -0500318 } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
319 /*
320 * For compressed surfaces, we need a color control surface
321 * (CCS). Color compression is only supported for Y tiled
322 * surfaces, and for each 32x16 tiles in the main surface we
323 * need a tile in the control surface. Y tiles are 128 bytes
324 * wide and 32 lines tall and we use that to first compute the
325 * width and height in tiles of the main surface. stride and
326 * height are already multiples of 128 and 32, respectively:
327 */
328 uint32_t stride = drv_stride_from_format(format, width, 0);
329 uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128);
330 uint32_t height_in_tiles = DIV_ROUND_UP(height, 32);
331 uint32_t size = width_in_tiles * height_in_tiles * 4096;
332 uint32_t offset = 0;
333
334 bo->meta.strides[0] = width_in_tiles * 128;
335 bo->meta.sizes[0] = size;
336 bo->meta.offsets[0] = offset;
337 offset += size;
338
339 /*
340 * Now, compute the width and height in tiles of the control
341 * surface by dividing and rounding up.
342 */
343 uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32);
344 uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16);
345 uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096;
346
347 /*
348 * With stride and height aligned to y tiles, offset is
349 * already a multiple of 4096, which is the required alignment
350 * of the CCS.
351 */
352 bo->meta.strides[1] = ccs_width_in_tiles * 128;
353 bo->meta.sizes[1] = ccs_size;
354 bo->meta.offsets[1] = offset;
355 offset += ccs_size;
356
357 bo->meta.num_planes = 2;
358 bo->meta.total_size = offset;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700359 } else {
360 i915_bo_from_format(bo, width, height, format);
361 }
David Stevens26fe6822020-03-09 12:23:42 +0000362 return 0;
363}
364
365static int i915_bo_create_from_metadata(struct bo *bo)
366{
367 int ret;
368 size_t plane;
369 struct drm_i915_gem_create gem_create;
370 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800371
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800372 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700373 gem_create.size = bo->meta.total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800374
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800375 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
376 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700377 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700378 return -errno;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700379 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700380
Gurchetan Singh298b7572019-09-19 09:55:18 -0700381 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800382 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400383
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800384 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
385 gem_set_tiling.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700386 gem_set_tiling.tiling_mode = bo->meta.tiling;
387 gem_set_tiling.stride = bo->meta.strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700388
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800389 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
390 if (ret) {
391 struct drm_gem_close gem_close;
392 memset(&gem_close, 0, sizeof(gem_close));
393 gem_close.handle = bo->handles[0].u32;
394 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800395
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700396 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700397 return -errno;
398 }
399
400 return 0;
401}
402
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800403static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800404{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800405 free(drv->priv);
406 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800407}
408
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800409static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
410{
411 int ret;
412 struct drm_i915_gem_get_tiling gem_get_tiling;
413
414 ret = drv_prime_bo_import(bo, data);
415 if (ret)
416 return ret;
417
418 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
419 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
420 gem_get_tiling.handle = bo->handles[0].u32;
421
422 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
423 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700424 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700425 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800426 return ret;
427 }
428
Gurchetan Singh298b7572019-09-19 09:55:18 -0700429 bo->meta.tiling = gem_get_tiling.tiling_mode;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800430 return 0;
431}
432
Gurchetan Singhee43c302017-11-14 18:20:27 -0800433static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700434{
435 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800436 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700437
Mark Yacoubc9565642020-02-07 11:02:22 -0500438 if (bo->meta.format_modifiers[0] == I915_FORMAT_MOD_Y_TILED_CCS)
439 return MAP_FAILED;
440
Gurchetan Singh298b7572019-09-19 09:55:18 -0700441 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800442 struct drm_i915_gem_mmap gem_map;
443 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700444
Tomasz Figa39eb9512018-11-01 00:45:31 +0900445 /* TODO(b/118799155): We don't seem to have a good way to
446 * detect the use cases for which WC mapping is really needed.
447 * The current heuristic seems overly coarse and may be slowing
448 * down some other use cases unnecessarily.
449 *
450 * For now, care must be taken not to use WC mappings for
451 * Renderscript and camera use cases, as they're
452 * performance-sensitive. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700453 if ((bo->meta.use_flags & BO_USE_SCANOUT) &&
454 !(bo->meta.use_flags &
Tomasz Figa39eb9512018-11-01 00:45:31 +0900455 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700456 gem_map.flags = I915_MMAP_WC;
457
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800458 gem_map.handle = bo->handles[0].u32;
459 gem_map.offset = 0;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700460 gem_map.size = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800461
462 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
463 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700464 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800465 return MAP_FAILED;
466 }
467
468 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800469 } else {
470 struct drm_i915_gem_mmap_gtt gem_map;
471 memset(&gem_map, 0, sizeof(gem_map));
472
473 gem_map.handle = bo->handles[0].u32;
474
475 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
476 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700477 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800478 return MAP_FAILED;
479 }
480
Gurchetan Singh298b7572019-09-19 09:55:18 -0700481 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED,
482 bo->drv->fd, gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800483 }
484
485 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700486 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800487 return addr;
488 }
489
Gurchetan Singh298b7572019-09-19 09:55:18 -0700490 vma->length = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800491 return addr;
492}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700493
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700494static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700495{
496 int ret;
497 struct drm_i915_gem_set_domain set_domain;
498
499 memset(&set_domain, 0, sizeof(set_domain));
500 set_domain.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700501 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700502 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700503 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700504 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
505 } else {
506 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700507 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700508 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
509 }
510
511 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
512 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700513 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700514 return ret;
515 }
516
517 return 0;
518}
519
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700520static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800521{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800522 struct i915_device *i915 = bo->drv->priv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700523 if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700524 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800525
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700526 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700527}
528
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700529static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700530{
531 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800532 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900533 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700534 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900535 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700536 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800537 return DRM_FORMAT_XBGR8888;
538 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900539 /*
540 * KBL camera subsystem requires NV12. Our other use cases
541 * don't care:
542 * - Hardware video supports NV12,
543 * - USB Camera HALv3 supports NV12,
544 * - USB Camera HALv1 doesn't use this format.
545 * Moreover, NV12 is preferred for video, due to overlay
546 * support on SKL+.
547 */
548 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700549 default:
550 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700551 }
552}
553
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700554const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700555 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700556 .init = i915_init,
557 .close = i915_close,
David Stevens26fe6822020-03-09 12:23:42 +0000558 .bo_compute_metadata = i915_bo_compute_metadata,
559 .bo_create_from_metadata = i915_bo_create_from_metadata,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800560 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800561 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700562 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700563 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700564 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700565 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700566 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700567};
568
569#endif