12f4ca1baSJingyu Wang // SPDX-License-Identifier: MIT
2130e0371SOded Gabbay /*
3130e0371SOded Gabbay * Copyright 2014 Advanced Micro Devices, Inc.
4130e0371SOded Gabbay *
5130e0371SOded Gabbay * Permission is hereby granted, free of charge, to any person obtaining a
6130e0371SOded Gabbay * copy of this software and associated documentation files (the "Software"),
7130e0371SOded Gabbay * to deal in the Software without restriction, including without limitation
8130e0371SOded Gabbay * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9130e0371SOded Gabbay * and/or sell copies of the Software, and to permit persons to whom the
10130e0371SOded Gabbay * Software is furnished to do so, subject to the following conditions:
11130e0371SOded Gabbay *
12130e0371SOded Gabbay * The above copyright notice and this permission notice shall be included in
13130e0371SOded Gabbay * all copies or substantial portions of the Software.
14130e0371SOded Gabbay *
15130e0371SOded Gabbay * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16130e0371SOded Gabbay * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17130e0371SOded Gabbay * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18130e0371SOded Gabbay * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19130e0371SOded Gabbay * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20130e0371SOded Gabbay * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21130e0371SOded Gabbay * OTHER DEALINGS IN THE SOFTWARE.
22130e0371SOded Gabbay */
23130e0371SOded Gabbay
24130e0371SOded Gabbay #include "amdgpu_amdkfd.h"
2593304810SJonathan Kim #include "amd_pcie.h"
262f7d10b3SJammy Zhou #include "amd_shared.h"
27fdf2f6c5SSam Ravnborg
28130e0371SOded Gabbay #include "amdgpu.h"
292db0cdbeSAlex Deucher #include "amdgpu_gfx.h"
302fbd6f94SChristian König #include "amdgpu_dma_buf.h"
31f4bff6e0SRajneesh Bhardwaj #include <drm/ttm/ttm_tt.h>
32130e0371SOded Gabbay #include <linux/module.h>
331dde0ea9SFelix Kuehling #include <linux/dma-buf.h>
34da361dd1Sshaoyunl #include "amdgpu_xgmi.h"
351d251d90SYong Zhao #include <uapi/linux/kfd_ioctl.h>
36c7490949STao Zhou #include "amdgpu_ras.h"
37c7490949STao Zhou #include "amdgpu_umc.h"
38b5fd0cf3SAndrey Grodzovsky #include "amdgpu_reset.h"
39130e0371SOded Gabbay
40611736d8SFelix Kuehling /* Total memory size in system memory and all GPU VRAM. Used to
41611736d8SFelix Kuehling * estimate worst case amount of memory to reserve for page tables
42611736d8SFelix Kuehling */
43611736d8SFelix Kuehling uint64_t amdgpu_amdkfd_total_mem_size;
44611736d8SFelix Kuehling
45402bde58Skernel test robot static bool kfd_initialized;
46c7651b73SFelix Kuehling
amdgpu_amdkfd_init(void)47efb1c658SOded Gabbay int amdgpu_amdkfd_init(void)
48130e0371SOded Gabbay {
49611736d8SFelix Kuehling struct sysinfo si;
50efb1c658SOded Gabbay int ret;
51efb1c658SOded Gabbay
52611736d8SFelix Kuehling si_meminfo(&si);
53df23d1bbSOak Zeng amdgpu_amdkfd_total_mem_size = si.freeram - si.freehigh;
54611736d8SFelix Kuehling amdgpu_amdkfd_total_mem_size *= si.mem_unit;
55611736d8SFelix Kuehling
56308176d6SAmber Lin ret = kgd2kfd_init();
57c7651b73SFelix Kuehling kfd_initialized = !ret;
58fcdfa432SOded Gabbay
59efb1c658SOded Gabbay return ret;
60130e0371SOded Gabbay }
61130e0371SOded Gabbay
amdgpu_amdkfd_fini(void)62130e0371SOded Gabbay void amdgpu_amdkfd_fini(void)
63130e0371SOded Gabbay {
64c7651b73SFelix Kuehling if (kfd_initialized) {
658e07e267SAmber Lin kgd2kfd_exit();
66c7651b73SFelix Kuehling kfd_initialized = false;
67c7651b73SFelix Kuehling }
68130e0371SOded Gabbay }
69130e0371SOded Gabbay
amdgpu_amdkfd_device_probe(struct amdgpu_device * adev)70dc102c43SAndres Rodriguez void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev)
71130e0371SOded Gabbay {
72050091abSYong Zhao bool vf = amdgpu_sriov_vf(adev);
735c33f214SFelix Kuehling
74c7651b73SFelix Kuehling if (!kfd_initialized)
75c7651b73SFelix Kuehling return;
76c7651b73SFelix Kuehling
77b5d1d755SGraham Sider adev->kfd.dev = kgd2kfd_probe(adev, vf);
78130e0371SOded Gabbay }
79130e0371SOded Gabbay
8022cb0164SAlex Deucher /**
8122cb0164SAlex Deucher * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
8222cb0164SAlex Deucher * setup amdkfd
8322cb0164SAlex Deucher *
8422cb0164SAlex Deucher * @adev: amdgpu_device pointer
8522cb0164SAlex Deucher * @aperture_base: output returning doorbell aperture base physical address
8622cb0164SAlex Deucher * @aperture_size: output returning doorbell aperture size in bytes
8722cb0164SAlex Deucher * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
8822cb0164SAlex Deucher *
8922cb0164SAlex Deucher * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
9022cb0164SAlex Deucher * takes doorbells required for its own rings and reports the setup to amdkfd.
9122cb0164SAlex Deucher * amdgpu reserved doorbells are at the start of the doorbell aperture.
9222cb0164SAlex Deucher */
amdgpu_doorbell_get_kfd_info(struct amdgpu_device * adev,phys_addr_t * aperture_base,size_t * aperture_size,size_t * start_offset)9322cb0164SAlex Deucher static void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
9422cb0164SAlex Deucher phys_addr_t *aperture_base,
9522cb0164SAlex Deucher size_t *aperture_size,
9622cb0164SAlex Deucher size_t *start_offset)
9722cb0164SAlex Deucher {
9822cb0164SAlex Deucher /*
990512e9ffSShashank Sharma * The first num_kernel_doorbells are used by amdgpu.
10022cb0164SAlex Deucher * amdkfd takes whatever's left in the aperture.
10122cb0164SAlex Deucher */
102cc009e61SMukul Joshi if (adev->enable_mes) {
103cc009e61SMukul Joshi /*
104cc009e61SMukul Joshi * With MES enabled, we only need to initialize
105cc009e61SMukul Joshi * the base address. The size and offset are
106cc009e61SMukul Joshi * not initialized as AMDGPU manages the whole
107cc009e61SMukul Joshi * doorbell space.
108cc009e61SMukul Joshi */
109cc009e61SMukul Joshi *aperture_base = adev->doorbell.base;
110cc009e61SMukul Joshi *aperture_size = 0;
111cc009e61SMukul Joshi *start_offset = 0;
1120512e9ffSShashank Sharma } else if (adev->doorbell.size > adev->doorbell.num_kernel_doorbells *
113cc009e61SMukul Joshi sizeof(u32)) {
11422cb0164SAlex Deucher *aperture_base = adev->doorbell.base;
11522cb0164SAlex Deucher *aperture_size = adev->doorbell.size;
1160512e9ffSShashank Sharma *start_offset = adev->doorbell.num_kernel_doorbells * sizeof(u32);
11722cb0164SAlex Deucher } else {
11822cb0164SAlex Deucher *aperture_base = 0;
11922cb0164SAlex Deucher *aperture_size = 0;
12022cb0164SAlex Deucher *start_offset = 0;
12122cb0164SAlex Deucher }
12222cb0164SAlex Deucher }
12322cb0164SAlex Deucher
124b5fd0cf3SAndrey Grodzovsky
amdgpu_amdkfd_reset_work(struct work_struct * work)125b5fd0cf3SAndrey Grodzovsky static void amdgpu_amdkfd_reset_work(struct work_struct *work)
126b5fd0cf3SAndrey Grodzovsky {
127b5fd0cf3SAndrey Grodzovsky struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
128b5fd0cf3SAndrey Grodzovsky kfd.reset_work);
129b5fd0cf3SAndrey Grodzovsky
130f1549c09SLikun Gao struct amdgpu_reset_context reset_context;
1312f4ca1baSJingyu Wang
132f1549c09SLikun Gao memset(&reset_context, 0, sizeof(reset_context));
133f1549c09SLikun Gao
134f1549c09SLikun Gao reset_context.method = AMD_RESET_METHOD_NONE;
135f1549c09SLikun Gao reset_context.reset_req_dev = adev;
136dbe2c4c8SEric Huang reset_context.src = adev->enable_mes ?
137dbe2c4c8SEric Huang AMDGPU_RESET_SRC_MES :
138dbe2c4c8SEric Huang AMDGPU_RESET_SRC_HWS;
139f1549c09SLikun Gao clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
140f1549c09SLikun Gao
141f1549c09SLikun Gao amdgpu_device_gpu_recover(adev, NULL, &reset_context);
142b5fd0cf3SAndrey Grodzovsky }
143b5fd0cf3SAndrey Grodzovsky
144733965a9SFlora Cui static const struct drm_client_funcs kfd_client_funcs = {
145733965a9SFlora Cui .unregister = drm_client_release,
146733965a9SFlora Cui };
147c0125b84SLe Ma
amdgpu_amdkfd_drm_client_create(struct amdgpu_device * adev)148c0125b84SLe Ma int amdgpu_amdkfd_drm_client_create(struct amdgpu_device *adev)
149c0125b84SLe Ma {
150c0125b84SLe Ma int ret;
151c0125b84SLe Ma
152f679fd60SAhmad Rehman if (!adev->kfd.init_complete || adev->kfd.client.dev)
153c0125b84SLe Ma return 0;
154c0125b84SLe Ma
155c0125b84SLe Ma ret = drm_client_init(&adev->ddev, &adev->kfd.client, "kfd",
156c0125b84SLe Ma &kfd_client_funcs);
157c0125b84SLe Ma if (ret) {
158c0125b84SLe Ma dev_err(adev->dev, "Failed to init DRM client: %d\n",
159c0125b84SLe Ma ret);
160c0125b84SLe Ma return ret;
161c0125b84SLe Ma }
162c0125b84SLe Ma
163c0125b84SLe Ma drm_client_register(&adev->kfd.client);
164c0125b84SLe Ma
165c0125b84SLe Ma return 0;
166c0125b84SLe Ma }
167c0125b84SLe Ma
amdgpu_amdkfd_device_init(struct amdgpu_device * adev)168dc102c43SAndres Rodriguez void amdgpu_amdkfd_device_init(struct amdgpu_device *adev)
169130e0371SOded Gabbay {
170234441ddSYong Zhao int i;
171d0b63bb3SAndres Rodriguez int last_valid_bit;
172611736d8SFelix Kuehling
17327fb73a0SMukul Joshi amdgpu_amdkfd_gpuvm_init_mem_limits();
17427fb73a0SMukul Joshi
175611736d8SFelix Kuehling if (adev->kfd.dev) {
176130e0371SOded Gabbay struct kgd2kfd_shared_resources gpu_resources = {
17740111ec2SFelix Kuehling .compute_vmid_bitmap =
17840111ec2SFelix Kuehling ((1 << AMDGPU_NUM_VMID) - 1) -
17940111ec2SFelix Kuehling ((1 << adev->vm_manager.first_kfd_vmid) - 1),
180d0b63bb3SAndres Rodriguez .num_pipe_per_mec = adev->gfx.mec.num_pipe_per_mec,
181155494dbSFelix Kuehling .num_queue_per_pipe = adev->gfx.mec.num_queue_per_pipe,
182155494dbSFelix Kuehling .gpuvm_size = min(adev->vm_manager.max_pfn
183155494dbSFelix Kuehling << AMDGPU_GPU_PAGE_SHIFT,
184ad9a5b78SChristian König AMDGPU_GMC_HOLE_START),
1854a580877SLuben Tuikov .drm_render_minor = adev_to_drm(adev)->render->index,
186234441ddSYong Zhao .sdma_doorbell_idx = adev->doorbell_index.sdma_engine,
187cc009e61SMukul Joshi .enable_mes = adev->enable_mes,
188130e0371SOded Gabbay };
189130e0371SOded Gabbay
190d0b63bb3SAndres Rodriguez /* this is going to have a few of the MSBs set that we need to
1910d87c9cfSKent Russell * clear
1920d87c9cfSKent Russell */
193e6945304SYong Zhao bitmap_complement(gpu_resources.cp_queue_bitmap,
194be697aa3SLe Ma adev->gfx.mec_bitmap[0].queue_bitmap,
19568fa72a4SMukul Joshi AMDGPU_MAX_QUEUES);
196d0b63bb3SAndres Rodriguez
197d0b63bb3SAndres Rodriguez /* According to linux/bitmap.h we shouldn't use bitmap_clear if
1980d87c9cfSKent Russell * nbits is not compile time constant
1990d87c9cfSKent Russell */
2003447d220SJay Cornwall last_valid_bit = 1 /* only first MEC can have compute queues */
201d0b63bb3SAndres Rodriguez * adev->gfx.mec.num_pipe_per_mec
202d0b63bb3SAndres Rodriguez * adev->gfx.mec.num_queue_per_pipe;
20368fa72a4SMukul Joshi for (i = last_valid_bit; i < AMDGPU_MAX_QUEUES; ++i)
204e6945304SYong Zhao clear_bit(i, gpu_resources.cp_queue_bitmap);
205d0b63bb3SAndres Rodriguez
206dc102c43SAndres Rodriguez amdgpu_doorbell_get_kfd_info(adev,
207130e0371SOded Gabbay &gpu_resources.doorbell_physical_address,
208130e0371SOded Gabbay &gpu_resources.doorbell_aperture_size,
209130e0371SOded Gabbay &gpu_resources.doorbell_start_offset);
210c5892230SShaoyun Liu
2111f86805aSYong Zhao /* Since SOC15, BIF starts to statically use the
2121f86805aSYong Zhao * lower 12 bits of doorbell addresses for routing
2131f86805aSYong Zhao * based on settings in registers like
2141f86805aSYong Zhao * SDMA0_DOORBELL_RANGE etc..
2151f86805aSYong Zhao * In order to route a doorbell to CP engine, the lower
2161f86805aSYong Zhao * 12 bits of its address has to be outside the range
2171f86805aSYong Zhao * set for SDMA, VCN, and IH blocks.
218642a0e80SFelix Kuehling */
219234441ddSYong Zhao if (adev->asic_type >= CHIP_VEGA10) {
2201f86805aSYong Zhao gpu_resources.non_cp_doorbells_start =
2211f86805aSYong Zhao adev->doorbell_index.first_non_cp;
2221f86805aSYong Zhao gpu_resources.non_cp_doorbells_end =
2231f86805aSYong Zhao adev->doorbell_index.last_non_cp;
224234441ddSYong Zhao }
225130e0371SOded Gabbay
2268e2712e7Sshaoyunl adev->kfd.init_complete = kgd2kfd_device_init(adev->kfd.dev,
227d69a3b76SMukul Joshi &gpu_resources);
228b5fd0cf3SAndrey Grodzovsky
2292302d507SPhilip Yang amdgpu_amdkfd_total_mem_size += adev->gmc.real_vram_size;
2302302d507SPhilip Yang
231b5fd0cf3SAndrey Grodzovsky INIT_WORK(&adev->kfd.reset_work, amdgpu_amdkfd_reset_work);
232130e0371SOded Gabbay }
233130e0371SOded Gabbay }
234130e0371SOded Gabbay
amdgpu_amdkfd_device_fini_sw(struct amdgpu_device * adev)235e9669fb7SAndrey Grodzovsky void amdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev)
236130e0371SOded Gabbay {
237611736d8SFelix Kuehling if (adev->kfd.dev) {
2388e07e267SAmber Lin kgd2kfd_device_exit(adev->kfd.dev);
239611736d8SFelix Kuehling adev->kfd.dev = NULL;
2402302d507SPhilip Yang amdgpu_amdkfd_total_mem_size -= adev->gmc.real_vram_size;
241130e0371SOded Gabbay }
242130e0371SOded Gabbay }
243130e0371SOded Gabbay
amdgpu_amdkfd_interrupt(struct amdgpu_device * adev,const void * ih_ring_entry)244dc102c43SAndres Rodriguez void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
245130e0371SOded Gabbay const void *ih_ring_entry)
246130e0371SOded Gabbay {
247611736d8SFelix Kuehling if (adev->kfd.dev)
2488e07e267SAmber Lin kgd2kfd_interrupt(adev->kfd.dev, ih_ring_entry);
249130e0371SOded Gabbay }
250130e0371SOded Gabbay
amdgpu_amdkfd_suspend(struct amdgpu_device * adev,bool run_pm)2519593f4d6SRajneesh Bhardwaj void amdgpu_amdkfd_suspend(struct amdgpu_device *adev, bool run_pm)
252130e0371SOded Gabbay {
253611736d8SFelix Kuehling if (adev->kfd.dev)
2549593f4d6SRajneesh Bhardwaj kgd2kfd_suspend(adev->kfd.dev, run_pm);
255130e0371SOded Gabbay }
256130e0371SOded Gabbay
amdgpu_amdkfd_resume(struct amdgpu_device * adev,bool run_pm)2579593f4d6SRajneesh Bhardwaj int amdgpu_amdkfd_resume(struct amdgpu_device *adev, bool run_pm)
258130e0371SOded Gabbay {
259130e0371SOded Gabbay int r = 0;
260130e0371SOded Gabbay
261611736d8SFelix Kuehling if (adev->kfd.dev)
2629593f4d6SRajneesh Bhardwaj r = kgd2kfd_resume(adev->kfd.dev, run_pm);
263130e0371SOded Gabbay
264130e0371SOded Gabbay return r;
265130e0371SOded Gabbay }
266130e0371SOded Gabbay
amdgpu_amdkfd_pre_reset(struct amdgpu_device * adev,struct amdgpu_reset_context * reset_context)267dbe2c4c8SEric Huang int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev,
268dbe2c4c8SEric Huang struct amdgpu_reset_context *reset_context)
2695c6dd71eSShaoyun Liu {
2705c6dd71eSShaoyun Liu int r = 0;
2715c6dd71eSShaoyun Liu
272611736d8SFelix Kuehling if (adev->kfd.dev)
273dbe2c4c8SEric Huang r = kgd2kfd_pre_reset(adev->kfd.dev, reset_context);
2745c6dd71eSShaoyun Liu
2755c6dd71eSShaoyun Liu return r;
2765c6dd71eSShaoyun Liu }
2775c6dd71eSShaoyun Liu
amdgpu_amdkfd_post_reset(struct amdgpu_device * adev)2785c6dd71eSShaoyun Liu int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev)
2795c6dd71eSShaoyun Liu {
2805c6dd71eSShaoyun Liu int r = 0;
2815c6dd71eSShaoyun Liu
282611736d8SFelix Kuehling if (adev->kfd.dev)
2838e07e267SAmber Lin r = kgd2kfd_post_reset(adev->kfd.dev);
2845c6dd71eSShaoyun Liu
2855c6dd71eSShaoyun Liu return r;
2865c6dd71eSShaoyun Liu }
2875c6dd71eSShaoyun Liu
amdgpu_amdkfd_gpu_reset(struct amdgpu_device * adev)2886bfc7c7eSGraham Sider void amdgpu_amdkfd_gpu_reset(struct amdgpu_device *adev)
28924da5a9cSShaoyun Liu {
29012938fadSChristian König if (amdgpu_device_should_recover_gpu(adev))
291b5fd0cf3SAndrey Grodzovsky amdgpu_reset_domain_schedule(adev->reset_domain,
292b5fd0cf3SAndrey Grodzovsky &adev->kfd.reset_work);
29324da5a9cSShaoyun Liu }
29424da5a9cSShaoyun Liu
amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device * adev,size_t size,void ** mem_obj,uint64_t * gpu_addr,void ** cpu_ptr,bool cp_mqd_gfx9)2956bfc7c7eSGraham Sider int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
296130e0371SOded Gabbay void **mem_obj, uint64_t *gpu_addr,
297fa5bde80SYong Zhao void **cpu_ptr, bool cp_mqd_gfx9)
298130e0371SOded Gabbay {
299473fee47SYong Zhao struct amdgpu_bo *bo = NULL;
3003216c6b7SChunming Zhou struct amdgpu_bo_param bp;
301130e0371SOded Gabbay int r;
302473fee47SYong Zhao void *cpu_ptr_tmp = NULL;
303130e0371SOded Gabbay
3043216c6b7SChunming Zhou memset(&bp, 0, sizeof(bp));
3053216c6b7SChunming Zhou bp.size = size;
3063216c6b7SChunming Zhou bp.byte_align = PAGE_SIZE;
3073216c6b7SChunming Zhou bp.domain = AMDGPU_GEM_DOMAIN_GTT;
3083216c6b7SChunming Zhou bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
3093216c6b7SChunming Zhou bp.type = ttm_bo_type_kernel;
3103216c6b7SChunming Zhou bp.resv = NULL;
3119fd5543eSNirmoy Das bp.bo_ptr_size = sizeof(struct amdgpu_bo);
31215426dbbSYong Zhao
313fa5bde80SYong Zhao if (cp_mqd_gfx9)
314fa5bde80SYong Zhao bp.flags |= AMDGPU_GEM_CREATE_CP_MQD_GFX9;
31515426dbbSYong Zhao
3163216c6b7SChunming Zhou r = amdgpu_bo_create(adev, &bp, &bo);
317130e0371SOded Gabbay if (r) {
318dc102c43SAndres Rodriguez dev_err(adev->dev,
319130e0371SOded Gabbay "failed to allocate BO for amdkfd (%d)\n", r);
320130e0371SOded Gabbay return r;
321130e0371SOded Gabbay }
322130e0371SOded Gabbay
323130e0371SOded Gabbay /* map the buffer */
324473fee47SYong Zhao r = amdgpu_bo_reserve(bo, true);
325130e0371SOded Gabbay if (r) {
326dc102c43SAndres Rodriguez dev_err(adev->dev, "(%d) failed to reserve bo for amdkfd\n", r);
327130e0371SOded Gabbay goto allocate_mem_reserve_bo_failed;
328130e0371SOded Gabbay }
329130e0371SOded Gabbay
3307b7c6c81SJunwei Zhang r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
331130e0371SOded Gabbay if (r) {
332dc102c43SAndres Rodriguez dev_err(adev->dev, "(%d) failed to pin bo for amdkfd\n", r);
333130e0371SOded Gabbay goto allocate_mem_pin_bo_failed;
334130e0371SOded Gabbay }
335130e0371SOded Gabbay
336bb812f1eSJunwei Zhang r = amdgpu_ttm_alloc_gart(&bo->tbo);
337bb812f1eSJunwei Zhang if (r) {
338bb812f1eSJunwei Zhang dev_err(adev->dev, "%p bind failed\n", bo);
339bb812f1eSJunwei Zhang goto allocate_mem_kmap_bo_failed;
340bb812f1eSJunwei Zhang }
341bb812f1eSJunwei Zhang
342473fee47SYong Zhao r = amdgpu_bo_kmap(bo, &cpu_ptr_tmp);
343130e0371SOded Gabbay if (r) {
344dc102c43SAndres Rodriguez dev_err(adev->dev,
345130e0371SOded Gabbay "(%d) failed to map bo to kernel for amdkfd\n", r);
346130e0371SOded Gabbay goto allocate_mem_kmap_bo_failed;
347130e0371SOded Gabbay }
348130e0371SOded Gabbay
349473fee47SYong Zhao *mem_obj = bo;
3507b7c6c81SJunwei Zhang *gpu_addr = amdgpu_bo_gpu_offset(bo);
351473fee47SYong Zhao *cpu_ptr = cpu_ptr_tmp;
352473fee47SYong Zhao
353473fee47SYong Zhao amdgpu_bo_unreserve(bo);
354130e0371SOded Gabbay
355130e0371SOded Gabbay return 0;
356130e0371SOded Gabbay
357130e0371SOded Gabbay allocate_mem_kmap_bo_failed:
358473fee47SYong Zhao amdgpu_bo_unpin(bo);
359130e0371SOded Gabbay allocate_mem_pin_bo_failed:
360473fee47SYong Zhao amdgpu_bo_unreserve(bo);
361130e0371SOded Gabbay allocate_mem_reserve_bo_failed:
362473fee47SYong Zhao amdgpu_bo_unref(&bo);
363130e0371SOded Gabbay
364130e0371SOded Gabbay return r;
365130e0371SOded Gabbay }
366130e0371SOded Gabbay
amdgpu_amdkfd_free_gtt_mem(struct amdgpu_device * adev,void ** mem_obj)367c86ad391SPhilip Yang void amdgpu_amdkfd_free_gtt_mem(struct amdgpu_device *adev, void **mem_obj)
368130e0371SOded Gabbay {
369c86ad391SPhilip Yang struct amdgpu_bo **bo = (struct amdgpu_bo **) mem_obj;
370130e0371SOded Gabbay
371c86ad391SPhilip Yang amdgpu_bo_reserve(*bo, true);
372c86ad391SPhilip Yang amdgpu_bo_kunmap(*bo);
373c86ad391SPhilip Yang amdgpu_bo_unpin(*bo);
374c86ad391SPhilip Yang amdgpu_bo_unreserve(*bo);
375c86ad391SPhilip Yang amdgpu_bo_unref(bo);
376130e0371SOded Gabbay }
377130e0371SOded Gabbay
amdgpu_amdkfd_alloc_gws(struct amdgpu_device * adev,size_t size,void ** mem_obj)3786bfc7c7eSGraham Sider int amdgpu_amdkfd_alloc_gws(struct amdgpu_device *adev, size_t size,
379ca66fb8fSOak Zeng void **mem_obj)
380ca66fb8fSOak Zeng {
381ca66fb8fSOak Zeng struct amdgpu_bo *bo = NULL;
38222b40f7aSNirmoy Das struct amdgpu_bo_user *ubo;
383ca66fb8fSOak Zeng struct amdgpu_bo_param bp;
384ca66fb8fSOak Zeng int r;
385ca66fb8fSOak Zeng
386ca66fb8fSOak Zeng memset(&bp, 0, sizeof(bp));
387ca66fb8fSOak Zeng bp.size = size;
388ca66fb8fSOak Zeng bp.byte_align = 1;
389ca66fb8fSOak Zeng bp.domain = AMDGPU_GEM_DOMAIN_GWS;
390ca66fb8fSOak Zeng bp.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
391ca66fb8fSOak Zeng bp.type = ttm_bo_type_device;
392ca66fb8fSOak Zeng bp.resv = NULL;
3939fd5543eSNirmoy Das bp.bo_ptr_size = sizeof(struct amdgpu_bo);
394ca66fb8fSOak Zeng
39522b40f7aSNirmoy Das r = amdgpu_bo_create_user(adev, &bp, &ubo);
396ca66fb8fSOak Zeng if (r) {
397ca66fb8fSOak Zeng dev_err(adev->dev,
398ca66fb8fSOak Zeng "failed to allocate gws BO for amdkfd (%d)\n", r);
399ca66fb8fSOak Zeng return r;
400ca66fb8fSOak Zeng }
401ca66fb8fSOak Zeng
40222b40f7aSNirmoy Das bo = &ubo->bo;
403ca66fb8fSOak Zeng *mem_obj = bo;
404ca66fb8fSOak Zeng return 0;
405ca66fb8fSOak Zeng }
406ca66fb8fSOak Zeng
amdgpu_amdkfd_free_gws(struct amdgpu_device * adev,void * mem_obj)4076bfc7c7eSGraham Sider void amdgpu_amdkfd_free_gws(struct amdgpu_device *adev, void *mem_obj)
408ca66fb8fSOak Zeng {
409ca66fb8fSOak Zeng struct amdgpu_bo *bo = (struct amdgpu_bo *)mem_obj;
410ca66fb8fSOak Zeng
411ca66fb8fSOak Zeng amdgpu_bo_unref(&bo);
412ca66fb8fSOak Zeng }
413ca66fb8fSOak Zeng
amdgpu_amdkfd_get_fw_version(struct amdgpu_device * adev,enum kgd_engine_type type)414574c4183SGraham Sider uint32_t amdgpu_amdkfd_get_fw_version(struct amdgpu_device *adev,
4150da8b10eSAmber Lin enum kgd_engine_type type)
4160da8b10eSAmber Lin {
4170da8b10eSAmber Lin switch (type) {
4180da8b10eSAmber Lin case KGD_ENGINE_PFP:
4190da8b10eSAmber Lin return adev->gfx.pfp_fw_version;
4200da8b10eSAmber Lin
4210da8b10eSAmber Lin case KGD_ENGINE_ME:
4220da8b10eSAmber Lin return adev->gfx.me_fw_version;
4230da8b10eSAmber Lin
4240da8b10eSAmber Lin case KGD_ENGINE_CE:
4250da8b10eSAmber Lin return adev->gfx.ce_fw_version;
4260da8b10eSAmber Lin
4270da8b10eSAmber Lin case KGD_ENGINE_MEC1:
4280da8b10eSAmber Lin return adev->gfx.mec_fw_version;
4290da8b10eSAmber Lin
4300da8b10eSAmber Lin case KGD_ENGINE_MEC2:
4310da8b10eSAmber Lin return adev->gfx.mec2_fw_version;
4320da8b10eSAmber Lin
4330da8b10eSAmber Lin case KGD_ENGINE_RLC:
4340da8b10eSAmber Lin return adev->gfx.rlc_fw_version;
4350da8b10eSAmber Lin
4360da8b10eSAmber Lin case KGD_ENGINE_SDMA1:
4370da8b10eSAmber Lin return adev->sdma.instance[0].fw_version;
4380da8b10eSAmber Lin
4390da8b10eSAmber Lin case KGD_ENGINE_SDMA2:
4400da8b10eSAmber Lin return adev->sdma.instance[1].fw_version;
4410da8b10eSAmber Lin
4420da8b10eSAmber Lin default:
4430da8b10eSAmber Lin return 0;
4440da8b10eSAmber Lin }
4450da8b10eSAmber Lin
4460da8b10eSAmber Lin return 0;
4470da8b10eSAmber Lin }
4480da8b10eSAmber Lin
amdgpu_amdkfd_get_local_mem_info(struct amdgpu_device * adev,struct kfd_local_mem_info * mem_info,struct amdgpu_xcp * xcp)449574c4183SGraham Sider void amdgpu_amdkfd_get_local_mem_info(struct amdgpu_device *adev,
450315e29ecSMukul Joshi struct kfd_local_mem_info *mem_info,
4519a3ce1a7SHawking Zhang struct amdgpu_xcp *xcp)
45230f1c042SHarish Kasiviswanathan {
45330f1c042SHarish Kasiviswanathan memset(mem_info, 0, sizeof(*mem_info));
4544c7e8a9eSGang Ba
4559a3ce1a7SHawking Zhang if (xcp) {
456315e29ecSMukul Joshi if (adev->gmc.real_vram_size == adev->gmc.visible_vram_size)
457315e29ecSMukul Joshi mem_info->local_mem_size_public =
4589a3ce1a7SHawking Zhang KFD_XCP_MEMORY_SIZE(adev, xcp->id);
459315e29ecSMukul Joshi else
460315e29ecSMukul Joshi mem_info->local_mem_size_private =
4619a3ce1a7SHawking Zhang KFD_XCP_MEMORY_SIZE(adev, xcp->id);
46289773b85SLang Yu } else if (adev->flags & AMD_IS_APU) {
46389773b85SLang Yu mem_info->local_mem_size_public = (ttm_tt_pages_limit() << PAGE_SHIFT);
46489773b85SLang Yu mem_info->local_mem_size_private = 0;
465315e29ecSMukul Joshi } else {
466770d13b1SChristian König mem_info->local_mem_size_public = adev->gmc.visible_vram_size;
467770d13b1SChristian König mem_info->local_mem_size_private = adev->gmc.real_vram_size -
468770d13b1SChristian König adev->gmc.visible_vram_size;
469315e29ecSMukul Joshi }
470770d13b1SChristian König mem_info->vram_width = adev->gmc.vram_width;
47130f1c042SHarish Kasiviswanathan
4724c7e8a9eSGang Ba pr_debug("Address base: %pap public 0x%llx private 0x%llx\n",
4734c7e8a9eSGang Ba &adev->gmc.aper_base,
47430f1c042SHarish Kasiviswanathan mem_info->local_mem_size_public,
47530f1c042SHarish Kasiviswanathan mem_info->local_mem_size_private);
47630f1c042SHarish Kasiviswanathan
4770bc119faSHorace Chen if (adev->pm.dpm_enabled) {
4786bdadb20SHawking Zhang if (amdgpu_emu_mode == 1)
4796bdadb20SHawking Zhang mem_info->mem_clk_max = 0;
4807ba01f9eSShaoyun Liu else
4816bdadb20SHawking Zhang mem_info->mem_clk_max = amdgpu_dpm_get_mclk(adev, false) / 100;
4826bdadb20SHawking Zhang } else
4837ba01f9eSShaoyun Liu mem_info->mem_clk_max = 100;
48430f1c042SHarish Kasiviswanathan }
48530f1c042SHarish Kasiviswanathan
amdgpu_amdkfd_get_gpu_clock_counter(struct amdgpu_device * adev)486574c4183SGraham Sider uint64_t amdgpu_amdkfd_get_gpu_clock_counter(struct amdgpu_device *adev)
487130e0371SOded Gabbay {
488dc102c43SAndres Rodriguez if (adev->gfx.funcs->get_gpu_clock_counter)
489dc102c43SAndres Rodriguez return adev->gfx.funcs->get_gpu_clock_counter(adev);
490130e0371SOded Gabbay return 0;
491130e0371SOded Gabbay }
492130e0371SOded Gabbay
amdgpu_amdkfd_get_max_engine_clock_in_mhz(struct amdgpu_device * adev)493574c4183SGraham Sider uint32_t amdgpu_amdkfd_get_max_engine_clock_in_mhz(struct amdgpu_device *adev)
494130e0371SOded Gabbay {
495a9efcc19SFelix Kuehling /* the sclk is in quantas of 10kHz */
4960bc119faSHorace Chen if (adev->pm.dpm_enabled)
497a9efcc19SFelix Kuehling return amdgpu_dpm_get_sclk(adev, false) / 100;
4987ba01f9eSShaoyun Liu else
4997ba01f9eSShaoyun Liu return 100;
500130e0371SOded Gabbay }
501ebdebf42SFlora Cui
amdgpu_amdkfd_get_dmabuf_info(struct amdgpu_device * adev,int dma_buf_fd,struct amdgpu_device ** dmabuf_adev,uint64_t * bo_size,void * metadata_buffer,size_t buffer_size,uint32_t * metadata_size,uint32_t * flags,int8_t * xcp_id)502574c4183SGraham Sider int amdgpu_amdkfd_get_dmabuf_info(struct amdgpu_device *adev, int dma_buf_fd,
503574c4183SGraham Sider struct amdgpu_device **dmabuf_adev,
5041dde0ea9SFelix Kuehling uint64_t *bo_size, void *metadata_buffer,
5051dde0ea9SFelix Kuehling size_t buffer_size, uint32_t *metadata_size,
5062fa9ff25SPhilip Yang uint32_t *flags, int8_t *xcp_id)
5071dde0ea9SFelix Kuehling {
5081dde0ea9SFelix Kuehling struct dma_buf *dma_buf;
5091dde0ea9SFelix Kuehling struct drm_gem_object *obj;
5101dde0ea9SFelix Kuehling struct amdgpu_bo *bo;
5111dde0ea9SFelix Kuehling uint64_t metadata_flags;
5121dde0ea9SFelix Kuehling int r = -EINVAL;
5131dde0ea9SFelix Kuehling
5141dde0ea9SFelix Kuehling dma_buf = dma_buf_get(dma_buf_fd);
5151dde0ea9SFelix Kuehling if (IS_ERR(dma_buf))
5161dde0ea9SFelix Kuehling return PTR_ERR(dma_buf);
5171dde0ea9SFelix Kuehling
5181dde0ea9SFelix Kuehling if (dma_buf->ops != &amdgpu_dmabuf_ops)
5191dde0ea9SFelix Kuehling /* Can't handle non-graphics buffers */
5201dde0ea9SFelix Kuehling goto out_put;
5211dde0ea9SFelix Kuehling
5221dde0ea9SFelix Kuehling obj = dma_buf->priv;
5234a580877SLuben Tuikov if (obj->dev->driver != adev_to_drm(adev)->driver)
5241dde0ea9SFelix Kuehling /* Can't handle buffers from different drivers */
5251dde0ea9SFelix Kuehling goto out_put;
5261dde0ea9SFelix Kuehling
5271348969aSLuben Tuikov adev = drm_to_adev(obj->dev);
5281dde0ea9SFelix Kuehling bo = gem_to_amdgpu_bo(obj);
5291dde0ea9SFelix Kuehling if (!(bo->preferred_domains & (AMDGPU_GEM_DOMAIN_VRAM |
5301dde0ea9SFelix Kuehling AMDGPU_GEM_DOMAIN_GTT)))
5311dde0ea9SFelix Kuehling /* Only VRAM and GTT BOs are supported */
5321dde0ea9SFelix Kuehling goto out_put;
5331dde0ea9SFelix Kuehling
5341dde0ea9SFelix Kuehling r = 0;
535574c4183SGraham Sider if (dmabuf_adev)
536574c4183SGraham Sider *dmabuf_adev = adev;
5371dde0ea9SFelix Kuehling if (bo_size)
5381dde0ea9SFelix Kuehling *bo_size = amdgpu_bo_size(bo);
5391dde0ea9SFelix Kuehling if (metadata_buffer)
5401dde0ea9SFelix Kuehling r = amdgpu_bo_get_metadata(bo, metadata_buffer, buffer_size,
5411dde0ea9SFelix Kuehling metadata_size, &metadata_flags);
5421dde0ea9SFelix Kuehling if (flags) {
5431dde0ea9SFelix Kuehling *flags = (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) ?
5441d251d90SYong Zhao KFD_IOC_ALLOC_MEM_FLAGS_VRAM
5451d251d90SYong Zhao : KFD_IOC_ALLOC_MEM_FLAGS_GTT;
5461dde0ea9SFelix Kuehling
5471dde0ea9SFelix Kuehling if (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
5481d251d90SYong Zhao *flags |= KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC;
5491dde0ea9SFelix Kuehling }
5502fa9ff25SPhilip Yang if (xcp_id)
5512fa9ff25SPhilip Yang *xcp_id = bo->xcp_id;
5521dde0ea9SFelix Kuehling
5531dde0ea9SFelix Kuehling out_put:
5541dde0ea9SFelix Kuehling dma_buf_put(dma_buf);
5551dde0ea9SFelix Kuehling return r;
5561dde0ea9SFelix Kuehling }
5571dde0ea9SFelix Kuehling
amdgpu_amdkfd_get_xgmi_hops_count(struct amdgpu_device * dst,struct amdgpu_device * src)558574c4183SGraham Sider uint8_t amdgpu_amdkfd_get_xgmi_hops_count(struct amdgpu_device *dst,
559574c4183SGraham Sider struct amdgpu_device *src)
560da361dd1Sshaoyunl {
561574c4183SGraham Sider struct amdgpu_device *peer_adev = src;
562574c4183SGraham Sider struct amdgpu_device *adev = dst;
563da361dd1Sshaoyunl int ret = amdgpu_xgmi_get_hops_count(adev, peer_adev);
564da361dd1Sshaoyunl
565da361dd1Sshaoyunl if (ret < 0) {
566da361dd1Sshaoyunl DRM_ERROR("amdgpu: failed to get xgmi hops count between node %d and %d. ret = %d\n",
567da361dd1Sshaoyunl adev->gmc.xgmi.physical_node_id,
568da361dd1Sshaoyunl peer_adev->gmc.xgmi.physical_node_id, ret);
569da361dd1Sshaoyunl ret = 0;
570da361dd1Sshaoyunl }
571da361dd1Sshaoyunl return (uint8_t)ret;
572da361dd1Sshaoyunl }
573db8b62c0SShaoyun Liu
amdgpu_amdkfd_get_xgmi_bandwidth_mbytes(struct amdgpu_device * dst,struct amdgpu_device * src,bool is_min)574574c4183SGraham Sider int amdgpu_amdkfd_get_xgmi_bandwidth_mbytes(struct amdgpu_device *dst,
575574c4183SGraham Sider struct amdgpu_device *src,
576574c4183SGraham Sider bool is_min)
5773f46c4e9SJonathan Kim {
578574c4183SGraham Sider struct amdgpu_device *adev = dst, *peer_adev;
5793f46c4e9SJonathan Kim int num_links;
5803f46c4e9SJonathan Kim
58135c425f5SJonathan Kim if (amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(9, 4, 2))
5823f46c4e9SJonathan Kim return 0;
5833f46c4e9SJonathan Kim
5843f46c4e9SJonathan Kim if (src)
585574c4183SGraham Sider peer_adev = src;
5863f46c4e9SJonathan Kim
5873f46c4e9SJonathan Kim /* num links returns 0 for indirect peers since indirect route is unknown. */
5883f46c4e9SJonathan Kim num_links = is_min ? 1 : amdgpu_xgmi_get_num_links(adev, peer_adev);
5893f46c4e9SJonathan Kim if (num_links < 0) {
5903f46c4e9SJonathan Kim DRM_ERROR("amdgpu: failed to get xgmi num links between node %d and %d. ret = %d\n",
5913f46c4e9SJonathan Kim adev->gmc.xgmi.physical_node_id,
5923f46c4e9SJonathan Kim peer_adev->gmc.xgmi.physical_node_id, num_links);
5933f46c4e9SJonathan Kim num_links = 0;
5943f46c4e9SJonathan Kim }
5953f46c4e9SJonathan Kim
5963f46c4e9SJonathan Kim /* Aldebaran xGMI DPM is defeatured so assume x16 x 25Gbps for bandwidth. */
5973f46c4e9SJonathan Kim return (num_links * 16 * 25000)/BITS_PER_BYTE;
5983f46c4e9SJonathan Kim }
5993f46c4e9SJonathan Kim
amdgpu_amdkfd_get_pcie_bandwidth_mbytes(struct amdgpu_device * adev,bool is_min)600574c4183SGraham Sider int amdgpu_amdkfd_get_pcie_bandwidth_mbytes(struct amdgpu_device *adev, bool is_min)
60193304810SJonathan Kim {
60293304810SJonathan Kim int num_lanes_shift = (is_min ? ffs(adev->pm.pcie_mlw_mask) :
60393304810SJonathan Kim fls(adev->pm.pcie_mlw_mask)) - 1;
60493304810SJonathan Kim int gen_speed_shift = (is_min ? ffs(adev->pm.pcie_gen_mask &
60593304810SJonathan Kim CAIL_PCIE_LINK_SPEED_SUPPORT_MASK) :
60693304810SJonathan Kim fls(adev->pm.pcie_gen_mask &
60793304810SJonathan Kim CAIL_PCIE_LINK_SPEED_SUPPORT_MASK)) - 1;
60893304810SJonathan Kim uint32_t num_lanes_mask = 1 << num_lanes_shift;
60993304810SJonathan Kim uint32_t gen_speed_mask = 1 << gen_speed_shift;
61093304810SJonathan Kim int num_lanes_factor = 0, gen_speed_mbits_factor = 0;
61193304810SJonathan Kim
61293304810SJonathan Kim switch (num_lanes_mask) {
61393304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X1:
61493304810SJonathan Kim num_lanes_factor = 1;
61593304810SJonathan Kim break;
61693304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X2:
61793304810SJonathan Kim num_lanes_factor = 2;
61893304810SJonathan Kim break;
61993304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X4:
62093304810SJonathan Kim num_lanes_factor = 4;
62193304810SJonathan Kim break;
62293304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X8:
62393304810SJonathan Kim num_lanes_factor = 8;
62493304810SJonathan Kim break;
62593304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X12:
62693304810SJonathan Kim num_lanes_factor = 12;
62793304810SJonathan Kim break;
62893304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X16:
62993304810SJonathan Kim num_lanes_factor = 16;
63093304810SJonathan Kim break;
63193304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X32:
63293304810SJonathan Kim num_lanes_factor = 32;
63393304810SJonathan Kim break;
63493304810SJonathan Kim }
63593304810SJonathan Kim
63693304810SJonathan Kim switch (gen_speed_mask) {
63793304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1:
63893304810SJonathan Kim gen_speed_mbits_factor = 2500;
63993304810SJonathan Kim break;
64093304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2:
64193304810SJonathan Kim gen_speed_mbits_factor = 5000;
64293304810SJonathan Kim break;
64393304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3:
64493304810SJonathan Kim gen_speed_mbits_factor = 8000;
64593304810SJonathan Kim break;
64693304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4:
64793304810SJonathan Kim gen_speed_mbits_factor = 16000;
64893304810SJonathan Kim break;
64993304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5:
65093304810SJonathan Kim gen_speed_mbits_factor = 32000;
65193304810SJonathan Kim break;
65293304810SJonathan Kim }
65393304810SJonathan Kim
65493304810SJonathan Kim return (num_lanes_factor * gen_speed_mbits_factor)/BITS_PER_BYTE;
65593304810SJonathan Kim }
65693304810SJonathan Kim
amdgpu_amdkfd_submit_ib(struct amdgpu_device * adev,enum kgd_engine_type engine,uint32_t vmid,uint64_t gpu_addr,uint32_t * ib_cmd,uint32_t ib_len)6576bfc7c7eSGraham Sider int amdgpu_amdkfd_submit_ib(struct amdgpu_device *adev,
6586bfc7c7eSGraham Sider enum kgd_engine_type engine,
6594c660c8fSFelix Kuehling uint32_t vmid, uint64_t gpu_addr,
6604c660c8fSFelix Kuehling uint32_t *ib_cmd, uint32_t ib_len)
6614c660c8fSFelix Kuehling {
6624c660c8fSFelix Kuehling struct amdgpu_job *job;
6634c660c8fSFelix Kuehling struct amdgpu_ib *ib;
6644c660c8fSFelix Kuehling struct amdgpu_ring *ring;
6654c660c8fSFelix Kuehling struct dma_fence *f = NULL;
6664c660c8fSFelix Kuehling int ret;
6674c660c8fSFelix Kuehling
6684c660c8fSFelix Kuehling switch (engine) {
6694c660c8fSFelix Kuehling case KGD_ENGINE_MEC1:
6704c660c8fSFelix Kuehling ring = &adev->gfx.compute_ring[0];
6714c660c8fSFelix Kuehling break;
6724c660c8fSFelix Kuehling case KGD_ENGINE_SDMA1:
6734c660c8fSFelix Kuehling ring = &adev->sdma.instance[0].ring;
6744c660c8fSFelix Kuehling break;
6754c660c8fSFelix Kuehling case KGD_ENGINE_SDMA2:
6764c660c8fSFelix Kuehling ring = &adev->sdma.instance[1].ring;
6774c660c8fSFelix Kuehling break;
6784c660c8fSFelix Kuehling default:
6794c660c8fSFelix Kuehling pr_err("Invalid engine in IB submission: %d\n", engine);
6804c660c8fSFelix Kuehling ret = -EINVAL;
6814c660c8fSFelix Kuehling goto err;
6824c660c8fSFelix Kuehling }
6834c660c8fSFelix Kuehling
684f7d66fb2SChristian König ret = amdgpu_job_alloc(adev, NULL, NULL, NULL, 1, &job);
6854c660c8fSFelix Kuehling if (ret)
6864c660c8fSFelix Kuehling goto err;
6874c660c8fSFelix Kuehling
6884c660c8fSFelix Kuehling ib = &job->ibs[0];
6894c660c8fSFelix Kuehling memset(ib, 0, sizeof(struct amdgpu_ib));
6904c660c8fSFelix Kuehling
6914c660c8fSFelix Kuehling ib->gpu_addr = gpu_addr;
6924c660c8fSFelix Kuehling ib->ptr = ib_cmd;
6934c660c8fSFelix Kuehling ib->length_dw = ib_len;
6944c660c8fSFelix Kuehling /* This works for NO_HWS. TODO: need to handle without knowing VMID */
6954c660c8fSFelix Kuehling job->vmid = vmid;
6964624459cSChristian König job->num_ibs = 1;
6974c660c8fSFelix Kuehling
6984c660c8fSFelix Kuehling ret = amdgpu_ib_schedule(ring, 1, ib, job, &f);
69994561899SDennis Li
7004c660c8fSFelix Kuehling if (ret) {
7014c660c8fSFelix Kuehling DRM_ERROR("amdgpu: failed to schedule IB.\n");
7024c660c8fSFelix Kuehling goto err_ib_sched;
7034c660c8fSFelix Kuehling }
7044c660c8fSFelix Kuehling
7059ae55f03SAndrey Grodzovsky /* Drop the initial kref_init count (see drm_sched_main as example) */
7069ae55f03SAndrey Grodzovsky dma_fence_put(f);
7074c660c8fSFelix Kuehling ret = dma_fence_wait(f, false);
7084c660c8fSFelix Kuehling
7094c660c8fSFelix Kuehling err_ib_sched:
7104c660c8fSFelix Kuehling amdgpu_job_free(job);
7114c660c8fSFelix Kuehling err:
7124c660c8fSFelix Kuehling return ret;
7134c660c8fSFelix Kuehling }
7144c660c8fSFelix Kuehling
amdgpu_amdkfd_set_compute_idle(struct amdgpu_device * adev,bool idle)7156bfc7c7eSGraham Sider void amdgpu_amdkfd_set_compute_idle(struct amdgpu_device *adev, bool idle)
71601c097dbSFelix Kuehling {
717e341631fSJesse Zhang enum amd_powergating_state state = idle ? AMD_PG_STATE_GATE : AMD_PG_STATE_UNGATE;
718776b0953SOri Messinger if (IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) == 11 &&
719776b0953SOri Messinger ((adev->mes.kiq_version & AMDGPU_MES_VERSION_MASK) <= 64)) {
720087b8542SGraham Sider pr_debug("GFXOFF is %s\n", idle ? "enabled" : "disabled");
721087b8542SGraham Sider amdgpu_gfx_off_ctrl(adev, idle);
722e341631fSJesse Zhang } else if ((IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) == 9) &&
723e341631fSJesse Zhang (adev->flags & AMD_IS_APU)) {
724e341631fSJesse Zhang /* Disable GFXOFF and PG. Temporary workaround
725e341631fSJesse Zhang * to fix some compute applications issue on GFX9.
726e341631fSJesse Zhang */
727e341631fSJesse Zhang adev->ip_blocks[AMD_IP_BLOCK_TYPE_GFX].version->funcs->set_powergating_state((void *)adev, state);
728087b8542SGraham Sider }
72901c097dbSFelix Kuehling amdgpu_dpm_switch_power_profile(adev,
730919a52fcSFelix Kuehling PP_SMC_POWER_PROFILE_COMPUTE,
731919a52fcSFelix Kuehling !idle);
73201c097dbSFelix Kuehling }
73301c097dbSFelix Kuehling
amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device * adev,u32 vmid)734155494dbSFelix Kuehling bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid)
735155494dbSFelix Kuehling {
73640111ec2SFelix Kuehling if (adev->kfd.dev)
73740111ec2SFelix Kuehling return vmid >= adev->vm_manager.first_kfd_vmid;
738155494dbSFelix Kuehling
739155494dbSFelix Kuehling return false;
740155494dbSFelix Kuehling }
741fcdfa432SOded Gabbay
amdgpu_amdkfd_have_atomics_support(struct amdgpu_device * adev)7426bfc7c7eSGraham Sider bool amdgpu_amdkfd_have_atomics_support(struct amdgpu_device *adev)
743aabf3a95SJack Xiao {
744aabf3a95SJack Xiao return adev->have_atomics_support;
745aabf3a95SJack Xiao }
746c7490949STao Zhou
amdgpu_amdkfd_debug_mem_fence(struct amdgpu_device * adev)747a70a93faSJonathan Kim void amdgpu_amdkfd_debug_mem_fence(struct amdgpu_device *adev)
748a70a93faSJonathan Kim {
749a70a93faSJonathan Kim amdgpu_device_flush_hdp(adev, NULL);
750a70a93faSJonathan Kim }
751a70a93faSJonathan Kim
amdgpu_amdkfd_is_fed(struct amdgpu_device * adev)752e1f6746fSLijo Lazar bool amdgpu_amdkfd_is_fed(struct amdgpu_device *adev)
753e1f6746fSLijo Lazar {
754e1f6746fSLijo Lazar return amdgpu_ras_get_fed_status(adev);
755e1f6746fSLijo Lazar }
756e1f6746fSLijo Lazar
amdgpu_amdkfd_ras_pasid_poison_consumption_handler(struct amdgpu_device * adev,enum amdgpu_ras_block block,uint16_t pasid,pasid_notify pasid_fn,void * data,uint32_t reset)757bfa579b3SYiPeng Chai void amdgpu_amdkfd_ras_pasid_poison_consumption_handler(struct amdgpu_device *adev,
758bfa579b3SYiPeng Chai enum amdgpu_ras_block block, uint16_t pasid,
759bfa579b3SYiPeng Chai pasid_notify pasid_fn, void *data, uint32_t reset)
760bfa579b3SYiPeng Chai {
761bfa579b3SYiPeng Chai amdgpu_umc_pasid_poison_handler(adev, block, pasid, pasid_fn, data, reset);
762bfa579b3SYiPeng Chai }
763bfa579b3SYiPeng Chai
amdgpu_amdkfd_ras_poison_consumption_handler(struct amdgpu_device * adev,enum amdgpu_ras_block block,uint32_t reset)764ed1e1e42SYiPeng Chai void amdgpu_amdkfd_ras_poison_consumption_handler(struct amdgpu_device *adev,
7652fc46e0bSTao Zhou enum amdgpu_ras_block block, uint32_t reset)
766c7490949STao Zhou {
767bfa579b3SYiPeng Chai amdgpu_umc_pasid_poison_handler(adev, block, 0, NULL, NULL, reset);
768c7490949STao Zhou }
7696475ae2bSTao Zhou
amdgpu_amdkfd_send_close_event_drain_irq(struct amdgpu_device * adev,uint32_t * payload)77012fb1ad7SJonathan Kim int amdgpu_amdkfd_send_close_event_drain_irq(struct amdgpu_device *adev,
77112fb1ad7SJonathan Kim uint32_t *payload)
77212fb1ad7SJonathan Kim {
77312fb1ad7SJonathan Kim int ret;
77412fb1ad7SJonathan Kim
77512fb1ad7SJonathan Kim /* Device or IH ring is not ready so bail. */
77612fb1ad7SJonathan Kim ret = amdgpu_ih_wait_on_checkpoint_process_ts(adev, &adev->irq.ih);
77712fb1ad7SJonathan Kim if (ret)
77812fb1ad7SJonathan Kim return ret;
77912fb1ad7SJonathan Kim
78012fb1ad7SJonathan Kim /* Send payload to fence KFD interrupts */
78112fb1ad7SJonathan Kim amdgpu_amdkfd_interrupt(adev, payload);
78212fb1ad7SJonathan Kim
78312fb1ad7SJonathan Kim return 0;
78412fb1ad7SJonathan Kim }
78512fb1ad7SJonathan Kim
amdgpu_amdkfd_check_and_lock_kfd(struct amdgpu_device * adev)7860c7315e7SMukul Joshi int amdgpu_amdkfd_check_and_lock_kfd(struct amdgpu_device *adev)
7870c7315e7SMukul Joshi {
7880c7315e7SMukul Joshi return kgd2kfd_check_and_lock_kfd();
7890c7315e7SMukul Joshi }
7900c7315e7SMukul Joshi
amdgpu_amdkfd_unlock_kfd(struct amdgpu_device * adev)7910c7315e7SMukul Joshi void amdgpu_amdkfd_unlock_kfd(struct amdgpu_device *adev)
7920c7315e7SMukul Joshi {
7930c7315e7SMukul Joshi kgd2kfd_unlock_kfd();
7940c7315e7SMukul Joshi }
79545b3a914SAlex Deucher
79645b3a914SAlex Deucher
amdgpu_amdkfd_xcp_memory_size(struct amdgpu_device * adev,int xcp_id)79745b3a914SAlex Deucher u64 amdgpu_amdkfd_xcp_memory_size(struct amdgpu_device *adev, int xcp_id)
79845b3a914SAlex Deucher {
79945b3a914SAlex Deucher s8 mem_id = KFD_XCP_MEM_ID(adev, xcp_id);
800f4bff6e0SRajneesh Bhardwaj u64 tmp;
80145b3a914SAlex Deucher
80245b3a914SAlex Deucher if (adev->gmc.num_mem_partitions && xcp_id >= 0 && mem_id >= 0) {
803f4bff6e0SRajneesh Bhardwaj if (adev->gmc.is_app_apu && adev->gmc.num_mem_partitions == 1) {
804f4bff6e0SRajneesh Bhardwaj /* In NPS1 mode, we should restrict the vram reporting
805f4bff6e0SRajneesh Bhardwaj * tied to the ttm_pages_limit which is 1/2 of the system
806f4bff6e0SRajneesh Bhardwaj * memory. For other partition modes, the HBM is uniformly
807f4bff6e0SRajneesh Bhardwaj * divided already per numa node reported. If user wants to
808f4bff6e0SRajneesh Bhardwaj * go beyond the default ttm limit and maximize the ROCm
809f4bff6e0SRajneesh Bhardwaj * allocations, they can go up to max ttm and sysmem limits.
810f4bff6e0SRajneesh Bhardwaj */
811f4bff6e0SRajneesh Bhardwaj
812f4bff6e0SRajneesh Bhardwaj tmp = (ttm_tt_pages_limit() << PAGE_SHIFT) / num_online_nodes();
813f4bff6e0SRajneesh Bhardwaj } else {
81445b3a914SAlex Deucher tmp = adev->gmc.mem_partitions[mem_id].size;
815f4bff6e0SRajneesh Bhardwaj }
81645b3a914SAlex Deucher do_div(tmp, adev->xcp_mgr->num_xcp_per_mem_partition);
817acf429dcSPhilip Yang return ALIGN_DOWN(tmp, PAGE_SIZE);
81889773b85SLang Yu } else if (adev->flags & AMD_IS_APU) {
81989773b85SLang Yu return (ttm_tt_pages_limit() << PAGE_SHIFT);
82045b3a914SAlex Deucher } else {
82145b3a914SAlex Deucher return adev->gmc.real_vram_size;
82245b3a914SAlex Deucher }
82345b3a914SAlex Deucher }
8249041b53aSMukul Joshi
amdgpu_amdkfd_unmap_hiq(struct amdgpu_device * adev,u32 doorbell_off,u32 inst)8259041b53aSMukul Joshi int amdgpu_amdkfd_unmap_hiq(struct amdgpu_device *adev, u32 doorbell_off,
8269041b53aSMukul Joshi u32 inst)
8279041b53aSMukul Joshi {
8289041b53aSMukul Joshi struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst];
8299041b53aSMukul Joshi struct amdgpu_ring *kiq_ring = &kiq->ring;
830bd3c4142SSrinivasan Shanmugam struct amdgpu_ring_funcs *ring_funcs;
831bd3c4142SSrinivasan Shanmugam struct amdgpu_ring *ring;
8329041b53aSMukul Joshi int r = 0;
8339041b53aSMukul Joshi
8349041b53aSMukul Joshi if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
8359041b53aSMukul Joshi return -EINVAL;
8369041b53aSMukul Joshi
837bd3c4142SSrinivasan Shanmugam ring_funcs = kzalloc(sizeof(*ring_funcs), GFP_KERNEL);
838bd3c4142SSrinivasan Shanmugam if (!ring_funcs)
839bd3c4142SSrinivasan Shanmugam return -ENOMEM;
8409041b53aSMukul Joshi
841bd3c4142SSrinivasan Shanmugam ring = kzalloc(sizeof(*ring), GFP_KERNEL);
842bd3c4142SSrinivasan Shanmugam if (!ring) {
843bd3c4142SSrinivasan Shanmugam r = -ENOMEM;
844bd3c4142SSrinivasan Shanmugam goto free_ring_funcs;
845bd3c4142SSrinivasan Shanmugam }
846bd3c4142SSrinivasan Shanmugam
847bd3c4142SSrinivasan Shanmugam ring_funcs->type = AMDGPU_RING_TYPE_COMPUTE;
848bd3c4142SSrinivasan Shanmugam ring->doorbell_index = doorbell_off;
849bd3c4142SSrinivasan Shanmugam ring->funcs = ring_funcs;
8509041b53aSMukul Joshi
8519041b53aSMukul Joshi spin_lock(&kiq->ring_lock);
8529041b53aSMukul Joshi
8539041b53aSMukul Joshi if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size)) {
8549041b53aSMukul Joshi spin_unlock(&kiq->ring_lock);
855bd3c4142SSrinivasan Shanmugam r = -ENOMEM;
856bd3c4142SSrinivasan Shanmugam goto free_ring;
8579041b53aSMukul Joshi }
8589041b53aSMukul Joshi
859bd3c4142SSrinivasan Shanmugam kiq->pmf->kiq_unmap_queues(kiq_ring, ring, RESET_QUEUES, 0, 0);
8609041b53aSMukul Joshi
8619041b53aSMukul Joshi if (kiq_ring->sched.ready && !adev->job_hang)
8629041b53aSMukul Joshi r = amdgpu_ring_test_helper(kiq_ring);
8639041b53aSMukul Joshi
8649041b53aSMukul Joshi spin_unlock(&kiq->ring_lock);
8659041b53aSMukul Joshi
866bd3c4142SSrinivasan Shanmugam free_ring:
867bd3c4142SSrinivasan Shanmugam kfree(ring);
868bd3c4142SSrinivasan Shanmugam
869bd3c4142SSrinivasan Shanmugam free_ring_funcs:
870bd3c4142SSrinivasan Shanmugam kfree(ring_funcs);
871bd3c4142SSrinivasan Shanmugam
8729041b53aSMukul Joshi return r;
8739041b53aSMukul Joshi }
874*234eebe1SAmber Lin
875*234eebe1SAmber Lin /* Stop scheduling on KFD */
amdgpu_amdkfd_stop_sched(struct amdgpu_device * adev,uint32_t node_id)876*234eebe1SAmber Lin int amdgpu_amdkfd_stop_sched(struct amdgpu_device *adev, uint32_t node_id)
877*234eebe1SAmber Lin {
878*234eebe1SAmber Lin if (!adev->kfd.init_complete)
879*234eebe1SAmber Lin return 0;
880*234eebe1SAmber Lin
881*234eebe1SAmber Lin return kgd2kfd_stop_sched(adev->kfd.dev, node_id);
882*234eebe1SAmber Lin }
883*234eebe1SAmber Lin
884*234eebe1SAmber Lin /* Start scheduling on KFD */
amdgpu_amdkfd_start_sched(struct amdgpu_device * adev,uint32_t node_id)885*234eebe1SAmber Lin int amdgpu_amdkfd_start_sched(struct amdgpu_device *adev, uint32_t node_id)
886*234eebe1SAmber Lin {
887*234eebe1SAmber Lin if (!adev->kfd.init_complete)
888*234eebe1SAmber Lin return 0;
889*234eebe1SAmber Lin
890*234eebe1SAmber Lin return kgd2kfd_start_sched(adev->kfd.dev, node_id);
891*234eebe1SAmber Lin }
892