2 * SPU file system -- file contents
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/ioctl.h>
27 #include <linux/module.h>
28 #include <linux/pagemap.h>
29 #include <linux/poll.h>
30 #include <linux/ptrace.h>
31 #include <linux/seq_file.h>
34 #include <asm/semaphore.h>
36 #include <asm/spu_info.h>
37 #include <asm/uaccess.h>
41 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
43 /* Simple attribute files */
45 int (*get)(void *, u64 *);
46 int (*set)(void *, u64);
47 char get_buf[24]; /* enough to store a u64 and "\n\0" */
50 const char *fmt; /* format for read operation */
51 struct mutex mutex; /* protects access to these buffers */
54 static int spufs_attr_open(struct inode *inode, struct file *file,
55 int (*get)(void *, u64 *), int (*set)(void *, u64),
58 struct spufs_attr *attr;
60 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
66 attr->data = inode->i_private;
68 mutex_init(&attr->mutex);
69 file->private_data = attr;
71 return nonseekable_open(inode, file);
74 static int spufs_attr_release(struct inode *inode, struct file *file)
76 kfree(file->private_data);
80 static ssize_t spufs_attr_read(struct file *file, char __user *buf,
81 size_t len, loff_t *ppos)
83 struct spufs_attr *attr;
87 attr = file->private_data;
91 ret = mutex_lock_interruptible(&attr->mutex);
95 if (*ppos) { /* continued read */
96 size = strlen(attr->get_buf);
97 } else { /* first read */
99 ret = attr->get(attr->data, &val);
103 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
104 attr->fmt, (unsigned long long)val);
107 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
109 mutex_unlock(&attr->mutex);
113 static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
114 size_t len, loff_t *ppos)
116 struct spufs_attr *attr;
121 attr = file->private_data;
125 ret = mutex_lock_interruptible(&attr->mutex);
130 size = min(sizeof(attr->set_buf) - 1, len);
131 if (copy_from_user(attr->set_buf, buf, size))
134 ret = len; /* claim we got the whole input */
135 attr->set_buf[size] = '\0';
136 val = simple_strtol(attr->set_buf, NULL, 0);
137 attr->set(attr->data, val);
139 mutex_unlock(&attr->mutex);
143 #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
144 static int __fops ## _open(struct inode *inode, struct file *file) \
146 __simple_attr_check_format(__fmt, 0ull); \
147 return spufs_attr_open(inode, file, __get, __set, __fmt); \
149 static struct file_operations __fops = { \
150 .owner = THIS_MODULE, \
151 .open = __fops ## _open, \
152 .release = spufs_attr_release, \
153 .read = spufs_attr_read, \
154 .write = spufs_attr_write, \
159 spufs_mem_open(struct inode *inode, struct file *file)
161 struct spufs_inode_info *i = SPUFS_I(inode);
162 struct spu_context *ctx = i->i_ctx;
164 mutex_lock(&ctx->mapping_lock);
165 file->private_data = ctx;
167 ctx->local_store = inode->i_mapping;
168 mutex_unlock(&ctx->mapping_lock);
173 spufs_mem_release(struct inode *inode, struct file *file)
175 struct spufs_inode_info *i = SPUFS_I(inode);
176 struct spu_context *ctx = i->i_ctx;
178 mutex_lock(&ctx->mapping_lock);
180 ctx->local_store = NULL;
181 mutex_unlock(&ctx->mapping_lock);
186 __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
187 size_t size, loff_t *pos)
189 char *local_store = ctx->ops->get_ls(ctx);
190 return simple_read_from_buffer(buffer, size, pos, local_store,
195 spufs_mem_read(struct file *file, char __user *buffer,
196 size_t size, loff_t *pos)
198 struct spu_context *ctx = file->private_data;
201 ret = spu_acquire(ctx);
204 ret = __spufs_mem_read(ctx, buffer, size, pos);
211 spufs_mem_write(struct file *file, const char __user *buffer,
212 size_t size, loff_t *ppos)
214 struct spu_context *ctx = file->private_data;
223 if (size > LS_SIZE - pos)
224 size = LS_SIZE - pos;
226 ret = spu_acquire(ctx);
230 local_store = ctx->ops->get_ls(ctx);
231 ret = copy_from_user(local_store + pos, buffer, size);
240 static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
241 unsigned long address)
243 struct spu_context *ctx = vma->vm_file->private_data;
244 unsigned long pfn, offset, addr0 = address;
245 #ifdef CONFIG_SPU_FS_64K_LS
246 struct spu_state *csa = &ctx->csa;
249 /* Check what page size we are using */
250 psize = get_slice_psize(vma->vm_mm, address);
252 /* Some sanity checking */
253 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
255 /* Wow, 64K, cool, we need to align the address though */
256 if (csa->use_big_pages) {
257 BUG_ON(vma->vm_start & 0xffff);
258 address &= ~0xfffful;
260 #endif /* CONFIG_SPU_FS_64K_LS */
262 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
263 if (offset >= LS_SIZE)
266 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
267 addr0, address, offset);
269 if (spu_acquire(ctx))
270 return NOPFN_REFAULT;
272 if (ctx->state == SPU_STATE_SAVED) {
273 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
275 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
277 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
279 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
281 vm_insert_pfn(vma, address, pfn);
285 return NOPFN_REFAULT;
289 static struct vm_operations_struct spufs_mem_mmap_vmops = {
290 .nopfn = spufs_mem_mmap_nopfn,
293 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
295 #ifdef CONFIG_SPU_FS_64K_LS
296 struct spu_context *ctx = file->private_data;
297 struct spu_state *csa = &ctx->csa;
299 /* Sanity check VMA alignment */
300 if (csa->use_big_pages) {
301 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
302 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
304 if (vma->vm_start & 0xffff)
306 if (vma->vm_pgoff & 0xf)
309 #endif /* CONFIG_SPU_FS_64K_LS */
311 if (!(vma->vm_flags & VM_SHARED))
314 vma->vm_flags |= VM_IO | VM_PFNMAP;
315 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
318 vma->vm_ops = &spufs_mem_mmap_vmops;
322 #ifdef CONFIG_SPU_FS_64K_LS
323 static unsigned long spufs_get_unmapped_area(struct file *file,
324 unsigned long addr, unsigned long len, unsigned long pgoff,
327 struct spu_context *ctx = file->private_data;
328 struct spu_state *csa = &ctx->csa;
330 /* If not using big pages, fallback to normal MM g_u_a */
331 if (!csa->use_big_pages)
332 return current->mm->get_unmapped_area(file, addr, len,
335 /* Else, try to obtain a 64K pages slice */
336 return slice_get_unmapped_area(addr, len, flags,
339 #endif /* CONFIG_SPU_FS_64K_LS */
341 static const struct file_operations spufs_mem_fops = {
342 .open = spufs_mem_open,
343 .release = spufs_mem_release,
344 .read = spufs_mem_read,
345 .write = spufs_mem_write,
346 .llseek = generic_file_llseek,
347 .mmap = spufs_mem_mmap,
348 #ifdef CONFIG_SPU_FS_64K_LS
349 .get_unmapped_area = spufs_get_unmapped_area,
353 static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
354 unsigned long address,
355 unsigned long ps_offs,
356 unsigned long ps_size)
358 struct spu_context *ctx = vma->vm_file->private_data;
359 unsigned long area, offset = address - vma->vm_start;
361 offset += vma->vm_pgoff << PAGE_SHIFT;
362 if (offset >= ps_size)
366 * We have to wait for context to be loaded before we have
367 * pages to hand out to the user, but we don't want to wait
368 * with the mmap_sem held.
369 * It is possible to drop the mmap_sem here, but then we need
370 * to return NOPFN_REFAULT because the mappings may have
373 if (spu_acquire(ctx))
374 return NOPFN_REFAULT;
376 if (ctx->state == SPU_STATE_SAVED) {
377 up_read(¤t->mm->mmap_sem);
378 spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
379 down_read(¤t->mm->mmap_sem);
381 area = ctx->spu->problem_phys + ps_offs;
382 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
386 return NOPFN_REFAULT;
390 static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
391 unsigned long address)
393 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
396 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
397 .nopfn = spufs_cntl_mmap_nopfn,
401 * mmap support for problem state control area [0x4000 - 0x4fff].
403 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
405 if (!(vma->vm_flags & VM_SHARED))
408 vma->vm_flags |= VM_IO | VM_PFNMAP;
409 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
410 | _PAGE_NO_CACHE | _PAGE_GUARDED);
412 vma->vm_ops = &spufs_cntl_mmap_vmops;
415 #else /* SPUFS_MMAP_4K */
416 #define spufs_cntl_mmap NULL
417 #endif /* !SPUFS_MMAP_4K */
419 static int spufs_cntl_get(void *data, u64 *val)
421 struct spu_context *ctx = data;
424 ret = spu_acquire(ctx);
427 *val = ctx->ops->status_read(ctx);
433 static int spufs_cntl_set(void *data, u64 val)
435 struct spu_context *ctx = data;
438 ret = spu_acquire(ctx);
441 ctx->ops->runcntl_write(ctx, val);
447 static int spufs_cntl_open(struct inode *inode, struct file *file)
449 struct spufs_inode_info *i = SPUFS_I(inode);
450 struct spu_context *ctx = i->i_ctx;
452 mutex_lock(&ctx->mapping_lock);
453 file->private_data = ctx;
455 ctx->cntl = inode->i_mapping;
456 mutex_unlock(&ctx->mapping_lock);
457 return spufs_attr_open(inode, file, spufs_cntl_get,
458 spufs_cntl_set, "0x%08lx");
462 spufs_cntl_release(struct inode *inode, struct file *file)
464 struct spufs_inode_info *i = SPUFS_I(inode);
465 struct spu_context *ctx = i->i_ctx;
467 spufs_attr_release(inode, file);
469 mutex_lock(&ctx->mapping_lock);
472 mutex_unlock(&ctx->mapping_lock);
476 static const struct file_operations spufs_cntl_fops = {
477 .open = spufs_cntl_open,
478 .release = spufs_cntl_release,
479 .read = spufs_attr_read,
480 .write = spufs_attr_write,
481 .mmap = spufs_cntl_mmap,
485 spufs_regs_open(struct inode *inode, struct file *file)
487 struct spufs_inode_info *i = SPUFS_I(inode);
488 file->private_data = i->i_ctx;
493 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
494 size_t size, loff_t *pos)
496 struct spu_lscsa *lscsa = ctx->csa.lscsa;
497 return simple_read_from_buffer(buffer, size, pos,
498 lscsa->gprs, sizeof lscsa->gprs);
502 spufs_regs_read(struct file *file, char __user *buffer,
503 size_t size, loff_t *pos)
506 struct spu_context *ctx = file->private_data;
508 ret = spu_acquire_saved(ctx);
511 ret = __spufs_regs_read(ctx, buffer, size, pos);
512 spu_release_saved(ctx);
517 spufs_regs_write(struct file *file, const char __user *buffer,
518 size_t size, loff_t *pos)
520 struct spu_context *ctx = file->private_data;
521 struct spu_lscsa *lscsa = ctx->csa.lscsa;
524 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
529 ret = spu_acquire_saved(ctx);
533 ret = copy_from_user(lscsa->gprs + *pos - size,
534 buffer, size) ? -EFAULT : size;
536 spu_release_saved(ctx);
540 static const struct file_operations spufs_regs_fops = {
541 .open = spufs_regs_open,
542 .read = spufs_regs_read,
543 .write = spufs_regs_write,
544 .llseek = generic_file_llseek,
548 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
549 size_t size, loff_t * pos)
551 struct spu_lscsa *lscsa = ctx->csa.lscsa;
552 return simple_read_from_buffer(buffer, size, pos,
553 &lscsa->fpcr, sizeof(lscsa->fpcr));
557 spufs_fpcr_read(struct file *file, char __user * buffer,
558 size_t size, loff_t * pos)
561 struct spu_context *ctx = file->private_data;
563 ret = spu_acquire_saved(ctx);
566 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
567 spu_release_saved(ctx);
572 spufs_fpcr_write(struct file *file, const char __user * buffer,
573 size_t size, loff_t * pos)
575 struct spu_context *ctx = file->private_data;
576 struct spu_lscsa *lscsa = ctx->csa.lscsa;
579 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
583 ret = spu_acquire_saved(ctx);
588 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
589 buffer, size) ? -EFAULT : size;
591 spu_release_saved(ctx);
595 static const struct file_operations spufs_fpcr_fops = {
596 .open = spufs_regs_open,
597 .read = spufs_fpcr_read,
598 .write = spufs_fpcr_write,
599 .llseek = generic_file_llseek,
602 /* generic open function for all pipe-like files */
603 static int spufs_pipe_open(struct inode *inode, struct file *file)
605 struct spufs_inode_info *i = SPUFS_I(inode);
606 file->private_data = i->i_ctx;
608 return nonseekable_open(inode, file);
612 * Read as many bytes from the mailbox as possible, until
613 * one of the conditions becomes true:
615 * - no more data available in the mailbox
616 * - end of the user provided buffer
617 * - end of the mapped area
619 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
620 size_t len, loff_t *pos)
622 struct spu_context *ctx = file->private_data;
623 u32 mbox_data, __user *udata;
629 if (!access_ok(VERIFY_WRITE, buf, len))
632 udata = (void __user *)buf;
634 count = spu_acquire(ctx);
638 for (count = 0; (count + 4) <= len; count += 4, udata++) {
640 ret = ctx->ops->mbox_read(ctx, &mbox_data);
645 * at the end of the mapped area, we can fault
646 * but still need to return the data we have
647 * read successfully so far.
649 ret = __put_user(mbox_data, udata);
664 static const struct file_operations spufs_mbox_fops = {
665 .open = spufs_pipe_open,
666 .read = spufs_mbox_read,
669 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
670 size_t len, loff_t *pos)
672 struct spu_context *ctx = file->private_data;
679 ret = spu_acquire(ctx);
683 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
687 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
693 static const struct file_operations spufs_mbox_stat_fops = {
694 .open = spufs_pipe_open,
695 .read = spufs_mbox_stat_read,
698 /* low-level ibox access function */
699 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
701 return ctx->ops->ibox_read(ctx, data);
704 static int spufs_ibox_fasync(int fd, struct file *file, int on)
706 struct spu_context *ctx = file->private_data;
708 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
711 /* interrupt-level ibox callback function. */
712 void spufs_ibox_callback(struct spu *spu)
714 struct spu_context *ctx = spu->ctx;
719 wake_up_all(&ctx->ibox_wq);
720 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
724 * Read as many bytes from the interrupt mailbox as possible, until
725 * one of the conditions becomes true:
727 * - no more data available in the mailbox
728 * - end of the user provided buffer
729 * - end of the mapped area
731 * If the file is opened without O_NONBLOCK, we wait here until
732 * any data is available, but return when we have been able to
735 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
736 size_t len, loff_t *pos)
738 struct spu_context *ctx = file->private_data;
739 u32 ibox_data, __user *udata;
745 if (!access_ok(VERIFY_WRITE, buf, len))
748 udata = (void __user *)buf;
750 count = spu_acquire(ctx);
754 /* wait only for the first element */
756 if (file->f_flags & O_NONBLOCK) {
757 if (!spu_ibox_read(ctx, &ibox_data))
760 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
765 /* if we can't write at all, return -EFAULT */
766 count = __put_user(ibox_data, udata);
770 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
772 ret = ctx->ops->ibox_read(ctx, &ibox_data);
776 * at the end of the mapped area, we can fault
777 * but still need to return the data we have
778 * read successfully so far.
780 ret = __put_user(ibox_data, udata);
791 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
793 struct spu_context *ctx = file->private_data;
796 poll_wait(file, &ctx->ibox_wq, wait);
799 * For now keep this uninterruptible and also ignore the rule
800 * that poll should not sleep. Will be fixed later.
802 mutex_lock(&ctx->state_mutex);
803 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
809 static const struct file_operations spufs_ibox_fops = {
810 .open = spufs_pipe_open,
811 .read = spufs_ibox_read,
812 .poll = spufs_ibox_poll,
813 .fasync = spufs_ibox_fasync,
816 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
817 size_t len, loff_t *pos)
819 struct spu_context *ctx = file->private_data;
826 ret = spu_acquire(ctx);
829 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
832 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
838 static const struct file_operations spufs_ibox_stat_fops = {
839 .open = spufs_pipe_open,
840 .read = spufs_ibox_stat_read,
843 /* low-level mailbox write */
844 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
846 return ctx->ops->wbox_write(ctx, data);
849 static int spufs_wbox_fasync(int fd, struct file *file, int on)
851 struct spu_context *ctx = file->private_data;
854 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
859 /* interrupt-level wbox callback function. */
860 void spufs_wbox_callback(struct spu *spu)
862 struct spu_context *ctx = spu->ctx;
867 wake_up_all(&ctx->wbox_wq);
868 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
872 * Write as many bytes to the interrupt mailbox as possible, until
873 * one of the conditions becomes true:
875 * - the mailbox is full
876 * - end of the user provided buffer
877 * - end of the mapped area
879 * If the file is opened without O_NONBLOCK, we wait here until
880 * space is availabyl, but return when we have been able to
883 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
884 size_t len, loff_t *pos)
886 struct spu_context *ctx = file->private_data;
887 u32 wbox_data, __user *udata;
893 udata = (void __user *)buf;
894 if (!access_ok(VERIFY_READ, buf, len))
897 if (__get_user(wbox_data, udata))
900 count = spu_acquire(ctx);
905 * make sure we can at least write one element, by waiting
906 * in case of !O_NONBLOCK
909 if (file->f_flags & O_NONBLOCK) {
910 if (!spu_wbox_write(ctx, wbox_data))
913 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
919 /* write as much as possible */
920 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
922 ret = __get_user(wbox_data, udata);
926 ret = spu_wbox_write(ctx, wbox_data);
936 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
938 struct spu_context *ctx = file->private_data;
941 poll_wait(file, &ctx->wbox_wq, wait);
944 * For now keep this uninterruptible and also ignore the rule
945 * that poll should not sleep. Will be fixed later.
947 mutex_lock(&ctx->state_mutex);
948 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
954 static const struct file_operations spufs_wbox_fops = {
955 .open = spufs_pipe_open,
956 .write = spufs_wbox_write,
957 .poll = spufs_wbox_poll,
958 .fasync = spufs_wbox_fasync,
961 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
962 size_t len, loff_t *pos)
964 struct spu_context *ctx = file->private_data;
971 ret = spu_acquire(ctx);
974 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
977 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
983 static const struct file_operations spufs_wbox_stat_fops = {
984 .open = spufs_pipe_open,
985 .read = spufs_wbox_stat_read,
988 static int spufs_signal1_open(struct inode *inode, struct file *file)
990 struct spufs_inode_info *i = SPUFS_I(inode);
991 struct spu_context *ctx = i->i_ctx;
993 mutex_lock(&ctx->mapping_lock);
994 file->private_data = ctx;
996 ctx->signal1 = inode->i_mapping;
997 mutex_unlock(&ctx->mapping_lock);
998 return nonseekable_open(inode, file);
1002 spufs_signal1_release(struct inode *inode, struct file *file)
1004 struct spufs_inode_info *i = SPUFS_I(inode);
1005 struct spu_context *ctx = i->i_ctx;
1007 mutex_lock(&ctx->mapping_lock);
1008 if (!--i->i_openers)
1009 ctx->signal1 = NULL;
1010 mutex_unlock(&ctx->mapping_lock);
1014 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
1015 size_t len, loff_t *pos)
1023 if (ctx->csa.spu_chnlcnt_RW[3]) {
1024 data = ctx->csa.spu_chnldata_RW[3];
1031 if (copy_to_user(buf, &data, 4))
1038 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1039 size_t len, loff_t *pos)
1042 struct spu_context *ctx = file->private_data;
1044 ret = spu_acquire_saved(ctx);
1047 ret = __spufs_signal1_read(ctx, buf, len, pos);
1048 spu_release_saved(ctx);
1053 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1054 size_t len, loff_t *pos)
1056 struct spu_context *ctx;
1060 ctx = file->private_data;
1065 if (copy_from_user(&data, buf, 4))
1068 ret = spu_acquire(ctx);
1071 ctx->ops->signal1_write(ctx, data);
1077 static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1078 unsigned long address)
1080 #if PAGE_SIZE == 0x1000
1081 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
1082 #elif PAGE_SIZE == 0x10000
1083 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1084 * signal 1 and 2 area
1086 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1088 #error unsupported page size
1092 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
1093 .nopfn = spufs_signal1_mmap_nopfn,
1096 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1098 if (!(vma->vm_flags & VM_SHARED))
1101 vma->vm_flags |= VM_IO | VM_PFNMAP;
1102 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1103 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1105 vma->vm_ops = &spufs_signal1_mmap_vmops;
1109 static const struct file_operations spufs_signal1_fops = {
1110 .open = spufs_signal1_open,
1111 .release = spufs_signal1_release,
1112 .read = spufs_signal1_read,
1113 .write = spufs_signal1_write,
1114 .mmap = spufs_signal1_mmap,
1117 static const struct file_operations spufs_signal1_nosched_fops = {
1118 .open = spufs_signal1_open,
1119 .release = spufs_signal1_release,
1120 .write = spufs_signal1_write,
1121 .mmap = spufs_signal1_mmap,
1124 static int spufs_signal2_open(struct inode *inode, struct file *file)
1126 struct spufs_inode_info *i = SPUFS_I(inode);
1127 struct spu_context *ctx = i->i_ctx;
1129 mutex_lock(&ctx->mapping_lock);
1130 file->private_data = ctx;
1131 if (!i->i_openers++)
1132 ctx->signal2 = inode->i_mapping;
1133 mutex_unlock(&ctx->mapping_lock);
1134 return nonseekable_open(inode, file);
1138 spufs_signal2_release(struct inode *inode, struct file *file)
1140 struct spufs_inode_info *i = SPUFS_I(inode);
1141 struct spu_context *ctx = i->i_ctx;
1143 mutex_lock(&ctx->mapping_lock);
1144 if (!--i->i_openers)
1145 ctx->signal2 = NULL;
1146 mutex_unlock(&ctx->mapping_lock);
1150 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
1151 size_t len, loff_t *pos)
1159 if (ctx->csa.spu_chnlcnt_RW[4]) {
1160 data = ctx->csa.spu_chnldata_RW[4];
1167 if (copy_to_user(buf, &data, 4))
1174 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1175 size_t len, loff_t *pos)
1177 struct spu_context *ctx = file->private_data;
1180 ret = spu_acquire_saved(ctx);
1183 ret = __spufs_signal2_read(ctx, buf, len, pos);
1184 spu_release_saved(ctx);
1189 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1190 size_t len, loff_t *pos)
1192 struct spu_context *ctx;
1196 ctx = file->private_data;
1201 if (copy_from_user(&data, buf, 4))
1204 ret = spu_acquire(ctx);
1207 ctx->ops->signal2_write(ctx, data);
1214 static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1215 unsigned long address)
1217 #if PAGE_SIZE == 0x1000
1218 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
1219 #elif PAGE_SIZE == 0x10000
1220 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1221 * signal 1 and 2 area
1223 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1225 #error unsupported page size
1229 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1230 .nopfn = spufs_signal2_mmap_nopfn,
1233 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1235 if (!(vma->vm_flags & VM_SHARED))
1238 vma->vm_flags |= VM_IO | VM_PFNMAP;
1239 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1240 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1242 vma->vm_ops = &spufs_signal2_mmap_vmops;
1245 #else /* SPUFS_MMAP_4K */
1246 #define spufs_signal2_mmap NULL
1247 #endif /* !SPUFS_MMAP_4K */
1249 static const struct file_operations spufs_signal2_fops = {
1250 .open = spufs_signal2_open,
1251 .release = spufs_signal2_release,
1252 .read = spufs_signal2_read,
1253 .write = spufs_signal2_write,
1254 .mmap = spufs_signal2_mmap,
1257 static const struct file_operations spufs_signal2_nosched_fops = {
1258 .open = spufs_signal2_open,
1259 .release = spufs_signal2_release,
1260 .write = spufs_signal2_write,
1261 .mmap = spufs_signal2_mmap,
1265 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1266 * work of acquiring (or not) the SPU context before calling through
1267 * to the actual get routine. The set routine is called directly.
1269 #define SPU_ATTR_NOACQUIRE 0
1270 #define SPU_ATTR_ACQUIRE 1
1271 #define SPU_ATTR_ACQUIRE_SAVED 2
1273 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
1274 static int __##__get(void *data, u64 *val) \
1276 struct spu_context *ctx = data; \
1279 if (__acquire == SPU_ATTR_ACQUIRE) { \
1280 ret = spu_acquire(ctx); \
1283 *val = __get(ctx); \
1285 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1286 ret = spu_acquire_saved(ctx); \
1289 *val = __get(ctx); \
1290 spu_release_saved(ctx); \
1292 *val = __get(ctx); \
1296 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1298 static int spufs_signal1_type_set(void *data, u64 val)
1300 struct spu_context *ctx = data;
1303 ret = spu_acquire(ctx);
1306 ctx->ops->signal1_type_set(ctx, val);
1312 static u64 spufs_signal1_type_get(struct spu_context *ctx)
1314 return ctx->ops->signal1_type_get(ctx);
1316 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1317 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
1320 static int spufs_signal2_type_set(void *data, u64 val)
1322 struct spu_context *ctx = data;
1325 ret = spu_acquire(ctx);
1328 ctx->ops->signal2_type_set(ctx, val);
1334 static u64 spufs_signal2_type_get(struct spu_context *ctx)
1336 return ctx->ops->signal2_type_get(ctx);
1338 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1339 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
1342 static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1343 unsigned long address)
1345 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
1348 static struct vm_operations_struct spufs_mss_mmap_vmops = {
1349 .nopfn = spufs_mss_mmap_nopfn,
1353 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1355 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1357 if (!(vma->vm_flags & VM_SHARED))
1360 vma->vm_flags |= VM_IO | VM_PFNMAP;
1361 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1362 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1364 vma->vm_ops = &spufs_mss_mmap_vmops;
1367 #else /* SPUFS_MMAP_4K */
1368 #define spufs_mss_mmap NULL
1369 #endif /* !SPUFS_MMAP_4K */
1371 static int spufs_mss_open(struct inode *inode, struct file *file)
1373 struct spufs_inode_info *i = SPUFS_I(inode);
1374 struct spu_context *ctx = i->i_ctx;
1376 file->private_data = i->i_ctx;
1378 mutex_lock(&ctx->mapping_lock);
1379 if (!i->i_openers++)
1380 ctx->mss = inode->i_mapping;
1381 mutex_unlock(&ctx->mapping_lock);
1382 return nonseekable_open(inode, file);
1386 spufs_mss_release(struct inode *inode, struct file *file)
1388 struct spufs_inode_info *i = SPUFS_I(inode);
1389 struct spu_context *ctx = i->i_ctx;
1391 mutex_lock(&ctx->mapping_lock);
1392 if (!--i->i_openers)
1394 mutex_unlock(&ctx->mapping_lock);
1398 static const struct file_operations spufs_mss_fops = {
1399 .open = spufs_mss_open,
1400 .release = spufs_mss_release,
1401 .mmap = spufs_mss_mmap,
1404 static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1405 unsigned long address)
1407 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
1410 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1411 .nopfn = spufs_psmap_mmap_nopfn,
1415 * mmap support for full problem state area [0x00000 - 0x1ffff].
1417 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1419 if (!(vma->vm_flags & VM_SHARED))
1422 vma->vm_flags |= VM_IO | VM_PFNMAP;
1423 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1424 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1426 vma->vm_ops = &spufs_psmap_mmap_vmops;
1430 static int spufs_psmap_open(struct inode *inode, struct file *file)
1432 struct spufs_inode_info *i = SPUFS_I(inode);
1433 struct spu_context *ctx = i->i_ctx;
1435 mutex_lock(&ctx->mapping_lock);
1436 file->private_data = i->i_ctx;
1437 if (!i->i_openers++)
1438 ctx->psmap = inode->i_mapping;
1439 mutex_unlock(&ctx->mapping_lock);
1440 return nonseekable_open(inode, file);
1444 spufs_psmap_release(struct inode *inode, struct file *file)
1446 struct spufs_inode_info *i = SPUFS_I(inode);
1447 struct spu_context *ctx = i->i_ctx;
1449 mutex_lock(&ctx->mapping_lock);
1450 if (!--i->i_openers)
1452 mutex_unlock(&ctx->mapping_lock);
1456 static const struct file_operations spufs_psmap_fops = {
1457 .open = spufs_psmap_open,
1458 .release = spufs_psmap_release,
1459 .mmap = spufs_psmap_mmap,
1464 static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1465 unsigned long address)
1467 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
1470 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1471 .nopfn = spufs_mfc_mmap_nopfn,
1475 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1477 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1479 if (!(vma->vm_flags & VM_SHARED))
1482 vma->vm_flags |= VM_IO | VM_PFNMAP;
1483 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1484 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1486 vma->vm_ops = &spufs_mfc_mmap_vmops;
1489 #else /* SPUFS_MMAP_4K */
1490 #define spufs_mfc_mmap NULL
1491 #endif /* !SPUFS_MMAP_4K */
1493 static int spufs_mfc_open(struct inode *inode, struct file *file)
1495 struct spufs_inode_info *i = SPUFS_I(inode);
1496 struct spu_context *ctx = i->i_ctx;
1498 /* we don't want to deal with DMA into other processes */
1499 if (ctx->owner != current->mm)
1502 if (atomic_read(&inode->i_count) != 1)
1505 mutex_lock(&ctx->mapping_lock);
1506 file->private_data = ctx;
1507 if (!i->i_openers++)
1508 ctx->mfc = inode->i_mapping;
1509 mutex_unlock(&ctx->mapping_lock);
1510 return nonseekable_open(inode, file);
1514 spufs_mfc_release(struct inode *inode, struct file *file)
1516 struct spufs_inode_info *i = SPUFS_I(inode);
1517 struct spu_context *ctx = i->i_ctx;
1519 mutex_lock(&ctx->mapping_lock);
1520 if (!--i->i_openers)
1522 mutex_unlock(&ctx->mapping_lock);
1526 /* interrupt-level mfc callback function. */
1527 void spufs_mfc_callback(struct spu *spu)
1529 struct spu_context *ctx = spu->ctx;
1534 wake_up_all(&ctx->mfc_wq);
1536 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1537 if (ctx->mfc_fasync) {
1538 u32 free_elements, tagstatus;
1541 /* no need for spu_acquire in interrupt context */
1542 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1543 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1546 if (free_elements & 0xffff)
1548 if (tagstatus & ctx->tagwait)
1551 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1555 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1557 /* See if there is one tag group is complete */
1558 /* FIXME we need locking around tagwait */
1559 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1560 ctx->tagwait &= ~*status;
1564 /* enable interrupt waiting for any tag group,
1565 may silently fail if interrupts are already enabled */
1566 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1570 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1571 size_t size, loff_t *pos)
1573 struct spu_context *ctx = file->private_data;
1580 ret = spu_acquire(ctx);
1585 if (file->f_flags & O_NONBLOCK) {
1586 status = ctx->ops->read_mfc_tagstatus(ctx);
1587 if (!(status & ctx->tagwait))
1590 /* XXX(hch): shouldn't we clear ret here? */
1591 ctx->tagwait &= ~status;
1593 ret = spufs_wait(ctx->mfc_wq,
1594 spufs_read_mfc_tagstatus(ctx, &status));
1602 if (copy_to_user(buffer, &status, 4))
1609 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1611 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1612 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1623 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1627 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1628 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1633 switch (cmd->size & 0xf) {
1654 pr_debug("invalid DMA alignment %x for size %x\n",
1655 cmd->lsa & 0xf, cmd->size);
1659 if (cmd->size > 16 * 1024) {
1660 pr_debug("invalid DMA size %x\n", cmd->size);
1664 if (cmd->tag & 0xfff0) {
1665 /* we reserve the higher tag numbers for kernel use */
1666 pr_debug("invalid DMA tag\n");
1671 /* not supported in this version */
1672 pr_debug("invalid DMA class\n");
1679 static int spu_send_mfc_command(struct spu_context *ctx,
1680 struct mfc_dma_command cmd,
1683 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1684 if (*error == -EAGAIN) {
1685 /* wait for any tag group to complete
1686 so we have space for the new command */
1687 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1688 /* try again, because the queue might be
1690 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1691 if (*error == -EAGAIN)
1697 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1698 size_t size, loff_t *pos)
1700 struct spu_context *ctx = file->private_data;
1701 struct mfc_dma_command cmd;
1704 if (size != sizeof cmd)
1708 if (copy_from_user(&cmd, buffer, sizeof cmd))
1711 ret = spufs_check_valid_dma(&cmd);
1715 ret = spu_acquire(ctx);
1719 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1723 if (file->f_flags & O_NONBLOCK) {
1724 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1727 ret = spufs_wait(ctx->mfc_wq,
1728 spu_send_mfc_command(ctx, cmd, &status));
1736 ctx->tagwait |= 1 << cmd.tag;
1745 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1747 struct spu_context *ctx = file->private_data;
1748 u32 free_elements, tagstatus;
1751 poll_wait(file, &ctx->mfc_wq, wait);
1754 * For now keep this uninterruptible and also ignore the rule
1755 * that poll should not sleep. Will be fixed later.
1757 mutex_lock(&ctx->state_mutex);
1758 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1759 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1760 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1764 if (free_elements & 0xffff)
1765 mask |= POLLOUT | POLLWRNORM;
1766 if (tagstatus & ctx->tagwait)
1767 mask |= POLLIN | POLLRDNORM;
1769 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1770 free_elements, tagstatus, ctx->tagwait);
1775 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1777 struct spu_context *ctx = file->private_data;
1780 ret = spu_acquire(ctx);
1784 /* this currently hangs */
1785 ret = spufs_wait(ctx->mfc_wq,
1786 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1789 ret = spufs_wait(ctx->mfc_wq,
1790 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1800 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1803 return spufs_mfc_flush(file, NULL);
1806 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1808 struct spu_context *ctx = file->private_data;
1810 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1813 static const struct file_operations spufs_mfc_fops = {
1814 .open = spufs_mfc_open,
1815 .release = spufs_mfc_release,
1816 .read = spufs_mfc_read,
1817 .write = spufs_mfc_write,
1818 .poll = spufs_mfc_poll,
1819 .flush = spufs_mfc_flush,
1820 .fsync = spufs_mfc_fsync,
1821 .fasync = spufs_mfc_fasync,
1822 .mmap = spufs_mfc_mmap,
1825 static int spufs_npc_set(void *data, u64 val)
1827 struct spu_context *ctx = data;
1830 ret = spu_acquire(ctx);
1833 ctx->ops->npc_write(ctx, val);
1839 static u64 spufs_npc_get(struct spu_context *ctx)
1841 return ctx->ops->npc_read(ctx);
1843 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1844 "0x%llx\n", SPU_ATTR_ACQUIRE);
1846 static int spufs_decr_set(void *data, u64 val)
1848 struct spu_context *ctx = data;
1849 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1852 ret = spu_acquire_saved(ctx);
1855 lscsa->decr.slot[0] = (u32) val;
1856 spu_release_saved(ctx);
1861 static u64 spufs_decr_get(struct spu_context *ctx)
1863 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1864 return lscsa->decr.slot[0];
1866 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1867 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
1869 static int spufs_decr_status_set(void *data, u64 val)
1871 struct spu_context *ctx = data;
1874 ret = spu_acquire_saved(ctx);
1878 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1880 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1881 spu_release_saved(ctx);
1886 static u64 spufs_decr_status_get(struct spu_context *ctx)
1888 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1889 return SPU_DECR_STATUS_RUNNING;
1893 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1894 spufs_decr_status_set, "0x%llx\n",
1895 SPU_ATTR_ACQUIRE_SAVED);
1897 static int spufs_event_mask_set(void *data, u64 val)
1899 struct spu_context *ctx = data;
1900 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1903 ret = spu_acquire_saved(ctx);
1906 lscsa->event_mask.slot[0] = (u32) val;
1907 spu_release_saved(ctx);
1912 static u64 spufs_event_mask_get(struct spu_context *ctx)
1914 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1915 return lscsa->event_mask.slot[0];
1918 DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1919 spufs_event_mask_set, "0x%llx\n",
1920 SPU_ATTR_ACQUIRE_SAVED);
1922 static u64 spufs_event_status_get(struct spu_context *ctx)
1924 struct spu_state *state = &ctx->csa;
1926 stat = state->spu_chnlcnt_RW[0];
1928 return state->spu_chnldata_RW[0];
1931 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1932 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1934 static int spufs_srr0_set(void *data, u64 val)
1936 struct spu_context *ctx = data;
1937 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1940 ret = spu_acquire_saved(ctx);
1943 lscsa->srr0.slot[0] = (u32) val;
1944 spu_release_saved(ctx);
1949 static u64 spufs_srr0_get(struct spu_context *ctx)
1951 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1952 return lscsa->srr0.slot[0];
1954 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1955 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1957 static u64 spufs_id_get(struct spu_context *ctx)
1961 if (ctx->state == SPU_STATE_RUNNABLE)
1962 num = ctx->spu->number;
1964 num = (unsigned int)-1;
1968 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1971 static u64 spufs_object_id_get(struct spu_context *ctx)
1973 /* FIXME: Should there really be no locking here? */
1974 return ctx->object_id;
1977 static int spufs_object_id_set(void *data, u64 id)
1979 struct spu_context *ctx = data;
1980 ctx->object_id = id;
1985 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1986 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
1988 static u64 spufs_lslr_get(struct spu_context *ctx)
1990 return ctx->csa.priv2.spu_lslr_RW;
1992 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1993 SPU_ATTR_ACQUIRE_SAVED);
1995 static int spufs_info_open(struct inode *inode, struct file *file)
1997 struct spufs_inode_info *i = SPUFS_I(inode);
1998 struct spu_context *ctx = i->i_ctx;
1999 file->private_data = ctx;
2003 static int spufs_caps_show(struct seq_file *s, void *private)
2005 struct spu_context *ctx = s->private;
2007 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2008 seq_puts(s, "sched\n");
2009 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2010 seq_puts(s, "step\n");
2014 static int spufs_caps_open(struct inode *inode, struct file *file)
2016 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2019 static const struct file_operations spufs_caps_fops = {
2020 .open = spufs_caps_open,
2022 .llseek = seq_lseek,
2023 .release = single_release,
2026 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2027 char __user *buf, size_t len, loff_t *pos)
2031 /* EOF if there's no entry in the mbox */
2032 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2035 data = ctx->csa.prob.pu_mb_R;
2037 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2040 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2041 size_t len, loff_t *pos)
2044 struct spu_context *ctx = file->private_data;
2046 if (!access_ok(VERIFY_WRITE, buf, len))
2049 ret = spu_acquire_saved(ctx);
2052 spin_lock(&ctx->csa.register_lock);
2053 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
2054 spin_unlock(&ctx->csa.register_lock);
2055 spu_release_saved(ctx);
2060 static const struct file_operations spufs_mbox_info_fops = {
2061 .open = spufs_info_open,
2062 .read = spufs_mbox_info_read,
2063 .llseek = generic_file_llseek,
2066 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2067 char __user *buf, size_t len, loff_t *pos)
2071 /* EOF if there's no entry in the ibox */
2072 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2075 data = ctx->csa.priv2.puint_mb_R;
2077 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2080 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2081 size_t len, loff_t *pos)
2083 struct spu_context *ctx = file->private_data;
2086 if (!access_ok(VERIFY_WRITE, buf, len))
2089 ret = spu_acquire_saved(ctx);
2092 spin_lock(&ctx->csa.register_lock);
2093 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
2094 spin_unlock(&ctx->csa.register_lock);
2095 spu_release_saved(ctx);
2100 static const struct file_operations spufs_ibox_info_fops = {
2101 .open = spufs_info_open,
2102 .read = spufs_ibox_info_read,
2103 .llseek = generic_file_llseek,
2106 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2107 char __user *buf, size_t len, loff_t *pos)
2113 wbox_stat = ctx->csa.prob.mb_stat_R;
2114 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2115 for (i = 0; i < cnt; i++) {
2116 data[i] = ctx->csa.spu_mailbox_data[i];
2119 return simple_read_from_buffer(buf, len, pos, &data,
2123 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2124 size_t len, loff_t *pos)
2126 struct spu_context *ctx = file->private_data;
2129 if (!access_ok(VERIFY_WRITE, buf, len))
2132 ret = spu_acquire_saved(ctx);
2135 spin_lock(&ctx->csa.register_lock);
2136 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
2137 spin_unlock(&ctx->csa.register_lock);
2138 spu_release_saved(ctx);
2143 static const struct file_operations spufs_wbox_info_fops = {
2144 .open = spufs_info_open,
2145 .read = spufs_wbox_info_read,
2146 .llseek = generic_file_llseek,
2149 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2150 char __user *buf, size_t len, loff_t *pos)
2152 struct spu_dma_info info;
2153 struct mfc_cq_sr *qp, *spuqp;
2156 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2157 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2158 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2159 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2160 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2161 for (i = 0; i < 16; i++) {
2162 qp = &info.dma_info_command_data[i];
2163 spuqp = &ctx->csa.priv2.spuq[i];
2165 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2166 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2167 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2168 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2171 return simple_read_from_buffer(buf, len, pos, &info,
2175 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2176 size_t len, loff_t *pos)
2178 struct spu_context *ctx = file->private_data;
2181 if (!access_ok(VERIFY_WRITE, buf, len))
2184 ret = spu_acquire_saved(ctx);
2187 spin_lock(&ctx->csa.register_lock);
2188 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2189 spin_unlock(&ctx->csa.register_lock);
2190 spu_release_saved(ctx);
2195 static const struct file_operations spufs_dma_info_fops = {
2196 .open = spufs_info_open,
2197 .read = spufs_dma_info_read,
2200 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2201 char __user *buf, size_t len, loff_t *pos)
2203 struct spu_proxydma_info info;
2204 struct mfc_cq_sr *qp, *puqp;
2205 int ret = sizeof info;
2211 if (!access_ok(VERIFY_WRITE, buf, len))
2214 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2215 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2216 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2217 for (i = 0; i < 8; i++) {
2218 qp = &info.proxydma_info_command_data[i];
2219 puqp = &ctx->csa.priv2.puq[i];
2221 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2222 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2223 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2224 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2227 return simple_read_from_buffer(buf, len, pos, &info,
2231 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2232 size_t len, loff_t *pos)
2234 struct spu_context *ctx = file->private_data;
2237 ret = spu_acquire_saved(ctx);
2240 spin_lock(&ctx->csa.register_lock);
2241 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2242 spin_unlock(&ctx->csa.register_lock);
2243 spu_release_saved(ctx);
2248 static const struct file_operations spufs_proxydma_info_fops = {
2249 .open = spufs_info_open,
2250 .read = spufs_proxydma_info_read,
2253 static int spufs_show_tid(struct seq_file *s, void *private)
2255 struct spu_context *ctx = s->private;
2257 seq_printf(s, "%d\n", ctx->tid);
2261 static int spufs_tid_open(struct inode *inode, struct file *file)
2263 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2266 static const struct file_operations spufs_tid_fops = {
2267 .open = spufs_tid_open,
2269 .llseek = seq_lseek,
2270 .release = single_release,
2273 static const char *ctx_state_names[] = {
2274 "user", "system", "iowait", "loaded"
2277 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2278 enum spu_utilization_state state)
2281 unsigned long long time = ctx->stats.times[state];
2284 * In general, utilization statistics are updated by the controlling
2285 * thread as the spu context moves through various well defined
2286 * state transitions, but if the context is lazily loaded its
2287 * utilization statistics are not updated as the controlling thread
2288 * is not tightly coupled with the execution of the spu context. We
2289 * calculate and apply the time delta from the last recorded state
2290 * of the spu context.
2292 if (ctx->spu && ctx->stats.util_state == state) {
2294 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2297 return time / NSEC_PER_MSEC;
2300 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2302 unsigned long long slb_flts = ctx->stats.slb_flt;
2304 if (ctx->state == SPU_STATE_RUNNABLE) {
2305 slb_flts += (ctx->spu->stats.slb_flt -
2306 ctx->stats.slb_flt_base);
2312 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2314 unsigned long long class2_intrs = ctx->stats.class2_intr;
2316 if (ctx->state == SPU_STATE_RUNNABLE) {
2317 class2_intrs += (ctx->spu->stats.class2_intr -
2318 ctx->stats.class2_intr_base);
2321 return class2_intrs;
2325 static int spufs_show_stat(struct seq_file *s, void *private)
2327 struct spu_context *ctx = s->private;
2330 ret = spu_acquire(ctx);
2334 seq_printf(s, "%s %llu %llu %llu %llu "
2335 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2336 ctx_state_names[ctx->stats.util_state],
2337 spufs_acct_time(ctx, SPU_UTIL_USER),
2338 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2339 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2340 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2341 ctx->stats.vol_ctx_switch,
2342 ctx->stats.invol_ctx_switch,
2343 spufs_slb_flts(ctx),
2344 ctx->stats.hash_flt,
2347 spufs_class2_intrs(ctx),
2348 ctx->stats.libassist);
2353 static int spufs_stat_open(struct inode *inode, struct file *file)
2355 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2358 static const struct file_operations spufs_stat_fops = {
2359 .open = spufs_stat_open,
2361 .llseek = seq_lseek,
2362 .release = single_release,
2366 struct tree_descr spufs_dir_contents[] = {
2367 { "capabilities", &spufs_caps_fops, 0444, },
2368 { "mem", &spufs_mem_fops, 0666, },
2369 { "regs", &spufs_regs_fops, 0666, },
2370 { "mbox", &spufs_mbox_fops, 0444, },
2371 { "ibox", &spufs_ibox_fops, 0444, },
2372 { "wbox", &spufs_wbox_fops, 0222, },
2373 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2374 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2375 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2376 { "signal1", &spufs_signal1_fops, 0666, },
2377 { "signal2", &spufs_signal2_fops, 0666, },
2378 { "signal1_type", &spufs_signal1_type, 0666, },
2379 { "signal2_type", &spufs_signal2_type, 0666, },
2380 { "cntl", &spufs_cntl_fops, 0666, },
2381 { "fpcr", &spufs_fpcr_fops, 0666, },
2382 { "lslr", &spufs_lslr_ops, 0444, },
2383 { "mfc", &spufs_mfc_fops, 0666, },
2384 { "mss", &spufs_mss_fops, 0666, },
2385 { "npc", &spufs_npc_ops, 0666, },
2386 { "srr0", &spufs_srr0_ops, 0666, },
2387 { "decr", &spufs_decr_ops, 0666, },
2388 { "decr_status", &spufs_decr_status_ops, 0666, },
2389 { "event_mask", &spufs_event_mask_ops, 0666, },
2390 { "event_status", &spufs_event_status_ops, 0444, },
2391 { "psmap", &spufs_psmap_fops, 0666, },
2392 { "phys-id", &spufs_id_ops, 0666, },
2393 { "object-id", &spufs_object_id_ops, 0666, },
2394 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2395 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2396 { "wbox_info", &spufs_wbox_info_fops, 0444, },
2397 { "dma_info", &spufs_dma_info_fops, 0444, },
2398 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
2399 { "tid", &spufs_tid_fops, 0444, },
2400 { "stat", &spufs_stat_fops, 0444, },
2404 struct tree_descr spufs_dir_nosched_contents[] = {
2405 { "capabilities", &spufs_caps_fops, 0444, },
2406 { "mem", &spufs_mem_fops, 0666, },
2407 { "mbox", &spufs_mbox_fops, 0444, },
2408 { "ibox", &spufs_ibox_fops, 0444, },
2409 { "wbox", &spufs_wbox_fops, 0222, },
2410 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2411 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2412 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2413 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2414 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2415 { "signal1_type", &spufs_signal1_type, 0666, },
2416 { "signal2_type", &spufs_signal2_type, 0666, },
2417 { "mss", &spufs_mss_fops, 0666, },
2418 { "mfc", &spufs_mfc_fops, 0666, },
2419 { "cntl", &spufs_cntl_fops, 0666, },
2420 { "npc", &spufs_npc_ops, 0666, },
2421 { "psmap", &spufs_psmap_fops, 0666, },
2422 { "phys-id", &spufs_id_ops, 0666, },
2423 { "object-id", &spufs_object_id_ops, 0666, },
2424 { "tid", &spufs_tid_fops, 0444, },
2425 { "stat", &spufs_stat_fops, 0444, },
2429 struct spufs_coredump_reader spufs_coredump_read[] = {
2430 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2431 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2432 { "lslr", NULL, spufs_lslr_get, 19 },
2433 { "decr", NULL, spufs_decr_get, 19 },
2434 { "decr_status", NULL, spufs_decr_status_get, 19 },
2435 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2436 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2437 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
2438 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2439 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2440 { "event_mask", NULL, spufs_event_mask_get, 19 },
2441 { "event_status", NULL, spufs_event_status_get, 19 },
2442 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2443 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2444 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2445 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2446 { "proxydma_info", __spufs_proxydma_info_read,
2447 NULL, sizeof(struct spu_proxydma_info)},
2448 { "object-id", NULL, spufs_object_id_get, 19 },
2449 { "npc", NULL, spufs_npc_get, 19 },