xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v8.c (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2014 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  */
22*b843c749SSergey Zigachev 
23*b843c749SSergey Zigachev #include <linux/module.h>
24*b843c749SSergey Zigachev #include <linux/fdtable.h>
25*b843c749SSergey Zigachev #include <linux/uaccess.h>
26*b843c749SSergey Zigachev #include <linux/firmware.h>
27*b843c749SSergey Zigachev #include <drm/drmP.h>
28*b843c749SSergey Zigachev #include "amdgpu.h"
29*b843c749SSergey Zigachev #include "amdgpu_amdkfd.h"
30*b843c749SSergey Zigachev #include "amdgpu_ucode.h"
31*b843c749SSergey Zigachev #include "gfx_v8_0.h"
32*b843c749SSergey Zigachev #include "gca/gfx_8_0_sh_mask.h"
33*b843c749SSergey Zigachev #include "gca/gfx_8_0_d.h"
34*b843c749SSergey Zigachev #include "gca/gfx_8_0_enum.h"
35*b843c749SSergey Zigachev #include "oss/oss_3_0_sh_mask.h"
36*b843c749SSergey Zigachev #include "oss/oss_3_0_d.h"
37*b843c749SSergey Zigachev #include "gmc/gmc_8_1_sh_mask.h"
38*b843c749SSergey Zigachev #include "gmc/gmc_8_1_d.h"
39*b843c749SSergey Zigachev #include "vi_structs.h"
40*b843c749SSergey Zigachev #include "vid.h"
41*b843c749SSergey Zigachev 
42*b843c749SSergey Zigachev enum hqd_dequeue_request_type {
43*b843c749SSergey Zigachev 	NO_ACTION = 0,
44*b843c749SSergey Zigachev 	DRAIN_PIPE,
45*b843c749SSergey Zigachev 	RESET_WAVES
46*b843c749SSergey Zigachev };
47*b843c749SSergey Zigachev 
48*b843c749SSergey Zigachev struct vi_sdma_mqd;
49*b843c749SSergey Zigachev 
50*b843c749SSergey Zigachev /*
51*b843c749SSergey Zigachev  * Register access functions
52*b843c749SSergey Zigachev  */
53*b843c749SSergey Zigachev 
54*b843c749SSergey Zigachev static void kgd_program_sh_mem_settings(struct kgd_dev *kgd, uint32_t vmid,
55*b843c749SSergey Zigachev 		uint32_t sh_mem_config,
56*b843c749SSergey Zigachev 		uint32_t sh_mem_ape1_base, uint32_t sh_mem_ape1_limit,
57*b843c749SSergey Zigachev 		uint32_t sh_mem_bases);
58*b843c749SSergey Zigachev static int kgd_set_pasid_vmid_mapping(struct kgd_dev *kgd, unsigned int pasid,
59*b843c749SSergey Zigachev 		unsigned int vmid);
60*b843c749SSergey Zigachev static int kgd_init_interrupts(struct kgd_dev *kgd, uint32_t pipe_id);
61*b843c749SSergey Zigachev static int kgd_hqd_load(struct kgd_dev *kgd, void *mqd, uint32_t pipe_id,
62*b843c749SSergey Zigachev 			uint32_t queue_id, uint32_t __user *wptr,
63*b843c749SSergey Zigachev 			uint32_t wptr_shift, uint32_t wptr_mask,
64*b843c749SSergey Zigachev 			struct mm_struct *mm);
65*b843c749SSergey Zigachev static int kgd_hqd_dump(struct kgd_dev *kgd,
66*b843c749SSergey Zigachev 			uint32_t pipe_id, uint32_t queue_id,
67*b843c749SSergey Zigachev 			uint32_t (**dump)[2], uint32_t *n_regs);
68*b843c749SSergey Zigachev static int kgd_hqd_sdma_load(struct kgd_dev *kgd, void *mqd,
69*b843c749SSergey Zigachev 			     uint32_t __user *wptr, struct mm_struct *mm);
70*b843c749SSergey Zigachev static int kgd_hqd_sdma_dump(struct kgd_dev *kgd,
71*b843c749SSergey Zigachev 			     uint32_t engine_id, uint32_t queue_id,
72*b843c749SSergey Zigachev 			     uint32_t (**dump)[2], uint32_t *n_regs);
73*b843c749SSergey Zigachev static bool kgd_hqd_is_occupied(struct kgd_dev *kgd, uint64_t queue_address,
74*b843c749SSergey Zigachev 		uint32_t pipe_id, uint32_t queue_id);
75*b843c749SSergey Zigachev static bool kgd_hqd_sdma_is_occupied(struct kgd_dev *kgd, void *mqd);
76*b843c749SSergey Zigachev static int kgd_hqd_destroy(struct kgd_dev *kgd, void *mqd,
77*b843c749SSergey Zigachev 				enum kfd_preempt_type reset_type,
78*b843c749SSergey Zigachev 				unsigned int utimeout, uint32_t pipe_id,
79*b843c749SSergey Zigachev 				uint32_t queue_id);
80*b843c749SSergey Zigachev static int kgd_hqd_sdma_destroy(struct kgd_dev *kgd, void *mqd,
81*b843c749SSergey Zigachev 				unsigned int utimeout);
82*b843c749SSergey Zigachev static int kgd_address_watch_disable(struct kgd_dev *kgd);
83*b843c749SSergey Zigachev static int kgd_address_watch_execute(struct kgd_dev *kgd,
84*b843c749SSergey Zigachev 					unsigned int watch_point_id,
85*b843c749SSergey Zigachev 					uint32_t cntl_val,
86*b843c749SSergey Zigachev 					uint32_t addr_hi,
87*b843c749SSergey Zigachev 					uint32_t addr_lo);
88*b843c749SSergey Zigachev static int kgd_wave_control_execute(struct kgd_dev *kgd,
89*b843c749SSergey Zigachev 					uint32_t gfx_index_val,
90*b843c749SSergey Zigachev 					uint32_t sq_cmd);
91*b843c749SSergey Zigachev static uint32_t kgd_address_watch_get_offset(struct kgd_dev *kgd,
92*b843c749SSergey Zigachev 					unsigned int watch_point_id,
93*b843c749SSergey Zigachev 					unsigned int reg_offset);
94*b843c749SSergey Zigachev 
95*b843c749SSergey Zigachev static bool get_atc_vmid_pasid_mapping_valid(struct kgd_dev *kgd,
96*b843c749SSergey Zigachev 		uint8_t vmid);
97*b843c749SSergey Zigachev static uint16_t get_atc_vmid_pasid_mapping_pasid(struct kgd_dev *kgd,
98*b843c749SSergey Zigachev 		uint8_t vmid);
99*b843c749SSergey Zigachev static uint16_t get_fw_version(struct kgd_dev *kgd, enum kgd_engine_type type);
100*b843c749SSergey Zigachev static void set_scratch_backing_va(struct kgd_dev *kgd,
101*b843c749SSergey Zigachev 					uint64_t va, uint32_t vmid);
102*b843c749SSergey Zigachev static void set_vm_context_page_table_base(struct kgd_dev *kgd, uint32_t vmid,
103*b843c749SSergey Zigachev 		uint32_t page_table_base);
104*b843c749SSergey Zigachev static int invalidate_tlbs(struct kgd_dev *kgd, uint16_t pasid);
105*b843c749SSergey Zigachev static int invalidate_tlbs_vmid(struct kgd_dev *kgd, uint16_t vmid);
106*b843c749SSergey Zigachev 
107*b843c749SSergey Zigachev /* Because of REG_GET_FIELD() being used, we put this function in the
108*b843c749SSergey Zigachev  * asic specific file.
109*b843c749SSergey Zigachev  */
get_tile_config(struct kgd_dev * kgd,struct tile_config * config)110*b843c749SSergey Zigachev static int get_tile_config(struct kgd_dev *kgd,
111*b843c749SSergey Zigachev 		struct tile_config *config)
112*b843c749SSergey Zigachev {
113*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
114*b843c749SSergey Zigachev 
115*b843c749SSergey Zigachev 	config->gb_addr_config = adev->gfx.config.gb_addr_config;
116*b843c749SSergey Zigachev 	config->num_banks = REG_GET_FIELD(adev->gfx.config.mc_arb_ramcfg,
117*b843c749SSergey Zigachev 				MC_ARB_RAMCFG, NOOFBANK);
118*b843c749SSergey Zigachev 	config->num_ranks = REG_GET_FIELD(adev->gfx.config.mc_arb_ramcfg,
119*b843c749SSergey Zigachev 				MC_ARB_RAMCFG, NOOFRANKS);
120*b843c749SSergey Zigachev 
121*b843c749SSergey Zigachev 	config->tile_config_ptr = adev->gfx.config.tile_mode_array;
122*b843c749SSergey Zigachev 	config->num_tile_configs =
123*b843c749SSergey Zigachev 			ARRAY_SIZE(adev->gfx.config.tile_mode_array);
124*b843c749SSergey Zigachev 	config->macro_tile_config_ptr =
125*b843c749SSergey Zigachev 			adev->gfx.config.macrotile_mode_array;
126*b843c749SSergey Zigachev 	config->num_macro_tile_configs =
127*b843c749SSergey Zigachev 			ARRAY_SIZE(adev->gfx.config.macrotile_mode_array);
128*b843c749SSergey Zigachev 
129*b843c749SSergey Zigachev 	return 0;
130*b843c749SSergey Zigachev }
131*b843c749SSergey Zigachev 
132*b843c749SSergey Zigachev static const struct kfd2kgd_calls kfd2kgd = {
133*b843c749SSergey Zigachev 	.init_gtt_mem_allocation = alloc_gtt_mem,
134*b843c749SSergey Zigachev 	.free_gtt_mem = free_gtt_mem,
135*b843c749SSergey Zigachev 	.get_local_mem_info = get_local_mem_info,
136*b843c749SSergey Zigachev 	.get_gpu_clock_counter = get_gpu_clock_counter,
137*b843c749SSergey Zigachev 	.get_max_engine_clock_in_mhz = get_max_engine_clock_in_mhz,
138*b843c749SSergey Zigachev 	.alloc_pasid = amdgpu_pasid_alloc,
139*b843c749SSergey Zigachev 	.free_pasid = amdgpu_pasid_free,
140*b843c749SSergey Zigachev 	.program_sh_mem_settings = kgd_program_sh_mem_settings,
141*b843c749SSergey Zigachev 	.set_pasid_vmid_mapping = kgd_set_pasid_vmid_mapping,
142*b843c749SSergey Zigachev 	.init_interrupts = kgd_init_interrupts,
143*b843c749SSergey Zigachev 	.hqd_load = kgd_hqd_load,
144*b843c749SSergey Zigachev 	.hqd_sdma_load = kgd_hqd_sdma_load,
145*b843c749SSergey Zigachev 	.hqd_dump = kgd_hqd_dump,
146*b843c749SSergey Zigachev 	.hqd_sdma_dump = kgd_hqd_sdma_dump,
147*b843c749SSergey Zigachev 	.hqd_is_occupied = kgd_hqd_is_occupied,
148*b843c749SSergey Zigachev 	.hqd_sdma_is_occupied = kgd_hqd_sdma_is_occupied,
149*b843c749SSergey Zigachev 	.hqd_destroy = kgd_hqd_destroy,
150*b843c749SSergey Zigachev 	.hqd_sdma_destroy = kgd_hqd_sdma_destroy,
151*b843c749SSergey Zigachev 	.address_watch_disable = kgd_address_watch_disable,
152*b843c749SSergey Zigachev 	.address_watch_execute = kgd_address_watch_execute,
153*b843c749SSergey Zigachev 	.wave_control_execute = kgd_wave_control_execute,
154*b843c749SSergey Zigachev 	.address_watch_get_offset = kgd_address_watch_get_offset,
155*b843c749SSergey Zigachev 	.get_atc_vmid_pasid_mapping_pasid =
156*b843c749SSergey Zigachev 			get_atc_vmid_pasid_mapping_pasid,
157*b843c749SSergey Zigachev 	.get_atc_vmid_pasid_mapping_valid =
158*b843c749SSergey Zigachev 			get_atc_vmid_pasid_mapping_valid,
159*b843c749SSergey Zigachev 	.get_fw_version = get_fw_version,
160*b843c749SSergey Zigachev 	.set_scratch_backing_va = set_scratch_backing_va,
161*b843c749SSergey Zigachev 	.get_tile_config = get_tile_config,
162*b843c749SSergey Zigachev 	.get_cu_info = get_cu_info,
163*b843c749SSergey Zigachev 	.get_vram_usage = amdgpu_amdkfd_get_vram_usage,
164*b843c749SSergey Zigachev 	.create_process_vm = amdgpu_amdkfd_gpuvm_create_process_vm,
165*b843c749SSergey Zigachev 	.acquire_process_vm = amdgpu_amdkfd_gpuvm_acquire_process_vm,
166*b843c749SSergey Zigachev 	.destroy_process_vm = amdgpu_amdkfd_gpuvm_destroy_process_vm,
167*b843c749SSergey Zigachev 	.get_process_page_dir = amdgpu_amdkfd_gpuvm_get_process_page_dir,
168*b843c749SSergey Zigachev 	.set_vm_context_page_table_base = set_vm_context_page_table_base,
169*b843c749SSergey Zigachev 	.alloc_memory_of_gpu = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu,
170*b843c749SSergey Zigachev 	.free_memory_of_gpu = amdgpu_amdkfd_gpuvm_free_memory_of_gpu,
171*b843c749SSergey Zigachev 	.map_memory_to_gpu = amdgpu_amdkfd_gpuvm_map_memory_to_gpu,
172*b843c749SSergey Zigachev 	.unmap_memory_to_gpu = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu,
173*b843c749SSergey Zigachev 	.sync_memory = amdgpu_amdkfd_gpuvm_sync_memory,
174*b843c749SSergey Zigachev 	.map_gtt_bo_to_kernel = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel,
175*b843c749SSergey Zigachev 	.restore_process_bos = amdgpu_amdkfd_gpuvm_restore_process_bos,
176*b843c749SSergey Zigachev 	.invalidate_tlbs = invalidate_tlbs,
177*b843c749SSergey Zigachev 	.invalidate_tlbs_vmid = invalidate_tlbs_vmid,
178*b843c749SSergey Zigachev 	.submit_ib = amdgpu_amdkfd_submit_ib,
179*b843c749SSergey Zigachev 	.get_vm_fault_info = amdgpu_amdkfd_gpuvm_get_vm_fault_info,
180*b843c749SSergey Zigachev 	.gpu_recover = amdgpu_amdkfd_gpu_reset,
181*b843c749SSergey Zigachev 	.set_compute_idle = amdgpu_amdkfd_set_compute_idle
182*b843c749SSergey Zigachev };
183*b843c749SSergey Zigachev 
amdgpu_amdkfd_gfx_8_0_get_functions(void)184*b843c749SSergey Zigachev struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void)
185*b843c749SSergey Zigachev {
186*b843c749SSergey Zigachev 	return (struct kfd2kgd_calls *)&kfd2kgd;
187*b843c749SSergey Zigachev }
188*b843c749SSergey Zigachev 
get_amdgpu_device(struct kgd_dev * kgd)189*b843c749SSergey Zigachev static inline struct amdgpu_device *get_amdgpu_device(struct kgd_dev *kgd)
190*b843c749SSergey Zigachev {
191*b843c749SSergey Zigachev 	return (struct amdgpu_device *)kgd;
192*b843c749SSergey Zigachev }
193*b843c749SSergey Zigachev 
lock_srbm(struct kgd_dev * kgd,uint32_t mec,uint32_t pipe,uint32_t queue,uint32_t vmid)194*b843c749SSergey Zigachev static void lock_srbm(struct kgd_dev *kgd, uint32_t mec, uint32_t pipe,
195*b843c749SSergey Zigachev 			uint32_t queue, uint32_t vmid)
196*b843c749SSergey Zigachev {
197*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
198*b843c749SSergey Zigachev 	uint32_t value = PIPEID(pipe) | MEID(mec) | VMID(vmid) | QUEUEID(queue);
199*b843c749SSergey Zigachev 
200*b843c749SSergey Zigachev 	mutex_lock(&adev->srbm_mutex);
201*b843c749SSergey Zigachev 	WREG32(mmSRBM_GFX_CNTL, value);
202*b843c749SSergey Zigachev }
203*b843c749SSergey Zigachev 
unlock_srbm(struct kgd_dev * kgd)204*b843c749SSergey Zigachev static void unlock_srbm(struct kgd_dev *kgd)
205*b843c749SSergey Zigachev {
206*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
207*b843c749SSergey Zigachev 
208*b843c749SSergey Zigachev 	WREG32(mmSRBM_GFX_CNTL, 0);
209*b843c749SSergey Zigachev 	mutex_unlock(&adev->srbm_mutex);
210*b843c749SSergey Zigachev }
211*b843c749SSergey Zigachev 
acquire_queue(struct kgd_dev * kgd,uint32_t pipe_id,uint32_t queue_id)212*b843c749SSergey Zigachev static void acquire_queue(struct kgd_dev *kgd, uint32_t pipe_id,
213*b843c749SSergey Zigachev 				uint32_t queue_id)
214*b843c749SSergey Zigachev {
215*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
216*b843c749SSergey Zigachev 
217*b843c749SSergey Zigachev 	uint32_t mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
218*b843c749SSergey Zigachev 	uint32_t pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
219*b843c749SSergey Zigachev 
220*b843c749SSergey Zigachev 	lock_srbm(kgd, mec, pipe, queue_id, 0);
221*b843c749SSergey Zigachev }
222*b843c749SSergey Zigachev 
release_queue(struct kgd_dev * kgd)223*b843c749SSergey Zigachev static void release_queue(struct kgd_dev *kgd)
224*b843c749SSergey Zigachev {
225*b843c749SSergey Zigachev 	unlock_srbm(kgd);
226*b843c749SSergey Zigachev }
227*b843c749SSergey Zigachev 
kgd_program_sh_mem_settings(struct kgd_dev * kgd,uint32_t vmid,uint32_t sh_mem_config,uint32_t sh_mem_ape1_base,uint32_t sh_mem_ape1_limit,uint32_t sh_mem_bases)228*b843c749SSergey Zigachev static void kgd_program_sh_mem_settings(struct kgd_dev *kgd, uint32_t vmid,
229*b843c749SSergey Zigachev 					uint32_t sh_mem_config,
230*b843c749SSergey Zigachev 					uint32_t sh_mem_ape1_base,
231*b843c749SSergey Zigachev 					uint32_t sh_mem_ape1_limit,
232*b843c749SSergey Zigachev 					uint32_t sh_mem_bases)
233*b843c749SSergey Zigachev {
234*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
235*b843c749SSergey Zigachev 
236*b843c749SSergey Zigachev 	lock_srbm(kgd, 0, 0, 0, vmid);
237*b843c749SSergey Zigachev 
238*b843c749SSergey Zigachev 	WREG32(mmSH_MEM_CONFIG, sh_mem_config);
239*b843c749SSergey Zigachev 	WREG32(mmSH_MEM_APE1_BASE, sh_mem_ape1_base);
240*b843c749SSergey Zigachev 	WREG32(mmSH_MEM_APE1_LIMIT, sh_mem_ape1_limit);
241*b843c749SSergey Zigachev 	WREG32(mmSH_MEM_BASES, sh_mem_bases);
242*b843c749SSergey Zigachev 
243*b843c749SSergey Zigachev 	unlock_srbm(kgd);
244*b843c749SSergey Zigachev }
245*b843c749SSergey Zigachev 
kgd_set_pasid_vmid_mapping(struct kgd_dev * kgd,unsigned int pasid,unsigned int vmid)246*b843c749SSergey Zigachev static int kgd_set_pasid_vmid_mapping(struct kgd_dev *kgd, unsigned int pasid,
247*b843c749SSergey Zigachev 					unsigned int vmid)
248*b843c749SSergey Zigachev {
249*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
250*b843c749SSergey Zigachev 
251*b843c749SSergey Zigachev 	/*
252*b843c749SSergey Zigachev 	 * We have to assume that there is no outstanding mapping.
253*b843c749SSergey Zigachev 	 * The ATC_VMID_PASID_MAPPING_UPDATE_STATUS bit could be 0 because
254*b843c749SSergey Zigachev 	 * a mapping is in progress or because a mapping finished
255*b843c749SSergey Zigachev 	 * and the SW cleared it.
256*b843c749SSergey Zigachev 	 * So the protocol is to always wait & clear.
257*b843c749SSergey Zigachev 	 */
258*b843c749SSergey Zigachev 	uint32_t pasid_mapping = (pasid == 0) ? 0 : (uint32_t)pasid |
259*b843c749SSergey Zigachev 			ATC_VMID0_PASID_MAPPING__VALID_MASK;
260*b843c749SSergey Zigachev 
261*b843c749SSergey Zigachev 	WREG32(mmATC_VMID0_PASID_MAPPING + vmid, pasid_mapping);
262*b843c749SSergey Zigachev 
263*b843c749SSergey Zigachev 	while (!(RREG32(mmATC_VMID_PASID_MAPPING_UPDATE_STATUS) & (1U << vmid)))
264*b843c749SSergey Zigachev 		cpu_relax();
265*b843c749SSergey Zigachev 	WREG32(mmATC_VMID_PASID_MAPPING_UPDATE_STATUS, 1U << vmid);
266*b843c749SSergey Zigachev 
267*b843c749SSergey Zigachev 	/* Mapping vmid to pasid also for IH block */
268*b843c749SSergey Zigachev 	WREG32(mmIH_VMID_0_LUT + vmid, pasid_mapping);
269*b843c749SSergey Zigachev 
270*b843c749SSergey Zigachev 	return 0;
271*b843c749SSergey Zigachev }
272*b843c749SSergey Zigachev 
kgd_init_interrupts(struct kgd_dev * kgd,uint32_t pipe_id)273*b843c749SSergey Zigachev static int kgd_init_interrupts(struct kgd_dev *kgd, uint32_t pipe_id)
274*b843c749SSergey Zigachev {
275*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
276*b843c749SSergey Zigachev 	uint32_t mec;
277*b843c749SSergey Zigachev 	uint32_t pipe;
278*b843c749SSergey Zigachev 
279*b843c749SSergey Zigachev 	mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
280*b843c749SSergey Zigachev 	pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
281*b843c749SSergey Zigachev 
282*b843c749SSergey Zigachev 	lock_srbm(kgd, mec, pipe, 0, 0);
283*b843c749SSergey Zigachev 
284*b843c749SSergey Zigachev 	WREG32(mmCPC_INT_CNTL, CP_INT_CNTL_RING0__TIME_STAMP_INT_ENABLE_MASK);
285*b843c749SSergey Zigachev 
286*b843c749SSergey Zigachev 	unlock_srbm(kgd);
287*b843c749SSergey Zigachev 
288*b843c749SSergey Zigachev 	return 0;
289*b843c749SSergey Zigachev }
290*b843c749SSergey Zigachev 
get_sdma_base_addr(struct vi_sdma_mqd * m)291*b843c749SSergey Zigachev static inline uint32_t get_sdma_base_addr(struct vi_sdma_mqd *m)
292*b843c749SSergey Zigachev {
293*b843c749SSergey Zigachev 	uint32_t retval;
294*b843c749SSergey Zigachev 
295*b843c749SSergey Zigachev 	retval = m->sdma_engine_id * SDMA1_REGISTER_OFFSET +
296*b843c749SSergey Zigachev 		m->sdma_queue_id * KFD_VI_SDMA_QUEUE_OFFSET;
297*b843c749SSergey Zigachev 	pr_debug("kfd: sdma base address: 0x%x\n", retval);
298*b843c749SSergey Zigachev 
299*b843c749SSergey Zigachev 	return retval;
300*b843c749SSergey Zigachev }
301*b843c749SSergey Zigachev 
get_mqd(void * mqd)302*b843c749SSergey Zigachev static inline struct vi_mqd *get_mqd(void *mqd)
303*b843c749SSergey Zigachev {
304*b843c749SSergey Zigachev 	return (struct vi_mqd *)mqd;
305*b843c749SSergey Zigachev }
306*b843c749SSergey Zigachev 
get_sdma_mqd(void * mqd)307*b843c749SSergey Zigachev static inline struct vi_sdma_mqd *get_sdma_mqd(void *mqd)
308*b843c749SSergey Zigachev {
309*b843c749SSergey Zigachev 	return (struct vi_sdma_mqd *)mqd;
310*b843c749SSergey Zigachev }
311*b843c749SSergey Zigachev 
kgd_hqd_load(struct kgd_dev * kgd,void * mqd,uint32_t pipe_id,uint32_t queue_id,uint32_t __user * wptr,uint32_t wptr_shift,uint32_t wptr_mask,struct mm_struct * mm)312*b843c749SSergey Zigachev static int kgd_hqd_load(struct kgd_dev *kgd, void *mqd, uint32_t pipe_id,
313*b843c749SSergey Zigachev 			uint32_t queue_id, uint32_t __user *wptr,
314*b843c749SSergey Zigachev 			uint32_t wptr_shift, uint32_t wptr_mask,
315*b843c749SSergey Zigachev 			struct mm_struct *mm)
316*b843c749SSergey Zigachev {
317*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
318*b843c749SSergey Zigachev 	struct vi_mqd *m;
319*b843c749SSergey Zigachev 	uint32_t *mqd_hqd;
320*b843c749SSergey Zigachev 	uint32_t reg, wptr_val, data;
321*b843c749SSergey Zigachev 	bool valid_wptr = false;
322*b843c749SSergey Zigachev 
323*b843c749SSergey Zigachev 	m = get_mqd(mqd);
324*b843c749SSergey Zigachev 
325*b843c749SSergey Zigachev 	acquire_queue(kgd, pipe_id, queue_id);
326*b843c749SSergey Zigachev 
327*b843c749SSergey Zigachev 	/* HIQ is set during driver init period with vmid set to 0*/
328*b843c749SSergey Zigachev 	if (m->cp_hqd_vmid == 0) {
329*b843c749SSergey Zigachev 		uint32_t value, mec, pipe;
330*b843c749SSergey Zigachev 
331*b843c749SSergey Zigachev 		mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
332*b843c749SSergey Zigachev 		pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
333*b843c749SSergey Zigachev 
334*b843c749SSergey Zigachev 		pr_debug("kfd: set HIQ, mec:%d, pipe:%d, queue:%d.\n",
335*b843c749SSergey Zigachev 			mec, pipe, queue_id);
336*b843c749SSergey Zigachev 		value = RREG32(mmRLC_CP_SCHEDULERS);
337*b843c749SSergey Zigachev 		value = REG_SET_FIELD(value, RLC_CP_SCHEDULERS, scheduler1,
338*b843c749SSergey Zigachev 			((mec << 5) | (pipe << 3) | queue_id | 0x80));
339*b843c749SSergey Zigachev 		WREG32(mmRLC_CP_SCHEDULERS, value);
340*b843c749SSergey Zigachev 	}
341*b843c749SSergey Zigachev 
342*b843c749SSergey Zigachev 	/* HQD registers extend from CP_MQD_BASE_ADDR to CP_HQD_EOP_WPTR_MEM. */
343*b843c749SSergey Zigachev 	mqd_hqd = &m->cp_mqd_base_addr_lo;
344*b843c749SSergey Zigachev 
345*b843c749SSergey Zigachev 	for (reg = mmCP_MQD_BASE_ADDR; reg <= mmCP_HQD_EOP_CONTROL; reg++)
346*b843c749SSergey Zigachev 		WREG32(reg, mqd_hqd[reg - mmCP_MQD_BASE_ADDR]);
347*b843c749SSergey Zigachev 
348*b843c749SSergey Zigachev 	/* Tonga errata: EOP RPTR/WPTR should be left unmodified.
349*b843c749SSergey Zigachev 	 * This is safe since EOP RPTR==WPTR for any inactive HQD
350*b843c749SSergey Zigachev 	 * on ASICs that do not support context-save.
351*b843c749SSergey Zigachev 	 * EOP writes/reads can start anywhere in the ring.
352*b843c749SSergey Zigachev 	 */
353*b843c749SSergey Zigachev 	if (get_amdgpu_device(kgd)->asic_type != CHIP_TONGA) {
354*b843c749SSergey Zigachev 		WREG32(mmCP_HQD_EOP_RPTR, m->cp_hqd_eop_rptr);
355*b843c749SSergey Zigachev 		WREG32(mmCP_HQD_EOP_WPTR, m->cp_hqd_eop_wptr);
356*b843c749SSergey Zigachev 		WREG32(mmCP_HQD_EOP_WPTR_MEM, m->cp_hqd_eop_wptr_mem);
357*b843c749SSergey Zigachev 	}
358*b843c749SSergey Zigachev 
359*b843c749SSergey Zigachev 	for (reg = mmCP_HQD_EOP_EVENTS; reg <= mmCP_HQD_ERROR; reg++)
360*b843c749SSergey Zigachev 		WREG32(reg, mqd_hqd[reg - mmCP_MQD_BASE_ADDR]);
361*b843c749SSergey Zigachev 
362*b843c749SSergey Zigachev 	/* Copy userspace write pointer value to register.
363*b843c749SSergey Zigachev 	 * Activate doorbell logic to monitor subsequent changes.
364*b843c749SSergey Zigachev 	 */
365*b843c749SSergey Zigachev 	data = REG_SET_FIELD(m->cp_hqd_pq_doorbell_control,
366*b843c749SSergey Zigachev 			     CP_HQD_PQ_DOORBELL_CONTROL, DOORBELL_EN, 1);
367*b843c749SSergey Zigachev 	WREG32(mmCP_HQD_PQ_DOORBELL_CONTROL, data);
368*b843c749SSergey Zigachev 
369*b843c749SSergey Zigachev 	/* read_user_ptr may take the mm->mmap_sem.
370*b843c749SSergey Zigachev 	 * release srbm_mutex to avoid circular dependency between
371*b843c749SSergey Zigachev 	 * srbm_mutex->mm_sem->reservation_ww_class_mutex->srbm_mutex.
372*b843c749SSergey Zigachev 	 */
373*b843c749SSergey Zigachev 	release_queue(kgd);
374*b843c749SSergey Zigachev 	valid_wptr = read_user_wptr(mm, wptr, wptr_val);
375*b843c749SSergey Zigachev 	acquire_queue(kgd, pipe_id, queue_id);
376*b843c749SSergey Zigachev 	if (valid_wptr)
377*b843c749SSergey Zigachev 		WREG32(mmCP_HQD_PQ_WPTR, (wptr_val << wptr_shift) & wptr_mask);
378*b843c749SSergey Zigachev 
379*b843c749SSergey Zigachev 	data = REG_SET_FIELD(m->cp_hqd_active, CP_HQD_ACTIVE, ACTIVE, 1);
380*b843c749SSergey Zigachev 	WREG32(mmCP_HQD_ACTIVE, data);
381*b843c749SSergey Zigachev 
382*b843c749SSergey Zigachev 	release_queue(kgd);
383*b843c749SSergey Zigachev 
384*b843c749SSergey Zigachev 	return 0;
385*b843c749SSergey Zigachev }
386*b843c749SSergey Zigachev 
kgd_hqd_dump(struct kgd_dev * kgd,uint32_t pipe_id,uint32_t queue_id,uint32_t (** dump)[2],uint32_t * n_regs)387*b843c749SSergey Zigachev static int kgd_hqd_dump(struct kgd_dev *kgd,
388*b843c749SSergey Zigachev 			uint32_t pipe_id, uint32_t queue_id,
389*b843c749SSergey Zigachev 			uint32_t (**dump)[2], uint32_t *n_regs)
390*b843c749SSergey Zigachev {
391*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
392*b843c749SSergey Zigachev 	uint32_t i = 0, reg;
393*b843c749SSergey Zigachev #define HQD_N_REGS (54+4)
394*b843c749SSergey Zigachev #define DUMP_REG(addr) do {				\
395*b843c749SSergey Zigachev 		if (WARN_ON_ONCE(i >= HQD_N_REGS))	\
396*b843c749SSergey Zigachev 			break;				\
397*b843c749SSergey Zigachev 		(*dump)[i][0] = (addr) << 2;		\
398*b843c749SSergey Zigachev 		(*dump)[i++][1] = RREG32(addr);		\
399*b843c749SSergey Zigachev 	} while (0)
400*b843c749SSergey Zigachev 
401*b843c749SSergey Zigachev 	*dump = kmalloc_array(HQD_N_REGS * 2, sizeof(uint32_t), GFP_KERNEL);
402*b843c749SSergey Zigachev 	if (*dump == NULL)
403*b843c749SSergey Zigachev 		return -ENOMEM;
404*b843c749SSergey Zigachev 
405*b843c749SSergey Zigachev 	acquire_queue(kgd, pipe_id, queue_id);
406*b843c749SSergey Zigachev 
407*b843c749SSergey Zigachev 	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE0);
408*b843c749SSergey Zigachev 	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE1);
409*b843c749SSergey Zigachev 	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE2);
410*b843c749SSergey Zigachev 	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE3);
411*b843c749SSergey Zigachev 
412*b843c749SSergey Zigachev 	for (reg = mmCP_MQD_BASE_ADDR; reg <= mmCP_HQD_EOP_DONES; reg++)
413*b843c749SSergey Zigachev 		DUMP_REG(reg);
414*b843c749SSergey Zigachev 
415*b843c749SSergey Zigachev 	release_queue(kgd);
416*b843c749SSergey Zigachev 
417*b843c749SSergey Zigachev 	WARN_ON_ONCE(i != HQD_N_REGS);
418*b843c749SSergey Zigachev 	*n_regs = i;
419*b843c749SSergey Zigachev 
420*b843c749SSergey Zigachev 	return 0;
421*b843c749SSergey Zigachev }
422*b843c749SSergey Zigachev 
kgd_hqd_sdma_load(struct kgd_dev * kgd,void * mqd,uint32_t __user * wptr,struct mm_struct * mm)423*b843c749SSergey Zigachev static int kgd_hqd_sdma_load(struct kgd_dev *kgd, void *mqd,
424*b843c749SSergey Zigachev 			     uint32_t __user *wptr, struct mm_struct *mm)
425*b843c749SSergey Zigachev {
426*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
427*b843c749SSergey Zigachev 	struct vi_sdma_mqd *m;
428*b843c749SSergey Zigachev 	unsigned long end_jiffies;
429*b843c749SSergey Zigachev 	uint32_t sdma_base_addr;
430*b843c749SSergey Zigachev 	uint32_t data;
431*b843c749SSergey Zigachev 
432*b843c749SSergey Zigachev 	m = get_sdma_mqd(mqd);
433*b843c749SSergey Zigachev 	sdma_base_addr = get_sdma_base_addr(m);
434*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL,
435*b843c749SSergey Zigachev 		m->sdmax_rlcx_rb_cntl & (~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK));
436*b843c749SSergey Zigachev 
437*b843c749SSergey Zigachev 	end_jiffies = msecs_to_jiffies(2000) + jiffies;
438*b843c749SSergey Zigachev 	while (true) {
439*b843c749SSergey Zigachev 		data = RREG32(sdma_base_addr + mmSDMA0_RLC0_CONTEXT_STATUS);
440*b843c749SSergey Zigachev 		if (data & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
441*b843c749SSergey Zigachev 			break;
442*b843c749SSergey Zigachev 		if (time_after(jiffies, end_jiffies))
443*b843c749SSergey Zigachev 			return -ETIME;
444*b843c749SSergey Zigachev 		usleep_range(500, 1000);
445*b843c749SSergey Zigachev 	}
446*b843c749SSergey Zigachev 	if (m->sdma_engine_id) {
447*b843c749SSergey Zigachev 		data = RREG32(mmSDMA1_GFX_CONTEXT_CNTL);
448*b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, SDMA1_GFX_CONTEXT_CNTL,
449*b843c749SSergey Zigachev 				RESUME_CTX, 0);
450*b843c749SSergey Zigachev 		WREG32(mmSDMA1_GFX_CONTEXT_CNTL, data);
451*b843c749SSergey Zigachev 	} else {
452*b843c749SSergey Zigachev 		data = RREG32(mmSDMA0_GFX_CONTEXT_CNTL);
453*b843c749SSergey Zigachev 		data = REG_SET_FIELD(data, SDMA0_GFX_CONTEXT_CNTL,
454*b843c749SSergey Zigachev 				RESUME_CTX, 0);
455*b843c749SSergey Zigachev 		WREG32(mmSDMA0_GFX_CONTEXT_CNTL, data);
456*b843c749SSergey Zigachev 	}
457*b843c749SSergey Zigachev 
458*b843c749SSergey Zigachev 	data = REG_SET_FIELD(m->sdmax_rlcx_doorbell, SDMA0_RLC0_DOORBELL,
459*b843c749SSergey Zigachev 			     ENABLE, 1);
460*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_DOORBELL, data);
461*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR, m->sdmax_rlcx_rb_rptr);
462*b843c749SSergey Zigachev 
463*b843c749SSergey Zigachev 	if (read_user_wptr(mm, wptr, data))
464*b843c749SSergey Zigachev 		WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_WPTR, data);
465*b843c749SSergey Zigachev 	else
466*b843c749SSergey Zigachev 		WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_WPTR,
467*b843c749SSergey Zigachev 		       m->sdmax_rlcx_rb_rptr);
468*b843c749SSergey Zigachev 
469*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_VIRTUAL_ADDR,
470*b843c749SSergey Zigachev 				m->sdmax_rlcx_virtual_addr);
471*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_BASE, m->sdmax_rlcx_rb_base);
472*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_BASE_HI,
473*b843c749SSergey Zigachev 			m->sdmax_rlcx_rb_base_hi);
474*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR_ADDR_LO,
475*b843c749SSergey Zigachev 			m->sdmax_rlcx_rb_rptr_addr_lo);
476*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR_ADDR_HI,
477*b843c749SSergey Zigachev 			m->sdmax_rlcx_rb_rptr_addr_hi);
478*b843c749SSergey Zigachev 
479*b843c749SSergey Zigachev 	data = REG_SET_FIELD(m->sdmax_rlcx_rb_cntl, SDMA0_RLC0_RB_CNTL,
480*b843c749SSergey Zigachev 			     RB_ENABLE, 1);
481*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL, data);
482*b843c749SSergey Zigachev 
483*b843c749SSergey Zigachev 	return 0;
484*b843c749SSergey Zigachev }
485*b843c749SSergey Zigachev 
kgd_hqd_sdma_dump(struct kgd_dev * kgd,uint32_t engine_id,uint32_t queue_id,uint32_t (** dump)[2],uint32_t * n_regs)486*b843c749SSergey Zigachev static int kgd_hqd_sdma_dump(struct kgd_dev *kgd,
487*b843c749SSergey Zigachev 			     uint32_t engine_id, uint32_t queue_id,
488*b843c749SSergey Zigachev 			     uint32_t (**dump)[2], uint32_t *n_regs)
489*b843c749SSergey Zigachev {
490*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
491*b843c749SSergey Zigachev 	uint32_t sdma_offset = engine_id * SDMA1_REGISTER_OFFSET +
492*b843c749SSergey Zigachev 		queue_id * KFD_VI_SDMA_QUEUE_OFFSET;
493*b843c749SSergey Zigachev 	uint32_t i = 0, reg;
494*b843c749SSergey Zigachev #undef HQD_N_REGS
495*b843c749SSergey Zigachev #define HQD_N_REGS (19+4+2+3+7)
496*b843c749SSergey Zigachev 
497*b843c749SSergey Zigachev 	*dump = kmalloc_array(HQD_N_REGS * 2, sizeof(uint32_t), GFP_KERNEL);
498*b843c749SSergey Zigachev 	if (*dump == NULL)
499*b843c749SSergey Zigachev 		return -ENOMEM;
500*b843c749SSergey Zigachev 
501*b843c749SSergey Zigachev 	for (reg = mmSDMA0_RLC0_RB_CNTL; reg <= mmSDMA0_RLC0_DOORBELL; reg++)
502*b843c749SSergey Zigachev 		DUMP_REG(sdma_offset + reg);
503*b843c749SSergey Zigachev 	for (reg = mmSDMA0_RLC0_VIRTUAL_ADDR; reg <= mmSDMA0_RLC0_WATERMARK;
504*b843c749SSergey Zigachev 	     reg++)
505*b843c749SSergey Zigachev 		DUMP_REG(sdma_offset + reg);
506*b843c749SSergey Zigachev 	for (reg = mmSDMA0_RLC0_CSA_ADDR_LO; reg <= mmSDMA0_RLC0_CSA_ADDR_HI;
507*b843c749SSergey Zigachev 	     reg++)
508*b843c749SSergey Zigachev 		DUMP_REG(sdma_offset + reg);
509*b843c749SSergey Zigachev 	for (reg = mmSDMA0_RLC0_IB_SUB_REMAIN; reg <= mmSDMA0_RLC0_DUMMY_REG;
510*b843c749SSergey Zigachev 	     reg++)
511*b843c749SSergey Zigachev 		DUMP_REG(sdma_offset + reg);
512*b843c749SSergey Zigachev 	for (reg = mmSDMA0_RLC0_MIDCMD_DATA0; reg <= mmSDMA0_RLC0_MIDCMD_CNTL;
513*b843c749SSergey Zigachev 	     reg++)
514*b843c749SSergey Zigachev 		DUMP_REG(sdma_offset + reg);
515*b843c749SSergey Zigachev 
516*b843c749SSergey Zigachev 	WARN_ON_ONCE(i != HQD_N_REGS);
517*b843c749SSergey Zigachev 	*n_regs = i;
518*b843c749SSergey Zigachev 
519*b843c749SSergey Zigachev 	return 0;
520*b843c749SSergey Zigachev }
521*b843c749SSergey Zigachev 
kgd_hqd_is_occupied(struct kgd_dev * kgd,uint64_t queue_address,uint32_t pipe_id,uint32_t queue_id)522*b843c749SSergey Zigachev static bool kgd_hqd_is_occupied(struct kgd_dev *kgd, uint64_t queue_address,
523*b843c749SSergey Zigachev 				uint32_t pipe_id, uint32_t queue_id)
524*b843c749SSergey Zigachev {
525*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
526*b843c749SSergey Zigachev 	uint32_t act;
527*b843c749SSergey Zigachev 	bool retval = false;
528*b843c749SSergey Zigachev 	uint32_t low, high;
529*b843c749SSergey Zigachev 
530*b843c749SSergey Zigachev 	acquire_queue(kgd, pipe_id, queue_id);
531*b843c749SSergey Zigachev 	act = RREG32(mmCP_HQD_ACTIVE);
532*b843c749SSergey Zigachev 	if (act) {
533*b843c749SSergey Zigachev 		low = lower_32_bits(queue_address >> 8);
534*b843c749SSergey Zigachev 		high = upper_32_bits(queue_address >> 8);
535*b843c749SSergey Zigachev 
536*b843c749SSergey Zigachev 		if (low == RREG32(mmCP_HQD_PQ_BASE) &&
537*b843c749SSergey Zigachev 				high == RREG32(mmCP_HQD_PQ_BASE_HI))
538*b843c749SSergey Zigachev 			retval = true;
539*b843c749SSergey Zigachev 	}
540*b843c749SSergey Zigachev 	release_queue(kgd);
541*b843c749SSergey Zigachev 	return retval;
542*b843c749SSergey Zigachev }
543*b843c749SSergey Zigachev 
kgd_hqd_sdma_is_occupied(struct kgd_dev * kgd,void * mqd)544*b843c749SSergey Zigachev static bool kgd_hqd_sdma_is_occupied(struct kgd_dev *kgd, void *mqd)
545*b843c749SSergey Zigachev {
546*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
547*b843c749SSergey Zigachev 	struct vi_sdma_mqd *m;
548*b843c749SSergey Zigachev 	uint32_t sdma_base_addr;
549*b843c749SSergey Zigachev 	uint32_t sdma_rlc_rb_cntl;
550*b843c749SSergey Zigachev 
551*b843c749SSergey Zigachev 	m = get_sdma_mqd(mqd);
552*b843c749SSergey Zigachev 	sdma_base_addr = get_sdma_base_addr(m);
553*b843c749SSergey Zigachev 
554*b843c749SSergey Zigachev 	sdma_rlc_rb_cntl = RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL);
555*b843c749SSergey Zigachev 
556*b843c749SSergey Zigachev 	if (sdma_rlc_rb_cntl & SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK)
557*b843c749SSergey Zigachev 		return true;
558*b843c749SSergey Zigachev 
559*b843c749SSergey Zigachev 	return false;
560*b843c749SSergey Zigachev }
561*b843c749SSergey Zigachev 
kgd_hqd_destroy(struct kgd_dev * kgd,void * mqd,enum kfd_preempt_type reset_type,unsigned int utimeout,uint32_t pipe_id,uint32_t queue_id)562*b843c749SSergey Zigachev static int kgd_hqd_destroy(struct kgd_dev *kgd, void *mqd,
563*b843c749SSergey Zigachev 				enum kfd_preempt_type reset_type,
564*b843c749SSergey Zigachev 				unsigned int utimeout, uint32_t pipe_id,
565*b843c749SSergey Zigachev 				uint32_t queue_id)
566*b843c749SSergey Zigachev {
567*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
568*b843c749SSergey Zigachev 	uint32_t temp;
569*b843c749SSergey Zigachev 	enum hqd_dequeue_request_type type;
570*b843c749SSergey Zigachev 	unsigned long flags, end_jiffies;
571*b843c749SSergey Zigachev 	int retry;
572*b843c749SSergey Zigachev 	struct vi_mqd *m = get_mqd(mqd);
573*b843c749SSergey Zigachev 
574*b843c749SSergey Zigachev 	if (adev->in_gpu_reset)
575*b843c749SSergey Zigachev 		return -EIO;
576*b843c749SSergey Zigachev 
577*b843c749SSergey Zigachev 	acquire_queue(kgd, pipe_id, queue_id);
578*b843c749SSergey Zigachev 
579*b843c749SSergey Zigachev 	if (m->cp_hqd_vmid == 0)
580*b843c749SSergey Zigachev 		WREG32_FIELD(RLC_CP_SCHEDULERS, scheduler1, 0);
581*b843c749SSergey Zigachev 
582*b843c749SSergey Zigachev 	switch (reset_type) {
583*b843c749SSergey Zigachev 	case KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN:
584*b843c749SSergey Zigachev 		type = DRAIN_PIPE;
585*b843c749SSergey Zigachev 		break;
586*b843c749SSergey Zigachev 	case KFD_PREEMPT_TYPE_WAVEFRONT_RESET:
587*b843c749SSergey Zigachev 		type = RESET_WAVES;
588*b843c749SSergey Zigachev 		break;
589*b843c749SSergey Zigachev 	default:
590*b843c749SSergey Zigachev 		type = DRAIN_PIPE;
591*b843c749SSergey Zigachev 		break;
592*b843c749SSergey Zigachev 	}
593*b843c749SSergey Zigachev 
594*b843c749SSergey Zigachev 	/* Workaround: If IQ timer is active and the wait time is close to or
595*b843c749SSergey Zigachev 	 * equal to 0, dequeueing is not safe. Wait until either the wait time
596*b843c749SSergey Zigachev 	 * is larger or timer is cleared. Also, ensure that IQ_REQ_PEND is
597*b843c749SSergey Zigachev 	 * cleared before continuing. Also, ensure wait times are set to at
598*b843c749SSergey Zigachev 	 * least 0x3.
599*b843c749SSergey Zigachev 	 */
600*b843c749SSergey Zigachev 	local_irq_save(flags);
601*b843c749SSergey Zigachev 	preempt_disable();
602*b843c749SSergey Zigachev 	retry = 5000; /* wait for 500 usecs at maximum */
603*b843c749SSergey Zigachev 	while (true) {
604*b843c749SSergey Zigachev 		temp = RREG32(mmCP_HQD_IQ_TIMER);
605*b843c749SSergey Zigachev 		if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, PROCESSING_IQ)) {
606*b843c749SSergey Zigachev 			pr_debug("HW is processing IQ\n");
607*b843c749SSergey Zigachev 			goto loop;
608*b843c749SSergey Zigachev 		}
609*b843c749SSergey Zigachev 		if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, ACTIVE)) {
610*b843c749SSergey Zigachev 			if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, RETRY_TYPE)
611*b843c749SSergey Zigachev 					== 3) /* SEM-rearm is safe */
612*b843c749SSergey Zigachev 				break;
613*b843c749SSergey Zigachev 			/* Wait time 3 is safe for CP, but our MMIO read/write
614*b843c749SSergey Zigachev 			 * time is close to 1 microsecond, so check for 10 to
615*b843c749SSergey Zigachev 			 * leave more buffer room
616*b843c749SSergey Zigachev 			 */
617*b843c749SSergey Zigachev 			if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, WAIT_TIME)
618*b843c749SSergey Zigachev 					>= 10)
619*b843c749SSergey Zigachev 				break;
620*b843c749SSergey Zigachev 			pr_debug("IQ timer is active\n");
621*b843c749SSergey Zigachev 		} else
622*b843c749SSergey Zigachev 			break;
623*b843c749SSergey Zigachev loop:
624*b843c749SSergey Zigachev 		if (!retry) {
625*b843c749SSergey Zigachev 			pr_err("CP HQD IQ timer status time out\n");
626*b843c749SSergey Zigachev 			break;
627*b843c749SSergey Zigachev 		}
628*b843c749SSergey Zigachev 		ndelay(100);
629*b843c749SSergey Zigachev 		--retry;
630*b843c749SSergey Zigachev 	}
631*b843c749SSergey Zigachev 	retry = 1000;
632*b843c749SSergey Zigachev 	while (true) {
633*b843c749SSergey Zigachev 		temp = RREG32(mmCP_HQD_DEQUEUE_REQUEST);
634*b843c749SSergey Zigachev 		if (!(temp & CP_HQD_DEQUEUE_REQUEST__IQ_REQ_PEND_MASK))
635*b843c749SSergey Zigachev 			break;
636*b843c749SSergey Zigachev 		pr_debug("Dequeue request is pending\n");
637*b843c749SSergey Zigachev 
638*b843c749SSergey Zigachev 		if (!retry) {
639*b843c749SSergey Zigachev 			pr_err("CP HQD dequeue request time out\n");
640*b843c749SSergey Zigachev 			break;
641*b843c749SSergey Zigachev 		}
642*b843c749SSergey Zigachev 		ndelay(100);
643*b843c749SSergey Zigachev 		--retry;
644*b843c749SSergey Zigachev 	}
645*b843c749SSergey Zigachev 	local_irq_restore(flags);
646*b843c749SSergey Zigachev 	preempt_enable();
647*b843c749SSergey Zigachev 
648*b843c749SSergey Zigachev 	WREG32(mmCP_HQD_DEQUEUE_REQUEST, type);
649*b843c749SSergey Zigachev 
650*b843c749SSergey Zigachev 	end_jiffies = (utimeout * HZ / 1000) + jiffies;
651*b843c749SSergey Zigachev 	while (true) {
652*b843c749SSergey Zigachev 		temp = RREG32(mmCP_HQD_ACTIVE);
653*b843c749SSergey Zigachev 		if (!(temp & CP_HQD_ACTIVE__ACTIVE_MASK))
654*b843c749SSergey Zigachev 			break;
655*b843c749SSergey Zigachev 		if (time_after(jiffies, end_jiffies)) {
656*b843c749SSergey Zigachev 			pr_err("cp queue preemption time out.\n");
657*b843c749SSergey Zigachev 			release_queue(kgd);
658*b843c749SSergey Zigachev 			return -ETIME;
659*b843c749SSergey Zigachev 		}
660*b843c749SSergey Zigachev 		usleep_range(500, 1000);
661*b843c749SSergey Zigachev 	}
662*b843c749SSergey Zigachev 
663*b843c749SSergey Zigachev 	release_queue(kgd);
664*b843c749SSergey Zigachev 	return 0;
665*b843c749SSergey Zigachev }
666*b843c749SSergey Zigachev 
kgd_hqd_sdma_destroy(struct kgd_dev * kgd,void * mqd,unsigned int utimeout)667*b843c749SSergey Zigachev static int kgd_hqd_sdma_destroy(struct kgd_dev *kgd, void *mqd,
668*b843c749SSergey Zigachev 				unsigned int utimeout)
669*b843c749SSergey Zigachev {
670*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
671*b843c749SSergey Zigachev 	struct vi_sdma_mqd *m;
672*b843c749SSergey Zigachev 	uint32_t sdma_base_addr;
673*b843c749SSergey Zigachev 	uint32_t temp;
674*b843c749SSergey Zigachev 	unsigned long end_jiffies = (utimeout * HZ / 1000) + jiffies;
675*b843c749SSergey Zigachev 
676*b843c749SSergey Zigachev 	m = get_sdma_mqd(mqd);
677*b843c749SSergey Zigachev 	sdma_base_addr = get_sdma_base_addr(m);
678*b843c749SSergey Zigachev 
679*b843c749SSergey Zigachev 	temp = RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL);
680*b843c749SSergey Zigachev 	temp = temp & ~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK;
681*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL, temp);
682*b843c749SSergey Zigachev 
683*b843c749SSergey Zigachev 	while (true) {
684*b843c749SSergey Zigachev 		temp = RREG32(sdma_base_addr + mmSDMA0_RLC0_CONTEXT_STATUS);
685*b843c749SSergey Zigachev 		if (temp & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
686*b843c749SSergey Zigachev 			break;
687*b843c749SSergey Zigachev 		if (time_after(jiffies, end_jiffies))
688*b843c749SSergey Zigachev 			return -ETIME;
689*b843c749SSergey Zigachev 		usleep_range(500, 1000);
690*b843c749SSergey Zigachev 	}
691*b843c749SSergey Zigachev 
692*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_DOORBELL, 0);
693*b843c749SSergey Zigachev 	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL,
694*b843c749SSergey Zigachev 		RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL) |
695*b843c749SSergey Zigachev 		SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK);
696*b843c749SSergey Zigachev 
697*b843c749SSergey Zigachev 	m->sdmax_rlcx_rb_rptr = RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR);
698*b843c749SSergey Zigachev 
699*b843c749SSergey Zigachev 	return 0;
700*b843c749SSergey Zigachev }
701*b843c749SSergey Zigachev 
get_atc_vmid_pasid_mapping_valid(struct kgd_dev * kgd,uint8_t vmid)702*b843c749SSergey Zigachev static bool get_atc_vmid_pasid_mapping_valid(struct kgd_dev *kgd,
703*b843c749SSergey Zigachev 							uint8_t vmid)
704*b843c749SSergey Zigachev {
705*b843c749SSergey Zigachev 	uint32_t reg;
706*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
707*b843c749SSergey Zigachev 
708*b843c749SSergey Zigachev 	reg = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
709*b843c749SSergey Zigachev 	return reg & ATC_VMID0_PASID_MAPPING__VALID_MASK;
710*b843c749SSergey Zigachev }
711*b843c749SSergey Zigachev 
get_atc_vmid_pasid_mapping_pasid(struct kgd_dev * kgd,uint8_t vmid)712*b843c749SSergey Zigachev static uint16_t get_atc_vmid_pasid_mapping_pasid(struct kgd_dev *kgd,
713*b843c749SSergey Zigachev 								uint8_t vmid)
714*b843c749SSergey Zigachev {
715*b843c749SSergey Zigachev 	uint32_t reg;
716*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
717*b843c749SSergey Zigachev 
718*b843c749SSergey Zigachev 	reg = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
719*b843c749SSergey Zigachev 	return reg & ATC_VMID0_PASID_MAPPING__PASID_MASK;
720*b843c749SSergey Zigachev }
721*b843c749SSergey Zigachev 
kgd_address_watch_disable(struct kgd_dev * kgd)722*b843c749SSergey Zigachev static int kgd_address_watch_disable(struct kgd_dev *kgd)
723*b843c749SSergey Zigachev {
724*b843c749SSergey Zigachev 	return 0;
725*b843c749SSergey Zigachev }
726*b843c749SSergey Zigachev 
kgd_address_watch_execute(struct kgd_dev * kgd,unsigned int watch_point_id,uint32_t cntl_val,uint32_t addr_hi,uint32_t addr_lo)727*b843c749SSergey Zigachev static int kgd_address_watch_execute(struct kgd_dev *kgd,
728*b843c749SSergey Zigachev 					unsigned int watch_point_id,
729*b843c749SSergey Zigachev 					uint32_t cntl_val,
730*b843c749SSergey Zigachev 					uint32_t addr_hi,
731*b843c749SSergey Zigachev 					uint32_t addr_lo)
732*b843c749SSergey Zigachev {
733*b843c749SSergey Zigachev 	return 0;
734*b843c749SSergey Zigachev }
735*b843c749SSergey Zigachev 
kgd_wave_control_execute(struct kgd_dev * kgd,uint32_t gfx_index_val,uint32_t sq_cmd)736*b843c749SSergey Zigachev static int kgd_wave_control_execute(struct kgd_dev *kgd,
737*b843c749SSergey Zigachev 					uint32_t gfx_index_val,
738*b843c749SSergey Zigachev 					uint32_t sq_cmd)
739*b843c749SSergey Zigachev {
740*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
741*b843c749SSergey Zigachev 	uint32_t data = 0;
742*b843c749SSergey Zigachev 
743*b843c749SSergey Zigachev 	mutex_lock(&adev->grbm_idx_mutex);
744*b843c749SSergey Zigachev 
745*b843c749SSergey Zigachev 	WREG32(mmGRBM_GFX_INDEX, gfx_index_val);
746*b843c749SSergey Zigachev 	WREG32(mmSQ_CMD, sq_cmd);
747*b843c749SSergey Zigachev 
748*b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
749*b843c749SSergey Zigachev 		INSTANCE_BROADCAST_WRITES, 1);
750*b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
751*b843c749SSergey Zigachev 		SH_BROADCAST_WRITES, 1);
752*b843c749SSergey Zigachev 	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
753*b843c749SSergey Zigachev 		SE_BROADCAST_WRITES, 1);
754*b843c749SSergey Zigachev 
755*b843c749SSergey Zigachev 	WREG32(mmGRBM_GFX_INDEX, data);
756*b843c749SSergey Zigachev 	mutex_unlock(&adev->grbm_idx_mutex);
757*b843c749SSergey Zigachev 
758*b843c749SSergey Zigachev 	return 0;
759*b843c749SSergey Zigachev }
760*b843c749SSergey Zigachev 
kgd_address_watch_get_offset(struct kgd_dev * kgd,unsigned int watch_point_id,unsigned int reg_offset)761*b843c749SSergey Zigachev static uint32_t kgd_address_watch_get_offset(struct kgd_dev *kgd,
762*b843c749SSergey Zigachev 					unsigned int watch_point_id,
763*b843c749SSergey Zigachev 					unsigned int reg_offset)
764*b843c749SSergey Zigachev {
765*b843c749SSergey Zigachev 	return 0;
766*b843c749SSergey Zigachev }
767*b843c749SSergey Zigachev 
set_scratch_backing_va(struct kgd_dev * kgd,uint64_t va,uint32_t vmid)768*b843c749SSergey Zigachev static void set_scratch_backing_va(struct kgd_dev *kgd,
769*b843c749SSergey Zigachev 					uint64_t va, uint32_t vmid)
770*b843c749SSergey Zigachev {
771*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
772*b843c749SSergey Zigachev 
773*b843c749SSergey Zigachev 	lock_srbm(kgd, 0, 0, 0, vmid);
774*b843c749SSergey Zigachev 	WREG32(mmSH_HIDDEN_PRIVATE_BASE_VMID, va);
775*b843c749SSergey Zigachev 	unlock_srbm(kgd);
776*b843c749SSergey Zigachev }
777*b843c749SSergey Zigachev 
get_fw_version(struct kgd_dev * kgd,enum kgd_engine_type type)778*b843c749SSergey Zigachev static uint16_t get_fw_version(struct kgd_dev *kgd, enum kgd_engine_type type)
779*b843c749SSergey Zigachev {
780*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
781*b843c749SSergey Zigachev 	const union amdgpu_firmware_header *hdr;
782*b843c749SSergey Zigachev 
783*b843c749SSergey Zigachev 	switch (type) {
784*b843c749SSergey Zigachev 	case KGD_ENGINE_PFP:
785*b843c749SSergey Zigachev 		hdr = (const union amdgpu_firmware_header *)
786*b843c749SSergey Zigachev 						adev->gfx.pfp_fw->data;
787*b843c749SSergey Zigachev 		break;
788*b843c749SSergey Zigachev 
789*b843c749SSergey Zigachev 	case KGD_ENGINE_ME:
790*b843c749SSergey Zigachev 		hdr = (const union amdgpu_firmware_header *)
791*b843c749SSergey Zigachev 						adev->gfx.me_fw->data;
792*b843c749SSergey Zigachev 		break;
793*b843c749SSergey Zigachev 
794*b843c749SSergey Zigachev 	case KGD_ENGINE_CE:
795*b843c749SSergey Zigachev 		hdr = (const union amdgpu_firmware_header *)
796*b843c749SSergey Zigachev 						adev->gfx.ce_fw->data;
797*b843c749SSergey Zigachev 		break;
798*b843c749SSergey Zigachev 
799*b843c749SSergey Zigachev 	case KGD_ENGINE_MEC1:
800*b843c749SSergey Zigachev 		hdr = (const union amdgpu_firmware_header *)
801*b843c749SSergey Zigachev 						adev->gfx.mec_fw->data;
802*b843c749SSergey Zigachev 		break;
803*b843c749SSergey Zigachev 
804*b843c749SSergey Zigachev 	case KGD_ENGINE_MEC2:
805*b843c749SSergey Zigachev 		hdr = (const union amdgpu_firmware_header *)
806*b843c749SSergey Zigachev 						adev->gfx.mec2_fw->data;
807*b843c749SSergey Zigachev 		break;
808*b843c749SSergey Zigachev 
809*b843c749SSergey Zigachev 	case KGD_ENGINE_RLC:
810*b843c749SSergey Zigachev 		hdr = (const union amdgpu_firmware_header *)
811*b843c749SSergey Zigachev 						adev->gfx.rlc_fw->data;
812*b843c749SSergey Zigachev 		break;
813*b843c749SSergey Zigachev 
814*b843c749SSergey Zigachev 	case KGD_ENGINE_SDMA1:
815*b843c749SSergey Zigachev 		hdr = (const union amdgpu_firmware_header *)
816*b843c749SSergey Zigachev 						adev->sdma.instance[0].fw->data;
817*b843c749SSergey Zigachev 		break;
818*b843c749SSergey Zigachev 
819*b843c749SSergey Zigachev 	case KGD_ENGINE_SDMA2:
820*b843c749SSergey Zigachev 		hdr = (const union amdgpu_firmware_header *)
821*b843c749SSergey Zigachev 						adev->sdma.instance[1].fw->data;
822*b843c749SSergey Zigachev 		break;
823*b843c749SSergey Zigachev 
824*b843c749SSergey Zigachev 	default:
825*b843c749SSergey Zigachev 		return 0;
826*b843c749SSergey Zigachev 	}
827*b843c749SSergey Zigachev 
828*b843c749SSergey Zigachev 	if (hdr == NULL)
829*b843c749SSergey Zigachev 		return 0;
830*b843c749SSergey Zigachev 
831*b843c749SSergey Zigachev 	/* Only 12 bit in use*/
832*b843c749SSergey Zigachev 	return hdr->common.ucode_version;
833*b843c749SSergey Zigachev }
834*b843c749SSergey Zigachev 
set_vm_context_page_table_base(struct kgd_dev * kgd,uint32_t vmid,uint32_t page_table_base)835*b843c749SSergey Zigachev static void set_vm_context_page_table_base(struct kgd_dev *kgd, uint32_t vmid,
836*b843c749SSergey Zigachev 		uint32_t page_table_base)
837*b843c749SSergey Zigachev {
838*b843c749SSergey Zigachev 	struct amdgpu_device *adev = get_amdgpu_device(kgd);
839*b843c749SSergey Zigachev 
840*b843c749SSergey Zigachev 	if (!amdgpu_amdkfd_is_kfd_vmid(adev, vmid)) {
841*b843c749SSergey Zigachev 		pr_err("trying to set page table base for wrong VMID\n");
842*b843c749SSergey Zigachev 		return;
843*b843c749SSergey Zigachev 	}
844*b843c749SSergey Zigachev 	WREG32(mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + vmid - 8, page_table_base);
845*b843c749SSergey Zigachev }
846*b843c749SSergey Zigachev 
invalidate_tlbs(struct kgd_dev * kgd,uint16_t pasid)847*b843c749SSergey Zigachev static int invalidate_tlbs(struct kgd_dev *kgd, uint16_t pasid)
848*b843c749SSergey Zigachev {
849*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
850*b843c749SSergey Zigachev 	int vmid;
851*b843c749SSergey Zigachev 	unsigned int tmp;
852*b843c749SSergey Zigachev 
853*b843c749SSergey Zigachev 	if (adev->in_gpu_reset)
854*b843c749SSergey Zigachev 		return -EIO;
855*b843c749SSergey Zigachev 
856*b843c749SSergey Zigachev 	for (vmid = 0; vmid < 16; vmid++) {
857*b843c749SSergey Zigachev 		if (!amdgpu_amdkfd_is_kfd_vmid(adev, vmid))
858*b843c749SSergey Zigachev 			continue;
859*b843c749SSergey Zigachev 
860*b843c749SSergey Zigachev 		tmp = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
861*b843c749SSergey Zigachev 		if ((tmp & ATC_VMID0_PASID_MAPPING__VALID_MASK) &&
862*b843c749SSergey Zigachev 			(tmp & ATC_VMID0_PASID_MAPPING__PASID_MASK) == pasid) {
863*b843c749SSergey Zigachev 			WREG32(mmVM_INVALIDATE_REQUEST, 1 << vmid);
864*b843c749SSergey Zigachev 			RREG32(mmVM_INVALIDATE_RESPONSE);
865*b843c749SSergey Zigachev 			break;
866*b843c749SSergey Zigachev 		}
867*b843c749SSergey Zigachev 	}
868*b843c749SSergey Zigachev 
869*b843c749SSergey Zigachev 	return 0;
870*b843c749SSergey Zigachev }
871*b843c749SSergey Zigachev 
invalidate_tlbs_vmid(struct kgd_dev * kgd,uint16_t vmid)872*b843c749SSergey Zigachev static int invalidate_tlbs_vmid(struct kgd_dev *kgd, uint16_t vmid)
873*b843c749SSergey Zigachev {
874*b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
875*b843c749SSergey Zigachev 
876*b843c749SSergey Zigachev 	if (!amdgpu_amdkfd_is_kfd_vmid(adev, vmid)) {
877*b843c749SSergey Zigachev 		pr_err("non kfd vmid %d\n", vmid);
878*b843c749SSergey Zigachev 		return -EINVAL;
879*b843c749SSergey Zigachev 	}
880*b843c749SSergey Zigachev 
881*b843c749SSergey Zigachev 	WREG32(mmVM_INVALIDATE_REQUEST, 1 << vmid);
882*b843c749SSergey Zigachev 	RREG32(mmVM_INVALIDATE_RESPONSE);
883*b843c749SSergey Zigachev 	return 0;
884*b843c749SSergey Zigachev }
885