[CELL] saving spus information for kexec crash
[safe/jmp/linux-2.6] / arch / powerpc / platforms / cell / spu_base.c
index 9e90965..c563066 100644 (file)
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#define DEBUG 1
+#undef DEBUG
 
 #include <linux/interrupt.h>
 #include <linux/list.h>
 #include <linux/module.h>
-#include <linux/poll.h>
 #include <linux/ptrace.h>
 #include <linux/slab.h>
 #include <linux/wait.h>
-
-#include <asm/io.h>
-#include <asm/prom.h>
-#include <asm/semaphore.h>
+#include <linux/mm.h>
+#include <linux/io.h>
+#include <linux/mutex.h>
+#include <linux/linux_logo.h>
 #include <asm/spu.h>
-#include <asm/mmu_context.h>
+#include <asm/spu_priv1.h>
+#include <asm/xmon.h>
+
+const struct spu_management_ops *spu_management_ops;
+EXPORT_SYMBOL_GPL(spu_management_ops);
+
+const struct spu_priv1_ops *spu_priv1_ops;
+
+static struct list_head spu_list[MAX_NUMNODES];
+static LIST_HEAD(spu_full_list);
+static DEFINE_MUTEX(spu_mutex);
+static DEFINE_SPINLOCK(spu_list_lock);
+
+EXPORT_SYMBOL_GPL(spu_priv1_ops);
+
+void spu_invalidate_slbs(struct spu *spu)
+{
+       struct spu_priv2 __iomem *priv2 = spu->priv2;
 
-#include "interrupt.h"
+       if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
+               out_be64(&priv2->slb_invalidate_all_W, 0UL);
+}
+EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
+
+/* This is called by the MM core when a segment size is changed, to
+ * request a flush of all the SPEs using a given mm
+ */
+void spu_flush_all_slbs(struct mm_struct *mm)
+{
+       struct spu *spu;
+       unsigned long flags;
+
+       spin_lock_irqsave(&spu_list_lock, flags);
+       list_for_each_entry(spu, &spu_full_list, full_list) {
+               if (spu->mm == mm)
+                       spu_invalidate_slbs(spu);
+       }
+       spin_unlock_irqrestore(&spu_list_lock, flags);
+}
+
+/* The hack below stinks... try to do something better one of
+ * these days... Does it even work properly with NR_CPUS == 1 ?
+ */
+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);
+}
+
+void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&spu_list_lock, flags);
+       spu->mm = mm;
+       spin_unlock_irqrestore(&spu_list_lock, flags);
+       if (mm)
+               mm_needs_global_tlbie(mm);
+}
+EXPORT_SYMBOL_GPL(spu_associate_mm);
 
 static int __spu_trap_invalid_dma(struct spu *spu)
 {
        pr_debug("%s\n", __FUNCTION__);
-       force_sig(SIGBUS, /* info, */ current);
+       spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
        return 0;
 }
 
 static int __spu_trap_dma_align(struct spu *spu)
 {
        pr_debug("%s\n", __FUNCTION__);
-       force_sig(SIGBUS, /* info, */ current);
+       spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
        return 0;
 }
 
 static int __spu_trap_error(struct spu *spu)
 {
        pr_debug("%s\n", __FUNCTION__);
-       force_sig(SIGILL, /* info, */ current);
+       spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
        return 0;
 }
 
 static void spu_restart_dma(struct spu *spu)
 {
        struct spu_priv2 __iomem *priv2 = spu->priv2;
-       out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
+
+       if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
+               out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
 }
 
 static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
 {
-       struct spu_priv2 __iomem *priv2;
-       struct mm_struct *mm;
+       struct spu_priv2 __iomem *priv2 = spu->priv2;
+       struct mm_struct *mm = spu->mm;
+       u64 esid, vsid, llp;
+       int psize;
 
        pr_debug("%s\n", __FUNCTION__);
 
-       if (REGION_ID(ea) != USER_REGION_ID) {
+       if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
+               /* SLBs are pre-loaded for context switch, so
+                * we should never get here!
+                */
+               printk("%s: invalid access during switch!\n", __func__);
+               return 1;
+       }
+       esid = (ea & ESID_MASK) | SLB_ESID_V;
+
+       switch(REGION_ID(ea)) {
+       case USER_REGION_ID:
+#ifdef CONFIG_PPC_MM_SLICES
+               psize = get_slice_psize(mm, ea);
+#else
+               psize = mm->context.user_psize;
+#endif
+               vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
+                               SLB_VSID_USER;
+               break;
+       case VMALLOC_REGION_ID:
+               if (ea < VMALLOC_END)
+                       psize = mmu_vmalloc_psize;
+               else
+                       psize = mmu_io_psize;
+               vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
+                       SLB_VSID_KERNEL;
+               break;
+       case KERNEL_REGION_ID:
+               psize = mmu_linear_psize;
+               vsid = (get_kernel_vsid(ea) << SLB_VSID_SHIFT) |
+                       SLB_VSID_KERNEL;
+               break;
+       default:
+               /* Future: support kernel segments so that drivers
+                * can use SPUs.
+                */
                pr_debug("invalid region access at %016lx\n", ea);
                return 1;
        }
+       llp = mmu_psize_defs[psize].sllp;
 
-       priv2 = spu->priv2;
-       mm = spu->mm;
+       out_be64(&priv2->slb_index_W, spu->slb_replace);
+       out_be64(&priv2->slb_vsid_RW, vsid | llp);
+       out_be64(&priv2->slb_esid_RW, esid);
 
+       spu->slb_replace++;
        if (spu->slb_replace >= 8)
                spu->slb_replace = 0;
 
-       out_be64(&priv2->slb_index_W, spu->slb_replace);
-       out_be64(&priv2->slb_vsid_RW,
-               (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT)
-                                                | SLB_VSID_USER);
-       out_be64(&priv2->slb_esid_RW, (ea & ESID_MASK) | SLB_ESID_V);
-
        spu_restart_dma(spu);
-
-       pr_debug("set slb %d context %lx, ea %016lx, vsid %016lx, esid %016lx\n",
-               spu->slb_replace, mm->context.id, ea,
-               (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT)| SLB_VSID_USER,
-                (ea & ESID_MASK) | SLB_ESID_V);
-       return 0;
-}
-
-static int __spu_trap_data_map(struct spu *spu, unsigned long ea)
-{
-       unsigned long dsisr;
-       struct spu_priv1 __iomem *priv1;
-
-       pr_debug("%s\n", __FUNCTION__);
-       priv1 = spu->priv1;
-       dsisr = in_be64(&priv1->mfc_dsisr_RW);
-
-       wake_up(&spu->stop_wq);
-
-       return 0;
-}
-
-static int __spu_trap_mailbox(struct spu *spu)
-{
-       wake_up_all(&spu->ibox_wq);
-       kill_fasync(&spu->ibox_fasync, SIGIO, POLLIN);
-
-       /* atomically disable SPU mailbox interrupts */
-       spin_lock(&spu->register_lock);
-       out_be64(&spu->priv1->int_mask_class2_RW,
-               in_be64(&spu->priv1->int_mask_class2_RW) & ~0x1);
-       spin_unlock(&spu->register_lock);
-       return 0;
-}
-
-static int __spu_trap_stop(struct spu *spu)
-{
-       pr_debug("%s\n", __FUNCTION__);
-       spu->stop_code = in_be32(&spu->problem->spu_status_R);
-       wake_up(&spu->stop_wq);
+       spu->stats.slb_flt++;
        return 0;
 }
 
