panfrost: Wait for last job to finish in force_flush_fragment
[mesa.git] / src / gallium / drivers / panfrost / pan_drm.c
1 /*
2 * © Copyright 2019 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25 #include <fcntl.h>
26 #include <xf86drm.h>
27
28 #include "drm-uapi/panfrost_drm.h"
29
30 #include "util/u_memory.h"
31 #include "util/os_time.h"
32
33 #include "pan_screen.h"
34 #include "pan_resource.h"
35 #include "pan_context.h"
36 #include "pan_drm.h"
37 #include "pan_trace.h"
38
39 struct panfrost_drm {
40 struct panfrost_driver base;
41 int fd;
42 };
43
44 static void
45 panfrost_drm_allocate_slab(struct panfrost_screen *screen,
46 struct panfrost_memory *mem,
47 size_t pages,
48 bool same_va,
49 int extra_flags,
50 int commit_count,
51 int extent)
52 {
53 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
54 struct drm_panfrost_create_bo create_bo = {
55 .size = pages * 4096,
56 .flags = 0, // TODO figure out proper flags..
57 };
58 struct drm_panfrost_mmap_bo mmap_bo = {0,};
59 int ret;
60
61 // TODO cache allocations
62 // TODO properly handle errors
63 // TODO take into account extra_flags
64
65 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
66 if (ret) {
67 fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret);
68 assert(0);
69 }
70
71 mem->gpu = create_bo.offset;
72 mem->gem_handle = create_bo.handle;
73 mem->stack_bottom = 0;
74 mem->size = create_bo.size;
75
76 // TODO map and unmap on demand?
77 mmap_bo.handle = create_bo.handle;
78 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);
79 if (ret) {
80 fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret);
81 assert(0);
82 }
83
84 mem->cpu = mmap(NULL, mem->size, PROT_READ | PROT_WRITE, MAP_SHARED,
85 drm->fd, mmap_bo.offset);
86 if (mem->cpu == MAP_FAILED) {
87 fprintf(stderr, "mmap failed: %p\n", mem->cpu);
88 assert(0);
89 }
90
91 /* Record the mmap if we're tracing */
92 if (!(extra_flags & PAN_ALLOCATE_GROWABLE))
93 pantrace_mmap(mem->gpu, mem->cpu, mem->size, NULL);
94 }
95
96 static void
97 panfrost_drm_free_slab(struct panfrost_screen *screen, struct panfrost_memory *mem)
98 {
99 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
100 struct drm_gem_close gem_close = {
101 .handle = mem->gem_handle,
102 };
103 int ret;
104
105 if (munmap((void *) (uintptr_t) mem->cpu, mem->size)) {
106 perror("munmap");
107 abort();
108 }
109
110 mem->cpu = NULL;
111
112 ret = drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
113 if (ret) {
114 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
115 assert(0);
116 }
117
118 mem->gem_handle = -1;
119 }
120
121 static struct panfrost_bo *
122 panfrost_drm_import_bo(struct panfrost_screen *screen, struct winsys_handle *whandle)
123 {
124 struct panfrost_bo *bo = CALLOC_STRUCT(panfrost_bo);
125 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
126 struct drm_panfrost_get_bo_offset get_bo_offset = {0,};
127 struct drm_panfrost_mmap_bo mmap_bo = {0,};
128 int ret;
129 unsigned gem_handle;
130
131 ret = drmPrimeFDToHandle(drm->fd, whandle->handle, &gem_handle);
132 assert(!ret);
133
134 get_bo_offset.handle = gem_handle;
135 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get_bo_offset);
136 assert(!ret);
137
138 bo->gem_handle = gem_handle;
139 bo->gpu = (mali_ptr) get_bo_offset.offset;
140
141 // TODO map and unmap on demand?
142 mmap_bo.handle = gem_handle;
143 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);
144 if (ret) {
145 fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret);
146 assert(0);
147 }
148
149 bo->size = lseek(whandle->handle, 0, SEEK_END);
150 assert(bo->size > 0);
151 bo->cpu = mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
152 drm->fd, mmap_bo.offset);
153 if (bo->cpu == MAP_FAILED) {
154 fprintf(stderr, "mmap failed: %p\n", bo->cpu);
155 assert(0);
156 }
157
158 /* Record the mmap if we're tracing */
159 pantrace_mmap(bo->gpu, bo->cpu, bo->size, NULL);
160
161 return bo;
162 }
163
164 static int
165 panfrost_drm_export_bo(struct panfrost_screen *screen, int gem_handle, unsigned int stride, struct winsys_handle *whandle)
166 {
167 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
168 struct drm_prime_handle args = {
169 .handle = gem_handle,
170 .flags = DRM_CLOEXEC,
171 };
172
173 int ret = drmIoctl(drm->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
174 if (ret == -1)
175 return FALSE;
176
177 whandle->handle = args.fd;
178 whandle->stride = stride;
179
180 return TRUE;
181 }
182
183 static void
184 panfrost_drm_free_imported_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
185 {
186 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
187 struct drm_gem_close gem_close = {
188 .handle = bo->gem_handle,
189 };
190 int ret;
191
192 ret = drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
193 if (ret) {
194 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
195 assert(0);
196 }
197
198 bo->gem_handle = -1;
199 bo->gpu = (mali_ptr)NULL;
200 }
201
202 static int
203 panfrost_drm_submit_job(struct panfrost_context *ctx, u64 job_desc, int reqs, struct pipe_surface *surf)
204 {
205 struct pipe_context *gallium = (struct pipe_context *) ctx;
206 struct panfrost_screen *screen = pan_screen(gallium->screen);
207 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
208 struct drm_panfrost_submit submit = {0,};
209 int bo_handles[7];
210
211 submit.in_syncs = (u64) (uintptr_t) &ctx->out_sync;
212 submit.in_sync_count = 1;
213
214 submit.out_sync = ctx->out_sync;
215
216 submit.jc = job_desc;
217 submit.requirements = reqs;
218
219 if (surf) {
220 struct panfrost_resource *res = pan_resource(surf->texture);
221 assert(res->bo->gem_handle > 0);
222 bo_handles[submit.bo_handle_count++] = res->bo->gem_handle;
223
224 if (res->bo->checksum_slab.gem_handle)
225 bo_handles[submit.bo_handle_count++] = res->bo->checksum_slab.gem_handle;
226 }
227
228 /* TODO: Add here the transient pools */
229 bo_handles[submit.bo_handle_count++] = ctx->shaders.gem_handle;
230 bo_handles[submit.bo_handle_count++] = ctx->scratchpad.gem_handle;
231 bo_handles[submit.bo_handle_count++] = ctx->tiler_heap.gem_handle;
232 bo_handles[submit.bo_handle_count++] = ctx->varying_mem.gem_handle;
233 bo_handles[submit.bo_handle_count++] = ctx->misc_0.gem_handle;
234 submit.bo_handles = (u64)bo_handles;
235
236 /* Dump memory _before_ submitting so we're not corrupted with actual GPU results */
237 pantrace_dump_memory();
238
239 if (drmIoctl(drm->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit)) {
240 fprintf(stderr, "Error submitting: %m\n");
241 return errno;
242 }
243
244 /* Trace the job if we're doing that and do a memory dump. We may
245 * want to adjust this logic once we're ready to trace FBOs */
246 pantrace_submit_job(submit.jc, submit.requirements, FALSE);
247
248 return 0;
249 }
250
251 static int
252 panfrost_drm_submit_vs_fs_job(struct panfrost_context *ctx, bool has_draws, bool is_scanout)
253 {
254 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[0];
255 int ret;
256
257 if (has_draws) {
258 ret = panfrost_drm_submit_job(ctx, ctx->set_value_job, 0, NULL);
259 assert(!ret);
260 }
261
262 ret = panfrost_drm_submit_job(ctx, panfrost_fragment_job(ctx), PANFROST_JD_REQ_FS, surf);
263
264 return ret;
265 }
266
267 static struct panfrost_fence *
268 panfrost_fence_create(struct panfrost_context *ctx)
269 {
270 struct pipe_context *gallium = (struct pipe_context *) ctx;
271 struct panfrost_screen *screen = pan_screen(gallium->screen);
272 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
273 struct panfrost_fence *f = calloc(1, sizeof(*f));
274 if (!f)
275 return NULL;
276
277 /* Snapshot the last Panfrost's rendering's out fence. We'd rather have
278 * another syncobj instead of a sync file, but this is all we get.
279 * (HandleToFD/FDToHandle just gives you another syncobj ID for the
280 * same syncobj).
281 */
282 drmSyncobjExportSyncFile(drm->fd, ctx->out_sync, &f->fd);
283 if (f->fd == -1) {
284 fprintf(stderr, "export failed\n");
285 free(f);
286 return NULL;
287 }
288
289 pipe_reference_init(&f->reference, 1);
290
291 return f;
292 }
293
294 static void
295 panfrost_drm_force_flush_fragment(struct panfrost_context *ctx,
296 struct pipe_fence_handle **fence)
297 {
298 struct pipe_context *gallium = (struct pipe_context *) ctx;
299 struct panfrost_screen *screen = pan_screen(gallium->screen);
300 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
301 int ret;
302
303 if (!screen->last_fragment_flushed) {
304 drmSyncobjWait(drm->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
305 screen->last_fragment_flushed = true;
306 }
307
308 if (fence) {
309 struct panfrost_fence *f = panfrost_fence_create(ctx);
310 gallium->screen->fence_reference(gallium->screen, fence, NULL);
311 *fence = (struct pipe_fence_handle *)f;
312 }
313 }
314
315 static void
316 panfrost_drm_enable_counters(struct panfrost_screen *screen)
317 {
318 fprintf(stderr, "unimplemented: %s\n", __func__);
319 }
320
321 static void
322 panfrost_drm_dump_counters(struct panfrost_screen *screen)
323 {
324 fprintf(stderr, "unimplemented: %s\n", __func__);
325 }
326
327 static unsigned
328 panfrost_drm_query_gpu_version(struct panfrost_screen *screen)
329 {
330 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
331 struct drm_panfrost_get_param get_param = {0,};
332 int ret;
333
334 get_param.param = DRM_PANFROST_PARAM_GPU_ID;
335 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);
336 assert(!ret);
337
338 return get_param.value;
339 }
340
341 static int
342 panfrost_drm_init_context(struct panfrost_context *ctx)
343 {
344 struct pipe_context *gallium = (struct pipe_context *) ctx;
345 struct panfrost_screen *screen = pan_screen(gallium->screen);
346 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
347
348 return drmSyncobjCreate(drm->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
349 &ctx->out_sync);
350 }
351
352 static void
353 panfrost_drm_fence_reference(struct pipe_screen *screen,
354 struct pipe_fence_handle **ptr,
355 struct pipe_fence_handle *fence)
356 {
357 struct panfrost_fence **p = (struct panfrost_fence **)ptr;
358 struct panfrost_fence *f = (struct panfrost_fence *)fence;
359 struct panfrost_fence *old = *p;
360
361 if (pipe_reference(&(*p)->reference, &f->reference)) {
362 close(old->fd);
363 free(old);
364 }
365 *p = f;
366 }
367
368 static boolean
369 panfrost_drm_fence_finish(struct pipe_screen *pscreen,
370 struct pipe_context *ctx,
371 struct pipe_fence_handle *fence,
372 uint64_t timeout)
373 {
374 struct panfrost_screen *screen = pan_screen(pscreen);
375 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
376 struct panfrost_fence *f = (struct panfrost_fence *)fence;
377 int ret;
378
379 unsigned syncobj;
380 ret = drmSyncobjCreate(drm->fd, 0, &syncobj);
381 if (ret) {
382 fprintf(stderr, "Failed to create syncobj to wait on: %m\n");
383 return false;
384 }
385
386 drmSyncobjImportSyncFile(drm->fd, syncobj, f->fd);
387 if (ret) {
388 fprintf(stderr, "Failed to import fence to syncobj: %m\n");
389 return false;
390 }
391
392 uint64_t abs_timeout = os_time_get_absolute_timeout(timeout);
393 if (abs_timeout == OS_TIMEOUT_INFINITE)
394 abs_timeout = INT64_MAX;
395
396 ret = drmSyncobjWait(drm->fd, &syncobj, 1, abs_timeout, 0, NULL);
397
398 drmSyncobjDestroy(drm->fd, syncobj);
399
400 return ret >= 0;
401 }
402
403 struct panfrost_driver *
404 panfrost_create_drm_driver(int fd)
405 {
406 struct panfrost_drm *driver = CALLOC_STRUCT(panfrost_drm);
407
408 driver->fd = fd;
409
410 driver->base.import_bo = panfrost_drm_import_bo;
411 driver->base.export_bo = panfrost_drm_export_bo;
412 driver->base.free_imported_bo = panfrost_drm_free_imported_bo;
413 driver->base.submit_vs_fs_job = panfrost_drm_submit_vs_fs_job;
414 driver->base.force_flush_fragment = panfrost_drm_force_flush_fragment;
415 driver->base.allocate_slab = panfrost_drm_allocate_slab;
416 driver->base.free_slab = panfrost_drm_free_slab;
417 driver->base.enable_counters = panfrost_drm_enable_counters;
418 driver->base.query_gpu_version = panfrost_drm_query_gpu_version;
419 driver->base.init_context = panfrost_drm_init_context;
420 driver->base.fence_reference = panfrost_drm_fence_reference;
421 driver->base.fence_finish = panfrost_drm_fence_finish;
422 driver->base.dump_counters = panfrost_drm_dump_counters;
423
424 return &driver->base;
425 }