blob: 39823cec08444c6244ea8ca149fb4af39630be99 [file] [log] [blame]
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001/* sched.c - SPU scheduler.
2 *
3 * Copyright (C) IBM 2005
4 * Author: Mark Nutter <mnutter@us.ibm.com>
5 *
Mark Nuttera68cf982006-10-04 17:26:12 +02006 * 2006-03-31 NUMA domains added.
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05007 *
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
Arnd Bergmann3b3d22c2005-12-05 22:52:24 -050023#undef DEBUG
24
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050025#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>
Mark Nuttera68cf982006-10-04 17:26:12 +020036#include <linux/numa.h>
37#include <linux/mutex.h>
Arnd Bergmann86767272006-10-04 17:26:21 +020038#include <linux/notifier.h>
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050039
40#include <asm/io.h>
41#include <asm/mmu_context.h>
42#include <asm/spu.h>
43#include <asm/spu_csa.h>
Geoff Levanda91942a2006-06-19 20:33:30 +020044#include <asm/spu_priv1.h>
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050045#include "spufs.h"
46
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010047#define SPU_TIMESLICE (HZ)
Arnd Bergmann2a911f02005-12-05 22:52:26 -050048
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050049struct spu_prio_array {
Christoph Hellwig72cb3602007-02-13 21:54:28 +010050 DECLARE_BITMAP(bitmap, MAX_PRIO);
Christoph Hellwig079cdb62007-02-13 21:54:23 +010051 struct list_head runq[MAX_PRIO];
52 spinlock_t runq_lock;
Mark Nuttera68cf982006-10-04 17:26:12 +020053 struct list_head active_list[MAX_NUMNODES];
54 struct mutex active_mutex[MAX_NUMNODES];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050055};
56
Mark Nuttera68cf982006-10-04 17:26:12 +020057static struct spu_prio_array *spu_prio;
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010058static struct workqueue_struct *spu_sched_wq;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050059
Mark Nuttera68cf982006-10-04 17:26:12 +020060static inline int node_allowed(int node)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050061{
Mark Nuttera68cf982006-10-04 17:26:12 +020062 cpumask_t mask;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050063
Mark Nuttera68cf982006-10-04 17:26:12 +020064 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;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050070}
71
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010072void spu_start_tick(struct spu_context *ctx)
73{
74 if (ctx->policy == SCHED_RR)
75 queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE);
76}
77
78void spu_stop_tick(struct spu_context *ctx)
79{
80 if (ctx->policy == SCHED_RR)
81 cancel_delayed_work(&ctx->sched_work);
82}
83
84void spu_sched_tick(struct work_struct *work)
85{
86 struct spu_context *ctx =
87 container_of(work, struct spu_context, sched_work.work);
88 struct spu *spu;
89 int rearm = 1;
90
91 mutex_lock(&ctx->state_mutex);
92 spu = ctx->spu;
93 if (spu) {
94 int best = sched_find_first_bit(spu_prio->bitmap);
95 if (best <= ctx->prio) {
96 spu_deactivate(ctx);
97 rearm = 0;
98 }
99 }
100 mutex_unlock(&ctx->state_mutex);
101
102 if (rearm)
103 spu_start_tick(ctx);
104}
105
Christoph Hellwig202557d2007-02-13 21:36:49 +0100106/**
107 * spu_add_to_active_list - add spu to active list
108 * @spu: spu to add to the active list
109 */
110static void spu_add_to_active_list(struct spu *spu)
111{
112 mutex_lock(&spu_prio->active_mutex[spu->node]);
113 list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
114 mutex_unlock(&spu_prio->active_mutex[spu->node]);
115}
116
117/**
118 * spu_remove_from_active_list - remove spu from active list
119 * @spu: spu to remove from the active list
Christoph Hellwig202557d2007-02-13 21:36:49 +0100120 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100121static void spu_remove_from_active_list(struct spu *spu)
Christoph Hellwig202557d2007-02-13 21:36:49 +0100122{
123 int node = spu->node;
Christoph Hellwig202557d2007-02-13 21:36:49 +0100124
125 mutex_lock(&spu_prio->active_mutex[node]);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100126 list_del_init(&spu->list);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100127 mutex_unlock(&spu_prio->active_mutex[node]);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100128}
129
Arnd Bergmann86767272006-10-04 17:26:21 +0200130static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
131
132static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
133{
134 blocking_notifier_call_chain(&spu_switch_notifier,
135 ctx ? ctx->object_id : 0, spu);
136}
137
138int spu_switch_event_register(struct notifier_block * n)
139{
140 return blocking_notifier_chain_register(&spu_switch_notifier, n);
141}
142
143int spu_switch_event_unregister(struct notifier_block * n)
144{
145 return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
146}
147
Christoph Hellwig202557d2007-02-13 21:36:49 +0100148/**
149 * spu_bind_context - bind spu context to physical spu
150 * @spu: physical spu to bind to
151 * @ctx: context to bind
152 */
153static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500154{
Mark Nuttera68cf982006-10-04 17:26:12 +0200155 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
156 spu->number, spu->node);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500157 spu->ctx = ctx;
158 spu->flags = 0;
159 ctx->spu = spu;
160 ctx->ops = &spu_hw_ops;
161 spu->pid = current->pid;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100162 spu_associate_mm(spu, ctx->owner);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500163 spu->ibox_callback = spufs_ibox_callback;
164 spu->wbox_callback = spufs_wbox_callback;
Arnd Bergmann51104592005-12-05 22:52:25 -0500165 spu->stop_callback = spufs_stop_callback;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100166 spu->mfc_callback = spufs_mfc_callback;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200167 spu->dma_callback = spufs_dma_callback;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500168 mb();
Arnd Bergmann51104592005-12-05 22:52:25 -0500169 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500170 spu_restore(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500171 spu->timestamp = jiffies;
Mark Nuttera68cf982006-10-04 17:26:12 +0200172 spu_cpu_affinity_set(spu, raw_smp_processor_id());
Arnd Bergmann86767272006-10-04 17:26:21 +0200173 spu_switch_notify(spu, ctx);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100174 spu_add_to_active_list(spu);
Christoph Hellwig81998ba2007-02-13 21:36:48 +0100175 ctx->state = SPU_STATE_RUNNABLE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500176}
177
Christoph Hellwig202557d2007-02-13 21:36:49 +0100178/**
179 * spu_unbind_context - unbind spu context from physical spu
180 * @spu: physical spu to unbind from
181 * @ctx: context to unbind
Christoph Hellwig202557d2007-02-13 21:36:49 +0100182 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100183static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500184{
Mark Nuttera68cf982006-10-04 17:26:12 +0200185 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
186 spu->pid, spu->number, spu->node);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100187
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100188 spu_remove_from_active_list(spu);
Arnd Bergmann86767272006-10-04 17:26:21 +0200189 spu_switch_notify(spu, NULL);
Arnd Bergmann51104592005-12-05 22:52:25 -0500190 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500191 spu_save(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500192 spu->timestamp = jiffies;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500193 ctx->state = SPU_STATE_SAVED;
194 spu->ibox_callback = NULL;
195 spu->wbox_callback = NULL;
Arnd Bergmann51104592005-12-05 22:52:25 -0500196 spu->stop_callback = NULL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100197 spu->mfc_callback = NULL;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200198 spu->dma_callback = NULL;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100199 spu_associate_mm(spu, NULL);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500200 spu->pid = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500201 ctx->ops = &spu_backing_ops;
202 ctx->spu = NULL;
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500203 spu->flags = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500204 spu->ctx = NULL;
205}
206
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100207/**
208 * spu_add_to_rq - add a context to the runqueue
209 * @ctx: context to add
210 */
211static void spu_add_to_rq(struct spu_context *ctx)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500212{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100213 spin_lock(&spu_prio->runq_lock);
214 list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
215 set_bit(ctx->prio, spu_prio->bitmap);
216 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200217}
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500218
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100219/**
220 * spu_del_from_rq - remove a context from the runqueue
221 * @ctx: context to remove
222 */
223static void spu_del_from_rq(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200224{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100225 spin_lock(&spu_prio->runq_lock);
226 list_del_init(&ctx->rq);
227 if (list_empty(&spu_prio->runq[ctx->prio]))
228 clear_bit(ctx->prio, spu_prio->bitmap);
229 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200230}
231
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100232/**
233 * spu_grab_context - remove one context from the runqueue
234 * @prio: priority of the context to be removed
235 *
236 * This function removes one context from the runqueue for priority @prio.
237 * If there is more than one context with the given priority the first
238 * task on the runqueue will be taken.
239 *
240 * Returns the spu_context it just removed.
241 *
242 * Must be called with spu_prio->runq_lock held.
243 */
244static struct spu_context *spu_grab_context(int prio)
Mark Nuttera68cf982006-10-04 17:26:12 +0200245{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100246 struct list_head *rq = &spu_prio->runq[prio];
247
248 if (list_empty(rq))
249 return NULL;
250 return list_entry(rq->next, struct spu_context, rq);
251}
252
253static void spu_prio_wait(struct spu_context *ctx)
254{
Mark Nuttera68cf982006-10-04 17:26:12 +0200255 DEFINE_WAIT(wait);
256
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100257 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
Mark Nuttera68cf982006-10-04 17:26:12 +0200258 if (!signal_pending(current)) {
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100259 mutex_unlock(&ctx->state_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200260 schedule();
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100261 mutex_lock(&ctx->state_mutex);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500262 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100263 __set_current_state(TASK_RUNNING);
264 remove_wait_queue(&ctx->stop_wq, &wait);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500265}
266
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100267/**
268 * spu_reschedule - try to find a runnable context for a spu
269 * @spu: spu available
270 *
271 * This function is called whenever a spu becomes idle. It looks for the
272 * most suitable runnable spu context and schedules it for execution.
273 */
274static void spu_reschedule(struct spu *spu)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500275{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100276 int best;
277
278 spu_free(spu);
279
280 spin_lock(&spu_prio->runq_lock);
281 best = sched_find_first_bit(spu_prio->bitmap);
Mark Nuttera68cf982006-10-04 17:26:12 +0200282 if (best < MAX_PRIO) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100283 struct spu_context *ctx = spu_grab_context(best);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100284 if (ctx)
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100285 wake_up(&ctx->stop_wq);
Arnd Bergmann51104592005-12-05 22:52:25 -0500286 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100287 spin_unlock(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500288}
289
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100290static struct spu *spu_get_idle(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200291{
292 struct spu *spu = NULL;
293 int node = cpu_to_node(raw_smp_processor_id());
294 int n;
295
296 for (n = 0; n < MAX_NUMNODES; n++, node++) {
297 node = (node < MAX_NUMNODES) ? node : 0;
298 if (!node_allowed(node))
299 continue;
300 spu = spu_alloc_node(node);
301 if (spu)
302 break;
303 }
304 return spu;
305}
306
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100307/**
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100308 * find_victim - find a lower priority context to preempt
309 * @ctx: canidate context for running
310 *
311 * Returns the freed physical spu to run the new context on.
312 */
313static struct spu *find_victim(struct spu_context *ctx)
314{
315 struct spu_context *victim = NULL;
316 struct spu *spu;
317 int node, n;
318
319 /*
320 * Look for a possible preemption candidate on the local node first.
321 * If there is no candidate look at the other nodes. This isn't
322 * exactly fair, but so far the whole spu schedule tries to keep
323 * a strong node affinity. We might want to fine-tune this in
324 * the future.
325 */
326 restart:
327 node = cpu_to_node(raw_smp_processor_id());
328 for (n = 0; n < MAX_NUMNODES; n++, node++) {
329 node = (node < MAX_NUMNODES) ? node : 0;
330 if (!node_allowed(node))
331 continue;
332
333 mutex_lock(&spu_prio->active_mutex[node]);
334 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
335 struct spu_context *tmp = spu->ctx;
336
337 if (tmp->rt_priority < ctx->rt_priority &&
338 (!victim || tmp->rt_priority < victim->rt_priority))
339 victim = spu->ctx;
340 }
341 mutex_unlock(&spu_prio->active_mutex[node]);
342
343 if (victim) {
344 /*
345 * This nests ctx->state_mutex, but we always lock
346 * higher priority contexts before lower priority
347 * ones, so this is safe until we introduce
348 * priority inheritance schemes.
349 */
350 if (!mutex_trylock(&victim->state_mutex)) {
351 victim = NULL;
352 goto restart;
353 }
354
355 spu = victim->spu;
356 if (!spu) {
357 /*
358 * This race can happen because we've dropped
359 * the active list mutex. No a problem, just
360 * restart the search.
361 */
362 mutex_unlock(&victim->state_mutex);
363 victim = NULL;
364 goto restart;
365 }
366 spu_unbind_context(spu, victim);
367 mutex_unlock(&victim->state_mutex);
368 return spu;
369 }
370 }
371
372 return NULL;
373}
374
375/**
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100376 * spu_activate - find a free spu for a context and execute it
377 * @ctx: spu context to schedule
378 * @flags: flags (currently ignored)
379 *
380 * Tries to find a free spu to run @ctx. If no free spu is availble
381 * add the context to the runqueue so it gets woken up once an spu
382 * is available.
383 */
Christoph Hellwig26bec672007-02-13 21:54:24 +0100384int spu_activate(struct spu_context *ctx, unsigned long flags)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500385{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500386
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100387 if (ctx->spu)
388 return 0;
389
390 do {
391 struct spu *spu;
392
393 spu = spu_get_idle(ctx);
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100394 /*
395 * If this is a realtime thread we try to get it running by
396 * preempting a lower priority thread.
397 */
398 if (!spu && ctx->rt_priority)
399 spu = find_victim(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100400 if (spu) {
Christoph Hellwig202557d2007-02-13 21:36:49 +0100401 spu_bind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100402 return 0;
Mark Nuttera68cf982006-10-04 17:26:12 +0200403 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100404
405 spu_add_to_rq(ctx);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100406 spu_prio_wait(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100407 spu_del_from_rq(ctx);
408 } while (!signal_pending(current));
409
410 return -ERESTARTSYS;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500411}
412
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100413/**
414 * spu_deactivate - unbind a context from it's physical spu
415 * @ctx: spu context to unbind
416 *
417 * Unbind @ctx from the physical spu it is running on and schedule
418 * the highest priority context to run on the freed physical spu.
419 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500420void spu_deactivate(struct spu_context *ctx)
421{
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100422 struct spu *spu = ctx->spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500423
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100424 if (spu) {
425 spu_unbind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100426 spu_reschedule(spu);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100427 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500428}
429
Christoph Hellwigae7b4c52007-02-13 21:54:26 +0100430/**
431 * spu_yield - yield a physical spu if others are waiting
432 * @ctx: spu context to yield
433 *
434 * Check if there is a higher priority context waiting and if yes
435 * unbind @ctx from the physical spu and schedule the highest
436 * priority context to run on the freed physical spu instead.
437 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500438void spu_yield(struct spu_context *ctx)
439{
440 struct spu *spu;
Arnd Bergmann51104592005-12-05 22:52:25 -0500441 int need_yield = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500442
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100443 if (mutex_trylock(&ctx->state_mutex)) {
Mark Nuttera68cf982006-10-04 17:26:12 +0200444 if ((spu = ctx->spu) != NULL) {
445 int best = sched_find_first_bit(spu_prio->bitmap);
446 if (best < MAX_PRIO) {
447 pr_debug("%s: yielding SPU %d NODE %d\n",
448 __FUNCTION__, spu->number, spu->node);
449 spu_deactivate(ctx);
Mark Nuttera68cf982006-10-04 17:26:12 +0200450 need_yield = 1;
Mark Nuttera68cf982006-10-04 17:26:12 +0200451 }
452 }
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100453 mutex_unlock(&ctx->state_mutex);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500454 }
Arnd Bergmann51104592005-12-05 22:52:25 -0500455 if (unlikely(need_yield))
456 yield();
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500457}
458
459int __init spu_sched_init(void)
460{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500461 int i;
462
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100463 spu_sched_wq = create_singlethread_workqueue("spusched");
464 if (!spu_sched_wq)
465 return 1;
466
Mark Nuttera68cf982006-10-04 17:26:12 +0200467 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
468 if (!spu_prio) {
469 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500470 __FUNCTION__);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100471 destroy_workqueue(spu_sched_wq);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500472 return 1;
473 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500474 for (i = 0; i < MAX_PRIO; i++) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100475 INIT_LIST_HEAD(&spu_prio->runq[i]);
Mark Nuttera68cf982006-10-04 17:26:12 +0200476 __clear_bit(i, spu_prio->bitmap);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500477 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200478 __set_bit(MAX_PRIO, spu_prio->bitmap);
479 for (i = 0; i < MAX_NUMNODES; i++) {
480 mutex_init(&spu_prio->active_mutex[i]);
481 INIT_LIST_HEAD(&spu_prio->active_list[i]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500482 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100483 spin_lock_init(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500484 return 0;
485}
486
487void __exit spu_sched_exit(void)
488{
Mark Nuttera68cf982006-10-04 17:26:12 +0200489 struct spu *spu, *tmp;
490 int node;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500491
Mark Nuttera68cf982006-10-04 17:26:12 +0200492 for (node = 0; node < MAX_NUMNODES; node++) {
493 mutex_lock(&spu_prio->active_mutex[node]);
494 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
495 list) {
496 list_del_init(&spu->list);
497 spu_free(spu);
498 }
499 mutex_unlock(&spu_prio->active_mutex[node]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500500 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200501 kfree(spu_prio);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100502 destroy_workqueue(spu_sched_wq);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500503}