]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/powerpc/platforms/cell/spufs/file.c
libfs: allow error return from simple attributes
[mirror_ubuntu-artful-kernel.git] / arch / powerpc / platforms / cell / spufs / file.c
1 /*
2 * SPU file system -- file contents
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #undef DEBUG
24
25 #include <linux/fs.h>
26 #include <linux/ioctl.h>
27 #include <linux/module.h>
28 #include <linux/pagemap.h>
29 #include <linux/poll.h>
30 #include <linux/ptrace.h>
31 #include <linux/seq_file.h>
32 #include <linux/marker.h>
33
34 #include <asm/io.h>
35 #include <asm/semaphore.h>
36 #include <asm/spu.h>
37 #include <asm/spu_info.h>
38 #include <asm/uaccess.h>
39
40 #include "spufs.h"
41
42 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
43
44 /* Simple attribute files */
45 struct spufs_attr {
46 int (*get)(void *, u64 *);
47 int (*set)(void *, u64);
48 char get_buf[24]; /* enough to store a u64 and "\n\0" */
49 char set_buf[24];
50 void *data;
51 const char *fmt; /* format for read operation */
52 struct mutex mutex; /* protects access to these buffers */
53 };
54
55 static int spufs_attr_open(struct inode *inode, struct file *file,
56 int (*get)(void *, u64 *), int (*set)(void *, u64),
57 const char *fmt)
58 {
59 struct spufs_attr *attr;
60
61 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
62 if (!attr)
63 return -ENOMEM;
64
65 attr->get = get;
66 attr->set = set;
67 attr->data = inode->i_private;
68 attr->fmt = fmt;
69 mutex_init(&attr->mutex);
70 file->private_data = attr;
71
72 return nonseekable_open(inode, file);
73 }
74
75 static int spufs_attr_release(struct inode *inode, struct file *file)
76 {
77 kfree(file->private_data);
78 return 0;
79 }
80
81 static ssize_t spufs_attr_read(struct file *file, char __user *buf,
82 size_t len, loff_t *ppos)
83 {
84 struct spufs_attr *attr;
85 size_t size;
86 ssize_t ret;
87
88 attr = file->private_data;
89 if (!attr->get)
90 return -EACCES;
91
92 ret = mutex_lock_interruptible(&attr->mutex);
93 if (ret)
94 return ret;
95
96 if (*ppos) { /* continued read */
97 size = strlen(attr->get_buf);
98 } else { /* first read */
99 u64 val;
100 ret = attr->get(attr->data, &val);
101 if (ret)
102 goto out;
103
104 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
105 attr->fmt, (unsigned long long)val);
106 }
107
108 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
109 out:
110 mutex_unlock(&attr->mutex);
111 return ret;
112 }
113
114 static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
115 size_t len, loff_t *ppos)
116 {
117 struct spufs_attr *attr;
118 u64 val;
119 size_t size;
120 ssize_t ret;
121
122 attr = file->private_data;
123 if (!attr->set)
124 return -EACCES;
125
126 ret = mutex_lock_interruptible(&attr->mutex);
127 if (ret)
128 return ret;
129
130 ret = -EFAULT;
131 size = min(sizeof(attr->set_buf) - 1, len);
132 if (copy_from_user(attr->set_buf, buf, size))
133 goto out;
134
135 ret = len; /* claim we got the whole input */
136 attr->set_buf[size] = '\0';
137 val = simple_strtol(attr->set_buf, NULL, 0);
138 attr->set(attr->data, val);
139 out:
140 mutex_unlock(&attr->mutex);
141 return ret;
142 }
143
144 #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
145 static int __fops ## _open(struct inode *inode, struct file *file) \
146 { \
147 __simple_attr_check_format(__fmt, 0ull); \
148 return spufs_attr_open(inode, file, __get, __set, __fmt); \
149 } \
150 static struct file_operations __fops = { \
151 .owner = THIS_MODULE, \
152 .open = __fops ## _open, \
153 .release = spufs_attr_release, \
154 .read = spufs_attr_read, \
155 .write = spufs_attr_write, \
156 };
157
158
159 static int
160 spufs_mem_open(struct inode *inode, struct file *file)
161 {
162 struct spufs_inode_info *i = SPUFS_I(inode);
163 struct spu_context *ctx = i->i_ctx;
164
165 mutex_lock(&ctx->mapping_lock);
166 file->private_data = ctx;
167 if (!i->i_openers++)
168 ctx->local_store = inode->i_mapping;
169 mutex_unlock(&ctx->mapping_lock);
170 return 0;
171 }
172
173 static int
174 spufs_mem_release(struct inode *inode, struct file *file)
175 {
176 struct spufs_inode_info *i = SPUFS_I(inode);
177 struct spu_context *ctx = i->i_ctx;
178
179 mutex_lock(&ctx->mapping_lock);
180 if (!--i->i_openers)
181 ctx->local_store = NULL;
182 mutex_unlock(&ctx->mapping_lock);
183 return 0;
184 }
185
186 static ssize_t
187 __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
188 size_t size, loff_t *pos)
189 {
190 char *local_store = ctx->ops->get_ls(ctx);
191 return simple_read_from_buffer(buffer, size, pos, local_store,
192 LS_SIZE);
193 }
194
195 static ssize_t
196 spufs_mem_read(struct file *file, char __user *buffer,
197 size_t size, loff_t *pos)
198 {
199 struct spu_context *ctx = file->private_data;
200 ssize_t ret;
201
202 ret = spu_acquire(ctx);
203 if (ret)
204 return ret;
205 ret = __spufs_mem_read(ctx, buffer, size, pos);
206 spu_release(ctx);
207
208 return ret;
209 }
210
211 static ssize_t
212 spufs_mem_write(struct file *file, const char __user *buffer,
213 size_t size, loff_t *ppos)
214 {
215 struct spu_context *ctx = file->private_data;
216 char *local_store;
217 loff_t pos = *ppos;
218 int ret;
219
220 if (pos < 0)
221 return -EINVAL;
222 if (pos > LS_SIZE)
223 return -EFBIG;
224 if (size > LS_SIZE - pos)
225 size = LS_SIZE - pos;
226
227 ret = spu_acquire(ctx);
228 if (ret)
229 return ret;
230
231 local_store = ctx->ops->get_ls(ctx);
232 ret = copy_from_user(local_store + pos, buffer, size);
233 spu_release(ctx);
234
235 if (ret)
236 return -EFAULT;
237 *ppos = pos + size;
238 return size;
239 }
240
241 static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
242 unsigned long address)
243 {
244 struct spu_context *ctx = vma->vm_file->private_data;
245 unsigned long pfn, offset, addr0 = address;
246 #ifdef CONFIG_SPU_FS_64K_LS
247 struct spu_state *csa = &ctx->csa;
248 int psize;
249
250 /* Check what page size we are using */
251 psize = get_slice_psize(vma->vm_mm, address);
252
253 /* Some sanity checking */
254 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
255
256 /* Wow, 64K, cool, we need to align the address though */
257 if (csa->use_big_pages) {
258 BUG_ON(vma->vm_start & 0xffff);
259 address &= ~0xfffful;
260 }
261 #endif /* CONFIG_SPU_FS_64K_LS */
262
263 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
264 if (offset >= LS_SIZE)
265 return NOPFN_SIGBUS;
266
267 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
268 addr0, address, offset);
269
270 if (spu_acquire(ctx))
271 return NOPFN_REFAULT;
272
273 if (ctx->state == SPU_STATE_SAVED) {
274 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
275 & ~_PAGE_NO_CACHE);
276 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
277 } else {
278 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
279 | _PAGE_NO_CACHE);
280 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
281 }
282 vm_insert_pfn(vma, address, pfn);
283
284 spu_release(ctx);
285
286 return NOPFN_REFAULT;
287 }
288
289
290 static struct vm_operations_struct spufs_mem_mmap_vmops = {
291 .nopfn = spufs_mem_mmap_nopfn,
292 };
293
294 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
295 {
296 #ifdef CONFIG_SPU_FS_64K_LS
297 struct spu_context *ctx = file->private_data;
298 struct spu_state *csa = &ctx->csa;
299
300 /* Sanity check VMA alignment */
301 if (csa->use_big_pages) {
302 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
303 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
304 vma->vm_pgoff);
305 if (vma->vm_start & 0xffff)
306 return -EINVAL;
307 if (vma->vm_pgoff & 0xf)
308 return -EINVAL;
309 }
310 #endif /* CONFIG_SPU_FS_64K_LS */
311
312 if (!(vma->vm_flags & VM_SHARED))
313 return -EINVAL;
314
315 vma->vm_flags |= VM_IO | VM_PFNMAP;
316 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
317 | _PAGE_NO_CACHE);
318
319 vma->vm_ops = &spufs_mem_mmap_vmops;
320 return 0;
321 }
322
323 #ifdef CONFIG_SPU_FS_64K_LS
324 static unsigned long spufs_get_unmapped_area(struct file *file,
325 unsigned long addr, unsigned long len, unsigned long pgoff,
326 unsigned long flags)
327 {
328 struct spu_context *ctx = file->private_data;
329 struct spu_state *csa = &ctx->csa;
330
331 /* If not using big pages, fallback to normal MM g_u_a */
332 if (!csa->use_big_pages)
333 return current->mm->get_unmapped_area(file, addr, len,
334 pgoff, flags);
335
336 /* Else, try to obtain a 64K pages slice */
337 return slice_get_unmapped_area(addr, len, flags,
338 MMU_PAGE_64K, 1, 0);
339 }
340 #endif /* CONFIG_SPU_FS_64K_LS */
341
342 static const struct file_operations spufs_mem_fops = {
343 .open = spufs_mem_open,
344 .release = spufs_mem_release,
345 .read = spufs_mem_read,
346 .write = spufs_mem_write,
347 .llseek = generic_file_llseek,
348 .mmap = spufs_mem_mmap,
349 #ifdef CONFIG_SPU_FS_64K_LS
350 .get_unmapped_area = spufs_get_unmapped_area,
351 #endif
352 };
353
354 static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
355 unsigned long address,
356 unsigned long ps_offs,
357 unsigned long ps_size)
358 {
359 struct spu_context *ctx = vma->vm_file->private_data;
360 unsigned long area, offset = address - vma->vm_start;
361
362 spu_context_nospu_trace(spufs_ps_nopfn__enter, ctx);
363
364 offset += vma->vm_pgoff << PAGE_SHIFT;
365 if (offset >= ps_size)
366 return NOPFN_SIGBUS;
367
368 /*
369 * We have to wait for context to be loaded before we have
370 * pages to hand out to the user, but we don't want to wait
371 * with the mmap_sem held.
372 * It is possible to drop the mmap_sem here, but then we need
373 * to return NOPFN_REFAULT because the mappings may have
374 * hanged.
375 */
376 if (spu_acquire(ctx))
377 return NOPFN_REFAULT;
378
379 if (ctx->state == SPU_STATE_SAVED) {
380 up_read(&current->mm->mmap_sem);
381 spu_context_nospu_trace(spufs_ps_nopfn__sleep, ctx);
382 spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
383 spu_context_trace(spufs_ps_nopfn__wake, ctx, ctx->spu);
384 down_read(&current->mm->mmap_sem);
385 } else {
386 area = ctx->spu->problem_phys + ps_offs;
387 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
388 spu_context_trace(spufs_ps_nopfn__insert, ctx, ctx->spu);
389 }
390
391 spu_release(ctx);
392 return NOPFN_REFAULT;
393 }
394
395 #if SPUFS_MMAP_4K
396 static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
397 unsigned long address)
398 {
399 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
400 }
401
402 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
403 .nopfn = spufs_cntl_mmap_nopfn,
404 };
405
406 /*
407 * mmap support for problem state control area [0x4000 - 0x4fff].
408 */
409 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
410 {
411 if (!(vma->vm_flags & VM_SHARED))
412 return -EINVAL;
413
414 vma->vm_flags |= VM_IO | VM_PFNMAP;
415 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
416 | _PAGE_NO_CACHE | _PAGE_GUARDED);
417
418 vma->vm_ops = &spufs_cntl_mmap_vmops;
419 return 0;
420 }
421 #else /* SPUFS_MMAP_4K */
422 #define spufs_cntl_mmap NULL
423 #endif /* !SPUFS_MMAP_4K */
424
425 static int spufs_cntl_get(void *data, u64 *val)
426 {
427 struct spu_context *ctx = data;
428 int ret;
429
430 ret = spu_acquire(ctx);
431 if (ret)
432 return ret;
433 *val = ctx->ops->status_read(ctx);
434 spu_release(ctx);
435
436 return 0;
437 }
438
439 static int spufs_cntl_set(void *data, u64 val)
440 {
441 struct spu_context *ctx = data;
442 int ret;
443
444 ret = spu_acquire(ctx);
445 if (ret)
446 return ret;
447 ctx->ops->runcntl_write(ctx, val);
448 spu_release(ctx);
449
450 return 0;
451 }
452
453 static int spufs_cntl_open(struct inode *inode, struct file *file)
454 {
455 struct spufs_inode_info *i = SPUFS_I(inode);
456 struct spu_context *ctx = i->i_ctx;
457
458 mutex_lock(&ctx->mapping_lock);
459 file->private_data = ctx;
460 if (!i->i_openers++)
461 ctx->cntl = inode->i_mapping;
462 mutex_unlock(&ctx->mapping_lock);
463 return simple_attr_open(inode, file, spufs_cntl_get,
464 spufs_cntl_set, "0x%08lx");
465 }
466
467 static int
468 spufs_cntl_release(struct inode *inode, struct file *file)
469 {
470 struct spufs_inode_info *i = SPUFS_I(inode);
471 struct spu_context *ctx = i->i_ctx;
472
473 simple_attr_close(inode, file);
474
475 mutex_lock(&ctx->mapping_lock);
476 if (!--i->i_openers)
477 ctx->cntl = NULL;
478 mutex_unlock(&ctx->mapping_lock);
479 return 0;
480 }
481
482 static const struct file_operations spufs_cntl_fops = {
483 .open = spufs_cntl_open,
484 .release = spufs_cntl_release,
485 .read = simple_attr_read,
486 .write = simple_attr_write,
487 .mmap = spufs_cntl_mmap,
488 };
489
490 static int
491 spufs_regs_open(struct inode *inode, struct file *file)
492 {
493 struct spufs_inode_info *i = SPUFS_I(inode);
494 file->private_data = i->i_ctx;
495 return 0;
496 }
497
498 static ssize_t
499 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
500 size_t size, loff_t *pos)
501 {
502 struct spu_lscsa *lscsa = ctx->csa.lscsa;
503 return simple_read_from_buffer(buffer, size, pos,
504 lscsa->gprs, sizeof lscsa->gprs);
505 }
506
507 static ssize_t
508 spufs_regs_read(struct file *file, char __user *buffer,
509 size_t size, loff_t *pos)
510 {
511 int ret;
512 struct spu_context *ctx = file->private_data;
513
514 ret = spu_acquire_saved(ctx);
515 if (ret)
516 return ret;
517 ret = __spufs_regs_read(ctx, buffer, size, pos);
518 spu_release_saved(ctx);
519 return ret;
520 }
521
522 static ssize_t
523 spufs_regs_write(struct file *file, const char __user *buffer,
524 size_t size, loff_t *pos)
525 {
526 struct spu_context *ctx = file->private_data;
527 struct spu_lscsa *lscsa = ctx->csa.lscsa;
528 int ret;
529
530 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
531 if (size <= 0)
532 return -EFBIG;
533 *pos += size;
534
535 ret = spu_acquire_saved(ctx);
536 if (ret)
537 return ret;
538
539 ret = copy_from_user(lscsa->gprs + *pos - size,
540 buffer, size) ? -EFAULT : size;
541
542 spu_release_saved(ctx);
543 return ret;
544 }
545
546 static const struct file_operations spufs_regs_fops = {
547 .open = spufs_regs_open,
548 .read = spufs_regs_read,
549 .write = spufs_regs_write,
550 .llseek = generic_file_llseek,
551 };
552
553 static ssize_t
554 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
555 size_t size, loff_t * pos)
556 {
557 struct spu_lscsa *lscsa = ctx->csa.lscsa;
558 return simple_read_from_buffer(buffer, size, pos,
559 &lscsa->fpcr, sizeof(lscsa->fpcr));
560 }
561
562 static ssize_t
563 spufs_fpcr_read(struct file *file, char __user * buffer,
564 size_t size, loff_t * pos)
565 {
566 int ret;
567 struct spu_context *ctx = file->private_data;
568
569 ret = spu_acquire_saved(ctx);
570 if (ret)
571 return ret;
572 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
573 spu_release_saved(ctx);
574 return ret;
575 }
576
577 static ssize_t
578 spufs_fpcr_write(struct file *file, const char __user * buffer,
579 size_t size, loff_t * pos)
580 {
581 struct spu_context *ctx = file->private_data;
582 struct spu_lscsa *lscsa = ctx->csa.lscsa;
583 int ret;
584
585 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
586 if (size <= 0)
587 return -EFBIG;
588
589 ret = spu_acquire_saved(ctx);
590 if (ret)
591 return ret;
592
593 *pos += size;
594 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
595 buffer, size) ? -EFAULT : size;
596
597 spu_release_saved(ctx);
598 return ret;
599 }
600
601 static const struct file_operations spufs_fpcr_fops = {
602 .open = spufs_regs_open,
603 .read = spufs_fpcr_read,
604 .write = spufs_fpcr_write,
605 .llseek = generic_file_llseek,
606 };
607
608 /* generic open function for all pipe-like files */
609 static int spufs_pipe_open(struct inode *inode, struct file *file)
610 {
611 struct spufs_inode_info *i = SPUFS_I(inode);
612 file->private_data = i->i_ctx;
613
614 return nonseekable_open(inode, file);
615 }
616
617 /*
618 * Read as many bytes from the mailbox as possible, until
619 * one of the conditions becomes true:
620 *
621 * - no more data available in the mailbox
622 * - end of the user provided buffer
623 * - end of the mapped area
624 */
625 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
626 size_t len, loff_t *pos)
627 {
628 struct spu_context *ctx = file->private_data;
629 u32 mbox_data, __user *udata;
630 ssize_t count;
631
632 if (len < 4)
633 return -EINVAL;
634
635 if (!access_ok(VERIFY_WRITE, buf, len))
636 return -EFAULT;
637
638 udata = (void __user *)buf;
639
640 count = spu_acquire(ctx);
641 if (count)
642 return count;
643
644 for (count = 0; (count + 4) <= len; count += 4, udata++) {
645 int ret;
646 ret = ctx->ops->mbox_read(ctx, &mbox_data);
647 if (ret == 0)
648 break;
649
650 /*
651 * at the end of the mapped area, we can fault
652 * but still need to return the data we have
653 * read successfully so far.
654 */
655 ret = __put_user(mbox_data, udata);
656 if (ret) {
657 if (!count)
658 count = -EFAULT;
659 break;
660 }
661 }
662 spu_release(ctx);
663
664 if (!count)
665 count = -EAGAIN;
666
667 return count;
668 }
669
670 static const struct file_operations spufs_mbox_fops = {
671 .open = spufs_pipe_open,
672 .read = spufs_mbox_read,
673 };
674
675 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
676 size_t len, loff_t *pos)
677 {
678 struct spu_context *ctx = file->private_data;
679 ssize_t ret;
680 u32 mbox_stat;
681
682 if (len < 4)
683 return -EINVAL;
684
685 ret = spu_acquire(ctx);
686 if (ret)
687 return ret;
688
689 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
690
691 spu_release(ctx);
692
693 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
694 return -EFAULT;
695
696 return 4;
697 }
698
699 static const struct file_operations spufs_mbox_stat_fops = {
700 .open = spufs_pipe_open,
701 .read = spufs_mbox_stat_read,
702 };
703
704 /* low-level ibox access function */
705 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
706 {
707 return ctx->ops->ibox_read(ctx, data);
708 }
709
710 static int spufs_ibox_fasync(int fd, struct file *file, int on)
711 {
712 struct spu_context *ctx = file->private_data;
713
714 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
715 }
716
717 /* interrupt-level ibox callback function. */
718 void spufs_ibox_callback(struct spu *spu)
719 {
720 struct spu_context *ctx = spu->ctx;
721
722 if (!ctx)
723 return;
724
725 wake_up_all(&ctx->ibox_wq);
726 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
727 }
728
729 /*
730 * Read as many bytes from the interrupt mailbox as possible, until
731 * one of the conditions becomes true:
732 *
733 * - no more data available in the mailbox
734 * - end of the user provided buffer
735 * - end of the mapped area
736 *
737 * If the file is opened without O_NONBLOCK, we wait here until
738 * any data is available, but return when we have been able to
739 * read something.
740 */
741 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
742 size_t len, loff_t *pos)
743 {
744 struct spu_context *ctx = file->private_data;
745 u32 ibox_data, __user *udata;
746 ssize_t count;
747
748 if (len < 4)
749 return -EINVAL;
750
751 if (!access_ok(VERIFY_WRITE, buf, len))
752 return -EFAULT;
753
754 udata = (void __user *)buf;
755
756 count = spu_acquire(ctx);
757 if (count)
758 return count;
759
760 /* wait only for the first element */
761 count = 0;
762 if (file->f_flags & O_NONBLOCK) {
763 if (!spu_ibox_read(ctx, &ibox_data))
764 count = -EAGAIN;
765 } else {
766 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
767 }
768 if (count)
769 goto out;
770
771 /* if we can't write at all, return -EFAULT */
772 count = __put_user(ibox_data, udata);
773 if (count)
774 goto out;
775
776 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
777 int ret;
778 ret = ctx->ops->ibox_read(ctx, &ibox_data);
779 if (ret == 0)
780 break;
781 /*
782 * at the end of the mapped area, we can fault
783 * but still need to return the data we have
784 * read successfully so far.
785 */
786 ret = __put_user(ibox_data, udata);
787 if (ret)
788 break;
789 }
790
791 out:
792 spu_release(ctx);
793
794 return count;
795 }
796
797 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
798 {
799 struct spu_context *ctx = file->private_data;
800 unsigned int mask;
801
802 poll_wait(file, &ctx->ibox_wq, wait);
803
804 /*
805 * For now keep this uninterruptible and also ignore the rule
806 * that poll should not sleep. Will be fixed later.
807 */
808 mutex_lock(&ctx->state_mutex);
809 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
810 spu_release(ctx);
811
812 return mask;
813 }
814
815 static const struct file_operations spufs_ibox_fops = {
816 .open = spufs_pipe_open,
817 .read = spufs_ibox_read,
818 .poll = spufs_ibox_poll,
819 .fasync = spufs_ibox_fasync,
820 };
821
822 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
823 size_t len, loff_t *pos)
824 {
825 struct spu_context *ctx = file->private_data;
826 ssize_t ret;
827 u32 ibox_stat;
828
829 if (len < 4)
830 return -EINVAL;
831
832 ret = spu_acquire(ctx);
833 if (ret)
834 return ret;
835 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
836 spu_release(ctx);
837
838 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
839 return -EFAULT;
840
841 return 4;
842 }
843
844 static const struct file_operations spufs_ibox_stat_fops = {
845 .open = spufs_pipe_open,
846 .read = spufs_ibox_stat_read,
847 };
848
849 /* low-level mailbox write */
850 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
851 {
852 return ctx->ops->wbox_write(ctx, data);
853 }
854
855 static int spufs_wbox_fasync(int fd, struct file *file, int on)
856 {
857 struct spu_context *ctx = file->private_data;
858 int ret;
859
860 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
861
862 return ret;
863 }
864
865 /* interrupt-level wbox callback function. */
866 void spufs_wbox_callback(struct spu *spu)
867 {
868 struct spu_context *ctx = spu->ctx;
869
870 if (!ctx)
871 return;
872
873 wake_up_all(&ctx->wbox_wq);
874 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
875 }
876
877 /*
878 * Write as many bytes to the interrupt mailbox as possible, until
879 * one of the conditions becomes true:
880 *
881 * - the mailbox is full
882 * - end of the user provided buffer
883 * - end of the mapped area
884 *
885 * If the file is opened without O_NONBLOCK, we wait here until
886 * space is availabyl, but return when we have been able to
887 * write something.
888 */
889 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
890 size_t len, loff_t *pos)
891 {
892 struct spu_context *ctx = file->private_data;
893 u32 wbox_data, __user *udata;
894 ssize_t count;
895
896 if (len < 4)
897 return -EINVAL;
898
899 udata = (void __user *)buf;
900 if (!access_ok(VERIFY_READ, buf, len))
901 return -EFAULT;
902
903 if (__get_user(wbox_data, udata))
904 return -EFAULT;
905
906 count = spu_acquire(ctx);
907 if (count)
908 return count;
909
910 /*
911 * make sure we can at least write one element, by waiting
912 * in case of !O_NONBLOCK
913 */
914 count = 0;
915 if (file->f_flags & O_NONBLOCK) {
916 if (!spu_wbox_write(ctx, wbox_data))
917 count = -EAGAIN;
918 } else {
919 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
920 }
921
922 if (count)
923 goto out;
924
925 /* write as much as possible */
926 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
927 int ret;
928 ret = __get_user(wbox_data, udata);
929 if (ret)
930 break;
931
932 ret = spu_wbox_write(ctx, wbox_data);
933 if (ret == 0)
934 break;
935 }
936
937 out:
938 spu_release(ctx);
939 return count;
940 }
941
942 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
943 {
944 struct spu_context *ctx = file->private_data;
945 unsigned int mask;
946
947 poll_wait(file, &ctx->wbox_wq, wait);
948
949 /*
950 * For now keep this uninterruptible and also ignore the rule
951 * that poll should not sleep. Will be fixed later.
952 */
953 mutex_lock(&ctx->state_mutex);
954 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
955 spu_release(ctx);
956
957 return mask;
958 }
959
960 static const struct file_operations spufs_wbox_fops = {
961 .open = spufs_pipe_open,
962 .write = spufs_wbox_write,
963 .poll = spufs_wbox_poll,
964 .fasync = spufs_wbox_fasync,
965 };
966
967 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
968 size_t len, loff_t *pos)
969 {
970 struct spu_context *ctx = file->private_data;
971 ssize_t ret;
972 u32 wbox_stat;
973
974 if (len < 4)
975 return -EINVAL;
976
977 ret = spu_acquire(ctx);
978 if (ret)
979 return ret;
980 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
981 spu_release(ctx);
982
983 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
984 return -EFAULT;
985
986 return 4;
987 }
988
989 static const struct file_operations spufs_wbox_stat_fops = {
990 .open = spufs_pipe_open,
991 .read = spufs_wbox_stat_read,
992 };
993
994 static int spufs_signal1_open(struct inode *inode, struct file *file)
995 {
996 struct spufs_inode_info *i = SPUFS_I(inode);
997 struct spu_context *ctx = i->i_ctx;
998
999 mutex_lock(&ctx->mapping_lock);
1000 file->private_data = ctx;
1001 if (!i->i_openers++)
1002 ctx->signal1 = inode->i_mapping;
1003 mutex_unlock(&ctx->mapping_lock);
1004 return nonseekable_open(inode, file);
1005 }
1006
1007 static int
1008 spufs_signal1_release(struct inode *inode, struct file *file)
1009 {
1010 struct spufs_inode_info *i = SPUFS_I(inode);
1011 struct spu_context *ctx = i->i_ctx;
1012
1013 mutex_lock(&ctx->mapping_lock);
1014 if (!--i->i_openers)
1015 ctx->signal1 = NULL;
1016 mutex_unlock(&ctx->mapping_lock);
1017 return 0;
1018 }
1019
1020 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
1021 size_t len, loff_t *pos)
1022 {
1023 int ret = 0;
1024 u32 data;
1025
1026 if (len < 4)
1027 return -EINVAL;
1028
1029 if (ctx->csa.spu_chnlcnt_RW[3]) {
1030 data = ctx->csa.spu_chnldata_RW[3];
1031 ret = 4;
1032 }
1033
1034 if (!ret)
1035 goto out;
1036
1037 if (copy_to_user(buf, &data, 4))
1038 return -EFAULT;
1039
1040 out:
1041 return ret;
1042 }
1043
1044 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1045 size_t len, loff_t *pos)
1046 {
1047 int ret;
1048 struct spu_context *ctx = file->private_data;
1049
1050 ret = spu_acquire_saved(ctx);
1051 if (ret)
1052 return ret;
1053 ret = __spufs_signal1_read(ctx, buf, len, pos);
1054 spu_release_saved(ctx);
1055
1056 return ret;
1057 }
1058
1059 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1060 size_t len, loff_t *pos)
1061 {
1062 struct spu_context *ctx;
1063 ssize_t ret;
1064 u32 data;
1065
1066 ctx = file->private_data;
1067
1068 if (len < 4)
1069 return -EINVAL;
1070
1071 if (copy_from_user(&data, buf, 4))
1072 return -EFAULT;
1073
1074 ret = spu_acquire(ctx);
1075 if (ret)
1076 return ret;
1077 ctx->ops->signal1_write(ctx, data);
1078 spu_release(ctx);
1079
1080 return 4;
1081 }
1082
1083 static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1084 unsigned long address)
1085 {
1086 #if PAGE_SIZE == 0x1000
1087 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
1088 #elif PAGE_SIZE == 0x10000
1089 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1090 * signal 1 and 2 area
1091 */
1092 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1093 #else
1094 #error unsupported page size
1095 #endif
1096 }
1097
1098 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
1099 .nopfn = spufs_signal1_mmap_nopfn,
1100 };
1101
1102 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1103 {
1104 if (!(vma->vm_flags & VM_SHARED))
1105 return -EINVAL;
1106
1107 vma->vm_flags |= VM_IO | VM_PFNMAP;
1108 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1109 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1110
1111 vma->vm_ops = &spufs_signal1_mmap_vmops;
1112 return 0;
1113 }
1114
1115 static const struct file_operations spufs_signal1_fops = {
1116 .open = spufs_signal1_open,
1117 .release = spufs_signal1_release,
1118 .read = spufs_signal1_read,
1119 .write = spufs_signal1_write,
1120 .mmap = spufs_signal1_mmap,
1121 };
1122
1123 static const struct file_operations spufs_signal1_nosched_fops = {
1124 .open = spufs_signal1_open,
1125 .release = spufs_signal1_release,
1126 .write = spufs_signal1_write,
1127 .mmap = spufs_signal1_mmap,
1128 };
1129
1130 static int spufs_signal2_open(struct inode *inode, struct file *file)
1131 {
1132 struct spufs_inode_info *i = SPUFS_I(inode);
1133 struct spu_context *ctx = i->i_ctx;
1134
1135 mutex_lock(&ctx->mapping_lock);
1136 file->private_data = ctx;
1137 if (!i->i_openers++)
1138 ctx->signal2 = inode->i_mapping;
1139 mutex_unlock(&ctx->mapping_lock);
1140 return nonseekable_open(inode, file);
1141 }
1142
1143 static int
1144 spufs_signal2_release(struct inode *inode, struct file *file)
1145 {
1146 struct spufs_inode_info *i = SPUFS_I(inode);
1147 struct spu_context *ctx = i->i_ctx;
1148
1149 mutex_lock(&ctx->mapping_lock);
1150 if (!--i->i_openers)
1151 ctx->signal2 = NULL;
1152 mutex_unlock(&ctx->mapping_lock);
1153 return 0;
1154 }
1155
1156 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
1157 size_t len, loff_t *pos)
1158 {
1159 int ret = 0;
1160 u32 data;
1161
1162 if (len < 4)
1163 return -EINVAL;
1164
1165 if (ctx->csa.spu_chnlcnt_RW[4]) {
1166 data = ctx->csa.spu_chnldata_RW[4];
1167 ret = 4;
1168 }
1169
1170 if (!ret)
1171 goto out;
1172
1173 if (copy_to_user(buf, &data, 4))
1174 return -EFAULT;
1175
1176 out:
1177 return ret;
1178 }
1179
1180 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1181 size_t len, loff_t *pos)
1182 {
1183 struct spu_context *ctx = file->private_data;
1184 int ret;
1185
1186 ret = spu_acquire_saved(ctx);
1187 if (ret)
1188 return ret;
1189 ret = __spufs_signal2_read(ctx, buf, len, pos);
1190 spu_release_saved(ctx);
1191
1192 return ret;
1193 }
1194
1195 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1196 size_t len, loff_t *pos)
1197 {
1198 struct spu_context *ctx;
1199 ssize_t ret;
1200 u32 data;
1201
1202 ctx = file->private_data;
1203
1204 if (len < 4)
1205 return -EINVAL;
1206
1207 if (copy_from_user(&data, buf, 4))
1208 return -EFAULT;
1209
1210 ret = spu_acquire(ctx);
1211 if (ret)
1212 return ret;
1213 ctx->ops->signal2_write(ctx, data);
1214 spu_release(ctx);
1215
1216 return 4;
1217 }
1218
1219 #if SPUFS_MMAP_4K
1220 static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1221 unsigned long address)
1222 {
1223 #if PAGE_SIZE == 0x1000
1224 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
1225 #elif PAGE_SIZE == 0x10000
1226 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1227 * signal 1 and 2 area
1228 */
1229 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1230 #else
1231 #error unsupported page size
1232 #endif
1233 }
1234
1235 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1236 .nopfn = spufs_signal2_mmap_nopfn,
1237 };
1238
1239 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1240 {
1241 if (!(vma->vm_flags & VM_SHARED))
1242 return -EINVAL;
1243
1244 vma->vm_flags |= VM_IO | VM_PFNMAP;
1245 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1246 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1247
1248 vma->vm_ops = &spufs_signal2_mmap_vmops;
1249 return 0;
1250 }
1251 #else /* SPUFS_MMAP_4K */
1252 #define spufs_signal2_mmap NULL
1253 #endif /* !SPUFS_MMAP_4K */
1254
1255 static const struct file_operations spufs_signal2_fops = {
1256 .open = spufs_signal2_open,
1257 .release = spufs_signal2_release,
1258 .read = spufs_signal2_read,
1259 .write = spufs_signal2_write,
1260 .mmap = spufs_signal2_mmap,
1261 };
1262
1263 static const struct file_operations spufs_signal2_nosched_fops = {
1264 .open = spufs_signal2_open,
1265 .release = spufs_signal2_release,
1266 .write = spufs_signal2_write,
1267 .mmap = spufs_signal2_mmap,
1268 };
1269
1270 /*
1271 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1272 * work of acquiring (or not) the SPU context before calling through
1273 * to the actual get routine. The set routine is called directly.
1274 */
1275 #define SPU_ATTR_NOACQUIRE 0
1276 #define SPU_ATTR_ACQUIRE 1
1277 #define SPU_ATTR_ACQUIRE_SAVED 2
1278
1279 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
1280 static int __##__get(void *data, u64 *val) \
1281 { \
1282 struct spu_context *ctx = data; \
1283 int ret = 0; \
1284 \
1285 if (__acquire == SPU_ATTR_ACQUIRE) { \
1286 ret = spu_acquire(ctx); \
1287 if (ret) \
1288 return ret; \
1289 *val = __get(ctx); \
1290 spu_release(ctx); \
1291 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1292 ret = spu_acquire_saved(ctx); \
1293 if (ret) \
1294 return ret; \
1295 *val = __get(ctx); \
1296 spu_release_saved(ctx); \
1297 } else \
1298 *val = __get(ctx); \
1299 \
1300 return 0; \
1301 } \
1302 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1303
1304 static int spufs_signal1_type_set(void *data, u64 val)
1305 {
1306 struct spu_context *ctx = data;
1307 int ret;
1308
1309 ret = spu_acquire(ctx);
1310 if (ret)
1311 return ret;
1312 ctx->ops->signal1_type_set(ctx, val);
1313 spu_release(ctx);
1314
1315 return 0;
1316 }
1317
1318 static u64 spufs_signal1_type_get(struct spu_context *ctx)
1319 {
1320 return ctx->ops->signal1_type_get(ctx);
1321 }
1322 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1323 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
1324
1325
1326 static int spufs_signal2_type_set(void *data, u64 val)
1327 {
1328 struct spu_context *ctx = data;
1329 int ret;
1330
1331 ret = spu_acquire(ctx);
1332 if (ret)
1333 return ret;
1334 ctx->ops->signal2_type_set(ctx, val);
1335 spu_release(ctx);
1336
1337 return 0;
1338 }
1339
1340 static u64 spufs_signal2_type_get(struct spu_context *ctx)
1341 {
1342 return ctx->ops->signal2_type_get(ctx);
1343 }
1344 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1345 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
1346
1347 #if SPUFS_MMAP_4K
1348 static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1349 unsigned long address)
1350 {
1351 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
1352 }
1353
1354 static struct vm_operations_struct spufs_mss_mmap_vmops = {
1355 .nopfn = spufs_mss_mmap_nopfn,
1356 };
1357
1358 /*
1359 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1360 */
1361 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1362 {
1363 if (!(vma->vm_flags & VM_SHARED))
1364 return -EINVAL;
1365
1366 vma->vm_flags |= VM_IO | VM_PFNMAP;
1367 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1368 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1369
1370 vma->vm_ops = &spufs_mss_mmap_vmops;
1371 return 0;
1372 }
1373 #else /* SPUFS_MMAP_4K */
1374 #define spufs_mss_mmap NULL
1375 #endif /* !SPUFS_MMAP_4K */
1376
1377 static int spufs_mss_open(struct inode *inode, struct file *file)
1378 {
1379 struct spufs_inode_info *i = SPUFS_I(inode);
1380 struct spu_context *ctx = i->i_ctx;
1381
1382 file->private_data = i->i_ctx;
1383
1384 mutex_lock(&ctx->mapping_lock);
1385 if (!i->i_openers++)
1386 ctx->mss = inode->i_mapping;
1387 mutex_unlock(&ctx->mapping_lock);
1388 return nonseekable_open(inode, file);
1389 }
1390
1391 static int
1392 spufs_mss_release(struct inode *inode, struct file *file)
1393 {
1394 struct spufs_inode_info *i = SPUFS_I(inode);
1395 struct spu_context *ctx = i->i_ctx;
1396
1397 mutex_lock(&ctx->mapping_lock);
1398 if (!--i->i_openers)
1399 ctx->mss = NULL;
1400 mutex_unlock(&ctx->mapping_lock);
1401 return 0;
1402 }
1403
1404 static const struct file_operations spufs_mss_fops = {
1405 .open = spufs_mss_open,
1406 .release = spufs_mss_release,
1407 .mmap = spufs_mss_mmap,
1408 };
1409
1410 static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1411 unsigned long address)
1412 {
1413 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
1414 }
1415
1416 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1417 .nopfn = spufs_psmap_mmap_nopfn,
1418 };
1419
1420 /*
1421 * mmap support for full problem state area [0x00000 - 0x1ffff].
1422 */
1423 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1424 {
1425 if (!(vma->vm_flags & VM_SHARED))
1426 return -EINVAL;
1427
1428 vma->vm_flags |= VM_IO | VM_PFNMAP;
1429 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1430 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1431
1432 vma->vm_ops = &spufs_psmap_mmap_vmops;
1433 return 0;
1434 }
1435
1436 static int spufs_psmap_open(struct inode *inode, struct file *file)
1437 {
1438 struct spufs_inode_info *i = SPUFS_I(inode);
1439 struct spu_context *ctx = i->i_ctx;
1440
1441 mutex_lock(&ctx->mapping_lock);
1442 file->private_data = i->i_ctx;
1443 if (!i->i_openers++)
1444 ctx->psmap = inode->i_mapping;
1445 mutex_unlock(&ctx->mapping_lock);
1446 return nonseekable_open(inode, file);
1447 }
1448
1449 static int
1450 spufs_psmap_release(struct inode *inode, struct file *file)
1451 {
1452 struct spufs_inode_info *i = SPUFS_I(inode);
1453 struct spu_context *ctx = i->i_ctx;
1454
1455 mutex_lock(&ctx->mapping_lock);
1456 if (!--i->i_openers)
1457 ctx->psmap = NULL;
1458 mutex_unlock(&ctx->mapping_lock);
1459 return 0;
1460 }
1461
1462 static const struct file_operations spufs_psmap_fops = {
1463 .open = spufs_psmap_open,
1464 .release = spufs_psmap_release,
1465 .mmap = spufs_psmap_mmap,
1466 };
1467
1468
1469 #if SPUFS_MMAP_4K
1470 static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1471 unsigned long address)
1472 {
1473 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
1474 }
1475
1476 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1477 .nopfn = spufs_mfc_mmap_nopfn,
1478 };
1479
1480 /*
1481 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1482 */
1483 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1484 {
1485 if (!(vma->vm_flags & VM_SHARED))
1486 return -EINVAL;
1487
1488 vma->vm_flags |= VM_IO | VM_PFNMAP;
1489 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1490 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1491
1492 vma->vm_ops = &spufs_mfc_mmap_vmops;
1493 return 0;
1494 }
1495 #else /* SPUFS_MMAP_4K */
1496 #define spufs_mfc_mmap NULL
1497 #endif /* !SPUFS_MMAP_4K */
1498
1499 static int spufs_mfc_open(struct inode *inode, struct file *file)
1500 {
1501 struct spufs_inode_info *i = SPUFS_I(inode);
1502 struct spu_context *ctx = i->i_ctx;
1503
1504 /* we don't want to deal with DMA into other processes */
1505 if (ctx->owner != current->mm)
1506 return -EINVAL;
1507
1508 if (atomic_read(&inode->i_count) != 1)
1509 return -EBUSY;
1510
1511 mutex_lock(&ctx->mapping_lock);
1512 file->private_data = ctx;
1513 if (!i->i_openers++)
1514 ctx->mfc = inode->i_mapping;
1515 mutex_unlock(&ctx->mapping_lock);
1516 return nonseekable_open(inode, file);
1517 }
1518
1519 static int
1520 spufs_mfc_release(struct inode *inode, struct file *file)
1521 {
1522 struct spufs_inode_info *i = SPUFS_I(inode);
1523 struct spu_context *ctx = i->i_ctx;
1524
1525 mutex_lock(&ctx->mapping_lock);
1526 if (!--i->i_openers)
1527 ctx->mfc = NULL;
1528 mutex_unlock(&ctx->mapping_lock);
1529 return 0;
1530 }
1531
1532 /* interrupt-level mfc callback function. */
1533 void spufs_mfc_callback(struct spu *spu)
1534 {
1535 struct spu_context *ctx = spu->ctx;
1536
1537 if (!ctx)
1538 return;
1539
1540 wake_up_all(&ctx->mfc_wq);
1541
1542 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1543 if (ctx->mfc_fasync) {
1544 u32 free_elements, tagstatus;
1545 unsigned int mask;
1546
1547 /* no need for spu_acquire in interrupt context */
1548 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1549 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1550
1551 mask = 0;
1552 if (free_elements & 0xffff)
1553 mask |= POLLOUT;
1554 if (tagstatus & ctx->tagwait)
1555 mask |= POLLIN;
1556
1557 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1558 }
1559 }
1560
1561 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1562 {
1563 /* See if there is one tag group is complete */
1564 /* FIXME we need locking around tagwait */
1565 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1566 ctx->tagwait &= ~*status;
1567 if (*status)
1568 return 1;
1569
1570 /* enable interrupt waiting for any tag group,
1571 may silently fail if interrupts are already enabled */
1572 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1573 return 0;
1574 }
1575
1576 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1577 size_t size, loff_t *pos)
1578 {
1579 struct spu_context *ctx = file->private_data;
1580 int ret = -EINVAL;
1581 u32 status;
1582
1583 if (size != 4)
1584 goto out;
1585
1586 ret = spu_acquire(ctx);
1587 if (ret)
1588 return ret;
1589
1590 ret = -EINVAL;
1591 if (file->f_flags & O_NONBLOCK) {
1592 status = ctx->ops->read_mfc_tagstatus(ctx);
1593 if (!(status & ctx->tagwait))
1594 ret = -EAGAIN;
1595 else
1596 /* XXX(hch): shouldn't we clear ret here? */
1597 ctx->tagwait &= ~status;
1598 } else {
1599 ret = spufs_wait(ctx->mfc_wq,
1600 spufs_read_mfc_tagstatus(ctx, &status));
1601 }
1602 spu_release(ctx);
1603
1604 if (ret)
1605 goto out;
1606
1607 ret = 4;
1608 if (copy_to_user(buffer, &status, 4))
1609 ret = -EFAULT;
1610
1611 out:
1612 return ret;
1613 }
1614
1615 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1616 {
1617 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1618 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1619
1620 switch (cmd->cmd) {
1621 case MFC_PUT_CMD:
1622 case MFC_PUTF_CMD:
1623 case MFC_PUTB_CMD:
1624 case MFC_GET_CMD:
1625 case MFC_GETF_CMD:
1626 case MFC_GETB_CMD:
1627 break;
1628 default:
1629 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1630 return -EIO;
1631 }
1632
1633 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1634 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1635 cmd->ea, cmd->lsa);
1636 return -EIO;
1637 }
1638
1639 switch (cmd->size & 0xf) {
1640 case 1:
1641 break;
1642 case 2:
1643 if (cmd->lsa & 1)
1644 goto error;
1645 break;
1646 case 4:
1647 if (cmd->lsa & 3)
1648 goto error;
1649 break;
1650 case 8:
1651 if (cmd->lsa & 7)
1652 goto error;
1653 break;
1654 case 0:
1655 if (cmd->lsa & 15)
1656 goto error;
1657 break;
1658 error:
1659 default:
1660 pr_debug("invalid DMA alignment %x for size %x\n",
1661 cmd->lsa & 0xf, cmd->size);
1662 return -EIO;
1663 }
1664
1665 if (cmd->size > 16 * 1024) {
1666 pr_debug("invalid DMA size %x\n", cmd->size);
1667 return -EIO;
1668 }
1669
1670 if (cmd->tag & 0xfff0) {
1671 /* we reserve the higher tag numbers for kernel use */
1672 pr_debug("invalid DMA tag\n");
1673 return -EIO;
1674 }
1675
1676 if (cmd->class) {
1677 /* not supported in this version */
1678 pr_debug("invalid DMA class\n");
1679 return -EIO;
1680 }
1681
1682 return 0;
1683 }
1684
1685 static int spu_send_mfc_command(struct spu_context *ctx,
1686 struct mfc_dma_command cmd,
1687 int *error)
1688 {
1689 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1690 if (*error == -EAGAIN) {
1691 /* wait for any tag group to complete
1692 so we have space for the new command */
1693 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1694 /* try again, because the queue might be
1695 empty again */
1696 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1697 if (*error == -EAGAIN)
1698 return 0;
1699 }
1700 return 1;
1701 }
1702
1703 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1704 size_t size, loff_t *pos)
1705 {
1706 struct spu_context *ctx = file->private_data;
1707 struct mfc_dma_command cmd;
1708 int ret = -EINVAL;
1709
1710 if (size != sizeof cmd)
1711 goto out;
1712
1713 ret = -EFAULT;
1714 if (copy_from_user(&cmd, buffer, sizeof cmd))
1715 goto out;
1716
1717 ret = spufs_check_valid_dma(&cmd);
1718 if (ret)
1719 goto out;
1720
1721 ret = spu_acquire(ctx);
1722 if (ret)
1723 goto out;
1724
1725 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1726 if (ret)
1727 goto out;
1728
1729 if (file->f_flags & O_NONBLOCK) {
1730 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1731 } else {
1732 int status;
1733 ret = spufs_wait(ctx->mfc_wq,
1734 spu_send_mfc_command(ctx, cmd, &status));
1735 if (status)
1736 ret = status;
1737 }
1738
1739 if (ret)
1740 goto out_unlock;
1741
1742 ctx->tagwait |= 1 << cmd.tag;
1743 ret = size;
1744
1745 out_unlock:
1746 spu_release(ctx);
1747 out:
1748 return ret;
1749 }
1750
1751 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1752 {
1753 struct spu_context *ctx = file->private_data;
1754 u32 free_elements, tagstatus;
1755 unsigned int mask;
1756
1757 poll_wait(file, &ctx->mfc_wq, wait);
1758
1759 /*
1760 * For now keep this uninterruptible and also ignore the rule
1761 * that poll should not sleep. Will be fixed later.
1762 */
1763 mutex_lock(&ctx->state_mutex);
1764 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1765 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1766 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1767 spu_release(ctx);
1768
1769 mask = 0;
1770 if (free_elements & 0xffff)
1771 mask |= POLLOUT | POLLWRNORM;
1772 if (tagstatus & ctx->tagwait)
1773 mask |= POLLIN | POLLRDNORM;
1774
1775 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1776 free_elements, tagstatus, ctx->tagwait);
1777
1778 return mask;
1779 }
1780
1781 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1782 {
1783 struct spu_context *ctx = file->private_data;
1784 int ret;
1785
1786 ret = spu_acquire(ctx);
1787 if (ret)
1788 return ret;
1789 #if 0
1790 /* this currently hangs */
1791 ret = spufs_wait(ctx->mfc_wq,
1792 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1793 if (ret)
1794 goto out;
1795 ret = spufs_wait(ctx->mfc_wq,
1796 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1797 out:
1798 #else
1799 ret = 0;
1800 #endif
1801 spu_release(ctx);
1802
1803 return ret;
1804 }
1805
1806 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1807 int datasync)
1808 {
1809 return spufs_mfc_flush(file, NULL);
1810 }
1811
1812 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1813 {
1814 struct spu_context *ctx = file->private_data;
1815
1816 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1817 }
1818
1819 static const struct file_operations spufs_mfc_fops = {
1820 .open = spufs_mfc_open,
1821 .release = spufs_mfc_release,
1822 .read = spufs_mfc_read,
1823 .write = spufs_mfc_write,
1824 .poll = spufs_mfc_poll,
1825 .flush = spufs_mfc_flush,
1826 .fsync = spufs_mfc_fsync,
1827 .fasync = spufs_mfc_fasync,
1828 .mmap = spufs_mfc_mmap,
1829 };
1830
1831 static int spufs_npc_set(void *data, u64 val)
1832 {
1833 struct spu_context *ctx = data;
1834 int ret;
1835
1836 ret = spu_acquire(ctx);
1837 if (ret)
1838 return ret;
1839 ctx->ops->npc_write(ctx, val);
1840 spu_release(ctx);
1841
1842 return 0;
1843 }
1844
1845 static u64 spufs_npc_get(struct spu_context *ctx)
1846 {
1847 return ctx->ops->npc_read(ctx);
1848 }
1849 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1850 "0x%llx\n", SPU_ATTR_ACQUIRE);
1851
1852 static int spufs_decr_set(void *data, u64 val)
1853 {
1854 struct spu_context *ctx = data;
1855 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1856 int ret;
1857
1858 ret = spu_acquire_saved(ctx);
1859 if (ret)
1860 return ret;
1861 lscsa->decr.slot[0] = (u32) val;
1862 spu_release_saved(ctx);
1863
1864 return 0;
1865 }
1866
1867 static u64 spufs_decr_get(struct spu_context *ctx)
1868 {
1869 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1870 return lscsa->decr.slot[0];
1871 }
1872 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1873 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
1874
1875 static int spufs_decr_status_set(void *data, u64 val)
1876 {
1877 struct spu_context *ctx = data;
1878 int ret;
1879
1880 ret = spu_acquire_saved(ctx);
1881 if (ret)
1882 return ret;
1883 if (val)
1884 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1885 else
1886 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1887 spu_release_saved(ctx);
1888
1889 return 0;
1890 }
1891
1892 static u64 spufs_decr_status_get(struct spu_context *ctx)
1893 {
1894 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1895 return SPU_DECR_STATUS_RUNNING;
1896 else
1897 return 0;
1898 }
1899 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1900 spufs_decr_status_set, "0x%llx\n",
1901 SPU_ATTR_ACQUIRE_SAVED);
1902
1903 static int spufs_event_mask_set(void *data, u64 val)
1904 {
1905 struct spu_context *ctx = data;
1906 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1907 int ret;
1908
1909 ret = spu_acquire_saved(ctx);
1910 if (ret)
1911 return ret;
1912 lscsa->event_mask.slot[0] = (u32) val;
1913 spu_release_saved(ctx);
1914
1915 return 0;
1916 }
1917
1918 static u64 spufs_event_mask_get(struct spu_context *ctx)
1919 {
1920 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1921 return lscsa->event_mask.slot[0];
1922 }
1923
1924 DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1925 spufs_event_mask_set, "0x%llx\n",
1926 SPU_ATTR_ACQUIRE_SAVED);
1927
1928 static u64 spufs_event_status_get(struct spu_context *ctx)
1929 {
1930 struct spu_state *state = &ctx->csa;
1931 u64 stat;
1932 stat = state->spu_chnlcnt_RW[0];
1933 if (stat)
1934 return state->spu_chnldata_RW[0];
1935 return 0;
1936 }
1937 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1938 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1939
1940 static int spufs_srr0_set(void *data, u64 val)
1941 {
1942 struct spu_context *ctx = data;
1943 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1944 int ret;
1945
1946 ret = spu_acquire_saved(ctx);
1947 if (ret)
1948 return ret;
1949 lscsa->srr0.slot[0] = (u32) val;
1950 spu_release_saved(ctx);
1951
1952 return 0;
1953 }
1954
1955 static u64 spufs_srr0_get(struct spu_context *ctx)
1956 {
1957 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1958 return lscsa->srr0.slot[0];
1959 }
1960 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1961 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1962
1963 static u64 spufs_id_get(struct spu_context *ctx)
1964 {
1965 u64 num;
1966
1967 if (ctx->state == SPU_STATE_RUNNABLE)
1968 num = ctx->spu->number;
1969 else
1970 num = (unsigned int)-1;
1971
1972 return num;
1973 }
1974 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1975 SPU_ATTR_ACQUIRE)
1976
1977 static u64 spufs_object_id_get(struct spu_context *ctx)
1978 {
1979 /* FIXME: Should there really be no locking here? */
1980 return ctx->object_id;
1981 }
1982
1983 static int spufs_object_id_set(void *data, u64 id)
1984 {
1985 struct spu_context *ctx = data;
1986 ctx->object_id = id;
1987
1988 return 0;
1989 }
1990
1991 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1992 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
1993
1994 static u64 spufs_lslr_get(struct spu_context *ctx)
1995 {
1996 return ctx->csa.priv2.spu_lslr_RW;
1997 }
1998 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1999 SPU_ATTR_ACQUIRE_SAVED);
2000
2001 static int spufs_info_open(struct inode *inode, struct file *file)
2002 {
2003 struct spufs_inode_info *i = SPUFS_I(inode);
2004 struct spu_context *ctx = i->i_ctx;
2005 file->private_data = ctx;
2006 return 0;
2007 }
2008
2009 static int spufs_caps_show(struct seq_file *s, void *private)
2010 {
2011 struct spu_context *ctx = s->private;
2012
2013 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2014 seq_puts(s, "sched\n");
2015 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2016 seq_puts(s, "step\n");
2017 return 0;
2018 }
2019
2020 static int spufs_caps_open(struct inode *inode, struct file *file)
2021 {
2022 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2023 }
2024
2025 static const struct file_operations spufs_caps_fops = {
2026 .open = spufs_caps_open,
2027 .read = seq_read,
2028 .llseek = seq_lseek,
2029 .release = single_release,
2030 };
2031
2032 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2033 char __user *buf, size_t len, loff_t *pos)
2034 {
2035 u32 data;
2036
2037 /* EOF if there's no entry in the mbox */
2038 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2039 return 0;
2040
2041 data = ctx->csa.prob.pu_mb_R;
2042
2043 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2044 }
2045
2046 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2047 size_t len, loff_t *pos)
2048 {
2049 int ret;
2050 struct spu_context *ctx = file->private_data;
2051
2052 if (!access_ok(VERIFY_WRITE, buf, len))
2053 return -EFAULT;
2054
2055 ret = spu_acquire_saved(ctx);
2056 if (ret)
2057 return ret;
2058 spin_lock(&ctx->csa.register_lock);
2059 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
2060 spin_unlock(&ctx->csa.register_lock);
2061 spu_release_saved(ctx);
2062
2063 return ret;
2064 }
2065
2066 static const struct file_operations spufs_mbox_info_fops = {
2067 .open = spufs_info_open,
2068 .read = spufs_mbox_info_read,
2069 .llseek = generic_file_llseek,
2070 };
2071
2072 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2073 char __user *buf, size_t len, loff_t *pos)
2074 {
2075 u32 data;
2076
2077 /* EOF if there's no entry in the ibox */
2078 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2079 return 0;
2080
2081 data = ctx->csa.priv2.puint_mb_R;
2082
2083 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2084 }
2085
2086 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2087 size_t len, loff_t *pos)
2088 {
2089 struct spu_context *ctx = file->private_data;
2090 int ret;
2091
2092 if (!access_ok(VERIFY_WRITE, buf, len))
2093 return -EFAULT;
2094
2095 ret = spu_acquire_saved(ctx);
2096 if (ret)
2097 return ret;
2098 spin_lock(&ctx->csa.register_lock);
2099 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
2100 spin_unlock(&ctx->csa.register_lock);
2101 spu_release_saved(ctx);
2102
2103 return ret;
2104 }
2105
2106 static const struct file_operations spufs_ibox_info_fops = {
2107 .open = spufs_info_open,
2108 .read = spufs_ibox_info_read,
2109 .llseek = generic_file_llseek,
2110 };
2111
2112 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2113 char __user *buf, size_t len, loff_t *pos)
2114 {
2115 int i, cnt;
2116 u32 data[4];
2117 u32 wbox_stat;
2118
2119 wbox_stat = ctx->csa.prob.mb_stat_R;
2120 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2121 for (i = 0; i < cnt; i++) {
2122 data[i] = ctx->csa.spu_mailbox_data[i];
2123 }
2124
2125 return simple_read_from_buffer(buf, len, pos, &data,
2126 cnt * sizeof(u32));
2127 }
2128
2129 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2130 size_t len, loff_t *pos)
2131 {
2132 struct spu_context *ctx = file->private_data;
2133 int ret;
2134
2135 if (!access_ok(VERIFY_WRITE, buf, len))
2136 return -EFAULT;
2137
2138 ret = spu_acquire_saved(ctx);
2139 if (ret)
2140 return ret;
2141 spin_lock(&ctx->csa.register_lock);
2142 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
2143 spin_unlock(&ctx->csa.register_lock);
2144 spu_release_saved(ctx);
2145
2146 return ret;
2147 }
2148
2149 static const struct file_operations spufs_wbox_info_fops = {
2150 .open = spufs_info_open,
2151 .read = spufs_wbox_info_read,
2152 .llseek = generic_file_llseek,
2153 };
2154
2155 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2156 char __user *buf, size_t len, loff_t *pos)
2157 {
2158 struct spu_dma_info info;
2159 struct mfc_cq_sr *qp, *spuqp;
2160 int i;
2161
2162 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2163 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2164 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2165 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2166 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2167 for (i = 0; i < 16; i++) {
2168 qp = &info.dma_info_command_data[i];
2169 spuqp = &ctx->csa.priv2.spuq[i];
2170
2171 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2172 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2173 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2174 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2175 }
2176
2177 return simple_read_from_buffer(buf, len, pos, &info,
2178 sizeof info);
2179 }
2180
2181 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2182 size_t len, loff_t *pos)
2183 {
2184 struct spu_context *ctx = file->private_data;
2185 int ret;
2186
2187 if (!access_ok(VERIFY_WRITE, buf, len))
2188 return -EFAULT;
2189
2190 ret = spu_acquire_saved(ctx);
2191 if (ret)
2192 return ret;
2193 spin_lock(&ctx->csa.register_lock);
2194 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2195 spin_unlock(&ctx->csa.register_lock);
2196 spu_release_saved(ctx);
2197
2198 return ret;
2199 }
2200
2201 static const struct file_operations spufs_dma_info_fops = {
2202 .open = spufs_info_open,
2203 .read = spufs_dma_info_read,
2204 };
2205
2206 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2207 char __user *buf, size_t len, loff_t *pos)
2208 {
2209 struct spu_proxydma_info info;
2210 struct mfc_cq_sr *qp, *puqp;
2211 int ret = sizeof info;
2212 int i;
2213
2214 if (len < ret)
2215 return -EINVAL;
2216
2217 if (!access_ok(VERIFY_WRITE, buf, len))
2218 return -EFAULT;
2219
2220 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2221 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2222 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2223 for (i = 0; i < 8; i++) {
2224 qp = &info.proxydma_info_command_data[i];
2225 puqp = &ctx->csa.priv2.puq[i];
2226
2227 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2228 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2229 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2230 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2231 }
2232
2233 return simple_read_from_buffer(buf, len, pos, &info,
2234 sizeof info);
2235 }
2236
2237 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2238 size_t len, loff_t *pos)
2239 {
2240 struct spu_context *ctx = file->private_data;
2241 int ret;
2242
2243 ret = spu_acquire_saved(ctx);
2244 if (ret)
2245 return ret;
2246 spin_lock(&ctx->csa.register_lock);
2247 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2248 spin_unlock(&ctx->csa.register_lock);
2249 spu_release_saved(ctx);
2250
2251 return ret;
2252 }
2253
2254 static const struct file_operations spufs_proxydma_info_fops = {
2255 .open = spufs_info_open,
2256 .read = spufs_proxydma_info_read,
2257 };
2258
2259 static int spufs_show_tid(struct seq_file *s, void *private)
2260 {
2261 struct spu_context *ctx = s->private;
2262
2263 seq_printf(s, "%d\n", ctx->tid);
2264 return 0;
2265 }
2266
2267 static int spufs_tid_open(struct inode *inode, struct file *file)
2268 {
2269 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2270 }
2271
2272 static const struct file_operations spufs_tid_fops = {
2273 .open = spufs_tid_open,
2274 .read = seq_read,
2275 .llseek = seq_lseek,
2276 .release = single_release,
2277 };
2278
2279 static const char *ctx_state_names[] = {
2280 "user", "system", "iowait", "loaded"
2281 };
2282
2283 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2284 enum spu_utilization_state state)
2285 {
2286 struct timespec ts;
2287 unsigned long long time = ctx->stats.times[state];
2288
2289 /*
2290 * In general, utilization statistics are updated by the controlling
2291 * thread as the spu context moves through various well defined
2292 * state transitions, but if the context is lazily loaded its
2293 * utilization statistics are not updated as the controlling thread
2294 * is not tightly coupled with the execution of the spu context. We
2295 * calculate and apply the time delta from the last recorded state
2296 * of the spu context.
2297 */
2298 if (ctx->spu && ctx->stats.util_state == state) {
2299 ktime_get_ts(&ts);
2300 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2301 }
2302
2303 return time / NSEC_PER_MSEC;
2304 }
2305
2306 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2307 {
2308 unsigned long long slb_flts = ctx->stats.slb_flt;
2309
2310 if (ctx->state == SPU_STATE_RUNNABLE) {
2311 slb_flts += (ctx->spu->stats.slb_flt -
2312 ctx->stats.slb_flt_base);
2313 }
2314
2315 return slb_flts;
2316 }
2317
2318 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2319 {
2320 unsigned long long class2_intrs = ctx->stats.class2_intr;
2321
2322 if (ctx->state == SPU_STATE_RUNNABLE) {
2323 class2_intrs += (ctx->spu->stats.class2_intr -
2324 ctx->stats.class2_intr_base);
2325 }
2326
2327 return class2_intrs;
2328 }
2329
2330
2331 static int spufs_show_stat(struct seq_file *s, void *private)
2332 {
2333 struct spu_context *ctx = s->private;
2334 int ret;
2335
2336 ret = spu_acquire(ctx);
2337 if (ret)
2338 return ret;
2339
2340 seq_printf(s, "%s %llu %llu %llu %llu "
2341 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2342 ctx_state_names[ctx->stats.util_state],
2343 spufs_acct_time(ctx, SPU_UTIL_USER),
2344 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2345 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2346 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2347 ctx->stats.vol_ctx_switch,
2348 ctx->stats.invol_ctx_switch,
2349 spufs_slb_flts(ctx),
2350 ctx->stats.hash_flt,
2351 ctx->stats.min_flt,
2352 ctx->stats.maj_flt,
2353 spufs_class2_intrs(ctx),
2354 ctx->stats.libassist);
2355 spu_release(ctx);
2356 return 0;
2357 }
2358
2359 static int spufs_stat_open(struct inode *inode, struct file *file)
2360 {
2361 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2362 }
2363
2364 static const struct file_operations spufs_stat_fops = {
2365 .open = spufs_stat_open,
2366 .read = seq_read,
2367 .llseek = seq_lseek,
2368 .release = single_release,
2369 };
2370
2371
2372 struct tree_descr spufs_dir_contents[] = {
2373 { "capabilities", &spufs_caps_fops, 0444, },
2374 { "mem", &spufs_mem_fops, 0666, },
2375 { "regs", &spufs_regs_fops, 0666, },
2376 { "mbox", &spufs_mbox_fops, 0444, },
2377 { "ibox", &spufs_ibox_fops, 0444, },
2378 { "wbox", &spufs_wbox_fops, 0222, },
2379 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2380 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2381 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2382 { "signal1", &spufs_signal1_fops, 0666, },
2383 { "signal2", &spufs_signal2_fops, 0666, },
2384 { "signal1_type", &spufs_signal1_type, 0666, },
2385 { "signal2_type", &spufs_signal2_type, 0666, },
2386 { "cntl", &spufs_cntl_fops, 0666, },
2387 { "fpcr", &spufs_fpcr_fops, 0666, },
2388 { "lslr", &spufs_lslr_ops, 0444, },
2389 { "mfc", &spufs_mfc_fops, 0666, },
2390 { "mss", &spufs_mss_fops, 0666, },
2391 { "npc", &spufs_npc_ops, 0666, },
2392 { "srr0", &spufs_srr0_ops, 0666, },
2393 { "decr", &spufs_decr_ops, 0666, },
2394 { "decr_status", &spufs_decr_status_ops, 0666, },
2395 { "event_mask", &spufs_event_mask_ops, 0666, },
2396 { "event_status", &spufs_event_status_ops, 0444, },
2397 { "psmap", &spufs_psmap_fops, 0666, },
2398 { "phys-id", &spufs_id_ops, 0666, },
2399 { "object-id", &spufs_object_id_ops, 0666, },
2400 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2401 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2402 { "wbox_info", &spufs_wbox_info_fops, 0444, },
2403 { "dma_info", &spufs_dma_info_fops, 0444, },
2404 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
2405 { "tid", &spufs_tid_fops, 0444, },
2406 { "stat", &spufs_stat_fops, 0444, },
2407 {},
2408 };
2409
2410 struct tree_descr spufs_dir_nosched_contents[] = {
2411 { "capabilities", &spufs_caps_fops, 0444, },
2412 { "mem", &spufs_mem_fops, 0666, },
2413 { "mbox", &spufs_mbox_fops, 0444, },
2414 { "ibox", &spufs_ibox_fops, 0444, },
2415 { "wbox", &spufs_wbox_fops, 0222, },
2416 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2417 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2418 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2419 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2420 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2421 { "signal1_type", &spufs_signal1_type, 0666, },
2422 { "signal2_type", &spufs_signal2_type, 0666, },
2423 { "mss", &spufs_mss_fops, 0666, },
2424 { "mfc", &spufs_mfc_fops, 0666, },
2425 { "cntl", &spufs_cntl_fops, 0666, },
2426 { "npc", &spufs_npc_ops, 0666, },
2427 { "psmap", &spufs_psmap_fops, 0666, },
2428 { "phys-id", &spufs_id_ops, 0666, },
2429 { "object-id", &spufs_object_id_ops, 0666, },
2430 { "tid", &spufs_tid_fops, 0444, },
2431 { "stat", &spufs_stat_fops, 0444, },
2432 {},
2433 };
2434
2435 struct spufs_coredump_reader spufs_coredump_read[] = {
2436 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2437 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2438 { "lslr", NULL, spufs_lslr_get, 19 },
2439 { "decr", NULL, spufs_decr_get, 19 },
2440 { "decr_status", NULL, spufs_decr_status_get, 19 },
2441 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2442 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2443 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
2444 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2445 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2446 { "event_mask", NULL, spufs_event_mask_get, 19 },
2447 { "event_status", NULL, spufs_event_status_get, 19 },
2448 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2449 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2450 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2451 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2452 { "proxydma_info", __spufs_proxydma_info_read,
2453 NULL, sizeof(struct spu_proxydma_info)},
2454 { "object-id", NULL, spufs_object_id_get, 19 },
2455 { "npc", NULL, spufs_npc_get, 19 },
2456 { NULL },
2457 };