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 | |
Yiwei Zhang | b7a6444 | 2021-09-30 05:13:10 +0000 | [diff] [blame] | 15 | #include "drv_helpers.h" |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 16 | #include "drv_priv.h" |
Gurchetan Singh | 9f3110b | 2020-04-03 15:15:30 -0700 | [diff] [blame] | 17 | #include "external/virgl_hw.h" |
| 18 | #include "external/virgl_protocol.h" |
| 19 | #include "external/virtgpu_drm.h" |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 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 | |
Jason Macnak | d6666c8 | 2021-09-29 11:13:25 -0700 | [diff] [blame] | 25 | #define MESA_LLVMPIPE_MAX_TEXTURE_2D_LEVELS 15 |
| 26 | #define MESA_LLVMPIPE_MAX_TEXTURE_2D_SIZE (1 << (MESA_LLVMPIPE_MAX_TEXTURE_2D_LEVELS - 1)) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 27 | #define MESA_LLVMPIPE_TILE_ORDER 6 |
| 28 | #define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER) |
| 29 | |
Jason Macnak | c06cc9c | 2021-10-06 10:16:19 -0700 | [diff] [blame] | 30 | // This comes from a combination of SwiftShader's VkPhysicalDeviceLimits::maxFramebufferWidth and |
| 31 | // VkPhysicalDeviceLimits::maxImageDimension2D (see https://crrev.com/c/1917130). |
| 32 | #define ANGLE_ON_SWIFTSHADER_MAX_TEXTURE_2D_SIZE 8192 |
| 33 | |
| 34 | #ifndef MIN |
| 35 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 36 | #endif |
| 37 | #define VIRGL_2D_MAX_TEXTURE_2D_SIZE \ |
| 38 | MIN(ANGLE_ON_SWIFTSHADER_MAX_TEXTURE_2D_SIZE, MESA_LLVMPIPE_MAX_TEXTURE_2D_SIZE) |
| 39 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 40 | 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] | 41 | DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888, |
| 42 | DRM_FORMAT_XRGB8888 }; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 43 | |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 44 | static const uint32_t dumb_texture_source_formats[] = { |
Yiwei Zhang | 35aa91b | 2021-09-17 22:14:11 +0000 | [diff] [blame] | 45 | DRM_FORMAT_R8, DRM_FORMAT_R16, DRM_FORMAT_YVU420, |
| 46 | DRM_FORMAT_NV12, DRM_FORMAT_NV21, DRM_FORMAT_YVU420_ANDROID, |
| 47 | DRM_FORMAT_ABGR2101010, DRM_FORMAT_ABGR16161616F |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 48 | }; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 49 | |
Yiwei Zhang | 35aa91b | 2021-09-17 22:14:11 +0000 | [diff] [blame] | 50 | static const uint32_t texture_source_formats[] = { |
Robert Tarasov | 9b27657 | 2022-06-01 20:52:18 +0000 | [diff] [blame] | 51 | DRM_FORMAT_NV12, DRM_FORMAT_NV21, DRM_FORMAT_R8, |
| 52 | DRM_FORMAT_R16, DRM_FORMAT_RG88, DRM_FORMAT_YVU420_ANDROID, |
| 53 | DRM_FORMAT_ABGR2101010, DRM_FORMAT_ABGR16161616F |
Yiwei Zhang | 35aa91b | 2021-09-17 22:14:11 +0000 | [diff] [blame] | 54 | }; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 55 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 56 | extern struct virtgpu_param params[]; |
| 57 | |
| 58 | struct virgl_priv { |
Lepton Wu | eebce65 | 2020-02-26 15:13:34 -0800 | [diff] [blame] | 59 | int caps_is_v2; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 60 | union virgl_caps caps; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 61 | int host_gbm_enabled; |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 62 | atomic_int next_blob_id; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 65 | static uint32_t translate_format(uint32_t drm_fourcc) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 66 | { |
| 67 | switch (drm_fourcc) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 68 | case DRM_FORMAT_BGR888: |
| 69 | case DRM_FORMAT_RGB888: |
| 70 | return VIRGL_FORMAT_R8G8B8_UNORM; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 71 | case DRM_FORMAT_XRGB8888: |
| 72 | return VIRGL_FORMAT_B8G8R8X8_UNORM; |
| 73 | case DRM_FORMAT_ARGB8888: |
| 74 | return VIRGL_FORMAT_B8G8R8A8_UNORM; |
| 75 | case DRM_FORMAT_XBGR8888: |
| 76 | return VIRGL_FORMAT_R8G8B8X8_UNORM; |
| 77 | case DRM_FORMAT_ABGR8888: |
| 78 | return VIRGL_FORMAT_R8G8B8A8_UNORM; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 79 | case DRM_FORMAT_ABGR16161616F: |
Lepton Wu | fef113c | 2020-10-30 16:29:26 -0700 | [diff] [blame] | 80 | return VIRGL_FORMAT_R16G16B16A16_FLOAT; |
Nataraj Deshpande | 450e576 | 2021-06-30 12:10:55 -0700 | [diff] [blame] | 81 | case DRM_FORMAT_ABGR2101010: |
| 82 | return VIRGL_FORMAT_R10G10B10A2_UNORM; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 83 | case DRM_FORMAT_RGB565: |
| 84 | return VIRGL_FORMAT_B5G6R5_UNORM; |
| 85 | case DRM_FORMAT_R8: |
| 86 | return VIRGL_FORMAT_R8_UNORM; |
Jason Macnak | 6e200ea | 2021-02-11 19:34:57 -0800 | [diff] [blame] | 87 | case DRM_FORMAT_R16: |
| 88 | return VIRGL_FORMAT_R16_UNORM; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 89 | case DRM_FORMAT_RG88: |
| 90 | return VIRGL_FORMAT_R8G8_UNORM; |
Gurchetan Singh | f5d280d | 2019-06-04 19:43:41 -0700 | [diff] [blame] | 91 | case DRM_FORMAT_NV12: |
| 92 | return VIRGL_FORMAT_NV12; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 93 | case DRM_FORMAT_NV21: |
| 94 | return VIRGL_FORMAT_NV21; |
Gurchetan Singh | f5d280d | 2019-06-04 19:43:41 -0700 | [diff] [blame] | 95 | case DRM_FORMAT_YVU420: |
| 96 | case DRM_FORMAT_YVU420_ANDROID: |
| 97 | return VIRGL_FORMAT_YV12; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 98 | default: |
Jason Macnak | 6e200ea | 2021-02-11 19:34:57 -0800 | [diff] [blame] | 99 | drv_log("Unhandled format:%d\n", drm_fourcc); |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | } |
| 103 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 104 | static bool virgl_bitmask_supports_format(struct virgl_supported_format_mask *supported, |
| 105 | uint32_t drm_format) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 106 | { |
| 107 | uint32_t virgl_format = translate_format(drm_format); |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 108 | if (!virgl_format) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 109 | return false; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 110 | |
| 111 | uint32_t bitmask_index = virgl_format / 32; |
| 112 | uint32_t bit_index = virgl_format % 32; |
| 113 | return supported->bitmask[bitmask_index] & (1 << bit_index); |
| 114 | } |
| 115 | |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 116 | // The metadata generated here for emulated buffers is slightly different than the metadata |
| 117 | // generated by drv_bo_from_format. In order to simplify transfers in the flush and invalidate |
| 118 | // functions below, the emulated buffers are oversized. For example, ignoring stride alignment |
| 119 | // requirements to demonstrate, a 6x6 YUV420 image buffer might have the following layout from |
| 120 | // drv_bo_from_format: |
| 121 | // |
| 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 | // | Y | Y | Y | Y | Y | Y | |
| 128 | // | U | U | U | U | U | U | |
| 129 | // | U | U | U | V | V | V | |
| 130 | // | V | V | V | V | V | V | |
| 131 | // |
| 132 | // where each plane immediately follows the previous plane in memory. This layout makes it |
| 133 | // difficult to compute the transfers needed for example when the middle 2x2 region of the |
| 134 | // image is locked and needs to be flushed/invalidated. |
| 135 | // |
| 136 | // Emulated multi-plane buffers instead have a layout of: |
| 137 | // |
| 138 | // | Y | Y | Y | Y | Y | Y | |
| 139 | // | Y | Y | Y | Y | Y | Y | |
| 140 | // | Y | Y | Y | Y | Y | Y | |
| 141 | // | Y | Y | Y | Y | Y | Y | |
| 142 | // | Y | Y | Y | Y | Y | Y | |
| 143 | // | Y | Y | Y | Y | Y | Y | |
| 144 | // | U | U | U | | | | |
| 145 | // | U | U | U | | | | |
| 146 | // | U | U | U | | | | |
| 147 | // | V | V | V | | | | |
| 148 | // | V | V | V | | | | |
| 149 | // | V | V | V | | | | |
| 150 | // |
| 151 | // where each plane is placed as a sub-image (albeit with a very large stride) in order to |
| 152 | // simplify transfers into 3 sub-image transfers for the above example. |
| 153 | // |
| 154 | // Additional note: the V-plane is not placed to the right of the U-plane due to some |
| 155 | // observed failures in media framework code which assumes the V-plane is not |
| 156 | // "row-interlaced" with the U-plane. |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 157 | 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] | 158 | { |
| 159 | uint32_t y_plane_height; |
| 160 | uint32_t c_plane_height; |
| 161 | uint32_t original_width = bo->meta.width; |
| 162 | uint32_t original_height = bo->meta.height; |
| 163 | |
| 164 | metadata->format = DRM_FORMAT_R8; |
| 165 | switch (bo->meta.format) { |
| 166 | case DRM_FORMAT_NV12: |
| 167 | case DRM_FORMAT_NV21: |
| 168 | // Bi-planar |
| 169 | metadata->num_planes = 2; |
| 170 | |
| 171 | y_plane_height = original_height; |
| 172 | c_plane_height = DIV_ROUND_UP(original_height, 2); |
| 173 | |
| 174 | metadata->width = original_width; |
| 175 | metadata->height = y_plane_height + c_plane_height; |
| 176 | |
| 177 | // Y-plane (full resolution) |
| 178 | metadata->strides[0] = metadata->width; |
| 179 | metadata->offsets[0] = 0; |
| 180 | metadata->sizes[0] = metadata->width * y_plane_height; |
| 181 | |
| 182 | // CbCr-plane (half resolution, interleaved, placed below Y-plane) |
| 183 | metadata->strides[1] = metadata->width; |
| 184 | metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0]; |
| 185 | metadata->sizes[1] = metadata->width * c_plane_height; |
| 186 | |
| 187 | metadata->total_size = metadata->width * metadata->height; |
| 188 | break; |
| 189 | case DRM_FORMAT_YVU420: |
| 190 | case DRM_FORMAT_YVU420_ANDROID: |
| 191 | // Tri-planar |
| 192 | metadata->num_planes = 3; |
| 193 | |
| 194 | y_plane_height = original_height; |
| 195 | c_plane_height = DIV_ROUND_UP(original_height, 2); |
| 196 | |
| 197 | metadata->width = ALIGN(original_width, 32); |
| 198 | metadata->height = y_plane_height + (2 * c_plane_height); |
| 199 | |
| 200 | // Y-plane (full resolution) |
| 201 | metadata->strides[0] = metadata->width; |
| 202 | metadata->offsets[0] = 0; |
| 203 | metadata->sizes[0] = metadata->width * original_height; |
| 204 | |
| 205 | // Cb-plane (half resolution, placed below Y-plane) |
| 206 | metadata->strides[1] = metadata->width; |
| 207 | metadata->offsets[1] = metadata->offsets[0] + metadata->sizes[0]; |
| 208 | metadata->sizes[1] = metadata->width * c_plane_height; |
| 209 | |
| 210 | // Cr-plane (half resolution, placed below Cb-plane) |
| 211 | metadata->strides[2] = metadata->width; |
| 212 | metadata->offsets[2] = metadata->offsets[1] + metadata->sizes[1]; |
| 213 | metadata->sizes[2] = metadata->width * c_plane_height; |
| 214 | |
| 215 | metadata->total_size = metadata->width * metadata->height; |
| 216 | break; |
| 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | struct virtio_transfers_params { |
| 223 | size_t xfers_needed; |
| 224 | struct rectangle xfer_boxes[DRV_MAX_PLANES]; |
| 225 | }; |
| 226 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 227 | static void virgl_get_emulated_transfers_params(const struct bo *bo, |
| 228 | const struct rectangle *transfer_box, |
| 229 | struct virtio_transfers_params *xfer_params) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 230 | { |
| 231 | uint32_t y_plane_height; |
| 232 | uint32_t c_plane_height; |
| 233 | struct bo_metadata emulated_metadata; |
| 234 | |
| 235 | if (transfer_box->x == 0 && transfer_box->y == 0 && transfer_box->width == bo->meta.width && |
| 236 | transfer_box->height == bo->meta.height) { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 237 | virgl_get_emulated_metadata(bo, &emulated_metadata); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 238 | |
| 239 | xfer_params->xfers_needed = 1; |
| 240 | xfer_params->xfer_boxes[0].x = 0; |
| 241 | xfer_params->xfer_boxes[0].y = 0; |
| 242 | xfer_params->xfer_boxes[0].width = emulated_metadata.width; |
| 243 | xfer_params->xfer_boxes[0].height = emulated_metadata.height; |
| 244 | |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | switch (bo->meta.format) { |
| 249 | case DRM_FORMAT_NV12: |
| 250 | case DRM_FORMAT_NV21: |
| 251 | // Bi-planar |
| 252 | xfer_params->xfers_needed = 2; |
| 253 | |
| 254 | y_plane_height = bo->meta.height; |
| 255 | c_plane_height = DIV_ROUND_UP(bo->meta.height, 2); |
| 256 | |
| 257 | // Y-plane (full resolution) |
| 258 | xfer_params->xfer_boxes[0].x = transfer_box->x; |
| 259 | xfer_params->xfer_boxes[0].y = transfer_box->y; |
| 260 | xfer_params->xfer_boxes[0].width = transfer_box->width; |
| 261 | xfer_params->xfer_boxes[0].height = transfer_box->height; |
| 262 | |
| 263 | // CbCr-plane (half resolution, interleaved, placed below Y-plane) |
| 264 | xfer_params->xfer_boxes[1].x = transfer_box->x; |
| 265 | xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height; |
| 266 | xfer_params->xfer_boxes[1].width = transfer_box->width; |
| 267 | xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2); |
| 268 | |
| 269 | break; |
| 270 | case DRM_FORMAT_YVU420: |
| 271 | case DRM_FORMAT_YVU420_ANDROID: |
| 272 | // Tri-planar |
| 273 | xfer_params->xfers_needed = 3; |
| 274 | |
| 275 | y_plane_height = bo->meta.height; |
| 276 | c_plane_height = DIV_ROUND_UP(bo->meta.height, 2); |
| 277 | |
| 278 | // Y-plane (full resolution) |
| 279 | xfer_params->xfer_boxes[0].x = transfer_box->x; |
| 280 | xfer_params->xfer_boxes[0].y = transfer_box->y; |
| 281 | xfer_params->xfer_boxes[0].width = transfer_box->width; |
| 282 | xfer_params->xfer_boxes[0].height = transfer_box->height; |
| 283 | |
| 284 | // Cb-plane (half resolution, placed below Y-plane) |
| 285 | xfer_params->xfer_boxes[1].x = transfer_box->x; |
| 286 | xfer_params->xfer_boxes[1].y = transfer_box->y + y_plane_height; |
| 287 | xfer_params->xfer_boxes[1].width = DIV_ROUND_UP(transfer_box->width, 2); |
| 288 | xfer_params->xfer_boxes[1].height = DIV_ROUND_UP(transfer_box->height, 2); |
| 289 | |
| 290 | // Cr-plane (half resolution, placed below Cb-plane) |
| 291 | xfer_params->xfer_boxes[2].x = transfer_box->x; |
| 292 | xfer_params->xfer_boxes[2].y = transfer_box->y + y_plane_height + c_plane_height; |
| 293 | xfer_params->xfer_boxes[2].width = DIV_ROUND_UP(transfer_box->width, 2); |
| 294 | xfer_params->xfer_boxes[2].height = DIV_ROUND_UP(transfer_box->height, 2); |
| 295 | |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 300 | static bool virgl_supports_combination_natively(struct driver *drv, uint32_t drm_format, |
| 301 | uint64_t use_flags) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 302 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 303 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 304 | |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 305 | if (priv->caps.max_version == 0) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 306 | return true; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 307 | |
| 308 | if ((use_flags & BO_USE_RENDERING) && |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 309 | !virgl_bitmask_supports_format(&priv->caps.v1.render, drm_format)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 310 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 311 | |
| 312 | if ((use_flags & BO_USE_TEXTURE) && |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 313 | !virgl_bitmask_supports_format(&priv->caps.v1.sampler, drm_format)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 314 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 315 | |
| 316 | if ((use_flags & BO_USE_SCANOUT) && priv->caps_is_v2 && |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 317 | !virgl_bitmask_supports_format(&priv->caps.v2.scanout, drm_format)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 318 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 319 | |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | // For virtio backends that do not support formats natively (e.g. multi-planar formats are not |
| 324 | // supported in virglrenderer when gbm is unavailable on the host machine), whether or not the |
| 325 | // format and usage combination can be handled as a blob (byte buffer). |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 326 | static bool virgl_supports_combination_through_emulation(struct driver *drv, uint32_t drm_format, |
| 327 | uint64_t use_flags) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 328 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 329 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 330 | |
| 331 | // Only enable emulation on non-gbm virtio backends. |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 332 | if (priv->host_gbm_enabled) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 333 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 334 | |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 335 | if (use_flags & (BO_USE_RENDERING | BO_USE_SCANOUT)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 336 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 337 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 338 | if (!virgl_supports_combination_natively(drv, DRM_FORMAT_R8, use_flags)) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 339 | return false; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 340 | |
| 341 | return drm_format == DRM_FORMAT_NV12 || drm_format == DRM_FORMAT_NV21 || |
| 342 | drm_format == DRM_FORMAT_YVU420 || drm_format == DRM_FORMAT_YVU420_ANDROID; |
| 343 | } |
| 344 | |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 345 | // Adds the given buffer combination to the list of supported buffer combinations if the |
| 346 | // combination is supported by the virtio backend. |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 347 | static void virgl_add_combination(struct driver *drv, uint32_t drm_format, |
| 348 | struct format_metadata *metadata, uint64_t use_flags) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 349 | { |
Yiwei Zhang | 9420ffe | 2021-09-24 06:24:30 +0000 | [diff] [blame] | 350 | if (params[param_3d].value) { |
| 351 | if ((use_flags & BO_USE_SCANOUT) && |
| 352 | !virgl_supports_combination_natively(drv, drm_format, BO_USE_SCANOUT)) { |
| 353 | drv_log("Strip scanout on format: %d\n", drm_format); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 354 | use_flags &= ~BO_USE_SCANOUT; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 357 | if (!virgl_supports_combination_natively(drv, drm_format, use_flags) && |
| 358 | !virgl_supports_combination_through_emulation(drv, drm_format, use_flags)) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 359 | drv_log("Skipping unsupported combination format:%d\n", drm_format); |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 360 | return; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | drv_add_combination(drv, drm_format, metadata, use_flags); |
| 365 | } |
| 366 | |
| 367 | // Adds each given buffer combination to the list of supported buffer combinations if the |
| 368 | // combination supported by the virtio backend. |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 369 | static void virgl_add_combinations(struct driver *drv, const uint32_t *drm_formats, |
| 370 | uint32_t num_formats, struct format_metadata *metadata, |
| 371 | uint64_t use_flags) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 372 | { |
| 373 | uint32_t i; |
| 374 | |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 375 | for (i = 0; i < num_formats; i++) |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 376 | virgl_add_combination(drv, drm_formats[i], metadata, use_flags); |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Jason Macnak | c06cc9c | 2021-10-06 10:16:19 -0700 | [diff] [blame] | 379 | static int virgl_2d_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 380 | uint64_t use_flags) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 381 | { |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 382 | if (bo->meta.format != DRM_FORMAT_R8) { |
Keiichi Watanabe | a13dda7 | 2018-08-02 22:45:05 +0900 | [diff] [blame] | 383 | width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE); |
| 384 | height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE); |
| 385 | } |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 386 | |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 387 | 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] | 388 | } |
| 389 | |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 390 | static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind, |
| 391 | uint32_t virgl_bind) |
| 392 | { |
| 393 | if ((*flag) & check_flag) { |
| 394 | (*flag) &= ~check_flag; |
| 395 | (*bind) |= virgl_bind; |
| 396 | } |
| 397 | } |
| 398 | |
David Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 399 | 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] | 400 | { |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 401 | /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */ |
| 402 | uint32_t bind = VIRGL_BIND_SHARED; |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 403 | |
| 404 | handle_flag(&use_flags, BO_USE_TEXTURE, &bind, VIRGL_BIND_SAMPLER_VIEW); |
| 405 | handle_flag(&use_flags, BO_USE_RENDERING, &bind, VIRGL_BIND_RENDER_TARGET); |
| 406 | handle_flag(&use_flags, BO_USE_SCANOUT, &bind, VIRGL_BIND_SCANOUT); |
David Stevens | 55a6cf9 | 2019-09-03 10:45:33 +0900 | [diff] [blame] | 407 | handle_flag(&use_flags, BO_USE_CURSOR, &bind, VIRGL_BIND_CURSOR); |
| 408 | handle_flag(&use_flags, BO_USE_LINEAR, &bind, VIRGL_BIND_LINEAR); |
Yiwei Zhang | bb9d4af | 2021-06-20 19:23:38 +0000 | [diff] [blame] | 409 | handle_flag(&use_flags, BO_USE_GPU_DATA_BUFFER, &bind, VIRGL_BIND_LINEAR); |
Yiwei Zhang | d3a73ff | 2021-07-08 05:48:01 +0000 | [diff] [blame] | 410 | handle_flag(&use_flags, BO_USE_FRONT_RENDERING, &bind, VIRGL_BIND_LINEAR); |
David Stevens | 55a6cf9 | 2019-09-03 10:45:33 +0900 | [diff] [blame] | 411 | |
David Stevens | 23de4e2 | 2020-05-15 14:15:35 +0900 | [diff] [blame] | 412 | if (use_flags & BO_USE_PROTECTED) { |
| 413 | handle_flag(&use_flags, BO_USE_PROTECTED, &bind, VIRGL_BIND_MINIGBM_PROTECTED); |
| 414 | } else { |
| 415 | // Make sure we don't set both flags, since that could be mistaken for |
| 416 | // protected. Give OFTEN priority over RARELY. |
| 417 | if (use_flags & BO_USE_SW_READ_OFTEN) { |
| 418 | handle_flag(&use_flags, BO_USE_SW_READ_OFTEN, &bind, |
| 419 | VIRGL_BIND_MINIGBM_SW_READ_OFTEN); |
| 420 | } else { |
| 421 | handle_flag(&use_flags, BO_USE_SW_READ_RARELY, &bind, |
| 422 | VIRGL_BIND_MINIGBM_SW_READ_RARELY); |
| 423 | } |
| 424 | if (use_flags & BO_USE_SW_WRITE_OFTEN) { |
| 425 | handle_flag(&use_flags, BO_USE_SW_WRITE_OFTEN, &bind, |
| 426 | VIRGL_BIND_MINIGBM_SW_WRITE_OFTEN); |
| 427 | } else { |
| 428 | handle_flag(&use_flags, BO_USE_SW_WRITE_RARELY, &bind, |
| 429 | VIRGL_BIND_MINIGBM_SW_WRITE_RARELY); |
| 430 | } |
| 431 | } |
David Stevens | 55a6cf9 | 2019-09-03 10:45:33 +0900 | [diff] [blame] | 432 | |
David Stevens | 23de4e2 | 2020-05-15 14:15:35 +0900 | [diff] [blame] | 433 | handle_flag(&use_flags, BO_USE_CAMERA_WRITE, &bind, VIRGL_BIND_MINIGBM_CAMERA_WRITE); |
| 434 | handle_flag(&use_flags, BO_USE_CAMERA_READ, &bind, VIRGL_BIND_MINIGBM_CAMERA_READ); |
| 435 | handle_flag(&use_flags, BO_USE_HW_VIDEO_DECODER, &bind, |
| 436 | VIRGL_BIND_MINIGBM_HW_VIDEO_DECODER); |
| 437 | handle_flag(&use_flags, BO_USE_HW_VIDEO_ENCODER, &bind, |
| 438 | VIRGL_BIND_MINIGBM_HW_VIDEO_ENCODER); |
David Stevens | 55a6cf9 | 2019-09-03 10:45:33 +0900 | [diff] [blame] | 439 | |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 440 | if (use_flags) |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 441 | 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] | 442 | |
Lepton Wu | dbab083 | 2019-04-19 12:26:39 -0700 | [diff] [blame] | 443 | return bind; |
| 444 | } |
| 445 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 446 | static int virgl_3d_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 447 | uint64_t use_flags) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 448 | { |
| 449 | int ret; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 450 | size_t i; |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 451 | uint32_t stride; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 452 | struct drm_virtgpu_resource_create res_create = { 0 }; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 453 | struct bo_metadata emulated_metadata; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 454 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 455 | if (virgl_supports_combination_natively(bo->drv, format, use_flags)) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 456 | stride = drv_stride_from_format(format, width, 0); |
| 457 | drv_bo_from_format(bo, stride, height, format); |
| 458 | } else { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 459 | assert(virgl_supports_combination_through_emulation(bo->drv, format, use_flags)); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 460 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 461 | virgl_get_emulated_metadata(bo, &emulated_metadata); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 462 | |
| 463 | format = emulated_metadata.format; |
| 464 | width = emulated_metadata.width; |
| 465 | height = emulated_metadata.height; |
| 466 | for (i = 0; i < emulated_metadata.num_planes; i++) { |
| 467 | bo->meta.strides[i] = emulated_metadata.strides[i]; |
| 468 | bo->meta.offsets[i] = emulated_metadata.offsets[i]; |
| 469 | bo->meta.sizes[i] = emulated_metadata.sizes[i]; |
| 470 | } |
| 471 | bo->meta.total_size = emulated_metadata.total_size; |
| 472 | } |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 473 | |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 474 | /* |
| 475 | * Setting the target is intended to ensure this resource gets bound as a 2D |
| 476 | * texture in the host renderer's GL state. All of these resource properties are |
| 477 | * sent unchanged by the kernel to the host, which in turn sends them unchanged to |
| 478 | * virglrenderer. When virglrenderer makes a resource, it will convert the target |
| 479 | * enum to the equivalent one in GL and then bind the resource to that target. |
| 480 | */ |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 481 | |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 482 | res_create.target = PIPE_TEXTURE_2D; |
| 483 | res_create.format = translate_format(format); |
David Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 484 | res_create.bind = compute_virgl_bind_flags(use_flags, format); |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 485 | res_create.width = width; |
| 486 | res_create.height = height; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 487 | |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 488 | /* For virgl 3D */ |
| 489 | res_create.depth = 1; |
| 490 | res_create.array_size = 1; |
| 491 | res_create.last_level = 0; |
| 492 | res_create.nr_samples = 0; |
| 493 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 494 | 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] | 495 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create); |
| 496 | if (ret) { |
| 497 | drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno)); |
| 498 | return ret; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 501 | for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) |
Kansho Nishida | d97877b | 2019-06-14 18:28:18 +0900 | [diff] [blame] | 502 | bo->handles[plane].u32 = res_create.bo_handle; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 503 | |
| 504 | return 0; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 507 | 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] | 508 | { |
| 509 | int ret; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 510 | struct drm_virtgpu_map gem_map = { 0 }; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 511 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 512 | gem_map.handle = bo->handles[0].u32; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 513 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map); |
| 514 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 515 | drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno)); |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 516 | return MAP_FAILED; |
| 517 | } |
| 518 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 519 | vma->length = bo->meta.total_size; |
| 520 | 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] | 521 | gem_map.offset); |
| 522 | } |
| 523 | |
Jason Macnak | d6666c8 | 2021-09-29 11:13:25 -0700 | [diff] [blame] | 524 | static uint32_t virgl_3d_get_max_texture_2d_size(struct driver *drv) |
| 525 | { |
| 526 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
| 527 | |
| 528 | if (priv->caps.v2.max_texture_2d_size) |
| 529 | return priv->caps.v2.max_texture_2d_size; |
| 530 | |
| 531 | return UINT32_MAX; |
| 532 | } |
| 533 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 534 | 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] | 535 | { |
| 536 | int ret; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 537 | struct drm_virtgpu_get_caps cap_args = { 0 }; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 538 | |
Lepton Wu | eebce65 | 2020-02-26 15:13:34 -0800 | [diff] [blame] | 539 | *caps_is_v2 = 0; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 540 | cap_args.addr = (unsigned long long)caps; |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 541 | if (params[param_capset_fix].value) { |
Lepton Wu | eebce65 | 2020-02-26 15:13:34 -0800 | [diff] [blame] | 542 | *caps_is_v2 = 1; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 543 | cap_args.cap_set_id = 2; |
| 544 | cap_args.size = sizeof(union virgl_caps); |
| 545 | } else { |
| 546 | cap_args.cap_set_id = 1; |
| 547 | cap_args.size = sizeof(struct virgl_caps_v1); |
| 548 | } |
| 549 | |
| 550 | ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args); |
| 551 | if (ret) { |
| 552 | 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] | 553 | *caps_is_v2 = 0; |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 554 | |
| 555 | // Fallback to v1 |
| 556 | cap_args.cap_set_id = 1; |
| 557 | cap_args.size = sizeof(struct virgl_caps_v1); |
| 558 | |
| 559 | ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GET_CAPS, &cap_args); |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 560 | if (ret) |
Jason Macnak | ddf4ec0 | 2020-02-03 16:36:46 -0800 | [diff] [blame] | 561 | 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] | 562 | } |
| 563 | |
| 564 | return ret; |
| 565 | } |
| 566 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 567 | static void virgl_init_params_and_caps(struct driver *drv) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 568 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 569 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
| 570 | if (params[param_3d].value) { |
| 571 | virgl_get_caps(drv, &priv->caps, &priv->caps_is_v2); |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 572 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 573 | // We use two criteria to determine whether host minigbm is used on the host for |
| 574 | // swapchain allocations. |
| 575 | // |
Gurchetan Singh | bbde01e | 2021-02-17 08:54:28 -0800 | [diff] [blame] | 576 | // (1) Host minigbm is only available via virglrenderer, and only virglrenderer |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 577 | // advertises capabilities. |
| 578 | // (2) Only host minigbm doesn't emulate YUV formats. Checking this is a bit of a |
| 579 | // proxy, but it works. |
Gurchetan Singh | bbde01e | 2021-02-17 08:54:28 -0800 | [diff] [blame] | 580 | priv->host_gbm_enabled = |
| 581 | priv->caps.max_version > 0 && |
| 582 | virgl_supports_combination_natively(drv, DRM_FORMAT_NV12, BO_USE_TEXTURE); |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 583 | } |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 586 | static int virgl_init(struct driver *drv) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 587 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 588 | struct virgl_priv *priv; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 589 | |
| 590 | priv = calloc(1, sizeof(*priv)); |
Yiwei Zhang | afdf87d | 2021-09-28 04:06:06 +0000 | [diff] [blame] | 591 | if (!priv) |
| 592 | return -ENOMEM; |
| 593 | |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 594 | drv->priv = priv; |
| 595 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 596 | virgl_init_params_and_caps(drv); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 597 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 598 | if (params[param_3d].value) { |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 599 | /* This doesn't mean host can scanout everything, it just means host |
| 600 | * hypervisor can show it. */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 601 | virgl_add_combinations(drv, render_target_formats, |
| 602 | ARRAY_SIZE(render_target_formats), &LINEAR_METADATA, |
| 603 | BO_USE_RENDER_MASK | BO_USE_SCANOUT); |
| 604 | virgl_add_combinations(drv, texture_source_formats, |
| 605 | ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA, |
| 606 | BO_USE_TEXTURE_MASK); |
Yiwei Zhang | 9420ffe | 2021-09-24 06:24:30 +0000 | [diff] [blame] | 607 | /* NV12 with scanout must flow through virgl_add_combination, so that the native |
| 608 | * support is checked and scanout use_flag can be conditionally stripped. */ |
| 609 | virgl_add_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA, |
| 610 | BO_USE_TEXTURE_MASK | BO_USE_CAMERA_READ | |
| 611 | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER | |
| 612 | BO_USE_HW_VIDEO_ENCODER | BO_USE_SCANOUT); |
Gurchetan Singh | 3f3e5f9 | 2019-07-08 09:50:01 -0700 | [diff] [blame] | 613 | } else { |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 614 | /* Virtio primary plane only allows this format. */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 615 | virgl_add_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA, |
| 616 | BO_USE_RENDER_MASK | BO_USE_SCANOUT); |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 617 | /* Virtio cursor plane only allows this format and Chrome cannot live without |
| 618 | * ARGB888 renderable format. */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 619 | virgl_add_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA, |
| 620 | BO_USE_RENDER_MASK | BO_USE_CURSOR); |
Dominik Behr | 6e6dc49 | 2019-10-09 15:43:52 -0700 | [diff] [blame] | 621 | /* Android needs more, but they cannot be bound as scanouts anymore after |
| 622 | * "drm/virtio: fix DRM_FORMAT_* handling" */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 623 | virgl_add_combinations(drv, render_target_formats, |
| 624 | ARRAY_SIZE(render_target_formats), &LINEAR_METADATA, |
| 625 | BO_USE_RENDER_MASK); |
| 626 | virgl_add_combinations(drv, dumb_texture_source_formats, |
| 627 | ARRAY_SIZE(dumb_texture_source_formats), &LINEAR_METADATA, |
| 628 | BO_USE_TEXTURE_MASK); |
Yiwei Zhang | 9fa17e7 | 2021-09-17 22:11:29 +0000 | [diff] [blame] | 629 | drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA, |
| 630 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | |
| 631 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER); |
Gurchetan Singh | 3f3e5f9 | 2019-07-08 09:50:01 -0700 | [diff] [blame] | 632 | } |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 633 | |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 634 | /* Android CTS tests require this. */ |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 635 | virgl_add_combination(drv, DRM_FORMAT_RGB888, &LINEAR_METADATA, BO_USE_SW_MASK); |
| 636 | virgl_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK); |
Emilian Peev | f76db98 | 2022-01-26 14:28:35 -0800 | [diff] [blame] | 637 | virgl_add_combination(drv, DRM_FORMAT_P010, &LINEAR_METADATA, BO_USE_TEXTURE | |
Jason Macnak | 2ce3577 | 2021-06-08 06:45:45 -0700 | [diff] [blame] | 638 | BO_USE_SW_MASK | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE); |
Keiichi Watanabe | a13dda7 | 2018-08-02 22:45:05 +0900 | [diff] [blame] | 639 | drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA, |
David Staessens | 04b7e24 | 2020-05-28 15:47:15 +0900 | [diff] [blame] | 640 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER | |
Yiwei Zhang | bb9d4af | 2021-06-20 19:23:38 +0000 | [diff] [blame] | 641 | BO_USE_HW_VIDEO_ENCODER | BO_USE_GPU_DATA_BUFFER); |
David Stevens | 519978f | 2020-12-11 14:09:56 +0900 | [diff] [blame] | 642 | |
| 643 | if (!priv->host_gbm_enabled) { |
| 644 | drv_modify_combination(drv, DRM_FORMAT_ABGR8888, &LINEAR_METADATA, |
| 645 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | |
| 646 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER); |
| 647 | drv_modify_combination(drv, DRM_FORMAT_XBGR8888, &LINEAR_METADATA, |
| 648 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | |
| 649 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER); |
| 650 | drv_modify_combination(drv, DRM_FORMAT_NV21, &LINEAR_METADATA, |
| 651 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | |
| 652 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER); |
| 653 | drv_modify_combination(drv, DRM_FORMAT_R16, &LINEAR_METADATA, |
| 654 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | |
| 655 | BO_USE_HW_VIDEO_DECODER); |
| 656 | drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA, |
| 657 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | |
| 658 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER); |
| 659 | drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &LINEAR_METADATA, |
| 660 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | |
| 661 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER); |
| 662 | } |
Keiichi Watanabe | a13dda7 | 2018-08-02 22:45:05 +0900 | [diff] [blame] | 663 | |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 664 | return drv_modify_linear_combinations(drv); |
| 665 | } |
| 666 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 667 | static void virgl_close(struct driver *drv) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 668 | { |
| 669 | free(drv->priv); |
| 670 | drv->priv = NULL; |
| 671 | } |
| 672 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 673 | static int virgl_bo_create_blob(struct driver *drv, struct bo *bo) |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 674 | { |
| 675 | int ret; |
| 676 | uint32_t stride; |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 677 | uint32_t cur_blob_id; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 678 | uint32_t cmd[VIRGL_PIPE_RES_CREATE_SIZE + 1] = { 0 }; |
| 679 | struct drm_virtgpu_resource_create_blob drm_rc_blob = { 0 }; |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 680 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 681 | |
David Stevens | d3f07bd | 2020-09-25 18:52:26 +0900 | [diff] [blame] | 682 | uint32_t blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE; |
| 683 | if (bo->meta.use_flags & BO_USE_SW_MASK) |
| 684 | blob_flags |= VIRTGPU_BLOB_FLAG_USE_MAPPABLE; |
David Stevens | 1b252e2 | 2021-08-03 16:48:17 +0900 | [diff] [blame] | 685 | |
| 686 | // For now, all blob use cases are cross device. When we add wider |
| 687 | // support for blobs, we can revisit making this unconditional. |
| 688 | blob_flags |= VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE; |
David Stevens | b42624c | 2020-09-10 10:50:26 +0900 | [diff] [blame] | 689 | |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 690 | cur_blob_id = atomic_fetch_add(&priv->next_blob_id, 1); |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 691 | stride = drv_stride_from_format(bo->meta.format, bo->meta.width, 0); |
| 692 | drv_bo_from_format(bo, stride, bo->meta.height, bo->meta.format); |
| 693 | bo->meta.total_size = ALIGN(bo->meta.total_size, PAGE_SIZE); |
David Stevens | b42624c | 2020-09-10 10:50:26 +0900 | [diff] [blame] | 694 | bo->meta.tiling = blob_flags; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 695 | |
| 696 | cmd[0] = VIRGL_CMD0(VIRGL_CCMD_PIPE_RESOURCE_CREATE, 0, VIRGL_PIPE_RES_CREATE_SIZE); |
| 697 | cmd[VIRGL_PIPE_RES_CREATE_TARGET] = PIPE_TEXTURE_2D; |
| 698 | cmd[VIRGL_PIPE_RES_CREATE_WIDTH] = bo->meta.width; |
| 699 | cmd[VIRGL_PIPE_RES_CREATE_HEIGHT] = bo->meta.height; |
| 700 | cmd[VIRGL_PIPE_RES_CREATE_FORMAT] = translate_format(bo->meta.format); |
David Stevens | cf28048 | 2020-12-21 11:43:44 +0900 | [diff] [blame] | 701 | cmd[VIRGL_PIPE_RES_CREATE_BIND] = |
| 702 | compute_virgl_bind_flags(bo->meta.use_flags, bo->meta.format); |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 703 | cmd[VIRGL_PIPE_RES_CREATE_DEPTH] = 1; |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 704 | cmd[VIRGL_PIPE_RES_CREATE_BLOB_ID] = cur_blob_id; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 705 | |
| 706 | drm_rc_blob.cmd = (uint64_t)&cmd; |
| 707 | drm_rc_blob.cmd_size = 4 * (VIRGL_PIPE_RES_CREATE_SIZE + 1); |
| 708 | drm_rc_blob.size = bo->meta.total_size; |
| 709 | drm_rc_blob.blob_mem = VIRTGPU_BLOB_MEM_HOST3D; |
David Stevens | b42624c | 2020-09-10 10:50:26 +0900 | [diff] [blame] | 710 | drm_rc_blob.blob_flags = blob_flags; |
David Stevens | 0fe561f | 2020-10-28 16:06:38 +0900 | [diff] [blame] | 711 | drm_rc_blob.blob_id = cur_blob_id; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 712 | |
| 713 | ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB, &drm_rc_blob); |
| 714 | if (ret < 0) { |
| 715 | drv_log("DRM_VIRTGPU_RESOURCE_CREATE_BLOB failed with %s\n", strerror(errno)); |
| 716 | return -errno; |
| 717 | } |
| 718 | |
| 719 | for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) |
| 720 | bo->handles[plane].u32 = drm_rc_blob.bo_handle; |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | static bool should_use_blob(struct driver *drv, uint32_t format, uint64_t use_flags) |
| 726 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 727 | struct virgl_priv *priv = (struct virgl_priv *)drv->priv; |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 728 | |
| 729 | // TODO(gurchetansingh): remove once all minigbm users are blob-safe |
| 730 | #ifndef VIRTIO_GPU_NEXT |
| 731 | return false; |
| 732 | #endif |
| 733 | |
| 734 | // Only use blob when host gbm is available |
| 735 | if (!priv->host_gbm_enabled) |
| 736 | return false; |
| 737 | |
Yiwei Zhang | bb9d4af | 2021-06-20 19:23:38 +0000 | [diff] [blame] | 738 | // Use regular resources if only the GPU needs efficient access. Blob resource is a better |
| 739 | // fit for BO_USE_GPU_DATA_BUFFER which is mapped to VIRGL_BIND_LINEAR. |
| 740 | if (!(use_flags & (BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN | BO_USE_LINEAR | |
| 741 | BO_USE_NON_GPU_HW | BO_USE_GPU_DATA_BUFFER))) |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 742 | return false; |
| 743 | |
David Stevens | d3f07bd | 2020-09-25 18:52:26 +0900 | [diff] [blame] | 744 | switch (format) { |
David Stevens | d3f07bd | 2020-09-25 18:52:26 +0900 | [diff] [blame] | 745 | case DRM_FORMAT_R8: |
| 746 | // Formats with strictly defined strides are supported |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 747 | return true; |
David Stevens | c6df2b2 | 2021-08-10 19:02:09 +0900 | [diff] [blame] | 748 | case DRM_FORMAT_YVU420_ANDROID: |
David Stevens | d3f07bd | 2020-09-25 18:52:26 +0900 | [diff] [blame] | 749 | case DRM_FORMAT_NV12: |
| 750 | // Knowing buffer metadata at buffer creation isn't yet supported, so buffers |
| 751 | // can't be properly mapped into the guest. |
| 752 | return (use_flags & BO_USE_SW_MASK) == 0; |
| 753 | default: |
| 754 | return false; |
| 755 | } |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 758 | static int virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 759 | uint64_t use_flags) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 760 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 761 | if (params[param_resource_blob].value && params[param_host_visible].value && |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 762 | should_use_blob(bo->drv, format, use_flags)) |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 763 | return virgl_bo_create_blob(bo->drv, bo); |
Gurchetan Singh | 0ee06fb | 2019-09-13 17:49:20 -0700 | [diff] [blame] | 764 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 765 | if (params[param_3d].value) |
| 766 | return virgl_3d_bo_create(bo, width, height, format, use_flags); |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 767 | else |
Jason Macnak | c06cc9c | 2021-10-06 10:16:19 -0700 | [diff] [blame] | 768 | return virgl_2d_dumb_bo_create(bo, width, height, format, use_flags); |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 771 | static int virgl_bo_destroy(struct bo *bo) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 772 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 773 | if (params[param_3d].value) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 774 | return drv_gem_bo_destroy(bo); |
| 775 | else |
| 776 | return drv_dumb_bo_destroy(bo); |
| 777 | } |
| 778 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 779 | 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] | 780 | { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 781 | if (params[param_3d].value) |
| 782 | return virgl_3d_bo_map(bo, vma, plane, map_flags); |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 783 | else |
| 784 | return drv_dumb_bo_map(bo, vma, plane, map_flags); |
| 785 | } |
| 786 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 787 | static int virgl_bo_invalidate(struct bo *bo, struct mapping *mapping) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 788 | { |
| 789 | int ret; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 790 | size_t i; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 791 | struct drm_virtgpu_3d_transfer_from_host xfer = { 0 }; |
| 792 | struct drm_virtgpu_3d_wait waitcmd = { 0 }; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 793 | struct virtio_transfers_params xfer_params; |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 794 | struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv; |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 795 | uint64_t host_write_flags; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 796 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 797 | if (!params[param_3d].value) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 798 | return 0; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 799 | |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 800 | // Invalidate is only necessary if the host writes to the buffer. The encoder and |
| 801 | // decoder flags don't differentiate between input and output buffers, but we can |
| 802 | // use the format to determine whether this buffer could be encoder/decoder output. |
| 803 | host_write_flags = BO_USE_RENDERING | BO_USE_CAMERA_WRITE; |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 804 | if (bo->meta.format == DRM_FORMAT_R8) |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 805 | host_write_flags |= BO_USE_HW_VIDEO_ENCODER; |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 806 | else |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 807 | host_write_flags |= BO_USE_HW_VIDEO_DECODER; |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 808 | |
David Stevens | 9fe8c20 | 2020-12-21 18:47:55 +0900 | [diff] [blame] | 809 | if ((bo->meta.use_flags & host_write_flags) == 0) |
David Stevens | 4d5358d | 2019-10-24 14:59:31 +0900 | [diff] [blame] | 810 | return 0; |
| 811 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 812 | 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] | 813 | return 0; |
| 814 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 815 | xfer.bo_handle = mapping->vma->handle; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 816 | |
Gurchetan Singh | 1b57fe2 | 2020-05-05 09:18:22 -0700 | [diff] [blame] | 817 | if (mapping->rect.x || mapping->rect.y) { |
Gurchetan Singh | 1b57fe2 | 2020-05-05 09:18:22 -0700 | [diff] [blame] | 818 | /* |
| 819 | * virglrenderer uses the box parameters and assumes that offset == 0 for planar |
| 820 | * images |
| 821 | */ |
| 822 | if (bo->meta.num_planes == 1) { |
| 823 | xfer.offset = |
| 824 | (bo->meta.strides[0] * mapping->rect.y) + |
| 825 | drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x; |
| 826 | } |
| 827 | } |
| 828 | |
David Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 829 | if ((bo->meta.use_flags & BO_USE_RENDERING) == 0) { |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 830 | // Unfortunately, the kernel doesn't actually pass the guest layer_stride |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 831 | // 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] | 832 | // For gbm based resources, we can work around this by using the level field |
| 833 | // to pass the stride to virglrenderer's gbm transfer code. However, we need |
| 834 | // to avoid doing this for resources which don't rely on that transfer code, |
| 835 | // which is resources with the BO_USE_RENDERING flag set. |
David Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 836 | // TODO(b/145993887): Send also stride when the patches are landed |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 837 | if (priv->host_gbm_enabled) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 838 | xfer.level = bo->meta.strides[0]; |
David Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 839 | } |
Gurchetan Singh | 05e67cc | 2019-06-28 17:21:40 -0700 | [diff] [blame] | 840 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 841 | 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] | 842 | xfer_params.xfers_needed = 1; |
| 843 | xfer_params.xfer_boxes[0] = mapping->rect; |
| 844 | } else { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 845 | assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format, |
| 846 | bo->meta.use_flags)); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 847 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 848 | virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | for (i = 0; i < xfer_params.xfers_needed; i++) { |
| 852 | xfer.box.x = xfer_params.xfer_boxes[i].x; |
| 853 | xfer.box.y = xfer_params.xfer_boxes[i].y; |
| 854 | xfer.box.w = xfer_params.xfer_boxes[i].width; |
| 855 | xfer.box.h = xfer_params.xfer_boxes[i].height; |
| 856 | xfer.box.d = 1; |
| 857 | |
| 858 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer); |
| 859 | if (ret) { |
| 860 | drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n", |
| 861 | strerror(errno)); |
| 862 | return -errno; |
| 863 | } |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 864 | } |
| 865 | |
David Stevens | 4d5358d | 2019-10-24 14:59:31 +0900 | [diff] [blame] | 866 | // The transfer needs to complete before invalidate returns so that any host changes |
| 867 | // are visible and to ensure the host doesn't overwrite subsequent guest changes. |
| 868 | // TODO(b/136733358): Support returning fences from transfers |
David Stevens | 4d5358d | 2019-10-24 14:59:31 +0900 | [diff] [blame] | 869 | waitcmd.handle = mapping->vma->handle; |
| 870 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd); |
| 871 | if (ret) { |
| 872 | drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno)); |
| 873 | return -errno; |
| 874 | } |
| 875 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 876 | return 0; |
| 877 | } |
| 878 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 879 | static int virgl_bo_flush(struct bo *bo, struct mapping *mapping) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 880 | { |
| 881 | int ret; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 882 | size_t i; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 883 | struct drm_virtgpu_3d_transfer_to_host xfer = { 0 }; |
| 884 | struct drm_virtgpu_3d_wait waitcmd = { 0 }; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 885 | struct virtio_transfers_params xfer_params; |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 886 | struct virgl_priv *priv = (struct virgl_priv *)bo->drv->priv; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 887 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 888 | if (!params[param_3d].value) |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 889 | return 0; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 890 | |
| 891 | if (!(mapping->vma->map_flags & BO_MAP_WRITE)) |
| 892 | return 0; |
| 893 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 894 | 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] | 895 | return 0; |
| 896 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 897 | xfer.bo_handle = mapping->vma->handle; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 898 | |
Gurchetan Singh | 1b57fe2 | 2020-05-05 09:18:22 -0700 | [diff] [blame] | 899 | if (mapping->rect.x || mapping->rect.y) { |
Gurchetan Singh | 1b57fe2 | 2020-05-05 09:18:22 -0700 | [diff] [blame] | 900 | /* |
| 901 | * virglrenderer uses the box parameters and assumes that offset == 0 for planar |
| 902 | * images |
| 903 | */ |
| 904 | if (bo->meta.num_planes == 1) { |
| 905 | xfer.offset = |
| 906 | (bo->meta.strides[0] * mapping->rect.y) + |
| 907 | drv_bytes_per_pixel_from_format(bo->meta.format, 0) * mapping->rect.x; |
| 908 | } |
| 909 | } |
| 910 | |
Gurchetan Singh | 05e67cc | 2019-06-28 17:21:40 -0700 | [diff] [blame] | 911 | // Unfortunately, the kernel doesn't actually pass the guest layer_stride and |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 912 | // 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] | 913 | // the level to work around this. |
Gurchetan Singh | cadc54f | 2021-02-01 12:03:11 -0800 | [diff] [blame] | 914 | if (priv->host_gbm_enabled) |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 915 | xfer.level = bo->meta.strides[0]; |
Gurchetan Singh | 05e67cc | 2019-06-28 17:21:40 -0700 | [diff] [blame] | 916 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 917 | 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] | 918 | xfer_params.xfers_needed = 1; |
| 919 | xfer_params.xfer_boxes[0] = mapping->rect; |
| 920 | } else { |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 921 | assert(virgl_supports_combination_through_emulation(bo->drv, bo->meta.format, |
| 922 | bo->meta.use_flags)); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 923 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 924 | virgl_get_emulated_transfers_params(bo, &mapping->rect, &xfer_params); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | for (i = 0; i < xfer_params.xfers_needed; i++) { |
| 928 | xfer.box.x = xfer_params.xfer_boxes[i].x; |
| 929 | xfer.box.y = xfer_params.xfer_boxes[i].y; |
| 930 | xfer.box.w = xfer_params.xfer_boxes[i].width; |
| 931 | xfer.box.h = xfer_params.xfer_boxes[i].height; |
| 932 | xfer.box.d = 1; |
| 933 | |
| 934 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer); |
| 935 | if (ret) { |
| 936 | drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n", |
| 937 | strerror(errno)); |
| 938 | return -errno; |
| 939 | } |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 940 | } |
| 941 | |
David Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 942 | // If the buffer is only accessed by the host GPU, then the flush is ordered |
| 943 | // with subsequent commands. However, if other host hardware can access the |
| 944 | // buffer, we need to wait for the transfer to complete for consistency. |
| 945 | // TODO(b/136733358): Support returning fences from transfers |
| 946 | if (bo->meta.use_flags & BO_USE_NON_GPU_HW) { |
David Stevens | baab6c8 | 2020-02-26 17:14:43 +0900 | [diff] [blame] | 947 | waitcmd.handle = mapping->vma->handle; |
| 948 | |
| 949 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd); |
| 950 | if (ret) { |
| 951 | drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno)); |
| 952 | return -errno; |
| 953 | } |
| 954 | } |
| 955 | |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 956 | return 0; |
| 957 | } |
| 958 | |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 959 | static void virgl_3d_resolve_format_and_use_flags(struct driver *drv, uint32_t format, |
| 960 | uint64_t use_flags, uint32_t *out_format, |
| 961 | uint64_t *out_use_flags) |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 962 | { |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 963 | *out_format = format; |
| 964 | *out_use_flags = use_flags; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 965 | switch (format) { |
| 966 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Keiichi Watanabe | a13dda7 | 2018-08-02 22:45:05 +0900 | [diff] [blame] | 967 | /* Camera subsystem requires NV12. */ |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 968 | if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) { |
| 969 | *out_format = DRM_FORMAT_NV12; |
| 970 | } else { |
| 971 | /* HACK: See b/28671744 */ |
| 972 | *out_format = DRM_FORMAT_XBGR8888; |
Yiwei Zhang | 3a171db | 2021-10-01 22:12:05 +0000 | [diff] [blame] | 973 | *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER; |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 974 | } |
| 975 | break; |
Lepton Wu | 249e863 | 2018-04-05 12:50:03 -0700 | [diff] [blame] | 976 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 977 | /* All of our host drivers prefer NV12 as their flexible media format. |
| 978 | * If that changes, this will need to be modified. */ |
| 979 | *out_format = DRM_FORMAT_NV12; |
| 980 | /* fallthrough */ |
| 981 | case DRM_FORMAT_NV12: |
| 982 | case DRM_FORMAT_ABGR8888: |
| 983 | case DRM_FORMAT_ARGB8888: |
| 984 | case DRM_FORMAT_RGB565: |
| 985 | case DRM_FORMAT_XBGR8888: |
| 986 | case DRM_FORMAT_XRGB8888: |
| 987 | /* These are the scanout capable formats to the guest. Strip scanout use_flag if the |
| 988 | * host does not natively support scanout on the requested format. */ |
| 989 | if ((use_flags & BO_USE_SCANOUT) && |
| 990 | !virgl_supports_combination_natively(drv, format, BO_USE_SCANOUT)) |
| 991 | *out_use_flags &= ~BO_USE_SCANOUT; |
| 992 | break; |
| 993 | case DRM_FORMAT_YVU420_ANDROID: |
| 994 | *out_use_flags &= ~BO_USE_SCANOUT; |
| 995 | /* HACK: See b/172389166. Also see gbm_bo_create. */ |
| 996 | *out_use_flags |= BO_USE_LINEAR; |
| 997 | break; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 998 | default: |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 999 | break; |
Zach Reizner | 85c4c5f | 2017-10-04 13:15:57 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
Yiwei Zhang | c1413ea | 2021-09-17 08:20:21 +0000 | [diff] [blame] | 1002 | |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 1003 | static void virgl_2d_resolve_format_and_use_flags(uint32_t format, uint64_t use_flags, |
| 1004 | uint32_t *out_format, uint64_t *out_use_flags) |
Yiwei Zhang | c1413ea | 2021-09-17 08:20:21 +0000 | [diff] [blame] | 1005 | { |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 1006 | *out_format = format; |
| 1007 | *out_use_flags = use_flags; |
Yiwei Zhang | c1413ea | 2021-09-17 08:20:21 +0000 | [diff] [blame] | 1008 | |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 1009 | /* HACK: See crrev/c/1849773 */ |
| 1010 | if (format != DRM_FORMAT_XRGB8888) |
| 1011 | *out_use_flags &= ~BO_USE_SCANOUT; |
| 1012 | |
| 1013 | switch (format) { |
| 1014 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
| 1015 | /* Camera subsystem requires NV12. */ |
| 1016 | if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) { |
| 1017 | *out_format = DRM_FORMAT_NV12; |
| 1018 | } else { |
| 1019 | /* HACK: See b/28671744 */ |
| 1020 | *out_format = DRM_FORMAT_XBGR8888; |
Yiwei Zhang | 3a171db | 2021-10-01 22:12:05 +0000 | [diff] [blame] | 1021 | *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER; |
Yiwei Zhang | 9420ffe | 2021-09-24 06:24:30 +0000 | [diff] [blame] | 1022 | } |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
| 1025 | *out_format = DRM_FORMAT_YVU420_ANDROID; |
| 1026 | /* fallthrough */ |
| 1027 | case DRM_FORMAT_YVU420_ANDROID: |
| 1028 | *out_use_flags &= ~BO_USE_SCANOUT; |
| 1029 | /* HACK: See b/172389166. Also see gbm_bo_create. */ |
| 1030 | *out_use_flags |= BO_USE_LINEAR; |
| 1031 | break; |
| 1032 | default: |
| 1033 | break; |
Yiwei Zhang | 9420ffe | 2021-09-24 06:24:30 +0000 | [diff] [blame] | 1034 | } |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 1035 | } |
Yiwei Zhang | c1413ea | 2021-09-17 08:20:21 +0000 | [diff] [blame] | 1036 | |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 1037 | static void virgl_resolve_format_and_use_flags(struct driver *drv, uint32_t format, |
| 1038 | uint64_t use_flags, uint32_t *out_format, |
| 1039 | uint64_t *out_use_flags) |
| 1040 | { |
| 1041 | if (params[param_3d].value) { |
| 1042 | return virgl_3d_resolve_format_and_use_flags(drv, format, use_flags, out_format, |
| 1043 | out_use_flags); |
| 1044 | } else { |
| 1045 | return virgl_2d_resolve_format_and_use_flags(format, use_flags, out_format, |
| 1046 | out_use_flags); |
| 1047 | } |
Yiwei Zhang | c1413ea | 2021-09-17 08:20:21 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 1050 | 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] | 1051 | uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier) |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 1052 | { |
| 1053 | int ret; |
Chia-I Wu | 2e41f63 | 2021-01-11 11:08:21 -0800 | [diff] [blame] | 1054 | struct drm_virtgpu_resource_info_cros res_info = { 0 }; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 1055 | |
Gurchetan Singh | 73c141e | 2021-01-21 14:51:19 -0800 | [diff] [blame] | 1056 | if (!params[param_3d].value) |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 1057 | return 0; |
| 1058 | |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 1059 | res_info.bo_handle = bo->handles[0].u32; |
Chia-I Wu | 5085562 | 2021-01-12 12:38:09 -0800 | [diff] [blame] | 1060 | res_info.type = VIRTGPU_RESOURCE_INFO_TYPE_EXTENDED; |
Chia-I Wu | 2e41f63 | 2021-01-11 11:08:21 -0800 | [diff] [blame] | 1061 | 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] | 1062 | if (ret) { |
| 1063 | drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_INFO failed with %s\n", strerror(errno)); |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
Yiwei Zhang | f58616e | 2021-08-26 05:54:15 +0000 | [diff] [blame] | 1067 | for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++) { |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 1068 | /* |
| 1069 | * Currently, kernel v4.14 (Betty) doesn't have the extended resource info |
| 1070 | * ioctl. |
| 1071 | */ |
Yiwei Zhang | f58616e | 2021-08-26 05:54:15 +0000 | [diff] [blame] | 1072 | if (!res_info.strides[plane]) |
| 1073 | break; |
| 1074 | |
| 1075 | strides[plane] = res_info.strides[plane]; |
| 1076 | offsets[plane] = res_info.offsets[plane]; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 1077 | } |
Yiwei Zhang | a1e93fd | 2021-04-30 07:01:55 +0000 | [diff] [blame] | 1078 | *format_modifier = res_info.format_modifier; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Jason Macnak | d6666c8 | 2021-09-29 11:13:25 -0700 | [diff] [blame] | 1083 | static uint32_t virgl_get_max_texture_2d_size(struct driver *drv) |
| 1084 | { |
| 1085 | if (params[param_3d].value) |
| 1086 | return virgl_3d_get_max_texture_2d_size(drv); |
| 1087 | else |
Jason Macnak | c06cc9c | 2021-10-06 10:16:19 -0700 | [diff] [blame] | 1088 | return VIRGL_2D_MAX_TEXTURE_2D_SIZE; |
Jason Macnak | d6666c8 | 2021-09-29 11:13:25 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Gurchetan Singh | bbde01e | 2021-02-17 08:54:28 -0800 | [diff] [blame] | 1091 | const struct backend virtgpu_virgl = { .name = "virtgpu_virgl", |
| 1092 | .init = virgl_init, |
| 1093 | .close = virgl_close, |
| 1094 | .bo_create = virgl_bo_create, |
| 1095 | .bo_destroy = virgl_bo_destroy, |
| 1096 | .bo_import = drv_prime_bo_import, |
| 1097 | .bo_map = virgl_bo_map, |
| 1098 | .bo_unmap = drv_bo_munmap, |
| 1099 | .bo_invalidate = virgl_bo_invalidate, |
| 1100 | .bo_flush = virgl_bo_flush, |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 1101 | .resolve_format_and_use_flags = |
| 1102 | virgl_resolve_format_and_use_flags, |
Jason Macnak | d6666c8 | 2021-09-29 11:13:25 -0700 | [diff] [blame] | 1103 | .resource_info = virgl_resource_info, |
| 1104 | .get_max_texture_2d_size = virgl_get_max_texture_2d_size }; |