blob: 8bcfc9d7599e69b1c17ba499e0c6846b3555a33c [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;
Jason Macnakdbc63f72022-06-23 18:34:55 -070095 case DRM_FORMAT_P010:
96 return VIRGL_FORMAT_P010;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -070097 case DRM_FORMAT_YVU420:
98 case DRM_FORMAT_YVU420_ANDROID:
99 return VIRGL_FORMAT_YV12;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700100 default:
Jason Macnak6e200ea2021-02-11 19:34:57 -0800101 drv_log("Unhandled format:%d\n", drm_fourcc);
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700102 return 0;
103 }
104}
105
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800106static bool virgl_bitmask_supports_format(struct virgl_supported_format_mask *supported,
107 uint32_t drm_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800108{
109 uint32_t virgl_format = translate_format(drm_format);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800110 if (!virgl_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800111 return false;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800112
113 uint32_t bitmask_index = virgl_format / 32;
114 uint32_t bit_index = virgl_format % 32;
115 return supported->bitmask[bitmask_index] & (1 << bit_index);
116}
117
Jason Macnak1de7f662020-01-24 15:05:57 -0800118// The metadata generated here for emulated buffers is slightly different than the metadata
119// generated by drv_bo_from_format. In order to simplify transfers in the flush and invalidate
120// functions below, the emulated buffers are oversized. For example, ignoring stride alignment
121// requirements to demonstrate, a 6x6 YUV420 image buffer might have the following layout from
122// drv_bo_from_format:
123//
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// | Y | Y | Y | Y | Y | Y |
129// | Y | Y | Y | Y | Y | Y |
130// | U | U | U | U | U | U |
131// | U | U | U | V | V | V |
132// | V | V | V | V | V | V |
133//
134// where each plane immediately follows the previous plane in memory. This layout makes it
135// difficult to compute the transfers needed for example when the middle 2x2 region of the
136// image is locked and needs to be flushed/invalidated.
137//
138// Emulated multi-plane buffers instead have a layout of:
139//
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// | Y | Y | Y | Y | Y | Y |
145// | Y | Y | Y | Y | Y | Y |
146// | U | U | U | | | |
147// | U | U | U | | | |
148// | U | U | U | | | |
149// | V | V | V | | | |
150// | V | V | V | | | |
151// | V | V | V | | | |
152//
153// where each plane is placed as a sub-image (albeit with a very large stride) in order to
154// simplify transfers into 3 sub-image transfers for the above example.
155//
156// Additional note: the V-plane is not placed to the right of the U-plane due to some
157// observed failures in media framework code which assumes the V-plane is not
158// "row-interlaced" with the U-plane.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800159static void virgl_get_emulated_metadata(const struct bo *bo, struct bo_metadata *metadata)
Jason Macnak1de7f662020-01-24 15:05:57 -0800160{
161 uint32_t y_plane_height;
162 uint32_t c_plane_height;
163 uint32_t original_width = bo->meta.width;
164 uint32_t original_height = bo->meta.height;
165
166 metadata->format = DRM_FORMAT_R8;
167 switch (bo->meta.format) {
168 case DRM_FORMAT_NV12:
169 case DRM_FORMAT_NV21:
170 // Bi-planar
171 metadata->num_planes = 2;
172
173 y_plane_height = original_height;
174 c_plane_height = DIV_ROUND_UP(original_height, 2);
175
176 metadata->width = original_width;
177 metadata->height = y_plane_height + c_plane_height;
178
179 // Y-plane (full resolution)
180 metadata->strides[0] = metadata->width;
181 metadata->offsets[0] = 0;
182 metadata->sizes[0] = metadata->width * y_plane_height;
183
184 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
185 metadata->strides[1] = metadata->width;
186 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
187 metadata->sizes[1] = metadata->width * c_plane_height;
188
189 metadata->total_size = metadata->width * metadata->height;
190 break;
191 case DRM_FORMAT_YVU420:
192 case DRM_FORMAT_YVU420_ANDROID:
193 // Tri-planar
194 metadata->num_planes = 3;
195
196 y_plane_height = original_height;
197 c_plane_height = DIV_ROUND_UP(original_height, 2);
198
199 metadata->width = ALIGN(original_width, 32);
200 metadata->height = y_plane_height + (2 * c_plane_height);
201
202 // Y-plane (full resolution)
203 metadata->strides[0] = metadata->width;
204 metadata->offsets[0] = 0;
205 metadata->sizes[0] = metadata->width * original_height;
206
207 // Cb-plane (half resolution, placed below Y-plane)
208 metadata->strides[1] = metadata->width;
209 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
210 metadata->sizes[1] = metadata->width * c_plane_height;
211
212 // Cr-plane (half resolution, placed below Cb-plane)
213 metadata->strides[2] = metadata->width;
214 metadata->offsets[2] = metadata->offsets[1] + metadata->sizes[1];
215 metadata->sizes[2] = metadata->width * c_plane_height;
216
217 metadata->total_size = metadata->width * metadata->height;
218 break;
219 default:
220 break;
221 }
222}
223
224struct virtio_transfers_params {
225 size_t xfers_needed;
226 struct rectangle xfer_boxes[DRV_MAX_PLANES];
227};
228
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800229static void virgl_get_emulated_transfers_params(const struct bo *bo,
230 const struct rectangle *transfer_box,
231 struct virtio_transfers_params *xfer_params)
Jason Macnak1de7f662020-01-24 15:05:57 -0800232{
233 uint32_t y_plane_height;
234 uint32_t c_plane_height;
235 struct bo_metadata emulated_metadata;
236
237 if (transfer_box->x == 0 && transfer_box->y == 0 && transfer_box->width == bo->meta.width &&
238 transfer_box->height == bo->meta.height) {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800239 virgl_get_emulated_metadata(bo, &emulated_metadata);
Jason Macnak1de7f662020-01-24 15:05:57 -0800240
241 xfer_params->xfers_needed = 1;
242 xfer_params->xfer_boxes[0].x = 0;
243 xfer_params->xfer_boxes[0].y = 0;
244 xfer_params->xfer_boxes[0].width = emulated_metadata.width;
245 xfer_params->xfer_boxes[0].height = emulated_metadata.height;
246
247 return;
248 }
249
250 switch (bo->meta.format) {
251 case DRM_FORMAT_NV12:
252 case DRM_FORMAT_NV21:
253 // Bi-planar
254 xfer_params->xfers_needed = 2;
255
256 y_plane_height = bo->meta.height;
257 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
258
259 // Y-plane (full resolution)
260 xfer_params->xfer_boxes[0].x = transfer_box->x;
261 xfer_params->xfer_boxes[0].y = transfer_box->y;
262 xfer_params->xfer_boxes[0].width = transfer_box->width;
263 xfer_params->xfer_boxes[0].height = transfer_box->height;
264
265 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
266 xfer_params->xfer_boxes[1].x = transfer_box->x;
267 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
268 xfer_params->xfer_boxes[1].width = transfer_box->width;
269 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
270
271 break;
272 case DRM_FORMAT_YVU420:
273 case DRM_FORMAT_YVU420_ANDROID:
274 // Tri-planar
275 xfer_params->xfers_needed = 3;
276
277 y_plane_height = bo->meta.height;
278 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
279
280 // Y-plane (full resolution)
281 xfer_params->xfer_boxes[0].x = transfer_box->x;
282 xfer_params->xfer_boxes[0].y = transfer_box->y;
283 xfer_params->xfer_boxes[0].width = transfer_box->width;
284 xfer_params->xfer_boxes[0].height = transfer_box->height;
285
286 // Cb-plane (half resolution, placed below Y-plane)
287 xfer_params->xfer_boxes[1].x = transfer_box->x;
288 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
289 xfer_params->xfer_boxes[1].width = DIV_ROUND_UP(transfer_box->width, 2);
290 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
291
292 // Cr-plane (half resolution, placed below Cb-plane)
293 xfer_params->xfer_boxes[2].x = transfer_box->x;
294 xfer_params->xfer_boxes[2].y = transfer_box->y + y_plane_height + c_plane_height;
295 xfer_params->xfer_boxes[2].width = DIV_ROUND_UP(transfer_box->width, 2);
296 xfer_params->xfer_boxes[2].height = DIV_ROUND_UP(transfer_box->height, 2);
297
298 break;
299 }
300}
301
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800302static bool virgl_supports_combination_natively(struct driver *drv, uint32_t drm_format,
303 uint64_t use_flags)
Jason Macnak1de7f662020-01-24 15:05:57 -0800304{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800305 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800306
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800307 if (priv->caps.max_version == 0)
Jason Macnak1de7f662020-01-24 15:05:57 -0800308 return true;
Jason Macnak1de7f662020-01-24 15:05:57 -0800309
310 if ((use_flags & BO_USE_RENDERING) &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800311 !virgl_bitmask_supports_format(&priv->caps.v1.render, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800312 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800313
314 if ((use_flags & BO_USE_TEXTURE) &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800315 !virgl_bitmask_supports_format(&priv->caps.v1.sampler, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800316 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800317
318 if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800319 !virgl_bitmask_supports_format(&priv->caps.v2.scanout, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800320 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800321
322 return true;
323}
324
325// For virtio backends that do not support formats natively (e.g. multi-planar formats are not
326// supported in virglrenderer when gbm is unavailable on the host machine), whether or not the
327// format and usage combination can be handled as a blob (byte buffer).
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800328static bool virgl_supports_combination_through_emulation(struct driver *drv, uint32_t drm_format,
329 uint64_t use_flags)
Jason Macnak1de7f662020-01-24 15:05:57 -0800330{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800331 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800332
333 // Only enable emulation on non-gbm virtio backends.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800334 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800335 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800336
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800337 if (use_flags & (BO_USE_RENDERING | BO_USE_SCANOUT))
Jason Macnak1de7f662020-01-24 15:05:57 -0800338 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800339
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800340 if (!virgl_supports_combination_natively(drv, DRM_FORMAT_R8, use_flags))
Jason Macnak1de7f662020-01-24 15:05:57 -0800341 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800342
343 return drm_format == DRM_FORMAT_NV12 || drm_format == DRM_FORMAT_NV21 ||
344 drm_format == DRM_FORMAT_YVU420 || drm_format == DRM_FORMAT_YVU420_ANDROID;
345}
346
Jason Macnakddf4ec02020-02-03 16:36:46 -0800347// Adds the given buffer combination to the list of supported buffer combinations if the
348// combination is supported by the virtio backend.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800349static void virgl_add_combination(struct driver *drv, uint32_t drm_format,
350 struct format_metadata *metadata, uint64_t use_flags)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800351{
Yiwei Zhang9420ffe2021-09-24 06:24:30 +0000352 if (params[param_3d].value) {
353 if ((use_flags & BO_USE_SCANOUT) &&
354 !virgl_supports_combination_natively(drv, drm_format, BO_USE_SCANOUT)) {
355 drv_log("Strip scanout on format: %d\n", drm_format);
Jason Macnak1de7f662020-01-24 15:05:57 -0800356 use_flags &= ~BO_USE_SCANOUT;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800357 }
358
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800359 if (!virgl_supports_combination_natively(drv, drm_format, use_flags) &&
360 !virgl_supports_combination_through_emulation(drv, drm_format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800361 drv_log("Skipping unsupported combination format:%d\n", drm_format);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800362 return;
363 }
364 }
365
366 drv_add_combination(drv, drm_format, metadata, use_flags);
367}
368
369// Adds each given buffer combination to the list of supported buffer combinations if the
370// combination supported by the virtio backend.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800371static void virgl_add_combinations(struct driver *drv, const uint32_t *drm_formats,
372 uint32_t num_formats, struct format_metadata *metadata,
373 uint64_t use_flags)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800374{
375 uint32_t i;
376
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800377 for (i = 0; i < num_formats; i++)
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800378 virgl_add_combination(drv, drm_formats[i], metadata, use_flags);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800379}
380
Jason Macnakc06cc9c2021-10-06 10:16:19 -0700381static int virgl_2d_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
382 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700383{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700384 if (bo->meta.format != DRM_FORMAT_R8) {
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900385 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
386 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
387 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700388
Dominik Behr6e6dc492019-10-09 15:43:52 -0700389 return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_DUMB32BPP);
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700390}
391
Lepton Wudbab0832019-04-19 12:26:39 -0700392static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
393 uint32_t virgl_bind)
394{
395 if ((*flag) & check_flag) {
396 (*flag) &= ~check_flag;
397 (*bind) |= virgl_bind;
398 }
399}
400
David Stevenscf280482020-12-21 11:43:44 +0900401static uint32_t compute_virgl_bind_flags(uint64_t use_flags, uint32_t format)
Lepton Wudbab0832019-04-19 12:26:39 -0700402{
Kansho Nishidad97877b2019-06-14 18:28:18 +0900403 /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
404 uint32_t bind = VIRGL_BIND_SHARED;
Lepton Wudbab0832019-04-19 12:26:39 -0700405
406 handle_flag(&use_flags, BO_USE_TEXTURE, &bind, VIRGL_BIND_SAMPLER_VIEW);
407 handle_flag(&use_flags, BO_USE_RENDERING, &bind, VIRGL_BIND_RENDER_TARGET);
408 handle_flag(&use_flags, BO_USE_SCANOUT, &bind, VIRGL_BIND_SCANOUT);
David Stevens55a6cf92019-09-03 10:45:33 +0900409 handle_flag(&use_flags, BO_USE_CURSOR, &bind, VIRGL_BIND_CURSOR);
410 handle_flag(&use_flags, BO_USE_LINEAR, &bind, VIRGL_BIND_LINEAR);
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000411 handle_flag(&use_flags, BO_USE_GPU_DATA_BUFFER, &bind, VIRGL_BIND_LINEAR);
Yiwei Zhangd3a73ff2021-07-08 05:48:01 +0000412 handle_flag(&use_flags, BO_USE_FRONT_RENDERING, &bind, VIRGL_BIND_LINEAR);
David Stevens55a6cf92019-09-03 10:45:33 +0900413
David Stevens23de4e22020-05-15 14:15:35 +0900414 if (use_flags & BO_USE_PROTECTED) {
415 handle_flag(&use_flags, BO_USE_PROTECTED, &bind, VIRGL_BIND_MINIGBM_PROTECTED);
416 } else {
417 // Make sure we don't set both flags, since that could be mistaken for
418 // protected. Give OFTEN priority over RARELY.
419 if (use_flags & BO_USE_SW_READ_OFTEN) {
420 handle_flag(&use_flags, BO_USE_SW_READ_OFTEN, &bind,
421 VIRGL_BIND_MINIGBM_SW_READ_OFTEN);
422 } else {
423 handle_flag(&use_flags, BO_USE_SW_READ_RARELY, &bind,
424 VIRGL_BIND_MINIGBM_SW_READ_RARELY);
425 }
426 if (use_flags & BO_USE_SW_WRITE_OFTEN) {
427 handle_flag(&use_flags, BO_USE_SW_WRITE_OFTEN, &bind,
428 VIRGL_BIND_MINIGBM_SW_WRITE_OFTEN);
429 } else {
430 handle_flag(&use_flags, BO_USE_SW_WRITE_RARELY, &bind,
431 VIRGL_BIND_MINIGBM_SW_WRITE_RARELY);
432 }
433 }
David Stevens55a6cf92019-09-03 10:45:33 +0900434
David Stevens23de4e22020-05-15 14:15:35 +0900435 handle_flag(&use_flags, BO_USE_CAMERA_WRITE, &bind, VIRGL_BIND_MINIGBM_CAMERA_WRITE);
436 handle_flag(&use_flags, BO_USE_CAMERA_READ, &bind, VIRGL_BIND_MINIGBM_CAMERA_READ);
437 handle_flag(&use_flags, BO_USE_HW_VIDEO_DECODER, &bind,
438 VIRGL_BIND_MINIGBM_HW_VIDEO_DECODER);
439 handle_flag(&use_flags, BO_USE_HW_VIDEO_ENCODER, &bind,
440 VIRGL_BIND_MINIGBM_HW_VIDEO_ENCODER);
David Stevens55a6cf92019-09-03 10:45:33 +0900441
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800442 if (use_flags)
Lepton Wudbab0832019-04-19 12:26:39 -0700443 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900444
Lepton Wudbab0832019-04-19 12:26:39 -0700445 return bind;
446}
447
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800448static int virgl_3d_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
449 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700450{
451 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800452 size_t i;
Kansho Nishidad97877b2019-06-14 18:28:18 +0900453 uint32_t stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700454 struct drm_virtgpu_resource_create res_create = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800455 struct bo_metadata emulated_metadata;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700456
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800457 if (virgl_supports_combination_natively(bo->drv, format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800458 stride = drv_stride_from_format(format, width, 0);
459 drv_bo_from_format(bo, stride, height, format);
460 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800461 assert(virgl_supports_combination_through_emulation(bo->drv, format, use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800462
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800463 virgl_get_emulated_metadata(bo, &emulated_metadata);
Jason Macnak1de7f662020-01-24 15:05:57 -0800464
465 format = emulated_metadata.format;
466 width = emulated_metadata.width;
467 height = emulated_metadata.height;
468 for (i = 0; i < emulated_metadata.num_planes; i++) {
469 bo->meta.strides[i] = emulated_metadata.strides[i];
470 bo->meta.offsets[i] = emulated_metadata.offsets[i];
471 bo->meta.sizes[i] = emulated_metadata.sizes[i];
472 }
473 bo->meta.total_size = emulated_metadata.total_size;
474 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700475
Kansho Nishidad97877b2019-06-14 18:28:18 +0900476 /*
477 * Setting the target is intended to ensure this resource gets bound as a 2D
478 * texture in the host renderer's GL state. All of these resource properties are
479 * sent unchanged by the kernel to the host, which in turn sends them unchanged to
480 * virglrenderer. When virglrenderer makes a resource, it will convert the target
481 * enum to the equivalent one in GL and then bind the resource to that target.
482 */
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700483
Kansho Nishidad97877b2019-06-14 18:28:18 +0900484 res_create.target = PIPE_TEXTURE_2D;
485 res_create.format = translate_format(format);
David Stevenscf280482020-12-21 11:43:44 +0900486 res_create.bind = compute_virgl_bind_flags(use_flags, format);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900487 res_create.width = width;
488 res_create.height = height;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700489
Kansho Nishidad97877b2019-06-14 18:28:18 +0900490 /* For virgl 3D */
491 res_create.depth = 1;
492 res_create.array_size = 1;
493 res_create.last_level = 0;
494 res_create.nr_samples = 0;
495
Gurchetan Singh298b7572019-09-19 09:55:18 -0700496 res_create.size = ALIGN(bo->meta.total_size, PAGE_SIZE); // PAGE_SIZE = 0x1000
Kansho Nishidad97877b2019-06-14 18:28:18 +0900497 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create);
498 if (ret) {
499 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
500 return ret;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700501 }
502
Gurchetan Singh298b7572019-09-19 09:55:18 -0700503 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
Kansho Nishidad97877b2019-06-14 18:28:18 +0900504 bo->handles[plane].u32 = res_create.bo_handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700505
506 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700507}
508
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800509static 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 -0700510{
511 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700512 struct drm_virtgpu_map gem_map = { 0 };
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700513
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700514 gem_map.handle = bo->handles[0].u32;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700515 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
516 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700517 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700518 return MAP_FAILED;
519 }
520
Gurchetan Singh298b7572019-09-19 09:55:18 -0700521 vma->length = bo->meta.total_size;
522 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700523 gem_map.offset);
524}
525
Jason Macnakd6666c82021-09-29 11:13:25 -0700526static uint32_t virgl_3d_get_max_texture_2d_size(struct driver *drv)
527{
528 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
529
530 if (priv->caps.v2.max_texture_2d_size)
531 return priv->caps.v2.max_texture_2d_size;
532
533 return UINT32_MAX;
534}
535
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800536static int virgl_get_caps(struct driver *drv, union virgl_caps *caps, int *caps_is_v2)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800537{
538 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700539 struct drm_virtgpu_get_caps cap_args = { 0 };
Jason Macnakddf4ec02020-02-03 16:36:46 -0800540
Lepton Wueebce652020-02-26 15:13:34 -0800541 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800542 cap_args.addr = (unsigned long long)caps;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800543 if (params[param_capset_fix].value) {
Lepton Wueebce652020-02-26 15:13:34 -0800544 *caps_is_v2 = 1;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800545 cap_args.cap_set_id = 2;
546 cap_args.size = sizeof(union virgl_caps);
547 } else {
548 cap_args.cap_set_id = 1;
549 cap_args.size = sizeof(struct virgl_caps_v1);
550 }
551
552 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
553 if (ret) {
554 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Lepton Wueebce652020-02-26 15:13:34 -0800555 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800556
557 // Fallback to v1
558 cap_args.cap_set_id = 1;
559 cap_args.size = sizeof(struct virgl_caps_v1);
560
561 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800562 if (ret)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800563 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Jason Macnakddf4ec02020-02-03 16:36:46 -0800564 }
565
566 return ret;
567}
568
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800569static void virgl_init_params_and_caps(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700570{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800571 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
572 if (params[param_3d].value) {
573 virgl_get_caps(drv, &priv->caps, &priv->caps_is_v2);
Lepton Wu249e8632018-04-05 12:50:03 -0700574
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800575 // We use two criteria to determine whether host minigbm is used on the host for
576 // swapchain allocations.
577 //
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800578 // (1) Host minigbm is only available via virglrenderer, and only virglrenderer
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800579 // advertises capabilities.
580 // (2) Only host minigbm doesn't emulate YUV formats. Checking this is a bit of a
581 // proxy, but it works.
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800582 priv->host_gbm_enabled =
583 priv->caps.max_version > 0 &&
584 virgl_supports_combination_natively(drv, DRM_FORMAT_NV12, BO_USE_TEXTURE);
Lepton Wu249e8632018-04-05 12:50:03 -0700585 }
Jason Macnak1de7f662020-01-24 15:05:57 -0800586}
587
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800588static int virgl_init(struct driver *drv)
Jason Macnak1de7f662020-01-24 15:05:57 -0800589{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800590 struct virgl_priv *priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800591
592 priv = calloc(1, sizeof(*priv));
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000593 if (!priv)
594 return -ENOMEM;
595
Jason Macnak1de7f662020-01-24 15:05:57 -0800596 drv->priv = priv;
597
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800598 virgl_init_params_and_caps(drv);
Jason Macnak1de7f662020-01-24 15:05:57 -0800599
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800600 if (params[param_3d].value) {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700601 /* This doesn't mean host can scanout everything, it just means host
602 * hypervisor can show it. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800603 virgl_add_combinations(drv, render_target_formats,
604 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
605 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
606 virgl_add_combinations(drv, texture_source_formats,
607 ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
608 BO_USE_TEXTURE_MASK);
Yiwei Zhang9420ffe2021-09-24 06:24:30 +0000609 /* NV12 with scanout must flow through virgl_add_combination, so that the native
610 * support is checked and scanout use_flag can be conditionally stripped. */
611 virgl_add_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
612 BO_USE_TEXTURE_MASK | BO_USE_CAMERA_READ |
613 BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
614 BO_USE_HW_VIDEO_ENCODER | BO_USE_SCANOUT);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700615 } else {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700616 /* Virtio primary plane only allows this format. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800617 virgl_add_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
618 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700619 /* Virtio cursor plane only allows this format and Chrome cannot live without
620 * ARGB888 renderable format. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800621 virgl_add_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
622 BO_USE_RENDER_MASK | BO_USE_CURSOR);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700623 /* Android needs more, but they cannot be bound as scanouts anymore after
624 * "drm/virtio: fix DRM_FORMAT_* handling" */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800625 virgl_add_combinations(drv, render_target_formats,
626 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
627 BO_USE_RENDER_MASK);
628 virgl_add_combinations(drv, dumb_texture_source_formats,
629 ARRAY_SIZE(dumb_texture_source_formats), &LINEAR_METADATA,
630 BO_USE_TEXTURE_MASK);
Yiwei Zhang9fa17e72021-09-17 22:11:29 +0000631 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
632 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
633 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700634 }
Lepton Wu249e8632018-04-05 12:50:03 -0700635
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700636 /* Android CTS tests require this. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800637 virgl_add_combination(drv, DRM_FORMAT_RGB888, &LINEAR_METADATA, BO_USE_SW_MASK);
638 virgl_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
Jason Macnakdbc63f72022-06-23 18:34:55 -0700639 /* Android Camera CTS tests requires this. Additionally, the scanout usage is needed for
640 * Camera preview and is expected to be conditionally stripped by virgl_add_combination
641 * when not natively supported and instead handled by HWComposer. */
642 virgl_add_combination(drv, DRM_FORMAT_P010, &LINEAR_METADATA,
643 BO_USE_SCANOUT | BO_USE_TEXTURE | BO_USE_SW_MASK |
644 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900645 drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
David Staessens04b7e242020-05-28 15:47:15 +0900646 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000647 BO_USE_HW_VIDEO_ENCODER | BO_USE_GPU_DATA_BUFFER);
David Stevens519978f2020-12-11 14:09:56 +0900648
649 if (!priv->host_gbm_enabled) {
650 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &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_XBGR8888, &LINEAR_METADATA,
654 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
655 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
656 drv_modify_combination(drv, DRM_FORMAT_NV21, &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_R16, &LINEAR_METADATA,
660 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
661 BO_USE_HW_VIDEO_DECODER);
662 drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA,
663 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
664 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
665 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &LINEAR_METADATA,
666 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
667 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
668 }
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900669
Lepton Wu249e8632018-04-05 12:50:03 -0700670 return drv_modify_linear_combinations(drv);
671}
672
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800673static void virgl_close(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700674{
675 free(drv->priv);
676 drv->priv = NULL;
677}
678
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800679static int virgl_bo_create_blob(struct driver *drv, struct bo *bo)
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700680{
681 int ret;
682 uint32_t stride;
David Stevens0fe561f2020-10-28 16:06:38 +0900683 uint32_t cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700684 uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 };
685 struct drm_virtgpu_resource_create_blob drm_rc_blob = { 0 };
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800686 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700687
David Stevensd3f07bd2020-09-25 18:52:26 +0900688 uint32_t blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE;
689 if (bo->meta.use_flags & BO_USE_SW_MASK)
690 blob_flags |= VIRTGPU_BLOB_FLAG_USE_MAPPABLE;
David Stevens1b252e22021-08-03 16:48:17 +0900691
692 // For now, all blob use cases are cross device. When we add wider
693 // support for blobs, we can revisit making this unconditional.
694 blob_flags |= VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE;
David Stevensb42624c2020-09-10 10:50:26 +0900695
David Stevens0fe561f2020-10-28 16:06:38 +0900696 cur_blob_id = atomic_fetch_add(&priv->next_blob_id, 1);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700697 stride = drv_stride_from_format(bo->meta.format, bo->meta.width, 0);
698 drv_bo_from_format(bo, stride, bo->meta.height, bo->meta.format);
699 bo->meta.total_size = ALIGN(bo->meta.total_size, PAGE_SIZE);
David Stevensb42624c2020-09-10 10:50:26 +0900700 bo->meta.tiling = blob_flags;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700701
702 cmd[0] = VIRGL_CMD0(VIRGL_CCMD_PIPE_RESOURCE_CREATE, 0, VIRGL_PIPE_RES_CREATE_SIZE);
703 cmd[VIRGL_PIPE_RES_CREATE_TARGET] = PIPE_TEXTURE_2D;
704 cmd[VIRGL_PIPE_RES_CREATE_WIDTH] = bo->meta.width;
705 cmd[VIRGL_PIPE_RES_CREATE_HEIGHT] = bo->meta.height;
706 cmd[VIRGL_PIPE_RES_CREATE_FORMAT] = translate_format(bo->meta.format);
David Stevenscf280482020-12-21 11:43:44 +0900707 cmd[VIRGL_PIPE_RES_CREATE_BIND] =
708 compute_virgl_bind_flags(bo->meta.use_flags, bo->meta.format);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700709 cmd[VIRGL_PIPE_RES_CREATE_DEPTH] = 1;
David Stevens0fe561f2020-10-28 16:06:38 +0900710 cmd[VIRGL_PIPE_RES_CREATE_BLOB_ID] = cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700711
712 drm_rc_blob.cmd = (uint64_t)&cmd;
713 drm_rc_blob.cmd_size = 4 * (VIRGL_PIPE_RES_CREATE_SIZE + 1);
714 drm_rc_blob.size = bo->meta.total_size;
715 drm_rc_blob.blob_mem = VIRTGPU_BLOB_MEM_HOST3D;
David Stevensb42624c2020-09-10 10:50:26 +0900716 drm_rc_blob.blob_flags = blob_flags;
David Stevens0fe561f2020-10-28 16:06:38 +0900717 drm_rc_blob.blob_id = cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700718
719 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &drm_rc_blob);
720 if (ret < 0) {
721 drv_log("DRM_VIRTGPU_RESOURCE_CREATE_BLOB failed with %s\n", strerror(errno));
722 return -errno;
723 }
724
725 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
726 bo->handles[plane].u32 = drm_rc_blob.bo_handle;
727
728 return 0;
729}
730
731static bool should_use_blob(struct driver *drv, uint32_t format, uint64_t use_flags)
732{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800733 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700734
735 // TODO(gurchetansingh): remove once all minigbm users are blob-safe
736#ifndef VIRTIO_GPU_NEXT
737 return false;
738#endif
739
740 // Only use blob when host gbm is available
741 if (!priv->host_gbm_enabled)
742 return false;
743
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000744 // Use regular resources if only the GPU needs efficient access. Blob resource is a better
745 // fit for BO_USE_GPU_DATA_BUFFER which is mapped to VIRGL_BIND_LINEAR.
746 if (!(use_flags & (BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN | BO_USE_LINEAR |
747 BO_USE_NON_GPU_HW | BO_USE_GPU_DATA_BUFFER)))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700748 return false;
749
David Stevensd3f07bd2020-09-25 18:52:26 +0900750 switch (format) {
David Stevensd3f07bd2020-09-25 18:52:26 +0900751 case DRM_FORMAT_R8:
752 // Formats with strictly defined strides are supported
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700753 return true;
David Stevensc6df2b22021-08-10 19:02:09 +0900754 case DRM_FORMAT_YVU420_ANDROID:
David Stevensd3f07bd2020-09-25 18:52:26 +0900755 case DRM_FORMAT_NV12:
756 // Knowing buffer metadata at buffer creation isn't yet supported, so buffers
757 // can't be properly mapped into the guest.
758 return (use_flags & BO_USE_SW_MASK) == 0;
759 default:
760 return false;
761 }
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700762}
763
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800764static int virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
765 uint64_t use_flags)
Lepton Wu249e8632018-04-05 12:50:03 -0700766{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800767 if (params[param_resource_blob].value && params[param_host_visible].value &&
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700768 should_use_blob(bo->drv, format, use_flags))
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800769 return virgl_bo_create_blob(bo->drv, bo);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700770
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800771 if (params[param_3d].value)
772 return virgl_3d_bo_create(bo, width, height, format, use_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700773 else
Jason Macnakc06cc9c2021-10-06 10:16:19 -0700774 return virgl_2d_dumb_bo_create(bo, width, height, format, use_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700775}
776
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800777static int virgl_bo_destroy(struct bo *bo)
Lepton Wu249e8632018-04-05 12:50:03 -0700778{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800779 if (params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700780 return drv_gem_bo_destroy(bo);
781 else
782 return drv_dumb_bo_destroy(bo);
783}
784
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800785static void *virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Lepton Wu249e8632018-04-05 12:50:03 -0700786{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800787 if (params[param_3d].value)
788 return virgl_3d_bo_map(bo, vma, plane, map_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700789 else
790 return drv_dumb_bo_map(bo, vma, plane, map_flags);
791}
792
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800793static int virgl_bo_invalidate(struct bo *bo, struct mapping *mapping)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700794{
795 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800796 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700797 struct drm_virtgpu_3d_transfer_from_host xfer = { 0 };
798 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800799 struct virtio_transfers_params xfer_params;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800800 struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv;
David Stevens9fe8c202020-12-21 18:47:55 +0900801 uint64_t host_write_flags;
Lepton Wu249e8632018-04-05 12:50:03 -0700802
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800803 if (!params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700804 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700805
David Stevens9fe8c202020-12-21 18:47:55 +0900806 // Invalidate is only necessary if the host writes to the buffer. The encoder and
807 // decoder flags don't differentiate between input and output buffers, but we can
808 // use the format to determine whether this buffer could be encoder/decoder output.
Jason Macnakdbc63f72022-06-23 18:34:55 -0700809 host_write_flags = BO_USE_RENDERING | BO_USE_CAMERA_WRITE | BO_USE_GPU_DATA_BUFFER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800810 if (bo->meta.format == DRM_FORMAT_R8)
David Stevens9fe8c202020-12-21 18:47:55 +0900811 host_write_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800812 else
David Stevens9fe8c202020-12-21 18:47:55 +0900813 host_write_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800814
David Stevens9fe8c202020-12-21 18:47:55 +0900815 if ((bo->meta.use_flags & host_write_flags) == 0)
David Stevens4d5358d2019-10-24 14:59:31 +0900816 return 0;
817
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800818 if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700819 return 0;
820
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700821 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700822
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700823 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700824 /*
825 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
826 * images
827 */
828 if (bo->meta.num_planes == 1) {
829 xfer.offset =
830 (bo->meta.strides[0] * mapping->rect.y) +
831 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
832 }
833 }
834
David Stevensbaab6c82020-02-26 17:14:43 +0900835 if ((bo->meta.use_flags & BO_USE_RENDERING) == 0) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800836 // Unfortunately, the kernel doesn't actually pass the guest layer_stride
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800837 // and guest stride to the host (compare virgl.h and virtgpu_drm.h).
Jason Macnak1de7f662020-01-24 15:05:57 -0800838 // For gbm based resources, we can work around this by using the level field
839 // to pass the stride to virglrenderer's gbm transfer code. However, we need
840 // to avoid doing this for resources which don't rely on that transfer code,
841 // which is resources with the BO_USE_RENDERING flag set.
David Stevensbaab6c82020-02-26 17:14:43 +0900842 // TODO(b/145993887): Send also stride when the patches are landed
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800843 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800844 xfer.level = bo->meta.strides[0];
David Stevensbaab6c82020-02-26 17:14:43 +0900845 }
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700846
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800847 if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800848 xfer_params.xfers_needed = 1;
849 xfer_params.xfer_boxes[0] = mapping->rect;
850 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800851 assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format,
852 bo->meta.use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800853
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800854 virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
Jason Macnak1de7f662020-01-24 15:05:57 -0800855 }
856
857 for (i = 0; i < xfer_params.xfers_needed; i++) {
858 xfer.box.x = xfer_params.xfer_boxes[i].x;
859 xfer.box.y = xfer_params.xfer_boxes[i].y;
860 xfer.box.w = xfer_params.xfer_boxes[i].width;
861 xfer.box.h = xfer_params.xfer_boxes[i].height;
862 xfer.box.d = 1;
863
864 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
865 if (ret) {
866 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n",
867 strerror(errno));
868 return -errno;
869 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700870 }
871
David Stevens4d5358d2019-10-24 14:59:31 +0900872 // The transfer needs to complete before invalidate returns so that any host changes
873 // are visible and to ensure the host doesn't overwrite subsequent guest changes.
874 // TODO(b/136733358): Support returning fences from transfers
David Stevens4d5358d2019-10-24 14:59:31 +0900875 waitcmd.handle = mapping->vma->handle;
876 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
877 if (ret) {
878 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
879 return -errno;
880 }
881
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700882 return 0;
883}
884
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800885static int virgl_bo_flush(struct bo *bo, struct mapping *mapping)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700886{
887 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800888 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700889 struct drm_virtgpu_3d_transfer_to_host xfer = { 0 };
890 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800891 struct virtio_transfers_params xfer_params;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800892 struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv;
Lepton Wu249e8632018-04-05 12:50:03 -0700893
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800894 if (!params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700895 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700896
897 if (!(mapping->vma->map_flags & BO_MAP_WRITE))
898 return 0;
899
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800900 if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700901 return 0;
902
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700903 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700904
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700905 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700906 /*
907 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
908 * images
909 */
910 if (bo->meta.num_planes == 1) {
911 xfer.offset =
912 (bo->meta.strides[0] * mapping->rect.y) +
913 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
914 }
915 }
916
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700917 // Unfortunately, the kernel doesn't actually pass the guest layer_stride and
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800918 // guest stride to the host (compare virgl.h and virtgpu_drm.h). We can use
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700919 // the level to work around this.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800920 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800921 xfer.level = bo->meta.strides[0];
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700922
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800923 if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800924 xfer_params.xfers_needed = 1;
925 xfer_params.xfer_boxes[0] = mapping->rect;
926 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800927 assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format,
928 bo->meta.use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800929
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800930 virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
Jason Macnak1de7f662020-01-24 15:05:57 -0800931 }
932
933 for (i = 0; i < xfer_params.xfers_needed; i++) {
934 xfer.box.x = xfer_params.xfer_boxes[i].x;
935 xfer.box.y = xfer_params.xfer_boxes[i].y;
936 xfer.box.w = xfer_params.xfer_boxes[i].width;
937 xfer.box.h = xfer_params.xfer_boxes[i].height;
938 xfer.box.d = 1;
939
940 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
941 if (ret) {
942 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n",
943 strerror(errno));
944 return -errno;
945 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700946 }
947
David Stevensbaab6c82020-02-26 17:14:43 +0900948 // If the buffer is only accessed by the host GPU, then the flush is ordered
949 // with subsequent commands. However, if other host hardware can access the
950 // buffer, we need to wait for the transfer to complete for consistency.
951 // TODO(b/136733358): Support returning fences from transfers
952 if (bo->meta.use_flags & BO_USE_NON_GPU_HW) {
David Stevensbaab6c82020-02-26 17:14:43 +0900953 waitcmd.handle = mapping->vma->handle;
954
955 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
956 if (ret) {
957 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
958 return -errno;
959 }
960 }
961
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700962 return 0;
963}
964
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000965static void virgl_3d_resolve_format_and_use_flags(struct driver *drv, uint32_t format,
966 uint64_t use_flags, uint32_t *out_format,
967 uint64_t *out_use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700968{
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000969 *out_format = format;
970 *out_use_flags = use_flags;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700971 switch (format) {
972 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900973 /* Camera subsystem requires NV12. */
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000974 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) {
975 *out_format = DRM_FORMAT_NV12;
976 } else {
977 /* HACK: See b/28671744 */
978 *out_format = DRM_FORMAT_XBGR8888;
Yiwei Zhang3a171db2021-10-01 22:12:05 +0000979 *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000980 }
981 break;
Lepton Wu249e8632018-04-05 12:50:03 -0700982 case DRM_FORMAT_FLEX_YCbCr_420_888:
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000983 /* All of our host drivers prefer NV12 as their flexible media format.
984 * If that changes, this will need to be modified. */
985 *out_format = DRM_FORMAT_NV12;
986 /* fallthrough */
987 case DRM_FORMAT_NV12:
988 case DRM_FORMAT_ABGR8888:
989 case DRM_FORMAT_ARGB8888:
990 case DRM_FORMAT_RGB565:
991 case DRM_FORMAT_XBGR8888:
992 case DRM_FORMAT_XRGB8888:
993 /* These are the scanout capable formats to the guest. Strip scanout use_flag if the
994 * host does not natively support scanout on the requested format. */
995 if ((use_flags & BO_USE_SCANOUT) &&
996 !virgl_supports_combination_natively(drv, format, BO_USE_SCANOUT))
997 *out_use_flags &= ~BO_USE_SCANOUT;
998 break;
999 case DRM_FORMAT_YVU420_ANDROID:
1000 *out_use_flags &= ~BO_USE_SCANOUT;
1001 /* HACK: See b/172389166. Also see gbm_bo_create. */
1002 *out_use_flags |= BO_USE_LINEAR;
1003 break;
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001004 default:
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001005 break;
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001006 }
1007}
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001008
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001009static void virgl_2d_resolve_format_and_use_flags(uint32_t format, uint64_t use_flags,
1010 uint32_t *out_format, uint64_t *out_use_flags)
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001011{
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001012 *out_format = format;
1013 *out_use_flags = use_flags;
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001014
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001015 /* HACK: See crrev/c/1849773 */
1016 if (format != DRM_FORMAT_XRGB8888)
1017 *out_use_flags &= ~BO_USE_SCANOUT;
1018
1019 switch (format) {
1020 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
1021 /* Camera subsystem requires NV12. */
1022 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) {
1023 *out_format = DRM_FORMAT_NV12;
1024 } else {
1025 /* HACK: See b/28671744 */
1026 *out_format = DRM_FORMAT_XBGR8888;
Yiwei Zhang3a171db2021-10-01 22:12:05 +00001027 *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
Yiwei Zhang9420ffe2021-09-24 06:24:30 +00001028 }
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001029 break;
1030 case DRM_FORMAT_FLEX_YCbCr_420_888:
1031 *out_format = DRM_FORMAT_YVU420_ANDROID;
1032 /* fallthrough */
1033 case DRM_FORMAT_YVU420_ANDROID:
1034 *out_use_flags &= ~BO_USE_SCANOUT;
1035 /* HACK: See b/172389166. Also see gbm_bo_create. */
1036 *out_use_flags |= BO_USE_LINEAR;
1037 break;
1038 default:
1039 break;
Yiwei Zhang9420ffe2021-09-24 06:24:30 +00001040 }
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001041}
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001042
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001043static void virgl_resolve_format_and_use_flags(struct driver *drv, uint32_t format,
1044 uint64_t use_flags, uint32_t *out_format,
1045 uint64_t *out_use_flags)
1046{
1047 if (params[param_3d].value) {
1048 return virgl_3d_resolve_format_and_use_flags(drv, format, use_flags, out_format,
1049 out_use_flags);
1050 } else {
1051 return virgl_2d_resolve_format_and_use_flags(format, use_flags, out_format,
1052 out_use_flags);
1053 }
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001054}
1055
Gurchetan Singh73c141e2021-01-21 14:51:19 -08001056static int virgl_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +00001057 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001058{
1059 int ret;
Chia-I Wu2e41f632021-01-11 11:08:21 -08001060 struct drm_virtgpu_resource_info_cros res_info = { 0 };
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001061
Gurchetan Singh73c141e2021-01-21 14:51:19 -08001062 if (!params[param_3d].value)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001063 return 0;
1064
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001065 res_info.bo_handle = bo->handles[0].u32;
Chia-I Wu50855622021-01-12 12:38:09 -08001066 res_info.type = VIRTGPU_RESOURCE_INFO_TYPE_EXTENDED;
Chia-I Wu2e41f632021-01-11 11:08:21 -08001067 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO_CROS, &res_info);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001068 if (ret) {
1069 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_INFO failed with %s\n", strerror(errno));
1070 return ret;
1071 }
1072
Yiwei Zhangf58616e2021-08-26 05:54:15 +00001073 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++) {
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001074 /*
1075 * Currently, kernel v4.14 (Betty) doesn't have the extended resource info
1076 * ioctl.
1077 */
Yiwei Zhangf58616e2021-08-26 05:54:15 +00001078 if (!res_info.strides[plane])
1079 break;
1080
1081 strides[plane] = res_info.strides[plane];
1082 offsets[plane] = res_info.offsets[plane];
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001083 }
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +00001084 *format_modifier = res_info.format_modifier;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001085
1086 return 0;
1087}
1088
Jason Macnakd6666c82021-09-29 11:13:25 -07001089static uint32_t virgl_get_max_texture_2d_size(struct driver *drv)
1090{
1091 if (params[param_3d].value)
1092 return virgl_3d_get_max_texture_2d_size(drv);
1093 else
Jason Macnakc06cc9c2021-10-06 10:16:19 -07001094 return VIRGL_2D_MAX_TEXTURE_2D_SIZE;
Jason Macnakd6666c82021-09-29 11:13:25 -07001095}
1096
Gurchetan Singhbbde01e2021-02-17 08:54:28 -08001097const struct backend virtgpu_virgl = { .name = "virtgpu_virgl",
1098 .init = virgl_init,
1099 .close = virgl_close,
1100 .bo_create = virgl_bo_create,
1101 .bo_destroy = virgl_bo_destroy,
1102 .bo_import = drv_prime_bo_import,
1103 .bo_map = virgl_bo_map,
1104 .bo_unmap = drv_bo_munmap,
1105 .bo_invalidate = virgl_bo_invalidate,
1106 .bo_flush = virgl_bo_flush,
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001107 .resolve_format_and_use_flags =
1108 virgl_resolve_format_and_use_flags,
Jason Macnakd6666c82021-09-29 11:13:25 -07001109 .resource_info = virgl_resource_info,
1110 .get_max_texture_2d_size = virgl_get_max_texture_2d_size };