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 render_target_formats[] = { DRM_FORMAT_ABGR16161616F, DRM_FORMAT_ABGR2101010,
27 DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB2101010,
28 DRM_FORMAT_ARGB8888, DRM_FORMAT_RGB565,
29 DRM_FORMAT_XBGR2101010, DRM_FORMAT_XBGR8888,
30 DRM_FORMAT_XRGB2101010, DRM_FORMAT_XRGB8888 };
32 static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12, DRM_FORMAT_P010,
33 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
40 static uint32_t i915_get_gen(int device_id)
42 const uint16_t gen3_ids[] = { 0x2582, 0x2592, 0x2772, 0x27A2, 0x27AE,
43 0x29C2, 0x29B2, 0x29D2, 0xA001, 0xA011 };
45 for (i = 0; i < ARRAY_SIZE(gen3_ids); i++)
46 if (gen3_ids[i] == device_id)
53 * We allow allocation of ARGB formats for SCANOUT if the corresponding XRGB
54 * formats supports it. It's up to the caller (chrome ozone) to ultimately not
55 * scan out ARGB if the display controller only supports XRGB, but we'll allow
56 * the allocation of the bo here.
58 static bool format_compatible(const struct combination *combo, uint32_t format)
60 if (combo->format == format)
64 case DRM_FORMAT_XRGB8888:
65 return combo->format == DRM_FORMAT_ARGB8888;
66 case DRM_FORMAT_XBGR8888:
67 return combo->format == DRM_FORMAT_ABGR8888;
68 case DRM_FORMAT_RGBX8888:
69 return combo->format == DRM_FORMAT_RGBA8888;
70 case DRM_FORMAT_BGRX8888:
71 return combo->format == DRM_FORMAT_BGRA8888;
72 case DRM_FORMAT_XRGB2101010:
73 return combo->format == DRM_FORMAT_ARGB2101010;
74 case DRM_FORMAT_XBGR2101010:
75 return combo->format == DRM_FORMAT_ABGR2101010;
81 static int i915_add_kms_item(struct driver *drv, const struct kms_item *item)
84 struct combination *combo;
87 * Older hardware can't scanout Y-tiled formats. Newer devices can, and
88 * report this functionality via format modifiers.
90 for (i = 0; i < drv_array_size(drv->combos); i++) {
91 combo = (struct combination *)drv_array_at_idx(drv->combos, i);
92 if (!format_compatible(combo, item->format))
95 if (item->modifier == DRM_FORMAT_MOD_LINEAR &&
96 combo->metadata.tiling == I915_TILING_X) {
98 * FIXME: drv_query_kms() does not report the available modifiers
99 * yet, but we know that all hardware can scanout from X-tiled
100 * buffers, so let's add this to our combinations, except for
101 * cursor, which must not be tiled.
103 combo->use_flags |= item->use_flags & ~BO_USE_CURSOR;
106 /* If we can scanout NV12, we support all tiling modes. */
107 if (item->format == DRM_FORMAT_NV12)
108 combo->use_flags |= item->use_flags;
110 if (combo->metadata.modifier == item->modifier)
111 combo->use_flags |= item->use_flags;
117 static int i915_add_combinations(struct driver *drv)
121 struct drv_array *kms_items;
122 struct format_metadata metadata;
123 uint64_t render_use_flags, texture_use_flags;
125 render_use_flags = BO_USE_RENDER_MASK;
126 texture_use_flags = BO_USE_TEXTURE_MASK;
128 metadata.tiling = I915_TILING_NONE;
129 metadata.priority = 1;
130 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
132 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
133 &metadata, render_use_flags);
135 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
136 &metadata, texture_use_flags);
139 * Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the
140 * Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future.
142 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_ENCODER);
143 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
144 BO_USE_HW_VIDEO_ENCODER | BO_USE_HW_VIDEO_DECODER);
146 /* Android CTS tests require this. */
147 drv_add_combination(drv, DRM_FORMAT_BGR888, &metadata, BO_USE_SW_MASK);
149 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
150 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
152 /* IPU3 camera ISP supports only NV12 output. */
153 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
154 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
156 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
159 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
160 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
162 render_use_flags &= ~BO_USE_RENDERSCRIPT;
163 render_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
164 render_use_flags &= ~BO_USE_SW_READ_OFTEN;
165 render_use_flags &= ~BO_USE_LINEAR;
166 render_use_flags &= ~BO_USE_PROTECTED;
168 texture_use_flags &= ~BO_USE_RENDERSCRIPT;
169 texture_use_flags &= ~BO_USE_SW_WRITE_OFTEN;
170 texture_use_flags &= ~BO_USE_SW_READ_OFTEN;
171 texture_use_flags &= ~BO_USE_LINEAR;
172 texture_use_flags &= ~BO_USE_PROTECTED;
174 metadata.tiling = I915_TILING_X;
175 metadata.priority = 2;
176 metadata.modifier = I915_FORMAT_MOD_X_TILED;
178 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
179 &metadata, render_use_flags);
181 metadata.tiling = I915_TILING_Y;
182 metadata.priority = 3;
183 metadata.modifier = I915_FORMAT_MOD_Y_TILED;
185 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
186 &metadata, render_use_flags);
188 /* Support y-tiled NV12 and P010 for libva */
189 drv_add_combination(drv, DRM_FORMAT_NV12, &metadata,
190 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
191 drv_add_combination(drv, DRM_FORMAT_P010, &metadata,
192 BO_USE_TEXTURE | BO_USE_HW_VIDEO_DECODER);
194 kms_items = drv_query_kms(drv);
198 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));
201 drv_array_destroy(kms_items);
206 drv_array_destroy(kms_items);
210 static int i915_align_dimensions(struct bo *bo, uint32_t tiling, uint32_t *stride,
211 uint32_t *aligned_height)
213 struct i915_device *i915 = bo->drv->priv;
214 uint32_t horizontal_alignment;
215 uint32_t vertical_alignment;
219 case I915_TILING_NONE:
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
227 horizontal_alignment = 64;
228 vertical_alignment = 4;
232 horizontal_alignment = 512;
233 vertical_alignment = 8;
237 if (i915->gen == 3) {
238 horizontal_alignment = 512;
239 vertical_alignment = 8;
241 horizontal_alignment = 128;
242 vertical_alignment = 32;
247 *aligned_height = ALIGN(*aligned_height, vertical_alignment);
249 *stride = ALIGN(*stride, horizontal_alignment);
251 while (*stride > horizontal_alignment)
252 horizontal_alignment <<= 1;
254 *stride = horizontal_alignment;
257 if (i915->gen <= 3 && *stride > 8192)
263 static void i915_clflush(void *start, size_t size)
265 void *p = (void *)(((uintptr_t)start) & ~I915_CACHELINE_MASK);
266 void *end = (void *)((uintptr_t)start + size);
268 __builtin_ia32_mfence();
270 __builtin_ia32_clflush(p);
271 p = (void *)((uintptr_t)p + I915_CACHELINE_SIZE);
275 static int i915_init(struct driver *drv)
279 struct i915_device *i915;
280 drm_i915_getparam_t get_param;
282 i915 = calloc(1, sizeof(*i915));
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);
291 drv_log("Failed to get I915_PARAM_CHIPSET_ID\n");
296 i915->gen = i915_get_gen(device_id);
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);
303 drv_log("Failed to get I915_PARAM_HAS_LLC\n");
310 return i915_add_combinations(drv);
313 static int i915_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
320 pagesize = getpagesize();
321 for (plane = 0; plane < drv_num_planes_from_format(format); plane++) {
322 uint32_t stride = drv_stride_from_format(format, width, plane);
323 uint32_t plane_height = drv_height_from_format(format, height, plane);
325 if (bo->meta.tiling != I915_TILING_NONE)
326 assert(IS_ALIGNED(offset, pagesize));
328 ret = i915_align_dimensions(bo, bo->meta.tiling, &stride, &plane_height);
332 bo->meta.strides[plane] = stride;
333 bo->meta.sizes[plane] = stride * plane_height;
334 bo->meta.offsets[plane] = offset;
335 offset += bo->meta.sizes[plane];
338 bo->meta.total_size = ALIGN(offset, pagesize);
343 static int i915_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
344 uint32_t format, uint64_t modifier)
348 struct drm_i915_gem_create gem_create;
349 struct drm_i915_gem_set_tiling gem_set_tiling;
352 case DRM_FORMAT_MOD_LINEAR:
353 bo->meta.tiling = I915_TILING_NONE;
355 case I915_FORMAT_MOD_X_TILED:
356 bo->meta.tiling = I915_TILING_X;
358 case I915_FORMAT_MOD_Y_TILED:
359 bo->meta.tiling = I915_TILING_Y;
363 bo->meta.format_modifiers[0] = modifier;
365 if (format == DRM_FORMAT_YVU420_ANDROID) {
367 * We only need to be able to use this as a linear texture,
368 * which doesn't put any HW restrictions on how we lay it
369 * out. The Android format does require the stride to be a
370 * multiple of 16 and expects the Cr and Cb stride to be
371 * ALIGN(Y_stride / 2, 16), which we can make happen by
372 * aligning to 32 bytes here.
374 uint32_t stride = ALIGN(width, 32);
375 drv_bo_from_format(bo, stride, height, format);
377 i915_bo_from_format(bo, width, height, format);
380 memset(&gem_create, 0, sizeof(gem_create));
381 gem_create.size = bo->meta.total_size;
383 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
385 drv_log("DRM_IOCTL_I915_GEM_CREATE failed (size=%llu)\n", gem_create.size);
389 for (plane = 0; plane < bo->meta.num_planes; plane++)
390 bo->handles[plane].u32 = gem_create.handle;
392 memset(&gem_set_tiling, 0, sizeof(gem_set_tiling));
393 gem_set_tiling.handle = bo->handles[0].u32;
394 gem_set_tiling.tiling_mode = bo->meta.tiling;
395 gem_set_tiling.stride = bo->meta.strides[0];
397 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_TILING, &gem_set_tiling);
399 struct drm_gem_close gem_close;
400 memset(&gem_close, 0, sizeof(gem_close));
401 gem_close.handle = bo->handles[0].u32;
402 drmIoctl(bo->drv->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
404 drv_log("DRM_IOCTL_I915_GEM_SET_TILING failed with %d\n", errno);
411 static int i915_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
414 struct combination *combo;
416 combo = drv_get_combination(bo->drv, format, use_flags);
420 return i915_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
423 static int i915_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
424 uint32_t format, const uint64_t *modifiers, uint32_t count)
426 static const uint64_t modifier_order[] = {
427 I915_FORMAT_MOD_Y_TILED,
428 I915_FORMAT_MOD_X_TILED,
429 DRM_FORMAT_MOD_LINEAR,
433 modifier = drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
435 return i915_bo_create_for_modifier(bo, width, height, format, modifier);
438 static void i915_close(struct driver *drv)
444 static int i915_bo_import(struct bo *bo, struct drv_import_fd_data *data)
447 struct drm_i915_gem_get_tiling gem_get_tiling;
449 ret = drv_prime_bo_import(bo, data);
453 /* TODO(gsingh): export modifiers and get rid of backdoor tiling. */
454 memset(&gem_get_tiling, 0, sizeof(gem_get_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.tiling == I915_TILING_NONE) {
474 struct drm_i915_gem_mmap gem_map;
475 memset(&gem_map, 0, sizeof(gem_map));
477 /* TODO(b/118799155): We don't seem to have a good way to
478 * detect the use cases for which WC mapping is really needed.
479 * The current heuristic seems overly coarse and may be slowing
480 * down some other use cases unnecessarily.
482 * For now, care must be taken not to use WC mappings for
483 * Renderscript and camera use cases, as they're
484 * performance-sensitive. */
485 if ((bo->meta.use_flags & BO_USE_SCANOUT) &&
486 !(bo->meta.use_flags &
487 (BO_USE_RENDERSCRIPT | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)))
488 gem_map.flags = I915_MMAP_WC;
490 gem_map.handle = bo->handles[0].u32;
492 gem_map.size = bo->meta.total_size;
494 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
496 drv_log("DRM_IOCTL_I915_GEM_MMAP failed\n");
500 addr = (void *)(uintptr_t)gem_map.addr_ptr;
502 struct drm_i915_gem_mmap_gtt gem_map;
503 memset(&gem_map, 0, sizeof(gem_map));
505 gem_map.handle = bo->handles[0].u32;
507 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
509 drv_log("DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
513 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED,
514 bo->drv->fd, gem_map.offset);
517 if (addr == MAP_FAILED) {
518 drv_log("i915 GEM mmap failed\n");
522 vma->length = bo->meta.total_size;
526 static int i915_bo_invalidate(struct bo *bo, struct mapping *mapping)
529 struct drm_i915_gem_set_domain set_domain;
531 memset(&set_domain, 0, sizeof(set_domain));
532 set_domain.handle = bo->handles[0].u32;
533 if (bo->meta.tiling == I915_TILING_NONE) {
534 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
535 if (mapping->vma->map_flags & BO_MAP_WRITE)
536 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
538 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
539 if (mapping->vma->map_flags & BO_MAP_WRITE)
540 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
543 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
545 drv_log("DRM_IOCTL_I915_GEM_SET_DOMAIN with %d\n", ret);
552 static int i915_bo_flush(struct bo *bo, struct mapping *mapping)
554 struct i915_device *i915 = bo->drv->priv;
555 if (!i915->has_llc && bo->meta.tiling == I915_TILING_NONE)
556 i915_clflush(mapping->vma->addr, mapping->vma->length);
561 static uint32_t i915_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
564 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
565 /* KBL camera subsystem requires NV12. */
566 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
567 return DRM_FORMAT_NV12;
568 /*HACK: See b/28671744 */
569 return DRM_FORMAT_XBGR8888;
570 case DRM_FORMAT_FLEX_YCbCr_420_888:
572 * KBL camera subsystem requires NV12. Our other use cases
574 * - Hardware video supports NV12,
575 * - USB Camera HALv3 supports NV12,
576 * - USB Camera HALv1 doesn't use this format.
577 * Moreover, NV12 is preferred for video, due to overlay
580 return DRM_FORMAT_NV12;
586 const struct backend backend_i915 = {
590 .bo_create = i915_bo_create,
591 .bo_create_with_modifiers = i915_bo_create_with_modifiers,
592 .bo_destroy = drv_gem_bo_destroy,
593 .bo_import = i915_bo_import,
594 .bo_map = i915_bo_map,
595 .bo_unmap = drv_bo_munmap,
596 .bo_invalidate = i915_bo_invalidate,
597 .bo_flush = i915_bo_flush,
598 .resolve_format = i915_resolve_format,