amdgpu_amdkfd.c 15 KB

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