1 1.3 riastrad /* $NetBSD: amdgpu_virt.h,v 1.3 2021/12/19 10:59:01 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2016 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * Author: Monk.liu (at) amd.com 25 1.1 riastrad */ 26 1.1 riastrad #ifndef AMDGPU_VIRT_H 27 1.1 riastrad #define AMDGPU_VIRT_H 28 1.1 riastrad 29 1.1 riastrad #define AMDGPU_SRIOV_CAPS_SRIOV_VBIOS (1 << 0) /* vBIOS is sr-iov ready */ 30 1.1 riastrad #define AMDGPU_SRIOV_CAPS_ENABLE_IOV (1 << 1) /* sr-iov is enabled on this GPU */ 31 1.1 riastrad #define AMDGPU_SRIOV_CAPS_IS_VF (1 << 2) /* this GPU is a virtual function */ 32 1.1 riastrad #define AMDGPU_PASSTHROUGH_MODE (1 << 3) /* thw whole GPU is pass through for VM */ 33 1.1 riastrad #define AMDGPU_SRIOV_CAPS_RUNTIME (1 << 4) /* is out of full access mode */ 34 1.1 riastrad 35 1.1 riastrad struct amdgpu_mm_table { 36 1.1 riastrad struct amdgpu_bo *bo; 37 1.1 riastrad uint32_t *cpu_addr; 38 1.1 riastrad uint64_t gpu_addr; 39 1.1 riastrad }; 40 1.1 riastrad 41 1.1 riastrad #define AMDGPU_VF_ERROR_ENTRY_SIZE 16 42 1.1 riastrad 43 1.1 riastrad /* struct error_entry - amdgpu VF error information. */ 44 1.1 riastrad struct amdgpu_vf_error_buffer { 45 1.1 riastrad struct mutex lock; 46 1.1 riastrad int read_count; 47 1.1 riastrad int write_count; 48 1.1 riastrad uint16_t code[AMDGPU_VF_ERROR_ENTRY_SIZE]; 49 1.1 riastrad uint16_t flags[AMDGPU_VF_ERROR_ENTRY_SIZE]; 50 1.1 riastrad uint64_t data[AMDGPU_VF_ERROR_ENTRY_SIZE]; 51 1.1 riastrad }; 52 1.1 riastrad 53 1.1 riastrad /** 54 1.1 riastrad * struct amdgpu_virt_ops - amdgpu device virt operations 55 1.1 riastrad */ 56 1.1 riastrad struct amdgpu_virt_ops { 57 1.1 riastrad int (*req_full_gpu)(struct amdgpu_device *adev, bool init); 58 1.1 riastrad int (*rel_full_gpu)(struct amdgpu_device *adev, bool init); 59 1.1 riastrad int (*reset_gpu)(struct amdgpu_device *adev); 60 1.1 riastrad int (*wait_reset)(struct amdgpu_device *adev); 61 1.1 riastrad void (*trans_msg)(struct amdgpu_device *adev, u32 req, u32 data1, u32 data2, u32 data3); 62 1.1 riastrad }; 63 1.1 riastrad 64 1.1 riastrad /* 65 1.1 riastrad * Firmware Reserve Frame buffer 66 1.1 riastrad */ 67 1.1 riastrad struct amdgpu_virt_fw_reserve { 68 1.1 riastrad struct amd_sriov_msg_pf2vf_info_header *p_pf2vf; 69 1.1 riastrad struct amd_sriov_msg_vf2pf_info_header *p_vf2pf; 70 1.1 riastrad unsigned int checksum_key; 71 1.1 riastrad }; 72 1.1 riastrad /* 73 1.1 riastrad * Defination between PF and VF 74 1.1 riastrad * Structures forcibly aligned to 4 to keep the same style as PF. 75 1.1 riastrad */ 76 1.1 riastrad #define AMDGIM_DATAEXCHANGE_OFFSET (64 * 1024) 77 1.1 riastrad 78 1.1 riastrad #define AMDGIM_GET_STRUCTURE_RESERVED_SIZE(total, u8, u16, u32, u64) \ 79 1.1 riastrad (total - (((u8)+3) / 4 + ((u16)+1) / 2 + (u32) + (u64)*2)) 80 1.1 riastrad 81 1.1 riastrad enum AMDGIM_FEATURE_FLAG { 82 1.1 riastrad /* GIM supports feature of Error log collecting */ 83 1.1 riastrad AMDGIM_FEATURE_ERROR_LOG_COLLECT = 0x1, 84 1.1 riastrad /* GIM supports feature of loading uCodes */ 85 1.1 riastrad AMDGIM_FEATURE_GIM_LOAD_UCODES = 0x2, 86 1.1 riastrad /* VRAM LOST by GIM */ 87 1.1 riastrad AMDGIM_FEATURE_GIM_FLR_VRAMLOST = 0x4, 88 1.1 riastrad /* PP ONE VF MODE in GIM */ 89 1.1 riastrad AMDGIM_FEATURE_PP_ONE_VF = (1 << 4), 90 1.1 riastrad }; 91 1.1 riastrad 92 1.1 riastrad struct amd_sriov_msg_pf2vf_info_header { 93 1.1 riastrad /* the total structure size in byte. */ 94 1.1 riastrad uint32_t size; 95 1.1 riastrad /* version of this structure, written by the GIM */ 96 1.1 riastrad uint32_t version; 97 1.1 riastrad /* reserved */ 98 1.1 riastrad uint32_t reserved[2]; 99 1.1 riastrad } __aligned(4); 100 1.1 riastrad struct amdgim_pf2vf_info_v1 { 101 1.1 riastrad /* header contains size and version */ 102 1.1 riastrad struct amd_sriov_msg_pf2vf_info_header header; 103 1.1 riastrad /* max_width * max_height */ 104 1.1 riastrad unsigned int uvd_enc_max_pixels_count; 105 1.1 riastrad /* 16x16 pixels/sec, codec independent */ 106 1.1 riastrad unsigned int uvd_enc_max_bandwidth; 107 1.1 riastrad /* max_width * max_height */ 108 1.1 riastrad unsigned int vce_enc_max_pixels_count; 109 1.1 riastrad /* 16x16 pixels/sec, codec independent */ 110 1.1 riastrad unsigned int vce_enc_max_bandwidth; 111 1.1 riastrad /* MEC FW position in kb from the start of visible frame buffer */ 112 1.1 riastrad unsigned int mecfw_kboffset; 113 1.1 riastrad /* The features flags of the GIM driver supports. */ 114 1.1 riastrad unsigned int feature_flags; 115 1.1 riastrad /* use private key from mailbox 2 to create chueksum */ 116 1.1 riastrad unsigned int checksum; 117 1.1 riastrad } __aligned(4); 118 1.1 riastrad 119 1.1 riastrad struct amdgim_pf2vf_info_v2 { 120 1.1 riastrad /* header contains size and version */ 121 1.1 riastrad struct amd_sriov_msg_pf2vf_info_header header; 122 1.1 riastrad /* use private key from mailbox 2 to create chueksum */ 123 1.1 riastrad uint32_t checksum; 124 1.1 riastrad /* The features flags of the GIM driver supports. */ 125 1.1 riastrad uint32_t feature_flags; 126 1.1 riastrad /* max_width * max_height */ 127 1.1 riastrad uint32_t uvd_enc_max_pixels_count; 128 1.1 riastrad /* 16x16 pixels/sec, codec independent */ 129 1.1 riastrad uint32_t uvd_enc_max_bandwidth; 130 1.1 riastrad /* max_width * max_height */ 131 1.1 riastrad uint32_t vce_enc_max_pixels_count; 132 1.1 riastrad /* 16x16 pixels/sec, codec independent */ 133 1.1 riastrad uint32_t vce_enc_max_bandwidth; 134 1.1 riastrad /* MEC FW position in kb from the start of VF visible frame buffer */ 135 1.1 riastrad uint64_t mecfw_kboffset; 136 1.1 riastrad /* MEC FW size in KB */ 137 1.1 riastrad uint32_t mecfw_ksize; 138 1.1 riastrad /* UVD FW position in kb from the start of VF visible frame buffer */ 139 1.1 riastrad uint64_t uvdfw_kboffset; 140 1.1 riastrad /* UVD FW size in KB */ 141 1.1 riastrad uint32_t uvdfw_ksize; 142 1.1 riastrad /* VCE FW position in kb from the start of VF visible frame buffer */ 143 1.1 riastrad uint64_t vcefw_kboffset; 144 1.1 riastrad /* VCE FW size in KB */ 145 1.1 riastrad uint32_t vcefw_ksize; 146 1.1 riastrad uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 0, 0, (9 + sizeof(struct amd_sriov_msg_pf2vf_info_header)/sizeof(uint32_t)), 3)]; 147 1.1 riastrad } __aligned(4); 148 1.1 riastrad 149 1.1 riastrad 150 1.1 riastrad struct amd_sriov_msg_vf2pf_info_header { 151 1.1 riastrad /* the total structure size in byte. */ 152 1.1 riastrad uint32_t size; 153 1.1 riastrad /*version of this structure, written by the guest */ 154 1.1 riastrad uint32_t version; 155 1.1 riastrad /* reserved */ 156 1.1 riastrad uint32_t reserved[2]; 157 1.1 riastrad } __aligned(4); 158 1.1 riastrad 159 1.1 riastrad struct amdgim_vf2pf_info_v1 { 160 1.1 riastrad /* header contains size and version */ 161 1.1 riastrad struct amd_sriov_msg_vf2pf_info_header header; 162 1.1 riastrad /* driver version */ 163 1.1 riastrad char driver_version[64]; 164 1.1 riastrad /* driver certification, 1=WHQL, 0=None */ 165 1.1 riastrad unsigned int driver_cert; 166 1.1 riastrad /* guest OS type and version: need a define */ 167 1.1 riastrad unsigned int os_info; 168 1.1 riastrad /* in the unit of 1M */ 169 1.1 riastrad unsigned int fb_usage; 170 1.1 riastrad /* guest gfx engine usage percentage */ 171 1.1 riastrad unsigned int gfx_usage; 172 1.1 riastrad /* guest gfx engine health percentage */ 173 1.1 riastrad unsigned int gfx_health; 174 1.1 riastrad /* guest compute engine usage percentage */ 175 1.1 riastrad unsigned int compute_usage; 176 1.1 riastrad /* guest compute engine health percentage */ 177 1.1 riastrad unsigned int compute_health; 178 1.1 riastrad /* guest vce engine usage percentage. 0xffff means N/A. */ 179 1.1 riastrad unsigned int vce_enc_usage; 180 1.1 riastrad /* guest vce engine health percentage. 0xffff means N/A. */ 181 1.1 riastrad unsigned int vce_enc_health; 182 1.1 riastrad /* guest uvd engine usage percentage. 0xffff means N/A. */ 183 1.1 riastrad unsigned int uvd_enc_usage; 184 1.1 riastrad /* guest uvd engine usage percentage. 0xffff means N/A. */ 185 1.1 riastrad unsigned int uvd_enc_health; 186 1.1 riastrad unsigned int checksum; 187 1.1 riastrad } __aligned(4); 188 1.1 riastrad 189 1.1 riastrad struct amdgim_vf2pf_info_v2 { 190 1.1 riastrad /* header contains size and version */ 191 1.1 riastrad struct amd_sriov_msg_vf2pf_info_header header; 192 1.1 riastrad uint32_t checksum; 193 1.1 riastrad /* driver version */ 194 1.1 riastrad uint8_t driver_version[64]; 195 1.1 riastrad /* driver certification, 1=WHQL, 0=None */ 196 1.1 riastrad uint32_t driver_cert; 197 1.1 riastrad /* guest OS type and version: need a define */ 198 1.1 riastrad uint32_t os_info; 199 1.1 riastrad /* in the unit of 1M */ 200 1.1 riastrad uint32_t fb_usage; 201 1.1 riastrad /* guest gfx engine usage percentage */ 202 1.1 riastrad uint32_t gfx_usage; 203 1.1 riastrad /* guest gfx engine health percentage */ 204 1.1 riastrad uint32_t gfx_health; 205 1.1 riastrad /* guest compute engine usage percentage */ 206 1.1 riastrad uint32_t compute_usage; 207 1.1 riastrad /* guest compute engine health percentage */ 208 1.1 riastrad uint32_t compute_health; 209 1.1 riastrad /* guest vce engine usage percentage. 0xffff means N/A. */ 210 1.1 riastrad uint32_t vce_enc_usage; 211 1.1 riastrad /* guest vce engine health percentage. 0xffff means N/A. */ 212 1.1 riastrad uint32_t vce_enc_health; 213 1.1 riastrad /* guest uvd engine usage percentage. 0xffff means N/A. */ 214 1.1 riastrad uint32_t uvd_enc_usage; 215 1.1 riastrad /* guest uvd engine usage percentage. 0xffff means N/A. */ 216 1.1 riastrad uint32_t uvd_enc_health; 217 1.1 riastrad uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 64, 0, (12 + sizeof(struct amd_sriov_msg_vf2pf_info_header)/sizeof(uint32_t)), 0)]; 218 1.1 riastrad } __aligned(4); 219 1.1 riastrad 220 1.1 riastrad #define AMDGPU_FW_VRAM_VF2PF_VER 2 221 1.1 riastrad typedef struct amdgim_vf2pf_info_v2 amdgim_vf2pf_info ; 222 1.1 riastrad 223 1.1 riastrad #define AMDGPU_FW_VRAM_VF2PF_WRITE(adev, field, val) \ 224 1.1 riastrad do { \ 225 1.1 riastrad ((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field = (val); \ 226 1.1 riastrad } while (0) 227 1.1 riastrad 228 1.1 riastrad #define AMDGPU_FW_VRAM_VF2PF_READ(adev, field, val) \ 229 1.1 riastrad do { \ 230 1.1 riastrad (*val) = ((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field; \ 231 1.1 riastrad } while (0) 232 1.1 riastrad 233 1.1 riastrad #define AMDGPU_FW_VRAM_PF2VF_READ(adev, field, val) \ 234 1.1 riastrad do { \ 235 1.1 riastrad if (!adev->virt.fw_reserve.p_pf2vf) \ 236 1.1 riastrad *(val) = 0; \ 237 1.1 riastrad else { \ 238 1.1 riastrad if (adev->virt.fw_reserve.p_pf2vf->version == 1) \ 239 1.1 riastrad *(val) = ((struct amdgim_pf2vf_info_v1 *)adev->virt.fw_reserve.p_pf2vf)->field; \ 240 1.1 riastrad if (adev->virt.fw_reserve.p_pf2vf->version == 2) \ 241 1.1 riastrad *(val) = ((struct amdgim_pf2vf_info_v2 *)adev->virt.fw_reserve.p_pf2vf)->field; \ 242 1.1 riastrad } \ 243 1.1 riastrad } while (0) 244 1.1 riastrad 245 1.1 riastrad /* GPU virtualization */ 246 1.1 riastrad struct amdgpu_virt { 247 1.1 riastrad uint32_t caps; 248 1.1 riastrad struct amdgpu_bo *csa_obj; 249 1.1 riastrad void *csa_cpu_addr; 250 1.1 riastrad bool chained_ib_support; 251 1.1 riastrad uint32_t reg_val_offs; 252 1.1 riastrad struct amdgpu_irq_src ack_irq; 253 1.1 riastrad struct amdgpu_irq_src rcv_irq; 254 1.1 riastrad struct work_struct flr_work; 255 1.1 riastrad struct amdgpu_mm_table mm_table; 256 1.1 riastrad const struct amdgpu_virt_ops *ops; 257 1.1 riastrad struct amdgpu_vf_error_buffer vf_errors; 258 1.1 riastrad struct amdgpu_virt_fw_reserve fw_reserve; 259 1.1 riastrad uint32_t gim_feature; 260 1.1 riastrad uint32_t reg_access_mode; 261 1.1 riastrad }; 262 1.1 riastrad 263 1.1 riastrad #define amdgpu_sriov_enabled(adev) \ 264 1.1 riastrad ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_ENABLE_IOV) 265 1.1 riastrad 266 1.1 riastrad #define amdgpu_sriov_vf(adev) \ 267 1.1 riastrad ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_IS_VF) 268 1.1 riastrad 269 1.1 riastrad #define amdgpu_sriov_bios(adev) \ 270 1.1 riastrad ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS) 271 1.1 riastrad 272 1.1 riastrad #define amdgpu_sriov_runtime(adev) \ 273 1.1 riastrad ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_RUNTIME) 274 1.1 riastrad 275 1.1 riastrad #define amdgpu_passthrough(adev) \ 276 1.1 riastrad ((adev)->virt.caps & AMDGPU_PASSTHROUGH_MODE) 277 1.1 riastrad 278 1.1 riastrad static inline bool is_virtual_machine(void) 279 1.1 riastrad { 280 1.1 riastrad #ifdef CONFIG_X86 281 1.3 riastrad #ifdef __NetBSD__ 282 1.3 riastrad return false; /* XXX */ 283 1.3 riastrad #else 284 1.1 riastrad return boot_cpu_has(X86_FEATURE_HYPERVISOR); 285 1.3 riastrad #endif 286 1.1 riastrad #else 287 1.1 riastrad return false; 288 1.1 riastrad #endif 289 1.1 riastrad } 290 1.1 riastrad 291 1.1 riastrad #define amdgpu_sriov_is_pp_one_vf(adev) \ 292 1.1 riastrad ((adev)->virt.gim_feature & AMDGIM_FEATURE_PP_ONE_VF) 293 1.1 riastrad 294 1.1 riastrad bool amdgpu_virt_mmio_blocked(struct amdgpu_device *adev); 295 1.1 riastrad void amdgpu_virt_init_setting(struct amdgpu_device *adev); 296 1.1 riastrad void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev, 297 1.1 riastrad uint32_t reg0, uint32_t rreg1, 298 1.1 riastrad uint32_t ref, uint32_t mask); 299 1.1 riastrad int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init); 300 1.1 riastrad int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init); 301 1.1 riastrad int amdgpu_virt_reset_gpu(struct amdgpu_device *adev); 302 1.1 riastrad int amdgpu_virt_wait_reset(struct amdgpu_device *adev); 303 1.1 riastrad int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev); 304 1.1 riastrad void amdgpu_virt_free_mm_table(struct amdgpu_device *adev); 305 1.1 riastrad int amdgpu_virt_fw_reserve_get_checksum(void *obj, unsigned long obj_size, 306 1.1 riastrad unsigned int key, 307 1.1 riastrad unsigned int chksum); 308 1.1 riastrad void amdgpu_virt_init_data_exchange(struct amdgpu_device *adev); 309 1.1 riastrad #endif 310