blob: 1018acd1746b76f7ac99ef91b62092d4d45529d6 [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
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
Arnd Bergmanna33a7d72006-03-23 00:00:11 +010023#undef DEBUG
24
Arnd Bergmann67207b92005-11-15 15:53:48 -050025#include <linux/fs.h>
26#include <linux/ioctl.h>
27#include <linux/module.h>
Arnd Bergmannd88cfff2005-12-05 22:52:22 -050028#include <linux/pagemap.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050029#include <linux/poll.h>
Arnd Bergmann51104592005-12-05 22:52:25 -050030#include <linux/ptrace.h>
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +100031#include <linux/seq_file.h>
Christoph Hellwig038200c2008-01-11 15:03:26 +110032#include <linux/marker.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050033
34#include <asm/io.h>
35#include <asm/semaphore.h>
36#include <asm/spu.h>
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +010037#include <asm/spu_info.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050038#include <asm/uaccess.h>
39
40#include "spufs.h"
41
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +020042#define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
43
Christoph Hellwig197b1a82007-12-20 16:39:59 +090044/* Simple attribute files */
45struct spufs_attr {
46 int (*get)(void *, u64 *);
47 int (*set)(void *, u64);
48 char get_buf[24]; /* enough to store a u64 and "\n\0" */
49 char set_buf[24];
50 void *data;
51 const char *fmt; /* format for read operation */
52 struct mutex mutex; /* protects access to these buffers */
53};
54
55static int spufs_attr_open(struct inode *inode, struct file *file,
56 int (*get)(void *, u64 *), int (*set)(void *, u64),
57 const char *fmt)
58{
59 struct spufs_attr *attr;
60
61 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
62 if (!attr)
63 return -ENOMEM;
64
65 attr->get = get;
66 attr->set = set;
67 attr->data = inode->i_private;
68 attr->fmt = fmt;
69 mutex_init(&attr->mutex);
70 file->private_data = attr;
71
72 return nonseekable_open(inode, file);
73}
74
75static int spufs_attr_release(struct inode *inode, struct file *file)
76{
77 kfree(file->private_data);
78 return 0;
79}
80
81static ssize_t spufs_attr_read(struct file *file, char __user *buf,
82 size_t len, loff_t *ppos)
83{
84 struct spufs_attr *attr;
85 size_t size;
86 ssize_t ret;
87
88 attr = file->private_data;
89 if (!attr->get)
90 return -EACCES;
91
92 ret = mutex_lock_interruptible(&attr->mutex);
93 if (ret)
94 return ret;
95
96 if (*ppos) { /* continued read */
97 size = strlen(attr->get_buf);
98 } else { /* first read */
99 u64 val;
100 ret = attr->get(attr->data, &val);
101 if (ret)
102 goto out;
103
104 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
105 attr->fmt, (unsigned long long)val);
106 }
107
108 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
109out:
110 mutex_unlock(&attr->mutex);
111 return ret;
112}
113
114static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
115 size_t len, loff_t *ppos)
116{
117 struct spufs_attr *attr;
118 u64 val;
119 size_t size;
120 ssize_t ret;
121
122 attr = file->private_data;
123 if (!attr->set)
124 return -EACCES;
125
126 ret = mutex_lock_interruptible(&attr->mutex);
127 if (ret)
128 return ret;
129
130 ret = -EFAULT;
131 size = min(sizeof(attr->set_buf) - 1, len);
132 if (copy_from_user(attr->set_buf, buf, size))
133 goto out;
134
135 ret = len; /* claim we got the whole input */
136 attr->set_buf[size] = '\0';
137 val = simple_strtol(attr->set_buf, NULL, 0);
138 attr->set(attr->data, val);
139out:
140 mutex_unlock(&attr->mutex);
141 return ret;
142}
143
144#define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
145static int __fops ## _open(struct inode *inode, struct file *file) \
146{ \
147 __simple_attr_check_format(__fmt, 0ull); \
148 return spufs_attr_open(inode, file, __get, __set, __fmt); \
149} \
150static struct file_operations __fops = { \
151 .owner = THIS_MODULE, \
152 .open = __fops ## _open, \
153 .release = spufs_attr_release, \
154 .read = spufs_attr_read, \
155 .write = spufs_attr_write, \
156};
157
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +1000158
Arnd Bergmann67207b92005-11-15 15:53:48 -0500159static int
160spufs_mem_open(struct inode *inode, struct file *file)
161{
162 struct spufs_inode_info *i = SPUFS_I(inode);
Mark Nutter6df10a82006-03-23 00:00:12 +0100163 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200164
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000165 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100166 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200167 if (!i->i_openers++)
168 ctx->local_store = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000169 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200170 return 0;
171}
172
173static int
174spufs_mem_release(struct inode *inode, struct file *file)
175{
176 struct spufs_inode_info *i = SPUFS_I(inode);
177 struct spu_context *ctx = i->i_ctx;
178
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000179 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200180 if (!--i->i_openers)
181 ctx->local_store = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000182 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500183 return 0;
184}
185
186static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100187__spufs_mem_read(struct spu_context *ctx, char __user *buffer,
188 size_t size, loff_t *pos)
189{
190 char *local_store = ctx->ops->get_ls(ctx);
191 return simple_read_from_buffer(buffer, size, pos, local_store,
192 LS_SIZE);
193}
194
195static ssize_t
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196spufs_mem_read(struct file *file, char __user *buffer,
197 size_t size, loff_t *pos)
198{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100199 struct spu_context *ctx = file->private_data;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100200 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500201
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900202 ret = spu_acquire(ctx);
203 if (ret)
204 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100205 ret = __spufs_mem_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500206 spu_release(ctx);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900207
Arnd Bergmann67207b92005-11-15 15:53:48 -0500208 return ret;
209}
210
211static ssize_t
212spufs_mem_write(struct file *file, const char __user *buffer,
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100213 size_t size, loff_t *ppos)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214{
215 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500216 char *local_store;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100217 loff_t pos = *ppos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500218 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500219
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100220 if (pos < 0)
221 return -EINVAL;
222 if (pos > LS_SIZE)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500223 return -EFBIG;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100224 if (size > LS_SIZE - pos)
225 size = LS_SIZE - pos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500226
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900227 ret = spu_acquire(ctx);
228 if (ret)
229 return ret;
230
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500231 local_store = ctx->ops->get_ls(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100232 ret = copy_from_user(local_store + pos, buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500233 spu_release(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100234
235 if (ret)
236 return -EFAULT;
237 *ppos = pos + size;
238 return size;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500239}
240
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100241static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
242 unsigned long address)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500243{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000244 struct spu_context *ctx = vma->vm_file->private_data;
245 unsigned long pfn, offset, addr0 = address;
246#ifdef CONFIG_SPU_FS_64K_LS
247 struct spu_state *csa = &ctx->csa;
248 int psize;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100249
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000250 /* Check what page size we are using */
251 psize = get_slice_psize(vma->vm_mm, address);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500252
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000253 /* Some sanity checking */
254 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
255
256 /* Wow, 64K, cool, we need to align the address though */
257 if (csa->use_big_pages) {
258 BUG_ON(vma->vm_start & 0xffff);
259 address &= ~0xfffful;
260 }
261#endif /* CONFIG_SPU_FS_64K_LS */
262
263 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
Masato Noguchi128b8542007-02-13 21:54:30 +0100264 if (offset >= LS_SIZE)
265 return NOPFN_SIGBUS;
266
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000267 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
268 addr0, address, offset);
269
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900270 if (spu_acquire(ctx))
271 return NOPFN_REFAULT;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500272
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200273 if (ctx->state == SPU_STATE_SAVED) {
274 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Arnd Bergmann932f535d2006-11-20 18:45:06 +0100275 & ~_PAGE_NO_CACHE);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100276 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200277 } else {
278 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100279 | _PAGE_NO_CACHE);
280 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200281 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100282 vm_insert_pfn(vma, address, pfn);
283
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500284 spu_release(ctx);
285
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100286 return NOPFN_REFAULT;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500287}
288
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100289
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500290static struct vm_operations_struct spufs_mem_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100291 .nopfn = spufs_mem_mmap_nopfn,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500292};
293
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000294static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500295{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000296#ifdef CONFIG_SPU_FS_64K_LS
297 struct spu_context *ctx = file->private_data;
298 struct spu_state *csa = &ctx->csa;
299
300 /* Sanity check VMA alignment */
301 if (csa->use_big_pages) {
302 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
303 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
304 vma->vm_pgoff);
305 if (vma->vm_start & 0xffff)
306 return -EINVAL;
307 if (vma->vm_pgoff & 0xf)
308 return -EINVAL;
309 }
310#endif /* CONFIG_SPU_FS_64K_LS */
311
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500312 if (!(vma->vm_flags & VM_SHARED))
313 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500314
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100315 vma->vm_flags |= VM_IO | VM_PFNMAP;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500316 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
317 | _PAGE_NO_CACHE);
318
319 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500320 return 0;
321}
322
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000323#ifdef CONFIG_SPU_FS_64K_LS
Sebastian Siewior12388192007-09-19 14:38:12 +1000324static unsigned long spufs_get_unmapped_area(struct file *file,
325 unsigned long addr, unsigned long len, unsigned long pgoff,
326 unsigned long flags)
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000327{
328 struct spu_context *ctx = file->private_data;
329 struct spu_state *csa = &ctx->csa;
330
331 /* If not using big pages, fallback to normal MM g_u_a */
332 if (!csa->use_big_pages)
333 return current->mm->get_unmapped_area(file, addr, len,
334 pgoff, flags);
335
336 /* Else, try to obtain a 64K pages slice */
337 return slice_get_unmapped_area(addr, len, flags,
338 MMU_PAGE_64K, 1, 0);
339}
340#endif /* CONFIG_SPU_FS_64K_LS */
341
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800342static const struct file_operations spufs_mem_fops = {
Jeremy Kerr70225432007-06-29 10:58:00 +1000343 .open = spufs_mem_open,
344 .release = spufs_mem_release,
345 .read = spufs_mem_read,
346 .write = spufs_mem_write,
347 .llseek = generic_file_llseek,
348 .mmap = spufs_mem_mmap,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000349#ifdef CONFIG_SPU_FS_64K_LS
350 .get_unmapped_area = spufs_get_unmapped_area,
351#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500352};
353
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100354static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
Mark Nutter6df10a82006-03-23 00:00:12 +0100355 unsigned long address,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100356 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200357 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100358{
Mark Nutter6df10a82006-03-23 00:00:12 +0100359 struct spu_context *ctx = vma->vm_file->private_data;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100360 unsigned long area, offset = address - vma->vm_start;
Mark Nutter6df10a82006-03-23 00:00:12 +0100361
Christoph Hellwig038200c2008-01-11 15:03:26 +1100362 spu_context_nospu_trace(spufs_ps_nopfn__enter, ctx);
363
Mark Nutter6df10a82006-03-23 00:00:12 +0100364 offset += vma->vm_pgoff << PAGE_SHIFT;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200365 if (offset >= ps_size)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100366 return NOPFN_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100367
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900368 /*
369 * We have to wait for context to be loaded before we have
370 * pages to hand out to the user, but we don't want to wait
371 * with the mmap_sem held.
372 * It is possible to drop the mmap_sem here, but then we need
373 * to return NOPFN_REFAULT because the mappings may have
374 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100375 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900376 if (spu_acquire(ctx))
377 return NOPFN_REFAULT;
378
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900379 if (ctx->state == SPU_STATE_SAVED) {
380 up_read(&current->mm->mmap_sem);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100381 spu_context_nospu_trace(spufs_ps_nopfn__sleep, ctx);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900382 spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100383 spu_context_trace(spufs_ps_nopfn__wake, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900384 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900385 } else {
386 area = ctx->spu->problem_phys + ps_offs;
387 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100388 spu_context_trace(spufs_ps_nopfn__insert, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900389 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100390
Mark Nutter6df10a82006-03-23 00:00:12 +0100391 spu_release(ctx);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100392 return NOPFN_REFAULT;
Mark Nutter6df10a82006-03-23 00:00:12 +0100393}
394
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200395#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100396static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
397 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +0100398{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100399 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +0100400}
401
402static struct vm_operations_struct spufs_cntl_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100403 .nopfn = spufs_cntl_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +0100404};
405
406/*
407 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100408 */
409static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
410{
411 if (!(vma->vm_flags & VM_SHARED))
412 return -EINVAL;
413
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100414 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100415 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200416 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100417
418 vma->vm_ops = &spufs_cntl_mmap_vmops;
419 return 0;
420}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200421#else /* SPUFS_MMAP_4K */
422#define spufs_cntl_mmap NULL
423#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100424
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900425static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200426{
427 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900428 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200429
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900430 ret = spu_acquire(ctx);
431 if (ret)
432 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900433 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200434 spu_release(ctx);
435
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900436 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200437}
438
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900439static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200440{
441 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900442 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200443
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900444 ret = spu_acquire(ctx);
445 if (ret)
446 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200447 ctx->ops->runcntl_write(ctx, val);
448 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900449
450 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200451}
452
Mark Nutter6df10a82006-03-23 00:00:12 +0100453static int spufs_cntl_open(struct inode *inode, struct file *file)
454{
455 struct spufs_inode_info *i = SPUFS_I(inode);
456 struct spu_context *ctx = i->i_ctx;
457
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000458 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100459 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200460 if (!i->i_openers++)
461 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000462 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900463 return spufs_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200464 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100465}
466
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200467static int
468spufs_cntl_release(struct inode *inode, struct file *file)
469{
470 struct spufs_inode_info *i = SPUFS_I(inode);
471 struct spu_context *ctx = i->i_ctx;
472
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900473 spufs_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200474
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000475 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200476 if (!--i->i_openers)
477 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000478 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200479 return 0;
480}
481
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800482static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100483 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200484 .release = spufs_cntl_release,
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900485 .read = spufs_attr_read,
486 .write = spufs_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100487 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100488};
489
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500490static int
491spufs_regs_open(struct inode *inode, struct file *file)
492{
493 struct spufs_inode_info *i = SPUFS_I(inode);
494 file->private_data = i->i_ctx;
495 return 0;
496}
497
498static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100499__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
500 size_t size, loff_t *pos)
501{
502 struct spu_lscsa *lscsa = ctx->csa.lscsa;
503 return simple_read_from_buffer(buffer, size, pos,
504 lscsa->gprs, sizeof lscsa->gprs);
505}
506
507static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500508spufs_regs_read(struct file *file, char __user *buffer,
509 size_t size, loff_t *pos)
510{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500511 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100512 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500513
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900514 ret = spu_acquire_saved(ctx);
515 if (ret)
516 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100517 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200518 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500519 return ret;
520}
521
522static ssize_t
523spufs_regs_write(struct file *file, const char __user *buffer,
524 size_t size, loff_t *pos)
525{
526 struct spu_context *ctx = file->private_data;
527 struct spu_lscsa *lscsa = ctx->csa.lscsa;
528 int ret;
529
530 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
531 if (size <= 0)
532 return -EFBIG;
533 *pos += size;
534
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900535 ret = spu_acquire_saved(ctx);
536 if (ret)
537 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500538
539 ret = copy_from_user(lscsa->gprs + *pos - size,
540 buffer, size) ? -EFAULT : size;
541
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200542 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500543 return ret;
544}
545
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800546static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500547 .open = spufs_regs_open,
548 .read = spufs_regs_read,
549 .write = spufs_regs_write,
550 .llseek = generic_file_llseek,
551};
552
553static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100554__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
555 size_t size, loff_t * pos)
556{
557 struct spu_lscsa *lscsa = ctx->csa.lscsa;
558 return simple_read_from_buffer(buffer, size, pos,
559 &lscsa->fpcr, sizeof(lscsa->fpcr));
560}
561
562static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500563spufs_fpcr_read(struct file *file, char __user * buffer,
564 size_t size, loff_t * pos)
565{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500566 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100567 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500568
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900569 ret = spu_acquire_saved(ctx);
570 if (ret)
571 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100572 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200573 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500574 return ret;
575}
576
577static ssize_t
578spufs_fpcr_write(struct file *file, const char __user * buffer,
579 size_t size, loff_t * pos)
580{
581 struct spu_context *ctx = file->private_data;
582 struct spu_lscsa *lscsa = ctx->csa.lscsa;
583 int ret;
584
585 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
586 if (size <= 0)
587 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900588
589 ret = spu_acquire_saved(ctx);
590 if (ret)
591 return ret;
592
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500593 *pos += size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500594 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
595 buffer, size) ? -EFAULT : size;
596
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200597 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500598 return ret;
599}
600
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800601static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500602 .open = spufs_regs_open,
603 .read = spufs_fpcr_read,
604 .write = spufs_fpcr_write,
605 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500606};
607
608/* generic open function for all pipe-like files */
609static int spufs_pipe_open(struct inode *inode, struct file *file)
610{
611 struct spufs_inode_info *i = SPUFS_I(inode);
612 file->private_data = i->i_ctx;
613
614 return nonseekable_open(inode, file);
615}
616
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200617/*
618 * Read as many bytes from the mailbox as possible, until
619 * one of the conditions becomes true:
620 *
621 * - no more data available in the mailbox
622 * - end of the user provided buffer
623 * - end of the mapped area
624 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500625static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
626 size_t len, loff_t *pos)
627{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500628 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200629 u32 mbox_data, __user *udata;
630 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500631
632 if (len < 4)
633 return -EINVAL;
634
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200635 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500636 return -EFAULT;
637
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200638 udata = (void __user *)buf;
639
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900640 count = spu_acquire(ctx);
641 if (count)
642 return count;
643
Arnd Bergmann274cef52006-10-24 18:01:42 +0200644 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200645 int ret;
646 ret = ctx->ops->mbox_read(ctx, &mbox_data);
647 if (ret == 0)
648 break;
649
650 /*
651 * at the end of the mapped area, we can fault
652 * but still need to return the data we have
653 * read successfully so far.
654 */
655 ret = __put_user(mbox_data, udata);
656 if (ret) {
657 if (!count)
658 count = -EFAULT;
659 break;
660 }
661 }
662 spu_release(ctx);
663
664 if (!count)
665 count = -EAGAIN;
666
667 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500668}
669
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800670static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500671 .open = spufs_pipe_open,
672 .read = spufs_mbox_read,
673};
674
675static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
676 size_t len, loff_t *pos)
677{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500678 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900679 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500680 u32 mbox_stat;
681
682 if (len < 4)
683 return -EINVAL;
684
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900685 ret = spu_acquire(ctx);
686 if (ret)
687 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500688
689 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
690
691 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500692
693 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
694 return -EFAULT;
695
696 return 4;
697}
698
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800699static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500700 .open = spufs_pipe_open,
701 .read = spufs_mbox_stat_read,
702};
703
704/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500705size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500706{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500707 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500708}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500709
710static int spufs_ibox_fasync(int fd, struct file *file, int on)
711{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500712 struct spu_context *ctx = file->private_data;
713
714 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
715}
716
717/* interrupt-level ibox callback function. */
718void spufs_ibox_callback(struct spu *spu)
719{
720 struct spu_context *ctx = spu->ctx;
721
Luke Browninge65c2f62007-12-20 16:39:59 +0900722 if (!ctx)
723 return;
724
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500725 wake_up_all(&ctx->ibox_wq);
726 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500727}
728
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200729/*
730 * Read as many bytes from the interrupt mailbox as possible, until
731 * one of the conditions becomes true:
732 *
733 * - no more data available in the mailbox
734 * - end of the user provided buffer
735 * - end of the mapped area
736 *
737 * If the file is opened without O_NONBLOCK, we wait here until
738 * any data is available, but return when we have been able to
739 * read something.
740 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500741static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
742 size_t len, loff_t *pos)
743{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500744 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200745 u32 ibox_data, __user *udata;
746 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500747
748 if (len < 4)
749 return -EINVAL;
750
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200751 if (!access_ok(VERIFY_WRITE, buf, len))
752 return -EFAULT;
753
754 udata = (void __user *)buf;
755
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900756 count = spu_acquire(ctx);
757 if (count)
758 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500759
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200760 /* wait only for the first element */
761 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500762 if (file->f_flags & O_NONBLOCK) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500763 if (!spu_ibox_read(ctx, &ibox_data))
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200764 count = -EAGAIN;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500765 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200766 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
767 }
768 if (count)
769 goto out;
770
771 /* if we can't write at all, return -EFAULT */
772 count = __put_user(ibox_data, udata);
773 if (count)
774 goto out;
775
776 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
777 int ret;
778 ret = ctx->ops->ibox_read(ctx, &ibox_data);
779 if (ret == 0)
780 break;
781 /*
782 * at the end of the mapped area, we can fault
783 * but still need to return the data we have
784 * read successfully so far.
785 */
786 ret = __put_user(ibox_data, udata);
787 if (ret)
788 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500789 }
790
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200791out:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500792 spu_release(ctx);
793
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200794 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500795}
796
797static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
798{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500799 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500800 unsigned int mask;
801
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500802 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500803
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900804 /*
805 * For now keep this uninterruptible and also ignore the rule
806 * that poll should not sleep. Will be fixed later.
807 */
808 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500809 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
810 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500811
812 return mask;
813}
814
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800815static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500816 .open = spufs_pipe_open,
817 .read = spufs_ibox_read,
818 .poll = spufs_ibox_poll,
819 .fasync = spufs_ibox_fasync,
820};
821
822static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
823 size_t len, loff_t *pos)
824{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500825 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900826 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500827 u32 ibox_stat;
828
829 if (len < 4)
830 return -EINVAL;
831
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900832 ret = spu_acquire(ctx);
833 if (ret)
834 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500835 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
836 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500837
838 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
839 return -EFAULT;
840
841 return 4;
842}
843
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800844static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500845 .open = spufs_pipe_open,
846 .read = spufs_ibox_stat_read,
847};
848
849/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500850size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500851{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500852 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500853}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500854
855static int spufs_wbox_fasync(int fd, struct file *file, int on)
856{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500857 struct spu_context *ctx = file->private_data;
858 int ret;
859
860 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
861
862 return ret;
863}
864
865/* interrupt-level wbox callback function. */
866void spufs_wbox_callback(struct spu *spu)
867{
868 struct spu_context *ctx = spu->ctx;
869
Luke Browninge65c2f62007-12-20 16:39:59 +0900870 if (!ctx)
871 return;
872
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500873 wake_up_all(&ctx->wbox_wq);
874 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500875}
876
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200877/*
878 * Write as many bytes to the interrupt mailbox as possible, until
879 * one of the conditions becomes true:
880 *
881 * - the mailbox is full
882 * - end of the user provided buffer
883 * - end of the mapped area
884 *
885 * If the file is opened without O_NONBLOCK, we wait here until
886 * space is availabyl, but return when we have been able to
887 * write something.
888 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500889static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
890 size_t len, loff_t *pos)
891{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500892 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200893 u32 wbox_data, __user *udata;
894 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500895
896 if (len < 4)
897 return -EINVAL;
898
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200899 udata = (void __user *)buf;
900 if (!access_ok(VERIFY_READ, buf, len))
901 return -EFAULT;
902
903 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500904 return -EFAULT;
905
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900906 count = spu_acquire(ctx);
907 if (count)
908 return count;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500909
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200910 /*
911 * make sure we can at least write one element, by waiting
912 * in case of !O_NONBLOCK
913 */
914 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500915 if (file->f_flags & O_NONBLOCK) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500916 if (!spu_wbox_write(ctx, wbox_data))
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200917 count = -EAGAIN;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500918 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200919 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Arnd Bergmann67207b92005-11-15 15:53:48 -0500920 }
921
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200922 if (count)
923 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500924
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200925 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200926 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
927 int ret;
928 ret = __get_user(wbox_data, udata);
929 if (ret)
930 break;
931
932 ret = spu_wbox_write(ctx, wbox_data);
933 if (ret == 0)
934 break;
935 }
936
937out:
938 spu_release(ctx);
939 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500940}
941
942static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
943{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500944 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500945 unsigned int mask;
946
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500947 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500948
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900949 /*
950 * For now keep this uninterruptible and also ignore the rule
951 * that poll should not sleep. Will be fixed later.
952 */
953 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500954 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
955 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500956
957 return mask;
958}
959
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800960static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500961 .open = spufs_pipe_open,
962 .write = spufs_wbox_write,
963 .poll = spufs_wbox_poll,
964 .fasync = spufs_wbox_fasync,
965};
966
967static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
968 size_t len, loff_t *pos)
969{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500970 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900971 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500972 u32 wbox_stat;
973
974 if (len < 4)
975 return -EINVAL;
976
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900977 ret = spu_acquire(ctx);
978 if (ret)
979 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500980 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
981 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500982
983 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
984 return -EFAULT;
985
986 return 4;
987}
988
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800989static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500990 .open = spufs_pipe_open,
991 .read = spufs_wbox_stat_read,
992};
993
Mark Nutter6df10a82006-03-23 00:00:12 +0100994static int spufs_signal1_open(struct inode *inode, struct file *file)
995{
996 struct spufs_inode_info *i = SPUFS_I(inode);
997 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200998
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000999 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001000 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001001 if (!i->i_openers++)
1002 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001003 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001004 return nonseekable_open(inode, file);
1005}
1006
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001007static int
1008spufs_signal1_release(struct inode *inode, struct file *file)
1009{
1010 struct spufs_inode_info *i = SPUFS_I(inode);
1011 struct spu_context *ctx = i->i_ctx;
1012
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001013 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001014 if (!--i->i_openers)
1015 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001016 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001017 return 0;
1018}
1019
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001020static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001021 size_t len, loff_t *pos)
1022{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001023 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001024 u32 data;
1025
Arnd Bergmann67207b92005-11-15 15:53:48 -05001026 if (len < 4)
1027 return -EINVAL;
1028
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001029 if (ctx->csa.spu_chnlcnt_RW[3]) {
1030 data = ctx->csa.spu_chnldata_RW[3];
1031 ret = 4;
1032 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001033
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001034 if (!ret)
1035 goto out;
1036
Arnd Bergmann67207b92005-11-15 15:53:48 -05001037 if (copy_to_user(buf, &data, 4))
1038 return -EFAULT;
1039
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001040out:
1041 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001042}
1043
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001044static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1045 size_t len, loff_t *pos)
1046{
1047 int ret;
1048 struct spu_context *ctx = file->private_data;
1049
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001050 ret = spu_acquire_saved(ctx);
1051 if (ret)
1052 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001053 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001054 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001055
1056 return ret;
1057}
1058
Arnd Bergmann67207b92005-11-15 15:53:48 -05001059static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1060 size_t len, loff_t *pos)
1061{
1062 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001063 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001064 u32 data;
1065
1066 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001067
1068 if (len < 4)
1069 return -EINVAL;
1070
1071 if (copy_from_user(&data, buf, 4))
1072 return -EFAULT;
1073
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001074 ret = spu_acquire(ctx);
1075 if (ret)
1076 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001077 ctx->ops->signal1_write(ctx, data);
1078 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001079
1080 return 4;
1081}
1082
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001083static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1084 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001085{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001086#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001087 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001088#elif PAGE_SIZE == 0x10000
1089 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1090 * signal 1 and 2 area
1091 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001092 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001093#else
1094#error unsupported page size
1095#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001096}
1097
1098static struct vm_operations_struct spufs_signal1_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001099 .nopfn = spufs_signal1_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001100};
1101
1102static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1103{
1104 if (!(vma->vm_flags & VM_SHARED))
1105 return -EINVAL;
1106
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001107 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001108 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001109 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001110
1111 vma->vm_ops = &spufs_signal1_mmap_vmops;
1112 return 0;
1113}
Mark Nutter6df10a82006-03-23 00:00:12 +01001114
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001115static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001116 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001117 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001118 .read = spufs_signal1_read,
1119 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001120 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001121};
1122
Jeremy Kerrd054b362007-07-20 21:39:31 +02001123static const struct file_operations spufs_signal1_nosched_fops = {
1124 .open = spufs_signal1_open,
1125 .release = spufs_signal1_release,
1126 .write = spufs_signal1_write,
1127 .mmap = spufs_signal1_mmap,
1128};
1129
Mark Nutter6df10a82006-03-23 00:00:12 +01001130static int spufs_signal2_open(struct inode *inode, struct file *file)
1131{
1132 struct spufs_inode_info *i = SPUFS_I(inode);
1133 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001134
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001135 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001136 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001137 if (!i->i_openers++)
1138 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001139 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001140 return nonseekable_open(inode, file);
1141}
1142
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001143static int
1144spufs_signal2_release(struct inode *inode, struct file *file)
1145{
1146 struct spufs_inode_info *i = SPUFS_I(inode);
1147 struct spu_context *ctx = i->i_ctx;
1148
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001149 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001150 if (!--i->i_openers)
1151 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001152 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001153 return 0;
1154}
1155
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001156static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001157 size_t len, loff_t *pos)
1158{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001159 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001160 u32 data;
1161
Arnd Bergmann67207b92005-11-15 15:53:48 -05001162 if (len < 4)
1163 return -EINVAL;
1164
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001165 if (ctx->csa.spu_chnlcnt_RW[4]) {
1166 data = ctx->csa.spu_chnldata_RW[4];
1167 ret = 4;
1168 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001169
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001170 if (!ret)
1171 goto out;
1172
Arnd Bergmann67207b92005-11-15 15:53:48 -05001173 if (copy_to_user(buf, &data, 4))
1174 return -EFAULT;
1175
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001176out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001177 return ret;
1178}
1179
1180static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1181 size_t len, loff_t *pos)
1182{
1183 struct spu_context *ctx = file->private_data;
1184 int ret;
1185
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001186 ret = spu_acquire_saved(ctx);
1187 if (ret)
1188 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001189 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001190 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001191
1192 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001193}
1194
1195static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1196 size_t len, loff_t *pos)
1197{
1198 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001199 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001200 u32 data;
1201
1202 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001203
1204 if (len < 4)
1205 return -EINVAL;
1206
1207 if (copy_from_user(&data, buf, 4))
1208 return -EFAULT;
1209
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001210 ret = spu_acquire(ctx);
1211 if (ret)
1212 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001213 ctx->ops->signal2_write(ctx, data);
1214 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001215
1216 return 4;
1217}
1218
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001219#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001220static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1221 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001222{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001223#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001224 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001225#elif PAGE_SIZE == 0x10000
1226 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1227 * signal 1 and 2 area
1228 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001229 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001230#else
1231#error unsupported page size
1232#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001233}
1234
1235static struct vm_operations_struct spufs_signal2_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001236 .nopfn = spufs_signal2_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001237};
1238
1239static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1240{
1241 if (!(vma->vm_flags & VM_SHARED))
1242 return -EINVAL;
1243
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001244 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001245 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001246 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001247
1248 vma->vm_ops = &spufs_signal2_mmap_vmops;
1249 return 0;
1250}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001251#else /* SPUFS_MMAP_4K */
1252#define spufs_signal2_mmap NULL
1253#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001254
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001255static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001256 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001257 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001258 .read = spufs_signal2_read,
1259 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001260 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001261};
1262
Jeremy Kerrd054b362007-07-20 21:39:31 +02001263static const struct file_operations spufs_signal2_nosched_fops = {
1264 .open = spufs_signal2_open,
1265 .release = spufs_signal2_release,
1266 .write = spufs_signal2_write,
1267 .mmap = spufs_signal2_mmap,
1268};
1269
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001270/*
1271 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1272 * work of acquiring (or not) the SPU context before calling through
1273 * to the actual get routine. The set routine is called directly.
1274 */
1275#define SPU_ATTR_NOACQUIRE 0
1276#define SPU_ATTR_ACQUIRE 1
1277#define SPU_ATTR_ACQUIRE_SAVED 2
1278
1279#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001280static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001281{ \
1282 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001283 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001284 \
1285 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001286 ret = spu_acquire(ctx); \
1287 if (ret) \
1288 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001289 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001290 spu_release(ctx); \
1291 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001292 ret = spu_acquire_saved(ctx); \
1293 if (ret) \
1294 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001295 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001296 spu_release_saved(ctx); \
1297 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001298 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001299 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001300 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001301} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001302DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001303
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001304static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001305{
1306 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001307 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001308
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001309 ret = spu_acquire(ctx);
1310 if (ret)
1311 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001312 ctx->ops->signal1_type_set(ctx, val);
1313 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001314
1315 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001316}
1317
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001318static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001319{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001320 return ctx->ops->signal1_type_get(ctx);
1321}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001322DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1323 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001324
Arnd Bergmann67207b92005-11-15 15:53:48 -05001325
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001326static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001327{
1328 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001329 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001330
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001331 ret = spu_acquire(ctx);
1332 if (ret)
1333 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001334 ctx->ops->signal2_type_set(ctx, val);
1335 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001336
1337 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001338}
1339
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001340static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001341{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001342 return ctx->ops->signal2_type_get(ctx);
1343}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001344DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1345 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001346
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001347#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001348static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1349 unsigned long address)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001350{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001351 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001352}
1353
1354static struct vm_operations_struct spufs_mss_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001355 .nopfn = spufs_mss_mmap_nopfn,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001356};
1357
1358/*
1359 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001360 */
1361static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1362{
1363 if (!(vma->vm_flags & VM_SHARED))
1364 return -EINVAL;
1365
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001366 vma->vm_flags |= VM_IO | VM_PFNMAP;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001367 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001368 | _PAGE_NO_CACHE | _PAGE_GUARDED);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001369
1370 vma->vm_ops = &spufs_mss_mmap_vmops;
1371 return 0;
1372}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001373#else /* SPUFS_MMAP_4K */
1374#define spufs_mss_mmap NULL
1375#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001376
1377static int spufs_mss_open(struct inode *inode, struct file *file)
1378{
1379 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001380 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001381
1382 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001383
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001384 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001385 if (!i->i_openers++)
1386 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001387 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001388 return nonseekable_open(inode, file);
1389}
1390
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001391static int
1392spufs_mss_release(struct inode *inode, struct file *file)
1393{
1394 struct spufs_inode_info *i = SPUFS_I(inode);
1395 struct spu_context *ctx = i->i_ctx;
1396
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001397 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001398 if (!--i->i_openers)
1399 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001400 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001401 return 0;
1402}
1403
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001404static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001405 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001406 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001407 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001408};
1409
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001410static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1411 unsigned long address)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001412{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001413 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001414}
1415
1416static struct vm_operations_struct spufs_psmap_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001417 .nopfn = spufs_psmap_mmap_nopfn,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001418};
1419
1420/*
1421 * mmap support for full problem state area [0x00000 - 0x1ffff].
1422 */
1423static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1424{
1425 if (!(vma->vm_flags & VM_SHARED))
1426 return -EINVAL;
1427
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001428 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001429 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1430 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1431
1432 vma->vm_ops = &spufs_psmap_mmap_vmops;
1433 return 0;
1434}
1435
1436static int spufs_psmap_open(struct inode *inode, struct file *file)
1437{
1438 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001439 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001440
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001441 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001442 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001443 if (!i->i_openers++)
1444 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001445 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001446 return nonseekable_open(inode, file);
1447}
1448
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001449static int
1450spufs_psmap_release(struct inode *inode, struct file *file)
1451{
1452 struct spufs_inode_info *i = SPUFS_I(inode);
1453 struct spu_context *ctx = i->i_ctx;
1454
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001455 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001456 if (!--i->i_openers)
1457 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001458 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001459 return 0;
1460}
1461
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001462static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001463 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001464 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001465 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001466};
1467
1468
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001469#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001470static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1471 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001472{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001473 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +01001474}
1475
1476static struct vm_operations_struct spufs_mfc_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001477 .nopfn = spufs_mfc_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001478};
1479
1480/*
1481 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001482 */
1483static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1484{
1485 if (!(vma->vm_flags & VM_SHARED))
1486 return -EINVAL;
1487
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001488 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001489 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001490 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001491
1492 vma->vm_ops = &spufs_mfc_mmap_vmops;
1493 return 0;
1494}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001495#else /* SPUFS_MMAP_4K */
1496#define spufs_mfc_mmap NULL
1497#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001498
1499static int spufs_mfc_open(struct inode *inode, struct file *file)
1500{
1501 struct spufs_inode_info *i = SPUFS_I(inode);
1502 struct spu_context *ctx = i->i_ctx;
1503
1504 /* we don't want to deal with DMA into other processes */
1505 if (ctx->owner != current->mm)
1506 return -EINVAL;
1507
1508 if (atomic_read(&inode->i_count) != 1)
1509 return -EBUSY;
1510
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001511 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001512 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001513 if (!i->i_openers++)
1514 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001515 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001516 return nonseekable_open(inode, file);
1517}
1518
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001519static int
1520spufs_mfc_release(struct inode *inode, struct file *file)
1521{
1522 struct spufs_inode_info *i = SPUFS_I(inode);
1523 struct spu_context *ctx = i->i_ctx;
1524
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001525 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001526 if (!--i->i_openers)
1527 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001528 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001529 return 0;
1530}
1531
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001532/* interrupt-level mfc callback function. */
1533void spufs_mfc_callback(struct spu *spu)
1534{
1535 struct spu_context *ctx = spu->ctx;
1536
Luke Browninge65c2f62007-12-20 16:39:59 +09001537 if (!ctx)
1538 return;
1539
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001540 wake_up_all(&ctx->mfc_wq);
1541
1542 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1543 if (ctx->mfc_fasync) {
1544 u32 free_elements, tagstatus;
1545 unsigned int mask;
1546
1547 /* no need for spu_acquire in interrupt context */
1548 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1549 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1550
1551 mask = 0;
1552 if (free_elements & 0xffff)
1553 mask |= POLLOUT;
1554 if (tagstatus & ctx->tagwait)
1555 mask |= POLLIN;
1556
1557 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1558 }
1559}
1560
1561static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1562{
1563 /* See if there is one tag group is complete */
1564 /* FIXME we need locking around tagwait */
1565 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1566 ctx->tagwait &= ~*status;
1567 if (*status)
1568 return 1;
1569
1570 /* enable interrupt waiting for any tag group,
1571 may silently fail if interrupts are already enabled */
1572 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1573 return 0;
1574}
1575
1576static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1577 size_t size, loff_t *pos)
1578{
1579 struct spu_context *ctx = file->private_data;
1580 int ret = -EINVAL;
1581 u32 status;
1582
1583 if (size != 4)
1584 goto out;
1585
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001586 ret = spu_acquire(ctx);
1587 if (ret)
1588 return ret;
1589
1590 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001591 if (file->f_flags & O_NONBLOCK) {
1592 status = ctx->ops->read_mfc_tagstatus(ctx);
1593 if (!(status & ctx->tagwait))
1594 ret = -EAGAIN;
1595 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001596 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001597 ctx->tagwait &= ~status;
1598 } else {
1599 ret = spufs_wait(ctx->mfc_wq,
1600 spufs_read_mfc_tagstatus(ctx, &status));
1601 }
1602 spu_release(ctx);
1603
1604 if (ret)
1605 goto out;
1606
1607 ret = 4;
1608 if (copy_to_user(buffer, &status, 4))
1609 ret = -EFAULT;
1610
1611out:
1612 return ret;
1613}
1614
1615static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1616{
1617 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1618 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1619
1620 switch (cmd->cmd) {
1621 case MFC_PUT_CMD:
1622 case MFC_PUTF_CMD:
1623 case MFC_PUTB_CMD:
1624 case MFC_GET_CMD:
1625 case MFC_GETF_CMD:
1626 case MFC_GETB_CMD:
1627 break;
1628 default:
1629 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1630 return -EIO;
1631 }
1632
1633 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1634 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1635 cmd->ea, cmd->lsa);
1636 return -EIO;
1637 }
1638
1639 switch (cmd->size & 0xf) {
1640 case 1:
1641 break;
1642 case 2:
1643 if (cmd->lsa & 1)
1644 goto error;
1645 break;
1646 case 4:
1647 if (cmd->lsa & 3)
1648 goto error;
1649 break;
1650 case 8:
1651 if (cmd->lsa & 7)
1652 goto error;
1653 break;
1654 case 0:
1655 if (cmd->lsa & 15)
1656 goto error;
1657 break;
1658 error:
1659 default:
1660 pr_debug("invalid DMA alignment %x for size %x\n",
1661 cmd->lsa & 0xf, cmd->size);
1662 return -EIO;
1663 }
1664
1665 if (cmd->size > 16 * 1024) {
1666 pr_debug("invalid DMA size %x\n", cmd->size);
1667 return -EIO;
1668 }
1669
1670 if (cmd->tag & 0xfff0) {
1671 /* we reserve the higher tag numbers for kernel use */
1672 pr_debug("invalid DMA tag\n");
1673 return -EIO;
1674 }
1675
1676 if (cmd->class) {
1677 /* not supported in this version */
1678 pr_debug("invalid DMA class\n");
1679 return -EIO;
1680 }
1681
1682 return 0;
1683}
1684
1685static int spu_send_mfc_command(struct spu_context *ctx,
1686 struct mfc_dma_command cmd,
1687 int *error)
1688{
1689 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1690 if (*error == -EAGAIN) {
1691 /* wait for any tag group to complete
1692 so we have space for the new command */
1693 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1694 /* try again, because the queue might be
1695 empty again */
1696 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1697 if (*error == -EAGAIN)
1698 return 0;
1699 }
1700 return 1;
1701}
1702
1703static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1704 size_t size, loff_t *pos)
1705{
1706 struct spu_context *ctx = file->private_data;
1707 struct mfc_dma_command cmd;
1708 int ret = -EINVAL;
1709
1710 if (size != sizeof cmd)
1711 goto out;
1712
1713 ret = -EFAULT;
1714 if (copy_from_user(&cmd, buffer, sizeof cmd))
1715 goto out;
1716
1717 ret = spufs_check_valid_dma(&cmd);
1718 if (ret)
1719 goto out;
1720
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001721 ret = spu_acquire(ctx);
1722 if (ret)
1723 goto out;
1724
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001725 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001726 if (ret)
1727 goto out;
1728
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001729 if (file->f_flags & O_NONBLOCK) {
1730 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1731 } else {
1732 int status;
1733 ret = spufs_wait(ctx->mfc_wq,
1734 spu_send_mfc_command(ctx, cmd, &status));
1735 if (status)
1736 ret = status;
1737 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001738
1739 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001740 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001741
1742 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001743 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001744
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001745out_unlock:
1746 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001747out:
1748 return ret;
1749}
1750
1751static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1752{
1753 struct spu_context *ctx = file->private_data;
1754 u32 free_elements, tagstatus;
1755 unsigned int mask;
1756
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001757 poll_wait(file, &ctx->mfc_wq, wait);
1758
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001759 /*
1760 * For now keep this uninterruptible and also ignore the rule
1761 * that poll should not sleep. Will be fixed later.
1762 */
1763 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001764 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1765 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1766 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1767 spu_release(ctx);
1768
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001769 mask = 0;
1770 if (free_elements & 0xffff)
1771 mask |= POLLOUT | POLLWRNORM;
1772 if (tagstatus & ctx->tagwait)
1773 mask |= POLLIN | POLLRDNORM;
1774
1775 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1776 free_elements, tagstatus, ctx->tagwait);
1777
1778 return mask;
1779}
1780
Al Viro73b6af82006-06-25 16:42:33 -07001781static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001782{
1783 struct spu_context *ctx = file->private_data;
1784 int ret;
1785
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001786 ret = spu_acquire(ctx);
1787 if (ret)
1788 return ret;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001789#if 0
1790/* this currently hangs */
1791 ret = spufs_wait(ctx->mfc_wq,
1792 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1793 if (ret)
1794 goto out;
1795 ret = spufs_wait(ctx->mfc_wq,
1796 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1797out:
1798#else
1799 ret = 0;
1800#endif
1801 spu_release(ctx);
1802
1803 return ret;
1804}
1805
1806static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1807 int datasync)
1808{
Al Viro73b6af82006-06-25 16:42:33 -07001809 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001810}
1811
1812static int spufs_mfc_fasync(int fd, struct file *file, int on)
1813{
1814 struct spu_context *ctx = file->private_data;
1815
1816 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1817}
1818
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001819static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001820 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001821 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001822 .read = spufs_mfc_read,
1823 .write = spufs_mfc_write,
1824 .poll = spufs_mfc_poll,
1825 .flush = spufs_mfc_flush,
1826 .fsync = spufs_mfc_fsync,
1827 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001828 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001829};
1830
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001831static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001832{
1833 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001834 int ret;
1835
1836 ret = spu_acquire(ctx);
1837 if (ret)
1838 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001839 ctx->ops->npc_write(ctx, val);
1840 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001841
1842 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001843}
1844
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001845static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001846{
1847 return ctx->ops->npc_read(ctx);
1848}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001849DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1850 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001851
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001852static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001853{
1854 struct spu_context *ctx = data;
1855 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001856 int ret;
1857
1858 ret = spu_acquire_saved(ctx);
1859 if (ret)
1860 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001861 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001862 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001863
1864 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001865}
1866
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001867static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001868{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001869 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001870 return lscsa->decr.slot[0];
1871}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001872DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1873 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001874
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001875static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001876{
1877 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001878 int ret;
1879
1880 ret = spu_acquire_saved(ctx);
1881 if (ret)
1882 return ret;
Masato Noguchid40a01d2007-07-20 21:39:38 +02001883 if (val)
1884 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1885 else
1886 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001887 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001888
1889 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001890}
1891
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001892static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001893{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001894 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1895 return SPU_DECR_STATUS_RUNNING;
1896 else
1897 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001898}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001899DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1900 spufs_decr_status_set, "0x%llx\n",
1901 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001902
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001903static int spufs_event_mask_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001904{
1905 struct spu_context *ctx = data;
1906 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001907 int ret;
1908
1909 ret = spu_acquire_saved(ctx);
1910 if (ret)
1911 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001912 lscsa->event_mask.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001913 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001914
1915 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001916}
1917
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001918static u64 spufs_event_mask_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001919{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001920 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001921 return lscsa->event_mask.slot[0];
1922}
1923
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001924DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1925 spufs_event_mask_set, "0x%llx\n",
1926 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001927
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001928static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001929{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001930 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001931 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001932 stat = state->spu_chnlcnt_RW[0];
1933 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001934 return state->spu_chnldata_RW[0];
1935 return 0;
1936}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001937DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1938 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001939
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001940static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001941{
1942 struct spu_context *ctx = data;
1943 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001944 int ret;
1945
1946 ret = spu_acquire_saved(ctx);
1947 if (ret)
1948 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001949 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001950 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001951
1952 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001953}
1954
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001955static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001956{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001957 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001958 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001959}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001960DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1961 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001962
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001963static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001964{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001965 u64 num;
1966
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001967 if (ctx->state == SPU_STATE_RUNNABLE)
1968 num = ctx->spu->number;
1969 else
1970 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001971
1972 return num;
1973}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001974DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1975 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001976
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001977static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001978{
1979 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001980 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001981}
1982
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001983static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02001984{
1985 struct spu_context *ctx = data;
1986 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001987
1988 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02001989}
1990
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001991DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1992 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02001993
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001994static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001995{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001996 return ctx->csa.priv2.spu_lslr_RW;
1997}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001998DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1999 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002000
2001static int spufs_info_open(struct inode *inode, struct file *file)
2002{
2003 struct spufs_inode_info *i = SPUFS_I(inode);
2004 struct spu_context *ctx = i->i_ctx;
2005 file->private_data = ctx;
2006 return 0;
2007}
2008
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002009static int spufs_caps_show(struct seq_file *s, void *private)
2010{
2011 struct spu_context *ctx = s->private;
2012
2013 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2014 seq_puts(s, "sched\n");
2015 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2016 seq_puts(s, "step\n");
2017 return 0;
2018}
2019
2020static int spufs_caps_open(struct inode *inode, struct file *file)
2021{
2022 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2023}
2024
2025static const struct file_operations spufs_caps_fops = {
2026 .open = spufs_caps_open,
2027 .read = seq_read,
2028 .llseek = seq_lseek,
2029 .release = single_release,
2030};
2031
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002032static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2033 char __user *buf, size_t len, loff_t *pos)
2034{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002035 u32 data;
2036
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002037 /* EOF if there's no entry in the mbox */
2038 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2039 return 0;
2040
2041 data = ctx->csa.prob.pu_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002042
2043 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2044}
2045
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002046static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2047 size_t len, loff_t *pos)
2048{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002049 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002050 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002051
2052 if (!access_ok(VERIFY_WRITE, buf, len))
2053 return -EFAULT;
2054
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002055 ret = spu_acquire_saved(ctx);
2056 if (ret)
2057 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002058 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002059 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002060 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002061 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002062
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002063 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002064}
2065
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002066static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002067 .open = spufs_info_open,
2068 .read = spufs_mbox_info_read,
2069 .llseek = generic_file_llseek,
2070};
2071
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002072static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2073 char __user *buf, size_t len, loff_t *pos)
2074{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002075 u32 data;
2076
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002077 /* EOF if there's no entry in the ibox */
2078 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2079 return 0;
2080
2081 data = ctx->csa.priv2.puint_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002082
2083 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2084}
2085
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002086static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2087 size_t len, loff_t *pos)
2088{
2089 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002090 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002091
2092 if (!access_ok(VERIFY_WRITE, buf, len))
2093 return -EFAULT;
2094
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002095 ret = spu_acquire_saved(ctx);
2096 if (ret)
2097 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002098 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002099 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002100 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002101 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002102
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002103 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002104}
2105
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002106static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002107 .open = spufs_info_open,
2108 .read = spufs_ibox_info_read,
2109 .llseek = generic_file_llseek,
2110};
2111
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002112static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2113 char __user *buf, size_t len, loff_t *pos)
2114{
2115 int i, cnt;
2116 u32 data[4];
2117 u32 wbox_stat;
2118
2119 wbox_stat = ctx->csa.prob.mb_stat_R;
2120 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2121 for (i = 0; i < cnt; i++) {
2122 data[i] = ctx->csa.spu_mailbox_data[i];
2123 }
2124
2125 return simple_read_from_buffer(buf, len, pos, &data,
2126 cnt * sizeof(u32));
2127}
2128
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002129static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2130 size_t len, loff_t *pos)
2131{
2132 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002133 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002134
2135 if (!access_ok(VERIFY_WRITE, buf, len))
2136 return -EFAULT;
2137
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002138 ret = spu_acquire_saved(ctx);
2139 if (ret)
2140 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002141 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002142 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002143 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002144 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002145
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002146 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002147}
2148
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002149static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002150 .open = spufs_info_open,
2151 .read = spufs_wbox_info_read,
2152 .llseek = generic_file_llseek,
2153};
2154
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002155static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2156 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002157{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002158 struct spu_dma_info info;
2159 struct mfc_cq_sr *qp, *spuqp;
2160 int i;
2161
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002162 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2163 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2164 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2165 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2166 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2167 for (i = 0; i < 16; i++) {
2168 qp = &info.dma_info_command_data[i];
2169 spuqp = &ctx->csa.priv2.spuq[i];
2170
2171 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2172 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2173 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2174 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2175 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002176
2177 return simple_read_from_buffer(buf, len, pos, &info,
2178 sizeof info);
2179}
2180
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002181static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2182 size_t len, loff_t *pos)
2183{
2184 struct spu_context *ctx = file->private_data;
2185 int ret;
2186
2187 if (!access_ok(VERIFY_WRITE, buf, len))
2188 return -EFAULT;
2189
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002190 ret = spu_acquire_saved(ctx);
2191 if (ret)
2192 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002193 spin_lock(&ctx->csa.register_lock);
2194 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2195 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002196 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002197
2198 return ret;
2199}
2200
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002201static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002202 .open = spufs_info_open,
2203 .read = spufs_dma_info_read,
2204};
2205
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002206static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2207 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002208{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002209 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002210 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002211 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002212 int i;
2213
2214 if (len < ret)
2215 return -EINVAL;
2216
2217 if (!access_ok(VERIFY_WRITE, buf, len))
2218 return -EFAULT;
2219
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002220 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2221 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2222 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2223 for (i = 0; i < 8; i++) {
2224 qp = &info.proxydma_info_command_data[i];
2225 puqp = &ctx->csa.priv2.puq[i];
2226
2227 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2228 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2229 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2230 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2231 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002232
2233 return simple_read_from_buffer(buf, len, pos, &info,
2234 sizeof info);
2235}
2236
2237static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2238 size_t len, loff_t *pos)
2239{
2240 struct spu_context *ctx = file->private_data;
2241 int ret;
2242
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002243 ret = spu_acquire_saved(ctx);
2244 if (ret)
2245 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002246 spin_lock(&ctx->csa.register_lock);
2247 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002248 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002249 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002250
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002251 return ret;
2252}
2253
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002254static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002255 .open = spufs_info_open,
2256 .read = spufs_proxydma_info_read,
2257};
2258
Christoph Hellwig476273a2007-06-29 10:58:01 +10002259static int spufs_show_tid(struct seq_file *s, void *private)
2260{
2261 struct spu_context *ctx = s->private;
2262
2263 seq_printf(s, "%d\n", ctx->tid);
2264 return 0;
2265}
2266
2267static int spufs_tid_open(struct inode *inode, struct file *file)
2268{
2269 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2270}
2271
2272static const struct file_operations spufs_tid_fops = {
2273 .open = spufs_tid_open,
2274 .read = seq_read,
2275 .llseek = seq_lseek,
2276 .release = single_release,
2277};
2278
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002279static const char *ctx_state_names[] = {
2280 "user", "system", "iowait", "loaded"
2281};
2282
2283static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002284 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002285{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002286 struct timespec ts;
2287 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002288
Andre Detsch27ec41d2007-07-20 21:39:33 +02002289 /*
2290 * In general, utilization statistics are updated by the controlling
2291 * thread as the spu context moves through various well defined
2292 * state transitions, but if the context is lazily loaded its
2293 * utilization statistics are not updated as the controlling thread
2294 * is not tightly coupled with the execution of the spu context. We
2295 * calculate and apply the time delta from the last recorded state
2296 * of the spu context.
2297 */
2298 if (ctx->spu && ctx->stats.util_state == state) {
2299 ktime_get_ts(&ts);
2300 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2301 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002302
Andre Detsch27ec41d2007-07-20 21:39:33 +02002303 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002304}
2305
2306static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2307{
2308 unsigned long long slb_flts = ctx->stats.slb_flt;
2309
2310 if (ctx->state == SPU_STATE_RUNNABLE) {
2311 slb_flts += (ctx->spu->stats.slb_flt -
2312 ctx->stats.slb_flt_base);
2313 }
2314
2315 return slb_flts;
2316}
2317
2318static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2319{
2320 unsigned long long class2_intrs = ctx->stats.class2_intr;
2321
2322 if (ctx->state == SPU_STATE_RUNNABLE) {
2323 class2_intrs += (ctx->spu->stats.class2_intr -
2324 ctx->stats.class2_intr_base);
2325 }
2326
2327 return class2_intrs;
2328}
2329
2330
2331static int spufs_show_stat(struct seq_file *s, void *private)
2332{
2333 struct spu_context *ctx = s->private;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002334 int ret;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002335
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002336 ret = spu_acquire(ctx);
2337 if (ret)
2338 return ret;
2339
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002340 seq_printf(s, "%s %llu %llu %llu %llu "
2341 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002342 ctx_state_names[ctx->stats.util_state],
2343 spufs_acct_time(ctx, SPU_UTIL_USER),
2344 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2345 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2346 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002347 ctx->stats.vol_ctx_switch,
2348 ctx->stats.invol_ctx_switch,
2349 spufs_slb_flts(ctx),
2350 ctx->stats.hash_flt,
2351 ctx->stats.min_flt,
2352 ctx->stats.maj_flt,
2353 spufs_class2_intrs(ctx),
2354 ctx->stats.libassist);
2355 spu_release(ctx);
2356 return 0;
2357}
2358
2359static int spufs_stat_open(struct inode *inode, struct file *file)
2360{
2361 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2362}
2363
2364static const struct file_operations spufs_stat_fops = {
2365 .open = spufs_stat_open,
2366 .read = seq_read,
2367 .llseek = seq_lseek,
2368 .release = single_release,
2369};
2370
2371
Arnd Bergmann67207b92005-11-15 15:53:48 -05002372struct tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002373 { "capabilities", &spufs_caps_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002374 { "mem", &spufs_mem_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002375 { "regs", &spufs_regs_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002376 { "mbox", &spufs_mbox_fops, 0444, },
2377 { "ibox", &spufs_ibox_fops, 0444, },
2378 { "wbox", &spufs_wbox_fops, 0222, },
2379 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2380 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2381 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002382 { "signal1", &spufs_signal1_fops, 0666, },
2383 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002384 { "signal1_type", &spufs_signal1_type, 0666, },
2385 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002386 { "cntl", &spufs_cntl_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002387 { "fpcr", &spufs_fpcr_fops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002388 { "lslr", &spufs_lslr_ops, 0444, },
2389 { "mfc", &spufs_mfc_fops, 0666, },
2390 { "mss", &spufs_mss_fops, 0666, },
2391 { "npc", &spufs_npc_ops, 0666, },
2392 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002393 { "decr", &spufs_decr_ops, 0666, },
2394 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002395 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002396 { "event_status", &spufs_event_status_ops, 0444, },
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02002397 { "psmap", &spufs_psmap_fops, 0666, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002398 { "phys-id", &spufs_id_ops, 0666, },
2399 { "object-id", &spufs_object_id_ops, 0666, },
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002400 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2401 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2402 { "wbox_info", &spufs_wbox_info_fops, 0444, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002403 { "dma_info", &spufs_dma_info_fops, 0444, },
2404 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002405 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002406 { "stat", &spufs_stat_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002407 {},
2408};
Mark Nutter5737edd2006-10-24 18:31:16 +02002409
2410struct tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002411 { "capabilities", &spufs_caps_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002412 { "mem", &spufs_mem_fops, 0666, },
2413 { "mbox", &spufs_mbox_fops, 0444, },
2414 { "ibox", &spufs_ibox_fops, 0444, },
2415 { "wbox", &spufs_wbox_fops, 0222, },
2416 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2417 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2418 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002419 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2420 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002421 { "signal1_type", &spufs_signal1_type, 0666, },
2422 { "signal2_type", &spufs_signal2_type, 0666, },
2423 { "mss", &spufs_mss_fops, 0666, },
2424 { "mfc", &spufs_mfc_fops, 0666, },
2425 { "cntl", &spufs_cntl_fops, 0666, },
2426 { "npc", &spufs_npc_ops, 0666, },
2427 { "psmap", &spufs_psmap_fops, 0666, },
2428 { "phys-id", &spufs_id_ops, 0666, },
2429 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002430 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002431 { "stat", &spufs_stat_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002432 {},
2433};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002434
2435struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002436 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2437 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002438 { "lslr", NULL, spufs_lslr_get, 19 },
2439 { "decr", NULL, spufs_decr_get, 19 },
2440 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002441 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2442 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002443 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002444 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002445 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2446 { "event_mask", NULL, spufs_event_mask_get, 19 },
2447 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002448 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2449 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2450 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2451 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2452 { "proxydma_info", __spufs_proxydma_info_read,
2453 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002454 { "object-id", NULL, spufs_object_id_get, 19 },
2455 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002456 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002457};