1d722e3fbSopenharmony_ci/* 2d722e3fbSopenharmony_ci * Copyright © 2018 NVIDIA Corporation 3d722e3fbSopenharmony_ci * 4d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation 7d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10d722e3fbSopenharmony_ci * 11d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included in 12d722e3fbSopenharmony_ci * all copies or substantial portions of the Software. 13d722e3fbSopenharmony_ci * 14d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17d722e3fbSopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18d722e3fbSopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19d722e3fbSopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20d722e3fbSopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 21d722e3fbSopenharmony_ci */ 22d722e3fbSopenharmony_ci 23d722e3fbSopenharmony_ci#include <errno.h> 24d722e3fbSopenharmony_ci#include <fcntl.h> 25d722e3fbSopenharmony_ci#include <stdio.h> 26d722e3fbSopenharmony_ci#include <string.h> 27d722e3fbSopenharmony_ci#include <unistd.h> 28d722e3fbSopenharmony_ci 29d722e3fbSopenharmony_ci#include "tegra.h" 30d722e3fbSopenharmony_ci 31d722e3fbSopenharmony_ci#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 32d722e3fbSopenharmony_ci 33d722e3fbSopenharmony_cistatic int channel_open(struct drm_tegra *drm, 34d722e3fbSopenharmony_ci struct drm_tegra_channel **channel) 35d722e3fbSopenharmony_ci{ 36d722e3fbSopenharmony_ci static const struct { 37d722e3fbSopenharmony_ci enum drm_tegra_class class; 38d722e3fbSopenharmony_ci const char *name; 39d722e3fbSopenharmony_ci } classes[] = { 40d722e3fbSopenharmony_ci { DRM_TEGRA_VIC, "VIC" }, 41d722e3fbSopenharmony_ci { DRM_TEGRA_GR2D, "GR2D" }, 42d722e3fbSopenharmony_ci }; 43d722e3fbSopenharmony_ci unsigned int i; 44d722e3fbSopenharmony_ci int err; 45d722e3fbSopenharmony_ci 46d722e3fbSopenharmony_ci for (i = 0; i < ARRAY_SIZE(classes); i++) { 47d722e3fbSopenharmony_ci err = drm_tegra_channel_open(drm, classes[i].class, channel); 48d722e3fbSopenharmony_ci if (err < 0) { 49d722e3fbSopenharmony_ci fprintf(stderr, "failed to open channel to %s: %s\n", 50d722e3fbSopenharmony_ci classes[i].name, strerror(-err)); 51d722e3fbSopenharmony_ci continue; 52d722e3fbSopenharmony_ci } 53d722e3fbSopenharmony_ci 54d722e3fbSopenharmony_ci break; 55d722e3fbSopenharmony_ci } 56d722e3fbSopenharmony_ci 57d722e3fbSopenharmony_ci return err; 58d722e3fbSopenharmony_ci} 59d722e3fbSopenharmony_ci 60d722e3fbSopenharmony_ciint main(int argc, char *argv[]) 61d722e3fbSopenharmony_ci{ 62d722e3fbSopenharmony_ci const char *device = "/dev/dri/renderD128"; 63d722e3fbSopenharmony_ci struct drm_tegra_syncpoint *syncpt; 64d722e3fbSopenharmony_ci struct drm_tegra_channel *channel; 65d722e3fbSopenharmony_ci struct drm_tegra_pushbuf *pushbuf; 66d722e3fbSopenharmony_ci struct drm_tegra_job *job; 67d722e3fbSopenharmony_ci struct drm_tegra *drm; 68d722e3fbSopenharmony_ci uint32_t *ptr; 69d722e3fbSopenharmony_ci int fd, err; 70d722e3fbSopenharmony_ci 71d722e3fbSopenharmony_ci if (argc > 1) 72d722e3fbSopenharmony_ci device = argv[1]; 73d722e3fbSopenharmony_ci 74d722e3fbSopenharmony_ci fd = open(device, O_RDWR); 75d722e3fbSopenharmony_ci if (fd < 0) { 76d722e3fbSopenharmony_ci fprintf(stderr, "open() failed: %s\n", strerror(errno)); 77d722e3fbSopenharmony_ci return 1; 78d722e3fbSopenharmony_ci } 79d722e3fbSopenharmony_ci 80d722e3fbSopenharmony_ci err = drm_tegra_new(fd, &drm); 81d722e3fbSopenharmony_ci if (err < 0) { 82d722e3fbSopenharmony_ci fprintf(stderr, "failed to open Tegra device: %s\n", strerror(-err)); 83d722e3fbSopenharmony_ci close(fd); 84d722e3fbSopenharmony_ci return 1; 85d722e3fbSopenharmony_ci } 86d722e3fbSopenharmony_ci 87d722e3fbSopenharmony_ci err = drm_tegra_syncpoint_new(drm, &syncpt); 88d722e3fbSopenharmony_ci if (err < 0) { 89d722e3fbSopenharmony_ci fprintf(stderr, "failed to allocate syncpoint: %s\n", strerror(-err)); 90d722e3fbSopenharmony_ci drm_tegra_close(drm); 91d722e3fbSopenharmony_ci close(fd); 92d722e3fbSopenharmony_ci return 1; 93d722e3fbSopenharmony_ci } 94d722e3fbSopenharmony_ci 95d722e3fbSopenharmony_ci err = channel_open(drm, &channel); 96d722e3fbSopenharmony_ci if (err < 0) { 97d722e3fbSopenharmony_ci fprintf(stderr, "failed to open channel: %s\n", strerror(-err)); 98d722e3fbSopenharmony_ci return 1; 99d722e3fbSopenharmony_ci } 100d722e3fbSopenharmony_ci 101d722e3fbSopenharmony_ci err = drm_tegra_job_new(channel, &job); 102d722e3fbSopenharmony_ci if (err < 0) { 103d722e3fbSopenharmony_ci fprintf(stderr, "failed to create job: %s\n", strerror(-err)); 104d722e3fbSopenharmony_ci return 1; 105d722e3fbSopenharmony_ci } 106d722e3fbSopenharmony_ci 107d722e3fbSopenharmony_ci err = drm_tegra_job_get_pushbuf(job, &pushbuf); 108d722e3fbSopenharmony_ci if (err < 0) { 109d722e3fbSopenharmony_ci fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err)); 110d722e3fbSopenharmony_ci return 1; 111d722e3fbSopenharmony_ci } 112d722e3fbSopenharmony_ci 113d722e3fbSopenharmony_ci err = drm_tegra_pushbuf_begin(pushbuf, 4, &ptr); 114d722e3fbSopenharmony_ci if (err < 0) { 115d722e3fbSopenharmony_ci fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err)); 116d722e3fbSopenharmony_ci return 1; 117d722e3fbSopenharmony_ci } 118d722e3fbSopenharmony_ci 119d722e3fbSopenharmony_ci err = drm_tegra_pushbuf_sync_cond(pushbuf, &ptr, syncpt, 120d722e3fbSopenharmony_ci DRM_TEGRA_SYNC_COND_IMMEDIATE); 121d722e3fbSopenharmony_ci if (err < 0) { 122d722e3fbSopenharmony_ci fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err)); 123d722e3fbSopenharmony_ci return 1; 124d722e3fbSopenharmony_ci } 125d722e3fbSopenharmony_ci 126d722e3fbSopenharmony_ci err = drm_tegra_pushbuf_end(pushbuf, ptr); 127d722e3fbSopenharmony_ci if (err < 0) { 128d722e3fbSopenharmony_ci fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err)); 129d722e3fbSopenharmony_ci return 1; 130d722e3fbSopenharmony_ci } 131d722e3fbSopenharmony_ci 132d722e3fbSopenharmony_ci err = drm_tegra_job_submit(job, NULL); 133d722e3fbSopenharmony_ci if (err < 0) { 134d722e3fbSopenharmony_ci fprintf(stderr, "failed to submit job: %s\n", strerror(-err)); 135d722e3fbSopenharmony_ci return 1; 136d722e3fbSopenharmony_ci } 137d722e3fbSopenharmony_ci 138d722e3fbSopenharmony_ci err = drm_tegra_job_wait(job, 250000000); 139d722e3fbSopenharmony_ci if (err < 0) { 140d722e3fbSopenharmony_ci fprintf(stderr, "failed to wait for job: %s\n", strerror(-err)); 141d722e3fbSopenharmony_ci return 1; 142d722e3fbSopenharmony_ci } 143d722e3fbSopenharmony_ci 144d722e3fbSopenharmony_ci drm_tegra_job_free(job); 145d722e3fbSopenharmony_ci drm_tegra_channel_close(channel); 146d722e3fbSopenharmony_ci drm_tegra_syncpoint_free(syncpt); 147d722e3fbSopenharmony_ci drm_tegra_close(drm); 148d722e3fbSopenharmony_ci close(fd); 149d722e3fbSopenharmony_ci 150d722e3fbSopenharmony_ci return 0; 151d722e3fbSopenharmony_ci} 152