]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - arch/powerpc/platforms/cell/spufs/file.c
[CELL] spufs: make sure context are scheduled again after spu_acquire_saved
[mv-sheeva.git] / arch / powerpc / platforms / cell / spufs / file.c
index 505266a568d4221d9305c9e4ef1b0e99cc3ffc88..88da996f6d2f1b2d8876e1208e57c52140677c84 100644 (file)
@@ -28,6 +28,7 @@
 #include <linux/pagemap.h>
 #include <linux/poll.h>
 #include <linux/ptrace.h>
+#include <linux/seq_file.h>
 
 #include <asm/io.h>
 #include <asm/semaphore.h>
 
 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
 
+
 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;
+
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
-       ctx->local_store = inode->i_mapping;
-       smp_wmb();
+       if (!i->i_openers++)
+               ctx->local_store = inode->i_mapping;
+       mutex_unlock(&ctx->mapping_lock);
+       return 0;
+}
+
+static int
+spufs_mem_release(struct inode *inode, struct file *file)
+{
+       struct spufs_inode_info *i = SPUFS_I(inode);
+       struct spu_context *ctx = i->i_ctx;
+
+       mutex_lock(&ctx->mapping_lock);
+       if (!--i->i_openers)
+               ctx->local_store = NULL;
+       mutex_unlock(&ctx->mapping_lock);
        return 0;
 }
 
@@ -102,14 +120,32 @@ spufs_mem_write(struct file *file, const char __user *buffer,
 static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
                                          unsigned long address)
 {
-       struct spu_context *ctx = vma->vm_file->private_data;
-       unsigned long pfn, offset = address - vma->vm_start;
+       struct spu_context *ctx = vma->vm_file->private_data;
+       unsigned long pfn, offset, addr0 = address;
+#ifdef CONFIG_SPU_FS_64K_LS
+       struct spu_state *csa = &ctx->csa;
+       int psize;
 
-       offset += vma->vm_pgoff << PAGE_SHIFT;
+       /* Check what page size we are using */
+       psize = get_slice_psize(vma->vm_mm, address);
 
+       /* Some sanity checking */
+       BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
+
+       /* Wow, 64K, cool, we need to align the address though */
+       if (csa->use_big_pages) {
+               BUG_ON(vma->vm_start & 0xffff);
+               address &= ~0xfffful;
+       }
+#endif /* CONFIG_SPU_FS_64K_LS */
+
+       offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
        if (offset >= LS_SIZE)
                return NOPFN_SIGBUS;
 
+       pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
+                addr0, address, offset);
+
        spu_acquire(ctx);
 
        if (ctx->state == SPU_STATE_SAVED) {
@@ -133,9 +169,24 @@ static struct vm_operations_struct spufs_mem_mmap_vmops = {
        .nopfn = spufs_mem_mmap_nopfn,
 };
 
-static int
-spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
-{
+static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+#ifdef CONFIG_SPU_FS_64K_LS
+       struct spu_context      *ctx = file->private_data;
+       struct spu_state        *csa = &ctx->csa;
+
+       /* Sanity check VMA alignment */
+       if (csa->use_big_pages) {
+               pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
+                        " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
+                        vma->vm_pgoff);
+               if (vma->vm_start & 0xffff)
+                       return -EINVAL;
+               if (vma->vm_pgoff & 0xf)
+                       return -EINVAL;
+       }
+#endif /* CONFIG_SPU_FS_64K_LS */
+
        if (!(vma->vm_flags & VM_SHARED))
                return -EINVAL;
 
@@ -147,12 +198,35 @@ spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
        return 0;
 }
 
+#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)
+{
+       struct spu_context      *ctx = file->private_data;
+       struct spu_state        *csa = &ctx->csa;
+
+       /* If not using big pages, fallback to normal MM g_u_a */
+       if (!csa->use_big_pages)
+               return current->mm->get_unmapped_area(file, addr, len,
+                                                     pgoff, flags);
+
+       /* Else, try to obtain a 64K pages slice */
+       return slice_get_unmapped_area(addr, len, flags,
+                                      MMU_PAGE_64K, 1, 0);
+}
+#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
 };
 
 static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
