etnaviv_buffer.c 11 KB

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