etnaviv_buffer.c 13 KB

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