101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2014-2017 Broadcom
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
247ec681f3Smrg#ifndef V3D_CL_H
257ec681f3Smrg#define V3D_CL_H
2601e04c3fSmrg
2701e04c3fSmrg#include <stdint.h>
2801e04c3fSmrg
2901e04c3fSmrg#include "util/u_math.h"
3001e04c3fSmrg#include "util/macros.h"
317ec681f3Smrg#include "broadcom/cle/v3d_packet_helpers.h"
3201e04c3fSmrg
3301e04c3fSmrgstruct v3d_bo;
3401e04c3fSmrgstruct v3d_job;
3501e04c3fSmrgstruct v3d_cl;
3601e04c3fSmrg
3701e04c3fSmrg/**
3801e04c3fSmrg * Undefined structure, used for typechecking that you're passing the pointers
3901e04c3fSmrg * to these functions correctly.
4001e04c3fSmrg */
4101e04c3fSmrgstruct v3d_cl_out;
4201e04c3fSmrg
4301e04c3fSmrg/** A reference to a BO used in the CL packing functions */
4401e04c3fSmrgstruct v3d_cl_reloc {
4501e04c3fSmrg        struct v3d_bo *bo;
4601e04c3fSmrg        uint32_t offset;
4701e04c3fSmrg};
4801e04c3fSmrg
4901e04c3fSmrgstatic inline void cl_pack_emit_reloc(struct v3d_cl *cl, const struct v3d_cl_reloc *);
5001e04c3fSmrg
5101e04c3fSmrg#define __gen_user_data struct v3d_cl
5201e04c3fSmrg#define __gen_address_type struct v3d_cl_reloc
5301e04c3fSmrg#define __gen_address_offset(reloc) (((reloc)->bo ? (reloc)->bo->offset : 0) + \
5401e04c3fSmrg                                     (reloc)->offset)
5501e04c3fSmrg#define __gen_emit_reloc cl_pack_emit_reloc
567ec681f3Smrg#define __gen_unpack_address(cl, s, e) __unpack_address(cl, s, e)
577ec681f3Smrg
587ec681f3Smrgstatic inline struct v3d_cl_reloc
597ec681f3Smrg__unpack_address(const uint8_t *cl, uint32_t s, uint32_t e)
607ec681f3Smrg{
617ec681f3Smrg    struct v3d_cl_reloc reloc =
627ec681f3Smrg            { NULL, __gen_unpack_uint(cl, s, e) << (31 - (e - s)) };
637ec681f3Smrg    return reloc;
647ec681f3Smrg}
6501e04c3fSmrg
6601e04c3fSmrgstruct v3d_cl {
6701e04c3fSmrg        void *base;
6801e04c3fSmrg        struct v3d_job *job;
6901e04c3fSmrg        struct v3d_cl_out *next;
7001e04c3fSmrg        struct v3d_bo *bo;
7101e04c3fSmrg        uint32_t size;
7201e04c3fSmrg};
7301e04c3fSmrg
7401e04c3fSmrgvoid v3d_init_cl(struct v3d_job *job, struct v3d_cl *cl);
7501e04c3fSmrgvoid v3d_destroy_cl(struct v3d_cl *cl);
7601e04c3fSmrgvoid v3d_dump_cl(void *cl, uint32_t size, bool is_render);
7701e04c3fSmrguint32_t v3d_gem_hindex(struct v3d_job *job, struct v3d_bo *bo);
7801e04c3fSmrg
7901e04c3fSmrgstruct PACKED unaligned_16 { uint16_t x; };
8001e04c3fSmrgstruct PACKED unaligned_32 { uint32_t x; };
8101e04c3fSmrg
8201e04c3fSmrgstatic inline uint32_t cl_offset(struct v3d_cl *cl)
8301e04c3fSmrg{
8401e04c3fSmrg        return (char *)cl->next - (char *)cl->base;
8501e04c3fSmrg}
8601e04c3fSmrg
8701e04c3fSmrgstatic inline struct v3d_cl_reloc cl_get_address(struct v3d_cl *cl)
8801e04c3fSmrg{
8901e04c3fSmrg        return (struct v3d_cl_reloc){ .bo = cl->bo, .offset = cl_offset(cl) };
9001e04c3fSmrg}
9101e04c3fSmrg
9201e04c3fSmrgstatic inline void
9301e04c3fSmrgcl_advance(struct v3d_cl_out **cl, uint32_t n)
9401e04c3fSmrg{
9501e04c3fSmrg        (*cl) = (struct v3d_cl_out *)((char *)(*cl) + n);
9601e04c3fSmrg}
9701e04c3fSmrg
9801e04c3fSmrgstatic inline struct v3d_cl_out *
9901e04c3fSmrgcl_start(struct v3d_cl *cl)
10001e04c3fSmrg{
10101e04c3fSmrg        return cl->next;
10201e04c3fSmrg}
10301e04c3fSmrg
10401e04c3fSmrgstatic inline void
10501e04c3fSmrgcl_end(struct v3d_cl *cl, struct v3d_cl_out *next)
10601e04c3fSmrg{
10701e04c3fSmrg        cl->next = next;
10801e04c3fSmrg        assert(cl_offset(cl) <= cl->size);
10901e04c3fSmrg}
11001e04c3fSmrg
11101e04c3fSmrg
11201e04c3fSmrgstatic inline void
11301e04c3fSmrgput_unaligned_32(struct v3d_cl_out *ptr, uint32_t val)
11401e04c3fSmrg{
11501e04c3fSmrg        struct unaligned_32 *p = (void *)ptr;
11601e04c3fSmrg        p->x = val;
11701e04c3fSmrg}
11801e04c3fSmrg
11901e04c3fSmrgstatic inline void
12001e04c3fSmrgput_unaligned_16(struct v3d_cl_out *ptr, uint16_t val)
12101e04c3fSmrg{
12201e04c3fSmrg        struct unaligned_16 *p = (void *)ptr;
12301e04c3fSmrg        p->x = val;
12401e04c3fSmrg}
12501e04c3fSmrg
12601e04c3fSmrgstatic inline void
12701e04c3fSmrgcl_u8(struct v3d_cl_out **cl, uint8_t n)
12801e04c3fSmrg{
12901e04c3fSmrg        *(uint8_t *)(*cl) = n;
13001e04c3fSmrg        cl_advance(cl, 1);
13101e04c3fSmrg}
13201e04c3fSmrg
13301e04c3fSmrgstatic inline void
13401e04c3fSmrgcl_u16(struct v3d_cl_out **cl, uint16_t n)
13501e04c3fSmrg{
13601e04c3fSmrg        put_unaligned_16(*cl, n);
13701e04c3fSmrg        cl_advance(cl, 2);
13801e04c3fSmrg}
13901e04c3fSmrg
14001e04c3fSmrgstatic inline void
14101e04c3fSmrgcl_u32(struct v3d_cl_out **cl, uint32_t n)
14201e04c3fSmrg{
14301e04c3fSmrg        put_unaligned_32(*cl, n);
14401e04c3fSmrg        cl_advance(cl, 4);
14501e04c3fSmrg}
14601e04c3fSmrg
14701e04c3fSmrgstatic inline void
14801e04c3fSmrgcl_aligned_u32(struct v3d_cl_out **cl, uint32_t n)
14901e04c3fSmrg{
15001e04c3fSmrg        *(uint32_t *)(*cl) = n;
15101e04c3fSmrg        cl_advance(cl, 4);
15201e04c3fSmrg}
15301e04c3fSmrg
15401e04c3fSmrgstatic inline void
15501e04c3fSmrgcl_aligned_reloc(struct v3d_cl *cl,
15601e04c3fSmrg                 struct v3d_cl_out **cl_out,
15701e04c3fSmrg                 struct v3d_bo *bo, uint32_t offset)
15801e04c3fSmrg{
15901e04c3fSmrg        cl_aligned_u32(cl_out, bo->offset + offset);
16001e04c3fSmrg        v3d_job_add_bo(cl->job, bo);
16101e04c3fSmrg}
16201e04c3fSmrg
16301e04c3fSmrgstatic inline void
16401e04c3fSmrgcl_ptr(struct v3d_cl_out **cl, void *ptr)
16501e04c3fSmrg{
16601e04c3fSmrg        *(struct v3d_cl_out **)(*cl) = ptr;
16701e04c3fSmrg        cl_advance(cl, sizeof(void *));
16801e04c3fSmrg}
16901e04c3fSmrg
17001e04c3fSmrgstatic inline void
17101e04c3fSmrgcl_f(struct v3d_cl_out **cl, float f)
17201e04c3fSmrg{
17301e04c3fSmrg        cl_u32(cl, fui(f));
17401e04c3fSmrg}
17501e04c3fSmrg
17601e04c3fSmrgstatic inline void
17701e04c3fSmrgcl_aligned_f(struct v3d_cl_out **cl, float f)
17801e04c3fSmrg{
17901e04c3fSmrg        cl_aligned_u32(cl, fui(f));
18001e04c3fSmrg}
18101e04c3fSmrg
18201e04c3fSmrg/**
18301e04c3fSmrg * Reference to a BO with its associated offset, used in the pack process.
18401e04c3fSmrg */
18501e04c3fSmrgstatic inline struct v3d_cl_reloc
18601e04c3fSmrgcl_address(struct v3d_bo *bo, uint32_t offset)
18701e04c3fSmrg{
18801e04c3fSmrg        struct v3d_cl_reloc reloc = {
18901e04c3fSmrg                .bo = bo,
19001e04c3fSmrg                .offset = offset,
19101e04c3fSmrg        };
19201e04c3fSmrg        return reloc;
19301e04c3fSmrg}
19401e04c3fSmrg
19501e04c3fSmrguint32_t v3d_cl_ensure_space(struct v3d_cl *cl, uint32_t size, uint32_t align);
19601e04c3fSmrgvoid v3d_cl_ensure_space_with_branch(struct v3d_cl *cl, uint32_t size);
19701e04c3fSmrg
19801e04c3fSmrg#define cl_packet_header(packet) V3DX(packet ## _header)
19901e04c3fSmrg#define cl_packet_length(packet) V3DX(packet ## _length)
20001e04c3fSmrg#define cl_packet_pack(packet)   V3DX(packet ## _pack)
20101e04c3fSmrg#define cl_packet_struct(packet) V3DX(packet)
20201e04c3fSmrg
20301e04c3fSmrgstatic inline void *
20401e04c3fSmrgcl_get_emit_space(struct v3d_cl_out **cl, size_t size)
20501e04c3fSmrg{
20601e04c3fSmrg        void *addr = *cl;
20701e04c3fSmrg        cl_advance(cl, size);
20801e04c3fSmrg        return addr;
20901e04c3fSmrg}
21001e04c3fSmrg
21101e04c3fSmrg/* Macro for setting up an emit of a CL struct.  A temporary unpacked struct
21201e04c3fSmrg * is created, which you get to set fields in of the form:
21301e04c3fSmrg *
21401e04c3fSmrg * cl_emit(bcl, FLAT_SHADE_FLAGS, flags) {
21501e04c3fSmrg *     .flags.flat_shade_flags = 1 << 2,
21601e04c3fSmrg * }
21701e04c3fSmrg *
21801e04c3fSmrg * or default values only can be emitted with just:
21901e04c3fSmrg *
22001e04c3fSmrg * cl_emit(bcl, FLAT_SHADE_FLAGS, flags);
22101e04c3fSmrg *
22201e04c3fSmrg * The trick here is that we make a for loop that will execute the body
22301e04c3fSmrg * (either the block or the ';' after the macro invocation) exactly once.
22401e04c3fSmrg */
22501e04c3fSmrg#define cl_emit(cl, packet, name)                                \
22601e04c3fSmrg        for (struct cl_packet_struct(packet) name = {            \
22701e04c3fSmrg                cl_packet_header(packet)                         \
22801e04c3fSmrg        },                                                       \
22901e04c3fSmrg        *_loop_terminate = &name;                                \
23001e04c3fSmrg        __builtin_expect(_loop_terminate != NULL, 1);            \
23101e04c3fSmrg        ({                                                       \
23201e04c3fSmrg                struct v3d_cl_out *cl_out = cl_start(cl);        \
23301e04c3fSmrg                cl_packet_pack(packet)(cl, (uint8_t *)cl_out, &name); \
23401e04c3fSmrg                cl_advance(&cl_out, cl_packet_length(packet));   \
23501e04c3fSmrg                cl_end(cl, cl_out);                              \
23601e04c3fSmrg                _loop_terminate = NULL;                          \
23701e04c3fSmrg        }))                                                      \
23801e04c3fSmrg
23901e04c3fSmrg#define cl_emit_with_prepacked(cl, packet, prepacked, name)      \
24001e04c3fSmrg        for (struct cl_packet_struct(packet) name = {            \
24101e04c3fSmrg                cl_packet_header(packet)                         \
24201e04c3fSmrg        },                                                       \
24301e04c3fSmrg        *_loop_terminate = &name;                                \
24401e04c3fSmrg        __builtin_expect(_loop_terminate != NULL, 1);            \
24501e04c3fSmrg        ({                                                       \
24601e04c3fSmrg                struct v3d_cl_out *cl_out = cl_start(cl);        \
24701e04c3fSmrg                uint8_t packed[cl_packet_length(packet)];         \
24801e04c3fSmrg                cl_packet_pack(packet)(cl, packed, &name);       \
24901e04c3fSmrg                for (int _i = 0; _i < cl_packet_length(packet); _i++) \
25001e04c3fSmrg                        ((uint8_t *)cl_out)[_i] = packed[_i] | (prepacked)[_i]; \
25101e04c3fSmrg                cl_advance(&cl_out, cl_packet_length(packet));   \
25201e04c3fSmrg                cl_end(cl, cl_out);                              \
25301e04c3fSmrg                _loop_terminate = NULL;                          \
25401e04c3fSmrg        }))                                                      \
25501e04c3fSmrg
25601e04c3fSmrg#define cl_emit_prepacked_sized(cl, packet, size) do {                \
25701e04c3fSmrg        memcpy((cl)->next, packet, size);             \
25801e04c3fSmrg        cl_advance(&(cl)->next, size);                \
25901e04c3fSmrg} while (0)
26001e04c3fSmrg
26101e04c3fSmrg#define cl_emit_prepacked(cl, packet) \
26201e04c3fSmrg        cl_emit_prepacked_sized(cl, packet, sizeof(*(packet)))
26301e04c3fSmrg
26401e04c3fSmrg#define v3dx_pack(packed, packet, name)                          \
26501e04c3fSmrg        for (struct cl_packet_struct(packet) name = {            \
26601e04c3fSmrg                cl_packet_header(packet)                         \
26701e04c3fSmrg        },                                                       \
26801e04c3fSmrg        *_loop_terminate = &name;                                \
26901e04c3fSmrg        __builtin_expect(_loop_terminate != NULL, 1);            \
27001e04c3fSmrg        ({                                                       \
27101e04c3fSmrg                cl_packet_pack(packet)(NULL, (uint8_t *)packed, &name); \
27201e04c3fSmrg                VG(VALGRIND_CHECK_MEM_IS_DEFINED((uint8_t *)packed, \
27301e04c3fSmrg                                                 cl_packet_length(packet))); \
27401e04c3fSmrg                _loop_terminate = NULL;                          \
27501e04c3fSmrg        }))                                                      \
27601e04c3fSmrg
27701e04c3fSmrg/**
27801e04c3fSmrg * Helper function called by the XML-generated pack functions for filling in
27901e04c3fSmrg * an address field in shader records.
28001e04c3fSmrg *
2817ec681f3Smrg * Since we have a private address space as of V3D, our BOs can have lifelong
28201e04c3fSmrg * offsets, and all the kernel needs to know is which BOs need to be paged in
28301e04c3fSmrg * for this exec.
28401e04c3fSmrg */
28501e04c3fSmrgstatic inline void
28601e04c3fSmrgcl_pack_emit_reloc(struct v3d_cl *cl, const struct v3d_cl_reloc *reloc)
28701e04c3fSmrg{
28801e04c3fSmrg        if (reloc->bo)
28901e04c3fSmrg                v3d_job_add_bo(cl->job, reloc->bo);
29001e04c3fSmrg}
29101e04c3fSmrg
2927ec681f3Smrg#endif /* V3D_CL_H */
293