1 // Copyright 2024 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 //! Protocol based on ${crosvm_src}/devices/src/virtio/gpu/protocol.rs 6 7 #![allow(dead_code)] 8 9 use zerocopy::AsBytes; 10 use zerocopy::FromBytes; 11 use zerocopy::FromZeroes; 12 13 use crate::rutabaga_utils::RutabagaHandle; 14 use crate::rutabaga_utils::VulkanInfo; 15 16 /* 2d commands */ 17 pub const KUMQUAT_GPU_PROTOCOL_RESOURCE_UNREF: u32 = 0x100; 18 pub const KUMQUAT_GPU_PROTOCOL_GET_NUM_CAPSETS: u32 = 0x101; 19 pub const KUMQUAT_GPU_PROTOCOL_GET_CAPSET_INFO: u32 = 0x102; 20 pub const KUMQUAT_GPU_PROTOCOL_GET_CAPSET: u32 = 0x103; 21 pub const KUMQUAT_GPU_PROTOCOL_RESOURCE_CREATE_BLOB: u32 = 0x104; 22 23 /* 3d commands */ 24 pub const KUMQUAT_GPU_PROTOCOL_CTX_CREATE: u32 = 0x200; 25 pub const KUMQUAT_GPU_PROTOCOL_CTX_DESTROY: u32 = 0x201; 26 pub const KUMQUAT_GPU_PROTOCOL_CTX_ATTACH_RESOURCE: u32 = 0x202; 27 pub const KUMQUAT_GPU_PROTOCOL_CTX_DETACH_RESOURCE: u32 = 0x203; 28 pub const KUMQUAT_GPU_PROTOCOL_RESOURCE_CREATE_3D: u32 = 0x204; 29 pub const KUMQUAT_GPU_PROTOCOL_TRANSFER_TO_HOST_3D: u32 = 0x205; 30 pub const KUMQUAT_GPU_PROTOCOL_TRANSFER_FROM_HOST_3D: u32 = 0x206; 31 pub const KUMQUAT_GPU_PROTOCOL_SUBMIT_3D: u32 = 0x207; 32 pub const KUMQUAT_GPU_PROTOCOL_RESOURCE_MAP_BLOB: u32 = 0x208; 33 pub const KUMQUAT_GPU_PROTOCOL_RESOURCE_UNMAP_BLOB: u32 = 0x209; 34 pub const KUMQUAT_GPU_PROTOCOL_SNAPSHOT_SAVE: u32 = 0x208; 35 pub const KUMQUAT_GPU_PROTOCOL_SNAPSHOT_RESTORE: u32 = 0x209; 36 37 /* success responses */ 38 pub const KUMQUAT_GPU_PROTOCOL_RESP_NODATA: u32 = 0x3001; 39 pub const KUMQUAT_GPU_PROTOCOL_RESP_NUM_CAPSETS: u32 = 0x3002; 40 pub const KUMQUAT_GPU_PROTOCOL_RESP_CAPSET_INFO: u32 = 0x3003; 41 pub const KUMQUAT_GPU_PROTOCOL_RESP_CAPSET: u32 = 0x3004; 42 pub const KUMQUAT_GPU_PROTOCOL_RESP_CONTEXT_CREATE: u32 = 0x3005; 43 pub const KUMQUAT_GPU_PROTOCOL_RESP_RESOURCE_CREATE: u32 = 0x3006; 44 pub const KUMQUAT_GPU_PROTOCOL_RESP_CMD_SUBMIT_3D: u32 = 0x3007; 45 pub const KUMQUAT_GPU_PROTOCOL_RESP_OK_SNAPSHOT: u32 = 0x3008; 46 47 #[derive(Copy, Clone, Debug, Default, AsBytes, FromZeroes, FromBytes)] 48 #[repr(C)] 49 pub struct kumquat_gpu_protocol_ctrl_hdr { 50 pub type_: u32, 51 pub payload: u32, 52 } 53 54 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 55 #[repr(C)] 56 pub struct kumquat_gpu_protocol_box { 57 pub x: u32, 58 pub y: u32, 59 pub z: u32, 60 pub w: u32, 61 pub h: u32, 62 pub d: u32, 63 } 64 65 /* KUMQUAT_GPU_PROTOCOL_TRANSFER_TO_HOST_3D, KUMQUAT_GPU_PROTOCOL_TRANSFER_FROM_HOST_3D */ 66 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 67 #[repr(C)] 68 pub struct kumquat_gpu_protocol_transfer_host_3d { 69 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 70 pub box_: kumquat_gpu_protocol_box, 71 pub offset: u64, 72 pub level: u32, 73 pub stride: u32, 74 pub layer_stride: u32, 75 pub ctx_id: u32, 76 pub resource_id: u32, 77 pub padding: u32, 78 } 79 80 /* KUMQUAT_GPU_PROTOCOL_RESOURCE_CREATE_3D */ 81 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 82 #[repr(C)] 83 pub struct kumquat_gpu_protocol_resource_create_3d { 84 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 85 pub target: u32, 86 pub format: u32, 87 pub bind: u32, 88 pub width: u32, 89 pub height: u32, 90 pub depth: u32, 91 pub array_size: u32, 92 pub last_level: u32, 93 pub nr_samples: u32, 94 pub flags: u32, 95 pub size: u32, 96 pub stride: u32, 97 pub ctx_id: u32, 98 } 99 100 #[derive(Debug, Copy, FromZeroes, FromBytes, AsBytes)] 101 #[repr(C)] 102 pub struct kumquat_gpu_protocol_ctx_create { 103 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 104 pub nlen: u32, 105 pub context_init: u32, 106 pub debug_name: [u8; 64], 107 } 108 109 impl Default for kumquat_gpu_protocol_ctx_create { default() -> Self110 fn default() -> Self { 111 // SAFETY: trivially safe 112 unsafe { ::std::mem::zeroed() } 113 } 114 } 115 116 impl Clone for kumquat_gpu_protocol_ctx_create { clone(&self) -> kumquat_gpu_protocol_ctx_create117 fn clone(&self) -> kumquat_gpu_protocol_ctx_create { 118 *self 119 } 120 } 121 122 /* KUMQUAT_GPU_PROTOCOL_CTX_ATTACH_RESOURCE, KUMQUAT_GPU_PROTOCOL_CTX_DETACH_RESOURCE */ 123 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 124 #[repr(C)] 125 pub struct kumquat_gpu_protocol_ctx_resource { 126 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 127 pub ctx_id: u32, 128 pub resource_id: u32, 129 } 130 131 /* KUMQUAT_GPU_PROTOCOL_SUBMIT_3D */ 132 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 133 #[repr(C)] 134 pub struct kumquat_gpu_protocol_cmd_submit { 135 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 136 pub ctx_id: u32, 137 pub pad: u32, 138 pub size: u32, 139 140 // The in-fence IDs are prepended to the cmd_buf and memory layout 141 // of the KUMQUAT_GPU_PROTOCOL_SUBMIT_3D buffer looks like this: 142 // _________________ 143 // | CMD_SUBMIT_3D | 144 // ----------------- 145 // | header | 146 // | in-fence IDs | 147 // | cmd_buf | 148 // ----------------- 149 // 150 // This makes in-fence IDs naturally aligned to the sizeof(u64) inside 151 // of the virtio buffer. 152 pub num_in_fences: u32, 153 pub flags: u32, 154 pub ring_idx: u8, 155 pub padding: [u8; 3], 156 } 157 158 /* KUMQUAT_GPU_PROTOCOL_RESP_CAPSET_INFO */ 159 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 160 #[repr(C)] 161 pub struct kumquat_gpu_protocol_resp_capset_info { 162 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 163 pub capset_id: u32, 164 pub version: u32, 165 pub size: u32, 166 pub padding: u32, 167 } 168 169 /* KUMQUAT_GPU_PROTOCOL_GET_CAPSET */ 170 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 171 #[repr(C)] 172 pub struct kumquat_gpu_protocol_get_capset { 173 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 174 pub capset_id: u32, 175 pub capset_version: u32, 176 } 177 178 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 179 #[repr(C)] 180 pub struct kumquat_gpu_protocol_resource_create_blob { 181 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 182 pub ctx_id: u32, 183 pub blob_mem: u32, 184 pub blob_flags: u32, 185 pub padding: u32, 186 pub blob_id: u64, 187 pub size: u64, 188 } 189 190 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 191 #[repr(C)] 192 pub struct kumquat_gpu_protocol_resp_resource_create { 193 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 194 pub resource_id: u32, 195 pub handle_type: u32, 196 pub vulkan_info: VulkanInfo, 197 } 198 199 #[derive(Copy, Clone, Debug, Default, FromZeroes, FromBytes, AsBytes)] 200 #[repr(C)] 201 pub struct kumquat_gpu_protocol_resp_cmd_submit_3d { 202 pub hdr: kumquat_gpu_protocol_ctrl_hdr, 203 pub fence_id: u64, 204 pub handle_type: u32, 205 pub padding: u32, 206 } 207 208 /// A virtio gpu command and associated metadata specific to each command. 209 #[derive(Debug)] 210 pub enum KumquatGpuProtocol { 211 OkNoData, 212 GetNumCapsets, 213 GetCapsetInfo(u32), 214 GetCapset(kumquat_gpu_protocol_get_capset), 215 CtxCreate(kumquat_gpu_protocol_ctx_create), 216 CtxDestroy(u32), 217 CtxAttachResource(kumquat_gpu_protocol_ctx_resource), 218 CtxDetachResource(kumquat_gpu_protocol_ctx_resource), 219 ResourceCreate3d(kumquat_gpu_protocol_resource_create_3d), 220 TransferToHost3d(kumquat_gpu_protocol_transfer_host_3d, RutabagaHandle), 221 TransferFromHost3d(kumquat_gpu_protocol_transfer_host_3d, RutabagaHandle), 222 CmdSubmit3d(kumquat_gpu_protocol_cmd_submit, Vec<u8>, Vec<u64>), 223 ResourceCreateBlob(kumquat_gpu_protocol_resource_create_blob), 224 SnapshotSave, 225 SnapshotRestore, 226 RespNumCapsets(u32), 227 RespCapsetInfo(kumquat_gpu_protocol_resp_capset_info), 228 RespCapset(Vec<u8>), 229 RespContextCreate(u32), 230 RespResourceCreate(kumquat_gpu_protocol_resp_resource_create, RutabagaHandle), 231 RespCmdSubmit3d(u64, RutabagaHandle), 232 RespOkSnapshot, 233 } 234 235 pub enum KumquatGpuProtocolWrite<T: AsBytes + FromBytes> { 236 Cmd(T), 237 CmdWithHandle(T, RutabagaHandle), 238 CmdWithData(T, Vec<u8>), 239 } 240