r600g: avoid reemiting literal, avoid scheduling empty cs
[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_asm.h"
24 #include "r600_context.h"
25 #include "util/u_memory.h"
26 #include "r600_sq.h"
27 #include <stdio.h>
28 #include <errno.h>
29
30 int r700_bc_alu_build(struct r600_bc *bc, struct r600_bc_alu *alu, unsigned id);
31
32 static struct r600_bc_cf *r600_bc_cf(void)
33 {
34 struct r600_bc_cf *cf = CALLOC_STRUCT(r600_bc_cf);
35
36 if (cf == NULL)
37 return NULL;
38 LIST_INITHEAD(&cf->list);
39 LIST_INITHEAD(&cf->alu);
40 LIST_INITHEAD(&cf->vtx);
41 LIST_INITHEAD(&cf->tex);
42 return cf;
43 }
44
45 static struct r600_bc_alu *r600_bc_alu(void)
46 {
47 struct r600_bc_alu *alu = CALLOC_STRUCT(r600_bc_alu);
48
49 if (alu == NULL)
50 return NULL;
51 LIST_INITHEAD(&alu->list);
52 return alu;
53 }
54
55 static struct r600_bc_vtx *r600_bc_vtx(void)
56 {
57 struct r600_bc_vtx *vtx = CALLOC_STRUCT(r600_bc_vtx);
58
59 if (vtx == NULL)
60 return NULL;
61 LIST_INITHEAD(&vtx->list);
62 return vtx;
63 }
64
65 static struct r600_bc_tex *r600_bc_tex(void)
66 {
67 struct r600_bc_tex *tex = CALLOC_STRUCT(r600_bc_tex);
68
69 if (tex == NULL)
70 return NULL;
71 LIST_INITHEAD(&tex->list);
72 return tex;
73 }
74
75 int r600_bc_init(struct r600_bc *bc, enum radeon_family family)
76 {
77 LIST_INITHEAD(&bc->cf);
78 bc->family = family;
79 return 0;
80 }
81
82 static int r600_bc_add_cf(struct r600_bc *bc)
83 {
84 struct r600_bc_cf *cf = r600_bc_cf();
85
86 if (cf == NULL)
87 return -ENOMEM;
88 LIST_ADDTAIL(&cf->list, &bc->cf);
89 if (bc->cf_last)
90 cf->id = bc->cf_last->id + 2;
91 bc->cf_last = cf;
92 bc->ncf++;
93 bc->ndw += 2;
94 bc->force_add_cf = 0;
95 return 0;
96 }
97
98 int r600_bc_add_output(struct r600_bc *bc, const struct r600_bc_output *output)
99 {
100 int r;
101
102 r = r600_bc_add_cf(bc);
103 if (r)
104 return r;
105 bc->cf_last->inst = output->inst;
106 memcpy(&bc->cf_last->output, output, sizeof(struct r600_bc_output));
107 return 0;
108 }
109
110 int r600_bc_add_alu(struct r600_bc *bc, const struct r600_bc_alu *alu)
111 {
112 struct r600_bc_alu *nalu = r600_bc_alu();
113 struct r600_bc_alu *lalu;
114 int i, r;
115
116 if (nalu == NULL)
117 return -ENOMEM;
118 memcpy(nalu, alu, sizeof(struct r600_bc_alu));
119 nalu->nliteral = 0;
120
121 /* cf can contains only alu or only vtx or only tex */
122 if (bc->cf_last == NULL || bc->cf_last->inst != (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU << 3) ||
123 bc->force_add_cf) {
124 /* at most 128 slots, one add alu can add 4 slots + 4 constant worst case */
125 r = r600_bc_add_cf(bc);
126 if (r) {
127 free(nalu);
128 return r;
129 }
130 bc->cf_last->inst = V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU << 3;
131 }
132 if (alu->last && (bc->cf_last->ndw >> 1) >= 124) {
133 bc->force_add_cf = 1;
134 }
135 /* number of gpr == the last gpr used in any alu */
136 for (i = 0; i < 3; i++) {
137 if (alu->src[i].sel >= bc->ngpr && alu->src[i].sel < 128) {
138 bc->ngpr = alu->src[i].sel + 1;
139 }
140 /* compute how many literal are needed
141 * either 2 or 4 literals
142 */
143 if (alu->src[i].sel == 253) {
144 if (((alu->src[i].chan + 2) & 0x6) > nalu->nliteral) {
145 nalu->nliteral = (alu->src[i].chan + 2) & 0x6;
146 }
147 }
148 }
149 if (!LIST_IS_EMPTY(&bc->cf_last->alu)) {
150 lalu = LIST_ENTRY(struct r600_bc_alu, bc->cf_last->alu.prev, list);
151 if (!lalu->last && lalu->nliteral > nalu->nliteral) {
152 nalu->nliteral = lalu->nliteral;
153 }
154 }
155 if (alu->dst.sel >= bc->ngpr) {
156 bc->ngpr = alu->dst.sel + 1;
157 }
158 LIST_ADDTAIL(&nalu->list, &bc->cf_last->alu);
159 /* each alu use 2 dwords */
160 bc->cf_last->ndw += 2;
161 bc->ndw += 2;
162 return 0;
163 }
164
165 int r600_bc_add_literal(struct r600_bc *bc, const u32 *value)
166 {
167 struct r600_bc_alu *alu;
168
169 if (bc->cf_last == NULL) {
170 R600_ERR("no last CF\n");
171 return -EINVAL;
172 }
173 if (bc->cf_last->inst == V_SQ_CF_WORD1_SQ_CF_INST_TEX) {
174 return 0;
175 }
176 if (bc->cf_last->inst != (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU << 3) ||
177 LIST_IS_EMPTY(&bc->cf_last->alu)) {
178 R600_ERR("last CF is not ALU (%p)\n", bc->cf_last);
179 return -EINVAL;
180 }
181 alu = LIST_ENTRY(struct r600_bc_alu, bc->cf_last->alu.prev, list);
182 if (!alu->last || !alu->nliteral || alu->literal_added) {
183 return 0;
184 }
185 memcpy(alu->value, value, 4 * 4);
186 bc->cf_last->ndw += alu->nliteral;
187 bc->ndw += alu->nliteral;
188 alu->literal_added = 1;
189 return 0;
190 }
191
192 int r600_bc_add_vtx(struct r600_bc *bc, const struct r600_bc_vtx *vtx)
193 {
194 struct r600_bc_vtx *nvtx = r600_bc_vtx();
195 int r;
196
197 if (nvtx == NULL)
198 return -ENOMEM;
199 memcpy(nvtx, vtx, sizeof(struct r600_bc_vtx));
200
201 /* cf can contains only alu or only vtx or only tex */
202 if (bc->cf_last == NULL ||
203 (bc->cf_last->inst != V_SQ_CF_WORD1_SQ_CF_INST_VTX &&
204 bc->cf_last->inst != V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC)) {
205 r = r600_bc_add_cf(bc);
206 if (r) {
207 free(nvtx);
208 return r;
209 }
210 bc->cf_last->inst = V_SQ_CF_WORD1_SQ_CF_INST_VTX;
211 }
212 LIST_ADDTAIL(&nvtx->list, &bc->cf_last->vtx);
213 /* each fetch use 4 dwords */
214 bc->cf_last->ndw += 4;
215 bc->ndw += 4;
216 return 0;
217 }
218
219 int r600_bc_add_tex(struct r600_bc *bc, const struct r600_bc_tex *tex)
220 {
221 struct r600_bc_tex *ntex = r600_bc_tex();
222 int r;
223
224 if (ntex == NULL)
225 return -ENOMEM;
226 memcpy(ntex, tex, sizeof(struct r600_bc_tex));
227
228 /* cf can contains only alu or only vtx or only tex */
229 if (bc->cf_last == NULL ||
230 bc->cf_last->inst != V_SQ_CF_WORD1_SQ_CF_INST_TEX) {
231 r = r600_bc_add_cf(bc);
232 if (r) {
233 free(ntex);
234 return r;
235 }
236 bc->cf_last->inst = V_SQ_CF_WORD1_SQ_CF_INST_TEX;
237 }
238 LIST_ADDTAIL(&ntex->list, &bc->cf_last->tex);
239 /* each texture fetch use 4 dwords */
240 bc->cf_last->ndw += 4;
241 bc->ndw += 4;
242 return 0;
243 }
244
245 static int r600_bc_vtx_build(struct r600_bc *bc, struct r600_bc_vtx *vtx, unsigned id)
246 {
247 bc->bytecode[id++] = S_SQ_VTX_WORD0_BUFFER_ID(vtx->buffer_id) |
248 S_SQ_VTX_WORD0_SRC_GPR(vtx->src_gpr) |
249 S_SQ_VTX_WORD0_SRC_SEL_X(vtx->src_sel_x) |
250 S_SQ_VTX_WORD0_MEGA_FETCH_COUNT(vtx->mega_fetch_count);
251 bc->bytecode[id++] = S_SQ_VTX_WORD1_DST_SEL_X(vtx->dst_sel_x) |
252 S_SQ_VTX_WORD1_DST_SEL_Y(vtx->dst_sel_y) |
253 S_SQ_VTX_WORD1_DST_SEL_Z(vtx->dst_sel_z) |
254 S_SQ_VTX_WORD1_DST_SEL_W(vtx->dst_sel_w) |
255 S_SQ_VTX_WORD1_USE_CONST_FIELDS(1) |
256 S_SQ_VTX_WORD1_GPR_DST_GPR(vtx->dst_gpr);
257 bc->bytecode[id++] = S_SQ_VTX_WORD2_MEGA_FETCH(1);
258 bc->bytecode[id++] = 0;
259 return 0;
260 }
261
262 static int r600_bc_tex_build(struct r600_bc *bc, struct r600_bc_tex *tex, unsigned id)
263 {
264 bc->bytecode[id++] = S_SQ_TEX_WORD0_TEX_INST(tex->inst) |
265 S_SQ_TEX_WORD0_RESOURCE_ID(tex->resource_id) |
266 S_SQ_TEX_WORD0_SRC_GPR(tex->src_gpr) |
267 S_SQ_TEX_WORD0_SRC_REL(tex->src_rel);
268 bc->bytecode[id++] = S_SQ_TEX_WORD1_DST_GPR(tex->dst_gpr) |
269 S_SQ_TEX_WORD1_DST_REL(tex->dst_rel) |
270 S_SQ_TEX_WORD1_DST_SEL_X(tex->dst_sel_x) |
271 S_SQ_TEX_WORD1_DST_SEL_Y(tex->dst_sel_y) |
272 S_SQ_TEX_WORD1_DST_SEL_Z(tex->dst_sel_z) |
273 S_SQ_TEX_WORD1_DST_SEL_W(tex->dst_sel_w) |
274 S_SQ_TEX_WORD1_LOD_BIAS(tex->lod_bias) |
275 S_SQ_TEX_WORD1_COORD_TYPE_X(tex->coord_type_x) |
276 S_SQ_TEX_WORD1_COORD_TYPE_Y(tex->coord_type_y) |
277 S_SQ_TEX_WORD1_COORD_TYPE_Z(tex->coord_type_z) |
278 S_SQ_TEX_WORD1_COORD_TYPE_W(tex->coord_type_w);
279 bc->bytecode[id++] = S_SQ_TEX_WORD2_OFFSET_X(tex->offset_x) |
280 S_SQ_TEX_WORD2_OFFSET_Y(tex->offset_y) |
281 S_SQ_TEX_WORD2_OFFSET_Z(tex->offset_z) |
282 S_SQ_TEX_WORD2_SAMPLER_ID(tex->sampler_id) |
283 S_SQ_TEX_WORD2_SRC_SEL_X(tex->src_sel_x) |
284 S_SQ_TEX_WORD2_SRC_SEL_Y(tex->src_sel_y) |
285 S_SQ_TEX_WORD2_SRC_SEL_Z(tex->src_sel_z) |
286 S_SQ_TEX_WORD2_SRC_SEL_W(tex->src_sel_w);
287 bc->bytecode[id++] = 0;
288 return 0;
289 }
290
291 static int r600_bc_alu_build(struct r600_bc *bc, struct r600_bc_alu *alu, unsigned id)
292 {
293 unsigned i;
294
295 /* don't replace gpr by pv or ps for destination register */
296 if (alu->is_op3) {
297 bc->bytecode[id++] = S_SQ_ALU_WORD0_SRC0_SEL(alu->src[0].sel) |
298 S_SQ_ALU_WORD0_SRC0_CHAN(alu->src[0].chan) |
299 S_SQ_ALU_WORD0_SRC1_SEL(alu->src[1].sel) |
300 S_SQ_ALU_WORD0_SRC1_CHAN(alu->src[1].chan) |
301 S_SQ_ALU_WORD0_LAST(alu->last);
302 bc->bytecode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |
303 S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |
304 S_SQ_ALU_WORD1_CLAMP(alu->dst.clamp) |
305 S_SQ_ALU_WORD1_OP3_SRC2_SEL(alu->src[2].sel) |
306 S_SQ_ALU_WORD1_OP3_SRC2_CHAN(alu->src[2].chan) |
307 S_SQ_ALU_WORD1_OP3_SRC2_NEG(alu->src[2].neg) |
308 S_SQ_ALU_WORD1_OP3_ALU_INST(alu->inst) |
309 S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
310 } else {
311 bc->bytecode[id++] = S_SQ_ALU_WORD0_SRC0_SEL(alu->src[0].sel) |
312 S_SQ_ALU_WORD0_SRC0_CHAN(alu->src[0].chan) |
313 S_SQ_ALU_WORD0_SRC0_NEG(alu->src[0].neg) |
314 S_SQ_ALU_WORD0_SRC1_SEL(alu->src[1].sel) |
315 S_SQ_ALU_WORD0_SRC1_CHAN(alu->src[1].chan) |
316 S_SQ_ALU_WORD0_SRC1_NEG(alu->src[1].neg) |
317 S_SQ_ALU_WORD0_LAST(alu->last);
318 bc->bytecode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |
319 S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |
320 S_SQ_ALU_WORD1_CLAMP(alu->dst.clamp) |
321 S_SQ_ALU_WORD1_OP2_SRC0_ABS(alu->src[0].abs) |
322 S_SQ_ALU_WORD1_OP2_SRC1_ABS(alu->src[1].abs) |
323 S_SQ_ALU_WORD1_OP2_WRITE_MASK(alu->dst.write) |
324 S_SQ_ALU_WORD1_OP2_ALU_INST(alu->inst) |
325 S_SQ_ALU_WORD1_BANK_SWIZZLE(0);
326 }
327 if (alu->last) {
328 for (i = 0; i < alu->nliteral; i++) {
329 bc->bytecode[id++] = alu->value[i];
330 }
331 }
332 return 0;
333 }
334
335 static int r600_bc_cf_build(struct r600_bc *bc, struct r600_bc_cf *cf)
336 {
337 unsigned id = cf->id;
338
339 switch (cf->inst) {
340 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU << 3):
341 bc->bytecode[id++] = S_SQ_CF_ALU_WORD0_ADDR(cf->addr >> 1);
342 bc->bytecode[id++] = S_SQ_CF_ALU_WORD1_CF_INST(cf->inst >> 3) |
343 S_SQ_CF_ALU_WORD1_BARRIER(1) |
344 S_SQ_CF_ALU_WORD1_COUNT((cf->ndw / 2) - 1);
345 break;
346 case V_SQ_CF_WORD1_SQ_CF_INST_TEX:
347 case V_SQ_CF_WORD1_SQ_CF_INST_VTX:
348 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC:
349 bc->bytecode[id++] = S_SQ_CF_WORD0_ADDR(cf->addr >> 1);
350 bc->bytecode[id++] = S_SQ_CF_WORD1_CF_INST(cf->inst) |
351 S_SQ_CF_WORD1_BARRIER(1) |
352 S_SQ_CF_WORD1_COUNT((cf->ndw / 4) - 1);
353 break;
354 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT:
355 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE:
356 bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf->output.gpr) |
357 S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf->output.elem_size) |
358 S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf->output.array_base) |
359 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf->output.type);
360 bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(cf->output.swizzle_x) |
361 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(cf->output.swizzle_y) |
362 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(cf->output.swizzle_z) |
363 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(cf->output.swizzle_w) |
364 S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf->output.barrier) |
365 S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(cf->output.inst) |
366 S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf->output.end_of_program);
367 break;
368 default:
369 R600_ERR("unsupported CF instruction (0x%X)\n", cf->inst);
370 return -EINVAL;
371 }
372 return 0;
373 }
374
375 int r600_bc_build(struct r600_bc *bc)
376 {
377 struct r600_bc_cf *cf;
378 struct r600_bc_alu *alu;
379 struct r600_bc_vtx *vtx;
380 struct r600_bc_tex *tex;
381 unsigned addr;
382 int r;
383
384
385 /* first path compute addr of each CF block */
386 /* addr start after all the CF instructions */
387 addr = bc->cf_last->id + 2;
388 LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {
389 switch (cf->inst) {
390 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU << 3):
391 break;
392 case V_SQ_CF_WORD1_SQ_CF_INST_TEX:
393 case V_SQ_CF_WORD1_SQ_CF_INST_VTX:
394 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC:
395 /* fetch node need to be 16 bytes aligned*/
396 addr += 3;
397 addr &= 0xFFFFFFFCUL;
398 break;
399 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT:
400 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE:
401 break;
402 default:
403 R600_ERR("unsupported CF instruction (0x%X)\n", cf->inst);
404 return -EINVAL;
405 }
406 cf->addr = addr;
407 addr += cf->ndw;
408 bc->ndw = cf->addr + cf->ndw;
409 }
410 free(bc->bytecode);
411 bc->bytecode = calloc(1, bc->ndw * 4);
412 if (bc->bytecode == NULL)
413 return -ENOMEM;
414 LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {
415 addr = cf->addr;
416 r = r600_bc_cf_build(bc, cf);
417 if (r)
418 return r;
419 switch (cf->inst) {
420 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU << 3):
421 LIST_FOR_EACH_ENTRY(alu, &cf->alu, list) {
422 switch (bc->family) {
423 case CHIP_R600:
424 case CHIP_RV610:
425 case CHIP_RV630:
426 case CHIP_RV670:
427 case CHIP_RV620:
428 case CHIP_RV635:
429 case CHIP_RS780:
430 case CHIP_RS880:
431 r = r600_bc_alu_build(bc, alu, addr);
432 break;
433 case CHIP_RV770:
434 case CHIP_RV730:
435 case CHIP_RV710:
436 case CHIP_RV740:
437 r = r700_bc_alu_build(bc, alu, addr);
438 break;
439 default:
440 R600_ERR("unknown family %d\n", bc->family);
441 return -EINVAL;
442 }
443 if (r)
444 return r;
445 addr += 2;
446 if (alu->last) {
447 addr += alu->nliteral;
448 }
449 }
450 break;
451 case V_SQ_CF_WORD1_SQ_CF_INST_VTX:
452 case V_SQ_CF_WORD1_SQ_CF_INST_VTX_TC:
453 LIST_FOR_EACH_ENTRY(vtx, &cf->vtx, list) {
454 r = r600_bc_vtx_build(bc, vtx, addr);
455 if (r)
456 return r;
457 addr += 4;
458 }
459 break;
460 case V_SQ_CF_WORD1_SQ_CF_INST_TEX:
461 LIST_FOR_EACH_ENTRY(tex, &cf->tex, list) {
462 r = r600_bc_tex_build(bc, tex, addr);
463 if (r)
464 return r;
465 addr += 4;
466 }
467 break;
468 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT:
469 case V_SQ_CF_ALLOC_EXPORT_WORD1_SQ_CF_INST_EXPORT_DONE:
470 break;
471 default:
472 R600_ERR("unsupported CF instruction (0x%X)\n", cf->inst);
473 return -EINVAL;
474 }
475 }
476 return 0;
477 }