xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef __AMDGPU_MES_H__
25 #define __AMDGPU_MES_H__
26 
27 #include "amdgpu_irq.h"
28 #include "kgd_kfd_interface.h"
29 #include "amdgpu_gfx.h"
30 #include "amdgpu_doorbell.h"
31 #include <linux/sched/mm.h>
32 #include <linux/workqueue.h>
33 
34 #define AMDGPU_MES_MAX_COMPUTE_PIPES        8
35 #define AMDGPU_MES_MAX_GFX_PIPES            2
36 #define AMDGPU_MES_MAX_SDMA_PIPES           2
37 
38 #define AMDGPU_MES_API_VERSION_SHIFT	12
39 #define AMDGPU_MES_FEAT_VERSION_SHIFT	24
40 
41 #define AMDGPU_MES_VERSION_MASK		0x00000fff
42 #define AMDGPU_MES_API_VERSION_MASK	0x00fff000
43 #define AMDGPU_MES_FEAT_VERSION_MASK	0xff000000
44 #define AMDGPU_MES_MSCRATCH_SIZE	0x40000
45 #define AMDGPU_MES_INVALID_DB_OFFSET	0xffffffff
46 
47 enum amdgpu_mes_priority_level {
48 	AMDGPU_MES_PRIORITY_LEVEL_LOW       = 0,
49 	AMDGPU_MES_PRIORITY_LEVEL_NORMAL    = 1,
50 	AMDGPU_MES_PRIORITY_LEVEL_MEDIUM    = 2,
51 	AMDGPU_MES_PRIORITY_LEVEL_HIGH      = 3,
52 	AMDGPU_MES_PRIORITY_LEVEL_REALTIME  = 4,
53 	AMDGPU_MES_PRIORITY_NUM_LEVELS
54 };
55 
56 #define AMDGPU_MES_PROC_CTX_SIZE 0x1000 /* one page area */
57 #define AMDGPU_MES_GANG_CTX_SIZE 0x1000 /* one page area */
58 #define AMDGPU_MES_PROC_CTX_ARRAY_MAX  128
59 #define AMDGPU_MES_GANG_CTX_ARRAY_MAX  512
60 struct amdgpu_mes_funcs;
61 
62 enum amdgpu_mes_pipe {
63 	AMDGPU_MES_PIPE_0 = 0,
64 	AMDGPU_MES_PIPE_1,
65 	AMDGPU_MAX_MES_PIPES = 2,
66 };
67 
68 #define AMDGPU_MES_SCHED_PIPE AMDGPU_MES_PIPE_0
69 #define AMDGPU_MES_KIQ_PIPE AMDGPU_MES_PIPE_1
70 
71 #define AMDGPU_MAX_MES_INST_PIPES \
72 	(AMDGPU_MAX_MES_PIPES * AMDGPU_MAX_GC_INSTANCES)
73 
74 #define MES_PIPE_INST(xcc_id, pipe_id) \
75 	(xcc_id * AMDGPU_MAX_MES_PIPES + pipe_id)
76 
77 struct amdgpu_mes {
78 	struct amdgpu_device            *adev;
79 
80 	struct mutex                    mutex_hidden;
81 
82 	struct ida                      doorbell_ida;
83 
84 	spinlock_t                      queue_id_lock;
85 
86 	uint32_t			sched_version;
87 	uint32_t			kiq_version;
88 	uint32_t			fw_version[AMDGPU_MAX_MES_PIPES];
89 	bool                            enable_legacy_queue_map;
90 
91 	uint32_t                        total_max_queue;
92 	uint32_t                        max_doorbell_slices;
93 
94 	/* GFX11 usermode queue oversubscription notify timer */
95 	atomic_t                        userq_hw_queue_count;
96 	struct delayed_work             userq_notify_unmap_work;
97 
98 	uint64_t                        default_process_quantum;
99 	uint64_t                        default_gang_quantum;
100 
101 	struct amdgpu_ring              ring[AMDGPU_MAX_MES_INST_PIPES];
102 	spinlock_t                      ring_lock[AMDGPU_MAX_MES_INST_PIPES];
103 
104 	const struct firmware           *fw[AMDGPU_MAX_MES_PIPES];
105 
106 	/* mes ucode */
107 	struct amdgpu_bo		*ucode_fw_obj[AMDGPU_MAX_MES_INST_PIPES];
108 	uint64_t			ucode_fw_gpu_addr[AMDGPU_MAX_MES_INST_PIPES];
109 	uint32_t			*ucode_fw_ptr[AMDGPU_MAX_MES_INST_PIPES];
110 	uint64_t                        uc_start_addr[AMDGPU_MAX_MES_PIPES];
111 
112 	/* mes ucode data */
113 	struct amdgpu_bo		*data_fw_obj[AMDGPU_MAX_MES_INST_PIPES];
114 	uint64_t			data_fw_gpu_addr[AMDGPU_MAX_MES_INST_PIPES];
115 	uint32_t			*data_fw_ptr[AMDGPU_MAX_MES_INST_PIPES];
116 	uint64_t                        data_start_addr[AMDGPU_MAX_MES_PIPES];
117 
118 	/* eop gpu obj */
119 	struct amdgpu_bo		*eop_gpu_obj[AMDGPU_MAX_MES_INST_PIPES];
120 	uint64_t                        eop_gpu_addr[AMDGPU_MAX_MES_INST_PIPES];
121 
122 	void                            *mqd_backup[AMDGPU_MAX_MES_INST_PIPES];
123 	struct amdgpu_irq_src	        irq[AMDGPU_MAX_MES_INST_PIPES];
124 
125 	uint32_t                        vmid_mask_gfxhub;
126 	uint32_t                        vmid_mask_mmhub;
127 	uint32_t                        gfx_hqd_mask[AMDGPU_MES_MAX_GFX_PIPES];
128 	uint32_t                        compute_hqd_mask[AMDGPU_MES_MAX_COMPUTE_PIPES];
129 	uint32_t                        sdma_hqd_mask[AMDGPU_MES_MAX_SDMA_PIPES];
130 	uint32_t                        aggregated_doorbells[AMDGPU_MES_PRIORITY_NUM_LEVELS];
131 
132 	uint32_t                        sch_ctx_offs[AMDGPU_MAX_MES_INST_PIPES];
133 	uint64_t                        sch_ctx_gpu_addr[AMDGPU_MAX_MES_INST_PIPES];
134 	uint64_t                        *sch_ctx_ptr[AMDGPU_MAX_MES_INST_PIPES];
135 	uint32_t                        query_status_fence_offs[AMDGPU_MAX_MES_INST_PIPES];
136 	uint64_t                        query_status_fence_gpu_addr[AMDGPU_MAX_MES_INST_PIPES];
137 	uint64_t                        *query_status_fence_ptr[AMDGPU_MAX_MES_INST_PIPES];
138 
139 	uint32_t			saved_flags;
140 
141 	/* initialize kiq pipe */
142 	int                             (*kiq_hw_init)(struct amdgpu_device *adev,
143                                                    uint32_t xcc_id);
144 	int                             (*kiq_hw_fini)(struct amdgpu_device *adev,
145                                                    uint32_t xcc_id);
146 
147 	/* MES doorbells */
148 	uint32_t			db_start_dw_offset;
149 	uint32_t			num_mes_dbs;
150 	unsigned long			*doorbell_bitmap;
151 
152 	/* MES event log buffer */
153 	uint32_t			event_log_size;
154 	struct amdgpu_bo	*event_log_gpu_obj;
155 	uint64_t			event_log_gpu_addr;
156 	void				*event_log_cpu_addr;
157 
158 	/* ip specific functions */
159 	const struct amdgpu_mes_funcs   *funcs;
160 
161 	/* mes resource_1 bo*/
162 	struct amdgpu_bo    *resource_1[AMDGPU_MAX_MES_PIPES];
163 	uint64_t            resource_1_gpu_addr[AMDGPU_MAX_MES_PIPES];
164 	void                *resource_1_addr[AMDGPU_MAX_MES_PIPES];
165 
166 	int				hung_queue_db_array_size;
167 	int				hung_queue_hqd_info_offset;
168 	struct amdgpu_bo		*hung_queue_db_array_gpu_obj[AMDGPU_MAX_MES_INST_PIPES];
169 	uint64_t			hung_queue_db_array_gpu_addr[AMDGPU_MAX_MES_INST_PIPES];
170 	void				*hung_queue_db_array_cpu_addr[AMDGPU_MAX_MES_INST_PIPES];
171 
172 	/* cooperative dispatch */
173 	bool                enable_coop_mode;
174 	int                 master_xcc_ids[AMDGPU_MAX_MES_INST_PIPES];
175 	struct amdgpu_bo    *shared_cmd_buf_obj[AMDGPU_MAX_MES_INST_PIPES];
176 	uint64_t            shared_cmd_buf_gpu_addr[AMDGPU_MAX_MES_INST_PIPES];
177 
178 	bool			compute_pipe_reset_enabled;
179 	bool			gfx_pipe_reset_enabled;
180 
181 	bool				use_rs64mem;
182 	struct amdgpu_bo		*ctx_array_size_bo;
183 	uint64_t			ctx_array_size_gpu_addr;
184 	uint32_t			*ctx_array_size_cpu_ptr;
185 
186 	uint32_t			proc_ctx_array_size;
187 	unsigned long			*proc_ctx_bitmap;
188 	uint32_t			gang_ctx_array_size;
189 	uint32_t			gang_ctx_array_index;
190 	unsigned long			*gang_ctx_bitmap;
191 };
192 
193 struct amdgpu_mes_hung_queue_hqd_info {
194 	union {
195 		struct {
196 			u32 queue_type: 3; // queue type
197 			u32 pipe_index: 4; // pipe index
198 			u32 queue_index: 8; // queue index
199 			u32 reserved: 17;
200 		};
201 
202 		u32 bit0_31;
203 	};
204 };
205 
206 struct amdgpu_mes_gang {
207 	int 				gang_id;
208 	int 				priority;
209 	int 				inprocess_gang_priority;
210 	int 				global_priority_level;
211 	struct list_head 		list;
212 	struct amdgpu_mes_process 	*process;
213 	struct amdgpu_bo 		*gang_ctx_bo;
214 	uint64_t 			gang_ctx_gpu_addr;
215 	void 				*gang_ctx_cpu_ptr;
216 	uint64_t 			gang_quantum;
217 	struct list_head 		queue_list;
218 };
219 
220 struct amdgpu_mes_queue {
221 	struct list_head 		list;
222 	struct amdgpu_mes_gang 		*gang;
223 	int 				queue_id;
224 	uint64_t 			doorbell_off;
225 	struct amdgpu_bo		*mqd_obj;
226 	void				*mqd_cpu_ptr;
227 	uint64_t 			mqd_gpu_addr;
228 	uint64_t 			wptr_gpu_addr;
229 	int 				queue_type;
230 	int 				paging;
231 	struct amdgpu_ring 		*ring;
232 };
233 
234 struct amdgpu_mes_queue_properties {
235 	int 			queue_type;
236 	uint64_t                hqd_base_gpu_addr;
237 	uint64_t                rptr_gpu_addr;
238 	uint64_t                wptr_gpu_addr;
239 	uint64_t                wptr_mc_addr;
240 	uint32_t                queue_size;
241 	uint64_t                eop_gpu_addr;
242 	uint32_t                hqd_pipe_priority;
243 	uint32_t                hqd_queue_priority;
244 	bool 			paging;
245 	struct amdgpu_ring 	*ring;
246 	/* out */
247 	uint64_t       		doorbell_off;
248 };
249 
250 struct amdgpu_mes_gang_properties {
251 	uint32_t 	priority;
252 	uint32_t 	gang_quantum;
253 	uint32_t 	inprocess_gang_priority;
254 	uint32_t 	priority_level;
255 	int 		global_priority_level;
256 };
257 
258 struct mes_add_queue_input {
259 	uint32_t        xcc_id;
260 	uint32_t	process_id;
261 	uint64_t	page_table_base_addr;
262 	uint64_t	process_va_start;
263 	uint64_t	process_va_end;
264 	uint64_t	process_quantum;
265 	uint64_t	process_context_addr;
266 	uint64_t	gang_quantum;
267 	uint64_t	gang_context_addr;
268 	uint32_t	inprocess_gang_priority;
269 	uint32_t	gang_global_priority_level;
270 	uint32_t	doorbell_offset;
271 	uint64_t	mqd_addr;
272 	uint64_t	wptr_addr;
273 	uint64_t	wptr_mc_addr;
274 	uint32_t	queue_type;
275 	uint32_t	paging;
276 	uint32_t        gws_base;
277 	uint32_t        gws_size;
278 	uint64_t	tba_addr;
279 	uint64_t	tma_addr;
280 	uint32_t	trap_en;
281 	uint32_t	skip_process_ctx_clear;
282 	uint32_t	is_kfd_process;
283 	uint32_t	is_aql_queue;
284 	uint32_t	queue_size;
285 	uint32_t	exclusively_scheduled;
286 	uint32_t	sh_mem_config_data;
287 	uint32_t	vm_cntx_cntl;
288 	uint32_t	process_context_array_index;
289 	uint32_t	gang_context_array_index;
290 };
291 
292 struct mes_remove_queue_input {
293 	uint32_t        xcc_id;
294 	uint32_t	doorbell_offset;
295 	uint64_t	gang_context_addr;
296 	uint32_t	queue_type;
297 	bool		remove_queue_after_reset;
298 	uint32_t	gang_context_array_index;
299 };
300 
301 struct mes_map_legacy_queue_input {
302 	uint32_t			   xcc_id;
303 	uint32_t                           queue_type;
304 	uint32_t                           doorbell_offset;
305 	uint32_t                           pipe_id;
306 	uint32_t                           queue_id;
307 	uint64_t                           mqd_addr;
308 	uint64_t                           wptr_addr;
309 };
310 
311 struct mes_unmap_legacy_queue_input {
312 	uint32_t                           xcc_id;
313 	enum amdgpu_unmap_queues_action    action;
314 	uint32_t                           queue_type;
315 	uint32_t                           doorbell_offset;
316 	uint32_t                           pipe_id;
317 	uint32_t                           queue_id;
318 	uint64_t                           trail_fence_addr;
319 	uint64_t                           trail_fence_data;
320 };
321 
322 struct mes_suspend_gang_input {
323 	uint32_t        xcc_id;
324 	bool		suspend_all_gangs;
325 	bool		suspend_all_sdma_gangs;
326 	uint64_t	gang_context_addr;
327 	uint64_t	suspend_fence_addr;
328 	uint32_t	suspend_fence_value;
329 	uint32_t	doorbell_offset;
330 };
331 
332 struct mes_resume_gang_input {
333 	uint32_t	xcc_id;
334 	bool		resume_all_gangs;
335 	uint64_t	gang_context_addr;
336 	uint32_t	doorbell_offset;
337 };
338 
339 struct mes_reset_queue_input {
340 	uint32_t			   xcc_id;
341 	uint32_t                           queue_type;
342 	uint32_t                           doorbell_offset;
343 	bool                               use_mmio;
344 	uint32_t                           me_id;
345 	uint32_t                           pipe_id;
346 	uint32_t                           queue_id;
347 	uint64_t                           mqd_addr;
348 	uint64_t                           wptr_addr;
349 	uint32_t                           vmid;
350 	bool                               legacy_gfx;
351 	bool                               is_kq;
352 };
353 
354 struct mes_detect_and_reset_queue_input {
355 	u32	queue_type;
356 	bool		detect_only;
357 	u32	xcc_id;
358 };
359 
360 struct mes_inv_tlbs_pasid_input {
361 	uint32_t        xcc_id;
362 	uint16_t        pasid;
363 	uint8_t         hub_id;
364 	uint8_t         flush_type;
365 };
366 
367 enum mes_misc_opcode {
368 	MES_MISC_OP_WRITE_REG,
369 	MES_MISC_OP_READ_REG,
370 	MES_MISC_OP_WRM_REG_WAIT,
371 	MES_MISC_OP_WRM_REG_WR_WAIT,
372 	MES_MISC_OP_SET_SHADER_DEBUGGER,
373 	MES_MISC_OP_CHANGE_CONFIG,
374 	MES_MISC_OP_NOTIFY_WORK_ON_UNMAPPED_QUEUE
375 };
376 
377 struct mes_misc_op_input {
378 	uint32_t                 xcc_id;
379 	enum mes_misc_opcode     op;
380 
381 	union {
382 		struct {
383 			uint32_t                  reg_offset;
384 			uint64_t                  buffer_addr;
385 		} read_reg;
386 
387 		struct {
388 			uint32_t                  reg_offset;
389 			uint32_t                  reg_value;
390 		} write_reg;
391 
392 		struct {
393 			uint32_t                   ref;
394 			uint32_t                   mask;
395 			uint32_t                   reg0;
396 			uint32_t                   reg1;
397 		} wrm_reg;
398 
399 		struct {
400 			uint64_t process_context_addr;
401 			union {
402 				struct {
403 					uint32_t single_memop : 1;
404 					uint32_t single_alu_op : 1;
405 					uint32_t reserved: 29;
406 					uint32_t process_ctx_flush: 1;
407 				};
408 				uint32_t u32all;
409 			} flags;
410 			uint32_t spi_gdbg_per_vmid_cntl;
411 			uint32_t tcp_watch_cntl[4];
412 			uint32_t trap_en;
413 		} set_shader_debugger;
414 
415 		struct {
416 			union {
417 				struct {
418 					uint32_t limit_single_process : 1;
419 					uint32_t enable_hws_logging_buffer : 1;
420 					uint32_t reserved : 30;
421 				};
422 				uint32_t all;
423 			} option;
424 			struct {
425 				uint32_t tdr_level;
426 				uint32_t tdr_delay;
427 			} tdr_config;
428 		} change_config;
429 	};
430 };
431 
432 struct amdgpu_mes_funcs {
433 	int (*add_hw_queue)(struct amdgpu_mes *mes,
434 			    struct mes_add_queue_input *input);
435 
436 	int (*remove_hw_queue)(struct amdgpu_mes *mes,
437 			       struct mes_remove_queue_input *input);
438 
439 	int (*map_legacy_queue)(struct amdgpu_mes *mes,
440 				struct mes_map_legacy_queue_input *input);
441 
442 	int (*unmap_legacy_queue)(struct amdgpu_mes *mes,
443 				  struct mes_unmap_legacy_queue_input *input);
444 
445 	int (*suspend_gang)(struct amdgpu_mes *mes,
446 			    struct mes_suspend_gang_input *input);
447 
448 	int (*resume_gang)(struct amdgpu_mes *mes,
449 			   struct mes_resume_gang_input *input);
450 
451 	int (*misc_op)(struct amdgpu_mes *mes,
452 		       struct mes_misc_op_input *input);
453 
454 	int (*reset_hw_queue)(struct amdgpu_mes *mes,
455 			      struct mes_reset_queue_input *input);
456 
457 	int (*detect_and_reset_hung_queues)(struct amdgpu_mes *mes,
458 			      struct mes_detect_and_reset_queue_input *input);
459 
460 
461 	int (*invalidate_tlbs_pasid)(struct amdgpu_mes *mes,
462 			      struct mes_inv_tlbs_pasid_input *input);
463 };
464 
465 enum amdgpu_mqd_update_flag {
466 	AMDGPU_UPDATE_FLAG_DBG_WA_ENABLE = 1,
467 	AMDGPU_UPDATE_FLAG_DBG_WA_DISABLE = 2,
468 	AMDGPU_UPDATE_FLAG_IS_GWS = 4, /* quirk for gfx9 IP */
469 };
470 
471 struct amdgpu_mqd_prop {
472 	uint64_t mqd_gpu_addr;
473 	uint64_t hqd_base_gpu_addr;
474 	uint64_t rptr_gpu_addr;
475 	uint64_t wptr_gpu_addr;
476 	uint32_t queue_size;
477 	bool use_doorbell;
478 	uint32_t doorbell_index;
479 	uint64_t eop_gpu_addr;
480 	uint32_t hqd_pipe_priority;
481 	uint32_t hqd_queue_priority;
482 	uint32_t mqd_stride_size;
483 	bool allow_tunneling;
484 	bool hqd_active;
485 	uint64_t shadow_addr;
486 	uint64_t gds_bkup_addr;
487 	uint64_t csa_addr;
488 	uint64_t fence_address;
489 	bool tmz_queue;
490 	bool kernel_queue;
491 	uint32_t *cu_mask;
492 	uint32_t cu_mask_count;
493 	uint32_t cu_flags;
494 	bool is_user_cu_masked;
495 };
496 
497 struct amdgpu_mqd {
498 	unsigned mqd_size;
499 	int (*init_mqd)(struct amdgpu_device *adev, void *mqd,
500 			struct amdgpu_mqd_prop *p);
501 };
502 
503 /*
504  * MES FW uses address(mqd_addr + sizeof(struct mqd) + 3*sizeof(uint32_t))
505  * as fence address and writes a 32 bit fence value to this address.
506  * Driver needs to allocate at least 4 DWs extra memory in addition to
507  * sizeof(struct mqd). Add 8 DWs and align to AMDGPU_GPU_PAGE_SIZE for safety.
508  */
509 #define AMDGPU_MQD_SIZE_ALIGN(mqd_size) AMDGPU_GPU_PAGE_ALIGN(((mqd_size) + 32))
510 
511 #define amdgpu_mes_kiq_hw_init(adev, xcc_id) \
512 	(adev)->mes.kiq_hw_init((adev), (xcc_id))
513 #define amdgpu_mes_kiq_hw_fini(adev, xcc_id) \
514 	(adev)->mes.kiq_hw_fini((adev), (xcc_id))
515 
516 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe);
517 void amdgpu_mes_validate_fw_version(struct amdgpu_device *adev);
518 int amdgpu_mes_init(struct amdgpu_device *adev);
519 void amdgpu_mes_fini(struct amdgpu_device *adev);
520 
521 int amdgpu_mes_suspend(struct amdgpu_device *adev, u32 xcc_id);
522 int amdgpu_mes_resume(struct amdgpu_device *adev, u32 xcc_id);
523 
524 int amdgpu_mes_map_legacy_queue(struct amdgpu_device *adev,
525 				struct amdgpu_ring *ring, uint32_t xcc_id);
526 int amdgpu_mes_unmap_legacy_queue(struct amdgpu_device *adev,
527 				  struct amdgpu_ring *ring,
528 				  enum amdgpu_unmap_queues_action action,
529 				  u64 gpu_addr, u64 seq, uint32_t xcc_id);
530 int amdgpu_mes_reset_legacy_queue(struct amdgpu_device *adev,
531 				  struct amdgpu_ring *ring,
532 				  unsigned int vmid,
533 				  bool use_mmio,
534 				  uint32_t xcc_id);
535 int amdgpu_mes_reset_queue_mmio(struct amdgpu_device *adev,
536 				int queue_type,
537 				unsigned int vmid,
538 				unsigned int me,
539 				unsigned int pipe,
540 				unsigned int queue,
541 				uint32_t xcc_id);
542 int amdgpu_mes_reset_user_queue(struct amdgpu_device *adev,
543 				int queue_type,
544 				unsigned int doorbell_index,
545 				unsigned int xcc_id);
546 
547 int amdgpu_mes_get_hung_queue_db_array_size(struct amdgpu_device *adev);
548 int amdgpu_mes_detect_and_reset_hung_queues(struct amdgpu_device *adev,
549 					    int queue_type,
550 					    bool detect_only,
551 					    unsigned int *hung_db_num,
552 					    u32 *hung_db_array,
553 					    uint32_t xcc_id);
554 
555 uint32_t amdgpu_mes_rreg(struct amdgpu_device *adev, uint32_t reg,
556 			 uint32_t xcc_id);
557 int amdgpu_mes_wreg(struct amdgpu_device *adev,
558 		    uint32_t reg, uint32_t val, uint32_t xcc_id);
559 int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device *adev,
560 				  uint32_t reg0, uint32_t reg1,
561 				  uint32_t ref, uint32_t mask, uint32_t xcc_id);
562 int amdgpu_mes_hdp_flush(struct amdgpu_device *adev);
563 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
564 				uint64_t process_context_addr,
565 				uint32_t spi_gdbg_per_vmid_cntl,
566 				const uint32_t *tcp_watch_cntl,
567 				uint32_t flags,
568 				bool trap_en,
569 				uint32_t xcc_id);
570 int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
571 				uint64_t process_context_addr, uint32_t xcc_id);
572 
573 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device *adev,
574 						   enum amdgpu_mes_priority_level prio);
575 
576 int amdgpu_mes_doorbell_process_slice(struct amdgpu_device *adev);
577 
578 /*
579  * MES lock can be taken in MMU notifiers.
580  *
581  * A bit more detail about why to set no-FS reclaim with MES lock:
582  *
583  * The purpose of the MMU notifier is to stop GPU access to memory so
584  * that the Linux VM subsystem can move pages around safely. This is
585  * done by preempting user mode queues for the affected process. When
586  * MES is used, MES lock needs to be taken to preempt the queues.
587  *
588  * The MMU notifier callback entry point in the driver is
589  * amdgpu_mn_invalidate_range_start_hsa. The relevant call chain from
590  * there is:
591  * amdgpu_amdkfd_evict_userptr -> kgd2kfd_quiesce_mm ->
592  * kfd_process_evict_queues -> pdd->dev->dqm->ops.evict_process_queues
593  *
594  * The last part of the chain is a function pointer where we take the
595  * MES lock.
596  *
597  * The problem with taking locks in the MMU notifier is, that MMU
598  * notifiers can be called in reclaim-FS context. That's where the
599  * kernel frees up pages to make room for new page allocations under
600  * memory pressure. While we are running in reclaim-FS context, we must
601  * not trigger another memory reclaim operation because that would
602  * recursively reenter the reclaim code and cause a deadlock. The
603  * memalloc_nofs_save/restore calls guarantee that.
604  *
605  * In addition we also need to avoid lock dependencies on other locks taken
606  * under the MES lock, for example reservation locks. Here is a possible
607  * scenario of a deadlock:
608  * Thread A: takes and holds reservation lock | triggers reclaim-FS |
609  * MMU notifier | blocks trying to take MES lock
610  * Thread B: takes and holds MES lock | blocks trying to take reservation lock
611  *
612  * In this scenario Thread B gets involved in a deadlock even without
613  * triggering a reclaim-FS operation itself.
614  * To fix this and break the lock dependency chain you'd need to either:
615  * 1. protect reservation locks with memalloc_nofs_save/restore, or
616  * 2. avoid taking reservation locks under the MES lock.
617  *
618  * Reservation locks are taken all over the kernel in different subsystems, we
619  * have no control over them and their lock dependencies.So the only workable
620  * solution is to avoid taking other locks under the MES lock.
621  * As a result, make sure no reclaim-FS happens while holding this lock anywhere
622  * to prevent deadlocks when an MMU notifier runs in reclaim-FS context.
623  */
624 static inline void amdgpu_mes_lock(struct amdgpu_mes *mes)
625 {
626 	mutex_lock(&mes->mutex_hidden);
627 	mes->saved_flags = memalloc_noreclaim_save();
628 }
629 
630 static inline void amdgpu_mes_unlock(struct amdgpu_mes *mes)
631 {
632 	memalloc_noreclaim_restore(mes->saved_flags);
633 	mutex_unlock(&mes->mutex_hidden);
634 }
635 
636 bool amdgpu_mes_suspend_resume_all_supported(struct amdgpu_device *adev);
637 bool amdgpu_mes_queue_reset_by_mes_supported(struct amdgpu_device *adev);
638 
639 int amdgpu_mes_update_enforce_isolation(struct amdgpu_device *adev);
640 
641 int amdgpu_mes_rs64mem_init(struct amdgpu_mes *mes);
642 void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes);
643 int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes);
644 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
645 				    uint32_t *index);
646 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
647 				    uint32_t index);
648 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
649 				    uint32_t *index);
650 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
651 				    uint32_t index);
652 
653 int amdgpu_mes_notify_unmap_queue(struct amdgpu_device *adev);
654 void amdgpu_mes_userq_queue_mapped(struct amdgpu_device *adev);
655 void amdgpu_mes_userq_queue_unmapped(struct amdgpu_device *adev);
656 
657 #endif /* __AMDGPU_MES_H__ */
658