Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 7 | #include <assert.h> |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 8 | #include <errno.h> |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 9 | #include <stdatomic.h> |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 10 | #include <stdint.h> |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <sys/mman.h> |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 13 | #include <xf86drm.h> |
| 14 | |
| 15 | #include "drv_priv.h" |
Gurchetan Singh | 9f3110b | 2020-04-03 15:15:30 -0700 | [diff] [blame] | 16 | #include "external/virgl_hw.h" |
| 17 | #include "external/virgl_protocol.h" |
| 18 | #include "external/virtgpu_drm.h" |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 19 | #include "helpers.h" |
| 20 | #include "util.h" |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 21 | #include "virtgpu.h" |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 22 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 23 | #define PIPE_TEXTURE_2D 2 |
| 24 | |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 25 | #define MESA_LLVMPIPE_TILE_ORDER 6 |
| 26 | #define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER) |
| 27 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 28 | static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 29 | DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888, |
| 30 | DRM_FORMAT_XRGB8888 }; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 31 | |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 32 | static 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 Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 36 | |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 37 | static 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 40 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 41 | extern struct virtgpu_param params[]; |
| 42 | |
| 43 | struct virgl_priv { |
Lepton Wu | eebce65 | 2020-02-26 15:13:34 -0800 | [diff] [blame] | 44 | int caps_is_v2; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 45 | union virgl_caps caps; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 46 | int host_gbm_enabled; |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 47 | atomic_int next_blob_id; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 50 | static uint32_t translate_format(uint32_t drm_fourcc) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 51 | { |
| 52 | switch (drm_fourcc) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 53 | case DRM_FORMAT_BGR888: |
| 54 | case DRM_FORMAT_RGB888: |
| 55 | return VIRGL_FORMAT_R8G8B8_UNORM; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 56 | 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 Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 64 | case DRM_FORMAT_ABGR16161616F: |
Lepton Wu | fef113c | 2020-10-30 16:29:26 -0700 | [diff] [blame] | 65 | return VIRGL_FORMAT_R16G16B16A16_FLOAT; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 66 | case DRM_FORMAT_RGB565: |
| 67 | return VIRGL_FORMAT_B5G6R5_UNORM; |
| 68 | case DRM_FORMAT_R8: |
| 69 | return VIRGL_FORMAT_R8_UNORM; |
Jason Macnak | 6e200ea | 2021-02-11 19:34:57 -0800 | [diff] [blame] | 70 | case DRM_FORMAT_R16: |
| 71 | return VIRGL_FORMAT_R16_UNORM; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 72 | case DRM_FORMAT_RG88: |
| 73 | return VIRGL_FORMAT_R8G8_UNORM; |
Gurchetan Singh | f5d280d | 2019-06-04 19:43:41 -0700 | [diff] [blame] | 74 | case DRM_FORMAT_NV12: |
| 75 | return VIRGL_FORMAT_NV12; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 76 | case DRM_FORMAT_NV21: |
| 77 | return VIRGL_FORMAT_NV21; |
Gurchetan Singh | f5d280d | 2019-06-04 19:43:41 -0700 | [diff] [blame] | 78 | case DRM_FORMAT_YVU420: |
| 79 | case DRM_FORMAT_YVU420_ANDROID: |
| 80 | return VIRGL_FORMAT_YV12; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 81 | default: |
Jason Macnak | 6e200ea | 2021-02-11 19:34:57 -0800 | [diff] [blame] | 82 | drv_log("Unhandled format:%d\n", drm_fourcc); |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | } |
| 86 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 87 | static bool virgl_bitmask_supports_format(struct virgl_supported_format_mask *supported, |
| 88 | uint32_t drm_format) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 89 | { |
| 90 | uint32_t virgl_format = translate_format(drm_format); |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 91 | if (!virgl_format) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 92 | return false; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 93 | |
| 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 Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 99 | // 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 Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 140 | static void virgl_get_emulated_metadata(const struct bo *bo, struct bo_metadata *metadata) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 141 | { |
| 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 | |
| 205 | struct virtio_transfers_params { |
| 206 | size_t xfers_needed; |
| 207 | struct rectangle xfer_boxes[DRV_MAX_PLANES]; |
| 208 | }; |
| 209 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 210 | static void virgl_get_emulated_transfers_params(const struct bo *bo, |
| 211 | const struct rectangle *transfer_box, |
| 212 | struct virtio_transfers_params *xfer_params) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 213 | { |
| 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 Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 220 | virgl_get_emulated_metadata(bo, &emulated_metadata); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 221 | |
| 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 Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 283 | static bool virgl_supports_combination_natively(struct driver *drv, uint32_t drm_format, |
| 284 | uint64_t use_flags) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 285 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 286 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 287 | |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 288 | if (priv->caps.max_version == 0) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 289 | return true; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 290 | |
| 291 | if ((use_flags & BO_USE_RENDERING) && |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 292 | !virgl_bitmask_supports_format(&priv->caps.v1.render, drm_format)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 293 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 294 | |
| 295 | if ((use_flags & BO_USE_TEXTURE) && |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 296 | !virgl_bitmask_supports_format(&priv->caps.v1.sampler, drm_format)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 297 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 298 | |
| 299 | if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 && |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 300 | !virgl_bitmask_supports_format(&priv->caps.v2.scanout, drm_format)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 301 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 302 | |
| 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 Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 309 | static bool virgl_supports_combination_through_emulation(struct driver *drv, uint32_t drm_format, |
| 310 | uint64_t use_flags) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 311 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 312 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 313 | |
| 314 | // Only enable emulation on non-gbm virtio backends. |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 315 | if (priv->host_gbm_enabled) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 316 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 317 | |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 318 | if (use_flags & (BO_USE_RENDERING | BO_USE_SCANOUT)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 319 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 320 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 321 | if (!virgl_supports_combination_natively(drv, DRM_FORMAT_R8, use_flags)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 322 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 323 | |
| 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 Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 328 | // Adds the given buffer combination to the list of supported buffer combinations if the |
| 329 | // combination is supported by the virtio backend. |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 330 | static void virgl_add_combination(struct driver *drv, uint32_t drm_format, |
| 331 | struct format_metadata *metadata, uint64_t use_flags) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 332 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 333 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 334 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 335 | if (params[param_3d].value && priv->caps.max_version >= 1) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 336 | if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 && |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 337 | !virgl_supports_combination_natively(drv, drm_format, use_flags)) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 338 | drv_log("Scanout format: %d\n", drm_format); |
| 339 | use_flags &= ~BO_USE_SCANOUT; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 342 | if (!virgl_supports_combination_natively(drv, drm_format, use_flags) && |
| 343 | !virgl_supports_combination_through_emulation(drv, drm_format, use_flags)) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 344 | drv_log("Skipping unsupported combination format:%d\n", drm_format); |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 345 | 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 Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 354 | static 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 Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 357 | { |
| 358 | uint32_t i; |
| 359 | |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 360 | for (i = 0; i < num_formats; i++) |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 361 | virgl_add_combination(drv, drm_formats[i], metadata, use_flags); |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 364 | static int virtio_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 365 | uint64_t use_flags) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 366 | { |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 367 | if (bo->meta.format != DRM_FORMAT_R8) { |
Keiichi Watanabe | a13dda7 | 2018-08-02 22:45:05 +0900 | [diff] [blame] | 368 | width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE); |
| 369 | height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE); |
| 370 | } |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 371 | |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 372 | return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_DUMB32BPP); |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 375 | static 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 Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 384 | static uint32_t compute_virgl_bind_flags(uint64_t use_flags, uint32_t format) |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 385 | { |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 386 | /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */ |
| 387 | uint32_t bind = VIRGL_BIND_SHARED; |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 388 | |
| 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 Stevens | 55a6cf9 | 2019-09-03 10:45:33 +0900 | [diff] [blame] | 392 | 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 Stevens | 23de4e2 | 2020-05-15 14:15:35 +0900 | [diff] [blame] | 395 | 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 Stevens | 55a6cf9 | 2019-09-03 10:45:33 +0900 | [diff] [blame] | 415 | |
David Stevens | 23de4e2 | 2020-05-15 14:15:35 +0900 | [diff] [blame] | 416 | 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 Stevens | 55a6cf9 | 2019-09-03 10:45:33 +0900 | [diff] [blame] | 422 | |
David Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 423 | /* |
| 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 Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 428 | if (format == DRM_FORMAT_YVU420_ANDROID) |
David Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 429 | bind |= VIRGL_BIND_LINEAR; |
David Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 430 | |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 431 | if (use_flags) |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 432 | drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags); |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 433 | |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 434 | return bind; |
| 435 | } |
| 436 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 437 | static int virgl_3d_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 438 | uint64_t use_flags) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 439 | { |
| 440 | int ret; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 441 | size_t i; |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 442 | uint32_t stride; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 443 | struct drm_virtgpu_resource_create res_create = { 0 }; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 444 | struct bo_metadata emulated_metadata; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 445 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 446 | if (virgl_supports_combination_natively(bo->drv, format, use_flags)) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 447 | stride = drv_stride_from_format(format, width, 0); |
| 448 | drv_bo_from_format(bo, stride, height, format); |
| 449 | } else { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 450 | assert(virgl_supports_combination_through_emulation(bo->drv, format, use_flags)); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 451 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 452 | virgl_get_emulated_metadata(bo, &emulated_metadata); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 453 | |
| 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 464 | |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 465 | /* |
| 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 472 | |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 473 | res_create.target = PIPE_TEXTURE_2D; |
| 474 | res_create.format = translate_format(format); |
David Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 475 | res_create.bind = compute_virgl_bind_flags(use_flags, format); |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 476 | res_create.width = width; |
| 477 | res_create.height = height; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 478 | |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 479 | /* 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 Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 485 | res_create.size = ALIGN(bo->meta.total_size, PAGE_SIZE); // PAGE_SIZE = 0x1000 |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 486 | 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 492 | for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 493 | bo->handles[plane].u32 = res_create.bo_handle; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 494 | |
| 495 | return 0; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 498 | static void *virgl_3d_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 499 | { |
| 500 | int ret; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 501 | struct drm_virtgpu_map gem_map = { 0 }; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 502 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 503 | gem_map.handle = bo->handles[0].u32; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 504 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map); |
| 505 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 506 | drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno)); |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 507 | return MAP_FAILED; |
| 508 | } |
| 509 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 510 | 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 512 | gem_map.offset); |
| 513 | } |
| 514 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 515 | static int virgl_get_caps(struct driver *drv, union virgl_caps *caps, int *caps_is_v2) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 516 | { |
| 517 | int ret; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 518 | struct drm_virtgpu_get_caps cap_args = { 0 }; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 519 | |
Lepton Wu | eebce65 | 2020-02-26 15:13:34 -0800 | [diff] [blame] | 520 | *caps_is_v2 = 0; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 521 | cap_args.addr = (unsigned long long)caps; |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 522 | if (params[param_capset_fix].value) { |
Lepton Wu | eebce65 | 2020-02-26 15:13:34 -0800 | [diff] [blame] | 523 | *caps_is_v2 = 1; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 524 | 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 Wu | eebce65 | 2020-02-26 15:13:34 -0800 | [diff] [blame] | 534 | *caps_is_v2 = 0; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 535 | |
| 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 Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 541 | if (ret) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 542 | drv_log("DRM_IOCTL_VIRTGPU_GET_CAPS failed with %s\n", strerror(errno)); |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | return ret; |
| 546 | } |
| 547 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 548 | static void virgl_init_params_and_caps(struct driver *drv) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 549 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 550 | 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 Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 553 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 554 | // We use two criteria to determine whether host minigbm is used on the host for |
| 555 | // swapchain allocations. |
| 556 | // |
Gurchetan Singh | bbde01e | 2021-02-17 08:54:28 -0800 | [diff] [blame] | 557 | // (1) Host minigbm is only available via virglrenderer, and only virglrenderer |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 558 | // 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 Singh | bbde01e | 2021-02-17 08:54:28 -0800 | [diff] [blame] | 561 | priv->host_gbm_enabled = |
| 562 | priv->caps.max_version > 0 && |
| 563 | virgl_supports_combination_natively(drv, DRM_FORMAT_NV12, BO_USE_TEXTURE); |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 564 | } |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 567 | static int virgl_init(struct driver *drv) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 568 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 569 | struct virgl_priv *priv; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 570 | |
| 571 | priv = calloc(1, sizeof(*priv)); |
| 572 | drv->priv = priv; |
| 573 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 574 | virgl_init_params_and_caps(drv); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 575 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 576 | if (params[param_3d].value) { |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 577 | /* This doesn't mean host can scanout everything, it just means host |
| 578 | * hypervisor can show it. */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 579 | 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 Singh | 3f3e5f9 | 2019-07-08 09:50:01 -0700 | [diff] [blame] | 585 | } else { |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 586 | /* Virtio primary plane only allows this format. */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 587 | virgl_add_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA, |
| 588 | BO_USE_RENDER_MASK | BO_USE_SCANOUT); |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 589 | /* Virtio cursor plane only allows this format and Chrome cannot live without |
| 590 | * ARGB888 renderable format. */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 591 | virgl_add_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA, |
| 592 | BO_USE_RENDER_MASK | BO_USE_CURSOR); |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 593 | /* Android needs more, but they cannot be bound as scanouts anymore after |
| 594 | * "drm/virtio: fix DRM_FORMAT_* handling" */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 595 | 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 Singh | 3f3e5f9 | 2019-07-08 09:50:01 -0700 | [diff] [blame] | 605 | } |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 606 | |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 607 | /* Android CTS tests require this. */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 608 | 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 Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 611 | BO_USE_SW_MASK | BO_USE_TEXTURE_MASK); |
Jason Macnak | b505a50 | 2021-06-08 06:39:53 -0700 | [diff] [blame] | 612 | virgl_add_combination(drv, DRM_FORMAT_ABGR2101010, &LINEAR_METADATA, |
| 613 | BO_USE_SW_MASK | BO_USE_TEXTURE_MASK); |
Jason Macnak | cab3966 | 2021-05-25 12:59:35 -0700 | [diff] [blame] | 614 | virgl_add_combination(drv, DRM_FORMAT_P010, &LINEAR_METADATA, |
Emilian Peev | a32d973 | 2021-01-21 11:35:27 -0800 | [diff] [blame] | 615 | BO_USE_SW_MASK | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE); |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 616 | |
David Stevens | 9f7897f | 2019-08-09 20:20:23 +0900 | [diff] [blame] | 617 | drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA, |
Hirokazu Honda | 20e4a93 | 2019-12-06 15:21:45 +0900 | [diff] [blame] | 618 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER | |
| 619 | BO_USE_HW_VIDEO_ENCODER); |
Keiichi Watanabe | a13dda7 | 2018-08-02 22:45:05 +0900 | [diff] [blame] | 620 | drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA, |
David Staessens | 04b7e24 | 2020-05-28 15:47:15 +0900 | [diff] [blame] | 621 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER | |
| 622 | BO_USE_HW_VIDEO_ENCODER); |
David Stevens | 519978f | 2020-12-11 14:09:56 +0900 | [diff] [blame] | 623 | |
| 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 Watanabe | a13dda7 | 2018-08-02 22:45:05 +0900 | [diff] [blame] | 644 | |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 645 | return drv_modify_linear_combinations(drv); |
| 646 | } |
| 647 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 648 | static void virgl_close(struct driver *drv) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 649 | { |
| 650 | free(drv->priv); |
| 651 | drv->priv = NULL; |
| 652 | } |
| 653 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 654 | static int virgl_bo_create_blob(struct driver *drv, struct bo *bo) |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 655 | { |
| 656 | int ret; |
| 657 | uint32_t stride; |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 658 | uint32_t cur_blob_id; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 659 | uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 }; |
| 660 | struct drm_virtgpu_resource_create_blob drm_rc_blob = { 0 }; |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 661 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 662 | |
David Stevens | d3f07bd | 2020-09-25 18:52:26 +0900 | [diff] [blame] | 663 | 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 Stevens | b42624c | 2020-09-10 10:50:26 +0900 | [diff] [blame] | 667 | blob_flags |= VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE; |
David Stevens | b42624c | 2020-09-10 10:50:26 +0900 | [diff] [blame] | 668 | |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 669 | cur_blob_id = atomic_fetch_add(&priv->next_blob_id, 1); |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 670 | 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 Stevens | b42624c | 2020-09-10 10:50:26 +0900 | [diff] [blame] | 673 | bo->meta.tiling = blob_flags; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 674 | |
| 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 Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 680 | cmd[VIRGL_PIPE_RES_CREATE_BIND] = |
| 681 | compute_virgl_bind_flags(bo->meta.use_flags, bo->meta.format); |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 682 | cmd[VIRGL_PIPE_RES_CREATE_DEPTH] = 1; |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 683 | cmd[VIRGL_PIPE_RES_CREATE_BLOB_ID] = cur_blob_id; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 684 | |
| 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 Stevens | b42624c | 2020-09-10 10:50:26 +0900 | [diff] [blame] | 689 | drm_rc_blob.blob_flags = blob_flags; |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 690 | drm_rc_blob.blob_id = cur_blob_id; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 691 | |
| 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 | |
| 704 | static bool should_use_blob(struct driver *drv, uint32_t format, uint64_t use_flags) |
| 705 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 706 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 707 | |
| 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 Stevens | d3f07bd | 2020-09-25 18:52:26 +0900 | [diff] [blame] | 717 | // 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 Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 720 | return false; |
| 721 | |
David Stevens | d3f07bd | 2020-09-25 18:52:26 +0900 | [diff] [blame] | 722 | switch (format) { |
| 723 | case DRM_FORMAT_YVU420_ANDROID: |
| 724 | case DRM_FORMAT_R8: |
| 725 | // Formats with strictly defined strides are supported |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 726 | return true; |
David Stevens | d3f07bd | 2020-09-25 18:52:26 +0900 | [diff] [blame] | 727 | 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 Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 736 | static int virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 737 | uint64_t use_flags) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 738 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 739 | if (params[param_resource_blob].value && params[param_host_visible].value && |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 740 | should_use_blob(bo->drv, format, use_flags)) |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 741 | return virgl_bo_create_blob(bo->drv, bo); |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 742 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 743 | if (params[param_3d].value) |
| 744 | return virgl_3d_bo_create(bo, width, height, format, use_flags); |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 745 | else |
| 746 | return virtio_dumb_bo_create(bo, width, height, format, use_flags); |
| 747 | } |
| 748 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 749 | static int virgl_bo_destroy(struct bo *bo) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 750 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 751 | if (params[param_3d].value) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 752 | return drv_gem_bo_destroy(bo); |
| 753 | else |
| 754 | return drv_dumb_bo_destroy(bo); |
| 755 | } |
| 756 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 757 | static void *virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 758 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 759 | if (params[param_3d].value) |
| 760 | return virgl_3d_bo_map(bo, vma, plane, map_flags); |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 761 | else |
| 762 | return drv_dumb_bo_map(bo, vma, plane, map_flags); |
| 763 | } |
| 764 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 765 | static int virgl_bo_invalidate(struct bo *bo, struct mapping *mapping) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 766 | { |
| 767 | int ret; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 768 | size_t i; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 769 | struct drm_virtgpu_3d_transfer_from_host xfer = { 0 }; |
| 770 | struct drm_virtgpu_3d_wait waitcmd = { 0 }; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 771 | struct virtio_transfers_params xfer_params; |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 772 | struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv; |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 773 | uint64_t host_write_flags; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 774 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 775 | if (!params[param_3d].value) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 776 | return 0; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 777 | |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 778 | // 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 Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 782 | if (bo->meta.format == DRM_FORMAT_R8) |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 783 | host_write_flags |= BO_USE_HW_VIDEO_ENCODER; |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 784 | else |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 785 | host_write_flags |= BO_USE_HW_VIDEO_DECODER; |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 786 | |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 787 | if ((bo->meta.use_flags & host_write_flags) == 0) |
David Stevens | 4d5358d | 2019-10-24 14:59:31 +0900 | [diff] [blame] | 788 | return 0; |
| 789 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 790 | if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE)) |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 791 | return 0; |
| 792 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 793 | xfer.bo_handle = mapping->vma->handle; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 794 | |
Gurchetan Singh | 1b57fe2 | 2020-05-05 09:18:22 -0700 | [diff] [blame] | 795 | if (mapping->rect.x || mapping->rect.y) { |
Gurchetan Singh | 1b57fe2 | 2020-05-05 09:18:22 -0700 | [diff] [blame] | 796 | /* |
| 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 Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 807 | if ((bo->meta.use_flags & BO_USE_RENDERING) == 0) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 808 | // Unfortunately, the kernel doesn't actually pass the guest layer_stride |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 809 | // and guest stride to the host (compare virgl.h and virtgpu_drm.h). |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 810 | // 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 Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 814 | // TODO(b/145993887): Send also stride when the patches are landed |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 815 | if (priv->host_gbm_enabled) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 816 | xfer.level = bo->meta.strides[0]; |
David Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 817 | } |
Gurchetan Singh | 05e67cc | 2019-06-28 17:21:40 -0700 | [diff] [blame] | 818 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 819 | if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 820 | xfer_params.xfers_needed = 1; |
| 821 | xfer_params.xfer_boxes[0] = mapping->rect; |
| 822 | } else { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 823 | assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format, |
| 824 | bo->meta.use_flags)); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 825 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 826 | virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 827 | } |
| 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 842 | } |
| 843 | |
David Stevens | 4d5358d | 2019-10-24 14:59:31 +0900 | [diff] [blame] | 844 | // 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 Stevens | 4d5358d | 2019-10-24 14:59:31 +0900 | [diff] [blame] | 847 | 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 857 | static int virgl_bo_flush(struct bo *bo, struct mapping *mapping) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 858 | { |
| 859 | int ret; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 860 | size_t i; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 861 | struct drm_virtgpu_3d_transfer_to_host xfer = { 0 }; |
| 862 | struct drm_virtgpu_3d_wait waitcmd = { 0 }; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 863 | struct virtio_transfers_params xfer_params; |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 864 | struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 865 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 866 | if (!params[param_3d].value) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 867 | return 0; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 868 | |
| 869 | if (!(mapping->vma->map_flags & BO_MAP_WRITE)) |
| 870 | return 0; |
| 871 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 872 | if (params[param_resource_blob].value && (bo->meta.tiling & VIRTGPU_BLOB_FLAG_USE_MAPPABLE)) |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 873 | return 0; |
| 874 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 875 | xfer.bo_handle = mapping->vma->handle; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 876 | |
Gurchetan Singh | 1b57fe2 | 2020-05-05 09:18:22 -0700 | [diff] [blame] | 877 | if (mapping->rect.x || mapping->rect.y) { |
Gurchetan Singh | 1b57fe2 | 2020-05-05 09:18:22 -0700 | [diff] [blame] | 878 | /* |
| 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 Singh | 05e67cc | 2019-06-28 17:21:40 -0700 | [diff] [blame] | 889 | // Unfortunately, the kernel doesn't actually pass the guest layer_stride and |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 890 | // guest stride to the host (compare virgl.h and virtgpu_drm.h). We can use |
Gurchetan Singh | 05e67cc | 2019-06-28 17:21:40 -0700 | [diff] [blame] | 891 | // the level to work around this. |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 892 | if (priv->host_gbm_enabled) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 893 | xfer.level = bo->meta.strides[0]; |
Gurchetan Singh | 05e67cc | 2019-06-28 17:21:40 -0700 | [diff] [blame] | 894 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 895 | if (virgl_supports_combination_natively(bo->drv, bo->meta.format, bo->meta.use_flags)) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 896 | xfer_params.xfers_needed = 1; |
| 897 | xfer_params.xfer_boxes[0] = mapping->rect; |
| 898 | } else { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 899 | assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format, |
| 900 | bo->meta.use_flags)); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 901 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 902 | virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 903 | } |
| 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 918 | } |
| 919 | |
David Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 920 | // 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 Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 925 | 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 Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 934 | return 0; |
| 935 | } |
| 936 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 937 | static uint32_t virgl_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 938 | { |
| 939 | switch (format) { |
| 940 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Keiichi Watanabe | a13dda7 | 2018-08-02 22:45:05 +0900 | [diff] [blame] | 941 | /* Camera subsystem requires NV12. */ |
| 942 | if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) |
| 943 | return DRM_FORMAT_NV12; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 944 | /*HACK: See b/28671744 */ |
| 945 | return DRM_FORMAT_XBGR8888; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 946 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
Gurchetan Singh | f5d280d | 2019-06-04 19:43:41 -0700 | [diff] [blame] | 947 | /* |
| 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 Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 951 | if (params[param_3d].value) |
Gurchetan Singh | f5d280d | 2019-06-04 19:43:41 -0700 | [diff] [blame] | 952 | return DRM_FORMAT_NV12; |
| 953 | else |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 954 | return DRM_FORMAT_YVU420_ANDROID; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 955 | default: |
| 956 | return format; |
| 957 | } |
| 958 | } |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 959 | static int virgl_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES], |
Yiwei Zhang | a1e93fd | 2021-04-30 07:01:55 +0000 | [diff] [blame] | 960 | uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier) |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 961 | { |
| 962 | int ret; |
Chia-I Wu | 2e41f63 | 2021-01-11 11:08:21 -0800 | [diff] [blame] | 963 | struct drm_virtgpu_resource_info_cros res_info = { 0 }; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 964 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 965 | if (!params[param_3d].value) |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 966 | return 0; |
| 967 | |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 968 | res_info.bo_handle = bo->handles[0].u32; |
Chia-I Wu | 5085562 | 2021-01-12 12:38:09 -0800 | [diff] [blame] | 969 | res_info.type = VIRTGPU_RESOURCE_INFO_TYPE_EXTENDED; |
Chia-I Wu | 2e41f63 | 2021-01-11 11:08:21 -0800 | [diff] [blame] | 970 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_INFO_CROS, &res_info); |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 971 | 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 Zhang | a1e93fd | 2021-04-30 07:01:55 +0000 | [diff] [blame] | 986 | *format_modifier = res_info.format_modifier; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
Gurchetan Singh | bbde01e | 2021-02-17 08:54:28 -0800 | [diff] [blame] | 991 | const 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 }; |