Path: blob/21.2-virgl/src/broadcom/vulkan/v3dv_cl.h
4560 views
/*1* Copyright © 2019 Raspberry Pi2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef V3DV_CL_H24#define V3DV_CL_H2526#include "broadcom/cle/v3d_packet_helpers.h"2728#include "list.h"2930struct v3dv_bo;31struct v3dv_job;32struct v3dv_cl;3334void v3dv_job_add_bo(struct v3dv_job *job, struct v3dv_bo *bo);3536/**37* Undefined structure, used for typechecking that you're passing the pointers38* to these functions correctly.39*/40struct v3dv_cl_out;4142/** A reference to a BO used in the CL packing functions */43struct v3dv_cl_reloc {44struct v3dv_bo *bo;45uint32_t offset;46};4748static inline void49pack_emit_reloc(void *cl, const void *reloc) {}5051#define __gen_user_data struct v3dv_cl52#define __gen_address_type struct v3dv_cl_reloc53#define __gen_address_offset(reloc) (((reloc)->bo ? (reloc)->bo->offset : 0) + \54(reloc)->offset)55#define __gen_emit_reloc cl_pack_emit_reloc56#define __gen_unpack_address(cl, s, e) __unpack_address(cl, s, e)5758struct v3dv_cl {59void *base;60struct v3dv_job *job;61struct v3dv_cl_out *next;62struct v3dv_bo *bo;63uint32_t size;64struct list_head bo_list;65};6667static inline struct v3dv_cl_reloc68__unpack_address(const uint8_t *cl, uint32_t s, uint32_t e)69{70struct v3dv_cl_reloc reloc =71{ NULL, __gen_unpack_uint(cl, s, e) << (31 - (e - s)) };72return reloc;73}7475static inline uint32_t76v3dv_cl_offset(struct v3dv_cl *cl)77{78return (char *)cl->next - (char *)cl->base;79}8081static inline struct v3dv_cl_reloc82v3dv_cl_address(struct v3dv_bo *bo, uint32_t offset)83{84struct v3dv_cl_reloc reloc = {85.bo = bo,86.offset = offset,87};88return reloc;89}9091static inline struct v3dv_cl_reloc92v3dv_cl_get_address(struct v3dv_cl *cl)93{94return (struct v3dv_cl_reloc){ .bo = cl->bo, .offset = v3dv_cl_offset(cl) };95}9697void v3dv_cl_init(struct v3dv_job *job, struct v3dv_cl *cl);98void v3dv_cl_destroy(struct v3dv_cl *cl);99100static inline struct v3dv_cl_out *101cl_start(struct v3dv_cl *cl)102{103return cl->next;104}105106static inline void107cl_end(struct v3dv_cl *cl, struct v3dv_cl_out *next)108{109cl->next = next;110assert(v3dv_cl_offset(cl) <= cl->size);111}112113static inline void114cl_advance(struct v3dv_cl_out **cl, uint32_t n)115{116(*cl) = (struct v3dv_cl_out *)((char *)(*cl) + n);117}118119static inline void120cl_aligned_u32(struct v3dv_cl_out **cl, uint32_t n)121{122*(uint32_t *)(*cl) = n;123cl_advance(cl, 4);124}125126static inline void127cl_aligned_f(struct v3dv_cl_out **cl, float f)128{129cl_aligned_u32(cl, fui(f));130}131132static inline void133cl_aligned_reloc(struct v3dv_cl *cl,134struct v3dv_cl_out **cl_out,135struct v3dv_bo *bo,136uint32_t offset)137{138cl_aligned_u32(cl_out, bo->offset + offset);139v3dv_job_add_bo(cl->job, bo);140}141142uint32_t v3dv_cl_ensure_space(struct v3dv_cl *cl, uint32_t space, uint32_t alignment);143void v3dv_cl_ensure_space_with_branch(struct v3dv_cl *cl, uint32_t space);144145/* We redefine ALIGN as a macro as we want to use cl_aligned_packet_length for146* struct fields147*/148#define ALIGN(value, alignment) \149(((value) + (alignment) - 1) & ~((alignment) - 1))150151#define cl_packet_header(packet) V3DX(packet ## _header)152#define cl_packet_length(packet) V3DX(packet ## _length)153#define cl_aligned_packet_length(packet, alignment) ALIGN(cl_packet_length(packet), alignment)154#define cl_packet_pack(packet) V3DX(packet ## _pack)155#define cl_packet_struct(packet) V3DX(packet)156157/* Macro for setting up an emit of a CL struct. A temporary unpacked struct158* is created, which you get to set fields in of the form:159*160* cl_emit(bcl, FLAT_SHADE_FLAGS, flags) {161* .flags.flat_shade_flags = 1 << 2,162* }163*164* or default values only can be emitted with just:165*166* cl_emit(bcl, FLAT_SHADE_FLAGS, flags);167*168* The trick here is that we make a for loop that will execute the body169* (either the block or the ';' after the macro invocation) exactly once.170*/171#define cl_emit(cl, packet, name) \172for (struct cl_packet_struct(packet) name = { \173cl_packet_header(packet) \174}, \175*_loop_terminate = &name; \176__builtin_expect(_loop_terminate != NULL, 1); \177({ \178struct v3dv_cl_out *cl_out = cl_start(cl); \179cl_packet_pack(packet)(cl, (uint8_t *)cl_out, &name); \180cl_advance(&cl_out, cl_packet_length(packet)); \181cl_end(cl, cl_out); \182_loop_terminate = NULL; \183})) \184185#define cl_emit_with_prepacked(cl, packet, prepacked, name) \186for (struct cl_packet_struct(packet) name = { \187cl_packet_header(packet) \188}, \189*_loop_terminate = &name; \190__builtin_expect(_loop_terminate != NULL, 1); \191({ \192struct v3dv_cl_out *cl_out = cl_start(cl); \193uint8_t packed[cl_packet_length(packet)]; \194cl_packet_pack(packet)(cl, packed, &name); \195for (int _i = 0; _i < cl_packet_length(packet); _i++) \196((uint8_t *)cl_out)[_i] = packed[_i] | (prepacked)[_i]; \197cl_advance(&cl_out, cl_packet_length(packet)); \198cl_end(cl, cl_out); \199_loop_terminate = NULL; \200})) \201202/**203* Helper function called by the XML-generated pack functions for filling in204* an address field in shader records.205*206* Since we have a private address space as of V3D, our BOs can have lifelong207* offsets, and all the kernel needs to know is which BOs need to be paged in208* for this exec.209*/210static inline void211cl_pack_emit_reloc(struct v3dv_cl *cl, const struct v3dv_cl_reloc *reloc)212{213if (reloc->bo)214v3dv_job_add_bo(cl->job, reloc->bo);215}216217#define cl_emit_prepacked_sized(cl, packet, size) do { \218memcpy((cl)->next, packet, size); \219cl_advance(&(cl)->next, size); \220} while (0)221222#define cl_emit_prepacked(cl, packet) \223cl_emit_prepacked_sized(cl, packet, sizeof(*(packet)))224225#define v3dvx_pack(packed, packet, name) \226for (struct cl_packet_struct(packet) name = { \227cl_packet_header(packet) \228}, \229*_loop_terminate = &name; \230__builtin_expect(_loop_terminate != NULL, 1); \231({ \232cl_packet_pack(packet)(NULL, (uint8_t *)packed, &name); \233VG(VALGRIND_CHECK_MEM_IS_DEFINED((uint8_t *)packed, \234cl_packet_length(packet))); \235_loop_terminate = NULL; \236})) \237238#endif /* V3DV_CL_H */239240241