blob: c75f2125d4b3348b93c0cb567177a6b227500bf0 [file] [log] [blame]
Ian Romanickaa1a5c02015-08-19 19:24:45 -07001/*
José Fonseca87712852014-01-17 16:27:50 +00002 * Copyright 2003 VMware, Inc.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00003 * All Rights Reserved.
Kenneth Graunkea7bdd4c2013-11-25 15:46:34 -08004 *
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00005 * 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,
Ian Romanick284dcad2015-08-19 16:36:35 -07009 * distribute, sublicense, and/or sell copies of the Software, and to
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000010 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kenneth Graunkea7bdd4c2013-11-25 15:46:34 -080012 *
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000013 * 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.
Kenneth Graunkea7bdd4c2013-11-25 15:46:34 -080016 *
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Ian Romanick284dcad2015-08-19 16:36:35 -070019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
José Fonseca87712852014-01-17 16:27:50 +000020 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000021 * 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.
Ian Romanickaa1a5c02015-08-19 19:24:45 -070024 */
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000025
Ben Widawsky7ce04052016-11-04 12:34:40 -070026#include <drm_fourcc.h>
Eric Anholtf6ca4a32011-03-09 12:54:14 -080027#include <errno.h>
Quentin Glidic7cb87642012-10-09 15:15:47 +020028#include <time.h>
Ian Romanick1e4ce082013-02-14 16:50:28 -080029#include <unistd.h>
Brian Paulecadb512008-09-18 15:17:05 -060030#include "main/context.h"
31#include "main/framebuffer.h"
Brian Paulecadb512008-09-18 15:17:05 -060032#include "main/renderbuffer.h"
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +020033#include "main/texobj.h"
Kristian Høgsberg2d995882010-02-11 17:18:01 -050034#include "main/hash.h"
Kristian Høgsbergd7322c92010-02-26 14:49:31 -050035#include "main/fbobject.h"
Brian Paul14aff232012-01-02 15:20:04 -070036#include "main/version.h"
Brian Pauld0dc75c2011-12-05 20:40:48 -070037#include "swrast/s_renderbuffer.h"
Kenneth Graunke1e0da622014-02-24 23:39:14 -080038#include "util/ralloc.h"
Jason Ekstrand8048c192017-03-01 08:58:43 -080039#include "brw_defines.h"
Emil Velikova39a8fb2016-01-18 12:54:03 +020040#include "compiler/nir/nir.h"
Brian Paul6c244b02009-01-26 12:38:46 -070041
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000042#include "utils.h"
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000043#include "xmlpool.h"
44
Ben Widawsky5c6e0d12017-03-13 18:20:02 -070045#ifndef DRM_FORMAT_MOD_INVALID
46#define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
47#endif
48
Ben Widawskyd78a36e2017-01-13 12:01:37 -080049#ifndef DRM_FORMAT_MOD_LINEAR
50#define DRM_FORMAT_MOD_LINEAR 0
51#endif
52
Eric Anholt68689232013-09-27 15:25:40 -070053static const __DRIconfigOptionsExtension brw_config_options = {
54 .base = { __DRI_CONFIG_OPTIONS, 1 },
55 .xml =
56DRI_CONF_BEGIN
Eric Anholta0e453a2008-01-17 14:23:04 -080057 DRI_CONF_SECTION_PERFORMANCE
Jesse Barnese9bf3e42008-07-31 11:50:37 -070058 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
Eric Anholtfe91c052008-03-05 14:14:54 -080059 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
60 * DRI_CONF_BO_REUSE_ALL
61 */
Dave Airlief75843a2008-08-24 17:59:10 +100062 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
Eric Anholtfe91c052008-03-05 14:14:54 -080063 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
Eric Anholta0e453a2008-01-17 14:23:04 -080068 DRI_CONF_SECTION_END
Chad Versace2f896622013-11-03 13:14:50 -080069
Eric Anholta0e453a2008-01-17 14:23:04 -080070 DRI_CONF_SECTION_QUALITY
Eric Anholtea6cf2b2013-01-02 17:02:58 -080071 DRI_CONF_FORCE_S3TC_ENABLE("false")
Chad Versace2f896622013-11-03 13:14:50 -080072
Gurchetan Singhd9546b02016-05-11 13:32:09 -070073 DRI_CONF_PRECISE_TRIG("false")
74
Chad Versace2f896622013-11-03 13:14:50 -080075 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
Eric Anholta0e453a2008-01-17 14:23:04 -080079 DRI_CONF_SECTION_END
Chad Versace2f896622013-11-03 13:14:50 -080080
Eric Anholta0e453a2008-01-17 14:23:04 -080081 DRI_CONF_SECTION_DEBUG
Eric Anholtea6cf2b2013-01-02 17:02:58 -080082 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")
Kenneth Graunke2f7a7ae2017-01-20 20:33:57 -080087 DRI_CONF_FORCE_GLSL_VERSION(0)
Eric Anholtea6cf2b2013-01-02 17:02:58 -080088 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
89 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
Kenneth Graunkeb3340cd2016-01-20 17:33:14 -080090 DRI_CONF_DUAL_COLOR_BLEND_BY_LOCATION("false")
Kenneth Graunke31f1cbc2014-08-08 01:03:15 -070091 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
John Brooksbf4d7672017-05-15 01:47:37 -040092 DRI_CONF_ALLOW_GLSL_BUILTIN_VARIABLE_REDECLARATION("false")
Lionel Landwerlin569231c2017-02-15 16:57:50 +000093 DRI_CONF_ALLOW_HIGHER_COMPAT_VERSION("false")
Samuel Pitoiset448f4c02017-03-17 01:06:54 +010094 DRI_CONF_FORCE_GLSL_ABS_SQRT("false")
Eric Anholt81aa5d72009-07-29 13:07:49 -070095
Eric Anholtea6cf2b2013-01-02 17:02:58 -080096 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
Eric Anholtc6abde22011-11-23 10:01:39 -080097 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
98 DRI_CONF_OPT_END
Eric Anholta0e453a2008-01-17 14:23:04 -080099 DRI_CONF_SECTION_END
Rob Clarkf78a6b12016-06-24 14:28:51 -0400100
101 DRI_CONF_SECTION_MISCELLANEOUS
102 DRI_CONF_GLSL_ZERO_INIT("false")
103 DRI_CONF_SECTION_END
Eric Anholt68689232013-09-27 15:25:40 -0700104DRI_CONF_END
105};
Eric Anholta0e453a2008-01-17 14:23:04 -0800106
Eric Anholtdf9f8912010-12-13 11:02:15 -0800107#include "intel_batchbuffer.h"
108#include "intel_buffers.h"
Kenneth Graunkeeed86b92017-03-20 16:42:55 -0700109#include "brw_bufmgr.h"
Eric Anholtdf9f8912010-12-13 11:02:15 -0800110#include "intel_fbo.h"
Chad Versaceda2816a2011-11-16 14:04:25 -0800111#include "intel_mipmap_tree.h"
Eric Anholtdf9f8912010-12-13 11:02:15 -0800112#include "intel_screen.h"
113#include "intel_tex.h"
Eric Anholt55667472014-04-29 15:30:15 -0700114#include "intel_image.h"
Eric Anholtdf9f8912010-12-13 11:02:15 -0800115
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200116#include "brw_context.h"
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200117
Eric Anholtdf9f8912010-12-13 11:02:15 -0800118#include "i915_drm.h"
119
Eric Anholt006c1a32012-08-07 10:05:38 -0700120/**
121 * For debugging purposes, this returns a time in seconds.
122 */
123double
124get_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
Kristian Høgsberg6d487792008-02-14 22:12:51 -0500133static const __DRItexBufferExtension intelTexBufferExtension = {
Emil Velikov38f20f72014-02-12 17:47:53 +0000134 .base = { __DRI_TEX_BUFFER, 3 },
Chad Versacec9f51262012-11-19 11:43:51 -0800135
136 .setTexBuffer = intelSetTexBuffer,
137 .setTexBuffer2 = intelSetTexBuffer2,
138 .releaseTexBuffer = NULL,
Kristian Høgsberg6d487792008-02-14 22:12:51 -0500139};
140
Kristian Høgsberg7c50d292010-01-08 12:35:47 -0500141static void
Eric Anholt313f2bc2012-12-28 11:44:08 -0800142intel_dri2_flush_with_flags(__DRIcontext *cPriv,
143 __DRIdrawable *dPriv,
144 unsigned flags,
145 enum __DRI2throttleReason reason)
Kristian Høgsberg7c50d292010-01-08 12:35:47 -0500146{
Eric Anholt313f2bc2012-12-28 11:44:08 -0800147 struct brw_context *brw = cPriv->driverPrivate;
148
149 if (!brw)
Anuj Phogatce1c9492012-01-17 13:21:52 -0800150 return;
Kristian Høgsberg7c50d292010-01-08 12:35:47 -0500151
Eric Anholt313f2bc2012-12-28 11:44:08 -0800152 struct gl_context *ctx = &brw->ctx;
Kristian Høgsberge67c3382010-05-18 21:50:44 -0400153
Eric Anholt313f2bc2012-12-28 11:44:08 -0800154 FLUSH_VERTICES(ctx, 0);
155
156 if (flags & __DRI2_FLUSH_DRAWABLE)
157 intel_resolve_for_dri2_flush(brw, dPriv);
158
Chris Wilson8b9bd192015-02-26 11:25:18 +0000159 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
160 brw->need_swap_throttle = true;
161 if (reason == __DRI2_THROTTLE_FLUSHFRONT)
162 brw->need_flush_throttle = true;
Eric Anholt313f2bc2012-12-28 11:44:08 -0800163
164 intel_batchbuffer_flush(brw);
Kristian Høgsberg7c50d292010-01-08 12:35:47 -0500165}
166
Eric Anholt313f2bc2012-12-28 11:44:08 -0800167/**
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 */
173static void
174intel_dri2_flush(__DRIdrawable *drawable)
175{
176 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
177 __DRI2_FLUSH_DRAWABLE,
178 __DRI2_THROTTLE_SWAPBUFFER);
179}
Chad Versacec9f51262012-11-19 11:43:51 -0800180
Eric Anholt313f2bc2012-12-28 11:44:08 -0800181static const struct __DRI2flushExtensionRec intelFlushExtension = {
182 .base = { __DRI2_FLUSH, 4 },
183
184 .flush = intel_dri2_flush,
Chad Versacec9f51262012-11-19 11:43:51 -0800185 .invalidate = dri2InvalidateDrawable,
Eric Anholt313f2bc2012-12-28 11:44:08 -0800186 .flush_with_flags = intel_dri2_flush_with_flags,
Kristian Høgsberg7c50d292010-01-08 12:35:47 -0500187};
188
Eric Anholt9ba6f472012-09-21 10:36:22 +0200189static struct intel_image_format intel_image_formats[] = {
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200190 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
191 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
192
Gwenole Beauchesnee1c50ab2014-09-09 10:56:24 +0200193 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
194 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
195
Keith Packardaea47572013-11-21 20:08:35 -0800196 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
197 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
198
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200199 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
200 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
201
Gwenole Beauchesnee1c50ab2014-09-09 10:56:24 +0200202 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
203 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
204
Chad Versacee2d69d52016-12-22 15:00:12 -0800205 { __DRI_IMAGE_FOURCC_ARGB1555, __DRI_IMAGE_COMPONENTS_RGBA, 1,
206 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555, 2 } } },
207
Singh, Satyeshware2620c12013-10-16 01:11:02 +0000208 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
209 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
210
Chad Versace56f1f472015-06-23 15:48:40 -0700211 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
212 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
213
Rainer Hochecker09b140a2017-01-05 16:58:56 +0100214 { __DRI_IMAGE_FOURCC_R16, __DRI_IMAGE_COMPONENTS_R, 1,
215 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 1 }, } },
216
Chad Versace56f1f472015-06-23 15:48:40 -0700217 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
218 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
219
Rainer Hochecker09b140a2017-01-05 16:58:56 +0100220 { __DRI_IMAGE_FOURCC_GR1616, __DRI_IMAGE_COMPONENTS_RG, 1,
221 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616, 2 }, } },
222
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200223 { __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
Kristian Høgsberg Kristensen2bb935b2016-04-27 15:00:54 -0700248 { __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
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200273 { __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
Johnson Lin165e7042017-06-16 13:51:34 +0800281 /* 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. */
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200289 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
290 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
Johnson Lin165e7042017-06-16 13:51:34 +0800291 { 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 } } }
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200295};
296
Daniel Stone467332a2017-05-03 18:05:10 +0100297static const struct {
298 uint32_t tiling;
299 uint64_t modifier;
Varad Gautamf804e062017-05-30 17:24:09 +0530300 unsigned since_gen;
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530301 unsigned height_align;
Daniel Stone467332a2017-05-03 18:05:10 +0100302} tiling_modifier_map[] = {
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530303 { .tiling = I915_TILING_NONE, .modifier = DRM_FORMAT_MOD_LINEAR,
Varad Gautamf804e062017-05-30 17:24:09 +0530304 .since_gen = 1, .height_align = 1 },
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530305 { .tiling = I915_TILING_X, .modifier = I915_FORMAT_MOD_X_TILED,
Varad Gautamf804e062017-05-30 17:24:09 +0530306 .since_gen = 1, .height_align = 8 },
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530307 { .tiling = I915_TILING_Y, .modifier = I915_FORMAT_MOD_Y_TILED,
Varad Gautamf804e062017-05-30 17:24:09 +0530308 .since_gen = 6, .height_align = 32 },
Daniel Stone467332a2017-05-03 18:05:10 +0100309};
310
Varad Gautamc3037722017-05-30 17:24:08 +0530311static bool
312modifier_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
Daniel Stone467332a2017-05-03 18:05:10 +0100324static uint32_t
325modifier_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
Daniel Stoned4342b12017-05-05 18:24:44 +0100334 unreachable("modifier_to_tiling should only receive known modifiers");
Daniel Stone467332a2017-05-03 18:05:10 +0100335}
336
Daniel Stone8b8af192017-05-03 09:38:13 +0100337static uint64_t
338tiling_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
Daniel Stoned4342b12017-05-05 18:24:44 +0100347 unreachable("tiling_to_modifier received unknown tiling mode");
Daniel Stone8b8af192017-05-03 09:38:13 +0100348}
349
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530350static unsigned
351get_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
Eric Anholte3a9ca42014-04-25 12:14:34 -0700363static void
364intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
365{
366 uint32_t tiling, swizzle;
Kenneth Graunked30a9272017-04-03 20:13:08 -0700367 brw_bo_get_tiling(image->bo, &tiling, &swizzle);
Eric Anholte3a9ca42014-04-25 12:14:34 -0700368
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
Topi Pohjolainen904587a2012-12-28 12:22:54 +0200375static struct intel_image_format *
376intel_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
Dave Airlie8f7338f2014-03-03 13:57:16 +1000390static 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
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500401static __DRIimage *
Ben Widawskyfc1e9f02016-10-20 14:51:53 -0700402intel_allocate_image(struct intel_screen *screen, int dri_format,
403 void *loaderPrivate)
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500404{
405 __DRIimage *image;
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500406
Brian Paul4fdac652012-09-01 07:47:24 -0600407 image = calloc(1, sizeof *image);
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500408 if (image == NULL)
409 return NULL;
410
Ben Widawskyfc1e9f02016-10-20 14:51:53 -0700411 image->screen = screen;
Kristian Høgsberg1bb15c02012-07-05 00:17:47 -0400412 image->dri_format = dri_format;
Kristian Høgsberg95bc0522012-07-05 13:02:02 -0400413 image->offset = 0;
Ander Conselvan de Oliveira249817e2012-04-30 12:32:45 +0300414
Keith Packard1f085ba2013-11-04 17:33:34 -0800415 image->format = driImageFormatToGLFormat(dri_format);
Ander Conselvan de Oliveira5ba6be22013-11-12 14:47:08 +0200416 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
417 image->format == MESA_FORMAT_NONE) {
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500418 free(image);
419 return NULL;
420 }
421
Kristian Høgsberg454fc072012-07-05 00:07:15 -0400422 image->internal_format = _mesa_get_format_base_format(image->format);
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500423 image->data = loaderPrivate;
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500424
Kristian Høgsberg1bb15c02012-07-05 00:17:47 -0400425 return image;
426}
427
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200428/**
Eric Anholte3a9ca42014-04-25 12:14:34 -0700429 * Sets up a DRIImage structure to point to a slice out of a miptree.
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200430 */
431static void
Kenneth Graunkeca437572013-07-02 23:17:14 -0700432intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200433 struct intel_mipmap_tree *mt, GLuint level,
434 GLuint zoffset)
435{
Kenneth Graunkeca437572013-07-02 23:17:14 -0700436 intel_miptree_make_shareable(brw, mt);
Paul Berryef9142d2013-05-21 14:21:44 -0700437
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200438 intel_miptree_check_level_layer(mt, level, zoffset);
439
Kenneth Graunkeb18871c2014-02-22 23:47:30 -0800440 image->width = minify(mt->physical_width0, level - mt->first_level);
441 image->height = minify(mt->physical_height0, level - mt->first_level);
Eric Anholte16c5c92014-04-25 13:29:41 -0700442 image->pitch = mt->pitch;
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200443
Eric Anholt6db640d2014-04-25 11:26:27 -0700444 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
445 &image->tile_x,
446 &image->tile_y);
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200447
Kenneth Graunked30a9272017-04-03 20:13:08 -0700448 brw_bo_unreference(image->bo);
Eric Anholte16c5c92014-04-25 13:29:41 -0700449 image->bo = mt->bo;
Kenneth Graunked30a9272017-04-03 20:13:08 -0700450 brw_bo_reference(mt->bo);
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200451}
452
Kristian Høgsberg1bb15c02012-07-05 00:17:47 -0400453static __DRIimage *
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -0800454intel_create_image_from_name(__DRIscreen *dri_screen,
Kristian Høgsberg1bb15c02012-07-05 00:17:47 -0400455 int width, int height, int format,
456 int name, int pitch, void *loaderPrivate)
457{
Kenneth Graunke9694b232015-11-30 15:47:13 -0800458 struct intel_screen *screen = dri_screen->driverPrivate;
Kristian Høgsberg1bb15c02012-07-05 00:17:47 -0400459 __DRIimage *image;
460 int cpp;
461
Ben Widawskyfc1e9f02016-10-20 14:51:53 -0700462 image = intel_allocate_image(screen, format, loaderPrivate);
Anuj Phogat484b89a2013-04-16 10:34:43 -0700463 if (image == NULL)
464 return NULL;
465
Kristian Høgsberg636646a2012-07-16 10:54:30 -0400466 if (image->format == MESA_FORMAT_NONE)
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200467 cpp = 1;
Kristian Høgsberg636646a2012-07-16 10:54:30 -0400468 else
469 cpp = _mesa_get_format_bytes(image->format);
Eric Anholte3a9ca42014-04-25 12:14:34 -0700470
471 image->width = width;
472 image->height = height;
473 image->pitch = pitch * cpp;
Kenneth Graunked30a9272017-04-03 20:13:08 -0700474 image->bo = brw_bo_gem_create_from_name(screen->bufmgr, "image",
Eric Anholte3a9ca42014-04-25 12:14:34 -0700475 name);
476 if (!image->bo) {
Brian Paulfe72a062012-09-01 07:47:24 -0600477 free(image);
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500478 return NULL;
479 }
Daniel Stone8b8af192017-05-03 09:38:13 +0100480 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500481
Iago Toral Quiroga2bba2152015-11-24 12:49:55 +0100482 return image;
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500483}
484
485static __DRIimage *
486intel_create_image_from_renderbuffer(__DRIcontext *context,
487 int renderbuffer, void *loaderPrivate)
488{
489 __DRIimage *image;
Kenneth Graunkeca437572013-07-02 23:17:14 -0700490 struct brw_context *brw = context->driverPrivate;
Kenneth Graunke8c9a54e2013-07-06 00:46:38 -0700491 struct gl_context *ctx = &brw->ctx;
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500492 struct gl_renderbuffer *rb;
493 struct intel_renderbuffer *irb;
494
Kenneth Graunkeca437572013-07-02 23:17:14 -0700495 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500496 if (!rb) {
Kenneth Graunkeca437572013-07-02 23:17:14 -0700497 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500498 return NULL;
499 }
500
501 irb = intel_renderbuffer(rb);
Kenneth Graunkeca437572013-07-02 23:17:14 -0700502 intel_miptree_make_shareable(brw, irb->mt);
Brian Paul4fdac652012-09-01 07:47:24 -0600503 image = calloc(1, sizeof *image);
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500504 if (image == NULL)
505 return NULL;
506
507 image->internal_format = rb->InternalFormat;
508 image->format = rb->Format;
Daniel Stone8b8af192017-05-03 09:38:13 +0100509 image->modifier = tiling_to_modifier(irb->mt->tiling);
Kristian Høgsberg95bc0522012-07-05 13:02:02 -0400510 image->offset = 0;
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500511 image->data = loaderPrivate;
Kenneth Graunked30a9272017-04-03 20:13:08 -0700512 brw_bo_unreference(image->bo);
Eric Anholte16c5c92014-04-25 13:29:41 -0700513 image->bo = irb->mt->bo;
Kenneth Graunked30a9272017-04-03 20:13:08 -0700514 brw_bo_reference(irb->mt->bo);
Eric Anholte16c5c92014-04-25 13:29:41 -0700515 image->width = rb->Width;
516 image->height = rb->Height;
517 image->pitch = irb->mt->pitch;
Keith Packard1f085ba2013-11-04 17:33:34 -0800518 image->dri_format = driGLFormatToImageFormat(image->format);
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200519 image->has_depthstencil = irb->mt->stencil_mt? true : false;
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500520
Eric Anholtc810e672013-05-10 12:36:43 -0700521 rb->NeedsFinishRenderTexture = true;
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200522 return image;
523}
524
525static __DRIimage *
526intel_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;
Kenneth Graunkeca437572013-07-02 23:17:14 -0700533 struct brw_context *brw = context->driverPrivate;
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200534 struct gl_texture_object *obj;
535 struct intel_texture_object *iobj;
536 GLuint face = 0;
537
Kenneth Graunke8c9a54e2013-07-06 00:46:38 -0700538 obj = _mesa_lookup_texture(&brw->ctx, texture);
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200539 if (!obj || obj->Target != target) {
540 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
541 return NULL;
Ander Conselvan de Oliveira249817e2012-04-30 12:32:45 +0300542 }
543
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200544 if (target == GL_TEXTURE_CUBE_MAP)
545 face = zoffset;
546
Kenneth Graunke8c9a54e2013-07-06 00:46:38 -0700547 _mesa_test_texobj_completeness(&brw->ctx, obj);
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200548 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;
Daniel Stone8b8af192017-05-03 09:38:13 +0100571 image->modifier = tiling_to_modifier(iobj->mt->tiling);
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200572 image->data = loaderPrivate;
Kenneth Graunkeca437572013-07-02 23:17:14 -0700573 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
Keith Packard1f085ba2013-11-04 17:33:34 -0800574 image->dri_format = driGLFormatToImageFormat(image->format);
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200575 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;
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500583 return image;
584}
585
586static void
587intel_destroy_image(__DRIimage *image)
588{
Kenneth Graunked30a9272017-04-03 20:13:08 -0700589 brw_bo_unreference(image->bo);
Eric Anholte3a9ca42014-04-25 12:14:34 -0700590 free(image);
Kristian Høgsbergc2624712010-02-11 18:59:40 -0500591}
592
Ben Widawskyd78a36e2017-01-13 12:01:37 -0800593enum modifier_priority {
594 MODIFIER_PRIORITY_INVALID = 0,
595 MODIFIER_PRIORITY_LINEAR,
Ben Widawskycd6bd7f2017-01-02 15:01:54 -0800596 MODIFIER_PRIORITY_X,
Ben Widawsky7ce04052016-11-04 12:34:40 -0700597 MODIFIER_PRIORITY_Y,
Ben Widawskyd78a36e2017-01-13 12:01:37 -0800598};
599
600const uint64_t priority_to_modifier[] = {
601 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
602 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
Ben Widawskycd6bd7f2017-01-02 15:01:54 -0800603 [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
Ben Widawsky7ce04052016-11-04 12:34:40 -0700604 [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
Ben Widawskyd78a36e2017-01-13 12:01:37 -0800605};
606
Ben Widawsky5c6e0d12017-03-13 18:20:02 -0700607static uint64_t
608select_best_modifier(struct gen_device_info *devinfo,
609 const uint64_t *modifiers,
610 const unsigned count)
611{
Ben Widawskyd78a36e2017-01-13 12:01:37 -0800612 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
613
614 for (int i = 0; i < count; i++) {
615 switch (modifiers[i]) {
Ben Widawsky7ce04052016-11-04 12:34:40 -0700616 case I915_FORMAT_MOD_Y_TILED:
617 prio = MAX2(prio, MODIFIER_PRIORITY_Y);
618 break;
Ben Widawskycd6bd7f2017-01-02 15:01:54 -0800619 case I915_FORMAT_MOD_X_TILED:
620 prio = MAX2(prio, MODIFIER_PRIORITY_X);
621 break;
Ben Widawskyd78a36e2017-01-13 12:01:37 -0800622 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];
Ben Widawsky5c6e0d12017-03-13 18:20:02 -0700632}
633
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400634static __DRIimage *
Ben Widawsky5e7d8d32017-03-13 14:53:43 -0700635intel_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)
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400641{
642 __DRIimage *image;
Kenneth Graunke9694b232015-11-30 15:47:13 -0800643 struct intel_screen *screen = dri_screen->driverPrivate;
Daniel Stone467332a2017-05-03 18:05:10 +0100644 uint32_t tiling;
Daniel Stone6b18d4a2017-05-30 17:23:49 +0530645 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530646 unsigned tiled_height;
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400647 int cpp;
648
Ben Widawsky5e7d8d32017-03-13 14:53:43 -0700649 /* 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
Kristian Høgsberge5169e92011-05-06 10:31:18 -0400655 if (use & __DRI_IMAGE_USE_CURSOR) {
656 if (width != 64 || height != 64)
657 return NULL;
Daniel Stone6b18d4a2017-05-30 17:23:49 +0530658 modifier = DRM_FORMAT_MOD_LINEAR;
Kristian Høgsberge5169e92011-05-06 10:31:18 -0400659 }
660
Axel Davye8f91952013-08-15 12:47:58 +0200661 if (use & __DRI_IMAGE_USE_LINEAR)
Daniel Stone6b18d4a2017-05-30 17:23:49 +0530662 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);
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530678 tiled_height = get_tiled_height(modifier, height);
Axel Davye8f91952013-08-15 12:47:58 +0200679
Ben Widawskyfc1e9f02016-10-20 14:51:53 -0700680 image = intel_allocate_image(screen, format, loaderPrivate);
Anuj Phogat484b89a2013-04-16 10:34:43 -0700681 if (image == NULL)
682 return NULL;
683
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400684 cpp = _mesa_get_format_bytes(image->format);
Jason Ekstrand6ee05302017-06-12 09:35:22 -0700685 image->bo = brw_bo_alloc_tiled_2d(screen->bufmgr, "image",
686 width, tiled_height, cpp, tiling,
687 &image->pitch, 0);
Eric Anholte3a9ca42014-04-25 12:14:34 -0700688 if (image->bo == NULL) {
Brian Paulfe72a062012-09-01 07:47:24 -0600689 free(image);
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400690 return NULL;
691 }
Eric Anholte3a9ca42014-04-25 12:14:34 -0700692 image->width = width;
693 image->height = height;
Ben Widawskyd78a36e2017-01-13 12:01:37 -0800694 image->modifier = modifier;
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200695
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400696 return image;
697}
698
Ben Widawsky5e7d8d32017-03-13 14:53:43 -0700699static __DRIimage *
700intel_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
709static __DRIimage *
710intel_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{
Ben Widawsky7ce04052016-11-04 12:34:40 -0700716 return intel_create_image_common(dri_screen, width, height, format, 0,
717 modifiers, count, loaderPrivate);
Ben Widawsky5e7d8d32017-03-13 14:53:43 -0700718}
719
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400720static GLboolean
721intel_query_image(__DRIimage *image, int attrib, int *value)
722{
723 switch (attrib) {
724 case __DRI_IMAGE_ATTRIB_STRIDE:
Eric Anholte3a9ca42014-04-25 12:14:34 -0700725 *value = image->pitch;
Kenneth Graunke2e5a1a22011-10-07 12:26:50 -0700726 return true;
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400727 case __DRI_IMAGE_ATTRIB_HANDLE:
Kenneth Graunke59fdd942017-04-03 15:39:09 -0700728 *value = image->bo->gem_handle;
Kenneth Graunke2e5a1a22011-10-07 12:26:50 -0700729 return true;
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400730 case __DRI_IMAGE_ATTRIB_NAME:
Kenneth Graunked30a9272017-04-03 20:13:08 -0700731 return !brw_bo_flink(image->bo, (uint32_t *) value);
Jesse Barnes8de5c352012-02-21 12:53:09 -0800732 case __DRI_IMAGE_ATTRIB_FORMAT:
Ander Conselvan de Oliveirafc7d2242012-04-26 16:21:19 +0300733 *value = image->dri_format;
734 return true;
Kristian Høgsberg44f066b2012-07-13 11:19:24 -0400735 case __DRI_IMAGE_ATTRIB_WIDTH:
Eric Anholte3a9ca42014-04-25 12:14:34 -0700736 *value = image->width;
Kristian Høgsberg44f066b2012-07-13 11:19:24 -0400737 return true;
738 case __DRI_IMAGE_ATTRIB_HEIGHT:
Eric Anholte3a9ca42014-04-25 12:14:34 -0700739 *value = image->height;
Kristian Høgsberg44f066b2012-07-13 11:19:24 -0400740 return true;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200741 case __DRI_IMAGE_ATTRIB_COMPONENTS:
742 if (image->planar_format == NULL)
743 return false;
744 *value = image->planar_format->components;
745 return true;
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500746 case __DRI_IMAGE_ATTRIB_FD:
Kenneth Graunked30a9272017-04-03 20:13:08 -0700747 return !brw_bo_gem_export_to_prime(image->bo, value);
Dave Airlie8f7338f2014-03-03 13:57:16 +1000748 case __DRI_IMAGE_ATTRIB_FOURCC:
Eric Engestrom25327432016-08-15 15:51:20 +0100749 return intel_lookup_fourcc(image->dri_format, value);
Dave Airlie8f7338f2014-03-03 13:57:16 +1000750 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
751 *value = 1;
752 return true;
Chuanbo Wenge4648ba2016-09-14 01:07:18 +0800753 case __DRI_IMAGE_ATTRIB_OFFSET:
754 *value = image->offset;
755 return true;
Ben Widawsky79f619c2017-03-17 13:29:08 -0700756 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;
Dave Airlie8f7338f2014-03-03 13:57:16 +1000762
Kristian Høgsberg44f066b2012-07-13 11:19:24 -0400763 default:
Kenneth Graunke2e5a1a22011-10-07 12:26:50 -0700764 return false;
Kristian Høgsbergf3019322010-06-06 20:39:19 -0400765 }
766}
767
Benjamin Franzke3af3c582011-03-09 20:56:02 +0100768static __DRIimage *
769intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
770{
771 __DRIimage *image;
772
Brian Paul4fdac652012-09-01 07:47:24 -0600773 image = calloc(1, sizeof *image);
Benjamin Franzke3af3c582011-03-09 20:56:02 +0100774 if (image == NULL)
775 return NULL;
776
Kenneth Graunked30a9272017-04-03 20:13:08 -0700777 brw_bo_reference(orig_image->bo);
Eric Anholte3a9ca42014-04-25 12:14:34 -0700778 image->bo = orig_image->bo;
Benjamin Franzke3af3c582011-03-09 20:56:02 +0100779 image->internal_format = orig_image->internal_format;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200780 image->planar_format = orig_image->planar_format;
Ander Conselvan de Oliveira249817e2012-04-30 12:32:45 +0300781 image->dri_format = orig_image->dri_format;
Benjamin Franzke3af3c582011-03-09 20:56:02 +0100782 image->format = orig_image->format;
Daniel Stone8b8af192017-05-03 09:38:13 +0100783 image->modifier = orig_image->modifier;
Kristian Høgsberg95bc0522012-07-05 13:02:02 -0400784 image->offset = orig_image->offset;
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200785 image->width = orig_image->width;
786 image->height = orig_image->height;
Eric Anholte3a9ca42014-04-25 12:14:34 -0700787 image->pitch = orig_image->pitch;
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +0200788 image->tile_x = orig_image->tile_x;
789 image->tile_y = orig_image->tile_y;
790 image->has_depthstencil = orig_image->has_depthstencil;
Benjamin Franzke3af3c582011-03-09 20:56:02 +0100791 image->data = loaderPrivate;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200792
793 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
794 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
795
Benjamin Franzke3af3c582011-03-09 20:56:02 +0100796 return image;
797}
798
Kristian Høgsberg221c6782012-01-18 15:32:35 -0500799static GLboolean
800intel_validate_usage(__DRIimage *image, unsigned int use)
801{
802 if (use & __DRI_IMAGE_USE_CURSOR) {
Eric Anholte3a9ca42014-04-25 12:14:34 -0700803 if (image->width != 64 || image->height != 64)
Kristian Høgsberg221c6782012-01-18 15:32:35 -0500804 return GL_FALSE;
805 }
806
807 return GL_TRUE;
808}
809
Kristian Høgsberg95bc0522012-07-05 13:02:02 -0400810static __DRIimage *
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -0800811intel_create_image_from_names(__DRIscreen *dri_screen,
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200812 int width, int height, int fourcc,
813 int *names, int num_names,
814 int *strides, int *offsets,
815 void *loaderPrivate)
Kristian Høgsberg95bc0522012-07-05 13:02:02 -0400816{
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200817 struct intel_image_format *f = NULL;
Kristian Høgsberg95bc0522012-07-05 13:02:02 -0400818 __DRIimage *image;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200819 int i, index;
820
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -0800821 if (dri_screen == NULL || names == NULL || num_names != 1)
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200822 return NULL;
823
Topi Pohjolainen904587a2012-12-28 12:22:54 +0200824 f = intel_image_format_lookup(fourcc);
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200825 if (f == NULL)
826 return NULL;
827
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -0800828 image = intel_create_image_from_name(dri_screen, width, height,
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200829 __DRI_IMAGE_FORMAT_NONE,
830 names[0], strides[0],
831 loaderPrivate);
832
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500833 if (image == NULL)
834 return NULL;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +0200835
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
846static __DRIimage *
Varad Gautamc3037722017-05-30 17:24:08 +0530847intel_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)
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500852{
Kenneth Graunke9694b232015-11-30 15:47:13 -0800853 struct intel_screen *screen = dri_screen->driverPrivate;
Topi Pohjolainen904587a2012-12-28 12:22:54 +0200854 struct intel_image_format *f;
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500855 __DRIimage *image;
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530856 unsigned tiled_height;
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500857 int i, index;
858
Kristian Høgsberg Kristensen1be11142016-05-01 21:25:35 -0700859 if (fds == NULL || num_fds < 1)
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500860 return NULL;
861
Topi Pohjolainen904587a2012-12-28 12:22:54 +0200862 f = intel_image_format_lookup(fourcc);
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500863 if (f == NULL)
864 return NULL;
865
Varad Gautamc3037722017-05-30 17:24:08 +0530866 if (modifier != DRM_FORMAT_MOD_INVALID && !modifier_is_supported(modifier))
867 return NULL;
868
Topi Pohjolainene8568a02013-04-25 14:33:09 +0300869 if (f->nplanes == 1)
Ben Widawskyfc1e9f02016-10-20 14:51:53 -0700870 image = intel_allocate_image(screen, f->planes[0].dri_format,
871 loaderPrivate);
Topi Pohjolainene8568a02013-04-25 14:33:09 +0300872 else
Ben Widawskyfc1e9f02016-10-20 14:51:53 -0700873 image = intel_allocate_image(screen, __DRI_IMAGE_FORMAT_NONE,
874 loaderPrivate);
Topi Pohjolainene8568a02013-04-25 14:33:09 +0300875
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500876 if (image == NULL)
877 return NULL;
878
Eric Anholte3a9ca42014-04-25 12:14:34 -0700879 image->width = width;
880 image->height = height;
881 image->pitch = strides[0];
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500882
883 image->planar_format = f;
Daniel Stone78703882017-06-06 11:53:55 +0100884
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
Daniel Stonef58e6352017-05-30 17:23:48 +0530891 /* 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
Varad Gautamc3037722017-05-30 17:24:08 +0530904 if (modifier != DRM_FORMAT_MOD_INVALID)
905 image->modifier = modifier;
906 else
907 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530908 tiled_height = get_tiled_height(image->modifier, height);
Daniel Stone78703882017-06-06 11:53:55 +0100909
Kristian Høgsberg Kristensen1be11142016-05-01 21:25:35 -0700910 int size = 0;
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500911 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];
Kristian Høgsberg Kristensen1be11142016-05-01 21:25:35 -0700915
Ben Widawsky37cdcaf2017-05-30 17:23:55 +0530916 const int plane_height = tiled_height >> f->planes[i].height_shift;
Kristian Høgsberg Kristensen89bb4be2016-05-25 09:30:26 -0700917 const int end = offsets[index] + plane_height * strides[index];
Kristian Høgsberg Kristensen1be11142016-05-01 21:25:35 -0700918 if (size < end)
919 size = end;
920 }
921
Daniel Stone78703882017-06-06 11:53:55 +0100922 /* 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);
Kristian Høgsberg Kristensen1be11142016-05-01 21:25:35 -0700928 free(image);
929 return NULL;
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500930 }
931
Gwenole Beauchesne3bd65dc2014-03-10 16:55:21 +0100932 if (f->nplanes == 1) {
933 image->offset = image->offsets[0];
Marius Predut28d9e902015-04-07 22:05:28 +0300934 intel_image_warn_if_unaligned(image, __func__);
Gwenole Beauchesne3bd65dc2014-03-10 16:55:21 +0100935 }
936
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500937 return image;
938}
939
Topi Pohjolainen674dedc2013-03-26 15:14:20 +0200940static __DRIimage *
Varad Gautamc3037722017-05-30 17:24:08 +0530941intel_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
952static __DRIimage *
953intel_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)
Topi Pohjolainen674dedc2013-03-26 15:14:20 +0200964{
965 __DRIimage *image;
966 struct intel_image_format *f = intel_image_format_lookup(fourcc);
967
Kristian Høgsberg Kristensen1be11142016-05-01 21:25:35 -0700968 if (!f) {
Topi Pohjolainen674dedc2013-03-26 15:14:20 +0200969 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
970 return NULL;
971 }
972
Varad Gautamc3037722017-05-30 17:24:08 +0530973 image = intel_create_image_from_fds_common(dri_screen, width, height,
974 fourcc, modifier,
975 fds, num_fds, strides, offsets,
976 loaderPrivate);
Topi Pohjolainen674dedc2013-03-26 15:14:20 +0200977
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
Topi Pohjolainen3a52cd32013-06-18 13:47:43 +0300988 image->dma_buf_imported = true;
Topi Pohjolainen674dedc2013-03-26 15:14:20 +0200989 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}
Kristian Høgsberg2356e282013-02-02 08:38:07 -0500997
998static __DRIimage *
Varad Gautamc3037722017-05-30 17:24:08 +0530999intel_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
Varad Gautamf804e062017-05-30 17:24:09 +05301021static GLboolean
1022intel_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
1042static GLboolean
1043intel_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
Varad Gautamc3037722017-05-30 17:24:08 +05301086static __DRIimage *
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02001087intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
1088{
Eric Anholt60894ed2013-01-10 15:11:28 -08001089 int width, height, offset, stride, dri_format, index;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02001090 struct intel_image_format *f;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02001091 __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
Eric Anholte3a9ca42014-04-25 12:14:34 -07001101 width = parent->width >> f->planes[plane].width_shift;
1102 height = parent->height >> f->planes[plane].height_shift;
Jakob Bornecrantz6a7dea92012-08-31 19:48:26 +02001103 dri_format = f->planes[plane].dri_format;
1104 index = f->planes[plane].buffer_index;
1105 offset = parent->offsets[index];
1106 stride = parent->strides[index];
Kristian Høgsberg95bc0522012-07-05 13:02:02 -04001107
Ben Widawskyfc1e9f02016-10-20 14:51:53 -07001108 image = intel_allocate_image(parent->screen, dri_format, loaderPrivate);
Anuj Phogat484b89a2013-04-16 10:34:43 -07001109 if (image == NULL)
1110 return NULL;
1111
Eric Anholte3a9ca42014-04-25 12:14:34 -07001112 if (offset + height * stride > parent->bo->size) {
Kristian Høgsberg95bc0522012-07-05 13:02:02 -04001113 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
Brian Paulfe72a062012-09-01 07:47:24 -06001114 free(image);
Kristian Høgsberg95bc0522012-07-05 13:02:02 -04001115 return NULL;
1116 }
1117
Eric Anholte3a9ca42014-04-25 12:14:34 -07001118 image->bo = parent->bo;
Kenneth Graunked30a9272017-04-03 20:13:08 -07001119 brw_bo_reference(parent->bo);
Daniel Stone8b8af192017-05-03 09:38:13 +01001120 image->modifier = parent->modifier;
Kristian Høgsberg95bc0522012-07-05 13:02:02 -04001121
Eric Anholte3a9ca42014-04-25 12:14:34 -07001122 image->width = width;
1123 image->height = height;
1124 image->pitch = stride;
Kristian Høgsberg95bc0522012-07-05 13:02:02 -04001125 image->offset = offset;
1126
Marius Predut28d9e902015-04-07 22:05:28 +03001127 intel_image_warn_if_unaligned(image, __func__);
Kristian Høgsberg95bc0522012-07-05 13:02:02 -04001128
1129 return image;
1130}
1131
Emil Velikov38f20f72014-02-12 17:47:53 +00001132static const __DRIimageExtension intelImageExtension = {
Varad Gautamf804e062017-05-30 17:24:09 +05301133 .base = { __DRI_IMAGE, 15 },
Chad Versacec9f51262012-11-19 11:43:51 -08001134
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,
Abdiel Janulgue6c7e95c2012-11-28 13:30:18 +02001143 .fromPlanar = intel_from_planar,
Kristian Høgsberg2356e282013-02-02 08:38:07 -05001144 .createImageFromTexture = intel_create_image_from_texture,
Topi Pohjolainen674dedc2013-03-26 15:14:20 +02001145 .createImageFromFds = intel_create_image_from_fds,
Dave Airlie8f7338f2014-03-03 13:57:16 +10001146 .createImageFromDmaBufs = intel_create_image_from_dma_bufs,
1147 .blitImage = NULL,
Chuanbo Wenge4648ba2016-09-14 01:07:18 +08001148 .getCapabilities = NULL,
1149 .mapImage = NULL,
1150 .unmapImage = NULL,
Ben Widawsky5e7d8d32017-03-13 14:53:43 -07001151 .createImageWithModifiers = intel_create_image_with_modifiers,
Varad Gautamc3037722017-05-30 17:24:08 +05301152 .createImageFromDmaBufs2 = intel_create_image_from_dma_bufs2,
Varad Gautamf804e062017-05-30 17:24:09 +05301153 .queryDmaBufFormats = intel_query_dma_buf_formats,
1154 .queryDmaBufModifiers = intel_query_dma_buf_modifiers,
Kristian Høgsbergc2624712010-02-11 18:59:40 -05001155};
1156
Kenneth Graunke034b2202017-03-30 13:52:46 -07001157static uint64_t
1158get_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
Ian Romanick1e4ce082013-02-14 16:50:28 -08001168static int
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001169brw_query_renderer_integer(__DRIscreen *dri_screen,
1170 int param, unsigned int *value)
Ian Romanick1e4ce082013-02-14 16:50:28 -08001171{
Kenneth Graunke9694b232015-11-30 15:47:13 -08001172 const struct intel_screen *const screen =
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001173 (struct intel_screen *) dri_screen->driverPrivate;
Ian Romanick1e4ce082013-02-14 16:50:28 -08001174
1175 switch (param) {
1176 case __DRI2_RENDERER_VENDOR_ID:
1177 value[0] = 0x8086;
1178 return 0;
1179 case __DRI2_RENDERER_DEVICE_ID:
Kenneth Graunke9694b232015-11-30 15:47:13 -08001180 value[0] = screen->deviceID;
Ian Romanick1e4ce082013-02-14 16:50:28 -08001181 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.
Ian Romanick1e4ce082013-02-14 16:50:28 -08001189 */
Ian Romanickcb6182b2013-11-11 10:57:55 -08001190 const unsigned gpu_mappable_megabytes =
Kenneth Graunke63682842017-03-30 22:27:42 -07001191 screen->aperture_threshold / (1024 * 1024);
Ian Romanick1e4ce082013-02-14 16:50:28 -08001192
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 =
Emil Velikovfc259562014-02-22 03:04:02 +00001203 (unsigned) (system_memory_bytes / (1024 * 1024));
Ian Romanick1e4ce082013-02-14 16:50:28 -08001204
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;
Adam Jackson8ce2afe2016-09-21 09:13:36 -04001211 case __DRI2_RENDERER_HAS_TEXTURE_3D:
1212 value[0] = 1;
1213 return 0;
Ian Romanick1e4ce082013-02-14 16:50:28 -08001214 default:
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001215 return driQueryRendererIntegerCommon(dri_screen, param, value);
Ian Romanick1e4ce082013-02-14 16:50:28 -08001216 }
1217
1218 return -1;
1219}
1220
1221static int
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001222brw_query_renderer_string(__DRIscreen *dri_screen,
1223 int param, const char **value)
Ian Romanick1e4ce082013-02-14 16:50:28 -08001224{
Kenneth Graunke9694b232015-11-30 15:47:13 -08001225 const struct intel_screen *screen =
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001226 (struct intel_screen *) dri_screen->driverPrivate;
Ian Romanick1e4ce082013-02-14 16:50:28 -08001227
1228 switch (param) {
1229 case __DRI2_RENDERER_VENDOR_ID:
1230 value[0] = brw_vendor_string;
1231 return 0;
1232 case __DRI2_RENDERER_DEVICE_ID:
Kenneth Graunke9694b232015-11-30 15:47:13 -08001233 value[0] = brw_get_renderer_string(screen);
Ian Romanick1e4ce082013-02-14 16:50:28 -08001234 return 0;
1235 default:
1236 break;
1237 }
1238
1239 return -1;
1240}
1241
Emil Velikov38f20f72014-02-12 17:47:53 +00001242static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
Ian Romanick1e4ce082013-02-14 16:50:28 -08001243 .base = { __DRI2_RENDERER_QUERY, 1 },
1244
1245 .queryInteger = brw_query_renderer_integer,
1246 .queryString = brw_query_renderer_string
1247};
1248
Emil Velikov38f20f72014-02-12 17:47:53 +00001249static const __DRIrobustnessExtension dri2Robustness = {
1250 .base = { __DRI2_ROBUSTNESS, 1 }
Ian Romanick1f712bd2012-09-11 11:08:44 +03001251};
1252
Kenneth Graunke9694b232015-11-30 15:47:13 -08001253static const __DRIextension *screenExtensions[] = {
Kristian Høgsberg6d487792008-02-14 22:12:51 -05001254 &intelTexBufferExtension.base,
Chad Versacec6362842015-05-05 19:05:32 -07001255 &intelFenceExtension.base,
Kristian Høgsberg7c50d292010-01-08 12:35:47 -05001256 &intelFlushExtension.base,
Kristian Høgsbergc2624712010-02-11 18:59:40 -05001257 &intelImageExtension.base,
Ian Romanick1e4ce082013-02-14 16:50:28 -08001258 &intelRendererQueryExtension.base,
Jesse Barnes234286c2010-04-22 12:47:41 -07001259 &dri2ConfigQueryExtension.base,
Ian Romanick9b1c6862013-11-19 17:01:23 -08001260 NULL
1261};
1262
1263static const __DRIextension *intelRobustScreenExtensions[] = {
1264 &intelTexBufferExtension.base,
Chad Versacec6362842015-05-05 19:05:32 -07001265 &intelFenceExtension.base,
Ian Romanick9b1c6862013-11-19 17:01:23 -08001266 &intelFlushExtension.base,
1267 &intelImageExtension.base,
1268 &intelRendererQueryExtension.base,
1269 &dri2ConfigQueryExtension.base,
Ian Romanick1f712bd2012-09-11 11:08:44 +03001270 &dri2Robustness.base,
Kristian Høgsbergac3e8382007-05-15 15:17:30 -04001271 NULL
1272};
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001273
Ben Widawskya8975a92016-04-11 09:49:41 -07001274static int
Chad Versace844e0bd2016-06-27 11:29:27 -07001275intel_get_param(struct intel_screen *screen, int param, int *value)
Kristian Høgsberg24e7e452008-01-09 18:04:19 -05001276{
Chad Versaced3a147b2016-06-27 11:33:36 -07001277 int ret = 0;
Alan Hourihane1c718c02008-02-22 00:18:54 +00001278 struct drm_i915_getparam gp;
Kristian Høgsberg24e7e452008-01-09 18:04:19 -05001279
Eric Anholtf33d1002012-02-16 11:30:49 -08001280 memset(&gp, 0, sizeof(gp));
Kristian Høgsberg24e7e452008-01-09 18:04:19 -05001281 gp.param = param;
1282 gp.value = value;
1283
Chad Versaced3a147b2016-06-27 11:33:36 -07001284 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 }
Kristian Høgsberg24e7e452008-01-09 18:04:19 -05001289
Ben Widawskya8975a92016-04-11 09:49:41 -07001290 return ret;
Kristian Høgsberg24e7e452008-01-09 18:04:19 -05001291}
Kristian Høgsbergac3e8382007-05-15 15:17:30 -04001292
Kenneth Graunke2e5a1a22011-10-07 12:26:50 -07001293static bool
Chad Versace844e0bd2016-06-27 11:29:27 -07001294intel_get_boolean(struct intel_screen *screen, int param)
Chris Wilson900a5c92011-03-01 14:46:50 +00001295{
1296 int value = 0;
Chad Versace844e0bd2016-06-27 11:29:27 -07001297 return (intel_get_param(screen, param, &value) == 0) && value;
Chris Wilson900a5c92011-03-01 14:46:50 +00001298}
1299
Chris Wilsonf92a87a2016-08-24 20:35:46 +01001300static int
1301intel_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
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001311static void
Kristian Høgsbergd61f0732010-01-01 17:09:12 -05001312intelDestroyScreen(__DRIscreen * sPriv)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001313{
Kenneth Graunke9694b232015-11-30 15:47:13 -08001314 struct intel_screen *screen = sPriv->driverPrivate;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001315
Kenneth Graunke662a7332017-04-03 18:10:23 -07001316 brw_bufmgr_destroy(screen->bufmgr);
Kenneth Graunke9694b232015-11-30 15:47:13 -08001317 driDestroyOptionInfo(&screen->optionCache);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001318
Kenneth Graunke9694b232015-11-30 15:47:13 -08001319 ralloc_free(screen);
George Sapountzis875a7572011-11-03 13:04:57 +02001320 sPriv->driverPrivate = NULL;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001321}
1322
1323
1324/**
Chad Versace85dd3e42017-05-23 16:04:55 -07001325 * 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.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001330 */
1331static GLboolean
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001332intelCreateBuffer(__DRIscreen *dri_screen,
Kristian Høgsbergd61f0732010-01-01 17:09:12 -05001333 __DRIdrawable * driDrawPriv,
Kristian Høgsbergd3491e72010-10-12 11:58:47 -04001334 const struct gl_config * mesaVis, GLboolean isPixmap)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001335{
Kristian Høgsbergd2821282010-01-01 23:21:16 -05001336 struct intel_renderbuffer *rb;
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001337 struct intel_screen *screen = (struct intel_screen *)
1338 dri_screen->driverPrivate;
Mark Mueller71fe9432014-01-04 14:11:43 -08001339 mesa_format rgbFormat;
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001340 unsigned num_samples =
1341 intel_quantize_num_samples(screen, mesaVis->samples);
Kristian Høgsbergd2821282010-01-01 23:21:16 -05001342
Chad Versace2b4fbc42012-07-09 16:51:23 -07001343 if (isPixmap)
1344 return false;
1345
Chad Versacee8a0a5d2017-05-23 16:07:10 -07001346 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
Chad Versace2b4fbc42012-07-09 16:51:23 -07001347 if (!fb)
1348 return false;
1349
1350 _mesa_initialize_window_framebuffer(fb, mesaVis);
1351
Eric Anholt41033502014-03-21 16:36:22 -07001352 if (screen->winsys_msaa_samples_override != -1) {
1353 num_samples = screen->winsys_msaa_samples_override;
1354 fb->Visual.samples = num_samples;
1355 }
1356
Haixia Shi35ade362016-04-07 11:05:09 -07001357 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;
Eric Anholt6fddd372013-06-20 16:10:43 -07001369 fb->Visual.sRGBCapable = true;
Eric Anholte15c21a2013-02-15 07:41:42 -08001370 }
Chad Versace2b4fbc42012-07-09 16:51:23 -07001371
1372 /* setup the hardware-based renderbuffers */
Chad Versacea9e5e9f2017-05-26 17:10:37 -07001373 rb = intel_create_winsys_renderbuffer(screen, rgbFormat, num_samples);
Timothy Arceria63919f2017-04-14 13:33:32 +10001374 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
Chad Versace2b4fbc42012-07-09 16:51:23 -07001375
1376 if (mesaVis->doubleBufferMode) {
Chad Versacea9e5e9f2017-05-26 17:10:37 -07001377 rb = intel_create_winsys_renderbuffer(screen, rgbFormat, num_samples);
Timothy Arceria63919f2017-04-14 13:33:32 +10001378 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
Chad Versace2b4fbc42012-07-09 16:51:23 -07001379 }
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
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001389 if (screen->devinfo.has_hiz_and_separate_stencil) {
Chad Versacea9e5e9f2017-05-26 17:10:37 -07001390 rb = intel_create_private_renderbuffer(screen,
1391 MESA_FORMAT_Z24_UNORM_X8_UINT,
Chad Versacee2f23762012-07-11 15:10:49 -07001392 num_samples);
Timothy Arceria63919f2017-04-14 13:33:32 +10001393 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
Chad Versacea9e5e9f2017-05-26 17:10:37 -07001394 rb = intel_create_private_renderbuffer(screen, MESA_FORMAT_S_UINT8,
Chad Versacee2f23762012-07-11 15:10:49 -07001395 num_samples);
Timothy Arceria63919f2017-04-14 13:33:32 +10001396 _mesa_attach_and_own_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
Chad Versace2b4fbc42012-07-09 16:51:23 -07001397 } else {
1398 /*
1399 * Use combined depth/stencil. Note that the renderbuffer is
1400 * attached to two attachment points.
1401 */
Chad Versacea9e5e9f2017-05-26 17:10:37 -07001402 rb = intel_create_private_renderbuffer(screen,
1403 MESA_FORMAT_Z24_UNORM_S8_UINT,
Chad Versacee2f23762012-07-11 15:10:49 -07001404 num_samples);
Timothy Arceria63919f2017-04-14 13:33:32 +10001405 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1406 _mesa_attach_and_reference_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
Chad Versace2b4fbc42012-07-09 16:51:23 -07001407 }
1408 }
1409 else if (mesaVis->depthBits == 16) {
1410 assert(mesaVis->stencilBits == 0);
Chad Versacea9e5e9f2017-05-26 17:10:37 -07001411 rb = intel_create_private_renderbuffer(screen, MESA_FORMAT_Z_UNORM16,
Chad Versacee2f23762012-07-11 15:10:49 -07001412 num_samples);
Timothy Arceria63919f2017-04-14 13:33:32 +10001413 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001414 }
1415 else {
Chad Versace2b4fbc42012-07-09 16:51:23 -07001416 assert(mesaVis->depthBits == 0);
1417 assert(mesaVis->stencilBits == 0);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001418 }
Chad Versace2b4fbc42012-07-09 16:51:23 -07001419
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;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001431}
1432
1433static void
Kristian Høgsbergd61f0732010-01-01 17:09:12 -05001434intelDestroyBuffer(__DRIdrawable * driDrawPriv)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001435{
Kristian Høgsbergd2821282010-01-01 23:21:16 -05001436 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
Kenneth Graunkea7bdd4c2013-11-25 15:46:34 -08001437
Kristian Høgsbergd2821282010-01-01 23:21:16 -05001438 _mesa_reference_framebuffer(&fb, NULL);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001439}
1440
Ben Widawsky4213b002016-04-07 10:53:12 -07001441static void
Kenneth Graunke9694b232015-11-30 15:47:13 -08001442intel_detect_sseu(struct intel_screen *screen)
Ben Widawsky4213b002016-04-07 10:53:12 -07001443{
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001444 assert(screen->devinfo.gen >= 8);
Ben Widawskycc01b632016-04-07 10:53:13 -07001445 int ret;
1446
Kenneth Graunke9694b232015-11-30 15:47:13 -08001447 screen->subslice_total = -1;
1448 screen->eu_total = -1;
Ben Widawsky4213b002016-04-07 10:53:12 -07001449
Kenneth Graunke9694b232015-11-30 15:47:13 -08001450 ret = intel_get_param(screen, I915_PARAM_SUBSLICE_TOTAL,
1451 &screen->subslice_total);
Ben Widawskya8975a92016-04-11 09:49:41 -07001452 if (ret < 0 && ret != -EINVAL)
Ben Widawskycc01b632016-04-07 10:53:13 -07001453 goto err_out;
1454
Kenneth Graunke9694b232015-11-30 15:47:13 -08001455 ret = intel_get_param(screen,
1456 I915_PARAM_EU_TOTAL, &screen->eu_total);
Ben Widawskya8975a92016-04-11 09:49:41 -07001457 if (ret < 0 && ret != -EINVAL)
Ben Widawskycc01b632016-04-07 10:53:13 -07001458 goto err_out;
Ben Widawsky4213b002016-04-07 10:53:12 -07001459
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 */
Kenneth Graunke9694b232015-11-30 15:47:13 -08001464 if (screen->subslice_total < 1 || screen->eu_total < 1)
Ben Widawsky4213b002016-04-07 10:53:12 -07001465 _mesa_warning(NULL,
1466 "Kernel 4.1 required to properly query GPU properties.\n");
Ben Widawskycc01b632016-04-07 10:53:13 -07001467
1468 return;
1469
1470err_out:
Kenneth Graunke9694b232015-11-30 15:47:13 -08001471 screen->subslice_total = -1;
1472 screen->eu_total = -1;
Mark Janesa2d28dd2016-05-19 13:42:16 -07001473 _mesa_warning(NULL, "Failed to query GPU properties (%s).\n", strerror(-ret));
Ben Widawsky4213b002016-04-07 10:53:12 -07001474}
1475
Kenneth Graunke2e5a1a22011-10-07 12:26:50 -07001476static bool
Kenneth Graunke9694b232015-11-30 15:47:13 -08001477intel_init_bufmgr(struct intel_screen *screen)
Eric Anholt7e0bbdc2008-09-04 22:16:31 +01001478{
Kenneth Graunke9694b232015-11-30 15:47:13 -08001479 __DRIscreen *dri_screen = screen->driScrnPriv;
Eric Anholt7e0bbdc2008-09-04 22:16:31 +01001480
Kenneth Graunke1dc02da2017-04-04 11:45:08 -07001481 if (getenv("INTEL_NO_HW") != NULL)
1482 screen->no_hw = true;
Eric Anholt7e0bbdc2008-09-04 22:16:31 +01001483
Kenneth Graunke662a7332017-04-03 18:10:23 -07001484 screen->bufmgr = brw_bufmgr_init(&screen->devinfo, dri_screen->fd, BATCH_SZ);
Kenneth Graunke9694b232015-11-30 15:47:13 -08001485 if (screen->bufmgr == NULL) {
Eric Anholt827ba442009-11-18 18:15:25 +01001486 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1487 __func__, __LINE__);
Kenneth Graunke2e5a1a22011-10-07 12:26:50 -07001488 return false;
Eric Anholt7e0bbdc2008-09-04 22:16:31 +01001489 }
1490
Kenneth Graunke08844942017-03-22 15:20:51 -07001491 if (!intel_get_boolean(screen, I915_PARAM_HAS_WAIT_TIMEOUT)) {
1492 fprintf(stderr, "[%s: %u] Kernel 3.6 required.\n", __func__, __LINE__);
Kenneth Graunke394edb52013-04-05 23:59:52 -07001493 return false;
1494 }
Chris Wilson900a5c92011-03-01 14:46:50 +00001495
Kenneth Graunke2e5a1a22011-10-07 12:26:50 -07001496 return true;
Eric Anholt7e0bbdc2008-09-04 22:16:31 +01001497}
1498
Daniel Vetterf172eae2012-03-02 21:38:44 +01001499static bool
1500intel_detect_swizzling(struct intel_screen *screen)
1501{
Kenneth Graunked30a9272017-04-03 20:13:08 -07001502 struct brw_bo *buffer;
Kenneth Graunke44ecbbe2017-04-11 00:02:35 -07001503 unsigned flags = 0;
Kenneth Graunke444ab812017-04-10 23:08:23 -07001504 uint32_t aligned_pitch;
Daniel Stonee54b2e92016-05-02 15:34:40 +01001505 uint32_t tiling = I915_TILING_X;
Daniel Vetterf172eae2012-03-02 21:38:44 +01001506 uint32_t swizzle_mode = 0;
1507
Jason Ekstrand6ee05302017-06-12 09:35:22 -07001508 buffer = brw_bo_alloc_tiled_2d(screen->bufmgr, "swizzle test",
1509 64, 64, 4, tiling, &aligned_pitch, flags);
Daniel Vetterf172eae2012-03-02 21:38:44 +01001510 if (buffer == NULL)
1511 return false;
1512
Kenneth Graunked30a9272017-04-03 20:13:08 -07001513 brw_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1514 brw_bo_unreference(buffer);
Daniel Vetterf172eae2012-03-02 21:38:44 +01001515
1516 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1517 return false;
1518 else
1519 return true;
1520}
1521
Chris Wilson013d7312015-07-21 11:12:57 +01001522static int
Chris Wilsonc8d3eba2015-04-29 13:32:38 +01001523intel_detect_timestamp(struct intel_screen *screen)
1524{
Chris Wilson013d7312015-07-21 11:12:57 +01001525 uint64_t dummy = 0, last = 0;
1526 int upper, lower, loops;
Chris Wilsonc8d3eba2015-04-29 13:32:38 +01001527
Chris Wilson013d7312015-07-21 11:12:57 +01001528 /* 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.
Chris Wilsonc8d3eba2015-04-29 13:32:38 +01001533 */
Kenneth Graunkedfd81372017-04-03 20:14:11 -07001534 if (brw_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
Chris Wilson013d7312015-07-21 11:12:57 +01001535 return 3;
Chris Wilsonc8d3eba2015-04-29 13:32:38 +01001536
Chris Wilson013d7312015-07-21 11:12:57 +01001537 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1538 * upper 32bits for a rapidly changing timestamp.
1539 */
Kenneth Graunkedfd81372017-04-03 20:14:11 -07001540 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &last))
Chris Wilson013d7312015-07-21 11:12:57 +01001541 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 */
Kenneth Graunkedfd81372017-04-03 20:14:11 -07001548 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
Chris Wilson013d7312015-07-21 11:12:57 +01001549 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;
Chris Wilsonc8d3eba2015-04-29 13:32:38 +01001564}
1565
Chris Wilson02a44482017-01-04 08:34:59 +01001566 /**
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 */
1573static bool
1574intel_detect_pipelined_register(struct intel_screen *screen,
1575 int reg, uint32_t expected_value, bool reset)
1576{
Kenneth Graunkee7ab0ea2017-03-28 16:13:41 -07001577 if (screen->no_hw)
1578 return false;
1579
Kenneth Graunked30a9272017-04-03 20:13:08 -07001580 struct brw_bo *results, *bo;
Chris Wilson02a44482017-01-04 08:34:59 +01001581 uint32_t *batch;
1582 uint32_t offset = 0;
Matt Turner5dc35e12017-05-05 11:20:05 -07001583 void *map;
Chris Wilson02a44482017-01-04 08:34:59 +01001584 bool success = false;
1585
1586 /* Create a zero'ed temporary buffer for reading our results */
Kenneth Graunked30a9272017-04-03 20:13:08 -07001587 results = brw_bo_alloc(screen->bufmgr, "registers", 4096, 0);
Chris Wilson02a44482017-01-04 08:34:59 +01001588 if (results == NULL)
1589 goto err;
1590
Kenneth Graunked30a9272017-04-03 20:13:08 -07001591 bo = brw_bo_alloc(screen->bufmgr, "batchbuffer", 4096, 0);
Chris Wilson02a44482017-01-04 08:34:59 +01001592 if (bo == NULL)
1593 goto err_results;
1594
Matt Turner2120cfe2017-05-18 11:26:08 -07001595 map = brw_bo_map(NULL, bo, MAP_WRITE);
Matt Turner5dc35e12017-05-05 11:20:05 -07001596 if (!map)
Chris Wilson02a44482017-01-04 08:34:59 +01001597 goto err_batch;
1598
Matt Turner5dc35e12017-05-05 11:20:05 -07001599 batch = map;
Chris Wilson02a44482017-01-04 08:34:59 +01001600
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;
Kenneth Graunkee7ab0ea2017-03-28 16:13:41 -07001609 struct drm_i915_gem_relocation_entry reloc = {
Matt Turner5dc35e12017-05-05 11:20:05 -07001610 .offset = (char *) batch - (char *) map,
Kenneth Graunkee7ab0ea2017-03-28 16:13:41 -07001611 .delta = offset * sizeof(uint32_t),
Kenneth Graunke59fdd942017-04-03 15:39:09 -07001612 .target_handle = results->gem_handle,
Kenneth Graunkee7ab0ea2017-03-28 16:13:41 -07001613 .read_domains = I915_GEM_DOMAIN_INSTRUCTION,
1614 .write_domain = I915_GEM_DOMAIN_INSTRUCTION,
1615 };
1616 *batch++ = reloc.presumed_offset + reloc.delta;
Chris Wilson02a44482017-01-04 08:34:59 +01001617
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
Kenneth Graunkee7ab0ea2017-03-28 16:13:41 -07001627 struct drm_i915_gem_exec_object2 exec_objects[2] = {
1628 {
Kenneth Graunke59fdd942017-04-03 15:39:09 -07001629 .handle = results->gem_handle,
Kenneth Graunkee7ab0ea2017-03-28 16:13:41 -07001630 },
1631 {
Kenneth Graunke59fdd942017-04-03 15:39:09 -07001632 .handle = bo->gem_handle,
Kenneth Graunkee7ab0ea2017-03-28 16:13:41 -07001633 .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,
Matt Turner5dc35e12017-05-05 11:20:05 -07001641 .batch_len = ALIGN((char *) batch - (char *) map, 8),
Kenneth Graunkee7ab0ea2017-03-28 16:13:41 -07001642 .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);
Chris Wilson02a44482017-01-04 08:34:59 +01001650
1651 /* Check whether the value got written. */
Matt Turner2120cfe2017-05-18 11:26:08 -07001652 void *results_map = brw_bo_map(NULL, results, MAP_READ);
Matt Turner5dc35e12017-05-05 11:20:05 -07001653 if (results_map) {
1654 success = *((uint32_t *)results_map + offset) == expected_value;
Kenneth Graunked30a9272017-04-03 20:13:08 -07001655 brw_bo_unmap(results);
Chris Wilson02a44482017-01-04 08:34:59 +01001656 }
1657
1658err_batch:
Kenneth Graunked30a9272017-04-03 20:13:08 -07001659 brw_bo_unreference(bo);
Chris Wilson02a44482017-01-04 08:34:59 +01001660err_results:
Kenneth Graunked30a9272017-04-03 20:13:08 -07001661 brw_bo_unreference(results);
Chris Wilson02a44482017-01-04 08:34:59 +01001662err:
1663 return success;
1664}
1665
1666static bool
1667intel_detect_pipelined_so(struct intel_screen *screen)
1668{
Kenneth Graunke5e29af52017-03-02 18:27:32 -08001669 const struct gen_device_info *devinfo = &screen->devinfo;
1670
Chris Wilson02a44482017-01-04 08:34:59 +01001671 /* Supposedly, Broadwell just works. */
Kenneth Graunke5e29af52017-03-02 18:27:32 -08001672 if (devinfo->gen >= 8)
Chris Wilson02a44482017-01-04 08:34:59 +01001673 return true;
1674
Kenneth Graunke5e29af52017-03-02 18:27:32 -08001675 if (devinfo->gen <= 6)
Chris Wilson02a44482017-01-04 08:34:59 +01001676 return false;
1677
Kenneth Graunke5e29af52017-03-02 18:27:32 -08001678 /* See the big explanation about command parser versions below */
1679 if (screen->cmd_parser_version >= (devinfo->is_haswell ? 7 : 2))
1680 return true;
1681
Chris Wilson02a44482017-01-04 08:34:59 +01001682 /* 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
Chad Versace95ebabb2013-11-06 19:40:25 -08001691/**
1692 * Return array of MSAA modes supported by the hardware. The array is
1693 * zero-terminated and sorted in decreasing order.
1694 */
1695const int*
1696intel_supported_msaa_modes(const struct intel_screen *screen)
1697{
Neil Roberts6c5f3712015-09-07 18:23:14 +01001698 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
Kenneth Graunke57405602014-02-10 11:42:47 -08001699 static const int gen8_modes[] = {8, 4, 2, 0, -1};
Chad Versace95ebabb2013-11-06 19:40:25 -08001700 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
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001704 if (screen->devinfo.gen >= 9) {
Neil Roberts6c5f3712015-09-07 18:23:14 +01001705 return gen9_modes;
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001706 } else if (screen->devinfo.gen >= 8) {
Kenneth Graunke57405602014-02-10 11:42:47 -08001707 return gen8_modes;
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001708 } else if (screen->devinfo.gen >= 7) {
Chad Versace95ebabb2013-11-06 19:40:25 -08001709 return gen7_modes;
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001710 } else if (screen->devinfo.gen == 6) {
Chad Versace95ebabb2013-11-06 19:40:25 -08001711 return gen6_modes;
1712 } else {
1713 return gen4_modes;
1714 }
1715}
1716
Chad Versaceb2d428c2012-07-12 14:17:22 -07001717static __DRIconfig**
1718intel_screen_make_configs(__DRIscreen *dri_screen)
1719{
Mark Mueller71fe9432014-01-04 14:11:43 -08001720 static const mesa_format formats[] = {
Mark Muellereeed49f2014-01-26 15:12:56 -08001721 MESA_FORMAT_B5G6R5_UNORM,
Boyan Ding28090b32015-03-25 19:36:54 +08001722 MESA_FORMAT_B8G8R8A8_UNORM,
Chad Versace2cde8ff2017-05-26 19:08:47 -07001723 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,
Ian Romanick1f6e10f2012-07-12 13:52:06 -07001745 };
1746
Chad Versace7dc0be82012-08-09 09:06:42 -07001747 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
Chad Versaceb2d428c2012-07-12 14:17:22 -07001748 static const GLenum back_buffer_modes[] = {
Chad Versace7dc0be82012-08-09 09:06:42 -07001749 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
Chad Versaceb2d428c2012-07-12 14:17:22 -07001750 };
1751
Chad Versace8b5d68d2012-08-02 14:51:47 -07001752 static const uint8_t singlesample_samples[1] = {0};
Chad Versacee943e5c2012-08-02 17:13:17 -07001753 static const uint8_t multisample_samples[2] = {4, 8};
Chad Versace8b5d68d2012-08-02 14:51:47 -07001754
Chad Versacee943e5c2012-08-02 17:13:17 -07001755 struct intel_screen *screen = dri_screen->driverPrivate;
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001756 const struct gen_device_info *devinfo = &screen->devinfo;
Chad Versace8b5d68d2012-08-02 14:51:47 -07001757 uint8_t depth_bits[4], stencil_bits[4];
Chad Versaceb2d428c2012-07-12 14:17:22 -07001758 __DRIconfig **configs = NULL;
1759
Chad Versacee943e5c2012-08-02 17:13:17 -07001760 /* Generate singlesample configs without accumulation buffer. */
Rhys Kiddf4ef8d02015-08-06 16:34:03 +10001761 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
Chad Versaceb2d428c2012-07-12 14:17:22 -07001762 __DRIconfig **new_configs;
Tapani Pällie4e3b072012-10-29 11:56:28 -07001763 int num_depth_stencil_bits = 2;
Chad Versaceb2d428c2012-07-12 14:17:22 -07001764
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
Tapani Pällie4e3b072012-10-29 11:56:28 -07001767 * buffer, gen >= 6 supports this.
Chad Versaceb2d428c2012-07-12 14:17:22 -07001768 */
Chad Versace8b5d68d2012-08-02 14:51:47 -07001769 depth_bits[0] = 0;
1770 stencil_bits[0] = 0;
1771
Mark Muellereeed49f2014-01-26 15:12:56 -08001772 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
Chad Versaceb2d428c2012-07-12 14:17:22 -07001773 depth_bits[1] = 16;
1774 stencil_bits[1] = 0;
Kenneth Graunkeafe05e72013-07-04 12:35:22 -07001775 if (devinfo->gen >= 6) {
Tapani Pällie4e3b072012-10-29 11:56:28 -07001776 depth_bits[2] = 24;
1777 stencil_bits[2] = 8;
1778 num_depth_stencil_bits = 3;
1779 }
Chad Versaceb2d428c2012-07-12 14:17:22 -07001780 } else {
1781 depth_bits[1] = 24;
1782 stencil_bits[1] = 8;
1783 }
1784
Ian Romanick1f6e10f2012-07-12 13:52:06 -07001785 new_configs = driCreateConfigs(formats[i],
Chad Versaceb2d428c2012-07-12 14:17:22 -07001786 depth_bits,
1787 stencil_bits,
Chad Versace8b5d68d2012-08-02 14:51:47 -07001788 num_depth_stencil_bits,
Chad Versace7dc0be82012-08-09 09:06:42 -07001789 back_buffer_modes, 2,
Chad Versace8b5d68d2012-08-02 14:51:47 -07001790 singlesample_samples, 1,
Ilia Mirkin52839002016-08-20 16:10:20 -04001791 false, false);
Chad Versacea4bf68c2012-08-01 21:23:47 -07001792 configs = driConcatConfigs(configs, new_configs);
Chad Versaceb2d428c2012-07-12 14:17:22 -07001793 }
1794
1795 /* Generate the minimum possible set of configs that include an
1796 * accumulation buffer.
1797 */
Rhys Kiddf4ef8d02015-08-06 16:34:03 +10001798 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
Chad Versaceb2d428c2012-07-12 14:17:22 -07001799 __DRIconfig **new_configs;
1800
Mark Muellereeed49f2014-01-26 15:12:56 -08001801 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
Chad Versaceb2d428c2012-07-12 14:17:22 -07001802 depth_bits[0] = 16;
1803 stencil_bits[0] = 0;
1804 } else {
1805 depth_bits[0] = 24;
1806 stencil_bits[0] = 8;
1807 }
1808
Ian Romanick1f6e10f2012-07-12 13:52:06 -07001809 new_configs = driCreateConfigs(formats[i],
Chad Versaceb2d428c2012-07-12 14:17:22 -07001810 depth_bits, stencil_bits, 1,
Chad Versace7dc0be82012-08-09 09:06:42 -07001811 back_buffer_modes, 1,
Chad Versace8b5d68d2012-08-02 14:51:47 -07001812 singlesample_samples, 1,
Ilia Mirkin52839002016-08-20 16:10:20 -04001813 true, false);
Chad Versacea4bf68c2012-08-01 21:23:47 -07001814 configs = driConcatConfigs(configs, new_configs);
Chad Versaceb2d428c2012-07-12 14:17:22 -07001815 }
1816
Chad Versacee943e5c2012-08-02 17:13:17 -07001817 /* 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
Chad Versace7dc0be82012-08-09 09:06:42 -07001828 * them.
Chad Versacee943e5c2012-08-02 17:13:17 -07001829 */
Rhys Kiddf4ef8d02015-08-06 16:34:03 +10001830 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
Kenneth Graunkeafe05e72013-07-04 12:35:22 -07001831 if (devinfo->gen < 6)
Chad Versacee943e5c2012-08-02 17:13:17 -07001832 break;
1833
1834 __DRIconfig **new_configs;
1835 const int num_depth_stencil_bits = 2;
Eric Anholt3aaeb3e2012-08-07 11:33:10 -07001836 int num_msaa_modes = 0;
Chad Versacee943e5c2012-08-02 17:13:17 -07001837
1838 depth_bits[0] = 0;
1839 stencil_bits[0] = 0;
1840
Mark Muellereeed49f2014-01-26 15:12:56 -08001841 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
Chad Versacee943e5c2012-08-02 17:13:17 -07001842 depth_bits[1] = 16;
1843 stencil_bits[1] = 0;
1844 } else {
1845 depth_bits[1] = 24;
1846 stencil_bits[1] = 8;
1847 }
1848
Kenneth Graunkeafe05e72013-07-04 12:35:22 -07001849 if (devinfo->gen >= 7)
Chad Versacee943e5c2012-08-02 17:13:17 -07001850 num_msaa_modes = 2;
Kenneth Graunkeafe05e72013-07-04 12:35:22 -07001851 else if (devinfo->gen == 6)
Chad Versacee943e5c2012-08-02 17:13:17 -07001852 num_msaa_modes = 1;
1853
Ian Romanick1f6e10f2012-07-12 13:52:06 -07001854 new_configs = driCreateConfigs(formats[i],
Chad Versacee943e5c2012-08-02 17:13:17 -07001855 depth_bits,
1856 stencil_bits,
1857 num_depth_stencil_bits,
Chad Versace7dc0be82012-08-09 09:06:42 -07001858 back_buffer_modes, 1,
Chad Versacee943e5c2012-08-02 17:13:17 -07001859 multisample_samples,
1860 num_msaa_modes,
Ilia Mirkin52839002016-08-20 16:10:20 -04001861 false, false);
Chad Versacee943e5c2012-08-02 17:13:17 -07001862 configs = driConcatConfigs(configs, new_configs);
1863 }
1864
Chad Versaceb2d428c2012-07-12 14:17:22 -07001865 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
Chad Versace4945086f32012-11-21 15:08:27 -08001874static void
1875set_max_gl_versions(struct intel_screen *screen)
1876{
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001877 __DRIscreen *dri_screen = screen->driScrnPriv;
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001878 const bool has_astc = screen->devinfo.gen >= 9;
Jordan Justenfde59a22013-02-21 16:59:33 -08001879
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001880 switch (screen->devinfo.gen) {
Anuj Phogat111881a2017-05-16 10:15:17 -07001881 case 10:
Jordan Justene8137282014-04-20 17:31:30 -07001882 case 9:
Kenneth Graunke232140a2013-11-01 11:45:47 -07001883 case 8:
Kenneth Graunke75128d62016-10-14 16:33:19 -07001884 dri_screen->max_gl_core_version = 45;
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001885 dri_screen->max_gl_compat_version = 30;
1886 dri_screen->max_gl_es1_version = 11;
Kenneth Graunkea53da572016-09-20 20:33:54 -07001887 dri_screen->max_gl_es2_version = has_astc ? 32 : 31;
Jordan Justene97b2072015-12-15 15:53:20 -08001888 break;
Chad Versace4945086f32012-11-21 15:08:27 -08001889 case 7:
Kenneth Graunke9b324e42017-03-02 11:33:37 -08001890 dri_screen->max_gl_core_version = 33;
Samuel Iglesias Gonsálveza494afd2016-08-26 07:39:04 +02001891 if (can_do_pipelined_register_writes(screen)) {
Juan A. Suarez Romero18779822017-03-29 11:41:35 +02001892 dri_screen->max_gl_core_version = 42;
Samuel Iglesias Gonsálveza494afd2016-08-26 07:39:04 +02001893 if (screen->devinfo.is_haswell && can_do_compute_dispatch(screen))
Kenneth Graunke9b324e42017-03-02 11:33:37 -08001894 dri_screen->max_gl_core_version = 43;
Samuel Iglesias Gonsálveza494afd2016-08-26 07:39:04 +02001895 if (screen->devinfo.is_haswell && can_do_mi_math_and_lrr(screen))
Kenneth Graunke9b324e42017-03-02 11:33:37 -08001896 dri_screen->max_gl_core_version = 45;
1897 }
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001898 dri_screen->max_gl_compat_version = 30;
1899 dri_screen->max_gl_es1_version = 11;
Lionel Landwerlinbc245902016-09-22 14:58:11 +03001900 dri_screen->max_gl_es2_version = screen->devinfo.is_haswell ? 31 : 30;
Jordan Justen93f5eb72016-06-08 13:17:41 -07001901 break;
Chad Versace4945086f32012-11-21 15:08:27 -08001902 case 6:
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001903 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;
Chad Versace4945086f32012-11-21 15:08:27 -08001907 break;
1908 case 5:
1909 case 4:
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08001910 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;
Chad Versace4945086f32012-11-21 15:08:27 -08001914 break;
Chad Versace4945086f32012-11-21 15:08:27 -08001915 default:
Matt Turner3d826722014-06-29 14:54:01 -07001916 unreachable("unrecognized intel_screen::gen");
Chad Versace4945086f32012-11-21 15:08:27 -08001917 }
Chad Versace4945086f32012-11-21 15:08:27 -08001918}
1919
Ben Widawsky9ecfc6b2015-10-23 14:38:39 -07001920/**
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 */
1929static __attribute__((__unused__)) int
Jason Ekstrand38dc2dd2015-04-16 17:52:03 -07001930brw_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
Jason Ekstrand870ff6c2016-05-25 18:19:50 -07001947static void
1948shader_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
1962static void
1963shader_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
Kenneth Graunke1dc02da2017-04-04 11:45:08 -07001987static int
1988parse_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 },
Anuj Phogatdd6c27a2017-04-17 12:55:19 -07002004 { "cnl", 0x5a52 },
Kenneth Graunke1dc02da2017-04-04 11:45:08 -07002005 };
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 */
2021static int
2022get_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
Chad Versace6b2bf272011-05-26 15:24:48 -07002035/**
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002036 * This is the driver specific part of the createNewScreen entry point.
Brian Paul8d976ae2008-06-11 19:33:14 -06002037 * Called when using DRI2.
2038 *
Kristian Høgsbergd3491e72010-10-12 11:58:47 -04002039 * \return the struct gl_config supported by this driver
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002040 */
Kristian Høgsberge82dd8c2008-03-26 19:26:59 -04002041static const
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08002042__DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002043{
Kenneth Graunke9694b232015-11-30 15:47:13 -08002044 struct intel_screen *screen;
Kristian Høgsberg7c50d292010-01-08 12:35:47 -05002045
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08002046 if (dri_screen->image.loader) {
2047 } else if (dri_screen->dri2.loader->base.version <= 2 ||
2048 dri_screen->dri2.loader->getBuffersWithFormat == NULL) {
Eric Anholt1b4374d2012-07-04 10:52:34 -07002049 fprintf(stderr,
2050 "\nERROR! DRI2 loader with getBuffersWithFormat() "
2051 "support required\n");
Kenneth Graunke77829362017-03-01 16:02:58 -08002052 return NULL;
Eric Anholt1b4374d2012-07-04 10:52:34 -07002053 }
2054
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002055 /* Allocate the private area */
Kenneth Graunke9694b232015-11-30 15:47:13 -08002056 screen = rzalloc(NULL, struct intel_screen);
2057 if (!screen) {
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002058 fprintf(stderr, "\nERROR! Allocating private area failed\n");
Kenneth Graunke77829362017-03-01 16:02:58 -08002059 return NULL;
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002060 }
2061 /* parse information in __driConfigOptions */
Kenneth Graunke9694b232015-11-30 15:47:13 -08002062 driParseOptionInfo(&screen->optionCache, brw_config_options.xml);
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002063
Kenneth Graunke9694b232015-11-30 15:47:13 -08002064 screen->driScrnPriv = dri_screen;
2065 dri_screen->driverPrivate = (void *) screen;
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002066
Kenneth Graunke1dc02da2017-04-04 11:45:08 -07002067 screen->deviceID = get_pci_device_id(screen);
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002068
Lionel Landwerlinbc245902016-09-22 14:58:11 +03002069 if (!gen_get_device_info(screen->deviceID, &screen->devinfo))
Kenneth Graunke77829362017-03-01 16:02:58 -08002070 return NULL;
Eric Anholt4ac2f092010-12-02 18:25:45 -08002071
Kenneth Graunke1dc02da2017-04-04 11:45:08 -07002072 if (!intel_init_bufmgr(screen))
2073 return NULL;
2074
Kenneth Graunkeb5b123a2017-03-01 15:52:55 -08002075 const struct gen_device_info *devinfo = &screen->devinfo;
2076
Kristian Høgsberg Kristensen99ca2252015-10-06 16:19:04 -07002077 brw_process_intel_debug_variable();
2078
Kenneth Graunkeb5b123a2017-03-01 15:52:55 -08002079 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && devinfo->gen < 7) {
Kristian Høgsberg Kristensen99ca2252015-10-06 16:19:04 -07002080 fprintf(stderr,
2081 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
2082 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
2083 }
2084
Kenneth Graunke9694b232015-11-30 15:47:13 -08002085 if (intel_get_integer(screen, I915_PARAM_MMAP_GTT_VERSION) >= 1) {
Chris Wilsonf92a87a2016-08-24 20:35:46 +01002086 /* 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
Kenneth Graunked30a9272017-04-03 20:13:08 -07002091 * one that exceeds the limit with fail in brw_bo_map_gtt().
Chris Wilsonf92a87a2016-08-24 20:35:46 +01002092 *
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 */
Kenneth Graunke9694b232015-11-30 15:47:13 -08002101 screen->max_gtt_map_object_size = UINT64_MAX;
Chris Wilsonf92a87a2016-08-24 20:35:46 +01002102 } 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 */
Kenneth Graunke9694b232015-11-30 15:47:13 -08002116 screen->max_gtt_map_object_size = gtt_size / 4;
Chris Wilsonf92a87a2016-08-24 20:35:46 +01002117 }
2118
Kenneth Graunke63682842017-03-30 22:27:42 -07002119 screen->aperture_threshold = get_aperture_size(dri_screen->fd) * 3 / 4;
2120
Kenneth Graunke9694b232015-11-30 15:47:13 -08002121 screen->hw_has_swizzling = intel_detect_swizzling(screen);
2122 screen->hw_has_timestamp = intel_detect_timestamp(screen);
Daniel Vetterf172eae2012-03-02 21:38:44 +01002123
Ben Widawskycc01b632016-04-07 10:53:13 -07002124 /* GENs prior to 8 do not support EU/Subslice info */
Kenneth Graunkeb5b123a2017-03-01 15:52:55 -08002125 if (devinfo->gen >= 8) {
Kenneth Graunke9694b232015-11-30 15:47:13 -08002126 intel_detect_sseu(screen);
Kenneth Graunkeb5b123a2017-03-01 15:52:55 -08002127 } else if (devinfo->gen == 7) {
2128 screen->subslice_total = 1 << (devinfo->gt - 1);
Kenneth Graunke9cd8f952016-06-08 23:36:16 -07002129 }
Ben Widawskycc01b632016-04-07 10:53:13 -07002130
Kenneth Graunke4a2ad6b2017-03-02 18:12:28 -08002131 /* Gen7-7.5 kernel requirements / command parser saga:
2132 *
2133 * - pre-v3.16:
2134 * Haswell and Baytrail cannot use any privileged batchbuffer features.
2135 *
2136 * Ivybridge has aliasing PPGTT on by default, which accidentally marks
2137 * all batches secure, allowing them to use any feature with no checking.
2138 * This is effectively equivalent to a command parser version of
2139 * \infinity - everything is possible.
2140 *
2141 * The command parser does not exist, and querying the version will
2142 * return -EINVAL.
2143 *
2144 * - v3.16:
2145 * The kernel enables the command parser by default, for systems with
2146 * aliasing PPGTT enabled (Ivybridge and Haswell). However, the
2147 * hardware checker is still enabled, so Haswell and Baytrail cannot
2148 * do anything.
2149 *
2150 * Ivybridge goes from "everything is possible" to "only what the
2151 * command parser allows" (if the user boots with i915.cmd_parser=0,
2152 * then everything is possible again). We can only safely use features
2153 * allowed by the supported command parser version.
2154 *
2155 * Annoyingly, I915_PARAM_CMD_PARSER_VERSION reports the static version
2156 * implemented by the kernel, even if it's turned off. So, checking
2157 * for version > 0 does not mean that you can write registers. We have
2158 * to try it and see. The version does, however, indicate the age of
2159 * the kernel.
2160 *
2161 * Instead of matching the hardware checker's behavior of converting
2162 * privileged commands to MI_NOOP, it makes execbuf2 start returning
2163 * -EINVAL, making it dangerous to try and use privileged features.
2164 *
2165 * Effective command parser versions:
2166 * - Haswell: 0 (reporting 1, writes don't work)
2167 * - Baytrail: 0 (reporting 1, writes don't work)
2168 * - Ivybridge: 1 (enabled) or infinite (disabled)
2169 *
2170 * - v3.17:
2171 * Baytrail aliasing PPGTT is enabled, making it like Ivybridge:
2172 * effectively version 1 (enabled) or infinite (disabled).
2173 *
2174 * - v3.19: f1f55cc0556031c8ee3fe99dae7251e78b9b653b
2175 * Command parser v2 supports predicate writes.
2176 *
2177 * - Haswell: 0 (reporting 1, writes don't work)
2178 * - Baytrail: 2 (enabled) or infinite (disabled)
2179 * - Ivybridge: 2 (enabled) or infinite (disabled)
2180 *
2181 * So version >= 2 is enough to know that Ivybridge and Baytrail
2182 * will work. Haswell still can't do anything.
2183 *
2184 * - v4.0: Version 3 happened. Largely not relevant.
2185 *
2186 * - v4.1: 6702cf16e0ba8b0129f5aa1b6609d4e9c70bc13b
2187 * L3 config registers are properly saved and restored as part
2188 * of the hardware context. We can approximately detect this point
2189 * in time by checking if I915_PARAM_REVISION is recognized - it
2190 * landed in a later commit, but in the same release cycle.
2191 *
2192 * - v4.2: 245054a1fe33c06ad233e0d58a27ec7b64db9284
2193 * Command parser finally gains secure batch promotion. On Haswell,
2194 * the hardware checker gets disabled, which finally allows it to do
2195 * privileged commands.
2196 *
2197 * I915_PARAM_CMD_PARSER_VERSION reports 3. Effective versions:
2198 * - Haswell: 3 (enabled) or 0 (disabled)
2199 * - Baytrail: 3 (enabled) or infinite (disabled)
2200 * - Ivybridge: 3 (enabled) or infinite (disabled)
2201 *
2202 * Unfortunately, detecting this point in time is tricky, because
2203 * no version bump happened when this important change occurred.
2204 * On Haswell, if we can write any register, then the kernel is at
2205 * least this new, and we can start trusting the version number.
2206 *
2207 * - v4.4: 2bbe6bbb0dc94fd4ce287bdac9e1bd184e23057b and
2208 * Command parser reaches version 4, allowing access to Haswell
2209 * atomic scratch and chicken3 registers. If version >= 4, we know
2210 * the kernel is new enough to support privileged features on all
2211 * hardware. However, the user might have disabled it...and the
2212 * kernel will still report version 4. So we still have to guess
2213 * and check.
2214 *
2215 * - v4.4: 7b9748cb513a6bef4af87b79f0da3ff7e8b56cd8
2216 * Command parser v5 whitelists indirect compute shader dispatch
2217 * registers, needed for OpenGL 4.3 and later.
2218 *
2219 * - v4.8:
2220 * Command parser v7 lets us use MI_MATH on Haswell.
2221 *
2222 * Additionally, the kernel begins reporting version 0 when
2223 * the command parser is disabled, allowing us to skip the
2224 * guess-and-check step on Haswell. Unfortunately, this also
2225 * means that we can no longer use it as an indicator of the
2226 * age of the kernel.
2227 */
Kenneth Graunke31693a12017-03-02 18:21:31 -08002228 if (intel_get_param(screen, I915_PARAM_CMD_PARSER_VERSION,
2229 &screen->cmd_parser_version) < 0) {
2230 /* Command parser does not exist - getparam is unrecognized */
2231 screen->cmd_parser_version = 0;
2232 }
2233
Matt Turner8ca8ebb2017-04-25 10:00:19 -07002234 /* Kernel 4.13 retuired for exec object capture */
2235#ifndef I915_PARAM_HAS_EXEC_CAPTURE
2236#define I915_PARAM_HAS_EXEC_CAPTURE 45
2237#endif
2238 if (intel_get_boolean(screen, I915_PARAM_HAS_EXEC_CAPTURE)) {
2239 screen->kernel_features |= KERNEL_ALLOWS_EXEC_CAPTURE;
2240 }
2241
Kenneth Graunke31693a12017-03-02 18:21:31 -08002242 if (!intel_detect_pipelined_so(screen)) {
2243 /* We can't do anything, so the effective version is 0. */
2244 screen->cmd_parser_version = 0;
2245 } else {
Iago Toral Quirogaa98f2e52017-01-04 10:46:08 +01002246 screen->kernel_features |= KERNEL_ALLOWS_SOL_OFFSET_WRITES;
Kenneth Graunke31693a12017-03-02 18:21:31 -08002247 }
Chris Wilson02a44482017-01-04 08:34:59 +01002248
Kenneth Graunke02ccd8f2017-04-11 08:33:20 -07002249 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 2)
2250 screen->kernel_features |= KERNEL_ALLOWS_PREDICATE_WRITES;
2251
2252 /* Haswell requires command parser version 4 in order to have L3
2253 * atomic scratch1 and chicken3 bits
2254 */
2255 if (devinfo->is_haswell && screen->cmd_parser_version >= 4) {
2256 screen->kernel_features |=
2257 KERNEL_ALLOWS_HSW_SCRATCH1_AND_ROW_CHICKEN3;
2258 }
2259
2260 /* Haswell requires command parser version 6 in order to write to the
2261 * MI_MATH GPR registers, and version 7 in order to use
2262 * MI_LOAD_REGISTER_REG (which all users of MI_MATH use).
2263 */
2264 if (devinfo->gen >= 8 ||
2265 (devinfo->is_haswell && screen->cmd_parser_version >= 7)) {
2266 screen->kernel_features |= KERNEL_ALLOWS_MI_MATH_AND_LRR;
2267 }
2268
2269 /* Gen7 needs at least command parser version 5 to support compute */
2270 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 5)
2271 screen->kernel_features |= KERNEL_ALLOWS_COMPUTE_DISPATCH;
2272
Eric Anholt41033502014-03-21 16:36:22 -07002273 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
2274 if (force_msaa) {
Kenneth Graunke9694b232015-11-30 15:47:13 -08002275 screen->winsys_msaa_samples_override =
2276 intel_quantize_num_samples(screen, atoi(force_msaa));
Eric Anholt41033502014-03-21 16:36:22 -07002277 printf("Forcing winsys sample count to %d\n",
Kenneth Graunke9694b232015-11-30 15:47:13 -08002278 screen->winsys_msaa_samples_override);
Eric Anholt41033502014-03-21 16:36:22 -07002279 } else {
Kenneth Graunke9694b232015-11-30 15:47:13 -08002280 screen->winsys_msaa_samples_override = -1;
Eric Anholt41033502014-03-21 16:36:22 -07002281 }
2282
Kenneth Graunke9694b232015-11-30 15:47:13 -08002283 set_max_gl_versions(screen);
Chad Versace4945086f32012-11-21 15:08:27 -08002284
Ian Romanick9b1c6862013-11-19 17:01:23 -08002285 /* Notification of GPU resets requires hardware contexts and a kernel new
2286 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
2287 * supported, calling it with a context of 0 will either generate EPERM or
2288 * no error. If the ioctl is not supported, it always generate EINVAL.
2289 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
2290 * extension to the loader.
Kenneth Graunke0380ec42014-03-12 01:43:40 -07002291 *
2292 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
Ian Romanick9b1c6862013-11-19 17:01:23 -08002293 */
Kenneth Graunkeb5b123a2017-03-01 15:52:55 -08002294 if (devinfo->gen >= 6) {
Kenneth Graunke0380ec42014-03-12 01:43:40 -07002295 struct drm_i915_reset_stats stats;
2296 memset(&stats, 0, sizeof(stats));
Ian Romanick9b1c6862013-11-19 17:01:23 -08002297
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08002298 const int ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
Ian Romanick9b1c6862013-11-19 17:01:23 -08002299
Kenneth Graunke9694b232015-11-30 15:47:13 -08002300 screen->has_context_reset_notification =
Kenneth Graunke0380ec42014-03-12 01:43:40 -07002301 (ret != -1 || errno != EINVAL);
2302 }
Ian Romanick53a65e52013-11-26 16:27:57 -08002303
Kenneth Graunke9694b232015-11-30 15:47:13 -08002304 dri_screen->extensions = !screen->has_context_reset_notification
2305 ? screenExtensions : intelRobustScreenExtensions;
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002306
Kenneth Graunkeb5b123a2017-03-01 15:52:55 -08002307 screen->compiler = brw_compiler_create(screen, devinfo);
Kenneth Graunke9694b232015-11-30 15:47:13 -08002308 screen->compiler->shader_debug_log = shader_debug_log_mesa;
2309 screen->compiler->shader_perf_log = shader_perf_log_mesa;
2310 screen->program_id = 1;
Kenneth Graunke7a0fd3c2014-03-17 13:53:44 -07002311
Chad Versace358661c2017-01-13 10:46:48 -08002312 screen->has_exec_fence =
2313 intel_get_boolean(screen, I915_PARAM_HAS_EXEC_FENCE);
2314
Chad Versace4b9cbfa2017-05-30 15:57:15 -07002315 intel_screen_init_surface_formats(screen);
2316
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08002317 return (const __DRIconfig**) intel_screen_make_configs(dri_screen);
Kristian Høgsbergc5c73c12008-01-21 17:07:33 -05002318}
Kristian Høgsberge82dd8c2008-03-26 19:26:59 -04002319
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002320struct intel_buffer {
2321 __DRIbuffer base;
Kenneth Graunked30a9272017-04-03 20:13:08 -07002322 struct brw_bo *bo;
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002323};
2324
2325static __DRIbuffer *
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08002326intelAllocateBuffer(__DRIscreen *dri_screen,
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002327 unsigned attachment, unsigned format,
2328 int width, int height)
2329{
2330 struct intel_buffer *intelBuffer;
Kenneth Graunke9694b232015-11-30 15:47:13 -08002331 struct intel_screen *screen = dri_screen->driverPrivate;
Chad Versace79653c12011-11-15 07:08:49 -08002332
Chad Versace83fa0842012-07-09 15:51:06 -07002333 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
2334 attachment == __DRI_BUFFER_BACK_LEFT);
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002335
Brian Paul4fdac652012-09-01 07:47:24 -06002336 intelBuffer = calloc(1, sizeof *intelBuffer);
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002337 if (intelBuffer == NULL)
2338 return NULL;
2339
Ben Widawsky7ce04052016-11-04 12:34:40 -07002340 /* The front and back buffers are color buffers, which are X tiled. GEN9+
2341 * supports Y tiled and compressed buffers, but there is no way to plumb that
2342 * through to here. */
Kenneth Graunke444ab812017-04-10 23:08:23 -07002343 uint32_t pitch;
Eric Anholt3278f962014-04-25 13:44:41 -07002344 int cpp = format / 8;
Jason Ekstrand6ee05302017-06-12 09:35:22 -07002345 intelBuffer->bo = brw_bo_alloc_tiled_2d(screen->bufmgr,
2346 "intelAllocateBuffer",
2347 width,
2348 height,
2349 cpp,
2350 I915_TILING_X, &pitch,
2351 BO_ALLOC_FOR_RENDER);
Kenneth Graunkea7bdd4c2013-11-25 15:46:34 -08002352
Eric Anholt3278f962014-04-25 13:44:41 -07002353 if (intelBuffer->bo == NULL) {
Brian Paulfe72a062012-09-01 07:47:24 -06002354 free(intelBuffer);
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002355 return NULL;
2356 }
Kenneth Graunkea7bdd4c2013-11-25 15:46:34 -08002357
Kenneth Graunked30a9272017-04-03 20:13:08 -07002358 brw_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002359
2360 intelBuffer->base.attachment = attachment;
Eric Anholt3278f962014-04-25 13:44:41 -07002361 intelBuffer->base.cpp = cpp;
2362 intelBuffer->base.pitch = pitch;
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002363
2364 return &intelBuffer->base;
2365}
2366
2367static void
Kenneth Graunke8fec9fb2015-11-30 16:04:08 -08002368intelReleaseBuffer(__DRIscreen *dri_screen, __DRIbuffer *buffer)
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002369{
2370 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
2371
Kenneth Graunked30a9272017-04-03 20:13:08 -07002372 brw_bo_unreference(intelBuffer->bo);
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002373 free(intelBuffer);
2374}
2375
Eric Anholt1925a9a2013-06-26 13:04:51 -07002376static const struct __DriverAPIRec brw_driver_api = {
George Sapountzis7192c372011-11-03 12:46:08 +02002377 .InitScreen = intelInitScreen2,
Kristian Høgsberge82dd8c2008-03-26 19:26:59 -04002378 .DestroyScreen = intelDestroyScreen,
Eric Anholtee8983b2013-09-26 17:08:28 -07002379 .CreateContext = brwCreateContext,
Kristian Høgsberge82dd8c2008-03-26 19:26:59 -04002380 .DestroyContext = intelDestroyContext,
2381 .CreateBuffer = intelCreateBuffer,
2382 .DestroyBuffer = intelDestroyBuffer,
Kristian Høgsberge82dd8c2008-03-26 19:26:59 -04002383 .MakeCurrent = intelMakeCurrent,
2384 .UnbindContext = intelUnbindContext,
Benjamin Franzke2adfde32011-02-04 12:01:31 +01002385 .AllocateBuffer = intelAllocateBuffer,
2386 .ReleaseBuffer = intelReleaseBuffer
Kristian Høgsberge82dd8c2008-03-26 19:26:59 -04002387};
Kristian Høgsberg39a0e4e2010-01-01 17:56:29 -05002388
Eric Anholt1925a9a2013-06-26 13:04:51 -07002389static const struct __DRIDriverVtableExtensionRec brw_vtable = {
2390 .base = { __DRI_DRIVER_VTABLE, 1 },
2391 .vtable = &brw_driver_api,
2392};
2393
2394static const __DRIextension *brw_driver_extensions[] = {
Kristian Høgsberg39a0e4e2010-01-01 17:56:29 -05002395 &driCoreExtension.base,
Keith Packard44244202013-11-04 18:09:51 -08002396 &driImageDriverExtension.base,
Kristian Høgsberg39a0e4e2010-01-01 17:56:29 -05002397 &driDRI2Extension.base,
Eric Anholt1925a9a2013-06-26 13:04:51 -07002398 &brw_vtable.base,
Eric Anholt68689232013-09-27 15:25:40 -07002399 &brw_config_options.base,
Kristian Høgsberg39a0e4e2010-01-01 17:56:29 -05002400 NULL
2401};
Eric Anholt1925a9a2013-06-26 13:04:51 -07002402
2403PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
2404{
2405 globalDriverAPI = &brw_driver_api;
2406
2407 return brw_driver_extensions;
2408}