1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Intel Corporation
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 shall be included
12bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci/**
24bf215546Sopenharmony_ci * Macro and function definitions needed in order to use genxml.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci * This should only be included in sources compiled per-generation.
27bf215546Sopenharmony_ci */
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "crocus_batch.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "genxml/gen_macros.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#define __gen_address_type struct crocus_address
34bf215546Sopenharmony_ci#define __gen_user_data struct crocus_batch
35bf215546Sopenharmony_ci#define __gen_combine_address crocus_combine_address
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistatic inline void *
38bf215546Sopenharmony_ci__gen_get_batch_dwords(struct crocus_batch *batch, unsigned dwords)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   return crocus_get_command_space(batch, dwords * sizeof(uint32_t));
41bf215546Sopenharmony_ci}
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cistatic inline struct crocus_address
44bf215546Sopenharmony_ci__gen_address_offset(struct crocus_address addr, uint64_t offset)
45bf215546Sopenharmony_ci{
46bf215546Sopenharmony_ci   addr.offset += offset;
47bf215546Sopenharmony_ci   return addr;
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_cistatic uint64_t
51bf215546Sopenharmony_ci__gen_combine_address(struct crocus_batch *batch, void *location,
52bf215546Sopenharmony_ci                      struct crocus_address addr, uint32_t delta)
53bf215546Sopenharmony_ci{
54bf215546Sopenharmony_ci   uint32_t offset = (char *)location - (char *)batch->command.map;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   if (addr.bo == NULL) {
57bf215546Sopenharmony_ci      return addr.offset + delta;
58bf215546Sopenharmony_ci   } else {
59bf215546Sopenharmony_ci      if (GFX_VER < 6 && crocus_ptr_in_state_buffer(batch, location)) {
60bf215546Sopenharmony_ci         offset = (char *) location - (char *) batch->state.map;
61bf215546Sopenharmony_ci         return crocus_state_reloc(batch, offset, addr.bo,
62bf215546Sopenharmony_ci                                   addr.offset + delta,
63bf215546Sopenharmony_ci                                   addr.reloc_flags);
64bf215546Sopenharmony_ci      }
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci      assert(!crocus_ptr_in_state_buffer(batch, location));
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci      offset = (char *) location - (char *) batch->command.map;
69bf215546Sopenharmony_ci      return crocus_command_reloc(batch, offset, addr.bo,
70bf215546Sopenharmony_ci                                  addr.offset + delta,
71bf215546Sopenharmony_ci                                  addr.reloc_flags);
72bf215546Sopenharmony_ci   }
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci#define __gen_address_type struct crocus_address
76bf215546Sopenharmony_ci#define __gen_user_data struct crocus_batch
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci#define __genxml_cmd_length(cmd) cmd ## _length
79bf215546Sopenharmony_ci#define __genxml_cmd_length_bias(cmd) cmd ## _length_bias
80bf215546Sopenharmony_ci#define __genxml_cmd_header(cmd) cmd ## _header
81bf215546Sopenharmony_ci#define __genxml_cmd_pack(cmd) cmd ## _pack
82bf215546Sopenharmony_ci#define __genxml_reg_num(cmd) cmd ## _num
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci#include "genxml/genX_pack.h"
85bf215546Sopenharmony_ci#include "genxml/gen_macros.h"
86bf215546Sopenharmony_ci#include "genxml/genX_bits.h"
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci/* CS_GPR(15) is reserved for combining conditional rendering predicates
89bf215546Sopenharmony_ci * with GL_ARB_indirect_parameters draw number predicates.
90bf215546Sopenharmony_ci */
91bf215546Sopenharmony_ci#define MI_BUILDER_NUM_ALLOC_GPRS 15
92bf215546Sopenharmony_ci#include "common/mi_builder.h"
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci#define _crocus_pack_command(batch, cmd, dst, name)                 \
95bf215546Sopenharmony_ci   for (struct cmd name = { __genxml_cmd_header(cmd) },           \
96bf215546Sopenharmony_ci        *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
97bf215546Sopenharmony_ci        ({ __genxml_cmd_pack(cmd)(batch, (void *)_dst, &name);    \
98bf215546Sopenharmony_ci           _dst = NULL;                                           \
99bf215546Sopenharmony_ci           }))
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci#define crocus_pack_command(cmd, dst, name) \
102bf215546Sopenharmony_ci   _crocus_pack_command(NULL, cmd, dst, name)
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci#define _crocus_pack_state(batch, cmd, dst, name)                   \
105bf215546Sopenharmony_ci   for (struct cmd name = {},                                     \
106bf215546Sopenharmony_ci        *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
107bf215546Sopenharmony_ci        __genxml_cmd_pack(cmd)(batch, (void *)_dst, &name),       \
108bf215546Sopenharmony_ci        _dst = NULL)
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci#define crocus_pack_state(cmd, dst, name)                           \
111bf215546Sopenharmony_ci   _crocus_pack_state(NULL, cmd, dst, name)
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci#define crocus_emit_cmd(batch, cmd, name) \
114bf215546Sopenharmony_ci   _crocus_pack_command(batch, cmd, __gen_get_batch_dwords(batch, __genxml_cmd_length(cmd)), name)
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci#define crocus_emit_merge(batch, dwords0, dwords1, num_dwords)    \
117bf215546Sopenharmony_ci   do {                                                         \
118bf215546Sopenharmony_ci      uint32_t *dw = __gen_get_batch_dwords(batch, num_dwords); \
119bf215546Sopenharmony_ci      for (uint32_t i = 0; i < num_dwords; i++)                 \
120bf215546Sopenharmony_ci         dw[i] = (dwords0)[i] | (dwords1)[i];                   \
121bf215546Sopenharmony_ci      VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, num_dwords));        \
122bf215546Sopenharmony_ci   } while (0)
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci#define crocus_emit_reg(batch, reg, name)                                 \
125bf215546Sopenharmony_ci   for (struct reg name = {}, *_cont = (struct reg *)1; _cont != NULL;  \
126bf215546Sopenharmony_ci        ({                                                              \
127bf215546Sopenharmony_ci            uint32_t _dw[__genxml_cmd_length(reg)];                     \
128bf215546Sopenharmony_ci            __genxml_cmd_pack(reg)(NULL, _dw, &name);                   \
129bf215546Sopenharmony_ci            for (unsigned i = 0; i < __genxml_cmd_length(reg); i++) {   \
130bf215546Sopenharmony_ci               crocus_emit_cmd(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {  \
131bf215546Sopenharmony_ci                  lri.RegisterOffset   = __genxml_reg_num(reg);         \
132bf215546Sopenharmony_ci                  lri.DataDWord        = _dw[i];                        \
133bf215546Sopenharmony_ci               }                                                        \
134bf215546Sopenharmony_ci            }                                                           \
135bf215546Sopenharmony_ci           _cont = NULL;                                                \
136bf215546Sopenharmony_ci         }))
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci/**
140bf215546Sopenharmony_ci * crocus_address constructor helpers:
141bf215546Sopenharmony_ci *
142bf215546Sopenharmony_ci * When using these to construct a CSO, pass NULL for \p bo, and manually
143bf215546Sopenharmony_ci * pin the BO later.  Otherwise, genxml's address handling will add the
144bf215546Sopenharmony_ci * BO to the current batch's validation list at CSO creation time, rather
145bf215546Sopenharmony_ci * than at draw time as desired.
146bf215546Sopenharmony_ci */
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ciUNUSED static struct crocus_address
149bf215546Sopenharmony_ciro_bo(struct crocus_bo *bo, uint64_t offset)
150bf215546Sopenharmony_ci{
151bf215546Sopenharmony_ci   return (struct crocus_address) { .bo = bo, .offset = offset, .reloc_flags = RELOC_32BIT };
152bf215546Sopenharmony_ci}
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ciUNUSED static struct crocus_address
155bf215546Sopenharmony_cirw_bo(struct crocus_bo *bo, uint64_t offset)
156bf215546Sopenharmony_ci{
157bf215546Sopenharmony_ci   return (struct crocus_address) { .bo = bo, .offset = offset, .reloc_flags = RELOC_32BIT | RELOC_WRITE };
158bf215546Sopenharmony_ci}
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ciUNUSED static struct crocus_address
161bf215546Sopenharmony_ciggtt_bo(struct crocus_bo *bo, uint64_t offset)
162bf215546Sopenharmony_ci{
163bf215546Sopenharmony_ci   return (struct crocus_address) { .bo = bo, .offset = offset, .reloc_flags = RELOC_WRITE | RELOC_NEEDS_GGTT };
164bf215546Sopenharmony_ci}
165