etnaviv_buffer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_cmdbuf.h"
  18. #include "etnaviv_gpu.h"
  19. #include "etnaviv_gem.h"
  20. #include "etnaviv_mmu.h"
  21. #include "common.xml.h"
  22. #include "state.xml.h"
  23. #include "state_hi.xml.h"
  24. #include "state_3d.xml.h"
  25. #include "cmdstream.xml.h"
  26. /*
  27. * Command Buffer helper:
  28. */
  29. static inline void OUT(struct etnaviv_cmdbuf *buffer, u32 data)
  30. {
  31. u32 *vaddr = (u32 *)buffer->vaddr;
  32. BUG_ON(buffer->user_size >= buffer->size);
  33. vaddr[buffer->user_size / 4] = data;
  34. buffer->user_size += 4;
  35. }
  36. static inline void CMD_LOAD_STATE(struct etnaviv_cmdbuf *buffer,
  37. u32 reg, u32 value)
  38. {
  39. u32 index = reg >> VIV_FE_LOAD_STATE_HEADER_OFFSET__SHR;
  40. buffer->user_size = ALIGN(buffer->user_size, 8);
  41. /* write a register via cmd stream */
  42. OUT(buffer, VIV_FE_LOAD_STATE_HEADER_OP_LOAD_STATE |
  43. VIV_FE_LOAD_STATE_HEADER_COUNT(1) |
  44. VIV_FE_LOAD_STATE_HEADER_OFFSET(index));
  45. OUT(buffer, value);
  46. }
  47. static inline void CMD_END(struct etnaviv_cmdbuf *buffer)
  48. {
  49. buffer->user_size = ALIGN(buffer->user_size, 8);
  50. OUT(buffer, VIV_FE_END_HEADER_OP_END);
  51. }
  52. static inline void CMD_WAIT(struct etnaviv_cmdbuf *buffer)
  53. {
  54. buffer->user_size = ALIGN(buffer->user_size, 8);
  55. OUT(buffer, VIV_FE_WAIT_HEADER_OP_WAIT | 200);
  56. }
  57. static inline void CMD_LINK(struct etnaviv_cmdbuf *buffer,
  58. u16 prefetch, u32 address)
  59. {
  60. buffer->user_size = ALIGN(buffer->user_size, 8);
  61. OUT(buffer, VIV_FE_LINK_HEADER_OP_LINK |
  62. VIV_FE_LINK_HEADER_PREFETCH(prefetch));
  63. OUT(buffer, address);
  64. }
  65. static inline void CMD_STALL(struct etnaviv_cmdbuf *buffer,
  66. u32 from, u32 to)
  67. {
  68. buffer->user_size = ALIGN(buffer->user_size, 8);
  69. OUT(buffer, VIV_FE_STALL_HEADER_OP_STALL);
  70. OUT(buffer, VIV_FE_STALL_TOKEN_FROM(from) | VIV_FE_STALL_TOKEN_TO(to));
  71. }
  72. static inline void CMD_SEM(struct etnaviv_cmdbuf *buffer, u32 from, u32 to)
  73. {
  74. CMD_LOAD_STATE(buffer, VIVS_GL_SEMAPHORE_TOKEN,
  75. VIVS_GL_SEMAPHORE_TOKEN_FROM(from) |
  76. VIVS_GL_SEMAPHORE_TOKEN_TO(to));
  77. }
  78. static void etnaviv_cmd_select_pipe(struct etnaviv_gpu *gpu,
  79. struct etnaviv_cmdbuf *buffer, u8 pipe)
  80. {
  81. u32 flush = 0;
  82. /*
  83. * This assumes that if we're switching to 2D, we're switching
  84. * away from 3D, and vice versa. Hence, if we're switching to
  85. * the 2D core, we need to flush the 3D depth and color caches,
  86. * otherwise we need to flush the 2D pixel engine cache.
  87. */
  88. if (gpu->exec_state == ETNA_PIPE_2D)
  89. flush = VIVS_GL_FLUSH_CACHE_PE2D;
  90. else if (gpu->exec_state == ETNA_PIPE_3D)
  91. flush = VIVS_GL_FLUSH_CACHE_DEPTH | VIVS_GL_FLUSH_CACHE_COLOR;
  92. CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE, flush);
  93. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  94. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  95. CMD_LOAD_STATE(buffer, VIVS_GL_PIPE_SELECT,
  96. VIVS_GL_PIPE_SELECT_PIPE(pipe));
  97. }
  98. static void etnaviv_buffer_dump(struct etnaviv_gpu *gpu,
  99. struct etnaviv_cmdbuf *buf, u32 off, u32 len)
  100. {
  101. u32 size = buf->size;
  102. u32 *ptr = buf->vaddr + off;
  103. dev_info(gpu->dev, "virt %p phys 0x%08x free 0x%08x\n",
  104. ptr, etnaviv_cmdbuf_get_va(buf) + off, size - len * 4 - off);
  105. print_hex_dump(KERN_INFO, "cmd ", DUMP_PREFIX_OFFSET, 16, 4,
  106. ptr, len * 4, 0);
  107. }
  108. /*
  109. * Safely replace the WAIT of a waitlink with a new command and argument.
  110. * The GPU may be executing this WAIT while we're modifying it, so we have
  111. * to write it in a specific order to avoid the GPU branching to somewhere
  112. * else. 'wl_offset' is the offset to the first byte of the WAIT command.
  113. */
  114. static void etnaviv_buffer_replace_wait(struct etnaviv_cmdbuf *buffer,
  115. unsigned int wl_offset, u32 cmd, u32 arg)
  116. {
  117. u32 *lw = buffer->vaddr + wl_offset;
  118. lw[1] = arg;
  119. mb();
  120. lw[0] = cmd;
  121. mb();
  122. }
  123. /*
  124. * Ensure that there is space in the command buffer to contiguously write
  125. * 'cmd_dwords' 64-bit words into the buffer, wrapping if necessary.
  126. */
  127. static u32 etnaviv_buffer_reserve(struct etnaviv_gpu *gpu,
  128. struct etnaviv_cmdbuf *buffer, unsigned int cmd_dwords)
  129. {
  130. if (buffer->user_size + cmd_dwords * sizeof(u64) > buffer->size)
  131. buffer->user_size = 0;
  132. return etnaviv_cmdbuf_get_va(buffer) + buffer->user_size;
  133. }
  134. u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu)
  135. {
  136. struct etnaviv_cmdbuf *buffer = gpu->buffer;
  137. /* initialize buffer */
  138. buffer->user_size = 0;
  139. CMD_WAIT(buffer);
  140. CMD_LINK(buffer, 2, etnaviv_cmdbuf_get_va(buffer) +
  141. buffer->user_size - 4);
  142. return buffer->user_size / 8;
  143. }
  144. u16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe_addr)
  145. {
  146. struct etnaviv_cmdbuf *buffer = gpu->buffer;
  147. buffer->user_size = 0;
  148. if (gpu->identity.features & chipFeatures_PIPE_3D) {
  149. CMD_LOAD_STATE(buffer, VIVS_GL_PIPE_SELECT,
  150. VIVS_GL_PIPE_SELECT_PIPE(ETNA_PIPE_3D));
  151. CMD_LOAD_STATE(buffer, VIVS_MMUv2_CONFIGURATION,
  152. mtlb_addr | VIVS_MMUv2_CONFIGURATION_MODE_MODE4_K);
  153. CMD_LOAD_STATE(buffer, VIVS_MMUv2_SAFE_ADDRESS, safe_addr);
  154. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  155. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  156. }
  157. if (gpu->identity.features & chipFeatures_PIPE_2D) {
  158. CMD_LOAD_STATE(buffer, VIVS_GL_PIPE_SELECT,
  159. VIVS_GL_PIPE_SELECT_PIPE(ETNA_PIPE_2D));
  160. CMD_LOAD_STATE(buffer, VIVS_MMUv2_CONFIGURATION,
  161. mtlb_addr | VIVS_MMUv2_CONFIGURATION_MODE_MODE4_K);
  162. CMD_LOAD_STATE(buffer, VIVS_MMUv2_SAFE_ADDRESS, safe_addr);
  163. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  164. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  165. }
  166. CMD_END(buffer);
  167. buffer->user_size = ALIGN(buffer->user_size, 8);
  168. return buffer->user_size / 8;
  169. }
  170. void etnaviv_buffer_end(struct etnaviv_gpu *gpu)
  171. {
  172. struct etnaviv_cmdbuf *buffer = gpu->buffer;
  173. unsigned int waitlink_offset = buffer->user_size - 16;
  174. u32 link_target, flush = 0;
  175. if (gpu->exec_state == ETNA_PIPE_2D)
  176. flush = VIVS_GL_FLUSH_CACHE_PE2D;
  177. else if (gpu->exec_state == ETNA_PIPE_3D)
  178. flush = VIVS_GL_FLUSH_CACHE_DEPTH |
  179. VIVS_GL_FLUSH_CACHE_COLOR |
  180. VIVS_GL_FLUSH_CACHE_TEXTURE |
  181. VIVS_GL_FLUSH_CACHE_TEXTUREVS |
  182. VIVS_GL_FLUSH_CACHE_SHADER_L2;
  183. if (flush) {
  184. unsigned int dwords = 7;
  185. link_target = etnaviv_buffer_reserve(gpu, buffer, dwords);
  186. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  187. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  188. CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE, flush);
  189. if (gpu->exec_state == ETNA_PIPE_3D)
  190. CMD_LOAD_STATE(buffer, VIVS_TS_FLUSH_CACHE,
  191. VIVS_TS_FLUSH_CACHE_FLUSH);
  192. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  193. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  194. CMD_END(buffer);
  195. etnaviv_buffer_replace_wait(buffer, waitlink_offset,
  196. VIV_FE_LINK_HEADER_OP_LINK |
  197. VIV_FE_LINK_HEADER_PREFETCH(dwords),
  198. link_target);
  199. } else {
  200. /* Replace the last link-wait with an "END" command */
  201. etnaviv_buffer_replace_wait(buffer, waitlink_offset,
  202. VIV_FE_END_HEADER_OP_END, 0);
  203. }
  204. }
  205. /* Append a command buffer to the ring buffer. */
  206. void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, unsigned int event,
  207. struct etnaviv_cmdbuf *cmdbuf)
  208. {
  209. struct etnaviv_cmdbuf *buffer = gpu->buffer;
  210. unsigned int waitlink_offset = buffer->user_size - 16;
  211. u32 return_target, return_dwords;
  212. u32 link_target, link_dwords;
  213. if (drm_debug & DRM_UT_DRIVER)
  214. etnaviv_buffer_dump(gpu, buffer, 0, 0x50);
  215. link_target = etnaviv_cmdbuf_get_va(cmdbuf);
  216. link_dwords = cmdbuf->size / 8;
  217. /*
  218. * If we need maintanence prior to submitting this buffer, we will
  219. * need to append a mmu flush load state, followed by a new
  220. * link to this buffer - a total of four additional words.
  221. */
  222. if (gpu->mmu->need_flush || gpu->switch_context) {
  223. u32 target, extra_dwords;
  224. /* link command */
  225. extra_dwords = 1;
  226. /* flush command */
  227. if (gpu->mmu->need_flush) {
  228. if (gpu->mmu->version == ETNAVIV_IOMMU_V1)
  229. extra_dwords += 1;
  230. else
  231. extra_dwords += 3;
  232. }
  233. /* pipe switch commands */
  234. if (gpu->switch_context)
  235. extra_dwords += 4;
  236. target = etnaviv_buffer_reserve(gpu, buffer, extra_dwords);
  237. if (gpu->mmu->need_flush) {
  238. /* Add the MMU flush */
  239. if (gpu->mmu->version == ETNAVIV_IOMMU_V1) {
  240. CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_MMU,
  241. VIVS_GL_FLUSH_MMU_FLUSH_FEMMU |
  242. VIVS_GL_FLUSH_MMU_FLUSH_UNK1 |
  243. VIVS_GL_FLUSH_MMU_FLUSH_UNK2 |
  244. VIVS_GL_FLUSH_MMU_FLUSH_PEMMU |
  245. VIVS_GL_FLUSH_MMU_FLUSH_UNK4);
  246. } else {
  247. CMD_LOAD_STATE(buffer, VIVS_MMUv2_CONFIGURATION,
  248. VIVS_MMUv2_CONFIGURATION_MODE_MASK |
  249. VIVS_MMUv2_CONFIGURATION_ADDRESS_MASK |
  250. VIVS_MMUv2_CONFIGURATION_FLUSH_FLUSH);
  251. CMD_SEM(buffer, SYNC_RECIPIENT_FE,
  252. SYNC_RECIPIENT_PE);
  253. CMD_STALL(buffer, SYNC_RECIPIENT_FE,
  254. SYNC_RECIPIENT_PE);
  255. }
  256. gpu->mmu->need_flush = false;
  257. }
  258. if (gpu->switch_context) {
  259. etnaviv_cmd_select_pipe(gpu, buffer, cmdbuf->exec_state);
  260. gpu->exec_state = cmdbuf->exec_state;
  261. gpu->switch_context = false;
  262. }
  263. /* And the link to the submitted buffer */
  264. CMD_LINK(buffer, link_dwords, link_target);
  265. /* Update the link target to point to above instructions */
  266. link_target = target;
  267. link_dwords = extra_dwords;
  268. }
  269. /*
  270. * Append a LINK to the submitted command buffer to return to
  271. * the ring buffer. return_target is the ring target address.
  272. * We need at most 7 dwords in the return target: 2 cache flush +
  273. * 2 semaphore stall + 1 event + 1 wait + 1 link.
  274. */
  275. return_dwords = 7;
  276. return_target = etnaviv_buffer_reserve(gpu, buffer, return_dwords);
  277. CMD_LINK(cmdbuf, return_dwords, return_target);
  278. /*
  279. * Append a cache flush, stall, event, wait and link pointing back to
  280. * the wait command to the ring buffer.
  281. */
  282. if (gpu->exec_state == ETNA_PIPE_2D) {
  283. CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE,
  284. VIVS_GL_FLUSH_CACHE_PE2D);
  285. } else {
  286. CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_CACHE,
  287. VIVS_GL_FLUSH_CACHE_DEPTH |
  288. VIVS_GL_FLUSH_CACHE_COLOR);
  289. CMD_LOAD_STATE(buffer, VIVS_TS_FLUSH_CACHE,
  290. VIVS_TS_FLUSH_CACHE_FLUSH);
  291. }
  292. CMD_SEM(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  293. CMD_STALL(buffer, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
  294. CMD_LOAD_STATE(buffer, VIVS_GL_EVENT, VIVS_GL_EVENT_EVENT_ID(event) |
  295. VIVS_GL_EVENT_FROM_PE);
  296. CMD_WAIT(buffer);
  297. CMD_LINK(buffer, 2, etnaviv_cmdbuf_get_va(buffer) +
  298. buffer->user_size - 4);
  299. if (drm_debug & DRM_UT_DRIVER)
  300. pr_info("stream link to 0x%08x @ 0x%08x %p\n",
  301. return_target, etnaviv_cmdbuf_get_va(cmdbuf),
  302. cmdbuf->vaddr);
  303. if (drm_debug & DRM_UT_DRIVER) {
  304. print_hex_dump(KERN_INFO, "cmd ", DUMP_PREFIX_OFFSET, 16, 4,
  305. cmdbuf->vaddr, cmdbuf->size, 0);
  306. pr_info("link op: %p\n", buffer->vaddr + waitlink_offset);
  307. pr_info("addr: 0x%08x\n", link_target);
  308. pr_info("back: 0x%08x\n", return_target);
  309. pr_info("event: %d\n", event);
  310. }
  311. /*
  312. * Kick off the submitted command by replacing the previous
  313. * WAIT with a link to the address in the ring buffer.
  314. */
  315. etnaviv_buffer_replace_wait(buffer, waitlink_offset,
  316. VIV_FE_LINK_HEADER_OP_LINK |
  317. VIV_FE_LINK_HEADER_PREFETCH(link_dwords),
  318. link_target);
  319. if (drm_debug & DRM_UT_DRIVER)
  320. etnaviv_buffer_dump(gpu, buffer, 0, 0x50);
  321. }