]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/powerpc/platforms/cell/spufs/sched.c
[CELL] spu_base: locking cleanup
[mirror_ubuntu-hirsute-kernel.git] / arch / powerpc / platforms / cell / spufs / sched.c
CommitLineData
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 50struct 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 60static unsigned long spu_avenrun[3];
a68cf983 61static struct spu_prio_array *spu_prio;
37901802
CH
62static struct task_struct *spusched_task;
63static 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 */
97void 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 */
108void __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
140void 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 149static 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
161static 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 */
176static 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
186static 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 196static 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
205static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
206
207static 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
213int spu_switch_event_register(struct notifier_block * n)
214{
215 return blocking_notifier_chain_register(&spu_switch_notifier, n);
216}
217
218int 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 */
228static 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);
27ec41d3 232 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
e9f8a0b6 233
aa6d5b20
AB
234 if (ctx->flags & SPU_CREATE_NOSCHED)
235 atomic_inc(&cbe_spu_info[spu->node].reserved_spus);
c5fc8d2a
AB
236 if (!list_empty(&ctx->aff_list))
237 atomic_inc(&ctx->gang->aff_sched_count);
aa6d5b20 238
e9f8a0b6
CH
239 ctx->stats.slb_flt_base = spu->stats.slb_flt;
240 ctx->stats.class2_intr_base = spu->stats.class2_intr;
241
8b3d6663
AB
242 spu->ctx = ctx;
243 spu->flags = 0;
244 ctx->spu = spu;
245 ctx->ops = &spu_hw_ops;
246 spu->pid = current->pid;
94b2a439 247 spu_associate_mm(spu, ctx->owner);
8b3d6663
AB
248 spu->ibox_callback = spufs_ibox_callback;
249 spu->wbox_callback = spufs_wbox_callback;
5110459f 250 spu->stop_callback = spufs_stop_callback;
a33a7d73 251 spu->mfc_callback = spufs_mfc_callback;
9add11da 252 spu->dma_callback = spufs_dma_callback;
8b3d6663 253 mb();
5110459f 254 spu_unmap_mappings(ctx);
8b3d6663 255 spu_restore(&ctx->csa, spu);
2a911f0b 256 spu->timestamp = jiffies;
a68cf983 257 spu_cpu_affinity_set(spu, raw_smp_processor_id());
86767277 258 spu_switch_notify(spu, ctx);
81998baf 259 ctx->state = SPU_STATE_RUNNABLE;
27ec41d3
AD
260
261 spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
8b3d6663
AB
262}
263
c5fc8d2a
AB
264/*
265 * XXX(hch): needs locking.
266 */
267static inline int sched_spu(struct spu *spu)
268{
269 return (!spu->ctx || !(spu->ctx->flags & SPU_CREATE_NOSCHED));
270}
271
272static void aff_merge_remaining_ctxs(struct spu_gang *gang)
273{
274 struct spu_context *ctx;
275
276 list_for_each_entry(ctx, &gang->aff_list_head, aff_list) {
277 if (list_empty(&ctx->aff_list))
278 list_add(&ctx->aff_list, &gang->aff_list_head);
279 }
280 gang->aff_flags |= AFF_MERGED;
281}
282
283static void aff_set_offsets(struct spu_gang *gang)
284{
285 struct spu_context *ctx;
286 int offset;
287
288 offset = -1;
289 list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,
290 aff_list) {
291 if (&ctx->aff_list == &gang->aff_list_head)
292 break;
293 ctx->aff_offset = offset--;
294 }
295
296 offset = 0;
297 list_for_each_entry(ctx, gang->aff_ref_ctx->aff_list.prev, aff_list) {
298 if (&ctx->aff_list == &gang->aff_list_head)
299 break;
300 ctx->aff_offset = offset++;
301 }
302
303 gang->aff_flags |= AFF_OFFSETS_SET;
304}
305
306static struct spu *aff_ref_location(struct spu_context *ctx, int mem_aff,
307 int group_size, int lowest_offset)
308{
309 struct spu *spu;
310 int node, n;
311
312 /*
313 * TODO: A better algorithm could be used to find a good spu to be
314 * used as reference location for the ctxs chain.
315 */
316 node = cpu_to_node(raw_smp_processor_id());
317 for (n = 0; n < MAX_NUMNODES; n++, node++) {
318 node = (node < MAX_NUMNODES) ? node : 0;
319 if (!node_allowed(ctx, node))
320 continue;
321 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
322 if ((!mem_aff || spu->has_mem_affinity) &&
323 sched_spu(spu))
324 return spu;
325 }
326 }
327 return NULL;
328}
329
330static void aff_set_ref_point_location(struct spu_gang *gang)
331{
332 int mem_aff, gs, lowest_offset;
333 struct spu_context *ctx;
334 struct spu *tmp;
335
336 mem_aff = gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM;
337 lowest_offset = 0;
338 gs = 0;
339
340 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
341 gs++;
342
343 list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,
344 aff_list) {
345 if (&ctx->aff_list == &gang->aff_list_head)
346 break;
347 lowest_offset = ctx->aff_offset;
348 }
349
350 gang->aff_ref_spu = aff_ref_location(ctx, mem_aff, gs, lowest_offset);
351}
352
353static struct spu *ctx_location(struct spu *ref, int offset)
354{
355 struct spu *spu;
356
357 spu = NULL;
358 if (offset >= 0) {
359 list_for_each_entry(spu, ref->aff_list.prev, aff_list) {
360 if (offset == 0)
361 break;
362 if (sched_spu(spu))
363 offset--;
364 }
365 } else {
366 list_for_each_entry_reverse(spu, ref->aff_list.next, aff_list) {
367 if (offset == 0)
368 break;
369 if (sched_spu(spu))
370 offset++;
371 }
372 }
373 return spu;
374}
375
376/*
377 * affinity_check is called each time a context is going to be scheduled.
378 * It returns the spu ptr on which the context must run.
379 */
380struct spu *affinity_check(struct spu_context *ctx)
381{
382 struct spu_gang *gang;
383
384 if (list_empty(&ctx->aff_list))
385 return NULL;
386 gang = ctx->gang;
387 mutex_lock(&gang->aff_mutex);
388 if (!gang->aff_ref_spu) {
389 if (!(gang->aff_flags & AFF_MERGED))
390 aff_merge_remaining_ctxs(gang);
391 if (!(gang->aff_flags & AFF_OFFSETS_SET))
392 aff_set_offsets(gang);
393 aff_set_ref_point_location(gang);
394 }
395 mutex_unlock(&gang->aff_mutex);
396 if (!gang->aff_ref_spu)
397 return NULL;
398 return ctx_location(gang->aff_ref_spu, ctx->aff_offset);
399}
400
202557d2
CH
401/**
402 * spu_unbind_context - unbind spu context from physical spu
403 * @spu: physical spu to unbind from
404 * @ctx: context to unbind
202557d2 405 */
678b2ff1 406static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
8b3d6663 407{
a68cf983
MN
408 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
409 spu->pid, spu->number, spu->node);
27ec41d3 410 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
fe2f896d 411
aa6d5b20
AB
412 if (spu->ctx->flags & SPU_CREATE_NOSCHED)
413 atomic_dec(&cbe_spu_info[spu->node].reserved_spus);
c5fc8d2a
AB
414 if (!list_empty(&ctx->aff_list))
415 if (atomic_dec_and_test(&ctx->gang->aff_sched_count))
416 ctx->gang->aff_ref_spu = NULL;
86767277 417 spu_switch_notify(spu, NULL);
5110459f 418 spu_unmap_mappings(ctx);
8b3d6663 419 spu_save(&ctx->csa, spu);
2a911f0b 420 spu->timestamp = jiffies;
8b3d6663
AB
421 ctx->state = SPU_STATE_SAVED;
422 spu->ibox_callback = NULL;
423 spu->wbox_callback = NULL;
5110459f 424 spu->stop_callback = NULL;
a33a7d73 425 spu->mfc_callback = NULL;
9add11da 426 spu->dma_callback = NULL;
94b2a439 427 spu_associate_mm(spu, NULL);
8b3d6663 428 spu->pid = 0;
8b3d6663 429 ctx->ops = &spu_backing_ops;
2a911f0b 430 spu->flags = 0;
8b3d6663 431 spu->ctx = NULL;
e9f8a0b6
CH
432
433 ctx->stats.slb_flt +=
434 (spu->stats.slb_flt - ctx->stats.slb_flt_base);
435 ctx->stats.class2_intr +=
436 (spu->stats.class2_intr - ctx->stats.class2_intr_base);
27ec41d3
AD
437
438 /* This maps the underlying spu state to idle */
439 spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
440 ctx->spu = NULL;
8b3d6663
AB
441}
442
079cdb61
CH
443/**
444 * spu_add_to_rq - add a context to the runqueue
445 * @ctx: context to add
446 */
4e0f4ed0 447static void __spu_add_to_rq(struct spu_context *ctx)
8b3d6663 448{
27449971
CH
449 /*
450 * Unfortunately this code path can be called from multiple threads
451 * on behalf of a single context due to the way the problem state
452 * mmap support works.
453 *
454 * Fortunately we need to wake up all these threads at the same time
455 * and can simply skip the runqueue addition for every but the first
456 * thread getting into this codepath.
457 *
458 * It's still quite hacky, and long-term we should proxy all other
459 * threads through the owner thread so that spu_run is in control
460 * of all the scheduling activity for a given context.
461 */
462 if (list_empty(&ctx->rq)) {
463 list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
464 set_bit(ctx->prio, spu_prio->bitmap);
465 if (!spu_prio->nr_waiting++)
466 __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
467 }
2a911f0b 468}
5110459f 469
4e0f4ed0 470static void __spu_del_from_rq(struct spu_context *ctx)
a475c2f4 471{
4e0f4ed0
LB
472 int prio = ctx->prio;
473
65de66f0 474 if (!list_empty(&ctx->rq)) {
c77239b8
CH
475 if (!--spu_prio->nr_waiting)
476 del_timer(&spusched_timer);
a475c2f4 477 list_del_init(&ctx->rq);
c77239b8
CH
478
479 if (list_empty(&spu_prio->runq[prio]))
480 clear_bit(prio, spu_prio->bitmap);
65de66f0 481 }
079cdb61 482}
a68cf983 483
079cdb61 484static void spu_prio_wait(struct spu_context *ctx)
8b3d6663 485{
a68cf983 486 DEFINE_WAIT(wait);
8b3d6663 487
4e0f4ed0 488 spin_lock(&spu_prio->runq_lock);
079cdb61 489 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
a68cf983 490 if (!signal_pending(current)) {
4e0f4ed0
LB
491 __spu_add_to_rq(ctx);
492 spin_unlock(&spu_prio->runq_lock);
650f8b02 493 mutex_unlock(&ctx->state_mutex);
a68cf983 494 schedule();
650f8b02 495 mutex_lock(&ctx->state_mutex);
4e0f4ed0
LB
496 spin_lock(&spu_prio->runq_lock);
497 __spu_del_from_rq(ctx);
8b3d6663 498 }
4e0f4ed0 499 spin_unlock(&spu_prio->runq_lock);
079cdb61
CH
500 __set_current_state(TASK_RUNNING);
501 remove_wait_queue(&ctx->stop_wq, &wait);
8b3d6663
AB
502}
503
079cdb61 504static struct spu *spu_get_idle(struct spu_context *ctx)
a68cf983
MN
505{
506 struct spu *spu = NULL;
507 int node = cpu_to_node(raw_smp_processor_id());
508 int n;
509
cbc23d3e
AB
510 spu = affinity_check(ctx);
511 if (spu)
512 return spu_alloc_spu(spu);
513
a68cf983
MN
514 for (n = 0; n < MAX_NUMNODES; n++, node++) {
515 node = (node < MAX_NUMNODES) ? node : 0;
ea1ae594 516 if (!node_allowed(ctx, node))
a68cf983
MN
517 continue;
518 spu = spu_alloc_node(node);
519 if (spu)
520 break;
521 }
522 return spu;
523}
8b3d6663 524
52f04fcf
CH
525/**
526 * find_victim - find a lower priority context to preempt
527 * @ctx: canidate context for running
528 *
529 * Returns the freed physical spu to run the new context on.
530 */
531static struct spu *find_victim(struct spu_context *ctx)
532{
533 struct spu_context *victim = NULL;
534 struct spu *spu;
535 int node, n;
536
537 /*
538 * Look for a possible preemption candidate on the local node first.
539 * If there is no candidate look at the other nodes. This isn't
540 * exactly fair, but so far the whole spu schedule tries to keep
541 * a strong node affinity. We might want to fine-tune this in
542 * the future.
543 */
544 restart:
545 node = cpu_to_node(raw_smp_processor_id());
546 for (n = 0; n < MAX_NUMNODES; n++, node++) {
547 node = (node < MAX_NUMNODES) ? node : 0;
ea1ae594 548 if (!node_allowed(ctx, node))
52f04fcf
CH
549 continue;
550
551 mutex_lock(&spu_prio->active_mutex[node]);
552 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
553 struct spu_context *tmp = spu->ctx;
554
fe443ef2
CH
555 if (tmp->prio > ctx->prio &&
556 (!victim || tmp->prio > victim->prio))
52f04fcf
CH
557 victim = spu->ctx;
558 }
559 mutex_unlock(&spu_prio->active_mutex[node]);
560
561 if (victim) {
562 /*
563 * This nests ctx->state_mutex, but we always lock
564 * higher priority contexts before lower priority
565 * ones, so this is safe until we introduce
566 * priority inheritance schemes.
567 */
568 if (!mutex_trylock(&victim->state_mutex)) {
569 victim = NULL;
570 goto restart;
571 }
572
573 spu = victim->spu;
574 if (!spu) {
575 /*
576 * This race can happen because we've dropped
577 * the active list mutex. No a problem, just
578 * restart the search.
579 */
580 mutex_unlock(&victim->state_mutex);
581 victim = NULL;
582 goto restart;
583 }
37901802 584 spu_remove_from_active_list(spu);
52f04fcf 585 spu_unbind_context(spu, victim);
e9f8a0b6 586 victim->stats.invol_ctx_switch++;
fe2f896d 587 spu->stats.invol_ctx_switch++;
52f04fcf 588 mutex_unlock(&victim->state_mutex);
e097b513
CH
589 /*
590 * We need to break out of the wait loop in spu_run
591 * manually to ensure this context gets put on the
592 * runqueue again ASAP.
593 */
594 wake_up(&victim->stop_wq);
52f04fcf
CH
595 return spu;
596 }
597 }
598
599 return NULL;
600}
601
079cdb61
CH
602/**
603 * spu_activate - find a free spu for a context and execute it
604 * @ctx: spu context to schedule
605 * @flags: flags (currently ignored)
606 *
08873095 607 * Tries to find a free spu to run @ctx. If no free spu is available
079cdb61
CH
608 * add the context to the runqueue so it gets woken up once an spu
609 * is available.
610 */
26bec673 611int spu_activate(struct spu_context *ctx, unsigned long flags)
8b3d6663 612{
079cdb61
CH
613 do {
614 struct spu *spu;
615
27449971
CH
616 /*
617 * If there are multiple threads waiting for a single context
618 * only one actually binds the context while the others will
619 * only be able to acquire the state_mutex once the context
620 * already is in runnable state.
621 */
622 if (ctx->spu)
623 return 0;
624
079cdb61 625 spu = spu_get_idle(ctx);
52f04fcf
CH
626 /*
627 * If this is a realtime thread we try to get it running by
628 * preempting a lower priority thread.
629 */
fe443ef2 630 if (!spu && rt_prio(ctx->prio))
52f04fcf 631 spu = find_victim(ctx);
079cdb61 632 if (spu) {
202557d2 633 spu_bind_context(spu, ctx);
37901802 634 spu_add_to_active_list(spu);
079cdb61 635 return 0;
a68cf983 636 }
079cdb61 637
50b520d4 638 spu_prio_wait(ctx);
079cdb61
CH
639 } while (!signal_pending(current));
640
641 return -ERESTARTSYS;
8b3d6663
AB
642}
643
bb5db29a
CH
644/**
645 * grab_runnable_context - try to find a runnable context
646 *
647 * Remove the highest priority context on the runqueue and return it
648 * to the caller. Returns %NULL if no runnable context was found.
649 */
ea1ae594 650static struct spu_context *grab_runnable_context(int prio, int node)
bb5db29a 651{
ea1ae594 652 struct spu_context *ctx;
bb5db29a
CH
653 int best;
654
655 spin_lock(&spu_prio->runq_lock);
7e90b749 656 best = find_first_bit(spu_prio->bitmap, prio);
ea1ae594 657 while (best < prio) {
bb5db29a
CH
658 struct list_head *rq = &spu_prio->runq[best];
659
ea1ae594
CH
660 list_for_each_entry(ctx, rq, rq) {
661 /* XXX(hch): check for affinity here aswell */
662 if (__node_allowed(ctx, node)) {
663 __spu_del_from_rq(ctx);
664 goto found;
665 }
666 }
667 best++;
bb5db29a 668 }
ea1ae594
CH
669 ctx = NULL;
670 found:
bb5db29a 671 spin_unlock(&spu_prio->runq_lock);
bb5db29a
CH
672 return ctx;
673}
674
675static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)
676{
677 struct spu *spu = ctx->spu;
678 struct spu_context *new = NULL;
679
680 if (spu) {
ea1ae594 681 new = grab_runnable_context(max_prio, spu->node);
bb5db29a 682 if (new || force) {
37901802 683 spu_remove_from_active_list(spu);
bb5db29a 684 spu_unbind_context(spu, ctx);
e9f8a0b6 685 ctx->stats.vol_ctx_switch++;
fe2f896d 686 spu->stats.vol_ctx_switch++;
bb5db29a
CH
687 spu_free(spu);
688 if (new)
689 wake_up(&new->stop_wq);
690 }
691
692 }
693
694 return new != NULL;
695}
696
678b2ff1
CH
697/**
698 * spu_deactivate - unbind a context from it's physical spu
699 * @ctx: spu context to unbind
700 *
701 * Unbind @ctx from the physical spu it is running on and schedule
702 * the highest priority context to run on the freed physical spu.
703 */
8b3d6663
AB
704void spu_deactivate(struct spu_context *ctx)
705{
bb5db29a 706 __spu_deactivate(ctx, 1, MAX_PRIO);
8b3d6663
AB
707}
708
ae7b4c52
CH
709/**
710 * spu_yield - yield a physical spu if others are waiting
711 * @ctx: spu context to yield
712 *
713 * Check if there is a higher priority context waiting and if yes
714 * unbind @ctx from the physical spu and schedule the highest
715 * priority context to run on the freed physical spu instead.
716 */
8b3d6663
AB
717void spu_yield(struct spu_context *ctx)
718{
e5c0b9ec
CH
719 if (!(ctx->flags & SPU_CREATE_NOSCHED)) {
720 mutex_lock(&ctx->state_mutex);
27ec41d3 721 __spu_deactivate(ctx, 0, MAX_PRIO);
e5c0b9ec
CH
722 mutex_unlock(&ctx->state_mutex);
723 }
bb5db29a 724}
8b3d6663 725
37901802 726static void spusched_tick(struct spu_context *ctx)
bb5db29a 727{
df09cf3e
CH
728 if (ctx->flags & SPU_CREATE_NOSCHED)
729 return;
730 if (ctx->policy == SCHED_FIFO)
731 return;
732
733 if (--ctx->time_slice)
37901802 734 return;
bb5db29a
CH
735
736 /*
37901802
CH
737 * Unfortunately active_mutex ranks outside of state_mutex, so
738 * we have to trylock here. If we fail give the context another
739 * tick and try again.
bb5db29a 740 */
37901802 741 if (mutex_trylock(&ctx->state_mutex)) {
7022543e 742 struct spu *spu = ctx->spu;
ea1ae594
CH
743 struct spu_context *new;
744
745 new = grab_runnable_context(ctx->prio + 1, spu->node);
37901802 746 if (new) {
bb5db29a 747
37901802
CH
748 __spu_remove_from_active_list(spu);
749 spu_unbind_context(spu, ctx);
e9f8a0b6 750 ctx->stats.invol_ctx_switch++;
fe2f896d 751 spu->stats.invol_ctx_switch++;
37901802
CH
752 spu_free(spu);
753 wake_up(&new->stop_wq);
754 /*
755 * We need to break out of the wait loop in
756 * spu_run manually to ensure this context
757 * gets put on the runqueue again ASAP.
758 */
759 wake_up(&ctx->stop_wq);
760 }
fe443ef2 761 spu_set_timeslice(ctx);
37901802 762 mutex_unlock(&ctx->state_mutex);
bb5db29a 763 } else {
37901802 764 ctx->time_slice++;
8b3d6663 765 }
8b3d6663
AB
766}
767
65de66f0
CH
768/**
769 * count_active_contexts - count nr of active tasks
770 *
771 * Return the number of tasks currently running or waiting to run.
772 *
773 * Note that we don't take runq_lock / active_mutex here. Reading
774 * a single 32bit value is atomic on powerpc, and we don't care
775 * about memory ordering issues here.
776 */
777static unsigned long count_active_contexts(void)
778{
779 int nr_active = 0, node;
780
781 for (node = 0; node < MAX_NUMNODES; node++)
782 nr_active += spu_prio->nr_active[node];
783 nr_active += spu_prio->nr_waiting;
784
785 return nr_active;
786}
787
788/**
789 * spu_calc_load - given tick count, update the avenrun load estimates.
790 * @tick: tick count
791 *
792 * No locking against reading these values from userspace, as for
793 * the CPU loadavg code.
794 */
795static void spu_calc_load(unsigned long ticks)
796{
797 unsigned long active_tasks; /* fixed-point */
798 static int count = LOAD_FREQ;
799
800 count -= ticks;
801
802 if (unlikely(count < 0)) {
803 active_tasks = count_active_contexts() * FIXED_1;
804 do {
805 CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks);
806 CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks);
807 CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks);
808 count += LOAD_FREQ;
809 } while (count < 0);
810 }
811}
812
37901802
CH
813static void spusched_wake(unsigned long data)
814{
815 mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
816 wake_up_process(spusched_task);
65de66f0 817 spu_calc_load(SPUSCHED_TICK);
37901802
CH
818}
819
820static int spusched_thread(void *unused)
821{
822 struct spu *spu, *next;
823 int node;
824
37901802
CH
825 while (!kthread_should_stop()) {
826 set_current_state(TASK_INTERRUPTIBLE);
827 schedule();
828 for (node = 0; node < MAX_NUMNODES; node++) {
829 mutex_lock(&spu_prio->active_mutex[node]);
830 list_for_each_entry_safe(spu, next,
831 &spu_prio->active_list[node],
832 list)
833 spusched_tick(spu->ctx);
834 mutex_unlock(&spu_prio->active_mutex[node]);
835 }
836 }
837
37901802
CH
838 return 0;
839}
840
65de66f0
CH
841#define LOAD_INT(x) ((x) >> FSHIFT)
842#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
843
844static int show_spu_loadavg(struct seq_file *s, void *private)
845{
846 int a, b, c;
847
848 a = spu_avenrun[0] + (FIXED_1/200);
849 b = spu_avenrun[1] + (FIXED_1/200);
850 c = spu_avenrun[2] + (FIXED_1/200);
851
852 /*
853 * Note that last_pid doesn't really make much sense for the
854 * SPU loadavg (it even seems very odd on the CPU side..),
855 * but we include it here to have a 100% compatible interface.
856 */
857 seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
858 LOAD_INT(a), LOAD_FRAC(a),
859 LOAD_INT(b), LOAD_FRAC(b),
860 LOAD_INT(c), LOAD_FRAC(c),
861 count_active_contexts(),
862 atomic_read(&nr_spu_contexts),
863 current->nsproxy->pid_ns->last_pid);
864 return 0;
865}
866
867static int spu_loadavg_open(struct inode *inode, struct file *file)
868{
869 return single_open(file, show_spu_loadavg, NULL);
870}
871
872static const struct file_operations spu_loadavg_fops = {
873 .open = spu_loadavg_open,
874 .read = seq_read,
875 .llseek = seq_lseek,
876 .release = single_release,
877};
878
8b3d6663
AB
879int __init spu_sched_init(void)
880{
65de66f0
CH
881 struct proc_dir_entry *entry;
882 int err = -ENOMEM, i;
8b3d6663 883
a68cf983 884 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
37901802 885 if (!spu_prio)
65de66f0 886 goto out;
37901802 887
8b3d6663 888 for (i = 0; i < MAX_PRIO; i++) {
079cdb61 889 INIT_LIST_HEAD(&spu_prio->runq[i]);
a68cf983 890 __clear_bit(i, spu_prio->bitmap);
8b3d6663 891 }
a68cf983
MN
892 for (i = 0; i < MAX_NUMNODES; i++) {
893 mutex_init(&spu_prio->active_mutex[i]);
894 INIT_LIST_HEAD(&spu_prio->active_list[i]);
8b3d6663 895 }
079cdb61 896 spin_lock_init(&spu_prio->runq_lock);
37901802 897
c77239b8
CH
898 setup_timer(&spusched_timer, spusched_wake, 0);
899
37901802
CH
900 spusched_task = kthread_run(spusched_thread, NULL, "spusched");
901 if (IS_ERR(spusched_task)) {
65de66f0
CH
902 err = PTR_ERR(spusched_task);
903 goto out_free_spu_prio;
37901802 904 }
f3f59bec 905
65de66f0
CH
906 entry = create_proc_entry("spu_loadavg", 0, NULL);
907 if (!entry)
908 goto out_stop_kthread;
909 entry->proc_fops = &spu_loadavg_fops;
910
f3f59bec
JK
911 pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",
912 SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);
8b3d6663 913 return 0;
37901802 914
65de66f0
CH
915 out_stop_kthread:
916 kthread_stop(spusched_task);
917 out_free_spu_prio:
918 kfree(spu_prio);
919 out:
920 return err;
8b3d6663
AB
921}
922
d1450317 923void spu_sched_exit(void)
8b3d6663 924{
a68cf983
MN
925 struct spu *spu, *tmp;
926 int node;
927
65de66f0
CH
928 remove_proc_entry("spu_loadavg", NULL);
929
c77239b8 930 del_timer_sync(&spusched_timer);
37901802
CH
931 kthread_stop(spusched_task);
932
a68cf983
MN
933 for (node = 0; node < MAX_NUMNODES; node++) {
934 mutex_lock(&spu_prio->active_mutex[node]);
935 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
936 list) {
937 list_del_init(&spu->list);
938 spu_free(spu);
939 }
940 mutex_unlock(&spu_prio->active_mutex[node]);
8b3d6663 941 }
a68cf983 942 kfree(spu_prio);
8b3d6663 943}