summaryrefslogtreecommitdiffstats
path: root/src/etnaviv/etna_bo.c
blob: 655e856cd346f66c833b17ba001d20deb85946a3 (plain)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (c) 2012-2013 Etnaviv Project
 *
 * 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, sub license,
 * 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 (including the
 * next paragraph) 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 NON-INFRINGEMENT. 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.
 */
#include <etnaviv/etna_bo.h>
#include <etnaviv/etna.h>
#include <etnaviv/etna_queue.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>

#include "gc_abi.h"

//#define DEBUG
#define ETNA_VIDMEM_ALIGNMENT (0x40)

enum etna_bo_type {
    ETNA_BO_TYPE_VIDMEM,    /* Main vidmem */
    ETNA_BO_TYPE_VIDMEM_EXTERNAL, /* Main vidmem, external handle */
    ETNA_BO_TYPE_USERMEM,   /* Mapped user memory */
    ETNA_BO_TYPE_CONTIGUOUS,/* Contiguous memory */
    ETNA_BO_TYPE_PHYSICAL   /* Mmap-ed physical memory */
};

/* Structure describing a block of video or user memory */
struct etna_bo {
    enum etna_bo_type bo_type;
    size_t size;
    enum viv_surf_type type;
    viv_node_t node;
    viv_addr_t address;
    void *logical;
    viv_usermem_t usermem_info;
};

#ifdef DEBUG
static const char *etna_bo_surf_type(struct etna_bo *mem)
{
    const char *ret = NULL;

    switch (mem->type) {
        case VIV_SURF_UNKNOWN:
            ret = "VIV_SURF_UNKNOWN";
        break;

        case VIV_SURF_INDEX:
            ret = "VIV_SURF_INDEX";
        break;

        case VIV_SURF_VERTEX:
            ret = "VIV_SURF_VERTEX";
        break;

        case VIV_SURF_TEXTURE:
            ret = "VIV_SURF_TEXTURE";
        break;

        case VIV_SURF_RENDER_TARGET:
            ret = "VIV_SURF_RENDER_TARGET";
        break;

        case VIV_SURF_DEPTH:
            ret = "VIV_SURF_DEPTH";
        break;

        case VIV_SURF_BITMAP:
            ret = "VIV_SURF_BITMAP";
        break;

        case VIV_SURF_TILE_STATUS:
            ret = "VIV_SURF_TILE_STATUS";
        break;

        case VIV_SURF_IMAGE:
            ret = "VIV_SURF_IMAGE";
        break;

        case VIV_SURF_MASK:
            ret = "VIV_SURF_MASK";
        break;

        case VIV_SURF_SCISSOR:
            ret = "VIV_SURF_SCISSOR";
        break;

        case VIV_SURF_HIERARCHICAL_DEPTH:
            ret = "VIV_SURF_HIERARCHICAL_DEPTH";
        break;

        default:
           ret = "hmmmm?";
        break;
    }
    return ret;
}
#endif

/* Lock (map) memory into both CPU and GPU memory space. */
static int etna_bo_lock(struct viv_conn *conn, struct etna_bo *mem)
{
    if(mem == NULL) return ETNA_INVALID_ADDR;
    if(mem->logical != NULL) return ETNA_ALREADY_LOCKED;

    if(viv_lock_vidmem(conn, mem->node, &mem->address, &mem->logical)!=0)
    {
#ifdef DEBUG
        fprintf(stderr, "Error locking render target memory\n");
#endif
        return ETNA_INTERNAL_ERROR;
    }
#ifdef DEBUG
    printf("Locked: phys=%08x log=%08x\n", (uint32_t)mem->address, (uint32_t)mem->logical);
#endif

    return ETNA_OK;
}

