]> err.no Git - linux-2.6/blobdiff - arch/powerpc/platforms/cell/spufs/sched.c
Pull esi-support into release branch
[linux-2.6] / arch / powerpc / platforms / cell / spufs / sched.c
index c0d9d83a9ac3f3b17bd7aac50aa9794a15b67c9b..1350294484b62e191a131daa99dbd6f8de51aab2 100644 (file)
@@ -24,8 +24,8 @@
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#define DEBUG 1
-#include <linux/config.h>
+#undef DEBUG
+
 #include <linux/module.h>
 #include <linux/errno.h>
 #include <linux/sched.h>
 #include <asm/mmu_context.h>
 #include <asm/spu.h>
 #include <asm/spu_csa.h>
+#include <asm/spu_priv1.h>
 #include "spufs.h"
 
+#define SPU_MIN_TIMESLICE      (100 * HZ / 1000)
+
 #define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1)
 struct spu_prio_array {
        atomic_t nr_blocked;
@@ -118,7 +121,8 @@ static void prio_wakeup(struct spu_runqueue *rq)
        }
 }
 
-static void prio_wait(struct spu_runqueue *rq, u64 flags)
+static void prio_wait(struct spu_runqueue *rq, struct spu_context *ctx,
+                     u64 flags)
 {
        int prio = current->prio;
        wait_queue_head_t *wq = &rq->prio.waitq[prio];
@@ -129,9 +133,11 @@ static void prio_wait(struct spu_runqueue *rq, u64 flags)
        prepare_to_wait_exclusive(wq, &wait, TASK_INTERRUPTIBLE);
        if (!signal_pending(current)) {
                up(&rq->sem);
+               up_write(&ctx->state_sema);
                pr_debug("%s: pid=%d prio=%d\n", __FUNCTION__,
                         current->pid, current->prio);
                schedule();
+               down_write(&ctx->state_sema);
                down(&rq->sem);
        }
        finish_wait(wq, &wait);
@@ -164,6 +170,7 @@ static inline void bind_context(struct spu *spu, struct spu_context *ctx)
                 spu->number);
        spu->ctx = ctx;
        spu->flags = 0;
+       ctx->flags = 0;
        ctx->spu = spu;
        ctx->ops = &spu_hw_ops;
        spu->pid = current->pid;
@@ -172,60 +179,90 @@ static inline void bind_context(struct spu *spu, struct spu_context *ctx)
        mm_needs_global_tlbie(spu->mm);
        spu->ibox_callback = spufs_ibox_callback;
        spu->wbox_callback = spufs_wbox_callback;
+       spu->stop_callback = spufs_stop_callback;
+       spu->mfc_callback = spufs_mfc_callback;
        mb();
+       spu_unmap_mappings(ctx);
        spu_restore(&ctx->csa, spu);
+       spu->timestamp = jiffies;
 }
 
 static inline void unbind_context(struct spu *spu, struct spu_context *ctx)
 {
        pr_debug("%s: unbind pid=%d SPU=%d\n", __FUNCTION__,
                 spu->pid, spu->number);
+       spu_unmap_mappings(ctx);
        spu_save(&ctx->csa, spu);
+       spu->timestamp = jiffies;
        ctx->state = SPU_STATE_SAVED;
        spu->ibox_callback = NULL;
        spu->wbox_callback = NULL;
+       spu->stop_callback = NULL;
+       spu->mfc_callback = NULL;
        spu->mm = NULL;
        spu->pid = 0;
        spu->prio = MAX_PRIO;
        ctx->ops = &spu_backing_ops;
        ctx->spu = NULL;
+       ctx->flags = 0;
+       spu->flags = 0;
        spu->ctx = NULL;
 }
 
