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