Merge branch 'for-2.6.25' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus...
[sfrench/cifs-2.6.git] / arch / powerpc / platforms / cell / spufs / file.c
index 45614c73c7841824d1a1f47834ebc2838bfbbe0a..c66c3756970d53d52ae56df931e38219a9e79ade 100644 (file)
@@ -28,6 +28,8 @@
 #include <linux/pagemap.h>
 #include <linux/poll.h>
 #include <linux/ptrace.h>
+#include <linux/seq_file.h>
+#include <linux/marker.h>
 
 #include <asm/io.h>
 #include <asm/semaphore.h>
 
 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
 
+/* Simple attribute files */
+struct spufs_attr {
+       int (*get)(void *, u64 *);
+       int (*set)(void *, u64);
+       char get_buf[24];       /* enough to store a u64 and "\n\0" */
+       char set_buf[24];
+       void *data;
+       const char *fmt;        /* format for read operation */
+       struct mutex mutex;     /* protects access to these buffers */
+};
+
+static int spufs_attr_open(struct inode *inode, struct file *file,
+               int (*get)(void *, u64 *), int (*set)(void *, u64),
+               const char *fmt)
+{
+       struct spufs_attr *attr;
+
+       attr = kmalloc(sizeof(*attr), GFP_KERNEL);
+       if (!attr)
+               return -ENOMEM;
+
+       attr->get = get;
+       attr->set = set;
+       attr->data = inode->i_private;
+       attr->fmt = fmt;
+       mutex_init(&attr->mutex);
+       file->private_data = attr;
+
+       return nonseekable_open(inode, file);
+}
+
+static int spufs_attr_release(struct inode *inode, struct file *file)
+{
+       kfree(file->private_data);
+       return 0;
+}
+
+static ssize_t spufs_attr_read(struct file *file, char __user *buf,
+               size_t len, loff_t *ppos)
+{
+       struct spufs_attr *attr;
+       size_t size;
+       ssize_t ret;
+
+       attr = file->private_data;
+       if (!attr->get)
+               return -EACCES;
+
+       ret = mutex_lock_interruptible(&attr->mutex);
+       if (ret)
+               return ret;
+
+       if (*ppos) {            /* continued read */
+               size = strlen(attr->get_buf);
+       } else {                /* first read */
+               u64 val;
+               ret = attr->get(attr->data, &val);
+               if (ret)
+                       goto out;
+
+               size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
+                                attr->fmt, (unsigned long long)val);
+       }
+
+       ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
+out:
+       mutex_unlock(&attr->mutex);
+       return ret;
+}
+
+static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
+               size_t len, loff_t *ppos)
+{
+       struct spufs_attr *attr;
+       u64 val;
+       size_t size;
+       ssize_t ret;
+
+       attr = file->private_data;
+       if (!attr->set)
+               return -EACCES;
+
+       ret = mutex_lock_interruptible(&attr->mutex);
+       if (ret)
+               return ret;
+
+       ret = -EFAULT;
+       size = min(sizeof(attr->set_buf) - 1, len);
+       if (copy_from_user(attr->set_buf, buf, size))
+               goto out;
+
+       ret = len; /* claim we got the whole input */
+       attr->set_buf[size] = '\0';
+       val = simple_strtol(attr->set_buf, NULL, 0);
+       attr->set(attr->data, val);
+out:
+       mutex_unlock(&attr->mutex);
+       return ret;
+}
+
+#define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt)     \
+static int __fops ## _open(struct inode *inode, struct file *file)     \
+{                                                                      \
+       __simple_attr_check_format(__fmt, 0ull);                        \
+       return spufs_attr_open(inode, file, __get, __set, __fmt);       \
+}                                                                      \
+static struct file_operations __fops = {                               \
+       .owner   = THIS_MODULE,                                         \
+       .open    = __fops ## _open,                                     \
+       .release = spufs_attr_release,                                  \
+       .read    = spufs_attr_read,                                     \
+       .write   = spufs_attr_write,                                    \
+};
+
+
 static int
 spufs_mem_open(struct inode *inode, struct file *file)
 {
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
        if (!i->i_openers++)
                ctx->local_store = inode->i_mapping;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -59,10 +176,10 @@ spufs_mem_release(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        if (!--i->i_openers)
                ctx->local_store = NULL;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -82,9 +199,12 @@ spufs_mem_read(struct file *file, char __user *buffer,
        struct spu_context *ctx = file->private_data;
        ssize_t ret;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        ret = __spufs_mem_read(ctx, buffer, size, pos);
        spu_release(ctx);
+
        return ret;
 }
 
@@ -104,7 +224,10 @@ spufs_mem_write(struct file *file, const char __user *buffer,
        if (size > LS_SIZE - pos)
                size = LS_SIZE - pos;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
+
        local_store = ctx->ops->get_ls(ctx);
        ret = copy_from_user(local_store + pos, buffer, size);
        spu_release(ctx);
@@ -144,7 +267,8 @@ static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
        pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
                 addr0, address, offset);
 
-       spu_acquire(ctx);
+       if (spu_acquire(ctx))
+               return NOPFN_REFAULT;
 
        if (ctx->state == SPU_STATE_SAVED) {
                vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
@@ -197,9 +321,9 @@ static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
 }
 
 #ifdef CONFIG_SPU_FS_64K_LS
-unsigned long spufs_get_unmapped_area(struct file *file, unsigned long addr,
-                                     unsigned long len, unsigned long pgoff,
-                                     unsigned long flags)
+static unsigned long spufs_get_unmapped_area(struct file *file,
+               unsigned long addr, unsigned long len, unsigned long pgoff,
+               unsigned long flags)
 {
        struct spu_context      *ctx = file->private_data;
        struct spu_state        *csa = &ctx->csa;
@@ -216,11 +340,12 @@ unsigned long spufs_get_unmapped_area(struct file *file, unsigned long addr,
 #endif /* CONFIG_SPU_FS_64K_LS */
 
 static const struct file_operations spufs_mem_fops = {
-       .open                   = spufs_mem_open,
-       .read                   = spufs_mem_read,
-       .write                  = spufs_mem_write,
-       .llseek                 = generic_file_llseek,
-       .mmap                   = spufs_mem_mmap,
+       .open                   = spufs_mem_open,
+       .release                = spufs_mem_release,
+       .read                   = spufs_mem_read,
+       .write                  = spufs_mem_write,
+       .llseek                 = generic_file_llseek,
+       .mmap                   = spufs_mem_mmap,
 #ifdef CONFIG_SPU_FS_64K_LS
        .get_unmapped_area      = spufs_get_unmapped_area,
 #endif
@@ -233,23 +358,39 @@ static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
 {
        struct spu_context *ctx = vma->vm_file->private_data;
        unsigned long area, offset = address - vma->vm_start;
-       int ret;
+       int ret = 0;
+
+       spu_context_nospu_trace(spufs_ps_nopfn__enter, ctx);
 
        offset += vma->vm_pgoff << PAGE_SHIFT;
        if (offset >= ps_size)
                return NOPFN_SIGBUS;
 
-       /* error here usually means a signal.. we might want to test
-        * the error code more precisely though
+       /*
+        * We have to wait for context to be loaded before we have
+        * pages to hand out to the user, but we don't want to wait
+        * with the mmap_sem held.
+        * It is possible to drop the mmap_sem here, but then we need
+        * to return NOPFN_REFAULT because the mappings may have
+        * hanged.
         */
-       ret = spu_acquire_runnable(ctx, 0);
-       if (ret)
+       if (spu_acquire(ctx))
                return NOPFN_REFAULT;
 
-       area = ctx->spu->problem_phys + ps_offs;
-       vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
-       spu_release(ctx);
+       if (ctx->state == SPU_STATE_SAVED) {
+               up_read(&current->mm->mmap_sem);
+               spu_context_nospu_trace(spufs_ps_nopfn__sleep, ctx);
+               ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
+               spu_context_trace(spufs_ps_nopfn__wake, ctx, ctx->spu);
+               down_read(&current->mm->mmap_sem);
+       } else {
+               area = ctx->spu->problem_phys + ps_offs;
+               vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
+               spu_context_trace(spufs_ps_nopfn__insert, ctx, ctx->spu);
+       }
 
+       if (!ret)
+               spu_release(ctx);
        return NOPFN_REFAULT;
 }
 
@@ -283,25 +424,32 @@ static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
 #define spufs_cntl_mmap NULL
 #endif /* !SPUFS_MMAP_4K */
 
-static u64 spufs_cntl_get(void *data)
+static int spufs_cntl_get(void *data, u64 *val)
 {
        struct spu_context *ctx = data;
-       u64 val;
+       int ret;
 
-       spu_acquire(ctx);
-       val = ctx->ops->status_read(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
+       *val = ctx->ops->status_read(ctx);
        spu_release(ctx);
 
-       return val;
+       return 0;
 }
 
-static void spufs_cntl_set(void *data, u64 val)
+static int spufs_cntl_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
+       int ret;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        ctx->ops->runcntl_write(ctx, val);
        spu_release(ctx);
+
+       return 0;
 }
 
 static int spufs_cntl_open(struct inode *inode, struct file *file)
@@ -309,11 +457,11 @@ static int spufs_cntl_open(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
        if (!i->i_openers++)
                ctx->cntl = inode->i_mapping;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return simple_attr_open(inode, file, spufs_cntl_get,
                                        spufs_cntl_set, "0x%08lx");
 }
@@ -324,12 +472,12 @@ spufs_cntl_release(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       simple_attr_close(inode, file);
+       simple_attr_release(inode, file);
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        if (!--i->i_openers)
                ctx->cntl = NULL;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -365,9 +513,11 @@ spufs_regs_read(struct file *file, char __user *buffer,
        int ret;
        struct spu_context *ctx = file->private_data;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        ret = __spufs_regs_read(ctx, buffer, size, pos);
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 
@@ -384,12 +534,14 @@ spufs_regs_write(struct file *file, const char __user *buffer,
                return -EFBIG;
        *pos += size;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
 
        ret = copy_from_user(lscsa->gprs + *pos - size,
                             buffer, size) ? -EFAULT : size;
 
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 
@@ -416,9 +568,11 @@ spufs_fpcr_read(struct file *file, char __user * buffer,
        int ret;
        struct spu_context *ctx = file->private_data;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        ret = __spufs_fpcr_read(ctx, buffer, size, pos);
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 
@@ -433,14 +587,16 @@ spufs_fpcr_write(struct file *file, const char __user * buffer,
        size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
        if (size <= 0)
                return -EFBIG;
-       *pos += size;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
 
+       *pos += size;
        ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
                             buffer, size) ? -EFAULT : size;
 
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 
@@ -483,7 +639,10 @@ static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
 
        udata = (void __user *)buf;
 
-       spu_acquire(ctx);
+       count = spu_acquire(ctx);
+       if (count)
+               return count;
+
        for (count = 0; (count + 4) <= len; count += 4, udata++) {
                int ret;
                ret = ctx->ops->mbox_read(ctx, &mbox_data);
@@ -519,12 +678,15 @@ static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
                        size_t len, loff_t *pos)
 {
        struct spu_context *ctx = file->private_data;
+       ssize_t ret;
        u32 mbox_stat;
 
        if (len < 4)
                return -EINVAL;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
 
        mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
 
@@ -559,6 +721,9 @@ void spufs_ibox_callback(struct spu *spu)
 {
        struct spu_context *ctx = spu->ctx;
 
+       if (!ctx)
+               return;
+
        wake_up_all(&ctx->ibox_wq);
        kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
 }
@@ -590,23 +755,27 @@ static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
 
        udata = (void __user *)buf;
 
-       spu_acquire(ctx);
+       count = spu_acquire(ctx);
+       if (count)
+               goto out;
 
        /* wait only for the first element */
        count = 0;
        if (file->f_flags & O_NONBLOCK) {
-               if (!spu_ibox_read(ctx, &ibox_data))
+               if (!spu_ibox_read(ctx, &ibox_data)) {
                        count = -EAGAIN;
+                       goto out_unlock;
+               }
        } else {
                count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
+               if (count)
+                       goto out;
        }
-       if (count)
-               goto out;
 
        /* if we can't write at all, return -EFAULT */
        count = __put_user(ibox_data, udata);
        if (count)
-               goto out;
+               goto out_unlock;
 
        for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
                int ret;
@@ -623,9 +792,9 @@ static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
                        break;
        }
 
-out:
+out_unlock:
        spu_release(ctx);
-
+out:
        return count;
 }
 
@@ -636,7 +805,11 @@ static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
 
        poll_wait(file, &ctx->ibox_wq, wait);
 
-       spu_acquire(ctx);
+       /*
+        * For now keep this uninterruptible and also ignore the rule
+        * that poll should not sleep.  Will be fixed later.
+        */
+       mutex_lock(&ctx->state_mutex);
        mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
        spu_release(ctx);
 
@@ -654,12 +827,15 @@ static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
                        size_t len, loff_t *pos)
 {
        struct spu_context *ctx = file->private_data;
+       ssize_t ret;
        u32 ibox_stat;
 
        if (len < 4)
                return -EINVAL;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
        spu_release(ctx);
 
@@ -695,6 +871,9 @@ void spufs_wbox_callback(struct spu *spu)
 {
        struct spu_context *ctx = spu->ctx;
 
+       if (!ctx)
+               return;
+
        wake_up_all(&ctx->wbox_wq);
        kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
 }
@@ -728,7 +907,9 @@ static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
        if (__get_user(wbox_data, udata))
                return -EFAULT;
 
-       spu_acquire(ctx);
+       count = spu_acquire(ctx);
+       if (count)
+               goto out;
 
        /*
         * make sure we can at least write one element, by waiting
@@ -736,16 +917,18 @@ static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
         */
        count = 0;
        if (file->f_flags & O_NONBLOCK) {
-               if (!spu_wbox_write(ctx, wbox_data))
+               if (!spu_wbox_write(ctx, wbox_data)) {
                        count = -EAGAIN;
+                       goto out_unlock;
+               }
        } else {
                count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
+               if (count)
+                       goto out;
        }
 
-       if (count)
-               goto out;
 
-       /* write aÑ• much as possible */
+       /* write as much as possible */
        for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
                int ret;
                ret = __get_user(wbox_data, udata);
@@ -757,8 +940,9 @@ static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
                        break;
        }
 
-out:
+out_unlock:
        spu_release(ctx);
+out:
        return count;
 }
 
@@ -769,7 +953,11 @@ static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
 
        poll_wait(file, &ctx->wbox_wq, wait);
 
-       spu_acquire(ctx);
+       /*
+        * For now keep this uninterruptible and also ignore the rule
+        * that poll should not sleep.  Will be fixed later.
+        */
+       mutex_lock(&ctx->state_mutex);
        mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
        spu_release(ctx);
 
@@ -787,12 +975,15 @@ static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
                        size_t len, loff_t *pos)
 {
        struct spu_context *ctx = file->private_data;
+       ssize_t ret;
        u32 wbox_stat;
 
        if (len < 4)
                return -EINVAL;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
        spu_release(ctx);
 
@@ -812,11 +1003,11 @@ static int spufs_signal1_open(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
        if (!i->i_openers++)
                ctx->signal1 = inode->i_mapping;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
@@ -826,10 +1017,10 @@ spufs_signal1_release(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        if (!--i->i_openers)
                ctx->signal1 = NULL;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -863,9 +1054,11 @@ static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
        int ret;
        struct spu_context *ctx = file->private_data;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        ret = __spufs_signal1_read(ctx, buf, len, pos);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -874,6 +1067,7 @@ static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
                        size_t len, loff_t *pos)
 {
        struct spu_context *ctx;
+       ssize_t ret;
        u32 data;
 
        ctx = file->private_data;
@@ -884,7 +1078,9 @@ static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
        if (copy_from_user(&data, buf, 4))
                return -EFAULT;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        ctx->ops->signal1_write(ctx, data);
        spu_release(ctx);
 
@@ -931,16 +1127,23 @@ static const struct file_operations spufs_signal1_fops = {
        .mmap = spufs_signal1_mmap,
 };
 
+static const struct file_operations spufs_signal1_nosched_fops = {
+       .open = spufs_signal1_open,
+       .release = spufs_signal1_release,
+       .write = spufs_signal1_write,
+       .mmap = spufs_signal1_mmap,
+};
+
 static int spufs_signal2_open(struct inode *inode, struct file *file)
 {
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
        if (!i->i_openers++)
                ctx->signal2 = inode->i_mapping;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
@@ -950,10 +1153,10 @@ spufs_signal2_release(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        if (!--i->i_openers)
                ctx->signal2 = NULL;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -987,9 +1190,11 @@ static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
        struct spu_context *ctx = file->private_data;
        int ret;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        ret = __spufs_signal2_read(ctx, buf, len, pos);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -998,6 +1203,7 @@ static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
                        size_t len, loff_t *pos)
 {
        struct spu_context *ctx;
+       ssize_t ret;
        u32 data;
 
        ctx = file->private_data;
@@ -1008,7 +1214,9 @@ static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
        if (copy_from_user(&data, buf, 4))
                return -EFAULT;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        ctx->ops->signal2_write(ctx, data);
        spu_release(ctx);
 
@@ -1059,63 +1267,89 @@ static const struct file_operations spufs_signal2_fops = {
        .mmap = spufs_signal2_mmap,
 };
 
-static void spufs_signal1_type_set(void *data, u64 val)
+static const struct file_operations spufs_signal2_nosched_fops = {
+       .open = spufs_signal2_open,
+       .release = spufs_signal2_release,
+       .write = spufs_signal2_write,
+       .mmap = spufs_signal2_mmap,
+};
+
+/*
+ * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
+ * work of acquiring (or not) the SPU context before calling through
+ * to the actual get routine. The set routine is called directly.
+ */
+#define SPU_ATTR_NOACQUIRE     0
+#define SPU_ATTR_ACQUIRE       1
+#define SPU_ATTR_ACQUIRE_SAVED 2
+
+#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
+static int __##__get(void *data, u64 *val)                             \
+{                                                                      \
+       struct spu_context *ctx = data;                                 \
+       int ret = 0;                                                    \
+                                                                       \
+       if (__acquire == SPU_ATTR_ACQUIRE) {                            \
+               ret = spu_acquire(ctx);                                 \
+               if (ret)                                                \
+                       return ret;                                     \
+               *val = __get(ctx);                                      \
+               spu_release(ctx);                                       \
+       } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) {               \
+               ret = spu_acquire_saved(ctx);                           \
+               if (ret)                                                \
+                       return ret;                                     \
+               *val = __get(ctx);                                      \
+               spu_release_saved(ctx);                                 \
+       } else                                                          \
+               *val = __get(ctx);                                      \
+                                                                       \
+       return 0;                                                       \
+}                                                                      \
+DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
+
+static int spufs_signal1_type_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
+       int ret;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        ctx->ops->signal1_type_set(ctx, val);
        spu_release(ctx);
+
+       return 0;
 }
 
-static u64 __spufs_signal1_type_get(void *data)
+static u64 spufs_signal1_type_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
        return ctx->ops->signal1_type_get(ctx);
 }
+DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
+                      spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
 
-static u64 spufs_signal1_type_get(void *data)
-{
-       struct spu_context *ctx = data;
-       u64 ret;
 
-       spu_acquire(ctx);
-       ret = __spufs_signal1_type_get(data);
-       spu_release(ctx);
-
-       return ret;
-}
-DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
-                                       spufs_signal1_type_set, "%llu");
-
-static void spufs_signal2_type_set(void *data, u64 val)
+static int spufs_signal2_type_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
+       int ret;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        ctx->ops->signal2_type_set(ctx, val);
        spu_release(ctx);
-}
 
-static u64 __spufs_signal2_type_get(void *data)
-{
-       struct spu_context *ctx = data;
-       return ctx->ops->signal2_type_get(ctx);
+       return 0;
 }
 
-static u64 spufs_signal2_type_get(void *data)
+static u64 spufs_signal2_type_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
-       u64 ret;
-
-       spu_acquire(ctx);
-       ret = __spufs_signal2_type_get(data);
-       spu_release(ctx);
-
-       return ret;
+       return ctx->ops->signal2_type_get(ctx);
 }
-DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
-                                       spufs_signal2_type_set, "%llu");
+DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
+                      spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
 
 #if SPUFS_MMAP_4K
 static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
@@ -1154,10 +1388,10 @@ static int spufs_mss_open(struct inode *inode, struct file *file)
 
        file->private_data = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        if (!i->i_openers++)
                ctx->mss = inode->i_mapping;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
@@ -1167,10 +1401,10 @@ spufs_mss_release(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        if (!--i->i_openers)
                ctx->mss = NULL;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -1211,11 +1445,11 @@ static int spufs_psmap_open(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = i->i_ctx;
        if (!i->i_openers++)
                ctx->psmap = inode->i_mapping;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
@@ -1225,10 +1459,10 @@ spufs_psmap_release(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        if (!--i->i_openers)
                ctx->psmap = NULL;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -1281,11 +1515,11 @@ static int spufs_mfc_open(struct inode *inode, struct file *file)
        if (atomic_read(&inode->i_count) != 1)
                return -EBUSY;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
        if (!i->i_openers++)
                ctx->mfc = inode->i_mapping;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
@@ -1295,10 +1529,10 @@ spufs_mfc_release(struct inode *inode, struct file *file)
        struct spufs_inode_info *i = SPUFS_I(inode);
        struct spu_context *ctx = i->i_ctx;
 
-       spin_lock(&ctx->mapping_lock);
+       mutex_lock(&ctx->mapping_lock);
        if (!--i->i_openers)
                ctx->mfc = NULL;
-       spin_unlock(&ctx->mapping_lock);
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -1307,6 +1541,9 @@ void spufs_mfc_callback(struct spu *spu)
 {
        struct spu_context *ctx = spu->ctx;
 
+       if (!ctx)
+               return;
+
        wake_up_all(&ctx->mfc_wq);
 
        pr_debug("%s %s\n", __FUNCTION__, spu->name);
@@ -1353,22 +1590,26 @@ static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
        if (size != 4)
                goto out;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
+
+       ret = -EINVAL;
        if (file->f_flags & O_NONBLOCK) {
                status = ctx->ops->read_mfc_tagstatus(ctx);
                if (!(status & ctx->tagwait))
                        ret = -EAGAIN;
                else
+                       /* XXX(hch): shouldn't we clear ret here? */
                        ctx->tagwait &= ~status;
        } else {
                ret = spufs_wait(ctx->mfc_wq,
                           spufs_read_mfc_tagstatus(ctx, &status));
+               if (ret)
+                       goto out;
        }
        spu_release(ctx);
 
-       if (ret)
-               goto out;
-
        ret = 4;
        if (copy_to_user(buffer, &status, 4))
                ret = -EFAULT;
@@ -1483,7 +1724,11 @@ static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
        if (ret)
                goto out;
 
-       ret = spu_acquire_runnable(ctx, 0);
+       ret = spu_acquire(ctx);
+       if (ret)
+               goto out;
+
+       ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
        if (ret)
                goto out;
 
@@ -1493,17 +1738,20 @@ static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
                int status;
                ret = spufs_wait(ctx->mfc_wq,
                                 spu_send_mfc_command(ctx, cmd, &status));
+               if (ret)
+                       goto out;
                if (status)
                        ret = status;
        }
-       spu_release(ctx);
 
        if (ret)
-               goto out;
+               goto out_unlock;
 
        ctx->tagwait |= 1 << cmd.tag;
        ret = size;
 
+out_unlock:
+       spu_release(ctx);
 out:
        return ret;
 }
@@ -1514,14 +1762,18 @@ static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
        u32 free_elements, tagstatus;
        unsigned int mask;
 
-       spu_acquire(ctx);
+       poll_wait(file, &ctx->mfc_wq, wait);
+
+       /*
+        * For now keep this uninterruptible and also ignore the rule
+        * that poll should not sleep.  Will be fixed later.
+        */
+       mutex_lock(&ctx->state_mutex);
        ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
        free_elements = ctx->ops->get_mfc_free_elements(ctx);
        tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
        spu_release(ctx);
 
-       poll_wait(file, &ctx->mfc_wq, wait);
-
        mask = 0;
        if (free_elements & 0xffff)
                mask |= POLLOUT | POLLWRNORM;
@@ -1539,7 +1791,9 @@ static int spufs_mfc_flush(struct file *file, fl_owner_t id)
        struct spu_context *ctx = file->private_data;
        int ret;
 
-       spu_acquire(ctx);
+       ret = spu_acquire(ctx);
+       if (ret)
+               goto out;
 #if 0
 /* this currently hangs */
        ret = spufs_wait(ctx->mfc_wq,
@@ -1548,12 +1802,13 @@ static int spufs_mfc_flush(struct file *file, fl_owner_t id)
                goto out;
        ret = spufs_wait(ctx->mfc_wq,
                         ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
-out:
+       if (ret)
+               goto out;
 #else
        ret = 0;
 #endif
        spu_release(ctx);
-
+out:
        return ret;
 }
 
@@ -1582,113 +1837,105 @@ static const struct file_operations spufs_mfc_fops = {
        .mmap    = spufs_mfc_mmap,
 };
 
-static void spufs_npc_set(void *data, u64 val)
+static int spufs_npc_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
-       spu_acquire(ctx);
+       int ret;
+
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
        ctx->ops->npc_write(ctx, val);
        spu_release(ctx);
+
+       return 0;
 }
 
-static u64 spufs_npc_get(void *data)
+static u64 spufs_npc_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
-       u64 ret;
-       spu_acquire(ctx);
-       ret = ctx->ops->npc_read(ctx);
-       spu_release(ctx);
-       return ret;
+       return ctx->ops->npc_read(ctx);
 }
-DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
-                       "0x%llx\n")
+DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
+                      "0x%llx\n", SPU_ATTR_ACQUIRE);
 
-static void spufs_decr_set(void *data, u64 val)
+static int spufs_decr_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
-       spu_acquire_saved(ctx);
+       int ret;
+
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        lscsa->decr.slot[0] = (u32) val;
-       spu_release(ctx);
+       spu_release_saved(ctx);
+
+       return 0;
 }
 
-static u64 __spufs_decr_get(void *data)
+static u64 spufs_decr_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
        return lscsa->decr.slot[0];
 }
+DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
+                      "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
 
-static u64 spufs_decr_get(void *data)
+static int spufs_decr_status_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
-       u64 ret;
-       spu_acquire_saved(ctx);
-       ret = __spufs_decr_get(data);
-       spu_release(ctx);
-       return ret;
-}
-DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
-                       "0x%llx\n")
+       int ret;
 
-static void spufs_decr_status_set(void *data, u64 val)
-{
-       struct spu_context *ctx = data;
-       struct spu_lscsa *lscsa = ctx->csa.lscsa;
-       spu_acquire_saved(ctx);
-       lscsa->decr_status.slot[0] = (u32) val;
-       spu_release(ctx);
-}
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
+       if (val)
+               ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
+       else
+               ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
+       spu_release_saved(ctx);
 
-static u64 __spufs_decr_status_get(void *data)
-{
-       struct spu_context *ctx = data;
-       struct spu_lscsa *lscsa = ctx->csa.lscsa;
-       return lscsa->decr_status.slot[0];
+       return 0;
 }
 
-static u64 spufs_decr_status_get(void *data)
+static u64 spufs_decr_status_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
-       u64 ret;
-       spu_acquire_saved(ctx);
-       ret = __spufs_decr_status_get(data);
-       spu_release(ctx);
-       return ret;
+       if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
+               return SPU_DECR_STATUS_RUNNING;
+       else
+               return 0;
 }
-DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
-                       spufs_decr_status_set, "0x%llx\n")
+DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
+                      spufs_decr_status_set, "0x%llx\n",
+                      SPU_ATTR_ACQUIRE_SAVED);
 
-static void spufs_event_mask_set(void *data, u64 val)
+static int spufs_event_mask_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
-       spu_acquire_saved(ctx);
+       int ret;
+
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        lscsa->event_mask.slot[0] = (u32) val;
-       spu_release(ctx);
+       spu_release_saved(ctx);
+
+       return 0;
 }
 
-static u64 __spufs_event_mask_get(void *data)
+static u64 spufs_event_mask_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
        return lscsa->event_mask.slot[0];
 }
 
-static u64 spufs_event_mask_get(void *data)
-{
-       struct spu_context *ctx = data;
-       u64 ret;
-       spu_acquire_saved(ctx);
-       ret = __spufs_event_mask_get(data);
-       spu_release(ctx);
-       return ret;
-}
-DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
-                       spufs_event_mask_set, "0x%llx\n")
+DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
+                      spufs_event_mask_set, "0x%llx\n",
+                      SPU_ATTR_ACQUIRE_SAVED);
 
-static u64 __spufs_event_status_get(void *data)
+static u64 spufs_event_status_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
        struct spu_state *state = &ctx->csa;
        u64 stat;
        stat = state->spu_chnlcnt_RW[0];
