-
Notifications
You must be signed in to change notification settings - Fork 24
/
gralloc_drm_nouveau.c
362 lines (303 loc) · 7.98 KB
/
gralloc_drm_nouveau.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (C) 2011 Chia-I Wu <[email protected]>
* Copyright (C) 2011 LunarG Inc.
*
* Based on xf86-video-nouveau, which has
*
* Copyright © 2007 Red Hat, Inc.
* Copyright © 2008 Maarten Maathuis
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#define LOG_TAG "GRALLOC-NOUVEAU"
#include <cutils/log.h>
#include <stdlib.h>
#include <errno.h>
#include <drm.h>
#include <nouveau_drmif.h>
#include <nouveau_channel.h>
#include <nouveau_bo.h>
#include "gralloc_drm.h"
#include "gralloc_drm_priv.h"
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define NVC0_TILE_HEIGHT(m) (8 << ((m) >> 4))
enum {
NvDmaFB = 0xd8000001,
NvDmaTT = 0xd8000002,
};
struct nouveau_info {
struct gralloc_drm_drv_t base;
int fd;
struct nouveau_device *dev;
struct nouveau_channel *chan;
int arch;
int tiled_scanout;
};
struct nouveau_buffer {
struct gralloc_drm_bo_t base;
struct nouveau_bo *bo;
};
static struct nouveau_bo *alloc_bo(struct nouveau_info *info,
int width, int height, int cpp, int usage, int *pitch)
{
struct nouveau_bo *bo = NULL;
int flags, tile_mode, tile_flags;
int tiled, scanout;
unsigned int align;
flags = NOUVEAU_BO_MAP | NOUVEAU_BO_VRAM;
tile_mode = 0;
tile_flags = 0;
scanout = !!(usage & GRALLOC_USAGE_HW_FB);
tiled = !(usage & (GRALLOC_USAGE_SW_READ_OFTEN |
GRALLOC_USAGE_SW_WRITE_OFTEN));
if (!info->chan)
tiled = 0;
else if (scanout && info->tiled_scanout)
tiled = 1;
/* calculate pitch align */
align = 64;
if (info->arch >= 0x50) {
if (scanout && !info->tiled_scanout)
align = 256;
else
tiled = 1;
}
*pitch = ALIGN(width * cpp, align);
if (tiled) {
if (info->arch >= 0xc0) {
if (height > 64)
tile_mode = 0x40;
else if (height > 32)
tile_mode = 0x30;
else if (height > 16)
tile_mode = 0x20;
else if (height > 8)
tile_mode = 0x10;
else
tile_mode = 0x00;
tile_flags = 0xfe00;
align = NVC0_TILE_HEIGHT(tile_mode);
height = ALIGN(height, align);
}
else if (info->arch >= 0x50) {
if (height > 32)
tile_mode = 4;
else if (height > 16)
tile_mode = 3;
else if (height > 8)
tile_mode = 2;
else if (height > 4)
tile_mode = 1;
else
tile_mode = 0;
tile_flags = (scanout && cpp != 2) ? 0x7a00 : 0x7000;
align = 1 << (tile_mode + 2);
height = ALIGN(height, align);
}
else {
align = *pitch / 4;
/* round down to the previous power of two */
align >>= 1;
align |= align >> 1;
align |= align >> 2;
align |= align >> 4;
align |= align >> 8;
align |= align >> 16;
align++;
align = MAX((info->dev->chipset >= 0x40) ? 1024 : 256,
align);
/* adjust pitch */
*pitch = ALIGN(*pitch, align);
tile_mode = *pitch;
}
}
if (cpp == 4)
tile_flags |= NOUVEAU_BO_TILE_32BPP;
else if (cpp == 2)
tile_flags |= NOUVEAU_BO_TILE_16BPP;
if (scanout)
tile_flags |= NOUVEAU_BO_TILE_SCANOUT;
if (nouveau_bo_new_tile(info->dev, flags, 0, *pitch * height,
tile_mode, tile_flags, &bo)) {
ALOGE("failed to allocate bo (flags 0x%x, size %d, tile_mode 0x%x, tile_flags 0x%x)",
flags, *pitch * height, tile_mode, tile_flags);
bo = NULL;
}
return bo;
}
static struct gralloc_drm_bo_t *
nouveau_alloc(struct gralloc_drm_drv_t *drv, struct gralloc_drm_handle_t *handle)
{
struct nouveau_info *info = (struct nouveau_info *) drv;
struct nouveau_buffer *nb;
int cpp;
cpp = gralloc_drm_get_bpp(handle->format);
if (!cpp) {
ALOGE("unrecognized format 0x%x", handle->format);
return NULL;
}
nb = calloc(1, sizeof(*nb));
if (!nb)
return NULL;
if (handle->name) {
if (nouveau_bo_handle_ref(info->dev, handle->name, &nb->bo)) {
ALOGE("failed to create nouveau bo from name %u",
handle->name);
free(nb);
return NULL;
}
}
else {
int width, height, pitch;
width = handle->width;
height = handle->height;
gralloc_drm_align_geometry(handle->format, &width, &height);
nb->bo = alloc_bo(info, width, height,
cpp, handle->usage, &pitch);
if (!nb->bo) {
ALOGE("failed to allocate nouveau bo %dx%dx%d",
handle->width, handle->height, cpp);
free(nb);
return NULL;
}
if (nouveau_bo_handle_get(nb->bo,
(uint32_t *) &handle->name)) {
ALOGE("failed to flink nouveau bo");
nouveau_bo_ref(NULL, &nb->bo);
free(nb);
return NULL;
}
handle->stride = pitch;
}
if (handle->usage & GRALLOC_USAGE_HW_FB)
nb->base.fb_handle = nb->bo->handle;
nb->base.handle = handle;
return &nb->base;
}
static void nouveau_free(struct gralloc_drm_drv_t *drv,
struct gralloc_drm_bo_t *bo)
{
struct nouveau_buffer *nb = (struct nouveau_buffer *) bo;
nouveau_bo_ref(NULL, &nb->bo);
free(nb);
}
static int nouveau_map(struct gralloc_drm_drv_t *drv,
struct gralloc_drm_bo_t *bo, int x, int y, int w, int h,
int enable_write, void **addr)
{
struct nouveau_buffer *nb = (struct nouveau_buffer *) bo;
uint32_t flags;
int err;
flags = NOUVEAU_BO_RD;
if (enable_write)
flags |= NOUVEAU_BO_WR;
/* TODO if tiled, allocate a linear copy of bo in GART and map it */
err = nouveau_bo_map(nb->bo, flags);
if (!err)
*addr = nb->bo->map;
return err;
}
static void nouveau_unmap(struct gralloc_drm_drv_t *drv,
struct gralloc_drm_bo_t *bo)
{
struct nouveau_buffer *nb = (struct nouveau_buffer *) bo;
/* TODO if tiled, unmap the linear bo and copy back */
nouveau_bo_unmap(nb->bo);
}
static void nouveau_destroy(struct gralloc_drm_drv_t *drv)
{
struct nouveau_info *info = (struct nouveau_info *) drv;
if (info->chan)
nouveau_channel_free(&info->chan);
nouveau_device_close(&info->dev);
free(info);
}
static int nouveau_init(struct nouveau_info *info)
{
int err = 0;
switch (info->dev->chipset & 0xf0) {
case 0x00:
info->arch = 0x04;
break;
case 0x10:
info->arch = 0x10;
break;
case 0x20:
info->arch = 0x20;
break;
case 0x30:
info->arch = 0x30;
break;
case 0x40:
case 0x60:
info->arch = 0x40;
break;
case 0x50:
case 0x80:
case 0x90:
case 0xa0:
info->arch = 0x50;
break;
case 0xc0:
info->arch = 0xc0;
break;
default:
ALOGE("unknown nouveau chipset 0x%x", info->dev->chipset);
err = -EINVAL;
break;
}
info->tiled_scanout = (info->chan != NULL);
return err;
}
struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_nouveau(int fd)
{
struct nouveau_info *info;
int err;
info = calloc(1, sizeof(*info));
if (!info)
return NULL;
info->fd = fd;
err = nouveau_device_open_existing(&info->dev, 0, info->fd, 0);
if (err) {
ALOGE("failed to create nouveau device");
free(info);
return NULL;
}
err = nouveau_channel_alloc(info->dev, NvDmaFB, NvDmaTT,
24 * 1024, &info->chan);
if (err) {
/* make it non-fatal temporarily as it may require firmwares */
ALOGW("failed to create nouveau channel");
info->chan = NULL;
}
err = nouveau_init(info);
if (err) {
if (info->chan)
nouveau_channel_free(&info->chan);
nouveau_device_close(&info->dev);
free(info);
return NULL;
}
info->base.destroy = nouveau_destroy;
info->base.alloc = nouveau_alloc;
info->base.free = nouveau_free;
info->base.map = nouveau_map;
info->base.unmap = nouveau_unmap;
return &info->base;
}