[POWERPC] spufs: Internal __spufs_get_foo() routines should take a spu_context *
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / powerpc / platforms / cell / spufs / file.c
blob4cd34e53acaaf6ad6e19593f2b0e11d56d8a9371
1 /*
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)
11 * any later version.
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.
23 #undef DEBUG
25 #include <linux/fs.h>
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>
33 #include <asm/io.h>
34 #include <asm/semaphore.h>
35 #include <asm/spu.h>
36 #include <asm/spu_info.h>
37 #include <asm/uaccess.h>
39 #include "spufs.h"
41 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
44 static int
45 spufs_mem_open(struct inode *inode, struct file *file)
47 struct spufs_inode_info *i = SPUFS_I(inode);
48 struct spu_context *ctx = i->i_ctx;
50 mutex_lock(&ctx->mapping_lock);
51 file->private_data = ctx;
52 if (!i->i_openers++)
53 ctx->local_store = inode->i_mapping;
54 mutex_unlock(&ctx->mapping_lock);
55 return 0;
58 static int
59 spufs_mem_release(struct inode *inode, struct file *file)
61 struct spufs_inode_info *i = SPUFS_I(inode);
62 struct spu_context *ctx = i->i_ctx;
64 mutex_lock(&ctx->mapping_lock);
65 if (!--i->i_openers)
66 ctx->local_store = NULL;
67 mutex_unlock(&ctx->mapping_lock);
68 return 0;
71 static ssize_t
72 __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
73 size_t size, loff_t *pos)
75 char *local_store = ctx->ops->get_ls(ctx);
76 return simple_read_from_buffer(buffer, size, pos, local_store,
77 LS_SIZE);
80 static ssize_t
81 spufs_mem_read(struct file *file, char __user *buffer,
82 size_t size, loff_t *pos)
84 struct spu_context *ctx = file->private_data;
85 ssize_t ret;
87 spu_acquire(ctx);
88 ret = __spufs_mem_read(ctx, buffer, size, pos);
89 spu_release(ctx);
90 return ret;
93 static ssize_t
94 spufs_mem_write(struct file *file, const char __user *buffer,
95 size_t size, loff_t *ppos)
97 struct spu_context *ctx = file->private_data;
98 char *local_store;
99 loff_t pos = *ppos;
100 int ret;
102 if (pos < 0)
103 return -EINVAL;
104 if (pos > LS_SIZE)
105 return -EFBIG;
106 if (size > LS_SIZE - pos)
107 size = LS_SIZE - pos;
109 spu_acquire(ctx);
110 local_store = ctx->ops->get_ls(ctx);
111 ret = copy_from_user(local_store + pos, buffer, size);
112 spu_release(ctx);
114 if (ret)
115 return -EFAULT;
116 *ppos = pos + size;
117 return size;
120 static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
121 unsigned long address)
123 struct spu_context *ctx = vma->vm_file->private_data;
124 unsigned long pfn, offset, addr0 = address;
125 #ifdef CONFIG_SPU_FS_64K_LS
126 struct spu_state *csa = &ctx->csa;
127 int psize;
129 /* Check what page size we are using */
130 psize = get_slice_psize(vma->vm_mm, address);
132 /* Some sanity checking */
133 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
135 /* Wow, 64K, cool, we need to align the address though */
136 if (csa->use_big_pages) {
137 BUG_ON(vma->vm_start & 0xffff);
138 address &= ~0xfffful;
140 #endif /* CONFIG_SPU_FS_64K_LS */
142 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
143 if (offset >= LS_SIZE)
144 return NOPFN_SIGBUS;
146 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
147 addr0, address, offset);
149 spu_acquire(ctx);
151 if (ctx->state == SPU_STATE_SAVED) {
152 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
153 & ~_PAGE_NO_CACHE);
154 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
155 } else {
156 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
157 | _PAGE_NO_CACHE);
158 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
160 vm_insert_pfn(vma, address, pfn);
162 spu_release(ctx);
164 return NOPFN_REFAULT;
168 static struct vm_operations_struct spufs_mem_mmap_vmops = {
169 .nopfn = spufs_mem_mmap_nopfn,
172 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
174 #ifdef CONFIG_SPU_FS_64K_LS
175 struct spu_context *ctx = file->private_data;
176 struct spu_state *csa = &ctx->csa;
178 /* Sanity check VMA alignment */
179 if (csa->use_big_pages) {
180 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
181 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
182 vma->vm_pgoff);
183 if (vma->vm_start & 0xffff)
184 return -EINVAL;
185 if (vma->vm_pgoff & 0xf)
186 return -EINVAL;
188 #endif /* CONFIG_SPU_FS_64K_LS */
190 if (!(vma->vm_flags & VM_SHARED))
191 return -EINVAL;
193 vma->vm_flags |= VM_IO | VM_PFNMAP;
194 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
195 | _PAGE_NO_CACHE);
197 vma->vm_ops = &spufs_mem_mmap_vmops;
198 return 0;
201 #ifdef CONFIG_SPU_FS_64K_LS
202 static unsigned long spufs_get_unmapped_area(struct file *file,
203 unsigned long addr, unsigned long len, unsigned long pgoff,
204 unsigned long flags)
206 struct spu_context *ctx = file->private_data;
207 struct spu_state *csa = &ctx->csa;
209 /* If not using big pages, fallback to normal MM g_u_a */
210 if (!csa->use_big_pages)
211 return current->mm->get_unmapped_area(file, addr, len,
212 pgoff, flags);
214 /* Else, try to obtain a 64K pages slice */
215 return slice_get_unmapped_area(addr, len, flags,
216 MMU_PAGE_64K, 1, 0);
218 #endif /* CONFIG_SPU_FS_64K_LS */
220 static const struct file_operations spufs_mem_fops = {
221 .open = spufs_mem_open,
222 .release = spufs_mem_release,
223 .read = spufs_mem_read,
224 .write = spufs_mem_write,
225 .llseek = generic_file_llseek,
226 .mmap = spufs_mem_mmap,
227 #ifdef CONFIG_SPU_FS_64K_LS
228 .get_unmapped_area = spufs_get_unmapped_area,
229 #endif
232 static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
233 unsigned long address,
234 unsigned long ps_offs,
235 unsigned long ps_size)
237 struct spu_context *ctx = vma->vm_file->private_data;
238 unsigned long area, offset = address - vma->vm_start;
239 int ret;
241 offset += vma->vm_pgoff << PAGE_SHIFT;
242 if (offset >= ps_size)
243 return NOPFN_SIGBUS;
245 /* error here usually means a signal.. we might want to test
246 * the error code more precisely though
248 ret = spu_acquire_runnable(ctx, 0);
249 if (ret)
250 return NOPFN_REFAULT;
252 area = ctx->spu->problem_phys + ps_offs;
253 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
254 spu_release(ctx);
256 return NOPFN_REFAULT;
259 #if SPUFS_MMAP_4K
260 static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
261 unsigned long address)
263 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
266 static struct vm_operations_struct spufs_cntl_mmap_vmops = {
267 .nopfn = spufs_cntl_mmap_nopfn,
271 * mmap support for problem state control area [0x4000 - 0x4fff].
273 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
275 if (!(vma->vm_flags & VM_SHARED))
276 return -EINVAL;
278 vma->vm_flags |= VM_IO | VM_PFNMAP;
279 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
280 | _PAGE_NO_CACHE | _PAGE_GUARDED);
282 vma->vm_ops = &spufs_cntl_mmap_vmops;
283 return 0;
285 #else /* SPUFS_MMAP_4K */
286 #define spufs_cntl_mmap NULL
287 #endif /* !SPUFS_MMAP_4K */
289 static u64 spufs_cntl_get(void *data)
291 struct spu_context *ctx = data;
292 u64 val;
294 spu_acquire(ctx);
295 val = ctx->ops->status_read(ctx);
296 spu_release(ctx);
298 return val;
301 static void spufs_cntl_set(void *data, u64 val)
303 struct spu_context *ctx = data;
305 spu_acquire(ctx);
306 ctx->ops->runcntl_write(ctx, val);
307 spu_release(ctx);
310 static int spufs_cntl_open(struct inode *inode, struct file *file)
312 struct spufs_inode_info *i = SPUFS_I(inode);
313 struct spu_context *ctx = i->i_ctx;
315 mutex_lock(&ctx->mapping_lock);
316 file->private_data = ctx;
317 if (!i->i_openers++)
318 ctx->cntl = inode->i_mapping;
319 mutex_unlock(&ctx->mapping_lock);
320 return simple_attr_open(inode, file, spufs_cntl_get,
321 spufs_cntl_set, "0x%08lx");
324 static int
325 spufs_cntl_release(struct inode *inode, struct file *file)
327 struct spufs_inode_info *i = SPUFS_I(inode);
328 struct spu_context *ctx = i->i_ctx;
330 simple_attr_close(inode, file);
332 mutex_lock(&ctx->mapping_lock);
333 if (!--i->i_openers)
334 ctx->cntl = NULL;
335 mutex_unlock(&ctx->mapping_lock);
336 return 0;
339 static const struct file_operations spufs_cntl_fops = {
340 .open = spufs_cntl_open,
341 .release = spufs_cntl_release,
342 .read = simple_attr_read,
343 .write = simple_attr_write,
344 .mmap = spufs_cntl_mmap,
347 static int
348 spufs_regs_open(struct inode *inode, struct file *file)
350 struct spufs_inode_info *i = SPUFS_I(inode);
351 file->private_data = i->i_ctx;
352 return 0;
355 static ssize_t
356 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
357 size_t size, loff_t *pos)
359 struct spu_lscsa *lscsa = ctx->csa.lscsa;
360 return simple_read_from_buffer(buffer, size, pos,
361 lscsa->gprs, sizeof lscsa->gprs);
364 static ssize_t
365 spufs_regs_read(struct file *file, char __user *buffer,
366 size_t size, loff_t *pos)
368 int ret;
369 struct spu_context *ctx = file->private_data;
371 spu_acquire_saved(ctx);
372 ret = __spufs_regs_read(ctx, buffer, size, pos);
373 spu_release_saved(ctx);
374 return ret;
377 static ssize_t
378 spufs_regs_write(struct file *file, const char __user *buffer,
379 size_t size, loff_t *pos)
381 struct spu_context *ctx = file->private_data;
382 struct spu_lscsa *lscsa = ctx->csa.lscsa;
383 int ret;
385 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
386 if (size <= 0)
387 return -EFBIG;
388 *pos += size;
390 spu_acquire_saved(ctx);
392 ret = copy_from_user(lscsa->gprs + *pos - size,
393 buffer, size) ? -EFAULT : size;
395 spu_release_saved(ctx);
396 return ret;
399 static const struct file_operations spufs_regs_fops = {
400 .open = spufs_regs_open,
401 .read = spufs_regs_read,
402 .write = spufs_regs_write,
403 .llseek = generic_file_llseek,
406 static ssize_t
407 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
408 size_t size, loff_t * pos)
410 struct spu_lscsa *lscsa = ctx->csa.lscsa;
411 return simple_read_from_buffer(buffer, size, pos,
412 &lscsa->fpcr, sizeof(lscsa->fpcr));
415 static ssize_t
416 spufs_fpcr_read(struct file *file, char __user * buffer,
417 size_t size, loff_t * pos)
419 int ret;
420 struct spu_context *ctx = file->private_data;
422 spu_acquire_saved(ctx);
423 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
424 spu_release_saved(ctx);
425 return ret;
428 static ssize_t
429 spufs_fpcr_write(struct file *file, const char __user * buffer,
430 size_t size, loff_t * pos)
432 struct spu_context *ctx = file->private_data;
433 struct spu_lscsa *lscsa = ctx->csa.lscsa;
434 int ret;
436 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
437 if (size <= 0)
438 return -EFBIG;
439 *pos += size;
441 spu_acquire_saved(ctx);
443 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
444 buffer, size) ? -EFAULT : size;
446 spu_release_saved(ctx);
447 return ret;
450 static const struct file_operations spufs_fpcr_fops = {
451 .open = spufs_regs_open,
452 .read = spufs_fpcr_read,
453 .write = spufs_fpcr_write,
454 .llseek = generic_file_llseek,
457 /* generic open function for all pipe-like files */
458 static int spufs_pipe_open(struct inode *inode, struct file *file)
460 struct spufs_inode_info *i = SPUFS_I(inode);
461 file->private_data = i->i_ctx;
463 return nonseekable_open(inode, file);
467 * Read as many bytes from the mailbox as possible, until
468 * one of the conditions becomes true:
470 * - no more data available in the mailbox
471 * - end of the user provided buffer
472 * - end of the mapped area
474 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
475 size_t len, loff_t *pos)
477 struct spu_context *ctx = file->private_data;
478 u32 mbox_data, __user *udata;
479 ssize_t count;
481 if (len < 4)
482 return -EINVAL;
484 if (!access_ok(VERIFY_WRITE, buf, len))
485 return -EFAULT;
487 udata = (void __user *)buf;
489 spu_acquire(ctx);
490 for (count = 0; (count + 4) <= len; count += 4, udata++) {
491 int ret;
492 ret = ctx->ops->mbox_read(ctx, &mbox_data);
493 if (ret == 0)
494 break;
497 * at the end of the mapped area, we can fault
498 * but still need to return the data we have
499 * read successfully so far.
501 ret = __put_user(mbox_data, udata);
502 if (ret) {
503 if (!count)
504 count = -EFAULT;
505 break;
508 spu_release(ctx);
510 if (!count)
511 count = -EAGAIN;
513 return count;
516 static const struct file_operations spufs_mbox_fops = {
517 .open = spufs_pipe_open,
518 .read = spufs_mbox_read,
521 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
522 size_t len, loff_t *pos)
524 struct spu_context *ctx = file->private_data;
525 u32 mbox_stat;
527 if (len < 4)
528 return -EINVAL;
530 spu_acquire(ctx);
532 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
534 spu_release(ctx);
536 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
537 return -EFAULT;
539 return 4;
542 static const struct file_operations spufs_mbox_stat_fops = {
543 .open = spufs_pipe_open,
544 .read = spufs_mbox_stat_read,
547 /* low-level ibox access function */
548 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
550 return ctx->ops->ibox_read(ctx, data);
553 static int spufs_ibox_fasync(int fd, struct file *file, int on)
555 struct spu_context *ctx = file->private_data;
557 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
560 /* interrupt-level ibox callback function. */
561 void spufs_ibox_callback(struct spu *spu)
563 struct spu_context *ctx = spu->ctx;
565 wake_up_all(&ctx->ibox_wq);
566 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
570 * Read as many bytes from the interrupt mailbox as possible, until
571 * one of the conditions becomes true:
573 * - no more data available in the mailbox
574 * - end of the user provided buffer
575 * - end of the mapped area
577 * If the file is opened without O_NONBLOCK, we wait here until
578 * any data is available, but return when we have been able to
579 * read something.
581 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
582 size_t len, loff_t *pos)
584 struct spu_context *ctx = file->private_data;
585 u32 ibox_data, __user *udata;
586 ssize_t count;
588 if (len < 4)
589 return -EINVAL;
591 if (!access_ok(VERIFY_WRITE, buf, len))
592 return -EFAULT;
594 udata = (void __user *)buf;
596 spu_acquire(ctx);
598 /* wait only for the first element */
599 count = 0;
600 if (file->f_flags & O_NONBLOCK) {
601 if (!spu_ibox_read(ctx, &ibox_data))
602 count = -EAGAIN;
603 } else {
604 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
606 if (count)
607 goto out;
609 /* if we can't write at all, return -EFAULT */
610 count = __put_user(ibox_data, udata);
611 if (count)
612 goto out;
614 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
615 int ret;
616 ret = ctx->ops->ibox_read(ctx, &ibox_data);
617 if (ret == 0)
618 break;
620 * at the end of the mapped area, we can fault
621 * but still need to return the data we have
622 * read successfully so far.
624 ret = __put_user(ibox_data, udata);
625 if (ret)
626 break;
629 out:
630 spu_release(ctx);
632 return count;
635 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
637 struct spu_context *ctx = file->private_data;
638 unsigned int mask;
640 poll_wait(file, &ctx->ibox_wq, wait);
642 spu_acquire(ctx);
643 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
644 spu_release(ctx);
646 return mask;
649 static const struct file_operations spufs_ibox_fops = {
650 .open = spufs_pipe_open,
651 .read = spufs_ibox_read,
652 .poll = spufs_ibox_poll,
653 .fasync = spufs_ibox_fasync,
656 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
657 size_t len, loff_t *pos)
659 struct spu_context *ctx = file->private_data;
660 u32 ibox_stat;
662 if (len < 4)
663 return -EINVAL;
665 spu_acquire(ctx);
666 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
667 spu_release(ctx);
669 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
670 return -EFAULT;
672 return 4;
675 static const struct file_operations spufs_ibox_stat_fops = {
676 .open = spufs_pipe_open,
677 .read = spufs_ibox_stat_read,
680 /* low-level mailbox write */
681 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
683 return ctx->ops->wbox_write(ctx, data);
686 static int spufs_wbox_fasync(int fd, struct file *file, int on)
688 struct spu_context *ctx = file->private_data;
689 int ret;
691 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
693 return ret;
696 /* interrupt-level wbox callback function. */
697 void spufs_wbox_callback(struct spu *spu)
699 struct spu_context *ctx = spu->ctx;
701 wake_up_all(&ctx->wbox_wq);
702 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
706 * Write as many bytes to the interrupt mailbox as possible, until
707 * one of the conditions becomes true:
709 * - the mailbox is full
710 * - end of the user provided buffer
711 * - end of the mapped area
713 * If the file is opened without O_NONBLOCK, we wait here until
714 * space is availabyl, but return when we have been able to
715 * write something.
717 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
718 size_t len, loff_t *pos)
720 struct spu_context *ctx = file->private_data;
721 u32 wbox_data, __user *udata;
722 ssize_t count;
724 if (len < 4)
725 return -EINVAL;
727 udata = (void __user *)buf;
728 if (!access_ok(VERIFY_READ, buf, len))
729 return -EFAULT;
731 if (__get_user(wbox_data, udata))
732 return -EFAULT;
734 spu_acquire(ctx);
737 * make sure we can at least write one element, by waiting
738 * in case of !O_NONBLOCK
740 count = 0;
741 if (file->f_flags & O_NONBLOCK) {
742 if (!spu_wbox_write(ctx, wbox_data))
743 count = -EAGAIN;
744 } else {
745 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
748 if (count)
749 goto out;
751 /* write aѕ much as possible */
752 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
753 int ret;
754 ret = __get_user(wbox_data, udata);
755 if (ret)
756 break;
758 ret = spu_wbox_write(ctx, wbox_data);
759 if (ret == 0)
760 break;
763 out:
764 spu_release(ctx);
765 return count;
768 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
770 struct spu_context *ctx = file->private_data;
771 unsigned int mask;
773 poll_wait(file, &ctx->wbox_wq, wait);
775 spu_acquire(ctx);
776 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
777 spu_release(ctx);
779 return mask;
782 static const struct file_operations spufs_wbox_fops = {
783 .open = spufs_pipe_open,
784 .write = spufs_wbox_write,
785 .poll = spufs_wbox_poll,
786 .fasync = spufs_wbox_fasync,
789 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
790 size_t len, loff_t *pos)
792 struct spu_context *ctx = file->private_data;
793 u32 wbox_stat;
795 if (len < 4)
796 return -EINVAL;
798 spu_acquire(ctx);
799 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
800 spu_release(ctx);
802 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
803 return -EFAULT;
805 return 4;
808 static const struct file_operations spufs_wbox_stat_fops = {
809 .open = spufs_pipe_open,
810 .read = spufs_wbox_stat_read,
813 static int spufs_signal1_open(struct inode *inode, struct file *file)
815 struct spufs_inode_info *i = SPUFS_I(inode);
816 struct spu_context *ctx = i->i_ctx;
818 mutex_lock(&ctx->mapping_lock);
819 file->private_data = ctx;
820 if (!i->i_openers++)
821 ctx->signal1 = inode->i_mapping;
822 mutex_unlock(&ctx->mapping_lock);
823 return nonseekable_open(inode, file);
826 static int
827 spufs_signal1_release(struct inode *inode, struct file *file)
829 struct spufs_inode_info *i = SPUFS_I(inode);
830 struct spu_context *ctx = i->i_ctx;
832 mutex_lock(&ctx->mapping_lock);
833 if (!--i->i_openers)
834 ctx->signal1 = NULL;
835 mutex_unlock(&ctx->mapping_lock);
836 return 0;
839 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
840 size_t len, loff_t *pos)
842 int ret = 0;
843 u32 data;
845 if (len < 4)
846 return -EINVAL;
848 if (ctx->csa.spu_chnlcnt_RW[3]) {
849 data = ctx->csa.spu_chnldata_RW[3];
850 ret = 4;
853 if (!ret)
854 goto out;
856 if (copy_to_user(buf, &data, 4))
857 return -EFAULT;
859 out:
860 return ret;
863 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
864 size_t len, loff_t *pos)
866 int ret;
867 struct spu_context *ctx = file->private_data;
869 spu_acquire_saved(ctx);
870 ret = __spufs_signal1_read(ctx, buf, len, pos);
871 spu_release_saved(ctx);
873 return ret;
876 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
877 size_t len, loff_t *pos)
879 struct spu_context *ctx;
880 u32 data;
882 ctx = file->private_data;
884 if (len < 4)
885 return -EINVAL;
887 if (copy_from_user(&data, buf, 4))
888 return -EFAULT;
890 spu_acquire(ctx);
891 ctx->ops->signal1_write(ctx, data);
892 spu_release(ctx);
894 return 4;
897 static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
898 unsigned long address)
900 #if PAGE_SIZE == 0x1000
901 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
902 #elif PAGE_SIZE == 0x10000
903 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
904 * signal 1 and 2 area
906 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
907 #else
908 #error unsupported page size
909 #endif
912 static struct vm_operations_struct spufs_signal1_mmap_vmops = {
913 .nopfn = spufs_signal1_mmap_nopfn,
916 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
918 if (!(vma->vm_flags & VM_SHARED))
919 return -EINVAL;
921 vma->vm_flags |= VM_IO | VM_PFNMAP;
922 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
923 | _PAGE_NO_CACHE | _PAGE_GUARDED);
925 vma->vm_ops = &spufs_signal1_mmap_vmops;
926 return 0;
929 static const struct file_operations spufs_signal1_fops = {
930 .open = spufs_signal1_open,
931 .release = spufs_signal1_release,
932 .read = spufs_signal1_read,
933 .write = spufs_signal1_write,
934 .mmap = spufs_signal1_mmap,
937 static const struct file_operations spufs_signal1_nosched_fops = {
938 .open = spufs_signal1_open,
939 .release = spufs_signal1_release,
940 .write = spufs_signal1_write,
941 .mmap = spufs_signal1_mmap,
944 static int spufs_signal2_open(struct inode *inode, struct file *file)
946 struct spufs_inode_info *i = SPUFS_I(inode);
947 struct spu_context *ctx = i->i_ctx;
949 mutex_lock(&ctx->mapping_lock);
950 file->private_data = ctx;
951 if (!i->i_openers++)
952 ctx->signal2 = inode->i_mapping;
953 mutex_unlock(&ctx->mapping_lock);
954 return nonseekable_open(inode, file);
957 static int
958 spufs_signal2_release(struct inode *inode, struct file *file)
960 struct spufs_inode_info *i = SPUFS_I(inode);
961 struct spu_context *ctx = i->i_ctx;
963 mutex_lock(&ctx->mapping_lock);
964 if (!--i->i_openers)
965 ctx->signal2 = NULL;
966 mutex_unlock(&ctx->mapping_lock);
967 return 0;
970 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
971 size_t len, loff_t *pos)
973 int ret = 0;
974 u32 data;
976 if (len < 4)
977 return -EINVAL;
979 if (ctx->csa.spu_chnlcnt_RW[4]) {
980 data = ctx->csa.spu_chnldata_RW[4];
981 ret = 4;
984 if (!ret)
985 goto out;
987 if (copy_to_user(buf, &data, 4))
988 return -EFAULT;
990 out:
991 return ret;
994 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
995 size_t len, loff_t *pos)
997 struct spu_context *ctx = file->private_data;
998 int ret;
1000 spu_acquire_saved(ctx);
1001 ret = __spufs_signal2_read(ctx, buf, len, pos);
1002 spu_release_saved(ctx);
1004 return ret;
1007 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1008 size_t len, loff_t *pos)
1010 struct spu_context *ctx;
1011 u32 data;
1013 ctx = file->private_data;
1015 if (len < 4)
1016 return -EINVAL;
1018 if (copy_from_user(&data, buf, 4))
1019 return -EFAULT;
1021 spu_acquire(ctx);
1022 ctx->ops->signal2_write(ctx, data);
1023 spu_release(ctx);
1025 return 4;
1028 #if SPUFS_MMAP_4K
1029 static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1030 unsigned long address)
1032 #if PAGE_SIZE == 0x1000
1033 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
1034 #elif PAGE_SIZE == 0x10000
1035 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1036 * signal 1 and 2 area
1038 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
1039 #else
1040 #error unsupported page size
1041 #endif
1044 static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1045 .nopfn = spufs_signal2_mmap_nopfn,
1048 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1050 if (!(vma->vm_flags & VM_SHARED))
1051 return -EINVAL;
1053 vma->vm_flags |= VM_IO | VM_PFNMAP;
1054 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1055 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1057 vma->vm_ops = &spufs_signal2_mmap_vmops;
1058 return 0;
1060 #else /* SPUFS_MMAP_4K */
1061 #define spufs_signal2_mmap NULL
1062 #endif /* !SPUFS_MMAP_4K */
1064 static const struct file_operations spufs_signal2_fops = {
1065 .open = spufs_signal2_open,
1066 .release = spufs_signal2_release,
1067 .read = spufs_signal2_read,
1068 .write = spufs_signal2_write,
1069 .mmap = spufs_signal2_mmap,
1072 static const struct file_operations spufs_signal2_nosched_fops = {
1073 .open = spufs_signal2_open,
1074 .release = spufs_signal2_release,
1075 .write = spufs_signal2_write,
1076 .mmap = spufs_signal2_mmap,
1079 static void spufs_signal1_type_set(void *data, u64 val)
1081 struct spu_context *ctx = data;
1083 spu_acquire(ctx);
1084 ctx->ops->signal1_type_set(ctx, val);
1085 spu_release(ctx);
1088 static u64 __spufs_signal1_type_get(struct spu_context *ctx)
1090 return ctx->ops->signal1_type_get(ctx);
1093 static u64 spufs_signal1_type_get(void *data)
1095 struct spu_context *ctx = data;
1096 u64 ret;
1098 spu_acquire(ctx);
1099 ret = __spufs_signal1_type_get(ctx);
1100 spu_release(ctx);
1102 return ret;
1104 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1105 spufs_signal1_type_set, "%llu");
1107 static void spufs_signal2_type_set(void *data, u64 val)
1109 struct spu_context *ctx = data;
1111 spu_acquire(ctx);
1112 ctx->ops->signal2_type_set(ctx, val);
1113 spu_release(ctx);
1116 static u64 __spufs_signal2_type_get(struct spu_context *ctx)
1118 return ctx->ops->signal2_type_get(ctx);
1121 static u64 spufs_signal2_type_get(void *data)
1123 struct spu_context *ctx = data;
1124 u64 ret;
1126 spu_acquire(ctx);
1127 ret = __spufs_signal2_type_get(ctx);
1128 spu_release(ctx);
1130 return ret;
1132 DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1133 spufs_signal2_type_set, "%llu");
1135 #if SPUFS_MMAP_4K
1136 static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1137 unsigned long address)
1139 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
1142 static struct vm_operations_struct spufs_mss_mmap_vmops = {
1143 .nopfn = spufs_mss_mmap_nopfn,
1147 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1149 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1151 if (!(vma->vm_flags & VM_SHARED))
1152 return -EINVAL;
1154 vma->vm_flags |= VM_IO | VM_PFNMAP;
1155 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1156 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1158 vma->vm_ops = &spufs_mss_mmap_vmops;
1159 return 0;
1161 #else /* SPUFS_MMAP_4K */
1162 #define spufs_mss_mmap NULL
1163 #endif /* !SPUFS_MMAP_4K */
1165 static int spufs_mss_open(struct inode *inode, struct file *file)
1167 struct spufs_inode_info *i = SPUFS_I(inode);
1168 struct spu_context *ctx = i->i_ctx;
1170 file->private_data = i->i_ctx;
1172 mutex_lock(&ctx->mapping_lock);
1173 if (!i->i_openers++)
1174 ctx->mss = inode->i_mapping;
1175 mutex_unlock(&ctx->mapping_lock);
1176 return nonseekable_open(inode, file);
1179 static int
1180 spufs_mss_release(struct inode *inode, struct file *file)
1182 struct spufs_inode_info *i = SPUFS_I(inode);
1183 struct spu_context *ctx = i->i_ctx;
1185 mutex_lock(&ctx->mapping_lock);
1186 if (!--i->i_openers)
1187 ctx->mss = NULL;
1188 mutex_unlock(&ctx->mapping_lock);
1189 return 0;
1192 static const struct file_operations spufs_mss_fops = {
1193 .open = spufs_mss_open,
1194 .release = spufs_mss_release,
1195 .mmap = spufs_mss_mmap,
1198 static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1199 unsigned long address)
1201 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
1204 static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1205 .nopfn = spufs_psmap_mmap_nopfn,
1209 * mmap support for full problem state area [0x00000 - 0x1ffff].
1211 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1213 if (!(vma->vm_flags & VM_SHARED))
1214 return -EINVAL;
1216 vma->vm_flags |= VM_IO | VM_PFNMAP;
1217 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1218 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1220 vma->vm_ops = &spufs_psmap_mmap_vmops;
1221 return 0;
1224 static int spufs_psmap_open(struct inode *inode, struct file *file)
1226 struct spufs_inode_info *i = SPUFS_I(inode);
1227 struct spu_context *ctx = i->i_ctx;
1229 mutex_lock(&ctx->mapping_lock);
1230 file->private_data = i->i_ctx;
1231 if (!i->i_openers++)
1232 ctx->psmap = inode->i_mapping;
1233 mutex_unlock(&ctx->mapping_lock);
1234 return nonseekable_open(inode, file);
1237 static int
1238 spufs_psmap_release(struct inode *inode, struct file *file)
1240 struct spufs_inode_info *i = SPUFS_I(inode);
1241 struct spu_context *ctx = i->i_ctx;
1243 mutex_lock(&ctx->mapping_lock);
1244 if (!--i->i_openers)
1245 ctx->psmap = NULL;
1246 mutex_unlock(&ctx->mapping_lock);
1247 return 0;
1250 static const struct file_operations spufs_psmap_fops = {
1251 .open = spufs_psmap_open,
1252 .release = spufs_psmap_release,
1253 .mmap = spufs_psmap_mmap,
1257 #if SPUFS_MMAP_4K
1258 static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1259 unsigned long address)
1261 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
1264 static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1265 .nopfn = spufs_mfc_mmap_nopfn,
1269 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1271 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1273 if (!(vma->vm_flags & VM_SHARED))
1274 return -EINVAL;
1276 vma->vm_flags |= VM_IO | VM_PFNMAP;
1277 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1278 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1280 vma->vm_ops = &spufs_mfc_mmap_vmops;
1281 return 0;
1283 #else /* SPUFS_MMAP_4K */
1284 #define spufs_mfc_mmap NULL
1285 #endif /* !SPUFS_MMAP_4K */
1287 static int spufs_mfc_open(struct inode *inode, struct file *file)
1289 struct spufs_inode_info *i = SPUFS_I(inode);
1290 struct spu_context *ctx = i->i_ctx;
1292 /* we don't want to deal with DMA into other processes */
1293 if (ctx->owner != current->mm)
1294 return -EINVAL;
1296 if (atomic_read(&inode->i_count) != 1)
1297 return -EBUSY;
1299 mutex_lock(&ctx->mapping_lock);
1300 file->private_data = ctx;
1301 if (!i->i_openers++)
1302 ctx->mfc = inode->i_mapping;
1303 mutex_unlock(&ctx->mapping_lock);
1304 return nonseekable_open(inode, file);
1307 static int
1308 spufs_mfc_release(struct inode *inode, struct file *file)
1310 struct spufs_inode_info *i = SPUFS_I(inode);
1311 struct spu_context *ctx = i->i_ctx;
1313 mutex_lock(&ctx->mapping_lock);
1314 if (!--i->i_openers)
1315 ctx->mfc = NULL;
1316 mutex_unlock(&ctx->mapping_lock);
1317 return 0;
1320 /* interrupt-level mfc callback function. */
1321 void spufs_mfc_callback(struct spu *spu)
1323 struct spu_context *ctx = spu->ctx;
1325 wake_up_all(&ctx->mfc_wq);
1327 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1328 if (ctx->mfc_fasync) {
1329 u32 free_elements, tagstatus;
1330 unsigned int mask;
1332 /* no need for spu_acquire in interrupt context */
1333 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1334 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1336 mask = 0;
1337 if (free_elements & 0xffff)
1338 mask |= POLLOUT;
1339 if (tagstatus & ctx->tagwait)
1340 mask |= POLLIN;
1342 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1346 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1348 /* See if there is one tag group is complete */
1349 /* FIXME we need locking around tagwait */
1350 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1351 ctx->tagwait &= ~*status;
1352 if (*status)
1353 return 1;
1355 /* enable interrupt waiting for any tag group,
1356 may silently fail if interrupts are already enabled */
1357 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1358 return 0;
1361 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1362 size_t size, loff_t *pos)
1364 struct spu_context *ctx = file->private_data;
1365 int ret = -EINVAL;
1366 u32 status;
1368 if (size != 4)
1369 goto out;
1371 spu_acquire(ctx);
1372 if (file->f_flags & O_NONBLOCK) {
1373 status = ctx->ops->read_mfc_tagstatus(ctx);
1374 if (!(status & ctx->tagwait))
1375 ret = -EAGAIN;
1376 else
1377 ctx->tagwait &= ~status;
1378 } else {
1379 ret = spufs_wait(ctx->mfc_wq,
1380 spufs_read_mfc_tagstatus(ctx, &status));
1382 spu_release(ctx);
1384 if (ret)
1385 goto out;
1387 ret = 4;
1388 if (copy_to_user(buffer, &status, 4))
1389 ret = -EFAULT;
1391 out:
1392 return ret;
1395 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1397 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1398 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1400 switch (cmd->cmd) {
1401 case MFC_PUT_CMD:
1402 case MFC_PUTF_CMD:
1403 case MFC_PUTB_CMD:
1404 case MFC_GET_CMD:
1405 case MFC_GETF_CMD:
1406 case MFC_GETB_CMD:
1407 break;
1408 default:
1409 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1410 return -EIO;
1413 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1414 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1415 cmd->ea, cmd->lsa);
1416 return -EIO;
1419 switch (cmd->size & 0xf) {
1420 case 1:
1421 break;
1422 case 2:
1423 if (cmd->lsa & 1)
1424 goto error;
1425 break;
1426 case 4:
1427 if (cmd->lsa & 3)
1428 goto error;
1429 break;
1430 case 8:
1431 if (cmd->lsa & 7)
1432 goto error;
1433 break;
1434 case 0:
1435 if (cmd->lsa & 15)
1436 goto error;
1437 break;
1438 error:
1439 default:
1440 pr_debug("invalid DMA alignment %x for size %x\n",
1441 cmd->lsa & 0xf, cmd->size);
1442 return -EIO;
1445 if (cmd->size > 16 * 1024) {
1446 pr_debug("invalid DMA size %x\n", cmd->size);
1447 return -EIO;
1450 if (cmd->tag & 0xfff0) {
1451 /* we reserve the higher tag numbers for kernel use */
1452 pr_debug("invalid DMA tag\n");
1453 return -EIO;
1456 if (cmd->class) {
1457 /* not supported in this version */
1458 pr_debug("invalid DMA class\n");
1459 return -EIO;
1462 return 0;
1465 static int spu_send_mfc_command(struct spu_context *ctx,
1466 struct mfc_dma_command cmd,
1467 int *error)
1469 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1470 if (*error == -EAGAIN) {
1471 /* wait for any tag group to complete
1472 so we have space for the new command */
1473 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1474 /* try again, because the queue might be
1475 empty again */
1476 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1477 if (*error == -EAGAIN)
1478 return 0;
1480 return 1;
1483 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1484 size_t size, loff_t *pos)
1486 struct spu_context *ctx = file->private_data;
1487 struct mfc_dma_command cmd;
1488 int ret = -EINVAL;
1490 if (size != sizeof cmd)
1491 goto out;
1493 ret = -EFAULT;
1494 if (copy_from_user(&cmd, buffer, sizeof cmd))
1495 goto out;
1497 ret = spufs_check_valid_dma(&cmd);
1498 if (ret)
1499 goto out;
1501 ret = spu_acquire_runnable(ctx, 0);
1502 if (ret)
1503 goto out;
1505 if (file->f_flags & O_NONBLOCK) {
1506 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1507 } else {
1508 int status;
1509 ret = spufs_wait(ctx->mfc_wq,
1510 spu_send_mfc_command(ctx, cmd, &status));
1511 if (status)
1512 ret = status;
1515 if (ret)
1516 goto out_unlock;
1518 ctx->tagwait |= 1 << cmd.tag;
1519 ret = size;
1521 out_unlock:
1522 spu_release(ctx);
1523 out:
1524 return ret;
1527 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1529 struct spu_context *ctx = file->private_data;
1530 u32 free_elements, tagstatus;
1531 unsigned int mask;
1533 poll_wait(file, &ctx->mfc_wq, wait);
1535 spu_acquire(ctx);
1536 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1537 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1538 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1539 spu_release(ctx);
1541 mask = 0;
1542 if (free_elements & 0xffff)
1543 mask |= POLLOUT | POLLWRNORM;
1544 if (tagstatus & ctx->tagwait)
1545 mask |= POLLIN | POLLRDNORM;
1547 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1548 free_elements, tagstatus, ctx->tagwait);
1550 return mask;
1553 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1555 struct spu_context *ctx = file->private_data;
1556 int ret;
1558 spu_acquire(ctx);
1559 #if 0
1560 /* this currently hangs */
1561 ret = spufs_wait(ctx->mfc_wq,
1562 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1563 if (ret)
1564 goto out;
1565 ret = spufs_wait(ctx->mfc_wq,
1566 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1567 out:
1568 #else
1569 ret = 0;
1570 #endif
1571 spu_release(ctx);
1573 return ret;
1576 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1577 int datasync)
1579 return spufs_mfc_flush(file, NULL);
1582 static int spufs_mfc_fasync(int fd, struct file *file, int on)
1584 struct spu_context *ctx = file->private_data;
1586 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1589 static const struct file_operations spufs_mfc_fops = {
1590 .open = spufs_mfc_open,
1591 .release = spufs_mfc_release,
1592 .read = spufs_mfc_read,
1593 .write = spufs_mfc_write,
1594 .poll = spufs_mfc_poll,
1595 .flush = spufs_mfc_flush,
1596 .fsync = spufs_mfc_fsync,
1597 .fasync = spufs_mfc_fasync,
1598 .mmap = spufs_mfc_mmap,
1601 static void spufs_npc_set(void *data, u64 val)
1603 struct spu_context *ctx = data;
1604 spu_acquire(ctx);
1605 ctx->ops->npc_write(ctx, val);
1606 spu_release(ctx);
1609 static u64 spufs_npc_get(void *data)
1611 struct spu_context *ctx = data;
1612 u64 ret;
1613 spu_acquire(ctx);
1614 ret = ctx->ops->npc_read(ctx);
1615 spu_release(ctx);
1616 return ret;
1618 DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1619 "0x%llx\n")
1621 static void spufs_decr_set(void *data, u64 val)
1623 struct spu_context *ctx = data;
1624 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1625 spu_acquire_saved(ctx);
1626 lscsa->decr.slot[0] = (u32) val;
1627 spu_release_saved(ctx);
1630 static u64 __spufs_decr_get(struct spu_context *ctx)
1632 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1633 return lscsa->decr.slot[0];
1636 static u64 spufs_decr_get(void *data)
1638 struct spu_context *ctx = data;
1639 u64 ret;
1640 spu_acquire_saved(ctx);
1641 ret = __spufs_decr_get(ctx);
1642 spu_release_saved(ctx);
1643 return ret;
1645 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1646 "0x%llx\n")
1648 static void spufs_decr_status_set(void *data, u64 val)
1650 struct spu_context *ctx = data;
1651 spu_acquire_saved(ctx);
1652 if (val)
1653 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1654 else
1655 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1656 spu_release_saved(ctx);
1659 static u64 __spufs_decr_status_get(struct spu_context *ctx)
1661 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1662 return SPU_DECR_STATUS_RUNNING;
1663 else
1664 return 0;
1667 static u64 spufs_decr_status_get(void *data)
1669 struct spu_context *ctx = data;
1670 u64 ret;
1671 spu_acquire_saved(ctx);
1672 ret = __spufs_decr_status_get(ctx);
1673 spu_release_saved(ctx);
1674 return ret;
1676 DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1677 spufs_decr_status_set, "0x%llx\n")
1679 static void spufs_event_mask_set(void *data, u64 val)
1681 struct spu_context *ctx = data;
1682 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1683 spu_acquire_saved(ctx);
1684 lscsa->event_mask.slot[0] = (u32) val;
1685 spu_release_saved(ctx);
1688 static u64 __spufs_event_mask_get(struct spu_context *ctx)
1690 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1691 return lscsa->event_mask.slot[0];
1694 static u64 spufs_event_mask_get(void *data)
1696 struct spu_context *ctx = data;
1697 u64 ret;
1698 spu_acquire_saved(ctx);
1699 ret = __spufs_event_mask_get(ctx);
1700 spu_release_saved(ctx);
1701 return ret;
1703 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1704 spufs_event_mask_set, "0x%llx\n")
1706 static u64 __spufs_event_status_get(struct spu_context *ctx)
1708 struct spu_state *state = &ctx->csa;
1709 u64 stat;
1710 stat = state->spu_chnlcnt_RW[0];
1711 if (stat)
1712 return state->spu_chnldata_RW[0];
1713 return 0;
1716 static u64 spufs_event_status_get(void *data)
1718 struct spu_context *ctx = data;
1719 u64 ret = 0;
1721 spu_acquire_saved(ctx);
1722 ret = __spufs_event_status_get(ctx);
1723 spu_release_saved(ctx);
1724 return ret;
1726 DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1727 NULL, "0x%llx\n")
1729 static void spufs_srr0_set(void *data, u64 val)
1731 struct spu_context *ctx = data;
1732 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1733 spu_acquire_saved(ctx);
1734 lscsa->srr0.slot[0] = (u32) val;
1735 spu_release_saved(ctx);
1738 static u64 spufs_srr0_get(void *data)
1740 struct spu_context *ctx = data;
1741 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1742 u64 ret;
1743 spu_acquire_saved(ctx);
1744 ret = lscsa->srr0.slot[0];
1745 spu_release_saved(ctx);
1746 return ret;
1748 DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1749 "0x%llx\n")
1751 static u64 spufs_id_get(void *data)
1753 struct spu_context *ctx = data;
1754 u64 num;
1756 spu_acquire(ctx);
1757 if (ctx->state == SPU_STATE_RUNNABLE)
1758 num = ctx->spu->number;
1759 else
1760 num = (unsigned int)-1;
1761 spu_release(ctx);
1763 return num;
1765 DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
1767 static u64 __spufs_object_id_get(struct spu_context *ctx)
1769 return ctx->object_id;
1772 static u64 spufs_object_id_get(void *data)
1774 /* FIXME: Should there really be no locking here? */
1775 return __spufs_object_id_get((struct spu_context *)data);
1778 static void spufs_object_id_set(void *data, u64 id)
1780 struct spu_context *ctx = data;
1781 ctx->object_id = id;
1784 DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1785 spufs_object_id_set, "0x%llx\n");
1787 static u64 __spufs_lslr_get(struct spu_context *ctx)
1789 return ctx->csa.priv2.spu_lslr_RW;
1792 static u64 spufs_lslr_get(void *data)
1794 struct spu_context *ctx = data;
1795 u64 ret;
1797 spu_acquire_saved(ctx);
1798 ret = __spufs_lslr_get(ctx);
1799 spu_release_saved(ctx);
1801 return ret;
1803 DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
1805 static int spufs_info_open(struct inode *inode, struct file *file)
1807 struct spufs_inode_info *i = SPUFS_I(inode);
1808 struct spu_context *ctx = i->i_ctx;
1809 file->private_data = ctx;
1810 return 0;
1813 static int spufs_caps_show(struct seq_file *s, void *private)
1815 struct spu_context *ctx = s->private;
1817 if (!(ctx->flags & SPU_CREATE_NOSCHED))
1818 seq_puts(s, "sched\n");
1819 if (!(ctx->flags & SPU_CREATE_ISOLATE))
1820 seq_puts(s, "step\n");
1821 return 0;
1824 static int spufs_caps_open(struct inode *inode, struct file *file)
1826 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1829 static const struct file_operations spufs_caps_fops = {
1830 .open = spufs_caps_open,
1831 .read = seq_read,
1832 .llseek = seq_lseek,
1833 .release = single_release,
1836 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1837 char __user *buf, size_t len, loff_t *pos)
1839 u32 mbox_stat;
1840 u32 data;
1842 mbox_stat = ctx->csa.prob.mb_stat_R;
1843 if (mbox_stat & 0x0000ff) {
1844 data = ctx->csa.prob.pu_mb_R;
1847 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1850 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1851 size_t len, loff_t *pos)
1853 int ret;
1854 struct spu_context *ctx = file->private_data;
1856 if (!access_ok(VERIFY_WRITE, buf, len))
1857 return -EFAULT;
1859 spu_acquire_saved(ctx);
1860 spin_lock(&ctx->csa.register_lock);
1861 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
1862 spin_unlock(&ctx->csa.register_lock);
1863 spu_release_saved(ctx);
1865 return ret;
1868 static const struct file_operations spufs_mbox_info_fops = {
1869 .open = spufs_info_open,
1870 .read = spufs_mbox_info_read,
1871 .llseek = generic_file_llseek,
1874 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
1875 char __user *buf, size_t len, loff_t *pos)
1877 u32 ibox_stat;
1878 u32 data;
1880 ibox_stat = ctx->csa.prob.mb_stat_R;
1881 if (ibox_stat & 0xff0000) {
1882 data = ctx->csa.priv2.puint_mb_R;
1885 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1888 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
1889 size_t len, loff_t *pos)
1891 struct spu_context *ctx = file->private_data;
1892 int ret;
1894 if (!access_ok(VERIFY_WRITE, buf, len))
1895 return -EFAULT;
1897 spu_acquire_saved(ctx);
1898 spin_lock(&ctx->csa.register_lock);
1899 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
1900 spin_unlock(&ctx->csa.register_lock);
1901 spu_release_saved(ctx);
1903 return ret;
1906 static const struct file_operations spufs_ibox_info_fops = {
1907 .open = spufs_info_open,
1908 .read = spufs_ibox_info_read,
1909 .llseek = generic_file_llseek,
1912 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
1913 char __user *buf, size_t len, loff_t *pos)
1915 int i, cnt;
1916 u32 data[4];
1917 u32 wbox_stat;
1919 wbox_stat = ctx->csa.prob.mb_stat_R;
1920 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
1921 for (i = 0; i < cnt; i++) {
1922 data[i] = ctx->csa.spu_mailbox_data[i];
1925 return simple_read_from_buffer(buf, len, pos, &data,
1926 cnt * sizeof(u32));
1929 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
1930 size_t len, loff_t *pos)
1932 struct spu_context *ctx = file->private_data;
1933 int ret;
1935 if (!access_ok(VERIFY_WRITE, buf, len))
1936 return -EFAULT;
1938 spu_acquire_saved(ctx);
1939 spin_lock(&ctx->csa.register_lock);
1940 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
1941 spin_unlock(&ctx->csa.register_lock);
1942 spu_release_saved(ctx);
1944 return ret;
1947 static const struct file_operations spufs_wbox_info_fops = {
1948 .open = spufs_info_open,
1949 .read = spufs_wbox_info_read,
1950 .llseek = generic_file_llseek,
1953 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
1954 char __user *buf, size_t len, loff_t *pos)
1956 struct spu_dma_info info;
1957 struct mfc_cq_sr *qp, *spuqp;
1958 int i;
1960 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
1961 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
1962 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
1963 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
1964 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
1965 for (i = 0; i < 16; i++) {
1966 qp = &info.dma_info_command_data[i];
1967 spuqp = &ctx->csa.priv2.spuq[i];
1969 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
1970 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
1971 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
1972 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
1975 return simple_read_from_buffer(buf, len, pos, &info,
1976 sizeof info);
1979 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
1980 size_t len, loff_t *pos)
1982 struct spu_context *ctx = file->private_data;
1983 int ret;
1985 if (!access_ok(VERIFY_WRITE, buf, len))
1986 return -EFAULT;
1988 spu_acquire_saved(ctx);
1989 spin_lock(&ctx->csa.register_lock);
1990 ret = __spufs_dma_info_read(ctx, buf, len, pos);
1991 spin_unlock(&ctx->csa.register_lock);
1992 spu_release_saved(ctx);
1994 return ret;
1997 static const struct file_operations spufs_dma_info_fops = {
1998 .open = spufs_info_open,
1999 .read = spufs_dma_info_read,
2002 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2003 char __user *buf, size_t len, loff_t *pos)
2005 struct spu_proxydma_info info;
2006 struct mfc_cq_sr *qp, *puqp;
2007 int ret = sizeof info;
2008 int i;
2010 if (len < ret)
2011 return -EINVAL;
2013 if (!access_ok(VERIFY_WRITE, buf, len))
2014 return -EFAULT;
2016 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2017 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2018 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2019 for (i = 0; i < 8; i++) {
2020 qp = &info.proxydma_info_command_data[i];
2021 puqp = &ctx->csa.priv2.puq[i];
2023 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2024 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2025 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2026 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2029 return simple_read_from_buffer(buf, len, pos, &info,
2030 sizeof info);
2033 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2034 size_t len, loff_t *pos)
2036 struct spu_context *ctx = file->private_data;
2037 int ret;
2039 spu_acquire_saved(ctx);
2040 spin_lock(&ctx->csa.register_lock);
2041 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2042 spin_unlock(&ctx->csa.register_lock);
2043 spu_release_saved(ctx);
2045 return ret;
2048 static const struct file_operations spufs_proxydma_info_fops = {
2049 .open = spufs_info_open,
2050 .read = spufs_proxydma_info_read,
2053 static int spufs_show_tid(struct seq_file *s, void *private)
2055 struct spu_context *ctx = s->private;
2057 seq_printf(s, "%d\n", ctx->tid);
2058 return 0;
2061 static int spufs_tid_open(struct inode *inode, struct file *file)
2063 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2066 static const struct file_operations spufs_tid_fops = {
2067 .open = spufs_tid_open,
2068 .read = seq_read,
2069 .llseek = seq_lseek,
2070 .release = single_release,
2073 static const char *ctx_state_names[] = {
2074 "user", "system", "iowait", "loaded"
2077 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2078 enum spu_utilization_state state)
2080 struct timespec ts;
2081 unsigned long long time = ctx->stats.times[state];
2084 * In general, utilization statistics are updated by the controlling
2085 * thread as the spu context moves through various well defined
2086 * state transitions, but if the context is lazily loaded its
2087 * utilization statistics are not updated as the controlling thread
2088 * is not tightly coupled with the execution of the spu context. We
2089 * calculate and apply the time delta from the last recorded state
2090 * of the spu context.
2092 if (ctx->spu && ctx->stats.util_state == state) {
2093 ktime_get_ts(&ts);
2094 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2097 return time / NSEC_PER_MSEC;
2100 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2102 unsigned long long slb_flts = ctx->stats.slb_flt;
2104 if (ctx->state == SPU_STATE_RUNNABLE) {
2105 slb_flts += (ctx->spu->stats.slb_flt -
2106 ctx->stats.slb_flt_base);
2109 return slb_flts;
2112 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2114 unsigned long long class2_intrs = ctx->stats.class2_intr;
2116 if (ctx->state == SPU_STATE_RUNNABLE) {
2117 class2_intrs += (ctx->spu->stats.class2_intr -
2118 ctx->stats.class2_intr_base);
2121 return class2_intrs;
2125 static int spufs_show_stat(struct seq_file *s, void *private)
2127 struct spu_context *ctx = s->private;
2129 spu_acquire(ctx);
2130 seq_printf(s, "%s %llu %llu %llu %llu "
2131 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2132 ctx_state_names[ctx->stats.util_state],
2133 spufs_acct_time(ctx, SPU_UTIL_USER),
2134 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2135 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2136 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2137 ctx->stats.vol_ctx_switch,
2138 ctx->stats.invol_ctx_switch,
2139 spufs_slb_flts(ctx),
2140 ctx->stats.hash_flt,
2141 ctx->stats.min_flt,
2142 ctx->stats.maj_flt,
2143 spufs_class2_intrs(ctx),
2144 ctx->stats.libassist);
2145 spu_release(ctx);
2146 return 0;
2149 static int spufs_stat_open(struct inode *inode, struct file *file)
2151 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2154 static const struct file_operations spufs_stat_fops = {
2155 .open = spufs_stat_open,
2156 .read = seq_read,
2157 .llseek = seq_lseek,
2158 .release = single_release,
2162 struct tree_descr spufs_dir_contents[] = {
2163 { "capabilities", &spufs_caps_fops, 0444, },
2164 { "mem", &spufs_mem_fops, 0666, },
2165 { "regs", &spufs_regs_fops, 0666, },
2166 { "mbox", &spufs_mbox_fops, 0444, },
2167 { "ibox", &spufs_ibox_fops, 0444, },
2168 { "wbox", &spufs_wbox_fops, 0222, },
2169 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2170 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2171 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2172 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2173 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2174 { "signal1_type", &spufs_signal1_type, 0666, },
2175 { "signal2_type", &spufs_signal2_type, 0666, },
2176 { "cntl", &spufs_cntl_fops, 0666, },
2177 { "fpcr", &spufs_fpcr_fops, 0666, },
2178 { "lslr", &spufs_lslr_ops, 0444, },
2179 { "mfc", &spufs_mfc_fops, 0666, },
2180 { "mss", &spufs_mss_fops, 0666, },
2181 { "npc", &spufs_npc_ops, 0666, },
2182 { "srr0", &spufs_srr0_ops, 0666, },
2183 { "decr", &spufs_decr_ops, 0666, },
2184 { "decr_status", &spufs_decr_status_ops, 0666, },
2185 { "event_mask", &spufs_event_mask_ops, 0666, },
2186 { "event_status", &spufs_event_status_ops, 0444, },
2187 { "psmap", &spufs_psmap_fops, 0666, },
2188 { "phys-id", &spufs_id_ops, 0666, },
2189 { "object-id", &spufs_object_id_ops, 0666, },
2190 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2191 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2192 { "wbox_info", &spufs_wbox_info_fops, 0444, },
2193 { "dma_info", &spufs_dma_info_fops, 0444, },
2194 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
2195 { "tid", &spufs_tid_fops, 0444, },
2196 { "stat", &spufs_stat_fops, 0444, },
2200 struct tree_descr spufs_dir_nosched_contents[] = {
2201 { "capabilities", &spufs_caps_fops, 0444, },
2202 { "mem", &spufs_mem_fops, 0666, },
2203 { "mbox", &spufs_mbox_fops, 0444, },
2204 { "ibox", &spufs_ibox_fops, 0444, },
2205 { "wbox", &spufs_wbox_fops, 0222, },
2206 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2207 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2208 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2209 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2210 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2211 { "signal1_type", &spufs_signal1_type, 0666, },
2212 { "signal2_type", &spufs_signal2_type, 0666, },
2213 { "mss", &spufs_mss_fops, 0666, },
2214 { "mfc", &spufs_mfc_fops, 0666, },
2215 { "cntl", &spufs_cntl_fops, 0666, },
2216 { "npc", &spufs_npc_ops, 0666, },
2217 { "psmap", &spufs_psmap_fops, 0666, },
2218 { "phys-id", &spufs_id_ops, 0666, },
2219 { "object-id", &spufs_object_id_ops, 0666, },
2220 { "tid", &spufs_tid_fops, 0444, },
2221 { "stat", &spufs_stat_fops, 0444, },
2225 struct spufs_coredump_reader spufs_coredump_read[] = {
2226 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2227 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2228 { "lslr", NULL, __spufs_lslr_get, 19 },
2229 { "decr", NULL, __spufs_decr_get, 19 },
2230 { "decr_status", NULL, __spufs_decr_status_get, 19 },
2231 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2232 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2233 { "signal1_type", NULL, __spufs_signal1_type_get, 19 },
2234 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2235 { "signal2_type", NULL, __spufs_signal2_type_get, 19 },
2236 { "event_mask", NULL, __spufs_event_mask_get, 19 },
2237 { "event_status", NULL, __spufs_event_status_get, 19 },
2238 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2239 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2240 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2241 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2242 { "proxydma_info", __spufs_proxydma_info_read,
2243 NULL, sizeof(struct spu_proxydma_info)},
2244 { "object-id", NULL, __spufs_object_id_get, 19 },
2245 { NULL },