1 /*
2 * Copyright © 2018 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 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "tegra.h"
30
31 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
32
channel_open(struct drm_tegra * drm,struct drm_tegra_channel ** channel)33 static int channel_open(struct drm_tegra *drm,
34 struct drm_tegra_channel **channel)
35 {
36 static const struct {
37 enum drm_tegra_class class;
38 const char *name;
39 } classes[] = {
40 { DRM_TEGRA_VIC, "VIC" },
41 { DRM_TEGRA_GR2D, "GR2D" },
42 };
43 unsigned int i;
44 int err;
45
46 for (i = 0; i < ARRAY_SIZE(classes); i++) {
47 err = drm_tegra_channel_open(drm, classes[i].class, channel);
48 if (err < 0) {
49 fprintf(stderr, "failed to open channel to %s: %s\n",
50 classes[i].name, strerror(-err));
51 continue;
52 }
53
54 break;
55 }
56
57 return err;
58 }
59
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 const char *device = "/dev/dri/renderD128";
63 struct drm_tegra_syncpoint *syncpt;
64 struct drm_tegra_channel *channel;
65 struct drm_tegra_pushbuf *pushbuf;
66 struct drm_tegra_job *job;
67 struct drm_tegra *drm;
68 uint32_t *ptr;
69 int fd, err;
70
71 if (argc > 1)
72 device = argv[1];
73
74 fd = open(device, O_RDWR);
75 if (fd < 0) {
76 fprintf(stderr, "open() failed: %s\n", strerror(errno));
77 return 1;
78 }
79
80 err = drm_tegra_new(fd, &drm);
81 if (err < 0) {
82 fprintf(stderr, "failed to open Tegra device: %s\n", strerror(-err));
83 close(fd);
84 return 1;
85 }
86
87 err = drm_tegra_syncpoint_new(drm, &syncpt);
88 if (err < 0) {
89 fprintf(stderr, "failed to allocate syncpoint: %s\n", strerror(-err));
90 drm_tegra_close(drm);
91 close(fd);
92 return 1;
93 }
94
95 err = channel_open(drm, &channel);
96 if (err < 0) {
97 fprintf(stderr, "failed to open channel: %s\n", strerror(-err));
98 return 1;
99 }
100
101 err = drm_tegra_job_new(channel, &job);
102 if (err < 0) {
103 fprintf(stderr, "failed to create job: %s\n", strerror(-err));
104 return 1;
105 }
106
107 err = drm_tegra_job_get_pushbuf(job, &pushbuf);
108 if (err < 0) {
109 fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err));
110 return 1;
111 }
112
113 err = drm_tegra_pushbuf_begin(pushbuf, 4, &ptr);
114 if (err < 0) {
115 fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err));
116 return 1;
117 }
118
119 err = drm_tegra_pushbuf_sync_cond(pushbuf, &ptr, syncpt,
120 DRM_TEGRA_SYNC_COND_IMMEDIATE);
121 if (err < 0) {
122 fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err));
123 return 1;
124 }
125
126 err = drm_tegra_pushbuf_end(pushbuf, ptr);
127 if (err < 0) {
128 fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err));
129 return 1;
130 }
131
132 err = drm_tegra_job_submit(job, NULL);
133 if (err < 0) {
134 fprintf(stderr, "failed to submit job: %s\n", strerror(-err));
135 return 1;
136 }
137
138 err = drm_tegra_job_wait(job, 250000000);
139 if (err < 0) {
140 fprintf(stderr, "failed to wait for job: %s\n", strerror(-err));
141 return 1;
142 }
143
144 drm_tegra_job_free(job);
145 drm_tegra_channel_close(channel);
146 drm_tegra_syncpoint_free(syncpt);
147 drm_tegra_close(drm);
148 close(fd);
149
150 return 0;
151 }
152