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