1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SPU file system -- file contents
5 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
7 * Author: Arnd Bergmann <arndb@de.ibm.com>
13 #include <linux/ioctl.h>
14 #include <linux/export.h>
15 #include <linux/pagemap.h>
16 #include <linux/poll.h>
17 #include <linux/ptrace.h>
18 #include <linux/seq_file.h>
19 #include <linux/slab.h>
24 #include <asm/spu_info.h>
25 #include <linux/uaccess.h>
30 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
32 /* Simple attribute files */
34 int (*get)(void *, u64 *);
35 int (*set)(void *, u64);
36 char get_buf[24]; /* enough to store a u64 and "\n\0" */
39 const char *fmt; /* format for read operation */
40 struct mutex mutex; /* protects access to these buffers */
43 static int spufs_attr_open(struct inode *inode, struct file *file,
44 int (*get)(void *, u64 *), int (*set)(void *, u64),
47 struct spufs_attr *attr;
49 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
55 attr->data = inode->i_private;
57 mutex_init(&attr->mutex);
58 file->private_data = attr;
60 return nonseekable_open(inode, file);
63 static int spufs_attr_release(struct inode *inode, struct file *file)
65 kfree(file->private_data);
69 static ssize_t spufs_attr_read(struct file *file, char __user *buf,
70 size_t len, loff_t *ppos)
72 struct spufs_attr *attr;
76 attr = file->private_data;
80 ret = mutex_lock_interruptible(&attr->mutex);
84 if (*ppos) { /* continued read */
85 size = strlen(attr->get_buf);
86 } else { /* first read */
88 ret = attr->get(attr->data, &val);
92 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
93 attr->fmt, (unsigned long long)val);
96 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
98 mutex_unlock(&attr->mutex);
102 static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
103 size_t len, loff_t *ppos)
105 struct spufs_attr *attr;
110 attr = file->private_data;
114 ret = mutex_lock_interruptible(&attr->mutex);
119 size = min(sizeof(attr->set_buf) - 1, len);
120 if (copy_from_user(attr->set_buf, buf, size))
123 ret = len; /* claim we got the whole input */
124 attr->set_buf[size] = '\0';
125 val = simple_strtol(attr->set_buf, NULL, 0);
126 attr->set(attr->data, val);
128 mutex_unlock(&attr->mutex);
132 #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
133 static int __fops ## _open(struct inode *inode, struct file *file) \
135 __simple_attr_check_format(__fmt, 0ull); \
136 return spufs_attr_open(inode, file, __get, __set, __fmt); \
138 static const struct file_operations __fops = { \
139 .open = __fops ## _open, \
140 .release = spufs_attr_release, \
141 .read = spufs_attr_read, \
142 .write = spufs_attr_write, \
143 .llseek = generic_file_llseek, \
148 spufs_mem_open(struct inode *inode, struct file *file)
150 struct spufs_inode_info *i = SPUFS_I(inode);
151 struct spu_context *ctx = i->i_ctx;
153 mutex_lock(&ctx->mapping_lock);
154 file->private_data = ctx;
156 ctx->local_store = inode->i_mapping;
157 mutex_unlock(&ctx->mapping_lock);
162 spufs_mem_release(struct inode *inode, struct file *file)
164 struct spufs_inode_info *i = SPUFS_I(inode);
165 struct spu_context *ctx = i->i_ctx;
167 mutex_lock(&ctx->mapping_lock);
169 ctx->local_store = NULL;
170 mutex_unlock(&ctx->mapping_lock);
175 __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
176 size_t size, loff_t *pos)
178 char *local_store = ctx->ops->get_ls(ctx);
179 return simple_read_from_buffer(buffer, size, pos, local_store,
184 spufs_mem_read(struct file *file, char __user *buffer,
185 size_t size, loff_t *pos)
187 struct spu_context *ctx = file->private_data;
190 ret = spu_acquire(ctx);
193 ret = __spufs_mem_read(ctx, buffer, size, pos);
200 spufs_mem_write(struct file *file, const char __user *buffer,
201 size_t size, loff_t *ppos)
203 struct spu_context *ctx = file->private_data;
211 ret = spu_acquire(ctx);
215 local_store = ctx->ops->get_ls(ctx);
216 size = simple_write_to_buffer(local_store, LS_SIZE, ppos, buffer, size);
223 spufs_mem_mmap_fault(struct vm_fault *vmf)
225 struct vm_area_struct *vma = vmf->vma;
226 struct spu_context *ctx = vma->vm_file->private_data;
227 unsigned long pfn, offset;
230 offset = vmf->pgoff << PAGE_SHIFT;
231 if (offset >= LS_SIZE)
232 return VM_FAULT_SIGBUS;
234 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
235 vmf->address, offset);
237 if (spu_acquire(ctx))
238 return VM_FAULT_NOPAGE;
240 if (ctx->state == SPU_STATE_SAVED) {
241 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
242 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
244 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
245 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
247 ret = vmf_insert_pfn(vma, vmf->address, pfn);
254 static int spufs_mem_mmap_access(struct vm_area_struct *vma,
255 unsigned long address,
256 void *buf, int len, int write)
258 struct spu_context *ctx = vma->vm_file->private_data;
259 unsigned long offset = address - vma->vm_start;
262 if (write && !(vma->vm_flags & VM_WRITE))
264 if (spu_acquire(ctx))
266 if ((offset + len) > vma->vm_end)
267 len = vma->vm_end - offset;
268 local_store = ctx->ops->get_ls(ctx);
270 memcpy_toio(local_store + offset, buf, len);
272 memcpy_fromio(buf, local_store + offset, len);
277 static const struct vm_operations_struct spufs_mem_mmap_vmops = {
278 .fault = spufs_mem_mmap_fault,
279 .access = spufs_mem_mmap_access,
282 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
284 if (!(vma->vm_flags & VM_SHARED))
287 vma->vm_flags |= VM_IO | VM_PFNMAP;
288 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
290 vma->vm_ops = &spufs_mem_mmap_vmops;
294 static const struct file_operations spufs_mem_fops = {
295 .open = spufs_mem_open,
296 .release = spufs_mem_release,
297 .read = spufs_mem_read,
298 .write = spufs_mem_write,
299 .llseek = generic_file_llseek,
300 .mmap = spufs_mem_mmap,
303 static vm_fault_t spufs_ps_fault(struct vm_fault *vmf,
304 unsigned long ps_offs,
305 unsigned long ps_size)
307 struct spu_context *ctx = vmf->vma->vm_file->private_data;
308 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
310 vm_fault_t ret = VM_FAULT_NOPAGE;
312 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
314 if (offset >= ps_size)
315 return VM_FAULT_SIGBUS;
317 if (fatal_signal_pending(current))
318 return VM_FAULT_SIGBUS;
321 * Because we release the mmap_sem, the context may be destroyed while
322 * we're in spu_wait. Grab an extra reference so it isn't destroyed
325 get_spu_context(ctx);
328 * We have to wait for context to be loaded before we have
329 * pages to hand out to the user, but we don't want to wait
330 * with the mmap_sem held.
331 * It is possible to drop the mmap_sem here, but then we need
332 * to return VM_FAULT_NOPAGE because the mappings may have
335 if (spu_acquire(ctx))
338 if (ctx->state == SPU_STATE_SAVED) {
339 up_read(¤t->mm->mmap_sem);
340 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
341 err = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
342 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
343 down_read(¤t->mm->mmap_sem);
345 area = ctx->spu->problem_phys + ps_offs;
346 ret = vmf_insert_pfn(vmf->vma, vmf->address,
347 (area + offset) >> PAGE_SHIFT);
348 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
355 put_spu_context(ctx);
360 static vm_fault_t spufs_cntl_mmap_fault(struct vm_fault *vmf)
362 return spufs_ps_fault(vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
365 static const struct vm_operations_struct spufs_cntl_mmap_vmops = {
366 .fault = spufs_cntl_mmap_fault,
370 * mmap support for problem state control area [0x4000 - 0x4fff].
372 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
374 if (!(vma->vm_flags & VM_SHARED))
377 vma->vm_flags |= VM_IO | VM_PFNMAP;
378 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
380 vma->vm_ops = &spufs_cntl_mmap_vmops;
383 #else /* SPUFS_MMAP_4K */
384 #define spufs_cntl_mmap NULL
385 #endif /* !SPUFS_MMAP_4K */
387 static int spufs_cntl_get(void *data, u64 *val)
389 struct spu_context *ctx = data;
392 ret = spu_acquire(ctx);
395 *val = ctx->ops->status_read(ctx);
401 static int spufs_cntl_set(void *data, u64 val)
403 struct spu_context *ctx = data;
406 ret = spu_acquire(ctx);
409 ctx->ops->runcntl_write(ctx, val);
415 static int spufs_cntl_open(struct inode *inode, struct file *file)
417 struct spufs_inode_info *i = SPUFS_I(inode);
418 struct spu_context *ctx = i->i_ctx;
420 mutex_lock(&ctx->mapping_lock);
421 file->private_data = ctx;
423 ctx->cntl = inode->i_mapping;
424 mutex_unlock(&ctx->mapping_lock);
425 return simple_attr_open(inode, file, spufs_cntl_get,
426 spufs_cntl_set, "0x%08lx");
430 spufs_cntl_release(struct inode *inode, struct file *file)
432 struct spufs_inode_info *i = SPUFS_I(inode);
433 struct spu_context *ctx = i->i_ctx;
435 simple_attr_release(inode, file);
437 mutex_lock(&ctx->mapping_lock);
440 mutex_unlock(&ctx->mapping_lock);
444 static const struct file_operations spufs_cntl_fops = {
445 .open = spufs_cntl_open,
446 .release = spufs_cntl_release,
447 .read = simple_attr_read,
448 .write = simple_attr_write,
450 .mmap = spufs_cntl_mmap,
454 spufs_regs_open(struct inode *inode, struct file *file)
456 struct spufs_inode_info *i = SPUFS_I(inode);
457 file->private_data = i->i_ctx;
462 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
463 size_t size, loff_t *pos)
465 struct spu_lscsa *lscsa = ctx->csa.lscsa;
466 return simple_read_from_buffer(buffer, size, pos,
467 lscsa->gprs, sizeof lscsa->gprs);
471 spufs_regs_read(struct file *file, char __user *buffer,
472 size_t size, loff_t *pos)
475 struct spu_context *ctx = file->private_data;
477 /* pre-check for file position: if we'd return EOF, there's no point
478 * causing a deschedule */
479 if (*pos >= sizeof(ctx->csa.lscsa->gprs))
482 ret = spu_acquire_saved(ctx);
485 ret = __spufs_regs_read(ctx, buffer, size, pos);
486 spu_release_saved(ctx);
491 spufs_regs_write(struct file *file, const char __user *buffer,
492 size_t size, loff_t *pos)
494 struct spu_context *ctx = file->private_data;
495 struct spu_lscsa *lscsa = ctx->csa.lscsa;
498 if (*pos >= sizeof(lscsa->gprs))
501 ret = spu_acquire_saved(ctx);
505 size = simple_write_to_buffer(lscsa->gprs, sizeof(lscsa->gprs), pos,
508 spu_release_saved(ctx);
512 static const struct file_operations spufs_regs_fops = {
513 .open = spufs_regs_open,
514 .read = spufs_regs_read,
515 .write = spufs_regs_write,
516 .llseek = generic_file_llseek,
520 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
521 size_t size, loff_t * pos)
523 struct spu_lscsa *lscsa = ctx->csa.lscsa;
524 return simple_read_from_buffer(buffer, size, pos,
525 &lscsa->fpcr, sizeof(lscsa->fpcr));
529 spufs_fpcr_read(struct file *file, char __user * buffer,
530 size_t size, loff_t * pos)
533 struct spu_context *ctx = file->private_data;
535 ret = spu_acquire_saved(ctx);
538 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
539 spu_release_saved(ctx);
544 spufs_fpcr_write(struct file *file, const char __user * buffer,
545 size_t size, loff_t * pos)
547 struct spu_context *ctx = file->private_data;
548 struct spu_lscsa *lscsa = ctx->csa.lscsa;
551 if (*pos >= sizeof(lscsa->fpcr))
554 ret = spu_acquire_saved(ctx);
558 size = simple_write_to_buffer(&lscsa->fpcr, sizeof(lscsa->fpcr), pos,
561 spu_release_saved(ctx);
565 static const struct file_operations spufs_fpcr_fops = {
566 .open = spufs_regs_open,
567 .read = spufs_fpcr_read,
568 .write = spufs_fpcr_write,
569 .llseek = generic_file_llseek,
572 /* generic open function for all pipe-like files */
573 static int spufs_pipe_open(struct inode *inode, struct file *file)
575 struct spufs_inode_info *i = SPUFS_I(inode);
576 file->private_data = i->i_ctx;
578 return stream_open(inode, file);
582 * Read as many bytes from the mailbox as possible, until
583 * one of the conditions becomes true:
585 * - no more data available in the mailbox
586 * - end of the user provided buffer
587 * - end of the mapped area
589 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
590 size_t len, loff_t *pos)
592 struct spu_context *ctx = file->private_data;
593 u32 mbox_data, __user *udata;
599 if (!access_ok(buf, len))
602 udata = (void __user *)buf;
604 count = spu_acquire(ctx);
608 for (count = 0; (count + 4) <= len; count += 4, udata++) {
610 ret = ctx->ops->mbox_read(ctx, &mbox_data);
615 * at the end of the mapped area, we can fault
616 * but still need to return the data we have
617 * read successfully so far.
619 ret = __put_user(mbox_data, udata);
634 static const struct file_operations spufs_mbox_fops = {
635 .open = spufs_pipe_open,
636 .read = spufs_mbox_read,
640 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
641 size_t len, loff_t *pos)
643 struct spu_context *ctx = file->private_data;
650 ret = spu_acquire(ctx);
654 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
658 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
664 static const struct file_operations spufs_mbox_stat_fops = {
665 .open = spufs_pipe_open,
666 .read = spufs_mbox_stat_read,
670 /* low-level ibox access function */
671 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
673 return ctx->ops->ibox_read(ctx, data);
676 /* interrupt-level ibox callback function. */
677 void spufs_ibox_callback(struct spu *spu)
679 struct spu_context *ctx = spu->ctx;
682 wake_up_all(&ctx->ibox_wq);
686 * Read as many bytes from the interrupt mailbox as possible, until
687 * one of the conditions becomes true:
689 * - no more data available in the mailbox
690 * - end of the user provided buffer
691 * - end of the mapped area
693 * If the file is opened without O_NONBLOCK, we wait here until
694 * any data is available, but return when we have been able to
697 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
698 size_t len, loff_t *pos)
700 struct spu_context *ctx = file->private_data;
701 u32 ibox_data, __user *udata;
707 if (!access_ok(buf, len))
710 udata = (void __user *)buf;
712 count = spu_acquire(ctx);
716 /* wait only for the first element */
718 if (file->f_flags & O_NONBLOCK) {
719 if (!spu_ibox_read(ctx, &ibox_data)) {
724 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
729 /* if we can't write at all, return -EFAULT */
730 count = __put_user(ibox_data, udata);
734 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
736 ret = ctx->ops->ibox_read(ctx, &ibox_data);
740 * at the end of the mapped area, we can fault
741 * but still need to return the data we have
742 * read successfully so far.
744 ret = __put_user(ibox_data, udata);
755 static __poll_t spufs_ibox_poll(struct file *file, poll_table *wait)
757 struct spu_context *ctx = file->private_data;
760 poll_wait(file, &ctx->ibox_wq, wait);
763 * For now keep this uninterruptible and also ignore the rule
764 * that poll should not sleep. Will be fixed later.
766 mutex_lock(&ctx->state_mutex);
767 mask = ctx->ops->mbox_stat_poll(ctx, EPOLLIN | EPOLLRDNORM);
773 static const struct file_operations spufs_ibox_fops = {
774 .open = spufs_pipe_open,
775 .read = spufs_ibox_read,
776 .poll = spufs_ibox_poll,
780 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
781 size_t len, loff_t *pos)
783 struct spu_context *ctx = file->private_data;
790 ret = spu_acquire(ctx);
793 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
796 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
802 static const struct file_operations spufs_ibox_stat_fops = {
803 .open = spufs_pipe_open,
804 .read = spufs_ibox_stat_read,
808 /* low-level mailbox write */
809 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
811 return ctx->ops->wbox_write(ctx, data);
814 /* interrupt-level wbox callback function. */
815 void spufs_wbox_callback(struct spu *spu)
817 struct spu_context *ctx = spu->ctx;
820 wake_up_all(&ctx->wbox_wq);
824 * Write as many bytes to the interrupt mailbox as possible, until
825 * one of the conditions becomes true:
827 * - the mailbox is full
828 * - end of the user provided buffer
829 * - end of the mapped area
831 * If the file is opened without O_NONBLOCK, we wait here until
832 * space is available, but return when we have been able to
835 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
836 size_t len, loff_t *pos)
838 struct spu_context *ctx = file->private_data;
839 u32 wbox_data, __user *udata;
845 udata = (void __user *)buf;
846 if (!access_ok(buf, len))
849 if (__get_user(wbox_data, udata))
852 count = spu_acquire(ctx);
857 * make sure we can at least write one element, by waiting
858 * in case of !O_NONBLOCK
861 if (file->f_flags & O_NONBLOCK) {
862 if (!spu_wbox_write(ctx, wbox_data)) {
867 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
873 /* write as much as possible */
874 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
876 ret = __get_user(wbox_data, udata);
880 ret = spu_wbox_write(ctx, wbox_data);
891 static __poll_t spufs_wbox_poll(struct file *file, poll_table *wait)
893 struct spu_context *ctx = file->private_data;
896 poll_wait(file, &ctx->wbox_wq, wait);
899 * For now keep this uninterruptible and also ignore the rule
900 * that poll should not sleep. Will be fixed later.
902 mutex_lock(&ctx->state_mutex);
903 mask = ctx->ops->mbox_stat_poll(ctx, EPOLLOUT | EPOLLWRNORM);
909 static const struct file_operations spufs_wbox_fops = {
910 .open = spufs_pipe_open,
911 .write = spufs_wbox_write,
912 .poll = spufs_wbox_poll,
916 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
917 size_t len, loff_t *pos)
919 struct spu_context *ctx = file->private_data;
926 ret = spu_acquire(ctx);
929 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
932 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
938 static const struct file_operations spufs_wbox_stat_fops = {
939 .open = spufs_pipe_open,
940 .read = spufs_wbox_stat_read,
944 static int spufs_signal1_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;
952 ctx->signal1 = inode->i_mapping;
953 mutex_unlock(&ctx->mapping_lock);
954 return nonseekable_open(inode, file);
958 spufs_signal1_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);
966 mutex_unlock(&ctx->mapping_lock);
970 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
971 size_t len, loff_t *pos)
979 if (ctx->csa.spu_chnlcnt_RW[3]) {
980 data = ctx->csa.spu_chnldata_RW[3];
987 if (copy_to_user(buf, &data, 4))
994 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
995 size_t len, loff_t *pos)
998 struct spu_context *ctx = file->private_data;
1000 ret = spu_acquire_saved(ctx);
1003 ret = __spufs_signal1_read(ctx, buf, len, pos);
1004 spu_release_saved(ctx);
1009 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1010 size_t len, loff_t *pos)
1012 struct spu_context *ctx;
1016 ctx = file->private_data;
1021 if (copy_from_user(&data, buf, 4))
1024 ret = spu_acquire(ctx);
1027 ctx->ops->signal1_write(ctx, data);
1034 spufs_signal1_mmap_fault(struct vm_fault *vmf)
1036 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1037 return spufs_ps_fault(vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1038 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
1039 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1040 * signal 1 and 2 area
1042 return spufs_ps_fault(vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
1044 #error unsupported page size
1048 static const struct vm_operations_struct spufs_signal1_mmap_vmops = {
1049 .fault = spufs_signal1_mmap_fault,
1052 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1054 if (!(vma->vm_flags & VM_SHARED))
1057 vma->vm_flags |= VM_IO | VM_PFNMAP;
1058 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1060 vma->vm_ops = &spufs_signal1_mmap_vmops;
1064 static const struct file_operations spufs_signal1_fops = {
1065 .open = spufs_signal1_open,
1066 .release = spufs_signal1_release,
1067 .read = spufs_signal1_read,
1068 .write = spufs_signal1_write,
1069 .mmap = spufs_signal1_mmap,
1070 .llseek = no_llseek,
1073 static const struct file_operations spufs_signal1_nosched_fops = {
1074 .open = spufs_signal1_open,
1075 .release = spufs_signal1_release,
1076 .write = spufs_signal1_write,
1077 .mmap = spufs_signal1_mmap,
1078 .llseek = no_llseek,
1081 static int spufs_signal2_open(struct inode *inode, struct file *file)
1083 struct spufs_inode_info *i = SPUFS_I(inode);
1084 struct spu_context *ctx = i->i_ctx;
1086 mutex_lock(&ctx->mapping_lock);
1087 file->private_data = ctx;
1088 if (!i->i_openers++)
1089 ctx->signal2 = inode->i_mapping;
1090 mutex_unlock(&ctx->mapping_lock);
1091 return nonseekable_open(inode, file);
1095 spufs_signal2_release(struct inode *inode, struct file *file)
1097 struct spufs_inode_info *i = SPUFS_I(inode);
1098 struct spu_context *ctx = i->i_ctx;
1100 mutex_lock(&ctx->mapping_lock);
1101 if (!--i->i_openers)
1102 ctx->signal2 = NULL;
1103 mutex_unlock(&ctx->mapping_lock);
1107 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
1108 size_t len, loff_t *pos)
1116 if (ctx->csa.spu_chnlcnt_RW[4]) {
1117 data = ctx->csa.spu_chnldata_RW[4];
1124 if (copy_to_user(buf, &data, 4))
1131 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1132 size_t len, loff_t *pos)
1134 struct spu_context *ctx = file->private_data;
1137 ret = spu_acquire_saved(ctx);
1140 ret = __spufs_signal2_read(ctx, buf, len, pos);
1141 spu_release_saved(ctx);
1146 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1147 size_t len, loff_t *pos)
1149 struct spu_context *ctx;
1153 ctx = file->private_data;
1158 if (copy_from_user(&data, buf, 4))
1161 ret = spu_acquire(ctx);
1164 ctx->ops->signal2_write(ctx, data);
1172 spufs_signal2_mmap_fault(struct vm_fault *vmf)
1174 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1175 return spufs_ps_fault(vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1176 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
1177 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1178 * signal 1 and 2 area
1180 return spufs_ps_fault(vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
1182 #error unsupported page size
1186 static const struct vm_operations_struct spufs_signal2_mmap_vmops = {
1187 .fault = spufs_signal2_mmap_fault,
1190 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1192 if (!(vma->vm_flags & VM_SHARED))
1195 vma->vm_flags |= VM_IO | VM_PFNMAP;
1196 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1198 vma->vm_ops = &spufs_signal2_mmap_vmops;
1201 #else /* SPUFS_MMAP_4K */
1202 #define spufs_signal2_mmap NULL
1203 #endif /* !SPUFS_MMAP_4K */
1205 static const struct file_operations spufs_signal2_fops = {
1206 .open = spufs_signal2_open,
1207 .release = spufs_signal2_release,
1208 .read = spufs_signal2_read,
1209 .write = spufs_signal2_write,
1210 .mmap = spufs_signal2_mmap,
1211 .llseek = no_llseek,
1214 static const struct file_operations spufs_signal2_nosched_fops = {
1215 .open = spufs_signal2_open,
1216 .release = spufs_signal2_release,
1217 .write = spufs_signal2_write,
1218 .mmap = spufs_signal2_mmap,
1219 .llseek = no_llseek,
1223 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1224 * work of acquiring (or not) the SPU context before calling through
1225 * to the actual get routine. The set routine is called directly.
1227 #define SPU_ATTR_NOACQUIRE 0
1228 #define SPU_ATTR_ACQUIRE 1
1229 #define SPU_ATTR_ACQUIRE_SAVED 2
1231 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
1232 static int __##__get(void *data, u64 *val) \
1234 struct spu_context *ctx = data; \
1237 if (__acquire == SPU_ATTR_ACQUIRE) { \
1238 ret = spu_acquire(ctx); \
1241 *val = __get(ctx); \
1243 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1244 ret = spu_acquire_saved(ctx); \
1247 *val = __get(ctx); \
1248 spu_release_saved(ctx); \
1250 *val = __get(ctx); \
1254 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1256 static int spufs_signal1_type_set(void *data, u64 val)
1258 struct spu_context *ctx = data;
1261 ret = spu_acquire(ctx);
1264 ctx->ops->signal1_type_set(ctx, val);
1270 static u64 spufs_signal1_type_get(struct spu_context *ctx)
1272 return ctx->ops->signal1_type_get(ctx);
1274 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1275 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1278 static int spufs_signal2_type_set(void *data, u64 val)
1280 struct spu_context *ctx = data;
1283 ret = spu_acquire(ctx);
1286 ctx->ops->signal2_type_set(ctx, val);
1292 static u64 spufs_signal2_type_get(struct spu_context *ctx)
1294 return ctx->ops->signal2_type_get(ctx);
1296 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1297 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1301 spufs_mss_mmap_fault(struct vm_fault *vmf)
1303 return spufs_ps_fault(vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
1306 static const struct vm_operations_struct spufs_mss_mmap_vmops = {
1307 .fault = spufs_mss_mmap_fault,
1311 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1313 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1315 if (!(vma->vm_flags & VM_SHARED))
1318 vma->vm_flags |= VM_IO | VM_PFNMAP;
1319 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1321 vma->vm_ops = &spufs_mss_mmap_vmops;
1324 #else /* SPUFS_MMAP_4K */
1325 #define spufs_mss_mmap NULL
1326 #endif /* !SPUFS_MMAP_4K */
1328 static int spufs_mss_open(struct inode *inode, struct file *file)
1330 struct spufs_inode_info *i = SPUFS_I(inode);
1331 struct spu_context *ctx = i->i_ctx;
1333 file->private_data = i->i_ctx;
1335 mutex_lock(&ctx->mapping_lock);
1336 if (!i->i_openers++)
1337 ctx->mss = inode->i_mapping;
1338 mutex_unlock(&ctx->mapping_lock);
1339 return nonseekable_open(inode, file);
1343 spufs_mss_release(struct inode *inode, struct file *file)
1345 struct spufs_inode_info *i = SPUFS_I(inode);
1346 struct spu_context *ctx = i->i_ctx;
1348 mutex_lock(&ctx->mapping_lock);
1349 if (!--i->i_openers)
1351 mutex_unlock(&ctx->mapping_lock);
1355 static const struct file_operations spufs_mss_fops = {
1356 .open = spufs_mss_open,
1357 .release = spufs_mss_release,
1358 .mmap = spufs_mss_mmap,
1359 .llseek = no_llseek,
1363 spufs_psmap_mmap_fault(struct vm_fault *vmf)
1365 return spufs_ps_fault(vmf, 0x0000, SPUFS_PS_MAP_SIZE);
1368 static const struct vm_operations_struct spufs_psmap_mmap_vmops = {
1369 .fault = spufs_psmap_mmap_fault,
1373 * mmap support for full problem state area [0x00000 - 0x1ffff].
1375 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1377 if (!(vma->vm_flags & VM_SHARED))
1380 vma->vm_flags |= VM_IO | VM_PFNMAP;
1381 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1383 vma->vm_ops = &spufs_psmap_mmap_vmops;
1387 static int spufs_psmap_open(struct inode *inode, struct file *file)
1389 struct spufs_inode_info *i = SPUFS_I(inode);
1390 struct spu_context *ctx = i->i_ctx;
1392 mutex_lock(&ctx->mapping_lock);
1393 file->private_data = i->i_ctx;
1394 if (!i->i_openers++)
1395 ctx->psmap = inode->i_mapping;
1396 mutex_unlock(&ctx->mapping_lock);
1397 return nonseekable_open(inode, file);
1401 spufs_psmap_release(struct inode *inode, struct file *file)
1403 struct spufs_inode_info *i = SPUFS_I(inode);
1404 struct spu_context *ctx = i->i_ctx;
1406 mutex_lock(&ctx->mapping_lock);
1407 if (!--i->i_openers)
1409 mutex_unlock(&ctx->mapping_lock);
1413 static const struct file_operations spufs_psmap_fops = {
1414 .open = spufs_psmap_open,
1415 .release = spufs_psmap_release,
1416 .mmap = spufs_psmap_mmap,
1417 .llseek = no_llseek,
1423 spufs_mfc_mmap_fault(struct vm_fault *vmf)
1425 return spufs_ps_fault(vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
1428 static const struct vm_operations_struct spufs_mfc_mmap_vmops = {
1429 .fault = spufs_mfc_mmap_fault,
1433 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1435 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1437 if (!(vma->vm_flags & VM_SHARED))
1440 vma->vm_flags |= VM_IO | VM_PFNMAP;
1441 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1443 vma->vm_ops = &spufs_mfc_mmap_vmops;
1446 #else /* SPUFS_MMAP_4K */
1447 #define spufs_mfc_mmap NULL
1448 #endif /* !SPUFS_MMAP_4K */
1450 static int spufs_mfc_open(struct inode *inode, struct file *file)
1452 struct spufs_inode_info *i = SPUFS_I(inode);
1453 struct spu_context *ctx = i->i_ctx;
1455 /* we don't want to deal with DMA into other processes */
1456 if (ctx->owner != current->mm)
1459 if (atomic_read(&inode->i_count) != 1)
1462 mutex_lock(&ctx->mapping_lock);
1463 file->private_data = ctx;
1464 if (!i->i_openers++)
1465 ctx->mfc = inode->i_mapping;
1466 mutex_unlock(&ctx->mapping_lock);
1467 return nonseekable_open(inode, file);
1471 spufs_mfc_release(struct inode *inode, struct file *file)
1473 struct spufs_inode_info *i = SPUFS_I(inode);
1474 struct spu_context *ctx = i->i_ctx;
1476 mutex_lock(&ctx->mapping_lock);
1477 if (!--i->i_openers)
1479 mutex_unlock(&ctx->mapping_lock);
1483 /* interrupt-level mfc callback function. */
1484 void spufs_mfc_callback(struct spu *spu)
1486 struct spu_context *ctx = spu->ctx;
1489 wake_up_all(&ctx->mfc_wq);
1492 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1494 /* See if there is one tag group is complete */
1495 /* FIXME we need locking around tagwait */
1496 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1497 ctx->tagwait &= ~*status;
1501 /* enable interrupt waiting for any tag group,
1502 may silently fail if interrupts are already enabled */
1503 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1507 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1508 size_t size, loff_t *pos)
1510 struct spu_context *ctx = file->private_data;
1517 ret = spu_acquire(ctx);
1522 if (file->f_flags & O_NONBLOCK) {
1523 status = ctx->ops->read_mfc_tagstatus(ctx);
1524 if (!(status & ctx->tagwait))
1527 /* XXX(hch): shouldn't we clear ret here? */
1528 ctx->tagwait &= ~status;
1530 ret = spufs_wait(ctx->mfc_wq,
1531 spufs_read_mfc_tagstatus(ctx, &status));
1538 if (copy_to_user(buffer, &status, 4))
1545 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1547 pr_debug("queueing DMA %x %llx %x %x %x\n", cmd->lsa,
1548 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1559 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1563 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1564 pr_debug("invalid DMA alignment, ea %llx lsa %x\n",
1569 switch (cmd->size & 0xf) {
1590 pr_debug("invalid DMA alignment %x for size %x\n",
1591 cmd->lsa & 0xf, cmd->size);
1595 if (cmd->size > 16 * 1024) {
1596 pr_debug("invalid DMA size %x\n", cmd->size);
1600 if (cmd->tag & 0xfff0) {
1601 /* we reserve the higher tag numbers for kernel use */
1602 pr_debug("invalid DMA tag\n");
1607 /* not supported in this version */
1608 pr_debug("invalid DMA class\n");
1615 static int spu_send_mfc_command(struct spu_context *ctx,
1616 struct mfc_dma_command cmd,
1619 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1620 if (*error == -EAGAIN) {
1621 /* wait for any tag group to complete
1622 so we have space for the new command */
1623 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1624 /* try again, because the queue might be
1626 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1627 if (*error == -EAGAIN)
1633 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1634 size_t size, loff_t *pos)
1636 struct spu_context *ctx = file->private_data;
1637 struct mfc_dma_command cmd;
1640 if (size != sizeof cmd)
1644 if (copy_from_user(&cmd, buffer, sizeof cmd))
1647 ret = spufs_check_valid_dma(&cmd);
1651 ret = spu_acquire(ctx);
1655 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1659 if (file->f_flags & O_NONBLOCK) {
1660 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1663 ret = spufs_wait(ctx->mfc_wq,
1664 spu_send_mfc_command(ctx, cmd, &status));
1674 ctx->tagwait |= 1 << cmd.tag;
1683 static __poll_t spufs_mfc_poll(struct file *file,poll_table *wait)
1685 struct spu_context *ctx = file->private_data;
1686 u32 free_elements, tagstatus;
1689 poll_wait(file, &ctx->mfc_wq, wait);
1692 * For now keep this uninterruptible and also ignore the rule
1693 * that poll should not sleep. Will be fixed later.
1695 mutex_lock(&ctx->state_mutex);
1696 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1697 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1698 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1702 if (free_elements & 0xffff)
1703 mask |= EPOLLOUT | EPOLLWRNORM;
1704 if (tagstatus & ctx->tagwait)
1705 mask |= EPOLLIN | EPOLLRDNORM;
1707 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
1708 free_elements, tagstatus, ctx->tagwait);
1713 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1715 struct spu_context *ctx = file->private_data;
1718 ret = spu_acquire(ctx);
1722 /* this currently hangs */
1723 ret = spufs_wait(ctx->mfc_wq,
1724 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1727 ret = spufs_wait(ctx->mfc_wq,
1728 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1739 static int spufs_mfc_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1741 struct inode *inode = file_inode(file);
1742 int err = file_write_and_wait_range(file, start, end);
1745 err = spufs_mfc_flush(file, NULL);
1746 inode_unlock(inode);
1751 static const struct file_operations spufs_mfc_fops = {
1752 .open = spufs_mfc_open,
1753 .release = spufs_mfc_release,
1754 .read = spufs_mfc_read,
1755 .write = spufs_mfc_write,
1756 .poll = spufs_mfc_poll,
1757 .flush = spufs_mfc_flush,
1758 .fsync = spufs_mfc_fsync,
1759 .mmap = spufs_mfc_mmap,
1760 .llseek = no_llseek,
1763 static int spufs_npc_set(void *data, u64 val)
1765 struct spu_context *ctx = data;
1768 ret = spu_acquire(ctx);
1771 ctx->ops->npc_write(ctx, val);
1777 static u64 spufs_npc_get(struct spu_context *ctx)
1779 return ctx->ops->npc_read(ctx);
1781 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1782 "0x%llx\n", SPU_ATTR_ACQUIRE);
1784 static int spufs_decr_set(void *data, u64 val)
1786 struct spu_context *ctx = data;
1787 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1790 ret = spu_acquire_saved(ctx);
1793 lscsa->decr.slot[0] = (u32) val;
1794 spu_release_saved(ctx);
1799 static u64 spufs_decr_get(struct spu_context *ctx)
1801 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1802 return lscsa->decr.slot[0];
1804 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1805 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
1807 static int spufs_decr_status_set(void *data, u64 val)
1809 struct spu_context *ctx = data;
1812 ret = spu_acquire_saved(ctx);
1816 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1818 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1819 spu_release_saved(ctx);
1824 static u64 spufs_decr_status_get(struct spu_context *ctx)
1826 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1827 return SPU_DECR_STATUS_RUNNING;
1831 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1832 spufs_decr_status_set, "0x%llx\n",
1833 SPU_ATTR_ACQUIRE_SAVED);
1835 static int spufs_event_mask_set(void *data, u64 val)
1837 struct spu_context *ctx = data;
1838 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1841 ret = spu_acquire_saved(ctx);
1844 lscsa->event_mask.slot[0] = (u32) val;
1845 spu_release_saved(ctx);
1850 static u64 spufs_event_mask_get(struct spu_context *ctx)
1852 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1853 return lscsa->event_mask.slot[0];
1856 DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1857 spufs_event_mask_set, "0x%llx\n",
1858 SPU_ATTR_ACQUIRE_SAVED);
1860 static u64 spufs_event_status_get(struct spu_context *ctx)
1862 struct spu_state *state = &ctx->csa;
1864 stat = state->spu_chnlcnt_RW[0];
1866 return state->spu_chnldata_RW[0];
1869 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1870 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1872 static int spufs_srr0_set(void *data, u64 val)
1874 struct spu_context *ctx = data;
1875 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1878 ret = spu_acquire_saved(ctx);
1881 lscsa->srr0.slot[0] = (u32) val;
1882 spu_release_saved(ctx);
1887 static u64 spufs_srr0_get(struct spu_context *ctx)
1889 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1890 return lscsa->srr0.slot[0];
1892 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1893 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1895 static u64 spufs_id_get(struct spu_context *ctx)
1899 if (ctx->state == SPU_STATE_RUNNABLE)
1900 num = ctx->spu->number;
1902 num = (unsigned int)-1;
1906 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1909 static u64 spufs_object_id_get(struct spu_context *ctx)
1911 /* FIXME: Should there really be no locking here? */
1912 return ctx->object_id;
1915 static int spufs_object_id_set(void *data, u64 id)
1917 struct spu_context *ctx = data;
1918 ctx->object_id = id;
1923 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1924 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
1926 static u64 spufs_lslr_get(struct spu_context *ctx)
1928 return ctx->csa.priv2.spu_lslr_RW;
1930 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1931 SPU_ATTR_ACQUIRE_SAVED);
1933 static int spufs_info_open(struct inode *inode, struct file *file)
1935 struct spufs_inode_info *i = SPUFS_I(inode);
1936 struct spu_context *ctx = i->i_ctx;
1937 file->private_data = ctx;
1941 static int spufs_caps_show(struct seq_file *s, void *private)
1943 struct spu_context *ctx = s->private;
1945 if (!(ctx->flags & SPU_CREATE_NOSCHED))
1946 seq_puts(s, "sched\n");
1947 if (!(ctx->flags & SPU_CREATE_ISOLATE))
1948 seq_puts(s, "step\n");
1952 static int spufs_caps_open(struct inode *inode, struct file *file)
1954 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1957 static const struct file_operations spufs_caps_fops = {
1958 .open = spufs_caps_open,
1960 .llseek = seq_lseek,
1961 .release = single_release,
1964 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1965 char __user *buf, size_t len, loff_t *pos)
1969 /* EOF if there's no entry in the mbox */
1970 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
1973 data = ctx->csa.prob.pu_mb_R;
1975 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1978 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1979 size_t len, loff_t *pos)
1981 struct spu_context *ctx = file->private_data;
1985 if (!access_ok(buf, len))
1988 ret = spu_acquire_saved(ctx);
1991 spin_lock(&ctx->csa.register_lock);
1992 stat = ctx->csa.prob.mb_stat_R;
1993 data = ctx->csa.prob.pu_mb_R;
1994 spin_unlock(&ctx->csa.register_lock);
1995 spu_release_saved(ctx);
1997 /* EOF if there's no entry in the mbox */
1998 if (!(stat & 0x0000ff))
2001 return simple_read_from_buffer(buf, len, pos, &data, sizeof(data));
2004 static const struct file_operations spufs_mbox_info_fops = {
2005 .open = spufs_info_open,
2006 .read = spufs_mbox_info_read,
2007 .llseek = generic_file_llseek,
2010 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2011 char __user *buf, size_t len, loff_t *pos)
2015 /* EOF if there's no entry in the ibox */
2016 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2019 data = ctx->csa.priv2.puint_mb_R;
2021 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2024 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2025 size_t len, loff_t *pos)
2027 struct spu_context *ctx = file->private_data;
2031 if (!access_ok(buf, len))
2034 ret = spu_acquire_saved(ctx);
2037 spin_lock(&ctx->csa.register_lock);
2038 stat = ctx->csa.prob.mb_stat_R;
2039 data = ctx->csa.priv2.puint_mb_R;
2040 spin_unlock(&ctx->csa.register_lock);
2041 spu_release_saved(ctx);
2043 /* EOF if there's no entry in the ibox */
2044 if (!(stat & 0xff0000))
2047 return simple_read_from_buffer(buf, len, pos, &data, sizeof(data));
2050 static const struct file_operations spufs_ibox_info_fops = {
2051 .open = spufs_info_open,
2052 .read = spufs_ibox_info_read,
2053 .llseek = generic_file_llseek,
2056 static size_t spufs_wbox_info_cnt(struct spu_context *ctx)
2058 return (4 - ((ctx->csa.prob.mb_stat_R & 0x00ff00) >> 8)) * sizeof(u32);
2061 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2062 char __user *buf, size_t len, loff_t *pos)
2068 wbox_stat = ctx->csa.prob.mb_stat_R;
2069 cnt = spufs_wbox_info_cnt(ctx);
2070 for (i = 0; i < cnt; i++) {
2071 data[i] = ctx->csa.spu_mailbox_data[i];
2074 return simple_read_from_buffer(buf, len, pos, &data,
2078 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2079 size_t len, loff_t *pos)
2081 struct spu_context *ctx = file->private_data;
2082 u32 data[ARRAY_SIZE(ctx->csa.spu_mailbox_data)];
2085 if (!access_ok(buf, len))
2088 ret = spu_acquire_saved(ctx);
2091 spin_lock(&ctx->csa.register_lock);
2092 count = spufs_wbox_info_cnt(ctx);
2093 memcpy(&data, &ctx->csa.spu_mailbox_data, sizeof(data));
2094 spin_unlock(&ctx->csa.register_lock);
2095 spu_release_saved(ctx);
2097 return simple_read_from_buffer(buf, len, pos, &data,
2098 count * sizeof(u32));
2101 static const struct file_operations spufs_wbox_info_fops = {
2102 .open = spufs_info_open,
2103 .read = spufs_wbox_info_read,
2104 .llseek = generic_file_llseek,
2107 static void spufs_get_dma_info(struct spu_context *ctx,
2108 struct spu_dma_info *info)
2112 info->dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2113 info->dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2114 info->dma_info_status = ctx->csa.spu_chnldata_RW[24];
2115 info->dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2116 info->dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2117 for (i = 0; i < 16; i++) {
2118 struct mfc_cq_sr *qp = &info->dma_info_command_data[i];
2119 struct mfc_cq_sr *spuqp = &ctx->csa.priv2.spuq[i];
2121 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2122 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2123 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2124 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2128 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2129 char __user *buf, size_t len, loff_t *pos)
2131 struct spu_dma_info info;
2133 spufs_get_dma_info(ctx, &info);
2135 return simple_read_from_buffer(buf, len, pos, &info,
2139 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2140 size_t len, loff_t *pos)
2142 struct spu_context *ctx = file->private_data;
2143 struct spu_dma_info info;
2146 if (!access_ok(buf, len))
2149 ret = spu_acquire_saved(ctx);
2152 spin_lock(&ctx->csa.register_lock);
2153 spufs_get_dma_info(ctx, &info);
2154 spin_unlock(&ctx->csa.register_lock);
2155 spu_release_saved(ctx);
2157 return simple_read_from_buffer(buf, len, pos, &info,
2161 static const struct file_operations spufs_dma_info_fops = {
2162 .open = spufs_info_open,
2163 .read = spufs_dma_info_read,
2164 .llseek = no_llseek,
2167 static void spufs_get_proxydma_info(struct spu_context *ctx,
2168 struct spu_proxydma_info *info)
2172 info->proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2173 info->proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2174 info->proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2176 for (i = 0; i < 8; i++) {
2177 struct mfc_cq_sr *qp = &info->proxydma_info_command_data[i];
2178 struct mfc_cq_sr *puqp = &ctx->csa.priv2.puq[i];
2180 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2181 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2182 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2183 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2187 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2188 char __user *buf, size_t len, loff_t *pos)
2190 struct spu_proxydma_info info;
2191 int ret = sizeof info;
2196 if (!access_ok(buf, len))
2199 spufs_get_proxydma_info(ctx, &info);
2201 return simple_read_from_buffer(buf, len, pos, &info,
2205 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2206 size_t len, loff_t *pos)
2208 struct spu_context *ctx = file->private_data;
2209 struct spu_proxydma_info info;
2212 ret = spu_acquire_saved(ctx);
2215 spin_lock(&ctx->csa.register_lock);
2216 spufs_get_proxydma_info(ctx, &info);
2217 spin_unlock(&ctx->csa.register_lock);
2218 spu_release_saved(ctx);
2220 return simple_read_from_buffer(buf, len, pos, &info,
2224 static const struct file_operations spufs_proxydma_info_fops = {
2225 .open = spufs_info_open,
2226 .read = spufs_proxydma_info_read,
2227 .llseek = no_llseek,
2230 static int spufs_show_tid(struct seq_file *s, void *private)
2232 struct spu_context *ctx = s->private;
2234 seq_printf(s, "%d\n", ctx->tid);
2238 static int spufs_tid_open(struct inode *inode, struct file *file)
2240 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2243 static const struct file_operations spufs_tid_fops = {
2244 .open = spufs_tid_open,
2246 .llseek = seq_lseek,
2247 .release = single_release,
2250 static const char *ctx_state_names[] = {
2251 "user", "system", "iowait", "loaded"
2254 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2255 enum spu_utilization_state state)
2257 unsigned long long time = ctx->stats.times[state];
2260 * In general, utilization statistics are updated by the controlling
2261 * thread as the spu context moves through various well defined
2262 * state transitions, but if the context is lazily loaded its
2263 * utilization statistics are not updated as the controlling thread
2264 * is not tightly coupled with the execution of the spu context. We
2265 * calculate and apply the time delta from the last recorded state
2266 * of the spu context.
2268 if (ctx->spu && ctx->stats.util_state == state) {
2269 time += ktime_get_ns() - ctx->stats.tstamp;
2272 return time / NSEC_PER_MSEC;
2275 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2277 unsigned long long slb_flts = ctx->stats.slb_flt;
2279 if (ctx->state == SPU_STATE_RUNNABLE) {
2280 slb_flts += (ctx->spu->stats.slb_flt -
2281 ctx->stats.slb_flt_base);
2287 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2289 unsigned long long class2_intrs = ctx->stats.class2_intr;
2291 if (ctx->state == SPU_STATE_RUNNABLE) {
2292 class2_intrs += (ctx->spu->stats.class2_intr -
2293 ctx->stats.class2_intr_base);
2296 return class2_intrs;
2300 static int spufs_show_stat(struct seq_file *s, void *private)
2302 struct spu_context *ctx = s->private;
2305 ret = spu_acquire(ctx);
2309 seq_printf(s, "%s %llu %llu %llu %llu "
2310 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2311 ctx_state_names[ctx->stats.util_state],
2312 spufs_acct_time(ctx, SPU_UTIL_USER),
2313 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2314 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2315 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2316 ctx->stats.vol_ctx_switch,
2317 ctx->stats.invol_ctx_switch,
2318 spufs_slb_flts(ctx),
2319 ctx->stats.hash_flt,
2322 spufs_class2_intrs(ctx),
2323 ctx->stats.libassist);
2328 static int spufs_stat_open(struct inode *inode, struct file *file)
2330 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2333 static const struct file_operations spufs_stat_fops = {
2334 .open = spufs_stat_open,
2336 .llseek = seq_lseek,
2337 .release = single_release,
2340 static inline int spufs_switch_log_used(struct spu_context *ctx)
2342 return (ctx->switch_log->head - ctx->switch_log->tail) %
2346 static inline int spufs_switch_log_avail(struct spu_context *ctx)
2348 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2351 static int spufs_switch_log_open(struct inode *inode, struct file *file)
2353 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2356 rc = spu_acquire(ctx);
2360 if (ctx->switch_log) {
2365 ctx->switch_log = kmalloc(struct_size(ctx->switch_log, log,
2366 SWITCH_LOG_BUFSIZE), GFP_KERNEL);
2368 if (!ctx->switch_log) {
2373 ctx->switch_log->head = ctx->switch_log->tail = 0;
2374 init_waitqueue_head(&ctx->switch_log->wait);
2382 static int spufs_switch_log_release(struct inode *inode, struct file *file)
2384 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2387 rc = spu_acquire(ctx);
2391 kfree(ctx->switch_log);
2392 ctx->switch_log = NULL;
2398 static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2400 struct switch_log_entry *p;
2402 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2404 return snprintf(tbuf, n, "%llu.%09u %d %u %u %llu\n",
2405 (unsigned long long) p->tstamp.tv_sec,
2406 (unsigned int) p->tstamp.tv_nsec,
2408 (unsigned int) p->type,
2409 (unsigned int) p->val,
2410 (unsigned long long) p->timebase);
2413 static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2414 size_t len, loff_t *ppos)
2416 struct inode *inode = file_inode(file);
2417 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2418 int error = 0, cnt = 0;
2423 error = spu_acquire(ctx);
2431 if (spufs_switch_log_used(ctx) == 0) {
2433 /* If there's data ready to go, we can
2434 * just return straight away */
2437 } else if (file->f_flags & O_NONBLOCK) {
2442 /* spufs_wait will drop the mutex and
2443 * re-acquire, but since we're in read(), the
2444 * file cannot be _released (and so
2445 * ctx->switch_log is stable).
2447 error = spufs_wait(ctx->switch_log->wait,
2448 spufs_switch_log_used(ctx) > 0);
2450 /* On error, spufs_wait returns without the
2451 * state mutex held */
2455 /* We may have had entries read from underneath
2456 * us while we dropped the mutex in spufs_wait,
2458 if (spufs_switch_log_used(ctx) == 0)
2463 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
2465 ctx->switch_log->tail =
2466 (ctx->switch_log->tail + 1) %
2469 /* If the record is greater than space available return
2470 * partial buffer (so far) */
2473 error = copy_to_user(buf + cnt, tbuf, width);
2481 return cnt == 0 ? error : cnt;
2484 static __poll_t spufs_switch_log_poll(struct file *file, poll_table *wait)
2486 struct inode *inode = file_inode(file);
2487 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2491 poll_wait(file, &ctx->switch_log->wait, wait);
2493 rc = spu_acquire(ctx);
2497 if (spufs_switch_log_used(ctx) > 0)
2505 static const struct file_operations spufs_switch_log_fops = {
2506 .open = spufs_switch_log_open,
2507 .read = spufs_switch_log_read,
2508 .poll = spufs_switch_log_poll,
2509 .release = spufs_switch_log_release,
2510 .llseek = no_llseek,
2514 * Log a context switch event to a switch log reader.
2516 * Must be called with ctx->state_mutex held.
2518 void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2521 if (!ctx->switch_log)
2524 if (spufs_switch_log_avail(ctx) > 1) {
2525 struct switch_log_entry *p;
2527 p = ctx->switch_log->log + ctx->switch_log->head;
2528 ktime_get_ts64(&p->tstamp);
2529 p->timebase = get_tb();
2530 p->spu_id = spu ? spu->number : -1;
2534 ctx->switch_log->head =
2535 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2538 wake_up(&ctx->switch_log->wait);
2541 static int spufs_show_ctx(struct seq_file *s, void *private)
2543 struct spu_context *ctx = s->private;
2546 mutex_lock(&ctx->state_mutex);
2548 struct spu *spu = ctx->spu;
2549 struct spu_priv2 __iomem *priv2 = spu->priv2;
2551 spin_lock_irq(&spu->register_lock);
2552 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2553 spin_unlock_irq(&spu->register_lock);
2555 struct spu_state *csa = &ctx->csa;
2557 mfc_control_RW = csa->priv2.mfc_control_RW;
2560 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
2561 " %c %llx %llx %llx %llx %x %x\n",
2562 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2567 ctx->spu ? ctx->spu->number : -1,
2568 !list_empty(&ctx->rq) ? 'q' : ' ',
2569 ctx->csa.class_0_pending,
2570 ctx->csa.class_0_dar,
2571 ctx->csa.class_1_dsisr,
2573 ctx->ops->runcntl_read(ctx),
2574 ctx->ops->status_read(ctx));
2576 mutex_unlock(&ctx->state_mutex);
2581 static int spufs_ctx_open(struct inode *inode, struct file *file)
2583 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2586 static const struct file_operations spufs_ctx_fops = {
2587 .open = spufs_ctx_open,
2589 .llseek = seq_lseek,
2590 .release = single_release,
2593 const struct spufs_tree_descr spufs_dir_contents[] = {
2594 { "capabilities", &spufs_caps_fops, 0444, },
2595 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2596 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
2597 { "mbox", &spufs_mbox_fops, 0444, },
2598 { "ibox", &spufs_ibox_fops, 0444, },
2599 { "wbox", &spufs_wbox_fops, 0222, },
2600 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2601 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2602 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2603 { "signal1", &spufs_signal1_fops, 0666, },
2604 { "signal2", &spufs_signal2_fops, 0666, },
2605 { "signal1_type", &spufs_signal1_type, 0666, },
2606 { "signal2_type", &spufs_signal2_type, 0666, },
2607 { "cntl", &spufs_cntl_fops, 0666, },
2608 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
2609 { "lslr", &spufs_lslr_ops, 0444, },
2610 { "mfc", &spufs_mfc_fops, 0666, },
2611 { "mss", &spufs_mss_fops, 0666, },
2612 { "npc", &spufs_npc_ops, 0666, },
2613 { "srr0", &spufs_srr0_ops, 0666, },
2614 { "decr", &spufs_decr_ops, 0666, },
2615 { "decr_status", &spufs_decr_status_ops, 0666, },
2616 { "event_mask", &spufs_event_mask_ops, 0666, },
2617 { "event_status", &spufs_event_status_ops, 0444, },
2618 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
2619 { "phys-id", &spufs_id_ops, 0666, },
2620 { "object-id", &spufs_object_id_ops, 0666, },
2621 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
2622 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
2623 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
2624 { "dma_info", &spufs_dma_info_fops, 0444,
2625 sizeof(struct spu_dma_info), },
2626 { "proxydma_info", &spufs_proxydma_info_fops, 0444,
2627 sizeof(struct spu_proxydma_info)},
2628 { "tid", &spufs_tid_fops, 0444, },
2629 { "stat", &spufs_stat_fops, 0444, },
2630 { "switch_log", &spufs_switch_log_fops, 0444 },
2634 const struct spufs_tree_descr spufs_dir_nosched_contents[] = {
2635 { "capabilities", &spufs_caps_fops, 0444, },
2636 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2637 { "mbox", &spufs_mbox_fops, 0444, },
2638 { "ibox", &spufs_ibox_fops, 0444, },
2639 { "wbox", &spufs_wbox_fops, 0222, },
2640 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2641 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2642 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2643 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2644 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2645 { "signal1_type", &spufs_signal1_type, 0666, },
2646 { "signal2_type", &spufs_signal2_type, 0666, },
2647 { "mss", &spufs_mss_fops, 0666, },
2648 { "mfc", &spufs_mfc_fops, 0666, },
2649 { "cntl", &spufs_cntl_fops, 0666, },
2650 { "npc", &spufs_npc_ops, 0666, },
2651 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
2652 { "phys-id", &spufs_id_ops, 0666, },
2653 { "object-id", &spufs_object_id_ops, 0666, },
2654 { "tid", &spufs_tid_fops, 0444, },
2655 { "stat", &spufs_stat_fops, 0444, },
2659 const struct spufs_tree_descr spufs_dir_debug_contents[] = {
2660 { ".ctx", &spufs_ctx_fops, 0444, },
2664 const struct spufs_coredump_reader spufs_coredump_read[] = {
2665 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2666 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2667 { "lslr", NULL, spufs_lslr_get, 19 },
2668 { "decr", NULL, spufs_decr_get, 19 },
2669 { "decr_status", NULL, spufs_decr_status_get, 19 },
2670 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2671 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2672 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
2673 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2674 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2675 { "event_mask", NULL, spufs_event_mask_get, 19 },
2676 { "event_status", NULL, spufs_event_status_get, 19 },
2677 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2678 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2679 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2680 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2681 { "proxydma_info", __spufs_proxydma_info_read,
2682 NULL, sizeof(struct spu_proxydma_info)},
2683 { "object-id", NULL, spufs_object_id_get, 19 },
2684 { "npc", NULL, spufs_npc_get, 19 },