blob: 003e330fc76fd90b1826860cf47155a72113d93e [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{
Christoph Hellwig08873092007-04-23 21:08:06 +020074 if (ctx->policy == SCHED_RR) {
75 /*
76 * Make sure the exiting bit is cleared.
77 */
78 clear_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010079 queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE);
Christoph Hellwig08873092007-04-23 21:08:06 +020080 }
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010081}
82
83void spu_stop_tick(struct spu_context *ctx)
84{
Christoph Hellwig08873092007-04-23 21:08:06 +020085 if (ctx->policy == SCHED_RR) {
86 /*
87 * While the work can be rearming normally setting this flag
88 * makes sure it does not rearm itself anymore.
89 */
90 set_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010091 cancel_delayed_work(&ctx->sched_work);
Christoph Hellwig08873092007-04-23 21:08:06 +020092 }
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010093}
94
95void spu_sched_tick(struct work_struct *work)
96{
97 struct spu_context *ctx =
98 container_of(work, struct spu_context, sched_work.work);
99 struct spu *spu;
100 int rearm = 1;
101
Christoph Hellwig08873092007-04-23 21:08:06 +0200102 /*
103 * If this context is being stopped avoid rescheduling from the
104 * scheduler tick because we would block on the state_mutex.
105 * The caller will yield the spu later on anyway.
106 */
107 if (test_bit(SPU_SCHED_EXITING, &ctx->sched_flags))
108 return;
109
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100110 mutex_lock(&ctx->state_mutex);
111 spu = ctx->spu;
112 if (spu) {
113 int best = sched_find_first_bit(spu_prio->bitmap);
114 if (best <= ctx->prio) {
115 spu_deactivate(ctx);
116 rearm = 0;
117 }
118 }
119 mutex_unlock(&ctx->state_mutex);
120
121 if (rearm)
122 spu_start_tick(ctx);
123}
124
Christoph Hellwig202557d2007-02-13 21:36:49 +0100125/**
126 * spu_add_to_active_list - add spu to active list
127 * @spu: spu to add to the active list
128 */
129static void spu_add_to_active_list(struct spu *spu)
130{
131 mutex_lock(&spu_prio->active_mutex[spu->node]);
132 list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
133 mutex_unlock(&spu_prio->active_mutex[spu->node]);
134}
135
136/**
137 * spu_remove_from_active_list - remove spu from active list
138 * @spu: spu to remove from the active list
Christoph Hellwig202557d2007-02-13 21:36:49 +0100139 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100140static void spu_remove_from_active_list(struct spu *spu)
Christoph Hellwig202557d2007-02-13 21:36:49 +0100141{
142 int node = spu->node;
Christoph Hellwig202557d2007-02-13 21:36:49 +0100143
144 mutex_lock(&spu_prio->active_mutex[node]);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100145 list_del_init(&spu->list);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100146 mutex_unlock(&spu_prio->active_mutex[node]);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100147}
148
Arnd Bergmann86767272006-10-04 17:26:21 +0200149static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
150
151static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
152{
153 blocking_notifier_call_chain(&spu_switch_notifier,
154 ctx ? ctx->object_id : 0, spu);
155}
156
157int spu_switch_event_register(struct notifier_block * n)
158{
159 return blocking_notifier_chain_register(&spu_switch_notifier, n);
160}
161
162int spu_switch_event_unregister(struct notifier_block * n)
163{
164 return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
165}
166
Christoph Hellwig202557d2007-02-13 21:36:49 +0100167/**
168 * spu_bind_context - bind spu context to physical spu
169 * @spu: physical spu to bind to
170 * @ctx: context to bind
171 */
172static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500173{
Mark Nuttera68cf982006-10-04 17:26:12 +0200174 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
175 spu->number, spu->node);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500176 spu->ctx = ctx;
177 spu->flags = 0;
178 ctx->spu = spu;
179 ctx->ops = &spu_hw_ops;
180 spu->pid = current->pid;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100181 spu_associate_mm(spu, ctx->owner);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500182 spu->ibox_callback = spufs_ibox_callback;
183 spu->wbox_callback = spufs_wbox_callback;
Arnd Bergmann51104592005-12-05 22:52:25 -0500184 spu->stop_callback = spufs_stop_callback;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100185 spu->mfc_callback = spufs_mfc_callback;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200186 spu->dma_callback = spufs_dma_callback;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500187 mb();
Arnd Bergmann51104592005-12-05 22:52:25 -0500188 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500189 spu_restore(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500190 spu->timestamp = jiffies;
Mark Nuttera68cf982006-10-04 17:26:12 +0200191 spu_cpu_affinity_set(spu, raw_smp_processor_id());
Arnd Bergmann86767272006-10-04 17:26:21 +0200192 spu_switch_notify(spu, ctx);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100193 spu_add_to_active_list(spu);
Christoph Hellwig81998ba2007-02-13 21:36:48 +0100194 ctx->state = SPU_STATE_RUNNABLE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500195}
196
Christoph Hellwig202557d2007-02-13 21:36:49 +0100197/**
198 * spu_unbind_context - unbind spu context from physical spu
199 * @spu: physical spu to unbind from
200 * @ctx: context to unbind
Christoph Hellwig202557d2007-02-13 21:36:49 +0100201 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100202static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500203{
Mark Nuttera68cf982006-10-04 17:26:12 +0200204 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
205 spu->pid, spu->number, spu->node);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100206
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100207 spu_remove_from_active_list(spu);
Arnd Bergmann86767272006-10-04 17:26:21 +0200208 spu_switch_notify(spu, NULL);
Arnd Bergmann51104592005-12-05 22:52:25 -0500209 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500210 spu_save(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500211 spu->timestamp = jiffies;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500212 ctx->state = SPU_STATE_SAVED;
213 spu->ibox_callback = NULL;
214 spu->wbox_callback = NULL;
Arnd Bergmann51104592005-12-05 22:52:25 -0500215 spu->stop_callback = NULL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100216 spu->mfc_callback = NULL;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200217 spu->dma_callback = NULL;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100218 spu_associate_mm(spu, NULL);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500219 spu->pid = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500220 ctx->ops = &spu_backing_ops;
221 ctx->spu = NULL;
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500222 spu->flags = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500223 spu->ctx = NULL;
224}
225
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100226/**
227 * spu_add_to_rq - add a context to the runqueue
228 * @ctx: context to add
229 */
230static void spu_add_to_rq(struct spu_context *ctx)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500231{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100232 spin_lock(&spu_prio->runq_lock);
233 list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
234 set_bit(ctx->prio, spu_prio->bitmap);
235 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200236}
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500237
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100238/**
239 * spu_del_from_rq - remove a context from the runqueue
240 * @ctx: context to remove
241 */
242static void spu_del_from_rq(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200243{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100244 spin_lock(&spu_prio->runq_lock);
245 list_del_init(&ctx->rq);
246 if (list_empty(&spu_prio->runq[ctx->prio]))
247 clear_bit(ctx->prio, spu_prio->bitmap);
248 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200249}
250
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100251/**
252 * spu_grab_context - remove one context from the runqueue
253 * @prio: priority of the context to be removed
254 *
255 * This function removes one context from the runqueue for priority @prio.
256 * If there is more than one context with the given priority the first
257 * task on the runqueue will be taken.
258 *
259 * Returns the spu_context it just removed.
260 *
261 * Must be called with spu_prio->runq_lock held.
262 */
263static struct spu_context *spu_grab_context(int prio)
Mark Nuttera68cf982006-10-04 17:26:12 +0200264{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100265 struct list_head *rq = &spu_prio->runq[prio];
266
267 if (list_empty(rq))
268 return NULL;
269 return list_entry(rq->next, struct spu_context, rq);
270}
271
272static void spu_prio_wait(struct spu_context *ctx)
273{
Mark Nuttera68cf982006-10-04 17:26:12 +0200274 DEFINE_WAIT(wait);
275
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100276 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
Mark Nuttera68cf982006-10-04 17:26:12 +0200277 if (!signal_pending(current)) {
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100278 mutex_unlock(&ctx->state_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200279 schedule();
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100280 mutex_lock(&ctx->state_mutex);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500281 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100282 __set_current_state(TASK_RUNNING);
283 remove_wait_queue(&ctx->stop_wq, &wait);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500284}
285
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100286/**
287 * spu_reschedule - try to find a runnable context for a spu
288 * @spu: spu available
289 *
290 * This function is called whenever a spu becomes idle. It looks for the
291 * most suitable runnable spu context and schedules it for execution.
292 */
293static void spu_reschedule(struct spu *spu)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500294{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100295 int best;
296
297 spu_free(spu);
298
299 spin_lock(&spu_prio->runq_lock);
300 best = sched_find_first_bit(spu_prio->bitmap);
Mark Nuttera68cf982006-10-04 17:26:12 +0200301 if (best < MAX_PRIO) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100302 struct spu_context *ctx = spu_grab_context(best);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100303 if (ctx)
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100304 wake_up(&ctx->stop_wq);
Arnd Bergmann51104592005-12-05 22:52:25 -0500305 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100306 spin_unlock(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500307}
308
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100309static struct spu *spu_get_idle(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200310{
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
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100326/**
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100327 * 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 */
332static 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 return spu;
388 }
389 }
390
391 return NULL;
392}
393
394/**
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100395 * spu_activate - find a free spu for a context and execute it
396 * @ctx: spu context to schedule
397 * @flags: flags (currently ignored)
398 *
Christoph Hellwig08873092007-04-23 21:08:06 +0200399 * Tries to find a free spu to run @ctx. If no free spu is available
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100400 * add the context to the runqueue so it gets woken up once an spu
401 * is available.
402 */
Christoph Hellwig26bec672007-02-13 21:54:24 +0100403int spu_activate(struct spu_context *ctx, unsigned long flags)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500404{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500405
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100406 if (ctx->spu)
407 return 0;
408
409 do {
410 struct spu *spu;
411
412 spu = spu_get_idle(ctx);
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100413 /*
414 * If this is a realtime thread we try to get it running by
415 * preempting a lower priority thread.
416 */
417 if (!spu && ctx->rt_priority)
418 spu = find_victim(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100419 if (spu) {
Christoph Hellwig202557d2007-02-13 21:36:49 +0100420 spu_bind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100421 return 0;
Mark Nuttera68cf982006-10-04 17:26:12 +0200422 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100423
424 spu_add_to_rq(ctx);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100425 spu_prio_wait(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100426 spu_del_from_rq(ctx);
427 } while (!signal_pending(current));
428
429 return -ERESTARTSYS;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500430}
431
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100432/**
433 * spu_deactivate - unbind a context from it's physical spu
434 * @ctx: spu context to unbind
435 *
436 * Unbind @ctx from the physical spu it is running on and schedule
437 * the highest priority context to run on the freed physical spu.
438 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500439void spu_deactivate(struct spu_context *ctx)
440{
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100441 struct spu *spu = ctx->spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500442
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100443 if (spu) {
444 spu_unbind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100445 spu_reschedule(spu);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100446 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500447}
448
Christoph Hellwigae7b4c52007-02-13 21:54:26 +0100449/**
450 * spu_yield - yield a physical spu if others are waiting
451 * @ctx: spu context to yield
452 *
453 * Check if there is a higher priority context waiting and if yes
454 * unbind @ctx from the physical spu and schedule the highest
455 * priority context to run on the freed physical spu instead.
456 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500457void spu_yield(struct spu_context *ctx)
458{
459 struct spu *spu;
460
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100461 if (mutex_trylock(&ctx->state_mutex)) {
Mark Nuttera68cf982006-10-04 17:26:12 +0200462 if ((spu = ctx->spu) != NULL) {
463 int best = sched_find_first_bit(spu_prio->bitmap);
464 if (best < MAX_PRIO) {
465 pr_debug("%s: yielding SPU %d NODE %d\n",
466 __FUNCTION__, spu->number, spu->node);
467 spu_deactivate(ctx);
Mark Nuttera68cf982006-10-04 17:26:12 +0200468 }
469 }
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100470 mutex_unlock(&ctx->state_mutex);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500471 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500472}
473
474int __init spu_sched_init(void)
475{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500476 int i;
477
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100478 spu_sched_wq = create_singlethread_workqueue("spusched");
479 if (!spu_sched_wq)
480 return 1;
481
Mark Nuttera68cf982006-10-04 17:26:12 +0200482 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
483 if (!spu_prio) {
484 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500485 __FUNCTION__);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100486 destroy_workqueue(spu_sched_wq);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500487 return 1;
488 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500489 for (i = 0; i < MAX_PRIO; i++) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100490 INIT_LIST_HEAD(&spu_prio->runq[i]);
Mark Nuttera68cf982006-10-04 17:26:12 +0200491 __clear_bit(i, spu_prio->bitmap);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500492 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200493 __set_bit(MAX_PRIO, spu_prio->bitmap);
494 for (i = 0; i < MAX_NUMNODES; i++) {
495 mutex_init(&spu_prio->active_mutex[i]);
496 INIT_LIST_HEAD(&spu_prio->active_list[i]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500497 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100498 spin_lock_init(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500499 return 0;
500}
501
502void __exit spu_sched_exit(void)
503{
Mark Nuttera68cf982006-10-04 17:26:12 +0200504 struct spu *spu, *tmp;
505 int node;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500506
Mark Nuttera68cf982006-10-04 17:26:12 +0200507 for (node = 0; node < MAX_NUMNODES; node++) {
508 mutex_lock(&spu_prio->active_mutex[node]);
509 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
510 list) {
511 list_del_init(&spu->list);
512 spu_free(spu);
513 }
514 mutex_unlock(&spu_prio->active_mutex[node]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500515 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200516 kfree(spu_prio);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100517 destroy_workqueue(spu_sched_wq);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500518}