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