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.
23 #define I915_CACHELINE_SIZE 64
24 #define I915_CACHELINE_MASK (I915_CACHELINE_SIZE - 1)
26 static const uint32_t scanout_render_formats[] = { DRM_FORMAT_ABGR2101010, DRM_FORMAT_ABGR8888,
27 DRM_FORMAT_ARGB2101010, DRM_FORMAT_ARGB8888,
28 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR2101010,
29 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB2101010,
30 DRM_FORMAT_XRGB8888 };
32 static const uint32_t render_formats[] = { DRM_FORMAT_ABGR16161616F };
34 static const uint32_t texture_only_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12, DRM_FORMAT_P010,
35 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
37 static const uint64_t gen_modifier_order[] = { I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED,
38 DRM_FORMAT_MOD_LINEAR };
40 static const uint64_t gen11_modifier_order[] = { I915_FORMAT_MOD_Y_TILED_CCS,
41 I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED,
42 DRM_FORMAT_MOD_LINEAR };
44 struct modifier_support_t {
45 const uint64_t *order;
52 struct modifier_support_t modifier;
55 static uint32_t i915_get_gen(int device_id)
57 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
58 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
59 const uint16_t gen11_ids[] = { 0x4E71, 0x4E61, 0x4E51, 0x4E55, 0x4E57 };
62 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
63 if (gen3_ids[i] == device_id)
66 for (i = 0; i < ARRAY_SIZE(gen11_ids); i++)
67 if (gen11_ids[i] == device_id)
73 static void i915_get_modifier_order(struct i915_device *i915)
75 if (i915->gen == 11) {
76 i915->modifier.order = gen11_modifier_order;
77 i915->modifier.count = ARRAY_SIZE(gen11_modifier_order);
79 i915->modifier.order = gen_modifier_order;
80 i915->modifier.count = ARRAY_SIZE(gen_modifier_order);
84 static uint64_t unset_flags(uint64_t current_flags, uint64_t mask)
86 uint64_t value = current_flags & ~mask;
90 static int i915_add_combinations(struct driver *drv)
92 struct format_metadata metadata;
93 uint64_t render, scanout_and_render, texture_only;
95 scanout_and_render = BO_USE_RENDER_MASK | BO_USE_SCANOUT;
96 render = BO_USE_RENDER_MASK;
97 texture_only = BO_USE_TEXTURE_MASK;
98 uint64_t linear_mask = BO_USE_RENDERSCRIPT | BO_USE_LINEAR | BO_USE_PROTECTED |
99 BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN;
101 metadata.tiling = I915_TILING_NONE;
102 metadata.priority = 1;
103 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
105 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
106 &metadata, scanout_and_render);
108 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
110 drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata,
113 drv_modify_linear_combinations(drv);
115 /* NV12 format for camera, display, decoding and encoding. */
116 /* IPU3 camera ISP supports only NV12 output. */
117 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
118 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
119 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
121 /* Android CTS tests require this. */
122 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
125 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
126 * from camera and input/output from hardware decoder/encoder.
128 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
129 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
130 BO_USE_HW_VIDEO_ENCODER);
132 render = unset_flags(render, linear_mask);
133 scanout_and_render = unset_flags(scanout_and_render, linear_mask);
135 metadata.tiling = I915_TILING_X;
136 metadata.priority = 2;
137 metadata.modifier = I915_FORMAT_MOD_X_TILED;
139 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
140 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
141 &metadata, scanout_and_render);
143 metadata.tiling = I915_TILING_Y;
144 metadata.priority = 3;
145 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
148 unset_flags(scanout_and_render, BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY);
149 /* Support y-tiled NV12 and P010 for libva */
150 #ifdef I915_SCANOUT_Y_TILED
151 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
152 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT);
154 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
155 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
157 scanout_and_render = unset_flags(scanout_and_render, BO_USE_SCANOUT);
158 drv_add_combination(drv, DRM_FORMAT_P010, &metadata,
159 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
161 drv_add_combinations(drv, render_formats, ARRAY_SIZE(render_formats), &metadata, render);
162 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
163 &metadata, scanout_and_render);
167 static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
168 uint32_t *aligned_height)
170 struct i915_device *i915 = bo->drv->priv;
171 uint32_t horizontal_alignment;
172 uint32_t vertical_alignment;
176 case I915_TILING_NONE:
178 * The Intel GPU doesn't need any alignment in linear mode,
179 * but libva requires the allocation stride to be aligned to
180 * 16 bytes and height to 4 rows. Further, we round up the
181 * horizontal alignment so that row start on a cache line (64
184 horizontal_alignment = 64;
185 vertical_alignment = 4;
189 horizontal_alignment = 512;
190 vertical_alignment = 8;
194 if (i915->gen == 3) {
195 horizontal_alignment = 512;
196 vertical_alignment = 8;
198 horizontal_alignment = 128;
199 vertical_alignment = 32;
204 *aligned_height = ALIGN(*aligned_height, vertical_alignment);
206 *stride = ALIGN(*stride, horizontal_alignment);
208 while (*stride > horizontal_alignment)
209 horizontal_alignment <<= 1;
211 *stride = horizontal_alignment;
214 if (i915->gen <= 3 && *stride > 8192)
220 static void i915_clflush(void *start, size_t size)
222 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
223 void *end = (void *)((uintptr_t)start + size);
225 __builtin_ia32_mfence();
227 __builtin_ia32_clflush(p);
228 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
232 static int i915_init(struct driver *drv)
236 struct i915_device *i915;
237 drm_i915_getparam_t get_param = { 0 };
239 i915 = calloc(1, sizeof(*i915));
243 get_param.param = I915_PARAM_CHIPSET_ID;
244 get_param.value = &device_id;
245 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
247 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
252 i915->gen = i915_get_gen(device_id);
253 i915_get_modifier_order(i915);
255 memset(&get_param, 0, sizeof(get_param));
256 get_param.param = I915_PARAM_HAS_LLC;
257 get_param.value = &i915->has_llc;
258 ret = drmIoctl(drv->fd, DRM_IOCTL_I915_GETPARAM, &get_param);
260 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
267 return i915_add_combinations(drv);
270 static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
277 pagesize = getpagesize();
278 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
279 uint32_t stride = drv_stride_from_format(format, width, plane);
280 uint32_t plane_height = drv_height_from_format(format, height, plane);
282 if (bo->meta.tiling != I915_TILING_NONE)
283 assert(IS_ALIGNED(offset, pagesize));
285 ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height);
289 bo->meta.strides[plane] = stride;
290 bo->meta.sizes[plane] = stride * plane_height;
291 bo->meta.offsets[plane] = offset;
292 offset += bo->meta.sizes[plane];
295 bo->meta.total_size = ALIGN(offset, pagesize);
300 static int i915_bo_compute_metadata(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
301 uint64_t use_flags, const uint64_t *modifiers, uint32_t count)
304 struct i915_device *i915 = bo->drv->priv;
305 bool huge_bo = (i915->gen <= 11) && (width > 4096);
309 drv_pick_modifier(modifiers, count, i915->modifier.order, i915->modifier.count);
311 struct combination *combo = drv_get_combination(bo->drv, format, use_flags);
314 modifier = combo->metadata.modifier;
318 * i915 only supports linear/x-tiled above 4096 wide
320 if (huge_bo && modifier != I915_FORMAT_MOD_X_TILED && modifier != DRM_FORMAT_MOD_LINEAR) {
322 for (i = 0; modifiers && i < count; i++) {
323 if (modifiers[i] == I915_FORMAT_MOD_X_TILED)
327 modifier = DRM_FORMAT_MOD_LINEAR;
329 modifier = I915_FORMAT_MOD_X_TILED;
333 case DRM_FORMAT_MOD_LINEAR:
334 bo->meta.tiling = I915_TILING_NONE;
336 case I915_FORMAT_MOD_X_TILED:
337 bo->meta.tiling = I915_TILING_X;
339 case I915_FORMAT_MOD_Y_TILED:
340 case I915_FORMAT_MOD_Y_TILED_CCS:
341 bo->meta.tiling = I915_TILING_Y;
345 bo->meta.format_modifiers[0] = modifier;
347 if (format == DRM_FORMAT_YVU420_ANDROID) {
349 * We only need to be able to use this as a linear texture,
350 * which doesn't put any HW restrictions on how we lay it
351 * out. The Android format does require the stride to be a
352 * multiple of 16 and expects the Cr and Cb stride to be
353 * ALIGN(Y_stride / 2, 16), which we can make happen by
354 * aligning to 32 bytes here.
356 uint32_t stride = ALIGN(width, 32);
357 drv_bo_from_format(bo, stride, height, format);
358 } else if (modifier == I915_FORMAT_MOD_Y_TILED_CCS) {
360 * For compressed surfaces, we need a color control surface
361 * (CCS). Color compression is only supported for Y tiled
362 * surfaces, and for each 32x16 tiles in the main surface we
363 * need a tile in the control surface. Y tiles are 128 bytes
364 * wide and 32 lines tall and we use that to first compute the
365 * width and height in tiles of the main surface. stride and
366 * height are already multiples of 128 and 32, respectively:
368 uint32_t stride = drv_stride_from_format(format, width, 0);
369 uint32_t width_in_tiles = DIV_ROUND_UP(stride, 128);
370 uint32_t height_in_tiles = DIV_ROUND_UP(height, 32);
371 uint32_t size = width_in_tiles * height_in_tiles * 4096;
374 bo->meta.strides[0] = width_in_tiles * 128;
375 bo->meta.sizes[0] = size;
376 bo->meta.offsets[0] = offset;
380 * Now, compute the width and height in tiles of the control
381 * surface by dividing and rounding up.
383 uint32_t ccs_width_in_tiles = DIV_ROUND_UP(width_in_tiles, 32);
384 uint32_t ccs_height_in_tiles = DIV_ROUND_UP(height_in_tiles, 16);
385 uint32_t ccs_size = ccs_width_in_tiles * ccs_height_in_tiles * 4096;
388 * With stride and height aligned to y tiles, offset is
389 * already a multiple of 4096, which is the required alignment
392 bo->meta.strides[1] = ccs_width_in_tiles * 128;
393 bo->meta.sizes[1] = ccs_size;
394 bo->meta.offsets[1] = offset;
397 bo->meta.num_planes = 2;
398 bo->meta.total_size = offset;
400 i915_bo_from_format(bo, width, height, format);
405 static int i915_bo_create_from_metadata(struct bo *bo)
409 struct drm_i915_gem_create gem_create = { 0 };
410 struct drm_i915_gem_set_tiling gem_set_tiling = { 0 };
412 gem_create.size = bo->meta.total_size;
413 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
415 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
419 for (plane = 0; plane < bo->meta.num_planes; plane++)
420 bo->handles[plane].u32 = gem_create.handle;
422 gem_set_tiling.handle = bo->handles[0].u32;
423 gem_set_tiling.tiling_mode = bo->meta.tiling;
424 gem_set_tiling.stride = bo->meta.strides[0];
426 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
428 struct drm_gem_close gem_close = { 0 };
429 gem_close.handle = bo->handles[0].u32;
430 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
432 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
439 static void i915_close(struct driver *drv)
445 static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
448 struct drm_i915_gem_get_tiling gem_get_tiling = { 0 };
450 ret = drv_prime_bo_import(bo, data);
454 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
455 gem_get_tiling.handle = bo->handles[0].u32;
457 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_GET_TILING, &gem_get_tiling);
459 drv_gem_bo_destroy(bo);
460 drv_log("DRM_IOCTL_I915_GEM_GET_TILING failed.\n");
464 bo->meta.tiling = gem_get_tiling.tiling_mode;
468 static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
473 if (bo->meta.format_modifiers[0] == I915_FORMAT_MOD_Y_TILED_CCS)
476 if (bo->meta.tiling == I915_TILING_NONE) {
477 struct drm_i915_gem_mmap gem_map = { 0 };
478 /* 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.
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->meta.use_flags & BO_USE_SCANOUT) &&
487 !(bo->meta.use_flags &
488 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
489 gem_map.flags = I915_MMAP_WC;
491 gem_map.handle = bo->handles[0].u32;
493 gem_map.size = bo->meta.total_size;
495 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
497 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
501 addr = (void *)(uintptr_t)gem_map.addr_ptr;
503 struct drm_i915_gem_mmap_gtt gem_map = { 0 };
505 gem_map.handle = bo->handles[0].u32;
506 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
508 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
512 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED,
513 bo->drv->fd, gem_map.offset);
516 if (addr == MAP_FAILED) {
517 drv_log("i915 GEM mmap failed\n");
521 vma->length = bo->meta.total_size;
525 static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
528 struct drm_i915_gem_set_domain set_domain = { 0 };
530 set_domain.handle = bo->handles[0].u32;
531 if (bo->meta.tiling == I915_TILING_NONE) {
532 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
533 if (mapping->vma->map_flags & BO_MAP_WRITE)
534 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
536 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
537 if (mapping->vma->map_flags & BO_MAP_WRITE)
538 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
541 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
543 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
550 static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
552 struct i915_device *i915 = bo->drv->priv;
553 if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE)
554 i915_clflush(mapping->vma->addr, mapping->vma->length);
559 static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
562 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
563 /* KBL camera subsystem requires NV12. */
564 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
565 return DRM_FORMAT_NV12;
566 /*HACK: See b/28671744 */
567 return DRM_FORMAT_XBGR8888;
568 case DRM_FORMAT_FLEX_YCbCr_420_888:
570 * KBL camera subsystem requires NV12. Our other use cases
572 * - Hardware video supports NV12,
573 * - USB Camera HALv3 supports NV12,
574 * - USB Camera HALv1 doesn't use this format.
575 * Moreover, NV12 is preferred for video, due to overlay
578 return DRM_FORMAT_NV12;
584 const struct backend backend_i915 = {
588 .bo_compute_metadata = i915_bo_compute_metadata,
589 .bo_create_from_metadata = i915_bo_create_from_metadata,
590 .bo_destroy = drv_gem_bo_destroy,
591 .bo_import = i915_bo_import,
592 .bo_map = i915_bo_map,
593 .bo_unmap = drv_bo_munmap,
594 .bo_invalidate = i915_bo_invalidate,
595 .bo_flush = i915_bo_flush,
596 .resolve_format = i915_resolve_format,