blob: c159c217c06851097a74fdcd5827fc756b9ad8a7 [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[] = {
Yiwei Zhang9420ffe2021-09-24 06:24:30 +000051 DRM_FORMAT_NV21, DRM_FORMAT_R8, DRM_FORMAT_R16, DRM_FORMAT_RG88,
52 DRM_FORMAT_YVU420_ANDROID, DRM_FORMAT_ABGR2101010, DRM_FORMAT_ABGR16161616F
Yiwei Zhang35aa91b2021-09-17 22:14:11 +000053};
Zach Reizner85c4c5f2017-10-04 13:15:57 -070054
Gurchetan Singh73c141e2021-01-21 14:51:19 -080055extern struct virtgpu_param params[];
56
57struct virgl_priv {
Lepton Wueebce652020-02-26 15:13:34 -080058 int caps_is_v2;
Jason Macnakddf4ec02020-02-03 16:36:46 -080059 union virgl_caps caps;
Jason Macnak1de7f662020-01-24 15:05:57 -080060 int host_gbm_enabled;
David Stevens0fe561f2020-10-28 16:06:38 +090061 atomic_int next_blob_id;
Lepton Wu249e8632018-04-05 12:50:03 -070062};
63
Kansho Nishidad97877b2019-06-14 18:28:18 +090064static uint32_t translate_format(uint32_t drm_fourcc)
Zach Reizner85c4c5f2017-10-04 13:15:57 -070065{
66 switch (drm_fourcc) {
Jason Macnak1de7f662020-01-24 15:05:57 -080067 case DRM_FORMAT_BGR888:
68 case DRM_FORMAT_RGB888:
69 return VIRGL_FORMAT_R8G8B8_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070070 case DRM_FORMAT_XRGB8888:
71 return VIRGL_FORMAT_B8G8R8X8_UNORM;
72 case DRM_FORMAT_ARGB8888:
73 return VIRGL_FORMAT_B8G8R8A8_UNORM;
74 case DRM_FORMAT_XBGR8888:
75 return VIRGL_FORMAT_R8G8B8X8_UNORM;
76 case DRM_FORMAT_ABGR8888:
77 return VIRGL_FORMAT_R8G8B8A8_UNORM;
Jason Macnak1de7f662020-01-24 15:05:57 -080078 case DRM_FORMAT_ABGR16161616F:
Lepton Wufef113c2020-10-30 16:29:26 -070079 return VIRGL_FORMAT_R16G16B16A16_FLOAT;
Nataraj Deshpande450e5762021-06-30 12:10:55 -070080 case DRM_FORMAT_ABGR2101010:
81 return VIRGL_FORMAT_R10G10B10A2_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070082 case DRM_FORMAT_RGB565:
83 return VIRGL_FORMAT_B5G6R5_UNORM;
84 case DRM_FORMAT_R8:
85 return VIRGL_FORMAT_R8_UNORM;
Jason Macnak6e200ea2021-02-11 19:34:57 -080086 case DRM_FORMAT_R16:
87 return VIRGL_FORMAT_R16_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070088 case DRM_FORMAT_RG88:
89 return VIRGL_FORMAT_R8G8_UNORM;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -070090 case DRM_FORMAT_NV12:
91 return VIRGL_FORMAT_NV12;
Jason Macnak1de7f662020-01-24 15:05:57 -080092 case DRM_FORMAT_NV21:
93 return VIRGL_FORMAT_NV21;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -070094 case DRM_FORMAT_YVU420:
95 case DRM_FORMAT_YVU420_ANDROID:
96 return VIRGL_FORMAT_YV12;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070097 default:
Jason Macnak6e200ea2021-02-11 19:34:57 -080098 drv_log("Unhandled format:%d\n", drm_fourcc);
Zach Reizner85c4c5f2017-10-04 13:15:57 -070099 return 0;
100 }
101}
102
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800103static bool virgl_bitmask_supports_format(struct virgl_supported_format_mask *supported,
104 uint32_t drm_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800105{
106 uint32_t virgl_format = translate_format(drm_format);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800107 if (!virgl_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800108 return false;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800109
110 uint32_t bitmask_index = virgl_format / 32;
111 uint32_t bit_index = virgl_format % 32;
112 return supported->bitmask[bitmask_index] & (1 << bit_index);
113}
114
Jason Macnak1de7f662020-01-24 15:05:57 -0800115// The metadata generated here for emulated buffers is slightly different than the metadata
116// generated by drv_bo_from_format. In order to simplify transfers in the flush and invalidate
117// functions below, the emulated buffers are oversized. For example, ignoring stride alignment
118// requirements to demonstrate, a 6x6 YUV420 image buffer might have the following layout from
119// drv_bo_from_format:
120//
121// | Y | Y | Y | Y | Y | Y |
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// | U | U | U | U | U | U |
128// | U | U | U | V | V | V |
129// | V | V | V | V | V | V |
130//
131// where each plane immediately follows the previous plane in memory. This layout makes it
132// difficult to compute the transfers needed for example when the middle 2x2 region of the
133// image is locked and needs to be flushed/invalidated.
134//
135// Emulated multi-plane buffers instead have a layout of:
136//
137// | Y | Y | Y | Y | Y | Y |
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// | U | U | U | | | |
144// | U | U | U | | | |
145// | U | U | U | | | |
146// | V | V | V | | | |
147// | V | V | V | | | |
148// | V | V | V | | | |
149//
150// where each plane is placed as a sub-image (albeit with a very large stride) in order to
151// simplify transfers into 3 sub-image transfers for the above example.
152//
153// Additional note: the V-plane is not placed to the right of the U-plane due to some
154// observed failures in media framework code which assumes the V-plane is not
155// "row-interlaced" with the U-plane.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800156static void virgl_get_emulated_metadata(const struct bo *bo, struct bo_metadata *metadata)
Jason Macnak1de7f662020-01-24 15:05:57 -0800157{
158 uint32_t y_plane_height;
159 uint32_t c_plane_height;
160 uint32_t original_width = bo->meta.width;
161 uint32_t original_height = bo->meta.height;
162
163 metadata->format = DRM_FORMAT_R8;
164 switch (bo->meta.format) {
165 case DRM_FORMAT_NV12:
166 case DRM_FORMAT_NV21:
167 // Bi-planar
168 metadata->num_planes = 2;
169
170 y_plane_height = original_height;
171 c_plane_height = DIV_ROUND_UP(original_height, 2);
172
173 metadata->width = original_width;
174 metadata->height = y_plane_height + c_plane_height;
175
176 // Y-plane (full resolution)
177 metadata->strides[0] = metadata->width;
178 metadata->offsets[0] = 0;
179 metadata->sizes[0] = metadata->width * y_plane_height;
180
181 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
182 metadata->strides[1] = metadata->width;
183 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
184 metadata->sizes[1] = metadata->width * c_plane_height;
185
186 metadata->total_size = metadata->width * metadata->height;
187 break;
188 case DRM_FORMAT_YVU420:
189 case DRM_FORMAT_YVU420_ANDROID:
190 // Tri-planar
191 metadata->num_planes = 3;
192
193 y_plane_height = original_height;
194 c_plane_height = DIV_ROUND_UP(original_height, 2);
195
196 metadata->width = ALIGN(original_width, 32);
197 metadata->height = y_plane_height + (2 * c_plane_height);
198
199 // Y-plane (full resolution)
200 metadata->strides[0] = metadata->width;
201 metadata->offsets[0] = 0;
202 metadata->sizes[0] = metadata->width * original_height;
203
204 // Cb-plane (half resolution, placed below Y-plane)
205 metadata->strides[1] = metadata->width;
206 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
207 metadata->sizes[1] = metadata->width * c_plane_height;
208
209 // Cr-plane (half resolution, placed below Cb-plane)
210 metadata->strides[2] = metadata->width;
211 metadata->offsets[2] = metadata->offsets[1] + metadata->sizes[1];
212 metadata->sizes[2] = metadata->width * c_plane_height;
213
214 metadata->total_size = metadata->width * metadata->height;
215 break;
216 default:
217 break;
218 }
219}
220
221struct virtio_transfers_params {
222 size_t xfers_needed;
223 struct rectangle xfer_boxes[DRV_MAX_PLANES];
224};
225
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800226static void virgl_get_emulated_transfers_params(const struct bo *bo,
227 const struct rectangle *transfer_box,
228 struct virtio_transfers_params *xfer_params)
Jason Macnak1de7f662020-01-24 15:05:57 -0800229{
230 uint32_t y_plane_height;
231 uint32_t c_plane_height;
232 struct bo_metadata emulated_metadata;
233
234 if (transfer_box->x == 0 && transfer_box->y == 0 && transfer_box->width == bo->meta.width &&
235 transfer_box->height == bo->meta.height) {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800236 virgl_get_emulated_metadata(bo, &emulated_metadata);
Jason Macnak1de7f662020-01-24 15:05:57 -0800237
238 xfer_params->xfers_needed = 1;
239 xfer_params->xfer_boxes[0].x = 0;
240 xfer_params->xfer_boxes[0].y = 0;
241 xfer_params->xfer_boxes[0].width = emulated_metadata.width;
242 xfer_params->xfer_boxes[0].height = emulated_metadata.height;
243
244 return;
245 }
246
247 switch (bo->meta.format) {
248 case DRM_FORMAT_NV12:
249 case DRM_FORMAT_NV21:
250 // Bi-planar
251 xfer_params->xfers_needed = 2;
252
253 y_plane_height = bo->meta.height;
254 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
255
256 // Y-plane (full resolution)
257 xfer_params->xfer_boxes[0].x = transfer_box->x;
258 xfer_params->xfer_boxes[0].y = transfer_box->y;
259 xfer_params->xfer_boxes[0].width = transfer_box->width;
260 xfer_params->xfer_boxes[0].height = transfer_box->height;
261
262 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
263 xfer_params->xfer_boxes[1].x = transfer_box->x;
264 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
265 xfer_params->xfer_boxes[1].width = transfer_box->width;
266 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
267
268 break;
269 case DRM_FORMAT_YVU420:
270 case DRM_FORMAT_YVU420_ANDROID:
271 // Tri-planar
272 xfer_params->xfers_needed = 3;
273
274 y_plane_height = bo->meta.height;
275 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
276
277 // Y-plane (full resolution)
278 xfer_params->xfer_boxes[0].x = transfer_box->x;
279 xfer_params->xfer_boxes[0].y = transfer_box->y;
280 xfer_params->xfer_boxes[0].width = transfer_box->width;
281 xfer_params->xfer_boxes[0].height = transfer_box->height;
282
283 // Cb-plane (half resolution, placed below Y-plane)
284 xfer_params->xfer_boxes[1].x = transfer_box->x;
285 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
286 xfer_params->xfer_boxes[1].width = DIV_ROUND_UP(transfer_box->width, 2);
287 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
288
289 // Cr-plane (half resolution, placed below Cb-plane)
290 xfer_params->xfer_boxes[2].x = transfer_box->x;
291 xfer_params->xfer_boxes[2].y = transfer_box->y + y_plane_height + c_plane_height;
292 xfer_params->xfer_boxes[2].width = DIV_ROUND_UP(transfer_box->width, 2);
293 xfer_params->xfer_boxes[2].height = DIV_ROUND_UP(transfer_box->height, 2);
294
295 break;
296 }
297}
298
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800299static bool virgl_supports_combination_natively(struct driver *drv, uint32_t drm_format,
300 uint64_t use_flags)
Jason Macnak1de7f662020-01-24 15:05:57 -0800301{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800302 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800303
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800304 if (priv->caps.max_version == 0)
Jason Macnak1de7f662020-01-24 15:05:57 -0800305 return true;
Jason Macnak1de7f662020-01-24 15:05:57 -0800306
307 if ((use_flags & BO_USE_RENDERING) &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800308 !virgl_bitmask_supports_format(&priv->caps.v1.render, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800309 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800310
311 if ((use_flags & BO_USE_TEXTURE) &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800312 !virgl_bitmask_supports_format(&priv->caps.v1.sampler, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800313 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800314
315 if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800316 !virgl_bitmask_supports_format(&priv->caps.v2.scanout, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800317 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800318
319 return true;
320}
321
322// For virtio backends that do not support formats natively (e.g. multi-planar formats are not
323// supported in virglrenderer when gbm is unavailable on the host machine), whether or not the
324// format and usage combination can be handled as a blob (byte buffer).
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800325static bool virgl_supports_combination_through_emulation(struct driver *drv, uint32_t drm_format,
326 uint64_t use_flags)
Jason Macnak1de7f662020-01-24 15:05:57 -0800327{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800328 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800329
330 // Only enable emulation on non-gbm virtio backends.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800331 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800332 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800333
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800334 if (use_flags & (BO_USE_RENDERING | BO_USE_SCANOUT))
Jason Macnak1de7f662020-01-24 15:05:57 -0800335 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800336
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800337 if (!virgl_supports_combination_natively(drv, DRM_FORMAT_R8, use_flags))
Jason Macnak1de7f662020-01-24 15:05:57 -0800338 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800339
340 return drm_format == DRM_FORMAT_NV12 || drm_format == DRM_FORMAT_NV21 ||
341 drm_format == DRM_FORMAT_YVU420 || drm_format == DRM_FORMAT_YVU420_ANDROID;
342}
343
Jason Macnakddf4ec02020-02-03 16:36:46 -0800344// Adds the given buffer combination to the list of supported buffer combinations if the
345// combination is supported by the virtio backend.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800346static void virgl_add_combination(struct driver *drv, uint32_t drm_format,
347 struct format_metadata *metadata, uint64_t use_flags)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800348{
Yiwei Zhang9420ffe2021-09-24 06:24:30 +0000349 if (params[param_3d].value) {
350 if ((use_flags & BO_USE_SCANOUT) &&
351 !virgl_supports_combination_natively(drv, drm_format, BO_USE_SCANOUT)) {
352 drv_log("Strip scanout on format: %d\n", drm_format);
Jason Macnak1de7f662020-01-24 15:05:57 -0800353 use_flags &= ~BO_USE_SCANOUT;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800354 }
355
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800356 if (!virgl_supports_combination_natively(drv, drm_format, use_flags) &&
357 !virgl_supports_combination_through_emulation(drv, drm_format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800358 drv_log("Skipping unsupported combination format:%d\n", drm_format);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800359 return;
360 }
361 }
362
363 drv_add_combination(drv, drm_format, metadata, use_flags);
364}
365
366// Adds each given buffer combination to the list of supported buffer combinations if the
367// combination supported by the virtio backend.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800368static void virgl_add_combinations(struct driver *drv, const uint32_t *drm_formats,
369 uint32_t num_formats, struct format_metadata *metadata,
370 uint64_t use_flags)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800371{
372 uint32_t i;
373
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800374 for (i = 0; i < num_formats; i++)
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800375 virgl_add_combination(drv, drm_formats[i], metadata, use_flags);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800376}
377
Jason Macnakc06cc9c2021-10-06 10:16:19 -0700378static int virgl_2d_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
379 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700380{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700381 if (bo->meta.format != DRM_FORMAT_R8) {
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900382 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
383 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
384 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700385
Dominik Behr6e6dc492019-10-09 15:43:52 -0700386 return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_DUMB32BPP);
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700387}
388
Lepton Wudbab0832019-04-19 12:26:39 -0700389static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
390 uint32_t virgl_bind)
391{
392 if ((*flag) & check_flag) {
393 (*flag) &= ~check_flag;
394 (*bind) |= virgl_bind;
395 }
396}
397
David Stevenscf280482020-12-21 11:43:44 +0900398static uint32_t compute_virgl_bind_flags(uint64_t use_flags, uint32_t format)
Lepton Wudbab0832019-04-19 12:26:39 -0700399{
Kansho Nishidad97877b2019-06-14 18:28:18 +0900400 /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
401 uint32_t bind = VIRGL_BIND_SHARED;
Lepton Wudbab0832019-04-19 12:26:39 -0700402
403 handle_flag(&use_flags, BO_USE_TEXTURE, &bind, VIRGL_BIND_SAMPLER_VIEW);
404 handle_flag(&use_flags, BO_USE_RENDERING, &bind, VIRGL_BIND_RENDER_TARGET);
405 handle_flag(&use_flags, BO_USE_SCANOUT, &bind, VIRGL_BIND_SCANOUT);
David Stevens55a6cf92019-09-03 10:45:33 +0900406 handle_flag(&use_flags, BO_USE_CURSOR, &bind, VIRGL_BIND_CURSOR);
407 handle_flag(&use_flags, BO_USE_LINEAR, &bind, VIRGL_BIND_LINEAR);
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000408 handle_flag(&use_flags, BO_USE_GPU_DATA_BUFFER, &bind, VIRGL_BIND_LINEAR);
Yiwei Zhangd3a73ff2021-07-08 05:48:01 +0000409 handle_flag(&use_flags, BO_USE_FRONT_RENDERING, &bind, VIRGL_BIND_LINEAR);
David Stevens55a6cf92019-09-03 10:45:33 +0900410
David Stevens23de4e22020-05-15 14:15:35 +0900411 if (use_flags & BO_USE_PROTECTED) {
412 handle_flag(&use_flags, BO_USE_PROTECTED, &bind, VIRGL_BIND_MINIGBM_PROTECTED);
413 } else {
414 // Make sure we don't set both flags, since that could be mistaken for
415 // protected. Give OFTEN priority over RARELY.
416 if (use_flags & BO_USE_SW_READ_OFTEN) {
417 handle_flag(&use_flags, BO_USE_SW_READ_OFTEN, &bind,
418 VIRGL_BIND_MINIGBM_SW_READ_OFTEN);
419 } else {
420 handle_flag(&use_flags, BO_USE_SW_READ_RARELY, &bind,
421 VIRGL_BIND_MINIGBM_SW_READ_RARELY);
422 }
423 if (use_flags & BO_USE_SW_WRITE_OFTEN) {
424 handle_flag(&use_flags, BO_USE_SW_WRITE_OFTEN, &bind,
425 VIRGL_BIND_MINIGBM_SW_WRITE_OFTEN);
426 } else {
427 handle_flag(&use_flags, BO_USE_SW_WRITE_RARELY, &bind,
428 VIRGL_BIND_MINIGBM_SW_WRITE_RARELY);
429 }
430 }
David Stevens55a6cf92019-09-03 10:45:33 +0900431
David Stevens23de4e22020-05-15 14:15:35 +0900432 handle_flag(&use_flags, BO_USE_CAMERA_WRITE, &bind, VIRGL_BIND_MINIGBM_CAMERA_WRITE);
433 handle_flag(&use_flags, BO_USE_CAMERA_READ, &bind, VIRGL_BIND_MINIGBM_CAMERA_READ);
434 handle_flag(&use_flags, BO_USE_HW_VIDEO_DECODER, &bind,
435 VIRGL_BIND_MINIGBM_HW_VIDEO_DECODER);
436 handle_flag(&use_flags, BO_USE_HW_VIDEO_ENCODER, &bind,
437 VIRGL_BIND_MINIGBM_HW_VIDEO_ENCODER);
David Stevens55a6cf92019-09-03 10:45:33 +0900438
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800439 if (use_flags)
Lepton Wudbab0832019-04-19 12:26:39 -0700440 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900441
Lepton Wudbab0832019-04-19 12:26:39 -0700442 return bind;
443}
444
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800445static int virgl_3d_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
446 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700447{
448 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800449 size_t i;
Kansho Nishidad97877b2019-06-14 18:28:18 +0900450 uint32_t stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700451 struct drm_virtgpu_resource_create res_create = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800452 struct bo_metadata emulated_metadata;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700453
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800454 if (virgl_supports_combination_natively(bo->drv, format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800455 stride = drv_stride_from_format(format, width, 0);
456 drv_bo_from_format(bo, stride, height, format);
457 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800458 assert(virgl_supports_combination_through_emulation(bo->drv, format, use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800459
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800460 virgl_get_emulated_metadata(bo, &emulated_metadata);
Jason Macnak1de7f662020-01-24 15:05:57 -0800461
462 format = emulated_metadata.format;
463 width = emulated_metadata.width;
464 height = emulated_metadata.height;
465 for (i = 0; i < emulated_metadata.num_planes; i++) {
466 bo->meta.strides[i] = emulated_metadata.strides[i];
467 bo->meta.offsets[i] = emulated_metadata.offsets[i];
468 bo->meta.sizes[i] = emulated_metadata.sizes[i];
469 }
470 bo->meta.total_size = emulated_metadata.total_size;
471 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700472
Kansho Nishidad97877b2019-06-14 18:28:18 +0900473 /*
474 * Setting the target is intended to ensure this resource gets bound as a 2D
475 * texture in the host renderer's GL state. All of these resource properties are
476 * sent unchanged by the kernel to the host, which in turn sends them unchanged to
477 * virglrenderer. When virglrenderer makes a resource, it will convert the target
478 * enum to the equivalent one in GL and then bind the resource to that target.
479 */
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700480
Kansho Nishidad97877b2019-06-14 18:28:18 +0900481 res_create.target = PIPE_TEXTURE_2D;
482 res_create.format = translate_format(format);
David Stevenscf280482020-12-21 11:43:44 +0900483 res_create.bind = compute_virgl_bind_flags(use_flags, format);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900484 res_create.width = width;
485 res_create.height = height;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700486
Kansho Nishidad97877b2019-06-14 18:28:18 +0900487 /* For virgl 3D */
488 res_create.depth = 1;
489 res_create.array_size = 1;
490 res_create.last_level = 0;
491 res_create.nr_samples = 0;
492
Gurchetan Singh298b7572019-09-19 09:55:18 -0700493 res_create.size = ALIGN(bo->meta.total_size, PAGE_SIZE); // PAGE_SIZE = 0x1000
Kansho Nishidad97877b2019-06-14 18:28:18 +0900494 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create);
495 if (ret) {
496 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
497 return ret;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700498 }
499
Gurchetan Singh298b7572019-09-19 09:55:18 -0700500 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
Kansho Nishidad97877b2019-06-14 18:28:18 +0900501 bo->handles[plane].u32 = res_create.bo_handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700502
503 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700504}
505
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800506static 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 -0700507{
508 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700509 struct drm_virtgpu_map gem_map = { 0 };
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700510
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700511 gem_map.handle = bo->handles[0].u32;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700512 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
513 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700514 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700515 return MAP_FAILED;
516 }
517
Gurchetan Singh298b7572019-09-19 09:55:18 -0700518 vma->length = bo->meta.total_size;
519 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700520 gem_map.offset);
521}
522
Jason Macnakd6666c82021-09-29 11:13:25 -0700523static uint32_t virgl_3d_get_max_texture_2d_size(struct driver *drv)
524{
525 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
526
527 if (priv->caps.v2.max_texture_2d_size)
528 return priv->caps.v2.max_texture_2d_size;
529
530 return UINT32_MAX;
531}
532
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800533static int virgl_get_caps(struct driver *drv, union virgl_caps *caps, int *caps_is_v2)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800534{
535 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700536 struct drm_virtgpu_get_caps cap_args = { 0 };
Jason Macnakddf4ec02020-02-03 16:36:46 -0800537
Lepton Wueebce652020-02-26 15:13:34 -0800538 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800539 cap_args.addr = (unsigned long long)caps;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800540 if (params[param_capset_fix].value) {
Lepton Wueebce652020-02-26 15:13:34 -0800541 *caps_is_v2 = 1;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800542 cap_args.cap_set_id = 2;
543 cap_args.size = sizeof(union virgl_caps);
544 } else {
545 cap_args.cap_set_id = 1;
546 cap_args.size = sizeof(struct virgl_caps_v1);
547 }
548
549 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
550 if (ret) {
551 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Lepton Wueebce652020-02-26 15:13:34 -0800552 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800553
554 // Fallback to v1
555 cap_args.cap_set_id = 1;
556 cap_args.size = sizeof(struct virgl_caps_v1);
557
558 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800559 if (ret)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800560 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Jason Macnakddf4ec02020-02-03 16:36:46 -0800561 }
562
563 return ret;
564}
565
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800566static void virgl_init_params_and_caps(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700567{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800568 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
569 if (params[param_3d].value) {
570 virgl_get_caps(drv, &priv->caps, &priv->caps_is_v2);
Lepton Wu249e8632018-04-05 12:50:03 -0700571
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800572 // We use two criteria to determine whether host minigbm is used on the host for
573 // swapchain allocations.
574 //
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800575 // (1) Host minigbm is only available via virglrenderer, and only virglrenderer
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800576 // advertises capabilities.
577 // (2) Only host minigbm doesn't emulate YUV formats. Checking this is a bit of a
578 // proxy, but it works.
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800579 priv->host_gbm_enabled =
580 priv->caps.max_version > 0 &&
581 virgl_supports_combination_natively(drv, DRM_FORMAT_NV12, BO_USE_TEXTURE);
Lepton Wu249e8632018-04-05 12:50:03 -0700582 }
Jason Macnak1de7f662020-01-24 15:05:57 -0800583}
584
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800585static int virgl_init(struct driver *drv)
Jason Macnak1de7f662020-01-24 15:05:57 -0800586{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800587 struct virgl_priv *priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800588
589 priv = calloc(1, sizeof(*priv));
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000590 if (!priv)
591 return -ENOMEM;
592
Jason Macnak1de7f662020-01-24 15:05:57 -0800593 drv->priv = priv;
594
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800595 virgl_init_params_and_caps(drv);
Jason Macnak1de7f662020-01-24 15:05:57 -0800596
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800597 if (params[param_3d].value) {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700598 /* This doesn't mean host can scanout everything, it just means host
599 * hypervisor can show it. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800600 virgl_add_combinations(drv, render_target_formats,
601 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
602 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
603 virgl_add_combinations(drv, texture_source_formats,
604 ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
605 BO_USE_TEXTURE_MASK);
Yiwei Zhang9420ffe2021-09-24 06:24:30 +0000606 /* NV12 with scanout must flow through virgl_add_combination, so that the native
607 * support is checked and scanout use_flag can be conditionally stripped. */
608 virgl_add_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
609 BO_USE_TEXTURE_MASK | BO_USE_CAMERA_READ |
610 BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
611 BO_USE_HW_VIDEO_ENCODER | BO_USE_SCANOUT);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700612 } else {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700613 /* Virtio primary plane only allows this format. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800614 virgl_add_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
615 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700616 /* Virtio cursor plane only allows this format and Chrome cannot live without
617 * ARGB888 renderable format. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800618 virgl_add_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
619 BO_USE_RENDER_MASK | BO_USE_CURSOR);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700620 /* Android needs more, but they cannot be bound as scanouts anymore after
621 * "drm/virtio: fix DRM_FORMAT_* handling" */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800622 virgl_add_combinations(drv, render_target_formats,
623 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
624 BO_USE_RENDER_MASK);
625 virgl_add_combinations(drv, dumb_texture_source_formats,
626 ARRAY_SIZE(dumb_texture_source_formats), &LINEAR_METADATA,
627 BO_USE_TEXTURE_MASK);
Yiwei Zhang9fa17e72021-09-17 22:11:29 +0000628 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
629 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
630 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700631 }
Lepton Wu249e8632018-04-05 12:50:03 -0700632
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700633 /* Android CTS tests require this. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800634 virgl_add_combination(drv, DRM_FORMAT_RGB888, &LINEAR_METADATA, BO_USE_SW_MASK);
635 virgl_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
Jason Macnak2ce35772021-06-08 06:45:45 -0700636 virgl_add_combination(drv, DRM_FORMAT_P010, &LINEAR_METADATA,
637 BO_USE_SW_MASK | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900638 drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
David Staessens04b7e242020-05-28 15:47:15 +0900639 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000640 BO_USE_HW_VIDEO_ENCODER | BO_USE_GPU_DATA_BUFFER);
David Stevens519978f2020-12-11 14:09:56 +0900641
642 if (!priv->host_gbm_enabled) {
643 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &LINEAR_METADATA,
644 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
645 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
646 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &LINEAR_METADATA,
647 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
648 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
649 drv_modify_combination(drv, DRM_FORMAT_NV21, &LINEAR_METADATA,
650 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
651 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
652 drv_modify_combination(drv, DRM_FORMAT_R16, &LINEAR_METADATA,
653 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
654 BO_USE_HW_VIDEO_DECODER);
655 drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA,
656 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
657 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
658 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &LINEAR_METADATA,
659 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
660 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
661 }
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900662
Lepton Wu249e8632018-04-05 12:50:03 -0700663 return drv_modify_linear_combinations(drv);
664}
665
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800666static void virgl_close(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700667{
668 free(drv->priv);
669 drv->priv = NULL;
670}
671
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800672static int virgl_bo_create_blob(struct driver *drv, struct bo *bo)
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700673{
674 int ret;
675 uint32_t stride;
David Stevens0fe561f2020-10-28 16:06:38 +0900676 uint32_t cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700677 uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 };
678 struct drm_virtgpu_resource_create_blob drm_rc_blob = { 0 };
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800679 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700680
David Stevensd3f07bd2020-09-25 18:52:26 +0900681 uint32_t blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE;
682 if (bo->meta.use_flags & BO_USE_SW_MASK)
683 blob_flags |= VIRTGPU_BLOB_FLAG_USE_MAPPABLE;
David Stevens1b252e22021-08-03 16:48:17 +0900684
685 // For now, all blob use cases are cross device. When we add wider
686 // support for blobs, we can revisit making this unconditional.
687 blob_flags |= VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE;
David Stevensb42624c2020-09-10 10:50:26 +0900688
David Stevens0fe561f2020-10-28 16:06:38 +0900689 cur_blob_id = atomic_fetch_add(&priv->next_blob_id, 1);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700690 stride = drv_stride_from_format(bo->meta.format, bo->meta.width, 0);
691 drv_bo_from_format(bo, stride, bo->meta.height, bo->meta.format);
692 bo->meta.total_size = ALIGN(bo->meta.total_size, PAGE_SIZE);
David Stevensb42624c2020-09-10 10:50:26 +0900693 bo->meta.tiling = blob_flags;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700694
695 cmd[0] = VIRGL_CMD0(VIRGL_CCMD_PIPE_RESOURCE_CREATE, 0, VIRGL_PIPE_RES_CREATE_SIZE);
696 cmd[VIRGL_PIPE_RES_CREATE_TARGET] = PIPE_TEXTURE_2D;
697 cmd[VIRGL_PIPE_RES_CREATE_WIDTH] = bo->meta.width;
698 cmd[VIRGL_PIPE_RES_CREATE_HEIGHT] = bo->meta.height;
699 cmd[VIRGL_PIPE_RES_CREATE_FORMAT] = translate_format(bo->meta.format);
David Stevenscf280482020-12-21 11:43:44 +0900700 cmd[VIRGL_PIPE_RES_CREATE_BIND] =
701 compute_virgl_bind_flags(bo->meta.use_flags, bo->meta.format);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700702 cmd[VIRGL_PIPE_RES_CREATE_DEPTH] = 1;
David Stevens0fe561f2020-10-28 16:06:38 +0900703 cmd[VIRGL_PIPE_RES_CREATE_BLOB_ID] = cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700704
705 drm_rc_blob.cmd = (uint64_t)&cmd;
706 drm_rc_blob.cmd_size = 4 * (VIRGL_PIPE_RES_CREATE_SIZE + 1);
707 drm_rc_blob.size = bo->meta.total_size;
708 drm_rc_blob.blob_mem = VIRTGPU_BLOB_MEM_HOST3D;
David Stevensb42624c2020-09-10 10:50:26 +0900709 drm_rc_blob.blob_flags = blob_flags;
David Stevens0fe561f2020-10-28 16:06:38 +0900710 drm_rc_blob.blob_id = cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700711
712 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &drm_rc_blob);
713 if (ret < 0) {
714 drv_log("DRM_VIRTGPU_RESOURCE_CREATE_BLOB failed with %s\n", strerror(errno));
715 return -errno;
716 }
717
718 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
719 bo->handles[plane].u32 = drm_rc_blob.bo_handle;
720
721 return 0;
722}
723
724static bool should_use_blob(struct driver *drv, uint32_t format, uint64_t use_flags)
725{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800726 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700727
728 // TODO(gurchetansingh): remove once all minigbm users are blob-safe
729#ifndef VIRTIO_GPU_NEXT
730 return false;
731#endif
732
733 // Only use blob when host gbm is available
734 if (!priv->host_gbm_enabled)
735 return false;
736
Yiwei Zhangbb9d4af2021-06-20 19:23:38 +0000737 // Use regular resources if only the GPU needs efficient access. Blob resource is a better
738 // fit for BO_USE_GPU_DATA_BUFFER which is mapped to VIRGL_BIND_LINEAR.
739 if (!(use_flags & (BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN | BO_USE_LINEAR |
740 BO_USE_NON_GPU_HW | BO_USE_GPU_DATA_BUFFER)))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700741 return false;
742
David Stevensd3f07bd2020-09-25 18:52:26 +0900743 switch (format) {
David Stevensd3f07bd2020-09-25 18:52:26 +0900744 case DRM_FORMAT_R8:
745 // Formats with strictly defined strides are supported
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700746 return true;
David Stevensc6df2b22021-08-10 19:02:09 +0900747 case DRM_FORMAT_YVU420_ANDROID:
David Stevensd3f07bd2020-09-25 18:52:26 +0900748 case DRM_FORMAT_NV12:
749 // Knowing buffer metadata at buffer creation isn't yet supported, so buffers
750 // can't be properly mapped into the guest.
751 return (use_flags & BO_USE_SW_MASK) == 0;
752 default:
753 return false;
754 }
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700755}
756
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800757static int virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
758 uint64_t use_flags)
Lepton Wu249e8632018-04-05 12:50:03 -0700759{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800760 if (params[param_resource_blob].value && params[param_host_visible].value &&
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700761 should_use_blob(bo->drv, format, use_flags))
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800762 return virgl_bo_create_blob(bo->drv, bo);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700763
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800764 if (params[param_3d].value)
765 return virgl_3d_bo_create(bo, width, height, format, use_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700766 else
Jason Macnakc06cc9c2021-10-06 10:16:19 -0700767 return virgl_2d_dumb_bo_create(bo, width, height, format, use_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700768}
769
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800770static int virgl_bo_destroy(struct bo *bo)
Lepton Wu249e8632018-04-05 12:50:03 -0700771{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800772 if (params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700773 return drv_gem_bo_destroy(bo);
774 else
775 return drv_dumb_bo_destroy(bo);
776}
777
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800778static void *virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Lepton Wu249e8632018-04-05 12:50:03 -0700779{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800780 if (params[param_3d].value)
781 return virgl_3d_bo_map(bo, vma, plane, map_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700782 else
783 return drv_dumb_bo_map(bo, vma, plane, map_flags);
784}
785
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800786static int virgl_bo_invalidate(struct bo *bo, struct mapping *mapping)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700787{
788 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800789 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700790 struct drm_virtgpu_3d_transfer_from_host xfer = { 0 };
791 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800792 struct virtio_transfers_params xfer_params;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800793 struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv;
David Stevens9fe8c202020-12-21 18:47:55 +0900794 uint64_t host_write_flags;
Lepton Wu249e8632018-04-05 12:50:03 -0700795
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800796 if (!params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700797 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700798
David Stevens9fe8c202020-12-21 18:47:55 +0900799 // Invalidate is only necessary if the host writes to the buffer. The encoder and
800 // decoder flags don't differentiate between input and output buffers, but we can
801 // use the format to determine whether this buffer could be encoder/decoder output.
802 host_write_flags = BO_USE_RENDERING | BO_USE_CAMERA_WRITE;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800803 if (bo->meta.format == DRM_FORMAT_R8)
David Stevens9fe8c202020-12-21 18:47:55 +0900804 host_write_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800805 else
David Stevens9fe8c202020-12-21 18:47:55 +0900806 host_write_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800807
David Stevens9fe8c202020-12-21 18:47:55 +0900808 if ((bo->meta.use_flags & host_write_flags) == 0)
David Stevens4d5358d2019-10-24 14:59:31 +0900809 return 0;
810
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800811 if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700812 return 0;
813
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700814 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700815
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700816 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700817 /*
818 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
819 * images
820 */
821 if (bo->meta.num_planes == 1) {
822 xfer.offset =
823 (bo->meta.strides[0] * mapping->rect.y) +
824 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
825 }
826 }
827
David Stevensbaab6c82020-02-26 17:14:43 +0900828 if ((bo->meta.use_flags & BO_USE_RENDERING) == 0) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800829 // Unfortunately, the kernel doesn't actually pass the guest layer_stride
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800830 // and guest stride to the host (compare virgl.h and virtgpu_drm.h).
Jason Macnak1de7f662020-01-24 15:05:57 -0800831 // For gbm based resources, we can work around this by using the level field
832 // to pass the stride to virglrenderer's gbm transfer code. However, we need
833 // to avoid doing this for resources which don't rely on that transfer code,
834 // which is resources with the BO_USE_RENDERING flag set.
David Stevensbaab6c82020-02-26 17:14:43 +0900835 // TODO(b/145993887): Send also stride when the patches are landed
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800836 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800837 xfer.level = bo->meta.strides[0];
David Stevensbaab6c82020-02-26 17:14:43 +0900838 }
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700839
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800840 if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800841 xfer_params.xfers_needed = 1;
842 xfer_params.xfer_boxes[0] = mapping->rect;
843 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800844 assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format,
845 bo->meta.use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800846
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800847 virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
Jason Macnak1de7f662020-01-24 15:05:57 -0800848 }
849
850 for (i = 0; i < xfer_params.xfers_needed; i++) {
851 xfer.box.x = xfer_params.xfer_boxes[i].x;
852 xfer.box.y = xfer_params.xfer_boxes[i].y;
853 xfer.box.w = xfer_params.xfer_boxes[i].width;
854 xfer.box.h = xfer_params.xfer_boxes[i].height;
855 xfer.box.d = 1;
856
857 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
858 if (ret) {
859 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n",
860 strerror(errno));
861 return -errno;
862 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700863 }
864
David Stevens4d5358d2019-10-24 14:59:31 +0900865 // The transfer needs to complete before invalidate returns so that any host changes
866 // are visible and to ensure the host doesn't overwrite subsequent guest changes.
867 // TODO(b/136733358): Support returning fences from transfers
David Stevens4d5358d2019-10-24 14:59:31 +0900868 waitcmd.handle = mapping->vma->handle;
869 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
870 if (ret) {
871 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
872 return -errno;
873 }
874
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700875 return 0;
876}
877
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800878static int virgl_bo_flush(struct bo *bo, struct mapping *mapping)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700879{
880 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800881 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700882 struct drm_virtgpu_3d_transfer_to_host xfer = { 0 };
883 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800884 struct virtio_transfers_params xfer_params;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800885 struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv;
Lepton Wu249e8632018-04-05 12:50:03 -0700886
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800887 if (!params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700888 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700889
890 if (!(mapping->vma->map_flags & BO_MAP_WRITE))
891 return 0;
892
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800893 if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700894 return 0;
895
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700896 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700897
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700898 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700899 /*
900 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
901 * images
902 */
903 if (bo->meta.num_planes == 1) {
904 xfer.offset =
905 (bo->meta.strides[0] * mapping->rect.y) +
906 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
907 }
908 }
909
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700910 // Unfortunately, the kernel doesn't actually pass the guest layer_stride and
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800911 // guest stride to the host (compare virgl.h and virtgpu_drm.h). We can use
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700912 // the level to work around this.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800913 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800914 xfer.level = bo->meta.strides[0];
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700915
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800916 if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800917 xfer_params.xfers_needed = 1;
918 xfer_params.xfer_boxes[0] = mapping->rect;
919 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800920 assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format,
921 bo->meta.use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800922
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800923 virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
Jason Macnak1de7f662020-01-24 15:05:57 -0800924 }
925
926 for (i = 0; i < xfer_params.xfers_needed; i++) {
927 xfer.box.x = xfer_params.xfer_boxes[i].x;
928 xfer.box.y = xfer_params.xfer_boxes[i].y;
929 xfer.box.w = xfer_params.xfer_boxes[i].width;
930 xfer.box.h = xfer_params.xfer_boxes[i].height;
931 xfer.box.d = 1;
932
933 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
934 if (ret) {
935 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n",
936 strerror(errno));
937 return -errno;
938 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700939 }
940
David Stevensbaab6c82020-02-26 17:14:43 +0900941 // If the buffer is only accessed by the host GPU, then the flush is ordered
942 // with subsequent commands. However, if other host hardware can access the
943 // buffer, we need to wait for the transfer to complete for consistency.
944 // TODO(b/136733358): Support returning fences from transfers
945 if (bo->meta.use_flags & BO_USE_NON_GPU_HW) {
David Stevensbaab6c82020-02-26 17:14:43 +0900946 waitcmd.handle = mapping->vma->handle;
947
948 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
949 if (ret) {
950 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
951 return -errno;
952 }
953 }
954
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700955 return 0;
956}
957
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000958static void virgl_3d_resolve_format_and_use_flags(struct driver *drv, uint32_t format,
959 uint64_t use_flags, uint32_t *out_format,
960 uint64_t *out_use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700961{
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000962 *out_format = format;
963 *out_use_flags = use_flags;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700964 switch (format) {
965 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900966 /* Camera subsystem requires NV12. */
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000967 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) {
968 *out_format = DRM_FORMAT_NV12;
969 } else {
970 /* HACK: See b/28671744 */
971 *out_format = DRM_FORMAT_XBGR8888;
Yiwei Zhang3a171db2021-10-01 22:12:05 +0000972 *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000973 }
974 break;
Lepton Wu249e8632018-04-05 12:50:03 -0700975 case DRM_FORMAT_FLEX_YCbCr_420_888:
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000976 /* All of our host drivers prefer NV12 as their flexible media format.
977 * If that changes, this will need to be modified. */
978 *out_format = DRM_FORMAT_NV12;
979 /* fallthrough */
980 case DRM_FORMAT_NV12:
981 case DRM_FORMAT_ABGR8888:
982 case DRM_FORMAT_ARGB8888:
983 case DRM_FORMAT_RGB565:
984 case DRM_FORMAT_XBGR8888:
985 case DRM_FORMAT_XRGB8888:
986 /* These are the scanout capable formats to the guest. Strip scanout use_flag if the
987 * host does not natively support scanout on the requested format. */
988 if ((use_flags & BO_USE_SCANOUT) &&
989 !virgl_supports_combination_natively(drv, format, BO_USE_SCANOUT))
990 *out_use_flags &= ~BO_USE_SCANOUT;
991 break;
992 case DRM_FORMAT_YVU420_ANDROID:
993 *out_use_flags &= ~BO_USE_SCANOUT;
994 /* HACK: See b/172389166. Also see gbm_bo_create. */
995 *out_use_flags |= BO_USE_LINEAR;
996 break;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700997 default:
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000998 break;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700999 }
1000}
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001001
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001002static void virgl_2d_resolve_format_and_use_flags(uint32_t format, uint64_t use_flags,
1003 uint32_t *out_format, uint64_t *out_use_flags)
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001004{
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001005 *out_format = format;
1006 *out_use_flags = use_flags;
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001007
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001008 /* HACK: See crrev/c/1849773 */
1009 if (format != DRM_FORMAT_XRGB8888)
1010 *out_use_flags &= ~BO_USE_SCANOUT;
1011
1012 switch (format) {
1013 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
1014 /* Camera subsystem requires NV12. */
1015 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) {
1016 *out_format = DRM_FORMAT_NV12;
1017 } else {
1018 /* HACK: See b/28671744 */
1019 *out_format = DRM_FORMAT_XBGR8888;
Yiwei Zhang3a171db2021-10-01 22:12:05 +00001020 *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
Yiwei Zhang9420ffe2021-09-24 06:24:30 +00001021 }
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001022 break;
1023 case DRM_FORMAT_FLEX_YCbCr_420_888:
1024 *out_format = DRM_FORMAT_YVU420_ANDROID;
1025 /* fallthrough */
1026 case DRM_FORMAT_YVU420_ANDROID:
1027 *out_use_flags &= ~BO_USE_SCANOUT;
1028 /* HACK: See b/172389166. Also see gbm_bo_create. */
1029 *out_use_flags |= BO_USE_LINEAR;
1030 break;
1031 default:
1032 break;
Yiwei Zhang9420ffe2021-09-24 06:24:30 +00001033 }
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001034}
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001035
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001036static void virgl_resolve_format_and_use_flags(struct driver *drv, uint32_t format,
1037 uint64_t use_flags, uint32_t *out_format,
1038 uint64_t *out_use_flags)
1039{
1040 if (params[param_3d].value) {
1041 return virgl_3d_resolve_format_and_use_flags(drv, format, use_flags, out_format,
1042 out_use_flags);
1043 } else {
1044 return virgl_2d_resolve_format_and_use_flags(format, use_flags, out_format,
1045 out_use_flags);
1046 }
Yiwei Zhangc1413ea2021-09-17 08:20:21 +00001047}
1048
Gurchetan Singh73c141e2021-01-21 14:51:19 -08001049static int virgl_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +00001050 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001051{
1052 int ret;
Chia-I Wu2e41f632021-01-11 11:08:21 -08001053 struct drm_virtgpu_resource_info_cros res_info = { 0 };
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001054
Gurchetan Singh73c141e2021-01-21 14:51:19 -08001055 if (!params[param_3d].value)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001056 return 0;
1057
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001058 res_info.bo_handle = bo->handles[0].u32;
Chia-I Wu50855622021-01-12 12:38:09 -08001059 res_info.type = VIRTGPU_RESOURCE_INFO_TYPE_EXTENDED;
Chia-I Wu2e41f632021-01-11 11:08:21 -08001060 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO_CROS, &res_info);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001061 if (ret) {
1062 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_INFO failed with %s\n", strerror(errno));
1063 return ret;
1064 }
1065
Yiwei Zhangf58616e2021-08-26 05:54:15 +00001066 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++) {
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001067 /*
1068 * Currently, kernel v4.14 (Betty) doesn't have the extended resource info
1069 * ioctl.
1070 */
Yiwei Zhangf58616e2021-08-26 05:54:15 +00001071 if (!res_info.strides[plane])
1072 break;
1073
1074 strides[plane] = res_info.strides[plane];
1075 offsets[plane] = res_info.offsets[plane];
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001076 }
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +00001077 *format_modifier = res_info.format_modifier;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001078
1079 return 0;
1080}
1081
Jason Macnakd6666c82021-09-29 11:13:25 -07001082static uint32_t virgl_get_max_texture_2d_size(struct driver *drv)
1083{
1084 if (params[param_3d].value)
1085 return virgl_3d_get_max_texture_2d_size(drv);
1086 else
Jason Macnakc06cc9c2021-10-06 10:16:19 -07001087 return VIRGL_2D_MAX_TEXTURE_2D_SIZE;
Jason Macnakd6666c82021-09-29 11:13:25 -07001088}
1089
Gurchetan Singhbbde01e2021-02-17 08:54:28 -08001090const struct backend virtgpu_virgl = { .name = "virtgpu_virgl",
1091 .init = virgl_init,
1092 .close = virgl_close,
1093 .bo_create = virgl_bo_create,
1094 .bo_destroy = virgl_bo_destroy,
1095 .bo_import = drv_prime_bo_import,
1096 .bo_map = virgl_bo_map,
1097 .bo_unmap = drv_bo_munmap,
1098 .bo_invalidate = virgl_bo_invalidate,
1099 .bo_flush = virgl_bo_flush,
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +00001100 .resolve_format_and_use_flags =
1101 virgl_resolve_format_and_use_flags,
Jason Macnakd6666c82021-09-29 11:13:25 -07001102 .resource_info = virgl_resource_info,
1103 .get_max_texture_2d_size = virgl_get_max_texture_2d_size };