]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/cell/spufs/file.c
Pull netlink into release branch
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / cell / spufs / file.c
1 /*
2  * SPU file system -- file contents
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  *
6  * Author: Arnd Bergmann <arndb@de.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #undef DEBUG
24
25 #include <linux/fs.h>
26 #include <linux/ioctl.h>
27 #include <linux/module.h>
28 #include <linux/pagemap.h>
29 #include <linux/poll.h>
30 #include <linux/ptrace.h>
31 #include <linux/seq_file.h>
32
33 #include <asm/io.h>
34 #include <asm/semaphore.h>
35 #include <asm/spu.h>
36 #include <asm/spu_info.h>
37 #include <asm/uaccess.h>
38
39 #include "spufs.h"
40
41 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
42
43
44 static int
45 spufs_mem_open(struct inode *inode, struct file *file)
46 {
47         struct spufs_inode_info *i = SPUFS_I(inode);
48         struct spu_context *ctx = i->i_ctx;
49
50         mutex_lock(&ctx->mapping_lock);
51         file->private_data = ctx;
52         if (!i->i_openers++)
53                 ctx->local_store = inode->i_mapping;
54         mutex_unlock(&ctx->mapping_lock);
55         return 0;
56 }
57
58 static int
59 spufs_mem_release(struct inode *inode, struct file *file)
60 {
61         struct spufs_inode_info *i = SPUFS_I(inode);
62         struct spu_context *ctx = i->i_ctx;
63
64         mutex_lock(&ctx->mapping_lock);
65         if (!--i->i_openers)
66                 ctx->local_store = NULL;
67         mutex_unlock(&ctx->mapping_lock);
68         return 0;
69 }
70
71 static ssize_t
72 __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
73                         size_t size, loff_t *pos)
74 {
75         char *local_store = ctx->ops->get_ls(ctx);
76         return simple_read_from_buffer(buffer, size, pos, local_store,
77                                         LS_SIZE);
78 }
79
80 static ssize_t
81 spufs_mem_read(struct file *file, char __user *buffer,
82                                 size_t size, loff_t *pos)
83 {
84         struct spu_context *ctx = file->private_data;
85         ssize_t ret;
86
87         spu_acquire(ctx);
88         ret = __spufs_mem_read(ctx, buffer, size, pos);
89         spu_release(ctx);
90         return ret;
91 }
92
93 static ssize_t
94 spufs_mem_write(struct file *file, const char __user *buffer,
95                                         size_t size, loff_t *ppos)
96 {
97         struct spu_context *ctx = file->private_data;
98         char *local_store;
99         loff_t pos = *ppos;
100         int ret;
101
102         if (pos < 0)
103                 return -EINVAL;
104         if (pos > LS_SIZE)
105                 return -EFBIG;
106         if (size > LS_SIZE - pos)
107                 size = LS_SIZE - pos;
108
109         spu_acquire(ctx);
110         local_store = ctx->ops->get_ls(ctx);
111         ret = copy_from_user(local_store + pos, buffer, size);
112         spu_release(ctx);
113
114         if (ret)
115                 return -EFAULT;
116         *ppos = pos + size;
117         return size;
118 }
119
120 static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
121                                           unsigned long address)
122 {
123         struct spu_context *ctx = vma->vm_file->private_data;
124         unsigned long pfn, offset, addr0 = address;
125 #ifdef CONFIG_SPU_FS_64K_LS
126         struct spu_state *csa = &ctx->csa;
127         int psize;
128
129         /* Check what page size we are using */
130         psize = get_slice_psize(vma->vm_mm, address);
131
132         /* Some sanity checking */
133         BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
134
135         /* Wow, 64K, cool, we need to align the address though */
136         if (csa->use_big_pages) {
137                 BUG_ON(vma->vm_start & 0xffff);
138                 address &= ~0xfffful;
139         }
140 #endif /* CONFIG_SPU_FS_64K_LS */
141
142         offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
143         if (offset >= LS_SIZE)
144                 return NOPFN_SIGBUS;
145
146         pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
147                  addr0, address, offset);
148
149         spu_acquire(ctx);
150
151         if (ctx->state == SPU_STATE_SAVED) {
152                 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
153                                                         & ~_PAGE_NO_CACHE);
154                 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
155         } else {
156                 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
157                                              | _PAGE_NO_CACHE);
158                 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
159         }
160         vm_insert_pfn(vma, address, pfn);
161
162         spu_release(ctx);
163
164         return NOPFN_REFAULT;
165 }
166
167
168 static struct vm_operations_struct spufs_mem_mmap_vmops = {
169         .nopfn = spufs_mem_mmap_nopfn,
170 };
171
172 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
173 {
174 #ifdef CONFIG_SPU_FS_64K_LS
175         struct spu_context      *ctx = file->private_data;
176         struct spu_state        *csa = &ctx->csa;
177
178         /* Sanity check VMA alignment */
179         if (csa->use_big_pages) {
180                 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
181                          " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
182                          vma->vm_pgoff);
183                 if (vma->vm_start & 0xffff)
184                         return -EINVAL;
185                 if (vma->vm_pgoff & 0xf)
186                         return -EINVAL;
187         }
188 #endif /* CONFIG_SPU_FS_64K_LS */
189
190         if (!(vma->vm_flags & VM_SHARED))
191                 return -EINVAL;
192
193         vma->vm_flags |= VM_IO | VM_PFNMAP;
194         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
195                                      | _PAGE_NO_CACHE);
196
197         vma->vm_ops = &spufs_mem_mmap_vmops;
198         return 0;
199 }
200
201 #ifdef CONFIG_SPU_FS_64K_LS
202 unsigned long spufs_get_unmapped_area(struct file *file, unsigned long addr,
203                                       unsigned long len, unsigned long pgoff,
204                                       unsigned long flags)
205 {
206         struct spu_context      *ctx = file->private_data;
207         struct spu_state        *csa = &ctx->csa;
208
209         /* If not using big pages, fallback to normal MM g_u_a */
210         if (!csa->use_big_pages)
211                 return current->mm->get_unmapped_area(file, addr, len,
212                                                       pgoff, flags);
213
214         /* Else, try to obtain a 64K pages slice */
215         return slice_get_unmapped_area(addr, len, flags,
216                                        MMU_PAGE_64K, 1, 0);
217 }
218 #endif /* CONFIG_SPU_FS_64K_LS */
219
220 static const struct file_operations spufs_mem_fops = {
221         .open                   = spufs_mem_open,
222         .release                = spufs_mem_release,
223         .read                   = spufs_mem_read,
224         .write                  = spufs_mem_write,
225         .llseek                 = generic_file_llseek,
226         .mmap                   = spufs_mem_mmap,
227 #ifdef CONFIG_SPU_FS_64K_LS
228         .get_unmapped_area      = spufs_get_unmapped_area,
229 #endif
230 };
231
232 static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
233                                     unsigned long address,
234                                     unsigned long ps_offs,
235                                     unsigned long ps_size)
236 {
237         struct spu_context *ctx = vma->vm_file->private_data;
238         unsigned long area, offset = address - vma->vm_start;
239         int ret;
240
241         offset += vma->vm_pgoff << PAGE_SHIFT;
242         if (offset >= ps_size)
243                 return NOPFN_SIGBUS;
244
245         /* error here usually means a signal.. we might want to test
246          * the error code more precisely though
247          */
248         ret = spu_acquire_runnable(ctx, 0);
249         if (ret)
250                 return NOPFN_REFAULT;
251
252         area = ctx->spu->problem_phys + ps_offs;
253         vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
254         spu_release(ctx);
255
256         return NOPFN_REFAULT;
257 }
258
259 #if SPUFS_MMAP_4K
260 static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
261                                            unsigned long address)
262 {
263         return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
264 }
265
266 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
267         .nopfn = spufs_cntl_mmap_nopfn,
268 };
269
270 /*
271  * mmap support for problem state control area [0x4000 - 0x4fff].
272  */
273 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
274 {
275         if (!(vma->vm_flags & VM_SHARED))
276                 return -EINVAL;
277
278         vma->vm_flags |= VM_IO | VM_PFNMAP;
279         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
280                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
281
282         vma->vm_ops = &spufs_cntl_mmap_vmops;
283         return 0;
284 }
285 #else /* SPUFS_MMAP_4K */
286 #define spufs_cntl_mmap NULL
287 #endif /* !SPUFS_MMAP_4K */
288
289 static u64 spufs_cntl_get(void *data)
290 {
291         struct spu_context *ctx = data;
292         u64 val;
293
294         spu_acquire(ctx);
295         val = ctx->ops->status_read(ctx);
296         spu_release(ctx);
297
298         return val;
299 }
300
301 static void spufs_cntl_set(void *data, u64 val)
302 {
303         struct spu_context *ctx = data;
304
305         spu_acquire(ctx);
306         ctx->ops->runcntl_write(ctx, val);
307         spu_release(ctx);
308 }
309
310 static int spufs_cntl_open(struct inode *inode, struct file *file)
311 {
312         struct spufs_inode_info *i = SPUFS_I(inode);
313         struct spu_context *ctx = i->i_ctx;
314
315         mutex_lock(&ctx->mapping_lock);
316         file->private_data = ctx;
317         if (!i->i_openers++)
318                 ctx->cntl = inode->i_mapping;
319         mutex_unlock(&ctx->mapping_lock);
320         return simple_attr_open(inode, file, spufs_cntl_get,
321                                         spufs_cntl_set, "0x%08lx");
322 }
323
324 static int
325 spufs_cntl_release(struct inode *inode, struct file *file)
326 {
327         struct spufs_inode_info *i = SPUFS_I(inode);
328         struct spu_context *ctx = i->i_ctx;
329
330         simple_attr_close(inode, file);
331
332         mutex_lock(&ctx->mapping_lock);
333         if (!--i->i_openers)
334                 ctx->cntl = NULL;
335         mutex_unlock(&ctx->mapping_lock);
336         return 0;
337 }
338
339 static const struct file_operations spufs_cntl_fops = {
340         .open = spufs_cntl_open,
341         .release = spufs_cntl_release,
342         .read = simple_attr_read,
343         .write = simple_attr_write,
344         .mmap = spufs_cntl_mmap,
345 };
346
347 static int
348 spufs_regs_open(struct inode *inode, struct file *file)
349 {
350         struct spufs_inode_info *i = SPUFS_I(inode);
351         file->private_data = i->i_ctx;
352         return 0;
353 }
354
355 static ssize_t
356 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
357                         size_t size, loff_t *pos)
358 {
359         struct spu_lscsa *lscsa = ctx->csa.lscsa;
360         return simple_read_from_buffer(buffer, size, pos,
361                                       lscsa->gprs, sizeof lscsa->gprs);
362 }
363
364 static ssize_t
365 spufs_regs_read(struct file *file, char __user *buffer,
366                 size_t size, loff_t *pos)
367 {
368         int ret;
369         struct spu_context *ctx = file->private_data;
370
371         spu_acquire_saved(ctx);
372         ret = __spufs_regs_read(ctx, buffer, size, pos);
373         spu_release_saved(ctx);
374         return ret;
375 }
376
377 static ssize_t
378 spufs_regs_write(struct file *file, const char __user *buffer,
379                  size_t size, loff_t *pos)
380 {
381         struct spu_context *ctx = file->private_data;
382         struct spu_lscsa *lscsa = ctx->csa.lscsa;
383         int ret;
384
385         size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
386         if (size <= 0)
387                 return -EFBIG;
388         *pos += size;
389
390         spu_acquire_saved(ctx);
391
392         ret = copy_from_user(lscsa->gprs + *pos - size,
393                              buffer, size) ? -EFAULT : size;
394
395         spu_release_saved(ctx);
396         return ret;
397 }
398
399 static const struct file_operations spufs_regs_fops = {
400         .open    = spufs_regs_open,
401         .read    = spufs_regs_read,
402         .write   = spufs_regs_write,
403         .llseek  = generic_file_llseek,
404 };
405
406 static ssize_t
407 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
408                         size_t size, loff_t * pos)
409 {
410         struct spu_lscsa *lscsa = ctx->csa.lscsa;
411         return simple_read_from_buffer(buffer, size, pos,
412                                       &lscsa->fpcr, sizeof(lscsa->fpcr));
413 }
414
415 static ssize_t
416 spufs_fpcr_read(struct file *file, char __user * buffer,
417                 size_t size, loff_t * pos)
418 {
419         int ret;
420         struct spu_context *ctx = file->private_data;
421
422         spu_acquire_saved(ctx);
423         ret = __spufs_fpcr_read(ctx, buffer, size, pos);
424         spu_release_saved(ctx);
425         return ret;
426 }
427
428 static ssize_t
429 spufs_fpcr_write(struct file *file, const char __user * buffer,
430                  size_t size, loff_t * pos)
431 {
432         struct spu_context *ctx = file->private_data;
433         struct spu_lscsa *lscsa = ctx->csa.lscsa;
434         int ret;
435
436         size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
437         if (size <= 0)
438                 return -EFBIG;
439         *pos += size;
440
441         spu_acquire_saved(ctx);
442
443         ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
444                              buffer, size) ? -EFAULT : size;
445
446         spu_release_saved(ctx);
447         return ret;
448 }
449
450 static const struct file_operations spufs_fpcr_fops = {
451         .open = spufs_regs_open,
452         .read = spufs_fpcr_read,
453         .write = spufs_fpcr_write,
454         .llseek = generic_file_llseek,
455 };
456
457 /* generic open function for all pipe-like files */
458 static int spufs_pipe_open(struct inode *inode, struct file *file)
459 {
460         struct spufs_inode_info *i = SPUFS_I(inode);
461         file->private_data = i->i_ctx;
462
463         return nonseekable_open(inode, file);
464 }
465
466 /*
467  * Read as many bytes from the mailbox as possible, until
468  * one of the conditions becomes true:
469  *
470  * - no more data available in the mailbox
471  * - end of the user provided buffer
472  * - end of the mapped area
473  */
474 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
475                         size_t len, loff_t *pos)
476 {
477         struct spu_context *ctx = file->private_data;
478         u32 mbox_data, __user *udata;
479         ssize_t count;
480
481         if (len < 4)
482                 return -EINVAL;
483
484         if (!access_ok(VERIFY_WRITE, buf, len))
485                 return -EFAULT;
486
487         udata = (void __user *)buf;
488
489         spu_acquire(ctx);
490         for (count = 0; (count + 4) <= len; count += 4, udata++) {
491                 int ret;
492                 ret = ctx->ops->mbox_read(ctx, &mbox_data);
493                 if (ret == 0)
494                         break;
495
496                 /*
497                  * at the end of the mapped area, we can fault
498                  * but still need to return the data we have
499                  * read successfully so far.
500                  */
501                 ret = __put_user(mbox_data, udata);
502                 if (ret) {
503                         if (!count)
504                                 count = -EFAULT;
505                         break;
506                 }
507         }
508         spu_release(ctx);
509
510         if (!count)
511                 count = -EAGAIN;
512
513         return count;
514 }
515
516 static const struct file_operations spufs_mbox_fops = {
517         .open   = spufs_pipe_open,
518         .read   = spufs_mbox_read,
519 };
520
521 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
522                         size_t len, loff_t *pos)
523 {
524         struct spu_context *ctx = file->private_data;
525         u32 mbox_stat;
526
527         if (len < 4)
528                 return -EINVAL;
529
530         spu_acquire(ctx);
531
532         mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
533
534         spu_release(ctx);
535
536         if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
537                 return -EFAULT;
538
539         return 4;
540 }
541
542 static const struct file_operations spufs_mbox_stat_fops = {
543         .open   = spufs_pipe_open,
544         .read   = spufs_mbox_stat_read,
545 };
546
547 /* low-level ibox access function */
548 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
549 {
550         return ctx->ops->ibox_read(ctx, data);
551 }
552
553 static int spufs_ibox_fasync(int fd, struct file *file, int on)
554 {
555         struct spu_context *ctx = file->private_data;
556
557         return fasync_helper(fd, file, on, &ctx->ibox_fasync);
558 }
559
560 /* interrupt-level ibox callback function. */
561 void spufs_ibox_callback(struct spu *spu)
562 {
563         struct spu_context *ctx = spu->ctx;
564
565         wake_up_all(&ctx->ibox_wq);
566         kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
567 }
568
569 /*
570  * Read as many bytes from the interrupt mailbox as possible, until
571  * one of the conditions becomes true:
572  *
573  * - no more data available in the mailbox
574  * - end of the user provided buffer
575  * - end of the mapped area
576  *
577  * If the file is opened without O_NONBLOCK, we wait here until
578  * any data is available, but return when we have been able to
579  * read something.
580  */
581 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
582                         size_t len, loff_t *pos)
583 {
584         struct spu_context *ctx = file->private_data;
585         u32 ibox_data, __user *udata;
586         ssize_t count;
587
588         if (len < 4)
589                 return -EINVAL;
590
591         if (!access_ok(VERIFY_WRITE, buf, len))
592                 return -EFAULT;
593
594         udata = (void __user *)buf;
595
596         spu_acquire(ctx);
597
598         /* wait only for the first element */
599         count = 0;
600         if (file->f_flags & O_NONBLOCK) {
601                 if (!spu_ibox_read(ctx, &ibox_data))
602                         count = -EAGAIN;
603         } else {
604                 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
605         }
606         if (count)
607                 goto out;
608
609         /* if we can't write at all, return -EFAULT */
610         count = __put_user(ibox_data, udata);
611         if (count)
612                 goto out;
613
614         for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
615                 int ret;
616                 ret = ctx->ops->ibox_read(ctx, &ibox_data);
617                 if (ret == 0)
618                         break;
619                 /*
620                  * at the end of the mapped area, we can fault
621                  * but still need to return the data we have
622                  * read successfully so far.
623                  */
624                 ret = __put_user(ibox_data, udata);
625                 if (ret)
626                         break;
627         }
628
629 out:
630         spu_release(ctx);
631
632         return count;
633 }
634
635 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
636 {
637         struct spu_context *ctx = file->private_data;
638         unsigned int mask;
639
640         poll_wait(file, &ctx->ibox_wq, wait);
641
642         spu_acquire(ctx);
643         mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
644         spu_release(ctx);
645
646         return mask;
647 }
648
649 static const struct file_operations spufs_ibox_fops = {
650         .open   = spufs_pipe_open,
651         .read   = spufs_ibox_read,
652         .poll   = spufs_ibox_poll,
653         .fasync = spufs_ibox_fasync,
654 };
655
656 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
657                         size_t len, loff_t *pos)
658 {
659         struct spu_context *ctx = file->private_data;
660         u32 ibox_stat;
661
662         if (len < 4)
663                 return -EINVAL;
664
665         spu_acquire(ctx);
666         ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
667         spu_release(ctx);
668
669         if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
670                 return -EFAULT;
671
672         return 4;
673 }
674
675 static const struct file_operations spufs_ibox_stat_fops = {
676         .open   = spufs_pipe_open,
677         .read   = spufs_ibox_stat_read,
678 };
679
680 /* low-level mailbox write */
681 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
682 {
683         return ctx->ops->wbox_write(ctx, data);
684 }
685
686 static int spufs_wbox_fasync(int fd, struct file *file, int on)
687 {
688         struct spu_context *ctx = file->private_data;
689         int ret;
690
691         ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
692
693         return ret;
694 }
695
696 /* interrupt-level wbox callback function. */
697 void spufs_wbox_callback(struct spu *spu)
698 {
699         struct spu_context *ctx = spu->ctx;
700
701         wake_up_all(&ctx->wbox_wq);
702         kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
703 }
704
705 /*
706  * Write as many bytes to the interrupt mailbox as possible, until
707  * one of the conditions becomes true:
708  *
709  * - the mailbox is full
710  * - end of the user provided buffer
711  * - end of the mapped area
712  *
713  * If the file is opened without O_NONBLOCK, we wait here until
714  * space is availabyl, but return when we have been able to
715  * write something.
716  */
717 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
718                         size_t len, loff_t *pos)
719 {
720         struct spu_context *ctx = file->private_data;
721         u32 wbox_data, __user *udata;
722         ssize_t count;
723
724         if (len < 4)
725                 return -EINVAL;
726
727         udata = (void __user *)buf;
728         if (!access_ok(VERIFY_READ, buf, len))
729                 return -EFAULT;
730
731         if (__get_user(wbox_data, udata))
732                 return -EFAULT;
733
734         spu_acquire(ctx);
735
736         /*
737          * make sure we can at least write one element, by waiting
738          * in case of !O_NONBLOCK
739          */
740         count = 0;
741         if (file->f_flags & O_NONBLOCK) {
742                 if (!spu_wbox_write(ctx, wbox_data))
743                         count = -EAGAIN;
744         } else {
745                 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
746         }
747
748         if (count)
749                 goto out;
750
751         /* write aÑ• much as possible */
752         for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
753                 int ret;
754                 ret = __get_user(wbox_data, udata);
755                 if (ret)
756                         break;
757
758                 ret = spu_wbox_write(ctx, wbox_data);
759                 if (ret == 0)
760                         break;
761         }
762
763 out:
764         spu_release(ctx);
765         return count;
766 }
767
768 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
769 {
770         struct spu_context *ctx = file->private_data;
771         unsigned int mask;
772
773         poll_wait(file, &ctx->wbox_wq, wait);
774
775         spu_acquire(ctx);
776         mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
777         spu_release(ctx);
778
779         return mask;
780 }
781
782 static const struct file_operations spufs_wbox_fops = {
783         .open   = spufs_pipe_open,
784         .write  = spufs_wbox_write,
785         .poll   = spufs_wbox_poll,
786         .fasync = spufs_wbox_fasync,
787 };
788
789 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
790                         size_t len, loff_t *pos)
791 {
792         struct spu_context *ctx = file->private_data;
793         u32 wbox_stat;
794
795         if (len < 4)
796                 return -EINVAL;
797
798         spu_acquire(ctx);
799         wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
800         spu_release(ctx);
801
802         if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
803                 return -EFAULT;
804
805         return 4;
806 }
807
808 static const struct file_operations spufs_wbox_stat_fops = {
809         .open   = spufs_pipe_open,
810         .read   = spufs_wbox_stat_read,
811 };
812
813 static int spufs_signal1_open(struct inode *inode, struct file *file)
814 {
815         struct spufs_inode_info *i = SPUFS_I(inode);
816         struct spu_context *ctx = i->i_ctx;
817
818         mutex_lock(&ctx->mapping_lock);
819         file->private_data = ctx;
820         if (!i->i_openers++)
821                 ctx->signal1 = inode->i_mapping;
822         mutex_unlock(&ctx->mapping_lock);
823         return nonseekable_open(inode, file);
824 }
825
826 static int
827 spufs_signal1_release(struct inode *inode, struct file *file)
828 {
829         struct spufs_inode_info *i = SPUFS_I(inode);
830         struct spu_context *ctx = i->i_ctx;
831
832         mutex_lock(&ctx->mapping_lock);
833         if (!--i->i_openers)
834                 ctx->signal1 = NULL;
835         mutex_unlock(&ctx->mapping_lock);
836         return 0;
837 }
838
839 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
840                         size_t len, loff_t *pos)
841 {
842         int ret = 0;
843         u32 data;
844
845         if (len < 4)
846                 return -EINVAL;
847
848         if (ctx->csa.spu_chnlcnt_RW[3]) {
849                 data = ctx->csa.spu_chnldata_RW[3];
850                 ret = 4;
851         }
852
853         if (!ret)
854                 goto out;
855
856         if (copy_to_user(buf, &data, 4))
857                 return -EFAULT;
858
859 out:
860         return ret;
861 }
862
863 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
864                         size_t len, loff_t *pos)
865 {
866         int ret;
867         struct spu_context *ctx = file->private_data;
868
869         spu_acquire_saved(ctx);
870         ret = __spufs_signal1_read(ctx, buf, len, pos);
871         spu_release_saved(ctx);
872
873         return ret;
874 }
875
876 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
877                         size_t len, loff_t *pos)
878 {
879         struct spu_context *ctx;
880         u32 data;
881
882         ctx = file->private_data;
883
884         if (len < 4)
885                 return -EINVAL;
886
887         if (copy_from_user(&data, buf, 4))
888                 return -EFAULT;
889
890         spu_acquire(ctx);
891         ctx->ops->signal1_write(ctx, data);
892         spu_release(ctx);
893
894         return 4;
895 }
896
897 static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
898                                               unsigned long address)
899 {
900 #if PAGE_SIZE == 0x1000
901         return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
902 #elif PAGE_SIZE == 0x10000
903         /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
904          * signal 1 and 2 area
905          */
906         return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
907 #else
908 #error unsupported page size
909 #endif
910 }
911
912 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
913         .nopfn = spufs_signal1_mmap_nopfn,
914 };
915
916 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
917 {
918         if (!(vma->vm_flags & VM_SHARED))
919                 return -EINVAL;
920
921         vma->vm_flags |= VM_IO | VM_PFNMAP;
922         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
923                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
924
925         vma->vm_ops = &spufs_signal1_mmap_vmops;
926         return 0;
927 }
928
929 static const struct file_operations spufs_signal1_fops = {
930         .open = spufs_signal1_open,
931         .release = spufs_signal1_release,
932         .read = spufs_signal1_read,
933         .write = spufs_signal1_write,
934         .mmap = spufs_signal1_mmap,
935 };
936
937 static const struct file_operations spufs_signal1_nosched_fops = {
938         .open = spufs_signal1_open,
939         .release = spufs_signal1_release,
940         .write = spufs_signal1_write,
941         .mmap = spufs_signal1_mmap,
942 };
943
944 static const struct file_operations spufs_signal1_nosched_fops = {
945         .open = spufs_signal1_open,
946         .release = spufs_signal1_release,
947         .write = spufs_signal1_write,
948         .mmap = spufs_signal1_mmap,
949 };
950
951 static int spufs_signal2_open(struct inode *inode, struct file *file)
952 {
953         struct spufs_inode_info *i = SPUFS_I(inode);
954         struct spu_context *ctx = i->i_ctx;
955
956         mutex_lock(&ctx->mapping_lock);
957         file->private_data = ctx;
958         if (!i->i_openers++)
959                 ctx->signal2 = inode->i_mapping;
960         mutex_unlock(&ctx->mapping_lock);
961         return nonseekable_open(inode, file);
962 }
963
964 static int
965 spufs_signal2_release(struct inode *inode, struct file *file)
966 {
967         struct spufs_inode_info *i = SPUFS_I(inode);
968         struct spu_context *ctx = i->i_ctx;
969
970         mutex_lock(&ctx->mapping_lock);
971         if (!--i->i_openers)
972                 ctx->signal2 = NULL;
973         mutex_unlock(&ctx->mapping_lock);
974         return 0;
975 }
976
977 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
978                         size_t len, loff_t *pos)
979 {
980         int ret = 0;
981         u32 data;
982
983         if (len < 4)
984                 return -EINVAL;
985
986         if (ctx->csa.spu_chnlcnt_RW[4]) {
987                 data =  ctx->csa.spu_chnldata_RW[4];
988                 ret = 4;
989         }
990
991         if (!ret)
992                 goto out;
993
994         if (copy_to_user(buf, &data, 4))
995                 return -EFAULT;
996
997 out:
998         return ret;
999 }
1000
1001 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1002                         size_t len, loff_t *pos)
1003 {
1004         struct spu_context *ctx = file->private_data;
1005         int ret;
1006
1007         spu_acquire_saved(ctx);
1008         ret = __spufs_signal2_read(ctx, buf, len, pos);
1009         spu_release_saved(ctx);
1010
1011         return ret;
1012 }
1013
1014 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1015                         size_t len, loff_t *pos)
1016 {
1017         struct spu_context *ctx;
1018         u32 data;
1019
1020         ctx = file->private_data;
1021
1022         if (len < 4)
1023                 return -EINVAL;
1024
1025         if (copy_from_user(&data, buf, 4))
1026                 return -EFAULT;
1027
1028         spu_acquire(ctx);
1029         ctx->ops->signal2_write(ctx, data);
1030         spu_release(ctx);
1031
1032         return 4;
1033 }
1034
1035 #if SPUFS_MMAP_4K
1036 static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1037                                               unsigned long address)
1038 {
1039 #if PAGE_SIZE == 0x1000
1040         return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
1041 #elif PAGE_SIZE == 0x10000
1042         /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1043          * signal 1 and 2 area
1044          */
1045         return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1046 #else
1047 #error unsupported page size
1048 #endif
1049 }
1050
1051 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1052         .nopfn = spufs_signal2_mmap_nopfn,
1053 };
1054
1055 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1056 {
1057         if (!(vma->vm_flags & VM_SHARED))
1058                 return -EINVAL;
1059
1060         vma->vm_flags |= VM_IO | VM_PFNMAP;
1061         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1062                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
1063
1064         vma->vm_ops = &spufs_signal2_mmap_vmops;
1065         return 0;
1066 }
1067 #else /* SPUFS_MMAP_4K */
1068 #define spufs_signal2_mmap NULL
1069 #endif /* !SPUFS_MMAP_4K */
1070
1071 static const struct file_operations spufs_signal2_fops = {
1072         .open = spufs_signal2_open,
1073         .release = spufs_signal2_release,
1074         .read = spufs_signal2_read,
1075         .write = spufs_signal2_write,
1076         .mmap = spufs_signal2_mmap,
1077 };
1078
1079 static const struct file_operations spufs_signal2_nosched_fops = {
1080         .open = spufs_signal2_open,
1081         .release = spufs_signal2_release,
1082         .write = spufs_signal2_write,
1083         .mmap = spufs_signal2_mmap,
1084 };
1085
1086 static const struct file_operations spufs_signal2_nosched_fops = {
1087         .open = spufs_signal2_open,
1088         .release = spufs_signal2_release,
1089         .write = spufs_signal2_write,
1090         .mmap = spufs_signal2_mmap,
1091 };
1092
1093 static void spufs_signal1_type_set(void *data, u64 val)
1094 {
1095         struct spu_context *ctx = data;
1096
1097         spu_acquire(ctx);
1098         ctx->ops->signal1_type_set(ctx, val);
1099         spu_release(ctx);
1100 }
1101
1102 static u64 __spufs_signal1_type_get(void *data)
1103 {
1104         struct spu_context *ctx = data;
1105         return ctx->ops->signal1_type_get(ctx);
1106 }
1107
1108 static u64 spufs_signal1_type_get(void *data)
1109 {
1110         struct spu_context *ctx = data;
1111         u64 ret;
1112
1113         spu_acquire(ctx);
1114         ret = __spufs_signal1_type_get(data);
1115         spu_release(ctx);
1116
1117         return ret;
1118 }
1119 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1120                                         spufs_signal1_type_set, "%llu");
1121
1122 static void spufs_signal2_type_set(void *data, u64 val)
1123 {
1124         struct spu_context *ctx = data;
1125
1126         spu_acquire(ctx);
1127         ctx->ops->signal2_type_set(ctx, val);
1128         spu_release(ctx);
1129 }
1130
1131 static u64 __spufs_signal2_type_get(void *data)
1132 {
1133         struct spu_context *ctx = data;
1134         return ctx->ops->signal2_type_get(ctx);
1135 }
1136
1137 static u64 spufs_signal2_type_get(void *data)
1138 {
1139         struct spu_context *ctx = data;
1140         u64 ret;
1141
1142         spu_acquire(ctx);
1143         ret = __spufs_signal2_type_get(data);
1144         spu_release(ctx);
1145
1146         return ret;
1147 }
1148 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1149                                         spufs_signal2_type_set, "%llu");
1150
1151 #if SPUFS_MMAP_4K
1152 static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1153                                           unsigned long address)
1154 {
1155         return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
1156 }
1157
1158 static struct vm_operations_struct spufs_mss_mmap_vmops = {
1159         .nopfn = spufs_mss_mmap_nopfn,
1160 };
1161
1162 /*
1163  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1164  */
1165 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1166 {
1167         if (!(vma->vm_flags & VM_SHARED))
1168                 return -EINVAL;
1169
1170         vma->vm_flags |= VM_IO | VM_PFNMAP;
1171         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1172                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
1173
1174         vma->vm_ops = &spufs_mss_mmap_vmops;
1175         return 0;
1176 }
1177 #else /* SPUFS_MMAP_4K */
1178 #define spufs_mss_mmap NULL
1179 #endif /* !SPUFS_MMAP_4K */
1180
1181 static int spufs_mss_open(struct inode *inode, struct file *file)
1182 {
1183         struct spufs_inode_info *i = SPUFS_I(inode);
1184         struct spu_context *ctx = i->i_ctx;
1185
1186         file->private_data = i->i_ctx;
1187
1188         mutex_lock(&ctx->mapping_lock);
1189         if (!i->i_openers++)
1190                 ctx->mss = inode->i_mapping;
1191         mutex_unlock(&ctx->mapping_lock);
1192         return nonseekable_open(inode, file);
1193 }
1194
1195 static int
1196 spufs_mss_release(struct inode *inode, struct file *file)
1197 {
1198         struct spufs_inode_info *i = SPUFS_I(inode);
1199         struct spu_context *ctx = i->i_ctx;
1200
1201         mutex_lock(&ctx->mapping_lock);
1202         if (!--i->i_openers)
1203                 ctx->mss = NULL;
1204         mutex_unlock(&ctx->mapping_lock);
1205         return 0;
1206 }
1207
1208 static const struct file_operations spufs_mss_fops = {
1209         .open    = spufs_mss_open,
1210         .release = spufs_mss_release,
1211         .mmap    = spufs_mss_mmap,
1212 };
1213
1214 static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1215                                             unsigned long address)
1216 {
1217         return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
1218 }
1219
1220 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1221         .nopfn = spufs_psmap_mmap_nopfn,
1222 };
1223
1224 /*
1225  * mmap support for full problem state area [0x00000 - 0x1ffff].
1226  */
1227 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1228 {
1229         if (!(vma->vm_flags & VM_SHARED))
1230                 return -EINVAL;
1231
1232         vma->vm_flags |= VM_IO | VM_PFNMAP;
1233         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1234                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
1235
1236         vma->vm_ops = &spufs_psmap_mmap_vmops;
1237         return 0;
1238 }
1239
1240 static int spufs_psmap_open(struct inode *inode, struct file *file)
1241 {
1242         struct spufs_inode_info *i = SPUFS_I(inode);
1243         struct spu_context *ctx = i->i_ctx;
1244
1245         mutex_lock(&ctx->mapping_lock);
1246         file->private_data = i->i_ctx;
1247         if (!i->i_openers++)
1248                 ctx->psmap = inode->i_mapping;
1249         mutex_unlock(&ctx->mapping_lock);
1250         return nonseekable_open(inode, file);
1251 }
1252
1253 static int
1254 spufs_psmap_release(struct inode *inode, struct file *file)
1255 {
1256         struct spufs_inode_info *i = SPUFS_I(inode);
1257         struct spu_context *ctx = i->i_ctx;
1258
1259         mutex_lock(&ctx->mapping_lock);
1260         if (!--i->i_openers)
1261                 ctx->psmap = NULL;
1262         mutex_unlock(&ctx->mapping_lock);
1263         return 0;
1264 }
1265
1266 static const struct file_operations spufs_psmap_fops = {
1267         .open    = spufs_psmap_open,
1268         .release = spufs_psmap_release,
1269         .mmap    = spufs_psmap_mmap,
1270 };
1271
1272
1273 #if SPUFS_MMAP_4K
1274 static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1275                                           unsigned long address)
1276 {
1277         return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
1278 }
1279
1280 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1281         .nopfn = spufs_mfc_mmap_nopfn,
1282 };
1283
1284 /*
1285  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1286  */
1287 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1288 {
1289         if (!(vma->vm_flags & VM_SHARED))
1290                 return -EINVAL;
1291
1292         vma->vm_flags |= VM_IO | VM_PFNMAP;
1293         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1294                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
1295
1296         vma->vm_ops = &spufs_mfc_mmap_vmops;
1297         return 0;
1298 }
1299 #else /* SPUFS_MMAP_4K */
1300 #define spufs_mfc_mmap NULL
1301 #endif /* !SPUFS_MMAP_4K */
1302
1303 static int spufs_mfc_open(struct inode *inode, struct file *file)
1304 {
1305         struct spufs_inode_info *i = SPUFS_I(inode);
1306         struct spu_context *ctx = i->i_ctx;
1307
1308         /* we don't want to deal with DMA into other processes */
1309         if (ctx->owner != current->mm)
1310                 return -EINVAL;
1311
1312         if (atomic_read(&inode->i_count) != 1)
1313                 return -EBUSY;
1314
1315         mutex_lock(&ctx->mapping_lock);
1316         file->private_data = ctx;
1317         if (!i->i_openers++)
1318                 ctx->mfc = inode->i_mapping;
1319         mutex_unlock(&ctx->mapping_lock);
1320         return nonseekable_open(inode, file);
1321 }
1322
1323 static int
1324 spufs_mfc_release(struct inode *inode, struct file *file)
1325 {
1326         struct spufs_inode_info *i = SPUFS_I(inode);
1327         struct spu_context *ctx = i->i_ctx;
1328
1329         mutex_lock(&ctx->mapping_lock);
1330         if (!--i->i_openers)
1331                 ctx->mfc = NULL;
1332         mutex_unlock(&ctx->mapping_lock);
1333         return 0;
1334 }
1335
1336 /* interrupt-level mfc callback function. */
1337 void spufs_mfc_callback(struct spu *spu)
1338 {
1339         struct spu_context *ctx = spu->ctx;
1340
1341         wake_up_all(&ctx->mfc_wq);
1342
1343         pr_debug("%s %s\n", __FUNCTION__, spu->name);
1344         if (ctx->mfc_fasync) {
1345                 u32 free_elements, tagstatus;
1346                 unsigned int mask;
1347
1348                 /* no need for spu_acquire in interrupt context */
1349                 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1350                 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1351
1352                 mask = 0;
1353                 if (free_elements & 0xffff)
1354                         mask |= POLLOUT;
1355                 if (tagstatus & ctx->tagwait)
1356                         mask |= POLLIN;
1357
1358                 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1359         }
1360 }
1361
1362 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1363 {
1364         /* See if there is one tag group is complete */
1365         /* FIXME we need locking around tagwait */
1366         *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1367         ctx->tagwait &= ~*status;
1368         if (*status)
1369                 return 1;
1370
1371         /* enable interrupt waiting for any tag group,
1372            may silently fail if interrupts are already enabled */
1373         ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1374         return 0;
1375 }
1376
1377 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1378                         size_t size, loff_t *pos)
1379 {
1380         struct spu_context *ctx = file->private_data;
1381         int ret = -EINVAL;
1382         u32 status;
1383
1384         if (size != 4)
1385                 goto out;
1386
1387         spu_acquire(ctx);
1388         if (file->f_flags & O_NONBLOCK) {
1389                 status = ctx->ops->read_mfc_tagstatus(ctx);
1390                 if (!(status & ctx->tagwait))
1391                         ret = -EAGAIN;
1392                 else
1393                         ctx->tagwait &= ~status;
1394         } else {
1395                 ret = spufs_wait(ctx->mfc_wq,
1396                            spufs_read_mfc_tagstatus(ctx, &status));
1397         }
1398         spu_release(ctx);
1399
1400         if (ret)
1401                 goto out;
1402
1403         ret = 4;
1404         if (copy_to_user(buffer, &status, 4))
1405                 ret = -EFAULT;
1406
1407 out:
1408         return ret;
1409 }
1410
1411 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1412 {
1413         pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1414                  cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1415
1416         switch (cmd->cmd) {
1417         case MFC_PUT_CMD:
1418         case MFC_PUTF_CMD:
1419         case MFC_PUTB_CMD:
1420         case MFC_GET_CMD:
1421         case MFC_GETF_CMD:
1422         case MFC_GETB_CMD:
1423                 break;
1424         default:
1425                 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1426                 return -EIO;
1427         }
1428
1429         if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1430                 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1431                                 cmd->ea, cmd->lsa);
1432                 return -EIO;
1433         }
1434
1435         switch (cmd->size & 0xf) {
1436         case 1:
1437                 break;
1438         case 2:
1439                 if (cmd->lsa & 1)
1440                         goto error;
1441                 break;
1442         case 4:
1443                 if (cmd->lsa & 3)
1444                         goto error;
1445                 break;
1446         case 8:
1447                 if (cmd->lsa & 7)
1448                         goto error;
1449                 break;
1450         case 0:
1451                 if (cmd->lsa & 15)
1452                         goto error;
1453                 break;
1454         error:
1455         default:
1456                 pr_debug("invalid DMA alignment %x for size %x\n",
1457                         cmd->lsa & 0xf, cmd->size);
1458                 return -EIO;
1459         }
1460
1461         if (cmd->size > 16 * 1024) {
1462                 pr_debug("invalid DMA size %x\n", cmd->size);
1463                 return -EIO;
1464         }
1465
1466         if (cmd->tag & 0xfff0) {
1467                 /* we reserve the higher tag numbers for kernel use */
1468                 pr_debug("invalid DMA tag\n");
1469                 return -EIO;
1470         }
1471
1472         if (cmd->class) {
1473                 /* not supported in this version */
1474                 pr_debug("invalid DMA class\n");
1475                 return -EIO;
1476         }
1477
1478         return 0;
1479 }
1480
1481 static int spu_send_mfc_command(struct spu_context *ctx,
1482                                 struct mfc_dma_command cmd,
1483                                 int *error)
1484 {
1485         *error = ctx->ops->send_mfc_command(ctx, &cmd);
1486         if (*error == -EAGAIN) {
1487                 /* wait for any tag group to complete
1488                    so we have space for the new command */
1489                 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1490                 /* try again, because the queue might be
1491                    empty again */
1492                 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1493                 if (*error == -EAGAIN)
1494                         return 0;
1495         }
1496         return 1;
1497 }
1498
1499 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1500                         size_t size, loff_t *pos)
1501 {
1502         struct spu_context *ctx = file->private_data;
1503         struct mfc_dma_command cmd;
1504         int ret = -EINVAL;
1505
1506         if (size != sizeof cmd)
1507                 goto out;
1508
1509         ret = -EFAULT;
1510         if (copy_from_user(&cmd, buffer, sizeof cmd))
1511                 goto out;
1512
1513         ret = spufs_check_valid_dma(&cmd);
1514         if (ret)
1515                 goto out;
1516
1517         ret = spu_acquire_runnable(ctx, 0);
1518         if (ret)
1519                 goto out;
1520
1521         if (file->f_flags & O_NONBLOCK) {
1522                 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1523         } else {
1524                 int status;
1525                 ret = spufs_wait(ctx->mfc_wq,
1526                                  spu_send_mfc_command(ctx, cmd, &status));
1527                 if (status)
1528                         ret = status;
1529         }
1530
1531         if (ret)
1532                 goto out_unlock;
1533
1534         ctx->tagwait |= 1 << cmd.tag;
1535         ret = size;
1536
1537 out_unlock:
1538         spu_release(ctx);
1539 out:
1540         return ret;
1541 }
1542
1543 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1544 {
1545         struct spu_context *ctx = file->private_data;
1546         u32 free_elements, tagstatus;
1547         unsigned int mask;
1548
1549         poll_wait(file, &ctx->mfc_wq, wait);
1550
1551         spu_acquire(ctx);
1552         ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1553         free_elements = ctx->ops->get_mfc_free_elements(ctx);
1554         tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1555         spu_release(ctx);
1556
1557         mask = 0;
1558         if (free_elements & 0xffff)
1559                 mask |= POLLOUT | POLLWRNORM;
1560         if (tagstatus & ctx->tagwait)
1561                 mask |= POLLIN | POLLRDNORM;
1562
1563         pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1564                 free_elements, tagstatus, ctx->tagwait);
1565
1566         return mask;
1567 }
1568
1569 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1570 {
1571         struct spu_context *ctx = file->private_data;
1572         int ret;
1573
1574         spu_acquire(ctx);
1575 #if 0
1576 /* this currently hangs */
1577         ret = spufs_wait(ctx->mfc_wq,
1578                          ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1579         if (ret)
1580                 goto out;
1581         ret = spufs_wait(ctx->mfc_wq,
1582                          ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1583 out:
1584 #else
1585         ret = 0;
1586 #endif
1587         spu_release(ctx);
1588
1589         return ret;
1590 }
1591
1592 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1593                            int datasync)
1594 {
1595         return spufs_mfc_flush(file, NULL);
1596 }
1597
1598 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1599 {
1600         struct spu_context *ctx = file->private_data;
1601
1602         return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1603 }
1604
1605 static const struct file_operations spufs_mfc_fops = {
1606         .open    = spufs_mfc_open,
1607         .release = spufs_mfc_release,
1608         .read    = spufs_mfc_read,
1609         .write   = spufs_mfc_write,
1610         .poll    = spufs_mfc_poll,
1611         .flush   = spufs_mfc_flush,
1612         .fsync   = spufs_mfc_fsync,
1613         .fasync  = spufs_mfc_fasync,
1614         .mmap    = spufs_mfc_mmap,
1615 };
1616
1617 static void spufs_npc_set(void *data, u64 val)
1618 {
1619         struct spu_context *ctx = data;
1620         spu_acquire(ctx);
1621         ctx->ops->npc_write(ctx, val);
1622         spu_release(ctx);
1623 }
1624
1625 static u64 spufs_npc_get(void *data)
1626 {
1627         struct spu_context *ctx = data;
1628         u64 ret;
1629         spu_acquire(ctx);
1630         ret = ctx->ops->npc_read(ctx);
1631         spu_release(ctx);
1632         return ret;
1633 }
1634 DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1635                         "0x%llx\n")
1636
1637 static void spufs_decr_set(void *data, u64 val)
1638 {
1639         struct spu_context *ctx = data;
1640         struct spu_lscsa *lscsa = ctx->csa.lscsa;
1641         spu_acquire_saved(ctx);
1642         lscsa->decr.slot[0] = (u32) val;
1643         spu_release_saved(ctx);
1644 }
1645
1646 static u64 __spufs_decr_get(void *data)
1647 {
1648         struct spu_context *ctx = data;
1649         struct spu_lscsa *lscsa = ctx->csa.lscsa;
1650         return lscsa->decr.slot[0];
1651 }
1652
1653 static u64 spufs_decr_get(void *data)
1654 {
1655         struct spu_context *ctx = data;
1656         u64 ret;
1657         spu_acquire_saved(ctx);
1658         ret = __spufs_decr_get(data);
1659         spu_release_saved(ctx);
1660         return ret;
1661 }
1662 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1663                         "0x%llx\n")
1664
1665 static void spufs_decr_status_set(void *data, u64 val)
1666 {
1667         struct spu_context *ctx = data;
1668         spu_acquire_saved(ctx);
1669         if (val)
1670                 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1671         else
1672                 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1673         spu_release_saved(ctx);
1674 }
1675
1676 static u64 __spufs_decr_status_get(void *data)
1677 {
1678         struct spu_context *ctx = data;
1679         if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1680                 return SPU_DECR_STATUS_RUNNING;
1681         else
1682                 return 0;
1683 }
1684
1685 static u64 spufs_decr_status_get(void *data)
1686 {
1687         struct spu_context *ctx = data;
1688         u64 ret;
1689         spu_acquire_saved(ctx);
1690         ret = __spufs_decr_status_get(data);
1691         spu_release_saved(ctx);
1692         return ret;
1693 }
1694 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1695                         spufs_decr_status_set, "0x%llx\n")
1696
1697 static void spufs_event_mask_set(void *data, u64 val)
1698 {
1699         struct spu_context *ctx = data;
1700         struct spu_lscsa *lscsa = ctx->csa.lscsa;
1701         spu_acquire_saved(ctx);
1702         lscsa->event_mask.slot[0] = (u32) val;
1703         spu_release_saved(ctx);
1704 }
1705
1706 static u64 __spufs_event_mask_get(void *data)
1707 {
1708         struct spu_context *ctx = data;
1709         struct spu_lscsa *lscsa = ctx->csa.lscsa;
1710         return lscsa->event_mask.slot[0];
1711 }
1712
1713 static u64 spufs_event_mask_get(void *data)
1714 {
1715         struct spu_context *ctx = data;
1716         u64 ret;
1717         spu_acquire_saved(ctx);
1718         ret = __spufs_event_mask_get(data);
1719         spu_release_saved(ctx);
1720         return ret;
1721 }
1722 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1723                         spufs_event_mask_set, "0x%llx\n")
1724
1725 static u64 __spufs_event_status_get(void *data)
1726 {
1727         struct spu_context *ctx = data;
1728         struct spu_state *state = &ctx->csa;
1729         u64 stat;
1730         stat = state->spu_chnlcnt_RW[0];
1731         if (stat)
1732                 return state->spu_chnldata_RW[0];
1733         return 0;
1734 }
1735
1736 static u64 spufs_event_status_get(void *data)
1737 {
1738         struct spu_context *ctx = data;
1739         u64 ret = 0;
1740
1741         spu_acquire_saved(ctx);
1742         ret = __spufs_event_status_get(data);
1743         spu_release_saved(ctx);
1744         return ret;
1745 }
1746 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1747                         NULL, "0x%llx\n")
1748
1749 static void spufs_srr0_set(void *data, u64 val)
1750 {
1751         struct spu_context *ctx = data;
1752         struct spu_lscsa *lscsa = ctx->csa.lscsa;
1753         spu_acquire_saved(ctx);
1754         lscsa->srr0.slot[0] = (u32) val;
1755         spu_release_saved(ctx);
1756 }
1757
1758 static u64 spufs_srr0_get(void *data)
1759 {
1760         struct spu_context *ctx = data;
1761         struct spu_lscsa *lscsa = ctx->csa.lscsa;
1762         u64 ret;
1763         spu_acquire_saved(ctx);
1764         ret = lscsa->srr0.slot[0];
1765         spu_release_saved(ctx);
1766         return ret;
1767 }
1768 DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1769                         "0x%llx\n")
1770
1771 static u64 spufs_id_get(void *data)
1772 {
1773         struct spu_context *ctx = data;
1774         u64 num;
1775
1776         spu_acquire(ctx);
1777         if (ctx->state == SPU_STATE_RUNNABLE)
1778                 num = ctx->spu->number;
1779         else
1780                 num = (unsigned int)-1;
1781         spu_release(ctx);
1782
1783         return num;
1784 }
1785 DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
1786
1787 static u64 __spufs_object_id_get(void *data)
1788 {
1789         struct spu_context *ctx = data;
1790         return ctx->object_id;
1791 }
1792
1793 static u64 spufs_object_id_get(void *data)
1794 {
1795         /* FIXME: Should there really be no locking here? */
1796         return __spufs_object_id_get(data);
1797 }
1798
1799 static void spufs_object_id_set(void *data, u64 id)
1800 {
1801         struct spu_context *ctx = data;
1802         ctx->object_id = id;
1803 }
1804
1805 DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1806                 spufs_object_id_set, "0x%llx\n");
1807
1808 static u64 __spufs_lslr_get(void *data)
1809 {
1810         struct spu_context *ctx = data;
1811         return ctx->csa.priv2.spu_lslr_RW;
1812 }
1813
1814 static u64 spufs_lslr_get(void *data)
1815 {
1816         struct spu_context *ctx = data;
1817         u64 ret;
1818
1819         spu_acquire_saved(ctx);
1820         ret = __spufs_lslr_get(data);
1821         spu_release_saved(ctx);
1822
1823         return ret;
1824 }
1825 DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
1826
1827 static int spufs_info_open(struct inode *inode, struct file *file)
1828 {
1829         struct spufs_inode_info *i = SPUFS_I(inode);
1830         struct spu_context *ctx = i->i_ctx;
1831         file->private_data = ctx;
1832         return 0;
1833 }
1834
1835 static int spufs_caps_show(struct seq_file *s, void *private)
1836 {
1837         struct spu_context *ctx = s->private;
1838
1839         if (!(ctx->flags & SPU_CREATE_NOSCHED))
1840                 seq_puts(s, "sched\n");
1841         if (!(ctx->flags & SPU_CREATE_ISOLATE))
1842                 seq_puts(s, "step\n");
1843         return 0;
1844 }
1845
1846 static int spufs_caps_open(struct inode *inode, struct file *file)
1847 {
1848         return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1849 }
1850
1851 static const struct file_operations spufs_caps_fops = {
1852         .open           = spufs_caps_open,
1853         .read           = seq_read,
1854         .llseek         = seq_lseek,
1855         .release        = single_release,
1856 };
1857
1858 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1859                         char __user *buf, size_t len, loff_t *pos)
1860 {
1861         u32 mbox_stat;
1862         u32 data;
1863
1864         mbox_stat = ctx->csa.prob.mb_stat_R;
1865         if (mbox_stat & 0x0000ff) {
1866                 data = ctx->csa.prob.pu_mb_R;
1867         }
1868
1869         return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1870 }
1871
1872 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1873                                    size_t len, loff_t *pos)
1874 {
1875         int ret;
1876         struct spu_context *ctx = file->private_data;
1877
1878         if (!access_ok(VERIFY_WRITE, buf, len))
1879                 return -EFAULT;
1880
1881         spu_acquire_saved(ctx);
1882         spin_lock(&ctx->csa.register_lock);
1883         ret = __spufs_mbox_info_read(ctx, buf, len, pos);
1884         spin_unlock(&ctx->csa.register_lock);
1885         spu_release_saved(ctx);
1886
1887         return ret;
1888 }
1889
1890 static const struct file_operations spufs_mbox_info_fops = {
1891         .open = spufs_info_open,
1892         .read = spufs_mbox_info_read,
1893         .llseek  = generic_file_llseek,
1894 };
1895
1896 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
1897                                 char __user *buf, size_t len, loff_t *pos)
1898 {
1899         u32 ibox_stat;
1900         u32 data;
1901
1902         ibox_stat = ctx->csa.prob.mb_stat_R;
1903         if (ibox_stat & 0xff0000) {
1904                 data = ctx->csa.priv2.puint_mb_R;
1905         }
1906
1907         return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1908 }
1909
1910 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
1911                                    size_t len, loff_t *pos)
1912 {
1913         struct spu_context *ctx = file->private_data;
1914         int ret;
1915
1916         if (!access_ok(VERIFY_WRITE, buf, len))
1917                 return -EFAULT;
1918
1919         spu_acquire_saved(ctx);
1920         spin_lock(&ctx->csa.register_lock);
1921         ret = __spufs_ibox_info_read(ctx, buf, len, pos);
1922         spin_unlock(&ctx->csa.register_lock);
1923         spu_release_saved(ctx);
1924
1925         return ret;
1926 }
1927
1928 static const struct file_operations spufs_ibox_info_fops = {
1929         .open = spufs_info_open,
1930         .read = spufs_ibox_info_read,
1931         .llseek  = generic_file_llseek,
1932 };
1933
1934 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
1935                         char __user *buf, size_t len, loff_t *pos)
1936 {
1937         int i, cnt;
1938         u32 data[4];
1939         u32 wbox_stat;
1940
1941         wbox_stat = ctx->csa.prob.mb_stat_R;
1942         cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
1943         for (i = 0; i < cnt; i++) {
1944                 data[i] = ctx->csa.spu_mailbox_data[i];
1945         }
1946
1947         return simple_read_from_buffer(buf, len, pos, &data,
1948                                 cnt * sizeof(u32));
1949 }
1950
1951 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
1952                                    size_t len, loff_t *pos)
1953 {
1954         struct spu_context *ctx = file->private_data;
1955         int ret;
1956
1957         if (!access_ok(VERIFY_WRITE, buf, len))
1958                 return -EFAULT;
1959
1960         spu_acquire_saved(ctx);
1961         spin_lock(&ctx->csa.register_lock);
1962         ret = __spufs_wbox_info_read(ctx, buf, len, pos);
1963         spin_unlock(&ctx->csa.register_lock);
1964         spu_release_saved(ctx);
1965
1966         return ret;
1967 }
1968
1969 static const struct file_operations spufs_wbox_info_fops = {
1970         .open = spufs_info_open,
1971         .read = spufs_wbox_info_read,
1972         .llseek  = generic_file_llseek,
1973 };
1974
1975 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
1976                         char __user *buf, size_t len, loff_t *pos)
1977 {
1978         struct spu_dma_info info;
1979         struct mfc_cq_sr *qp, *spuqp;
1980         int i;
1981
1982         info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
1983         info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
1984         info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
1985         info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
1986         info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
1987         for (i = 0; i < 16; i++) {
1988                 qp = &info.dma_info_command_data[i];
1989                 spuqp = &ctx->csa.priv2.spuq[i];
1990
1991                 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
1992                 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
1993                 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
1994                 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
1995         }
1996
1997         return simple_read_from_buffer(buf, len, pos, &info,
1998                                 sizeof info);
1999 }
2000
2001 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2002                               size_t len, loff_t *pos)
2003 {
2004         struct spu_context *ctx = file->private_data;
2005         int ret;
2006
2007         if (!access_ok(VERIFY_WRITE, buf, len))
2008                 return -EFAULT;
2009
2010         spu_acquire_saved(ctx);
2011         spin_lock(&ctx->csa.register_lock);
2012         ret = __spufs_dma_info_read(ctx, buf, len, pos);
2013         spin_unlock(&ctx->csa.register_lock);
2014         spu_release_saved(ctx);
2015
2016         return ret;
2017 }
2018
2019 static const struct file_operations spufs_dma_info_fops = {
2020         .open = spufs_info_open,
2021         .read = spufs_dma_info_read,
2022 };
2023
2024 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2025                         char __user *buf, size_t len, loff_t *pos)
2026 {
2027         struct spu_proxydma_info info;
2028         struct mfc_cq_sr *qp, *puqp;
2029         int ret = sizeof info;
2030         int i;
2031
2032         if (len < ret)
2033                 return -EINVAL;
2034
2035         if (!access_ok(VERIFY_WRITE, buf, len))
2036                 return -EFAULT;
2037
2038         info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2039         info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2040         info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2041         for (i = 0; i < 8; i++) {
2042                 qp = &info.proxydma_info_command_data[i];
2043                 puqp = &ctx->csa.priv2.puq[i];
2044
2045                 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2046                 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2047                 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2048                 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2049         }
2050
2051         return simple_read_from_buffer(buf, len, pos, &info,
2052                                 sizeof info);
2053 }
2054
2055 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2056                                    size_t len, loff_t *pos)
2057 {
2058         struct spu_context *ctx = file->private_data;
2059         int ret;
2060
2061         spu_acquire_saved(ctx);
2062         spin_lock(&ctx->csa.register_lock);
2063         ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2064         spin_unlock(&ctx->csa.register_lock);
2065         spu_release_saved(ctx);
2066
2067         return ret;
2068 }
2069
2070 static const struct file_operations spufs_proxydma_info_fops = {
2071         .open = spufs_info_open,
2072         .read = spufs_proxydma_info_read,
2073 };
2074
2075 static int spufs_show_tid(struct seq_file *s, void *private)
2076 {
2077         struct spu_context *ctx = s->private;
2078
2079         seq_printf(s, "%d\n", ctx->tid);
2080         return 0;
2081 }
2082
2083 static int spufs_tid_open(struct inode *inode, struct file *file)
2084 {
2085         return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2086 }
2087
2088 static const struct file_operations spufs_tid_fops = {
2089         .open           = spufs_tid_open,
2090         .read           = seq_read,
2091         .llseek         = seq_lseek,
2092         .release        = single_release,
2093 };
2094
2095 static const char *ctx_state_names[] = {
2096         "user", "system", "iowait", "loaded"
2097 };
2098
2099 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2100                 enum spu_utilization_state state)
2101 {
2102         struct timespec ts;
2103         unsigned long long time = ctx->stats.times[state];
2104
2105         /*
2106          * In general, utilization statistics are updated by the controlling
2107          * thread as the spu context moves through various well defined
2108          * state transitions, but if the context is lazily loaded its
2109          * utilization statistics are not updated as the controlling thread
2110          * is not tightly coupled with the execution of the spu context.  We
2111          * calculate and apply the time delta from the last recorded state
2112          * of the spu context.
2113          */
2114         if (ctx->spu && ctx->stats.util_state == state) {
2115                 ktime_get_ts(&ts);
2116                 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2117         }
2118
2119         return time / NSEC_PER_MSEC;
2120 }
2121
2122 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2123 {
2124         unsigned long long slb_flts = ctx->stats.slb_flt;
2125
2126         if (ctx->state == SPU_STATE_RUNNABLE) {
2127                 slb_flts += (ctx->spu->stats.slb_flt -
2128                              ctx->stats.slb_flt_base);
2129         }
2130
2131         return slb_flts;
2132 }
2133
2134 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2135 {
2136         unsigned long long class2_intrs = ctx->stats.class2_intr;
2137
2138         if (ctx->state == SPU_STATE_RUNNABLE) {
2139                 class2_intrs += (ctx->spu->stats.class2_intr -
2140                                  ctx->stats.class2_intr_base);
2141         }
2142
2143         return class2_intrs;
2144 }
2145
2146
2147 static int spufs_show_stat(struct seq_file *s, void *private)
2148 {
2149         struct spu_context *ctx = s->private;
2150
2151         spu_acquire(ctx);
2152         seq_printf(s, "%s %llu %llu %llu %llu "
2153                       "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2154                 ctx_state_names[ctx->stats.util_state],
2155                 spufs_acct_time(ctx, SPU_UTIL_USER),
2156                 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2157                 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2158                 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2159                 ctx->stats.vol_ctx_switch,
2160                 ctx->stats.invol_ctx_switch,
2161                 spufs_slb_flts(ctx),
2162                 ctx->stats.hash_flt,
2163                 ctx->stats.min_flt,
2164                 ctx->stats.maj_flt,
2165                 spufs_class2_intrs(ctx),
2166                 ctx->stats.libassist);
2167         spu_release(ctx);
2168         return 0;
2169 }
2170
2171 static int spufs_stat_open(struct inode *inode, struct file *file)
2172 {
2173         return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2174 }
2175
2176 static const struct file_operations spufs_stat_fops = {
2177         .open           = spufs_stat_open,
2178         .read           = seq_read,
2179         .llseek         = seq_lseek,
2180         .release        = single_release,
2181 };
2182
2183
2184 struct tree_descr spufs_dir_contents[] = {
2185         { "capabilities", &spufs_caps_fops, 0444, },
2186         { "mem",  &spufs_mem_fops,  0666, },
2187         { "regs", &spufs_regs_fops,  0666, },
2188         { "mbox", &spufs_mbox_fops, 0444, },
2189         { "ibox", &spufs_ibox_fops, 0444, },
2190         { "wbox", &spufs_wbox_fops, 0222, },
2191         { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2192         { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2193         { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2194         { "signal1", &spufs_signal1_nosched_fops, 0222, },
2195         { "signal2", &spufs_signal2_nosched_fops, 0222, },
2196         { "signal1_type", &spufs_signal1_type, 0666, },
2197         { "signal2_type", &spufs_signal2_type, 0666, },
2198         { "cntl", &spufs_cntl_fops,  0666, },
2199         { "fpcr", &spufs_fpcr_fops, 0666, },
2200         { "lslr", &spufs_lslr_ops, 0444, },
2201         { "mfc", &spufs_mfc_fops, 0666, },
2202         { "mss", &spufs_mss_fops, 0666, },
2203         { "npc", &spufs_npc_ops, 0666, },
2204         { "srr0", &spufs_srr0_ops, 0666, },
2205         { "decr", &spufs_decr_ops, 0666, },
2206         { "decr_status", &spufs_decr_status_ops, 0666, },
2207         { "event_mask", &spufs_event_mask_ops, 0666, },
2208         { "event_status", &spufs_event_status_ops, 0444, },
2209         { "psmap", &spufs_psmap_fops, 0666, },
2210         { "phys-id", &spufs_id_ops, 0666, },
2211         { "object-id", &spufs_object_id_ops, 0666, },
2212         { "mbox_info", &spufs_mbox_info_fops, 0444, },
2213         { "ibox_info", &spufs_ibox_info_fops, 0444, },
2214         { "wbox_info", &spufs_wbox_info_fops, 0444, },
2215         { "dma_info", &spufs_dma_info_fops, 0444, },
2216         { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
2217         { "tid", &spufs_tid_fops, 0444, },
2218         { "stat", &spufs_stat_fops, 0444, },
2219         {},
2220 };
2221
2222 struct tree_descr spufs_dir_nosched_contents[] = {
2223         { "capabilities", &spufs_caps_fops, 0444, },
2224         { "mem",  &spufs_mem_fops,  0666, },
2225         { "mbox", &spufs_mbox_fops, 0444, },
2226         { "ibox", &spufs_ibox_fops, 0444, },
2227         { "wbox", &spufs_wbox_fops, 0222, },
2228         { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2229         { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2230         { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2231         { "signal1", &spufs_signal1_nosched_fops, 0222, },
2232         { "signal2", &spufs_signal2_nosched_fops, 0222, },
2233         { "signal1_type", &spufs_signal1_type, 0666, },
2234         { "signal2_type", &spufs_signal2_type, 0666, },
2235         { "mss", &spufs_mss_fops, 0666, },
2236         { "mfc", &spufs_mfc_fops, 0666, },
2237         { "cntl", &spufs_cntl_fops,  0666, },
2238         { "npc", &spufs_npc_ops, 0666, },
2239         { "psmap", &spufs_psmap_fops, 0666, },
2240         { "phys-id", &spufs_id_ops, 0666, },
2241         { "object-id", &spufs_object_id_ops, 0666, },
2242         { "tid", &spufs_tid_fops, 0444, },
2243         { "stat", &spufs_stat_fops, 0444, },
2244         {},
2245 };
2246
2247 struct spufs_coredump_reader spufs_coredump_read[] = {
2248         { "regs", __spufs_regs_read, NULL, 128 * 16 },
2249         { "fpcr", __spufs_fpcr_read, NULL, 16 },
2250         { "lslr", NULL, __spufs_lslr_get, 11 },
2251         { "decr", NULL, __spufs_decr_get, 11 },
2252         { "decr_status", NULL, __spufs_decr_status_get, 11 },
2253         { "mem", __spufs_mem_read, NULL, 256 * 1024, },
2254         { "signal1", __spufs_signal1_read, NULL, 4 },
2255         { "signal1_type", NULL, __spufs_signal1_type_get, 2 },
2256         { "signal2", __spufs_signal2_read, NULL, 4 },
2257         { "signal2_type", NULL, __spufs_signal2_type_get, 2 },
2258         { "event_mask", NULL, __spufs_event_mask_get, 8 },
2259         { "event_status", NULL, __spufs_event_status_get, 8 },
2260         { "mbox_info", __spufs_mbox_info_read, NULL, 4 },
2261         { "ibox_info", __spufs_ibox_info_read, NULL, 4 },
2262         { "wbox_info", __spufs_wbox_info_read, NULL, 16 },
2263         { "dma_info", __spufs_dma_info_read, NULL, 69 * 8 },
2264         { "proxydma_info", __spufs_proxydma_info_read, NULL, 35 * 8 },
2265         { "object-id", NULL, __spufs_object_id_get, 19 },
2266         { },
2267 };
2268 int spufs_coredump_num_notes = ARRAY_SIZE(spufs_coredump_read) - 1;
2269