job.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright © 2012, 2013 Thierry Reding
  3. * Copyright © 2013 Erik Faye-Lund
  4. * Copyright © 2014 NVIDIA Corporation
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. # include "config.h"
  26. #endif
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <unistd.h>
  32. #include <sys/ioctl.h>
  33. #include <poll.h>
  34. #include "private.h"
  35. struct drm_tegra_submit_cmd *
  36. drm_tegra_job_add_command(struct drm_tegra_job *job, uint32_t type,
  37. uint32_t flags)
  38. {
  39. struct drm_tegra_submit_cmd *commands, *command;
  40. size_t size;
  41. size = (job->num_commands + 1) * sizeof(*commands);
  42. commands = realloc(job->commands, size);
  43. if (!commands)
  44. return NULL;
  45. command = &commands[job->num_commands];
  46. memset(command, 0, sizeof(*command));
  47. command->type = type;
  48. command->flags = flags;
  49. job->commands = commands;
  50. job->num_commands++;
  51. return command;
  52. }
  53. drm_public int
  54. drm_tegra_job_new(struct drm_tegra_channel *channel,
  55. struct drm_tegra_job **jobp)
  56. {
  57. struct drm_tegra_job *job;
  58. job = calloc(1, sizeof(*job));
  59. if (!job)
  60. return -ENOMEM;
  61. job->page_size = sysconf(_SC_PAGESIZE);
  62. job->channel = channel;
  63. *jobp = job;
  64. return 0;
  65. }
  66. drm_public int drm_tegra_job_free(struct drm_tegra_job *job)
  67. {
  68. if (!job)
  69. return -EINVAL;
  70. if (job->pushbuf)
  71. drm_tegra_pushbuf_free(job->pushbuf);
  72. if (job->commands)
  73. free(job->commands);
  74. if (job->buffers)
  75. free(job->buffers);
  76. free(job);
  77. return 0;
  78. }
  79. drm_public int
  80. drm_tegra_job_get_pushbuf(struct drm_tegra_job *job,
  81. struct drm_tegra_pushbuf **pushbufp)
  82. {
  83. struct drm_tegra_pushbuf *pushbuf;
  84. if (!job->pushbuf) {
  85. pushbuf = calloc(1, sizeof(*pushbuf));
  86. if (!pushbuf)
  87. return -ENOMEM;
  88. pushbuf->job = job;
  89. pushbuf->start = calloc(1, job->page_size);
  90. if (!pushbuf->start) {
  91. free(pushbuf);
  92. return -ENOMEM;
  93. }
  94. pushbuf->end = pushbuf->start + job->page_size / 4;
  95. pushbuf->ptr = pushbuf->start;
  96. job->pushbuf = pushbuf;
  97. }
  98. *pushbufp = job->pushbuf;
  99. return 0;
  100. }
  101. drm_public int
  102. drm_tegra_job_submit(struct drm_tegra_job *job, struct drm_tegra_fence *fence)
  103. {
  104. struct drm_tegra_channel *channel = job->channel;
  105. struct drm_tegra *drm = channel->drm;
  106. struct drm_tegra_channel_submit args;
  107. int err;
  108. memset(&args, 0, sizeof(args));
  109. args.context = channel->context;
  110. args.num_bufs = job->num_buffers;
  111. args.num_cmds = job->num_commands;
  112. args.gather_data_words = job->pushbuf->ptr - job->pushbuf->start;
  113. args.syncpt.id = job->syncpt.id;
  114. args.syncpt.increments = job->syncpt.increments;
  115. args.bufs_ptr = (uintptr_t)job->buffers;
  116. args.cmds_ptr = (uintptr_t)job->commands;
  117. args.gather_data_ptr = (uintptr_t)job->pushbuf->start;
  118. err = ioctl(drm->fd, DRM_IOCTL_TEGRA_CHANNEL_SUBMIT, &args);
  119. if (err < 0)
  120. return -errno;
  121. job->syncpt.fence = args.syncpt.value;
  122. if (fence) {
  123. fence->drm = drm;
  124. fence->syncpt = job->syncpt.id;
  125. fence->value = job->syncpt.fence;
  126. }
  127. return 0;
  128. }
  129. drm_public int
  130. drm_tegra_job_wait(struct drm_tegra_job *job, unsigned long timeout)
  131. {
  132. struct drm_tegra_channel *channel = job->channel;
  133. struct drm_tegra *drm = channel->drm;
  134. struct drm_tegra_syncpoint_wait args;
  135. struct timespec ts;
  136. int err;
  137. clock_gettime(CLOCK_MONOTONIC, &ts);
  138. memset(&args, 0, sizeof(args));
  139. args.timeout_ns = ts.tv_sec * 1000000000 + ts.tv_nsec + timeout;
  140. args.id = job->syncpt.id;
  141. args.threshold = job->syncpt.fence;
  142. err = ioctl(drm->fd, DRM_IOCTL_TEGRA_SYNCPOINT_WAIT, &args);
  143. if (err < 0)
  144. return -errno;
  145. return 0;
  146. }