blob: a583ad94fad15fee47d533d8b0758dd4ece8618b [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,
David Rileyb75efb62019-05-28 13:08:12 -070030 DRM_FORMAT_XRGB8888, DRM_FORMAT_ARGB2101010 };
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,
Gurchetan Singh39490e92019-05-28 17:49:09 -070036 DRM_FORMAT_NV12, DRM_FORMAT_P010 };
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
Miguel Casascdb25542019-07-18 13:07:30 -0400191 /* Support y-tiled NV12 and P010 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);
Miguel Casascdb25542019-07-18 13:07:30 -0400194 drv_add_combination(drv, DRM_FORMAT_P010, &metadata,
195 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
Kristian H. Kristensen3cb5bba2018-04-04 16:10:42 -0700196
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700197 kms_items = drv_query_kms(drv);
198 if (!kms_items)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800199 return 0;
200
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700201 for (i = 0; i < drv_array_size(kms_items); i++) {
202 ret = i915_add_kms_item(drv, (struct kms_item *)drv_array_at_idx(kms_items, i));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800203 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700204 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800205 return ret;
206 }
207 }
208
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700209 drv_array_destroy(kms_items);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800210 return 0;
211}
212
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800213static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
214 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700215{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700216 struct i915_device *i915 = bo->drv->priv;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700217 uint32_t horizontal_alignment;
218 uint32_t vertical_alignment;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700219
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700220 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700221 default:
222 case I915_TILING_NONE:
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700223 /*
224 * The Intel GPU doesn't need any alignment in linear mode,
225 * but libva requires the allocation stride to be aligned to
226 * 16 bytes and height to 4 rows. Further, we round up the
227 * horizontal alignment so that row start on a cache line (64
228 * bytes).
229 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700230 horizontal_alignment = 64;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700231 vertical_alignment = 4;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700232 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800233
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700234 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700235 horizontal_alignment = 512;
236 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700237 break;
238
239 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700240 if (i915->gen == 3) {
241 horizontal_alignment = 512;
242 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800243 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700244 horizontal_alignment = 128;
245 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700246 }
247 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700248 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800249
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700250 *aligned_height = ALIGN(bo->height, vertical_alignment);
251 if (i915->gen > 3) {
252 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800253 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700254 while (*stride > horizontal_alignment)
255 horizontal_alignment <<= 1;
256
257 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800258 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800259
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700260 if (i915->gen <= 3 && *stride > 8192)
261 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800262
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700263 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700264}
265
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800266static void i915_clflush(void *start, size_t size)
267{
268 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
269 void *end = (void *)((uintptr_t)start + size);
270
271 __builtin_ia32_mfence();
272 while (p < end) {
273 __builtin_ia32_clflush(p);
274 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
275 }
276}
277
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800278static int i915_init(struct driver *drv)
279{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800280 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800281 int device_id;
282 struct i915_device *i915;
283 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800284
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800285 i915 = calloc(1, sizeof(*i915));
286 if (!i915)
287 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800288
289 memset(&get_param, 0, sizeof(get_param));
290 get_param.param = I915_PARAM_CHIPSET_ID;
291 get_param.value = &device_id;
292 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
293 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700294 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800295 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800296 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800297 }
298
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800299 i915->gen = i915_get_gen(device_id);
300
301 memset(&get_param, 0, sizeof(get_param));
302 get_param.param = I915_PARAM_HAS_LLC;
303 get_param.value = &i915->has_llc;
304 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
305 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700306 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800307 free(i915);
308 return -EINVAL;
309 }
310
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800311 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800312
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800313 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800314}
315
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700316static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
317{
318 uint32_t offset;
319 size_t plane;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800320 int ret, pagesize;
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700321
322 offset = 0;
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800323 pagesize = getpagesize();
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700324 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
325 uint32_t stride = drv_stride_from_format(format, width, plane);
326 uint32_t plane_height = drv_height_from_format(format, height, plane);
327
328 if (bo->tiling != I915_TILING_NONE)
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800329 assert(IS_ALIGNED(offset, pagesize));
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700330
331 ret = i915_align_dimensions(bo, bo->tiling, &stride, &plane_height);
332 if (ret)
333 return ret;
334
335 bo->strides[plane] = stride;
336 bo->sizes[plane] = stride * plane_height;
337 bo->offsets[plane] = offset;
338 offset += bo->sizes[plane];
339 }
340
Gurchetan Singhcc35e692019-02-28 15:44:54 -0800341 bo->total_size = ALIGN(offset, pagesize);
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700342
343 return 0;
344}
345
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700346static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
347 uint32_t format, uint64_t modifier)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700348{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700349 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800350 size_t plane;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800351 struct drm_i915_gem_create gem_create;
352 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700353
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700354 switch (modifier) {
355 case DRM_FORMAT_MOD_LINEAR:
356 bo->tiling = I915_TILING_NONE;
357 break;
358 case I915_FORMAT_MOD_X_TILED:
359 bo->tiling = I915_TILING_X;
360 break;
361 case I915_FORMAT_MOD_Y_TILED:
362 bo->tiling = I915_TILING_Y;
363 break;
364 }
Owen Linbbb69fd2017-06-05 14:33:08 +0800365
Kristian H. Kristensen2b8f89e2018-02-07 16:10:06 -0800366 bo->format_modifiers[0] = modifier;
367
Kristian H. Kristensene8778f02018-04-04 14:21:41 -0700368 if (format == DRM_FORMAT_YVU420_ANDROID) {
369 /*
370 * We only need to be able to use this as a linear texture,
371 * which doesn't put any HW restrictions on how we lay it
372 * out. The Android format does require the stride to be a
373 * multiple of 16 and expects the Cr and Cb stride to be
374 * ALIGN(Y_stride / 2, 16), which we can make happen by
375 * aligning to 32 bytes here.
376 */
377 uint32_t stride = ALIGN(width, 32);
378 drv_bo_from_format(bo, stride, height, format);
379 } else {
380 i915_bo_from_format(bo, width, height, format);
381 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800382
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800383 memset(&gem_create, 0, sizeof(gem_create));
384 gem_create.size = bo->total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800385
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800386 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
387 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700388 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700389 return -errno;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700390 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700391
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800392 for (plane = 0; plane < bo->num_planes; plane++)
393 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400394
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800395 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
396 gem_set_tiling.handle = bo->handles[0].u32;
397 gem_set_tiling.tiling_mode = bo->tiling;
398 gem_set_tiling.stride = bo->strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700399
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800400 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
401 if (ret) {
402 struct drm_gem_close gem_close;
403 memset(&gem_close, 0, sizeof(gem_close));
404 gem_close.handle = bo->handles[0].u32;
405 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800406
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700407 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700408 return -errno;
409 }
410
411 return 0;
412}
413
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700414static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
415 uint64_t use_flags)
416{
417 struct combination *combo;
418
419 combo = drv_get_combination(bo->drv, format, use_flags);
420 if (!combo)
421 return -EINVAL;
422
423 return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
424}
425
426static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
427 uint32_t format, const uint64_t *modifiers, uint32_t count)
428{
429 static const uint64_t modifier_order[] = {
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700430 I915_FORMAT_MOD_Y_TILED,
431 I915_FORMAT_MOD_X_TILED,
432 DRM_FORMAT_MOD_LINEAR,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700433 };
434 uint64_t modifier;
435
436 modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
437
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700438 return i915_bo_create_for_modifier(bo, width, height, format, modifier);
439}
440
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800441static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800442{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800443 free(drv->priv);
444 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800445}
446
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800447static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
448{
449 int ret;
450 struct drm_i915_gem_get_tiling gem_get_tiling;
451
452 ret = drv_prime_bo_import(bo, data);
453 if (ret)
454 return ret;
455
456 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
457 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
458 gem_get_tiling.handle = bo->handles[0].u32;
459
460 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
461 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700462 drv_gem_bo_destroy(bo);
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700463 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800464 return ret;
465 }
466
467 bo->tiling = gem_get_tiling.tiling_mode;
468 return 0;
469}
470
Gurchetan Singhee43c302017-11-14 18:20:27 -0800471static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700472{
473 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800474 void *addr;
Gurchetan Singhef920532016-08-12 16:38:25 -0700475
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800476 if (bo->tiling == I915_TILING_NONE) {
477 struct drm_i915_gem_mmap gem_map;
478 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700479
Tomasz Figa39eb9512018-11-01 00:45:31 +0900480 /* TODO(b/118799155): We don't seem to have a good way to
481 * detect the use cases for which WC mapping is really needed.
482 * The current heuristic seems overly coarse and may be slowing
483 * down some other use cases unnecessarily.
484 *
485 * For now, care must be taken not to use WC mappings for
486 * Renderscript and camera use cases, as they're
487 * performance-sensitive. */
488 if ((bo->use_flags & BO_USE_SCANOUT) &&
489 !(bo->use_flags &
490 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
Gurchetan Singh5af20232017-09-19 15:10:58 -0700491 gem_map.flags = I915_MMAP_WC;
492
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800493 gem_map.handle = bo->handles[0].u32;
494 gem_map.offset = 0;
495 gem_map.size = bo->total_size;
496
497 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
498 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700499 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800500 return MAP_FAILED;
501 }
502
503 addr = (void *)(uintptr_t)gem_map.addr_ptr;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800504 } else {
505 struct drm_i915_gem_mmap_gtt gem_map;
506 memset(&gem_map, 0, sizeof(gem_map));
507
508 gem_map.handle = bo->handles[0].u32;
509
510 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
511 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700512 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800513 return MAP_FAILED;
514 }
515
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700516 addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
517 gem_map.offset);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800518 }
519
520 if (addr == MAP_FAILED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700521 drv_log("i915 GEM mmap failed\n");
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800522 return addr;
523 }
524
Gurchetan Singhee43c302017-11-14 18:20:27 -0800525 vma->length = bo->total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800526 return addr;
527}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700528
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700529static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700530{
531 int ret;
532 struct drm_i915_gem_set_domain set_domain;
533
534 memset(&set_domain, 0, sizeof(set_domain));
535 set_domain.handle = bo->handles[0].u32;
536 if (bo->tiling == I915_TILING_NONE) {
537 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700538 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700539 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
540 } else {
541 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700542 if (mapping->vma->map_flags & BO_MAP_WRITE)
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700543 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
544 }
545
546 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
547 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700548 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700549 return ret;
550 }
551
552 return 0;
553}
554
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700555static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800556{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800557 struct i915_device *i915 = bo->drv->priv;
558 if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700559 i915_clflush(mapping->vma->addr, mapping->vma->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800560
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700561 return 0;
Gurchetan Singhef920532016-08-12 16:38:25 -0700562}
563
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700564static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700565{
566 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800567 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Tomasz Figad30c0a52017-07-05 17:50:18 +0900568 /* KBL camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700569 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Tomasz Figad30c0a52017-07-05 17:50:18 +0900570 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700571 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800572 return DRM_FORMAT_XBGR8888;
573 case DRM_FORMAT_FLEX_YCbCr_420_888:
Tomasz Figab92e4f82017-06-22 16:52:43 +0900574 /*
575 * KBL camera subsystem requires NV12. Our other use cases
576 * don't care:
577 * - Hardware video supports NV12,
578 * - USB Camera HALv3 supports NV12,
579 * - USB Camera HALv1 doesn't use this format.
580 * Moreover, NV12 is preferred for video, due to overlay
581 * support on SKL+.
582 */
583 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700584 default:
585 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700586 }
587}
588
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700589const struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700590 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700591 .init = i915_init,
592 .close = i915_close,
593 .bo_create = i915_bo_create,
Kristian H. Kristensen6061eab2017-10-03 13:53:19 -0700594 .bo_create_with_modifiers = i915_bo_create_with_modifiers,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800595 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800596 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700597 .bo_map = i915_bo_map,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700598 .bo_unmap = drv_bo_munmap,
Gurchetan Singh2d1877f2017-10-10 14:12:46 -0700599 .bo_invalidate = i915_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700600 .bo_flush = i915_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700601 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700602};
603
604#endif