@@ -238,16 +312,33 @@ 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;
 
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
-       ctx->cntl = inode->i_mapping;
-       smp_wmb();
+       if (!i->i_openers++)
+               ctx->cntl = inode->i_mapping;
+       mutex_unlock(&ctx->mapping_lock);
        return simple_attr_open(inode, file, spufs_cntl_get,
                                        spufs_cntl_set, "0x%08lx");
 }
 
+static int
+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);
+
+       mutex_lock(&ctx->mapping_lock);
+       if (!--i->i_openers)
+               ctx->cntl = NULL;
+       mutex_unlock(&ctx->mapping_lock);
+       return 0;
+}
+
 static const struct file_operations spufs_cntl_fops = {
        .open = spufs_cntl_open,
-       .release = simple_attr_close,
+       .release = spufs_cntl_release,
        .read = simple_attr_read,
        .write = simple_attr_write,
        .mmap = spufs_cntl_mmap,
@@ -279,7 +370,7 @@ spufs_regs_read(struct file *file, char __user *buffer,
 
        spu_acquire_saved(ctx);
        ret = __spufs_regs_read(ctx, buffer, size, pos);
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 
@@ -301,7 +392,7 @@ spufs_regs_write(struct file *file, const char __user *buffer,
        ret = copy_from_user(lscsa->gprs + *pos - size,
                             buffer, size) ? -EFAULT : size;
 
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 
@@ -330,7 +421,7 @@ spufs_fpcr_read(struct file *file, char __user * buffer,
 
        spu_acquire_saved(ctx);
        ret = __spufs_fpcr_read(ctx, buffer, size, pos);
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 
@@ -352,7 +443,7 @@ spufs_fpcr_write(struct file *file, const char __user * buffer,
        ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
                             buffer, size) ? -EFAULT : size;
 
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 
@@ -723,12 +814,28 @@ 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;
+
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
-       ctx->signal1 = inode->i_mapping;
-       smp_wmb();
+       if (!i->i_openers++)
+               ctx->signal1 = inode->i_mapping;
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
+static int
+spufs_signal1_release(struct inode *inode, struct file *file)
+{
+       struct spufs_inode_info *i = SPUFS_I(inode);
+       struct spu_context *ctx = i->i_ctx;
+
+       mutex_lock(&ctx->mapping_lock);
+       if (!--i->i_openers)
+               ctx->signal1 = NULL;
+       mutex_unlock(&ctx->mapping_lock);
+       return 0;
+}
+
 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
                        size_t len, loff_t *pos)
 {
@@ -761,7 +868,7 @@ static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
 
        spu_acquire_saved(ctx);
        ret = __spufs_signal1_read(ctx, buf, len, pos);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -821,21 +928,45 @@ static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
 
 static const struct file_operations spufs_signal1_fops = {
        .open = spufs_signal1_open,
+       .release = spufs_signal1_release,
        .read = spufs_signal1_read,
        .write = spufs_signal1_write,
        .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;
+
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
-       ctx->signal2 = inode->i_mapping;
-       smp_wmb();
+       if (!i->i_openers++)
+               ctx->signal2 = inode->i_mapping;
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
+static int
+spufs_signal2_release(struct inode *inode, struct file *file)
+{
+       struct spufs_inode_info *i = SPUFS_I(inode);
+       struct spu_context *ctx = i->i_ctx;
+
+       mutex_lock(&ctx->mapping_lock);
+       if (!--i->i_openers)
+               ctx->signal2 = NULL;
+       mutex_unlock(&ctx->mapping_lock);
+       return 0;
+}
+
 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
                        size_t len, loff_t *pos)
 {
@@ -868,7 +999,7 @@ static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
 
        spu_acquire_saved(ctx);
        ret = __spufs_signal2_read(ctx, buf, len, pos);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -932,11 +1063,19 @@ static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
 
 static const struct file_operations spufs_signal2_fops = {
        .open = spufs_signal2_open,
+       .release = spufs_signal2_release,
        .read = spufs_signal2_read,
        .write = spufs_signal2_write,
        .mmap = spufs_signal2_mmap,
 };
 
+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,
+};
+
 static void spufs_signal1_type_set(void *data, u64 val)
 {
        struct spu_context *ctx = data;
@@ -1031,13 +1170,30 @@ static int spufs_mss_open(struct inode *inode, struct file *file)
        struct spu_context *ctx = i->i_ctx;
 
        file->private_data = i->i_ctx;
-       ctx->mss = inode->i_mapping;
-       smp_wmb();
+
+       mutex_lock(&ctx->mapping_lock);
+       if (!i->i_openers++)
+               ctx->mss = inode->i_mapping;
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
+static int
+spufs_mss_release(struct inode *inode, struct file *file)
+{
+       struct spufs_inode_info *i = SPUFS_I(inode);
+       struct spu_context *ctx = i->i_ctx;
+
+       mutex_lock(&ctx->mapping_lock);
+       if (!--i->i_openers)
+               ctx->mss = NULL;
+       mutex_unlock(&ctx->mapping_lock);
+       return 0;
+}
+
 static const struct file_operations spufs_mss_fops = {
        .open    = spufs_mss_open,
+       .release = spufs_mss_release,
        .mmap    = spufs_mss_mmap,
 };
 
@@ -1072,14 +1228,30 @@ 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;
 
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = i->i_ctx;
-       ctx->psmap = inode->i_mapping;
-       smp_wmb();
+       if (!i->i_openers++)
+               ctx->psmap = inode->i_mapping;
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
+static int
+spufs_psmap_release(struct inode *inode, struct file *file)
+{
+       struct spufs_inode_info *i = SPUFS_I(inode);
+       struct spu_context *ctx = i->i_ctx;
+
+       mutex_lock(&ctx->mapping_lock);
+       if (!--i->i_openers)
+               ctx->psmap = NULL;
+       mutex_unlock(&ctx->mapping_lock);
+       return 0;
+}
+
 static const struct file_operations spufs_psmap_fops = {
        .open    = spufs_psmap_open,
+       .release = spufs_psmap_release,
        .mmap    = spufs_psmap_mmap,
 };
 
@@ -1126,12 +1298,27 @@ static int spufs_mfc_open(struct inode *inode, struct file *file)
        if (atomic_read(&inode->i_count) != 1)
                return -EBUSY;
 
+       mutex_lock(&ctx->mapping_lock);
        file->private_data = ctx;
-       ctx->mfc = inode->i_mapping;
-       smp_wmb();
+       if (!i->i_openers++)
+               ctx->mfc = inode->i_mapping;
+       mutex_unlock(&ctx->mapping_lock);
        return nonseekable_open(inode, file);
 }
 
+static int
+spufs_mfc_release(struct inode *inode, struct file *file)
+{
+       struct spufs_inode_info *i = SPUFS_I(inode);
+       struct spu_context *ctx = i->i_ctx;
+
+       mutex_lock(&ctx->mapping_lock);
+       if (!--i->i_openers)
+               ctx->mfc = NULL;
+       mutex_unlock(&ctx->mapping_lock);
+       return 0;
+}
+
 /* interrupt-level mfc callback function. */
 void spufs_mfc_callback(struct spu *spu)
 {
@@ -1313,7 +1500,10 @@ static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
        if (ret)
                goto out;
 
-       spu_acquire_runnable(ctx, 0);
+       ret = spu_acquire_runnable(ctx, 0);
+       if (ret)
+               goto out;
+
        if (file->f_flags & O_NONBLOCK) {
                ret = ctx->ops->send_mfc_command(ctx, &cmd);
        } else {
@@ -1323,14 +1513,15 @@ static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
                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;
 }
@@ -1341,14 +1532,14 @@ static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
        u32 free_elements, tagstatus;
        unsigned int mask;
 
+       poll_wait(file, &ctx->mfc_wq, wait);
+
        spu_acquire(ctx);
        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;
@@ -1399,6 +1590,7 @@ static int spufs_mfc_fasync(int fd, struct file *file, int on)
 
 static const struct file_operations spufs_mfc_fops = {
        .open    = spufs_mfc_open,
+       .release = spufs_mfc_release,
        .read    = spufs_mfc_read,
        .write   = spufs_mfc_write,
        .poll    = spufs_mfc_poll,
@@ -1434,7 +1626,7 @@ static void spufs_decr_set(void *data, u64 val)
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
        spu_acquire_saved(ctx);
        lscsa->decr.slot[0] = (u32) val;
-       spu_release(ctx);
+       spu_release_saved(ctx);
 }
 
 static u64 __spufs_decr_get(void *data)
@@ -1450,7 +1642,7 @@ static u64 spufs_decr_get(void *data)
        u64 ret;
        spu_acquire_saved(ctx);
        ret = __spufs_decr_get(data);
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
@@ -1462,7 +1654,7 @@ static void spufs_decr_status_set(void *data, u64 val)
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
        spu_acquire_saved(ctx);
        lscsa->decr_status.slot[0] = (u32) val;
-       spu_release(ctx);
+       spu_release_saved(ctx);
 }
 
 static u64 __spufs_decr_status_get(void *data)
@@ -1478,7 +1670,7 @@ static u64 spufs_decr_status_get(void *data)
        u64 ret;
        spu_acquire_saved(ctx);
        ret = __spufs_decr_status_get(data);
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
@@ -1490,7 +1682,7 @@ static void spufs_event_mask_set(void *data, u64 val)
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
        spu_acquire_saved(ctx);
        lscsa->event_mask.slot[0] = (u32) val;
-       spu_release(ctx);
+       spu_release_saved(ctx);
 }
 
 static u64 __spufs_event_mask_get(void *data)
@@ -1506,7 +1698,7 @@ static u64 spufs_event_mask_get(void *data)
        u64 ret;
        spu_acquire_saved(ctx);
        ret = __spufs_event_mask_get(data);
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
@@ -1530,7 +1722,7 @@ static u64 spufs_event_status_get(void *data)
 
        spu_acquire_saved(ctx);
        ret = __spufs_event_status_get(data);
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
@@ -1542,7 +1734,7 @@ static void spufs_srr0_set(void *data, u64 val)
        struct spu_lscsa *lscsa = ctx->csa.lscsa;
        spu_acquire_saved(ctx);
        lscsa->srr0.slot[0] = (u32) val;
-       spu_release(ctx);
+       spu_release_saved(ctx);
 }
 
 static u64 spufs_srr0_get(void *data)
@@ -1552,7 +1744,7 @@ static u64 spufs_srr0_get(void *data)
        u64 ret;
        spu_acquire_saved(ctx);
        ret = lscsa->srr0.slot[0];
-       spu_release(ctx);
+       spu_release_saved(ctx);
        return ret;
 }
 DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
@@ -1608,7 +1800,7 @@ static u64 spufs_lslr_get(void *data)
 
        spu_acquire_saved(ctx);
        ret = __spufs_lslr_get(data);
-       spu_release(ctx);
+       spu_release_saved(ctx);
 
        return ret;
 }
@@ -1622,6 +1814,29 @@ 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)
 {
@@ -1649,7 +1864,7 @@ static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
        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;
 }
@@ -1687,7 +1902,7 @@ static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
        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;
 }
@@ -1728,7 +1943,7 @@ static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
        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;
 }
@@ -1778,7 +1993,7 @@ static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
        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;
 }
@@ -1829,7 +2044,7 @@ static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
        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;
 }
@@ -1839,7 +2054,117 @@ 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;
+
+       spu_acquire(ctx);
+       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, },
@@ -1871,10 +2196,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, },
@@ -1882,8 +2210,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, },
@@ -1893,6 +2221,8 @@ 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, },
        {},
 };