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.
24 #include <linux/ioctl.h>
25 #include <linux/module.h>
26 #include <linux/pagemap.h>
27 #include <linux/poll.h>
28 #include <linux/ptrace.h>
31 #include <asm/semaphore.h>
33 #include <asm/uaccess.h>
39 spufs_mem_open(struct inode *inode, struct file *file)
41 struct spufs_inode_info *i = SPUFS_I(inode);
42 file->private_data = i->i_ctx;
43 file->f_mapping = i->i_ctx->local_store;
48 spufs_mem_read(struct file *file, char __user *buffer,
49 size_t size, loff_t *pos)
51 struct spu_context *ctx = file->private_data;
57 local_store = ctx->ops->get_ls(ctx);
58 ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE);
65 spufs_mem_write(struct file *file, const char __user *buffer,
66 size_t size, loff_t *pos)
68 struct spu_context *ctx = file->private_data;
72 size = min_t(ssize_t, LS_SIZE - *pos, size);
79 local_store = ctx->ops->get_ls(ctx);
80 ret = copy_from_user(local_store + *pos - size,
81 buffer, size) ? -EFAULT : size;
87 #ifdef CONFIG_SPARSEMEM
89 spufs_mem_mmap_nopage(struct vm_area_struct *vma,
90 unsigned long address, int *type)
92 struct page *page = NOPAGE_SIGBUS;
94 struct spu_context *ctx = vma->vm_file->private_data;
95 unsigned long offset = address - vma->vm_start;
96 offset += vma->vm_pgoff << PAGE_SHIFT;
100 if (ctx->state == SPU_STATE_SAVED)
101 page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
103 page = pfn_to_page((ctx->spu->local_store_phys + offset)
109 *type = VM_FAULT_MINOR;
111 page_cache_get(page);
115 static struct vm_operations_struct spufs_mem_mmap_vmops = {
116 .nopage = spufs_mem_mmap_nopage,
120 spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
122 if (!(vma->vm_flags & VM_SHARED))
126 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
129 vma->vm_ops = &spufs_mem_mmap_vmops;
134 static struct file_operations spufs_mem_fops = {
135 .open = spufs_mem_open,
136 .read = spufs_mem_read,
137 .write = spufs_mem_write,
138 .llseek = generic_file_llseek,
139 #ifdef CONFIG_SPARSEMEM
140 .mmap = spufs_mem_mmap,
145 spufs_regs_open(struct inode *inode, struct file *file)
147 struct spufs_inode_info *i = SPUFS_I(inode);
148 file->private_data = i->i_ctx;
153 spufs_regs_read(struct file *file, char __user *buffer,
154 size_t size, loff_t *pos)
156 struct spu_context *ctx = file->private_data;
157 struct spu_lscsa *lscsa = ctx->csa.lscsa;
160 spu_acquire_saved(ctx);
162 ret = simple_read_from_buffer(buffer, size, pos,
163 lscsa->gprs, sizeof lscsa->gprs);
170 spufs_regs_write(struct file *file, const char __user *buffer,
171 size_t size, loff_t *pos)
173 struct spu_context *ctx = file->private_data;
174 struct spu_lscsa *lscsa = ctx->csa.lscsa;
177 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
182 spu_acquire_saved(ctx);
184 ret = copy_from_user(lscsa->gprs + *pos - size,
185 buffer, size) ? -EFAULT : size;
191 static struct file_operations spufs_regs_fops = {
192 .open = spufs_regs_open,
193 .read = spufs_regs_read,
194 .write = spufs_regs_write,
195 .llseek = generic_file_llseek,
199 spufs_fpcr_read(struct file *file, char __user * buffer,
200 size_t size, loff_t * pos)
202 struct spu_context *ctx = file->private_data;
203 struct spu_lscsa *lscsa = ctx->csa.lscsa;
206 spu_acquire_saved(ctx);
208 ret = simple_read_from_buffer(buffer, size, pos,
209 &lscsa->fpcr, sizeof(lscsa->fpcr));
216 spufs_fpcr_write(struct file *file, const char __user * buffer,
217 size_t size, loff_t * pos)
219 struct spu_context *ctx = file->private_data;
220 struct spu_lscsa *lscsa = ctx->csa.lscsa;
223 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
228 spu_acquire_saved(ctx);
230 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
231 buffer, size) ? -EFAULT : size;
237 static struct file_operations spufs_fpcr_fops = {
238 .open = spufs_regs_open,
239 .read = spufs_fpcr_read,
240 .write = spufs_fpcr_write,
241 .llseek = generic_file_llseek,
244 /* generic open function for all pipe-like files */
245 static int spufs_pipe_open(struct inode *inode, struct file *file)
247 struct spufs_inode_info *i = SPUFS_I(inode);
248 file->private_data = i->i_ctx;
250 return nonseekable_open(inode, file);
253 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
254 size_t len, loff_t *pos)
256 struct spu_context *ctx = file->private_data;
264 ret = ctx->ops->mbox_read(ctx, &mbox_data);
270 if (copy_to_user(buf, &mbox_data, sizeof mbox_data))
276 static struct file_operations spufs_mbox_fops = {
277 .open = spufs_pipe_open,
278 .read = spufs_mbox_read,
281 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
282 size_t len, loff_t *pos)
284 struct spu_context *ctx = file->private_data;
292 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
296 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
302 static struct file_operations spufs_mbox_stat_fops = {
303 .open = spufs_pipe_open,
304 .read = spufs_mbox_stat_read,
309 * Same as wait_event_interruptible(), except that here
310 * we need to call spu_release(ctx) before sleeping, and
311 * then spu_acquire(ctx) when awoken.
314 #define spufs_wait(wq, condition) \
317 DEFINE_WAIT(__wait); \
319 prepare_to_wait(&(wq), &__wait, TASK_INTERRUPTIBLE); \
322 if (!signal_pending(current)) { \
328 __ret = -ERESTARTSYS; \
331 finish_wait(&(wq), &__wait); \
335 /* low-level ibox access function */
336 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
338 return ctx->ops->ibox_read(ctx, data);
341 static int spufs_ibox_fasync(int fd, struct file *file, int on)
343 struct spu_context *ctx = file->private_data;
345 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
348 /* interrupt-level ibox callback function. */
349 void spufs_ibox_callback(struct spu *spu)
351 struct spu_context *ctx = spu->ctx;
353 wake_up_all(&ctx->ibox_wq);
354 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
357 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
358 size_t len, loff_t *pos)
360 struct spu_context *ctx = file->private_data;
370 if (file->f_flags & O_NONBLOCK) {
371 if (!spu_ibox_read(ctx, &ibox_data))
374 ret = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
383 if (copy_to_user(buf, &ibox_data, sizeof ibox_data))
389 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
391 struct spu_context *ctx = file->private_data;
397 mbox_stat = ctx->ops->mbox_stat_read(ctx);
401 poll_wait(file, &ctx->ibox_wq, wait);
404 if (mbox_stat & 0xff0000)
405 mask |= POLLIN | POLLRDNORM;
410 static struct file_operations spufs_ibox_fops = {
411 .open = spufs_pipe_open,
412 .read = spufs_ibox_read,
413 .poll = spufs_ibox_poll,
414 .fasync = spufs_ibox_fasync,
417 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
418 size_t len, loff_t *pos)
420 struct spu_context *ctx = file->private_data;
427 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
430 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
436 static struct file_operations spufs_ibox_stat_fops = {
437 .open = spufs_pipe_open,
438 .read = spufs_ibox_stat_read,
441 /* low-level mailbox write */
442 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
444 return ctx->ops->wbox_write(ctx, data);
447 static int spufs_wbox_fasync(int fd, struct file *file, int on)
449 struct spu_context *ctx = file->private_data;
452 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
457 /* interrupt-level wbox callback function. */
458 void spufs_wbox_callback(struct spu *spu)
460 struct spu_context *ctx = spu->ctx;
462 wake_up_all(&ctx->wbox_wq);
463 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
466 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
467 size_t len, loff_t *pos)
469 struct spu_context *ctx = file->private_data;
476 if (copy_from_user(&wbox_data, buf, sizeof wbox_data))
482 if (file->f_flags & O_NONBLOCK) {
483 if (!spu_wbox_write(ctx, wbox_data))
486 ret = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
491 return ret ? ret : sizeof wbox_data;
494 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
496 struct spu_context *ctx = file->private_data;
501 mbox_stat = ctx->ops->mbox_stat_read(ctx);
504 poll_wait(file, &ctx->wbox_wq, wait);
507 if (mbox_stat & 0x00ff00)
508 mask = POLLOUT | POLLWRNORM;
513 static struct file_operations spufs_wbox_fops = {
514 .open = spufs_pipe_open,
515 .write = spufs_wbox_write,
516 .poll = spufs_wbox_poll,
517 .fasync = spufs_wbox_fasync,
520 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
521 size_t len, loff_t *pos)
523 struct spu_context *ctx = file->private_data;
530 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
533 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
539 static struct file_operations spufs_wbox_stat_fops = {
540 .open = spufs_pipe_open,
541 .read = spufs_wbox_stat_read,
544 /* interrupt-level stop callback function. */
545 void spufs_stop_callback(struct spu *spu)
547 struct spu_context *ctx = spu->ctx;
549 wake_up_all(&ctx->stop_wq);
552 static inline int spu_stopped(struct spu_context *ctx, u32 * stat)
557 *stat = ctx->ops->status_read(ctx);
558 if (ctx->state != SPU_STATE_RUNNABLE)
561 pte_fault = spu->dsisr &
562 (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED);
563 return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0;
566 static inline int spu_run_init(struct spu_context *ctx, u32 * npc,
571 if ((ret = spu_acquire_runnable(ctx)) != 0)
573 ctx->ops->npc_write(ctx, *npc);
574 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
578 static inline int spu_run_fini(struct spu_context *ctx, u32 * npc,
583 *status = ctx->ops->status_read(ctx);
584 *npc = ctx->ops->npc_read(ctx);
587 if (signal_pending(current))
589 if (unlikely(current->ptrace & PT_PTRACED)) {
590 if ((*status & SPU_STATUS_STOPPED_BY_STOP)
591 && (*status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) {
592 force_sig(SIGTRAP, current);
599 static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc,
604 if ((ret = spu_run_fini(ctx, npc, status)) != 0)
606 if (*status & (SPU_STATUS_STOPPED_BY_STOP |
607 SPU_STATUS_STOPPED_BY_HALT)) {
610 if ((ret = spu_run_init(ctx, npc, status)) != 0)
615 static inline int spu_process_events(struct spu_context *ctx)
617 struct spu *spu = ctx->spu;
618 u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED;
621 if (spu->dsisr & pte_fault)
622 ret = spu_irq_class_1_bottom(spu);
623 if (spu->class_0_pending)
624 ret = spu_irq_class_0_bottom(spu);
625 if (!ret && signal_pending(current))
630 long spufs_run_spu(struct file *file, struct spu_context *ctx,
631 u32 * npc, u32 * status)
635 if ((ret = spu_run_init(ctx, npc, status)) != 0)
639 ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, status));
642 if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) {
643 ret = spu_reacquire_runnable(ctx, npc, status);
649 ret = spu_process_events(ctx);
651 } while (!ret && !(*status & (SPU_STATUS_STOPPED_BY_STOP |
652 SPU_STATUS_STOPPED_BY_HALT)));
654 ctx->ops->runcntl_stop(ctx);
655 ret = spu_run_fini(ctx, npc, status);
663 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
664 size_t len, loff_t *pos)
666 struct spu_context *ctx = file->private_data;
673 data = ctx->ops->signal1_read(ctx);
676 if (copy_to_user(buf, &data, 4))
682 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
683 size_t len, loff_t *pos)
685 struct spu_context *ctx;
688 ctx = file->private_data;
693 if (copy_from_user(&data, buf, 4))
697 ctx->ops->signal1_write(ctx, data);
703 static struct file_operations spufs_signal1_fops = {
704 .open = spufs_pipe_open,
705 .read = spufs_signal1_read,
706 .write = spufs_signal1_write,
709 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
710 size_t len, loff_t *pos)
712 struct spu_context *ctx;
715 ctx = file->private_data;
721 data = ctx->ops->signal2_read(ctx);
724 if (copy_to_user(buf, &data, 4))
730 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
731 size_t len, loff_t *pos)
733 struct spu_context *ctx;
736 ctx = file->private_data;
741 if (copy_from_user(&data, buf, 4))
745 ctx->ops->signal2_write(ctx, data);
751 static struct file_operations spufs_signal2_fops = {
752 .open = spufs_pipe_open,
753 .read = spufs_signal2_read,
754 .write = spufs_signal2_write,
757 static void spufs_signal1_type_set(void *data, u64 val)
759 struct spu_context *ctx = data;
762 ctx->ops->signal1_type_set(ctx, val);
766 static u64 spufs_signal1_type_get(void *data)
768 struct spu_context *ctx = data;
772 ret = ctx->ops->signal1_type_get(ctx);
777 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
778 spufs_signal1_type_set, "%llu");
780 static void spufs_signal2_type_set(void *data, u64 val)
782 struct spu_context *ctx = data;
785 ctx->ops->signal2_type_set(ctx, val);
789 static u64 spufs_signal2_type_get(void *data)
791 struct spu_context *ctx = data;
795 ret = ctx->ops->signal2_type_get(ctx);
800 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
801 spufs_signal2_type_set, "%llu");
803 static void spufs_npc_set(void *data, u64 val)
805 struct spu_context *ctx = data;
807 ctx->ops->npc_write(ctx, val);
811 static u64 spufs_npc_get(void *data)
813 struct spu_context *ctx = data;
816 ret = ctx->ops->npc_read(ctx);
820 DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
822 static void spufs_decr_set(void *data, u64 val)
824 struct spu_context *ctx = data;
825 struct spu_lscsa *lscsa = ctx->csa.lscsa;
826 spu_acquire_saved(ctx);
827 lscsa->decr.slot[0] = (u32) val;
831 static u64 spufs_decr_get(void *data)
833 struct spu_context *ctx = data;
834 struct spu_lscsa *lscsa = ctx->csa.lscsa;
836 spu_acquire_saved(ctx);
837 ret = lscsa->decr.slot[0];
841 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
844 static void spufs_decr_status_set(void *data, u64 val)
846 struct spu_context *ctx = data;
847 struct spu_lscsa *lscsa = ctx->csa.lscsa;
848 spu_acquire_saved(ctx);
849 lscsa->decr_status.slot[0] = (u32) val;
853 static u64 spufs_decr_status_get(void *data)
855 struct spu_context *ctx = data;
856 struct spu_lscsa *lscsa = ctx->csa.lscsa;
858 spu_acquire_saved(ctx);
859 ret = lscsa->decr_status.slot[0];
863 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
864 spufs_decr_status_set, "%llx\n")
866 static void spufs_spu_tag_mask_set(void *data, u64 val)
868 struct spu_context *ctx = data;
869 struct spu_lscsa *lscsa = ctx->csa.lscsa;
870 spu_acquire_saved(ctx);
871 lscsa->tag_mask.slot[0] = (u32) val;
875 static u64 spufs_spu_tag_mask_get(void *data)
877 struct spu_context *ctx = data;
878 struct spu_lscsa *lscsa = ctx->csa.lscsa;
880 spu_acquire_saved(ctx);
881 ret = lscsa->tag_mask.slot[0];
885 DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
886 spufs_spu_tag_mask_set, "%llx\n")
888 static void spufs_event_mask_set(void *data, u64 val)
890 struct spu_context *ctx = data;
891 struct spu_lscsa *lscsa = ctx->csa.lscsa;
892 spu_acquire_saved(ctx);
893 lscsa->event_mask.slot[0] = (u32) val;
897 static u64 spufs_event_mask_get(void *data)
899 struct spu_context *ctx = data;
900 struct spu_lscsa *lscsa = ctx->csa.lscsa;
902 spu_acquire_saved(ctx);
903 ret = lscsa->event_mask.slot[0];
907 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
908 spufs_event_mask_set, "%llx\n")
910 static void spufs_srr0_set(void *data, u64 val)
912 struct spu_context *ctx = data;
913 struct spu_lscsa *lscsa = ctx->csa.lscsa;
914 spu_acquire_saved(ctx);
915 lscsa->srr0.slot[0] = (u32) val;
919 static u64 spufs_srr0_get(void *data)
921 struct spu_context *ctx = data;
922 struct spu_lscsa *lscsa = ctx->csa.lscsa;
924 spu_acquire_saved(ctx);
925 ret = lscsa->srr0.slot[0];
929 DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
932 struct tree_descr spufs_dir_contents[] = {
933 { "mem", &spufs_mem_fops, 0666, },
934 { "regs", &spufs_regs_fops, 0666, },
935 { "mbox", &spufs_mbox_fops, 0444, },
936 { "ibox", &spufs_ibox_fops, 0444, },
937 { "wbox", &spufs_wbox_fops, 0222, },
938 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
939 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
940 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
941 { "signal1", &spufs_signal1_fops, 0666, },
942 { "signal2", &spufs_signal2_fops, 0666, },
943 { "signal1_type", &spufs_signal1_type, 0666, },
944 { "signal2_type", &spufs_signal2_type, 0666, },
945 { "npc", &spufs_npc_ops, 0666, },
946 { "fpcr", &spufs_fpcr_fops, 0666, },
947 { "decr", &spufs_decr_ops, 0666, },
948 { "decr_status", &spufs_decr_status_ops, 0666, },
949 { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
950 { "event_mask", &spufs_event_mask_ops, 0666, },
951 { "srr0", &spufs_srr0_ops, 0666, },