amdgpu_amdkfd.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "amdgpu_gfx.h"
  27. #include <linux/module.h>
  28. const struct kgd2kfd_calls *kgd2kfd;
  29. bool (*kgd2kfd_init_p)(unsigned int, const struct kgd2kfd_calls**);
  30. int amdgpu_amdkfd_init(void)
  31. {
  32. int ret;
  33. #if defined(CONFIG_HSA_AMD_MODULE)
  34. int (*kgd2kfd_init_p)(unsigned int, const struct kgd2kfd_calls**);
  35. kgd2kfd_init_p = symbol_request(kgd2kfd_init);
  36. if (kgd2kfd_init_p == NULL)
  37. return -ENOENT;
  38. ret = kgd2kfd_init_p(KFD_INTERFACE_VERSION, &kgd2kfd);
  39. if (ret) {
  40. symbol_put(kgd2kfd_init);
  41. kgd2kfd = NULL;
  42. }
  43. #elif defined(CONFIG_HSA_AMD)
  44. ret = kgd2kfd_init(KFD_INTERFACE_VERSION, &kgd2kfd);
  45. if (ret)
  46. kgd2kfd = NULL;
  47. #else
  48. ret = -ENOENT;
  49. #endif
  50. return ret;
  51. }
  52. void amdgpu_amdkfd_fini(void)
  53. {
  54. if (kgd2kfd) {
  55. kgd2kfd->exit();
  56. symbol_put(kgd2kfd_init);
  57. }
  58. }
  59. void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev)
  60. {
  61. const struct kfd2kgd_calls *kfd2kgd;
  62. if (!kgd2kfd)
  63. return;
  64. switch (adev->asic_type) {
  65. #ifdef CONFIG_DRM_AMDGPU_CIK
  66. case CHIP_KAVERI:
  67. kfd2kgd = amdgpu_amdkfd_gfx_7_get_functions();
  68. break;
  69. #endif
  70. case CHIP_CARRIZO:
  71. kfd2kgd = amdgpu_amdkfd_gfx_8_0_get_functions();
  72. break;
  73. default:
  74. dev_info(adev->dev, "kfd not supported on this ASIC\n");
  75. return;
  76. }
  77. adev->kfd = kgd2kfd->probe((struct kgd_dev *)adev,
  78. adev->pdev, kfd2kgd);
  79. }
  80. void amdgpu_amdkfd_device_init(struct amdgpu_device *adev)
  81. {
  82. int i;
  83. int last_valid_bit;
  84. if (adev->kfd) {
  85. struct kgd2kfd_shared_resources gpu_resources = {
  86. .compute_vmid_bitmap = 0xFF00,
  87. .num_pipe_per_mec = adev->gfx.mec.num_pipe_per_mec,
  88. .num_queue_per_pipe = adev->gfx.mec.num_queue_per_pipe
  89. };
  90. /* this is going to have a few of the MSBs set that we need to
  91. * clear */
  92. bitmap_complement(gpu_resources.queue_bitmap,
  93. adev->gfx.mec.queue_bitmap,
  94. KGD_MAX_QUEUES);
  95. /* remove the KIQ bit as well */
  96. if (adev->gfx.kiq.ring.ready)
  97. clear_bit(amdgpu_gfx_queue_to_bit(adev,
  98. adev->gfx.kiq.ring.me - 1,
  99. adev->gfx.kiq.ring.pipe,
  100. adev->gfx.kiq.ring.queue),
  101. gpu_resources.queue_bitmap);
  102. /* According to linux/bitmap.h we shouldn't use bitmap_clear if
  103. * nbits is not compile time constant */
  104. last_valid_bit = 1 /* only first MEC can have compute queues */
  105. * adev->gfx.mec.num_pipe_per_mec
  106. * adev->gfx.mec.num_queue_per_pipe;
  107. for (i = last_valid_bit; i < KGD_MAX_QUEUES; ++i)
  108. clear_bit(i, gpu_resources.queue_bitmap);
  109. amdgpu_doorbell_get_kfd_info(adev,
  110. &gpu_resources.doorbell_physical_address,
  111. &gpu_resources.doorbell_aperture_size,
  112. &gpu_resources.doorbell_start_offset);
  113. kgd2kfd->device_init(adev->kfd, &gpu_resources);
  114. }
  115. }
  116. void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev)
  117. {
  118. if (adev->kfd) {
  119. kgd2kfd->device_exit(adev->kfd);
  120. adev->kfd = NULL;
  121. }
  122. }
  123. void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
  124. const void *ih_ring_entry)
  125. {
  126. if (adev->kfd)
  127. kgd2kfd->interrupt(adev->kfd, ih_ring_entry);
  128. }
  129. void amdgpu_amdkfd_suspend(struct amdgpu_device *adev)
  130. {
  131. if (adev->kfd)
  132. kgd2kfd->suspend(adev->kfd);
  133. }
  134. int amdgpu_amdkfd_resume(struct amdgpu_device *adev)
  135. {
  136. int r = 0;
  137. if (adev->kfd)
  138. r = kgd2kfd->resume(adev->kfd);
  139. return r;
  140. }
  141. int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
  142. void **mem_obj, uint64_t *gpu_addr,
  143. void **cpu_ptr)
  144. {
  145. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  146. struct kgd_mem **mem = (struct kgd_mem **) mem_obj;
  147. int r;
  148. BUG_ON(kgd == NULL);
  149. BUG_ON(gpu_addr == NULL);
  150. BUG_ON(cpu_ptr == NULL);
  151. *mem = kmalloc(sizeof(struct kgd_mem), GFP_KERNEL);
  152. if ((*mem) == NULL)
  153. return -ENOMEM;
  154. r = amdgpu_bo_create(adev, size, PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_GTT,
  155. AMDGPU_GEM_CREATE_CPU_GTT_USWC, NULL, NULL, 0,
  156. &(*mem)->bo);
  157. if (r) {
  158. dev_err(adev->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(adev->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(adev->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(adev->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 *adev =
  206. (struct amdgpu_device *)kgd;
  207. BUG_ON(kgd == NULL);
  208. return adev->mc.real_vram_size;
  209. }
  210. uint64_t get_gpu_clock_counter(struct kgd_dev *kgd)
  211. {
  212. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  213. if (adev->gfx.funcs->get_gpu_clock_counter)
  214. return adev->gfx.funcs->get_gpu_clock_counter(adev);
  215. return 0;
  216. }
  217. uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd)
  218. {
  219. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  220. /* the sclk is in quantas of 10kHz */
  221. if (amdgpu_sriov_vf(adev))
  222. return adev->clock.default_sclk / 100;
  223. return amdgpu_dpm_get_sclk(adev, false) / 100;
  224. }