0545-drm-vc4-Add-fragment-shader-threading-support.patch 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. From 9bb867075fc4b0b7efc1640dc4cdd5b70b482ff1 Mon Sep 17 00:00:00 2001
  2. From: Jonas Pfeil <pfeiljonas@gmx.de>
  3. Date: Tue, 8 Nov 2016 00:18:39 +0100
  4. Subject: [PATCH] drm/vc4: Add fragment shader threading support
  5. FS threading brings performance improvements of 0-20% in glmark2.
  6. The validation code checks for thread switch signals and ensures that
  7. the registers of the other thread are not touched, and that our clamps
  8. are not live across thread switches. It also checks that the
  9. threading and branching instructions do not interfere.
  10. (Original patch by Jonas, changes by anholt for style cleanup,
  11. removing validation the kernel doesn't need to do, and adding the flag
  12. for userspace).
  13. v2: Minor style fixes from checkpatch.
  14. Signed-off-by: Jonas Pfeil <pfeiljonas@gmx.de>
  15. Signed-off-by: Eric Anholt <eric@anholt.net>
  16. (cherry picked from commit c778cc5df944291dcdb1ca7a6bb781fbc22550c5)
  17. ---
  18. drivers/gpu/drm/vc4/vc4_drv.c | 1 +
  19. drivers/gpu/drm/vc4/vc4_drv.h | 2 +
  20. drivers/gpu/drm/vc4/vc4_validate.c | 17 +++++---
  21. drivers/gpu/drm/vc4/vc4_validate_shaders.c | 63 ++++++++++++++++++++++++++++++
  22. include/uapi/drm/vc4_drm.h | 1 +
  23. 5 files changed, 79 insertions(+), 5 deletions(-)
  24. --- a/drivers/gpu/drm/vc4/vc4_drv.c
  25. +++ b/drivers/gpu/drm/vc4/vc4_drv.c
  26. @@ -107,6 +107,7 @@ static int vc4_get_param_ioctl(struct dr
  27. break;
  28. case DRM_VC4_PARAM_SUPPORTS_BRANCHES:
  29. case DRM_VC4_PARAM_SUPPORTS_ETC1:
  30. + case DRM_VC4_PARAM_SUPPORTS_THREADED_FS:
  31. args->value = true;
  32. break;
  33. default:
  34. --- a/drivers/gpu/drm/vc4/vc4_drv.h
  35. +++ b/drivers/gpu/drm/vc4/vc4_drv.h
  36. @@ -395,6 +395,8 @@ struct vc4_validated_shader_info {
  37. uint32_t num_uniform_addr_offsets;
  38. uint32_t *uniform_addr_offsets;
  39. +
  40. + bool is_threaded;
  41. };
  42. /**
  43. --- a/drivers/gpu/drm/vc4/vc4_validate.c
  44. +++ b/drivers/gpu/drm/vc4/vc4_validate.c
  45. @@ -789,11 +789,6 @@ validate_gl_shader_rec(struct drm_device
  46. exec->shader_rec_v += roundup(packet_size, 16);
  47. exec->shader_rec_size -= packet_size;
  48. - if (!(*(uint16_t *)pkt_u & VC4_SHADER_FLAG_FS_SINGLE_THREAD)) {
  49. - DRM_ERROR("Multi-threaded fragment shaders not supported.\n");
  50. - return -EINVAL;
  51. - }
  52. -
  53. for (i = 0; i < shader_reloc_count; i++) {
  54. if (src_handles[i] > exec->bo_count) {
  55. DRM_ERROR("Shader handle %d too big\n", src_handles[i]);
  56. @@ -810,6 +805,18 @@ validate_gl_shader_rec(struct drm_device
  57. return -EINVAL;
  58. }
  59. + if (((*(uint16_t *)pkt_u & VC4_SHADER_FLAG_FS_SINGLE_THREAD) == 0) !=
  60. + to_vc4_bo(&bo[0]->base)->validated_shader->is_threaded) {
  61. + DRM_ERROR("Thread mode of CL and FS do not match\n");
  62. + return -EINVAL;
  63. + }
  64. +
  65. + if (to_vc4_bo(&bo[1]->base)->validated_shader->is_threaded ||
  66. + to_vc4_bo(&bo[2]->base)->validated_shader->is_threaded) {
  67. + DRM_ERROR("cs and vs cannot be threaded\n");
  68. + return -EINVAL;
  69. + }
  70. +
  71. for (i = 0; i < shader_reloc_count; i++) {
  72. struct vc4_validated_shader_info *validated_shader;
  73. uint32_t o = shader_reloc_offsets[i];
  74. --- a/drivers/gpu/drm/vc4/vc4_validate_shaders.c
  75. +++ b/drivers/gpu/drm/vc4/vc4_validate_shaders.c
  76. @@ -83,6 +83,13 @@ struct vc4_shader_validation_state {
  77. * basic blocks.
  78. */
  79. bool needs_uniform_address_for_loop;
  80. +
  81. + /* Set when we find an instruction writing the top half of the
  82. + * register files. If we allowed writing the unusable regs in
  83. + * a threaded shader, then the other shader running on our
  84. + * QPU's clamp validation would be invalid.
  85. + */
  86. + bool all_registers_used;
  87. };
  88. static uint32_t
  89. @@ -119,6 +126,13 @@ raddr_add_a_to_live_reg_index(uint64_t i
  90. }
  91. static bool
  92. +live_reg_is_upper_half(uint32_t lri)
  93. +{
  94. + return (lri >= 16 && lri < 32) ||
  95. + (lri >= 32 + 16 && lri < 32 + 32);
  96. +}
  97. +
  98. +static bool
  99. is_tmu_submit(uint32_t waddr)
  100. {
  101. return (waddr == QPU_W_TMU0_S ||
  102. @@ -390,6 +404,9 @@ check_reg_write(struct vc4_validated_sha
  103. } else {
  104. validation_state->live_immediates[lri] = ~0;
  105. }
  106. +
  107. + if (live_reg_is_upper_half(lri))
  108. + validation_state->all_registers_used = true;
  109. }
  110. switch (waddr) {
  111. @@ -598,6 +615,11 @@ check_instruction_reads(struct vc4_valid
  112. }
  113. }
  114. + if ((raddr_a >= 16 && raddr_a < 32) ||
  115. + (raddr_b >= 16 && raddr_b < 32 && sig != QPU_SIG_SMALL_IMM)) {
  116. + validation_state->all_registers_used = true;
  117. + }
  118. +
  119. return true;
  120. }
  121. @@ -753,6 +775,7 @@ vc4_validate_shader(struct drm_gem_cma_o
  122. {
  123. bool found_shader_end = false;
  124. int shader_end_ip = 0;
  125. + uint32_t last_thread_switch_ip = -3;
  126. uint32_t ip;
  127. struct vc4_validated_shader_info *validated_shader = NULL;
  128. struct vc4_shader_validation_state validation_state;
  129. @@ -785,6 +808,17 @@ vc4_validate_shader(struct drm_gem_cma_o
  130. if (!vc4_handle_branch_target(&validation_state))
  131. goto fail;
  132. + if (ip == last_thread_switch_ip + 3) {
  133. + /* Reset r0-r3 live clamp data */
  134. + int i;
  135. +
  136. + for (i = 64; i < LIVE_REG_COUNT; i++) {
  137. + validation_state.live_min_clamp_offsets[i] = ~0;
  138. + validation_state.live_max_clamp_regs[i] = false;
  139. + validation_state.live_immediates[i] = ~0;
  140. + }
  141. + }
  142. +
  143. switch (sig) {
  144. case QPU_SIG_NONE:
  145. case QPU_SIG_WAIT_FOR_SCOREBOARD:
  146. @@ -794,6 +828,8 @@ vc4_validate_shader(struct drm_gem_cma_o
  147. case QPU_SIG_LOAD_TMU1:
  148. case QPU_SIG_PROG_END:
  149. case QPU_SIG_SMALL_IMM:
  150. + case QPU_SIG_THREAD_SWITCH:
  151. + case QPU_SIG_LAST_THREAD_SWITCH:
  152. if (!check_instruction_writes(validated_shader,
  153. &validation_state)) {
  154. DRM_ERROR("Bad write at ip %d\n", ip);
  155. @@ -809,6 +845,18 @@ vc4_validate_shader(struct drm_gem_cma_o
  156. shader_end_ip = ip;
  157. }
  158. + if (sig == QPU_SIG_THREAD_SWITCH ||
  159. + sig == QPU_SIG_LAST_THREAD_SWITCH) {
  160. + validated_shader->is_threaded = true;
  161. +
  162. + if (ip < last_thread_switch_ip + 3) {
  163. + DRM_ERROR("Thread switch too soon after "
  164. + "last switch at ip %d\n", ip);
  165. + goto fail;
  166. + }
  167. + last_thread_switch_ip = ip;
  168. + }
  169. +
  170. break;
  171. case QPU_SIG_LOAD_IMM:
  172. @@ -823,6 +871,13 @@ vc4_validate_shader(struct drm_gem_cma_o
  173. if (!check_branch(inst, validated_shader,
  174. &validation_state, ip))
  175. goto fail;
  176. +
  177. + if (ip < last_thread_switch_ip + 3) {
  178. + DRM_ERROR("Branch in thread switch at ip %d",
  179. + ip);
  180. + goto fail;
  181. + }
  182. +
  183. break;
  184. default:
  185. DRM_ERROR("Unsupported QPU signal %d at "
  186. @@ -844,6 +899,14 @@ vc4_validate_shader(struct drm_gem_cma_o
  187. goto fail;
  188. }
  189. + /* Might corrupt other thread */
  190. + if (validated_shader->is_threaded &&
  191. + validation_state.all_registers_used) {
  192. + DRM_ERROR("Shader uses threading, but uses the upper "
  193. + "half of the registers, too\n");
  194. + goto fail;
  195. + }
  196. +
  197. /* If we did a backwards branch and we haven't emitted a uniforms
  198. * reset since then, we still need the uniforms stream to have the
  199. * uniforms address available so that the backwards branch can do its
  200. --- a/include/uapi/drm/vc4_drm.h
  201. +++ b/include/uapi/drm/vc4_drm.h
  202. @@ -287,6 +287,7 @@ struct drm_vc4_get_hang_state {
  203. #define DRM_VC4_PARAM_V3D_IDENT2 2
  204. #define DRM_VC4_PARAM_SUPPORTS_BRANCHES 3
  205. #define DRM_VC4_PARAM_SUPPORTS_ETC1 4
  206. +#define DRM_VC4_PARAM_SUPPORTS_THREADED_FS 5
  207. struct drm_vc4_get_param {
  208. __u32 param;