• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 
23 #include <xf86drm.h>
24 #include <nouveau_drm.h>
25 #include <nvif/class.h>
26 #include "util/u_format.h"
27 #include "util/u_format_s3tc.h"
28 #include "pipe/p_screen.h"
29 
30 #include "nouveau_vp3_video.h"
31 
32 #include "nvc0/nvc0_context.h"
33 #include "nvc0/nvc0_screen.h"
34 
35 #include "nvc0/mme/com9097.mme.h"
36 #include "nvc0/mme/com90c0.mme.h"
37 
38 #include "nv50/g80_texture.xml.h"
39 
40 static boolean
nvc0_screen_is_format_supported(struct pipe_screen * pscreen,enum pipe_format format,enum pipe_texture_target target,unsigned sample_count,unsigned bindings)41 nvc0_screen_is_format_supported(struct pipe_screen *pscreen,
42                                 enum pipe_format format,
43                                 enum pipe_texture_target target,
44                                 unsigned sample_count,
45                                 unsigned bindings)
46 {
47    const struct util_format_description *desc = util_format_description(format);
48 
49    if (sample_count > 8)
50       return false;
51    if (!(0x117 & (1 << sample_count))) /* 0, 1, 2, 4 or 8 */
52       return false;
53 
54    /* Short-circuit the rest of the logic -- this is used by the state tracker
55     * to determine valid MS levels in a no-attachments scenario.
56     */
57    if (format == PIPE_FORMAT_NONE && bindings & PIPE_BIND_RENDER_TARGET)
58       return true;
59 
60    if (!util_format_is_supported(format, bindings))
61       return false;
62 
63    if ((bindings & PIPE_BIND_SAMPLER_VIEW) && (target != PIPE_BUFFER))
64       if (util_format_get_blocksizebits(format) == 3 * 32)
65          return false;
66 
67    if (bindings & PIPE_BIND_LINEAR)
68       if (util_format_is_depth_or_stencil(format) ||
69           (target != PIPE_TEXTURE_1D &&
70            target != PIPE_TEXTURE_2D &&
71            target != PIPE_TEXTURE_RECT) ||
72           sample_count > 1)
73          return false;
74 
75    /* Restrict ETC2 and ASTC formats here. These are only supported on GK20A.
76     */
77    if ((desc->layout == UTIL_FORMAT_LAYOUT_ETC ||
78         desc->layout == UTIL_FORMAT_LAYOUT_ASTC) &&
79        /* The claim is that this should work on GM107 but it doesn't. Need to
80         * test further and figure out if it's a nouveau issue or a HW one.
81        nouveau_screen(pscreen)->class_3d < GM107_3D_CLASS &&
82         */
83        nouveau_screen(pscreen)->class_3d != NVEA_3D_CLASS)
84       return false;
85 
86    /* shared is always supported */
87    bindings &= ~(PIPE_BIND_LINEAR |
88                  PIPE_BIND_SHARED);
89 
90    if (bindings & PIPE_BIND_SHADER_IMAGE) {
91       if (sample_count > 1 &&
92           nouveau_screen(pscreen)->class_3d >= GM107_3D_CLASS) {
93          /* MS images are currently unsupported on Maxwell because they have to
94           * be handled explicitly. */
95          return false;
96       }
97 
98       if (format == PIPE_FORMAT_B8G8R8A8_UNORM &&
99           nouveau_screen(pscreen)->class_3d < NVE4_3D_CLASS) {
100          /* This should work on Fermi, but for currently unknown reasons it
101           * does not and results in breaking reads from pbos. */
102          return false;
103       }
104    }
105 
106    return (( nvc0_format_table[format].usage |
107             nvc0_vertex_format[format].usage) & bindings) == bindings;
108 }
109 
110 static int
nvc0_screen_get_param(struct pipe_screen * pscreen,enum pipe_cap param)111 nvc0_screen_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
112 {
113    const uint16_t class_3d = nouveau_screen(pscreen)->class_3d;
114    struct nouveau_device *dev = nouveau_screen(pscreen)->device;
115 
116    switch (param) {
117    /* non-boolean caps */
118    case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
119    case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
120       return 15;
121    case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
122       return 12;
123    case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
124       return 2048;
125    case PIPE_CAP_MIN_TEXEL_OFFSET:
126       return -8;
127    case PIPE_CAP_MAX_TEXEL_OFFSET:
128       return 7;
129    case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
130       return -32;
131    case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
132       return 31;
133    case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
134       return 128 * 1024 * 1024;
135    case PIPE_CAP_GLSL_FEATURE_LEVEL:
136       return 430;
137    case PIPE_CAP_MAX_RENDER_TARGETS:
138       return 8;
139    case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
140       return 1;
141    case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
142       return 4;
143    case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
144    case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
145       return 128;
146    case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
147    case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
148       return 1024;
149    case PIPE_CAP_MAX_VERTEX_STREAMS:
150       return 4;
151    case PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE:
152       return 2048;
153    case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
154       return 256;
155    case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
156       if (class_3d < GM107_3D_CLASS)
157          return 256; /* IMAGE bindings require alignment to 256 */
158       return 16;
159    case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
160       return 16;
161    case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
162       return NOUVEAU_MIN_BUFFER_MAP_ALIGN;
163    case PIPE_CAP_MAX_VIEWPORTS:
164       return NVC0_MAX_VIEWPORTS;
165    case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
166       return 4;
167    case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK:
168       return PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50;
169    case PIPE_CAP_ENDIANNESS:
170       return PIPE_ENDIAN_LITTLE;
171    case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
172       return 30;
173    case PIPE_CAP_MAX_WINDOW_RECTANGLES:
174       return NVC0_MAX_WINDOW_RECTANGLES;
175 
176    /* supported caps */
177    case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
178    case PIPE_CAP_TEXTURE_SWIZZLE:
179    case PIPE_CAP_NPOT_TEXTURES:
180    case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
181    case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
182    case PIPE_CAP_ANISOTROPIC_FILTER:
183    case PIPE_CAP_SEAMLESS_CUBE_MAP:
184    case PIPE_CAP_CUBE_MAP_ARRAY:
185    case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
186    case PIPE_CAP_TEXTURE_MULTISAMPLE:
187    case PIPE_CAP_DEPTH_CLIP_DISABLE:
188    case PIPE_CAP_POINT_SPRITE:
189    case PIPE_CAP_TGSI_TEXCOORD:
190    case PIPE_CAP_SM3:
191    case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
192    case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
193    case PIPE_CAP_VERTEX_COLOR_CLAMPED:
194    case PIPE_CAP_QUERY_TIMESTAMP:
195    case PIPE_CAP_QUERY_TIME_ELAPSED:
196    case PIPE_CAP_OCCLUSION_QUERY:
197    case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
198    case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
199    case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
200    case PIPE_CAP_BLEND_EQUATION_SEPARATE:
201    case PIPE_CAP_INDEP_BLEND_ENABLE:
202    case PIPE_CAP_INDEP_BLEND_FUNC:
203    case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
204    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
205    case PIPE_CAP_PRIMITIVE_RESTART:
206    case PIPE_CAP_TGSI_INSTANCEID:
207    case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
208    case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
209    case PIPE_CAP_CONDITIONAL_RENDER:
210    case PIPE_CAP_TEXTURE_BARRIER:
211    case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
212    case PIPE_CAP_START_INSTANCE:
213    case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
214    case PIPE_CAP_DRAW_INDIRECT:
215    case PIPE_CAP_USER_VERTEX_BUFFERS:
216    case PIPE_CAP_TEXTURE_QUERY_LOD:
217    case PIPE_CAP_SAMPLE_SHADING:
218    case PIPE_CAP_TEXTURE_GATHER_OFFSETS:
219    case PIPE_CAP_TEXTURE_GATHER_SM5:
220    case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE:
221    case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
222    case PIPE_CAP_SAMPLER_VIEW_TARGET:
223    case PIPE_CAP_CLIP_HALFZ:
224    case PIPE_CAP_POLYGON_OFFSET_CLAMP:
225    case PIPE_CAP_MULTISAMPLE_Z_RESOLVE:
226    case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
227    case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
228    case PIPE_CAP_DEPTH_BOUNDS_TEST:
229    case PIPE_CAP_TGSI_TXQS:
230    case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
231    case PIPE_CAP_FORCE_PERSAMPLE_INTERP:
232    case PIPE_CAP_SHAREABLE_SHADERS:
233    case PIPE_CAP_CLEAR_TEXTURE:
234    case PIPE_CAP_DRAW_PARAMETERS:
235    case PIPE_CAP_TGSI_PACK_HALF_FLOAT:
236    case PIPE_CAP_MULTI_DRAW_INDIRECT:
237    case PIPE_CAP_MULTI_DRAW_INDIRECT_PARAMS:
238    case PIPE_CAP_TGSI_FS_FACE_IS_INTEGER_SYSVAL:
239    case PIPE_CAP_QUERY_BUFFER_OBJECT:
240    case PIPE_CAP_INVALIDATE_BUFFER:
241    case PIPE_CAP_STRING_MARKER:
242    case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
243    case PIPE_CAP_CULL_DISTANCE:
244    case PIPE_CAP_PRIMITIVE_RESTART_FOR_PATCHES:
245    case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
246    case PIPE_CAP_TGSI_VOTE:
247    case PIPE_CAP_POLYGON_OFFSET_UNITS_UNSCALED:
248    case PIPE_CAP_TGSI_ARRAY_COMPONENTS:
249    case PIPE_CAP_TGSI_MUL_ZERO_WINS:
250    case PIPE_CAP_DOUBLES:
251    case PIPE_CAP_INT64:
252    case PIPE_CAP_TGSI_TEX_TXF_LZ:
253    case PIPE_CAP_TGSI_CLOCK:
254    case PIPE_CAP_COMPUTE:
255    case PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX:
256    case PIPE_CAP_ALLOW_MAPPED_BUFFERS_DURING_EXECUTION:
257       return 1;
258    case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
259       return nouveau_screen(pscreen)->vram_domain & NOUVEAU_BO_VRAM ? 1 : 0;
260    case PIPE_CAP_TGSI_FS_FBFETCH:
261       return class_3d >= NVE4_3D_CLASS; /* needs testing on fermi */
262    case PIPE_CAP_POLYGON_MODE_FILL_RECTANGLE:
263    case PIPE_CAP_TGSI_VS_LAYER_VIEWPORT:
264    case PIPE_CAP_TGSI_TES_LAYER_VIEWPORT:
265    case PIPE_CAP_POST_DEPTH_COVERAGE:
266       return class_3d >= GM200_3D_CLASS;
267    case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
268    case PIPE_CAP_TGSI_BALLOT:
269       return class_3d >= NVE4_3D_CLASS;
270    case PIPE_CAP_BINDLESS_TEXTURE:
271       return class_3d >= NVE4_3D_CLASS && class_3d < GM107_3D_CLASS;
272 
273    /* unsupported caps */
274    case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
275    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
276    case PIPE_CAP_SHADER_STENCIL_EXPORT:
277    case PIPE_CAP_TGSI_CAN_COMPACT_CONSTANTS:
278    case PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY:
279    case PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY:
280    case PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY:
281    case PIPE_CAP_FAKE_SW_MSAA:
282    case PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION:
283    case PIPE_CAP_VERTEXID_NOBASE:
284    case PIPE_CAP_RESOURCE_FROM_USER_MEMORY:
285    case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:
286    case PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL:
287    case PIPE_CAP_GENERATE_MIPMAP:
288    case PIPE_CAP_BUFFER_SAMPLER_VIEW_RGBA_ONLY:
289    case PIPE_CAP_SURFACE_REINTERPRET_BLOCKS:
290    case PIPE_CAP_QUERY_MEMORY_INFO:
291    case PIPE_CAP_PCI_GROUP:
292    case PIPE_CAP_PCI_BUS:
293    case PIPE_CAP_PCI_DEVICE:
294    case PIPE_CAP_PCI_FUNCTION:
295    case PIPE_CAP_VIEWPORT_SUBPIXEL_BITS:
296    case PIPE_CAP_TGSI_CAN_READ_OUTPUTS:
297    case PIPE_CAP_NATIVE_FENCE_FD:
298    case PIPE_CAP_GLSL_OPTIMIZE_CONSERVATIVELY:
299    case PIPE_CAP_INT64_DIVMOD:
300    case PIPE_CAP_SPARSE_BUFFER_PAGE_SIZE:
301    case PIPE_CAP_NIR_SAMPLERS_AS_DEREF:
302    case PIPE_CAP_QUERY_SO_OVERFLOW:
303    case PIPE_CAP_MEMOBJ:
304    case PIPE_CAP_LOAD_CONSTBUF:
305    case PIPE_CAP_TGSI_ANY_REG_AS_ADDRESS:
306    case PIPE_CAP_TILE_RASTER_ORDER:
307    case PIPE_CAP_MAX_COMBINED_SHADER_OUTPUT_RESOURCES:
308    case PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET:
309    case PIPE_CAP_CONTEXT_PRIORITY_MASK:
310       return 0;
311 
312    case PIPE_CAP_VENDOR_ID:
313       return 0x10de;
314    case PIPE_CAP_DEVICE_ID: {
315       uint64_t device_id;
316       if (nouveau_getparam(dev, NOUVEAU_GETPARAM_PCI_DEVICE, &device_id)) {
317          NOUVEAU_ERR("NOUVEAU_GETPARAM_PCI_DEVICE failed.\n");
318          return -1;
319       }
320       return device_id;
321    }
322    case PIPE_CAP_ACCELERATED:
323       return 1;
324    case PIPE_CAP_VIDEO_MEMORY:
325       return dev->vram_size >> 20;
326    case PIPE_CAP_UMA:
327       return 0;
328    }
329 
330    NOUVEAU_ERR("unknown PIPE_CAP %d\n", param);
331    return 0;
332 }
333 
334 static int
nvc0_screen_get_shader_param(struct pipe_screen * pscreen,enum pipe_shader_type shader,enum pipe_shader_cap param)335 nvc0_screen_get_shader_param(struct pipe_screen *pscreen,
336                              enum pipe_shader_type shader,
337                              enum pipe_shader_cap param)
338 {
339    const uint16_t class_3d = nouveau_screen(pscreen)->class_3d;
340 
341    switch (shader) {
342    case PIPE_SHADER_VERTEX:
343    case PIPE_SHADER_GEOMETRY:
344    case PIPE_SHADER_FRAGMENT:
345    case PIPE_SHADER_COMPUTE:
346    case PIPE_SHADER_TESS_CTRL:
347    case PIPE_SHADER_TESS_EVAL:
348       break;
349    default:
350       return 0;
351    }
352 
353    switch (param) {
354    case PIPE_SHADER_CAP_PREFERRED_IR:
355       return PIPE_SHADER_IR_TGSI;
356    case PIPE_SHADER_CAP_SUPPORTED_IRS:
357       return 1 << PIPE_SHADER_IR_TGSI;
358    case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
359    case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
360    case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
361    case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
362       return 16384;
363    case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
364       return 16;
365    case PIPE_SHADER_CAP_MAX_INPUTS:
366       if (shader == PIPE_SHADER_VERTEX)
367          return 32;
368       /* NOTE: These only count our slots for GENERIC varyings.
369        * The address space may be larger, but the actual hard limit seems to be
370        * less than what the address space layout permits, so don't add TEXCOORD,
371        * COLOR, etc. here.
372        */
373       if (shader == PIPE_SHADER_FRAGMENT)
374          return 0x1f0 / 16;
375       /* Actually this counts CLIPVERTEX, which occupies the last generic slot,
376        * and excludes 0x60 per-patch inputs.
377        */
378       return 0x200 / 16;
379    case PIPE_SHADER_CAP_MAX_OUTPUTS:
380       return 32;
381    case PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE:
382       return 65536;
383    case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
384       return NVC0_MAX_PIPE_CONSTBUFS;
385    case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
386       return shader != PIPE_SHADER_FRAGMENT;
387    case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
388    case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
389    case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
390       return 1;
391    case PIPE_SHADER_CAP_MAX_TEMPS:
392       return NVC0_CAP_MAX_PROGRAM_TEMPS;
393    case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
394       return 1;
395    case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
396       return 1;
397    case PIPE_SHADER_CAP_SUBROUTINES:
398       return 1;
399    case PIPE_SHADER_CAP_INTEGERS:
400       return 1;
401    case PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED:
402       return 1;
403    case PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED:
404       return 1;
405    case PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS:
406       return 1;
407    case PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED:
408    case PIPE_SHADER_CAP_TGSI_LDEXP_SUPPORTED:
409    case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
410    case PIPE_SHADER_CAP_LOWER_IF_THRESHOLD:
411    case PIPE_SHADER_CAP_INT64_ATOMICS:
412    case PIPE_SHADER_CAP_FP16:
413    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
414    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
415       return 0;
416    case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
417       return NVC0_MAX_BUFFERS;
418    case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
419       return (class_3d >= NVE4_3D_CLASS) ? 32 : 16;
420    case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
421       return (class_3d >= NVE4_3D_CLASS) ? 32 : 16;
422    case PIPE_SHADER_CAP_MAX_UNROLL_ITERATIONS_HINT:
423       return 32;
424    case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
425       if (class_3d >= NVE4_3D_CLASS)
426          return NVC0_MAX_IMAGES;
427       if (shader == PIPE_SHADER_FRAGMENT || shader == PIPE_SHADER_COMPUTE)
428          return NVC0_MAX_IMAGES;
429       return 0;
430    default:
431       NOUVEAU_ERR("unknown PIPE_SHADER_CAP %d\n", param);
432       return 0;
433    }
434 }
435 
436 static float
nvc0_screen_get_paramf(struct pipe_screen * pscreen,enum pipe_capf param)437 nvc0_screen_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
438 {
439    switch (param) {
440    case PIPE_CAPF_MAX_LINE_WIDTH:
441    case PIPE_CAPF_MAX_LINE_WIDTH_AA:
442       return 10.0f;
443    case PIPE_CAPF_MAX_POINT_WIDTH:
444       return 63.0f;
445    case PIPE_CAPF_MAX_POINT_WIDTH_AA:
446       return 63.375f;
447    case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
448       return 16.0f;
449    case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
450       return 15.0f;
451    case PIPE_CAPF_GUARD_BAND_LEFT:
452    case PIPE_CAPF_GUARD_BAND_TOP:
453       return 0.0f;
454    case PIPE_CAPF_GUARD_BAND_RIGHT:
455    case PIPE_CAPF_GUARD_BAND_BOTTOM:
456       return 0.0f; /* that or infinity */
457    }
458 
459    NOUVEAU_ERR("unknown PIPE_CAPF %d\n", param);
460    return 0.0f;
461 }
462 
463 static int
nvc0_screen_get_compute_param(struct pipe_screen * pscreen,enum pipe_shader_ir ir_type,enum pipe_compute_cap param,void * data)464 nvc0_screen_get_compute_param(struct pipe_screen *pscreen,
465                               enum pipe_shader_ir ir_type,
466                               enum pipe_compute_cap param, void *data)
467 {
468    struct nvc0_screen *screen = nvc0_screen(pscreen);
469    const uint16_t obj_class = screen->compute->oclass;
470 
471 #define RET(x) do {                  \
472    if (data)                         \
473       memcpy(data, x, sizeof(x));    \
474    return sizeof(x);                 \
475 } while (0)
476 
477    switch (param) {
478    case PIPE_COMPUTE_CAP_GRID_DIMENSION:
479       RET((uint64_t []) { 3 });
480    case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
481       if (obj_class >= NVE4_COMPUTE_CLASS) {
482          RET(((uint64_t []) { 0x7fffffff, 65535, 65535 }));
483       } else {
484          RET(((uint64_t []) { 65535, 65535, 65535 }));
485       }
486    case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
487       RET(((uint64_t []) { 1024, 1024, 64 }));
488    case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
489       RET((uint64_t []) { 1024 });
490    case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
491       if (obj_class >= NVE4_COMPUTE_CLASS) {
492          RET((uint64_t []) { 1024 });
493       } else {
494          RET((uint64_t []) { 512 });
495       }
496    case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE: /* g[] */
497       RET((uint64_t []) { 1ULL << 40 });
498    case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE: /* s[] */
499       switch (obj_class) {
500       case GM200_COMPUTE_CLASS:
501          RET((uint64_t []) { 96 << 10 });
502          break;
503       case GM107_COMPUTE_CLASS:
504          RET((uint64_t []) { 64 << 10 });
505          break;
506       default:
507          RET((uint64_t []) { 48 << 10 });
508          break;
509       }
510    case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE: /* l[] */
511       RET((uint64_t []) { 512 << 10 });
512    case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE: /* c[], arbitrary limit */
513       RET((uint64_t []) { 4096 });
514    case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
515       RET((uint32_t []) { 32 });
516    case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
517       RET((uint64_t []) { 1ULL << 40 });
518    case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
519       RET((uint32_t []) { 0 });
520    case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
521       RET((uint32_t []) { screen->mp_count_compute });
522    case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
523       RET((uint32_t []) { 512 }); /* FIXME: arbitrary limit */
524    case PIPE_COMPUTE_CAP_ADDRESS_BITS:
525       RET((uint32_t []) { 64 });
526    default:
527       return 0;
528    }
529 
530 #undef RET
531 }
532 
533 static void
nvc0_screen_destroy(struct pipe_screen * pscreen)534 nvc0_screen_destroy(struct pipe_screen *pscreen)
535 {
536    struct nvc0_screen *screen = nvc0_screen(pscreen);
537 
538    if (!nouveau_drm_screen_unref(&screen->base))
539       return;
540 
541    if (screen->base.fence.current) {
542       struct nouveau_fence *current = NULL;
543 
544       /* nouveau_fence_wait will create a new current fence, so wait on the
545        * _current_ one, and remove both.
546        */
547       nouveau_fence_ref(screen->base.fence.current, &current);
548       nouveau_fence_wait(current, NULL);
549       nouveau_fence_ref(NULL, &current);
550       nouveau_fence_ref(NULL, &screen->base.fence.current);
551    }
552    if (screen->base.pushbuf)
553       screen->base.pushbuf->user_priv = NULL;
554 
555    if (screen->blitter)
556       nvc0_blitter_destroy(screen);
557    if (screen->pm.prog) {
558       screen->pm.prog->code = NULL; /* hardcoded, don't FREE */
559       nvc0_program_destroy(NULL, screen->pm.prog);
560       FREE(screen->pm.prog);
561    }
562 
563    nouveau_bo_ref(NULL, &screen->text);
564    nouveau_bo_ref(NULL, &screen->uniform_bo);
565    nouveau_bo_ref(NULL, &screen->tls);
566    nouveau_bo_ref(NULL, &screen->txc);
567    nouveau_bo_ref(NULL, &screen->fence.bo);
568    nouveau_bo_ref(NULL, &screen->poly_cache);
569 
570    nouveau_heap_destroy(&screen->lib_code);
571    nouveau_heap_destroy(&screen->text_heap);
572 
573    FREE(screen->default_tsc);
574    FREE(screen->tic.entries);
575 
576    nouveau_object_del(&screen->eng3d);
577    nouveau_object_del(&screen->eng2d);
578    nouveau_object_del(&screen->m2mf);
579    nouveau_object_del(&screen->compute);
580    nouveau_object_del(&screen->nvsw);
581 
582    nouveau_screen_fini(&screen->base);
583 
584    FREE(screen);
585 }
586 
587 static int
nvc0_graph_set_macro(struct nvc0_screen * screen,uint32_t m,unsigned pos,unsigned size,const uint32_t * data)588 nvc0_graph_set_macro(struct nvc0_screen *screen, uint32_t m, unsigned pos,
589                      unsigned size, const uint32_t *data)
590 {
591    struct nouveau_pushbuf *push = screen->base.pushbuf;
592 
593    size /= 4;
594 
595    assert((pos + size) <= 0x800);
596 
597    BEGIN_NVC0(push, SUBC_3D(NVC0_GRAPH_MACRO_ID), 2);
598    PUSH_DATA (push, (m - 0x3800) / 8);
599    PUSH_DATA (push, pos);
600    BEGIN_1IC0(push, SUBC_3D(NVC0_GRAPH_MACRO_UPLOAD_POS), size + 1);
601    PUSH_DATA (push, pos);
602    PUSH_DATAp(push, data, size);
603 
604    return pos + size;
605 }
606 
607 static void
nvc0_magic_3d_init(struct nouveau_pushbuf * push,uint16_t obj_class)608 nvc0_magic_3d_init(struct nouveau_pushbuf *push, uint16_t obj_class)
609 {
610    BEGIN_NVC0(push, SUBC_3D(0x10cc), 1);
611    PUSH_DATA (push, 0xff);
612    BEGIN_NVC0(push, SUBC_3D(0x10e0), 2);
613    PUSH_DATA (push, 0xff);
614    PUSH_DATA (push, 0xff);
615    BEGIN_NVC0(push, SUBC_3D(0x10ec), 2);
616    PUSH_DATA (push, 0xff);
617    PUSH_DATA (push, 0xff);
618    BEGIN_NVC0(push, SUBC_3D(0x074c), 1);
619    PUSH_DATA (push, 0x3f);
620 
621    BEGIN_NVC0(push, SUBC_3D(0x16a8), 1);
622    PUSH_DATA (push, (3 << 16) | 3);
623    BEGIN_NVC0(push, SUBC_3D(0x1794), 1);
624    PUSH_DATA (push, (2 << 16) | 2);
625 
626    if (obj_class < GM107_3D_CLASS) {
627       BEGIN_NVC0(push, SUBC_3D(0x12ac), 1);
628       PUSH_DATA (push, 0);
629    }
630    BEGIN_NVC0(push, SUBC_3D(0x0218), 1);
631    PUSH_DATA (push, 0x10);
632    BEGIN_NVC0(push, SUBC_3D(0x10fc), 1);
633    PUSH_DATA (push, 0x10);
634    BEGIN_NVC0(push, SUBC_3D(0x1290), 1);
635    PUSH_DATA (push, 0x10);
636    BEGIN_NVC0(push, SUBC_3D(0x12d8), 2);
637    PUSH_DATA (push, 0x10);
638    PUSH_DATA (push, 0x10);
639    BEGIN_NVC0(push, SUBC_3D(0x1140), 1);
640    PUSH_DATA (push, 0x10);
641    BEGIN_NVC0(push, SUBC_3D(0x1610), 1);
642    PUSH_DATA (push, 0xe);
643 
644    BEGIN_NVC0(push, NVC0_3D(VERTEX_ID_GEN_MODE), 1);
645    PUSH_DATA (push, NVC0_3D_VERTEX_ID_GEN_MODE_DRAW_ARRAYS_ADD_START);
646    BEGIN_NVC0(push, SUBC_3D(0x030c), 1);
647    PUSH_DATA (push, 0);
648    BEGIN_NVC0(push, SUBC_3D(0x0300), 1);
649    PUSH_DATA (push, 3);
650 
651    BEGIN_NVC0(push, SUBC_3D(0x02d0), 1);
652    PUSH_DATA (push, 0x3fffff);
653    BEGIN_NVC0(push, SUBC_3D(0x0fdc), 1);
654    PUSH_DATA (push, 1);
655    BEGIN_NVC0(push, SUBC_3D(0x19c0), 1);
656    PUSH_DATA (push, 1);
657 
658    if (obj_class < GM107_3D_CLASS) {
659       BEGIN_NVC0(push, SUBC_3D(0x075c), 1);
660       PUSH_DATA (push, 3);
661 
662       if (obj_class >= NVE4_3D_CLASS) {
663          BEGIN_NVC0(push, SUBC_3D(0x07fc), 1);
664          PUSH_DATA (push, 1);
665       }
666    }
667 
668    /* TODO: find out what software methods 0x1528, 0x1280 and (on nve4) 0x02dc
669     * are supposed to do */
670 }
671 
672 static void
nvc0_screen_fence_emit(struct pipe_screen * pscreen,u32 * sequence)673 nvc0_screen_fence_emit(struct pipe_screen *pscreen, u32 *sequence)
674 {
675    struct nvc0_screen *screen = nvc0_screen(pscreen);
676    struct nouveau_pushbuf *push = screen->base.pushbuf;
677 
678    /* we need to do it after possible flush in MARK_RING */
679    *sequence = ++screen->base.fence.sequence;
680 
681    assert(PUSH_AVAIL(push) + push->rsvd_kick >= 5);
682    PUSH_DATA (push, NVC0_FIFO_PKHDR_SQ(NVC0_3D(QUERY_ADDRESS_HIGH), 4));
683    PUSH_DATAh(push, screen->fence.bo->offset);
684    PUSH_DATA (push, screen->fence.bo->offset);
685    PUSH_DATA (push, *sequence);
686    PUSH_DATA (push, NVC0_3D_QUERY_GET_FENCE | NVC0_3D_QUERY_GET_SHORT |
687               (0xf << NVC0_3D_QUERY_GET_UNIT__SHIFT));
688 }
689 
690 static u32
nvc0_screen_fence_update(struct pipe_screen * pscreen)691 nvc0_screen_fence_update(struct pipe_screen *pscreen)
692 {
693    struct nvc0_screen *screen = nvc0_screen(pscreen);
694    return screen->fence.map[0];
695 }
696 
697 static int
nvc0_screen_init_compute(struct nvc0_screen * screen)698 nvc0_screen_init_compute(struct nvc0_screen *screen)
699 {
700    screen->base.base.get_compute_param = nvc0_screen_get_compute_param;
701 
702    switch (screen->base.device->chipset & ~0xf) {
703    case 0xc0:
704    case 0xd0:
705       return nvc0_screen_compute_setup(screen, screen->base.pushbuf);
706    case 0xe0:
707    case 0xf0:
708    case 0x100:
709    case 0x110:
710    case 0x120:
711    case 0x130:
712       return nve4_screen_compute_setup(screen, screen->base.pushbuf);
713    default:
714       return -1;
715    }
716 }
717 
718 static int
nvc0_screen_resize_tls_area(struct nvc0_screen * screen,uint32_t lpos,uint32_t lneg,uint32_t cstack)719 nvc0_screen_resize_tls_area(struct nvc0_screen *screen,
720                             uint32_t lpos, uint32_t lneg, uint32_t cstack)
721 {
722    struct nouveau_bo *bo = NULL;
723    int ret;
724    uint64_t size = (lpos + lneg) * 32 + cstack;
725 
726    if (size >= (1 << 20)) {
727       NOUVEAU_ERR("requested TLS size too large: 0x%"PRIx64"\n", size);
728       return -1;
729    }
730 
731    size *= (screen->base.device->chipset >= 0xe0) ? 64 : 48; /* max warps */
732    size  = align(size, 0x8000);
733    size *= screen->mp_count;
734 
735    size = align(size, 1 << 17);
736 
737    ret = nouveau_bo_new(screen->base.device, NV_VRAM_DOMAIN(&screen->base), 1 << 17, size,
738                         NULL, &bo);
739    if (ret)
740       return ret;
741 
742    /* Make sure that the pushbuf has acquired a reference to the old tls
743     * segment, as it may have commands that will reference it.
744     */
745    if (screen->tls)
746       PUSH_REFN(screen->base.pushbuf, screen->tls,
747                 NV_VRAM_DOMAIN(&screen->base) | NOUVEAU_BO_RDWR);
748    nouveau_bo_ref(NULL, &screen->tls);
749    screen->tls = bo;
750    return 0;
751 }
752 
753 int
nvc0_screen_resize_text_area(struct nvc0_screen * screen,uint64_t size)754 nvc0_screen_resize_text_area(struct nvc0_screen *screen, uint64_t size)
755 {
756    struct nouveau_pushbuf *push = screen->base.pushbuf;
757    struct nouveau_bo *bo;
758    int ret;
759 
760    ret = nouveau_bo_new(screen->base.device, NV_VRAM_DOMAIN(&screen->base),
761                         1 << 17, size, NULL, &bo);
762    if (ret)
763       return ret;
764 
765    /* Make sure that the pushbuf has acquired a reference to the old text
766     * segment, as it may have commands that will reference it.
767     */
768    if (screen->text)
769       PUSH_REFN(push, screen->text,
770                 NV_VRAM_DOMAIN(&screen->base) | NOUVEAU_BO_RD);
771    nouveau_bo_ref(NULL, &screen->text);
772    screen->text = bo;
773 
774    nouveau_heap_destroy(&screen->lib_code);
775    nouveau_heap_destroy(&screen->text_heap);
776 
777    /* XXX: getting a page fault at the end of the code buffer every few
778     *  launches, don't use the last 256 bytes to work around them - prefetch ?
779     */
780    nouveau_heap_init(&screen->text_heap, 0, size - 0x100);
781 
782    /* update the code segment setup */
783    BEGIN_NVC0(push, NVC0_3D(CODE_ADDRESS_HIGH), 2);
784    PUSH_DATAh(push, screen->text->offset);
785    PUSH_DATA (push, screen->text->offset);
786    if (screen->compute) {
787       BEGIN_NVC0(push, NVC0_CP(CODE_ADDRESS_HIGH), 2);
788       PUSH_DATAh(push, screen->text->offset);
789       PUSH_DATA (push, screen->text->offset);
790    }
791 
792    return 0;
793 }
794 
795 #define FAIL_SCREEN_INIT(str, err)                    \
796    do {                                               \
797       NOUVEAU_ERR(str, err);                          \
798       goto fail;                                      \
799    } while(0)
800 
801 struct nouveau_screen *
nvc0_screen_create(struct nouveau_device * dev)802 nvc0_screen_create(struct nouveau_device *dev)
803 {
804    struct nvc0_screen *screen;
805    struct pipe_screen *pscreen;
806    struct nouveau_object *chan;
807    struct nouveau_pushbuf *push;
808    uint64_t value;
809    uint32_t obj_class;
810    uint32_t flags;
811    int ret;
812    unsigned i;
813 
814    switch (dev->chipset & ~0xf) {
815    case 0xc0:
816    case 0xd0:
817    case 0xe0:
818    case 0xf0:
819    case 0x100:
820    case 0x110:
821    case 0x120:
822    case 0x130:
823       break;
824    default:
825       return NULL;
826    }
827 
828    screen = CALLOC_STRUCT(nvc0_screen);
829    if (!screen)
830       return NULL;
831    pscreen = &screen->base.base;
832    pscreen->destroy = nvc0_screen_destroy;
833 
834    ret = nouveau_screen_init(&screen->base, dev);
835    if (ret)
836       FAIL_SCREEN_INIT("Base screen init failed: %d\n", ret);
837    chan = screen->base.channel;
838    push = screen->base.pushbuf;
839    push->user_priv = screen;
840    push->rsvd_kick = 5;
841 
842    screen->base.vidmem_bindings |= PIPE_BIND_CONSTANT_BUFFER |
843       PIPE_BIND_SHADER_BUFFER |
844       PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER |
845       PIPE_BIND_COMMAND_ARGS_BUFFER | PIPE_BIND_QUERY_BUFFER;
846    screen->base.sysmem_bindings |=
847       PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER;
848 
849    if (screen->base.vram_domain & NOUVEAU_BO_GART) {
850       screen->base.sysmem_bindings |= screen->base.vidmem_bindings;
851       screen->base.vidmem_bindings = 0;
852    }
853 
854    pscreen->context_create = nvc0_create;
855    pscreen->is_format_supported = nvc0_screen_is_format_supported;
856    pscreen->get_param = nvc0_screen_get_param;
857    pscreen->get_shader_param = nvc0_screen_get_shader_param;
858    pscreen->get_paramf = nvc0_screen_get_paramf;
859    pscreen->get_driver_query_info = nvc0_screen_get_driver_query_info;
860    pscreen->get_driver_query_group_info = nvc0_screen_get_driver_query_group_info;
861 
862    nvc0_screen_init_resource_functions(pscreen);
863 
864    screen->base.base.get_video_param = nouveau_vp3_screen_get_video_param;
865    screen->base.base.is_video_format_supported = nouveau_vp3_screen_video_supported;
866 
867    flags = NOUVEAU_BO_GART | NOUVEAU_BO_MAP;
868    if (screen->base.drm->version >= 0x01000202)
869       flags |= NOUVEAU_BO_COHERENT;
870 
871    ret = nouveau_bo_new(dev, flags, 0, 4096, NULL, &screen->fence.bo);
872    if (ret)
873       FAIL_SCREEN_INIT("Error allocating fence BO: %d\n", ret);
874    nouveau_bo_map(screen->fence.bo, 0, NULL);
875    screen->fence.map = screen->fence.bo->map;
876    screen->base.fence.emit = nvc0_screen_fence_emit;
877    screen->base.fence.update = nvc0_screen_fence_update;
878 
879 
880    ret = nouveau_object_new(chan, (dev->chipset < 0xe0) ? 0x1f906e : 0x906e,
881                             NVIF_CLASS_SW_GF100, NULL, 0, &screen->nvsw);
882    if (ret)
883       FAIL_SCREEN_INIT("Error creating SW object: %d\n", ret);
884 
885    BEGIN_NVC0(push, SUBC_SW(NV01_SUBCHAN_OBJECT), 1);
886    PUSH_DATA (push, screen->nvsw->handle);
887 
888    switch (dev->chipset & ~0xf) {
889    case 0x130:
890    case 0x120:
891    case 0x110:
892    case 0x100:
893    case 0xf0:
894       obj_class = NVF0_P2MF_CLASS;
895       break;
896    case 0xe0:
897       obj_class = NVE4_P2MF_CLASS;
898       break;
899    default:
900       obj_class = NVC0_M2MF_CLASS;
901       break;
902    }
903    ret = nouveau_object_new(chan, 0xbeef323f, obj_class, NULL, 0,
904                             &screen->m2mf);
905    if (ret)
906       FAIL_SCREEN_INIT("Error allocating PGRAPH context for M2MF: %d\n", ret);
907 
908    BEGIN_NVC0(push, SUBC_M2MF(NV01_SUBCHAN_OBJECT), 1);
909    PUSH_DATA (push, screen->m2mf->oclass);
910    if (screen->m2mf->oclass == NVE4_P2MF_CLASS) {
911       BEGIN_NVC0(push, SUBC_COPY(NV01_SUBCHAN_OBJECT), 1);
912       PUSH_DATA (push, 0xa0b5);
913    }
914 
915    ret = nouveau_object_new(chan, 0xbeef902d, NVC0_2D_CLASS, NULL, 0,
916                             &screen->eng2d);
917    if (ret)
918       FAIL_SCREEN_INIT("Error allocating PGRAPH context for 2D: %d\n", ret);
919 
920    BEGIN_NVC0(push, SUBC_2D(NV01_SUBCHAN_OBJECT), 1);
921    PUSH_DATA (push, screen->eng2d->oclass);
922    BEGIN_NVC0(push, SUBC_2D(NVC0_2D_SINGLE_GPC), 1);
923    PUSH_DATA (push, 0);
924    BEGIN_NVC0(push, NVC0_2D(OPERATION), 1);
925    PUSH_DATA (push, NV50_2D_OPERATION_SRCCOPY);
926    BEGIN_NVC0(push, NVC0_2D(CLIP_ENABLE), 1);
927    PUSH_DATA (push, 0);
928    BEGIN_NVC0(push, NVC0_2D(COLOR_KEY_ENABLE), 1);
929    PUSH_DATA (push, 0);
930    BEGIN_NVC0(push, SUBC_2D(0x0884), 1);
931    PUSH_DATA (push, 0x3f);
932    BEGIN_NVC0(push, SUBC_2D(0x0888), 1);
933    PUSH_DATA (push, 1);
934    BEGIN_NVC0(push, NVC0_2D(COND_MODE), 1);
935    PUSH_DATA (push, NV50_2D_COND_MODE_ALWAYS);
936 
937    BEGIN_NVC0(push, SUBC_2D(NVC0_GRAPH_NOTIFY_ADDRESS_HIGH), 2);
938    PUSH_DATAh(push, screen->fence.bo->offset + 16);
939    PUSH_DATA (push, screen->fence.bo->offset + 16);
940 
941    switch (dev->chipset & ~0xf) {
942    case 0x130:
943       switch (dev->chipset) {
944       case 0x130:
945       case 0x13b:
946          obj_class = GP100_3D_CLASS;
947          break;
948       default:
949          obj_class = GP102_3D_CLASS;
950          break;
951       }
952       break;
953    case 0x120:
954       obj_class = GM200_3D_CLASS;
955       break;
956    case 0x110:
957       obj_class = GM107_3D_CLASS;
958       break;
959    case 0x100:
960    case 0xf0:
961       obj_class = NVF0_3D_CLASS;
962       break;
963    case 0xe0:
964       switch (dev->chipset) {
965       case 0xea:
966          obj_class = NVEA_3D_CLASS;
967          break;
968       default:
969          obj_class = NVE4_3D_CLASS;
970          break;
971       }
972       break;
973    case 0xd0:
974       obj_class = NVC8_3D_CLASS;
975       break;
976    case 0xc0:
977    default:
978       switch (dev->chipset) {
979       case 0xc8:
980          obj_class = NVC8_3D_CLASS;
981          break;
982       case 0xc1:
983          obj_class = NVC1_3D_CLASS;
984          break;
985       default:
986          obj_class = NVC0_3D_CLASS;
987          break;
988       }
989       break;
990    }
991    ret = nouveau_object_new(chan, 0xbeef003d, obj_class, NULL, 0,
992                             &screen->eng3d);
993    if (ret)
994       FAIL_SCREEN_INIT("Error allocating PGRAPH context for 3D: %d\n", ret);
995    screen->base.class_3d = obj_class;
996 
997    BEGIN_NVC0(push, SUBC_3D(NV01_SUBCHAN_OBJECT), 1);
998    PUSH_DATA (push, screen->eng3d->oclass);
999 
1000    BEGIN_NVC0(push, NVC0_3D(COND_MODE), 1);
1001    PUSH_DATA (push, NVC0_3D_COND_MODE_ALWAYS);
1002 
1003    if (debug_get_bool_option("NOUVEAU_SHADER_WATCHDOG", true)) {
1004       /* kill shaders after about 1 second (at 100 MHz) */
1005       BEGIN_NVC0(push, NVC0_3D(WATCHDOG_TIMER), 1);
1006       PUSH_DATA (push, 0x17);
1007    }
1008 
1009    IMMED_NVC0(push, NVC0_3D(ZETA_COMP_ENABLE),
1010                     screen->base.drm->version >= 0x01000101);
1011    BEGIN_NVC0(push, NVC0_3D(RT_COMP_ENABLE(0)), 8);
1012    for (i = 0; i < 8; ++i)
1013       PUSH_DATA(push, screen->base.drm->version >= 0x01000101);
1014 
1015    BEGIN_NVC0(push, NVC0_3D(RT_CONTROL), 1);
1016    PUSH_DATA (push, 1);
1017 
1018    BEGIN_NVC0(push, NVC0_3D(CSAA_ENABLE), 1);
1019    PUSH_DATA (push, 0);
1020    BEGIN_NVC0(push, NVC0_3D(MULTISAMPLE_ENABLE), 1);
1021    PUSH_DATA (push, 0);
1022    BEGIN_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), 1);
1023    PUSH_DATA (push, NVC0_3D_MULTISAMPLE_MODE_MS1);
1024    BEGIN_NVC0(push, NVC0_3D(MULTISAMPLE_CTRL), 1);
1025    PUSH_DATA (push, 0);
1026    BEGIN_NVC0(push, NVC0_3D(LINE_WIDTH_SEPARATE), 1);
1027    PUSH_DATA (push, 1);
1028    BEGIN_NVC0(push, NVC0_3D(PRIM_RESTART_WITH_DRAW_ARRAYS), 1);
1029    PUSH_DATA (push, 1);
1030    BEGIN_NVC0(push, NVC0_3D(BLEND_SEPARATE_ALPHA), 1);
1031    PUSH_DATA (push, 1);
1032    BEGIN_NVC0(push, NVC0_3D(BLEND_ENABLE_COMMON), 1);
1033    PUSH_DATA (push, 0);
1034    BEGIN_NVC0(push, NVC0_3D(SHADE_MODEL), 1);
1035    PUSH_DATA (push, NVC0_3D_SHADE_MODEL_SMOOTH);
1036    if (screen->eng3d->oclass < NVE4_3D_CLASS) {
1037       IMMED_NVC0(push, NVC0_3D(TEX_MISC), 0);
1038    } else {
1039       BEGIN_NVC0(push, NVE4_3D(TEX_CB_INDEX), 1);
1040       PUSH_DATA (push, 15);
1041    }
1042    BEGIN_NVC0(push, NVC0_3D(CALL_LIMIT_LOG), 1);
1043    PUSH_DATA (push, 8); /* 128 */
1044    BEGIN_NVC0(push, NVC0_3D(ZCULL_STATCTRS_ENABLE), 1);
1045    PUSH_DATA (push, 1);
1046    if (screen->eng3d->oclass >= NVC1_3D_CLASS) {
1047       BEGIN_NVC0(push, NVC0_3D(CACHE_SPLIT), 1);
1048       PUSH_DATA (push, NVC0_3D_CACHE_SPLIT_48K_SHARED_16K_L1);
1049    }
1050 
1051    nvc0_magic_3d_init(push, screen->eng3d->oclass);
1052 
1053    ret = nvc0_screen_resize_text_area(screen, 1 << 19);
1054    if (ret)
1055       FAIL_SCREEN_INIT("Error allocating TEXT area: %d\n", ret);
1056 
1057    /* 6 user uniform areas, 6 driver areas, and 1 for the runout */
1058    ret = nouveau_bo_new(dev, NV_VRAM_DOMAIN(&screen->base), 1 << 12, 13 << 16, NULL,
1059                         &screen->uniform_bo);
1060    if (ret)
1061       FAIL_SCREEN_INIT("Error allocating uniform BO: %d\n", ret);
1062 
1063    PUSH_REFN (push, screen->uniform_bo, NV_VRAM_DOMAIN(&screen->base) | NOUVEAU_BO_WR);
1064 
1065    /* return { 0.0, 0.0, 0.0, 0.0 } for out-of-bounds vtxbuf access */
1066    BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
1067    PUSH_DATA (push, 256);
1068    PUSH_DATAh(push, screen->uniform_bo->offset + NVC0_CB_AUX_RUNOUT_INFO);
1069    PUSH_DATA (push, screen->uniform_bo->offset + NVC0_CB_AUX_RUNOUT_INFO);
1070    BEGIN_1IC0(push, NVC0_3D(CB_POS), 5);
1071    PUSH_DATA (push, 0);
1072    PUSH_DATAf(push, 0.0f);
1073    PUSH_DATAf(push, 0.0f);
1074    PUSH_DATAf(push, 0.0f);
1075    PUSH_DATAf(push, 0.0f);
1076    BEGIN_NVC0(push, NVC0_3D(VERTEX_RUNOUT_ADDRESS_HIGH), 2);
1077    PUSH_DATAh(push, screen->uniform_bo->offset + NVC0_CB_AUX_RUNOUT_INFO);
1078    PUSH_DATA (push, screen->uniform_bo->offset + NVC0_CB_AUX_RUNOUT_INFO);
1079 
1080    if (screen->base.drm->version >= 0x01000101) {
1081       ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_GRAPH_UNITS, &value);
1082       if (ret)
1083          FAIL_SCREEN_INIT("NOUVEAU_GETPARAM_GRAPH_UNITS failed: %d\n", ret);
1084    } else {
1085       if (dev->chipset >= 0xe0 && dev->chipset < 0xf0)
1086          value = (8 << 8) | 4;
1087       else
1088          value = (16 << 8) | 4;
1089    }
1090    screen->gpc_count = value & 0x000000ff;
1091    screen->mp_count = value >> 8;
1092    screen->mp_count_compute = screen->mp_count;
1093 
1094    ret = nvc0_screen_resize_tls_area(screen, 128 * 16, 0, 0x200);
1095    if (ret)
1096       FAIL_SCREEN_INIT("Error allocating TLS area: %d\n", ret);
1097 
1098    BEGIN_NVC0(push, NVC0_3D(TEMP_ADDRESS_HIGH), 4);
1099    PUSH_DATAh(push, screen->tls->offset);
1100    PUSH_DATA (push, screen->tls->offset);
1101    PUSH_DATA (push, screen->tls->size >> 32);
1102    PUSH_DATA (push, screen->tls->size);
1103    BEGIN_NVC0(push, NVC0_3D(WARP_TEMP_ALLOC), 1);
1104    PUSH_DATA (push, 0);
1105    /* Reduce likelihood of collision with real buffers by placing the hole at
1106     * the top of the 4G area. This will have to be dealt with for real
1107     * eventually by blocking off that area from the VM.
1108     */
1109    BEGIN_NVC0(push, NVC0_3D(LOCAL_BASE), 1);
1110    PUSH_DATA (push, 0xff << 24);
1111 
1112    if (screen->eng3d->oclass < GM107_3D_CLASS) {
1113       ret = nouveau_bo_new(dev, NV_VRAM_DOMAIN(&screen->base), 1 << 17, 1 << 20, NULL,
1114                            &screen->poly_cache);
1115       if (ret)
1116          FAIL_SCREEN_INIT("Error allocating poly cache BO: %d\n", ret);
1117 
1118       BEGIN_NVC0(push, NVC0_3D(VERTEX_QUARANTINE_ADDRESS_HIGH), 3);
1119       PUSH_DATAh(push, screen->poly_cache->offset);
1120       PUSH_DATA (push, screen->poly_cache->offset);
1121       PUSH_DATA (push, 3);
1122    }
1123 
1124    ret = nouveau_bo_new(dev, NV_VRAM_DOMAIN(&screen->base), 1 << 17, 1 << 17, NULL,
1125                         &screen->txc);
1126    if (ret)
1127       FAIL_SCREEN_INIT("Error allocating txc BO: %d\n", ret);
1128 
1129    BEGIN_NVC0(push, NVC0_3D(TIC_ADDRESS_HIGH), 3);
1130    PUSH_DATAh(push, screen->txc->offset);
1131    PUSH_DATA (push, screen->txc->offset);
1132    PUSH_DATA (push, NVC0_TIC_MAX_ENTRIES - 1);
1133    if (screen->eng3d->oclass >= GM107_3D_CLASS) {
1134       screen->tic.maxwell = true;
1135       if (screen->eng3d->oclass == GM107_3D_CLASS) {
1136          screen->tic.maxwell =
1137             debug_get_bool_option("NOUVEAU_MAXWELL_TIC", true);
1138          IMMED_NVC0(push, SUBC_3D(0x0f10), screen->tic.maxwell);
1139       }
1140    }
1141 
1142    BEGIN_NVC0(push, NVC0_3D(TSC_ADDRESS_HIGH), 3);
1143    PUSH_DATAh(push, screen->txc->offset + 65536);
1144    PUSH_DATA (push, screen->txc->offset + 65536);
1145    PUSH_DATA (push, NVC0_TSC_MAX_ENTRIES - 1);
1146 
1147    BEGIN_NVC0(push, NVC0_3D(SCREEN_Y_CONTROL), 1);
1148    PUSH_DATA (push, 0);
1149    BEGIN_NVC0(push, NVC0_3D(WINDOW_OFFSET_X), 2);
1150    PUSH_DATA (push, 0);
1151    PUSH_DATA (push, 0);
1152    BEGIN_NVC0(push, NVC0_3D(ZCULL_REGION), 1); /* deactivate ZCULL */
1153    PUSH_DATA (push, 0x3f);
1154 
1155    BEGIN_NVC0(push, NVC0_3D(CLIP_RECTS_MODE), 1);
1156    PUSH_DATA (push, NVC0_3D_CLIP_RECTS_MODE_INSIDE_ANY);
1157    BEGIN_NVC0(push, NVC0_3D(CLIP_RECT_HORIZ(0)), 8 * 2);
1158    for (i = 0; i < 8 * 2; ++i)
1159       PUSH_DATA(push, 0);
1160    BEGIN_NVC0(push, NVC0_3D(CLIP_RECTS_EN), 1);
1161    PUSH_DATA (push, 0);
1162    BEGIN_NVC0(push, NVC0_3D(CLIPID_ENABLE), 1);
1163    PUSH_DATA (push, 0);
1164 
1165    /* neither scissors, viewport nor stencil mask should affect clears */
1166    BEGIN_NVC0(push, NVC0_3D(CLEAR_FLAGS), 1);
1167    PUSH_DATA (push, 0);
1168 
1169    BEGIN_NVC0(push, NVC0_3D(VIEWPORT_TRANSFORM_EN), 1);
1170    PUSH_DATA (push, 1);
1171    for (i = 0; i < NVC0_MAX_VIEWPORTS; i++) {
1172       BEGIN_NVC0(push, NVC0_3D(DEPTH_RANGE_NEAR(i)), 2);
1173       PUSH_DATAf(push, 0.0f);
1174       PUSH_DATAf(push, 1.0f);
1175    }
1176    BEGIN_NVC0(push, NVC0_3D(VIEW_VOLUME_CLIP_CTRL), 1);
1177    PUSH_DATA (push, NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK1_UNK1);
1178 
1179    /* We use scissors instead of exact view volume clipping,
1180     * so they're always enabled.
1181     */
1182    for (i = 0; i < NVC0_MAX_VIEWPORTS; i++) {
1183       BEGIN_NVC0(push, NVC0_3D(SCISSOR_ENABLE(i)), 3);
1184       PUSH_DATA (push, 1);
1185       PUSH_DATA (push, 8192 << 16);
1186       PUSH_DATA (push, 8192 << 16);
1187    }
1188 
1189 #define MK_MACRO(m, n) i = nvc0_graph_set_macro(screen, m, i, sizeof(n), n);
1190 
1191    i = 0;
1192    MK_MACRO(NVC0_3D_MACRO_VERTEX_ARRAY_PER_INSTANCE, mme9097_per_instance_bf);
1193    MK_MACRO(NVC0_3D_MACRO_BLEND_ENABLES, mme9097_blend_enables);
1194    MK_MACRO(NVC0_3D_MACRO_VERTEX_ARRAY_SELECT, mme9097_vertex_array_select);
1195    MK_MACRO(NVC0_3D_MACRO_TEP_SELECT, mme9097_tep_select);
1196    MK_MACRO(NVC0_3D_MACRO_GP_SELECT, mme9097_gp_select);
1197    MK_MACRO(NVC0_3D_MACRO_POLYGON_MODE_FRONT, mme9097_poly_mode_front);
1198    MK_MACRO(NVC0_3D_MACRO_POLYGON_MODE_BACK, mme9097_poly_mode_back);
1199    MK_MACRO(NVC0_3D_MACRO_DRAW_ARRAYS_INDIRECT, mme9097_draw_arrays_indirect);
1200    MK_MACRO(NVC0_3D_MACRO_DRAW_ELEMENTS_INDIRECT, mme9097_draw_elts_indirect);
1201    MK_MACRO(NVC0_3D_MACRO_DRAW_ARRAYS_INDIRECT_COUNT, mme9097_draw_arrays_indirect_count);
1202    MK_MACRO(NVC0_3D_MACRO_DRAW_ELEMENTS_INDIRECT_COUNT, mme9097_draw_elts_indirect_count);
1203    MK_MACRO(NVC0_3D_MACRO_QUERY_BUFFER_WRITE, mme9097_query_buffer_write);
1204    MK_MACRO(NVC0_CP_MACRO_LAUNCH_GRID_INDIRECT, mme90c0_launch_grid_indirect);
1205 
1206    BEGIN_NVC0(push, NVC0_3D(RASTERIZE_ENABLE), 1);
1207    PUSH_DATA (push, 1);
1208    BEGIN_NVC0(push, NVC0_3D(RT_SEPARATE_FRAG_DATA), 1);
1209    PUSH_DATA (push, 1);
1210    BEGIN_NVC0(push, NVC0_3D(MACRO_GP_SELECT), 1);
1211    PUSH_DATA (push, 0x40);
1212    BEGIN_NVC0(push, NVC0_3D(LAYER), 1);
1213    PUSH_DATA (push, 0);
1214    BEGIN_NVC0(push, NVC0_3D(MACRO_TEP_SELECT), 1);
1215    PUSH_DATA (push, 0x30);
1216    BEGIN_NVC0(push, NVC0_3D(PATCH_VERTICES), 1);
1217    PUSH_DATA (push, 3);
1218    BEGIN_NVC0(push, NVC0_3D(SP_SELECT(2)), 1);
1219    PUSH_DATA (push, 0x20);
1220    BEGIN_NVC0(push, NVC0_3D(SP_SELECT(0)), 1);
1221    PUSH_DATA (push, 0x00);
1222    screen->save_state.patch_vertices = 3;
1223 
1224    BEGIN_NVC0(push, NVC0_3D(POINT_COORD_REPLACE), 1);
1225    PUSH_DATA (push, 0);
1226    BEGIN_NVC0(push, NVC0_3D(POINT_RASTER_RULES), 1);
1227    PUSH_DATA (push, NVC0_3D_POINT_RASTER_RULES_OGL);
1228 
1229    IMMED_NVC0(push, NVC0_3D(EDGEFLAG), 1);
1230 
1231    if (nvc0_screen_init_compute(screen))
1232       goto fail;
1233 
1234    /* XXX: Compute and 3D are somehow aliased on Fermi. */
1235    for (i = 0; i < 5; ++i) {
1236       /* TIC and TSC entries for each unit (nve4+ only) */
1237       /* auxiliary constants (6 user clip planes, base instance id) */
1238       BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
1239       PUSH_DATA (push, NVC0_CB_AUX_SIZE);
1240       PUSH_DATAh(push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(i));
1241       PUSH_DATA (push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(i));
1242       BEGIN_NVC0(push, NVC0_3D(CB_BIND(i)), 1);
1243       PUSH_DATA (push, (15 << 4) | 1);
1244       if (screen->eng3d->oclass >= NVE4_3D_CLASS) {
1245          unsigned j;
1246          BEGIN_1IC0(push, NVC0_3D(CB_POS), 9);
1247          PUSH_DATA (push, NVC0_CB_AUX_UNK_INFO);
1248          for (j = 0; j < 8; ++j)
1249             PUSH_DATA(push, j);
1250       } else {
1251          BEGIN_NVC0(push, NVC0_3D(TEX_LIMITS(i)), 1);
1252          PUSH_DATA (push, 0x54);
1253       }
1254 
1255       /* MS sample coordinate offsets: these do not work with _ALT modes ! */
1256       BEGIN_1IC0(push, NVC0_3D(CB_POS), 1 + 2 * 8);
1257       PUSH_DATA (push, NVC0_CB_AUX_MS_INFO);
1258       PUSH_DATA (push, 0); /* 0 */
1259       PUSH_DATA (push, 0);
1260       PUSH_DATA (push, 1); /* 1 */
1261       PUSH_DATA (push, 0);
1262       PUSH_DATA (push, 0); /* 2 */
1263       PUSH_DATA (push, 1);
1264       PUSH_DATA (push, 1); /* 3 */
1265       PUSH_DATA (push, 1);
1266       PUSH_DATA (push, 2); /* 4 */
1267       PUSH_DATA (push, 0);
1268       PUSH_DATA (push, 3); /* 5 */
1269       PUSH_DATA (push, 0);
1270       PUSH_DATA (push, 2); /* 6 */
1271       PUSH_DATA (push, 1);
1272       PUSH_DATA (push, 3); /* 7 */
1273       PUSH_DATA (push, 1);
1274    }
1275    BEGIN_NVC0(push, NVC0_3D(LINKED_TSC), 1);
1276    PUSH_DATA (push, 0);
1277 
1278    PUSH_KICK (push);
1279 
1280    screen->tic.entries = CALLOC(
1281          NVC0_TIC_MAX_ENTRIES + NVC0_TSC_MAX_ENTRIES + NVE4_IMG_MAX_HANDLES,
1282          sizeof(void *));
1283    screen->tsc.entries = screen->tic.entries + NVC0_TIC_MAX_ENTRIES;
1284    screen->img.entries = (void *)(screen->tsc.entries + NVC0_TSC_MAX_ENTRIES);
1285 
1286    if (!nvc0_blitter_create(screen))
1287       goto fail;
1288 
1289    screen->default_tsc = CALLOC_STRUCT(nv50_tsc_entry);
1290    screen->default_tsc->tsc[0] = G80_TSC_0_SRGB_CONVERSION;
1291 
1292    nouveau_fence_new(&screen->base, &screen->base.fence.current);
1293 
1294    return &screen->base;
1295 
1296 fail:
1297    screen->base.base.context_create = NULL;
1298    return &screen->base;
1299 }
1300 
1301 int
nvc0_screen_tic_alloc(struct nvc0_screen * screen,void * entry)1302 nvc0_screen_tic_alloc(struct nvc0_screen *screen, void *entry)
1303 {
1304    int i = screen->tic.next;
1305 
1306    while (screen->tic.lock[i / 32] & (1 << (i % 32)))
1307       i = (i + 1) & (NVC0_TIC_MAX_ENTRIES - 1);
1308 
1309    screen->tic.next = (i + 1) & (NVC0_TIC_MAX_ENTRIES - 1);
1310 
1311    if (screen->tic.entries[i])
1312       nv50_tic_entry(screen->tic.entries[i])->id = -1;
1313 
1314    screen->tic.entries[i] = entry;
1315    return i;
1316 }
1317 
1318 int
nvc0_screen_tsc_alloc(struct nvc0_screen * screen,void * entry)1319 nvc0_screen_tsc_alloc(struct nvc0_screen *screen, void *entry)
1320 {
1321    int i = screen->tsc.next;
1322 
1323    while (screen->tsc.lock[i / 32] & (1 << (i % 32)))
1324       i = (i + 1) & (NVC0_TSC_MAX_ENTRIES - 1);
1325 
1326    screen->tsc.next = (i + 1) & (NVC0_TSC_MAX_ENTRIES - 1);
1327 
1328    if (screen->tsc.entries[i])
1329       nv50_tsc_entry(screen->tsc.entries[i])->id = -1;
1330 
1331    screen->tsc.entries[i] = entry;
1332    return i;
1333 }
1334