nvc0: bind user uniforms for compute on Kepler
[mesa.git] / src / gallium / drivers / nouveau / nvc0 / nve4_compute.c
1 /*
2 * Copyright 2012 Nouveau Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Christoph Bumiller
23 */
24
25 #include "nvc0/nvc0_context.h"
26 #include "nvc0/nve4_compute.h"
27
28 #include "codegen/nv50_ir_driver.h"
29
30 #ifdef DEBUG
31 static void nve4_compute_dump_launch_desc(const struct nve4_cp_launch_desc *);
32 #endif
33
34
35 int
36 nve4_screen_compute_setup(struct nvc0_screen *screen,
37 struct nouveau_pushbuf *push)
38 {
39 struct nouveau_device *dev = screen->base.device;
40 struct nouveau_object *chan = screen->base.channel;
41 int i;
42 int ret;
43 uint32_t obj_class;
44 uint64_t address;
45
46 switch (dev->chipset & ~0xf) {
47 case 0x100:
48 case 0xf0:
49 obj_class = NVF0_COMPUTE_CLASS; /* GK110 */
50 break;
51 case 0xe0:
52 obj_class = NVE4_COMPUTE_CLASS; /* GK104 */
53 break;
54 case 0x110:
55 obj_class = GM107_COMPUTE_CLASS;
56 break;
57 default:
58 NOUVEAU_ERR("unsupported chipset: NV%02x\n", dev->chipset);
59 return -1;
60 }
61
62 ret = nouveau_object_new(chan, 0xbeef00c0, obj_class, NULL, 0,
63 &screen->compute);
64 if (ret) {
65 NOUVEAU_ERR("Failed to allocate compute object: %d\n", ret);
66 return ret;
67 }
68
69 ret = nouveau_bo_new(dev, NV_VRAM_DOMAIN(&screen->base), 0, 1 << 12, NULL,
70 &screen->parm);
71 if (ret)
72 return ret;
73
74 BEGIN_NVC0(push, SUBC_CP(NV01_SUBCHAN_OBJECT), 1);
75 PUSH_DATA (push, screen->compute->oclass);
76
77 BEGIN_NVC0(push, NVE4_CP(TEMP_ADDRESS_HIGH), 2);
78 PUSH_DATAh(push, screen->tls->offset);
79 PUSH_DATA (push, screen->tls->offset);
80 /* No idea why there are 2. Divide size by 2 to be safe.
81 * Actually this might be per-MP TEMP size and looks like I'm only using
82 * 2 MPs instead of all 8.
83 */
84 BEGIN_NVC0(push, NVE4_CP(MP_TEMP_SIZE_HIGH(0)), 3);
85 PUSH_DATAh(push, screen->tls->size / screen->mp_count);
86 PUSH_DATA (push, (screen->tls->size / screen->mp_count) & ~0x7fff);
87 PUSH_DATA (push, 0xff);
88 BEGIN_NVC0(push, NVE4_CP(MP_TEMP_SIZE_HIGH(1)), 3);
89 PUSH_DATAh(push, screen->tls->size / screen->mp_count);
90 PUSH_DATA (push, (screen->tls->size / screen->mp_count) & ~0x7fff);
91 PUSH_DATA (push, 0xff);
92
93 /* Unified address space ? Who needs that ? Certainly not OpenCL.
94 *
95 * FATAL: Buffers with addresses inside [0x1000000, 0x3000000] will NOT be
96 * accessible. We cannot prevent that at the moment, so expect failure.
97 */
98 BEGIN_NVC0(push, NVE4_CP(LOCAL_BASE), 1);
99 PUSH_DATA (push, 1 << 24);
100 BEGIN_NVC0(push, NVE4_CP(SHARED_BASE), 1);
101 PUSH_DATA (push, 2 << 24);
102
103 BEGIN_NVC0(push, NVE4_CP(CODE_ADDRESS_HIGH), 2);
104 PUSH_DATAh(push, screen->text->offset);
105 PUSH_DATA (push, screen->text->offset);
106
107 BEGIN_NVC0(push, SUBC_CP(0x0310), 1);
108 PUSH_DATA (push, (obj_class >= NVF0_COMPUTE_CLASS) ? 0x400 : 0x300);
109
110 /* NOTE: these do not affect the state used by the 3D object */
111 BEGIN_NVC0(push, NVE4_CP(TIC_ADDRESS_HIGH), 3);
112 PUSH_DATAh(push, screen->txc->offset);
113 PUSH_DATA (push, screen->txc->offset);
114 PUSH_DATA (push, NVC0_TIC_MAX_ENTRIES - 1);
115 BEGIN_NVC0(push, NVE4_CP(TSC_ADDRESS_HIGH), 3);
116 PUSH_DATAh(push, screen->txc->offset + 65536);
117 PUSH_DATA (push, screen->txc->offset + 65536);
118 PUSH_DATA (push, NVC0_TSC_MAX_ENTRIES - 1);
119
120 if (obj_class >= NVF0_COMPUTE_CLASS) {
121 /* The blob calls GK110_COMPUTE.FIRMWARE[0x6], along with the args (0x1)
122 * passed with GK110_COMPUTE.GRAPH.SCRATCH[0x2]. This is currently
123 * disabled because our firmware doesn't support these commands and the
124 * GPU hangs if they are used. */
125 BEGIN_NIC0(push, SUBC_CP(0x0248), 64);
126 for (i = 63; i >= 0; i--)
127 PUSH_DATA(push, 0x38000 | i);
128 IMMED_NVC0(push, SUBC_CP(NV50_GRAPH_SERIALIZE), 0);
129 }
130
131 BEGIN_NVC0(push, NVE4_CP(TEX_CB_INDEX), 1);
132 PUSH_DATA (push, 7); /* does not interfere with 3D */
133
134 if (obj_class == NVF0_COMPUTE_CLASS)
135 IMMED_NVC0(push, SUBC_CP(0x02c4), 1);
136
137 address = screen->uniform_bo->offset + NVC0_CB_AUX_INFO(5);
138
139 /* MS sample coordinate offsets: these do not work with _ALT modes ! */
140 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
141 PUSH_DATAh(push, address + NVC0_CB_AUX_MS_INFO);
142 PUSH_DATA (push, address + NVC0_CB_AUX_MS_INFO);
143 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
144 PUSH_DATA (push, 64);
145 PUSH_DATA (push, 1);
146 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 17);
147 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x20 << 1));
148 PUSH_DATA (push, 0); /* 0 */
149 PUSH_DATA (push, 0);
150 PUSH_DATA (push, 1); /* 1 */
151 PUSH_DATA (push, 0);
152 PUSH_DATA (push, 0); /* 2 */
153 PUSH_DATA (push, 1);
154 PUSH_DATA (push, 1); /* 3 */
155 PUSH_DATA (push, 1);
156 PUSH_DATA (push, 2); /* 4 */
157 PUSH_DATA (push, 0);
158 PUSH_DATA (push, 3); /* 5 */
159 PUSH_DATA (push, 0);
160 PUSH_DATA (push, 2); /* 6 */
161 PUSH_DATA (push, 1);
162 PUSH_DATA (push, 3); /* 7 */
163 PUSH_DATA (push, 1);
164
165 #ifdef NOUVEAU_NVE4_MP_TRAP_HANDLER
166 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
167 PUSH_DATAh(push, screen->parm->offset + NVE4_CP_INPUT_TRAP_INFO_PTR);
168 PUSH_DATA (push, screen->parm->offset + NVE4_CP_INPUT_TRAP_INFO_PTR);
169 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
170 PUSH_DATA (push, 28);
171 PUSH_DATA (push, 1);
172 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 8);
173 PUSH_DATA (push, 1);
174 PUSH_DATA (push, screen->parm->offset + NVE4_CP_PARAM_TRAP_INFO);
175 PUSH_DATAh(push, screen->parm->offset + NVE4_CP_PARAM_TRAP_INFO);
176 PUSH_DATA (push, screen->tls->offset);
177 PUSH_DATAh(push, screen->tls->offset);
178 PUSH_DATA (push, screen->tls->size / 2); /* MP TEMP block size */
179 PUSH_DATA (push, screen->tls->size / 2 / 64); /* warp TEMP block size */
180 PUSH_DATA (push, 0); /* warp cfstack size */
181 #endif
182
183 BEGIN_NVC0(push, NVE4_CP(FLUSH), 1);
184 PUSH_DATA (push, NVE4_COMPUTE_FLUSH_CB);
185
186 return 0;
187 }
188
189
190 static void
191 nve4_compute_validate_surfaces(struct nvc0_context *nvc0)
192 {
193 struct nvc0_screen *screen = nvc0->screen;
194 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
195 struct nv50_surface *sf;
196 struct nv04_resource *res;
197 uint32_t mask;
198 unsigned i;
199 const unsigned t = 1;
200 uint64_t address;
201
202 address = screen->uniform_bo->offset + NVC0_CB_AUX_INFO(5);
203
204 mask = nvc0->surfaces_dirty[t];
205 while (mask) {
206 i = ffs(mask) - 1;
207 mask &= ~(1 << i);
208
209 /*
210 * NVE4's surface load/store instructions receive all the information
211 * directly instead of via binding points, so we have to supply them.
212 */
213 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
214 PUSH_DATAh(push, address + NVC0_CB_AUX_BUF_INFO(i));
215 PUSH_DATA (push, address + NVC0_CB_AUX_BUF_INFO(i));
216 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
217 PUSH_DATA (push, 64);
218 PUSH_DATA (push, 1);
219 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 17);
220 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x20 << 1));
221
222 nve4_set_surface_info(push, nvc0->surfaces[t][i], screen);
223
224 sf = nv50_surface(nvc0->surfaces[t][i]);
225 if (sf) {
226 res = nv04_resource(sf->base.texture);
227
228 if (sf->base.writable)
229 BCTX_REFN(nvc0->bufctx_cp, CP_SUF, res, RDWR);
230 else
231 BCTX_REFN(nvc0->bufctx_cp, CP_SUF, res, RD);
232 }
233 }
234 if (nvc0->surfaces_dirty[t]) {
235 BEGIN_NVC0(push, NVE4_CP(FLUSH), 1);
236 PUSH_DATA (push, NVE4_COMPUTE_FLUSH_CB);
237 }
238
239 /* re-reference non-dirty surfaces */
240 mask = nvc0->surfaces_valid[t] & ~nvc0->surfaces_dirty[t];
241 while (mask) {
242 i = ffs(mask) - 1;
243 mask &= ~(1 << i);
244
245 sf = nv50_surface(nvc0->surfaces[t][i]);
246 res = nv04_resource(sf->base.texture);
247
248 if (sf->base.writable)
249 BCTX_REFN(nvc0->bufctx_cp, CP_SUF, res, RDWR);
250 else
251 BCTX_REFN(nvc0->bufctx_cp, CP_SUF, res, RD);
252 }
253
254 nvc0->surfaces_dirty[t] = 0;
255 }
256
257
258 /* Thankfully, textures with samplers follow the normal rules. */
259 static void
260 nve4_compute_validate_samplers(struct nvc0_context *nvc0)
261 {
262 bool need_flush = nve4_validate_tsc(nvc0, 5);
263 if (need_flush) {
264 BEGIN_NVC0(nvc0->base.pushbuf, NVE4_CP(TSC_FLUSH), 1);
265 PUSH_DATA (nvc0->base.pushbuf, 0);
266 }
267 }
268 /* (Code duplicated at bottom for various non-convincing reasons.
269 * E.g. we might want to use the COMPUTE subchannel to upload TIC/TSC
270 * entries to avoid a subchannel switch.
271 * Same for texture cache flushes.
272 * Also, the bufctx differs, and more IFs in the 3D version looks ugly.)
273 */
274 static void nve4_compute_validate_textures(struct nvc0_context *);
275
276 static void
277 nve4_compute_set_tex_handles(struct nvc0_context *nvc0)
278 {
279 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
280 struct nvc0_screen *screen = nvc0->screen;
281 uint64_t address;
282 const unsigned s = nvc0_shader_stage(PIPE_SHADER_COMPUTE);
283 unsigned i, n;
284 uint32_t dirty = nvc0->textures_dirty[s] | nvc0->samplers_dirty[s];
285
286 if (!dirty)
287 return;
288 i = ffs(dirty) - 1;
289 n = util_logbase2(dirty) + 1 - i;
290 assert(n);
291
292 address = screen->uniform_bo->offset + NVC0_CB_AUX_INFO(s);
293
294 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
295 PUSH_DATAh(push, address + NVC0_CB_AUX_TEX_INFO(i));
296 PUSH_DATA (push, address + NVC0_CB_AUX_TEX_INFO(i));
297 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
298 PUSH_DATA (push, n * 4);
299 PUSH_DATA (push, 0x1);
300 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 1 + n);
301 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x20 << 1));
302 PUSH_DATAp(push, &nvc0->tex_handles[s][i], n);
303
304 BEGIN_NVC0(push, NVE4_CP(FLUSH), 1);
305 PUSH_DATA (push, NVE4_COMPUTE_FLUSH_CB);
306
307 nvc0->textures_dirty[s] = 0;
308 nvc0->samplers_dirty[s] = 0;
309 }
310
311 static void
312 nve4_compute_validate_constbufs(struct nvc0_context *nvc0)
313 {
314 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
315 const int s = 5;
316
317 while (nvc0->constbuf_dirty[s]) {
318 int i = ffs(nvc0->constbuf_dirty[s]) - 1;
319 nvc0->constbuf_dirty[s] &= ~(1 << i);
320
321 if (nvc0->constbuf[s][i].user) {
322 struct nouveau_bo *bo = nvc0->screen->uniform_bo;
323 const unsigned base = NVC0_CB_USR_INFO(s);
324 const unsigned size = nvc0->constbuf[s][0].size;
325 assert(i == 0); /* we really only want OpenGL uniforms here */
326 assert(nvc0->constbuf[s][0].u.data);
327
328 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
329 PUSH_DATAh(push, bo->offset + base);
330 PUSH_DATA (push, bo->offset + base);
331 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
332 PUSH_DATA (push, size);
333 PUSH_DATA (push, 0x1);
334 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 1 + (size / 4));
335 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x20 << 1));
336 PUSH_DATAp(push, nvc0->constbuf[s][0].u.data, size / 4);
337 }
338 else {
339 /* TODO: will be updated in the next commit */
340 }
341 }
342
343 BEGIN_NVC0(push, NVE4_CP(FLUSH), 1);
344 PUSH_DATA (push, NVE4_COMPUTE_FLUSH_CB);
345 }
346
347 static void
348 nve4_compute_validate_buffers(struct nvc0_context *nvc0)
349 {
350 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
351 uint64_t address;
352 const int s = 5;
353 int i;
354
355 address = nvc0->screen->uniform_bo->offset + NVC0_CB_AUX_INFO(s);
356
357 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
358 PUSH_DATAh(push, address + NVC0_CB_AUX_BUF_INFO(0));
359 PUSH_DATA (push, address + NVC0_CB_AUX_BUF_INFO(0));
360 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
361 PUSH_DATA (push, 4 * NVC0_MAX_BUFFERS * 4);
362 PUSH_DATA (push, 0x1);
363 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 1 + 4 * NVC0_MAX_BUFFERS);
364 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x20 << 1));
365
366 for (i = 0; i < NVC0_MAX_BUFFERS; i++) {
367 if (nvc0->buffers[s][i].buffer) {
368 struct nv04_resource *res =
369 nv04_resource(nvc0->buffers[s][i].buffer);
370 PUSH_DATA (push, res->address + nvc0->buffers[s][i].buffer_offset);
371 PUSH_DATAh(push, res->address + nvc0->buffers[s][i].buffer_offset);
372 PUSH_DATA (push, nvc0->buffers[s][i].buffer_size);
373 PUSH_DATA (push, 0);
374 BCTX_REFN(nvc0->bufctx_cp, CP_BUF, res, RDWR);
375 } else {
376 PUSH_DATA (push, 0);
377 PUSH_DATA (push, 0);
378 PUSH_DATA (push, 0);
379 PUSH_DATA (push, 0);
380 }
381 }
382 }
383
384 static struct nvc0_state_validate
385 validate_list_cp[] = {
386 { nvc0_compprog_validate, NVC0_NEW_CP_PROGRAM },
387 { nve4_compute_validate_textures, NVC0_NEW_CP_TEXTURES },
388 { nve4_compute_validate_samplers, NVC0_NEW_CP_SAMPLERS },
389 { nve4_compute_set_tex_handles, NVC0_NEW_CP_TEXTURES |
390 NVC0_NEW_CP_SAMPLERS },
391 { nve4_compute_validate_surfaces, NVC0_NEW_CP_SURFACES },
392 { nvc0_compute_validate_globals, NVC0_NEW_CP_GLOBALS },
393 { nve4_compute_validate_buffers, NVC0_NEW_CP_BUFFERS },
394 { nve4_compute_validate_constbufs, NVC0_NEW_CP_CONSTBUF },
395 };
396
397 static bool
398 nve4_state_validate_cp(struct nvc0_context *nvc0, uint32_t mask)
399 {
400 bool ret;
401
402 ret = nvc0_state_validate(nvc0, mask, validate_list_cp,
403 ARRAY_SIZE(validate_list_cp), &nvc0->dirty_cp,
404 nvc0->bufctx_cp);
405
406 if (unlikely(nvc0->state.flushed))
407 nvc0_bufctx_fence(nvc0, nvc0->bufctx_cp, true);
408 return ret;
409 }
410
411 static void
412 nve4_compute_upload_input(struct nvc0_context *nvc0,
413 struct nve4_cp_launch_desc *desc,
414 const void *input,
415 const uint *block_layout,
416 const uint *grid_layout)
417 {
418 struct nvc0_screen *screen = nvc0->screen;
419 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
420 struct nvc0_program *cp = nvc0->compprog;
421 uint64_t address;
422
423 address = screen->uniform_bo->offset + NVC0_CB_AUX_INFO(5);
424
425 if (cp->parm_size) {
426 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
427 PUSH_DATAh(push, screen->parm->offset);
428 PUSH_DATA (push, screen->parm->offset);
429 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
430 PUSH_DATA (push, cp->parm_size);
431 PUSH_DATA (push, 0x1);
432 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 1 + (cp->parm_size / 4));
433 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x20 << 1));
434 PUSH_DATAp(push, input, cp->parm_size / 4);
435
436 /* Bind user parameters coming from clover. */
437 /* TODO: This should be harmonized with uniform_bo. */
438 assert(!(desc->cb_mask & (1 << 0)));
439 nve4_cp_launch_desc_set_cb(desc, 0, screen->parm, 0, 1 << 12);
440 }
441 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
442 PUSH_DATAh(push, address + NVC0_CB_AUX_GRID_INFO);
443 PUSH_DATA (push, address + NVC0_CB_AUX_GRID_INFO);
444 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
445 PUSH_DATA (push, 7 * 4);
446 PUSH_DATA (push, 0x1);
447 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 1 + 7);
448 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x20 << 1));
449 PUSH_DATAp(push, block_layout, 3);
450 PUSH_DATAp(push, grid_layout, 3);
451 PUSH_DATA (push, 0);
452
453 BEGIN_NVC0(push, NVE4_CP(FLUSH), 1);
454 PUSH_DATA (push, NVE4_COMPUTE_FLUSH_CB);
455 }
456
457 static inline uint8_t
458 nve4_compute_derive_cache_split(struct nvc0_context *nvc0, uint32_t shared_size)
459 {
460 if (shared_size > (32 << 10))
461 return NVC0_3D_CACHE_SPLIT_48K_SHARED_16K_L1;
462 if (shared_size > (16 << 10))
463 return NVE4_3D_CACHE_SPLIT_32K_SHARED_32K_L1;
464 return NVC1_3D_CACHE_SPLIT_16K_SHARED_48K_L1;
465 }
466
467 static void
468 nve4_compute_setup_launch_desc(struct nvc0_context *nvc0,
469 struct nve4_cp_launch_desc *desc,
470 uint32_t label,
471 const uint *block_layout,
472 const uint *grid_layout)
473 {
474 const struct nvc0_screen *screen = nvc0->screen;
475 const struct nvc0_program *cp = nvc0->compprog;
476
477 nve4_cp_launch_desc_init_default(desc);
478
479 desc->entry = nvc0_program_symbol_offset(cp, label);
480
481 desc->griddim_x = grid_layout[0];
482 desc->griddim_y = grid_layout[1];
483 desc->griddim_z = grid_layout[2];
484 desc->blockdim_x = block_layout[0];
485 desc->blockdim_y = block_layout[1];
486 desc->blockdim_z = block_layout[2];
487
488 desc->shared_size = align(cp->cp.smem_size, 0x100);
489 desc->local_size_p = align(cp->cp.lmem_size, 0x10);
490 desc->local_size_n = 0;
491 desc->cstack_size = 0x800;
492 desc->cache_split = nve4_compute_derive_cache_split(nvc0, cp->cp.smem_size);
493
494 desc->gpr_alloc = cp->num_gprs;
495 desc->bar_alloc = cp->num_barriers;
496
497 // Only bind OpenGL uniforms and the driver constant buffer through the
498 // launch descriptor because UBOs are sticked to the driver cb to avoid the
499 // limitation of 8 CBs.
500 if (nvc0->constbuf[5][0].user) {
501 nve4_cp_launch_desc_set_cb(desc, 0, screen->uniform_bo,
502 NVC0_CB_USR_INFO(5), 1 << 16);
503 }
504 nve4_cp_launch_desc_set_cb(desc, 7, screen->uniform_bo,
505 NVC0_CB_AUX_INFO(5), 1 << 10);
506 }
507
508 static inline struct nve4_cp_launch_desc *
509 nve4_compute_alloc_launch_desc(struct nouveau_context *nv,
510 struct nouveau_bo **pbo, uint64_t *pgpuaddr)
511 {
512 uint8_t *ptr = nouveau_scratch_get(nv, 512, pgpuaddr, pbo);
513 if (!ptr)
514 return NULL;
515 if (*pgpuaddr & 255) {
516 unsigned adj = 256 - (*pgpuaddr & 255);
517 ptr += adj;
518 *pgpuaddr += adj;
519 }
520 return (struct nve4_cp_launch_desc *)ptr;
521 }
522
523 void
524 nve4_launch_grid(struct pipe_context *pipe, const struct pipe_grid_info *info)
525 {
526 struct nvc0_context *nvc0 = nvc0_context(pipe);
527 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
528 struct nve4_cp_launch_desc *desc;
529 uint64_t desc_gpuaddr;
530 struct nouveau_bo *desc_bo;
531 int ret;
532
533 desc = nve4_compute_alloc_launch_desc(&nvc0->base, &desc_bo, &desc_gpuaddr);
534 if (!desc) {
535 ret = -1;
536 goto out;
537 }
538 BCTX_REFN_bo(nvc0->bufctx_cp, CP_DESC, NOUVEAU_BO_GART | NOUVEAU_BO_RD,
539 desc_bo);
540
541 ret = !nve4_state_validate_cp(nvc0, ~0);
542 if (ret)
543 goto out;
544
545 nve4_compute_setup_launch_desc(nvc0, desc, info->pc,
546 info->block, info->grid);
547
548 nve4_compute_upload_input(nvc0, desc, info->input, info->block, info->grid);
549
550 #ifdef DEBUG
551 if (debug_get_num_option("NV50_PROG_DEBUG", 0))
552 nve4_compute_dump_launch_desc(desc);
553 #endif
554
555 /* upload descriptor and flush */
556 #if 0
557 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
558 PUSH_DATAh(push, desc_gpuaddr);
559 PUSH_DATA (push, desc_gpuaddr);
560 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
561 PUSH_DATA (push, 256);
562 PUSH_DATA (push, 1);
563 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 1 + (256 / 4));
564 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x08 << 1));
565 PUSH_DATAp(push, (const uint32_t *)desc, 256 / 4);
566 BEGIN_NVC0(push, NVE4_CP(FLUSH), 1);
567 PUSH_DATA (push, NVE4_COMPUTE_FLUSH_CB | NVE4_COMPUTE_FLUSH_CODE);
568 #endif
569 BEGIN_NVC0(push, NVE4_CP(LAUNCH_DESC_ADDRESS), 1);
570 PUSH_DATA (push, desc_gpuaddr >> 8);
571 BEGIN_NVC0(push, NVE4_CP(LAUNCH), 1);
572 PUSH_DATA (push, 0x3);
573 BEGIN_NVC0(push, SUBC_CP(NV50_GRAPH_SERIALIZE), 1);
574 PUSH_DATA (push, 0);
575
576 out:
577 if (ret)
578 NOUVEAU_ERR("Failed to launch grid !\n");
579 nouveau_scratch_done(&nvc0->base);
580 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_DESC);
581 }
582
583
584 #define NVE4_TIC_ENTRY_INVALID 0x000fffff
585
586 static void
587 nve4_compute_validate_textures(struct nvc0_context *nvc0)
588 {
589 struct nouveau_bo *txc = nvc0->screen->txc;
590 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
591 const unsigned s = 5;
592 unsigned i;
593 uint32_t commands[2][32];
594 unsigned n[2] = { 0, 0 };
595
596 for (i = 0; i < nvc0->num_textures[s]; ++i) {
597 struct nv50_tic_entry *tic = nv50_tic_entry(nvc0->textures[s][i]);
598 struct nv04_resource *res;
599 const bool dirty = !!(nvc0->textures_dirty[s] & (1 << i));
600
601 if (!tic) {
602 nvc0->tex_handles[s][i] |= NVE4_TIC_ENTRY_INVALID;
603 continue;
604 }
605 res = nv04_resource(tic->pipe.texture);
606
607 if (tic->id < 0) {
608 tic->id = nvc0_screen_tic_alloc(nvc0->screen, tic);
609
610 PUSH_SPACE(push, 16);
611 BEGIN_NVC0(push, NVE4_CP(UPLOAD_DST_ADDRESS_HIGH), 2);
612 PUSH_DATAh(push, txc->offset + (tic->id * 32));
613 PUSH_DATA (push, txc->offset + (tic->id * 32));
614 BEGIN_NVC0(push, NVE4_CP(UPLOAD_LINE_LENGTH_IN), 2);
615 PUSH_DATA (push, 32);
616 PUSH_DATA (push, 1);
617 BEGIN_1IC0(push, NVE4_CP(UPLOAD_EXEC), 9);
618 PUSH_DATA (push, NVE4_COMPUTE_UPLOAD_EXEC_LINEAR | (0x20 << 1));
619 PUSH_DATAp(push, &tic->tic[0], 8);
620
621 commands[0][n[0]++] = (tic->id << 4) | 1;
622 } else
623 if (res->status & NOUVEAU_BUFFER_STATUS_GPU_WRITING) {
624 commands[1][n[1]++] = (tic->id << 4) | 1;
625 }
626 nvc0->screen->tic.lock[tic->id / 32] |= 1 << (tic->id % 32);
627
628 res->status &= ~NOUVEAU_BUFFER_STATUS_GPU_WRITING;
629 res->status |= NOUVEAU_BUFFER_STATUS_GPU_READING;
630
631 nvc0->tex_handles[s][i] &= ~NVE4_TIC_ENTRY_INVALID;
632 nvc0->tex_handles[s][i] |= tic->id;
633 if (dirty)
634 BCTX_REFN(nvc0->bufctx_cp, CP_TEX(i), res, RD);
635 }
636 for (; i < nvc0->state.num_textures[s]; ++i)
637 nvc0->tex_handles[s][i] |= NVE4_TIC_ENTRY_INVALID;
638
639 if (n[0]) {
640 BEGIN_NIC0(push, NVE4_CP(TIC_FLUSH), n[0]);
641 PUSH_DATAp(push, commands[0], n[0]);
642 }
643 if (n[1]) {
644 BEGIN_NIC0(push, NVE4_CP(TEX_CACHE_CTL), n[1]);
645 PUSH_DATAp(push, commands[1], n[1]);
646 }
647
648 nvc0->state.num_textures[s] = nvc0->num_textures[s];
649 }
650
651
652 #ifdef DEBUG
653 static const char *nve4_cache_split_name(unsigned value)
654 {
655 switch (value) {
656 case NVC1_3D_CACHE_SPLIT_16K_SHARED_48K_L1: return "16K_SHARED_48K_L1";
657 case NVE4_3D_CACHE_SPLIT_32K_SHARED_32K_L1: return "32K_SHARED_32K_L1";
658 case NVC0_3D_CACHE_SPLIT_48K_SHARED_16K_L1: return "48K_SHARED_16K_L1";
659 default:
660 return "(invalid)";
661 }
662 }
663
664 static void
665 nve4_compute_dump_launch_desc(const struct nve4_cp_launch_desc *desc)
666 {
667 const uint32_t *data = (const uint32_t *)desc;
668 unsigned i;
669 bool zero = false;
670
671 debug_printf("COMPUTE LAUNCH DESCRIPTOR:\n");
672
673 for (i = 0; i < sizeof(*desc); i += 4) {
674 if (data[i / 4]) {
675 debug_printf("[%x]: 0x%08x\n", i, data[i / 4]);
676 zero = false;
677 } else
678 if (!zero) {
679 debug_printf("...\n");
680 zero = true;
681 }
682 }
683
684 debug_printf("entry = 0x%x\n", desc->entry);
685 debug_printf("grid dimensions = %ux%ux%u\n",
686 desc->griddim_x, desc->griddim_y, desc->griddim_z);
687 debug_printf("block dimensions = %ux%ux%u\n",
688 desc->blockdim_x, desc->blockdim_y, desc->blockdim_z);
689 debug_printf("s[] size: 0x%x\n", desc->shared_size);
690 debug_printf("l[] size: -0x%x / +0x%x\n",
691 desc->local_size_n, desc->local_size_p);
692 debug_printf("stack size: 0x%x\n", desc->cstack_size);
693 debug_printf("barrier count: %u\n", desc->bar_alloc);
694 debug_printf("$r count: %u\n", desc->gpr_alloc);
695 debug_printf("cache split: %s\n", nve4_cache_split_name(desc->cache_split));
696
697 for (i = 0; i < 8; ++i) {
698 uint64_t address;
699 uint32_t size = desc->cb[i].size;
700 bool valid = !!(desc->cb_mask & (1 << i));
701
702 address = ((uint64_t)desc->cb[i].address_h << 32) | desc->cb[i].address_l;
703
704 if (!valid && !address && !size)
705 continue;
706 debug_printf("CB[%u]: address = 0x%"PRIx64", size 0x%x%s\n",
707 i, address, size, valid ? "" : " (invalid)");
708 }
709 }
710 #endif
711
712 #ifdef NOUVEAU_NVE4_MP_TRAP_HANDLER
713 static void
714 nve4_compute_trap_info(struct nvc0_context *nvc0)
715 {
716 struct nvc0_screen *screen = nvc0->screen;
717 struct nouveau_bo *bo = screen->parm;
718 int ret, i;
719 volatile struct nve4_mp_trap_info *info;
720 uint8_t *map;
721
722 ret = nouveau_bo_map(bo, NOUVEAU_BO_RDWR, nvc0->base.client);
723 if (ret)
724 return;
725 map = (uint8_t *)bo->map;
726 info = (volatile struct nve4_mp_trap_info *)(map + NVE4_CP_PARAM_TRAP_INFO);
727
728 if (info->lock) {
729 debug_printf("trapstat = %08x\n", info->trapstat);
730 debug_printf("warperr = %08x\n", info->warperr);
731 debug_printf("PC = %x\n", info->pc);
732 debug_printf("tid = %u %u %u\n",
733 info->tid[0], info->tid[1], info->tid[2]);
734 debug_printf("ctaid = %u %u %u\n",
735 info->ctaid[0], info->ctaid[1], info->ctaid[2]);
736 for (i = 0; i <= 63; ++i)
737 debug_printf("$r%i = %08x\n", i, info->r[i]);
738 for (i = 0; i <= 6; ++i)
739 debug_printf("$p%i = %i\n", i, (info->flags >> i) & 1);
740 debug_printf("$c = %x\n", info->flags >> 12);
741 }
742 info->lock = 0;
743 }
744 #endif