OSDN Git Service

minigbm: fix typo in PRESUBMIT.cfg
[android-x86/external-minigbm.git] / tegra.c
1 /*
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.
5  */
6
7 #ifdef DRV_TEGRA
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/mman.h>
12 #include <tegra_drm.h>
13 #include <xf86drm.h>
14
15 #include "drv_priv.h"
16 #include "helpers.h"
17 #include "util.h"
18
19 /*
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.
23  */
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)
28
29 // clang-format off
30 enum nv_mem_kind
31 {
32         NV_MEM_KIND_PITCH = 0,
33         NV_MEM_KIND_C32_2CRA = 0xdb,
34         NV_MEM_KIND_GENERIC_16Bx2 = 0xfe,
35 };
36
37 enum tegra_map_type {
38         TEGRA_READ_TILED_BUFFER = 0,
39         TEGRA_WRITE_TILED_BUFFER = 1,
40 };
41 // clang-format on
42
43 struct tegra_private_map_data {
44         void *tiled;
45         void *untiled;
46 };
47
48 static const uint32_t supported_formats[] = { DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB8888 };
49
50 static int compute_block_height_log2(int height)
51 {
52         int block_height_log2 = NV_DEFAULT_BLOCK_HEIGHT_LOG2;
53
54         if (block_height_log2 > 0) {
55                 /* Shrink, if a smaller block height could cover the whole
56                  * surface height. */
57                 int proposed = NV_BLOCKLINEAR_GOB_HEIGHT << (block_height_log2 - 1);
58                 while (proposed >= height) {
59                         block_height_log2--;
60                         if (block_height_log2 == 0)
61                                 break;
62                         proposed /= 2;
63                 }
64         }
65         return block_height_log2;
66 }
67
68 static void compute_layout_blocklinear(int width, int height, int format, enum nv_mem_kind *kind,
69                                        uint32_t *block_height_log2, uint32_t *stride,
70                                        uint32_t *size)
71 {
72         int pitch = drv_stride_from_format(format, width, 0);
73
74         /* Align to blocklinear blocks. */
75         pitch = ALIGN(pitch, NV_BLOCKLINEAR_GOB_WIDTH);
76
77         /* Compute padded height. */
78         *block_height_log2 = compute_block_height_log2(height);
79         int block_height = 1 << *block_height_log2;
80         int padded_height = ALIGN(height, NV_BLOCKLINEAR_GOB_HEIGHT * block_height);
81
82         int bytes = pitch * padded_height;
83
84         /* Pad the allocation to the preferred page size.
85          * This will reduce the required page table size (see discussion in NV
86          * bug 1321091), and also acts as a WAR for NV bug 1325421.
87          */
88         bytes = ALIGN(bytes, NV_PREFERRED_PAGE_SIZE);
89
90         *kind = NV_MEM_KIND_C32_2CRA;
91         *stride = pitch;
92         *size = bytes;
93 }
94
95 static void compute_layout_linear(int width, int height, int format, uint32_t *stride,
96                                   uint32_t *size)
97 {
98         *stride = ALIGN(drv_stride_from_format(format, width, 0), 64);
99         *size = *stride * height;
100 }
101
102 static void transfer_tile(struct bo *bo, uint8_t *tiled, uint8_t *untiled, enum tegra_map_type type,
103                           uint32_t bytes_per_pixel, uint32_t gob_top, uint32_t gob_left,
104                           uint32_t gob_size_pixels)
105 {
106         uint8_t *tmp;
107         uint32_t x, y, k;
108         for (k = 0; k < gob_size_pixels; k++) {
109                 /*
110                  * Given the kth pixel starting from the tile specified by
111                  * gob_top and gob_left, unswizzle to get the standard (x, y)
112                  * representation.
113                  */
114                 x = gob_left + (((k >> 3) & 8) | ((k >> 1) & 4) | (k & 3));
115                 y = gob_top + ((k >> 7 << 3) | ((k >> 3) & 6) | ((k >> 2) & 1));
116
117                 tmp = untiled + (y * bo->strides[0]) + (x * bytes_per_pixel);
118
119                 if (type == TEGRA_READ_TILED_BUFFER)
120                         memcpy(tmp, tiled, bytes_per_pixel);
121                 else if (type == TEGRA_WRITE_TILED_BUFFER)
122                         memcpy(tiled, tmp, bytes_per_pixel);
123
124                 /* Move on to next pixel. */
125                 tiled += bytes_per_pixel;
126         }
127 }
128
129 static void transfer_tiled_memory(struct bo *bo, uint8_t *tiled, uint8_t *untiled,
130                                   enum tegra_map_type type)
131 {
132         uint32_t gob_width, gob_height, gob_size_bytes, gob_size_pixels, gob_count_x, gob_count_y,
133             gob_top, gob_left;
134         uint32_t i, j, offset;
135         uint8_t *tmp;
136         uint32_t bytes_per_pixel = drv_stride_from_format(bo->format, 1, 0);
137
138         /*
139          * The blocklinear format consists of 8*(2^n) x 64 byte sized tiles,
140          * where 0 <= n <= 4.
141          */
142         gob_width = DIV_ROUND_UP(NV_BLOCKLINEAR_GOB_WIDTH, bytes_per_pixel);
143         gob_height = NV_BLOCKLINEAR_GOB_HEIGHT * (1 << NV_DEFAULT_BLOCK_HEIGHT_LOG2);
144         /* Calculate the height from maximum possible gob height */
145         while (gob_height > NV_BLOCKLINEAR_GOB_HEIGHT && gob_height >= 2 * bo->height)
146                 gob_height /= 2;
147
148         gob_size_bytes = gob_height * NV_BLOCKLINEAR_GOB_WIDTH;
149         gob_size_pixels = gob_height * gob_width;
150
151         gob_count_x = DIV_ROUND_UP(bo->strides[0], NV_BLOCKLINEAR_GOB_WIDTH);
152         gob_count_y = DIV_ROUND_UP(bo->height, gob_height);
153
154         offset = 0;
155         for (j = 0; j < gob_count_y; j++) {
156                 gob_top = j * gob_height;
157                 for (i = 0; i < gob_count_x; i++) {
158                         tmp = tiled + offset;
159                         gob_left = i * gob_width;
160
161                         transfer_tile(bo, tmp, untiled, type, bytes_per_pixel, gob_top, gob_left,
162                                       gob_size_pixels);
163
164                         offset += gob_size_bytes;
165                 }
166         }
167 }
168
169 static int tegra_init(struct driver *drv)
170 {
171         int ret;
172         struct format_metadata metadata;
173         uint64_t flags = BO_COMMON_USE_MASK;
174
175         metadata.tiling = NV_MEM_KIND_PITCH;
176         metadata.priority = 1;
177         metadata.modifier = DRM_FORMAT_MOD_NONE;
178
179         ret = drv_add_combinations(drv, supported_formats, ARRAY_SIZE(supported_formats), &metadata,
180                                    flags);
181         if (ret)
182                 return ret;
183
184         drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
185         drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
186
187         flags &= ~BO_USE_SW_WRITE_OFTEN;
188         flags &= ~BO_USE_SW_READ_OFTEN;
189         flags &= ~BO_USE_LINEAR;
190
191         metadata.tiling = NV_MEM_KIND_C32_2CRA;
192         metadata.priority = 2;
193
194         ret = drv_add_combinations(drv, supported_formats, ARRAY_SIZE(supported_formats), &metadata,
195                                    flags);
196         if (ret)
197                 return ret;
198
199         drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_SCANOUT);
200         drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_SCANOUT);
201         return 0;
202 }
203
204 static int tegra_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
205                            uint32_t flags)
206 {
207         uint32_t size, stride, block_height_log2 = 0;
208         enum nv_mem_kind kind = NV_MEM_KIND_PITCH;
209         struct drm_tegra_gem_create gem_create;
210         int ret;
211
212         if (flags & (BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN))
213                 compute_layout_linear(width, height, format, &stride, &size);
214         else
215                 compute_layout_blocklinear(width, height, format, &kind, &block_height_log2,
216                                            &stride, &size);
217
218         memset(&gem_create, 0, sizeof(gem_create));
219         gem_create.size = size;
220         gem_create.flags = 0;
221
222         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_TEGRA_GEM_CREATE, &gem_create);
223         if (ret) {
224                 fprintf(stderr, "drv: DRM_IOCTL_TEGRA_GEM_CREATE failed (size=%zu)\n", size);
225                 return ret;
226         }
227
228         bo->handles[0].u32 = gem_create.handle;
229         bo->offsets[0] = 0;
230         bo->total_size = bo->sizes[0] = size;
231         bo->strides[0] = stride;
232
233         if (kind != NV_MEM_KIND_PITCH) {
234                 struct drm_tegra_gem_set_tiling gem_tile;
235
236                 memset(&gem_tile, 0, sizeof(gem_tile));
237                 gem_tile.handle = bo->handles[0].u32;
238                 gem_tile.mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
239                 gem_tile.value = block_height_log2;
240
241                 ret = drmCommandWriteRead(bo->drv->fd, DRM_TEGRA_GEM_SET_TILING, &gem_tile,
242                                           sizeof(gem_tile));
243                 if (ret < 0) {
244                         drv_gem_bo_destroy(bo);
245                         return ret;
246                 }
247
248                 /* Encode blocklinear parameters for EGLImage creation. */
249                 bo->tiling = (kind & 0xff) | ((block_height_log2 & 0xf) << 8);
250                 bo->format_modifiers[0] = fourcc_mod_code(NV, bo->tiling);
251         }
252
253         return 0;
254 }
255
256 static void *tegra_bo_map(struct bo *bo, struct map_info *data, size_t plane)
257 {
258         int ret;
259         struct drm_tegra_gem_mmap gem_map;
260         struct tegra_private_map_data *priv;
261
262         memset(&gem_map, 0, sizeof(gem_map));
263         gem_map.handle = bo->handles[0].u32;
264
265         ret = drmCommandWriteRead(bo->drv->fd, DRM_TEGRA_GEM_MMAP, &gem_map, sizeof(gem_map));
266         if (ret < 0) {
267                 fprintf(stderr, "drv: DRM_TEGRA_GEM_MMAP failed\n");
268                 return MAP_FAILED;
269         }
270
271         void *addr = mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->drv->fd,
272                           gem_map.offset);
273
274         data->length = bo->total_size;
275
276         if ((bo->tiling & 0xFF) == NV_MEM_KIND_C32_2CRA && addr != MAP_FAILED) {
277                 priv = calloc(1, sizeof(*priv));
278                 priv->untiled = calloc(1, bo->total_size);
279                 priv->tiled = addr;
280                 data->priv = priv;
281                 transfer_tiled_memory(bo, priv->tiled, priv->untiled, TEGRA_READ_TILED_BUFFER);
282                 addr = priv->untiled;
283         }
284
285         return addr;
286 }
287
288 static int tegra_bo_unmap(struct bo *bo, struct map_info *data)
289 {
290         if (data->priv) {
291                 struct tegra_private_map_data *priv = data->priv;
292                 transfer_tiled_memory(bo, priv->tiled, priv->untiled, TEGRA_WRITE_TILED_BUFFER);
293                 data->addr = priv->tiled;
294                 free(priv->untiled);
295                 free(priv);
296                 data->priv = NULL;
297         }
298
299         return munmap(data->addr, data->length);
300 }
301
302 struct backend backend_tegra = {
303         .name = "tegra",
304         .init = tegra_init,
305         .bo_create = tegra_bo_create,
306         .bo_destroy = drv_gem_bo_destroy,
307         .bo_import = drv_prime_bo_import,
308         .bo_map = tegra_bo_map,
309         .bo_unmap = tegra_bo_unmap,
310 };
311
312 #endif