10ed5401bSmrg/*
20ed5401bSmrg * Copyright © 2018 NVIDIA Corporation
30ed5401bSmrg *
40ed5401bSmrg * Permission is hereby granted, free of charge, to any person obtaining a
50ed5401bSmrg * copy of this software and associated documentation files (the "Software"),
60ed5401bSmrg * to deal in the Software without restriction, including without limitation
70ed5401bSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
80ed5401bSmrg * and/or sell copies of the Software, and to permit persons to whom the
90ed5401bSmrg * Software is furnished to do so, subject to the following conditions:
100ed5401bSmrg *
110ed5401bSmrg * The above copyright notice and this permission notice shall be included in
120ed5401bSmrg * all copies or substantial portions of the Software.
130ed5401bSmrg *
140ed5401bSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
150ed5401bSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
160ed5401bSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
170ed5401bSmrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
180ed5401bSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
190ed5401bSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
200ed5401bSmrg * OTHER DEALINGS IN THE SOFTWARE.
210ed5401bSmrg */
220ed5401bSmrg
230ed5401bSmrg#include <errno.h>
240ed5401bSmrg#include <fcntl.h>
250ed5401bSmrg#include <stdio.h>
260ed5401bSmrg#include <string.h>
270ed5401bSmrg#include <unistd.h>
280ed5401bSmrg
290ed5401bSmrg#include "tegra.h"
300ed5401bSmrg
310ed5401bSmrg#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
320ed5401bSmrg
330ed5401bSmrgstatic int channel_open(struct drm_tegra *drm,
340ed5401bSmrg                        struct drm_tegra_channel **channel)
350ed5401bSmrg{
360ed5401bSmrg    static const struct {
370ed5401bSmrg        enum drm_tegra_class class;
380ed5401bSmrg        const char *name;
390ed5401bSmrg    } classes[] = {
400ed5401bSmrg        { DRM_TEGRA_VIC,  "VIC"  },
410ed5401bSmrg        { DRM_TEGRA_GR2D, "GR2D" },
420ed5401bSmrg    };
430ed5401bSmrg    unsigned int i;
440ed5401bSmrg    int err;
450ed5401bSmrg
460ed5401bSmrg    for (i = 0; i < ARRAY_SIZE(classes); i++) {
470ed5401bSmrg        err = drm_tegra_channel_open(drm, classes[i].class, channel);
480ed5401bSmrg        if (err < 0) {
490ed5401bSmrg            fprintf(stderr, "failed to open channel to %s: %s\n",
500ed5401bSmrg                    classes[i].name, strerror(-err));
510ed5401bSmrg            continue;
520ed5401bSmrg        }
530ed5401bSmrg
540ed5401bSmrg        break;
550ed5401bSmrg    }
560ed5401bSmrg
570ed5401bSmrg    return err;
580ed5401bSmrg}
590ed5401bSmrg
600ed5401bSmrgint main(int argc, char *argv[])
610ed5401bSmrg{
620ed5401bSmrg    const char *device = "/dev/dri/renderD128";
630ed5401bSmrg    struct drm_tegra_syncpoint *syncpt;
640ed5401bSmrg    struct drm_tegra_channel *channel;
650ed5401bSmrg    struct drm_tegra_pushbuf *pushbuf;
660ed5401bSmrg    struct drm_tegra_job *job;
670ed5401bSmrg    struct drm_tegra *drm;
680ed5401bSmrg    uint32_t *ptr;
690ed5401bSmrg    int fd, err;
700ed5401bSmrg
710ed5401bSmrg    if (argc > 1)
720ed5401bSmrg        device = argv[1];
730ed5401bSmrg
740ed5401bSmrg    fd = open(device, O_RDWR);
750ed5401bSmrg    if (fd < 0) {
760ed5401bSmrg        fprintf(stderr, "open() failed: %s\n", strerror(errno));
770ed5401bSmrg        return 1;
780ed5401bSmrg    }
790ed5401bSmrg
800ed5401bSmrg    err = drm_tegra_new(fd, &drm);
810ed5401bSmrg    if (err < 0) {
820ed5401bSmrg        fprintf(stderr, "failed to open Tegra device: %s\n", strerror(-err));
830ed5401bSmrg        close(fd);
840ed5401bSmrg        return 1;
850ed5401bSmrg    }
860ed5401bSmrg
870ed5401bSmrg    err = drm_tegra_syncpoint_new(drm, &syncpt);
880ed5401bSmrg    if (err < 0) {
890ed5401bSmrg        fprintf(stderr, "failed to allocate syncpoint: %s\n", strerror(-err));
900ed5401bSmrg        drm_tegra_close(drm);
910ed5401bSmrg        close(fd);
920ed5401bSmrg        return 1;
930ed5401bSmrg    }
940ed5401bSmrg
950ed5401bSmrg    err = channel_open(drm, &channel);
960ed5401bSmrg    if (err < 0) {
970ed5401bSmrg        fprintf(stderr, "failed to open channel: %s\n", strerror(-err));
980ed5401bSmrg        return 1;
990ed5401bSmrg    }
1000ed5401bSmrg
1010ed5401bSmrg    err = drm_tegra_job_new(channel, &job);
1020ed5401bSmrg    if (err < 0) {
1030ed5401bSmrg        fprintf(stderr, "failed to create job: %s\n", strerror(-err));
1040ed5401bSmrg        return 1;
1050ed5401bSmrg    }
1060ed5401bSmrg
1070ed5401bSmrg    err = drm_tegra_job_get_pushbuf(job, &pushbuf);
1080ed5401bSmrg    if (err < 0) {
1090ed5401bSmrg        fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err));
1100ed5401bSmrg        return 1;
1110ed5401bSmrg    }
1120ed5401bSmrg
1130ed5401bSmrg    err = drm_tegra_pushbuf_begin(pushbuf, 4, &ptr);
1140ed5401bSmrg    if (err < 0) {
1150ed5401bSmrg        fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err));
1160ed5401bSmrg        return 1;
1170ed5401bSmrg    }
1180ed5401bSmrg
1190ed5401bSmrg    err = drm_tegra_pushbuf_sync_cond(pushbuf, &ptr, syncpt,
1200ed5401bSmrg                                      DRM_TEGRA_SYNC_COND_IMMEDIATE);
1210ed5401bSmrg    if (err < 0) {
1220ed5401bSmrg        fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err));
1230ed5401bSmrg        return 1;
1240ed5401bSmrg    }
1250ed5401bSmrg
1260ed5401bSmrg    err = drm_tegra_pushbuf_end(pushbuf, ptr);
1270ed5401bSmrg    if (err < 0) {
1280ed5401bSmrg        fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err));
1290ed5401bSmrg        return 1;
1300ed5401bSmrg    }
1310ed5401bSmrg
1320ed5401bSmrg    err = drm_tegra_job_submit(job, NULL);
1330ed5401bSmrg    if (err < 0) {
1340ed5401bSmrg        fprintf(stderr, "failed to submit job: %s\n", strerror(-err));
1350ed5401bSmrg        return 1;
1360ed5401bSmrg    }
1370ed5401bSmrg
1380ed5401bSmrg    err = drm_tegra_job_wait(job, 250000000);
1390ed5401bSmrg    if (err < 0) {
1400ed5401bSmrg        fprintf(stderr, "failed to wait for job: %s\n", strerror(-err));
1410ed5401bSmrg        return 1;
1420ed5401bSmrg    }
1430ed5401bSmrg
1440ed5401bSmrg    drm_tegra_job_free(job);
1450ed5401bSmrg    drm_tegra_channel_close(channel);
1460ed5401bSmrg    drm_tegra_syncpoint_free(syncpt);
1470ed5401bSmrg    drm_tegra_close(drm);
1480ed5401bSmrg    close(fd);
1490ed5401bSmrg
1500ed5401bSmrg    return 0;
1510ed5401bSmrg}
152