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