-static int __spu_trap_halt(struct spu *spu)
+extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
+static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
 {
-       pr_debug("%s\n", __FUNCTION__);
-       spu->stop_code = in_be32(&spu->problem->spu_status_R);
-       wake_up(&spu->stop_wq);
-       return 0;
-}
+       pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
 
-static int __spu_trap_tag_group(struct spu *spu)
-{
-       pr_debug("%s\n", __FUNCTION__);
-       /* wake_up(&spu->dma_wq); */
-       return 0;
-}
+       /* Handle kernel space hash faults immediately.
+          User hash faults need to be deferred to process context. */
+       if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
+           && REGION_ID(ea) != USER_REGION_ID
+           && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
+               spu_restart_dma(spu);
+               return 0;
+       }
 
-static int __spu_trap_spubox(struct spu *spu)
-{
-       wake_up_all(&spu->wbox_wq);
-       kill_fasync(&spu->wbox_fasync, SIGIO, POLLOUT);
+       if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
+               printk("%s: invalid access during switch!\n", __func__);
+               return 1;
+       }
 
-       /* atomically disable SPU mailbox interrupts */
-       spin_lock(&spu->register_lock);
-       out_be64(&spu->priv1->int_mask_class2_RW,
-               in_be64(&spu->priv1->int_mask_class2_RW) & ~0x10);
-       spin_unlock(&spu->register_lock);
+       spu->dar = ea;
+       spu->dsisr = dsisr;
+       mb();
+       spu->stop_callback(spu);
        return 0;
 }
 
 static irqreturn_t
-spu_irq_class_0(int irq, void *data, struct pt_regs *regs)
+spu_irq_class_0(int irq, void *data)
 {
        struct spu *spu;
 
        spu = data;
        spu->class_0_pending = 1;
-       wake_up(&spu->stop_wq);
+       spu->stop_callback(spu);
 
        return IRQ_HANDLED;
 }
 
