blob: f7e2fd91856b8badf6f7f47026c01550910790d7 [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>
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070011#include <stdbool.h>
Gurchetan Singhcc015e82017-01-17 16:15:25 -080012#include <stdio.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070013#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070014#include <sys/mman.h>
Gurchetan Singhcc35e692019-02-28 15:44:54 -080015#include <unistd.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"
Gurchetan Singh13b00122020-10-07 14:31:20 -070019#include "external/i915_drm.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
Binu R S8d705182020-07-20 10:36:53 +053037static const uint64_t gen_modifier_order[] = { I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED,
38 DRM_FORMAT_MOD_LINEAR };
39
40static const uint64_t gen11_modifier_order[] = { I915_FORMAT_MOD_Y_TILED_CCS,
41 I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED,
42 DRM_FORMAT_MOD_LINEAR };
43
44struct modifier_support_t {
45 const uint64_t *order;
46 uint32_t count;
47};
48
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080049struct i915_device {
Gurchetan Singh68af9c22017-01-18 13:48:11 -080050 uint32_t gen;
51 int32_t has_llc;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -070052 int32_t has_hw_protection;
Binu R S8d705182020-07-20 10:36:53 +053053 struct modifier_support_t modifier;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070054};
55
Gurchetan Singh68af9c22017-01-18 13:48:11 -080056static uint32_t i915_get_gen(int device_id)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070057{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080058 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
59 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
Binu R S8d705182020-07-20 10:36:53 +053060 const uint16_t gen11_ids[] = { 0x4E71, 0x4E61, 0x4E51, 0x4E55, 0x4E57 };
Gurchetan Singh238001f2020-10-28 15:00:10 -070061 const uint16_t gen12_ids[] = { 0x9A40, 0x9A49, 0x9A59, 0x9A60, 0x9A68, 0x9A70,
62 0x9A78, 0x9AC0, 0x9AC9, 0x9AD9, 0x9AF8 };
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070063 unsigned i;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080064 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070065 if (gen3_ids[i] == device_id)
66 return 3;
Binu R S8d705182020-07-20 10:36:53 +053067 /* Gen 11 */
68 for (i = 0; i < ARRAY_SIZE(gen11_ids); i++)
69 if (gen11_ids[i] == device_id)
70 return 11;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070071
Sushma Venkatesh Reddy20604be2020-10-08 10:18:01 -070072 /* Gen 12 */
73 for (i = 0; i < ARRAY_SIZE(gen12_ids); i++)
74 if (gen12_ids[i] == device_id)
75 return 12;
76
Stéphane Marchesin25a26062014-09-12 16:18:59 -070077 return 4;
78}
79
Binu R S8d705182020-07-20 10:36:53 +053080static void i915_get_modifier_order(struct i915_device *i915)
81{
82 if (i915->gen == 11) {
83 i915->modifier.order = gen11_modifier_order;
84 i915->modifier.count = ARRAY_SIZE(gen11_modifier_order);
85 } else {
86 i915->modifier.order = gen_modifier_order;
87 i915->modifier.count = ARRAY_SIZE(gen_modifier_order);
88 }
89}
90
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000091static uint64_t unset_flags(uint64_t current_flags, uint64_t mask)
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070092{
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000093 uint64_t value = current_flags & ~mask;
94 return value;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080095}
96
97static int i915_add_combinations(struct driver *drv)
98{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080099 struct format_metadata metadata;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700100 uint64_t render, scanout_and_render, texture_only, hw_protected;
101 struct i915_device *i915 = drv->priv;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700102
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000103 scanout_and_render = BO_USE_RENDER_MASK | BO_USE_SCANOUT;
104 render = BO_USE_RENDER_MASK;
105 texture_only = BO_USE_TEXTURE_MASK;
Jeffrey Kardatzkedba19872020-12-04 16:58:28 -0800106 // HW protected buffers also need to be scanned out.
107 hw_protected = i915->has_hw_protection ? (BO_USE_PROTECTED | BO_USE_SCANOUT) : 0;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700108
Gurchetan Singhbbba9dd2020-10-12 17:31:10 -0700109 uint64_t linear_mask =
110 BO_USE_RENDERSCRIPT | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800111
112 metadata.tiling = I915_TILING_NONE;
113 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700114 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800115
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000116 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
117 &metadata, scanout_and_render);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800118
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000119 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700120
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000121 drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata,
122 texture_only);
123
124 drv_modify_linear_combinations(drv);
Hirokazu Hondafd8b8ab2020-06-16 15:28:56 +0900125
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900126 /* NV12 format for camera, display, decoding and encoding. */
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000127 /* IPU3 camera ISP supports only NV12 output. */
David Stevens6116b312019-09-03 10:49:50 +0900128 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900129 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700130 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
131 hw_protected);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +0900132
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700133 /* Android CTS tests require this. */
134 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
135
Tomasz Figad30c0a52017-07-05 17:50:18 +0900136 /*
137 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900138 * from camera and input/output from hardware decoder/encoder.
Tomasz Figad30c0a52017-07-05 17:50:18 +0900139 */
140 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900141 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
142 BO_USE_HW_VIDEO_ENCODER);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900143
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000144 render = unset_flags(render, linear_mask);
145 scanout_and_render = unset_flags(scanout_and_render, linear_mask);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800146
147 metadata.tiling = I915_TILING_X;
148 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900149 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800150
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000151 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
152 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
153 &metadata, scanout_and_render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700154
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800155 metadata.tiling = I915_TILING_Y;
156 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900157 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800158
Gurchetan Singh8d884742020-03-24 13:48:54 -0700159 scanout_and_render =
160 unset_flags(scanout_and_render, BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000161/* Support y-tiled NV12 and P010 for libva */
162#ifdef I915_SCANOUT_Y_TILED
Jeffrey Kardatzkedba19872020-12-04 16:58:28 -0800163 uint64_t nv12_usage =
164 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT | hw_protected;
165 uint64_t p010_usage = BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | hw_protected;
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000166#else
Jeffrey Kardatzkedba19872020-12-04 16:58:28 -0800167 uint64_t nv12_usage = BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER;
168 uint64_t p010_usage = nv12_usage;
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000169#endif
Jeffrey Kardatzkedba19872020-12-04 16:58:28 -0800170 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata, nv12_usage);
171 drv_add_combination(drv, DRM_FORMAT_P010, &metadata, p010_usage);
172
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000173 scanout_and_render = unset_flags(scanout_and_render, BO_USE_SCANOUT);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700174
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000175 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
176 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
177 &metadata, scanout_and_render);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800178 return 0;
179}
180
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800181static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
182 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700183{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700184 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700185 uint32_t horizontal_alignment;
186 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700187
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700188 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700189 default:
190 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700191 /*
192 * The Intel GPU doesn't need any alignment in linear mode,
193 * but libva requires the allocation stride to be aligned to
194 * 16 bytes and height to 4 rows. Further, we round up the
195 * horizontal alignment so that row start on a cache line (64
196 * bytes).
197 */
Dominik Behr1c6e70a2020-11-05 18:58:06 -0800198#ifdef LINEAR_ALIGN_256
199 /*
200 * If we want to import these buffers to amdgpu they need to
201 * their match LINEAR_ALIGNED requirement of 256 byte alignement.
202 */
203 horizontal_alignment = 256;
204#else
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700205 horizontal_alignment = 64;
Dominik Behr1c6e70a2020-11-05 18:58:06 -0800206#endif
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700207 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700208 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800209
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700210 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700211 horizontal_alignment = 512;
212 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700213 break;
214
215 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700216 if (i915->gen == 3) {
217 horizontal_alignment = 512;
218 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800219 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700220 horizontal_alignment = 128;
221 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700222 }
223 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700224 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800225
David Stevens793675a2019-09-25 11:17:48 +0900226 *aligned_height = ALIGN(*aligned_height, vertical_alignment);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700227 if (i915->gen > 3) {
228 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800229 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700230 while (*stride > horizontal_alignment)
231 horizontal_alignment <<= 1;
232
233 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800234 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800235
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700236 if (i915->gen <= 3 && *stride > 8192)
237 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800238
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700239 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700240}
241
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800242static void i915_clflush(void *start, size_t size)
243{
244 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
245 void *end = (void *)((uintptr_t)start + size);
246
247 __builtin_ia32_mfence();
248 while (p < end) {
249 __builtin_ia32_clflush(p);
250 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
251 }
252}
253
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800254static int i915_init(struct driver *drv)
255{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800256 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800257 int device_id;
258 struct i915_device *i915;
Gurchetan Singh99644382020-10-07 15:28:11 -0700259 drm_i915_getparam_t get_param = { 0 };
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800260
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800261 i915 = calloc(1, sizeof(*i915));
262 if (!i915)
263 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800264
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800265 get_param.param = I915_PARAM_CHIPSET_ID;
266 get_param.value = &device_id;
267 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
268 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700269 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800270 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800271 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800272 }
273
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800274 i915->gen = i915_get_gen(device_id);
Binu R S8d705182020-07-20 10:36:53 +0530275 i915_get_modifier_order(i915);
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800276
277 memset(&get_param, 0, sizeof(get_param));
278 get_param.param = I915_PARAM_HAS_LLC;
279 get_param.value = &i915->has_llc;
280 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
281 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700282 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800283 free(i915);
284 return -EINVAL;
285 }
286
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700287 if (i915->gen >= 12)
288 i915->has_hw_protection = 1;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800289
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700290 drv->priv = i915;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800291 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800292}
293
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700294static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
295{
296 uint32_t offset;
297 size_t plane;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800298 int ret, pagesize;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700299
300 offset = 0;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800301 pagesize = getpagesize();
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700302 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
303 uint32_t stride = drv_stride_from_format(format, width, plane);
304 uint32_t plane_height = drv_height_from_format(format, height, plane);
305
Gurchetan Singh298b7572019-09-19 09:55:18 -0700306 if (bo->meta.tiling != I915_TILING_NONE)
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800307 assert(IS_ALIGNED(offset, pagesize));
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700308
Gurchetan Singh298b7572019-09-19 09:55:18 -0700309 ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700310 if (ret)
311 return ret;
312
Gurchetan Singh298b7572019-09-19 09:55:18 -0700313 bo->meta.strides[plane] = stride;
314 bo->meta.sizes[plane] = stride * plane_height;
315 bo->meta.offsets[plane] = offset;
316 offset += bo->meta.sizes[plane];
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700317 }
318
Gurchetan Singh298b7572019-09-19 09:55:18 -0700319 bo->meta.total_size = ALIGN(offset, pagesize);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700320
321 return 0;
322}
323
David Stevens26fe6822020-03-09 12:23:42 +0000324static int i915_bo_compute_metadata(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
325 uint64_t use_flags, const uint64_t *modifiers, uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700326{
David Stevens26fe6822020-03-09 12:23:42 +0000327 uint64_t modifier;
Sean Paula9d3f772020-05-19 10:17:07 -0400328 struct i915_device *i915 = bo->drv->priv;
Abhishek Kumard39fe4e2020-10-09 16:08:01 +0530329 bool huge_bo = (i915->gen < 11) && (width > 4096);
David Stevens26fe6822020-03-09 12:23:42 +0000330
331 if (modifiers) {
332 modifier =
Binu R S8d705182020-07-20 10:36:53 +0530333 drv_pick_modifier(modifiers, count, i915->modifier.order, i915->modifier.count);
David Stevens26fe6822020-03-09 12:23:42 +0000334 } else {
335 struct combination *combo = drv_get_combination(bo->drv, format, use_flags);
336 if (!combo)
337 return -EINVAL;
338 modifier = combo->metadata.modifier;
339 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700340
Sean Paula9d3f772020-05-19 10:17:07 -0400341 /*
Abhishek Kumar6085bf32020-10-12 16:24:03 +0530342 * i915 only supports linear/x-tiled above 4096 wide on Gen9/Gen10 GPU.
343 * VAAPI decode in NV12 Y tiled format so skip modifier change for NV12/P010 huge bo.
Sean Paula9d3f772020-05-19 10:17:07 -0400344 */
Abhishek Kumar6085bf32020-10-12 16:24:03 +0530345 if (huge_bo && format != DRM_FORMAT_NV12 && format != DRM_FORMAT_P010 &&
346 modifier != I915_FORMAT_MOD_X_TILED && modifier != DRM_FORMAT_MOD_LINEAR) {
Sean Paula9d3f772020-05-19 10:17:07 -0400347 uint32_t i;
348 for (i = 0; modifiers && i < count; i++) {
349 if (modifiers[i] == I915_FORMAT_MOD_X_TILED)
350 break;
351 }
352 if (i == count)
353 modifier = DRM_FORMAT_MOD_LINEAR;
354 else
355 modifier = I915_FORMAT_MOD_X_TILED;
356 }
357
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500358 /*
359 * Skip I915_FORMAT_MOD_Y_TILED_CCS modifier if compression is disabled
360 * Pick y tiled modifier if it has been passed in, otherwise use linear
361 */
362 if (!bo->drv->compression && modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
363 uint32_t i;
364 for (i = 0; modifiers && i < count; i++) {
365 if (modifiers[i] == I915_FORMAT_MOD_Y_TILED)
366 break;
367 }
368 if (i == count)
369 modifier = DRM_FORMAT_MOD_LINEAR;
370 else
371 modifier = I915_FORMAT_MOD_Y_TILED;
372 }
373
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700374 switch (modifier) {
375 case DRM_FORMAT_MOD_LINEAR:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700376 bo->meta.tiling = I915_TILING_NONE;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700377 break;
378 case I915_FORMAT_MOD_X_TILED:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700379 bo->meta.tiling = I915_TILING_X;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700380 break;
381 case I915_FORMAT_MOD_Y_TILED:
Mark Yacoubc9565642020-02-07 11:02:22 -0500382 case I915_FORMAT_MOD_Y_TILED_CCS:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700383 bo->meta.tiling = I915_TILING_Y;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700384 break;
385 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800386
Gurchetan Singh298b7572019-09-19 09:55:18 -0700387 bo->meta.format_modifiers[0] = modifier;
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800388
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700389 if (format == DRM_FORMAT_YVU420_ANDROID) {
390 /*
391 * We only need to be able to use this as a linear texture,
392 * which doesn't put any HW restrictions on how we lay it
393 * out. The Android format does require the stride to be a
394 * multiple of 16 and expects the Cr and Cb stride to be
395 * ALIGN(Y_stride / 2, 16), which we can make happen by
396 * aligning to 32 bytes here.
397 */
398 uint32_t stride = ALIGN(width, 32);
399 drv_bo_from_format(bo, stride, height, format);
Mark Yacoubc9565642020-02-07 11:02:22 -0500400 } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
401 /*
402 * For compressed surfaces, we need a color control surface
403 * (CCS). Color compression is only supported for Y tiled
404 * surfaces, and for each 32x16 tiles in the main surface we
405 * need a tile in the control surface. Y tiles are 128 bytes
406 * wide and 32 lines tall and we use that to first compute the
407 * width and height in tiles of the main surface. stride and
408 * height are already multiples of 128 and 32, respectively:
409 */
410 uint32_t stride = drv_stride_from_format(format, width, 0);
411 uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128);
412 uint32_t height_in_tiles = DIV_ROUND_UP(height, 32);
413 uint32_t size = width_in_tiles * height_in_tiles * 4096;
414 uint32_t offset = 0;
415
416 bo->meta.strides[0] = width_in_tiles * 128;
417 bo->meta.sizes[0] = size;
418 bo->meta.offsets[0] = offset;
419 offset += size;
420
421 /*
422 * Now, compute the width and height in tiles of the control
423 * surface by dividing and rounding up.
424 */
425 uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32);
426 uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16);
427 uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096;
428
429 /*
430 * With stride and height aligned to y tiles, offset is
431 * already a multiple of 4096, which is the required alignment
432 * of the CCS.
433 */
434 bo->meta.strides[1] = ccs_width_in_tiles * 128;
435 bo->meta.sizes[1] = ccs_size;
436 bo->meta.offsets[1] = offset;
437 offset += ccs_size;
438
439 bo->meta.num_planes = 2;
440 bo->meta.total_size = offset;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700441 } else {
442 i915_bo_from_format(bo, width, height, format);
443 }
David Stevens26fe6822020-03-09 12:23:42 +0000444 return 0;
445}
446
447static int i915_bo_create_from_metadata(struct bo *bo)
448{
449 int ret;
450 size_t plane;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700451 uint32_t gem_handle;
Gurchetan Singh99644382020-10-07 15:28:11 -0700452 struct drm_i915_gem_set_tiling gem_set_tiling = { 0 };
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700453 struct i915_device *i915 = bo->drv->priv;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800454
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700455 if (i915->has_hw_protection && (bo->meta.use_flags & BO_USE_PROTECTED)) {
456 struct drm_i915_gem_object_param protected_param = {
457 .param = I915_OBJECT_PARAM | I915_PARAM_PROTECTED_CONTENT,
458 .data = 1,
459 };
460
461 struct drm_i915_gem_create_ext_setparam setparam_protected = {
462 .base = { .name = I915_GEM_CREATE_EXT_SETPARAM },
463 .param = protected_param,
464 };
465
466 struct drm_i915_gem_create_ext create_ext = {
467 .size = bo->meta.total_size,
468 .extensions = (uintptr_t)&setparam_protected,
469 };
470
471 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE_EXT, &create_ext);
472 if (ret) {
473 drv_log("DRM_IOCTL_I915_GEM_CREATE_EXT failed (size=%llu)\n",
474 create_ext.size);
475 return -errno;
476 }
477
478 gem_handle = create_ext.handle;
479 } else {
480 struct drm_i915_gem_create gem_create = { 0 };
481 gem_create.size = bo->meta.total_size;
482 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
483 if (ret) {
484 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
485 return -errno;
486 }
487
488 gem_handle = gem_create.handle;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700489 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700490
Gurchetan Singh298b7572019-09-19 09:55:18 -0700491 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700492 bo->handles[plane].u32 = gem_handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400493
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800494 gem_set_tiling.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700495 gem_set_tiling.tiling_mode = bo->meta.tiling;
496 gem_set_tiling.stride = bo->meta.strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700497
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800498 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
499 if (ret) {
Gurchetan Singh99644382020-10-07 15:28:11 -0700500 struct drm_gem_close gem_close = { 0 };
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800501 gem_close.handle = bo->handles[0].u32;
502 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800503
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700504 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700505 return -errno;
506 }
507
508 return 0;
509}
510
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800511static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800512{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800513 free(drv->priv);
514 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800515}
516
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800517static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
518{
519 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700520 struct drm_i915_gem_get_tiling gem_get_tiling = { 0 };
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800521
522 ret = drv_prime_bo_import(bo, data);
523 if (ret)
524 return ret;
525
526 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800527 gem_get_tiling.handle = bo->handles[0].u32;
528
529 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
530 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700531 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700532 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800533 return ret;
534 }
535
Gurchetan Singh298b7572019-09-19 09:55:18 -0700536 bo->meta.tiling = gem_get_tiling.tiling_mode;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800537 return 0;
538}
539
Gurchetan Singhee43c302017-11-14 18:20:27 -0800540static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700541{
542 int ret;
Kristian H. Kristensen8e9c2412020-11-19 19:20:04 +0000543 void *addr = MAP_FAILED;
Gurchetan Singhef920532016-08-12 16:38:25 -0700544
Mark Yacoubc9565642020-02-07 11:02:22 -0500545 if (bo->meta.format_modifiers[0] == I915_FORMAT_MOD_Y_TILED_CCS)
546 return MAP_FAILED;
547
Gurchetan Singh298b7572019-09-19 09:55:18 -0700548 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh99644382020-10-07 15:28:11 -0700549 struct drm_i915_gem_mmap gem_map = { 0 };
Tomasz Figa39eb9512018-11-01 00:45:31 +0900550 /* TODO(b/118799155): We don't seem to have a good way to
551 * detect the use cases for which WC mapping is really needed.
552 * The current heuristic seems overly coarse and may be slowing
553 * down some other use cases unnecessarily.
554 *
555 * For now, care must be taken not to use WC mappings for
556 * Renderscript and camera use cases, as they're
557 * performance-sensitive. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700558 if ((bo->meta.use_flags & BO_USE_SCANOUT) &&
559 !(bo->meta.use_flags &
Tomasz Figa39eb9512018-11-01 00:45:31 +0900560 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700561 gem_map.flags = I915_MMAP_WC;
562
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800563 gem_map.handle = bo->handles[0].u32;
564 gem_map.offset = 0;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700565 gem_map.size = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800566
567 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
Kristian H. Kristensen8e9c2412020-11-19 19:20:04 +0000568 /* DRM_IOCTL_I915_GEM_MMAP mmaps the underlying shm
569 * file and returns a user space address directly, ie,
570 * doesn't go through mmap. If we try that on a
571 * dma-buf that doesn't have a shm file, i915.ko
572 * returns ENXIO. Fall through to
573 * DRM_IOCTL_I915_GEM_MMAP_GTT in that case, which
574 * will mmap on the drm fd instead. */
575 if (ret == 0)
576 addr = (void *)(uintptr_t)gem_map.addr_ptr;
577 }
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800578
Kristian H. Kristensen8e9c2412020-11-19 19:20:04 +0000579 if (addr == MAP_FAILED) {
Gurchetan Singh99644382020-10-07 15:28:11 -0700580 struct drm_i915_gem_mmap_gtt gem_map = { 0 };
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800581
582 gem_map.handle = bo->handles[0].u32;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800583 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
584 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700585 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800586 return MAP_FAILED;
587 }
588
Gurchetan Singh298b7572019-09-19 09:55:18 -0700589 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED,
590 bo->drv->fd, gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800591 }
592
593 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700594 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800595 return addr;
596 }
597
Gurchetan Singh298b7572019-09-19 09:55:18 -0700598 vma->length = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800599 return addr;
600}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700601
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700602static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700603{
604 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700605 struct drm_i915_gem_set_domain set_domain = { 0 };
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700606
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700607 set_domain.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700608 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700609 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700610 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700611 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
612 } else {
613 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700614 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700615 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
616 }
617
618 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
619 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700620 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700621 return ret;
622 }
623
624 return 0;
625}
626
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700627static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800628{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800629 struct i915_device *i915 = bo->drv->priv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700630 if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700631 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800632
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700633 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700634}
635
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700636static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700637{
638 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800639 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900640 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700641 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900642 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700643 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800644 return DRM_FORMAT_XBGR8888;
645 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900646 /*
647 * KBL camera subsystem requires NV12. Our other use cases
648 * don't care:
649 * - Hardware video supports NV12,
650 * - USB Camera HALv3 supports NV12,
651 * - USB Camera HALv1 doesn't use this format.
652 * Moreover, NV12 is preferred for video, due to overlay
653 * support on SKL+.
654 */
655 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700656 default:
657 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700658 }
659}
660
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700661const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700662 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700663 .init = i915_init,
664 .close = i915_close,
David Stevens26fe6822020-03-09 12:23:42 +0000665 .bo_compute_metadata = i915_bo_compute_metadata,
666 .bo_create_from_metadata = i915_bo_create_from_metadata,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800667 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800668 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700669 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700670 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700671 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700672 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700673 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700674};
675
676#endif