summaryrefslogtreecommitdiff
path: root/tegra/fence.c
blob: 7e3dc43218900889fa9a8bb8dcc407b8cd196ca2 (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
/*
 * Copyright © 2018 NVIDIA Corporation
 *
 * 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 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <errno.h>
#include <stdbool.h>
#include <stdio.h> /* XXX remove */
#include <string.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <sys/poll.h>

#include "private.h"

static int drm_tegra_syncobj_destroy(struct drm_tegra *drm, uint32_t handle)
{
    struct drm_syncobj_destroy args;
    int err;

    memset(&args, 0, sizeof(args));
    args.handle = handle;

    err = ioctl(drm->fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
    if (err < 0)
        return -errno;

    return 0;
}

int drm_tegra_fence_wait(struct drm_tegra *drm, struct drm_tegra_fence *fence)
{
    int err;

    if ((fence->flags & DRM_TEGRA_FENCE_EMIT) == 0)
        return 0;

    printf("fence: %p\n", fence);
    printf("  handle: %u\n", fence->handle);
    printf("  flags: %x\n", fence->flags);
    printf("  offset: %x\n", fence->offset);
    printf("  index: %u\n", fence->index);
    printf("  value: %u\n", fence->value);

    if (fence->flags & DRM_TEGRA_FENCE_FD) {
        while (true) {
            struct pollfd fds = {
                .fd = fence->handle,
                .events = POLLIN,
            };

            err = poll(&fds, 1, -1);
            if (err > 0) {
                if (fds.revents & (POLLERR | POLLNVAL))
                    err = -EINVAL;
                else
                    err = 0;

                break;
            }

            if (err == 0) {
                err = -ETIMEDOUT;
                break;
            }

            if (errno != EINTR && errno != EAGAIN) {
                err = -errno;
                break;
            }
        }

        close(fence->handle);
    } else {
        struct drm_syncobj_wait args;

        memset(&args, 0, sizeof(args));
        args.handles = (uintptr_t)&fence->handle;
        args.count_handles = 1;
        args.timeout_nsec = -1;
        args.flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL;

        err = ioctl(drm->fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
        if (err < 0)
            err = -errno;

        drm_tegra_syncobj_destroy(drm, fence->handle);
    }

    return err;
}