-static int
+int
 spu_irq_class_0_bottom(struct spu *spu)
 {
-       unsigned long stat;
+       unsigned long stat, mask;
+       unsigned long flags;
 
        spu->class_0_pending = 0;
 
-       stat = in_be64(&spu->priv1->int_stat_class0_RW);
+       spin_lock_irqsave(&spu->register_lock, flags);
+       mask = spu_int_mask_get(spu, 0);
+       stat = spu_int_stat_get(spu, 0);
 
-       if (stat & 1) /* invalid MFC DMA */
-               __spu_trap_invalid_dma(spu);
+       stat &= mask;
 
-       if (stat & 2) /* invalid DMA alignment */
+       if (stat & 1) /* invalid DMA alignment */
                __spu_trap_dma_align(spu);
 
+       if (stat & 2) /* invalid MFC DMA */
+               __spu_trap_invalid_dma(spu);
+
        if (stat & 4) /* error on SPU */
                __spu_trap_error(spu);
 
-       out_be64(&spu->priv1->int_stat_class0_RW, stat);
-       return 0;
+       spu_int_stat_clear(spu, 0, stat);
+       spin_unlock_irqrestore(&spu->register_lock, flags);
+
+       return (stat & 0x7) ? -EIO : 0;
 }
+EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
 
 static irqreturn_t
-spu_irq_class_1(int irq, void *data, struct pt_regs *regs)
+spu_irq_class_1(int irq, void *data)
 {
        struct spu *spu;
-       unsigned long stat, dar;
+       unsigned long stat, mask, dar, dsisr;
 
        spu = data;
-       stat  = in_be64(&spu->priv1->int_stat_class1_RW);
-       dar   = in_be64(&spu->priv1->mfc_dar_RW);
+
+       /* atomically read & clear class1 status. */
+       spin_lock(&spu->register_lock);
+       mask  = spu_int_mask_get(spu, 1);
+       stat  = spu_int_stat_get(spu, 1) & mask;
+       dar   = spu_mfc_dar_get(spu);
+       dsisr = spu_mfc_dsisr_get(spu);
+       if (stat & 2) /* mapping fault */
+               spu_mfc_dsisr_set(spu, 0ul);
+       spu_int_stat_clear(spu, 1, stat);
+       spin_unlock(&spu->register_lock);
+       pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
+                       dar, dsisr);
 
        if (stat & 1) /* segment fault */
                __spu_trap_data_seg(spu, dar);
 
        if (stat & 2) { /* mapping fault */
-               __spu_trap_data_map(spu, dar);
+               __spu_trap_data_map(spu, dar, dsisr);
        }
 
        if (stat & 4) /* ls compare & suspend on get */
@@ -218,95 +290,106 @@ spu_irq_class_1(int irq, void *data, struct pt_regs *regs)
        if (stat & 8) /* ls compare & suspend on put */
                ;
 
-       out_be64(&spu->priv1->int_stat_class1_RW, stat);
        return stat ? IRQ_HANDLED : IRQ_NONE;
 }
 
 static irqreturn_t
-spu_irq_class_2(int irq, void *data, struct pt_regs *regs)
+spu_irq_class_2(int irq, void *data)
 {
        struct spu *spu;
        unsigned long stat;
+       unsigned long mask;
 
        spu = data;
-       stat = in_be64(&spu->priv1->int_stat_class2_RW);
-
-       pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat,
-               in_be64(&spu->priv1->int_mask_class2_RW));
+       spin_lock(&spu->register_lock);
+       stat = spu_int_stat_get(spu, 2);
+       mask = spu_int_mask_get(spu, 2);
+       /* ignore interrupts we're not waiting for */
+       stat &= mask;
+       /*
+        * mailbox interrupts (0x1 and 0x10) are level triggered.
+        * mask them now before acknowledging.
+        */
+       if (stat & 0x11)
+               spu_int_mask_and(spu, 2, ~(stat & 0x11));
+       /* acknowledge all interrupts before the callbacks */
+       spu_int_stat_clear(spu, 2, stat);
+       spin_unlock(&spu->register_lock);
 
+       pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
 
        if (stat & 1)  /* PPC core mailbox */
-               __spu_trap_mailbox(spu);
+               spu->ibox_callback(spu);
 
        if (stat & 2) /* SPU stop-and-signal */
-               __spu_trap_stop(spu);
+               spu->stop_callback(spu);
 
        if (stat & 4) /* SPU halted */
-               __spu_trap_halt(spu);
+               spu->stop_callback(spu);
 
        if (stat & 8) /* DMA tag group complete */
-               __spu_trap_tag_group(spu);
+               spu->mfc_callback(spu);
 
        if (stat & 0x10) /* SPU mailbox threshold */
-               __spu_trap_spubox(spu);
+               spu->wbox_callback(spu);
 
-       out_be64(&spu->priv1->int_stat_class2_RW, stat);
+       spu->stats.class2_intr++;
        return stat ? IRQ_HANDLED : IRQ_NONE;
 }
 
-static int
-spu_request_irqs(struct spu *spu)
+static int spu_request_irqs(struct spu *spu)
 {
-       int ret;
-       int irq_base;
-
-       irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
-
-       snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", spu->number);
-       ret = request_irq(irq_base + spu->isrc,
-                spu_irq_class_0, 0, spu->irq_c0, spu);
-       if (ret)
-               goto out;
-       out_be64(&spu->priv1->int_mask_class0_RW, 0x7);
-
-       snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", spu->number);
-       ret = request_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc,
-                spu_irq_class_1, 0, spu->irq_c1, spu);
-       if (ret)
-               goto out1;
-       out_be64(&spu->priv1->int_mask_class1_RW, 0x3);
+       int ret = 0;
 
-       snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", spu->number);
-       ret = request_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc,
-                spu_irq_class_2, 0, spu->irq_c2, spu);
-       if (ret)
-               goto out2;
-       out_be64(&spu->priv1->int_mask_class2_RW, 0xe);
-       goto out;
+       if (spu->irqs[0] != NO_IRQ) {
+               snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
+                        spu->number);
+               ret = request_irq(spu->irqs[0], spu_irq_class_0,
+                                 IRQF_DISABLED,
+                                 spu->irq_c0, spu);
+               if (ret)
+                       goto bail0;
+       }
+       if (spu->irqs[1] != NO_IRQ) {
+               snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
+                        spu->number);
+               ret = request_irq(spu->irqs[1], spu_irq_class_1,
+                                 IRQF_DISABLED,
+                                 spu->irq_c1, spu);
+               if (ret)
+                       goto bail1;
+       }
+       if (spu->irqs[2] != NO_IRQ) {
+               snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
+                        spu->number);
+               ret = request_irq(spu->irqs[2], spu_irq_class_2,
+                                 IRQF_DISABLED,
+                                 spu->irq_c2, spu);
+               if (ret)
+                       goto bail2;
+       }
+       return 0;
 
-out2:
-       free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
-out1:
-       free_irq(irq_base + spu->isrc, spu);
-out:
+bail2:
+       if (spu->irqs[1] != NO_IRQ)
+               free_irq(spu->irqs[1], spu);
+bail1:
+       if (spu->irqs[0] != NO_IRQ)
+               free_irq(spu->irqs[0], spu);
+bail0:
        return ret;
 }
 
-static void
-spu_free_irqs(struct spu *spu)
+static void spu_free_irqs(struct spu *spu)
 {
-       int irq_base;
-
-       irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
-
-       free_irq(irq_base + spu->isrc, spu);
-       free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
-       free_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, spu);
+       if (spu->irqs[0] != NO_IRQ)
+               free_irq(spu->irqs[0], spu);
+       if (spu->irqs[1] != NO_IRQ)
+               free_irq(spu->irqs[1], spu);
+       if (spu->irqs[2] != NO_IRQ)
+               free_irq(spu->irqs[2], spu);
 }
 
