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