libfs: allow error return from simple attributes
[linux-2.6/btrfs-unstable.git] / arch / powerpc / platforms / cell / spufs / file.c
blob9326714717ca1db5febb93eb692a075a3e5039fc
1 /*
2 * SPU file system -- file contents
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
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.
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.
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.
23 #undef DEBUG
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>
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>
40 #include "spufs.h"
42 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
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 */
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)
59 struct spufs_attr *attr;
61 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
62 if (!attr)
63 return -ENOMEM;
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;
72 return nonseekable_open(inode, file);
75 static int spufs_attr_release(struct inode *inode, struct file *file)
77 kfree(file->private_data);
78 return 0;
81 static ssize_t spufs_attr_read(struct file *file, char __user *buf,
82 size_t len, loff_t *ppos)
84 struct spufs_attr *attr;
85 size_t size;
86 ssize_t ret;
88 attr = file->private_data;
89 if (!attr->get)
90 return -EACCES;
92 ret = mutex_lock_interruptible(&attr->mutex);
93 if (ret)
94 return ret;
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;
104 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
105 attr->fmt, (unsigned long long)val);
108 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
109 out:
110 mutex_unlock(&attr->mutex);
111 return ret;
114 static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
115 size_t len, loff_t *ppos)
117 struct spufs_attr *attr;
118 u64 val;
119 size_t size;
120 ssize_t ret;
122 attr = file->private_data;
123 if (!attr->set)
124 return -EACCES;
126 ret = mutex_lock_interruptible(&attr->mutex);
127 if (ret)
128 return ret;
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;
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;
144 #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
145 static int __fops ## _open(struct inode *inode, struct file *file) \
147 __simple_attr_check_format(__fmt, 0ull); \
148 return spufs_attr_open(inode, file, __get, __set, __fmt); \
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, \
159 static int
160 spufs_mem_open(struct inode *inode, struct file *file)
162 struct spufs_inode_info *i = SPUFS_I(inode);
163 struct spu_context *ctx = i->i_ctx;
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;
173 static int
174 spufs_mem_release(struct inode *inode, struct file *file)
176 struct spufs_inode_info *i = SPUFS_I(inode);
177 struct spu_context *ctx = i->i_ctx;
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;
186 static ssize_t
187 __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
188 size_t size, loff_t *pos)
190 char *local_store = ctx->ops->get_ls(ctx);
191 return simple_read_from_buffer(buffer, size, pos, local_store,
192 LS_SIZE);
195 static ssize_t
196 spufs_mem_read(struct file *file, char __user *buffer,
197 size_t size, loff_t *pos)
199 struct spu_context *ctx = file->private_data;
200 ssize_t ret;
202 ret = spu_acquire(ctx);
203 if (ret)
204 return ret;
205 ret = __spufs_mem_read(ctx, buffer, size, pos);
206 spu_release(ctx);
208 return ret;
211 static ssize_t
212 spufs_mem_write(struct file *file, const char __user *buffer,
213 size_t size, loff_t *ppos)
215 struct spu_context *ctx = file->private_data;
216 char *local_store;
217 loff_t pos = *ppos;
218 int ret;
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;
227 ret = spu_acquire(ctx);
228 if (ret)
229 return ret;
231 local_store = ctx->ops->get_ls(ctx);
232 ret = copy_from_user(local_store + pos, buffer, size);
233 spu_release(ctx);
235 if (ret)
236 return -EFAULT;
237 *ppos = pos + size;
238 return size;
241 static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
242 unsigned long address)
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;
250 /* Check what page size we are using */
251 psize = get_slice_psize(vma->vm_mm, address);
253 /* Some sanity checking */
254 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
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;
261 #endif /* CONFIG_SPU_FS_64K_LS */
263 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
264 if (offset >= LS_SIZE)
265 return NOPFN_SIGBUS;
267 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
268 addr0, address, offset);
270 if (spu_acquire(ctx))
271 return NOPFN_REFAULT;
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;
282 vm_insert_pfn(vma, address, pfn);
284 spu_release(ctx);
286 return NOPFN_REFAULT;
290 static struct vm_operations_struct spufs_mem_mmap_vmops = {
291 .nopfn = spufs_mem_mmap_nopfn,
294 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
296 #ifdef CONFIG_SPU_FS_64K_LS
297 struct spu_context *ctx = file->private_data;
298 struct spu_state *csa = &ctx->csa;
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;
310 #endif /* CONFIG_SPU_FS_64K_LS */
312 if (!(vma->vm_flags & VM_SHARED))
313 return -EINVAL;
315 vma->vm_flags |= VM_IO | VM_PFNMAP;
316 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
317 | _PAGE_NO_CACHE);
319 vma->vm_ops = &spufs_mem_mmap_vmops;
320 return 0;
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)
328 struct spu_context *ctx = file->private_data;
329 struct spu_state *csa = &ctx->csa;
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);
336 /* Else, try to obtain a 64K pages slice */
337 return slice_get_unmapped_area(addr, len, flags,
338 MMU_PAGE_64K, 1, 0);
340 #endif /* CONFIG_SPU_FS_64K_LS */
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
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)
359 struct spu_context *ctx = vma->vm_file->private_data;
360 unsigned long area, offset = address - vma->vm_start;
362 spu_context_nospu_trace(spufs_ps_nopfn__enter, ctx);
364 offset += vma->vm_pgoff << PAGE_SHIFT;
365 if (offset >= ps_size)
366 return NOPFN_SIGBUS;
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.
376 if (spu_acquire(ctx))
377 return NOPFN_REFAULT;
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);
391 spu_release(ctx);
392 return NOPFN_REFAULT;
395 #if SPUFS_MMAP_4K
396 static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
397 unsigned long address)
399 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
402 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
403 .nopfn = spufs_cntl_mmap_nopfn,
407 * mmap support for problem state control area [0x4000 - 0x4fff].
409 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
411 if (!(vma->vm_flags & VM_SHARED))
412 return -EINVAL;
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);
418 vma->vm_ops = &spufs_cntl_mmap_vmops;
419 return 0;
421 #else /* SPUFS_MMAP_4K */
422 #define spufs_cntl_mmap NULL
423 #endif /* !SPUFS_MMAP_4K */
425 static int spufs_cntl_get(void *data, u64 *val)
427 struct spu_context *ctx = data;
428 int ret;
430 ret = spu_acquire(ctx);
431 if (ret)
432 return ret;
433 *val = ctx->ops->status_read(ctx);
434 spu_release(ctx);
436 return 0;
439 static int spufs_cntl_set(void *data, u64 val)
441 struct spu_context *ctx = data;
442 int ret;
444 ret = spu_acquire(ctx);
445 if (ret)
446 return ret;
447 ctx->ops->runcntl_write(ctx, val);
448 spu_release(ctx);
450 return 0;
453 static int spufs_cntl_open(struct inode *inode, struct file *file)
455 struct spufs_inode_info *i = SPUFS_I(inode);
456 struct spu_context *ctx = i->i_ctx;
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");
467 static int
468 spufs_cntl_release(struct inode *inode, struct file *file)
470 struct spufs_inode_info *i = SPUFS_I(inode);
471 struct spu_context *ctx = i->i_ctx;
473 simple_attr_close(inode, file);
475 mutex_lock(&ctx->mapping_lock);
476 if (!--i->i_openers)
477 ctx->cntl = NULL;
478 mutex_unlock(&ctx->mapping_lock);
479 return 0;
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,
490 static int
491 spufs_regs_open(struct inode *inode, struct file *file)
493 struct spufs_inode_info *i = SPUFS_I(inode);
494 file->private_data = i->i_ctx;
495 return 0;
498 static ssize_t
499 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
500 size_t size, loff_t *pos)
502 struct spu_lscsa *lscsa = ctx->csa.lscsa;
503 return simple_read_from_buffer(buffer, size, pos,
504 lscsa->gprs, sizeof lscsa->gprs);
507 static ssize_t
508 spufs_regs_read(struct file *file, char __user *buffer,
509 size_t size, loff_t *pos)
511 int ret;
512 struct spu_context *ctx = file->private_data;
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;
522 static ssize_t
523 spufs_regs_write(struct file *file, const char __user *buffer,
524 size_t size, loff_t *pos)
526 struct spu_context *ctx = file->private_data;
527 struct spu_lscsa *lscsa = ctx->csa.lscsa;
528 int ret;
530 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
531 if (size <= 0)
532 return -EFBIG;
533 *pos += size;
535 ret = spu_acquire_saved(ctx);
536 if (ret)
537 return ret;
539 ret = copy_from_user(lscsa->gprs + *pos - size,
540 buffer, size) ? -EFAULT : size;
542 spu_release_saved(ctx);
543 return ret;
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,
553 static ssize_t
554 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
555 size_t size, loff_t * pos)
557 struct spu_lscsa *lscsa = ctx->csa.lscsa;
558 return simple_read_from_buffer(buffer, size, pos,
559 &lscsa->fpcr, sizeof(lscsa->fpcr));
562 static ssize_t
563 spufs_fpcr_read(struct file *file, char __user * buffer,
564 size_t size, loff_t * pos)
566 int ret;
567 struct spu_context *ctx = file->private_data;
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;
577 static ssize_t
578 spufs_fpcr_write(struct file *file, const char __user * buffer,
579 size_t size, loff_t * pos)
581 struct spu_context *ctx = file->private_data;
582 struct spu_lscsa *lscsa = ctx->csa.lscsa;
583 int ret;
585 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
586 if (size <= 0)
587 return -EFBIG;
589 ret = spu_acquire_saved(ctx);
590 if (ret)
591 return ret;
593 *pos += size;
594 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
595 buffer, size) ? -EFAULT : size;
597 spu_release_saved(ctx);
598 return ret;
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,
608 /* generic open function for all pipe-like files */
609 static int spufs_pipe_open(struct inode *inode, struct file *file)
611 struct spufs_inode_info *i = SPUFS_I(inode);
612 file->private_data = i->i_ctx;
614 return nonseekable_open(inode, file);
618 * Read as many bytes from the mailbox as possible, until
619 * one of the conditions becomes true:
621 * - no more data available in the mailbox
622 * - end of the user provided buffer
623 * - end of the mapped area
625 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
626 size_t len, loff_t *pos)
628 struct spu_context *ctx = file->private_data;
629 u32 mbox_data, __user *udata;
630 ssize_t count;
632 if (len < 4)
633 return -EINVAL;
635 if (!access_ok(VERIFY_WRITE, buf, len))
636 return -EFAULT;
638 udata = (void __user *)buf;
640 count = spu_acquire(ctx);
641 if (count)
642 return count;
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;
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.
655 ret = __put_user(mbox_data, udata);
656 if (ret) {
657 if (!count)
658 count = -EFAULT;
659 break;
662 spu_release(ctx);
664 if (!count)
665 count = -EAGAIN;
667 return count;
670 static const struct file_operations spufs_mbox_fops = {
671 .open = spufs_pipe_open,
672 .read = spufs_mbox_read,
675 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
676 size_t len, loff_t *pos)
678 struct spu_context *ctx = file->private_data;
679 ssize_t ret;
680 u32 mbox_stat;
682 if (len < 4)
683 return -EINVAL;
685 ret = spu_acquire(ctx);
686 if (ret)
687 return ret;
689 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
691 spu_release(ctx);
693 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
694 return -EFAULT;
696 return 4;
699 static const struct file_operations spufs_mbox_stat_fops = {
700 .open = spufs_pipe_open,
701 .read = spufs_mbox_stat_read,
704 /* low-level ibox access function */
705 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
707 return ctx->ops->ibox_read(ctx, data);
710 static int spufs_ibox_fasync(int fd, struct file *file, int on)
712 struct spu_context *ctx = file->private_data;
714 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
717 /* interrupt-level ibox callback function. */
718 void spufs_ibox_callback(struct spu *spu)
720 struct spu_context *ctx = spu->ctx;
722 if (!ctx)
723 return;
725 wake_up_all(&ctx->ibox_wq);
726 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
730 * Read as many bytes from the interrupt mailbox as possible, until
731 * one of the conditions becomes true:
733 * - no more data available in the mailbox
734 * - end of the user provided buffer
735 * - end of the mapped area
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.
741 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
742 size_t len, loff_t *pos)
744 struct spu_context *ctx = file->private_data;
745 u32 ibox_data, __user *udata;
746 ssize_t count;
748 if (len < 4)
749 return -EINVAL;
751 if (!access_ok(VERIFY_WRITE, buf, len))
752 return -EFAULT;
754 udata = (void __user *)buf;
756 count = spu_acquire(ctx);
757 if (count)
758 return count;
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));
768 if (count)
769 goto out;
771 /* if we can't write at all, return -EFAULT */
772 count = __put_user(ibox_data, udata);
773 if (count)
774 goto out;
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;
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.
786 ret = __put_user(ibox_data, udata);
787 if (ret)
788 break;
791 out:
792 spu_release(ctx);
794 return count;
797 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
799 struct spu_context *ctx = file->private_data;
800 unsigned int mask;
802 poll_wait(file, &ctx->ibox_wq, wait);
805 * For now keep this uninterruptible and also ignore the rule
806 * that poll should not sleep. Will be fixed later.
808 mutex_lock(&ctx->state_mutex);
809 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
810 spu_release(ctx);
812 return mask;
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,
822 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
823 size_t len, loff_t *pos)
825 struct spu_context *ctx = file->private_data;
826 ssize_t ret;
827 u32 ibox_stat;
829 if (len < 4)
830 return -EINVAL;
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);
838 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
839 return -EFAULT;
841 return 4;
844 static const struct file_operations spufs_ibox_stat_fops = {
845 .open = spufs_pipe_open,
846 .read = spufs_ibox_stat_read,
849 /* low-level mailbox write */
850 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
852 return ctx->ops->wbox_write(ctx, data);
855 static int spufs_wbox_fasync(int fd, struct file *file, int on)
857 struct spu_context *ctx = file->private_data;
858 int ret;
860 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
862 return ret;
865 /* interrupt-level wbox callback function. */
866 void spufs_wbox_callback(struct spu *spu)
868 struct spu_context *ctx = spu->ctx;
870 if (!ctx)
871 return;
873 wake_up_all(&ctx->wbox_wq);
874 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
878 * Write as many bytes to the interrupt mailbox as possible, until
879 * one of the conditions becomes true:
881 * - the mailbox is full
882 * - end of the user provided buffer
883 * - end of the mapped area
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.
889 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
890 size_t len, loff_t *pos)
892 struct spu_context *ctx = file->private_data;
893 u32 wbox_data, __user *udata;
894 ssize_t count;
896 if (len < 4)
897 return -EINVAL;
899 udata = (void __user *)buf;
900 if (!access_ok(VERIFY_READ, buf, len))
901 return -EFAULT;
903 if (__get_user(wbox_data, udata))
904 return -EFAULT;
906 count = spu_acquire(ctx);
907 if (count)
908 return count;
911 * make sure we can at least write one element, by waiting
912 * in case of !O_NONBLOCK
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));
922 if (count)
923 goto out;
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;
932 ret = spu_wbox_write(ctx, wbox_data);
933 if (ret == 0)
934 break;
937 out:
938 spu_release(ctx);
939 return count;
942 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
944 struct spu_context *ctx = file->private_data;
945 unsigned int mask;
947 poll_wait(file, &ctx->wbox_wq, wait);
950 * For now keep this uninterruptible and also ignore the rule
951 * that poll should not sleep. Will be fixed later.
953 mutex_lock(&ctx->state_mutex);
954 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
955 spu_release(ctx);
957 return mask;
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,
967 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
968 size_t len, loff_t *pos)
970 struct spu_context *ctx = file->private_data;
971 ssize_t ret;
972 u32 wbox_stat;
974 if (len < 4)
975 return -EINVAL;
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);
983 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
984 return -EFAULT;
986 return 4;
989 static const struct file_operations spufs_wbox_stat_fops = {
990 .open = spufs_pipe_open,
991 .read = spufs_wbox_stat_read,
994 static int spufs_signal1_open(struct inode *inode, struct file *file)
996 struct spufs_inode_info *i = SPUFS_I(inode);
997 struct spu_context *ctx = i->i_ctx;
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);
1007 static int
1008 spufs_signal1_release(struct inode *inode, struct file *file)
1010 struct spufs_inode_info *i = SPUFS_I(inode);
1011 struct spu_context *ctx = i->i_ctx;
1013 mutex_lock(&ctx->mapping_lock);
1014 if (!--i->i_openers)
1015 ctx->signal1 = NULL;
1016 mutex_unlock(&ctx->mapping_lock);
1017 return 0;
1020 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
1021 size_t len, loff_t *pos)
1023 int ret = 0;
1024 u32 data;
1026 if (len < 4)
1027 return -EINVAL;
1029 if (ctx->csa.spu_chnlcnt_RW[3]) {
1030 data = ctx->csa.spu_chnldata_RW[3];
1031 ret = 4;
1034 if (!ret)
1035 goto out;
1037 if (copy_to_user(buf, &data, 4))
1038 return -EFAULT;
1040 out:
1041 return ret;
1044 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1045 size_t len, loff_t *pos)
1047 int ret;
1048 struct spu_context *ctx = file->private_data;
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);
1056 return ret;
1059 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1060 size_t len, loff_t *pos)
1062 struct spu_context *ctx;
1063 ssize_t ret;
1064 u32 data;
1066 ctx = file->private_data;
1068 if (len < 4)
1069 return -EINVAL;
1071 if (copy_from_user(&data, buf, 4))
1072 return -EFAULT;
1074 ret = spu_acquire(ctx);
1075 if (ret)
1076 return ret;
1077 ctx->ops->signal1_write(ctx, data);
1078 spu_release(ctx);
1080 return 4;
1083 static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1084 unsigned long address)
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
1092 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1093 #else
1094 #error unsupported page size
1095 #endif
1098 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
1099 .nopfn = spufs_signal1_mmap_nopfn,
1102 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1104 if (!(vma->vm_flags & VM_SHARED))
1105 return -EINVAL;
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);
1111 vma->vm_ops = &spufs_signal1_mmap_vmops;
1112 return 0;
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,
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,
1130 static int spufs_signal2_open(struct inode *inode, struct file *file)
1132 struct spufs_inode_info *i = SPUFS_I(inode);
1133 struct spu_context *ctx = i->i_ctx;
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);
1143 static int
1144 spufs_signal2_release(struct inode *inode, struct file *file)
1146 struct spufs_inode_info *i = SPUFS_I(inode);
1147 struct spu_context *ctx = i->i_ctx;
1149 mutex_lock(&ctx->mapping_lock);
1150 if (!--i->i_openers)
1151 ctx->signal2 = NULL;
1152 mutex_unlock(&ctx->mapping_lock);
1153 return 0;
1156 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
1157 size_t len, loff_t *pos)
1159 int ret = 0;
1160 u32 data;
1162 if (len < 4)
1163 return -EINVAL;
1165 if (ctx->csa.spu_chnlcnt_RW[4]) {
1166 data = ctx->csa.spu_chnldata_RW[4];
1167 ret = 4;
1170 if (!ret)
1171 goto out;
1173 if (copy_to_user(buf, &data, 4))
1174 return -EFAULT;
1176 out:
1177 return ret;
1180 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1181 size_t len, loff_t *pos)
1183 struct spu_context *ctx = file->private_data;
1184 int ret;
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);
1192 return ret;
1195 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1196 size_t len, loff_t *pos)
1198 struct spu_context *ctx;
1199 ssize_t ret;
1200 u32 data;
1202 ctx = file->private_data;
1204 if (len < 4)
1205 return -EINVAL;
1207 if (copy_from_user(&data, buf, 4))
1208 return -EFAULT;
1210 ret = spu_acquire(ctx);
1211 if (ret)
1212 return ret;
1213 ctx->ops->signal2_write(ctx, data);
1214 spu_release(ctx);
1216 return 4;
1219 #if SPUFS_MMAP_4K
1220 static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1221 unsigned long address)
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
1229 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1230 #else
1231 #error unsupported page size
1232 #endif
1235 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1236 .nopfn = spufs_signal2_mmap_nopfn,
1239 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1241 if (!(vma->vm_flags & VM_SHARED))
1242 return -EINVAL;
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);
1248 vma->vm_ops = &spufs_signal2_mmap_vmops;
1249 return 0;
1251 #else /* SPUFS_MMAP_4K */
1252 #define spufs_signal2_mmap NULL
1253 #endif /* !SPUFS_MMAP_4K */
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,
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,
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.
1275 #define SPU_ATTR_NOACQUIRE 0
1276 #define SPU_ATTR_ACQUIRE 1
1277 #define SPU_ATTR_ACQUIRE_SAVED 2
1279 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
1280 static int __##__get(void *data, u64 *val) \
1282 struct spu_context *ctx = data; \
1283 int ret = 0; \
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); \
1300 return 0; \
1302 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1304 static int spufs_signal1_type_set(void *data, u64 val)
1306 struct spu_context *ctx = data;
1307 int ret;
1309 ret = spu_acquire(ctx);
1310 if (ret)
1311 return ret;
1312 ctx->ops->signal1_type_set(ctx, val);
1313 spu_release(ctx);
1315 return 0;
1318 static u64 spufs_signal1_type_get(struct spu_context *ctx)
1320 return ctx->ops->signal1_type_get(ctx);
1322 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1323 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
1326 static int spufs_signal2_type_set(void *data, u64 val)
1328 struct spu_context *ctx = data;
1329 int ret;
1331 ret = spu_acquire(ctx);
1332 if (ret)
1333 return ret;
1334 ctx->ops->signal2_type_set(ctx, val);
1335 spu_release(ctx);
1337 return 0;
1340 static u64 spufs_signal2_type_get(struct spu_context *ctx)
1342 return ctx->ops->signal2_type_get(ctx);
1344 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1345 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
1347 #if SPUFS_MMAP_4K
1348 static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1349 unsigned long address)
1351 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
1354 static struct vm_operations_struct spufs_mss_mmap_vmops = {
1355 .nopfn = spufs_mss_mmap_nopfn,
1359 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1361 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1363 if (!(vma->vm_flags & VM_SHARED))
1364 return -EINVAL;
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);
1370 vma->vm_ops = &spufs_mss_mmap_vmops;
1371 return 0;
1373 #else /* SPUFS_MMAP_4K */
1374 #define spufs_mss_mmap NULL
1375 #endif /* !SPUFS_MMAP_4K */
1377 static int spufs_mss_open(struct inode *inode, struct file *file)
1379 struct spufs_inode_info *i = SPUFS_I(inode);
1380 struct spu_context *ctx = i->i_ctx;
1382 file->private_data = i->i_ctx;
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);
1391 static int
1392 spufs_mss_release(struct inode *inode, struct file *file)
1394 struct spufs_inode_info *i = SPUFS_I(inode);
1395 struct spu_context *ctx = i->i_ctx;
1397 mutex_lock(&ctx->mapping_lock);
1398 if (!--i->i_openers)
1399 ctx->mss = NULL;
1400 mutex_unlock(&ctx->mapping_lock);
1401 return 0;
1404 static const struct file_operations spufs_mss_fops = {
1405 .open = spufs_mss_open,
1406 .release = spufs_mss_release,
1407 .mmap = spufs_mss_mmap,
1410 static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1411 unsigned long address)
1413 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
1416 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1417 .nopfn = spufs_psmap_mmap_nopfn,
1421 * mmap support for full problem state area [0x00000 - 0x1ffff].
1423 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1425 if (!(vma->vm_flags & VM_SHARED))
1426 return -EINVAL;
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);
1432 vma->vm_ops = &spufs_psmap_mmap_vmops;
1433 return 0;
1436 static int spufs_psmap_open(struct inode *inode, struct file *file)
1438 struct spufs_inode_info *i = SPUFS_I(inode);
1439 struct spu_context *ctx = i->i_ctx;
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);
1449 static int
1450 spufs_psmap_release(struct inode *inode, struct file *file)
1452 struct spufs_inode_info *i = SPUFS_I(inode);
1453 struct spu_context *ctx = i->i_ctx;
1455 mutex_lock(&ctx->mapping_lock);
1456 if (!--i->i_openers)
1457 ctx->psmap = NULL;
1458 mutex_unlock(&ctx->mapping_lock);
1459 return 0;
1462 static const struct file_operations spufs_psmap_fops = {
1463 .open = spufs_psmap_open,
1464 .release = spufs_psmap_release,
1465 .mmap = spufs_psmap_mmap,
1469 #if SPUFS_MMAP_4K
1470 static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1471 unsigned long address)
1473 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
1476 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1477 .nopfn = spufs_mfc_mmap_nopfn,
1481 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1483 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1485 if (!(vma->vm_flags & VM_SHARED))
1486 return -EINVAL;
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);
1492 vma->vm_ops = &spufs_mfc_mmap_vmops;
1493 return 0;
1495 #else /* SPUFS_MMAP_4K */
1496 #define spufs_mfc_mmap NULL
1497 #endif /* !SPUFS_MMAP_4K */
1499 static int spufs_mfc_open(struct inode *inode, struct file *file)
1501 struct spufs_inode_info *i = SPUFS_I(inode);
1502 struct spu_context *ctx = i->i_ctx;
1504 /* we don't want to deal with DMA into other processes */
1505 if (ctx->owner != current->mm)
1506 return -EINVAL;
1508 if (atomic_read(&inode->i_count) != 1)
1509 return -EBUSY;
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);
1519 static int
1520 spufs_mfc_release(struct inode *inode, struct file *file)
1522 struct spufs_inode_info *i = SPUFS_I(inode);
1523 struct spu_context *ctx = i->i_ctx;
1525 mutex_lock(&ctx->mapping_lock);
1526 if (!--i->i_openers)
1527 ctx->mfc = NULL;
1528 mutex_unlock(&ctx->mapping_lock);
1529 return 0;
1532 /* interrupt-level mfc callback function. */
1533 void spufs_mfc_callback(struct spu *spu)
1535 struct spu_context *ctx = spu->ctx;
1537 if (!ctx)
1538 return;
1540 wake_up_all(&ctx->mfc_wq);
1542 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1543 if (ctx->mfc_fasync) {
1544 u32 free_elements, tagstatus;
1545 unsigned int mask;
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);
1551 mask = 0;
1552 if (free_elements & 0xffff)
1553 mask |= POLLOUT;
1554 if (tagstatus & ctx->tagwait)
1555 mask |= POLLIN;
1557 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1561 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
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;
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;
1576 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1577 size_t size, loff_t *pos)
1579 struct spu_context *ctx = file->private_data;
1580 int ret = -EINVAL;
1581 u32 status;
1583 if (size != 4)
1584 goto out;
1586 ret = spu_acquire(ctx);
1587 if (ret)
1588 return ret;
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));
1602 spu_release(ctx);
1604 if (ret)
1605 goto out;
1607 ret = 4;
1608 if (copy_to_user(buffer, &status, 4))
1609 ret = -EFAULT;
1611 out:
1612 return ret;
1615 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1617 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1618 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
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;
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;
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;
1665 if (cmd->size > 16 * 1024) {
1666 pr_debug("invalid DMA size %x\n", cmd->size);
1667 return -EIO;
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;
1676 if (cmd->class) {
1677 /* not supported in this version */
1678 pr_debug("invalid DMA class\n");
1679 return -EIO;
1682 return 0;
1685 static int spu_send_mfc_command(struct spu_context *ctx,
1686 struct mfc_dma_command cmd,
1687 int *error)
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;
1700 return 1;
1703 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1704 size_t size, loff_t *pos)
1706 struct spu_context *ctx = file->private_data;
1707 struct mfc_dma_command cmd;
1708 int ret = -EINVAL;
1710 if (size != sizeof cmd)
1711 goto out;
1713 ret = -EFAULT;
1714 if (copy_from_user(&cmd, buffer, sizeof cmd))
1715 goto out;
1717 ret = spufs_check_valid_dma(&cmd);
1718 if (ret)
1719 goto out;
1721 ret = spu_acquire(ctx);
1722 if (ret)
1723 goto out;
1725 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1726 if (ret)
1727 goto out;
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;
1739 if (ret)
1740 goto out_unlock;
1742 ctx->tagwait |= 1 << cmd.tag;
1743 ret = size;
1745 out_unlock:
1746 spu_release(ctx);
1747 out:
1748 return ret;
1751 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1753 struct spu_context *ctx = file->private_data;
1754 u32 free_elements, tagstatus;
1755 unsigned int mask;
1757 poll_wait(file, &ctx->mfc_wq, wait);
1760 * For now keep this uninterruptible and also ignore the rule
1761 * that poll should not sleep. Will be fixed later.
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);
1769 mask = 0;
1770 if (free_elements & 0xffff)
1771 mask |= POLLOUT | POLLWRNORM;
1772 if (tagstatus & ctx->tagwait)
1773 mask |= POLLIN | POLLRDNORM;
1775 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1776 free_elements, tagstatus, ctx->tagwait);
1778 return mask;
1781 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1783 struct spu_context *ctx = file->private_data;
1784 int ret;
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);
1803 return ret;
1806 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1807 int datasync)
1809 return spufs_mfc_flush(file, NULL);
1812 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1814 struct spu_context *ctx = file->private_data;
1816 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
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,
1831 static int spufs_npc_set(void *data, u64 val)
1833 struct spu_context *ctx = data;
1834 int ret;
1836 ret = spu_acquire(ctx);
1837 if (ret)
1838 return ret;
1839 ctx->ops->npc_write(ctx, val);
1840 spu_release(ctx);
1842 return 0;
1845 static u64 spufs_npc_get(struct spu_context *ctx)
1847 return ctx->ops->npc_read(ctx);
1849 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1850 "0x%llx\n", SPU_ATTR_ACQUIRE);
1852 static int spufs_decr_set(void *data, u64 val)
1854 struct spu_context *ctx = data;
1855 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1856 int ret;
1858 ret = spu_acquire_saved(ctx);
1859 if (ret)
1860 return ret;
1861 lscsa->decr.slot[0] = (u32) val;
1862 spu_release_saved(ctx);
1864 return 0;
1867 static u64 spufs_decr_get(struct spu_context *ctx)
1869 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1870 return lscsa->decr.slot[0];
1872 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1873 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
1875 static int spufs_decr_status_set(void *data, u64 val)
1877 struct spu_context *ctx = data;
1878 int ret;
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);
1889 return 0;
1892 static u64 spufs_decr_status_get(struct spu_context *ctx)
1894 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1895 return SPU_DECR_STATUS_RUNNING;
1896 else
1897 return 0;
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);
1903 static int spufs_event_mask_set(void *data, u64 val)
1905 struct spu_context *ctx = data;
1906 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1907 int ret;
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);
1915 return 0;
1918 static u64 spufs_event_mask_get(struct spu_context *ctx)
1920 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1921 return lscsa->event_mask.slot[0];
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);
1928 static u64 spufs_event_status_get(struct spu_context *ctx)
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;
1937 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1938 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1940 static int spufs_srr0_set(void *data, u64 val)
1942 struct spu_context *ctx = data;
1943 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1944 int ret;
1946 ret = spu_acquire_saved(ctx);
1947 if (ret)
1948 return ret;
1949 lscsa->srr0.slot[0] = (u32) val;
1950 spu_release_saved(ctx);
1952 return 0;
1955 static u64 spufs_srr0_get(struct spu_context *ctx)
1957 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1958 return lscsa->srr0.slot[0];
1960 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1961 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1963 static u64 spufs_id_get(struct spu_context *ctx)
1965 u64 num;
1967 if (ctx->state == SPU_STATE_RUNNABLE)
1968 num = ctx->spu->number;
1969 else
1970 num = (unsigned int)-1;
1972 return num;
1974 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1975 SPU_ATTR_ACQUIRE)
1977 static u64 spufs_object_id_get(struct spu_context *ctx)
1979 /* FIXME: Should there really be no locking here? */
1980 return ctx->object_id;
1983 static int spufs_object_id_set(void *data, u64 id)
1985 struct spu_context *ctx = data;
1986 ctx->object_id = id;
1988 return 0;
1991 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1992 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
1994 static u64 spufs_lslr_get(struct spu_context *ctx)
1996 return ctx->csa.priv2.spu_lslr_RW;
1998 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1999 SPU_ATTR_ACQUIRE_SAVED);
2001 static int spufs_info_open(struct inode *inode, struct file *file)
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;
2009 static int spufs_caps_show(struct seq_file *s, void *private)
2011 struct spu_context *ctx = s->private;
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;
2020 static int spufs_caps_open(struct inode *inode, struct file *file)
2022 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
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,
2032 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2033 char __user *buf, size_t len, loff_t *pos)
2035 u32 data;
2037 /* EOF if there's no entry in the mbox */
2038 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2039 return 0;
2041 data = ctx->csa.prob.pu_mb_R;
2043 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2046 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2047 size_t len, loff_t *pos)
2049 int ret;
2050 struct spu_context *ctx = file->private_data;
2052 if (!access_ok(VERIFY_WRITE, buf, len))
2053 return -EFAULT;
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);
2063 return ret;
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,
2072 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2073 char __user *buf, size_t len, loff_t *pos)
2075 u32 data;
2077 /* EOF if there's no entry in the ibox */
2078 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2079 return 0;
2081 data = ctx->csa.priv2.puint_mb_R;
2083 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2086 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2087 size_t len, loff_t *pos)
2089 struct spu_context *ctx = file->private_data;
2090 int ret;
2092 if (!access_ok(VERIFY_WRITE, buf, len))
2093 return -EFAULT;
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);
2103 return ret;
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,
2112 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2113 char __user *buf, size_t len, loff_t *pos)
2115 int i, cnt;
2116 u32 data[4];
2117 u32 wbox_stat;
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];
2125 return simple_read_from_buffer(buf, len, pos, &data,
2126 cnt * sizeof(u32));
2129 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2130 size_t len, loff_t *pos)
2132 struct spu_context *ctx = file->private_data;
2133 int ret;
2135 if (!access_ok(VERIFY_WRITE, buf, len))
2136 return -EFAULT;
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);
2146 return ret;
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,
2155 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2156 char __user *buf, size_t len, loff_t *pos)
2158 struct spu_dma_info info;
2159 struct mfc_cq_sr *qp, *spuqp;
2160 int i;
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];
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;
2177 return simple_read_from_buffer(buf, len, pos, &info,
2178 sizeof info);
2181 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2182 size_t len, loff_t *pos)
2184 struct spu_context *ctx = file->private_data;
2185 int ret;
2187 if (!access_ok(VERIFY_WRITE, buf, len))
2188 return -EFAULT;
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);
2198 return ret;
2201 static const struct file_operations spufs_dma_info_fops = {
2202 .open = spufs_info_open,
2203 .read = spufs_dma_info_read,
2206 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2207 char __user *buf, size_t len, loff_t *pos)
2209 struct spu_proxydma_info info;
2210 struct mfc_cq_sr *qp, *puqp;
2211 int ret = sizeof info;
2212 int i;
2214 if (len < ret)
2215 return -EINVAL;
2217 if (!access_ok(VERIFY_WRITE, buf, len))
2218 return -EFAULT;
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];
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;
2233 return simple_read_from_buffer(buf, len, pos, &info,
2234 sizeof info);
2237 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2238 size_t len, loff_t *pos)
2240 struct spu_context *ctx = file->private_data;
2241 int ret;
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);
2251 return ret;
2254 static const struct file_operations spufs_proxydma_info_fops = {
2255 .open = spufs_info_open,
2256 .read = spufs_proxydma_info_read,
2259 static int spufs_show_tid(struct seq_file *s, void *private)
2261 struct spu_context *ctx = s->private;
2263 seq_printf(s, "%d\n", ctx->tid);
2264 return 0;
2267 static int spufs_tid_open(struct inode *inode, struct file *file)
2269 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
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,
2279 static const char *ctx_state_names[] = {
2280 "user", "system", "iowait", "loaded"
2283 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2284 enum spu_utilization_state state)
2286 struct timespec ts;
2287 unsigned long long time = ctx->stats.times[state];
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.
2298 if (ctx->spu && ctx->stats.util_state == state) {
2299 ktime_get_ts(&ts);
2300 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2303 return time / NSEC_PER_MSEC;
2306 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2308 unsigned long long slb_flts = ctx->stats.slb_flt;
2310 if (ctx->state == SPU_STATE_RUNNABLE) {
2311 slb_flts += (ctx->spu->stats.slb_flt -
2312 ctx->stats.slb_flt_base);
2315 return slb_flts;
2318 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2320 unsigned long long class2_intrs = ctx->stats.class2_intr;
2322 if (ctx->state == SPU_STATE_RUNNABLE) {
2323 class2_intrs += (ctx->spu->stats.class2_intr -
2324 ctx->stats.class2_intr_base);
2327 return class2_intrs;
2331 static int spufs_show_stat(struct seq_file *s, void *private)
2333 struct spu_context *ctx = s->private;
2334 int ret;
2336 ret = spu_acquire(ctx);
2337 if (ret)
2338 return ret;
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;
2359 static int spufs_stat_open(struct inode *inode, struct file *file)
2361 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
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,
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, },
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, },
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 },