amdgpu_amdkfd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. static const unsigned int compute_vmid_bitmap = 0xFF00;
  31. int amdgpu_amdkfd_init(void)
  32. {
  33. int ret;
  34. #if defined(CONFIG_HSA_AMD_MODULE)
  35. int (*kgd2kfd_init_p)(unsigned int, 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. kgd2kfd = NULL;
  50. ret = -ENOENT;
  51. #endif
  52. #if defined(CONFIG_HSA_AMD_MODULE) || defined(CONFIG_HSA_AMD)
  53. amdgpu_amdkfd_gpuvm_init_mem_limits();
  54. #endif
  55. return ret;
  56. }
  57. void amdgpu_amdkfd_fini(void)
  58. {
  59. if (kgd2kfd) {
  60. kgd2kfd->exit();
  61. symbol_put(kgd2kfd_init);
  62. }
  63. }
  64. void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev)
  65. {
  66. const struct kfd2kgd_calls *kfd2kgd;
  67. if (!kgd2kfd)
  68. return;
  69. switch (adev->asic_type) {
  70. #ifdef CONFIG_DRM_AMDGPU_CIK
  71. case CHIP_KAVERI:
  72. case CHIP_HAWAII:
  73. kfd2kgd = amdgpu_amdkfd_gfx_7_get_functions();
  74. break;
  75. #endif
  76. case CHIP_CARRIZO:
  77. case CHIP_TONGA:
  78. case CHIP_FIJI:
  79. case CHIP_POLARIS10:
  80. case CHIP_POLARIS11:
  81. kfd2kgd = amdgpu_amdkfd_gfx_8_0_get_functions();
  82. break;
  83. case CHIP_VEGA10:
  84. case CHIP_RAVEN:
  85. kfd2kgd = amdgpu_amdkfd_gfx_9_0_get_functions();
  86. break;
  87. default:
  88. dev_info(adev->dev, "kfd not supported on this ASIC\n");
  89. return;
  90. }
  91. adev->kfd = kgd2kfd->probe((struct kgd_dev *)adev,
  92. adev->pdev, kfd2kgd);
  93. }
  94. /**
  95. * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
  96. * setup amdkfd
  97. *
  98. * @adev: amdgpu_device pointer
  99. * @aperture_base: output returning doorbell aperture base physical address
  100. * @aperture_size: output returning doorbell aperture size in bytes
  101. * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
  102. *
  103. * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
  104. * takes doorbells required for its own rings and reports the setup to amdkfd.
  105. * amdgpu reserved doorbells are at the start of the doorbell aperture.
  106. */
  107. static void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
  108. phys_addr_t *aperture_base,
  109. size_t *aperture_size,
  110. size_t *start_offset)
  111. {
  112. /*
  113. * The first num_doorbells are used by amdgpu.
  114. * amdkfd takes whatever's left in the aperture.
  115. */
  116. if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
  117. *aperture_base = adev->doorbell.base;
  118. *aperture_size = adev->doorbell.size;
  119. *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
  120. } else {
  121. *aperture_base = 0;
  122. *aperture_size = 0;
  123. *start_offset = 0;
  124. }
  125. }
  126. void amdgpu_amdkfd_device_init(struct amdgpu_device *adev)
  127. {
  128. int i;
  129. int last_valid_bit;
  130. if (adev->kfd) {
  131. struct kgd2kfd_shared_resources gpu_resources = {
  132. .compute_vmid_bitmap = compute_vmid_bitmap,
  133. .num_pipe_per_mec = adev->gfx.mec.num_pipe_per_mec,
  134. .num_queue_per_pipe = adev->gfx.mec.num_queue_per_pipe,
  135. .gpuvm_size = min(adev->vm_manager.max_pfn
  136. << AMDGPU_GPU_PAGE_SHIFT,
  137. AMDGPU_VA_HOLE_START),
  138. .drm_render_minor = adev->ddev->render->index
  139. };
  140. /* this is going to have a few of the MSBs set that we need to
  141. * clear */
  142. bitmap_complement(gpu_resources.queue_bitmap,
  143. adev->gfx.mec.queue_bitmap,
  144. KGD_MAX_QUEUES);
  145. /* remove the KIQ bit as well */
  146. if (adev->gfx.kiq.ring.ready)
  147. clear_bit(amdgpu_gfx_queue_to_bit(adev,
  148. adev->gfx.kiq.ring.me - 1,
  149. adev->gfx.kiq.ring.pipe,
  150. adev->gfx.kiq.ring.queue),
  151. gpu_resources.queue_bitmap);
  152. /* According to linux/bitmap.h we shouldn't use bitmap_clear if
  153. * nbits is not compile time constant */
  154. last_valid_bit = 1 /* only first MEC can have compute queues */
  155. * adev->gfx.mec.num_pipe_per_mec
  156. * adev->gfx.mec.num_queue_per_pipe;
  157. for (i = last_valid_bit; i < KGD_MAX_QUEUES; ++i)
  158. clear_bit(i, gpu_resources.queue_bitmap);
  159. amdgpu_doorbell_get_kfd_info(adev,
  160. &gpu_resources.doorbell_physical_address,
  161. &gpu_resources.doorbell_aperture_size,
  162. &gpu_resources.doorbell_start_offset);
  163. if (adev->asic_type >= CHIP_VEGA10) {
  164. /* On SOC15 the BIF is involved in routing
  165. * doorbells using the low 12 bits of the
  166. * address. Communicate the assignments to
  167. * KFD. KFD uses two doorbell pages per
  168. * process in case of 64-bit doorbells so we
  169. * can use each doorbell assignment twice.
  170. */
  171. gpu_resources.sdma_doorbell[0][0] =
  172. AMDGPU_DOORBELL64_sDMA_ENGINE0;
  173. gpu_resources.sdma_doorbell[0][1] =
  174. AMDGPU_DOORBELL64_sDMA_ENGINE0 + 0x200;
  175. gpu_resources.sdma_doorbell[1][0] =
  176. AMDGPU_DOORBELL64_sDMA_ENGINE1;
  177. gpu_resources.sdma_doorbell[1][1] =
  178. AMDGPU_DOORBELL64_sDMA_ENGINE1 + 0x200;
  179. /* Doorbells 0x0f0-0ff and 0x2f0-2ff are reserved for
  180. * SDMA, IH and VCN. So don't use them for the CP.
  181. */
  182. gpu_resources.reserved_doorbell_mask = 0x1f0;
  183. gpu_resources.reserved_doorbell_val = 0x0f0;
  184. }
  185. kgd2kfd->device_init(adev->kfd, &gpu_resources);
  186. }
  187. }
  188. void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev)
  189. {
  190. if (adev->kfd) {
  191. kgd2kfd->device_exit(adev->kfd);
  192. adev->kfd = NULL;
  193. }
  194. }
  195. void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
  196. const void *ih_ring_entry)
  197. {
  198. if (adev->kfd)
  199. kgd2kfd->interrupt(adev->kfd, ih_ring_entry);
  200. }
  201. void amdgpu_amdkfd_suspend(struct amdgpu_device *adev)
  202. {
  203. if (adev->kfd)
  204. kgd2kfd->suspend(adev->kfd);
  205. }
  206. int amdgpu_amdkfd_resume(struct amdgpu_device *adev)
  207. {
  208. int r = 0;
  209. if (adev->kfd)
  210. r = kgd2kfd->resume(adev->kfd);
  211. return r;
  212. }
  213. int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev)
  214. {
  215. int r = 0;
  216. if (adev->kfd)
  217. r = kgd2kfd->pre_reset(adev->kfd);
  218. return r;
  219. }
  220. int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev)
  221. {
  222. int r = 0;
  223. if (adev->kfd)
  224. r = kgd2kfd->post_reset(adev->kfd);
  225. return r;
  226. }
  227. void amdgpu_amdkfd_gpu_reset(struct kgd_dev *kgd)
  228. {
  229. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  230. amdgpu_device_gpu_recover(adev, NULL, false);
  231. }
  232. int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
  233. void **mem_obj, uint64_t *gpu_addr,
  234. void **cpu_ptr, bool mqd_gfx9)
  235. {
  236. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  237. struct amdgpu_bo *bo = NULL;
  238. struct amdgpu_bo_param bp;
  239. int r;
  240. void *cpu_ptr_tmp = NULL;
  241. memset(&bp, 0, sizeof(bp));
  242. bp.size = size;
  243. bp.byte_align = PAGE_SIZE;
  244. bp.domain = AMDGPU_GEM_DOMAIN_GTT;
  245. bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  246. bp.type = ttm_bo_type_kernel;
  247. bp.resv = NULL;
  248. if (mqd_gfx9)
  249. bp.flags |= AMDGPU_GEM_CREATE_MQD_GFX9;
  250. r = amdgpu_bo_create(adev, &bp, &bo);
  251. if (r) {
  252. dev_err(adev->dev,
  253. "failed to allocate BO for amdkfd (%d)\n", r);
  254. return r;
  255. }
  256. /* map the buffer */
  257. r = amdgpu_bo_reserve(bo, true);
  258. if (r) {
  259. dev_err(adev->dev, "(%d) failed to reserve bo for amdkfd\n", r);
  260. goto allocate_mem_reserve_bo_failed;
  261. }
  262. r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
  263. if (r) {
  264. dev_err(adev->dev, "(%d) failed to pin bo for amdkfd\n", r);
  265. goto allocate_mem_pin_bo_failed;
  266. }
  267. r = amdgpu_ttm_alloc_gart(&bo->tbo);
  268. if (r) {
  269. dev_err(adev->dev, "%p bind failed\n", bo);
  270. goto allocate_mem_kmap_bo_failed;
  271. }
  272. r = amdgpu_bo_kmap(bo, &cpu_ptr_tmp);
  273. if (r) {
  274. dev_err(adev->dev,
  275. "(%d) failed to map bo to kernel for amdkfd\n", r);
  276. goto allocate_mem_kmap_bo_failed;
  277. }
  278. *mem_obj = bo;
  279. *gpu_addr = amdgpu_bo_gpu_offset(bo);
  280. *cpu_ptr = cpu_ptr_tmp;
  281. amdgpu_bo_unreserve(bo);
  282. return 0;
  283. allocate_mem_kmap_bo_failed:
  284. amdgpu_bo_unpin(bo);
  285. allocate_mem_pin_bo_failed:
  286. amdgpu_bo_unreserve(bo);
  287. allocate_mem_reserve_bo_failed:
  288. amdgpu_bo_unref(&bo);
  289. return r;
  290. }
  291. void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj)
  292. {
  293. struct amdgpu_bo *bo = (struct amdgpu_bo *) mem_obj;
  294. amdgpu_bo_reserve(bo, true);
  295. amdgpu_bo_kunmap(bo);
  296. amdgpu_bo_unpin(bo);
  297. amdgpu_bo_unreserve(bo);
  298. amdgpu_bo_unref(&(bo));
  299. }
  300. void get_local_mem_info(struct kgd_dev *kgd,
  301. struct kfd_local_mem_info *mem_info)
  302. {
  303. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  304. uint64_t address_mask = adev->dev->dma_mask ? ~*adev->dev->dma_mask :
  305. ~((1ULL << 32) - 1);
  306. resource_size_t aper_limit = adev->gmc.aper_base + adev->gmc.aper_size;
  307. memset(mem_info, 0, sizeof(*mem_info));
  308. if (!(adev->gmc.aper_base & address_mask || aper_limit & address_mask)) {
  309. mem_info->local_mem_size_public = adev->gmc.visible_vram_size;
  310. mem_info->local_mem_size_private = adev->gmc.real_vram_size -
  311. adev->gmc.visible_vram_size;
  312. } else {
  313. mem_info->local_mem_size_public = 0;
  314. mem_info->local_mem_size_private = adev->gmc.real_vram_size;
  315. }
  316. mem_info->vram_width = adev->gmc.vram_width;
  317. pr_debug("Address base: %pap limit %pap public 0x%llx private 0x%llx\n",
  318. &adev->gmc.aper_base, &aper_limit,
  319. mem_info->local_mem_size_public,
  320. mem_info->local_mem_size_private);
  321. if (amdgpu_sriov_vf(adev))
  322. mem_info->mem_clk_max = adev->clock.default_mclk / 100;
  323. else if (adev->powerplay.pp_funcs)
  324. mem_info->mem_clk_max = amdgpu_dpm_get_mclk(adev, false) / 100;
  325. else
  326. mem_info->mem_clk_max = 100;
  327. }
  328. uint64_t get_gpu_clock_counter(struct kgd_dev *kgd)
  329. {
  330. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  331. if (adev->gfx.funcs->get_gpu_clock_counter)
  332. return adev->gfx.funcs->get_gpu_clock_counter(adev);
  333. return 0;
  334. }
  335. uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd)
  336. {
  337. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  338. /* the sclk is in quantas of 10kHz */
  339. if (amdgpu_sriov_vf(adev))
  340. return adev->clock.default_sclk / 100;
  341. else if (adev->powerplay.pp_funcs)
  342. return amdgpu_dpm_get_sclk(adev, false) / 100;
  343. else
  344. return 100;
  345. }
  346. void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info)
  347. {
  348. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  349. struct amdgpu_cu_info acu_info = adev->gfx.cu_info;
  350. memset(cu_info, 0, sizeof(*cu_info));
  351. if (sizeof(cu_info->cu_bitmap) != sizeof(acu_info.bitmap))
  352. return;
  353. cu_info->cu_active_number = acu_info.number;
  354. cu_info->cu_ao_mask = acu_info.ao_cu_mask;
  355. memcpy(&cu_info->cu_bitmap[0], &acu_info.bitmap[0],
  356. sizeof(acu_info.bitmap));
  357. cu_info->num_shader_engines = adev->gfx.config.max_shader_engines;
  358. cu_info->num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
  359. cu_info->num_cu_per_sh = adev->gfx.config.max_cu_per_sh;
  360. cu_info->simd_per_cu = acu_info.simd_per_cu;
  361. cu_info->max_waves_per_simd = acu_info.max_waves_per_simd;
  362. cu_info->wave_front_size = acu_info.wave_front_size;
  363. cu_info->max_scratch_slots_per_cu = acu_info.max_scratch_slots_per_cu;
  364. cu_info->lds_size = acu_info.lds_size;
  365. }
  366. uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd)
  367. {
  368. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  369. return amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
  370. }
  371. int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine,
  372. uint32_t vmid, uint64_t gpu_addr,
  373. uint32_t *ib_cmd, uint32_t ib_len)
  374. {
  375. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  376. struct amdgpu_job *job;
  377. struct amdgpu_ib *ib;
  378. struct amdgpu_ring *ring;
  379. struct dma_fence *f = NULL;
  380. int ret;
  381. switch (engine) {
  382. case KGD_ENGINE_MEC1:
  383. ring = &adev->gfx.compute_ring[0];
  384. break;
  385. case KGD_ENGINE_SDMA1:
  386. ring = &adev->sdma.instance[0].ring;
  387. break;
  388. case KGD_ENGINE_SDMA2:
  389. ring = &adev->sdma.instance[1].ring;
  390. break;
  391. default:
  392. pr_err("Invalid engine in IB submission: %d\n", engine);
  393. ret = -EINVAL;
  394. goto err;
  395. }
  396. ret = amdgpu_job_alloc(adev, 1, &job, NULL);
  397. if (ret)
  398. goto err;
  399. ib = &job->ibs[0];
  400. memset(ib, 0, sizeof(struct amdgpu_ib));
  401. ib->gpu_addr = gpu_addr;
  402. ib->ptr = ib_cmd;
  403. ib->length_dw = ib_len;
  404. /* This works for NO_HWS. TODO: need to handle without knowing VMID */
  405. job->vmid = vmid;
  406. ret = amdgpu_ib_schedule(ring, 1, ib, job, &f);
  407. if (ret) {
  408. DRM_ERROR("amdgpu: failed to schedule IB.\n");
  409. goto err_ib_sched;
  410. }
  411. ret = dma_fence_wait(f, false);
  412. err_ib_sched:
  413. dma_fence_put(f);
  414. amdgpu_job_free(job);
  415. err:
  416. return ret;
  417. }
  418. void amdgpu_amdkfd_set_compute_idle(struct kgd_dev *kgd, bool idle)
  419. {
  420. struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
  421. if (adev->powerplay.pp_funcs &&
  422. adev->powerplay.pp_funcs->switch_power_profile)
  423. amdgpu_dpm_switch_power_profile(adev,
  424. PP_SMC_POWER_PROFILE_COMPUTE,
  425. !idle);
  426. }
  427. bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid)
  428. {
  429. if (adev->kfd) {
  430. if ((1 << vmid) & compute_vmid_bitmap)
  431. return true;
  432. }
  433. return false;
  434. }
  435. #if !defined(CONFIG_HSA_AMD_MODULE) && !defined(CONFIG_HSA_AMD)
  436. bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm)
  437. {
  438. return false;
  439. }
  440. void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo)
  441. {
  442. }
  443. void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
  444. struct amdgpu_vm *vm)
  445. {
  446. }
  447. struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f)
  448. {
  449. return NULL;
  450. }
  451. int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm)
  452. {
  453. return 0;
  454. }
  455. struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void)
  456. {
  457. return NULL;
  458. }
  459. struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void)
  460. {
  461. return NULL;
  462. }
  463. struct kfd2kgd_calls *amdgpu_amdkfd_gfx_9_0_get_functions(void)
  464. {
  465. return NULL;
  466. }
  467. #endif