xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_reset.h (revision 1bb76ff151c0aba8e3312a604e4cd2e5195cf4b7)
15ca02815Sjsg /*
25ca02815Sjsg  * Copyright 2021 Advanced Micro Devices, Inc.
35ca02815Sjsg  *
45ca02815Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
55ca02815Sjsg  * copy of this software and associated documentation files (the "Software"),
65ca02815Sjsg  * to deal in the Software without restriction, including without limitation
75ca02815Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
85ca02815Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
95ca02815Sjsg  * Software is furnished to do so, subject to the following conditions:
105ca02815Sjsg  *
115ca02815Sjsg  * The above copyright notice and this permission notice shall be included in
125ca02815Sjsg  * all copies or substantial portions of the Software.
135ca02815Sjsg  *
145ca02815Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
155ca02815Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
165ca02815Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
175ca02815Sjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
185ca02815Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
195ca02815Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
205ca02815Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
215ca02815Sjsg  *
225ca02815Sjsg  */
235ca02815Sjsg 
245ca02815Sjsg #ifndef __AMDGPU_RESET_H__
255ca02815Sjsg #define __AMDGPU_RESET_H__
265ca02815Sjsg 
275ca02815Sjsg #include "amdgpu.h"
285ca02815Sjsg 
295ca02815Sjsg enum AMDGPU_RESET_FLAGS {
305ca02815Sjsg 
315ca02815Sjsg 	AMDGPU_NEED_FULL_RESET = 0,
325ca02815Sjsg 	AMDGPU_SKIP_HW_RESET = 1,
33*1bb76ff1Sjsg 	AMDGPU_RESET_FOR_DEVICE_REMOVE = 2,
345ca02815Sjsg };
355ca02815Sjsg 
365ca02815Sjsg struct amdgpu_reset_context {
375ca02815Sjsg 	enum amd_reset_method method;
385ca02815Sjsg 	struct amdgpu_device *reset_req_dev;
395ca02815Sjsg 	struct amdgpu_job *job;
405ca02815Sjsg 	struct amdgpu_hive_info *hive;
41*1bb76ff1Sjsg 	struct list_head *reset_device_list;
425ca02815Sjsg 	unsigned long flags;
435ca02815Sjsg };
445ca02815Sjsg 
455ca02815Sjsg struct amdgpu_reset_handler {
465ca02815Sjsg 	enum amd_reset_method reset_method;
475ca02815Sjsg 	struct list_head handler_list;
485ca02815Sjsg 	int (*prepare_env)(struct amdgpu_reset_control *reset_ctl,
495ca02815Sjsg 			   struct amdgpu_reset_context *context);
505ca02815Sjsg 	int (*prepare_hwcontext)(struct amdgpu_reset_control *reset_ctl,
515ca02815Sjsg 				 struct amdgpu_reset_context *context);
525ca02815Sjsg 	int (*perform_reset)(struct amdgpu_reset_control *reset_ctl,
535ca02815Sjsg 			     struct amdgpu_reset_context *context);
545ca02815Sjsg 	int (*restore_hwcontext)(struct amdgpu_reset_control *reset_ctl,
555ca02815Sjsg 				 struct amdgpu_reset_context *context);
565ca02815Sjsg 	int (*restore_env)(struct amdgpu_reset_control *reset_ctl,
575ca02815Sjsg 			   struct amdgpu_reset_context *context);
585ca02815Sjsg 
595ca02815Sjsg 	int (*do_reset)(struct amdgpu_device *adev);
605ca02815Sjsg };
615ca02815Sjsg 
625ca02815Sjsg struct amdgpu_reset_control {
635ca02815Sjsg 	void *handle;
645ca02815Sjsg 	struct work_struct reset_work;
655ca02815Sjsg 	struct rwlock reset_lock;
665ca02815Sjsg 	struct list_head reset_handlers;
675ca02815Sjsg 	atomic_t in_reset;
685ca02815Sjsg 	enum amd_reset_method active_reset;
695ca02815Sjsg 	struct amdgpu_reset_handler *(*get_reset_handler)(
705ca02815Sjsg 		struct amdgpu_reset_control *reset_ctl,
715ca02815Sjsg 		struct amdgpu_reset_context *context);
725ca02815Sjsg 	void (*async_reset)(struct work_struct *work);
735ca02815Sjsg };
745ca02815Sjsg 
75*1bb76ff1Sjsg 
76*1bb76ff1Sjsg enum amdgpu_reset_domain_type {
77*1bb76ff1Sjsg 	SINGLE_DEVICE,
78*1bb76ff1Sjsg 	XGMI_HIVE
79*1bb76ff1Sjsg };
80*1bb76ff1Sjsg 
81*1bb76ff1Sjsg struct amdgpu_reset_domain {
82*1bb76ff1Sjsg 	struct kref refcount;
83*1bb76ff1Sjsg 	struct workqueue_struct *wq;
84*1bb76ff1Sjsg 	enum amdgpu_reset_domain_type type;
85*1bb76ff1Sjsg 	struct rwlock sem;
86*1bb76ff1Sjsg 	atomic_t in_gpu_reset;
87*1bb76ff1Sjsg 	atomic_t reset_res;
88*1bb76ff1Sjsg };
89*1bb76ff1Sjsg 
90*1bb76ff1Sjsg 
915ca02815Sjsg int amdgpu_reset_init(struct amdgpu_device *adev);
925ca02815Sjsg int amdgpu_reset_fini(struct amdgpu_device *adev);
935ca02815Sjsg 
945ca02815Sjsg int amdgpu_reset_prepare_hwcontext(struct amdgpu_device *adev,
955ca02815Sjsg 				   struct amdgpu_reset_context *reset_context);
965ca02815Sjsg 
975ca02815Sjsg int amdgpu_reset_perform_reset(struct amdgpu_device *adev,
985ca02815Sjsg 			       struct amdgpu_reset_context *reset_context);
995ca02815Sjsg 
1005ca02815Sjsg int amdgpu_reset_add_handler(struct amdgpu_reset_control *reset_ctl,
1015ca02815Sjsg 			     struct amdgpu_reset_handler *handler);
1025ca02815Sjsg 
103*1bb76ff1Sjsg struct amdgpu_reset_domain *amdgpu_reset_create_reset_domain(enum amdgpu_reset_domain_type type,
104*1bb76ff1Sjsg 							     char *wq_name);
105*1bb76ff1Sjsg 
106*1bb76ff1Sjsg void amdgpu_reset_destroy_reset_domain(struct kref *ref);
107*1bb76ff1Sjsg 
amdgpu_reset_get_reset_domain(struct amdgpu_reset_domain * domain)108*1bb76ff1Sjsg static inline bool amdgpu_reset_get_reset_domain(struct amdgpu_reset_domain *domain)
109*1bb76ff1Sjsg {
110*1bb76ff1Sjsg 	return kref_get_unless_zero(&domain->refcount) != 0;
111*1bb76ff1Sjsg }
112*1bb76ff1Sjsg 
amdgpu_reset_put_reset_domain(struct amdgpu_reset_domain * domain)113*1bb76ff1Sjsg static inline void amdgpu_reset_put_reset_domain(struct amdgpu_reset_domain *domain)
114*1bb76ff1Sjsg {
115*1bb76ff1Sjsg 	if (domain)
116*1bb76ff1Sjsg 		kref_put(&domain->refcount, amdgpu_reset_destroy_reset_domain);
117*1bb76ff1Sjsg }
118*1bb76ff1Sjsg 
amdgpu_reset_domain_schedule(struct amdgpu_reset_domain * domain,struct work_struct * work)119*1bb76ff1Sjsg static inline bool amdgpu_reset_domain_schedule(struct amdgpu_reset_domain *domain,
120*1bb76ff1Sjsg 						struct work_struct *work)
121*1bb76ff1Sjsg {
122*1bb76ff1Sjsg 	return queue_work(domain->wq, work);
123*1bb76ff1Sjsg }
124*1bb76ff1Sjsg 
125*1bb76ff1Sjsg void amdgpu_device_lock_reset_domain(struct amdgpu_reset_domain *reset_domain);
126*1bb76ff1Sjsg 
127*1bb76ff1Sjsg void amdgpu_device_unlock_reset_domain(struct amdgpu_reset_domain *reset_domain);
128*1bb76ff1Sjsg 
1295ca02815Sjsg #endif
130