@@ -1696,97 +1943,69 @@ static u64 __spufs_event_status_get(void *data)
                return state->spu_chnldata_RW[0];
        return 0;
 }
+DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
+                      NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
 
-static u64 spufs_event_status_get(void *data)
-{
-       struct spu_context *ctx = data;
-       u64 ret = 0;
-
-       spu_acquire_saved(ctx);
-       ret = __spufs_event_status_get(data);
-       spu_release(ctx);
-       return ret;
-}
-DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
-                       NULL, "0x%llx\n")
-
-static void spufs_srr0_set(void *data, u64 val)
+static int spufs_srr0_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
-       spu_acquire_saved(ctx);
+       int ret;
+
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        lscsa->srr0.slot[0] = (u32) val;
-       spu_release(ctx);
+       spu_release_saved(ctx);
+
+       return 0;
 }
 
-static u64 spufs_srr0_get(void *data)
+static u64 spufs_srr0_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
-       u64 ret;
-       spu_acquire_saved(ctx);
-       ret = lscsa->srr0.slot[0];
-       spu_release(ctx);
-       return ret;
+       return lscsa->srr0.slot[0];
 }
-DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
-                       "0x%llx\n")
+DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
+                      "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
 
-static u64 spufs_id_get(void *data)
+static u64 spufs_id_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
        u64 num;
 
