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 				 u32 modify_type)
32 
33 {
34 	int err;
35 
36 	saved->reset = engine->i915->params.reset;
37 	saved->flags = engine->flags;
38 	saved->timeslice = engine->props.timeslice_duration_ms;
39 	saved->preempt_timeout = engine->props.preempt_timeout_ms;
40 
41 	switch (modify_type) {
42 	case SELFTEST_SCHEDULER_MODIFY_FAST_RESET:
43 		/*
44 		 * Enable force pre-emption on time slice expiration
45 		 * together with engine reset on pre-emption timeout.
46 		 * This is required to make the GuC notice and reset
47 		 * the single hanging context.
48 		 * Also, reduce the preemption timeout to something
49 		 * small to speed the test up.
50 		 */
51 		engine->i915->params.reset = 2;
52 		engine->flags |= I915_ENGINE_WANT_FORCED_PREEMPTION;
53 		engine->props.timeslice_duration_ms = REDUCED_TIMESLICE;
54 		engine->props.preempt_timeout_ms = REDUCED_PREEMPT;
55 		break;
56 
57 	case SELFTEST_SCHEDULER_MODIFY_NO_HANGCHECK:
58 		engine->props.preempt_timeout_ms = 0;
59 		break;
60 
61 	default:
62 		pr_err("Invalid scheduler policy modification type: %d!\n", modify_type);
63 		return -EINVAL;
64 	}
65 
66 	if (!intel_engine_uses_guc(engine))
67 		return 0;
68 
69 	err = intel_guc_global_policies_update(&engine->gt->uc.guc);
70 	if (err)
71 		intel_selftest_restore_policy(engine, saved);
72 
73 	return err;
74 }
75 
76 int intel_selftest_restore_policy(struct intel_engine_cs *engine,
77 				  struct intel_selftest_saved_policy *saved)
78 {
79 	/* Restore the original policies */
80 	engine->i915->params.reset = saved->reset;
81 	engine->flags = saved->flags;
82 	engine->props.timeslice_duration_ms = saved->timeslice;
83 	engine->props.preempt_timeout_ms = saved->preempt_timeout;
84 
85 	if (!intel_engine_uses_guc(engine))
86 		return 0;
87 
88 	return intel_guc_global_policies_update(&engine->gt->uc.guc);
89 }
90 
91 int intel_selftest_wait_for_rq(struct i915_request *rq)
92 {
93 	long ret;
94 
95 	ret = i915_request_wait(rq, 0, WAIT_FOR_RESET_TIME);
96 	if (ret < 0)
97 		return ret;
98 
99 	return 0;
100 }
101