struct vc4_shader_validation_state { /* Current IP being validated. */
uint32_t ip;
/* IP at the end of the BO, do not read shader[max_ip] */
uint32_t max_ip;
uint64_t *shader;
struct vc4_texture_sample_info tmu_setup[2]; int tmu_write_count[2];
/* For registers that were last written to by a MIN instruction with *oneargumentbeingauniform,theaddressoftheuniform. *Otherwise,~0. * *Thisisusedforthevalidationofdirectaddressmemoryreads.
*/
uint32_t live_min_clamp_offsets[LIVE_REG_COUNT]; bool live_max_clamp_regs[LIVE_REG_COUNT];
uint32_t live_immediates[LIVE_REG_COUNT];
/* Bitfield of which IPs are used as branch targets. * *Usedforvalidationthattheuniformstreamisupdatedattheright *pointsandclearingthetexturing/clampingstate.
*/ unsignedlong *branch_targets;
/* Set when entering a basic block, and cleared when the uniform *addressupdateisfound.Thisisusedtomakesurethatwedon't *readuniformswhentheaddressisundefined.
*/ bool needs_uniform_address_update;
/* Set when we find a backwards branch. If the branch is backwards, *thetaragetisprobablydoinganaddressresettoreaduniforms, *andsoweneedtobesurethatauniformsaddressispresentinthe *stream,eveniftheshaderdidn'tneedtoreaduniformsinlater *basicblocks.
*/ bool needs_uniform_address_for_loop;
/* Set when we find an instruction writing the top half of the *registerfiles.Ifweallowedwritingtheunusableregsin *athreadedshader,thentheothershaderrunningonour *QPU'sclampvalidationwouldbeinvalid.
*/ bool all_registers_used;
};
if (sig == QPU_SIG_SMALL_IMM) {
DRM_DEBUG("direct TMU read used small immediate\n"); returnfalse;
}
/* Make sure that this texture load is an add of the base *addressoftheUBOtoaclampedoffsetwithintheUBO.
*/ if (is_mul ||
QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {
DRM_DEBUG("direct TMU load wasn't an add\n"); returnfalse;
}
/* We assert that the clamped address is the first *argument,andtheUBObaseaddressisthesecondargument. *Thisisarbitrary,butsimplerthansupportingflippingthe *twoeitherway.
*/
clamp_reg = raddr_add_a_to_live_reg_index(inst); if (clamp_reg == ~0) {
DRM_DEBUG("direct TMU load wasn't clamped\n"); returnfalse;
}
/* Store the clamp value's offset in p1 (see reloc_tex() in *vc4_validate.c).
*/
validation_state->tmu_setup[tmu].p_offset[1] =
clamp_offset;
if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
!(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
DRM_DEBUG("direct TMU load didn't add to a uniform\n"); returnfalse;
}
validation_state->tmu_setup[tmu].is_direct = true;
} else { if (raddr_a == QPU_R_UNIF || (sig != QPU_SIG_SMALL_IMM &&
raddr_b == QPU_R_UNIF)) {
DRM_DEBUG("uniform read in the same instruction as " "texture setup.\n"); returnfalse;
}
}
if (validation_state->tmu_write_count[tmu] >= 4) {
DRM_DEBUG("TMU%d got too many parameters before dispatch\n",
tmu); returnfalse;
}
validation_state->tmu_setup[tmu].p_offset[validation_state->tmu_write_count[tmu]] =
validated_shader->uniforms_size;
validation_state->tmu_write_count[tmu]++; /* Since direct uses a RADDR uniform reference, it will get counted in *check_instruction_reads()
*/ if (!is_direct) { if (validation_state->needs_uniform_address_update) {
DRM_DEBUG("Texturing with undefined uniform address\n"); returnfalse;
}
validated_shader->uniforms_size += 4;
}
if (submit) { if (!record_texture_sample(validated_shader,
validation_state, tmu)) { returnfalse;
}
/* We only support absolute uniform address changes, and we *requirethattheybeinthecurrentbasicblockbeforeany *ofitsuniformreads. * *OnecouldpotentiallyemitmoreefficientQPUcode,by *noticingthat(say)anifstatementdoesuniformcontrol *flowforallthreadsandthattheifreadsthesamenumber *ofuniformsoneachside.However,thisschemeiseasyto *validatesoit'sallweallowfornow.
*/ switch (QPU_GET_FIELD(inst, QPU_SIG)) { case QPU_SIG_NONE: case QPU_SIG_SCOREBOARD_UNLOCK: case QPU_SIG_COLOR_LOAD: case QPU_SIG_LOAD_TMU0: case QPU_SIG_LOAD_TMU1: break; default:
DRM_DEBUG("uniforms address change must be " "normal math\n"); returnfalse;
}
if (is_mul || QPU_GET_FIELD(inst, QPU_OP_ADD) != QPU_A_ADD) {
DRM_DEBUG("Uniform address reset must be an ADD.\n"); returnfalse;
}
if (QPU_GET_FIELD(inst, QPU_COND_ADD) != QPU_COND_ALWAYS) {
DRM_DEBUG("Uniform address reset must be unconditional.\n"); returnfalse;
}
if (QPU_GET_FIELD(inst, QPU_PACK) != QPU_PACK_A_NOP &&
!(inst & QPU_PM)) {
DRM_DEBUG("No packing allowed on uniforms reset\n"); returnfalse;
}
if (add_lri == -1) {
DRM_DEBUG("First argument of uniform address write must be " "an immediate value.\n"); returnfalse;
}
if (validation_state->live_immediates[add_lri] != expected_offset) {
DRM_DEBUG("Resetting uniforms with offset %db instead of %db\n",
validation_state->live_immediates[add_lri],
expected_offset); returnfalse;
}
if (!(add_b == QPU_MUX_A && raddr_a == QPU_R_UNIF) &&
!(add_b == QPU_MUX_B && raddr_b == QPU_R_UNIF)) {
DRM_DEBUG("Second argument of uniform address write must be " "a uniform.\n"); returnfalse;
}
case QPU_W_TLB_COLOR_MS: case QPU_W_TLB_COLOR_ALL: case QPU_W_TLB_Z: /* These only interact with the tile buffer, not main memory, *sothey'resafe.
*/ returntrue;
case QPU_W_TMU0_S: case QPU_W_TMU0_T: case QPU_W_TMU0_R: case QPU_W_TMU0_B: case QPU_W_TMU1_S: case QPU_W_TMU1_T: case QPU_W_TMU1_R: case QPU_W_TMU1_B: return check_tmu_write(validated_shader, validation_state,
is_mul);
case QPU_W_HOST_INT: case QPU_W_TMU_NOSWAP: case QPU_W_TLB_ALPHA_MASK: case QPU_W_MUTEX_RELEASE: /* XXX: I haven't thought about these, so don't support them *fornow.
*/
DRM_DEBUG("Unsupported waddr %d\n", waddr); returnfalse;
case QPU_W_VPM_ADDR:
DRM_DEBUG("General VPM DMA unsupported\n"); returnfalse;
case QPU_W_VPM: case QPU_W_VPMVCD_SETUP: /* We allow VPM setup in general, even including VPM DMA *configurationsetup,becausethe(unsafe)DMAcanonlybe *triggeredbyQPU_W_VPM_ADDRwrites.
*/ returntrue;
/* Check whether OP_ADD's A argumennt comes from a live MAX(x, 0), *beforeweclearpreviouslivestate.
*/
lri_add_a = raddr_add_a_to_live_reg_index(inst);
add_a_is_min_0 = (lri_add_a != ~0 &&
validation_state->live_max_clamp_regs[lri_add_a]);
/* Clear live state for registers written by our instruction. */
lri_add = waddr_to_live_reg_index(waddr_add, ws);
lri_mul = waddr_to_live_reg_index(waddr_mul, !ws); if (lri_mul != ~0) {
validation_state->live_max_clamp_regs[lri_mul] = false;
validation_state->live_min_clamp_offsets[lri_mul] = ~0;
} if (lri_add != ~0) {
validation_state->live_max_clamp_regs[lri_add] = false;
validation_state->live_min_clamp_offsets[lri_add] = ~0;
} else { /* Nothing further to do for live tracking, since only ADDs *generatenewliveclampregisters.
*/ return;
}
/* Now, handle remaining live clamp tracking for the ADD operation. */
if (cond_add != QPU_COND_ALWAYS) return;
if (op_add == QPU_A_MAX) { /* Track live clamps of a value to a minimum of 0 (in either *arg).
*/ if (sig != QPU_SIG_SMALL_IMM || raddr_b != 0 ||
(add_a != QPU_MUX_B && add_b != QPU_MUX_B)) { return;
}
validation_state->live_max_clamp_regs[lri_add] = true;
} elseif (op_add == QPU_A_MIN) { /* Track live clamps of a value clamped to a minimum of 0 and *amaximumofsomeuniform'soffset.
*/ if (!add_a_is_min_0) return;
if ((int)branch_imm < 0)
validation_state->needs_uniform_address_for_loop = true;
/* We don't want to have to worry about validation of this, and *there'snoneedforit.
*/ if (waddr_add != QPU_W_NOP || waddr_mul != QPU_W_NOP) {
DRM_DEBUG("branch instruction at %d wrote a register.\n",
validation_state->ip); returnfalse;
}
/* Make sure that all branches are absolute and point within the shader, and *notetheirtargetsforlater.
*/ staticbool
vc4_validate_branches(struct vc4_shader_validation_state *validation_state)
{
uint32_t max_branch_target = 0; int ip; int last_branch = -2;
for (ip = 0; ip < validation_state->max_ip; ip++) {
uint64_t inst = validation_state->shader[ip];
int32_t branch_imm = QPU_GET_FIELD(inst, QPU_BRANCH_TARGET);
uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
uint32_t after_delay_ip = ip + 4;
uint32_t branch_target_ip;
if (sig == QPU_SIG_PROG_END) { /* There are two delay slots after program end is *signaledthatarestillexecuted,thenwe're *finished.validation_state->max_ipisthe *instructionafterthelastvalidinstructioninthe *program.
*/
validation_state->max_ip = ip + 3; continue;
}
if (sig != QPU_SIG_BRANCH) continue;
if (ip - last_branch < 4) {
DRM_DEBUG("Branch at %d during delay slots\n", ip); returnfalse;
}
last_branch = ip;
if (inst & QPU_BRANCH_REG) {
DRM_DEBUG("branching from register relative " "not supported\n"); returnfalse;
}
if (!(inst & QPU_BRANCH_REL)) {
DRM_DEBUG("relative branching required\n"); returnfalse;
}
/* The actual branch target is the instruction after the delay *slots,pluswhateverbyteoffsetisinthelow32bitsof *theinstruction.Makesurewe'renotbranchingbeyondthe *endoftheshaderobject.
*/ if (branch_imm % sizeof(inst) != 0) {
DRM_DEBUG("branch target not aligned\n"); returnfalse;
}
branch_target_ip = after_delay_ip + (branch_imm >> 3); if (branch_target_ip >= validation_state->max_ip) {
DRM_DEBUG("Branch at %d outside of shader (ip %d/%d)\n",
ip, branch_target_ip,
validation_state->max_ip); returnfalse;
}
set_bit(branch_target_ip, validation_state->branch_targets);
/* Make sure that the non-branching path is also not outside *theshader.
*/ if (after_delay_ip >= validation_state->max_ip) {
DRM_DEBUG("Branch at %d continues past shader end " "(%d/%d)\n",
ip, after_delay_ip, validation_state->max_ip); returnfalse;
}
set_bit(after_delay_ip, validation_state->branch_targets);
max_branch_target = max(max_branch_target, after_delay_ip);
}
if (max_branch_target > validation_state->max_ip - 3) {
DRM_DEBUG("Branch landed after QPU_SIG_PROG_END"); returnfalse;
}
returntrue;
}
/* Resets any known state for the shader, used when we may be branched to from *multiplelocationsintheprogram(oratshaderstart).
*/ staticvoid
reset_validation_state(struct vc4_shader_validation_state *validation_state)
{ int i;
for (i = 0; i < 8; i++)
validation_state->tmu_setup[i / 4].p_offset[i % 4] = ~0;
for (i = 0; i < LIVE_REG_COUNT; i++) {
validation_state->live_min_clamp_offsets[i] = ~0;
validation_state->live_max_clamp_regs[i] = false;
validation_state->live_immediates[i] = ~0;
}
}
staticbool
vc4_handle_branch_target(struct vc4_shader_validation_state *validation_state)
{
uint32_t ip = validation_state->ip;
if (!test_bit(ip, validation_state->branch_targets)) returntrue;
if (texturing_in_progress(validation_state)) {
DRM_DEBUG("Branch target landed during TMU setup\n"); returnfalse;
}
/* Reset our live values tracking, since this instruction may have *multiplepredecessors. * *Onecouldpotentiallydoanalysistodeterminethat,for *example,allpredecessorshavealivemaxclampinthesame *register,butwedon'tbotherwiththat.
*/
reset_validation_state(validation_state);
/* Since we've entered a basic block from potentially multiple *predecessors,weneedtheuniformsaddresstobeupdatedbeforeany *unformsareread.Werequirethatafteranybranchpoint,thenext *uniformtobeloadedisauniformaddressoffset.Thatuniform's *offsetwillbemarkedbytheuniformaddressregisterwrite *validation,oraone-offtheend-of-programcheck.
*/
validation_state->needs_uniform_address_update = true;
validation_state.branch_targets =
kcalloc(BITS_TO_LONGS(validation_state.max_ip), sizeof(unsignedlong), GFP_KERNEL); if (!validation_state.branch_targets) goto fail;
validated_shader = kcalloc(1, sizeof(*validated_shader), GFP_KERNEL); if (!validated_shader) goto fail;
if (!vc4_validate_branches(&validation_state)) goto fail;
for (ip = 0; ip < validation_state.max_ip; ip++) {
uint64_t inst = validation_state.shader[ip];
uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
validation_state.ip = ip;
if (!vc4_handle_branch_target(&validation_state)) goto fail;
if (ip == last_thread_switch_ip + 3) { /* Reset r0-r3 live clamp data */ int i;
for (i = 64; i < LIVE_REG_COUNT; i++) {
validation_state.live_min_clamp_offsets[i] = ~0;
validation_state.live_max_clamp_regs[i] = false;
validation_state.live_immediates[i] = ~0;
}
}
switch (sig) { case QPU_SIG_NONE: case QPU_SIG_WAIT_FOR_SCOREBOARD: case QPU_SIG_SCOREBOARD_UNLOCK: case QPU_SIG_COLOR_LOAD: case QPU_SIG_LOAD_TMU0: case QPU_SIG_LOAD_TMU1: case QPU_SIG_PROG_END: case QPU_SIG_SMALL_IMM: case QPU_SIG_THREAD_SWITCH: case QPU_SIG_LAST_THREAD_SWITCH: if (!check_instruction_writes(validated_shader,
&validation_state)) {
DRM_DEBUG("Bad write at ip %d\n", ip); goto fail;
}
if (!check_instruction_reads(validated_shader,
&validation_state)) goto fail;
if (sig == QPU_SIG_THREAD_SWITCH ||
sig == QPU_SIG_LAST_THREAD_SWITCH) {
validated_shader->is_threaded = true;
if (ip < last_thread_switch_ip + 3) {
DRM_DEBUG("Thread switch too soon after " "last switch at ip %d\n", ip); goto fail;
}
last_thread_switch_ip = ip;
}
break;
case QPU_SIG_LOAD_IMM: if (!check_instruction_writes(validated_shader,
&validation_state)) {
DRM_DEBUG("Bad LOAD_IMM write at ip %d\n", ip); goto fail;
} break;
case QPU_SIG_BRANCH: if (!check_branch(inst, validated_shader,
&validation_state, ip)) goto fail;
if (ip < last_thread_switch_ip + 3) {
DRM_DEBUG("Branch in thread switch at ip %d",
ip); goto fail;
}
break; default:
DRM_DEBUG("Unsupported QPU signal %d at " "instruction %d\n", sig, ip); goto fail;
}
/* There are two delay slots after program end is signaled *thatarestillexecuted,thenwe'refinished.
*/ if (found_shader_end && ip == shader_end_ip + 2) break;
}
if (ip == validation_state.max_ip) {
DRM_DEBUG("shader failed to terminate before " "shader BO end at %zd\n",
shader_obj->base.size); goto fail;
}
/* Might corrupt other thread */ if (validated_shader->is_threaded &&
validation_state.all_registers_used) {
DRM_DEBUG("Shader uses threading, but uses the upper " "half of the registers, too\n"); goto fail;
}
/* If we did a backwards branch and we haven't emitted a uniforms *resetsincethen,westillneedtheuniformsstreamtohavethe *uniformsaddressavailablesothatthebackwardsbranchcandoits *uniformsreset. * *Wecouldpotentiallyprovethatthebackwardsbranchdoesn't *containanyusesofuniformsuntilprogramexit,butthatdoesn't *seemtobeworththetrouble.
*/ if (validation_state.needs_uniform_address_for_loop) { if (!require_uniform_address_uniform(validated_shader)) goto fail;
validated_shader->uniforms_size += 4;
}
/* Again, no chance of integer overflow here because the worst case *scenariois8bytesofuniformsplushandlesper8-byte *instruction.
*/
validated_shader->uniforms_src_size =
(validated_shader->uniforms_size + 4 * validated_shader->num_texture_samples);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.