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/spu_info.h>
36 #include <asm/uaccess.h>
40 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
44 spufs_mem_open(struct inode *inode, struct file *file)
46 struct spufs_inode_info *i = SPUFS_I(inode);
47 struct spu_context *ctx = i->i_ctx;
48 file->private_data = ctx;
49 file->f_mapping = inode->i_mapping;
50 ctx->local_store = inode->i_mapping;
55 spufs_mem_read(struct file *file, char __user *buffer,
56 size_t size, loff_t *pos)
58 struct spu_context *ctx = file->private_data;
64 local_store = ctx->ops->get_ls(ctx);
65 ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE);
72 spufs_mem_write(struct file *file, const char __user *buffer,
73 size_t size, loff_t *pos)
75 struct spu_context *ctx = file->private_data;
79 size = min_t(ssize_t, LS_SIZE - *pos, size);
86 local_store = ctx->ops->get_ls(ctx);
87 ret = copy_from_user(local_store + *pos - size,
88 buffer, size) ? -EFAULT : size;
95 spufs_mem_mmap_nopage(struct vm_area_struct *vma,
96 unsigned long address, int *type)
98 struct page *page = NOPAGE_SIGBUS;
100 struct spu_context *ctx = vma->vm_file->private_data;
101 unsigned long offset = address - vma->vm_start;
102 offset += vma->vm_pgoff << PAGE_SHIFT;
106 if (ctx->state == SPU_STATE_SAVED) {
107 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
108 & ~(_PAGE_NO_CACHE | _PAGE_GUARDED));
109 page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
111 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
112 | _PAGE_NO_CACHE | _PAGE_GUARDED);
113 page = pfn_to_page((ctx->spu->local_store_phys + offset)
119 *type = VM_FAULT_MINOR;
121 page_cache_get(page);
125 static struct vm_operations_struct spufs_mem_mmap_vmops = {
126 .nopage = spufs_mem_mmap_nopage,
130 spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
132 if (!(vma->vm_flags & VM_SHARED))
136 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
139 vma->vm_ops = &spufs_mem_mmap_vmops;
143 static struct file_operations spufs_mem_fops = {
144 .open = spufs_mem_open,
145 .read = spufs_mem_read,
146 .write = spufs_mem_write,
147 .llseek = generic_file_llseek,
148 .mmap = spufs_mem_mmap,
151 static struct page *spufs_ps_nopage(struct vm_area_struct *vma,
152 unsigned long address,
153 int *type, unsigned long ps_offs,
154 unsigned long ps_size)
156 struct page *page = NOPAGE_SIGBUS;
157 int fault_type = VM_FAULT_SIGBUS;
158 struct spu_context *ctx = vma->vm_file->private_data;
159 unsigned long offset = address - vma->vm_start;
163 offset += vma->vm_pgoff << PAGE_SHIFT;
164 if (offset >= ps_size)
167 ret = spu_acquire_runnable(ctx);
171 area = ctx->spu->problem_phys + ps_offs;
172 page = pfn_to_page((area + offset) >> PAGE_SHIFT);
173 fault_type = VM_FAULT_MINOR;
174 page_cache_get(page);
186 static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma,
187 unsigned long address, int *type)
189 return spufs_ps_nopage(vma, address, type, 0x4000, 0x1000);
192 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
193 .nopage = spufs_cntl_mmap_nopage,
197 * mmap support for problem state control area [0x4000 - 0x4fff].
199 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
201 if (!(vma->vm_flags & VM_SHARED))
204 vma->vm_flags |= VM_RESERVED;
205 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
206 | _PAGE_NO_CACHE | _PAGE_GUARDED);
208 vma->vm_ops = &spufs_cntl_mmap_vmops;
211 #else /* SPUFS_MMAP_4K */
212 #define spufs_cntl_mmap NULL
213 #endif /* !SPUFS_MMAP_4K */
215 static u64 spufs_cntl_get(void *data)
217 struct spu_context *ctx = data;
221 val = ctx->ops->status_read(ctx);
227 static void spufs_cntl_set(void *data, u64 val)
229 struct spu_context *ctx = data;
232 ctx->ops->runcntl_write(ctx, val);
236 static int spufs_cntl_open(struct inode *inode, struct file *file)
238 struct spufs_inode_info *i = SPUFS_I(inode);
239 struct spu_context *ctx = i->i_ctx;
241 file->private_data = ctx;
242 file->f_mapping = inode->i_mapping;
243 ctx->cntl = inode->i_mapping;
244 return simple_attr_open(inode, file, spufs_cntl_get,
245 spufs_cntl_set, "0x%08lx");
248 static struct file_operations spufs_cntl_fops = {
249 .open = spufs_cntl_open,
250 .release = simple_attr_close,
251 .read = simple_attr_read,
252 .write = simple_attr_write,
253 .mmap = spufs_cntl_mmap,
257 spufs_regs_open(struct inode *inode, struct file *file)
259 struct spufs_inode_info *i = SPUFS_I(inode);
260 file->private_data = i->i_ctx;
265 spufs_regs_read(struct file *file, char __user *buffer,
266 size_t size, loff_t *pos)
268 struct spu_context *ctx = file->private_data;
269 struct spu_lscsa *lscsa = ctx->csa.lscsa;
272 spu_acquire_saved(ctx);
274 ret = simple_read_from_buffer(buffer, size, pos,
275 lscsa->gprs, sizeof lscsa->gprs);
282 spufs_regs_write(struct file *file, const char __user *buffer,
283 size_t size, loff_t *pos)
285 struct spu_context *ctx = file->private_data;
286 struct spu_lscsa *lscsa = ctx->csa.lscsa;
289 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
294 spu_acquire_saved(ctx);
296 ret = copy_from_user(lscsa->gprs + *pos - size,
297 buffer, size) ? -EFAULT : size;
303 static struct file_operations spufs_regs_fops = {
304 .open = spufs_regs_open,
305 .read = spufs_regs_read,
306 .write = spufs_regs_write,
307 .llseek = generic_file_llseek,
311 spufs_fpcr_read(struct file *file, char __user * buffer,
312 size_t size, loff_t * pos)
314 struct spu_context *ctx = file->private_data;
315 struct spu_lscsa *lscsa = ctx->csa.lscsa;
318 spu_acquire_saved(ctx);
320 ret = simple_read_from_buffer(buffer, size, pos,
321 &lscsa->fpcr, sizeof(lscsa->fpcr));
328 spufs_fpcr_write(struct file *file, const char __user * buffer,
329 size_t size, loff_t * pos)
331 struct spu_context *ctx = file->private_data;
332 struct spu_lscsa *lscsa = ctx->csa.lscsa;
335 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
340 spu_acquire_saved(ctx);
342 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
343 buffer, size) ? -EFAULT : size;
349 static struct file_operations spufs_fpcr_fops = {
350 .open = spufs_regs_open,
351 .read = spufs_fpcr_read,
352 .write = spufs_fpcr_write,
353 .llseek = generic_file_llseek,
356 /* generic open function for all pipe-like files */
357 static int spufs_pipe_open(struct inode *inode, struct file *file)
359 struct spufs_inode_info *i = SPUFS_I(inode);
360 file->private_data = i->i_ctx;
362 return nonseekable_open(inode, file);
366 * Read as many bytes from the mailbox as possible, until
367 * one of the conditions becomes true:
369 * - no more data available in the mailbox
370 * - end of the user provided buffer
371 * - end of the mapped area
373 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
374 size_t len, loff_t *pos)
376 struct spu_context *ctx = file->private_data;
377 u32 mbox_data, __user *udata;
383 if (!access_ok(VERIFY_WRITE, buf, len))
386 udata = (void __user *)buf;
389 for (count = 0; (count + 4) <= len; count += 4, udata++) {
391 ret = ctx->ops->mbox_read(ctx, &mbox_data);
396 * at the end of the mapped area, we can fault
397 * but still need to return the data we have
398 * read successfully so far.
400 ret = __put_user(mbox_data, udata);
415 static struct file_operations spufs_mbox_fops = {
416 .open = spufs_pipe_open,
417 .read = spufs_mbox_read,
420 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
421 size_t len, loff_t *pos)
423 struct spu_context *ctx = file->private_data;
431 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
435 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
441 static struct file_operations spufs_mbox_stat_fops = {
442 .open = spufs_pipe_open,
443 .read = spufs_mbox_stat_read,
446 /* low-level ibox access function */
447 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
449 return ctx->ops->ibox_read(ctx, data);
452 static int spufs_ibox_fasync(int fd, struct file *file, int on)
454 struct spu_context *ctx = file->private_data;
456 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
459 /* interrupt-level ibox callback function. */
460 void spufs_ibox_callback(struct spu *spu)
462 struct spu_context *ctx = spu->ctx;
464 wake_up_all(&ctx->ibox_wq);
465 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
469 * Read as many bytes from the interrupt mailbox as possible, until
470 * one of the conditions becomes true:
472 * - no more data available in the mailbox
473 * - end of the user provided buffer
474 * - end of the mapped area
476 * If the file is opened without O_NONBLOCK, we wait here until
477 * any data is available, but return when we have been able to
480 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
481 size_t len, loff_t *pos)
483 struct spu_context *ctx = file->private_data;
484 u32 ibox_data, __user *udata;
490 if (!access_ok(VERIFY_WRITE, buf, len))
493 udata = (void __user *)buf;
497 /* wait only for the first element */
499 if (file->f_flags & O_NONBLOCK) {
500 if (!spu_ibox_read(ctx, &ibox_data))
503 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
508 /* if we can't write at all, return -EFAULT */
509 count = __put_user(ibox_data, udata);
513 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
515 ret = ctx->ops->ibox_read(ctx, &ibox_data);
519 * at the end of the mapped area, we can fault
520 * but still need to return the data we have
521 * read successfully so far.
523 ret = __put_user(ibox_data, udata);
534 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
536 struct spu_context *ctx = file->private_data;
539 poll_wait(file, &ctx->ibox_wq, wait);
542 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
548 static struct file_operations spufs_ibox_fops = {
549 .open = spufs_pipe_open,
550 .read = spufs_ibox_read,
551 .poll = spufs_ibox_poll,
552 .fasync = spufs_ibox_fasync,
555 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
556 size_t len, loff_t *pos)
558 struct spu_context *ctx = file->private_data;
565 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
568 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
574 static struct file_operations spufs_ibox_stat_fops = {
575 .open = spufs_pipe_open,
576 .read = spufs_ibox_stat_read,
579 /* low-level mailbox write */
580 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
582 return ctx->ops->wbox_write(ctx, data);
585 static int spufs_wbox_fasync(int fd, struct file *file, int on)
587 struct spu_context *ctx = file->private_data;
590 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
595 /* interrupt-level wbox callback function. */
596 void spufs_wbox_callback(struct spu *spu)
598 struct spu_context *ctx = spu->ctx;
600 wake_up_all(&ctx->wbox_wq);
601 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
605 * Write as many bytes to the interrupt mailbox as possible, until
606 * one of the conditions becomes true:
608 * - the mailbox is full
609 * - end of the user provided buffer
610 * - end of the mapped area
612 * If the file is opened without O_NONBLOCK, we wait here until
613 * space is availabyl, but return when we have been able to
616 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
617 size_t len, loff_t *pos)
619 struct spu_context *ctx = file->private_data;
620 u32 wbox_data, __user *udata;
626 udata = (void __user *)buf;
627 if (!access_ok(VERIFY_READ, buf, len))
630 if (__get_user(wbox_data, udata))
636 * make sure we can at least write one element, by waiting
637 * in case of !O_NONBLOCK
640 if (file->f_flags & O_NONBLOCK) {
641 if (!spu_wbox_write(ctx, wbox_data))
644 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
650 /* write aѕ much as possible */
651 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
653 ret = __get_user(wbox_data, udata);
657 ret = spu_wbox_write(ctx, wbox_data);
667 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
669 struct spu_context *ctx = file->private_data;
672 poll_wait(file, &ctx->wbox_wq, wait);
675 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
681 static struct file_operations spufs_wbox_fops = {
682 .open = spufs_pipe_open,
683 .write = spufs_wbox_write,
684 .poll = spufs_wbox_poll,
685 .fasync = spufs_wbox_fasync,
688 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
689 size_t len, loff_t *pos)
691 struct spu_context *ctx = file->private_data;
698 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
701 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
707 static struct file_operations spufs_wbox_stat_fops = {
708 .open = spufs_pipe_open,
709 .read = spufs_wbox_stat_read,
712 static int spufs_signal1_open(struct inode *inode, struct file *file)
714 struct spufs_inode_info *i = SPUFS_I(inode);
715 struct spu_context *ctx = i->i_ctx;
716 file->private_data = ctx;
717 file->f_mapping = inode->i_mapping;
718 ctx->signal1 = inode->i_mapping;
719 return nonseekable_open(inode, file);
722 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
723 size_t len, loff_t *pos)
725 struct spu_context *ctx = file->private_data;
732 data = ctx->ops->signal1_read(ctx);
735 if (copy_to_user(buf, &data, 4))
741 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
742 size_t len, loff_t *pos)
744 struct spu_context *ctx;
747 ctx = file->private_data;
752 if (copy_from_user(&data, buf, 4))
756 ctx->ops->signal1_write(ctx, data);
762 static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
763 unsigned long address, int *type)
765 #if PAGE_SIZE == 0x1000
766 return spufs_ps_nopage(vma, address, type, 0x14000, 0x1000);
767 #elif PAGE_SIZE == 0x10000
768 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
769 * signal 1 and 2 area
771 return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
773 #error unsupported page size
777 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
778 .nopage = spufs_signal1_mmap_nopage,
781 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
783 if (!(vma->vm_flags & VM_SHARED))
786 vma->vm_flags |= VM_RESERVED;
787 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
788 | _PAGE_NO_CACHE | _PAGE_GUARDED);
790 vma->vm_ops = &spufs_signal1_mmap_vmops;
794 static struct file_operations spufs_signal1_fops = {
795 .open = spufs_signal1_open,
796 .read = spufs_signal1_read,
797 .write = spufs_signal1_write,
798 .mmap = spufs_signal1_mmap,
801 static int spufs_signal2_open(struct inode *inode, struct file *file)
803 struct spufs_inode_info *i = SPUFS_I(inode);
804 struct spu_context *ctx = i->i_ctx;
805 file->private_data = ctx;
806 file->f_mapping = inode->i_mapping;
807 ctx->signal2 = inode->i_mapping;
808 return nonseekable_open(inode, file);
811 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
812 size_t len, loff_t *pos)
814 struct spu_context *ctx;
817 ctx = file->private_data;
823 data = ctx->ops->signal2_read(ctx);
826 if (copy_to_user(buf, &data, 4))
832 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
833 size_t len, loff_t *pos)
835 struct spu_context *ctx;
838 ctx = file->private_data;
843 if (copy_from_user(&data, buf, 4))
847 ctx->ops->signal2_write(ctx, data);
854 static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
855 unsigned long address, int *type)
857 #if PAGE_SIZE == 0x1000
858 return spufs_ps_nopage(vma, address, type, 0x1c000, 0x1000);
859 #elif PAGE_SIZE == 0x10000
860 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
861 * signal 1 and 2 area
863 return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
865 #error unsupported page size
869 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
870 .nopage = spufs_signal2_mmap_nopage,
873 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
875 if (!(vma->vm_flags & VM_SHARED))
879 vma->vm_flags |= VM_RESERVED;
880 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
881 | _PAGE_NO_CACHE | _PAGE_GUARDED);
883 vma->vm_ops = &spufs_signal2_mmap_vmops;
886 #else /* SPUFS_MMAP_4K */
887 #define spufs_signal2_mmap NULL
888 #endif /* !SPUFS_MMAP_4K */
890 static struct file_operations spufs_signal2_fops = {
891 .open = spufs_signal2_open,
892 .read = spufs_signal2_read,
893 .write = spufs_signal2_write,
894 .mmap = spufs_signal2_mmap,
897 static void spufs_signal1_type_set(void *data, u64 val)
899 struct spu_context *ctx = data;
902 ctx->ops->signal1_type_set(ctx, val);
906 static u64 spufs_signal1_type_get(void *data)
908 struct spu_context *ctx = data;
912 ret = ctx->ops->signal1_type_get(ctx);
917 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
918 spufs_signal1_type_set, "%llu");
920 static void spufs_signal2_type_set(void *data, u64 val)
922 struct spu_context *ctx = data;
925 ctx->ops->signal2_type_set(ctx, val);
929 static u64 spufs_signal2_type_get(void *data)
931 struct spu_context *ctx = data;
935 ret = ctx->ops->signal2_type_get(ctx);
940 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
941 spufs_signal2_type_set, "%llu");
944 static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
945 unsigned long address, int *type)
947 return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000);
950 static struct vm_operations_struct spufs_mss_mmap_vmops = {
951 .nopage = spufs_mss_mmap_nopage,
955 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
957 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
959 if (!(vma->vm_flags & VM_SHARED))
962 vma->vm_flags |= VM_RESERVED;
963 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
964 | _PAGE_NO_CACHE | _PAGE_GUARDED);
966 vma->vm_ops = &spufs_mss_mmap_vmops;
969 #else /* SPUFS_MMAP_4K */
970 #define spufs_mss_mmap NULL
971 #endif /* !SPUFS_MMAP_4K */
973 static int spufs_mss_open(struct inode *inode, struct file *file)
975 struct spufs_inode_info *i = SPUFS_I(inode);
977 file->private_data = i->i_ctx;
978 return nonseekable_open(inode, file);
981 static struct file_operations spufs_mss_fops = {
982 .open = spufs_mss_open,
983 .mmap = spufs_mss_mmap,
986 static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma,
987 unsigned long address, int *type)
989 return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000);
992 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
993 .nopage = spufs_psmap_mmap_nopage,
997 * mmap support for full problem state area [0x00000 - 0x1ffff].
999 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1001 if (!(vma->vm_flags & VM_SHARED))
1004 vma->vm_flags |= VM_RESERVED;
1005 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1006 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1008 vma->vm_ops = &spufs_psmap_mmap_vmops;
1012 static int spufs_psmap_open(struct inode *inode, struct file *file)
1014 struct spufs_inode_info *i = SPUFS_I(inode);
1016 file->private_data = i->i_ctx;
1017 return nonseekable_open(inode, file);
1020 static struct file_operations spufs_psmap_fops = {
1021 .open = spufs_psmap_open,
1022 .mmap = spufs_psmap_mmap,
1027 static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
1028 unsigned long address, int *type)
1030 return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000);
1033 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1034 .nopage = spufs_mfc_mmap_nopage,
1038 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1040 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1042 if (!(vma->vm_flags & VM_SHARED))
1045 vma->vm_flags |= VM_RESERVED;
1046 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1047 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1049 vma->vm_ops = &spufs_mfc_mmap_vmops;
1052 #else /* SPUFS_MMAP_4K */
1053 #define spufs_mfc_mmap NULL
1054 #endif /* !SPUFS_MMAP_4K */
1056 static int spufs_mfc_open(struct inode *inode, struct file *file)
1058 struct spufs_inode_info *i = SPUFS_I(inode);
1059 struct spu_context *ctx = i->i_ctx;
1061 /* we don't want to deal with DMA into other processes */
1062 if (ctx->owner != current->mm)
1065 if (atomic_read(&inode->i_count) != 1)
1068 file->private_data = ctx;
1069 return nonseekable_open(inode, file);
1072 /* interrupt-level mfc callback function. */
1073 void spufs_mfc_callback(struct spu *spu)
1075 struct spu_context *ctx = spu->ctx;
1077 wake_up_all(&ctx->mfc_wq);
1079 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1080 if (ctx->mfc_fasync) {
1081 u32 free_elements, tagstatus;
1084 /* no need for spu_acquire in interrupt context */
1085 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1086 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1089 if (free_elements & 0xffff)
1091 if (tagstatus & ctx->tagwait)
1094 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1098 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1100 /* See if there is one tag group is complete */
1101 /* FIXME we need locking around tagwait */
1102 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1103 ctx->tagwait &= ~*status;
1107 /* enable interrupt waiting for any tag group,
1108 may silently fail if interrupts are already enabled */
1109 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1113 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1114 size_t size, loff_t *pos)
1116 struct spu_context *ctx = file->private_data;
1124 if (file->f_flags & O_NONBLOCK) {
1125 status = ctx->ops->read_mfc_tagstatus(ctx);
1126 if (!(status & ctx->tagwait))
1129 ctx->tagwait &= ~status;
1131 ret = spufs_wait(ctx->mfc_wq,
1132 spufs_read_mfc_tagstatus(ctx, &status));
1140 if (copy_to_user(buffer, &status, 4))
1147 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1149 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1150 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1161 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1165 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1166 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1171 switch (cmd->size & 0xf) {
1192 pr_debug("invalid DMA alignment %x for size %x\n",
1193 cmd->lsa & 0xf, cmd->size);
1197 if (cmd->size > 16 * 1024) {
1198 pr_debug("invalid DMA size %x\n", cmd->size);
1202 if (cmd->tag & 0xfff0) {
1203 /* we reserve the higher tag numbers for kernel use */
1204 pr_debug("invalid DMA tag\n");
1209 /* not supported in this version */
1210 pr_debug("invalid DMA class\n");
1217 static int spu_send_mfc_command(struct spu_context *ctx,
1218 struct mfc_dma_command cmd,
1221 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1222 if (*error == -EAGAIN) {
1223 /* wait for any tag group to complete
1224 so we have space for the new command */
1225 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1226 /* try again, because the queue might be
1228 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1229 if (*error == -EAGAIN)
1235 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1236 size_t size, loff_t *pos)
1238 struct spu_context *ctx = file->private_data;
1239 struct mfc_dma_command cmd;
1242 if (size != sizeof cmd)
1246 if (copy_from_user(&cmd, buffer, sizeof cmd))
1249 ret = spufs_check_valid_dma(&cmd);
1253 spu_acquire_runnable(ctx);
1254 if (file->f_flags & O_NONBLOCK) {
1255 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1258 ret = spufs_wait(ctx->mfc_wq,
1259 spu_send_mfc_command(ctx, cmd, &status));
1268 ctx->tagwait |= 1 << cmd.tag;
1274 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1276 struct spu_context *ctx = file->private_data;
1277 u32 free_elements, tagstatus;
1281 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1282 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1283 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1286 poll_wait(file, &ctx->mfc_wq, wait);
1289 if (free_elements & 0xffff)
1290 mask |= POLLOUT | POLLWRNORM;
1291 if (tagstatus & ctx->tagwait)
1292 mask |= POLLIN | POLLRDNORM;
1294 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1295 free_elements, tagstatus, ctx->tagwait);
1300 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1302 struct spu_context *ctx = file->private_data;
1307 /* this currently hangs */
1308 ret = spufs_wait(ctx->mfc_wq,
1309 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1312 ret = spufs_wait(ctx->mfc_wq,
1313 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1323 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1326 return spufs_mfc_flush(file, NULL);
1329 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1331 struct spu_context *ctx = file->private_data;
1333 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1336 static struct file_operations spufs_mfc_fops = {
1337 .open = spufs_mfc_open,
1338 .read = spufs_mfc_read,
1339 .write = spufs_mfc_write,
1340 .poll = spufs_mfc_poll,
1341 .flush = spufs_mfc_flush,
1342 .fsync = spufs_mfc_fsync,
1343 .fasync = spufs_mfc_fasync,
1344 .mmap = spufs_mfc_mmap,
1348 static int spufs_recycle_open(struct inode *inode, struct file *file)
1350 file->private_data = SPUFS_I(inode)->i_ctx;
1351 return nonseekable_open(inode, file);
1354 static ssize_t spufs_recycle_write(struct file *file,
1355 const char __user *buffer, size_t size, loff_t *pos)
1357 struct spu_context *ctx = file->private_data;
1360 if (!(ctx->flags & SPU_CREATE_ISOLATE))
1366 ret = spu_recycle_isolated(ctx);
1373 static struct file_operations spufs_recycle_fops = {
1374 .open = spufs_recycle_open,
1375 .write = spufs_recycle_write,
1378 static void spufs_npc_set(void *data, u64 val)
1380 struct spu_context *ctx = data;
1382 ctx->ops->npc_write(ctx, val);
1386 static u64 spufs_npc_get(void *data)
1388 struct spu_context *ctx = data;
1391 ret = ctx->ops->npc_read(ctx);
1395 DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1398 static void spufs_decr_set(void *data, u64 val)
1400 struct spu_context *ctx = data;
1401 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1402 spu_acquire_saved(ctx);
1403 lscsa->decr.slot[0] = (u32) val;
1407 static u64 spufs_decr_get(void *data)
1409 struct spu_context *ctx = data;
1410 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1412 spu_acquire_saved(ctx);
1413 ret = lscsa->decr.slot[0];
1417 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1420 static void spufs_decr_status_set(void *data, u64 val)
1422 struct spu_context *ctx = data;
1423 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1424 spu_acquire_saved(ctx);
1425 lscsa->decr_status.slot[0] = (u32) val;
1429 static u64 spufs_decr_status_get(void *data)
1431 struct spu_context *ctx = data;
1432 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1434 spu_acquire_saved(ctx);
1435 ret = lscsa->decr_status.slot[0];
1439 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1440 spufs_decr_status_set, "0x%llx\n")
1442 static void spufs_event_mask_set(void *data, u64 val)
1444 struct spu_context *ctx = data;
1445 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1446 spu_acquire_saved(ctx);
1447 lscsa->event_mask.slot[0] = (u32) val;
1451 static u64 spufs_event_mask_get(void *data)
1453 struct spu_context *ctx = data;
1454 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1456 spu_acquire_saved(ctx);
1457 ret = lscsa->event_mask.slot[0];
1461 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1462 spufs_event_mask_set, "0x%llx\n")
1464 static u64 spufs_event_status_get(void *data)
1466 struct spu_context *ctx = data;
1467 struct spu_state *state = &ctx->csa;
1471 spu_acquire_saved(ctx);
1472 stat = state->spu_chnlcnt_RW[0];
1474 ret = state->spu_chnldata_RW[0];
1478 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1481 static void spufs_srr0_set(void *data, u64 val)
1483 struct spu_context *ctx = data;
1484 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1485 spu_acquire_saved(ctx);
1486 lscsa->srr0.slot[0] = (u32) val;
1490 static u64 spufs_srr0_get(void *data)
1492 struct spu_context *ctx = data;
1493 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1495 spu_acquire_saved(ctx);
1496 ret = lscsa->srr0.slot[0];
1500 DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1503 static u64 spufs_id_get(void *data)
1505 struct spu_context *ctx = data;
1509 if (ctx->state == SPU_STATE_RUNNABLE)
1510 num = ctx->spu->number;
1512 num = (unsigned int)-1;
1517 DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
1519 static u64 spufs_object_id_get(void *data)
1521 struct spu_context *ctx = data;
1522 return ctx->object_id;
1525 static void spufs_object_id_set(void *data, u64 id)
1527 struct spu_context *ctx = data;
1528 ctx->object_id = id;
1531 DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1532 spufs_object_id_set, "0x%llx\n");
1534 static u64 spufs_lslr_get(void *data)
1536 struct spu_context *ctx = data;
1539 spu_acquire_saved(ctx);
1540 ret = ctx->csa.priv2.spu_lslr_RW;
1545 DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
1547 static int spufs_info_open(struct inode *inode, struct file *file)
1549 struct spufs_inode_info *i = SPUFS_I(inode);
1550 struct spu_context *ctx = i->i_ctx;
1551 file->private_data = ctx;
1555 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1556 size_t len, loff_t *pos)
1558 struct spu_context *ctx = file->private_data;
1562 if (!access_ok(VERIFY_WRITE, buf, len))
1565 spu_acquire_saved(ctx);
1566 spin_lock(&ctx->csa.register_lock);
1567 mbox_stat = ctx->csa.prob.mb_stat_R;
1568 if (mbox_stat & 0x0000ff) {
1569 data = ctx->csa.prob.pu_mb_R;
1571 spin_unlock(&ctx->csa.register_lock);
1574 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1577 static struct file_operations spufs_mbox_info_fops = {
1578 .open = spufs_info_open,
1579 .read = spufs_mbox_info_read,
1580 .llseek = generic_file_llseek,
1583 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
1584 size_t len, loff_t *pos)
1586 struct spu_context *ctx = file->private_data;
1590 if (!access_ok(VERIFY_WRITE, buf, len))
1593 spu_acquire_saved(ctx);
1594 spin_lock(&ctx->csa.register_lock);
1595 ibox_stat = ctx->csa.prob.mb_stat_R;
1596 if (ibox_stat & 0xff0000) {
1597 data = ctx->csa.priv2.puint_mb_R;
1599 spin_unlock(&ctx->csa.register_lock);
1602 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1605 static struct file_operations spufs_ibox_info_fops = {
1606 .open = spufs_info_open,
1607 .read = spufs_ibox_info_read,
1608 .llseek = generic_file_llseek,
1611 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
1612 size_t len, loff_t *pos)
1614 struct spu_context *ctx = file->private_data;
1619 if (!access_ok(VERIFY_WRITE, buf, len))
1622 spu_acquire_saved(ctx);
1623 spin_lock(&ctx->csa.register_lock);
1624 wbox_stat = ctx->csa.prob.mb_stat_R;
1625 cnt = (wbox_stat & 0x00ff00) >> 8;
1626 for (i = 0; i < cnt; i++) {
1627 data[i] = ctx->csa.spu_mailbox_data[i];
1629 spin_unlock(&ctx->csa.register_lock);
1632 return simple_read_from_buffer(buf, len, pos, &data,
1636 static struct file_operations spufs_wbox_info_fops = {
1637 .open = spufs_info_open,
1638 .read = spufs_wbox_info_read,
1639 .llseek = generic_file_llseek,
1642 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
1643 size_t len, loff_t *pos)
1645 struct spu_context *ctx = file->private_data;
1646 struct spu_dma_info info;
1647 struct mfc_cq_sr *qp, *spuqp;
1650 if (!access_ok(VERIFY_WRITE, buf, len))
1653 spu_acquire_saved(ctx);
1654 spin_lock(&ctx->csa.register_lock);
1655 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
1656 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
1657 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
1658 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
1659 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
1660 for (i = 0; i < 16; i++) {
1661 qp = &info.dma_info_command_data[i];
1662 spuqp = &ctx->csa.priv2.spuq[i];
1664 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
1665 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
1666 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
1667 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
1669 spin_unlock(&ctx->csa.register_lock);
1672 return simple_read_from_buffer(buf, len, pos, &info,
1676 static struct file_operations spufs_dma_info_fops = {
1677 .open = spufs_info_open,
1678 .read = spufs_dma_info_read,
1681 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
1682 size_t len, loff_t *pos)
1684 struct spu_context *ctx = file->private_data;
1685 struct spu_proxydma_info info;
1686 int ret = sizeof info;
1687 struct mfc_cq_sr *qp, *puqp;
1693 if (!access_ok(VERIFY_WRITE, buf, len))
1696 spu_acquire_saved(ctx);
1697 spin_lock(&ctx->csa.register_lock);
1698 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
1699 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
1700 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
1701 for (i = 0; i < 8; i++) {
1702 qp = &info.proxydma_info_command_data[i];
1703 puqp = &ctx->csa.priv2.puq[i];
1705 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
1706 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
1707 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
1708 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
1710 spin_unlock(&ctx->csa.register_lock);
1713 if (copy_to_user(buf, &info, sizeof info))
1719 static struct file_operations spufs_proxydma_info_fops = {
1720 .open = spufs_info_open,
1721 .read = spufs_proxydma_info_read,
1724 struct tree_descr spufs_dir_contents[] = {
1725 { "mem", &spufs_mem_fops, 0666, },
1726 { "regs", &spufs_regs_fops, 0666, },
1727 { "mbox", &spufs_mbox_fops, 0444, },
1728 { "ibox", &spufs_ibox_fops, 0444, },
1729 { "wbox", &spufs_wbox_fops, 0222, },
1730 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
1731 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
1732 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
1733 { "signal1", &spufs_signal1_fops, 0666, },
1734 { "signal2", &spufs_signal2_fops, 0666, },
1735 { "signal1_type", &spufs_signal1_type, 0666, },
1736 { "signal2_type", &spufs_signal2_type, 0666, },
1737 { "cntl", &spufs_cntl_fops, 0666, },
1738 { "fpcr", &spufs_fpcr_fops, 0666, },
1739 { "lslr", &spufs_lslr_ops, 0444, },
1740 { "mfc", &spufs_mfc_fops, 0666, },
1741 { "mss", &spufs_mss_fops, 0666, },
1742 { "npc", &spufs_npc_ops, 0666, },
1743 { "srr0", &spufs_srr0_ops, 0666, },
1744 { "decr", &spufs_decr_ops, 0666, },
1745 { "decr_status", &spufs_decr_status_ops, 0666, },
1746 { "event_mask", &spufs_event_mask_ops, 0666, },
1747 { "event_status", &spufs_event_status_ops, 0444, },
1748 { "psmap", &spufs_psmap_fops, 0666, },
1749 { "phys-id", &spufs_id_ops, 0666, },
1750 { "object-id", &spufs_object_id_ops, 0666, },
1751 { "mbox_info", &spufs_mbox_info_fops, 0444, },
1752 { "ibox_info", &spufs_ibox_info_fops, 0444, },
1753 { "wbox_info", &spufs_wbox_info_fops, 0444, },
1754 { "dma_info", &spufs_dma_info_fops, 0444, },
1755 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
1759 struct tree_descr spufs_dir_nosched_contents[] = {
1760 { "mem", &spufs_mem_fops, 0666, },
1761 { "mbox", &spufs_mbox_fops, 0444, },
1762 { "ibox", &spufs_ibox_fops, 0444, },
1763 { "wbox", &spufs_wbox_fops, 0222, },
1764 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
1765 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
1766 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
1767 { "signal1", &spufs_signal1_fops, 0666, },
1768 { "signal2", &spufs_signal2_fops, 0666, },
1769 { "signal1_type", &spufs_signal1_type, 0666, },
1770 { "signal2_type", &spufs_signal2_type, 0666, },
1771 { "mss", &spufs_mss_fops, 0666, },
1772 { "mfc", &spufs_mfc_fops, 0666, },
1773 { "cntl", &spufs_cntl_fops, 0666, },
1774 { "npc", &spufs_npc_ops, 0666, },
1775 { "psmap", &spufs_psmap_fops, 0666, },
1776 { "phys-id", &spufs_id_ops, 0666, },
1777 { "object-id", &spufs_object_id_ops, 0666, },
1778 { "recycle", &spufs_recycle_fops, 0222, },