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 target to schedule */ 57 if (!scheduler->next_vgpu) 58 return; 59 60 gvt_dbg_sched("try to schedule next vgpu %d\n", 61 scheduler->next_vgpu->id); 62 63 /* 64 * after the flag is set, workload dispatch thread will 65 * stop dispatching workload for current vgpu 66 */ 67 scheduler->need_reschedule = true; 68 69 /* still have uncompleted workload? */ 70 for_each_engine(engine, gvt->dev_priv, i) { 71 if (scheduler->current_workload[i]) { 72 gvt_dbg_sched("still have running workload\n"); 73 return; 74 } 75 } 76 77 gvt_dbg_sched("switch to next vgpu %d\n", 78 scheduler->next_vgpu->id); 79 80 /* switch current vgpu */ 81 scheduler->current_vgpu = scheduler->next_vgpu; 82 scheduler->next_vgpu = NULL; 83 84 scheduler->need_reschedule = false; 85 86 /* wake up workload dispatch thread */ 87 for_each_engine(engine, gvt->dev_priv, i) 88 wake_up(&scheduler->waitq[i]); 89 } 90 91 struct tbs_vgpu_data { 92 struct list_head list; 93 struct intel_vgpu *vgpu; 94 /* put some per-vgpu sched stats here */ 95 }; 96 97 struct tbs_sched_data { 98 struct intel_gvt *gvt; 99 struct delayed_work work; 100 unsigned long period; 101 struct list_head runq_head; 102 }; 103 104 #define GVT_DEFAULT_TIME_SLICE (1 * HZ / 1000) 105 106 static void tbs_sched_func(struct work_struct *work) 107 { 108 struct tbs_sched_data *sched_data = container_of(work, 109 struct tbs_sched_data, work.work); 110 struct tbs_vgpu_data *vgpu_data; 111 112 struct intel_gvt *gvt = sched_data->gvt; 113 struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler; 114 115 struct intel_vgpu *vgpu = NULL; 116 struct list_head *pos, *head; 117 118 mutex_lock(&gvt->lock); 119 120 /* no vgpu or has already had a target */ 121 if (list_empty(&sched_data->runq_head) || scheduler->next_vgpu) 122 goto out; 123 124 if (scheduler->current_vgpu) { 125 vgpu_data = scheduler->current_vgpu->sched_data; 126 head = &vgpu_data->list; 127 } else { 128 gvt_dbg_sched("no current vgpu search from q head\n"); 129 head = &sched_data->runq_head; 130 } 131 132 /* search a vgpu with pending workload */ 133 list_for_each(pos, head) { 134 if (pos == &sched_data->runq_head) 135 continue; 136 137 vgpu_data = container_of(pos, struct tbs_vgpu_data, list); 138 if (!vgpu_has_pending_workload(vgpu_data->vgpu)) 139 continue; 140 141 vgpu = vgpu_data->vgpu; 142 break; 143 } 144 145 if (vgpu) { 146 scheduler->next_vgpu = vgpu; 147 gvt_dbg_sched("pick next vgpu %d\n", vgpu->id); 148 } 149 out: 150 if (scheduler->next_vgpu) { 151 gvt_dbg_sched("try to schedule next vgpu %d\n", 152 scheduler->next_vgpu->id); 153 try_to_schedule_next_vgpu(gvt); 154 } 155 156 /* 157 * still have vgpu on runq 158 * or last schedule haven't finished due to running workload 159 */ 160 if (!list_empty(&sched_data->runq_head) || scheduler->next_vgpu) 161 schedule_delayed_work(&sched_data->work, sched_data->period); 162 163 mutex_unlock(&gvt->lock); 164 } 165 166 static int tbs_sched_init(struct intel_gvt *gvt) 167 { 168 struct intel_gvt_workload_scheduler *scheduler = 169 &gvt->scheduler; 170 171 struct tbs_sched_data *data; 172 173 data = kzalloc(sizeof(*data), GFP_KERNEL); 174 if (!data) 175 return -ENOMEM; 176 177 INIT_LIST_HEAD(&data->runq_head); 178 INIT_DELAYED_WORK(&data->work, tbs_sched_func); 179 data->period = GVT_DEFAULT_TIME_SLICE; 180 data->gvt = gvt; 181 182 scheduler->sched_data = data; 183 return 0; 184 } 185 186 static void tbs_sched_clean(struct intel_gvt *gvt) 187 { 188 struct intel_gvt_workload_scheduler *scheduler = 189 &gvt->scheduler; 190 struct tbs_sched_data *data = scheduler->sched_data; 191 192 cancel_delayed_work(&data->work); 193 kfree(data); 194 scheduler->sched_data = NULL; 195 } 196 197 static int tbs_sched_init_vgpu(struct intel_vgpu *vgpu) 198 { 199 struct tbs_vgpu_data *data; 200 201 data = kzalloc(sizeof(*data), GFP_KERNEL); 202 if (!data) 203 return -ENOMEM; 204 205 data->vgpu = vgpu; 206 INIT_LIST_HEAD(&data->list); 207 208 vgpu->sched_data = data; 209 return 0; 210 } 211 212 static void tbs_sched_clean_vgpu(struct intel_vgpu *vgpu) 213 { 214 kfree(vgpu->sched_data); 215 vgpu->sched_data = NULL; 216 } 217 218 static void tbs_sched_start_schedule(struct intel_vgpu *vgpu) 219 { 220 struct tbs_sched_data *sched_data = vgpu->gvt->scheduler.sched_data; 221 struct tbs_vgpu_data *vgpu_data = vgpu->sched_data; 222 223 if (!list_empty(&vgpu_data->list)) 224 return; 225 226 list_add_tail(&vgpu_data->list, &sched_data->runq_head); 227 schedule_delayed_work(&sched_data->work, sched_data->period); 228 } 229 230 static void tbs_sched_stop_schedule(struct intel_vgpu *vgpu) 231 { 232 struct tbs_vgpu_data *vgpu_data = vgpu->sched_data; 233 234 list_del_init(&vgpu_data->list); 235 } 236 237 static struct intel_gvt_sched_policy_ops tbs_schedule_ops = { 238 .init = tbs_sched_init, 239 .clean = tbs_sched_clean, 240 .init_vgpu = tbs_sched_init_vgpu, 241 .clean_vgpu = tbs_sched_clean_vgpu, 242 .start_schedule = tbs_sched_start_schedule, 243 .stop_schedule = tbs_sched_stop_schedule, 244 }; 245 246 int intel_gvt_init_sched_policy(struct intel_gvt *gvt) 247 { 248 gvt->scheduler.sched_ops = &tbs_schedule_ops; 249 250 return gvt->scheduler.sched_ops->init(gvt); 251 } 252 253 void intel_gvt_clean_sched_policy(struct intel_gvt *gvt) 254 { 255 gvt->scheduler.sched_ops->clean(gvt); 256 } 257 258 int intel_vgpu_init_sched_policy(struct intel_vgpu *vgpu) 259 { 260 return vgpu->gvt->scheduler.sched_ops->init_vgpu(vgpu); 261 } 262 263 void intel_vgpu_clean_sched_policy(struct intel_vgpu *vgpu) 264 { 265 vgpu->gvt->scheduler.sched_ops->clean_vgpu(vgpu); 266 } 267 268 void intel_vgpu_start_schedule(struct intel_vgpu *vgpu) 269 { 270 gvt_dbg_core("vgpu%d: start schedule\n", vgpu->id); 271 272 vgpu->gvt->scheduler.sched_ops->start_schedule(vgpu); 273 } 274 275 void intel_vgpu_stop_schedule(struct intel_vgpu *vgpu) 276 { 277 struct intel_gvt_workload_scheduler *scheduler = 278 &vgpu->gvt->scheduler; 279 280 gvt_dbg_core("vgpu%d: stop schedule\n", vgpu->id); 281 282 scheduler->sched_ops->stop_schedule(vgpu); 283 284 if (scheduler->next_vgpu == vgpu) 285 scheduler->next_vgpu = NULL; 286 287 if (scheduler->current_vgpu == vgpu) { 288 /* stop workload dispatching */ 289 scheduler->need_reschedule = true; 290 scheduler->current_vgpu = NULL; 291 } 292 } 293