blob: b343e91b527af35f59dc3b1baf1b1ccdaef04866 [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>
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"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070019#include "helpers.h"
20#include "util.h"
21
Gurchetan Singh68af9c22017-01-18 13:48:11 -080022#define I915_CACHELINE_SIZE 64
23#define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
24
Gurchetan Singh767c5382018-05-05 00:42:12 +000025static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB1555,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070026 DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
27 DRM_FORMAT_XBGR2101010, DRM_FORMAT_XBGR8888,
28 DRM_FORMAT_XRGB1555, DRM_FORMAT_XRGB2101010,
29 DRM_FORMAT_XRGB8888 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080030
Tomasz Figab92e4f82017-06-22 16:52:43 +090031static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_R8,
32 DRM_FORMAT_UYVY, DRM_FORMAT_YUYV };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070033
Tomasz Figab92e4f82017-06-22 16:52:43 +090034static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID,
35 DRM_FORMAT_NV12 };
Gurchetan Singh179687e2016-10-28 10:07:35 -070036
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080037struct i915_device {
Gurchetan Singh68af9c22017-01-18 13:48:11 -080038 uint32_t gen;
39 int32_t has_llc;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070040};
41
Gurchetan Singh68af9c22017-01-18 13:48:11 -080042static uint32_t i915_get_gen(int device_id)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070043{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080044 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
45 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070046 unsigned i;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080047 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070048 if (gen3_ids[i] == device_id)
49 return 3;
50
51 return 4;
52}
53
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070054/*
55 * We allow allocation of ARGB formats for SCANOUT if the corresponding XRGB
56 * formats supports it. It's up to the caller (chrome ozone) to ultimately not
57 * scan out ARGB if the display controller only supports XRGB, but we'll allow
58 * the allocation of the bo here.
59 */
60static bool format_compatible(const struct combination *combo, uint32_t format)
61{
62 if (combo->format == format)
63 return true;
64
65 switch (format) {
66 case DRM_FORMAT_XRGB8888:
67 return combo->format == DRM_FORMAT_ARGB8888;
68 case DRM_FORMAT_XBGR8888:
69 return combo->format == DRM_FORMAT_ABGR8888;
70 case DRM_FORMAT_RGBX8888:
71 return combo->format == DRM_FORMAT_RGBA8888;
72 case DRM_FORMAT_BGRX8888:
73 return combo->format == DRM_FORMAT_BGRA8888;
74 default:
75 return false;
76 }
77}
78
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080079static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
80{
81 uint32_t i;
82 struct combination *combo;
83
84 /*
85 * Older hardware can't scanout Y-tiled formats. Newer devices can, and
86 * report this functionality via format modifiers.
87 */
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -070088 for (i = 0; i < drv_array_size(drv->combos); i++) {
89 combo = (struct combination *)drv_array_at_idx(drv->combos, i);
Kristian H. Kristensen9c3fb322018-04-11 15:55:13 -070090 if (!format_compatible(combo, item->format))
Tomasz Figae821cc22017-07-08 15:53:11 +090091 continue;
92
Gurchetan Singhd118a0e2018-01-12 23:31:50 +000093 if (item->modifier == DRM_FORMAT_MOD_LINEAR &&
Tomasz Figae821cc22017-07-08 15:53:11 +090094 combo->metadata.tiling == I915_TILING_X) {
95 /*
96 * FIXME: drv_query_kms() does not report the available modifiers
97 * yet, but we know that all hardware can scanout from X-tiled
98 * buffers, so let's add this to our combinations, except for
99 * cursor, which must not be tiled.
100 */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700101 combo->use_flags |= item->use_flags & ~BO_USE_CURSOR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800102 }
Tomasz Figae821cc22017-07-08 15:53:11 +0900103
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700104 /* If we can scanout NV12, we support all tiling modes. */
105 if (item->format == DRM_FORMAT_NV12)
106 combo->use_flags |= item->use_flags;
107
Tomasz Figae821cc22017-07-08 15:53:11 +0900108 if (combo->metadata.modifier == item->modifier)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700109 combo->use_flags |= item->use_flags;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800110 }
111
112 return 0;
113}
114
115static int i915_add_combinations(struct driver *drv)
116{
117 int ret;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700118 uint32_t i;
119 struct drv_array *kms_items;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800120 struct format_metadata metadata;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700121 uint64_t render_use_flags, texture_use_flags;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700122
Gurchetan Singha1892b22017-09-28 16:40:52 -0700123 render_use_flags = BO_USE_RENDER_MASK;
124 texture_use_flags = BO_USE_TEXTURE_MASK;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800125
126 metadata.tiling = I915_TILING_NONE;
127 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700128 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800129
Gurchetan Singhd3001452017-11-03 17:18:36 -0700130 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
131 &metadata, render_use_flags);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800132
Gurchetan Singhd3001452017-11-03 17:18:36 -0700133 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
134 &metadata, texture_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700135
Gurchetan Singhd3001452017-11-03 17:18:36 -0700136 drv_add_combinations(drv, tileable_texture_source_formats,
137 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
138 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800139
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700140 /* Android CTS tests require this. */
141 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
142
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800143 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
144 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800145
Tomasz Figad30c0a52017-07-05 17:50:18 +0900146 /* IPU3 camera ISP supports only NV12 output. */
147 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900148 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900149 /*
150 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
151 * from camera.
152 */
153 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
Tomasz Figafd0b0162017-07-11 18:28:02 +0900154 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Tomasz Figad30c0a52017-07-05 17:50:18 +0900155
Gurchetan Singha1892b22017-09-28 16:40:52 -0700156 render_use_flags &= ~BO_USE_RENDERSCRIPT;
157 render_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
158 render_use_flags &= ~BO_USE_SW_READ_OFTEN;
159 render_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700160 render_use_flags &= ~BO_USE_PROTECTED;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700161
Gurchetan Singha1892b22017-09-28 16:40:52 -0700162 texture_use_flags &= ~BO_USE_RENDERSCRIPT;
163 texture_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
164 texture_use_flags &= ~BO_USE_SW_READ_OFTEN;
165 texture_use_flags &= ~BO_USE_LINEAR;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700166 texture_use_flags &= ~BO_USE_PROTECTED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800167
168 metadata.tiling = I915_TILING_X;
169 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900170 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800171
Gurchetan Singhd3001452017-11-03 17:18:36 -0700172 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
173 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700174
Gurchetan Singhd3001452017-11-03 17:18:36 -0700175 drv_add_combinations(drv, tileable_texture_source_formats,
176 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
177 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800178
179 metadata.tiling = I915_TILING_Y;
180 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900181 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800182
Gurchetan Singhd3001452017-11-03 17:18:36 -0700183 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
184 &metadata, render_use_flags);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700185
Gurchetan Singhd3001452017-11-03 17:18:36 -0700186 drv_add_combinations(drv, tileable_texture_source_formats,
187 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
188 texture_use_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800189
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700190 /* Support y-tiled NV12 for libva */
Gurchetan Singh86ddfdc2018-09-17 17:13:45 -0700191 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
192 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700193
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700194 kms_items = drv_query_kms(drv);
195 if (!kms_items)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800196 return 0;
197
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700198 for (i = 0; i < drv_array_size(kms_items); i++) {
199 ret = i915_add_kms_item(drv, (struct kms_item *)drv_array_at_idx(kms_items, i));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800200 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700201 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800202 return ret;
203 }
204 }
205
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700206 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800207 return 0;
208}
209
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800210static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
211 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700212{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700213 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700214 uint32_t horizontal_alignment;
215 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700216
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700217 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700218 default:
219 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700220 /*
221 * The Intel GPU doesn't need any alignment in linear mode,
222 * but libva requires the allocation stride to be aligned to
223 * 16 bytes and height to 4 rows. Further, we round up the
224 * horizontal alignment so that row start on a cache line (64
225 * bytes).
226 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700227 horizontal_alignment = 64;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700228 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700229 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800230
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700231 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700232 horizontal_alignment = 512;
233 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700234 break;
235
236 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700237 if (i915->gen == 3) {
238 horizontal_alignment = 512;
239 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800240 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700241 horizontal_alignment = 128;
242 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700243 }
244 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700245 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800246
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700247 *aligned_height = ALIGN(bo->height, vertical_alignment);
248 if (i915->gen > 3) {
249 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800250 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700251 while (*stride > horizontal_alignment)
252 horizontal_alignment <<= 1;
253
254 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800255 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800256
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700257 if (i915->gen <= 3 && *stride > 8192)
258 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800259
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700260 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700261}
262
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800263static void i915_clflush(void *start, size_t size)
264{
265 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
266 void *end = (void *)((uintptr_t)start + size);
267
268 __builtin_ia32_mfence();
269 while (p < end) {
270 __builtin_ia32_clflush(p);
271 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
272 }
273}
274
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800275static int i915_init(struct driver *drv)
276{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800277 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800278 int device_id;
279 struct i915_device *i915;
280 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800281
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800282 i915 = calloc(1, sizeof(*i915));
283 if (!i915)
284 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800285
286 memset(&get_param, 0, sizeof(get_param));
287 get_param.param = I915_PARAM_CHIPSET_ID;
288 get_param.value = &device_id;
289 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
290 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700291 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800292 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800293 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800294 }
295
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800296 i915->gen = i915_get_gen(device_id);
297
298 memset(&get_param, 0, sizeof(get_param));
299 get_param.param = I915_PARAM_HAS_LLC;
300 get_param.value = &i915->has_llc;
301 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
302 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700303 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800304 free(i915);
305 return -EINVAL;
306 }
307
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800308 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800309
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800310 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800311}
312
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700313static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
314{
315 uint32_t offset;
316 size_t plane;
317 int ret;
318
319 offset = 0;
320 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
321 uint32_t stride = drv_stride_from_format(format, width, plane);
322 uint32_t plane_height = drv_height_from_format(format, height, plane);
323
324 if (bo->tiling != I915_TILING_NONE)
325 assert(IS_ALIGNED(offset, 4096));
326
327 ret = i915_align_dimensions(bo, bo->tiling, &stride, &plane_height);
328 if (ret)
329 return ret;
330
331 bo->strides[plane] = stride;
332 bo->sizes[plane] = stride * plane_height;
333 bo->offsets[plane] = offset;
334 offset += bo->sizes[plane];
335 }
336
337 bo->total_size = offset;
338
339 return 0;
340}
341
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700342static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
343 uint32_t format, uint64_t modifier)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700344{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700345 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800346 size_t plane;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800347 struct drm_i915_gem_create gem_create;
348 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700349
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700350 switch (modifier) {
351 case DRM_FORMAT_MOD_LINEAR:
352 bo->tiling = I915_TILING_NONE;
353 break;
354 case I915_FORMAT_MOD_X_TILED:
355 bo->tiling = I915_TILING_X;
356 break;
357 case I915_FORMAT_MOD_Y_TILED:
358 bo->tiling = I915_TILING_Y;
359 break;
360 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800361
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800362 bo->format_modifiers[0] = modifier;
363
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700364 if (format == DRM_FORMAT_YVU420_ANDROID) {
365 /*
366 * We only need to be able to use this as a linear texture,
367 * which doesn't put any HW restrictions on how we lay it
368 * out. The Android format does require the stride to be a
369 * multiple of 16 and expects the Cr and Cb stride to be
370 * ALIGN(Y_stride / 2, 16), which we can make happen by
371 * aligning to 32 bytes here.
372 */
373 uint32_t stride = ALIGN(width, 32);
374 drv_bo_from_format(bo, stride, height, format);
375 } else {
376 i915_bo_from_format(bo, width, height, format);
377 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800378
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800379 memset(&gem_create, 0, sizeof(gem_create));
380 gem_create.size = bo->total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800381
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800382 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
383 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700384 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800385 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700386 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700387
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800388 for (plane = 0; plane < bo->num_planes; plane++)
389 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400390
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800391 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
392 gem_set_tiling.handle = bo->handles[0].u32;
393 gem_set_tiling.tiling_mode = bo->tiling;
394 gem_set_tiling.stride = bo->strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700395
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800396 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
397 if (ret) {
398 struct drm_gem_close gem_close;
399 memset(&gem_close, 0, sizeof(gem_close));
400 gem_close.handle = bo->handles[0].u32;
401 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800402
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700403 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700404 return -errno;
405 }
406
407 return 0;
408}
409
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700410static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
411 uint64_t use_flags)
412{
413 struct combination *combo;
414
415 combo = drv_get_combination(bo->drv, format, use_flags);
416 if (!combo)
417 return -EINVAL;
418
419 return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
420}
421
422static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
423 uint32_t format, const uint64_t *modifiers, uint32_t count)
424{
425 static const uint64_t modifier_order[] = {
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700426 I915_FORMAT_MOD_Y_TILED,
427 I915_FORMAT_MOD_X_TILED,
428 DRM_FORMAT_MOD_LINEAR,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700429 };
430 uint64_t modifier;
431
432 modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
433
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700434 return i915_bo_create_for_modifier(bo, width, height, format, modifier);
435}
436
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800437static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800438{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800439 free(drv->priv);
440 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800441}
442
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800443static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
444{
445 int ret;
446 struct drm_i915_gem_get_tiling gem_get_tiling;
447
448 ret = drv_prime_bo_import(bo, data);
449 if (ret)
450 return ret;
451
452 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
453 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
454 gem_get_tiling.handle = bo->handles[0].u32;
455
456 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
457 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700458 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700459 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800460 return ret;
461 }
462
463 bo->tiling = gem_get_tiling.tiling_mode;
464 return 0;
465}
466
Gurchetan Singhee43c302017-11-14 18:20:27 -0800467static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700468{
469 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800470 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700471
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800472 if (bo->tiling == I915_TILING_NONE) {
473 struct drm_i915_gem_mmap gem_map;
474 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700475
Gurchetan Singha1892b22017-09-28 16:40:52 -0700476 if ((bo->use_flags & BO_USE_SCANOUT) && !(bo->use_flags & BO_USE_RENDERSCRIPT))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700477 gem_map.flags = I915_MMAP_WC;
478
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800479 gem_map.handle = bo->handles[0].u32;
480 gem_map.offset = 0;
481 gem_map.size = bo->total_size;
482
483 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
484 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700485 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800486 return MAP_FAILED;
487 }
488
489 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800490 } else {
491 struct drm_i915_gem_mmap_gtt gem_map;
492 memset(&gem_map, 0, sizeof(gem_map));
493
494 gem_map.handle = bo->handles[0].u32;
495
496 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
497 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700498 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800499 return MAP_FAILED;
500 }
501
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700502 addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
503 gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800504 }
505
506 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700507 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800508 return addr;
509 }
510
Gurchetan Singhee43c302017-11-14 18:20:27 -0800511 vma->length = bo->total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800512 return addr;
513}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700514
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700515static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700516{
517 int ret;
518 struct drm_i915_gem_set_domain set_domain;
519
520 memset(&set_domain, 0, sizeof(set_domain));
521 set_domain.handle = bo->handles[0].u32;
522 if (bo->tiling == I915_TILING_NONE) {
523 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700524 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700525 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
526 } else {
527 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700528 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700529 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
530 }
531
532 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
533 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700534 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700535 return ret;
536 }
537
538 return 0;
539}
540
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700541static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800542{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800543 struct i915_device *i915 = bo->drv->priv;
544 if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700545 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800546
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700547 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700548}
549
Gurchetan Singha1892b22017-09-28 16:40:52 -0700550static uint32_t i915_resolve_format(uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700551{
552 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800553 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900554 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700555 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900556 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700557 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800558 return DRM_FORMAT_XBGR8888;
559 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900560 /*
561 * KBL camera subsystem requires NV12. Our other use cases
562 * don't care:
563 * - Hardware video supports NV12,
564 * - USB Camera HALv3 supports NV12,
565 * - USB Camera HALv1 doesn't use this format.
566 * Moreover, NV12 is preferred for video, due to overlay
567 * support on SKL+.
568 */
569 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700570 default:
571 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700572 }
573}
574
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700575const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700576 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700577 .init = i915_init,
578 .close = i915_close,
579 .bo_create = i915_bo_create,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700580 .bo_create_with_modifiers = i915_bo_create_with_modifiers,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800581 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800582 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700583 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700584 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700585 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700586 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700587 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700588};
589
590#endif