]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/i915/gvt/sched_policy.c
drm/i915/gvt: avoid unnecessary vgpu switch
[karo-tx-linux.git] / drivers / gpu / drm / i915 / gvt / sched_policy.c
1 /*
2  * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Anhua Xu
25  *    Kevin Tian <kevin.tian@intel.com>
26  *
27  * Contributors:
28  *    Min He <min.he@intel.com>
29  *    Bing Niu <bing.niu@intel.com>
30  *    Zhi Wang <zhi.a.wang@intel.com>
31  *
32  */
33
34 #include "i915_drv.h"
35 #include "gvt.h"
36
37 static bool vgpu_has_pending_workload(struct intel_vgpu *vgpu)
38 {
39         enum intel_engine_id i;
40         struct intel_engine_cs *engine;
41
42         for_each_engine(engine, vgpu->gvt->dev_priv, i) {
43                 if (!list_empty(workload_q_head(vgpu, i)))
44                         return true;
45         }
46
47         return false;
48 }
49
50 static void try_to_schedule_next_vgpu(struct intel_gvt *gvt)
51 {
52         struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
53         enum intel_engine_id i;
54         struct intel_engine_cs *engine;
55
56         /* no need to schedule if next_vgpu is the same with current_vgpu,
57          * let scheduler chose next_vgpu again by setting it to NULL.
58          */
59         if (scheduler->next_vgpu == scheduler->current_vgpu) {
60                 scheduler->next_vgpu = NULL;
61                 return;
62         }
63
64         gvt_dbg_sched("try to schedule next vgpu %d\n",
65                         scheduler->next_vgpu->id);
66
67         /*
68          * after the flag is set, workload dispatch thread will
69          * stop dispatching workload for current vgpu
70          */
71         scheduler->need_reschedule = true;
72
73         /* still have uncompleted workload? */
74         for_each_engine(engine, gvt->dev_priv, i) {
75                 if (scheduler->current_workload[i]) {
76                         gvt_dbg_sched("still have running workload\n");
77                         return;
78                 }
79         }
80
81         gvt_dbg_sched("switch to next vgpu %d\n",
82                         scheduler->next_vgpu->id);
83
84         /* switch current vgpu */
85         scheduler->current_vgpu = scheduler->next_vgpu;
86         scheduler->next_vgpu = NULL;
87
88         scheduler->need_reschedule = false;
89
90         /* wake up workload dispatch thread */
91         for_each_engine(engine, gvt->dev_priv, i)
92                 wake_up(&scheduler->waitq[i]);
93 }
94
95 struct tbs_vgpu_data {
96         struct list_head list;
97         struct intel_vgpu *vgpu;
98         /* put some per-vgpu sched stats here */
99 };
100
101 struct tbs_sched_data {
102         struct intel_gvt *gvt;
103         struct delayed_work work;
104         unsigned long period;
105         struct list_head runq_head;
106 };
107
108 #define GVT_DEFAULT_TIME_SLICE (msecs_to_jiffies(1))
109
110 static void tbs_sched_func(struct work_struct *work)
111 {
112         struct tbs_sched_data *sched_data = container_of(work,
113                         struct tbs_sched_data, work.work);
114         struct tbs_vgpu_data *vgpu_data;
115
116         struct intel_gvt *gvt = sched_data->gvt;
117         struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
118
119         struct intel_vgpu *vgpu = NULL;
120         struct list_head *pos, *head;
121
122         mutex_lock(&gvt->lock);
123
124         /* no vgpu or has already had a target */
125         if (list_empty(&sched_data->runq_head) || scheduler->next_vgpu)
126                 goto out;
127
128         if (scheduler->current_vgpu) {
129                 vgpu_data = scheduler->current_vgpu->sched_data;
130                 head = &vgpu_data->list;
131         } else {
132                 head = &sched_data->runq_head;
133         }
134
135         /* search a vgpu with pending workload */
136         list_for_each(pos, head) {
137                 if (pos == &sched_data->runq_head)
138                         continue;
139
140                 vgpu_data = container_of(pos, struct tbs_vgpu_data, list);
141                 if (!vgpu_has_pending_workload(vgpu_data->vgpu))
142                         continue;
143
144                 vgpu = vgpu_data->vgpu;
145                 break;
146         }
147
148         if (vgpu) {
149                 scheduler->next_vgpu = vgpu;
150                 gvt_dbg_sched("pick next vgpu %d\n", vgpu->id);
151         }
152 out:
153         if (scheduler->next_vgpu) {
154                 gvt_dbg_sched("try to schedule next vgpu %d\n",
155                                 scheduler->next_vgpu->id);
156                 try_to_schedule_next_vgpu(gvt);
157         }
158
159         /*
160          * still have vgpu on runq
161          * or last schedule haven't finished due to running workload
162          */
163         if (!list_empty(&sched_data->runq_head) || scheduler->next_vgpu)
164                 schedule_delayed_work(&sched_data->work, sched_data->period);
165
166         mutex_unlock(&gvt->lock);
167 }
168
169 static int tbs_sched_init(struct intel_gvt *gvt)
170 {
171         struct intel_gvt_workload_scheduler *scheduler =
172                 &gvt->scheduler;
173
174         struct tbs_sched_data *data;
175
176         data = kzalloc(sizeof(*data), GFP_KERNEL);
177         if (!data)
178                 return -ENOMEM;
179
180         INIT_LIST_HEAD(&data->runq_head);
181         INIT_DELAYED_WORK(&data->work, tbs_sched_func);
182         data->period = GVT_DEFAULT_TIME_SLICE;
183         data->gvt = gvt;
184
185         scheduler->sched_data = data;
186         return 0;
187 }
188
189 static void tbs_sched_clean(struct intel_gvt *gvt)
190 {
191         struct intel_gvt_workload_scheduler *scheduler =
192                 &gvt->scheduler;
193         struct tbs_sched_data *data = scheduler->sched_data;
194
195         cancel_delayed_work(&data->work);
196         kfree(data);
197         scheduler->sched_data = NULL;
198 }
199
200 static int tbs_sched_init_vgpu(struct intel_vgpu *vgpu)
201 {
202         struct tbs_vgpu_data *data;
203
204         data = kzalloc(sizeof(*data), GFP_KERNEL);
205         if (!data)
206                 return -ENOMEM;
207
208         data->vgpu = vgpu;
209         INIT_LIST_HEAD(&data->list);
210
211         vgpu->sched_data = data;
212         return 0;
213 }
214
215 static void tbs_sched_clean_vgpu(struct intel_vgpu *vgpu)
216 {
217         kfree(vgpu->sched_data);
218         vgpu->sched_data = NULL;
219 }
220
221 static void tbs_sched_start_schedule(struct intel_vgpu *vgpu)
222 {
223         struct tbs_sched_data *sched_data = vgpu->gvt->scheduler.sched_data;
224         struct tbs_vgpu_data *vgpu_data = vgpu->sched_data;
225
226         if (!list_empty(&vgpu_data->list))
227                 return;
228
229         list_add_tail(&vgpu_data->list, &sched_data->runq_head);
230         schedule_delayed_work(&sched_data->work, 0);
231 }
232
233 static void tbs_sched_stop_schedule(struct intel_vgpu *vgpu)
234 {
235         struct tbs_vgpu_data *vgpu_data = vgpu->sched_data;
236
237         list_del_init(&vgpu_data->list);
238 }
239
240 static struct intel_gvt_sched_policy_ops tbs_schedule_ops = {
241         .init = tbs_sched_init,
242         .clean = tbs_sched_clean,
243         .init_vgpu = tbs_sched_init_vgpu,
244         .clean_vgpu = tbs_sched_clean_vgpu,
245         .start_schedule = tbs_sched_start_schedule,
246         .stop_schedule = tbs_sched_stop_schedule,
247 };
248
249 int intel_gvt_init_sched_policy(struct intel_gvt *gvt)
250 {
251         gvt->scheduler.sched_ops = &tbs_schedule_ops;
252
253         return gvt->scheduler.sched_ops->init(gvt);
254 }
255
256 void intel_gvt_clean_sched_policy(struct intel_gvt *gvt)
257 {
258         gvt->scheduler.sched_ops->clean(gvt);
259 }
260
261 int intel_vgpu_init_sched_policy(struct intel_vgpu *vgpu)
262 {
263         return vgpu->gvt->scheduler.sched_ops->init_vgpu(vgpu);
264 }
265
266 void intel_vgpu_clean_sched_policy(struct intel_vgpu *vgpu)
267 {
268         vgpu->gvt->scheduler.sched_ops->clean_vgpu(vgpu);
269 }
270
271 void intel_vgpu_start_schedule(struct intel_vgpu *vgpu)
272 {
273         gvt_dbg_core("vgpu%d: start schedule\n", vgpu->id);
274
275         vgpu->gvt->scheduler.sched_ops->start_schedule(vgpu);
276 }
277
278 void intel_vgpu_stop_schedule(struct intel_vgpu *vgpu)
279 {
280         struct intel_gvt_workload_scheduler *scheduler =
281                 &vgpu->gvt->scheduler;
282
283         gvt_dbg_core("vgpu%d: stop schedule\n", vgpu->id);
284
285         scheduler->sched_ops->stop_schedule(vgpu);
286
287         if (scheduler->next_vgpu == vgpu)
288                 scheduler->next_vgpu = NULL;
289
290         if (scheduler->current_vgpu == vgpu) {
291                 /* stop workload dispatching */
292                 scheduler->need_reschedule = true;
293                 scheduler->current_vgpu = NULL;
294         }
295 }