blob: 1582d7645237de6f8a130ee6b7081333dbf5ed40 [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);
Arnd Bergmann390c5342007-04-23 21:08:10 +020079 mb();
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010080 queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE);
Christoph Hellwig08873092007-04-23 21:08:06 +020081 }
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010082}
83
84void spu_stop_tick(struct spu_context *ctx)
85{
Christoph Hellwig08873092007-04-23 21:08:06 +020086 if (ctx->policy == SCHED_RR) {
87 /*
88 * While the work can be rearming normally setting this flag
89 * makes sure it does not rearm itself anymore.
90 */
91 set_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
Arnd Bergmann390c5342007-04-23 21:08:10 +020092 mb();
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010093 cancel_delayed_work(&ctx->sched_work);
Christoph Hellwig08873092007-04-23 21:08:06 +020094 }
Christoph Hellwig2eb1b122007-02-13 21:54:29 +010095}
96
97void spu_sched_tick(struct work_struct *work)
98{
99 struct spu_context *ctx =
100 container_of(work, struct spu_context, sched_work.work);
101 struct spu *spu;
Christoph Hellwigb3e76cc2007-04-23 21:08:08 +0200102 int preempted = 0;
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100103
Christoph Hellwig08873092007-04-23 21:08:06 +0200104 /*
105 * If this context is being stopped avoid rescheduling from the
106 * scheduler tick because we would block on the state_mutex.
107 * The caller will yield the spu later on anyway.
108 */
109 if (test_bit(SPU_SCHED_EXITING, &ctx->sched_flags))
110 return;
111
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100112 mutex_lock(&ctx->state_mutex);
113 spu = ctx->spu;
114 if (spu) {
115 int best = sched_find_first_bit(spu_prio->bitmap);
116 if (best <= ctx->prio) {
117 spu_deactivate(ctx);
Christoph Hellwigb3e76cc2007-04-23 21:08:08 +0200118 preempted = 1;
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100119 }
120 }
121 mutex_unlock(&ctx->state_mutex);
122
Christoph Hellwigb3e76cc2007-04-23 21:08:08 +0200123 if (preempted) {
124 /*
125 * We need to break out of the wait loop in spu_run manually
126 * to ensure this context gets put on the runqueue again
127 * ASAP.
128 */
129 wake_up(&ctx->stop_wq);
130 } else
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100131 spu_start_tick(ctx);
132}
133
Christoph Hellwig202557d2007-02-13 21:36:49 +0100134/**
135 * spu_add_to_active_list - add spu to active list
136 * @spu: spu to add to the active list
137 */
138static void spu_add_to_active_list(struct spu *spu)
139{
140 mutex_lock(&spu_prio->active_mutex[spu->node]);
141 list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
142 mutex_unlock(&spu_prio->active_mutex[spu->node]);
143}
144
145/**
146 * spu_remove_from_active_list - remove spu from active list
147 * @spu: spu to remove from the active list
Christoph Hellwig202557d2007-02-13 21:36:49 +0100148 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100149static void spu_remove_from_active_list(struct spu *spu)
Christoph Hellwig202557d2007-02-13 21:36:49 +0100150{
151 int node = spu->node;
Christoph Hellwig202557d2007-02-13 21:36:49 +0100152
153 mutex_lock(&spu_prio->active_mutex[node]);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100154 list_del_init(&spu->list);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100155 mutex_unlock(&spu_prio->active_mutex[node]);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100156}
157
Arnd Bergmann86767272006-10-04 17:26:21 +0200158static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
159
160static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
161{
162 blocking_notifier_call_chain(&spu_switch_notifier,
163 ctx ? ctx->object_id : 0, spu);
164}
165
166int spu_switch_event_register(struct notifier_block * n)
167{
168 return blocking_notifier_chain_register(&spu_switch_notifier, n);
169}
170
171int spu_switch_event_unregister(struct notifier_block * n)
172{
173 return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
174}
175
Christoph Hellwig202557d2007-02-13 21:36:49 +0100176/**
177 * spu_bind_context - bind spu context to physical spu
178 * @spu: physical spu to bind to
179 * @ctx: context to bind
180 */
181static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500182{
Mark Nuttera68cf982006-10-04 17:26:12 +0200183 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
184 spu->number, spu->node);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500185 spu->ctx = ctx;
186 spu->flags = 0;
187 ctx->spu = spu;
188 ctx->ops = &spu_hw_ops;
189 spu->pid = current->pid;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100190 spu_associate_mm(spu, ctx->owner);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500191 spu->ibox_callback = spufs_ibox_callback;
192 spu->wbox_callback = spufs_wbox_callback;
Arnd Bergmann51104592005-12-05 22:52:25 -0500193 spu->stop_callback = spufs_stop_callback;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100194 spu->mfc_callback = spufs_mfc_callback;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200195 spu->dma_callback = spufs_dma_callback;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500196 mb();
Arnd Bergmann51104592005-12-05 22:52:25 -0500197 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500198 spu_restore(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500199 spu->timestamp = jiffies;
Mark Nuttera68cf982006-10-04 17:26:12 +0200200 spu_cpu_affinity_set(spu, raw_smp_processor_id());
Arnd Bergmann86767272006-10-04 17:26:21 +0200201 spu_switch_notify(spu, ctx);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100202 spu_add_to_active_list(spu);
Christoph Hellwig81998ba2007-02-13 21:36:48 +0100203 ctx->state = SPU_STATE_RUNNABLE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500204}
205
Christoph Hellwig202557d2007-02-13 21:36:49 +0100206/**
207 * spu_unbind_context - unbind spu context from physical spu
208 * @spu: physical spu to unbind from
209 * @ctx: context to unbind
Christoph Hellwig202557d2007-02-13 21:36:49 +0100210 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100211static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500212{
Mark Nuttera68cf982006-10-04 17:26:12 +0200213 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
214 spu->pid, spu->number, spu->node);
Christoph Hellwig202557d2007-02-13 21:36:49 +0100215
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100216 spu_remove_from_active_list(spu);
Arnd Bergmann86767272006-10-04 17:26:21 +0200217 spu_switch_notify(spu, NULL);
Arnd Bergmann51104592005-12-05 22:52:25 -0500218 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500219 spu_save(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500220 spu->timestamp = jiffies;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500221 ctx->state = SPU_STATE_SAVED;
222 spu->ibox_callback = NULL;
223 spu->wbox_callback = NULL;
Arnd Bergmann51104592005-12-05 22:52:25 -0500224 spu->stop_callback = NULL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100225 spu->mfc_callback = NULL;
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200226 spu->dma_callback = NULL;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100227 spu_associate_mm(spu, NULL);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500228 spu->pid = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500229 ctx->ops = &spu_backing_ops;
230 ctx->spu = NULL;
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500231 spu->flags = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500232 spu->ctx = NULL;
233}
234
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100235/**
236 * spu_add_to_rq - add a context to the runqueue
237 * @ctx: context to add
238 */
239static void spu_add_to_rq(struct spu_context *ctx)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500240{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100241 spin_lock(&spu_prio->runq_lock);
242 list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
243 set_bit(ctx->prio, spu_prio->bitmap);
Arnd Bergmann390c5342007-04-23 21:08:10 +0200244 mb();
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100245 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200246}
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500247
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100248/**
249 * spu_del_from_rq - remove a context from the runqueue
250 * @ctx: context to remove
251 */
252static void spu_del_from_rq(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200253{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100254 spin_lock(&spu_prio->runq_lock);
255 list_del_init(&ctx->rq);
256 if (list_empty(&spu_prio->runq[ctx->prio]))
257 clear_bit(ctx->prio, spu_prio->bitmap);
258 spin_unlock(&spu_prio->runq_lock);
Mark Nuttera68cf982006-10-04 17:26:12 +0200259}
260
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100261/**
262 * spu_grab_context - remove one context from the runqueue
263 * @prio: priority of the context to be removed
264 *
265 * This function removes one context from the runqueue for priority @prio.
266 * If there is more than one context with the given priority the first
267 * task on the runqueue will be taken.
268 *
269 * Returns the spu_context it just removed.
270 *
271 * Must be called with spu_prio->runq_lock held.
272 */
273static struct spu_context *spu_grab_context(int prio)
Mark Nuttera68cf982006-10-04 17:26:12 +0200274{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100275 struct list_head *rq = &spu_prio->runq[prio];
276
277 if (list_empty(rq))
278 return NULL;
279 return list_entry(rq->next, struct spu_context, rq);
280}
281
282static void spu_prio_wait(struct spu_context *ctx)
283{
Mark Nuttera68cf982006-10-04 17:26:12 +0200284 DEFINE_WAIT(wait);
285
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100286 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
Mark Nuttera68cf982006-10-04 17:26:12 +0200287 if (!signal_pending(current)) {
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100288 mutex_unlock(&ctx->state_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200289 schedule();
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100290 mutex_lock(&ctx->state_mutex);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500291 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100292 __set_current_state(TASK_RUNNING);
293 remove_wait_queue(&ctx->stop_wq, &wait);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500294}
295
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100296/**
297 * spu_reschedule - try to find a runnable context for a spu
298 * @spu: spu available
299 *
300 * This function is called whenever a spu becomes idle. It looks for the
301 * most suitable runnable spu context and schedules it for execution.
302 */
303static void spu_reschedule(struct spu *spu)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500304{
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100305 int best;
306
307 spu_free(spu);
308
309 spin_lock(&spu_prio->runq_lock);
310 best = sched_find_first_bit(spu_prio->bitmap);
Mark Nuttera68cf982006-10-04 17:26:12 +0200311 if (best < MAX_PRIO) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100312 struct spu_context *ctx = spu_grab_context(best);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100313 if (ctx)
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100314 wake_up(&ctx->stop_wq);
Arnd Bergmann51104592005-12-05 22:52:25 -0500315 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100316 spin_unlock(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500317}
318
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100319static struct spu *spu_get_idle(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200320{
321 struct spu *spu = NULL;
322 int node = cpu_to_node(raw_smp_processor_id());
323 int n;
324
325 for (n = 0; n < MAX_NUMNODES; n++, node++) {
326 node = (node < MAX_NUMNODES) ? node : 0;
327 if (!node_allowed(node))
328 continue;
329 spu = spu_alloc_node(node);
330 if (spu)
331 break;
332 }
333 return spu;
334}
335
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100336/**
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100337 * find_victim - find a lower priority context to preempt
338 * @ctx: canidate context for running
339 *
340 * Returns the freed physical spu to run the new context on.
341 */
342static struct spu *find_victim(struct spu_context *ctx)
343{
344 struct spu_context *victim = NULL;
345 struct spu *spu;
346 int node, n;
347
348 /*
349 * Look for a possible preemption candidate on the local node first.
350 * If there is no candidate look at the other nodes. This isn't
351 * exactly fair, but so far the whole spu schedule tries to keep
352 * a strong node affinity. We might want to fine-tune this in
353 * the future.
354 */
355 restart:
356 node = cpu_to_node(raw_smp_processor_id());
357 for (n = 0; n < MAX_NUMNODES; n++, node++) {
358 node = (node < MAX_NUMNODES) ? node : 0;
359 if (!node_allowed(node))
360 continue;
361
362 mutex_lock(&spu_prio->active_mutex[node]);
363 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
364 struct spu_context *tmp = spu->ctx;
365
366 if (tmp->rt_priority < ctx->rt_priority &&
367 (!victim || tmp->rt_priority < victim->rt_priority))
368 victim = spu->ctx;
369 }
370 mutex_unlock(&spu_prio->active_mutex[node]);
371
372 if (victim) {
373 /*
374 * This nests ctx->state_mutex, but we always lock
375 * higher priority contexts before lower priority
376 * ones, so this is safe until we introduce
377 * priority inheritance schemes.
378 */
379 if (!mutex_trylock(&victim->state_mutex)) {
380 victim = NULL;
381 goto restart;
382 }
383
384 spu = victim->spu;
385 if (!spu) {
386 /*
387 * This race can happen because we've dropped
388 * the active list mutex. No a problem, just
389 * restart the search.
390 */
391 mutex_unlock(&victim->state_mutex);
392 victim = NULL;
393 goto restart;
394 }
395 spu_unbind_context(spu, victim);
396 mutex_unlock(&victim->state_mutex);
Christoph Hellwige097b512007-04-23 21:08:09 +0200397 /*
398 * We need to break out of the wait loop in spu_run
399 * manually to ensure this context gets put on the
400 * runqueue again ASAP.
401 */
402 wake_up(&victim->stop_wq);
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100403 return spu;
404 }
405 }
406
407 return NULL;
408}
409
410/**
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100411 * spu_activate - find a free spu for a context and execute it
412 * @ctx: spu context to schedule
413 * @flags: flags (currently ignored)
414 *
Christoph Hellwig08873092007-04-23 21:08:06 +0200415 * Tries to find a free spu to run @ctx. If no free spu is available
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100416 * add the context to the runqueue so it gets woken up once an spu
417 * is available.
418 */
Christoph Hellwig26bec672007-02-13 21:54:24 +0100419int spu_activate(struct spu_context *ctx, unsigned long flags)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500420{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500421
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100422 if (ctx->spu)
423 return 0;
424
425 do {
426 struct spu *spu;
427
428 spu = spu_get_idle(ctx);
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100429 /*
430 * If this is a realtime thread we try to get it running by
431 * preempting a lower priority thread.
432 */
433 if (!spu && ctx->rt_priority)
434 spu = find_victim(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100435 if (spu) {
Christoph Hellwig202557d2007-02-13 21:36:49 +0100436 spu_bind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100437 return 0;
Mark Nuttera68cf982006-10-04 17:26:12 +0200438 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100439
440 spu_add_to_rq(ctx);
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100441 spu_prio_wait(ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100442 spu_del_from_rq(ctx);
443 } while (!signal_pending(current));
444
445 return -ERESTARTSYS;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500446}
447
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100448/**
449 * spu_deactivate - unbind a context from it's physical spu
450 * @ctx: spu context to unbind
451 *
452 * Unbind @ctx from the physical spu it is running on and schedule
453 * the highest priority context to run on the freed physical spu.
454 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500455void spu_deactivate(struct spu_context *ctx)
456{
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100457 struct spu *spu = ctx->spu;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500458
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100459 if (spu) {
460 spu_unbind_context(spu, ctx);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100461 spu_reschedule(spu);
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100462 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500463}
464
Christoph Hellwigae7b4c52007-02-13 21:54:26 +0100465/**
466 * spu_yield - yield a physical spu if others are waiting
467 * @ctx: spu context to yield
468 *
469 * Check if there is a higher priority context waiting and if yes
470 * unbind @ctx from the physical spu and schedule the highest
471 * priority context to run on the freed physical spu instead.
472 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500473void spu_yield(struct spu_context *ctx)
474{
475 struct spu *spu;
476
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100477 if (mutex_trylock(&ctx->state_mutex)) {
Mark Nuttera68cf982006-10-04 17:26:12 +0200478 if ((spu = ctx->spu) != NULL) {
479 int best = sched_find_first_bit(spu_prio->bitmap);
480 if (best < MAX_PRIO) {
481 pr_debug("%s: yielding SPU %d NODE %d\n",
482 __FUNCTION__, spu->number, spu->node);
483 spu_deactivate(ctx);
Mark Nuttera68cf982006-10-04 17:26:12 +0200484 }
485 }
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100486 mutex_unlock(&ctx->state_mutex);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500487 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500488}
489
490int __init spu_sched_init(void)
491{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500492 int i;
493
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100494 spu_sched_wq = create_singlethread_workqueue("spusched");
495 if (!spu_sched_wq)
496 return 1;
497
Mark Nuttera68cf982006-10-04 17:26:12 +0200498 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
499 if (!spu_prio) {
500 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500501 __FUNCTION__);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100502 destroy_workqueue(spu_sched_wq);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500503 return 1;
504 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500505 for (i = 0; i < MAX_PRIO; i++) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100506 INIT_LIST_HEAD(&spu_prio->runq[i]);
Mark Nuttera68cf982006-10-04 17:26:12 +0200507 __clear_bit(i, spu_prio->bitmap);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500508 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200509 __set_bit(MAX_PRIO, spu_prio->bitmap);
510 for (i = 0; i < MAX_NUMNODES; i++) {
511 mutex_init(&spu_prio->active_mutex[i]);
512 INIT_LIST_HEAD(&spu_prio->active_list[i]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500513 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100514 spin_lock_init(&spu_prio->runq_lock);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500515 return 0;
516}
517
518void __exit spu_sched_exit(void)
519{
Mark Nuttera68cf982006-10-04 17:26:12 +0200520 struct spu *spu, *tmp;
521 int node;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500522
Mark Nuttera68cf982006-10-04 17:26:12 +0200523 for (node = 0; node < MAX_NUMNODES; node++) {
524 mutex_lock(&spu_prio->active_mutex[node]);
525 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
526 list) {
527 list_del_init(&spu->list);
528 spu_free(spu);
529 }
530 mutex_unlock(&spu_prio->active_mutex[node]);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500531 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200532 kfree(spu_prio);
Christoph Hellwig2eb1b122007-02-13 21:54:29 +0100533 destroy_workqueue(spu_sched_wq);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500534}