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