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
|
/*
* 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 <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;
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) {
fprintf(stderr, "create ioctl failure\n");
abort();
}
bo->handle = create.handle;
assert(create.size >= size);
return bo;
}
void
vc4_bo_free(struct vc4_bo *bo)
{
struct vc4_screen *screen = bo->screen;
if (bo->map) {
#ifdef USE_VC4_SIMULATOR
if (bo->simulator_winsys_map) {
free(bo->map);
bo->map = bo->simulator_winsys_map;
}
#endif
munmap(bo->map, bo->size);
}
struct drm_gem_close c;
memset(&c, 0, sizeof(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));
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;
bo->name = "winsys";
#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)
{
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;
return true;
}
bool
vc4_wait_seqno(struct vc4_screen *screen, uint64_t seqno, uint64_t timeout_ns)
{
#ifndef USE_VC4_SIMULATOR
struct drm_vc4_wait_seqno wait;
memset(&wait, 0, sizeof(wait));
wait.seqno = seqno;
wait.timeout_ns = timeout_ns;
int ret = drmIoctl(screen->fd, DRM_IOCTL_VC4_WAIT_SEQNO, &wait);
if (ret == -ETIME) {
return false;
} else if (ret != 0) {
fprintf(stderr, "wait failed\n");
abort();
} else {
screen->finished_seqno = wait.seqno;
return true;
}
#else
return true;
#endif
}
bool
vc4_bo_wait(struct vc4_bo *bo, uint64_t timeout_ns)
{
#ifndef USE_VC4_SIMULATOR
struct vc4_screen *screen = bo->screen;
struct drm_vc4_wait_bo wait;
memset(&wait, 0, sizeof(wait));
wait.handle = bo->handle;
wait.timeout_ns = timeout_ns;
int ret = drmIoctl(screen->fd, DRM_IOCTL_VC4_WAIT_BO, &wait);
if (ret == -ETIME) {
return false;
} else if (ret != 0) {
fprintf(stderr, "wait failed\n");
abort();
} else {
return true;
}
#else
return true;
#endif
}
void *
vc4_bo_map_unsynchronized(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) {
fprintf(stderr, "map ioctl failure\n");
abort();
}
bo->map = mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
bo->screen->fd, map.offset);
if (bo->map == MAP_FAILED) {
fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
bo->handle, (long long)map.offset, bo->size);
abort();
}
return bo->map;
}
void *
vc4_bo_map(struct vc4_bo *bo)
{
void *map = vc4_bo_map_unsynchronized(bo);
bool ok = vc4_bo_wait(bo, PIPE_TIMEOUT_INFINITE);
if (!ok) {
fprintf(stderr, "BO wait for map failed\n");
abort();
}
return map;
}
|