blob: 46f575d7843e988eebc8ca162d9fa507a02c857d [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>
11#include <stdio.h>
12#include <string.h>
13#include <sys/mman.h>
Zach Reizner85c4c5f2017-10-04 13:15:57 -070014#include <xf86drm.h>
15
16#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 "helpers.h"
21#include "util.h"
Zach Reizner85c4c5f2017-10-04 13:15:57 -070022
Tao Wu33815882018-03-12 18:07:43 -070023#ifndef PAGE_SIZE
Zach Reizner85c4c5f2017-10-04 13:15:57 -070024#define PAGE_SIZE 0x1000
Tao Wu33815882018-03-12 18:07:43 -070025#endif
Zach Reizner85c4c5f2017-10-04 13:15:57 -070026#define PIPE_TEXTURE_2D 2
27
Lepton Wu249e8632018-04-05 12:50:03 -070028#define MESA_LLVMPIPE_TILE_ORDER 6
29#define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
30
Gurchetan Singhd708f612019-09-12 17:26:45 -070031struct feature {
32 uint64_t feature;
33 const char *name;
34 uint32_t enabled;
35};
36
37enum feature_id {
38 feat_3d,
39 feat_capset_fix,
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -070040 feat_resource_blob,
41 feat_host_visible,
42 feat_host_cross_device,
Gurchetan Singhd708f612019-09-12 17:26:45 -070043 feat_max,
44};
45
46#define FEATURE(x) \
47 (struct feature) \
48 { \
49 x, #x, 0 \
50 }
51
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -070052static struct feature features[] = {
53 FEATURE(VIRTGPU_PARAM_3D_FEATURES), FEATURE(VIRTGPU_PARAM_CAPSET_QUERY_FIX),
54 FEATURE(VIRTGPU_PARAM_RESOURCE_BLOB), FEATURE(VIRTGPU_PARAM_HOST_VISIBLE),
55 FEATURE(VIRTGPU_PARAM_CROSS_DEVICE),
56};
Gurchetan Singhd708f612019-09-12 17:26:45 -070057
Zach Reizner85c4c5f2017-10-04 13:15:57 -070058static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070059 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
60 DRM_FORMAT_XRGB8888 };
Zach Reizner85c4c5f2017-10-04 13:15:57 -070061
Jason Macnak1de7f662020-01-24 15:05:57 -080062static const uint32_t dumb_texture_source_formats[] = {
63 DRM_FORMAT_R8, DRM_FORMAT_R16, DRM_FORMAT_YVU420,
64 DRM_FORMAT_NV12, DRM_FORMAT_NV21, DRM_FORMAT_YVU420_ANDROID
65};
Lepton Wu249e8632018-04-05 12:50:03 -070066
Jason Macnak1de7f662020-01-24 15:05:57 -080067static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_NV21,
68 DRM_FORMAT_R8, DRM_FORMAT_R16,
69 DRM_FORMAT_RG88, DRM_FORMAT_YVU420_ANDROID };
Zach Reizner85c4c5f2017-10-04 13:15:57 -070070
Lepton Wu249e8632018-04-05 12:50:03 -070071struct virtio_gpu_priv {
Lepton Wueebce652020-02-26 15:13:34 -080072 int caps_is_v2;
Jason Macnakddf4ec02020-02-03 16:36:46 -080073 union virgl_caps caps;
Jason Macnak1de7f662020-01-24 15:05:57 -080074 int host_gbm_enabled;
David Stevens0fe561f2020-10-28 16:06:38 +090075 atomic_int next_blob_id;
Lepton Wu249e8632018-04-05 12:50:03 -070076};
77
Kansho Nishidad97877b2019-06-14 18:28:18 +090078static uint32_t translate_format(uint32_t drm_fourcc)
Zach Reizner85c4c5f2017-10-04 13:15:57 -070079{
80 switch (drm_fourcc) {
Jason Macnak1de7f662020-01-24 15:05:57 -080081 case DRM_FORMAT_BGR888:
82 case DRM_FORMAT_RGB888:
83 return VIRGL_FORMAT_R8G8B8_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070084 case DRM_FORMAT_XRGB8888:
85 return VIRGL_FORMAT_B8G8R8X8_UNORM;
86 case DRM_FORMAT_ARGB8888:
87 return VIRGL_FORMAT_B8G8R8A8_UNORM;
88 case DRM_FORMAT_XBGR8888:
89 return VIRGL_FORMAT_R8G8B8X8_UNORM;
90 case DRM_FORMAT_ABGR8888:
91 return VIRGL_FORMAT_R8G8B8A8_UNORM;
Jason Macnak1de7f662020-01-24 15:05:57 -080092 case DRM_FORMAT_ABGR16161616F:
Lepton Wufef113c2020-10-30 16:29:26 -070093 return VIRGL_FORMAT_R16G16B16A16_FLOAT;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070094 case DRM_FORMAT_RGB565:
95 return VIRGL_FORMAT_B5G6R5_UNORM;
96 case DRM_FORMAT_R8:
97 return VIRGL_FORMAT_R8_UNORM;
98 case DRM_FORMAT_RG88:
99 return VIRGL_FORMAT_R8G8_UNORM;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -0700100 case DRM_FORMAT_NV12:
101 return VIRGL_FORMAT_NV12;
Jason Macnak1de7f662020-01-24 15:05:57 -0800102 case DRM_FORMAT_NV21:
103 return VIRGL_FORMAT_NV21;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -0700104 case DRM_FORMAT_YVU420:
105 case DRM_FORMAT_YVU420_ANDROID:
106 return VIRGL_FORMAT_YV12;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700107 default:
108 return 0;
109 }
110}
111
Jason Macnak1de7f662020-01-24 15:05:57 -0800112static bool virtio_gpu_bitmask_supports_format(struct virgl_supported_format_mask *supported,
113 uint32_t drm_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800114{
115 uint32_t virgl_format = translate_format(drm_format);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800116 if (!virgl_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800117 return false;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800118
119 uint32_t bitmask_index = virgl_format / 32;
120 uint32_t bit_index = virgl_format % 32;
121 return supported->bitmask[bitmask_index] & (1 << bit_index);
122}
123
Jason Macnak1de7f662020-01-24 15:05:57 -0800124// The metadata generated here for emulated buffers is slightly different than the metadata
125// generated by drv_bo_from_format. In order to simplify transfers in the flush and invalidate
126// functions below, the emulated buffers are oversized. For example, ignoring stride alignment
127// requirements to demonstrate, a 6x6 YUV420 image buffer might have the following layout from
128// drv_bo_from_format:
129//
130// | Y | Y | Y | Y | Y | Y |
131// | Y | Y | Y | Y | Y | Y |
132// | Y | Y | Y | Y | Y | Y |
133// | Y | Y | Y | Y | Y | Y |
134// | Y | Y | Y | Y | Y | Y |
135// | Y | Y | Y | Y | Y | Y |
136// | U | U | U | U | U | U |
137// | U | U | U | V | V | V |
138// | V | V | V | V | V | V |
139//
140// where each plane immediately follows the previous plane in memory. This layout makes it
141// difficult to compute the transfers needed for example when the middle 2x2 region of the
142// image is locked and needs to be flushed/invalidated.
143//
144// Emulated multi-plane buffers instead have a layout of:
145//
146// | Y | Y | Y | Y | Y | Y |
147// | Y | Y | Y | Y | Y | Y |
148// | Y | Y | Y | Y | Y | Y |
149// | Y | Y | Y | Y | Y | Y |
150// | Y | Y | Y | Y | Y | Y |
151// | Y | Y | Y | Y | Y | Y |
152// | U | U | U | | | |
153// | U | U | U | | | |
154// | U | U | U | | | |
155// | V | V | V | | | |
156// | V | V | V | | | |
157// | V | V | V | | | |
158//
159// where each plane is placed as a sub-image (albeit with a very large stride) in order to
160// simplify transfers into 3 sub-image transfers for the above example.
161//
162// Additional note: the V-plane is not placed to the right of the U-plane due to some
163// observed failures in media framework code which assumes the V-plane is not
164// "row-interlaced" with the U-plane.
165static void virtio_gpu_get_emulated_metadata(const struct bo *bo, struct bo_metadata *metadata)
166{
167 uint32_t y_plane_height;
168 uint32_t c_plane_height;
169 uint32_t original_width = bo->meta.width;
170 uint32_t original_height = bo->meta.height;
171
172 metadata->format = DRM_FORMAT_R8;
173 switch (bo->meta.format) {
174 case DRM_FORMAT_NV12:
175 case DRM_FORMAT_NV21:
176 // Bi-planar
177 metadata->num_planes = 2;
178
179 y_plane_height = original_height;
180 c_plane_height = DIV_ROUND_UP(original_height, 2);
181
182 metadata->width = original_width;
183 metadata->height = y_plane_height + c_plane_height;
184
185 // Y-plane (full resolution)
186 metadata->strides[0] = metadata->width;
187 metadata->offsets[0] = 0;
188 metadata->sizes[0] = metadata->width * y_plane_height;
189
190 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
191 metadata->strides[1] = metadata->width;
192 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
193 metadata->sizes[1] = metadata->width * c_plane_height;
194
195 metadata->total_size = metadata->width * metadata->height;
196 break;
197 case DRM_FORMAT_YVU420:
198 case DRM_FORMAT_YVU420_ANDROID:
199 // Tri-planar
200 metadata->num_planes = 3;
201
202 y_plane_height = original_height;
203 c_plane_height = DIV_ROUND_UP(original_height, 2);
204
205 metadata->width = ALIGN(original_width, 32);
206 metadata->height = y_plane_height + (2 * c_plane_height);
207
208 // Y-plane (full resolution)
209 metadata->strides[0] = metadata->width;
210 metadata->offsets[0] = 0;
211 metadata->sizes[0] = metadata->width * original_height;
212
213 // Cb-plane (half resolution, placed below Y-plane)
214 metadata->strides[1] = metadata->width;
215 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
216 metadata->sizes[1] = metadata->width * c_plane_height;
217
218 // Cr-plane (half resolution, placed below Cb-plane)
219 metadata->strides[2] = metadata->width;
220 metadata->offsets[2] = metadata->offsets[1] + metadata->sizes[1];
221 metadata->sizes[2] = metadata->width * c_plane_height;
222
223 metadata->total_size = metadata->width * metadata->height;
224 break;
225 default:
226 break;
227 }
228}
229
230struct virtio_transfers_params {
231 size_t xfers_needed;
232 struct rectangle xfer_boxes[DRV_MAX_PLANES];
233};
234
235static void virtio_gpu_get_emulated_transfers_params(const struct bo *bo,
236 const struct rectangle *transfer_box,
237 struct virtio_transfers_params *xfer_params)
238{
239 uint32_t y_plane_height;
240 uint32_t c_plane_height;
241 struct bo_metadata emulated_metadata;
242
243 if (transfer_box->x == 0 && transfer_box->y == 0 && transfer_box->width == bo->meta.width &&
244 transfer_box->height == bo->meta.height) {
245 virtio_gpu_get_emulated_metadata(bo, &emulated_metadata);
246
247 xfer_params->xfers_needed = 1;
248 xfer_params->xfer_boxes[0].x = 0;
249 xfer_params->xfer_boxes[0].y = 0;
250 xfer_params->xfer_boxes[0].width = emulated_metadata.width;
251 xfer_params->xfer_boxes[0].height = emulated_metadata.height;
252
253 return;
254 }
255
256 switch (bo->meta.format) {
257 case DRM_FORMAT_NV12:
258 case DRM_FORMAT_NV21:
259 // Bi-planar
260 xfer_params->xfers_needed = 2;
261
262 y_plane_height = bo->meta.height;
263 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
264
265 // Y-plane (full resolution)
266 xfer_params->xfer_boxes[0].x = transfer_box->x;
267 xfer_params->xfer_boxes[0].y = transfer_box->y;
268 xfer_params->xfer_boxes[0].width = transfer_box->width;
269 xfer_params->xfer_boxes[0].height = transfer_box->height;
270
271 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
272 xfer_params->xfer_boxes[1].x = transfer_box->x;
273 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
274 xfer_params->xfer_boxes[1].width = transfer_box->width;
275 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
276
277 break;
278 case DRM_FORMAT_YVU420:
279 case DRM_FORMAT_YVU420_ANDROID:
280 // Tri-planar
281 xfer_params->xfers_needed = 3;
282
283 y_plane_height = bo->meta.height;
284 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
285
286 // Y-plane (full resolution)
287 xfer_params->xfer_boxes[0].x = transfer_box->x;
288 xfer_params->xfer_boxes[0].y = transfer_box->y;
289 xfer_params->xfer_boxes[0].width = transfer_box->width;
290 xfer_params->xfer_boxes[0].height = transfer_box->height;
291
292 // Cb-plane (half resolution, placed below Y-plane)
293 xfer_params->xfer_boxes[1].x = transfer_box->x;
294 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
295 xfer_params->xfer_boxes[1].width = DIV_ROUND_UP(transfer_box->width, 2);
296 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
297
298 // Cr-plane (half resolution, placed below Cb-plane)
299 xfer_params->xfer_boxes[2].x = transfer_box->x;
300 xfer_params->xfer_boxes[2].y = transfer_box->y + y_plane_height + c_plane_height;
301 xfer_params->xfer_boxes[2].width = DIV_ROUND_UP(transfer_box->width, 2);
302 xfer_params->xfer_boxes[2].height = DIV_ROUND_UP(transfer_box->height, 2);
303
304 break;
305 }
306}
307
308static bool virtio_gpu_supports_combination_natively(struct driver *drv, uint32_t drm_format,
309 uint64_t use_flags)
310{
311 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
312
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800313 if (priv->caps.max_version == 0)
Jason Macnak1de7f662020-01-24 15:05:57 -0800314 return true;
Jason Macnak1de7f662020-01-24 15:05:57 -0800315
316 if ((use_flags & BO_USE_RENDERING) &&
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800317 !virtio_gpu_bitmask_supports_format(&priv->caps.v1.render, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800318 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800319
320 if ((use_flags & BO_USE_TEXTURE) &&
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800321 !virtio_gpu_bitmask_supports_format(&priv->caps.v1.sampler, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800322 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800323
324 if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 &&
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800325 !virtio_gpu_bitmask_supports_format(&priv->caps.v2.scanout, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800326 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800327
328 return true;
329}
330
331// For virtio backends that do not support formats natively (e.g. multi-planar formats are not
332// supported in virglrenderer when gbm is unavailable on the host machine), whether or not the
333// format and usage combination can be handled as a blob (byte buffer).
334static bool virtio_gpu_supports_combination_through_emulation(struct driver *drv,
335 uint32_t drm_format,
336 uint64_t use_flags)
337{
338 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
339
340 // Only enable emulation on non-gbm virtio backends.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800341 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800342 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800343
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800344 if (use_flags & (BO_USE_RENDERING | BO_USE_SCANOUT))
Jason Macnak1de7f662020-01-24 15:05:57 -0800345 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800346
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800347 if (!virtio_gpu_supports_combination_natively(drv, DRM_FORMAT_R8, use_flags))
Jason Macnak1de7f662020-01-24 15:05:57 -0800348 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800349
350 return drm_format == DRM_FORMAT_NV12 || drm_format == DRM_FORMAT_NV21 ||
351 drm_format == DRM_FORMAT_YVU420 || drm_format == DRM_FORMAT_YVU420_ANDROID;
352}
353
Jason Macnakddf4ec02020-02-03 16:36:46 -0800354// Adds the given buffer combination to the list of supported buffer combinations if the
355// combination is supported by the virtio backend.
356static void virtio_gpu_add_combination(struct driver *drv, uint32_t drm_format,
357 struct format_metadata *metadata, uint64_t use_flags)
358{
359 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
360
Gurchetan Singhd708f612019-09-12 17:26:45 -0700361 if (features[feat_3d].enabled && priv->caps.max_version >= 1) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800362 if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 &&
363 !virtio_gpu_supports_combination_natively(drv, drm_format, use_flags)) {
364 drv_log("Scanout format: %d\n", drm_format);
365 use_flags &= ~BO_USE_SCANOUT;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800366 }
367
Jason Macnak1de7f662020-01-24 15:05:57 -0800368 if (!virtio_gpu_supports_combination_natively(drv, drm_format, use_flags) &&
369 !virtio_gpu_supports_combination_through_emulation(drv, drm_format,
370 use_flags)) {
371 drv_log("Skipping unsupported combination format:%d\n", drm_format);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800372 return;
373 }
374 }
375
376 drv_add_combination(drv, drm_format, metadata, use_flags);
377}
378
379// Adds each given buffer combination to the list of supported buffer combinations if the
380// combination supported by the virtio backend.
381static void virtio_gpu_add_combinations(struct driver *drv, const uint32_t *drm_formats,
382 uint32_t num_formats, struct format_metadata *metadata,
383 uint64_t use_flags)
384{
385 uint32_t i;
386
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800387 for (i = 0; i < num_formats; i++)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800388 virtio_gpu_add_combination(drv, drm_formats[i], metadata, use_flags);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800389}
390
Lepton Wu249e8632018-04-05 12:50:03 -0700391static int virtio_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
392 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700393{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700394 if (bo->meta.format != DRM_FORMAT_R8) {
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900395 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
396 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
397 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700398
Dominik Behr6e6dc492019-10-09 15:43:52 -0700399 return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_DUMB32BPP);
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700400}
401
Lepton Wudbab0832019-04-19 12:26:39 -0700402static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
403 uint32_t virgl_bind)
404{
405 if ((*flag) & check_flag) {
406 (*flag) &= ~check_flag;
407 (*bind) |= virgl_bind;
408 }
409}
410
David Stevenscf280482020-12-21 11:43:44 +0900411static uint32_t compute_virgl_bind_flags(uint64_t use_flags, uint32_t format)
Lepton Wudbab0832019-04-19 12:26:39 -0700412{
Kansho Nishidad97877b2019-06-14 18:28:18 +0900413 /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
414 uint32_t bind = VIRGL_BIND_SHARED;
Lepton Wudbab0832019-04-19 12:26:39 -0700415
416 handle_flag(&use_flags, BO_USE_TEXTURE, &bind, VIRGL_BIND_SAMPLER_VIEW);
417 handle_flag(&use_flags, BO_USE_RENDERING, &bind, VIRGL_BIND_RENDER_TARGET);
418 handle_flag(&use_flags, BO_USE_SCANOUT, &bind, VIRGL_BIND_SCANOUT);
David Stevens55a6cf92019-09-03 10:45:33 +0900419 handle_flag(&use_flags, BO_USE_CURSOR, &bind, VIRGL_BIND_CURSOR);
420 handle_flag(&use_flags, BO_USE_LINEAR, &bind, VIRGL_BIND_LINEAR);
421
David Stevens23de4e22020-05-15 14:15:35 +0900422 if (use_flags & BO_USE_PROTECTED) {
423 handle_flag(&use_flags, BO_USE_PROTECTED, &bind, VIRGL_BIND_MINIGBM_PROTECTED);
424 } else {
425 // Make sure we don't set both flags, since that could be mistaken for
426 // protected. Give OFTEN priority over RARELY.
427 if (use_flags & BO_USE_SW_READ_OFTEN) {
428 handle_flag(&use_flags, BO_USE_SW_READ_OFTEN, &bind,
429 VIRGL_BIND_MINIGBM_SW_READ_OFTEN);
430 } else {
431 handle_flag(&use_flags, BO_USE_SW_READ_RARELY, &bind,
432 VIRGL_BIND_MINIGBM_SW_READ_RARELY);
433 }
434 if (use_flags & BO_USE_SW_WRITE_OFTEN) {
435 handle_flag(&use_flags, BO_USE_SW_WRITE_OFTEN, &bind,
436 VIRGL_BIND_MINIGBM_SW_WRITE_OFTEN);
437 } else {
438 handle_flag(&use_flags, BO_USE_SW_WRITE_RARELY, &bind,
439 VIRGL_BIND_MINIGBM_SW_WRITE_RARELY);
440 }
441 }
David Stevens55a6cf92019-09-03 10:45:33 +0900442
David Stevens23de4e22020-05-15 14:15:35 +0900443 handle_flag(&use_flags, BO_USE_CAMERA_WRITE, &bind, VIRGL_BIND_MINIGBM_CAMERA_WRITE);
444 handle_flag(&use_flags, BO_USE_CAMERA_READ, &bind, VIRGL_BIND_MINIGBM_CAMERA_READ);
445 handle_flag(&use_flags, BO_USE_HW_VIDEO_DECODER, &bind,
446 VIRGL_BIND_MINIGBM_HW_VIDEO_DECODER);
447 handle_flag(&use_flags, BO_USE_HW_VIDEO_ENCODER, &bind,
448 VIRGL_BIND_MINIGBM_HW_VIDEO_ENCODER);
David Stevens55a6cf92019-09-03 10:45:33 +0900449
David Stevenscf280482020-12-21 11:43:44 +0900450 /*
451 * HACK: This is for HAL_PIXEL_FORMAT_YV12 buffers allocated by arcvm. None of
452 * our platforms can display YV12, so we can treat as a SW buffer. Remove once
453 * this can be intelligently resolved in the guest. Also see gbm_bo_create.
454 */
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800455 if (format == DRM_FORMAT_YVU420_ANDROID)
David Stevenscf280482020-12-21 11:43:44 +0900456 bind |= VIRGL_BIND_LINEAR;
David Stevenscf280482020-12-21 11:43:44 +0900457
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800458 if (use_flags)
Lepton Wudbab0832019-04-19 12:26:39 -0700459 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900460
Lepton Wudbab0832019-04-19 12:26:39 -0700461 return bind;
462}
463
Lepton Wu249e8632018-04-05 12:50:03 -0700464static int virtio_virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
465 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700466{
467 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800468 size_t i;
Kansho Nishidad97877b2019-06-14 18:28:18 +0900469 uint32_t stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700470 struct drm_virtgpu_resource_create res_create = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800471 struct bo_metadata emulated_metadata;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700472
Jason Macnak1de7f662020-01-24 15:05:57 -0800473 if (virtio_gpu_supports_combination_natively(bo->drv, format, use_flags)) {
474 stride = drv_stride_from_format(format, width, 0);
475 drv_bo_from_format(bo, stride, height, format);
476 } else {
477 assert(
478 virtio_gpu_supports_combination_through_emulation(bo->drv, format, use_flags));
479
480 virtio_gpu_get_emulated_metadata(bo, &emulated_metadata);
481
482 format = emulated_metadata.format;
483 width = emulated_metadata.width;
484 height = emulated_metadata.height;
485 for (i = 0; i < emulated_metadata.num_planes; i++) {
486 bo->meta.strides[i] = emulated_metadata.strides[i];
487 bo->meta.offsets[i] = emulated_metadata.offsets[i];
488 bo->meta.sizes[i] = emulated_metadata.sizes[i];
489 }
490 bo->meta.total_size = emulated_metadata.total_size;
491 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700492
Kansho Nishidad97877b2019-06-14 18:28:18 +0900493 /*
494 * Setting the target is intended to ensure this resource gets bound as a 2D
495 * texture in the host renderer's GL state. All of these resource properties are
496 * sent unchanged by the kernel to the host, which in turn sends them unchanged to
497 * virglrenderer. When virglrenderer makes a resource, it will convert the target
498 * enum to the equivalent one in GL and then bind the resource to that target.
499 */
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700500
Kansho Nishidad97877b2019-06-14 18:28:18 +0900501 res_create.target = PIPE_TEXTURE_2D;
502 res_create.format = translate_format(format);
David Stevenscf280482020-12-21 11:43:44 +0900503 res_create.bind = compute_virgl_bind_flags(use_flags, format);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900504 res_create.width = width;
505 res_create.height = height;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700506
Kansho Nishidad97877b2019-06-14 18:28:18 +0900507 /* For virgl 3D */
508 res_create.depth = 1;
509 res_create.array_size = 1;
510 res_create.last_level = 0;
511 res_create.nr_samples = 0;
512
Gurchetan Singh298b7572019-09-19 09:55:18 -0700513 res_create.size = ALIGN(bo->meta.total_size, PAGE_SIZE); // PAGE_SIZE = 0x1000
Kansho Nishidad97877b2019-06-14 18:28:18 +0900514 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create);
515 if (ret) {
516 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
517 return ret;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700518 }
519
Gurchetan Singh298b7572019-09-19 09:55:18 -0700520 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
Kansho Nishidad97877b2019-06-14 18:28:18 +0900521 bo->handles[plane].u32 = res_create.bo_handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700522
523 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700524}
525
Lepton Wu249e8632018-04-05 12:50:03 -0700526static void *virtio_virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700527{
528 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700529 struct drm_virtgpu_map gem_map = { 0 };
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700530
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700531 gem_map.handle = bo->handles[0].u32;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700532 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
533 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700534 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700535 return MAP_FAILED;
536 }
537
Gurchetan Singh298b7572019-09-19 09:55:18 -0700538 vma->length = bo->meta.total_size;
539 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700540 gem_map.offset);
541}
542
Lepton Wueebce652020-02-26 15:13:34 -0800543static int virtio_gpu_get_caps(struct driver *drv, union virgl_caps *caps, int *caps_is_v2)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800544{
545 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700546 struct drm_virtgpu_get_caps cap_args = { 0 };
Jason Macnakddf4ec02020-02-03 16:36:46 -0800547
Lepton Wueebce652020-02-26 15:13:34 -0800548 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800549 cap_args.addr = (unsigned long long)caps;
Gurchetan Singhd708f612019-09-12 17:26:45 -0700550 if (features[feat_capset_fix].enabled) {
Lepton Wueebce652020-02-26 15:13:34 -0800551 *caps_is_v2 = 1;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800552 cap_args.cap_set_id = 2;
553 cap_args.size = sizeof(union virgl_caps);
554 } else {
555 cap_args.cap_set_id = 1;
556 cap_args.size = sizeof(struct virgl_caps_v1);
557 }
558
559 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
560 if (ret) {
561 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Lepton Wueebce652020-02-26 15:13:34 -0800562 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800563
564 // Fallback to v1
565 cap_args.cap_set_id = 1;
566 cap_args.size = sizeof(struct virgl_caps_v1);
567
568 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800569 if (ret)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800570 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Jason Macnakddf4ec02020-02-03 16:36:46 -0800571 }
572
573 return ret;
574}
575
Jason Macnak1de7f662020-01-24 15:05:57 -0800576static void virtio_gpu_init_features_and_caps(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700577{
Jason Macnak1de7f662020-01-24 15:05:57 -0800578 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
Lepton Wu249e8632018-04-05 12:50:03 -0700579
Gurchetan Singhd708f612019-09-12 17:26:45 -0700580 for (uint32_t i = 0; i < ARRAY_SIZE(features); i++) {
581 struct drm_virtgpu_getparam params = { 0 };
Lepton Wu249e8632018-04-05 12:50:03 -0700582
Gurchetan Singhd708f612019-09-12 17:26:45 -0700583 params.param = features[i].feature;
584 params.value = (uint64_t)(uintptr_t)&features[i].enabled;
Jason Macnak1de7f662020-01-24 15:05:57 -0800585 int ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GETPARAM, &params);
Gurchetan Singhd708f612019-09-12 17:26:45 -0700586 if (ret)
587 drv_log("DRM_IOCTL_VIRTGPU_GET_PARAM failed with %s\n", strerror(errno));
Lepton Wu249e8632018-04-05 12:50:03 -0700588 }
589
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800590 if (features[feat_3d].enabled)
Lepton Wueebce652020-02-26 15:13:34 -0800591 virtio_gpu_get_caps(drv, &priv->caps, &priv->caps_is_v2);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800592
Jason Macnak1de7f662020-01-24 15:05:57 -0800593 // Multi-planar formats are currently only supported in virglrenderer through gbm.
594 priv->host_gbm_enabled =
595 virtio_gpu_supports_combination_natively(drv, DRM_FORMAT_NV12, BO_USE_TEXTURE);
596}
597
598static int virtio_gpu_init(struct driver *drv)
599{
600 struct virtio_gpu_priv *priv;
601
602 priv = calloc(1, sizeof(*priv));
603 drv->priv = priv;
604
605 virtio_gpu_init_features_and_caps(drv);
606
607 if (features[feat_3d].enabled) {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700608 /* This doesn't mean host can scanout everything, it just means host
609 * hypervisor can show it. */
Jason Macnakddf4ec02020-02-03 16:36:46 -0800610 virtio_gpu_add_combinations(drv, render_target_formats,
611 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
612 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
613 virtio_gpu_add_combinations(drv, texture_source_formats,
614 ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
615 BO_USE_TEXTURE_MASK);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700616 } else {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700617 /* Virtio primary plane only allows this format. */
Jason Macnakddf4ec02020-02-03 16:36:46 -0800618 virtio_gpu_add_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
619 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700620 /* Virtio cursor plane only allows this format and Chrome cannot live without
621 * ARGB888 renderable format. */
Jason Macnakddf4ec02020-02-03 16:36:46 -0800622 virtio_gpu_add_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
623 BO_USE_RENDER_MASK | BO_USE_CURSOR);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700624 /* Android needs more, but they cannot be bound as scanouts anymore after
625 * "drm/virtio: fix DRM_FORMAT_* handling" */
Jason Macnakddf4ec02020-02-03 16:36:46 -0800626 virtio_gpu_add_combinations(drv, render_target_formats,
627 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
628 BO_USE_RENDER_MASK);
629 virtio_gpu_add_combinations(drv, dumb_texture_source_formats,
630 ARRAY_SIZE(dumb_texture_source_formats),
631 &LINEAR_METADATA, BO_USE_TEXTURE_MASK);
632 virtio_gpu_add_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
633 BO_USE_SW_MASK | BO_USE_LINEAR);
Jason Macnak1de7f662020-01-24 15:05:57 -0800634 virtio_gpu_add_combination(drv, DRM_FORMAT_NV21, &LINEAR_METADATA,
635 BO_USE_SW_MASK | BO_USE_LINEAR);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700636 }
Lepton Wu249e8632018-04-05 12:50:03 -0700637
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700638 /* Android CTS tests require this. */
Jason Macnak1de7f662020-01-24 15:05:57 -0800639 virtio_gpu_add_combination(drv, DRM_FORMAT_RGB888, &LINEAR_METADATA, BO_USE_SW_MASK);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800640 virtio_gpu_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
Jason Macnak1de7f662020-01-24 15:05:57 -0800641 virtio_gpu_add_combination(drv, DRM_FORMAT_ABGR16161616F, &LINEAR_METADATA,
642 BO_USE_SW_MASK | BO_USE_TEXTURE_MASK);
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700643
David Stevens9f7897f2019-08-09 20:20:23 +0900644 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
Hirokazu Honda20e4a932019-12-06 15:21:45 +0900645 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
646 BO_USE_HW_VIDEO_ENCODER);
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900647 drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
David Staessens04b7e242020-05-28 15:47:15 +0900648 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
649 BO_USE_HW_VIDEO_ENCODER);
David Stevens519978f2020-12-11 14:09:56 +0900650
651 if (!priv->host_gbm_enabled) {
652 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &LINEAR_METADATA,
653 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
654 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
655 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &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_NV21, &LINEAR_METADATA,
659 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
660 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
661 drv_modify_combination(drv, DRM_FORMAT_R16, &LINEAR_METADATA,
662 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
663 BO_USE_HW_VIDEO_DECODER);
664 drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA,
665 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
666 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
667 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &LINEAR_METADATA,
668 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
669 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
670 }
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900671
Lepton Wu249e8632018-04-05 12:50:03 -0700672 return drv_modify_linear_combinations(drv);
673}
674
675static void virtio_gpu_close(struct driver *drv)
676{
677 free(drv->priv);
678 drv->priv = NULL;
679}
680
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700681static int virtio_gpu_bo_create_blob(struct driver *drv, struct bo *bo)
682{
683 int ret;
684 uint32_t stride;
David Stevens0fe561f2020-10-28 16:06:38 +0900685 uint32_t cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700686 uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 };
687 struct drm_virtgpu_resource_create_blob drm_rc_blob = { 0 };
David Stevens0fe561f2020-10-28 16:06:38 +0900688 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700689
David Stevensd3f07bd2020-09-25 18:52:26 +0900690 uint32_t blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE;
691 if (bo->meta.use_flags & BO_USE_SW_MASK)
692 blob_flags |= VIRTGPU_BLOB_FLAG_USE_MAPPABLE;
693 if (bo->meta.use_flags & BO_USE_NON_GPU_HW)
David Stevensb42624c2020-09-10 10:50:26 +0900694 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{
733 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
734
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
David Stevensd3f07bd2020-09-25 18:52:26 +0900744 // Use regular resources if only the GPU needs efficient access
745 if (!(use_flags &
746 (BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN | BO_USE_LINEAR | BO_USE_NON_GPU_HW)))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700747 return false;
748
David Stevensd3f07bd2020-09-25 18:52:26 +0900749 switch (format) {
750 case DRM_FORMAT_YVU420_ANDROID:
751 case DRM_FORMAT_R8:
752 // Formats with strictly defined strides are supported
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700753 return true;
David Stevensd3f07bd2020-09-25 18:52:26 +0900754 case DRM_FORMAT_NV12:
755 // Knowing buffer metadata at buffer creation isn't yet supported, so buffers
756 // can't be properly mapped into the guest.
757 return (use_flags & BO_USE_SW_MASK) == 0;
758 default:
759 return false;
760 }
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700761}
762
Lepton Wu249e8632018-04-05 12:50:03 -0700763static int virtio_gpu_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
764 uint64_t use_flags)
765{
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700766 if (features[feat_resource_blob].enabled && features[feat_host_visible].enabled &&
767 should_use_blob(bo->drv, format, use_flags))
768 return virtio_gpu_bo_create_blob(bo->drv, bo);
769
Gurchetan Singhd708f612019-09-12 17:26:45 -0700770 if (features[feat_3d].enabled)
Lepton Wu249e8632018-04-05 12:50:03 -0700771 return virtio_virgl_bo_create(bo, width, height, format, use_flags);
772 else
773 return virtio_dumb_bo_create(bo, width, height, format, use_flags);
774}
775
776static int virtio_gpu_bo_destroy(struct bo *bo)
777{
Gurchetan Singhd708f612019-09-12 17:26:45 -0700778 if (features[feat_3d].enabled)
Lepton Wu249e8632018-04-05 12:50:03 -0700779 return drv_gem_bo_destroy(bo);
780 else
781 return drv_dumb_bo_destroy(bo);
782}
783
784static void *virtio_gpu_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
785{
Gurchetan Singhd708f612019-09-12 17:26:45 -0700786 if (features[feat_3d].enabled)
Lepton Wu249e8632018-04-05 12:50:03 -0700787 return virtio_virgl_bo_map(bo, vma, plane, map_flags);
788 else
789 return drv_dumb_bo_map(bo, vma, plane, map_flags);
790}
791
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700792static int virtio_gpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
793{
794 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800795 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700796 struct drm_virtgpu_3d_transfer_from_host xfer = { 0 };
797 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800798 struct virtio_transfers_params xfer_params;
799 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
David Stevens9fe8c202020-12-21 18:47:55 +0900800 uint64_t host_write_flags;
Lepton Wu249e8632018-04-05 12:50:03 -0700801
Gurchetan Singhd708f612019-09-12 17:26:45 -0700802 if (!features[feat_3d].enabled)
Lepton Wu249e8632018-04-05 12:50:03 -0700803 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700804
David Stevens9fe8c202020-12-21 18:47:55 +0900805 // Invalidate is only necessary if the host writes to the buffer. The encoder and
806 // decoder flags don't differentiate between input and output buffers, but we can
807 // use the format to determine whether this buffer could be encoder/decoder output.
808 host_write_flags = BO_USE_RENDERING | BO_USE_CAMERA_WRITE;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800809 if (bo->meta.format == DRM_FORMAT_R8)
David Stevens9fe8c202020-12-21 18:47:55 +0900810 host_write_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800811 else
David Stevens9fe8c202020-12-21 18:47:55 +0900812 host_write_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800813
David Stevens9fe8c202020-12-21 18:47:55 +0900814 if ((bo->meta.use_flags & host_write_flags) == 0)
David Stevens4d5358d2019-10-24 14:59:31 +0900815 return 0;
816
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700817 if (features[feat_resource_blob].enabled &&
818 (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
819 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
837 // and guest stride to the host (compare virtio_gpu.h and virtgpu_drm.h).
838 // 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
Jason Macnak1de7f662020-01-24 15:05:57 -0800847 if (virtio_gpu_supports_combination_natively(bo->drv, bo->meta.format,
848 bo->meta.use_flags)) {
849 xfer_params.xfers_needed = 1;
850 xfer_params.xfer_boxes[0] = mapping->rect;
851 } else {
852 assert(virtio_gpu_supports_combination_through_emulation(bo->drv, bo->meta.format,
853 bo->meta.use_flags));
854
855 virtio_gpu_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
856 }
857
858 for (i = 0; i < xfer_params.xfers_needed; i++) {
859 xfer.box.x = xfer_params.xfer_boxes[i].x;
860 xfer.box.y = xfer_params.xfer_boxes[i].y;
861 xfer.box.w = xfer_params.xfer_boxes[i].width;
862 xfer.box.h = xfer_params.xfer_boxes[i].height;
863 xfer.box.d = 1;
864
865 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
866 if (ret) {
867 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n",
868 strerror(errno));
869 return -errno;
870 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700871 }
872
David Stevens4d5358d2019-10-24 14:59:31 +0900873 // The transfer needs to complete before invalidate returns so that any host changes
874 // are visible and to ensure the host doesn't overwrite subsequent guest changes.
875 // TODO(b/136733358): Support returning fences from transfers
David Stevens4d5358d2019-10-24 14:59:31 +0900876 waitcmd.handle = mapping->vma->handle;
877 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
878 if (ret) {
879 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
880 return -errno;
881 }
882
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700883 return 0;
884}
885
886static int virtio_gpu_bo_flush(struct bo *bo, struct mapping *mapping)
887{
888 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800889 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700890 struct drm_virtgpu_3d_transfer_to_host xfer = { 0 };
891 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800892 struct virtio_transfers_params xfer_params;
893 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
Lepton Wu249e8632018-04-05 12:50:03 -0700894
Gurchetan Singhd708f612019-09-12 17:26:45 -0700895 if (!features[feat_3d].enabled)
Lepton Wu249e8632018-04-05 12:50:03 -0700896 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700897
898 if (!(mapping->vma->map_flags & BO_MAP_WRITE))
899 return 0;
900
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700901 if (features[feat_resource_blob].enabled &&
902 (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
903 return 0;
904
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700905 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700906
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700907 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700908 /*
909 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
910 * images
911 */
912 if (bo->meta.num_planes == 1) {
913 xfer.offset =
914 (bo->meta.strides[0] * mapping->rect.y) +
915 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
916 }
917 }
918
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700919 // Unfortunately, the kernel doesn't actually pass the guest layer_stride and
920 // guest stride to the host (compare virtio_gpu.h and virtgpu_drm.h). We can use
921 // the level to work around this.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800922 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800923 xfer.level = bo->meta.strides[0];
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700924
Jason Macnak1de7f662020-01-24 15:05:57 -0800925 if (virtio_gpu_supports_combination_natively(bo->drv, bo->meta.format,
926 bo->meta.use_flags)) {
927 xfer_params.xfers_needed = 1;
928 xfer_params.xfer_boxes[0] = mapping->rect;
929 } else {
930 assert(virtio_gpu_supports_combination_through_emulation(bo->drv, bo->meta.format,
931 bo->meta.use_flags));
932
933 virtio_gpu_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
934 }
935
936 for (i = 0; i < xfer_params.xfers_needed; i++) {
937 xfer.box.x = xfer_params.xfer_boxes[i].x;
938 xfer.box.y = xfer_params.xfer_boxes[i].y;
939 xfer.box.w = xfer_params.xfer_boxes[i].width;
940 xfer.box.h = xfer_params.xfer_boxes[i].height;
941 xfer.box.d = 1;
942
943 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
944 if (ret) {
945 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n",
946 strerror(errno));
947 return -errno;
948 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700949 }
950
David Stevensbaab6c82020-02-26 17:14:43 +0900951 // If the buffer is only accessed by the host GPU, then the flush is ordered
952 // with subsequent commands. However, if other host hardware can access the
953 // buffer, we need to wait for the transfer to complete for consistency.
954 // TODO(b/136733358): Support returning fences from transfers
955 if (bo->meta.use_flags & BO_USE_NON_GPU_HW) {
David Stevensbaab6c82020-02-26 17:14:43 +0900956 waitcmd.handle = mapping->vma->handle;
957
958 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
959 if (ret) {
960 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
961 return -errno;
962 }
963 }
964
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700965 return 0;
966}
967
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700968static uint32_t virtio_gpu_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700969{
970 switch (format) {
971 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900972 /* Camera subsystem requires NV12. */
973 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
974 return DRM_FORMAT_NV12;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700975 /*HACK: See b/28671744 */
976 return DRM_FORMAT_XBGR8888;
Lepton Wu249e8632018-04-05 12:50:03 -0700977 case DRM_FORMAT_FLEX_YCbCr_420_888:
Gurchetan Singhf5d280d2019-06-04 19:43:41 -0700978 /*
979 * All of our host drivers prefer NV12 as their flexible media format.
980 * If that changes, this will need to be modified.
981 */
Gurchetan Singhd708f612019-09-12 17:26:45 -0700982 if (features[feat_3d].enabled)
Gurchetan Singhf5d280d2019-06-04 19:43:41 -0700983 return DRM_FORMAT_NV12;
984 else
Jason Macnak1de7f662020-01-24 15:05:57 -0800985 return DRM_FORMAT_YVU420_ANDROID;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700986 default:
987 return format;
988 }
989}
990
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700991static int virtio_gpu_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
992 uint32_t offsets[DRV_MAX_PLANES])
993{
994 int ret;
Chia-I Wu2e41f632021-01-11 11:08:21 -0800995 struct drm_virtgpu_resource_info_cros res_info = { 0 };
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700996
Gurchetan Singhd708f612019-09-12 17:26:45 -0700997 if (!features[feat_3d].enabled)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700998 return 0;
999
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001000 res_info.bo_handle = bo->handles[0].u32;
Chia-I Wu50855622021-01-12 12:38:09 -08001001 res_info.type = VIRTGPU_RESOURCE_INFO_TYPE_EXTENDED;
Chia-I Wu2e41f632021-01-11 11:08:21 -08001002 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO_CROS, &res_info);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001003 if (ret) {
1004 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_INFO failed with %s\n", strerror(errno));
1005 return ret;
1006 }
1007
1008 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
1009 /*
1010 * Currently, kernel v4.14 (Betty) doesn't have the extended resource info
1011 * ioctl.
1012 */
1013 if (res_info.strides[plane]) {
1014 strides[plane] = res_info.strides[plane];
1015 offsets[plane] = res_info.offsets[plane];
1016 }
1017 }
1018
1019 return 0;
1020}
1021
Lepton Wu249e8632018-04-05 12:50:03 -07001022const struct backend backend_virtio_gpu = {
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001023 .name = "virtio_gpu",
1024 .init = virtio_gpu_init,
Lepton Wu249e8632018-04-05 12:50:03 -07001025 .close = virtio_gpu_close,
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001026 .bo_create = virtio_gpu_bo_create,
Lepton Wu249e8632018-04-05 12:50:03 -07001027 .bo_destroy = virtio_gpu_bo_destroy,
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001028 .bo_import = drv_prime_bo_import,
Lepton Wu249e8632018-04-05 12:50:03 -07001029 .bo_map = virtio_gpu_bo_map,
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001030 .bo_unmap = drv_bo_munmap,
1031 .bo_invalidate = virtio_gpu_bo_invalidate,
1032 .bo_flush = virtio_gpu_bo_flush,
1033 .resolve_format = virtio_gpu_resolve_format,
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07001034 .resource_info = virtio_gpu_resource_info,
Zach Reizner85c4c5f2017-10-04 13:15:57 -07001035};