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)
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.
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>
35 #include <asm/semaphore.h>
37 #include <asm/spu_info.h>
38 #include <asm/uaccess.h>
42 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
44 /* Simple attribute files */
46 int (*get)(void *, u64 *);
47 int (*set)(void *, u64);
48 char get_buf[24]; /* enough to store a u64 and "\n\0" */
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),
59 struct spufs_attr *attr;
61 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
67 attr->data = inode->i_private;
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);
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;
88 attr = file->private_data;
92 ret = mutex_lock_interruptible(&attr->mutex);
96 if (*ppos) { /* continued read */
97 size = strlen(attr->get_buf);
98 } else { /* first read */
100 ret = attr->get(attr->data, &val);
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);
110 mutex_unlock(&attr->mutex);
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;
122 attr = file->private_data;
126 ret = mutex_lock_interruptible(&attr->mutex);
131 size = min(sizeof(attr->set_buf) - 1, len);
132 if (copy_from_user(attr->set_buf, buf, size))
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);
140 mutex_unlock(&attr->mutex);
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, \
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;
168 ctx->local_store = inode->i_mapping;
169 mutex_unlock(&ctx->mapping_lock);
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);
181 ctx->local_store = NULL;
182 mutex_unlock(&ctx->mapping_lock);
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,
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;
202 ret = spu_acquire(ctx);
205 ret = __spufs_mem_read(ctx, buffer, size, pos);
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;
224 if (size > LS_SIZE - pos)
225 size = LS_SIZE - pos;
227 ret = spu_acquire(ctx);
231 local_store = ctx->ops->get_ls(ctx);
232 ret = copy_from_user(local_store + pos, buffer, 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;
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)
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)
276 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
278 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
280 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
282 vm_insert_pfn(vma, address, pfn);
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,
305 if (vma->vm_start & 0xffff)
307 if (vma->vm_pgoff & 0xf)
310 #endif /* CONFIG_SPU_FS_64K_LS */
312 if (!(vma->vm_flags & VM_SHARED))
315 vma->vm_flags |= VM_IO | VM_PFNMAP;
316 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
319 vma->vm_ops = &spufs_mem_mmap_vmops;
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,
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,
336 /* Else, try to obtain a 64K pages slice */
337 return slice_get_unmapped_area(addr, len, flags,
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,
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;
363 spu_context_nospu_trace(spufs_ps_nopfn__enter, ctx);
365 offset += vma->vm_pgoff << PAGE_SHIFT;
366 if (offset >= ps_size)
370 * We have to wait for context to be loaded before we have
371 * pages to hand out to the user, but we don't want to wait
372 * with the mmap_sem held.
373 * It is possible to drop the mmap_sem here, but then we need
374 * to return NOPFN_REFAULT because the mappings may have
377 if (spu_acquire(ctx))
378 return NOPFN_REFAULT;
380 if (ctx->state == SPU_STATE_SAVED) {
381 up_read(¤t->mm->mmap_sem);
382 spu_context_nospu_trace(spufs_ps_nopfn__sleep, ctx);
383 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
384 spu_context_trace(spufs_ps_nopfn__wake, ctx, ctx->spu);
385 down_read(¤t->mm->mmap_sem);
387 area = ctx->spu->problem_phys + ps_offs;
388 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
389 spu_context_trace(spufs_ps_nopfn__insert, ctx, ctx->spu);
394 return NOPFN_REFAULT;
398 static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
399 unsigned long address)
401 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
404 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
405 .nopfn = spufs_cntl_mmap_nopfn,
409 * mmap support for problem state control area [0x4000 - 0x4fff].
411 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
413 if (!(vma->vm_flags & VM_SHARED))
416 vma->vm_flags |= VM_IO | VM_PFNMAP;
417 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
418 | _PAGE_NO_CACHE | _PAGE_GUARDED);
420 vma->vm_ops = &spufs_cntl_mmap_vmops;
423 #else /* SPUFS_MMAP_4K */
424 #define spufs_cntl_mmap NULL
425 #endif /* !SPUFS_MMAP_4K */
427 static int spufs_cntl_get(void *data, u64 *val)
429 struct spu_context *ctx = data;
432 ret = spu_acquire(ctx);
435 *val = ctx->ops->status_read(ctx);
441 static int spufs_cntl_set(void *data, u64 val)
443 struct spu_context *ctx = data;
446 ret = spu_acquire(ctx);
449 ctx->ops->runcntl_write(ctx, val);
455 static int spufs_cntl_open(struct inode *inode, struct file *file)
457 struct spufs_inode_info *i = SPUFS_I(inode);
458 struct spu_context *ctx = i->i_ctx;
460 mutex_lock(&ctx->mapping_lock);
461 file->private_data = ctx;
463 ctx->cntl = inode->i_mapping;
464 mutex_unlock(&ctx->mapping_lock);
465 return simple_attr_open(inode, file, spufs_cntl_get,
466 spufs_cntl_set, "0x%08lx");
470 spufs_cntl_release(struct inode *inode, struct file *file)
472 struct spufs_inode_info *i = SPUFS_I(inode);
473 struct spu_context *ctx = i->i_ctx;
475 simple_attr_release(inode, file);
477 mutex_lock(&ctx->mapping_lock);
480 mutex_unlock(&ctx->mapping_lock);
484 static const struct file_operations spufs_cntl_fops = {
485 .open = spufs_cntl_open,
486 .release = spufs_cntl_release,
487 .read = simple_attr_read,
488 .write = simple_attr_write,
489 .mmap = spufs_cntl_mmap,
493 spufs_regs_open(struct inode *inode, struct file *file)
495 struct spufs_inode_info *i = SPUFS_I(inode);
496 file->private_data = i->i_ctx;
501 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
502 size_t size, loff_t *pos)
504 struct spu_lscsa *lscsa = ctx->csa.lscsa;
505 return simple_read_from_buffer(buffer, size, pos,
506 lscsa->gprs, sizeof lscsa->gprs);
510 spufs_regs_read(struct file *file, char __user *buffer,
511 size_t size, loff_t *pos)
514 struct spu_context *ctx = file->private_data;
516 ret = spu_acquire_saved(ctx);
519 ret = __spufs_regs_read(ctx, buffer, size, pos);
520 spu_release_saved(ctx);
525 spufs_regs_write(struct file *file, const char __user *buffer,
526 size_t size, loff_t *pos)
528 struct spu_context *ctx = file->private_data;
529 struct spu_lscsa *lscsa = ctx->csa.lscsa;
532 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
537 ret = spu_acquire_saved(ctx);
541 ret = copy_from_user(lscsa->gprs + *pos - size,
542 buffer, size) ? -EFAULT : size;
544 spu_release_saved(ctx);
548 static const struct file_operations spufs_regs_fops = {
549 .open = spufs_regs_open,
550 .read = spufs_regs_read,
551 .write = spufs_regs_write,
552 .llseek = generic_file_llseek,
556 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
557 size_t size, loff_t * pos)
559 struct spu_lscsa *lscsa = ctx->csa.lscsa;
560 return simple_read_from_buffer(buffer, size, pos,
561 &lscsa->fpcr, sizeof(lscsa->fpcr));
565 spufs_fpcr_read(struct file *file, char __user * buffer,
566 size_t size, loff_t * pos)
569 struct spu_context *ctx = file->private_data;
571 ret = spu_acquire_saved(ctx);
574 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
575 spu_release_saved(ctx);
580 spufs_fpcr_write(struct file *file, const char __user * buffer,
581 size_t size, loff_t * pos)
583 struct spu_context *ctx = file->private_data;
584 struct spu_lscsa *lscsa = ctx->csa.lscsa;
587 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
591 ret = spu_acquire_saved(ctx);
596 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
597 buffer, size) ? -EFAULT : size;
599 spu_release_saved(ctx);
603 static const struct file_operations spufs_fpcr_fops = {
604 .open = spufs_regs_open,
605 .read = spufs_fpcr_read,
606 .write = spufs_fpcr_write,
607 .llseek = generic_file_llseek,
610 /* generic open function for all pipe-like files */
611 static int spufs_pipe_open(struct inode *inode, struct file *file)
613 struct spufs_inode_info *i = SPUFS_I(inode);
614 file->private_data = i->i_ctx;
616 return nonseekable_open(inode, file);
620 * Read as many bytes from the mailbox as possible, until
621 * one of the conditions becomes true:
623 * - no more data available in the mailbox
624 * - end of the user provided buffer
625 * - end of the mapped area
627 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
628 size_t len, loff_t *pos)
630 struct spu_context *ctx = file->private_data;
631 u32 mbox_data, __user *udata;
637 if (!access_ok(VERIFY_WRITE, buf, len))
640 udata = (void __user *)buf;
642 count = spu_acquire(ctx);
646 for (count = 0; (count + 4) <= len; count += 4, udata++) {
648 ret = ctx->ops->mbox_read(ctx, &mbox_data);
653 * at the end of the mapped area, we can fault
654 * but still need to return the data we have
655 * read successfully so far.
657 ret = __put_user(mbox_data, udata);
672 static const struct file_operations spufs_mbox_fops = {
673 .open = spufs_pipe_open,
674 .read = spufs_mbox_read,
677 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
678 size_t len, loff_t *pos)
680 struct spu_context *ctx = file->private_data;
687 ret = spu_acquire(ctx);
691 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
695 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
701 static const struct file_operations spufs_mbox_stat_fops = {
702 .open = spufs_pipe_open,
703 .read = spufs_mbox_stat_read,
706 /* low-level ibox access function */
707 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
709 return ctx->ops->ibox_read(ctx, data);
712 static int spufs_ibox_fasync(int fd, struct file *file, int on)
714 struct spu_context *ctx = file->private_data;
716 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
719 /* interrupt-level ibox callback function. */
720 void spufs_ibox_callback(struct spu *spu)
722 struct spu_context *ctx = spu->ctx;
727 wake_up_all(&ctx->ibox_wq);
728 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
732 * Read as many bytes from the interrupt mailbox as possible, until
733 * one of the conditions becomes true:
735 * - no more data available in the mailbox
736 * - end of the user provided buffer
737 * - end of the mapped area
739 * If the file is opened without O_NONBLOCK, we wait here until
740 * any data is available, but return when we have been able to
743 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
744 size_t len, loff_t *pos)
746 struct spu_context *ctx = file->private_data;
747 u32 ibox_data, __user *udata;
753 if (!access_ok(VERIFY_WRITE, buf, len))
756 udata = (void __user *)buf;
758 count = spu_acquire(ctx);
762 /* wait only for the first element */
764 if (file->f_flags & O_NONBLOCK) {
765 if (!spu_ibox_read(ctx, &ibox_data)) {
770 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
775 /* if we can't write at all, return -EFAULT */
776 count = __put_user(ibox_data, udata);
780 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
782 ret = ctx->ops->ibox_read(ctx, &ibox_data);
786 * at the end of the mapped area, we can fault
787 * but still need to return the data we have
788 * read successfully so far.
790 ret = __put_user(ibox_data, udata);
801 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
803 struct spu_context *ctx = file->private_data;
806 poll_wait(file, &ctx->ibox_wq, wait);
809 * For now keep this uninterruptible and also ignore the rule
810 * that poll should not sleep. Will be fixed later.
812 mutex_lock(&ctx->state_mutex);
813 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
819 static const struct file_operations spufs_ibox_fops = {
820 .open = spufs_pipe_open,
821 .read = spufs_ibox_read,
822 .poll = spufs_ibox_poll,
823 .fasync = spufs_ibox_fasync,
826 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
827 size_t len, loff_t *pos)
829 struct spu_context *ctx = file->private_data;
836 ret = spu_acquire(ctx);
839 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
842 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
848 static const struct file_operations spufs_ibox_stat_fops = {
849 .open = spufs_pipe_open,
850 .read = spufs_ibox_stat_read,
853 /* low-level mailbox write */
854 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
856 return ctx->ops->wbox_write(ctx, data);
859 static int spufs_wbox_fasync(int fd, struct file *file, int on)
861 struct spu_context *ctx = file->private_data;
864 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
869 /* interrupt-level wbox callback function. */
870 void spufs_wbox_callback(struct spu *spu)
872 struct spu_context *ctx = spu->ctx;
877 wake_up_all(&ctx->wbox_wq);
878 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
882 * Write as many bytes to the interrupt mailbox as possible, until
883 * one of the conditions becomes true:
885 * - the mailbox is full
886 * - end of the user provided buffer
887 * - end of the mapped area
889 * If the file is opened without O_NONBLOCK, we wait here until
890 * space is availabyl, but return when we have been able to
893 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
894 size_t len, loff_t *pos)
896 struct spu_context *ctx = file->private_data;
897 u32 wbox_data, __user *udata;
903 udata = (void __user *)buf;
904 if (!access_ok(VERIFY_READ, buf, len))
907 if (__get_user(wbox_data, udata))
910 count = spu_acquire(ctx);
915 * make sure we can at least write one element, by waiting
916 * in case of !O_NONBLOCK
919 if (file->f_flags & O_NONBLOCK) {
920 if (!spu_wbox_write(ctx, wbox_data)) {
925 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
931 /* write as much as possible */
932 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
934 ret = __get_user(wbox_data, udata);
938 ret = spu_wbox_write(ctx, wbox_data);
949 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
951 struct spu_context *ctx = file->private_data;
954 poll_wait(file, &ctx->wbox_wq, wait);
957 * For now keep this uninterruptible and also ignore the rule
958 * that poll should not sleep. Will be fixed later.
960 mutex_lock(&ctx->state_mutex);
961 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
967 static const struct file_operations spufs_wbox_fops = {
968 .open = spufs_pipe_open,
969 .write = spufs_wbox_write,
970 .poll = spufs_wbox_poll,
971 .fasync = spufs_wbox_fasync,
974 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
975 size_t len, loff_t *pos)
977 struct spu_context *ctx = file->private_data;
984 ret = spu_acquire(ctx);
987 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
990 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
996 static const struct file_operations spufs_wbox_stat_fops = {
997 .open = spufs_pipe_open,
998 .read = spufs_wbox_stat_read,
1001 static int spufs_signal1_open(struct inode *inode, struct file *file)
1003 struct spufs_inode_info *i = SPUFS_I(inode);
1004 struct spu_context *ctx = i->i_ctx;
1006 mutex_lock(&ctx->mapping_lock);
1007 file->private_data = ctx;
1008 if (!i->i_openers++)
1009 ctx->signal1 = inode->i_mapping;
1010 mutex_unlock(&ctx->mapping_lock);
1011 return nonseekable_open(inode, file);
1015 spufs_signal1_release(struct inode *inode, struct file *file)
1017 struct spufs_inode_info *i = SPUFS_I(inode);
1018 struct spu_context *ctx = i->i_ctx;
1020 mutex_lock(&ctx->mapping_lock);
1021 if (!--i->i_openers)
1022 ctx->signal1 = NULL;
1023 mutex_unlock(&ctx->mapping_lock);
1027 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
1028 size_t len, loff_t *pos)
1036 if (ctx->csa.spu_chnlcnt_RW[3]) {
1037 data = ctx->csa.spu_chnldata_RW[3];
1044 if (copy_to_user(buf, &data, 4))
1051 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1052 size_t len, loff_t *pos)
1055 struct spu_context *ctx = file->private_data;
1057 ret = spu_acquire_saved(ctx);
1060 ret = __spufs_signal1_read(ctx, buf, len, pos);
1061 spu_release_saved(ctx);
1066 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1067 size_t len, loff_t *pos)
1069 struct spu_context *ctx;
1073 ctx = file->private_data;
1078 if (copy_from_user(&data, buf, 4))
1081 ret = spu_acquire(ctx);
1084 ctx->ops->signal1_write(ctx, data);
1090 static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1091 unsigned long address)
1093 #if PAGE_SIZE == 0x1000
1094 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
1095 #elif PAGE_SIZE == 0x10000
1096 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1097 * signal 1 and 2 area
1099 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1101 #error unsupported page size
1105 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
1106 .nopfn = spufs_signal1_mmap_nopfn,
1109 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1111 if (!(vma->vm_flags & VM_SHARED))
1114 vma->vm_flags |= VM_IO | VM_PFNMAP;
1115 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1116 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1118 vma->vm_ops = &spufs_signal1_mmap_vmops;
1122 static const struct file_operations spufs_signal1_fops = {
1123 .open = spufs_signal1_open,
1124 .release = spufs_signal1_release,
1125 .read = spufs_signal1_read,
1126 .write = spufs_signal1_write,
1127 .mmap = spufs_signal1_mmap,
1130 static const struct file_operations spufs_signal1_nosched_fops = {
1131 .open = spufs_signal1_open,
1132 .release = spufs_signal1_release,
1133 .write = spufs_signal1_write,
1134 .mmap = spufs_signal1_mmap,
1137 static int spufs_signal2_open(struct inode *inode, struct file *file)
1139 struct spufs_inode_info *i = SPUFS_I(inode);
1140 struct spu_context *ctx = i->i_ctx;
1142 mutex_lock(&ctx->mapping_lock);
1143 file->private_data = ctx;
1144 if (!i->i_openers++)
1145 ctx->signal2 = inode->i_mapping;
1146 mutex_unlock(&ctx->mapping_lock);
1147 return nonseekable_open(inode, file);
1151 spufs_signal2_release(struct inode *inode, struct file *file)
1153 struct spufs_inode_info *i = SPUFS_I(inode);
1154 struct spu_context *ctx = i->i_ctx;
1156 mutex_lock(&ctx->mapping_lock);
1157 if (!--i->i_openers)
1158 ctx->signal2 = NULL;
1159 mutex_unlock(&ctx->mapping_lock);
1163 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
1164 size_t len, loff_t *pos)
1172 if (ctx->csa.spu_chnlcnt_RW[4]) {
1173 data = ctx->csa.spu_chnldata_RW[4];
1180 if (copy_to_user(buf, &data, 4))
1187 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1188 size_t len, loff_t *pos)
1190 struct spu_context *ctx = file->private_data;
1193 ret = spu_acquire_saved(ctx);
1196 ret = __spufs_signal2_read(ctx, buf, len, pos);
1197 spu_release_saved(ctx);
1202 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1203 size_t len, loff_t *pos)
1205 struct spu_context *ctx;
1209 ctx = file->private_data;
1214 if (copy_from_user(&data, buf, 4))
1217 ret = spu_acquire(ctx);
1220 ctx->ops->signal2_write(ctx, data);
1227 static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1228 unsigned long address)
1230 #if PAGE_SIZE == 0x1000
1231 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
1232 #elif PAGE_SIZE == 0x10000
1233 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1234 * signal 1 and 2 area
1236 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1238 #error unsupported page size
1242 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1243 .nopfn = spufs_signal2_mmap_nopfn,
1246 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1248 if (!(vma->vm_flags & VM_SHARED))
1251 vma->vm_flags |= VM_IO | VM_PFNMAP;
1252 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1253 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1255 vma->vm_ops = &spufs_signal2_mmap_vmops;
1258 #else /* SPUFS_MMAP_4K */
1259 #define spufs_signal2_mmap NULL
1260 #endif /* !SPUFS_MMAP_4K */
1262 static const struct file_operations spufs_signal2_fops = {
1263 .open = spufs_signal2_open,
1264 .release = spufs_signal2_release,
1265 .read = spufs_signal2_read,
1266 .write = spufs_signal2_write,
1267 .mmap = spufs_signal2_mmap,
1270 static const struct file_operations spufs_signal2_nosched_fops = {
1271 .open = spufs_signal2_open,
1272 .release = spufs_signal2_release,
1273 .write = spufs_signal2_write,
1274 .mmap = spufs_signal2_mmap,
1278 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1279 * work of acquiring (or not) the SPU context before calling through
1280 * to the actual get routine. The set routine is called directly.
1282 #define SPU_ATTR_NOACQUIRE 0
1283 #define SPU_ATTR_ACQUIRE 1
1284 #define SPU_ATTR_ACQUIRE_SAVED 2
1286 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
1287 static int __##__get(void *data, u64 *val) \
1289 struct spu_context *ctx = data; \
1292 if (__acquire == SPU_ATTR_ACQUIRE) { \
1293 ret = spu_acquire(ctx); \
1296 *val = __get(ctx); \
1298 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1299 ret = spu_acquire_saved(ctx); \
1302 *val = __get(ctx); \
1303 spu_release_saved(ctx); \
1305 *val = __get(ctx); \
1309 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1311 static int spufs_signal1_type_set(void *data, u64 val)
1313 struct spu_context *ctx = data;
1316 ret = spu_acquire(ctx);
1319 ctx->ops->signal1_type_set(ctx, val);
1325 static u64 spufs_signal1_type_get(struct spu_context *ctx)
1327 return ctx->ops->signal1_type_get(ctx);
1329 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1330 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
1333 static int spufs_signal2_type_set(void *data, u64 val)
1335 struct spu_context *ctx = data;
1338 ret = spu_acquire(ctx);
1341 ctx->ops->signal2_type_set(ctx, val);
1347 static u64 spufs_signal2_type_get(struct spu_context *ctx)
1349 return ctx->ops->signal2_type_get(ctx);
1351 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1352 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
1355 static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1356 unsigned long address)
1358 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
1361 static struct vm_operations_struct spufs_mss_mmap_vmops = {
1362 .nopfn = spufs_mss_mmap_nopfn,
1366 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1368 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1370 if (!(vma->vm_flags & VM_SHARED))
1373 vma->vm_flags |= VM_IO | VM_PFNMAP;
1374 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1375 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1377 vma->vm_ops = &spufs_mss_mmap_vmops;
1380 #else /* SPUFS_MMAP_4K */
1381 #define spufs_mss_mmap NULL
1382 #endif /* !SPUFS_MMAP_4K */
1384 static int spufs_mss_open(struct inode *inode, struct file *file)
1386 struct spufs_inode_info *i = SPUFS_I(inode);
1387 struct spu_context *ctx = i->i_ctx;
1389 file->private_data = i->i_ctx;
1391 mutex_lock(&ctx->mapping_lock);
1392 if (!i->i_openers++)
1393 ctx->mss = inode->i_mapping;
1394 mutex_unlock(&ctx->mapping_lock);
1395 return nonseekable_open(inode, file);
1399 spufs_mss_release(struct inode *inode, struct file *file)
1401 struct spufs_inode_info *i = SPUFS_I(inode);
1402 struct spu_context *ctx = i->i_ctx;
1404 mutex_lock(&ctx->mapping_lock);
1405 if (!--i->i_openers)
1407 mutex_unlock(&ctx->mapping_lock);
1411 static const struct file_operations spufs_mss_fops = {
1412 .open = spufs_mss_open,
1413 .release = spufs_mss_release,
1414 .mmap = spufs_mss_mmap,
1417 static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1418 unsigned long address)
1420 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
1423 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1424 .nopfn = spufs_psmap_mmap_nopfn,
1428 * mmap support for full problem state area [0x00000 - 0x1ffff].
1430 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1432 if (!(vma->vm_flags & VM_SHARED))
1435 vma->vm_flags |= VM_IO | VM_PFNMAP;
1436 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1437 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1439 vma->vm_ops = &spufs_psmap_mmap_vmops;
1443 static int spufs_psmap_open(struct inode *inode, struct file *file)
1445 struct spufs_inode_info *i = SPUFS_I(inode);
1446 struct spu_context *ctx = i->i_ctx;
1448 mutex_lock(&ctx->mapping_lock);
1449 file->private_data = i->i_ctx;
1450 if (!i->i_openers++)
1451 ctx->psmap = inode->i_mapping;
1452 mutex_unlock(&ctx->mapping_lock);
1453 return nonseekable_open(inode, file);
1457 spufs_psmap_release(struct inode *inode, struct file *file)
1459 struct spufs_inode_info *i = SPUFS_I(inode);
1460 struct spu_context *ctx = i->i_ctx;
1462 mutex_lock(&ctx->mapping_lock);
1463 if (!--i->i_openers)
1465 mutex_unlock(&ctx->mapping_lock);
1469 static const struct file_operations spufs_psmap_fops = {
1470 .open = spufs_psmap_open,
1471 .release = spufs_psmap_release,
1472 .mmap = spufs_psmap_mmap,
1477 static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1478 unsigned long address)
1480 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
1483 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1484 .nopfn = spufs_mfc_mmap_nopfn,
1488 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1490 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1492 if (!(vma->vm_flags & VM_SHARED))
1495 vma->vm_flags |= VM_IO | VM_PFNMAP;
1496 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1497 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1499 vma->vm_ops = &spufs_mfc_mmap_vmops;
1502 #else /* SPUFS_MMAP_4K */
1503 #define spufs_mfc_mmap NULL
1504 #endif /* !SPUFS_MMAP_4K */
1506 static int spufs_mfc_open(struct inode *inode, struct file *file)
1508 struct spufs_inode_info *i = SPUFS_I(inode);
1509 struct spu_context *ctx = i->i_ctx;
1511 /* we don't want to deal with DMA into other processes */
1512 if (ctx->owner != current->mm)
1515 if (atomic_read(&inode->i_count) != 1)
1518 mutex_lock(&ctx->mapping_lock);
1519 file->private_data = ctx;
1520 if (!i->i_openers++)
1521 ctx->mfc = inode->i_mapping;
1522 mutex_unlock(&ctx->mapping_lock);
1523 return nonseekable_open(inode, file);
1527 spufs_mfc_release(struct inode *inode, struct file *file)
1529 struct spufs_inode_info *i = SPUFS_I(inode);
1530 struct spu_context *ctx = i->i_ctx;
1532 mutex_lock(&ctx->mapping_lock);
1533 if (!--i->i_openers)
1535 mutex_unlock(&ctx->mapping_lock);
1539 /* interrupt-level mfc callback function. */
1540 void spufs_mfc_callback(struct spu *spu)
1542 struct spu_context *ctx = spu->ctx;
1547 wake_up_all(&ctx->mfc_wq);
1549 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1550 if (ctx->mfc_fasync) {
1551 u32 free_elements, tagstatus;
1554 /* no need for spu_acquire in interrupt context */
1555 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1556 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1559 if (free_elements & 0xffff)
1561 if (tagstatus & ctx->tagwait)
1564 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1568 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1570 /* See if there is one tag group is complete */
1571 /* FIXME we need locking around tagwait */
1572 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1573 ctx->tagwait &= ~*status;
1577 /* enable interrupt waiting for any tag group,
1578 may silently fail if interrupts are already enabled */
1579 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1583 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1584 size_t size, loff_t *pos)
1586 struct spu_context *ctx = file->private_data;
1593 ret = spu_acquire(ctx);
1598 if (file->f_flags & O_NONBLOCK) {
1599 status = ctx->ops->read_mfc_tagstatus(ctx);
1600 if (!(status & ctx->tagwait))
1603 /* XXX(hch): shouldn't we clear ret here? */
1604 ctx->tagwait &= ~status;
1606 ret = spufs_wait(ctx->mfc_wq,
1607 spufs_read_mfc_tagstatus(ctx, &status));
1614 if (copy_to_user(buffer, &status, 4))
1621 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1623 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1624 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1635 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1639 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1640 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1645 switch (cmd->size & 0xf) {
1666 pr_debug("invalid DMA alignment %x for size %x\n",
1667 cmd->lsa & 0xf, cmd->size);
1671 if (cmd->size > 16 * 1024) {
1672 pr_debug("invalid DMA size %x\n", cmd->size);
1676 if (cmd->tag & 0xfff0) {
1677 /* we reserve the higher tag numbers for kernel use */
1678 pr_debug("invalid DMA tag\n");
1683 /* not supported in this version */
1684 pr_debug("invalid DMA class\n");
1691 static int spu_send_mfc_command(struct spu_context *ctx,
1692 struct mfc_dma_command cmd,
1695 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1696 if (*error == -EAGAIN) {
1697 /* wait for any tag group to complete
1698 so we have space for the new command */
1699 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1700 /* try again, because the queue might be
1702 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1703 if (*error == -EAGAIN)
1709 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1710 size_t size, loff_t *pos)
1712 struct spu_context *ctx = file->private_data;
1713 struct mfc_dma_command cmd;
1716 if (size != sizeof cmd)
1720 if (copy_from_user(&cmd, buffer, sizeof cmd))
1723 ret = spufs_check_valid_dma(&cmd);
1727 ret = spu_acquire(ctx);
1731 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1735 if (file->f_flags & O_NONBLOCK) {
1736 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1739 ret = spufs_wait(ctx->mfc_wq,
1740 spu_send_mfc_command(ctx, cmd, &status));
1750 ctx->tagwait |= 1 << cmd.tag;
1759 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1761 struct spu_context *ctx = file->private_data;
1762 u32 free_elements, tagstatus;
1765 poll_wait(file, &ctx->mfc_wq, wait);
1768 * For now keep this uninterruptible and also ignore the rule
1769 * that poll should not sleep. Will be fixed later.
1771 mutex_lock(&ctx->state_mutex);
1772 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1773 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1774 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1778 if (free_elements & 0xffff)
1779 mask |= POLLOUT | POLLWRNORM;
1780 if (tagstatus & ctx->tagwait)
1781 mask |= POLLIN | POLLRDNORM;
1783 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1784 free_elements, tagstatus, ctx->tagwait);
1789 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1791 struct spu_context *ctx = file->private_data;
1794 ret = spu_acquire(ctx);
1798 /* this currently hangs */
1799 ret = spufs_wait(ctx->mfc_wq,
1800 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1803 ret = spufs_wait(ctx->mfc_wq,
1804 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1815 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1818 return spufs_mfc_flush(file, NULL);
1821 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1823 struct spu_context *ctx = file->private_data;
1825 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1828 static const struct file_operations spufs_mfc_fops = {
1829 .open = spufs_mfc_open,
1830 .release = spufs_mfc_release,
1831 .read = spufs_mfc_read,
1832 .write = spufs_mfc_write,
1833 .poll = spufs_mfc_poll,
1834 .flush = spufs_mfc_flush,
1835 .fsync = spufs_mfc_fsync,
1836 .fasync = spufs_mfc_fasync,
1837 .mmap = spufs_mfc_mmap,
1840 static int spufs_npc_set(void *data, u64 val)
1842 struct spu_context *ctx = data;
1845 ret = spu_acquire(ctx);
1848 ctx->ops->npc_write(ctx, val);
1854 static u64 spufs_npc_get(struct spu_context *ctx)
1856 return ctx->ops->npc_read(ctx);
1858 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1859 "0x%llx\n", SPU_ATTR_ACQUIRE);
1861 static int spufs_decr_set(void *data, u64 val)
1863 struct spu_context *ctx = data;
1864 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1867 ret = spu_acquire_saved(ctx);
1870 lscsa->decr.slot[0] = (u32) val;
1871 spu_release_saved(ctx);
1876 static u64 spufs_decr_get(struct spu_context *ctx)
1878 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1879 return lscsa->decr.slot[0];
1881 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1882 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
1884 static int spufs_decr_status_set(void *data, u64 val)
1886 struct spu_context *ctx = data;
1889 ret = spu_acquire_saved(ctx);
1893 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1895 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1896 spu_release_saved(ctx);
1901 static u64 spufs_decr_status_get(struct spu_context *ctx)
1903 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1904 return SPU_DECR_STATUS_RUNNING;
1908 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1909 spufs_decr_status_set, "0x%llx\n",
1910 SPU_ATTR_ACQUIRE_SAVED);
1912 static int spufs_event_mask_set(void *data, u64 val)
1914 struct spu_context *ctx = data;
1915 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1918 ret = spu_acquire_saved(ctx);
1921 lscsa->event_mask.slot[0] = (u32) val;
1922 spu_release_saved(ctx);
1927 static u64 spufs_event_mask_get(struct spu_context *ctx)
1929 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1930 return lscsa->event_mask.slot[0];
1933 DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1934 spufs_event_mask_set, "0x%llx\n",
1935 SPU_ATTR_ACQUIRE_SAVED);
1937 static u64 spufs_event_status_get(struct spu_context *ctx)
1939 struct spu_state *state = &ctx->csa;
1941 stat = state->spu_chnlcnt_RW[0];
1943 return state->spu_chnldata_RW[0];
1946 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1947 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1949 static int spufs_srr0_set(void *data, u64 val)
1951 struct spu_context *ctx = data;
1952 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1955 ret = spu_acquire_saved(ctx);
1958 lscsa->srr0.slot[0] = (u32) val;
1959 spu_release_saved(ctx);
1964 static u64 spufs_srr0_get(struct spu_context *ctx)
1966 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1967 return lscsa->srr0.slot[0];
1969 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1970 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1972 static u64 spufs_id_get(struct spu_context *ctx)
1976 if (ctx->state == SPU_STATE_RUNNABLE)
1977 num = ctx->spu->number;
1979 num = (unsigned int)-1;
1983 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1986 static u64 spufs_object_id_get(struct spu_context *ctx)
1988 /* FIXME: Should there really be no locking here? */
1989 return ctx->object_id;
1992 static int spufs_object_id_set(void *data, u64 id)
1994 struct spu_context *ctx = data;
1995 ctx->object_id = id;
2000 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2001 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
2003 static u64 spufs_lslr_get(struct spu_context *ctx)
2005 return ctx->csa.priv2.spu_lslr_RW;
2007 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2008 SPU_ATTR_ACQUIRE_SAVED);
2010 static int spufs_info_open(struct inode *inode, struct file *file)
2012 struct spufs_inode_info *i = SPUFS_I(inode);
2013 struct spu_context *ctx = i->i_ctx;
2014 file->private_data = ctx;
2018 static int spufs_caps_show(struct seq_file *s, void *private)
2020 struct spu_context *ctx = s->private;
2022 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2023 seq_puts(s, "sched\n");
2024 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2025 seq_puts(s, "step\n");
2029 static int spufs_caps_open(struct inode *inode, struct file *file)
2031 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2034 static const struct file_operations spufs_caps_fops = {
2035 .open = spufs_caps_open,
2037 .llseek = seq_lseek,
2038 .release = single_release,
2041 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2042 char __user *buf, size_t len, loff_t *pos)
2046 /* EOF if there's no entry in the mbox */
2047 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2050 data = ctx->csa.prob.pu_mb_R;
2052 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2055 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2056 size_t len, loff_t *pos)
2059 struct spu_context *ctx = file->private_data;
2061 if (!access_ok(VERIFY_WRITE, buf, len))
2064 ret = spu_acquire_saved(ctx);
2067 spin_lock(&ctx->csa.register_lock);
2068 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
2069 spin_unlock(&ctx->csa.register_lock);
2070 spu_release_saved(ctx);
2075 static const struct file_operations spufs_mbox_info_fops = {
2076 .open = spufs_info_open,
2077 .read = spufs_mbox_info_read,
2078 .llseek = generic_file_llseek,
2081 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2082 char __user *buf, size_t len, loff_t *pos)
2086 /* EOF if there's no entry in the ibox */
2087 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2090 data = ctx->csa.priv2.puint_mb_R;
2092 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2095 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2096 size_t len, loff_t *pos)
2098 struct spu_context *ctx = file->private_data;
2101 if (!access_ok(VERIFY_WRITE, buf, len))
2104 ret = spu_acquire_saved(ctx);
2107 spin_lock(&ctx->csa.register_lock);
2108 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
2109 spin_unlock(&ctx->csa.register_lock);
2110 spu_release_saved(ctx);
2115 static const struct file_operations spufs_ibox_info_fops = {
2116 .open = spufs_info_open,
2117 .read = spufs_ibox_info_read,
2118 .llseek = generic_file_llseek,
2121 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2122 char __user *buf, size_t len, loff_t *pos)
2128 wbox_stat = ctx->csa.prob.mb_stat_R;
2129 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2130 for (i = 0; i < cnt; i++) {
2131 data[i] = ctx->csa.spu_mailbox_data[i];
2134 return simple_read_from_buffer(buf, len, pos, &data,
2138 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2139 size_t len, loff_t *pos)
2141 struct spu_context *ctx = file->private_data;
2144 if (!access_ok(VERIFY_WRITE, buf, len))
2147 ret = spu_acquire_saved(ctx);
2150 spin_lock(&ctx->csa.register_lock);
2151 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
2152 spin_unlock(&ctx->csa.register_lock);
2153 spu_release_saved(ctx);
2158 static const struct file_operations spufs_wbox_info_fops = {
2159 .open = spufs_info_open,
2160 .read = spufs_wbox_info_read,
2161 .llseek = generic_file_llseek,
2164 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2165 char __user *buf, size_t len, loff_t *pos)
2167 struct spu_dma_info info;
2168 struct mfc_cq_sr *qp, *spuqp;
2171 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2172 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2173 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2174 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2175 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2176 for (i = 0; i < 16; i++) {
2177 qp = &info.dma_info_command_data[i];
2178 spuqp = &ctx->csa.priv2.spuq[i];
2180 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2181 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2182 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2183 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2186 return simple_read_from_buffer(buf, len, pos, &info,
2190 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2191 size_t len, loff_t *pos)
2193 struct spu_context *ctx = file->private_data;
2196 if (!access_ok(VERIFY_WRITE, buf, len))
2199 ret = spu_acquire_saved(ctx);
2202 spin_lock(&ctx->csa.register_lock);
2203 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2204 spin_unlock(&ctx->csa.register_lock);
2205 spu_release_saved(ctx);
2210 static const struct file_operations spufs_dma_info_fops = {
2211 .open = spufs_info_open,
2212 .read = spufs_dma_info_read,
2215 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2216 char __user *buf, size_t len, loff_t *pos)
2218 struct spu_proxydma_info info;
2219 struct mfc_cq_sr *qp, *puqp;
2220 int ret = sizeof info;
2226 if (!access_ok(VERIFY_WRITE, buf, len))
2229 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2230 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2231 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2232 for (i = 0; i < 8; i++) {
2233 qp = &info.proxydma_info_command_data[i];
2234 puqp = &ctx->csa.priv2.puq[i];
2236 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2237 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2238 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2239 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2242 return simple_read_from_buffer(buf, len, pos, &info,
2246 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2247 size_t len, loff_t *pos)
2249 struct spu_context *ctx = file->private_data;
2252 ret = spu_acquire_saved(ctx);
2255 spin_lock(&ctx->csa.register_lock);
2256 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2257 spin_unlock(&ctx->csa.register_lock);
2258 spu_release_saved(ctx);
2263 static const struct file_operations spufs_proxydma_info_fops = {
2264 .open = spufs_info_open,
2265 .read = spufs_proxydma_info_read,
2268 static int spufs_show_tid(struct seq_file *s, void *private)
2270 struct spu_context *ctx = s->private;
2272 seq_printf(s, "%d\n", ctx->tid);
2276 static int spufs_tid_open(struct inode *inode, struct file *file)
2278 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2281 static const struct file_operations spufs_tid_fops = {
2282 .open = spufs_tid_open,
2284 .llseek = seq_lseek,
2285 .release = single_release,
2288 static const char *ctx_state_names[] = {
2289 "user", "system", "iowait", "loaded"
2292 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2293 enum spu_utilization_state state)
2296 unsigned long long time = ctx->stats.times[state];
2299 * In general, utilization statistics are updated by the controlling
2300 * thread as the spu context moves through various well defined
2301 * state transitions, but if the context is lazily loaded its
2302 * utilization statistics are not updated as the controlling thread
2303 * is not tightly coupled with the execution of the spu context. We
2304 * calculate and apply the time delta from the last recorded state
2305 * of the spu context.
2307 if (ctx->spu && ctx->stats.util_state == state) {
2309 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2312 return time / NSEC_PER_MSEC;
2315 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2317 unsigned long long slb_flts = ctx->stats.slb_flt;
2319 if (ctx->state == SPU_STATE_RUNNABLE) {
2320 slb_flts += (ctx->spu->stats.slb_flt -
2321 ctx->stats.slb_flt_base);
2327 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2329 unsigned long long class2_intrs = ctx->stats.class2_intr;
2331 if (ctx->state == SPU_STATE_RUNNABLE) {
2332 class2_intrs += (ctx->spu->stats.class2_intr -
2333 ctx->stats.class2_intr_base);
2336 return class2_intrs;
2340 static int spufs_show_stat(struct seq_file *s, void *private)
2342 struct spu_context *ctx = s->private;
2345 ret = spu_acquire(ctx);
2349 seq_printf(s, "%s %llu %llu %llu %llu "
2350 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2351 ctx_state_names[ctx->stats.util_state],
2352 spufs_acct_time(ctx, SPU_UTIL_USER),
2353 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2354 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2355 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2356 ctx->stats.vol_ctx_switch,
2357 ctx->stats.invol_ctx_switch,
2358 spufs_slb_flts(ctx),
2359 ctx->stats.hash_flt,
2362 spufs_class2_intrs(ctx),
2363 ctx->stats.libassist);
2368 static int spufs_stat_open(struct inode *inode, struct file *file)
2370 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2373 static const struct file_operations spufs_stat_fops = {
2374 .open = spufs_stat_open,
2376 .llseek = seq_lseek,
2377 .release = single_release,
2381 struct tree_descr spufs_dir_contents[] = {
2382 { "capabilities", &spufs_caps_fops, 0444, },
2383 { "mem", &spufs_mem_fops, 0666, },
2384 { "regs", &spufs_regs_fops, 0666, },
2385 { "mbox", &spufs_mbox_fops, 0444, },
2386 { "ibox", &spufs_ibox_fops, 0444, },
2387 { "wbox", &spufs_wbox_fops, 0222, },
2388 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2389 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2390 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2391 { "signal1", &spufs_signal1_fops, 0666, },
2392 { "signal2", &spufs_signal2_fops, 0666, },
2393 { "signal1_type", &spufs_signal1_type, 0666, },
2394 { "signal2_type", &spufs_signal2_type, 0666, },
2395 { "cntl", &spufs_cntl_fops, 0666, },
2396 { "fpcr", &spufs_fpcr_fops, 0666, },
2397 { "lslr", &spufs_lslr_ops, 0444, },
2398 { "mfc", &spufs_mfc_fops, 0666, },
2399 { "mss", &spufs_mss_fops, 0666, },
2400 { "npc", &spufs_npc_ops, 0666, },
2401 { "srr0", &spufs_srr0_ops, 0666, },
2402 { "decr", &spufs_decr_ops, 0666, },
2403 { "decr_status", &spufs_decr_status_ops, 0666, },
2404 { "event_mask", &spufs_event_mask_ops, 0666, },
2405 { "event_status", &spufs_event_status_ops, 0444, },
2406 { "psmap", &spufs_psmap_fops, 0666, },
2407 { "phys-id", &spufs_id_ops, 0666, },
2408 { "object-id", &spufs_object_id_ops, 0666, },
2409 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2410 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2411 { "wbox_info", &spufs_wbox_info_fops, 0444, },
2412 { "dma_info", &spufs_dma_info_fops, 0444, },
2413 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
2414 { "tid", &spufs_tid_fops, 0444, },
2415 { "stat", &spufs_stat_fops, 0444, },
2419 struct tree_descr spufs_dir_nosched_contents[] = {
2420 { "capabilities", &spufs_caps_fops, 0444, },
2421 { "mem", &spufs_mem_fops, 0666, },
2422 { "mbox", &spufs_mbox_fops, 0444, },
2423 { "ibox", &spufs_ibox_fops, 0444, },
2424 { "wbox", &spufs_wbox_fops, 0222, },
2425 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2426 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2427 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2428 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2429 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2430 { "signal1_type", &spufs_signal1_type, 0666, },
2431 { "signal2_type", &spufs_signal2_type, 0666, },
2432 { "mss", &spufs_mss_fops, 0666, },
2433 { "mfc", &spufs_mfc_fops, 0666, },
2434 { "cntl", &spufs_cntl_fops, 0666, },
2435 { "npc", &spufs_npc_ops, 0666, },
2436 { "psmap", &spufs_psmap_fops, 0666, },
2437 { "phys-id", &spufs_id_ops, 0666, },
2438 { "object-id", &spufs_object_id_ops, 0666, },
2439 { "tid", &spufs_tid_fops, 0444, },
2440 { "stat", &spufs_stat_fops, 0444, },
2444 struct spufs_coredump_reader spufs_coredump_read[] = {
2445 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2446 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2447 { "lslr", NULL, spufs_lslr_get, 19 },
2448 { "decr", NULL, spufs_decr_get, 19 },
2449 { "decr_status", NULL, spufs_decr_status_get, 19 },
2450 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2451 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2452 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
2453 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2454 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2455 { "event_mask", NULL, spufs_event_mask_get, 19 },
2456 { "event_status", NULL, spufs_event_status_get, 19 },
2457 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2458 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2459 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2460 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2461 { "proxydma_info", __spufs_proxydma_info_read,
2462 NULL, sizeof(struct spu_proxydma_info)},
2463 { "object-id", NULL, spufs_object_id_get, 19 },
2464 { "npc", NULL, spufs_npc_get, 19 },