1/*
2 * Copyright © 2021 NVIDIA Corporation
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#ifdef HAVE_CONFIG_H
24#  include "config.h"
25#endif
26
27#include <errno.h>
28#include <string.h>
29
30#include <sys/ioctl.h>
31
32#include "private.h"
33
34drm_public int
35drm_tegra_syncpoint_new(struct drm_tegra *drm,
36                        struct drm_tegra_syncpoint **syncptp)
37{
38    struct drm_tegra_syncpoint_allocate args;
39    struct drm_tegra_syncpoint *syncpt;
40    int err;
41
42    syncpt = calloc(1, sizeof(*syncpt));
43    if (!syncpt)
44        return -ENOMEM;
45
46    memset(&args, 0, sizeof(args));
47
48    err = ioctl(drm->fd, DRM_IOCTL_TEGRA_SYNCPOINT_ALLOCATE, &args);
49    if (err < 0) {
50        free(syncpt);
51        return -errno;
52    }
53
54    syncpt->drm = drm;
55    syncpt->id = args.id;
56
57    *syncptp = syncpt;
58
59    return 0;
60}
61
62drm_public int
63drm_tegra_syncpoint_free(struct drm_tegra_syncpoint *syncpt)
64{
65    struct drm_tegra_syncpoint_free args;
66    struct drm_tegra *drm = syncpt->drm;
67    int err;
68
69    if (!syncpt)
70        return -EINVAL;
71
72    memset(&args, 0, sizeof(args));
73    args.id = syncpt->id;
74
75    err = ioctl(drm->fd, DRM_IOCTL_TEGRA_SYNCPOINT_FREE, &args);
76    if (err < 0)
77        return -errno;
78
79    free(syncpt);
80
81    return 0;
82}
83
84drm_public int
85drm_tegra_fence_wait(struct drm_tegra_fence *fence, unsigned long timeout)
86{
87    struct drm_tegra_syncpoint_wait args;
88    struct drm_tegra *drm = fence->drm;
89    int err;
90
91    memset(&args, 0, sizeof(args));
92    args.timeout_ns = 0;
93    args.id = fence->syncpt;
94    args.threshold = fence->value;
95
96    err = ioctl(drm->fd, DRM_IOCTL_TEGRA_SYNCPOINT_WAIT, &args);
97    if (err < 0)
98        return -errno;
99
100    return 0;
101}
102