OSDN Git Service

minigm: add kind C32_2CRA and set it as default
[android-x86/external-minigbm.git] / tegra.c
1 /*
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.
5  */
6
7 #ifdef GBM_TEGRA
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <xf86drm.h>
12 #include <tegra_drm.h>
13
14 #include "gbm_priv.h"
15 #include "helpers.h"
16 #include "util.h"
17
18 /*
19  * GOB (Group Of Bytes) is the basic unit of the blocklinear layout.
20  * GOBs are arranged to blocks, where the height of the block (measured
21  * in GOBs) is configurable.
22  */
23 #define NV_BLOCKLINEAR_GOB_HEIGHT 8
24 #define NV_BLOCKLINEAR_GOB_WIDTH 64
25 #define NV_DEFAULT_BLOCK_HEIGHT_LOG2 4
26 #define NV_PREFERRED_PAGE_SIZE (128 * 1024)
27
28 enum nv_mem_kind
29 {
30         NV_MEM_KIND_PITCH = 0,
31         NV_MEM_KIND_C32_2CRA = 0xdb,
32         NV_MEM_KIND_GENERIC_16Bx2 = 0xfe,
33 };
34
35 static int compute_block_height_log2(int height)
36 {
37         int block_height_log2 = NV_DEFAULT_BLOCK_HEIGHT_LOG2;
38
39         if (block_height_log2 > 0) {
40                 /* Shrink, if a smaller block height could cover the whole
41                  * surface height. */
42                 int proposed = NV_BLOCKLINEAR_GOB_HEIGHT << (block_height_log2 - 1);
43                 while (proposed >= height) {
44                         block_height_log2--;
45                         if (block_height_log2 == 0)
46                                 break;
47                         proposed /= 2;
48                 }
49         }
50         return block_height_log2;
51 }
52
53 static void compute_layout_blocklinear(int width, int height, int format,
54                                        enum nv_mem_kind *kind,
55                                        uint32_t *block_height_log2,
56                                        uint32_t *stride, uint32_t *size)
57 {
58         int pitch = gbm_stride_from_format(format, width);
59
60         /* Align to blocklinear blocks. */
61         pitch = ALIGN(pitch, NV_BLOCKLINEAR_GOB_WIDTH);
62
63         /* Compute padded height. */
64         *block_height_log2 = compute_block_height_log2(height);
65         int block_height = 1 << *block_height_log2;
66         int padded_height =
67                 ALIGN(height, NV_BLOCKLINEAR_GOB_HEIGHT * block_height);
68
69         int bytes = pitch * padded_height;
70
71         /* Pad the allocation to the preferred page size.
72          * This will reduce the required page table size (see discussion in NV
73          * bug 1321091), and also acts as a WAR for NV bug 1325421.
74          */
75         bytes = ALIGN(bytes, NV_PREFERRED_PAGE_SIZE);
76
77         *kind = NV_MEM_KIND_C32_2CRA;
78         *stride = pitch;
79         *size = bytes;
80 }
81
82 static void compute_layout_linear(int width, int height, int format,
83                                   uint32_t *stride, uint32_t *size)
84 {
85         *stride = gbm_stride_from_format(format, width);
86         *size = *stride * height;
87 }
88
89 static int gbm_tegra_bo_create(struct gbm_bo *bo, uint32_t width,
90                                uint32_t height, uint32_t format, uint32_t flags)
91 {
92         uint32_t size, stride, block_height_log2 = 0;
93         enum nv_mem_kind kind = NV_MEM_KIND_PITCH;
94         struct drm_tegra_gem_create gem_create;
95         int ret;
96
97         if (flags & GBM_BO_USE_RENDERING)
98                 compute_layout_blocklinear(width, height, format, &kind,
99                                            &block_height_log2, &stride, &size);
100         else
101                 compute_layout_linear(width, height, format, &stride, &size);
102
103         memset(&gem_create, 0, sizeof(gem_create));
104         gem_create.size = size;
105         gem_create.flags = 0;
106
107         ret = drmIoctl(bo->gbm->fd, DRM_IOCTL_TEGRA_GEM_CREATE, &gem_create);
108         if (ret) {
109                 fprintf(stderr, "minigbm: DRM_IOCTL_TEGRA_GEM_CREATE failed "
110                                 "(size=%zu)\n", size);
111                 return ret;
112         }
113
114         bo->handles[0].u32 = gem_create.handle;
115         bo->offsets[0] = 0;
116         bo->sizes[0] = size;
117         bo->strides[0] = stride;
118
119         if (kind != NV_MEM_KIND_PITCH) {
120                 struct drm_tegra_gem_set_tiling gem_tile;
121
122                 memset(&gem_tile, 0, sizeof(gem_tile));
123                 gem_tile.handle = bo->handles[0].u32;
124                 gem_tile.mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
125                 gem_tile.value = block_height_log2;
126
127                 ret = drmCommandWriteRead(bo->gbm->fd, DRM_TEGRA_GEM_SET_TILING,
128                                           &gem_tile, sizeof(gem_tile));
129                 if (ret < 0) {
130                         gbm_gem_bo_destroy(bo);
131                         return ret;
132                 }
133
134                 /* Encode blocklinear parameters for EGLImage creation. */
135                 bo->tiling = (kind & 0xff) |
136                              ((block_height_log2 & 0xf) << 8);
137                 bo->format_modifiers[0] = gbm_fourcc_mod_code(NV, bo->tiling);
138         }
139
140         return 0;
141 }
142
143 const struct gbm_driver gbm_driver_tegra =
144 {
145         .name = "tegra",
146         .bo_create = gbm_tegra_bo_create,
147         .bo_destroy = gbm_gem_bo_destroy,
148         .format_list = {
149                 /* Linear support */
150                 {GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_CURSOR | GBM_BO_USE_LINEAR},
151                 {GBM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_CURSOR | GBM_BO_USE_LINEAR},
152                 /* Blocklinear support */
153                 {GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING},
154                 {GBM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING},
155         }
156 };
157
158 #endif