-static struct spu *preempt_active(struct spu_runqueue *rq)
+static void spu_reaper(void *data)
 {
-       struct list_head *p;
-       struct spu_context *ctx;
+       struct spu_context *ctx = data;
        struct spu *spu;
 
-       /* Future: implement real preemption.  For now just
-        * boot a lower priority ctx that is in "detached"
-        * state, i.e. on a processor but not currently in
-        * spu_run().
-        */
+       down_write(&ctx->state_sema);
+       spu = ctx->spu;
+       if (spu && test_bit(SPU_CONTEXT_PREEMPT, &ctx->flags)) {
+               if (atomic_read(&spu->rq->prio.nr_blocked)) {
+                       pr_debug("%s: spu=%d\n", __func__, spu->number);
+                       ctx->ops->runcntl_stop(ctx);
+                       spu_deactivate(ctx);
+                       wake_up_all(&ctx->stop_wq);
+               } else {
+                       clear_bit(SPU_CONTEXT_PREEMPT, &ctx->flags);
+               }
+       }
+       up_write(&ctx->state_sema);
+       put_spu_context(ctx);
+}
+
+static void schedule_spu_reaper(struct spu_runqueue *rq, struct spu *spu)
+{
+       struct spu_context *ctx = get_spu_context(spu->ctx);
+       unsigned long now = jiffies;
+       unsigned long expire = spu->timestamp + SPU_MIN_TIMESLICE;
+
+       set_bit(SPU_CONTEXT_PREEMPT, &ctx->flags);
+       INIT_WORK(&ctx->reap_work, spu_reaper, ctx);
+       if (time_after(now, expire))
+               schedule_work(&ctx->reap_work);
+       else
+               schedule_delayed_work(&ctx->reap_work, expire - now);
+}
+
+static void check_preempt_active(struct spu_runqueue *rq)
+{
+       struct list_head *p;
+       struct spu *worst = NULL;
+
        list_for_each(p, &rq->active_list) {
-               spu = list_entry(p, struct spu, sched_list);
-               if (current->prio < spu->prio) {
-                       ctx = spu->ctx;
-                       if (down_write_trylock(&ctx->state_sema)) {
-                               if (ctx->state != SPU_STATE_RUNNABLE) {
-                                       up_write(&ctx->state_sema);
-                                       continue;
-                               }
-                               pr_debug("%s: booting pid=%d from SPU %d\n",
-                                        __FUNCTION__, spu->pid, spu->number);
-                               del_active(rq, spu);
-                               up(&rq->sem);
-                               unbind_context(spu, ctx);
-                               up_write(&ctx->state_sema);
-                               return spu;
+               struct spu *spu = list_entry(p, struct spu, sched_list);
+               struct spu_context *ctx = spu->ctx;
+               if (!test_bit(SPU_CONTEXT_PREEMPT, &ctx->flags)) {
+                       if (!worst || (spu->prio > worst->prio)) {
+                               worst = spu;
                        }
                }
        }
-       return NULL;
+       if (worst && (current->prio < worst->prio))
+               schedule_spu_reaper(rq, worst);
 }
 
-static struct spu *get_idle_spu(u64 flags)
+static struct spu *get_idle_spu(struct spu_context *ctx, u64 flags)
 {
        struct spu_runqueue *rq;
        struct spu *spu = NULL;
@@ -250,11 +287,8 @@ static struct spu *get_idle_spu(u64 flags)
                                continue;
                        }
                } else {
-                       if (is_best_prio(rq)) {
-                               if ((spu = preempt_active(rq)) != NULL)
-                                       return spu;
-                       }
-                       prio_wait(rq, flags);
+                       check_preempt_active(rq);
+                       prio_wait(rq, ctx, flags);
                        if (signal_pending(current)) {
                                prio_wakeup(rq);
                                spu = NULL;
@@ -321,10 +355,15 @@ int spu_activate(struct spu_context *ctx, u64 flags)
 
        if (ctx->spu)
                return 0;
-       spu = get_idle_spu(flags);
+       spu = get_idle_spu(ctx, flags);
        if (!spu)
                return (signal_pending(current)) ? -ERESTARTSYS : -EAGAIN;
        bind_context(spu, ctx);
+       /*
+        * We're likely to wait for interrupts on the same
+        * CPU that we are now on, so send them here.
+        */
+       spu_cpu_affinity_set(spu, raw_smp_processor_id());
        put_active_spu(spu);
        return 0;
 }
@@ -346,17 +385,21 @@ void spu_deactivate(struct spu_context *ctx)
 void spu_yield(struct spu_context *ctx)
 {
        struct spu *spu;
+       int need_yield = 0;
 
-       if (!down_write_trylock(&ctx->state_sema))
-               return;
+       down_write(&ctx->state_sema);
        spu = ctx->spu;
-       if ((ctx->state == SPU_STATE_RUNNABLE) &&
-           (sched_find_first_bit(spu->rq->prio.bitmap) <= current->prio)) {
+       if (spu && (sched_find_first_bit(spu->rq->prio.bitmap) < MAX_PRIO)) {
                pr_debug("%s: yielding SPU %d\n", __FUNCTION__, spu->number);
                spu_deactivate(ctx);
                ctx->state = SPU_STATE_SAVED;
+               need_yield = 1;
+       } else if (spu) {
+               spu->prio = MAX_PRIO;
        }
        up_write(&ctx->state_sema);
+       if (unlikely(need_yield))
+               yield();
 }
 
 int __init spu_sched_init(void)
@@ -391,6 +434,7 @@ int __init spu_sched_init(void)
                pr_debug("%s: adding SPU[%d]\n", __FUNCTION__, spu->number);
                add_idle(rq, spu);
                spu->rq = rq;
+               spu->timestamp = jiffies;
        }
        if (!rq->nr_idle) {
                printk(KERN_WARNING "%s: No available SPUs.\n", __FUNCTION__);