-       spu_acquire(ctx);
        if (ctx->state == SPU_STATE_RUNNABLE)
                num = ctx->spu->number;
        else
                num = (unsigned int)-1;
-       spu_release(ctx);
 
        return num;
 }
-DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
-
-static u64 __spufs_object_id_get(void *data)
-{
-       struct spu_context *ctx = data;
-       return ctx->object_id;
-}
+DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
+                      SPU_ATTR_ACQUIRE)
 
-static u64 spufs_object_id_get(void *data)
+static u64 spufs_object_id_get(struct spu_context *ctx)
 {
        /* FIXME: Should there really be no locking here? */
-       return __spufs_object_id_get(data);
+       return ctx->object_id;
 }
 
-static void spufs_object_id_set(void *data, u64 id)
+static int spufs_object_id_set(void *data, u64 id)
 {
        struct spu_context *ctx = data;
        ctx->object_id = id;
+
+       return 0;
 }
 
-DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
-               spufs_object_id_set, "0x%llx\n");
+DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
+                      spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
 
-static u64 __spufs_lslr_get(void *data)
+static u64 spufs_lslr_get(struct spu_context *ctx)
 {
-       struct spu_context *ctx = data;
        return ctx->csa.priv2.spu_lslr_RW;
 }
-
-static u64 spufs_lslr_get(void *data)
-{
-       struct spu_context *ctx = data;
-       u64 ret;
-
-       spu_acquire_saved(ctx);
-       ret = __spufs_lslr_get(data);
-       spu_release(ctx);
-
-       return ret;
-}
-DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
+DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
+                      SPU_ATTR_ACQUIRE_SAVED);
 
 static int spufs_info_open(struct inode *inode, struct file *file)
 {
@@ -1796,16 +2015,39 @@ static int spufs_info_open(struct inode *inode, struct file *file)
        return 0;
 }
 
