amdgpu_amdkfd.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2014 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. #include "amdgpu_amdkfd.h"
  23. #include "amd_shared.h"
  24. #include <drm/drmP.h>
  25. #include "amdgpu.h"
  26. #include <linux/module.h>
  27. const struct kfd2kgd_calls *kfd2kgd;
  28. const struct kgd2kfd_calls *kgd2kfd;
  29. bool (*kgd2kfd_init_p)(unsigned, const struct kgd2kfd_calls**);
  30. bool amdgpu_amdkfd_init(void)
  31. {
  32. #if defined(CONFIG_HSA_AMD_MODULE)
  33. bool (*kgd2kfd_init_p)(unsigned, const struct kgd2kfd_calls**);
  34. kgd2kfd_init_p = symbol_request(kgd2kfd_init);
  35. if (kgd2kfd_init_p == NULL)
  36. return false;
  37. #endif
  38. return true;
  39. }
  40. bool amdgpu_amdkfd_load_interface(struct amdgpu_device *rdev)
  41. {
  42. #if defined(CONFIG_HSA_AMD_MODULE)
  43. bool (*kgd2kfd_init_p)(unsigned, const struct kgd2kfd_calls**);
  44. #endif
  45. switch (rdev->asic_type) {
  46. #ifdef CONFIG_DRM_AMDGPU_CIK
  47. case CHIP_KAVERI:
  48. kfd2kgd = amdgpu_amdkfd_gfx_7_get_functions();
  49. break;
  50. #endif
  51. case CHIP_CARRIZO:
  52. kfd2kgd = amdgpu_amdkfd_gfx_8_0_get_functions();
  53. break;
  54. default:
  55. return false;
  56. }
  57. #if defined(CONFIG_HSA_AMD_MODULE)
  58. kgd2kfd_init_p = symbol_request(kgd2kfd_init);
  59. if (kgd2kfd_init_p == NULL) {
  60. kfd2kgd = NULL;
  61. return false;
  62. }
  63. if (!kgd2kfd_init_p(KFD_INTERFACE_VERSION, &kgd2kfd)) {
  64. symbol_put(kgd2kfd_init);
  65. kfd2kgd = NULL;
  66. kgd2kfd = NULL;
  67. return false;
  68. }
  69. return true;
  70. #elif defined(CONFIG_HSA_AMD)
  71. if (!kgd2kfd_init(KFD_INTERFACE_VERSION, &kgd2kfd)) {
  72. kfd2kgd = NULL;
  73. kgd2kfd = NULL;
  74. return false;
  75. }
  76. return true;
  77. #else
  78. kfd2kgd = NULL;
  79. return false;
  80. #endif
  81. }
  82. void amdgpu_amdkfd_fini(void)
  83. {
  84. if (kgd2kfd) {
  85. kgd2kfd->exit();
  86. symbol_put(kgd2kfd_init);
  87. }
  88. }
  89. void amdgpu_amdkfd_device_probe(struct amdgpu_device *rdev)
  90. {
  91. if (kgd2kfd)
  92. rdev->kfd = kgd2kfd->probe((struct kgd_dev *)rdev,
  93. rdev->pdev, kfd2kgd);
  94. }
  95. void amdgpu_amdkfd_device_init(struct amdgpu_device *rdev)
  96. {
  97. if (rdev->kfd) {
  98. struct kgd2kfd_shared_resources gpu_resources = {
  99. .compute_vmid_bitmap = 0xFF00,
  100. .first_compute_pipe = 1,
  101. .compute_pipe_count = 4 - 1,
  102. };
  103. amdgpu_doorbell_get_kfd_info(rdev,
  104. &gpu_resources.doorbell_physical_address,
  105. &gpu_resources.doorbell_aperture_size,
  106. &gpu_resources.doorbell_start_offset);
  107. kgd2kfd->device_init(rdev->kfd, &gpu_resources);
  108. }
  109. }
  110. void amdgpu_amdkfd_device_fini(struct amdgpu_device *rdev)
  111. {
  112. if (rdev->kfd) {
  113. kgd2kfd->device_exit(rdev->kfd);
  114. rdev->kfd = NULL;
  115. }
  116. }
  117. void amdgpu_amdkfd_interrupt(struct amdgpu_device *rdev,
  118. const void *ih_ring_entry)
  119. {
  120. if (rdev->kfd)
  121. kgd2kfd->interrupt(rdev->kfd, ih_ring_entry);
  122. }
  123. void amdgpu_amdkfd_suspend(struct amdgpu_device *rdev)
  124. {
  125. if (rdev->kfd)
  126. kgd2kfd->suspend(rdev->kfd);
  127. }
  128. int amdgpu_amdkfd_resume(struct amdgpu_device *rdev)
  129. {
  130. int r = 0;
  131. if (rdev->kfd)
  132. r = kgd2kfd->resume(rdev->kfd);
  133. return r;
  134. }
  135. u32 pool_to_domain(enum kgd_memory_pool p)
  136. {
  137. switch (p) {
  138. case KGD_POOL_FRAMEBUFFER: return AMDGPU_GEM_DOMAIN_VRAM;
  139. default: return AMDGPU_GEM_DOMAIN_GTT;
  140. }
  141. }
  142. int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
  143. void **mem_obj, uint64_t *gpu_addr,
  144. void **cpu_ptr)
  145. {
  146. struct amdgpu_device *rdev = (struct amdgpu_device *)kgd;
  147. struct kgd_mem **mem = (struct kgd_mem **) mem_obj;
  148. int r;
  149. BUG_ON(kgd == NULL);
  150. BUG_ON(gpu_addr == NULL);
  151. BUG_ON(cpu_ptr == NULL);
  152. *mem = kmalloc(sizeof(struct kgd_mem), GFP_KERNEL);
  153. if ((*mem) == NULL)
  154. return -ENOMEM;
  155. r = amdgpu_bo_create(rdev, size, PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_GTT,
  156. AMDGPU_GEM_CREATE_CPU_GTT_USWC, NULL, NULL, &(*mem)->bo);
  157. if (r) {
  158. dev_err(rdev->dev,
  159. "failed to allocate BO for amdkfd (%d)\n", r);
  160. return r;
  161. }
  162. /* map the buffer */
  163. r = amdgpu_bo_reserve((*mem)->bo, true);
  164. if (r) {
  165. dev_err(rdev->dev, "(%d) failed to reserve bo for amdkfd\n", r);
  166. goto allocate_mem_reserve_bo_failed;
  167. }
  168. r = amdgpu_bo_pin((*mem)->bo, AMDGPU_GEM_DOMAIN_GTT,
  169. &(*mem)->gpu_addr);
  170. if (r) {
  171. dev_err(rdev->dev, "(%d) failed to pin bo for amdkfd\n", r);
  172. goto allocate_mem_pin_bo_failed;
  173. }
  174. *gpu_addr = (*mem)->gpu_addr;
  175. r = amdgpu_bo_kmap((*mem)->bo, &(*mem)->cpu_ptr);
  176. if (r) {
  177. dev_err(rdev->dev,
  178. "(%d) failed to map bo to kernel for amdkfd\n", r);
  179. goto allocate_mem_kmap_bo_failed;
  180. }
  181. *cpu_ptr = (*mem)->cpu_ptr;
  182. amdgpu_bo_unreserve((*mem)->bo);
  183. return 0;
  184. allocate_mem_kmap_bo_failed:
  185. amdgpu_bo_unpin((*mem)->bo);
  186. allocate_mem_pin_bo_failed:
  187. amdgpu_bo_unreserve((*mem)->bo);
  188. allocate_mem_reserve_bo_failed:
  189. amdgpu_bo_unref(&(*mem)->bo);
  190. return r;
  191. }
  192. void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj)
  193. {
  194. struct kgd_mem *mem = (struct kgd_mem *) mem_obj;
  195. BUG_ON(mem == NULL);
  196. amdgpu_bo_reserve(mem->bo, true);
  197. amdgpu_bo_kunmap(mem->bo);
  198. amdgpu_bo_unpin(mem->bo);
  199. amdgpu_bo_unreserve(mem->bo);
  200. amdgpu_bo_unref(&(mem->bo));
  201. kfree(mem);
  202. }
  203. uint64_t get_vmem_size(struct kgd_dev *kgd)
  204. {
  205. struct amdgpu_device *rdev =
  206. (struct amdgpu_device *)kgd;
  207. BUG_ON(kgd == NULL);
  208. return rdev->mc.real_vram_size;
  209. }
  210. uint64_t get_gpu_clock_counter(struct kgd_dev *kgd)
  211. {
  212. struct amdgpu_device *rdev = (struct amdgpu_device *)kgd;
  213. if (rdev->asic_funcs->get_gpu_clock_counter)
  214. return rdev->asic_funcs->get_gpu_clock_counter(rdev);
  215. return 0;
  216. }
  217. uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd)
  218. {
  219. struct amdgpu_device *rdev = (struct amdgpu_device *)kgd;
  220. /* The sclk is in quantas of 10kHz */
  221. return rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk / 100;
  222. }