]> err.no Git - linux-2.6/blob - arch/powerpc/platforms/cell/spufs/sched.c
[POWERPC] spufs: remove woken threads from the runqueue early
[linux-2.6] / 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/smp_lock.h>
34 #include <linux/stddef.h>
35 #include <linux/unistd.h>
36 #include <linux/numa.h>
37 #include <linux/mutex.h>
38 #include <linux/notifier.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 #define SPU_TIMESLICE   (HZ)
48
49 struct spu_prio_array {
50         DECLARE_BITMAP(bitmap, MAX_PRIO);
51         struct list_head runq[MAX_PRIO];
52         spinlock_t runq_lock;
53         struct list_head active_list[MAX_NUMNODES];
54         struct mutex active_mutex[MAX_NUMNODES];
55 };
56
57 static struct spu_prio_array *spu_prio;
58 static struct workqueue_struct *spu_sched_wq;
59
60 static inline int node_allowed(int node)
61 {
62         cpumask_t mask;
63
64         if (!nr_cpus_node(node))
65                 return 0;
66         mask = node_to_cpumask(node);
67         if (!cpus_intersects(mask, current->cpus_allowed))
68                 return 0;
69         return 1;
70 }
71
72 void spu_start_tick(struct spu_context *ctx)
73 {
74         if (ctx->policy == SCHED_RR) {
75                 /*
76                  * Make sure the exiting bit is cleared.
77                  */
78                 clear_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
79                 mb();
80                 queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE);
81         }
82 }
83
84 void spu_stop_tick(struct spu_context *ctx)
85 {
86         if (ctx->policy == SCHED_RR) {
87                 /*
88                  * While the work can be rearming normally setting this flag
89                  * makes sure it does not rearm itself anymore.
90                  */
91                 set_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
92                 mb();
93                 cancel_delayed_work(&ctx->sched_work);
94         }
95 }
96
97 void spu_sched_tick(struct work_struct *work)
98 {
99         struct spu_context *ctx =
100                 container_of(work, struct spu_context, sched_work.work);
101         struct spu *spu;
102         int preempted = 0;
103
104         /*
105          * If this context is being stopped avoid rescheduling from the
106          * scheduler tick because we would block on the state_mutex.
107          * The caller will yield the spu later on anyway.
108          */
109         if (test_bit(SPU_SCHED_EXITING, &ctx->sched_flags))
110                 return;
111
112         mutex_lock(&ctx->state_mutex);
113         spu = ctx->spu;
114         if (spu) {
115                 int best = sched_find_first_bit(spu_prio->bitmap);
116                 if (best <= ctx->prio) {
117                         spu_deactivate(ctx);
118                         preempted = 1;
119                 }
120         }
121         mutex_unlock(&ctx->state_mutex);
122
123         if (preempted) {
124                 /*
125                  * We need to break out of the wait loop in spu_run manually
126                  * to ensure this context gets put on the runqueue again
127                  * ASAP.
128                  */
129                 wake_up(&ctx->stop_wq);
130         } else
131                 spu_start_tick(ctx);
132 }
133
134 /**
135  * spu_add_to_active_list - add spu to active list
136  * @spu:        spu to add to the active list
137  */
138 static void spu_add_to_active_list(struct spu *spu)
139 {
140         mutex_lock(&spu_prio->active_mutex[spu->node]);
141         list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
142         mutex_unlock(&spu_prio->active_mutex[spu->node]);
143 }
144
145 /**
146  * spu_remove_from_active_list - remove spu from active list
147  * @spu:       spu to remove from the active list
148  */
149 static void spu_remove_from_active_list(struct spu *spu)
150 {
151         int node = spu->node;
152
153         mutex_lock(&spu_prio->active_mutex[node]);
154         list_del_init(&spu->list);
155         mutex_unlock(&spu_prio->active_mutex[node]);
156 }
157
158 static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
159
160 static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
161 {
162         blocking_notifier_call_chain(&spu_switch_notifier,
163                             ctx ? ctx->object_id : 0, spu);
164 }
165
166 int spu_switch_event_register(struct notifier_block * n)
167 {
168         return blocking_notifier_chain_register(&spu_switch_notifier, n);
169 }
170
171 int spu_switch_event_unregister(struct notifier_block * n)
172 {
173         return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
174 }
175
176 /**
177  * spu_bind_context - bind spu context to physical spu
178  * @spu:        physical spu to bind to
179  * @ctx:        context to bind
180  */
181 static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
182 {
183         pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
184                  spu->number, spu->node);
185         spu->ctx = ctx;
186         spu->flags = 0;
187         ctx->spu = spu;
188         ctx->ops = &spu_hw_ops;
189         spu->pid = current->pid;
190         spu_associate_mm(spu, ctx->owner);
191         spu->ibox_callback = spufs_ibox_callback;
192         spu->wbox_callback = spufs_wbox_callback;
193         spu->stop_callback = spufs_stop_callback;
194         spu->mfc_callback = spufs_mfc_callback;
195         spu->dma_callback = spufs_dma_callback;
196         mb();
197         spu_unmap_mappings(ctx);
198         spu_restore(&ctx->csa, spu);
199         spu->timestamp = jiffies;
200         spu_cpu_affinity_set(spu, raw_smp_processor_id());
201         spu_switch_notify(spu, ctx);
202         spu_add_to_active_list(spu);
203         ctx->state = SPU_STATE_RUNNABLE;
204 }
205
206 /**
207  * spu_unbind_context - unbind spu context from physical spu
208  * @spu:        physical spu to unbind from
209  * @ctx:        context to unbind
210  */
211 static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
212 {
213         pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
214                  spu->pid, spu->number, spu->node);
215
216         spu_remove_from_active_list(spu);
217         spu_switch_notify(spu, NULL);
218         spu_unmap_mappings(ctx);
219         spu_save(&ctx->csa, spu);
220         spu->timestamp = jiffies;
221         ctx->state = SPU_STATE_SAVED;
222         spu->ibox_callback = NULL;
223         spu->wbox_callback = NULL;
224         spu->stop_callback = NULL;
225         spu->mfc_callback = NULL;
226         spu->dma_callback = NULL;
227         spu_associate_mm(spu, NULL);
228         spu->pid = 0;
229         ctx->ops = &spu_backing_ops;
230         ctx->spu = NULL;
231         spu->flags = 0;
232         spu->ctx = NULL;
233 }
234
235 /**
236  * spu_add_to_rq - add a context to the runqueue
237  * @ctx:       context to add
238  */
239 static void spu_add_to_rq(struct spu_context *ctx)
240 {
241         spin_lock(&spu_prio->runq_lock);
242         list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
243         set_bit(ctx->prio, spu_prio->bitmap);
244         mb();
245         spin_unlock(&spu_prio->runq_lock);
246 }
247
248 static void __spu_del_from_rq(struct spu_context *ctx, int prio)
249 {
250         if (!list_empty(&ctx->rq))
251                 list_del_init(&ctx->rq);
252         if (list_empty(&spu_prio->runq[prio]))
253                 clear_bit(ctx->prio, spu_prio->bitmap);
254 }
255
256 /**
257  * spu_del_from_rq - remove a context from the runqueue
258  * @ctx:       context to remove
259  */
260 static void spu_del_from_rq(struct spu_context *ctx)
261 {
262         spin_lock(&spu_prio->runq_lock);
263         __spu_del_from_rq(ctx, ctx->prio);
264         spin_unlock(&spu_prio->runq_lock);
265 }
266
267 static void spu_prio_wait(struct spu_context *ctx)
268 {
269         DEFINE_WAIT(wait);
270
271         prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
272         if (!signal_pending(current)) {
273                 mutex_unlock(&ctx->state_mutex);
274                 schedule();
275                 mutex_lock(&ctx->state_mutex);
276         }
277         __set_current_state(TASK_RUNNING);
278         remove_wait_queue(&ctx->stop_wq, &wait);
279 }
280
281 /**
282  * spu_reschedule - try to find a runnable context for a spu
283  * @spu:       spu available
284  *
285  * This function is called whenever a spu becomes idle.  It looks for the
286  * most suitable runnable spu context and schedules it for execution.
287  */
288 static void spu_reschedule(struct spu *spu)
289 {
290         int best;
291
292         spu_free(spu);
293
294         spin_lock(&spu_prio->runq_lock);
295         best = sched_find_first_bit(spu_prio->bitmap);
296         if (best < MAX_PRIO) {
297                 struct list_head *rq = &spu_prio->runq[best];
298                 struct spu_context *ctx;
299
300                 BUG_ON(list_empty(rq));
301
302                 ctx = list_entry(rq->next, struct spu_context, rq);
303                 __spu_del_from_rq(ctx, best);
304                 wake_up(&ctx->stop_wq);
305         }
306         spin_unlock(&spu_prio->runq_lock);
307 }
308
309 static struct spu *spu_get_idle(struct spu_context *ctx)
310 {
311         struct spu *spu = NULL;
312         int node = cpu_to_node(raw_smp_processor_id());
313         int n;
314
315         for (n = 0; n < MAX_NUMNODES; n++, node++) {
316                 node = (node < MAX_NUMNODES) ? node : 0;
317                 if (!node_allowed(node))
318                         continue;
319                 spu = spu_alloc_node(node);
320                 if (spu)
321                         break;
322         }
323         return spu;
324 }
325
326 /**
327  * find_victim - find a lower priority context to preempt
328  * @ctx:        canidate context for running
329  *
330  * Returns the freed physical spu to run the new context on.
331  */
332 static struct spu *find_victim(struct spu_context *ctx)
333 {
334         struct spu_context *victim = NULL;
335         struct spu *spu;
336         int node, n;
337
338         /*
339          * Look for a possible preemption candidate on the local node first.
340          * If there is no candidate look at the other nodes.  This isn't
341          * exactly fair, but so far the whole spu schedule tries to keep
342          * a strong node affinity.  We might want to fine-tune this in
343          * the future.
344          */
345  restart:
346         node = cpu_to_node(raw_smp_processor_id());
347         for (n = 0; n < MAX_NUMNODES; n++, node++) {
348                 node = (node < MAX_NUMNODES) ? node : 0;
349                 if (!node_allowed(node))
350                         continue;
351
352                 mutex_lock(&spu_prio->active_mutex[node]);
353                 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
354                         struct spu_context *tmp = spu->ctx;
355
356                         if (tmp->rt_priority < ctx->rt_priority &&
357                             (!victim || tmp->rt_priority < victim->rt_priority))
358                                 victim = spu->ctx;
359                 }
360                 mutex_unlock(&spu_prio->active_mutex[node]);
361
362                 if (victim) {
363                         /*
364                          * This nests ctx->state_mutex, but we always lock
365                          * higher priority contexts before lower priority
366                          * ones, so this is safe until we introduce
367                          * priority inheritance schemes.
368                          */
369                         if (!mutex_trylock(&victim->state_mutex)) {
370                                 victim = NULL;
371                                 goto restart;
372                         }
373
374                         spu = victim->spu;
375                         if (!spu) {
376                                 /*
377                                  * This race can happen because we've dropped
378                                  * the active list mutex.  No a problem, just
379                                  * restart the search.
380                                  */
381                                 mutex_unlock(&victim->state_mutex);
382                                 victim = NULL;
383                                 goto restart;
384                         }
385                         spu_unbind_context(spu, victim);
386                         mutex_unlock(&victim->state_mutex);
387                         /*
388                          * We need to break out of the wait loop in spu_run
389                          * manually to ensure this context gets put on the
390                          * runqueue again ASAP.
391                          */
392                         wake_up(&victim->stop_wq);
393                         return spu;
394                 }
395         }
396
397         return NULL;
398 }
399
400 /**
401  * spu_activate - find a free spu for a context and execute it
402  * @ctx:        spu context to schedule
403  * @flags:      flags (currently ignored)
404  *
405  * Tries to find a free spu to run @ctx.  If no free spu is available
406  * add the context to the runqueue so it gets woken up once an spu
407  * is available.
408  */
409 int spu_activate(struct spu_context *ctx, unsigned long flags)
410 {
411
412         if (ctx->spu)
413                 return 0;
414
415         do {
416                 struct spu *spu;
417
418                 spu = spu_get_idle(ctx);
419                 /*
420                  * If this is a realtime thread we try to get it running by
421                  * preempting a lower priority thread.
422                  */
423                 if (!spu && ctx->rt_priority)
424                         spu = find_victim(ctx);
425                 if (spu) {
426                         spu_bind_context(spu, ctx);
427                         return 0;
428                 }
429
430                 spu_add_to_rq(ctx);
431                 spu_prio_wait(ctx);
432                 spu_del_from_rq(ctx);
433         } while (!signal_pending(current));
434
435         return -ERESTARTSYS;
436 }
437
438 /**
439  * spu_deactivate - unbind a context from it's physical spu
440  * @ctx:        spu context to unbind
441  *
442  * Unbind @ctx from the physical spu it is running on and schedule
443  * the highest priority context to run on the freed physical spu.
444  */
445 void spu_deactivate(struct spu_context *ctx)
446 {
447         struct spu *spu = ctx->spu;
448
449         if (spu) {
450                 spu_unbind_context(spu, ctx);
451                 spu_reschedule(spu);
452         }
453 }
454
455 /**
456  * spu_yield -  yield a physical spu if others are waiting
457  * @ctx:        spu context to yield
458  *
459  * Check if there is a higher priority context waiting and if yes
460  * unbind @ctx from the physical spu and schedule the highest
461  * priority context to run on the freed physical spu instead.
462  */
463 void spu_yield(struct spu_context *ctx)
464 {
465         struct spu *spu;
466
467         if (mutex_trylock(&ctx->state_mutex)) {
468                 if ((spu = ctx->spu) != NULL) {
469                         int best = sched_find_first_bit(spu_prio->bitmap);
470                         if (best < MAX_PRIO) {
471                                 pr_debug("%s: yielding SPU %d NODE %d\n",
472                                          __FUNCTION__, spu->number, spu->node);
473                                 spu_deactivate(ctx);
474                         }
475                 }
476                 mutex_unlock(&ctx->state_mutex);
477         }
478 }
479
480 int __init spu_sched_init(void)
481 {
482         int i;
483
484         spu_sched_wq = create_singlethread_workqueue("spusched");
485         if (!spu_sched_wq)
486                 return 1;
487
488         spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
489         if (!spu_prio) {
490                 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
491                        __FUNCTION__);
492                        destroy_workqueue(spu_sched_wq);
493                 return 1;
494         }
495         for (i = 0; i < MAX_PRIO; i++) {
496                 INIT_LIST_HEAD(&spu_prio->runq[i]);
497                 __clear_bit(i, spu_prio->bitmap);
498         }
499         __set_bit(MAX_PRIO, spu_prio->bitmap);
500         for (i = 0; i < MAX_NUMNODES; i++) {
501                 mutex_init(&spu_prio->active_mutex[i]);
502                 INIT_LIST_HEAD(&spu_prio->active_list[i]);
503         }
504         spin_lock_init(&spu_prio->runq_lock);
505         return 0;
506 }
507
508 void __exit spu_sched_exit(void)
509 {
510         struct spu *spu, *tmp;
511         int node;
512
513         for (node = 0; node < MAX_NUMNODES; node++) {
514                 mutex_lock(&spu_prio->active_mutex[node]);
515                 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
516                                          list) {
517                         list_del_init(&spu->list);
518                         spu_free(spu);
519                 }
520                 mutex_unlock(&spu_prio->active_mutex[node]);
521         }
522         kfree(spu_prio);
523         destroy_workqueue(spu_sched_wq);
524 }