1d87f36a0SRajneesh Bhardwaj // SPDX-License-Identifier: GPL-2.0 OR MIT
24a488a7aSOded Gabbay /*
3d87f36a0SRajneesh Bhardwaj * Copyright 2014-2022 Advanced Micro Devices, Inc.
44a488a7aSOded Gabbay *
54a488a7aSOded Gabbay * Permission is hereby granted, free of charge, to any person obtaining a
64a488a7aSOded Gabbay * copy of this software and associated documentation files (the "Software"),
74a488a7aSOded Gabbay * to deal in the Software without restriction, including without limitation
84a488a7aSOded Gabbay * the rights to use, copy, modify, merge, publish, distribute, sublicense,
94a488a7aSOded Gabbay * and/or sell copies of the Software, and to permit persons to whom the
104a488a7aSOded Gabbay * Software is furnished to do so, subject to the following conditions:
114a488a7aSOded Gabbay *
124a488a7aSOded Gabbay * The above copyright notice and this permission notice shall be included in
134a488a7aSOded Gabbay * all copies or substantial portions of the Software.
144a488a7aSOded Gabbay *
154a488a7aSOded Gabbay * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
164a488a7aSOded Gabbay * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174a488a7aSOded Gabbay * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
184a488a7aSOded Gabbay * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
194a488a7aSOded Gabbay * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
204a488a7aSOded Gabbay * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
214a488a7aSOded Gabbay * OTHER DEALINGS IN THE SOFTWARE.
224a488a7aSOded Gabbay */
234a488a7aSOded Gabbay
244a488a7aSOded Gabbay #include <linux/bsearch.h>
254a488a7aSOded Gabbay #include <linux/pci.h>
264a488a7aSOded Gabbay #include <linux/slab.h>
274a488a7aSOded Gabbay #include "kfd_priv.h"
2864c7f8cfSBen Goz #include "kfd_device_queue_manager.h"
29507968ddSFelix Kuehling #include "kfd_pm4_headers_vi.h"
30fd6a440eSJonathan Kim #include "kfd_pm4_headers_aldebaran.h"
310db54b24SYong Zhao #include "cwsr_trap_handler.h"
325b87245fSAmber Lin #include "amdgpu_amdkfd.h"
332c2b0d88SMukul Joshi #include "kfd_smi_events.h"
3484b4dd3fSPhilip Yang #include "kfd_svm.h"
35814ab993SPhilip Yang #include "kfd_migrate.h"
365b983db8SAlex Deucher #include "amdgpu.h"
378e7fd193SLijo Lazar #include "amdgpu_xcp.h"
384a488a7aSOded Gabbay
3919f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768
40e42051d2SShaoyun Liu
41e42051d2SShaoyun Liu /*
42e42051d2SShaoyun Liu * kfd_locked is used to lock the kfd driver during suspend or reset
43e42051d2SShaoyun Liu * once locked, kfd driver will stop any further GPU execution.
44e42051d2SShaoyun Liu * create process (open) will return -EAGAIN.
45e42051d2SShaoyun Liu */
46fe1f05dfSMukul Joshi static int kfd_locked;
4719f6d2a6SOded Gabbay
48a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
49e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v7_kfd2kgd;
50a3e520a2SAlex Deucher #endif
51e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v8_kfd2kgd;
52e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v9_kfd2kgd;
53e392c887SYong Zhao extern const struct kfd2kgd_calls arcturus_kfd2kgd;
545073506cSJonathan Kim extern const struct kfd2kgd_calls aldebaran_kfd2kgd;
55f544afacSAmber Lin extern const struct kfd2kgd_calls gc_9_4_3_kfd2kgd;
56e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v10_kfd2kgd;
573a2f0c81SYong Zhao extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd;
58cc009e61SMukul Joshi extern const struct kfd2kgd_calls gfx_v11_kfd2kgd;
59e392c887SYong Zhao
606e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
616e81090bSOded Gabbay unsigned int chunk_size);
626e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
636e81090bSOded Gabbay
648dc1db31SMukul Joshi static int kfd_resume(struct kfd_node *kfd);
65b8935a7cSYong Zhao
kfd_device_info_set_sdma_info(struct kfd_dev * kfd)66cc009e61SMukul Joshi static void kfd_device_info_set_sdma_info(struct kfd_dev *kfd)
67f89c6bf7SGuchun Chen {
68f89c6bf7SGuchun Chen uint32_t sdma_version = kfd->adev->ip_versions[SDMA0_HWIP][0];
69f89c6bf7SGuchun Chen
70f89c6bf7SGuchun Chen switch (sdma_version) {
71f89c6bf7SGuchun Chen case IP_VERSION(4, 0, 0):/* VEGA10 */
72f89c6bf7SGuchun Chen case IP_VERSION(4, 0, 1):/* VEGA12 */
73f89c6bf7SGuchun Chen case IP_VERSION(4, 1, 0):/* RAVEN */
74f89c6bf7SGuchun Chen case IP_VERSION(4, 1, 1):/* RAVEN */
755eb877b2SKent Russell case IP_VERSION(4, 1, 2):/* RENOIR */
76f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 1):/* VANGOGH */
77f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 3):/* YELLOW_CARP */
787c4f4f19SMario Limonciello case IP_VERSION(5, 2, 6):/* GC 10.3.6 */
797c4f4f19SMario Limonciello case IP_VERSION(5, 2, 7):/* GC 10.3.7 */
80f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 2;
81f89c6bf7SGuchun Chen break;
82f89c6bf7SGuchun Chen case IP_VERSION(4, 2, 0):/* VEGA20 */
835eb877b2SKent Russell case IP_VERSION(4, 2, 2):/* ARCTURUS */
84f89c6bf7SGuchun Chen case IP_VERSION(4, 4, 0):/* ALDEBARAN */
851bd6dd21SMukul Joshi case IP_VERSION(4, 4, 2):
86f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 0):/* NAVI10 */
87f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 1):/* CYAN_SKILLFISH */
88f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 2):/* NAVI14 */
89f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 5):/* NAVI12 */
90f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 0):/* SIENNA_CICHLID */
915eb877b2SKent Russell case IP_VERSION(5, 2, 2):/* NAVY_FLOUNDER */
92f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 4):/* DIMGREY_CAVEFISH */
93f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 5):/* BEIGE_GOBY */
94cc009e61SMukul Joshi case IP_VERSION(6, 0, 0):
95efb4fd10SYifan Zhang case IP_VERSION(6, 0, 1):
9622dd871eSEric Huang case IP_VERSION(6, 0, 2):
975ddb5fe9SDavid Belanger case IP_VERSION(6, 0, 3):
98f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 8;
99f89c6bf7SGuchun Chen break;
100f89c6bf7SGuchun Chen default:
101f89c6bf7SGuchun Chen dev_warn(kfd_device,
10220c5e425SGraham Sider "Default sdma queue per engine(8) is set due to mismatch of sdma ip block(SDMA_HWIP:0x%x).\n",
103f89c6bf7SGuchun Chen sdma_version);
104f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 8;
105f89c6bf7SGuchun Chen }
106cc009e61SMukul Joshi
107597364adSMukul Joshi bitmap_zero(kfd->device_info.reserved_sdma_queues_bitmap, KFD_MAX_SDMA_QUEUES);
108597364adSMukul Joshi
109cc009e61SMukul Joshi switch (sdma_version) {
110cc009e61SMukul Joshi case IP_VERSION(6, 0, 0):
111597364adSMukul Joshi case IP_VERSION(6, 0, 1):
11222dd871eSEric Huang case IP_VERSION(6, 0, 2):
1135ddb5fe9SDavid Belanger case IP_VERSION(6, 0, 3):
114cc009e61SMukul Joshi /* Reserve 1 for paging and 1 for gfx */
115cc009e61SMukul Joshi kfd->device_info.num_reserved_sdma_queues_per_engine = 2;
116cc009e61SMukul Joshi /* BIT(0)=engine-0 queue-0; BIT(1)=engine-1 queue-0; BIT(2)=engine-0 queue-1; ... */
117597364adSMukul Joshi bitmap_set(kfd->device_info.reserved_sdma_queues_bitmap, 0,
118597364adSMukul Joshi kfd->adev->sdma.num_instances *
119597364adSMukul Joshi kfd->device_info.num_reserved_sdma_queues_per_engine);
120e48e6a13SYifan Zhang break;
121cc009e61SMukul Joshi default:
122cc009e61SMukul Joshi break;
123cc009e61SMukul Joshi }
124f89c6bf7SGuchun Chen }
125f89c6bf7SGuchun Chen
kfd_device_info_set_event_interrupt_class(struct kfd_dev * kfd)126f89c6bf7SGuchun Chen static void kfd_device_info_set_event_interrupt_class(struct kfd_dev *kfd)
127f89c6bf7SGuchun Chen {
128f89c6bf7SGuchun Chen uint32_t gc_version = KFD_GC_VERSION(kfd);
129f89c6bf7SGuchun Chen
130f89c6bf7SGuchun Chen switch (gc_version) {
131f89c6bf7SGuchun Chen case IP_VERSION(9, 0, 1): /* VEGA10 */
132f89c6bf7SGuchun Chen case IP_VERSION(9, 1, 0): /* RAVEN */
133f89c6bf7SGuchun Chen case IP_VERSION(9, 2, 1): /* VEGA12 */
134f89c6bf7SGuchun Chen case IP_VERSION(9, 2, 2): /* RAVEN */
135f89c6bf7SGuchun Chen case IP_VERSION(9, 3, 0): /* RENOIR */
136f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 0): /* VEGA20 */
137f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 1): /* ARCTURUS */
138f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 2): /* ALDEBARAN */
13912fb1ad7SJonathan Kim kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;
14012fb1ad7SJonathan Kim break;
141d4300362SMukul Joshi case IP_VERSION(9, 4, 3): /* GC 9.4.3 */
142d4300362SMukul Joshi kfd->device_info.event_interrupt_class =
143d4300362SMukul Joshi &event_interrupt_class_v9_4_3;
144d4300362SMukul Joshi break;
145f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 1): /* VANGOGH */
146f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 3): /* YELLOW_CARP */
1477c4f4f19SMario Limonciello case IP_VERSION(10, 3, 6): /* GC 10.3.6 */
1487c4f4f19SMario Limonciello case IP_VERSION(10, 3, 7): /* GC 10.3.7 */
149f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 3): /* CYAN_SKILLFISH */
150f9ed188dSLang Yu case IP_VERSION(10, 1, 4):
151f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 10): /* NAVI10 */
152f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 2): /* NAVI12 */
153f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 1): /* NAVI14 */
154f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 0): /* SIENNA_CICHLID */
155f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 2): /* NAVY_FLOUNDER */
156f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 4): /* DIMGREY_CAVEFISH */
157f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 5): /* BEIGE_GOBY */
15812fb1ad7SJonathan Kim kfd->device_info.event_interrupt_class = &event_interrupt_class_v10;
159f89c6bf7SGuchun Chen break;
160cc009e61SMukul Joshi case IP_VERSION(11, 0, 0):
16126776a70SHuang Rui case IP_VERSION(11, 0, 1):
162ec661f1cSEric Huang case IP_VERSION(11, 0, 2):
1635ddb5fe9SDavid Belanger case IP_VERSION(11, 0, 3):
16488c21c2bSYifan Zhang case IP_VERSION(11, 0, 4):
165cc009e61SMukul Joshi kfd->device_info.event_interrupt_class = &event_interrupt_class_v11;
166cc009e61SMukul Joshi break;
167f89c6bf7SGuchun Chen default:
168f89c6bf7SGuchun Chen dev_warn(kfd_device, "v9 event interrupt handler is set due to "
169f89c6bf7SGuchun Chen "mismatch of gc ip block(GC_HWIP:0x%x).\n", gc_version);
170f89c6bf7SGuchun Chen kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;
171f89c6bf7SGuchun Chen }
172f89c6bf7SGuchun Chen }
173f89c6bf7SGuchun Chen
kfd_device_info_init(struct kfd_dev * kfd,bool vf,uint32_t gfx_target_version)174f0dc99a6SGraham Sider static void kfd_device_info_init(struct kfd_dev *kfd,
175f0dc99a6SGraham Sider bool vf, uint32_t gfx_target_version)
176f0dc99a6SGraham Sider {
177f0dc99a6SGraham Sider uint32_t gc_version = KFD_GC_VERSION(kfd);
178f0dc99a6SGraham Sider uint32_t asic_type = kfd->adev->asic_type;
179f0dc99a6SGraham Sider
180f0dc99a6SGraham Sider kfd->device_info.max_pasid_bits = 16;
181f0dc99a6SGraham Sider kfd->device_info.max_no_of_hqd = 24;
182f0dc99a6SGraham Sider kfd->device_info.num_of_watch_points = 4;
183f0dc99a6SGraham Sider kfd->device_info.mqd_size_aligned = MQD_SIZE_ALIGNED;
184f0dc99a6SGraham Sider kfd->device_info.gfx_target_version = gfx_target_version;
185f0dc99a6SGraham Sider
186f0dc99a6SGraham Sider if (KFD_IS_SOC15(kfd)) {
187f0dc99a6SGraham Sider kfd->device_info.doorbell_size = 8;
188f0dc99a6SGraham Sider kfd->device_info.ih_ring_entry_size = 8 * sizeof(uint32_t);
189f0dc99a6SGraham Sider kfd->device_info.supports_cwsr = true;
190f0dc99a6SGraham Sider
191cc009e61SMukul Joshi kfd_device_info_set_sdma_info(kfd);
192f89c6bf7SGuchun Chen
193f89c6bf7SGuchun Chen kfd_device_info_set_event_interrupt_class(kfd);
194f0dc99a6SGraham Sider
195f0dc99a6SGraham Sider if (gc_version < IP_VERSION(11, 0, 0)) {
196f0dc99a6SGraham Sider /* Navi2x+, Navi1x+ */
19768e355c0SJesse Zhang if (gc_version == IP_VERSION(10, 3, 6))
19868e355c0SJesse Zhang kfd->device_info.no_atomic_fw_version = 14;
199c4e85551SPrike Liang else if (gc_version == IP_VERSION(10, 3, 7))
200c4e85551SPrike Liang kfd->device_info.no_atomic_fw_version = 3;
20168e355c0SJesse Zhang else if (gc_version >= IP_VERSION(10, 3, 0))
202f0dc99a6SGraham Sider kfd->device_info.no_atomic_fw_version = 92;
20327cc310fSchen gong else if (gc_version >= IP_VERSION(10, 1, 1))
20427cc310fSchen gong kfd->device_info.no_atomic_fw_version = 145;
205f0dc99a6SGraham Sider
206f0dc99a6SGraham Sider /* Navi1x+ */
207f0dc99a6SGraham Sider if (gc_version >= IP_VERSION(10, 1, 1))
208f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics = true;
20900fa4035SSreekant Somasekharan } else if (gc_version < IP_VERSION(12, 0, 0)) {
21000fa4035SSreekant Somasekharan /*
21100fa4035SSreekant Somasekharan * PCIe atomics support acknowledgment in GFX11 RS64 CPFW requires
21200fa4035SSreekant Somasekharan * MEC version >= 509. Prior RS64 CPFW versions (and all F32) require
21300fa4035SSreekant Somasekharan * PCIe atomics support.
21400fa4035SSreekant Somasekharan */
21500fa4035SSreekant Somasekharan kfd->device_info.needs_pci_atomics = true;
21600fa4035SSreekant Somasekharan kfd->device_info.no_atomic_fw_version = kfd->adev->gfx.rs64_enable ? 509 : 0;
217f0dc99a6SGraham Sider }
218f0dc99a6SGraham Sider } else {
219f0dc99a6SGraham Sider kfd->device_info.doorbell_size = 4;
220f0dc99a6SGraham Sider kfd->device_info.ih_ring_entry_size = 4 * sizeof(uint32_t);
221f0dc99a6SGraham Sider kfd->device_info.event_interrupt_class = &event_interrupt_class_cik;
222f0dc99a6SGraham Sider kfd->device_info.num_sdma_queues_per_engine = 2;
223f0dc99a6SGraham Sider
224f0dc99a6SGraham Sider if (asic_type != CHIP_KAVERI &&
225f0dc99a6SGraham Sider asic_type != CHIP_HAWAII &&
226f0dc99a6SGraham Sider asic_type != CHIP_TONGA)
227f0dc99a6SGraham Sider kfd->device_info.supports_cwsr = true;
228f0dc99a6SGraham Sider
229f0dc99a6SGraham Sider if (asic_type != CHIP_HAWAII && !vf)
230f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics = true;
231f0dc99a6SGraham Sider }
232f0dc99a6SGraham Sider }
233f0dc99a6SGraham Sider
kgd2kfd_probe(struct amdgpu_device * adev,bool vf)234b5d1d755SGraham Sider struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf)
2354a488a7aSOded Gabbay {
236f0dc99a6SGraham Sider struct kfd_dev *kfd = NULL;
237f0dc99a6SGraham Sider const struct kfd2kgd_calls *f2g = NULL;
238f0dc99a6SGraham Sider uint32_t gfx_target_version = 0;
239050091abSYong Zhao
240c868d584SAlex Deucher switch (adev->asic_type) {
241c868d584SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
242c868d584SAlex Deucher case CHIP_KAVERI:
243f0dc99a6SGraham Sider gfx_target_version = 70000;
244f0dc99a6SGraham Sider if (!vf)
245c868d584SAlex Deucher f2g = &gfx_v7_kfd2kgd;
246c868d584SAlex Deucher break;
247c868d584SAlex Deucher #endif
248c868d584SAlex Deucher case CHIP_CARRIZO:
249f0dc99a6SGraham Sider gfx_target_version = 80001;
250f0dc99a6SGraham Sider if (!vf)
251c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd;
252c868d584SAlex Deucher break;
253c868d584SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
254c868d584SAlex Deucher case CHIP_HAWAII:
255f0dc99a6SGraham Sider gfx_target_version = 70001;
2560f7ef0b9SFelix Kuehling if (!amdgpu_exp_hw_support)
2570f7ef0b9SFelix Kuehling pr_info(
2580f7ef0b9SFelix Kuehling "KFD support on Hawaii is experimental. See modparam exp_hw_support\n"
2590f7ef0b9SFelix Kuehling );
2600f7ef0b9SFelix Kuehling else if (!vf)
261c868d584SAlex Deucher f2g = &gfx_v7_kfd2kgd;
262c868d584SAlex Deucher break;
263c868d584SAlex Deucher #endif
264c868d584SAlex Deucher case CHIP_TONGA:
265f0dc99a6SGraham Sider gfx_target_version = 80002;
266f0dc99a6SGraham Sider if (!vf)
267c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd;
268c868d584SAlex Deucher break;
269c868d584SAlex Deucher case CHIP_FIJI:
270c868d584SAlex Deucher case CHIP_POLARIS10:
271f0dc99a6SGraham Sider gfx_target_version = 80003;
272c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd;
273c868d584SAlex Deucher break;
274c868d584SAlex Deucher case CHIP_POLARIS11:
275c868d584SAlex Deucher case CHIP_POLARIS12:
276c868d584SAlex Deucher case CHIP_VEGAM:
277f0dc99a6SGraham Sider gfx_target_version = 80003;
278f0dc99a6SGraham Sider if (!vf)
279c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd;
280c868d584SAlex Deucher break;
281c868d584SAlex Deucher default:
282c868d584SAlex Deucher switch (adev->ip_versions[GC_HWIP][0]) {
2832c1f19b3SGraham Sider /* Vega 10 */
284c868d584SAlex Deucher case IP_VERSION(9, 0, 1):
285f0dc99a6SGraham Sider gfx_target_version = 90000;
286c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd;
287c868d584SAlex Deucher break;
2882c1f19b3SGraham Sider /* Raven */
289c868d584SAlex Deucher case IP_VERSION(9, 1, 0):
290c868d584SAlex Deucher case IP_VERSION(9, 2, 2):
291f0dc99a6SGraham Sider gfx_target_version = 90002;
292f0dc99a6SGraham Sider if (!vf)
293c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd;
294c868d584SAlex Deucher break;
2952c1f19b3SGraham Sider /* Vega12 */
296c868d584SAlex Deucher case IP_VERSION(9, 2, 1):
297f0dc99a6SGraham Sider gfx_target_version = 90004;
298f0dc99a6SGraham Sider if (!vf)
299c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd;
300c868d584SAlex Deucher break;
3012c1f19b3SGraham Sider /* Renoir */
302c868d584SAlex Deucher case IP_VERSION(9, 3, 0):
303f0dc99a6SGraham Sider gfx_target_version = 90012;
304f0dc99a6SGraham Sider if (!vf)
305c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd;
306c868d584SAlex Deucher break;
3072c1f19b3SGraham Sider /* Vega20 */
308c868d584SAlex Deucher case IP_VERSION(9, 4, 0):
309f0dc99a6SGraham Sider gfx_target_version = 90006;
310f0dc99a6SGraham Sider if (!vf)
311c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd;
312c868d584SAlex Deucher break;
3132c1f19b3SGraham Sider /* Arcturus */
314c868d584SAlex Deucher case IP_VERSION(9, 4, 1):
315f0dc99a6SGraham Sider gfx_target_version = 90008;
316c868d584SAlex Deucher f2g = &arcturus_kfd2kgd;
317c868d584SAlex Deucher break;
3182c1f19b3SGraham Sider /* Aldebaran */
319c868d584SAlex Deucher case IP_VERSION(9, 4, 2):
320f0dc99a6SGraham Sider gfx_target_version = 90010;
321c868d584SAlex Deucher f2g = &aldebaran_kfd2kgd;
322c868d584SAlex Deucher break;
32370bdfedaSGraham Sider case IP_VERSION(9, 4, 3):
32407a14752SGraham Sider gfx_target_version = adev->rev_id >= 1 ? 90402
32507a14752SGraham Sider : adev->flags & AMD_IS_APU ? 90400
32607a14752SGraham Sider : 90401;
327f544afacSAmber Lin f2g = &gc_9_4_3_kfd2kgd;
32870bdfedaSGraham Sider break;
3292c1f19b3SGraham Sider /* Navi10 */
330c868d584SAlex Deucher case IP_VERSION(10, 1, 10):
331f0dc99a6SGraham Sider gfx_target_version = 100100;
332f0dc99a6SGraham Sider if (!vf)
333c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd;
334c868d584SAlex Deucher break;
3352c1f19b3SGraham Sider /* Navi12 */
336c868d584SAlex Deucher case IP_VERSION(10, 1, 2):
337f0dc99a6SGraham Sider gfx_target_version = 100101;
338c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd;
339c868d584SAlex Deucher break;
3402c1f19b3SGraham Sider /* Navi14 */
341c868d584SAlex Deucher case IP_VERSION(10, 1, 1):
342f0dc99a6SGraham Sider gfx_target_version = 100102;
343f0dc99a6SGraham Sider if (!vf)
344c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd;
345c868d584SAlex Deucher break;
3462c1f19b3SGraham Sider /* Cyan Skillfish */
347c868d584SAlex Deucher case IP_VERSION(10, 1, 3):
348f9ed188dSLang Yu case IP_VERSION(10, 1, 4):
349f0dc99a6SGraham Sider gfx_target_version = 100103;
350f0dc99a6SGraham Sider if (!vf)
351c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd;
352c868d584SAlex Deucher break;
3532c1f19b3SGraham Sider /* Sienna Cichlid */
354c868d584SAlex Deucher case IP_VERSION(10, 3, 0):
355f0dc99a6SGraham Sider gfx_target_version = 100300;
356c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd;
357c868d584SAlex Deucher break;
3582c1f19b3SGraham Sider /* Navy Flounder */
359c868d584SAlex Deucher case IP_VERSION(10, 3, 2):
360f0dc99a6SGraham Sider gfx_target_version = 100301;
361c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd;
362c868d584SAlex Deucher break;
3632c1f19b3SGraham Sider /* Van Gogh */
364c868d584SAlex Deucher case IP_VERSION(10, 3, 1):
365f0dc99a6SGraham Sider gfx_target_version = 100303;
366f0dc99a6SGraham Sider if (!vf)
367c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd;
368c868d584SAlex Deucher break;
3692c1f19b3SGraham Sider /* Dimgrey Cavefish */
370c868d584SAlex Deucher case IP_VERSION(10, 3, 4):
371f0dc99a6SGraham Sider gfx_target_version = 100302;
372c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd;
373c868d584SAlex Deucher break;
3742c1f19b3SGraham Sider /* Beige Goby */
375c868d584SAlex Deucher case IP_VERSION(10, 3, 5):
376f0dc99a6SGraham Sider gfx_target_version = 100304;
377c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd;
378c868d584SAlex Deucher break;
3792c1f19b3SGraham Sider /* Yellow Carp */
380c868d584SAlex Deucher case IP_VERSION(10, 3, 3):
381f0dc99a6SGraham Sider gfx_target_version = 100305;
382f0dc99a6SGraham Sider if (!vf)
383c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd;
384c868d584SAlex Deucher break;
3857c4f4f19SMario Limonciello case IP_VERSION(10, 3, 6):
3867c4f4f19SMario Limonciello case IP_VERSION(10, 3, 7):
3872724efa3SPrike Liang gfx_target_version = 100306;
3887c4f4f19SMario Limonciello if (!vf)
3897c4f4f19SMario Limonciello f2g = &gfx_v10_3_kfd2kgd;
3907c4f4f19SMario Limonciello break;
391cc009e61SMukul Joshi case IP_VERSION(11, 0, 0):
392cc009e61SMukul Joshi gfx_target_version = 110000;
393cc009e61SMukul Joshi f2g = &gfx_v11_kfd2kgd;
394cc009e61SMukul Joshi break;
39526776a70SHuang Rui case IP_VERSION(11, 0, 1):
39688c21c2bSYifan Zhang case IP_VERSION(11, 0, 4):
39726776a70SHuang Rui gfx_target_version = 110003;
39826776a70SHuang Rui f2g = &gfx_v11_kfd2kgd;
39926776a70SHuang Rui break;
400ec661f1cSEric Huang case IP_VERSION(11, 0, 2):
401ec661f1cSEric Huang gfx_target_version = 110002;
402ec661f1cSEric Huang f2g = &gfx_v11_kfd2kgd;
403ec661f1cSEric Huang break;
4045ddb5fe9SDavid Belanger case IP_VERSION(11, 0, 3):
4055ddb5fe9SDavid Belanger /* Note: Compiler version is 11.0.1 while HW version is 11.0.3 */
4065ddb5fe9SDavid Belanger gfx_target_version = 110001;
4075ddb5fe9SDavid Belanger f2g = &gfx_v11_kfd2kgd;
4085ddb5fe9SDavid Belanger break;
409c868d584SAlex Deucher default:
410f0dc99a6SGraham Sider break;
411050091abSYong Zhao }
412c868d584SAlex Deucher break;
413c868d584SAlex Deucher }
4144a488a7aSOded Gabbay
415f0dc99a6SGraham Sider if (!f2g) {
416e4804a39SGraham Sider if (adev->ip_versions[GC_HWIP][0])
417e4804a39SGraham Sider dev_err(kfd_device, "GC IP %06x %s not supported in kfd\n",
418e4804a39SGraham Sider adev->ip_versions[GC_HWIP][0], vf ? "VF" : "");
419e4804a39SGraham Sider else
420050091abSYong Zhao dev_err(kfd_device, "%s %s not supported in kfd\n",
421c868d584SAlex Deucher amdgpu_asic_name[adev->asic_type], vf ? "VF" : "");
4224a488a7aSOded Gabbay return NULL;
4234ebc7182SYong Zhao }
4244a488a7aSOded Gabbay
425d35f00d8SEric Huang kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
426d35f00d8SEric Huang if (!kfd)
427d35f00d8SEric Huang return NULL;
428d35f00d8SEric Huang
429c6c57446SGraham Sider kfd->adev = adev;
430f0dc99a6SGraham Sider kfd_device_info_init(kfd, vf, gfx_target_version);
43119f6d2a6SOded Gabbay kfd->init_complete = false;
432cea405b1SXihan Zhang kfd->kfd2kgd = f2g;
43343d8107fSHarish Kasiviswanathan atomic_set(&kfd->compute_profile, 0);
434cea405b1SXihan Zhang
435cea405b1SXihan Zhang mutex_init(&kfd->doorbell_mutex);
4364a488a7aSOded Gabbay
43759d7115dSMukul Joshi ida_init(&kfd->doorbell_ida);
43859d7115dSMukul Joshi
4394a488a7aSOded Gabbay return kfd;
4404a488a7aSOded Gabbay }
4414a488a7aSOded Gabbay
kfd_cwsr_init(struct kfd_dev * kfd)442373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
443373d7080SFelix Kuehling {
444f0dc99a6SGraham Sider if (cwsr_enable && kfd->device_info.supports_cwsr) {
445046e674bSGraham Sider if (KFD_GC_VERSION(kfd) < IP_VERSION(9, 0, 1)) {
446373d7080SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
447373d7080SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx8_hex;
448373d7080SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
449046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)) {
4503baa24f0SOak Zeng BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
4513baa24f0SOak Zeng kfd->cwsr_isa = cwsr_trap_arcturus_hex;
4523baa24f0SOak Zeng kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
453046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)) {
4540ef6845cSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE);
4550ef6845cSJay Cornwall kfd->cwsr_isa = cwsr_trap_aldebaran_hex;
4560ef6845cSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);
457828d9a87SHawking Zhang } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 3)) {
4581d44ff3dSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_4_3_hex) > PAGE_SIZE);
4591d44ff3dSJay Cornwall kfd->cwsr_isa = cwsr_trap_gfx9_4_3_hex;
4601d44ff3dSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_4_3_hex);
461046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 1, 1)) {
4623e76c239SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
4633e76c239SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx9_hex;
4643e76c239SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
465046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 3, 0)) {
46680b6cfedSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
46780b6cfedSJay Cornwall kfd->cwsr_isa = cwsr_trap_nv1x_hex;
46880b6cfedSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
4696a817038SJay Cornwall } else if (KFD_GC_VERSION(kfd) < IP_VERSION(11, 0, 0)) {
47014328aa5SPhilip Cox BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
47114328aa5SPhilip Cox kfd->cwsr_isa = cwsr_trap_gfx10_hex;
47214328aa5SPhilip Cox kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
4736a817038SJay Cornwall } else {
4746a817038SJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_gfx11_hex) > PAGE_SIZE);
4756a817038SJay Cornwall kfd->cwsr_isa = cwsr_trap_gfx11_hex;
4766a817038SJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx11_hex);
4773e76c239SFelix Kuehling }
4783e76c239SFelix Kuehling
479373d7080SFelix Kuehling kfd->cwsr_enabled = true;
480373d7080SFelix Kuehling }
481373d7080SFelix Kuehling }
482373d7080SFelix Kuehling
kfd_gws_init(struct kfd_node * node)4838dc1db31SMukul Joshi static int kfd_gws_init(struct kfd_node *node)
48429633d0eSJoseph Greathouse {
48529633d0eSJoseph Greathouse int ret = 0;
4868dc1db31SMukul Joshi struct kfd_dev *kfd = node->kfd;
4877a1c5c67SJonathan Kim uint32_t mes_rev = node->adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;
48829633d0eSJoseph Greathouse
4898dc1db31SMukul Joshi if (node->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
49029633d0eSJoseph Greathouse return 0;
49129633d0eSJoseph Greathouse
4928dc1db31SMukul Joshi if (hws_gws_support || (KFD_IS_SOC15(node) &&
4938dc1db31SMukul Joshi ((KFD_GC_VERSION(node) == IP_VERSION(9, 0, 1)
494046e674bSGraham Sider && kfd->mec2_fw_version >= 0x81b3) ||
4958dc1db31SMukul Joshi (KFD_GC_VERSION(node) <= IP_VERSION(9, 4, 0)
496046e674bSGraham Sider && kfd->mec2_fw_version >= 0x1b3) ||
4978dc1db31SMukul Joshi (KFD_GC_VERSION(node) == IP_VERSION(9, 4, 1)
498046e674bSGraham Sider && kfd->mec2_fw_version >= 0x30) ||
4998dc1db31SMukul Joshi (KFD_GC_VERSION(node) == IP_VERSION(9, 4, 2)
500beb15bc1SJonathan Kim && kfd->mec2_fw_version >= 0x28) ||
501fc133accSMukul Joshi (KFD_GC_VERSION(node) == IP_VERSION(9, 4, 3)) ||
5028dc1db31SMukul Joshi (KFD_GC_VERSION(node) >= IP_VERSION(10, 3, 0)
5038dc1db31SMukul Joshi && KFD_GC_VERSION(node) < IP_VERSION(11, 0, 0)
5047a1c5c67SJonathan Kim && kfd->mec2_fw_version >= 0x6b) ||
5057a1c5c67SJonathan Kim (KFD_GC_VERSION(node) >= IP_VERSION(11, 0, 0)
5067a1c5c67SJonathan Kim && KFD_GC_VERSION(node) < IP_VERSION(12, 0, 0)
5077a1c5c67SJonathan Kim && mes_rev >= 68))))
5088dc1db31SMukul Joshi ret = amdgpu_amdkfd_alloc_gws(node->adev,
5098dc1db31SMukul Joshi node->adev->gds.gws_size, &node->gws);
51029633d0eSJoseph Greathouse
51129633d0eSJoseph Greathouse return ret;
51229633d0eSJoseph Greathouse }
51329633d0eSJoseph Greathouse
kfd_smi_init(struct kfd_node * dev)5148dc1db31SMukul Joshi static void kfd_smi_init(struct kfd_node *dev)
5152243f493SRajneesh Bhardwaj {
516938a0650SAmber Lin INIT_LIST_HEAD(&dev->smi_clients);
517938a0650SAmber Lin spin_lock_init(&dev->smi_lock);
518938a0650SAmber Lin }
519938a0650SAmber Lin
kfd_init_node(struct kfd_node * node)5208dc1db31SMukul Joshi static int kfd_init_node(struct kfd_node *node)
5218dc1db31SMukul Joshi {
5228dc1db31SMukul Joshi int err = -1;
5238dc1db31SMukul Joshi
5248dc1db31SMukul Joshi if (kfd_interrupt_init(node)) {
5258dc1db31SMukul Joshi dev_err(kfd_device, "Error initializing interrupts\n");
5268dc1db31SMukul Joshi goto kfd_interrupt_error;
5278dc1db31SMukul Joshi }
5288dc1db31SMukul Joshi
5298dc1db31SMukul Joshi node->dqm = device_queue_manager_init(node);
5308dc1db31SMukul Joshi if (!node->dqm) {
5318dc1db31SMukul Joshi dev_err(kfd_device, "Error initializing queue manager\n");
5328dc1db31SMukul Joshi goto device_queue_manager_error;
5338dc1db31SMukul Joshi }
5348dc1db31SMukul Joshi
5358dc1db31SMukul Joshi if (kfd_gws_init(node)) {
5368dc1db31SMukul Joshi dev_err(kfd_device, "Could not allocate %d gws\n",
5378dc1db31SMukul Joshi node->adev->gds.gws_size);
5388dc1db31SMukul Joshi goto gws_error;
5398dc1db31SMukul Joshi }
5408dc1db31SMukul Joshi
5418dc1db31SMukul Joshi if (kfd_resume(node))
5428dc1db31SMukul Joshi goto kfd_resume_error;
5438dc1db31SMukul Joshi
5448dc1db31SMukul Joshi if (kfd_topology_add_device(node)) {
5458dc1db31SMukul Joshi dev_err(kfd_device, "Error adding device to topology\n");
5468dc1db31SMukul Joshi goto kfd_topology_add_device_error;
5478dc1db31SMukul Joshi }
5488dc1db31SMukul Joshi
5498dc1db31SMukul Joshi kfd_smi_init(node);
5508dc1db31SMukul Joshi
5518dc1db31SMukul Joshi return 0;
5528dc1db31SMukul Joshi
5538dc1db31SMukul Joshi kfd_topology_add_device_error:
5548dc1db31SMukul Joshi kfd_resume_error:
5558dc1db31SMukul Joshi gws_error:
5568dc1db31SMukul Joshi device_queue_manager_uninit(node->dqm);
5578dc1db31SMukul Joshi device_queue_manager_error:
5588dc1db31SMukul Joshi kfd_interrupt_exit(node);
5598dc1db31SMukul Joshi kfd_interrupt_error:
5608dc1db31SMukul Joshi if (node->gws)
5618dc1db31SMukul Joshi amdgpu_amdkfd_free_gws(node->adev, node->gws);
5628dc1db31SMukul Joshi
5638dc1db31SMukul Joshi /* Cleanup the node memory here */
5648dc1db31SMukul Joshi kfree(node);
5658dc1db31SMukul Joshi return err;
5668dc1db31SMukul Joshi }
5678dc1db31SMukul Joshi
kfd_cleanup_nodes(struct kfd_dev * kfd,unsigned int num_nodes)56874c5b85dSMukul Joshi static void kfd_cleanup_nodes(struct kfd_dev *kfd, unsigned int num_nodes)
5698dc1db31SMukul Joshi {
57074c5b85dSMukul Joshi struct kfd_node *knode;
57174c5b85dSMukul Joshi unsigned int i;
5728dc1db31SMukul Joshi
57374c5b85dSMukul Joshi for (i = 0; i < num_nodes; i++) {
57474c5b85dSMukul Joshi knode = kfd->nodes[i];
5758dc1db31SMukul Joshi device_queue_manager_uninit(knode->dqm);
5768dc1db31SMukul Joshi kfd_interrupt_exit(knode);
5778dc1db31SMukul Joshi kfd_topology_remove_device(knode);
5788dc1db31SMukul Joshi if (knode->gws)
5798dc1db31SMukul Joshi amdgpu_amdkfd_free_gws(knode->adev, knode->gws);
5808dc1db31SMukul Joshi kfree(knode);
58174c5b85dSMukul Joshi kfd->nodes[i] = NULL;
58274c5b85dSMukul Joshi }
5838dc1db31SMukul Joshi }
5848dc1db31SMukul Joshi
kfd_setup_interrupt_bitmap(struct kfd_node * node,unsigned int kfd_node_idx)585d4300362SMukul Joshi static void kfd_setup_interrupt_bitmap(struct kfd_node *node,
586d4300362SMukul Joshi unsigned int kfd_node_idx)
587d4300362SMukul Joshi {
588d4300362SMukul Joshi struct amdgpu_device *adev = node->adev;
589d4300362SMukul Joshi uint32_t xcc_mask = node->xcc_mask;
590d4300362SMukul Joshi uint32_t xcc, mapped_xcc;
591d4300362SMukul Joshi /*
592d4300362SMukul Joshi * Interrupt bitmap is setup for processing interrupts from
593d4300362SMukul Joshi * different XCDs and AIDs.
594d4300362SMukul Joshi * Interrupt bitmap is defined as follows:
595d4300362SMukul Joshi * 1. Bits 0-15 - correspond to the NodeId field.
596d4300362SMukul Joshi * Each bit corresponds to NodeId number. For example, if
597d4300362SMukul Joshi * a KFD node has interrupt bitmap set to 0x7, then this
598d4300362SMukul Joshi * KFD node will process interrupts with NodeId = 0, 1 and 2
599d4300362SMukul Joshi * in the IH cookie.
600d4300362SMukul Joshi * 2. Bits 16-31 - unused.
601d4300362SMukul Joshi *
602d4300362SMukul Joshi * Please note that the kfd_node_idx argument passed to this
603d4300362SMukul Joshi * function is not related to NodeId field received in the
604d4300362SMukul Joshi * IH cookie.
605d4300362SMukul Joshi *
606d4300362SMukul Joshi * In CPX mode, a KFD node will process an interrupt if:
607d4300362SMukul Joshi * - the Node Id matches the corresponding bit set in
608d4300362SMukul Joshi * Bits 0-15.
609d4300362SMukul Joshi * - AND VMID reported in the interrupt lies within the
610d4300362SMukul Joshi * VMID range of the node.
611d4300362SMukul Joshi */
612d4300362SMukul Joshi for_each_inst(xcc, xcc_mask) {
613d4300362SMukul Joshi mapped_xcc = GET_INST(GC, xcc);
614d4300362SMukul Joshi node->interrupt_bitmap |= (mapped_xcc % 2 ? 5 : 3) << (4 * (mapped_xcc / 2));
615d4300362SMukul Joshi }
616d4300362SMukul Joshi dev_info(kfd_device, "Node: %d, interrupt_bitmap: %x\n", kfd_node_idx,
617d4300362SMukul Joshi node->interrupt_bitmap);
618d4300362SMukul Joshi }
619d4300362SMukul Joshi
kgd2kfd_device_init(struct kfd_dev * kfd,const struct kgd2kfd_shared_resources * gpu_resources)6204a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
6214a488a7aSOded Gabbay const struct kgd2kfd_shared_resources *gpu_resources)
6224a488a7aSOded Gabbay {
62374c5b85dSMukul Joshi unsigned int size, map_process_packet_size, i;
6248dc1db31SMukul Joshi struct kfd_node *node;
6258dc1db31SMukul Joshi uint32_t first_vmid_kfd, last_vmid_kfd, vmid_num_kfd;
6268dc1db31SMukul Joshi unsigned int max_proc_per_quantum;
6272ad00e75SSrinivasan Shanmugam int partition_mode;
628a75f2271SLijo Lazar int xcp_idx;
62919f6d2a6SOded Gabbay
630574c4183SGraham Sider kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
6315ade6c9cSFelix Kuehling KGD_ENGINE_MEC1);
632574c4183SGraham Sider kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
63329633d0eSJoseph Greathouse KGD_ENGINE_MEC2);
634574c4183SGraham Sider kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
6355ade6c9cSFelix Kuehling KGD_ENGINE_SDMA1);
6364a488a7aSOded Gabbay kfd->shared_resources = *gpu_resources;
6374a488a7aSOded Gabbay
638a75f2271SLijo Lazar kfd->num_nodes = amdgpu_xcp_get_num_xcp(kfd->adev->xcp_mgr);
639a75f2271SLijo Lazar
64074c5b85dSMukul Joshi if (kfd->num_nodes == 0) {
64174c5b85dSMukul Joshi dev_err(kfd_device,
6422ad00e75SSrinivasan Shanmugam "KFD num nodes cannot be 0, num_xcc_in_node: %d\n",
6432ad00e75SSrinivasan Shanmugam kfd->adev->gfx.num_xcc_per_xcp);
64474c5b85dSMukul Joshi goto out;
64574c5b85dSMukul Joshi }
64644008d7aSYong Zhao
647e312af6cSFelix Kuehling /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
648e312af6cSFelix Kuehling * 32 and 64-bit requests are possible and must be
649e312af6cSFelix Kuehling * supported.
650e312af6cSFelix Kuehling */
6516bfc7c7eSGraham Sider kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->adev);
652e312af6cSFelix Kuehling if (!kfd->pci_atomic_requested &&
653f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics &&
654f0dc99a6SGraham Sider (!kfd->device_info.no_atomic_fw_version ||
655f0dc99a6SGraham Sider kfd->mec_fw_version < kfd->device_info.no_atomic_fw_version)) {
656e312af6cSFelix Kuehling dev_info(kfd_device,
657e312af6cSFelix Kuehling "skipped device %x:%x, PCI rejects atomics %d<%d\n",
658d69a3b76SMukul Joshi kfd->adev->pdev->vendor, kfd->adev->pdev->device,
659e312af6cSFelix Kuehling kfd->mec_fw_version,
660f0dc99a6SGraham Sider kfd->device_info.no_atomic_fw_version);
661e312af6cSFelix Kuehling return false;
662e312af6cSFelix Kuehling }
663e312af6cSFelix Kuehling
66474c5b85dSMukul Joshi first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
66574c5b85dSMukul Joshi last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
66674c5b85dSMukul Joshi vmid_num_kfd = last_vmid_kfd - first_vmid_kfd + 1;
66774c5b85dSMukul Joshi
66874c5b85dSMukul Joshi /* For GFX9.4.3, we need special handling for VMIDs depending on
66974c5b85dSMukul Joshi * partition mode.
67074c5b85dSMukul Joshi * In CPX mode, the VMID range needs to be shared between XCDs.
67174c5b85dSMukul Joshi * Additionally, there are 13 VMIDs (3-15) available for KFD. To
67274c5b85dSMukul Joshi * divide them equally, we change starting VMID to 4 and not use
67374c5b85dSMukul Joshi * VMID 3.
67474c5b85dSMukul Joshi * If the VMID range changes for GFX9.4.3, then this code MUST be
67574c5b85dSMukul Joshi * revisited.
67674c5b85dSMukul Joshi */
6770409022cSHawking Zhang if (kfd->adev->xcp_mgr) {
6780409022cSHawking Zhang partition_mode = amdgpu_xcp_query_partition_mode(kfd->adev->xcp_mgr,
6790409022cSHawking Zhang AMDGPU_XCP_FL_LOCKED);
6800409022cSHawking Zhang if (partition_mode == AMDGPU_CPX_PARTITION_MODE &&
68174c5b85dSMukul Joshi kfd->num_nodes != 1) {
68274c5b85dSMukul Joshi vmid_num_kfd /= 2;
68374c5b85dSMukul Joshi first_vmid_kfd = last_vmid_kfd + 1 - vmid_num_kfd*2;
68474c5b85dSMukul Joshi }
6850409022cSHawking Zhang }
68674c5b85dSMukul Joshi
687a99c6d4fSFelix Kuehling /* Verify module parameters regarding mapped process number*/
688b7dfbd2eSTushar Patel if (hws_max_conc_proc >= 0)
6898dc1db31SMukul Joshi max_proc_per_quantum = min((u32)hws_max_conc_proc, vmid_num_kfd);
690b7dfbd2eSTushar Patel else
6918dc1db31SMukul Joshi max_proc_per_quantum = vmid_num_kfd;
692a99c6d4fSFelix Kuehling
69319f6d2a6SOded Gabbay /* calculate max size of mqds needed for queues */
694b8cbab04SOded Gabbay size = max_num_of_queues_per_device *
695f0dc99a6SGraham Sider kfd->device_info.mqd_size_aligned;
69619f6d2a6SOded Gabbay
697e18e794eSOded Gabbay /*
698e18e794eSOded Gabbay * calculate max size of runlist packet.
699e18e794eSOded Gabbay * There can be only 2 packets at once
700e18e794eSOded Gabbay */
701046e674bSGraham Sider map_process_packet_size = KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) ?
702fd6a440eSJonathan Kim sizeof(struct pm4_mes_map_process_aldebaran) :
703fd6a440eSJonathan Kim sizeof(struct pm4_mes_map_process);
704fd6a440eSJonathan Kim size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +
705507968ddSFelix Kuehling max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
706507968ddSFelix Kuehling + sizeof(struct pm4_mes_runlist)) * 2;
707e18e794eSOded Gabbay
708e18e794eSOded Gabbay /* Add size of HIQ & DIQ */
709e18e794eSOded Gabbay size += KFD_KERNEL_QUEUE_SIZE * 2;
710e18e794eSOded Gabbay
711e18e794eSOded Gabbay /* add another 512KB for all other allocations on gart (HPD, fences) */
71219f6d2a6SOded Gabbay size += 512 * 1024;
71319f6d2a6SOded Gabbay
7147cd52c91SAmber Lin if (amdgpu_amdkfd_alloc_gtt_mem(
7156bfc7c7eSGraham Sider kfd->adev, size, &kfd->gtt_mem,
71615426dbbSYong Zhao &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
71715426dbbSYong Zhao false)) {
71879775b62SKent Russell dev_err(kfd_device, "Could not allocate %d bytes\n", size);
719e09d4fc8SOak Zeng goto alloc_gtt_mem_failure;
72019f6d2a6SOded Gabbay }
72119f6d2a6SOded Gabbay
72279775b62SKent Russell dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
723e18e794eSOded Gabbay
72473a1da0bSOded Gabbay /* Initialize GTT sa with 512 byte chunk size */
72573a1da0bSOded Gabbay if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
72679775b62SKent Russell dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
72773a1da0bSOded Gabbay goto kfd_gtt_sa_init_error;
72873a1da0bSOded Gabbay }
72973a1da0bSOded Gabbay
730735df2baSFelix Kuehling if (kfd_doorbell_init(kfd)) {
731735df2baSFelix Kuehling dev_err(kfd_device,
732735df2baSFelix Kuehling "Error initializing doorbell aperture\n");
733735df2baSFelix Kuehling goto kfd_doorbell_error;
734735df2baSFelix Kuehling }
73519f6d2a6SOded Gabbay
736c5650327SDivya Shikre if (amdgpu_use_xgmi_p2p)
73702274fc0SGraham Sider kfd->hive_id = kfd->adev->gmc.xgmi.hive_id;
7380c1690e3SShaoyun Liu
73992085240SJonathan Kim /*
74092085240SJonathan Kim * For GFX9.4.3, the KFD abstracts all partitions within a socket as
74192085240SJonathan Kim * xGMI connected in the topology so assign a unique hive id per
74292085240SJonathan Kim * device based on the pci device location if device is in PCIe mode.
74392085240SJonathan Kim */
74492085240SJonathan Kim if (!kfd->hive_id && (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 3)) && kfd->num_nodes > 1)
74592085240SJonathan Kim kfd->hive_id = pci_dev_id(kfd->adev->pdev);
74692085240SJonathan Kim
74702274fc0SGraham Sider kfd->noretry = kfd->adev->gmc.noretry;
7489b498efaSAlex Deucher
749373d7080SFelix Kuehling kfd_cwsr_init(kfd);
750373d7080SFelix Kuehling
75174c5b85dSMukul Joshi dev_info(kfd_device, "Total number of KFD nodes to be created: %d\n",
75274c5b85dSMukul Joshi kfd->num_nodes);
7538c45a834SPhilip Yang
7548c45a834SPhilip Yang /* Allocate the KFD nodes */
755a75f2271SLijo Lazar for (i = 0, xcp_idx = 0; i < kfd->num_nodes; i++) {
75674c5b85dSMukul Joshi node = kzalloc(sizeof(struct kfd_node), GFP_KERNEL);
75774c5b85dSMukul Joshi if (!node)
75874c5b85dSMukul Joshi goto node_alloc_error;
75974c5b85dSMukul Joshi
760a805889aSMukul Joshi node->node_id = i;
76174c5b85dSMukul Joshi node->adev = kfd->adev;
76274c5b85dSMukul Joshi node->kfd = kfd;
76374c5b85dSMukul Joshi node->kfd2kgd = kfd->kfd2kgd;
76474c5b85dSMukul Joshi node->vm_info.vmid_num_kfd = vmid_num_kfd;
765a75f2271SLijo Lazar node->xcp = amdgpu_get_next_xcp(kfd->adev->xcp_mgr, &xcp_idx);
766a75f2271SLijo Lazar /* TODO : Check if error handling is needed */
767c4050ff1SLijo Lazar if (node->xcp) {
768a75f2271SLijo Lazar amdgpu_xcp_get_inst_details(node->xcp, AMDGPU_XCP_GFX,
769a75f2271SLijo Lazar &node->xcc_mask);
770c4050ff1SLijo Lazar ++xcp_idx;
771c4050ff1SLijo Lazar } else {
772a75f2271SLijo Lazar node->xcc_mask =
773a75f2271SLijo Lazar (1U << NUM_XCC(kfd->adev->gfx.xcc_mask)) - 1;
774c4050ff1SLijo Lazar }
77574c5b85dSMukul Joshi
7764c6ce75fSPhilip Yang if (node->xcp) {
7774c6ce75fSPhilip Yang dev_info(kfd_device, "KFD node %d partition %d size %lldM\n",
7784c6ce75fSPhilip Yang node->node_id, node->xcp->mem_id,
779315e29ecSMukul Joshi KFD_XCP_MEMORY_SIZE(node->adev, node->node_id) >> 20);
7804c6ce75fSPhilip Yang }
7814c6ce75fSPhilip Yang
78274c5b85dSMukul Joshi if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 3) &&
7838e7fd193SLijo Lazar partition_mode == AMDGPU_CPX_PARTITION_MODE &&
78474c5b85dSMukul Joshi kfd->num_nodes != 1) {
78574c5b85dSMukul Joshi /* For GFX9.4.3 and CPX mode, first XCD gets VMID range
78674c5b85dSMukul Joshi * 4-9 and second XCD gets VMID range 10-15.
78774c5b85dSMukul Joshi */
78874c5b85dSMukul Joshi
78974c5b85dSMukul Joshi node->vm_info.first_vmid_kfd = (i%2 == 0) ?
79074c5b85dSMukul Joshi first_vmid_kfd :
79174c5b85dSMukul Joshi first_vmid_kfd+vmid_num_kfd;
79274c5b85dSMukul Joshi node->vm_info.last_vmid_kfd = (i%2 == 0) ?
79374c5b85dSMukul Joshi last_vmid_kfd-vmid_num_kfd :
79474c5b85dSMukul Joshi last_vmid_kfd;
79574c5b85dSMukul Joshi node->compute_vmid_bitmap =
79674c5b85dSMukul Joshi ((0x1 << (node->vm_info.last_vmid_kfd + 1)) - 1) -
79774c5b85dSMukul Joshi ((0x1 << (node->vm_info.first_vmid_kfd)) - 1);
79874c5b85dSMukul Joshi } else {
79974c5b85dSMukul Joshi node->vm_info.first_vmid_kfd = first_vmid_kfd;
80074c5b85dSMukul Joshi node->vm_info.last_vmid_kfd = last_vmid_kfd;
80174c5b85dSMukul Joshi node->compute_vmid_bitmap =
80274c5b85dSMukul Joshi gpu_resources->compute_vmid_bitmap;
80374c5b85dSMukul Joshi }
80474c5b85dSMukul Joshi node->max_proc_per_quantum = max_proc_per_quantum;
80574c5b85dSMukul Joshi atomic_set(&node->sram_ecc_flag, 0);
806315e29ecSMukul Joshi
807315e29ecSMukul Joshi amdgpu_amdkfd_get_local_mem_info(kfd->adev,
8089a3ce1a7SHawking Zhang &node->local_mem_info, node->xcp);
809315e29ecSMukul Joshi
810d4300362SMukul Joshi if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 3))
811d4300362SMukul Joshi kfd_setup_interrupt_bitmap(node, i);
812d4300362SMukul Joshi
8138dc1db31SMukul Joshi /* Initialize the KFD node */
8148dc1db31SMukul Joshi if (kfd_init_node(node)) {
8158dc1db31SMukul Joshi dev_err(kfd_device, "Error initializing KFD node\n");
8168dc1db31SMukul Joshi goto node_init_error;
8178dc1db31SMukul Joshi }
81874c5b85dSMukul Joshi kfd->nodes[i] = node;
81974c5b85dSMukul Joshi }
8208dc1db31SMukul Joshi
82184b4dd3fSPhilip Yang svm_range_set_max_pages(kfd->adev);
82225f50704SPhilip Yang
823e0f85f46SJonathan Kim spin_lock_init(&kfd->watch_points_lock);
824e0f85f46SJonathan Kim
8254a488a7aSOded Gabbay kfd->init_complete = true;
826d69a3b76SMukul Joshi dev_info(kfd_device, "added device %x:%x\n", kfd->adev->pdev->vendor,
827d69a3b76SMukul Joshi kfd->adev->pdev->device);
8284a488a7aSOded Gabbay
82979775b62SKent Russell pr_debug("Starting kfd with the following scheduling policy %d\n",
8308dc1db31SMukul Joshi node->dqm->sched_policy);
83164c7f8cfSBen Goz
83219f6d2a6SOded Gabbay goto out;
83319f6d2a6SOded Gabbay
8348dc1db31SMukul Joshi node_init_error:
8358dc1db31SMukul Joshi node_alloc_error:
83674c5b85dSMukul Joshi kfd_cleanup_nodes(kfd, i);
837735df2baSFelix Kuehling kfd_doorbell_fini(kfd);
838735df2baSFelix Kuehling kfd_doorbell_error:
83973a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd);
84073a1da0bSOded Gabbay kfd_gtt_sa_init_error:
841*30ceb873SPhilip Yang amdgpu_amdkfd_free_gtt_mem(kfd->adev, &kfd->gtt_mem);
842e09d4fc8SOak Zeng alloc_gtt_mem_failure:
84319f6d2a6SOded Gabbay dev_err(kfd_device,
84479775b62SKent Russell "device %x:%x NOT added due to errors\n",
845d69a3b76SMukul Joshi kfd->adev->pdev->vendor, kfd->adev->pdev->device);
84619f6d2a6SOded Gabbay out:
84719f6d2a6SOded Gabbay return kfd->init_complete;
8484a488a7aSOded Gabbay }
8494a488a7aSOded Gabbay
kgd2kfd_device_exit(struct kfd_dev * kfd)8504a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
8514a488a7aSOded Gabbay {
852b17f068aSOded Gabbay if (kfd->init_complete) {
85374c5b85dSMukul Joshi /* Cleanup KFD nodes */
85474c5b85dSMukul Joshi kfd_cleanup_nodes(kfd, kfd->num_nodes);
85574c5b85dSMukul Joshi /* Cleanup common/shared resources */
856735df2baSFelix Kuehling kfd_doorbell_fini(kfd);
85759d7115dSMukul Joshi ida_destroy(&kfd->doorbell_ida);
85873a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd);
859*30ceb873SPhilip Yang amdgpu_amdkfd_free_gtt_mem(kfd->adev, &kfd->gtt_mem);
860b17f068aSOded Gabbay }
8615b5c4e40SEvgeny Pinchuk
8624a488a7aSOded Gabbay kfree(kfd);
8634a488a7aSOded Gabbay }
8644a488a7aSOded Gabbay
kgd2kfd_pre_reset(struct kfd_dev * kfd)865e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
866e3b7a967SShaoyun Liu {
86774c5b85dSMukul Joshi struct kfd_node *node;
86874c5b85dSMukul Joshi int i;
8698dc1db31SMukul Joshi
870e42051d2SShaoyun Liu if (!kfd->init_complete)
871e42051d2SShaoyun Liu return 0;
87209c34e8dSFelix Kuehling
87374c5b85dSMukul Joshi for (i = 0; i < kfd->num_nodes; i++) {
87474c5b85dSMukul Joshi node = kfd->nodes[i];
8758dc1db31SMukul Joshi kfd_smi_event_update_gpu_reset(node, false);
8768dc1db31SMukul Joshi node->dqm->ops.pre_reset(node->dqm);
87774c5b85dSMukul Joshi }
87809c34e8dSFelix Kuehling
8799593f4d6SRajneesh Bhardwaj kgd2kfd_suspend(kfd, false);
880e42051d2SShaoyun Liu
88174c5b85dSMukul Joshi for (i = 0; i < kfd->num_nodes; i++)
88274c5b85dSMukul Joshi kfd_signal_reset_event(kfd->nodes[i]);
88374c5b85dSMukul Joshi
884e3b7a967SShaoyun Liu return 0;
885e3b7a967SShaoyun Liu }
886e3b7a967SShaoyun Liu
887e42051d2SShaoyun Liu /*
888e42051d2SShaoyun Liu * Fix me. KFD won't be able to resume existing process for now.
889e42051d2SShaoyun Liu * We will keep all existing process in a evicted state and
890e42051d2SShaoyun Liu * wait the process to be terminated.
891e42051d2SShaoyun Liu */
892e42051d2SShaoyun Liu
kgd2kfd_post_reset(struct kfd_dev * kfd)893e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
894e3b7a967SShaoyun Liu {
895a1bd079fSyu kuai int ret;
89674c5b85dSMukul Joshi struct kfd_node *node;
89774c5b85dSMukul Joshi int i;
898e42051d2SShaoyun Liu
899e42051d2SShaoyun Liu if (!kfd->init_complete)
900e3b7a967SShaoyun Liu return 0;
901e42051d2SShaoyun Liu
90274c5b85dSMukul Joshi for (i = 0; i < kfd->num_nodes; i++) {
90374c5b85dSMukul Joshi ret = kfd_resume(kfd->nodes[i]);
904e42051d2SShaoyun Liu if (ret)
905e42051d2SShaoyun Liu return ret;
90674c5b85dSMukul Joshi }
90774c5b85dSMukul Joshi
908fe1f05dfSMukul Joshi mutex_lock(&kfd_processes_mutex);
909fe1f05dfSMukul Joshi --kfd_locked;
910fe1f05dfSMukul Joshi mutex_unlock(&kfd_processes_mutex);
9119b54d201SEric Huang
91274c5b85dSMukul Joshi for (i = 0; i < kfd->num_nodes; i++) {
91374c5b85dSMukul Joshi node = kfd->nodes[i];
9148dc1db31SMukul Joshi atomic_set(&node->sram_ecc_flag, 0);
9158dc1db31SMukul Joshi kfd_smi_event_update_gpu_reset(node, true);
91674c5b85dSMukul Joshi }
91755977744SMukul Joshi
918e42051d2SShaoyun Liu return 0;
919e42051d2SShaoyun Liu }
920e42051d2SShaoyun Liu
kfd_is_locked(void)921e42051d2SShaoyun Liu bool kfd_is_locked(void)
922e42051d2SShaoyun Liu {
923fe1f05dfSMukul Joshi lockdep_assert_held(&kfd_processes_mutex);
924fe1f05dfSMukul Joshi return (kfd_locked > 0);
925e3b7a967SShaoyun Liu }
926e3b7a967SShaoyun Liu
kgd2kfd_suspend(struct kfd_dev * kfd,bool run_pm)9279593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
9284a488a7aSOded Gabbay {
92974c5b85dSMukul Joshi struct kfd_node *node;
93074c5b85dSMukul Joshi int i;
9318dc1db31SMukul Joshi
932733fa1f7SYong Zhao if (!kfd->init_complete)
933733fa1f7SYong Zhao return;
934733fa1f7SYong Zhao
9359593f4d6SRajneesh Bhardwaj /* for runtime suspend, skip locking kfd */
9369593f4d6SRajneesh Bhardwaj if (!run_pm) {
937fe1f05dfSMukul Joshi mutex_lock(&kfd_processes_mutex);
93826103436SFelix Kuehling /* For first KFD device suspend all the KFD processes */
939b6f66265SZhigang Luo if (++kfd_locked == 1)
94026103436SFelix Kuehling kfd_suspend_all_processes();
941b6f66265SZhigang Luo mutex_unlock(&kfd_processes_mutex);
9429593f4d6SRajneesh Bhardwaj }
94326103436SFelix Kuehling
94474c5b85dSMukul Joshi for (i = 0; i < kfd->num_nodes; i++) {
94574c5b85dSMukul Joshi node = kfd->nodes[i];
9468dc1db31SMukul Joshi node->dqm->ops.stop(node->dqm);
94774c5b85dSMukul Joshi }
9484a488a7aSOded Gabbay }
9494a488a7aSOded Gabbay
kgd2kfd_resume(struct kfd_dev * kfd,bool run_pm)9509593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
9514a488a7aSOded Gabbay {
952b6f66265SZhigang Luo int ret, i;
95326103436SFelix Kuehling
954b8935a7cSYong Zhao if (!kfd->init_complete)
955b8935a7cSYong Zhao return 0;
956b17f068aSOded Gabbay
95774c5b85dSMukul Joshi for (i = 0; i < kfd->num_nodes; i++) {
95874c5b85dSMukul Joshi ret = kfd_resume(kfd->nodes[i]);
95926103436SFelix Kuehling if (ret)
96026103436SFelix Kuehling return ret;
96174c5b85dSMukul Joshi }
962b17f068aSOded Gabbay
9639593f4d6SRajneesh Bhardwaj /* for runtime resume, skip unlocking kfd */
9649593f4d6SRajneesh Bhardwaj if (!run_pm) {
965fe1f05dfSMukul Joshi mutex_lock(&kfd_processes_mutex);
966b6f66265SZhigang Luo if (--kfd_locked == 0)
96726103436SFelix Kuehling ret = kfd_resume_all_processes();
968b6f66265SZhigang Luo WARN_ONCE(kfd_locked < 0, "KFD suspend / resume ref. error");
969b6f66265SZhigang Luo mutex_unlock(&kfd_processes_mutex);
9709593f4d6SRajneesh Bhardwaj }
97126103436SFelix Kuehling
97226103436SFelix Kuehling return ret;
9734ebc7182SYong Zhao }
9744ebc7182SYong Zhao
kfd_resume(struct kfd_node * node)9758dc1db31SMukul Joshi static int kfd_resume(struct kfd_node *node)
976f8846323SJames Zhu {
977f8846323SJames Zhu int err = 0;
978f8846323SJames Zhu
9798dc1db31SMukul Joshi err = node->dqm->ops.start(node->dqm);
980499f4d38SYifan Zhang if (err)
981b8935a7cSYong Zhao dev_err(kfd_device,
982b8935a7cSYong Zhao "Error starting queue manager for device %x:%x\n",
9838dc1db31SMukul Joshi node->adev->pdev->vendor, node->adev->pdev->device);
984b17f068aSOded Gabbay
985b8935a7cSYong Zhao return err;
9864a488a7aSOded Gabbay }
9874a488a7aSOded Gabbay
kfd_queue_work(struct workqueue_struct * wq,struct work_struct * work)988b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq,
989b3eca59dSPhilip Yang struct work_struct *work)
990b3eca59dSPhilip Yang {
991b3eca59dSPhilip Yang int cpu, new_cpu;
992b3eca59dSPhilip Yang
993b3eca59dSPhilip Yang cpu = new_cpu = smp_processor_id();
994b3eca59dSPhilip Yang do {
995b3eca59dSPhilip Yang new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
996b3eca59dSPhilip Yang if (cpu_to_node(new_cpu) == numa_node_id())
997b3eca59dSPhilip Yang break;
998b3eca59dSPhilip Yang } while (cpu != new_cpu);
999b3eca59dSPhilip Yang
1000b3eca59dSPhilip Yang queue_work_on(new_cpu, wq, work);
1001b3eca59dSPhilip Yang }
1002b3eca59dSPhilip Yang
1003b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
kgd2kfd_interrupt(struct kfd_dev * kfd,const void * ih_ring_entry)1004b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
10054a488a7aSOded Gabbay {
100674c5b85dSMukul Joshi uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE], i;
100758e69886SLan Xiao bool is_patched = false;
10082383a767SChristian König unsigned long flags;
100974c5b85dSMukul Joshi struct kfd_node *node;
101058e69886SLan Xiao
10112249d558SAndrew Lewycky if (!kfd->init_complete)
10122249d558SAndrew Lewycky return;
10132249d558SAndrew Lewycky
1014f0dc99a6SGraham Sider if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) {
101558e69886SLan Xiao dev_err_once(kfd_device, "Ring entry too small\n");
101658e69886SLan Xiao return;
101758e69886SLan Xiao }
101858e69886SLan Xiao
101974c5b85dSMukul Joshi for (i = 0; i < kfd->num_nodes; i++) {
102074c5b85dSMukul Joshi node = kfd->nodes[i];
10218dc1db31SMukul Joshi spin_lock_irqsave(&node->interrupt_lock, flags);
10222249d558SAndrew Lewycky
10238dc1db31SMukul Joshi if (node->interrupts_active
10248dc1db31SMukul Joshi && interrupt_is_wanted(node, ih_ring_entry,
102558e69886SLan Xiao patched_ihre, &is_patched)
10268dc1db31SMukul Joshi && enqueue_ih_ring_entry(node,
102774c5b85dSMukul Joshi is_patched ? patched_ihre : ih_ring_entry)) {
10288dc1db31SMukul Joshi kfd_queue_work(node->ih_wq, &node->interrupt_work);
10298dc1db31SMukul Joshi spin_unlock_irqrestore(&node->interrupt_lock, flags);
103074c5b85dSMukul Joshi return;
103174c5b85dSMukul Joshi }
103274c5b85dSMukul Joshi spin_unlock_irqrestore(&node->interrupt_lock, flags);
103374c5b85dSMukul Joshi }
103474c5b85dSMukul Joshi
10354a488a7aSOded Gabbay }
10366e81090bSOded Gabbay
kgd2kfd_quiesce_mm(struct mm_struct * mm,uint32_t trigger)1037c7f21978SPhilip Yang int kgd2kfd_quiesce_mm(struct mm_struct *mm, uint32_t trigger)
10386b95e797SFelix Kuehling {
10396b95e797SFelix Kuehling struct kfd_process *p;
10406b95e797SFelix Kuehling int r;
10416b95e797SFelix Kuehling
10426b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed
10436b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are
10446b95e797SFelix Kuehling * running so the lookup function increments the process ref count.
10456b95e797SFelix Kuehling */
10466b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm);
10476b95e797SFelix Kuehling if (!p)
10486b95e797SFelix Kuehling return -ESRCH;
10496b95e797SFelix Kuehling
1050b2057956SFelix Kuehling WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
1051c7f21978SPhilip Yang r = kfd_process_evict_queues(p, trigger);
10526b95e797SFelix Kuehling
10536b95e797SFelix Kuehling kfd_unref_process(p);
10546b95e797SFelix Kuehling return r;
10556b95e797SFelix Kuehling }
10566b95e797SFelix Kuehling
kgd2kfd_resume_mm(struct mm_struct * mm)10576b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
10586b95e797SFelix Kuehling {
10596b95e797SFelix Kuehling struct kfd_process *p;
10606b95e797SFelix Kuehling int r;
10616b95e797SFelix Kuehling
10626b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed
10636b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are
10646b95e797SFelix Kuehling * running so the lookup function increments the process ref count.
10656b95e797SFelix Kuehling */
10666b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm);
10676b95e797SFelix Kuehling if (!p)
10686b95e797SFelix Kuehling return -ESRCH;
10696b95e797SFelix Kuehling
10706b95e797SFelix Kuehling r = kfd_process_restore_queues(p);
10716b95e797SFelix Kuehling
10726b95e797SFelix Kuehling kfd_unref_process(p);
10736b95e797SFelix Kuehling return r;
10746b95e797SFelix Kuehling }
10756b95e797SFelix Kuehling
107626103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
107726103436SFelix Kuehling * prepare for safe eviction of KFD BOs that belong to the specified
107826103436SFelix Kuehling * process.
107926103436SFelix Kuehling *
108026103436SFelix Kuehling * @mm: mm_struct that identifies the specified KFD process
108126103436SFelix Kuehling * @fence: eviction fence attached to KFD process BOs
108226103436SFelix Kuehling *
108326103436SFelix Kuehling */
kgd2kfd_schedule_evict_and_restore_process(struct mm_struct * mm,struct dma_fence * fence)108426103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
108526103436SFelix Kuehling struct dma_fence *fence)
108626103436SFelix Kuehling {
108726103436SFelix Kuehling struct kfd_process *p;
108826103436SFelix Kuehling unsigned long active_time;
108926103436SFelix Kuehling unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
109026103436SFelix Kuehling
109126103436SFelix Kuehling if (!fence)
109226103436SFelix Kuehling return -EINVAL;
109326103436SFelix Kuehling
109426103436SFelix Kuehling if (dma_fence_is_signaled(fence))
109526103436SFelix Kuehling return 0;
109626103436SFelix Kuehling
109726103436SFelix Kuehling p = kfd_lookup_process_by_mm(mm);
109826103436SFelix Kuehling if (!p)
109926103436SFelix Kuehling return -ENODEV;
110026103436SFelix Kuehling
110126103436SFelix Kuehling if (fence->seqno == p->last_eviction_seqno)
110226103436SFelix Kuehling goto out;
110326103436SFelix Kuehling
110426103436SFelix Kuehling p->last_eviction_seqno = fence->seqno;
110526103436SFelix Kuehling
110626103436SFelix Kuehling /* Avoid KFD process starvation. Wait for at least
110726103436SFelix Kuehling * PROCESS_ACTIVE_TIME_MS before evicting the process again
110826103436SFelix Kuehling */
110926103436SFelix Kuehling active_time = get_jiffies_64() - p->last_restore_timestamp;
111026103436SFelix Kuehling if (delay_jiffies > active_time)
111126103436SFelix Kuehling delay_jiffies -= active_time;
111226103436SFelix Kuehling else
111326103436SFelix Kuehling delay_jiffies = 0;
111426103436SFelix Kuehling
111526103436SFelix Kuehling /* During process initialization eviction_work.dwork is initialized
111626103436SFelix Kuehling * to kfd_evict_bo_worker
111726103436SFelix Kuehling */
1118b2057956SFelix Kuehling WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
1119b2057956SFelix Kuehling p->lead_thread->pid, delay_jiffies);
112026103436SFelix Kuehling schedule_delayed_work(&p->eviction_work, delay_jiffies);
112126103436SFelix Kuehling out:
112226103436SFelix Kuehling kfd_unref_process(p);
112326103436SFelix Kuehling return 0;
112426103436SFelix Kuehling }
112526103436SFelix Kuehling
kfd_gtt_sa_init(struct kfd_dev * kfd,unsigned int buf_size,unsigned int chunk_size)11266e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
11276e81090bSOded Gabbay unsigned int chunk_size)
11286e81090bSOded Gabbay {
112932fa8219SFelix Kuehling if (WARN_ON(buf_size < chunk_size))
113032fa8219SFelix Kuehling return -EINVAL;
113132fa8219SFelix Kuehling if (WARN_ON(buf_size == 0))
113232fa8219SFelix Kuehling return -EINVAL;
113332fa8219SFelix Kuehling if (WARN_ON(chunk_size == 0))
113432fa8219SFelix Kuehling return -EINVAL;
11356e81090bSOded Gabbay
11366e81090bSOded Gabbay kfd->gtt_sa_chunk_size = chunk_size;
11376e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
11386e81090bSOded Gabbay
1139f43a9f18SChristophe JAILLET kfd->gtt_sa_bitmap = bitmap_zalloc(kfd->gtt_sa_num_of_chunks,
1140f43a9f18SChristophe JAILLET GFP_KERNEL);
11416e81090bSOded Gabbay if (!kfd->gtt_sa_bitmap)
11426e81090bSOded Gabbay return -ENOMEM;
11436e81090bSOded Gabbay
114479775b62SKent Russell pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
11456e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
11466e81090bSOded Gabbay
11476e81090bSOded Gabbay mutex_init(&kfd->gtt_sa_lock);
11486e81090bSOded Gabbay
11496e81090bSOded Gabbay return 0;
11506e81090bSOded Gabbay }
11516e81090bSOded Gabbay
kfd_gtt_sa_fini(struct kfd_dev * kfd)11526e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
11536e81090bSOded Gabbay {
11546e81090bSOded Gabbay mutex_destroy(&kfd->gtt_sa_lock);
1155f43a9f18SChristophe JAILLET bitmap_free(kfd->gtt_sa_bitmap);
11566e81090bSOded Gabbay }
11576e81090bSOded Gabbay
kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,unsigned int bit_num,unsigned int chunk_size)11586e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
11596e81090bSOded Gabbay unsigned int bit_num,
11606e81090bSOded Gabbay unsigned int chunk_size)
11616e81090bSOded Gabbay {
11626e81090bSOded Gabbay return start_addr + bit_num * chunk_size;
11636e81090bSOded Gabbay }
11646e81090bSOded Gabbay
kfd_gtt_sa_calc_cpu_addr(void * start_addr,unsigned int bit_num,unsigned int chunk_size)11656e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
11666e81090bSOded Gabbay unsigned int bit_num,
11676e81090bSOded Gabbay unsigned int chunk_size)
11686e81090bSOded Gabbay {
11696e81090bSOded Gabbay return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
11706e81090bSOded Gabbay }
11716e81090bSOded Gabbay
kfd_gtt_sa_allocate(struct kfd_node * node,unsigned int size,struct kfd_mem_obj ** mem_obj)11728dc1db31SMukul Joshi int kfd_gtt_sa_allocate(struct kfd_node *node, unsigned int size,
11736e81090bSOded Gabbay struct kfd_mem_obj **mem_obj)
11746e81090bSOded Gabbay {
11756e81090bSOded Gabbay unsigned int found, start_search, cur_size;
11768dc1db31SMukul Joshi struct kfd_dev *kfd = node->kfd;
11776e81090bSOded Gabbay
11786e81090bSOded Gabbay if (size == 0)
11796e81090bSOded Gabbay return -EINVAL;
11806e81090bSOded Gabbay
11816e81090bSOded Gabbay if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
11826e81090bSOded Gabbay return -ENOMEM;
11836e81090bSOded Gabbay
11841cd106ecSFelix Kuehling *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
11851cd106ecSFelix Kuehling if (!(*mem_obj))
11866e81090bSOded Gabbay return -ENOMEM;
11876e81090bSOded Gabbay
118879775b62SKent Russell pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
11896e81090bSOded Gabbay
11906e81090bSOded Gabbay start_search = 0;
11916e81090bSOded Gabbay
11926e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock);
11936e81090bSOded Gabbay
11946e81090bSOded Gabbay kfd_gtt_restart_search:
11956e81090bSOded Gabbay /* Find the first chunk that is free */
11966e81090bSOded Gabbay found = find_next_zero_bit(kfd->gtt_sa_bitmap,
11976e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks,
11986e81090bSOded Gabbay start_search);
11996e81090bSOded Gabbay
120079775b62SKent Russell pr_debug("Found = %d\n", found);
12016e81090bSOded Gabbay
12026e81090bSOded Gabbay /* If there wasn't any free chunk, bail out */
12036e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks)
12046e81090bSOded Gabbay goto kfd_gtt_no_free_chunk;
12056e81090bSOded Gabbay
12066e81090bSOded Gabbay /* Update fields of mem_obj */
12076e81090bSOded Gabbay (*mem_obj)->range_start = found;
12086e81090bSOded Gabbay (*mem_obj)->range_end = found;
12096e81090bSOded Gabbay (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
12106e81090bSOded Gabbay kfd->gtt_start_gpu_addr,
12116e81090bSOded Gabbay found,
12126e81090bSOded Gabbay kfd->gtt_sa_chunk_size);
12136e81090bSOded Gabbay (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
12146e81090bSOded Gabbay kfd->gtt_start_cpu_ptr,
12156e81090bSOded Gabbay found,
12166e81090bSOded Gabbay kfd->gtt_sa_chunk_size);
12176e81090bSOded Gabbay
121879775b62SKent Russell pr_debug("gpu_addr = %p, cpu_addr = %p\n",
12196e81090bSOded Gabbay (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
12206e81090bSOded Gabbay
12216e81090bSOded Gabbay /* If we need only one chunk, mark it as allocated and get out */
12226e81090bSOded Gabbay if (size <= kfd->gtt_sa_chunk_size) {
122379775b62SKent Russell pr_debug("Single bit\n");
1224b8b9ba58SChristophe JAILLET __set_bit(found, kfd->gtt_sa_bitmap);
12256e81090bSOded Gabbay goto kfd_gtt_out;
12266e81090bSOded Gabbay }
12276e81090bSOded Gabbay
12286e81090bSOded Gabbay /* Otherwise, try to see if we have enough contiguous chunks */
12296e81090bSOded Gabbay cur_size = size - kfd->gtt_sa_chunk_size;
12306e81090bSOded Gabbay do {
12316e81090bSOded Gabbay (*mem_obj)->range_end =
12326e81090bSOded Gabbay find_next_zero_bit(kfd->gtt_sa_bitmap,
12336e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, ++found);
12346e81090bSOded Gabbay /*
12356e81090bSOded Gabbay * If next free chunk is not contiguous than we need to
12366e81090bSOded Gabbay * restart our search from the last free chunk we found (which
12376e81090bSOded Gabbay * wasn't contiguous to the previous ones
12386e81090bSOded Gabbay */
12396e81090bSOded Gabbay if ((*mem_obj)->range_end != found) {
12406e81090bSOded Gabbay start_search = found;
12416e81090bSOded Gabbay goto kfd_gtt_restart_search;
12426e81090bSOded Gabbay }
12436e81090bSOded Gabbay
12446e81090bSOded Gabbay /*
12456e81090bSOded Gabbay * If we reached end of buffer, bail out with error
12466e81090bSOded Gabbay */
12476e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks)
12486e81090bSOded Gabbay goto kfd_gtt_no_free_chunk;
12496e81090bSOded Gabbay
12506e81090bSOded Gabbay /* Check if we don't need another chunk */
12516e81090bSOded Gabbay if (cur_size <= kfd->gtt_sa_chunk_size)
12526e81090bSOded Gabbay cur_size = 0;
12536e81090bSOded Gabbay else
12546e81090bSOded Gabbay cur_size -= kfd->gtt_sa_chunk_size;
12556e81090bSOded Gabbay
12566e81090bSOded Gabbay } while (cur_size > 0);
12576e81090bSOded Gabbay
125879775b62SKent Russell pr_debug("range_start = %d, range_end = %d\n",
12596e81090bSOded Gabbay (*mem_obj)->range_start, (*mem_obj)->range_end);
12606e81090bSOded Gabbay
12616e81090bSOded Gabbay /* Mark the chunks as allocated */
1262b8b9ba58SChristophe JAILLET bitmap_set(kfd->gtt_sa_bitmap, (*mem_obj)->range_start,
1263b8b9ba58SChristophe JAILLET (*mem_obj)->range_end - (*mem_obj)->range_start + 1);
12646e81090bSOded Gabbay
12656e81090bSOded Gabbay kfd_gtt_out:
12666e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock);
12676e81090bSOded Gabbay return 0;
12686e81090bSOded Gabbay
12696e81090bSOded Gabbay kfd_gtt_no_free_chunk:
12703148a6a0SJack Zhang pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
12716e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock);
12723148a6a0SJack Zhang kfree(*mem_obj);
12736e81090bSOded Gabbay return -ENOMEM;
12746e81090bSOded Gabbay }
12756e81090bSOded Gabbay
kfd_gtt_sa_free(struct kfd_node * node,struct kfd_mem_obj * mem_obj)12768dc1db31SMukul Joshi int kfd_gtt_sa_free(struct kfd_node *node, struct kfd_mem_obj *mem_obj)
12776e81090bSOded Gabbay {
12788dc1db31SMukul Joshi struct kfd_dev *kfd = node->kfd;
12798dc1db31SMukul Joshi
12809216ed29SOded Gabbay /* Act like kfree when trying to free a NULL object */
12819216ed29SOded Gabbay if (!mem_obj)
12829216ed29SOded Gabbay return 0;
12836e81090bSOded Gabbay
128479775b62SKent Russell pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
12856e81090bSOded Gabbay mem_obj, mem_obj->range_start, mem_obj->range_end);
12866e81090bSOded Gabbay
12876e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock);
12886e81090bSOded Gabbay
12896e81090bSOded Gabbay /* Mark the chunks as free */
1290b8b9ba58SChristophe JAILLET bitmap_clear(kfd->gtt_sa_bitmap, mem_obj->range_start,
1291b8b9ba58SChristophe JAILLET mem_obj->range_end - mem_obj->range_start + 1);
12926e81090bSOded Gabbay
12936e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock);
12946e81090bSOded Gabbay
12956e81090bSOded Gabbay kfree(mem_obj);
12966e81090bSOded Gabbay return 0;
12976e81090bSOded Gabbay }
1298a29ec470SShaoyun Liu
kgd2kfd_set_sram_ecc_flag(struct kfd_dev * kfd)12999b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
13009b54d201SEric Huang {
130174c5b85dSMukul Joshi /*
130274c5b85dSMukul Joshi * TODO: Currently update SRAM ECC flag for first node.
130374c5b85dSMukul Joshi * This needs to be updated later when we can
130474c5b85dSMukul Joshi * identify SRAM ECC error on other nodes also.
130574c5b85dSMukul Joshi */
13069b54d201SEric Huang if (kfd)
130774c5b85dSMukul Joshi atomic_inc(&kfd->nodes[0]->sram_ecc_flag);
13089b54d201SEric Huang }
13099b54d201SEric Huang
kfd_inc_compute_active(struct kfd_node * node)13108dc1db31SMukul Joshi void kfd_inc_compute_active(struct kfd_node *node)
131143d8107fSHarish Kasiviswanathan {
13128dc1db31SMukul Joshi if (atomic_inc_return(&node->kfd->compute_profile) == 1)
13138dc1db31SMukul Joshi amdgpu_amdkfd_set_compute_idle(node->adev, false);
131443d8107fSHarish Kasiviswanathan }
131543d8107fSHarish Kasiviswanathan
kfd_dec_compute_active(struct kfd_node * node)13168dc1db31SMukul Joshi void kfd_dec_compute_active(struct kfd_node *node)
131743d8107fSHarish Kasiviswanathan {
13188dc1db31SMukul Joshi int count = atomic_dec_return(&node->kfd->compute_profile);
131943d8107fSHarish Kasiviswanathan
132043d8107fSHarish Kasiviswanathan if (count == 0)
13218dc1db31SMukul Joshi amdgpu_amdkfd_set_compute_idle(node->adev, true);
132243d8107fSHarish Kasiviswanathan WARN_ONCE(count < 0, "Compute profile ref. count error");
132343d8107fSHarish Kasiviswanathan }
132443d8107fSHarish Kasiviswanathan
kgd2kfd_smi_event_throttle(struct kfd_dev * kfd,uint64_t throttle_bitmask)1325410e302eSGraham Sider void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)
13262c2b0d88SMukul Joshi {
132774c5b85dSMukul Joshi /*
132874c5b85dSMukul Joshi * TODO: For now, raise the throttling event only on first node.
132974c5b85dSMukul Joshi * This will need to change after we are able to determine
133074c5b85dSMukul Joshi * which node raised the throttling event.
133174c5b85dSMukul Joshi */
1332158fc08dSAmber Lin if (kfd && kfd->init_complete)
133374c5b85dSMukul Joshi kfd_smi_event_update_thermal_throttling(kfd->nodes[0],
133474c5b85dSMukul Joshi throttle_bitmask);
13352c2b0d88SMukul Joshi }
13362c2b0d88SMukul Joshi
1337ee2f17f4SAmber Lin /* kfd_get_num_sdma_engines returns the number of PCIe optimized SDMA and
1338ee2f17f4SAmber Lin * kfd_get_num_xgmi_sdma_engines returns the number of XGMI SDMA.
1339ee2f17f4SAmber Lin * When the device has more than two engines, we reserve two for PCIe to enable
1340ee2f17f4SAmber Lin * full-duplex and the rest are used as XGMI.
1341ee2f17f4SAmber Lin */
kfd_get_num_sdma_engines(struct kfd_node * node)13428dc1db31SMukul Joshi unsigned int kfd_get_num_sdma_engines(struct kfd_node *node)
1343ee2f17f4SAmber Lin {
1344ee2f17f4SAmber Lin /* If XGMI is not supported, all SDMA engines are PCIe */
13458dc1db31SMukul Joshi if (!node->adev->gmc.xgmi.supported)
1346a805889aSMukul Joshi return node->adev->sdma.num_instances/(int)node->kfd->num_nodes;
1347ee2f17f4SAmber Lin
1348a805889aSMukul Joshi return min(node->adev->sdma.num_instances/(int)node->kfd->num_nodes, 2);
1349ee2f17f4SAmber Lin }
1350ee2f17f4SAmber Lin
kfd_get_num_xgmi_sdma_engines(struct kfd_node * node)13518dc1db31SMukul Joshi unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_node *node)
1352ee2f17f4SAmber Lin {
1353ee2f17f4SAmber Lin /* After reserved for PCIe, the rest of engines are XGMI */
1354a805889aSMukul Joshi return node->adev->sdma.num_instances/(int)node->kfd->num_nodes -
1355a805889aSMukul Joshi kfd_get_num_sdma_engines(node);
1356ee2f17f4SAmber Lin }
1357ee2f17f4SAmber Lin
kgd2kfd_check_and_lock_kfd(void)13580c7315e7SMukul Joshi int kgd2kfd_check_and_lock_kfd(void)
13590c7315e7SMukul Joshi {
13600c7315e7SMukul Joshi mutex_lock(&kfd_processes_mutex);
13610c7315e7SMukul Joshi if (!hash_empty(kfd_processes_table) || kfd_is_locked()) {
13620c7315e7SMukul Joshi mutex_unlock(&kfd_processes_mutex);
13630c7315e7SMukul Joshi return -EBUSY;
13640c7315e7SMukul Joshi }
13650c7315e7SMukul Joshi
13660c7315e7SMukul Joshi ++kfd_locked;
13670c7315e7SMukul Joshi mutex_unlock(&kfd_processes_mutex);
13680c7315e7SMukul Joshi
13690c7315e7SMukul Joshi return 0;
13700c7315e7SMukul Joshi }
13710c7315e7SMukul Joshi
kgd2kfd_unlock_kfd(void)13720c7315e7SMukul Joshi void kgd2kfd_unlock_kfd(void)
13730c7315e7SMukul Joshi {
13740c7315e7SMukul Joshi mutex_lock(&kfd_processes_mutex);
13750c7315e7SMukul Joshi --kfd_locked;
13760c7315e7SMukul Joshi mutex_unlock(&kfd_processes_mutex);
13770c7315e7SMukul Joshi }
13780c7315e7SMukul Joshi
1379a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1380a29ec470SShaoyun Liu
1381a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1382a29ec470SShaoyun Liu * which will trigger a GPU reset and bring the HWS back to normal state
1383a29ec470SShaoyun Liu */
kfd_debugfs_hang_hws(struct kfd_node * dev)13848dc1db31SMukul Joshi int kfd_debugfs_hang_hws(struct kfd_node *dev)
1385a29ec470SShaoyun Liu {
1386a29ec470SShaoyun Liu if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1387a29ec470SShaoyun Liu pr_err("HWS is not enabled");
1388a29ec470SShaoyun Liu return -EINVAL;
1389a29ec470SShaoyun Liu }
1390a29ec470SShaoyun Liu
13914f942aaeSOak Zeng return dqm_debugfs_hang_hws(dev->dqm);
1392a29ec470SShaoyun Liu }
1393a29ec470SShaoyun Liu
1394a29ec470SShaoyun Liu #endif
1395