etnaviv_buffer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2014 Etnaviv Project
  3. * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "etnaviv_gpu.h"
  18. #include "etnaviv_gem.h"
  19. #include "etnaviv_mmu.h"
  20. #include "common.xml.h"
  21. #include "state.xml.h"
  22. #include "state_3d.xml.h"
  23. #include "cmdstream.xml.h"
  24. /*
  25. * Command Buffer helper:
  26. */
  27. static inline void OUT(struct etnaviv_cmdbuf *buffer, u32 data)
  28. {
  29. u32 *vaddr = (u32 *)buffer->vaddr;
  30. BUG_ON(buffer->user_size >= buffer->size);
  31. vaddr[buffer->user_size / 4] = data;
  32. buffer->user_size += 4;
  33. }
  34. static inline void CMD_LOAD_STATE(struct etnaviv_cmdbuf *buffer,
  35. u32 reg, u32 value)
  36. {
  37. u32 index = reg >> VIV_FE_LOAD_STATE_HEADER_OFFSET__SHR;
  38. buffer->user_size = ALIGN(buffer->user_size, 8);
  39. /* write a register via cmd stream */
  40. OUT(buffer, VIV_FE_LOAD_STATE_HEADER_OP_LOAD_STATE |
  41. VIV_FE_LOAD_STATE_HEADER_COUNT(1) |
  42. VIV_FE_LOAD_STATE_HEADER_OFFSET(index));
  43. OUT(buffer, value);
  44. }
  45. static inline void CMD_END(struct etnaviv_cmdbuf *buffer)
  46. {
  47. buffer->user_size = ALIGN(buffer->user_size, 8);
  48. OUT(buffer, VIV_FE_END_HEADER_OP_END);
  49. }
  50. static inline void CMD_WAIT(struct etnaviv_cmdbuf *buffer)
  51. {
  52. buffer->user_size = ALIGN(buffer->user_size, 8);
  53. OUT(buffer, VIV_FE_WAIT_HEADER_OP_WAIT | 200);
  54. }
  55. static inline void CMD_LINK(struct etnaviv_cmdbuf *buffer,
  56. u16 prefetch, u32 address)
  57. {
  58. buffer->user_size = ALIGN(buffer->user_size, 8);
  59. OUT(buffer, VIV_FE_LINK_HEADER_OP_LINK |
  60. VIV_FE_LINK_HEADER_PREFETCH(prefetch));
  61. OUT(buffer, address);
  62. }
  63. static inline void CMD_STALL(struct etnaviv_cmdbuf *buffer,
  64. u32 from, u32 to)
  65. {
  66. buffer->user_size = ALIGN(buffer->user_size, 8);
  67. OUT(buffer, VIV_FE_STALL_HEADER_OP_STALL);
  68. OUT(buffer, VIV_FE_STALL_TOKEN_FROM(from) | VIV_FE_STALL_TOKEN_TO(to));
  69. }
  70. static inline void CMD_SEM(struct etnaviv_cmdbuf *buffer, u32 from, u32 to)
  71. {
  72. CMD_LOAD_STATE(buffer, VIVS_GL_SEMAPHORE_TOKEN,
  73. VIVS_GL_SEMAPHORE_TOKEN_FROM(from) |
  74. VIVS_GL_SEMAPHORE_TOKEN_TO(to));
  75. }
  76. static void etnaviv_cmd_select_pipe(struct etnaviv_gpu *gpu,
  77. struct etnaviv_cmdbuf *buffer, u8 pipe)
  78. {
  79. u32 flush = 0;
  80. /*
  81. * This assumes that if we're switching to 2D, we're switching
  82. * away from 3D, and vice versa. Hence, if we're switching to
  83. * the 2D core, we need to flush the 3D depth and color caches,
  84. * otherwise we need to flush the 2D pixel engine cache.
  85. */
  86. if (gpu->exec_state == ETNA_PIPE_2D)
  87. flush = VIVS_GL_FLUSH_CACHE_PE2D;
  88. else if (gpu->exec_state == ETNA_PIPE_3D)
  89. flush = VIVS_GL_FLUSH_CACHE_DEPTH | VIVS_GL_FLUSH_CACHE_COLOR;
  90. CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE, flush);
  91. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  92. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  93. CMD_LOAD_STATE(buffer, VIVS_GL_PIPE_SELECT,
  94. VIVS_GL_PIPE_SELECT_PIPE(pipe));
  95. }
  96. static u32 gpu_va(struct etnaviv_gpu *gpu, struct etnaviv_cmdbuf *buf)
  97. {
  98. return buf->paddr - gpu->memory_base;
  99. }
  100. static void etnaviv_buffer_dump(struct etnaviv_gpu *gpu,
  101. struct etnaviv_cmdbuf *buf, u32 off, u32 len)
  102. {
  103. u32 size = buf->size;
  104. u32 *ptr = buf->vaddr + off;
  105. dev_info(gpu->dev, "virt %p phys 0x%08x free 0x%08x\n",
  106. ptr, gpu_va(gpu, buf) + off, size - len * 4 - off);
  107. print_hex_dump(KERN_INFO, "cmd ", DUMP_PREFIX_OFFSET, 16, 4,
  108. ptr, len * 4, 0);
  109. }
  110. /*
  111. * Safely replace the WAIT of a waitlink with a new command and argument.
  112. * The GPU may be executing this WAIT while we're modifying it, so we have
  113. * to write it in a specific order to avoid the GPU branching to somewhere
  114. * else. 'wl_offset' is the offset to the first byte of the WAIT command.
  115. */
  116. static void etnaviv_buffer_replace_wait(struct etnaviv_cmdbuf *buffer,
  117. unsigned int wl_offset, u32 cmd, u32 arg)
  118. {
  119. u32 *lw = buffer->vaddr + wl_offset;
  120. lw[1] = arg;
  121. mb();
  122. lw[0] = cmd;
  123. mb();
  124. }
  125. /*
  126. * Ensure that there is space in the command buffer to contiguously write
  127. * 'cmd_dwords' 64-bit words into the buffer, wrapping if necessary.
  128. */
  129. static u32 etnaviv_buffer_reserve(struct etnaviv_gpu *gpu,
  130. struct etnaviv_cmdbuf *buffer, unsigned int cmd_dwords)
  131. {
  132. if (buffer->user_size + cmd_dwords * sizeof(u64) > buffer->size)
  133. buffer->user_size = 0;
  134. return gpu_va(gpu, buffer) + buffer->user_size;
  135. }
  136. u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu)
  137. {
  138. struct etnaviv_cmdbuf *buffer = gpu->buffer;
  139. /* initialize buffer */
  140. buffer->user_size = 0;
  141. CMD_WAIT(buffer);
  142. CMD_LINK(buffer, 2, gpu_va(gpu, buffer) + buffer->user_size - 4);
  143. return buffer->user_size / 8;
  144. }
  145. void etnaviv_buffer_end(struct etnaviv_gpu *gpu)
  146. {
  147. struct etnaviv_cmdbuf *buffer = gpu->buffer;
  148. unsigned int waitlink_offset = buffer->user_size - 16;
  149. u32 link_target, flush = 0;
  150. if (gpu->exec_state == ETNA_PIPE_2D)
  151. flush = VIVS_GL_FLUSH_CACHE_PE2D;
  152. else if (gpu->exec_state == ETNA_PIPE_3D)
  153. flush = VIVS_GL_FLUSH_CACHE_DEPTH |
  154. VIVS_GL_FLUSH_CACHE_COLOR |
  155. VIVS_GL_FLUSH_CACHE_TEXTURE |
  156. VIVS_GL_FLUSH_CACHE_TEXTUREVS |
  157. VIVS_GL_FLUSH_CACHE_SHADER_L2;
  158. if (flush) {
  159. unsigned int dwords = 7;
  160. link_target = etnaviv_buffer_reserve(gpu, buffer, dwords);
  161. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  162. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  163. CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE, flush);
  164. if (gpu->exec_state == ETNA_PIPE_3D)
  165. CMD_LOAD_STATE(buffer, VIVS_TS_FLUSH_CACHE,
  166. VIVS_TS_FLUSH_CACHE_FLUSH);
  167. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  168. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  169. CMD_END(buffer);
  170. etnaviv_buffer_replace_wait(buffer, waitlink_offset,
  171. VIV_FE_LINK_HEADER_OP_LINK |
  172. VIV_FE_LINK_HEADER_PREFETCH(dwords),
  173. link_target);
  174. } else {
  175. /* Replace the last link-wait with an "END" command */
  176. etnaviv_buffer_replace_wait(buffer, waitlink_offset,
  177. VIV_FE_END_HEADER_OP_END, 0);
  178. }
  179. }
  180. /* Append a command buffer to the ring buffer. */
  181. void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, unsigned int event,
  182. struct etnaviv_cmdbuf *cmdbuf)
  183. {
  184. struct etnaviv_cmdbuf *buffer = gpu->buffer;
  185. unsigned int waitlink_offset = buffer->user_size - 16;
  186. u32 return_target, return_dwords;
  187. u32 link_target, link_dwords;
  188. if (drm_debug & DRM_UT_DRIVER)
  189. etnaviv_buffer_dump(gpu, buffer, 0, 0x50);
  190. link_target = gpu_va(gpu, cmdbuf);
  191. link_dwords = cmdbuf->size / 8;
  192. /*
  193. * If we need maintanence prior to submitting this buffer, we will
  194. * need to append a mmu flush load state, followed by a new
  195. * link to this buffer - a total of four additional words.
  196. */
  197. if (gpu->mmu->need_flush || gpu->switch_context) {
  198. u32 target, extra_dwords;
  199. /* link command */
  200. extra_dwords = 1;
  201. /* flush command */
  202. if (gpu->mmu->need_flush)
  203. extra_dwords += 1;
  204. /* pipe switch commands */
  205. if (gpu->switch_context)
  206. extra_dwords += 4;
  207. target = etnaviv_buffer_reserve(gpu, buffer, extra_dwords);
  208. if (gpu->mmu->need_flush) {
  209. /* Add the MMU flush */
  210. CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_MMU,
  211. VIVS_GL_FLUSH_MMU_FLUSH_FEMMU |
  212. VIVS_GL_FLUSH_MMU_FLUSH_UNK1 |
  213. VIVS_GL_FLUSH_MMU_FLUSH_UNK2 |
  214. VIVS_GL_FLUSH_MMU_FLUSH_PEMMU |
  215. VIVS_GL_FLUSH_MMU_FLUSH_UNK4);
  216. gpu->mmu->need_flush = false;
  217. }
  218. if (gpu->switch_context) {
  219. etnaviv_cmd_select_pipe(gpu, buffer, cmdbuf->exec_state);
  220. gpu->exec_state = cmdbuf->exec_state;
  221. gpu->switch_context = false;
  222. }
  223. /* And the link to the submitted buffer */
  224. CMD_LINK(buffer, link_dwords, link_target);
  225. /* Update the link target to point to above instructions */
  226. link_target = target;
  227. link_dwords = extra_dwords;
  228. }
  229. /*
  230. * Append a LINK to the submitted command buffer to return to
  231. * the ring buffer. return_target is the ring target address.
  232. * We need three dwords: event, wait, link.
  233. */
  234. return_dwords = 3;
  235. return_target = etnaviv_buffer_reserve(gpu, buffer, return_dwords);
  236. CMD_LINK(cmdbuf, return_dwords, return_target);
  237. /*
  238. * Append event, wait and link pointing back to the wait
  239. * command to the ring buffer.
  240. */
  241. CMD_LOAD_STATE(buffer, VIVS_GL_EVENT, VIVS_GL_EVENT_EVENT_ID(event) |
  242. VIVS_GL_EVENT_FROM_PE);
  243. CMD_WAIT(buffer);
  244. CMD_LINK(buffer, 2, return_target + 8);
  245. if (drm_debug & DRM_UT_DRIVER)
  246. pr_info("stream link to 0x%08x @ 0x%08x %p\n",
  247. return_target, gpu_va(gpu, cmdbuf), cmdbuf->vaddr);
  248. if (drm_debug & DRM_UT_DRIVER) {
  249. print_hex_dump(KERN_INFO, "cmd ", DUMP_PREFIX_OFFSET, 16, 4,
  250. cmdbuf->vaddr, cmdbuf->size, 0);
  251. pr_info("link op: %p\n", buffer->vaddr + waitlink_offset);
  252. pr_info("addr: 0x%08x\n", link_target);
  253. pr_info("back: 0x%08x\n", return_target);
  254. pr_info("event: %d\n", event);
  255. }
  256. /*
  257. * Kick off the submitted command by replacing the previous
  258. * WAIT with a link to the address in the ring buffer.
  259. */
  260. etnaviv_buffer_replace_wait(buffer, waitlink_offset,
  261. VIV_FE_LINK_HEADER_OP_LINK |
  262. VIV_FE_LINK_HEADER_PREFETCH(link_dwords),
  263. link_target);
  264. if (drm_debug & DRM_UT_DRIVER)
  265. etnaviv_buffer_dump(gpu, buffer, 0, 0x50);
  266. }