]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/powerpc/platforms/cell/spufs/file.c
[POWERPC] spufs: add enchanced simple attr macros
[mirror_ubuntu-hirsute-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>
27#include <linux/module.h>
d88cfffa 28#include <linux/pagemap.h>
67207b96 29#include <linux/poll.h>
5110459f 30#include <linux/ptrace.h>
cbe709c1 31#include <linux/seq_file.h>
67207b96
AB
32
33#include <asm/io.h>
34#include <asm/semaphore.h>
35#include <asm/spu.h>
b9e3bd77 36#include <asm/spu_info.h>
67207b96
AB
37#include <asm/uaccess.h>
38
39#include "spufs.h"
40
27d5bf2a
BH
41#define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
42
197b1a82
CH
43/* Simple attribute files */
44struct spufs_attr {
45 int (*get)(void *, u64 *);
46 int (*set)(void *, u64);
47 char get_buf[24]; /* enough to store a u64 and "\n\0" */
48 char set_buf[24];
49 void *data;
50 const char *fmt; /* format for read operation */
51 struct mutex mutex; /* protects access to these buffers */
52};
53
54static int spufs_attr_open(struct inode *inode, struct file *file,
55 int (*get)(void *, u64 *), int (*set)(void *, u64),
56 const char *fmt)
57{
58 struct spufs_attr *attr;
59
60 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
61 if (!attr)
62 return -ENOMEM;
63
64 attr->get = get;
65 attr->set = set;
66 attr->data = inode->i_private;
67 attr->fmt = fmt;
68 mutex_init(&attr->mutex);
69 file->private_data = attr;
70
71 return nonseekable_open(inode, file);
72}
73
74static int spufs_attr_release(struct inode *inode, struct file *file)
75{
76 kfree(file->private_data);
77 return 0;
78}
79
80static ssize_t spufs_attr_read(struct file *file, char __user *buf,
81 size_t len, loff_t *ppos)
82{
83 struct spufs_attr *attr;
84 size_t size;
85 ssize_t ret;
86
87 attr = file->private_data;
88 if (!attr->get)
89 return -EACCES;
90
91 ret = mutex_lock_interruptible(&attr->mutex);
92 if (ret)
93 return ret;
94
95 if (*ppos) { /* continued read */
96 size = strlen(attr->get_buf);
97 } else { /* first read */
98 u64 val;
99 ret = attr->get(attr->data, &val);
100 if (ret)
101 goto out;
102
103 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
104 attr->fmt, (unsigned long long)val);
105 }
106
107 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
108out:
109 mutex_unlock(&attr->mutex);
110 return ret;
111}
112
113static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
114 size_t len, loff_t *ppos)
115{
116 struct spufs_attr *attr;
117 u64 val;
118 size_t size;
119 ssize_t ret;
120
121 attr = file->private_data;
122 if (!attr->set)
123 return -EACCES;
124
125 ret = mutex_lock_interruptible(&attr->mutex);
126 if (ret)
127 return ret;
128
129 ret = -EFAULT;
130 size = min(sizeof(attr->set_buf) - 1, len);
131 if (copy_from_user(attr->set_buf, buf, size))
132 goto out;
133
134 ret = len; /* claim we got the whole input */
135 attr->set_buf[size] = '\0';
136 val = simple_strtol(attr->set_buf, NULL, 0);
137 attr->set(attr->data, val);
138out:
139 mutex_unlock(&attr->mutex);
140 return ret;
141}
142
143#define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
144static int __fops ## _open(struct inode *inode, struct file *file) \
145{ \
146 __simple_attr_check_format(__fmt, 0ull); \
147 return spufs_attr_open(inode, file, __get, __set, __fmt); \
148} \
149static struct file_operations __fops = { \
150 .owner = THIS_MODULE, \
151 .open = __fops ## _open, \
152 .release = spufs_attr_release, \
153 .read = spufs_attr_read, \
154 .write = spufs_attr_write, \
155};
156
cbe709c1 157
67207b96
AB
158static int
159spufs_mem_open(struct inode *inode, struct file *file)
160{
161 struct spufs_inode_info *i = SPUFS_I(inode);
6df10a82 162 struct spu_context *ctx = i->i_ctx;
43c2bbd9 163
47d3a5fa 164 mutex_lock(&ctx->mapping_lock);
6df10a82 165 file->private_data = ctx;
43c2bbd9
CH
166 if (!i->i_openers++)
167 ctx->local_store = inode->i_mapping;
47d3a5fa 168 mutex_unlock(&ctx->mapping_lock);
43c2bbd9
CH
169 return 0;
170}
171
172static int
173spufs_mem_release(struct inode *inode, struct file *file)
174{
175 struct spufs_inode_info *i = SPUFS_I(inode);
176 struct spu_context *ctx = i->i_ctx;
177
47d3a5fa 178 mutex_lock(&ctx->mapping_lock);
43c2bbd9
CH
179 if (!--i->i_openers)
180 ctx->local_store = NULL;
47d3a5fa 181 mutex_unlock(&ctx->mapping_lock);
67207b96
AB
182 return 0;
183}
184
bf1ab978
DGM
185static ssize_t
186__spufs_mem_read(struct spu_context *ctx, char __user *buffer,
187 size_t size, loff_t *pos)
188{
189 char *local_store = ctx->ops->get_ls(ctx);
190 return simple_read_from_buffer(buffer, size, pos, local_store,
191 LS_SIZE);
192}
193
67207b96
AB
194static ssize_t
195spufs_mem_read(struct file *file, char __user *buffer,
196 size_t size, loff_t *pos)
197{
bf1ab978 198 struct spu_context *ctx = file->private_data;
aa0ed2bd 199 ssize_t ret;
67207b96 200
8b3d6663 201 spu_acquire(ctx);
bf1ab978 202 ret = __spufs_mem_read(ctx, buffer, size, pos);
8b3d6663 203 spu_release(ctx);
67207b96
AB
204 return ret;
205}
206
207static ssize_t
208spufs_mem_write(struct file *file, const char __user *buffer,
aa0ed2bd 209 size_t size, loff_t *ppos)
67207b96
AB
210{
211 struct spu_context *ctx = file->private_data;
8b3d6663 212 char *local_store;
aa0ed2bd 213 loff_t pos = *ppos;
8b3d6663 214 int ret;
67207b96 215
aa0ed2bd
AB
216 if (pos < 0)
217 return -EINVAL;
218 if (pos > LS_SIZE)
67207b96 219 return -EFBIG;
aa0ed2bd
AB
220 if (size > LS_SIZE - pos)
221 size = LS_SIZE - pos;
8b3d6663
AB
222
223 spu_acquire(ctx);
8b3d6663 224 local_store = ctx->ops->get_ls(ctx);
aa0ed2bd 225 ret = copy_from_user(local_store + pos, buffer, size);
8b3d6663 226 spu_release(ctx);
aa0ed2bd
AB
227
228 if (ret)
229 return -EFAULT;
230 *ppos = pos + size;
231 return size;
67207b96
AB
232}
233
78bde53e
BH
234static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
235 unsigned long address)
8b3d6663 236{
f1fa74f4
BH
237 struct spu_context *ctx = vma->vm_file->private_data;
238 unsigned long pfn, offset, addr0 = address;
239#ifdef CONFIG_SPU_FS_64K_LS
240 struct spu_state *csa = &ctx->csa;
241 int psize;
242
243 /* Check what page size we are using */
244 psize = get_slice_psize(vma->vm_mm, address);
245
246 /* Some sanity checking */
247 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
248
249 /* Wow, 64K, cool, we need to align the address though */
250 if (csa->use_big_pages) {
251 BUG_ON(vma->vm_start & 0xffff);
252 address &= ~0xfffful;
253 }
254#endif /* CONFIG_SPU_FS_64K_LS */
8b3d6663 255
f1fa74f4 256 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
128b8546
MN
257 if (offset >= LS_SIZE)
258 return NOPFN_SIGBUS;
259
f1fa74f4
BH
260 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
261 addr0, address, offset);
262
8b3d6663
AB
263 spu_acquire(ctx);
264
ac91cb8d
AB
265 if (ctx->state == SPU_STATE_SAVED) {
266 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
932f535d 267 & ~_PAGE_NO_CACHE);
78bde53e 268 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
ac91cb8d
AB
269 } else {
270 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
78bde53e
BH
271 | _PAGE_NO_CACHE);
272 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
ac91cb8d 273 }
78bde53e 274 vm_insert_pfn(vma, address, pfn);
8b3d6663 275
78bde53e 276 spu_release(ctx);
8b3d6663 277
78bde53e 278 return NOPFN_REFAULT;
8b3d6663
AB
279}
280
78bde53e 281
8b3d6663 282static struct vm_operations_struct spufs_mem_mmap_vmops = {
78bde53e 283 .nopfn = spufs_mem_mmap_nopfn,
8b3d6663
AB
284};
285
f1fa74f4 286static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
67207b96 287{
f1fa74f4
BH
288#ifdef CONFIG_SPU_FS_64K_LS
289 struct spu_context *ctx = file->private_data;
290 struct spu_state *csa = &ctx->csa;
291
292 /* Sanity check VMA alignment */
293 if (csa->use_big_pages) {
294 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
295 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
296 vma->vm_pgoff);
297 if (vma->vm_start & 0xffff)
298 return -EINVAL;
299 if (vma->vm_pgoff & 0xf)
300 return -EINVAL;
301 }
302#endif /* CONFIG_SPU_FS_64K_LS */
303
8b3d6663
AB
304 if (!(vma->vm_flags & VM_SHARED))
305 return -EINVAL;
67207b96 306
78bde53e 307 vma->vm_flags |= VM_IO | VM_PFNMAP;
8b3d6663
AB
308 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
309 | _PAGE_NO_CACHE);
310
311 vma->vm_ops = &spufs_mem_mmap_vmops;
67207b96
AB
312 return 0;
313}
314
f1fa74f4 315#ifdef CONFIG_SPU_FS_64K_LS
1238819a
SS
316static unsigned long spufs_get_unmapped_area(struct file *file,
317 unsigned long addr, unsigned long len, unsigned long pgoff,
318 unsigned long flags)
f1fa74f4
BH
319{
320 struct spu_context *ctx = file->private_data;
321 struct spu_state *csa = &ctx->csa;
322
323 /* If not using big pages, fallback to normal MM g_u_a */
324 if (!csa->use_big_pages)
325 return current->mm->get_unmapped_area(file, addr, len,
326 pgoff, flags);
327
328 /* Else, try to obtain a 64K pages slice */
329 return slice_get_unmapped_area(addr, len, flags,
330 MMU_PAGE_64K, 1, 0);
331}
332#endif /* CONFIG_SPU_FS_64K_LS */
333
5dfe4c96 334static const struct file_operations spufs_mem_fops = {
7022543e
JK
335 .open = spufs_mem_open,
336 .release = spufs_mem_release,
337 .read = spufs_mem_read,
338 .write = spufs_mem_write,
339 .llseek = generic_file_llseek,
340 .mmap = spufs_mem_mmap,
f1fa74f4
BH
341#ifdef CONFIG_SPU_FS_64K_LS
342 .get_unmapped_area = spufs_get_unmapped_area,
343#endif
8b3d6663
AB
344};
345
78bde53e 346static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
6df10a82 347 unsigned long address,
78bde53e 348 unsigned long ps_offs,
27d5bf2a 349 unsigned long ps_size)
6df10a82 350{
6df10a82 351 struct spu_context *ctx = vma->vm_file->private_data;
78bde53e 352 unsigned long area, offset = address - vma->vm_start;
6df10a82
MN
353
354 offset += vma->vm_pgoff << PAGE_SHIFT;
27d5bf2a 355 if (offset >= ps_size)
78bde53e 356 return NOPFN_SIGBUS;
6df10a82 357
33bfd7a7
AB
358 /*
359 * We have to wait for context to be loaded before we have
360 * pages to hand out to the user, but we don't want to wait
361 * with the mmap_sem held.
362 * It is possible to drop the mmap_sem here, but then we need
363 * to return NOPFN_REFAULT because the mappings may have
364 * hanged.
78bde53e 365 */
33bfd7a7
AB
366 spu_acquire(ctx);
367 if (ctx->state == SPU_STATE_SAVED) {
368 up_read(&current->mm->mmap_sem);
369 spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
370 down_read(&current->mm->mmap_sem);
371 goto out;
372 }
6df10a82
MN
373
374 area = ctx->spu->problem_phys + ps_offs;
78bde53e 375 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
33bfd7a7
AB
376
377out:
6df10a82
MN
378 spu_release(ctx);
379
78bde53e 380 return NOPFN_REFAULT;
6df10a82
MN
381}
382
27d5bf2a 383#if SPUFS_MMAP_4K
78bde53e
BH
384static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
385 unsigned long address)
6df10a82 386{
78bde53e 387 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
6df10a82
MN
388}
389
390static struct vm_operations_struct spufs_cntl_mmap_vmops = {
78bde53e 391 .nopfn = spufs_cntl_mmap_nopfn,
6df10a82
MN
392};
393
394/*
395 * mmap support for problem state control area [0x4000 - 0x4fff].
6df10a82
MN
396 */
397static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
398{
399 if (!(vma->vm_flags & VM_SHARED))
400 return -EINVAL;
401
78bde53e 402 vma->vm_flags |= VM_IO | VM_PFNMAP;
6df10a82 403 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
23cc7701 404 | _PAGE_NO_CACHE | _PAGE_GUARDED);
6df10a82
MN
405
406 vma->vm_ops = &spufs_cntl_mmap_vmops;
407 return 0;
408}
27d5bf2a
BH
409#else /* SPUFS_MMAP_4K */
410#define spufs_cntl_mmap NULL
411#endif /* !SPUFS_MMAP_4K */
6df10a82 412
197b1a82 413static int spufs_cntl_get(void *data, u64 *val)
6df10a82 414{
e1dbff2b 415 struct spu_context *ctx = data;
6df10a82 416
e1dbff2b 417 spu_acquire(ctx);
197b1a82 418 *val = ctx->ops->status_read(ctx);
e1dbff2b
AB
419 spu_release(ctx);
420
197b1a82 421 return 0;
6df10a82
MN
422}
423
197b1a82 424static int spufs_cntl_set(void *data, u64 val)
6df10a82 425{
e1dbff2b
AB
426 struct spu_context *ctx = data;
427
428 spu_acquire(ctx);
429 ctx->ops->runcntl_write(ctx, val);
430 spu_release(ctx);
197b1a82
CH
431
432 return 0;
6df10a82
MN
433}
434
e1dbff2b 435static int spufs_cntl_open(struct inode *inode, struct file *file)
6df10a82 436{
e1dbff2b
AB
437 struct spufs_inode_info *i = SPUFS_I(inode);
438 struct spu_context *ctx = i->i_ctx;
439
47d3a5fa 440 mutex_lock(&ctx->mapping_lock);
e1dbff2b 441 file->private_data = ctx;
43c2bbd9
CH
442 if (!i->i_openers++)
443 ctx->cntl = inode->i_mapping;
47d3a5fa 444 mutex_unlock(&ctx->mapping_lock);
197b1a82 445 return spufs_attr_open(inode, file, spufs_cntl_get,
e1dbff2b 446 spufs_cntl_set, "0x%08lx");
6df10a82
MN
447}
448
43c2bbd9
CH
449static int
450spufs_cntl_release(struct inode *inode, struct file *file)
451{
452 struct spufs_inode_info *i = SPUFS_I(inode);
453 struct spu_context *ctx = i->i_ctx;
454
197b1a82 455 spufs_attr_release(inode, file);
43c2bbd9 456
47d3a5fa 457 mutex_lock(&ctx->mapping_lock);
43c2bbd9
CH
458 if (!--i->i_openers)
459 ctx->cntl = NULL;
47d3a5fa 460 mutex_unlock(&ctx->mapping_lock);
43c2bbd9
CH
461 return 0;
462}
463
5dfe4c96 464static const struct file_operations spufs_cntl_fops = {
6df10a82 465 .open = spufs_cntl_open,
43c2bbd9 466 .release = spufs_cntl_release,
197b1a82
CH
467 .read = spufs_attr_read,
468 .write = spufs_attr_write,
6df10a82 469 .mmap = spufs_cntl_mmap,
6df10a82
MN
470};
471
8b3d6663
AB
472static int
473spufs_regs_open(struct inode *inode, struct file *file)
474{
475 struct spufs_inode_info *i = SPUFS_I(inode);
476 file->private_data = i->i_ctx;
477 return 0;
478}
479
bf1ab978
DGM
480static ssize_t
481__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
482 size_t size, loff_t *pos)
483{
484 struct spu_lscsa *lscsa = ctx->csa.lscsa;
485 return simple_read_from_buffer(buffer, size, pos,
486 lscsa->gprs, sizeof lscsa->gprs);
487}
488
8b3d6663
AB
489static ssize_t
490spufs_regs_read(struct file *file, char __user *buffer,
491 size_t size, loff_t *pos)
492{
8b3d6663 493 int ret;
bf1ab978 494 struct spu_context *ctx = file->private_data;
8b3d6663
AB
495
496 spu_acquire_saved(ctx);
bf1ab978 497 ret = __spufs_regs_read(ctx, buffer, size, pos);
27b1ea09 498 spu_release_saved(ctx);
8b3d6663
AB
499 return ret;
500}
501
502static ssize_t
503spufs_regs_write(struct file *file, const char __user *buffer,
504 size_t size, loff_t *pos)
505{
506 struct spu_context *ctx = file->private_data;
507 struct spu_lscsa *lscsa = ctx->csa.lscsa;
508 int ret;
509
510 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
511 if (size <= 0)
512 return -EFBIG;
513 *pos += size;
514
515 spu_acquire_saved(ctx);
516
517 ret = copy_from_user(lscsa->gprs + *pos - size,
518 buffer, size) ? -EFAULT : size;
519
27b1ea09 520 spu_release_saved(ctx);
8b3d6663
AB
521 return ret;
522}
523
5dfe4c96 524static const struct file_operations spufs_regs_fops = {
8b3d6663
AB
525 .open = spufs_regs_open,
526 .read = spufs_regs_read,
527 .write = spufs_regs_write,
67207b96
AB
528 .llseek = generic_file_llseek,
529};
530
bf1ab978
DGM
531static ssize_t
532__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
533 size_t size, loff_t * pos)
534{
535 struct spu_lscsa *lscsa = ctx->csa.lscsa;
536 return simple_read_from_buffer(buffer, size, pos,
537 &lscsa->fpcr, sizeof(lscsa->fpcr));
538}
539
8b3d6663
AB
540static ssize_t
541spufs_fpcr_read(struct file *file, char __user * buffer,
542 size_t size, loff_t * pos)
543{
8b3d6663 544 int ret;
bf1ab978 545 struct spu_context *ctx = file->private_data;
8b3d6663
AB
546
547 spu_acquire_saved(ctx);
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
561 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
562 if (size <= 0)
563 return -EFBIG;
564 *pos += size;
565
566 spu_acquire_saved(ctx);
567
568 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
569 buffer, size) ? -EFAULT : size;
570
27b1ea09 571 spu_release_saved(ctx);
8b3d6663
AB
572 return ret;
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
8b3d6663 614 spu_acquire(ctx);
274cef5e 615 for (count = 0; (count + 4) <= len; count += 4, udata++) {
cdcc89bb
AB
616 int ret;
617 ret = ctx->ops->mbox_read(ctx, &mbox_data);
618 if (ret == 0)
619 break;
620
621 /*
622 * at the end of the mapped area, we can fault
623 * but still need to return the data we have
624 * read successfully so far.
625 */
626 ret = __put_user(mbox_data, udata);
627 if (ret) {
628 if (!count)
629 count = -EFAULT;
630 break;
631 }
632 }
8b3d6663 633 spu_release(ctx);
67207b96 634
cdcc89bb
AB
635 if (!count)
636 count = -EAGAIN;
67207b96 637
cdcc89bb 638 return count;
67207b96
AB
639}
640
5dfe4c96 641static const struct file_operations spufs_mbox_fops = {
67207b96
AB
642 .open = spufs_pipe_open,
643 .read = spufs_mbox_read,
644};
645
646static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
647 size_t len, loff_t *pos)
648{
8b3d6663 649 struct spu_context *ctx = file->private_data;
67207b96
AB
650 u32 mbox_stat;
651
652 if (len < 4)
653 return -EINVAL;
654
8b3d6663
AB
655 spu_acquire(ctx);
656
657 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
658
659 spu_release(ctx);
67207b96
AB
660
661 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
662 return -EFAULT;
663
664 return 4;
665}
666
5dfe4c96 667static const struct file_operations spufs_mbox_stat_fops = {
67207b96
AB
668 .open = spufs_pipe_open,
669 .read = spufs_mbox_stat_read,
670};
671
672/* low-level ibox access function */
8b3d6663 673size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
67207b96 674{
8b3d6663
AB
675 return ctx->ops->ibox_read(ctx, data);
676}
67207b96 677
8b3d6663
AB
678static int spufs_ibox_fasync(int fd, struct file *file, int on)
679{
680 struct spu_context *ctx = file->private_data;
67207b96 681
8b3d6663 682 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
67207b96 683}
67207b96 684
8b3d6663
AB
685/* interrupt-level ibox callback function. */
686void spufs_ibox_callback(struct spu *spu)
67207b96 687{
8b3d6663
AB
688 struct spu_context *ctx = spu->ctx;
689
e65c2f6f
LB
690 if (!ctx)
691 return;
692
8b3d6663
AB
693 wake_up_all(&ctx->ibox_wq);
694 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
67207b96
AB
695}
696
cdcc89bb
AB
697/*
698 * Read as many bytes from the interrupt mailbox as possible, until
699 * one of the conditions becomes true:
700 *
701 * - no more data available in the mailbox
702 * - end of the user provided buffer
703 * - end of the mapped area
704 *
705 * If the file is opened without O_NONBLOCK, we wait here until
706 * any data is available, but return when we have been able to
707 * read something.
708 */
67207b96
AB
709static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
710 size_t len, loff_t *pos)
711{
8b3d6663 712 struct spu_context *ctx = file->private_data;
cdcc89bb
AB
713 u32 ibox_data, __user *udata;
714 ssize_t count;
67207b96
AB
715
716 if (len < 4)
717 return -EINVAL;
718
cdcc89bb
AB
719 if (!access_ok(VERIFY_WRITE, buf, len))
720 return -EFAULT;
721
722 udata = (void __user *)buf;
723
8b3d6663 724 spu_acquire(ctx);
67207b96 725
cdcc89bb
AB
726 /* wait only for the first element */
727 count = 0;
67207b96 728 if (file->f_flags & O_NONBLOCK) {
8b3d6663 729 if (!spu_ibox_read(ctx, &ibox_data))
cdcc89bb 730 count = -EAGAIN;
67207b96 731 } else {
cdcc89bb 732 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
67207b96 733 }
cdcc89bb
AB
734 if (count)
735 goto out;
67207b96 736
cdcc89bb
AB
737 /* if we can't write at all, return -EFAULT */
738 count = __put_user(ibox_data, udata);
739 if (count)
740 goto out;
8b3d6663 741
cdcc89bb
AB
742 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
743 int ret;
744 ret = ctx->ops->ibox_read(ctx, &ibox_data);
745 if (ret == 0)
746 break;
747 /*
748 * at the end of the mapped area, we can fault
749 * but still need to return the data we have
750 * read successfully so far.
751 */
752 ret = __put_user(ibox_data, udata);
753 if (ret)
754 break;
755 }
67207b96 756
cdcc89bb
AB
757out:
758 spu_release(ctx);
67207b96 759
cdcc89bb 760 return count;
67207b96
AB
761}
762
763static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
764{
8b3d6663 765 struct spu_context *ctx = file->private_data;
67207b96
AB
766 unsigned int mask;
767
8b3d6663 768 poll_wait(file, &ctx->ibox_wq, wait);
67207b96 769
3a843d7c
AB
770 spu_acquire(ctx);
771 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
772 spu_release(ctx);
67207b96
AB
773
774 return mask;
775}
776
5dfe4c96 777static const struct file_operations spufs_ibox_fops = {
67207b96
AB
778 .open = spufs_pipe_open,
779 .read = spufs_ibox_read,
780 .poll = spufs_ibox_poll,
781 .fasync = spufs_ibox_fasync,
782};
783
784static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
785 size_t len, loff_t *pos)
786{
8b3d6663 787 struct spu_context *ctx = file->private_data;
67207b96
AB
788 u32 ibox_stat;
789
790 if (len < 4)
791 return -EINVAL;
792
8b3d6663
AB
793 spu_acquire(ctx);
794 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
795 spu_release(ctx);
67207b96
AB
796
797 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
798 return -EFAULT;
799
800 return 4;
801}
802
5dfe4c96 803static const struct file_operations spufs_ibox_stat_fops = {
67207b96
AB
804 .open = spufs_pipe_open,
805 .read = spufs_ibox_stat_read,
806};
807
808/* low-level mailbox write */
8b3d6663 809size_t spu_wbox_write(struct spu_context *ctx, u32 data)
67207b96 810{
8b3d6663
AB
811 return ctx->ops->wbox_write(ctx, data);
812}
67207b96 813
8b3d6663
AB
814static int spufs_wbox_fasync(int fd, struct file *file, int on)
815{
816 struct spu_context *ctx = file->private_data;
817 int ret;
67207b96 818
8b3d6663 819 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
67207b96 820
67207b96
AB
821 return ret;
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
e65c2f6f
LB
829 if (!ctx)
830 return;
831
8b3d6663
AB
832 wake_up_all(&ctx->wbox_wq);
833 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
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
845 * space is availabyl, but return when we have been able to
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
AB
858 udata = (void __user *)buf;
859 if (!access_ok(VERIFY_READ, buf, len))
860 return -EFAULT;
861
862 if (__get_user(wbox_data, udata))
67207b96
AB
863 return -EFAULT;
864
8b3d6663
AB
865 spu_acquire(ctx);
866
cdcc89bb
AB
867 /*
868 * make sure we can at least write one element, by waiting
869 * in case of !O_NONBLOCK
870 */
871 count = 0;
67207b96 872 if (file->f_flags & O_NONBLOCK) {
8b3d6663 873 if (!spu_wbox_write(ctx, wbox_data))
cdcc89bb 874 count = -EAGAIN;
67207b96 875 } else {
cdcc89bb 876 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
67207b96
AB
877 }
878
cdcc89bb
AB
879 if (count)
880 goto out;
8b3d6663 881
96de0e25 882 /* write as much as possible */
cdcc89bb
AB
883 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
884 int ret;
885 ret = __get_user(wbox_data, udata);
886 if (ret)
887 break;
888
889 ret = spu_wbox_write(ctx, wbox_data);
890 if (ret == 0)
891 break;
892 }
893
894out:
895 spu_release(ctx);
896 return count;
67207b96
AB
897}
898
899static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
900{
8b3d6663 901 struct spu_context *ctx = file->private_data;
67207b96
AB
902 unsigned int mask;
903
8b3d6663 904 poll_wait(file, &ctx->wbox_wq, wait);
67207b96 905
3a843d7c
AB
906 spu_acquire(ctx);
907 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
908 spu_release(ctx);
67207b96
AB
909
910 return mask;
911}
912
5dfe4c96 913static const struct file_operations spufs_wbox_fops = {
67207b96
AB
914 .open = spufs_pipe_open,
915 .write = spufs_wbox_write,
916 .poll = spufs_wbox_poll,
917 .fasync = spufs_wbox_fasync,
918};
919
920static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
921 size_t len, loff_t *pos)
922{
8b3d6663 923 struct spu_context *ctx = file->private_data;
67207b96
AB
924 u32 wbox_stat;
925
926 if (len < 4)
927 return -EINVAL;
928
8b3d6663
AB
929 spu_acquire(ctx);
930 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
931 spu_release(ctx);
67207b96
AB
932
933 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
934 return -EFAULT;
935
936 return 4;
937}
938
5dfe4c96 939static const struct file_operations spufs_wbox_stat_fops = {
67207b96
AB
940 .open = spufs_pipe_open,
941 .read = spufs_wbox_stat_read,
942};
943
6df10a82
MN
944static int spufs_signal1_open(struct inode *inode, struct file *file)
945{
946 struct spufs_inode_info *i = SPUFS_I(inode);
947 struct spu_context *ctx = i->i_ctx;
43c2bbd9 948
47d3a5fa 949 mutex_lock(&ctx->mapping_lock);
6df10a82 950 file->private_data = ctx;
43c2bbd9
CH
951 if (!i->i_openers++)
952 ctx->signal1 = inode->i_mapping;
47d3a5fa 953 mutex_unlock(&ctx->mapping_lock);
6df10a82
MN
954 return nonseekable_open(inode, file);
955}
956
43c2bbd9
CH
957static int
958spufs_signal1_release(struct inode *inode, struct file *file)
959{
960 struct spufs_inode_info *i = SPUFS_I(inode);
961 struct spu_context *ctx = i->i_ctx;
962
47d3a5fa 963 mutex_lock(&ctx->mapping_lock);
43c2bbd9
CH
964 if (!--i->i_openers)
965 ctx->signal1 = NULL;
47d3a5fa 966 mutex_unlock(&ctx->mapping_lock);
43c2bbd9
CH
967 return 0;
968}
969
bf1ab978 970static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
67207b96
AB
971 size_t len, loff_t *pos)
972{
17f88ceb 973 int ret = 0;
67207b96
AB
974 u32 data;
975
67207b96
AB
976 if (len < 4)
977 return -EINVAL;
978
17f88ceb
DGM
979 if (ctx->csa.spu_chnlcnt_RW[3]) {
980 data = ctx->csa.spu_chnldata_RW[3];
981 ret = 4;
982 }
8b3d6663 983
17f88ceb
DGM
984 if (!ret)
985 goto out;
986
67207b96
AB
987 if (copy_to_user(buf, &data, 4))
988 return -EFAULT;
989
17f88ceb
DGM
990out:
991 return ret;
67207b96
AB
992}
993
bf1ab978
DGM
994static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
995 size_t len, loff_t *pos)
996{
997 int ret;
998 struct spu_context *ctx = file->private_data;
999
1000 spu_acquire_saved(ctx);
1001 ret = __spufs_signal1_read(ctx, buf, len, pos);
27b1ea09 1002 spu_release_saved(ctx);
bf1ab978
DGM
1003
1004 return ret;
1005}
1006
67207b96
AB
1007static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1008 size_t len, loff_t *pos)
1009{
1010 struct spu_context *ctx;
67207b96
AB
1011 u32 data;
1012
1013 ctx = file->private_data;
67207b96
AB
1014
1015 if (len < 4)
1016 return -EINVAL;
1017
1018 if (copy_from_user(&data, buf, 4))
1019 return -EFAULT;
1020
8b3d6663
AB
1021 spu_acquire(ctx);
1022 ctx->ops->signal1_write(ctx, data);
1023 spu_release(ctx);
67207b96
AB
1024
1025 return 4;
1026}
1027
78bde53e
BH
1028static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1029 unsigned long address)
6df10a82 1030{
27d5bf2a 1031#if PAGE_SIZE == 0x1000
78bde53e 1032 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
27d5bf2a
BH
1033#elif PAGE_SIZE == 0x10000
1034 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1035 * signal 1 and 2 area
1036 */
78bde53e 1037 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
27d5bf2a
BH
1038#else
1039#error unsupported page size
1040#endif
6df10a82
MN
1041}
1042
1043static struct vm_operations_struct spufs_signal1_mmap_vmops = {
78bde53e 1044 .nopfn = spufs_signal1_mmap_nopfn,
6df10a82
MN
1045};
1046
1047static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1048{
1049 if (!(vma->vm_flags & VM_SHARED))
1050 return -EINVAL;
1051
78bde53e 1052 vma->vm_flags |= VM_IO | VM_PFNMAP;
6df10a82 1053 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
23cc7701 1054 | _PAGE_NO_CACHE | _PAGE_GUARDED);
6df10a82
MN
1055
1056 vma->vm_ops = &spufs_signal1_mmap_vmops;
1057 return 0;
1058}
6df10a82 1059
5dfe4c96 1060static const struct file_operations spufs_signal1_fops = {
6df10a82 1061 .open = spufs_signal1_open,
43c2bbd9 1062 .release = spufs_signal1_release,
67207b96
AB
1063 .read = spufs_signal1_read,
1064 .write = spufs_signal1_write,
6df10a82 1065 .mmap = spufs_signal1_mmap,
67207b96
AB
1066};
1067
d054b36f
JK
1068static const struct file_operations spufs_signal1_nosched_fops = {
1069 .open = spufs_signal1_open,
1070 .release = spufs_signal1_release,
1071 .write = spufs_signal1_write,
1072 .mmap = spufs_signal1_mmap,
1073};
1074
6df10a82
MN
1075static int spufs_signal2_open(struct inode *inode, struct file *file)
1076{
1077 struct spufs_inode_info *i = SPUFS_I(inode);
1078 struct spu_context *ctx = i->i_ctx;
43c2bbd9 1079
47d3a5fa 1080 mutex_lock(&ctx->mapping_lock);
6df10a82 1081 file->private_data = ctx;
43c2bbd9
CH
1082 if (!i->i_openers++)
1083 ctx->signal2 = inode->i_mapping;
47d3a5fa 1084 mutex_unlock(&ctx->mapping_lock);
6df10a82
MN
1085 return nonseekable_open(inode, file);
1086}
1087
43c2bbd9
CH
1088static int
1089spufs_signal2_release(struct inode *inode, struct file *file)
1090{
1091 struct spufs_inode_info *i = SPUFS_I(inode);
1092 struct spu_context *ctx = i->i_ctx;
1093
47d3a5fa 1094 mutex_lock(&ctx->mapping_lock);
43c2bbd9
CH
1095 if (!--i->i_openers)
1096 ctx->signal2 = NULL;
47d3a5fa 1097 mutex_unlock(&ctx->mapping_lock);
43c2bbd9
CH
1098 return 0;
1099}
1100
bf1ab978 1101static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
67207b96
AB
1102 size_t len, loff_t *pos)
1103{
17f88ceb 1104 int ret = 0;
67207b96
AB
1105 u32 data;
1106
67207b96
AB
1107 if (len < 4)
1108 return -EINVAL;
1109
17f88ceb
DGM
1110 if (ctx->csa.spu_chnlcnt_RW[4]) {
1111 data = ctx->csa.spu_chnldata_RW[4];
1112 ret = 4;
1113 }
8b3d6663 1114
17f88ceb
DGM
1115 if (!ret)
1116 goto out;
1117
67207b96
AB
1118 if (copy_to_user(buf, &data, 4))
1119 return -EFAULT;
1120
17f88ceb 1121out:
bf1ab978
DGM
1122 return ret;
1123}
1124
1125static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1126 size_t len, loff_t *pos)
1127{
1128 struct spu_context *ctx = file->private_data;
1129 int ret;
1130
1131 spu_acquire_saved(ctx);
1132 ret = __spufs_signal2_read(ctx, buf, len, pos);
27b1ea09 1133 spu_release_saved(ctx);
bf1ab978
DGM
1134
1135 return ret;
67207b96
AB
1136}
1137
1138static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1139 size_t len, loff_t *pos)
1140{
1141 struct spu_context *ctx;
67207b96
AB
1142 u32 data;
1143
1144 ctx = file->private_data;
67207b96
AB
1145
1146 if (len < 4)
1147 return -EINVAL;
1148
1149 if (copy_from_user(&data, buf, 4))
1150 return -EFAULT;
1151
8b3d6663
AB
1152 spu_acquire(ctx);
1153 ctx->ops->signal2_write(ctx, data);
1154 spu_release(ctx);
67207b96
AB
1155
1156 return 4;
1157}
1158
27d5bf2a 1159#if SPUFS_MMAP_4K
78bde53e
BH
1160static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1161 unsigned long address)
6df10a82 1162{
27d5bf2a 1163#if PAGE_SIZE == 0x1000
78bde53e 1164 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
27d5bf2a
BH
1165#elif PAGE_SIZE == 0x10000
1166 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1167 * signal 1 and 2 area
1168 */
78bde53e 1169 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
27d5bf2a
BH
1170#else
1171#error unsupported page size
1172#endif
6df10a82
MN
1173}
1174
1175static struct vm_operations_struct spufs_signal2_mmap_vmops = {
78bde53e 1176 .nopfn = spufs_signal2_mmap_nopfn,
6df10a82
MN
1177};
1178
1179static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1180{
1181 if (!(vma->vm_flags & VM_SHARED))
1182 return -EINVAL;
1183
78bde53e 1184 vma->vm_flags |= VM_IO | VM_PFNMAP;
6df10a82 1185 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
23cc7701 1186 | _PAGE_NO_CACHE | _PAGE_GUARDED);
6df10a82
MN
1187
1188 vma->vm_ops = &spufs_signal2_mmap_vmops;
1189 return 0;
1190}
27d5bf2a
BH
1191#else /* SPUFS_MMAP_4K */
1192#define spufs_signal2_mmap NULL
1193#endif /* !SPUFS_MMAP_4K */
6df10a82 1194
5dfe4c96 1195static const struct file_operations spufs_signal2_fops = {
6df10a82 1196 .open = spufs_signal2_open,
43c2bbd9 1197 .release = spufs_signal2_release,
67207b96
AB
1198 .read = spufs_signal2_read,
1199 .write = spufs_signal2_write,
6df10a82 1200 .mmap = spufs_signal2_mmap,
67207b96
AB
1201};
1202
d054b36f
JK
1203static const struct file_operations spufs_signal2_nosched_fops = {
1204 .open = spufs_signal2_open,
1205 .release = spufs_signal2_release,
1206 .write = spufs_signal2_write,
1207 .mmap = spufs_signal2_mmap,
1208};
1209
104f0cc2
ME
1210/*
1211 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1212 * work of acquiring (or not) the SPU context before calling through
1213 * to the actual get routine. The set routine is called directly.
1214 */
1215#define SPU_ATTR_NOACQUIRE 0
1216#define SPU_ATTR_ACQUIRE 1
1217#define SPU_ATTR_ACQUIRE_SAVED 2
1218
1219#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
197b1a82 1220static int __##__get(void *data, u64 *val) \
104f0cc2
ME
1221{ \
1222 struct spu_context *ctx = data; \
104f0cc2
ME
1223 \
1224 if (__acquire == SPU_ATTR_ACQUIRE) { \
1225 spu_acquire(ctx); \
197b1a82 1226 *val = __get(ctx); \
104f0cc2
ME
1227 spu_release(ctx); \
1228 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1229 spu_acquire_saved(ctx); \
197b1a82 1230 *val = __get(ctx); \
104f0cc2
ME
1231 spu_release_saved(ctx); \
1232 } else \
197b1a82 1233 *val = __get(ctx); \
104f0cc2 1234 \
197b1a82 1235 return 0; \
104f0cc2 1236} \
197b1a82 1237DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
104f0cc2 1238
197b1a82 1239static int spufs_signal1_type_set(void *data, u64 val)
67207b96
AB
1240{
1241 struct spu_context *ctx = data;
67207b96 1242
8b3d6663
AB
1243 spu_acquire(ctx);
1244 ctx->ops->signal1_type_set(ctx, val);
1245 spu_release(ctx);
197b1a82
CH
1246
1247 return 0;
67207b96
AB
1248}
1249
104f0cc2 1250static u64 spufs_signal1_type_get(struct spu_context *ctx)
bf1ab978 1251{
bf1ab978
DGM
1252 return ctx->ops->signal1_type_get(ctx);
1253}
104f0cc2
ME
1254DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1255 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
bf1ab978 1256
67207b96 1257
197b1a82 1258static int spufs_signal2_type_set(void *data, u64 val)
67207b96
AB
1259{
1260 struct spu_context *ctx = data;
67207b96 1261
8b3d6663
AB
1262 spu_acquire(ctx);
1263 ctx->ops->signal2_type_set(ctx, val);
1264 spu_release(ctx);
197b1a82
CH
1265
1266 return 0;
67207b96
AB
1267}
1268
104f0cc2 1269static u64 spufs_signal2_type_get(struct spu_context *ctx)
bf1ab978 1270{
bf1ab978
DGM
1271 return ctx->ops->signal2_type_get(ctx);
1272}
104f0cc2
ME
1273DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1274 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
67207b96 1275
27d5bf2a 1276#if SPUFS_MMAP_4K
78bde53e
BH
1277static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1278 unsigned long address)
d9379c4b 1279{
78bde53e 1280 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
d9379c4b
AB
1281}
1282
1283static struct vm_operations_struct spufs_mss_mmap_vmops = {
78bde53e 1284 .nopfn = spufs_mss_mmap_nopfn,
d9379c4b
AB
1285};
1286
1287/*
1288 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
d9379c4b
AB
1289 */
1290static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1291{
1292 if (!(vma->vm_flags & VM_SHARED))
1293 return -EINVAL;
1294
78bde53e 1295 vma->vm_flags |= VM_IO | VM_PFNMAP;
d9379c4b 1296 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
23cc7701 1297 | _PAGE_NO_CACHE | _PAGE_GUARDED);
d9379c4b
AB
1298
1299 vma->vm_ops = &spufs_mss_mmap_vmops;
1300 return 0;
1301}
27d5bf2a
BH
1302#else /* SPUFS_MMAP_4K */
1303#define spufs_mss_mmap NULL
1304#endif /* !SPUFS_MMAP_4K */
d9379c4b
AB
1305
1306static int spufs_mss_open(struct inode *inode, struct file *file)
1307{
1308 struct spufs_inode_info *i = SPUFS_I(inode);
17e0e270 1309 struct spu_context *ctx = i->i_ctx;
d9379c4b
AB
1310
1311 file->private_data = i->i_ctx;
43c2bbd9 1312
47d3a5fa 1313 mutex_lock(&ctx->mapping_lock);
43c2bbd9
CH
1314 if (!i->i_openers++)
1315 ctx->mss = inode->i_mapping;
47d3a5fa 1316 mutex_unlock(&ctx->mapping_lock);
d9379c4b
AB
1317 return nonseekable_open(inode, file);
1318}
1319
43c2bbd9
CH
1320static int
1321spufs_mss_release(struct inode *inode, struct file *file)
1322{
1323 struct spufs_inode_info *i = SPUFS_I(inode);
1324 struct spu_context *ctx = i->i_ctx;
1325
47d3a5fa 1326 mutex_lock(&ctx->mapping_lock);
43c2bbd9
CH
1327 if (!--i->i_openers)
1328 ctx->mss = NULL;
47d3a5fa 1329 mutex_unlock(&ctx->mapping_lock);
43c2bbd9
CH
1330 return 0;
1331}
1332
5dfe4c96 1333static const struct file_operations spufs_mss_fops = {
d9379c4b 1334 .open = spufs_mss_open,
43c2bbd9 1335 .release = spufs_mss_release,
d9379c4b 1336 .mmap = spufs_mss_mmap,
27d5bf2a
BH
1337};
1338
78bde53e
BH
1339static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1340 unsigned long address)
27d5bf2a 1341{
78bde53e 1342 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
27d5bf2a
BH
1343}
1344
1345static struct vm_operations_struct spufs_psmap_mmap_vmops = {
78bde53e 1346 .nopfn = spufs_psmap_mmap_nopfn,
27d5bf2a
BH
1347};
1348
1349/*
1350 * mmap support for full problem state area [0x00000 - 0x1ffff].
1351 */
1352static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1353{
1354 if (!(vma->vm_flags & VM_SHARED))
1355 return -EINVAL;
1356
78bde53e 1357 vma->vm_flags |= VM_IO | VM_PFNMAP;
27d5bf2a
BH
1358 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1359 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1360
1361 vma->vm_ops = &spufs_psmap_mmap_vmops;
1362 return 0;
1363}
1364
1365static int spufs_psmap_open(struct inode *inode, struct file *file)
1366{
1367 struct spufs_inode_info *i = SPUFS_I(inode);
17e0e270 1368 struct spu_context *ctx = i->i_ctx;
27d5bf2a 1369
47d3a5fa 1370 mutex_lock(&ctx->mapping_lock);
27d5bf2a 1371 file->private_data = i->i_ctx;
43c2bbd9
CH
1372 if (!i->i_openers++)
1373 ctx->psmap = inode->i_mapping;
47d3a5fa 1374 mutex_unlock(&ctx->mapping_lock);
27d5bf2a
BH
1375 return nonseekable_open(inode, file);
1376}
1377
43c2bbd9
CH
1378static int
1379spufs_psmap_release(struct inode *inode, struct file *file)
1380{
1381 struct spufs_inode_info *i = SPUFS_I(inode);
1382 struct spu_context *ctx = i->i_ctx;
1383
47d3a5fa 1384 mutex_lock(&ctx->mapping_lock);
43c2bbd9
CH
1385 if (!--i->i_openers)
1386 ctx->psmap = NULL;
47d3a5fa 1387 mutex_unlock(&ctx->mapping_lock);
43c2bbd9
CH
1388 return 0;
1389}
1390
5dfe4c96 1391static const struct file_operations spufs_psmap_fops = {
27d5bf2a 1392 .open = spufs_psmap_open,
43c2bbd9 1393 .release = spufs_psmap_release,
27d5bf2a 1394 .mmap = spufs_psmap_mmap,
d9379c4b
AB
1395};
1396
1397
27d5bf2a 1398#if SPUFS_MMAP_4K
78bde53e
BH
1399static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1400 unsigned long address)
6df10a82 1401{
78bde53e 1402 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
6df10a82
MN
1403}
1404
1405static struct vm_operations_struct spufs_mfc_mmap_vmops = {
78bde53e 1406 .nopfn = spufs_mfc_mmap_nopfn,
6df10a82
MN
1407};
1408
1409/*
1410 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
6df10a82
MN
1411 */
1412static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1413{
1414 if (!(vma->vm_flags & VM_SHARED))
1415 return -EINVAL;
1416
78bde53e 1417 vma->vm_flags |= VM_IO | VM_PFNMAP;
6df10a82 1418 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
23cc7701 1419 | _PAGE_NO_CACHE | _PAGE_GUARDED);
6df10a82
MN
1420
1421 vma->vm_ops = &spufs_mfc_mmap_vmops;
1422 return 0;
1423}
27d5bf2a
BH
1424#else /* SPUFS_MMAP_4K */
1425#define spufs_mfc_mmap NULL
1426#endif /* !SPUFS_MMAP_4K */
a33a7d73
AB
1427
1428static int spufs_mfc_open(struct inode *inode, struct file *file)
1429{
1430 struct spufs_inode_info *i = SPUFS_I(inode);
1431 struct spu_context *ctx = i->i_ctx;
1432
1433 /* we don't want to deal with DMA into other processes */
1434 if (ctx->owner != current->mm)
1435 return -EINVAL;
1436
1437 if (atomic_read(&inode->i_count) != 1)
1438 return -EBUSY;
1439
47d3a5fa 1440 mutex_lock(&ctx->mapping_lock);
a33a7d73 1441 file->private_data = ctx;
43c2bbd9
CH
1442 if (!i->i_openers++)
1443 ctx->mfc = inode->i_mapping;
47d3a5fa 1444 mutex_unlock(&ctx->mapping_lock);
a33a7d73
AB
1445 return nonseekable_open(inode, file);
1446}
1447
43c2bbd9
CH
1448static int
1449spufs_mfc_release(struct inode *inode, struct file *file)
1450{
1451 struct spufs_inode_info *i = SPUFS_I(inode);
1452 struct spu_context *ctx = i->i_ctx;
1453
47d3a5fa 1454 mutex_lock(&ctx->mapping_lock);
43c2bbd9
CH
1455 if (!--i->i_openers)
1456 ctx->mfc = NULL;
47d3a5fa 1457 mutex_unlock(&ctx->mapping_lock);
43c2bbd9
CH
1458 return 0;
1459}
1460
a33a7d73
AB
1461/* interrupt-level mfc callback function. */
1462void spufs_mfc_callback(struct spu *spu)
1463{
1464 struct spu_context *ctx = spu->ctx;
1465
e65c2f6f
LB
1466 if (!ctx)
1467 return;
1468
a33a7d73
AB
1469 wake_up_all(&ctx->mfc_wq);
1470
1471 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1472 if (ctx->mfc_fasync) {
1473 u32 free_elements, tagstatus;
1474 unsigned int mask;
1475
1476 /* no need for spu_acquire in interrupt context */
1477 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1478 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1479
1480 mask = 0;
1481 if (free_elements & 0xffff)
1482 mask |= POLLOUT;
1483 if (tagstatus & ctx->tagwait)
1484 mask |= POLLIN;
1485
1486 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1487 }
1488}
1489
1490static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1491{
1492 /* See if there is one tag group is complete */
1493 /* FIXME we need locking around tagwait */
1494 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1495 ctx->tagwait &= ~*status;
1496 if (*status)
1497 return 1;
1498
1499 /* enable interrupt waiting for any tag group,
1500 may silently fail if interrupts are already enabled */
1501 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1502 return 0;
1503}
1504
1505static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1506 size_t size, loff_t *pos)
1507{
1508 struct spu_context *ctx = file->private_data;
1509 int ret = -EINVAL;
1510 u32 status;
1511
1512 if (size != 4)
1513 goto out;
1514
1515 spu_acquire(ctx);
1516 if (file->f_flags & O_NONBLOCK) {
1517 status = ctx->ops->read_mfc_tagstatus(ctx);
1518 if (!(status & ctx->tagwait))
1519 ret = -EAGAIN;
1520 else
1521 ctx->tagwait &= ~status;
1522 } else {
1523 ret = spufs_wait(ctx->mfc_wq,
1524 spufs_read_mfc_tagstatus(ctx, &status));
1525 }
1526 spu_release(ctx);
1527
1528 if (ret)
1529 goto out;
1530
1531 ret = 4;
1532 if (copy_to_user(buffer, &status, 4))
1533 ret = -EFAULT;
1534
1535out:
1536 return ret;
1537}
1538
1539static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1540{
1541 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1542 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1543
1544 switch (cmd->cmd) {
1545 case MFC_PUT_CMD:
1546 case MFC_PUTF_CMD:
1547 case MFC_PUTB_CMD:
1548 case MFC_GET_CMD:
1549 case MFC_GETF_CMD:
1550 case MFC_GETB_CMD:
1551 break;
1552 default:
1553 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1554 return -EIO;
1555 }
1556
1557 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1558 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1559 cmd->ea, cmd->lsa);
1560 return -EIO;
1561 }
1562
1563 switch (cmd->size & 0xf) {
1564 case 1:
1565 break;
1566 case 2:
1567 if (cmd->lsa & 1)
1568 goto error;
1569 break;
1570 case 4:
1571 if (cmd->lsa & 3)
1572 goto error;
1573 break;
1574 case 8:
1575 if (cmd->lsa & 7)
1576 goto error;
1577 break;
1578 case 0:
1579 if (cmd->lsa & 15)
1580 goto error;
1581 break;
1582 error:
1583 default:
1584 pr_debug("invalid DMA alignment %x for size %x\n",
1585 cmd->lsa & 0xf, cmd->size);
1586 return -EIO;
1587 }
1588
1589 if (cmd->size > 16 * 1024) {
1590 pr_debug("invalid DMA size %x\n", cmd->size);
1591 return -EIO;
1592 }
1593
1594 if (cmd->tag & 0xfff0) {
1595 /* we reserve the higher tag numbers for kernel use */
1596 pr_debug("invalid DMA tag\n");
1597 return -EIO;
1598 }
1599
1600 if (cmd->class) {
1601 /* not supported in this version */
1602 pr_debug("invalid DMA class\n");
1603 return -EIO;
1604 }
1605
1606 return 0;
1607}
1608
1609static int spu_send_mfc_command(struct spu_context *ctx,
1610 struct mfc_dma_command cmd,
1611 int *error)
1612{
1613 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1614 if (*error == -EAGAIN) {
1615 /* wait for any tag group to complete
1616 so we have space for the new command */
1617 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1618 /* try again, because the queue might be
1619 empty again */
1620 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1621 if (*error == -EAGAIN)
1622 return 0;
1623 }
1624 return 1;
1625}
1626
1627static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1628 size_t size, loff_t *pos)
1629{
1630 struct spu_context *ctx = file->private_data;
1631 struct mfc_dma_command cmd;
1632 int ret = -EINVAL;
1633
1634 if (size != sizeof cmd)
1635 goto out;
1636
1637 ret = -EFAULT;
1638 if (copy_from_user(&cmd, buffer, sizeof cmd))
1639 goto out;
1640
1641 ret = spufs_check_valid_dma(&cmd);
1642 if (ret)
1643 goto out;
1644
33bfd7a7
AB
1645 spu_acquire(ctx);
1646 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
577f8f10
AM
1647 if (ret)
1648 goto out;
1649
a33a7d73
AB
1650 if (file->f_flags & O_NONBLOCK) {
1651 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1652 } else {
1653 int status;
1654 ret = spufs_wait(ctx->mfc_wq,
1655 spu_send_mfc_command(ctx, cmd, &status));
1656 if (status)
1657 ret = status;
1658 }
a33a7d73
AB
1659
1660 if (ret)
933b0e35 1661 goto out_unlock;
a33a7d73
AB
1662
1663 ctx->tagwait |= 1 << cmd.tag;
3692dc66 1664 ret = size;
a33a7d73 1665
933b0e35
KA
1666out_unlock:
1667 spu_release(ctx);
a33a7d73
AB
1668out:
1669 return ret;
1670}
1671
1672static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1673{
1674 struct spu_context *ctx = file->private_data;
1675 u32 free_elements, tagstatus;
1676 unsigned int mask;
1677
933b0e35
KA
1678 poll_wait(file, &ctx->mfc_wq, wait);
1679
a33a7d73
AB
1680 spu_acquire(ctx);
1681 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1682 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1683 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1684 spu_release(ctx);
1685
a33a7d73
AB
1686 mask = 0;
1687 if (free_elements & 0xffff)
1688 mask |= POLLOUT | POLLWRNORM;
1689 if (tagstatus & ctx->tagwait)
1690 mask |= POLLIN | POLLRDNORM;
1691
1692 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1693 free_elements, tagstatus, ctx->tagwait);
1694
1695 return mask;
1696}
1697
73b6af8a 1698static int spufs_mfc_flush(struct file *file, fl_owner_t id)
a33a7d73
AB
1699{
1700 struct spu_context *ctx = file->private_data;
1701 int ret;
1702
1703 spu_acquire(ctx);
1704#if 0
1705/* this currently hangs */
1706 ret = spufs_wait(ctx->mfc_wq,
1707 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1708 if (ret)
1709 goto out;
1710 ret = spufs_wait(ctx->mfc_wq,
1711 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1712out:
1713#else
1714 ret = 0;
1715#endif
1716 spu_release(ctx);
1717
1718 return ret;
1719}
1720
1721static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1722 int datasync)
1723{
73b6af8a 1724 return spufs_mfc_flush(file, NULL);
a33a7d73
AB
1725}
1726
1727static int spufs_mfc_fasync(int fd, struct file *file, int on)
1728{
1729 struct spu_context *ctx = file->private_data;
1730
1731 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1732}
1733
5dfe4c96 1734static const struct file_operations spufs_mfc_fops = {
a33a7d73 1735 .open = spufs_mfc_open,
43c2bbd9 1736 .release = spufs_mfc_release,
a33a7d73
AB
1737 .read = spufs_mfc_read,
1738 .write = spufs_mfc_write,
1739 .poll = spufs_mfc_poll,
1740 .flush = spufs_mfc_flush,
1741 .fsync = spufs_mfc_fsync,
1742 .fasync = spufs_mfc_fasync,
6df10a82 1743 .mmap = spufs_mfc_mmap,
a33a7d73
AB
1744};
1745
197b1a82 1746static int spufs_npc_set(void *data, u64 val)
67207b96
AB
1747{
1748 struct spu_context *ctx = data;
8b3d6663
AB
1749 spu_acquire(ctx);
1750 ctx->ops->npc_write(ctx, val);
1751 spu_release(ctx);
197b1a82
CH
1752
1753 return 0;
67207b96
AB
1754}
1755
104f0cc2 1756static u64 spufs_npc_get(struct spu_context *ctx)
78810ff6
ME
1757{
1758 return ctx->ops->npc_read(ctx);
1759}
104f0cc2
ME
1760DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1761 "0x%llx\n", SPU_ATTR_ACQUIRE);
67207b96 1762
197b1a82 1763static int spufs_decr_set(void *data, u64 val)
8b3d6663
AB
1764{
1765 struct spu_context *ctx = data;
1766 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1767 spu_acquire_saved(ctx);
1768 lscsa->decr.slot[0] = (u32) val;
27b1ea09 1769 spu_release_saved(ctx);
197b1a82
CH
1770
1771 return 0;
8b3d6663
AB
1772}
1773
104f0cc2 1774static u64 spufs_decr_get(struct spu_context *ctx)
8b3d6663 1775{
8b3d6663 1776 struct spu_lscsa *lscsa = ctx->csa.lscsa;
bf1ab978
DGM
1777 return lscsa->decr.slot[0];
1778}
104f0cc2
ME
1779DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1780 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
8b3d6663 1781
197b1a82 1782static int spufs_decr_status_set(void *data, u64 val)
8b3d6663
AB
1783{
1784 struct spu_context *ctx = data;
8b3d6663 1785 spu_acquire_saved(ctx);
d40a01d4
MN
1786 if (val)
1787 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1788 else
1789 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
27b1ea09 1790 spu_release_saved(ctx);
197b1a82
CH
1791
1792 return 0;
8b3d6663
AB
1793}
1794
104f0cc2 1795static u64 spufs_decr_status_get(struct spu_context *ctx)
8b3d6663 1796{
d40a01d4
MN
1797 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1798 return SPU_DECR_STATUS_RUNNING;
1799 else
1800 return 0;
bf1ab978 1801}
104f0cc2
ME
1802DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1803 spufs_decr_status_set, "0x%llx\n",
1804 SPU_ATTR_ACQUIRE_SAVED);
8b3d6663 1805
197b1a82 1806static int spufs_event_mask_set(void *data, u64 val)
8b3d6663
AB
1807{
1808 struct spu_context *ctx = data;
1809 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1810 spu_acquire_saved(ctx);
1811 lscsa->event_mask.slot[0] = (u32) val;
27b1ea09 1812 spu_release_saved(ctx);
197b1a82
CH
1813
1814 return 0;
8b3d6663
AB
1815}
1816
104f0cc2 1817static u64 spufs_event_mask_get(struct spu_context *ctx)
8b3d6663 1818{
8b3d6663 1819 struct spu_lscsa *lscsa = ctx->csa.lscsa;
bf1ab978
DGM
1820 return lscsa->event_mask.slot[0];
1821}
1822
104f0cc2
ME
1823DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1824 spufs_event_mask_set, "0x%llx\n",
1825 SPU_ATTR_ACQUIRE_SAVED);
8b3d6663 1826
104f0cc2 1827static u64 spufs_event_status_get(struct spu_context *ctx)
b9e3bd77 1828{
b9e3bd77 1829 struct spu_state *state = &ctx->csa;
b9e3bd77 1830 u64 stat;
b9e3bd77
DGM
1831 stat = state->spu_chnlcnt_RW[0];
1832 if (stat)
bf1ab978
DGM
1833 return state->spu_chnldata_RW[0];
1834 return 0;
1835}
104f0cc2
ME
1836DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1837 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
b9e3bd77 1838
197b1a82 1839static int spufs_srr0_set(void *data, u64 val)
8b3d6663
AB
1840{
1841 struct spu_context *ctx = data;
1842 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1843 spu_acquire_saved(ctx);
1844 lscsa->srr0.slot[0] = (u32) val;
27b1ea09 1845 spu_release_saved(ctx);
197b1a82
CH
1846
1847 return 0;
8b3d6663
AB
1848}
1849
104f0cc2 1850static u64 spufs_srr0_get(struct spu_context *ctx)
8b3d6663 1851{
8b3d6663 1852 struct spu_lscsa *lscsa = ctx->csa.lscsa;
104f0cc2 1853 return lscsa->srr0.slot[0];
8b3d6663 1854}
104f0cc2
ME
1855DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1856 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
8b3d6663 1857
104f0cc2 1858static u64 spufs_id_get(struct spu_context *ctx)
7b1a7014 1859{
7b1a7014
AB
1860 u64 num;
1861
7b1a7014
AB
1862 if (ctx->state == SPU_STATE_RUNNABLE)
1863 num = ctx->spu->number;
1864 else
1865 num = (unsigned int)-1;
7b1a7014
AB
1866
1867 return num;
1868}
104f0cc2
ME
1869DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1870 SPU_ATTR_ACQUIRE)
7b1a7014 1871
104f0cc2 1872static u64 spufs_object_id_get(struct spu_context *ctx)
bf1ab978
DGM
1873{
1874 /* FIXME: Should there really be no locking here? */
104f0cc2 1875 return ctx->object_id;
bf1ab978
DGM
1876}
1877
197b1a82 1878static int spufs_object_id_set(void *data, u64 id)
86767277
AB
1879{
1880 struct spu_context *ctx = data;
1881 ctx->object_id = id;
197b1a82
CH
1882
1883 return 0;
86767277
AB
1884}
1885
104f0cc2
ME
1886DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1887 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
86767277 1888
104f0cc2 1889static u64 spufs_lslr_get(struct spu_context *ctx)
bf1ab978 1890{
bf1ab978
DGM
1891 return ctx->csa.priv2.spu_lslr_RW;
1892}
104f0cc2
ME
1893DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1894 SPU_ATTR_ACQUIRE_SAVED);
b9e3bd77
DGM
1895
1896static int spufs_info_open(struct inode *inode, struct file *file)
1897{
1898 struct spufs_inode_info *i = SPUFS_I(inode);
1899 struct spu_context *ctx = i->i_ctx;
1900 file->private_data = ctx;
1901 return 0;
1902}
1903
cbe709c1
BH
1904static int spufs_caps_show(struct seq_file *s, void *private)
1905{
1906 struct spu_context *ctx = s->private;
1907
1908 if (!(ctx->flags & SPU_CREATE_NOSCHED))
1909 seq_puts(s, "sched\n");
1910 if (!(ctx->flags & SPU_CREATE_ISOLATE))
1911 seq_puts(s, "step\n");
1912 return 0;
1913}
1914
1915static int spufs_caps_open(struct inode *inode, struct file *file)
1916{
1917 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1918}
1919
1920static const struct file_operations spufs_caps_fops = {
1921 .open = spufs_caps_open,
1922 .read = seq_read,
1923 .llseek = seq_lseek,
1924 .release = single_release,
1925};
1926
bf1ab978
DGM
1927static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1928 char __user *buf, size_t len, loff_t *pos)
1929{
1930 u32 mbox_stat;
1931 u32 data;
1932
1933 mbox_stat = ctx->csa.prob.mb_stat_R;
1934 if (mbox_stat & 0x0000ff) {
1935 data = ctx->csa.prob.pu_mb_R;
1936 }
1937
1938 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1939}
1940
69a2f00c
DGM
1941static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1942 size_t len, loff_t *pos)
1943{
bf1ab978 1944 int ret;
69a2f00c 1945 struct spu_context *ctx = file->private_data;
69a2f00c
DGM
1946
1947 if (!access_ok(VERIFY_WRITE, buf, len))
1948 return -EFAULT;
1949
1950 spu_acquire_saved(ctx);
1951 spin_lock(&ctx->csa.register_lock);
bf1ab978 1952 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
69a2f00c 1953 spin_unlock(&ctx->csa.register_lock);
27b1ea09 1954 spu_release_saved(ctx);
69a2f00c 1955
bf1ab978 1956 return ret;
69a2f00c
DGM
1957}
1958
5dfe4c96 1959static const struct file_operations spufs_mbox_info_fops = {
69a2f00c
DGM
1960 .open = spufs_info_open,
1961 .read = spufs_mbox_info_read,
1962 .llseek = generic_file_llseek,
1963};
1964
bf1ab978
DGM
1965static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
1966 char __user *buf, size_t len, loff_t *pos)
1967{
1968 u32 ibox_stat;
1969 u32 data;
1970
1971 ibox_stat = ctx->csa.prob.mb_stat_R;
1972 if (ibox_stat & 0xff0000) {
1973 data = ctx->csa.priv2.puint_mb_R;
1974 }
1975
1976 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1977}
1978
69a2f00c
DGM
1979static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
1980 size_t len, loff_t *pos)
1981{
1982 struct spu_context *ctx = file->private_data;
bf1ab978 1983 int ret;
69a2f00c
DGM
1984
1985 if (!access_ok(VERIFY_WRITE, buf, len))
1986 return -EFAULT;
1987
1988 spu_acquire_saved(ctx);
1989 spin_lock(&ctx->csa.register_lock);
bf1ab978 1990 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
69a2f00c 1991 spin_unlock(&ctx->csa.register_lock);
27b1ea09 1992 spu_release_saved(ctx);
69a2f00c 1993
bf1ab978 1994 return ret;
69a2f00c
DGM
1995}
1996
5dfe4c96 1997static const struct file_operations spufs_ibox_info_fops = {
69a2f00c
DGM
1998 .open = spufs_info_open,
1999 .read = spufs_ibox_info_read,
2000 .llseek = generic_file_llseek,
2001};
2002
bf1ab978
DGM
2003static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2004 char __user *buf, size_t len, loff_t *pos)
69a2f00c 2005{
69a2f00c
DGM
2006 int i, cnt;
2007 u32 data[4];
2008 u32 wbox_stat;
2009
bf1ab978
DGM
2010 wbox_stat = ctx->csa.prob.mb_stat_R;
2011 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2012 for (i = 0; i < cnt; i++) {
2013 data[i] = ctx->csa.spu_mailbox_data[i];
2014 }
2015
2016 return simple_read_from_buffer(buf, len, pos, &data,
2017 cnt * sizeof(u32));
2018}
2019
2020static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2021 size_t len, loff_t *pos)
2022{
2023 struct spu_context *ctx = file->private_data;
2024 int ret;
2025
69a2f00c
DGM
2026 if (!access_ok(VERIFY_WRITE, buf, len))
2027 return -EFAULT;
2028
2029 spu_acquire_saved(ctx);
2030 spin_lock(&ctx->csa.register_lock);
bf1ab978 2031 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
69a2f00c 2032 spin_unlock(&ctx->csa.register_lock);
27b1ea09 2033 spu_release_saved(ctx);
69a2f00c 2034
bf1ab978 2035 return ret;
69a2f00c
DGM
2036}
2037
5dfe4c96 2038static const struct file_operations spufs_wbox_info_fops = {
69a2f00c
DGM
2039 .open = spufs_info_open,
2040 .read = spufs_wbox_info_read,
2041 .llseek = generic_file_llseek,
2042};
2043
bf1ab978
DGM
2044static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2045 char __user *buf, size_t len, loff_t *pos)
b9e3bd77 2046{
b9e3bd77
DGM
2047 struct spu_dma_info info;
2048 struct mfc_cq_sr *qp, *spuqp;
2049 int i;
2050
b9e3bd77
DGM
2051 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2052 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2053 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2054 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2055 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2056 for (i = 0; i < 16; i++) {
2057 qp = &info.dma_info_command_data[i];
2058 spuqp = &ctx->csa.priv2.spuq[i];
2059
2060 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2061 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2062 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2063 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2064 }
b9e3bd77
DGM
2065
2066 return simple_read_from_buffer(buf, len, pos, &info,
2067 sizeof info);
2068}
2069
bf1ab978
DGM
2070static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2071 size_t len, loff_t *pos)
2072{
2073 struct spu_context *ctx = file->private_data;
2074 int ret;
2075
2076 if (!access_ok(VERIFY_WRITE, buf, len))
2077 return -EFAULT;
2078
2079 spu_acquire_saved(ctx);
2080 spin_lock(&ctx->csa.register_lock);
2081 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2082 spin_unlock(&ctx->csa.register_lock);
27b1ea09 2083 spu_release_saved(ctx);
bf1ab978
DGM
2084
2085 return ret;
2086}
2087
5dfe4c96 2088static const struct file_operations spufs_dma_info_fops = {
b9e3bd77
DGM
2089 .open = spufs_info_open,
2090 .read = spufs_dma_info_read,
2091};
2092
bf1ab978
DGM
2093static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2094 char __user *buf, size_t len, loff_t *pos)
b9e3bd77 2095{
b9e3bd77 2096 struct spu_proxydma_info info;
b9e3bd77 2097 struct mfc_cq_sr *qp, *puqp;
bf1ab978 2098 int ret = sizeof info;
b9e3bd77
DGM
2099 int i;
2100
2101 if (len < ret)
2102 return -EINVAL;
2103
2104 if (!access_ok(VERIFY_WRITE, buf, len))
2105 return -EFAULT;
2106
b9e3bd77
DGM
2107 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2108 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2109 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2110 for (i = 0; i < 8; i++) {
2111 qp = &info.proxydma_info_command_data[i];
2112 puqp = &ctx->csa.priv2.puq[i];
2113
2114 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2115 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2116 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2117 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2118 }
bf1ab978
DGM
2119
2120 return simple_read_from_buffer(buf, len, pos, &info,
2121 sizeof info);
2122}
2123
2124static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2125 size_t len, loff_t *pos)
2126{
2127 struct spu_context *ctx = file->private_data;
2128 int ret;
2129
2130 spu_acquire_saved(ctx);
2131 spin_lock(&ctx->csa.register_lock);
2132 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
b9e3bd77 2133 spin_unlock(&ctx->csa.register_lock);
27b1ea09 2134 spu_release_saved(ctx);
b9e3bd77 2135
b9e3bd77
DGM
2136 return ret;
2137}
2138
5dfe4c96 2139static const struct file_operations spufs_proxydma_info_fops = {
b9e3bd77
DGM
2140 .open = spufs_info_open,
2141 .read = spufs_proxydma_info_read,
2142};
2143
476273ad
CH
2144static int spufs_show_tid(struct seq_file *s, void *private)
2145{
2146 struct spu_context *ctx = s->private;
2147
2148 seq_printf(s, "%d\n", ctx->tid);
2149 return 0;
2150}
2151
2152static int spufs_tid_open(struct inode *inode, struct file *file)
2153{
2154 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2155}
2156
2157static const struct file_operations spufs_tid_fops = {
2158 .open = spufs_tid_open,
2159 .read = seq_read,
2160 .llseek = seq_lseek,
2161 .release = single_release,
2162};
2163
e9f8a0b6
CH
2164static const char *ctx_state_names[] = {
2165 "user", "system", "iowait", "loaded"
2166};
2167
2168static unsigned long long spufs_acct_time(struct spu_context *ctx,
27ec41d3 2169 enum spu_utilization_state state)
e9f8a0b6 2170{
27ec41d3
AD
2171 struct timespec ts;
2172 unsigned long long time = ctx->stats.times[state];
e9f8a0b6 2173
27ec41d3
AD
2174 /*
2175 * In general, utilization statistics are updated by the controlling
2176 * thread as the spu context moves through various well defined
2177 * state transitions, but if the context is lazily loaded its
2178 * utilization statistics are not updated as the controlling thread
2179 * is not tightly coupled with the execution of the spu context. We
2180 * calculate and apply the time delta from the last recorded state
2181 * of the spu context.
2182 */
2183 if (ctx->spu && ctx->stats.util_state == state) {
2184 ktime_get_ts(&ts);
2185 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2186 }
e9f8a0b6 2187
27ec41d3 2188 return time / NSEC_PER_MSEC;
e9f8a0b6
CH
2189}
2190
2191static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2192{
2193 unsigned long long slb_flts = ctx->stats.slb_flt;
2194
2195 if (ctx->state == SPU_STATE_RUNNABLE) {
2196 slb_flts += (ctx->spu->stats.slb_flt -
2197 ctx->stats.slb_flt_base);
2198 }
2199
2200 return slb_flts;
2201}
2202
2203static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2204{
2205 unsigned long long class2_intrs = ctx->stats.class2_intr;
2206
2207 if (ctx->state == SPU_STATE_RUNNABLE) {
2208 class2_intrs += (ctx->spu->stats.class2_intr -
2209 ctx->stats.class2_intr_base);
2210 }
2211
2212 return class2_intrs;
2213}
2214
2215
2216static int spufs_show_stat(struct seq_file *s, void *private)
2217{
2218 struct spu_context *ctx = s->private;
2219
2220 spu_acquire(ctx);
2221 seq_printf(s, "%s %llu %llu %llu %llu "
2222 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
27ec41d3
AD
2223 ctx_state_names[ctx->stats.util_state],
2224 spufs_acct_time(ctx, SPU_UTIL_USER),
2225 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2226 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2227 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
e9f8a0b6
CH
2228 ctx->stats.vol_ctx_switch,
2229 ctx->stats.invol_ctx_switch,
2230 spufs_slb_flts(ctx),
2231 ctx->stats.hash_flt,
2232 ctx->stats.min_flt,
2233 ctx->stats.maj_flt,
2234 spufs_class2_intrs(ctx),
2235 ctx->stats.libassist);
2236 spu_release(ctx);
2237 return 0;
2238}
2239
2240static int spufs_stat_open(struct inode *inode, struct file *file)
2241{
2242 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2243}
2244
2245static const struct file_operations spufs_stat_fops = {
2246 .open = spufs_stat_open,
2247 .read = seq_read,
2248 .llseek = seq_lseek,
2249 .release = single_release,
2250};
2251
2252
67207b96 2253struct tree_descr spufs_dir_contents[] = {
cbe709c1 2254 { "capabilities", &spufs_caps_fops, 0444, },
67207b96 2255 { "mem", &spufs_mem_fops, 0666, },
8b3d6663 2256 { "regs", &spufs_regs_fops, 0666, },
67207b96
AB
2257 { "mbox", &spufs_mbox_fops, 0444, },
2258 { "ibox", &spufs_ibox_fops, 0444, },
2259 { "wbox", &spufs_wbox_fops, 0222, },
2260 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2261 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2262 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
603c4612
JK
2263 { "signal1", &spufs_signal1_fops, 0666, },
2264 { "signal2", &spufs_signal2_fops, 0666, },
67207b96
AB
2265 { "signal1_type", &spufs_signal1_type, 0666, },
2266 { "signal2_type", &spufs_signal2_type, 0666, },
6df10a82 2267 { "cntl", &spufs_cntl_fops, 0666, },
8b3d6663 2268 { "fpcr", &spufs_fpcr_fops, 0666, },
b9e3bd77
DGM
2269 { "lslr", &spufs_lslr_ops, 0444, },
2270 { "mfc", &spufs_mfc_fops, 0666, },
2271 { "mss", &spufs_mss_fops, 0666, },
2272 { "npc", &spufs_npc_ops, 0666, },
2273 { "srr0", &spufs_srr0_ops, 0666, },
8b3d6663
AB
2274 { "decr", &spufs_decr_ops, 0666, },
2275 { "decr_status", &spufs_decr_status_ops, 0666, },
8b3d6663 2276 { "event_mask", &spufs_event_mask_ops, 0666, },
b9e3bd77 2277 { "event_status", &spufs_event_status_ops, 0444, },
27d5bf2a 2278 { "psmap", &spufs_psmap_fops, 0666, },
86767277
AB
2279 { "phys-id", &spufs_id_ops, 0666, },
2280 { "object-id", &spufs_object_id_ops, 0666, },
69a2f00c
DGM
2281 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2282 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2283 { "wbox_info", &spufs_wbox_info_fops, 0444, },
b9e3bd77
DGM
2284 { "dma_info", &spufs_dma_info_fops, 0444, },
2285 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
476273ad 2286 { "tid", &spufs_tid_fops, 0444, },
e9f8a0b6 2287 { "stat", &spufs_stat_fops, 0444, },
67207b96
AB
2288 {},
2289};
5737edd1
MN
2290
2291struct tree_descr spufs_dir_nosched_contents[] = {
cbe709c1 2292 { "capabilities", &spufs_caps_fops, 0444, },
5737edd1
MN
2293 { "mem", &spufs_mem_fops, 0666, },
2294 { "mbox", &spufs_mbox_fops, 0444, },
2295 { "ibox", &spufs_ibox_fops, 0444, },
2296 { "wbox", &spufs_wbox_fops, 0222, },
2297 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2298 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2299 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
d054b36f
JK
2300 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2301 { "signal2", &spufs_signal2_nosched_fops, 0222, },
5737edd1
MN
2302 { "signal1_type", &spufs_signal1_type, 0666, },
2303 { "signal2_type", &spufs_signal2_type, 0666, },
2304 { "mss", &spufs_mss_fops, 0666, },
2305 { "mfc", &spufs_mfc_fops, 0666, },
2306 { "cntl", &spufs_cntl_fops, 0666, },
2307 { "npc", &spufs_npc_ops, 0666, },
2308 { "psmap", &spufs_psmap_fops, 0666, },
2309 { "phys-id", &spufs_id_ops, 0666, },
2310 { "object-id", &spufs_object_id_ops, 0666, },
476273ad 2311 { "tid", &spufs_tid_fops, 0444, },
e9f8a0b6 2312 { "stat", &spufs_stat_fops, 0444, },
5737edd1
MN
2313 {},
2314};
bf1ab978
DGM
2315
2316struct spufs_coredump_reader spufs_coredump_read[] = {
4fca9c42
ME
2317 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2318 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
104f0cc2
ME
2319 { "lslr", NULL, spufs_lslr_get, 19 },
2320 { "decr", NULL, spufs_decr_get, 19 },
2321 { "decr_status", NULL, spufs_decr_status_get, 19 },
4fca9c42
ME
2322 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2323 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
104f0cc2 2324 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
4fca9c42 2325 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
104f0cc2
ME
2326 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2327 { "event_mask", NULL, spufs_event_mask_get, 19 },
2328 { "event_status", NULL, spufs_event_status_get, 19 },
4fca9c42
ME
2329 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2330 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2331 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2332 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2333 { "proxydma_info", __spufs_proxydma_info_read,
2334 NULL, sizeof(struct spu_proxydma_info)},
104f0cc2
ME
2335 { "object-id", NULL, spufs_object_id_get, 19 },
2336 { "npc", NULL, spufs_npc_get, 19 },
936d5bf1 2337 { NULL },
bf1ab978 2338};