1 /*
2  * Copyright 2015 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  * Authors: monk liu <monk.liu@amd.com>
23  */
24 
25 #include <drm/drm_auth.h>
26 #include <drm/drm_drv.h>
27 #include "amdgpu.h"
28 #include "amdgpu_sched.h"
29 #include "amdgpu_ras.h"
30 #include <linux/nospec.h>
31 
32 #define to_amdgpu_ctx_entity(e)	\
33 	container_of((e), struct amdgpu_ctx_entity, entity)
34 
35 const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
36 	[AMDGPU_HW_IP_GFX]	=	1,
37 	[AMDGPU_HW_IP_COMPUTE]	=	4,
38 	[AMDGPU_HW_IP_DMA]	=	2,
39 	[AMDGPU_HW_IP_UVD]	=	1,
40 	[AMDGPU_HW_IP_VCE]	=	1,
41 	[AMDGPU_HW_IP_UVD_ENC]	=	1,
42 	[AMDGPU_HW_IP_VCN_DEC]	=	1,
43 	[AMDGPU_HW_IP_VCN_ENC]	=	1,
44 	[AMDGPU_HW_IP_VCN_JPEG]	=	1,
45 };
46 
amdgpu_ctx_priority_is_valid(int32_t ctx_prio)47 bool amdgpu_ctx_priority_is_valid(int32_t ctx_prio)
48 {
49 	switch (ctx_prio) {
50 	case AMDGPU_CTX_PRIORITY_UNSET:
51 	case AMDGPU_CTX_PRIORITY_VERY_LOW:
52 	case AMDGPU_CTX_PRIORITY_LOW:
53 	case AMDGPU_CTX_PRIORITY_NORMAL:
54 	case AMDGPU_CTX_PRIORITY_HIGH:
55 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
56 		return true;
57 	default:
58 		return false;
59 	}
60 }
61 
62 static enum drm_sched_priority
amdgpu_ctx_to_drm_sched_prio(int32_t ctx_prio)63 amdgpu_ctx_to_drm_sched_prio(int32_t ctx_prio)
64 {
65 	switch (ctx_prio) {
66 	case AMDGPU_CTX_PRIORITY_UNSET:
67 		return DRM_SCHED_PRIORITY_UNSET;
68 
69 	case AMDGPU_CTX_PRIORITY_VERY_LOW:
70 		return DRM_SCHED_PRIORITY_MIN;
71 
72 	case AMDGPU_CTX_PRIORITY_LOW:
73 		return DRM_SCHED_PRIORITY_MIN;
74 
75 	case AMDGPU_CTX_PRIORITY_NORMAL:
76 		return DRM_SCHED_PRIORITY_NORMAL;
77 
78 	case AMDGPU_CTX_PRIORITY_HIGH:
79 		return DRM_SCHED_PRIORITY_HIGH;
80 
81 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
82 		return DRM_SCHED_PRIORITY_HIGH;
83 
84 	/* This should not happen as we sanitized userspace provided priority
85 	 * already, WARN if this happens.
86 	 */
87 	default:
88 		WARN(1, "Invalid context priority %d\n", ctx_prio);
89 		return DRM_SCHED_PRIORITY_NORMAL;
90 	}
91 
92 }
93 
amdgpu_ctx_priority_permit(struct drm_file * filp,int32_t priority)94 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
95 				      int32_t priority)
96 {
97 	if (!amdgpu_ctx_priority_is_valid(priority))
98 		return -EINVAL;
99 
100 	/* NORMAL and below are accessible by everyone */
101 	if (priority <= AMDGPU_CTX_PRIORITY_NORMAL)
102 		return 0;
103 
104 	if (capable(CAP_SYS_NICE))
105 		return 0;
106 
107 	if (drm_is_current_master(filp))
108 		return 0;
109 
110 	return -EACCES;
111 }
112 
amdgpu_ctx_prio_to_compute_prio(int32_t prio)113 static enum amdgpu_gfx_pipe_priority amdgpu_ctx_prio_to_compute_prio(int32_t prio)
114 {
115 	switch (prio) {
116 	case AMDGPU_CTX_PRIORITY_HIGH:
117 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
118 		return AMDGPU_GFX_PIPE_PRIO_HIGH;
119 	default:
120 		return AMDGPU_GFX_PIPE_PRIO_NORMAL;
121 	}
122 }
123 
amdgpu_ctx_sched_prio_to_ring_prio(int32_t prio)124 static enum amdgpu_ring_priority_level amdgpu_ctx_sched_prio_to_ring_prio(int32_t prio)
125 {
126 	switch (prio) {
127 	case AMDGPU_CTX_PRIORITY_HIGH:
128 		return AMDGPU_RING_PRIO_1;
129 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
130 		return AMDGPU_RING_PRIO_2;
131 	default:
132 		return AMDGPU_RING_PRIO_0;
133 	}
134 }
135 
amdgpu_ctx_get_hw_prio(struct amdgpu_ctx * ctx,u32 hw_ip)136 static unsigned int amdgpu_ctx_get_hw_prio(struct amdgpu_ctx *ctx, u32 hw_ip)
137 {
138 	struct amdgpu_device *adev = ctx->mgr->adev;
139 	unsigned int hw_prio;
140 	int32_t ctx_prio;
141 
142 	ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
143 			ctx->init_priority : ctx->override_priority;
144 
145 	switch (hw_ip) {
146 	case AMDGPU_HW_IP_COMPUTE:
147 		hw_prio = amdgpu_ctx_prio_to_compute_prio(ctx_prio);
148 		break;
149 	case AMDGPU_HW_IP_VCE:
150 	case AMDGPU_HW_IP_VCN_ENC:
151 		hw_prio = amdgpu_ctx_sched_prio_to_ring_prio(ctx_prio);
152 		break;
153 	default:
154 		hw_prio = AMDGPU_RING_PRIO_DEFAULT;
155 		break;
156 	}
157 
158 	hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
159 	if (adev->gpu_sched[hw_ip][hw_prio].num_scheds == 0)
160 		hw_prio = AMDGPU_RING_PRIO_DEFAULT;
161 
162 	return hw_prio;
163 }
164 
165 /* Calculate the time spend on the hw */
amdgpu_ctx_fence_time(struct dma_fence * fence)166 static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence)
167 {
168 	struct drm_sched_fence *s_fence;
169 
170 	if (!fence)
171 		return ns_to_ktime(0);
172 
173 	/* When the fence is not even scheduled it can't have spend time */
174 	s_fence = to_drm_sched_fence(fence);
175 	if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled.flags))
176 		return ns_to_ktime(0);
177 
178 	/* When it is still running account how much already spend */
179 	if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.flags))
180 		return ktime_sub(ktime_get(), s_fence->scheduled.timestamp);
181 
182 	return ktime_sub(s_fence->finished.timestamp,
183 			 s_fence->scheduled.timestamp);
184 }
185 
amdgpu_ctx_entity_time(struct amdgpu_ctx * ctx,struct amdgpu_ctx_entity * centity)186 static ktime_t amdgpu_ctx_entity_time(struct amdgpu_ctx *ctx,
187 				      struct amdgpu_ctx_entity *centity)
188 {
189 	ktime_t res = ns_to_ktime(0);
190 	uint32_t i;
191 
192 	spin_lock(&ctx->ring_lock);
193 	for (i = 0; i < amdgpu_sched_jobs; i++) {
194 		res = ktime_add(res, amdgpu_ctx_fence_time(centity->fences[i]));
195 	}
196 	spin_unlock(&ctx->ring_lock);
197 	return res;
198 }
199 
amdgpu_ctx_init_entity(struct amdgpu_ctx * ctx,u32 hw_ip,const u32 ring)200 static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
201 				  const u32 ring)
202 {
203 	struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
204 	struct amdgpu_device *adev = ctx->mgr->adev;
205 	struct amdgpu_ctx_entity *entity;
206 	enum drm_sched_priority drm_prio;
207 	unsigned int hw_prio, num_scheds;
208 	int32_t ctx_prio;
209 	int r;
210 
211 	entity = kzalloc(struct_size(entity, fences, amdgpu_sched_jobs),
212 			 GFP_KERNEL);
213 	if (!entity)
214 		return  -ENOMEM;
215 
216 	ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
217 			ctx->init_priority : ctx->override_priority;
218 	entity->hw_ip = hw_ip;
219 	entity->sequence = 1;
220 	hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
221 	drm_prio = amdgpu_ctx_to_drm_sched_prio(ctx_prio);
222 
223 	hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
224 	scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
225 	num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
226 
227 	/* disable load balance if the hw engine retains context among dependent jobs */
228 	if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
229 	    hw_ip == AMDGPU_HW_IP_VCN_DEC ||
230 	    hw_ip == AMDGPU_HW_IP_UVD_ENC ||
231 	    hw_ip == AMDGPU_HW_IP_UVD) {
232 		sched = drm_sched_pick_best(scheds, num_scheds);
233 		scheds = &sched;
234 		num_scheds = 1;
235 	}
236 
237 	r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
238 				  &ctx->guilty);
239 	if (r)
240 		goto error_free_entity;
241 
242 	/* It's not an error if we fail to install the new entity */
243 	if (cmpxchg(&ctx->entities[hw_ip][ring], NULL, entity))
244 		goto cleanup_entity;
245 
246 	return 0;
247 
248 cleanup_entity:
249 	drm_sched_entity_fini(&entity->entity);
250 
251 error_free_entity:
252 	kfree(entity);
253 
254 	return r;
255 }
256 
amdgpu_ctx_fini_entity(struct amdgpu_ctx_entity * entity)257 static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_ctx_entity *entity)
258 {
259 	ktime_t res = ns_to_ktime(0);
260 	int i;
261 
262 	if (!entity)
263 		return res;
264 
265 	for (i = 0; i < amdgpu_sched_jobs; ++i) {
266 		res = ktime_add(res, amdgpu_ctx_fence_time(entity->fences[i]));
267 		dma_fence_put(entity->fences[i]);
268 	}
269 
270 	kfree(entity);
271 	return res;
272 }
273 
amdgpu_ctx_get_stable_pstate(struct amdgpu_ctx * ctx,u32 * stable_pstate)274 static int amdgpu_ctx_get_stable_pstate(struct amdgpu_ctx *ctx,
275 					u32 *stable_pstate)
276 {
277 	struct amdgpu_device *adev = ctx->mgr->adev;
278 	enum amd_dpm_forced_level current_level;
279 
280 	current_level = amdgpu_dpm_get_performance_level(adev);
281 
282 	switch (current_level) {
283 	case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
284 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_STANDARD;
285 		break;
286 	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
287 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK;
288 		break;
289 	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
290 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK;
291 		break;
292 	case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
293 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_PEAK;
294 		break;
295 	default:
296 		*stable_pstate = AMDGPU_CTX_STABLE_PSTATE_NONE;
297 		break;
298 	}
299 	return 0;
300 }
301 
amdgpu_ctx_init(struct amdgpu_ctx_mgr * mgr,int32_t priority,struct drm_file * filp,struct amdgpu_ctx * ctx)302 static int amdgpu_ctx_init(struct amdgpu_ctx_mgr *mgr, int32_t priority,
303 			   struct drm_file *filp, struct amdgpu_ctx *ctx)
304 {
305 	u32 current_stable_pstate;
306 	int r;
307 
308 	r = amdgpu_ctx_priority_permit(filp, priority);
309 	if (r)
310 		return r;
311 
312 	memset(ctx, 0, sizeof(*ctx));
313 
314 	kref_init(&ctx->refcount);
315 	ctx->mgr = mgr;
316 	spin_lock_init(&ctx->ring_lock);
317 	mutex_init(&ctx->lock);
318 
319 	ctx->reset_counter = atomic_read(&mgr->adev->gpu_reset_counter);
320 	ctx->reset_counter_query = ctx->reset_counter;
321 	ctx->vram_lost_counter = atomic_read(&mgr->adev->vram_lost_counter);
322 	ctx->init_priority = priority;
323 	ctx->override_priority = AMDGPU_CTX_PRIORITY_UNSET;
324 
325 	r = amdgpu_ctx_get_stable_pstate(ctx, &current_stable_pstate);
326 	if (r)
327 		return r;
328 
329 	ctx->stable_pstate = current_stable_pstate;
330 
331 	return 0;
332 }
333 
amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx * ctx,u32 stable_pstate)334 static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx,
335 					u32 stable_pstate)
336 {
337 	struct amdgpu_device *adev = ctx->mgr->adev;
338 	enum amd_dpm_forced_level level;
339 	u32 current_stable_pstate;
340 	int r;
341 
342 	mutex_lock(&adev->pm.stable_pstate_ctx_lock);
343 	if (adev->pm.stable_pstate_ctx && adev->pm.stable_pstate_ctx != ctx) {
344 		r = -EBUSY;
345 		goto done;
346 	}
347 
348 	r = amdgpu_ctx_get_stable_pstate(ctx, &current_stable_pstate);
349 	if (r || (stable_pstate == current_stable_pstate))
350 		goto done;
351 
352 	switch (stable_pstate) {
353 	case AMDGPU_CTX_STABLE_PSTATE_NONE:
354 		level = AMD_DPM_FORCED_LEVEL_AUTO;
355 		break;
356 	case AMDGPU_CTX_STABLE_PSTATE_STANDARD:
357 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
358 		break;
359 	case AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK:
360 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
361 		break;
362 	case AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK:
363 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
364 		break;
365 	case AMDGPU_CTX_STABLE_PSTATE_PEAK:
366 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
367 		break;
368 	default:
369 		r = -EINVAL;
370 		goto done;
371 	}
372 
373 	r = amdgpu_dpm_force_performance_level(adev, level);
374 
375 	if (level == AMD_DPM_FORCED_LEVEL_AUTO)
376 		adev->pm.stable_pstate_ctx = NULL;
377 	else
378 		adev->pm.stable_pstate_ctx = ctx;
379 done:
380 	mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
381 
382 	return r;
383 }
384 
amdgpu_ctx_fini(struct kref * ref)385 static void amdgpu_ctx_fini(struct kref *ref)
386 {
387 	struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
388 	struct amdgpu_ctx_mgr *mgr = ctx->mgr;
389 	struct amdgpu_device *adev = mgr->adev;
390 	unsigned i, j, idx;
391 
392 	if (!adev)
393 		return;
394 
395 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
396 		for (j = 0; j < AMDGPU_MAX_ENTITY_NUM; ++j) {
397 			ktime_t spend;
398 
399 			spend = amdgpu_ctx_fini_entity(ctx->entities[i][j]);
400 			atomic64_add(ktime_to_ns(spend), &mgr->time_spend[i]);
401 		}
402 	}
403 
404 	if (drm_dev_enter(&adev->ddev, &idx)) {
405 		amdgpu_ctx_set_stable_pstate(ctx, ctx->stable_pstate);
406 		drm_dev_exit(idx);
407 	}
408 
409 	mutex_destroy(&ctx->lock);
410 	kfree(ctx);
411 }
412 
amdgpu_ctx_get_entity(struct amdgpu_ctx * ctx,u32 hw_ip,u32 instance,u32 ring,struct drm_sched_entity ** entity)413 int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
414 			  u32 ring, struct drm_sched_entity **entity)
415 {
416 	int r;
417 
418 	if (hw_ip >= AMDGPU_HW_IP_NUM) {
419 		DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
420 		return -EINVAL;
421 	}
422 
423 	/* Right now all IPs have only one instance - multiple rings. */
424 	if (instance != 0) {
425 		DRM_DEBUG("invalid ip instance: %d\n", instance);
426 		return -EINVAL;
427 	}
428 
429 	if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
430 		DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
431 		return -EINVAL;
432 	}
433 
434 	if (ctx->entities[hw_ip][ring] == NULL) {
435 		r = amdgpu_ctx_init_entity(ctx, hw_ip, ring);
436 		if (r)
437 			return r;
438 	}
439 
440 	*entity = &ctx->entities[hw_ip][ring]->entity;
441 	return 0;
442 }
443 
amdgpu_ctx_alloc(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,struct drm_file * filp,int32_t priority,uint32_t * id)444 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
445 			    struct amdgpu_fpriv *fpriv,
446 			    struct drm_file *filp,
447 			    int32_t priority,
448 			    uint32_t *id)
449 {
450 	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
451 	struct amdgpu_ctx *ctx;
452 	int r;
453 
454 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
455 	if (!ctx)
456 		return -ENOMEM;
457 
458 	mutex_lock(&mgr->lock);
459 	r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
460 	if (r < 0) {
461 		mutex_unlock(&mgr->lock);
462 		kfree(ctx);
463 		return r;
464 	}
465 
466 	*id = (uint32_t)r;
467 	r = amdgpu_ctx_init(mgr, priority, filp, ctx);
468 	if (r) {
469 		idr_remove(&mgr->ctx_handles, *id);
470 		*id = 0;
471 		kfree(ctx);
472 	}
473 	mutex_unlock(&mgr->lock);
474 	return r;
475 }
476 
amdgpu_ctx_do_release(struct kref * ref)477 static void amdgpu_ctx_do_release(struct kref *ref)
478 {
479 	struct amdgpu_ctx *ctx;
480 	u32 i, j;
481 
482 	ctx = container_of(ref, struct amdgpu_ctx, refcount);
483 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
484 		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
485 			if (!ctx->entities[i][j])
486 				continue;
487 
488 			drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
489 		}
490 	}
491 
492 	amdgpu_ctx_fini(ref);
493 }
494 
amdgpu_ctx_free(struct amdgpu_fpriv * fpriv,uint32_t id)495 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
496 {
497 	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
498 	struct amdgpu_ctx *ctx;
499 
500 	mutex_lock(&mgr->lock);
501 	ctx = idr_remove(&mgr->ctx_handles, id);
502 	if (ctx)
503 		kref_put(&ctx->refcount, amdgpu_ctx_do_release);
504 	mutex_unlock(&mgr->lock);
505 	return ctx ? 0 : -EINVAL;
506 }
507 
amdgpu_ctx_query(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,uint32_t id,union drm_amdgpu_ctx_out * out)508 static int amdgpu_ctx_query(struct amdgpu_device *adev,
509 			    struct amdgpu_fpriv *fpriv, uint32_t id,
510 			    union drm_amdgpu_ctx_out *out)
511 {
512 	struct amdgpu_ctx *ctx;
513 	struct amdgpu_ctx_mgr *mgr;
514 	unsigned reset_counter;
515 
516 	if (!fpriv)
517 		return -EINVAL;
518 
519 	mgr = &fpriv->ctx_mgr;
520 	mutex_lock(&mgr->lock);
521 	ctx = idr_find(&mgr->ctx_handles, id);
522 	if (!ctx) {
523 		mutex_unlock(&mgr->lock);
524 		return -EINVAL;
525 	}
526 
527 	/* TODO: these two are always zero */
528 	out->state.flags = 0x0;
529 	out->state.hangs = 0x0;
530 
531 	/* determine if a GPU reset has occured since the last call */
532 	reset_counter = atomic_read(&adev->gpu_reset_counter);
533 	/* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
534 	if (ctx->reset_counter_query == reset_counter)
535 		out->state.reset_status = AMDGPU_CTX_NO_RESET;
536 	else
537 		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
538 	ctx->reset_counter_query = reset_counter;
539 
540 	mutex_unlock(&mgr->lock);
541 	return 0;
542 }
543 
544 #define AMDGPU_RAS_COUNTE_DELAY_MS 3000
545 
amdgpu_ctx_query2(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,uint32_t id,union drm_amdgpu_ctx_out * out)546 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
547 			     struct amdgpu_fpriv *fpriv, uint32_t id,
548 			     union drm_amdgpu_ctx_out *out)
549 {
550 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
551 	struct amdgpu_ctx *ctx;
552 	struct amdgpu_ctx_mgr *mgr;
553 
554 	if (!fpriv)
555 		return -EINVAL;
556 
557 	mgr = &fpriv->ctx_mgr;
558 	mutex_lock(&mgr->lock);
559 	ctx = idr_find(&mgr->ctx_handles, id);
560 	if (!ctx) {
561 		mutex_unlock(&mgr->lock);
562 		return -EINVAL;
563 	}
564 
565 	out->state.flags = 0x0;
566 	out->state.hangs = 0x0;
567 
568 	if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
569 		out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
570 
571 	if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
572 		out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
573 
574 	if (atomic_read(&ctx->guilty))
575 		out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
576 
577 	if (adev->ras_enabled && con) {
578 		/* Return the cached values in O(1),
579 		 * and schedule delayed work to cache
580 		 * new vaues.
581 		 */
582 		int ce_count, ue_count;
583 
584 		ce_count = atomic_read(&con->ras_ce_count);
585 		ue_count = atomic_read(&con->ras_ue_count);
586 
587 		if (ce_count != ctx->ras_counter_ce) {
588 			ctx->ras_counter_ce = ce_count;
589 			out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_CE;
590 		}
591 
592 		if (ue_count != ctx->ras_counter_ue) {
593 			ctx->ras_counter_ue = ue_count;
594 			out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_UE;
595 		}
596 
597 		schedule_delayed_work(&con->ras_counte_delay_work,
598 				      msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
599 	}
600 
601 	mutex_unlock(&mgr->lock);
602 	return 0;
603 }
604 
605 
606 
amdgpu_ctx_stable_pstate(struct amdgpu_device * adev,struct amdgpu_fpriv * fpriv,uint32_t id,bool set,u32 * stable_pstate)607 static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev,
608 				    struct amdgpu_fpriv *fpriv, uint32_t id,
609 				    bool set, u32 *stable_pstate)
610 {
611 	struct amdgpu_ctx *ctx;
612 	struct amdgpu_ctx_mgr *mgr;
613 	int r;
614 
615 	if (!fpriv)
616 		return -EINVAL;
617 
618 	mgr = &fpriv->ctx_mgr;
619 	mutex_lock(&mgr->lock);
620 	ctx = idr_find(&mgr->ctx_handles, id);
621 	if (!ctx) {
622 		mutex_unlock(&mgr->lock);
623 		return -EINVAL;
624 	}
625 
626 	if (set)
627 		r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
628 	else
629 		r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
630 
631 	mutex_unlock(&mgr->lock);
632 	return r;
633 }
634 
amdgpu_ctx_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)635 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
636 		     struct drm_file *filp)
637 {
638 	int r;
639 	uint32_t id, stable_pstate;
640 	int32_t priority;
641 
642 	union drm_amdgpu_ctx *args = data;
643 	struct amdgpu_device *adev = drm_to_adev(dev);
644 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
645 
646 	id = args->in.ctx_id;
647 	priority = args->in.priority;
648 
649 	/* For backwards compatibility reasons, we need to accept
650 	 * ioctls with garbage in the priority field */
651 	if (!amdgpu_ctx_priority_is_valid(priority))
652 		priority = AMDGPU_CTX_PRIORITY_NORMAL;
653 
654 	switch (args->in.op) {
655 	case AMDGPU_CTX_OP_ALLOC_CTX:
656 		r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
657 		args->out.alloc.ctx_id = id;
658 		break;
659 	case AMDGPU_CTX_OP_FREE_CTX:
660 		r = amdgpu_ctx_free(fpriv, id);
661 		break;
662 	case AMDGPU_CTX_OP_QUERY_STATE:
663 		r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
664 		break;
665 	case AMDGPU_CTX_OP_QUERY_STATE2:
666 		r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
667 		break;
668 	case AMDGPU_CTX_OP_GET_STABLE_PSTATE:
669 		if (args->in.flags)
670 			return -EINVAL;
671 		r = amdgpu_ctx_stable_pstate(adev, fpriv, id, false, &stable_pstate);
672 		if (!r)
673 			args->out.pstate.flags = stable_pstate;
674 		break;
675 	case AMDGPU_CTX_OP_SET_STABLE_PSTATE:
676 		if (args->in.flags & ~AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK)
677 			return -EINVAL;
678 		stable_pstate = args->in.flags & AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK;
679 		if (stable_pstate > AMDGPU_CTX_STABLE_PSTATE_PEAK)
680 			return -EINVAL;
681 		r = amdgpu_ctx_stable_pstate(adev, fpriv, id, true, &stable_pstate);
682 		break;
683 	default:
684 		return -EINVAL;
685 	}
686 
687 	return r;
688 }
689 
amdgpu_ctx_get(struct amdgpu_fpriv * fpriv,uint32_t id)690 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
691 {
692 	struct amdgpu_ctx *ctx;
693 	struct amdgpu_ctx_mgr *mgr;
694 
695 	if (!fpriv)
696 		return NULL;
697 
698 	mgr = &fpriv->ctx_mgr;
699 
700 	mutex_lock(&mgr->lock);
701 	ctx = idr_find(&mgr->ctx_handles, id);
702 	if (ctx)
703 		kref_get(&ctx->refcount);
704 	mutex_unlock(&mgr->lock);
705 	return ctx;
706 }
707 
amdgpu_ctx_put(struct amdgpu_ctx * ctx)708 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
709 {
710 	if (ctx == NULL)
711 		return -EINVAL;
712 
713 	kref_put(&ctx->refcount, amdgpu_ctx_do_release);
714 	return 0;
715 }
716 
amdgpu_ctx_add_fence(struct amdgpu_ctx * ctx,struct drm_sched_entity * entity,struct dma_fence * fence)717 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
718 			      struct drm_sched_entity *entity,
719 			      struct dma_fence *fence)
720 {
721 	struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
722 	uint64_t seq = centity->sequence;
723 	struct dma_fence *other = NULL;
724 	unsigned idx = 0;
725 
726 	idx = seq & (amdgpu_sched_jobs - 1);
727 	other = centity->fences[idx];
728 	WARN_ON(other && !dma_fence_is_signaled(other));
729 
730 	dma_fence_get(fence);
731 
732 	spin_lock(&ctx->ring_lock);
733 	centity->fences[idx] = fence;
734 	centity->sequence++;
735 	spin_unlock(&ctx->ring_lock);
736 
737 	atomic64_add(ktime_to_ns(amdgpu_ctx_fence_time(other)),
738 		     &ctx->mgr->time_spend[centity->hw_ip]);
739 
740 	dma_fence_put(other);
741 	return seq;
742 }
743 
amdgpu_ctx_get_fence(struct amdgpu_ctx * ctx,struct drm_sched_entity * entity,uint64_t seq)744 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
745 				       struct drm_sched_entity *entity,
746 				       uint64_t seq)
747 {
748 	struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
749 	struct dma_fence *fence;
750 
751 	spin_lock(&ctx->ring_lock);
752 
753 	if (seq == ~0ull)
754 		seq = centity->sequence - 1;
755 
756 	if (seq >= centity->sequence) {
757 		spin_unlock(&ctx->ring_lock);
758 		return ERR_PTR(-EINVAL);
759 	}
760 
761 
762 	if (seq + amdgpu_sched_jobs < centity->sequence) {
763 		spin_unlock(&ctx->ring_lock);
764 		return NULL;
765 	}
766 
767 	fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
768 	spin_unlock(&ctx->ring_lock);
769 
770 	return fence;
771 }
772 
amdgpu_ctx_set_entity_priority(struct amdgpu_ctx * ctx,struct amdgpu_ctx_entity * aentity,int hw_ip,int32_t priority)773 static void amdgpu_ctx_set_entity_priority(struct amdgpu_ctx *ctx,
774 					   struct amdgpu_ctx_entity *aentity,
775 					   int hw_ip,
776 					   int32_t priority)
777 {
778 	struct amdgpu_device *adev = ctx->mgr->adev;
779 	unsigned int hw_prio;
780 	struct drm_gpu_scheduler **scheds = NULL;
781 	unsigned num_scheds;
782 
783 	/* set sw priority */
784 	drm_sched_entity_set_priority(&aentity->entity,
785 				      amdgpu_ctx_to_drm_sched_prio(priority));
786 
787 	/* set hw priority */
788 	if (hw_ip == AMDGPU_HW_IP_COMPUTE) {
789 		hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
790 		hw_prio = array_index_nospec(hw_prio, AMDGPU_RING_PRIO_MAX);
791 		scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
792 		num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
793 		drm_sched_entity_modify_sched(&aentity->entity, scheds,
794 					      num_scheds);
795 	}
796 }
797 
amdgpu_ctx_priority_override(struct amdgpu_ctx * ctx,int32_t priority)798 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
799 				  int32_t priority)
800 {
801 	int32_t ctx_prio;
802 	unsigned i, j;
803 
804 	ctx->override_priority = priority;
805 
806 	ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
807 			ctx->init_priority : ctx->override_priority;
808 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
809 		for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
810 			if (!ctx->entities[i][j])
811 				continue;
812 
813 			amdgpu_ctx_set_entity_priority(ctx, ctx->entities[i][j],
814 						       i, ctx_prio);
815 		}
816 	}
817 }
818 
amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx * ctx,struct drm_sched_entity * entity)819 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
820 			       struct drm_sched_entity *entity)
821 {
822 	struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
823 	struct dma_fence *other;
824 	unsigned idx;
825 	long r;
826 
827 	spin_lock(&ctx->ring_lock);
828 	idx = centity->sequence & (amdgpu_sched_jobs - 1);
829 	other = dma_fence_get(centity->fences[idx]);
830 	spin_unlock(&ctx->ring_lock);
831 
832 	if (!other)
833 		return 0;
834 
835 	r = dma_fence_wait(other, true);
836 	if (r < 0 && r != -ERESTARTSYS)
837 		DRM_ERROR("Error (%ld) waiting for fence!\n", r);
838 
839 	dma_fence_put(other);
840 	return r;
841 }
842 
amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr * mgr,struct amdgpu_device * adev)843 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
844 			 struct amdgpu_device *adev)
845 {
846 	unsigned int i;
847 
848 	mgr->adev = adev;
849 	mutex_init(&mgr->lock);
850 	idr_init(&mgr->ctx_handles);
851 
852 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
853 		atomic64_set(&mgr->time_spend[i], 0);
854 }
855 
amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr * mgr,long timeout)856 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
857 {
858 	struct amdgpu_ctx *ctx;
859 	struct idr *idp;
860 	uint32_t id, i, j;
861 
862 	idp = &mgr->ctx_handles;
863 
864 	mutex_lock(&mgr->lock);
865 	idr_for_each_entry(idp, ctx, id) {
866 		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
867 			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
868 				struct drm_sched_entity *entity;
869 
870 				if (!ctx->entities[i][j])
871 					continue;
872 
873 				entity = &ctx->entities[i][j]->entity;
874 				timeout = drm_sched_entity_flush(entity, timeout);
875 			}
876 		}
877 	}
878 	mutex_unlock(&mgr->lock);
879 	return timeout;
880 }
881 
amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr * mgr)882 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
883 {
884 	struct amdgpu_ctx *ctx;
885 	struct idr *idp;
886 	uint32_t id, i, j;
887 
888 	idp = &mgr->ctx_handles;
889 
890 	idr_for_each_entry(idp, ctx, id) {
891 		if (kref_read(&ctx->refcount) != 1) {
892 			DRM_ERROR("ctx %p is still alive\n", ctx);
893 			continue;
894 		}
895 
896 		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
897 			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
898 				struct drm_sched_entity *entity;
899 
900 				if (!ctx->entities[i][j])
901 					continue;
902 
903 				entity = &ctx->entities[i][j]->entity;
904 				drm_sched_entity_fini(entity);
905 			}
906 		}
907 	}
908 }
909 
amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr * mgr)910 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
911 {
912 	struct amdgpu_ctx *ctx;
913 	struct idr *idp;
914 	uint32_t id;
915 
916 	amdgpu_ctx_mgr_entity_fini(mgr);
917 
918 	idp = &mgr->ctx_handles;
919 
920 	idr_for_each_entry(idp, ctx, id) {
921 		if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
922 			DRM_ERROR("ctx %p is still alive\n", ctx);
923 	}
924 
925 	idr_destroy(&mgr->ctx_handles);
926 	mutex_destroy(&mgr->lock);
927 }
928 
amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr * mgr,ktime_t usage[AMDGPU_HW_IP_NUM])929 void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
930 			  ktime_t usage[AMDGPU_HW_IP_NUM])
931 {
932 	struct amdgpu_ctx *ctx;
933 	unsigned int hw_ip, i;
934 	uint32_t id;
935 
936 	/*
937 	 * This is a little bit racy because it can be that a ctx or a fence are
938 	 * destroyed just in the moment we try to account them. But that is ok
939 	 * since exactly that case is explicitely allowed by the interface.
940 	 */
941 	mutex_lock(&mgr->lock);
942 	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
943 		uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
944 
945 		usage[hw_ip] = ns_to_ktime(ns);
946 	}
947 
948 	idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
949 		for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
950 			for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
951 				struct amdgpu_ctx_entity *centity;
952 				ktime_t spend;
953 
954 				centity = ctx->entities[hw_ip][i];
955 				if (!centity)
956 					continue;
957 				spend = amdgpu_ctx_entity_time(ctx, centity);
958 				usage[hw_ip] = ktime_add(usage[hw_ip], spend);
959 			}
960 		}
961 	}
962 	mutex_unlock(&mgr->lock);
963 }
964