blob: adf2739cb4e7fb336342cb62d0c6e6a474f3df9e [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
9#include <errno.h>
Gurchetan Singh82a8eed2017-01-03 13:01:37 -080010#include <i915_drm.h>
Gurchetan Singhcc015e82017-01-17 16:15:25 -080011#include <stdio.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070012#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070013#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070014#include <xf86drm.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070015
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include "drv_priv.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070017#include "helpers.h"
18#include "util.h"
19
Gurchetan Singh68af9c22017-01-18 13:48:11 -080020#define I915_CACHELINE_SIZE 64
21#define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
22
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070023static const uint32_t render_target_formats[] = { DRM_FORMAT_ARGB1555, DRM_FORMAT_ABGR8888,
24 DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
25 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB1555,
26 DRM_FORMAT_XRGB8888 };
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080027
Dongseong Hwang750e0b92017-06-07 15:17:25 -070028static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_NV12,
29 DRM_FORMAT_R8, DRM_FORMAT_UYVY,
30 DRM_FORMAT_YUYV };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070031
32static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
Gurchetan Singh179687e2016-10-28 10:07:35 -070033
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080034struct i915_device {
Gurchetan Singh68af9c22017-01-18 13:48:11 -080035 uint32_t gen;
36 int32_t has_llc;
Stéphane Marchesin25a26062014-09-12 16:18:59 -070037};
38
Gurchetan Singh68af9c22017-01-18 13:48:11 -080039static uint32_t i915_get_gen(int device_id)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070040{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080041 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
42 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
Stéphane Marchesina39dfde2014-09-15 15:38:25 -070043 unsigned i;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080044 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
Stéphane Marchesin25a26062014-09-12 16:18:59 -070045 if (gen3_ids[i] == device_id)
46 return 3;
47
48 return 4;
49}
50
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080051static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
52{
53 uint32_t i;
54 struct combination *combo;
55
56 /*
57 * Older hardware can't scanout Y-tiled formats. Newer devices can, and
58 * report this functionality via format modifiers.
59 */
60 for (i = 0; i < drv->backend->combos.size; i++) {
61 combo = &drv->backend->combos.data[i];
Tomasz Figae821cc22017-07-08 15:53:11 +090062 if (combo->format != item->format)
63 continue;
64
65 if (item->modifier == DRM_FORMAT_MOD_NONE &&
66 combo->metadata.tiling == I915_TILING_X) {
67 /*
68 * FIXME: drv_query_kms() does not report the available modifiers
69 * yet, but we know that all hardware can scanout from X-tiled
70 * buffers, so let's add this to our combinations, except for
71 * cursor, which must not be tiled.
72 */
73 combo->usage |= item->usage & ~BO_USE_CURSOR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080074 }
Tomasz Figae821cc22017-07-08 15:53:11 +090075
76 if (combo->metadata.modifier == item->modifier)
77 combo->usage |= item->usage;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080078 }
79
80 return 0;
81}
82
83static int i915_add_combinations(struct driver *drv)
84{
85 int ret;
86 uint32_t i, num_items;
87 struct kms_item *items;
88 struct format_metadata metadata;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070089 uint64_t render_flags, texture_flags;
90
91 render_flags = BO_USE_RENDER_MASK;
92 texture_flags = BO_USE_TEXTURE_MASK;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080093
94 metadata.tiling = I915_TILING_NONE;
95 metadata.priority = 1;
96 metadata.modifier = DRM_FORMAT_MOD_NONE;
97
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070098 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
99 &metadata, render_flags);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800100 if (ret)
101 return ret;
102
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700103 ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
104 &metadata, texture_flags);
105 if (ret)
106 return ret;
107
108 ret = drv_add_combinations(drv, tileable_texture_source_formats,
Dongseong Hwang3c5be5a2017-06-14 10:47:11 -0700109 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
110 texture_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800111 if (ret)
112 return ret;
113
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800114 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
115 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800116
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700117 render_flags &= ~BO_USE_SW_WRITE_OFTEN;
118 render_flags &= ~BO_USE_SW_READ_OFTEN;
119 render_flags &= ~BO_USE_LINEAR;
120
121 texture_flags &= ~BO_USE_SW_WRITE_OFTEN;
122 texture_flags &= ~BO_USE_SW_READ_OFTEN;
123 texture_flags &= ~BO_USE_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800124
125 metadata.tiling = I915_TILING_X;
126 metadata.priority = 2;
Tomasz Figae821cc22017-07-08 15:53:11 +0900127 metadata.modifier = I915_FORMAT_MOD_X_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800128
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700129 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
130 &metadata, render_flags);
131 if (ret)
132 return ret;
133
134 ret = drv_add_combinations(drv, tileable_texture_source_formats,
135 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
136 texture_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800137 if (ret)
138 return ret;
139
140 metadata.tiling = I915_TILING_Y;
141 metadata.priority = 3;
Tomasz Figae821cc22017-07-08 15:53:11 +0900142 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800143
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700144 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
145 &metadata, render_flags);
146 if (ret)
147 return ret;
148
149 ret = drv_add_combinations(drv, tileable_texture_source_formats,
150 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
151 texture_flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800152 if (ret)
153 return ret;
154
155 items = drv_query_kms(drv, &num_items);
156 if (!items || !num_items)
157 return 0;
158
159 for (i = 0; i < num_items; i++) {
160 ret = i915_add_kms_item(drv, &items[i]);
161 if (ret) {
162 free(items);
163 return ret;
164 }
165 }
166
167 free(items);
168 return 0;
169}
170
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800171static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
172 uint32_t *aligned_height)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700173{
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700174 struct i915_device *i915 = bo->drv->priv;
175 uint32_t horizontal_alignment = 4;
176 uint32_t vertical_alignment = 4;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700177
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700178 switch (tiling) {
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700179 default:
180 case I915_TILING_NONE:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700181 horizontal_alignment = 64;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700182 break;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800183
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700184 case I915_TILING_X:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700185 horizontal_alignment = 512;
186 vertical_alignment = 8;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700187 break;
188
189 case I915_TILING_Y:
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700190 if (i915->gen == 3) {
191 horizontal_alignment = 512;
192 vertical_alignment = 8;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800193 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700194 horizontal_alignment = 128;
195 vertical_alignment = 32;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700196 }
197 break;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700198 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800199
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700200 *aligned_height = ALIGN(bo->height, vertical_alignment);
201 if (i915->gen > 3) {
202 *stride = ALIGN(*stride, horizontal_alignment);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800203 } else {
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700204 while (*stride > horizontal_alignment)
205 horizontal_alignment <<= 1;
206
207 *stride = horizontal_alignment;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800208 }
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800209
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700210 if (i915->gen <= 3 && *stride > 8192)
211 return -EINVAL;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800212
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700213 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700214}
215
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800216static void i915_clflush(void *start, size_t size)
217{
218 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
219 void *end = (void *)((uintptr_t)start + size);
220
221 __builtin_ia32_mfence();
222 while (p < end) {
223 __builtin_ia32_clflush(p);
224 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
225 }
226}
227
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800228static int i915_init(struct driver *drv)
229{
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800230 int ret;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800231 int device_id;
232 struct i915_device *i915;
233 drm_i915_getparam_t get_param;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800234
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800235 i915 = calloc(1, sizeof(*i915));
236 if (!i915)
237 return -ENOMEM;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800238
239 memset(&get_param, 0, sizeof(get_param));
240 get_param.param = I915_PARAM_CHIPSET_ID;
241 get_param.value = &device_id;
242 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
243 if (ret) {
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800244 fprintf(stderr, "drv: Failed to get I915_PARAM_CHIPSET_ID\n");
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800245 free(i915);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800246 return -EINVAL;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800247 }
248
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800249 i915->gen = i915_get_gen(device_id);
250
251 memset(&get_param, 0, sizeof(get_param));
252 get_param.param = I915_PARAM_HAS_LLC;
253 get_param.value = &i915->has_llc;
254 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
255 if (ret) {
256 fprintf(stderr, "drv: Failed to get I915_PARAM_HAS_LLC\n");
257 free(i915);
258 return -EINVAL;
259 }
260
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800261 drv->priv = i915;
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800262
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800263 return i915_add_combinations(drv);
Gurchetan Singh3eb8d8f2017-01-03 13:36:13 -0800264}
265
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800266static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
267 uint32_t flags)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700268{
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700269 int ret;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800270 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700271 uint32_t stride;
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800272 struct drm_i915_gem_create gem_create;
273 struct drm_i915_gem_set_tiling gem_set_tiling;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700274
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800275 if (flags & (BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN))
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800276 bo->tiling = I915_TILING_NONE;
Gurchetan Singh458976f2016-11-23 17:32:33 -0800277 else if (flags & BO_USE_SCANOUT)
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800278 bo->tiling = I915_TILING_X;
Gurchetan Singh6bab0c12016-10-13 19:08:48 -0700279 else
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800280 bo->tiling = I915_TILING_Y;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700281
Owen Linbbb69fd2017-06-05 14:33:08 +0800282 if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID)
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800283 bo->tiling = I915_TILING_NONE;
Owen Linbbb69fd2017-06-05 14:33:08 +0800284
285 stride = drv_stride_from_format(format, width, 0);
Gurchetan Singh507f5dd2017-03-16 13:14:30 -0700286
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800287 ret = i915_align_dimensions(bo, bo->tiling, &stride, &height);
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700288 if (ret)
289 return ret;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800290
Owen Linbbb69fd2017-06-05 14:33:08 +0800291 /*
292 * Align the Y plane to 128 bytes so the chroma planes would be aligned
293 * to 64 byte boundaries. This is an Intel HW requirement.
294 */
295 if (format == DRM_FORMAT_YVU420)
296 stride = ALIGN(stride, 128);
297
298 /*
299 * HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned.
300 */
301 if (format == DRM_FORMAT_YVU420_ANDROID)
302 height = bo->height;
303
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700304 drv_bo_from_format(bo, stride, height, format);
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800305
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800306 memset(&gem_create, 0, sizeof(gem_create));
307 gem_create.size = bo->total_size;
Stéphane Marchesin5d867a42014-11-24 17:09:49 -0800308
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800309 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
310 if (ret) {
311 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n",
312 gem_create.size);
313 return ret;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700314 }
Gurchetan Singh83dc4fb2016-07-19 15:52:33 -0700315
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800316 for (plane = 0; plane < bo->num_planes; plane++)
317 bo->handles[plane].u32 = gem_create.handle;
Daniel Nicoara1de26dc2014-09-25 18:53:19 -0400318
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800319 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
320 gem_set_tiling.handle = bo->handles[0].u32;
321 gem_set_tiling.tiling_mode = bo->tiling;
322 gem_set_tiling.stride = bo->strides[0];
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700323
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800324 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
325 if (ret) {
326 struct drm_gem_close gem_close;
327 memset(&gem_close, 0, sizeof(gem_close));
328 gem_close.handle = bo->handles[0].u32;
329 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800330
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800331 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_TILING failed with %d", errno);
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700332 return -errno;
333 }
334
335 return 0;
336}
337
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800338static void i915_close(struct driver *drv)
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800339{
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800340 free(drv->priv);
341 drv->priv = NULL;
Gurchetan Singh82a8eed2017-01-03 13:01:37 -0800342}
343
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800344static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
345{
346 int ret;
347 struct drm_i915_gem_get_tiling gem_get_tiling;
348
349 ret = drv_prime_bo_import(bo, data);
350 if (ret)
351 return ret;
352
353 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
354 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
355 gem_get_tiling.handle = bo->handles[0].u32;
356
357 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
358 if (ret) {
Joe Kniss9e5d12a2017-06-29 11:54:22 -0700359 drv_gem_bo_destroy(bo);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800360 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_GET_TILING failed.");
361 return ret;
362 }
363
364 bo->tiling = gem_get_tiling.tiling_mode;
365 return 0;
366}
367
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700368static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700369{
370 int ret;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800371 void *addr;
372 struct drm_i915_gem_set_domain set_domain;
Gurchetan Singhef920532016-08-12 16:38:25 -0700373
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800374 memset(&set_domain, 0, sizeof(set_domain));
375 set_domain.handle = bo->handles[0].u32;
376 if (bo->tiling == I915_TILING_NONE) {
377 struct drm_i915_gem_mmap gem_map;
378 memset(&gem_map, 0, sizeof(gem_map));
Gurchetan Singhef920532016-08-12 16:38:25 -0700379
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800380 gem_map.handle = bo->handles[0].u32;
381 gem_map.offset = 0;
382 gem_map.size = bo->total_size;
383
384 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
385 if (ret) {
386 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP failed\n");
387 return MAP_FAILED;
388 }
389
390 addr = (void *)(uintptr_t)gem_map.addr_ptr;
391 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
392 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
393
394 } else {
395 struct drm_i915_gem_mmap_gtt gem_map;
396 memset(&gem_map, 0, sizeof(gem_map));
397
398 gem_map.handle = bo->handles[0].u32;
399
400 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
401 if (ret) {
402 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
403 return MAP_FAILED;
404 }
405
406 addr = mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->drv->fd,
407 gem_map.offset);
408
409 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
410 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
411 }
412
413 if (addr == MAP_FAILED) {
414 fprintf(stderr, "drv: i915 GEM mmap failed\n");
415 return addr;
416 }
417
418 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
Gurchetan Singhef920532016-08-12 16:38:25 -0700419 if (ret) {
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800420 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_DOMAIN failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -0700421 return MAP_FAILED;
422 }
423
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800424 data->length = bo->total_size;
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800425 return addr;
426}
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700427
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800428static int i915_bo_unmap(struct bo *bo, struct map_info *data)
429{
Gurchetan Singh68af9c22017-01-18 13:48:11 -0800430 struct i915_device *i915 = bo->drv->priv;
431 if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
432 i915_clflush(data->addr, data->length);
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800433
434 return munmap(data->addr, data->length);
Gurchetan Singhef920532016-08-12 16:38:25 -0700435}
436
Tomasz Figace1ae022017-07-05 18:15:06 +0900437static uint32_t i915_resolve_format(uint32_t format, uint64_t usage)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700438{
439 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800440 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700441 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800442 return DRM_FORMAT_XBGR8888;
443 case DRM_FORMAT_FLEX_YCbCr_420_888:
Owen Linbbb69fd2017-06-05 14:33:08 +0800444 return DRM_FORMAT_YVU420;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700445 default:
446 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700447 }
448}
449
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800450struct backend backend_i915 = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700451 .name = "i915",
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700452 .init = i915_init,
453 .close = i915_close,
454 .bo_create = i915_bo_create,
Gurchetan Singhcc015e82017-01-17 16:15:25 -0800455 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800456 .bo_import = i915_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700457 .bo_map = i915_bo_map,
Gurchetan Singhfcad5ad2017-01-05 20:39:31 -0800458 .bo_unmap = i915_bo_unmap,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700459 .resolve_format = i915_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700460};
461
462#endif