1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2014-2017 Broadcom 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#ifndef V3D_CL_H 25bf215546Sopenharmony_ci#define V3D_CL_H 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include <stdint.h> 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include "util/u_math.h" 30bf215546Sopenharmony_ci#include "util/macros.h" 31bf215546Sopenharmony_ci#include "broadcom/cle/v3d_packet_helpers.h" 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_cistruct v3d_bo; 34bf215546Sopenharmony_cistruct v3d_job; 35bf215546Sopenharmony_cistruct v3d_cl; 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci/** 38bf215546Sopenharmony_ci * Undefined structure, used for typechecking that you're passing the pointers 39bf215546Sopenharmony_ci * to these functions correctly. 40bf215546Sopenharmony_ci */ 41bf215546Sopenharmony_cistruct v3d_cl_out; 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci/** A reference to a BO used in the CL packing functions */ 44bf215546Sopenharmony_cistruct v3d_cl_reloc { 45bf215546Sopenharmony_ci struct v3d_bo *bo; 46bf215546Sopenharmony_ci uint32_t offset; 47bf215546Sopenharmony_ci}; 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_cistatic inline void cl_pack_emit_reloc(struct v3d_cl *cl, const struct v3d_cl_reloc *); 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci#define __gen_user_data struct v3d_cl 52bf215546Sopenharmony_ci#define __gen_address_type struct v3d_cl_reloc 53bf215546Sopenharmony_ci#define __gen_address_offset(reloc) (((reloc)->bo ? (reloc)->bo->offset : 0) + \ 54bf215546Sopenharmony_ci (reloc)->offset) 55bf215546Sopenharmony_ci#define __gen_emit_reloc cl_pack_emit_reloc 56bf215546Sopenharmony_ci#define __gen_unpack_address(cl, s, e) __unpack_address(cl, s, e) 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_cistatic inline struct v3d_cl_reloc 59bf215546Sopenharmony_ci__unpack_address(const uint8_t *cl, uint32_t s, uint32_t e) 60bf215546Sopenharmony_ci{ 61bf215546Sopenharmony_ci struct v3d_cl_reloc reloc = 62bf215546Sopenharmony_ci { NULL, __gen_unpack_uint(cl, s, e) << (31 - (e - s)) }; 63bf215546Sopenharmony_ci return reloc; 64bf215546Sopenharmony_ci} 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_cistruct v3d_cl { 67bf215546Sopenharmony_ci void *base; 68bf215546Sopenharmony_ci struct v3d_job *job; 69bf215546Sopenharmony_ci struct v3d_cl_out *next; 70bf215546Sopenharmony_ci struct v3d_bo *bo; 71bf215546Sopenharmony_ci uint32_t size; 72bf215546Sopenharmony_ci}; 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_civoid v3d_init_cl(struct v3d_job *job, struct v3d_cl *cl); 75bf215546Sopenharmony_civoid v3d_destroy_cl(struct v3d_cl *cl); 76bf215546Sopenharmony_civoid v3d_dump_cl(void *cl, uint32_t size, bool is_render); 77bf215546Sopenharmony_ciuint32_t v3d_gem_hindex(struct v3d_job *job, struct v3d_bo *bo); 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_cistruct PACKED unaligned_16 { uint16_t x; }; 80bf215546Sopenharmony_cistruct PACKED unaligned_32 { uint32_t x; }; 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_cistatic inline uint32_t cl_offset(struct v3d_cl *cl) 83bf215546Sopenharmony_ci{ 84bf215546Sopenharmony_ci return (char *)cl->next - (char *)cl->base; 85bf215546Sopenharmony_ci} 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_cistatic inline struct v3d_cl_reloc cl_get_address(struct v3d_cl *cl) 88bf215546Sopenharmony_ci{ 89bf215546Sopenharmony_ci return (struct v3d_cl_reloc){ .bo = cl->bo, .offset = cl_offset(cl) }; 90bf215546Sopenharmony_ci} 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_cistatic inline void 93bf215546Sopenharmony_cicl_advance(struct v3d_cl_out **cl, uint32_t n) 94bf215546Sopenharmony_ci{ 95bf215546Sopenharmony_ci (*cl) = (struct v3d_cl_out *)((char *)(*cl) + n); 96bf215546Sopenharmony_ci} 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_cistatic inline struct v3d_cl_out * 99bf215546Sopenharmony_cicl_start(struct v3d_cl *cl) 100bf215546Sopenharmony_ci{ 101bf215546Sopenharmony_ci return cl->next; 102bf215546Sopenharmony_ci} 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_cistatic inline void 105bf215546Sopenharmony_cicl_end(struct v3d_cl *cl, struct v3d_cl_out *next) 106bf215546Sopenharmony_ci{ 107bf215546Sopenharmony_ci cl->next = next; 108bf215546Sopenharmony_ci assert(cl_offset(cl) <= cl->size); 109bf215546Sopenharmony_ci} 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_cistatic inline void 113bf215546Sopenharmony_ciput_unaligned_32(struct v3d_cl_out *ptr, uint32_t val) 114bf215546Sopenharmony_ci{ 115bf215546Sopenharmony_ci struct unaligned_32 *p = (void *)ptr; 116bf215546Sopenharmony_ci p->x = val; 117bf215546Sopenharmony_ci} 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_cistatic inline void 120bf215546Sopenharmony_ciput_unaligned_16(struct v3d_cl_out *ptr, uint16_t val) 121bf215546Sopenharmony_ci{ 122bf215546Sopenharmony_ci struct unaligned_16 *p = (void *)ptr; 123bf215546Sopenharmony_ci p->x = val; 124bf215546Sopenharmony_ci} 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_cistatic inline void 127bf215546Sopenharmony_cicl_u8(struct v3d_cl_out **cl, uint8_t n) 128bf215546Sopenharmony_ci{ 129bf215546Sopenharmony_ci *(uint8_t *)(*cl) = n; 130bf215546Sopenharmony_ci cl_advance(cl, 1); 131bf215546Sopenharmony_ci} 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_cistatic inline void 134bf215546Sopenharmony_cicl_u16(struct v3d_cl_out **cl, uint16_t n) 135bf215546Sopenharmony_ci{ 136bf215546Sopenharmony_ci put_unaligned_16(*cl, n); 137bf215546Sopenharmony_ci cl_advance(cl, 2); 138bf215546Sopenharmony_ci} 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_cistatic inline void 141bf215546Sopenharmony_cicl_u32(struct v3d_cl_out **cl, uint32_t n) 142bf215546Sopenharmony_ci{ 143bf215546Sopenharmony_ci put_unaligned_32(*cl, n); 144bf215546Sopenharmony_ci cl_advance(cl, 4); 145bf215546Sopenharmony_ci} 146bf215546Sopenharmony_ci 147bf215546Sopenharmony_cistatic inline void 148bf215546Sopenharmony_cicl_aligned_u32(struct v3d_cl_out **cl, uint32_t n) 149bf215546Sopenharmony_ci{ 150bf215546Sopenharmony_ci *(uint32_t *)(*cl) = n; 151bf215546Sopenharmony_ci cl_advance(cl, 4); 152bf215546Sopenharmony_ci} 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_cistatic inline void 155bf215546Sopenharmony_cicl_aligned_reloc(struct v3d_cl *cl, 156bf215546Sopenharmony_ci struct v3d_cl_out **cl_out, 157bf215546Sopenharmony_ci struct v3d_bo *bo, uint32_t offset) 158bf215546Sopenharmony_ci{ 159bf215546Sopenharmony_ci cl_aligned_u32(cl_out, bo->offset + offset); 160bf215546Sopenharmony_ci v3d_job_add_bo(cl->job, bo); 161bf215546Sopenharmony_ci} 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_cistatic inline void 164bf215546Sopenharmony_cicl_ptr(struct v3d_cl_out **cl, void *ptr) 165bf215546Sopenharmony_ci{ 166bf215546Sopenharmony_ci *(struct v3d_cl_out **)(*cl) = ptr; 167bf215546Sopenharmony_ci cl_advance(cl, sizeof(void *)); 168bf215546Sopenharmony_ci} 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_cistatic inline void 171bf215546Sopenharmony_cicl_f(struct v3d_cl_out **cl, float f) 172bf215546Sopenharmony_ci{ 173bf215546Sopenharmony_ci cl_u32(cl, fui(f)); 174bf215546Sopenharmony_ci} 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_cistatic inline void 177bf215546Sopenharmony_cicl_aligned_f(struct v3d_cl_out **cl, float f) 178bf215546Sopenharmony_ci{ 179bf215546Sopenharmony_ci cl_aligned_u32(cl, fui(f)); 180bf215546Sopenharmony_ci} 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_ci/** 183bf215546Sopenharmony_ci * Reference to a BO with its associated offset, used in the pack process. 184bf215546Sopenharmony_ci */ 185bf215546Sopenharmony_cistatic inline struct v3d_cl_reloc 186bf215546Sopenharmony_cicl_address(struct v3d_bo *bo, uint32_t offset) 187bf215546Sopenharmony_ci{ 188bf215546Sopenharmony_ci struct v3d_cl_reloc reloc = { 189bf215546Sopenharmony_ci .bo = bo, 190bf215546Sopenharmony_ci .offset = offset, 191bf215546Sopenharmony_ci }; 192bf215546Sopenharmony_ci return reloc; 193bf215546Sopenharmony_ci} 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ciuint32_t v3d_cl_ensure_space(struct v3d_cl *cl, uint32_t size, uint32_t align); 196bf215546Sopenharmony_civoid v3d_cl_ensure_space_with_branch(struct v3d_cl *cl, uint32_t size); 197bf215546Sopenharmony_ci 198bf215546Sopenharmony_ci#define cl_packet_header(packet) V3DX(packet ## _header) 199bf215546Sopenharmony_ci#define cl_packet_length(packet) V3DX(packet ## _length) 200bf215546Sopenharmony_ci#define cl_packet_pack(packet) V3DX(packet ## _pack) 201bf215546Sopenharmony_ci#define cl_packet_struct(packet) V3DX(packet) 202bf215546Sopenharmony_ci 203bf215546Sopenharmony_cistatic inline void * 204bf215546Sopenharmony_cicl_get_emit_space(struct v3d_cl_out **cl, size_t size) 205bf215546Sopenharmony_ci{ 206bf215546Sopenharmony_ci void *addr = *cl; 207bf215546Sopenharmony_ci cl_advance(cl, size); 208bf215546Sopenharmony_ci return addr; 209bf215546Sopenharmony_ci} 210bf215546Sopenharmony_ci 211bf215546Sopenharmony_ci/* Macro for setting up an emit of a CL struct. A temporary unpacked struct 212bf215546Sopenharmony_ci * is created, which you get to set fields in of the form: 213bf215546Sopenharmony_ci * 214bf215546Sopenharmony_ci * cl_emit(bcl, FLAT_SHADE_FLAGS, flags) { 215bf215546Sopenharmony_ci * .flags.flat_shade_flags = 1 << 2, 216bf215546Sopenharmony_ci * } 217bf215546Sopenharmony_ci * 218bf215546Sopenharmony_ci * or default values only can be emitted with just: 219bf215546Sopenharmony_ci * 220bf215546Sopenharmony_ci * cl_emit(bcl, FLAT_SHADE_FLAGS, flags); 221bf215546Sopenharmony_ci * 222bf215546Sopenharmony_ci * The trick here is that we make a for loop that will execute the body 223bf215546Sopenharmony_ci * (either the block or the ';' after the macro invocation) exactly once. 224bf215546Sopenharmony_ci */ 225bf215546Sopenharmony_ci#define cl_emit(cl, packet, name) \ 226bf215546Sopenharmony_ci for (struct cl_packet_struct(packet) name = { \ 227bf215546Sopenharmony_ci cl_packet_header(packet) \ 228bf215546Sopenharmony_ci }, \ 229bf215546Sopenharmony_ci *_loop_terminate = &name; \ 230bf215546Sopenharmony_ci __builtin_expect(_loop_terminate != NULL, 1); \ 231bf215546Sopenharmony_ci ({ \ 232bf215546Sopenharmony_ci struct v3d_cl_out *cl_out = cl_start(cl); \ 233bf215546Sopenharmony_ci cl_packet_pack(packet)(cl, (uint8_t *)cl_out, &name); \ 234bf215546Sopenharmony_ci cl_advance(&cl_out, cl_packet_length(packet)); \ 235bf215546Sopenharmony_ci cl_end(cl, cl_out); \ 236bf215546Sopenharmony_ci _loop_terminate = NULL; \ 237bf215546Sopenharmony_ci })) \ 238bf215546Sopenharmony_ci 239bf215546Sopenharmony_ci#define cl_emit_with_prepacked(cl, packet, prepacked, name) \ 240bf215546Sopenharmony_ci for (struct cl_packet_struct(packet) name = { \ 241bf215546Sopenharmony_ci cl_packet_header(packet) \ 242bf215546Sopenharmony_ci }, \ 243bf215546Sopenharmony_ci *_loop_terminate = &name; \ 244bf215546Sopenharmony_ci __builtin_expect(_loop_terminate != NULL, 1); \ 245bf215546Sopenharmony_ci ({ \ 246bf215546Sopenharmony_ci struct v3d_cl_out *cl_out = cl_start(cl); \ 247bf215546Sopenharmony_ci uint8_t packed[cl_packet_length(packet)]; \ 248bf215546Sopenharmony_ci cl_packet_pack(packet)(cl, packed, &name); \ 249bf215546Sopenharmony_ci for (int _i = 0; _i < cl_packet_length(packet); _i++) \ 250bf215546Sopenharmony_ci ((uint8_t *)cl_out)[_i] = packed[_i] | (prepacked)[_i]; \ 251bf215546Sopenharmony_ci cl_advance(&cl_out, cl_packet_length(packet)); \ 252bf215546Sopenharmony_ci cl_end(cl, cl_out); \ 253bf215546Sopenharmony_ci _loop_terminate = NULL; \ 254bf215546Sopenharmony_ci })) \ 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_ci#define cl_emit_prepacked_sized(cl, packet, size) do { \ 257bf215546Sopenharmony_ci memcpy((cl)->next, packet, size); \ 258bf215546Sopenharmony_ci cl_advance(&(cl)->next, size); \ 259bf215546Sopenharmony_ci} while (0) 260bf215546Sopenharmony_ci 261bf215546Sopenharmony_ci#define cl_emit_prepacked(cl, packet) \ 262bf215546Sopenharmony_ci cl_emit_prepacked_sized(cl, packet, sizeof(*(packet))) 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_ci#define v3dx_pack(packed, packet, name) \ 265bf215546Sopenharmony_ci for (struct cl_packet_struct(packet) name = { \ 266bf215546Sopenharmony_ci cl_packet_header(packet) \ 267bf215546Sopenharmony_ci }, \ 268bf215546Sopenharmony_ci *_loop_terminate = &name; \ 269bf215546Sopenharmony_ci __builtin_expect(_loop_terminate != NULL, 1); \ 270bf215546Sopenharmony_ci ({ \ 271bf215546Sopenharmony_ci cl_packet_pack(packet)(NULL, (uint8_t *)packed, &name); \ 272bf215546Sopenharmony_ci VG(VALGRIND_CHECK_MEM_IS_DEFINED((uint8_t *)packed, \ 273bf215546Sopenharmony_ci cl_packet_length(packet))); \ 274bf215546Sopenharmony_ci _loop_terminate = NULL; \ 275bf215546Sopenharmony_ci })) \ 276bf215546Sopenharmony_ci 277bf215546Sopenharmony_ci/** 278bf215546Sopenharmony_ci * Helper function called by the XML-generated pack functions for filling in 279bf215546Sopenharmony_ci * an address field in shader records. 280bf215546Sopenharmony_ci * 281bf215546Sopenharmony_ci * Since we have a private address space as of V3D, our BOs can have lifelong 282bf215546Sopenharmony_ci * offsets, and all the kernel needs to know is which BOs need to be paged in 283bf215546Sopenharmony_ci * for this exec. 284bf215546Sopenharmony_ci */ 285bf215546Sopenharmony_cistatic inline void 286bf215546Sopenharmony_cicl_pack_emit_reloc(struct v3d_cl *cl, const struct v3d_cl_reloc *reloc) 287bf215546Sopenharmony_ci{ 288bf215546Sopenharmony_ci if (reloc->bo) 289bf215546Sopenharmony_ci v3d_job_add_bo(cl->job, reloc->bo); 290bf215546Sopenharmony_ci} 291bf215546Sopenharmony_ci 292bf215546Sopenharmony_ci#endif /* V3D_CL_H */ 293