etnaviv_cmd_stream.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (C) 2014-2015 Etnaviv Project
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Christian Gmeiner <christian.gmeiner@gmail.com>
  25. */
  26. #include <assert.h>
  27. #include "etnaviv_drmif.h"
  28. #include "etnaviv_priv.h"
  29. static pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER;
  30. static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz)
  31. {
  32. if ((nr + 1) > *max) {
  33. if ((*max * 2) < (nr + 1))
  34. *max = nr + 5;
  35. else
  36. *max = *max * 2;
  37. ptr = realloc(ptr, *max * sz);
  38. }
  39. return ptr;
  40. }
  41. #define APPEND(x, name) ({ \
  42. (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
  43. (x)->nr_ ## name ++; \
  44. })
  45. static inline struct etna_cmd_stream_priv *
  46. etna_cmd_stream_priv(struct etna_cmd_stream *stream)
  47. {
  48. return (struct etna_cmd_stream_priv *)stream;
  49. }
  50. drm_public struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe,
  51. uint32_t size,
  52. void (*reset_notify)(struct etna_cmd_stream *stream, void *priv),
  53. void *priv)
  54. {
  55. struct etna_cmd_stream_priv *stream = NULL;
  56. if (size == 0) {
  57. ERROR_MSG("invalid size of 0");
  58. goto fail;
  59. }
  60. stream = calloc(1, sizeof(*stream));
  61. if (!stream) {
  62. ERROR_MSG("allocation failed");
  63. goto fail;
  64. }
  65. /* allocate even number of 32-bit words */
  66. size = ALIGN(size, 2);
  67. stream->base.buffer = malloc(size * sizeof(uint32_t));
  68. if (!stream->base.buffer) {
  69. ERROR_MSG("allocation failed");
  70. goto fail;
  71. }
  72. stream->base.size = size;
  73. stream->pipe = pipe;
  74. stream->reset_notify = reset_notify;
  75. stream->reset_notify_priv = priv;
  76. return &stream->base;
  77. fail:
  78. if (stream)
  79. etna_cmd_stream_del(&stream->base);
  80. return NULL;
  81. }
  82. drm_public void etna_cmd_stream_del(struct etna_cmd_stream *stream)
  83. {
  84. struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
  85. free(stream->buffer);
  86. free(priv->submit.relocs);
  87. free(priv->submit.pmrs);
  88. free(priv);
  89. }
  90. static void reset_buffer(struct etna_cmd_stream *stream)
  91. {
  92. struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
  93. stream->offset = 0;
  94. priv->submit.nr_bos = 0;
  95. priv->submit.nr_relocs = 0;
  96. priv->submit.nr_pmrs = 0;
  97. priv->nr_bos = 0;
  98. if (priv->reset_notify)
  99. priv->reset_notify(stream, priv->reset_notify_priv);
  100. }
  101. drm_public uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream)
  102. {
  103. return etna_cmd_stream_priv(stream)->last_timestamp;
  104. }
  105. static uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo)
  106. {
  107. struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
  108. uint32_t idx;
  109. idx = APPEND(&priv->submit, bos);
  110. idx = APPEND(priv, bos);
  111. priv->submit.bos[idx].flags = 0;
  112. priv->submit.bos[idx].handle = bo->handle;
  113. priv->bos[idx] = etna_bo_ref(bo);
  114. return idx;
  115. }
  116. /* add (if needed) bo, return idx: */
  117. static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
  118. uint32_t flags)
  119. {
  120. struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
  121. uint32_t idx;
  122. pthread_mutex_lock(&idx_lock);
  123. if (bo->current_stream == stream) {
  124. idx = bo->idx;
  125. } else {
  126. /* slow-path: */
  127. for (idx = 0; idx < priv->nr_bos; idx++)
  128. if (priv->bos[idx] == bo)
  129. break;
  130. if (idx == priv->nr_bos) {
  131. /* not found */
  132. idx = append_bo(stream, bo);
  133. }
  134. bo->current_stream = stream;
  135. bo->idx = idx;
  136. }
  137. pthread_mutex_unlock(&idx_lock);
  138. if (flags & ETNA_RELOC_READ)
  139. priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ;
  140. if (flags & ETNA_RELOC_WRITE)
  141. priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE;
  142. return idx;
  143. }
  144. static void flush(struct etna_cmd_stream *stream, int in_fence_fd,
  145. int *out_fence_fd)
  146. {
  147. struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
  148. int ret, id = priv->pipe->id;
  149. struct etna_gpu *gpu = priv->pipe->gpu;
  150. struct drm_etnaviv_gem_submit req = {
  151. .pipe = gpu->core,
  152. .exec_state = id,
  153. .bos = VOID2U64(priv->submit.bos),
  154. .nr_bos = priv->submit.nr_bos,
  155. .relocs = VOID2U64(priv->submit.relocs),
  156. .nr_relocs = priv->submit.nr_relocs,
  157. .pmrs = VOID2U64(priv->submit.pmrs),
  158. .nr_pmrs = priv->submit.nr_pmrs,
  159. .stream = VOID2U64(stream->buffer),
  160. .stream_size = stream->offset * 4, /* in bytes */
  161. };
  162. if (in_fence_fd != -1) {
  163. req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT;
  164. req.fence_fd = in_fence_fd;
  165. }
  166. if (out_fence_fd)
  167. req.flags |= ETNA_SUBMIT_FENCE_FD_OUT;
  168. ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT,
  169. &req, sizeof(req));
  170. if (ret)
  171. ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
  172. else
  173. priv->last_timestamp = req.fence;
  174. for (uint32_t i = 0; i < priv->nr_bos; i++) {
  175. struct etna_bo *bo = priv->bos[i];
  176. bo->current_stream = NULL;
  177. etna_bo_del(bo);
  178. }
  179. if (out_fence_fd)
  180. *out_fence_fd = req.fence_fd;
  181. }
  182. drm_public void etna_cmd_stream_flush(struct etna_cmd_stream *stream)
  183. {
  184. flush(stream, -1, NULL);
  185. reset_buffer(stream);
  186. }
  187. drm_public void etna_cmd_stream_flush2(struct etna_cmd_stream *stream,
  188. int in_fence_fd,
  189. int *out_fence_fd)
  190. {
  191. flush(stream, in_fence_fd, out_fence_fd);
  192. reset_buffer(stream);
  193. }
  194. drm_public void etna_cmd_stream_finish(struct etna_cmd_stream *stream)
  195. {
  196. struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
  197. flush(stream, -1, NULL);
  198. etna_pipe_wait(priv->pipe, priv->last_timestamp, 5000);
  199. reset_buffer(stream);
  200. }
  201. drm_public void etna_cmd_stream_reloc(struct etna_cmd_stream *stream,
  202. const struct etna_reloc *r)
  203. {
  204. struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
  205. struct drm_etnaviv_gem_submit_reloc *reloc;
  206. uint32_t idx = APPEND(&priv->submit, relocs);
  207. uint32_t addr = 0;
  208. reloc = &priv->submit.relocs[idx];
  209. reloc->reloc_idx = bo2idx(stream, r->bo, r->flags);
  210. reloc->reloc_offset = r->offset;
  211. reloc->submit_offset = stream->offset * 4; /* in bytes */
  212. reloc->flags = 0;
  213. etna_cmd_stream_emit(stream, addr);
  214. }
  215. drm_public void etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p)
  216. {
  217. struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
  218. struct drm_etnaviv_gem_submit_pmr *pmr;
  219. uint32_t idx = APPEND(&priv->submit, pmrs);
  220. pmr = &priv->submit.pmrs[idx];
  221. pmr->flags = p->flags;
  222. pmr->sequence = p->sequence;
  223. pmr->read_offset = p->offset;
  224. pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE);
  225. pmr->domain = p->signal->domain->id;
  226. pmr->signal = p->signal->signal;
  227. }