blob: 922d257e501a7468d125eb5982e522ebcdddb182 [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2014 The Chromium OS Authors. All rights reserved.
Stéphane Marchesin25a26062014-09-12 16:18:59 -07003 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07007#ifdef DRV_I915
Stéphane Marchesin25a26062014-09-12 16:18:59 -07008
Kristian H. Kristensene8778f02018-04-04 14:21:41 -07009#include <assert.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070010#include <errno.h>
Gurchetan Singh82a8eed2017-01-03 13:01:37 -080011#include <i915_drm.h>
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070012#include <stdbool.h>
Gurchetan Singhcc015e82017-01-17 16:15:25 -080013#include <stdio.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070014#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070015#include <sys/mman.h>
Gurchetan Singhcc35e692019-02-28 15:44:54 -080016#include <unistd.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070017#include <xf86drm.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070018
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070019#include "drv_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070020#include "helpers.h"
21#include "util.h"
22
Gurchetan Singh68af9c22017-01-18 13:48:11 -080023#define I915_CACHELINE_SIZE 64
24#define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
25
Gurchetan Singh767c5382018-05-05 00:42:12 +000026static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB1555,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070027 DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
28 DRM_FORMAT_XBGR2101010, DRM_FORMAT_XBGR8888,
29 DRM_FORMAT_XRGB1555, DRM_FORMAT_XRGB2101010,
30 DRM_FORMAT_XRGB8888 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080031
Tomasz Figab92e4f82017-06-22 16:52:43 +090032static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_R8,
33 DRM_FORMAT_UYVY, DRM_FORMAT_YUYV };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070034
Tomasz Figab92e4f82017-06-22 16:52:43 +090035static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID,
36 DRM_FORMAT_NV12 };
Gurchetan Singh179687e2016-10-28 10:07:35 -070037
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080038struct i915_device {
Gurchetan Singh68af9c22017-01-18 13:48:11 -080039 uint32_t gen;
40 int32_t has_llc;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070041};
42
Gurchetan Singh68af9c22017-01-18 13:48:11 -080043static uint32_t i915_get_gen(int device_id)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070044{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080045 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
46 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070047 unsigned i;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080048 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070049 if (gen3_ids[i] == device_id)
50 return 3;
51
52 return 4;
53}
54
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070055/*
56 * We allow allocation of ARGB formats for SCANOUT if the corresponding XRGB
57 * formats supports it. It's up to the caller (chrome ozone) to ultimately not
58 * scan out ARGB if the display controller only supports XRGB, but we'll allow
59 * the allocation of the bo here.
60 */
61static bool format_compatible(const struct combination *combo, uint32_t format)
62{
63 if (combo->format == format)
64 return true;
65
66 switch (format) {
67 case DRM_FORMAT_XRGB8888:
68 return combo->format == DRM_FORMAT_ARGB8888;
69 case DRM_FORMAT_XBGR8888:
70 return combo->format == DRM_FORMAT_ABGR8888;
71 case DRM_FORMAT_RGBX8888:
72 return combo->format == DRM_FORMAT_RGBA8888;
73 case DRM_FORMAT_BGRX8888:
74 return combo->format == DRM_FORMAT_BGRA8888;
75 default:
76 return false;
77 }
78}
79
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080080static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
81{
82 uint32_t i;
83 struct combination *combo;
84
85 /*
86 * Older hardware can't scanout Y-tiled formats. Newer devices can, and
87 * report this functionality via format modifiers.
88 */
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -070089 for (i = 0; i < drv_array_size(drv->combos); i++) {
90 combo = (struct combination *)drv_array_at_idx(drv->combos, i);
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070091 if (!format_compatible(combo, item->format))
Tomasz Figae821cc22017-07-08 15:53:11 +090092 continue;
93
Gurchetan Singhd118a0e2018-01-12 23:31:50 +000094 if (item->modifier == DRM_FORMAT_MOD_LINEAR &&
Tomasz Figae821cc22017-07-08 15:53:11 +090095 combo->metadata.tiling == I915_TILING_X) {
96 /*
97 * FIXME: drv_query_kms() does not report the available modifiers
98 * yet, but we know that all hardware can scanout from X-tiled
99 * buffers, so let's add this to our combinations, except for
100 * cursor, which must not be tiled.
101 */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700102 combo->use_flags |= item->use_flags & ~BO_USE_CURSOR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800103 }
Tomasz Figae821cc22017-07-08 15:53:11 +0900104
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700105 /* If we can scanout NV12, we support all tiling modes. */
106 if (item->format == DRM_FORMAT_NV12)
107 combo->use_flags |= item->use_flags;
108
Tomasz Figae821cc22017-07-08 15:53:11 +0900109 if (combo->metadata.modifier == item->modifier)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700110 combo->use_flags |= item->use_flags;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800111 }
112
113 return 0;
114}
115
116static int i915_add_combinations(struct driver *drv)
117{
118 int ret;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700119 uint32_t i;
120 struct drv_array *kms_items;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800121 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700122 uint64_t render_use_flags, texture_use_flags;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700123
Gurchetan Singha1892b22017-09-28 16:40:52 -0700124 render_use_flags = BO_USE_RENDER_MASK;
125 texture_use_flags = BO_USE_TEXTURE_MASK;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800126
127 metadata.tiling = I915_TILING_NONE;
128 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700129 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800130
Gurchetan Singhd3001452017-11-03 17:18:36 -0700131 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
132 &metadata, render_use_flags);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800133
Gurchetan Singhd3001452017-11-03 17:18:36 -0700134 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
135 &metadata, texture_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700136
Gurchetan Singhd3001452017-11-03 17:18:36 -0700137 drv_add_combinations(drv, tileable_texture_source_formats,
138 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
139 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800140
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700141 /* Android CTS tests require this. */
142 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
143
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800144 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
145 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800146
Tomasz Figad30c0a52017-07-05 17:50:18 +0900147 /* IPU3 camera ISP supports only NV12 output. */
148 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900149 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900150 /*
151 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
152 * from camera.
153 */
154 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900155 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900156
Gurchetan Singha1892b22017-09-28 16:40:52 -0700157 render_use_flags &= ~BO_USE_RENDERSCRIPT;
158 render_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
159 render_use_flags &= ~BO_USE_SW_READ_OFTEN;
160 render_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700161 render_use_flags &= ~BO_USE_PROTECTED;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700162
Gurchetan Singha1892b22017-09-28 16:40:52 -0700163 texture_use_flags &= ~BO_USE_RENDERSCRIPT;
164 texture_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
165 texture_use_flags &= ~BO_USE_SW_READ_OFTEN;
166 texture_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700167 texture_use_flags &= ~BO_USE_PROTECTED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800168
169 metadata.tiling = I915_TILING_X;
170 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900171 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800172
Gurchetan Singhd3001452017-11-03 17:18:36 -0700173 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
174 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700175
Gurchetan Singhd3001452017-11-03 17:18:36 -0700176 drv_add_combinations(drv, tileable_texture_source_formats,
177 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
178 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800179
180 metadata.tiling = I915_TILING_Y;
181 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900182 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800183
Gurchetan Singhd3001452017-11-03 17:18:36 -0700184 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
185 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700186
Gurchetan Singhd3001452017-11-03 17:18:36 -0700187 drv_add_combinations(drv, tileable_texture_source_formats,
188 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
189 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800190
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700191 /* Support y-tiled NV12 for libva */
Gurchetan Singh86ddfdc2018-09-17 17:13:45 -0700192 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
193 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700194
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700195 kms_items = drv_query_kms(drv);
196 if (!kms_items)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800197 return 0;
198
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700199 for (i = 0; i < drv_array_size(kms_items); i++) {
200 ret = i915_add_kms_item(drv, (struct kms_item *)drv_array_at_idx(kms_items, i));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800201 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700202 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800203 return ret;
204 }
205 }
206
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700207 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800208 return 0;
209}
210
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800211static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
212 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700213{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700214 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700215 uint32_t horizontal_alignment;
216 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700217
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700218 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700219 default:
220 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700221 /*
222 * The Intel GPU doesn't need any alignment in linear mode,
223 * but libva requires the allocation stride to be aligned to
224 * 16 bytes and height to 4 rows. Further, we round up the
225 * horizontal alignment so that row start on a cache line (64
226 * bytes).
227 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700228 horizontal_alignment = 64;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700229 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700230 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800231
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700232 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700233 horizontal_alignment = 512;
234 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700235 break;
236
237 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700238 if (i915->gen == 3) {
239 horizontal_alignment = 512;
240 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800241 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700242 horizontal_alignment = 128;
243 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700244 }
245 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700246 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800247
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700248 *aligned_height = ALIGN(bo->height, vertical_alignment);
249 if (i915->gen > 3) {
250 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800251 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700252 while (*stride > horizontal_alignment)
253 horizontal_alignment <<= 1;
254
255 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800256 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800257
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700258 if (i915->gen <= 3 && *stride > 8192)
259 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800260
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700261 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700262}
263
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800264static void i915_clflush(void *start, size_t size)
265{
266 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
267 void *end = (void *)((uintptr_t)start + size);
268
269 __builtin_ia32_mfence();
270 while (p < end) {
271 __builtin_ia32_clflush(p);
272 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
273 }
274}
275
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800276static int i915_init(struct driver *drv)
277{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800278 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800279 int device_id;
280 struct i915_device *i915;
281 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800282
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800283 i915 = calloc(1, sizeof(*i915));
284 if (!i915)
285 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800286
287 memset(&get_param, 0, sizeof(get_param));
288 get_param.param = I915_PARAM_CHIPSET_ID;
289 get_param.value = &device_id;
290 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
291 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700292 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800293 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800294 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800295 }
296
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800297 i915->gen = i915_get_gen(device_id);
298
299 memset(&get_param, 0, sizeof(get_param));
300 get_param.param = I915_PARAM_HAS_LLC;
301 get_param.value = &i915->has_llc;
302 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
303 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700304 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800305 free(i915);
306 return -EINVAL;
307 }
308
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800309 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800310
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800311 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800312}
313
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700314static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
315{
316 uint32_t offset;
317 size_t plane;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800318 int ret, pagesize;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700319
320 offset = 0;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800321 pagesize = getpagesize();
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700322 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
323 uint32_t stride = drv_stride_from_format(format, width, plane);
324 uint32_t plane_height = drv_height_from_format(format, height, plane);
325
326 if (bo->tiling != I915_TILING_NONE)
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800327 assert(IS_ALIGNED(offset, pagesize));
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700328
329 ret = i915_align_dimensions(bo, bo->tiling, &stride, &plane_height);
330 if (ret)
331 return ret;
332
333 bo->strides[plane] = stride;
334 bo->sizes[plane] = stride * plane_height;
335 bo->offsets[plane] = offset;
336 offset += bo->sizes[plane];
337 }
338
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800339 bo->total_size = ALIGN(offset, pagesize);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700340
341 return 0;
342}
343
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700344static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
345 uint32_t format, uint64_t modifier)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700346{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700347 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800348 size_t plane;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800349 struct drm_i915_gem_create gem_create;
350 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700351
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700352 switch (modifier) {
353 case DRM_FORMAT_MOD_LINEAR:
354 bo->tiling = I915_TILING_NONE;
355 break;
356 case I915_FORMAT_MOD_X_TILED:
357 bo->tiling = I915_TILING_X;
358 break;
359 case I915_FORMAT_MOD_Y_TILED:
360 bo->tiling = I915_TILING_Y;
361 break;
362 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800363
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800364 bo->format_modifiers[0] = modifier;
365
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700366 if (format == DRM_FORMAT_YVU420_ANDROID) {
367 /*
368 * We only need to be able to use this as a linear texture,
369 * which doesn't put any HW restrictions on how we lay it
370 * out. The Android format does require the stride to be a
371 * multiple of 16 and expects the Cr and Cb stride to be
372 * ALIGN(Y_stride / 2, 16), which we can make happen by
373 * aligning to 32 bytes here.
374 */
375 uint32_t stride = ALIGN(width, 32);
376 drv_bo_from_format(bo, stride, height, format);
377 } else {
378 i915_bo_from_format(bo, width, height, format);
379 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800380
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800381 memset(&gem_create, 0, sizeof(gem_create));
382 gem_create.size = bo->total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800383
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800384 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
385 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700386 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800387 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700388 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700389
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800390 for (plane = 0; plane < bo->num_planes; plane++)
391 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400392
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800393 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
394 gem_set_tiling.handle = bo->handles[0].u32;
395 gem_set_tiling.tiling_mode = bo->tiling;
396 gem_set_tiling.stride = bo->strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700397
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800398 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
399 if (ret) {
400 struct drm_gem_close gem_close;
401 memset(&gem_close, 0, sizeof(gem_close));
402 gem_close.handle = bo->handles[0].u32;
403 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800404
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700405 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700406 return -errno;
407 }
408
409 return 0;
410}
411
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700412static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
413 uint64_t use_flags)
414{
415 struct combination *combo;
416
417 combo = drv_get_combination(bo->drv, format, use_flags);
418 if (!combo)
419 return -EINVAL;
420
421 return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
422}
423
424static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
425 uint32_t format, const uint64_t *modifiers, uint32_t count)
426{
427 static const uint64_t modifier_order[] = {
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700428 I915_FORMAT_MOD_Y_TILED,
429 I915_FORMAT_MOD_X_TILED,
430 DRM_FORMAT_MOD_LINEAR,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700431 };
432 uint64_t modifier;
433
434 modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
435
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700436 return i915_bo_create_for_modifier(bo, width, height, format, modifier);
437}
438
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800439static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800440{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800441 free(drv->priv);
442 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800443}
444
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800445static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
446{
447 int ret;
448 struct drm_i915_gem_get_tiling gem_get_tiling;
449
450 ret = drv_prime_bo_import(bo, data);
451 if (ret)
452 return ret;
453
454 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
455 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
456 gem_get_tiling.handle = bo->handles[0].u32;
457
458 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
459 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700460 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700461 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800462 return ret;
463 }
464
465 bo->tiling = gem_get_tiling.tiling_mode;
466 return 0;
467}
468
Gurchetan Singhee43c302017-11-14 18:20:27 -0800469static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700470{
471 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800472 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700473
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800474 if (bo->tiling == I915_TILING_NONE) {
475 struct drm_i915_gem_mmap gem_map;
476 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700477
Tomasz Figa39eb9512018-11-01 00:45:31 +0900478 /* TODO(b/118799155): We don't seem to have a good way to
479 * detect the use cases for which WC mapping is really needed.
480 * The current heuristic seems overly coarse and may be slowing
481 * down some other use cases unnecessarily.
482 *
483 * For now, care must be taken not to use WC mappings for
484 * Renderscript and camera use cases, as they're
485 * performance-sensitive. */
486 if ((bo->use_flags & BO_USE_SCANOUT) &&
487 !(bo->use_flags &
488 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700489 gem_map.flags = I915_MMAP_WC;
490
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800491 gem_map.handle = bo->handles[0].u32;
492 gem_map.offset = 0;
493 gem_map.size = bo->total_size;
494
495 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
496 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700497 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800498 return MAP_FAILED;
499 }
500
501 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800502 } else {
503 struct drm_i915_gem_mmap_gtt gem_map;
504 memset(&gem_map, 0, sizeof(gem_map));
505
506 gem_map.handle = bo->handles[0].u32;
507
508 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
509 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700510 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800511 return MAP_FAILED;
512 }
513
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700514 addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
515 gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800516 }
517
518 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700519 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800520 return addr;
521 }
522
Gurchetan Singhee43c302017-11-14 18:20:27 -0800523 vma->length = bo->total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800524 return addr;
525}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700526
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700527static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700528{
529 int ret;
530 struct drm_i915_gem_set_domain set_domain;
531
532 memset(&set_domain, 0, sizeof(set_domain));
533 set_domain.handle = bo->handles[0].u32;
534 if (bo->tiling == I915_TILING_NONE) {
535 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700536 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700537 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
538 } else {
539 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700540 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700541 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
542 }
543
544 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
545 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700546 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700547 return ret;
548 }
549
550 return 0;
551}
552
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700553static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800554{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800555 struct i915_device *i915 = bo->drv->priv;
556 if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700557 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800558
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700559 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700560}
561
Gurchetan Singha1892b22017-09-28 16:40:52 -0700562static uint32_t i915_resolve_format(uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700563{
564 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800565 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900566 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700567 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900568 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700569 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800570 return DRM_FORMAT_XBGR8888;
571 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900572 /*
573 * KBL camera subsystem requires NV12. Our other use cases
574 * don't care:
575 * - Hardware video supports NV12,
576 * - USB Camera HALv3 supports NV12,
577 * - USB Camera HALv1 doesn't use this format.
578 * Moreover, NV12 is preferred for video, due to overlay
579 * support on SKL+.
580 */
581 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700582 default:
583 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700584 }
585}
586
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700587const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700588 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700589 .init = i915_init,
590 .close = i915_close,
591 .bo_create = i915_bo_create,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700592 .bo_create_with_modifiers = i915_bo_create_with_modifiers,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800593 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800594 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700595 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700596 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700597 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700598 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700599 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700600};
601
602#endif