2 * Copyright (c) 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.
12 #include <tegra_drm.h>
18 * GOB (Group Of Bytes) is the basic unit of the blocklinear layout.
19 * GOBs are arranged to blocks, where the height of the block (measured
20 * in GOBs) is configurable.
22 #define NV_BLOCKLINEAR_GOB_HEIGHT 8
23 #define NV_BLOCKLINEAR_GOB_WIDTH 64
24 #define NV_DEFAULT_BLOCK_HEIGHT_LOG2 4
25 #define NV_PREFERRED_PAGE_SIZE (128 * 1024)
29 NV_MEM_KIND_PITCH = 0,
30 NV_MEM_KIND_GENERIC_16Bx2 = 0xfe,
33 static int compute_block_height_log2(int height)
35 int block_height_log2 = NV_DEFAULT_BLOCK_HEIGHT_LOG2;
37 if (block_height_log2 > 0) {
38 /* Shrink, if a smaller block height could cover the whole
40 int proposed = NV_BLOCKLINEAR_GOB_HEIGHT << (block_height_log2 - 1);
41 while (proposed >= height) {
43 if (block_height_log2 == 0)
48 return block_height_log2;
51 static inline uint32_t align_up(uint32_t value, uint32_t alignment)
53 return (value + (alignment-1)) & ~(alignment-1);
56 static void compute_layout_blocklinear(int width, int height, int format,
57 enum nv_mem_kind *kind,
58 uint32_t *block_height_log2,
59 uint32_t *stride, uint32_t *size)
61 int pitch = width * gbm_bytes_from_format(format);
63 /* Align to blocklinear blocks. */
64 pitch = align_up(pitch, NV_BLOCKLINEAR_GOB_WIDTH);
66 /* Compute padded height. */
67 *block_height_log2 = compute_block_height_log2(height);
68 int block_height = 1 << *block_height_log2;
70 align_up(height, NV_BLOCKLINEAR_GOB_HEIGHT * block_height);
72 int bytes = pitch * padded_height;
74 /* Pad the allocation to the preferred page size.
75 * This will reduce the required page table size (see discussion in NV
76 * bug 1321091), and also acts as a WAR for NV bug 1325421.
78 bytes = align_up(bytes, NV_PREFERRED_PAGE_SIZE);
80 *kind = NV_MEM_KIND_GENERIC_16Bx2;
85 static void compute_layout_linear(int width, int height, int format,
86 uint32_t *stride, uint32_t *size)
88 *stride = width * gbm_bytes_from_format(format);
89 *size = *stride * height;
92 static int gbm_tegra_bo_create(struct gbm_bo *bo, uint32_t width,
93 uint32_t height, uint32_t format, uint32_t flags)
95 uint32_t size, stride, block_height_log2 = 0;
96 enum nv_mem_kind kind = NV_MEM_KIND_PITCH;
97 struct drm_tegra_gem_create gem_create;
100 if (flags & GBM_BO_USE_RENDERING)
101 compute_layout_blocklinear(width, height, format, &kind,
102 &block_height_log2, &stride, &size);
104 compute_layout_linear(width, height, format, &stride, &size);
106 memset(&gem_create, 0, sizeof(gem_create));
107 gem_create.size = size;
108 gem_create.flags = 0;
110 ret = drmIoctl(bo->gbm->fd, DRM_IOCTL_TEGRA_GEM_CREATE, &gem_create);
112 fprintf(stderr, "minigbm: DRM_IOCTL_TEGRA_GEM_CREATE failed "
113 "(size=%zu)\n", size);
117 bo->handle.u32 = gem_create.handle;
121 if (kind != NV_MEM_KIND_PITCH) {
122 struct drm_tegra_gem_set_tiling gem_tile;
124 memset(&gem_tile, 0, sizeof(gem_tile));
125 gem_tile.handle = bo->handle.u32;
126 gem_tile.mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
127 gem_tile.value = block_height_log2;
129 ret = drmCommandWriteRead(bo->gbm->fd, DRM_TEGRA_GEM_SET_TILING,
130 &gem_tile, sizeof(gem_tile));
132 gbm_gem_bo_destroy(bo);
136 /* Encode blocklinear parameters for EGLImage creation. */
138 /* XXX Bringup hack: If the highest order bit is set in
139 * EGL_DMA_BUF_PLANE0_PITCH_EXT, Nvidia driver treats it as
140 * a hint that the buffer is tiled, and the remaining bits in
141 * the pitch attribute are treated as vendor specific tiling
142 * arguments. Using this hack means that we don't need to add
143 * a new FOURCC format, or EGL_DMA_BUF_PLANE0_TILING_EXT
144 * attribute to the dma-buf import extension.
146 bo->tiling = (1 << 31) |
148 ((block_height_log2 & 0xf) << 8);
154 struct gbm_driver gbm_driver_tegra =
157 .bo_create = gbm_tegra_bo_create,
158 .bo_destroy = gbm_gem_bo_destroy,
161 {GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE},
162 {GBM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE},
163 /* Blocklinear support */
164 {GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING},
165 {GBM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING},