]> err.no Git - linux-2.6/blobdiff - arch/powerpc/platforms/cell/spufs/sched.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[linux-2.6] / arch / powerpc / platforms / cell / spufs / sched.c
index 814f65e025f1c22799ca3c09feae61318c9790a1..91030b8abdca6fbc62ca05efad87367f102c6885 100644 (file)
 #include <asm/spu_priv1.h>
 #include "spufs.h"
 
-#define SPU_MIN_TIMESLICE      (100 * HZ / 1000)
+#define SPU_TIMESLICE  (HZ)
 
-#define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1)
 struct spu_prio_array {
-       unsigned long bitmap[SPU_BITMAP_SIZE];
+       DECLARE_BITMAP(bitmap, MAX_PRIO);
        struct list_head runq[MAX_PRIO];
        spinlock_t runq_lock;
        struct list_head active_list[MAX_NUMNODES];
@@ -56,6 +55,7 @@ struct spu_prio_array {
 };
 
 static struct spu_prio_array *spu_prio;
+static struct workqueue_struct *spu_sched_wq;
 
 static inline int node_allowed(int node)
 {
@@ -69,6 +69,68 @@ static inline int node_allowed(int node)
        return 1;
 }
 
+void spu_start_tick(struct spu_context *ctx)
+{
+       if (ctx->policy == SCHED_RR) {
+               /*
+                * Make sure the exiting bit is cleared.
+                */
+               clear_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
+               mb();
+               queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE);
+       }
+}
+
+void spu_stop_tick(struct spu_context *ctx)
+{
+       if (ctx->policy == SCHED_RR) {
+               /*
+                * While the work can be rearming normally setting this flag
+                * makes sure it does not rearm itself anymore.
+                */
+               set_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
+               mb();
+               cancel_delayed_work(&ctx->sched_work);
+       }
+}
+
+void spu_sched_tick(struct work_struct *work)
+{
+       struct spu_context *ctx =
+               container_of(work, struct spu_context, sched_work.work);
+       struct spu *spu;
+       int preempted = 0;
+
+       /*
+        * If this context is being stopped avoid rescheduling from the
+        * scheduler tick because we would block on the state_mutex.
+        * The caller will yield the spu later on anyway.
+        */
+       if (test_bit(SPU_SCHED_EXITING, &ctx->sched_flags))
+               return;
+
+       mutex_lock(&ctx->state_mutex);
+       spu = ctx->spu;
+       if (spu) {
+               int best = sched_find_first_bit(spu_prio->bitmap);
+               if (best <= ctx->prio) {
+                       spu_deactivate(ctx);
+                       preempted = 1;
+               }
+       }
+       mutex_unlock(&ctx->state_mutex);
+
+       if (preempted) {
+               /*
+                * We need to break out of the wait loop in spu_run manually
+                * to ensure this context gets put on the runqueue again
+                * ASAP.
+                */
+               wake_up(&ctx->stop_wq);
+       } else
+               spu_start_tick(ctx);
+}
+
 /**
  * spu_add_to_active_list - add spu to active list
  * @spu:       spu to add to the active list
@@ -93,14 +155,6 @@ static void spu_remove_from_active_list(struct spu *spu)
        mutex_unlock(&spu_prio->active_mutex[node]);
 }
 
-static inline void mm_needs_global_tlbie(struct mm_struct *mm)
-{
-       int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
-
-       /* Global TLBIE broadcast required with SPEs. */
-       __cpus_setall(&mm->cpu_vm_mask, nr);
-}
-
 static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
 
 static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
@@ -133,8 +187,7 @@ static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
        ctx->spu = spu;
        ctx->ops = &spu_hw_ops;
        spu->pid = current->pid;
-       spu->mm = ctx->owner;
-       mm_needs_global_tlbie(spu->mm);
+       spu_associate_mm(spu, ctx->owner);
        spu->ibox_callback = spufs_ibox_callback;
        spu->wbox_callback = spufs_wbox_callback;
        spu->stop_callback = spufs_stop_callback;
@@ -171,7 +224,7 @@ static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
        spu->stop_callback = NULL;
        spu->mfc_callback = NULL;
        spu->dma_callback = NULL;
-       spu->mm = NULL;
+       spu_associate_mm(spu, NULL);
        spu->pid = 0;
        ctx->ops = &spu_backing_ops;
        ctx->spu = NULL;
@@ -183,62 +236,42 @@ static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
  * spu_add_to_rq - add a context to the runqueue
  * @ctx:       context to add
  */
-static void spu_add_to_rq(struct spu_context *ctx)
+static void __spu_add_to_rq(struct spu_context *ctx)
 {
-       spin_lock(&spu_prio->runq_lock);
-       list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
-       set_bit(ctx->prio, spu_prio->bitmap);
-       spin_unlock(&spu_prio->runq_lock);
-}
+       int prio = ctx->prio;
 
-/**
- * spu_del_from_rq - remove a context from the runqueue
- * @ctx:       context to remove
- */
-static void spu_del_from_rq(struct spu_context *ctx)
-{
-       spin_lock(&spu_prio->runq_lock);
-       list_del_init(&ctx->rq);
-       if (list_empty(&spu_prio->runq[ctx->prio]))
-               clear_bit(ctx->prio, spu_prio->bitmap);
-       spin_unlock(&spu_prio->runq_lock);
+       list_add_tail(&ctx->rq, &spu_prio->runq[prio]);
+       set_bit(prio, spu_prio->bitmap);
 }
 
