From e5e8dafc991ee922ec159491c680caff0cfe9235 Mon Sep 17 00:00:00 2001 From: "Kalamarz, Lukasz" Date: Tue, 24 Apr 2018 10:32:12 +0200 Subject: lib/intel_batchbuffer: Move batch functions from media/render/gpgpu libs Batch functions were copy/pasted across several libs. With moving it into intel_batchbuffer lib test can now be easly maintained without worrying that we forgot to modify older version of lib. v2: Added documentation into lib and rebased patch v3: Fixed typos and rebased patch v4: Fixed documentation issues v5: Rename, clean up of leftovers from previous version and documentation polishing v6: Fixing assert Signed-off-by: Lukasz Kalamarz Cc: Katarzyna Dec Cc: Radoslaw Szwichtenberg Cc: Daniele Ceraolo Spurio Reviewed-by: Daniele Ceraolo Spurio Reviewed-by: Katarzyna Dec --- lib/intel_batchbuffer.c | 80 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 8 deletions(-) (limited to 'lib/intel_batchbuffer.c') diff --git a/lib/intel_batchbuffer.c b/lib/intel_batchbuffer.c index 53ddae24..f87e6bed 100644 --- a/lib/intel_batchbuffer.c +++ b/lib/intel_batchbuffer.c @@ -65,6 +65,61 @@ * library as a dependency. */ +/** + * intel_batchbuffer_align: + * @batch: batchbuffer object + * @align: value in bytes to which we want to align + * + * Aligns the current in-batch offset to the given value. + * + * Returns: Batchbuffer offset aligned to the given value. + */ +uint32_t +intel_batchbuffer_align(struct intel_batchbuffer *batch, uint32_t align) +{ + uint32_t offset = batch->ptr - batch->buffer; + + offset = ALIGN(offset, align); + batch->ptr = batch->buffer + offset; + return offset; +} + +/** + * intel_batchbuffer_subdata_alloc: + * @batch: batchbuffer object + * @size: amount of bytes need to allocate + * @align: value in bytes to which we want to align + * + * Verify if sufficient @size within @batch is available to deny overflow. + * Then allocate @size bytes within @batch. + * + * Returns: Offset within @batch between allocated subdata and base of @batch. + */ +void * +intel_batchbuffer_subdata_alloc(struct intel_batchbuffer *batch, uint32_t size, + uint32_t align) +{ + uint32_t offset = intel_batchbuffer_align(batch, align); + + igt_assert(size <= intel_batchbuffer_space(batch)); + + batch->ptr += size; + return memset(batch->buffer + offset, 0, size); +} + +/** + * intel_batchbuffer_subdata_offset: + * @batch: batchbuffer object + * @ptr: pointer to given data + * + * Returns: Offset within @batch between @ptr and base of @batch. + */ +uint32_t +intel_batchbuffer_subdata_offset(struct intel_batchbuffer *batch, void *ptr) +{ + return (uint8_t *)ptr - batch->buffer; +} + /** * intel_batchbuffer_reset: * @batch: batchbuffer object @@ -288,22 +343,31 @@ intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch, } /** - * intel_batchbuffer_data: + * intel_batchbuffer_copy_data: * @batch: batchbuffer object * @data: pointer to the data to write into the batchbuffer * @bytes: number of bytes to write into the batchbuffer + * @align: value in bytes to which we want to align * * This transfers the given @data into the batchbuffer. Note that the length - * must be DWORD aligned, i.e. multiples of 32bits. + * must be DWORD aligned, i.e. multiples of 32bits. The caller must + * confirm that there is enough space in the batch for the data to be + * copied. + * + * Returns: Offset of copied data. */ -void -intel_batchbuffer_data(struct intel_batchbuffer *batch, - const void *data, unsigned int bytes) +uint32_t +intel_batchbuffer_copy_data(struct intel_batchbuffer *batch, + const void *data, unsigned int bytes, + uint32_t align) { + uint32_t *subdata; + igt_assert((bytes & 3) == 0); - intel_batchbuffer_require_space(batch, bytes); - memcpy(batch->ptr, data, bytes); - batch->ptr += bytes; + subdata = intel_batchbuffer_subdata_alloc(batch, bytes, align); + memcpy(subdata, data, bytes); + + return intel_batchbuffer_subdata_offset(batch, subdata); } /** -- cgit v1.2.3