aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/panfrost/pan_drm.c
blob: 135eb4d829146c5da75e4ba26bae7e8c47b2560d (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * © Copyright 2019 Collabora, Ltd.
 * Copyright 2019 Alyssa Rosenzweig
 *
 * 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 <fcntl.h>
#include <xf86drm.h>

#include "drm-uapi/panfrost_drm.h"

#include "util/u_memory.h"
#include "util/os_time.h"
#include "os/os_mman.h"

#include "pan_screen.h"
#include "pan_resource.h"
#include "pan_context.h"
#include "pan_util.h"
#include "pandecode/decode.h"

void
panfrost_drm_mmap_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
{
        struct drm_panfrost_mmap_bo mmap_bo = { .handle = bo->gem_handle };
        int ret;

        if (bo->cpu)
                return;

        ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);
        if (ret) {
                fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret);
                assert(0);
        }

        bo->cpu = os_mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
                          screen->fd, mmap_bo.offset);
        if (bo->cpu == MAP_FAILED) {
                fprintf(stderr, "mmap failed: %p\n", bo->cpu);
                assert(0);
        }

        /* Record the mmap if we're tracing */
        if (pan_debug & PAN_DBG_TRACE)
                pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
}

static void
panfrost_drm_munmap_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
{
        if (!bo->cpu)
                return;

        if (os_munmap((void *) (uintptr_t)bo->cpu, bo->size)) {
                perror("munmap");
                abort();
        }

        bo->cpu = NULL;
}

struct panfrost_bo *
panfrost_drm_create_bo(struct panfrost_screen *screen, size_t size,
                       uint32_t flags)
{
        struct panfrost_bo *bo;

        /* Kernel will fail (confusingly) with EPERM otherwise */
        assert(size > 0);

        unsigned translated_flags = 0;

        /* TODO: translate flags to kernel flags, if the kernel supports */

        struct drm_panfrost_create_bo create_bo = {
                .size = size,
                .flags = translated_flags,
        };

        /* Before creating a BO, we first want to check the cache */

        bo = panfrost_bo_cache_fetch(screen, size, flags);

        if (bo == NULL) {
                /* Otherwise, the cache misses and we need to allocate a BO fresh from
                 * the kernel */

                int ret;

                ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
                if (ret) {
                        fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret);
                        assert(0);
                }

                /* We have a BO allocated from the kernel; fill in the userspace
                 * version */

                bo = rzalloc(screen, struct panfrost_bo);
                bo->size = create_bo.size;
                bo->gpu = create_bo.offset;
                bo->gem_handle = create_bo.handle;
        }

        /* Only mmap now if we know we need to. For CPU-invisible buffers, we
         * never map since we don't care about their contents; they're purely
         * for GPU-internal use. */

        if (!(flags & (PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_DELAY_MMAP)))
                panfrost_drm_mmap_bo(screen, bo);

        pipe_reference_init(&bo->reference, 1);
        return bo;
}

void
panfrost_drm_release_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
{
        struct drm_gem_close gem_close = { .handle = bo->gem_handle };
        int ret;

        if (!bo)
                return;

        panfrost_drm_munmap_bo(screen, bo);

        ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
        if (ret) {
                fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
                assert(0);
        }

        ralloc_free(bo);
}

void
panfrost_drm_allocate_slab(struct panfrost_screen *screen,
                           struct panfrost_memory *mem,
                           size_t pages,
                           bool same_va,
                           int extra_flags,
                           int commit_count,
                           int extent)
{
        // TODO cache allocations
        // TODO properly handle errors
        // TODO take into account extra_flags
        mem->bo = panfrost_drm_create_bo(screen, pages * 4096, extra_flags);
        mem->stack_bottom = 0;
}

void
panfrost_drm_free_slab(struct panfrost_screen *screen, struct panfrost_memory *mem)
{
        panfrost_bo_unreference(&screen->base, mem->bo);
        mem->bo = NULL;
}

struct panfrost_bo *
panfrost_drm_import_bo(struct panfrost_screen *screen, int fd)
{
        struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
        struct drm_panfrost_get_bo_offset get_bo_offset = {0,};
        MAYBE_UNUSED int ret;
        unsigned gem_handle;

        ret = drmPrimeFDToHandle(screen->fd, fd, &gem_handle);
        assert(!ret);

        get_bo_offset.handle = gem_handle;
        ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get_bo_offset);
        assert(!ret);

        bo->gem_handle = gem_handle;
        bo->gpu = (mali_ptr) get_bo_offset.offset;
        bo->size = lseek(fd, 0, SEEK_END);
        assert(bo->size > 0);
        pipe_reference_init(&bo->reference, 1);

        // TODO map and unmap on demand?
        panfrost_drm_mmap_bo(screen, bo);
        return bo;
}

