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