]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/cell/spufs/sched.c
7bb5229b1e3c7b002cf388fd79a148969ea418d1
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / cell / spufs / sched.c
1 /* sched.c - SPU scheduler.
2  *
3  * Copyright (C) IBM 2005
4  * Author: Mark Nutter <mnutter@us.ibm.com>
5  *
6  * 2006-03-31   NUMA domains added.
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
23 #undef DEBUG
24
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>
33 #include <linux/stddef.h>
34 #include <linux/unistd.h>
35 #include <linux/numa.h>
36 #include <linux/mutex.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39
40 #include <asm/io.h>
41 #include <asm/mmu_context.h>
42 #include <asm/spu.h>
43 #include <asm/spu_csa.h>
44 #include <asm/spu_priv1.h>
45 #include "spufs.h"
46
47 struct spu_prio_array {
48         DECLARE_BITMAP(bitmap, MAX_PRIO);
49         struct list_head runq[MAX_PRIO];
50         spinlock_t runq_lock;
51         struct list_head active_list[MAX_NUMNODES];
52         struct mutex active_mutex[MAX_NUMNODES];
53 };
54
55 static struct spu_prio_array *spu_prio;
56 static struct task_struct *spusched_task;
57 static struct timer_list spusched_timer;
58
59 /*
60  * Priority of a normal, non-rt, non-niced'd process (aka nice level 0).
61  */
62 #define NORMAL_PRIO             120
63
64 /*
65  * Frequency of the spu scheduler tick.  By default we do one SPU scheduler
66  * tick for every 10 CPU scheduler ticks.
67  */
68 #define SPUSCHED_TICK           (10)
69
70 /*
71  * These are the 'tuning knobs' of the scheduler:
72  *
73  * Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is
74  * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs.
75  */
76 #define MIN_SPU_TIMESLICE       max(5 * HZ / (1000 * SPUSCHED_TICK), 1)
77 #define DEF_SPU_TIMESLICE       (100 * HZ / (1000 * SPUSCHED_TICK))
78
79 #define MAX_USER_PRIO           (MAX_PRIO - MAX_RT_PRIO)
80 #define SCALE_PRIO(x, prio) \
81         max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE)
82
83 /*
84  * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values:
85  * [800ms ... 100ms ... 5ms]
86  *
87  * The higher a thread's priority, the bigger timeslices
88  * it gets during one round of execution. But even the lowest
89  * priority thread gets MIN_TIMESLICE worth of execution time.
90  */
91 void spu_set_timeslice(struct spu_context *ctx)
92 {
93         if (ctx->prio < NORMAL_PRIO)
94                 ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio);
95         else
96                 ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio);
97 }
98
99 /*
100  * Update scheduling information from the owning thread.
101  */
102 void __spu_update_sched_info(struct spu_context *ctx)
103 {
104         /*
105          * We do our own priority calculations, so we normally want
106          * ->static_prio to start with. Unfortunately thies field
107          * contains junk for threads with a realtime scheduling
108          * policy so we have to look at ->prio in this case.
109          */
110         if (rt_prio(current->prio))
111                 ctx->prio = current->prio;
112         else
113                 ctx->prio = current->static_prio;
114         ctx->policy = current->policy;
115
116         /*
117          * A lot of places that don't hold active_mutex poke into
118          * cpus_allowed, including grab_runnable_context which
119          * already holds the runq_lock.  So abuse runq_lock
120          * to protect this field aswell.
121          */
122         spin_lock(&spu_prio->runq_lock);
123         ctx->cpus_allowed = current->cpus_allowed;
124         spin_unlock(&spu_prio->runq_lock);
125 }
126
127 void spu_update_sched_info(struct spu_context *ctx)
128 {
129         int node = ctx->spu->node;
130
131         mutex_lock(&spu_prio->active_mutex[node]);
132         __spu_update_sched_info(ctx);
133         mutex_unlock(&spu_prio->active_mutex[node]);
134 }
135
136 static int __node_allowed(struct spu_context *ctx, int node)
137 {
138         if (nr_cpus_node(node)) {
139                 cpumask_t mask = node_to_cpumask(node);
140
141                 if (cpus_intersects(mask, ctx->cpus_allowed))
142                         return 1;
143         }
144
145         return 0;
146 }
147
148 static int node_allowed(struct spu_context *ctx, int node)
149 {
150         int rval;
151
152         spin_lock(&spu_prio->runq_lock);
153         rval = __node_allowed(ctx, node);
154         spin_unlock(&spu_prio->runq_lock);
155
156         return rval;
157 }
158
159 /**
160  * spu_add_to_active_list - add spu to active list
161  * @spu:        spu to add to the active list
162  */
163 static void spu_add_to_active_list(struct spu *spu)
164 {
165         mutex_lock(&spu_prio->active_mutex[spu->node]);
166         list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
167         mutex_unlock(&spu_prio->active_mutex[spu->node]);
168 }
169
170 static void __spu_remove_from_active_list(struct spu *spu)
171 {
172         list_del_init(&spu->list);
173 }
174
175 /**
176  * spu_remove_from_active_list - remove spu from active list
177  * @spu:       spu to remove from the active list
178  */
179 static void spu_remove_from_active_list(struct spu *spu)
180 {
181         int node = spu->node;
182
183         mutex_lock(&spu_prio->active_mutex[node]);
184         __spu_remove_from_active_list(spu);
185         mutex_unlock(&spu_prio->active_mutex[node]);
186 }
187
188 static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
189
190 static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
191 {
192         blocking_notifier_call_chain(&spu_switch_notifier,
193                             ctx ? ctx->object_id : 0, spu);
194 }
195
196 int spu_switch_event_register(struct notifier_block * n)
197 {
198         return blocking_notifier_chain_register(&spu_switch_notifier, n);
199 }
200
201 int spu_switch_event_unregister(struct notifier_block * n)
202 {
203         return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
204 }
205
206 /**
207  * spu_bind_context - bind spu context to physical spu
208  * @spu:        physical spu to bind to
209  * @ctx:        context to bind
210  */
211 static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
212 {
213         pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
214                  spu->number, spu->node);
215         spu->ctx = ctx;
216         spu->flags = 0;
217         ctx->spu = spu;
218         ctx->ops = &spu_hw_ops;
219         spu->pid = current->pid;
220         spu_associate_mm(spu, ctx->owner);
221         spu->ibox_callback = spufs_ibox_callback;
222         spu->wbox_callback = spufs_wbox_callback;
223         spu->stop_callback = spufs_stop_callback;
224         spu->mfc_callback = spufs_mfc_callback;
225         spu->dma_callback = spufs_dma_callback;
226         mb();
227         spu_unmap_mappings(ctx);
228         spu_restore(&ctx->csa, spu);
229         spu->timestamp = jiffies;
230         spu_cpu_affinity_set(spu, raw_smp_processor_id());
231         spu_switch_notify(spu, ctx);
232         ctx->state = SPU_STATE_RUNNABLE;
233 }
234
235 /**
236  * spu_unbind_context - unbind spu context from physical spu
237  * @spu:        physical spu to unbind from
238  * @ctx:        context to unbind
239  */
240 static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
241 {
242         pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
243                  spu->pid, spu->number, spu->node);
244
245         spu_switch_notify(spu, NULL);
246         spu_unmap_mappings(ctx);
247         spu_save(&ctx->csa, spu);
248         spu->timestamp = jiffies;
249         ctx->state = SPU_STATE_SAVED;
250         spu->ibox_callback = NULL;
251         spu->wbox_callback = NULL;
252         spu->stop_callback = NULL;
253         spu->mfc_callback = NULL;
254         spu->dma_callback = NULL;
255         spu_associate_mm(spu, NULL);
256         spu->pid = 0;
257         ctx->ops = &spu_backing_ops;
258         ctx->spu = NULL;
259         spu->flags = 0;
260         spu->ctx = NULL;
261 }
262
263 /**
264  * spu_add_to_rq - add a context to the runqueue
265  * @ctx:       context to add
266  */
267 static void __spu_add_to_rq(struct spu_context *ctx)
268 {
269         int prio = ctx->prio;
270
271         list_add_tail(&ctx->rq, &spu_prio->runq[prio]);
272         set_bit(prio, spu_prio->bitmap);
273 }
274
275 static void __spu_del_from_rq(struct spu_context *ctx)
276 {
277         int prio = ctx->prio;
278
279         if (!list_empty(&ctx->rq))
280                 list_del_init(&ctx->rq);
281         if (list_empty(&spu_prio->runq[prio]))
282                 clear_bit(prio, spu_prio->bitmap);
283 }
284
285 static void spu_prio_wait(struct spu_context *ctx)
286 {
287         DEFINE_WAIT(wait);
288
289         spin_lock(&spu_prio->runq_lock);
290         prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
291         if (!signal_pending(current)) {
292                 __spu_add_to_rq(ctx);
293                 spin_unlock(&spu_prio->runq_lock);
294                 mutex_unlock(&ctx->state_mutex);
295                 schedule();
296                 mutex_lock(&ctx->state_mutex);
297                 spin_lock(&spu_prio->runq_lock);
298                 __spu_del_from_rq(ctx);
299         }
300         spin_unlock(&spu_prio->runq_lock);
301         __set_current_state(TASK_RUNNING);
302         remove_wait_queue(&ctx->stop_wq, &wait);
303 }
304
305 static struct spu *spu_get_idle(struct spu_context *ctx)
306 {
307         struct spu *spu = NULL;
308         int node = cpu_to_node(raw_smp_processor_id());
309         int n;
310
311         for (n = 0; n < MAX_NUMNODES; n++, node++) {
312                 node = (node < MAX_NUMNODES) ? node : 0;
313                 if (!node_allowed(ctx, node))
314                         continue;
315                 spu = spu_alloc_node(node);
316                 if (spu)
317                         break;
318         }
319         return spu;
320 }
321
322 /**
323  * find_victim - find a lower priority context to preempt
324  * @ctx:        canidate context for running
325  *
326  * Returns the freed physical spu to run the new context on.
327  */
328 static struct spu *find_victim(struct spu_context *ctx)
329 {
330         struct spu_context *victim = NULL;
331         struct spu *spu;
332         int node, n;
333
334         /*
335          * Look for a possible preemption candidate on the local node first.
336          * If there is no candidate look at the other nodes.  This isn't
337          * exactly fair, but so far the whole spu schedule tries to keep
338          * a strong node affinity.  We might want to fine-tune this in
339          * the future.
340          */
341  restart:
342         node = cpu_to_node(raw_smp_processor_id());
343         for (n = 0; n < MAX_NUMNODES; n++, node++) {
344                 node = (node < MAX_NUMNODES) ? node : 0;
345                 if (!node_allowed(ctx, node))
346                         continue;
347
348                 mutex_lock(&spu_prio->active_mutex[node]);
349                 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
350                         struct spu_context *tmp = spu->ctx;
351
352                         if (tmp->prio > ctx->prio &&
353                             (!victim || tmp->prio > victim->prio))
354                                 victim = spu->ctx;
355                 }
356                 mutex_unlock(&spu_prio->active_mutex[node]);
357
358                 if (victim) {
359                         /*
360                          * This nests ctx->state_mutex, but we always lock
361                          * higher priority contexts before lower priority
362                          * ones, so this is safe until we introduce
363                          * priority inheritance schemes.
364                          */
365                         if (!mutex_trylock(&victim->state_mutex)) {
366                                 victim = NULL;
367                                 goto restart;
368                         }
369
370                         spu = victim->spu;
371                         if (!spu) {
372                                 /*
373                                  * This race can happen because we've dropped
374                                  * the active list mutex.  No a problem, just
375                                  * restart the search.
376                                  */
377                                 mutex_unlock(&victim->state_mutex);
378                                 victim = NULL;
379                                 goto restart;
380                         }
381                         spu_remove_from_active_list(spu);
382                         spu_unbind_context(spu, victim);
383                         mutex_unlock(&victim->state_mutex);
384                         /*
385                          * We need to break out of the wait loop in spu_run
386                          * manually to ensure this context gets put on the
387                          * runqueue again ASAP.
388                          */
389                         wake_up(&victim->stop_wq);
390                         return spu;
391                 }
392         }
393
394         return NULL;
395 }
396
397 /**
398  * spu_activate - find a free spu for a context and execute it
399  * @ctx:        spu context to schedule
400  * @flags:      flags (currently ignored)
401  *
402  * Tries to find a free spu to run @ctx.  If no free spu is available
403  * add the context to the runqueue so it gets woken up once an spu
404  * is available.
405  */
406 int spu_activate(struct spu_context *ctx, unsigned long flags)
407 {
408
409         if (ctx->spu)
410                 return 0;
411
412         do {
413                 struct spu *spu;
414
415                 spu = spu_get_idle(ctx);
416                 /*
417                  * If this is a realtime thread we try to get it running by
418                  * preempting a lower priority thread.
419                  */
420                 if (!spu && rt_prio(ctx->prio))
421                         spu = find_victim(ctx);
422                 if (spu) {
423                         spu_bind_context(spu, ctx);
424                         spu_add_to_active_list(spu);
425                         return 0;
426                 }
427
428                 spu_prio_wait(ctx);
429         } while (!signal_pending(current));
430
431         return -ERESTARTSYS;
432 }
433
434 /**
435  * grab_runnable_context - try to find a runnable context
436  *
437  * Remove the highest priority context on the runqueue and return it
438  * to the caller.  Returns %NULL if no runnable context was found.
439  */
440 static struct spu_context *grab_runnable_context(int prio, int node)
441 {
442         struct spu_context *ctx;
443         int best;
444
445         spin_lock(&spu_prio->runq_lock);
446         best = sched_find_first_bit(spu_prio->bitmap);
447         while (best < prio) {
448                 struct list_head *rq = &spu_prio->runq[best];
449
450                 list_for_each_entry(ctx, rq, rq) {
451                         /* XXX(hch): check for affinity here aswell */
452                         if (__node_allowed(ctx, node)) {
453                                 __spu_del_from_rq(ctx);
454                                 goto found;
455                         }
456                 }
457                 best++;
458         }
459         ctx = NULL;
460  found:
461         spin_unlock(&spu_prio->runq_lock);
462         return ctx;
463 }
464
465 static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)
466 {
467         struct spu *spu = ctx->spu;
468         struct spu_context *new = NULL;
469
470         if (spu) {
471                 new = grab_runnable_context(max_prio, spu->node);
472                 if (new || force) {
473                         spu_remove_from_active_list(spu);
474                         spu_unbind_context(spu, ctx);
475                         spu_free(spu);
476                         if (new)
477                                 wake_up(&new->stop_wq);
478                 }
479
480         }
481
482         return new != NULL;
483 }
484
485 /**
486  * spu_deactivate - unbind a context from it's physical spu
487  * @ctx:        spu context to unbind
488  *
489  * Unbind @ctx from the physical spu it is running on and schedule
490  * the highest priority context to run on the freed physical spu.
491  */
492 void spu_deactivate(struct spu_context *ctx)
493 {
494         /*
495          * We must never reach this for a nosched context,
496          * but handle the case gracefull instead of panicing.
497          */
498         if (ctx->flags & SPU_CREATE_NOSCHED) {
499                 WARN_ON(1);
500                 return;
501         }
502
503         __spu_deactivate(ctx, 1, MAX_PRIO);
504 }
505
506 /**
507  * spu_yield -  yield a physical spu if others are waiting
508  * @ctx:        spu context to yield
509  *
510  * Check if there is a higher priority context waiting and if yes
511  * unbind @ctx from the physical spu and schedule the highest
512  * priority context to run on the freed physical spu instead.
513  */
514 void spu_yield(struct spu_context *ctx)
515 {
516         if (!(ctx->flags & SPU_CREATE_NOSCHED)) {
517                 mutex_lock(&ctx->state_mutex);
518                 __spu_deactivate(ctx, 0, MAX_PRIO);
519                 mutex_unlock(&ctx->state_mutex);
520         }
521 }
522
523 static void spusched_tick(struct spu_context *ctx)
524 {
525         if (ctx->flags & SPU_CREATE_NOSCHED)
526                 return;
527         if (ctx->policy == SCHED_FIFO)
528                 return;
529
530         if (--ctx->time_slice)
531                 return;
532
533         /*
534          * Unfortunately active_mutex ranks outside of state_mutex, so
535          * we have to trylock here.  If we fail give the context another
536          * tick and try again.
537          */
538         if (mutex_trylock(&ctx->state_mutex)) {
539                 struct spu *spu = ctx->spu;
540                 struct spu_context *new;
541
542                 new = grab_runnable_context(ctx->prio + 1, spu->node);
543                 if (new) {
544
545                         __spu_remove_from_active_list(spu);
546                         spu_unbind_context(spu, ctx);
547                         spu_free(spu);
548                         wake_up(&new->stop_wq);
549                         /*
550                          * We need to break out of the wait loop in
551                          * spu_run manually to ensure this context
552                          * gets put on the runqueue again ASAP.
553                          */
554                         wake_up(&ctx->stop_wq);
555                 }
556                 spu_set_timeslice(ctx);
557                 mutex_unlock(&ctx->state_mutex);
558         } else {
559                 ctx->time_slice++;
560         }
561 }
562
563 static void spusched_wake(unsigned long data)
564 {
565         mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
566         wake_up_process(spusched_task);
567 }
568
569 static int spusched_thread(void *unused)
570 {
571         struct spu *spu, *next;
572         int node;
573
574         setup_timer(&spusched_timer, spusched_wake, 0);
575         __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
576
577         while (!kthread_should_stop()) {
578                 set_current_state(TASK_INTERRUPTIBLE);
579                 schedule();
580                 for (node = 0; node < MAX_NUMNODES; node++) {
581                         mutex_lock(&spu_prio->active_mutex[node]);
582                         list_for_each_entry_safe(spu, next,
583                                                  &spu_prio->active_list[node],
584                                                  list)
585                                 spusched_tick(spu->ctx);
586                         mutex_unlock(&spu_prio->active_mutex[node]);
587                 }
588         }
589
590         del_timer_sync(&spusched_timer);
591         return 0;
592 }
593
594 int __init spu_sched_init(void)
595 {
596         int i;
597
598         spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
599         if (!spu_prio)
600                 return -ENOMEM;
601
602         for (i = 0; i < MAX_PRIO; i++) {
603                 INIT_LIST_HEAD(&spu_prio->runq[i]);
604                 __clear_bit(i, spu_prio->bitmap);
605         }
606         __set_bit(MAX_PRIO, spu_prio->bitmap);
607         for (i = 0; i < MAX_NUMNODES; i++) {
608                 mutex_init(&spu_prio->active_mutex[i]);
609                 INIT_LIST_HEAD(&spu_prio->active_list[i]);
610         }
611         spin_lock_init(&spu_prio->runq_lock);
612
613         spusched_task = kthread_run(spusched_thread, NULL, "spusched");
614         if (IS_ERR(spusched_task)) {
615                 kfree(spu_prio);
616                 return PTR_ERR(spusched_task);
617         }
618
619         pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",
620                         SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);
621         return 0;
622
623 }
624
625 void __exit spu_sched_exit(void)
626 {
627         struct spu *spu, *tmp;
628         int node;
629
630         kthread_stop(spusched_task);
631
632         for (node = 0; node < MAX_NUMNODES; node++) {
633                 mutex_lock(&spu_prio->active_mutex[node]);
634                 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
635                                          list) {
636                         list_del_init(&spu->list);
637                         spu_free(spu);
638                 }
639                 mutex_unlock(&spu_prio->active_mutex[node]);
640         }
641         kfree(spu_prio);
642 }