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