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