+static int spufs_caps_show(struct seq_file *s, void *private)
+{
+       struct spu_context *ctx = s->private;
+
+       if (!(ctx->flags & SPU_CREATE_NOSCHED))
+               seq_puts(s, "sched\n");
+       if (!(ctx->flags & SPU_CREATE_ISOLATE))
+               seq_puts(s, "step\n");
+       return 0;
+}
+
+static int spufs_caps_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
+}
+
+static const struct file_operations spufs_caps_fops = {
+       .open           = spufs_caps_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+};
+
 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
                        char __user *buf, size_t len, loff_t *pos)
 {
-       u32 mbox_stat;
        u32 data;
 
-       mbox_stat = ctx->csa.prob.mb_stat_R;
-       if (mbox_stat & 0x0000ff) {
-               data = ctx->csa.prob.pu_mb_R;
-       }
+       /* EOF if there's no entry in the mbox */
+       if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
+               return 0;
+
+       data = ctx->csa.prob.pu_mb_R;
 
        return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
 }
@@ -1819,11 +2061,13 @@ static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
        if (!access_ok(VERIFY_WRITE, buf, len))
                return -EFAULT;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        spin_lock(&ctx->csa.register_lock);
        ret = __spufs_mbox_info_read(ctx, buf, len, pos);
        spin_unlock(&ctx->csa.register_lock);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -1837,13 +2081,13 @@ static const struct file_operations spufs_mbox_info_fops = {
 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
                                char __user *buf, size_t len, loff_t *pos)
 {
-       u32 ibox_stat;
        u32 data;
 
-       ibox_stat = ctx->csa.prob.mb_stat_R;
-       if (ibox_stat & 0xff0000) {
-               data = ctx->csa.priv2.puint_mb_R;
-       }
+       /* EOF if there's no entry in the ibox */
+       if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
+               return 0;
+
+       data = ctx->csa.priv2.puint_mb_R;
 
        return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
 }
