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