blob: ba4b01e01acebb2c5921dc3af83b5429582b4966 [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 -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;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050058
Mark Nuttera68cf982006-10-04 17:26:12 +020059static inline int node_allowed(int node)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050060{
Mark Nuttera68cf982006-10-04 17:26:12 +020061 cpumask_t mask;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050062
Mark Nuttera68cf982006-10-04 17:26:12 +020063 if (!nr_cpus_node(node))
64 return 0;
65 mask = node_to_cpumask(node);
66 if (!cpus_intersects(mask, current->cpus_allowed))
67 return 0;
68 return 1;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050069}
70
Christoph Hellwig202557d2007-02-13 21:36:49 +010071/**
72 * spu_add_to_active_list - add spu to active list
73 * @spu: spu to add to the active list
74 */
75static void spu_add_to_active_list(struct spu *spu)
76{
77 mutex_lock(&spu_prio->active_mutex[spu->node]);
78 list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
79 mutex_unlock(&spu_prio->active_mutex[spu->node]);
80}
81
82/**
83 * spu_remove_from_active_list - remove spu from active list
84 * @spu: spu to remove from the active list
Christoph Hellwig202557d2007-02-13 21:36:49 +010085 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +010086static void spu_remove_from_active_list(struct spu *spu)
Christoph Hellwig202557d2007-02-13 21:36:49 +010087{
88 int node = spu->node;
Christoph Hellwig202557d2007-02-13 21:36:49 +010089
90 mutex_lock(&spu_prio->active_mutex[node]);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +010091 list_del_init(&spu->list);
Christoph Hellwig202557d2007-02-13 21:36:49 +010092 mutex_unlock(&spu_prio->active_mutex[node]);
Christoph Hellwig202557d2007-02-13 21:36:49 +010093}
94
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050095static inline void mm_needs_global_tlbie(struct mm_struct *mm)
96{
Mark Nuttera68cf982006-10-04 17:26:12 +020097 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
98
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050099 /* Global TLBIE broadcast required with SPEs. */
Mark Nuttera68cf982006-10-04 17:26:12 +0200100 __cpus_setall(&mm->cpu_vm_mask, nr);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500101}
102
Arnd Bergmann86767272006-10-04 17:26:21 +0200103static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
104
105static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
106{
107 blocking_notifier_call_chain(&spu_switch_notifier,
108 ctx ? ctx->object_id : 0, spu);
109}
110
111int spu_switch_event_register(struct notifier_block * n)
112{
113 return blocking_notifier_chain_register(&spu_switch_notifier, n);
114}
115
116int spu_switch_event_unregister(struct notifier_block * n)
117{
118 return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
119}
120
Christoph Hellwig202557d2007-02-13 21:36:49 +0100121/**
122 * spu_bind_context - bind spu context to physical spu
123 * @spu: physical spu to bind to
124 * @ctx: context to bind
125 */
126static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500127{
Mark Nuttera68cf982006-10-04 17:26:12 +0200128 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
129 spu->number, spu->node);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500130 spu->ctx = ctx;
131 spu->flags = 0;
132 ctx->spu = spu;
133 ctx->ops = &spu_hw_ops;
134 spu->pid = current->pid;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500135 spu->mm = ctx->owner;
136 mm_needs_global_tlbie(spu->mm);
137 spu->ibox_callback = spufs_ibox_callback;
138 spu->wbox_callback = spufs_wbox_callback;
Arnd Bergmann51104592005-12-05 22:52:25 -0500139 spu->stop_callback = spufs_stop_callback;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100140 spu->mfc_callback = spufs_mfc_callback;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200141 spu->dma_callback = spufs_dma_callback;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500142 mb();
Arnd Bergmann51104592005-12-05 22:52:25 -0500143 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500144 spu_restore(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500145 spu->timestamp = jiffies;
Mark Nuttera68cf982006-10-04 17:26:12 +0200146 spu_cpu_affinity_set(spu, raw_smp_processor_id());
Arnd Bergmann86767272006-10-04 17:26:21 +0200147 spu_switch_notify(spu, ctx);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100148 spu_add_to_active_list(spu);
Christoph Hellwig81998ba2007-02-13 21:36:48 +0100149 ctx->state = SPU_STATE_RUNNABLE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500150}
151
Christoph Hellwig202557d2007-02-13 21:36:49 +0100152/**
153 * spu_unbind_context - unbind spu context from physical spu
154 * @spu: physical spu to unbind from
155 * @ctx: context to unbind
Christoph Hellwig202557d2007-02-13 21:36:49 +0100156 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100157static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500158{
Mark Nuttera68cf982006-10-04 17:26:12 +0200159 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
160 spu->pid, spu->number, spu->node);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100161
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100162 spu_remove_from_active_list(spu);
Arnd Bergmann86767272006-10-04 17:26:21 +0200163 spu_switch_notify(spu, NULL);
Arnd Bergmann51104592005-12-05 22:52:25 -0500164 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500165 spu_save(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500166 spu->timestamp = jiffies;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500167 ctx->state = SPU_STATE_SAVED;
168 spu->ibox_callback = NULL;
169 spu->wbox_callback = NULL;
Arnd Bergmann51104592005-12-05 22:52:25 -0500170 spu->stop_callback = NULL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100171 spu->mfc_callback = NULL;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200172 spu->dma_callback = NULL;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500173 spu->mm = NULL;
174 spu->pid = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500175 ctx->ops = &spu_backing_ops;
176 ctx->spu = NULL;
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500177 spu->flags = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500178 spu->ctx = NULL;
179}
180
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100181/**
182 * spu_add_to_rq - add a context to the runqueue
183 * @ctx: context to add
184 */
185static void spu_add_to_rq(struct spu_context *ctx)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500186{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100187 spin_lock(&spu_prio->runq_lock);
188 list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
189 set_bit(ctx->prio, spu_prio->bitmap);
190 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200191}
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500192
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100193/**
194 * spu_del_from_rq - remove a context from the runqueue
195 * @ctx: context to remove
196 */
197static void spu_del_from_rq(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200198{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100199 spin_lock(&spu_prio->runq_lock);
200 list_del_init(&ctx->rq);
201 if (list_empty(&spu_prio->runq[ctx->prio]))
202 clear_bit(ctx->prio, spu_prio->bitmap);
203 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200204}
205
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100206/**
207 * spu_grab_context - remove one context from the runqueue
208 * @prio: priority of the context to be removed
209 *
210 * This function removes one context from the runqueue for priority @prio.
211 * If there is more than one context with the given priority the first
212 * task on the runqueue will be taken.
213 *
214 * Returns the spu_context it just removed.
215 *
216 * Must be called with spu_prio->runq_lock held.
217 */
218static struct spu_context *spu_grab_context(int prio)
Mark Nuttera68cf982006-10-04 17:26:12 +0200219{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100220 struct list_head *rq = &spu_prio->runq[prio];
221
222 if (list_empty(rq))
223 return NULL;
224 return list_entry(rq->next, struct spu_context, rq);
225}
226
227static void spu_prio_wait(struct spu_context *ctx)
228{
Mark Nuttera68cf982006-10-04 17:26:12 +0200229 DEFINE_WAIT(wait);
230
Christoph Hellwig26bec672007-02-13 21:54:24 +0100231 set_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100232 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
Mark Nuttera68cf982006-10-04 17:26:12 +0200233 if (!signal_pending(current)) {
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100234 mutex_unlock(&ctx->state_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200235 schedule();
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100236 mutex_lock(&ctx->state_mutex);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500237 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100238 __set_current_state(TASK_RUNNING);
239 remove_wait_queue(&ctx->stop_wq, &wait);
Christoph Hellwig26bec672007-02-13 21:54:24 +0100240 clear_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500241}
242
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100243/**
244 * spu_reschedule - try to find a runnable context for a spu
245 * @spu: spu available
246 *
247 * This function is called whenever a spu becomes idle. It looks for the
248 * most suitable runnable spu context and schedules it for execution.
249 */
250static void spu_reschedule(struct spu *spu)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500251{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100252 int best;
253
254 spu_free(spu);
255
256 spin_lock(&spu_prio->runq_lock);
257 best = sched_find_first_bit(spu_prio->bitmap);
Mark Nuttera68cf982006-10-04 17:26:12 +0200258 if (best < MAX_PRIO) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100259 struct spu_context *ctx = spu_grab_context(best);
Christoph Hellwig26bec672007-02-13 21:54:24 +0100260 if (ctx && test_bit(SPU_SCHED_WAKE, &ctx->sched_flags))
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100261 wake_up(&ctx->stop_wq);
Arnd Bergmann51104592005-12-05 22:52:25 -0500262 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100263 spin_unlock(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500264}
265
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100266static struct spu *spu_get_idle(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200267{
268 struct spu *spu = NULL;
269 int node = cpu_to_node(raw_smp_processor_id());
270 int n;
271
272 for (n = 0; n < MAX_NUMNODES; n++, node++) {
273 node = (node < MAX_NUMNODES) ? node : 0;
274 if (!node_allowed(node))
275 continue;
276 spu = spu_alloc_node(node);
277 if (spu)
278 break;
279 }
280 return spu;
281}
282
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100283/**
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100284 * find_victim - find a lower priority context to preempt
285 * @ctx: canidate context for running
286 *
287 * Returns the freed physical spu to run the new context on.
288 */
289static struct spu *find_victim(struct spu_context *ctx)
290{
291 struct spu_context *victim = NULL;
292 struct spu *spu;
293 int node, n;
294
295 /*
296 * Look for a possible preemption candidate on the local node first.
297 * If there is no candidate look at the other nodes. This isn't
298 * exactly fair, but so far the whole spu schedule tries to keep
299 * a strong node affinity. We might want to fine-tune this in
300 * the future.
301 */
302 restart:
303 node = cpu_to_node(raw_smp_processor_id());
304 for (n = 0; n < MAX_NUMNODES; n++, node++) {
305 node = (node < MAX_NUMNODES) ? node : 0;
306 if (!node_allowed(node))
307 continue;
308
309 mutex_lock(&spu_prio->active_mutex[node]);
310 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
311 struct spu_context *tmp = spu->ctx;
312
313 if (tmp->rt_priority < ctx->rt_priority &&
314 (!victim || tmp->rt_priority < victim->rt_priority))
315 victim = spu->ctx;
316 }
317 mutex_unlock(&spu_prio->active_mutex[node]);
318
319 if (victim) {
320 /*
321 * This nests ctx->state_mutex, but we always lock
322 * higher priority contexts before lower priority
323 * ones, so this is safe until we introduce
324 * priority inheritance schemes.
325 */
326 if (!mutex_trylock(&victim->state_mutex)) {
327 victim = NULL;
328 goto restart;
329 }
330
331 spu = victim->spu;
332 if (!spu) {
333 /*
334 * This race can happen because we've dropped
335 * the active list mutex. No a problem, just
336 * restart the search.
337 */
338 mutex_unlock(&victim->state_mutex);
339 victim = NULL;
340 goto restart;
341 }
342 spu_unbind_context(spu, victim);
343 mutex_unlock(&victim->state_mutex);
344 return spu;
345 }
346 }
347
348 return NULL;
349}
350
351/**
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100352 * spu_activate - find a free spu for a context and execute it
353 * @ctx: spu context to schedule
354 * @flags: flags (currently ignored)
355 *
356 * Tries to find a free spu to run @ctx. If no free spu is availble
357 * add the context to the runqueue so it gets woken up once an spu
358 * is available.
359 */
Christoph Hellwig26bec672007-02-13 21:54:24 +0100360int spu_activate(struct spu_context *ctx, unsigned long flags)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500361{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500362
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100363 if (ctx->spu)
364 return 0;
365
366 do {
367 struct spu *spu;
368
369 spu = spu_get_idle(ctx);
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100370 /*
371 * If this is a realtime thread we try to get it running by
372 * preempting a lower priority thread.
373 */
374 if (!spu && ctx->rt_priority)
375 spu = find_victim(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100376 if (spu) {
Christoph Hellwig202557d2007-02-13 21:36:49 +0100377 spu_bind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100378 return 0;
Mark Nuttera68cf982006-10-04 17:26:12 +0200379 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100380
381 spu_add_to_rq(ctx);
Christoph Hellwig26bec672007-02-13 21:54:24 +0100382 if (!(flags & SPU_ACTIVATE_NOWAKE))
383 spu_prio_wait(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100384 spu_del_from_rq(ctx);
385 } while (!signal_pending(current));
386
387 return -ERESTARTSYS;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500388}
389
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100390/**
391 * spu_deactivate - unbind a context from it's physical spu
392 * @ctx: spu context to unbind
393 *
394 * Unbind @ctx from the physical spu it is running on and schedule
395 * the highest priority context to run on the freed physical spu.
396 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500397void spu_deactivate(struct spu_context *ctx)
398{
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100399 struct spu *spu = ctx->spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500400
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100401 if (spu) {
402 spu_unbind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100403 spu_reschedule(spu);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100404 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500405}
406
Christoph Hellwigae7b4c52007-02-13 21:54:26 +0100407/**
408 * spu_yield - yield a physical spu if others are waiting
409 * @ctx: spu context to yield
410 *
411 * Check if there is a higher priority context waiting and if yes
412 * unbind @ctx from the physical spu and schedule the highest
413 * priority context to run on the freed physical spu instead.
414 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500415void spu_yield(struct spu_context *ctx)
416{
417 struct spu *spu;
Arnd Bergmann51104592005-12-05 22:52:25 -0500418 int need_yield = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500419
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100420 if (mutex_trylock(&ctx->state_mutex)) {
Mark Nuttera68cf982006-10-04 17:26:12 +0200421 if ((spu = ctx->spu) != NULL) {
422 int best = sched_find_first_bit(spu_prio->bitmap);
423 if (best < MAX_PRIO) {
424 pr_debug("%s: yielding SPU %d NODE %d\n",
425 __FUNCTION__, spu->number, spu->node);
426 spu_deactivate(ctx);
Mark Nuttera68cf982006-10-04 17:26:12 +0200427 need_yield = 1;
Mark Nuttera68cf982006-10-04 17:26:12 +0200428 }
429 }
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100430 mutex_unlock(&ctx->state_mutex);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500431 }
Arnd Bergmann51104592005-12-05 22:52:25 -0500432 if (unlikely(need_yield))
433 yield();
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500434}
435
436int __init spu_sched_init(void)
437{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500438 int i;
439
Mark Nuttera68cf982006-10-04 17:26:12 +0200440 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
441 if (!spu_prio) {
442 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500443 __FUNCTION__);
444 return 1;
445 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500446 for (i = 0; i < MAX_PRIO; i++) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100447 INIT_LIST_HEAD(&spu_prio->runq[i]);
Mark Nuttera68cf982006-10-04 17:26:12 +0200448 __clear_bit(i, spu_prio->bitmap);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500449 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200450 __set_bit(MAX_PRIO, spu_prio->bitmap);
451 for (i = 0; i < MAX_NUMNODES; i++) {
452 mutex_init(&spu_prio->active_mutex[i]);
453 INIT_LIST_HEAD(&spu_prio->active_list[i]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500454 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100455 spin_lock_init(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500456 return 0;
457}
458
459void __exit spu_sched_exit(void)
460{
Mark Nuttera68cf982006-10-04 17:26:12 +0200461 struct spu *spu, *tmp;
462 int node;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500463
Mark Nuttera68cf982006-10-04 17:26:12 +0200464 for (node = 0; node < MAX_NUMNODES; node++) {
465 mutex_lock(&spu_prio->active_mutex[node]);
466 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
467 list) {
468 list_del_init(&spu->list);
469 spu_free(spu);
470 }
471 mutex_unlock(&spu_prio->active_mutex[node]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500472 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200473 kfree(spu_prio);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500474}