]> git.ipfire.org Git - people/ms/linux.git/blob - arch/powerpc/platforms/cell/spufs/file.c
vfs: do bulk POLL* -> EPOLL* replacement
[people/ms/linux.git] / arch / powerpc / platforms / cell / spufs / file.c
1 /*
2 * SPU file system -- file contents
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #undef DEBUG
24
25 #include <linux/fs.h>
26 #include <linux/ioctl.h>
27 #include <linux/export.h>
28 #include <linux/pagemap.h>
29 #include <linux/poll.h>
30 #include <linux/ptrace.h>
31 #include <linux/seq_file.h>
32 #include <linux/slab.h>
33
34 #include <asm/io.h>
35 #include <asm/time.h>
36 #include <asm/spu.h>
37 #include <asm/spu_info.h>
38 #include <linux/uaccess.h>
39
40 #include "spufs.h"
41 #include "sputrace.h"
42
43 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
44
45 /* Simple attribute files */
46 struct spufs_attr {
47 int (*get)(void *, u64 *);
48 int (*set)(void *, u64);
49 char get_buf[24]; /* enough to store a u64 and "\n\0" */
50 char set_buf[24];
51 void *data;
52 const char *fmt; /* format for read operation */
53 struct mutex mutex; /* protects access to these buffers */
54 };
55
56 static int spufs_attr_open(struct inode *inode, struct file *file,
57 int (*get)(void *, u64 *), int (*set)(void *, u64),
58 const char *fmt)
59 {
60 struct spufs_attr *attr;
61
62 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
63 if (!attr)
64 return -ENOMEM;
65
66 attr->get = get;
67 attr->set = set;
68 attr->data = inode->i_private;
69 attr->fmt = fmt;
70 mutex_init(&attr->mutex);
71 file->private_data = attr;
72
73 return nonseekable_open(inode, file);
74 }
75
76 static int spufs_attr_release(struct inode *inode, struct file *file)
77 {
78 kfree(file->private_data);
79 return 0;
80 }
81
82 static ssize_t spufs_attr_read(struct file *file, char __user *buf,
83 size_t len, loff_t *ppos)
84 {
85 struct spufs_attr *attr;
86 size_t size;
87 ssize_t ret;
88
89 attr = file->private_data;
90 if (!attr->get)
91 return -EACCES;
92
93 ret = mutex_lock_interruptible(&attr->mutex);
94 if (ret)
95 return ret;
96
97 if (*ppos) { /* continued read */
98 size = strlen(attr->get_buf);
99 } else { /* first read */
100 u64 val;
101 ret = attr->get(attr->data, &val);
102 if (ret)
103 goto out;
104
105 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
106 attr->fmt, (unsigned long long)val);
107 }
108
109 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
110 out:
111 mutex_unlock(&attr->mutex);
112 return ret;
113 }
114
115 static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
116 size_t len, loff_t *ppos)
117 {
118 struct spufs_attr *attr;
119 u64 val;
120 size_t size;
121 ssize_t ret;
122
123 attr = file->private_data;
124 if (!attr->set)
125 return -EACCES;
126
127 ret = mutex_lock_interruptible(&attr->mutex);
128 if (ret)
129 return ret;
130
131 ret = -EFAULT;
132 size = min(sizeof(attr->set_buf) - 1, len);
133 if (copy_from_user(attr->set_buf, buf, size))
134 goto out;
135
136 ret = len; /* claim we got the whole input */
137 attr->set_buf[size] = '\0';
138 val = simple_strtol(attr->set_buf, NULL, 0);
139 attr->set(attr->data, val);
140 out:
141 mutex_unlock(&attr->mutex);
142 return ret;
143 }
144
145 #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
146 static int __fops ## _open(struct inode *inode, struct file *file) \
147 { \
148 __simple_attr_check_format(__fmt, 0ull); \
149 return spufs_attr_open(inode, file, __get, __set, __fmt); \
150 } \
151 static const struct file_operations __fops = { \
152 .open = __fops ## _open, \
153 .release = spufs_attr_release, \
154 .read = spufs_attr_read, \
155 .write = spufs_attr_write, \
156 .llseek = generic_file_llseek, \
157 };
158
159
160 static int
161 spufs_mem_open(struct inode *inode, struct file *file)
162 {
163 struct spufs_inode_info *i = SPUFS_I(inode);
164 struct spu_context *ctx = i->i_ctx;
165
166 mutex_lock(&ctx->mapping_lock);
167 file->private_data = ctx;
168 if (!i->i_openers++)
169 ctx->local_store = inode->i_mapping;
170 mutex_unlock(&ctx->mapping_lock);
171 return 0;
172 }
173
174 static int
175 spufs_mem_release(struct inode *inode, struct file *file)
176 {
177 struct spufs_inode_info *i = SPUFS_I(inode);
178 struct spu_context *ctx = i->i_ctx;
179
180 mutex_lock(&ctx->mapping_lock);
181 if (!--i->i_openers)
182 ctx->local_store = NULL;
183 mutex_unlock(&ctx->mapping_lock);
184 return 0;
185 }
186
187 static ssize_t
188 __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
189 size_t size, loff_t *pos)
190 {
191 char *local_store = ctx->ops->get_ls(ctx);
192 return simple_read_from_buffer(buffer, size, pos, local_store,
193 LS_SIZE);
194 }
195
196 static ssize_t
197 spufs_mem_read(struct file *file, char __user *buffer,
198 size_t size, loff_t *pos)
199 {
200 struct spu_context *ctx = file->private_data;
201 ssize_t ret;
202
203 ret = spu_acquire(ctx);
204 if (ret)
205 return ret;
206 ret = __spufs_mem_read(ctx, buffer, size, pos);
207 spu_release(ctx);
208
209 return ret;
210 }
211
212 static ssize_t
213 spufs_mem_write(struct file *file, const char __user *buffer,
214 size_t size, loff_t *ppos)
215 {
216 struct spu_context *ctx = file->private_data;
217 char *local_store;
218 loff_t pos = *ppos;
219 int ret;
220
221 if (pos > LS_SIZE)
222 return -EFBIG;
223
224 ret = spu_acquire(ctx);
225 if (ret)
226 return ret;
227
228 local_store = ctx->ops->get_ls(ctx);
229 size = simple_write_to_buffer(local_store, LS_SIZE, ppos, buffer, size);
230 spu_release(ctx);
231
232 return size;
233 }
234
235 static int
236 spufs_mem_mmap_fault(struct vm_fault *vmf)
237 {
238 struct vm_area_struct *vma = vmf->vma;
239 struct spu_context *ctx = vma->vm_file->private_data;
240 unsigned long pfn, offset;
241
242 offset = vmf->pgoff << PAGE_SHIFT;
243 if (offset >= LS_SIZE)
244 return VM_FAULT_SIGBUS;
245
246 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
247 vmf->address, offset);
248
249 if (spu_acquire(ctx))
250 return VM_FAULT_NOPAGE;
251
252 if (ctx->state == SPU_STATE_SAVED) {
253 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
254 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
255 } else {
256 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
257 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
258 }
259 vm_insert_pfn(vma, vmf->address, pfn);
260
261 spu_release(ctx);
262
263 return VM_FAULT_NOPAGE;
264 }
265
266 static int spufs_mem_mmap_access(struct vm_area_struct *vma,
267 unsigned long address,
268 void *buf, int len, int write)
269 {
270 struct spu_context *ctx = vma->vm_file->private_data;
271 unsigned long offset = address - vma->vm_start;
272 char *local_store;
273
274 if (write && !(vma->vm_flags & VM_WRITE))
275 return -EACCES;
276 if (spu_acquire(ctx))
277 return -EINTR;
278 if ((offset + len) > vma->vm_end)
279 len = vma->vm_end - offset;
280 local_store = ctx->ops->get_ls(ctx);
281 if (write)
282 memcpy_toio(local_store + offset, buf, len);
283 else
284 memcpy_fromio(buf, local_store + offset, len);
285 spu_release(ctx);
286 return len;
287 }
288
289 static const struct vm_operations_struct spufs_mem_mmap_vmops = {
290 .fault = spufs_mem_mmap_fault,
291 .access = spufs_mem_mmap_access,
292 };
293
294 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
295 {
296 if (!(vma->vm_flags & VM_SHARED))
297 return -EINVAL;
298
299 vma->vm_flags |= VM_IO | VM_PFNMAP;
300 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
301
302 vma->vm_ops = &spufs_mem_mmap_vmops;
303 return 0;
304 }
305
306 static const struct file_operations spufs_mem_fops = {
307 .open = spufs_mem_open,
308 .release = spufs_mem_release,
309 .read = spufs_mem_read,
310 .write = spufs_mem_write,
311 .llseek = generic_file_llseek,
312 .mmap = spufs_mem_mmap,
313 };
314
315 static int spufs_ps_fault(struct vm_fault *vmf,
316 unsigned long ps_offs,
317 unsigned long ps_size)
318 {
319 struct spu_context *ctx = vmf->vma->vm_file->private_data;
320 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
321 int ret = 0;
322
323 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
324
325 if (offset >= ps_size)
326 return VM_FAULT_SIGBUS;
327
328 if (fatal_signal_pending(current))
329 return VM_FAULT_SIGBUS;
330
331 /*
332 * Because we release the mmap_sem, the context may be destroyed while
333 * we're in spu_wait. Grab an extra reference so it isn't destroyed
334 * in the meantime.
335 */
336 get_spu_context(ctx);
337
338 /*
339 * We have to wait for context to be loaded before we have
340 * pages to hand out to the user, but we don't want to wait
341 * with the mmap_sem held.
342 * It is possible to drop the mmap_sem here, but then we need
343 * to return VM_FAULT_NOPAGE because the mappings may have
344 * hanged.
345 */
346 if (spu_acquire(ctx))
347 goto refault;
348
349 if (ctx->state == SPU_STATE_SAVED) {
350 up_read(&current->mm->mmap_sem);
351 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
352 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
353 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
354 down_read(&current->mm->mmap_sem);
355 } else {
356 area = ctx->spu->problem_phys + ps_offs;
357 vm_insert_pfn(vmf->vma, vmf->address, (area + offset) >> PAGE_SHIFT);
358 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
359 }
360
361 if (!ret)
362 spu_release(ctx);
363
364 refault:
365 put_spu_context(ctx);
366 return VM_FAULT_NOPAGE;
367 }
368
369 #if SPUFS_MMAP_4K
370 static int spufs_cntl_mmap_fault(struct vm_fault *vmf)
371 {
372 return spufs_ps_fault(vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
373 }
374
375 static const struct vm_operations_struct spufs_cntl_mmap_vmops = {
376 .fault = spufs_cntl_mmap_fault,
377 };
378
379 /*
380 * mmap support for problem state control area [0x4000 - 0x4fff].
381 */
382 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
383 {
384 if (!(vma->vm_flags & VM_SHARED))
385 return -EINVAL;
386
387 vma->vm_flags |= VM_IO | VM_PFNMAP;
388 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
389
390 vma->vm_ops = &spufs_cntl_mmap_vmops;
391 return 0;
392 }
393 #else /* SPUFS_MMAP_4K */
394 #define spufs_cntl_mmap NULL
395 #endif /* !SPUFS_MMAP_4K */
396
397 static int spufs_cntl_get(void *data, u64 *val)
398 {
399 struct spu_context *ctx = data;
400 int ret;
401
402 ret = spu_acquire(ctx);
403 if (ret)
404 return ret;
405 *val = ctx->ops->status_read(ctx);
406 spu_release(ctx);
407
408 return 0;
409 }
410
411 static int spufs_cntl_set(void *data, u64 val)
412 {
413 struct spu_context *ctx = data;
414 int ret;
415
416 ret = spu_acquire(ctx);
417 if (ret)
418 return ret;
419 ctx->ops->runcntl_write(ctx, val);
420 spu_release(ctx);
421
422 return 0;
423 }
424
425 static int spufs_cntl_open(struct inode *inode, struct file *file)
426 {
427 struct spufs_inode_info *i = SPUFS_I(inode);
428 struct spu_context *ctx = i->i_ctx;
429
430 mutex_lock(&ctx->mapping_lock);
431 file->private_data = ctx;
432 if (!i->i_openers++)
433 ctx->cntl = inode->i_mapping;
434 mutex_unlock(&ctx->mapping_lock);
435 return simple_attr_open(inode, file, spufs_cntl_get,
436 spufs_cntl_set, "0x%08lx");
437 }
438
439 static int
440 spufs_cntl_release(struct inode *inode, struct file *file)
441 {
442 struct spufs_inode_info *i = SPUFS_I(inode);
443 struct spu_context *ctx = i->i_ctx;
444
445 simple_attr_release(inode, file);
446
447 mutex_lock(&ctx->mapping_lock);
448 if (!--i->i_openers)
449 ctx->cntl = NULL;
450 mutex_unlock(&ctx->mapping_lock);
451 return 0;
452 }
453
454 static const struct file_operations spufs_cntl_fops = {
455 .open = spufs_cntl_open,
456 .release = spufs_cntl_release,
457 .read = simple_attr_read,
458 .write = simple_attr_write,
459 .llseek = generic_file_llseek,
460 .mmap = spufs_cntl_mmap,
461 };
462
463 static int
464 spufs_regs_open(struct inode *inode, struct file *file)
465 {
466 struct spufs_inode_info *i = SPUFS_I(inode);
467 file->private_data = i->i_ctx;
468 return 0;
469 }
470
471 static ssize_t
472 __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
473 size_t size, loff_t *pos)
474 {
475 struct spu_lscsa *lscsa = ctx->csa.lscsa;
476 return simple_read_from_buffer(buffer, size, pos,
477 lscsa->gprs, sizeof lscsa->gprs);
478 }
479
480 static ssize_t
481 spufs_regs_read(struct file *file, char __user *buffer,
482 size_t size, loff_t *pos)
483 {
484 int ret;
485 struct spu_context *ctx = file->private_data;
486
487 /* pre-check for file position: if we'd return EOF, there's no point
488 * causing a deschedule */
489 if (*pos >= sizeof(ctx->csa.lscsa->gprs))
490 return 0;
491
492 ret = spu_acquire_saved(ctx);
493 if (ret)
494 return ret;
495 ret = __spufs_regs_read(ctx, buffer, size, pos);
496 spu_release_saved(ctx);
497 return ret;
498 }
499
500 static ssize_t
501 spufs_regs_write(struct file *file, const char __user *buffer,
502 size_t size, loff_t *pos)
503 {
504 struct spu_context *ctx = file->private_data;
505 struct spu_lscsa *lscsa = ctx->csa.lscsa;
506 int ret;
507
508 if (*pos >= sizeof(lscsa->gprs))
509 return -EFBIG;
510
511 ret = spu_acquire_saved(ctx);
512 if (ret)
513 return ret;
514
515 size = simple_write_to_buffer(lscsa->gprs, sizeof(lscsa->gprs), pos,
516 buffer, size);
517
518 spu_release_saved(ctx);
519 return size;
520 }
521
522 static const struct file_operations spufs_regs_fops = {
523 .open = spufs_regs_open,
524 .read = spufs_regs_read,
525 .write = spufs_regs_write,
526 .llseek = generic_file_llseek,
527 };
528
529 static ssize_t
530 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
531 size_t size, loff_t * pos)
532 {
533 struct spu_lscsa *lscsa = ctx->csa.lscsa;
534 return simple_read_from_buffer(buffer, size, pos,
535 &lscsa->fpcr, sizeof(lscsa->fpcr));
536 }
537
538 static ssize_t
539 spufs_fpcr_read(struct file *file, char __user * buffer,
540 size_t size, loff_t * pos)
541 {
542 int ret;
543 struct spu_context *ctx = file->private_data;
544
545 ret = spu_acquire_saved(ctx);
546 if (ret)
547 return ret;
548 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
549 spu_release_saved(ctx);
550 return ret;
551 }
552
553 static ssize_t
554 spufs_fpcr_write(struct file *file, const char __user * buffer,
555 size_t size, loff_t * pos)
556 {
557 struct spu_context *ctx = file->private_data;
558 struct spu_lscsa *lscsa = ctx->csa.lscsa;
559 int ret;
560
561 if (*pos >= sizeof(lscsa->fpcr))
562 return -EFBIG;
563
564 ret = spu_acquire_saved(ctx);
565 if (ret)
566 return ret;
567
568 size = simple_write_to_buffer(&lscsa->fpcr, sizeof(lscsa->fpcr), pos,
569 buffer, size);
570
571 spu_release_saved(ctx);
572 return size;
573 }
574
575 static const struct file_operations spufs_fpcr_fops = {
576 .open = spufs_regs_open,
577 .read = spufs_fpcr_read,
578 .write = spufs_fpcr_write,
579 .llseek = generic_file_llseek,
580 };
581
582 /* generic open function for all pipe-like files */
583 static int spufs_pipe_open(struct inode *inode, struct file *file)
584 {
585 struct spufs_inode_info *i = SPUFS_I(inode);
586 file->private_data = i->i_ctx;
587
588 return nonseekable_open(inode, file);
589 }
590
591 /*
592 * Read as many bytes from the mailbox as possible, until
593 * one of the conditions becomes true:
594 *
595 * - no more data available in the mailbox
596 * - end of the user provided buffer
597 * - end of the mapped area
598 */
599 static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
600 size_t len, loff_t *pos)
601 {
602 struct spu_context *ctx = file->private_data;
603 u32 mbox_data, __user *udata;
604 ssize_t count;
605
606 if (len < 4)
607 return -EINVAL;
608
609 if (!access_ok(VERIFY_WRITE, buf, len))
610 return -EFAULT;
611
612 udata = (void __user *)buf;
613
614 count = spu_acquire(ctx);
615 if (count)
616 return count;
617
618 for (count = 0; (count + 4) <= len; count += 4, udata++) {
619 int ret;
620 ret = ctx->ops->mbox_read(ctx, &mbox_data);
621 if (ret == 0)
622 break;
623
624 /*
625 * at the end of the mapped area, we can fault
626 * but still need to return the data we have
627 * read successfully so far.
628 */
629 ret = __put_user(mbox_data, udata);
630 if (ret) {
631 if (!count)
632 count = -EFAULT;
633 break;
634 }
635 }
636 spu_release(ctx);
637
638 if (!count)
639 count = -EAGAIN;
640
641 return count;
642 }
643
644 static const struct file_operations spufs_mbox_fops = {
645 .open = spufs_pipe_open,
646 .read = spufs_mbox_read,
647 .llseek = no_llseek,
648 };
649
650 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
651 size_t len, loff_t *pos)
652 {
653 struct spu_context *ctx = file->private_data;
654 ssize_t ret;
655 u32 mbox_stat;
656
657 if (len < 4)
658 return -EINVAL;
659
660 ret = spu_acquire(ctx);
661 if (ret)
662 return ret;
663
664 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
665
666 spu_release(ctx);
667
668 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
669 return -EFAULT;
670
671 return 4;
672 }
673
674 static const struct file_operations spufs_mbox_stat_fops = {
675 .open = spufs_pipe_open,
676 .read = spufs_mbox_stat_read,
677 .llseek = no_llseek,
678 };
679
680 /* low-level ibox access function */
681 size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
682 {
683 return ctx->ops->ibox_read(ctx, data);
684 }
685
686 /* interrupt-level ibox callback function. */
687 void spufs_ibox_callback(struct spu *spu)
688 {
689 struct spu_context *ctx = spu->ctx;
690
691 if (ctx)
692 wake_up_all(&ctx->ibox_wq);
693 }
694
695 /*
696 * Read as many bytes from the interrupt mailbox as possible, until
697 * one of the conditions becomes true:
698 *
699 * - no more data available in the mailbox
700 * - end of the user provided buffer
701 * - end of the mapped area
702 *
703 * If the file is opened without O_NONBLOCK, we wait here until
704 * any data is available, but return when we have been able to
705 * read something.
706 */
707 static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
708 size_t len, loff_t *pos)
709 {
710 struct spu_context *ctx = file->private_data;
711 u32 ibox_data, __user *udata;
712 ssize_t count;
713
714 if (len < 4)
715 return -EINVAL;
716
717 if (!access_ok(VERIFY_WRITE, buf, len))
718 return -EFAULT;
719
720 udata = (void __user *)buf;
721
722 count = spu_acquire(ctx);
723 if (count)
724 goto out;
725
726 /* wait only for the first element */
727 count = 0;
728 if (file->f_flags & O_NONBLOCK) {
729 if (!spu_ibox_read(ctx, &ibox_data)) {
730 count = -EAGAIN;
731 goto out_unlock;
732 }
733 } else {
734 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
735 if (count)
736 goto out;
737 }
738
739 /* if we can't write at all, return -EFAULT */
740 count = __put_user(ibox_data, udata);
741 if (count)
742 goto out_unlock;
743
744 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
745 int ret;
746 ret = ctx->ops->ibox_read(ctx, &ibox_data);
747 if (ret == 0)
748 break;
749 /*
750 * at the end of the mapped area, we can fault
751 * but still need to return the data we have
752 * read successfully so far.
753 */
754 ret = __put_user(ibox_data, udata);
755 if (ret)
756 break;
757 }
758
759 out_unlock:
760 spu_release(ctx);
761 out:
762 return count;
763 }
764
765 static __poll_t spufs_ibox_poll(struct file *file, poll_table *wait)
766 {
767 struct spu_context *ctx = file->private_data;
768 __poll_t mask;
769
770 poll_wait(file, &ctx->ibox_wq, wait);
771
772 /*
773 * For now keep this uninterruptible and also ignore the rule
774 * that poll should not sleep. Will be fixed later.
775 */
776 mutex_lock(&ctx->state_mutex);
777 mask = ctx->ops->mbox_stat_poll(ctx, EPOLLIN | EPOLLRDNORM);
778 spu_release(ctx);
779
780 return mask;
781 }
782
783 static const struct file_operations spufs_ibox_fops = {
784 .open = spufs_pipe_open,
785 .read = spufs_ibox_read,
786 .poll = spufs_ibox_poll,
787 .llseek = no_llseek,
788 };
789
790 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
791 size_t len, loff_t *pos)
792 {
793 struct spu_context *ctx = file->private_data;
794 ssize_t ret;
795 u32 ibox_stat;
796
797 if (len < 4)
798 return -EINVAL;
799
800 ret = spu_acquire(ctx);
801 if (ret)
802 return ret;
803 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
804 spu_release(ctx);
805
806 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
807 return -EFAULT;
808
809 return 4;
810 }
811
812 static const struct file_operations spufs_ibox_stat_fops = {
813 .open = spufs_pipe_open,
814 .read = spufs_ibox_stat_read,
815 .llseek = no_llseek,
816 };
817
818 /* low-level mailbox write */
819 size_t spu_wbox_write(struct spu_context *ctx, u32 data)
820 {
821 return ctx->ops->wbox_write(ctx, data);
822 }
823
824 /* interrupt-level wbox callback function. */
825 void spufs_wbox_callback(struct spu *spu)
826 {
827 struct spu_context *ctx = spu->ctx;
828
829 if (ctx)
830 wake_up_all(&ctx->wbox_wq);
831 }
832
833 /*
834 * Write as many bytes to the interrupt mailbox as possible, until
835 * one of the conditions becomes true:
836 *
837 * - the mailbox is full
838 * - end of the user provided buffer
839 * - end of the mapped area
840 *
841 * If the file is opened without O_NONBLOCK, we wait here until
842 * space is available, but return when we have been able to
843 * write something.
844 */
845 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
846 size_t len, loff_t *pos)
847 {
848 struct spu_context *ctx = file->private_data;
849 u32 wbox_data, __user *udata;
850 ssize_t count;
851
852 if (len < 4)
853 return -EINVAL;
854
855 udata = (void __user *)buf;
856 if (!access_ok(VERIFY_READ, buf, len))
857 return -EFAULT;
858
859 if (__get_user(wbox_data, udata))
860 return -EFAULT;
861
862 count = spu_acquire(ctx);
863 if (count)
864 goto out;
865
866 /*
867 * make sure we can at least write one element, by waiting
868 * in case of !O_NONBLOCK
869 */
870 count = 0;
871 if (file->f_flags & O_NONBLOCK) {
872 if (!spu_wbox_write(ctx, wbox_data)) {
873 count = -EAGAIN;
874 goto out_unlock;
875 }
876 } else {
877 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
878 if (count)
879 goto out;
880 }
881
882
883 /* write as much as possible */
884 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
885 int ret;
886 ret = __get_user(wbox_data, udata);
887 if (ret)
888 break;
889
890 ret = spu_wbox_write(ctx, wbox_data);
891 if (ret == 0)
892 break;
893 }
894
895 out_unlock:
896 spu_release(ctx);
897 out:
898 return count;
899 }
900
901 static __poll_t spufs_wbox_poll(struct file *file, poll_table *wait)
902 {
903 struct spu_context *ctx = file->private_data;
904 __poll_t mask;
905
906 poll_wait(file, &ctx->wbox_wq, wait);
907
908 /*
909 * For now keep this uninterruptible and also ignore the rule
910 * that poll should not sleep. Will be fixed later.
911 */
912 mutex_lock(&ctx->state_mutex);
913 mask = ctx->ops->mbox_stat_poll(ctx, EPOLLOUT | EPOLLWRNORM);
914 spu_release(ctx);
915
916 return mask;
917 }
918
919 static const struct file_operations spufs_wbox_fops = {
920 .open = spufs_pipe_open,
921 .write = spufs_wbox_write,
922 .poll = spufs_wbox_poll,
923 .llseek = no_llseek,
924 };
925
926 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
927 size_t len, loff_t *pos)
928 {
929 struct spu_context *ctx = file->private_data;
930 ssize_t ret;
931 u32 wbox_stat;
932
933 if (len < 4)
934 return -EINVAL;
935
936 ret = spu_acquire(ctx);
937 if (ret)
938 return ret;
939 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
940 spu_release(ctx);
941
942 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
943 return -EFAULT;
944
945 return 4;
946 }
947
948 static const struct file_operations spufs_wbox_stat_fops = {
949 .open = spufs_pipe_open,
950 .read = spufs_wbox_stat_read,
951 .llseek = no_llseek,
952 };
953
954 static int spufs_signal1_open(struct inode *inode, struct file *file)
955 {
956 struct spufs_inode_info *i = SPUFS_I(inode);
957 struct spu_context *ctx = i->i_ctx;
958
959 mutex_lock(&ctx->mapping_lock);
960 file->private_data = ctx;
961 if (!i->i_openers++)
962 ctx->signal1 = inode->i_mapping;
963 mutex_unlock(&ctx->mapping_lock);
964 return nonseekable_open(inode, file);
965 }
966
967 static int
968 spufs_signal1_release(struct inode *inode, struct file *file)
969 {
970 struct spufs_inode_info *i = SPUFS_I(inode);
971 struct spu_context *ctx = i->i_ctx;
972
973 mutex_lock(&ctx->mapping_lock);
974 if (!--i->i_openers)
975 ctx->signal1 = NULL;
976 mutex_unlock(&ctx->mapping_lock);
977 return 0;
978 }
979
980 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
981 size_t len, loff_t *pos)
982 {
983 int ret = 0;
984 u32 data;
985
986 if (len < 4)
987 return -EINVAL;
988
989 if (ctx->csa.spu_chnlcnt_RW[3]) {
990 data = ctx->csa.spu_chnldata_RW[3];
991 ret = 4;
992 }
993
994 if (!ret)
995 goto out;
996
997 if (copy_to_user(buf, &data, 4))
998 return -EFAULT;
999
1000 out:
1001 return ret;
1002 }
1003
1004 static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1005 size_t len, loff_t *pos)
1006 {
1007 int ret;
1008 struct spu_context *ctx = file->private_data;
1009
1010 ret = spu_acquire_saved(ctx);
1011 if (ret)
1012 return ret;
1013 ret = __spufs_signal1_read(ctx, buf, len, pos);
1014 spu_release_saved(ctx);
1015
1016 return ret;
1017 }
1018
1019 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1020 size_t len, loff_t *pos)
1021 {
1022 struct spu_context *ctx;
1023 ssize_t ret;
1024 u32 data;
1025
1026 ctx = file->private_data;
1027
1028 if (len < 4)
1029 return -EINVAL;
1030
1031 if (copy_from_user(&data, buf, 4))
1032 return -EFAULT;
1033
1034 ret = spu_acquire(ctx);
1035 if (ret)
1036 return ret;
1037 ctx->ops->signal1_write(ctx, data);
1038 spu_release(ctx);
1039
1040 return 4;
1041 }
1042
1043 static int
1044 spufs_signal1_mmap_fault(struct vm_fault *vmf)
1045 {
1046 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1047 return spufs_ps_fault(vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1048 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
1049 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1050 * signal 1 and 2 area
1051 */
1052 return spufs_ps_fault(vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
1053 #else
1054 #error unsupported page size
1055 #endif
1056 }
1057
1058 static const struct vm_operations_struct spufs_signal1_mmap_vmops = {
1059 .fault = spufs_signal1_mmap_fault,
1060 };
1061
1062 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1063 {
1064 if (!(vma->vm_flags & VM_SHARED))
1065 return -EINVAL;
1066
1067 vma->vm_flags |= VM_IO | VM_PFNMAP;
1068 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1069
1070 vma->vm_ops = &spufs_signal1_mmap_vmops;
1071 return 0;
1072 }
1073
1074 static const struct file_operations spufs_signal1_fops = {
1075 .open = spufs_signal1_open,
1076 .release = spufs_signal1_release,
1077 .read = spufs_signal1_read,
1078 .write = spufs_signal1_write,
1079 .mmap = spufs_signal1_mmap,
1080 .llseek = no_llseek,
1081 };
1082
1083 static const struct file_operations spufs_signal1_nosched_fops = {
1084 .open = spufs_signal1_open,
1085 .release = spufs_signal1_release,
1086 .write = spufs_signal1_write,
1087 .mmap = spufs_signal1_mmap,
1088 .llseek = no_llseek,
1089 };
1090
1091 static int spufs_signal2_open(struct inode *inode, struct file *file)
1092 {
1093 struct spufs_inode_info *i = SPUFS_I(inode);
1094 struct spu_context *ctx = i->i_ctx;
1095
1096 mutex_lock(&ctx->mapping_lock);
1097 file->private_data = ctx;
1098 if (!i->i_openers++)
1099 ctx->signal2 = inode->i_mapping;
1100 mutex_unlock(&ctx->mapping_lock);
1101 return nonseekable_open(inode, file);
1102 }
1103
1104 static int
1105 spufs_signal2_release(struct inode *inode, struct file *file)
1106 {
1107 struct spufs_inode_info *i = SPUFS_I(inode);
1108 struct spu_context *ctx = i->i_ctx;
1109
1110 mutex_lock(&ctx->mapping_lock);
1111 if (!--i->i_openers)
1112 ctx->signal2 = NULL;
1113 mutex_unlock(&ctx->mapping_lock);
1114 return 0;
1115 }
1116
1117 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
1118 size_t len, loff_t *pos)
1119 {
1120 int ret = 0;
1121 u32 data;
1122
1123 if (len < 4)
1124 return -EINVAL;
1125
1126 if (ctx->csa.spu_chnlcnt_RW[4]) {
1127 data = ctx->csa.spu_chnldata_RW[4];
1128 ret = 4;
1129 }
1130
1131 if (!ret)
1132 goto out;
1133
1134 if (copy_to_user(buf, &data, 4))
1135 return -EFAULT;
1136
1137 out:
1138 return ret;
1139 }
1140
1141 static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1142 size_t len, loff_t *pos)
1143 {
1144 struct spu_context *ctx = file->private_data;
1145 int ret;
1146
1147 ret = spu_acquire_saved(ctx);
1148 if (ret)
1149 return ret;
1150 ret = __spufs_signal2_read(ctx, buf, len, pos);
1151 spu_release_saved(ctx);
1152
1153 return ret;
1154 }
1155
1156 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1157 size_t len, loff_t *pos)
1158 {
1159 struct spu_context *ctx;
1160 ssize_t ret;
1161 u32 data;
1162
1163 ctx = file->private_data;
1164
1165 if (len < 4)
1166 return -EINVAL;
1167
1168 if (copy_from_user(&data, buf, 4))
1169 return -EFAULT;
1170
1171 ret = spu_acquire(ctx);
1172 if (ret)
1173 return ret;
1174 ctx->ops->signal2_write(ctx, data);
1175 spu_release(ctx);
1176
1177 return 4;
1178 }
1179
1180 #if SPUFS_MMAP_4K
1181 static int
1182 spufs_signal2_mmap_fault(struct vm_fault *vmf)
1183 {
1184 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1185 return spufs_ps_fault(vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1186 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
1187 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1188 * signal 1 and 2 area
1189 */
1190 return spufs_ps_fault(vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
1191 #else
1192 #error unsupported page size
1193 #endif
1194 }
1195
1196 static const struct vm_operations_struct spufs_signal2_mmap_vmops = {
1197 .fault = spufs_signal2_mmap_fault,
1198 };
1199
1200 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1201 {
1202 if (!(vma->vm_flags & VM_SHARED))
1203 return -EINVAL;
1204
1205 vma->vm_flags |= VM_IO | VM_PFNMAP;
1206 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1207
1208 vma->vm_ops = &spufs_signal2_mmap_vmops;
1209 return 0;
1210 }
1211 #else /* SPUFS_MMAP_4K */
1212 #define spufs_signal2_mmap NULL
1213 #endif /* !SPUFS_MMAP_4K */
1214
1215 static const struct file_operations spufs_signal2_fops = {
1216 .open = spufs_signal2_open,
1217 .release = spufs_signal2_release,
1218 .read = spufs_signal2_read,
1219 .write = spufs_signal2_write,
1220 .mmap = spufs_signal2_mmap,
1221 .llseek = no_llseek,
1222 };
1223
1224 static const struct file_operations spufs_signal2_nosched_fops = {
1225 .open = spufs_signal2_open,
1226 .release = spufs_signal2_release,
1227 .write = spufs_signal2_write,
1228 .mmap = spufs_signal2_mmap,
1229 .llseek = no_llseek,
1230 };
1231
1232 /*
1233 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1234 * work of acquiring (or not) the SPU context before calling through
1235 * to the actual get routine. The set routine is called directly.
1236 */
1237 #define SPU_ATTR_NOACQUIRE 0
1238 #define SPU_ATTR_ACQUIRE 1
1239 #define SPU_ATTR_ACQUIRE_SAVED 2
1240
1241 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
1242 static int __##__get(void *data, u64 *val) \
1243 { \
1244 struct spu_context *ctx = data; \
1245 int ret = 0; \
1246 \
1247 if (__acquire == SPU_ATTR_ACQUIRE) { \
1248 ret = spu_acquire(ctx); \
1249 if (ret) \
1250 return ret; \
1251 *val = __get(ctx); \
1252 spu_release(ctx); \
1253 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1254 ret = spu_acquire_saved(ctx); \
1255 if (ret) \
1256 return ret; \
1257 *val = __get(ctx); \
1258 spu_release_saved(ctx); \
1259 } else \
1260 *val = __get(ctx); \
1261 \
1262 return 0; \
1263 } \
1264 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1265
1266 static int spufs_signal1_type_set(void *data, u64 val)
1267 {
1268 struct spu_context *ctx = data;
1269 int ret;
1270
1271 ret = spu_acquire(ctx);
1272 if (ret)
1273 return ret;
1274 ctx->ops->signal1_type_set(ctx, val);
1275 spu_release(ctx);
1276
1277 return 0;
1278 }
1279
1280 static u64 spufs_signal1_type_get(struct spu_context *ctx)
1281 {
1282 return ctx->ops->signal1_type_get(ctx);
1283 }
1284 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1285 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1286
1287
1288 static int spufs_signal2_type_set(void *data, u64 val)
1289 {
1290 struct spu_context *ctx = data;
1291 int ret;
1292
1293 ret = spu_acquire(ctx);
1294 if (ret)
1295 return ret;
1296 ctx->ops->signal2_type_set(ctx, val);
1297 spu_release(ctx);
1298
1299 return 0;
1300 }
1301
1302 static u64 spufs_signal2_type_get(struct spu_context *ctx)
1303 {
1304 return ctx->ops->signal2_type_get(ctx);
1305 }
1306 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1307 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1308
1309 #if SPUFS_MMAP_4K
1310 static int
1311 spufs_mss_mmap_fault(struct vm_fault *vmf)
1312 {
1313 return spufs_ps_fault(vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
1314 }
1315
1316 static const struct vm_operations_struct spufs_mss_mmap_vmops = {
1317 .fault = spufs_mss_mmap_fault,
1318 };
1319
1320 /*
1321 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1322 */
1323 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1324 {
1325 if (!(vma->vm_flags & VM_SHARED))
1326 return -EINVAL;
1327
1328 vma->vm_flags |= VM_IO | VM_PFNMAP;
1329 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1330
1331 vma->vm_ops = &spufs_mss_mmap_vmops;
1332 return 0;
1333 }
1334 #else /* SPUFS_MMAP_4K */
1335 #define spufs_mss_mmap NULL
1336 #endif /* !SPUFS_MMAP_4K */
1337
1338 static int spufs_mss_open(struct inode *inode, struct file *file)
1339 {
1340 struct spufs_inode_info *i = SPUFS_I(inode);
1341 struct spu_context *ctx = i->i_ctx;
1342
1343 file->private_data = i->i_ctx;
1344
1345 mutex_lock(&ctx->mapping_lock);
1346 if (!i->i_openers++)
1347 ctx->mss = inode->i_mapping;
1348 mutex_unlock(&ctx->mapping_lock);
1349 return nonseekable_open(inode, file);
1350 }
1351
1352 static int
1353 spufs_mss_release(struct inode *inode, struct file *file)
1354 {
1355 struct spufs_inode_info *i = SPUFS_I(inode);
1356 struct spu_context *ctx = i->i_ctx;
1357
1358 mutex_lock(&ctx->mapping_lock);
1359 if (!--i->i_openers)
1360 ctx->mss = NULL;
1361 mutex_unlock(&ctx->mapping_lock);
1362 return 0;
1363 }
1364
1365 static const struct file_operations spufs_mss_fops = {
1366 .open = spufs_mss_open,
1367 .release = spufs_mss_release,
1368 .mmap = spufs_mss_mmap,
1369 .llseek = no_llseek,
1370 };
1371
1372 static int
1373 spufs_psmap_mmap_fault(struct vm_fault *vmf)
1374 {
1375 return spufs_ps_fault(vmf, 0x0000, SPUFS_PS_MAP_SIZE);
1376 }
1377
1378 static const struct vm_operations_struct spufs_psmap_mmap_vmops = {
1379 .fault = spufs_psmap_mmap_fault,
1380 };
1381
1382 /*
1383 * mmap support for full problem state area [0x00000 - 0x1ffff].
1384 */
1385 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1386 {
1387 if (!(vma->vm_flags & VM_SHARED))
1388 return -EINVAL;
1389
1390 vma->vm_flags |= VM_IO | VM_PFNMAP;
1391 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1392
1393 vma->vm_ops = &spufs_psmap_mmap_vmops;
1394 return 0;
1395 }
1396
1397 static int spufs_psmap_open(struct inode *inode, struct file *file)
1398 {
1399 struct spufs_inode_info *i = SPUFS_I(inode);
1400 struct spu_context *ctx = i->i_ctx;
1401
1402 mutex_lock(&ctx->mapping_lock);
1403 file->private_data = i->i_ctx;
1404 if (!i->i_openers++)
1405 ctx->psmap = inode->i_mapping;
1406 mutex_unlock(&ctx->mapping_lock);
1407 return nonseekable_open(inode, file);
1408 }
1409
1410 static int
1411 spufs_psmap_release(struct inode *inode, struct file *file)
1412 {
1413 struct spufs_inode_info *i = SPUFS_I(inode);
1414 struct spu_context *ctx = i->i_ctx;
1415
1416 mutex_lock(&ctx->mapping_lock);
1417 if (!--i->i_openers)
1418 ctx->psmap = NULL;
1419 mutex_unlock(&ctx->mapping_lock);
1420 return 0;
1421 }
1422
1423 static const struct file_operations spufs_psmap_fops = {
1424 .open = spufs_psmap_open,
1425 .release = spufs_psmap_release,
1426 .mmap = spufs_psmap_mmap,
1427 .llseek = no_llseek,
1428 };
1429
1430
1431 #if SPUFS_MMAP_4K
1432 static int
1433 spufs_mfc_mmap_fault(struct vm_fault *vmf)
1434 {
1435 return spufs_ps_fault(vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
1436 }
1437
1438 static const struct vm_operations_struct spufs_mfc_mmap_vmops = {
1439 .fault = spufs_mfc_mmap_fault,
1440 };
1441
1442 /*
1443 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1444 */
1445 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1446 {
1447 if (!(vma->vm_flags & VM_SHARED))
1448 return -EINVAL;
1449
1450 vma->vm_flags |= VM_IO | VM_PFNMAP;
1451 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1452
1453 vma->vm_ops = &spufs_mfc_mmap_vmops;
1454 return 0;
1455 }
1456 #else /* SPUFS_MMAP_4K */
1457 #define spufs_mfc_mmap NULL
1458 #endif /* !SPUFS_MMAP_4K */
1459
1460 static int spufs_mfc_open(struct inode *inode, struct file *file)
1461 {
1462 struct spufs_inode_info *i = SPUFS_I(inode);
1463 struct spu_context *ctx = i->i_ctx;
1464
1465 /* we don't want to deal with DMA into other processes */
1466 if (ctx->owner != current->mm)
1467 return -EINVAL;
1468
1469 if (atomic_read(&inode->i_count) != 1)
1470 return -EBUSY;
1471
1472 mutex_lock(&ctx->mapping_lock);
1473 file->private_data = ctx;
1474 if (!i->i_openers++)
1475 ctx->mfc = inode->i_mapping;
1476 mutex_unlock(&ctx->mapping_lock);
1477 return nonseekable_open(inode, file);
1478 }
1479
1480 static int
1481 spufs_mfc_release(struct inode *inode, struct file *file)
1482 {
1483 struct spufs_inode_info *i = SPUFS_I(inode);
1484 struct spu_context *ctx = i->i_ctx;
1485
1486 mutex_lock(&ctx->mapping_lock);
1487 if (!--i->i_openers)
1488 ctx->mfc = NULL;
1489 mutex_unlock(&ctx->mapping_lock);
1490 return 0;
1491 }
1492
1493 /* interrupt-level mfc callback function. */
1494 void spufs_mfc_callback(struct spu *spu)
1495 {
1496 struct spu_context *ctx = spu->ctx;
1497
1498 if (ctx)
1499 wake_up_all(&ctx->mfc_wq);
1500 }
1501
1502 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1503 {
1504 /* See if there is one tag group is complete */
1505 /* FIXME we need locking around tagwait */
1506 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1507 ctx->tagwait &= ~*status;
1508 if (*status)
1509 return 1;
1510
1511 /* enable interrupt waiting for any tag group,
1512 may silently fail if interrupts are already enabled */
1513 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1514 return 0;
1515 }
1516
1517 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1518 size_t size, loff_t *pos)
1519 {
1520 struct spu_context *ctx = file->private_data;
1521 int ret = -EINVAL;
1522 u32 status;
1523
1524 if (size != 4)
1525 goto out;
1526
1527 ret = spu_acquire(ctx);
1528 if (ret)
1529 return ret;
1530
1531 ret = -EINVAL;
1532 if (file->f_flags & O_NONBLOCK) {
1533 status = ctx->ops->read_mfc_tagstatus(ctx);
1534 if (!(status & ctx->tagwait))
1535 ret = -EAGAIN;
1536 else
1537 /* XXX(hch): shouldn't we clear ret here? */
1538 ctx->tagwait &= ~status;
1539 } else {
1540 ret = spufs_wait(ctx->mfc_wq,
1541 spufs_read_mfc_tagstatus(ctx, &status));
1542 if (ret)
1543 goto out;
1544 }
1545 spu_release(ctx);
1546
1547 ret = 4;
1548 if (copy_to_user(buffer, &status, 4))
1549 ret = -EFAULT;
1550
1551 out:
1552 return ret;
1553 }
1554
1555 static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1556 {
1557 pr_debug("queueing DMA %x %llx %x %x %x\n", cmd->lsa,
1558 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1559
1560 switch (cmd->cmd) {
1561 case MFC_PUT_CMD:
1562 case MFC_PUTF_CMD:
1563 case MFC_PUTB_CMD:
1564 case MFC_GET_CMD:
1565 case MFC_GETF_CMD:
1566 case MFC_GETB_CMD:
1567 break;
1568 default:
1569 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1570 return -EIO;
1571 }
1572
1573 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1574 pr_debug("invalid DMA alignment, ea %llx lsa %x\n",
1575 cmd->ea, cmd->lsa);
1576 return -EIO;
1577 }
1578
1579 switch (cmd->size & 0xf) {
1580 case 1:
1581 break;
1582 case 2:
1583 if (cmd->lsa & 1)
1584 goto error;
1585 break;
1586 case 4:
1587 if (cmd->lsa & 3)
1588 goto error;
1589 break;
1590 case 8:
1591 if (cmd->lsa & 7)
1592 goto error;
1593 break;
1594 case 0:
1595 if (cmd->lsa & 15)
1596 goto error;
1597 break;
1598 error:
1599 default:
1600 pr_debug("invalid DMA alignment %x for size %x\n",
1601 cmd->lsa & 0xf, cmd->size);
1602 return -EIO;
1603 }
1604
1605 if (cmd->size > 16 * 1024) {
1606 pr_debug("invalid DMA size %x\n", cmd->size);
1607 return -EIO;
1608 }
1609
1610 if (cmd->tag & 0xfff0) {
1611 /* we reserve the higher tag numbers for kernel use */
1612 pr_debug("invalid DMA tag\n");
1613 return -EIO;
1614 }
1615
1616 if (cmd->class) {
1617 /* not supported in this version */
1618 pr_debug("invalid DMA class\n");
1619 return -EIO;
1620 }
1621
1622 return 0;
1623 }
1624
1625 static int spu_send_mfc_command(struct spu_context *ctx,
1626 struct mfc_dma_command cmd,
1627 int *error)
1628 {
1629 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1630 if (*error == -EAGAIN) {
1631 /* wait for any tag group to complete
1632 so we have space for the new command */
1633 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1634 /* try again, because the queue might be
1635 empty again */
1636 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1637 if (*error == -EAGAIN)
1638 return 0;
1639 }
1640 return 1;
1641 }
1642
1643 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1644 size_t size, loff_t *pos)
1645 {
1646 struct spu_context *ctx = file->private_data;
1647 struct mfc_dma_command cmd;
1648 int ret = -EINVAL;
1649
1650 if (size != sizeof cmd)
1651 goto out;
1652
1653 ret = -EFAULT;
1654 if (copy_from_user(&cmd, buffer, sizeof cmd))
1655 goto out;
1656
1657 ret = spufs_check_valid_dma(&cmd);
1658 if (ret)
1659 goto out;
1660
1661 ret = spu_acquire(ctx);
1662 if (ret)
1663 goto out;
1664
1665 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1666 if (ret)
1667 goto out;
1668
1669 if (file->f_flags & O_NONBLOCK) {
1670 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1671 } else {
1672 int status;
1673 ret = spufs_wait(ctx->mfc_wq,
1674 spu_send_mfc_command(ctx, cmd, &status));
1675 if (ret)
1676 goto out;
1677 if (status)
1678 ret = status;
1679 }
1680
1681 if (ret)
1682 goto out_unlock;
1683
1684 ctx->tagwait |= 1 << cmd.tag;
1685 ret = size;
1686
1687 out_unlock:
1688 spu_release(ctx);
1689 out:
1690 return ret;
1691 }
1692
1693 static __poll_t spufs_mfc_poll(struct file *file,poll_table *wait)
1694 {
1695 struct spu_context *ctx = file->private_data;
1696 u32 free_elements, tagstatus;
1697 __poll_t mask;
1698
1699 poll_wait(file, &ctx->mfc_wq, wait);
1700
1701 /*
1702 * For now keep this uninterruptible and also ignore the rule
1703 * that poll should not sleep. Will be fixed later.
1704 */
1705 mutex_lock(&ctx->state_mutex);
1706 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1707 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1708 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1709 spu_release(ctx);
1710
1711 mask = 0;
1712 if (free_elements & 0xffff)
1713 mask |= EPOLLOUT | EPOLLWRNORM;
1714 if (tagstatus & ctx->tagwait)
1715 mask |= EPOLLIN | EPOLLRDNORM;
1716
1717 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
1718 free_elements, tagstatus, ctx->tagwait);
1719
1720 return mask;
1721 }
1722
1723 static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1724 {
1725 struct spu_context *ctx = file->private_data;
1726 int ret;
1727
1728 ret = spu_acquire(ctx);
1729 if (ret)
1730 goto out;
1731 #if 0
1732 /* this currently hangs */
1733 ret = spufs_wait(ctx->mfc_wq,
1734 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1735 if (ret)
1736 goto out;
1737 ret = spufs_wait(ctx->mfc_wq,
1738 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1739 if (ret)
1740 goto out;
1741 #else
1742 ret = 0;
1743 #endif
1744 spu_release(ctx);
1745 out:
1746 return ret;
1747 }
1748
1749 static int spufs_mfc_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1750 {
1751 struct inode *inode = file_inode(file);
1752 int err = file_write_and_wait_range(file, start, end);
1753 if (!err) {
1754 inode_lock(inode);
1755 err = spufs_mfc_flush(file, NULL);
1756 inode_unlock(inode);
1757 }
1758 return err;
1759 }
1760
1761 static const struct file_operations spufs_mfc_fops = {
1762 .open = spufs_mfc_open,
1763 .release = spufs_mfc_release,
1764 .read = spufs_mfc_read,
1765 .write = spufs_mfc_write,
1766 .poll = spufs_mfc_poll,
1767 .flush = spufs_mfc_flush,
1768 .fsync = spufs_mfc_fsync,
1769 .mmap = spufs_mfc_mmap,
1770 .llseek = no_llseek,
1771 };
1772
1773 static int spufs_npc_set(void *data, u64 val)
1774 {
1775 struct spu_context *ctx = data;
1776 int ret;
1777
1778 ret = spu_acquire(ctx);
1779 if (ret)
1780 return ret;
1781 ctx->ops->npc_write(ctx, val);
1782 spu_release(ctx);
1783
1784 return 0;
1785 }
1786
1787 static u64 spufs_npc_get(struct spu_context *ctx)
1788 {
1789 return ctx->ops->npc_read(ctx);
1790 }
1791 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1792 "0x%llx\n", SPU_ATTR_ACQUIRE);
1793
1794 static int spufs_decr_set(void *data, u64 val)
1795 {
1796 struct spu_context *ctx = data;
1797 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1798 int ret;
1799
1800 ret = spu_acquire_saved(ctx);
1801 if (ret)
1802 return ret;
1803 lscsa->decr.slot[0] = (u32) val;
1804 spu_release_saved(ctx);
1805
1806 return 0;
1807 }
1808
1809 static u64 spufs_decr_get(struct spu_context *ctx)
1810 {
1811 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1812 return lscsa->decr.slot[0];
1813 }
1814 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1815 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
1816
1817 static int spufs_decr_status_set(void *data, u64 val)
1818 {
1819 struct spu_context *ctx = data;
1820 int ret;
1821
1822 ret = spu_acquire_saved(ctx);
1823 if (ret)
1824 return ret;
1825 if (val)
1826 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1827 else
1828 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
1829 spu_release_saved(ctx);
1830
1831 return 0;
1832 }
1833
1834 static u64 spufs_decr_status_get(struct spu_context *ctx)
1835 {
1836 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1837 return SPU_DECR_STATUS_RUNNING;
1838 else
1839 return 0;
1840 }
1841 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1842 spufs_decr_status_set, "0x%llx\n",
1843 SPU_ATTR_ACQUIRE_SAVED);
1844
1845 static int spufs_event_mask_set(void *data, u64 val)
1846 {
1847 struct spu_context *ctx = data;
1848 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1849 int ret;
1850
1851 ret = spu_acquire_saved(ctx);
1852 if (ret)
1853 return ret;
1854 lscsa->event_mask.slot[0] = (u32) val;
1855 spu_release_saved(ctx);
1856
1857 return 0;
1858 }
1859
1860 static u64 spufs_event_mask_get(struct spu_context *ctx)
1861 {
1862 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1863 return lscsa->event_mask.slot[0];
1864 }
1865
1866 DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1867 spufs_event_mask_set, "0x%llx\n",
1868 SPU_ATTR_ACQUIRE_SAVED);
1869
1870 static u64 spufs_event_status_get(struct spu_context *ctx)
1871 {
1872 struct spu_state *state = &ctx->csa;
1873 u64 stat;
1874 stat = state->spu_chnlcnt_RW[0];
1875 if (stat)
1876 return state->spu_chnldata_RW[0];
1877 return 0;
1878 }
1879 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1880 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1881
1882 static int spufs_srr0_set(void *data, u64 val)
1883 {
1884 struct spu_context *ctx = data;
1885 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1886 int ret;
1887
1888 ret = spu_acquire_saved(ctx);
1889 if (ret)
1890 return ret;
1891 lscsa->srr0.slot[0] = (u32) val;
1892 spu_release_saved(ctx);
1893
1894 return 0;
1895 }
1896
1897 static u64 spufs_srr0_get(struct spu_context *ctx)
1898 {
1899 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1900 return lscsa->srr0.slot[0];
1901 }
1902 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1903 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1904
1905 static u64 spufs_id_get(struct spu_context *ctx)
1906 {
1907 u64 num;
1908
1909 if (ctx->state == SPU_STATE_RUNNABLE)
1910 num = ctx->spu->number;
1911 else
1912 num = (unsigned int)-1;
1913
1914 return num;
1915 }
1916 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1917 SPU_ATTR_ACQUIRE)
1918
1919 static u64 spufs_object_id_get(struct spu_context *ctx)
1920 {
1921 /* FIXME: Should there really be no locking here? */
1922 return ctx->object_id;
1923 }
1924
1925 static int spufs_object_id_set(void *data, u64 id)
1926 {
1927 struct spu_context *ctx = data;
1928 ctx->object_id = id;
1929
1930 return 0;
1931 }
1932
1933 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1934 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
1935
1936 static u64 spufs_lslr_get(struct spu_context *ctx)
1937 {
1938 return ctx->csa.priv2.spu_lslr_RW;
1939 }
1940 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1941 SPU_ATTR_ACQUIRE_SAVED);
1942
1943 static int spufs_info_open(struct inode *inode, struct file *file)
1944 {
1945 struct spufs_inode_info *i = SPUFS_I(inode);
1946 struct spu_context *ctx = i->i_ctx;
1947 file->private_data = ctx;
1948 return 0;
1949 }
1950
1951 static int spufs_caps_show(struct seq_file *s, void *private)
1952 {
1953 struct spu_context *ctx = s->private;
1954
1955 if (!(ctx->flags & SPU_CREATE_NOSCHED))
1956 seq_puts(s, "sched\n");
1957 if (!(ctx->flags & SPU_CREATE_ISOLATE))
1958 seq_puts(s, "step\n");
1959 return 0;
1960 }
1961
1962 static int spufs_caps_open(struct inode *inode, struct file *file)
1963 {
1964 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1965 }
1966
1967 static const struct file_operations spufs_caps_fops = {
1968 .open = spufs_caps_open,
1969 .read = seq_read,
1970 .llseek = seq_lseek,
1971 .release = single_release,
1972 };
1973
1974 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1975 char __user *buf, size_t len, loff_t *pos)
1976 {
1977 u32 data;
1978
1979 /* EOF if there's no entry in the mbox */
1980 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
1981 return 0;
1982
1983 data = ctx->csa.prob.pu_mb_R;
1984
1985 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1986 }
1987
1988 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1989 size_t len, loff_t *pos)
1990 {
1991 int ret;
1992 struct spu_context *ctx = file->private_data;
1993
1994 if (!access_ok(VERIFY_WRITE, buf, len))
1995 return -EFAULT;
1996
1997 ret = spu_acquire_saved(ctx);
1998 if (ret)
1999 return ret;
2000 spin_lock(&ctx->csa.register_lock);
2001 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
2002 spin_unlock(&ctx->csa.register_lock);
2003 spu_release_saved(ctx);
2004
2005 return ret;
2006 }
2007
2008 static const struct file_operations spufs_mbox_info_fops = {
2009 .open = spufs_info_open,
2010 .read = spufs_mbox_info_read,
2011 .llseek = generic_file_llseek,
2012 };
2013
2014 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2015 char __user *buf, size_t len, loff_t *pos)
2016 {
2017 u32 data;
2018
2019 /* EOF if there's no entry in the ibox */
2020 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2021 return 0;
2022
2023 data = ctx->csa.priv2.puint_mb_R;
2024
2025 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2026 }
2027
2028 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2029 size_t len, loff_t *pos)
2030 {
2031 struct spu_context *ctx = file->private_data;
2032 int ret;
2033
2034 if (!access_ok(VERIFY_WRITE, buf, len))
2035 return -EFAULT;
2036
2037 ret = spu_acquire_saved(ctx);
2038 if (ret)
2039 return ret;
2040 spin_lock(&ctx->csa.register_lock);
2041 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
2042 spin_unlock(&ctx->csa.register_lock);
2043 spu_release_saved(ctx);
2044
2045 return ret;
2046 }
2047
2048 static const struct file_operations spufs_ibox_info_fops = {
2049 .open = spufs_info_open,
2050 .read = spufs_ibox_info_read,
2051 .llseek = generic_file_llseek,
2052 };
2053
2054 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2055 char __user *buf, size_t len, loff_t *pos)
2056 {
2057 int i, cnt;
2058 u32 data[4];
2059 u32 wbox_stat;
2060
2061 wbox_stat = ctx->csa.prob.mb_stat_R;
2062 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2063 for (i = 0; i < cnt; i++) {
2064 data[i] = ctx->csa.spu_mailbox_data[i];
2065 }
2066
2067 return simple_read_from_buffer(buf, len, pos, &data,
2068 cnt * sizeof(u32));
2069 }
2070
2071 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2072 size_t len, loff_t *pos)
2073 {
2074 struct spu_context *ctx = file->private_data;
2075 int ret;
2076
2077 if (!access_ok(VERIFY_WRITE, buf, len))
2078 return -EFAULT;
2079
2080 ret = spu_acquire_saved(ctx);
2081 if (ret)
2082 return ret;
2083 spin_lock(&ctx->csa.register_lock);
2084 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
2085 spin_unlock(&ctx->csa.register_lock);
2086 spu_release_saved(ctx);
2087
2088 return ret;
2089 }
2090
2091 static const struct file_operations spufs_wbox_info_fops = {
2092 .open = spufs_info_open,
2093 .read = spufs_wbox_info_read,
2094 .llseek = generic_file_llseek,
2095 };
2096
2097 static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2098 char __user *buf, size_t len, loff_t *pos)
2099 {
2100 struct spu_dma_info info;
2101 struct mfc_cq_sr *qp, *spuqp;
2102 int i;
2103
2104 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2105 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2106 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2107 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2108 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2109 for (i = 0; i < 16; i++) {
2110 qp = &info.dma_info_command_data[i];
2111 spuqp = &ctx->csa.priv2.spuq[i];
2112
2113 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2114 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2115 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2116 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2117 }
2118
2119 return simple_read_from_buffer(buf, len, pos, &info,
2120 sizeof info);
2121 }
2122
2123 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2124 size_t len, loff_t *pos)
2125 {
2126 struct spu_context *ctx = file->private_data;
2127 int ret;
2128
2129 if (!access_ok(VERIFY_WRITE, buf, len))
2130 return -EFAULT;
2131
2132 ret = spu_acquire_saved(ctx);
2133 if (ret)
2134 return ret;
2135 spin_lock(&ctx->csa.register_lock);
2136 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2137 spin_unlock(&ctx->csa.register_lock);
2138 spu_release_saved(ctx);
2139
2140 return ret;
2141 }
2142
2143 static const struct file_operations spufs_dma_info_fops = {
2144 .open = spufs_info_open,
2145 .read = spufs_dma_info_read,
2146 .llseek = no_llseek,
2147 };
2148
2149 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2150 char __user *buf, size_t len, loff_t *pos)
2151 {
2152 struct spu_proxydma_info info;
2153 struct mfc_cq_sr *qp, *puqp;
2154 int ret = sizeof info;
2155 int i;
2156
2157 if (len < ret)
2158 return -EINVAL;
2159
2160 if (!access_ok(VERIFY_WRITE, buf, len))
2161 return -EFAULT;
2162
2163 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2164 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2165 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2166 for (i = 0; i < 8; i++) {
2167 qp = &info.proxydma_info_command_data[i];
2168 puqp = &ctx->csa.priv2.puq[i];
2169
2170 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2171 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2172 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2173 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2174 }
2175
2176 return simple_read_from_buffer(buf, len, pos, &info,
2177 sizeof info);
2178 }
2179
2180 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2181 size_t len, loff_t *pos)
2182 {
2183 struct spu_context *ctx = file->private_data;
2184 int ret;
2185
2186 ret = spu_acquire_saved(ctx);
2187 if (ret)
2188 return ret;
2189 spin_lock(&ctx->csa.register_lock);
2190 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2191 spin_unlock(&ctx->csa.register_lock);
2192 spu_release_saved(ctx);
2193
2194 return ret;
2195 }
2196
2197 static const struct file_operations spufs_proxydma_info_fops = {
2198 .open = spufs_info_open,
2199 .read = spufs_proxydma_info_read,
2200 .llseek = no_llseek,
2201 };
2202
2203 static int spufs_show_tid(struct seq_file *s, void *private)
2204 {
2205 struct spu_context *ctx = s->private;
2206
2207 seq_printf(s, "%d\n", ctx->tid);
2208 return 0;
2209 }
2210
2211 static int spufs_tid_open(struct inode *inode, struct file *file)
2212 {
2213 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2214 }
2215
2216 static const struct file_operations spufs_tid_fops = {
2217 .open = spufs_tid_open,
2218 .read = seq_read,
2219 .llseek = seq_lseek,
2220 .release = single_release,
2221 };
2222
2223 static const char *ctx_state_names[] = {
2224 "user", "system", "iowait", "loaded"
2225 };
2226
2227 static unsigned long long spufs_acct_time(struct spu_context *ctx,
2228 enum spu_utilization_state state)
2229 {
2230 unsigned long long time = ctx->stats.times[state];
2231
2232 /*
2233 * In general, utilization statistics are updated by the controlling
2234 * thread as the spu context moves through various well defined
2235 * state transitions, but if the context is lazily loaded its
2236 * utilization statistics are not updated as the controlling thread
2237 * is not tightly coupled with the execution of the spu context. We
2238 * calculate and apply the time delta from the last recorded state
2239 * of the spu context.
2240 */
2241 if (ctx->spu && ctx->stats.util_state == state) {
2242 time += ktime_get_ns() - ctx->stats.tstamp;
2243 }
2244
2245 return time / NSEC_PER_MSEC;
2246 }
2247
2248 static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2249 {
2250 unsigned long long slb_flts = ctx->stats.slb_flt;
2251
2252 if (ctx->state == SPU_STATE_RUNNABLE) {
2253 slb_flts += (ctx->spu->stats.slb_flt -
2254 ctx->stats.slb_flt_base);
2255 }
2256
2257 return slb_flts;
2258 }
2259
2260 static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2261 {
2262 unsigned long long class2_intrs = ctx->stats.class2_intr;
2263
2264 if (ctx->state == SPU_STATE_RUNNABLE) {
2265 class2_intrs += (ctx->spu->stats.class2_intr -
2266 ctx->stats.class2_intr_base);
2267 }
2268
2269 return class2_intrs;
2270 }
2271
2272
2273 static int spufs_show_stat(struct seq_file *s, void *private)
2274 {
2275 struct spu_context *ctx = s->private;
2276 int ret;
2277
2278 ret = spu_acquire(ctx);
2279 if (ret)
2280 return ret;
2281
2282 seq_printf(s, "%s %llu %llu %llu %llu "
2283 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2284 ctx_state_names[ctx->stats.util_state],
2285 spufs_acct_time(ctx, SPU_UTIL_USER),
2286 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2287 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2288 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2289 ctx->stats.vol_ctx_switch,
2290 ctx->stats.invol_ctx_switch,
2291 spufs_slb_flts(ctx),
2292 ctx->stats.hash_flt,
2293 ctx->stats.min_flt,
2294 ctx->stats.maj_flt,
2295 spufs_class2_intrs(ctx),
2296 ctx->stats.libassist);
2297 spu_release(ctx);
2298 return 0;
2299 }
2300
2301 static int spufs_stat_open(struct inode *inode, struct file *file)
2302 {
2303 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2304 }
2305
2306 static const struct file_operations spufs_stat_fops = {
2307 .open = spufs_stat_open,
2308 .read = seq_read,
2309 .llseek = seq_lseek,
2310 .release = single_release,
2311 };
2312
2313 static inline int spufs_switch_log_used(struct spu_context *ctx)
2314 {
2315 return (ctx->switch_log->head - ctx->switch_log->tail) %
2316 SWITCH_LOG_BUFSIZE;
2317 }
2318
2319 static inline int spufs_switch_log_avail(struct spu_context *ctx)
2320 {
2321 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2322 }
2323
2324 static int spufs_switch_log_open(struct inode *inode, struct file *file)
2325 {
2326 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2327 int rc;
2328
2329 rc = spu_acquire(ctx);
2330 if (rc)
2331 return rc;
2332
2333 if (ctx->switch_log) {
2334 rc = -EBUSY;
2335 goto out;
2336 }
2337
2338 ctx->switch_log = kmalloc(sizeof(struct switch_log) +
2339 SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
2340 GFP_KERNEL);
2341
2342 if (!ctx->switch_log) {
2343 rc = -ENOMEM;
2344 goto out;
2345 }
2346
2347 ctx->switch_log->head = ctx->switch_log->tail = 0;
2348 init_waitqueue_head(&ctx->switch_log->wait);
2349 rc = 0;
2350
2351 out:
2352 spu_release(ctx);
2353 return rc;
2354 }
2355
2356 static int spufs_switch_log_release(struct inode *inode, struct file *file)
2357 {
2358 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2359 int rc;
2360
2361 rc = spu_acquire(ctx);
2362 if (rc)
2363 return rc;
2364
2365 kfree(ctx->switch_log);
2366 ctx->switch_log = NULL;
2367 spu_release(ctx);
2368
2369 return 0;
2370 }
2371
2372 static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2373 {
2374 struct switch_log_entry *p;
2375
2376 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2377
2378 return snprintf(tbuf, n, "%llu.%09u %d %u %u %llu\n",
2379 (unsigned long long) p->tstamp.tv_sec,
2380 (unsigned int) p->tstamp.tv_nsec,
2381 p->spu_id,
2382 (unsigned int) p->type,
2383 (unsigned int) p->val,
2384 (unsigned long long) p->timebase);
2385 }
2386
2387 static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2388 size_t len, loff_t *ppos)
2389 {
2390 struct inode *inode = file_inode(file);
2391 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2392 int error = 0, cnt = 0;
2393
2394 if (!buf)
2395 return -EINVAL;
2396
2397 error = spu_acquire(ctx);
2398 if (error)
2399 return error;
2400
2401 while (cnt < len) {
2402 char tbuf[128];
2403 int width;
2404
2405 if (spufs_switch_log_used(ctx) == 0) {
2406 if (cnt > 0) {
2407 /* If there's data ready to go, we can
2408 * just return straight away */
2409 break;
2410
2411 } else if (file->f_flags & O_NONBLOCK) {
2412 error = -EAGAIN;
2413 break;
2414
2415 } else {
2416 /* spufs_wait will drop the mutex and
2417 * re-acquire, but since we're in read(), the
2418 * file cannot be _released (and so
2419 * ctx->switch_log is stable).
2420 */
2421 error = spufs_wait(ctx->switch_log->wait,
2422 spufs_switch_log_used(ctx) > 0);
2423
2424 /* On error, spufs_wait returns without the
2425 * state mutex held */
2426 if (error)
2427 return error;
2428
2429 /* We may have had entries read from underneath
2430 * us while we dropped the mutex in spufs_wait,
2431 * so re-check */
2432 if (spufs_switch_log_used(ctx) == 0)
2433 continue;
2434 }
2435 }
2436
2437 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
2438 if (width < len)
2439 ctx->switch_log->tail =
2440 (ctx->switch_log->tail + 1) %
2441 SWITCH_LOG_BUFSIZE;
2442 else
2443 /* If the record is greater than space available return
2444 * partial buffer (so far) */
2445 break;
2446
2447 error = copy_to_user(buf + cnt, tbuf, width);
2448 if (error)
2449 break;
2450 cnt += width;
2451 }
2452
2453 spu_release(ctx);
2454
2455 return cnt == 0 ? error : cnt;
2456 }
2457
2458 static __poll_t spufs_switch_log_poll(struct file *file, poll_table *wait)
2459 {
2460 struct inode *inode = file_inode(file);
2461 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2462 __poll_t mask = 0;
2463 int rc;
2464
2465 poll_wait(file, &ctx->switch_log->wait, wait);
2466
2467 rc = spu_acquire(ctx);
2468 if (rc)
2469 return rc;
2470
2471 if (spufs_switch_log_used(ctx) > 0)
2472 mask |= EPOLLIN;
2473
2474 spu_release(ctx);
2475
2476 return mask;
2477 }
2478
2479 static const struct file_operations spufs_switch_log_fops = {
2480 .open = spufs_switch_log_open,
2481 .read = spufs_switch_log_read,
2482 .poll = spufs_switch_log_poll,
2483 .release = spufs_switch_log_release,
2484 .llseek = no_llseek,
2485 };
2486
2487 /**
2488 * Log a context switch event to a switch log reader.
2489 *
2490 * Must be called with ctx->state_mutex held.
2491 */
2492 void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2493 u32 type, u32 val)
2494 {
2495 if (!ctx->switch_log)
2496 return;
2497
2498 if (spufs_switch_log_avail(ctx) > 1) {
2499 struct switch_log_entry *p;
2500
2501 p = ctx->switch_log->log + ctx->switch_log->head;
2502 ktime_get_ts64(&p->tstamp);
2503 p->timebase = get_tb();
2504 p->spu_id = spu ? spu->number : -1;
2505 p->type = type;
2506 p->val = val;
2507
2508 ctx->switch_log->head =
2509 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2510 }
2511
2512 wake_up(&ctx->switch_log->wait);
2513 }
2514
2515 static int spufs_show_ctx(struct seq_file *s, void *private)
2516 {
2517 struct spu_context *ctx = s->private;
2518 u64 mfc_control_RW;
2519
2520 mutex_lock(&ctx->state_mutex);
2521 if (ctx->spu) {
2522 struct spu *spu = ctx->spu;
2523 struct spu_priv2 __iomem *priv2 = spu->priv2;
2524
2525 spin_lock_irq(&spu->register_lock);
2526 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2527 spin_unlock_irq(&spu->register_lock);
2528 } else {
2529 struct spu_state *csa = &ctx->csa;
2530
2531 mfc_control_RW = csa->priv2.mfc_control_RW;
2532 }
2533
2534 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
2535 " %c %llx %llx %llx %llx %x %x\n",
2536 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2537 ctx->flags,
2538 ctx->sched_flags,
2539 ctx->prio,
2540 ctx->time_slice,
2541 ctx->spu ? ctx->spu->number : -1,
2542 !list_empty(&ctx->rq) ? 'q' : ' ',
2543 ctx->csa.class_0_pending,
2544 ctx->csa.class_0_dar,
2545 ctx->csa.class_1_dsisr,
2546 mfc_control_RW,
2547 ctx->ops->runcntl_read(ctx),
2548 ctx->ops->status_read(ctx));
2549
2550 mutex_unlock(&ctx->state_mutex);
2551
2552 return 0;
2553 }
2554
2555 static int spufs_ctx_open(struct inode *inode, struct file *file)
2556 {
2557 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2558 }
2559
2560 static const struct file_operations spufs_ctx_fops = {
2561 .open = spufs_ctx_open,
2562 .read = seq_read,
2563 .llseek = seq_lseek,
2564 .release = single_release,
2565 };
2566
2567 const struct spufs_tree_descr spufs_dir_contents[] = {
2568 { "capabilities", &spufs_caps_fops, 0444, },
2569 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2570 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
2571 { "mbox", &spufs_mbox_fops, 0444, },
2572 { "ibox", &spufs_ibox_fops, 0444, },
2573 { "wbox", &spufs_wbox_fops, 0222, },
2574 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2575 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2576 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2577 { "signal1", &spufs_signal1_fops, 0666, },
2578 { "signal2", &spufs_signal2_fops, 0666, },
2579 { "signal1_type", &spufs_signal1_type, 0666, },
2580 { "signal2_type", &spufs_signal2_type, 0666, },
2581 { "cntl", &spufs_cntl_fops, 0666, },
2582 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
2583 { "lslr", &spufs_lslr_ops, 0444, },
2584 { "mfc", &spufs_mfc_fops, 0666, },
2585 { "mss", &spufs_mss_fops, 0666, },
2586 { "npc", &spufs_npc_ops, 0666, },
2587 { "srr0", &spufs_srr0_ops, 0666, },
2588 { "decr", &spufs_decr_ops, 0666, },
2589 { "decr_status", &spufs_decr_status_ops, 0666, },
2590 { "event_mask", &spufs_event_mask_ops, 0666, },
2591 { "event_status", &spufs_event_status_ops, 0444, },
2592 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
2593 { "phys-id", &spufs_id_ops, 0666, },
2594 { "object-id", &spufs_object_id_ops, 0666, },
2595 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
2596 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
2597 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
2598 { "dma_info", &spufs_dma_info_fops, 0444,
2599 sizeof(struct spu_dma_info), },
2600 { "proxydma_info", &spufs_proxydma_info_fops, 0444,
2601 sizeof(struct spu_proxydma_info)},
2602 { "tid", &spufs_tid_fops, 0444, },
2603 { "stat", &spufs_stat_fops, 0444, },
2604 { "switch_log", &spufs_switch_log_fops, 0444 },
2605 {},
2606 };
2607
2608 const struct spufs_tree_descr spufs_dir_nosched_contents[] = {
2609 { "capabilities", &spufs_caps_fops, 0444, },
2610 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2611 { "mbox", &spufs_mbox_fops, 0444, },
2612 { "ibox", &spufs_ibox_fops, 0444, },
2613 { "wbox", &spufs_wbox_fops, 0222, },
2614 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2615 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2616 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2617 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2618 { "signal2", &spufs_signal2_nosched_fops, 0222, },
2619 { "signal1_type", &spufs_signal1_type, 0666, },
2620 { "signal2_type", &spufs_signal2_type, 0666, },
2621 { "mss", &spufs_mss_fops, 0666, },
2622 { "mfc", &spufs_mfc_fops, 0666, },
2623 { "cntl", &spufs_cntl_fops, 0666, },
2624 { "npc", &spufs_npc_ops, 0666, },
2625 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
2626 { "phys-id", &spufs_id_ops, 0666, },
2627 { "object-id", &spufs_object_id_ops, 0666, },
2628 { "tid", &spufs_tid_fops, 0444, },
2629 { "stat", &spufs_stat_fops, 0444, },
2630 {},
2631 };
2632
2633 const struct spufs_tree_descr spufs_dir_debug_contents[] = {
2634 { ".ctx", &spufs_ctx_fops, 0444, },
2635 {},
2636 };
2637
2638 const struct spufs_coredump_reader spufs_coredump_read[] = {
2639 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2640 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2641 { "lslr", NULL, spufs_lslr_get, 19 },
2642 { "decr", NULL, spufs_decr_get, 19 },
2643 { "decr_status", NULL, spufs_decr_status_get, 19 },
2644 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2645 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2646 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
2647 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2648 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2649 { "event_mask", NULL, spufs_event_mask_get, 19 },
2650 { "event_status", NULL, spufs_event_status_get, 19 },
2651 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2652 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2653 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2654 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2655 { "proxydma_info", __spufs_proxydma_info_read,
2656 NULL, sizeof(struct spu_proxydma_info)},
2657 { "object-id", NULL, spufs_object_id_get, 19 },
2658 { "npc", NULL, spufs_npc_get, 19 },
2659 { NULL },
2660 };