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 u64 spufs_cntl_get(void *data)
216 struct spu_context *ctx = data;
220 val = ctx->ops->status_read(ctx);
226 static void spufs_cntl_set(void *data, u64 val)
228 struct spu_context *ctx = data;
231 ctx->ops->runcntl_write(ctx, val);
235 static int spufs_cntl_open(struct inode *inode, struct file *file)
237 struct spufs_inode_info *i = SPUFS_I(inode);
238 struct spu_context *ctx = i->i_ctx;
240 file->private_data = ctx;
241 file->f_mapping = inode->i_mapping;
242 ctx->cntl = inode->i_mapping;
243 return simple_attr_open(inode, file, spufs_cntl_get,
244 spufs_cntl_set, "0x%08lx");
247 static struct file_operations spufs_cntl_fops = {
248 .open = spufs_cntl_open,
249 .release = simple_attr_close,
250 .read = simple_attr_read,
251 .write = simple_attr_write,
252 .mmap = spufs_cntl_mmap,
256 spufs_regs_open(struct inode *inode, struct file *file)
258 struct spufs_inode_info *i = SPUFS_I(inode);
259 file->private_data = i->i_ctx;
264 spufs_regs_read(struct file *file, char __user *buffer,
265 size_t size, loff_t *pos)
267 struct spu_context *ctx = file->private_data;
268 struct spu_lscsa *lscsa = ctx->csa.lscsa;
271 spu_acquire_saved(ctx);
273 ret = simple_read_from_buffer(buffer, size, pos,
274 lscsa->gprs, sizeof lscsa->gprs);
281 spufs_regs_write(struct file *file, const char __user *buffer,
282 size_t size, loff_t *pos)
284 struct spu_context *ctx = file->private_data;
285 struct spu_lscsa *lscsa = ctx->csa.lscsa;
288 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
293 spu_acquire_saved(ctx);
295 ret = copy_from_user(lscsa->gprs + *pos - size,
296 buffer, size) ? -EFAULT : size;
302 static struct file_operations spufs_regs_fops = {
303 .open = spufs_regs_open,
304 .read = spufs_regs_read,
305 .write = spufs_regs_write,
306 .llseek = generic_file_llseek,
310 spufs_fpcr_read(struct file *file, char __user * buffer,
311 size_t size, loff_t * pos)
313 struct spu_context *ctx = file->private_data;
314 struct spu_lscsa *lscsa = ctx->csa.lscsa;
317 spu_acquire_saved(ctx);
319 ret = simple_read_from_buffer(buffer, size, pos,
320 &lscsa->fpcr, sizeof(lscsa->fpcr));
327 spufs_fpcr_write(struct file *file, const char __user * buffer,
328 size_t size, loff_t * pos)
330 struct spu_context *ctx = file->private_data;
331 struct spu_lscsa *lscsa = ctx->csa.lscsa;
334 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
339 spu_acquire_saved(ctx);
341 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
342 buffer, size) ? -EFAULT : size;
348 static struct file_operations spufs_fpcr_fops = {
349 .open = spufs_regs_open,
350 .read = spufs_fpcr_read,
351 .write = spufs_fpcr_write,
352 .llseek = generic_file_llseek,
355 /* generic open function for all pipe-like files */
356 static int spufs_pipe_open(struct inode *inode, struct file *file)
358 struct spufs_inode_info *i = SPUFS_I(inode);
359 file->private_data = i->i_ctx;
361 return nonseekable_open(inode, file);
365 * Read as many bytes from the mailbox as possible, until
366 * one of the conditions becomes true:
368 * - no more data available in the mailbox
369 * - end of the user provided buffer
370 * - end of the mapped area
372 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
373 size_t len, loff_t *pos)
375 struct spu_context *ctx = file->private_data;
376 u32 mbox_data, __user *udata;
382 if (!access_ok(VERIFY_WRITE, buf, len))
385 udata = (void __user *)buf;
388 for (count = 0; (count + 4) <= len; count += 4, udata++) {
390 ret = ctx->ops->mbox_read(ctx, &mbox_data);
395 * at the end of the mapped area, we can fault
396 * but still need to return the data we have
397 * read successfully so far.
399 ret = __put_user(mbox_data, udata);
414 static struct file_operations spufs_mbox_fops = {
415 .open = spufs_pipe_open,
416 .read = spufs_mbox_read,
419 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
420 size_t len, loff_t *pos)
422 struct spu_context *ctx = file->private_data;
430 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
434 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
440 static struct file_operations spufs_mbox_stat_fops = {
441 .open = spufs_pipe_open,
442 .read = spufs_mbox_stat_read,
445 /* low-level ibox access function */
446 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
448 return ctx->ops->ibox_read(ctx, data);
451 static int spufs_ibox_fasync(int fd, struct file *file, int on)
453 struct spu_context *ctx = file->private_data;
455 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
458 /* interrupt-level ibox callback function. */
459 void spufs_ibox_callback(struct spu *spu)
461 struct spu_context *ctx = spu->ctx;
463 wake_up_all(&ctx->ibox_wq);
464 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
468 * Read as many bytes from the interrupt mailbox as possible, until
469 * one of the conditions becomes true:
471 * - no more data available in the mailbox
472 * - end of the user provided buffer
473 * - end of the mapped area
475 * If the file is opened without O_NONBLOCK, we wait here until
476 * any data is available, but return when we have been able to
479 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
480 size_t len, loff_t *pos)
482 struct spu_context *ctx = file->private_data;
483 u32 ibox_data, __user *udata;
489 if (!access_ok(VERIFY_WRITE, buf, len))
492 udata = (void __user *)buf;
496 /* wait only for the first element */
498 if (file->f_flags & O_NONBLOCK) {
499 if (!spu_ibox_read(ctx, &ibox_data))
502 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
507 /* if we can't write at all, return -EFAULT */
508 count = __put_user(ibox_data, udata);
512 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
514 ret = ctx->ops->ibox_read(ctx, &ibox_data);
518 * at the end of the mapped area, we can fault
519 * but still need to return the data we have
520 * read successfully so far.
522 ret = __put_user(ibox_data, udata);
533 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
535 struct spu_context *ctx = file->private_data;
538 poll_wait(file, &ctx->ibox_wq, wait);
541 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
547 static struct file_operations spufs_ibox_fops = {
548 .open = spufs_pipe_open,
549 .read = spufs_ibox_read,
550 .poll = spufs_ibox_poll,
551 .fasync = spufs_ibox_fasync,
554 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
555 size_t len, loff_t *pos)
557 struct spu_context *ctx = file->private_data;
564 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
567 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
573 static struct file_operations spufs_ibox_stat_fops = {
574 .open = spufs_pipe_open,
575 .read = spufs_ibox_stat_read,
578 /* low-level mailbox write */
579 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
581 return ctx->ops->wbox_write(ctx, data);
584 static int spufs_wbox_fasync(int fd, struct file *file, int on)
586 struct spu_context *ctx = file->private_data;
589 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
594 /* interrupt-level wbox callback function. */
595 void spufs_wbox_callback(struct spu *spu)
597 struct spu_context *ctx = spu->ctx;
599 wake_up_all(&ctx->wbox_wq);
600 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
604 * Write as many bytes to the interrupt mailbox as possible, until
605 * one of the conditions becomes true:
607 * - the mailbox is full
608 * - end of the user provided buffer
609 * - end of the mapped area
611 * If the file is opened without O_NONBLOCK, we wait here until
612 * space is availabyl, but return when we have been able to
615 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
616 size_t len, loff_t *pos)
618 struct spu_context *ctx = file->private_data;
619 u32 wbox_data, __user *udata;
625 udata = (void __user *)buf;
626 if (!access_ok(VERIFY_READ, buf, len))
629 if (__get_user(wbox_data, udata))
635 * make sure we can at least write one element, by waiting
636 * in case of !O_NONBLOCK
639 if (file->f_flags & O_NONBLOCK) {
640 if (!spu_wbox_write(ctx, wbox_data))
643 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
649 /* write aѕ much as possible */
650 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
652 ret = __get_user(wbox_data, udata);
656 ret = spu_wbox_write(ctx, wbox_data);
666 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
668 struct spu_context *ctx = file->private_data;
671 poll_wait(file, &ctx->wbox_wq, wait);
674 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
680 static struct file_operations spufs_wbox_fops = {
681 .open = spufs_pipe_open,
682 .write = spufs_wbox_write,
683 .poll = spufs_wbox_poll,
684 .fasync = spufs_wbox_fasync,
687 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
688 size_t len, loff_t *pos)
690 struct spu_context *ctx = file->private_data;
697 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
700 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
706 static struct file_operations spufs_wbox_stat_fops = {
707 .open = spufs_pipe_open,
708 .read = spufs_wbox_stat_read,
711 static int spufs_signal1_open(struct inode *inode, struct file *file)
713 struct spufs_inode_info *i = SPUFS_I(inode);
714 struct spu_context *ctx = i->i_ctx;
715 file->private_data = ctx;
716 file->f_mapping = inode->i_mapping;
717 ctx->signal1 = inode->i_mapping;
718 return nonseekable_open(inode, file);
721 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
722 size_t len, loff_t *pos)
724 struct spu_context *ctx = file->private_data;
731 data = ctx->ops->signal1_read(ctx);
734 if (copy_to_user(buf, &data, 4))
740 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
741 size_t len, loff_t *pos)
743 struct spu_context *ctx;
746 ctx = file->private_data;
751 if (copy_from_user(&data, buf, 4))
755 ctx->ops->signal1_write(ctx, data);
761 static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
762 unsigned long address, int *type)
764 #if PAGE_SIZE == 0x1000
765 return spufs_ps_nopage(vma, address, type, 0x14000, 0x1000);
766 #elif PAGE_SIZE == 0x10000
767 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
768 * signal 1 and 2 area
770 return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
772 #error unsupported page size
776 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
777 .nopage = spufs_signal1_mmap_nopage,
780 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
782 if (!(vma->vm_flags & VM_SHARED))
785 vma->vm_flags |= VM_RESERVED;
786 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
787 | _PAGE_NO_CACHE | _PAGE_GUARDED);
789 vma->vm_ops = &spufs_signal1_mmap_vmops;
793 static struct file_operations spufs_signal1_fops = {
794 .open = spufs_signal1_open,
795 .read = spufs_signal1_read,
796 .write = spufs_signal1_write,
797 .mmap = spufs_signal1_mmap,
800 static int spufs_signal2_open(struct inode *inode, struct file *file)
802 struct spufs_inode_info *i = SPUFS_I(inode);
803 struct spu_context *ctx = i->i_ctx;
804 file->private_data = ctx;
805 file->f_mapping = inode->i_mapping;
806 ctx->signal2 = inode->i_mapping;
807 return nonseekable_open(inode, file);
810 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
811 size_t len, loff_t *pos)
813 struct spu_context *ctx;
816 ctx = file->private_data;
822 data = ctx->ops->signal2_read(ctx);
825 if (copy_to_user(buf, &data, 4))
831 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
832 size_t len, loff_t *pos)
834 struct spu_context *ctx;
837 ctx = file->private_data;
842 if (copy_from_user(&data, buf, 4))
846 ctx->ops->signal2_write(ctx, data);
853 static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
854 unsigned long address, int *type)
856 #if PAGE_SIZE == 0x1000
857 return spufs_ps_nopage(vma, address, type, 0x1c000, 0x1000);
858 #elif PAGE_SIZE == 0x10000
859 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
860 * signal 1 and 2 area
862 return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
864 #error unsupported page size
868 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
869 .nopage = spufs_signal2_mmap_nopage,
872 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
874 if (!(vma->vm_flags & VM_SHARED))
878 vma->vm_flags |= VM_RESERVED;
879 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
880 | _PAGE_NO_CACHE | _PAGE_GUARDED);
882 vma->vm_ops = &spufs_signal2_mmap_vmops;
885 #else /* SPUFS_MMAP_4K */
886 #define spufs_signal2_mmap NULL
887 #endif /* !SPUFS_MMAP_4K */
889 static struct file_operations spufs_signal2_fops = {
890 .open = spufs_signal2_open,
891 .read = spufs_signal2_read,
892 .write = spufs_signal2_write,
893 .mmap = spufs_signal2_mmap,
896 static void spufs_signal1_type_set(void *data, u64 val)
898 struct spu_context *ctx = data;
901 ctx->ops->signal1_type_set(ctx, val);
905 static u64 spufs_signal1_type_get(void *data)
907 struct spu_context *ctx = data;
911 ret = ctx->ops->signal1_type_get(ctx);
916 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
917 spufs_signal1_type_set, "%llu");
919 static void spufs_signal2_type_set(void *data, u64 val)
921 struct spu_context *ctx = data;
924 ctx->ops->signal2_type_set(ctx, val);
928 static u64 spufs_signal2_type_get(void *data)
930 struct spu_context *ctx = data;
934 ret = ctx->ops->signal2_type_get(ctx);
939 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
940 spufs_signal2_type_set, "%llu");
943 static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
944 unsigned long address, int *type)
946 return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000);
949 static struct vm_operations_struct spufs_mss_mmap_vmops = {
950 .nopage = spufs_mss_mmap_nopage,
954 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
956 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
958 if (!(vma->vm_flags & VM_SHARED))
961 vma->vm_flags |= VM_RESERVED;
962 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
963 | _PAGE_NO_CACHE | _PAGE_GUARDED);
965 vma->vm_ops = &spufs_mss_mmap_vmops;
968 #else /* SPUFS_MMAP_4K */
969 #define spufs_mss_mmap NULL
970 #endif /* !SPUFS_MMAP_4K */
972 static int spufs_mss_open(struct inode *inode, struct file *file)
974 struct spufs_inode_info *i = SPUFS_I(inode);
976 file->private_data = i->i_ctx;
977 return nonseekable_open(inode, file);
980 static struct file_operations spufs_mss_fops = {
981 .open = spufs_mss_open,
982 .mmap = spufs_mss_mmap,
985 static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma,
986 unsigned long address, int *type)
988 return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000);
991 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
992 .nopage = spufs_psmap_mmap_nopage,
996 * mmap support for full problem state area [0x00000 - 0x1ffff].
998 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1000 if (!(vma->vm_flags & VM_SHARED))
1003 vma->vm_flags |= VM_RESERVED;
1004 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1005 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1007 vma->vm_ops = &spufs_psmap_mmap_vmops;
1011 static int spufs_psmap_open(struct inode *inode, struct file *file)
1013 struct spufs_inode_info *i = SPUFS_I(inode);
1015 file->private_data = i->i_ctx;
1016 return nonseekable_open(inode, file);
1019 static struct file_operations spufs_psmap_fops = {
1020 .open = spufs_psmap_open,
1021 .mmap = spufs_psmap_mmap,
1026 static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
1027 unsigned long address, int *type)
1029 return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000);
1032 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1033 .nopage = spufs_mfc_mmap_nopage,
1037 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1039 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1041 if (!(vma->vm_flags & VM_SHARED))
1044 vma->vm_flags |= VM_RESERVED;
1045 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1046 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1048 vma->vm_ops = &spufs_mfc_mmap_vmops;
1051 #else /* SPUFS_MMAP_4K */
1052 #define spufs_mfc_mmap NULL
1053 #endif /* !SPUFS_MMAP_4K */
1055 static int spufs_mfc_open(struct inode *inode, struct file *file)
1057 struct spufs_inode_info *i = SPUFS_I(inode);
1058 struct spu_context *ctx = i->i_ctx;
1060 /* we don't want to deal with DMA into other processes */
1061 if (ctx->owner != current->mm)
1064 if (atomic_read(&inode->i_count) != 1)
1067 file->private_data = ctx;
1068 return nonseekable_open(inode, file);
1071 /* interrupt-level mfc callback function. */
1072 void spufs_mfc_callback(struct spu *spu)
1074 struct spu_context *ctx = spu->ctx;
1076 wake_up_all(&ctx->mfc_wq);
1078 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1079 if (ctx->mfc_fasync) {
1080 u32 free_elements, tagstatus;
1083 /* no need for spu_acquire in interrupt context */
1084 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1085 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1088 if (free_elements & 0xffff)
1090 if (tagstatus & ctx->tagwait)
1093 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1097 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1099 /* See if there is one tag group is complete */
1100 /* FIXME we need locking around tagwait */
1101 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1102 ctx->tagwait &= ~*status;
1106 /* enable interrupt waiting for any tag group,
1107 may silently fail if interrupts are already enabled */
1108 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1112 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1113 size_t size, loff_t *pos)
1115 struct spu_context *ctx = file->private_data;
1123 if (file->f_flags & O_NONBLOCK) {
1124 status = ctx->ops->read_mfc_tagstatus(ctx);
1125 if (!(status & ctx->tagwait))
1128 ctx->tagwait &= ~status;
1130 ret = spufs_wait(ctx->mfc_wq,
1131 spufs_read_mfc_tagstatus(ctx, &status));
1139 if (copy_to_user(buffer, &status, 4))
1146 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1148 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1149 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1160 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1164 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1165 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1170 switch (cmd->size & 0xf) {
1191 pr_debug("invalid DMA alignment %x for size %x\n",
1192 cmd->lsa & 0xf, cmd->size);
1196 if (cmd->size > 16 * 1024) {
1197 pr_debug("invalid DMA size %x\n", cmd->size);
1201 if (cmd->tag & 0xfff0) {
1202 /* we reserve the higher tag numbers for kernel use */
1203 pr_debug("invalid DMA tag\n");
1208 /* not supported in this version */
1209 pr_debug("invalid DMA class\n");
1216 static int spu_send_mfc_command(struct spu_context *ctx,
1217 struct mfc_dma_command cmd,
1220 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1221 if (*error == -EAGAIN) {
1222 /* wait for any tag group to complete
1223 so we have space for the new command */
1224 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1225 /* try again, because the queue might be
1227 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1228 if (*error == -EAGAIN)
1234 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1235 size_t size, loff_t *pos)
1237 struct spu_context *ctx = file->private_data;
1238 struct mfc_dma_command cmd;
1241 if (size != sizeof cmd)
1245 if (copy_from_user(&cmd, buffer, sizeof cmd))
1248 ret = spufs_check_valid_dma(&cmd);
1252 spu_acquire_runnable(ctx);
1253 if (file->f_flags & O_NONBLOCK) {
1254 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1257 ret = spufs_wait(ctx->mfc_wq,
1258 spu_send_mfc_command(ctx, cmd, &status));
1267 ctx->tagwait |= 1 << cmd.tag;
1273 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1275 struct spu_context *ctx = file->private_data;
1276 u32 free_elements, tagstatus;
1280 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1281 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1282 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1285 poll_wait(file, &ctx->mfc_wq, wait);
1288 if (free_elements & 0xffff)
1289 mask |= POLLOUT | POLLWRNORM;
1290 if (tagstatus & ctx->tagwait)
1291 mask |= POLLIN | POLLRDNORM;
1293 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1294 free_elements, tagstatus, ctx->tagwait);
1299 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1301 struct spu_context *ctx = file->private_data;
1306 /* this currently hangs */
1307 ret = spufs_wait(ctx->mfc_wq,
1308 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1311 ret = spufs_wait(ctx->mfc_wq,
1312 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1322 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1325 return spufs_mfc_flush(file, NULL);
1328 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1330 struct spu_context *ctx = file->private_data;
1332 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1335 static struct file_operations spufs_mfc_fops = {
1336 .open = spufs_mfc_open,
1337 .read = spufs_mfc_read,
1338 .write = spufs_mfc_write,
1339 .poll = spufs_mfc_poll,
1340 .flush = spufs_mfc_flush,
1341 .fsync = spufs_mfc_fsync,
1342 .fasync = spufs_mfc_fasync,
1343 .mmap = spufs_mfc_mmap,
1346 static void spufs_npc_set(void *data, u64 val)
1348 struct spu_context *ctx = data;
1350 ctx->ops->npc_write(ctx, val);
1354 static u64 spufs_npc_get(void *data)
1356 struct spu_context *ctx = data;
1359 ret = ctx->ops->npc_read(ctx);
1363 DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
1365 static void spufs_decr_set(void *data, u64 val)
1367 struct spu_context *ctx = data;
1368 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1369 spu_acquire_saved(ctx);
1370 lscsa->decr.slot[0] = (u32) val;
1374 static u64 spufs_decr_get(void *data)
1376 struct spu_context *ctx = data;
1377 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1379 spu_acquire_saved(ctx);
1380 ret = lscsa->decr.slot[0];
1384 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1387 static void spufs_decr_status_set(void *data, u64 val)
1389 struct spu_context *ctx = data;
1390 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1391 spu_acquire_saved(ctx);
1392 lscsa->decr_status.slot[0] = (u32) val;
1396 static u64 spufs_decr_status_get(void *data)
1398 struct spu_context *ctx = data;
1399 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1401 spu_acquire_saved(ctx);
1402 ret = lscsa->decr_status.slot[0];
1406 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1407 spufs_decr_status_set, "%llx\n")
1409 static void spufs_spu_tag_mask_set(void *data, u64 val)
1411 struct spu_context *ctx = data;
1412 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1413 spu_acquire_saved(ctx);
1414 lscsa->tag_mask.slot[0] = (u32) val;
1418 static u64 spufs_spu_tag_mask_get(void *data)
1420 struct spu_context *ctx = data;
1421 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1423 spu_acquire_saved(ctx);
1424 ret = lscsa->tag_mask.slot[0];
1428 DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
1429 spufs_spu_tag_mask_set, "%llx\n")
1431 static void spufs_event_mask_set(void *data, u64 val)
1433 struct spu_context *ctx = data;
1434 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1435 spu_acquire_saved(ctx);
1436 lscsa->event_mask.slot[0] = (u32) val;
1440 static u64 spufs_event_mask_get(void *data)
1442 struct spu_context *ctx = data;
1443 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1445 spu_acquire_saved(ctx);
1446 ret = lscsa->event_mask.slot[0];
1450 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1451 spufs_event_mask_set, "%llx\n")
1453 static void spufs_srr0_set(void *data, u64 val)
1455 struct spu_context *ctx = data;
1456 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1457 spu_acquire_saved(ctx);
1458 lscsa->srr0.slot[0] = (u32) val;
1462 static u64 spufs_srr0_get(void *data)
1464 struct spu_context *ctx = data;
1465 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1467 spu_acquire_saved(ctx);
1468 ret = lscsa->srr0.slot[0];
1472 DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1475 static u64 spufs_id_get(void *data)
1477 struct spu_context *ctx = data;
1481 if (ctx->state == SPU_STATE_RUNNABLE)
1482 num = ctx->spu->number;
1484 num = (unsigned int)-1;
1489 DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
1491 static u64 spufs_object_id_get(void *data)
1493 struct spu_context *ctx = data;
1494 return ctx->object_id;
1497 static void spufs_object_id_set(void *data, u64 id)
1499 struct spu_context *ctx = data;
1500 ctx->object_id = id;
1503 DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1504 spufs_object_id_set, "0x%llx\n");
1506 struct tree_descr spufs_dir_contents[] = {
1507 { "mem", &spufs_mem_fops, 0666, },
1508 { "regs", &spufs_regs_fops, 0666, },
1509 { "mbox", &spufs_mbox_fops, 0444, },
1510 { "ibox", &spufs_ibox_fops, 0444, },
1511 { "wbox", &spufs_wbox_fops, 0222, },
1512 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
1513 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
1514 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
1515 { "signal1", &spufs_signal1_fops, 0666, },
1516 { "signal2", &spufs_signal2_fops, 0666, },
1517 { "signal1_type", &spufs_signal1_type, 0666, },
1518 { "signal2_type", &spufs_signal2_type, 0666, },
1519 { "mss", &spufs_mss_fops, 0666, },
1520 { "mfc", &spufs_mfc_fops, 0666, },
1521 { "cntl", &spufs_cntl_fops, 0666, },
1522 { "npc", &spufs_npc_ops, 0666, },
1523 { "fpcr", &spufs_fpcr_fops, 0666, },
1524 { "decr", &spufs_decr_ops, 0666, },
1525 { "decr_status", &spufs_decr_status_ops, 0666, },
1526 { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
1527 { "event_mask", &spufs_event_mask_ops, 0666, },
1528 { "srr0", &spufs_srr0_ops, 0666, },
1529 { "psmap", &spufs_psmap_fops, 0666, },
1530 { "phys-id", &spufs_id_ops, 0666, },
1531 { "object-id", &spufs_object_id_ops, 0666, },