int
panfrost_drm_export_bo(struct panfrost_screen *screen, const struct panfrost_bo *bo)
{
        struct drm_prime_handle args = {
                .handle = bo->gem_handle,
                .flags = DRM_CLOEXEC,
        };

        int ret = drmIoctl(screen->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
        if (ret == -1)
                return -1;

        return args.fd;
}

static int
panfrost_drm_submit_job(struct panfrost_context *ctx, u64 job_desc, int reqs)
{
        struct pipe_context *gallium = (struct pipe_context *) ctx;
        struct panfrost_screen *screen = pan_screen(gallium->screen);
        struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
        struct drm_panfrost_submit submit = {0,};
        int *bo_handles, ret;

        submit.in_syncs = (u64) (uintptr_t) &ctx->out_sync;
        submit.in_sync_count = 1;

        submit.out_sync = ctx->out_sync;

        submit.jc = job_desc;
        submit.requirements = reqs;

        bo_handles = calloc(job->bos->entries, sizeof(*bo_handles));
        assert(bo_handles);

        set_foreach(job->bos, entry) {
                struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
                assert(bo->gem_handle > 0);
                bo_handles[submit.bo_handle_count++] = bo->gem_handle;
        }

        submit.bo_handles = (u64) (uintptr_t) bo_handles;
        ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
        free(bo_handles);
        if (ret) {
                fprintf(stderr, "Error submitting: %m\n");
                return errno;
        }

        /* Trace the job if we're doing that */
        if (pan_debug & PAN_DBG_TRACE) {
                /* Wait so we can get errors reported back */
                drmSyncobjWait(screen->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
                pandecode_jc(submit.jc, FALSE);
        }

        return 0;
}

int
panfrost_drm_submit_vs_fs_job(struct panfrost_context *ctx, bool has_draws, bool is_scanout)
{
        int ret = 0;

        struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);

        /* TODO: Add here the transient pools */
        panfrost_job_add_bo(job, ctx->shaders.bo);
        panfrost_job_add_bo(job, ctx->scratchpad.bo);
        panfrost_job_add_bo(job, ctx->tiler_heap.bo);
        panfrost_job_add_bo(job, ctx->varying_mem.bo);
        panfrost_job_add_bo(job, ctx->tiler_polygon_list.bo);

        if (job->first_job.gpu) {
                ret = panfrost_drm_submit_job(ctx, job->first_job.gpu, 0);
                assert(!ret);
        }

        if (job->first_tiler.gpu || job->clear) {
                struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[0];
                if (surf) {
                        struct panfrost_resource *res = pan_resource(surf->texture);
                        assert(res->bo);
                        panfrost_job_add_bo(job, res->bo);
                }
                ret = panfrost_drm_submit_job(ctx, panfrost_fragment_job(ctx, has_draws), PANFROST_JD_REQ_FS);
                assert(!ret);
        }

        return ret;
}

static struct panfrost_fence *
panfrost_fence_create(struct panfrost_context *ctx)
{
        struct pipe_context *gallium = (struct pipe_context *) ctx;
        struct panfrost_screen *screen = pan_screen(gallium->screen);
        struct panfrost_fence *f = calloc(1, sizeof(*f));
        if (!f)
                return NULL;

        /* Snapshot the last Panfrost's rendering's out fence.  We'd rather have
         * another syncobj instead of a sync file, but this is all we get.
         * (HandleToFD/FDToHandle just gives you another syncobj ID for the
         * same syncobj).
         */
        drmSyncobjExportSyncFile(screen->fd, ctx->out_sync, &f->fd);
        if (f->fd == -1) {
                fprintf(stderr, "export failed\n");
                free(f);
                return NULL;
        }

        pipe_reference_init(&f->reference, 1);

        return f;
}

void
panfrost_drm_force_flush_fragment(struct panfrost_context *ctx,
                                  struct pipe_fence_handle **fence)
{
        struct pipe_context *gallium = (struct pipe_context *) ctx;
        struct panfrost_screen *screen = pan_screen(gallium->screen);

        if (!screen->last_fragment_flushed) {
                drmSyncobjWait(screen->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
                screen->last_fragment_flushed = true;

                /* The job finished up, so we're safe to clean it up now */
                panfrost_free_job(ctx, screen->last_job);
        }

        if (fence) {
                struct panfrost_fence *f = panfrost_fence_create(ctx);
                gallium->screen->fence_reference(gallium->screen, fence, NULL);
                *fence = (struct pipe_fence_handle *)f;
        }
}

unsigned
panfrost_drm_query_gpu_version(struct panfrost_screen *screen)
{
        struct drm_panfrost_get_param get_param = {0,};
        MAYBE_UNUSED int ret;

        get_param.param = DRM_PANFROST_PARAM_GPU_PROD_ID;
        ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);
        assert(!ret);

        return get_param.value;
}

int
panfrost_drm_init_context(struct panfrost_context *ctx)
{
        struct pipe_context *gallium = (struct pipe_context *) ctx;
        struct panfrost_screen *screen = pan_screen(gallium->screen);

        return drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
                                &ctx->out_sync);
}

void
panfrost_drm_fence_reference(struct pipe_screen *screen,
                             struct pipe_fence_handle **ptr,
                             struct pipe_fence_handle *fence)
{
        struct panfrost_fence **p = (struct panfrost_fence **)ptr;
        struct panfrost_fence *f = (struct panfrost_fence *)fence;
        struct panfrost_fence *old = *p;

        if (pipe_reference(&(*p)->reference, &f->reference)) {
                close(old->fd);
                free(old);
        }
        *p = f;
}

boolean
panfrost_drm_fence_finish(struct pipe_screen *pscreen,
                          struct pipe_context *ctx,
                          struct pipe_fence_handle *fence,
                          uint64_t timeout)
{
        struct panfrost_screen *screen = pan_screen(pscreen);
        struct panfrost_fence *f = (struct panfrost_fence *)fence;
        int ret;

        unsigned syncobj;
        ret = drmSyncobjCreate(screen->fd, 0, &syncobj);
        if (ret) {
                fprintf(stderr, "Failed to create syncobj to wait on: %m\n");
                return false;
        }

        drmSyncobjImportSyncFile(screen->fd, syncobj, f->fd);
        if (ret) {
                fprintf(stderr, "Failed to import fence to syncobj: %m\n");
                return false;
        }

        uint64_t abs_timeout = os_time_get_absolute_timeout(timeout);
        if (abs_timeout == OS_TIMEOUT_INFINITE)
                abs_timeout = INT64_MAX;

        ret = drmSyncobjWait(screen->fd, &syncobj, 1, abs_timeout, 0, NULL);

        drmSyncobjDestroy(screen->fd, syncobj);

        return ret >= 0;
}