blob: b73c369cc6f167a4fa52c9162cfed557c9c1bba0 [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 Bergmann932f5352006-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
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900393 /*
Jeremy Kerrd5883132008-02-26 13:31:42 +1100394 * Because we release the mmap_sem, the context may be destroyed while
395 * we're in spu_wait. Grab an extra reference so it isn't destroyed
396 * in the meantime.
397 */
398 get_spu_context(ctx);
399
400 /*
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900401 * We have to wait for context to be loaded before we have
402 * pages to hand out to the user, but we don't want to wait
403 * with the mmap_sem held.
404 * It is possible to drop the mmap_sem here, but then we need
Nick Pigginb1e22702008-06-10 09:26:08 +1000405 * to return VM_FAULT_NOPAGE because the mappings may have
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900406 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100407 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900408 if (spu_acquire(ctx))
Jeremy Kerrd5883132008-02-26 13:31:42 +1100409 goto refault;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900410
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900411 if (ctx->state == SPU_STATE_SAVED) {
412 up_read(&current->mm->mmap_sem);
Nick Pigginb1e22702008-06-10 09:26:08 +1000413 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100414 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Nick Pigginb1e22702008-06-10 09:26:08 +1000415 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900416 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900417 } else {
418 area = ctx->spu->problem_phys + ps_offs;
Nick Pigginb1e22702008-06-10 09:26:08 +1000419 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
420 (area + offset) >> PAGE_SHIFT);
421 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900422 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100423
Christoph Hellwigeebead52008-02-08 15:50:41 +1100424 if (!ret)
425 spu_release(ctx);
Jeremy Kerrd5883132008-02-26 13:31:42 +1100426
427refault:
428 put_spu_context(ctx);
Nick Pigginb1e22702008-06-10 09:26:08 +1000429 return VM_FAULT_NOPAGE;
Mark Nutter6df10a82006-03-23 00:00:12 +0100430}
431
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200432#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +1000433static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
434 struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +0100435{
Jeremy Kerr87ff6092008-07-01 10:22:50 +1000436 return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +0100437}
438
439static struct vm_operations_struct spufs_cntl_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000440 .fault = spufs_cntl_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +0100441};
442
443/*
444 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100445 */
446static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
447{
448 if (!(vma->vm_flags & VM_SHARED))
449 return -EINVAL;
450
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100451 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100452 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200453 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100454
455 vma->vm_ops = &spufs_cntl_mmap_vmops;
456 return 0;
457}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200458#else /* SPUFS_MMAP_4K */
459#define spufs_cntl_mmap NULL
460#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100461
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900462static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200463{
464 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900465 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200466
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900467 ret = spu_acquire(ctx);
468 if (ret)
469 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900470 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200471 spu_release(ctx);
472
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900473 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200474}
475
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900476static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200477{
478 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900479 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200480
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900481 ret = spu_acquire(ctx);
482 if (ret)
483 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200484 ctx->ops->runcntl_write(ctx, val);
485 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900486
487 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200488}
489
Mark Nutter6df10a82006-03-23 00:00:12 +0100490static int spufs_cntl_open(struct inode *inode, struct file *file)
491{
492 struct spufs_inode_info *i = SPUFS_I(inode);
493 struct spu_context *ctx = i->i_ctx;
494
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000495 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100496 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200497 if (!i->i_openers++)
498 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000499 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800500 return simple_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200501 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100502}
503
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200504static int
505spufs_cntl_release(struct inode *inode, struct file *file)
506{
507 struct spufs_inode_info *i = SPUFS_I(inode);
508 struct spu_context *ctx = i->i_ctx;
509
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800510 simple_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200511
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000512 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200513 if (!--i->i_openers)
514 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000515 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200516 return 0;
517}
518
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800519static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100520 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200521 .release = spufs_cntl_release,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800522 .read = simple_attr_read,
523 .write = simple_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100524 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100525};
526
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500527static int
528spufs_regs_open(struct inode *inode, struct file *file)
529{
530 struct spufs_inode_info *i = SPUFS_I(inode);
531 file->private_data = i->i_ctx;
532 return 0;
533}
534
535static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100536__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
537 size_t size, loff_t *pos)
538{
539 struct spu_lscsa *lscsa = ctx->csa.lscsa;
540 return simple_read_from_buffer(buffer, size, pos,
541 lscsa->gprs, sizeof lscsa->gprs);
542}
543
544static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500545spufs_regs_read(struct file *file, char __user *buffer,
546 size_t size, loff_t *pos)
547{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500548 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100549 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500550
Jeremy Kerrf027faa2008-10-16 11:11:12 +1100551 /* pre-check for file position: if we'd return EOF, there's no point
552 * causing a deschedule */
553 if (*pos >= sizeof(ctx->csa.lscsa->gprs))
554 return 0;
555
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900556 ret = spu_acquire_saved(ctx);
557 if (ret)
558 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100559 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200560 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500561 return ret;
562}
563
564static ssize_t
565spufs_regs_write(struct file *file, const char __user *buffer,
566 size_t size, loff_t *pos)
567{
568 struct spu_context *ctx = file->private_data;
569 struct spu_lscsa *lscsa = ctx->csa.lscsa;
570 int ret;
571
572 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
573 if (size <= 0)
574 return -EFBIG;
575 *pos += size;
576
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900577 ret = spu_acquire_saved(ctx);
578 if (ret)
579 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500580
581 ret = copy_from_user(lscsa->gprs + *pos - size,
582 buffer, size) ? -EFAULT : size;
583
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200584 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500585 return ret;
586}
587
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800588static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500589 .open = spufs_regs_open,
590 .read = spufs_regs_read,
591 .write = spufs_regs_write,
592 .llseek = generic_file_llseek,
593};
594
595static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100596__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
597 size_t size, loff_t * pos)
598{
599 struct spu_lscsa *lscsa = ctx->csa.lscsa;
600 return simple_read_from_buffer(buffer, size, pos,
601 &lscsa->fpcr, sizeof(lscsa->fpcr));
602}
603
604static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500605spufs_fpcr_read(struct file *file, char __user * buffer,
606 size_t size, loff_t * pos)
607{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500608 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100609 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500610
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900611 ret = spu_acquire_saved(ctx);
612 if (ret)
613 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100614 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200615 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500616 return ret;
617}
618
619static ssize_t
620spufs_fpcr_write(struct file *file, const char __user * buffer,
621 size_t size, loff_t * pos)
622{
623 struct spu_context *ctx = file->private_data;
624 struct spu_lscsa *lscsa = ctx->csa.lscsa;
625 int ret;
626
627 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
628 if (size <= 0)
629 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900630
631 ret = spu_acquire_saved(ctx);
632 if (ret)
633 return ret;
634
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500635 *pos += size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500636 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
637 buffer, size) ? -EFAULT : size;
638
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200639 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500640 return ret;
641}
642
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800643static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500644 .open = spufs_regs_open,
645 .read = spufs_fpcr_read,
646 .write = spufs_fpcr_write,
647 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500648};
649
650/* generic open function for all pipe-like files */
651static int spufs_pipe_open(struct inode *inode, struct file *file)
652{
653 struct spufs_inode_info *i = SPUFS_I(inode);
654 file->private_data = i->i_ctx;
655
656 return nonseekable_open(inode, file);
657}
658
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200659/*
660 * Read as many bytes from the mailbox as possible, until
661 * one of the conditions becomes true:
662 *
663 * - no more data available in the mailbox
664 * - end of the user provided buffer
665 * - end of the mapped area
666 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500667static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
668 size_t len, loff_t *pos)
669{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500670 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200671 u32 mbox_data, __user *udata;
672 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500673
674 if (len < 4)
675 return -EINVAL;
676
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200677 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500678 return -EFAULT;
679
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200680 udata = (void __user *)buf;
681
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900682 count = spu_acquire(ctx);
683 if (count)
684 return count;
685
Arnd Bergmann274cef52006-10-24 18:01:42 +0200686 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200687 int ret;
688 ret = ctx->ops->mbox_read(ctx, &mbox_data);
689 if (ret == 0)
690 break;
691
692 /*
693 * at the end of the mapped area, we can fault
694 * but still need to return the data we have
695 * read successfully so far.
696 */
697 ret = __put_user(mbox_data, udata);
698 if (ret) {
699 if (!count)
700 count = -EFAULT;
701 break;
702 }
703 }
704 spu_release(ctx);
705
706 if (!count)
707 count = -EAGAIN;
708
709 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500710}
711
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800712static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500713 .open = spufs_pipe_open,
714 .read = spufs_mbox_read,
715};
716
717static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
718 size_t len, loff_t *pos)
719{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500720 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900721 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500722 u32 mbox_stat;
723
724 if (len < 4)
725 return -EINVAL;
726
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900727 ret = spu_acquire(ctx);
728 if (ret)
729 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500730
731 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
732
733 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500734
735 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
736 return -EFAULT;
737
738 return 4;
739}
740
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800741static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500742 .open = spufs_pipe_open,
743 .read = spufs_mbox_stat_read,
744};
745
746/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500747size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500748{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500749 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500750}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500751
752static int spufs_ibox_fasync(int fd, struct file *file, int on)
753{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500754 struct spu_context *ctx = file->private_data;
755
756 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
757}
758
759/* interrupt-level ibox callback function. */
760void spufs_ibox_callback(struct spu *spu)
761{
762 struct spu_context *ctx = spu->ctx;
763
Luke Browninge65c2f62007-12-20 16:39:59 +0900764 if (!ctx)
765 return;
766
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500767 wake_up_all(&ctx->ibox_wq);
768 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500769}
770
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200771/*
772 * Read as many bytes from the interrupt mailbox as possible, until
773 * one of the conditions becomes true:
774 *
775 * - no more data available in the mailbox
776 * - end of the user provided buffer
777 * - end of the mapped area
778 *
779 * If the file is opened without O_NONBLOCK, we wait here until
780 * any data is available, but return when we have been able to
781 * read something.
782 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500783static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
784 size_t len, loff_t *pos)
785{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500786 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200787 u32 ibox_data, __user *udata;
788 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500789
790 if (len < 4)
791 return -EINVAL;
792
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200793 if (!access_ok(VERIFY_WRITE, buf, len))
794 return -EFAULT;
795
796 udata = (void __user *)buf;
797
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900798 count = spu_acquire(ctx);
799 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100800 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500801
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200802 /* wait only for the first element */
803 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500804 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100805 if (!spu_ibox_read(ctx, &ibox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200806 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100807 goto out_unlock;
808 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500809 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200810 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100811 if (count)
812 goto out;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200813 }
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200814
815 /* if we can't write at all, return -EFAULT */
816 count = __put_user(ibox_data, udata);
817 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100818 goto out_unlock;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200819
820 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
821 int ret;
822 ret = ctx->ops->ibox_read(ctx, &ibox_data);
823 if (ret == 0)
824 break;
825 /*
826 * at the end of the mapped area, we can fault
827 * but still need to return the data we have
828 * read successfully so far.
829 */
830 ret = __put_user(ibox_data, udata);
831 if (ret)
832 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500833 }
834
Christoph Hellwigeebead52008-02-08 15:50:41 +1100835out_unlock:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500836 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100837out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200838 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500839}
840
841static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
842{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500843 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500844 unsigned int mask;
845
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500846 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500847
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900848 /*
849 * For now keep this uninterruptible and also ignore the rule
850 * that poll should not sleep. Will be fixed later.
851 */
852 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500853 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
854 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500855
856 return mask;
857}
858
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800859static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500860 .open = spufs_pipe_open,
861 .read = spufs_ibox_read,
862 .poll = spufs_ibox_poll,
863 .fasync = spufs_ibox_fasync,
864};
865
866static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
867 size_t len, loff_t *pos)
868{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500869 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900870 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500871 u32 ibox_stat;
872
873 if (len < 4)
874 return -EINVAL;
875
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900876 ret = spu_acquire(ctx);
877 if (ret)
878 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500879 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
880 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500881
882 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
883 return -EFAULT;
884
885 return 4;
886}
887
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800888static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500889 .open = spufs_pipe_open,
890 .read = spufs_ibox_stat_read,
891};
892
893/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500894size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500895{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500896 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500897}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500898
899static int spufs_wbox_fasync(int fd, struct file *file, int on)
900{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500901 struct spu_context *ctx = file->private_data;
902 int ret;
903
904 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
905
906 return ret;
907}
908
909/* interrupt-level wbox callback function. */
910void spufs_wbox_callback(struct spu *spu)
911{
912 struct spu_context *ctx = spu->ctx;
913
Luke Browninge65c2f62007-12-20 16:39:59 +0900914 if (!ctx)
915 return;
916
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500917 wake_up_all(&ctx->wbox_wq);
918 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500919}
920
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200921/*
922 * Write as many bytes to the interrupt mailbox as possible, until
923 * one of the conditions becomes true:
924 *
925 * - the mailbox is full
926 * - end of the user provided buffer
927 * - end of the mapped area
928 *
929 * If the file is opened without O_NONBLOCK, we wait here until
930 * space is availabyl, but return when we have been able to
931 * write something.
932 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500933static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
934 size_t len, loff_t *pos)
935{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500936 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200937 u32 wbox_data, __user *udata;
938 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500939
940 if (len < 4)
941 return -EINVAL;
942
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200943 udata = (void __user *)buf;
944 if (!access_ok(VERIFY_READ, buf, len))
945 return -EFAULT;
946
947 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500948 return -EFAULT;
949
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900950 count = spu_acquire(ctx);
951 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100952 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500953
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200954 /*
955 * make sure we can at least write one element, by waiting
956 * in case of !O_NONBLOCK
957 */
958 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500959 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100960 if (!spu_wbox_write(ctx, wbox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200961 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100962 goto out_unlock;
963 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500964 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200965 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100966 if (count)
967 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500968 }
969
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500970
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200971 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200972 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
973 int ret;
974 ret = __get_user(wbox_data, udata);
975 if (ret)
976 break;
977
978 ret = spu_wbox_write(ctx, wbox_data);
979 if (ret == 0)
980 break;
981 }
982
Christoph Hellwigeebead52008-02-08 15:50:41 +1100983out_unlock:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200984 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100985out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200986 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500987}
988
989static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
990{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500991 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500992 unsigned int mask;
993
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500994 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500995
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900996 /*
997 * For now keep this uninterruptible and also ignore the rule
998 * that poll should not sleep. Will be fixed later.
999 */
1000 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -05001001 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
1002 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001003
1004 return mask;
1005}
1006
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001007static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001008 .open = spufs_pipe_open,
1009 .write = spufs_wbox_write,
1010 .poll = spufs_wbox_poll,
1011 .fasync = spufs_wbox_fasync,
1012};
1013
1014static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
1015 size_t len, loff_t *pos)
1016{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001017 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001018 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001019 u32 wbox_stat;
1020
1021 if (len < 4)
1022 return -EINVAL;
1023
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001024 ret = spu_acquire(ctx);
1025 if (ret)
1026 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001027 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
1028 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001029
1030 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
1031 return -EFAULT;
1032
1033 return 4;
1034}
1035
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001036static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001037 .open = spufs_pipe_open,
1038 .read = spufs_wbox_stat_read,
1039};
1040
Mark Nutter6df10a82006-03-23 00:00:12 +01001041static int spufs_signal1_open(struct inode *inode, struct file *file)
1042{
1043 struct spufs_inode_info *i = SPUFS_I(inode);
1044 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001045
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001046 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001047 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001048 if (!i->i_openers++)
1049 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001050 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001051 return nonseekable_open(inode, file);
1052}
1053
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001054static int
1055spufs_signal1_release(struct inode *inode, struct file *file)
1056{
1057 struct spufs_inode_info *i = SPUFS_I(inode);
1058 struct spu_context *ctx = i->i_ctx;
1059
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001060 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001061 if (!--i->i_openers)
1062 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001063 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001064 return 0;
1065}
1066
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001067static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001068 size_t len, loff_t *pos)
1069{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001070 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001071 u32 data;
1072
Arnd Bergmann67207b92005-11-15 15:53:48 -05001073 if (len < 4)
1074 return -EINVAL;
1075
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001076 if (ctx->csa.spu_chnlcnt_RW[3]) {
1077 data = ctx->csa.spu_chnldata_RW[3];
1078 ret = 4;
1079 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001080
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001081 if (!ret)
1082 goto out;
1083
Arnd Bergmann67207b92005-11-15 15:53:48 -05001084 if (copy_to_user(buf, &data, 4))
1085 return -EFAULT;
1086
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001087out:
1088 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001089}
1090
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001091static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1092 size_t len, loff_t *pos)
1093{
1094 int ret;
1095 struct spu_context *ctx = file->private_data;
1096
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001097 ret = spu_acquire_saved(ctx);
1098 if (ret)
1099 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001100 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001101 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001102
1103 return ret;
1104}
1105
Arnd Bergmann67207b92005-11-15 15:53:48 -05001106static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1107 size_t len, loff_t *pos)
1108{
1109 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001110 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001111 u32 data;
1112
1113 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001114
1115 if (len < 4)
1116 return -EINVAL;
1117
1118 if (copy_from_user(&data, buf, 4))
1119 return -EFAULT;
1120
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001121 ret = spu_acquire(ctx);
1122 if (ret)
1123 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001124 ctx->ops->signal1_write(ctx, data);
1125 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001126
1127 return 4;
1128}
1129
Nick Pigginb1e22702008-06-10 09:26:08 +10001130static int
1131spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001132{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001133#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1134 return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1135#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001136 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1137 * signal 1 and 2 area
1138 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001139 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001140#else
1141#error unsupported page size
1142#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001143}
1144
1145static struct vm_operations_struct spufs_signal1_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001146 .fault = spufs_signal1_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001147};
1148
1149static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1150{
1151 if (!(vma->vm_flags & VM_SHARED))
1152 return -EINVAL;
1153
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001154 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001155 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001156 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001157
1158 vma->vm_ops = &spufs_signal1_mmap_vmops;
1159 return 0;
1160}
Mark Nutter6df10a82006-03-23 00:00:12 +01001161
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001162static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001163 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001164 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001165 .read = spufs_signal1_read,
1166 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001167 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001168};
1169
Jeremy Kerrd054b362007-07-20 21:39:31 +02001170static const struct file_operations spufs_signal1_nosched_fops = {
1171 .open = spufs_signal1_open,
1172 .release = spufs_signal1_release,
1173 .write = spufs_signal1_write,
1174 .mmap = spufs_signal1_mmap,
1175};
1176
Mark Nutter6df10a82006-03-23 00:00:12 +01001177static int spufs_signal2_open(struct inode *inode, struct file *file)
1178{
1179 struct spufs_inode_info *i = SPUFS_I(inode);
1180 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001181
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001182 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001183 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001184 if (!i->i_openers++)
1185 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001186 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001187 return nonseekable_open(inode, file);
1188}
1189
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001190static int
1191spufs_signal2_release(struct inode *inode, struct file *file)
1192{
1193 struct spufs_inode_info *i = SPUFS_I(inode);
1194 struct spu_context *ctx = i->i_ctx;
1195
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001196 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001197 if (!--i->i_openers)
1198 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001199 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001200 return 0;
1201}
1202
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001203static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001204 size_t len, loff_t *pos)
1205{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001206 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001207 u32 data;
1208
Arnd Bergmann67207b92005-11-15 15:53:48 -05001209 if (len < 4)
1210 return -EINVAL;
1211
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001212 if (ctx->csa.spu_chnlcnt_RW[4]) {
1213 data = ctx->csa.spu_chnldata_RW[4];
1214 ret = 4;
1215 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001216
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001217 if (!ret)
1218 goto out;
1219
Arnd Bergmann67207b92005-11-15 15:53:48 -05001220 if (copy_to_user(buf, &data, 4))
1221 return -EFAULT;
1222
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001223out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001224 return ret;
1225}
1226
1227static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1228 size_t len, loff_t *pos)
1229{
1230 struct spu_context *ctx = file->private_data;
1231 int ret;
1232
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001233 ret = spu_acquire_saved(ctx);
1234 if (ret)
1235 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001236 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001237 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001238
1239 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001240}
1241
1242static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1243 size_t len, loff_t *pos)
1244{
1245 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001246 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001247 u32 data;
1248
1249 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001250
1251 if (len < 4)
1252 return -EINVAL;
1253
1254 if (copy_from_user(&data, buf, 4))
1255 return -EFAULT;
1256
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001257 ret = spu_acquire(ctx);
1258 if (ret)
1259 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001260 ctx->ops->signal2_write(ctx, data);
1261 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001262
1263 return 4;
1264}
1265
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001266#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001267static int
1268spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001269{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001270#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1271 return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1272#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001273 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1274 * signal 1 and 2 area
1275 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001276 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001277#else
1278#error unsupported page size
1279#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001280}
1281
1282static struct vm_operations_struct spufs_signal2_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001283 .fault = spufs_signal2_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001284};
1285
1286static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1287{
1288 if (!(vma->vm_flags & VM_SHARED))
1289 return -EINVAL;
1290
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001291 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001292 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001293 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001294
1295 vma->vm_ops = &spufs_signal2_mmap_vmops;
1296 return 0;
1297}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001298#else /* SPUFS_MMAP_4K */
1299#define spufs_signal2_mmap NULL
1300#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001301
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001302static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001303 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001304 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001305 .read = spufs_signal2_read,
1306 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001307 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001308};
1309
Jeremy Kerrd054b362007-07-20 21:39:31 +02001310static const struct file_operations spufs_signal2_nosched_fops = {
1311 .open = spufs_signal2_open,
1312 .release = spufs_signal2_release,
1313 .write = spufs_signal2_write,
1314 .mmap = spufs_signal2_mmap,
1315};
1316
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001317/*
1318 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1319 * work of acquiring (or not) the SPU context before calling through
1320 * to the actual get routine. The set routine is called directly.
1321 */
1322#define SPU_ATTR_NOACQUIRE 0
1323#define SPU_ATTR_ACQUIRE 1
1324#define SPU_ATTR_ACQUIRE_SAVED 2
1325
1326#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001327static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001328{ \
1329 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001330 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001331 \
1332 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001333 ret = spu_acquire(ctx); \
1334 if (ret) \
1335 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001336 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001337 spu_release(ctx); \
1338 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001339 ret = spu_acquire_saved(ctx); \
1340 if (ret) \
1341 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001342 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001343 spu_release_saved(ctx); \
1344 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001345 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001346 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001347 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001348} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001349DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001350
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001351static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001352{
1353 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001354 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001355
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001356 ret = spu_acquire(ctx);
1357 if (ret)
1358 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001359 ctx->ops->signal1_type_set(ctx, val);
1360 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001361
1362 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001363}
1364
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001365static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001366{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001367 return ctx->ops->signal1_type_get(ctx);
1368}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001369DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001370 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001371
Arnd Bergmann67207b92005-11-15 15:53:48 -05001372
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001373static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001374{
1375 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001376 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001377
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001378 ret = spu_acquire(ctx);
1379 if (ret)
1380 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001381 ctx->ops->signal2_type_set(ctx, val);
1382 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001383
1384 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001385}
1386
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001387static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001388{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001389 return ctx->ops->signal2_type_get(ctx);
1390}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001391DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001392 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001393
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001394#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001395static int
1396spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001397{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001398 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001399}
1400
1401static struct vm_operations_struct spufs_mss_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001402 .fault = spufs_mss_mmap_fault,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001403};
1404
1405/*
1406 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001407 */
1408static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1409{
1410 if (!(vma->vm_flags & VM_SHARED))
1411 return -EINVAL;
1412
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001413 vma->vm_flags |= VM_IO | VM_PFNMAP;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001414 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001415 | _PAGE_NO_CACHE | _PAGE_GUARDED);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001416
1417 vma->vm_ops = &spufs_mss_mmap_vmops;
1418 return 0;
1419}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001420#else /* SPUFS_MMAP_4K */
1421#define spufs_mss_mmap NULL
1422#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001423
1424static int spufs_mss_open(struct inode *inode, struct file *file)
1425{
1426 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001427 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001428
1429 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001430
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001431 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001432 if (!i->i_openers++)
1433 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001434 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001435 return nonseekable_open(inode, file);
1436}
1437
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001438static int
1439spufs_mss_release(struct inode *inode, struct file *file)
1440{
1441 struct spufs_inode_info *i = SPUFS_I(inode);
1442 struct spu_context *ctx = i->i_ctx;
1443
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001444 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001445 if (!--i->i_openers)
1446 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001447 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001448 return 0;
1449}
1450
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001451static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001452 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001453 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001454 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001455};
1456
Nick Pigginb1e22702008-06-10 09:26:08 +10001457static int
1458spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001459{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001460 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001461}
1462
1463static struct vm_operations_struct spufs_psmap_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001464 .fault = spufs_psmap_mmap_fault,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001465};
1466
1467/*
1468 * mmap support for full problem state area [0x00000 - 0x1ffff].
1469 */
1470static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1471{
1472 if (!(vma->vm_flags & VM_SHARED))
1473 return -EINVAL;
1474
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001475 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001476 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1477 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1478
1479 vma->vm_ops = &spufs_psmap_mmap_vmops;
1480 return 0;
1481}
1482
1483static int spufs_psmap_open(struct inode *inode, struct file *file)
1484{
1485 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001486 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001487
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001488 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001489 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001490 if (!i->i_openers++)
1491 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001492 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001493 return nonseekable_open(inode, file);
1494}
1495
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001496static int
1497spufs_psmap_release(struct inode *inode, struct file *file)
1498{
1499 struct spufs_inode_info *i = SPUFS_I(inode);
1500 struct spu_context *ctx = i->i_ctx;
1501
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001502 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001503 if (!--i->i_openers)
1504 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001505 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001506 return 0;
1507}
1508
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001509static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001510 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001511 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001512 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001513};
1514
1515
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001516#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001517static int
1518spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001519{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001520 return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +01001521}
1522
1523static struct vm_operations_struct spufs_mfc_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001524 .fault = spufs_mfc_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001525};
1526
1527/*
1528 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001529 */
1530static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1531{
1532 if (!(vma->vm_flags & VM_SHARED))
1533 return -EINVAL;
1534
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001535 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001536 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001537 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001538
1539 vma->vm_ops = &spufs_mfc_mmap_vmops;
1540 return 0;
1541}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001542#else /* SPUFS_MMAP_4K */
1543#define spufs_mfc_mmap NULL
1544#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001545
1546static int spufs_mfc_open(struct inode *inode, struct file *file)
1547{
1548 struct spufs_inode_info *i = SPUFS_I(inode);
1549 struct spu_context *ctx = i->i_ctx;
1550
1551 /* we don't want to deal with DMA into other processes */
1552 if (ctx->owner != current->mm)
1553 return -EINVAL;
1554
1555 if (atomic_read(&inode->i_count) != 1)
1556 return -EBUSY;
1557
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001558 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001559 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001560 if (!i->i_openers++)
1561 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001562 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001563 return nonseekable_open(inode, file);
1564}
1565
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001566static int
1567spufs_mfc_release(struct inode *inode, struct file *file)
1568{
1569 struct spufs_inode_info *i = SPUFS_I(inode);
1570 struct spu_context *ctx = i->i_ctx;
1571
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001572 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001573 if (!--i->i_openers)
1574 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001575 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001576 return 0;
1577}
1578
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001579/* interrupt-level mfc callback function. */
1580void spufs_mfc_callback(struct spu *spu)
1581{
1582 struct spu_context *ctx = spu->ctx;
1583
Luke Browninge65c2f62007-12-20 16:39:59 +09001584 if (!ctx)
1585 return;
1586
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001587 wake_up_all(&ctx->mfc_wq);
1588
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001589 pr_debug("%s %s\n", __func__, spu->name);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001590 if (ctx->mfc_fasync) {
1591 u32 free_elements, tagstatus;
1592 unsigned int mask;
1593
1594 /* no need for spu_acquire in interrupt context */
1595 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1596 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1597
1598 mask = 0;
1599 if (free_elements & 0xffff)
1600 mask |= POLLOUT;
1601 if (tagstatus & ctx->tagwait)
1602 mask |= POLLIN;
1603
1604 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1605 }
1606}
1607
1608static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1609{
1610 /* See if there is one tag group is complete */
1611 /* FIXME we need locking around tagwait */
1612 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1613 ctx->tagwait &= ~*status;
1614 if (*status)
1615 return 1;
1616
1617 /* enable interrupt waiting for any tag group,
1618 may silently fail if interrupts are already enabled */
1619 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1620 return 0;
1621}
1622
1623static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1624 size_t size, loff_t *pos)
1625{
1626 struct spu_context *ctx = file->private_data;
1627 int ret = -EINVAL;
1628 u32 status;
1629
1630 if (size != 4)
1631 goto out;
1632
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001633 ret = spu_acquire(ctx);
1634 if (ret)
1635 return ret;
1636
1637 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001638 if (file->f_flags & O_NONBLOCK) {
1639 status = ctx->ops->read_mfc_tagstatus(ctx);
1640 if (!(status & ctx->tagwait))
1641 ret = -EAGAIN;
1642 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001643 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001644 ctx->tagwait &= ~status;
1645 } else {
1646 ret = spufs_wait(ctx->mfc_wq,
1647 spufs_read_mfc_tagstatus(ctx, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001648 if (ret)
1649 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001650 }
1651 spu_release(ctx);
1652
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001653 ret = 4;
1654 if (copy_to_user(buffer, &status, 4))
1655 ret = -EFAULT;
1656
1657out:
1658 return ret;
1659}
1660
1661static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1662{
1663 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1664 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1665
1666 switch (cmd->cmd) {
1667 case MFC_PUT_CMD:
1668 case MFC_PUTF_CMD:
1669 case MFC_PUTB_CMD:
1670 case MFC_GET_CMD:
1671 case MFC_GETF_CMD:
1672 case MFC_GETB_CMD:
1673 break;
1674 default:
1675 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1676 return -EIO;
1677 }
1678
1679 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1680 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1681 cmd->ea, cmd->lsa);
1682 return -EIO;
1683 }
1684
1685 switch (cmd->size & 0xf) {
1686 case 1:
1687 break;
1688 case 2:
1689 if (cmd->lsa & 1)
1690 goto error;
1691 break;
1692 case 4:
1693 if (cmd->lsa & 3)
1694 goto error;
1695 break;
1696 case 8:
1697 if (cmd->lsa & 7)
1698 goto error;
1699 break;
1700 case 0:
1701 if (cmd->lsa & 15)
1702 goto error;
1703 break;
1704 error:
1705 default:
1706 pr_debug("invalid DMA alignment %x for size %x\n",
1707 cmd->lsa & 0xf, cmd->size);
1708 return -EIO;
1709 }
1710
1711 if (cmd->size > 16 * 1024) {
1712 pr_debug("invalid DMA size %x\n", cmd->size);
1713 return -EIO;
1714 }
1715
1716 if (cmd->tag & 0xfff0) {
1717 /* we reserve the higher tag numbers for kernel use */
1718 pr_debug("invalid DMA tag\n");
1719 return -EIO;
1720 }
1721
1722 if (cmd->class) {
1723 /* not supported in this version */
1724 pr_debug("invalid DMA class\n");
1725 return -EIO;
1726 }
1727
1728 return 0;
1729}
1730
1731static int spu_send_mfc_command(struct spu_context *ctx,
1732 struct mfc_dma_command cmd,
1733 int *error)
1734{
1735 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1736 if (*error == -EAGAIN) {
1737 /* wait for any tag group to complete
1738 so we have space for the new command */
1739 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1740 /* try again, because the queue might be
1741 empty again */
1742 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1743 if (*error == -EAGAIN)
1744 return 0;
1745 }
1746 return 1;
1747}
1748
1749static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1750 size_t size, loff_t *pos)
1751{
1752 struct spu_context *ctx = file->private_data;
1753 struct mfc_dma_command cmd;
1754 int ret = -EINVAL;
1755
1756 if (size != sizeof cmd)
1757 goto out;
1758
1759 ret = -EFAULT;
1760 if (copy_from_user(&cmd, buffer, sizeof cmd))
1761 goto out;
1762
1763 ret = spufs_check_valid_dma(&cmd);
1764 if (ret)
1765 goto out;
1766
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001767 ret = spu_acquire(ctx);
1768 if (ret)
1769 goto out;
1770
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001771 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001772 if (ret)
1773 goto out;
1774
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001775 if (file->f_flags & O_NONBLOCK) {
1776 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1777 } else {
1778 int status;
1779 ret = spufs_wait(ctx->mfc_wq,
1780 spu_send_mfc_command(ctx, cmd, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001781 if (ret)
1782 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001783 if (status)
1784 ret = status;
1785 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001786
1787 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001788 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001789
1790 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001791 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001792
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001793out_unlock:
1794 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001795out:
1796 return ret;
1797}
1798
1799static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1800{
1801 struct spu_context *ctx = file->private_data;
1802 u32 free_elements, tagstatus;
1803 unsigned int mask;
1804
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001805 poll_wait(file, &ctx->mfc_wq, wait);
1806
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001807 /*
1808 * For now keep this uninterruptible and also ignore the rule
1809 * that poll should not sleep. Will be fixed later.
1810 */
1811 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001812 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1813 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1814 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1815 spu_release(ctx);
1816
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001817 mask = 0;
1818 if (free_elements & 0xffff)
1819 mask |= POLLOUT | POLLWRNORM;
1820 if (tagstatus & ctx->tagwait)
1821 mask |= POLLIN | POLLRDNORM;
1822
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001823 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001824 free_elements, tagstatus, ctx->tagwait);
1825
1826 return mask;
1827}
1828
Al Viro73b6af82006-06-25 16:42:33 -07001829static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001830{
1831 struct spu_context *ctx = file->private_data;
1832 int ret;
1833
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001834 ret = spu_acquire(ctx);
1835 if (ret)
Christoph Hellwigeebead52008-02-08 15:50:41 +11001836 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001837#if 0
1838/* this currently hangs */
1839 ret = spufs_wait(ctx->mfc_wq,
1840 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1841 if (ret)
1842 goto out;
1843 ret = spufs_wait(ctx->mfc_wq,
1844 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001845 if (ret)
1846 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001847#else
1848 ret = 0;
1849#endif
1850 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001851out:
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001852 return ret;
1853}
1854
1855static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1856 int datasync)
1857{
Al Viro73b6af82006-06-25 16:42:33 -07001858 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001859}
1860
1861static int spufs_mfc_fasync(int fd, struct file *file, int on)
1862{
1863 struct spu_context *ctx = file->private_data;
1864
1865 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1866}
1867
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001868static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001869 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001870 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001871 .read = spufs_mfc_read,
1872 .write = spufs_mfc_write,
1873 .poll = spufs_mfc_poll,
1874 .flush = spufs_mfc_flush,
1875 .fsync = spufs_mfc_fsync,
1876 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001877 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001878};
1879
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001880static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001881{
1882 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001883 int ret;
1884
1885 ret = spu_acquire(ctx);
1886 if (ret)
1887 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001888 ctx->ops->npc_write(ctx, val);
1889 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001890
1891 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001892}
1893
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001894static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001895{
1896 return ctx->ops->npc_read(ctx);
1897}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001898DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1899 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001900
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001901static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001902{
1903 struct spu_context *ctx = data;
1904 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001905 int ret;
1906
1907 ret = spu_acquire_saved(ctx);
1908 if (ret)
1909 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001910 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001911 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001912
1913 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001914}
1915
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001916static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001917{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001918 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001919 return lscsa->decr.slot[0];
1920}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001921DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1922 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001923
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001924static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001925{
1926 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001927 int ret;
1928
1929 ret = spu_acquire_saved(ctx);
1930 if (ret)
1931 return ret;
Masato Noguchid40a01d2007-07-20 21:39:38 +02001932 if (val)
1933 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1934 else
1935 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001936 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001937
1938 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001939}
1940
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001941static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001942{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001943 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1944 return SPU_DECR_STATUS_RUNNING;
1945 else
1946 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001947}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001948DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1949 spufs_decr_status_set, "0x%llx\n",
1950 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001951
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001952static int spufs_event_mask_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001953{
1954 struct spu_context *ctx = data;
1955 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001956 int ret;
1957
1958 ret = spu_acquire_saved(ctx);
1959 if (ret)
1960 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001961 lscsa->event_mask.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001962 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001963
1964 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001965}
1966
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001967static u64 spufs_event_mask_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001968{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001969 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001970 return lscsa->event_mask.slot[0];
1971}
1972
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001973DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1974 spufs_event_mask_set, "0x%llx\n",
1975 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001976
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001977static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001978{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001979 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001980 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001981 stat = state->spu_chnlcnt_RW[0];
1982 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001983 return state->spu_chnldata_RW[0];
1984 return 0;
1985}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001986DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1987 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001988
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001989static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001990{
1991 struct spu_context *ctx = data;
1992 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001993 int ret;
1994
1995 ret = spu_acquire_saved(ctx);
1996 if (ret)
1997 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001998 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001999 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002000
2001 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002002}
2003
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002004static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002005{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002006 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002007 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002008}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002009DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
2010 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002011
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002012static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002013{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002014 u64 num;
2015
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002016 if (ctx->state == SPU_STATE_RUNNABLE)
2017 num = ctx->spu->number;
2018 else
2019 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002020
2021 return num;
2022}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002023DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
2024 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002025
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002026static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002027{
2028 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002029 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002030}
2031
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002032static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02002033{
2034 struct spu_context *ctx = data;
2035 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002036
2037 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02002038}
2039
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002040DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2041 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02002042
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002043static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002044{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002045 return ctx->csa.priv2.spu_lslr_RW;
2046}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002047DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2048 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002049
2050static int spufs_info_open(struct inode *inode, struct file *file)
2051{
2052 struct spufs_inode_info *i = SPUFS_I(inode);
2053 struct spu_context *ctx = i->i_ctx;
2054 file->private_data = ctx;
2055 return 0;
2056}
2057
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002058static int spufs_caps_show(struct seq_file *s, void *private)
2059{
2060 struct spu_context *ctx = s->private;
2061
2062 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2063 seq_puts(s, "sched\n");
2064 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2065 seq_puts(s, "step\n");
2066 return 0;
2067}
2068
2069static int spufs_caps_open(struct inode *inode, struct file *file)
2070{
2071 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2072}
2073
2074static const struct file_operations spufs_caps_fops = {
2075 .open = spufs_caps_open,
2076 .read = seq_read,
2077 .llseek = seq_lseek,
2078 .release = single_release,
2079};
2080
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002081static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2082 char __user *buf, size_t len, loff_t *pos)
2083{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002084 u32 data;
2085
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002086 /* EOF if there's no entry in the mbox */
2087 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2088 return 0;
2089
2090 data = ctx->csa.prob.pu_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002091
2092 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2093}
2094
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002095static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2096 size_t len, loff_t *pos)
2097{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002098 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002099 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002100
2101 if (!access_ok(VERIFY_WRITE, buf, len))
2102 return -EFAULT;
2103
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002104 ret = spu_acquire_saved(ctx);
2105 if (ret)
2106 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002107 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002108 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002109 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002110 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002111
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002112 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002113}
2114
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002115static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002116 .open = spufs_info_open,
2117 .read = spufs_mbox_info_read,
2118 .llseek = generic_file_llseek,
2119};
2120
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002121static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2122 char __user *buf, size_t len, loff_t *pos)
2123{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002124 u32 data;
2125
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002126 /* EOF if there's no entry in the ibox */
2127 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2128 return 0;
2129
2130 data = ctx->csa.priv2.puint_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002131
2132 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2133}
2134
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002135static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2136 size_t len, loff_t *pos)
2137{
2138 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002139 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002140
2141 if (!access_ok(VERIFY_WRITE, buf, len))
2142 return -EFAULT;
2143
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002144 ret = spu_acquire_saved(ctx);
2145 if (ret)
2146 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002147 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002148 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002149 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002150 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002151
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002152 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002153}
2154
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002155static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002156 .open = spufs_info_open,
2157 .read = spufs_ibox_info_read,
2158 .llseek = generic_file_llseek,
2159};
2160
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002161static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2162 char __user *buf, size_t len, loff_t *pos)
2163{
2164 int i, cnt;
2165 u32 data[4];
2166 u32 wbox_stat;
2167
2168 wbox_stat = ctx->csa.prob.mb_stat_R;
2169 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2170 for (i = 0; i < cnt; i++) {
2171 data[i] = ctx->csa.spu_mailbox_data[i];
2172 }
2173
2174 return simple_read_from_buffer(buf, len, pos, &data,
2175 cnt * sizeof(u32));
2176}
2177
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002178static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2179 size_t len, loff_t *pos)
2180{
2181 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002182 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002183
2184 if (!access_ok(VERIFY_WRITE, buf, len))
2185 return -EFAULT;
2186
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002187 ret = spu_acquire_saved(ctx);
2188 if (ret)
2189 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002190 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002191 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002192 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002193 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002194
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002195 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002196}
2197
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002198static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002199 .open = spufs_info_open,
2200 .read = spufs_wbox_info_read,
2201 .llseek = generic_file_llseek,
2202};
2203
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002204static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2205 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002206{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002207 struct spu_dma_info info;
2208 struct mfc_cq_sr *qp, *spuqp;
2209 int i;
2210
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002211 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2212 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2213 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2214 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2215 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2216 for (i = 0; i < 16; i++) {
2217 qp = &info.dma_info_command_data[i];
2218 spuqp = &ctx->csa.priv2.spuq[i];
2219
2220 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2221 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2222 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2223 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2224 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002225
2226 return simple_read_from_buffer(buf, len, pos, &info,
2227 sizeof info);
2228}
2229
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002230static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2231 size_t len, loff_t *pos)
2232{
2233 struct spu_context *ctx = file->private_data;
2234 int ret;
2235
2236 if (!access_ok(VERIFY_WRITE, buf, len))
2237 return -EFAULT;
2238
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002239 ret = spu_acquire_saved(ctx);
2240 if (ret)
2241 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002242 spin_lock(&ctx->csa.register_lock);
2243 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2244 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002245 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002246
2247 return ret;
2248}
2249
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002250static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002251 .open = spufs_info_open,
2252 .read = spufs_dma_info_read,
2253};
2254
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002255static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2256 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002257{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002258 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002259 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002260 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002261 int i;
2262
2263 if (len < ret)
2264 return -EINVAL;
2265
2266 if (!access_ok(VERIFY_WRITE, buf, len))
2267 return -EFAULT;
2268
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002269 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2270 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2271 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2272 for (i = 0; i < 8; i++) {
2273 qp = &info.proxydma_info_command_data[i];
2274 puqp = &ctx->csa.priv2.puq[i];
2275
2276 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2277 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2278 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2279 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2280 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002281
2282 return simple_read_from_buffer(buf, len, pos, &info,
2283 sizeof info);
2284}
2285
2286static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2287 size_t len, loff_t *pos)
2288{
2289 struct spu_context *ctx = file->private_data;
2290 int ret;
2291
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002292 ret = spu_acquire_saved(ctx);
2293 if (ret)
2294 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002295 spin_lock(&ctx->csa.register_lock);
2296 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002297 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002298 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002299
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002300 return ret;
2301}
2302
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002303static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002304 .open = spufs_info_open,
2305 .read = spufs_proxydma_info_read,
2306};
2307
Christoph Hellwig476273a2007-06-29 10:58:01 +10002308static int spufs_show_tid(struct seq_file *s, void *private)
2309{
2310 struct spu_context *ctx = s->private;
2311
2312 seq_printf(s, "%d\n", ctx->tid);
2313 return 0;
2314}
2315
2316static int spufs_tid_open(struct inode *inode, struct file *file)
2317{
2318 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2319}
2320
2321static const struct file_operations spufs_tid_fops = {
2322 .open = spufs_tid_open,
2323 .read = seq_read,
2324 .llseek = seq_lseek,
2325 .release = single_release,
2326};
2327
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002328static const char *ctx_state_names[] = {
2329 "user", "system", "iowait", "loaded"
2330};
2331
2332static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002333 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002334{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002335 struct timespec ts;
2336 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002337
Andre Detsch27ec41d2007-07-20 21:39:33 +02002338 /*
2339 * In general, utilization statistics are updated by the controlling
2340 * thread as the spu context moves through various well defined
2341 * state transitions, but if the context is lazily loaded its
2342 * utilization statistics are not updated as the controlling thread
2343 * is not tightly coupled with the execution of the spu context. We
2344 * calculate and apply the time delta from the last recorded state
2345 * of the spu context.
2346 */
2347 if (ctx->spu && ctx->stats.util_state == state) {
2348 ktime_get_ts(&ts);
2349 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2350 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002351
Andre Detsch27ec41d2007-07-20 21:39:33 +02002352 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002353}
2354
2355static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2356{
2357 unsigned long long slb_flts = ctx->stats.slb_flt;
2358
2359 if (ctx->state == SPU_STATE_RUNNABLE) {
2360 slb_flts += (ctx->spu->stats.slb_flt -
2361 ctx->stats.slb_flt_base);
2362 }
2363
2364 return slb_flts;
2365}
2366
2367static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2368{
2369 unsigned long long class2_intrs = ctx->stats.class2_intr;
2370
2371 if (ctx->state == SPU_STATE_RUNNABLE) {
2372 class2_intrs += (ctx->spu->stats.class2_intr -
2373 ctx->stats.class2_intr_base);
2374 }
2375
2376 return class2_intrs;
2377}
2378
2379
2380static int spufs_show_stat(struct seq_file *s, void *private)
2381{
2382 struct spu_context *ctx = s->private;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002383 int ret;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002384
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002385 ret = spu_acquire(ctx);
2386 if (ret)
2387 return ret;
2388
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002389 seq_printf(s, "%s %llu %llu %llu %llu "
2390 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002391 ctx_state_names[ctx->stats.util_state],
2392 spufs_acct_time(ctx, SPU_UTIL_USER),
2393 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2394 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2395 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002396 ctx->stats.vol_ctx_switch,
2397 ctx->stats.invol_ctx_switch,
2398 spufs_slb_flts(ctx),
2399 ctx->stats.hash_flt,
2400 ctx->stats.min_flt,
2401 ctx->stats.maj_flt,
2402 spufs_class2_intrs(ctx),
2403 ctx->stats.libassist);
2404 spu_release(ctx);
2405 return 0;
2406}
2407
2408static int spufs_stat_open(struct inode *inode, struct file *file)
2409{
2410 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2411}
2412
2413static const struct file_operations spufs_stat_fops = {
2414 .open = spufs_stat_open,
2415 .read = seq_read,
2416 .llseek = seq_lseek,
2417 .release = single_release,
2418};
2419
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002420static inline int spufs_switch_log_used(struct spu_context *ctx)
2421{
2422 return (ctx->switch_log->head - ctx->switch_log->tail) %
2423 SWITCH_LOG_BUFSIZE;
2424}
2425
2426static inline int spufs_switch_log_avail(struct spu_context *ctx)
2427{
2428 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2429}
2430
2431static int spufs_switch_log_open(struct inode *inode, struct file *file)
2432{
2433 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002434 int rc;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002435
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002436 rc = spu_acquire(ctx);
2437 if (rc)
2438 return rc;
2439
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002440 if (ctx->switch_log) {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002441 rc = -EBUSY;
2442 goto out;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002443 }
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002444
Jeremy Kerr837ef882008-10-17 12:02:31 +11002445 ctx->switch_log = kmalloc(sizeof(struct switch_log) +
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002446 SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
2447 GFP_KERNEL);
2448
2449 if (!ctx->switch_log) {
2450 rc = -ENOMEM;
2451 goto out;
2452 }
2453
Jeremy Kerr837ef882008-10-17 12:02:31 +11002454 ctx->switch_log->head = ctx->switch_log->tail = 0;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002455 init_waitqueue_head(&ctx->switch_log->wait);
2456 rc = 0;
2457
2458out:
2459 spu_release(ctx);
2460 return rc;
2461}
2462
2463static int spufs_switch_log_release(struct inode *inode, struct file *file)
2464{
2465 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2466 int rc;
2467
2468 rc = spu_acquire(ctx);
2469 if (rc)
2470 return rc;
2471
2472 kfree(ctx->switch_log);
2473 ctx->switch_log = NULL;
2474 spu_release(ctx);
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002475
2476 return 0;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002477}
2478
2479static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2480{
2481 struct switch_log_entry *p;
2482
2483 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2484
2485 return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
2486 (unsigned int) p->tstamp.tv_sec,
2487 (unsigned int) p->tstamp.tv_nsec,
2488 p->spu_id,
2489 (unsigned int) p->type,
2490 (unsigned int) p->val,
2491 (unsigned long long) p->timebase);
2492}
2493
2494static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2495 size_t len, loff_t *ppos)
2496{
2497 struct inode *inode = file->f_path.dentry->d_inode;
2498 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2499 int error = 0, cnt = 0;
2500
2501 if (!buf || len < 0)
2502 return -EINVAL;
2503
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002504 error = spu_acquire(ctx);
2505 if (error)
2506 return error;
2507
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002508 while (cnt < len) {
2509 char tbuf[128];
2510 int width;
2511
Jeremy Kerr14f693e2008-10-16 10:51:46 +11002512 if (spufs_switch_log_used(ctx) == 0) {
2513 if (cnt > 0) {
2514 /* If there's data ready to go, we can
2515 * just return straight away */
2516 break;
2517
2518 } else if (file->f_flags & O_NONBLOCK) {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002519 error = -EAGAIN;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002520 break;
Jeremy Kerr14f693e2008-10-16 10:51:46 +11002521
2522 } else {
2523 /* spufs_wait will drop the mutex and
2524 * re-acquire, but since we're in read(), the
2525 * file cannot be _released (and so
2526 * ctx->switch_log is stable).
2527 */
2528 error = spufs_wait(ctx->switch_log->wait,
2529 spufs_switch_log_used(ctx) > 0);
2530
2531 /* On error, spufs_wait returns without the
2532 * state mutex held */
2533 if (error)
2534 return error;
2535
2536 /* We may have had entries read from underneath
2537 * us while we dropped the mutex in spufs_wait,
2538 * so re-check */
2539 if (spufs_switch_log_used(ctx) == 0)
2540 continue;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002541 }
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002542 }
2543
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002544 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002545 if (width < len)
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002546 ctx->switch_log->tail =
2547 (ctx->switch_log->tail + 1) %
2548 SWITCH_LOG_BUFSIZE;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002549 else
2550 /* If the record is greater than space available return
2551 * partial buffer (so far) */
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002552 break;
2553
2554 error = copy_to_user(buf + cnt, tbuf, width);
2555 if (error)
2556 break;
2557 cnt += width;
2558 }
2559
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002560 spu_release(ctx);
2561
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002562 return cnt == 0 ? error : cnt;
2563}
2564
2565static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
2566{
2567 struct inode *inode = file->f_path.dentry->d_inode;
2568 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2569 unsigned int mask = 0;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002570 int rc;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002571
2572 poll_wait(file, &ctx->switch_log->wait, wait);
2573
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002574 rc = spu_acquire(ctx);
2575 if (rc)
2576 return rc;
2577
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002578 if (spufs_switch_log_used(ctx) > 0)
2579 mask |= POLLIN;
2580
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002581 spu_release(ctx);
2582
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002583 return mask;
2584}
2585
2586static const struct file_operations spufs_switch_log_fops = {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002587 .owner = THIS_MODULE,
2588 .open = spufs_switch_log_open,
2589 .read = spufs_switch_log_read,
2590 .poll = spufs_switch_log_poll,
2591 .release = spufs_switch_log_release,
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002592};
2593
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002594/**
2595 * Log a context switch event to a switch log reader.
2596 *
2597 * Must be called with ctx->state_mutex held.
2598 */
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002599void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2600 u32 type, u32 val)
2601{
2602 if (!ctx->switch_log)
2603 return;
2604
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002605 if (spufs_switch_log_avail(ctx) > 1) {
2606 struct switch_log_entry *p;
2607
2608 p = ctx->switch_log->log + ctx->switch_log->head;
2609 ktime_get_ts(&p->tstamp);
2610 p->timebase = get_tb();
2611 p->spu_id = spu ? spu->number : -1;
2612 p->type = type;
2613 p->val = val;
2614
2615 ctx->switch_log->head =
2616 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2617 }
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002618
2619 wake_up(&ctx->switch_log->wait);
2620}
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002621
Luke Browning46deed62008-06-16 11:36:43 +10002622static int spufs_show_ctx(struct seq_file *s, void *private)
2623{
2624 struct spu_context *ctx = s->private;
2625 u64 mfc_control_RW;
2626
2627 mutex_lock(&ctx->state_mutex);
2628 if (ctx->spu) {
2629 struct spu *spu = ctx->spu;
2630 struct spu_priv2 __iomem *priv2 = spu->priv2;
2631
2632 spin_lock_irq(&spu->register_lock);
2633 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2634 spin_unlock_irq(&spu->register_lock);
2635 } else {
2636 struct spu_state *csa = &ctx->csa;
2637
2638 mfc_control_RW = csa->priv2.mfc_control_RW;
2639 }
2640
2641 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
2642 " %c %lx %lx %lx %lx %x %x\n",
2643 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2644 ctx->flags,
2645 ctx->sched_flags,
2646 ctx->prio,
2647 ctx->time_slice,
2648 ctx->spu ? ctx->spu->number : -1,
2649 !list_empty(&ctx->rq) ? 'q' : ' ',
2650 ctx->csa.class_0_pending,
2651 ctx->csa.class_0_dar,
2652 ctx->csa.class_1_dsisr,
2653 mfc_control_RW,
2654 ctx->ops->runcntl_read(ctx),
2655 ctx->ops->status_read(ctx));
2656
2657 mutex_unlock(&ctx->state_mutex);
2658
2659 return 0;
2660}
2661
2662static int spufs_ctx_open(struct inode *inode, struct file *file)
2663{
2664 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2665}
2666
2667static const struct file_operations spufs_ctx_fops = {
2668 .open = spufs_ctx_open,
2669 .read = seq_read,
2670 .llseek = seq_lseek,
2671 .release = single_release,
2672};
2673
Jeremy Kerr23d893f2008-06-30 12:17:28 +10002674struct spufs_tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002675 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002676 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2677 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002678 { "mbox", &spufs_mbox_fops, 0444, },
2679 { "ibox", &spufs_ibox_fops, 0444, },
2680 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002681 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2682 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2683 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002684 { "signal1", &spufs_signal1_fops, 0666, },
2685 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002686 { "signal1_type", &spufs_signal1_type, 0666, },
2687 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002688 { "cntl", &spufs_cntl_fops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002689 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002690 { "lslr", &spufs_lslr_ops, 0444, },
2691 { "mfc", &spufs_mfc_fops, 0666, },
2692 { "mss", &spufs_mss_fops, 0666, },
2693 { "npc", &spufs_npc_ops, 0666, },
2694 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002695 { "decr", &spufs_decr_ops, 0666, },
2696 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002697 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002698 { "event_status", &spufs_event_status_ops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002699 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002700 { "phys-id", &spufs_id_ops, 0666, },
2701 { "object-id", &spufs_object_id_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002702 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
2703 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
2704 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
2705 { "dma_info", &spufs_dma_info_fops, 0444,
2706 sizeof(struct spu_dma_info), },
2707 { "proxydma_info", &spufs_proxydma_info_fops, 0444,
2708 sizeof(struct spu_proxydma_info)},
Christoph Hellwig476273a2007-06-29 10:58:01 +10002709 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002710 { "stat", &spufs_stat_fops, 0444, },
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002711 { "switch_log", &spufs_switch_log_fops, 0444 },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002712 {},
2713};
Mark Nutter5737edd2006-10-24 18:31:16 +02002714
Jeremy Kerr23d893f2008-06-30 12:17:28 +10002715struct spufs_tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002716 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002717 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002718 { "mbox", &spufs_mbox_fops, 0444, },
2719 { "ibox", &spufs_ibox_fops, 0444, },
2720 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002721 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2722 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2723 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002724 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2725 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002726 { "signal1_type", &spufs_signal1_type, 0666, },
2727 { "signal2_type", &spufs_signal2_type, 0666, },
2728 { "mss", &spufs_mss_fops, 0666, },
2729 { "mfc", &spufs_mfc_fops, 0666, },
2730 { "cntl", &spufs_cntl_fops, 0666, },
2731 { "npc", &spufs_npc_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002732 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002733 { "phys-id", &spufs_id_ops, 0666, },
2734 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002735 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002736 { "stat", &spufs_stat_fops, 0444, },
Jeremy Kerr2c3e4782008-07-03 11:42:20 +10002737 {},
2738};
2739
2740struct spufs_tree_descr spufs_dir_debug_contents[] = {
Luke Browning46deed62008-06-16 11:36:43 +10002741 { ".ctx", &spufs_ctx_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002742 {},
2743};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002744
2745struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002746 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2747 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002748 { "lslr", NULL, spufs_lslr_get, 19 },
2749 { "decr", NULL, spufs_decr_get, 19 },
2750 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002751 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2752 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002753 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002754 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002755 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2756 { "event_mask", NULL, spufs_event_mask_get, 19 },
2757 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002758 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2759 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2760 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2761 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2762 { "proxydma_info", __spufs_proxydma_info_read,
2763 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002764 { "object-id", NULL, spufs_object_id_get, 19 },
2765 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002766 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002767};