blob: dc43471bf4ad36a43409398dd0c134dad7743113 [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
Mark Yacoub6e277082020-12-07 16:36:17 -050037static const uint64_t gen_modifier_order[] = { I915_FORMAT_MOD_Y_TILED_CCS, I915_FORMAT_MOD_Y_TILED,
38 I915_FORMAT_MOD_X_TILED, DRM_FORMAT_MOD_LINEAR };
Binu R S8d705182020-07-20 10:36:53 +053039
Evan Green71097242021-01-28 12:08:50 -080040static const uint64_t gen11_modifier_order[] = { I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED,
Binu R S8d705182020-07-20 10:36:53 +053041 DRM_FORMAT_MOD_LINEAR };
42
43struct modifier_support_t {
44 const uint64_t *order;
45 uint32_t count;
46};
47
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080048struct i915_device {
Gurchetan Singh68af9c22017-01-18 13:48:11 -080049 uint32_t gen;
50 int32_t has_llc;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -070051 int32_t has_hw_protection;
Binu R S8d705182020-07-20 10:36:53 +053052 struct modifier_support_t modifier;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070053};
54
Gurchetan Singh68af9c22017-01-18 13:48:11 -080055static uint32_t i915_get_gen(int device_id)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070056{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080057 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
58 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
Binu R S8d705182020-07-20 10:36:53 +053059 const uint16_t gen11_ids[] = { 0x4E71, 0x4E61, 0x4E51, 0x4E55, 0x4E57 };
Gurchetan Singh238001f2020-10-28 15:00:10 -070060 const uint16_t gen12_ids[] = { 0x9A40, 0x9A49, 0x9A59, 0x9A60, 0x9A68, 0x9A70,
61 0x9A78, 0x9AC0, 0x9AC9, 0x9AD9, 0x9AF8 };
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070062 unsigned i;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080063 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070064 if (gen3_ids[i] == device_id)
65 return 3;
Binu R S8d705182020-07-20 10:36:53 +053066 /* Gen 11 */
67 for (i = 0; i < ARRAY_SIZE(gen11_ids); i++)
68 if (gen11_ids[i] == device_id)
69 return 11;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070070
Sushma Venkatesh Reddy20604be2020-10-08 10:18:01 -070071 /* Gen 12 */
72 for (i = 0; i < ARRAY_SIZE(gen12_ids); i++)
73 if (gen12_ids[i] == device_id)
74 return 12;
75
Stéphane Marchesin25a26062014-09-12 16:18:59 -070076 return 4;
77}
78
Binu R S8d705182020-07-20 10:36:53 +053079static void i915_get_modifier_order(struct i915_device *i915)
80{
81 if (i915->gen == 11) {
82 i915->modifier.order = gen11_modifier_order;
83 i915->modifier.count = ARRAY_SIZE(gen11_modifier_order);
84 } else {
85 i915->modifier.order = gen_modifier_order;
86 i915->modifier.count = ARRAY_SIZE(gen_modifier_order);
87 }
88}
89
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000090static uint64_t unset_flags(uint64_t current_flags, uint64_t mask)
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070091{
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +000092 uint64_t value = current_flags & ~mask;
93 return value;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080094}
95
96static int i915_add_combinations(struct driver *drv)
97{
Gurchetan Singhf98d1c12020-10-07 15:46:23 -070098 struct i915_device *i915 = drv->priv;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070099
Miguel Casasda47b7d2021-04-15 21:46:33 -0400100 const uint64_t scanout_and_render = BO_USE_RENDER_MASK | BO_USE_SCANOUT;
101 const uint64_t render = BO_USE_RENDER_MASK;
102 const uint64_t texture_only = BO_USE_TEXTURE_MASK;
Jeffrey Kardatzkedba19872020-12-04 16:58:28 -0800103 // HW protected buffers also need to be scanned out.
Miguel Casasda47b7d2021-04-15 21:46:33 -0400104 const uint64_t hw_protected =
105 i915->has_hw_protection ? (BO_USE_PROTECTED | BO_USE_SCANOUT) : 0;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700106
Miguel Casasda47b7d2021-04-15 21:46:33 -0400107 // TODO(mcasas): Consider adding BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY
108 // as well.
109 const uint64_t linear_mask =
Gurchetan Singhbbba9dd2020-10-12 17:31:10 -0700110 BO_USE_RENDERSCRIPT | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800111
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400112 struct format_metadata metadata_linear = {
113 .tiling = I915_TILING_NONE,
114 .priority = 1,
115 .modifier = DRM_FORMAT_MOD_LINEAR
116 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800117
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000118 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400119 &metadata_linear, scanout_and_render);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800120
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400121 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata_linear, render);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700122
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400123 drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata_linear,
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000124 texture_only);
125
126 drv_modify_linear_combinations(drv);
Hirokazu Hondafd8b8ab2020-06-16 15:28:56 +0900127
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900128 /* NV12 format for camera, display, decoding and encoding. */
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000129 /* IPU3 camera ISP supports only NV12 output. */
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400130 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata_linear,
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900131 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700132 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
133 hw_protected);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +0900134
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700135 /* Android CTS tests require this. */
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400136 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata_linear, BO_USE_SW_MASK);
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700137
Tomasz Figad30c0a52017-07-05 17:50:18 +0900138 /*
139 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900140 * from camera and input/output from hardware decoder/encoder.
Tomasz Figad30c0a52017-07-05 17:50:18 +0900141 */
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400142 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata_linear,
David Stevens49518142020-06-15 13:48:48 +0900143 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
144 BO_USE_HW_VIDEO_ENCODER);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900145
Miguel Casasda47b7d2021-04-15 21:46:33 -0400146 const uint64_t render_not_linear = unset_flags(render, linear_mask);
147 const uint64_t scanout_and_render_not_linear =
148 unset_flags(scanout_and_render, linear_mask);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800149
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400150 struct format_metadata metadata_x_tiled = {
151 .tiling = I915_TILING_X,
152 .priority = 2,
153 .modifier = I915_FORMAT_MOD_X_TILED
154 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800155
Miguel Casasda47b7d2021-04-15 21:46:33 -0400156 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata_x_tiled, render_not_linear);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000157 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
Miguel Casasda47b7d2021-04-15 21:46:33 -0400158 &metadata_x_tiled, scanout_and_render_not_linear);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700159
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400160 struct format_metadata metadata_y_tiled = {
161 .tiling = I915_TILING_Y,
162 .priority = 3,
163 .modifier = I915_FORMAT_MOD_Y_TILED
164 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800165
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000166/* Support y-tiled NV12 and P010 for libva */
167#ifdef I915_SCANOUT_Y_TILED
Miguel Casas231913e2021-04-06 19:17:25 -0400168 const uint64_t nv12_usage =
Jeffrey Kardatzkedba19872020-12-04 16:58:28 -0800169 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT | hw_protected;
Miguel Casas231913e2021-04-06 19:17:25 -0400170 const uint64_t p010_usage = BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | hw_protected |
171 (i915->gen >= 11 ? BO_USE_SCANOUT : 0);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000172#else
Miguel Casas231913e2021-04-06 19:17:25 -0400173 const uint64_t nv12_usage = BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER;
174 const uint64_t p010_usage = nv12_usage;
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000175#endif
Miguel Casasf2dc08e2021-04-15 20:51:24 -0400176 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata_y_tiled, nv12_usage);
177 drv_add_combination(drv, DRM_FORMAT_P010, &metadata_y_tiled, p010_usage);
Jeffrey Kardatzkedba19872020-12-04 16:58:28 -0800178
Miguel Casasda47b7d2021-04-15 21:46:33 -0400179 const uint64_t render_not_linear_nor_sw_read_write =
180 unset_flags(scanout_and_render_not_linear,
181 BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY | BO_USE_SCANOUT);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700182
Miguel Casasda47b7d2021-04-15 21:46:33 -0400183 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata_y_tiled, render_not_linear);
Ilja H. Friedelf39dcbc2020-02-26 02:50:51 +0000184 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
Miguel Casasda47b7d2021-04-15 21:46:33 -0400185 &metadata_y_tiled, render_not_linear_nor_sw_read_write);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800186 return 0;
187}
188
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800189static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
190 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700191{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700192 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700193 uint32_t horizontal_alignment;
194 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700195
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700196 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700197 default:
198 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700199 /*
200 * The Intel GPU doesn't need any alignment in linear mode,
201 * but libva requires the allocation stride to be aligned to
202 * 16 bytes and height to 4 rows. Further, we round up the
203 * horizontal alignment so that row start on a cache line (64
204 * bytes).
205 */
Dominik Behr1c6e70a2020-11-05 18:58:06 -0800206#ifdef LINEAR_ALIGN_256
207 /*
208 * If we want to import these buffers to amdgpu they need to
209 * their match LINEAR_ALIGNED requirement of 256 byte alignement.
210 */
211 horizontal_alignment = 256;
212#else
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700213 horizontal_alignment = 64;
Dominik Behr1c6e70a2020-11-05 18:58:06 -0800214#endif
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700215 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700216 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800217
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700218 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700219 horizontal_alignment = 512;
220 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700221 break;
222
223 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700224 if (i915->gen == 3) {
225 horizontal_alignment = 512;
226 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800227 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700228 horizontal_alignment = 128;
229 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700230 }
231 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700232 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800233
David Stevens793675a2019-09-25 11:17:48 +0900234 *aligned_height = ALIGN(*aligned_height, vertical_alignment);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700235 if (i915->gen > 3) {
236 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800237 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700238 while (*stride > horizontal_alignment)
239 horizontal_alignment <<= 1;
240
241 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800242 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800243
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700244 if (i915->gen <= 3 && *stride > 8192)
245 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800246
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700247 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700248}
249
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800250static void i915_clflush(void *start, size_t size)
251{
252 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
253 void *end = (void *)((uintptr_t)start + size);
254
255 __builtin_ia32_mfence();
256 while (p < end) {
257 __builtin_ia32_clflush(p);
258 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
259 }
260}
261
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800262static int i915_init(struct driver *drv)
263{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800264 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800265 int device_id;
266 struct i915_device *i915;
Gurchetan Singh99644382020-10-07 15:28:11 -0700267 drm_i915_getparam_t get_param = { 0 };
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800268
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800269 i915 = calloc(1, sizeof(*i915));
270 if (!i915)
271 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800272
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800273 get_param.param = I915_PARAM_CHIPSET_ID;
274 get_param.value = &device_id;
275 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
276 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700277 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800278 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800279 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800280 }
281
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800282 i915->gen = i915_get_gen(device_id);
Binu R S8d705182020-07-20 10:36:53 +0530283 i915_get_modifier_order(i915);
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800284
285 memset(&get_param, 0, sizeof(get_param));
286 get_param.param = I915_PARAM_HAS_LLC;
287 get_param.value = &i915->has_llc;
288 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
289 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700290 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800291 free(i915);
292 return -EINVAL;
293 }
294
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700295 if (i915->gen >= 12)
296 i915->has_hw_protection = 1;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800297
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700298 drv->priv = i915;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800299 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800300}
301
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700302static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
303{
304 uint32_t offset;
305 size_t plane;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800306 int ret, pagesize;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700307
308 offset = 0;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800309 pagesize = getpagesize();
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700310 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
311 uint32_t stride = drv_stride_from_format(format, width, plane);
312 uint32_t plane_height = drv_height_from_format(format, height, plane);
313
Gurchetan Singh298b7572019-09-19 09:55:18 -0700314 if (bo->meta.tiling != I915_TILING_NONE)
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800315 assert(IS_ALIGNED(offset, pagesize));
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700316
Gurchetan Singh298b7572019-09-19 09:55:18 -0700317 ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700318 if (ret)
319 return ret;
320
Gurchetan Singh298b7572019-09-19 09:55:18 -0700321 bo->meta.strides[plane] = stride;
322 bo->meta.sizes[plane] = stride * plane_height;
323 bo->meta.offsets[plane] = offset;
324 offset += bo->meta.sizes[plane];
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700325 }
326
Gurchetan Singh298b7572019-09-19 09:55:18 -0700327 bo->meta.total_size = ALIGN(offset, pagesize);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700328
329 return 0;
330}
331
David Stevens26fe6822020-03-09 12:23:42 +0000332static int i915_bo_compute_metadata(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
333 uint64_t use_flags, const uint64_t *modifiers, uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700334{
David Stevens26fe6822020-03-09 12:23:42 +0000335 uint64_t modifier;
Sean Paula9d3f772020-05-19 10:17:07 -0400336 struct i915_device *i915 = bo->drv->priv;
Abhishek Kumard39fe4e2020-10-09 16:08:01 +0530337 bool huge_bo = (i915->gen < 11) && (width > 4096);
David Stevens26fe6822020-03-09 12:23:42 +0000338
339 if (modifiers) {
340 modifier =
Binu R S8d705182020-07-20 10:36:53 +0530341 drv_pick_modifier(modifiers, count, i915->modifier.order, i915->modifier.count);
David Stevens26fe6822020-03-09 12:23:42 +0000342 } else {
343 struct combination *combo = drv_get_combination(bo->drv, format, use_flags);
344 if (!combo)
345 return -EINVAL;
346 modifier = combo->metadata.modifier;
347 }
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700348
Sean Paula9d3f772020-05-19 10:17:07 -0400349 /*
Abhishek Kumar6085bf32020-10-12 16:24:03 +0530350 * i915 only supports linear/x-tiled above 4096 wide on Gen9/Gen10 GPU.
351 * VAAPI decode in NV12 Y tiled format so skip modifier change for NV12/P010 huge bo.
Sean Paula9d3f772020-05-19 10:17:07 -0400352 */
Abhishek Kumar6085bf32020-10-12 16:24:03 +0530353 if (huge_bo && format != DRM_FORMAT_NV12 && format != DRM_FORMAT_P010 &&
354 modifier != I915_FORMAT_MOD_X_TILED && modifier != DRM_FORMAT_MOD_LINEAR) {
Sean Paula9d3f772020-05-19 10:17:07 -0400355 uint32_t i;
356 for (i = 0; modifiers && i < count; i++) {
357 if (modifiers[i] == I915_FORMAT_MOD_X_TILED)
358 break;
359 }
360 if (i == count)
361 modifier = DRM_FORMAT_MOD_LINEAR;
362 else
363 modifier = I915_FORMAT_MOD_X_TILED;
364 }
365
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500366 /*
367 * Skip I915_FORMAT_MOD_Y_TILED_CCS modifier if compression is disabled
368 * Pick y tiled modifier if it has been passed in, otherwise use linear
369 */
370 if (!bo->drv->compression && modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
371 uint32_t i;
372 for (i = 0; modifiers && i < count; i++) {
373 if (modifiers[i] == I915_FORMAT_MOD_Y_TILED)
374 break;
375 }
376 if (i == count)
377 modifier = DRM_FORMAT_MOD_LINEAR;
378 else
379 modifier = I915_FORMAT_MOD_Y_TILED;
380 }
381
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700382 switch (modifier) {
383 case DRM_FORMAT_MOD_LINEAR:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700384 bo->meta.tiling = I915_TILING_NONE;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700385 break;
386 case I915_FORMAT_MOD_X_TILED:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700387 bo->meta.tiling = I915_TILING_X;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700388 break;
389 case I915_FORMAT_MOD_Y_TILED:
Mark Yacoubc9565642020-02-07 11:02:22 -0500390 case I915_FORMAT_MOD_Y_TILED_CCS:
Gurchetan Singh298b7572019-09-19 09:55:18 -0700391 bo->meta.tiling = I915_TILING_Y;
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700392 break;
393 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800394
Gurchetan Singh52155b42021-01-27 17:55:17 -0800395 bo->meta.format_modifier = modifier;
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800396
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700397 if (format == DRM_FORMAT_YVU420_ANDROID) {
398 /*
399 * We only need to be able to use this as a linear texture,
400 * which doesn't put any HW restrictions on how we lay it
401 * out. The Android format does require the stride to be a
402 * multiple of 16 and expects the Cr and Cb stride to be
403 * ALIGN(Y_stride / 2, 16), which we can make happen by
404 * aligning to 32 bytes here.
405 */
406 uint32_t stride = ALIGN(width, 32);
407 drv_bo_from_format(bo, stride, height, format);
Mark Yacoubc9565642020-02-07 11:02:22 -0500408 } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
409 /*
410 * For compressed surfaces, we need a color control surface
411 * (CCS). Color compression is only supported for Y tiled
412 * surfaces, and for each 32x16 tiles in the main surface we
413 * need a tile in the control surface. Y tiles are 128 bytes
414 * wide and 32 lines tall and we use that to first compute the
415 * width and height in tiles of the main surface. stride and
416 * height are already multiples of 128 and 32, respectively:
417 */
418 uint32_t stride = drv_stride_from_format(format, width, 0);
419 uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128);
420 uint32_t height_in_tiles = DIV_ROUND_UP(height, 32);
421 uint32_t size = width_in_tiles * height_in_tiles * 4096;
422 uint32_t offset = 0;
423
424 bo->meta.strides[0] = width_in_tiles * 128;
425 bo->meta.sizes[0] = size;
426 bo->meta.offsets[0] = offset;
427 offset += size;
428
429 /*
430 * Now, compute the width and height in tiles of the control
431 * surface by dividing and rounding up.
432 */
433 uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32);
434 uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16);
435 uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096;
436
437 /*
438 * With stride and height aligned to y tiles, offset is
439 * already a multiple of 4096, which is the required alignment
440 * of the CCS.
441 */
442 bo->meta.strides[1] = ccs_width_in_tiles * 128;
443 bo->meta.sizes[1] = ccs_size;
444 bo->meta.offsets[1] = offset;
445 offset += ccs_size;
446
447 bo->meta.num_planes = 2;
448 bo->meta.total_size = offset;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700449 } else {
450 i915_bo_from_format(bo, width, height, format);
451 }
David Stevens26fe6822020-03-09 12:23:42 +0000452 return 0;
453}
454
455static int i915_bo_create_from_metadata(struct bo *bo)
456{
457 int ret;
458 size_t plane;
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700459 uint32_t gem_handle;
Gurchetan Singh99644382020-10-07 15:28:11 -0700460 struct drm_i915_gem_set_tiling gem_set_tiling = { 0 };
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700461 struct i915_device *i915 = bo->drv->priv;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800462
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700463 if (i915->has_hw_protection && (bo->meta.use_flags & BO_USE_PROTECTED)) {
464 struct drm_i915_gem_object_param protected_param = {
465 .param = I915_OBJECT_PARAM | I915_PARAM_PROTECTED_CONTENT,
466 .data = 1,
467 };
468
469 struct drm_i915_gem_create_ext_setparam setparam_protected = {
470 .base = { .name = I915_GEM_CREATE_EXT_SETPARAM },
471 .param = protected_param,
472 };
473
474 struct drm_i915_gem_create_ext create_ext = {
475 .size = bo->meta.total_size,
476 .extensions = (uintptr_t)&setparam_protected,
477 };
478
479 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE_EXT, &create_ext);
480 if (ret) {
481 drv_log("DRM_IOCTL_I915_GEM_CREATE_EXT failed (size=%llu)\n",
482 create_ext.size);
483 return -errno;
484 }
485
486 gem_handle = create_ext.handle;
487 } else {
488 struct drm_i915_gem_create gem_create = { 0 };
489 gem_create.size = bo->meta.total_size;
490 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
491 if (ret) {
492 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
493 return -errno;
494 }
495
496 gem_handle = gem_create.handle;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700497 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700498
Gurchetan Singh298b7572019-09-19 09:55:18 -0700499 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singhf98d1c12020-10-07 15:46:23 -0700500 bo->handles[plane].u32 = gem_handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400501
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800502 gem_set_tiling.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700503 gem_set_tiling.tiling_mode = bo->meta.tiling;
504 gem_set_tiling.stride = bo->meta.strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700505
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800506 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
507 if (ret) {
Gurchetan Singh99644382020-10-07 15:28:11 -0700508 struct drm_gem_close gem_close = { 0 };
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800509 gem_close.handle = bo->handles[0].u32;
510 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800511
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700512 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700513 return -errno;
514 }
515
516 return 0;
517}
518
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800519static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800520{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800521 free(drv->priv);
522 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800523}
524
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800525static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
526{
527 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700528 struct drm_i915_gem_get_tiling gem_get_tiling = { 0 };
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800529
530 ret = drv_prime_bo_import(bo, data);
531 if (ret)
532 return ret;
533
534 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800535 gem_get_tiling.handle = bo->handles[0].u32;
536
537 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
538 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700539 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700540 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800541 return ret;
542 }
543
Gurchetan Singh298b7572019-09-19 09:55:18 -0700544 bo->meta.tiling = gem_get_tiling.tiling_mode;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800545 return 0;
546}
547
Gurchetan Singhee43c302017-11-14 18:20:27 -0800548static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700549{
550 int ret;
Kristian H. Kristensen8e9c2412020-11-19 19:20:04 +0000551 void *addr = MAP_FAILED;
Gurchetan Singhef920532016-08-12 16:38:25 -0700552
Gurchetan Singh52155b42021-01-27 17:55:17 -0800553 if (bo->meta.format_modifier == I915_FORMAT_MOD_Y_TILED_CCS)
Mark Yacoubc9565642020-02-07 11:02:22 -0500554 return MAP_FAILED;
555
Gurchetan Singh298b7572019-09-19 09:55:18 -0700556 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh99644382020-10-07 15:28:11 -0700557 struct drm_i915_gem_mmap gem_map = { 0 };
Tomasz Figa39eb9512018-11-01 00:45:31 +0900558 /* TODO(b/118799155): We don't seem to have a good way to
559 * detect the use cases for which WC mapping is really needed.
560 * The current heuristic seems overly coarse and may be slowing
561 * down some other use cases unnecessarily.
562 *
563 * For now, care must be taken not to use WC mappings for
564 * Renderscript and camera use cases, as they're
565 * performance-sensitive. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700566 if ((bo->meta.use_flags & BO_USE_SCANOUT) &&
567 !(bo->meta.use_flags &
Tomasz Figa39eb9512018-11-01 00:45:31 +0900568 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700569 gem_map.flags = I915_MMAP_WC;
570
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800571 gem_map.handle = bo->handles[0].u32;
572 gem_map.offset = 0;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700573 gem_map.size = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800574
575 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
Kristian H. Kristensen8e9c2412020-11-19 19:20:04 +0000576 /* DRM_IOCTL_I915_GEM_MMAP mmaps the underlying shm
577 * file and returns a user space address directly, ie,
578 * doesn't go through mmap. If we try that on a
579 * dma-buf that doesn't have a shm file, i915.ko
580 * returns ENXIO. Fall through to
581 * DRM_IOCTL_I915_GEM_MMAP_GTT in that case, which
582 * will mmap on the drm fd instead. */
583 if (ret == 0)
584 addr = (void *)(uintptr_t)gem_map.addr_ptr;
585 }
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800586
Kristian H. Kristensen8e9c2412020-11-19 19:20:04 +0000587 if (addr == MAP_FAILED) {
Gurchetan Singh99644382020-10-07 15:28:11 -0700588 struct drm_i915_gem_mmap_gtt gem_map = { 0 };
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800589
590 gem_map.handle = bo->handles[0].u32;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800591 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
592 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700593 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800594 return MAP_FAILED;
595 }
596
Gurchetan Singh298b7572019-09-19 09:55:18 -0700597 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED,
598 bo->drv->fd, gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800599 }
600
601 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700602 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800603 return addr;
604 }
605
Gurchetan Singh298b7572019-09-19 09:55:18 -0700606 vma->length = bo->meta.total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800607 return addr;
608}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700609
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700610static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700611{
612 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700613 struct drm_i915_gem_set_domain set_domain = { 0 };
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700614
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700615 set_domain.handle = bo->handles[0].u32;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700616 if (bo->meta.tiling == I915_TILING_NONE) {
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700617 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700618 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700619 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
620 } else {
621 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700622 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700623 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
624 }
625
626 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
627 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700628 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700629 return ret;
630 }
631
632 return 0;
633}
634
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700635static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800636{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800637 struct i915_device *i915 = bo->drv->priv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700638 if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700639 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800640
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700641 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700642}
643
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700644const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700645 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700646 .init = i915_init,
647 .close = i915_close,
David Stevens26fe6822020-03-09 12:23:42 +0000648 .bo_compute_metadata = i915_bo_compute_metadata,
649 .bo_create_from_metadata = i915_bo_create_from_metadata,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800650 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800651 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700652 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700653 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700654 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700655 .bo_flush = i915_bo_flush,
Gurchetan Singh695125c2021-02-03 08:44:09 -0800656 .resolve_format = drv_resolve_format_helper,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700657};
658
659#endif