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