Commit | Line | Data |
---|---|---|
8b3d6663 AB |
1 | /* sched.c - SPU scheduler. |
2 | * | |
3 | * Copyright (C) IBM 2005 | |
4 | * Author: Mark Nutter <mnutter@us.ibm.com> | |
5 | * | |
a68cf983 | 6 | * 2006-03-31 NUMA domains added. |
8b3d6663 AB |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | ||
3b3d22cb AB |
23 | #undef DEBUG |
24 | ||
8b3d6663 AB |
25 | #include <linux/module.h> |
26 | #include <linux/errno.h> | |
27 | #include <linux/sched.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/mm.h> | |
30 | #include <linux/completion.h> | |
31 | #include <linux/vmalloc.h> | |
32 | #include <linux/smp.h> | |
8b3d6663 AB |
33 | #include <linux/stddef.h> |
34 | #include <linux/unistd.h> | |
a68cf983 MN |
35 | #include <linux/numa.h> |
36 | #include <linux/mutex.h> | |
86767277 | 37 | #include <linux/notifier.h> |
37901802 | 38 | #include <linux/kthread.h> |
65de66f0 CH |
39 | #include <linux/pid_namespace.h> |
40 | #include <linux/proc_fs.h> | |
41 | #include <linux/seq_file.h> | |
8b3d6663 AB |
42 | |
43 | #include <asm/io.h> | |
44 | #include <asm/mmu_context.h> | |
45 | #include <asm/spu.h> | |
46 | #include <asm/spu_csa.h> | |
a91942ae | 47 | #include <asm/spu_priv1.h> |
8b3d6663 AB |
48 | #include "spufs.h" |
49 | ||
8b3d6663 | 50 | struct spu_prio_array { |
72cb3608 | 51 | DECLARE_BITMAP(bitmap, MAX_PRIO); |
079cdb61 CH |
52 | struct list_head runq[MAX_PRIO]; |
53 | spinlock_t runq_lock; | |
a68cf983 MN |
54 | struct list_head active_list[MAX_NUMNODES]; |
55 | struct mutex active_mutex[MAX_NUMNODES]; | |
65de66f0 CH |
56 | int nr_active[MAX_NUMNODES]; |
57 | int nr_waiting; | |
8b3d6663 AB |
58 | }; |
59 | ||
65de66f0 | 60 | static unsigned long spu_avenrun[3]; |
a68cf983 | 61 | static struct spu_prio_array *spu_prio; |
37901802 CH |
62 | static struct task_struct *spusched_task; |
63 | static struct timer_list spusched_timer; | |
8b3d6663 | 64 | |
fe443ef2 CH |
65 | /* |
66 | * Priority of a normal, non-rt, non-niced'd process (aka nice level 0). | |
67 | */ | |
68 | #define NORMAL_PRIO 120 | |
69 | ||
70 | /* | |
71 | * Frequency of the spu scheduler tick. By default we do one SPU scheduler | |
72 | * tick for every 10 CPU scheduler ticks. | |
73 | */ | |
74 | #define SPUSCHED_TICK (10) | |
75 | ||
76 | /* | |
77 | * These are the 'tuning knobs' of the scheduler: | |
78 | * | |
60e24239 JK |
79 | * Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is |
80 | * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs. | |
fe443ef2 | 81 | */ |
60e24239 JK |
82 | #define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1) |
83 | #define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK)) | |
fe443ef2 CH |
84 | |
85 | #define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO) | |
86 | #define SCALE_PRIO(x, prio) \ | |
87 | max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE) | |
88 | ||
89 | /* | |
90 | * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values: | |
91 | * [800ms ... 100ms ... 5ms] | |
92 | * | |
93 | * The higher a thread's priority, the bigger timeslices | |
94 | * it gets during one round of execution. But even the lowest | |
95 | * priority thread gets MIN_TIMESLICE worth of execution time. | |
96 | */ | |
97 | void spu_set_timeslice(struct spu_context *ctx) | |
98 | { | |
99 | if (ctx->prio < NORMAL_PRIO) | |
100 | ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio); | |
101 | else | |
102 | ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio); | |
103 | } | |
104 | ||
2cf2b3b4 CH |
105 | /* |
106 | * Update scheduling information from the owning thread. | |
107 | */ | |
108 | void __spu_update_sched_info(struct spu_context *ctx) | |
109 | { | |
476273ad CH |
110 | /* |
111 | * 32-Bit assignment are atomic on powerpc, and we don't care about | |
112 | * memory ordering here because retriving the controlling thread is | |
113 | * per defintion racy. | |
114 | */ | |
115 | ctx->tid = current->pid; | |
116 | ||
2cf2b3b4 CH |
117 | /* |
118 | * We do our own priority calculations, so we normally want | |
119 | * ->static_prio to start with. Unfortunately thies field | |
120 | * contains junk for threads with a realtime scheduling | |
121 | * policy so we have to look at ->prio in this case. | |
122 | */ | |
123 | if (rt_prio(current->prio)) | |
124 | ctx->prio = current->prio; | |
125 | else | |
126 | ctx->prio = current->static_prio; | |
127 | ctx->policy = current->policy; | |
ea1ae594 CH |
128 | |
129 | /* | |
130 | * A lot of places that don't hold active_mutex poke into | |
131 | * cpus_allowed, including grab_runnable_context which | |
132 | * already holds the runq_lock. So abuse runq_lock | |
133 | * to protect this field aswell. | |
134 | */ | |
135 | spin_lock(&spu_prio->runq_lock); | |
136 | ctx->cpus_allowed = current->cpus_allowed; | |
137 | spin_unlock(&spu_prio->runq_lock); | |
2cf2b3b4 CH |
138 | } |
139 | ||
140 | void spu_update_sched_info(struct spu_context *ctx) | |
141 | { | |
142 | int node = ctx->spu->node; | |
143 | ||
144 | mutex_lock(&spu_prio->active_mutex[node]); | |
145 | __spu_update_sched_info(ctx); | |
146 | mutex_unlock(&spu_prio->active_mutex[node]); | |
147 | } | |
148 | ||
ea1ae594 | 149 | static int __node_allowed(struct spu_context *ctx, int node) |
8b3d6663 | 150 | { |
ea1ae594 CH |
151 | if (nr_cpus_node(node)) { |
152 | cpumask_t mask = node_to_cpumask(node); | |
8b3d6663 | 153 | |
ea1ae594 CH |
154 | if (cpus_intersects(mask, ctx->cpus_allowed)) |
155 | return 1; | |
156 | } | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
161 | static int node_allowed(struct spu_context *ctx, int node) | |
162 | { | |
163 | int rval; | |
164 | ||
165 | spin_lock(&spu_prio->runq_lock); | |
166 | rval = __node_allowed(ctx, node); | |
167 | spin_unlock(&spu_prio->runq_lock); | |
168 | ||
169 | return rval; | |
8b3d6663 AB |
170 | } |
171 | ||
202557d2 CH |
172 | /** |
173 | * spu_add_to_active_list - add spu to active list | |
174 | * @spu: spu to add to the active list | |
175 | */ | |
176 | static void spu_add_to_active_list(struct spu *spu) | |
177 | { | |
65de66f0 CH |
178 | int node = spu->node; |
179 | ||
180 | mutex_lock(&spu_prio->active_mutex[node]); | |
181 | spu_prio->nr_active[node]++; | |
182 | list_add_tail(&spu->list, &spu_prio->active_list[node]); | |
183 | mutex_unlock(&spu_prio->active_mutex[node]); | |
202557d2 CH |
184 | } |
185 | ||
37901802 CH |
186 | static void __spu_remove_from_active_list(struct spu *spu) |
187 | { | |
188 | list_del_init(&spu->list); | |
65de66f0 | 189 | spu_prio->nr_active[spu->node]--; |
37901802 CH |
190 | } |
191 | ||
202557d2 CH |
192 | /** |
193 | * spu_remove_from_active_list - remove spu from active list | |
194 | * @spu: spu to remove from the active list | |
202557d2 | 195 | */ |
678b2ff1 | 196 | static void spu_remove_from_active_list(struct spu *spu) |
202557d2 CH |
197 | { |
198 | int node = spu->node; | |
202557d2 CH |
199 | |
200 | mutex_lock(&spu_prio->active_mutex[node]); | |
37901802 | 201 | __spu_remove_from_active_list(spu); |
202557d2 | 202 | mutex_unlock(&spu_prio->active_mutex[node]); |
202557d2 CH |
203 | } |
204 | ||
86767277 AB |
205 | static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier); |
206 | ||
207 | static void spu_switch_notify(struct spu *spu, struct spu_context *ctx) | |
208 | { | |
209 | blocking_notifier_call_chain(&spu_switch_notifier, | |
210 | ctx ? ctx->object_id : 0, spu); | |
211 | } | |
212 | ||
213 | int spu_switch_event_register(struct notifier_block * n) | |
214 | { | |
215 | return blocking_notifier_chain_register(&spu_switch_notifier, n); | |
216 | } | |
217 | ||
218 | int spu_switch_event_unregister(struct notifier_block * n) | |
219 | { | |
220 | return blocking_notifier_chain_unregister(&spu_switch_notifier, n); | |
221 | } | |
222 | ||
202557d2 CH |
223 | /** |
224 | * spu_bind_context - bind spu context to physical spu | |
225 | * @spu: physical spu to bind to | |
226 | * @ctx: context to bind | |
227 | */ | |
228 | static void spu_bind_context(struct spu *spu, struct spu_context *ctx) | |
8b3d6663 | 229 | { |
a68cf983 MN |
230 | pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid, |
231 | spu->number, spu->node); | |
e9f8a0b6 CH |
232 | |
233 | ctx->stats.slb_flt_base = spu->stats.slb_flt; | |
234 | ctx->stats.class2_intr_base = spu->stats.class2_intr; | |
235 | ||
8b3d6663 AB |
236 | spu->ctx = ctx; |
237 | spu->flags = 0; | |
238 | ctx->spu = spu; | |
239 | ctx->ops = &spu_hw_ops; | |
240 | spu->pid = current->pid; | |
94b2a439 | 241 | spu_associate_mm(spu, ctx->owner); |
8b3d6663 AB |
242 | spu->ibox_callback = spufs_ibox_callback; |
243 | spu->wbox_callback = spufs_wbox_callback; | |
5110459f | 244 | spu->stop_callback = spufs_stop_callback; |
a33a7d73 | 245 | spu->mfc_callback = spufs_mfc_callback; |
9add11da | 246 | spu->dma_callback = spufs_dma_callback; |
8b3d6663 | 247 | mb(); |
5110459f | 248 | spu_unmap_mappings(ctx); |
8b3d6663 | 249 | spu_restore(&ctx->csa, spu); |
2a911f0b | 250 | spu->timestamp = jiffies; |
a68cf983 | 251 | spu_cpu_affinity_set(spu, raw_smp_processor_id()); |
86767277 | 252 | spu_switch_notify(spu, ctx); |
81998baf | 253 | ctx->state = SPU_STATE_RUNNABLE; |
8b3d6663 AB |
254 | } |
255 | ||
202557d2 CH |
256 | /** |
257 | * spu_unbind_context - unbind spu context from physical spu | |
258 | * @spu: physical spu to unbind from | |
259 | * @ctx: context to unbind | |
202557d2 | 260 | */ |
678b2ff1 | 261 | static void spu_unbind_context(struct spu *spu, struct spu_context *ctx) |
8b3d6663 | 262 | { |
a68cf983 MN |
263 | pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__, |
264 | spu->pid, spu->number, spu->node); | |
202557d2 | 265 | |
86767277 | 266 | spu_switch_notify(spu, NULL); |
5110459f | 267 | spu_unmap_mappings(ctx); |
8b3d6663 | 268 | spu_save(&ctx->csa, spu); |
2a911f0b | 269 | spu->timestamp = jiffies; |
8b3d6663 AB |
270 | ctx->state = SPU_STATE_SAVED; |
271 | spu->ibox_callback = NULL; | |
272 | spu->wbox_callback = NULL; | |
5110459f | 273 | spu->stop_callback = NULL; |
a33a7d73 | 274 | spu->mfc_callback = NULL; |
9add11da | 275 | spu->dma_callback = NULL; |
94b2a439 | 276 | spu_associate_mm(spu, NULL); |
8b3d6663 | 277 | spu->pid = 0; |
8b3d6663 AB |
278 | ctx->ops = &spu_backing_ops; |
279 | ctx->spu = NULL; | |
2a911f0b | 280 | spu->flags = 0; |
8b3d6663 | 281 | spu->ctx = NULL; |
e9f8a0b6 CH |
282 | |
283 | ctx->stats.slb_flt += | |
284 | (spu->stats.slb_flt - ctx->stats.slb_flt_base); | |
285 | ctx->stats.class2_intr += | |
286 | (spu->stats.class2_intr - ctx->stats.class2_intr_base); | |
8b3d6663 AB |
287 | } |
288 | ||
079cdb61 CH |
289 | /** |
290 | * spu_add_to_rq - add a context to the runqueue | |
291 | * @ctx: context to add | |
292 | */ | |
4e0f4ed0 | 293 | static void __spu_add_to_rq(struct spu_context *ctx) |
8b3d6663 | 294 | { |
4e0f4ed0 LB |
295 | int prio = ctx->prio; |
296 | ||
297 | list_add_tail(&ctx->rq, &spu_prio->runq[prio]); | |
298 | set_bit(prio, spu_prio->bitmap); | |
c77239b8 CH |
299 | if (!spu_prio->nr_waiting++) |
300 | __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK); | |
2a911f0b | 301 | } |
5110459f | 302 | |
4e0f4ed0 | 303 | static void __spu_del_from_rq(struct spu_context *ctx) |
a475c2f4 | 304 | { |
4e0f4ed0 LB |
305 | int prio = ctx->prio; |
306 | ||
65de66f0 | 307 | if (!list_empty(&ctx->rq)) { |
c77239b8 CH |
308 | if (!--spu_prio->nr_waiting) |
309 | del_timer(&spusched_timer); | |
a475c2f4 | 310 | list_del_init(&ctx->rq); |
c77239b8 CH |
311 | |
312 | if (list_empty(&spu_prio->runq[prio])) | |
313 | clear_bit(prio, spu_prio->bitmap); | |
65de66f0 | 314 | } |
079cdb61 | 315 | } |
a68cf983 | 316 | |
079cdb61 | 317 | static void spu_prio_wait(struct spu_context *ctx) |
8b3d6663 | 318 | { |
a68cf983 | 319 | DEFINE_WAIT(wait); |
8b3d6663 | 320 | |
4e0f4ed0 | 321 | spin_lock(&spu_prio->runq_lock); |
079cdb61 | 322 | prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE); |
a68cf983 | 323 | if (!signal_pending(current)) { |
4e0f4ed0 LB |
324 | __spu_add_to_rq(ctx); |
325 | spin_unlock(&spu_prio->runq_lock); | |
650f8b02 | 326 | mutex_unlock(&ctx->state_mutex); |
a68cf983 | 327 | schedule(); |
650f8b02 | 328 | mutex_lock(&ctx->state_mutex); |
4e0f4ed0 LB |
329 | spin_lock(&spu_prio->runq_lock); |
330 | __spu_del_from_rq(ctx); | |
8b3d6663 | 331 | } |
4e0f4ed0 | 332 | spin_unlock(&spu_prio->runq_lock); |
079cdb61 CH |
333 | __set_current_state(TASK_RUNNING); |
334 | remove_wait_queue(&ctx->stop_wq, &wait); | |
8b3d6663 AB |
335 | } |
336 | ||
079cdb61 | 337 | static struct spu *spu_get_idle(struct spu_context *ctx) |
a68cf983 MN |
338 | { |
339 | struct spu *spu = NULL; | |
340 | int node = cpu_to_node(raw_smp_processor_id()); | |
341 | int n; | |
342 | ||
343 | for (n = 0; n < MAX_NUMNODES; n++, node++) { | |
344 | node = (node < MAX_NUMNODES) ? node : 0; | |
ea1ae594 | 345 | if (!node_allowed(ctx, node)) |
a68cf983 MN |
346 | continue; |
347 | spu = spu_alloc_node(node); | |
348 | if (spu) | |
349 | break; | |
350 | } | |
351 | return spu; | |
352 | } | |
8b3d6663 | 353 | |
52f04fcf CH |
354 | /** |
355 | * find_victim - find a lower priority context to preempt | |
356 | * @ctx: canidate context for running | |
357 | * | |
358 | * Returns the freed physical spu to run the new context on. | |
359 | */ | |
360 | static struct spu *find_victim(struct spu_context *ctx) | |
361 | { | |
362 | struct spu_context *victim = NULL; | |
363 | struct spu *spu; | |
364 | int node, n; | |
365 | ||
366 | /* | |
367 | * Look for a possible preemption candidate on the local node first. | |
368 | * If there is no candidate look at the other nodes. This isn't | |
369 | * exactly fair, but so far the whole spu schedule tries to keep | |
370 | * a strong node affinity. We might want to fine-tune this in | |
371 | * the future. | |
372 | */ | |
373 | restart: | |
374 | node = cpu_to_node(raw_smp_processor_id()); | |
375 | for (n = 0; n < MAX_NUMNODES; n++, node++) { | |
376 | node = (node < MAX_NUMNODES) ? node : 0; | |
ea1ae594 | 377 | if (!node_allowed(ctx, node)) |
52f04fcf CH |
378 | continue; |
379 | ||
380 | mutex_lock(&spu_prio->active_mutex[node]); | |
381 | list_for_each_entry(spu, &spu_prio->active_list[node], list) { | |
382 | struct spu_context *tmp = spu->ctx; | |
383 | ||
fe443ef2 CH |
384 | if (tmp->prio > ctx->prio && |
385 | (!victim || tmp->prio > victim->prio)) | |
52f04fcf CH |
386 | victim = spu->ctx; |
387 | } | |
388 | mutex_unlock(&spu_prio->active_mutex[node]); | |
389 | ||
390 | if (victim) { | |
391 | /* | |
392 | * This nests ctx->state_mutex, but we always lock | |
393 | * higher priority contexts before lower priority | |
394 | * ones, so this is safe until we introduce | |
395 | * priority inheritance schemes. | |
396 | */ | |
397 | if (!mutex_trylock(&victim->state_mutex)) { | |
398 | victim = NULL; | |
399 | goto restart; | |
400 | } | |
401 | ||
402 | spu = victim->spu; | |
403 | if (!spu) { | |
404 | /* | |
405 | * This race can happen because we've dropped | |
406 | * the active list mutex. No a problem, just | |
407 | * restart the search. | |
408 | */ | |
409 | mutex_unlock(&victim->state_mutex); | |
410 | victim = NULL; | |
411 | goto restart; | |
412 | } | |
37901802 | 413 | spu_remove_from_active_list(spu); |
52f04fcf | 414 | spu_unbind_context(spu, victim); |
e9f8a0b6 | 415 | victim->stats.invol_ctx_switch++; |
52f04fcf | 416 | mutex_unlock(&victim->state_mutex); |
e097b513 CH |
417 | /* |
418 | * We need to break out of the wait loop in spu_run | |
419 | * manually to ensure this context gets put on the | |
420 | * runqueue again ASAP. | |
421 | */ | |
422 | wake_up(&victim->stop_wq); | |
52f04fcf CH |
423 | return spu; |
424 | } | |
425 | } | |
426 | ||
427 | return NULL; | |
428 | } | |
429 | ||
079cdb61 CH |
430 | /** |
431 | * spu_activate - find a free spu for a context and execute it | |
432 | * @ctx: spu context to schedule | |
433 | * @flags: flags (currently ignored) | |
434 | * | |
08873095 | 435 | * Tries to find a free spu to run @ctx. If no free spu is available |
079cdb61 CH |
436 | * add the context to the runqueue so it gets woken up once an spu |
437 | * is available. | |
438 | */ | |
26bec673 | 439 | int spu_activate(struct spu_context *ctx, unsigned long flags) |
8b3d6663 | 440 | { |
e9f8a0b6 | 441 | spuctx_switch_state(ctx, SPUCTX_UTIL_SYSTEM); |
8b3d6663 | 442 | |
079cdb61 CH |
443 | if (ctx->spu) |
444 | return 0; | |
445 | ||
446 | do { | |
447 | struct spu *spu; | |
448 | ||
449 | spu = spu_get_idle(ctx); | |
52f04fcf CH |
450 | /* |
451 | * If this is a realtime thread we try to get it running by | |
452 | * preempting a lower priority thread. | |
453 | */ | |
fe443ef2 | 454 | if (!spu && rt_prio(ctx->prio)) |
52f04fcf | 455 | spu = find_victim(ctx); |
079cdb61 | 456 | if (spu) { |
202557d2 | 457 | spu_bind_context(spu, ctx); |
37901802 | 458 | spu_add_to_active_list(spu); |
079cdb61 | 459 | return 0; |
a68cf983 | 460 | } |
079cdb61 | 461 | |
50b520d4 | 462 | spu_prio_wait(ctx); |
079cdb61 CH |
463 | } while (!signal_pending(current)); |
464 | ||
465 | return -ERESTARTSYS; | |
8b3d6663 AB |
466 | } |
467 | ||
bb5db29a CH |
468 | /** |
469 | * grab_runnable_context - try to find a runnable context | |
470 | * | |
471 | * Remove the highest priority context on the runqueue and return it | |
472 | * to the caller. Returns %NULL if no runnable context was found. | |
473 | */ | |
ea1ae594 | 474 | static struct spu_context *grab_runnable_context(int prio, int node) |
bb5db29a | 475 | { |
ea1ae594 | 476 | struct spu_context *ctx; |
bb5db29a CH |
477 | int best; |
478 | ||
479 | spin_lock(&spu_prio->runq_lock); | |
480 | best = sched_find_first_bit(spu_prio->bitmap); | |
ea1ae594 | 481 | while (best < prio) { |
bb5db29a CH |
482 | struct list_head *rq = &spu_prio->runq[best]; |
483 | ||
ea1ae594 CH |
484 | list_for_each_entry(ctx, rq, rq) { |
485 | /* XXX(hch): check for affinity here aswell */ | |
486 | if (__node_allowed(ctx, node)) { | |
487 | __spu_del_from_rq(ctx); | |
488 | goto found; | |
489 | } | |
490 | } | |
491 | best++; | |
bb5db29a | 492 | } |
ea1ae594 CH |
493 | ctx = NULL; |
494 | found: | |
bb5db29a | 495 | spin_unlock(&spu_prio->runq_lock); |
bb5db29a CH |
496 | return ctx; |
497 | } | |
498 | ||
499 | static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio) | |
500 | { | |
501 | struct spu *spu = ctx->spu; | |
502 | struct spu_context *new = NULL; | |
503 | ||
504 | if (spu) { | |
ea1ae594 | 505 | new = grab_runnable_context(max_prio, spu->node); |
bb5db29a | 506 | if (new || force) { |
37901802 | 507 | spu_remove_from_active_list(spu); |
bb5db29a | 508 | spu_unbind_context(spu, ctx); |
e9f8a0b6 | 509 | ctx->stats.vol_ctx_switch++; |
bb5db29a CH |
510 | spu_free(spu); |
511 | if (new) | |
512 | wake_up(&new->stop_wq); | |
513 | } | |
514 | ||
515 | } | |
516 | ||
517 | return new != NULL; | |
518 | } | |
519 | ||
678b2ff1 CH |
520 | /** |
521 | * spu_deactivate - unbind a context from it's physical spu | |
522 | * @ctx: spu context to unbind | |
523 | * | |
524 | * Unbind @ctx from the physical spu it is running on and schedule | |
525 | * the highest priority context to run on the freed physical spu. | |
526 | */ | |
8b3d6663 AB |
527 | void spu_deactivate(struct spu_context *ctx) |
528 | { | |
46cbf939 CH |
529 | /* |
530 | * We must never reach this for a nosched context, | |
531 | * but handle the case gracefull instead of panicing. | |
532 | */ | |
533 | if (ctx->flags & SPU_CREATE_NOSCHED) { | |
534 | WARN_ON(1); | |
535 | return; | |
536 | } | |
537 | ||
bb5db29a | 538 | __spu_deactivate(ctx, 1, MAX_PRIO); |
e9f8a0b6 | 539 | spuctx_switch_state(ctx, SPUCTX_UTIL_USER); |
8b3d6663 AB |
540 | } |
541 | ||
ae7b4c52 CH |
542 | /** |
543 | * spu_yield - yield a physical spu if others are waiting | |
544 | * @ctx: spu context to yield | |
545 | * | |
546 | * Check if there is a higher priority context waiting and if yes | |
547 | * unbind @ctx from the physical spu and schedule the highest | |
548 | * priority context to run on the freed physical spu instead. | |
549 | */ | |
8b3d6663 AB |
550 | void spu_yield(struct spu_context *ctx) |
551 | { | |
e5c0b9ec CH |
552 | if (!(ctx->flags & SPU_CREATE_NOSCHED)) { |
553 | mutex_lock(&ctx->state_mutex); | |
e9f8a0b6 CH |
554 | if (__spu_deactivate(ctx, 0, MAX_PRIO)) |
555 | spuctx_switch_state(ctx, SPUCTX_UTIL_USER); | |
556 | else | |
557 | spuctx_switch_state(ctx, SPUCTX_UTIL_LOADED); | |
e5c0b9ec CH |
558 | mutex_unlock(&ctx->state_mutex); |
559 | } | |
bb5db29a | 560 | } |
8b3d6663 | 561 | |
37901802 | 562 | static void spusched_tick(struct spu_context *ctx) |
bb5db29a | 563 | { |
df09cf3e CH |
564 | if (ctx->flags & SPU_CREATE_NOSCHED) |
565 | return; | |
566 | if (ctx->policy == SCHED_FIFO) | |
567 | return; | |
568 | ||
569 | if (--ctx->time_slice) | |
37901802 | 570 | return; |
bb5db29a CH |
571 | |
572 | /* | |
37901802 CH |
573 | * Unfortunately active_mutex ranks outside of state_mutex, so |
574 | * we have to trylock here. If we fail give the context another | |
575 | * tick and try again. | |
bb5db29a | 576 | */ |
37901802 | 577 | if (mutex_trylock(&ctx->state_mutex)) { |
7022543e | 578 | struct spu *spu = ctx->spu; |
ea1ae594 CH |
579 | struct spu_context *new; |
580 | ||
581 | new = grab_runnable_context(ctx->prio + 1, spu->node); | |
37901802 | 582 | if (new) { |
bb5db29a | 583 | |
37901802 CH |
584 | __spu_remove_from_active_list(spu); |
585 | spu_unbind_context(spu, ctx); | |
e9f8a0b6 | 586 | ctx->stats.invol_ctx_switch++; |
37901802 CH |
587 | spu_free(spu); |
588 | wake_up(&new->stop_wq); | |
589 | /* | |
590 | * We need to break out of the wait loop in | |
591 | * spu_run manually to ensure this context | |
592 | * gets put on the runqueue again ASAP. | |
593 | */ | |
594 | wake_up(&ctx->stop_wq); | |
595 | } | |
fe443ef2 | 596 | spu_set_timeslice(ctx); |
37901802 | 597 | mutex_unlock(&ctx->state_mutex); |
bb5db29a | 598 | } else { |
37901802 | 599 | ctx->time_slice++; |
8b3d6663 | 600 | } |
8b3d6663 AB |
601 | } |
602 | ||
65de66f0 CH |
603 | /** |
604 | * count_active_contexts - count nr of active tasks | |
605 | * | |
606 | * Return the number of tasks currently running or waiting to run. | |
607 | * | |
608 | * Note that we don't take runq_lock / active_mutex here. Reading | |
609 | * a single 32bit value is atomic on powerpc, and we don't care | |
610 | * about memory ordering issues here. | |
611 | */ | |
612 | static unsigned long count_active_contexts(void) | |
613 | { | |
614 | int nr_active = 0, node; | |
615 | ||
616 | for (node = 0; node < MAX_NUMNODES; node++) | |
617 | nr_active += spu_prio->nr_active[node]; | |
618 | nr_active += spu_prio->nr_waiting; | |
619 | ||
620 | return nr_active; | |
621 | } | |
622 | ||
623 | /** | |
624 | * spu_calc_load - given tick count, update the avenrun load estimates. | |
625 | * @tick: tick count | |
626 | * | |
627 | * No locking against reading these values from userspace, as for | |
628 | * the CPU loadavg code. | |
629 | */ | |
630 | static void spu_calc_load(unsigned long ticks) | |
631 | { | |
632 | unsigned long active_tasks; /* fixed-point */ | |
633 | static int count = LOAD_FREQ; | |
634 | ||
635 | count -= ticks; | |
636 | ||
637 | if (unlikely(count < 0)) { | |
638 | active_tasks = count_active_contexts() * FIXED_1; | |
639 | do { | |
640 | CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks); | |
641 | CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks); | |
642 | CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks); | |
643 | count += LOAD_FREQ; | |
644 | } while (count < 0); | |
645 | } | |
646 | } | |
647 | ||
37901802 CH |
648 | static void spusched_wake(unsigned long data) |
649 | { | |
650 | mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK); | |
651 | wake_up_process(spusched_task); | |
65de66f0 | 652 | spu_calc_load(SPUSCHED_TICK); |
37901802 CH |
653 | } |
654 | ||
655 | static int spusched_thread(void *unused) | |
656 | { | |
657 | struct spu *spu, *next; | |
658 | int node; | |
659 | ||
37901802 CH |
660 | while (!kthread_should_stop()) { |
661 | set_current_state(TASK_INTERRUPTIBLE); | |
662 | schedule(); | |
663 | for (node = 0; node < MAX_NUMNODES; node++) { | |
664 | mutex_lock(&spu_prio->active_mutex[node]); | |
665 | list_for_each_entry_safe(spu, next, | |
666 | &spu_prio->active_list[node], | |
667 | list) | |
668 | spusched_tick(spu->ctx); | |
669 | mutex_unlock(&spu_prio->active_mutex[node]); | |
670 | } | |
671 | } | |
672 | ||
37901802 CH |
673 | return 0; |
674 | } | |
675 | ||
65de66f0 CH |
676 | #define LOAD_INT(x) ((x) >> FSHIFT) |
677 | #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) | |
678 | ||
679 | static int show_spu_loadavg(struct seq_file *s, void *private) | |
680 | { | |
681 | int a, b, c; | |
682 | ||
683 | a = spu_avenrun[0] + (FIXED_1/200); | |
684 | b = spu_avenrun[1] + (FIXED_1/200); | |
685 | c = spu_avenrun[2] + (FIXED_1/200); | |
686 | ||
687 | /* | |
688 | * Note that last_pid doesn't really make much sense for the | |
689 | * SPU loadavg (it even seems very odd on the CPU side..), | |
690 | * but we include it here to have a 100% compatible interface. | |
691 | */ | |
692 | seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n", | |
693 | LOAD_INT(a), LOAD_FRAC(a), | |
694 | LOAD_INT(b), LOAD_FRAC(b), | |
695 | LOAD_INT(c), LOAD_FRAC(c), | |
696 | count_active_contexts(), | |
697 | atomic_read(&nr_spu_contexts), | |
698 | current->nsproxy->pid_ns->last_pid); | |
699 | return 0; | |
700 | } | |
701 | ||
702 | static int spu_loadavg_open(struct inode *inode, struct file *file) | |
703 | { | |
704 | return single_open(file, show_spu_loadavg, NULL); | |
705 | } | |
706 | ||
707 | static const struct file_operations spu_loadavg_fops = { | |
708 | .open = spu_loadavg_open, | |
709 | .read = seq_read, | |
710 | .llseek = seq_lseek, | |
711 | .release = single_release, | |
712 | }; | |
713 | ||
8b3d6663 AB |
714 | int __init spu_sched_init(void) |
715 | { | |
65de66f0 CH |
716 | struct proc_dir_entry *entry; |
717 | int err = -ENOMEM, i; | |
8b3d6663 | 718 | |
a68cf983 | 719 | spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL); |
37901802 | 720 | if (!spu_prio) |
65de66f0 | 721 | goto out; |
37901802 | 722 | |
8b3d6663 | 723 | for (i = 0; i < MAX_PRIO; i++) { |
079cdb61 | 724 | INIT_LIST_HEAD(&spu_prio->runq[i]); |
a68cf983 | 725 | __clear_bit(i, spu_prio->bitmap); |
8b3d6663 | 726 | } |
a68cf983 MN |
727 | __set_bit(MAX_PRIO, spu_prio->bitmap); |
728 | for (i = 0; i < MAX_NUMNODES; i++) { | |
729 | mutex_init(&spu_prio->active_mutex[i]); | |
730 | INIT_LIST_HEAD(&spu_prio->active_list[i]); | |
8b3d6663 | 731 | } |
079cdb61 | 732 | spin_lock_init(&spu_prio->runq_lock); |
37901802 | 733 | |
c77239b8 CH |
734 | setup_timer(&spusched_timer, spusched_wake, 0); |
735 | ||
37901802 CH |
736 | spusched_task = kthread_run(spusched_thread, NULL, "spusched"); |
737 | if (IS_ERR(spusched_task)) { | |
65de66f0 CH |
738 | err = PTR_ERR(spusched_task); |
739 | goto out_free_spu_prio; | |
37901802 | 740 | } |
f3f59bec | 741 | |
65de66f0 CH |
742 | entry = create_proc_entry("spu_loadavg", 0, NULL); |
743 | if (!entry) | |
744 | goto out_stop_kthread; | |
745 | entry->proc_fops = &spu_loadavg_fops; | |
746 | ||
f3f59bec JK |
747 | pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n", |
748 | SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE); | |
8b3d6663 | 749 | return 0; |
37901802 | 750 | |
65de66f0 CH |
751 | out_stop_kthread: |
752 | kthread_stop(spusched_task); | |
753 | out_free_spu_prio: | |
754 | kfree(spu_prio); | |
755 | out: | |
756 | return err; | |
8b3d6663 AB |
757 | } |
758 | ||
759 | void __exit spu_sched_exit(void) | |
760 | { | |
a68cf983 MN |
761 | struct spu *spu, *tmp; |
762 | int node; | |
763 | ||
65de66f0 CH |
764 | remove_proc_entry("spu_loadavg", NULL); |
765 | ||
c77239b8 | 766 | del_timer_sync(&spusched_timer); |
37901802 CH |
767 | kthread_stop(spusched_task); |
768 | ||
a68cf983 MN |
769 | for (node = 0; node < MAX_NUMNODES; node++) { |
770 | mutex_lock(&spu_prio->active_mutex[node]); | |
771 | list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node], | |
772 | list) { | |
773 | list_del_init(&spu->list); | |
774 | spu_free(spu); | |
775 | } | |
776 | mutex_unlock(&spu_prio->active_mutex[node]); | |
8b3d6663 | 777 | } |
a68cf983 | 778 | kfree(spu_prio); |
8b3d6663 | 779 | } |