2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "r600_context.h"
25 #include "util/u_memory.h"
30 int r700_bc_alu_build(struct r600_bc
*bc
, struct r600_bc_alu
*alu
, unsigned id
);
32 static struct r600_bc_cf
*r600_bc_cf(void)
34 struct r600_bc_cf
*cf
= CALLOC_STRUCT(r600_bc_cf
);
38 LIST_INITHEAD(&cf
->list
);
39 LIST_INITHEAD(&cf
->alu
);
40 LIST_INITHEAD(&cf
->vtx
);
41 LIST_INITHEAD(&cf
->tex
);
45 static struct r600_bc_alu
*r600_bc_alu(void)
47 struct r600_bc_alu
*alu
= CALLOC_STRUCT(r600_bc_alu
);
51 LIST_INITHEAD(&alu
->list
);
55 static struct r600_bc_vtx
*r600_bc_vtx(void)
57 struct r600_bc_vtx
*vtx
= CALLOC_STRUCT(r600_bc_vtx
);
61 LIST_INITHEAD(&vtx
->list
);
65 static struct r600_bc_tex
*r600_bc_tex(void)
67 struct r600_bc_tex
*tex
= CALLOC_STRUCT(r600_bc_tex
);
71 LIST_INITHEAD(&tex
->list
);
75 int r600_bc_init(struct r600_bc
*bc
, enum radeon_family family
)
77 LIST_INITHEAD(&bc
->cf
);
82 static int r600_bc_add_cf(struct r600_bc
*bc
)
84 struct r600_bc_cf
*cf
= r600_bc_cf();
88 LIST_ADDTAIL(&cf
->list
, &bc
->cf
);
90 cf
->id
= bc
->cf_last
->id
+ 2;
97 int r600_bc_add_output(struct r600_bc
*bc
, const struct r600_bc_output
*output
)
101 r
= r600_bc_add_cf(bc
);
104 bc
->cf_last
->inst
= output
->inst
;
105 memcpy(&bc
->cf_last
->output
, output
, sizeof(struct r600_bc_output
));
109 int r600_bc_add_alu(struct r600_bc
*bc
, const struct r600_bc_alu
*alu
)
111 struct r600_bc_alu
*nalu
= r600_bc_alu();
112 struct r600_bc_alu
*lalu
;
117 memcpy(nalu
, alu
, sizeof(struct r600_bc_alu
));
120 /* cf can contains only alu or only vtx or only tex */
121 if (bc
->cf_last
== NULL
|| bc
->cf_last
->inst
!= (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3)) {
122 r
= r600_bc_add_cf(bc
);
127 bc
->cf_last
->inst
= V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3;
129 /* number of gpr == the last gpr used in any alu */
130 for (i
= 0; i
< 3; i
++) {
131 if (alu
->src
[i
].sel
>= bc
->ngpr
&& alu
->src
[i
].sel
< 128) {
132 bc
->ngpr
= alu
->src
[i
].sel
+ 1;
134 /* compute how many literal are needed
135 * either 2 or 4 literals
137 if (alu
->src
[i
].sel
== 253) {
138 if (((alu
->src
[i
].chan
+ 2) & 0x6) > nalu
->nliteral
) {
139 nalu
->nliteral
= (alu
->src
[i
].chan
+ 2) & 0x6;
143 if (!LIST_IS_EMPTY(&bc
->cf_last
->alu
)) {
144 lalu
= LIST_ENTRY(struct r600_bc_alu
, bc
->cf_last
->alu
.prev
, list
);
145 if (!lalu
->last
&& lalu
->nliteral
> nalu
->nliteral
) {
146 nalu
->nliteral
= lalu
->nliteral
;
149 if (alu
->dst
.sel
>= bc
->ngpr
) {
150 bc
->ngpr
= alu
->dst
.sel
+ 1;
152 LIST_ADDTAIL(&nalu
->list
, &bc
->cf_last
->alu
);
153 /* each alu use 2 dwords */
154 bc
->cf_last
->ndw
+= 2;
159 int r600_bc_add_literal(struct r600_bc
*bc
, const u32
*value
)
161 struct r600_bc_alu
*alu
;
163 if (bc
->cf_last
== NULL
) {
164 R600_ERR("no last CF\n");
167 if (bc
->cf_last
->inst
== V_SQ_CF_WORD1_SQ_CF_INST_TEX
) {
170 if (bc
->cf_last
->inst
!= (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3) ||
171 LIST_IS_EMPTY(&bc
->cf_last
->alu
)) {
172 R600_ERR("last CF is not ALU (%p)\n", bc
->cf_last
);
175 alu
= LIST_ENTRY(struct r600_bc_alu
, bc
->cf_last
->alu
.prev
, list
);
176 if (!alu
->last
|| !alu
->nliteral
) {
179 memcpy(alu
->value
, value
, 4 * 4);
180 bc
->cf_last
->ndw
+= alu
->nliteral
;
181 bc
->ndw
+= alu
->nliteral
;
185 int r600_bc_add_vtx(struct r600_bc
*bc
, const struct r600_bc_vtx
*vtx
)
187 struct r600_bc_vtx
*nvtx
= r600_bc_vtx();
192 memcpy(nvtx
, vtx
, sizeof(struct r600_bc_vtx
));
194 /* cf can contains only alu or only vtx or only tex */
195 if (bc
->cf_last
== NULL
||
196 (bc
->cf_last
->inst
!= V_SQ_CF_WORD1_SQ_CF_INST_VTX
&&
197 bc
->cf_last
->inst
!= V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC
)) {
198 r
= r600_bc_add_cf(bc
);
203 bc
->cf_last
->inst
= V_SQ_CF_WORD1_SQ_CF_INST_VTX
;
205 LIST_ADDTAIL(&nvtx
->list
, &bc
->cf_last
->vtx
);
206 /* each fetch use 4 dwords */
207 bc
->cf_last
->ndw
+= 4;
212 int r600_bc_add_tex(struct r600_bc
*bc
, const struct r600_bc_tex
*tex
)
214 struct r600_bc_tex
*ntex
= r600_bc_tex();
219 memcpy(ntex
, tex
, sizeof(struct r600_bc_tex
));
221 /* cf can contains only alu or only vtx or only tex */
222 if (bc
->cf_last
== NULL
||
223 bc
->cf_last
->inst
!= V_SQ_CF_WORD1_SQ_CF_INST_TEX
) {
224 r
= r600_bc_add_cf(bc
);
229 bc
->cf_last
->inst
= V_SQ_CF_WORD1_SQ_CF_INST_TEX
;
231 LIST_ADDTAIL(&ntex
->list
, &bc
->cf_last
->tex
);
232 /* each texture fetch use 4 dwords */
233 bc
->cf_last
->ndw
+= 4;
238 static int r600_bc_vtx_build(struct r600_bc
*bc
, struct r600_bc_vtx
*vtx
, unsigned id
)
240 bc
->bytecode
[id
++] = S_SQ_VTX_WORD0_BUFFER_ID(vtx
->buffer_id
) |
241 S_SQ_VTX_WORD0_SRC_GPR(vtx
->src_gpr
) |
242 S_SQ_VTX_WORD0_SRC_SEL_X(vtx
->src_sel_x
) |
243 S_SQ_VTX_WORD0_MEGA_FETCH_COUNT(vtx
->mega_fetch_count
);
244 bc
->bytecode
[id
++] = S_SQ_VTX_WORD1_DST_SEL_X(vtx
->dst_sel_x
) |
245 S_SQ_VTX_WORD1_DST_SEL_Y(vtx
->dst_sel_y
) |
246 S_SQ_VTX_WORD1_DST_SEL_Z(vtx
->dst_sel_z
) |
247 S_SQ_VTX_WORD1_DST_SEL_W(vtx
->dst_sel_w
) |
248 S_SQ_VTX_WORD1_USE_CONST_FIELDS(1) |
249 S_SQ_VTX_WORD1_GPR_DST_GPR(vtx
->dst_gpr
);
250 bc
->bytecode
[id
++] = S_SQ_VTX_WORD2_MEGA_FETCH(1);
251 bc
->bytecode
[id
++] = 0;
255 static int r600_bc_tex_build(struct r600_bc
*bc
, struct r600_bc_tex
*tex
, unsigned id
)
257 bc
->bytecode
[id
++] = S_SQ_TEX_WORD0_TEX_INST(tex
->inst
) |
258 S_SQ_TEX_WORD0_RESOURCE_ID(tex
->resource_id
) |
259 S_SQ_TEX_WORD0_SRC_GPR(tex
->src_gpr
) |
260 S_SQ_TEX_WORD0_SRC_REL(tex
->src_rel
);
261 bc
->bytecode
[id
++] = S_SQ_TEX_WORD1_DST_GPR(tex
->dst_gpr
) |
262 S_SQ_TEX_WORD1_DST_REL(tex
->dst_rel
) |
263 S_SQ_TEX_WORD1_DST_SEL_X(tex
->dst_sel_x
) |
264 S_SQ_TEX_WORD1_DST_SEL_Y(tex
->dst_sel_y
) |
265 S_SQ_TEX_WORD1_DST_SEL_Z(tex
->dst_sel_z
) |
266 S_SQ_TEX_WORD1_DST_SEL_W(tex
->dst_sel_w
) |
267 S_SQ_TEX_WORD1_LOD_BIAS(tex
->lod_bias
) |
268 S_SQ_TEX_WORD1_COORD_TYPE_X(tex
->coord_type_x
) |
269 S_SQ_TEX_WORD1_COORD_TYPE_Y(tex
->coord_type_y
) |
270 S_SQ_TEX_WORD1_COORD_TYPE_Z(tex
->coord_type_z
) |
271 S_SQ_TEX_WORD1_COORD_TYPE_W(tex
->coord_type_w
);
272 bc
->bytecode
[id
++] = S_SQ_TEX_WORD2_OFFSET_X(tex
->offset_x
) |
273 S_SQ_TEX_WORD2_OFFSET_Y(tex
->offset_y
) |
274 S_SQ_TEX_WORD2_OFFSET_Z(tex
->offset_z
) |
275 S_SQ_TEX_WORD2_SAMPLER_ID(tex
->sampler_id
) |
276 S_SQ_TEX_WORD2_SRC_SEL_X(tex
->src_sel_x
) |
277 S_SQ_TEX_WORD2_SRC_SEL_Y(tex
->src_sel_y
) |
278 S_SQ_TEX_WORD2_SRC_SEL_Z(tex
->src_sel_z
) |
279 S_SQ_TEX_WORD2_SRC_SEL_W(tex
->src_sel_w
);
280 bc
->bytecode
[id
++] = 0;
284 int r600_bc_alu_build(struct r600_bc
*bc
, struct r600_bc_alu
*alu
, unsigned id
)
288 /* don't replace gpr by pv or ps for destination register */
290 bc
->bytecode
[id
++] = S_SQ_ALU_WORD0_SRC0_SEL(alu
->src
[0].sel
) |
291 S_SQ_ALU_WORD0_SRC0_CHAN(alu
->src
[0].chan
) |
292 S_SQ_ALU_WORD0_SRC1_SEL(alu
->src
[1].sel
) |
293 S_SQ_ALU_WORD0_SRC1_CHAN(alu
->src
[1].chan
) |
294 S_SQ_ALU_WORD0_LAST(alu
->last
);
295 bc
->bytecode
[id
++] = S_SQ_ALU_WORD1_DST_GPR(alu
->dst
.sel
) |
296 S_SQ_ALU_WORD1_DST_CHAN(alu
->dst
.chan
) |
297 S_SQ_ALU_WORD1_CLAMP(alu
->dst
.clamp
) |
298 S_SQ_ALU_WORD1_OP3_SRC2_SEL(alu
->src
[2].sel
) |
299 S_SQ_ALU_WORD1_OP3_SRC2_CHAN(alu
->src
[2].chan
) |
300 S_SQ_ALU_WORD1_OP3_SRC2_NEG(alu
->src
[2].neg
) |
301 S_SQ_ALU_WORD1_OP3_ALU_INST(alu
->inst
) |
302 S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
304 bc
->bytecode
[id
++] = S_SQ_ALU_WORD0_SRC0_SEL(alu
->src
[0].sel
) |
305 S_SQ_ALU_WORD0_SRC0_CHAN(alu
->src
[0].chan
) |
306 S_SQ_ALU_WORD0_SRC0_NEG(alu
->src
[0].neg
) |
307 S_SQ_ALU_WORD0_SRC1_SEL(alu
->src
[1].sel
) |
308 S_SQ_ALU_WORD0_SRC1_CHAN(alu
->src
[1].chan
) |
309 S_SQ_ALU_WORD0_SRC1_NEG(alu
->src
[1].neg
) |
310 S_SQ_ALU_WORD0_LAST(alu
->last
);
311 bc
->bytecode
[id
++] = S_SQ_ALU_WORD1_DST_GPR(alu
->dst
.sel
) |
312 S_SQ_ALU_WORD1_DST_CHAN(alu
->dst
.chan
) |
313 S_SQ_ALU_WORD1_CLAMP(alu
->dst
.clamp
) |
314 S_SQ_ALU_WORD1_OP2_SRC0_ABS(alu
->src
[0].abs
) |
315 S_SQ_ALU_WORD1_OP2_SRC1_ABS(alu
->src
[1].abs
) |
316 S_SQ_ALU_WORD1_OP2_WRITE_MASK(alu
->dst
.write
) |
317 S_SQ_ALU_WORD1_OP2_ALU_INST(alu
->inst
) |
318 S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
321 for (i
= 0; i
< alu
->nliteral
; i
++) {
322 bc
->bytecode
[id
++] = alu
->value
[i
];
328 int r600_bc_cf_build(struct r600_bc
*bc
, struct r600_bc_cf
*cf
)
330 unsigned id
= cf
->id
;
333 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3):
334 bc
->bytecode
[id
++] = S_SQ_CF_ALU_WORD0_ADDR(cf
->addr
>> 1);
335 bc
->bytecode
[id
++] = S_SQ_CF_ALU_WORD1_CF_INST(cf
->inst
>> 3) |
336 S_SQ_CF_ALU_WORD1_BARRIER(1) |
337 S_SQ_CF_ALU_WORD1_COUNT((cf
->ndw
/ 2) - 1);
339 case V_SQ_CF_WORD1_SQ_CF_INST_TEX
:
340 case V_SQ_CF_WORD1_SQ_CF_INST_VTX
:
341 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC
:
342 bc
->bytecode
[id
++] = S_SQ_CF_WORD0_ADDR(cf
->addr
>> 1);
343 bc
->bytecode
[id
++] = S_SQ_CF_WORD1_CF_INST(cf
->inst
) |
344 S_SQ_CF_WORD1_BARRIER(1) |
345 S_SQ_CF_WORD1_COUNT((cf
->ndw
/ 4) - 1);
347 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT
:
348 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE
:
349 bc
->bytecode
[id
++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf
->output
.gpr
) |
350 S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf
->output
.elem_size
) |
351 S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf
->output
.array_base
) |
352 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf
->output
.type
);
353 bc
->bytecode
[id
++] = S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(cf
->output
.swizzle_x
) |
354 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(cf
->output
.swizzle_y
) |
355 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(cf
->output
.swizzle_z
) |
356 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(cf
->output
.swizzle_w
) |
357 S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf
->output
.barrier
) |
358 S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(cf
->output
.inst
) |
359 S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf
->output
.end_of_program
);
362 R600_ERR("unsupported CF instruction (0x%X)\n", cf
->inst
);
368 int r600_bc_build(struct r600_bc
*bc
)
370 struct r600_bc_cf
*cf
;
371 struct r600_bc_alu
*alu
;
372 struct r600_bc_vtx
*vtx
;
373 struct r600_bc_tex
*tex
;
378 /* first path compute addr of each CF block */
379 /* addr start after all the CF instructions */
380 addr
= bc
->cf_last
->id
+ 2;
381 LIST_FOR_EACH_ENTRY(cf
, &bc
->cf
, list
) {
383 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3):
385 case V_SQ_CF_WORD1_SQ_CF_INST_TEX
:
386 case V_SQ_CF_WORD1_SQ_CF_INST_VTX
:
387 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC
:
388 /* fetch node need to be 16 bytes aligned*/
390 addr
&= 0xFFFFFFFCUL
;
392 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT
:
393 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE
:
396 R600_ERR("unsupported CF instruction (0x%X)\n", cf
->inst
);
401 bc
->ndw
= cf
->addr
+ cf
->ndw
;
404 bc
->bytecode
= calloc(1, bc
->ndw
* 4);
405 if (bc
->bytecode
== NULL
)
407 LIST_FOR_EACH_ENTRY(cf
, &bc
->cf
, list
) {
409 r
= r600_bc_cf_build(bc
, cf
);
413 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU
<< 3):
414 LIST_FOR_EACH_ENTRY(alu
, &cf
->alu
, list
) {
415 switch (bc
->family
) {
424 r
= r600_bc_alu_build(bc
, alu
, addr
);
430 r
= r700_bc_alu_build(bc
, alu
, addr
);
433 R600_ERR("unknown family %d\n", bc
->family
);
440 addr
+= alu
->nliteral
;
444 case V_SQ_CF_WORD1_SQ_CF_INST_VTX
:
445 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC
:
446 LIST_FOR_EACH_ENTRY(vtx
, &cf
->vtx
, list
) {
447 r
= r600_bc_vtx_build(bc
, vtx
, addr
);
453 case V_SQ_CF_WORD1_SQ_CF_INST_TEX
:
454 LIST_FOR_EACH_ENTRY(tex
, &cf
->tex
, list
) {
455 r
= r600_bc_tex_build(bc
, tex
, addr
);
461 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT
:
462 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE
:
465 R600_ERR("unsupported CF instruction (0x%X)\n", cf
->inst
);