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>
33 #include <asm/semaphore.h>
35 #include <asm/uaccess.h>
39 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
43 spufs_mem_open(struct inode *inode, struct file *file)
45 struct spufs_inode_info *i = SPUFS_I(inode);
46 struct spu_context *ctx = i->i_ctx;
47 file->private_data = ctx;
48 file->f_mapping = inode->i_mapping;
49 ctx->local_store = inode->i_mapping;
54 spufs_mem_read(struct file *file, char __user *buffer,
55 size_t size, loff_t *pos)
57 struct spu_context *ctx = file->private_data;
63 local_store = ctx->ops->get_ls(ctx);
64 ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE);
71 spufs_mem_write(struct file *file, const char __user *buffer,
72 size_t size, loff_t *pos)
74 struct spu_context *ctx = file->private_data;
78 size = min_t(ssize_t, LS_SIZE - *pos, size);
85 local_store = ctx->ops->get_ls(ctx);
86 ret = copy_from_user(local_store + *pos - size,
87 buffer, size) ? -EFAULT : size;
94 spufs_mem_mmap_nopage(struct vm_area_struct *vma,
95 unsigned long address, int *type)
97 struct page *page = NOPAGE_SIGBUS;
99 struct spu_context *ctx = vma->vm_file->private_data;
100 unsigned long offset = address - vma->vm_start;
101 offset += vma->vm_pgoff << PAGE_SHIFT;
105 if (ctx->state == SPU_STATE_SAVED) {
106 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
107 & ~(_PAGE_NO_CACHE | _PAGE_GUARDED));
108 page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
110 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
111 | _PAGE_NO_CACHE | _PAGE_GUARDED);
112 page = pfn_to_page((ctx->spu->local_store_phys + offset)
118 *type = VM_FAULT_MINOR;
120 page_cache_get(page);
124 static struct vm_operations_struct spufs_mem_mmap_vmops = {
125 .nopage = spufs_mem_mmap_nopage,
129 spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
131 if (!(vma->vm_flags & VM_SHARED))
135 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
138 vma->vm_ops = &spufs_mem_mmap_vmops;
142 static struct file_operations spufs_mem_fops = {
143 .open = spufs_mem_open,
144 .read = spufs_mem_read,
145 .write = spufs_mem_write,
146 .llseek = generic_file_llseek,
147 .mmap = spufs_mem_mmap,
150 static struct page *spufs_ps_nopage(struct vm_area_struct *vma,
151 unsigned long address,
152 int *type, unsigned long ps_offs,
153 unsigned long ps_size)
155 struct page *page = NOPAGE_SIGBUS;
156 int fault_type = VM_FAULT_SIGBUS;
157 struct spu_context *ctx = vma->vm_file->private_data;
158 unsigned long offset = address - vma->vm_start;
162 offset += vma->vm_pgoff << PAGE_SHIFT;
163 if (offset >= ps_size)
166 ret = spu_acquire_runnable(ctx);
170 area = ctx->spu->problem_phys + ps_offs;
171 page = pfn_to_page((area + offset) >> PAGE_SHIFT);
172 fault_type = VM_FAULT_MINOR;
173 page_cache_get(page);
185 static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma,
186 unsigned long address, int *type)
188 return spufs_ps_nopage(vma, address, type, 0x4000, 0x1000);
191 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
192 .nopage = spufs_cntl_mmap_nopage,
196 * mmap support for problem state control area [0x4000 - 0x4fff].
198 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
200 if (!(vma->vm_flags & VM_SHARED))
203 vma->vm_flags |= VM_RESERVED;
204 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
205 | _PAGE_NO_CACHE | _PAGE_GUARDED);
207 vma->vm_ops = &spufs_cntl_mmap_vmops;
210 #else /* SPUFS_MMAP_4K */
211 #define spufs_cntl_mmap NULL
212 #endif /* !SPUFS_MMAP_4K */
214 static int spufs_cntl_open(struct inode *inode, struct file *file)
216 struct spufs_inode_info *i = SPUFS_I(inode);
217 struct spu_context *ctx = i->i_ctx;
219 file->private_data = ctx;
220 file->f_mapping = inode->i_mapping;
221 ctx->cntl = inode->i_mapping;
226 spufs_cntl_read(struct file *file, char __user *buffer,
227 size_t size, loff_t *pos)
229 /* FIXME: read from spu status */
234 spufs_cntl_write(struct file *file, const char __user *buffer,
235 size_t size, loff_t *pos)
237 /* FIXME: write to runctl bit */
241 static struct file_operations spufs_cntl_fops = {
242 .open = spufs_cntl_open,
243 .read = spufs_cntl_read,
244 .write = spufs_cntl_write,
245 .mmap = spufs_cntl_mmap,
249 spufs_regs_open(struct inode *inode, struct file *file)
251 struct spufs_inode_info *i = SPUFS_I(inode);
252 file->private_data = i->i_ctx;
257 spufs_regs_read(struct file *file, char __user *buffer,
258 size_t size, loff_t *pos)
260 struct spu_context *ctx = file->private_data;
261 struct spu_lscsa *lscsa = ctx->csa.lscsa;
264 spu_acquire_saved(ctx);
266 ret = simple_read_from_buffer(buffer, size, pos,
267 lscsa->gprs, sizeof lscsa->gprs);
274 spufs_regs_write(struct file *file, const char __user *buffer,
275 size_t size, loff_t *pos)
277 struct spu_context *ctx = file->private_data;
278 struct spu_lscsa *lscsa = ctx->csa.lscsa;
281 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
286 spu_acquire_saved(ctx);
288 ret = copy_from_user(lscsa->gprs + *pos - size,
289 buffer, size) ? -EFAULT : size;
295 static struct file_operations spufs_regs_fops = {
296 .open = spufs_regs_open,
297 .read = spufs_regs_read,
298 .write = spufs_regs_write,
299 .llseek = generic_file_llseek,
303 spufs_fpcr_read(struct file *file, char __user * buffer,
304 size_t size, loff_t * pos)
306 struct spu_context *ctx = file->private_data;
307 struct spu_lscsa *lscsa = ctx->csa.lscsa;
310 spu_acquire_saved(ctx);
312 ret = simple_read_from_buffer(buffer, size, pos,
313 &lscsa->fpcr, sizeof(lscsa->fpcr));
320 spufs_fpcr_write(struct file *file, const char __user * buffer,
321 size_t size, loff_t * pos)
323 struct spu_context *ctx = file->private_data;
324 struct spu_lscsa *lscsa = ctx->csa.lscsa;
327 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
332 spu_acquire_saved(ctx);
334 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
335 buffer, size) ? -EFAULT : size;
341 static struct file_operations spufs_fpcr_fops = {
342 .open = spufs_regs_open,
343 .read = spufs_fpcr_read,
344 .write = spufs_fpcr_write,
345 .llseek = generic_file_llseek,
348 /* generic open function for all pipe-like files */
349 static int spufs_pipe_open(struct inode *inode, struct file *file)
351 struct spufs_inode_info *i = SPUFS_I(inode);
352 file->private_data = i->i_ctx;
354 return nonseekable_open(inode, file);
357 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
358 size_t len, loff_t *pos)
360 struct spu_context *ctx = file->private_data;
368 ret = ctx->ops->mbox_read(ctx, &mbox_data);
374 if (copy_to_user(buf, &mbox_data, sizeof mbox_data))
380 static struct file_operations spufs_mbox_fops = {
381 .open = spufs_pipe_open,
382 .read = spufs_mbox_read,
385 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
386 size_t len, loff_t *pos)
388 struct spu_context *ctx = file->private_data;
396 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
400 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
406 static struct file_operations spufs_mbox_stat_fops = {
407 .open = spufs_pipe_open,
408 .read = spufs_mbox_stat_read,
411 /* low-level ibox access function */
412 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
414 return ctx->ops->ibox_read(ctx, data);
417 static int spufs_ibox_fasync(int fd, struct file *file, int on)
419 struct spu_context *ctx = file->private_data;
421 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
424 /* interrupt-level ibox callback function. */
425 void spufs_ibox_callback(struct spu *spu)
427 struct spu_context *ctx = spu->ctx;
429 wake_up_all(&ctx->ibox_wq);
430 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
433 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
434 size_t len, loff_t *pos)
436 struct spu_context *ctx = file->private_data;
446 if (file->f_flags & O_NONBLOCK) {
447 if (!spu_ibox_read(ctx, &ibox_data))
450 ret = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
459 if (copy_to_user(buf, &ibox_data, sizeof ibox_data))
465 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
467 struct spu_context *ctx = file->private_data;
470 poll_wait(file, &ctx->ibox_wq, wait);
473 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
479 static struct file_operations spufs_ibox_fops = {
480 .open = spufs_pipe_open,
481 .read = spufs_ibox_read,
482 .poll = spufs_ibox_poll,
483 .fasync = spufs_ibox_fasync,
486 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
487 size_t len, loff_t *pos)
489 struct spu_context *ctx = file->private_data;
496 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
499 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
505 static struct file_operations spufs_ibox_stat_fops = {
506 .open = spufs_pipe_open,
507 .read = spufs_ibox_stat_read,
510 /* low-level mailbox write */
511 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
513 return ctx->ops->wbox_write(ctx, data);
516 static int spufs_wbox_fasync(int fd, struct file *file, int on)
518 struct spu_context *ctx = file->private_data;
521 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
526 /* interrupt-level wbox callback function. */
527 void spufs_wbox_callback(struct spu *spu)
529 struct spu_context *ctx = spu->ctx;
531 wake_up_all(&ctx->wbox_wq);
532 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
535 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
536 size_t len, loff_t *pos)
538 struct spu_context *ctx = file->private_data;
545 if (copy_from_user(&wbox_data, buf, sizeof wbox_data))
551 if (file->f_flags & O_NONBLOCK) {
552 if (!spu_wbox_write(ctx, wbox_data))
555 ret = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
560 return ret ? ret : sizeof wbox_data;
563 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
565 struct spu_context *ctx = file->private_data;
568 poll_wait(file, &ctx->wbox_wq, wait);
571 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
577 static struct file_operations spufs_wbox_fops = {
578 .open = spufs_pipe_open,
579 .write = spufs_wbox_write,
580 .poll = spufs_wbox_poll,
581 .fasync = spufs_wbox_fasync,
584 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
585 size_t len, loff_t *pos)
587 struct spu_context *ctx = file->private_data;
594 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
597 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
603 static struct file_operations spufs_wbox_stat_fops = {
604 .open = spufs_pipe_open,
605 .read = spufs_wbox_stat_read,
608 static int spufs_signal1_open(struct inode *inode, struct file *file)
610 struct spufs_inode_info *i = SPUFS_I(inode);
611 struct spu_context *ctx = i->i_ctx;
612 file->private_data = ctx;
613 file->f_mapping = inode->i_mapping;
614 ctx->signal1 = inode->i_mapping;
615 return nonseekable_open(inode, file);
618 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
619 size_t len, loff_t *pos)
621 struct spu_context *ctx = file->private_data;
628 data = ctx->ops->signal1_read(ctx);
631 if (copy_to_user(buf, &data, 4))
637 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
638 size_t len, loff_t *pos)
640 struct spu_context *ctx;
643 ctx = file->private_data;
648 if (copy_from_user(&data, buf, 4))
652 ctx->ops->signal1_write(ctx, data);
658 static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
659 unsigned long address, int *type)
661 #if PAGE_SIZE == 0x1000
662 return spufs_ps_nopage(vma, address, type, 0x14000, 0x1000);
663 #elif PAGE_SIZE == 0x10000
664 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
665 * signal 1 and 2 area
667 return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
669 #error unsupported page size
673 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
674 .nopage = spufs_signal1_mmap_nopage,
677 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
679 if (!(vma->vm_flags & VM_SHARED))
682 vma->vm_flags |= VM_RESERVED;
683 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
684 | _PAGE_NO_CACHE | _PAGE_GUARDED);
686 vma->vm_ops = &spufs_signal1_mmap_vmops;
690 static struct file_operations spufs_signal1_fops = {
691 .open = spufs_signal1_open,
692 .read = spufs_signal1_read,
693 .write = spufs_signal1_write,
694 .mmap = spufs_signal1_mmap,
697 static int spufs_signal2_open(struct inode *inode, struct file *file)
699 struct spufs_inode_info *i = SPUFS_I(inode);
700 struct spu_context *ctx = i->i_ctx;
701 file->private_data = ctx;
702 file->f_mapping = inode->i_mapping;
703 ctx->signal2 = inode->i_mapping;
704 return nonseekable_open(inode, file);
707 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
708 size_t len, loff_t *pos)
710 struct spu_context *ctx;
713 ctx = file->private_data;
719 data = ctx->ops->signal2_read(ctx);
722 if (copy_to_user(buf, &data, 4))
728 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
729 size_t len, loff_t *pos)
731 struct spu_context *ctx;
734 ctx = file->private_data;
739 if (copy_from_user(&data, buf, 4))
743 ctx->ops->signal2_write(ctx, data);
750 static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
751 unsigned long address, int *type)
753 #if PAGE_SIZE == 0x1000
754 return spufs_ps_nopage(vma, address, type, 0x1c000, 0x1000);
755 #elif PAGE_SIZE == 0x10000
756 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
757 * signal 1 and 2 area
759 return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
761 #error unsupported page size
765 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
766 .nopage = spufs_signal2_mmap_nopage,
769 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
771 if (!(vma->vm_flags & VM_SHARED))
775 vma->vm_flags |= VM_RESERVED;
776 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
777 | _PAGE_NO_CACHE | _PAGE_GUARDED);
779 vma->vm_ops = &spufs_signal2_mmap_vmops;
782 #else /* SPUFS_MMAP_4K */
783 #define spufs_signal2_mmap NULL
784 #endif /* !SPUFS_MMAP_4K */
786 static struct file_operations spufs_signal2_fops = {
787 .open = spufs_signal2_open,
788 .read = spufs_signal2_read,
789 .write = spufs_signal2_write,
790 .mmap = spufs_signal2_mmap,
793 static void spufs_signal1_type_set(void *data, u64 val)
795 struct spu_context *ctx = data;
798 ctx->ops->signal1_type_set(ctx, val);
802 static u64 spufs_signal1_type_get(void *data)
804 struct spu_context *ctx = data;
808 ret = ctx->ops->signal1_type_get(ctx);
813 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
814 spufs_signal1_type_set, "%llu");
816 static void spufs_signal2_type_set(void *data, u64 val)
818 struct spu_context *ctx = data;
821 ctx->ops->signal2_type_set(ctx, val);
825 static u64 spufs_signal2_type_get(void *data)
827 struct spu_context *ctx = data;
831 ret = ctx->ops->signal2_type_get(ctx);
836 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
837 spufs_signal2_type_set, "%llu");
840 static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
841 unsigned long address, int *type)
843 return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000);
846 static struct vm_operations_struct spufs_mss_mmap_vmops = {
847 .nopage = spufs_mss_mmap_nopage,
851 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
853 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
855 if (!(vma->vm_flags & VM_SHARED))
858 vma->vm_flags |= VM_RESERVED;
859 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
860 | _PAGE_NO_CACHE | _PAGE_GUARDED);
862 vma->vm_ops = &spufs_mss_mmap_vmops;
865 #else /* SPUFS_MMAP_4K */
866 #define spufs_mss_mmap NULL
867 #endif /* !SPUFS_MMAP_4K */
869 static int spufs_mss_open(struct inode *inode, struct file *file)
871 struct spufs_inode_info *i = SPUFS_I(inode);
873 file->private_data = i->i_ctx;
874 return nonseekable_open(inode, file);
877 static struct file_operations spufs_mss_fops = {
878 .open = spufs_mss_open,
879 .mmap = spufs_mss_mmap,
882 static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma,
883 unsigned long address, int *type)
885 return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000);
888 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
889 .nopage = spufs_psmap_mmap_nopage,
893 * mmap support for full problem state area [0x00000 - 0x1ffff].
895 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
897 if (!(vma->vm_flags & VM_SHARED))
900 vma->vm_flags |= VM_RESERVED;
901 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
902 | _PAGE_NO_CACHE | _PAGE_GUARDED);
904 vma->vm_ops = &spufs_psmap_mmap_vmops;
908 static int spufs_psmap_open(struct inode *inode, struct file *file)
910 struct spufs_inode_info *i = SPUFS_I(inode);
912 file->private_data = i->i_ctx;
913 return nonseekable_open(inode, file);
916 static struct file_operations spufs_psmap_fops = {
917 .open = spufs_psmap_open,
918 .mmap = spufs_psmap_mmap,
923 static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
924 unsigned long address, int *type)
926 return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000);
929 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
930 .nopage = spufs_mfc_mmap_nopage,
934 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
936 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
938 if (!(vma->vm_flags & VM_SHARED))
941 vma->vm_flags |= VM_RESERVED;
942 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
943 | _PAGE_NO_CACHE | _PAGE_GUARDED);
945 vma->vm_ops = &spufs_mfc_mmap_vmops;
948 #else /* SPUFS_MMAP_4K */
949 #define spufs_mfc_mmap NULL
950 #endif /* !SPUFS_MMAP_4K */
952 static int spufs_mfc_open(struct inode *inode, struct file *file)
954 struct spufs_inode_info *i = SPUFS_I(inode);
955 struct spu_context *ctx = i->i_ctx;
957 /* we don't want to deal with DMA into other processes */
958 if (ctx->owner != current->mm)
961 if (atomic_read(&inode->i_count) != 1)
964 file->private_data = ctx;
965 return nonseekable_open(inode, file);
968 /* interrupt-level mfc callback function. */
969 void spufs_mfc_callback(struct spu *spu)
971 struct spu_context *ctx = spu->ctx;
973 wake_up_all(&ctx->mfc_wq);
975 pr_debug("%s %s\n", __FUNCTION__, spu->name);
976 if (ctx->mfc_fasync) {
977 u32 free_elements, tagstatus;
980 /* no need for spu_acquire in interrupt context */
981 free_elements = ctx->ops->get_mfc_free_elements(ctx);
982 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
985 if (free_elements & 0xffff)
987 if (tagstatus & ctx->tagwait)
990 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
994 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
996 /* See if there is one tag group is complete */
997 /* FIXME we need locking around tagwait */
998 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
999 ctx->tagwait &= ~*status;
1003 /* enable interrupt waiting for any tag group,
1004 may silently fail if interrupts are already enabled */
1005 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1009 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1010 size_t size, loff_t *pos)
1012 struct spu_context *ctx = file->private_data;
1020 if (file->f_flags & O_NONBLOCK) {
1021 status = ctx->ops->read_mfc_tagstatus(ctx);
1022 if (!(status & ctx->tagwait))
1025 ctx->tagwait &= ~status;
1027 ret = spufs_wait(ctx->mfc_wq,
1028 spufs_read_mfc_tagstatus(ctx, &status));
1036 if (copy_to_user(buffer, &status, 4))
1043 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1045 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1046 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1057 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1061 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1062 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1067 switch (cmd->size & 0xf) {
1088 pr_debug("invalid DMA alignment %x for size %x\n",
1089 cmd->lsa & 0xf, cmd->size);
1093 if (cmd->size > 16 * 1024) {
1094 pr_debug("invalid DMA size %x\n", cmd->size);
1098 if (cmd->tag & 0xfff0) {
1099 /* we reserve the higher tag numbers for kernel use */
1100 pr_debug("invalid DMA tag\n");
1105 /* not supported in this version */
1106 pr_debug("invalid DMA class\n");
1113 static int spu_send_mfc_command(struct spu_context *ctx,
1114 struct mfc_dma_command cmd,
1117 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1118 if (*error == -EAGAIN) {
1119 /* wait for any tag group to complete
1120 so we have space for the new command */
1121 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1122 /* try again, because the queue might be
1124 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1125 if (*error == -EAGAIN)
1131 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1132 size_t size, loff_t *pos)
1134 struct spu_context *ctx = file->private_data;
1135 struct mfc_dma_command cmd;
1138 if (size != sizeof cmd)
1142 if (copy_from_user(&cmd, buffer, sizeof cmd))
1145 ret = spufs_check_valid_dma(&cmd);
1149 spu_acquire_runnable(ctx);
1150 if (file->f_flags & O_NONBLOCK) {
1151 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1154 ret = spufs_wait(ctx->mfc_wq,
1155 spu_send_mfc_command(ctx, cmd, &status));
1164 ctx->tagwait |= 1 << cmd.tag;
1170 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1172 struct spu_context *ctx = file->private_data;
1173 u32 free_elements, tagstatus;
1177 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1178 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1179 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1182 poll_wait(file, &ctx->mfc_wq, wait);
1185 if (free_elements & 0xffff)
1186 mask |= POLLOUT | POLLWRNORM;
1187 if (tagstatus & ctx->tagwait)
1188 mask |= POLLIN | POLLRDNORM;
1190 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1191 free_elements, tagstatus, ctx->tagwait);
1196 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1198 struct spu_context *ctx = file->private_data;
1203 /* this currently hangs */
1204 ret = spufs_wait(ctx->mfc_wq,
1205 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1208 ret = spufs_wait(ctx->mfc_wq,
1209 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1219 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1222 return spufs_mfc_flush(file, NULL);
1225 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1227 struct spu_context *ctx = file->private_data;
1229 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1232 static struct file_operations spufs_mfc_fops = {
1233 .open = spufs_mfc_open,
1234 .read = spufs_mfc_read,
1235 .write = spufs_mfc_write,
1236 .poll = spufs_mfc_poll,
1237 .flush = spufs_mfc_flush,
1238 .fsync = spufs_mfc_fsync,
1239 .fasync = spufs_mfc_fasync,
1240 .mmap = spufs_mfc_mmap,
1243 static void spufs_npc_set(void *data, u64 val)
1245 struct spu_context *ctx = data;
1247 ctx->ops->npc_write(ctx, val);
1251 static u64 spufs_npc_get(void *data)
1253 struct spu_context *ctx = data;
1256 ret = ctx->ops->npc_read(ctx);
1260 DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
1262 static void spufs_decr_set(void *data, u64 val)
1264 struct spu_context *ctx = data;
1265 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1266 spu_acquire_saved(ctx);
1267 lscsa->decr.slot[0] = (u32) val;
1271 static u64 spufs_decr_get(void *data)
1273 struct spu_context *ctx = data;
1274 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1276 spu_acquire_saved(ctx);
1277 ret = lscsa->decr.slot[0];
1281 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1284 static void spufs_decr_status_set(void *data, u64 val)
1286 struct spu_context *ctx = data;
1287 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1288 spu_acquire_saved(ctx);
1289 lscsa->decr_status.slot[0] = (u32) val;
1293 static u64 spufs_decr_status_get(void *data)
1295 struct spu_context *ctx = data;
1296 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1298 spu_acquire_saved(ctx);
1299 ret = lscsa->decr_status.slot[0];
1303 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1304 spufs_decr_status_set, "%llx\n")
1306 static void spufs_spu_tag_mask_set(void *data, u64 val)
1308 struct spu_context *ctx = data;
1309 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1310 spu_acquire_saved(ctx);
1311 lscsa->tag_mask.slot[0] = (u32) val;
1315 static u64 spufs_spu_tag_mask_get(void *data)
1317 struct spu_context *ctx = data;
1318 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1320 spu_acquire_saved(ctx);
1321 ret = lscsa->tag_mask.slot[0];
1325 DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
1326 spufs_spu_tag_mask_set, "%llx\n")
1328 static void spufs_event_mask_set(void *data, u64 val)
1330 struct spu_context *ctx = data;
1331 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1332 spu_acquire_saved(ctx);
1333 lscsa->event_mask.slot[0] = (u32) val;
1337 static u64 spufs_event_mask_get(void *data)
1339 struct spu_context *ctx = data;
1340 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1342 spu_acquire_saved(ctx);
1343 ret = lscsa->event_mask.slot[0];
1347 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1348 spufs_event_mask_set, "%llx\n")
1350 static void spufs_srr0_set(void *data, u64 val)
1352 struct spu_context *ctx = data;
1353 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1354 spu_acquire_saved(ctx);
1355 lscsa->srr0.slot[0] = (u32) val;
1359 static u64 spufs_srr0_get(void *data)
1361 struct spu_context *ctx = data;
1362 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1364 spu_acquire_saved(ctx);
1365 ret = lscsa->srr0.slot[0];
1369 DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1372 static u64 spufs_id_get(void *data)
1374 struct spu_context *ctx = data;
1378 if (ctx->state == SPU_STATE_RUNNABLE)
1379 num = ctx->spu->number;
1381 num = (unsigned int)-1;
1386 DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
1388 struct tree_descr spufs_dir_contents[] = {
1389 { "mem", &spufs_mem_fops, 0666, },
1390 { "regs", &spufs_regs_fops, 0666, },
1391 { "mbox", &spufs_mbox_fops, 0444, },
1392 { "ibox", &spufs_ibox_fops, 0444, },
1393 { "wbox", &spufs_wbox_fops, 0222, },
1394 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
1395 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
1396 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
1397 { "signal1", &spufs_signal1_fops, 0666, },
1398 { "signal2", &spufs_signal2_fops, 0666, },
1399 { "signal1_type", &spufs_signal1_type, 0666, },
1400 { "signal2_type", &spufs_signal2_type, 0666, },
1401 { "mss", &spufs_mss_fops, 0666, },
1402 { "mfc", &spufs_mfc_fops, 0666, },
1403 { "cntl", &spufs_cntl_fops, 0666, },
1404 { "npc", &spufs_npc_ops, 0666, },
1405 { "fpcr", &spufs_fpcr_fops, 0666, },
1406 { "decr", &spufs_decr_ops, 0666, },
1407 { "decr_status", &spufs_decr_status_ops, 0666, },
1408 { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
1409 { "event_mask", &spufs_event_mask_ops, 0666, },
1410 { "srr0", &spufs_srr0_ops, 0666, },
1411 { "phys-id", &spufs_id_ops, 0666, },
1412 { "psmap", &spufs_psmap_fops, 0666, },