i965: Allocate tile aligned height
[mesa.git] / src / mesa / drivers / dri / i965 / intel_screen.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <drm_fourcc.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "main/texobj.h"
34 #include "main/hash.h"
35 #include "main/fbobject.h"
36 #include "main/version.h"
37 #include "swrast/s_renderbuffer.h"
38 #include "util/ralloc.h"
39 #include "brw_defines.h"
40 #include "compiler/nir/nir.h"
41
42 #include "utils.h"
43 #include "xmlpool.h"
44
45 #ifndef DRM_FORMAT_MOD_INVALID
46 #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
47 #endif
48
49 #ifndef DRM_FORMAT_MOD_LINEAR
50 #define DRM_FORMAT_MOD_LINEAR 0
51 #endif
52
53 static const __DRIconfigOptionsExtension brw_config_options = {
54 .base = { __DRI_CONFIG_OPTIONS, 1 },
55 .xml =
56 DRI_CONF_BEGIN
57 DRI_CONF_SECTION_PERFORMANCE
58 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
59 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
60 * DRI_CONF_BO_REUSE_ALL
61 */
62 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
63 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
64 DRI_CONF_ENUM(0, "Disable buffer object reuse")
65 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
66 DRI_CONF_DESC_END
67 DRI_CONF_OPT_END
68 DRI_CONF_SECTION_END
69
70 DRI_CONF_SECTION_QUALITY
71 DRI_CONF_FORCE_S3TC_ENABLE("false")
72
73 DRI_CONF_PRECISE_TRIG("false")
74
75 DRI_CONF_OPT_BEGIN(clamp_max_samples, int, -1)
76 DRI_CONF_DESC(en, "Clamp the value of GL_MAX_SAMPLES to the "
77 "given integer. If negative, then do not clamp.")
78 DRI_CONF_OPT_END
79 DRI_CONF_SECTION_END
80
81 DRI_CONF_SECTION_DEBUG
82 DRI_CONF_NO_RAST("false")
83 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
84 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
85 DRI_CONF_DISABLE_THROTTLING("false")
86 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
87 DRI_CONF_FORCE_GLSL_VERSION(0)
88 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
89 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
90 DRI_CONF_DUAL_COLOR_BLEND_BY_LOCATION("false")
91 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
92 DRI_CONF_ALLOW_GLSL_BUILTIN_VARIABLE_REDECLARATION("false")
93 DRI_CONF_ALLOW_HIGHER_COMPAT_VERSION("false")
94 DRI_CONF_FORCE_GLSL_ABS_SQRT("false")
95
96 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
97 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
98 DRI_CONF_OPT_END
99 DRI_CONF_SECTION_END
100
101 DRI_CONF_SECTION_MISCELLANEOUS
102 DRI_CONF_GLSL_ZERO_INIT("false")
103 DRI_CONF_SECTION_END
104 DRI_CONF_END
105 };
106
107 #include "intel_batchbuffer.h"
108 #include "intel_buffers.h"
109 #include "brw_bufmgr.h"
110 #include "intel_fbo.h"
111 #include "intel_mipmap_tree.h"
112 #include "intel_screen.h"
113 #include "intel_tex.h"
114 #include "intel_image.h"
115
116 #include "brw_context.h"
117
118 #include "i915_drm.h"
119
120 /**
121 * For debugging purposes, this returns a time in seconds.
122 */
123 double
124 get_time(void)
125 {
126 struct timespec tp;
127
128 clock_gettime(CLOCK_MONOTONIC, &tp);
129
130 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
131 }
132
133 static const __DRItexBufferExtension intelTexBufferExtension = {
134 .base = { __DRI_TEX_BUFFER, 3 },
135
136 .setTexBuffer = intelSetTexBuffer,
137 .setTexBuffer2 = intelSetTexBuffer2,
138 .releaseTexBuffer = NULL,
139 };
140
141 static void
142 intel_dri2_flush_with_flags(__DRIcontext *cPriv,
143 __DRIdrawable *dPriv,
144 unsigned flags,
145 enum __DRI2throttleReason reason)
146 {
147 struct brw_context *brw = cPriv->driverPrivate;
148
149 if (!brw)
150 return;
151
152 struct gl_context *ctx = &brw->ctx;
153
154 FLUSH_VERTICES(ctx, 0);
155
156 if (flags & __DRI2_FLUSH_DRAWABLE)
157 intel_resolve_for_dri2_flush(brw, dPriv);
158
159 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
160 brw->need_swap_throttle = true;
161 if (reason == __DRI2_THROTTLE_FLUSHFRONT)
162 brw->need_flush_throttle = true;
163
164 intel_batchbuffer_flush(brw);
165 }
166
167 /**
168 * Provides compatibility with loaders that only support the older (version
169 * 1-3) flush interface.
170 *
171 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
172 */
173 static void
174 intel_dri2_flush(__DRIdrawable *drawable)
175 {
176 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
177 __DRI2_FLUSH_DRAWABLE,
178 __DRI2_THROTTLE_SWAPBUFFER);
179 }
180
181 static const struct __DRI2flushExtensionRec intelFlushExtension = {
182 .base = { __DRI2_FLUSH, 4 },
183
184 .flush = intel_dri2_flush,
185 .invalidate = dri2InvalidateDrawable,
186 .flush_with_flags = intel_dri2_flush_with_flags,
187 };
188
189 static struct intel_image_format intel_image_formats[] = {
190 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
191 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
192
193 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
194 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
195
196 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
197 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
198
199 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
200 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
201
202 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
203 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
204
205 { __DRI_IMAGE_FOURCC_ARGB1555, __DRI_IMAGE_COMPONENTS_RGBA, 1,
206 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555, 2 } } },
207
208 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
209 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
210
211 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
212 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
213
214 { __DRI_IMAGE_FOURCC_R16, __DRI_IMAGE_COMPONENTS_R, 1,
215 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 1 }, } },
216
217 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
218 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
219
220 { __DRI_IMAGE_FOURCC_GR1616, __DRI_IMAGE_COMPONENTS_RG, 1,
221 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616, 2 }, } },
222
223 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
224 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
225 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
226 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
227
228 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
229 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
230 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
231 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
232
233 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
234 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
235 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
236 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
237
238 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
239 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
240 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
241 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
242
243 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
244 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
245 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
246 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
247
248 { __DRI_IMAGE_FOURCC_YVU410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
249 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
250 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
251 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
252
253 { __DRI_IMAGE_FOURCC_YVU411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
254 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
255 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
256 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
257
258 { __DRI_IMAGE_FOURCC_YVU420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
259 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
260 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
261 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
262
263 { __DRI_IMAGE_FOURCC_YVU422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
264 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
265 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
266 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
267
268 { __DRI_IMAGE_FOURCC_YVU444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
269 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
270 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
271 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
272
273 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
274 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
275 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
276
277 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
278 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
279 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
280
281 /* For YUYV buffers, we set up two overlapping DRI images and treat
282 * them as planar buffers in the compositors. Plane 0 is GR88 and
283 * samples YU or YV pairs and places Y into the R component, while
284 * plane 1 is ARGB and samples YUYV clusters and places pairs and
285 * places U into the G component and V into A. This lets the
286 * texture sampler interpolate the Y components correctly when
287 * sampling from plane 0, and interpolate U and V correctly when
288 * sampling from plane 1. */
289 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
290 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
291 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } }
292 };
293
294 static const struct {
295 uint32_t tiling;
296 uint64_t modifier;
297 unsigned height_align;
298 } tiling_modifier_map[] = {
299 { .tiling = I915_TILING_NONE, .modifier = DRM_FORMAT_MOD_LINEAR,
300 .height_align = 1 },
301 { .tiling = I915_TILING_X, .modifier = I915_FORMAT_MOD_X_TILED,
302 .height_align = 8 },
303 { .tiling = I915_TILING_Y, .modifier = I915_FORMAT_MOD_Y_TILED,
304 .height_align = 32 },
305 };
306
307 static uint32_t
308 modifier_to_tiling(uint64_t modifier)
309 {
310 int i;
311
312 for (i = 0; i < ARRAY_SIZE(tiling_modifier_map); i++) {
313 if (tiling_modifier_map[i].modifier == modifier)
314 return tiling_modifier_map[i].tiling;
315 }
316
317 unreachable("modifier_to_tiling should only receive known modifiers");
318 }
319
320 static uint64_t
321 tiling_to_modifier(uint32_t tiling)
322 {
323 int i;
324
325 for (i = 0; i < ARRAY_SIZE(tiling_modifier_map); i++) {
326 if (tiling_modifier_map[i].tiling == tiling)
327 return tiling_modifier_map[i].modifier;
328 }
329
330 unreachable("tiling_to_modifier received unknown tiling mode");
331 }
332
333 static unsigned
334 get_tiled_height(uint64_t modifier, unsigned height)
335 {
336 int i;
337
338 for (i = 0; i < ARRAY_SIZE(tiling_modifier_map); i++) {
339 if (tiling_modifier_map[i].modifier == modifier)
340 return ALIGN(height, tiling_modifier_map[i].height_align);
341 }
342
343 unreachable("get_tiled_height received unknown tiling mode");
344 }
345
346 static void
347 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
348 {
349 uint32_t tiling, swizzle;
350 brw_bo_get_tiling(image->bo, &tiling, &swizzle);
351
352 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
353 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
354 func, image->offset);
355 }
356 }
357
358 static struct intel_image_format *
359 intel_image_format_lookup(int fourcc)
360 {
361 struct intel_image_format *f = NULL;
362
363 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
364 if (intel_image_formats[i].fourcc == fourcc) {
365 f = &intel_image_formats[i];
366 break;
367 }
368 }
369
370 return f;
371 }
372
373 static boolean intel_lookup_fourcc(int dri_format, int *fourcc)
374 {
375 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
376 if (intel_image_formats[i].planes[0].dri_format == dri_format) {
377 *fourcc = intel_image_formats[i].fourcc;
378 return true;
379 }
380 }
381 return false;
382 }
383
384 static __DRIimage *
385 intel_allocate_image(struct intel_screen *screen, int dri_format,
386 void *loaderPrivate)
387 {
388 __DRIimage *image;
389
390 image = calloc(1, sizeof *image);
391 if (image == NULL)
392 return NULL;
393
394 image->screen = screen;
395 image->dri_format = dri_format;
396 image->offset = 0;
397
398 image->format = driImageFormatToGLFormat(dri_format);
399 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
400 image->format == MESA_FORMAT_NONE) {
401 free(image);
402 return NULL;
403 }
404
405 image->internal_format = _mesa_get_format_base_format(image->format);
406 image->data = loaderPrivate;
407
408 return image;
409 }
410
411 /**
412 * Sets up a DRIImage structure to point to a slice out of a miptree.
413 */
414 static void
415 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
416 struct intel_mipmap_tree *mt, GLuint level,
417 GLuint zoffset)
418 {
419 intel_miptree_make_shareable(brw, mt);
420
421 intel_miptree_check_level_layer(mt, level, zoffset);
422
423 image->width = minify(mt->physical_width0, level - mt->first_level);
424 image->height = minify(mt->physical_height0, level - mt->first_level);
425 image->pitch = mt->pitch;
426
427 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
428 &image->tile_x,
429 &image->tile_y);
430
431 brw_bo_unreference(image->bo);
432 image->bo = mt->bo;
433 brw_bo_reference(mt->bo);
434 }
435
436 static __DRIimage *
437 intel_create_image_from_name(__DRIscreen *dri_screen,
438 int width, int height, int format,
439 int name, int pitch, void *loaderPrivate)
440 {
441 struct intel_screen *screen = dri_screen->driverPrivate;
442 __DRIimage *image;
443 int cpp;
444
445 image = intel_allocate_image(screen, format, loaderPrivate);
446 if (image == NULL)
447 return NULL;
448
449 if (image->format == MESA_FORMAT_NONE)
450 cpp = 1;
451 else
452 cpp = _mesa_get_format_bytes(image->format);
453
454 image->width = width;
455 image->height = height;
456 image->pitch = pitch * cpp;
457 image->bo = brw_bo_gem_create_from_name(screen->bufmgr, "image",
458 name);
459 if (!image->bo) {
460 free(image);
461 return NULL;
462 }
463 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
464
465 return image;
466 }
467
468 static __DRIimage *
469 intel_create_image_from_renderbuffer(__DRIcontext *context,
470 int renderbuffer, void *loaderPrivate)
471 {
472 __DRIimage *image;
473 struct brw_context *brw = context->driverPrivate;
474 struct gl_context *ctx = &brw->ctx;
475 struct gl_renderbuffer *rb;
476 struct intel_renderbuffer *irb;
477
478 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
479 if (!rb) {
480 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
481 return NULL;
482 }
483
484 irb = intel_renderbuffer(rb);
485 intel_miptree_make_shareable(brw, irb->mt);
486 image = calloc(1, sizeof *image);
487 if (image == NULL)
488 return NULL;
489
490 image->internal_format = rb->InternalFormat;
491 image->format = rb->Format;
492 image->modifier = tiling_to_modifier(irb->mt->tiling);
493 image->offset = 0;
494 image->data = loaderPrivate;
495 brw_bo_unreference(image->bo);
496 image->bo = irb->mt->bo;
497 brw_bo_reference(irb->mt->bo);
498 image->width = rb->Width;
499 image->height = rb->Height;
500 image->pitch = irb->mt->pitch;
501 image->dri_format = driGLFormatToImageFormat(image->format);
502 image->has_depthstencil = irb->mt->stencil_mt? true : false;
503
504 rb->NeedsFinishRenderTexture = true;
505 return image;
506 }
507
508 static __DRIimage *
509 intel_create_image_from_texture(__DRIcontext *context, int target,
510 unsigned texture, int zoffset,
511 int level,
512 unsigned *error,
513 void *loaderPrivate)
514 {
515 __DRIimage *image;
516 struct brw_context *brw = context->driverPrivate;
517 struct gl_texture_object *obj;
518 struct intel_texture_object *iobj;
519 GLuint face = 0;
520
521 obj = _mesa_lookup_texture(&brw->ctx, texture);
522 if (!obj || obj->Target != target) {
523 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
524 return NULL;
525 }
526
527 if (target == GL_TEXTURE_CUBE_MAP)
528 face = zoffset;
529
530 _mesa_test_texobj_completeness(&brw->ctx, obj);
531 iobj = intel_texture_object(obj);
532 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
533 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
534 return NULL;
535 }
536
537 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
538 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
539 return NULL;
540 }
541
542 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
543 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
544 return NULL;
545 }
546 image = calloc(1, sizeof *image);
547 if (image == NULL) {
548 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
549 return NULL;
550 }
551
552 image->internal_format = obj->Image[face][level]->InternalFormat;
553 image->format = obj->Image[face][level]->TexFormat;
554 image->modifier = tiling_to_modifier(iobj->mt->tiling);
555 image->data = loaderPrivate;
556 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
557 image->dri_format = driGLFormatToImageFormat(image->format);
558 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
559 if (image->dri_format == MESA_FORMAT_NONE) {
560 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
561 free(image);
562 return NULL;
563 }
564
565 *error = __DRI_IMAGE_ERROR_SUCCESS;
566 return image;
567 }
568
569 static void
570 intel_destroy_image(__DRIimage *image)
571 {
572 brw_bo_unreference(image->bo);
573 free(image);
574 }
575
576 enum modifier_priority {
577 MODIFIER_PRIORITY_INVALID = 0,
578 MODIFIER_PRIORITY_LINEAR,
579 MODIFIER_PRIORITY_X,
580 MODIFIER_PRIORITY_Y,
581 };
582
583 const uint64_t priority_to_modifier[] = {
584 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
585 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
586 [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
587 [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
588 };
589
590 static uint64_t
591 select_best_modifier(struct gen_device_info *devinfo,
592 const uint64_t *modifiers,
593 const unsigned count)
594 {
595 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
596
597 for (int i = 0; i < count; i++) {
598 switch (modifiers[i]) {
599 case I915_FORMAT_MOD_Y_TILED:
600 prio = MAX2(prio, MODIFIER_PRIORITY_Y);
601 break;
602 case I915_FORMAT_MOD_X_TILED:
603 prio = MAX2(prio, MODIFIER_PRIORITY_X);
604 break;
605 case DRM_FORMAT_MOD_LINEAR:
606 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
607 break;
608 case DRM_FORMAT_MOD_INVALID:
609 default:
610 break;
611 }
612 }
613
614 return priority_to_modifier[prio];
615 }
616
617 static __DRIimage *
618 intel_create_image_common(__DRIscreen *dri_screen,
619 int width, int height, int format,
620 unsigned int use,
621 const uint64_t *modifiers,
622 unsigned count,
623 void *loaderPrivate)
624 {
625 __DRIimage *image;
626 struct intel_screen *screen = dri_screen->driverPrivate;
627 uint32_t tiling;
628 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
629 unsigned tiled_height;
630 int cpp;
631
632 /* Callers of this may specify a modifier, or a dri usage, but not both. The
633 * newer modifier interface deprecates the older usage flags newer modifier
634 * interface deprecates the older usage flags.
635 */
636 assert(!(use && count));
637
638 if (use & __DRI_IMAGE_USE_CURSOR) {
639 if (width != 64 || height != 64)
640 return NULL;
641 modifier = DRM_FORMAT_MOD_LINEAR;
642 }
643
644 if (use & __DRI_IMAGE_USE_LINEAR)
645 modifier = DRM_FORMAT_MOD_LINEAR;
646
647 if (modifier == DRM_FORMAT_MOD_INVALID) {
648 if (modifiers) {
649 /* User requested specific modifiers */
650 modifier = select_best_modifier(&screen->devinfo, modifiers, count);
651 if (modifier == DRM_FORMAT_MOD_INVALID)
652 return NULL;
653 } else {
654 /* Historically, X-tiled was the default, and so lack of modifier means
655 * X-tiled.
656 */
657 modifier = I915_FORMAT_MOD_X_TILED;
658 }
659 }
660 tiling = modifier_to_tiling(modifier);
661 tiled_height = get_tiled_height(modifier, height);
662
663 image = intel_allocate_image(screen, format, loaderPrivate);
664 if (image == NULL)
665 return NULL;
666
667 cpp = _mesa_get_format_bytes(image->format);
668 image->bo = brw_bo_alloc_tiled(screen->bufmgr, "image",
669 width, tiled_height, cpp, tiling,
670 &image->pitch, 0);
671 if (image->bo == NULL) {
672 free(image);
673 return NULL;
674 }
675 image->width = width;
676 image->height = height;
677 image->modifier = modifier;
678
679 return image;
680 }
681
682 static __DRIimage *
683 intel_create_image(__DRIscreen *dri_screen,
684 int width, int height, int format,
685 unsigned int use,
686 void *loaderPrivate)
687 {
688 return intel_create_image_common(dri_screen, width, height, format, use, NULL, 0,
689 loaderPrivate);
690 }
691
692 static __DRIimage *
693 intel_create_image_with_modifiers(__DRIscreen *dri_screen,
694 int width, int height, int format,
695 const uint64_t *modifiers,
696 const unsigned count,
697 void *loaderPrivate)
698 {
699 return intel_create_image_common(dri_screen, width, height, format, 0,
700 modifiers, count, loaderPrivate);
701 }
702
703 static GLboolean
704 intel_query_image(__DRIimage *image, int attrib, int *value)
705 {
706 switch (attrib) {
707 case __DRI_IMAGE_ATTRIB_STRIDE:
708 *value = image->pitch;
709 return true;
710 case __DRI_IMAGE_ATTRIB_HANDLE:
711 *value = image->bo->gem_handle;
712 return true;
713 case __DRI_IMAGE_ATTRIB_NAME:
714 return !brw_bo_flink(image->bo, (uint32_t *) value);
715 case __DRI_IMAGE_ATTRIB_FORMAT:
716 *value = image->dri_format;
717 return true;
718 case __DRI_IMAGE_ATTRIB_WIDTH:
719 *value = image->width;
720 return true;
721 case __DRI_IMAGE_ATTRIB_HEIGHT:
722 *value = image->height;
723 return true;
724 case __DRI_IMAGE_ATTRIB_COMPONENTS:
725 if (image->planar_format == NULL)
726 return false;
727 *value = image->planar_format->components;
728 return true;
729 case __DRI_IMAGE_ATTRIB_FD:
730 return !brw_bo_gem_export_to_prime(image->bo, value);
731 case __DRI_IMAGE_ATTRIB_FOURCC:
732 return intel_lookup_fourcc(image->dri_format, value);
733 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
734 *value = 1;
735 return true;
736 case __DRI_IMAGE_ATTRIB_OFFSET:
737 *value = image->offset;
738 return true;
739 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
740 *value = (image->modifier & 0xffffffff);
741 return true;
742 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
743 *value = ((image->modifier >> 32) & 0xffffffff);
744 return true;
745
746 default:
747 return false;
748 }
749 }
750
751 static __DRIimage *
752 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
753 {
754 __DRIimage *image;
755
756 image = calloc(1, sizeof *image);
757 if (image == NULL)
758 return NULL;
759
760 brw_bo_reference(orig_image->bo);
761 image->bo = orig_image->bo;
762 image->internal_format = orig_image->internal_format;
763 image->planar_format = orig_image->planar_format;
764 image->dri_format = orig_image->dri_format;
765 image->format = orig_image->format;
766 image->modifier = orig_image->modifier;
767 image->offset = orig_image->offset;
768 image->width = orig_image->width;
769 image->height = orig_image->height;
770 image->pitch = orig_image->pitch;
771 image->tile_x = orig_image->tile_x;
772 image->tile_y = orig_image->tile_y;
773 image->has_depthstencil = orig_image->has_depthstencil;
774 image->data = loaderPrivate;
775
776 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
777 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
778
779 return image;
780 }
781
782 static GLboolean
783 intel_validate_usage(__DRIimage *image, unsigned int use)
784 {
785 if (use & __DRI_IMAGE_USE_CURSOR) {
786 if (image->width != 64 || image->height != 64)
787 return GL_FALSE;
788 }
789
790 return GL_TRUE;
791 }
792
793 static __DRIimage *
794 intel_create_image_from_names(__DRIscreen *dri_screen,
795 int width, int height, int fourcc,
796 int *names, int num_names,
797 int *strides, int *offsets,
798 void *loaderPrivate)
799 {
800 struct intel_image_format *f = NULL;
801 __DRIimage *image;
802 int i, index;
803
804 if (dri_screen == NULL || names == NULL || num_names != 1)
805 return NULL;
806
807 f = intel_image_format_lookup(fourcc);
808 if (f == NULL)
809 return NULL;
810
811 image = intel_create_image_from_name(dri_screen, width, height,
812 __DRI_IMAGE_FORMAT_NONE,
813 names[0], strides[0],
814 loaderPrivate);
815
816 if (image == NULL)
817 return NULL;
818
819 image->planar_format = f;
820 for (i = 0; i < f->nplanes; i++) {
821 index = f->planes[i].buffer_index;
822 image->offsets[index] = offsets[index];
823 image->strides[index] = strides[index];
824 }
825
826 return image;
827 }
828
829 static __DRIimage *
830 intel_create_image_from_fds(__DRIscreen *dri_screen,
831 int width, int height, int fourcc,
832 int *fds, int num_fds, int *strides, int *offsets,
833 void *loaderPrivate)
834 {
835 struct intel_screen *screen = dri_screen->driverPrivate;
836 struct intel_image_format *f;
837 __DRIimage *image;
838 unsigned tiled_height;
839 int i, index;
840
841 if (fds == NULL || num_fds < 1)
842 return NULL;
843
844 /* We only support all planes from the same bo */
845 for (i = 0; i < num_fds; i++)
846 if (fds[0] != fds[i])
847 return NULL;
848
849 f = intel_image_format_lookup(fourcc);
850 if (f == NULL)
851 return NULL;
852
853 if (f->nplanes == 1)
854 image = intel_allocate_image(screen, f->planes[0].dri_format,
855 loaderPrivate);
856 else
857 image = intel_allocate_image(screen, __DRI_IMAGE_FORMAT_NONE,
858 loaderPrivate);
859
860 if (image == NULL)
861 return NULL;
862
863 image->width = width;
864 image->height = height;
865 image->pitch = strides[0];
866
867 image->planar_format = f;
868
869 image->bo = brw_bo_gem_create_from_prime(screen->bufmgr, fds[0]);
870 if (image->bo == NULL) {
871 free(image);
872 return NULL;
873 }
874
875 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
876 tiled_height = get_tiled_height(image->modifier, height);
877
878 int size = 0;
879 for (i = 0; i < f->nplanes; i++) {
880 index = f->planes[i].buffer_index;
881 image->offsets[index] = offsets[index];
882 image->strides[index] = strides[index];
883
884 const int plane_height = tiled_height >> f->planes[i].height_shift;
885 const int end = offsets[index] + plane_height * strides[index];
886 if (size < end)
887 size = end;
888 }
889
890 /* Check that the requested image actually fits within the BO. 'size'
891 * is already relative to the offsets, so we don't need to add that. */
892 if (image->bo->size == 0) {
893 image->bo->size = size;
894 } else if (size > image->bo->size) {
895 brw_bo_unreference(image->bo);
896 free(image);
897 return NULL;
898 }
899
900 if (f->nplanes == 1) {
901 image->offset = image->offsets[0];
902 intel_image_warn_if_unaligned(image, __func__);
903 }
904
905 return image;
906 }
907
908 static __DRIimage *
909 intel_create_image_from_dma_bufs(__DRIscreen *dri_screen,
910 int width, int height, int fourcc,
911 int *fds, int num_fds,
912 int *strides, int *offsets,
913 enum __DRIYUVColorSpace yuv_color_space,
914 enum __DRISampleRange sample_range,
915 enum __DRIChromaSiting horizontal_siting,
916 enum __DRIChromaSiting vertical_siting,
917 unsigned *error,
918 void *loaderPrivate)
919 {
920 __DRIimage *image;
921 struct intel_image_format *f = intel_image_format_lookup(fourcc);
922
923 if (!f) {
924 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
925 return NULL;
926 }
927
928 image = intel_create_image_from_fds(dri_screen, width, height, fourcc, fds,
929 num_fds, strides, offsets,
930 loaderPrivate);
931
932 /*
933 * Invalid parameters and any inconsistencies between are assumed to be
934 * checked by the caller. Therefore besides unsupported formats one can fail
935 * only in allocation.
936 */
937 if (!image) {
938 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
939 return NULL;
940 }
941
942 image->dma_buf_imported = true;
943 image->yuv_color_space = yuv_color_space;
944 image->sample_range = sample_range;
945 image->horizontal_siting = horizontal_siting;
946 image->vertical_siting = vertical_siting;
947
948 *error = __DRI_IMAGE_ERROR_SUCCESS;
949 return image;
950 }
951
952 static __DRIimage *
953 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
954 {
955 int width, height, offset, stride, dri_format, index;
956 struct intel_image_format *f;
957 __DRIimage *image;
958
959 if (parent == NULL || parent->planar_format == NULL)
960 return NULL;
961
962 f = parent->planar_format;
963
964 if (plane >= f->nplanes)
965 return NULL;
966
967 width = parent->width >> f->planes[plane].width_shift;
968 height = parent->height >> f->planes[plane].height_shift;
969 dri_format = f->planes[plane].dri_format;
970 index = f->planes[plane].buffer_index;
971 offset = parent->offsets[index];
972 stride = parent->strides[index];
973
974 image = intel_allocate_image(parent->screen, dri_format, loaderPrivate);
975 if (image == NULL)
976 return NULL;
977
978 if (offset + height * stride > parent->bo->size) {
979 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
980 free(image);
981 return NULL;
982 }
983
984 image->bo = parent->bo;
985 brw_bo_reference(parent->bo);
986 image->modifier = parent->modifier;
987
988 image->width = width;
989 image->height = height;
990 image->pitch = stride;
991 image->offset = offset;
992
993 intel_image_warn_if_unaligned(image, __func__);
994
995 return image;
996 }
997
998 static const __DRIimageExtension intelImageExtension = {
999 .base = { __DRI_IMAGE, 14 },
1000
1001 .createImageFromName = intel_create_image_from_name,
1002 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
1003 .destroyImage = intel_destroy_image,
1004 .createImage = intel_create_image,
1005 .queryImage = intel_query_image,
1006 .dupImage = intel_dup_image,
1007 .validateUsage = intel_validate_usage,
1008 .createImageFromNames = intel_create_image_from_names,
1009 .fromPlanar = intel_from_planar,
1010 .createImageFromTexture = intel_create_image_from_texture,
1011 .createImageFromFds = intel_create_image_from_fds,
1012 .createImageFromDmaBufs = intel_create_image_from_dma_bufs,
1013 .blitImage = NULL,
1014 .getCapabilities = NULL,
1015 .mapImage = NULL,
1016 .unmapImage = NULL,
1017 .createImageWithModifiers = intel_create_image_with_modifiers,
1018 };
1019
1020 static uint64_t
1021 get_aperture_size(int fd)
1022 {
1023 struct drm_i915_gem_get_aperture aperture;
1024
1025 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture) != 0)
1026 return 0;
1027
1028 return aperture.aper_size;
1029 }
1030
1031 static int
1032 brw_query_renderer_integer(__DRIscreen *dri_screen,
1033 int param, unsigned int *value)
1034 {
1035 const struct intel_screen *const screen =
1036 (struct intel_screen *) dri_screen->driverPrivate;
1037
1038 switch (param) {
1039 case __DRI2_RENDERER_VENDOR_ID:
1040 value[0] = 0x8086;
1041 return 0;
1042 case __DRI2_RENDERER_DEVICE_ID:
1043 value[0] = screen->deviceID;
1044 return 0;
1045 case __DRI2_RENDERER_ACCELERATED:
1046 value[0] = 1;
1047 return 0;
1048 case __DRI2_RENDERER_VIDEO_MEMORY: {
1049 /* Once a batch uses more than 75% of the maximum mappable size, we
1050 * assume that there's some fragmentation, and we start doing extra
1051 * flushing, etc. That's the big cliff apps will care about.
1052 */
1053 const unsigned gpu_mappable_megabytes =
1054 screen->aperture_threshold / (1024 * 1024);
1055
1056 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
1057 const long system_page_size = sysconf(_SC_PAGE_SIZE);
1058
1059 if (system_memory_pages <= 0 || system_page_size <= 0)
1060 return -1;
1061
1062 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
1063 * (uint64_t) system_page_size;
1064
1065 const unsigned system_memory_megabytes =
1066 (unsigned) (system_memory_bytes / (1024 * 1024));
1067
1068 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
1069 return 0;
1070 }
1071 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
1072 value[0] = 1;
1073 return 0;
1074 case __DRI2_RENDERER_HAS_TEXTURE_3D:
1075 value[0] = 1;
1076 return 0;
1077 default:
1078 return driQueryRendererIntegerCommon(dri_screen, param, value);
1079 }
1080
1081 return -1;
1082 }
1083
1084 static int
1085 brw_query_renderer_string(__DRIscreen *dri_screen,
1086 int param, const char **value)
1087 {
1088 const struct intel_screen *screen =
1089 (struct intel_screen *) dri_screen->driverPrivate;
1090
1091 switch (param) {
1092 case __DRI2_RENDERER_VENDOR_ID:
1093 value[0] = brw_vendor_string;
1094 return 0;
1095 case __DRI2_RENDERER_DEVICE_ID:
1096 value[0] = brw_get_renderer_string(screen);
1097 return 0;
1098 default:
1099 break;
1100 }
1101
1102 return -1;
1103 }
1104
1105 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
1106 .base = { __DRI2_RENDERER_QUERY, 1 },
1107
1108 .queryInteger = brw_query_renderer_integer,
1109 .queryString = brw_query_renderer_string
1110 };
1111
1112 static const __DRIrobustnessExtension dri2Robustness = {
1113 .base = { __DRI2_ROBUSTNESS, 1 }
1114 };
1115
1116 static const __DRIextension *screenExtensions[] = {
1117 &intelTexBufferExtension.base,
1118 &intelFenceExtension.base,
1119 &intelFlushExtension.base,
1120 &intelImageExtension.base,
1121 &intelRendererQueryExtension.base,
1122 &dri2ConfigQueryExtension.base,
1123 NULL
1124 };
1125
1126 static const __DRIextension *intelRobustScreenExtensions[] = {
1127 &intelTexBufferExtension.base,
1128 &intelFenceExtension.base,
1129 &intelFlushExtension.base,
1130 &intelImageExtension.base,
1131 &intelRendererQueryExtension.base,
1132 &dri2ConfigQueryExtension.base,
1133 &dri2Robustness.base,
1134 NULL
1135 };
1136
1137 static int
1138 intel_get_param(struct intel_screen *screen, int param, int *value)
1139 {
1140 int ret = 0;
1141 struct drm_i915_getparam gp;
1142
1143 memset(&gp, 0, sizeof(gp));
1144 gp.param = param;
1145 gp.value = value;
1146
1147 if (drmIoctl(screen->driScrnPriv->fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1) {
1148 ret = -errno;
1149 if (ret != -EINVAL)
1150 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
1151 }
1152
1153 return ret;
1154 }
1155
1156 static bool
1157 intel_get_boolean(struct intel_screen *screen, int param)
1158 {
1159 int value = 0;
1160 return (intel_get_param(screen, param, &value) == 0) && value;
1161 }
1162
1163 static int
1164 intel_get_integer(struct intel_screen *screen, int param)
1165 {
1166 int value = -1;
1167
1168 if (intel_get_param(screen, param, &value) == 0)
1169 return value;
1170
1171 return -1;
1172 }
1173
1174 static void
1175 intelDestroyScreen(__DRIscreen * sPriv)
1176 {
1177 struct intel_screen *screen = sPriv->driverPrivate;
1178
1179 brw_bufmgr_destroy(screen->bufmgr);
1180 driDestroyOptionInfo(&screen->optionCache);
1181
1182 ralloc_free(screen);
1183 sPriv->driverPrivate = NULL;
1184 }
1185
1186
1187 /**
1188 * Create a gl_framebuffer and attach it to __DRIdrawable::driverPrivate.
1189 *
1190 *_This implements driDriverAPI::createNewDrawable, which the DRI layer calls
1191 * when creating a EGLSurface, GLXDrawable, or GLXPixmap. Despite the name,
1192 * this does not allocate GPU memory.
1193 */
1194 static GLboolean
1195 intelCreateBuffer(__DRIscreen *dri_screen,
1196 __DRIdrawable * driDrawPriv,
1197 const struct gl_config * mesaVis, GLboolean isPixmap)
1198 {
1199 struct intel_renderbuffer *rb;
1200 struct intel_screen *screen = (struct intel_screen *)
1201 dri_screen->driverPrivate;
1202 mesa_format rgbFormat;
1203 unsigned num_samples =
1204 intel_quantize_num_samples(screen, mesaVis->samples);
1205
1206 if (isPixmap)
1207 return false;
1208
1209 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
1210 if (!fb)
1211 return false;
1212
1213 _mesa_initialize_window_framebuffer(fb, mesaVis);
1214
1215 if (screen->winsys_msaa_samples_override != -1) {
1216 num_samples = screen->winsys_msaa_samples_override;
1217 fb->Visual.samples = num_samples;
1218 }
1219
1220 if (mesaVis->redBits == 5) {
1221 rgbFormat = mesaVis->redMask == 0x1f ? MESA_FORMAT_R5G6B5_UNORM
1222 : MESA_FORMAT_B5G6R5_UNORM;
1223 } else if (mesaVis->sRGBCapable) {
1224 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1225 : MESA_FORMAT_B8G8R8A8_SRGB;
1226 } else if (mesaVis->alphaBits == 0) {
1227 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8X8_UNORM
1228 : MESA_FORMAT_B8G8R8X8_UNORM;
1229 } else {
1230 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1231 : MESA_FORMAT_B8G8R8A8_SRGB;
1232 fb->Visual.sRGBCapable = true;
1233 }
1234
1235 /* setup the hardware-based renderbuffers */
1236 rb = intel_create_winsys_renderbuffer(rgbFormat, num_samples);
1237 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
1238
1239 if (mesaVis->doubleBufferMode) {
1240 rb = intel_create_winsys_renderbuffer(rgbFormat, num_samples);
1241 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
1242 }
1243
1244 /*
1245 * Assert here that the gl_config has an expected depth/stencil bit
1246 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1247 * which constructs the advertised configs.)
1248 */
1249 if (mesaVis->depthBits == 24) {
1250 assert(mesaVis->stencilBits == 8);
1251
1252 if (screen->devinfo.has_hiz_and_separate_stencil) {
1253 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_X8_UINT,
1254 num_samples);
1255 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1256 rb = intel_create_private_renderbuffer(MESA_FORMAT_S_UINT8,
1257 num_samples);
1258 _mesa_attach_and_own_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
1259 } else {
1260 /*
1261 * Use combined depth/stencil. Note that the renderbuffer is
1262 * attached to two attachment points.
1263 */
1264 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT,
1265 num_samples);
1266 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1267 _mesa_attach_and_reference_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
1268 }
1269 }
1270 else if (mesaVis->depthBits == 16) {
1271 assert(mesaVis->stencilBits == 0);
1272 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16,
1273 num_samples);
1274 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1275 }
1276 else {
1277 assert(mesaVis->depthBits == 0);
1278 assert(mesaVis->stencilBits == 0);
1279 }
1280
1281 /* now add any/all software-based renderbuffers we may need */
1282 _swrast_add_soft_renderbuffers(fb,
1283 false, /* never sw color */
1284 false, /* never sw depth */
1285 false, /* never sw stencil */
1286 mesaVis->accumRedBits > 0,
1287 false, /* never sw alpha */
1288 false /* never sw aux */ );
1289 driDrawPriv->driverPrivate = fb;
1290
1291 return true;
1292 }
1293
1294 static void
1295 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1296 {
1297 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1298
1299 _mesa_reference_framebuffer(&fb, NULL);
1300 }
1301
1302 static void
1303 intel_detect_sseu(struct intel_screen *screen)
1304 {
1305 assert(screen->devinfo.gen >= 8);
1306 int ret;
1307
1308 screen->subslice_total = -1;
1309 screen->eu_total = -1;
1310
1311 ret = intel_get_param(screen, I915_PARAM_SUBSLICE_TOTAL,
1312 &screen->subslice_total);
1313 if (ret < 0 && ret != -EINVAL)
1314 goto err_out;
1315
1316 ret = intel_get_param(screen,
1317 I915_PARAM_EU_TOTAL, &screen->eu_total);
1318 if (ret < 0 && ret != -EINVAL)
1319 goto err_out;
1320
1321 /* Without this information, we cannot get the right Braswell brandstrings,
1322 * and we have to use conservative numbers for GPGPU on many platforms, but
1323 * otherwise, things will just work.
1324 */
1325 if (screen->subslice_total < 1 || screen->eu_total < 1)
1326 _mesa_warning(NULL,
1327 "Kernel 4.1 required to properly query GPU properties.\n");
1328
1329 return;
1330
1331 err_out:
1332 screen->subslice_total = -1;
1333 screen->eu_total = -1;
1334 _mesa_warning(NULL, "Failed to query GPU properties (%s).\n", strerror(-ret));
1335 }
1336
1337 static bool
1338 intel_init_bufmgr(struct intel_screen *screen)
1339 {
1340 __DRIscreen *dri_screen = screen->driScrnPriv;
1341
1342 if (getenv("INTEL_NO_HW") != NULL)
1343 screen->no_hw = true;
1344
1345 screen->bufmgr = brw_bufmgr_init(&screen->devinfo, dri_screen->fd, BATCH_SZ);
1346 if (screen->bufmgr == NULL) {
1347 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1348 __func__, __LINE__);
1349 return false;
1350 }
1351
1352 if (!intel_get_boolean(screen, I915_PARAM_HAS_WAIT_TIMEOUT)) {
1353 fprintf(stderr, "[%s: %u] Kernel 3.6 required.\n", __func__, __LINE__);
1354 return false;
1355 }
1356
1357 return true;
1358 }
1359
1360 static bool
1361 intel_detect_swizzling(struct intel_screen *screen)
1362 {
1363 struct brw_bo *buffer;
1364 unsigned flags = 0;
1365 uint32_t aligned_pitch;
1366 uint32_t tiling = I915_TILING_X;
1367 uint32_t swizzle_mode = 0;
1368
1369 buffer = brw_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1370 64, 64, 4, tiling, &aligned_pitch, flags);
1371 if (buffer == NULL)
1372 return false;
1373
1374 brw_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1375 brw_bo_unreference(buffer);
1376
1377 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1378 return false;
1379 else
1380 return true;
1381 }
1382
1383 static int
1384 intel_detect_timestamp(struct intel_screen *screen)
1385 {
1386 uint64_t dummy = 0, last = 0;
1387 int upper, lower, loops;
1388
1389 /* On 64bit systems, some old kernels trigger a hw bug resulting in the
1390 * TIMESTAMP register being shifted and the low 32bits always zero.
1391 *
1392 * More recent kernels offer an interface to read the full 36bits
1393 * everywhere.
1394 */
1395 if (brw_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
1396 return 3;
1397
1398 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1399 * upper 32bits for a rapidly changing timestamp.
1400 */
1401 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &last))
1402 return 0;
1403
1404 upper = lower = 0;
1405 for (loops = 0; loops < 10; loops++) {
1406 /* The TIMESTAMP should change every 80ns, so several round trips
1407 * through the kernel should be enough to advance it.
1408 */
1409 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
1410 return 0;
1411
1412 upper += (dummy >> 32) != (last >> 32);
1413 if (upper > 1) /* beware 32bit counter overflow */
1414 return 2; /* upper dword holds the low 32bits of the timestamp */
1415
1416 lower += (dummy & 0xffffffff) != (last & 0xffffffff);
1417 if (lower > 1)
1418 return 1; /* timestamp is unshifted */
1419
1420 last = dummy;
1421 }
1422
1423 /* No advancement? No timestamp! */
1424 return 0;
1425 }
1426
1427 /**
1428 * Test if we can use MI_LOAD_REGISTER_MEM from an untrusted batchbuffer.
1429 *
1430 * Some combinations of hardware and kernel versions allow this feature,
1431 * while others don't. Instead of trying to enumerate every case, just
1432 * try and write a register and see if works.
1433 */
1434 static bool
1435 intel_detect_pipelined_register(struct intel_screen *screen,
1436 int reg, uint32_t expected_value, bool reset)
1437 {
1438 if (screen->no_hw)
1439 return false;
1440
1441 struct brw_bo *results, *bo;
1442 uint32_t *batch;
1443 uint32_t offset = 0;
1444 void *map;
1445 bool success = false;
1446
1447 /* Create a zero'ed temporary buffer for reading our results */
1448 results = brw_bo_alloc(screen->bufmgr, "registers", 4096, 0);
1449 if (results == NULL)
1450 goto err;
1451
1452 bo = brw_bo_alloc(screen->bufmgr, "batchbuffer", 4096, 0);
1453 if (bo == NULL)
1454 goto err_results;
1455
1456 map = brw_bo_map(NULL, bo, MAP_WRITE);
1457 if (!map)
1458 goto err_batch;
1459
1460 batch = map;
1461
1462 /* Write the register. */
1463 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1464 *batch++ = reg;
1465 *batch++ = expected_value;
1466
1467 /* Save the register's value back to the buffer. */
1468 *batch++ = MI_STORE_REGISTER_MEM | (3 - 2);
1469 *batch++ = reg;
1470 struct drm_i915_gem_relocation_entry reloc = {
1471 .offset = (char *) batch - (char *) map,
1472 .delta = offset * sizeof(uint32_t),
1473 .target_handle = results->gem_handle,
1474 .read_domains = I915_GEM_DOMAIN_INSTRUCTION,
1475 .write_domain = I915_GEM_DOMAIN_INSTRUCTION,
1476 };
1477 *batch++ = reloc.presumed_offset + reloc.delta;
1478
1479 /* And afterwards clear the register */
1480 if (reset) {
1481 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1482 *batch++ = reg;
1483 *batch++ = 0;
1484 }
1485
1486 *batch++ = MI_BATCH_BUFFER_END;
1487
1488 struct drm_i915_gem_exec_object2 exec_objects[2] = {
1489 {
1490 .handle = results->gem_handle,
1491 },
1492 {
1493 .handle = bo->gem_handle,
1494 .relocation_count = 1,
1495 .relocs_ptr = (uintptr_t) &reloc,
1496 }
1497 };
1498
1499 struct drm_i915_gem_execbuffer2 execbuf = {
1500 .buffers_ptr = (uintptr_t) exec_objects,
1501 .buffer_count = 2,
1502 .batch_len = ALIGN((char *) batch - (char *) map, 8),
1503 .flags = I915_EXEC_RENDER,
1504 };
1505
1506 /* Don't bother with error checking - if the execbuf fails, the
1507 * value won't be written and we'll just report that there's no access.
1508 */
1509 __DRIscreen *dri_screen = screen->driScrnPriv;
1510 drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
1511
1512 /* Check whether the value got written. */
1513 void *results_map = brw_bo_map(NULL, results, MAP_READ);
1514 if (results_map) {
1515 success = *((uint32_t *)results_map + offset) == expected_value;
1516 brw_bo_unmap(results);
1517 }
1518
1519 err_batch:
1520 brw_bo_unreference(bo);
1521 err_results:
1522 brw_bo_unreference(results);
1523 err:
1524 return success;
1525 }
1526
1527 static bool
1528 intel_detect_pipelined_so(struct intel_screen *screen)
1529 {
1530 const struct gen_device_info *devinfo = &screen->devinfo;
1531
1532 /* Supposedly, Broadwell just works. */
1533 if (devinfo->gen >= 8)
1534 return true;
1535
1536 if (devinfo->gen <= 6)
1537 return false;
1538
1539 /* See the big explanation about command parser versions below */
1540 if (screen->cmd_parser_version >= (devinfo->is_haswell ? 7 : 2))
1541 return true;
1542
1543 /* We use SO_WRITE_OFFSET0 since you're supposed to write it (unlike the
1544 * statistics registers), and we already reset it to zero before using it.
1545 */
1546 return intel_detect_pipelined_register(screen,
1547 GEN7_SO_WRITE_OFFSET(0),
1548 0x1337d0d0,
1549 false);
1550 }
1551
1552 /**
1553 * Return array of MSAA modes supported by the hardware. The array is
1554 * zero-terminated and sorted in decreasing order.
1555 */
1556 const int*
1557 intel_supported_msaa_modes(const struct intel_screen *screen)
1558 {
1559 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
1560 static const int gen8_modes[] = {8, 4, 2, 0, -1};
1561 static const int gen7_modes[] = {8, 4, 0, -1};
1562 static const int gen6_modes[] = {4, 0, -1};
1563 static const int gen4_modes[] = {0, -1};
1564
1565 if (screen->devinfo.gen >= 9) {
1566 return gen9_modes;
1567 } else if (screen->devinfo.gen >= 8) {
1568 return gen8_modes;
1569 } else if (screen->devinfo.gen >= 7) {
1570 return gen7_modes;
1571 } else if (screen->devinfo.gen == 6) {
1572 return gen6_modes;
1573 } else {
1574 return gen4_modes;
1575 }
1576 }
1577
1578 static __DRIconfig**
1579 intel_screen_make_configs(__DRIscreen *dri_screen)
1580 {
1581 static const mesa_format formats[] = {
1582 MESA_FORMAT_B5G6R5_UNORM,
1583 MESA_FORMAT_B8G8R8A8_UNORM,
1584 MESA_FORMAT_B8G8R8X8_UNORM
1585 };
1586
1587 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1588 static const GLenum back_buffer_modes[] = {
1589 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1590 };
1591
1592 static const uint8_t singlesample_samples[1] = {0};
1593 static const uint8_t multisample_samples[2] = {4, 8};
1594
1595 struct intel_screen *screen = dri_screen->driverPrivate;
1596 const struct gen_device_info *devinfo = &screen->devinfo;
1597 uint8_t depth_bits[4], stencil_bits[4];
1598 __DRIconfig **configs = NULL;
1599
1600 /* Generate singlesample configs without accumulation buffer. */
1601 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1602 __DRIconfig **new_configs;
1603 int num_depth_stencil_bits = 2;
1604
1605 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1606 * buffer that has a different number of bits per pixel than the color
1607 * buffer, gen >= 6 supports this.
1608 */
1609 depth_bits[0] = 0;
1610 stencil_bits[0] = 0;
1611
1612 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1613 depth_bits[1] = 16;
1614 stencil_bits[1] = 0;
1615 if (devinfo->gen >= 6) {
1616 depth_bits[2] = 24;
1617 stencil_bits[2] = 8;
1618 num_depth_stencil_bits = 3;
1619 }
1620 } else {
1621 depth_bits[1] = 24;
1622 stencil_bits[1] = 8;
1623 }
1624
1625 new_configs = driCreateConfigs(formats[i],
1626 depth_bits,
1627 stencil_bits,
1628 num_depth_stencil_bits,
1629 back_buffer_modes, 2,
1630 singlesample_samples, 1,
1631 false, false);
1632 configs = driConcatConfigs(configs, new_configs);
1633 }
1634
1635 /* Generate the minimum possible set of configs that include an
1636 * accumulation buffer.
1637 */
1638 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1639 __DRIconfig **new_configs;
1640
1641 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1642 depth_bits[0] = 16;
1643 stencil_bits[0] = 0;
1644 } else {
1645 depth_bits[0] = 24;
1646 stencil_bits[0] = 8;
1647 }
1648
1649 new_configs = driCreateConfigs(formats[i],
1650 depth_bits, stencil_bits, 1,
1651 back_buffer_modes, 1,
1652 singlesample_samples, 1,
1653 true, false);
1654 configs = driConcatConfigs(configs, new_configs);
1655 }
1656
1657 /* Generate multisample configs.
1658 *
1659 * This loop breaks early, and hence is a no-op, on gen < 6.
1660 *
1661 * Multisample configs must follow the singlesample configs in order to
1662 * work around an X server bug present in 1.12. The X server chooses to
1663 * associate the first listed RGBA888-Z24S8 config, regardless of its
1664 * sample count, with the 32-bit depth visual used for compositing.
1665 *
1666 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1667 * supported. Singlebuffer configs are not supported because no one wants
1668 * them.
1669 */
1670 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1671 if (devinfo->gen < 6)
1672 break;
1673
1674 __DRIconfig **new_configs;
1675 const int num_depth_stencil_bits = 2;
1676 int num_msaa_modes = 0;
1677
1678 depth_bits[0] = 0;
1679 stencil_bits[0] = 0;
1680
1681 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1682 depth_bits[1] = 16;
1683 stencil_bits[1] = 0;
1684 } else {
1685 depth_bits[1] = 24;
1686 stencil_bits[1] = 8;
1687 }
1688
1689 if (devinfo->gen >= 7)
1690 num_msaa_modes = 2;
1691 else if (devinfo->gen == 6)
1692 num_msaa_modes = 1;
1693
1694 new_configs = driCreateConfigs(formats[i],
1695 depth_bits,
1696 stencil_bits,
1697 num_depth_stencil_bits,
1698 back_buffer_modes, 1,
1699 multisample_samples,
1700 num_msaa_modes,
1701 false, false);
1702 configs = driConcatConfigs(configs, new_configs);
1703 }
1704
1705 if (configs == NULL) {
1706 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1707 __LINE__);
1708 return NULL;
1709 }
1710
1711 return configs;
1712 }
1713
1714 static void
1715 set_max_gl_versions(struct intel_screen *screen)
1716 {
1717 __DRIscreen *dri_screen = screen->driScrnPriv;
1718 const bool has_astc = screen->devinfo.gen >= 9;
1719
1720 switch (screen->devinfo.gen) {
1721 case 9:
1722 case 8:
1723 dri_screen->max_gl_core_version = 45;
1724 dri_screen->max_gl_compat_version = 30;
1725 dri_screen->max_gl_es1_version = 11;
1726 dri_screen->max_gl_es2_version = has_astc ? 32 : 31;
1727 break;
1728 case 7:
1729 dri_screen->max_gl_core_version = 33;
1730 if (can_do_pipelined_register_writes(screen)) {
1731 dri_screen->max_gl_core_version = 42;
1732 if (screen->devinfo.is_haswell && can_do_compute_dispatch(screen))
1733 dri_screen->max_gl_core_version = 43;
1734 if (screen->devinfo.is_haswell && can_do_mi_math_and_lrr(screen))
1735 dri_screen->max_gl_core_version = 45;
1736 }
1737 dri_screen->max_gl_compat_version = 30;
1738 dri_screen->max_gl_es1_version = 11;
1739 dri_screen->max_gl_es2_version = screen->devinfo.is_haswell ? 31 : 30;
1740 break;
1741 case 6:
1742 dri_screen->max_gl_core_version = 33;
1743 dri_screen->max_gl_compat_version = 30;
1744 dri_screen->max_gl_es1_version = 11;
1745 dri_screen->max_gl_es2_version = 30;
1746 break;
1747 case 5:
1748 case 4:
1749 dri_screen->max_gl_core_version = 0;
1750 dri_screen->max_gl_compat_version = 21;
1751 dri_screen->max_gl_es1_version = 11;
1752 dri_screen->max_gl_es2_version = 20;
1753 break;
1754 default:
1755 unreachable("unrecognized intel_screen::gen");
1756 }
1757 }
1758
1759 /**
1760 * Return the revision (generally the revid field of the PCI header) of the
1761 * graphics device.
1762 *
1763 * XXX: This function is useful to keep around even if it is not currently in
1764 * use. It is necessary for new platforms and revision specific workarounds or
1765 * features. Please don't remove it so that we know it at least continues to
1766 * build.
1767 */
1768 static __attribute__((__unused__)) int
1769 brw_get_revision(int fd)
1770 {
1771 struct drm_i915_getparam gp;
1772 int revision;
1773 int ret;
1774
1775 memset(&gp, 0, sizeof(gp));
1776 gp.param = I915_PARAM_REVISION;
1777 gp.value = &revision;
1778
1779 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1780 if (ret)
1781 revision = -1;
1782
1783 return revision;
1784 }
1785
1786 static void
1787 shader_debug_log_mesa(void *data, const char *fmt, ...)
1788 {
1789 struct brw_context *brw = (struct brw_context *)data;
1790 va_list args;
1791
1792 va_start(args, fmt);
1793 GLuint msg_id = 0;
1794 _mesa_gl_vdebug(&brw->ctx, &msg_id,
1795 MESA_DEBUG_SOURCE_SHADER_COMPILER,
1796 MESA_DEBUG_TYPE_OTHER,
1797 MESA_DEBUG_SEVERITY_NOTIFICATION, fmt, args);
1798 va_end(args);
1799 }
1800
1801 static void
1802 shader_perf_log_mesa(void *data, const char *fmt, ...)
1803 {
1804 struct brw_context *brw = (struct brw_context *)data;
1805
1806 va_list args;
1807 va_start(args, fmt);
1808
1809 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
1810 va_list args_copy;
1811 va_copy(args_copy, args);
1812 vfprintf(stderr, fmt, args_copy);
1813 va_end(args_copy);
1814 }
1815
1816 if (brw->perf_debug) {
1817 GLuint msg_id = 0;
1818 _mesa_gl_vdebug(&brw->ctx, &msg_id,
1819 MESA_DEBUG_SOURCE_SHADER_COMPILER,
1820 MESA_DEBUG_TYPE_PERFORMANCE,
1821 MESA_DEBUG_SEVERITY_MEDIUM, fmt, args);
1822 }
1823 va_end(args);
1824 }
1825
1826 static int
1827 parse_devid_override(const char *devid_override)
1828 {
1829 static const struct {
1830 const char *name;
1831 int pci_id;
1832 } name_map[] = {
1833 { "brw", 0x2a02 },
1834 { "g4x", 0x2a42 },
1835 { "ilk", 0x0042 },
1836 { "snb", 0x0126 },
1837 { "ivb", 0x016a },
1838 { "hsw", 0x0d2e },
1839 { "byt", 0x0f33 },
1840 { "bdw", 0x162e },
1841 { "skl", 0x1912 },
1842 { "kbl", 0x5912 },
1843 };
1844
1845 for (unsigned i = 0; i < ARRAY_SIZE(name_map); i++) {
1846 if (!strcmp(name_map[i].name, devid_override))
1847 return name_map[i].pci_id;
1848 }
1849
1850 return strtod(devid_override, NULL);
1851 }
1852
1853 /**
1854 * Get the PCI ID for the device. This can be overridden by setting the
1855 * INTEL_DEVID_OVERRIDE environment variable to the desired ID.
1856 *
1857 * Returns -1 on ioctl failure.
1858 */
1859 static int
1860 get_pci_device_id(struct intel_screen *screen)
1861 {
1862 if (geteuid() == getuid()) {
1863 char *devid_override = getenv("INTEL_DEVID_OVERRIDE");
1864 if (devid_override) {
1865 screen->no_hw = true;
1866 return parse_devid_override(devid_override);
1867 }
1868 }
1869
1870 return intel_get_integer(screen, I915_PARAM_CHIPSET_ID);
1871 }
1872
1873 /**
1874 * This is the driver specific part of the createNewScreen entry point.
1875 * Called when using DRI2.
1876 *
1877 * \return the struct gl_config supported by this driver
1878 */
1879 static const
1880 __DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
1881 {
1882 struct intel_screen *screen;
1883
1884 if (dri_screen->image.loader) {
1885 } else if (dri_screen->dri2.loader->base.version <= 2 ||
1886 dri_screen->dri2.loader->getBuffersWithFormat == NULL) {
1887 fprintf(stderr,
1888 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1889 "support required\n");
1890 return NULL;
1891 }
1892
1893 /* Allocate the private area */
1894 screen = rzalloc(NULL, struct intel_screen);
1895 if (!screen) {
1896 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1897 return NULL;
1898 }
1899 /* parse information in __driConfigOptions */
1900 driParseOptionInfo(&screen->optionCache, brw_config_options.xml);
1901
1902 screen->driScrnPriv = dri_screen;
1903 dri_screen->driverPrivate = (void *) screen;
1904
1905 screen->deviceID = get_pci_device_id(screen);
1906
1907 if (!gen_get_device_info(screen->deviceID, &screen->devinfo))
1908 return NULL;
1909
1910 if (!intel_init_bufmgr(screen))
1911 return NULL;
1912
1913 const struct gen_device_info *devinfo = &screen->devinfo;
1914
1915 brw_process_intel_debug_variable();
1916
1917 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && devinfo->gen < 7) {
1918 fprintf(stderr,
1919 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
1920 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
1921 }
1922
1923 if (intel_get_integer(screen, I915_PARAM_MMAP_GTT_VERSION) >= 1) {
1924 /* Theorectically unlimited! At least for individual objects...
1925 *
1926 * Currently the entire (global) address space for all GTT maps is
1927 * limited to 64bits. That is all objects on the system that are
1928 * setup for GTT mmapping must fit within 64bits. An attempt to use
1929 * one that exceeds the limit with fail in brw_bo_map_gtt().
1930 *
1931 * Long before we hit that limit, we will be practically limited by
1932 * that any single object must fit in physical memory (RAM). The upper
1933 * limit on the CPU's address space is currently 48bits (Skylake), of
1934 * which only 39bits can be physical memory. (The GPU itself also has
1935 * a 48bit addressable virtual space.) We can fit over 32 million
1936 * objects of the current maximum allocable size before running out
1937 * of mmap space.
1938 */
1939 screen->max_gtt_map_object_size = UINT64_MAX;
1940 } else {
1941 /* Estimate the size of the mappable aperture into the GTT. There's an
1942 * ioctl to get the whole GTT size, but not one to get the mappable subset.
1943 * It turns out it's basically always 256MB, though some ancient hardware
1944 * was smaller.
1945 */
1946 uint32_t gtt_size = 256 * 1024 * 1024;
1947
1948 /* We don't want to map two objects such that a memcpy between them would
1949 * just fault one mapping in and then the other over and over forever. So
1950 * we would need to divide the GTT size by 2. Additionally, some GTT is
1951 * taken up by things like the framebuffer and the ringbuffer and such, so
1952 * be more conservative.
1953 */
1954 screen->max_gtt_map_object_size = gtt_size / 4;
1955 }
1956
1957 screen->aperture_threshold = get_aperture_size(dri_screen->fd) * 3 / 4;
1958
1959 screen->hw_has_swizzling = intel_detect_swizzling(screen);
1960 screen->hw_has_timestamp = intel_detect_timestamp(screen);
1961
1962 /* GENs prior to 8 do not support EU/Subslice info */
1963 if (devinfo->gen >= 8) {
1964 intel_detect_sseu(screen);
1965 } else if (devinfo->gen == 7) {
1966 screen->subslice_total = 1 << (devinfo->gt - 1);
1967 }
1968
1969 /* Gen7-7.5 kernel requirements / command parser saga:
1970 *
1971 * - pre-v3.16:
1972 * Haswell and Baytrail cannot use any privileged batchbuffer features.
1973 *
1974 * Ivybridge has aliasing PPGTT on by default, which accidentally marks
1975 * all batches secure, allowing them to use any feature with no checking.
1976 * This is effectively equivalent to a command parser version of
1977 * \infinity - everything is possible.
1978 *
1979 * The command parser does not exist, and querying the version will
1980 * return -EINVAL.
1981 *
1982 * - v3.16:
1983 * The kernel enables the command parser by default, for systems with
1984 * aliasing PPGTT enabled (Ivybridge and Haswell). However, the
1985 * hardware checker is still enabled, so Haswell and Baytrail cannot
1986 * do anything.
1987 *
1988 * Ivybridge goes from "everything is possible" to "only what the
1989 * command parser allows" (if the user boots with i915.cmd_parser=0,
1990 * then everything is possible again). We can only safely use features
1991 * allowed by the supported command parser version.
1992 *
1993 * Annoyingly, I915_PARAM_CMD_PARSER_VERSION reports the static version
1994 * implemented by the kernel, even if it's turned off. So, checking
1995 * for version > 0 does not mean that you can write registers. We have
1996 * to try it and see. The version does, however, indicate the age of
1997 * the kernel.
1998 *
1999 * Instead of matching the hardware checker's behavior of converting
2000 * privileged commands to MI_NOOP, it makes execbuf2 start returning
2001 * -EINVAL, making it dangerous to try and use privileged features.
2002 *
2003 * Effective command parser versions:
2004 * - Haswell: 0 (reporting 1, writes don't work)
2005 * - Baytrail: 0 (reporting 1, writes don't work)
2006 * - Ivybridge: 1 (enabled) or infinite (disabled)
2007 *
2008 * - v3.17:
2009 * Baytrail aliasing PPGTT is enabled, making it like Ivybridge:
2010 * effectively version 1 (enabled) or infinite (disabled).
2011 *
2012 * - v3.19: f1f55cc0556031c8ee3fe99dae7251e78b9b653b
2013 * Command parser v2 supports predicate writes.
2014 *
2015 * - Haswell: 0 (reporting 1, writes don't work)
2016 * - Baytrail: 2 (enabled) or infinite (disabled)
2017 * - Ivybridge: 2 (enabled) or infinite (disabled)
2018 *
2019 * So version >= 2 is enough to know that Ivybridge and Baytrail
2020 * will work. Haswell still can't do anything.
2021 *
2022 * - v4.0: Version 3 happened. Largely not relevant.
2023 *
2024 * - v4.1: 6702cf16e0ba8b0129f5aa1b6609d4e9c70bc13b
2025 * L3 config registers are properly saved and restored as part
2026 * of the hardware context. We can approximately detect this point
2027 * in time by checking if I915_PARAM_REVISION is recognized - it
2028 * landed in a later commit, but in the same release cycle.
2029 *
2030 * - v4.2: 245054a1fe33c06ad233e0d58a27ec7b64db9284
2031 * Command parser finally gains secure batch promotion. On Haswell,
2032 * the hardware checker gets disabled, which finally allows it to do
2033 * privileged commands.
2034 *
2035 * I915_PARAM_CMD_PARSER_VERSION reports 3. Effective versions:
2036 * - Haswell: 3 (enabled) or 0 (disabled)
2037 * - Baytrail: 3 (enabled) or infinite (disabled)
2038 * - Ivybridge: 3 (enabled) or infinite (disabled)
2039 *
2040 * Unfortunately, detecting this point in time is tricky, because
2041 * no version bump happened when this important change occurred.
2042 * On Haswell, if we can write any register, then the kernel is at
2043 * least this new, and we can start trusting the version number.
2044 *
2045 * - v4.4: 2bbe6bbb0dc94fd4ce287bdac9e1bd184e23057b and
2046 * Command parser reaches version 4, allowing access to Haswell
2047 * atomic scratch and chicken3 registers. If version >= 4, we know
2048 * the kernel is new enough to support privileged features on all
2049 * hardware. However, the user might have disabled it...and the
2050 * kernel will still report version 4. So we still have to guess
2051 * and check.
2052 *
2053 * - v4.4: 7b9748cb513a6bef4af87b79f0da3ff7e8b56cd8
2054 * Command parser v5 whitelists indirect compute shader dispatch
2055 * registers, needed for OpenGL 4.3 and later.
2056 *
2057 * - v4.8:
2058 * Command parser v7 lets us use MI_MATH on Haswell.
2059 *
2060 * Additionally, the kernel begins reporting version 0 when
2061 * the command parser is disabled, allowing us to skip the
2062 * guess-and-check step on Haswell. Unfortunately, this also
2063 * means that we can no longer use it as an indicator of the
2064 * age of the kernel.
2065 */
2066 if (intel_get_param(screen, I915_PARAM_CMD_PARSER_VERSION,
2067 &screen->cmd_parser_version) < 0) {
2068 /* Command parser does not exist - getparam is unrecognized */
2069 screen->cmd_parser_version = 0;
2070 }
2071
2072 /* Kernel 4.13 retuired for exec object capture */
2073 #ifndef I915_PARAM_HAS_EXEC_CAPTURE
2074 #define I915_PARAM_HAS_EXEC_CAPTURE 45
2075 #endif
2076 if (intel_get_boolean(screen, I915_PARAM_HAS_EXEC_CAPTURE)) {
2077 screen->kernel_features |= KERNEL_ALLOWS_EXEC_CAPTURE;
2078 }
2079
2080 if (!intel_detect_pipelined_so(screen)) {
2081 /* We can't do anything, so the effective version is 0. */
2082 screen->cmd_parser_version = 0;
2083 } else {
2084 screen->kernel_features |= KERNEL_ALLOWS_SOL_OFFSET_WRITES;
2085 }
2086
2087 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 2)
2088 screen->kernel_features |= KERNEL_ALLOWS_PREDICATE_WRITES;
2089
2090 /* Haswell requires command parser version 4 in order to have L3
2091 * atomic scratch1 and chicken3 bits
2092 */
2093 if (devinfo->is_haswell && screen->cmd_parser_version >= 4) {
2094 screen->kernel_features |=
2095 KERNEL_ALLOWS_HSW_SCRATCH1_AND_ROW_CHICKEN3;
2096 }
2097
2098 /* Haswell requires command parser version 6 in order to write to the
2099 * MI_MATH GPR registers, and version 7 in order to use
2100 * MI_LOAD_REGISTER_REG (which all users of MI_MATH use).
2101 */
2102 if (devinfo->gen >= 8 ||
2103 (devinfo->is_haswell && screen->cmd_parser_version >= 7)) {
2104 screen->kernel_features |= KERNEL_ALLOWS_MI_MATH_AND_LRR;
2105 }
2106
2107 /* Gen7 needs at least command parser version 5 to support compute */
2108 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 5)
2109 screen->kernel_features |= KERNEL_ALLOWS_COMPUTE_DISPATCH;
2110
2111 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
2112 if (force_msaa) {
2113 screen->winsys_msaa_samples_override =
2114 intel_quantize_num_samples(screen, atoi(force_msaa));
2115 printf("Forcing winsys sample count to %d\n",
2116 screen->winsys_msaa_samples_override);
2117 } else {
2118 screen->winsys_msaa_samples_override = -1;
2119 }
2120
2121 set_max_gl_versions(screen);
2122
2123 /* Notification of GPU resets requires hardware contexts and a kernel new
2124 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
2125 * supported, calling it with a context of 0 will either generate EPERM or
2126 * no error. If the ioctl is not supported, it always generate EINVAL.
2127 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
2128 * extension to the loader.
2129 *
2130 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
2131 */
2132 if (devinfo->gen >= 6) {
2133 struct drm_i915_reset_stats stats;
2134 memset(&stats, 0, sizeof(stats));
2135
2136 const int ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
2137
2138 screen->has_context_reset_notification =
2139 (ret != -1 || errno != EINVAL);
2140 }
2141
2142 dri_screen->extensions = !screen->has_context_reset_notification
2143 ? screenExtensions : intelRobustScreenExtensions;
2144
2145 screen->compiler = brw_compiler_create(screen, devinfo);
2146 screen->compiler->shader_debug_log = shader_debug_log_mesa;
2147 screen->compiler->shader_perf_log = shader_perf_log_mesa;
2148 screen->program_id = 1;
2149
2150 screen->has_exec_fence =
2151 intel_get_boolean(screen, I915_PARAM_HAS_EXEC_FENCE);
2152
2153 return (const __DRIconfig**) intel_screen_make_configs(dri_screen);
2154 }
2155
2156 struct intel_buffer {
2157 __DRIbuffer base;
2158 struct brw_bo *bo;
2159 };
2160
2161 static __DRIbuffer *
2162 intelAllocateBuffer(__DRIscreen *dri_screen,
2163 unsigned attachment, unsigned format,
2164 int width, int height)
2165 {
2166 struct intel_buffer *intelBuffer;
2167 struct intel_screen *screen = dri_screen->driverPrivate;
2168
2169 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
2170 attachment == __DRI_BUFFER_BACK_LEFT);
2171
2172 intelBuffer = calloc(1, sizeof *intelBuffer);
2173 if (intelBuffer == NULL)
2174 return NULL;
2175
2176 /* The front and back buffers are color buffers, which are X tiled. GEN9+
2177 * supports Y tiled and compressed buffers, but there is no way to plumb that
2178 * through to here. */
2179 uint32_t pitch;
2180 int cpp = format / 8;
2181 intelBuffer->bo = brw_bo_alloc_tiled(screen->bufmgr,
2182 "intelAllocateBuffer",
2183 width,
2184 height,
2185 cpp,
2186 I915_TILING_X, &pitch,
2187 BO_ALLOC_FOR_RENDER);
2188
2189 if (intelBuffer->bo == NULL) {
2190 free(intelBuffer);
2191 return NULL;
2192 }
2193
2194 brw_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
2195
2196 intelBuffer->base.attachment = attachment;
2197 intelBuffer->base.cpp = cpp;
2198 intelBuffer->base.pitch = pitch;
2199
2200 return &intelBuffer->base;
2201 }
2202
2203 static void
2204 intelReleaseBuffer(__DRIscreen *dri_screen, __DRIbuffer *buffer)
2205 {
2206 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
2207
2208 brw_bo_unreference(intelBuffer->bo);
2209 free(intelBuffer);
2210 }
2211
2212 static const struct __DriverAPIRec brw_driver_api = {
2213 .InitScreen = intelInitScreen2,
2214 .DestroyScreen = intelDestroyScreen,
2215 .CreateContext = brwCreateContext,
2216 .DestroyContext = intelDestroyContext,
2217 .CreateBuffer = intelCreateBuffer,
2218 .DestroyBuffer = intelDestroyBuffer,
2219 .MakeCurrent = intelMakeCurrent,
2220 .UnbindContext = intelUnbindContext,
2221 .AllocateBuffer = intelAllocateBuffer,
2222 .ReleaseBuffer = intelReleaseBuffer
2223 };
2224
2225 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
2226 .base = { __DRI_DRIVER_VTABLE, 1 },
2227 .vtable = &brw_driver_api,
2228 };
2229
2230 static const __DRIextension *brw_driver_extensions[] = {
2231 &driCoreExtension.base,
2232 &driImageDriverExtension.base,
2233 &driDRI2Extension.base,
2234 &brw_vtable.base,
2235 &brw_config_options.base,
2236 NULL
2237 };
2238
2239 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
2240 {
2241 globalDriverAPI = &brw_driver_api;
2242
2243 return brw_driver_extensions;
2244 }