blob: 32ca6a15528ea456fda135aca95c1875c2fe425a [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
15#include "drv_priv.h"
Gurchetan Singh9f3110b2020-04-03 15:15:30 -070016#include "external/virgl_hw.h"
17#include "external/virgl_protocol.h"
18#include "external/virtgpu_drm.h"
Zach Reizner85c4c5f2017-10-04 13:15:57 -070019#include "helpers.h"
20#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
Lepton Wu249e8632018-04-05 12:50:03 -070025#define MESA_LLVMPIPE_TILE_ORDER 6
26#define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
27
Zach Reizner85c4c5f2017-10-04 13:15:57 -070028static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070029 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
30 DRM_FORMAT_XRGB8888 };
Zach Reizner85c4c5f2017-10-04 13:15:57 -070031
Jason Macnak1de7f662020-01-24 15:05:57 -080032static const uint32_t dumb_texture_source_formats[] = {
33 DRM_FORMAT_R8, DRM_FORMAT_R16, DRM_FORMAT_YVU420,
34 DRM_FORMAT_NV12, DRM_FORMAT_NV21, DRM_FORMAT_YVU420_ANDROID
35};
Lepton Wu249e8632018-04-05 12:50:03 -070036
Jason Macnak1de7f662020-01-24 15:05:57 -080037static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_NV21,
38 DRM_FORMAT_R8, DRM_FORMAT_R16,
39 DRM_FORMAT_RG88, DRM_FORMAT_YVU420_ANDROID };
Zach Reizner85c4c5f2017-10-04 13:15:57 -070040
Gurchetan Singh73c141e2021-01-21 14:51:19 -080041extern struct virtgpu_param params[];
42
43struct virgl_priv {
Lepton Wueebce652020-02-26 15:13:34 -080044 int caps_is_v2;
Jason Macnakddf4ec02020-02-03 16:36:46 -080045 union virgl_caps caps;
Jason Macnak1de7f662020-01-24 15:05:57 -080046 int host_gbm_enabled;
David Stevens0fe561f2020-10-28 16:06:38 +090047 atomic_int next_blob_id;
Lepton Wu249e8632018-04-05 12:50:03 -070048};
49
Kansho Nishidad97877b2019-06-14 18:28:18 +090050static uint32_t translate_format(uint32_t drm_fourcc)
Zach Reizner85c4c5f2017-10-04 13:15:57 -070051{
52 switch (drm_fourcc) {
Jason Macnak1de7f662020-01-24 15:05:57 -080053 case DRM_FORMAT_BGR888:
54 case DRM_FORMAT_RGB888:
55 return VIRGL_FORMAT_R8G8B8_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070056 case DRM_FORMAT_XRGB8888:
57 return VIRGL_FORMAT_B8G8R8X8_UNORM;
58 case DRM_FORMAT_ARGB8888:
59 return VIRGL_FORMAT_B8G8R8A8_UNORM;
60 case DRM_FORMAT_XBGR8888:
61 return VIRGL_FORMAT_R8G8B8X8_UNORM;
62 case DRM_FORMAT_ABGR8888:
63 return VIRGL_FORMAT_R8G8B8A8_UNORM;
Jason Macnak1de7f662020-01-24 15:05:57 -080064 case DRM_FORMAT_ABGR16161616F:
Lepton Wufef113c2020-10-30 16:29:26 -070065 return VIRGL_FORMAT_R16G16B16A16_FLOAT;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070066 case DRM_FORMAT_RGB565:
67 return VIRGL_FORMAT_B5G6R5_UNORM;
68 case DRM_FORMAT_R8:
69 return VIRGL_FORMAT_R8_UNORM;
Jason Macnak6e200ea2021-02-11 19:34:57 -080070 case DRM_FORMAT_R16:
71 return VIRGL_FORMAT_R16_UNORM;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070072 case DRM_FORMAT_RG88:
73 return VIRGL_FORMAT_R8G8_UNORM;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -070074 case DRM_FORMAT_NV12:
75 return VIRGL_FORMAT_NV12;
Jason Macnak1de7f662020-01-24 15:05:57 -080076 case DRM_FORMAT_NV21:
77 return VIRGL_FORMAT_NV21;
Gurchetan Singhf5d280d2019-06-04 19:43:41 -070078 case DRM_FORMAT_YVU420:
79 case DRM_FORMAT_YVU420_ANDROID:
80 return VIRGL_FORMAT_YV12;
Zach Reizner85c4c5f2017-10-04 13:15:57 -070081 default:
Jason Macnak6e200ea2021-02-11 19:34:57 -080082 drv_log("Unhandled format:%d\n", drm_fourcc);
Zach Reizner85c4c5f2017-10-04 13:15:57 -070083 return 0;
84 }
85}
86
Gurchetan Singh73c141e2021-01-21 14:51:19 -080087static bool virgl_bitmask_supports_format(struct virgl_supported_format_mask *supported,
88 uint32_t drm_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -080089{
90 uint32_t virgl_format = translate_format(drm_format);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -080091 if (!virgl_format)
Jason Macnakddf4ec02020-02-03 16:36:46 -080092 return false;
Jason Macnakddf4ec02020-02-03 16:36:46 -080093
94 uint32_t bitmask_index = virgl_format / 32;
95 uint32_t bit_index = virgl_format % 32;
96 return supported->bitmask[bitmask_index] & (1 << bit_index);
97}
98
Jason Macnak1de7f662020-01-24 15:05:57 -080099// The metadata generated here for emulated buffers is slightly different than the metadata
100// generated by drv_bo_from_format. In order to simplify transfers in the flush and invalidate
101// functions below, the emulated buffers are oversized. For example, ignoring stride alignment
102// requirements to demonstrate, a 6x6 YUV420 image buffer might have the following layout from
103// drv_bo_from_format:
104//
105// | Y | Y | Y | Y | Y | Y |
106// | Y | Y | Y | Y | Y | Y |
107// | Y | Y | Y | Y | Y | Y |
108// | Y | Y | Y | Y | Y | Y |
109// | Y | Y | Y | Y | Y | Y |
110// | Y | Y | Y | Y | Y | Y |
111// | U | U | U | U | U | U |
112// | U | U | U | V | V | V |
113// | V | V | V | V | V | V |
114//
115// where each plane immediately follows the previous plane in memory. This layout makes it
116// difficult to compute the transfers needed for example when the middle 2x2 region of the
117// image is locked and needs to be flushed/invalidated.
118//
119// Emulated multi-plane buffers instead have a layout of:
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 | | | |
128// | U | U | U | | | |
129// | U | U | U | | | |
130// | V | V | V | | | |
131// | V | V | V | | | |
132// | V | V | V | | | |
133//
134// where each plane is placed as a sub-image (albeit with a very large stride) in order to
135// simplify transfers into 3 sub-image transfers for the above example.
136//
137// Additional note: the V-plane is not placed to the right of the U-plane due to some
138// observed failures in media framework code which assumes the V-plane is not
139// "row-interlaced" with the U-plane.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800140static void virgl_get_emulated_metadata(const struct bo *bo, struct bo_metadata *metadata)
Jason Macnak1de7f662020-01-24 15:05:57 -0800141{
142 uint32_t y_plane_height;
143 uint32_t c_plane_height;
144 uint32_t original_width = bo->meta.width;
145 uint32_t original_height = bo->meta.height;
146
147 metadata->format = DRM_FORMAT_R8;
148 switch (bo->meta.format) {
149 case DRM_FORMAT_NV12:
150 case DRM_FORMAT_NV21:
151 // Bi-planar
152 metadata->num_planes = 2;
153
154 y_plane_height = original_height;
155 c_plane_height = DIV_ROUND_UP(original_height, 2);
156
157 metadata->width = original_width;
158 metadata->height = y_plane_height + c_plane_height;
159
160 // Y-plane (full resolution)
161 metadata->strides[0] = metadata->width;
162 metadata->offsets[0] = 0;
163 metadata->sizes[0] = metadata->width * y_plane_height;
164
165 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
166 metadata->strides[1] = metadata->width;
167 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
168 metadata->sizes[1] = metadata->width * c_plane_height;
169
170 metadata->total_size = metadata->width * metadata->height;
171 break;
172 case DRM_FORMAT_YVU420:
173 case DRM_FORMAT_YVU420_ANDROID:
174 // Tri-planar
175 metadata->num_planes = 3;
176
177 y_plane_height = original_height;
178 c_plane_height = DIV_ROUND_UP(original_height, 2);
179
180 metadata->width = ALIGN(original_width, 32);
181 metadata->height = y_plane_height + (2 * c_plane_height);
182
183 // Y-plane (full resolution)
184 metadata->strides[0] = metadata->width;
185 metadata->offsets[0] = 0;
186 metadata->sizes[0] = metadata->width * original_height;
187
188 // Cb-plane (half resolution, placed below Y-plane)
189 metadata->strides[1] = metadata->width;
190 metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0];
191 metadata->sizes[1] = metadata->width * c_plane_height;
192
193 // Cr-plane (half resolution, placed below Cb-plane)
194 metadata->strides[2] = metadata->width;
195 metadata->offsets[2] = metadata->offsets[1] + metadata->sizes[1];
196 metadata->sizes[2] = metadata->width * c_plane_height;
197
198 metadata->total_size = metadata->width * metadata->height;
199 break;
200 default:
201 break;
202 }
203}
204
205struct virtio_transfers_params {
206 size_t xfers_needed;
207 struct rectangle xfer_boxes[DRV_MAX_PLANES];
208};
209
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800210static void virgl_get_emulated_transfers_params(const struct bo *bo,
211 const struct rectangle *transfer_box,
212 struct virtio_transfers_params *xfer_params)
Jason Macnak1de7f662020-01-24 15:05:57 -0800213{
214 uint32_t y_plane_height;
215 uint32_t c_plane_height;
216 struct bo_metadata emulated_metadata;
217
218 if (transfer_box->x == 0 && transfer_box->y == 0 && transfer_box->width == bo->meta.width &&
219 transfer_box->height == bo->meta.height) {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800220 virgl_get_emulated_metadata(bo, &emulated_metadata);
Jason Macnak1de7f662020-01-24 15:05:57 -0800221
222 xfer_params->xfers_needed = 1;
223 xfer_params->xfer_boxes[0].x = 0;
224 xfer_params->xfer_boxes[0].y = 0;
225 xfer_params->xfer_boxes[0].width = emulated_metadata.width;
226 xfer_params->xfer_boxes[0].height = emulated_metadata.height;
227
228 return;
229 }
230
231 switch (bo->meta.format) {
232 case DRM_FORMAT_NV12:
233 case DRM_FORMAT_NV21:
234 // Bi-planar
235 xfer_params->xfers_needed = 2;
236
237 y_plane_height = bo->meta.height;
238 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
239
240 // Y-plane (full resolution)
241 xfer_params->xfer_boxes[0].x = transfer_box->x;
242 xfer_params->xfer_boxes[0].y = transfer_box->y;
243 xfer_params->xfer_boxes[0].width = transfer_box->width;
244 xfer_params->xfer_boxes[0].height = transfer_box->height;
245
246 // CbCr-plane (half resolution, interleaved, placed below Y-plane)
247 xfer_params->xfer_boxes[1].x = transfer_box->x;
248 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
249 xfer_params->xfer_boxes[1].width = transfer_box->width;
250 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
251
252 break;
253 case DRM_FORMAT_YVU420:
254 case DRM_FORMAT_YVU420_ANDROID:
255 // Tri-planar
256 xfer_params->xfers_needed = 3;
257
258 y_plane_height = bo->meta.height;
259 c_plane_height = DIV_ROUND_UP(bo->meta.height, 2);
260
261 // Y-plane (full resolution)
262 xfer_params->xfer_boxes[0].x = transfer_box->x;
263 xfer_params->xfer_boxes[0].y = transfer_box->y;
264 xfer_params->xfer_boxes[0].width = transfer_box->width;
265 xfer_params->xfer_boxes[0].height = transfer_box->height;
266
267 // Cb-plane (half resolution, placed below Y-plane)
268 xfer_params->xfer_boxes[1].x = transfer_box->x;
269 xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height;
270 xfer_params->xfer_boxes[1].width = DIV_ROUND_UP(transfer_box->width, 2);
271 xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2);
272
273 // Cr-plane (half resolution, placed below Cb-plane)
274 xfer_params->xfer_boxes[2].x = transfer_box->x;
275 xfer_params->xfer_boxes[2].y = transfer_box->y + y_plane_height + c_plane_height;
276 xfer_params->xfer_boxes[2].width = DIV_ROUND_UP(transfer_box->width, 2);
277 xfer_params->xfer_boxes[2].height = DIV_ROUND_UP(transfer_box->height, 2);
278
279 break;
280 }
281}
282
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800283static bool virgl_supports_combination_natively(struct driver *drv, uint32_t drm_format,
284 uint64_t use_flags)
Jason Macnak1de7f662020-01-24 15:05:57 -0800285{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800286 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800287
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800288 if (priv->caps.max_version == 0)
Jason Macnak1de7f662020-01-24 15:05:57 -0800289 return true;
Jason Macnak1de7f662020-01-24 15:05:57 -0800290
291 if ((use_flags & BO_USE_RENDERING) &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800292 !virgl_bitmask_supports_format(&priv->caps.v1.render, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800293 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800294
295 if ((use_flags & BO_USE_TEXTURE) &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800296 !virgl_bitmask_supports_format(&priv->caps.v1.sampler, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800297 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800298
299 if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800300 !virgl_bitmask_supports_format(&priv->caps.v2.scanout, drm_format))
Jason Macnak1de7f662020-01-24 15:05:57 -0800301 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800302
303 return true;
304}
305
306// For virtio backends that do not support formats natively (e.g. multi-planar formats are not
307// supported in virglrenderer when gbm is unavailable on the host machine), whether or not the
308// format and usage combination can be handled as a blob (byte buffer).
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800309static bool virgl_supports_combination_through_emulation(struct driver *drv, uint32_t drm_format,
310 uint64_t use_flags)
Jason Macnak1de7f662020-01-24 15:05:57 -0800311{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800312 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800313
314 // Only enable emulation on non-gbm virtio backends.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800315 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800316 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800317
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800318 if (use_flags & (BO_USE_RENDERING | BO_USE_SCANOUT))
Jason Macnak1de7f662020-01-24 15:05:57 -0800319 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800320
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800321 if (!virgl_supports_combination_natively(drv, DRM_FORMAT_R8, use_flags))
Jason Macnak1de7f662020-01-24 15:05:57 -0800322 return false;
Jason Macnak1de7f662020-01-24 15:05:57 -0800323
324 return drm_format == DRM_FORMAT_NV12 || drm_format == DRM_FORMAT_NV21 ||
325 drm_format == DRM_FORMAT_YVU420 || drm_format == DRM_FORMAT_YVU420_ANDROID;
326}
327
Jason Macnakddf4ec02020-02-03 16:36:46 -0800328// Adds the given buffer combination to the list of supported buffer combinations if the
329// combination is supported by the virtio backend.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800330static void virgl_add_combination(struct driver *drv, uint32_t drm_format,
331 struct format_metadata *metadata, uint64_t use_flags)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800332{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800333 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800334
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800335 if (params[param_3d].value && priv->caps.max_version >= 1) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800336 if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 &&
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800337 !virgl_supports_combination_natively(drv, drm_format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800338 drv_log("Scanout format: %d\n", drm_format);
339 use_flags &= ~BO_USE_SCANOUT;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800340 }
341
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800342 if (!virgl_supports_combination_natively(drv, drm_format, use_flags) &&
343 !virgl_supports_combination_through_emulation(drv, drm_format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800344 drv_log("Skipping unsupported combination format:%d\n", drm_format);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800345 return;
346 }
347 }
348
349 drv_add_combination(drv, drm_format, metadata, use_flags);
350}
351
352// Adds each given buffer combination to the list of supported buffer combinations if the
353// combination supported by the virtio backend.
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800354static void virgl_add_combinations(struct driver *drv, const uint32_t *drm_formats,
355 uint32_t num_formats, struct format_metadata *metadata,
356 uint64_t use_flags)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800357{
358 uint32_t i;
359
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800360 for (i = 0; i < num_formats; i++)
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800361 virgl_add_combination(drv, drm_formats[i], metadata, use_flags);
Jason Macnakddf4ec02020-02-03 16:36:46 -0800362}
363
Lepton Wu249e8632018-04-05 12:50:03 -0700364static int virtio_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
365 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700366{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700367 if (bo->meta.format != DRM_FORMAT_R8) {
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900368 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
369 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
370 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700371
Dominik Behr6e6dc492019-10-09 15:43:52 -0700372 return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_DUMB32BPP);
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700373}
374
Lepton Wudbab0832019-04-19 12:26:39 -0700375static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
376 uint32_t virgl_bind)
377{
378 if ((*flag) & check_flag) {
379 (*flag) &= ~check_flag;
380 (*bind) |= virgl_bind;
381 }
382}
383
David Stevenscf280482020-12-21 11:43:44 +0900384static uint32_t compute_virgl_bind_flags(uint64_t use_flags, uint32_t format)
Lepton Wudbab0832019-04-19 12:26:39 -0700385{
Kansho Nishidad97877b2019-06-14 18:28:18 +0900386 /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
387 uint32_t bind = VIRGL_BIND_SHARED;
Lepton Wudbab0832019-04-19 12:26:39 -0700388
389 handle_flag(&use_flags, BO_USE_TEXTURE, &bind, VIRGL_BIND_SAMPLER_VIEW);
390 handle_flag(&use_flags, BO_USE_RENDERING, &bind, VIRGL_BIND_RENDER_TARGET);
391 handle_flag(&use_flags, BO_USE_SCANOUT, &bind, VIRGL_BIND_SCANOUT);
David Stevens55a6cf92019-09-03 10:45:33 +0900392 handle_flag(&use_flags, BO_USE_CURSOR, &bind, VIRGL_BIND_CURSOR);
393 handle_flag(&use_flags, BO_USE_LINEAR, &bind, VIRGL_BIND_LINEAR);
394
David Stevens23de4e22020-05-15 14:15:35 +0900395 if (use_flags & BO_USE_PROTECTED) {
396 handle_flag(&use_flags, BO_USE_PROTECTED, &bind, VIRGL_BIND_MINIGBM_PROTECTED);
397 } else {
398 // Make sure we don't set both flags, since that could be mistaken for
399 // protected. Give OFTEN priority over RARELY.
400 if (use_flags & BO_USE_SW_READ_OFTEN) {
401 handle_flag(&use_flags, BO_USE_SW_READ_OFTEN, &bind,
402 VIRGL_BIND_MINIGBM_SW_READ_OFTEN);
403 } else {
404 handle_flag(&use_flags, BO_USE_SW_READ_RARELY, &bind,
405 VIRGL_BIND_MINIGBM_SW_READ_RARELY);
406 }
407 if (use_flags & BO_USE_SW_WRITE_OFTEN) {
408 handle_flag(&use_flags, BO_USE_SW_WRITE_OFTEN, &bind,
409 VIRGL_BIND_MINIGBM_SW_WRITE_OFTEN);
410 } else {
411 handle_flag(&use_flags, BO_USE_SW_WRITE_RARELY, &bind,
412 VIRGL_BIND_MINIGBM_SW_WRITE_RARELY);
413 }
414 }
David Stevens55a6cf92019-09-03 10:45:33 +0900415
David Stevens23de4e22020-05-15 14:15:35 +0900416 handle_flag(&use_flags, BO_USE_CAMERA_WRITE, &bind, VIRGL_BIND_MINIGBM_CAMERA_WRITE);
417 handle_flag(&use_flags, BO_USE_CAMERA_READ, &bind, VIRGL_BIND_MINIGBM_CAMERA_READ);
418 handle_flag(&use_flags, BO_USE_HW_VIDEO_DECODER, &bind,
419 VIRGL_BIND_MINIGBM_HW_VIDEO_DECODER);
420 handle_flag(&use_flags, BO_USE_HW_VIDEO_ENCODER, &bind,
421 VIRGL_BIND_MINIGBM_HW_VIDEO_ENCODER);
David Stevens55a6cf92019-09-03 10:45:33 +0900422
David Stevenscf280482020-12-21 11:43:44 +0900423 /*
424 * HACK: This is for HAL_PIXEL_FORMAT_YV12 buffers allocated by arcvm. None of
425 * our platforms can display YV12, so we can treat as a SW buffer. Remove once
426 * this can be intelligently resolved in the guest. Also see gbm_bo_create.
427 */
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800428 if (format == DRM_FORMAT_YVU420_ANDROID)
David Stevenscf280482020-12-21 11:43:44 +0900429 bind |= VIRGL_BIND_LINEAR;
David Stevenscf280482020-12-21 11:43:44 +0900430
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800431 if (use_flags)
Lepton Wudbab0832019-04-19 12:26:39 -0700432 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900433
Lepton Wudbab0832019-04-19 12:26:39 -0700434 return bind;
435}
436
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800437static int virgl_3d_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
438 uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700439{
440 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800441 size_t i;
Kansho Nishidad97877b2019-06-14 18:28:18 +0900442 uint32_t stride;
Gurchetan Singh99644382020-10-07 15:28:11 -0700443 struct drm_virtgpu_resource_create res_create = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800444 struct bo_metadata emulated_metadata;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700445
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800446 if (virgl_supports_combination_natively(bo->drv, format, use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800447 stride = drv_stride_from_format(format, width, 0);
448 drv_bo_from_format(bo, stride, height, format);
449 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800450 assert(virgl_supports_combination_through_emulation(bo->drv, format, use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800451
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800452 virgl_get_emulated_metadata(bo, &emulated_metadata);
Jason Macnak1de7f662020-01-24 15:05:57 -0800453
454 format = emulated_metadata.format;
455 width = emulated_metadata.width;
456 height = emulated_metadata.height;
457 for (i = 0; i < emulated_metadata.num_planes; i++) {
458 bo->meta.strides[i] = emulated_metadata.strides[i];
459 bo->meta.offsets[i] = emulated_metadata.offsets[i];
460 bo->meta.sizes[i] = emulated_metadata.sizes[i];
461 }
462 bo->meta.total_size = emulated_metadata.total_size;
463 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700464
Kansho Nishidad97877b2019-06-14 18:28:18 +0900465 /*
466 * Setting the target is intended to ensure this resource gets bound as a 2D
467 * texture in the host renderer's GL state. All of these resource properties are
468 * sent unchanged by the kernel to the host, which in turn sends them unchanged to
469 * virglrenderer. When virglrenderer makes a resource, it will convert the target
470 * enum to the equivalent one in GL and then bind the resource to that target.
471 */
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700472
Kansho Nishidad97877b2019-06-14 18:28:18 +0900473 res_create.target = PIPE_TEXTURE_2D;
474 res_create.format = translate_format(format);
David Stevenscf280482020-12-21 11:43:44 +0900475 res_create.bind = compute_virgl_bind_flags(use_flags, format);
Kansho Nishidad97877b2019-06-14 18:28:18 +0900476 res_create.width = width;
477 res_create.height = height;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700478
Kansho Nishidad97877b2019-06-14 18:28:18 +0900479 /* For virgl 3D */
480 res_create.depth = 1;
481 res_create.array_size = 1;
482 res_create.last_level = 0;
483 res_create.nr_samples = 0;
484
Gurchetan Singh298b7572019-09-19 09:55:18 -0700485 res_create.size = ALIGN(bo->meta.total_size, PAGE_SIZE); // PAGE_SIZE = 0x1000
Kansho Nishidad97877b2019-06-14 18:28:18 +0900486 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create);
487 if (ret) {
488 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
489 return ret;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700490 }
491
Gurchetan Singh298b7572019-09-19 09:55:18 -0700492 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
Kansho Nishidad97877b2019-06-14 18:28:18 +0900493 bo->handles[plane].u32 = res_create.bo_handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700494
495 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700496}
497
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800498static 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 -0700499{
500 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700501 struct drm_virtgpu_map gem_map = { 0 };
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700502
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700503 gem_map.handle = bo->handles[0].u32;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700504 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
505 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700506 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700507 return MAP_FAILED;
508 }
509
Gurchetan Singh298b7572019-09-19 09:55:18 -0700510 vma->length = bo->meta.total_size;
511 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700512 gem_map.offset);
513}
514
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800515static int virgl_get_caps(struct driver *drv, union virgl_caps *caps, int *caps_is_v2)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800516{
517 int ret;
Gurchetan Singh99644382020-10-07 15:28:11 -0700518 struct drm_virtgpu_get_caps cap_args = { 0 };
Jason Macnakddf4ec02020-02-03 16:36:46 -0800519
Lepton Wueebce652020-02-26 15:13:34 -0800520 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800521 cap_args.addr = (unsigned long long)caps;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800522 if (params[param_capset_fix].value) {
Lepton Wueebce652020-02-26 15:13:34 -0800523 *caps_is_v2 = 1;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800524 cap_args.cap_set_id = 2;
525 cap_args.size = sizeof(union virgl_caps);
526 } else {
527 cap_args.cap_set_id = 1;
528 cap_args.size = sizeof(struct virgl_caps_v1);
529 }
530
531 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
532 if (ret) {
533 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Lepton Wueebce652020-02-26 15:13:34 -0800534 *caps_is_v2 = 0;
Jason Macnakddf4ec02020-02-03 16:36:46 -0800535
536 // Fallback to v1
537 cap_args.cap_set_id = 1;
538 cap_args.size = sizeof(struct virgl_caps_v1);
539
540 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args);
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800541 if (ret)
Jason Macnakddf4ec02020-02-03 16:36:46 -0800542 drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno));
Jason Macnakddf4ec02020-02-03 16:36:46 -0800543 }
544
545 return ret;
546}
547
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800548static void virgl_init_params_and_caps(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700549{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800550 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
551 if (params[param_3d].value) {
552 virgl_get_caps(drv, &priv->caps, &priv->caps_is_v2);
Lepton Wu249e8632018-04-05 12:50:03 -0700553
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800554 // We use two criteria to determine whether host minigbm is used on the host for
555 // swapchain allocations.
556 //
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800557 // (1) Host minigbm is only available via virglrenderer, and only virglrenderer
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800558 // advertises capabilities.
559 // (2) Only host minigbm doesn't emulate YUV formats. Checking this is a bit of a
560 // proxy, but it works.
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800561 priv->host_gbm_enabled =
562 priv->caps.max_version > 0 &&
563 virgl_supports_combination_natively(drv, DRM_FORMAT_NV12, BO_USE_TEXTURE);
Lepton Wu249e8632018-04-05 12:50:03 -0700564 }
Jason Macnak1de7f662020-01-24 15:05:57 -0800565}
566
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800567static int virgl_init(struct driver *drv)
Jason Macnak1de7f662020-01-24 15:05:57 -0800568{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800569 struct virgl_priv *priv;
Jason Macnak1de7f662020-01-24 15:05:57 -0800570
571 priv = calloc(1, sizeof(*priv));
572 drv->priv = priv;
573
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800574 virgl_init_params_and_caps(drv);
Jason Macnak1de7f662020-01-24 15:05:57 -0800575
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800576 if (params[param_3d].value) {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700577 /* This doesn't mean host can scanout everything, it just means host
578 * hypervisor can show it. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800579 virgl_add_combinations(drv, render_target_formats,
580 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
581 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
582 virgl_add_combinations(drv, texture_source_formats,
583 ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
584 BO_USE_TEXTURE_MASK);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700585 } else {
Dominik Behr6e6dc492019-10-09 15:43:52 -0700586 /* Virtio primary plane only allows this format. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800587 virgl_add_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
588 BO_USE_RENDER_MASK | BO_USE_SCANOUT);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700589 /* Virtio cursor plane only allows this format and Chrome cannot live without
590 * ARGB888 renderable format. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800591 virgl_add_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
592 BO_USE_RENDER_MASK | BO_USE_CURSOR);
Dominik Behr6e6dc492019-10-09 15:43:52 -0700593 /* Android needs more, but they cannot be bound as scanouts anymore after
594 * "drm/virtio: fix DRM_FORMAT_* handling" */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800595 virgl_add_combinations(drv, render_target_formats,
596 ARRAY_SIZE(render_target_formats), &LINEAR_METADATA,
597 BO_USE_RENDER_MASK);
598 virgl_add_combinations(drv, dumb_texture_source_formats,
599 ARRAY_SIZE(dumb_texture_source_formats), &LINEAR_METADATA,
600 BO_USE_TEXTURE_MASK);
601 virgl_add_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
602 BO_USE_SW_MASK | BO_USE_LINEAR);
603 virgl_add_combination(drv, DRM_FORMAT_NV21, &LINEAR_METADATA,
604 BO_USE_SW_MASK | BO_USE_LINEAR);
Gurchetan Singh3f3e5f92019-07-08 09:50:01 -0700605 }
Lepton Wu249e8632018-04-05 12:50:03 -0700606
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700607 /* Android CTS tests require this. */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800608 virgl_add_combination(drv, DRM_FORMAT_RGB888, &LINEAR_METADATA, BO_USE_SW_MASK);
609 virgl_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
610 virgl_add_combination(drv, DRM_FORMAT_ABGR16161616F, &LINEAR_METADATA,
Jason Macnak1de7f662020-01-24 15:05:57 -0800611 BO_USE_SW_MASK | BO_USE_TEXTURE_MASK);
Jason Macnakb505a502021-06-08 06:39:53 -0700612 virgl_add_combination(drv, DRM_FORMAT_ABGR2101010, &LINEAR_METADATA,
613 BO_USE_SW_MASK | BO_USE_TEXTURE_MASK);
Jason Macnakcab39662021-05-25 12:59:35 -0700614 virgl_add_combination(drv, DRM_FORMAT_P010, &LINEAR_METADATA,
Emilian Peeva32d9732021-01-21 11:35:27 -0800615 BO_USE_SW_MASK | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700616
David Stevens9f7897f2019-08-09 20:20:23 +0900617 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
Hirokazu Honda20e4a932019-12-06 15:21:45 +0900618 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
619 BO_USE_HW_VIDEO_ENCODER);
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900620 drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
David Staessens04b7e242020-05-28 15:47:15 +0900621 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
622 BO_USE_HW_VIDEO_ENCODER);
David Stevens519978f2020-12-11 14:09:56 +0900623
624 if (!priv->host_gbm_enabled) {
625 drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &LINEAR_METADATA,
626 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
627 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
628 drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &LINEAR_METADATA,
629 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
630 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
631 drv_modify_combination(drv, DRM_FORMAT_NV21, &LINEAR_METADATA,
632 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
633 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
634 drv_modify_combination(drv, DRM_FORMAT_R16, &LINEAR_METADATA,
635 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
636 BO_USE_HW_VIDEO_DECODER);
637 drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA,
638 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
639 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
640 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &LINEAR_METADATA,
641 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
642 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
643 }
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900644
Lepton Wu249e8632018-04-05 12:50:03 -0700645 return drv_modify_linear_combinations(drv);
646}
647
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800648static void virgl_close(struct driver *drv)
Lepton Wu249e8632018-04-05 12:50:03 -0700649{
650 free(drv->priv);
651 drv->priv = NULL;
652}
653
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800654static int virgl_bo_create_blob(struct driver *drv, struct bo *bo)
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700655{
656 int ret;
657 uint32_t stride;
David Stevens0fe561f2020-10-28 16:06:38 +0900658 uint32_t cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700659 uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 };
660 struct drm_virtgpu_resource_create_blob drm_rc_blob = { 0 };
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800661 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700662
David Stevensd3f07bd2020-09-25 18:52:26 +0900663 uint32_t blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE;
664 if (bo->meta.use_flags & BO_USE_SW_MASK)
665 blob_flags |= VIRTGPU_BLOB_FLAG_USE_MAPPABLE;
666 if (bo->meta.use_flags & BO_USE_NON_GPU_HW)
David Stevensb42624c2020-09-10 10:50:26 +0900667 blob_flags |= VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE;
David Stevensb42624c2020-09-10 10:50:26 +0900668
David Stevens0fe561f2020-10-28 16:06:38 +0900669 cur_blob_id = atomic_fetch_add(&priv->next_blob_id, 1);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700670 stride = drv_stride_from_format(bo->meta.format, bo->meta.width, 0);
671 drv_bo_from_format(bo, stride, bo->meta.height, bo->meta.format);
672 bo->meta.total_size = ALIGN(bo->meta.total_size, PAGE_SIZE);
David Stevensb42624c2020-09-10 10:50:26 +0900673 bo->meta.tiling = blob_flags;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700674
675 cmd[0] = VIRGL_CMD0(VIRGL_CCMD_PIPE_RESOURCE_CREATE, 0, VIRGL_PIPE_RES_CREATE_SIZE);
676 cmd[VIRGL_PIPE_RES_CREATE_TARGET] = PIPE_TEXTURE_2D;
677 cmd[VIRGL_PIPE_RES_CREATE_WIDTH] = bo->meta.width;
678 cmd[VIRGL_PIPE_RES_CREATE_HEIGHT] = bo->meta.height;
679 cmd[VIRGL_PIPE_RES_CREATE_FORMAT] = translate_format(bo->meta.format);
David Stevenscf280482020-12-21 11:43:44 +0900680 cmd[VIRGL_PIPE_RES_CREATE_BIND] =
681 compute_virgl_bind_flags(bo->meta.use_flags, bo->meta.format);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700682 cmd[VIRGL_PIPE_RES_CREATE_DEPTH] = 1;
David Stevens0fe561f2020-10-28 16:06:38 +0900683 cmd[VIRGL_PIPE_RES_CREATE_BLOB_ID] = cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700684
685 drm_rc_blob.cmd = (uint64_t)&cmd;
686 drm_rc_blob.cmd_size = 4 * (VIRGL_PIPE_RES_CREATE_SIZE + 1);
687 drm_rc_blob.size = bo->meta.total_size;
688 drm_rc_blob.blob_mem = VIRTGPU_BLOB_MEM_HOST3D;
David Stevensb42624c2020-09-10 10:50:26 +0900689 drm_rc_blob.blob_flags = blob_flags;
David Stevens0fe561f2020-10-28 16:06:38 +0900690 drm_rc_blob.blob_id = cur_blob_id;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700691
692 ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &drm_rc_blob);
693 if (ret < 0) {
694 drv_log("DRM_VIRTGPU_RESOURCE_CREATE_BLOB failed with %s\n", strerror(errno));
695 return -errno;
696 }
697
698 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
699 bo->handles[plane].u32 = drm_rc_blob.bo_handle;
700
701 return 0;
702}
703
704static bool should_use_blob(struct driver *drv, uint32_t format, uint64_t use_flags)
705{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800706 struct virgl_priv *priv = (struct virgl_priv *)drv->priv;
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700707
708 // TODO(gurchetansingh): remove once all minigbm users are blob-safe
709#ifndef VIRTIO_GPU_NEXT
710 return false;
711#endif
712
713 // Only use blob when host gbm is available
714 if (!priv->host_gbm_enabled)
715 return false;
716
David Stevensd3f07bd2020-09-25 18:52:26 +0900717 // Use regular resources if only the GPU needs efficient access
718 if (!(use_flags &
719 (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 -0700720 return false;
721
David Stevensd3f07bd2020-09-25 18:52:26 +0900722 switch (format) {
723 case DRM_FORMAT_YVU420_ANDROID:
724 case DRM_FORMAT_R8:
725 // Formats with strictly defined strides are supported
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700726 return true;
David Stevensd3f07bd2020-09-25 18:52:26 +0900727 case DRM_FORMAT_NV12:
728 // Knowing buffer metadata at buffer creation isn't yet supported, so buffers
729 // can't be properly mapped into the guest.
730 return (use_flags & BO_USE_SW_MASK) == 0;
731 default:
732 return false;
733 }
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700734}
735
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800736static int virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
737 uint64_t use_flags)
Lepton Wu249e8632018-04-05 12:50:03 -0700738{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800739 if (params[param_resource_blob].value && params[param_host_visible].value &&
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700740 should_use_blob(bo->drv, format, use_flags))
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800741 return virgl_bo_create_blob(bo->drv, bo);
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700742
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800743 if (params[param_3d].value)
744 return virgl_3d_bo_create(bo, width, height, format, use_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700745 else
746 return virtio_dumb_bo_create(bo, width, height, format, use_flags);
747}
748
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800749static int virgl_bo_destroy(struct bo *bo)
Lepton Wu249e8632018-04-05 12:50:03 -0700750{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800751 if (params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700752 return drv_gem_bo_destroy(bo);
753 else
754 return drv_dumb_bo_destroy(bo);
755}
756
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800757static void *virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Lepton Wu249e8632018-04-05 12:50:03 -0700758{
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800759 if (params[param_3d].value)
760 return virgl_3d_bo_map(bo, vma, plane, map_flags);
Lepton Wu249e8632018-04-05 12:50:03 -0700761 else
762 return drv_dumb_bo_map(bo, vma, plane, map_flags);
763}
764
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800765static int virgl_bo_invalidate(struct bo *bo, struct mapping *mapping)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700766{
767 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800768 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700769 struct drm_virtgpu_3d_transfer_from_host xfer = { 0 };
770 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800771 struct virtio_transfers_params xfer_params;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800772 struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv;
David Stevens9fe8c202020-12-21 18:47:55 +0900773 uint64_t host_write_flags;
Lepton Wu249e8632018-04-05 12:50:03 -0700774
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800775 if (!params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700776 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700777
David Stevens9fe8c202020-12-21 18:47:55 +0900778 // Invalidate is only necessary if the host writes to the buffer. The encoder and
779 // decoder flags don't differentiate between input and output buffers, but we can
780 // use the format to determine whether this buffer could be encoder/decoder output.
781 host_write_flags = BO_USE_RENDERING | BO_USE_CAMERA_WRITE;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800782 if (bo->meta.format == DRM_FORMAT_R8)
David Stevens9fe8c202020-12-21 18:47:55 +0900783 host_write_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800784 else
David Stevens9fe8c202020-12-21 18:47:55 +0900785 host_write_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800786
David Stevens9fe8c202020-12-21 18:47:55 +0900787 if ((bo->meta.use_flags & host_write_flags) == 0)
David Stevens4d5358d2019-10-24 14:59:31 +0900788 return 0;
789
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800790 if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700791 return 0;
792
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700793 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700794
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700795 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700796 /*
797 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
798 * images
799 */
800 if (bo->meta.num_planes == 1) {
801 xfer.offset =
802 (bo->meta.strides[0] * mapping->rect.y) +
803 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
804 }
805 }
806
David Stevensbaab6c82020-02-26 17:14:43 +0900807 if ((bo->meta.use_flags & BO_USE_RENDERING) == 0) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800808 // Unfortunately, the kernel doesn't actually pass the guest layer_stride
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800809 // and guest stride to the host (compare virgl.h and virtgpu_drm.h).
Jason Macnak1de7f662020-01-24 15:05:57 -0800810 // For gbm based resources, we can work around this by using the level field
811 // to pass the stride to virglrenderer's gbm transfer code. However, we need
812 // to avoid doing this for resources which don't rely on that transfer code,
813 // which is resources with the BO_USE_RENDERING flag set.
David Stevensbaab6c82020-02-26 17:14:43 +0900814 // TODO(b/145993887): Send also stride when the patches are landed
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800815 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800816 xfer.level = bo->meta.strides[0];
David Stevensbaab6c82020-02-26 17:14:43 +0900817 }
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700818
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800819 if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800820 xfer_params.xfers_needed = 1;
821 xfer_params.xfer_boxes[0] = mapping->rect;
822 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800823 assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format,
824 bo->meta.use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800825
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800826 virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
Jason Macnak1de7f662020-01-24 15:05:57 -0800827 }
828
829 for (i = 0; i < xfer_params.xfers_needed; i++) {
830 xfer.box.x = xfer_params.xfer_boxes[i].x;
831 xfer.box.y = xfer_params.xfer_boxes[i].y;
832 xfer.box.w = xfer_params.xfer_boxes[i].width;
833 xfer.box.h = xfer_params.xfer_boxes[i].height;
834 xfer.box.d = 1;
835
836 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
837 if (ret) {
838 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n",
839 strerror(errno));
840 return -errno;
841 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700842 }
843
David Stevens4d5358d2019-10-24 14:59:31 +0900844 // The transfer needs to complete before invalidate returns so that any host changes
845 // are visible and to ensure the host doesn't overwrite subsequent guest changes.
846 // TODO(b/136733358): Support returning fences from transfers
David Stevens4d5358d2019-10-24 14:59:31 +0900847 waitcmd.handle = mapping->vma->handle;
848 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
849 if (ret) {
850 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
851 return -errno;
852 }
853
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700854 return 0;
855}
856
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800857static int virgl_bo_flush(struct bo *bo, struct mapping *mapping)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700858{
859 int ret;
Jason Macnak1de7f662020-01-24 15:05:57 -0800860 size_t i;
Gurchetan Singh99644382020-10-07 15:28:11 -0700861 struct drm_virtgpu_3d_transfer_to_host xfer = { 0 };
862 struct drm_virtgpu_3d_wait waitcmd = { 0 };
Jason Macnak1de7f662020-01-24 15:05:57 -0800863 struct virtio_transfers_params xfer_params;
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800864 struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv;
Lepton Wu249e8632018-04-05 12:50:03 -0700865
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800866 if (!params[param_3d].value)
Lepton Wu249e8632018-04-05 12:50:03 -0700867 return 0;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700868
869 if (!(mapping->vma->map_flags & BO_MAP_WRITE))
870 return 0;
871
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800872 if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE))
Gurchetan Singh0ee06fb2019-09-13 17:49:20 -0700873 return 0;
874
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700875 xfer.bo_handle = mapping->vma->handle;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700876
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700877 if (mapping->rect.x || mapping->rect.y) {
Gurchetan Singh1b57fe22020-05-05 09:18:22 -0700878 /*
879 * virglrenderer uses the box parameters and assumes that offset == 0 for planar
880 * images
881 */
882 if (bo->meta.num_planes == 1) {
883 xfer.offset =
884 (bo->meta.strides[0] * mapping->rect.y) +
885 drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x;
886 }
887 }
888
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700889 // Unfortunately, the kernel doesn't actually pass the guest layer_stride and
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800890 // guest stride to the host (compare virgl.h and virtgpu_drm.h). We can use
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700891 // the level to work around this.
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800892 if (priv->host_gbm_enabled)
Jason Macnak1de7f662020-01-24 15:05:57 -0800893 xfer.level = bo->meta.strides[0];
Gurchetan Singh05e67cc2019-06-28 17:21:40 -0700894
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800895 if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) {
Jason Macnak1de7f662020-01-24 15:05:57 -0800896 xfer_params.xfers_needed = 1;
897 xfer_params.xfer_boxes[0] = mapping->rect;
898 } else {
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800899 assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format,
900 bo->meta.use_flags));
Jason Macnak1de7f662020-01-24 15:05:57 -0800901
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800902 virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params);
Jason Macnak1de7f662020-01-24 15:05:57 -0800903 }
904
905 for (i = 0; i < xfer_params.xfers_needed; i++) {
906 xfer.box.x = xfer_params.xfer_boxes[i].x;
907 xfer.box.y = xfer_params.xfer_boxes[i].y;
908 xfer.box.w = xfer_params.xfer_boxes[i].width;
909 xfer.box.h = xfer_params.xfer_boxes[i].height;
910 xfer.box.d = 1;
911
912 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
913 if (ret) {
914 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n",
915 strerror(errno));
916 return -errno;
917 }
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700918 }
919
David Stevensbaab6c82020-02-26 17:14:43 +0900920 // If the buffer is only accessed by the host GPU, then the flush is ordered
921 // with subsequent commands. However, if other host hardware can access the
922 // buffer, we need to wait for the transfer to complete for consistency.
923 // TODO(b/136733358): Support returning fences from transfers
924 if (bo->meta.use_flags & BO_USE_NON_GPU_HW) {
David Stevensbaab6c82020-02-26 17:14:43 +0900925 waitcmd.handle = mapping->vma->handle;
926
927 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
928 if (ret) {
929 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
930 return -errno;
931 }
932 }
933
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700934 return 0;
935}
936
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800937static uint32_t virgl_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700938{
939 switch (format) {
940 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Keiichi Watanabea13dda72018-08-02 22:45:05 +0900941 /* Camera subsystem requires NV12. */
942 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
943 return DRM_FORMAT_NV12;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700944 /*HACK: See b/28671744 */
945 return DRM_FORMAT_XBGR8888;
Lepton Wu249e8632018-04-05 12:50:03 -0700946 case DRM_FORMAT_FLEX_YCbCr_420_888:
Gurchetan Singhf5d280d2019-06-04 19:43:41 -0700947 /*
948 * All of our host drivers prefer NV12 as their flexible media format.
949 * If that changes, this will need to be modified.
950 */
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800951 if (params[param_3d].value)
Gurchetan Singhf5d280d2019-06-04 19:43:41 -0700952 return DRM_FORMAT_NV12;
953 else
Jason Macnak1de7f662020-01-24 15:05:57 -0800954 return DRM_FORMAT_YVU420_ANDROID;
Zach Reizner85c4c5f2017-10-04 13:15:57 -0700955 default:
956 return format;
957 }
958}
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800959static int virgl_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000960 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700961{
962 int ret;
Chia-I Wu2e41f632021-01-11 11:08:21 -0800963 struct drm_virtgpu_resource_info_cros res_info = { 0 };
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700964
Gurchetan Singh73c141e2021-01-21 14:51:19 -0800965 if (!params[param_3d].value)
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700966 return 0;
967
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700968 res_info.bo_handle = bo->handles[0].u32;
Chia-I Wu50855622021-01-12 12:38:09 -0800969 res_info.type = VIRTGPU_RESOURCE_INFO_TYPE_EXTENDED;
Chia-I Wu2e41f632021-01-11 11:08:21 -0800970 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO_CROS, &res_info);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700971 if (ret) {
972 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_INFO failed with %s\n", strerror(errno));
973 return ret;
974 }
975
976 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
977 /*
978 * Currently, kernel v4.14 (Betty) doesn't have the extended resource info
979 * ioctl.
980 */
981 if (res_info.strides[plane]) {
982 strides[plane] = res_info.strides[plane];
983 offsets[plane] = res_info.offsets[plane];
984 }
985 }
Yiwei Zhanga1e93fd2021-04-30 07:01:55 +0000986 *format_modifier = res_info.format_modifier;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700987
988 return 0;
989}
990
Gurchetan Singhbbde01e2021-02-17 08:54:28 -0800991const struct backend virtgpu_virgl = { .name = "virtgpu_virgl",
992 .init = virgl_init,
993 .close = virgl_close,
994 .bo_create = virgl_bo_create,
995 .bo_destroy = virgl_bo_destroy,
996 .bo_import = drv_prime_bo_import,
997 .bo_map = virgl_bo_map,
998 .bo_unmap = drv_bo_munmap,
999 .bo_invalidate = virgl_bo_invalidate,
1000 .bo_flush = virgl_bo_flush,
1001 .resolve_format = virgl_resolve_format,
1002 .resource_info = virgl_resource_info };