amdgpu_amdkfd.c 6.6 KB

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