blob: 814f65e025f1c22799ca3c09feae61318c9790a1 [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
Arnd Bergmann7945a4a2005-12-09 19:04:16 +010047#define SPU_MIN_TIMESLICE (100 * HZ / 1000)
Arnd Bergmann2a911f02005-12-05 22:52:26 -050048
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050049#define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1)
50struct spu_prio_array {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050051 unsigned long bitmap[SPU_BITMAP_SIZE];
Christoph Hellwig079cdb62007-02-13 21:54:23 +010052 struct list_head runq[MAX_PRIO];
53 spinlock_t runq_lock;
Mark Nuttera68cf982006-10-04 17:26:12 +020054 struct list_head active_list[MAX_NUMNODES];
55 struct mutex active_mutex[MAX_NUMNODES];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050056};
57
Mark Nuttera68cf982006-10-04 17:26:12 +020058static struct spu_prio_array *spu_prio;
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 Hellwig202557d2007-02-13 21:36:49 +010072/**
73 * spu_add_to_active_list - add spu to active list
74 * @spu: spu to add to the active list
75 */
76static void spu_add_to_active_list(struct spu *spu)
77{
78 mutex_lock(&spu_prio->active_mutex[spu->node]);
79 list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
80 mutex_unlock(&spu_prio->active_mutex[spu->node]);
81}
82
83/**
84 * spu_remove_from_active_list - remove spu from active list
85 * @spu: spu to remove from the active list
Christoph Hellwig202557d2007-02-13 21:36:49 +010086 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +010087static void spu_remove_from_active_list(struct spu *spu)
Christoph Hellwig202557d2007-02-13 21:36:49 +010088{
89 int node = spu->node;
Christoph Hellwig202557d2007-02-13 21:36:49 +010090
91 mutex_lock(&spu_prio->active_mutex[node]);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +010092 list_del_init(&spu->list);
Christoph Hellwig202557d2007-02-13 21:36:49 +010093 mutex_unlock(&spu_prio->active_mutex[node]);
Christoph Hellwig202557d2007-02-13 21:36:49 +010094}
95
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050096static inline void mm_needs_global_tlbie(struct mm_struct *mm)
97{
Mark Nuttera68cf982006-10-04 17:26:12 +020098 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
99
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500100 /* Global TLBIE broadcast required with SPEs. */
Mark Nuttera68cf982006-10-04 17:26:12 +0200101 __cpus_setall(&mm->cpu_vm_mask, nr);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500102}
103
Arnd Bergmann86767272006-10-04 17:26:21 +0200104static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
105
106static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
107{
108 blocking_notifier_call_chain(&spu_switch_notifier,
109 ctx ? ctx->object_id : 0, spu);
110}
111
112int spu_switch_event_register(struct notifier_block * n)
113{
114 return blocking_notifier_chain_register(&spu_switch_notifier, n);
115}
116
117int spu_switch_event_unregister(struct notifier_block * n)
118{
119 return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
120}
121
Christoph Hellwig202557d2007-02-13 21:36:49 +0100122/**
123 * spu_bind_context - bind spu context to physical spu
124 * @spu: physical spu to bind to
125 * @ctx: context to bind
126 */
127static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500128{
Mark Nuttera68cf982006-10-04 17:26:12 +0200129 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
130 spu->number, spu->node);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500131 spu->ctx = ctx;
132 spu->flags = 0;
133 ctx->spu = spu;
134 ctx->ops = &spu_hw_ops;
135 spu->pid = current->pid;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500136 spu->mm = ctx->owner;
137 mm_needs_global_tlbie(spu->mm);
138 spu->ibox_callback = spufs_ibox_callback;
139 spu->wbox_callback = spufs_wbox_callback;
Arnd Bergmann51104592005-12-05 22:52:25 -0500140 spu->stop_callback = spufs_stop_callback;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100141 spu->mfc_callback = spufs_mfc_callback;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200142 spu->dma_callback = spufs_dma_callback;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500143 mb();
Arnd Bergmann51104592005-12-05 22:52:25 -0500144 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500145 spu_restore(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500146 spu->timestamp = jiffies;
Mark Nuttera68cf982006-10-04 17:26:12 +0200147 spu_cpu_affinity_set(spu, raw_smp_processor_id());
Arnd Bergmann86767272006-10-04 17:26:21 +0200148 spu_switch_notify(spu, ctx);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100149 spu_add_to_active_list(spu);
Christoph Hellwig81998ba2007-02-13 21:36:48 +0100150 ctx->state = SPU_STATE_RUNNABLE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500151}
152
Christoph Hellwig202557d2007-02-13 21:36:49 +0100153/**
154 * spu_unbind_context - unbind spu context from physical spu
155 * @spu: physical spu to unbind from
156 * @ctx: context to unbind
Christoph Hellwig202557d2007-02-13 21:36:49 +0100157 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100158static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500159{
Mark Nuttera68cf982006-10-04 17:26:12 +0200160 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
161 spu->pid, spu->number, spu->node);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100162
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100163 spu_remove_from_active_list(spu);
Arnd Bergmann86767272006-10-04 17:26:21 +0200164 spu_switch_notify(spu, NULL);
Arnd Bergmann51104592005-12-05 22:52:25 -0500165 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500166 spu_save(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500167 spu->timestamp = jiffies;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500168 ctx->state = SPU_STATE_SAVED;
169 spu->ibox_callback = NULL;
170 spu->wbox_callback = NULL;
Arnd Bergmann51104592005-12-05 22:52:25 -0500171 spu->stop_callback = NULL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100172 spu->mfc_callback = NULL;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200173 spu->dma_callback = NULL;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500174 spu->mm = NULL;
175 spu->pid = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500176 ctx->ops = &spu_backing_ops;
177 ctx->spu = NULL;
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500178 spu->flags = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500179 spu->ctx = NULL;
180}
181
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100182/**
183 * spu_add_to_rq - add a context to the runqueue
184 * @ctx: context to add
185 */
186static void spu_add_to_rq(struct spu_context *ctx)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500187{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100188 spin_lock(&spu_prio->runq_lock);
189 list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
190 set_bit(ctx->prio, spu_prio->bitmap);
191 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200192}
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500193
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100194/**
195 * spu_del_from_rq - remove a context from the runqueue
196 * @ctx: context to remove
197 */
198static void spu_del_from_rq(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200199{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100200 spin_lock(&spu_prio->runq_lock);
201 list_del_init(&ctx->rq);
202 if (list_empty(&spu_prio->runq[ctx->prio]))
203 clear_bit(ctx->prio, spu_prio->bitmap);
204 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200205}
206
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100207/**
208 * spu_grab_context - remove one context from the runqueue
209 * @prio: priority of the context to be removed
210 *
211 * This function removes one context from the runqueue for priority @prio.
212 * If there is more than one context with the given priority the first
213 * task on the runqueue will be taken.
214 *
215 * Returns the spu_context it just removed.
216 *
217 * Must be called with spu_prio->runq_lock held.
218 */
219static struct spu_context *spu_grab_context(int prio)
Mark Nuttera68cf982006-10-04 17:26:12 +0200220{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100221 struct list_head *rq = &spu_prio->runq[prio];
222
223 if (list_empty(rq))
224 return NULL;
225 return list_entry(rq->next, struct spu_context, rq);
226}
227
228static void spu_prio_wait(struct spu_context *ctx)
229{
Mark Nuttera68cf982006-10-04 17:26:12 +0200230 DEFINE_WAIT(wait);
231
Christoph Hellwig26bec672007-02-13 21:54:24 +0100232 set_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100233 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
Mark Nuttera68cf982006-10-04 17:26:12 +0200234 if (!signal_pending(current)) {
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100235 mutex_unlock(&ctx->state_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200236 schedule();
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100237 mutex_lock(&ctx->state_mutex);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500238 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100239 __set_current_state(TASK_RUNNING);
240 remove_wait_queue(&ctx->stop_wq, &wait);
Christoph Hellwig26bec672007-02-13 21:54:24 +0100241 clear_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500242}
243
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100244/**
245 * spu_reschedule - try to find a runnable context for a spu
246 * @spu: spu available
247 *
248 * This function is called whenever a spu becomes idle. It looks for the
249 * most suitable runnable spu context and schedules it for execution.
250 */
251static void spu_reschedule(struct spu *spu)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500252{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100253 int best;
254
255 spu_free(spu);
256
257 spin_lock(&spu_prio->runq_lock);
258 best = sched_find_first_bit(spu_prio->bitmap);
Mark Nuttera68cf982006-10-04 17:26:12 +0200259 if (best < MAX_PRIO) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100260 struct spu_context *ctx = spu_grab_context(best);
Christoph Hellwig26bec672007-02-13 21:54:24 +0100261 if (ctx && test_bit(SPU_SCHED_WAKE, &ctx->sched_flags))
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100262 wake_up(&ctx->stop_wq);
Arnd Bergmann51104592005-12-05 22:52:25 -0500263 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100264 spin_unlock(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500265}
266
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100267static struct spu *spu_get_idle(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200268{
269 struct spu *spu = NULL;
270 int node = cpu_to_node(raw_smp_processor_id());
271 int n;
272
273 for (n = 0; n < MAX_NUMNODES; n++, node++) {
274 node = (node < MAX_NUMNODES) ? node : 0;
275 if (!node_allowed(node))
276 continue;
277 spu = spu_alloc_node(node);
278 if (spu)
279 break;
280 }
281 return spu;
282}
283
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100284/**
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100285 * find_victim - find a lower priority context to preempt
286 * @ctx: canidate context for running
287 *
288 * Returns the freed physical spu to run the new context on.
289 */
290static struct spu *find_victim(struct spu_context *ctx)
291{
292 struct spu_context *victim = NULL;
293 struct spu *spu;
294 int node, n;
295
296 /*
297 * Look for a possible preemption candidate on the local node first.
298 * If there is no candidate look at the other nodes. This isn't
299 * exactly fair, but so far the whole spu schedule tries to keep
300 * a strong node affinity. We might want to fine-tune this in
301 * the future.
302 */
303 restart:
304 node = cpu_to_node(raw_smp_processor_id());
305 for (n = 0; n < MAX_NUMNODES; n++, node++) {
306 node = (node < MAX_NUMNODES) ? node : 0;
307 if (!node_allowed(node))
308 continue;
309
310 mutex_lock(&spu_prio->active_mutex[node]);
311 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
312 struct spu_context *tmp = spu->ctx;
313
314 if (tmp->rt_priority < ctx->rt_priority &&
315 (!victim || tmp->rt_priority < victim->rt_priority))
316 victim = spu->ctx;
317 }
318 mutex_unlock(&spu_prio->active_mutex[node]);
319
320 if (victim) {
321 /*
322 * This nests ctx->state_mutex, but we always lock
323 * higher priority contexts before lower priority
324 * ones, so this is safe until we introduce
325 * priority inheritance schemes.
326 */
327 if (!mutex_trylock(&victim->state_mutex)) {
328 victim = NULL;
329 goto restart;
330 }
331
332 spu = victim->spu;
333 if (!spu) {
334 /*
335 * This race can happen because we've dropped
336 * the active list mutex. No a problem, just
337 * restart the search.
338 */
339 mutex_unlock(&victim->state_mutex);
340 victim = NULL;
341 goto restart;
342 }
343 spu_unbind_context(spu, victim);
344 mutex_unlock(&victim->state_mutex);
345 return spu;
346 }
347 }
348
349 return NULL;
350}
351
352/**
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100353 * spu_activate - find a free spu for a context and execute it
354 * @ctx: spu context to schedule
355 * @flags: flags (currently ignored)
356 *
357 * Tries to find a free spu to run @ctx. If no free spu is availble
358 * add the context to the runqueue so it gets woken up once an spu
359 * is available.
360 */
Christoph Hellwig26bec672007-02-13 21:54:24 +0100361int spu_activate(struct spu_context *ctx, unsigned long flags)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500362{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500363
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100364 if (ctx->spu)
365 return 0;
366
367 do {
368 struct spu *spu;
369
370 spu = spu_get_idle(ctx);
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100371 /*
372 * If this is a realtime thread we try to get it running by
373 * preempting a lower priority thread.
374 */
375 if (!spu && ctx->rt_priority)
376 spu = find_victim(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100377 if (spu) {
Christoph Hellwig202557d2007-02-13 21:36:49 +0100378 spu_bind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100379 return 0;
Mark Nuttera68cf982006-10-04 17:26:12 +0200380 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100381
382 spu_add_to_rq(ctx);
Christoph Hellwig26bec672007-02-13 21:54:24 +0100383 if (!(flags & SPU_ACTIVATE_NOWAKE))
384 spu_prio_wait(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100385 spu_del_from_rq(ctx);
386 } while (!signal_pending(current));
387
388 return -ERESTARTSYS;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500389}
390
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100391/**
392 * spu_deactivate - unbind a context from it's physical spu
393 * @ctx: spu context to unbind
394 *
395 * Unbind @ctx from the physical spu it is running on and schedule
396 * the highest priority context to run on the freed physical spu.
397 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500398void spu_deactivate(struct spu_context *ctx)
399{
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100400 struct spu *spu = ctx->spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500401
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100402 if (spu) {
403 spu_unbind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100404 spu_reschedule(spu);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100405 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500406}
407
Christoph Hellwigae7b4c52007-02-13 21:54:26 +0100408/**
409 * spu_yield - yield a physical spu if others are waiting
410 * @ctx: spu context to yield
411 *
412 * Check if there is a higher priority context waiting and if yes
413 * unbind @ctx from the physical spu and schedule the highest
414 * priority context to run on the freed physical spu instead.
415 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500416void spu_yield(struct spu_context *ctx)
417{
418 struct spu *spu;
Arnd Bergmann51104592005-12-05 22:52:25 -0500419 int need_yield = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500420
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100421 if (mutex_trylock(&ctx->state_mutex)) {
Mark Nuttera68cf982006-10-04 17:26:12 +0200422 if ((spu = ctx->spu) != NULL) {
423 int best = sched_find_first_bit(spu_prio->bitmap);
424 if (best < MAX_PRIO) {
425 pr_debug("%s: yielding SPU %d NODE %d\n",
426 __FUNCTION__, spu->number, spu->node);
427 spu_deactivate(ctx);
Mark Nuttera68cf982006-10-04 17:26:12 +0200428 need_yield = 1;
Mark Nuttera68cf982006-10-04 17:26:12 +0200429 }
430 }
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100431 mutex_unlock(&ctx->state_mutex);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500432 }
Arnd Bergmann51104592005-12-05 22:52:25 -0500433 if (unlikely(need_yield))
434 yield();
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500435}
436
437int __init spu_sched_init(void)
438{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500439 int i;
440
Mark Nuttera68cf982006-10-04 17:26:12 +0200441 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
442 if (!spu_prio) {
443 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500444 __FUNCTION__);
445 return 1;
446 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500447 for (i = 0; i < MAX_PRIO; i++) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100448 INIT_LIST_HEAD(&spu_prio->runq[i]);
Mark Nuttera68cf982006-10-04 17:26:12 +0200449 __clear_bit(i, spu_prio->bitmap);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500450 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200451 __set_bit(MAX_PRIO, spu_prio->bitmap);
452 for (i = 0; i < MAX_NUMNODES; i++) {
453 mutex_init(&spu_prio->active_mutex[i]);
454 INIT_LIST_HEAD(&spu_prio->active_list[i]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500455 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100456 spin_lock_init(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500457 return 0;
458}
459
460void __exit spu_sched_exit(void)
461{
Mark Nuttera68cf982006-10-04 17:26:12 +0200462 struct spu *spu, *tmp;
463 int node;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500464
Mark Nuttera68cf982006-10-04 17:26:12 +0200465 for (node = 0; node < MAX_NUMNODES; node++) {
466 mutex_lock(&spu_prio->active_mutex[node]);
467 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
468 list) {
469 list_del_init(&spu->list);
470 spu_free(spu);
471 }
472 mutex_unlock(&spu_prio->active_mutex[node]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500473 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200474 kfree(spu_prio);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500475}