amdgpu_virt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. void (*trans_msg)(struct amdgpu_device *adev, u32 req, u32 data1, u32 data2, u32 data3);
  54. };
  55. /*
  56. * Firmware Reserve Frame buffer
  57. */
  58. struct amdgpu_virt_fw_reserve {
  59. struct amdgim_pf2vf_info_header *p_pf2vf;
  60. struct amdgim_vf2pf_info_header *p_vf2pf;
  61. unsigned int checksum_key;
  62. };
  63. /*
  64. * Defination between PF and VF
  65. * Structures forcibly aligned to 4 to keep the same style as PF.
  66. */
  67. #define AMDGIM_DATAEXCHANGE_OFFSET (64 * 1024)
  68. #define AMDGIM_GET_STRUCTURE_RESERVED_SIZE(total, u8, u16, u32, u64) \
  69. (total - (((u8)+3) / 4 + ((u16)+1) / 2 + (u32) + (u64)*2))
  70. enum AMDGIM_FEATURE_FLAG {
  71. /* GIM supports feature of Error log collecting */
  72. AMDGIM_FEATURE_ERROR_LOG_COLLECT = 0x1,
  73. /* GIM supports feature of loading uCodes */
  74. AMDGIM_FEATURE_GIM_LOAD_UCODES = 0x2,
  75. };
  76. struct amdgim_pf2vf_info_header {
  77. /* the total structure size in byte. */
  78. uint32_t size;
  79. /* version of this structure, written by the GIM */
  80. uint32_t version;
  81. } __aligned(4);
  82. struct amdgim_pf2vf_info_v1 {
  83. /* header contains size and version */
  84. struct amdgim_pf2vf_info_header header;
  85. /* max_width * max_height */
  86. unsigned int uvd_enc_max_pixels_count;
  87. /* 16x16 pixels/sec, codec independent */
  88. unsigned int uvd_enc_max_bandwidth;
  89. /* max_width * max_height */
  90. unsigned int vce_enc_max_pixels_count;
  91. /* 16x16 pixels/sec, codec independent */
  92. unsigned int vce_enc_max_bandwidth;
  93. /* MEC FW position in kb from the start of visible frame buffer */
  94. unsigned int mecfw_kboffset;
  95. /* The features flags of the GIM driver supports. */
  96. unsigned int feature_flags;
  97. /* use private key from mailbox 2 to create chueksum */
  98. unsigned int checksum;
  99. } __aligned(4);
  100. struct amdgim_pf2vf_info_v2 {
  101. /* header contains size and version */
  102. struct amdgim_pf2vf_info_header header;
  103. /* use private key from mailbox 2 to create chueksum */
  104. uint32_t checksum;
  105. /* The features flags of the GIM driver supports. */
  106. uint32_t feature_flags;
  107. /* max_width * max_height */
  108. uint32_t uvd_enc_max_pixels_count;
  109. /* 16x16 pixels/sec, codec independent */
  110. uint32_t uvd_enc_max_bandwidth;
  111. /* max_width * max_height */
  112. uint32_t vce_enc_max_pixels_count;
  113. /* 16x16 pixels/sec, codec independent */
  114. uint32_t vce_enc_max_bandwidth;
  115. /* MEC FW position in kb from the start of VF visible frame buffer */
  116. uint64_t mecfw_kboffset;
  117. /* MEC FW size in KB */
  118. uint32_t mecfw_ksize;
  119. /* UVD FW position in kb from the start of VF visible frame buffer */
  120. uint64_t uvdfw_kboffset;
  121. /* UVD FW size in KB */
  122. uint32_t uvdfw_ksize;
  123. /* VCE FW position in kb from the start of VF visible frame buffer */
  124. uint64_t vcefw_kboffset;
  125. /* VCE FW size in KB */
  126. uint32_t vcefw_ksize;
  127. uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 0, 0, (9 + sizeof(struct amdgim_pf2vf_info_header)/sizeof(uint32_t)), 3)];
  128. } __aligned(4);
  129. struct amdgim_vf2pf_info_header {
  130. /* the total structure size in byte. */
  131. uint32_t size;
  132. /*version of this structure, written by the guest */
  133. uint32_t version;
  134. } __aligned(4);
  135. struct amdgim_vf2pf_info_v1 {
  136. /* header contains size and version */
  137. struct amdgim_vf2pf_info_header header;
  138. /* driver version */
  139. char driver_version[64];
  140. /* driver certification, 1=WHQL, 0=None */
  141. unsigned int driver_cert;
  142. /* guest OS type and version: need a define */
  143. unsigned int os_info;
  144. /* in the unit of 1M */
  145. unsigned int fb_usage;
  146. /* guest gfx engine usage percentage */
  147. unsigned int gfx_usage;
  148. /* guest gfx engine health percentage */
  149. unsigned int gfx_health;
  150. /* guest compute engine usage percentage */
  151. unsigned int compute_usage;
  152. /* guest compute engine health percentage */
  153. unsigned int compute_health;
  154. /* guest vce engine usage percentage. 0xffff means N/A. */
  155. unsigned int vce_enc_usage;
  156. /* guest vce engine health percentage. 0xffff means N/A. */
  157. unsigned int vce_enc_health;
  158. /* guest uvd engine usage percentage. 0xffff means N/A. */
  159. unsigned int uvd_enc_usage;
  160. /* guest uvd engine usage percentage. 0xffff means N/A. */
  161. unsigned int uvd_enc_health;
  162. unsigned int checksum;
  163. } __aligned(4);
  164. struct amdgim_vf2pf_info_v2 {
  165. /* header contains size and version */
  166. struct amdgim_vf2pf_info_header header;
  167. uint32_t checksum;
  168. /* driver version */
  169. uint8_t driver_version[64];
  170. /* driver certification, 1=WHQL, 0=None */
  171. uint32_t driver_cert;
  172. /* guest OS type and version: need a define */
  173. uint32_t os_info;
  174. /* in the unit of 1M */
  175. uint32_t fb_usage;
  176. /* guest gfx engine usage percentage */
  177. uint32_t gfx_usage;
  178. /* guest gfx engine health percentage */
  179. uint32_t gfx_health;
  180. /* guest compute engine usage percentage */
  181. uint32_t compute_usage;
  182. /* guest compute engine health percentage */
  183. uint32_t compute_health;
  184. /* guest vce engine usage percentage. 0xffff means N/A. */
  185. uint32_t vce_enc_usage;
  186. /* guest vce engine health percentage. 0xffff means N/A. */
  187. uint32_t vce_enc_health;
  188. /* guest uvd engine usage percentage. 0xffff means N/A. */
  189. uint32_t uvd_enc_usage;
  190. /* guest uvd engine usage percentage. 0xffff means N/A. */
  191. uint32_t uvd_enc_health;
  192. uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 64, 0, (12 + sizeof(struct amdgim_vf2pf_info_header)/sizeof(uint32_t)), 0)];
  193. } __aligned(4);
  194. #define AMDGPU_FW_VRAM_VF2PF_VER 2
  195. typedef struct amdgim_vf2pf_info_v2 amdgim_vf2pf_info ;
  196. #define AMDGPU_FW_VRAM_VF2PF_WRITE(adev, field, val) \
  197. do { \
  198. ((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field = (val); \
  199. } while (0)
  200. #define AMDGPU_FW_VRAM_VF2PF_READ(adev, field, val) \
  201. do { \
  202. (*val) = ((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field; \
  203. } while (0)
  204. #define AMDGPU_FW_VRAM_PF2VF_READ(adev, field, val) \
  205. do { \
  206. if (!adev->virt.fw_reserve.p_pf2vf) \
  207. *(val) = 0; \
  208. else { \
  209. if (adev->virt.fw_reserve.p_pf2vf->version == 1) \
  210. *(val) = ((struct amdgim_pf2vf_info_v1 *)adev->virt.fw_reserve.p_pf2vf)->field; \
  211. if (adev->virt.fw_reserve.p_pf2vf->version == 2) \
  212. *(val) = ((struct amdgim_pf2vf_info_v2 *)adev->virt.fw_reserve.p_pf2vf)->field; \
  213. } \
  214. } while (0)
  215. /* GPU virtualization */
  216. struct amdgpu_virt {
  217. uint32_t caps;
  218. struct amdgpu_bo *csa_obj;
  219. uint64_t csa_vmid0_addr;
  220. bool chained_ib_support;
  221. uint32_t reg_val_offs;
  222. struct mutex lock_reset;
  223. struct amdgpu_irq_src ack_irq;
  224. struct amdgpu_irq_src rcv_irq;
  225. struct work_struct flr_work;
  226. struct amdgpu_mm_table mm_table;
  227. const struct amdgpu_virt_ops *ops;
  228. struct amdgpu_vf_error_buffer vf_errors;
  229. struct amdgpu_virt_fw_reserve fw_reserve;
  230. };
  231. #define AMDGPU_CSA_SIZE (8 * 1024)
  232. #define AMDGPU_CSA_VADDR (AMDGPU_VA_RESERVED_SIZE - AMDGPU_CSA_SIZE)
  233. #define amdgpu_sriov_enabled(adev) \
  234. ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_ENABLE_IOV)
  235. #define amdgpu_sriov_vf(adev) \
  236. ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_IS_VF)
  237. #define amdgpu_sriov_bios(adev) \
  238. ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS)
  239. #define amdgpu_sriov_runtime(adev) \
  240. ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_RUNTIME)
  241. #define amdgpu_passthrough(adev) \
  242. ((adev)->virt.caps & AMDGPU_PASSTHROUGH_MODE)
  243. static inline bool is_virtual_machine(void)
  244. {
  245. #ifdef CONFIG_X86
  246. return boot_cpu_has(X86_FEATURE_HYPERVISOR);
  247. #else
  248. return false;
  249. #endif
  250. }
  251. struct amdgpu_vm;
  252. int amdgpu_allocate_static_csa(struct amdgpu_device *adev);
  253. int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  254. struct amdgpu_bo_va **bo_va);
  255. void amdgpu_virt_init_setting(struct amdgpu_device *adev);
  256. uint32_t amdgpu_virt_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);
  257. void amdgpu_virt_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v);
  258. int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init);
  259. int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init);
  260. int amdgpu_virt_reset_gpu(struct amdgpu_device *adev);
  261. int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job);
  262. int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev);
  263. void amdgpu_virt_free_mm_table(struct amdgpu_device *adev);
  264. int amdgpu_virt_fw_reserve_get_checksum(void *obj, unsigned long obj_size,
  265. unsigned int key,
  266. unsigned int chksum);
  267. void amdgpu_virt_init_data_exchange(struct amdgpu_device *adev);
  268. #endif