1 /* sched.c - SPU scheduler.
3 * Copyright (C) IBM 2005
4 * Author: Mark Nutter <mnutter@us.ibm.com>
6 * 2006-03-31 NUMA domains added.
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)
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.
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.
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/sched.h>
28 #include <linux/kernel.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>
36 #include <linux/numa.h>
37 #include <linux/mutex.h>
38 #include <linux/notifier.h>
41 #include <asm/mmu_context.h>
43 #include <asm/spu_csa.h>
44 #include <asm/spu_priv1.h>
47 #define SPU_MIN_TIMESLICE (100 * HZ / 1000)
49 #define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1)
50 struct spu_prio_array {
51 unsigned long bitmap[SPU_BITMAP_SIZE];
52 struct list_head runq[MAX_PRIO];
54 struct list_head active_list[MAX_NUMNODES];
55 struct mutex active_mutex[MAX_NUMNODES];
58 static struct spu_prio_array *spu_prio;
60 static inline int node_allowed(int node)
64 if (!nr_cpus_node(node))
66 mask = node_to_cpumask(node);
67 if (!cpus_intersects(mask, current->cpus_allowed))
73 * spu_add_to_active_list - add spu to active list
74 * @spu: spu to add to the active list
76 static void spu_add_to_active_list(struct spu *spu)
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]);
84 * spu_remove_from_active_list - remove spu from active list
85 * @spu: spu to remove from the active list
87 static void spu_remove_from_active_list(struct spu *spu)
91 mutex_lock(&spu_prio->active_mutex[node]);
92 list_del_init(&spu->list);
93 mutex_unlock(&spu_prio->active_mutex[node]);
96 static inline void mm_needs_global_tlbie(struct mm_struct *mm)
98 int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
100 /* Global TLBIE broadcast required with SPEs. */
101 __cpus_setall(&mm->cpu_vm_mask, nr);
104 static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
106 static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
108 blocking_notifier_call_chain(&spu_switch_notifier,
109 ctx ? ctx->object_id : 0, spu);
112 int spu_switch_event_register(struct notifier_block * n)
114 return blocking_notifier_chain_register(&spu_switch_notifier, n);
117 int spu_switch_event_unregister(struct notifier_block * n)
119 return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
123 * spu_bind_context - bind spu context to physical spu
124 * @spu: physical spu to bind to
125 * @ctx: context to bind
127 static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
129 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
130 spu->number, spu->node);
134 ctx->ops = &spu_hw_ops;
135 spu->pid = current->pid;
136 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;
140 spu->stop_callback = spufs_stop_callback;
141 spu->mfc_callback = spufs_mfc_callback;
142 spu->dma_callback = spufs_dma_callback;
144 spu_unmap_mappings(ctx);
145 spu_restore(&ctx->csa, spu);
146 spu->timestamp = jiffies;
147 spu_cpu_affinity_set(spu, raw_smp_processor_id());
148 spu_switch_notify(spu, ctx);
149 spu_add_to_active_list(spu);
150 ctx->state = SPU_STATE_RUNNABLE;
154 * spu_unbind_context - unbind spu context from physical spu
155 * @spu: physical spu to unbind from
156 * @ctx: context to unbind
158 static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
160 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
161 spu->pid, spu->number, spu->node);
163 spu_remove_from_active_list(spu);
164 spu_switch_notify(spu, NULL);
165 spu_unmap_mappings(ctx);
166 spu_save(&ctx->csa, spu);
167 spu->timestamp = jiffies;
168 ctx->state = SPU_STATE_SAVED;
169 spu->ibox_callback = NULL;
170 spu->wbox_callback = NULL;
171 spu->stop_callback = NULL;
172 spu->mfc_callback = NULL;
173 spu->dma_callback = NULL;
176 ctx->ops = &spu_backing_ops;
183 * spu_add_to_rq - add a context to the runqueue
184 * @ctx: context to add
186 static void spu_add_to_rq(struct spu_context *ctx)
188 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);
195 * spu_del_from_rq - remove a context from the runqueue
196 * @ctx: context to remove
198 static void spu_del_from_rq(struct spu_context *ctx)
200 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);
208 * spu_grab_context - remove one context from the runqueue
209 * @prio: priority of the context to be removed
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.
215 * Returns the spu_context it just removed.
217 * Must be called with spu_prio->runq_lock held.
219 static struct spu_context *spu_grab_context(int prio)
221 struct list_head *rq = &spu_prio->runq[prio];
225 return list_entry(rq->next, struct spu_context, rq);
228 static void spu_prio_wait(struct spu_context *ctx)
232 set_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
233 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
234 if (!signal_pending(current)) {
235 mutex_unlock(&ctx->state_mutex);
237 mutex_lock(&ctx->state_mutex);
239 __set_current_state(TASK_RUNNING);
240 remove_wait_queue(&ctx->stop_wq, &wait);
241 clear_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
245 * spu_reschedule - try to find a runnable context for a spu
246 * @spu: spu available
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.
251 static void spu_reschedule(struct spu *spu)
257 spin_lock(&spu_prio->runq_lock);
258 best = sched_find_first_bit(spu_prio->bitmap);
259 if (best < MAX_PRIO) {
260 struct spu_context *ctx = spu_grab_context(best);
261 if (ctx && test_bit(SPU_SCHED_WAKE, &ctx->sched_flags))
262 wake_up(&ctx->stop_wq);
264 spin_unlock(&spu_prio->runq_lock);
267 static struct spu *spu_get_idle(struct spu_context *ctx)
269 struct spu *spu = NULL;
270 int node = cpu_to_node(raw_smp_processor_id());
273 for (n = 0; n < MAX_NUMNODES; n++, node++) {
274 node = (node < MAX_NUMNODES) ? node : 0;
275 if (!node_allowed(node))
277 spu = spu_alloc_node(node);
285 * spu_activate - find a free spu for a context and execute it
286 * @ctx: spu context to schedule
287 * @flags: flags (currently ignored)
289 * Tries to find a free spu to run @ctx. If no free spu is availble
290 * add the context to the runqueue so it gets woken up once an spu
293 int spu_activate(struct spu_context *ctx, unsigned long flags)
302 spu = spu_get_idle(ctx);
304 spu_bind_context(spu, ctx);
309 if (!(flags & SPU_ACTIVATE_NOWAKE))
311 spu_del_from_rq(ctx);
312 } while (!signal_pending(current));
318 * spu_deactivate - unbind a context from it's physical spu
319 * @ctx: spu context to unbind
321 * Unbind @ctx from the physical spu it is running on and schedule
322 * the highest priority context to run on the freed physical spu.
324 void spu_deactivate(struct spu_context *ctx)
326 struct spu *spu = ctx->spu;
329 spu_unbind_context(spu, ctx);
335 * spu_yield - yield a physical spu if others are waiting
336 * @ctx: spu context to yield
338 * Check if there is a higher priority context waiting and if yes
339 * unbind @ctx from the physical spu and schedule the highest
340 * priority context to run on the freed physical spu instead.
342 void spu_yield(struct spu_context *ctx)
347 if (mutex_trylock(&ctx->state_mutex)) {
348 if ((spu = ctx->spu) != NULL) {
349 int best = sched_find_first_bit(spu_prio->bitmap);
350 if (best < MAX_PRIO) {
351 pr_debug("%s: yielding SPU %d NODE %d\n",
352 __FUNCTION__, spu->number, spu->node);
357 mutex_unlock(&ctx->state_mutex);
359 if (unlikely(need_yield))
363 int __init spu_sched_init(void)
367 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
369 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
373 for (i = 0; i < MAX_PRIO; i++) {
374 INIT_LIST_HEAD(&spu_prio->runq[i]);
375 __clear_bit(i, spu_prio->bitmap);
377 __set_bit(MAX_PRIO, spu_prio->bitmap);
378 for (i = 0; i < MAX_NUMNODES; i++) {
379 mutex_init(&spu_prio->active_mutex[i]);
380 INIT_LIST_HEAD(&spu_prio->active_list[i]);
382 spin_lock_init(&spu_prio->runq_lock);
386 void __exit spu_sched_exit(void)
388 struct spu *spu, *tmp;
391 for (node = 0; node < MAX_NUMNODES; node++) {
392 mutex_lock(&spu_prio->active_mutex[node]);
393 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
395 list_del_init(&spu->list);
398 mutex_unlock(&spu_prio->active_mutex[node]);