blob: dafa753ecde521b73a5f7390e18f22bc9f7f5a93 [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;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700106 hw_protected = (i915->has_hw_protection) ? BO_USE_PROTECTED : 0;
107
Gurchetan Singhbbba9dd2020-10-12 17:31:10 -0700108 uint64_t linear_mask =
109 BO_USE_RENDERSCRIPT | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800110
111 metadata.tiling = I915_TILING_NONE;
112 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700113 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800114
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000115 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
116 &metadata, scanout_and_render);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800117
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000118 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700119
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000120 drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata,
121 texture_only);
122
123 drv_modify_linear_combinations(drv);
Hirokazu Hondafd8b8ab2020-06-16 15:28:56 +0900124
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900125 /* NV12 format for camera, display, decoding and encoding. */
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000126 /* IPU3 camera ISP supports only NV12 output. */
David Stevens6116b312019-09-03 10:49:50 +0900127 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900128 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700129 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
130 hw_protected);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +0900131
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700132 /* Android CTS tests require this. */
133 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
134
Tomasz Figad30c0a52017-07-05 17:50:18 +0900135 /*
136 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900137 * from camera and input/output from hardware decoder/encoder.
Tomasz Figad30c0a52017-07-05 17:50:18 +0900138 */
139 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
David Stevens49518142020-06-15 13:48:48 +0900140 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
141 BO_USE_HW_VIDEO_ENCODER);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900142
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000143 render = unset_flags(render, linear_mask);
144 scanout_and_render = unset_flags(scanout_and_render, linear_mask);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800145
146 metadata.tiling = I915_TILING_X;
147 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900148 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800149
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000150 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
151 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
152 &metadata, scanout_and_render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700153
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800154 metadata.tiling = I915_TILING_Y;
155 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900156 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800157
Gurchetan Singh8d884742020-03-24 13:48:54 -0700158 scanout_and_render =
159 unset_flags(scanout_and_render, BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000160/* Support y-tiled NV12 and P010 for libva */
161#ifdef I915_SCANOUT_Y_TILED
162 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700163 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT |
164 hw_protected);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000165#else
Gurchetan Singh86ddfdc2018-09-17 17:13:45 -0700166 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
167 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000168#endif
169 scanout_and_render = unset_flags(scanout_and_render, BO_USE_SCANOUT);
Miguel Casascdb25542019-07-18 13:07:30 -0400170 drv_add_combination(drv, DRM_FORMAT_P010, &metadata,
171 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700172
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000173 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
174 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
175 &metadata, scanout_and_render);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800176 return 0;
177}
178
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800179static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
180 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700181{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700182 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700183 uint32_t horizontal_alignment;
184 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700185
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700186 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700187 default:
188 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700189 /*
190 * The Intel GPU doesn't need any alignment in linear mode,
191 * but libva requires the allocation stride to be aligned to
192 * 16 bytes and height to 4 rows. Further, we round up the
193 * horizontal alignment so that row start on a cache line (64
194 * bytes).
195 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700196 horizontal_alignment = 64;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700197 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700198 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800199
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700200 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700201 horizontal_alignment = 512;
202 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700203 break;
204
205 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700206 if (i915->gen == 3) {
207 horizontal_alignment = 512;
208 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800209 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700210 horizontal_alignment = 128;
211 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700212 }
213 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700214 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800215
David Stevens793675a2019-09-25 11:17:48 +0900216 *aligned_height = ALIGN(*aligned_height, vertical_alignment);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700217 if (i915->gen > 3) {
218 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800219 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700220 while (*stride > horizontal_alignment)
221 horizontal_alignment <<= 1;
222
223 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800224 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800225
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700226 if (i915->gen <= 3 && *stride > 8192)
227 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800228
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700229 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700230}
231
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800232static void i915_clflush(void *start, size_t size)
233{
234 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
235 void *end = (void *)((uintptr_t)start + size);
236
237 __builtin_ia32_mfence();
238 while (p < end) {
239 __builtin_ia32_clflush(p);
240 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
241 }
242}
243
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800244static int i915_init(struct driver *drv)
245{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800246 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800247 int device_id;
248 struct i915_device *i915;
Gurchetan Singh99644382020-10-07 15:28:11 -0700249 drm_i915_getparam_t get_param = { 0 };
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800250
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800251 i915 = calloc(1, sizeof(*i915));
252 if (!i915)
253 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800254
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800255 get_param.param = I915_PARAM_CHIPSET_ID;
256 get_param.value = &device_id;
257 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
258 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700259 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800260 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800261 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800262 }
263
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800264 i915->gen = i915_get_gen(device_id);
Binu R S8d705182020-07-20 10:36:53 +0530265 i915_get_modifier_order(i915);
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800266
267 memset(&get_param, 0, sizeof(get_param));
268 get_param.param = I915_PARAM_HAS_LLC;
269 get_param.value = &i915->has_llc;
270 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
271 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700272 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800273 free(i915);
274 return -EINVAL;
275 }
276
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700277 if (i915->gen >= 12)
278 i915->has_hw_protection = 1;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800279
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700280 drv->priv = i915;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800281 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800282}
283
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700284static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
285{
286 uint32_t offset;
287 size_t plane;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800288 int ret, pagesize;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700289
290 offset = 0;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800291 pagesize = getpagesize();
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700292 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
293 uint32_t stride = drv_stride_from_format(format, width, plane);
294 uint32_t plane_height = drv_height_from_format(format, height, plane);
295
Gurchetan Singh298b7572019-09-19 09:55:18 -0700296 if (bo->meta.tiling != I915_TILING_NONE)
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800297 assert(IS_ALIGNED(offset, pagesize));
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700298
Gurchetan Singh298b7572019-09-19 09:55:18 -0700299 ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700300 if (ret)
301 return ret;
302
Gurchetan Singh298b7572019-09-19 09:55:18 -0700303 bo->meta.strides[plane] = stride;
304 bo->meta.sizes[plane] = stride * plane_height;
305 bo->meta.offsets[plane] = offset;
306 offset += bo->meta.sizes[plane];
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700307 }
308
Gurchetan Singh298b7572019-09-19 09:55:18 -0700309 bo->meta.total_size = ALIGN(offset, pagesize);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700310
311 return 0;
312}
313
David Stevens26fe6822020-03-09 12:23:42 +0000314static int i915_bo_compute_metadata(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
315 uint64_t use_flags, const uint64_t *modifiers, uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700316{
David Stevens26fe6822020-03-09 12:23:42 +0000317 uint64_t modifier;
Sean Paula9d3f772020-05-19 10:17:07 -0400318 struct i915_device *i915 = bo->drv->priv;
Abhishek Kumard39fe4e2020-10-09 16:08:01 +0530319 bool huge_bo = (i915->gen < 11) && (width > 4096);
David Stevens26fe6822020-03-09 12:23:42 +0000320
321 if (modifiers) {
322 modifier =
Binu R S8d705182020-07-20 10:36:53 +0530323 drv_pick_modifier(modifiers, count, i915->modifier.order, i915->modifier.count);
David Stevens26fe6822020-03-09 12:23:42 +0000324 } else {
325 struct combination *combo = drv_get_combination(bo->drv, format, use_flags);
326 if (!combo)
327 return -EINVAL;
328 modifier = combo->metadata.modifier;
329 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700330
Sean Paula9d3f772020-05-19 10:17:07 -0400331 /*
Abhishek Kumar6085bf32020-10-12 16:24:03 +0530332 * i915 only supports linear/x-tiled above 4096 wide on Gen9/Gen10 GPU.
333 * VAAPI decode in NV12 Y tiled format so skip modifier change for NV12/P010 huge bo.
Sean Paula9d3f772020-05-19 10:17:07 -0400334 */
Abhishek Kumar6085bf32020-10-12 16:24:03 +0530335 if (huge_bo && format != DRM_FORMAT_NV12 && format != DRM_FORMAT_P010 &&
336 modifier != I915_FORMAT_MOD_X_TILED && modifier != DRM_FORMAT_MOD_LINEAR) {
Sean Paula9d3f772020-05-19 10:17:07 -0400337 uint32_t i;
338 for (i = 0; modifiers && i < count; i++) {
339 if (modifiers[i] == I915_FORMAT_MOD_X_TILED)
340 break;
341 }
342 if (i == count)
343 modifier = DRM_FORMAT_MOD_LINEAR;
344 else
345 modifier = I915_FORMAT_MOD_X_TILED;
346 }
347
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700348 switch (modifier) {
349 case DRM_FORMAT_MOD_LINEAR:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700350 bo->meta.tiling = I915_TILING_NONE;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700351 break;
352 case I915_FORMAT_MOD_X_TILED:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700353 bo->meta.tiling = I915_TILING_X;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700354 break;
355 case I915_FORMAT_MOD_Y_TILED:
Mark Yacoubc9565642020-02-07 11:02:22 -0500356 case I915_FORMAT_MOD_Y_TILED_CCS:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700357 bo->meta.tiling = I915_TILING_Y;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700358 break;
359 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800360
Gurchetan Singh298b7572019-09-19 09:55:18 -0700361 bo->meta.format_modifiers[0] = modifier;
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800362
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700363 if (format == DRM_FORMAT_YVU420_ANDROID) {
364 /*
365 * We only need to be able to use this as a linear texture,
366 * which doesn't put any HW restrictions on how we lay it
367 * out. The Android format does require the stride to be a
368 * multiple of 16 and expects the Cr and Cb stride to be
369 * ALIGN(Y_stride / 2, 16), which we can make happen by
370 * aligning to 32 bytes here.
371 */
372 uint32_t stride = ALIGN(width, 32);
373 drv_bo_from_format(bo, stride, height, format);
Mark Yacoubc9565642020-02-07 11:02:22 -0500374 } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
375 /*
376 * For compressed surfaces, we need a color control surface
377 * (CCS). Color compression is only supported for Y tiled
378 * surfaces, and for each 32x16 tiles in the main surface we
379 * need a tile in the control surface. Y tiles are 128 bytes
380 * wide and 32 lines tall and we use that to first compute the
381 * width and height in tiles of the main surface. stride and
382 * height are already multiples of 128 and 32, respectively:
383 */
384 uint32_t stride = drv_stride_from_format(format, width, 0);
385 uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128);
386 uint32_t height_in_tiles = DIV_ROUND_UP(height, 32);
387 uint32_t size = width_in_tiles * height_in_tiles * 4096;
388 uint32_t offset = 0;
389
390 bo->meta.strides[0] = width_in_tiles * 128;
391 bo->meta.sizes[0] = size;
392 bo->meta.offsets[0] = offset;
393 offset += size;
394
395 /*
396 * Now, compute the width and height in tiles of the control
397 * surface by dividing and rounding up.
398 */
399 uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32);
400 uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16);
401 uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096;
402
403 /*
404 * With stride and height aligned to y tiles, offset is
405 * already a multiple of 4096, which is the required alignment
406 * of the CCS.
407 */
408 bo->meta.strides[1] = ccs_width_in_tiles * 128;
409 bo->meta.sizes[1] = ccs_size;
410 bo->meta.offsets[1] = offset;
411 offset += ccs_size;
412
413 bo->meta.num_planes = 2;
414 bo->meta.total_size = offset;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700415 } else {
416 i915_bo_from_format(bo, width, height, format);
417 }
David Stevens26fe6822020-03-09 12:23:42 +0000418 return 0;
419}
420
421static int i915_bo_create_from_metadata(struct bo *bo)
422{
423 int ret;
424 size_t plane;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700425 uint32_t gem_handle;
Gurchetan Singh99644382020-10-07 15:28:11 -0700426 struct drm_i915_gem_set_tiling gem_set_tiling = { 0 };
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700427 struct i915_device *i915 = bo->drv->priv;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800428
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700429 if (i915->has_hw_protection && (bo->meta.use_flags & BO_USE_PROTECTED)) {
430 struct drm_i915_gem_object_param protected_param = {
431 .param = I915_OBJECT_PARAM | I915_PARAM_PROTECTED_CONTENT,
432 .data = 1,
433 };
434
435 struct drm_i915_gem_create_ext_setparam setparam_protected = {
436 .base = { .name = I915_GEM_CREATE_EXT_SETPARAM },
437 .param = protected_param,
438 };
439
440 struct drm_i915_gem_create_ext create_ext = {
441 .size = bo->meta.total_size,
442 .extensions = (uintptr_t)&setparam_protected,
443 };
444
445 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE_EXT, &create_ext);
446 if (ret) {
447 drv_log("DRM_IOCTL_I915_GEM_CREATE_EXT failed (size=%llu)\n",
448 create_ext.size);
449 return -errno;
450 }
451
452 gem_handle = create_ext.handle;
453 } else {
454 struct drm_i915_gem_create gem_create = { 0 };
455 gem_create.size = bo->meta.total_size;
456 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
457 if (ret) {
458 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
459 return -errno;
460 }
461
462 gem_handle = gem_create.handle;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700463 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700464
Gurchetan Singh298b7572019-09-19 09:55:18 -0700465 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700466 bo->handles[plane].u32 = gem_handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400467
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800468 gem_set_tiling.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700469 gem_set_tiling.tiling_mode = bo->meta.tiling;
470 gem_set_tiling.stride = bo->meta.strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700471
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800472 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
473 if (ret) {
Gurchetan Singh99644382020-10-07 15:28:11 -0700474 struct drm_gem_close gem_close = { 0 };
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800475 gem_close.handle = bo->handles[0].u32;
476 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800477
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700478 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700479 return -errno;
480 }
481
482 return 0;
483}
484
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800485static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800486{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800487 free(drv->priv);
488 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800489}
490
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800491static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
492{
493 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700494 struct drm_i915_gem_get_tiling gem_get_tiling = { 0 };
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800495
496 ret = drv_prime_bo_import(bo, data);
497 if (ret)
498 return ret;
499
500 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800501 gem_get_tiling.handle = bo->handles[0].u32;
502
503 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
504 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700505 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700506 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800507 return ret;
508 }
509
Gurchetan Singh298b7572019-09-19 09:55:18 -0700510 bo->meta.tiling = gem_get_tiling.tiling_mode;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800511 return 0;
512}
513
Gurchetan Singhee43c302017-11-14 18:20:27 -0800514static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700515{
516 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800517 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700518
Mark Yacoubc9565642020-02-07 11:02:22 -0500519 if (bo->meta.format_modifiers[0] == I915_FORMAT_MOD_Y_TILED_CCS)
520 return MAP_FAILED;
521
Gurchetan Singh298b7572019-09-19 09:55:18 -0700522 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh99644382020-10-07 15:28:11 -0700523 struct drm_i915_gem_mmap gem_map = { 0 };
Tomasz Figa39eb9512018-11-01 00:45:31 +0900524 /* TODO(b/118799155): We don't seem to have a good way to
525 * detect the use cases for which WC mapping is really needed.
526 * The current heuristic seems overly coarse and may be slowing
527 * down some other use cases unnecessarily.
528 *
529 * For now, care must be taken not to use WC mappings for
530 * Renderscript and camera use cases, as they're
531 * performance-sensitive. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700532 if ((bo->meta.use_flags & BO_USE_SCANOUT) &&
533 !(bo->meta.use_flags &
Tomasz Figa39eb9512018-11-01 00:45:31 +0900534 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700535 gem_map.flags = I915_MMAP_WC;
536
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800537 gem_map.handle = bo->handles[0].u32;
538 gem_map.offset = 0;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700539 gem_map.size = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800540
541 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
542 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700543 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800544 return MAP_FAILED;
545 }
546
547 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800548 } else {
Gurchetan Singh99644382020-10-07 15:28:11 -0700549 struct drm_i915_gem_mmap_gtt gem_map = { 0 };
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800550
551 gem_map.handle = bo->handles[0].u32;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800552 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
553 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700554 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800555 return MAP_FAILED;
556 }
557
Gurchetan Singh298b7572019-09-19 09:55:18 -0700558 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED,
559 bo->drv->fd, gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800560 }
561
562 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700563 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800564 return addr;
565 }
566
Gurchetan Singh298b7572019-09-19 09:55:18 -0700567 vma->length = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800568 return addr;
569}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700570
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700571static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700572{
573 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700574 struct drm_i915_gem_set_domain set_domain = { 0 };
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700575
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700576 set_domain.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700577 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700578 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700579 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700580 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
581 } else {
582 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700583 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700584 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
585 }
586
587 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
588 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700589 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700590 return ret;
591 }
592
593 return 0;
594}
595
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700596static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800597{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800598 struct i915_device *i915 = bo->drv->priv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700599 if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700600 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800601
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700602 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700603}
604
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700605static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700606{
607 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800608 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900609 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700610 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900611 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700612 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800613 return DRM_FORMAT_XBGR8888;
614 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900615 /*
616 * KBL camera subsystem requires NV12. Our other use cases
617 * don't care:
618 * - Hardware video supports NV12,
619 * - USB Camera HALv3 supports NV12,
620 * - USB Camera HALv1 doesn't use this format.
621 * Moreover, NV12 is preferred for video, due to overlay
622 * support on SKL+.
623 */
624 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700625 default:
626 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700627 }
628}
629
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700630const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700631 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700632 .init = i915_init,
633 .close = i915_close,
David Stevens26fe6822020-03-09 12:23:42 +0000634 .bo_compute_metadata = i915_bo_compute_metadata,
635 .bo_create_from_metadata = i915_bo_create_from_metadata,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800636 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800637 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700638 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700639 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700640 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700641 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700642 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700643};
644
645#endif