2 * Copyright 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
20 #define I915_CACHELINE_SIZE 64
21 #define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
23 static 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 };
28 static const uint32_t tileable_texture_source_formats[] = { DRM_FORMAT_GR88, DRM_FORMAT_NV12,
29 DRM_FORMAT_R8, DRM_FORMAT_UYVY,
32 static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
39 static uint32_t i915_get_gen(int device_id)
41 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
42 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
44 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
45 if (gen3_ids[i] == device_id)
51 static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
54 struct combination *combo;
57 * Older hardware can't scanout Y-tiled formats. Newer devices can, and
58 * report this functionality via format modifiers.
60 for (i = 0; i < drv->backend->combos.size; i++) {
61 combo = &drv->backend->combos.data[i];
62 if (combo->format != item->format)
65 if (item->modifier == DRM_FORMAT_MOD_NONE &&
66 combo->metadata.tiling == I915_TILING_X) {
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.
73 combo->usage |= item->usage & ~BO_USE_CURSOR;
76 if (combo->metadata.modifier == item->modifier)
77 combo->usage |= item->usage;
83 static int i915_add_combinations(struct driver *drv)
86 uint32_t i, num_items;
87 struct kms_item *items;
88 struct format_metadata metadata;
89 uint64_t render_flags, texture_flags;
91 render_flags = BO_USE_RENDER_MASK;
92 texture_flags = BO_USE_TEXTURE_MASK;
94 metadata.tiling = I915_TILING_NONE;
95 metadata.priority = 1;
96 metadata.modifier = DRM_FORMAT_MOD_NONE;
98 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
99 &metadata, render_flags);
103 ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
104 &metadata, texture_flags);
108 ret = drv_add_combinations(drv, tileable_texture_source_formats,
109 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
114 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);
117 /* IPU3 camera ISP supports only NV12 output. */
118 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
119 BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE);
121 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
124 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
125 BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE);
127 render_flags &= ~BO_USE_SW_WRITE_OFTEN;
128 render_flags &= ~BO_USE_SW_READ_OFTEN;
129 render_flags &= ~BO_USE_LINEAR;
131 texture_flags &= ~BO_USE_SW_WRITE_OFTEN;
132 texture_flags &= ~BO_USE_SW_READ_OFTEN;
133 texture_flags &= ~BO_USE_LINEAR;
135 metadata.tiling = I915_TILING_X;
136 metadata.priority = 2;
137 metadata.modifier = I915_FORMAT_MOD_X_TILED;
139 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
140 &metadata, render_flags);
144 ret = drv_add_combinations(drv, tileable_texture_source_formats,
145 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
150 metadata.tiling = I915_TILING_Y;
151 metadata.priority = 3;
152 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
154 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
155 &metadata, render_flags);
159 ret = drv_add_combinations(drv, tileable_texture_source_formats,
160 ARRAY_SIZE(tileable_texture_source_formats), &metadata,
165 items = drv_query_kms(drv, &num_items);
166 if (!items || !num_items)
169 for (i = 0; i < num_items; i++) {
170 ret = i915_add_kms_item(drv, &items[i]);
181 static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
182 uint32_t *aligned_height)
184 struct i915_device *i915 = bo->drv->priv;
185 uint32_t horizontal_alignment = 4;
186 uint32_t vertical_alignment = 4;
190 case I915_TILING_NONE:
191 horizontal_alignment = 64;
195 horizontal_alignment = 512;
196 vertical_alignment = 8;
200 if (i915->gen == 3) {
201 horizontal_alignment = 512;
202 vertical_alignment = 8;
204 horizontal_alignment = 128;
205 vertical_alignment = 32;
210 *aligned_height = ALIGN(bo->height, vertical_alignment);
212 *stride = ALIGN(*stride, horizontal_alignment);
214 while (*stride > horizontal_alignment)
215 horizontal_alignment <<= 1;
217 *stride = horizontal_alignment;
220 if (i915->gen <= 3 && *stride > 8192)
226 static void i915_clflush(void *start, size_t size)
228 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
229 void *end = (void *)((uintptr_t)start + size);
231 __builtin_ia32_mfence();
233 __builtin_ia32_clflush(p);
234 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
238 static int i915_init(struct driver *drv)
242 struct i915_device *i915;
243 drm_i915_getparam_t get_param;
245 i915 = calloc(1, sizeof(*i915));
249 memset(&get_param, 0, sizeof(get_param));
250 get_param.param = I915_PARAM_CHIPSET_ID;
251 get_param.value = &device_id;
252 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
254 fprintf(stderr, "drv: Failed to get I915_PARAM_CHIPSET_ID\n");
259 i915->gen = i915_get_gen(device_id);
261 memset(&get_param, 0, sizeof(get_param));
262 get_param.param = I915_PARAM_HAS_LLC;
263 get_param.value = &i915->has_llc;
264 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
266 fprintf(stderr, "drv: Failed to get I915_PARAM_HAS_LLC\n");
273 return i915_add_combinations(drv);
276 static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
282 struct drm_i915_gem_create gem_create;
283 struct drm_i915_gem_set_tiling gem_set_tiling;
284 struct combination *combo;
286 combo = drv_get_combination(bo->drv, format, flags);
290 bo->tiling = combo->metadata.tiling;
292 stride = drv_stride_from_format(format, width, 0);
294 ret = i915_align_dimensions(bo, bo->tiling, &stride, &height);
299 * Align the Y plane to 128 bytes so the chroma planes would be aligned
300 * to 64 byte boundaries. This is an Intel HW requirement.
302 if (format == DRM_FORMAT_YVU420)
303 stride = ALIGN(stride, 128);
306 * HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned.
308 if (format == DRM_FORMAT_YVU420_ANDROID)
311 drv_bo_from_format(bo, stride, height, format);
313 memset(&gem_create, 0, sizeof(gem_create));
314 gem_create.size = bo->total_size;
316 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
318 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n",
323 for (plane = 0; plane < bo->num_planes; plane++)
324 bo->handles[plane].u32 = gem_create.handle;
326 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
327 gem_set_tiling.handle = bo->handles[0].u32;
328 gem_set_tiling.tiling_mode = bo->tiling;
329 gem_set_tiling.stride = bo->strides[0];
331 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
333 struct drm_gem_close gem_close;
334 memset(&gem_close, 0, sizeof(gem_close));
335 gem_close.handle = bo->handles[0].u32;
336 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
338 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_TILING failed with %d", errno);
345 static void i915_close(struct driver *drv)
351 static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
354 struct drm_i915_gem_get_tiling gem_get_tiling;
356 ret = drv_prime_bo_import(bo, data);
360 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
361 memset(&gem_get_tiling, 0, sizeof(gem_get_tiling));
362 gem_get_tiling.handle = bo->handles[0].u32;
364 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
366 drv_gem_bo_destroy(bo);
367 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_GET_TILING failed.");
371 bo->tiling = gem_get_tiling.tiling_mode;
375 static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane, int prot)
379 struct drm_i915_gem_set_domain set_domain;
381 memset(&set_domain, 0, sizeof(set_domain));
382 set_domain.handle = bo->handles[0].u32;
383 if (bo->tiling == I915_TILING_NONE) {
384 struct drm_i915_gem_mmap gem_map;
385 memset(&gem_map, 0, sizeof(gem_map));
387 gem_map.handle = bo->handles[0].u32;
389 gem_map.size = bo->total_size;
391 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
393 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP failed\n");
397 addr = (void *)(uintptr_t)gem_map.addr_ptr;
398 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
399 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
402 struct drm_i915_gem_mmap_gtt gem_map;
403 memset(&gem_map, 0, sizeof(gem_map));
405 gem_map.handle = bo->handles[0].u32;
407 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
409 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
413 addr = mmap(0, bo->total_size, prot, MAP_SHARED, bo->drv->fd, gem_map.offset);
414 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
415 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
418 if (addr == MAP_FAILED) {
419 fprintf(stderr, "drv: i915 GEM mmap failed\n");
423 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
425 fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_DOMAIN failed\n");
429 data->length = bo->total_size;
433 static int i915_bo_unmap(struct bo *bo, struct map_info *data)
435 struct i915_device *i915 = bo->drv->priv;
436 if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
437 i915_clflush(data->addr, data->length);
439 return munmap(data->addr, data->length);
442 static uint32_t i915_resolve_format(uint32_t format, uint64_t usage)
445 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
446 /* KBL camera subsystem requires NV12. */
447 if (usage & (BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE))
448 return DRM_FORMAT_NV12;
449 /*HACK: See b/28671744 */
450 return DRM_FORMAT_XBGR8888;
451 case DRM_FORMAT_FLEX_YCbCr_420_888:
452 /* KBL camera subsystem requires NV12. */
453 if (usage & (BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE))
454 return DRM_FORMAT_NV12;
455 return DRM_FORMAT_YVU420;
461 struct backend backend_i915 = {
465 .bo_create = i915_bo_create,
466 .bo_destroy = drv_gem_bo_destroy,
467 .bo_import = i915_bo_import,
468 .bo_map = i915_bo_map,
469 .bo_unmap = i915_bo_unmap,
470 .resolve_format = i915_resolve_format,