blob: 1b26071a86ca8f17e1f2c04f22ffb9d60d13e2b8 [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>
FUJITA Tomonoridfe1e092008-05-13 19:07:42 +100035#include <asm/time.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050036#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
Nick Pigginb1e22702008-06-10 09:26:08 +1000241static int
242spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
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;
Nick Pigginb1e22702008-06-10 09:26:08 +1000245 unsigned long address = (unsigned long)vmf->virtual_address;
246 unsigned long pfn, offset;
247
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000248#ifdef CONFIG_SPU_FS_64K_LS
249 struct spu_state *csa = &ctx->csa;
250 int psize;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100251
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000252 /* Check what page size we are using */
253 psize = get_slice_psize(vma->vm_mm, address);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500254
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000255 /* Some sanity checking */
256 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
257
258 /* Wow, 64K, cool, we need to align the address though */
259 if (csa->use_big_pages) {
260 BUG_ON(vma->vm_start & 0xffff);
261 address &= ~0xfffful;
262 }
263#endif /* CONFIG_SPU_FS_64K_LS */
264
Nick Pigginb1e22702008-06-10 09:26:08 +1000265 offset = vmf->pgoff << PAGE_SHIFT;
Masato Noguchi128b8542007-02-13 21:54:30 +0100266 if (offset >= LS_SIZE)
Nick Pigginb1e22702008-06-10 09:26:08 +1000267 return VM_FAULT_SIGBUS;
Masato Noguchi128b8542007-02-13 21:54:30 +0100268
Nick Pigginb1e22702008-06-10 09:26:08 +1000269 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
270 address, offset);
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000271
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900272 if (spu_acquire(ctx))
Nick Pigginb1e22702008-06-10 09:26:08 +1000273 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500274
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200275 if (ctx->state == SPU_STATE_SAVED) {
276 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Arnd Bergmann932f535d2006-11-20 18:45:06 +0100277 & ~_PAGE_NO_CACHE);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100278 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200279 } else {
280 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100281 | _PAGE_NO_CACHE);
282 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200283 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100284 vm_insert_pfn(vma, address, pfn);
285
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500286 spu_release(ctx);
287
Nick Pigginb1e22702008-06-10 09:26:08 +1000288 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500289}
290
Benjamin Herrenschmidta3528942008-07-23 21:27:09 -0700291static int spufs_mem_mmap_access(struct vm_area_struct *vma,
292 unsigned long address,
293 void *buf, int len, int write)
294{
295 struct spu_context *ctx = vma->vm_file->private_data;
296 unsigned long offset = address - vma->vm_start;
297 char *local_store;
298
299 if (write && !(vma->vm_flags & VM_WRITE))
300 return -EACCES;
301 if (spu_acquire(ctx))
302 return -EINTR;
303 if ((offset + len) > vma->vm_end)
304 len = vma->vm_end - offset;
305 local_store = ctx->ops->get_ls(ctx);
306 if (write)
307 memcpy_toio(local_store + offset, buf, len);
308 else
309 memcpy_fromio(buf, local_store + offset, len);
310 spu_release(ctx);
311 return len;
312}
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100313
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500314static struct vm_operations_struct spufs_mem_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000315 .fault = spufs_mem_mmap_fault,
Benjamin Herrenschmidta3528942008-07-23 21:27:09 -0700316 .access = spufs_mem_mmap_access,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500317};
318
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000319static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500320{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000321#ifdef CONFIG_SPU_FS_64K_LS
322 struct spu_context *ctx = file->private_data;
323 struct spu_state *csa = &ctx->csa;
324
325 /* Sanity check VMA alignment */
326 if (csa->use_big_pages) {
327 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
328 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
329 vma->vm_pgoff);
330 if (vma->vm_start & 0xffff)
331 return -EINVAL;
332 if (vma->vm_pgoff & 0xf)
333 return -EINVAL;
334 }
335#endif /* CONFIG_SPU_FS_64K_LS */
336
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500337 if (!(vma->vm_flags & VM_SHARED))
338 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500339
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100340 vma->vm_flags |= VM_IO | VM_PFNMAP;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500341 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
342 | _PAGE_NO_CACHE);
343
344 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500345 return 0;
346}
347
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000348#ifdef CONFIG_SPU_FS_64K_LS
Sebastian Siewior12388192007-09-19 14:38:12 +1000349static unsigned long spufs_get_unmapped_area(struct file *file,
350 unsigned long addr, unsigned long len, unsigned long pgoff,
351 unsigned long flags)
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000352{
353 struct spu_context *ctx = file->private_data;
354 struct spu_state *csa = &ctx->csa;
355
356 /* If not using big pages, fallback to normal MM g_u_a */
357 if (!csa->use_big_pages)
358 return current->mm->get_unmapped_area(file, addr, len,
359 pgoff, flags);
360
361 /* Else, try to obtain a 64K pages slice */
362 return slice_get_unmapped_area(addr, len, flags,
363 MMU_PAGE_64K, 1, 0);
364}
365#endif /* CONFIG_SPU_FS_64K_LS */
366
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800367static const struct file_operations spufs_mem_fops = {
Jeremy Kerr70225432007-06-29 10:58:00 +1000368 .open = spufs_mem_open,
369 .release = spufs_mem_release,
370 .read = spufs_mem_read,
371 .write = spufs_mem_write,
372 .llseek = generic_file_llseek,
373 .mmap = spufs_mem_mmap,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000374#ifdef CONFIG_SPU_FS_64K_LS
375 .get_unmapped_area = spufs_get_unmapped_area,
376#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500377};
378
Nick Pigginb1e22702008-06-10 09:26:08 +1000379static int spufs_ps_fault(struct vm_area_struct *vma,
380 struct vm_fault *vmf,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100381 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200382 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100383{
Mark Nutter6df10a82006-03-23 00:00:12 +0100384 struct spu_context *ctx = vma->vm_file->private_data;
Nick Pigginb1e22702008-06-10 09:26:08 +1000385 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100386 int ret = 0;
Mark Nutter6df10a82006-03-23 00:00:12 +0100387
Nick Pigginb1e22702008-06-10 09:26:08 +1000388 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100389
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200390 if (offset >= ps_size)
Nick Pigginb1e22702008-06-10 09:26:08 +1000391 return VM_FAULT_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100392
Jeremy Kerr60657262008-11-11 10:22:22 +1100393 if (fatal_signal_pending(current))
394 return VM_FAULT_SIGBUS;
395
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900396 /*
Jeremy Kerrd5883132008-02-26 13:31:42 +1100397 * Because we release the mmap_sem, the context may be destroyed while
398 * we're in spu_wait. Grab an extra reference so it isn't destroyed
399 * in the meantime.
400 */
401 get_spu_context(ctx);
402
403 /*
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900404 * We have to wait for context to be loaded before we have
405 * pages to hand out to the user, but we don't want to wait
406 * with the mmap_sem held.
407 * It is possible to drop the mmap_sem here, but then we need
Nick Pigginb1e22702008-06-10 09:26:08 +1000408 * to return VM_FAULT_NOPAGE because the mappings may have
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900409 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100410 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900411 if (spu_acquire(ctx))
Jeremy Kerrd5883132008-02-26 13:31:42 +1100412 goto refault;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900413
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900414 if (ctx->state == SPU_STATE_SAVED) {
415 up_read(&current->mm->mmap_sem);
Nick Pigginb1e22702008-06-10 09:26:08 +1000416 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100417 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Nick Pigginb1e22702008-06-10 09:26:08 +1000418 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900419 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900420 } else {
421 area = ctx->spu->problem_phys + ps_offs;
Nick Pigginb1e22702008-06-10 09:26:08 +1000422 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
423 (area + offset) >> PAGE_SHIFT);
424 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900425 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100426
Christoph Hellwigeebead52008-02-08 15:50:41 +1100427 if (!ret)
428 spu_release(ctx);
Jeremy Kerrd5883132008-02-26 13:31:42 +1100429
430refault:
431 put_spu_context(ctx);
Nick Pigginb1e22702008-06-10 09:26:08 +1000432 return VM_FAULT_NOPAGE;
Mark Nutter6df10a82006-03-23 00:00:12 +0100433}
434
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200435#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +1000436static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
437 struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +0100438{
Jeremy Kerr87ff6092008-07-01 10:22:50 +1000439 return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +0100440}
441
442static struct vm_operations_struct spufs_cntl_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000443 .fault = spufs_cntl_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +0100444};
445
446/*
447 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100448 */
449static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
450{
451 if (!(vma->vm_flags & VM_SHARED))
452 return -EINVAL;
453
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100454 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100455 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200456 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100457
458 vma->vm_ops = &spufs_cntl_mmap_vmops;
459 return 0;
460}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200461#else /* SPUFS_MMAP_4K */
462#define spufs_cntl_mmap NULL
463#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100464
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900465static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200466{
467 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900468 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200469
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900470 ret = spu_acquire(ctx);
471 if (ret)
472 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900473 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200474 spu_release(ctx);
475
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900476 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200477}
478
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900479static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200480{
481 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900482 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200483
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900484 ret = spu_acquire(ctx);
485 if (ret)
486 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200487 ctx->ops->runcntl_write(ctx, val);
488 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900489
490 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200491}
492
Mark Nutter6df10a82006-03-23 00:00:12 +0100493static int spufs_cntl_open(struct inode *inode, struct file *file)
494{
495 struct spufs_inode_info *i = SPUFS_I(inode);
496 struct spu_context *ctx = i->i_ctx;
497
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000498 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100499 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200500 if (!i->i_openers++)
501 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000502 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800503 return simple_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200504 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100505}
506
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200507static int
508spufs_cntl_release(struct inode *inode, struct file *file)
509{
510 struct spufs_inode_info *i = SPUFS_I(inode);
511 struct spu_context *ctx = i->i_ctx;
512
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800513 simple_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200514
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000515 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200516 if (!--i->i_openers)
517 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000518 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200519 return 0;
520}
521
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800522static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100523 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200524 .release = spufs_cntl_release,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800525 .read = simple_attr_read,
526 .write = simple_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100527 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100528};
529
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500530static int
531spufs_regs_open(struct inode *inode, struct file *file)
532{
533 struct spufs_inode_info *i = SPUFS_I(inode);
534 file->private_data = i->i_ctx;
535 return 0;
536}
537
538static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100539__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
540 size_t size, loff_t *pos)
541{
542 struct spu_lscsa *lscsa = ctx->csa.lscsa;
543 return simple_read_from_buffer(buffer, size, pos,
544 lscsa->gprs, sizeof lscsa->gprs);
545}
546
547static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500548spufs_regs_read(struct file *file, char __user *buffer,
549 size_t size, loff_t *pos)
550{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500551 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100552 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500553
Jeremy Kerrf027faa2008-10-16 11:11:12 +1100554 /* pre-check for file position: if we'd return EOF, there's no point
555 * causing a deschedule */
556 if (*pos >= sizeof(ctx->csa.lscsa->gprs))
557 return 0;
558
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900559 ret = spu_acquire_saved(ctx);
560 if (ret)
561 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100562 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200563 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500564 return ret;
565}
566
567static ssize_t
568spufs_regs_write(struct file *file, const char __user *buffer,
569 size_t size, loff_t *pos)
570{
571 struct spu_context *ctx = file->private_data;
572 struct spu_lscsa *lscsa = ctx->csa.lscsa;
573 int ret;
574
575 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
576 if (size <= 0)
577 return -EFBIG;
578 *pos += size;
579
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900580 ret = spu_acquire_saved(ctx);
581 if (ret)
582 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500583
584 ret = copy_from_user(lscsa->gprs + *pos - size,
585 buffer, size) ? -EFAULT : size;
586
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200587 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500588 return ret;
589}
590
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800591static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500592 .open = spufs_regs_open,
593 .read = spufs_regs_read,
594 .write = spufs_regs_write,
595 .llseek = generic_file_llseek,
596};
597
598static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100599__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
600 size_t size, loff_t * pos)
601{
602 struct spu_lscsa *lscsa = ctx->csa.lscsa;
603 return simple_read_from_buffer(buffer, size, pos,
604 &lscsa->fpcr, sizeof(lscsa->fpcr));
605}
606
607static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500608spufs_fpcr_read(struct file *file, char __user * buffer,
609 size_t size, loff_t * pos)
610{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500611 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100612 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500613
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900614 ret = spu_acquire_saved(ctx);
615 if (ret)
616 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100617 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200618 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500619 return ret;
620}
621
622static ssize_t
623spufs_fpcr_write(struct file *file, const char __user * buffer,
624 size_t size, loff_t * pos)
625{
626 struct spu_context *ctx = file->private_data;
627 struct spu_lscsa *lscsa = ctx->csa.lscsa;
628 int ret;
629
630 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
631 if (size <= 0)
632 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900633
634 ret = spu_acquire_saved(ctx);
635 if (ret)
636 return ret;
637
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500638 *pos += size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500639 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
640 buffer, size) ? -EFAULT : size;
641
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200642 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500643 return ret;
644}
645
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800646static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500647 .open = spufs_regs_open,
648 .read = spufs_fpcr_read,
649 .write = spufs_fpcr_write,
650 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500651};
652
653/* generic open function for all pipe-like files */
654static int spufs_pipe_open(struct inode *inode, struct file *file)
655{
656 struct spufs_inode_info *i = SPUFS_I(inode);
657 file->private_data = i->i_ctx;
658
659 return nonseekable_open(inode, file);
660}
661
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200662/*
663 * Read as many bytes from the mailbox as possible, until
664 * one of the conditions becomes true:
665 *
666 * - no more data available in the mailbox
667 * - end of the user provided buffer
668 * - end of the mapped area
669 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500670static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
671 size_t len, loff_t *pos)
672{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500673 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200674 u32 mbox_data, __user *udata;
675 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500676
677 if (len < 4)
678 return -EINVAL;
679
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200680 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500681 return -EFAULT;
682
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200683 udata = (void __user *)buf;
684
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900685 count = spu_acquire(ctx);
686 if (count)
687 return count;
688
Arnd Bergmann274cef52006-10-24 18:01:42 +0200689 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200690 int ret;
691 ret = ctx->ops->mbox_read(ctx, &mbox_data);
692 if (ret == 0)
693 break;
694
695 /*
696 * at the end of the mapped area, we can fault
697 * but still need to return the data we have
698 * read successfully so far.
699 */
700 ret = __put_user(mbox_data, udata);
701 if (ret) {
702 if (!count)
703 count = -EFAULT;
704 break;
705 }
706 }
707 spu_release(ctx);
708
709 if (!count)
710 count = -EAGAIN;
711
712 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500713}
714
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800715static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500716 .open = spufs_pipe_open,
717 .read = spufs_mbox_read,
718};
719
720static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
721 size_t len, loff_t *pos)
722{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500723 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900724 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500725 u32 mbox_stat;
726
727 if (len < 4)
728 return -EINVAL;
729
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900730 ret = spu_acquire(ctx);
731 if (ret)
732 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500733
734 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
735
736 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500737
738 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
739 return -EFAULT;
740
741 return 4;
742}
743
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800744static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500745 .open = spufs_pipe_open,
746 .read = spufs_mbox_stat_read,
747};
748
749/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500750size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500751{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500752 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500753}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500754
755static int spufs_ibox_fasync(int fd, struct file *file, int on)
756{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500757 struct spu_context *ctx = file->private_data;
758
759 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
760}
761
762/* interrupt-level ibox callback function. */
763void spufs_ibox_callback(struct spu *spu)
764{
765 struct spu_context *ctx = spu->ctx;
766
Luke Browninge65c2f62007-12-20 16:39:59 +0900767 if (!ctx)
768 return;
769
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500770 wake_up_all(&ctx->ibox_wq);
771 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500772}
773
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200774/*
775 * Read as many bytes from the interrupt mailbox as possible, until
776 * one of the conditions becomes true:
777 *
778 * - no more data available in the mailbox
779 * - end of the user provided buffer
780 * - end of the mapped area
781 *
782 * If the file is opened without O_NONBLOCK, we wait here until
783 * any data is available, but return when we have been able to
784 * read something.
785 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500786static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
787 size_t len, loff_t *pos)
788{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500789 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200790 u32 ibox_data, __user *udata;
791 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500792
793 if (len < 4)
794 return -EINVAL;
795
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200796 if (!access_ok(VERIFY_WRITE, buf, len))
797 return -EFAULT;
798
799 udata = (void __user *)buf;
800
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900801 count = spu_acquire(ctx);
802 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100803 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500804
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200805 /* wait only for the first element */
806 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500807 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100808 if (!spu_ibox_read(ctx, &ibox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200809 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100810 goto out_unlock;
811 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500812 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200813 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100814 if (count)
815 goto out;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200816 }
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200817
818 /* if we can't write at all, return -EFAULT */
819 count = __put_user(ibox_data, udata);
820 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100821 goto out_unlock;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200822
823 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
824 int ret;
825 ret = ctx->ops->ibox_read(ctx, &ibox_data);
826 if (ret == 0)
827 break;
828 /*
829 * at the end of the mapped area, we can fault
830 * but still need to return the data we have
831 * read successfully so far.
832 */
833 ret = __put_user(ibox_data, udata);
834 if (ret)
835 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500836 }
837
Christoph Hellwigeebead52008-02-08 15:50:41 +1100838out_unlock:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500839 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100840out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200841 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500842}
843
844static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
845{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500846 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500847 unsigned int mask;
848
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500849 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500850
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900851 /*
852 * For now keep this uninterruptible and also ignore the rule
853 * that poll should not sleep. Will be fixed later.
854 */
855 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500856 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
857 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500858
859 return mask;
860}
861
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800862static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500863 .open = spufs_pipe_open,
864 .read = spufs_ibox_read,
865 .poll = spufs_ibox_poll,
866 .fasync = spufs_ibox_fasync,
867};
868
869static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
870 size_t len, loff_t *pos)
871{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500872 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900873 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500874 u32 ibox_stat;
875
876 if (len < 4)
877 return -EINVAL;
878
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900879 ret = spu_acquire(ctx);
880 if (ret)
881 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500882 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
883 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500884
885 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
886 return -EFAULT;
887
888 return 4;
889}
890
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800891static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500892 .open = spufs_pipe_open,
893 .read = spufs_ibox_stat_read,
894};
895
896/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500897size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500898{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500899 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500900}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500901
902static int spufs_wbox_fasync(int fd, struct file *file, int on)
903{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500904 struct spu_context *ctx = file->private_data;
905 int ret;
906
907 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
908
909 return ret;
910}
911
912/* interrupt-level wbox callback function. */
913void spufs_wbox_callback(struct spu *spu)
914{
915 struct spu_context *ctx = spu->ctx;
916
Luke Browninge65c2f62007-12-20 16:39:59 +0900917 if (!ctx)
918 return;
919
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500920 wake_up_all(&ctx->wbox_wq);
921 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500922}
923
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200924/*
925 * Write as many bytes to the interrupt mailbox as possible, until
926 * one of the conditions becomes true:
927 *
928 * - the mailbox is full
929 * - end of the user provided buffer
930 * - end of the mapped area
931 *
932 * If the file is opened without O_NONBLOCK, we wait here until
933 * space is availabyl, but return when we have been able to
934 * write something.
935 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500936static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
937 size_t len, loff_t *pos)
938{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500939 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200940 u32 wbox_data, __user *udata;
941 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500942
943 if (len < 4)
944 return -EINVAL;
945
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200946 udata = (void __user *)buf;
947 if (!access_ok(VERIFY_READ, buf, len))
948 return -EFAULT;
949
950 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500951 return -EFAULT;
952
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900953 count = spu_acquire(ctx);
954 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100955 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500956
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200957 /*
958 * make sure we can at least write one element, by waiting
959 * in case of !O_NONBLOCK
960 */
961 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500962 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100963 if (!spu_wbox_write(ctx, wbox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200964 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100965 goto out_unlock;
966 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500967 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200968 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100969 if (count)
970 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500971 }
972
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500973
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200974 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200975 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
976 int ret;
977 ret = __get_user(wbox_data, udata);
978 if (ret)
979 break;
980
981 ret = spu_wbox_write(ctx, wbox_data);
982 if (ret == 0)
983 break;
984 }
985
Christoph Hellwigeebead52008-02-08 15:50:41 +1100986out_unlock:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200987 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100988out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200989 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500990}
991
992static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
993{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500994 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500995 unsigned int mask;
996
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500997 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500998
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900999 /*
1000 * For now keep this uninterruptible and also ignore the rule
1001 * that poll should not sleep. Will be fixed later.
1002 */
1003 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -05001004 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
1005 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001006
1007 return mask;
1008}
1009
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001010static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001011 .open = spufs_pipe_open,
1012 .write = spufs_wbox_write,
1013 .poll = spufs_wbox_poll,
1014 .fasync = spufs_wbox_fasync,
1015};
1016
1017static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
1018 size_t len, loff_t *pos)
1019{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001020 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001021 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001022 u32 wbox_stat;
1023
1024 if (len < 4)
1025 return -EINVAL;
1026
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001027 ret = spu_acquire(ctx);
1028 if (ret)
1029 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001030 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
1031 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001032
1033 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
1034 return -EFAULT;
1035
1036 return 4;
1037}
1038
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001039static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001040 .open = spufs_pipe_open,
1041 .read = spufs_wbox_stat_read,
1042};
1043
Mark Nutter6df10a82006-03-23 00:00:12 +01001044static int spufs_signal1_open(struct inode *inode, struct file *file)
1045{
1046 struct spufs_inode_info *i = SPUFS_I(inode);
1047 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001048
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001049 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001050 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001051 if (!i->i_openers++)
1052 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001053 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001054 return nonseekable_open(inode, file);
1055}
1056
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001057static int
1058spufs_signal1_release(struct inode *inode, struct file *file)
1059{
1060 struct spufs_inode_info *i = SPUFS_I(inode);
1061 struct spu_context *ctx = i->i_ctx;
1062
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001063 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001064 if (!--i->i_openers)
1065 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001066 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001067 return 0;
1068}
1069
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001070static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001071 size_t len, loff_t *pos)
1072{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001073 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001074 u32 data;
1075
Arnd Bergmann67207b92005-11-15 15:53:48 -05001076 if (len < 4)
1077 return -EINVAL;
1078
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001079 if (ctx->csa.spu_chnlcnt_RW[3]) {
1080 data = ctx->csa.spu_chnldata_RW[3];
1081 ret = 4;
1082 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001083
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001084 if (!ret)
1085 goto out;
1086
Arnd Bergmann67207b92005-11-15 15:53:48 -05001087 if (copy_to_user(buf, &data, 4))
1088 return -EFAULT;
1089
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001090out:
1091 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001092}
1093
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001094static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1095 size_t len, loff_t *pos)
1096{
1097 int ret;
1098 struct spu_context *ctx = file->private_data;
1099
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001100 ret = spu_acquire_saved(ctx);
1101 if (ret)
1102 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001103 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001104 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001105
1106 return ret;
1107}
1108
Arnd Bergmann67207b92005-11-15 15:53:48 -05001109static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1110 size_t len, loff_t *pos)
1111{
1112 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001113 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001114 u32 data;
1115
1116 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001117
1118 if (len < 4)
1119 return -EINVAL;
1120
1121 if (copy_from_user(&data, buf, 4))
1122 return -EFAULT;
1123
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001124 ret = spu_acquire(ctx);
1125 if (ret)
1126 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001127 ctx->ops->signal1_write(ctx, data);
1128 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001129
1130 return 4;
1131}
1132
Nick Pigginb1e22702008-06-10 09:26:08 +10001133static int
1134spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001135{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001136#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1137 return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1138#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001139 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1140 * signal 1 and 2 area
1141 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001142 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001143#else
1144#error unsupported page size
1145#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001146}
1147
1148static struct vm_operations_struct spufs_signal1_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001149 .fault = spufs_signal1_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001150};
1151
1152static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1153{
1154 if (!(vma->vm_flags & VM_SHARED))
1155 return -EINVAL;
1156
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001157 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001158 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001159 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001160
1161 vma->vm_ops = &spufs_signal1_mmap_vmops;
1162 return 0;
1163}
Mark Nutter6df10a82006-03-23 00:00:12 +01001164
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001165static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001166 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001167 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001168 .read = spufs_signal1_read,
1169 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001170 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001171};
1172
Jeremy Kerrd054b362007-07-20 21:39:31 +02001173static const struct file_operations spufs_signal1_nosched_fops = {
1174 .open = spufs_signal1_open,
1175 .release = spufs_signal1_release,
1176 .write = spufs_signal1_write,
1177 .mmap = spufs_signal1_mmap,
1178};
1179
Mark Nutter6df10a82006-03-23 00:00:12 +01001180static int spufs_signal2_open(struct inode *inode, struct file *file)
1181{
1182 struct spufs_inode_info *i = SPUFS_I(inode);
1183 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001184
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001185 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001186 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001187 if (!i->i_openers++)
1188 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001189 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001190 return nonseekable_open(inode, file);
1191}
1192
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001193static int
1194spufs_signal2_release(struct inode *inode, struct file *file)
1195{
1196 struct spufs_inode_info *i = SPUFS_I(inode);
1197 struct spu_context *ctx = i->i_ctx;
1198
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001199 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001200 if (!--i->i_openers)
1201 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001202 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001203 return 0;
1204}
1205
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001206static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001207 size_t len, loff_t *pos)
1208{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001209 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001210 u32 data;
1211
Arnd Bergmann67207b92005-11-15 15:53:48 -05001212 if (len < 4)
1213 return -EINVAL;
1214
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001215 if (ctx->csa.spu_chnlcnt_RW[4]) {
1216 data = ctx->csa.spu_chnldata_RW[4];
1217 ret = 4;
1218 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001219
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001220 if (!ret)
1221 goto out;
1222
Arnd Bergmann67207b92005-11-15 15:53:48 -05001223 if (copy_to_user(buf, &data, 4))
1224 return -EFAULT;
1225
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001226out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001227 return ret;
1228}
1229
1230static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1231 size_t len, loff_t *pos)
1232{
1233 struct spu_context *ctx = file->private_data;
1234 int ret;
1235
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001236 ret = spu_acquire_saved(ctx);
1237 if (ret)
1238 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001239 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001240 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001241
1242 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001243}
1244
1245static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1246 size_t len, loff_t *pos)
1247{
1248 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001249 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001250 u32 data;
1251
1252 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001253
1254 if (len < 4)
1255 return -EINVAL;
1256
1257 if (copy_from_user(&data, buf, 4))
1258 return -EFAULT;
1259
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001260 ret = spu_acquire(ctx);
1261 if (ret)
1262 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001263 ctx->ops->signal2_write(ctx, data);
1264 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001265
1266 return 4;
1267}
1268
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001269#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001270static int
1271spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001272{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001273#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1274 return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1275#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001276 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1277 * signal 1 and 2 area
1278 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001279 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001280#else
1281#error unsupported page size
1282#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001283}
1284
1285static struct vm_operations_struct spufs_signal2_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001286 .fault = spufs_signal2_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001287};
1288
1289static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1290{
1291 if (!(vma->vm_flags & VM_SHARED))
1292 return -EINVAL;
1293
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001294 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001295 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001296 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001297
1298 vma->vm_ops = &spufs_signal2_mmap_vmops;
1299 return 0;
1300}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001301#else /* SPUFS_MMAP_4K */
1302#define spufs_signal2_mmap NULL
1303#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001304
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001305static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001306 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001307 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001308 .read = spufs_signal2_read,
1309 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001310 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001311};
1312
Jeremy Kerrd054b362007-07-20 21:39:31 +02001313static const struct file_operations spufs_signal2_nosched_fops = {
1314 .open = spufs_signal2_open,
1315 .release = spufs_signal2_release,
1316 .write = spufs_signal2_write,
1317 .mmap = spufs_signal2_mmap,
1318};
1319
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001320/*
1321 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1322 * work of acquiring (or not) the SPU context before calling through
1323 * to the actual get routine. The set routine is called directly.
1324 */
1325#define SPU_ATTR_NOACQUIRE 0
1326#define SPU_ATTR_ACQUIRE 1
1327#define SPU_ATTR_ACQUIRE_SAVED 2
1328
1329#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001330static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001331{ \
1332 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001333 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001334 \
1335 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001336 ret = spu_acquire(ctx); \
1337 if (ret) \
1338 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001339 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001340 spu_release(ctx); \
1341 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001342 ret = spu_acquire_saved(ctx); \
1343 if (ret) \
1344 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001345 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001346 spu_release_saved(ctx); \
1347 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001348 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001349 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001350 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001351} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001352DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001353
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001354static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001355{
1356 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001357 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001358
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001359 ret = spu_acquire(ctx);
1360 if (ret)
1361 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001362 ctx->ops->signal1_type_set(ctx, val);
1363 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001364
1365 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001366}
1367
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001368static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001369{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001370 return ctx->ops->signal1_type_get(ctx);
1371}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001372DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001373 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001374
Arnd Bergmann67207b92005-11-15 15:53:48 -05001375
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001376static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001377{
1378 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001379 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001380
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001381 ret = spu_acquire(ctx);
1382 if (ret)
1383 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001384 ctx->ops->signal2_type_set(ctx, val);
1385 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001386
1387 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001388}
1389
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001390static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001391{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001392 return ctx->ops->signal2_type_get(ctx);
1393}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001394DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001395 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001396
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001397#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001398static int
1399spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001400{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001401 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001402}
1403
1404static struct vm_operations_struct spufs_mss_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001405 .fault = spufs_mss_mmap_fault,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001406};
1407
1408/*
1409 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001410 */
1411static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1412{
1413 if (!(vma->vm_flags & VM_SHARED))
1414 return -EINVAL;
1415
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001416 vma->vm_flags |= VM_IO | VM_PFNMAP;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001417 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001418 | _PAGE_NO_CACHE | _PAGE_GUARDED);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001419
1420 vma->vm_ops = &spufs_mss_mmap_vmops;
1421 return 0;
1422}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001423#else /* SPUFS_MMAP_4K */
1424#define spufs_mss_mmap NULL
1425#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001426
1427static int spufs_mss_open(struct inode *inode, struct file *file)
1428{
1429 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001430 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001431
1432 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001433
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001434 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001435 if (!i->i_openers++)
1436 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001437 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001438 return nonseekable_open(inode, file);
1439}
1440
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001441static int
1442spufs_mss_release(struct inode *inode, struct file *file)
1443{
1444 struct spufs_inode_info *i = SPUFS_I(inode);
1445 struct spu_context *ctx = i->i_ctx;
1446
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001447 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001448 if (!--i->i_openers)
1449 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001450 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001451 return 0;
1452}
1453
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001454static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001455 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001456 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001457 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001458};
1459
Nick Pigginb1e22702008-06-10 09:26:08 +10001460static int
1461spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001462{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001463 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001464}
1465
1466static struct vm_operations_struct spufs_psmap_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001467 .fault = spufs_psmap_mmap_fault,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001468};
1469
1470/*
1471 * mmap support for full problem state area [0x00000 - 0x1ffff].
1472 */
1473static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1474{
1475 if (!(vma->vm_flags & VM_SHARED))
1476 return -EINVAL;
1477
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001478 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001479 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1480 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1481
1482 vma->vm_ops = &spufs_psmap_mmap_vmops;
1483 return 0;
1484}
1485
1486static int spufs_psmap_open(struct inode *inode, struct file *file)
1487{
1488 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001489 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001490
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001491 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001492 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001493 if (!i->i_openers++)
1494 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001495 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001496 return nonseekable_open(inode, file);
1497}
1498
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001499static int
1500spufs_psmap_release(struct inode *inode, struct file *file)
1501{
1502 struct spufs_inode_info *i = SPUFS_I(inode);
1503 struct spu_context *ctx = i->i_ctx;
1504
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001505 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001506 if (!--i->i_openers)
1507 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001508 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001509 return 0;
1510}
1511
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001512static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001513 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001514 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001515 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001516};
1517
1518
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001519#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001520static int
1521spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001522{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001523 return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +01001524}
1525
1526static struct vm_operations_struct spufs_mfc_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001527 .fault = spufs_mfc_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001528};
1529
1530/*
1531 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001532 */
1533static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1534{
1535 if (!(vma->vm_flags & VM_SHARED))
1536 return -EINVAL;
1537
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001538 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001539 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001540 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001541
1542 vma->vm_ops = &spufs_mfc_mmap_vmops;
1543 return 0;
1544}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001545#else /* SPUFS_MMAP_4K */
1546#define spufs_mfc_mmap NULL
1547#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001548
1549static int spufs_mfc_open(struct inode *inode, struct file *file)
1550{
1551 struct spufs_inode_info *i = SPUFS_I(inode);
1552 struct spu_context *ctx = i->i_ctx;
1553
1554 /* we don't want to deal with DMA into other processes */
1555 if (ctx->owner != current->mm)
1556 return -EINVAL;
1557
1558 if (atomic_read(&inode->i_count) != 1)
1559 return -EBUSY;
1560
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001561 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001562 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001563 if (!i->i_openers++)
1564 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001565 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001566 return nonseekable_open(inode, file);
1567}
1568
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001569static int
1570spufs_mfc_release(struct inode *inode, struct file *file)
1571{
1572 struct spufs_inode_info *i = SPUFS_I(inode);
1573 struct spu_context *ctx = i->i_ctx;
1574
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001575 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001576 if (!--i->i_openers)
1577 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001578 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001579 return 0;
1580}
1581
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001582/* interrupt-level mfc callback function. */
1583void spufs_mfc_callback(struct spu *spu)
1584{
1585 struct spu_context *ctx = spu->ctx;
1586
Luke Browninge65c2f62007-12-20 16:39:59 +09001587 if (!ctx)
1588 return;
1589
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001590 wake_up_all(&ctx->mfc_wq);
1591
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001592 pr_debug("%s %s\n", __func__, spu->name);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001593 if (ctx->mfc_fasync) {
1594 u32 free_elements, tagstatus;
1595 unsigned int mask;
1596
1597 /* no need for spu_acquire in interrupt context */
1598 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1599 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1600
1601 mask = 0;
1602 if (free_elements & 0xffff)
1603 mask |= POLLOUT;
1604 if (tagstatus & ctx->tagwait)
1605 mask |= POLLIN;
1606
1607 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1608 }
1609}
1610
1611static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1612{
1613 /* See if there is one tag group is complete */
1614 /* FIXME we need locking around tagwait */
1615 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1616 ctx->tagwait &= ~*status;
1617 if (*status)
1618 return 1;
1619
1620 /* enable interrupt waiting for any tag group,
1621 may silently fail if interrupts are already enabled */
1622 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1623 return 0;
1624}
1625
1626static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1627 size_t size, loff_t *pos)
1628{
1629 struct spu_context *ctx = file->private_data;
1630 int ret = -EINVAL;
1631 u32 status;
1632
1633 if (size != 4)
1634 goto out;
1635
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001636 ret = spu_acquire(ctx);
1637 if (ret)
1638 return ret;
1639
1640 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001641 if (file->f_flags & O_NONBLOCK) {
1642 status = ctx->ops->read_mfc_tagstatus(ctx);
1643 if (!(status & ctx->tagwait))
1644 ret = -EAGAIN;
1645 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001646 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001647 ctx->tagwait &= ~status;
1648 } else {
1649 ret = spufs_wait(ctx->mfc_wq,
1650 spufs_read_mfc_tagstatus(ctx, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001651 if (ret)
1652 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001653 }
1654 spu_release(ctx);
1655
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001656 ret = 4;
1657 if (copy_to_user(buffer, &status, 4))
1658 ret = -EFAULT;
1659
1660out:
1661 return ret;
1662}
1663
1664static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1665{
1666 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1667 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1668
1669 switch (cmd->cmd) {
1670 case MFC_PUT_CMD:
1671 case MFC_PUTF_CMD:
1672 case MFC_PUTB_CMD:
1673 case MFC_GET_CMD:
1674 case MFC_GETF_CMD:
1675 case MFC_GETB_CMD:
1676 break;
1677 default:
1678 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1679 return -EIO;
1680 }
1681
1682 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1683 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1684 cmd->ea, cmd->lsa);
1685 return -EIO;
1686 }
1687
1688 switch (cmd->size & 0xf) {
1689 case 1:
1690 break;
1691 case 2:
1692 if (cmd->lsa & 1)
1693 goto error;
1694 break;
1695 case 4:
1696 if (cmd->lsa & 3)
1697 goto error;
1698 break;
1699 case 8:
1700 if (cmd->lsa & 7)
1701 goto error;
1702 break;
1703 case 0:
1704 if (cmd->lsa & 15)
1705 goto error;
1706 break;
1707 error:
1708 default:
1709 pr_debug("invalid DMA alignment %x for size %x\n",
1710 cmd->lsa & 0xf, cmd->size);
1711 return -EIO;
1712 }
1713
1714 if (cmd->size > 16 * 1024) {
1715 pr_debug("invalid DMA size %x\n", cmd->size);
1716 return -EIO;
1717 }
1718
1719 if (cmd->tag & 0xfff0) {
1720 /* we reserve the higher tag numbers for kernel use */
1721 pr_debug("invalid DMA tag\n");
1722 return -EIO;
1723 }
1724
1725 if (cmd->class) {
1726 /* not supported in this version */
1727 pr_debug("invalid DMA class\n");
1728 return -EIO;
1729 }
1730
1731 return 0;
1732}
1733
1734static int spu_send_mfc_command(struct spu_context *ctx,
1735 struct mfc_dma_command cmd,
1736 int *error)
1737{
1738 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1739 if (*error == -EAGAIN) {
1740 /* wait for any tag group to complete
1741 so we have space for the new command */
1742 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1743 /* try again, because the queue might be
1744 empty again */
1745 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1746 if (*error == -EAGAIN)
1747 return 0;
1748 }
1749 return 1;
1750}
1751
1752static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1753 size_t size, loff_t *pos)
1754{
1755 struct spu_context *ctx = file->private_data;
1756 struct mfc_dma_command cmd;
1757 int ret = -EINVAL;
1758
1759 if (size != sizeof cmd)
1760 goto out;
1761
1762 ret = -EFAULT;
1763 if (copy_from_user(&cmd, buffer, sizeof cmd))
1764 goto out;
1765
1766 ret = spufs_check_valid_dma(&cmd);
1767 if (ret)
1768 goto out;
1769
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001770 ret = spu_acquire(ctx);
1771 if (ret)
1772 goto out;
1773
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001774 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001775 if (ret)
1776 goto out;
1777
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001778 if (file->f_flags & O_NONBLOCK) {
1779 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1780 } else {
1781 int status;
1782 ret = spufs_wait(ctx->mfc_wq,
1783 spu_send_mfc_command(ctx, cmd, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001784 if (ret)
1785 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001786 if (status)
1787 ret = status;
1788 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001789
1790 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001791 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001792
1793 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001794 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001795
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001796out_unlock:
1797 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001798out:
1799 return ret;
1800}
1801
1802static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1803{
1804 struct spu_context *ctx = file->private_data;
1805 u32 free_elements, tagstatus;
1806 unsigned int mask;
1807
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001808 poll_wait(file, &ctx->mfc_wq, wait);
1809
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001810 /*
1811 * For now keep this uninterruptible and also ignore the rule
1812 * that poll should not sleep. Will be fixed later.
1813 */
1814 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001815 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1816 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1817 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1818 spu_release(ctx);
1819
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001820 mask = 0;
1821 if (free_elements & 0xffff)
1822 mask |= POLLOUT | POLLWRNORM;
1823 if (tagstatus & ctx->tagwait)
1824 mask |= POLLIN | POLLRDNORM;
1825
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001826 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001827 free_elements, tagstatus, ctx->tagwait);
1828
1829 return mask;
1830}
1831
Al Viro73b6af82006-06-25 16:42:33 -07001832static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001833{
1834 struct spu_context *ctx = file->private_data;
1835 int ret;
1836
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001837 ret = spu_acquire(ctx);
1838 if (ret)
Christoph Hellwigeebead52008-02-08 15:50:41 +11001839 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001840#if 0
1841/* this currently hangs */
1842 ret = spufs_wait(ctx->mfc_wq,
1843 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1844 if (ret)
1845 goto out;
1846 ret = spufs_wait(ctx->mfc_wq,
1847 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001848 if (ret)
1849 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001850#else
1851 ret = 0;
1852#endif
1853 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001854out:
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001855 return ret;
1856}
1857
1858static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1859 int datasync)
1860{
Al Viro73b6af82006-06-25 16:42:33 -07001861 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001862}
1863
1864static int spufs_mfc_fasync(int fd, struct file *file, int on)
1865{
1866 struct spu_context *ctx = file->private_data;
1867
1868 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1869}
1870
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001871static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001872 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001873 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001874 .read = spufs_mfc_read,
1875 .write = spufs_mfc_write,
1876 .poll = spufs_mfc_poll,
1877 .flush = spufs_mfc_flush,
1878 .fsync = spufs_mfc_fsync,
1879 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001880 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001881};
1882
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001883static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001884{
1885 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001886 int ret;
1887
1888 ret = spu_acquire(ctx);
1889 if (ret)
1890 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001891 ctx->ops->npc_write(ctx, val);
1892 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001893
1894 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001895}
1896
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001897static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001898{
1899 return ctx->ops->npc_read(ctx);
1900}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001901DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1902 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001903
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001904static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001905{
1906 struct spu_context *ctx = data;
1907 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001908 int ret;
1909
1910 ret = spu_acquire_saved(ctx);
1911 if (ret)
1912 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001913 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001914 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001915
1916 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001917}
1918
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001919static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001920{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001921 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001922 return lscsa->decr.slot[0];
1923}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001924DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1925 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001926
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001927static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001928{
1929 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001930 int ret;
1931
1932 ret = spu_acquire_saved(ctx);
1933 if (ret)
1934 return ret;
Masato Noguchid40a01d2007-07-20 21:39:38 +02001935 if (val)
1936 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1937 else
1938 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001939 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001940
1941 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001942}
1943
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001944static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001945{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001946 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1947 return SPU_DECR_STATUS_RUNNING;
1948 else
1949 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001950}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001951DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1952 spufs_decr_status_set, "0x%llx\n",
1953 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001954
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001955static int spufs_event_mask_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001956{
1957 struct spu_context *ctx = data;
1958 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001959 int ret;
1960
1961 ret = spu_acquire_saved(ctx);
1962 if (ret)
1963 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001964 lscsa->event_mask.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001965 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001966
1967 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001968}
1969
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001970static u64 spufs_event_mask_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001971{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001972 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001973 return lscsa->event_mask.slot[0];
1974}
1975
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001976DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1977 spufs_event_mask_set, "0x%llx\n",
1978 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001979
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001980static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001981{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001982 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001983 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001984 stat = state->spu_chnlcnt_RW[0];
1985 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001986 return state->spu_chnldata_RW[0];
1987 return 0;
1988}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001989DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1990 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001991
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001992static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001993{
1994 struct spu_context *ctx = data;
1995 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001996 int ret;
1997
1998 ret = spu_acquire_saved(ctx);
1999 if (ret)
2000 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002001 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002002 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002003
2004 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002005}
2006
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002007static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002008{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002009 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002010 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002011}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002012DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
2013 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002014
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002015static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002016{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002017 u64 num;
2018
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002019 if (ctx->state == SPU_STATE_RUNNABLE)
2020 num = ctx->spu->number;
2021 else
2022 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002023
2024 return num;
2025}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002026DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
2027 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002028
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002029static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002030{
2031 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002032 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002033}
2034
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002035static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02002036{
2037 struct spu_context *ctx = data;
2038 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002039
2040 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02002041}
2042
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002043DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2044 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02002045
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002046static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002047{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002048 return ctx->csa.priv2.spu_lslr_RW;
2049}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002050DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2051 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002052
2053static int spufs_info_open(struct inode *inode, struct file *file)
2054{
2055 struct spufs_inode_info *i = SPUFS_I(inode);
2056 struct spu_context *ctx = i->i_ctx;
2057 file->private_data = ctx;
2058 return 0;
2059}
2060
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002061static int spufs_caps_show(struct seq_file *s, void *private)
2062{
2063 struct spu_context *ctx = s->private;
2064
2065 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2066 seq_puts(s, "sched\n");
2067 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2068 seq_puts(s, "step\n");
2069 return 0;
2070}
2071
2072static int spufs_caps_open(struct inode *inode, struct file *file)
2073{
2074 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2075}
2076
2077static const struct file_operations spufs_caps_fops = {
2078 .open = spufs_caps_open,
2079 .read = seq_read,
2080 .llseek = seq_lseek,
2081 .release = single_release,
2082};
2083
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002084static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2085 char __user *buf, size_t len, loff_t *pos)
2086{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002087 u32 data;
2088
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002089 /* EOF if there's no entry in the mbox */
2090 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2091 return 0;
2092
2093 data = ctx->csa.prob.pu_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002094
2095 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2096}
2097
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002098static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2099 size_t len, loff_t *pos)
2100{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002101 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002102 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002103
2104 if (!access_ok(VERIFY_WRITE, buf, len))
2105 return -EFAULT;
2106
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002107 ret = spu_acquire_saved(ctx);
2108 if (ret)
2109 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002110 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002111 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002112 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002113 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002114
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002115 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002116}
2117
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002118static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002119 .open = spufs_info_open,
2120 .read = spufs_mbox_info_read,
2121 .llseek = generic_file_llseek,
2122};
2123
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002124static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2125 char __user *buf, size_t len, loff_t *pos)
2126{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002127 u32 data;
2128
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002129 /* EOF if there's no entry in the ibox */
2130 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2131 return 0;
2132
2133 data = ctx->csa.priv2.puint_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002134
2135 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2136}
2137
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002138static ssize_t spufs_ibox_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_ibox_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_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002159 .open = spufs_info_open,
2160 .read = spufs_ibox_info_read,
2161 .llseek = generic_file_llseek,
2162};
2163
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002164static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2165 char __user *buf, size_t len, loff_t *pos)
2166{
2167 int i, cnt;
2168 u32 data[4];
2169 u32 wbox_stat;
2170
2171 wbox_stat = ctx->csa.prob.mb_stat_R;
2172 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2173 for (i = 0; i < cnt; i++) {
2174 data[i] = ctx->csa.spu_mailbox_data[i];
2175 }
2176
2177 return simple_read_from_buffer(buf, len, pos, &data,
2178 cnt * sizeof(u32));
2179}
2180
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002181static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2182 size_t len, loff_t *pos)
2183{
2184 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002185 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002186
2187 if (!access_ok(VERIFY_WRITE, buf, len))
2188 return -EFAULT;
2189
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002190 ret = spu_acquire_saved(ctx);
2191 if (ret)
2192 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002193 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002194 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002195 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002196 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002197
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002198 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002199}
2200
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002201static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002202 .open = spufs_info_open,
2203 .read = spufs_wbox_info_read,
2204 .llseek = generic_file_llseek,
2205};
2206
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002207static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2208 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002209{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002210 struct spu_dma_info info;
2211 struct mfc_cq_sr *qp, *spuqp;
2212 int i;
2213
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002214 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2215 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2216 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2217 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2218 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2219 for (i = 0; i < 16; i++) {
2220 qp = &info.dma_info_command_data[i];
2221 spuqp = &ctx->csa.priv2.spuq[i];
2222
2223 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2224 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2225 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2226 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2227 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002228
2229 return simple_read_from_buffer(buf, len, pos, &info,
2230 sizeof info);
2231}
2232
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002233static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2234 size_t len, loff_t *pos)
2235{
2236 struct spu_context *ctx = file->private_data;
2237 int ret;
2238
2239 if (!access_ok(VERIFY_WRITE, buf, len))
2240 return -EFAULT;
2241
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002242 ret = spu_acquire_saved(ctx);
2243 if (ret)
2244 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002245 spin_lock(&ctx->csa.register_lock);
2246 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2247 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002248 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002249
2250 return ret;
2251}
2252
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002253static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002254 .open = spufs_info_open,
2255 .read = spufs_dma_info_read,
2256};
2257
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002258static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2259 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002260{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002261 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002262 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002263 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002264 int i;
2265
2266 if (len < ret)
2267 return -EINVAL;
2268
2269 if (!access_ok(VERIFY_WRITE, buf, len))
2270 return -EFAULT;
2271
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002272 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2273 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2274 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2275 for (i = 0; i < 8; i++) {
2276 qp = &info.proxydma_info_command_data[i];
2277 puqp = &ctx->csa.priv2.puq[i];
2278
2279 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2280 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2281 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2282 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2283 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002284
2285 return simple_read_from_buffer(buf, len, pos, &info,
2286 sizeof info);
2287}
2288
2289static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2290 size_t len, loff_t *pos)
2291{
2292 struct spu_context *ctx = file->private_data;
2293 int ret;
2294
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002295 ret = spu_acquire_saved(ctx);
2296 if (ret)
2297 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002298 spin_lock(&ctx->csa.register_lock);
2299 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002300 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002301 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002302
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002303 return ret;
2304}
2305
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002306static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002307 .open = spufs_info_open,
2308 .read = spufs_proxydma_info_read,
2309};
2310
Christoph Hellwig476273a2007-06-29 10:58:01 +10002311static int spufs_show_tid(struct seq_file *s, void *private)
2312{
2313 struct spu_context *ctx = s->private;
2314
2315 seq_printf(s, "%d\n", ctx->tid);
2316 return 0;
2317}
2318
2319static int spufs_tid_open(struct inode *inode, struct file *file)
2320{
2321 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2322}
2323
2324static const struct file_operations spufs_tid_fops = {
2325 .open = spufs_tid_open,
2326 .read = seq_read,
2327 .llseek = seq_lseek,
2328 .release = single_release,
2329};
2330
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002331static const char *ctx_state_names[] = {
2332 "user", "system", "iowait", "loaded"
2333};
2334
2335static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002336 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002337{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002338 struct timespec ts;
2339 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002340
Andre Detsch27ec41d2007-07-20 21:39:33 +02002341 /*
2342 * In general, utilization statistics are updated by the controlling
2343 * thread as the spu context moves through various well defined
2344 * state transitions, but if the context is lazily loaded its
2345 * utilization statistics are not updated as the controlling thread
2346 * is not tightly coupled with the execution of the spu context. We
2347 * calculate and apply the time delta from the last recorded state
2348 * of the spu context.
2349 */
2350 if (ctx->spu && ctx->stats.util_state == state) {
2351 ktime_get_ts(&ts);
2352 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2353 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002354
Andre Detsch27ec41d2007-07-20 21:39:33 +02002355 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002356}
2357
2358static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2359{
2360 unsigned long long slb_flts = ctx->stats.slb_flt;
2361
2362 if (ctx->state == SPU_STATE_RUNNABLE) {
2363 slb_flts += (ctx->spu->stats.slb_flt -
2364 ctx->stats.slb_flt_base);
2365 }
2366
2367 return slb_flts;
2368}
2369
2370static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2371{
2372 unsigned long long class2_intrs = ctx->stats.class2_intr;
2373
2374 if (ctx->state == SPU_STATE_RUNNABLE) {
2375 class2_intrs += (ctx->spu->stats.class2_intr -
2376 ctx->stats.class2_intr_base);
2377 }
2378
2379 return class2_intrs;
2380}
2381
2382
2383static int spufs_show_stat(struct seq_file *s, void *private)
2384{
2385 struct spu_context *ctx = s->private;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002386 int ret;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002387
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002388 ret = spu_acquire(ctx);
2389 if (ret)
2390 return ret;
2391
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002392 seq_printf(s, "%s %llu %llu %llu %llu "
2393 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002394 ctx_state_names[ctx->stats.util_state],
2395 spufs_acct_time(ctx, SPU_UTIL_USER),
2396 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2397 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2398 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002399 ctx->stats.vol_ctx_switch,
2400 ctx->stats.invol_ctx_switch,
2401 spufs_slb_flts(ctx),
2402 ctx->stats.hash_flt,
2403 ctx->stats.min_flt,
2404 ctx->stats.maj_flt,
2405 spufs_class2_intrs(ctx),
2406 ctx->stats.libassist);
2407 spu_release(ctx);
2408 return 0;
2409}
2410
2411static int spufs_stat_open(struct inode *inode, struct file *file)
2412{
2413 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2414}
2415
2416static const struct file_operations spufs_stat_fops = {
2417 .open = spufs_stat_open,
2418 .read = seq_read,
2419 .llseek = seq_lseek,
2420 .release = single_release,
2421};
2422
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002423static inline int spufs_switch_log_used(struct spu_context *ctx)
2424{
2425 return (ctx->switch_log->head - ctx->switch_log->tail) %
2426 SWITCH_LOG_BUFSIZE;
2427}
2428
2429static inline int spufs_switch_log_avail(struct spu_context *ctx)
2430{
2431 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2432}
2433
2434static int spufs_switch_log_open(struct inode *inode, struct file *file)
2435{
2436 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002437 int rc;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002438
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002439 rc = spu_acquire(ctx);
2440 if (rc)
2441 return rc;
2442
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002443 if (ctx->switch_log) {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002444 rc = -EBUSY;
2445 goto out;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002446 }
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002447
Jeremy Kerr837ef882008-10-17 12:02:31 +11002448 ctx->switch_log = kmalloc(sizeof(struct switch_log) +
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002449 SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
2450 GFP_KERNEL);
2451
2452 if (!ctx->switch_log) {
2453 rc = -ENOMEM;
2454 goto out;
2455 }
2456
Jeremy Kerr837ef882008-10-17 12:02:31 +11002457 ctx->switch_log->head = ctx->switch_log->tail = 0;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002458 init_waitqueue_head(&ctx->switch_log->wait);
2459 rc = 0;
2460
2461out:
2462 spu_release(ctx);
2463 return rc;
2464}
2465
2466static int spufs_switch_log_release(struct inode *inode, struct file *file)
2467{
2468 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2469 int rc;
2470
2471 rc = spu_acquire(ctx);
2472 if (rc)
2473 return rc;
2474
2475 kfree(ctx->switch_log);
2476 ctx->switch_log = NULL;
2477 spu_release(ctx);
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002478
2479 return 0;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002480}
2481
2482static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2483{
2484 struct switch_log_entry *p;
2485
2486 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2487
2488 return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
2489 (unsigned int) p->tstamp.tv_sec,
2490 (unsigned int) p->tstamp.tv_nsec,
2491 p->spu_id,
2492 (unsigned int) p->type,
2493 (unsigned int) p->val,
2494 (unsigned long long) p->timebase);
2495}
2496
2497static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2498 size_t len, loff_t *ppos)
2499{
2500 struct inode *inode = file->f_path.dentry->d_inode;
2501 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2502 int error = 0, cnt = 0;
2503
2504 if (!buf || len < 0)
2505 return -EINVAL;
2506
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002507 error = spu_acquire(ctx);
2508 if (error)
2509 return error;
2510
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002511 while (cnt < len) {
2512 char tbuf[128];
2513 int width;
2514
Jeremy Kerr14f693e2008-10-16 10:51:46 +11002515 if (spufs_switch_log_used(ctx) == 0) {
2516 if (cnt > 0) {
2517 /* If there's data ready to go, we can
2518 * just return straight away */
2519 break;
2520
2521 } else if (file->f_flags & O_NONBLOCK) {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002522 error = -EAGAIN;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002523 break;
Jeremy Kerr14f693e2008-10-16 10:51:46 +11002524
2525 } else {
2526 /* spufs_wait will drop the mutex and
2527 * re-acquire, but since we're in read(), the
2528 * file cannot be _released (and so
2529 * ctx->switch_log is stable).
2530 */
2531 error = spufs_wait(ctx->switch_log->wait,
2532 spufs_switch_log_used(ctx) > 0);
2533
2534 /* On error, spufs_wait returns without the
2535 * state mutex held */
2536 if (error)
2537 return error;
2538
2539 /* We may have had entries read from underneath
2540 * us while we dropped the mutex in spufs_wait,
2541 * so re-check */
2542 if (spufs_switch_log_used(ctx) == 0)
2543 continue;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002544 }
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002545 }
2546
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002547 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002548 if (width < len)
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002549 ctx->switch_log->tail =
2550 (ctx->switch_log->tail + 1) %
2551 SWITCH_LOG_BUFSIZE;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002552 else
2553 /* If the record is greater than space available return
2554 * partial buffer (so far) */
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002555 break;
2556
2557 error = copy_to_user(buf + cnt, tbuf, width);
2558 if (error)
2559 break;
2560 cnt += width;
2561 }
2562
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002563 spu_release(ctx);
2564
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002565 return cnt == 0 ? error : cnt;
2566}
2567
2568static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
2569{
2570 struct inode *inode = file->f_path.dentry->d_inode;
2571 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2572 unsigned int mask = 0;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002573 int rc;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002574
2575 poll_wait(file, &ctx->switch_log->wait, wait);
2576
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002577 rc = spu_acquire(ctx);
2578 if (rc)
2579 return rc;
2580
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002581 if (spufs_switch_log_used(ctx) > 0)
2582 mask |= POLLIN;
2583
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002584 spu_release(ctx);
2585
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002586 return mask;
2587}
2588
2589static const struct file_operations spufs_switch_log_fops = {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002590 .owner = THIS_MODULE,
2591 .open = spufs_switch_log_open,
2592 .read = spufs_switch_log_read,
2593 .poll = spufs_switch_log_poll,
2594 .release = spufs_switch_log_release,
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002595};
2596
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002597/**
2598 * Log a context switch event to a switch log reader.
2599 *
2600 * Must be called with ctx->state_mutex held.
2601 */
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002602void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2603 u32 type, u32 val)
2604{
2605 if (!ctx->switch_log)
2606 return;
2607
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002608 if (spufs_switch_log_avail(ctx) > 1) {
2609 struct switch_log_entry *p;
2610
2611 p = ctx->switch_log->log + ctx->switch_log->head;
2612 ktime_get_ts(&p->tstamp);
2613 p->timebase = get_tb();
2614 p->spu_id = spu ? spu->number : -1;
2615 p->type = type;
2616 p->val = val;
2617
2618 ctx->switch_log->head =
2619 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2620 }
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002621
2622 wake_up(&ctx->switch_log->wait);
2623}
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002624
Luke Browning46deed62008-06-16 11:36:43 +10002625static int spufs_show_ctx(struct seq_file *s, void *private)
2626{
2627 struct spu_context *ctx = s->private;
2628 u64 mfc_control_RW;
2629
2630 mutex_lock(&ctx->state_mutex);
2631 if (ctx->spu) {
2632 struct spu *spu = ctx->spu;
2633 struct spu_priv2 __iomem *priv2 = spu->priv2;
2634
2635 spin_lock_irq(&spu->register_lock);
2636 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2637 spin_unlock_irq(&spu->register_lock);
2638 } else {
2639 struct spu_state *csa = &ctx->csa;
2640
2641 mfc_control_RW = csa->priv2.mfc_control_RW;
2642 }
2643
2644 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
2645 " %c %lx %lx %lx %lx %x %x\n",
2646 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2647 ctx->flags,
2648 ctx->sched_flags,
2649 ctx->prio,
2650 ctx->time_slice,
2651 ctx->spu ? ctx->spu->number : -1,
2652 !list_empty(&ctx->rq) ? 'q' : ' ',
2653 ctx->csa.class_0_pending,
2654 ctx->csa.class_0_dar,
2655 ctx->csa.class_1_dsisr,
2656 mfc_control_RW,
2657 ctx->ops->runcntl_read(ctx),
2658 ctx->ops->status_read(ctx));
2659
2660 mutex_unlock(&ctx->state_mutex);
2661
2662 return 0;
2663}
2664
2665static int spufs_ctx_open(struct inode *inode, struct file *file)
2666{
2667 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2668}
2669
2670static const struct file_operations spufs_ctx_fops = {
2671 .open = spufs_ctx_open,
2672 .read = seq_read,
2673 .llseek = seq_lseek,
2674 .release = single_release,
2675};
2676
Jeremy Kerr23d893f2008-06-30 12:17:28 +10002677struct spufs_tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002678 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002679 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2680 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002681 { "mbox", &spufs_mbox_fops, 0444, },
2682 { "ibox", &spufs_ibox_fops, 0444, },
2683 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002684 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2685 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2686 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002687 { "signal1", &spufs_signal1_fops, 0666, },
2688 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002689 { "signal1_type", &spufs_signal1_type, 0666, },
2690 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002691 { "cntl", &spufs_cntl_fops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002692 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002693 { "lslr", &spufs_lslr_ops, 0444, },
2694 { "mfc", &spufs_mfc_fops, 0666, },
2695 { "mss", &spufs_mss_fops, 0666, },
2696 { "npc", &spufs_npc_ops, 0666, },
2697 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002698 { "decr", &spufs_decr_ops, 0666, },
2699 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002700 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002701 { "event_status", &spufs_event_status_ops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002702 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002703 { "phys-id", &spufs_id_ops, 0666, },
2704 { "object-id", &spufs_object_id_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002705 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
2706 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
2707 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
2708 { "dma_info", &spufs_dma_info_fops, 0444,
2709 sizeof(struct spu_dma_info), },
2710 { "proxydma_info", &spufs_proxydma_info_fops, 0444,
2711 sizeof(struct spu_proxydma_info)},
Christoph Hellwig476273a2007-06-29 10:58:01 +10002712 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002713 { "stat", &spufs_stat_fops, 0444, },
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002714 { "switch_log", &spufs_switch_log_fops, 0444 },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002715 {},
2716};
Mark Nutter5737edd2006-10-24 18:31:16 +02002717
Jeremy Kerr23d893f2008-06-30 12:17:28 +10002718struct spufs_tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002719 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002720 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002721 { "mbox", &spufs_mbox_fops, 0444, },
2722 { "ibox", &spufs_ibox_fops, 0444, },
2723 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002724 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2725 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2726 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002727 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2728 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002729 { "signal1_type", &spufs_signal1_type, 0666, },
2730 { "signal2_type", &spufs_signal2_type, 0666, },
2731 { "mss", &spufs_mss_fops, 0666, },
2732 { "mfc", &spufs_mfc_fops, 0666, },
2733 { "cntl", &spufs_cntl_fops, 0666, },
2734 { "npc", &spufs_npc_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002735 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002736 { "phys-id", &spufs_id_ops, 0666, },
2737 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002738 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002739 { "stat", &spufs_stat_fops, 0444, },
Jeremy Kerr2c3e4782008-07-03 11:42:20 +10002740 {},
2741};
2742
2743struct spufs_tree_descr spufs_dir_debug_contents[] = {
Luke Browning46deed62008-06-16 11:36:43 +10002744 { ".ctx", &spufs_ctx_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002745 {},
2746};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002747
2748struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002749 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2750 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002751 { "lslr", NULL, spufs_lslr_get, 19 },
2752 { "decr", NULL, spufs_decr_get, 19 },
2753 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002754 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2755 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002756 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002757 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002758 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2759 { "event_mask", NULL, spufs_event_mask_get, 19 },
2760 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002761 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2762 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2763 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2764 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2765 { "proxydma_info", __spufs_proxydma_info_read,
2766 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002767 { "object-id", NULL, spufs_object_id_get, 19 },
2768 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002769 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002770};