-static LIST_HEAD(spu_list);
-static DECLARE_MUTEX(spu_mutex);
-
 static void spu_init_channels(struct spu *spu)
 {
        static const struct {
@@ -320,7 +403,7 @@ static void spu_init_channels(struct spu *spu)
                { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
                { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
        };
-       struct spu_priv2 *priv2;
+       struct spu_priv2 __iomem *priv2;
        int i;
 
        priv2 = spu->priv2;
@@ -341,397 +424,267 @@ static void spu_init_channels(struct spu *spu)
        }
 }
 
-static void spu_init_regs(struct spu *spu)
+struct spu *spu_alloc_node(int node)
 {
-       out_be64(&spu->priv1->int_mask_class0_RW, 0x7);
-       out_be64(&spu->priv1->int_mask_class1_RW, 0x3);
-       out_be64(&spu->priv1->int_mask_class2_RW, 0xe);
-}
+       struct spu *spu = NULL;
 
-struct spu *spu_alloc(void)
-{
-       struct spu *spu;
-
-       down(&spu_mutex);
-       if (!list_empty(&spu_list)) {
-               spu = list_entry(spu_list.next, struct spu, list);
+       mutex_lock(&spu_mutex);
+       if (!list_empty(&spu_list[node])) {
+               spu = list_entry(spu_list[node].next, struct spu, list);
                list_del_init(&spu->list);
-               pr_debug("Got SPU %x %d\n", spu->isrc, spu->number);
-       } else {
-               pr_debug("No SPU left\n");
-               spu = NULL;
+               pr_debug("Got SPU %d %d\n", spu->number, spu->node);
        }
-       up(&spu_mutex);
+       mutex_unlock(&spu_mutex);
 
-       if (spu) {
+       if (spu)
                spu_init_channels(spu);
-               spu_init_regs(spu);
-       }
-
        return spu;
 }
-EXPORT_SYMBOL(spu_alloc);
-
-void spu_free(struct spu *spu)
-{
-       down(&spu_mutex);
-       spu->ibox_fasync = NULL;
-       spu->wbox_fasync = NULL;
-       list_add_tail(&spu->list, &spu_list);
-       up(&spu_mutex);
-}
-EXPORT_SYMBOL(spu_free);
+EXPORT_SYMBOL_GPL(spu_alloc_node);
 
-extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
-static int spu_handle_mm_fault(struct spu *spu)
+struct spu *spu_alloc(void)
 {
-       struct spu_priv1 __iomem *priv1;
-       struct mm_struct *mm = spu->mm;
-       struct vm_area_struct *vma;
-       u64 ea, dsisr, is_write;
-       int ret;
+       struct spu *spu = NULL;
+       int node;
 
-       priv1 = spu->priv1;
-       ea = in_be64(&priv1->mfc_dar_RW);
-       dsisr = in_be64(&priv1->mfc_dsisr_RW);
-#if 0
-       if (!IS_VALID_EA(ea)) {
-               return -EFAULT;
-       }
-#endif /* XXX */
-       if (mm == NULL) {
-               return -EFAULT;
-       }
-       if (mm->pgd == NULL) {
-               return -EFAULT;
-       }
-
-       down_read(&mm->mmap_sem);
-       vma = find_vma(mm, ea);
-       if (!vma)
-               goto bad_area;
-       if (vma->vm_start <= ea)
-               goto good_area;
-       if (!(vma->vm_flags & VM_GROWSDOWN))
-               goto bad_area;
-#if 0
-       if (expand_stack(vma, ea))
-               goto bad_area;
-#endif /* XXX */
-good_area:
-       is_write = dsisr & MFC_DSISR_ACCESS_PUT;
-       if (is_write) {
-               if (!(vma->vm_flags & VM_WRITE))
-                       goto bad_area;
-       } else {
-               if (dsisr & MFC_DSISR_ACCESS_DENIED)
-                       goto bad_area;
-               if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
-                       goto bad_area;
-       }
-       ret = 0;
-       switch (handle_mm_fault(mm, vma, ea, is_write)) {
-       case VM_FAULT_MINOR:
-               current->min_flt++;
-               break;
-       case VM_FAULT_MAJOR:
-               current->maj_flt++;
-               break;
-       case VM_FAULT_SIGBUS:
-               ret = -EFAULT;
-               goto bad_area;
-       case VM_FAULT_OOM:
-               ret = -ENOMEM;
-               goto bad_area;
-       default:
-               BUG();
+       for (node = 0; node < MAX_NUMNODES; node++) {
+               spu = spu_alloc_node(node);
+               if (spu)
+                       break;
        }
-       up_read(&mm->mmap_sem);
-       return ret;
 
-bad_area:
-       up_read(&mm->mmap_sem);
-       return -EFAULT;
+       return spu;
 }
 
-static int spu_handle_pte_fault(struct spu *spu)
+void spu_free(struct spu *spu)
 {
-       struct spu_priv1 __iomem *priv1;
-       u64 ea, dsisr, access, error = 0UL;
-       int ret = 0;
-
-       priv1 = spu->priv1;
-       ea = in_be64(&priv1->mfc_dar_RW);
-       dsisr = in_be64(&priv1->mfc_dsisr_RW);
-       access = (_PAGE_PRESENT | _PAGE_USER);
-       if (dsisr & MFC_DSISR_PTE_NOT_FOUND) {
-               if (hash_page(ea, access, 0x300) != 0)
-                       error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
-       }
-       if ((error & CLASS1_ENABLE_STORAGE_FAULT_INTR) ||
-           (dsisr & MFC_DSISR_ACCESS_DENIED)) {
-               if ((ret = spu_handle_mm_fault(spu)) != 0)
-                       error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
-               else
-                       error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
-       }
-       if (!error)
-               spu_restart_dma(spu);
-
-       return ret;
+       mutex_lock(&spu_mutex);
+       list_add_tail(&spu->list, &spu_list[spu->node]);
+       mutex_unlock(&spu_mutex);
 }
+EXPORT_SYMBOL_GPL(spu_free);
 
-int spu_run(struct spu *spu)
+static int spu_shutdown(struct sys_device *sysdev)
 {
-       struct spu_problem __iomem *prob;
-       struct spu_priv1 __iomem *priv1;
-       struct spu_priv2 __iomem *priv2;
-       unsigned long status;
-       int ret;
-
-       prob = spu->problem;
-       priv1 = spu->priv1;
-       priv2 = spu->priv2;
+       struct spu *spu = container_of(sysdev, struct spu, sysdev);
 
-       /* Let SPU run.  */
-       spu->mm = current->mm;
-       eieio();
-       out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_RUNNABLE);
-
-       do {
-               ret = wait_event_interruptible(spu->stop_wq,
-                        (!((status = in_be32(&prob->spu_status_R)) & 0x1))
-                       || (in_be64(&priv1->mfc_dsisr_RW) & MFC_DSISR_PTE_NOT_FOUND)
-                       || spu->class_0_pending);
-
-               if (status & SPU_STATUS_STOPPED_BY_STOP)
-                       ret = -EAGAIN;
-               else if (status & SPU_STATUS_STOPPED_BY_HALT)
-                       ret = -EIO;
-               else if (in_be64(&priv1->mfc_dsisr_RW) & MFC_DSISR_PTE_NOT_FOUND)
-                       ret = spu_handle_pte_fault(spu);
-
-               if (spu->class_0_pending)
-                       spu_irq_class_0_bottom(spu);
-
-               if (!ret && signal_pending(current))
-                       ret = -ERESTARTSYS;
-
-       } while (!ret);
-
-       /* Ensure SPU is stopped.  */
-       out_be32(&prob->spu_runcntl_RW, SPU_RUNCNTL_STOP);
-       eieio();
-       while (in_be32(&prob->spu_status_R) & SPU_STATUS_RUNNING)
-               cpu_relax();
-
-       out_be64(&priv2->slb_invalidate_all_W, 0);
-       out_be64(&priv1->tlb_invalidate_entry_W, 0UL);
-       eieio();
-
-       spu->mm = NULL;
-
-       /* Check for SPU breakpoint.  */
-       if (unlikely(current->ptrace & PT_PTRACED)) {
-               status = in_be32(&prob->spu_status_R);
-
-               if ((status & SPU_STATUS_STOPPED_BY_STOP)
-                   && status >> SPU_STOP_STATUS_SHIFT == 0x3fff) {
-                       force_sig(SIGTRAP, current);
-                       ret = -ERESTARTSYS;
-               }
-       }
-
-       return ret;
+       spu_free_irqs(spu);
+       spu_destroy_spu(spu);
+       return 0;
 }
-EXPORT_SYMBOL(spu_run);
-
-static void __iomem * __init map_spe_prop(struct device_node *n,
-                                                const char *name)
-{
-       struct address_prop {
-               unsigned long address;
-               unsigned int len;
-       } __attribute__((packed)) *prop;
 
-       void *p;
-       int proplen;
+struct sysdev_class spu_sysdev_class = {
+       set_kset_name("spu"),
+       .shutdown = spu_shutdown,
+};
 
-       p = get_property(n, name, &proplen);
-       if (proplen != sizeof (struct address_prop))
-               return NULL;
+int spu_add_sysdev_attr(struct sysdev_attribute *attr)
+{
+       struct spu *spu;
+       mutex_lock(&spu_mutex);
 
-       prop = p;
+       list_for_each_entry(spu, &spu_full_list, full_list)
+               sysdev_create_file(&spu->sysdev, attr);
 
-       return ioremap(prop->address, prop->len);
+       mutex_unlock(&spu_mutex);
+       return 0;
 }
+EXPORT_SYMBOL_GPL(spu_add_sysdev_attr);
 
-static void spu_unmap(struct spu *spu)
+int spu_add_sysdev_attr_group(struct attribute_group *attrs)
 {
-       iounmap(spu->priv2);
-       iounmap(spu->priv1);
-       iounmap(spu->problem);
-       iounmap((u8 __iomem *)spu->local_store);
-}
+       struct spu *spu;
+       mutex_lock(&spu_mutex);
 
-static int __init spu_map_device(struct spu *spu, struct device_node *spe)
-{
-       char *prop;
-       int ret;
+       list_for_each_entry(spu, &spu_full_list, full_list)
+               sysfs_create_group(&spu->sysdev.kobj, attrs);
 
-       ret = -ENODEV;
-       prop = get_property(spe, "isrc", NULL);
-       if (!prop)
-               goto out;
-       spu->isrc = *(unsigned int *)prop;
+       mutex_unlock(&spu_mutex);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(spu_add_sysdev_attr_group);
 
-       spu->name = get_property(spe, "name", NULL);
-       if (!spu->name)
-               goto out;
 
-       prop = get_property(spe, "local-store", NULL);
-       if (!prop)
-               goto out;
-       spu->local_store_phys = *(unsigned long *)prop;
+void spu_remove_sysdev_attr(struct sysdev_attribute *attr)
+{
+       struct spu *spu;
+       mutex_lock(&spu_mutex);
 
-       /* we use local store as ram, not io memory */
-       spu->local_store = (void __force *)map_spe_prop(spe, "local-store");
-       if (!spu->local_store)
-               goto out;
+       list_for_each_entry(spu, &spu_full_list, full_list)
+               sysdev_remove_file(&spu->sysdev, attr);
 
-       spu->problem= map_spe_prop(spe, "problem");
-       if (!spu->problem)
-               goto out_unmap;
+       mutex_unlock(&spu_mutex);
+}
+EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr);
 
-       spu->priv1= map_spe_prop(spe, "priv1");
-       if (!spu->priv1)
-               goto out_unmap;
+void spu_remove_sysdev_attr_group(struct attribute_group *attrs)
+{
+       struct spu *spu;
+       mutex_lock(&spu_mutex);
 
-       spu->priv2= map_spe_prop(spe, "priv2");
-       if (!spu->priv2)
-               goto out_unmap;
-       ret = 0;
-       goto out;
+       list_for_each_entry(spu, &spu_full_list, full_list)
+               sysfs_remove_group(&spu->sysdev.kobj, attrs);
 
-out_unmap:
-       spu_unmap(spu);
-out:
-       return ret;
+       mutex_unlock(&spu_mutex);
 }
+EXPORT_SYMBOL_GPL(spu_remove_sysdev_attr_group);
 
-static int __init find_spu_node_id(struct device_node *spe)
+static int spu_create_sysdev(struct spu *spu)
 {
-       unsigned int *id;
-       struct device_node *cpu;
+       int ret;
 
-       cpu = spe->parent->parent;
-       id = (unsigned int *)get_property(cpu, "node-id", NULL);
+       spu->sysdev.id = spu->number;
+       spu->sysdev.cls = &spu_sysdev_class;
+       ret = sysdev_register(&spu->sysdev);
+       if (ret) {
+               printk(KERN_ERR "Can't register SPU %d with sysfs\n",
+                               spu->number);
+               return ret;
+       }
 
-       return id ? *id : 0;
+       sysfs_add_device_to_node(&spu->sysdev, spu->node);
+
+       return 0;
 }
 
-static int __init create_spu(struct device_node *spe)
+static int __init create_spu(void *data)
 {
        struct spu *spu;
        int ret;
        static int number;
+       unsigned long flags;
 
        ret = -ENOMEM;
-       spu = kmalloc(sizeof (*spu), GFP_KERNEL);
+       spu = kzalloc(sizeof (*spu), GFP_KERNEL);
        if (!spu)
                goto out;
 
-       ret = spu_map_device(spu, spe);
-       if (ret)
-               goto out_free;
-
-       spu->node = find_spu_node_id(spe);
-       spu->stop_code = 0;
-       spu->slb_replace = 0;
-       spu->mm = NULL;
-       spu->class_0_pending = 0;
        spin_lock_init(&spu->register_lock);
+       mutex_lock(&spu_mutex);
+       spu->number = number++;
+       mutex_unlock(&spu_mutex);
 
-       out_be64(&spu->priv1->mfc_sdr_RW, mfspr(SPRN_SDR1));
-       out_be64(&spu->priv1->mfc_sr1_RW, 0x33);
-
-       init_waitqueue_head(&spu->stop_wq);
-       init_waitqueue_head(&spu->wbox_wq);
-       init_waitqueue_head(&spu->ibox_wq);
+       ret = spu_create_spu(spu, data);
 
-       spu->ibox_fasync = NULL;
-       spu->wbox_fasync = NULL;
+       if (ret)
+               goto out_free;
 
-       down(&spu_mutex);
-       spu->number = number++;
+       spu_mfc_sdr_setup(spu);
+       spu_mfc_sr1_set(spu, 0x33);
        ret = spu_request_irqs(spu);
        if (ret)
-               goto out_unmap;
+               goto out_destroy;
+
+       ret = spu_create_sysdev(spu);
+       if (ret)
+               goto out_free_irqs;
 
-       list_add(&spu->list, &spu_list);
-       up(&spu_mutex);
+       mutex_lock(&spu_mutex);
+       spin_lock_irqsave(&spu_list_lock, flags);
+       list_add(&spu->list, &spu_list[spu->node]);
+       list_add(&spu->full_list, &spu_full_list);
+       spin_unlock_irqrestore(&spu_list_lock, flags);
+       mutex_unlock(&spu_mutex);
+
+       spu->stats.utilization_state = SPU_UTIL_IDLE;
+       spu->stats.tstamp = jiffies;
 
-       pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
-               spu->name, spu->isrc, spu->local_store,
-               spu->problem, spu->priv1, spu->priv2, spu->number);
        goto out;
 
-out_unmap:
-       up(&spu_mutex);
-       spu_unmap(spu);
+out_free_irqs:
+       spu_free_irqs(spu);
+out_destroy:
+       spu_destroy_spu(spu);
 out_free:
        kfree(spu);
 out:
        return ret;
 }
 
-static void destroy_spu(struct spu *spu)
+static const char *spu_state_names[] = {
+       "user", "system", "iowait", "idle"
+};
+
+static unsigned long long spu_acct_time(struct spu *spu,
+               enum spu_utilization_state state)
 {
-       list_del_init(&spu->list);
+       unsigned long long time = spu->stats.times[state];
 
-       spu_free_irqs(spu);
-       spu_unmap(spu);
-       kfree(spu);
+       if (spu->stats.utilization_state == state)
+               time += jiffies - spu->stats.tstamp;
+
+       return jiffies_to_msecs(time);
 }
 
-static void cleanup_spu_base(void)
+
+static ssize_t spu_stat_show(struct sys_device *sysdev, char *buf)
 {
-       struct spu *spu, *tmp;
-       down(&spu_mutex);
-       list_for_each_entry_safe(spu, tmp, &spu_list, list)
-               destroy_spu(spu);
-       up(&spu_mutex);
+       struct spu *spu = container_of(sysdev, struct spu, sysdev);
+
+       return sprintf(buf, "%s %llu %llu %llu %llu "
+                     "%llu %llu %llu %llu %llu %llu %llu %llu\n",
+               spu_state_names[spu->stats.utilization_state],
+               spu_acct_time(spu, SPU_UTIL_USER),
+               spu_acct_time(spu, SPU_UTIL_SYSTEM),
+               spu_acct_time(spu, SPU_UTIL_IOWAIT),
+               spu_acct_time(spu, SPU_UTIL_IDLE),
+               spu->stats.vol_ctx_switch,
+               spu->stats.invol_ctx_switch,
+               spu->stats.slb_flt,
+               spu->stats.hash_flt,
+               spu->stats.min_flt,
+               spu->stats.maj_flt,
+               spu->stats.class2_intr,
+               spu->stats.libassist);
 }
-module_exit(cleanup_spu_base);
+
+static SYSDEV_ATTR(stat, 0644, spu_stat_show, NULL);
 
 static int __init init_spu_base(void)
 {
-       struct device_node *node;
-       int ret;
+       int i, ret = 0;
 
-       ret = -ENODEV;
-       for (node = of_find_node_by_type(NULL, "spe");
-                       node; node = of_find_node_by_type(node, "spe")) {
-               ret = create_spu(node);
-               if (ret) {
-                       printk(KERN_WARNING "%s: Error initializing %s\n",
-                               __FUNCTION__, node->name);
-                       cleanup_spu_base();
-                       break;
-               }
+       for (i = 0; i < MAX_NUMNODES; i++)
+               INIT_LIST_HEAD(&spu_list[i]);
+
+       if (!spu_management_ops)
+               goto out;
+
+       /* create sysdev class for spus */
+       ret = sysdev_class_register(&spu_sysdev_class);
+       if (ret)
+               goto out;
+
+       ret = spu_enumerate_spus(create_spu);
+
+       if (ret < 0) {
+               printk(KERN_WARNING "%s: Error initializing spus\n",
+                       __FUNCTION__);
+               goto out_unregister_sysdev_class;
        }
-       /* in some old firmware versions, the spe is called 'spc', so we
-          look for that as well */
-       for (node = of_find_node_by_type(NULL, "spc");
-                       node; node = of_find_node_by_type(node, "spc")) {
-               ret = create_spu(node);
-               if (ret) {
-                       printk(KERN_WARNING "%s: Error initializing %s\n",
-                               __FUNCTION__, node->name);
-                       cleanup_spu_base();
-                       break;
-               }
+
+       if (ret > 0) {
+               /*
+                * We cannot put the forward declaration in
+                * <linux/linux_logo.h> because of conflicting session type
+                * conflicts for const and __initdata with different compiler
+                * versions
+                */
+               extern const struct linux_logo logo_spe_clut224;
+
+               fb_append_extra_logo(&logo_spe_clut224, ret);
        }
+
+       xmon_register_spus(&spu_full_list);
+       crash_register_spus(&spu_full_list);
+       spu_add_sysdev_attr(&attr_stat);
+
+       return 0;
+
+ out_unregister_sysdev_class:
+       sysdev_class_unregister(&spu_sysdev_class);
+ out:
+
        return ret;
 }
 module_init(init_spu_base);