a0dc1deb8eba7d2e0b0a4ccdab52cf66b3c05cc0
[mesa.git] / src / gallium / drivers / r600 / r600_asm.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * 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:
10 *
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
13 * Software.
14 *
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.
22 */
23 #include "r600_sq.h"
24 #include "r600_opcodes.h"
25 #include "r600_formats.h"
26 #include "r600_shader.h"
27 #include "r600d.h"
28
29 #include <errno.h>
30 #include <byteswap.h>
31 #include "util/u_dump.h"
32 #include "util/u_memory.h"
33 #include "pipe/p_shader_tokens.h"
34
35 #define NUM_OF_CYCLES 3
36 #define NUM_OF_COMPONENTS 4
37
38 static inline unsigned int r600_bytecode_get_num_operands(
39 struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
40 {
41 return r600_isa_alu(alu->op)->src_count;
42 }
43
44 int r700_bytecode_alu_build(struct r600_bytecode *bc,
45 struct r600_bytecode_alu *alu, unsigned id);
46
47 static struct r600_bytecode_cf *r600_bytecode_cf(void)
48 {
49 struct r600_bytecode_cf *cf = CALLOC_STRUCT(r600_bytecode_cf);
50
51 if (cf == NULL)
52 return NULL;
53 LIST_INITHEAD(&cf->list);
54 LIST_INITHEAD(&cf->alu);
55 LIST_INITHEAD(&cf->vtx);
56 LIST_INITHEAD(&cf->tex);
57 return cf;
58 }
59
60 static struct r600_bytecode_alu *r600_bytecode_alu(void)
61 {
62 struct r600_bytecode_alu *alu = CALLOC_STRUCT(r600_bytecode_alu);
63
64 if (alu == NULL)
65 return NULL;
66 LIST_INITHEAD(&alu->list);
67 return alu;
68 }
69
70 static struct r600_bytecode_vtx *r600_bytecode_vtx(void)
71 {
72 struct r600_bytecode_vtx *vtx = CALLOC_STRUCT(r600_bytecode_vtx);
73
74 if (vtx == NULL)
75 return NULL;
76 LIST_INITHEAD(&vtx->list);
77 return vtx;
78 }
79
80 static struct r600_bytecode_tex *r600_bytecode_tex(void)
81 {
82 struct r600_bytecode_tex *tex = CALLOC_STRUCT(r600_bytecode_tex);
83
84 if (tex == NULL)
85 return NULL;
86 LIST_INITHEAD(&tex->list);
87 return tex;
88 }
89
90 static unsigned stack_entry_size(enum radeon_family chip) {
91 /* Wavefront size:
92 * 64: R600/RV670/RV770/Cypress/R740/Barts/Turks/Caicos/
93 * Aruba/Sumo/Sumo2/redwood/juniper
94 * 32: R630/R730/R710/Palm/Cedar
95 * 16: R610/Rs780
96 *
97 * Stack row size:
98 * Wavefront Size 16 32 48 64
99 * Columns per Row (R6xx/R7xx/R8xx only) 8 8 4 4
100 * Columns per Row (R9xx+) 8 4 4 4 */
101
102 switch (chip) {
103 /* FIXME: are some chips missing here? */
104 /* wavefront size 16 */
105 case CHIP_RV610:
106 case CHIP_RS780:
107 case CHIP_RV620:
108 case CHIP_RS880:
109 /* wavefront size 32 */
110 case CHIP_RV630:
111 case CHIP_RV635:
112 case CHIP_RV730:
113 case CHIP_RV710:
114 case CHIP_PALM:
115 case CHIP_CEDAR:
116 return 8;
117
118 /* wavefront size 64 */
119 default:
120 return 4;
121 }
122 }
123
124 void r600_bytecode_init(struct r600_bytecode *bc,
125 enum chip_class chip_class,
126 enum radeon_family family,
127 enum r600_msaa_texture_mode msaa_texture_mode)
128 {
129 if ((chip_class == R600) &&
130 (family != CHIP_RV670 && family != CHIP_RS780 && family != CHIP_RS880)) {
131 bc->ar_handling = AR_HANDLE_RV6XX;
132 bc->r6xx_nop_after_rel_dst = 1;
133 } else {
134 bc->ar_handling = AR_HANDLE_NORMAL;
135 bc->r6xx_nop_after_rel_dst = 0;
136 }
137
138 LIST_INITHEAD(&bc->cf);
139 bc->chip_class = chip_class;
140 bc->msaa_texture_mode = msaa_texture_mode;
141 bc->stack.entry_size = stack_entry_size(family);
142 }
143
144 int r600_bytecode_add_cf(struct r600_bytecode *bc)
145 {
146 struct r600_bytecode_cf *cf = r600_bytecode_cf();
147
148 if (cf == NULL)
149 return -ENOMEM;
150 LIST_ADDTAIL(&cf->list, &bc->cf);
151 if (bc->cf_last) {
152 cf->id = bc->cf_last->id + 2;
153 if (bc->cf_last->eg_alu_extended) {
154 /* take into account extended alu size */
155 cf->id += 2;
156 bc->ndw += 2;
157 }
158 }
159 bc->cf_last = cf;
160 bc->ncf++;
161 bc->ndw += 2;
162 bc->force_add_cf = 0;
163 bc->ar_loaded = 0;
164 return 0;
165 }
166
167 int r600_bytecode_add_output(struct r600_bytecode *bc,
168 const struct r600_bytecode_output *output)
169 {
170 int r;
171
172 if (output->gpr >= bc->ngpr)
173 bc->ngpr = output->gpr + 1;
174
175 if (bc->cf_last && (bc->cf_last->op == output->op ||
176 (bc->cf_last->op == CF_OP_EXPORT &&
177 output->op == CF_OP_EXPORT_DONE)) &&
178 output->type == bc->cf_last->output.type &&
179 output->elem_size == bc->cf_last->output.elem_size &&
180 output->swizzle_x == bc->cf_last->output.swizzle_x &&
181 output->swizzle_y == bc->cf_last->output.swizzle_y &&
182 output->swizzle_z == bc->cf_last->output.swizzle_z &&
183 output->swizzle_w == bc->cf_last->output.swizzle_w &&
184 output->comp_mask == bc->cf_last->output.comp_mask &&
185 (output->burst_count + bc->cf_last->output.burst_count) <= 16) {
186
187 if ((output->gpr + output->burst_count) == bc->cf_last->output.gpr &&
188 (output->array_base + output->burst_count) == bc->cf_last->output.array_base) {
189
190 bc->cf_last->output.end_of_program |= output->end_of_program;
191 bc->cf_last->op = bc->cf_last->output.op = output->op;
192 bc->cf_last->output.gpr = output->gpr;
193 bc->cf_last->output.array_base = output->array_base;
194 bc->cf_last->output.burst_count += output->burst_count;
195 return 0;
196
197 } else if (output->gpr == (bc->cf_last->output.gpr + bc->cf_last->output.burst_count) &&
198 output->array_base == (bc->cf_last->output.array_base + bc->cf_last->output.burst_count)) {
199
200 bc->cf_last->output.end_of_program |= output->end_of_program;
201 bc->cf_last->op = bc->cf_last->output.op = output->op;
202 bc->cf_last->output.burst_count += output->burst_count;
203 return 0;
204 }
205 }
206
207 r = r600_bytecode_add_cf(bc);
208 if (r)
209 return r;
210 bc->cf_last->op = output->op;
211 memcpy(&bc->cf_last->output, output, sizeof(struct r600_bytecode_output));
212 return 0;
213 }
214
215 /* alu instructions that can ony exits once per group */
216 static int is_alu_once_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
217 {
218 return r600_isa_alu(alu->op)->flags & (AF_KILL | AF_PRED);
219 }
220
221 static int is_alu_reduction_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
222 {
223 return (r600_isa_alu(alu->op)->flags & AF_REPL) &&
224 (r600_isa_alu_slots(bc->isa->hw_class, alu->op) == AF_4V);
225 }
226
227 static int is_alu_mova_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
228 {
229 return r600_isa_alu(alu->op)->flags & AF_MOVA;
230 }
231
232 static int alu_uses_rel(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
233 {
234 unsigned num_src = r600_bytecode_get_num_operands(bc, alu);
235 unsigned src;
236
237 if (alu->dst.rel) {
238 return 1;
239 }
240
241 for (src = 0; src < num_src; ++src) {
242 if (alu->src[src].rel) {
243 return 1;
244 }
245 }
246 return 0;
247 }
248
249 static int is_alu_vec_unit_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
250 {
251 unsigned slots = r600_isa_alu_slots(bc->isa->hw_class, alu->op);
252 return !(slots & AF_S);
253 }
254
255 static int is_alu_trans_unit_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
256 {
257 unsigned slots = r600_isa_alu_slots(bc->isa->hw_class, alu->op);
258 return !(slots & AF_V);
259 }
260
261 /* alu instructions that can execute on any unit */
262 static int is_alu_any_unit_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
263 {
264 unsigned slots = r600_isa_alu_slots(bc->isa->hw_class, alu->op);
265 return slots == AF_VS;
266 }
267
268 static int is_nop_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)
269 {
270 return alu->op == ALU_OP0_NOP;
271 }
272
273 static int assign_alu_units(struct r600_bytecode *bc, struct r600_bytecode_alu *alu_first,
274 struct r600_bytecode_alu *assignment[5])
275 {
276 struct r600_bytecode_alu *alu;
277 unsigned i, chan, trans;
278 int max_slots = bc->chip_class == CAYMAN ? 4 : 5;
279
280 for (i = 0; i < max_slots; i++)
281 assignment[i] = NULL;
282
283 for (alu = alu_first; alu; alu = LIST_ENTRY(struct r600_bytecode_alu, alu->list.next, list)) {
284 chan = alu->dst.chan;
285 if (max_slots == 4)
286 trans = 0;
287 else if (is_alu_trans_unit_inst(bc, alu))
288 trans = 1;
289 else if (is_alu_vec_unit_inst(bc, alu))
290 trans = 0;
291 else if (assignment[chan])
292 trans = 1; /* Assume ALU_INST_PREFER_VECTOR. */
293 else
294 trans = 0;
295
296 if (trans) {
297 if (assignment[4]) {
298 assert(0); /* ALU.Trans has already been allocated. */
299 return -1;
300 }
301 assignment[4] = alu;
302 } else {
303 if (assignment[chan]) {
304 assert(0); /* ALU.chan has already been allocated. */
305 return -1;
306 }
307 assignment[chan] = alu;
308 }
309
310 if (alu->last)
311 break;
312 }
313 return 0;
314 }
315
316 struct alu_bank_swizzle {
317 int hw_gpr[NUM_OF_CYCLES][NUM_OF_COMPONENTS];
318 int hw_cfile_addr[4];
319 int hw_cfile_elem[4];
320 };
321
322 static const unsigned cycle_for_bank_swizzle_vec[][3] = {
323 [SQ_ALU_VEC_012] = { 0, 1, 2 },
324 [SQ_ALU_VEC_021] = { 0, 2, 1 },
325 [SQ_ALU_VEC_120] = { 1, 2, 0 },
326 [SQ_ALU_VEC_102] = { 1, 0, 2 },
327 [SQ_ALU_VEC_201] = { 2, 0, 1 },
328 [SQ_ALU_VEC_210] = { 2, 1, 0 }
329 };
330
331 static const unsigned cycle_for_bank_swizzle_scl[][3] = {
332 [SQ_ALU_SCL_210] = { 2, 1, 0 },
333 [SQ_ALU_SCL_122] = { 1, 2, 2 },
334 [SQ_ALU_SCL_212] = { 2, 1, 2 },
335 [SQ_ALU_SCL_221] = { 2, 2, 1 }
336 };
337
338 static void init_bank_swizzle(struct alu_bank_swizzle *bs)
339 {
340 int i, cycle, component;
341 /* set up gpr use */
342 for (cycle = 0; cycle < NUM_OF_CYCLES; cycle++)
343 for (component = 0; component < NUM_OF_COMPONENTS; component++)
344 bs->hw_gpr[cycle][component] = -1;
345 for (i = 0; i < 4; i++)
346 bs->hw_cfile_addr[i] = -1;
347 for (i = 0; i < 4; i++)
348 bs->hw_cfile_elem[i] = -1;
349 }
350
351 static int reserve_gpr(struct alu_bank_swizzle *bs, unsigned sel, unsigned chan, unsigned cycle)
352 {
353 if (bs->hw_gpr[cycle][chan] == -1)
354 bs->hw_gpr[cycle][chan] = sel;
355 else if (bs->hw_gpr[cycle][chan] != (int)sel) {
356 /* Another scalar operation has already used the GPR read port for the channel. */
357 return -1;
358 }
359 return 0;
360 }
361
362 static int reserve_cfile(struct r600_bytecode *bc, struct alu_bank_swizzle *bs, unsigned sel, unsigned chan)
363 {
364 int res, num_res = 4;
365 if (bc->chip_class >= R700) {
366 num_res = 2;
367 chan /= 2;
368 }
369 for (res = 0; res < num_res; ++res) {
370 if (bs->hw_cfile_addr[res] == -1) {
371 bs->hw_cfile_addr[res] = sel;
372 bs->hw_cfile_elem[res] = chan;
373 return 0;
374 } else if (bs->hw_cfile_addr[res] == sel &&
375 bs->hw_cfile_elem[res] == chan)
376 return 0; /* Read for this scalar element already reserved, nothing to do here. */
377 }
378 /* All cfile read ports are used, cannot reference vector element. */
379 return -1;
380 }
381
382 static int is_gpr(unsigned sel)
383 {
384 return (sel >= 0 && sel <= 127);
385 }
386
387 /* CB constants start at 512, and get translated to a kcache index when ALU
388 * clauses are constructed. Note that we handle kcache constants the same way
389 * as (the now gone) cfile constants, is that really required? */
390 static int is_cfile(unsigned sel)
391 {
392 return (sel > 255 && sel < 512) ||
393 (sel > 511 && sel < 4607) || /* Kcache before translation. */
394 (sel > 127 && sel < 192); /* Kcache after translation. */
395 }
396
397 static int is_const(int sel)
398 {
399 return is_cfile(sel) ||
400 (sel >= V_SQ_ALU_SRC_0 &&
401 sel <= V_SQ_ALU_SRC_LITERAL);
402 }
403
404 static int check_vector(struct r600_bytecode *bc, struct r600_bytecode_alu *alu,
405 struct alu_bank_swizzle *bs, int bank_swizzle)
406 {
407 int r, src, num_src, sel, elem, cycle;
408
409 num_src = r600_bytecode_get_num_operands(bc, alu);
410 for (src = 0; src < num_src; src++) {
411 sel = alu->src[src].sel;
412 elem = alu->src[src].chan;
413 if (is_gpr(sel)) {
414 cycle = cycle_for_bank_swizzle_vec[bank_swizzle][src];
415 if (src == 1 && sel == alu->src[0].sel && elem == alu->src[0].chan)
416 /* Nothing to do; special-case optimization,
417 * second source uses first source’s reservation. */
418 continue;
419 else {
420 r = reserve_gpr(bs, sel, elem, cycle);
421 if (r)
422 return r;
423 }
424 } else if (is_cfile(sel)) {
425 r = reserve_cfile(bc, bs, (alu->src[src].kc_bank<<16) + sel, elem);
426 if (r)
427 return r;
428 }
429 /* No restrictions on PV, PS, literal or special constants. */
430 }
431 return 0;
432 }
433
434 static int check_scalar(struct r600_bytecode *bc, struct r600_bytecode_alu *alu,
435 struct alu_bank_swizzle *bs, int bank_swizzle)
436 {
437 int r, src, num_src, const_count, sel, elem, cycle;
438
439 num_src = r600_bytecode_get_num_operands(bc, alu);
440 for (const_count = 0, src = 0; src < num_src; ++src) {
441 sel = alu->src[src].sel;
442 elem = alu->src[src].chan;
443 if (is_const(sel)) { /* Any constant, including literal and inline constants. */
444 if (const_count >= 2)
445 /* More than two references to a constant in
446 * transcendental operation. */
447 return -1;
448 else
449 const_count++;
450 }
451 if (is_cfile(sel)) {
452 r = reserve_cfile(bc, bs, (alu->src[src].kc_bank<<16) + sel, elem);
453 if (r)
454 return r;
455 }
456 }
457 for (src = 0; src < num_src; ++src) {
458 sel = alu->src[src].sel;
459 elem = alu->src[src].chan;
460 if (is_gpr(sel)) {
461 cycle = cycle_for_bank_swizzle_scl[bank_swizzle][src];
462 if (cycle < const_count)
463 /* Cycle for GPR load conflicts with
464 * constant load in transcendental operation. */
465 return -1;
466 r = reserve_gpr(bs, sel, elem, cycle);
467 if (r)
468 return r;
469 }
470 /* PV PS restrictions */
471 if (const_count && (sel == 254 || sel == 255)) {
472 cycle = cycle_for_bank_swizzle_scl[bank_swizzle][src];
473 if (cycle < const_count)
474 return -1;
475 }
476 }
477 return 0;
478 }
479
480 static int check_and_set_bank_swizzle(struct r600_bytecode *bc,
481 struct r600_bytecode_alu *slots[5])
482 {
483 struct alu_bank_swizzle bs;
484 int bank_swizzle[5];
485 int i, r = 0, forced = 1;
486 boolean scalar_only = bc->chip_class == CAYMAN ? false : true;
487 int max_slots = bc->chip_class == CAYMAN ? 4 : 5;
488
489 for (i = 0; i < max_slots; i++) {
490 if (slots[i]) {
491 if (slots[i]->bank_swizzle_force) {
492 slots[i]->bank_swizzle = slots[i]->bank_swizzle_force;
493 } else {
494 forced = 0;
495 }
496 }
497
498 if (i < 4 && slots[i])
499 scalar_only = false;
500 }
501 if (forced)
502 return 0;
503
504 /* Just check every possible combination of bank swizzle.
505 * Not very efficent, but works on the first try in most of the cases. */
506 for (i = 0; i < 4; i++)
507 if (!slots[i] || !slots[i]->bank_swizzle_force)
508 bank_swizzle[i] = SQ_ALU_VEC_012;
509 else
510 bank_swizzle[i] = slots[i]->bank_swizzle;
511
512 bank_swizzle[4] = SQ_ALU_SCL_210;
513 while(bank_swizzle[4] <= SQ_ALU_SCL_221) {
514
515 init_bank_swizzle(&bs);
516 if (scalar_only == false) {
517 for (i = 0; i < 4; i++) {
518 if (slots[i]) {
519 r = check_vector(bc, slots[i], &bs, bank_swizzle[i]);
520 if (r)
521 break;
522 }
523 }
524 } else
525 r = 0;
526
527 if (!r && slots[4] && max_slots == 5) {
528 r = check_scalar(bc, slots[4], &bs, bank_swizzle[4]);
529 }
530 if (!r) {
531 for (i = 0; i < max_slots; i++) {
532 if (slots[i])
533 slots[i]->bank_swizzle = bank_swizzle[i];
534 }
535 return 0;
536 }
537
538 if (scalar_only) {
539 bank_swizzle[4]++;
540 } else {
541 for (i = 0; i < max_slots; i++) {
542 if (!slots[i] || !slots[i]->bank_swizzle_force) {
543 bank_swizzle[i]++;
544 if (bank_swizzle[i] <= SQ_ALU_VEC_210)
545 break;
546 else if (i < max_slots - 1)
547 bank_swizzle[i] = SQ_ALU_VEC_012;
548 else
549 return -1;
550 }
551 }
552 }
553 }
554
555 /* Couldn't find a working swizzle. */
556 return -1;
557 }
558
559 static int replace_gpr_with_pv_ps(struct r600_bytecode *bc,
560 struct r600_bytecode_alu *slots[5], struct r600_bytecode_alu *alu_prev)
561 {
562 struct r600_bytecode_alu *prev[5];
563 int gpr[5], chan[5];
564 int i, j, r, src, num_src;
565 int max_slots = bc->chip_class == CAYMAN ? 4 : 5;
566
567 r = assign_alu_units(bc, alu_prev, prev);
568 if (r)
569 return r;
570
571 for (i = 0; i < max_slots; ++i) {
572 if (prev[i] && (prev[i]->dst.write || prev[i]->is_op3) && !prev[i]->dst.rel) {
573 gpr[i] = prev[i]->dst.sel;
574 /* cube writes more than PV.X */
575 if (is_alu_reduction_inst(bc, prev[i]))
576 chan[i] = 0;
577 else
578 chan[i] = prev[i]->dst.chan;
579 } else
580 gpr[i] = -1;
581 }
582
583 for (i = 0; i < max_slots; ++i) {
584 struct r600_bytecode_alu *alu = slots[i];
585 if(!alu)
586 continue;
587
588 num_src = r600_bytecode_get_num_operands(bc, alu);
589 for (src = 0; src < num_src; ++src) {
590 if (!is_gpr(alu->src[src].sel) || alu->src[src].rel)
591 continue;
592
593 if (bc->chip_class < CAYMAN) {
594 if (alu->src[src].sel == gpr[4] &&
595 alu->src[src].chan == chan[4] &&
596 alu_prev->pred_sel == alu->pred_sel) {
597 alu->src[src].sel = V_SQ_ALU_SRC_PS;
598 alu->src[src].chan = 0;
599 continue;
600 }
601 }
602
603 for (j = 0; j < 4; ++j) {
604 if (alu->src[src].sel == gpr[j] &&
605 alu->src[src].chan == j &&
606 alu_prev->pred_sel == alu->pred_sel) {
607 alu->src[src].sel = V_SQ_ALU_SRC_PV;
608 alu->src[src].chan = chan[j];
609 break;
610 }
611 }
612 }
613 }
614
615 return 0;
616 }
617
618 void r600_bytecode_special_constants(uint32_t value, unsigned *sel, unsigned *neg)
619 {
620 switch(value) {
621 case 0:
622 *sel = V_SQ_ALU_SRC_0;
623 break;
624 case 1:
625 *sel = V_SQ_ALU_SRC_1_INT;
626 break;
627 case -1:
628 *sel = V_SQ_ALU_SRC_M_1_INT;
629 break;
630 case 0x3F800000: /* 1.0f */
631 *sel = V_SQ_ALU_SRC_1;
632 break;
633 case 0x3F000000: /* 0.5f */
634 *sel = V_SQ_ALU_SRC_0_5;
635 break;
636 case 0xBF800000: /* -1.0f */
637 *sel = V_SQ_ALU_SRC_1;
638 *neg ^= 1;
639 break;
640 case 0xBF000000: /* -0.5f */
641 *sel = V_SQ_ALU_SRC_0_5;
642 *neg ^= 1;
643 break;
644 default:
645 *sel = V_SQ_ALU_SRC_LITERAL;
646 break;
647 }
648 }
649
650 /* compute how many literal are needed */
651 static int r600_bytecode_alu_nliterals(struct r600_bytecode *bc, struct r600_bytecode_alu *alu,
652 uint32_t literal[4], unsigned *nliteral)
653 {
654 unsigned num_src = r600_bytecode_get_num_operands(bc, alu);
655 unsigned i, j;
656
657 for (i = 0; i < num_src; ++i) {
658 if (alu->src[i].sel == V_SQ_ALU_SRC_LITERAL) {
659 uint32_t value = alu->src[i].value;
660 unsigned found = 0;
661 for (j = 0; j < *nliteral; ++j) {
662 if (literal[j] == value) {
663 found = 1;
664 break;
665 }
666 }
667 if (!found) {
668 if (*nliteral >= 4)
669 return -EINVAL;
670 literal[(*nliteral)++] = value;
671 }
672 }
673 }
674 return 0;
675 }
676
677 static void r600_bytecode_alu_adjust_literals(struct r600_bytecode *bc,
678 struct r600_bytecode_alu *alu,
679 uint32_t literal[4], unsigned nliteral)
680 {
681 unsigned num_src = r600_bytecode_get_num_operands(bc, alu);
682 unsigned i, j;
683
684 for (i = 0; i < num_src; ++i) {
685 if (alu->src[i].sel == V_SQ_ALU_SRC_LITERAL) {
686 uint32_t value = alu->src[i].value;
687 for (j = 0; j < nliteral; ++j) {
688 if (literal[j] == value) {
689 alu->src[i].chan = j;
690 break;
691 }
692 }
693 }
694 }
695 }
696
697 static int merge_inst_groups(struct r600_bytecode *bc, struct r600_bytecode_alu *slots[5],
698 struct r600_bytecode_alu *alu_prev)
699 {
700 struct r600_bytecode_alu *prev[5];
701 struct r600_bytecode_alu *result[5] = { NULL };
702
703 uint32_t literal[4], prev_literal[4];
704 unsigned nliteral = 0, prev_nliteral = 0;
705
706 int i, j, r, src, num_src;
707 int num_once_inst = 0;
708 int have_mova = 0, have_rel = 0;
709 int max_slots = bc->chip_class == CAYMAN ? 4 : 5;
710
711 r = assign_alu_units(bc, alu_prev, prev);
712 if (r)
713 return r;
714
715 for (i = 0; i < max_slots; ++i) {
716 if (prev[i]) {
717 if (prev[i]->pred_sel)
718 return 0;
719 if (is_alu_once_inst(bc, prev[i]))
720 return 0;
721 }
722 if (slots[i]) {
723 if (slots[i]->pred_sel)
724 return 0;
725 if (is_alu_once_inst(bc, slots[i]))
726 return 0;
727 }
728 }
729
730 for (i = 0; i < max_slots; ++i) {
731 struct r600_bytecode_alu *alu;
732
733 if (num_once_inst > 0)
734 return 0;
735
736 /* check number of literals */
737 if (prev[i]) {
738 if (r600_bytecode_alu_nliterals(bc, prev[i], literal, &nliteral))
739 return 0;
740 if (r600_bytecode_alu_nliterals(bc, prev[i], prev_literal, &prev_nliteral))
741 return 0;
742 if (is_alu_mova_inst(bc, prev[i])) {
743 if (have_rel)
744 return 0;
745 have_mova = 1;
746 }
747
748 if (alu_uses_rel(bc, prev[i])) {
749 if (have_mova) {
750 return 0;
751 }
752 have_rel = 1;
753 }
754
755 num_once_inst += is_alu_once_inst(bc, prev[i]);
756 }
757 if (slots[i] && r600_bytecode_alu_nliterals(bc, slots[i], literal, &nliteral))
758 return 0;
759
760 /* Let's check used slots. */
761 if (prev[i] && !slots[i]) {
762 result[i] = prev[i];
763 continue;
764 } else if (prev[i] && slots[i]) {
765 if (max_slots == 5 && result[4] == NULL && prev[4] == NULL && slots[4] == NULL) {
766 /* Trans unit is still free try to use it. */
767 if (is_alu_any_unit_inst(bc, slots[i])) {
768 result[i] = prev[i];
769 result[4] = slots[i];
770 } else if (is_alu_any_unit_inst(bc, prev[i])) {
771 if (slots[i]->dst.sel == prev[i]->dst.sel &&
772 (slots[i]->dst.write == 1 || slots[i]->is_op3) &&
773 (prev[i]->dst.write == 1 || prev[i]->is_op3))
774 return 0;
775
776 result[i] = slots[i];
777 result[4] = prev[i];
778 } else
779 return 0;
780 } else
781 return 0;
782 } else if(!slots[i]) {
783 continue;
784 } else {
785 if (max_slots == 5 && slots[i] && prev[4] &&
786 slots[i]->dst.sel == prev[4]->dst.sel &&
787 slots[i]->dst.chan == prev[4]->dst.chan &&
788 (slots[i]->dst.write == 1 || slots[i]->is_op3) &&
789 (prev[4]->dst.write == 1 || prev[4]->is_op3))
790 return 0;
791
792 result[i] = slots[i];
793 }
794
795 alu = slots[i];
796 num_once_inst += is_alu_once_inst(bc, alu);
797
798 /* don't reschedule NOPs */
799 if (is_nop_inst(bc, alu))
800 return 0;
801
802 if (is_alu_mova_inst(bc, alu)) {
803 if (have_rel) {
804 return 0;
805 }
806 have_mova = 1;
807 }
808
809 if (alu_uses_rel(bc, alu)) {
810 if (have_mova) {
811 return 0;
812 }
813 have_rel = 1;
814 }
815
816 /* Let's check source gprs */
817 num_src = r600_bytecode_get_num_operands(bc, alu);
818 for (src = 0; src < num_src; ++src) {
819
820 /* Constants don't matter. */
821 if (!is_gpr(alu->src[src].sel))
822 continue;
823
824 for (j = 0; j < max_slots; ++j) {
825 if (!prev[j] || !(prev[j]->dst.write || prev[j]->is_op3))
826 continue;
827
828 /* If it's relative then we can't determin which gpr is really used. */
829 if (prev[j]->dst.chan == alu->src[src].chan &&
830 (prev[j]->dst.sel == alu->src[src].sel ||
831 prev[j]->dst.rel || alu->src[src].rel))
832 return 0;
833 }
834 }
835 }
836
837 /* more than one PRED_ or KILL_ ? */
838 if (num_once_inst > 1)
839 return 0;
840
841 /* check if the result can still be swizzlet */
842 r = check_and_set_bank_swizzle(bc, result);
843 if (r)
844 return 0;
845
846 /* looks like everything worked out right, apply the changes */
847
848 /* undo adding previus literals */
849 bc->cf_last->ndw -= align(prev_nliteral, 2);
850
851 /* sort instructions */
852 for (i = 0; i < max_slots; ++i) {
853 slots[i] = result[i];
854 if (result[i]) {
855 LIST_DEL(&result[i]->list);
856 result[i]->last = 0;
857 LIST_ADDTAIL(&result[i]->list, &bc->cf_last->alu);
858 }
859 }
860
861 /* determine new last instruction */
862 LIST_ENTRY(struct r600_bytecode_alu, bc->cf_last->alu.prev, list)->last = 1;
863
864 /* determine new first instruction */
865 for (i = 0; i < max_slots; ++i) {
866 if (result[i]) {
867 bc->cf_last->curr_bs_head = result[i];
868 break;
869 }
870 }
871
872 bc->cf_last->prev_bs_head = bc->cf_last->prev2_bs_head;
873 bc->cf_last->prev2_bs_head = NULL;
874
875 return 0;
876 }
877
878 /* we'll keep kcache sets sorted by bank & addr */
879 static int r600_bytecode_alloc_kcache_line(struct r600_bytecode *bc,
880 struct r600_bytecode_kcache *kcache,
881 unsigned bank, unsigned line)
882 {
883 int i, kcache_banks = bc->chip_class >= EVERGREEN ? 4 : 2;
884
885 for (i = 0; i < kcache_banks; i++) {
886 if (kcache[i].mode) {
887 int d;
888
889 if (kcache[i].bank < bank)
890 continue;
891
892 if ((kcache[i].bank == bank && kcache[i].addr > line+1) ||
893 kcache[i].bank > bank) {
894 /* try to insert new line */
895 if (kcache[kcache_banks-1].mode) {
896 /* all sets are in use */
897 return -ENOMEM;
898 }
899
900 memmove(&kcache[i+1],&kcache[i], (kcache_banks-i-1)*sizeof(struct r600_bytecode_kcache));
901 kcache[i].mode = V_SQ_CF_KCACHE_LOCK_1;
902 kcache[i].bank = bank;
903 kcache[i].addr = line;
904 return 0;
905 }
906
907 d = line - kcache[i].addr;
908
909 if (d == -1) {
910 kcache[i].addr--;
911 if (kcache[i].mode == V_SQ_CF_KCACHE_LOCK_2) {
912 /* we are prepending the line to the current set,
913 * discarding the existing second line,
914 * so we'll have to insert line+2 after it */
915 line += 2;
916 continue;
917 } else if (kcache[i].mode == V_SQ_CF_KCACHE_LOCK_1) {
918 kcache[i].mode = V_SQ_CF_KCACHE_LOCK_2;
919 return 0;
920 } else {
921 /* V_SQ_CF_KCACHE_LOCK_LOOP_INDEX is not supported */
922 return -ENOMEM;
923 }
924 } else if (d == 1) {
925 kcache[i].mode = V_SQ_CF_KCACHE_LOCK_2;
926 return 0;
927 } else if (d == 0)
928 return 0;
929 } else { /* free kcache set - use it */
930 kcache[i].mode = V_SQ_CF_KCACHE_LOCK_1;
931 kcache[i].bank = bank;
932 kcache[i].addr = line;
933 return 0;
934 }
935 }
936 return -ENOMEM;
937 }
938
939 static int r600_bytecode_alloc_inst_kcache_lines(struct r600_bytecode *bc,
940 struct r600_bytecode_kcache *kcache,
941 struct r600_bytecode_alu *alu)
942 {
943 int i, r;
944
945 for (i = 0; i < 3; i++) {
946 unsigned bank, line, sel = alu->src[i].sel;
947
948 if (sel < 512)
949 continue;
950
951 bank = alu->src[i].kc_bank;
952 line = (sel-512)>>4;
953
954 if ((r = r600_bytecode_alloc_kcache_line(bc, kcache, bank, line)))
955 return r;
956 }
957 return 0;
958 }
959
960 static int r600_bytecode_assign_kcache_banks(struct r600_bytecode *bc,
961 struct r600_bytecode_alu *alu,
962 struct r600_bytecode_kcache * kcache)
963 {
964 int i, j;
965
966 /* Alter the src operands to refer to the kcache. */
967 for (i = 0; i < 3; ++i) {
968 static const unsigned int base[] = {128, 160, 256, 288};
969 unsigned int line, sel = alu->src[i].sel, found = 0;
970
971 if (sel < 512)
972 continue;
973
974 sel -= 512;
975 line = sel>>4;
976
977 for (j = 0; j < 4 && !found; ++j) {
978 switch (kcache[j].mode) {
979 case V_SQ_CF_KCACHE_NOP:
980 case V_SQ_CF_KCACHE_LOCK_LOOP_INDEX:
981 R600_ERR("unexpected kcache line mode\n");
982 return -ENOMEM;
983 default:
984 if (kcache[j].bank == alu->src[i].kc_bank &&
985 kcache[j].addr <= line &&
986 line < kcache[j].addr + kcache[j].mode) {
987 alu->src[i].sel = sel - (kcache[j].addr<<4);
988 alu->src[i].sel += base[j];
989 found=1;
990 }
991 }
992 }
993 }
994 return 0;
995 }
996
997 static int r600_bytecode_alloc_kcache_lines(struct r600_bytecode *bc,
998 struct r600_bytecode_alu *alu,
999 unsigned type)
1000 {
1001 struct r600_bytecode_kcache kcache_sets[4];
1002 struct r600_bytecode_kcache *kcache = kcache_sets;
1003 int r;
1004
1005 memcpy(kcache, bc->cf_last->kcache, 4 * sizeof(struct r600_bytecode_kcache));
1006
1007 if ((r = r600_bytecode_alloc_inst_kcache_lines(bc, kcache, alu))) {
1008 /* can't alloc, need to start new clause */
1009 if ((r = r600_bytecode_add_cf(bc))) {
1010 return r;
1011 }
1012 bc->cf_last->op = type;
1013
1014 /* retry with the new clause */
1015 kcache = bc->cf_last->kcache;
1016 if ((r = r600_bytecode_alloc_inst_kcache_lines(bc, kcache, alu))) {
1017 /* can't alloc again- should never happen */
1018 return r;
1019 }
1020 } else {
1021 /* update kcache sets */
1022 memcpy(bc->cf_last->kcache, kcache, 4 * sizeof(struct r600_bytecode_kcache));
1023 }
1024
1025 /* if we actually used more than 2 kcache sets - use ALU_EXTENDED on eg+ */
1026 if (kcache[2].mode != V_SQ_CF_KCACHE_NOP) {
1027 if (bc->chip_class < EVERGREEN)
1028 return -ENOMEM;
1029 bc->cf_last->eg_alu_extended = 1;
1030 }
1031
1032 return 0;
1033 }
1034
1035 static int insert_nop_r6xx(struct r600_bytecode *bc)
1036 {
1037 struct r600_bytecode_alu alu;
1038 int r, i;
1039
1040 for (i = 0; i < 4; i++) {
1041 memset(&alu, 0, sizeof(alu));
1042 alu.op = ALU_OP0_NOP;
1043 alu.src[0].chan = i;
1044 alu.dst.chan = i;
1045 alu.last = (i == 3);
1046 r = r600_bytecode_add_alu(bc, &alu);
1047 if (r)
1048 return r;
1049 }
1050 return 0;
1051 }
1052
1053 /* load AR register from gpr (bc->ar_reg) with MOVA_INT */
1054 static int load_ar_r6xx(struct r600_bytecode *bc)
1055 {
1056 struct r600_bytecode_alu alu;
1057 int r;
1058
1059 if (bc->ar_loaded)
1060 return 0;
1061
1062 /* hack to avoid making MOVA the last instruction in the clause */
1063 if ((bc->cf_last->ndw>>1) >= 110)
1064 bc->force_add_cf = 1;
1065
1066 memset(&alu, 0, sizeof(alu));
1067 alu.op = ALU_OP1_MOVA_GPR_INT;
1068 alu.src[0].sel = bc->ar_reg;
1069 alu.src[0].chan = bc->ar_chan;
1070 alu.last = 1;
1071 alu.index_mode = INDEX_MODE_LOOP;
1072 r = r600_bytecode_add_alu(bc, &alu);
1073 if (r)
1074 return r;
1075
1076 /* no requirement to set uses waterfall on MOVA_GPR_INT */
1077 bc->ar_loaded = 1;
1078 return 0;
1079 }
1080
1081 /* load AR register from gpr (bc->ar_reg) with MOVA_INT */
1082 static int load_ar(struct r600_bytecode *bc)
1083 {
1084 struct r600_bytecode_alu alu;
1085 int r;
1086
1087 if (bc->ar_handling)
1088 return load_ar_r6xx(bc);
1089
1090 if (bc->ar_loaded)
1091 return 0;
1092
1093 /* hack to avoid making MOVA the last instruction in the clause */
1094 if ((bc->cf_last->ndw>>1) >= 110)
1095 bc->force_add_cf = 1;
1096
1097 memset(&alu, 0, sizeof(alu));
1098 alu.op = ALU_OP1_MOVA_INT;
1099 alu.src[0].sel = bc->ar_reg;
1100 alu.src[0].chan = bc->ar_chan;
1101 alu.last = 1;
1102 r = r600_bytecode_add_alu(bc, &alu);
1103 if (r)
1104 return r;
1105
1106 bc->cf_last->r6xx_uses_waterfall = 1;
1107 bc->ar_loaded = 1;
1108 return 0;
1109 }
1110
1111 int r600_bytecode_add_alu_type(struct r600_bytecode *bc,
1112 const struct r600_bytecode_alu *alu, unsigned type)
1113 {
1114 struct r600_bytecode_alu *nalu = r600_bytecode_alu();
1115 struct r600_bytecode_alu *lalu;
1116 int i, r;
1117
1118 if (nalu == NULL)
1119 return -ENOMEM;
1120 memcpy(nalu, alu, sizeof(struct r600_bytecode_alu));
1121
1122 if (bc->cf_last != NULL && bc->cf_last->op != type) {
1123 /* check if we could add it anyway */
1124 if (bc->cf_last->op == CF_OP_ALU &&
1125 type == CF_OP_ALU_PUSH_BEFORE) {
1126 LIST_FOR_EACH_ENTRY(lalu, &bc->cf_last->alu, list) {
1127 if (lalu->execute_mask) {
1128 bc->force_add_cf = 1;
1129 break;
1130 }
1131 }
1132 } else
1133 bc->force_add_cf = 1;
1134 }
1135
1136 /* cf can contains only alu or only vtx or only tex */
1137 if (bc->cf_last == NULL || bc->force_add_cf) {
1138 r = r600_bytecode_add_cf(bc);
1139 if (r) {
1140 free(nalu);
1141 return r;
1142 }
1143 }
1144 bc->cf_last->op = type;
1145
1146 /* Check AR usage and load it if required */
1147 for (i = 0; i < 3; i++)
1148 if (nalu->src[i].rel && !bc->ar_loaded)
1149 load_ar(bc);
1150
1151 if (nalu->dst.rel && !bc->ar_loaded)
1152 load_ar(bc);
1153
1154 /* Setup the kcache for this ALU instruction. This will start a new
1155 * ALU clause if needed. */
1156 if ((r = r600_bytecode_alloc_kcache_lines(bc, nalu, type))) {
1157 free(nalu);
1158 return r;
1159 }
1160
1161 if (!bc->cf_last->curr_bs_head) {
1162 bc->cf_last->curr_bs_head = nalu;
1163 }
1164 /* number of gpr == the last gpr used in any alu */
1165 for (i = 0; i < 3; i++) {
1166 if (nalu->src[i].sel >= bc->ngpr && nalu->src[i].sel < 128) {
1167 bc->ngpr = nalu->src[i].sel + 1;
1168 }
1169 if (nalu->src[i].sel == V_SQ_ALU_SRC_LITERAL)
1170 r600_bytecode_special_constants(nalu->src[i].value,
1171 &nalu->src[i].sel, &nalu->src[i].neg);
1172 }
1173 if (nalu->dst.sel >= bc->ngpr) {
1174 bc->ngpr = nalu->dst.sel + 1;
1175 }
1176 LIST_ADDTAIL(&nalu->list, &bc->cf_last->alu);
1177 /* each alu use 2 dwords */
1178 bc->cf_last->ndw += 2;
1179 bc->ndw += 2;
1180
1181 /* process cur ALU instructions for bank swizzle */
1182 if (nalu->last) {
1183 uint32_t literal[4];
1184 unsigned nliteral;
1185 struct r600_bytecode_alu *slots[5];
1186 int max_slots = bc->chip_class == CAYMAN ? 4 : 5;
1187 r = assign_alu_units(bc, bc->cf_last->curr_bs_head, slots);
1188 if (r)
1189 return r;
1190
1191 if (bc->cf_last->prev_bs_head) {
1192 r = merge_inst_groups(bc, slots, bc->cf_last->prev_bs_head);
1193 if (r)
1194 return r;
1195 }
1196
1197 if (bc->cf_last->prev_bs_head) {
1198 r = replace_gpr_with_pv_ps(bc, slots, bc->cf_last->prev_bs_head);
1199 if (r)
1200 return r;
1201 }
1202
1203 r = check_and_set_bank_swizzle(bc, slots);
1204 if (r)
1205 return r;
1206
1207 for (i = 0, nliteral = 0; i < max_slots; i++) {
1208 if (slots[i]) {
1209 r = r600_bytecode_alu_nliterals(bc, slots[i], literal, &nliteral);
1210 if (r)
1211 return r;
1212 }
1213 }
1214 bc->cf_last->ndw += align(nliteral, 2);
1215
1216 /* at most 128 slots, one add alu can add 5 slots + 4 constants(2 slots)
1217 * worst case */
1218 if ((bc->cf_last->ndw >> 1) >= 120) {
1219 bc->force_add_cf = 1;
1220 }
1221
1222 bc->cf_last->prev2_bs_head = bc->cf_last->prev_bs_head;
1223 bc->cf_last->prev_bs_head = bc->cf_last->curr_bs_head;
1224 bc->cf_last->curr_bs_head = NULL;
1225 }
1226
1227 if (nalu->dst.rel && bc->r6xx_nop_after_rel_dst)
1228 insert_nop_r6xx(bc);
1229
1230 return 0;
1231 }
1232
1233 int r600_bytecode_add_alu(struct r600_bytecode *bc, const struct r600_bytecode_alu *alu)
1234 {
1235 return r600_bytecode_add_alu_type(bc, alu, CF_OP_ALU);
1236 }
1237
1238 static unsigned r600_bytecode_num_tex_and_vtx_instructions(const struct r600_bytecode *bc)
1239 {
1240 switch (bc->chip_class) {
1241 case R600:
1242 return 8;
1243
1244 case R700:
1245 case EVERGREEN:
1246 case CAYMAN:
1247 return 16;
1248
1249 default:
1250 R600_ERR("Unknown chip class %d.\n", bc->chip_class);
1251 return 8;
1252 }
1253 }
1254
1255 static inline boolean last_inst_was_not_vtx_fetch(struct r600_bytecode *bc)
1256 {
1257 return !((r600_isa_cf(bc->cf_last->op)->flags & CF_FETCH) &&
1258 (bc->chip_class == CAYMAN ||
1259 bc->cf_last->op != CF_OP_TEX));
1260 }
1261
1262 int r600_bytecode_add_vtx(struct r600_bytecode *bc, const struct r600_bytecode_vtx *vtx)
1263 {
1264 struct r600_bytecode_vtx *nvtx = r600_bytecode_vtx();
1265 int r;
1266
1267 if (nvtx == NULL)
1268 return -ENOMEM;
1269 memcpy(nvtx, vtx, sizeof(struct r600_bytecode_vtx));
1270
1271 /* cf can contains only alu or only vtx or only tex */
1272 if (bc->cf_last == NULL ||
1273 last_inst_was_not_vtx_fetch(bc) ||
1274 bc->force_add_cf) {
1275 r = r600_bytecode_add_cf(bc);
1276 if (r) {
1277 free(nvtx);
1278 return r;
1279 }
1280 switch (bc->chip_class) {
1281 case R600:
1282 case R700:
1283 case EVERGREEN:
1284 bc->cf_last->op = CF_OP_VTX;
1285 break;
1286 case CAYMAN:
1287 bc->cf_last->op = CF_OP_TEX;
1288 break;
1289 default:
1290 R600_ERR("Unknown chip class %d.\n", bc->chip_class);
1291 free(nvtx);
1292 return -EINVAL;
1293 }
1294 }
1295 LIST_ADDTAIL(&nvtx->list, &bc->cf_last->vtx);
1296 /* each fetch use 4 dwords */
1297 bc->cf_last->ndw += 4;
1298 bc->ndw += 4;
1299 if ((bc->cf_last->ndw / 4) >= r600_bytecode_num_tex_and_vtx_instructions(bc))
1300 bc->force_add_cf = 1;
1301
1302 bc->ngpr = MAX2(bc->ngpr, vtx->src_gpr + 1);
1303 bc->ngpr = MAX2(bc->ngpr, vtx->dst_gpr + 1);
1304
1305 return 0;
1306 }
1307
1308 int r600_bytecode_add_tex(struct r600_bytecode *bc, const struct r600_bytecode_tex *tex)
1309 {
1310 struct r600_bytecode_tex *ntex = r600_bytecode_tex();
1311 int r;
1312
1313 if (ntex == NULL)
1314 return -ENOMEM;
1315 memcpy(ntex, tex, sizeof(struct r600_bytecode_tex));
1316
1317 /* we can't fetch data und use it as texture lookup address in the same TEX clause */
1318 if (bc->cf_last != NULL &&
1319 bc->cf_last->op == CF_OP_TEX) {
1320 struct r600_bytecode_tex *ttex;
1321 LIST_FOR_EACH_ENTRY(ttex, &bc->cf_last->tex, list) {
1322 if (ttex->dst_gpr == ntex->src_gpr) {
1323 bc->force_add_cf = 1;
1324 break;
1325 }
1326 }
1327 /* slight hack to make gradients always go into same cf */
1328 if (ntex->op == FETCH_OP_SET_GRADIENTS_H)
1329 bc->force_add_cf = 1;
1330 }
1331
1332 /* cf can contains only alu or only vtx or only tex */
1333 if (bc->cf_last == NULL ||
1334 bc->cf_last->op != CF_OP_TEX ||
1335 bc->force_add_cf) {
1336 r = r600_bytecode_add_cf(bc);
1337 if (r) {
1338 free(ntex);
1339 return r;
1340 }
1341 bc->cf_last->op = CF_OP_TEX;
1342 }
1343 if (ntex->src_gpr >= bc->ngpr) {
1344 bc->ngpr = ntex->src_gpr + 1;
1345 }
1346 if (ntex->dst_gpr >= bc->ngpr) {
1347 bc->ngpr = ntex->dst_gpr + 1;
1348 }
1349 LIST_ADDTAIL(&ntex->list, &bc->cf_last->tex);
1350 /* each texture fetch use 4 dwords */
1351 bc->cf_last->ndw += 4;
1352 bc->ndw += 4;
1353 if ((bc->cf_last->ndw / 4) >= r600_bytecode_num_tex_and_vtx_instructions(bc))
1354 bc->force_add_cf = 1;
1355 return 0;
1356 }
1357
1358 int r600_bytecode_add_cfinst(struct r600_bytecode *bc, unsigned op)
1359 {
1360 int r;
1361 r = r600_bytecode_add_cf(bc);
1362 if (r)
1363 return r;
1364
1365 bc->cf_last->cond = V_SQ_CF_COND_ACTIVE;
1366 bc->cf_last->op = op;
1367 return 0;
1368 }
1369
1370 int cm_bytecode_add_cf_end(struct r600_bytecode *bc)
1371 {
1372 return r600_bytecode_add_cfinst(bc, CF_OP_CF_END);
1373 }
1374
1375 /* common to all 3 families */
1376 static int r600_bytecode_vtx_build(struct r600_bytecode *bc, struct r600_bytecode_vtx *vtx, unsigned id)
1377 {
1378 bc->bytecode[id] = S_SQ_VTX_WORD0_BUFFER_ID(vtx->buffer_id) |
1379 S_SQ_VTX_WORD0_FETCH_TYPE(vtx->fetch_type) |
1380 S_SQ_VTX_WORD0_SRC_GPR(vtx->src_gpr) |
1381 S_SQ_VTX_WORD0_SRC_SEL_X(vtx->src_sel_x);
1382 if (bc->chip_class < CAYMAN)
1383 bc->bytecode[id] |= S_SQ_VTX_WORD0_MEGA_FETCH_COUNT(vtx->mega_fetch_count);
1384 id++;
1385 bc->bytecode[id++] = S_SQ_VTX_WORD1_DST_SEL_X(vtx->dst_sel_x) |
1386 S_SQ_VTX_WORD1_DST_SEL_Y(vtx->dst_sel_y) |
1387 S_SQ_VTX_WORD1_DST_SEL_Z(vtx->dst_sel_z) |
1388 S_SQ_VTX_WORD1_DST_SEL_W(vtx->dst_sel_w) |
1389 S_SQ_VTX_WORD1_USE_CONST_FIELDS(vtx->use_const_fields) |
1390 S_SQ_VTX_WORD1_DATA_FORMAT(vtx->data_format) |
1391 S_SQ_VTX_WORD1_NUM_FORMAT_ALL(vtx->num_format_all) |
1392 S_SQ_VTX_WORD1_FORMAT_COMP_ALL(vtx->format_comp_all) |
1393 S_SQ_VTX_WORD1_SRF_MODE_ALL(vtx->srf_mode_all) |
1394 S_SQ_VTX_WORD1_GPR_DST_GPR(vtx->dst_gpr);
1395 bc->bytecode[id] = S_SQ_VTX_WORD2_OFFSET(vtx->offset)|
1396 S_SQ_VTX_WORD2_ENDIAN_SWAP(vtx->endian);
1397 if (bc->chip_class < CAYMAN)
1398 bc->bytecode[id] |= S_SQ_VTX_WORD2_MEGA_FETCH(1);
1399 id++;
1400 bc->bytecode[id++] = 0;
1401 return 0;
1402 }
1403
1404 /* common to all 3 families */
1405 static int r600_bytecode_tex_build(struct r600_bytecode *bc, struct r600_bytecode_tex *tex, unsigned id)
1406 {
1407 bc->bytecode[id++] = S_SQ_TEX_WORD0_TEX_INST(
1408 r600_isa_fetch_opcode(bc->isa->hw_class, tex->op)) |
1409 EG_S_SQ_TEX_WORD0_INST_MOD(tex->inst_mod) |
1410 S_SQ_TEX_WORD0_RESOURCE_ID(tex->resource_id) |
1411 S_SQ_TEX_WORD0_SRC_GPR(tex->src_gpr) |
1412 S_SQ_TEX_WORD0_SRC_REL(tex->src_rel);
1413 bc->bytecode[id++] = S_SQ_TEX_WORD1_DST_GPR(tex->dst_gpr) |
1414 S_SQ_TEX_WORD1_DST_REL(tex->dst_rel) |
1415 S_SQ_TEX_WORD1_DST_SEL_X(tex->dst_sel_x) |
1416 S_SQ_TEX_WORD1_DST_SEL_Y(tex->dst_sel_y) |
1417 S_SQ_TEX_WORD1_DST_SEL_Z(tex->dst_sel_z) |
1418 S_SQ_TEX_WORD1_DST_SEL_W(tex->dst_sel_w) |
1419 S_SQ_TEX_WORD1_LOD_BIAS(tex->lod_bias) |
1420 S_SQ_TEX_WORD1_COORD_TYPE_X(tex->coord_type_x) |
1421 S_SQ_TEX_WORD1_COORD_TYPE_Y(tex->coord_type_y) |
1422 S_SQ_TEX_WORD1_COORD_TYPE_Z(tex->coord_type_z) |
1423 S_SQ_TEX_WORD1_COORD_TYPE_W(tex->coord_type_w);
1424 bc->bytecode[id++] = S_SQ_TEX_WORD2_OFFSET_X(tex->offset_x) |
1425 S_SQ_TEX_WORD2_OFFSET_Y(tex->offset_y) |
1426 S_SQ_TEX_WORD2_OFFSET_Z(tex->offset_z) |
1427 S_SQ_TEX_WORD2_SAMPLER_ID(tex->sampler_id) |
1428 S_SQ_TEX_WORD2_SRC_SEL_X(tex->src_sel_x) |
1429 S_SQ_TEX_WORD2_SRC_SEL_Y(tex->src_sel_y) |
1430 S_SQ_TEX_WORD2_SRC_SEL_Z(tex->src_sel_z) |
1431 S_SQ_TEX_WORD2_SRC_SEL_W(tex->src_sel_w);
1432 bc->bytecode[id++] = 0;
1433 return 0;
1434 }
1435
1436 /* r600 only, r700/eg bits in r700_asm.c */
1437 static int r600_bytecode_alu_build(struct r600_bytecode *bc, struct r600_bytecode_alu *alu, unsigned id)
1438 {
1439 unsigned opcode = r600_isa_alu_opcode(bc->isa->hw_class, alu->op);
1440
1441 /* don't replace gpr by pv or ps for destination register */
1442 bc->bytecode[id++] = S_SQ_ALU_WORD0_SRC0_SEL(alu->src[0].sel) |
1443 S_SQ_ALU_WORD0_SRC0_REL(alu->src[0].rel) |
1444 S_SQ_ALU_WORD0_SRC0_CHAN(alu->src[0].chan) |
1445 S_SQ_ALU_WORD0_SRC0_NEG(alu->src[0].neg) |
1446 S_SQ_ALU_WORD0_SRC1_SEL(alu->src[1].sel) |
1447 S_SQ_ALU_WORD0_SRC1_REL(alu->src[1].rel) |
1448 S_SQ_ALU_WORD0_SRC1_CHAN(alu->src[1].chan) |
1449 S_SQ_ALU_WORD0_SRC1_NEG(alu->src[1].neg) |
1450 S_SQ_ALU_WORD0_INDEX_MODE(alu->index_mode) |
1451 S_SQ_ALU_WORD0_PRED_SEL(alu->pred_sel) |
1452 S_SQ_ALU_WORD0_LAST(alu->last);
1453
1454 if (alu->is_op3) {
1455 bc->bytecode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |
1456 S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |
1457 S_SQ_ALU_WORD1_DST_REL(alu->dst.rel) |
1458 S_SQ_ALU_WORD1_CLAMP(alu->dst.clamp) |
1459 S_SQ_ALU_WORD1_OP3_SRC2_SEL(alu->src[2].sel) |
1460 S_SQ_ALU_WORD1_OP3_SRC2_REL(alu->src[2].rel) |
1461 S_SQ_ALU_WORD1_OP3_SRC2_CHAN(alu->src[2].chan) |
1462 S_SQ_ALU_WORD1_OP3_SRC2_NEG(alu->src[2].neg) |
1463 S_SQ_ALU_WORD1_OP3_ALU_INST(opcode) |
1464 S_SQ_ALU_WORD1_BANK_SWIZZLE(alu->bank_swizzle);
1465 } else {
1466 bc->bytecode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |
1467 S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |
1468 S_SQ_ALU_WORD1_DST_REL(alu->dst.rel) |
1469 S_SQ_ALU_WORD1_CLAMP(alu->dst.clamp) |
1470 S_SQ_ALU_WORD1_OP2_SRC0_ABS(alu->src[0].abs) |
1471 S_SQ_ALU_WORD1_OP2_SRC1_ABS(alu->src[1].abs) |
1472 S_SQ_ALU_WORD1_OP2_WRITE_MASK(alu->dst.write) |
1473 S_SQ_ALU_WORD1_OP2_OMOD(alu->omod) |
1474 S_SQ_ALU_WORD1_OP2_ALU_INST(opcode) |
1475 S_SQ_ALU_WORD1_BANK_SWIZZLE(alu->bank_swizzle) |
1476 S_SQ_ALU_WORD1_OP2_UPDATE_EXECUTE_MASK(alu->execute_mask) |
1477 S_SQ_ALU_WORD1_OP2_UPDATE_PRED(alu->update_pred);
1478 }
1479 return 0;
1480 }
1481
1482 static void r600_bytecode_cf_vtx_build(uint32_t *bytecode, const struct r600_bytecode_cf *cf)
1483 {
1484 *bytecode++ = S_SQ_CF_WORD0_ADDR(cf->addr >> 1);
1485 *bytecode++ = S_SQ_CF_WORD1_CF_INST(r600_isa_cf_opcode(ISA_CC_R600, cf->op)) |
1486 S_SQ_CF_WORD1_BARRIER(1) |
1487 S_SQ_CF_WORD1_COUNT((cf->ndw / 4) - 1);
1488 }
1489
1490 /* common for r600/r700 - eg in eg_asm.c */
1491 static int r600_bytecode_cf_build(struct r600_bytecode *bc, struct r600_bytecode_cf *cf)
1492 {
1493 unsigned id = cf->id;
1494 const struct cf_op_info *cfop = r600_isa_cf(cf->op);
1495 unsigned opcode = r600_isa_cf_opcode(bc->isa->hw_class, cf->op);
1496
1497 if (cfop->flags & CF_ALU) {
1498 bc->bytecode[id++] = S_SQ_CF_ALU_WORD0_ADDR(cf->addr >> 1) |
1499 S_SQ_CF_ALU_WORD0_KCACHE_MODE0(cf->kcache[0].mode) |
1500 S_SQ_CF_ALU_WORD0_KCACHE_BANK0(cf->kcache[0].bank) |
1501 S_SQ_CF_ALU_WORD0_KCACHE_BANK1(cf->kcache[1].bank);
1502
1503 bc->bytecode[id++] = S_SQ_CF_ALU_WORD1_CF_INST(opcode) |
1504 S_SQ_CF_ALU_WORD1_KCACHE_MODE1(cf->kcache[1].mode) |
1505 S_SQ_CF_ALU_WORD1_KCACHE_ADDR0(cf->kcache[0].addr) |
1506 S_SQ_CF_ALU_WORD1_KCACHE_ADDR1(cf->kcache[1].addr) |
1507 S_SQ_CF_ALU_WORD1_BARRIER(1) |
1508 S_SQ_CF_ALU_WORD1_USES_WATERFALL(bc->chip_class == R600 ? cf->r6xx_uses_waterfall : 0) |
1509 S_SQ_CF_ALU_WORD1_COUNT((cf->ndw / 2) - 1);
1510 } else if (cfop->flags & CF_FETCH) {
1511 if (bc->chip_class == R700)
1512 r700_bytecode_cf_vtx_build(&bc->bytecode[id], cf);
1513 else
1514 r600_bytecode_cf_vtx_build(&bc->bytecode[id], cf);
1515 } else if (cfop->flags & CF_EXP) {
1516 bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf->output.gpr) |
1517 S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf->output.elem_size) |
1518 S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf->output.array_base) |
1519 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf->output.type);
1520 bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(cf->output.burst_count - 1) |
1521 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(cf->output.swizzle_x) |
1522 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(cf->output.swizzle_y) |
1523 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(cf->output.swizzle_z) |
1524 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(cf->output.swizzle_w) |
1525 S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf->output.barrier) |
1526 S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(opcode) |
1527 S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf->output.end_of_program);
1528 } else if (cfop->flags & CF_STRM) {
1529 bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf->output.gpr) |
1530 S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf->output.elem_size) |
1531 S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf->output.array_base) |
1532 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf->output.type);
1533 bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(cf->output.burst_count - 1) |
1534 S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf->output.barrier) |
1535 S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(opcode) |
1536 S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf->output.end_of_program) |
1537 S_SQ_CF_ALLOC_EXPORT_WORD1_BUF_ARRAY_SIZE(cf->output.array_size) |
1538 S_SQ_CF_ALLOC_EXPORT_WORD1_BUF_COMP_MASK(cf->output.comp_mask);
1539 } else {
1540 bc->bytecode[id++] = S_SQ_CF_WORD0_ADDR(cf->cf_addr >> 1);
1541 bc->bytecode[id++] = S_SQ_CF_WORD1_CF_INST(opcode) |
1542 S_SQ_CF_WORD1_BARRIER(1) |
1543 S_SQ_CF_WORD1_COND(cf->cond) |
1544 S_SQ_CF_WORD1_POP_COUNT(cf->pop_count);
1545 }
1546 return 0;
1547 }
1548
1549 int r600_bytecode_build(struct r600_bytecode *bc)
1550 {
1551 struct r600_bytecode_cf *cf;
1552 struct r600_bytecode_alu *alu;
1553 struct r600_bytecode_vtx *vtx;
1554 struct r600_bytecode_tex *tex;
1555 uint32_t literal[4];
1556 unsigned nliteral;
1557 unsigned addr;
1558 int i, r;
1559
1560 if (!bc->nstack) // If not 0, Stack_size already provided by llvm
1561 bc->nstack = bc->stack.max_entries;
1562
1563 if (bc->type == TGSI_PROCESSOR_VERTEX && !bc->nstack) {
1564 bc->nstack = 1;
1565 }
1566
1567 /* first path compute addr of each CF block */
1568 /* addr start after all the CF instructions */
1569 addr = bc->cf_last->id + 2;
1570 LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {
1571 if (r600_isa_cf(cf->op)->flags & CF_FETCH) {
1572 addr += 3;
1573 addr &= 0xFFFFFFFCUL;
1574 }
1575 cf->addr = addr;
1576 addr += cf->ndw;
1577 bc->ndw = cf->addr + cf->ndw;
1578 }
1579 free(bc->bytecode);
1580 bc->bytecode = calloc(1, bc->ndw * 4);
1581 if (bc->bytecode == NULL)
1582 return -ENOMEM;
1583 LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {
1584 const struct cf_op_info *cfop = r600_isa_cf(cf->op);
1585 addr = cf->addr;
1586 if (bc->chip_class >= EVERGREEN)
1587 r = eg_bytecode_cf_build(bc, cf);
1588 else
1589 r = r600_bytecode_cf_build(bc, cf);
1590 if (r)
1591 return r;
1592 if (cfop->flags & CF_ALU) {
1593 nliteral = 0;
1594 memset(literal, 0, sizeof(literal));
1595 LIST_FOR_EACH_ENTRY(alu, &cf->alu, list) {
1596 r = r600_bytecode_alu_nliterals(bc, alu, literal, &nliteral);
1597 if (r)
1598 return r;
1599 r600_bytecode_alu_adjust_literals(bc, alu, literal, nliteral);
1600 r600_bytecode_assign_kcache_banks(bc, alu, cf->kcache);
1601
1602 switch(bc->chip_class) {
1603 case R600:
1604 r = r600_bytecode_alu_build(bc, alu, addr);
1605 break;
1606 case R700:
1607 case EVERGREEN: /* eg alu is same encoding as r700 */
1608 case CAYMAN:
1609 r = r700_bytecode_alu_build(bc, alu, addr);
1610 break;
1611 default:
1612 R600_ERR("unknown chip class %d.\n", bc->chip_class);
1613 return -EINVAL;
1614 }
1615 if (r)
1616 return r;
1617 addr += 2;
1618 if (alu->last) {
1619 for (i = 0; i < align(nliteral, 2); ++i) {
1620 bc->bytecode[addr++] = literal[i];
1621 }
1622 nliteral = 0;
1623 memset(literal, 0, sizeof(literal));
1624 }
1625 }
1626 } else if (cf->op == CF_OP_VTX) {
1627 LIST_FOR_EACH_ENTRY(vtx, &cf->vtx, list) {
1628 r = r600_bytecode_vtx_build(bc, vtx, addr);
1629 if (r)
1630 return r;
1631 addr += 4;
1632 }
1633 } else if (cf->op == CF_OP_TEX) {
1634 LIST_FOR_EACH_ENTRY(vtx, &cf->vtx, list) {
1635 assert(bc->chip_class >= EVERGREEN);
1636 r = r600_bytecode_vtx_build(bc, vtx, addr);
1637 if (r)
1638 return r;
1639 addr += 4;
1640 }
1641 LIST_FOR_EACH_ENTRY(tex, &cf->tex, list) {
1642 r = r600_bytecode_tex_build(bc, tex, addr);
1643 if (r)
1644 return r;
1645 addr += 4;
1646 }
1647 }
1648 }
1649 return 0;
1650 }
1651
1652 void r600_bytecode_clear(struct r600_bytecode *bc)
1653 {
1654 struct r600_bytecode_cf *cf = NULL, *next_cf;
1655
1656 free(bc->bytecode);
1657 bc->bytecode = NULL;
1658
1659 LIST_FOR_EACH_ENTRY_SAFE(cf, next_cf, &bc->cf, list) {
1660 struct r600_bytecode_alu *alu = NULL, *next_alu;
1661 struct r600_bytecode_tex *tex = NULL, *next_tex;
1662 struct r600_bytecode_tex *vtx = NULL, *next_vtx;
1663
1664 LIST_FOR_EACH_ENTRY_SAFE(alu, next_alu, &cf->alu, list) {
1665 free(alu);
1666 }
1667
1668 LIST_INITHEAD(&cf->alu);
1669
1670 LIST_FOR_EACH_ENTRY_SAFE(tex, next_tex, &cf->tex, list) {
1671 free(tex);
1672 }
1673
1674 LIST_INITHEAD(&cf->tex);
1675
1676 LIST_FOR_EACH_ENTRY_SAFE(vtx, next_vtx, &cf->vtx, list) {
1677 free(vtx);
1678 }
1679
1680 LIST_INITHEAD(&cf->vtx);
1681
1682 free(cf);
1683 }
1684
1685 LIST_INITHEAD(&cf->list);
1686 }
1687
1688 static int print_swizzle(unsigned swz)
1689 {
1690 const char * swzchars = "xyzw01?_";
1691 assert(swz<8 && swz != 6);
1692 return fprintf(stderr, "%c", swzchars[swz]);
1693 }
1694
1695 static int print_sel(unsigned sel, unsigned rel, unsigned index_mode,
1696 unsigned need_brackets)
1697 {
1698 int o = 0;
1699 if (rel && index_mode >= 5 && sel < 128)
1700 o += fprintf(stderr, "G");
1701 if (rel || need_brackets) {
1702 o += fprintf(stderr, "[");
1703 }
1704 o += fprintf(stderr, "%d", sel);
1705 if (rel) {
1706 if (index_mode == 0 || index_mode == 6)
1707 o += fprintf(stderr, "+AR");
1708 else if (index_mode == 4)
1709 o += fprintf(stderr, "+AL");
1710 }
1711 if (rel || need_brackets) {
1712 o += fprintf(stderr, "]");
1713 }
1714 return o;
1715 }
1716
1717 static int print_dst(struct r600_bytecode_alu *alu)
1718 {
1719 int o = 0;
1720 unsigned sel = alu->dst.sel;
1721 char reg_char = 'R';
1722 if (sel > 128 - 4) { /* clause temporary gpr */
1723 sel -= 128 - 4;
1724 reg_char = 'T';
1725 }
1726
1727 if (alu->dst.write || alu->is_op3) {
1728 o += fprintf(stderr, "%c", reg_char);
1729 o += print_sel(alu->dst.sel, alu->dst.rel, alu->index_mode, 0);
1730 } else {
1731 o += fprintf(stderr, "__");
1732 }
1733 o += fprintf(stderr, ".");
1734 o += print_swizzle(alu->dst.chan);
1735 return o;
1736 }
1737
1738 static int print_src(struct r600_bytecode_alu *alu, unsigned idx)
1739 {
1740 int o = 0;
1741 struct r600_bytecode_alu_src *src = &alu->src[idx];
1742 unsigned sel = src->sel, need_sel = 1, need_chan = 1, need_brackets = 0;
1743
1744 if (src->neg)
1745 o += fprintf(stderr,"-");
1746 if (src->abs)
1747 o += fprintf(stderr,"|");
1748
1749 if (sel < 128 - 4) {
1750 o += fprintf(stderr, "R");
1751 } else if (sel < 128) {
1752 o += fprintf(stderr, "T");
1753 sel -= 128 - 4;
1754 } else if (sel < 160) {
1755 o += fprintf(stderr, "KC0");
1756 need_brackets = 1;
1757 sel -= 128;
1758 } else if (sel < 192) {
1759 o += fprintf(stderr, "KC1");
1760 need_brackets = 1;
1761 sel -= 160;
1762 } else if (sel >= 512) {
1763 o += fprintf(stderr, "C%d", src->kc_bank);
1764 need_brackets = 1;
1765 sel -= 512;
1766 } else if (sel >= 448) {
1767 o += fprintf(stderr, "Param");
1768 sel -= 448;
1769 need_chan = 0;
1770 } else if (sel >= 288) {
1771 o += fprintf(stderr, "KC3");
1772 need_brackets = 1;
1773 sel -= 288;
1774 } else if (sel >= 256) {
1775 o += fprintf(stderr, "KC2");
1776 need_brackets = 1;
1777 sel -= 256;
1778 } else {
1779 need_sel = 0;
1780 need_chan = 0;
1781 switch (sel) {
1782 case V_SQ_ALU_SRC_PS:
1783 o += fprintf(stderr, "PS");
1784 break;
1785 case V_SQ_ALU_SRC_PV:
1786 o += fprintf(stderr, "PV");
1787 need_chan = 1;
1788 break;
1789 case V_SQ_ALU_SRC_LITERAL:
1790 o += fprintf(stderr, "[0x%08X %f]", src->value, *(float*)&src->value);
1791 break;
1792 case V_SQ_ALU_SRC_0_5:
1793 o += fprintf(stderr, "0.5");
1794 break;
1795 case V_SQ_ALU_SRC_M_1_INT:
1796 o += fprintf(stderr, "-1");
1797 break;
1798 case V_SQ_ALU_SRC_1_INT:
1799 o += fprintf(stderr, "1");
1800 break;
1801 case V_SQ_ALU_SRC_1:
1802 o += fprintf(stderr, "1.0");
1803 break;
1804 case V_SQ_ALU_SRC_0:
1805 o += fprintf(stderr, "0");
1806 break;
1807 default:
1808 o += fprintf(stderr, "??IMM_%d", sel);
1809 break;
1810 }
1811 }
1812
1813 if (need_sel)
1814 o += print_sel(sel, src->rel, alu->index_mode, need_brackets);
1815
1816 if (need_chan) {
1817 o += fprintf(stderr, ".");
1818 o += print_swizzle(src->chan);
1819 }
1820
1821 if (src->abs)
1822 o += fprintf(stderr,"|");
1823
1824 return o;
1825 }
1826
1827 static int print_indent(int p, int c)
1828 {
1829 int o = 0;
1830 while (p++ < c)
1831 o += fprintf(stderr, " ");
1832 return o;
1833 }
1834
1835 void r600_bytecode_disasm(struct r600_bytecode *bc)
1836 {
1837 static int index = 0;
1838 struct r600_bytecode_cf *cf = NULL;
1839 struct r600_bytecode_alu *alu = NULL;
1840 struct r600_bytecode_vtx *vtx = NULL;
1841 struct r600_bytecode_tex *tex = NULL;
1842
1843 unsigned i, id, ngr = 0, last;
1844 uint32_t literal[4];
1845 unsigned nliteral;
1846 char chip = '6';
1847
1848 switch (bc->chip_class) {
1849 case R700:
1850 chip = '7';
1851 break;
1852 case EVERGREEN:
1853 chip = 'E';
1854 break;
1855 case CAYMAN:
1856 chip = 'C';
1857 break;
1858 case R600:
1859 default:
1860 chip = '6';
1861 break;
1862 }
1863 fprintf(stderr, "bytecode %d dw -- %d gprs -- %d nstack -------------\n",
1864 bc->ndw, bc->ngpr, bc->nstack);
1865 fprintf(stderr, "shader %d -- %c\n", index++, chip);
1866
1867 LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {
1868 id = cf->id;
1869 if (cf->op == CF_NATIVE) {
1870 fprintf(stderr, "%04d %08X %08X CF_NATIVE\n", id, bc->bytecode[id],
1871 bc->bytecode[id + 1]);
1872 } else {
1873 const struct cf_op_info *cfop = r600_isa_cf(cf->op);
1874 if (cfop->flags & CF_ALU) {
1875 if (cf->eg_alu_extended) {
1876 fprintf(stderr, "%04d %08X %08X %s\n", id, bc->bytecode[id],
1877 bc->bytecode[id + 1], "ALU_EXT");
1878 id += 2;
1879 }
1880 fprintf(stderr, "%04d %08X %08X %s ", id, bc->bytecode[id],
1881 bc->bytecode[id + 1], cfop->name);
1882 fprintf(stderr, "%d @%d ", cf->ndw / 2, cf->addr);
1883 for (i = 0; i < 4; ++i) {
1884 if (cf->kcache[i].mode) {
1885 int c_start = (cf->kcache[i].addr << 4);
1886 int c_end = c_start + (cf->kcache[i].mode << 4);
1887 fprintf(stderr, "KC%d[CB%d:%d-%d] ",
1888 i, cf->kcache[i].bank, c_start, c_end);
1889 }
1890 }
1891 fprintf(stderr, "\n");
1892 } else if (cfop->flags & CF_FETCH) {
1893 fprintf(stderr, "%04d %08X %08X %s ", id, bc->bytecode[id],
1894 bc->bytecode[id + 1], cfop->name);
1895 fprintf(stderr, "%d @%d ", cf->ndw / 4, cf->addr);
1896 fprintf(stderr, "\n");
1897 } else if (cfop->flags & CF_EXP) {
1898 int o = 0;
1899 const char *exp_type[] = {"PIXEL", "POS ", "PARAM"};
1900 o += fprintf(stderr, "%04d %08X %08X %s ", id, bc->bytecode[id],
1901 bc->bytecode[id + 1], cfop->name);
1902 o += print_indent(o, 43);
1903 o += fprintf(stderr, "%s ", exp_type[cf->output.type]);
1904 if (cf->output.burst_count > 1) {
1905 o += fprintf(stderr, "%d-%d ", cf->output.array_base,
1906 cf->output.array_base + cf->output.burst_count - 1);
1907
1908 o += print_indent(o, 55);
1909 o += fprintf(stderr, "R%d-%d.", cf->output.gpr,
1910 cf->output.gpr + cf->output.burst_count - 1);
1911 } else {
1912 o += fprintf(stderr, "%d ", cf->output.array_base);
1913 o += print_indent(o, 55);
1914 o += fprintf(stderr, "R%d.", cf->output.gpr);
1915 }
1916
1917 o += print_swizzle(cf->output.swizzle_x);
1918 o += print_swizzle(cf->output.swizzle_y);
1919 o += print_swizzle(cf->output.swizzle_z);
1920 o += print_swizzle(cf->output.swizzle_w);
1921
1922 print_indent(o, 67);
1923
1924 fprintf(stderr, " ES:%X ", cf->output.elem_size);
1925 if (!cf->output.barrier)
1926 fprintf(stderr, "NO_BARRIER ");
1927 if (cf->output.end_of_program)
1928 fprintf(stderr, "EOP ");
1929 fprintf(stderr, "\n");
1930 } else if (r600_isa_cf(cf->op)->flags & CF_STRM) {
1931 int o = 0;
1932 const char *exp_type[] = {"WRITE", "WRITE_IND", "WRITE_ACK",
1933 "WRITE_IND_ACK"};
1934 o += fprintf(stderr, "%04d %08X %08X %s ", id,
1935 bc->bytecode[id], bc->bytecode[id + 1], cfop->name);
1936 o += print_indent(o, 43);
1937 o += fprintf(stderr, "%s ", exp_type[cf->output.type]);
1938 if (cf->output.burst_count > 1) {
1939 o += fprintf(stderr, "%d-%d ", cf->output.array_base,
1940 cf->output.array_base + cf->output.burst_count - 1);
1941 o += print_indent(o, 55);
1942 o += fprintf(stderr, "R%d-%d.", cf->output.gpr,
1943 cf->output.gpr + cf->output.burst_count - 1);
1944 } else {
1945 o += fprintf(stderr, "%d ", cf->output.array_base);
1946 o += print_indent(o, 55);
1947 o += fprintf(stderr, "R%d.", cf->output.gpr);
1948 }
1949 for (i = 0; i < 4; ++i) {
1950 if (cf->output.comp_mask & (1 << i))
1951 o += print_swizzle(i);
1952 else
1953 o += print_swizzle(7);
1954 }
1955
1956 o += print_indent(o, 67);
1957
1958 fprintf(stderr, " ES:%i ", cf->output.elem_size);
1959 if (cf->output.array_size != 0xFFF)
1960 fprintf(stderr, "AS:%i ", cf->output.array_size);
1961 if (!cf->output.barrier)
1962 fprintf(stderr, "NO_BARRIER ");
1963 if (cf->output.end_of_program)
1964 fprintf(stderr, "EOP ");
1965 fprintf(stderr, "\n");
1966 } else {
1967 fprintf(stderr, "%04d %08X %08X %s ", id, bc->bytecode[id],
1968 bc->bytecode[id + 1], cfop->name);
1969 fprintf(stderr, "@%d ", cf->cf_addr);
1970 if (cf->cond)
1971 fprintf(stderr, "CND:%X ", cf->cond);
1972 if (cf->pop_count)
1973 fprintf(stderr, "POP:%X ", cf->pop_count);
1974 fprintf(stderr, "\n");
1975 }
1976 }
1977
1978 id = cf->addr;
1979 nliteral = 0;
1980 last = 1;
1981 LIST_FOR_EACH_ENTRY(alu, &cf->alu, list) {
1982 const char *omod_str[] = {"","*2","*4","/2"};
1983 const struct alu_op_info *aop = r600_isa_alu(alu->op);
1984 int o = 0;
1985
1986 r600_bytecode_alu_nliterals(bc, alu, literal, &nliteral);
1987 o += fprintf(stderr, " %04d %08X %08X ", id, bc->bytecode[id], bc->bytecode[id+1]);
1988 if (last)
1989 o += fprintf(stderr, "%4d ", ++ngr);
1990 else
1991 o += fprintf(stderr, " ");
1992 o += fprintf(stderr, "%c%c %c ", alu->execute_mask ? 'M':' ',
1993 alu->update_pred ? 'P':' ',
1994 alu->pred_sel ? alu->pred_sel==2 ? '0':'1':' ');
1995
1996 o += fprintf(stderr, "%s%s%s ", aop->name,
1997 omod_str[alu->omod], alu->dst.clamp ? "_sat":"");
1998
1999 o += print_indent(o,60);
2000 o += print_dst(alu);
2001 for (i = 0; i < aop->src_count; ++i) {
2002 o += fprintf(stderr, i == 0 ? ", ": ", ");
2003 o += print_src(alu, i);
2004 }
2005
2006 if (alu->bank_swizzle) {
2007 o += print_indent(o,75);
2008 o += fprintf(stderr, " BS:%d", alu->bank_swizzle);
2009 }
2010
2011 fprintf(stderr, "\n");
2012 id += 2;
2013
2014 if (alu->last) {
2015 for (i = 0; i < nliteral; i++, id++) {
2016 float *f = (float*)(bc->bytecode + id);
2017 o = fprintf(stderr, " %04d %08X", id, bc->bytecode[id]);
2018 print_indent(o, 60);
2019 fprintf(stderr, " %f (%d)\n", *f, *(bc->bytecode + id));
2020 }
2021 id += nliteral & 1;
2022 nliteral = 0;
2023 }
2024 last = alu->last;
2025 }
2026
2027 LIST_FOR_EACH_ENTRY(tex, &cf->tex, list) {
2028 int o = 0;
2029 o += fprintf(stderr, " %04d %08X %08X %08X ", id, bc->bytecode[id],
2030 bc->bytecode[id + 1], bc->bytecode[id + 2]);
2031
2032 o += fprintf(stderr, "%s ", r600_isa_fetch(tex->op)->name);
2033
2034 o += print_indent(o, 50);
2035
2036 o += fprintf(stderr, "R%d.", tex->dst_gpr);
2037 o += print_swizzle(tex->dst_sel_x);
2038 o += print_swizzle(tex->dst_sel_y);
2039 o += print_swizzle(tex->dst_sel_z);
2040 o += print_swizzle(tex->dst_sel_w);
2041
2042 o += fprintf(stderr, ", R%d.", tex->src_gpr);
2043 o += print_swizzle(tex->src_sel_x);
2044 o += print_swizzle(tex->src_sel_y);
2045 o += print_swizzle(tex->src_sel_z);
2046 o += print_swizzle(tex->src_sel_w);
2047
2048 o += fprintf(stderr, ", RID:%d", tex->resource_id);
2049 o += fprintf(stderr, ", SID:%d ", tex->sampler_id);
2050
2051 if (tex->lod_bias)
2052 fprintf(stderr, "LB:%d ", tex->lod_bias);
2053
2054 fprintf(stderr, "CT:%c%c%c%c ",
2055 tex->coord_type_x ? 'N' : 'U',
2056 tex->coord_type_y ? 'N' : 'U',
2057 tex->coord_type_z ? 'N' : 'U',
2058 tex->coord_type_w ? 'N' : 'U');
2059
2060 if (tex->offset_x)
2061 fprintf(stderr, "OX:%d ", tex->offset_x);
2062 if (tex->offset_y)
2063 fprintf(stderr, "OY:%d ", tex->offset_y);
2064 if (tex->offset_z)
2065 fprintf(stderr, "OZ:%d ", tex->offset_z);
2066
2067 id += 4;
2068 fprintf(stderr, "\n");
2069 }
2070
2071 LIST_FOR_EACH_ENTRY(vtx, &cf->vtx, list) {
2072 int o = 0;
2073 const char * fetch_type[] = {"VERTEX", "INSTANCE", ""};
2074 o += fprintf(stderr, " %04d %08X %08X %08X ", id, bc->bytecode[id],
2075 bc->bytecode[id + 1], bc->bytecode[id + 2]);
2076
2077 o += fprintf(stderr, "%s ", r600_isa_fetch(vtx->op)->name);
2078
2079 o += print_indent(o, 50);
2080
2081 o += fprintf(stderr, "R%d.", vtx->dst_gpr);
2082 o += print_swizzle(vtx->dst_sel_x);
2083 o += print_swizzle(vtx->dst_sel_y);
2084 o += print_swizzle(vtx->dst_sel_z);
2085 o += print_swizzle(vtx->dst_sel_w);
2086
2087 o += fprintf(stderr, ", R%d.", vtx->src_gpr);
2088 o += print_swizzle(vtx->src_sel_x);
2089
2090 if (vtx->offset)
2091 fprintf(stderr, " +%db", vtx->offset);
2092
2093 o += print_indent(o, 55);
2094
2095 fprintf(stderr, ", RID:%d ", vtx->buffer_id);
2096
2097 fprintf(stderr, "%s ", fetch_type[vtx->fetch_type]);
2098
2099 if (bc->chip_class < CAYMAN && vtx->mega_fetch_count)
2100 fprintf(stderr, "MFC:%d ", vtx->mega_fetch_count);
2101
2102 fprintf(stderr, "UCF:%d ", vtx->use_const_fields);
2103 fprintf(stderr, "FMT(DTA:%d ", vtx->data_format);
2104 fprintf(stderr, "NUM:%d ", vtx->num_format_all);
2105 fprintf(stderr, "COMP:%d ", vtx->format_comp_all);
2106 fprintf(stderr, "MODE:%d)\n", vtx->srf_mode_all);
2107
2108 id += 4;
2109 }
2110 }
2111
2112 fprintf(stderr, "--------------------------------------\n");
2113 }
2114
2115 void r600_vertex_data_type(enum pipe_format pformat,
2116 unsigned *format,
2117 unsigned *num_format, unsigned *format_comp, unsigned *endian)
2118 {
2119 const struct util_format_description *desc;
2120 unsigned i;
2121
2122 *format = 0;
2123 *num_format = 0;
2124 *format_comp = 0;
2125 *endian = ENDIAN_NONE;
2126
2127 desc = util_format_description(pformat);
2128 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
2129 goto out_unknown;
2130 }
2131
2132 /* Find the first non-VOID channel. */
2133 for (i = 0; i < 4; i++) {
2134 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
2135 break;
2136 }
2137 }
2138
2139 *endian = r600_endian_swap(desc->channel[i].size);
2140
2141 switch (desc->channel[i].type) {
2142 /* Half-floats, floats, ints */
2143 case UTIL_FORMAT_TYPE_FLOAT:
2144 switch (desc->channel[i].size) {
2145 case 16:
2146 switch (desc->nr_channels) {
2147 case 1:
2148 *format = FMT_16_FLOAT;
2149 break;
2150 case 2:
2151 *format = FMT_16_16_FLOAT;
2152 break;
2153 case 3:
2154 case 4:
2155 *format = FMT_16_16_16_16_FLOAT;
2156 break;
2157 }
2158 break;
2159 case 32:
2160 switch (desc->nr_channels) {
2161 case 1:
2162 *format = FMT_32_FLOAT;
2163 break;
2164 case 2:
2165 *format = FMT_32_32_FLOAT;
2166 break;
2167 case 3:
2168 *format = FMT_32_32_32_FLOAT;
2169 break;
2170 case 4:
2171 *format = FMT_32_32_32_32_FLOAT;
2172 break;
2173 }
2174 break;
2175 default:
2176 goto out_unknown;
2177 }
2178 break;
2179 /* Unsigned ints */
2180 case UTIL_FORMAT_TYPE_UNSIGNED:
2181 /* Signed ints */
2182 case UTIL_FORMAT_TYPE_SIGNED:
2183 switch (desc->channel[i].size) {
2184 case 8:
2185 switch (desc->nr_channels) {
2186 case 1:
2187 *format = FMT_8;
2188 break;
2189 case 2:
2190 *format = FMT_8_8;
2191 break;
2192 case 3:
2193 case 4:
2194 *format = FMT_8_8_8_8;
2195 break;
2196 }
2197 break;
2198 case 10:
2199 if (desc->nr_channels != 4)
2200 goto out_unknown;
2201
2202 *format = FMT_2_10_10_10;
2203 break;
2204 case 16:
2205 switch (desc->nr_channels) {
2206 case 1:
2207 *format = FMT_16;
2208 break;
2209 case 2:
2210 *format = FMT_16_16;
2211 break;
2212 case 3:
2213 case 4:
2214 *format = FMT_16_16_16_16;
2215 break;
2216 }
2217 break;
2218 case 32:
2219 switch (desc->nr_channels) {
2220 case 1:
2221 *format = FMT_32;
2222 break;
2223 case 2:
2224 *format = FMT_32_32;
2225 break;
2226 case 3:
2227 *format = FMT_32_32_32;
2228 break;
2229 case 4:
2230 *format = FMT_32_32_32_32;
2231 break;
2232 }
2233 break;
2234 default:
2235 goto out_unknown;
2236 }
2237 break;
2238 default:
2239 goto out_unknown;
2240 }
2241
2242 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
2243 *format_comp = 1;
2244 }
2245
2246 *num_format = 0;
2247 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED ||
2248 desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
2249 if (!desc->channel[i].normalized) {
2250 if (desc->channel[i].pure_integer)
2251 *num_format = 1;
2252 else
2253 *num_format = 2;
2254 }
2255 }
2256 return;
2257 out_unknown:
2258 R600_ERR("unsupported vertex format %s\n", util_format_name(pformat));
2259 }
2260
2261 void *r600_create_vertex_fetch_shader(struct pipe_context *ctx,
2262 unsigned count,
2263 const struct pipe_vertex_element *elements)
2264 {
2265 struct r600_context *rctx = (struct r600_context *)ctx;
2266 struct r600_bytecode bc;
2267 struct r600_bytecode_vtx vtx;
2268 const struct util_format_description *desc;
2269 unsigned fetch_resource_start = rctx->chip_class >= EVERGREEN ? 0 : 160;
2270 unsigned format, num_format, format_comp, endian;
2271 uint32_t *bytecode;
2272 int i, j, r, fs_size;
2273 struct r600_fetch_shader *shader;
2274
2275 assert(count < 32);
2276
2277 memset(&bc, 0, sizeof(bc));
2278 r600_bytecode_init(&bc, rctx->chip_class, rctx->family,
2279 rctx->screen->msaa_texture_support);
2280
2281 bc.isa = rctx->isa;
2282
2283 for (i = 0; i < count; i++) {
2284 if (elements[i].instance_divisor > 1) {
2285 if (rctx->chip_class == CAYMAN) {
2286 for (j = 0; j < 4; j++) {
2287 struct r600_bytecode_alu alu;
2288 memset(&alu, 0, sizeof(alu));
2289 alu.op = ALU_OP2_MULHI_UINT;
2290 alu.src[0].sel = 0;
2291 alu.src[0].chan = 3;
2292 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
2293 alu.src[1].value = (1ll << 32) / elements[i].instance_divisor + 1;
2294 alu.dst.sel = i + 1;
2295 alu.dst.chan = j;
2296 alu.dst.write = j == 3;
2297 alu.last = j == 3;
2298 if ((r = r600_bytecode_add_alu(&bc, &alu))) {
2299 r600_bytecode_clear(&bc);
2300 return NULL;
2301 }
2302 }
2303 } else {
2304 struct r600_bytecode_alu alu;
2305 memset(&alu, 0, sizeof(alu));
2306 alu.op = ALU_OP2_MULHI_UINT;
2307 alu.src[0].sel = 0;
2308 alu.src[0].chan = 3;
2309 alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;
2310 alu.src[1].value = (1ll << 32) / elements[i].instance_divisor + 1;
2311 alu.dst.sel = i + 1;
2312 alu.dst.chan = 3;
2313 alu.dst.write = 1;
2314 alu.last = 1;
2315 if ((r = r600_bytecode_add_alu(&bc, &alu))) {
2316 r600_bytecode_clear(&bc);
2317 return NULL;
2318 }
2319 }
2320 }
2321 }
2322
2323 for (i = 0; i < count; i++) {
2324 r600_vertex_data_type(elements[i].src_format,
2325 &format, &num_format, &format_comp, &endian);
2326
2327 desc = util_format_description(elements[i].src_format);
2328 if (desc == NULL) {
2329 r600_bytecode_clear(&bc);
2330 R600_ERR("unknown format %d\n", elements[i].src_format);
2331 return NULL;
2332 }
2333
2334 if (elements[i].src_offset > 65535) {
2335 r600_bytecode_clear(&bc);
2336 R600_ERR("too big src_offset: %u\n", elements[i].src_offset);
2337 return NULL;
2338 }
2339
2340 memset(&vtx, 0, sizeof(vtx));
2341 vtx.buffer_id = elements[i].vertex_buffer_index + fetch_resource_start;
2342 vtx.fetch_type = elements[i].instance_divisor ? 1 : 0;
2343 vtx.src_gpr = elements[i].instance_divisor > 1 ? i + 1 : 0;
2344 vtx.src_sel_x = elements[i].instance_divisor ? 3 : 0;
2345 vtx.mega_fetch_count = 0x1F;
2346 vtx.dst_gpr = i + 1;
2347 vtx.dst_sel_x = desc->swizzle[0];
2348 vtx.dst_sel_y = desc->swizzle[1];
2349 vtx.dst_sel_z = desc->swizzle[2];
2350 vtx.dst_sel_w = desc->swizzle[3];
2351 vtx.data_format = format;
2352 vtx.num_format_all = num_format;
2353 vtx.format_comp_all = format_comp;
2354 vtx.srf_mode_all = 1;
2355 vtx.offset = elements[i].src_offset;
2356 vtx.endian = endian;
2357
2358 if ((r = r600_bytecode_add_vtx(&bc, &vtx))) {
2359 r600_bytecode_clear(&bc);
2360 return NULL;
2361 }
2362 }
2363
2364 r600_bytecode_add_cfinst(&bc, CF_OP_RET);
2365
2366 if ((r = r600_bytecode_build(&bc))) {
2367 r600_bytecode_clear(&bc);
2368 return NULL;
2369 }
2370
2371 if (rctx->screen->debug_flags & DBG_FS) {
2372 fprintf(stderr, "--------------------------------------------------------------\n");
2373 fprintf(stderr, "Vertex elements state:\n");
2374 for (i = 0; i < count; i++) {
2375 fprintf(stderr, " ");
2376 util_dump_vertex_element(stderr, elements+i);
2377 fprintf(stderr, "\n");
2378 }
2379
2380 r600_bytecode_disasm(&bc);
2381 fprintf(stderr, "______________________________________________________________\n");
2382 }
2383
2384 fs_size = bc.ndw*4;
2385
2386 /* Allocate the CSO. */
2387 shader = CALLOC_STRUCT(r600_fetch_shader);
2388 if (!shader) {
2389 r600_bytecode_clear(&bc);
2390 return NULL;
2391 }
2392
2393 u_suballocator_alloc(rctx->allocator_fetch_shader, fs_size, &shader->offset,
2394 (struct pipe_resource**)&shader->buffer);
2395 if (!shader->buffer) {
2396 r600_bytecode_clear(&bc);
2397 FREE(shader);
2398 return NULL;
2399 }
2400
2401 bytecode = r600_buffer_mmap_sync_with_rings(rctx, shader->buffer, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED);
2402 bytecode += shader->offset / 4;
2403
2404 if (R600_BIG_ENDIAN) {
2405 for (i = 0; i < fs_size / 4; ++i) {
2406 bytecode[i] = bswap_32(bc.bytecode[i]);
2407 }
2408 } else {
2409 memcpy(bytecode, bc.bytecode, fs_size);
2410 }
2411 rctx->ws->buffer_unmap(shader->buffer->cs_buf);
2412
2413 r600_bytecode_clear(&bc);
2414 return shader;
2415 }
2416
2417 void r600_bytecode_alu_read(struct r600_bytecode *bc,
2418 struct r600_bytecode_alu *alu, uint32_t word0, uint32_t word1)
2419 {
2420 /* WORD0 */
2421 alu->src[0].sel = G_SQ_ALU_WORD0_SRC0_SEL(word0);
2422 alu->src[0].rel = G_SQ_ALU_WORD0_SRC0_REL(word0);
2423 alu->src[0].chan = G_SQ_ALU_WORD0_SRC0_CHAN(word0);
2424 alu->src[0].neg = G_SQ_ALU_WORD0_SRC0_NEG(word0);
2425 alu->src[1].sel = G_SQ_ALU_WORD0_SRC1_SEL(word0);
2426 alu->src[1].rel = G_SQ_ALU_WORD0_SRC1_REL(word0);
2427 alu->src[1].chan = G_SQ_ALU_WORD0_SRC1_CHAN(word0);
2428 alu->src[1].neg = G_SQ_ALU_WORD0_SRC1_NEG(word0);
2429 alu->index_mode = G_SQ_ALU_WORD0_INDEX_MODE(word0);
2430 alu->pred_sel = G_SQ_ALU_WORD0_PRED_SEL(word0);
2431 alu->last = G_SQ_ALU_WORD0_LAST(word0);
2432
2433 /* WORD1 */
2434 alu->bank_swizzle = G_SQ_ALU_WORD1_BANK_SWIZZLE(word1);
2435 if (alu->bank_swizzle)
2436 alu->bank_swizzle_force = alu->bank_swizzle;
2437 alu->dst.sel = G_SQ_ALU_WORD1_DST_GPR(word1);
2438 alu->dst.rel = G_SQ_ALU_WORD1_DST_REL(word1);
2439 alu->dst.chan = G_SQ_ALU_WORD1_DST_CHAN(word1);
2440 alu->dst.clamp = G_SQ_ALU_WORD1_CLAMP(word1);
2441 if (G_SQ_ALU_WORD1_ENCODING(word1)) /*ALU_DWORD1_OP3*/
2442 {
2443 alu->is_op3 = 1;
2444 alu->src[2].sel = G_SQ_ALU_WORD1_OP3_SRC2_SEL(word1);
2445 alu->src[2].rel = G_SQ_ALU_WORD1_OP3_SRC2_REL(word1);
2446 alu->src[2].chan = G_SQ_ALU_WORD1_OP3_SRC2_CHAN(word1);
2447 alu->src[2].neg = G_SQ_ALU_WORD1_OP3_SRC2_NEG(word1);
2448 alu->op = r600_isa_alu_by_opcode(bc->isa,
2449 G_SQ_ALU_WORD1_OP3_ALU_INST(word1), /* is_op3 = */ 1);
2450
2451 }
2452 else /*ALU_DWORD1_OP2*/
2453 {
2454 alu->src[0].abs = G_SQ_ALU_WORD1_OP2_SRC0_ABS(word1);
2455 alu->src[1].abs = G_SQ_ALU_WORD1_OP2_SRC1_ABS(word1);
2456 alu->op = r600_isa_alu_by_opcode(bc->isa,
2457 G_SQ_ALU_WORD1_OP2_ALU_INST(word1), /* is_op3 = */ 0);
2458 alu->omod = G_SQ_ALU_WORD1_OP2_OMOD(word1);
2459 alu->dst.write = G_SQ_ALU_WORD1_OP2_WRITE_MASK(word1);
2460 alu->update_pred = G_SQ_ALU_WORD1_OP2_UPDATE_PRED(word1);
2461 alu->execute_mask =
2462 G_SQ_ALU_WORD1_OP2_UPDATE_EXECUTE_MASK(word1);
2463 }
2464 }
2465
2466 void r600_bytecode_export_read(struct r600_bytecode *bc,
2467 struct r600_bytecode_output *output, uint32_t word0, uint32_t word1)
2468 {
2469 output->array_base = G_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(word0);
2470 output->type = G_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(word0);
2471 output->gpr = G_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(word0);
2472 output->elem_size = G_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(word0);
2473
2474 output->swizzle_x = G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(word1);
2475 output->swizzle_y = G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(word1);
2476 output->swizzle_z = G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(word1);
2477 output->swizzle_w = G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(word1);
2478 output->burst_count = G_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(word1);
2479 output->end_of_program = G_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(word1);
2480 output->op = r600_isa_cf_by_opcode(bc->isa,
2481 G_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(word1), 0);
2482 output->barrier = G_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(word1);
2483 output->array_size = G_SQ_CF_ALLOC_EXPORT_WORD1_BUF_ARRAY_SIZE(word1);
2484 output->comp_mask = G_SQ_CF_ALLOC_EXPORT_WORD1_BUF_COMP_MASK(word1);
2485 }