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