nouveau: switch to libdrm_nouveau-2.0
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_screen.c
1 /*
2 * Copyright 2010 Christoph Bumiller
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "util/u_format.h"
24 #include "util/u_format_s3tc.h"
25 #include "pipe/p_screen.h"
26
27 #include "vl/vl_decoder.h"
28 #include "vl/vl_video_buffer.h"
29
30 #include "nvc0_context.h"
31 #include "nvc0_screen.h"
32
33 #include "nouveau/nv_object.xml.h"
34 #include "nvc0_graph_macros.h"
35
36 static boolean
37 nvc0_screen_is_format_supported(struct pipe_screen *pscreen,
38 enum pipe_format format,
39 enum pipe_texture_target target,
40 unsigned sample_count,
41 unsigned bindings)
42 {
43 if (!(0x117 & (1 << sample_count))) /* 0, 1, 2, 4 or 8 */
44 return FALSE;
45
46 if (!util_format_is_supported(format, bindings))
47 return FALSE;
48
49 switch (format) {
50 case PIPE_FORMAT_R8G8B8A8_UNORM:
51 case PIPE_FORMAT_R8G8B8X8_UNORM:
52 /* HACK: GL requires equal formats for MS resolve and window is BGRA */
53 if (bindings & PIPE_BIND_RENDER_TARGET)
54 return FALSE;
55 default:
56 break;
57 }
58
59 /* transfers & shared are always supported */
60 bindings &= ~(PIPE_BIND_TRANSFER_READ |
61 PIPE_BIND_TRANSFER_WRITE |
62 PIPE_BIND_SHARED);
63
64 return (nvc0_format_table[format].usage & bindings) == bindings;
65 }
66
67 static int
68 nvc0_screen_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
69 {
70 switch (param) {
71 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
72 return 16 * PIPE_SHADER_TYPES; /* NOTE: should not count COMPUTE */
73 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
74 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
75 return 15;
76 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
77 return 12;
78 case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
79 return 2048;
80 case PIPE_CAP_MIN_TEXEL_OFFSET:
81 return -8;
82 case PIPE_CAP_MAX_TEXEL_OFFSET:
83 return 7;
84 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
85 case PIPE_CAP_TEXTURE_SWIZZLE:
86 case PIPE_CAP_TEXTURE_SHADOW_MAP:
87 case PIPE_CAP_NPOT_TEXTURES:
88 case PIPE_CAP_ANISOTROPIC_FILTER:
89 case PIPE_CAP_SEAMLESS_CUBE_MAP:
90 return 1;
91 case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
92 return 0;
93 case PIPE_CAP_TWO_SIDED_STENCIL:
94 case PIPE_CAP_DEPTH_CLIP_DISABLE:
95 case PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE:
96 case PIPE_CAP_POINT_SPRITE:
97 return 1;
98 case PIPE_CAP_SM3:
99 return 1;
100 case PIPE_CAP_GLSL_FEATURE_LEVEL:
101 return 150;
102 case PIPE_CAP_MAX_RENDER_TARGETS:
103 return 8;
104 case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
105 case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
106 case PIPE_CAP_VERTEX_COLOR_CLAMPED:
107 return 1;
108 case PIPE_CAP_TIMER_QUERY:
109 case PIPE_CAP_OCCLUSION_QUERY:
110 case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
111 return 1;
112 case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
113 return 4;
114 case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
115 case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
116 return 128;
117 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
118 case PIPE_CAP_INDEP_BLEND_ENABLE:
119 case PIPE_CAP_INDEP_BLEND_FUNC:
120 return 1;
121 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
122 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
123 return 1;
124 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
125 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
126 return 0;
127 case PIPE_CAP_SHADER_STENCIL_EXPORT:
128 return 0;
129 case PIPE_CAP_PRIMITIVE_RESTART:
130 case PIPE_CAP_TGSI_INSTANCEID:
131 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
132 case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
133 case PIPE_CAP_CONDITIONAL_RENDER:
134 case PIPE_CAP_TEXTURE_BARRIER:
135 case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
136 return 1;
137 case PIPE_CAP_TGSI_CAN_COMPACT_VARYINGS:
138 case PIPE_CAP_TGSI_CAN_COMPACT_CONSTANTS:
139 return 0; /* state trackers will know better */
140 default:
141 NOUVEAU_ERR("unknown PIPE_CAP %d\n", param);
142 return 0;
143 }
144 }
145
146 static int
147 nvc0_screen_get_shader_param(struct pipe_screen *pscreen, unsigned shader,
148 enum pipe_shader_cap param)
149 {
150 switch (shader) {
151 case PIPE_SHADER_VERTEX:
152 /*
153 case PIPE_SHADER_TESSELLATION_CONTROL:
154 case PIPE_SHADER_TESSELLATION_EVALUATION:
155 */
156 case PIPE_SHADER_GEOMETRY:
157 case PIPE_SHADER_FRAGMENT:
158 break;
159 default:
160 return 0;
161 }
162
163 switch (param) {
164 case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
165 case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
166 case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
167 case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
168 return 16384;
169 case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
170 return 16;
171 case PIPE_SHADER_CAP_MAX_INPUTS:
172 if (shader == PIPE_SHADER_VERTEX)
173 return 32;
174 if (shader == PIPE_SHADER_FRAGMENT)
175 return (0x200 + 0x20 + 0x80) / 16; /* generic + colors + TexCoords */
176 return (0x200 + 0x40 + 0x80) / 16; /* without 0x60 for per-patch inputs */
177 case PIPE_SHADER_CAP_MAX_CONSTS:
178 return 65536 / 16;
179 case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
180 return 14;
181 case PIPE_SHADER_CAP_MAX_ADDRS:
182 return 1;
183 case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
184 case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
185 return shader != PIPE_SHADER_FRAGMENT;
186 case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
187 case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
188 return 1;
189 case PIPE_SHADER_CAP_MAX_PREDS:
190 return 0;
191 case PIPE_SHADER_CAP_MAX_TEMPS:
192 return NVC0_CAP_MAX_PROGRAM_TEMPS;
193 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
194 return 1;
195 case PIPE_SHADER_CAP_SUBROUTINES:
196 return 1; /* but inlining everything, we need function declarations */
197 case PIPE_SHADER_CAP_INTEGERS:
198 return 1;
199 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
200 return 16; /* would be 32 in linked (OpenGL-style) mode */
201 /*
202 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLER_VIEWS:
203 return 32;
204 */
205 default:
206 NOUVEAU_ERR("unknown PIPE_SHADER_CAP %d\n", param);
207 return 0;
208 }
209 }
210
211 static float
212 nvc0_screen_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
213 {
214 switch (param) {
215 case PIPE_CAPF_MAX_LINE_WIDTH:
216 case PIPE_CAPF_MAX_LINE_WIDTH_AA:
217 return 10.0f;
218 case PIPE_CAPF_MAX_POINT_WIDTH:
219 return 63.0f;
220 case PIPE_CAPF_MAX_POINT_WIDTH_AA:
221 return 63.375f;
222 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
223 return 16.0f;
224 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
225 return 15.0f;
226 default:
227 NOUVEAU_ERR("unknown PIPE_CAP %d\n", param);
228 return 0.0f;
229 }
230 }
231
232 static void
233 nvc0_screen_destroy(struct pipe_screen *pscreen)
234 {
235 struct nvc0_screen *screen = nvc0_screen(pscreen);
236
237 if (screen->base.fence.current) {
238 nouveau_fence_wait(screen->base.fence.current);
239 nouveau_fence_ref(NULL, &screen->base.fence.current);
240 }
241 if (screen->base.pushbuf)
242 screen->base.pushbuf->user_priv = NULL;
243
244 if (screen->blitctx)
245 FREE(screen->blitctx);
246
247 nouveau_bo_ref(NULL, &screen->text);
248 nouveau_bo_ref(NULL, &screen->tls);
249 nouveau_bo_ref(NULL, &screen->txc);
250 nouveau_bo_ref(NULL, &screen->fence.bo);
251 nouveau_bo_ref(NULL, &screen->vfetch_cache);
252
253 nouveau_heap_destroy(&screen->lib_code);
254 nouveau_heap_destroy(&screen->text_heap);
255
256 if (screen->tic.entries)
257 FREE(screen->tic.entries);
258
259 nouveau_mm_destroy(screen->mm_VRAM_fe0);
260
261 nouveau_object_del(&screen->fermi);
262 nouveau_object_del(&screen->eng2d);
263 nouveau_object_del(&screen->m2mf);
264
265 nouveau_screen_fini(&screen->base);
266
267 FREE(screen);
268 }
269
270 static int
271 nvc0_graph_set_macro(struct nvc0_screen *screen, uint32_t m, unsigned pos,
272 unsigned size, const uint32_t *data)
273 {
274 struct nouveau_pushbuf *push = screen->base.pushbuf;
275
276 size /= 4;
277
278 BEGIN_NVC0(push, SUBC_3D(NVC0_GRAPH_MACRO_ID), 2);
279 PUSH_DATA (push, (m - 0x3800) / 8);
280 PUSH_DATA (push, pos);
281 BEGIN_1IC0(push, SUBC_3D(NVC0_GRAPH_MACRO_UPLOAD_POS), size + 1);
282 PUSH_DATA (push, pos);
283 PUSH_DATAp(push, data, size);
284
285 return pos + size;
286 }
287
288 static void
289 nvc0_magic_3d_init(struct nouveau_pushbuf *push)
290 {
291 BEGIN_NVC0(push, SUBC_3D(0x10cc), 1);
292 PUSH_DATA (push, 0xff);
293 BEGIN_NVC0(push, SUBC_3D(0x10e0), 2);
294 PUSH_DATA(push, 0xff);
295 PUSH_DATA(push, 0xff);
296 BEGIN_NVC0(push, SUBC_3D(0x10ec), 2);
297 PUSH_DATA(push, 0xff);
298 PUSH_DATA(push, 0xff);
299 BEGIN_NVC0(push, SUBC_3D(0x074c), 1);
300 PUSH_DATA (push, 0x3f);
301
302 BEGIN_NVC0(push, SUBC_3D(0x16a8), 1);
303 PUSH_DATA (push, (3 << 16) | 3);
304 BEGIN_NVC0(push, SUBC_3D(0x1794), 1);
305 PUSH_DATA (push, (2 << 16) | 2);
306 BEGIN_NVC0(push, SUBC_3D(0x0de8), 1);
307 PUSH_DATA (push, 1);
308
309 #if 0 /* software method */
310 BEGIN_NVC0(push, SUBC_3D(0x1528), 1); /* MP poke */
311 PUSH_DATA (push, 0);
312 #endif
313
314 BEGIN_NVC0(push, SUBC_3D(0x12ac), 1);
315 PUSH_DATA (push, 0);
316 BEGIN_NVC0(push, SUBC_3D(0x0218), 1);
317 PUSH_DATA (push, 0x10);
318 BEGIN_NVC0(push, SUBC_3D(0x10fc), 1);
319 PUSH_DATA (push, 0x10);
320 BEGIN_NVC0(push, SUBC_3D(0x1290), 1);
321 PUSH_DATA (push, 0x10);
322 BEGIN_NVC0(push, SUBC_3D(0x12d8), 2);
323 PUSH_DATA (push, 0x10);
324 PUSH_DATA (push, 0x10);
325 BEGIN_NVC0(push, SUBC_3D(0x06d4), 1);
326 PUSH_DATA (push, 8);
327 BEGIN_NVC0(push, SUBC_3D(0x1140), 1);
328 PUSH_DATA (push, 0x10);
329 BEGIN_NVC0(push, SUBC_3D(0x1610), 1);
330 PUSH_DATA (push, 0xe);
331
332 BEGIN_NVC0(push, SUBC_3D(0x164c), 1);
333 PUSH_DATA (push, 1 << 12);
334 BEGIN_NVC0(push, SUBC_3D(0x151c), 1);
335 PUSH_DATA (push, 1);
336 BEGIN_NVC0(push, SUBC_3D(0x030c), 1);
337 PUSH_DATA (push, 0);
338 BEGIN_NVC0(push, SUBC_3D(0x0300), 1);
339 PUSH_DATA (push, 3);
340 #if 0 /* software method */
341 BEGIN_NVC0(push, SUBC_3D(0x1280), 1); /* PGRAPH poke */
342 PUSH_DATA (push, 0);
343 #endif
344 BEGIN_NVC0(push, SUBC_3D(0x02d0), 1);
345 PUSH_DATA (push, 0x1f40);
346 BEGIN_NVC0(push, SUBC_3D(0x0fdc), 1);
347 PUSH_DATA (push, 1);
348 BEGIN_NVC0(push, SUBC_3D(0x19c0), 1);
349 PUSH_DATA (push, 1);
350 BEGIN_NVC0(push, SUBC_3D(0x075c), 1);
351 PUSH_DATA (push, 3);
352 }
353
354 static void
355 nvc0_screen_fence_emit(struct pipe_screen *pscreen, u32 *sequence)
356 {
357 struct nvc0_screen *screen = nvc0_screen(pscreen);
358 struct nouveau_pushbuf *push = screen->base.pushbuf;
359
360 /* we need to do it after possible flush in MARK_RING */
361 *sequence = ++screen->base.fence.sequence;
362
363 BEGIN_NVC0(push, NVC0_3D(QUERY_ADDRESS_HIGH), 4);
364 PUSH_DATAh(push, screen->fence.bo->offset);
365 PUSH_DATA (push, screen->fence.bo->offset);
366 PUSH_DATA (push, *sequence);
367 PUSH_DATA (push, NVC0_3D_QUERY_GET_FENCE | NVC0_3D_QUERY_GET_SHORT |
368 (0xf << NVC0_3D_QUERY_GET_UNIT__SHIFT));
369 }
370
371 static u32
372 nvc0_screen_fence_update(struct pipe_screen *pscreen)
373 {
374 struct nvc0_screen *screen = nvc0_screen(pscreen);
375 return screen->fence.map[0];
376 }
377
378 #define FAIL_SCREEN_INIT(str, err) \
379 do { \
380 NOUVEAU_ERR(str, err); \
381 nvc0_screen_destroy(pscreen); \
382 return NULL; \
383 } while(0)
384
385 struct pipe_screen *
386 nvc0_screen_create(struct nouveau_device *dev)
387 {
388 struct nvc0_screen *screen;
389 struct pipe_screen *pscreen;
390 struct nouveau_object *chan;
391 struct nouveau_pushbuf *push;
392 int ret;
393 unsigned i;
394 union nouveau_bo_config mm_config;
395
396 screen = CALLOC_STRUCT(nvc0_screen);
397 if (!screen)
398 return NULL;
399 pscreen = &screen->base.base;
400
401 screen->base.sysmem_bindings = PIPE_BIND_CONSTANT_BUFFER;
402
403 ret = nouveau_screen_init(&screen->base, dev);
404 if (ret) {
405 nvc0_screen_destroy(pscreen);
406 return NULL;
407 }
408 chan = screen->base.channel;
409 push = screen->base.pushbuf;
410 push->user_priv = screen;
411
412 pscreen->destroy = nvc0_screen_destroy;
413 pscreen->context_create = nvc0_create;
414 pscreen->is_format_supported = nvc0_screen_is_format_supported;
415 pscreen->get_param = nvc0_screen_get_param;
416 pscreen->get_shader_param = nvc0_screen_get_shader_param;
417 pscreen->get_paramf = nvc0_screen_get_paramf;
418
419 nvc0_screen_init_resource_functions(pscreen);
420
421 nouveau_screen_init_vdec(&screen->base);
422
423 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0, 4096, NULL,
424 &screen->fence.bo);
425 if (ret)
426 goto fail;
427 nouveau_bo_map(screen->fence.bo, 0, NULL);
428 screen->fence.map = screen->fence.bo->map;
429 screen->base.fence.emit = nvc0_screen_fence_emit;
430 screen->base.fence.update = nvc0_screen_fence_update;
431
432 for (i = 0; i < NVC0_SCRATCH_NR_BUFFERS; ++i) {
433 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART, 0, NVC0_SCRATCH_SIZE, NULL,
434 &screen->scratch.bo[i]);
435 if (ret)
436 goto fail;
437 }
438
439 ret = nouveau_object_new(chan, 0xbeef9039, NVC0_M2MF_CLASS, NULL, 0,
440 &screen->m2mf);
441 if (ret)
442 FAIL_SCREEN_INIT("Error allocating PGRAPH context for M2MF: %d\n", ret);
443
444 BEGIN_NVC0(push, SUBC_M2MF(NV01_SUBCHAN_OBJECT), 1);
445 PUSH_DATA (push, screen->m2mf->oclass);
446 BEGIN_NVC0(push, NVC0_M2MF(NOTIFY_ADDRESS_HIGH), 3);
447 PUSH_DATAh(push, screen->fence.bo->offset + 16);
448 PUSH_DATA (push, screen->fence.bo->offset + 16);
449 PUSH_DATA (push, 0);
450
451 ret = nouveau_object_new(chan, 0xbeef902d, NVC0_2D_CLASS, NULL, 0,
452 &screen->eng2d);
453 if (ret)
454 FAIL_SCREEN_INIT("Error allocating PGRAPH context for 2D: %d\n", ret);
455
456 BEGIN_NVC0(push, SUBC_2D(NV01_SUBCHAN_OBJECT), 1);
457 PUSH_DATA (push, screen->eng2d->oclass);
458 BEGIN_NVC0(push, NVC0_2D(OPERATION), 1);
459 PUSH_DATA (push, NVC0_2D_OPERATION_SRCCOPY);
460 BEGIN_NVC0(push, NVC0_2D(CLIP_ENABLE), 1);
461 PUSH_DATA (push, 0);
462 BEGIN_NVC0(push, NVC0_2D(COLOR_KEY_ENABLE), 1);
463 PUSH_DATA (push, 0);
464 BEGIN_NVC0(push, SUBC_2D(0x0884), 1);
465 PUSH_DATA (push, 0x3f);
466 BEGIN_NVC0(push, SUBC_2D(0x0888), 1);
467 PUSH_DATA (push, 1);
468
469 ret = nouveau_object_new(chan, 0xbeef9097, NVC0_3D_CLASS, NULL, 0,
470 &screen->fermi);
471 if (ret)
472 FAIL_SCREEN_INIT("Error allocating PGRAPH context for 3D: %d\n", ret);
473
474 BEGIN_NVC0(push, SUBC_3D(NV01_SUBCHAN_OBJECT), 1);
475 PUSH_DATA (push, screen->fermi->oclass);
476 BEGIN_NVC0(push, NVC0_3D(NOTIFY_ADDRESS_HIGH), 3);
477 PUSH_DATAh(push, screen->fence.bo->offset + 32);
478 PUSH_DATA (push, screen->fence.bo->offset + 32);
479 PUSH_DATA (push, 0);
480
481 BEGIN_NVC0(push, NVC0_3D(COND_MODE), 1);
482 PUSH_DATA (push, NVC0_3D_COND_MODE_ALWAYS);
483
484 if (debug_get_bool_option("NOUVEAU_SHADER_WATCHDOG", TRUE)) {
485 /* kill shaders after about 1 second (at 100 MHz) */
486 BEGIN_NVC0(push, NVC0_3D(WATCHDOG_TIMER), 1);
487 PUSH_DATA (push, 0x17);
488 }
489
490 BEGIN_NVC0(push, NVC0_3D(RT_CONTROL), 1);
491 PUSH_DATA (push, 1);
492
493 BEGIN_NVC0(push, NVC0_3D(CSAA_ENABLE), 1);
494 PUSH_DATA (push, 0);
495 BEGIN_NVC0(push, NVC0_3D(MULTISAMPLE_ENABLE), 1);
496 PUSH_DATA (push, 0);
497 BEGIN_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), 1);
498 PUSH_DATA (push, NVC0_3D_MULTISAMPLE_MODE_MS1);
499 BEGIN_NVC0(push, NVC0_3D(MULTISAMPLE_CTRL), 1);
500 PUSH_DATA (push, 0);
501 BEGIN_NVC0(push, NVC0_3D(LINE_WIDTH_SEPARATE), 1);
502 PUSH_DATA (push, 1);
503 BEGIN_NVC0(push, NVC0_3D(LINE_LAST_PIXEL), 1);
504 PUSH_DATA (push, 0);
505 BEGIN_NVC0(push, NVC0_3D(BLEND_SEPARATE_ALPHA), 1);
506 PUSH_DATA (push, 1);
507 BEGIN_NVC0(push, NVC0_3D(BLEND_ENABLE_COMMON), 1);
508 PUSH_DATA (push, 0);
509 BEGIN_NVC0(push, NVC0_3D(TEX_MISC), 1);
510 PUSH_DATA (push, NVC0_3D_TEX_MISC_SEAMLESS_CUBE_MAP);
511
512 nvc0_magic_3d_init(push);
513
514 ret = nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 1 << 17, 1 << 20, NULL,
515 &screen->text);
516 if (ret)
517 goto fail;
518
519 /* XXX: getting a page fault at the end of the code buffer every few
520 * launches, don't use the last 256 bytes to work around them - prefetch ?
521 */
522 nouveau_heap_init(&screen->text_heap, 0, (1 << 20) - 0x100);
523
524 ret = nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 1 << 12, 6 << 16, NULL,
525 &screen->uniforms);
526 if (ret)
527 goto fail;
528
529 /* auxiliary constants (6 user clip planes, base instance id) */
530 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
531 PUSH_DATA (push, 256);
532 PUSH_DATAh(push, screen->uniforms->offset + (5 << 16));
533 PUSH_DATA (push, screen->uniforms->offset + (5 << 16));
534 for (i = 0; i < 5; ++i) {
535 BEGIN_NVC0(push, NVC0_3D(CB_BIND(i)), 1);
536 PUSH_DATA (push, (15 << 4) | 1);
537 }
538
539 screen->tls_size = (16 * 32) * (NVC0_CAP_MAX_PROGRAM_TEMPS * 16);
540 ret = nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 1 << 17,
541 screen->tls_size, NULL, &screen->tls);
542 if (ret)
543 goto fail;
544
545 BEGIN_NVC0(push, NVC0_3D(CODE_ADDRESS_HIGH), 2);
546 PUSH_DATAh(push, screen->text->offset);
547 PUSH_DATA (push, screen->text->offset);
548 BEGIN_NVC0(push, NVC0_3D(TEMP_ADDRESS_HIGH), 4);
549 PUSH_DATAh(push, screen->tls->offset);
550 PUSH_DATA (push, screen->tls->offset);
551 PUSH_DATA (push, screen->tls_size >> 32);
552 PUSH_DATA (push, screen->tls_size);
553 BEGIN_NVC0(push, NVC0_3D(WARP_TEMP_ALLOC), 1);
554 PUSH_DATA (push, 0);
555 BEGIN_NVC0(push, NVC0_3D(LOCAL_BASE), 1);
556 PUSH_DATA (push, 0);
557
558 for (i = 0; i < 5; ++i) {
559 BEGIN_NVC0(push, NVC0_3D(TEX_LIMITS(i)), 1);
560 PUSH_DATA (push, 0x54);
561 }
562 BEGIN_NVC0(push, NVC0_3D(LINKED_TSC), 1);
563 PUSH_DATA (push, 0);
564
565 ret = nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 1 << 17, 1 << 20, NULL,
566 &screen->vfetch_cache);
567 if (ret)
568 goto fail;
569
570 BEGIN_NVC0(push, NVC0_3D(VERTEX_QUARANTINE_ADDRESS_HIGH), 3);
571 PUSH_DATAh(push, screen->vfetch_cache->offset);
572 PUSH_DATA (push, screen->vfetch_cache->offset);
573 PUSH_DATA (push, 3);
574
575 ret = nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 1 << 17, 1 << 17, NULL,
576 &screen->txc);
577 if (ret)
578 goto fail;
579
580 BEGIN_NVC0(push, NVC0_3D(TIC_ADDRESS_HIGH), 3);
581 PUSH_DATAh(push, screen->txc->offset);
582 PUSH_DATA (push, screen->txc->offset);
583 PUSH_DATA (push, NVC0_TIC_MAX_ENTRIES - 1);
584
585 BEGIN_NVC0(push, NVC0_3D(TSC_ADDRESS_HIGH), 3);
586 PUSH_DATAh(push, screen->txc->offset + 65536);
587 PUSH_DATA (push, screen->txc->offset + 65536);
588 PUSH_DATA (push, NVC0_TSC_MAX_ENTRIES - 1);
589
590 BEGIN_NVC0(push, NVC0_3D(SCREEN_Y_CONTROL), 1);
591 PUSH_DATA (push, 0);
592 BEGIN_NVC0(push, NVC0_3D(WINDOW_OFFSET_X), 2);
593 PUSH_DATA (push, 0);
594 PUSH_DATA (push, 0);
595 BEGIN_NVC0(push, NVC0_3D(ZCULL_REGION), 1); /* deactivate ZCULL */
596 PUSH_DATA (push, 0x3f);
597
598 BEGIN_NVC0(push, NVC0_3D(CLIP_RECTS_MODE), 1);
599 PUSH_DATA (push, NVC0_3D_CLIP_RECTS_MODE_INSIDE_ANY);
600 BEGIN_NVC0(push, NVC0_3D(CLIP_RECT_HORIZ(0)), 8 * 2);
601 for (i = 0; i < 8 * 2; ++i)
602 PUSH_DATA(push, 0);
603 BEGIN_NVC0(push, NVC0_3D(CLIP_RECTS_EN), 1);
604 PUSH_DATA (push, 0);
605 BEGIN_NVC0(push, NVC0_3D(CLIPID_ENABLE), 1);
606 PUSH_DATA (push, 0);
607
608 /* neither scissors, viewport nor stencil mask should affect clears */
609 BEGIN_NVC0(push, NVC0_3D(CLEAR_FLAGS), 1);
610 PUSH_DATA (push, 0);
611
612 BEGIN_NVC0(push, NVC0_3D(VIEWPORT_TRANSFORM_EN), 1);
613 PUSH_DATA (push, 1);
614 BEGIN_NVC0(push, NVC0_3D(DEPTH_RANGE_NEAR(0)), 2);
615 PUSH_DATAf(push, 0.0f);
616 PUSH_DATAf(push, 1.0f);
617 BEGIN_NVC0(push, NVC0_3D(VIEW_VOLUME_CLIP_CTRL), 1);
618 PUSH_DATA (push, NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK1_UNK1);
619
620 /* We use scissors instead of exact view volume clipping,
621 * so they're always enabled.
622 */
623 BEGIN_NVC0(push, NVC0_3D(SCISSOR_ENABLE(0)), 3);
624 PUSH_DATA (push, 1);
625 PUSH_DATA (push, 8192 << 16);
626 PUSH_DATA (push, 8192 << 16);
627
628 #define MK_MACRO(m, n) i = nvc0_graph_set_macro(screen, m, i, sizeof(n), n);
629
630 i = 0;
631 MK_MACRO(NVC0_3D_BLEND_ENABLES, nvc0_9097_blend_enables);
632 MK_MACRO(NVC0_3D_VERTEX_ARRAY_SELECT, nvc0_9097_vertex_array_select);
633 MK_MACRO(NVC0_3D_TEP_SELECT, nvc0_9097_tep_select);
634 MK_MACRO(NVC0_3D_GP_SELECT, nvc0_9097_gp_select);
635 MK_MACRO(NVC0_3D_POLYGON_MODE_FRONT, nvc0_9097_poly_mode_front);
636 MK_MACRO(NVC0_3D_POLYGON_MODE_BACK, nvc0_9097_poly_mode_back);
637
638 BEGIN_NVC0(push, NVC0_3D(RASTERIZE_ENABLE), 1);
639 PUSH_DATA (push, 1);
640 BEGIN_NVC0(push, NVC0_3D(RT_SEPARATE_FRAG_DATA), 1);
641 PUSH_DATA (push, 1);
642 BEGIN_NVC0(push, NVC0_3D(GP_SELECT), 1);
643 PUSH_DATA (push, 0x40);
644 BEGIN_NVC0(push, NVC0_3D(LAYER), 1);
645 PUSH_DATA (push, 0);
646 BEGIN_NVC0(push, NVC0_3D(TEP_SELECT), 1);
647 PUSH_DATA (push, 0x30);
648 BEGIN_NVC0(push, NVC0_3D(PATCH_VERTICES), 1);
649 PUSH_DATA (push, 3);
650 BEGIN_NVC0(push, NVC0_3D(SP_SELECT(2)), 1);
651 PUSH_DATA (push, 0x20);
652 BEGIN_NVC0(push, NVC0_3D(SP_SELECT(0)), 1);
653 PUSH_DATA (push, 0x00);
654
655 BEGIN_NVC0(push, NVC0_3D(POINT_COORD_REPLACE), 1);
656 PUSH_DATA (push, 0);
657 BEGIN_NVC0(push, NVC0_3D(POINT_RASTER_RULES), 1);
658 PUSH_DATA (push, NVC0_3D_POINT_RASTER_RULES_OGL);
659
660 IMMED_NVC0(push, NVC0_3D(EDGEFLAG), 1);
661
662 BEGIN_NVC0(push, NVC0_3D(VERTEX_RUNOUT_ADDRESS_HIGH), 2);
663 PUSH_DATA (push, 0xab);
664 PUSH_DATA (push, 0x00000000);
665
666 PUSH_KICK (push);
667
668 screen->tic.entries = CALLOC(4096, sizeof(void *));
669 screen->tsc.entries = screen->tic.entries + 2048;
670
671 mm_config.nvc0.tile_mode = 0;
672 mm_config.nvc0.memtype = 0xfe0;
673 screen->mm_VRAM_fe0 = nouveau_mm_create(dev, NOUVEAU_BO_VRAM, &mm_config);
674
675 if (!nvc0_blitctx_create(screen))
676 goto fail;
677
678 nouveau_fence_new(&screen->base, &screen->base.fence.current, FALSE);
679
680 return pscreen;
681
682 fail:
683 nvc0_screen_destroy(pscreen);
684 return NULL;
685 }
686
687 int
688 nvc0_screen_tic_alloc(struct nvc0_screen *screen, void *entry)
689 {
690 int i = screen->tic.next;
691
692 while (screen->tic.lock[i / 32] & (1 << (i % 32)))
693 i = (i + 1) & (NVC0_TIC_MAX_ENTRIES - 1);
694
695 screen->tic.next = (i + 1) & (NVC0_TIC_MAX_ENTRIES - 1);
696
697 if (screen->tic.entries[i])
698 nv50_tic_entry(screen->tic.entries[i])->id = -1;
699
700 screen->tic.entries[i] = entry;
701 return i;
702 }
703
704 int
705 nvc0_screen_tsc_alloc(struct nvc0_screen *screen, void *entry)
706 {
707 int i = screen->tsc.next;
708
709 while (screen->tsc.lock[i / 32] & (1 << (i % 32)))
710 i = (i + 1) & (NVC0_TSC_MAX_ENTRIES - 1);
711
712 screen->tsc.next = (i + 1) & (NVC0_TSC_MAX_ENTRIES - 1);
713
714 if (screen->tsc.entries[i])
715 nv50_tsc_entry(screen->tsc.entries[i])->id = -1;
716
717 screen->tsc.entries[i] = entry;
718 return i;
719 }