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