amdgpu_virt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. };
  77. struct amdgim_pf2vf_info_header {
  78. /* the total structure size in byte. */
  79. uint32_t size;
  80. /* version of this structure, written by the GIM */
  81. uint32_t version;
  82. } __aligned(4);
  83. struct amdgim_pf2vf_info_v1 {
  84. /* header contains size and version */
  85. struct amdgim_pf2vf_info_header header;
  86. /* max_width * max_height */
  87. unsigned int uvd_enc_max_pixels_count;
  88. /* 16x16 pixels/sec, codec independent */
  89. unsigned int uvd_enc_max_bandwidth;
  90. /* max_width * max_height */
  91. unsigned int vce_enc_max_pixels_count;
  92. /* 16x16 pixels/sec, codec independent */
  93. unsigned int vce_enc_max_bandwidth;
  94. /* MEC FW position in kb from the start of visible frame buffer */
  95. unsigned int mecfw_kboffset;
  96. /* The features flags of the GIM driver supports. */
  97. unsigned int feature_flags;
  98. /* use private key from mailbox 2 to create chueksum */
  99. unsigned int checksum;
  100. } __aligned(4);
  101. struct amdgim_pf2vf_info_v2 {
  102. /* header contains size and version */
  103. struct amdgim_pf2vf_info_header header;
  104. /* use private key from mailbox 2 to create chueksum */
  105. uint32_t checksum;
  106. /* The features flags of the GIM driver supports. */
  107. uint32_t feature_flags;
  108. /* max_width * max_height */
  109. uint32_t uvd_enc_max_pixels_count;
  110. /* 16x16 pixels/sec, codec independent */
  111. uint32_t uvd_enc_max_bandwidth;
  112. /* max_width * max_height */
  113. uint32_t vce_enc_max_pixels_count;
  114. /* 16x16 pixels/sec, codec independent */
  115. uint32_t vce_enc_max_bandwidth;
  116. /* MEC FW position in kb from the start of VF visible frame buffer */
  117. uint64_t mecfw_kboffset;
  118. /* MEC FW size in KB */
  119. uint32_t mecfw_ksize;
  120. /* UVD FW position in kb from the start of VF visible frame buffer */
  121. uint64_t uvdfw_kboffset;
  122. /* UVD FW size in KB */
  123. uint32_t uvdfw_ksize;
  124. /* VCE FW position in kb from the start of VF visible frame buffer */
  125. uint64_t vcefw_kboffset;
  126. /* VCE FW size in KB */
  127. uint32_t vcefw_ksize;
  128. uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 0, 0, (9 + sizeof(struct amdgim_pf2vf_info_header)/sizeof(uint32_t)), 3)];
  129. } __aligned(4);
  130. struct amdgim_vf2pf_info_header {
  131. /* the total structure size in byte. */
  132. uint32_t size;
  133. /*version of this structure, written by the guest */
  134. uint32_t version;
  135. } __aligned(4);
  136. struct amdgim_vf2pf_info_v1 {
  137. /* header contains size and version */
  138. struct amdgim_vf2pf_info_header header;
  139. /* driver version */
  140. char driver_version[64];
  141. /* driver certification, 1=WHQL, 0=None */
  142. unsigned int driver_cert;
  143. /* guest OS type and version: need a define */
  144. unsigned int os_info;
  145. /* in the unit of 1M */
  146. unsigned int fb_usage;
  147. /* guest gfx engine usage percentage */
  148. unsigned int gfx_usage;
  149. /* guest gfx engine health percentage */
  150. unsigned int gfx_health;
  151. /* guest compute engine usage percentage */
  152. unsigned int compute_usage;
  153. /* guest compute engine health percentage */
  154. unsigned int compute_health;
  155. /* guest vce engine usage percentage. 0xffff means N/A. */
  156. unsigned int vce_enc_usage;
  157. /* guest vce engine health percentage. 0xffff means N/A. */
  158. unsigned int vce_enc_health;
  159. /* guest uvd engine usage percentage. 0xffff means N/A. */
  160. unsigned int uvd_enc_usage;
  161. /* guest uvd engine usage percentage. 0xffff means N/A. */
  162. unsigned int uvd_enc_health;
  163. unsigned int checksum;
  164. } __aligned(4);
  165. struct amdgim_vf2pf_info_v2 {
  166. /* header contains size and version */
  167. struct amdgim_vf2pf_info_header header;
  168. uint32_t checksum;
  169. /* driver version */
  170. uint8_t driver_version[64];
  171. /* driver certification, 1=WHQL, 0=None */
  172. uint32_t driver_cert;
  173. /* guest OS type and version: need a define */
  174. uint32_t os_info;
  175. /* in the unit of 1M */
  176. uint32_t fb_usage;
  177. /* guest gfx engine usage percentage */
  178. uint32_t gfx_usage;
  179. /* guest gfx engine health percentage */
  180. uint32_t gfx_health;
  181. /* guest compute engine usage percentage */
  182. uint32_t compute_usage;
  183. /* guest compute engine health percentage */
  184. uint32_t compute_health;
  185. /* guest vce engine usage percentage. 0xffff means N/A. */
  186. uint32_t vce_enc_usage;
  187. /* guest vce engine health percentage. 0xffff means N/A. */
  188. uint32_t vce_enc_health;
  189. /* guest uvd engine usage percentage. 0xffff means N/A. */
  190. uint32_t uvd_enc_usage;
  191. /* guest uvd engine usage percentage. 0xffff means N/A. */
  192. uint32_t uvd_enc_health;
  193. uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 64, 0, (12 + sizeof(struct amdgim_vf2pf_info_header)/sizeof(uint32_t)), 0)];
  194. } __aligned(4);
  195. #define AMDGPU_FW_VRAM_VF2PF_VER 2
  196. typedef struct amdgim_vf2pf_info_v2 amdgim_vf2pf_info ;
  197. #define AMDGPU_FW_VRAM_VF2PF_WRITE(adev, field, val) \
  198. do { \
  199. ((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field = (val); \
  200. } while (0)
  201. #define AMDGPU_FW_VRAM_VF2PF_READ(adev, field, val) \
  202. do { \
  203. (*val) = ((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field; \
  204. } while (0)
  205. #define AMDGPU_FW_VRAM_PF2VF_READ(adev, field, val) \
  206. do { \
  207. if (!adev->virt.fw_reserve.p_pf2vf) \
  208. *(val) = 0; \
  209. else { \
  210. if (adev->virt.fw_reserve.p_pf2vf->version == 1) \
  211. *(val) = ((struct amdgim_pf2vf_info_v1 *)adev->virt.fw_reserve.p_pf2vf)->field; \
  212. if (adev->virt.fw_reserve.p_pf2vf->version == 2) \
  213. *(val) = ((struct amdgim_pf2vf_info_v2 *)adev->virt.fw_reserve.p_pf2vf)->field; \
  214. } \
  215. } while (0)
  216. /* GPU virtualization */
  217. struct amdgpu_virt {
  218. uint32_t caps;
  219. struct amdgpu_bo *csa_obj;
  220. uint64_t csa_vmid0_addr;
  221. bool chained_ib_support;
  222. uint32_t reg_val_offs;
  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. bool amdgpu_virt_mmio_blocked(struct amdgpu_device *adev);
  253. int amdgpu_allocate_static_csa(struct amdgpu_device *adev);
  254. int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  255. struct amdgpu_bo_va **bo_va);
  256. void amdgpu_virt_init_setting(struct amdgpu_device *adev);
  257. uint32_t amdgpu_virt_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);
  258. void amdgpu_virt_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v);
  259. int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init);
  260. int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init);
  261. int amdgpu_virt_reset_gpu(struct amdgpu_device *adev);
  262. int amdgpu_virt_wait_reset(struct amdgpu_device *adev);
  263. int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev);
  264. void amdgpu_virt_free_mm_table(struct amdgpu_device *adev);
  265. int amdgpu_virt_fw_reserve_get_checksum(void *obj, unsigned long obj_size,
  266. unsigned int key,
  267. unsigned int chksum);
  268. void amdgpu_virt_init_data_exchange(struct amdgpu_device *adev);
  269. #endif