blob: 7dbf57c302828ebf85518820ac8cd5a1fc785a7d [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 Bergmann8b3d6662005-11-15 15:53:52 -0500130static inline void mm_needs_global_tlbie(struct mm_struct *mm)
131{
Mark Nuttera68cf982006-10-04 17:26:12 +0200132 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
133
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500134 /* Global TLBIE broadcast required with SPEs. */
Mark Nuttera68cf982006-10-04 17:26:12 +0200135 __cpus_setall(&mm->cpu_vm_mask, nr);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500136}
137
Arnd Bergmann86767272006-10-04 17:26:21 +0200138static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
139
140static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
141{
142 blocking_notifier_call_chain(&spu_switch_notifier,
143 ctx ? ctx->object_id : 0, spu);
144}
145
146int spu_switch_event_register(struct notifier_block * n)
147{
148 return blocking_notifier_chain_register(&spu_switch_notifier, n);
149}
150
151int spu_switch_event_unregister(struct notifier_block * n)
152{
153 return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
154}
155
Christoph Hellwig202557d2007-02-13 21:36:49 +0100156/**
157 * spu_bind_context - bind spu context to physical spu
158 * @spu: physical spu to bind to
159 * @ctx: context to bind
160 */
161static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500162{
Mark Nuttera68cf982006-10-04 17:26:12 +0200163 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
164 spu->number, spu->node);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500165 spu->ctx = ctx;
166 spu->flags = 0;
167 ctx->spu = spu;
168 ctx->ops = &spu_hw_ops;
169 spu->pid = current->pid;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500170 spu->mm = ctx->owner;
171 mm_needs_global_tlbie(spu->mm);
172 spu->ibox_callback = spufs_ibox_callback;
173 spu->wbox_callback = spufs_wbox_callback;
Arnd Bergmann51104592005-12-05 22:52:25 -0500174 spu->stop_callback = spufs_stop_callback;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100175 spu->mfc_callback = spufs_mfc_callback;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200176 spu->dma_callback = spufs_dma_callback;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500177 mb();
Arnd Bergmann51104592005-12-05 22:52:25 -0500178 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500179 spu_restore(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500180 spu->timestamp = jiffies;
Mark Nuttera68cf982006-10-04 17:26:12 +0200181 spu_cpu_affinity_set(spu, raw_smp_processor_id());
Arnd Bergmann86767272006-10-04 17:26:21 +0200182 spu_switch_notify(spu, ctx);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100183 spu_add_to_active_list(spu);
Christoph Hellwig81998ba2007-02-13 21:36:48 +0100184 ctx->state = SPU_STATE_RUNNABLE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500185}
186
Christoph Hellwig202557d2007-02-13 21:36:49 +0100187/**
188 * spu_unbind_context - unbind spu context from physical spu
189 * @spu: physical spu to unbind from
190 * @ctx: context to unbind
Christoph Hellwig202557d2007-02-13 21:36:49 +0100191 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100192static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500193{
Mark Nuttera68cf982006-10-04 17:26:12 +0200194 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
195 spu->pid, spu->number, spu->node);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100196
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100197 spu_remove_from_active_list(spu);
Arnd Bergmann86767272006-10-04 17:26:21 +0200198 spu_switch_notify(spu, NULL);
Arnd Bergmann51104592005-12-05 22:52:25 -0500199 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500200 spu_save(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500201 spu->timestamp = jiffies;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500202 ctx->state = SPU_STATE_SAVED;
203 spu->ibox_callback = NULL;
204 spu->wbox_callback = NULL;
Arnd Bergmann51104592005-12-05 22:52:25 -0500205 spu->stop_callback = NULL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100206 spu->mfc_callback = NULL;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200207 spu->dma_callback = NULL;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500208 spu->mm = NULL;
209 spu->pid = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500210 ctx->ops = &spu_backing_ops;
211 ctx->spu = NULL;
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500212 spu->flags = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500213 spu->ctx = NULL;
214}
215
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100216/**
217 * spu_add_to_rq - add a context to the runqueue
218 * @ctx: context to add
219 */
220static void spu_add_to_rq(struct spu_context *ctx)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500221{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100222 spin_lock(&spu_prio->runq_lock);
223 list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
224 set_bit(ctx->prio, spu_prio->bitmap);
225 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200226}
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500227
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100228/**
229 * spu_del_from_rq - remove a context from the runqueue
230 * @ctx: context to remove
231 */
232static void spu_del_from_rq(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200233{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100234 spin_lock(&spu_prio->runq_lock);
235 list_del_init(&ctx->rq);
236 if (list_empty(&spu_prio->runq[ctx->prio]))
237 clear_bit(ctx->prio, spu_prio->bitmap);
238 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200239}
240
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100241/**
242 * spu_grab_context - remove one context from the runqueue
243 * @prio: priority of the context to be removed
244 *
245 * This function removes one context from the runqueue for priority @prio.
246 * If there is more than one context with the given priority the first
247 * task on the runqueue will be taken.
248 *
249 * Returns the spu_context it just removed.
250 *
251 * Must be called with spu_prio->runq_lock held.
252 */
253static struct spu_context *spu_grab_context(int prio)
Mark Nuttera68cf982006-10-04 17:26:12 +0200254{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100255 struct list_head *rq = &spu_prio->runq[prio];
256
257 if (list_empty(rq))
258 return NULL;
259 return list_entry(rq->next, struct spu_context, rq);
260}
261
262static void spu_prio_wait(struct spu_context *ctx)
263{
Mark Nuttera68cf982006-10-04 17:26:12 +0200264 DEFINE_WAIT(wait);
265
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100266 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
Mark Nuttera68cf982006-10-04 17:26:12 +0200267 if (!signal_pending(current)) {
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100268 mutex_unlock(&ctx->state_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200269 schedule();
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100270 mutex_lock(&ctx->state_mutex);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500271 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100272 __set_current_state(TASK_RUNNING);
273 remove_wait_queue(&ctx->stop_wq, &wait);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500274}
275
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100276/**
277 * spu_reschedule - try to find a runnable context for a spu
278 * @spu: spu available
279 *
280 * This function is called whenever a spu becomes idle. It looks for the
281 * most suitable runnable spu context and schedules it for execution.
282 */
283static void spu_reschedule(struct spu *spu)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500284{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100285 int best;
286
287 spu_free(spu);
288
289 spin_lock(&spu_prio->runq_lock);
290 best = sched_find_first_bit(spu_prio->bitmap);
Mark Nuttera68cf982006-10-04 17:26:12 +0200291 if (best < MAX_PRIO) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100292 struct spu_context *ctx = spu_grab_context(best);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100293 if (ctx)
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100294 wake_up(&ctx->stop_wq);
Arnd Bergmann51104592005-12-05 22:52:25 -0500295 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100296 spin_unlock(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500297}
298
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100299static struct spu *spu_get_idle(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200300{
301 struct spu *spu = NULL;
302 int node = cpu_to_node(raw_smp_processor_id());
303 int n;
304
305 for (n = 0; n < MAX_NUMNODES; n++, node++) {
306 node = (node < MAX_NUMNODES) ? node : 0;
307 if (!node_allowed(node))
308 continue;
309 spu = spu_alloc_node(node);
310 if (spu)
311 break;
312 }
313 return spu;
314}
315
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100316/**
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100317 * find_victim - find a lower priority context to preempt
318 * @ctx: canidate context for running
319 *
320 * Returns the freed physical spu to run the new context on.
321 */
322static struct spu *find_victim(struct spu_context *ctx)
323{
324 struct spu_context *victim = NULL;
325 struct spu *spu;
326 int node, n;
327
328 /*
329 * Look for a possible preemption candidate on the local node first.
330 * If there is no candidate look at the other nodes. This isn't
331 * exactly fair, but so far the whole spu schedule tries to keep
332 * a strong node affinity. We might want to fine-tune this in
333 * the future.
334 */
335 restart:
336 node = cpu_to_node(raw_smp_processor_id());
337 for (n = 0; n < MAX_NUMNODES; n++, node++) {
338 node = (node < MAX_NUMNODES) ? node : 0;
339 if (!node_allowed(node))
340 continue;
341
342 mutex_lock(&spu_prio->active_mutex[node]);
343 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
344 struct spu_context *tmp = spu->ctx;
345
346 if (tmp->rt_priority < ctx->rt_priority &&
347 (!victim || tmp->rt_priority < victim->rt_priority))
348 victim = spu->ctx;
349 }
350 mutex_unlock(&spu_prio->active_mutex[node]);
351
352 if (victim) {
353 /*
354 * This nests ctx->state_mutex, but we always lock
355 * higher priority contexts before lower priority
356 * ones, so this is safe until we introduce
357 * priority inheritance schemes.
358 */
359 if (!mutex_trylock(&victim->state_mutex)) {
360 victim = NULL;
361 goto restart;
362 }
363
364 spu = victim->spu;
365 if (!spu) {
366 /*
367 * This race can happen because we've dropped
368 * the active list mutex. No a problem, just
369 * restart the search.
370 */
371 mutex_unlock(&victim->state_mutex);
372 victim = NULL;
373 goto restart;
374 }
375 spu_unbind_context(spu, victim);
376 mutex_unlock(&victim->state_mutex);
377 return spu;
378 }
379 }
380
381 return NULL;
382}
383
384/**
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100385 * spu_activate - find a free spu for a context and execute it
386 * @ctx: spu context to schedule
387 * @flags: flags (currently ignored)
388 *
389 * Tries to find a free spu to run @ctx. If no free spu is availble
390 * add the context to the runqueue so it gets woken up once an spu
391 * is available.
392 */
Christoph Hellwig26bec672007-02-13 21:54:24 +0100393int spu_activate(struct spu_context *ctx, unsigned long flags)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500394{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500395
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100396 if (ctx->spu)
397 return 0;
398
399 do {
400 struct spu *spu;
401
402 spu = spu_get_idle(ctx);
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100403 /*
404 * If this is a realtime thread we try to get it running by
405 * preempting a lower priority thread.
406 */
407 if (!spu && ctx->rt_priority)
408 spu = find_victim(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100409 if (spu) {
Christoph Hellwig202557d2007-02-13 21:36:49 +0100410 spu_bind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100411 return 0;
Mark Nuttera68cf982006-10-04 17:26:12 +0200412 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100413
414 spu_add_to_rq(ctx);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100415 spu_prio_wait(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100416 spu_del_from_rq(ctx);
417 } while (!signal_pending(current));
418
419 return -ERESTARTSYS;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500420}
421
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100422/**
423 * spu_deactivate - unbind a context from it's physical spu
424 * @ctx: spu context to unbind
425 *
426 * Unbind @ctx from the physical spu it is running on and schedule
427 * the highest priority context to run on the freed physical spu.
428 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500429void spu_deactivate(struct spu_context *ctx)
430{
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100431 struct spu *spu = ctx->spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500432
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100433 if (spu) {
434 spu_unbind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100435 spu_reschedule(spu);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100436 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500437}
438
Christoph Hellwigae7b4c52007-02-13 21:54:26 +0100439/**
440 * spu_yield - yield a physical spu if others are waiting
441 * @ctx: spu context to yield
442 *
443 * Check if there is a higher priority context waiting and if yes
444 * unbind @ctx from the physical spu and schedule the highest
445 * priority context to run on the freed physical spu instead.
446 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500447void spu_yield(struct spu_context *ctx)
448{
449 struct spu *spu;
Arnd Bergmann51104592005-12-05 22:52:25 -0500450 int need_yield = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500451
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100452 if (mutex_trylock(&ctx->state_mutex)) {
Mark Nuttera68cf982006-10-04 17:26:12 +0200453 if ((spu = ctx->spu) != NULL) {
454 int best = sched_find_first_bit(spu_prio->bitmap);
455 if (best < MAX_PRIO) {
456 pr_debug("%s: yielding SPU %d NODE %d\n",
457 __FUNCTION__, spu->number, spu->node);
458 spu_deactivate(ctx);
Mark Nuttera68cf982006-10-04 17:26:12 +0200459 need_yield = 1;
Mark Nuttera68cf982006-10-04 17:26:12 +0200460 }
461 }
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100462 mutex_unlock(&ctx->state_mutex);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500463 }
Arnd Bergmann51104592005-12-05 22:52:25 -0500464 if (unlikely(need_yield))
465 yield();
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500466}
467
468int __init spu_sched_init(void)
469{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500470 int i;
471
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100472 spu_sched_wq = create_singlethread_workqueue("spusched");
473 if (!spu_sched_wq)
474 return 1;
475
Mark Nuttera68cf982006-10-04 17:26:12 +0200476 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
477 if (!spu_prio) {
478 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500479 __FUNCTION__);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100480 destroy_workqueue(spu_sched_wq);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500481 return 1;
482 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500483 for (i = 0; i < MAX_PRIO; i++) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100484 INIT_LIST_HEAD(&spu_prio->runq[i]);
Mark Nuttera68cf982006-10-04 17:26:12 +0200485 __clear_bit(i, spu_prio->bitmap);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500486 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200487 __set_bit(MAX_PRIO, spu_prio->bitmap);
488 for (i = 0; i < MAX_NUMNODES; i++) {
489 mutex_init(&spu_prio->active_mutex[i]);
490 INIT_LIST_HEAD(&spu_prio->active_list[i]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500491 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100492 spin_lock_init(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500493 return 0;
494}
495
496void __exit spu_sched_exit(void)
497{
Mark Nuttera68cf982006-10-04 17:26:12 +0200498 struct spu *spu, *tmp;
499 int node;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500500
Mark Nuttera68cf982006-10-04 17:26:12 +0200501 for (node = 0; node < MAX_NUMNODES; node++) {
502 mutex_lock(&spu_prio->active_mutex[node]);
503 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
504 list) {
505 list_del_init(&spu->list);
506 spu_free(spu);
507 }
508 mutex_unlock(&spu_prio->active_mutex[node]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500509 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200510 kfree(spu_prio);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100511 destroy_workqueue(spu_sched_wq);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500512}