/** * radeon_vce_cs_reloc - command submission relocation * * @p: parser context * @lo: address of lower dword * @hi: address of higher dword * @size: size of checker for relocation buffer * * Patch relocation inside command stream with real buffer address */ int radeon_vce_cs_reloc(struct radeon_cs_parser *p, int lo, int hi, unsigned size) { struct radeon_cs_chunk *relocs_chunk; struct radeon_cs_reloc *reloc; uint64_t start, end, offset; unsigned idx; relocs_chunk = &p->chunks[p->chunk_relocs_idx]; offset = radeon_get_ib_value(p, lo); idx = radeon_get_ib_value(p, hi); if (idx >= relocs_chunk->length_dw) { DRM_ERROR("Relocs at %d after relocations chunk end %d !\n", idx, relocs_chunk->length_dw); return -EINVAL; } reloc = p->relocs_ptr[(idx / 4)]; start = reloc->gpu_offset; end = start + radeon_bo_size(reloc->robj); start += offset; p->ib.ptr[lo] = start & 0xFFFFFFFF; p->ib.ptr[hi] = start >> 32; if (end <= start) { DRM_ERROR("invalid reloc offset %llX!\n", offset); return -EINVAL; } if ((end - start) < size) { DRM_ERROR("buffer to small (%d / %d)!\n", (unsigned)(end - start), size); return -EINVAL; } return 0; }
/** * radeon_vce_cs_reloc - command submission relocation * * @p: parser context * @lo: address of lower dword * @hi: address of higher dword * * Patch relocation inside command stream with real buffer address */ int radeon_vce_cs_reloc(struct radeon_cs_parser *p, int lo, int hi) { struct radeon_cs_chunk *relocs_chunk; uint64_t offset; unsigned idx; relocs_chunk = &p->chunks[p->chunk_relocs_idx]; offset = radeon_get_ib_value(p, lo); idx = radeon_get_ib_value(p, hi); if (idx >= relocs_chunk->length_dw) { DRM_ERROR("Relocs at %d after relocations chunk end %d !\n", idx, relocs_chunk->length_dw); return -EINVAL; } offset += p->relocs_ptr[(idx / 4)]->gpu_offset; p->ib.ptr[lo] = offset & 0xFFFFFFFF; p->ib.ptr[hi] = offset >> 32; return 0; }
/** * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet * @parser: parser structure holding parsing context. * @pkt: where to store packet information * * Assume that chunk_ib_index is properly set. Will return -EINVAL * if packet is bigger than remaining ib size. or if packets is unknown. **/ int radeon_cs_packet_parse(struct radeon_cs_parser *p, struct radeon_cs_packet *pkt, unsigned idx) { struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx]; struct radeon_device *rdev = p->rdev; uint32_t header; if (idx >= ib_chunk->length_dw) { DRM_ERROR("Can not parse packet at %d after CS end %d !\n", idx, ib_chunk->length_dw); return -EINVAL; } header = radeon_get_ib_value(p, idx); pkt->idx = idx; pkt->type = RADEON_CP_PACKET_GET_TYPE(header); pkt->count = RADEON_CP_PACKET_GET_COUNT(header); pkt->one_reg_wr = 0; switch (pkt->type) { case RADEON_PACKET_TYPE0: if (rdev->family < CHIP_R600) { pkt->reg = R100_CP_PACKET0_GET_REG(header); pkt->one_reg_wr = RADEON_CP_PACKET0_GET_ONE_REG_WR(header); } else pkt->reg = R600_CP_PACKET0_GET_REG(header); break; case RADEON_PACKET_TYPE3: pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header); break; case RADEON_PACKET_TYPE2: pkt->count = -1; break; default: DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx); return -EINVAL; } if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) { DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n", pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw); return -EINVAL; } return 0; }
/** * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet * @parser: parser structure holding parsing context. * @data: pointer to relocation data * @offset_start: starting offset * @offset_mask: offset mask (to align start offset on) * @reloc: reloc informations * * Check if next packet is relocation packet3, do bo validation and compute * GPU offset using the provided start. **/ int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p, struct radeon_cs_reloc **cs_reloc, int nomm) { struct radeon_cs_chunk *relocs_chunk; struct radeon_cs_packet p3reloc; unsigned idx; int r; if (p->chunk_relocs_idx == -1) { DRM_ERROR("No relocation chunk !\n"); return -EINVAL; } *cs_reloc = NULL; relocs_chunk = &p->chunks[p->chunk_relocs_idx]; r = radeon_cs_packet_parse(p, &p3reloc, p->idx); if (r) return r; p->idx += p3reloc.count + 2; if (p3reloc.type != RADEON_PACKET_TYPE3 || p3reloc.opcode != RADEON_PACKET3_NOP) { DRM_ERROR("No packet3 for relocation for packet at %d.\n", p3reloc.idx); radeon_cs_dump_packet(p, &p3reloc); return -EINVAL; } idx = radeon_get_ib_value(p, p3reloc.idx + 1); if (idx >= relocs_chunk->length_dw) { DRM_ERROR("Relocs at %d after relocations chunk end %d !\n", idx, relocs_chunk->length_dw); radeon_cs_dump_packet(p, &p3reloc); return -EINVAL; } /* FIXME: we assume reloc size is 4 dwords */ if (nomm) { *cs_reloc = p->relocs; (*cs_reloc)->gpu_offset = (u64)relocs_chunk->kdata[idx + 3] << 32; (*cs_reloc)->gpu_offset |= relocs_chunk->kdata[idx + 0]; } else *cs_reloc = p->relocs_ptr[(idx / 4)]; return 0; }
/** * radeon_vce_cs_parse - parse and validate the command stream * * @p: parser context * */ int radeon_vce_cs_parse(struct radeon_cs_parser *p) { uint32_t handle = 0; bool destroy = false; int i, r; while (p->idx < p->chunks[p->chunk_ib_idx].length_dw) { uint32_t len = radeon_get_ib_value(p, p->idx); uint32_t cmd = radeon_get_ib_value(p, p->idx + 1); if ((len < 8) || (len & 3)) { DRM_ERROR("invalid VCE command length (%d)!\n", len); return -EINVAL; } switch (cmd) { case 0x00000001: // session handle = radeon_get_ib_value(p, p->idx + 2); break; case 0x00000002: // task info case 0x01000001: // create case 0x04000001: // config extension case 0x04000002: // pic control case 0x04000005: // rate control case 0x04000007: // motion estimation case 0x04000008: // rdo break; case 0x03000001: // encode r = radeon_vce_cs_reloc(p, p->idx + 10, p->idx + 9); if (r) return r; r = radeon_vce_cs_reloc(p, p->idx + 12, p->idx + 11); if (r) return r; break; case 0x02000001: // destroy destroy = true; break; case 0x05000001: // context buffer case 0x05000004: // video bitstream buffer case 0x05000005: // feedback buffer r = radeon_vce_cs_reloc(p, p->idx + 3, p->idx + 2); if (r) return r; break; default: DRM_ERROR("invalid VCE command (0x%x)!\n", cmd); return -EINVAL; } p->idx += len / 4; } if (destroy) { /* IB contains a destroy msg, free the handle */ for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) atomic_cmpxchg(&p->rdev->vce.handles[i], handle, 0); return 0; } /* create or encode, validate the handle */ for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) { if (atomic_read(&p->rdev->vce.handles[i]) == handle) return 0; } /* handle not found try to alloc a new one */ for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) { if (!atomic_cmpxchg(&p->rdev->vce.handles[i], 0, handle)) { p->rdev->vce.filp[i] = p->filp; return 0; } } DRM_ERROR("No more free VCE handles!\n"); return -EINVAL; }
/** * radeon_vce_cs_parse - parse and validate the command stream * * @p: parser context * */ int radeon_vce_cs_parse(struct radeon_cs_parser *p) { int session_idx = -1; bool destroyed = false; uint32_t tmp, handle = 0; uint32_t *size = &tmp; int i, r; while (p->idx < p->chunks[p->chunk_ib_idx].length_dw) { uint32_t len = radeon_get_ib_value(p, p->idx); uint32_t cmd = radeon_get_ib_value(p, p->idx + 1); if ((len < 8) || (len & 3)) { DRM_ERROR("invalid VCE command length (%d)!\n", len); return -EINVAL; } if (destroyed) { DRM_ERROR("No other command allowed after destroy!\n"); return -EINVAL; } switch (cmd) { case 0x00000001: // session handle = radeon_get_ib_value(p, p->idx + 2); session_idx = radeon_vce_validate_handle(p, handle); if (session_idx < 0) return session_idx; size = &p->rdev->vce.img_size[session_idx]; break; case 0x00000002: // task info break; case 0x01000001: // create *size = radeon_get_ib_value(p, p->idx + 8) * radeon_get_ib_value(p, p->idx + 10) * 8 * 3 / 2; break; case 0x04000001: // config extension case 0x04000002: // pic control case 0x04000005: // rate control case 0x04000007: // motion estimation case 0x04000008: // rdo break; case 0x03000001: // encode r = radeon_vce_cs_reloc(p, p->idx + 10, p->idx + 9, *size); if (r) return r; r = radeon_vce_cs_reloc(p, p->idx + 12, p->idx + 11, *size / 3); if (r) return r; break; case 0x02000001: // destroy destroyed = true; break; case 0x05000001: // context buffer r = radeon_vce_cs_reloc(p, p->idx + 3, p->idx + 2, *size * 2); if (r) return r; break; case 0x05000004: // video bitstream buffer tmp = radeon_get_ib_value(p, p->idx + 4); r = radeon_vce_cs_reloc(p, p->idx + 3, p->idx + 2, tmp); if (r) return r; break; case 0x05000005: // feedback buffer r = radeon_vce_cs_reloc(p, p->idx + 3, p->idx + 2, 4096); if (r) return r; break; default: DRM_ERROR("invalid VCE command (0x%x)!\n", cmd); return -EINVAL; } if (session_idx == -1) { DRM_ERROR("no session command at start of IB\n"); return -EINVAL; } p->idx += len / 4; } if (destroyed) { /* IB contains a destroy msg, free the handle */ for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) atomic_cmpxchg(&p->rdev->vce.handles[i], handle, 0); } return 0; }