amdgpu_amdkfd.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. case CHIP_HAWAII:
  68. kfd2kgd = amdgpu_amdkfd_gfx_7_get_functions();
  69. break;
  70. #endif
  71. case CHIP_CARRIZO:
  72. case CHIP_TONGA:
  73. case CHIP_FIJI:
  74. case CHIP_POLARIS10:
  75. case CHIP_POLARIS11:
  76. kfd2kgd = amdgpu_amdkfd_gfx_8_0_get_functions();
  77. break;
  78. default:
  79. dev_dbg(adev->dev, "kfd not supported on this ASIC\n");
  80. return;
  81. }
  82. adev->kfd = kgd2kfd->probe((struct kgd_dev *)adev,
  83. adev->pdev, kfd2kgd);
  84. }
  85. /**
  86. * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
  87. * setup amdkfd
  88. *
  89. * @adev: amdgpu_device pointer
  90. * @aperture_base: output returning doorbell aperture base physical address
  91. * @aperture_size: output returning doorbell aperture size in bytes
  92. * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
  93. *
  94. * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
  95. * takes doorbells required for its own rings and reports the setup to amdkfd.
  96. * amdgpu reserved doorbells are at the start of the doorbell aperture.
  97. */
  98. static void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
  99. phys_addr_t *aperture_base,
  100. size_t *aperture_size,
  101. size_t *start_offset)
  102. {
  103. /*
  104. * The first num_doorbells are used by amdgpu.
  105. * amdkfd takes whatever's left in the aperture.
  106. */
  107. if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
  108. *aperture_base = adev->doorbell.base;
  109. *aperture_size = adev->doorbell.size;
  110. *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
  111. } else {
  112. *aperture_base = 0;
  113. *aperture_size = 0;
  114. *start_offset = 0;
  115. }
  116. }
  117. void amdgpu_amdkfd_device_init(struct amdgpu_device *adev)
  118. {
  119. int i;
  120. int last_valid_bit;
  121. if (adev->kfd) {
  122. struct kgd2kfd_shared_resources gpu_resources = {
  123. .compute_vmid_bitmap = 0xFF00,
  124. .num_pipe_per_mec = adev->gfx.mec.num_pipe_per_mec,
  125. .num_queue_per_pipe = adev->gfx.mec.num_queue_per_pipe
  126. };
  127. /* this is going to have a few of the MSBs set that we need to
  128. * clear */
  129. bitmap_complement(gpu_resources.queue_bitmap,
  130. adev->gfx.mec.queue_bitmap,
  131. KGD_MAX_QUEUES);
  132. /* remove the KIQ bit as well */
  133. if (adev->gfx.kiq.ring.ready)
  134. clear_bit(amdgpu_gfx_queue_to_bit(adev,
  135. adev->gfx.kiq.ring.me - 1,
  136. adev->gfx.kiq.ring.pipe,
  137. adev->gfx.kiq.ring.queue),
  138. gpu_resources.queue_bitmap);
  139. /* According to linux/bitmap.h we shouldn't use bitmap_clear if
  140. * nbits is not compile time constant */
  141. last_valid_bit = 1 /* only first MEC can have compute queues */
  142. * adev->gfx.mec.num_pipe_per_mec
  143. * adev->gfx.mec.num_queue_per_pipe;
  144. for (i = last_valid_bit; i < KGD_MAX_QUEUES; ++i)
  145. clear_bit(i, gpu_resources.queue_bitmap);
  146. amdgpu_doorbell_get_kfd_info(adev,
  147. &gpu_resources.doorbell_physical_address,
  148. &gpu_resources.doorbell_aperture_size,
  149. &gpu_resources.doorbell_start_offset);
  150. kgd2kfd->device_init(adev->kfd, &gpu_resources);
  151. }
  152. }
  153. void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev)
  154. {
  155. if (adev->kfd) {
  156. kgd2kfd->device_exit(adev->kfd);
  157. adev->kfd = NULL;
  158. }
  159. }
  160. void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
  161. const void *ih_ring_entry)
  162. {
  163. if (adev->kfd)
  164. kgd2kfd->interrupt(adev->kfd, ih_ring_entry);
  165. }
  166. void amdgpu_amdkfd_suspend(struct amdgpu_device *adev)
  167. {
  168. if (adev->kfd)
  169. kgd2kfd->suspend(adev->kfd);
  170. }
  171. int amdgpu_amdkfd_resume(struct amdgpu_device *adev)
  172. {
  173. int r = 0;
  174. if (adev->kfd)
  175. r = kgd2kfd->resume(adev->kfd);
  176. return r;
  177. }
  178. int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
  179. void **mem_obj, uint64_t *gpu_addr,
  180. void **cpu_ptr)
  181. {
  182. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  183. struct kgd_mem **mem = (struct kgd_mem **) mem_obj;
  184. int r;
  185. *mem = kmalloc(sizeof(struct kgd_mem), GFP_KERNEL);
  186. if ((*mem) == NULL)
  187. return -ENOMEM;
  188. r = amdgpu_bo_create(adev, size, PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_GTT,
  189. AMDGPU_GEM_CREATE_CPU_GTT_USWC, NULL, NULL, &(*mem)->bo);
  190. if (r) {
  191. dev_err(adev->dev,
  192. "failed to allocate BO for amdkfd (%d)\n", r);
  193. return r;
  194. }
  195. /* map the buffer */
  196. r = amdgpu_bo_reserve((*mem)->bo, true);
  197. if (r) {
  198. dev_err(adev->dev, "(%d) failed to reserve bo for amdkfd\n", r);
  199. goto allocate_mem_reserve_bo_failed;
  200. }
  201. r = amdgpu_bo_pin((*mem)->bo, AMDGPU_GEM_DOMAIN_GTT,
  202. &(*mem)->gpu_addr);
  203. if (r) {
  204. dev_err(adev->dev, "(%d) failed to pin bo for amdkfd\n", r);
  205. goto allocate_mem_pin_bo_failed;
  206. }
  207. *gpu_addr = (*mem)->gpu_addr;
  208. r = amdgpu_bo_kmap((*mem)->bo, &(*mem)->cpu_ptr);
  209. if (r) {
  210. dev_err(adev->dev,
  211. "(%d) failed to map bo to kernel for amdkfd\n", r);
  212. goto allocate_mem_kmap_bo_failed;
  213. }
  214. *cpu_ptr = (*mem)->cpu_ptr;
  215. amdgpu_bo_unreserve((*mem)->bo);
  216. return 0;
  217. allocate_mem_kmap_bo_failed:
  218. amdgpu_bo_unpin((*mem)->bo);
  219. allocate_mem_pin_bo_failed:
  220. amdgpu_bo_unreserve((*mem)->bo);
  221. allocate_mem_reserve_bo_failed:
  222. amdgpu_bo_unref(&(*mem)->bo);
  223. return r;
  224. }
  225. void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj)
  226. {
  227. struct kgd_mem *mem = (struct kgd_mem *) mem_obj;
  228. amdgpu_bo_reserve(mem->bo, true);
  229. amdgpu_bo_kunmap(mem->bo);
  230. amdgpu_bo_unpin(mem->bo);
  231. amdgpu_bo_unreserve(mem->bo);
  232. amdgpu_bo_unref(&(mem->bo));
  233. kfree(mem);
  234. }
  235. void get_local_mem_info(struct kgd_dev *kgd,
  236. struct kfd_local_mem_info *mem_info)
  237. {
  238. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  239. uint64_t address_mask = adev->dev->dma_mask ? ~*adev->dev->dma_mask :
  240. ~((1ULL << 32) - 1);
  241. resource_size_t aper_limit = adev->gmc.aper_base + adev->gmc.aper_size;
  242. memset(mem_info, 0, sizeof(*mem_info));
  243. if (!(adev->gmc.aper_base & address_mask || aper_limit & address_mask)) {
  244. mem_info->local_mem_size_public = adev->gmc.visible_vram_size;
  245. mem_info->local_mem_size_private = adev->gmc.real_vram_size -
  246. adev->gmc.visible_vram_size;
  247. } else {
  248. mem_info->local_mem_size_public = 0;
  249. mem_info->local_mem_size_private = adev->gmc.real_vram_size;
  250. }
  251. mem_info->vram_width = adev->gmc.vram_width;
  252. pr_debug("Address base: %pap limit %pap public 0x%llx private 0x%llx\n",
  253. &adev->gmc.aper_base, &aper_limit,
  254. mem_info->local_mem_size_public,
  255. mem_info->local_mem_size_private);
  256. if (amdgpu_emu_mode == 1) {
  257. mem_info->mem_clk_max = 100;
  258. return;
  259. }
  260. if (amdgpu_sriov_vf(adev))
  261. mem_info->mem_clk_max = adev->clock.default_mclk / 100;
  262. else
  263. mem_info->mem_clk_max = amdgpu_dpm_get_mclk(adev, false) / 100;
  264. }
  265. uint64_t get_gpu_clock_counter(struct kgd_dev *kgd)
  266. {
  267. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  268. if (adev->gfx.funcs->get_gpu_clock_counter)
  269. return adev->gfx.funcs->get_gpu_clock_counter(adev);
  270. return 0;
  271. }
  272. uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd)
  273. {
  274. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  275. /* the sclk is in quantas of 10kHz */
  276. if (amdgpu_emu_mode == 1)
  277. return 100;
  278. if (amdgpu_sriov_vf(adev))
  279. return adev->clock.default_sclk / 100;
  280. return amdgpu_dpm_get_sclk(adev, false) / 100;
  281. }
  282. void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info)
  283. {
  284. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  285. struct amdgpu_cu_info acu_info = adev->gfx.cu_info;
  286. memset(cu_info, 0, sizeof(*cu_info));
  287. if (sizeof(cu_info->cu_bitmap) != sizeof(acu_info.bitmap))
  288. return;
  289. cu_info->cu_active_number = acu_info.number;
  290. cu_info->cu_ao_mask = acu_info.ao_cu_mask;
  291. memcpy(&cu_info->cu_bitmap[0], &acu_info.bitmap[0],
  292. sizeof(acu_info.bitmap));
  293. cu_info->num_shader_engines = adev->gfx.config.max_shader_engines;
  294. cu_info->num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
  295. cu_info->num_cu_per_sh = adev->gfx.config.max_cu_per_sh;
  296. cu_info->simd_per_cu = acu_info.simd_per_cu;
  297. cu_info->max_waves_per_simd = acu_info.max_waves_per_simd;
  298. cu_info->wave_front_size = acu_info.wave_front_size;
  299. cu_info->max_scratch_slots_per_cu = acu_info.max_scratch_slots_per_cu;
  300. cu_info->lds_size = acu_info.lds_size;
  301. }
  302. uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd)
  303. {
  304. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  305. return amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
  306. }