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.
13 #include <tegra_drm.h>
20 * GOB (Group Of Bytes) is the basic unit of the blocklinear layout.
21 * GOBs are arranged to blocks, where the height of the block (measured
22 * in GOBs) is configurable.
24 #define NV_BLOCKLINEAR_GOB_HEIGHT 8
25 #define NV_BLOCKLINEAR_GOB_WIDTH 64
26 #define NV_DEFAULT_BLOCK_HEIGHT_LOG2 4
27 #define NV_PREFERRED_PAGE_SIZE (128 * 1024)
31 NV_MEM_KIND_PITCH = 0,
32 NV_MEM_KIND_C32_2CRA = 0xdb,
33 NV_MEM_KIND_GENERIC_16Bx2 = 0xfe,
37 TEGRA_READ_TILED_BUFFER = 0,
38 TEGRA_WRITE_TILED_BUFFER = 1,
41 struct tegra_private_map_data {
46 static const uint32_t supported_formats[] = {
47 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB8888
50 static int compute_block_height_log2(int height)
52 int block_height_log2 = NV_DEFAULT_BLOCK_HEIGHT_LOG2;
54 if (block_height_log2 > 0) {
55 /* Shrink, if a smaller block height could cover the whole
57 int proposed = NV_BLOCKLINEAR_GOB_HEIGHT << (block_height_log2 - 1);
58 while (proposed >= height) {
60 if (block_height_log2 == 0)
65 return block_height_log2;
68 static void compute_layout_blocklinear(int width, int height, int format,
69 enum nv_mem_kind *kind,
70 uint32_t *block_height_log2,
71 uint32_t *stride, uint32_t *size)
73 int pitch = drv_stride_from_format(format, width, 0);
75 /* Align to blocklinear blocks. */
76 pitch = ALIGN(pitch, NV_BLOCKLINEAR_GOB_WIDTH);
78 /* Compute padded height. */
79 *block_height_log2 = compute_block_height_log2(height);
80 int block_height = 1 << *block_height_log2;
82 ALIGN(height, NV_BLOCKLINEAR_GOB_HEIGHT * block_height);
84 int bytes = pitch * padded_height;
86 /* Pad the allocation to the preferred page size.
87 * This will reduce the required page table size (see discussion in NV
88 * bug 1321091), and also acts as a WAR for NV bug 1325421.
90 bytes = ALIGN(bytes, NV_PREFERRED_PAGE_SIZE);
92 *kind = NV_MEM_KIND_C32_2CRA;
97 static void compute_layout_linear(int width, int height, int format,
98 uint32_t *stride, uint32_t *size)
100 *stride = ALIGN(drv_stride_from_format(format, width, 0), 64);
101 *size = *stride * height;
104 static void transfer_tile(struct bo *bo, uint8_t *tiled, uint8_t *untiled,
105 enum tegra_map_type type, uint32_t bytes_per_pixel,
106 uint32_t gob_top, uint32_t gob_left,
107 uint32_t gob_size_pixels)
111 for (k = 0; k < gob_size_pixels; k++) {
113 * Given the kth pixel starting from the tile specified by
114 * gob_top and gob_left, unswizzle to get the standard (x, y)
117 x = gob_left + (((k >> 3) & 8) | ((k >> 1) & 4) | (k & 3));
118 y = gob_top + ((k >> 7 << 3) | ((k >> 3) & 6) | ((k >> 2) & 1));
120 tmp = untiled + (y * bo->strides[0]) + (x * bytes_per_pixel);
122 if (type == TEGRA_READ_TILED_BUFFER)
123 memcpy(tmp, tiled, bytes_per_pixel);
124 else if (type == TEGRA_WRITE_TILED_BUFFER)
125 memcpy(tiled, tmp, bytes_per_pixel);
127 /* Move on to next pixel. */
128 tiled += bytes_per_pixel;
132 static void transfer_tiled_memory(struct bo *bo, uint8_t *tiled,
133 uint8_t *untiled, enum tegra_map_type type)
135 uint32_t gob_width, gob_height, gob_size_bytes, gob_size_pixels,
136 gob_count_x, gob_count_y, gob_top, gob_left;
137 uint32_t i, j, offset;
139 uint32_t bytes_per_pixel = drv_stride_from_format(bo->format, 1, 0);
142 * The blocklinear format consists of 8*(2^n) x 64 byte sized tiles,
145 gob_width = DIV_ROUND_UP(NV_BLOCKLINEAR_GOB_WIDTH, bytes_per_pixel);
146 gob_height = NV_BLOCKLINEAR_GOB_HEIGHT *
147 (1 << NV_DEFAULT_BLOCK_HEIGHT_LOG2);
148 /* Calculate the height from maximum possible gob height */
149 while (gob_height > NV_BLOCKLINEAR_GOB_HEIGHT
150 && gob_height >= 2 * bo->height)
153 gob_size_bytes = gob_height * NV_BLOCKLINEAR_GOB_WIDTH;
154 gob_size_pixels = gob_height * gob_width;
156 gob_count_x = DIV_ROUND_UP(bo->strides[0], NV_BLOCKLINEAR_GOB_WIDTH);
157 gob_count_y = DIV_ROUND_UP(bo->height, gob_height);
160 for (j = 0; j < gob_count_y; j++) {
161 gob_top = j * gob_height;
162 for (i = 0; i < gob_count_x; i++) {
163 tmp = tiled + offset;
164 gob_left = i * gob_width;
166 transfer_tile(bo, tmp, untiled, type, bytes_per_pixel,
167 gob_top, gob_left, gob_size_pixels);
169 offset += gob_size_bytes;
174 static int tegra_init(struct driver *drv)
177 struct format_metadata metadata;
178 uint64_t flags = BO_COMMON_USE_MASK;
180 metadata.tiling = NV_MEM_KIND_PITCH;
181 metadata.priority = 1;
182 metadata.modifier = DRM_FORMAT_MOD_NONE;
184 ret = drv_add_combinations(drv, supported_formats,
185 ARRAY_SIZE(supported_formats), &metadata,
190 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
191 BO_USE_CURSOR | BO_USE_SCANOUT);
192 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
193 BO_USE_CURSOR | BO_USE_SCANOUT);
195 flags &= ~BO_USE_SW_WRITE_OFTEN;
196 flags &= ~BO_USE_SW_READ_OFTEN;
197 flags &= ~BO_USE_LINEAR;
199 metadata.tiling = NV_MEM_KIND_C32_2CRA;
200 metadata.priority = 2;
202 ret = drv_add_combinations(drv, supported_formats,
203 ARRAY_SIZE(supported_formats), &metadata,
208 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata,
210 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata,
215 static int tegra_bo_create(struct bo *bo, uint32_t width, uint32_t height,
216 uint32_t format, uint32_t flags)
218 uint32_t size, stride, block_height_log2 = 0;
219 enum nv_mem_kind kind = NV_MEM_KIND_PITCH;
220 struct drm_tegra_gem_create gem_create;
223 if (flags & (BO_USE_CURSOR | BO_USE_LINEAR |
224 BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN))
225 compute_layout_linear(width, height, format, &stride, &size);
227 compute_layout_blocklinear(width, height, format, &kind,
228 &block_height_log2, &stride, &size);
230 memset(&gem_create, 0, sizeof(gem_create));
231 gem_create.size = size;
232 gem_create.flags = 0;
234 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_TEGRA_GEM_CREATE, &gem_create);
236 fprintf(stderr, "drv: DRM_IOCTL_TEGRA_GEM_CREATE failed "
237 "(size=%zu)\n", size);
241 bo->handles[0].u32 = gem_create.handle;
243 bo->total_size = bo->sizes[0] = size;
244 bo->strides[0] = stride;
246 if (kind != NV_MEM_KIND_PITCH) {
247 struct drm_tegra_gem_set_tiling gem_tile;
249 memset(&gem_tile, 0, sizeof(gem_tile));
250 gem_tile.handle = bo->handles[0].u32;
251 gem_tile.mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
252 gem_tile.value = block_height_log2;
254 ret = drmCommandWriteRead(bo->drv->fd, DRM_TEGRA_GEM_SET_TILING,
255 &gem_tile, sizeof(gem_tile));
257 drv_gem_bo_destroy(bo);
261 /* Encode blocklinear parameters for EGLImage creation. */
262 bo->tiling = (kind & 0xff) |
263 ((block_height_log2 & 0xf) << 8);
264 bo->format_modifiers[0] = fourcc_mod_code(NV, bo->tiling);
270 static void *tegra_bo_map(struct bo *bo, struct map_info *data, size_t plane)
273 struct drm_tegra_gem_mmap gem_map;
274 struct tegra_private_map_data *priv;
276 memset(&gem_map, 0, sizeof(gem_map));
277 gem_map.handle = bo->handles[0].u32;
279 ret = drmCommandWriteRead(bo->drv->fd, DRM_TEGRA_GEM_MMAP, &gem_map,
282 fprintf(stderr, "drv: DRM_TEGRA_GEM_MMAP failed\n");
286 void *addr = mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
287 bo->drv->fd, gem_map.offset);
289 data->length = bo->total_size;
291 if ((bo->tiling & 0xFF) == NV_MEM_KIND_C32_2CRA && addr != MAP_FAILED) {
292 priv = calloc(1, sizeof(*priv));
293 priv->untiled = calloc(1, bo->total_size);
296 transfer_tiled_memory(bo, priv->tiled, priv->untiled,
297 TEGRA_READ_TILED_BUFFER);
298 addr = priv->untiled;
304 static int tegra_bo_unmap(struct bo *bo, struct map_info *data)
307 struct tegra_private_map_data *priv = data->priv;
308 transfer_tiled_memory(bo, priv->tiled, priv->untiled,
309 TEGRA_WRITE_TILED_BUFFER);
310 data->addr = priv->tiled;
316 return munmap(data->addr, data->length);
319 struct backend backend_tegra =
323 .bo_create = tegra_bo_create,
324 .bo_destroy = drv_gem_bo_destroy,
325 .bo_import = drv_prime_bo_import,
326 .bo_map = tegra_bo_map,
327 .bo_unmap = tegra_bo_unmap,