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