pushbuf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "util_math.h"
  31. #include "private.h"
  32. #define HOST1X_OPCODE_NONINCR(offset, count) \
  33. ((0x2 << 28) | (((offset) & 0xfff) << 16) | ((count) & 0xffff))
  34. static inline unsigned int
  35. drm_tegra_pushbuf_get_offset(struct drm_tegra_pushbuf *pushbuf, uint32_t *ptr)
  36. {
  37. return ptr - pushbuf->start;
  38. }
  39. void drm_tegra_pushbuf_free(struct drm_tegra_pushbuf *pushbuf)
  40. {
  41. if (pushbuf->start)
  42. free(pushbuf->start);
  43. free(pushbuf);
  44. }
  45. /**
  46. * drm_tegra_pushbuf_begin() - prepare push buffer for a series of pushes
  47. * @pushbuf: push buffer
  48. * @words: maximum number of words in series of pushes to follow
  49. */
  50. drm_public int
  51. drm_tegra_pushbuf_begin(struct drm_tegra_pushbuf *pushbuf,
  52. unsigned int words, uint32_t **ptrp)
  53. {
  54. struct drm_tegra_job *job = pushbuf->job;
  55. unsigned long offset;
  56. size_t size;
  57. void *ptr;
  58. if (pushbuf->ptr + words >= pushbuf->end) {
  59. words = pushbuf->end - pushbuf->start + words;
  60. size = ALIGN(words * 4, job->page_size);
  61. offset = pushbuf->ptr - pushbuf->start;
  62. ptr = realloc(pushbuf->start, size);
  63. if (!ptr)
  64. return -ENOMEM;
  65. pushbuf->start = ptr;
  66. pushbuf->end = pushbuf->start + size / 4;
  67. pushbuf->ptr = pushbuf->start + offset;
  68. }
  69. if (ptrp)
  70. *ptrp = pushbuf->ptr;
  71. return 0;
  72. }
  73. drm_public int
  74. drm_tegra_pushbuf_end(struct drm_tegra_pushbuf *pushbuf, uint32_t *ptr)
  75. {
  76. struct drm_tegra_submit_cmd *command;
  77. command = drm_tegra_job_add_command(pushbuf->job,
  78. DRM_TEGRA_SUBMIT_CMD_GATHER_UPTR,
  79. 0);
  80. if (!command)
  81. return -ENOMEM;
  82. command->gather_uptr.words = ptr - pushbuf->start;
  83. pushbuf->ptr = ptr;
  84. return 0;
  85. }
  86. drm_public int
  87. drm_tegra_pushbuf_wait(struct drm_tegra_pushbuf *pushbuf,
  88. struct drm_tegra_syncpoint *syncpt,
  89. uint32_t value)
  90. {
  91. struct drm_tegra_submit_cmd *command;
  92. command = drm_tegra_job_add_command(pushbuf->job,
  93. DRM_TEGRA_SUBMIT_CMD_WAIT_SYNCPT,
  94. 0);
  95. if (!command)
  96. return -ENOMEM;
  97. command->wait_syncpt.id = syncpt->id;
  98. command->wait_syncpt.value = value;
  99. return 0;
  100. }
  101. drm_public int
  102. drm_tegra_pushbuf_relocate(struct drm_tegra_pushbuf *pushbuf, uint32_t **ptrp,
  103. struct drm_tegra_mapping *target,
  104. unsigned long offset, unsigned int shift,
  105. uint32_t flags)
  106. {
  107. struct drm_tegra_submit_buf *buffers, *buffer;
  108. struct drm_tegra_job *job = pushbuf->job;
  109. size_t size;
  110. size = (job->num_buffers + 1) * sizeof(*buffer);
  111. buffers = realloc(job->buffers, size);
  112. if (!buffers)
  113. return -ENOMEM;
  114. buffer = &buffers[job->num_buffers];
  115. memset(buffer, 0, sizeof(*buffer));
  116. buffer->mapping = target->id;
  117. buffer->flags = flags;
  118. buffer->reloc.target_offset = offset;
  119. buffer->reloc.gather_offset_words = drm_tegra_pushbuf_get_offset(pushbuf,
  120. *ptrp);
  121. buffer->reloc.shift = shift;
  122. *(*ptrp)++ = 0xdeadbeef;
  123. job->buffers = buffers;
  124. job->num_buffers++;
  125. return 0;
  126. }
  127. drm_public int
  128. drm_tegra_pushbuf_sync(struct drm_tegra_pushbuf *pushbuf,
  129. struct drm_tegra_syncpoint *syncpt,
  130. unsigned int count)
  131. {
  132. struct drm_tegra_job *job = pushbuf->job;
  133. job->syncpt.increments += count;
  134. job->syncpt.id = syncpt->id;
  135. return 0;
  136. }
  137. drm_public int
  138. drm_tegra_pushbuf_sync_cond(struct drm_tegra_pushbuf *pushbuf, uint32_t **ptrp,
  139. struct drm_tegra_syncpoint *syncpt,
  140. enum drm_tegra_sync_cond cond)
  141. {
  142. struct drm_tegra_channel *channel = pushbuf->job->channel;
  143. if (cond >= DRM_TEGRA_SYNC_COND_MAX)
  144. return -EINVAL;
  145. *(*ptrp)++ = HOST1X_OPCODE_NONINCR(0x0, 0x1);
  146. *(*ptrp)++ = cond << channel->cond_shift | syncpt->id;
  147. return drm_tegra_pushbuf_sync(pushbuf, syncpt, 1);
  148. }