r600g: mipmap early support + EX2/ABS instruction + culling
[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 return 0;
95 }
96
97 int r600_bc_add_output(struct r600_bc *bc, const struct r600_bc_output *output)
98 {
99 int r;
100
101 r = r600_bc_add_cf(bc);
102 if (r)
103 return r;
104 bc->cf_last->inst = output->inst;
105 memcpy(&bc->cf_last->output, output, sizeof(struct r600_bc_output));
106 return 0;
107 }
108
109 int r600_bc_add_alu(struct r600_bc *bc, const struct r600_bc_alu *alu)
110 {
111 struct r600_bc_alu *nalu = r600_bc_alu();
112 struct r600_bc_alu *lalu;
113 int i, r;
114
115 if (nalu == NULL)
116 return -ENOMEM;
117 memcpy(nalu, alu, sizeof(struct r600_bc_alu));
118 nalu->nliteral = 0;
119
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);
123 if (r) {
124 free(nalu);
125 return r;
126 }
127 bc->cf_last->inst = V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU << 3;
128 }
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;
133 }
134 /* compute how many literal are needed
135 * either 2 or 4 literals
136 */
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;
140 }
141 }
142 }
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;
147 }
148 }
149 if (alu->dst.sel >= bc->ngpr) {
150 bc->ngpr = alu->dst.sel + 1;
151 }
152 LIST_ADDTAIL(&nalu->list, &bc->cf_last->alu);
153 /* each alu use 2 dwords */
154 bc->cf_last->ndw += 2;
155 bc->ndw += 2;
156 return 0;
157 }
158
159 int r600_bc_add_literal(struct r600_bc *bc, const u32 *value)
160 {
161 struct r600_bc_alu *alu;
162
163 if (bc->cf_last == NULL) {
164 R600_ERR("no last CF\n");
165 return -EINVAL;
166 }
167 if (bc->cf_last->inst == V_SQ_CF_WORD1_SQ_CF_INST_TEX) {
168 return 0;
169 }
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);
173 return -EINVAL;
174 }
175 alu = LIST_ENTRY(struct r600_bc_alu, bc->cf_last->alu.prev, list);
176 if (!alu->last || !alu->nliteral) {
177 return 0;
178 }
179 memcpy(alu->value, value, 4 * 4);
180 bc->cf_last->ndw += alu->nliteral;
181 bc->ndw += alu->nliteral;
182 return 0;
183 }
184
185 int r600_bc_add_vtx(struct r600_bc *bc, const struct r600_bc_vtx *vtx)
186 {
187 struct r600_bc_vtx *nvtx = r600_bc_vtx();
188 int r;
189
190 if (nvtx == NULL)
191 return -ENOMEM;
192 memcpy(nvtx, vtx, sizeof(struct r600_bc_vtx));
193
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);
199 if (r) {
200 free(nvtx);
201 return r;
202 }
203 bc->cf_last->inst = V_SQ_CF_WORD1_SQ_CF_INST_VTX;
204 }
205 LIST_ADDTAIL(&nvtx->list, &bc->cf_last->vtx);
206 /* each fetch use 4 dwords */
207 bc->cf_last->ndw += 4;
208 bc->ndw += 4;
209 return 0;
210 }
211
212 int r600_bc_add_tex(struct r600_bc *bc, const struct r600_bc_tex *tex)
213 {
214 struct r600_bc_tex *ntex = r600_bc_tex();
215 int r;
216
217 if (ntex == NULL)
218 return -ENOMEM;
219 memcpy(ntex, tex, sizeof(struct r600_bc_tex));
220
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);
225 if (r) {
226 free(ntex);
227 return r;
228 }
229 bc->cf_last->inst = V_SQ_CF_WORD1_SQ_CF_INST_TEX;
230 }
231 LIST_ADDTAIL(&ntex->list, &bc->cf_last->tex);
232 /* each texture fetch use 4 dwords */
233 bc->cf_last->ndw += 4;
234 bc->ndw += 4;
235 return 0;
236 }
237
238 static int r600_bc_vtx_build(struct r600_bc *bc, struct r600_bc_vtx *vtx, unsigned id)
239 {
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;
252 return 0;
253 }
254
255 static int r600_bc_tex_build(struct r600_bc *bc, struct r600_bc_tex *tex, unsigned id)
256 {
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;
281 return 0;
282 }
283
284 int r600_bc_alu_build(struct r600_bc *bc, struct r600_bc_alu *alu, unsigned id)
285 {
286 unsigned i;
287
288 /* don't replace gpr by pv or ps for destination register */
289 if (alu->is_op3) {
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);
303 } else {
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);
319 }
320 if (alu->last) {
321 for (i = 0; i < alu->nliteral; i++) {
322 bc->bytecode[id++] = alu->value[i];
323 }
324 }
325 return 0;
326 }
327
328 int r600_bc_cf_build(struct r600_bc *bc, struct r600_bc_cf *cf)
329 {
330 unsigned id = cf->id;
331
332 switch (cf->inst) {
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);
338 break;
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);
346 break;
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);
360 break;
361 default:
362 R600_ERR("unsupported CF instruction (0x%X)\n", cf->inst);
363 return -EINVAL;
364 }
365 return 0;
366 }
367
368 int r600_bc_build(struct r600_bc *bc)
369 {
370 struct r600_bc_cf *cf;
371 struct r600_bc_alu *alu;
372 struct r600_bc_vtx *vtx;
373 struct r600_bc_tex *tex;
374 unsigned addr;
375 int r;
376
377
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) {
382 switch (cf->inst) {
383 case (V_SQ_CF_ALU_WORD1_SQ_CF_INST_ALU << 3):
384 break;
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*/
389 addr += 3;
390 addr &= 0xFFFFFFFCUL;
391 break;
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:
394 break;
395 default:
396 R600_ERR("unsupported CF instruction (0x%X)\n", cf->inst);
397 return -EINVAL;
398 }
399 cf->addr = addr;
400 addr += cf->ndw;
401 bc->ndw = cf->addr + cf->ndw;
402 }
403 free(bc->bytecode);
404 bc->bytecode = calloc(1, bc->ndw * 4);
405 if (bc->bytecode == NULL)
406 return -ENOMEM;
407 LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {
408 addr = cf->addr;
409 r = r600_bc_cf_build(bc, cf);
410 if (r)
411 return r;
412 switch (cf->inst) {
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) {
416 case CHIP_R600:
417 case CHIP_RV610:
418 case CHIP_RV630:
419 case CHIP_RV670:
420 case CHIP_RV620:
421 case CHIP_RV635:
422 case CHIP_RS780:
423 case CHIP_RS880:
424 r = r600_bc_alu_build(bc, alu, addr);
425 break;
426 case CHIP_RV770:
427 case CHIP_RV730:
428 case CHIP_RV710:
429 case CHIP_RV740:
430 r = r700_bc_alu_build(bc, alu, addr);
431 break;
432 default:
433 R600_ERR("unknown family %d\n", bc->family);
434 return -EINVAL;
435 }
436 if (r)
437 return r;
438 addr += 2;
439 if (alu->last) {
440 addr += alu->nliteral;
441 }
442 }
443 break;
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);
448 if (r)
449 return r;
450 addr += 4;
451 }
452 break;
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);
456 if (r)
457 return r;
458 addr += 4;
459 }
460 break;
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:
463 break;
464 default:
465 R600_ERR("unsupported CF instruction (0x%X)\n", cf->inst);
466 return -EINVAL;
467 }
468 }
469 return 0;
470 }