@@ -1857,11 +2101,13 @@ static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
        if (!access_ok(VERIFY_WRITE, buf, len))
                return -EFAULT;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        spin_lock(&ctx->csa.register_lock);
        ret = __spufs_ibox_info_read(ctx, buf, len, pos);
        spin_unlock(&ctx->csa.register_lock);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -1898,11 +2144,13 @@ static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
        if (!access_ok(VERIFY_WRITE, buf, len))
                return -EFAULT;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        spin_lock(&ctx->csa.register_lock);
        ret = __spufs_wbox_info_read(ctx, buf, len, pos);
        spin_unlock(&ctx->csa.register_lock);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -1948,11 +2196,13 @@ static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
        if (!access_ok(VERIFY_WRITE, buf, len))
                return -EFAULT;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        spin_lock(&ctx->csa.register_lock);
        ret = __spufs_dma_info_read(ctx, buf, len, pos);
        spin_unlock(&ctx->csa.register_lock);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -1999,11 +2249,13 @@ static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
        struct spu_context *ctx = file->private_data;
        int ret;
 
-       spu_acquire_saved(ctx);
+       ret = spu_acquire_saved(ctx);
+       if (ret)
+               return ret;
        spin_lock(&ctx->csa.register_lock);
        ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
        spin_unlock(&ctx->csa.register_lock);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -2013,7 +2265,121 @@ static const struct file_operations spufs_proxydma_info_fops = {
        .read = spufs_proxydma_info_read,
 };
 