/* Unlock memory from both CPU and GPU memory space */
static int etna_bo_unlock(struct viv_conn *conn, struct etna_bo *mem, struct etna_queue *queue)
{
    if(mem == NULL) return ETNA_INVALID_ADDR;
    int async = 0;
    /* Unlocking video memory seems to be a two-step process. First try it synchronously
     * then the kernel can request an asynchronous component. Just queueing it asynchronously
     * in the first place will not free the virtual memory on v4 */
    if(viv_unlock_vidmem(conn, mem->node, mem->type, false, &async) != ETNA_OK)
    {
        return ETNA_INTERNAL_ERROR;
    }
    if(async)
    {
        if(queue)
        {
            /* If a queue is passed, add the async part at the end of the queue, to be submitted
             * with next flush.
             */
            if(etna_queue_unlock_vidmem(queue, mem->node, mem->type) != ETNA_OK)
            {
                return ETNA_INTERNAL_ERROR;
            }
        } else { /* No queue, need to submit async part directly as event */
            if(viv_unlock_vidmem(conn, mem->node, mem->type, true, &async) != ETNA_OK)
            {
                return ETNA_INTERNAL_ERROR;
            }
        }
    }
    mem->logical = NULL;
    mem->address = 0;
    return ETNA_OK;
}

struct etna_bo* etna_bo_new(struct viv_conn *conn, size_t bytes, uint32_t flags)
{
    struct etna_bo *mem = ETNA_CALLOC_STRUCT(etna_bo);
    if(mem == NULL) return NULL;

    if((flags & DRM_ETNA_GEM_TYPE_MASK) == DRM_ETNA_GEM_TYPE_CMD)
    {
        mem->bo_type = ETNA_BO_TYPE_CONTIGUOUS;
        /* Command buffers must be allocated with viv_alloc_contiguous */
        if(viv_alloc_contiguous(conn, bytes,
                    &mem->address,
                    &mem->logical,
                    &mem->size)!=0)
        {
            ETNA_FREE(mem);
            return NULL;
        }
    } else {
        enum viv_surf_type type = VIV_SURF_UNKNOWN;
        enum viv_pool pool = VIV_POOL_DEFAULT;
        /* Convert GEM bits to surface type */
        switch(flags & DRM_ETNA_GEM_TYPE_MASK)
        {
        case DRM_ETNA_GEM_TYPE_IDX: type = VIV_SURF_INDEX; break;
        case DRM_ETNA_GEM_TYPE_VTX: type = VIV_SURF_VERTEX; break;
        case DRM_ETNA_GEM_TYPE_TEX: type = VIV_SURF_TEXTURE; break;
        case DRM_ETNA_GEM_TYPE_RT:  type = VIV_SURF_RENDER_TARGET; break;
        case DRM_ETNA_GEM_TYPE_ZS:  type = VIV_SURF_DEPTH; break;
        case DRM_ETNA_GEM_TYPE_HZ:  type = VIV_SURF_HIERARCHICAL_DEPTH; break;
        case DRM_ETNA_GEM_TYPE_BMP: type = VIV_SURF_BITMAP; break;
        case DRM_ETNA_GEM_TYPE_TS:  type = VIV_SURF_TILE_STATUS; break;
        default: /* Invalid type */
            ETNA_FREE(mem);
            return NULL;
            break;
        }

        mem->bo_type = ETNA_BO_TYPE_VIDMEM;
        mem->type = type;
        if(viv_alloc_linear_vidmem(conn, bytes, ETNA_VIDMEM_ALIGNMENT, type, pool, &mem->node, &mem->size)!=0)
        {
#ifdef DEBUG
            fprintf(stderr, "Error allocating memory\n");
#endif
            return NULL;
        }
#ifdef DEBUG
        printf("Allocated: type:%s mem=%p node=%08x size=%08x\n", etna_bo_surf_type(mem), mem, (uint32_t)mem->node, mem->size);
#endif
        int status = etna_bo_lock(conn, mem);
        if(status != ETNA_OK)
        {
            etna_bo_del(conn, mem, NULL);
            return NULL;
        }
    }
    return mem;
}

struct etna_bo *etna_bo_from_usermem(struct viv_conn *conn, void *memory, size_t size)
{
    struct etna_bo *mem = ETNA_CALLOC_STRUCT(etna_bo);
    if(mem == NULL) return NULL;

    mem->bo_type = ETNA_BO_TYPE_USERMEM;
    mem->logical = memory;
    mem->size = size;

    if(viv_map_user_memory(conn, memory, size, &mem->usermem_info, &mem->address)!=0)
    {
        ETNA_FREE(mem);
        return NULL;
    }

    return mem;
}

