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