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>
12 #include <linux/coredump.h>
14 #include <linux/ioctl.h>
15 #include <linux/export.h>
16 #include <linux/pagemap.h>
17 #include <linux/poll.h>
18 #include <linux/ptrace.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
25 #include <asm/spu_info.h>
26 #include <linux/uaccess.h>
31 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
33 /* Simple attribute files */
35 int (*get)(void *, u64 *);
36 int (*set)(void *, u64);
37 char get_buf[24]; /* enough to store a u64 and "\n\0" */
40 const char *fmt; /* format for read operation */
41 struct mutex mutex; /* protects access to these buffers */
44 static int spufs_attr_open(struct inode *inode, struct file *file,
45 int (*get)(void *, u64 *), int (*set)(void *, u64),
48 struct spufs_attr *attr;
50 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
56 attr->data = inode->i_private;
58 mutex_init(&attr->mutex);
59 file->private_data = attr;
61 return nonseekable_open(inode, file);
64 static int spufs_attr_release(struct inode *inode, struct file *file)
66 kfree(file->private_data);
70 static ssize_t spufs_attr_read(struct file *file, char __user *buf,
71 size_t len, loff_t *ppos)
73 struct spufs_attr *attr;
77 attr = file->private_data;
81 ret = mutex_lock_interruptible(&attr->mutex);
85 if (*ppos) { /* continued read */
86 size = strlen(attr->get_buf);
87 } else { /* first read */
89 ret = attr->get(attr->data, &val);
93 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
94 attr->fmt, (unsigned long long)val);
97 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
99 mutex_unlock(&attr->mutex);
103 static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
104 size_t len, loff_t *ppos)
106 struct spufs_attr *attr;
111 attr = file->private_data;
115 ret = mutex_lock_interruptible(&attr->mutex);
120 size = min(sizeof(attr->set_buf) - 1, len);
121 if (copy_from_user(attr->set_buf, buf, size))
124 ret = len; /* claim we got the whole input */
125 attr->set_buf[size] = '\0';
126 val = simple_strtol(attr->set_buf, NULL, 0);
127 attr->set(attr->data, val);
129 mutex_unlock(&attr->mutex);
133 static ssize_t spufs_dump_emit(struct coredump_params *cprm, void *buf,
136 if (!dump_emit(cprm, buf, size))
141 #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
142 static int __fops ## _open(struct inode *inode, struct file *file) \
144 __simple_attr_check_format(__fmt, 0ull); \
145 return spufs_attr_open(inode, file, __get, __set, __fmt); \
147 static const struct file_operations __fops = { \
148 .open = __fops ## _open, \
149 .release = spufs_attr_release, \
150 .read = spufs_attr_read, \
151 .write = spufs_attr_write, \
152 .llseek = generic_file_llseek, \
157 spufs_mem_open(struct inode *inode, struct file *file)
159 struct spufs_inode_info *i = SPUFS_I(inode);
160 struct spu_context *ctx = i->i_ctx;
162 mutex_lock(&ctx->mapping_lock);
163 file->private_data = ctx;
165 ctx->local_store = inode->i_mapping;
166 mutex_unlock(&ctx->mapping_lock);
171 spufs_mem_release(struct inode *inode, struct file *file)
173 struct spufs_inode_info *i = SPUFS_I(inode);
174 struct spu_context *ctx = i->i_ctx;
176 mutex_lock(&ctx->mapping_lock);
178 ctx->local_store = NULL;
179 mutex_unlock(&ctx->mapping_lock);
184 spufs_mem_dump(struct spu_context *ctx, struct coredump_params *cprm)
186 return spufs_dump_emit(cprm, ctx->ops->get_ls(ctx), LS_SIZE);
190 spufs_mem_read(struct file *file, char __user *buffer,
191 size_t size, loff_t *pos)
193 struct spu_context *ctx = file->private_data;
196 ret = spu_acquire(ctx);
199 ret = simple_read_from_buffer(buffer, size, pos, ctx->ops->get_ls(ctx),
207 spufs_mem_write(struct file *file, const char __user *buffer,
208 size_t size, loff_t *ppos)
210 struct spu_context *ctx = file->private_data;
218 ret = spu_acquire(ctx);
222 local_store = ctx->ops->get_ls(ctx);
223 size = simple_write_to_buffer(local_store, LS_SIZE, ppos, buffer, size);
230 spufs_mem_mmap_fault(struct vm_fault *vmf)
232 struct vm_area_struct *vma = vmf->vma;
233 struct spu_context *ctx = vma->vm_file->private_data;
234 unsigned long pfn, offset;
237 offset = vmf->pgoff << PAGE_SHIFT;
238 if (offset >= LS_SIZE)
239 return VM_FAULT_SIGBUS;
241 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
242 vmf->address, offset);
244 if (spu_acquire(ctx))
245 return VM_FAULT_NOPAGE;
247 if (ctx->state == SPU_STATE_SAVED) {
248 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
249 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
251 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
252 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
254 ret = vmf_insert_pfn(vma, vmf->address, pfn);
261 static int spufs_mem_mmap_access(struct vm_area_struct *vma,
262 unsigned long address,
263 void *buf, int len, int write)
265 struct spu_context *ctx = vma->vm_file->private_data;
266 unsigned long offset = address - vma->vm_start;
269 if (write && !(vma->vm_flags & VM_WRITE))
271 if (spu_acquire(ctx))
273 if ((offset + len) > vma->vm_end)
274 len = vma->vm_end - offset;
275 local_store = ctx->ops->get_ls(ctx);
277 memcpy_toio(local_store + offset, buf, len);
279 memcpy_fromio(buf, local_store + offset, len);
284 static const struct vm_operations_struct spufs_mem_mmap_vmops = {
285 .fault = spufs_mem_mmap_fault,
286 .access = spufs_mem_mmap_access,
289 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
291 if (!(vma->vm_flags & VM_SHARED))
294 vm_flags_set(vma, VM_IO | VM_PFNMAP);
295 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
297 vma->vm_ops = &spufs_mem_mmap_vmops;
301 static const struct file_operations spufs_mem_fops = {
302 .open = spufs_mem_open,
303 .release = spufs_mem_release,
304 .read = spufs_mem_read,
305 .write = spufs_mem_write,
306 .llseek = generic_file_llseek,
307 .mmap = spufs_mem_mmap,
310 static vm_fault_t spufs_ps_fault(struct vm_fault *vmf,
311 unsigned long ps_offs,
312 unsigned long ps_size)
314 struct spu_context *ctx = vmf->vma->vm_file->private_data;
315 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
317 vm_fault_t ret = VM_FAULT_NOPAGE;
319 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
321 if (offset >= ps_size)
322 return VM_FAULT_SIGBUS;
324 if (fatal_signal_pending(current))
325 return VM_FAULT_SIGBUS;
328 * Because we release the mmap_lock, the context may be destroyed while
329 * we're in spu_wait. Grab an extra reference so it isn't destroyed
332 get_spu_context(ctx);
335 * We have to wait for context to be loaded before we have
336 * pages to hand out to the user, but we don't want to wait
337 * with the mmap_lock held.
338 * It is possible to drop the mmap_lock here, but then we need
339 * to return VM_FAULT_NOPAGE because the mappings may have
342 if (spu_acquire(ctx))
345 if (ctx->state == SPU_STATE_SAVED) {
346 mmap_read_unlock(current->mm);
347 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
348 err = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
349 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
350 mmap_read_lock(current->mm);
352 area = ctx->spu->problem_phys + ps_offs;
353 ret = vmf_insert_pfn(vmf->vma, vmf->address,
354 (area + offset) >> PAGE_SHIFT);
355 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
362 put_spu_context(ctx);
367 static vm_fault_t spufs_cntl_mmap_fault(struct vm_fault *vmf)
369 return spufs_ps_fault(vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
372 static const struct vm_operations_struct spufs_cntl_mmap_vmops = {
373 .fault = spufs_cntl_mmap_fault,
377 * mmap support for problem state control area [0x4000 - 0x4fff].
379 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
381 if (!(vma->vm_flags & VM_SHARED))
384 vm_flags_set(vma, VM_IO | VM_PFNMAP);
385 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
387 vma->vm_ops = &spufs_cntl_mmap_vmops;
390 #else /* SPUFS_MMAP_4K */
391 #define spufs_cntl_mmap NULL
392 #endif /* !SPUFS_MMAP_4K */
394 static int spufs_cntl_get(void *data, u64 *val)
396 struct spu_context *ctx = data;
399 ret = spu_acquire(ctx);
402 *val = ctx->ops->status_read(ctx);
408 static int spufs_cntl_set(void *data, u64 val)
410 struct spu_context *ctx = data;
413 ret = spu_acquire(ctx);
416 ctx->ops->runcntl_write(ctx, val);
422 static int spufs_cntl_open(struct inode *inode, struct file *file)
424 struct spufs_inode_info *i = SPUFS_I(inode);
425 struct spu_context *ctx = i->i_ctx;
427 mutex_lock(&ctx->mapping_lock);
428 file->private_data = ctx;
430 ctx->cntl = inode->i_mapping;
431 mutex_unlock(&ctx->mapping_lock);
432 return simple_attr_open(inode, file, spufs_cntl_get,
433 spufs_cntl_set, "0x%08lx");
437 spufs_cntl_release(struct inode *inode, struct file *file)
439 struct spufs_inode_info *i = SPUFS_I(inode);
440 struct spu_context *ctx = i->i_ctx;
442 simple_attr_release(inode, file);
444 mutex_lock(&ctx->mapping_lock);
447 mutex_unlock(&ctx->mapping_lock);
451 static const struct file_operations spufs_cntl_fops = {
452 .open = spufs_cntl_open,
453 .release = spufs_cntl_release,
454 .read = simple_attr_read,
455 .write = simple_attr_write,
457 .mmap = spufs_cntl_mmap,
461 spufs_regs_open(struct inode *inode, struct file *file)
463 struct spufs_inode_info *i = SPUFS_I(inode);
464 file->private_data = i->i_ctx;
469 spufs_regs_dump(struct spu_context *ctx, struct coredump_params *cprm)
471 return spufs_dump_emit(cprm, ctx->csa.lscsa->gprs,
472 sizeof(ctx->csa.lscsa->gprs));
476 spufs_regs_read(struct file *file, char __user *buffer,
477 size_t size, loff_t *pos)
480 struct spu_context *ctx = file->private_data;
482 /* pre-check for file position: if we'd return EOF, there's no point
483 * causing a deschedule */
484 if (*pos >= sizeof(ctx->csa.lscsa->gprs))
487 ret = spu_acquire_saved(ctx);
490 ret = simple_read_from_buffer(buffer, size, pos, ctx->csa.lscsa->gprs,
491 sizeof(ctx->csa.lscsa->gprs));
492 spu_release_saved(ctx);
497 spufs_regs_write(struct file *file, const char __user *buffer,
498 size_t size, loff_t *pos)
500 struct spu_context *ctx = file->private_data;
501 struct spu_lscsa *lscsa = ctx->csa.lscsa;
504 if (*pos >= sizeof(lscsa->gprs))
507 ret = spu_acquire_saved(ctx);
511 size = simple_write_to_buffer(lscsa->gprs, sizeof(lscsa->gprs), pos,
514 spu_release_saved(ctx);
518 static const struct file_operations spufs_regs_fops = {
519 .open = spufs_regs_open,
520 .read = spufs_regs_read,
521 .write = spufs_regs_write,
522 .llseek = generic_file_llseek,
526 spufs_fpcr_dump(struct spu_context *ctx, struct coredump_params *cprm)
528 return spufs_dump_emit(cprm, &ctx->csa.lscsa->fpcr,
529 sizeof(ctx->csa.lscsa->fpcr));
533 spufs_fpcr_read(struct file *file, char __user * buffer,
534 size_t size, loff_t * pos)
537 struct spu_context *ctx = file->private_data;
539 ret = spu_acquire_saved(ctx);
542 ret = simple_read_from_buffer(buffer, size, pos, &ctx->csa.lscsa->fpcr,
543 sizeof(ctx->csa.lscsa->fpcr));
544 spu_release_saved(ctx);
549 spufs_fpcr_write(struct file *file, const char __user * buffer,
550 size_t size, loff_t * pos)
552 struct spu_context *ctx = file->private_data;
553 struct spu_lscsa *lscsa = ctx->csa.lscsa;
556 if (*pos >= sizeof(lscsa->fpcr))
559 ret = spu_acquire_saved(ctx);
563 size = simple_write_to_buffer(&lscsa->fpcr, sizeof(lscsa->fpcr), pos,
566 spu_release_saved(ctx);
570 static const struct file_operations spufs_fpcr_fops = {
571 .open = spufs_regs_open,
572 .read = spufs_fpcr_read,
573 .write = spufs_fpcr_write,
574 .llseek = generic_file_llseek,
577 /* generic open function for all pipe-like files */
578 static int spufs_pipe_open(struct inode *inode, struct file *file)
580 struct spufs_inode_info *i = SPUFS_I(inode);
581 file->private_data = i->i_ctx;
583 return stream_open(inode, file);
587 * Read as many bytes from the mailbox as possible, until
588 * one of the conditions becomes true:
590 * - no more data available in the mailbox
591 * - end of the user provided buffer
592 * - end of the mapped area
594 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
595 size_t len, loff_t *pos)
597 struct spu_context *ctx = file->private_data;
598 u32 mbox_data, __user *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 = (void __user *)buf;
707 count = spu_acquire(ctx);
711 /* wait only for the first element */
713 if (file->f_flags & O_NONBLOCK) {
714 if (!spu_ibox_read(ctx, &ibox_data)) {
719 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
724 /* if we can't write at all, return -EFAULT */
725 count = put_user(ibox_data, udata);
729 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
731 ret = ctx->ops->ibox_read(ctx, &ibox_data);
735 * at the end of the mapped area, we can fault
736 * but still need to return the data we have
737 * read successfully so far.
739 ret = put_user(ibox_data, udata);
750 static __poll_t spufs_ibox_poll(struct file *file, poll_table *wait)
752 struct spu_context *ctx = file->private_data;
755 poll_wait(file, &ctx->ibox_wq, wait);
758 * For now keep this uninterruptible and also ignore the rule
759 * that poll should not sleep. Will be fixed later.
761 mutex_lock(&ctx->state_mutex);
762 mask = ctx->ops->mbox_stat_poll(ctx, EPOLLIN | EPOLLRDNORM);
768 static const struct file_operations spufs_ibox_fops = {
769 .open = spufs_pipe_open,
770 .read = spufs_ibox_read,
771 .poll = spufs_ibox_poll,
775 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
776 size_t len, loff_t *pos)
778 struct spu_context *ctx = file->private_data;
785 ret = spu_acquire(ctx);
788 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
791 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
797 static const struct file_operations spufs_ibox_stat_fops = {
798 .open = spufs_pipe_open,
799 .read = spufs_ibox_stat_read,
803 /* low-level mailbox write */
804 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
806 return ctx->ops->wbox_write(ctx, data);
809 /* interrupt-level wbox callback function. */
810 void spufs_wbox_callback(struct spu *spu)
812 struct spu_context *ctx = spu->ctx;
815 wake_up_all(&ctx->wbox_wq);
819 * Write as many bytes to the interrupt mailbox as possible, until
820 * one of the conditions becomes true:
822 * - the mailbox is full
823 * - end of the user provided buffer
824 * - end of the mapped area
826 * If the file is opened without O_NONBLOCK, we wait here until
827 * space is available, but return when we have been able to
830 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
831 size_t len, loff_t *pos)
833 struct spu_context *ctx = file->private_data;
834 u32 wbox_data, __user *udata = (void __user *)buf;
840 if (get_user(wbox_data, udata))
843 count = spu_acquire(ctx);
848 * make sure we can at least write one element, by waiting
849 * in case of !O_NONBLOCK
852 if (file->f_flags & O_NONBLOCK) {
853 if (!spu_wbox_write(ctx, wbox_data)) {
858 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
864 /* write as much as possible */
865 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
867 ret = get_user(wbox_data, udata);
871 ret = spu_wbox_write(ctx, wbox_data);
882 static __poll_t spufs_wbox_poll(struct file *file, poll_table *wait)
884 struct spu_context *ctx = file->private_data;
887 poll_wait(file, &ctx->wbox_wq, wait);
890 * For now keep this uninterruptible and also ignore the rule
891 * that poll should not sleep. Will be fixed later.
893 mutex_lock(&ctx->state_mutex);
894 mask = ctx->ops->mbox_stat_poll(ctx, EPOLLOUT | EPOLLWRNORM);
900 static const struct file_operations spufs_wbox_fops = {
901 .open = spufs_pipe_open,
902 .write = spufs_wbox_write,
903 .poll = spufs_wbox_poll,
907 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
908 size_t len, loff_t *pos)
910 struct spu_context *ctx = file->private_data;
917 ret = spu_acquire(ctx);
920 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
923 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
929 static const struct file_operations spufs_wbox_stat_fops = {
930 .open = spufs_pipe_open,
931 .read = spufs_wbox_stat_read,
935 static int spufs_signal1_open(struct inode *inode, struct file *file)
937 struct spufs_inode_info *i = SPUFS_I(inode);
938 struct spu_context *ctx = i->i_ctx;
940 mutex_lock(&ctx->mapping_lock);
941 file->private_data = ctx;
943 ctx->signal1 = inode->i_mapping;
944 mutex_unlock(&ctx->mapping_lock);
945 return nonseekable_open(inode, file);
949 spufs_signal1_release(struct inode *inode, struct file *file)
951 struct spufs_inode_info *i = SPUFS_I(inode);
952 struct spu_context *ctx = i->i_ctx;
954 mutex_lock(&ctx->mapping_lock);
957 mutex_unlock(&ctx->mapping_lock);
961 static ssize_t spufs_signal1_dump(struct spu_context *ctx,
962 struct coredump_params *cprm)
964 if (!ctx->csa.spu_chnlcnt_RW[3])
966 return spufs_dump_emit(cprm, &ctx->csa.spu_chnldata_RW[3],
967 sizeof(ctx->csa.spu_chnldata_RW[3]));
970 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
973 if (len < sizeof(ctx->csa.spu_chnldata_RW[3]))
975 if (!ctx->csa.spu_chnlcnt_RW[3])
977 if (copy_to_user(buf, &ctx->csa.spu_chnldata_RW[3],
978 sizeof(ctx->csa.spu_chnldata_RW[3])))
980 return sizeof(ctx->csa.spu_chnldata_RW[3]);
983 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
984 size_t len, loff_t *pos)
987 struct spu_context *ctx = file->private_data;
989 ret = spu_acquire_saved(ctx);
992 ret = __spufs_signal1_read(ctx, buf, len);
993 spu_release_saved(ctx);
998 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
999 size_t len, loff_t *pos)
1001 struct spu_context *ctx;
1005 ctx = file->private_data;
1010 if (copy_from_user(&data, buf, 4))
1013 ret = spu_acquire(ctx);
1016 ctx->ops->signal1_write(ctx, data);
1023 spufs_signal1_mmap_fault(struct vm_fault *vmf)
1025 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1026 return spufs_ps_fault(vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1027 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
1028 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1029 * signal 1 and 2 area
1031 return spufs_ps_fault(vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
1033 #error unsupported page size
1037 static const struct vm_operations_struct spufs_signal1_mmap_vmops = {
1038 .fault = spufs_signal1_mmap_fault,
1041 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1043 if (!(vma->vm_flags & VM_SHARED))
1046 vm_flags_set(vma, VM_IO | VM_PFNMAP);
1047 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1049 vma->vm_ops = &spufs_signal1_mmap_vmops;
1053 static const struct file_operations spufs_signal1_fops = {
1054 .open = spufs_signal1_open,
1055 .release = spufs_signal1_release,
1056 .read = spufs_signal1_read,
1057 .write = spufs_signal1_write,
1058 .mmap = spufs_signal1_mmap,
1059 .llseek = no_llseek,
1062 static const struct file_operations spufs_signal1_nosched_fops = {
1063 .open = spufs_signal1_open,
1064 .release = spufs_signal1_release,
1065 .write = spufs_signal1_write,
1066 .mmap = spufs_signal1_mmap,
1067 .llseek = no_llseek,
1070 static int spufs_signal2_open(struct inode *inode, struct file *file)
1072 struct spufs_inode_info *i = SPUFS_I(inode);
1073 struct spu_context *ctx = i->i_ctx;
1075 mutex_lock(&ctx->mapping_lock);
1076 file->private_data = ctx;
1077 if (!i->i_openers++)
1078 ctx->signal2 = inode->i_mapping;
1079 mutex_unlock(&ctx->mapping_lock);
1080 return nonseekable_open(inode, file);
1084 spufs_signal2_release(struct inode *inode, struct file *file)
1086 struct spufs_inode_info *i = SPUFS_I(inode);
1087 struct spu_context *ctx = i->i_ctx;
1089 mutex_lock(&ctx->mapping_lock);
1090 if (!--i->i_openers)
1091 ctx->signal2 = NULL;
1092 mutex_unlock(&ctx->mapping_lock);
1096 static ssize_t spufs_signal2_dump(struct spu_context *ctx,
1097 struct coredump_params *cprm)
1099 if (!ctx->csa.spu_chnlcnt_RW[4])
1101 return spufs_dump_emit(cprm, &ctx->csa.spu_chnldata_RW[4],
1102 sizeof(ctx->csa.spu_chnldata_RW[4]));
1105 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
1108 if (len < sizeof(ctx->csa.spu_chnldata_RW[4]))
1110 if (!ctx->csa.spu_chnlcnt_RW[4])
1112 if (copy_to_user(buf, &ctx->csa.spu_chnldata_RW[4],
1113 sizeof(ctx->csa.spu_chnldata_RW[4])))
1115 return sizeof(ctx->csa.spu_chnldata_RW[4]);
1118 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1119 size_t len, loff_t *pos)
1121 struct spu_context *ctx = file->private_data;
1124 ret = spu_acquire_saved(ctx);
1127 ret = __spufs_signal2_read(ctx, buf, len);
1128 spu_release_saved(ctx);
1133 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1134 size_t len, loff_t *pos)
1136 struct spu_context *ctx;
1140 ctx = file->private_data;
1145 if (copy_from_user(&data, buf, 4))
1148 ret = spu_acquire(ctx);
1151 ctx->ops->signal2_write(ctx, data);
1159 spufs_signal2_mmap_fault(struct vm_fault *vmf)
1161 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1162 return spufs_ps_fault(vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1163 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
1164 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1165 * signal 1 and 2 area
1167 return spufs_ps_fault(vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
1169 #error unsupported page size
1173 static const struct vm_operations_struct spufs_signal2_mmap_vmops = {
1174 .fault = spufs_signal2_mmap_fault,
1177 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1179 if (!(vma->vm_flags & VM_SHARED))
1182 vm_flags_set(vma, VM_IO | VM_PFNMAP);
1183 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1185 vma->vm_ops = &spufs_signal2_mmap_vmops;
1188 #else /* SPUFS_MMAP_4K */
1189 #define spufs_signal2_mmap NULL
1190 #endif /* !SPUFS_MMAP_4K */
1192 static const struct file_operations spufs_signal2_fops = {
1193 .open = spufs_signal2_open,
1194 .release = spufs_signal2_release,
1195 .read = spufs_signal2_read,
1196 .write = spufs_signal2_write,
1197 .mmap = spufs_signal2_mmap,
1198 .llseek = no_llseek,
1201 static const struct file_operations spufs_signal2_nosched_fops = {
1202 .open = spufs_signal2_open,
1203 .release = spufs_signal2_release,
1204 .write = spufs_signal2_write,
1205 .mmap = spufs_signal2_mmap,
1206 .llseek = no_llseek,
1210 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1211 * work of acquiring (or not) the SPU context before calling through
1212 * to the actual get routine. The set routine is called directly.
1214 #define SPU_ATTR_NOACQUIRE 0
1215 #define SPU_ATTR_ACQUIRE 1
1216 #define SPU_ATTR_ACQUIRE_SAVED 2
1218 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
1219 static int __##__get(void *data, u64 *val) \
1221 struct spu_context *ctx = data; \
1224 if (__acquire == SPU_ATTR_ACQUIRE) { \
1225 ret = spu_acquire(ctx); \
1228 *val = __get(ctx); \
1230 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1231 ret = spu_acquire_saved(ctx); \
1234 *val = __get(ctx); \
1235 spu_release_saved(ctx); \
1237 *val = __get(ctx); \
1241 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1243 static int spufs_signal1_type_set(void *data, u64 val)
1245 struct spu_context *ctx = data;
1248 ret = spu_acquire(ctx);
1251 ctx->ops->signal1_type_set(ctx, val);
1257 static u64 spufs_signal1_type_get(struct spu_context *ctx)
1259 return ctx->ops->signal1_type_get(ctx);
1261 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1262 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1265 static int spufs_signal2_type_set(void *data, u64 val)
1267 struct spu_context *ctx = data;
1270 ret = spu_acquire(ctx);
1273 ctx->ops->signal2_type_set(ctx, val);
1279 static u64 spufs_signal2_type_get(struct spu_context *ctx)
1281 return ctx->ops->signal2_type_get(ctx);
1283 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1284 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1288 spufs_mss_mmap_fault(struct vm_fault *vmf)
1290 return spufs_ps_fault(vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
1293 static const struct vm_operations_struct spufs_mss_mmap_vmops = {
1294 .fault = spufs_mss_mmap_fault,
1298 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1300 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1302 if (!(vma->vm_flags & VM_SHARED))
1305 vm_flags_set(vma, VM_IO | VM_PFNMAP);
1306 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1308 vma->vm_ops = &spufs_mss_mmap_vmops;
1311 #else /* SPUFS_MMAP_4K */
1312 #define spufs_mss_mmap NULL
1313 #endif /* !SPUFS_MMAP_4K */
1315 static int spufs_mss_open(struct inode *inode, struct file *file)
1317 struct spufs_inode_info *i = SPUFS_I(inode);
1318 struct spu_context *ctx = i->i_ctx;
1320 file->private_data = i->i_ctx;
1322 mutex_lock(&ctx->mapping_lock);
1323 if (!i->i_openers++)
1324 ctx->mss = inode->i_mapping;
1325 mutex_unlock(&ctx->mapping_lock);
1326 return nonseekable_open(inode, file);
1330 spufs_mss_release(struct inode *inode, struct file *file)
1332 struct spufs_inode_info *i = SPUFS_I(inode);
1333 struct spu_context *ctx = i->i_ctx;
1335 mutex_lock(&ctx->mapping_lock);
1336 if (!--i->i_openers)
1338 mutex_unlock(&ctx->mapping_lock);
1342 static const struct file_operations spufs_mss_fops = {
1343 .open = spufs_mss_open,
1344 .release = spufs_mss_release,
1345 .mmap = spufs_mss_mmap,
1346 .llseek = no_llseek,
1350 spufs_psmap_mmap_fault(struct vm_fault *vmf)
1352 return spufs_ps_fault(vmf, 0x0000, SPUFS_PS_MAP_SIZE);
1355 static const struct vm_operations_struct spufs_psmap_mmap_vmops = {
1356 .fault = spufs_psmap_mmap_fault,
1360 * mmap support for full problem state area [0x00000 - 0x1ffff].
1362 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1364 if (!(vma->vm_flags & VM_SHARED))
1367 vm_flags_set(vma, VM_IO | VM_PFNMAP);
1368 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1370 vma->vm_ops = &spufs_psmap_mmap_vmops;
1374 static int spufs_psmap_open(struct inode *inode, struct file *file)
1376 struct spufs_inode_info *i = SPUFS_I(inode);
1377 struct spu_context *ctx = i->i_ctx;
1379 mutex_lock(&ctx->mapping_lock);
1380 file->private_data = i->i_ctx;
1381 if (!i->i_openers++)
1382 ctx->psmap = inode->i_mapping;
1383 mutex_unlock(&ctx->mapping_lock);
1384 return nonseekable_open(inode, file);
1388 spufs_psmap_release(struct inode *inode, struct file *file)
1390 struct spufs_inode_info *i = SPUFS_I(inode);
1391 struct spu_context *ctx = i->i_ctx;
1393 mutex_lock(&ctx->mapping_lock);
1394 if (!--i->i_openers)
1396 mutex_unlock(&ctx->mapping_lock);
1400 static const struct file_operations spufs_psmap_fops = {
1401 .open = spufs_psmap_open,
1402 .release = spufs_psmap_release,
1403 .mmap = spufs_psmap_mmap,
1404 .llseek = no_llseek,
1410 spufs_mfc_mmap_fault(struct vm_fault *vmf)
1412 return spufs_ps_fault(vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
1415 static const struct vm_operations_struct spufs_mfc_mmap_vmops = {
1416 .fault = spufs_mfc_mmap_fault,
1420 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1422 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1424 if (!(vma->vm_flags & VM_SHARED))
1427 vm_flags_set(vma, VM_IO | VM_PFNMAP);
1428 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1430 vma->vm_ops = &spufs_mfc_mmap_vmops;
1433 #else /* SPUFS_MMAP_4K */
1434 #define spufs_mfc_mmap NULL
1435 #endif /* !SPUFS_MMAP_4K */
1437 static int spufs_mfc_open(struct inode *inode, struct file *file)
1439 struct spufs_inode_info *i = SPUFS_I(inode);
1440 struct spu_context *ctx = i->i_ctx;
1442 /* we don't want to deal with DMA into other processes */
1443 if (ctx->owner != current->mm)
1446 if (atomic_read(&inode->i_count) != 1)
1449 mutex_lock(&ctx->mapping_lock);
1450 file->private_data = ctx;
1451 if (!i->i_openers++)
1452 ctx->mfc = inode->i_mapping;
1453 mutex_unlock(&ctx->mapping_lock);
1454 return nonseekable_open(inode, file);
1458 spufs_mfc_release(struct inode *inode, struct file *file)
1460 struct spufs_inode_info *i = SPUFS_I(inode);
1461 struct spu_context *ctx = i->i_ctx;
1463 mutex_lock(&ctx->mapping_lock);
1464 if (!--i->i_openers)
1466 mutex_unlock(&ctx->mapping_lock);
1470 /* interrupt-level mfc callback function. */
1471 void spufs_mfc_callback(struct spu *spu)
1473 struct spu_context *ctx = spu->ctx;
1476 wake_up_all(&ctx->mfc_wq);
1479 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1481 /* See if there is one tag group is complete */
1482 /* FIXME we need locking around tagwait */
1483 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1484 ctx->tagwait &= ~*status;
1488 /* enable interrupt waiting for any tag group,
1489 may silently fail if interrupts are already enabled */
1490 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1494 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1495 size_t size, loff_t *pos)
1497 struct spu_context *ctx = file->private_data;
1504 ret = spu_acquire(ctx);
1509 if (file->f_flags & O_NONBLOCK) {
1510 status = ctx->ops->read_mfc_tagstatus(ctx);
1511 if (!(status & ctx->tagwait))
1514 /* XXX(hch): shouldn't we clear ret here? */
1515 ctx->tagwait &= ~status;
1517 ret = spufs_wait(ctx->mfc_wq,
1518 spufs_read_mfc_tagstatus(ctx, &status));
1525 if (copy_to_user(buffer, &status, 4))
1532 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1534 pr_debug("queueing DMA %x %llx %x %x %x\n", cmd->lsa,
1535 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1546 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1550 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1551 pr_debug("invalid DMA alignment, ea %llx lsa %x\n",
1556 switch (cmd->size & 0xf) {
1577 pr_debug("invalid DMA alignment %x for size %x\n",
1578 cmd->lsa & 0xf, cmd->size);
1582 if (cmd->size > 16 * 1024) {
1583 pr_debug("invalid DMA size %x\n", cmd->size);
1587 if (cmd->tag & 0xfff0) {
1588 /* we reserve the higher tag numbers for kernel use */
1589 pr_debug("invalid DMA tag\n");
1594 /* not supported in this version */
1595 pr_debug("invalid DMA class\n");
1602 static int spu_send_mfc_command(struct spu_context *ctx,
1603 struct mfc_dma_command cmd,
1606 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1607 if (*error == -EAGAIN) {
1608 /* wait for any tag group to complete
1609 so we have space for the new command */
1610 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1611 /* try again, because the queue might be
1613 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1614 if (*error == -EAGAIN)
1620 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1621 size_t size, loff_t *pos)
1623 struct spu_context *ctx = file->private_data;
1624 struct mfc_dma_command cmd;
1627 if (size != sizeof cmd)
1631 if (copy_from_user(&cmd, buffer, sizeof cmd))
1634 ret = spufs_check_valid_dma(&cmd);
1638 ret = spu_acquire(ctx);
1642 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1646 if (file->f_flags & O_NONBLOCK) {
1647 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1650 ret = spufs_wait(ctx->mfc_wq,
1651 spu_send_mfc_command(ctx, cmd, &status));
1661 ctx->tagwait |= 1 << cmd.tag;
1670 static __poll_t spufs_mfc_poll(struct file *file,poll_table *wait)
1672 struct spu_context *ctx = file->private_data;
1673 u32 free_elements, tagstatus;
1676 poll_wait(file, &ctx->mfc_wq, wait);
1679 * For now keep this uninterruptible and also ignore the rule
1680 * that poll should not sleep. Will be fixed later.
1682 mutex_lock(&ctx->state_mutex);
1683 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1684 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1685 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1689 if (free_elements & 0xffff)
1690 mask |= EPOLLOUT | EPOLLWRNORM;
1691 if (tagstatus & ctx->tagwait)
1692 mask |= EPOLLIN | EPOLLRDNORM;
1694 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
1695 free_elements, tagstatus, ctx->tagwait);
1700 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1702 struct spu_context *ctx = file->private_data;
1705 ret = spu_acquire(ctx);
1709 /* this currently hangs */
1710 ret = spufs_wait(ctx->mfc_wq,
1711 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1714 ret = spufs_wait(ctx->mfc_wq,
1715 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1726 static int spufs_mfc_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1728 struct inode *inode = file_inode(file);
1729 int err = file_write_and_wait_range(file, start, end);
1732 err = spufs_mfc_flush(file, NULL);
1733 inode_unlock(inode);
1738 static const struct file_operations spufs_mfc_fops = {
1739 .open = spufs_mfc_open,
1740 .release = spufs_mfc_release,
1741 .read = spufs_mfc_read,
1742 .write = spufs_mfc_write,
1743 .poll = spufs_mfc_poll,
1744 .flush = spufs_mfc_flush,
1745 .fsync = spufs_mfc_fsync,
1746 .mmap = spufs_mfc_mmap,
1747 .llseek = no_llseek,
1750 static int spufs_npc_set(void *data, u64 val)
1752 struct spu_context *ctx = data;
1755 ret = spu_acquire(ctx);
1758 ctx->ops->npc_write(ctx, val);
1764 static u64 spufs_npc_get(struct spu_context *ctx)
1766 return ctx->ops->npc_read(ctx);
1768 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1769 "0x%llx\n", SPU_ATTR_ACQUIRE);
1771 static int spufs_decr_set(void *data, u64 val)
1773 struct spu_context *ctx = data;
1774 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1777 ret = spu_acquire_saved(ctx);
1780 lscsa->decr.slot[0] = (u32) val;
1781 spu_release_saved(ctx);
1786 static u64 spufs_decr_get(struct spu_context *ctx)
1788 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1789 return lscsa->decr.slot[0];
1791 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1792 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
1794 static int spufs_decr_status_set(void *data, u64 val)
1796 struct spu_context *ctx = data;
1799 ret = spu_acquire_saved(ctx);
1803 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1805 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1806 spu_release_saved(ctx);
1811 static u64 spufs_decr_status_get(struct spu_context *ctx)
1813 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1814 return SPU_DECR_STATUS_RUNNING;
1818 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1819 spufs_decr_status_set, "0x%llx\n",
1820 SPU_ATTR_ACQUIRE_SAVED);
1822 static int spufs_event_mask_set(void *data, u64 val)
1824 struct spu_context *ctx = data;
1825 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1828 ret = spu_acquire_saved(ctx);
1831 lscsa->event_mask.slot[0] = (u32) val;
1832 spu_release_saved(ctx);
1837 static u64 spufs_event_mask_get(struct spu_context *ctx)
1839 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1840 return lscsa->event_mask.slot[0];
1843 DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1844 spufs_event_mask_set, "0x%llx\n",
1845 SPU_ATTR_ACQUIRE_SAVED);
1847 static u64 spufs_event_status_get(struct spu_context *ctx)
1849 struct spu_state *state = &ctx->csa;
1851 stat = state->spu_chnlcnt_RW[0];
1853 return state->spu_chnldata_RW[0];
1856 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1857 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1859 static int spufs_srr0_set(void *data, u64 val)
1861 struct spu_context *ctx = data;
1862 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1865 ret = spu_acquire_saved(ctx);
1868 lscsa->srr0.slot[0] = (u32) val;
1869 spu_release_saved(ctx);
1874 static u64 spufs_srr0_get(struct spu_context *ctx)
1876 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1877 return lscsa->srr0.slot[0];
1879 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1880 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1882 static u64 spufs_id_get(struct spu_context *ctx)
1886 if (ctx->state == SPU_STATE_RUNNABLE)
1887 num = ctx->spu->number;
1889 num = (unsigned int)-1;
1893 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1896 static u64 spufs_object_id_get(struct spu_context *ctx)
1898 /* FIXME: Should there really be no locking here? */
1899 return ctx->object_id;
1902 static int spufs_object_id_set(void *data, u64 id)
1904 struct spu_context *ctx = data;
1905 ctx->object_id = id;
1910 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1911 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
1913 static u64 spufs_lslr_get(struct spu_context *ctx)
1915 return ctx->csa.priv2.spu_lslr_RW;
1917 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1918 SPU_ATTR_ACQUIRE_SAVED);
1920 static int spufs_info_open(struct inode *inode, struct file *file)
1922 struct spufs_inode_info *i = SPUFS_I(inode);
1923 struct spu_context *ctx = i->i_ctx;
1924 file->private_data = ctx;
1928 static int spufs_caps_show(struct seq_file *s, void *private)
1930 struct spu_context *ctx = s->private;
1932 if (!(ctx->flags & SPU_CREATE_NOSCHED))
1933 seq_puts(s, "sched\n");
1934 if (!(ctx->flags & SPU_CREATE_ISOLATE))
1935 seq_puts(s, "step\n");
1939 static int spufs_caps_open(struct inode *inode, struct file *file)
1941 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1944 static const struct file_operations spufs_caps_fops = {
1945 .open = spufs_caps_open,
1947 .llseek = seq_lseek,
1948 .release = single_release,
1951 static ssize_t spufs_mbox_info_dump(struct spu_context *ctx,
1952 struct coredump_params *cprm)
1954 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
1956 return spufs_dump_emit(cprm, &ctx->csa.prob.pu_mb_R,
1957 sizeof(ctx->csa.prob.pu_mb_R));
1960 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1961 size_t len, loff_t *pos)
1963 struct spu_context *ctx = file->private_data;
1967 ret = spu_acquire_saved(ctx);
1970 spin_lock(&ctx->csa.register_lock);
1971 stat = ctx->csa.prob.mb_stat_R;
1972 data = ctx->csa.prob.pu_mb_R;
1973 spin_unlock(&ctx->csa.register_lock);
1974 spu_release_saved(ctx);
1976 /* EOF if there's no entry in the mbox */
1977 if (!(stat & 0x0000ff))
1980 return simple_read_from_buffer(buf, len, pos, &data, sizeof(data));
1983 static const struct file_operations spufs_mbox_info_fops = {
1984 .open = spufs_info_open,
1985 .read = spufs_mbox_info_read,
1986 .llseek = generic_file_llseek,
1989 static ssize_t spufs_ibox_info_dump(struct spu_context *ctx,
1990 struct coredump_params *cprm)
1992 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
1994 return spufs_dump_emit(cprm, &ctx->csa.priv2.puint_mb_R,
1995 sizeof(ctx->csa.priv2.puint_mb_R));
1998 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
1999 size_t len, loff_t *pos)
2001 struct spu_context *ctx = file->private_data;
2005 ret = spu_acquire_saved(ctx);
2008 spin_lock(&ctx->csa.register_lock);
2009 stat = ctx->csa.prob.mb_stat_R;
2010 data = ctx->csa.priv2.puint_mb_R;
2011 spin_unlock(&ctx->csa.register_lock);
2012 spu_release_saved(ctx);
2014 /* EOF if there's no entry in the ibox */
2015 if (!(stat & 0xff0000))
2018 return simple_read_from_buffer(buf, len, pos, &data, sizeof(data));
2021 static const struct file_operations spufs_ibox_info_fops = {
2022 .open = spufs_info_open,
2023 .read = spufs_ibox_info_read,
2024 .llseek = generic_file_llseek,
2027 static size_t spufs_wbox_info_cnt(struct spu_context *ctx)
2029 return (4 - ((ctx->csa.prob.mb_stat_R & 0x00ff00) >> 8)) * sizeof(u32);
2032 static ssize_t spufs_wbox_info_dump(struct spu_context *ctx,
2033 struct coredump_params *cprm)
2035 return spufs_dump_emit(cprm, &ctx->csa.spu_mailbox_data,
2036 spufs_wbox_info_cnt(ctx));
2039 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2040 size_t len, loff_t *pos)
2042 struct spu_context *ctx = file->private_data;
2043 u32 data[ARRAY_SIZE(ctx->csa.spu_mailbox_data)];
2046 ret = spu_acquire_saved(ctx);
2049 spin_lock(&ctx->csa.register_lock);
2050 count = spufs_wbox_info_cnt(ctx);
2051 memcpy(&data, &ctx->csa.spu_mailbox_data, sizeof(data));
2052 spin_unlock(&ctx->csa.register_lock);
2053 spu_release_saved(ctx);
2055 return simple_read_from_buffer(buf, len, pos, &data,
2056 count * sizeof(u32));
2059 static const struct file_operations spufs_wbox_info_fops = {
2060 .open = spufs_info_open,
2061 .read = spufs_wbox_info_read,
2062 .llseek = generic_file_llseek,
2065 static void spufs_get_dma_info(struct spu_context *ctx,
2066 struct spu_dma_info *info)
2070 info->dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2071 info->dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2072 info->dma_info_status = ctx->csa.spu_chnldata_RW[24];
2073 info->dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2074 info->dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2075 for (i = 0; i < 16; i++) {
2076 struct mfc_cq_sr *qp = &info->dma_info_command_data[i];
2077 struct mfc_cq_sr *spuqp = &ctx->csa.priv2.spuq[i];
2079 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2080 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2081 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2082 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2086 static ssize_t spufs_dma_info_dump(struct spu_context *ctx,
2087 struct coredump_params *cprm)
2089 struct spu_dma_info info;
2091 spufs_get_dma_info(ctx, &info);
2092 return spufs_dump_emit(cprm, &info, sizeof(info));
2095 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2096 size_t len, loff_t *pos)
2098 struct spu_context *ctx = file->private_data;
2099 struct spu_dma_info info;
2102 ret = spu_acquire_saved(ctx);
2105 spin_lock(&ctx->csa.register_lock);
2106 spufs_get_dma_info(ctx, &info);
2107 spin_unlock(&ctx->csa.register_lock);
2108 spu_release_saved(ctx);
2110 return simple_read_from_buffer(buf, len, pos, &info,
2114 static const struct file_operations spufs_dma_info_fops = {
2115 .open = spufs_info_open,
2116 .read = spufs_dma_info_read,
2117 .llseek = no_llseek,
2120 static void spufs_get_proxydma_info(struct spu_context *ctx,
2121 struct spu_proxydma_info *info)
2125 info->proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2126 info->proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2127 info->proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2129 for (i = 0; i < 8; i++) {
2130 struct mfc_cq_sr *qp = &info->proxydma_info_command_data[i];
2131 struct mfc_cq_sr *puqp = &ctx->csa.priv2.puq[i];
2133 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2134 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2135 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2136 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2140 static ssize_t spufs_proxydma_info_dump(struct spu_context *ctx,
2141 struct coredump_params *cprm)
2143 struct spu_proxydma_info info;
2145 spufs_get_proxydma_info(ctx, &info);
2146 return spufs_dump_emit(cprm, &info, sizeof(info));
2149 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2150 size_t len, loff_t *pos)
2152 struct spu_context *ctx = file->private_data;
2153 struct spu_proxydma_info info;
2156 if (len < sizeof(info))
2159 ret = spu_acquire_saved(ctx);
2162 spin_lock(&ctx->csa.register_lock);
2163 spufs_get_proxydma_info(ctx, &info);
2164 spin_unlock(&ctx->csa.register_lock);
2165 spu_release_saved(ctx);
2167 return simple_read_from_buffer(buf, len, pos, &info,
2171 static const struct file_operations spufs_proxydma_info_fops = {
2172 .open = spufs_info_open,
2173 .read = spufs_proxydma_info_read,
2174 .llseek = no_llseek,
2177 static int spufs_show_tid(struct seq_file *s, void *private)
2179 struct spu_context *ctx = s->private;
2181 seq_printf(s, "%d\n", ctx->tid);
2185 static int spufs_tid_open(struct inode *inode, struct file *file)
2187 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2190 static const struct file_operations spufs_tid_fops = {
2191 .open = spufs_tid_open,
2193 .llseek = seq_lseek,
2194 .release = single_release,
2197 static const char *ctx_state_names[] = {
2198 "user", "system", "iowait", "loaded"
2201 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2202 enum spu_utilization_state state)
2204 unsigned long long time = ctx->stats.times[state];
2207 * In general, utilization statistics are updated by the controlling
2208 * thread as the spu context moves through various well defined
2209 * state transitions, but if the context is lazily loaded its
2210 * utilization statistics are not updated as the controlling thread
2211 * is not tightly coupled with the execution of the spu context. We
2212 * calculate and apply the time delta from the last recorded state
2213 * of the spu context.
2215 if (ctx->spu && ctx->stats.util_state == state) {
2216 time += ktime_get_ns() - ctx->stats.tstamp;
2219 return time / NSEC_PER_MSEC;
2222 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2224 unsigned long long slb_flts = ctx->stats.slb_flt;
2226 if (ctx->state == SPU_STATE_RUNNABLE) {
2227 slb_flts += (ctx->spu->stats.slb_flt -
2228 ctx->stats.slb_flt_base);
2234 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2236 unsigned long long class2_intrs = ctx->stats.class2_intr;
2238 if (ctx->state == SPU_STATE_RUNNABLE) {
2239 class2_intrs += (ctx->spu->stats.class2_intr -
2240 ctx->stats.class2_intr_base);
2243 return class2_intrs;
2247 static int spufs_show_stat(struct seq_file *s, void *private)
2249 struct spu_context *ctx = s->private;
2252 ret = spu_acquire(ctx);
2256 seq_printf(s, "%s %llu %llu %llu %llu "
2257 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2258 ctx_state_names[ctx->stats.util_state],
2259 spufs_acct_time(ctx, SPU_UTIL_USER),
2260 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2261 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2262 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2263 ctx->stats.vol_ctx_switch,
2264 ctx->stats.invol_ctx_switch,
2265 spufs_slb_flts(ctx),
2266 ctx->stats.hash_flt,
2269 spufs_class2_intrs(ctx),
2270 ctx->stats.libassist);
2275 static int spufs_stat_open(struct inode *inode, struct file *file)
2277 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2280 static const struct file_operations spufs_stat_fops = {
2281 .open = spufs_stat_open,
2283 .llseek = seq_lseek,
2284 .release = single_release,
2287 static inline int spufs_switch_log_used(struct spu_context *ctx)
2289 return (ctx->switch_log->head - ctx->switch_log->tail) %
2293 static inline int spufs_switch_log_avail(struct spu_context *ctx)
2295 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2298 static int spufs_switch_log_open(struct inode *inode, struct file *file)
2300 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2303 rc = spu_acquire(ctx);
2307 if (ctx->switch_log) {
2312 ctx->switch_log = kmalloc(struct_size(ctx->switch_log, log,
2313 SWITCH_LOG_BUFSIZE), GFP_KERNEL);
2315 if (!ctx->switch_log) {
2320 ctx->switch_log->head = ctx->switch_log->tail = 0;
2321 init_waitqueue_head(&ctx->switch_log->wait);
2329 static int spufs_switch_log_release(struct inode *inode, struct file *file)
2331 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2334 rc = spu_acquire(ctx);
2338 kfree(ctx->switch_log);
2339 ctx->switch_log = NULL;
2345 static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2347 struct switch_log_entry *p;
2349 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2351 return snprintf(tbuf, n, "%llu.%09u %d %u %u %llu\n",
2352 (unsigned long long) p->tstamp.tv_sec,
2353 (unsigned int) p->tstamp.tv_nsec,
2355 (unsigned int) p->type,
2356 (unsigned int) p->val,
2357 (unsigned long long) p->timebase);
2360 static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2361 size_t len, loff_t *ppos)
2363 struct inode *inode = file_inode(file);
2364 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2365 int error = 0, cnt = 0;
2370 error = spu_acquire(ctx);
2378 if (spufs_switch_log_used(ctx) == 0) {
2380 /* If there's data ready to go, we can
2381 * just return straight away */
2384 } else if (file->f_flags & O_NONBLOCK) {
2389 /* spufs_wait will drop the mutex and
2390 * re-acquire, but since we're in read(), the
2391 * file cannot be _released (and so
2392 * ctx->switch_log is stable).
2394 error = spufs_wait(ctx->switch_log->wait,
2395 spufs_switch_log_used(ctx) > 0);
2397 /* On error, spufs_wait returns without the
2398 * state mutex held */
2402 /* We may have had entries read from underneath
2403 * us while we dropped the mutex in spufs_wait,
2405 if (spufs_switch_log_used(ctx) == 0)
2410 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
2412 ctx->switch_log->tail =
2413 (ctx->switch_log->tail + 1) %
2416 /* If the record is greater than space available return
2417 * partial buffer (so far) */
2420 error = copy_to_user(buf + cnt, tbuf, width);
2428 return cnt == 0 ? error : cnt;
2431 static __poll_t spufs_switch_log_poll(struct file *file, poll_table *wait)
2433 struct inode *inode = file_inode(file);
2434 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2438 poll_wait(file, &ctx->switch_log->wait, wait);
2440 rc = spu_acquire(ctx);
2444 if (spufs_switch_log_used(ctx) > 0)
2452 static const struct file_operations spufs_switch_log_fops = {
2453 .open = spufs_switch_log_open,
2454 .read = spufs_switch_log_read,
2455 .poll = spufs_switch_log_poll,
2456 .release = spufs_switch_log_release,
2457 .llseek = no_llseek,
2461 * Log a context switch event to a switch log reader.
2463 * Must be called with ctx->state_mutex held.
2465 void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2468 if (!ctx->switch_log)
2471 if (spufs_switch_log_avail(ctx) > 1) {
2472 struct switch_log_entry *p;
2474 p = ctx->switch_log->log + ctx->switch_log->head;
2475 ktime_get_ts64(&p->tstamp);
2476 p->timebase = get_tb();
2477 p->spu_id = spu ? spu->number : -1;
2481 ctx->switch_log->head =
2482 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2485 wake_up(&ctx->switch_log->wait);
2488 static int spufs_show_ctx(struct seq_file *s, void *private)
2490 struct spu_context *ctx = s->private;
2493 mutex_lock(&ctx->state_mutex);
2495 struct spu *spu = ctx->spu;
2496 struct spu_priv2 __iomem *priv2 = spu->priv2;
2498 spin_lock_irq(&spu->register_lock);
2499 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2500 spin_unlock_irq(&spu->register_lock);
2502 struct spu_state *csa = &ctx->csa;
2504 mfc_control_RW = csa->priv2.mfc_control_RW;
2507 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
2508 " %c %llx %llx %llx %llx %x %x\n",
2509 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2514 ctx->spu ? ctx->spu->number : -1,
2515 !list_empty(&ctx->rq) ? 'q' : ' ',
2516 ctx->csa.class_0_pending,
2517 ctx->csa.class_0_dar,
2518 ctx->csa.class_1_dsisr,
2520 ctx->ops->runcntl_read(ctx),
2521 ctx->ops->status_read(ctx));
2523 mutex_unlock(&ctx->state_mutex);
2528 static int spufs_ctx_open(struct inode *inode, struct file *file)
2530 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2533 static const struct file_operations spufs_ctx_fops = {
2534 .open = spufs_ctx_open,
2536 .llseek = seq_lseek,
2537 .release = single_release,
2540 const struct spufs_tree_descr spufs_dir_contents[] = {
2541 { "capabilities", &spufs_caps_fops, 0444, },
2542 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2543 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
2544 { "mbox", &spufs_mbox_fops, 0444, },
2545 { "ibox", &spufs_ibox_fops, 0444, },
2546 { "wbox", &spufs_wbox_fops, 0222, },
2547 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2548 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2549 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2550 { "signal1", &spufs_signal1_fops, 0666, },
2551 { "signal2", &spufs_signal2_fops, 0666, },
2552 { "signal1_type", &spufs_signal1_type, 0666, },
2553 { "signal2_type", &spufs_signal2_type, 0666, },
2554 { "cntl", &spufs_cntl_fops, 0666, },
2555 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
2556 { "lslr", &spufs_lslr_ops, 0444, },
2557 { "mfc", &spufs_mfc_fops, 0666, },
2558 { "mss", &spufs_mss_fops, 0666, },
2559 { "npc", &spufs_npc_ops, 0666, },
2560 { "srr0", &spufs_srr0_ops, 0666, },
2561 { "decr", &spufs_decr_ops, 0666, },
2562 { "decr_status", &spufs_decr_status_ops, 0666, },
2563 { "event_mask", &spufs_event_mask_ops, 0666, },
2564 { "event_status", &spufs_event_status_ops, 0444, },
2565 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
2566 { "phys-id", &spufs_id_ops, 0666, },
2567 { "object-id", &spufs_object_id_ops, 0666, },
2568 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
2569 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
2570 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
2571 { "dma_info", &spufs_dma_info_fops, 0444,
2572 sizeof(struct spu_dma_info), },
2573 { "proxydma_info", &spufs_proxydma_info_fops, 0444,
2574 sizeof(struct spu_proxydma_info)},
2575 { "tid", &spufs_tid_fops, 0444, },
2576 { "stat", &spufs_stat_fops, 0444, },
2577 { "switch_log", &spufs_switch_log_fops, 0444 },
2581 const struct spufs_tree_descr spufs_dir_nosched_contents[] = {
2582 { "capabilities", &spufs_caps_fops, 0444, },
2583 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2584 { "mbox", &spufs_mbox_fops, 0444, },
2585 { "ibox", &spufs_ibox_fops, 0444, },
2586 { "wbox", &spufs_wbox_fops, 0222, },
2587 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2588 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2589 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2590 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2591 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2592 { "signal1_type", &spufs_signal1_type, 0666, },
2593 { "signal2_type", &spufs_signal2_type, 0666, },
2594 { "mss", &spufs_mss_fops, 0666, },
2595 { "mfc", &spufs_mfc_fops, 0666, },
2596 { "cntl", &spufs_cntl_fops, 0666, },
2597 { "npc", &spufs_npc_ops, 0666, },
2598 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
2599 { "phys-id", &spufs_id_ops, 0666, },
2600 { "object-id", &spufs_object_id_ops, 0666, },
2601 { "tid", &spufs_tid_fops, 0444, },
2602 { "stat", &spufs_stat_fops, 0444, },
2606 const struct spufs_tree_descr spufs_dir_debug_contents[] = {
2607 { ".ctx", &spufs_ctx_fops, 0444, },
2611 const struct spufs_coredump_reader spufs_coredump_read[] = {
2612 { "regs", spufs_regs_dump, NULL, sizeof(struct spu_reg128[128])},
2613 { "fpcr", spufs_fpcr_dump, NULL, sizeof(struct spu_reg128) },
2614 { "lslr", NULL, spufs_lslr_get, 19 },
2615 { "decr", NULL, spufs_decr_get, 19 },
2616 { "decr_status", NULL, spufs_decr_status_get, 19 },
2617 { "mem", spufs_mem_dump, NULL, LS_SIZE, },
2618 { "signal1", spufs_signal1_dump, NULL, sizeof(u32) },
2619 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
2620 { "signal2", spufs_signal2_dump, NULL, sizeof(u32) },
2621 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2622 { "event_mask", NULL, spufs_event_mask_get, 19 },
2623 { "event_status", NULL, spufs_event_status_get, 19 },
2624 { "mbox_info", spufs_mbox_info_dump, NULL, sizeof(u32) },
2625 { "ibox_info", spufs_ibox_info_dump, NULL, sizeof(u32) },
2626 { "wbox_info", spufs_wbox_info_dump, NULL, 4 * sizeof(u32)},
2627 { "dma_info", spufs_dma_info_dump, NULL, sizeof(struct spu_dma_info)},
2628 { "proxydma_info", spufs_proxydma_info_dump,
2629 NULL, sizeof(struct spu_proxydma_info)},
2630 { "object-id", NULL, spufs_object_id_get, 19 },
2631 { "npc", NULL, spufs_npc_get, 19 },