summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/vc4/vc4_bufmgr.c
blob: 653787e1905c9c237df557c3b7f7fa18495ab332 (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
/*
 * Copyright © 2014 Broadcom
 *
 * 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 (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 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.
 */

#include <errno.h>
#include <err.h>
#include <stdio.h>
#include <sys/mman.h>
#include <xf86drm.h>
#include <xf86drmMode.h>

#include "util/u_memory.h"

#include "vc4_context.h"
#include "vc4_screen.h"

struct vc4_bo *
vc4_bo_alloc(struct vc4_screen *screen, uint32_t size, const char *name)
{
        struct vc4_bo *bo = CALLOC_STRUCT(vc4_bo);
        if (!bo)
                return NULL;

        pipe_reference_init(&bo->reference, 1);
        bo->screen = screen;
        bo->size = size;
        bo->name = name;

#ifndef USE_VC4_SIMULATOR
        struct drm_mode_create_dumb create;
        memset(&create, 0, sizeof(create));

        create.width = 128;
        create.bpp = 8;
        create.height = (size + 127) / 128;

        int ret = drmIoctl(screen->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
        if (ret != 0)
                errx(1, "create ioctl");

        bo->handle = create.handle;
        assert(create.size >= size);
#else /* USE_VC4_SIMULATOR */
        static int next_handle = 0;
        bo->handle = next_handle++;

        bo->map = malloc(size);
#endif /* USE_VC4_SIMULATOR */

        return bo;
}

void
vc4_bo_free(struct vc4_bo *bo)
{
#ifndef USE_VC4_SIMULATOR
        struct vc4_screen *screen = bo->screen;

        struct drm_gem_close c;
        c.handle = bo->handle;
        int ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_CLOSE, &c);
        if (ret != 0)
                fprintf(stderr, "close object %d: %s\n", bo->handle, strerror(errno));
#else
        free(bo->map);
#endif

        free(bo);
}

struct vc4_bo *
vc4_bo_open_name(struct vc4_screen *screen, uint32_t name,
                 uint32_t winsys_stride)
{
        struct vc4_bo *bo = CALLOC_STRUCT(vc4_bo);

        struct drm_gem_open o;
        o.name = name;
        int ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_OPEN, &o);
        if (ret) {
                fprintf(stderr, "Failed to open bo %d: %s\n",
                        name, strerror(errno));
                free(bo);
                return NULL;
        }

        pipe_reference_init(&bo->reference, 1);
        bo->screen = screen;
        bo->handle = o.handle;
        bo->size = o.size;

#ifdef USE_VC4_SIMULATOR
        vc4_bo_map(bo);
        bo->simulator_winsys_map = bo->map;
        bo->simulator_winsys_stride = winsys_stride;
        bo->map = malloc(bo->size);
#endif

        return bo;
}

struct vc4_bo *
vc4_bo_alloc_mem(struct vc4_screen *screen, const void *data, uint32_t size,
                 const char *name)
{
        void *map;
        struct vc4_bo *bo;

        bo = vc4_bo_alloc(screen, size, name);
        map = vc4_bo_map(bo);
        memcpy(map, data, size);
        return bo;
}

bool
vc4_bo_flink(struct vc4_bo *bo, uint32_t *name)
{
#ifndef USE_VC4_SIMULATOR
        struct drm_gem_flink flink = {
                .handle = bo->handle,
        };
        int ret = drmIoctl(bo->screen->fd, DRM_IOCTL_GEM_FLINK, &flink);
        if (ret) {
                fprintf(stderr, "Failed to flink bo %d: %s\n",
                        bo->handle, strerror(errno));
                free(bo);
                return false;
        }

        *name = flink.name;
#endif /* USE_VC4_SIMULATOR */

        return true;
}

void *
vc4_bo_map(struct vc4_bo *bo)
{
        int ret;

        if (bo->map)
                return bo->map;

        struct drm_mode_map_dumb map;
        memset(&map, 0, sizeof(map));
        map.handle = bo->handle;
        ret = drmIoctl(bo->screen->fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
        if (ret != 0)
                errx(1, "map ioctl");

        bo->map = mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
                       bo->screen->fd, map.offset);
        if (bo->map == MAP_FAILED) {
                errx(1, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
                     bo->handle, (long long)map.offset, bo->size);
        }

        return bo->map;
}