OSDN Git Service

minigbm: remove GBM_BO_USE_WRITE from the drivers.
[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_GENERIC_16Bx2 = 0xfe,
32 };
33
34 static int compute_block_height_log2(int height)
35 {
36         int block_height_log2 = NV_DEFAULT_BLOCK_HEIGHT_LOG2;
37
38         if (block_height_log2 > 0) {
39                 /* Shrink, if a smaller block height could cover the whole
40                  * surface height. */
41                 int proposed = NV_BLOCKLINEAR_GOB_HEIGHT << (block_height_log2 - 1);
42                 while (proposed >= height) {
43                         block_height_log2--;
44                         if (block_height_log2 == 0)
45                                 break;
46                         proposed /= 2;
47                 }
48         }
49         return block_height_log2;
50 }
51
52 static void compute_layout_blocklinear(int width, int height, int format,
53                                        enum nv_mem_kind *kind,
54                                        uint32_t *block_height_log2,
55                                        uint32_t *stride, uint32_t *size)
56 {
57         int pitch = gbm_stride_from_format(format, width);
58
59         /* Align to blocklinear blocks. */
60         pitch = ALIGN(pitch, NV_BLOCKLINEAR_GOB_WIDTH);
61
62         /* Compute padded height. */
63         *block_height_log2 = compute_block_height_log2(height);
64         int block_height = 1 << *block_height_log2;
65         int padded_height =
66                 ALIGN(height, NV_BLOCKLINEAR_GOB_HEIGHT * block_height);
67
68         int bytes = pitch * padded_height;
69
70         /* Pad the allocation to the preferred page size.
71          * This will reduce the required page table size (see discussion in NV
72          * bug 1321091), and also acts as a WAR for NV bug 1325421.
73          */
74         bytes = ALIGN(bytes, NV_PREFERRED_PAGE_SIZE);
75
76         *kind = NV_MEM_KIND_GENERIC_16Bx2;
77         *stride = pitch;
78         *size = bytes;
79 }
80
81 static void compute_layout_linear(int width, int height, int format,
82                                   uint32_t *stride, uint32_t *size)
83 {
84         *stride = gbm_stride_from_format(format, width);
85         *size = *stride * height;
86 }
87
88 static int gbm_tegra_bo_create(struct gbm_bo *bo, uint32_t width,
89                                uint32_t height, uint32_t format, uint32_t flags)
90 {
91         uint32_t size, stride, block_height_log2 = 0;
92         enum nv_mem_kind kind = NV_MEM_KIND_PITCH;
93         struct drm_tegra_gem_create gem_create;
94         int ret;
95
96         if (flags & GBM_BO_USE_RENDERING)
97                 compute_layout_blocklinear(width, height, format, &kind,
98                                            &block_height_log2, &stride, &size);
99         else
100                 compute_layout_linear(width, height, format, &stride, &size);
101
102         memset(&gem_create, 0, sizeof(gem_create));
103         gem_create.size = size;
104         gem_create.flags = 0;
105
106         ret = drmIoctl(bo->gbm->fd, DRM_IOCTL_TEGRA_GEM_CREATE, &gem_create);
107         if (ret) {
108                 fprintf(stderr, "minigbm: DRM_IOCTL_TEGRA_GEM_CREATE failed "
109                                 "(size=%zu)\n", size);
110                 return ret;
111         }
112
113         bo->handles[0].u32 = gem_create.handle;
114         bo->offsets[0] = 0;
115         bo->sizes[0] = size;
116         bo->strides[0] = stride;
117
118         if (kind != NV_MEM_KIND_PITCH) {
119                 struct drm_tegra_gem_set_tiling gem_tile;
120
121                 memset(&gem_tile, 0, sizeof(gem_tile));
122                 gem_tile.handle = bo->handles[0].u32;
123                 gem_tile.mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
124                 gem_tile.value = block_height_log2;
125
126                 ret = drmCommandWriteRead(bo->gbm->fd, DRM_TEGRA_GEM_SET_TILING,
127                                           &gem_tile, sizeof(gem_tile));
128                 if (ret < 0) {
129                         gbm_gem_bo_destroy(bo);
130                         return ret;
131                 }
132
133                 /* Encode blocklinear parameters for EGLImage creation. */
134
135                 /* XXX Bringup hack: If the highest order bit is set in
136                  * EGL_DMA_BUF_PLANE0_PITCH_EXT, Nvidia driver treats it as
137                  * a hint that the buffer is tiled, and the remaining bits in
138                  * the pitch attribute are treated as vendor specific tiling
139                  * arguments.  Using this hack means that we don't need to add
140                  * a new FOURCC format, or EGL_DMA_BUF_PLANE0_TILING_EXT
141                  * attribute to the dma-buf import extension.
142                  */
143                 bo->tiling = (1 << 31) |
144                              (kind & 0xff) |
145                              ((block_height_log2 & 0xf) << 8);
146         }
147
148         return 0;
149 }
150
151 const struct gbm_driver gbm_driver_tegra =
152 {
153         .name = "tegra",
154         .bo_create = gbm_tegra_bo_create,
155         .bo_destroy = gbm_gem_bo_destroy,
156         .format_list = {
157                 /* Linear support */
158                 {GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_CURSOR | GBM_BO_USE_LINEAR},
159                 {GBM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_CURSOR | GBM_BO_USE_LINEAR},
160                 /* Blocklinear support */
161                 {GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING},
162                 {GBM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING},
163         }
164 };
165
166 #endif