1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2018 Intel Corporation
5  */
6 
7 #ifndef _I915_SCHEDULER_H_
8 #define _I915_SCHEDULER_H_
9 
10 #include <linux/bitops.h>
11 #include <linux/list.h>
12 #include <linux/kernel.h>
13 
14 #include "i915_scheduler_types.h"
15 
16 struct drm_printer;
17 
18 #define priolist_for_each_request(it, plist) \
19 	list_for_each_entry(it, &(plist)->requests, sched.link)
20 
21 #define priolist_for_each_request_consume(it, n, plist) \
22 	list_for_each_entry_safe(it, n, &(plist)->requests, sched.link)
23 
24 void i915_sched_node_init(struct i915_sched_node *node);
25 void i915_sched_node_reinit(struct i915_sched_node *node);
26 
27 bool __i915_sched_node_add_dependency(struct i915_sched_node *node,
28 				      struct i915_sched_node *signal,
29 				      struct i915_dependency *dep,
30 				      unsigned long flags);
31 
32 int i915_sched_node_add_dependency(struct i915_sched_node *node,
33 				   struct i915_sched_node *signal,
34 				   unsigned long flags);
35 
36 void i915_sched_node_fini(struct i915_sched_node *node);
37 
38 void i915_schedule(struct i915_request *request,
39 		   const struct i915_sched_attr *attr);
40 
41 struct list_head *
42 i915_sched_lookup_priolist(struct i915_sched_engine *sched_engine, int prio);
43 
44 void __i915_priolist_free(struct i915_priolist *p);
45 static inline void i915_priolist_free(struct i915_priolist *p)
46 {
47 	if (p->priority != I915_PRIORITY_NORMAL)
48 		__i915_priolist_free(p);
49 }
50 
51 struct i915_sched_engine *
52 i915_sched_engine_create(unsigned int subclass);
53 
54 static inline struct i915_sched_engine *
55 i915_sched_engine_get(struct i915_sched_engine *sched_engine)
56 {
57 	kref_get(&sched_engine->ref);
58 	return sched_engine;
59 }
60 
61 static inline void
62 i915_sched_engine_put(struct i915_sched_engine *sched_engine)
63 {
64 	kref_put(&sched_engine->ref, sched_engine->destroy);
65 }
66 
67 static inline bool
68 i915_sched_engine_is_empty(struct i915_sched_engine *sched_engine)
69 {
70 	return RB_EMPTY_ROOT(&sched_engine->queue.rb_root);
71 }
72 
73 static inline void
74 i915_sched_engine_reset_on_empty(struct i915_sched_engine *sched_engine)
75 {
76 	if (i915_sched_engine_is_empty(sched_engine))
77 		sched_engine->no_priolist = false;
78 }
79 
80 static inline void
81 i915_sched_engine_active_lock_bh(struct i915_sched_engine *sched_engine)
82 {
83 	local_bh_disable(); /* prevent local softirq and lock recursion */
84 	tasklet_lock(&sched_engine->tasklet);
85 }
86 
87 static inline void
88 i915_sched_engine_active_unlock_bh(struct i915_sched_engine *sched_engine)
89 {
90 	tasklet_unlock(&sched_engine->tasklet);
91 	local_bh_enable(); /* restore softirq, and kick ksoftirqd! */
92 }
93 
94 void i915_request_show_with_schedule(struct drm_printer *m,
95 				     const struct i915_request *rq,
96 				     const char *prefix,
97 				     int indent);
98 
99 static inline bool
100 i915_sched_engine_disabled(struct i915_sched_engine *sched_engine)
101 {
102 	return sched_engine->disabled(sched_engine);
103 }
104 
105 void i915_scheduler_module_exit(void);
106 int i915_scheduler_module_init(void);
107 
108 #endif /* _I915_SCHEDULER_H_ */
109