syncpt-wait.c 4.3 KB

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