Pull asus into release branch
[pandora-kernel.git] / arch / powerpc / platforms / cell / spufs / sched.c
index 40202a7..39823ce 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,40 @@ static inline int node_allowed(int node)
        return 1;
 }
 
+void spu_start_tick(struct spu_context *ctx)
+{
+       if (ctx->policy == SCHED_RR)
+               queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE);
+}
+
+void spu_stop_tick(struct spu_context *ctx)
+{
+       if (ctx->policy == SCHED_RR)
+               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 rearm = 1;
+
+       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);
+                       rearm = 0;
+               }
+       }
+       mutex_unlock(&ctx->state_mutex);
+
+       if (rearm)
+               spu_start_tick(ctx);
+}
+
 /**
  * spu_add_to_active_list - add spu to active list
  * @spu:       spu to add to the active list
@@ -93,14 +127,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 +159,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 +196,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;
@@ -229,7 +254,6 @@ static void spu_prio_wait(struct spu_context *ctx)
 {
        DEFINE_WAIT(wait);
 
-       set_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
        prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
        if (!signal_pending(current)) {
                mutex_unlock(&ctx->state_mutex);
@@ -238,7 +262,6 @@ static void spu_prio_wait(struct spu_context *ctx)
        }
        __set_current_state(TASK_RUNNING);
        remove_wait_queue(&ctx->stop_wq, &wait);
-       clear_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
 }
 
 /**
@@ -258,7 +281,7 @@ static void spu_reschedule(struct spu *spu)
        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))
+               if (ctx)
                        wake_up(&ctx->stop_wq);
        }
        spin_unlock(&spu_prio->runq_lock);
@@ -281,13 +304,73 @@ static struct spu *spu_get_idle(struct spu_context *ctx)
        return spu;
 }
 
-/* The three externally callable interfaces
- * for the scheduler begin here.
+/**
+ * find_victim - find a lower priority context to preempt
+ * @ctx:       canidate context for running
  *
- *     spu_activate    - bind a context to SPU, waiting as needed.
- *     spu_deactivate  - unbind a context from its SPU.
- *     spu_yield       - yield an SPU if others are waiting.
+ * Returns the freed physical spu to run the new context on.
  */
+static struct spu *find_victim(struct spu_context *ctx)
+{
+       struct spu_context *victim = NULL;
+       struct spu *spu;
+       int node, n;
+
+       /*
+        * Look for a possible preemption candidate on the local node first.
+        * If there is no candidate look at the other nodes.  This isn't
+        * exactly fair, but so far the whole spu schedule tries to keep
+        * a strong node affinity.  We might want to fine-tune this in
+        * the future.
+        */
+ restart:
+       node = cpu_to_node(raw_smp_processor_id());
+       for (n = 0; n < MAX_NUMNODES; n++, node++) {
+               node = (node < MAX_NUMNODES) ? node : 0;
+               if (!node_allowed(node))
+                       continue;
+
+               mutex_lock(&spu_prio->active_mutex[node]);
+               list_for_each_entry(spu, &spu_prio->active_list[node], list) {
+                       struct spu_context *tmp = spu->ctx;
+
+                       if (tmp->rt_priority < ctx->rt_priority &&
+                           (!victim || tmp->rt_priority < victim->rt_priority))
+                               victim = spu->ctx;
+               }
+               mutex_unlock(&spu_prio->active_mutex[node]);
+
+               if (victim) {
+                       /*
+                        * This nests ctx->state_mutex, but we always lock
+                        * higher priority contexts before lower priority
+                        * ones, so this is safe until we introduce
+                        * priority inheritance schemes.
+                        */
+                       if (!mutex_trylock(&victim->state_mutex)) {
+                               victim = NULL;
+                               goto restart;
+                       }
+
+                       spu = victim->spu;
+                       if (!spu) {
+                               /*
+                                * This race can happen because we've dropped
+                                * the active list mutex.  No a problem, just
+                                * restart the search.
+                                */
+                               mutex_unlock(&victim->state_mutex);
+                               victim = NULL;
+                               goto restart;
+                       }
+                       spu_unbind_context(spu, victim);
+                       mutex_unlock(&victim->state_mutex);
+                       return spu;
+               }
+       }
+
+       return NULL;
+}
 
 /**
  * spu_activate - find a free spu for a context and execute it
@@ -308,14 +391,19 @@ int spu_activate(struct spu_context *ctx, unsigned long flags)
                struct spu *spu;
 
                spu = spu_get_idle(ctx);
+               /*
+                * If this is a realtime thread we try to get it running by
+                * preempting a lower priority thread.
+                */
+               if (!spu && ctx->rt_priority)
+                       spu = find_victim(ctx);
                if (spu) {
                        spu_bind_context(spu, ctx);
                        return 0;
                }
 
                spu_add_to_rq(ctx);
-               if (!(flags & SPU_ACTIVATE_NOWAKE))
-                       spu_prio_wait(ctx);
+               spu_prio_wait(ctx);
                spu_del_from_rq(ctx);
        } while (!signal_pending(current));
 
@@ -339,6 +427,14 @@ void spu_deactivate(struct spu_context *ctx)
        }
 }
 
+/**
+ * spu_yield -  yield a physical spu if others are waiting
+ * @ctx:       spu context to yield
+ *
+ * Check if there is a higher priority context waiting and if yes
+ * unbind @ctx from the physical spu and schedule the highest
+ * priority context to run on the freed physical spu instead.
+ */
 void spu_yield(struct spu_context *ctx)
 {
        struct spu *spu;
@@ -364,10 +460,15 @@ 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++) {
@@ -398,4 +499,5 @@ void __exit spu_sched_exit(void)
                mutex_unlock(&spu_prio->active_mutex[node]);
        }
        kfree(spu_prio);
+       destroy_workqueue(spu_sched_wq);
 }