struct etna_bo *etna_bo_from_fbdev(struct viv_conn *conn, int fd, size_t offset, size_t size)
{
    struct fb_fix_screeninfo finfo;
    struct etna_bo *mem = ETNA_CALLOC_STRUCT(etna_bo);
    if(mem == NULL) return NULL;

    if(ioctl(fd, FBIOGET_FSCREENINFO, &finfo))
        goto error;

    mem->bo_type = ETNA_BO_TYPE_PHYSICAL;
    if((mem->logical = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset)) == NULL)
        goto error;
    mem->address = finfo.smem_start + offset;
    mem->size = size;
    return mem;
error:
    ETNA_FREE(mem);
    return NULL;
}

struct etna_bo *etna_bo_from_name(struct viv_conn *conn, uint32_t name)
{
    struct etna_bo *mem = ETNA_CALLOC_STRUCT(etna_bo);
    if(mem == NULL) return NULL;

    mem->bo_type = ETNA_BO_TYPE_VIDMEM_EXTERNAL;
    mem->node = (viv_node_t)name;

    /* Lock to this address space */
    int status = etna_bo_lock(conn, mem);
    if(status != ETNA_OK)
    {
        free(mem);
        return NULL;
    }
    return mem;
}

struct etna_bo *etna_bo_ref(struct etna_bo *bo)
{
    /* TODO */
    return bo;
}

int etna_bo_del(struct viv_conn *conn, struct etna_bo *mem, struct etna_queue *queue)
{
    int rv = ETNA_OK;
    if(mem == NULL) return ETNA_OK;
    switch(mem->bo_type)
    {
    case ETNA_BO_TYPE_VIDMEM:
        if(mem->logical != NULL)
        {
            if((rv = etna_bo_unlock(conn, mem, queue)) != ETNA_OK)
            {
                printf("etna: Warning: could not unlock memory\n");
            }
        }
        if(queue)
        {
            if((rv = etna_queue_free_vidmem(queue, mem->node)) != ETNA_OK)
            {
                printf("etna: Warning: could not queue free video memory\n");
            }
        } else {
            if((rv = viv_free_vidmem(conn, mem->node)) != ETNA_OK)
            {
                printf("etna: Warning: could not free video memory\n");
            }
        }
        break;
    case ETNA_BO_TYPE_VIDMEM_EXTERNAL:
        if((rv = etna_bo_unlock(conn, mem, queue)) != ETNA_OK)
        {
            printf("etna: Warning: could not unlock memory\n");
        }
        break;
    case ETNA_BO_TYPE_USERMEM:
        if(queue)
        {
            rv = etna_queue_unmap_user_memory(queue, mem->logical, mem->size, mem->usermem_info, mem->address);
        } else
        {
            rv = viv_unmap_user_memory(conn, mem->logical, mem->size, mem->usermem_info, mem->address);
        }
        break;
    case ETNA_BO_TYPE_CONTIGUOUS:
        if(queue)
        {
            rv = etna_queue_free_contiguous(queue, mem->size, mem->address, mem->logical);
        } else {
            rv = viv_free_contiguous(conn, mem->size, mem->address, mem->logical);
        }
        break;
    case ETNA_BO_TYPE_PHYSICAL:
        if(munmap(mem->logical, mem->size) < 0)
        {
            rv = ETNA_OUT_OF_MEMORY;
        }
        break;
    }
    ETNA_FREE(mem);
    return rv;
}

int etna_bo_get_name(struct etna_bo *bo, uint32_t *name)
{
    *name = (uint32_t)bo->node;
    return 0;
}

uint32_t etna_bo_handle(struct etna_bo *bo)
{
    return (uint32_t)bo->node;
}

uint32_t etna_bo_size(struct etna_bo *bo)
{
    return bo->size;
}

void *etna_bo_map(struct etna_bo *bo)
{
    return bo->logical;
}

int etna_bo_cpu_prep(struct etna_bo *bo, struct etna_ctx *pipe, uint32_t op)
{
    /* TODO */
    return 0;
}

void etna_bo_cpu_fini(struct etna_bo *bo)
{
    /* No-op */
}

uint32_t etna_bo_gpu_address(struct etna_bo *bo)
{
    return bo->address;
}