1d38ceaf9SAlex Deucher /*
2d38ceaf9SAlex Deucher  * Copyright 2015 Advanced Micro Devices, Inc.
3d38ceaf9SAlex Deucher  *
4d38ceaf9SAlex Deucher  * Permission is hereby granted, free of charge, to any person obtaining a
5d38ceaf9SAlex Deucher  * copy of this software and associated documentation files (the "Software"),
6d38ceaf9SAlex Deucher  * to deal in the Software without restriction, including without limitation
7d38ceaf9SAlex Deucher  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8d38ceaf9SAlex Deucher  * and/or sell copies of the Software, and to permit persons to whom the
9d38ceaf9SAlex Deucher  * Software is furnished to do so, subject to the following conditions:
10d38ceaf9SAlex Deucher  *
11d38ceaf9SAlex Deucher  * The above copyright notice and this permission notice shall be included in
12d38ceaf9SAlex Deucher  * all copies or substantial portions of the Software.
13d38ceaf9SAlex Deucher  *
14d38ceaf9SAlex Deucher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15d38ceaf9SAlex Deucher  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16d38ceaf9SAlex Deucher  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17d38ceaf9SAlex Deucher  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18d38ceaf9SAlex Deucher  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19d38ceaf9SAlex Deucher  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20d38ceaf9SAlex Deucher  * OTHER DEALINGS IN THE SOFTWARE.
21d38ceaf9SAlex Deucher  *
22d38ceaf9SAlex Deucher  * Authors: monk liu <[email protected]>
23d38ceaf9SAlex Deucher  */
24d38ceaf9SAlex Deucher 
25c2636dc5SAndres Rodriguez #include <drm/drm_auth.h>
2657230f0cSAndrey Grodzovsky #include <drm/drm_drv.h>
27d38ceaf9SAlex Deucher #include "amdgpu.h"
2852c6a62cSAndres Rodriguez #include "amdgpu_sched.h"
29ae363a21Sxinhui pan #include "amdgpu_ras.h"
301c6d567bSNirmoy Das #include <linux/nospec.h>
31d38ceaf9SAlex Deucher 
321b1f2fecSChristian König #define to_amdgpu_ctx_entity(e)	\
331b1f2fecSChristian König 	container_of((e), struct amdgpu_ctx_entity, entity)
341b1f2fecSChristian König 
351b1f2fecSChristian König const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
361b1f2fecSChristian König 	[AMDGPU_HW_IP_GFX]	=	1,
371b1f2fecSChristian König 	[AMDGPU_HW_IP_COMPUTE]	=	4,
381b1f2fecSChristian König 	[AMDGPU_HW_IP_DMA]	=	2,
391b1f2fecSChristian König 	[AMDGPU_HW_IP_UVD]	=	1,
401b1f2fecSChristian König 	[AMDGPU_HW_IP_VCE]	=	1,
411b1f2fecSChristian König 	[AMDGPU_HW_IP_UVD_ENC]	=	1,
421b1f2fecSChristian König 	[AMDGPU_HW_IP_VCN_DEC]	=	1,
431b1f2fecSChristian König 	[AMDGPU_HW_IP_VCN_ENC]	=	1,
44f52c9643SAlex Deucher 	[AMDGPU_HW_IP_VCN_JPEG]	=	1,
45523c1280SLang Yu 	[AMDGPU_HW_IP_VPE]	=	1,
461b1f2fecSChristian König };
471b1f2fecSChristian König 
amdgpu_ctx_priority_is_valid(int32_t ctx_prio)4884d588c3SNirmoy Das bool amdgpu_ctx_priority_is_valid(int32_t ctx_prio)
49c2636dc5SAndres Rodriguez {
5084d588c3SNirmoy Das 	switch (ctx_prio) {
5184d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_VERY_LOW:
5284d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_LOW:
5384d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_NORMAL:
5484d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_HIGH:
5584d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
5684d588c3SNirmoy Das 		return true;
5784d588c3SNirmoy Das 	default:
58eab02619SLuben Tuikov 	case AMDGPU_CTX_PRIORITY_UNSET:
59d3df66fdSLuben Tuikov 		/* UNSET priority is not valid and we don't carry that
60d3df66fdSLuben Tuikov 		 * around, but set it to NORMAL in the only place this
61d3df66fdSLuben Tuikov 		 * function is called, amdgpu_ctx_ioctl().
62d3df66fdSLuben Tuikov 		 */
6384d588c3SNirmoy Das 		return false;
6484d588c3SNirmoy Das 	}
6584d588c3SNirmoy Das }
6684d588c3SNirmoy Das 
6784d588c3SNirmoy Das static enum drm_sched_priority
amdgpu_ctx_to_drm_sched_prio(int32_t ctx_prio)6884d588c3SNirmoy Das amdgpu_ctx_to_drm_sched_prio(int32_t ctx_prio)
6984d588c3SNirmoy Das {
7084d588c3SNirmoy Das 	switch (ctx_prio) {
7184d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_UNSET:
72fa8391adSLuben Tuikov 		pr_warn_once("AMD-->DRM context priority value UNSET-->NORMAL");
73fa8391adSLuben Tuikov 		return DRM_SCHED_PRIORITY_NORMAL;
7484d588c3SNirmoy Das 
7584d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_VERY_LOW:
76fe375c74SLuben Tuikov 		return DRM_SCHED_PRIORITY_LOW;
7784d588c3SNirmoy Das 
7884d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_LOW:
79fe375c74SLuben Tuikov 		return DRM_SCHED_PRIORITY_LOW;
8084d588c3SNirmoy Das 
8184d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_NORMAL:
8284d588c3SNirmoy Das 		return DRM_SCHED_PRIORITY_NORMAL;
8384d588c3SNirmoy Das 
8484d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_HIGH:
8584d588c3SNirmoy Das 		return DRM_SCHED_PRIORITY_HIGH;
8684d588c3SNirmoy Das 
8784d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
8884d588c3SNirmoy Das 		return DRM_SCHED_PRIORITY_HIGH;
8984d588c3SNirmoy Das 
9084d588c3SNirmoy Das 	/* This should not happen as we sanitized userspace provided priority
9184d588c3SNirmoy Das 	 * already, WARN if this happens.
9284d588c3SNirmoy Das 	 */
9384d588c3SNirmoy Das 	default:
9484d588c3SNirmoy Das 		WARN(1, "Invalid context priority %d\n", ctx_prio);
9584d588c3SNirmoy Das 		return DRM_SCHED_PRIORITY_NORMAL;
9684d588c3SNirmoy Das 	}
9784d588c3SNirmoy Das 
9884d588c3SNirmoy Das }
9984d588c3SNirmoy Das 
amdgpu_ctx_priority_permit(struct drm_file * filp,int32_t priority)10084d588c3SNirmoy Das static int amdgpu_ctx_priority_permit(struct drm_file *filp,
10184d588c3SNirmoy Das 				      int32_t priority)
10284d588c3SNirmoy Das {
103c2636dc5SAndres Rodriguez 	/* NORMAL and below are accessible by everyone */
10484d588c3SNirmoy Das 	if (priority <= AMDGPU_CTX_PRIORITY_NORMAL)
105c2636dc5SAndres Rodriguez 		return 0;
106c2636dc5SAndres Rodriguez 
107c2636dc5SAndres Rodriguez 	if (capable(CAP_SYS_NICE))
108c2636dc5SAndres Rodriguez 		return 0;
109c2636dc5SAndres Rodriguez 
110c2636dc5SAndres Rodriguez 	if (drm_is_current_master(filp))
111c2636dc5SAndres Rodriguez 		return 0;
112c2636dc5SAndres Rodriguez 
113c2636dc5SAndres Rodriguez 	return -EACCES;
114c2636dc5SAndres Rodriguez }
115c2636dc5SAndres Rodriguez 
amdgpu_ctx_prio_to_gfx_pipe_prio(int32_t prio)116b07d1d73SArunpravin Paneer Selvam static enum amdgpu_gfx_pipe_priority amdgpu_ctx_prio_to_gfx_pipe_prio(int32_t prio)
11733abcb1fSNirmoy Das {
11833abcb1fSNirmoy Das 	switch (prio) {
11984d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_HIGH:
12084d588c3SNirmoy Das 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
12133abcb1fSNirmoy Das 		return AMDGPU_GFX_PIPE_PRIO_HIGH;
12233abcb1fSNirmoy Das 	default:
12333abcb1fSNirmoy Das 		return AMDGPU_GFX_PIPE_PRIO_NORMAL;
12433abcb1fSNirmoy Das 	}
12533abcb1fSNirmoy Das }
12633abcb1fSNirmoy Das 
amdgpu_ctx_sched_prio_to_ring_prio(int32_t prio)1277d7630fcSSatyajit Sahu static enum amdgpu_ring_priority_level amdgpu_ctx_sched_prio_to_ring_prio(int32_t prio)
1287d7630fcSSatyajit Sahu {
1297d7630fcSSatyajit Sahu 	switch (prio) {
1307d7630fcSSatyajit Sahu 	case AMDGPU_CTX_PRIORITY_HIGH:
1317d7630fcSSatyajit Sahu 		return AMDGPU_RING_PRIO_1;
1327d7630fcSSatyajit Sahu 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
1337d7630fcSSatyajit Sahu 		return AMDGPU_RING_PRIO_2;
1347d7630fcSSatyajit Sahu 	default:
1357d7630fcSSatyajit Sahu 		return AMDGPU_RING_PRIO_0;
1367d7630fcSSatyajit Sahu 	}
1377d7630fcSSatyajit Sahu }
1387d7630fcSSatyajit Sahu 
amdgpu_ctx_get_hw_prio(struct amdgpu_ctx * ctx,u32 hw_ip)13984d588c3SNirmoy Das static unsigned int amdgpu_ctx_get_hw_prio(struct amdgpu_ctx *ctx, u32 hw_ip)
1401c6d567bSNirmoy Das {
14169493c03SChristian König 	struct amdgpu_device *adev = ctx->mgr->adev;
1421c6d567bSNirmoy Das 	unsigned int hw_prio;
14369493c03SChristian König 	int32_t ctx_prio;
1441c6d567bSNirmoy Das 
14584d588c3SNirmoy Das 	ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
14684d588c3SNirmoy Das 			ctx->init_priority : ctx->override_priority;
14784d588c3SNirmoy Das 
14884d588c3SNirmoy Das 	switch (hw_ip) {
149b07d1d73SArunpravin Paneer Selvam 	case AMDGPU_HW_IP_GFX:
15084d588c3SNirmoy Das 	case AMDGPU_HW_IP_COMPUTE:
151b07d1d73SArunpravin Paneer Selvam 		hw_prio = amdgpu_ctx_prio_to_gfx_pipe_prio(ctx_prio);
15284d588c3SNirmoy Das 		break;
1537d7630fcSSatyajit Sahu 	case AMDGPU_HW_IP_VCE:
1547d7630fcSSatyajit Sahu 	case AMDGPU_HW_IP_VCN_ENC:
1557d7630fcSSatyajit Sahu 		hw_prio = amdgpu_ctx_sched_prio_to_ring_prio(ctx_prio);
1567d7630fcSSatyajit Sahu 		break;
15784d588c3SNirmoy Das 	default:
15884d588c3SNirmoy Das 		hw_prio = AMDGPU_RING_PRIO_DEFAULT;
15984d588c3SNirmoy Das 		break;
16084d588c3SNirmoy Das 	}
16184d588c3SNirmoy Das 
1621c6d567bSNirmoy Das 	hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
1631c6d567bSNirmoy Das 	if (adev->gpu_sched[hw_ip][hw_prio].num_scheds == 0)
1641c6d567bSNirmoy Das 		hw_prio = AMDGPU_RING_PRIO_DEFAULT;
1651c6d567bSNirmoy Das 
1661c6d567bSNirmoy Das 	return hw_prio;
1671c6d567bSNirmoy Das }
1681c6d567bSNirmoy Das 
169af0b5416SChristian König /* Calculate the time spend on the hw */
amdgpu_ctx_fence_time(struct dma_fence * fence)170af0b5416SChristian König static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence)
171af0b5416SChristian König {
172af0b5416SChristian König 	struct drm_sched_fence *s_fence;
173af0b5416SChristian König 
174af0b5416SChristian König 	if (!fence)
175af0b5416SChristian König 		return ns_to_ktime(0);
176af0b5416SChristian König 
177af0b5416SChristian König 	/* When the fence is not even scheduled it can't have spend time */
178af0b5416SChristian König 	s_fence = to_drm_sched_fence(fence);
179af0b5416SChristian König 	if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled.flags))
180af0b5416SChristian König 		return ns_to_ktime(0);
181af0b5416SChristian König 
182af0b5416SChristian König 	/* When it is still running account how much already spend */
183af0b5416SChristian König 	if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.flags))
184af0b5416SChristian König 		return ktime_sub(ktime_get(), s_fence->scheduled.timestamp);
185af0b5416SChristian König 
186af0b5416SChristian König 	return ktime_sub(s_fence->finished.timestamp,
187af0b5416SChristian König 			 s_fence->scheduled.timestamp);
188af0b5416SChristian König }
189af0b5416SChristian König 
amdgpu_ctx_entity_time(struct amdgpu_ctx * ctx,struct amdgpu_ctx_entity * centity)190af0b5416SChristian König static ktime_t amdgpu_ctx_entity_time(struct amdgpu_ctx *ctx,
191af0b5416SChristian König 				      struct amdgpu_ctx_entity *centity)
192af0b5416SChristian König {
193af0b5416SChristian König 	ktime_t res = ns_to_ktime(0);
194af0b5416SChristian König 	uint32_t i;
195af0b5416SChristian König 
196af0b5416SChristian König 	spin_lock(&ctx->ring_lock);
197af0b5416SChristian König 	for (i = 0; i < amdgpu_sched_jobs; i++) {
198af0b5416SChristian König 		res = ktime_add(res, amdgpu_ctx_fence_time(centity->fences[i]));
199af0b5416SChristian König 	}
200af0b5416SChristian König 	spin_unlock(&ctx->ring_lock);
201af0b5416SChristian König 	return res;
202af0b5416SChristian König }
20384d588c3SNirmoy Das 
amdgpu_ctx_init_entity(struct amdgpu_ctx * ctx,u32 hw_ip,const u32 ring)2041c6d567bSNirmoy Das static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
2051c6d567bSNirmoy Das 				  const u32 ring)
206d38ceaf9SAlex Deucher {
207af0b5416SChristian König 	struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
20869493c03SChristian König 	struct amdgpu_device *adev = ctx->mgr->adev;
209977f7e10SNirmoy Das 	struct amdgpu_ctx_entity *entity;
21084d588c3SNirmoy Das 	enum drm_sched_priority drm_prio;
211af0b5416SChristian König 	unsigned int hw_prio, num_scheds;
212af0b5416SChristian König 	int32_t ctx_prio;
21347f38501SChristian König 	int r;
21447f38501SChristian König 
215201a4eb9SGustavo A. R. Silva 	entity = kzalloc(struct_size(entity, fences, amdgpu_sched_jobs),
2161b1f2fecSChristian König 			 GFP_KERNEL);
217977f7e10SNirmoy Das 	if (!entity)
21863e3ab9aSNirmoy Das 		return  -ENOMEM;
21963e3ab9aSNirmoy Das 
22084d588c3SNirmoy Das 	ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
221977f7e10SNirmoy Das 			ctx->init_priority : ctx->override_priority;
222af0b5416SChristian König 	entity->hw_ip = hw_ip;
22384d588c3SNirmoy Das 	entity->sequence = 1;
22484d588c3SNirmoy Das 	hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
22584d588c3SNirmoy Das 	drm_prio = amdgpu_ctx_to_drm_sched_prio(ctx_prio);
2261c6d567bSNirmoy Das 
2271c6d567bSNirmoy Das 	hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
2289a18292dSJames Zhu 
2299a18292dSJames Zhu 	if (!(adev)->xcp_mgr) {
2301c6d567bSNirmoy Das 		scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
2311c6d567bSNirmoy Das 		num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
2329a18292dSJames Zhu 	} else {
2339a18292dSJames Zhu 		struct amdgpu_fpriv *fpriv;
2349a18292dSJames Zhu 
2359a18292dSJames Zhu 		fpriv = container_of(ctx->ctx_mgr, struct amdgpu_fpriv, ctx_mgr);
2369a18292dSJames Zhu 		r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
2379a18292dSJames Zhu 						&num_scheds, &scheds);
2389a18292dSJames Zhu 		if (r)
2399a18292dSJames Zhu 			goto cleanup_entity;
2409a18292dSJames Zhu 	}
2411c6d567bSNirmoy Das 
242bc21585fSNirmoy Das 	/* disable load balance if the hw engine retains context among dependent jobs */
243bc21585fSNirmoy Das 	if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
244bc21585fSNirmoy Das 	    hw_ip == AMDGPU_HW_IP_VCN_DEC ||
245bc21585fSNirmoy Das 	    hw_ip == AMDGPU_HW_IP_UVD_ENC ||
246bc21585fSNirmoy Das 	    hw_ip == AMDGPU_HW_IP_UVD) {
2471c6d567bSNirmoy Das 		sched = drm_sched_pick_best(scheds, num_scheds);
2480a96afc7SLe Ma 		scheds = &sched;
249f880799dSNirmoy Das 		num_scheds = 1;
250845e6fdfSChristian König 	}
251845e6fdfSChristian König 
25284d588c3SNirmoy Das 	r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
253977f7e10SNirmoy Das 				  &ctx->guilty);
2549cb7e5a9SChunming Zhou 	if (r)
255977f7e10SNirmoy Das 		goto error_free_entity;
256977f7e10SNirmoy Das 
257d18b8eadSChristian König 	/* It's not an error if we fail to install the new entity */
258d18b8eadSChristian König 	if (cmpxchg(&ctx->entities[hw_ip][ring], NULL, entity))
259d18b8eadSChristian König 		goto cleanup_entity;
260d18b8eadSChristian König 
261977f7e10SNirmoy Das 	return 0;
262977f7e10SNirmoy Das 
263d18b8eadSChristian König cleanup_entity:
264d18b8eadSChristian König 	drm_sched_entity_fini(&entity->entity);
265d18b8eadSChristian König 
266977f7e10SNirmoy Das error_free_entity:
267977f7e10SNirmoy Das 	kfree(entity);
268977f7e10SNirmoy Das 
269977f7e10SNirmoy Das 	return r;
2709cb7e5a9SChunming Zhou }
2719cb7e5a9SChunming Zhou 
amdgpu_ctx_fini_entity(struct amdgpu_device * adev,struct amdgpu_ctx_entity * entity)2723e7c6fe3SJames Zhu static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_device *adev,
2733e7c6fe3SJames Zhu 				  struct amdgpu_ctx_entity *entity)
274af0b5416SChristian König {
275af0b5416SChristian König 	ktime_t res = ns_to_ktime(0);
276af0b5416SChristian König 	int i;
277af0b5416SChristian König 
278af0b5416SChristian König 	if (!entity)
279af0b5416SChristian König 		return res;
280af0b5416SChristian König 
281af0b5416SChristian König 	for (i = 0; i < amdgpu_sched_jobs; ++i) {
282af0b5416SChristian König 		res = ktime_add(res, amdgpu_ctx_fence_time(entity->fences[i]));
283af0b5416SChristian König 		dma_fence_put(entity->fences[i]);
284af0b5416SChristian König 	}
285af0b5416SChristian König 
2863e7c6fe3SJames Zhu 	amdgpu_xcp_release_sched(adev, entity);
2873e7c6fe3SJames Zhu 
288af0b5416SChristian König 	kfree(entity);
289af0b5416SChristian König 	return res;
290af0b5416SChristian König }
291af0b5416SChristian König 
amdgpu_ctx_get_stable_pstate(struct amdgpu_ctx * ctx,u32 * stable_pstate)2928cda7a4fSAlex Deucher static int amdgpu_ctx_get_stable_pstate(struct amdgpu_ctx *ctx,
2938cda7a4fSAlex Deucher 					u32 *stable_pstate)
2948cda7a4fSAlex Deucher {
29569493c03SChristian König 	struct amdgpu_device *adev = ctx->mgr->adev;
2968cda7a4fSAlex Deucher 	enum amd_dpm_forced_level current_level;
2978cda7a4fSAlex Deucher 
2988cda7a4fSAlex Deucher 	current_level = amdgpu_dpm_get_performance_level(adev);
2998cda7a4fSAlex Deucher 
3008cda7a4fSAlex Deucher 	switch (current_level) {
3018cda7a4fSAlex Deucher 	case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
3028cda7a4fSAlex Deucher 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_STANDARD;
3038cda7a4fSAlex Deucher 		break;
3048cda7a4fSAlex Deucher 	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
3058cda7a4fSAlex Deucher 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK;
3068cda7a4fSAlex Deucher 		break;
3078cda7a4fSAlex Deucher 	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
3088cda7a4fSAlex Deucher 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK;
3098cda7a4fSAlex Deucher 		break;
3108cda7a4fSAlex Deucher 	case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
3118cda7a4fSAlex Deucher 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_PEAK;
3128cda7a4fSAlex Deucher 		break;
3138cda7a4fSAlex Deucher 	default:
3148cda7a4fSAlex Deucher 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_NONE;
3158cda7a4fSAlex Deucher 		break;
3168cda7a4fSAlex Deucher 	}
3178cda7a4fSAlex Deucher 	return 0;
3188cda7a4fSAlex Deucher }
3198cda7a4fSAlex Deucher 
amdgpu_ctx_init(struct amdgpu_ctx_mgr * mgr,int32_t priority,struct drm_file * filp,struct amdgpu_ctx * ctx)320958afce9SAlex Deucher static int amdgpu_ctx_init(struct amdgpu_ctx_mgr *mgr, int32_t priority,
321958afce9SAlex Deucher 			   struct drm_file *filp, struct amdgpu_ctx *ctx)
322958afce9SAlex Deucher {
323c30e326eSJames Zhu 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
324958afce9SAlex Deucher 	u32 current_stable_pstate;
325958afce9SAlex Deucher 	int r;
326958afce9SAlex Deucher 
327958afce9SAlex Deucher 	r = amdgpu_ctx_priority_permit(filp, priority);
328958afce9SAlex Deucher 	if (r)
329958afce9SAlex Deucher 		return r;
330958afce9SAlex Deucher 
331958afce9SAlex Deucher 	memset(ctx, 0, sizeof(*ctx));
332958afce9SAlex Deucher 
333958afce9SAlex Deucher 	kref_init(&ctx->refcount);
334958afce9SAlex Deucher 	ctx->mgr = mgr;
335958afce9SAlex Deucher 	spin_lock_init(&ctx->ring_lock);
336958afce9SAlex Deucher 
337958afce9SAlex Deucher 	ctx->reset_counter = atomic_read(&mgr->adev->gpu_reset_counter);
338958afce9SAlex Deucher 	ctx->reset_counter_query = ctx->reset_counter;
339f88e295eSChristian König 	ctx->generation = amdgpu_vm_generation(mgr->adev, &fpriv->vm);
340958afce9SAlex Deucher 	ctx->init_priority = priority;
341958afce9SAlex Deucher 	ctx->override_priority = AMDGPU_CTX_PRIORITY_UNSET;
342958afce9SAlex Deucher 
343958afce9SAlex Deucher 	r = amdgpu_ctx_get_stable_pstate(ctx, &current_stable_pstate);
344958afce9SAlex Deucher 	if (r)
345958afce9SAlex Deucher 		return r;
346958afce9SAlex Deucher 
34779610d30SChengming Gui 	if (mgr->adev->pm.stable_pstate_ctx)
34879610d30SChengming Gui 		ctx->stable_pstate = mgr->adev->pm.stable_pstate_ctx->stable_pstate;
34979610d30SChengming Gui 	else
350958afce9SAlex Deucher 		ctx->stable_pstate = current_stable_pstate;
351958afce9SAlex Deucher 
352c30e326eSJames Zhu 	ctx->ctx_mgr = &(fpriv->ctx_mgr);
353958afce9SAlex Deucher 	return 0;
354958afce9SAlex Deucher }
355958afce9SAlex Deucher 
amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx * ctx,u32 stable_pstate)3568cda7a4fSAlex Deucher static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx,
3578cda7a4fSAlex Deucher 					u32 stable_pstate)
3588cda7a4fSAlex Deucher {
35969493c03SChristian König 	struct amdgpu_device *adev = ctx->mgr->adev;
3608cda7a4fSAlex Deucher 	enum amd_dpm_forced_level level;
361505c170bSAlex Deucher 	u32 current_stable_pstate;
3628cda7a4fSAlex Deucher 	int r;
3638cda7a4fSAlex Deucher 
3648cda7a4fSAlex Deucher 	mutex_lock(&adev->pm.stable_pstate_ctx_lock);
3658cda7a4fSAlex Deucher 	if (adev->pm.stable_pstate_ctx && adev->pm.stable_pstate_ctx != ctx) {
3668cda7a4fSAlex Deucher 		r = -EBUSY;
3678cda7a4fSAlex Deucher 		goto done;
3688cda7a4fSAlex Deucher 	}
3698cda7a4fSAlex Deucher 
370505c170bSAlex Deucher 	r = amdgpu_ctx_get_stable_pstate(ctx, &current_stable_pstate);
371505c170bSAlex Deucher 	if (r || (stable_pstate == current_stable_pstate))
372505c170bSAlex Deucher 		goto done;
373505c170bSAlex Deucher 
3748cda7a4fSAlex Deucher 	switch (stable_pstate) {
3758cda7a4fSAlex Deucher 	case AMDGPU_CTX_STABLE_PSTATE_NONE:
3768cda7a4fSAlex Deucher 		level = AMD_DPM_FORCED_LEVEL_AUTO;
3778cda7a4fSAlex Deucher 		break;
3788cda7a4fSAlex Deucher 	case AMDGPU_CTX_STABLE_PSTATE_STANDARD:
3798cda7a4fSAlex Deucher 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
3808cda7a4fSAlex Deucher 		break;
3818cda7a4fSAlex Deucher 	case AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK:
3828cda7a4fSAlex Deucher 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
3838cda7a4fSAlex Deucher 		break;
3848cda7a4fSAlex Deucher 	case AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK:
3858cda7a4fSAlex Deucher 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
3868cda7a4fSAlex Deucher 		break;
3878cda7a4fSAlex Deucher 	case AMDGPU_CTX_STABLE_PSTATE_PEAK:
3888cda7a4fSAlex Deucher 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
3898cda7a4fSAlex Deucher 		break;
3908cda7a4fSAlex Deucher 	default:
3918cda7a4fSAlex Deucher 		r = -EINVAL;
3928cda7a4fSAlex Deucher 		goto done;
3938cda7a4fSAlex Deucher 	}
3948cda7a4fSAlex Deucher 
3958cda7a4fSAlex Deucher 	r = amdgpu_dpm_force_performance_level(adev, level);
3968cda7a4fSAlex Deucher 
3978cda7a4fSAlex Deucher 	if (level == AMD_DPM_FORCED_LEVEL_AUTO)
3988cda7a4fSAlex Deucher 		adev->pm.stable_pstate_ctx = NULL;
3998cda7a4fSAlex Deucher 	else
4008cda7a4fSAlex Deucher 		adev->pm.stable_pstate_ctx = ctx;
4018cda7a4fSAlex Deucher done:
4028cda7a4fSAlex Deucher 	mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
4038cda7a4fSAlex Deucher 
4048cda7a4fSAlex Deucher 	return r;
4058cda7a4fSAlex Deucher }
4068cda7a4fSAlex Deucher 
amdgpu_ctx_fini(struct kref * ref)4078ee3a52eSEmily Deng static void amdgpu_ctx_fini(struct kref *ref)
40847f38501SChristian König {
4098ee3a52eSEmily Deng 	struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
41069493c03SChristian König 	struct amdgpu_ctx_mgr *mgr = ctx->mgr;
41169493c03SChristian König 	struct amdgpu_device *adev = mgr->adev;
41257230f0cSAndrey Grodzovsky 	unsigned i, j, idx;
41347f38501SChristian König 
414fe295b27SDave Airlie 	if (!adev)
415fe295b27SDave Airlie 		return;
416fe295b27SDave Airlie 
417977f7e10SNirmoy Das 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
418977f7e10SNirmoy Das 		for (j = 0; j < AMDGPU_MAX_ENTITY_NUM; ++j) {
419af0b5416SChristian König 			ktime_t spend;
420af0b5416SChristian König 
4213e7c6fe3SJames Zhu 			spend = amdgpu_ctx_fini_entity(adev, ctx->entities[i][j]);
422af0b5416SChristian König 			atomic64_add(ktime_to_ns(spend), &mgr->time_spend[i]);
423977f7e10SNirmoy Das 		}
42463e3ab9aSNirmoy Das 	}
42557230f0cSAndrey Grodzovsky 
426a79f56d1SGuchun Chen 	if (drm_dev_enter(adev_to_drm(adev), &idx)) {
427958afce9SAlex Deucher 		amdgpu_ctx_set_stable_pstate(ctx, ctx->stable_pstate);
42857230f0cSAndrey Grodzovsky 		drm_dev_exit(idx);
42957230f0cSAndrey Grodzovsky 	}
43057230f0cSAndrey Grodzovsky 
4318ee3a52eSEmily Deng 	kfree(ctx);
43247f38501SChristian König }
43347f38501SChristian König 
amdgpu_ctx_get_entity(struct amdgpu_ctx * ctx,u32 hw_ip,u32 instance,u32 ring,struct drm_sched_entity ** entity)4340d346a14SChristian König int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
4350d346a14SChristian König 			  u32 ring, struct drm_sched_entity **entity)
436869a53d4SChristian König {
437977f7e10SNirmoy Das 	int r;
43871eaac36SZhenGuo Yin 	struct drm_sched_entity *ctx_entity;
439977f7e10SNirmoy Das 
4401b1f2fecSChristian König 	if (hw_ip >= AMDGPU_HW_IP_NUM) {
4411b1f2fecSChristian König 		DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
4421b1f2fecSChristian König 		return -EINVAL;
4431b1f2fecSChristian König 	}
444869a53d4SChristian König 
445869a53d4SChristian König 	/* Right now all IPs have only one instance - multiple rings. */
446869a53d4SChristian König 	if (instance != 0) {
447869a53d4SChristian König 		DRM_DEBUG("invalid ip instance: %d\n", instance);
448869a53d4SChristian König 		return -EINVAL;
449869a53d4SChristian König 	}
450869a53d4SChristian König 
4511b1f2fecSChristian König 	if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
4521b1f2fecSChristian König 		DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
453869a53d4SChristian König 		return -EINVAL;
454869a53d4SChristian König 	}
455869a53d4SChristian König 
456977f7e10SNirmoy Das 	if (ctx->entities[hw_ip][ring] == NULL) {
457977f7e10SNirmoy Das 		r = amdgpu_ctx_init_entity(ctx, hw_ip, ring);
458977f7e10SNirmoy Das 		if (r)
459977f7e10SNirmoy Das 			return r;
460977f7e10SNirmoy Das 	}
461977f7e10SNirmoy Das 
46271eaac36SZhenGuo Yin 	ctx_entity = &ctx->entities[hw_ip][ring]->entity;
46371eaac36SZhenGuo Yin 	r = drm_sched_entity_error(ctx_entity);
46471eaac36SZhenGuo Yin 	if (r) {
46571eaac36SZhenGuo Yin 		DRM_DEBUG("error entity %p\n", ctx_entity);
46671eaac36SZhenGuo Yin 		return r;
46771eaac36SZhenGuo Yin 	}
46871eaac36SZhenGuo Yin 
46971eaac36SZhenGuo Yin 	*entity = ctx_entity;
470869a53d4SChristian König 	return 0;
471869a53d4SChristian König }
472869a53d4SChristian König 
amdgpu_ctx_alloc(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,struct drm_file * filp,int32_t priority,uint32_t * id)47347f38501SChristian König static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
47447f38501SChristian König 			    struct amdgpu_fpriv *fpriv,
475c2636dc5SAndres Rodriguez 			    struct drm_file *filp,
47684d588c3SNirmoy Das 			    int32_t priority,
47747f38501SChristian König 			    uint32_t *id)
47847f38501SChristian König {
47947f38501SChristian König 	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
48047f38501SChristian König 	struct amdgpu_ctx *ctx;
48147f38501SChristian König 	int r;
48247f38501SChristian König 
48347f38501SChristian König 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
48447f38501SChristian König 	if (!ctx)
48547f38501SChristian König 		return -ENOMEM;
48647f38501SChristian König 
48747f38501SChristian König 	mutex_lock(&mgr->lock);
48808d1bdd4SRex Zhu 	r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
48947f38501SChristian König 	if (r < 0) {
49047f38501SChristian König 		mutex_unlock(&mgr->lock);
49147f38501SChristian König 		kfree(ctx);
49247f38501SChristian König 		return r;
49347f38501SChristian König 	}
494c2636dc5SAndres Rodriguez 
49547f38501SChristian König 	*id = (uint32_t)r;
49669493c03SChristian König 	r = amdgpu_ctx_init(mgr, priority, filp, ctx);
497c648ed7cSChunming Zhou 	if (r) {
498c648ed7cSChunming Zhou 		idr_remove(&mgr->ctx_handles, *id);
499c648ed7cSChunming Zhou 		*id = 0;
500c648ed7cSChunming Zhou 		kfree(ctx);
501c648ed7cSChunming Zhou 	}
50247f38501SChristian König 	mutex_unlock(&mgr->lock);
50347f38501SChristian König 	return r;
50447f38501SChristian König }
50547f38501SChristian König 
amdgpu_ctx_do_release(struct kref * ref)50647f38501SChristian König static void amdgpu_ctx_do_release(struct kref *ref)
507d38ceaf9SAlex Deucher {
508d38ceaf9SAlex Deucher 	struct amdgpu_ctx *ctx;
509977f7e10SNirmoy Das 	u32 i, j;
510d38ceaf9SAlex Deucher 
51147f38501SChristian König 	ctx = container_of(ref, struct amdgpu_ctx, refcount);
512977f7e10SNirmoy Das 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
513977f7e10SNirmoy Das 		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
514977f7e10SNirmoy Das 			if (!ctx->entities[i][j])
515977f7e10SNirmoy Das 				continue;
51647f38501SChristian König 
517977f7e10SNirmoy Das 			drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
518977f7e10SNirmoy Das 		}
519977f7e10SNirmoy Das 	}
52047f38501SChristian König 
5218ee3a52eSEmily Deng 	amdgpu_ctx_fini(ref);
52247f38501SChristian König }
52347f38501SChristian König 
amdgpu_ctx_free(struct amdgpu_fpriv * fpriv,uint32_t id)52447f38501SChristian König static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
52547f38501SChristian König {
52623ca0e4eSChunming Zhou 	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
52747f38501SChristian König 	struct amdgpu_ctx *ctx;
52847f38501SChristian König 
5290147ee0fSMarek Olšák 	mutex_lock(&mgr->lock);
530d3e709e6SMatthew Wilcox 	ctx = idr_remove(&mgr->ctx_handles, id);
531d3e709e6SMatthew Wilcox 	if (ctx)
532f11358daSMarek Olšák 		kref_put(&ctx->refcount, amdgpu_ctx_do_release);
5330147ee0fSMarek Olšák 	mutex_unlock(&mgr->lock);
534d3e709e6SMatthew Wilcox 	return ctx ? 0 : -EINVAL;
535d38ceaf9SAlex Deucher }
536d38ceaf9SAlex Deucher 
amdgpu_ctx_query(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,uint32_t id,union drm_amdgpu_ctx_out * out)537d94aed5aSMarek Olšák static int amdgpu_ctx_query(struct amdgpu_device *adev,
538d94aed5aSMarek Olšák 			    struct amdgpu_fpriv *fpriv, uint32_t id,
539d94aed5aSMarek Olšák 			    union drm_amdgpu_ctx_out *out)
540d38ceaf9SAlex Deucher {
541d38ceaf9SAlex Deucher 	struct amdgpu_ctx *ctx;
54223ca0e4eSChunming Zhou 	struct amdgpu_ctx_mgr *mgr;
543d94aed5aSMarek Olšák 	unsigned reset_counter;
544d38ceaf9SAlex Deucher 
54523ca0e4eSChunming Zhou 	if (!fpriv)
54623ca0e4eSChunming Zhou 		return -EINVAL;
54723ca0e4eSChunming Zhou 
54823ca0e4eSChunming Zhou 	mgr = &fpriv->ctx_mgr;
5490147ee0fSMarek Olšák 	mutex_lock(&mgr->lock);
550d38ceaf9SAlex Deucher 	ctx = idr_find(&mgr->ctx_handles, id);
551d94aed5aSMarek Olšák 	if (!ctx) {
5520147ee0fSMarek Olšák 		mutex_unlock(&mgr->lock);
553d38ceaf9SAlex Deucher 		return -EINVAL;
554d38ceaf9SAlex Deucher 	}
555d38ceaf9SAlex Deucher 
556d94aed5aSMarek Olšák 	/* TODO: these two are always zero */
5570b492a4cSAlex Deucher 	out->state.flags = 0x0;
5580b492a4cSAlex Deucher 	out->state.hangs = 0x0;
559d94aed5aSMarek Olšák 
560d94aed5aSMarek Olšák 	/* determine if a GPU reset has occured since the last call */
561d94aed5aSMarek Olšák 	reset_counter = atomic_read(&adev->gpu_reset_counter);
562d94aed5aSMarek Olšák 	/* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
563668ca1b4SMonk Liu 	if (ctx->reset_counter_query == reset_counter)
564d94aed5aSMarek Olšák 		out->state.reset_status = AMDGPU_CTX_NO_RESET;
565d94aed5aSMarek Olšák 	else
566d94aed5aSMarek Olšák 		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
567668ca1b4SMonk Liu 	ctx->reset_counter_query = reset_counter;
568d94aed5aSMarek Olšák 
569d94aed5aSMarek Olšák 	mutex_unlock(&mgr->lock);
570d94aed5aSMarek Olšák 	return 0;
571d94aed5aSMarek Olšák }
572d94aed5aSMarek Olšák 
57305adfd80SLuben Tuikov #define AMDGPU_RAS_COUNTE_DELAY_MS 3000
57405adfd80SLuben Tuikov 
amdgpu_ctx_query2(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,uint32_t id,union drm_amdgpu_ctx_out * out)575bc1b1bf6SMonk Liu static int amdgpu_ctx_query2(struct amdgpu_device *adev,
576bc1b1bf6SMonk Liu 			     struct amdgpu_fpriv *fpriv, uint32_t id,
577bc1b1bf6SMonk Liu 			     union drm_amdgpu_ctx_out *out)
578bc1b1bf6SMonk Liu {
57905adfd80SLuben Tuikov 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
580bc1b1bf6SMonk Liu 	struct amdgpu_ctx *ctx;
581bc1b1bf6SMonk Liu 	struct amdgpu_ctx_mgr *mgr;
582bc1b1bf6SMonk Liu 
583bc1b1bf6SMonk Liu 	if (!fpriv)
584bc1b1bf6SMonk Liu 		return -EINVAL;
585bc1b1bf6SMonk Liu 
586bc1b1bf6SMonk Liu 	mgr = &fpriv->ctx_mgr;
587bc1b1bf6SMonk Liu 	mutex_lock(&mgr->lock);
588bc1b1bf6SMonk Liu 	ctx = idr_find(&mgr->ctx_handles, id);
589bc1b1bf6SMonk Liu 	if (!ctx) {
590bc1b1bf6SMonk Liu 		mutex_unlock(&mgr->lock);
591bc1b1bf6SMonk Liu 		return -EINVAL;
592bc1b1bf6SMonk Liu 	}
593bc1b1bf6SMonk Liu 
594bc1b1bf6SMonk Liu 	out->state.flags = 0x0;
595bc1b1bf6SMonk Liu 	out->state.hangs = 0x0;
596bc1b1bf6SMonk Liu 
597bc1b1bf6SMonk Liu 	if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
598bc1b1bf6SMonk Liu 		out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
599bc1b1bf6SMonk Liu 
600f88e295eSChristian König 	if (ctx->generation != amdgpu_vm_generation(adev, &fpriv->vm))
601bc1b1bf6SMonk Liu 		out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
602bc1b1bf6SMonk Liu 
603bc1b1bf6SMonk Liu 	if (atomic_read(&ctx->guilty))
604bc1b1bf6SMonk Liu 		out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
605bc1b1bf6SMonk Liu 
606489763afSPierre-Eric Pelloux-Prayer 	if (amdgpu_in_reset(adev))
607489763afSPierre-Eric Pelloux-Prayer 		out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET_IN_PROGRESS;
608489763afSPierre-Eric Pelloux-Prayer 
60905adfd80SLuben Tuikov 	if (adev->ras_enabled && con) {
61005adfd80SLuben Tuikov 		/* Return the cached values in O(1),
61105adfd80SLuben Tuikov 		 * and schedule delayed work to cache
61205adfd80SLuben Tuikov 		 * new vaues.
61305adfd80SLuben Tuikov 		 */
61405adfd80SLuben Tuikov 		int ce_count, ue_count;
61505adfd80SLuben Tuikov 
61605adfd80SLuben Tuikov 		ce_count = atomic_read(&con->ras_ce_count);
61705adfd80SLuben Tuikov 		ue_count = atomic_read(&con->ras_ue_count);
61805adfd80SLuben Tuikov 
61905adfd80SLuben Tuikov 		if (ce_count != ctx->ras_counter_ce) {
62005adfd80SLuben Tuikov 			ctx->ras_counter_ce = ce_count;
62105adfd80SLuben Tuikov 			out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_CE;
62205adfd80SLuben Tuikov 		}
62305adfd80SLuben Tuikov 
62405adfd80SLuben Tuikov 		if (ue_count != ctx->ras_counter_ue) {
62505adfd80SLuben Tuikov 			ctx->ras_counter_ue = ue_count;
62605adfd80SLuben Tuikov 			out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_UE;
62705adfd80SLuben Tuikov 		}
62805adfd80SLuben Tuikov 
62905adfd80SLuben Tuikov 		schedule_delayed_work(&con->ras_counte_delay_work,
63005adfd80SLuben Tuikov 				      msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
63105adfd80SLuben Tuikov 	}
63205adfd80SLuben Tuikov 
633bc1b1bf6SMonk Liu 	mutex_unlock(&mgr->lock);
634bc1b1bf6SMonk Liu 	return 0;
635bc1b1bf6SMonk Liu }
636bc1b1bf6SMonk Liu 
amdgpu_ctx_stable_pstate(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,uint32_t id,bool set,u32 * stable_pstate)6378cda7a4fSAlex Deucher static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev,
6388cda7a4fSAlex Deucher 				    struct amdgpu_fpriv *fpriv, uint32_t id,
6398cda7a4fSAlex Deucher 				    bool set, u32 *stable_pstate)
6408cda7a4fSAlex Deucher {
6418cda7a4fSAlex Deucher 	struct amdgpu_ctx *ctx;
6428cda7a4fSAlex Deucher 	struct amdgpu_ctx_mgr *mgr;
6438cda7a4fSAlex Deucher 	int r;
6448cda7a4fSAlex Deucher 
6458cda7a4fSAlex Deucher 	if (!fpriv)
6468cda7a4fSAlex Deucher 		return -EINVAL;
6478cda7a4fSAlex Deucher 
6488cda7a4fSAlex Deucher 	mgr = &fpriv->ctx_mgr;
6498cda7a4fSAlex Deucher 	mutex_lock(&mgr->lock);
6508cda7a4fSAlex Deucher 	ctx = idr_find(&mgr->ctx_handles, id);
6518cda7a4fSAlex Deucher 	if (!ctx) {
6528cda7a4fSAlex Deucher 		mutex_unlock(&mgr->lock);
6538cda7a4fSAlex Deucher 		return -EINVAL;
6548cda7a4fSAlex Deucher 	}
6558cda7a4fSAlex Deucher 
6568cda7a4fSAlex Deucher 	if (set)
6578cda7a4fSAlex Deucher 		r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
6588cda7a4fSAlex Deucher 	else
6598cda7a4fSAlex Deucher 		r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
6608cda7a4fSAlex Deucher 
6618cda7a4fSAlex Deucher 	mutex_unlock(&mgr->lock);
6628cda7a4fSAlex Deucher 	return r;
6638cda7a4fSAlex Deucher }
6648cda7a4fSAlex Deucher 
amdgpu_ctx_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)665d38ceaf9SAlex Deucher int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
666d38ceaf9SAlex Deucher 		     struct drm_file *filp)
667d38ceaf9SAlex Deucher {
668d38ceaf9SAlex Deucher 	int r;
6698cda7a4fSAlex Deucher 	uint32_t id, stable_pstate;
67084d588c3SNirmoy Das 	int32_t priority;
671d38ceaf9SAlex Deucher 
672d38ceaf9SAlex Deucher 	union drm_amdgpu_ctx *args = data;
6731348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(dev);
674d38ceaf9SAlex Deucher 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
675d38ceaf9SAlex Deucher 
676d38ceaf9SAlex Deucher 	id = args->in.ctx_id;
67784d588c3SNirmoy Das 	priority = args->in.priority;
678c2636dc5SAndres Rodriguez 
679d3df66fdSLuben Tuikov 	/* For backwards compatibility, we need to accept ioctls with garbage
680d3df66fdSLuben Tuikov 	 * in the priority field. Garbage values in the priority field, result
681d3df66fdSLuben Tuikov 	 * in the priority being set to NORMAL.
682d3df66fdSLuben Tuikov 	 */
68384d588c3SNirmoy Das 	if (!amdgpu_ctx_priority_is_valid(priority))
68484d588c3SNirmoy Das 		priority = AMDGPU_CTX_PRIORITY_NORMAL;
685d38ceaf9SAlex Deucher 
686d38ceaf9SAlex Deucher 	switch (args->in.op) {
687d38ceaf9SAlex Deucher 	case AMDGPU_CTX_OP_ALLOC_CTX:
688*0573a1e2SBas Nieuwenhuizen 		if (args->in.flags)
689*0573a1e2SBas Nieuwenhuizen 			return -EINVAL;
690c2636dc5SAndres Rodriguez 		r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
691d38ceaf9SAlex Deucher 		args->out.alloc.ctx_id = id;
692d38ceaf9SAlex Deucher 		break;
693d38ceaf9SAlex Deucher 	case AMDGPU_CTX_OP_FREE_CTX:
694*0573a1e2SBas Nieuwenhuizen 		if (args->in.flags)
695*0573a1e2SBas Nieuwenhuizen 			return -EINVAL;
69647f38501SChristian König 		r = amdgpu_ctx_free(fpriv, id);
697d38ceaf9SAlex Deucher 		break;
698d38ceaf9SAlex Deucher 	case AMDGPU_CTX_OP_QUERY_STATE:
699*0573a1e2SBas Nieuwenhuizen 		if (args->in.flags)
700*0573a1e2SBas Nieuwenhuizen 			return -EINVAL;
701d94aed5aSMarek Olšák 		r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
702d38ceaf9SAlex Deucher 		break;
703bc1b1bf6SMonk Liu 	case AMDGPU_CTX_OP_QUERY_STATE2:
704*0573a1e2SBas Nieuwenhuizen 		if (args->in.flags)
705*0573a1e2SBas Nieuwenhuizen 			return -EINVAL;
706bc1b1bf6SMonk Liu 		r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
707bc1b1bf6SMonk Liu 		break;
7088cda7a4fSAlex Deucher 	case AMDGPU_CTX_OP_GET_STABLE_PSTATE:
7098cda7a4fSAlex Deucher 		if (args->in.flags)
7108cda7a4fSAlex Deucher 			return -EINVAL;
7118cda7a4fSAlex Deucher 		r = amdgpu_ctx_stable_pstate(adev, fpriv, id, false, &stable_pstate);
712eed1a5c7STom Rix 		if (!r)
7138cda7a4fSAlex Deucher 			args->out.pstate.flags = stable_pstate;
7148cda7a4fSAlex Deucher 		break;
7158cda7a4fSAlex Deucher 	case AMDGPU_CTX_OP_SET_STABLE_PSTATE:
7168cda7a4fSAlex Deucher 		if (args->in.flags & ~AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK)
7178cda7a4fSAlex Deucher 			return -EINVAL;
7188cda7a4fSAlex Deucher 		stable_pstate = args->in.flags & AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK;
7198cda7a4fSAlex Deucher 		if (stable_pstate > AMDGPU_CTX_STABLE_PSTATE_PEAK)
7208cda7a4fSAlex Deucher 			return -EINVAL;
7218cda7a4fSAlex Deucher 		r = amdgpu_ctx_stable_pstate(adev, fpriv, id, true, &stable_pstate);
7228cda7a4fSAlex Deucher 		break;
723d38ceaf9SAlex Deucher 	default:
724d38ceaf9SAlex Deucher 		return -EINVAL;
725d38ceaf9SAlex Deucher 	}
726d38ceaf9SAlex Deucher 
727d38ceaf9SAlex Deucher 	return r;
728d38ceaf9SAlex Deucher }
72966b3cf2aSJammy Zhou 
amdgpu_ctx_get(struct amdgpu_fpriv * fpriv,uint32_t id)73066b3cf2aSJammy Zhou struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
73166b3cf2aSJammy Zhou {
73266b3cf2aSJammy Zhou 	struct amdgpu_ctx *ctx;
73323ca0e4eSChunming Zhou 	struct amdgpu_ctx_mgr *mgr;
73423ca0e4eSChunming Zhou 
73523ca0e4eSChunming Zhou 	if (!fpriv)
73623ca0e4eSChunming Zhou 		return NULL;
73723ca0e4eSChunming Zhou 
73823ca0e4eSChunming Zhou 	mgr = &fpriv->ctx_mgr;
73966b3cf2aSJammy Zhou 
74066b3cf2aSJammy Zhou 	mutex_lock(&mgr->lock);
74166b3cf2aSJammy Zhou 	ctx = idr_find(&mgr->ctx_handles, id);
74266b3cf2aSJammy Zhou 	if (ctx)
74366b3cf2aSJammy Zhou 		kref_get(&ctx->refcount);
74466b3cf2aSJammy Zhou 	mutex_unlock(&mgr->lock);
74566b3cf2aSJammy Zhou 	return ctx;
74666b3cf2aSJammy Zhou }
74766b3cf2aSJammy Zhou 
amdgpu_ctx_put(struct amdgpu_ctx * ctx)74866b3cf2aSJammy Zhou int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
74966b3cf2aSJammy Zhou {
75066b3cf2aSJammy Zhou 	if (ctx == NULL)
75166b3cf2aSJammy Zhou 		return -EINVAL;
75266b3cf2aSJammy Zhou 
75366b3cf2aSJammy Zhou 	kref_put(&ctx->refcount, amdgpu_ctx_do_release);
75466b3cf2aSJammy Zhou 	return 0;
75566b3cf2aSJammy Zhou }
75621c16bf6SChristian König 
amdgpu_ctx_add_fence(struct amdgpu_ctx * ctx,struct drm_sched_entity * entity,struct dma_fence * fence)75769493c03SChristian König uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
7580d346a14SChristian König 			      struct drm_sched_entity *entity,
75969493c03SChristian König 			      struct dma_fence *fence)
76021c16bf6SChristian König {
7611b1f2fecSChristian König 	struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
7621b1f2fecSChristian König 	uint64_t seq = centity->sequence;
763f54d1867SChris Wilson 	struct dma_fence *other = NULL;
7640d346a14SChristian König 	unsigned idx = 0;
76521c16bf6SChristian König 
7665b011235SChunming Zhou 	idx = seq & (amdgpu_sched_jobs - 1);
7671b1f2fecSChristian König 	other = centity->fences[idx];
76869493c03SChristian König 	WARN_ON(other && !dma_fence_is_signaled(other));
76921c16bf6SChristian König 
770f54d1867SChris Wilson 	dma_fence_get(fence);
77121c16bf6SChristian König 
77221c16bf6SChristian König 	spin_lock(&ctx->ring_lock);
7731b1f2fecSChristian König 	centity->fences[idx] = fence;
7741b1f2fecSChristian König 	centity->sequence++;
77521c16bf6SChristian König 	spin_unlock(&ctx->ring_lock);
77621c16bf6SChristian König 
777af0b5416SChristian König 	atomic64_add(ktime_to_ns(amdgpu_ctx_fence_time(other)),
778af0b5416SChristian König 		     &ctx->mgr->time_spend[centity->hw_ip]);
779af0b5416SChristian König 
780f54d1867SChris Wilson 	dma_fence_put(other);
78169493c03SChristian König 	return seq;
78221c16bf6SChristian König }
78321c16bf6SChristian König 
amdgpu_ctx_get_fence(struct amdgpu_ctx * ctx,struct drm_sched_entity * entity,uint64_t seq)784f54d1867SChris Wilson struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
7850d346a14SChristian König 				       struct drm_sched_entity *entity,
7860d346a14SChristian König 				       uint64_t seq)
78721c16bf6SChristian König {
7881b1f2fecSChristian König 	struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
789f54d1867SChris Wilson 	struct dma_fence *fence;
79021c16bf6SChristian König 
79121c16bf6SChristian König 	spin_lock(&ctx->ring_lock);
792b43a9a7eSChunming Zhou 
793d7b1eeb2SMonk Liu 	if (seq == ~0ull)
7941b1f2fecSChristian König 		seq = centity->sequence - 1;
795d7b1eeb2SMonk Liu 
7961b1f2fecSChristian König 	if (seq >= centity->sequence) {
79721c16bf6SChristian König 		spin_unlock(&ctx->ring_lock);
79821c16bf6SChristian König 		return ERR_PTR(-EINVAL);
79921c16bf6SChristian König 	}
80021c16bf6SChristian König 
801b43a9a7eSChunming Zhou 
8021b1f2fecSChristian König 	if (seq + amdgpu_sched_jobs < centity->sequence) {
80321c16bf6SChristian König 		spin_unlock(&ctx->ring_lock);
80421c16bf6SChristian König 		return NULL;
80521c16bf6SChristian König 	}
80621c16bf6SChristian König 
8071b1f2fecSChristian König 	fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
80821c16bf6SChristian König 	spin_unlock(&ctx->ring_lock);
80921c16bf6SChristian König 
81021c16bf6SChristian König 	return fence;
81121c16bf6SChristian König }
812efd4ccb5SChristian König 
amdgpu_ctx_set_entity_priority(struct amdgpu_ctx * ctx,struct amdgpu_ctx_entity * aentity,int hw_ip,int32_t priority)8132316a86bSNirmoy Das static void amdgpu_ctx_set_entity_priority(struct amdgpu_ctx *ctx,
8142316a86bSNirmoy Das 					   struct amdgpu_ctx_entity *aentity,
8152316a86bSNirmoy Das 					   int hw_ip,
81684d588c3SNirmoy Das 					   int32_t priority)
8172316a86bSNirmoy Das {
81869493c03SChristian König 	struct amdgpu_device *adev = ctx->mgr->adev;
8191c6d567bSNirmoy Das 	unsigned int hw_prio;
8202316a86bSNirmoy Das 	struct drm_gpu_scheduler **scheds = NULL;
8212316a86bSNirmoy Das 	unsigned num_scheds;
8222316a86bSNirmoy Das 
8232316a86bSNirmoy Das 	/* set sw priority */
82484d588c3SNirmoy Das 	drm_sched_entity_set_priority(&aentity->entity,
82584d588c3SNirmoy Das 				      amdgpu_ctx_to_drm_sched_prio(priority));
8262316a86bSNirmoy Das 
8272316a86bSNirmoy Das 	/* set hw priority */
828b07d1d73SArunpravin Paneer Selvam 	if (hw_ip == AMDGPU_HW_IP_COMPUTE || hw_ip == AMDGPU_HW_IP_GFX) {
82984d588c3SNirmoy Das 		hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
8301c6d567bSNirmoy Das 		hw_prio = array_index_nospec(hw_prio, AMDGPU_RING_PRIO_MAX);
8311c6d567bSNirmoy Das 		scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
8321c6d567bSNirmoy Das 		num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
8332316a86bSNirmoy Das 		drm_sched_entity_modify_sched(&aentity->entity, scheds,
8342316a86bSNirmoy Das 					      num_scheds);
8352316a86bSNirmoy Das 	}
8362316a86bSNirmoy Das }
8372316a86bSNirmoy Das 
amdgpu_ctx_priority_override(struct amdgpu_ctx * ctx,int32_t priority)838c23be4aeSAndres Rodriguez void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
83984d588c3SNirmoy Das 				  int32_t priority)
840c23be4aeSAndres Rodriguez {
84184d588c3SNirmoy Das 	int32_t ctx_prio;
842977f7e10SNirmoy Das 	unsigned i, j;
843c23be4aeSAndres Rodriguez 
844c23be4aeSAndres Rodriguez 	ctx->override_priority = priority;
845c23be4aeSAndres Rodriguez 
84684d588c3SNirmoy Das 	ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
847c23be4aeSAndres Rodriguez 			ctx->init_priority : ctx->override_priority;
848977f7e10SNirmoy Das 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
849977f7e10SNirmoy Das 		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
850977f7e10SNirmoy Das 			if (!ctx->entities[i][j])
851977f7e10SNirmoy Das 				continue;
852c23be4aeSAndres Rodriguez 
8532316a86bSNirmoy Das 			amdgpu_ctx_set_entity_priority(ctx, ctx->entities[i][j],
8542316a86bSNirmoy Das 						       i, ctx_prio);
855c23be4aeSAndres Rodriguez 		}
856c23be4aeSAndres Rodriguez 	}
857977f7e10SNirmoy Das }
858c23be4aeSAndres Rodriguez 
amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx * ctx,struct drm_sched_entity * entity)8590d346a14SChristian König int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
8600d346a14SChristian König 			       struct drm_sched_entity *entity)
8610ae94444SAndrey Grodzovsky {
8621b1f2fecSChristian König 	struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
86375e1cafdSChristian König 	struct dma_fence *other;
86475e1cafdSChristian König 	unsigned idx;
86575e1cafdSChristian König 	long r;
8660ae94444SAndrey Grodzovsky 
86775e1cafdSChristian König 	spin_lock(&ctx->ring_lock);
86875e1cafdSChristian König 	idx = centity->sequence & (amdgpu_sched_jobs - 1);
86975e1cafdSChristian König 	other = dma_fence_get(centity->fences[idx]);
87075e1cafdSChristian König 	spin_unlock(&ctx->ring_lock);
87175e1cafdSChristian König 
87275e1cafdSChristian König 	if (!other)
87375e1cafdSChristian König 		return 0;
87475e1cafdSChristian König 
875719a39a1SAndrey Grodzovsky 	r = dma_fence_wait(other, true);
87675e1cafdSChristian König 	if (r < 0 && r != -ERESTARTSYS)
8770ae94444SAndrey Grodzovsky 		DRM_ERROR("Error (%ld) waiting for fence!\n", r);
878719a39a1SAndrey Grodzovsky 
87975e1cafdSChristian König 	dma_fence_put(other);
8800ae94444SAndrey Grodzovsky 	return r;
8810ae94444SAndrey Grodzovsky }
8820ae94444SAndrey Grodzovsky 
amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr * mgr,struct amdgpu_device * adev)88369493c03SChristian König void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
88469493c03SChristian König 			 struct amdgpu_device *adev)
885efd4ccb5SChristian König {
886af0b5416SChristian König 	unsigned int i;
887af0b5416SChristian König 
88869493c03SChristian König 	mgr->adev = adev;
889efd4ccb5SChristian König 	mutex_init(&mgr->lock);
8902ddd1e6cSDanilo Krummrich 	idr_init_base(&mgr->ctx_handles, 1);
891af0b5416SChristian König 
892af0b5416SChristian König 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
893af0b5416SChristian König 		atomic64_set(&mgr->time_spend[i], 0);
894efd4ccb5SChristian König }
895efd4ccb5SChristian König 
amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr * mgr,long timeout)89656753e73SChristian König long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
8978ee3a52eSEmily Deng {
8988ee3a52eSEmily Deng 	struct amdgpu_ctx *ctx;
8998ee3a52eSEmily Deng 	struct idr *idp;
900977f7e10SNirmoy Das 	uint32_t id, i, j;
9018ee3a52eSEmily Deng 
9028ee3a52eSEmily Deng 	idp = &mgr->ctx_handles;
9038ee3a52eSEmily Deng 
90448ad368aSAndrey Grodzovsky 	mutex_lock(&mgr->lock);
9058ee3a52eSEmily Deng 	idr_for_each_entry(idp, ctx, id) {
906977f7e10SNirmoy Das 		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
907977f7e10SNirmoy Das 			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
9081b1f2fecSChristian König 				struct drm_sched_entity *entity;
90920b6b788SAndrey Grodzovsky 
910977f7e10SNirmoy Das 				if (!ctx->entities[i][j])
911977f7e10SNirmoy Das 					continue;
912977f7e10SNirmoy Das 
913977f7e10SNirmoy Das 				entity = &ctx->entities[i][j]->entity;
91456753e73SChristian König 				timeout = drm_sched_entity_flush(entity, timeout);
9158ee3a52eSEmily Deng 			}
9168ee3a52eSEmily Deng 		}
917977f7e10SNirmoy Das 	}
91848ad368aSAndrey Grodzovsky 	mutex_unlock(&mgr->lock);
91956753e73SChristian König 	return timeout;
92020b6b788SAndrey Grodzovsky }
9218ee3a52eSEmily Deng 
amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr * mgr)922c49d8280SAndrey Grodzovsky void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
9238ee3a52eSEmily Deng {
9248ee3a52eSEmily Deng 	struct amdgpu_ctx *ctx;
9258ee3a52eSEmily Deng 	struct idr *idp;
926977f7e10SNirmoy Das 	uint32_t id, i, j;
9278ee3a52eSEmily Deng 
9288ee3a52eSEmily Deng 	idp = &mgr->ctx_handles;
9298ee3a52eSEmily Deng 
9308ee3a52eSEmily Deng 	idr_for_each_entry(idp, ctx, id) {
9311b1f2fecSChristian König 		if (kref_read(&ctx->refcount) != 1) {
9328ee3a52eSEmily Deng 			DRM_ERROR("ctx %p is still alive\n", ctx);
9331b1f2fecSChristian König 			continue;
9348ee3a52eSEmily Deng 		}
9351b1f2fecSChristian König 
936977f7e10SNirmoy Das 		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
937977f7e10SNirmoy Das 			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
938977f7e10SNirmoy Das 				struct drm_sched_entity *entity;
939977f7e10SNirmoy Das 
940977f7e10SNirmoy Das 				if (!ctx->entities[i][j])
941977f7e10SNirmoy Das 					continue;
942977f7e10SNirmoy Das 
943977f7e10SNirmoy Das 				entity = &ctx->entities[i][j]->entity;
944977f7e10SNirmoy Das 				drm_sched_entity_fini(entity);
945977f7e10SNirmoy Das 			}
946977f7e10SNirmoy Das 		}
9478ee3a52eSEmily Deng 	}
94820b6b788SAndrey Grodzovsky }
9498ee3a52eSEmily Deng 
amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr * mgr)950efd4ccb5SChristian König void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
951efd4ccb5SChristian König {
952efd4ccb5SChristian König 	struct amdgpu_ctx *ctx;
953efd4ccb5SChristian König 	struct idr *idp;
954efd4ccb5SChristian König 	uint32_t id;
955efd4ccb5SChristian König 
956c49d8280SAndrey Grodzovsky 	amdgpu_ctx_mgr_entity_fini(mgr);
9578ee3a52eSEmily Deng 
958efd4ccb5SChristian König 	idp = &mgr->ctx_handles;
959efd4ccb5SChristian König 
960efd4ccb5SChristian König 	idr_for_each_entry(idp, ctx, id) {
9618ee3a52eSEmily Deng 		if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
962efd4ccb5SChristian König 			DRM_ERROR("ctx %p is still alive\n", ctx);
963efd4ccb5SChristian König 	}
964efd4ccb5SChristian König 
965efd4ccb5SChristian König 	idr_destroy(&mgr->ctx_handles);
966efd4ccb5SChristian König 	mutex_destroy(&mgr->lock);
967efd4ccb5SChristian König }
96887444254SRoy Sun 
amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr * mgr,ktime_t usage[AMDGPU_HW_IP_NUM])969af0b5416SChristian König void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
970af0b5416SChristian König 			  ktime_t usage[AMDGPU_HW_IP_NUM])
97187444254SRoy Sun {
97287444254SRoy Sun 	struct amdgpu_ctx *ctx;
973af0b5416SChristian König 	unsigned int hw_ip, i;
97487444254SRoy Sun 	uint32_t id;
97587444254SRoy Sun 
976af0b5416SChristian König 	/*
977af0b5416SChristian König 	 * This is a little bit racy because it can be that a ctx or a fence are
978af0b5416SChristian König 	 * destroyed just in the moment we try to account them. But that is ok
979af0b5416SChristian König 	 * since exactly that case is explicitely allowed by the interface.
9805c439c38SDavid M Nieto 	 */
981af0b5416SChristian König 	mutex_lock(&mgr->lock);
982af0b5416SChristian König 	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
983af0b5416SChristian König 		uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
9845c439c38SDavid M Nieto 
985af0b5416SChristian König 		usage[hw_ip] = ns_to_ktime(ns);
98687444254SRoy Sun 	}
98787444254SRoy Sun 
988af0b5416SChristian König 	idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
989af0b5416SChristian König 		for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
990af0b5416SChristian König 			for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
991af0b5416SChristian König 				struct amdgpu_ctx_entity *centity;
992af0b5416SChristian König 				ktime_t spend;
99387444254SRoy Sun 
994af0b5416SChristian König 				centity = ctx->entities[hw_ip][i];
995af0b5416SChristian König 				if (!centity)
996af0b5416SChristian König 					continue;
997af0b5416SChristian König 				spend = amdgpu_ctx_entity_time(ctx, centity);
998af0b5416SChristian König 				usage[hw_ip] = ktime_add(usage[hw_ip], spend);
999af0b5416SChristian König 			}
1000af0b5416SChristian König 		}
1001af0b5416SChristian König 	}
1002af0b5416SChristian König 	mutex_unlock(&mgr->lock);
100387444254SRoy Sun }
1004