-/**
- * spu_grab_context - remove one context from the runqueue
- * @prio:      priority of the context to be removed
- *
- * This function removes one context from the runqueue for priority @prio.
- * If there is more than one context with the given priority the first
- * task on the runqueue will be taken.
- *
- * Returns the spu_context it just removed.
- *
- * Must be called with spu_prio->runq_lock held.
- */
-static struct spu_context *spu_grab_context(int prio)
+static void __spu_del_from_rq(struct spu_context *ctx)
 {
-       struct list_head *rq = &spu_prio->runq[prio];
+       int prio = ctx->prio;
 
-       if (list_empty(rq))
-               return NULL;
-       return list_entry(rq->next, struct spu_context, rq);
+       if (!list_empty(&ctx->rq))
+               list_del_init(&ctx->rq);
+       if (list_empty(&spu_prio->runq[prio]))
+               clear_bit(prio, spu_prio->bitmap);
 }
 
 static void spu_prio_wait(struct spu_context *ctx)
 {
        DEFINE_WAIT(wait);
 
-       set_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
+       spin_lock(&spu_prio->runq_lock);
        prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
        if (!signal_pending(current)) {
+               __spu_add_to_rq(ctx);
+               spin_unlock(&spu_prio->runq_lock);
                mutex_unlock(&ctx->state_mutex);
                schedule();
                mutex_lock(&ctx->state_mutex);
+               spin_lock(&spu_prio->runq_lock);
+               __spu_del_from_rq(ctx);
        }
+       spin_unlock(&spu_prio->runq_lock);
        __set_current_state(TASK_RUNNING);
        remove_wait_queue(&ctx->stop_wq, &wait);
-       clear_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
 }
 
 /**
@@ -257,9 +290,14 @@ static void spu_reschedule(struct spu *spu)
        spin_lock(&spu_prio->runq_lock);
        best = sched_find_first_bit(spu_prio->bitmap);
        if (best < MAX_PRIO) {
-               struct spu_context *ctx = spu_grab_context(best);
-               if (ctx && test_bit(SPU_SCHED_WAKE, &ctx->sched_flags))
-                       wake_up(&ctx->stop_wq);
+               struct list_head *rq = &spu_prio->runq[best];
+               struct spu_context *ctx;
+
+               BUG_ON(list_empty(rq));
+
+               ctx = list_entry(rq->next, struct spu_context, rq);
+               __spu_del_from_rq(ctx);
+               wake_up(&ctx->stop_wq);
        }
        spin_unlock(&spu_prio->runq_lock);
 }
@@ -342,6 +380,12 @@ static struct spu *find_victim(struct spu_context *ctx)
                        }
                        spu_unbind_context(spu, victim);
                        mutex_unlock(&victim->state_mutex);
+                       /*
+                        * We need to break out of the wait loop in spu_run
+                        * manually to ensure this context gets put on the
+                        * runqueue again ASAP.
+                        */
+                       wake_up(&victim->stop_wq);
                        return spu;
                }
        }
@@ -354,7 +398,7 @@ static struct spu *find_victim(struct spu_context *ctx)
  * @ctx:       spu context to schedule
  * @flags:     flags (currently ignored)
  *
- * Tries to find a free spu to run @ctx.  If no free spu is availble
+ * Tries to find a free spu to run @ctx.  If no free spu is available
  * add the context to the runqueue so it gets woken up once an spu
  * is available.
  */
@@ -379,10 +423,7 @@ int spu_activate(struct spu_context *ctx, unsigned long flags)
                        return 0;
                }
 
-               spu_add_to_rq(ctx);
-               if (!(flags & SPU_ACTIVATE_NOWAKE))
-                       spu_prio_wait(ctx);
-               spu_del_from_rq(ctx);
+               spu_prio_wait(ctx);
        } while (!signal_pending(current));
 
        return -ERESTARTSYS;
@@ -416,7 +457,6 @@ void spu_deactivate(struct spu_context *ctx)
 void spu_yield(struct spu_context *ctx)
 {
        struct spu *spu;
-       int need_yield = 0;
 
        if (mutex_trylock(&ctx->state_mutex)) {
                if ((spu = ctx->spu) != NULL) {
@@ -425,23 +465,25 @@ void spu_yield(struct spu_context *ctx)
                                pr_debug("%s: yielding SPU %d NODE %d\n",
                                         __FUNCTION__, spu->number, spu->node);
                                spu_deactivate(ctx);
-                               need_yield = 1;
                        }
                }
                mutex_unlock(&ctx->state_mutex);
        }
-       if (unlikely(need_yield))
-               yield();
 }
 
 int __init spu_sched_init(void)
 {
        int i;
 
+       spu_sched_wq = create_singlethread_workqueue("spusched");
+       if (!spu_sched_wq)
+               return 1;
+
        spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
        if (!spu_prio) {
                printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
                       __FUNCTION__);
+                      destroy_workqueue(spu_sched_wq);
                return 1;
        }
        for (i = 0; i < MAX_PRIO; i++) {
@@ -472,4 +514,5 @@ void __exit spu_sched_exit(void)
                mutex_unlock(&spu_prio->active_mutex[node]);
        }
        kfree(spu_prio);
+       destroy_workqueue(spu_sched_wq);
 }