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