+static int spufs_show_tid(struct seq_file *s, void *private)
+{
+       struct spu_context *ctx = s->private;
+
+       seq_printf(s, "%d\n", ctx->tid);
+       return 0;
+}
+
+static int spufs_tid_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
+}
+
+static const struct file_operations spufs_tid_fops = {
+       .open           = spufs_tid_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+};
+
+static const char *ctx_state_names[] = {
+       "user", "system", "iowait", "loaded"
+};
+
+static unsigned long long spufs_acct_time(struct spu_context *ctx,
+               enum spu_utilization_state state)
+{
+       struct timespec ts;
+       unsigned long long time = ctx->stats.times[state];
+
+       /*
+        * In general, utilization statistics are updated by the controlling
+        * thread as the spu context moves through various well defined
+        * state transitions, but if the context is lazily loaded its
+        * utilization statistics are not updated as the controlling thread
+        * is not tightly coupled with the execution of the spu context.  We
+        * calculate and apply the time delta from the last recorded state
+        * of the spu context.
+        */
+       if (ctx->spu && ctx->stats.util_state == state) {
+               ktime_get_ts(&ts);
+               time += timespec_to_ns(&ts) - ctx->stats.tstamp;
+       }
+
+       return time / NSEC_PER_MSEC;
+}
+
+static unsigned long long spufs_slb_flts(struct spu_context *ctx)
+{
+       unsigned long long slb_flts = ctx->stats.slb_flt;
+
+       if (ctx->state == SPU_STATE_RUNNABLE) {
+               slb_flts += (ctx->spu->stats.slb_flt -
+                            ctx->stats.slb_flt_base);
+       }
+
+       return slb_flts;
+}
+
+static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
+{
+       unsigned long long class2_intrs = ctx->stats.class2_intr;
+
+       if (ctx->state == SPU_STATE_RUNNABLE) {
+               class2_intrs += (ctx->spu->stats.class2_intr -
+                                ctx->stats.class2_intr_base);
+       }
+
+       return class2_intrs;
+}
+
+
+static int spufs_show_stat(struct seq_file *s, void *private)
+{
+       struct spu_context *ctx = s->private;
+       int ret;
+
+       ret = spu_acquire(ctx);
+       if (ret)
+               return ret;
+
+       seq_printf(s, "%s %llu %llu %llu %llu "
+                     "%llu %llu %llu %llu %llu %llu %llu %llu\n",
+               ctx_state_names[ctx->stats.util_state],
+               spufs_acct_time(ctx, SPU_UTIL_USER),
+               spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
+               spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
+               spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
+               ctx->stats.vol_ctx_switch,
+               ctx->stats.invol_ctx_switch,
+               spufs_slb_flts(ctx),
+               ctx->stats.hash_flt,
+               ctx->stats.min_flt,
+               ctx->stats.maj_flt,
+               spufs_class2_intrs(ctx),
+               ctx->stats.libassist);
+       spu_release(ctx);
+       return 0;
+}
+
+static int spufs_stat_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
+}
+
+static const struct file_operations spufs_stat_fops = {
+       .open           = spufs_stat_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+};
+
+
 struct tree_descr spufs_dir_contents[] = {
+       { "capabilities", &spufs_caps_fops, 0444, },
        { "mem",  &spufs_mem_fops,  0666, },
        { "regs", &spufs_regs_fops,  0666, },
        { "mbox", &spufs_mbox_fops, 0444, },
@@ -2045,10 +2411,13 @@ struct tree_descr spufs_dir_contents[] = {
        { "wbox_info", &spufs_wbox_info_fops, 0444, },
        { "dma_info", &spufs_dma_info_fops, 0444, },
        { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
+       { "tid", &spufs_tid_fops, 0444, },
+       { "stat", &spufs_stat_fops, 0444, },
        {},
 };
 
 struct tree_descr spufs_dir_nosched_contents[] = {
+       { "capabilities", &spufs_caps_fops, 0444, },
        { "mem",  &spufs_mem_fops,  0666, },
        { "mbox", &spufs_mbox_fops, 0444, },
        { "ibox", &spufs_ibox_fops, 0444, },
@@ -2056,8 +2425,8 @@ struct tree_descr spufs_dir_nosched_contents[] = {
        { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
        { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
        { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
-       { "signal1", &spufs_signal1_fops, 0666, },
-       { "signal2", &spufs_signal2_fops, 0666, },
+       { "signal1", &spufs_signal1_nosched_fops, 0222, },
+       { "signal2", &spufs_signal2_nosched_fops, 0222, },
        { "signal1_type", &spufs_signal1_type, 0666, },
        { "signal2_type", &spufs_signal2_type, 0666, },
        { "mss", &spufs_mss_fops, 0666, },
@@ -2067,29 +2436,31 @@ struct tree_descr spufs_dir_nosched_contents[] = {
        { "psmap", &spufs_psmap_fops, 0666, },
        { "phys-id", &spufs_id_ops, 0666, },
        { "object-id", &spufs_object_id_ops, 0666, },
+       { "tid", &spufs_tid_fops, 0444, },
+       { "stat", &spufs_stat_fops, 0444, },
        {},
 };
 
 struct spufs_coredump_reader spufs_coredump_read[] = {
-       { "regs", __spufs_regs_read, NULL, 128 * 16 },
-       { "fpcr", __spufs_fpcr_read, NULL, 16 },
-       { "lslr", NULL, __spufs_lslr_get, 11 },
-       { "decr", NULL, __spufs_decr_get, 11 },
-       { "decr_status", NULL, __spufs_decr_status_get, 11 },
-       { "mem", __spufs_mem_read, NULL, 256 * 1024, },
-       { "signal1", __spufs_signal1_read, NULL, 4 },
-       { "signal1_type", NULL, __spufs_signal1_type_get, 2 },
-       { "signal2", __spufs_signal2_read, NULL, 4 },
-       { "signal2_type", NULL, __spufs_signal2_type_get, 2 },
-       { "event_mask", NULL, __spufs_event_mask_get, 8 },
-       { "event_status", NULL, __spufs_event_status_get, 8 },
-       { "mbox_info", __spufs_mbox_info_read, NULL, 4 },
-       { "ibox_info", __spufs_ibox_info_read, NULL, 4 },
-       { "wbox_info", __spufs_wbox_info_read, NULL, 16 },
-       { "dma_info", __spufs_dma_info_read, NULL, 69 * 8 },
-       { "proxydma_info", __spufs_proxydma_info_read, NULL, 35 * 8 },
-       { "object-id", NULL, __spufs_object_id_get, 19 },
-       { },
+       { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
+       { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
+       { "lslr", NULL, spufs_lslr_get, 19 },
+       { "decr", NULL, spufs_decr_get, 19 },
+       { "decr_status", NULL, spufs_decr_status_get, 19 },
+       { "mem", __spufs_mem_read, NULL, LS_SIZE, },
+       { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
+       { "signal1_type", NULL, spufs_signal1_type_get, 19 },
+       { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
+       { "signal2_type", NULL, spufs_signal2_type_get, 19 },
+       { "event_mask", NULL, spufs_event_mask_get, 19 },
+       { "event_status", NULL, spufs_event_status_get, 19 },
+       { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
+       { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
+       { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
+       { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
+       { "proxydma_info", __spufs_proxydma_info_read,
+                          NULL, sizeof(struct spu_proxydma_info)},
+       { "object-id", NULL, spufs_object_id_get, 19 },
+       { "npc", NULL, spufs_npc_get, 19 },
+       { NULL },
 };
-int spufs_coredump_num_notes = ARRAY_SIZE(spufs_coredump_read) - 1;
-