xref: /linux/drivers/gpu/drm/i915/selftests/intel_scheduler_helpers.c (revision 702648721db590b3425c31ade294000e18808345)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 //#include "gt/intel_engine_user.h"
7 #include "gt/intel_gt.h"
8 #include "i915_drv.h"
9 #include "i915_selftest.h"
10 
11 #include "selftests/intel_scheduler_helpers.h"
12 
13 #define REDUCED_TIMESLICE	5
14 #define REDUCED_PREEMPT		10
15 #define WAIT_FOR_RESET_TIME	10000
16 
17 struct intel_engine_cs *intel_selftest_find_any_engine(struct intel_gt *gt)
18 {
19 	struct intel_engine_cs *engine;
20 	enum intel_engine_id id;
21 
22 	for_each_engine(engine, gt, id)
23 		return engine;
24 
25 	pr_err("No valid engine found!\n");
26 	return NULL;
27 }
28 
29 int intel_selftest_modify_policy(struct intel_engine_cs *engine,
30 				 struct intel_selftest_saved_policy *saved,
31 				 enum selftest_scheduler_modify modify_type)
32 {
33 	int err;
34 
35 	saved->reset = engine->i915->params.reset;
36 	saved->flags = engine->flags;
37 	saved->timeslice = engine->props.timeslice_duration_ms;
38 	saved->preempt_timeout = engine->props.preempt_timeout_ms;
39 
40 	switch (modify_type) {
41 	case SELFTEST_SCHEDULER_MODIFY_FAST_RESET:
42 		/*
43 		 * Enable force pre-emption on time slice expiration
44 		 * together with engine reset on pre-emption timeout.
45 		 * This is required to make the GuC notice and reset
46 		 * the single hanging context.
47 		 * Also, reduce the preemption timeout to something
48 		 * small to speed the test up.
49 		 */
50 		engine->i915->params.reset = 2;
51 		engine->flags |= I915_ENGINE_WANT_FORCED_PREEMPTION;
52 		engine->props.timeslice_duration_ms = REDUCED_TIMESLICE;
53 		engine->props.preempt_timeout_ms = REDUCED_PREEMPT;
54 		break;
55 
56 	case SELFTEST_SCHEDULER_MODIFY_NO_HANGCHECK:
57 		engine->props.preempt_timeout_ms = 0;
58 		break;
59 
60 	default:
61 		pr_err("Invalid scheduler policy modification type: %d!\n", modify_type);
62 		return -EINVAL;
63 	}
64 
65 	if (!intel_engine_uses_guc(engine))
66 		return 0;
67 
68 	err = intel_guc_global_policies_update(&engine->gt->uc.guc);
69 	if (err)
70 		intel_selftest_restore_policy(engine, saved);
71 
72 	return err;
73 }
74 
75 int intel_selftest_restore_policy(struct intel_engine_cs *engine,
76 				  struct intel_selftest_saved_policy *saved)
77 {
78 	/* Restore the original policies */
79 	engine->i915->params.reset = saved->reset;
80 	engine->flags = saved->flags;
81 	engine->props.timeslice_duration_ms = saved->timeslice;
82 	engine->props.preempt_timeout_ms = saved->preempt_timeout;
83 
84 	if (!intel_engine_uses_guc(engine))
85 		return 0;
86 
87 	return intel_guc_global_policies_update(&engine->gt->uc.guc);
88 }
89 
90 int intel_selftest_wait_for_rq(struct i915_request *rq)
91 {
92 	long ret;
93 
94 	ret = i915_request_wait(rq, 0, WAIT_FOR_RESET_TIME);
95 	if (ret < 0)
96 		return ret;
97 
98 	return 0;
99 }
100