2 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3 * Copyright (C) 2010-2011 LunarG Inc.
5 * drm_gem_intel_copy is based on xorg-driver-intel, which has
7 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
9 * Copyright (c) 2005 Jesse Barnes <jbarnes@virtuousgeek.org>
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
30 #define LOG_TAG "GRALLOC-I915"
32 #include <cutils/log.h>
37 #include <intel_bufmgr.h>
40 #include "gralloc_drm.h"
41 #include "gralloc_drm_priv.h"
44 #define MI_BATCH_BUFFER_END (0x0a << 23)
45 #define MI_FLUSH (0x04 << 23)
46 #define MI_FLUSH_DW (0x26 << 23)
47 #define MI_WRITE_DIRTY_STATE (1 << 4)
48 #define MI_INVALIDATE_MAP_CACHE (1 << 0)
49 #define XY_SRC_COPY_BLT_CMD ((2 << 29) | (0x53 << 22) | 6)
50 #define XY_SRC_COPY_BLT_WRITE_ALPHA (1 << 21)
51 #define XY_SRC_COPY_BLT_WRITE_RGB (1 << 20)
52 #define XY_SRC_COPY_BLT_SRC_TILED (1 << 15)
53 #define XY_SRC_COPY_BLT_DST_TILED (1 << 11)
56 struct gralloc_drm_drv_t base;
59 drm_intel_bufmgr *bufmgr;
62 drm_intel_bo *batch_ibo;
63 uint32_t *batch, *cur;
69 struct gralloc_drm_bo_t base;
75 batch_next(struct intel_info *info)
77 info->cur = info->batch;
80 drm_intel_bo_unreference(info->batch_ibo);
82 info->batch_ibo = drm_intel_bo_alloc(info->bufmgr,
83 "gralloc-batchbuffer", info->size, 4096);
85 return (info->batch_ibo) ? 0 : -ENOMEM;
89 batch_count(struct intel_info *info)
91 return info->cur - info->batch;
95 batch_dword(struct intel_info *info, uint32_t dword)
101 batch_reloc(struct intel_info *info, struct gralloc_drm_bo_t *bo,
102 uint32_t read_domains, uint32_t write_domain)
104 struct intel_buffer *target = (struct intel_buffer *) bo;
105 uint32_t offset = (info->cur - info->batch) * sizeof(info->batch[0]);
108 ret = drm_intel_bo_emit_reloc(info->batch_ibo, offset,
109 target->ibo, 0, read_domains, write_domain);
111 batch_dword(info, target->ibo->offset);
117 batch_flush(struct intel_info *info)
121 batch_dword(info, MI_BATCH_BUFFER_END);
122 size = batch_count(info);
124 batch_dword(info, MI_NOOP);
125 size = batch_count(info);
128 size *= sizeof(info->batch[0]);
129 ret = drm_intel_bo_subdata(info->batch_ibo, 0, size, info->batch);
131 ALOGE("failed to subdata batch");
134 ret = drm_intel_bo_mrb_exec(info->batch_ibo, size,
135 NULL, 0, 0, info->exec_blt);
137 ALOGE("failed to exec batch");
141 return batch_next(info);
144 info->cur = info->batch;
150 batch_reserve(struct intel_info *info, int count)
154 if (batch_count(info) + count > info->capacity)
155 ret = batch_flush(info);
161 batch_destroy(struct intel_info *info)
163 if (info->batch_ibo) {
164 drm_intel_bo_unreference(info->batch_ibo);
165 info->batch_ibo = NULL;
175 batch_init(struct intel_info *info)
179 info->capacity = 512;
180 info->size = (info->capacity + 16) * sizeof(info->batch[0]);
182 info->batch = malloc(info->size);
186 ret = batch_next(info);
195 static void intel_resolve_format(struct gralloc_drm_drv_t *drv,
196 struct gralloc_drm_bo_t *bo,
197 uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
200 * TODO - should take account hw specific padding, alignment
201 * for camera, video decoder etc.
204 struct intel_buffer *ib = (struct intel_buffer *) bo;
206 memset(pitches, 0, 4 * sizeof(uint32_t));
207 memset(offsets, 0, 4 * sizeof(uint32_t));
208 memset(handles, 0, 4 * sizeof(uint32_t));
210 pitches[0] = ib->base.handle->stride;
211 handles[0] = ib->base.fb_handle;
213 switch(ib->base.handle->format) {
214 case HAL_PIXEL_FORMAT_YV12:
216 // U and V stride are half of Y plane
217 pitches[2] = pitches[0]/2;
218 pitches[1] = pitches[0]/2;
220 // like I420 but U and V are in reverse order
221 offsets[2] = offsets[0] +
222 pitches[0] * ib->base.handle->height;
223 offsets[1] = offsets[2] +
224 pitches[2] * ib->base.handle->height/2;
226 handles[1] = handles[2] = handles[0];
229 case HAL_PIXEL_FORMAT_DRM_NV12:
231 // U and V are interleaved in 2nd plane
232 pitches[1] = pitches[0];
233 offsets[1] = offsets[0] +
234 pitches[0] * ib->base.handle->height;
236 handles[1] = handles[0];
242 static void intel_blit(struct gralloc_drm_drv_t *drv,
243 struct gralloc_drm_bo_t *dst,
244 struct gralloc_drm_bo_t *src,
245 uint16_t dst_x1, uint16_t dst_y1,
246 uint16_t dst_x2, uint16_t dst_y2,
247 uint16_t src_x1, uint16_t src_y1,
248 uint16_t src_x2, uint16_t src_y2)
250 struct intel_info *info = (struct intel_info *) drv;
251 struct intel_buffer *dst_ib = (struct intel_buffer *) dst;
252 struct intel_buffer *src_ib = (struct intel_buffer *) src;
253 drm_intel_bo *bo_table[3];
254 uint32_t cmd, br13, dst_pitch, src_pitch;
257 * XY_SRC_COPY_BLT_CMD does not support scaling,
258 * rectangle dimensions much match
260 if (src_x2 - src_x1 != dst_x2 - dst_x1 ||
261 src_y2 - src_y1 != dst_y2 - dst_y1) {
262 ALOGE("%s, src and dst rect must match", __func__);
266 if (dst->handle->format != src->handle->format) {
267 ALOGE("%s, src and dst format must match", __func__);
271 /* nothing to blit */
272 if (src_x2 <= src_x1 || src_y2 <= src_y1)
275 /* clamp x2, y2 to surface size */
276 if (src_x2 > src->handle->width)
277 src_x2 = src->handle->width;
278 if (src_y2 > src->handle->height)
279 src_y2 = src->handle->height;
281 if (dst_x2 > dst->handle->width)
282 dst_x2 = dst->handle->width;
283 if (dst_y2 > dst->handle->height)
284 dst_y2 = dst->handle->height;
286 bo_table[0] = info->batch_ibo;
287 bo_table[1] = src_ib->ibo;
288 bo_table[2] = dst_ib->ibo;
289 if (drm_intel_bufmgr_check_aperture_space(bo_table, 3)) {
290 if (batch_flush(info))
292 assert(!drm_intel_bufmgr_check_aperture_space(bo_table, 3));
295 cmd = XY_SRC_COPY_BLT_CMD;
296 br13 = 0xcc << 16; /* ROP_S/GXcopy */
297 dst_pitch = dst->handle->stride;
298 src_pitch = src->handle->stride;
300 /* Blit pitch must be dword-aligned. Otherwise, the hardware appears to
303 if (src_pitch % 4 != 0 || dst_pitch % 4 != 0) {
304 ALOGE("%s, src and dst pitch must be dword aligned", __func__);
308 switch (gralloc_drm_get_bpp(dst->handle->format)) {
315 br13 |= (1 << 24) | (1 << 25);
316 cmd |= XY_SRC_COPY_BLT_WRITE_ALPHA | XY_SRC_COPY_BLT_WRITE_RGB;
319 ALOGE("%s, copy with unsupported format", __func__);
323 if (info->gen >= 40) {
324 if (dst_ib->tiling != I915_TILING_NONE) {
325 assert(dst_pitch % 512 == 0);
327 cmd |= XY_SRC_COPY_BLT_DST_TILED;
329 if (src_ib->tiling != I915_TILING_NONE) {
330 assert(src_pitch % 512 == 0);
332 cmd |= XY_SRC_COPY_BLT_SRC_TILED;
336 if (batch_reserve(info, 8))
339 batch_dword(info, cmd);
340 batch_dword(info, br13 | (uint16_t)dst_pitch);
341 batch_dword(info, (dst_y1 << 16) | dst_x1);
342 batch_dword(info, (dst_y2 << 16) | dst_x2);
343 batch_reloc(info, dst, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER);
344 batch_dword(info, (src_y1 << 16) | src_x1);
345 batch_dword(info, (uint16_t)src_pitch);
346 batch_reloc(info, src, I915_GEM_DOMAIN_RENDER, 0);
348 if (info->gen >= 60) {
349 batch_reserve(info, 4);
350 batch_dword(info, MI_FLUSH_DW | 2);
351 batch_dword(info, 0);
352 batch_dword(info, 0);
353 batch_dword(info, 0);
356 int flags = (info->gen >= 40) ? 0 :
357 MI_WRITE_DIRTY_STATE | MI_INVALIDATE_MAP_CACHE;
359 batch_reserve(info, 1);
360 batch_dword(info, MI_FLUSH | flags);
366 static drm_intel_bo *alloc_ibo(struct intel_info *info,
367 const struct gralloc_drm_handle_t *handle,
368 uint32_t *tiling, unsigned long *stride)
372 int aligned_width, aligned_height, bpp;
376 bpp = gralloc_drm_get_bpp(handle->format);
378 ALOGE("unrecognized format 0x%x", handle->format);
382 aligned_width = handle->width;
383 aligned_height = handle->height;
384 gralloc_drm_align_geometry(handle->format,
385 &aligned_width, &aligned_height);
387 if (handle->usage & GRALLOC_USAGE_HW_FB) {
388 unsigned long max_stride;
390 max_stride = 32 * 1024;
397 aligned_width = ALIGN(aligned_width, 64);
398 flags = BO_ALLOC_FOR_RENDER;
400 *tiling = I915_TILING_X;
401 *stride = aligned_width * bpp;
402 if (*stride > max_stride) {
403 *tiling = I915_TILING_NONE;
404 max_stride = 32 * 1024;
405 if (*stride > max_stride)
410 ibo = drm_intel_bo_alloc_tiled(info->bufmgr, name,
411 aligned_width, aligned_height,
412 bpp, tiling, stride, flags);
413 if (!ibo || *stride > max_stride) {
415 drm_intel_bo_unreference(ibo);
419 if (*tiling != I915_TILING_NONE) {
421 *tiling = I915_TILING_NONE;
422 max_stride = 32 * 1024;
427 drm_intel_bo_disable_reuse(ibo);
432 if (handle->usage & (GRALLOC_USAGE_SW_READ_OFTEN |
433 GRALLOC_USAGE_SW_WRITE_OFTEN))
434 *tiling = I915_TILING_NONE;
435 else if ((handle->usage & GRALLOC_USAGE_HW_RENDER) ||
436 ((handle->usage & GRALLOC_USAGE_HW_TEXTURE) &&
437 handle->width >= 64))
438 *tiling = I915_TILING_X;
440 *tiling = I915_TILING_NONE;
442 if (handle->usage & GRALLOC_USAGE_HW_TEXTURE) {
443 name = "gralloc-texture";
444 /* see 2D texture layout of DRI drivers */
445 aligned_width = ALIGN(aligned_width, 4);
446 aligned_height = ALIGN(aligned_height, 2);
449 name = "gralloc-buffer";
452 if (handle->usage & GRALLOC_USAGE_HW_RENDER)
453 flags = BO_ALLOC_FOR_RENDER;
455 ibo = drm_intel_bo_alloc_tiled(info->bufmgr, name,
456 aligned_width, aligned_height,
457 bpp, tiling, stride, flags);
463 static struct gralloc_drm_bo_t *intel_alloc(struct gralloc_drm_drv_t *drv,
464 struct gralloc_drm_handle_t *handle)
466 struct intel_info *info = (struct intel_info *) drv;
467 struct intel_buffer *ib;
469 ib = calloc(1, sizeof(*ib));
476 ib->ibo = drm_intel_bo_gem_create_from_name(info->bufmgr,
477 "gralloc-r", handle->name);
479 ALOGE("failed to create ibo from name %u",
485 if (drm_intel_bo_get_tiling(ib->ibo, &ib->tiling, &dummy)) {
486 ALOGE("failed to get ibo tiling");
487 drm_intel_bo_unreference(ib->ibo);
493 unsigned long stride;
495 ib->ibo = alloc_ibo(info, handle, &ib->tiling, &stride);
497 ALOGE("failed to allocate ibo %dx%d (format %d)",
505 handle->stride = stride;
507 if (drm_intel_bo_flink(ib->ibo, (uint32_t *) &handle->name)) {
508 ALOGE("failed to flink ibo");
509 drm_intel_bo_unreference(ib->ibo);
515 ib->base.fb_handle = ib->ibo->handle;
517 ib->base.handle = handle;
522 static void intel_free(struct gralloc_drm_drv_t *drv,
523 struct gralloc_drm_bo_t *bo)
525 struct intel_buffer *ib = (struct intel_buffer *) bo;
527 drm_intel_bo_unreference(ib->ibo);
531 static int intel_map(struct gralloc_drm_drv_t *drv,
532 struct gralloc_drm_bo_t *bo,
533 int x, int y, int w, int h,
534 int enable_write, void **addr)
536 struct intel_buffer *ib = (struct intel_buffer *) bo;
539 if (ib->tiling != I915_TILING_NONE ||
540 (ib->base.handle->usage & GRALLOC_USAGE_HW_FB))
541 err = drm_intel_gem_bo_map_gtt(ib->ibo);
543 err = drm_intel_bo_map(ib->ibo, enable_write);
545 *addr = ib->ibo->virtual;
550 static void intel_unmap(struct gralloc_drm_drv_t *drv,
551 struct gralloc_drm_bo_t *bo)
553 struct intel_buffer *ib = (struct intel_buffer *) bo;
555 if (ib->tiling != I915_TILING_NONE ||
556 (ib->base.handle->usage & GRALLOC_USAGE_HW_FB))
557 drm_intel_gem_bo_unmap_gtt(ib->ibo);
559 drm_intel_bo_unmap(ib->ibo);
562 #include "intel_chipset.h" /* for platform detection macros */
563 static void intel_init_kms_features(struct gralloc_drm_drv_t *drv,
564 struct gralloc_drm_t *drm)
566 struct intel_info *info = (struct intel_info *) drv;
567 struct drm_i915_getparam gp;
568 int pageflipping, id, has_blt;
570 switch (drm->primary.fb_format) {
571 case HAL_PIXEL_FORMAT_BGRA_8888:
572 case HAL_PIXEL_FORMAT_RGB_565:
575 drm->primary.fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
579 drm->mode_quirk_vmwgfx = 0;
581 drm->mode_sync_flip = 1;
583 memset(&gp, 0, sizeof(gp));
584 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
585 gp.value = &pageflipping;
586 if (drmCommandWriteRead(drm->fd, DRM_I915_GETPARAM, &gp, sizeof(gp)))
589 memset(&gp, 0, sizeof(gp));
590 gp.param = I915_PARAM_CHIPSET_ID;
592 if (drmCommandWriteRead(drm->fd, DRM_I915_GETPARAM, &gp, sizeof(gp)))
595 memset(&gp, 0, sizeof(gp));
596 gp.param = I915_PARAM_HAS_BLT;
598 if (drmCommandWriteRead(drm->fd, DRM_I915_GETPARAM, &gp, sizeof(gp)))
600 info->exec_blt = has_blt ? I915_EXEC_BLT : 0;
602 /* GEN4, G4X, GEN5, GEN6, GEN7 */
603 if ((IS_9XX(id) || IS_G4X(id)) && !IS_GEN3(id)) {
606 else if (IS_GEN6(id))
608 else if (IS_GEN5(id))
617 if (pageflipping && info->gen > 30)
618 drm->swap_mode = DRM_SWAP_FLIP;
619 else if (info->batch && info->gen == 30)
620 drm->swap_mode = DRM_SWAP_COPY;
622 drm->swap_mode = DRM_SWAP_SETCRTC;
624 if (drm->resources) {
627 pipe = drm_intel_get_pipe_from_crtc_id(info->bufmgr,
628 drm->primary.crtc_id);
629 drm->swap_interval = (pipe >= 0) ? 1 : 0;
630 drm->vblank_secondary = (pipe > 0);
633 drm->swap_interval = 0;
637 static void intel_destroy(struct gralloc_drm_drv_t *drv)
639 struct intel_info *info = (struct intel_info *) drv;
642 drm_intel_bufmgr_destroy(info->bufmgr);
646 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_intel(int fd)
648 struct intel_info *info;
650 info = calloc(1, sizeof(*info));
652 ALOGE("failed to allocate driver info");
657 info->bufmgr = drm_intel_bufmgr_gem_init(info->fd, 16 * 1024);
659 ALOGE("failed to create buffer manager");
666 info->base.destroy = intel_destroy;
667 info->base.init_kms_features = intel_init_kms_features;
668 info->base.alloc = intel_alloc;
669 info->base.free = intel_free;
670 info->base.map = intel_map;
671 info->base.unmap = intel_unmap;
672 info->base.blit = intel_blit;
673 info->base.resolve_format = intel_resolve_format;