2 * Copyright 2013 Nouveau Project
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Christoph Bumiller, Samuel Pitoiset
25 #include "nvc0/nvc0_context.h"
26 #include "nvc0/nvc0_compute.h"
29 nvc0_screen_compute_setup(struct nvc0_screen
*screen
,
30 struct nouveau_pushbuf
*push
)
32 struct nouveau_object
*chan
= screen
->base
.channel
;
33 struct nouveau_device
*dev
= screen
->base
.device
;
38 switch (dev
->chipset
& ~0xf) {
41 /* In theory, GF110+ should also support NVC8_COMPUTE_CLASS but,
42 * in practice, a ILLEGAL_CLASS dmesg fail appears when using it. */
43 obj_class
= NVC0_COMPUTE_CLASS
;
46 NOUVEAU_ERR("unsupported chipset: NV%02x\n", dev
->chipset
);
50 ret
= nouveau_object_new(chan
, 0xbeef90c0, obj_class
, NULL
, 0,
53 NOUVEAU_ERR("Failed to allocate compute object: %d\n", ret
);
57 ret
= nouveau_bo_new(dev
, NV_VRAM_DOMAIN(&screen
->base
), 0, 1 << 12, NULL
,
62 BEGIN_NVC0(push
, SUBC_COMPUTE(NV01_SUBCHAN_OBJECT
), 1);
63 PUSH_DATA (push
, screen
->compute
->oclass
);
66 BEGIN_NVC0(push
, NVC0_COMPUTE(MP_LIMIT
), 1);
67 PUSH_DATA (push
, screen
->mp_count
);
68 BEGIN_NVC0(push
, NVC0_COMPUTE(CALL_LIMIT_LOG
), 1);
69 PUSH_DATA (push
, 0xf);
71 BEGIN_NVC0(push
, SUBC_COMPUTE(0x02a0), 1);
72 PUSH_DATA (push
, 0x8000);
74 /* global memory setup */
75 BEGIN_NVC0(push
, SUBC_COMPUTE(0x02c4), 1);
77 BEGIN_NIC0(push
, NVC0_COMPUTE(GLOBAL_BASE
), 0x100);
78 for (i
= 0; i
<= 0xff; i
++)
79 PUSH_DATA (push
, (0xc << 28) | (i
<< 16) | i
);
80 BEGIN_NVC0(push
, SUBC_COMPUTE(0x02c4), 1);
83 /* local memory and cstack setup */
84 BEGIN_NVC0(push
, NVC0_COMPUTE(TEMP_ADDRESS_HIGH
), 2);
85 PUSH_DATAh(push
, screen
->tls
->offset
);
86 PUSH_DATA (push
, screen
->tls
->offset
);
87 BEGIN_NVC0(push
, NVC0_COMPUTE(TEMP_SIZE_HIGH
), 2);
88 PUSH_DATAh(push
, screen
->tls
->size
);
89 PUSH_DATA (push
, screen
->tls
->size
);
90 BEGIN_NVC0(push
, NVC0_COMPUTE(WARP_TEMP_ALLOC
), 1);
92 BEGIN_NVC0(push
, NVC0_COMPUTE(LOCAL_BASE
), 1);
93 PUSH_DATA (push
, 1 << 24);
95 /* shared memory setup */
96 BEGIN_NVC0(push
, NVC0_COMPUTE(CACHE_SPLIT
), 1);
97 PUSH_DATA (push
, NVC0_COMPUTE_CACHE_SPLIT_48K_SHARED_16K_L1
);
98 BEGIN_NVC0(push
, NVC0_COMPUTE(SHARED_BASE
), 1);
99 PUSH_DATA (push
, 2 << 24);
100 BEGIN_NVC0(push
, NVC0_COMPUTE(SHARED_SIZE
), 1);
103 /* code segment setup */
104 BEGIN_NVC0(push
, NVC0_COMPUTE(CODE_ADDRESS_HIGH
), 2);
105 PUSH_DATAh(push
, screen
->text
->offset
);
106 PUSH_DATA (push
, screen
->text
->offset
);
108 /* TODO: textures & samplers */
114 nvc0_compute_validate_program(struct nvc0_context
*nvc0
)
116 struct nvc0_program
*prog
= nvc0
->compprog
;
121 if (!prog
->translated
) {
122 prog
->translated
= nvc0_program_translate(
123 prog
, nvc0
->screen
->base
.device
->chipset
, &nvc0
->base
.debug
);
124 if (!prog
->translated
)
127 if (unlikely(!prog
->code_size
))
130 if (likely(prog
->code_size
)) {
131 if (nvc0_program_upload_code(nvc0
, prog
)) {
132 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
133 BEGIN_NVC0(push
, NVC0_COMPUTE(FLUSH
), 1);
134 PUSH_DATA (push
, NVC0_COMPUTE_FLUSH_CODE
);
142 nvc0_compute_state_validate(struct nvc0_context
*nvc0
)
144 if (!nvc0_compute_validate_program(nvc0
))
147 /* TODO: textures, samplers, surfaces, global memory buffers */
149 nvc0_bufctx_fence(nvc0
, nvc0
->bufctx_cp
, false);
151 nouveau_pushbuf_bufctx(nvc0
->base
.pushbuf
, nvc0
->bufctx_cp
);
152 if (unlikely(nouveau_pushbuf_validate(nvc0
->base
.pushbuf
)))
154 if (unlikely(nvc0
->state
.flushed
))
155 nvc0_bufctx_fence(nvc0
, nvc0
->bufctx_cp
, true);
162 nvc0_compute_upload_input(struct nvc0_context
*nvc0
, const void *input
)
164 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
165 struct nvc0_screen
*screen
= nvc0
->screen
;
166 struct nvc0_program
*cp
= nvc0
->compprog
;
169 BEGIN_NVC0(push
, NVC0_COMPUTE(CB_SIZE
), 3);
170 PUSH_DATA (push
, align(cp
->parm_size
, 0x100));
171 PUSH_DATAh(push
, screen
->parm
->offset
);
172 PUSH_DATA (push
, screen
->parm
->offset
);
173 BEGIN_NVC0(push
, NVC0_COMPUTE(CB_BIND
), 1);
174 PUSH_DATA (push
, (0 << 8) | 1);
175 /* NOTE: size is limited to 4 KiB, which is < NV04_PFIFO_MAX_PACKET_LEN */
176 BEGIN_1IC0(push
, NVC0_COMPUTE(CB_POS
), 1 + cp
->parm_size
/ 4);
178 PUSH_DATAp(push
, input
, cp
->parm_size
/ 4);
180 BEGIN_NVC0(push
, NVC0_COMPUTE(FLUSH
), 1);
181 PUSH_DATA (push
, NVC0_COMPUTE_FLUSH_CB
);
186 nvc0_launch_grid(struct pipe_context
*pipe
,
187 const uint
*block_layout
, const uint
*grid_layout
,
191 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
192 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
193 struct nvc0_program
*cp
= nvc0
->compprog
;
197 ret
= !nvc0_compute_state_validate(nvc0
);
201 nvc0_compute_upload_input(nvc0
, input
);
203 BEGIN_NVC0(push
, NVC0_COMPUTE(CP_START_ID
), 1);
204 PUSH_DATA (push
, nvc0_program_symbol_offset(cp
, label
));
206 BEGIN_NVC0(push
, NVC0_COMPUTE(LOCAL_POS_ALLOC
), 3);
207 PUSH_DATA (push
, align(cp
->cp
.lmem_size
, 0x10));
209 PUSH_DATA (push
, 0x800); /* WARP_CSTACK_SIZE */
211 BEGIN_NVC0(push
, NVC0_COMPUTE(SHARED_SIZE
), 3);
212 PUSH_DATA (push
, align(cp
->cp
.smem_size
, 0x100));
213 PUSH_DATA (push
, block_layout
[0] * block_layout
[1] * block_layout
[2]);
214 PUSH_DATA (push
, cp
->num_barriers
);
215 BEGIN_NVC0(push
, NVC0_COMPUTE(CP_GPR_ALLOC
), 1);
216 PUSH_DATA (push
, cp
->num_gprs
);
218 /* grid/block setup */
219 BEGIN_NVC0(push
, NVC0_COMPUTE(GRIDDIM_YX
), 2);
220 PUSH_DATA (push
, (grid_layout
[1] << 16) | grid_layout
[0]);
221 PUSH_DATA (push
, grid_layout
[2]);
222 BEGIN_NVC0(push
, NVC0_COMPUTE(BLOCKDIM_YX
), 2);
223 PUSH_DATA (push
, (block_layout
[1] << 16) | block_layout
[0]);
224 PUSH_DATA (push
, block_layout
[2]);
226 /* launch preliminary setup */
227 BEGIN_NVC0(push
, NVC0_COMPUTE(GRIDID
), 1);
228 PUSH_DATA (push
, 0x1);
229 BEGIN_NVC0(push
, SUBC_COMPUTE(0x036c), 1);
231 BEGIN_NVC0(push
, NVC0_COMPUTE(FLUSH
), 1);
232 PUSH_DATA (push
, NVC0_COMPUTE_FLUSH_GLOBAL
| NVC0_COMPUTE_FLUSH_UNK8
);
234 /* kernel launching */
235 BEGIN_NVC0(push
, NVC0_COMPUTE(COMPUTE_BEGIN
), 1);
237 BEGIN_NVC0(push
, SUBC_COMPUTE(0x0a08), 1);
239 BEGIN_NVC0(push
, NVC0_COMPUTE(LAUNCH
), 1);
240 PUSH_DATA (push
, 0x1000);
241 BEGIN_NVC0(push
, NVC0_COMPUTE(COMPUTE_END
), 1);
243 BEGIN_NVC0(push
, SUBC_COMPUTE(0x0360), 1);
244 PUSH_DATA (push
, 0x1);
246 /* rebind all the 3D constant buffers
247 * (looks like binding a CB on COMPUTE clobbers 3D state) */
248 nvc0
->dirty
|= NVC0_NEW_CONSTBUF
;
249 for (s
= 0; s
< 6; s
++) {
250 for (i
= 0; i
< NVC0_MAX_PIPE_CONSTBUFS
; i
++)
251 if (nvc0
->constbuf
[s
][i
].u
.buf
)
252 nvc0
->constbuf_dirty
[s
] |= 1 << i
;
254 memset(nvc0
->state
.uniform_buffer_bound
, 0,
255 sizeof(nvc0
->state
.uniform_buffer_bound
));
259 NOUVEAU_ERR("Failed to launch grid !\n");