powerpc/spufs: avoid magic numbers for mapping sizes
[linux-2.6/mini2440.git] / arch / powerpc / platforms / cell / spufs / file.c
blobd0a497d9140b69a89e2057f12209f14ca4071c9a
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/time.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 int
242 spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
244 struct spu_context *ctx = vma->vm_file->private_data;
245 unsigned long address = (unsigned long)vmf->virtual_address;
246 unsigned long pfn, offset;
248 #ifdef CONFIG_SPU_FS_64K_LS
249 struct spu_state *csa = &ctx->csa;
250 int psize;
252 /* Check what page size we are using */
253 psize = get_slice_psize(vma->vm_mm, address);
255 /* Some sanity checking */
256 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
258 /* Wow, 64K, cool, we need to align the address though */
259 if (csa->use_big_pages) {
260 BUG_ON(vma->vm_start & 0xffff);
261 address &= ~0xfffful;
263 #endif /* CONFIG_SPU_FS_64K_LS */
265 offset = vmf->pgoff << PAGE_SHIFT;
266 if (offset >= LS_SIZE)
267 return VM_FAULT_SIGBUS;
269 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
270 address, offset);
272 if (spu_acquire(ctx))
273 return VM_FAULT_NOPAGE;
275 if (ctx->state == SPU_STATE_SAVED) {
276 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
277 & ~_PAGE_NO_CACHE);
278 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
279 } else {
280 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
281 | _PAGE_NO_CACHE);
282 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
284 vm_insert_pfn(vma, address, pfn);
286 spu_release(ctx);
288 return VM_FAULT_NOPAGE;
292 static struct vm_operations_struct spufs_mem_mmap_vmops = {
293 .fault = spufs_mem_mmap_fault,
296 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
298 #ifdef CONFIG_SPU_FS_64K_LS
299 struct spu_context *ctx = file->private_data;
300 struct spu_state *csa = &ctx->csa;
302 /* Sanity check VMA alignment */
303 if (csa->use_big_pages) {
304 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
305 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
306 vma->vm_pgoff);
307 if (vma->vm_start & 0xffff)
308 return -EINVAL;
309 if (vma->vm_pgoff & 0xf)
310 return -EINVAL;
312 #endif /* CONFIG_SPU_FS_64K_LS */
314 if (!(vma->vm_flags & VM_SHARED))
315 return -EINVAL;
317 vma->vm_flags |= VM_IO | VM_PFNMAP;
318 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
319 | _PAGE_NO_CACHE);
321 vma->vm_ops = &spufs_mem_mmap_vmops;
322 return 0;
325 #ifdef CONFIG_SPU_FS_64K_LS
326 static unsigned long spufs_get_unmapped_area(struct file *file,
327 unsigned long addr, unsigned long len, unsigned long pgoff,
328 unsigned long flags)
330 struct spu_context *ctx = file->private_data;
331 struct spu_state *csa = &ctx->csa;
333 /* If not using big pages, fallback to normal MM g_u_a */
334 if (!csa->use_big_pages)
335 return current->mm->get_unmapped_area(file, addr, len,
336 pgoff, flags);
338 /* Else, try to obtain a 64K pages slice */
339 return slice_get_unmapped_area(addr, len, flags,
340 MMU_PAGE_64K, 1, 0);
342 #endif /* CONFIG_SPU_FS_64K_LS */
344 static const struct file_operations spufs_mem_fops = {
345 .open = spufs_mem_open,
346 .release = spufs_mem_release,
347 .read = spufs_mem_read,
348 .write = spufs_mem_write,
349 .llseek = generic_file_llseek,
350 .mmap = spufs_mem_mmap,
351 #ifdef CONFIG_SPU_FS_64K_LS
352 .get_unmapped_area = spufs_get_unmapped_area,
353 #endif
356 static int spufs_ps_fault(struct vm_area_struct *vma,
357 struct vm_fault *vmf,
358 unsigned long ps_offs,
359 unsigned long ps_size)
361 struct spu_context *ctx = vma->vm_file->private_data;
362 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
363 int ret = 0;
365 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
367 if (offset >= ps_size)
368 return VM_FAULT_SIGBUS;
371 * Because we release the mmap_sem, the context may be destroyed while
372 * we're in spu_wait. Grab an extra reference so it isn't destroyed
373 * in the meantime.
375 get_spu_context(ctx);
378 * We have to wait for context to be loaded before we have
379 * pages to hand out to the user, but we don't want to wait
380 * with the mmap_sem held.
381 * It is possible to drop the mmap_sem here, but then we need
382 * to return VM_FAULT_NOPAGE because the mappings may have
383 * hanged.
385 if (spu_acquire(ctx))
386 goto refault;
388 if (ctx->state == SPU_STATE_SAVED) {
389 up_read(&current->mm->mmap_sem);
390 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
391 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
392 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
393 down_read(&current->mm->mmap_sem);
394 } else {
395 area = ctx->spu->problem_phys + ps_offs;
396 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
397 (area + offset) >> PAGE_SHIFT);
398 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
401 if (!ret)
402 spu_release(ctx);
404 refault:
405 put_spu_context(ctx);
406 return VM_FAULT_NOPAGE;
409 #if SPUFS_MMAP_4K
410 static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
411 struct vm_fault *vmf)
413 return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
416 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
417 .fault = spufs_cntl_mmap_fault,
421 * mmap support for problem state control area [0x4000 - 0x4fff].
423 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
425 if (!(vma->vm_flags & VM_SHARED))
426 return -EINVAL;
428 vma->vm_flags |= VM_IO | VM_PFNMAP;
429 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
430 | _PAGE_NO_CACHE | _PAGE_GUARDED);
432 vma->vm_ops = &spufs_cntl_mmap_vmops;
433 return 0;
435 #else /* SPUFS_MMAP_4K */
436 #define spufs_cntl_mmap NULL
437 #endif /* !SPUFS_MMAP_4K */
439 static int spufs_cntl_get(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 *val = ctx->ops->status_read(ctx);
448 spu_release(ctx);
450 return 0;
453 static int spufs_cntl_set(void *data, u64 val)
455 struct spu_context *ctx = data;
456 int ret;
458 ret = spu_acquire(ctx);
459 if (ret)
460 return ret;
461 ctx->ops->runcntl_write(ctx, val);
462 spu_release(ctx);
464 return 0;
467 static int spufs_cntl_open(struct inode *inode, struct file *file)
469 struct spufs_inode_info *i = SPUFS_I(inode);
470 struct spu_context *ctx = i->i_ctx;
472 mutex_lock(&ctx->mapping_lock);
473 file->private_data = ctx;
474 if (!i->i_openers++)
475 ctx->cntl = inode->i_mapping;
476 mutex_unlock(&ctx->mapping_lock);
477 return simple_attr_open(inode, file, spufs_cntl_get,
478 spufs_cntl_set, "0x%08lx");
481 static int
482 spufs_cntl_release(struct inode *inode, struct file *file)
484 struct spufs_inode_info *i = SPUFS_I(inode);
485 struct spu_context *ctx = i->i_ctx;
487 simple_attr_release(inode, file);
489 mutex_lock(&ctx->mapping_lock);
490 if (!--i->i_openers)
491 ctx->cntl = NULL;
492 mutex_unlock(&ctx->mapping_lock);
493 return 0;
496 static const struct file_operations spufs_cntl_fops = {
497 .open = spufs_cntl_open,
498 .release = spufs_cntl_release,
499 .read = simple_attr_read,
500 .write = simple_attr_write,
501 .mmap = spufs_cntl_mmap,
504 static int
505 spufs_regs_open(struct inode *inode, struct file *file)
507 struct spufs_inode_info *i = SPUFS_I(inode);
508 file->private_data = i->i_ctx;
509 return 0;
512 static ssize_t
513 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
514 size_t size, loff_t *pos)
516 struct spu_lscsa *lscsa = ctx->csa.lscsa;
517 return simple_read_from_buffer(buffer, size, pos,
518 lscsa->gprs, sizeof lscsa->gprs);
521 static ssize_t
522 spufs_regs_read(struct file *file, char __user *buffer,
523 size_t size, loff_t *pos)
525 int ret;
526 struct spu_context *ctx = file->private_data;
528 ret = spu_acquire_saved(ctx);
529 if (ret)
530 return ret;
531 ret = __spufs_regs_read(ctx, buffer, size, pos);
532 spu_release_saved(ctx);
533 return ret;
536 static ssize_t
537 spufs_regs_write(struct file *file, const char __user *buffer,
538 size_t size, loff_t *pos)
540 struct spu_context *ctx = file->private_data;
541 struct spu_lscsa *lscsa = ctx->csa.lscsa;
542 int ret;
544 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
545 if (size <= 0)
546 return -EFBIG;
547 *pos += size;
549 ret = spu_acquire_saved(ctx);
550 if (ret)
551 return ret;
553 ret = copy_from_user(lscsa->gprs + *pos - size,
554 buffer, size) ? -EFAULT : size;
556 spu_release_saved(ctx);
557 return ret;
560 static const struct file_operations spufs_regs_fops = {
561 .open = spufs_regs_open,
562 .read = spufs_regs_read,
563 .write = spufs_regs_write,
564 .llseek = generic_file_llseek,
567 static ssize_t
568 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
569 size_t size, loff_t * pos)
571 struct spu_lscsa *lscsa = ctx->csa.lscsa;
572 return simple_read_from_buffer(buffer, size, pos,
573 &lscsa->fpcr, sizeof(lscsa->fpcr));
576 static ssize_t
577 spufs_fpcr_read(struct file *file, char __user * buffer,
578 size_t size, loff_t * pos)
580 int ret;
581 struct spu_context *ctx = file->private_data;
583 ret = spu_acquire_saved(ctx);
584 if (ret)
585 return ret;
586 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
587 spu_release_saved(ctx);
588 return ret;
591 static ssize_t
592 spufs_fpcr_write(struct file *file, const char __user * buffer,
593 size_t size, loff_t * pos)
595 struct spu_context *ctx = file->private_data;
596 struct spu_lscsa *lscsa = ctx->csa.lscsa;
597 int ret;
599 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
600 if (size <= 0)
601 return -EFBIG;
603 ret = spu_acquire_saved(ctx);
604 if (ret)
605 return ret;
607 *pos += size;
608 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
609 buffer, size) ? -EFAULT : size;
611 spu_release_saved(ctx);
612 return ret;
615 static const struct file_operations spufs_fpcr_fops = {
616 .open = spufs_regs_open,
617 .read = spufs_fpcr_read,
618 .write = spufs_fpcr_write,
619 .llseek = generic_file_llseek,
622 /* generic open function for all pipe-like files */
623 static int spufs_pipe_open(struct inode *inode, struct file *file)
625 struct spufs_inode_info *i = SPUFS_I(inode);
626 file->private_data = i->i_ctx;
628 return nonseekable_open(inode, file);
632 * Read as many bytes from the mailbox as possible, until
633 * one of the conditions becomes true:
635 * - no more data available in the mailbox
636 * - end of the user provided buffer
637 * - end of the mapped area
639 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
640 size_t len, loff_t *pos)
642 struct spu_context *ctx = file->private_data;
643 u32 mbox_data, __user *udata;
644 ssize_t count;
646 if (len < 4)
647 return -EINVAL;
649 if (!access_ok(VERIFY_WRITE, buf, len))
650 return -EFAULT;
652 udata = (void __user *)buf;
654 count = spu_acquire(ctx);
655 if (count)
656 return count;
658 for (count = 0; (count + 4) <= len; count += 4, udata++) {
659 int ret;
660 ret = ctx->ops->mbox_read(ctx, &mbox_data);
661 if (ret == 0)
662 break;
665 * at the end of the mapped area, we can fault
666 * but still need to return the data we have
667 * read successfully so far.
669 ret = __put_user(mbox_data, udata);
670 if (ret) {
671 if (!count)
672 count = -EFAULT;
673 break;
676 spu_release(ctx);
678 if (!count)
679 count = -EAGAIN;
681 return count;
684 static const struct file_operations spufs_mbox_fops = {
685 .open = spufs_pipe_open,
686 .read = spufs_mbox_read,
689 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
690 size_t len, loff_t *pos)
692 struct spu_context *ctx = file->private_data;
693 ssize_t ret;
694 u32 mbox_stat;
696 if (len < 4)
697 return -EINVAL;
699 ret = spu_acquire(ctx);
700 if (ret)
701 return ret;
703 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
705 spu_release(ctx);
707 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
708 return -EFAULT;
710 return 4;
713 static const struct file_operations spufs_mbox_stat_fops = {
714 .open = spufs_pipe_open,
715 .read = spufs_mbox_stat_read,
718 /* low-level ibox access function */
719 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
721 return ctx->ops->ibox_read(ctx, data);
724 static int spufs_ibox_fasync(int fd, struct file *file, int on)
726 struct spu_context *ctx = file->private_data;
728 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
731 /* interrupt-level ibox callback function. */
732 void spufs_ibox_callback(struct spu *spu)
734 struct spu_context *ctx = spu->ctx;
736 if (!ctx)
737 return;
739 wake_up_all(&ctx->ibox_wq);
740 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
744 * Read as many bytes from the interrupt mailbox as possible, until
745 * one of the conditions becomes true:
747 * - no more data available in the mailbox
748 * - end of the user provided buffer
749 * - end of the mapped area
751 * If the file is opened without O_NONBLOCK, we wait here until
752 * any data is available, but return when we have been able to
753 * read something.
755 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
756 size_t len, loff_t *pos)
758 struct spu_context *ctx = file->private_data;
759 u32 ibox_data, __user *udata;
760 ssize_t count;
762 if (len < 4)
763 return -EINVAL;
765 if (!access_ok(VERIFY_WRITE, buf, len))
766 return -EFAULT;
768 udata = (void __user *)buf;
770 count = spu_acquire(ctx);
771 if (count)
772 goto out;
774 /* wait only for the first element */
775 count = 0;
776 if (file->f_flags & O_NONBLOCK) {
777 if (!spu_ibox_read(ctx, &ibox_data)) {
778 count = -EAGAIN;
779 goto out_unlock;
781 } else {
782 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
783 if (count)
784 goto out;
787 /* if we can't write at all, return -EFAULT */
788 count = __put_user(ibox_data, udata);
789 if (count)
790 goto out_unlock;
792 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
793 int ret;
794 ret = ctx->ops->ibox_read(ctx, &ibox_data);
795 if (ret == 0)
796 break;
798 * at the end of the mapped area, we can fault
799 * but still need to return the data we have
800 * read successfully so far.
802 ret = __put_user(ibox_data, udata);
803 if (ret)
804 break;
807 out_unlock:
808 spu_release(ctx);
809 out:
810 return count;
813 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
815 struct spu_context *ctx = file->private_data;
816 unsigned int mask;
818 poll_wait(file, &ctx->ibox_wq, wait);
821 * For now keep this uninterruptible and also ignore the rule
822 * that poll should not sleep. Will be fixed later.
824 mutex_lock(&ctx->state_mutex);
825 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
826 spu_release(ctx);
828 return mask;
831 static const struct file_operations spufs_ibox_fops = {
832 .open = spufs_pipe_open,
833 .read = spufs_ibox_read,
834 .poll = spufs_ibox_poll,
835 .fasync = spufs_ibox_fasync,
838 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
839 size_t len, loff_t *pos)
841 struct spu_context *ctx = file->private_data;
842 ssize_t ret;
843 u32 ibox_stat;
845 if (len < 4)
846 return -EINVAL;
848 ret = spu_acquire(ctx);
849 if (ret)
850 return ret;
851 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
852 spu_release(ctx);
854 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
855 return -EFAULT;
857 return 4;
860 static const struct file_operations spufs_ibox_stat_fops = {
861 .open = spufs_pipe_open,
862 .read = spufs_ibox_stat_read,
865 /* low-level mailbox write */
866 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
868 return ctx->ops->wbox_write(ctx, data);
871 static int spufs_wbox_fasync(int fd, struct file *file, int on)
873 struct spu_context *ctx = file->private_data;
874 int ret;
876 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
878 return ret;
881 /* interrupt-level wbox callback function. */
882 void spufs_wbox_callback(struct spu *spu)
884 struct spu_context *ctx = spu->ctx;
886 if (!ctx)
887 return;
889 wake_up_all(&ctx->wbox_wq);
890 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
894 * Write as many bytes to the interrupt mailbox as possible, until
895 * one of the conditions becomes true:
897 * - the mailbox is full
898 * - end of the user provided buffer
899 * - end of the mapped area
901 * If the file is opened without O_NONBLOCK, we wait here until
902 * space is availabyl, but return when we have been able to
903 * write something.
905 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
906 size_t len, loff_t *pos)
908 struct spu_context *ctx = file->private_data;
909 u32 wbox_data, __user *udata;
910 ssize_t count;
912 if (len < 4)
913 return -EINVAL;
915 udata = (void __user *)buf;
916 if (!access_ok(VERIFY_READ, buf, len))
917 return -EFAULT;
919 if (__get_user(wbox_data, udata))
920 return -EFAULT;
922 count = spu_acquire(ctx);
923 if (count)
924 goto out;
927 * make sure we can at least write one element, by waiting
928 * in case of !O_NONBLOCK
930 count = 0;
931 if (file->f_flags & O_NONBLOCK) {
932 if (!spu_wbox_write(ctx, wbox_data)) {
933 count = -EAGAIN;
934 goto out_unlock;
936 } else {
937 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
938 if (count)
939 goto out;
943 /* write as much as possible */
944 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
945 int ret;
946 ret = __get_user(wbox_data, udata);
947 if (ret)
948 break;
950 ret = spu_wbox_write(ctx, wbox_data);
951 if (ret == 0)
952 break;
955 out_unlock:
956 spu_release(ctx);
957 out:
958 return count;
961 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
963 struct spu_context *ctx = file->private_data;
964 unsigned int mask;
966 poll_wait(file, &ctx->wbox_wq, wait);
969 * For now keep this uninterruptible and also ignore the rule
970 * that poll should not sleep. Will be fixed later.
972 mutex_lock(&ctx->state_mutex);
973 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
974 spu_release(ctx);
976 return mask;
979 static const struct file_operations spufs_wbox_fops = {
980 .open = spufs_pipe_open,
981 .write = spufs_wbox_write,
982 .poll = spufs_wbox_poll,
983 .fasync = spufs_wbox_fasync,
986 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
987 size_t len, loff_t *pos)
989 struct spu_context *ctx = file->private_data;
990 ssize_t ret;
991 u32 wbox_stat;
993 if (len < 4)
994 return -EINVAL;
996 ret = spu_acquire(ctx);
997 if (ret)
998 return ret;
999 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
1000 spu_release(ctx);
1002 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
1003 return -EFAULT;
1005 return 4;
1008 static const struct file_operations spufs_wbox_stat_fops = {
1009 .open = spufs_pipe_open,
1010 .read = spufs_wbox_stat_read,
1013 static int spufs_signal1_open(struct inode *inode, struct file *file)
1015 struct spufs_inode_info *i = SPUFS_I(inode);
1016 struct spu_context *ctx = i->i_ctx;
1018 mutex_lock(&ctx->mapping_lock);
1019 file->private_data = ctx;
1020 if (!i->i_openers++)
1021 ctx->signal1 = inode->i_mapping;
1022 mutex_unlock(&ctx->mapping_lock);
1023 return nonseekable_open(inode, file);
1026 static int
1027 spufs_signal1_release(struct inode *inode, struct file *file)
1029 struct spufs_inode_info *i = SPUFS_I(inode);
1030 struct spu_context *ctx = i->i_ctx;
1032 mutex_lock(&ctx->mapping_lock);
1033 if (!--i->i_openers)
1034 ctx->signal1 = NULL;
1035 mutex_unlock(&ctx->mapping_lock);
1036 return 0;
1039 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
1040 size_t len, loff_t *pos)
1042 int ret = 0;
1043 u32 data;
1045 if (len < 4)
1046 return -EINVAL;
1048 if (ctx->csa.spu_chnlcnt_RW[3]) {
1049 data = ctx->csa.spu_chnldata_RW[3];
1050 ret = 4;
1053 if (!ret)
1054 goto out;
1056 if (copy_to_user(buf, &data, 4))
1057 return -EFAULT;
1059 out:
1060 return ret;
1063 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1064 size_t len, loff_t *pos)
1066 int ret;
1067 struct spu_context *ctx = file->private_data;
1069 ret = spu_acquire_saved(ctx);
1070 if (ret)
1071 return ret;
1072 ret = __spufs_signal1_read(ctx, buf, len, pos);
1073 spu_release_saved(ctx);
1075 return ret;
1078 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1079 size_t len, loff_t *pos)
1081 struct spu_context *ctx;
1082 ssize_t ret;
1083 u32 data;
1085 ctx = file->private_data;
1087 if (len < 4)
1088 return -EINVAL;
1090 if (copy_from_user(&data, buf, 4))
1091 return -EFAULT;
1093 ret = spu_acquire(ctx);
1094 if (ret)
1095 return ret;
1096 ctx->ops->signal1_write(ctx, data);
1097 spu_release(ctx);
1099 return 4;
1102 static int
1103 spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1105 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1106 return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1107 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
1108 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1109 * signal 1 and 2 area
1111 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
1112 #else
1113 #error unsupported page size
1114 #endif
1117 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
1118 .fault = spufs_signal1_mmap_fault,
1121 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1123 if (!(vma->vm_flags & VM_SHARED))
1124 return -EINVAL;
1126 vma->vm_flags |= VM_IO | VM_PFNMAP;
1127 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1128 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1130 vma->vm_ops = &spufs_signal1_mmap_vmops;
1131 return 0;
1134 static const struct file_operations spufs_signal1_fops = {
1135 .open = spufs_signal1_open,
1136 .release = spufs_signal1_release,
1137 .read = spufs_signal1_read,
1138 .write = spufs_signal1_write,
1139 .mmap = spufs_signal1_mmap,
1142 static const struct file_operations spufs_signal1_nosched_fops = {
1143 .open = spufs_signal1_open,
1144 .release = spufs_signal1_release,
1145 .write = spufs_signal1_write,
1146 .mmap = spufs_signal1_mmap,
1149 static int spufs_signal2_open(struct inode *inode, struct file *file)
1151 struct spufs_inode_info *i = SPUFS_I(inode);
1152 struct spu_context *ctx = i->i_ctx;
1154 mutex_lock(&ctx->mapping_lock);
1155 file->private_data = ctx;
1156 if (!i->i_openers++)
1157 ctx->signal2 = inode->i_mapping;
1158 mutex_unlock(&ctx->mapping_lock);
1159 return nonseekable_open(inode, file);
1162 static int
1163 spufs_signal2_release(struct inode *inode, struct file *file)
1165 struct spufs_inode_info *i = SPUFS_I(inode);
1166 struct spu_context *ctx = i->i_ctx;
1168 mutex_lock(&ctx->mapping_lock);
1169 if (!--i->i_openers)
1170 ctx->signal2 = NULL;
1171 mutex_unlock(&ctx->mapping_lock);
1172 return 0;
1175 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
1176 size_t len, loff_t *pos)
1178 int ret = 0;
1179 u32 data;
1181 if (len < 4)
1182 return -EINVAL;
1184 if (ctx->csa.spu_chnlcnt_RW[4]) {
1185 data = ctx->csa.spu_chnldata_RW[4];
1186 ret = 4;
1189 if (!ret)
1190 goto out;
1192 if (copy_to_user(buf, &data, 4))
1193 return -EFAULT;
1195 out:
1196 return ret;
1199 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1200 size_t len, loff_t *pos)
1202 struct spu_context *ctx = file->private_data;
1203 int ret;
1205 ret = spu_acquire_saved(ctx);
1206 if (ret)
1207 return ret;
1208 ret = __spufs_signal2_read(ctx, buf, len, pos);
1209 spu_release_saved(ctx);
1211 return ret;
1214 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1215 size_t len, loff_t *pos)
1217 struct spu_context *ctx;
1218 ssize_t ret;
1219 u32 data;
1221 ctx = file->private_data;
1223 if (len < 4)
1224 return -EINVAL;
1226 if (copy_from_user(&data, buf, 4))
1227 return -EFAULT;
1229 ret = spu_acquire(ctx);
1230 if (ret)
1231 return ret;
1232 ctx->ops->signal2_write(ctx, data);
1233 spu_release(ctx);
1235 return 4;
1238 #if SPUFS_MMAP_4K
1239 static int
1240 spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1242 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1243 return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1244 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
1245 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1246 * signal 1 and 2 area
1248 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
1249 #else
1250 #error unsupported page size
1251 #endif
1254 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1255 .fault = spufs_signal2_mmap_fault,
1258 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1260 if (!(vma->vm_flags & VM_SHARED))
1261 return -EINVAL;
1263 vma->vm_flags |= VM_IO | VM_PFNMAP;
1264 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1265 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1267 vma->vm_ops = &spufs_signal2_mmap_vmops;
1268 return 0;
1270 #else /* SPUFS_MMAP_4K */
1271 #define spufs_signal2_mmap NULL
1272 #endif /* !SPUFS_MMAP_4K */
1274 static const struct file_operations spufs_signal2_fops = {
1275 .open = spufs_signal2_open,
1276 .release = spufs_signal2_release,
1277 .read = spufs_signal2_read,
1278 .write = spufs_signal2_write,
1279 .mmap = spufs_signal2_mmap,
1282 static const struct file_operations spufs_signal2_nosched_fops = {
1283 .open = spufs_signal2_open,
1284 .release = spufs_signal2_release,
1285 .write = spufs_signal2_write,
1286 .mmap = spufs_signal2_mmap,
1290 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1291 * work of acquiring (or not) the SPU context before calling through
1292 * to the actual get routine. The set routine is called directly.
1294 #define SPU_ATTR_NOACQUIRE 0
1295 #define SPU_ATTR_ACQUIRE 1
1296 #define SPU_ATTR_ACQUIRE_SAVED 2
1298 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
1299 static int __##__get(void *data, u64 *val) \
1301 struct spu_context *ctx = data; \
1302 int ret = 0; \
1304 if (__acquire == SPU_ATTR_ACQUIRE) { \
1305 ret = spu_acquire(ctx); \
1306 if (ret) \
1307 return ret; \
1308 *val = __get(ctx); \
1309 spu_release(ctx); \
1310 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1311 ret = spu_acquire_saved(ctx); \
1312 if (ret) \
1313 return ret; \
1314 *val = __get(ctx); \
1315 spu_release_saved(ctx); \
1316 } else \
1317 *val = __get(ctx); \
1319 return 0; \
1321 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1323 static int spufs_signal1_type_set(void *data, u64 val)
1325 struct spu_context *ctx = data;
1326 int ret;
1328 ret = spu_acquire(ctx);
1329 if (ret)
1330 return ret;
1331 ctx->ops->signal1_type_set(ctx, val);
1332 spu_release(ctx);
1334 return 0;
1337 static u64 spufs_signal1_type_get(struct spu_context *ctx)
1339 return ctx->ops->signal1_type_get(ctx);
1341 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1342 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1345 static int spufs_signal2_type_set(void *data, u64 val)
1347 struct spu_context *ctx = data;
1348 int ret;
1350 ret = spu_acquire(ctx);
1351 if (ret)
1352 return ret;
1353 ctx->ops->signal2_type_set(ctx, val);
1354 spu_release(ctx);
1356 return 0;
1359 static u64 spufs_signal2_type_get(struct spu_context *ctx)
1361 return ctx->ops->signal2_type_get(ctx);
1363 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1364 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1366 #if SPUFS_MMAP_4K
1367 static int
1368 spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1370 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
1373 static struct vm_operations_struct spufs_mss_mmap_vmops = {
1374 .fault = spufs_mss_mmap_fault,
1378 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1380 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1382 if (!(vma->vm_flags & VM_SHARED))
1383 return -EINVAL;
1385 vma->vm_flags |= VM_IO | VM_PFNMAP;
1386 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1387 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1389 vma->vm_ops = &spufs_mss_mmap_vmops;
1390 return 0;
1392 #else /* SPUFS_MMAP_4K */
1393 #define spufs_mss_mmap NULL
1394 #endif /* !SPUFS_MMAP_4K */
1396 static int spufs_mss_open(struct inode *inode, struct file *file)
1398 struct spufs_inode_info *i = SPUFS_I(inode);
1399 struct spu_context *ctx = i->i_ctx;
1401 file->private_data = i->i_ctx;
1403 mutex_lock(&ctx->mapping_lock);
1404 if (!i->i_openers++)
1405 ctx->mss = inode->i_mapping;
1406 mutex_unlock(&ctx->mapping_lock);
1407 return nonseekable_open(inode, file);
1410 static int
1411 spufs_mss_release(struct inode *inode, struct file *file)
1413 struct spufs_inode_info *i = SPUFS_I(inode);
1414 struct spu_context *ctx = i->i_ctx;
1416 mutex_lock(&ctx->mapping_lock);
1417 if (!--i->i_openers)
1418 ctx->mss = NULL;
1419 mutex_unlock(&ctx->mapping_lock);
1420 return 0;
1423 static const struct file_operations spufs_mss_fops = {
1424 .open = spufs_mss_open,
1425 .release = spufs_mss_release,
1426 .mmap = spufs_mss_mmap,
1429 static int
1430 spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1432 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
1435 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1436 .fault = spufs_psmap_mmap_fault,
1440 * mmap support for full problem state area [0x00000 - 0x1ffff].
1442 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1444 if (!(vma->vm_flags & VM_SHARED))
1445 return -EINVAL;
1447 vma->vm_flags |= VM_IO | VM_PFNMAP;
1448 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1449 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1451 vma->vm_ops = &spufs_psmap_mmap_vmops;
1452 return 0;
1455 static int spufs_psmap_open(struct inode *inode, struct file *file)
1457 struct spufs_inode_info *i = SPUFS_I(inode);
1458 struct spu_context *ctx = i->i_ctx;
1460 mutex_lock(&ctx->mapping_lock);
1461 file->private_data = i->i_ctx;
1462 if (!i->i_openers++)
1463 ctx->psmap = inode->i_mapping;
1464 mutex_unlock(&ctx->mapping_lock);
1465 return nonseekable_open(inode, file);
1468 static int
1469 spufs_psmap_release(struct inode *inode, struct file *file)
1471 struct spufs_inode_info *i = SPUFS_I(inode);
1472 struct spu_context *ctx = i->i_ctx;
1474 mutex_lock(&ctx->mapping_lock);
1475 if (!--i->i_openers)
1476 ctx->psmap = NULL;
1477 mutex_unlock(&ctx->mapping_lock);
1478 return 0;
1481 static const struct file_operations spufs_psmap_fops = {
1482 .open = spufs_psmap_open,
1483 .release = spufs_psmap_release,
1484 .mmap = spufs_psmap_mmap,
1488 #if SPUFS_MMAP_4K
1489 static int
1490 spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1492 return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
1495 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1496 .fault = spufs_mfc_mmap_fault,
1500 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1502 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1504 if (!(vma->vm_flags & VM_SHARED))
1505 return -EINVAL;
1507 vma->vm_flags |= VM_IO | VM_PFNMAP;
1508 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1509 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1511 vma->vm_ops = &spufs_mfc_mmap_vmops;
1512 return 0;
1514 #else /* SPUFS_MMAP_4K */
1515 #define spufs_mfc_mmap NULL
1516 #endif /* !SPUFS_MMAP_4K */
1518 static int spufs_mfc_open(struct inode *inode, struct file *file)
1520 struct spufs_inode_info *i = SPUFS_I(inode);
1521 struct spu_context *ctx = i->i_ctx;
1523 /* we don't want to deal with DMA into other processes */
1524 if (ctx->owner != current->mm)
1525 return -EINVAL;
1527 if (atomic_read(&inode->i_count) != 1)
1528 return -EBUSY;
1530 mutex_lock(&ctx->mapping_lock);
1531 file->private_data = ctx;
1532 if (!i->i_openers++)
1533 ctx->mfc = inode->i_mapping;
1534 mutex_unlock(&ctx->mapping_lock);
1535 return nonseekable_open(inode, file);
1538 static int
1539 spufs_mfc_release(struct inode *inode, struct file *file)
1541 struct spufs_inode_info *i = SPUFS_I(inode);
1542 struct spu_context *ctx = i->i_ctx;
1544 mutex_lock(&ctx->mapping_lock);
1545 if (!--i->i_openers)
1546 ctx->mfc = NULL;
1547 mutex_unlock(&ctx->mapping_lock);
1548 return 0;
1551 /* interrupt-level mfc callback function. */
1552 void spufs_mfc_callback(struct spu *spu)
1554 struct spu_context *ctx = spu->ctx;
1556 if (!ctx)
1557 return;
1559 wake_up_all(&ctx->mfc_wq);
1561 pr_debug("%s %s\n", __func__, spu->name);
1562 if (ctx->mfc_fasync) {
1563 u32 free_elements, tagstatus;
1564 unsigned int mask;
1566 /* no need for spu_acquire in interrupt context */
1567 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1568 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1570 mask = 0;
1571 if (free_elements & 0xffff)
1572 mask |= POLLOUT;
1573 if (tagstatus & ctx->tagwait)
1574 mask |= POLLIN;
1576 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1580 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1582 /* See if there is one tag group is complete */
1583 /* FIXME we need locking around tagwait */
1584 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1585 ctx->tagwait &= ~*status;
1586 if (*status)
1587 return 1;
1589 /* enable interrupt waiting for any tag group,
1590 may silently fail if interrupts are already enabled */
1591 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1592 return 0;
1595 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1596 size_t size, loff_t *pos)
1598 struct spu_context *ctx = file->private_data;
1599 int ret = -EINVAL;
1600 u32 status;
1602 if (size != 4)
1603 goto out;
1605 ret = spu_acquire(ctx);
1606 if (ret)
1607 return ret;
1609 ret = -EINVAL;
1610 if (file->f_flags & O_NONBLOCK) {
1611 status = ctx->ops->read_mfc_tagstatus(ctx);
1612 if (!(status & ctx->tagwait))
1613 ret = -EAGAIN;
1614 else
1615 /* XXX(hch): shouldn't we clear ret here? */
1616 ctx->tagwait &= ~status;
1617 } else {
1618 ret = spufs_wait(ctx->mfc_wq,
1619 spufs_read_mfc_tagstatus(ctx, &status));
1620 if (ret)
1621 goto out;
1623 spu_release(ctx);
1625 ret = 4;
1626 if (copy_to_user(buffer, &status, 4))
1627 ret = -EFAULT;
1629 out:
1630 return ret;
1633 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1635 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1636 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1638 switch (cmd->cmd) {
1639 case MFC_PUT_CMD:
1640 case MFC_PUTF_CMD:
1641 case MFC_PUTB_CMD:
1642 case MFC_GET_CMD:
1643 case MFC_GETF_CMD:
1644 case MFC_GETB_CMD:
1645 break;
1646 default:
1647 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1648 return -EIO;
1651 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1652 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1653 cmd->ea, cmd->lsa);
1654 return -EIO;
1657 switch (cmd->size & 0xf) {
1658 case 1:
1659 break;
1660 case 2:
1661 if (cmd->lsa & 1)
1662 goto error;
1663 break;
1664 case 4:
1665 if (cmd->lsa & 3)
1666 goto error;
1667 break;
1668 case 8:
1669 if (cmd->lsa & 7)
1670 goto error;
1671 break;
1672 case 0:
1673 if (cmd->lsa & 15)
1674 goto error;
1675 break;
1676 error:
1677 default:
1678 pr_debug("invalid DMA alignment %x for size %x\n",
1679 cmd->lsa & 0xf, cmd->size);
1680 return -EIO;
1683 if (cmd->size > 16 * 1024) {
1684 pr_debug("invalid DMA size %x\n", cmd->size);
1685 return -EIO;
1688 if (cmd->tag & 0xfff0) {
1689 /* we reserve the higher tag numbers for kernel use */
1690 pr_debug("invalid DMA tag\n");
1691 return -EIO;
1694 if (cmd->class) {
1695 /* not supported in this version */
1696 pr_debug("invalid DMA class\n");
1697 return -EIO;
1700 return 0;
1703 static int spu_send_mfc_command(struct spu_context *ctx,
1704 struct mfc_dma_command cmd,
1705 int *error)
1707 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1708 if (*error == -EAGAIN) {
1709 /* wait for any tag group to complete
1710 so we have space for the new command */
1711 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1712 /* try again, because the queue might be
1713 empty again */
1714 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1715 if (*error == -EAGAIN)
1716 return 0;
1718 return 1;
1721 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1722 size_t size, loff_t *pos)
1724 struct spu_context *ctx = file->private_data;
1725 struct mfc_dma_command cmd;
1726 int ret = -EINVAL;
1728 if (size != sizeof cmd)
1729 goto out;
1731 ret = -EFAULT;
1732 if (copy_from_user(&cmd, buffer, sizeof cmd))
1733 goto out;
1735 ret = spufs_check_valid_dma(&cmd);
1736 if (ret)
1737 goto out;
1739 ret = spu_acquire(ctx);
1740 if (ret)
1741 goto out;
1743 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1744 if (ret)
1745 goto out;
1747 if (file->f_flags & O_NONBLOCK) {
1748 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1749 } else {
1750 int status;
1751 ret = spufs_wait(ctx->mfc_wq,
1752 spu_send_mfc_command(ctx, cmd, &status));
1753 if (ret)
1754 goto out;
1755 if (status)
1756 ret = status;
1759 if (ret)
1760 goto out_unlock;
1762 ctx->tagwait |= 1 << cmd.tag;
1763 ret = size;
1765 out_unlock:
1766 spu_release(ctx);
1767 out:
1768 return ret;
1771 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1773 struct spu_context *ctx = file->private_data;
1774 u32 free_elements, tagstatus;
1775 unsigned int mask;
1777 poll_wait(file, &ctx->mfc_wq, wait);
1780 * For now keep this uninterruptible and also ignore the rule
1781 * that poll should not sleep. Will be fixed later.
1783 mutex_lock(&ctx->state_mutex);
1784 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1785 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1786 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1787 spu_release(ctx);
1789 mask = 0;
1790 if (free_elements & 0xffff)
1791 mask |= POLLOUT | POLLWRNORM;
1792 if (tagstatus & ctx->tagwait)
1793 mask |= POLLIN | POLLRDNORM;
1795 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
1796 free_elements, tagstatus, ctx->tagwait);
1798 return mask;
1801 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1803 struct spu_context *ctx = file->private_data;
1804 int ret;
1806 ret = spu_acquire(ctx);
1807 if (ret)
1808 goto out;
1809 #if 0
1810 /* this currently hangs */
1811 ret = spufs_wait(ctx->mfc_wq,
1812 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1813 if (ret)
1814 goto out;
1815 ret = spufs_wait(ctx->mfc_wq,
1816 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1817 if (ret)
1818 goto out;
1819 #else
1820 ret = 0;
1821 #endif
1822 spu_release(ctx);
1823 out:
1824 return ret;
1827 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1828 int datasync)
1830 return spufs_mfc_flush(file, NULL);
1833 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1835 struct spu_context *ctx = file->private_data;
1837 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1840 static const struct file_operations spufs_mfc_fops = {
1841 .open = spufs_mfc_open,
1842 .release = spufs_mfc_release,
1843 .read = spufs_mfc_read,
1844 .write = spufs_mfc_write,
1845 .poll = spufs_mfc_poll,
1846 .flush = spufs_mfc_flush,
1847 .fsync = spufs_mfc_fsync,
1848 .fasync = spufs_mfc_fasync,
1849 .mmap = spufs_mfc_mmap,
1852 static int spufs_npc_set(void *data, u64 val)
1854 struct spu_context *ctx = data;
1855 int ret;
1857 ret = spu_acquire(ctx);
1858 if (ret)
1859 return ret;
1860 ctx->ops->npc_write(ctx, val);
1861 spu_release(ctx);
1863 return 0;
1866 static u64 spufs_npc_get(struct spu_context *ctx)
1868 return ctx->ops->npc_read(ctx);
1870 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1871 "0x%llx\n", SPU_ATTR_ACQUIRE);
1873 static int spufs_decr_set(void *data, u64 val)
1875 struct spu_context *ctx = data;
1876 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1877 int ret;
1879 ret = spu_acquire_saved(ctx);
1880 if (ret)
1881 return ret;
1882 lscsa->decr.slot[0] = (u32) val;
1883 spu_release_saved(ctx);
1885 return 0;
1888 static u64 spufs_decr_get(struct spu_context *ctx)
1890 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1891 return lscsa->decr.slot[0];
1893 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1894 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
1896 static int spufs_decr_status_set(void *data, u64 val)
1898 struct spu_context *ctx = data;
1899 int ret;
1901 ret = spu_acquire_saved(ctx);
1902 if (ret)
1903 return ret;
1904 if (val)
1905 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1906 else
1907 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1908 spu_release_saved(ctx);
1910 return 0;
1913 static u64 spufs_decr_status_get(struct spu_context *ctx)
1915 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1916 return SPU_DECR_STATUS_RUNNING;
1917 else
1918 return 0;
1920 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1921 spufs_decr_status_set, "0x%llx\n",
1922 SPU_ATTR_ACQUIRE_SAVED);
1924 static int spufs_event_mask_set(void *data, u64 val)
1926 struct spu_context *ctx = data;
1927 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1928 int ret;
1930 ret = spu_acquire_saved(ctx);
1931 if (ret)
1932 return ret;
1933 lscsa->event_mask.slot[0] = (u32) val;
1934 spu_release_saved(ctx);
1936 return 0;
1939 static u64 spufs_event_mask_get(struct spu_context *ctx)
1941 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1942 return lscsa->event_mask.slot[0];
1945 DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1946 spufs_event_mask_set, "0x%llx\n",
1947 SPU_ATTR_ACQUIRE_SAVED);
1949 static u64 spufs_event_status_get(struct spu_context *ctx)
1951 struct spu_state *state = &ctx->csa;
1952 u64 stat;
1953 stat = state->spu_chnlcnt_RW[0];
1954 if (stat)
1955 return state->spu_chnldata_RW[0];
1956 return 0;
1958 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1959 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1961 static int spufs_srr0_set(void *data, u64 val)
1963 struct spu_context *ctx = data;
1964 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1965 int ret;
1967 ret = spu_acquire_saved(ctx);
1968 if (ret)
1969 return ret;
1970 lscsa->srr0.slot[0] = (u32) val;
1971 spu_release_saved(ctx);
1973 return 0;
1976 static u64 spufs_srr0_get(struct spu_context *ctx)
1978 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1979 return lscsa->srr0.slot[0];
1981 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1982 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1984 static u64 spufs_id_get(struct spu_context *ctx)
1986 u64 num;
1988 if (ctx->state == SPU_STATE_RUNNABLE)
1989 num = ctx->spu->number;
1990 else
1991 num = (unsigned int)-1;
1993 return num;
1995 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1996 SPU_ATTR_ACQUIRE)
1998 static u64 spufs_object_id_get(struct spu_context *ctx)
2000 /* FIXME: Should there really be no locking here? */
2001 return ctx->object_id;
2004 static int spufs_object_id_set(void *data, u64 id)
2006 struct spu_context *ctx = data;
2007 ctx->object_id = id;
2009 return 0;
2012 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2013 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
2015 static u64 spufs_lslr_get(struct spu_context *ctx)
2017 return ctx->csa.priv2.spu_lslr_RW;
2019 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2020 SPU_ATTR_ACQUIRE_SAVED);
2022 static int spufs_info_open(struct inode *inode, struct file *file)
2024 struct spufs_inode_info *i = SPUFS_I(inode);
2025 struct spu_context *ctx = i->i_ctx;
2026 file->private_data = ctx;
2027 return 0;
2030 static int spufs_caps_show(struct seq_file *s, void *private)
2032 struct spu_context *ctx = s->private;
2034 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2035 seq_puts(s, "sched\n");
2036 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2037 seq_puts(s, "step\n");
2038 return 0;
2041 static int spufs_caps_open(struct inode *inode, struct file *file)
2043 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2046 static const struct file_operations spufs_caps_fops = {
2047 .open = spufs_caps_open,
2048 .read = seq_read,
2049 .llseek = seq_lseek,
2050 .release = single_release,
2053 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2054 char __user *buf, size_t len, loff_t *pos)
2056 u32 data;
2058 /* EOF if there's no entry in the mbox */
2059 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2060 return 0;
2062 data = ctx->csa.prob.pu_mb_R;
2064 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2067 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2068 size_t len, loff_t *pos)
2070 int ret;
2071 struct spu_context *ctx = file->private_data;
2073 if (!access_ok(VERIFY_WRITE, buf, len))
2074 return -EFAULT;
2076 ret = spu_acquire_saved(ctx);
2077 if (ret)
2078 return ret;
2079 spin_lock(&ctx->csa.register_lock);
2080 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
2081 spin_unlock(&ctx->csa.register_lock);
2082 spu_release_saved(ctx);
2084 return ret;
2087 static const struct file_operations spufs_mbox_info_fops = {
2088 .open = spufs_info_open,
2089 .read = spufs_mbox_info_read,
2090 .llseek = generic_file_llseek,
2093 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2094 char __user *buf, size_t len, loff_t *pos)
2096 u32 data;
2098 /* EOF if there's no entry in the ibox */
2099 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2100 return 0;
2102 data = ctx->csa.priv2.puint_mb_R;
2104 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2107 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2108 size_t len, loff_t *pos)
2110 struct spu_context *ctx = file->private_data;
2111 int ret;
2113 if (!access_ok(VERIFY_WRITE, buf, len))
2114 return -EFAULT;
2116 ret = spu_acquire_saved(ctx);
2117 if (ret)
2118 return ret;
2119 spin_lock(&ctx->csa.register_lock);
2120 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
2121 spin_unlock(&ctx->csa.register_lock);
2122 spu_release_saved(ctx);
2124 return ret;
2127 static const struct file_operations spufs_ibox_info_fops = {
2128 .open = spufs_info_open,
2129 .read = spufs_ibox_info_read,
2130 .llseek = generic_file_llseek,
2133 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2134 char __user *buf, size_t len, loff_t *pos)
2136 int i, cnt;
2137 u32 data[4];
2138 u32 wbox_stat;
2140 wbox_stat = ctx->csa.prob.mb_stat_R;
2141 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2142 for (i = 0; i < cnt; i++) {
2143 data[i] = ctx->csa.spu_mailbox_data[i];
2146 return simple_read_from_buffer(buf, len, pos, &data,
2147 cnt * sizeof(u32));
2150 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2151 size_t len, loff_t *pos)
2153 struct spu_context *ctx = file->private_data;
2154 int ret;
2156 if (!access_ok(VERIFY_WRITE, buf, len))
2157 return -EFAULT;
2159 ret = spu_acquire_saved(ctx);
2160 if (ret)
2161 return ret;
2162 spin_lock(&ctx->csa.register_lock);
2163 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
2164 spin_unlock(&ctx->csa.register_lock);
2165 spu_release_saved(ctx);
2167 return ret;
2170 static const struct file_operations spufs_wbox_info_fops = {
2171 .open = spufs_info_open,
2172 .read = spufs_wbox_info_read,
2173 .llseek = generic_file_llseek,
2176 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2177 char __user *buf, size_t len, loff_t *pos)
2179 struct spu_dma_info info;
2180 struct mfc_cq_sr *qp, *spuqp;
2181 int i;
2183 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2184 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2185 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2186 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2187 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2188 for (i = 0; i < 16; i++) {
2189 qp = &info.dma_info_command_data[i];
2190 spuqp = &ctx->csa.priv2.spuq[i];
2192 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2193 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2194 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2195 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2198 return simple_read_from_buffer(buf, len, pos, &info,
2199 sizeof info);
2202 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2203 size_t len, loff_t *pos)
2205 struct spu_context *ctx = file->private_data;
2206 int ret;
2208 if (!access_ok(VERIFY_WRITE, buf, len))
2209 return -EFAULT;
2211 ret = spu_acquire_saved(ctx);
2212 if (ret)
2213 return ret;
2214 spin_lock(&ctx->csa.register_lock);
2215 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2216 spin_unlock(&ctx->csa.register_lock);
2217 spu_release_saved(ctx);
2219 return ret;
2222 static const struct file_operations spufs_dma_info_fops = {
2223 .open = spufs_info_open,
2224 .read = spufs_dma_info_read,
2227 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2228 char __user *buf, size_t len, loff_t *pos)
2230 struct spu_proxydma_info info;
2231 struct mfc_cq_sr *qp, *puqp;
2232 int ret = sizeof info;
2233 int i;
2235 if (len < ret)
2236 return -EINVAL;
2238 if (!access_ok(VERIFY_WRITE, buf, len))
2239 return -EFAULT;
2241 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2242 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2243 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2244 for (i = 0; i < 8; i++) {
2245 qp = &info.proxydma_info_command_data[i];
2246 puqp = &ctx->csa.priv2.puq[i];
2248 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2249 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2250 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2251 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2254 return simple_read_from_buffer(buf, len, pos, &info,
2255 sizeof info);
2258 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2259 size_t len, loff_t *pos)
2261 struct spu_context *ctx = file->private_data;
2262 int ret;
2264 ret = spu_acquire_saved(ctx);
2265 if (ret)
2266 return ret;
2267 spin_lock(&ctx->csa.register_lock);
2268 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2269 spin_unlock(&ctx->csa.register_lock);
2270 spu_release_saved(ctx);
2272 return ret;
2275 static const struct file_operations spufs_proxydma_info_fops = {
2276 .open = spufs_info_open,
2277 .read = spufs_proxydma_info_read,
2280 static int spufs_show_tid(struct seq_file *s, void *private)
2282 struct spu_context *ctx = s->private;
2284 seq_printf(s, "%d\n", ctx->tid);
2285 return 0;
2288 static int spufs_tid_open(struct inode *inode, struct file *file)
2290 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2293 static const struct file_operations spufs_tid_fops = {
2294 .open = spufs_tid_open,
2295 .read = seq_read,
2296 .llseek = seq_lseek,
2297 .release = single_release,
2300 static const char *ctx_state_names[] = {
2301 "user", "system", "iowait", "loaded"
2304 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2305 enum spu_utilization_state state)
2307 struct timespec ts;
2308 unsigned long long time = ctx->stats.times[state];
2311 * In general, utilization statistics are updated by the controlling
2312 * thread as the spu context moves through various well defined
2313 * state transitions, but if the context is lazily loaded its
2314 * utilization statistics are not updated as the controlling thread
2315 * is not tightly coupled with the execution of the spu context. We
2316 * calculate and apply the time delta from the last recorded state
2317 * of the spu context.
2319 if (ctx->spu && ctx->stats.util_state == state) {
2320 ktime_get_ts(&ts);
2321 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2324 return time / NSEC_PER_MSEC;
2327 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2329 unsigned long long slb_flts = ctx->stats.slb_flt;
2331 if (ctx->state == SPU_STATE_RUNNABLE) {
2332 slb_flts += (ctx->spu->stats.slb_flt -
2333 ctx->stats.slb_flt_base);
2336 return slb_flts;
2339 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2341 unsigned long long class2_intrs = ctx->stats.class2_intr;
2343 if (ctx->state == SPU_STATE_RUNNABLE) {
2344 class2_intrs += (ctx->spu->stats.class2_intr -
2345 ctx->stats.class2_intr_base);
2348 return class2_intrs;
2352 static int spufs_show_stat(struct seq_file *s, void *private)
2354 struct spu_context *ctx = s->private;
2355 int ret;
2357 ret = spu_acquire(ctx);
2358 if (ret)
2359 return ret;
2361 seq_printf(s, "%s %llu %llu %llu %llu "
2362 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2363 ctx_state_names[ctx->stats.util_state],
2364 spufs_acct_time(ctx, SPU_UTIL_USER),
2365 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2366 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2367 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2368 ctx->stats.vol_ctx_switch,
2369 ctx->stats.invol_ctx_switch,
2370 spufs_slb_flts(ctx),
2371 ctx->stats.hash_flt,
2372 ctx->stats.min_flt,
2373 ctx->stats.maj_flt,
2374 spufs_class2_intrs(ctx),
2375 ctx->stats.libassist);
2376 spu_release(ctx);
2377 return 0;
2380 static int spufs_stat_open(struct inode *inode, struct file *file)
2382 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2385 static const struct file_operations spufs_stat_fops = {
2386 .open = spufs_stat_open,
2387 .read = seq_read,
2388 .llseek = seq_lseek,
2389 .release = single_release,
2392 static inline int spufs_switch_log_used(struct spu_context *ctx)
2394 return (ctx->switch_log->head - ctx->switch_log->tail) %
2395 SWITCH_LOG_BUFSIZE;
2398 static inline int spufs_switch_log_avail(struct spu_context *ctx)
2400 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2403 static int spufs_switch_log_open(struct inode *inode, struct file *file)
2405 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2408 * We (ab-)use the mapping_lock here because it serves the similar
2409 * purpose for synchronizing open/close elsewhere. Maybe it should
2410 * be renamed eventually.
2412 mutex_lock(&ctx->mapping_lock);
2413 if (ctx->switch_log) {
2414 spin_lock(&ctx->switch_log->lock);
2415 ctx->switch_log->head = 0;
2416 ctx->switch_log->tail = 0;
2417 spin_unlock(&ctx->switch_log->lock);
2418 } else {
2420 * We allocate the switch log data structures on first open.
2421 * They will never be free because we assume a context will
2422 * be traced until it goes away.
2424 ctx->switch_log = kzalloc(sizeof(struct switch_log) +
2425 SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
2426 GFP_KERNEL);
2427 if (!ctx->switch_log)
2428 goto out;
2429 spin_lock_init(&ctx->switch_log->lock);
2430 init_waitqueue_head(&ctx->switch_log->wait);
2432 mutex_unlock(&ctx->mapping_lock);
2434 return 0;
2435 out:
2436 mutex_unlock(&ctx->mapping_lock);
2437 return -ENOMEM;
2440 static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2442 struct switch_log_entry *p;
2444 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2446 return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
2447 (unsigned int) p->tstamp.tv_sec,
2448 (unsigned int) p->tstamp.tv_nsec,
2449 p->spu_id,
2450 (unsigned int) p->type,
2451 (unsigned int) p->val,
2452 (unsigned long long) p->timebase);
2455 static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2456 size_t len, loff_t *ppos)
2458 struct inode *inode = file->f_path.dentry->d_inode;
2459 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2460 int error = 0, cnt = 0;
2462 if (!buf || len < 0)
2463 return -EINVAL;
2465 while (cnt < len) {
2466 char tbuf[128];
2467 int width;
2469 if (file->f_flags & O_NONBLOCK) {
2470 if (spufs_switch_log_used(ctx) <= 0)
2471 return cnt ? cnt : -EAGAIN;
2472 } else {
2473 /* Wait for data in buffer */
2474 error = wait_event_interruptible(ctx->switch_log->wait,
2475 spufs_switch_log_used(ctx) > 0);
2476 if (error)
2477 break;
2480 spin_lock(&ctx->switch_log->lock);
2481 if (ctx->switch_log->head == ctx->switch_log->tail) {
2482 /* multiple readers race? */
2483 spin_unlock(&ctx->switch_log->lock);
2484 continue;
2487 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
2488 if (width < len) {
2489 ctx->switch_log->tail =
2490 (ctx->switch_log->tail + 1) %
2491 SWITCH_LOG_BUFSIZE;
2494 spin_unlock(&ctx->switch_log->lock);
2497 * If the record is greater than space available return
2498 * partial buffer (so far)
2500 if (width >= len)
2501 break;
2503 error = copy_to_user(buf + cnt, tbuf, width);
2504 if (error)
2505 break;
2506 cnt += width;
2509 return cnt == 0 ? error : cnt;
2512 static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
2514 struct inode *inode = file->f_path.dentry->d_inode;
2515 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2516 unsigned int mask = 0;
2518 poll_wait(file, &ctx->switch_log->wait, wait);
2520 if (spufs_switch_log_used(ctx) > 0)
2521 mask |= POLLIN;
2523 return mask;
2526 static const struct file_operations spufs_switch_log_fops = {
2527 .owner = THIS_MODULE,
2528 .open = spufs_switch_log_open,
2529 .read = spufs_switch_log_read,
2530 .poll = spufs_switch_log_poll,
2533 void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2534 u32 type, u32 val)
2536 if (!ctx->switch_log)
2537 return;
2539 spin_lock(&ctx->switch_log->lock);
2540 if (spufs_switch_log_avail(ctx) > 1) {
2541 struct switch_log_entry *p;
2543 p = ctx->switch_log->log + ctx->switch_log->head;
2544 ktime_get_ts(&p->tstamp);
2545 p->timebase = get_tb();
2546 p->spu_id = spu ? spu->number : -1;
2547 p->type = type;
2548 p->val = val;
2550 ctx->switch_log->head =
2551 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2553 spin_unlock(&ctx->switch_log->lock);
2555 wake_up(&ctx->switch_log->wait);
2558 static int spufs_show_ctx(struct seq_file *s, void *private)
2560 struct spu_context *ctx = s->private;
2561 u64 mfc_control_RW;
2563 mutex_lock(&ctx->state_mutex);
2564 if (ctx->spu) {
2565 struct spu *spu = ctx->spu;
2566 struct spu_priv2 __iomem *priv2 = spu->priv2;
2568 spin_lock_irq(&spu->register_lock);
2569 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2570 spin_unlock_irq(&spu->register_lock);
2571 } else {
2572 struct spu_state *csa = &ctx->csa;
2574 mfc_control_RW = csa->priv2.mfc_control_RW;
2577 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
2578 " %c %lx %lx %lx %lx %x %x\n",
2579 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2580 ctx->flags,
2581 ctx->sched_flags,
2582 ctx->prio,
2583 ctx->time_slice,
2584 ctx->spu ? ctx->spu->number : -1,
2585 !list_empty(&ctx->rq) ? 'q' : ' ',
2586 ctx->csa.class_0_pending,
2587 ctx->csa.class_0_dar,
2588 ctx->csa.class_1_dsisr,
2589 mfc_control_RW,
2590 ctx->ops->runcntl_read(ctx),
2591 ctx->ops->status_read(ctx));
2593 mutex_unlock(&ctx->state_mutex);
2595 return 0;
2598 static int spufs_ctx_open(struct inode *inode, struct file *file)
2600 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2603 static const struct file_operations spufs_ctx_fops = {
2604 .open = spufs_ctx_open,
2605 .read = seq_read,
2606 .llseek = seq_lseek,
2607 .release = single_release,
2610 struct tree_descr spufs_dir_contents[] = {
2611 { "capabilities", &spufs_caps_fops, 0444, },
2612 { "mem", &spufs_mem_fops, 0666, },
2613 { "regs", &spufs_regs_fops, 0666, },
2614 { "mbox", &spufs_mbox_fops, 0444, },
2615 { "ibox", &spufs_ibox_fops, 0444, },
2616 { "wbox", &spufs_wbox_fops, 0222, },
2617 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2618 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2619 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2620 { "signal1", &spufs_signal1_fops, 0666, },
2621 { "signal2", &spufs_signal2_fops, 0666, },
2622 { "signal1_type", &spufs_signal1_type, 0666, },
2623 { "signal2_type", &spufs_signal2_type, 0666, },
2624 { "cntl", &spufs_cntl_fops, 0666, },
2625 { "fpcr", &spufs_fpcr_fops, 0666, },
2626 { "lslr", &spufs_lslr_ops, 0444, },
2627 { "mfc", &spufs_mfc_fops, 0666, },
2628 { "mss", &spufs_mss_fops, 0666, },
2629 { "npc", &spufs_npc_ops, 0666, },
2630 { "srr0", &spufs_srr0_ops, 0666, },
2631 { "decr", &spufs_decr_ops, 0666, },
2632 { "decr_status", &spufs_decr_status_ops, 0666, },
2633 { "event_mask", &spufs_event_mask_ops, 0666, },
2634 { "event_status", &spufs_event_status_ops, 0444, },
2635 { "psmap", &spufs_psmap_fops, 0666, },
2636 { "phys-id", &spufs_id_ops, 0666, },
2637 { "object-id", &spufs_object_id_ops, 0666, },
2638 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2639 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2640 { "wbox_info", &spufs_wbox_info_fops, 0444, },
2641 { "dma_info", &spufs_dma_info_fops, 0444, },
2642 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
2643 { "tid", &spufs_tid_fops, 0444, },
2644 { "stat", &spufs_stat_fops, 0444, },
2645 { "switch_log", &spufs_switch_log_fops, 0444 },
2646 { ".ctx", &spufs_ctx_fops, 0444, },
2650 struct tree_descr spufs_dir_nosched_contents[] = {
2651 { "capabilities", &spufs_caps_fops, 0444, },
2652 { "mem", &spufs_mem_fops, 0666, },
2653 { "mbox", &spufs_mbox_fops, 0444, },
2654 { "ibox", &spufs_ibox_fops, 0444, },
2655 { "wbox", &spufs_wbox_fops, 0222, },
2656 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2657 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2658 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2659 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2660 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2661 { "signal1_type", &spufs_signal1_type, 0666, },
2662 { "signal2_type", &spufs_signal2_type, 0666, },
2663 { "mss", &spufs_mss_fops, 0666, },
2664 { "mfc", &spufs_mfc_fops, 0666, },
2665 { "cntl", &spufs_cntl_fops, 0666, },
2666 { "npc", &spufs_npc_ops, 0666, },
2667 { "psmap", &spufs_psmap_fops, 0666, },
2668 { "phys-id", &spufs_id_ops, 0666, },
2669 { "object-id", &spufs_object_id_ops, 0666, },
2670 { "tid", &spufs_tid_fops, 0444, },
2671 { "stat", &spufs_stat_fops, 0444, },
2672 { ".ctx", &spufs_ctx_fops, 0444, },
2676 struct spufs_coredump_reader spufs_coredump_read[] = {
2677 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2678 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2679 { "lslr", NULL, spufs_lslr_get, 19 },
2680 { "decr", NULL, spufs_decr_get, 19 },
2681 { "decr_status", NULL, spufs_decr_status_get, 19 },
2682 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2683 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2684 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
2685 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2686 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2687 { "event_mask", NULL, spufs_event_mask_get, 19 },
2688 { "event_status", NULL, spufs_event_status_get, 19 },
2689 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2690 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2691 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2692 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2693 { "proxydma_info", __spufs_proxydma_info_read,
2694 NULL, sizeof(struct spu_proxydma_info)},
2695 { "object-id", NULL, spufs_object_id_get, 19 },
2696 { "npc", NULL, spufs_npc_get, 19 },
2697 { NULL },