blob: 00d914232af1450318f1c08256c3a0da91d47294 [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>
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050033#include <linux/stddef.h>
34#include <linux/unistd.h>
Mark Nuttera68cf982006-10-04 17:26:12 +020035#include <linux/numa.h>
36#include <linux/mutex.h>
Arnd Bergmann86767272006-10-04 17:26:21 +020037#include <linux/notifier.h>
Christoph Hellwig37901802007-06-29 10:57:51 +100038#include <linux/kthread.h>
Christoph Hellwig65de66f2007-06-29 10:58:02 +100039#include <linux/pid_namespace.h>
40#include <linux/proc_fs.h>
41#include <linux/seq_file.h>
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050042
43#include <asm/io.h>
44#include <asm/mmu_context.h>
45#include <asm/spu.h>
46#include <asm/spu_csa.h>
Geoff Levanda91942a2006-06-19 20:33:30 +020047#include <asm/spu_priv1.h>
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050048#include "spufs.h"
49
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050050struct spu_prio_array {
Christoph Hellwig72cb3602007-02-13 21:54:28 +010051 DECLARE_BITMAP(bitmap, MAX_PRIO);
Christoph Hellwig079cdb62007-02-13 21:54:23 +010052 struct list_head runq[MAX_PRIO];
53 spinlock_t runq_lock;
Christoph Hellwig65de66f2007-06-29 10:58:02 +100054 int nr_waiting;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050055};
56
Christoph Hellwig65de66f2007-06-29 10:58:02 +100057static unsigned long spu_avenrun[3];
Mark Nuttera68cf982006-10-04 17:26:12 +020058static struct spu_prio_array *spu_prio;
Christoph Hellwig37901802007-06-29 10:57:51 +100059static struct task_struct *spusched_task;
60static struct timer_list spusched_timer;
Aegis Lin90608a22007-12-20 16:39:59 +090061static struct timer_list spuloadavg_timer;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050062
Christoph Hellwigfe443ef2007-06-29 10:57:52 +100063/*
64 * Priority of a normal, non-rt, non-niced'd process (aka nice level 0).
65 */
66#define NORMAL_PRIO 120
67
68/*
69 * Frequency of the spu scheduler tick. By default we do one SPU scheduler
70 * tick for every 10 CPU scheduler ticks.
71 */
72#define SPUSCHED_TICK (10)
73
74/*
75 * These are the 'tuning knobs' of the scheduler:
76 *
Jeremy Kerr60e24232007-06-29 10:57:53 +100077 * Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is
78 * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs.
Christoph Hellwigfe443ef2007-06-29 10:57:52 +100079 */
Jeremy Kerr60e24232007-06-29 10:57:53 +100080#define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1)
81#define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK))
Christoph Hellwigfe443ef2007-06-29 10:57:52 +100082
83#define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO)
84#define SCALE_PRIO(x, prio) \
85 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE)
86
87/*
88 * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values:
89 * [800ms ... 100ms ... 5ms]
90 *
91 * The higher a thread's priority, the bigger timeslices
92 * it gets during one round of execution. But even the lowest
93 * priority thread gets MIN_TIMESLICE worth of execution time.
94 */
95void spu_set_timeslice(struct spu_context *ctx)
96{
97 if (ctx->prio < NORMAL_PRIO)
98 ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio);
99 else
100 ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio);
101}
102
Christoph Hellwig2cf2b3b2007-06-29 10:57:55 +1000103/*
104 * Update scheduling information from the owning thread.
105 */
106void __spu_update_sched_info(struct spu_context *ctx)
107{
108 /*
Luke Browning91569532007-12-20 16:39:59 +0900109 * assert that the context is not on the runqueue, so it is safe
110 * to change its scheduling parameters.
111 */
112 BUG_ON(!list_empty(&ctx->rq));
113
114 /*
Julio M. Merino Vidal9b1d21f2007-12-20 16:39:59 +0900115 * 32-Bit assignments are atomic on powerpc, and we don't care about
116 * memory ordering here because retrieving the controlling thread is
117 * per definition racy.
Christoph Hellwig476273a2007-06-29 10:58:01 +1000118 */
119 ctx->tid = current->pid;
120
121 /*
Christoph Hellwig2cf2b3b2007-06-29 10:57:55 +1000122 * We do our own priority calculations, so we normally want
Julio M. Merino Vidal9b1d21f2007-12-20 16:39:59 +0900123 * ->static_prio to start with. Unfortunately this field
Christoph Hellwig2cf2b3b2007-06-29 10:57:55 +1000124 * contains junk for threads with a realtime scheduling
125 * policy so we have to look at ->prio in this case.
126 */
127 if (rt_prio(current->prio))
128 ctx->prio = current->prio;
129 else
130 ctx->prio = current->static_prio;
131 ctx->policy = current->policy;
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000132
133 /*
Luke Browning91569532007-12-20 16:39:59 +0900134 * TO DO: the context may be loaded, so we may need to activate
135 * it again on a different node. But it shouldn't hurt anything
136 * to update its parameters, because we know that the scheduler
137 * is not actively looking at this field, since it is not on the
138 * runqueue. The context will be rescheduled on the proper node
139 * if it is timesliced or preempted.
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000140 */
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000141 ctx->cpus_allowed = current->cpus_allowed;
Christoph Hellwig2cf2b3b2007-06-29 10:57:55 +1000142}
143
144void spu_update_sched_info(struct spu_context *ctx)
145{
Luke Browning91569532007-12-20 16:39:59 +0900146 int node;
Christoph Hellwig2cf2b3b2007-06-29 10:57:55 +1000147
Luke Browning91569532007-12-20 16:39:59 +0900148 if (ctx->state == SPU_STATE_RUNNABLE) {
149 node = ctx->spu->node;
Luke Browninge65c2f62007-12-20 16:39:59 +0900150
151 /*
152 * Take list_mutex to sync with find_victim().
153 */
Luke Browning91569532007-12-20 16:39:59 +0900154 mutex_lock(&cbe_spu_info[node].list_mutex);
155 __spu_update_sched_info(ctx);
156 mutex_unlock(&cbe_spu_info[node].list_mutex);
157 } else {
158 __spu_update_sched_info(ctx);
159 }
Christoph Hellwig2cf2b3b2007-06-29 10:57:55 +1000160}
161
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000162static int __node_allowed(struct spu_context *ctx, int node)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500163{
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000164 if (nr_cpus_node(node)) {
165 cpumask_t mask = node_to_cpumask(node);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500166
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000167 if (cpus_intersects(mask, ctx->cpus_allowed))
168 return 1;
169 }
170
171 return 0;
172}
173
174static int node_allowed(struct spu_context *ctx, int node)
175{
176 int rval;
177
178 spin_lock(&spu_prio->runq_lock);
179 rval = __node_allowed(ctx, node);
180 spin_unlock(&spu_prio->runq_lock);
181
182 return rval;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500183}
184
Bob Nelsonaed3a8c2007-12-15 01:27:30 +1100185void do_notify_spus_active(void)
Bob Nelson36aaccc2007-07-20 21:39:52 +0200186{
187 int node;
188
189 /*
190 * Wake up the active spu_contexts.
191 *
192 * When the awakened processes see their "notify_active" flag is set,
Julio M. Merino Vidal9b1d21f2007-12-20 16:39:59 +0900193 * they will call spu_switch_notify().
Bob Nelson36aaccc2007-07-20 21:39:52 +0200194 */
195 for_each_online_node(node) {
196 struct spu *spu;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200197
198 mutex_lock(&cbe_spu_info[node].list_mutex);
199 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
200 if (spu->alloc_state != SPU_FREE) {
201 struct spu_context *ctx = spu->ctx;
202 set_bit(SPU_SCHED_NOTIFY_ACTIVE,
203 &ctx->sched_flags);
204 mb();
205 wake_up_all(&ctx->stop_wq);
206 }
Bob Nelson36aaccc2007-07-20 21:39:52 +0200207 }
Christoph Hellwig486acd42007-07-20 21:39:54 +0200208 mutex_unlock(&cbe_spu_info[node].list_mutex);
Bob Nelson36aaccc2007-07-20 21:39:52 +0200209 }
210}
211
Christoph Hellwig202557d2007-02-13 21:36:49 +0100212/**
213 * spu_bind_context - bind spu context to physical spu
214 * @spu: physical spu to bind to
215 * @ctx: context to bind
216 */
217static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500218{
Mark Nuttera68cf982006-10-04 17:26:12 +0200219 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
220 spu->number, spu->node);
Andre Detsch27ec41d2007-07-20 21:39:33 +0200221 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000222
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200223 if (ctx->flags & SPU_CREATE_NOSCHED)
224 atomic_inc(&cbe_spu_info[spu->node].reserved_spus);
225
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000226 ctx->stats.slb_flt_base = spu->stats.slb_flt;
227 ctx->stats.class2_intr_base = spu->stats.class2_intr;
228
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500229 spu->ctx = ctx;
230 spu->flags = 0;
231 ctx->spu = spu;
232 ctx->ops = &spu_hw_ops;
233 spu->pid = current->pid;
Bob Nelson14748552007-07-20 21:39:53 +0200234 spu->tgid = current->tgid;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100235 spu_associate_mm(spu, ctx->owner);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500236 spu->ibox_callback = spufs_ibox_callback;
237 spu->wbox_callback = spufs_wbox_callback;
Arnd Bergmann51104592005-12-05 22:52:25 -0500238 spu->stop_callback = spufs_stop_callback;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100239 spu->mfc_callback = spufs_mfc_callback;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500240 mb();
Arnd Bergmann51104592005-12-05 22:52:25 -0500241 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500242 spu_restore(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500243 spu->timestamp = jiffies;
Mark Nuttera68cf982006-10-04 17:26:12 +0200244 spu_cpu_affinity_set(spu, raw_smp_processor_id());
Arnd Bergmann86767272006-10-04 17:26:21 +0200245 spu_switch_notify(spu, ctx);
Christoph Hellwig81998ba2007-02-13 21:36:48 +0100246 ctx->state = SPU_STATE_RUNNABLE;
Andre Detsch27ec41d2007-07-20 21:39:33 +0200247
248 spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500249}
250
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200251/*
Christoph Hellwig486acd42007-07-20 21:39:54 +0200252 * Must be used with the list_mutex held.
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200253 */
254static inline int sched_spu(struct spu *spu)
255{
Christoph Hellwig486acd42007-07-20 21:39:54 +0200256 BUG_ON(!mutex_is_locked(&cbe_spu_info[spu->node].list_mutex));
257
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200258 return (!spu->ctx || !(spu->ctx->flags & SPU_CREATE_NOSCHED));
259}
260
261static void aff_merge_remaining_ctxs(struct spu_gang *gang)
262{
263 struct spu_context *ctx;
264
265 list_for_each_entry(ctx, &gang->aff_list_head, aff_list) {
266 if (list_empty(&ctx->aff_list))
267 list_add(&ctx->aff_list, &gang->aff_list_head);
268 }
269 gang->aff_flags |= AFF_MERGED;
270}
271
272static void aff_set_offsets(struct spu_gang *gang)
273{
274 struct spu_context *ctx;
275 int offset;
276
277 offset = -1;
278 list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,
279 aff_list) {
280 if (&ctx->aff_list == &gang->aff_list_head)
281 break;
282 ctx->aff_offset = offset--;
283 }
284
285 offset = 0;
286 list_for_each_entry(ctx, gang->aff_ref_ctx->aff_list.prev, aff_list) {
287 if (&ctx->aff_list == &gang->aff_list_head)
288 break;
289 ctx->aff_offset = offset++;
290 }
291
292 gang->aff_flags |= AFF_OFFSETS_SET;
293}
294
295static struct spu *aff_ref_location(struct spu_context *ctx, int mem_aff,
296 int group_size, int lowest_offset)
297{
298 struct spu *spu;
299 int node, n;
300
301 /*
302 * TODO: A better algorithm could be used to find a good spu to be
303 * used as reference location for the ctxs chain.
304 */
305 node = cpu_to_node(raw_smp_processor_id());
306 for (n = 0; n < MAX_NUMNODES; n++, node++) {
307 node = (node < MAX_NUMNODES) ? node : 0;
308 if (!node_allowed(ctx, node))
309 continue;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200310 mutex_lock(&cbe_spu_info[node].list_mutex);
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200311 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
312 if ((!mem_aff || spu->has_mem_affinity) &&
Christoph Hellwig486acd42007-07-20 21:39:54 +0200313 sched_spu(spu)) {
314 mutex_unlock(&cbe_spu_info[node].list_mutex);
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200315 return spu;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200316 }
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200317 }
Christoph Hellwig486acd42007-07-20 21:39:54 +0200318 mutex_unlock(&cbe_spu_info[node].list_mutex);
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200319 }
320 return NULL;
321}
322
323static void aff_set_ref_point_location(struct spu_gang *gang)
324{
325 int mem_aff, gs, lowest_offset;
326 struct spu_context *ctx;
327 struct spu *tmp;
328
329 mem_aff = gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM;
330 lowest_offset = 0;
331 gs = 0;
332
333 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
334 gs++;
335
336 list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,
337 aff_list) {
338 if (&ctx->aff_list == &gang->aff_list_head)
339 break;
340 lowest_offset = ctx->aff_offset;
341 }
342
Andre Detsch683e3ab2007-07-31 09:48:11 +1000343 gang->aff_ref_spu = aff_ref_location(gang->aff_ref_ctx, mem_aff, gs,
344 lowest_offset);
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200345}
346
Christoph Hellwig486acd42007-07-20 21:39:54 +0200347static struct spu *ctx_location(struct spu *ref, int offset, int node)
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200348{
349 struct spu *spu;
350
351 spu = NULL;
352 if (offset >= 0) {
353 list_for_each_entry(spu, ref->aff_list.prev, aff_list) {
Christoph Hellwig486acd42007-07-20 21:39:54 +0200354 BUG_ON(spu->node != node);
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200355 if (offset == 0)
356 break;
357 if (sched_spu(spu))
358 offset--;
359 }
360 } else {
361 list_for_each_entry_reverse(spu, ref->aff_list.next, aff_list) {
Christoph Hellwig486acd42007-07-20 21:39:54 +0200362 BUG_ON(spu->node != node);
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200363 if (offset == 0)
364 break;
365 if (sched_spu(spu))
366 offset++;
367 }
368 }
Christoph Hellwig486acd42007-07-20 21:39:54 +0200369
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200370 return spu;
371}
372
373/*
374 * affinity_check is called each time a context is going to be scheduled.
375 * It returns the spu ptr on which the context must run.
376 */
Christoph Hellwig486acd42007-07-20 21:39:54 +0200377static int has_affinity(struct spu_context *ctx)
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200378{
Christoph Hellwig486acd42007-07-20 21:39:54 +0200379 struct spu_gang *gang = ctx->gang;
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200380
381 if (list_empty(&ctx->aff_list))
Christoph Hellwig486acd42007-07-20 21:39:54 +0200382 return 0;
383
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200384 if (!gang->aff_ref_spu) {
385 if (!(gang->aff_flags & AFF_MERGED))
386 aff_merge_remaining_ctxs(gang);
387 if (!(gang->aff_flags & AFF_OFFSETS_SET))
388 aff_set_offsets(gang);
389 aff_set_ref_point_location(gang);
390 }
Christoph Hellwig486acd42007-07-20 21:39:54 +0200391
392 return gang->aff_ref_spu != NULL;
Arnd Bergmannc5fc8d22007-07-20 21:39:48 +0200393}
394
Christoph Hellwig202557d2007-02-13 21:36:49 +0100395/**
396 * spu_unbind_context - unbind spu context from physical spu
397 * @spu: physical spu to unbind from
398 * @ctx: context to unbind
Christoph Hellwig202557d2007-02-13 21:36:49 +0100399 */
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100400static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500401{
Mark Nuttera68cf982006-10-04 17:26:12 +0200402 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
403 spu->pid, spu->number, spu->node);
Andre Detsch27ec41d2007-07-20 21:39:33 +0200404 spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000405
Arnd Bergmannaa6d5b22007-07-20 21:39:44 +0200406 if (spu->ctx->flags & SPU_CREATE_NOSCHED)
407 atomic_dec(&cbe_spu_info[spu->node].reserved_spus);
Andre Detsch36ddbb12007-09-19 14:38:12 +1000408
409 if (ctx->gang){
410 mutex_lock(&ctx->gang->aff_mutex);
411 if (has_affinity(ctx)) {
412 if (atomic_dec_and_test(&ctx->gang->aff_sched_count))
413 ctx->gang->aff_ref_spu = NULL;
414 }
415 mutex_unlock(&ctx->gang->aff_mutex);
416 }
417
Arnd Bergmann86767272006-10-04 17:26:21 +0200418 spu_switch_notify(spu, NULL);
Arnd Bergmann51104592005-12-05 22:52:25 -0500419 spu_unmap_mappings(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500420 spu_save(&ctx->csa, spu);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500421 spu->timestamp = jiffies;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500422 ctx->state = SPU_STATE_SAVED;
423 spu->ibox_callback = NULL;
424 spu->wbox_callback = NULL;
Arnd Bergmann51104592005-12-05 22:52:25 -0500425 spu->stop_callback = NULL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +0100426 spu->mfc_callback = NULL;
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100427 spu_associate_mm(spu, NULL);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500428 spu->pid = 0;
Bob Nelson14748552007-07-20 21:39:53 +0200429 spu->tgid = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500430 ctx->ops = &spu_backing_ops;
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500431 spu->flags = 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500432 spu->ctx = NULL;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000433
434 ctx->stats.slb_flt +=
435 (spu->stats.slb_flt - ctx->stats.slb_flt_base);
436 ctx->stats.class2_intr +=
437 (spu->stats.class2_intr - ctx->stats.class2_intr_base);
Andre Detsch27ec41d2007-07-20 21:39:33 +0200438
439 /* This maps the underlying spu state to idle */
440 spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
441 ctx->spu = NULL;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500442}
443
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100444/**
445 * spu_add_to_rq - add a context to the runqueue
446 * @ctx: context to add
447 */
Luke Browning4e0f4ed2007-04-23 21:08:13 +0200448static void __spu_add_to_rq(struct spu_context *ctx)
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500449{
Christoph Hellwig27449972007-06-29 10:58:06 +1000450 /*
451 * Unfortunately this code path can be called from multiple threads
452 * on behalf of a single context due to the way the problem state
453 * mmap support works.
454 *
455 * Fortunately we need to wake up all these threads at the same time
456 * and can simply skip the runqueue addition for every but the first
457 * thread getting into this codepath.
458 *
459 * It's still quite hacky, and long-term we should proxy all other
460 * threads through the owner thread so that spu_run is in control
461 * of all the scheduling activity for a given context.
462 */
463 if (list_empty(&ctx->rq)) {
464 list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
465 set_bit(ctx->prio, spu_prio->bitmap);
466 if (!spu_prio->nr_waiting++)
467 __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
468 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200469}
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500470
Luke Browninge65c2f62007-12-20 16:39:59 +0900471static void spu_add_to_rq(struct spu_context *ctx)
472{
473 spin_lock(&spu_prio->runq_lock);
474 __spu_add_to_rq(ctx);
475 spin_unlock(&spu_prio->runq_lock);
476}
477
Luke Browning4e0f4ed2007-04-23 21:08:13 +0200478static void __spu_del_from_rq(struct spu_context *ctx)
Christoph Hellwiga475c2f2007-04-23 21:08:11 +0200479{
Luke Browning4e0f4ed2007-04-23 21:08:13 +0200480 int prio = ctx->prio;
481
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000482 if (!list_empty(&ctx->rq)) {
Christoph Hellwigc77239b2007-06-29 10:58:05 +1000483 if (!--spu_prio->nr_waiting)
484 del_timer(&spusched_timer);
Christoph Hellwiga475c2f2007-04-23 21:08:11 +0200485 list_del_init(&ctx->rq);
Christoph Hellwigc77239b2007-06-29 10:58:05 +1000486
487 if (list_empty(&spu_prio->runq[prio]))
488 clear_bit(prio, spu_prio->bitmap);
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000489 }
Mark Nuttera68cf982006-10-04 17:26:12 +0200490}
491
Luke Browninge65c2f62007-12-20 16:39:59 +0900492void spu_del_from_rq(struct spu_context *ctx)
493{
494 spin_lock(&spu_prio->runq_lock);
495 __spu_del_from_rq(ctx);
496 spin_unlock(&spu_prio->runq_lock);
497}
498
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100499static void spu_prio_wait(struct spu_context *ctx)
500{
Mark Nuttera68cf982006-10-04 17:26:12 +0200501 DEFINE_WAIT(wait);
502
Luke Browninge65c2f62007-12-20 16:39:59 +0900503 /*
504 * The caller must explicitly wait for a context to be loaded
505 * if the nosched flag is set. If NOSCHED is not set, the caller
506 * queues the context and waits for an spu event or error.
507 */
508 BUG_ON(!(ctx->flags & SPU_CREATE_NOSCHED));
509
Luke Browning4e0f4ed2007-04-23 21:08:13 +0200510 spin_lock(&spu_prio->runq_lock);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100511 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
Mark Nuttera68cf982006-10-04 17:26:12 +0200512 if (!signal_pending(current)) {
Luke Browning4e0f4ed2007-04-23 21:08:13 +0200513 __spu_add_to_rq(ctx);
514 spin_unlock(&spu_prio->runq_lock);
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100515 mutex_unlock(&ctx->state_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200516 schedule();
Christoph Hellwig650f8b02007-02-13 21:36:50 +0100517 mutex_lock(&ctx->state_mutex);
Luke Browning4e0f4ed2007-04-23 21:08:13 +0200518 spin_lock(&spu_prio->runq_lock);
519 __spu_del_from_rq(ctx);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500520 }
Luke Browning4e0f4ed2007-04-23 21:08:13 +0200521 spin_unlock(&spu_prio->runq_lock);
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100522 __set_current_state(TASK_RUNNING);
523 remove_wait_queue(&ctx->stop_wq, &wait);
Arnd Bergmann2a911f02005-12-05 22:52:26 -0500524}
525
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100526static struct spu *spu_get_idle(struct spu_context *ctx)
Mark Nuttera68cf982006-10-04 17:26:12 +0200527{
Andre Detsch36ddbb12007-09-19 14:38:12 +1000528 struct spu *spu, *aff_ref_spu;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200529 int node, n;
Mark Nuttera68cf982006-10-04 17:26:12 +0200530
Andre Detsch36ddbb12007-09-19 14:38:12 +1000531 if (ctx->gang) {
532 mutex_lock(&ctx->gang->aff_mutex);
533 if (has_affinity(ctx)) {
534 aff_ref_spu = ctx->gang->aff_ref_spu;
535 atomic_inc(&ctx->gang->aff_sched_count);
536 mutex_unlock(&ctx->gang->aff_mutex);
537 node = aff_ref_spu->node;
Arnd Bergmanncbc23d32007-07-20 21:39:49 +0200538
Andre Detsch36ddbb12007-09-19 14:38:12 +1000539 mutex_lock(&cbe_spu_info[node].list_mutex);
540 spu = ctx_location(aff_ref_spu, ctx->aff_offset, node);
541 if (spu && spu->alloc_state == SPU_FREE)
542 goto found;
543 mutex_unlock(&cbe_spu_info[node].list_mutex);
544
545 mutex_lock(&ctx->gang->aff_mutex);
546 if (atomic_dec_and_test(&ctx->gang->aff_sched_count))
547 ctx->gang->aff_ref_spu = NULL;
548 mutex_unlock(&ctx->gang->aff_mutex);
549
550 return NULL;
551 }
552 mutex_unlock(&ctx->gang->aff_mutex);
Christoph Hellwig486acd42007-07-20 21:39:54 +0200553 }
Christoph Hellwig486acd42007-07-20 21:39:54 +0200554 node = cpu_to_node(raw_smp_processor_id());
Mark Nuttera68cf982006-10-04 17:26:12 +0200555 for (n = 0; n < MAX_NUMNODES; n++, node++) {
556 node = (node < MAX_NUMNODES) ? node : 0;
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000557 if (!node_allowed(ctx, node))
Mark Nuttera68cf982006-10-04 17:26:12 +0200558 continue;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200559
560 mutex_lock(&cbe_spu_info[node].list_mutex);
561 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
562 if (spu->alloc_state == SPU_FREE)
563 goto found;
564 }
565 mutex_unlock(&cbe_spu_info[node].list_mutex);
Mark Nuttera68cf982006-10-04 17:26:12 +0200566 }
Christoph Hellwig486acd42007-07-20 21:39:54 +0200567
568 return NULL;
569
570 found:
571 spu->alloc_state = SPU_USED;
572 mutex_unlock(&cbe_spu_info[node].list_mutex);
573 pr_debug("Got SPU %d %d\n", spu->number, spu->node);
574 spu_init_channels(spu);
Mark Nuttera68cf982006-10-04 17:26:12 +0200575 return spu;
576}
577
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100578/**
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100579 * find_victim - find a lower priority context to preempt
580 * @ctx: canidate context for running
581 *
582 * Returns the freed physical spu to run the new context on.
583 */
584static struct spu *find_victim(struct spu_context *ctx)
585{
586 struct spu_context *victim = NULL;
587 struct spu *spu;
588 int node, n;
589
590 /*
591 * Look for a possible preemption candidate on the local node first.
592 * If there is no candidate look at the other nodes. This isn't
Julio M. Merino Vidal9b1d21f2007-12-20 16:39:59 +0900593 * exactly fair, but so far the whole spu scheduler tries to keep
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100594 * a strong node affinity. We might want to fine-tune this in
595 * the future.
596 */
597 restart:
598 node = cpu_to_node(raw_smp_processor_id());
599 for (n = 0; n < MAX_NUMNODES; n++, node++) {
600 node = (node < MAX_NUMNODES) ? node : 0;
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000601 if (!node_allowed(ctx, node))
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100602 continue;
603
Christoph Hellwig486acd42007-07-20 21:39:54 +0200604 mutex_lock(&cbe_spu_info[node].list_mutex);
605 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100606 struct spu_context *tmp = spu->ctx;
607
Christoph Hellwigc0e7b4a2007-09-19 14:38:12 +1000608 if (tmp && tmp->prio > ctx->prio &&
Luke Browninge65c2f62007-12-20 16:39:59 +0900609 !(tmp->flags & SPU_CREATE_NOSCHED) &&
Christoph Hellwigfe443ef2007-06-29 10:57:52 +1000610 (!victim || tmp->prio > victim->prio))
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100611 victim = spu->ctx;
612 }
Christoph Hellwig486acd42007-07-20 21:39:54 +0200613 mutex_unlock(&cbe_spu_info[node].list_mutex);
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100614
615 if (victim) {
616 /*
617 * This nests ctx->state_mutex, but we always lock
618 * higher priority contexts before lower priority
619 * ones, so this is safe until we introduce
620 * priority inheritance schemes.
Luke Browning91569532007-12-20 16:39:59 +0900621 *
622 * XXX if the highest priority context is locked,
623 * this can loop a long time. Might be better to
624 * look at another context or give up after X retries.
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100625 */
626 if (!mutex_trylock(&victim->state_mutex)) {
627 victim = NULL;
628 goto restart;
629 }
630
631 spu = victim->spu;
Luke Browningb1925412007-12-20 16:39:59 +0900632 if (!spu || victim->prio <= ctx->prio) {
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100633 /*
634 * This race can happen because we've dropped
Luke Browningb1925412007-12-20 16:39:59 +0900635 * the active list mutex. Not a problem, just
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100636 * restart the search.
637 */
638 mutex_unlock(&victim->state_mutex);
639 victim = NULL;
640 goto restart;
641 }
Christoph Hellwig486acd42007-07-20 21:39:54 +0200642
643 mutex_lock(&cbe_spu_info[node].list_mutex);
644 cbe_spu_info[node].nr_active--;
Christoph Hellwigc0e7b4a2007-09-19 14:38:12 +1000645 spu_unbind_context(spu, victim);
Christoph Hellwig486acd42007-07-20 21:39:54 +0200646 mutex_unlock(&cbe_spu_info[node].list_mutex);
647
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +1000648 victim->stats.invol_ctx_switch++;
Christoph Hellwigfe2f8962007-06-29 10:58:07 +1000649 spu->stats.invol_ctx_switch++;
Luke Browninge65c2f62007-12-20 16:39:59 +0900650 spu_add_to_rq(victim);
651
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100652 mutex_unlock(&victim->state_mutex);
Luke Browninge65c2f62007-12-20 16:39:59 +0900653
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100654 return spu;
655 }
656 }
657
658 return NULL;
659}
660
Luke Browninge65c2f62007-12-20 16:39:59 +0900661static void __spu_schedule(struct spu *spu, struct spu_context *ctx)
662{
663 int node = spu->node;
664 int success = 0;
665
666 spu_set_timeslice(ctx);
667
668 mutex_lock(&cbe_spu_info[node].list_mutex);
669 if (spu->ctx == NULL) {
670 spu_bind_context(spu, ctx);
671 cbe_spu_info[node].nr_active++;
672 spu->alloc_state = SPU_USED;
673 success = 1;
674 }
675 mutex_unlock(&cbe_spu_info[node].list_mutex);
676
677 if (success)
678 wake_up_all(&ctx->run_wq);
679 else
680 spu_add_to_rq(ctx);
681}
682
683static void spu_schedule(struct spu *spu, struct spu_context *ctx)
684{
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900685 /* not a candidate for interruptible because it's called either
686 from the scheduler thread or from spu_deactivate */
687 mutex_lock(&ctx->state_mutex);
Luke Browninge65c2f62007-12-20 16:39:59 +0900688 __spu_schedule(spu, ctx);
689 spu_release(ctx);
690}
691
692static void spu_unschedule(struct spu *spu, struct spu_context *ctx)
693{
694 int node = spu->node;
695
696 mutex_lock(&cbe_spu_info[node].list_mutex);
697 cbe_spu_info[node].nr_active--;
698 spu->alloc_state = SPU_FREE;
699 spu_unbind_context(spu, ctx);
700 ctx->stats.invol_ctx_switch++;
701 spu->stats.invol_ctx_switch++;
702 mutex_unlock(&cbe_spu_info[node].list_mutex);
703}
704
Christoph Hellwig52f04fc2007-02-13 21:54:27 +0100705/**
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100706 * spu_activate - find a free spu for a context and execute it
707 * @ctx: spu context to schedule
708 * @flags: flags (currently ignored)
709 *
Christoph Hellwig08873092007-04-23 21:08:06 +0200710 * Tries to find a free spu to run @ctx. If no free spu is available
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100711 * add the context to the runqueue so it gets woken up once an spu
712 * is available.
713 */
Christoph Hellwig26bec672007-02-13 21:54:24 +0100714int spu_activate(struct spu_context *ctx, unsigned long flags)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500715{
Luke Browninge65c2f62007-12-20 16:39:59 +0900716 struct spu *spu;
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100717
Luke Browninge65c2f62007-12-20 16:39:59 +0900718 /*
719 * If there are multiple threads waiting for a single context
720 * only one actually binds the context while the others will
721 * only be able to acquire the state_mutex once the context
722 * already is in runnable state.
723 */
724 if (ctx->spu)
725 return 0;
Christoph Hellwig27449972007-06-29 10:58:06 +1000726
Luke Browninge65c2f62007-12-20 16:39:59 +0900727spu_activate_top:
728 if (signal_pending(current))
729 return -ERESTARTSYS;
Christoph Hellwig486acd42007-07-20 21:39:54 +0200730
Luke Browninge65c2f62007-12-20 16:39:59 +0900731 spu = spu_get_idle(ctx);
732 /*
733 * If this is a realtime thread we try to get it running by
734 * preempting a lower priority thread.
735 */
736 if (!spu && rt_prio(ctx->prio))
737 spu = find_victim(ctx);
738 if (spu) {
739 unsigned long runcntl;
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100740
Luke Browninge65c2f62007-12-20 16:39:59 +0900741 runcntl = ctx->ops->runcntl_read(ctx);
742 __spu_schedule(spu, ctx);
743 if (runcntl & SPU_RUNCNTL_RUNNABLE)
744 spuctx_switch_state(ctx, SPU_UTIL_USER);
745
746 return 0;
747 }
748
749 if (ctx->flags & SPU_CREATE_NOSCHED) {
Christoph Hellwig50b520d2007-03-10 00:05:36 +0100750 spu_prio_wait(ctx);
Luke Browninge65c2f62007-12-20 16:39:59 +0900751 goto spu_activate_top;
752 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +0100753
Luke Browninge65c2f62007-12-20 16:39:59 +0900754 spu_add_to_rq(ctx);
755
756 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500757}
758
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100759/**
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000760 * grab_runnable_context - try to find a runnable context
761 *
762 * Remove the highest priority context on the runqueue and return it
763 * to the caller. Returns %NULL if no runnable context was found.
764 */
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000765static struct spu_context *grab_runnable_context(int prio, int node)
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000766{
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000767 struct spu_context *ctx;
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000768 int best;
769
770 spin_lock(&spu_prio->runq_lock);
Masato Noguchi7e90b742007-07-20 21:39:43 +0200771 best = find_first_bit(spu_prio->bitmap, prio);
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000772 while (best < prio) {
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000773 struct list_head *rq = &spu_prio->runq[best];
774
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000775 list_for_each_entry(ctx, rq, rq) {
776 /* XXX(hch): check for affinity here aswell */
777 if (__node_allowed(ctx, node)) {
778 __spu_del_from_rq(ctx);
779 goto found;
780 }
781 }
782 best++;
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000783 }
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000784 ctx = NULL;
785 found:
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000786 spin_unlock(&spu_prio->runq_lock);
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000787 return ctx;
788}
789
790static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)
791{
792 struct spu *spu = ctx->spu;
793 struct spu_context *new = NULL;
794
795 if (spu) {
Christoph Hellwigea1ae592007-06-29 10:57:56 +1000796 new = grab_runnable_context(max_prio, spu->node);
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000797 if (new || force) {
Luke Browninge65c2f62007-12-20 16:39:59 +0900798 spu_unschedule(spu, ctx);
799 if (new) {
800 if (new->flags & SPU_CREATE_NOSCHED)
801 wake_up(&new->stop_wq);
802 else {
803 spu_release(ctx);
804 spu_schedule(spu, new);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900805 /* this one can't easily be made
806 interruptible */
807 mutex_lock(&ctx->state_mutex);
Luke Browninge65c2f62007-12-20 16:39:59 +0900808 }
809 }
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000810 }
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000811 }
812
813 return new != NULL;
814}
815
816/**
Christoph Hellwig678b2ff2007-02-13 21:54:25 +0100817 * spu_deactivate - unbind a context from it's physical spu
818 * @ctx: spu context to unbind
819 *
820 * Unbind @ctx from the physical spu it is running on and schedule
821 * the highest priority context to run on the freed physical spu.
822 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500823void spu_deactivate(struct spu_context *ctx)
824{
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000825 __spu_deactivate(ctx, 1, MAX_PRIO);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500826}
827
Christoph Hellwigae7b4c52007-02-13 21:54:26 +0100828/**
Bob Nelson14748552007-07-20 21:39:53 +0200829 * spu_yield - yield a physical spu if others are waiting
Christoph Hellwigae7b4c52007-02-13 21:54:26 +0100830 * @ctx: spu context to yield
831 *
832 * Check if there is a higher priority context waiting and if yes
833 * unbind @ctx from the physical spu and schedule the highest
834 * priority context to run on the freed physical spu instead.
835 */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500836void spu_yield(struct spu_context *ctx)
837{
Christoph Hellwige5c0b9e2007-06-05 11:25:59 +1000838 if (!(ctx->flags & SPU_CREATE_NOSCHED)) {
839 mutex_lock(&ctx->state_mutex);
Andre Detsch27ec41d2007-07-20 21:39:33 +0200840 __spu_deactivate(ctx, 0, MAX_PRIO);
Christoph Hellwige5c0b9e2007-06-05 11:25:59 +1000841 mutex_unlock(&ctx->state_mutex);
842 }
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000843}
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500844
Christoph Hellwig486acd42007-07-20 21:39:54 +0200845static noinline void spusched_tick(struct spu_context *ctx)
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000846{
Luke Browninge65c2f62007-12-20 16:39:59 +0900847 struct spu_context *new = NULL;
848 struct spu *spu = NULL;
849 u32 status;
850
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900851 if (spu_acquire(ctx))
852 BUG(); /* a kernel thread never has signals pending */
Luke Browninge65c2f62007-12-20 16:39:59 +0900853
854 if (ctx->state != SPU_STATE_RUNNABLE)
855 goto out;
856 if (spu_stopped(ctx, &status))
857 goto out;
Christoph Hellwigdf09cf32007-06-29 10:57:58 +1000858 if (ctx->flags & SPU_CREATE_NOSCHED)
Luke Browninge65c2f62007-12-20 16:39:59 +0900859 goto out;
Christoph Hellwigdf09cf32007-06-29 10:57:58 +1000860 if (ctx->policy == SCHED_FIFO)
Luke Browninge65c2f62007-12-20 16:39:59 +0900861 goto out;
Christoph Hellwigdf09cf32007-06-29 10:57:58 +1000862
863 if (--ctx->time_slice)
Luke Browninge65c2f62007-12-20 16:39:59 +0900864 goto out;
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000865
Luke Browninge65c2f62007-12-20 16:39:59 +0900866 spu = ctx->spu;
867 new = grab_runnable_context(ctx->prio + 1, spu->node);
868 if (new) {
869 spu_unschedule(spu, ctx);
870 spu_add_to_rq(ctx);
Christoph Hellwigbb5db292007-06-04 23:26:51 +1000871 } else {
Christoph Hellwig37901802007-06-29 10:57:51 +1000872 ctx->time_slice++;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500873 }
Luke Browninge65c2f62007-12-20 16:39:59 +0900874out:
875 spu_release(ctx);
876
877 if (new)
878 spu_schedule(spu, new);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500879}
880
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000881/**
882 * count_active_contexts - count nr of active tasks
883 *
884 * Return the number of tasks currently running or waiting to run.
885 *
Christoph Hellwig486acd42007-07-20 21:39:54 +0200886 * Note that we don't take runq_lock / list_mutex here. Reading
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000887 * a single 32bit value is atomic on powerpc, and we don't care
888 * about memory ordering issues here.
889 */
890static unsigned long count_active_contexts(void)
891{
892 int nr_active = 0, node;
893
894 for (node = 0; node < MAX_NUMNODES; node++)
Christoph Hellwig486acd42007-07-20 21:39:54 +0200895 nr_active += cbe_spu_info[node].nr_active;
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000896 nr_active += spu_prio->nr_waiting;
897
898 return nr_active;
899}
900
901/**
Aegis Lin90608a22007-12-20 16:39:59 +0900902 * spu_calc_load - update the avenrun load estimates.
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000903 *
904 * No locking against reading these values from userspace, as for
905 * the CPU loadavg code.
906 */
Aegis Lin90608a22007-12-20 16:39:59 +0900907static void spu_calc_load(void)
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000908{
909 unsigned long active_tasks; /* fixed-point */
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000910
Aegis Lin90608a22007-12-20 16:39:59 +0900911 active_tasks = count_active_contexts() * FIXED_1;
912 CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks);
913 CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks);
914 CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks);
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000915}
916
Christoph Hellwig37901802007-06-29 10:57:51 +1000917static void spusched_wake(unsigned long data)
918{
919 mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
920 wake_up_process(spusched_task);
Aegis Lin90608a22007-12-20 16:39:59 +0900921}
922
923static void spuloadavg_wake(unsigned long data)
924{
925 mod_timer(&spuloadavg_timer, jiffies + LOAD_FREQ);
926 spu_calc_load();
Christoph Hellwig37901802007-06-29 10:57:51 +1000927}
928
929static int spusched_thread(void *unused)
930{
Christoph Hellwig486acd42007-07-20 21:39:54 +0200931 struct spu *spu;
Christoph Hellwig37901802007-06-29 10:57:51 +1000932 int node;
933
Christoph Hellwig37901802007-06-29 10:57:51 +1000934 while (!kthread_should_stop()) {
935 set_current_state(TASK_INTERRUPTIBLE);
936 schedule();
937 for (node = 0; node < MAX_NUMNODES; node++) {
Luke Browninge65c2f62007-12-20 16:39:59 +0900938 struct mutex *mtx = &cbe_spu_info[node].list_mutex;
939
940 mutex_lock(mtx);
941 list_for_each_entry(spu, &cbe_spu_info[node].spus,
942 cbe_list) {
943 struct spu_context *ctx = spu->ctx;
944
945 if (ctx) {
946 mutex_unlock(mtx);
947 spusched_tick(ctx);
948 mutex_lock(mtx);
949 }
950 }
951 mutex_unlock(mtx);
Christoph Hellwig37901802007-06-29 10:57:51 +1000952 }
953 }
954
Christoph Hellwig37901802007-06-29 10:57:51 +1000955 return 0;
956}
957
Jeremy Kerr7cd58e42007-12-20 16:39:59 +0900958void spuctx_switch_state(struct spu_context *ctx,
959 enum spu_utilization_state new_state)
960{
961 unsigned long long curtime;
962 signed long long delta;
963 struct timespec ts;
964 struct spu *spu;
965 enum spu_utilization_state old_state;
966
967 ktime_get_ts(&ts);
968 curtime = timespec_to_ns(&ts);
969 delta = curtime - ctx->stats.tstamp;
970
971 WARN_ON(!mutex_is_locked(&ctx->state_mutex));
972 WARN_ON(delta < 0);
973
974 spu = ctx->spu;
975 old_state = ctx->stats.util_state;
976 ctx->stats.util_state = new_state;
977 ctx->stats.tstamp = curtime;
978
979 /*
980 * Update the physical SPU utilization statistics.
981 */
982 if (spu) {
983 ctx->stats.times[old_state] += delta;
984 spu->stats.times[old_state] += delta;
985 spu->stats.util_state = new_state;
986 spu->stats.tstamp = curtime;
987 }
988}
989
Christoph Hellwig65de66f2007-06-29 10:58:02 +1000990#define LOAD_INT(x) ((x) >> FSHIFT)
991#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
992
993static int show_spu_loadavg(struct seq_file *s, void *private)
994{
995 int a, b, c;
996
997 a = spu_avenrun[0] + (FIXED_1/200);
998 b = spu_avenrun[1] + (FIXED_1/200);
999 c = spu_avenrun[2] + (FIXED_1/200);
1000
1001 /*
1002 * Note that last_pid doesn't really make much sense for the
Julio M. Merino Vidal9b1d21f2007-12-20 16:39:59 +09001003 * SPU loadavg (it even seems very odd on the CPU side...),
Christoph Hellwig65de66f2007-06-29 10:58:02 +10001004 * but we include it here to have a 100% compatible interface.
1005 */
1006 seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
1007 LOAD_INT(a), LOAD_FRAC(a),
1008 LOAD_INT(b), LOAD_FRAC(b),
1009 LOAD_INT(c), LOAD_FRAC(c),
1010 count_active_contexts(),
1011 atomic_read(&nr_spu_contexts),
1012 current->nsproxy->pid_ns->last_pid);
1013 return 0;
1014}
1015
1016static int spu_loadavg_open(struct inode *inode, struct file *file)
1017{
1018 return single_open(file, show_spu_loadavg, NULL);
1019}
1020
1021static const struct file_operations spu_loadavg_fops = {
1022 .open = spu_loadavg_open,
1023 .read = seq_read,
1024 .llseek = seq_lseek,
1025 .release = single_release,
1026};
1027
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001028int __init spu_sched_init(void)
1029{
Christoph Hellwig65de66f2007-06-29 10:58:02 +10001030 struct proc_dir_entry *entry;
1031 int err = -ENOMEM, i;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001032
Mark Nuttera68cf982006-10-04 17:26:12 +02001033 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
Christoph Hellwig37901802007-06-29 10:57:51 +10001034 if (!spu_prio)
Christoph Hellwig65de66f2007-06-29 10:58:02 +10001035 goto out;
Christoph Hellwig37901802007-06-29 10:57:51 +10001036
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001037 for (i = 0; i < MAX_PRIO; i++) {
Christoph Hellwig079cdb62007-02-13 21:54:23 +01001038 INIT_LIST_HEAD(&spu_prio->runq[i]);
Mark Nuttera68cf982006-10-04 17:26:12 +02001039 __clear_bit(i, spu_prio->bitmap);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001040 }
Christoph Hellwig079cdb62007-02-13 21:54:23 +01001041 spin_lock_init(&spu_prio->runq_lock);
Christoph Hellwig37901802007-06-29 10:57:51 +10001042
Christoph Hellwigc77239b2007-06-29 10:58:05 +10001043 setup_timer(&spusched_timer, spusched_wake, 0);
Aegis Lin90608a22007-12-20 16:39:59 +09001044 setup_timer(&spuloadavg_timer, spuloadavg_wake, 0);
Christoph Hellwigc77239b2007-06-29 10:58:05 +10001045
Christoph Hellwig37901802007-06-29 10:57:51 +10001046 spusched_task = kthread_run(spusched_thread, NULL, "spusched");
1047 if (IS_ERR(spusched_task)) {
Christoph Hellwig65de66f2007-06-29 10:58:02 +10001048 err = PTR_ERR(spusched_task);
1049 goto out_free_spu_prio;
Christoph Hellwig37901802007-06-29 10:57:51 +10001050 }
Jeremy Kerrf3f59be2007-06-29 10:57:54 +10001051
Aegis Lin90608a22007-12-20 16:39:59 +09001052 mod_timer(&spuloadavg_timer, 0);
1053
Christoph Hellwig65de66f2007-06-29 10:58:02 +10001054 entry = create_proc_entry("spu_loadavg", 0, NULL);
1055 if (!entry)
1056 goto out_stop_kthread;
1057 entry->proc_fops = &spu_loadavg_fops;
1058
Jeremy Kerrf3f59be2007-06-29 10:57:54 +10001059 pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",
1060 SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001061 return 0;
Christoph Hellwig37901802007-06-29 10:57:51 +10001062
Christoph Hellwig65de66f2007-06-29 10:58:02 +10001063 out_stop_kthread:
1064 kthread_stop(spusched_task);
1065 out_free_spu_prio:
1066 kfree(spu_prio);
1067 out:
1068 return err;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001069}
1070
Sebastian Siewiord1450312007-07-20 21:39:29 +02001071void spu_sched_exit(void)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001072{
Christoph Hellwig486acd42007-07-20 21:39:54 +02001073 struct spu *spu;
Mark Nuttera68cf982006-10-04 17:26:12 +02001074 int node;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001075
Christoph Hellwig65de66f2007-06-29 10:58:02 +10001076 remove_proc_entry("spu_loadavg", NULL);
1077
Christoph Hellwigc77239b2007-06-29 10:58:05 +10001078 del_timer_sync(&spusched_timer);
Aegis Lin90608a22007-12-20 16:39:59 +09001079 del_timer_sync(&spuloadavg_timer);
Christoph Hellwig37901802007-06-29 10:57:51 +10001080 kthread_stop(spusched_task);
1081
Mark Nuttera68cf982006-10-04 17:26:12 +02001082 for (node = 0; node < MAX_NUMNODES; node++) {
Christoph Hellwig486acd42007-07-20 21:39:54 +02001083 mutex_lock(&cbe_spu_info[node].list_mutex);
1084 list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list)
1085 if (spu->alloc_state != SPU_FREE)
1086 spu->alloc_state = SPU_FREE;
1087 mutex_unlock(&cbe_spu_info[node].list_mutex);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001088 }
Mark Nuttera68cf982006-10-04 17:26:12 +02001089 kfree(spu_prio);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001090}