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