blob: 9846313a2352560a8bb7f3b1c88e323da226ee3b [file] [log] [blame]
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001/*
2 * Copyright 2017 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Jason Macnak1de7f662020-01-24 15:05:57 -08007#include <assert.h>
Zach Reizner85c4c5f2017-10-04 13:15:57 -07008#include <errno.h>
David Stevens0fe561f2020-10-28 16:06:38 +09009#include <stdatomic.h>
Zach Reizner85c4c5f2017-10-04 13:15:57 -070010#include <stdint.h>
Zach Reizner85c4c5f2017-10-04 13:15:57 -070011#include <string.h>
12#include <sys/mman.h>
Zach Reizner85c4c5f2017-10-04 13:15:57 -070013#include <xf86drm.h>
14
Yiwei Zhangb7a64442021-09-30 05:13:10 +000015#include "drv_helpers.h"
Zach Reizner85c4c5f2017-10-04 13:15:57 -070016#include "drv_priv.h"
Gurchetan Singh9f3110b2020-04-03 15:15:30 -070017#include "external/virgl_hw.h"
18#include "external/virgl_protocol.h"
19#include "external/virtgpu_drm.h"
Zach Reizner85c4c5f2017-10-04 13:15:57 -070020#include "util.h"
Gurchetan Singh73c141e2021-01-21 14:51:19 -080021#include "virtgpu.h"
Zach Reizner85c4c5f2017-10-04 13:15:57 -070022
Zach Reizner85c4c5f2017-10-04 13:15:57 -070023#define PIPE_TEXTURE_2D 2
24
Jason Macnakd6666c82021-09-29 11:13:25 -070025#define MESA_LLVMPIPE_MAX_TEXTURE_2D_LEVELS 15
26#define MESA_LLVMPIPE_MAX_TEXTURE_2D_SIZE (1 << (MESA_LLVMPIPE_MAX_TEXTURE_2D_LEVELS - 1))
Lepton Wu249e8632018-04-05 12:50:03 -070027#define MESA_LLVMPIPE_TILE_ORDER 6
28#define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
29
Jason Macnakc06cc9c2021-10-06 10:16:19 -070030// This comes from a combination of SwiftShader's VkPhysicalDeviceLimits::maxFramebufferWidth and
31// VkPhysicalDeviceLimits::maxImageDimension2D (see https://crrev.com/c/1917130).
32#define ANGLE_ON_SWIFTSHADER_MAX_TEXTURE_2D_SIZE 8192
33
34#ifndef MIN
35#define MIN(a, b) ((a) < (b) ? (a) : (b))
36#endif
37#define VIRGL_2D_MAX_TEXTURE_2D_SIZE \
38 MIN(ANGLE_ON_SWIFTSHADER_MAX_TEXTURE_2D_SIZE, MESA_LLVMPIPE_MAX_TEXTURE_2D_SIZE)
39
Zach Reizner85c4c5f2017-10-04 13:15:57 -070040static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070041 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
42 DRM_FORMAT_XRGB8888 };
Zach Reizner85c4c5f2017-10-04 13:15:57 -070043
Jason Macnak1de7f662020-01-24 15:05:57 -080044static const uint32_t dumb_texture_source_formats[] = {
Yiwei Zhang35aa91b2021-09-17 22:14:11 +000045 DRM_FORMAT_R8, DRM_FORMAT_R16, DRM_FORMAT_YVU420,
46 DRM_FORMAT_NV12, DRM_FORMAT_NV21, DRM_FORMAT_YVU420_ANDROID,
47 DRM_FORMAT_ABGR2101010, DRM_FORMAT_ABGR16161616F
Jason Macnak1de7f662020-01-24 15:05:57 -080048};
Lepton Wu249e8632018-04-05 12:50:03 -070049
Yiwei Zhang35aa91b2021-09-17 22:14:11 +000050static const uint32_t texture_source_formats[] = {
Robert Tarasov9b276572022-06-01 20:52:18 +000051 DRM_FORMAT_NV12, DRM_FORMAT_NV21, DRM_FORMAT_R8,
52 DRM_FORMAT_R16, DRM_FORMAT_RG88, DRM_FORMAT_YVU420_ANDROID,
53 DRM_FORMAT_ABGR2101010, DRM_FORMAT_ABGR16161616F
Yiwei Zhang35aa91b2021-09-17 22:14:11 +000054};
Zach Reizner85c4c5f2017-10-04 13:15:57 -070055
Gurchetan Singh73c141e2021-01-21 14:51:19 -080056extern struct virtgpu_param params[];
57
58struct virgl_priv {
Lepton Wueebce652020-02-26 15:13:34 -080059 int caps_is_v2;
Jason Macnakddf4ec02020-02-03 16:36:46 -080060 union virgl_caps caps;
Jason Macnak1de7f662020-01-24 15:05:57 -080061 int host_gbm_enabled;
David Stevens0fe561f2020-10-28 16:06:38 +090062 atomic_int next_blob_id;
Lepton Wu249e8632018-04-05 12:50:03 -070063};
64
Kansho Nishidad97877b2019-06-14 18:28:18 +090065static uint32_t translate_format(uint32_t drm_fourcc)
Zach Reizner85c4c5f2017-10-04 13:15:57 -070066{
67 switch (drm_fourcc) {
Jason Macnak1de7f662020-01-24 15:05:57 -080068 case DRM_FORMAT_BGR888:
69 case DRM_FORMAT_RGB888:
70 return VIRGL_FORMAT_R8G8B8_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070071 case DRM_FORMAT_XRGB8888:
72 return VIRGL_FORMAT_B8G8R8X8_UNORM;
73 case DRM_FORMAT_ARGB8888:
74 return VIRGL_FORMAT_B8G8R8A8_UNORM;
75 case DRM_FORMAT_XBGR8888:
76 return VIRGL_FORMAT_R8G8B8X8_UNORM;
77 case DRM_FORMAT_ABGR8888:
78 return VIRGL_FORMAT_R8G8B8A8_UNORM;
Jason Macnak1de7f662020-01-24 15:05:57 -080079 case DRM_FORMAT_ABGR16161616F:
Lepton Wufef113c2020-10-30 16:29:26 -070080 return VIRGL_FORMAT_R16G16B16A16_FLOAT;
Nataraj Deshpande450e5762021-06-30 12:10:55 -070081 case DRM_FORMAT_ABGR2101010:
82 return VIRGL_FORMAT_R10G10B10A2_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070083 case DRM_FORMAT_RGB565:
84 return VIRGL_FORMAT_B5G6R5_UNORM;
85 case DRM_FORMAT_R8:
86 return VIRGL_FORMAT_R8_UNORM;
Jason Macnak6e200ea2021-02-11 19:34:57 -080087 case DRM_FORMAT_R16:
88 return VIRGL_FORMAT_R16_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070089 case DRM_FORMAT_RG88:
90 return VIRGL_FORMAT_R8G8_UNORM;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -070091 case DRM_FORMAT_NV12:
92 return VIRGL_FORMAT_NV12;
Jason Macnak1de7f662020-01-24 15:05:57 -080093 case DRM_FORMAT_NV21:
94 return VIRGL_FORMAT_NV21;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -070095 case DRM_FORMAT_YVU420:
96 case DRM_FORMAT_YVU420_ANDROID:
97 return VIRGL_FORMAT_YV12;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070098 default:
Jason Macnak6e200ea2021-02-11 19:34:57 -080099 drv_log("Unhandled format:%d\n", drm_fourcc);
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700100 return 0;
101 }
102}
103
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800104static bool virgl_bitmask_supports_format(struct virgl_supported_format_mask *supported,
105 uint32_t drm_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800106{
107 uint32_t virgl_format = translate_format(drm_format);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800108 if (!virgl_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800109 return false;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800110
111 uint32_t bitmask_index = virgl_format / 32;
112 uint32_t bit_index = virgl_format % 32;
113 return supported->bitmask[bitmask_index] & (1 << bit_index);
114}
115
Jason Macnak1de7f662020-01-24 15:05:57 -0800116// The metadata generated here for emulated buffers is slightly different than the metadata
117// generated by drv_bo_from_format. In order to simplify transfers in the flush and invalidate
118// functions below, the emulated buffers are oversized. For example, ignoring stride alignment
119// requirements to demonstrate, a 6x6 YUV420 image buffer might have the following layout from
120// drv_bo_from_format:
121//
122// | Y | Y | Y | Y | Y | Y |
123// | Y | Y | Y | Y | Y | Y |
124// | Y | Y | Y | Y | Y | Y |
125// | Y | Y | Y | Y | Y | Y |
126// | Y | Y | Y | Y | Y | Y |
127// | Y | Y | Y | Y | Y | Y |
128// | U | U | U | U | U | U |
129// | U | U | U | V | V | V |
130// | V | V | V | V | V | V |
131//
132// where each plane immediately follows the previous plane in memory. This layout makes it
133// difficult to compute the transfers needed for example when the middle 2x2 region of the
134// image is locked and needs to be flushed/invalidated.
135//
136// Emulated multi-plane buffers instead have a layout of:
137//
138// | Y | Y | Y | Y | Y | Y |
139// | Y | Y | Y | Y | Y | Y |
140// | Y | Y | Y | Y | Y | Y |
141// | Y | Y | Y | Y | Y | Y |
142// | Y | Y | Y | Y | Y | Y |
143// | Y | Y | Y | Y | Y | Y |
144// | U | U | U | | | |
145// | U | U | U | | | |
146// | U | U | U | | | |
147// | V | V | V | | | |
148// | V | V | V | | | |
149// | V | V | V | | | |
150//
151// where each plane is placed as a sub-image (albeit with a very large stride) in order to
152// simplify transfers into 3 sub-image transfers for the above example.
153//
154// Additional note: the V-plane is not placed to the right of the U-plane due to some
155// observed failures in media framework code which assumes the V-plane is not
156// "row-interlaced" with the U-plane.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800157static void virgl_get_emulated_metadata(const struct bo *bo, struct bo_metadata *metadata)
Jason Macnak1de7f662020-01-24 15:05:57 -0800158{
159 uint32_t y_plane_height;
160 uint32_t c_plane_height;
161 uint32_t original_width = bo->meta.width;
162 uint32_t original_height = bo->meta.height;
163
164 metadata->format = DRM_FORMAT_R8;
165 switch (bo->meta.format) {
166 case DRM_FORMAT_NV12:
167 case DRM_FORMAT_NV21:
168 // Bi-planar
169 metadata->num_planes = 2;
170
171 y_plane_height = original_height;
172 c_plane_height = DIV_ROUND_UP(original_height, 2);
173
174 metadata->width = original_width;
175 metadata->height = y_plane_height + c_plane_height;
176
177 // Y-plane (full resolution)
178 metadata->strides[0] = metadata->width;
179 metadata->offsets[0] = 0;
180 metadata->sizes[0] = metadata->width * y_plane_height;
181
182 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
183 metadata->strides[1] = metadata->width;
184 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
185 metadata->sizes[1] = metadata->width * c_plane_height;
186
187 metadata->total_size = metadata->width * metadata->height;
188 break;
189 case DRM_FORMAT_YVU420:
190 case DRM_FORMAT_YVU420_ANDROID:
191 // Tri-planar
192 metadata->num_planes = 3;
193
194 y_plane_height = original_height;
195 c_plane_height = DIV_ROUND_UP(original_height, 2);
196
197 metadata->width = ALIGN(original_width, 32);
198 metadata->height = y_plane_height + (2 * c_plane_height);
199
200 // Y-plane (full resolution)
201 metadata->strides[0] = metadata->width;
202 metadata->offsets[0] = 0;
203 metadata->sizes[0] = metadata->width * original_height;
204
205 // Cb-plane (half resolution, placed below Y-plane)
206 metadata->strides[1] = metadata->width;
207 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
208 metadata->sizes[1] = metadata->width * c_plane_height;
209
210 // Cr-plane (half resolution, placed below Cb-plane)
211 metadata->strides[2] = metadata->width;
212 metadata->offsets[2] = metadata->offsets[1] + metadata->sizes[1];
213 metadata->sizes[2] = metadata->width * c_plane_height;
214
215 metadata->total_size = metadata->width * metadata->height;
216 break;
217 default:
218 break;
219 }
220}
221
222struct virtio_transfers_params {
223 size_t xfers_needed;
224 struct rectangle xfer_boxes[DRV_MAX_PLANES];
225};
226
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800227static void virgl_get_emulated_transfers_params(const struct bo *bo,
228 const struct rectangle *transfer_box,
229 struct virtio_transfers_params *xfer_params)
Jason Macnak1de7f662020-01-24 15:05:57 -0800230{
231 uint32_t y_plane_height;
232 uint32_t c_plane_height;
233 struct bo_metadata emulated_metadata;
234
235 if (transfer_box->x == 0 && transfer_box->y == 0 && transfer_box->width == bo->meta.width &&
236 transfer_box->height == bo->meta.height) {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800237 virgl_get_emulated_metadata(bo, &emulated_metadata);
Jason Macnak1de7f662020-01-24 15:05:57 -0800238
239 xfer_params->xfers_needed = 1;
240 xfer_params->xfer_boxes[0].x = 0;
241 xfer_params->xfer_boxes[0].y = 0;
242 xfer_params->xfer_boxes[0].width = emulated_metadata.width;
243 xfer_params->xfer_boxes[0].height = emulated_metadata.height;
244
245 return;
246 }
247
248 switch (bo->meta.format) {
249 case DRM_FORMAT_NV12:
250 case DRM_FORMAT_NV21:
251 // Bi-planar
252 xfer_params->xfers_needed = 2;
253
254 y_plane_height = bo->meta.height;
255 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
256
257 // Y-plane (full resolution)
258 xfer_params->xfer_boxes[0].x = transfer_box->x;
259 xfer_params->xfer_boxes[0].y = transfer_box->y;
260 xfer_params->xfer_boxes[0].width = transfer_box->width;
261 xfer_params->xfer_boxes[0].height = transfer_box->height;
262
263 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
264 xfer_params->xfer_boxes[1].x = transfer_box->x;
265 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
266 xfer_params->xfer_boxes[1].width = transfer_box->width;
267 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
268
269 break;
270 case DRM_FORMAT_YVU420:
271 case DRM_FORMAT_YVU420_ANDROID:
272 // Tri-planar
273 xfer_params->xfers_needed = 3;
274
275 y_plane_height = bo->meta.height;
276 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
277
278 // Y-plane (full resolution)
279 xfer_params->xfer_boxes[0].x = transfer_box->x;
280 xfer_params->xfer_boxes[0].y = transfer_box->y;
281 xfer_params->xfer_boxes[0].width = transfer_box->width;
282 xfer_params->xfer_boxes[0].height = transfer_box->height;
283
284 // Cb-plane (half resolution, placed below Y-plane)
285 xfer_params->xfer_boxes[1].x = transfer_box->x;
286 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
287 xfer_params->xfer_boxes[1].width = DIV_ROUND_UP(transfer_box->width, 2);
288 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
289
290 // Cr-plane (half resolution, placed below Cb-plane)
291 xfer_params->xfer_boxes[2].x = transfer_box->x;
292 xfer_params->xfer_boxes[2].y = transfer_box->y + y_plane_height + c_plane_height;
293 xfer_params->xfer_boxes[2].width = DIV_ROUND_UP(transfer_box->width, 2);
294 xfer_params->xfer_boxes[2].height = DIV_ROUND_UP(transfer_box->height, 2);
295
296 break;
297 }
298}
299
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800300static bool virgl_supports_combination_natively(struct driver *drv, uint32_t drm_format,
301 uint64_t use_flags)
Jason Macnak1de7f662020-01-24 15:05:57 -0800302{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800303 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800304
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800305 if (priv->caps.max_version == 0)
Jason Macnak1de7f662020-01-24 15:05:57 -0800306 return true;
Jason Macnak1de7f662020-01-24 15:05:57 -0800307
308 if ((use_flags & BO_USE_RENDERING) &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800309 !virgl_bitmask_supports_format(&priv->caps.v1.render, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800310 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800311
312 if ((use_flags & BO_USE_TEXTURE) &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800313 !virgl_bitmask_supports_format(&priv->caps.v1.sampler, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800314 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800315
316 if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800317 !virgl_bitmask_supports_format(&priv->caps.v2.scanout, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800318 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800319
320 return true;
321}
322
323// For virtio backends that do not support formats natively (e.g. multi-planar formats are not
324// supported in virglrenderer when gbm is unavailable on the host machine), whether or not the
325// format and usage combination can be handled as a blob (byte buffer).
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800326static bool virgl_supports_combination_through_emulation(struct driver *drv, uint32_t drm_format,
327 uint64_t use_flags)
Jason Macnak1de7f662020-01-24 15:05:57 -0800328{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800329 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800330
331 // Only enable emulation on non-gbm virtio backends.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800332 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800333 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800334
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800335 if (use_flags & (BO_USE_RENDERING | BO_USE_SCANOUT))
Jason Macnak1de7f662020-01-24 15:05:57 -0800336 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800337
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800338 if (!virgl_supports_combination_natively(drv, DRM_FORMAT_R8, use_flags))
Jason Macnak1de7f662020-01-24 15:05:57 -0800339 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800340
341 return drm_format == DRM_FORMAT_NV12 || drm_format == DRM_FORMAT_NV21 ||
342 drm_format == DRM_FORMAT_YVU420 || drm_format == DRM_FORMAT_YVU420_ANDROID;
343}
344
Jason Macnakddf4ec02020-02-03 16:36:46 -0800345// Adds the given buffer combination to the list of supported buffer combinations if the
346// combination is supported by the virtio backend.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800347static void virgl_add_combination(struct driver *drv, uint32_t drm_format,
348 struct format_metadata *metadata, uint64_t use_flags)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800349{
Yiwei Zhang9420ffe2021-09-24 06:24:30 +0000350 if (params[param_3d].value) {
351 if ((use_flags & BO_USE_SCANOUT) &&
352 !virgl_supports_combination_natively(drv, drm_format, BO_USE_SCANOUT)) {
353 drv_log("Strip scanout on format: %d\n", drm_format);
Jason Macnak1de7f662020-01-24 15:05:57 -0800354 use_flags &= ~BO_USE_SCANOUT;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800355 }
356
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800357 if (!virgl_supports_combination_natively(drv, drm_format, use_flags) &&
358 !virgl_supports_combination_through_emulation(drv, drm_format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800359 drv_log("Skipping unsupported combination format:%d\n", drm_format);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800360 return;
361 }
362 }
363
364 drv_add_combination(drv, drm_format, metadata, use_flags);
365}
366
367// Adds each given buffer combination to the list of supported buffer combinations if the
368// combination supported by the virtio backend.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800369static void virgl_add_combinations(struct driver *drv, const uint32_t *drm_formats,
370 uint32_t num_formats, struct format_metadata *metadata,
371 uint64_t use_flags)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800372{
373 uint32_t i;
374
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800375 for (i = 0; i < num_formats; i++)
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800376 virgl_add_combination(drv, drm_formats[i], metadata, use_flags);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800377}
378
Jason Macnakc06cc9c2021-10-06 10:16:19 -0700379static int virgl_2d_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
380 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700381{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700382 if (bo->meta.format != DRM_FORMAT_R8) {
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900383 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
384 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
385 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700386
Dominik Behr6e6dc492019-10-09 15:43:52 -0700387 return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_DUMB32BPP);
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700388}
389
Lepton Wudbab0832019-04-19 12:26:39 -0700390static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
391 uint32_t virgl_bind)
392{
393 if ((*flag) & check_flag) {
394 (*flag) &= ~check_flag;
395 (*bind) |= virgl_bind;
396 }
397}
398
David Stevenscf280482020-12-21 11:43:44 +0900399static uint32_t compute_virgl_bind_flags(uint64_t use_flags, uint32_t format)
Lepton Wudbab0832019-04-19 12:26:39 -0700400{
Kansho Nishidad97877b2019-06-14 18:28:18 +0900401 /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
402 uint32_t bind = VIRGL_BIND_SHARED;
Lepton Wudbab0832019-04-19 12:26:39 -0700403
404 handle_flag(&use_flags, BO_USE_TEXTURE, &bind, VIRGL_BIND_SAMPLER_VIEW);
405 handle_flag(&use_flags, BO_USE_RENDERING, &bind, VIRGL_BIND_RENDER_TARGET);
406 handle_flag(&use_flags, BO_USE_SCANOUT, &bind, VIRGL_BIND_SCANOUT);
David Stevens55a6cf92019-09-03 10:45:33 +0900407 handle_flag(&use_flags, BO_USE_CURSOR, &bind, VIRGL_BIND_CURSOR);
408 handle_flag(&use_flags, BO_USE_LINEAR, &bind, VIRGL_BIND_LINEAR);
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000409 handle_flag(&use_flags, BO_USE_GPU_DATA_BUFFER, &bind, VIRGL_BIND_LINEAR);
Yiwei Zhangd3a73ff2021-07-08 05:48:01 +0000410 handle_flag(&use_flags, BO_USE_FRONT_RENDERING, &bind, VIRGL_BIND_LINEAR);
David Stevens55a6cf92019-09-03 10:45:33 +0900411
David Stevens23de4e22020-05-15 14:15:35 +0900412 if (use_flags & BO_USE_PROTECTED) {
413 handle_flag(&use_flags, BO_USE_PROTECTED, &bind, VIRGL_BIND_MINIGBM_PROTECTED);
414 } else {
415 // Make sure we don't set both flags, since that could be mistaken for
416 // protected. Give OFTEN priority over RARELY.
417 if (use_flags & BO_USE_SW_READ_OFTEN) {
418 handle_flag(&use_flags, BO_USE_SW_READ_OFTEN, &bind,
419 VIRGL_BIND_MINIGBM_SW_READ_OFTEN);
420 } else {
421 handle_flag(&use_flags, BO_USE_SW_READ_RARELY, &bind,
422 VIRGL_BIND_MINIGBM_SW_READ_RARELY);
423 }
424 if (use_flags & BO_USE_SW_WRITE_OFTEN) {
425 handle_flag(&use_flags, BO_USE_SW_WRITE_OFTEN, &bind,
426 VIRGL_BIND_MINIGBM_SW_WRITE_OFTEN);
427 } else {
428 handle_flag(&use_flags, BO_USE_SW_WRITE_RARELY, &bind,
429 VIRGL_BIND_MINIGBM_SW_WRITE_RARELY);
430 }
431 }
David Stevens55a6cf92019-09-03 10:45:33 +0900432
David Stevens23de4e22020-05-15 14:15:35 +0900433 handle_flag(&use_flags, BO_USE_CAMERA_WRITE, &bind, VIRGL_BIND_MINIGBM_CAMERA_WRITE);
434 handle_flag(&use_flags, BO_USE_CAMERA_READ, &bind, VIRGL_BIND_MINIGBM_CAMERA_READ);
435 handle_flag(&use_flags, BO_USE_HW_VIDEO_DECODER, &bind,
436 VIRGL_BIND_MINIGBM_HW_VIDEO_DECODER);
437 handle_flag(&use_flags, BO_USE_HW_VIDEO_ENCODER, &bind,
438 VIRGL_BIND_MINIGBM_HW_VIDEO_ENCODER);
David Stevens55a6cf92019-09-03 10:45:33 +0900439
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800440 if (use_flags)
Lepton Wudbab0832019-04-19 12:26:39 -0700441 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900442
Lepton Wudbab0832019-04-19 12:26:39 -0700443 return bind;
444}
445
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800446static int virgl_3d_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
447 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700448{
449 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800450 size_t i;
Kansho Nishidad97877b2019-06-14 18:28:18 +0900451 uint32_t stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700452 struct drm_virtgpu_resource_create res_create = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800453 struct bo_metadata emulated_metadata;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700454
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800455 if (virgl_supports_combination_natively(bo->drv, format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800456 stride = drv_stride_from_format(format, width, 0);
457 drv_bo_from_format(bo, stride, height, format);
458 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800459 assert(virgl_supports_combination_through_emulation(bo->drv, format, use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800460
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800461 virgl_get_emulated_metadata(bo, &emulated_metadata);
Jason Macnak1de7f662020-01-24 15:05:57 -0800462
463 format = emulated_metadata.format;
464 width = emulated_metadata.width;
465 height = emulated_metadata.height;
466 for (i = 0; i < emulated_metadata.num_planes; i++) {
467 bo->meta.strides[i] = emulated_metadata.strides[i];
468 bo->meta.offsets[i] = emulated_metadata.offsets[i];
469 bo->meta.sizes[i] = emulated_metadata.sizes[i];
470 }
471 bo->meta.total_size = emulated_metadata.total_size;
472 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700473
Kansho Nishidad97877b2019-06-14 18:28:18 +0900474 /*
475 * Setting the target is intended to ensure this resource gets bound as a 2D
476 * texture in the host renderer's GL state. All of these resource properties are
477 * sent unchanged by the kernel to the host, which in turn sends them unchanged to
478 * virglrenderer. When virglrenderer makes a resource, it will convert the target
479 * enum to the equivalent one in GL and then bind the resource to that target.
480 */
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700481
Kansho Nishidad97877b2019-06-14 18:28:18 +0900482 res_create.target = PIPE_TEXTURE_2D;
483 res_create.format = translate_format(format);
David Stevenscf280482020-12-21 11:43:44 +0900484 res_create.bind = compute_virgl_bind_flags(use_flags, format);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900485 res_create.width = width;
486 res_create.height = height;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700487
Kansho Nishidad97877b2019-06-14 18:28:18 +0900488 /* For virgl 3D */
489 res_create.depth = 1;
490 res_create.array_size = 1;
491 res_create.last_level = 0;
492 res_create.nr_samples = 0;
493
Gurchetan Singh298b7572019-09-19 09:55:18 -0700494 res_create.size = ALIGN(bo->meta.total_size, PAGE_SIZE); // PAGE_SIZE = 0x1000
Kansho Nishidad97877b2019-06-14 18:28:18 +0900495 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create);
496 if (ret) {
497 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
498 return ret;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700499 }
500
Gurchetan Singh298b7572019-09-19 09:55:18 -0700501 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
Kansho Nishidad97877b2019-06-14 18:28:18 +0900502 bo->handles[plane].u32 = res_create.bo_handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700503
504 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700505}
506
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800507static void *virgl_3d_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700508{
509 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700510 struct drm_virtgpu_map gem_map = { 0 };
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700511
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700512 gem_map.handle = bo->handles[0].u32;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700513 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
514 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700515 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700516 return MAP_FAILED;
517 }
518
Gurchetan Singh298b7572019-09-19 09:55:18 -0700519 vma->length = bo->meta.total_size;
520 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700521 gem_map.offset);
522}
523
Jason Macnakd6666c82021-09-29 11:13:25 -0700524static uint32_t virgl_3d_get_max_texture_2d_size(struct driver *drv)
525{
526 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
527
528 if (priv->caps.v2.max_texture_2d_size)
529 return priv->caps.v2.max_texture_2d_size;
530
531 return UINT32_MAX;
532}
533
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800534static int virgl_get_caps(struct driver *drv, union virgl_caps *caps, int *caps_is_v2)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800535{
536 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700537 struct drm_virtgpu_get_caps cap_args = { 0 };
Jason Macnakddf4ec02020-02-03 16:36:46 -0800538
Lepton Wueebce652020-02-26 15:13:34 -0800539 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800540 cap_args.addr = (unsigned long long)caps;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800541 if (params[param_capset_fix].value) {
Lepton Wueebce652020-02-26 15:13:34 -0800542 *caps_is_v2 = 1;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800543 cap_args.cap_set_id = 2;
544 cap_args.size = sizeof(union virgl_caps);
545 } else {
546 cap_args.cap_set_id = 1;
547 cap_args.size = sizeof(struct virgl_caps_v1);
548 }
549
550 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
551 if (ret) {
552 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Lepton Wueebce652020-02-26 15:13:34 -0800553 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800554
555 // Fallback to v1
556 cap_args.cap_set_id = 1;
557 cap_args.size = sizeof(struct virgl_caps_v1);
558
559 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800560 if (ret)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800561 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Jason Macnakddf4ec02020-02-03 16:36:46 -0800562 }
563
564 return ret;
565}
566
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800567static void virgl_init_params_and_caps(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700568{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800569 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
570 if (params[param_3d].value) {
571 virgl_get_caps(drv, &priv->caps, &priv->caps_is_v2);
Lepton Wu249e8632018-04-05 12:50:03 -0700572
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800573 // We use two criteria to determine whether host minigbm is used on the host for
574 // swapchain allocations.
575 //
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800576 // (1) Host minigbm is only available via virglrenderer, and only virglrenderer
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800577 // advertises capabilities.
578 // (2) Only host minigbm doesn't emulate YUV formats. Checking this is a bit of a
579 // proxy, but it works.
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800580 priv->host_gbm_enabled =
581 priv->caps.max_version > 0 &&
582 virgl_supports_combination_natively(drv, DRM_FORMAT_NV12, BO_USE_TEXTURE);
Lepton Wu249e8632018-04-05 12:50:03 -0700583 }
Jason Macnak1de7f662020-01-24 15:05:57 -0800584}
585
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800586static int virgl_init(struct driver *drv)
Jason Macnak1de7f662020-01-24 15:05:57 -0800587{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800588 struct virgl_priv *priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800589
590 priv = calloc(1, sizeof(*priv));
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000591 if (!priv)
592 return -ENOMEM;
593
Jason Macnak1de7f662020-01-24 15:05:57 -0800594 drv->priv = priv;
595
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800596 virgl_init_params_and_caps(drv);
Jason Macnak1de7f662020-01-24 15:05:57 -0800597
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800598 if (params[param_3d].value) {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700599 /* This doesn't mean host can scanout everything, it just means host
600 * hypervisor can show it. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800601 virgl_add_combinations(drv, render_target_formats,
602 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
603 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
604 virgl_add_combinations(drv, texture_source_formats,
605 ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
606 BO_USE_TEXTURE_MASK);
Yiwei Zhang9420ffe2021-09-24 06:24:30 +0000607 /* NV12 with scanout must flow through virgl_add_combination, so that the native
608 * support is checked and scanout use_flag can be conditionally stripped. */
609 virgl_add_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
610 BO_USE_TEXTURE_MASK | BO_USE_CAMERA_READ |
611 BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
612 BO_USE_HW_VIDEO_ENCODER | BO_USE_SCANOUT);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700613 } else {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700614 /* Virtio primary plane only allows this format. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800615 virgl_add_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
616 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700617 /* Virtio cursor plane only allows this format and Chrome cannot live without
618 * ARGB888 renderable format. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800619 virgl_add_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
620 BO_USE_RENDER_MASK | BO_USE_CURSOR);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700621 /* Android needs more, but they cannot be bound as scanouts anymore after
622 * "drm/virtio: fix DRM_FORMAT_* handling" */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800623 virgl_add_combinations(drv, render_target_formats,
624 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
625 BO_USE_RENDER_MASK);
626 virgl_add_combinations(drv, dumb_texture_source_formats,
627 ARRAY_SIZE(dumb_texture_source_formats), &LINEAR_METADATA,
628 BO_USE_TEXTURE_MASK);
Yiwei Zhang9fa17e72021-09-17 22:11:29 +0000629 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
630 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
631 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700632 }
Lepton Wu249e8632018-04-05 12:50:03 -0700633
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700634 /* Android CTS tests require this. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800635 virgl_add_combination(drv, DRM_FORMAT_RGB888, &LINEAR_METADATA, BO_USE_SW_MASK);
636 virgl_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
Emilian Peevf76db982022-01-26 14:28:35 -0800637 virgl_add_combination(drv, DRM_FORMAT_P010, &LINEAR_METADATA, BO_USE_TEXTURE |
Jason Macnak2ce35772021-06-08 06:45:45 -0700638 BO_USE_SW_MASK | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900639 drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
David Staessens04b7e242020-05-28 15:47:15 +0900640 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000641 BO_USE_HW_VIDEO_ENCODER | BO_USE_GPU_DATA_BUFFER);
David Stevens519978f2020-12-11 14:09:56 +0900642
643 if (!priv->host_gbm_enabled) {
644 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &LINEAR_METADATA,
645 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
646 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
647 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &LINEAR_METADATA,
648 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
649 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
650 drv_modify_combination(drv, DRM_FORMAT_NV21, &LINEAR_METADATA,
651 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
652 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
653 drv_modify_combination(drv, DRM_FORMAT_R16, &LINEAR_METADATA,
654 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
655 BO_USE_HW_VIDEO_DECODER);
656 drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA,
657 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
658 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
659 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &LINEAR_METADATA,
660 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
661 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
662 }
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900663
Lepton Wu249e8632018-04-05 12:50:03 -0700664 return drv_modify_linear_combinations(drv);
665}
666
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800667static void virgl_close(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700668{
669 free(drv->priv);
670 drv->priv = NULL;
671}
672
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800673static int virgl_bo_create_blob(struct driver *drv, struct bo *bo)
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700674{
675 int ret;
676 uint32_t stride;
David Stevens0fe561f2020-10-28 16:06:38 +0900677 uint32_t cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700678 uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 };
679 struct drm_virtgpu_resource_create_blob drm_rc_blob = { 0 };
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800680 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700681
David Stevensd3f07bd2020-09-25 18:52:26 +0900682 uint32_t blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE;
683 if (bo->meta.use_flags & BO_USE_SW_MASK)
684 blob_flags |= VIRTGPU_BLOB_FLAG_USE_MAPPABLE;
David Stevens1b252e22021-08-03 16:48:17 +0900685
686 // For now, all blob use cases are cross device. When we add wider
687 // support for blobs, we can revisit making this unconditional.
688 blob_flags |= VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE;
David Stevensb42624c2020-09-10 10:50:26 +0900689
David Stevens0fe561f2020-10-28 16:06:38 +0900690 cur_blob_id = atomic_fetch_add(&priv->next_blob_id, 1);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700691 stride = drv_stride_from_format(bo->meta.format, bo->meta.width, 0);
692 drv_bo_from_format(bo, stride, bo->meta.height, bo->meta.format);
693 bo->meta.total_size = ALIGN(bo->meta.total_size, PAGE_SIZE);
David Stevensb42624c2020-09-10 10:50:26 +0900694 bo->meta.tiling = blob_flags;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700695
696 cmd[0] = VIRGL_CMD0(VIRGL_CCMD_PIPE_RESOURCE_CREATE, 0, VIRGL_PIPE_RES_CREATE_SIZE);
697 cmd[VIRGL_PIPE_RES_CREATE_TARGET] = PIPE_TEXTURE_2D;
698 cmd[VIRGL_PIPE_RES_CREATE_WIDTH] = bo->meta.width;
699 cmd[VIRGL_PIPE_RES_CREATE_HEIGHT] = bo->meta.height;
700 cmd[VIRGL_PIPE_RES_CREATE_FORMAT] = translate_format(bo->meta.format);
David Stevenscf280482020-12-21 11:43:44 +0900701 cmd[VIRGL_PIPE_RES_CREATE_BIND] =
702 compute_virgl_bind_flags(bo->meta.use_flags, bo->meta.format);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700703 cmd[VIRGL_PIPE_RES_CREATE_DEPTH] = 1;
David Stevens0fe561f2020-10-28 16:06:38 +0900704 cmd[VIRGL_PIPE_RES_CREATE_BLOB_ID] = cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700705
706 drm_rc_blob.cmd = (uint64_t)&cmd;
707 drm_rc_blob.cmd_size = 4 * (VIRGL_PIPE_RES_CREATE_SIZE + 1);
708 drm_rc_blob.size = bo->meta.total_size;
709 drm_rc_blob.blob_mem = VIRTGPU_BLOB_MEM_HOST3D;
David Stevensb42624c2020-09-10 10:50:26 +0900710 drm_rc_blob.blob_flags = blob_flags;
David Stevens0fe561f2020-10-28 16:06:38 +0900711 drm_rc_blob.blob_id = cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700712
713 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &drm_rc_blob);
714 if (ret < 0) {
715 drv_log("DRM_VIRTGPU_RESOURCE_CREATE_BLOB failed with %s\n", strerror(errno));
716 return -errno;
717 }
718
719 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
720 bo->handles[plane].u32 = drm_rc_blob.bo_handle;
721
722 return 0;
723}
724
725static bool should_use_blob(struct driver *drv, uint32_t format, uint64_t use_flags)
726{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800727 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700728
729 // TODO(gurchetansingh): remove once all minigbm users are blob-safe
730#ifndef VIRTIO_GPU_NEXT
731 return false;
732#endif
733
734 // Only use blob when host gbm is available
735 if (!priv->host_gbm_enabled)
736 return false;
737
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000738 // Use regular resources if only the GPU needs efficient access. Blob resource is a better
739 // fit for BO_USE_GPU_DATA_BUFFER which is mapped to VIRGL_BIND_LINEAR.
740 if (!(use_flags & (BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN | BO_USE_LINEAR |
741 BO_USE_NON_GPU_HW | BO_USE_GPU_DATA_BUFFER)))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700742 return false;
743
David Stevensd3f07bd2020-09-25 18:52:26 +0900744 switch (format) {
David Stevensd3f07bd2020-09-25 18:52:26 +0900745 case DRM_FORMAT_R8:
746 // Formats with strictly defined strides are supported
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700747 return true;
David Stevensc6df2b22021-08-10 19:02:09 +0900748 case DRM_FORMAT_YVU420_ANDROID:
David Stevensd3f07bd2020-09-25 18:52:26 +0900749 case DRM_FORMAT_NV12:
750 // Knowing buffer metadata at buffer creation isn't yet supported, so buffers
751 // can't be properly mapped into the guest.
752 return (use_flags & BO_USE_SW_MASK) == 0;
753 default:
754 return false;
755 }
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700756}
757
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800758static int virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
759 uint64_t use_flags)
Lepton Wu249e8632018-04-05 12:50:03 -0700760{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800761 if (params[param_resource_blob].value && params[param_host_visible].value &&
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700762 should_use_blob(bo->drv, format, use_flags))
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800763 return virgl_bo_create_blob(bo->drv, bo);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700764
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800765 if (params[param_3d].value)
766 return virgl_3d_bo_create(bo, width, height, format, use_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700767 else
Jason Macnakc06cc9c2021-10-06 10:16:19 -0700768 return virgl_2d_dumb_bo_create(bo, width, height, format, use_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700769}
770
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800771static int virgl_bo_destroy(struct bo *bo)
Lepton Wu249e8632018-04-05 12:50:03 -0700772{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800773 if (params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700774 return drv_gem_bo_destroy(bo);
775 else
776 return drv_dumb_bo_destroy(bo);
777}
778
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800779static void *virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Lepton Wu249e8632018-04-05 12:50:03 -0700780{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800781 if (params[param_3d].value)
782 return virgl_3d_bo_map(bo, vma, plane, map_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700783 else
784 return drv_dumb_bo_map(bo, vma, plane, map_flags);
785}
786
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800787static int virgl_bo_invalidate(struct bo *bo, struct mapping *mapping)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700788{
789 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800790 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700791 struct drm_virtgpu_3d_transfer_from_host xfer = { 0 };
792 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800793 struct virtio_transfers_params xfer_params;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800794 struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv;
David Stevens9fe8c202020-12-21 18:47:55 +0900795 uint64_t host_write_flags;
Lepton Wu249e8632018-04-05 12:50:03 -0700796
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800797 if (!params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700798 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700799
David Stevens9fe8c202020-12-21 18:47:55 +0900800 // Invalidate is only necessary if the host writes to the buffer. The encoder and
801 // decoder flags don't differentiate between input and output buffers, but we can
802 // use the format to determine whether this buffer could be encoder/decoder output.
803 host_write_flags = BO_USE_RENDERING | BO_USE_CAMERA_WRITE;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800804 if (bo->meta.format == DRM_FORMAT_R8)
David Stevens9fe8c202020-12-21 18:47:55 +0900805 host_write_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800806 else
David Stevens9fe8c202020-12-21 18:47:55 +0900807 host_write_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800808
David Stevens9fe8c202020-12-21 18:47:55 +0900809 if ((bo->meta.use_flags & host_write_flags) == 0)
David Stevens4d5358d2019-10-24 14:59:31 +0900810 return 0;
811
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800812 if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700813 return 0;
814
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700815 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700816
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700817 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700818 /*
819 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
820 * images
821 */
822 if (bo->meta.num_planes == 1) {
823 xfer.offset =
824 (bo->meta.strides[0] * mapping->rect.y) +
825 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
826 }
827 }
828
David Stevensbaab6c82020-02-26 17:14:43 +0900829 if ((bo->meta.use_flags & BO_USE_RENDERING) == 0) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800830 // Unfortunately, the kernel doesn't actually pass the guest layer_stride
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800831 // and guest stride to the host (compare virgl.h and virtgpu_drm.h).
Jason Macnak1de7f662020-01-24 15:05:57 -0800832 // For gbm based resources, we can work around this by using the level field
833 // to pass the stride to virglrenderer's gbm transfer code. However, we need
834 // to avoid doing this for resources which don't rely on that transfer code,
835 // which is resources with the BO_USE_RENDERING flag set.
David Stevensbaab6c82020-02-26 17:14:43 +0900836 // TODO(b/145993887): Send also stride when the patches are landed
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800837 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800838 xfer.level = bo->meta.strides[0];
David Stevensbaab6c82020-02-26 17:14:43 +0900839 }
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700840
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800841 if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800842 xfer_params.xfers_needed = 1;
843 xfer_params.xfer_boxes[0] = mapping->rect;
844 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800845 assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format,
846 bo->meta.use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800847
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800848 virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
Jason Macnak1de7f662020-01-24 15:05:57 -0800849 }
850
851 for (i = 0; i < xfer_params.xfers_needed; i++) {
852 xfer.box.x = xfer_params.xfer_boxes[i].x;
853 xfer.box.y = xfer_params.xfer_boxes[i].y;
854 xfer.box.w = xfer_params.xfer_boxes[i].width;
855 xfer.box.h = xfer_params.xfer_boxes[i].height;
856 xfer.box.d = 1;
857
858 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
859 if (ret) {
860 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n",
861 strerror(errno));
862 return -errno;
863 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700864 }
865
David Stevens4d5358d2019-10-24 14:59:31 +0900866 // The transfer needs to complete before invalidate returns so that any host changes
867 // are visible and to ensure the host doesn't overwrite subsequent guest changes.
868 // TODO(b/136733358): Support returning fences from transfers
David Stevens4d5358d2019-10-24 14:59:31 +0900869 waitcmd.handle = mapping->vma->handle;
870 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
871 if (ret) {
872 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
873 return -errno;
874 }
875
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700876 return 0;
877}
878
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800879static int virgl_bo_flush(struct bo *bo, struct mapping *mapping)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700880{
881 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800882 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700883 struct drm_virtgpu_3d_transfer_to_host xfer = { 0 };
884 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800885 struct virtio_transfers_params xfer_params;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800886 struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv;
Lepton Wu249e8632018-04-05 12:50:03 -0700887
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800888 if (!params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700889 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700890
891 if (!(mapping->vma->map_flags & BO_MAP_WRITE))
892 return 0;
893
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800894 if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700895 return 0;
896
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700897 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700898
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700899 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700900 /*
901 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
902 * images
903 */
904 if (bo->meta.num_planes == 1) {
905 xfer.offset =
906 (bo->meta.strides[0] * mapping->rect.y) +
907 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
908 }
909 }
910
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700911 // Unfortunately, the kernel doesn't actually pass the guest layer_stride and
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800912 // guest stride to the host (compare virgl.h and virtgpu_drm.h). We can use
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700913 // the level to work around this.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800914 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800915 xfer.level = bo->meta.strides[0];
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700916
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800917 if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800918 xfer_params.xfers_needed = 1;
919 xfer_params.xfer_boxes[0] = mapping->rect;
920 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800921 assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format,
922 bo->meta.use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800923
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800924 virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
Jason Macnak1de7f662020-01-24 15:05:57 -0800925 }
926
927 for (i = 0; i < xfer_params.xfers_needed; i++) {
928 xfer.box.x = xfer_params.xfer_boxes[i].x;
929 xfer.box.y = xfer_params.xfer_boxes[i].y;
930 xfer.box.w = xfer_params.xfer_boxes[i].width;
931 xfer.box.h = xfer_params.xfer_boxes[i].height;
932 xfer.box.d = 1;
933
934 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
935 if (ret) {
936 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n",
937 strerror(errno));
938 return -errno;
939 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700940 }
941
David Stevensbaab6c82020-02-26 17:14:43 +0900942 // If the buffer is only accessed by the host GPU, then the flush is ordered
943 // with subsequent commands. However, if other host hardware can access the
944 // buffer, we need to wait for the transfer to complete for consistency.
945 // TODO(b/136733358): Support returning fences from transfers
946 if (bo->meta.use_flags & BO_USE_NON_GPU_HW) {
David Stevensbaab6c82020-02-26 17:14:43 +0900947 waitcmd.handle = mapping->vma->handle;
948
949 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
950 if (ret) {
951 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
952 return -errno;
953 }
954 }
955
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700956 return 0;
957}
958
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000959static void virgl_3d_resolve_format_and_use_flags(struct driver *drv, uint32_t format,
960 uint64_t use_flags, uint32_t *out_format,
961 uint64_t *out_use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700962{
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000963 *out_format = format;
964 *out_use_flags = use_flags;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700965 switch (format) {
966 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900967 /* Camera subsystem requires NV12. */
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000968 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) {
969 *out_format = DRM_FORMAT_NV12;
970 } else {
971 /* HACK: See b/28671744 */
972 *out_format = DRM_FORMAT_XBGR8888;
Yiwei Zhang3a171db2021-10-01 22:12:05 +0000973 *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000974 }
975 break;
Lepton Wu249e8632018-04-05 12:50:03 -0700976 case DRM_FORMAT_FLEX_YCbCr_420_888:
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000977 /* All of our host drivers prefer NV12 as their flexible media format.
978 * If that changes, this will need to be modified. */
979 *out_format = DRM_FORMAT_NV12;
980 /* fallthrough */
981 case DRM_FORMAT_NV12:
982 case DRM_FORMAT_ABGR8888:
983 case DRM_FORMAT_ARGB8888:
984 case DRM_FORMAT_RGB565:
985 case DRM_FORMAT_XBGR8888:
986 case DRM_FORMAT_XRGB8888:
987 /* These are the scanout capable formats to the guest. Strip scanout use_flag if the
988 * host does not natively support scanout on the requested format. */
989 if ((use_flags & BO_USE_SCANOUT) &&
990 !virgl_supports_combination_natively(drv, format, BO_USE_SCANOUT))
991 *out_use_flags &= ~BO_USE_SCANOUT;
992 break;
993 case DRM_FORMAT_YVU420_ANDROID:
994 *out_use_flags &= ~BO_USE_SCANOUT;
995 /* HACK: See b/172389166. Also see gbm_bo_create. */
996 *out_use_flags |= BO_USE_LINEAR;
997 break;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700998 default:
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000999 break;
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001000 }
1001}
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001002
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001003static void virgl_2d_resolve_format_and_use_flags(uint32_t format, uint64_t use_flags,
1004 uint32_t *out_format, uint64_t *out_use_flags)
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001005{
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001006 *out_format = format;
1007 *out_use_flags = use_flags;
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001008
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001009 /* HACK: See crrev/c/1849773 */
1010 if (format != DRM_FORMAT_XRGB8888)
1011 *out_use_flags &= ~BO_USE_SCANOUT;
1012
1013 switch (format) {
1014 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
1015 /* Camera subsystem requires NV12. */
1016 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) {
1017 *out_format = DRM_FORMAT_NV12;
1018 } else {
1019 /* HACK: See b/28671744 */
1020 *out_format = DRM_FORMAT_XBGR8888;
Yiwei Zhang3a171db2021-10-01 22:12:05 +00001021 *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
Yiwei Zhang9420ffe2021-09-24 06:24:30 +00001022 }
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001023 break;
1024 case DRM_FORMAT_FLEX_YCbCr_420_888:
1025 *out_format = DRM_FORMAT_YVU420_ANDROID;
1026 /* fallthrough */
1027 case DRM_FORMAT_YVU420_ANDROID:
1028 *out_use_flags &= ~BO_USE_SCANOUT;
1029 /* HACK: See b/172389166. Also see gbm_bo_create. */
1030 *out_use_flags |= BO_USE_LINEAR;
1031 break;
1032 default:
1033 break;
Yiwei Zhang9420ffe2021-09-24 06:24:30 +00001034 }
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001035}
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001036
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001037static void virgl_resolve_format_and_use_flags(struct driver *drv, uint32_t format,
1038 uint64_t use_flags, uint32_t *out_format,
1039 uint64_t *out_use_flags)
1040{
1041 if (params[param_3d].value) {
1042 return virgl_3d_resolve_format_and_use_flags(drv, format, use_flags, out_format,
1043 out_use_flags);
1044 } else {
1045 return virgl_2d_resolve_format_and_use_flags(format, use_flags, out_format,
1046 out_use_flags);
1047 }
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001048}
1049
Gurchetan Singh73c141e2021-01-21 14:51:19 -08001050static int virgl_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +00001051 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001052{
1053 int ret;
Chia-I Wu2e41f632021-01-11 11:08:21 -08001054 struct drm_virtgpu_resource_info_cros res_info = { 0 };
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001055
Gurchetan Singh73c141e2021-01-21 14:51:19 -08001056 if (!params[param_3d].value)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001057 return 0;
1058
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001059 res_info.bo_handle = bo->handles[0].u32;
Chia-I Wu50855622021-01-12 12:38:09 -08001060 res_info.type = VIRTGPU_RESOURCE_INFO_TYPE_EXTENDED;
Chia-I Wu2e41f632021-01-11 11:08:21 -08001061 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO_CROS, &res_info);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001062 if (ret) {
1063 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_INFO failed with %s\n", strerror(errno));
1064 return ret;
1065 }
1066
Yiwei Zhangf58616e2021-08-26 05:54:15 +00001067 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++) {
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001068 /*
1069 * Currently, kernel v4.14 (Betty) doesn't have the extended resource info
1070 * ioctl.
1071 */
Yiwei Zhangf58616e2021-08-26 05:54:15 +00001072 if (!res_info.strides[plane])
1073 break;
1074
1075 strides[plane] = res_info.strides[plane];
1076 offsets[plane] = res_info.offsets[plane];
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001077 }
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +00001078 *format_modifier = res_info.format_modifier;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001079
1080 return 0;
1081}
1082
Jason Macnakd6666c82021-09-29 11:13:25 -07001083static uint32_t virgl_get_max_texture_2d_size(struct driver *drv)
1084{
1085 if (params[param_3d].value)
1086 return virgl_3d_get_max_texture_2d_size(drv);
1087 else
Jason Macnakc06cc9c2021-10-06 10:16:19 -07001088 return VIRGL_2D_MAX_TEXTURE_2D_SIZE;
Jason Macnakd6666c82021-09-29 11:13:25 -07001089}
1090
Gurchetan Singhbbde01e2021-02-17 08:54:28 -08001091const struct backend virtgpu_virgl = { .name = "virtgpu_virgl",
1092 .init = virgl_init,
1093 .close = virgl_close,
1094 .bo_create = virgl_bo_create,
1095 .bo_destroy = virgl_bo_destroy,
1096 .bo_import = drv_prime_bo_import,
1097 .bo_map = virgl_bo_map,
1098 .bo_unmap = drv_bo_munmap,
1099 .bo_invalidate = virgl_bo_invalidate,
1100 .bo_flush = virgl_bo_flush,
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001101 .resolve_format_and_use_flags =
1102 virgl_resolve_format_and_use_flags,
Jason Macnakd6666c82021-09-29 11:13:25 -07001103 .resource_info = virgl_resource_info,
1104 .get_max_texture_2d_size = virgl_get_max_texture_2d_size };