blob: e4ab9d5a86f003040a7ce2f76f37a33c0555e8a5 [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;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100361 int ret = 0;
Mark Nutter6df10a82006-03-23 00:00:12 +0100362
Christoph Hellwig038200c2008-01-11 15:03:26 +1100363 spu_context_nospu_trace(spufs_ps_nopfn__enter, ctx);
364
Mark Nutter6df10a82006-03-23 00:00:12 +0100365 offset += vma->vm_pgoff << PAGE_SHIFT;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200366 if (offset >= ps_size)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100367 return NOPFN_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100368
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900369 /*
370 * We have to wait for context to be loaded before we have
371 * pages to hand out to the user, but we don't want to wait
372 * with the mmap_sem held.
373 * It is possible to drop the mmap_sem here, but then we need
374 * to return NOPFN_REFAULT because the mappings may have
375 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100376 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900377 if (spu_acquire(ctx))
378 return NOPFN_REFAULT;
379
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900380 if (ctx->state == SPU_STATE_SAVED) {
381 up_read(&current->mm->mmap_sem);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100382 spu_context_nospu_trace(spufs_ps_nopfn__sleep, ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100383 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100384 spu_context_trace(spufs_ps_nopfn__wake, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900385 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900386 } else {
387 area = ctx->spu->problem_phys + ps_offs;
388 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100389 spu_context_trace(spufs_ps_nopfn__insert, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900390 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100391
Christoph Hellwigeebead52008-02-08 15:50:41 +1100392 if (!ret)
393 spu_release(ctx);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100394 return NOPFN_REFAULT;
Mark Nutter6df10a82006-03-23 00:00:12 +0100395}
396
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200397#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100398static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
399 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +0100400{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100401 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +0100402}
403
404static struct vm_operations_struct spufs_cntl_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100405 .nopfn = spufs_cntl_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +0100406};
407
408/*
409 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100410 */
411static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
412{
413 if (!(vma->vm_flags & VM_SHARED))
414 return -EINVAL;
415
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100416 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100417 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200418 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100419
420 vma->vm_ops = &spufs_cntl_mmap_vmops;
421 return 0;
422}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200423#else /* SPUFS_MMAP_4K */
424#define spufs_cntl_mmap NULL
425#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100426
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900427static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200428{
429 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900430 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200431
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900432 ret = spu_acquire(ctx);
433 if (ret)
434 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900435 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200436 spu_release(ctx);
437
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900438 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200439}
440
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900441static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200442{
443 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900444 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200445
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900446 ret = spu_acquire(ctx);
447 if (ret)
448 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200449 ctx->ops->runcntl_write(ctx, val);
450 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900451
452 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200453}
454
Mark Nutter6df10a82006-03-23 00:00:12 +0100455static int spufs_cntl_open(struct inode *inode, struct file *file)
456{
457 struct spufs_inode_info *i = SPUFS_I(inode);
458 struct spu_context *ctx = i->i_ctx;
459
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000460 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100461 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200462 if (!i->i_openers++)
463 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000464 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900465 return spufs_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200466 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100467}
468
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200469static int
470spufs_cntl_release(struct inode *inode, struct file *file)
471{
472 struct spufs_inode_info *i = SPUFS_I(inode);
473 struct spu_context *ctx = i->i_ctx;
474
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900475 spufs_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200476
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000477 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200478 if (!--i->i_openers)
479 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000480 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200481 return 0;
482}
483
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800484static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100485 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200486 .release = spufs_cntl_release,
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900487 .read = spufs_attr_read,
488 .write = spufs_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100489 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100490};
491
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500492static int
493spufs_regs_open(struct inode *inode, struct file *file)
494{
495 struct spufs_inode_info *i = SPUFS_I(inode);
496 file->private_data = i->i_ctx;
497 return 0;
498}
499
500static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100501__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
502 size_t size, loff_t *pos)
503{
504 struct spu_lscsa *lscsa = ctx->csa.lscsa;
505 return simple_read_from_buffer(buffer, size, pos,
506 lscsa->gprs, sizeof lscsa->gprs);
507}
508
509static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500510spufs_regs_read(struct file *file, char __user *buffer,
511 size_t size, loff_t *pos)
512{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500513 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100514 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500515
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900516 ret = spu_acquire_saved(ctx);
517 if (ret)
518 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100519 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200520 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500521 return ret;
522}
523
524static ssize_t
525spufs_regs_write(struct file *file, const char __user *buffer,
526 size_t size, loff_t *pos)
527{
528 struct spu_context *ctx = file->private_data;
529 struct spu_lscsa *lscsa = ctx->csa.lscsa;
530 int ret;
531
532 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
533 if (size <= 0)
534 return -EFBIG;
535 *pos += size;
536
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900537 ret = spu_acquire_saved(ctx);
538 if (ret)
539 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500540
541 ret = copy_from_user(lscsa->gprs + *pos - size,
542 buffer, size) ? -EFAULT : size;
543
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200544 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500545 return ret;
546}
547
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800548static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500549 .open = spufs_regs_open,
550 .read = spufs_regs_read,
551 .write = spufs_regs_write,
552 .llseek = generic_file_llseek,
553};
554
555static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100556__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
557 size_t size, loff_t * pos)
558{
559 struct spu_lscsa *lscsa = ctx->csa.lscsa;
560 return simple_read_from_buffer(buffer, size, pos,
561 &lscsa->fpcr, sizeof(lscsa->fpcr));
562}
563
564static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500565spufs_fpcr_read(struct file *file, char __user * buffer,
566 size_t size, loff_t * pos)
567{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500568 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100569 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500570
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900571 ret = spu_acquire_saved(ctx);
572 if (ret)
573 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100574 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200575 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500576 return ret;
577}
578
579static ssize_t
580spufs_fpcr_write(struct file *file, const char __user * buffer,
581 size_t size, loff_t * pos)
582{
583 struct spu_context *ctx = file->private_data;
584 struct spu_lscsa *lscsa = ctx->csa.lscsa;
585 int ret;
586
587 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
588 if (size <= 0)
589 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900590
591 ret = spu_acquire_saved(ctx);
592 if (ret)
593 return ret;
594
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500595 *pos += size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500596 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
597 buffer, size) ? -EFAULT : size;
598
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200599 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500600 return ret;
601}
602
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800603static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500604 .open = spufs_regs_open,
605 .read = spufs_fpcr_read,
606 .write = spufs_fpcr_write,
607 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500608};
609
610/* generic open function for all pipe-like files */
611static int spufs_pipe_open(struct inode *inode, struct file *file)
612{
613 struct spufs_inode_info *i = SPUFS_I(inode);
614 file->private_data = i->i_ctx;
615
616 return nonseekable_open(inode, file);
617}
618
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200619/*
620 * Read as many bytes from the mailbox as possible, until
621 * one of the conditions becomes true:
622 *
623 * - no more data available in the mailbox
624 * - end of the user provided buffer
625 * - end of the mapped area
626 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500627static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
628 size_t len, loff_t *pos)
629{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500630 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200631 u32 mbox_data, __user *udata;
632 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500633
634 if (len < 4)
635 return -EINVAL;
636
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200637 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500638 return -EFAULT;
639
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200640 udata = (void __user *)buf;
641
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900642 count = spu_acquire(ctx);
643 if (count)
644 return count;
645
Arnd Bergmann274cef52006-10-24 18:01:42 +0200646 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200647 int ret;
648 ret = ctx->ops->mbox_read(ctx, &mbox_data);
649 if (ret == 0)
650 break;
651
652 /*
653 * at the end of the mapped area, we can fault
654 * but still need to return the data we have
655 * read successfully so far.
656 */
657 ret = __put_user(mbox_data, udata);
658 if (ret) {
659 if (!count)
660 count = -EFAULT;
661 break;
662 }
663 }
664 spu_release(ctx);
665
666 if (!count)
667 count = -EAGAIN;
668
669 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500670}
671
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800672static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500673 .open = spufs_pipe_open,
674 .read = spufs_mbox_read,
675};
676
677static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
678 size_t len, loff_t *pos)
679{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500680 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900681 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500682 u32 mbox_stat;
683
684 if (len < 4)
685 return -EINVAL;
686
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900687 ret = spu_acquire(ctx);
688 if (ret)
689 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500690
691 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
692
693 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500694
695 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
696 return -EFAULT;
697
698 return 4;
699}
700
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800701static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500702 .open = spufs_pipe_open,
703 .read = spufs_mbox_stat_read,
704};
705
706/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500707size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500708{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500709 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500710}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500711
712static int spufs_ibox_fasync(int fd, struct file *file, int on)
713{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500714 struct spu_context *ctx = file->private_data;
715
716 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
717}
718
719/* interrupt-level ibox callback function. */
720void spufs_ibox_callback(struct spu *spu)
721{
722 struct spu_context *ctx = spu->ctx;
723
Luke Browninge65c2f62007-12-20 16:39:59 +0900724 if (!ctx)
725 return;
726
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500727 wake_up_all(&ctx->ibox_wq);
728 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500729}
730
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200731/*
732 * Read as many bytes from the interrupt mailbox as possible, until
733 * one of the conditions becomes true:
734 *
735 * - no more data available in the mailbox
736 * - end of the user provided buffer
737 * - end of the mapped area
738 *
739 * If the file is opened without O_NONBLOCK, we wait here until
740 * any data is available, but return when we have been able to
741 * read something.
742 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500743static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
744 size_t len, loff_t *pos)
745{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500746 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200747 u32 ibox_data, __user *udata;
748 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500749
750 if (len < 4)
751 return -EINVAL;
752
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200753 if (!access_ok(VERIFY_WRITE, buf, len))
754 return -EFAULT;
755
756 udata = (void __user *)buf;
757
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900758 count = spu_acquire(ctx);
759 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100760 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500761
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200762 /* wait only for the first element */
763 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500764 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100765 if (!spu_ibox_read(ctx, &ibox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200766 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100767 goto out_unlock;
768 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500769 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200770 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100771 if (count)
772 goto out;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200773 }
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200774
775 /* if we can't write at all, return -EFAULT */
776 count = __put_user(ibox_data, udata);
777 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100778 goto out_unlock;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200779
780 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
781 int ret;
782 ret = ctx->ops->ibox_read(ctx, &ibox_data);
783 if (ret == 0)
784 break;
785 /*
786 * at the end of the mapped area, we can fault
787 * but still need to return the data we have
788 * read successfully so far.
789 */
790 ret = __put_user(ibox_data, udata);
791 if (ret)
792 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500793 }
794
Christoph Hellwigeebead52008-02-08 15:50:41 +1100795out_unlock:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500796 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100797out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200798 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500799}
800
801static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
802{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500803 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500804 unsigned int mask;
805
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500806 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500807
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900808 /*
809 * For now keep this uninterruptible and also ignore the rule
810 * that poll should not sleep. Will be fixed later.
811 */
812 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500813 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
814 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500815
816 return mask;
817}
818
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800819static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500820 .open = spufs_pipe_open,
821 .read = spufs_ibox_read,
822 .poll = spufs_ibox_poll,
823 .fasync = spufs_ibox_fasync,
824};
825
826static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
827 size_t len, loff_t *pos)
828{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500829 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900830 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500831 u32 ibox_stat;
832
833 if (len < 4)
834 return -EINVAL;
835
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900836 ret = spu_acquire(ctx);
837 if (ret)
838 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500839 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
840 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500841
842 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
843 return -EFAULT;
844
845 return 4;
846}
847
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800848static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500849 .open = spufs_pipe_open,
850 .read = spufs_ibox_stat_read,
851};
852
853/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500854size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500855{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500856 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500857}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500858
859static int spufs_wbox_fasync(int fd, struct file *file, int on)
860{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500861 struct spu_context *ctx = file->private_data;
862 int ret;
863
864 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
865
866 return ret;
867}
868
869/* interrupt-level wbox callback function. */
870void spufs_wbox_callback(struct spu *spu)
871{
872 struct spu_context *ctx = spu->ctx;
873
Luke Browninge65c2f62007-12-20 16:39:59 +0900874 if (!ctx)
875 return;
876
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500877 wake_up_all(&ctx->wbox_wq);
878 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500879}
880
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200881/*
882 * Write as many bytes to the interrupt mailbox as possible, until
883 * one of the conditions becomes true:
884 *
885 * - the mailbox is full
886 * - end of the user provided buffer
887 * - end of the mapped area
888 *
889 * If the file is opened without O_NONBLOCK, we wait here until
890 * space is availabyl, but return when we have been able to
891 * write something.
892 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500893static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
894 size_t len, loff_t *pos)
895{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500896 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200897 u32 wbox_data, __user *udata;
898 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500899
900 if (len < 4)
901 return -EINVAL;
902
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200903 udata = (void __user *)buf;
904 if (!access_ok(VERIFY_READ, buf, len))
905 return -EFAULT;
906
907 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500908 return -EFAULT;
909
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900910 count = spu_acquire(ctx);
911 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100912 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500913
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200914 /*
915 * make sure we can at least write one element, by waiting
916 * in case of !O_NONBLOCK
917 */
918 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500919 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100920 if (!spu_wbox_write(ctx, wbox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200921 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100922 goto out_unlock;
923 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500924 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200925 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100926 if (count)
927 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500928 }
929
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500930
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200931 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200932 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
933 int ret;
934 ret = __get_user(wbox_data, udata);
935 if (ret)
936 break;
937
938 ret = spu_wbox_write(ctx, wbox_data);
939 if (ret == 0)
940 break;
941 }
942
Christoph Hellwigeebead52008-02-08 15:50:41 +1100943out_unlock:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200944 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100945out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200946 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500947}
948
949static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
950{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500951 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500952 unsigned int mask;
953
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500954 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500955
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900956 /*
957 * For now keep this uninterruptible and also ignore the rule
958 * that poll should not sleep. Will be fixed later.
959 */
960 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500961 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
962 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500963
964 return mask;
965}
966
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800967static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500968 .open = spufs_pipe_open,
969 .write = spufs_wbox_write,
970 .poll = spufs_wbox_poll,
971 .fasync = spufs_wbox_fasync,
972};
973
974static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
975 size_t len, loff_t *pos)
976{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500977 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900978 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500979 u32 wbox_stat;
980
981 if (len < 4)
982 return -EINVAL;
983
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900984 ret = spu_acquire(ctx);
985 if (ret)
986 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500987 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
988 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500989
990 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
991 return -EFAULT;
992
993 return 4;
994}
995
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800996static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500997 .open = spufs_pipe_open,
998 .read = spufs_wbox_stat_read,
999};
1000
Mark Nutter6df10a82006-03-23 00:00:12 +01001001static int spufs_signal1_open(struct inode *inode, struct file *file)
1002{
1003 struct spufs_inode_info *i = SPUFS_I(inode);
1004 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001005
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001006 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001007 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001008 if (!i->i_openers++)
1009 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001010 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001011 return nonseekable_open(inode, file);
1012}
1013
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001014static int
1015spufs_signal1_release(struct inode *inode, struct file *file)
1016{
1017 struct spufs_inode_info *i = SPUFS_I(inode);
1018 struct spu_context *ctx = i->i_ctx;
1019
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001020 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001021 if (!--i->i_openers)
1022 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001023 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001024 return 0;
1025}
1026
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001027static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001028 size_t len, loff_t *pos)
1029{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001030 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001031 u32 data;
1032
Arnd Bergmann67207b92005-11-15 15:53:48 -05001033 if (len < 4)
1034 return -EINVAL;
1035
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001036 if (ctx->csa.spu_chnlcnt_RW[3]) {
1037 data = ctx->csa.spu_chnldata_RW[3];
1038 ret = 4;
1039 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001040
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001041 if (!ret)
1042 goto out;
1043
Arnd Bergmann67207b92005-11-15 15:53:48 -05001044 if (copy_to_user(buf, &data, 4))
1045 return -EFAULT;
1046
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001047out:
1048 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001049}
1050
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001051static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1052 size_t len, loff_t *pos)
1053{
1054 int ret;
1055 struct spu_context *ctx = file->private_data;
1056
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001057 ret = spu_acquire_saved(ctx);
1058 if (ret)
1059 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001060 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001061 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001062
1063 return ret;
1064}
1065
Arnd Bergmann67207b92005-11-15 15:53:48 -05001066static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1067 size_t len, loff_t *pos)
1068{
1069 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001070 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001071 u32 data;
1072
1073 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001074
1075 if (len < 4)
1076 return -EINVAL;
1077
1078 if (copy_from_user(&data, buf, 4))
1079 return -EFAULT;
1080
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001081 ret = spu_acquire(ctx);
1082 if (ret)
1083 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001084 ctx->ops->signal1_write(ctx, data);
1085 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001086
1087 return 4;
1088}
1089
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001090static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1091 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001092{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001093#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001094 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001095#elif PAGE_SIZE == 0x10000
1096 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1097 * signal 1 and 2 area
1098 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001099 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001100#else
1101#error unsupported page size
1102#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001103}
1104
1105static struct vm_operations_struct spufs_signal1_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001106 .nopfn = spufs_signal1_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001107};
1108
1109static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1110{
1111 if (!(vma->vm_flags & VM_SHARED))
1112 return -EINVAL;
1113
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001114 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001115 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001116 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001117
1118 vma->vm_ops = &spufs_signal1_mmap_vmops;
1119 return 0;
1120}
Mark Nutter6df10a82006-03-23 00:00:12 +01001121
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001122static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001123 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001124 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001125 .read = spufs_signal1_read,
1126 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001127 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001128};
1129
Jeremy Kerrd054b362007-07-20 21:39:31 +02001130static const struct file_operations spufs_signal1_nosched_fops = {
1131 .open = spufs_signal1_open,
1132 .release = spufs_signal1_release,
1133 .write = spufs_signal1_write,
1134 .mmap = spufs_signal1_mmap,
1135};
1136
Mark Nutter6df10a82006-03-23 00:00:12 +01001137static int spufs_signal2_open(struct inode *inode, struct file *file)
1138{
1139 struct spufs_inode_info *i = SPUFS_I(inode);
1140 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001141
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001142 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001143 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001144 if (!i->i_openers++)
1145 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001146 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001147 return nonseekable_open(inode, file);
1148}
1149
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001150static int
1151spufs_signal2_release(struct inode *inode, struct file *file)
1152{
1153 struct spufs_inode_info *i = SPUFS_I(inode);
1154 struct spu_context *ctx = i->i_ctx;
1155
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001156 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001157 if (!--i->i_openers)
1158 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001159 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001160 return 0;
1161}
1162
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001163static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001164 size_t len, loff_t *pos)
1165{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001166 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001167 u32 data;
1168
Arnd Bergmann67207b92005-11-15 15:53:48 -05001169 if (len < 4)
1170 return -EINVAL;
1171
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001172 if (ctx->csa.spu_chnlcnt_RW[4]) {
1173 data = ctx->csa.spu_chnldata_RW[4];
1174 ret = 4;
1175 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001176
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001177 if (!ret)
1178 goto out;
1179
Arnd Bergmann67207b92005-11-15 15:53:48 -05001180 if (copy_to_user(buf, &data, 4))
1181 return -EFAULT;
1182
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001183out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001184 return ret;
1185}
1186
1187static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1188 size_t len, loff_t *pos)
1189{
1190 struct spu_context *ctx = file->private_data;
1191 int ret;
1192
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001193 ret = spu_acquire_saved(ctx);
1194 if (ret)
1195 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001196 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001197 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001198
1199 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001200}
1201
1202static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1203 size_t len, loff_t *pos)
1204{
1205 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001206 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001207 u32 data;
1208
1209 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001210
1211 if (len < 4)
1212 return -EINVAL;
1213
1214 if (copy_from_user(&data, buf, 4))
1215 return -EFAULT;
1216
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001217 ret = spu_acquire(ctx);
1218 if (ret)
1219 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001220 ctx->ops->signal2_write(ctx, data);
1221 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001222
1223 return 4;
1224}
1225
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001226#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001227static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1228 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001229{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001230#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001231 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001232#elif PAGE_SIZE == 0x10000
1233 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1234 * signal 1 and 2 area
1235 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001236 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001237#else
1238#error unsupported page size
1239#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001240}
1241
1242static struct vm_operations_struct spufs_signal2_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001243 .nopfn = spufs_signal2_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001244};
1245
1246static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1247{
1248 if (!(vma->vm_flags & VM_SHARED))
1249 return -EINVAL;
1250
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001251 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001252 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001253 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001254
1255 vma->vm_ops = &spufs_signal2_mmap_vmops;
1256 return 0;
1257}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001258#else /* SPUFS_MMAP_4K */
1259#define spufs_signal2_mmap NULL
1260#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001261
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001262static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001263 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001264 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001265 .read = spufs_signal2_read,
1266 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001267 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001268};
1269
Jeremy Kerrd054b362007-07-20 21:39:31 +02001270static const struct file_operations spufs_signal2_nosched_fops = {
1271 .open = spufs_signal2_open,
1272 .release = spufs_signal2_release,
1273 .write = spufs_signal2_write,
1274 .mmap = spufs_signal2_mmap,
1275};
1276
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001277/*
1278 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1279 * work of acquiring (or not) the SPU context before calling through
1280 * to the actual get routine. The set routine is called directly.
1281 */
1282#define SPU_ATTR_NOACQUIRE 0
1283#define SPU_ATTR_ACQUIRE 1
1284#define SPU_ATTR_ACQUIRE_SAVED 2
1285
1286#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001287static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001288{ \
1289 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001290 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001291 \
1292 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001293 ret = spu_acquire(ctx); \
1294 if (ret) \
1295 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001296 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001297 spu_release(ctx); \
1298 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001299 ret = spu_acquire_saved(ctx); \
1300 if (ret) \
1301 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001302 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001303 spu_release_saved(ctx); \
1304 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001305 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001306 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001307 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001308} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001309DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001310
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001311static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001312{
1313 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001314 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001315
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001316 ret = spu_acquire(ctx);
1317 if (ret)
1318 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001319 ctx->ops->signal1_type_set(ctx, val);
1320 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001321
1322 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001323}
1324
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001325static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001326{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001327 return ctx->ops->signal1_type_get(ctx);
1328}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001329DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1330 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001331
Arnd Bergmann67207b92005-11-15 15:53:48 -05001332
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001333static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001334{
1335 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001336 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001337
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001338 ret = spu_acquire(ctx);
1339 if (ret)
1340 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001341 ctx->ops->signal2_type_set(ctx, val);
1342 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001343
1344 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001345}
1346
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001347static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001348{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001349 return ctx->ops->signal2_type_get(ctx);
1350}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001351DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1352 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001353
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001354#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001355static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1356 unsigned long address)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001357{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001358 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001359}
1360
1361static struct vm_operations_struct spufs_mss_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001362 .nopfn = spufs_mss_mmap_nopfn,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001363};
1364
1365/*
1366 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001367 */
1368static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1369{
1370 if (!(vma->vm_flags & VM_SHARED))
1371 return -EINVAL;
1372
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001373 vma->vm_flags |= VM_IO | VM_PFNMAP;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001374 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001375 | _PAGE_NO_CACHE | _PAGE_GUARDED);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001376
1377 vma->vm_ops = &spufs_mss_mmap_vmops;
1378 return 0;
1379}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001380#else /* SPUFS_MMAP_4K */
1381#define spufs_mss_mmap NULL
1382#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001383
1384static int spufs_mss_open(struct inode *inode, struct file *file)
1385{
1386 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001387 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001388
1389 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001390
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001391 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001392 if (!i->i_openers++)
1393 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001394 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001395 return nonseekable_open(inode, file);
1396}
1397
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001398static int
1399spufs_mss_release(struct inode *inode, struct file *file)
1400{
1401 struct spufs_inode_info *i = SPUFS_I(inode);
1402 struct spu_context *ctx = i->i_ctx;
1403
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001404 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001405 if (!--i->i_openers)
1406 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001407 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001408 return 0;
1409}
1410
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001411static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001412 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001413 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001414 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001415};
1416
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001417static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1418 unsigned long address)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001419{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001420 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001421}
1422
1423static struct vm_operations_struct spufs_psmap_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001424 .nopfn = spufs_psmap_mmap_nopfn,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001425};
1426
1427/*
1428 * mmap support for full problem state area [0x00000 - 0x1ffff].
1429 */
1430static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1431{
1432 if (!(vma->vm_flags & VM_SHARED))
1433 return -EINVAL;
1434
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001435 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001436 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1437 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1438
1439 vma->vm_ops = &spufs_psmap_mmap_vmops;
1440 return 0;
1441}
1442
1443static int spufs_psmap_open(struct inode *inode, struct file *file)
1444{
1445 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001446 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001447
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001448 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001449 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001450 if (!i->i_openers++)
1451 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001452 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001453 return nonseekable_open(inode, file);
1454}
1455
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001456static int
1457spufs_psmap_release(struct inode *inode, struct file *file)
1458{
1459 struct spufs_inode_info *i = SPUFS_I(inode);
1460 struct spu_context *ctx = i->i_ctx;
1461
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001462 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001463 if (!--i->i_openers)
1464 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001465 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001466 return 0;
1467}
1468
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001469static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001470 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001471 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001472 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001473};
1474
1475
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001476#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001477static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1478 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001479{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001480 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +01001481}
1482
1483static struct vm_operations_struct spufs_mfc_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001484 .nopfn = spufs_mfc_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001485};
1486
1487/*
1488 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001489 */
1490static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1491{
1492 if (!(vma->vm_flags & VM_SHARED))
1493 return -EINVAL;
1494
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001495 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001496 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001497 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001498
1499 vma->vm_ops = &spufs_mfc_mmap_vmops;
1500 return 0;
1501}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001502#else /* SPUFS_MMAP_4K */
1503#define spufs_mfc_mmap NULL
1504#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001505
1506static int spufs_mfc_open(struct inode *inode, struct file *file)
1507{
1508 struct spufs_inode_info *i = SPUFS_I(inode);
1509 struct spu_context *ctx = i->i_ctx;
1510
1511 /* we don't want to deal with DMA into other processes */
1512 if (ctx->owner != current->mm)
1513 return -EINVAL;
1514
1515 if (atomic_read(&inode->i_count) != 1)
1516 return -EBUSY;
1517
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001518 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001519 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001520 if (!i->i_openers++)
1521 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001522 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001523 return nonseekable_open(inode, file);
1524}
1525
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001526static int
1527spufs_mfc_release(struct inode *inode, struct file *file)
1528{
1529 struct spufs_inode_info *i = SPUFS_I(inode);
1530 struct spu_context *ctx = i->i_ctx;
1531
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001532 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001533 if (!--i->i_openers)
1534 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001535 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001536 return 0;
1537}
1538
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001539/* interrupt-level mfc callback function. */
1540void spufs_mfc_callback(struct spu *spu)
1541{
1542 struct spu_context *ctx = spu->ctx;
1543
Luke Browninge65c2f62007-12-20 16:39:59 +09001544 if (!ctx)
1545 return;
1546
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001547 wake_up_all(&ctx->mfc_wq);
1548
1549 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1550 if (ctx->mfc_fasync) {
1551 u32 free_elements, tagstatus;
1552 unsigned int mask;
1553
1554 /* no need for spu_acquire in interrupt context */
1555 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1556 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1557
1558 mask = 0;
1559 if (free_elements & 0xffff)
1560 mask |= POLLOUT;
1561 if (tagstatus & ctx->tagwait)
1562 mask |= POLLIN;
1563
1564 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1565 }
1566}
1567
1568static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1569{
1570 /* See if there is one tag group is complete */
1571 /* FIXME we need locking around tagwait */
1572 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1573 ctx->tagwait &= ~*status;
1574 if (*status)
1575 return 1;
1576
1577 /* enable interrupt waiting for any tag group,
1578 may silently fail if interrupts are already enabled */
1579 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1580 return 0;
1581}
1582
1583static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1584 size_t size, loff_t *pos)
1585{
1586 struct spu_context *ctx = file->private_data;
1587 int ret = -EINVAL;
1588 u32 status;
1589
1590 if (size != 4)
1591 goto out;
1592
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001593 ret = spu_acquire(ctx);
1594 if (ret)
1595 return ret;
1596
1597 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001598 if (file->f_flags & O_NONBLOCK) {
1599 status = ctx->ops->read_mfc_tagstatus(ctx);
1600 if (!(status & ctx->tagwait))
1601 ret = -EAGAIN;
1602 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001603 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001604 ctx->tagwait &= ~status;
1605 } else {
1606 ret = spufs_wait(ctx->mfc_wq,
1607 spufs_read_mfc_tagstatus(ctx, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001608 if (ret)
1609 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001610 }
1611 spu_release(ctx);
1612
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001613 ret = 4;
1614 if (copy_to_user(buffer, &status, 4))
1615 ret = -EFAULT;
1616
1617out:
1618 return ret;
1619}
1620
1621static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1622{
1623 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1624 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1625
1626 switch (cmd->cmd) {
1627 case MFC_PUT_CMD:
1628 case MFC_PUTF_CMD:
1629 case MFC_PUTB_CMD:
1630 case MFC_GET_CMD:
1631 case MFC_GETF_CMD:
1632 case MFC_GETB_CMD:
1633 break;
1634 default:
1635 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1636 return -EIO;
1637 }
1638
1639 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1640 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1641 cmd->ea, cmd->lsa);
1642 return -EIO;
1643 }
1644
1645 switch (cmd->size & 0xf) {
1646 case 1:
1647 break;
1648 case 2:
1649 if (cmd->lsa & 1)
1650 goto error;
1651 break;
1652 case 4:
1653 if (cmd->lsa & 3)
1654 goto error;
1655 break;
1656 case 8:
1657 if (cmd->lsa & 7)
1658 goto error;
1659 break;
1660 case 0:
1661 if (cmd->lsa & 15)
1662 goto error;
1663 break;
1664 error:
1665 default:
1666 pr_debug("invalid DMA alignment %x for size %x\n",
1667 cmd->lsa & 0xf, cmd->size);
1668 return -EIO;
1669 }
1670
1671 if (cmd->size > 16 * 1024) {
1672 pr_debug("invalid DMA size %x\n", cmd->size);
1673 return -EIO;
1674 }
1675
1676 if (cmd->tag & 0xfff0) {
1677 /* we reserve the higher tag numbers for kernel use */
1678 pr_debug("invalid DMA tag\n");
1679 return -EIO;
1680 }
1681
1682 if (cmd->class) {
1683 /* not supported in this version */
1684 pr_debug("invalid DMA class\n");
1685 return -EIO;
1686 }
1687
1688 return 0;
1689}
1690
1691static int spu_send_mfc_command(struct spu_context *ctx,
1692 struct mfc_dma_command cmd,
1693 int *error)
1694{
1695 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1696 if (*error == -EAGAIN) {
1697 /* wait for any tag group to complete
1698 so we have space for the new command */
1699 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1700 /* try again, because the queue might be
1701 empty again */
1702 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1703 if (*error == -EAGAIN)
1704 return 0;
1705 }
1706 return 1;
1707}
1708
1709static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1710 size_t size, loff_t *pos)
1711{
1712 struct spu_context *ctx = file->private_data;
1713 struct mfc_dma_command cmd;
1714 int ret = -EINVAL;
1715
1716 if (size != sizeof cmd)
1717 goto out;
1718
1719 ret = -EFAULT;
1720 if (copy_from_user(&cmd, buffer, sizeof cmd))
1721 goto out;
1722
1723 ret = spufs_check_valid_dma(&cmd);
1724 if (ret)
1725 goto out;
1726
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001727 ret = spu_acquire(ctx);
1728 if (ret)
1729 goto out;
1730
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001731 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001732 if (ret)
1733 goto out;
1734
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001735 if (file->f_flags & O_NONBLOCK) {
1736 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1737 } else {
1738 int status;
1739 ret = spufs_wait(ctx->mfc_wq,
1740 spu_send_mfc_command(ctx, cmd, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001741 if (ret)
1742 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001743 if (status)
1744 ret = status;
1745 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001746
1747 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001748 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001749
1750 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001751 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001752
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001753out_unlock:
1754 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001755out:
1756 return ret;
1757}
1758
1759static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1760{
1761 struct spu_context *ctx = file->private_data;
1762 u32 free_elements, tagstatus;
1763 unsigned int mask;
1764
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001765 poll_wait(file, &ctx->mfc_wq, wait);
1766
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001767 /*
1768 * For now keep this uninterruptible and also ignore the rule
1769 * that poll should not sleep. Will be fixed later.
1770 */
1771 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001772 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1773 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1774 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1775 spu_release(ctx);
1776
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001777 mask = 0;
1778 if (free_elements & 0xffff)
1779 mask |= POLLOUT | POLLWRNORM;
1780 if (tagstatus & ctx->tagwait)
1781 mask |= POLLIN | POLLRDNORM;
1782
1783 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1784 free_elements, tagstatus, ctx->tagwait);
1785
1786 return mask;
1787}
1788
Al Viro73b6af82006-06-25 16:42:33 -07001789static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001790{
1791 struct spu_context *ctx = file->private_data;
1792 int ret;
1793
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001794 ret = spu_acquire(ctx);
1795 if (ret)
Christoph Hellwigeebead52008-02-08 15:50:41 +11001796 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001797#if 0
1798/* this currently hangs */
1799 ret = spufs_wait(ctx->mfc_wq,
1800 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1801 if (ret)
1802 goto out;
1803 ret = spufs_wait(ctx->mfc_wq,
1804 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001805 if (ret)
1806 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001807#else
1808 ret = 0;
1809#endif
1810 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001811out:
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001812 return ret;
1813}
1814
1815static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1816 int datasync)
1817{
Al Viro73b6af82006-06-25 16:42:33 -07001818 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001819}
1820
1821static int spufs_mfc_fasync(int fd, struct file *file, int on)
1822{
1823 struct spu_context *ctx = file->private_data;
1824
1825 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1826}
1827
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001828static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001829 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001830 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001831 .read = spufs_mfc_read,
1832 .write = spufs_mfc_write,
1833 .poll = spufs_mfc_poll,
1834 .flush = spufs_mfc_flush,
1835 .fsync = spufs_mfc_fsync,
1836 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001837 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001838};
1839
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001840static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001841{
1842 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001843 int ret;
1844
1845 ret = spu_acquire(ctx);
1846 if (ret)
1847 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001848 ctx->ops->npc_write(ctx, val);
1849 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001850
1851 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001852}
1853
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001854static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001855{
1856 return ctx->ops->npc_read(ctx);
1857}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001858DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1859 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001860
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001861static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001862{
1863 struct spu_context *ctx = data;
1864 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001865 int ret;
1866
1867 ret = spu_acquire_saved(ctx);
1868 if (ret)
1869 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001870 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001871 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001872
1873 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001874}
1875
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001876static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001877{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001878 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001879 return lscsa->decr.slot[0];
1880}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001881DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1882 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001883
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001884static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001885{
1886 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001887 int ret;
1888
1889 ret = spu_acquire_saved(ctx);
1890 if (ret)
1891 return ret;
Masato Noguchid40a01d2007-07-20 21:39:38 +02001892 if (val)
1893 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1894 else
1895 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001896 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001897
1898 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001899}
1900
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001901static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001902{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001903 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1904 return SPU_DECR_STATUS_RUNNING;
1905 else
1906 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001907}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001908DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1909 spufs_decr_status_set, "0x%llx\n",
1910 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001911
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001912static int spufs_event_mask_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001913{
1914 struct spu_context *ctx = data;
1915 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001916 int ret;
1917
1918 ret = spu_acquire_saved(ctx);
1919 if (ret)
1920 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001921 lscsa->event_mask.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001922 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001923
1924 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001925}
1926
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001927static u64 spufs_event_mask_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001928{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001929 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001930 return lscsa->event_mask.slot[0];
1931}
1932
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001933DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1934 spufs_event_mask_set, "0x%llx\n",
1935 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001936
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001937static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001938{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001939 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001940 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001941 stat = state->spu_chnlcnt_RW[0];
1942 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001943 return state->spu_chnldata_RW[0];
1944 return 0;
1945}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001946DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1947 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001948
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001949static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001950{
1951 struct spu_context *ctx = data;
1952 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001953 int ret;
1954
1955 ret = spu_acquire_saved(ctx);
1956 if (ret)
1957 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001958 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001959 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001960
1961 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001962}
1963
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001964static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001965{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001966 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001967 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001968}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001969DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1970 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001971
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001972static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001973{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001974 u64 num;
1975
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001976 if (ctx->state == SPU_STATE_RUNNABLE)
1977 num = ctx->spu->number;
1978 else
1979 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001980
1981 return num;
1982}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001983DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1984 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001985
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001986static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001987{
1988 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001989 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001990}
1991
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001992static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02001993{
1994 struct spu_context *ctx = data;
1995 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001996
1997 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02001998}
1999
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002000DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2001 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02002002
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002003static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002004{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002005 return ctx->csa.priv2.spu_lslr_RW;
2006}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002007DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2008 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002009
2010static int spufs_info_open(struct inode *inode, struct file *file)
2011{
2012 struct spufs_inode_info *i = SPUFS_I(inode);
2013 struct spu_context *ctx = i->i_ctx;
2014 file->private_data = ctx;
2015 return 0;
2016}
2017
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002018static int spufs_caps_show(struct seq_file *s, void *private)
2019{
2020 struct spu_context *ctx = s->private;
2021
2022 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2023 seq_puts(s, "sched\n");
2024 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2025 seq_puts(s, "step\n");
2026 return 0;
2027}
2028
2029static int spufs_caps_open(struct inode *inode, struct file *file)
2030{
2031 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2032}
2033
2034static const struct file_operations spufs_caps_fops = {
2035 .open = spufs_caps_open,
2036 .read = seq_read,
2037 .llseek = seq_lseek,
2038 .release = single_release,
2039};
2040
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002041static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2042 char __user *buf, size_t len, loff_t *pos)
2043{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002044 u32 data;
2045
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002046 /* EOF if there's no entry in the mbox */
2047 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2048 return 0;
2049
2050 data = ctx->csa.prob.pu_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002051
2052 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2053}
2054
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002055static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2056 size_t len, loff_t *pos)
2057{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002058 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002059 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002060
2061 if (!access_ok(VERIFY_WRITE, buf, len))
2062 return -EFAULT;
2063
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002064 ret = spu_acquire_saved(ctx);
2065 if (ret)
2066 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002067 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002068 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002069 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002070 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002071
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002072 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002073}
2074
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002075static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002076 .open = spufs_info_open,
2077 .read = spufs_mbox_info_read,
2078 .llseek = generic_file_llseek,
2079};
2080
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002081static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2082 char __user *buf, size_t len, loff_t *pos)
2083{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002084 u32 data;
2085
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002086 /* EOF if there's no entry in the ibox */
2087 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2088 return 0;
2089
2090 data = ctx->csa.priv2.puint_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002091
2092 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2093}
2094
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002095static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2096 size_t len, loff_t *pos)
2097{
2098 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002099 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002100
2101 if (!access_ok(VERIFY_WRITE, buf, len))
2102 return -EFAULT;
2103
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002104 ret = spu_acquire_saved(ctx);
2105 if (ret)
2106 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002107 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002108 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002109 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002110 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002111
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002112 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002113}
2114
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002115static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002116 .open = spufs_info_open,
2117 .read = spufs_ibox_info_read,
2118 .llseek = generic_file_llseek,
2119};
2120
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002121static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2122 char __user *buf, size_t len, loff_t *pos)
2123{
2124 int i, cnt;
2125 u32 data[4];
2126 u32 wbox_stat;
2127
2128 wbox_stat = ctx->csa.prob.mb_stat_R;
2129 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2130 for (i = 0; i < cnt; i++) {
2131 data[i] = ctx->csa.spu_mailbox_data[i];
2132 }
2133
2134 return simple_read_from_buffer(buf, len, pos, &data,
2135 cnt * sizeof(u32));
2136}
2137
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002138static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2139 size_t len, loff_t *pos)
2140{
2141 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002142 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002143
2144 if (!access_ok(VERIFY_WRITE, buf, len))
2145 return -EFAULT;
2146
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002147 ret = spu_acquire_saved(ctx);
2148 if (ret)
2149 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002150 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002151 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002152 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002153 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002154
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002155 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002156}
2157
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002158static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002159 .open = spufs_info_open,
2160 .read = spufs_wbox_info_read,
2161 .llseek = generic_file_llseek,
2162};
2163
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002164static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2165 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002166{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002167 struct spu_dma_info info;
2168 struct mfc_cq_sr *qp, *spuqp;
2169 int i;
2170
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002171 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2172 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2173 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2174 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2175 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2176 for (i = 0; i < 16; i++) {
2177 qp = &info.dma_info_command_data[i];
2178 spuqp = &ctx->csa.priv2.spuq[i];
2179
2180 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2181 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2182 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2183 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2184 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002185
2186 return simple_read_from_buffer(buf, len, pos, &info,
2187 sizeof info);
2188}
2189
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002190static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2191 size_t len, loff_t *pos)
2192{
2193 struct spu_context *ctx = file->private_data;
2194 int ret;
2195
2196 if (!access_ok(VERIFY_WRITE, buf, len))
2197 return -EFAULT;
2198
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002199 ret = spu_acquire_saved(ctx);
2200 if (ret)
2201 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002202 spin_lock(&ctx->csa.register_lock);
2203 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2204 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002205 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002206
2207 return ret;
2208}
2209
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002210static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002211 .open = spufs_info_open,
2212 .read = spufs_dma_info_read,
2213};
2214
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002215static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2216 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002217{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002218 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002219 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002220 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002221 int i;
2222
2223 if (len < ret)
2224 return -EINVAL;
2225
2226 if (!access_ok(VERIFY_WRITE, buf, len))
2227 return -EFAULT;
2228
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002229 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2230 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2231 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2232 for (i = 0; i < 8; i++) {
2233 qp = &info.proxydma_info_command_data[i];
2234 puqp = &ctx->csa.priv2.puq[i];
2235
2236 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2237 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2238 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2239 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2240 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002241
2242 return simple_read_from_buffer(buf, len, pos, &info,
2243 sizeof info);
2244}
2245
2246static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2247 size_t len, loff_t *pos)
2248{
2249 struct spu_context *ctx = file->private_data;
2250 int ret;
2251
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002252 ret = spu_acquire_saved(ctx);
2253 if (ret)
2254 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002255 spin_lock(&ctx->csa.register_lock);
2256 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002257 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002258 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002259
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002260 return ret;
2261}
2262
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002263static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002264 .open = spufs_info_open,
2265 .read = spufs_proxydma_info_read,
2266};
2267
Christoph Hellwig476273a2007-06-29 10:58:01 +10002268static int spufs_show_tid(struct seq_file *s, void *private)
2269{
2270 struct spu_context *ctx = s->private;
2271
2272 seq_printf(s, "%d\n", ctx->tid);
2273 return 0;
2274}
2275
2276static int spufs_tid_open(struct inode *inode, struct file *file)
2277{
2278 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2279}
2280
2281static const struct file_operations spufs_tid_fops = {
2282 .open = spufs_tid_open,
2283 .read = seq_read,
2284 .llseek = seq_lseek,
2285 .release = single_release,
2286};
2287
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002288static const char *ctx_state_names[] = {
2289 "user", "system", "iowait", "loaded"
2290};
2291
2292static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002293 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002294{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002295 struct timespec ts;
2296 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002297
Andre Detsch27ec41d2007-07-20 21:39:33 +02002298 /*
2299 * In general, utilization statistics are updated by the controlling
2300 * thread as the spu context moves through various well defined
2301 * state transitions, but if the context is lazily loaded its
2302 * utilization statistics are not updated as the controlling thread
2303 * is not tightly coupled with the execution of the spu context. We
2304 * calculate and apply the time delta from the last recorded state
2305 * of the spu context.
2306 */
2307 if (ctx->spu && ctx->stats.util_state == state) {
2308 ktime_get_ts(&ts);
2309 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2310 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002311
Andre Detsch27ec41d2007-07-20 21:39:33 +02002312 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002313}
2314
2315static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2316{
2317 unsigned long long slb_flts = ctx->stats.slb_flt;
2318
2319 if (ctx->state == SPU_STATE_RUNNABLE) {
2320 slb_flts += (ctx->spu->stats.slb_flt -
2321 ctx->stats.slb_flt_base);
2322 }
2323
2324 return slb_flts;
2325}
2326
2327static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2328{
2329 unsigned long long class2_intrs = ctx->stats.class2_intr;
2330
2331 if (ctx->state == SPU_STATE_RUNNABLE) {
2332 class2_intrs += (ctx->spu->stats.class2_intr -
2333 ctx->stats.class2_intr_base);
2334 }
2335
2336 return class2_intrs;
2337}
2338
2339
2340static int spufs_show_stat(struct seq_file *s, void *private)
2341{
2342 struct spu_context *ctx = s->private;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002343 int ret;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002344
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002345 ret = spu_acquire(ctx);
2346 if (ret)
2347 return ret;
2348
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002349 seq_printf(s, "%s %llu %llu %llu %llu "
2350 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002351 ctx_state_names[ctx->stats.util_state],
2352 spufs_acct_time(ctx, SPU_UTIL_USER),
2353 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2354 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2355 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002356 ctx->stats.vol_ctx_switch,
2357 ctx->stats.invol_ctx_switch,
2358 spufs_slb_flts(ctx),
2359 ctx->stats.hash_flt,
2360 ctx->stats.min_flt,
2361 ctx->stats.maj_flt,
2362 spufs_class2_intrs(ctx),
2363 ctx->stats.libassist);
2364 spu_release(ctx);
2365 return 0;
2366}
2367
2368static int spufs_stat_open(struct inode *inode, struct file *file)
2369{
2370 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2371}
2372
2373static const struct file_operations spufs_stat_fops = {
2374 .open = spufs_stat_open,
2375 .read = seq_read,
2376 .llseek = seq_lseek,
2377 .release = single_release,
2378};
2379
2380
Arnd Bergmann67207b92005-11-15 15:53:48 -05002381struct tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002382 { "capabilities", &spufs_caps_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002383 { "mem", &spufs_mem_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002384 { "regs", &spufs_regs_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002385 { "mbox", &spufs_mbox_fops, 0444, },
2386 { "ibox", &spufs_ibox_fops, 0444, },
2387 { "wbox", &spufs_wbox_fops, 0222, },
2388 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2389 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2390 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002391 { "signal1", &spufs_signal1_fops, 0666, },
2392 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002393 { "signal1_type", &spufs_signal1_type, 0666, },
2394 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002395 { "cntl", &spufs_cntl_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002396 { "fpcr", &spufs_fpcr_fops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002397 { "lslr", &spufs_lslr_ops, 0444, },
2398 { "mfc", &spufs_mfc_fops, 0666, },
2399 { "mss", &spufs_mss_fops, 0666, },
2400 { "npc", &spufs_npc_ops, 0666, },
2401 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002402 { "decr", &spufs_decr_ops, 0666, },
2403 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002404 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002405 { "event_status", &spufs_event_status_ops, 0444, },
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02002406 { "psmap", &spufs_psmap_fops, 0666, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002407 { "phys-id", &spufs_id_ops, 0666, },
2408 { "object-id", &spufs_object_id_ops, 0666, },
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002409 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2410 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2411 { "wbox_info", &spufs_wbox_info_fops, 0444, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002412 { "dma_info", &spufs_dma_info_fops, 0444, },
2413 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002414 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002415 { "stat", &spufs_stat_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002416 {},
2417};
Mark Nutter5737edd2006-10-24 18:31:16 +02002418
2419struct tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002420 { "capabilities", &spufs_caps_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002421 { "mem", &spufs_mem_fops, 0666, },
2422 { "mbox", &spufs_mbox_fops, 0444, },
2423 { "ibox", &spufs_ibox_fops, 0444, },
2424 { "wbox", &spufs_wbox_fops, 0222, },
2425 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2426 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2427 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002428 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2429 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002430 { "signal1_type", &spufs_signal1_type, 0666, },
2431 { "signal2_type", &spufs_signal2_type, 0666, },
2432 { "mss", &spufs_mss_fops, 0666, },
2433 { "mfc", &spufs_mfc_fops, 0666, },
2434 { "cntl", &spufs_cntl_fops, 0666, },
2435 { "npc", &spufs_npc_ops, 0666, },
2436 { "psmap", &spufs_psmap_fops, 0666, },
2437 { "phys-id", &spufs_id_ops, 0666, },
2438 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002439 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002440 { "stat", &spufs_stat_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002441 {},
2442};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002443
2444struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002445 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2446 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002447 { "lslr", NULL, spufs_lslr_get, 19 },
2448 { "decr", NULL, spufs_decr_get, 19 },
2449 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002450 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2451 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002452 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002453 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002454 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2455 { "event_mask", NULL, spufs_event_mask_get, 19 },
2456 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002457 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2458 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2459 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2460 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2461 { "proxydma_info", __spufs_proxydma_info_read,
2462 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002463 { "object-id", NULL, spufs_object_id_get, 19 },
2464 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002465 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002466};