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