@@ -83,6 +83,13 @@ struct vc4_shader_validation_state {
8383 * basic blocks.
8484 */
8585 bool needs_uniform_address_for_loop ;
86+
87+ /* Set when we find an instruction writing the top half of the
88+ * register files. If we allowed writing the unusable regs in
89+ * a threaded shader, then the other shader running on our
90+ * QPU's clamp validation would be invalid.
91+ */
92+ bool all_registers_used ;
8693};
8794
8895static uint32_t
@@ -118,6 +125,13 @@ raddr_add_a_to_live_reg_index(uint64_t inst)
118125 return ~0 ;
119126}
120127
128+ static bool
129+ live_reg_is_upper_half (uint32_t lri )
130+ {
131+ return (lri >= 16 && lri < 32 ) ||
132+ (lri >= 32 + 16 && lri < 32 + 32 );
133+ }
134+
121135static bool
122136is_tmu_submit (uint32_t waddr )
123137{
@@ -390,6 +404,9 @@ check_reg_write(struct vc4_validated_shader_info *validated_shader,
390404 } else {
391405 validation_state -> live_immediates [lri ] = ~0 ;
392406 }
407+
408+ if (live_reg_is_upper_half (lri ))
409+ validation_state -> all_registers_used = true;
393410 }
394411
395412 switch (waddr ) {
@@ -598,6 +615,11 @@ check_instruction_reads(struct vc4_validated_shader_info *validated_shader,
598615 }
599616 }
600617
618+ if ((raddr_a >= 16 && raddr_a < 32 ) ||
619+ (raddr_b >= 16 && raddr_b < 32 && sig != QPU_SIG_SMALL_IMM )) {
620+ validation_state -> all_registers_used = true;
621+ }
622+
601623 return true;
602624}
603625
@@ -753,6 +775,7 @@ vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
753775{
754776 bool found_shader_end = false;
755777 int shader_end_ip = 0 ;
778+ uint32_t last_thread_switch_ip = -3 ;
756779 uint32_t ip ;
757780 struct vc4_validated_shader_info * validated_shader = NULL ;
758781 struct vc4_shader_validation_state validation_state ;
@@ -785,6 +808,17 @@ vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
785808 if (!vc4_handle_branch_target (& validation_state ))
786809 goto fail ;
787810
811+ if (ip == last_thread_switch_ip + 3 ) {
812+ /* Reset r0-r3 live clamp data */
813+ int i ;
814+
815+ for (i = 64 ; i < LIVE_REG_COUNT ; i ++ ) {
816+ validation_state .live_min_clamp_offsets [i ] = ~0 ;
817+ validation_state .live_max_clamp_regs [i ] = false;
818+ validation_state .live_immediates [i ] = ~0 ;
819+ }
820+ }
821+
788822 switch (sig ) {
789823 case QPU_SIG_NONE :
790824 case QPU_SIG_WAIT_FOR_SCOREBOARD :
@@ -794,6 +828,8 @@ vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
794828 case QPU_SIG_LOAD_TMU1 :
795829 case QPU_SIG_PROG_END :
796830 case QPU_SIG_SMALL_IMM :
831+ case QPU_SIG_THREAD_SWITCH :
832+ case QPU_SIG_LAST_THREAD_SWITCH :
797833 if (!check_instruction_writes (validated_shader ,
798834 & validation_state )) {
799835 DRM_ERROR ("Bad write at ip %d\n" , ip );
@@ -809,6 +845,18 @@ vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
809845 shader_end_ip = ip ;
810846 }
811847
848+ if (sig == QPU_SIG_THREAD_SWITCH ||
849+ sig == QPU_SIG_LAST_THREAD_SWITCH ) {
850+ validated_shader -> is_threaded = true;
851+
852+ if (ip < last_thread_switch_ip + 3 ) {
853+ DRM_ERROR ("Thread switch too soon after "
854+ "last switch at ip %d\n" , ip );
855+ goto fail ;
856+ }
857+ last_thread_switch_ip = ip ;
858+ }
859+
812860 break ;
813861
814862 case QPU_SIG_LOAD_IMM :
@@ -823,6 +871,13 @@ vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
823871 if (!check_branch (inst , validated_shader ,
824872 & validation_state , ip ))
825873 goto fail ;
874+
875+ if (ip < last_thread_switch_ip + 3 ) {
876+ DRM_ERROR ("Branch in thread switch at ip %d" ,
877+ ip );
878+ goto fail ;
879+ }
880+
826881 break ;
827882 default :
828883 DRM_ERROR ("Unsupported QPU signal %d at "
@@ -844,6 +899,14 @@ vc4_validate_shader(struct drm_gem_cma_object *shader_obj)
844899 goto fail ;
845900 }
846901
902+ /* Might corrupt other thread */
903+ if (validated_shader -> is_threaded &&
904+ validation_state .all_registers_used ) {
905+ DRM_ERROR ("Shader uses threading, but uses the upper "
906+ "half of the registers, too\n" );
907+ goto fail ;
908+ }
909+
847910 /* If we did a backwards branch and we haven't emitted a uniforms
848911 * reset since then, we still need the uniforms stream to have the
849912 * uniforms address available so that the backwards branch can do its
0 commit comments