Pull video into test branch
[sfrench/cifs-2.6.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         file->f_mapping = inode->i_mapping;
49         ctx->local_store = inode->i_mapping;
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 struct page *
99 spufs_mem_mmap_nopage(struct vm_area_struct *vma,
100                       unsigned long address, int *type)
101 {
102         struct page *page = NOPAGE_SIGBUS;
103
104         struct spu_context *ctx = vma->vm_file->private_data;
105         unsigned long offset = address - vma->vm_start;
106         offset += vma->vm_pgoff << PAGE_SHIFT;
107
108         spu_acquire(ctx);
109
110         if (ctx->state == SPU_STATE_SAVED) {
111                 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
112                                                         & ~_PAGE_NO_CACHE);
113                 page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
114         } else {
115                 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
116                                                         | _PAGE_NO_CACHE);
117                 page = pfn_to_page((ctx->spu->local_store_phys + offset)
118                                    >> PAGE_SHIFT);
119         }
120         spu_release(ctx);
121
122         if (type)
123                 *type = VM_FAULT_MINOR;
124
125         page_cache_get(page);
126         return page;
127 }
128
129 static struct vm_operations_struct spufs_mem_mmap_vmops = {
130         .nopage = spufs_mem_mmap_nopage,
131 };
132
133 static int
134 spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
135 {
136         if (!(vma->vm_flags & VM_SHARED))
137                 return -EINVAL;
138
139         vma->vm_flags |= VM_IO;
140         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
141                                      | _PAGE_NO_CACHE);
142
143         vma->vm_ops = &spufs_mem_mmap_vmops;
144         return 0;
145 }
146
147 static struct file_operations spufs_mem_fops = {
148         .open    = spufs_mem_open,
149         .read    = spufs_mem_read,
150         .write   = spufs_mem_write,
151         .llseek  = generic_file_llseek,
152         .mmap    = spufs_mem_mmap,
153 };
154
155 static struct page *spufs_ps_nopage(struct vm_area_struct *vma,
156                                     unsigned long address,
157                                     int *type, unsigned long ps_offs,
158                                     unsigned long ps_size)
159 {
160         struct page *page = NOPAGE_SIGBUS;
161         int fault_type = VM_FAULT_SIGBUS;
162         struct spu_context *ctx = vma->vm_file->private_data;
163         unsigned long offset = address - vma->vm_start;
164         unsigned long area;
165         int ret;
166
167         offset += vma->vm_pgoff << PAGE_SHIFT;
168         if (offset >= ps_size)
169                 goto out;
170
171         ret = spu_acquire_runnable(ctx);
172         if (ret)
173                 goto out;
174
175         area = ctx->spu->problem_phys + ps_offs;
176         page = pfn_to_page((area + offset) >> PAGE_SHIFT);
177         fault_type = VM_FAULT_MINOR;
178         page_cache_get(page);
179
180         spu_release(ctx);
181
182       out:
183         if (type)
184                 *type = fault_type;
185
186         return page;
187 }
188
189 #if SPUFS_MMAP_4K
190 static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma,
191                                            unsigned long address, int *type)
192 {
193         return spufs_ps_nopage(vma, address, type, 0x4000, 0x1000);
194 }
195
196 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
197         .nopage = spufs_cntl_mmap_nopage,
198 };
199
200 /*
201  * mmap support for problem state control area [0x4000 - 0x4fff].
202  */
203 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
204 {
205         if (!(vma->vm_flags & VM_SHARED))
206                 return -EINVAL;
207
208         vma->vm_flags |= VM_IO;
209         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
210                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
211
212         vma->vm_ops = &spufs_cntl_mmap_vmops;
213         return 0;
214 }
215 #else /* SPUFS_MMAP_4K */
216 #define spufs_cntl_mmap NULL
217 #endif /* !SPUFS_MMAP_4K */
218
219 static u64 spufs_cntl_get(void *data)
220 {
221         struct spu_context *ctx = data;
222         u64 val;
223
224         spu_acquire(ctx);
225         val = ctx->ops->status_read(ctx);
226         spu_release(ctx);
227
228         return val;
229 }
230
231 static void spufs_cntl_set(void *data, u64 val)
232 {
233         struct spu_context *ctx = data;
234
235         spu_acquire(ctx);
236         ctx->ops->runcntl_write(ctx, val);
237         spu_release(ctx);
238 }
239
240 static int spufs_cntl_open(struct inode *inode, struct file *file)
241 {
242         struct spufs_inode_info *i = SPUFS_I(inode);
243         struct spu_context *ctx = i->i_ctx;
244
245         file->private_data = ctx;
246         file->f_mapping = inode->i_mapping;
247         ctx->cntl = inode->i_mapping;
248         return simple_attr_open(inode, file, spufs_cntl_get,
249                                         spufs_cntl_set, "0x%08lx");
250 }
251
252 static struct file_operations spufs_cntl_fops = {
253         .open = spufs_cntl_open,
254         .release = simple_attr_close,
255         .read = simple_attr_read,
256         .write = simple_attr_write,
257         .mmap = spufs_cntl_mmap,
258 };
259
260 static int
261 spufs_regs_open(struct inode *inode, struct file *file)
262 {
263         struct spufs_inode_info *i = SPUFS_I(inode);
264         file->private_data = i->i_ctx;
265         return 0;
266 }
267
268 static ssize_t
269 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
270                         size_t size, loff_t *pos)
271 {
272         struct spu_lscsa *lscsa = ctx->csa.lscsa;
273         return simple_read_from_buffer(buffer, size, pos,
274                                       lscsa->gprs, sizeof lscsa->gprs);
275 }
276
277 static ssize_t
278 spufs_regs_read(struct file *file, char __user *buffer,
279                 size_t size, loff_t *pos)
280 {
281         int ret;
282         struct spu_context *ctx = file->private_data;
283
284         spu_acquire_saved(ctx);
285         ret = __spufs_regs_read(ctx, buffer, size, pos);
286         spu_release(ctx);
287         return ret;
288 }
289
290 static ssize_t
291 spufs_regs_write(struct file *file, const char __user *buffer,
292                  size_t size, loff_t *pos)
293 {
294         struct spu_context *ctx = file->private_data;
295         struct spu_lscsa *lscsa = ctx->csa.lscsa;
296         int ret;
297
298         size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
299         if (size <= 0)
300                 return -EFBIG;
301         *pos += size;
302
303         spu_acquire_saved(ctx);
304
305         ret = copy_from_user(lscsa->gprs + *pos - size,
306                              buffer, size) ? -EFAULT : size;
307
308         spu_release(ctx);
309         return ret;
310 }
311
312 static struct file_operations spufs_regs_fops = {
313         .open    = spufs_regs_open,
314         .read    = spufs_regs_read,
315         .write   = spufs_regs_write,
316         .llseek  = generic_file_llseek,
317 };
318
319 static ssize_t
320 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
321                         size_t size, loff_t * pos)
322 {
323         struct spu_lscsa *lscsa = ctx->csa.lscsa;
324         return simple_read_from_buffer(buffer, size, pos,
325                                       &lscsa->fpcr, sizeof(lscsa->fpcr));
326 }
327
328 static ssize_t
329 spufs_fpcr_read(struct file *file, char __user * buffer,
330                 size_t size, loff_t * pos)
331 {
332         int ret;
333         struct spu_context *ctx = file->private_data;
334
335         spu_acquire_saved(ctx);
336         ret = __spufs_fpcr_read(ctx, buffer, size, pos);
337         spu_release(ctx);
338         return ret;
339 }
340
341 static ssize_t
342 spufs_fpcr_write(struct file *file, const char __user * buffer,
343                  size_t size, loff_t * pos)
344 {
345         struct spu_context *ctx = file->private_data;
346         struct spu_lscsa *lscsa = ctx->csa.lscsa;
347         int ret;
348
349         size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
350         if (size <= 0)
351                 return -EFBIG;
352         *pos += size;
353
354         spu_acquire_saved(ctx);
355
356         ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
357                              buffer, size) ? -EFAULT : size;
358
359         spu_release(ctx);
360         return ret;
361 }
362
363 static struct file_operations spufs_fpcr_fops = {
364         .open = spufs_regs_open,
365         .read = spufs_fpcr_read,
366         .write = spufs_fpcr_write,
367         .llseek = generic_file_llseek,
368 };
369
370 /* generic open function for all pipe-like files */
371 static int spufs_pipe_open(struct inode *inode, struct file *file)
372 {
373         struct spufs_inode_info *i = SPUFS_I(inode);
374         file->private_data = i->i_ctx;
375
376         return nonseekable_open(inode, file);
377 }
378
379 /*
380  * Read as many bytes from the mailbox as possible, until
381  * one of the conditions becomes true:
382  *
383  * - no more data available in the mailbox
384  * - end of the user provided buffer
385  * - end of the mapped area
386  */
387 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
388                         size_t len, loff_t *pos)
389 {
390         struct spu_context *ctx = file->private_data;
391         u32 mbox_data, __user *udata;
392         ssize_t count;
393
394         if (len < 4)
395                 return -EINVAL;
396
397         if (!access_ok(VERIFY_WRITE, buf, len))
398                 return -EFAULT;
399
400         udata = (void __user *)buf;
401
402         spu_acquire(ctx);
403         for (count = 0; (count + 4) <= len; count += 4, udata++) {
404                 int ret;
405                 ret = ctx->ops->mbox_read(ctx, &mbox_data);
406                 if (ret == 0)
407                         break;
408
409                 /*
410                  * at the end of the mapped area, we can fault
411                  * but still need to return the data we have
412                  * read successfully so far.
413                  */
414                 ret = __put_user(mbox_data, udata);
415                 if (ret) {
416                         if (!count)
417                                 count = -EFAULT;
418                         break;
419                 }
420         }
421         spu_release(ctx);
422
423         if (!count)
424                 count = -EAGAIN;
425
426         return count;
427 }
428
429 static struct file_operations spufs_mbox_fops = {
430         .open   = spufs_pipe_open,
431         .read   = spufs_mbox_read,
432 };
433
434 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
435                         size_t len, loff_t *pos)
436 {
437         struct spu_context *ctx = file->private_data;
438         u32 mbox_stat;
439
440         if (len < 4)
441                 return -EINVAL;
442
443         spu_acquire(ctx);
444
445         mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
446
447         spu_release(ctx);
448
449         if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
450                 return -EFAULT;
451
452         return 4;
453 }
454
455 static struct file_operations spufs_mbox_stat_fops = {
456         .open   = spufs_pipe_open,
457         .read   = spufs_mbox_stat_read,
458 };
459
460 /* low-level ibox access function */
461 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
462 {
463         return ctx->ops->ibox_read(ctx, data);
464 }
465
466 static int spufs_ibox_fasync(int fd, struct file *file, int on)
467 {
468         struct spu_context *ctx = file->private_data;
469
470         return fasync_helper(fd, file, on, &ctx->ibox_fasync);
471 }
472
473 /* interrupt-level ibox callback function. */
474 void spufs_ibox_callback(struct spu *spu)
475 {
476         struct spu_context *ctx = spu->ctx;
477
478         wake_up_all(&ctx->ibox_wq);
479         kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
480 }
481
482 /*
483  * Read as many bytes from the interrupt mailbox as possible, until
484  * one of the conditions becomes true:
485  *
486  * - no more data available in the mailbox
487  * - end of the user provided buffer
488  * - end of the mapped area
489  *
490  * If the file is opened without O_NONBLOCK, we wait here until
491  * any data is available, but return when we have been able to
492  * read something.
493  */
494 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
495                         size_t len, loff_t *pos)
496 {
497         struct spu_context *ctx = file->private_data;
498         u32 ibox_data, __user *udata;
499         ssize_t count;
500
501         if (len < 4)
502                 return -EINVAL;
503
504         if (!access_ok(VERIFY_WRITE, buf, len))
505                 return -EFAULT;
506
507         udata = (void __user *)buf;
508
509         spu_acquire(ctx);
510
511         /* wait only for the first element */
512         count = 0;
513         if (file->f_flags & O_NONBLOCK) {
514                 if (!spu_ibox_read(ctx, &ibox_data))
515                         count = -EAGAIN;
516         } else {
517                 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
518         }
519         if (count)
520                 goto out;
521
522         /* if we can't write at all, return -EFAULT */
523         count = __put_user(ibox_data, udata);
524         if (count)
525                 goto out;
526
527         for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
528                 int ret;
529                 ret = ctx->ops->ibox_read(ctx, &ibox_data);
530                 if (ret == 0)
531                         break;
532                 /*
533                  * at the end of the mapped area, we can fault
534                  * but still need to return the data we have
535                  * read successfully so far.
536                  */
537                 ret = __put_user(ibox_data, udata);
538                 if (ret)
539                         break;
540         }
541
542 out:
543         spu_release(ctx);
544
545         return count;
546 }
547
548 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
549 {
550         struct spu_context *ctx = file->private_data;
551         unsigned int mask;
552
553         poll_wait(file, &ctx->ibox_wq, wait);
554
555         spu_acquire(ctx);
556         mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
557         spu_release(ctx);
558
559         return mask;
560 }
561
562 static struct file_operations spufs_ibox_fops = {
563         .open   = spufs_pipe_open,
564         .read   = spufs_ibox_read,
565         .poll   = spufs_ibox_poll,
566         .fasync = spufs_ibox_fasync,
567 };
568
569 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
570                         size_t len, loff_t *pos)
571 {
572         struct spu_context *ctx = file->private_data;
573         u32 ibox_stat;
574
575         if (len < 4)
576                 return -EINVAL;
577
578         spu_acquire(ctx);
579         ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
580         spu_release(ctx);
581
582         if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
583                 return -EFAULT;
584
585         return 4;
586 }
587
588 static struct file_operations spufs_ibox_stat_fops = {
589         .open   = spufs_pipe_open,
590         .read   = spufs_ibox_stat_read,
591 };
592
593 /* low-level mailbox write */
594 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
595 {
596         return ctx->ops->wbox_write(ctx, data);
597 }
598
599 static int spufs_wbox_fasync(int fd, struct file *file, int on)
600 {
601         struct spu_context *ctx = file->private_data;
602         int ret;
603
604         ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
605
606         return ret;
607 }
608
609 /* interrupt-level wbox callback function. */
610 void spufs_wbox_callback(struct spu *spu)
611 {
612         struct spu_context *ctx = spu->ctx;
613
614         wake_up_all(&ctx->wbox_wq);
615         kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
616 }
617
618 /*
619  * Write as many bytes to the interrupt mailbox as possible, until
620  * one of the conditions becomes true:
621  *
622  * - the mailbox is full
623  * - end of the user provided buffer
624  * - end of the mapped area
625  *
626  * If the file is opened without O_NONBLOCK, we wait here until
627  * space is availabyl, but return when we have been able to
628  * write something.
629  */
630 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
631                         size_t len, loff_t *pos)
632 {
633         struct spu_context *ctx = file->private_data;
634         u32 wbox_data, __user *udata;
635         ssize_t count;
636
637         if (len < 4)
638                 return -EINVAL;
639
640         udata = (void __user *)buf;
641         if (!access_ok(VERIFY_READ, buf, len))
642                 return -EFAULT;
643
644         if (__get_user(wbox_data, udata))
645                 return -EFAULT;
646
647         spu_acquire(ctx);
648
649         /*
650          * make sure we can at least write one element, by waiting
651          * in case of !O_NONBLOCK
652          */
653         count = 0;
654         if (file->f_flags & O_NONBLOCK) {
655                 if (!spu_wbox_write(ctx, wbox_data))
656                         count = -EAGAIN;
657         } else {
658                 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
659         }
660
661         if (count)
662                 goto out;
663
664         /* write aÑ• much as possible */
665         for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
666                 int ret;
667                 ret = __get_user(wbox_data, udata);
668                 if (ret)
669                         break;
670
671                 ret = spu_wbox_write(ctx, wbox_data);
672                 if (ret == 0)
673                         break;
674         }
675
676 out:
677         spu_release(ctx);
678         return count;
679 }
680
681 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
682 {
683         struct spu_context *ctx = file->private_data;
684         unsigned int mask;
685
686         poll_wait(file, &ctx->wbox_wq, wait);
687
688         spu_acquire(ctx);
689         mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
690         spu_release(ctx);
691
692         return mask;
693 }
694
695 static struct file_operations spufs_wbox_fops = {
696         .open   = spufs_pipe_open,
697         .write  = spufs_wbox_write,
698         .poll   = spufs_wbox_poll,
699         .fasync = spufs_wbox_fasync,
700 };
701
702 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
703                         size_t len, loff_t *pos)
704 {
705         struct spu_context *ctx = file->private_data;
706         u32 wbox_stat;
707
708         if (len < 4)
709                 return -EINVAL;
710
711         spu_acquire(ctx);
712         wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
713         spu_release(ctx);
714
715         if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
716                 return -EFAULT;
717
718         return 4;
719 }
720
721 static struct file_operations spufs_wbox_stat_fops = {
722         .open   = spufs_pipe_open,
723         .read   = spufs_wbox_stat_read,
724 };
725
726 static int spufs_signal1_open(struct inode *inode, struct file *file)
727 {
728         struct spufs_inode_info *i = SPUFS_I(inode);
729         struct spu_context *ctx = i->i_ctx;
730         file->private_data = ctx;
731         file->f_mapping = inode->i_mapping;
732         ctx->signal1 = inode->i_mapping;
733         return nonseekable_open(inode, file);
734 }
735
736 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
737                         size_t len, loff_t *pos)
738 {
739         int ret = 0;
740         u32 data;
741
742         if (len < 4)
743                 return -EINVAL;
744
745         if (ctx->csa.spu_chnlcnt_RW[3]) {
746                 data = ctx->csa.spu_chnldata_RW[3];
747                 ret = 4;
748         }
749
750         if (!ret)
751                 goto out;
752
753         if (copy_to_user(buf, &data, 4))
754                 return -EFAULT;
755
756 out:
757         return ret;
758 }
759
760 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
761                         size_t len, loff_t *pos)
762 {
763         int ret;
764         struct spu_context *ctx = file->private_data;
765
766         spu_acquire_saved(ctx);
767         ret = __spufs_signal1_read(ctx, buf, len, pos);
768         spu_release(ctx);
769
770         return ret;
771 }
772
773 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
774                         size_t len, loff_t *pos)
775 {
776         struct spu_context *ctx;
777         u32 data;
778
779         ctx = file->private_data;
780
781         if (len < 4)
782                 return -EINVAL;
783
784         if (copy_from_user(&data, buf, 4))
785                 return -EFAULT;
786
787         spu_acquire(ctx);
788         ctx->ops->signal1_write(ctx, data);
789         spu_release(ctx);
790
791         return 4;
792 }
793
794 static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
795                                               unsigned long address, int *type)
796 {
797 #if PAGE_SIZE == 0x1000
798         return spufs_ps_nopage(vma, address, type, 0x14000, 0x1000);
799 #elif PAGE_SIZE == 0x10000
800         /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
801          * signal 1 and 2 area
802          */
803         return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
804 #else
805 #error unsupported page size
806 #endif
807 }
808
809 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
810         .nopage = spufs_signal1_mmap_nopage,
811 };
812
813 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
814 {
815         if (!(vma->vm_flags & VM_SHARED))
816                 return -EINVAL;
817
818         vma->vm_flags |= VM_IO;
819         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
820                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
821
822         vma->vm_ops = &spufs_signal1_mmap_vmops;
823         return 0;
824 }
825
826 static struct file_operations spufs_signal1_fops = {
827         .open = spufs_signal1_open,
828         .read = spufs_signal1_read,
829         .write = spufs_signal1_write,
830         .mmap = spufs_signal1_mmap,
831 };
832
833 static int spufs_signal2_open(struct inode *inode, struct file *file)
834 {
835         struct spufs_inode_info *i = SPUFS_I(inode);
836         struct spu_context *ctx = i->i_ctx;
837         file->private_data = ctx;
838         file->f_mapping = inode->i_mapping;
839         ctx->signal2 = inode->i_mapping;
840         return nonseekable_open(inode, file);
841 }
842
843 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
844                         size_t len, loff_t *pos)
845 {
846         int ret = 0;
847         u32 data;
848
849         if (len < 4)
850                 return -EINVAL;
851
852         if (ctx->csa.spu_chnlcnt_RW[4]) {
853                 data =  ctx->csa.spu_chnldata_RW[4];
854                 ret = 4;
855         }
856
857         if (!ret)
858                 goto out;
859
860         if (copy_to_user(buf, &data, 4))
861                 return -EFAULT;
862
863 out:
864         return ret;
865 }
866
867 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
868                         size_t len, loff_t *pos)
869 {
870         struct spu_context *ctx = file->private_data;
871         int ret;
872
873         spu_acquire_saved(ctx);
874         ret = __spufs_signal2_read(ctx, buf, len, pos);
875         spu_release(ctx);
876
877         return ret;
878 }
879
880 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
881                         size_t len, loff_t *pos)
882 {
883         struct spu_context *ctx;
884         u32 data;
885
886         ctx = file->private_data;
887
888         if (len < 4)
889                 return -EINVAL;
890
891         if (copy_from_user(&data, buf, 4))
892                 return -EFAULT;
893
894         spu_acquire(ctx);
895         ctx->ops->signal2_write(ctx, data);
896         spu_release(ctx);
897
898         return 4;
899 }
900
901 #if SPUFS_MMAP_4K
902 static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
903                                               unsigned long address, int *type)
904 {
905 #if PAGE_SIZE == 0x1000
906         return spufs_ps_nopage(vma, address, type, 0x1c000, 0x1000);
907 #elif PAGE_SIZE == 0x10000
908         /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
909          * signal 1 and 2 area
910          */
911         return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
912 #else
913 #error unsupported page size
914 #endif
915 }
916
917 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
918         .nopage = spufs_signal2_mmap_nopage,
919 };
920
921 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
922 {
923         if (!(vma->vm_flags & VM_SHARED))
924                 return -EINVAL;
925
926         vma->vm_flags |= VM_IO;
927         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
928                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
929
930         vma->vm_ops = &spufs_signal2_mmap_vmops;
931         return 0;
932 }
933 #else /* SPUFS_MMAP_4K */
934 #define spufs_signal2_mmap NULL
935 #endif /* !SPUFS_MMAP_4K */
936
937 static struct file_operations spufs_signal2_fops = {
938         .open = spufs_signal2_open,
939         .read = spufs_signal2_read,
940         .write = spufs_signal2_write,
941         .mmap = spufs_signal2_mmap,
942 };
943
944 static void spufs_signal1_type_set(void *data, u64 val)
945 {
946         struct spu_context *ctx = data;
947
948         spu_acquire(ctx);
949         ctx->ops->signal1_type_set(ctx, val);
950         spu_release(ctx);
951 }
952
953 static u64 __spufs_signal1_type_get(void *data)
954 {
955         struct spu_context *ctx = data;
956         return ctx->ops->signal1_type_get(ctx);
957 }
958
959 static u64 spufs_signal1_type_get(void *data)
960 {
961         struct spu_context *ctx = data;
962         u64 ret;
963
964         spu_acquire(ctx);
965         ret = __spufs_signal1_type_get(data);
966         spu_release(ctx);
967
968         return ret;
969 }
970 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
971                                         spufs_signal1_type_set, "%llu");
972
973 static void spufs_signal2_type_set(void *data, u64 val)
974 {
975         struct spu_context *ctx = data;
976
977         spu_acquire(ctx);
978         ctx->ops->signal2_type_set(ctx, val);
979         spu_release(ctx);
980 }
981
982 static u64 __spufs_signal2_type_get(void *data)
983 {
984         struct spu_context *ctx = data;
985         return ctx->ops->signal2_type_get(ctx);
986 }
987
988 static u64 spufs_signal2_type_get(void *data)
989 {
990         struct spu_context *ctx = data;
991         u64 ret;
992
993         spu_acquire(ctx);
994         ret = __spufs_signal2_type_get(data);
995         spu_release(ctx);
996
997         return ret;
998 }
999 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1000                                         spufs_signal2_type_set, "%llu");
1001
1002 #if SPUFS_MMAP_4K
1003 static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
1004                                            unsigned long address, int *type)
1005 {
1006         return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000);
1007 }
1008
1009 static struct vm_operations_struct spufs_mss_mmap_vmops = {
1010         .nopage = spufs_mss_mmap_nopage,
1011 };
1012
1013 /*
1014  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1015  */
1016 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1017 {
1018         if (!(vma->vm_flags & VM_SHARED))
1019                 return -EINVAL;
1020
1021         vma->vm_flags |= VM_IO;
1022         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1023                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
1024
1025         vma->vm_ops = &spufs_mss_mmap_vmops;
1026         return 0;
1027 }
1028 #else /* SPUFS_MMAP_4K */
1029 #define spufs_mss_mmap NULL
1030 #endif /* !SPUFS_MMAP_4K */
1031
1032 static int spufs_mss_open(struct inode *inode, struct file *file)
1033 {
1034         struct spufs_inode_info *i = SPUFS_I(inode);
1035
1036         file->private_data = i->i_ctx;
1037         return nonseekable_open(inode, file);
1038 }
1039
1040 static struct file_operations spufs_mss_fops = {
1041         .open    = spufs_mss_open,
1042         .mmap    = spufs_mss_mmap,
1043 };
1044
1045 static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma,
1046                                            unsigned long address, int *type)
1047 {
1048         return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000);
1049 }
1050
1051 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1052         .nopage = spufs_psmap_mmap_nopage,
1053 };
1054
1055 /*
1056  * mmap support for full problem state area [0x00000 - 0x1ffff].
1057  */
1058 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1059 {
1060         if (!(vma->vm_flags & VM_SHARED))
1061                 return -EINVAL;
1062
1063         vma->vm_flags |= VM_IO;
1064         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1065                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
1066
1067         vma->vm_ops = &spufs_psmap_mmap_vmops;
1068         return 0;
1069 }
1070
1071 static int spufs_psmap_open(struct inode *inode, struct file *file)
1072 {
1073         struct spufs_inode_info *i = SPUFS_I(inode);
1074
1075         file->private_data = i->i_ctx;
1076         return nonseekable_open(inode, file);
1077 }
1078
1079 static struct file_operations spufs_psmap_fops = {
1080         .open    = spufs_psmap_open,
1081         .mmap    = spufs_psmap_mmap,
1082 };
1083
1084
1085 #if SPUFS_MMAP_4K
1086 static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
1087                                            unsigned long address, int *type)
1088 {
1089         return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000);
1090 }
1091
1092 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1093         .nopage = spufs_mfc_mmap_nopage,
1094 };
1095
1096 /*
1097  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1098  */
1099 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1100 {
1101         if (!(vma->vm_flags & VM_SHARED))
1102                 return -EINVAL;
1103
1104         vma->vm_flags |= VM_IO;
1105         vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1106                                      | _PAGE_NO_CACHE | _PAGE_GUARDED);
1107
1108         vma->vm_ops = &spufs_mfc_mmap_vmops;
1109         return 0;
1110 }
1111 #else /* SPUFS_MMAP_4K */
1112 #define spufs_mfc_mmap NULL
1113 #endif /* !SPUFS_MMAP_4K */
1114
1115 static int spufs_mfc_open(struct inode *inode, struct file *file)
1116 {
1117         struct spufs_inode_info *i = SPUFS_I(inode);
1118         struct spu_context *ctx = i->i_ctx;
1119
1120         /* we don't want to deal with DMA into other processes */
1121         if (ctx->owner != current->mm)
1122                 return -EINVAL;
1123
1124         if (atomic_read(&inode->i_count) != 1)
1125                 return -EBUSY;
1126
1127         file->private_data = ctx;
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);
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 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 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 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 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 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 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