amdgpu_virt.h 10 KB

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