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