blob: fee645b580cc58de3a17631aebda9c45f5103194 [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
2 * SPU file system -- file contents
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
Arnd Bergmanna33a7d72006-03-23 00:00:11 +010023#undef DEBUG
24
Arnd Bergmann67207b92005-11-15 15:53:48 -050025#include <linux/fs.h>
26#include <linux/ioctl.h>
27#include <linux/module.h>
Arnd Bergmannd88cfff2005-12-05 22:52:22 -050028#include <linux/pagemap.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050029#include <linux/poll.h>
Arnd Bergmann51104592005-12-05 22:52:25 -050030#include <linux/ptrace.h>
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +100031#include <linux/seq_file.h>
Christoph Hellwig038200c2008-01-11 15:03:26 +110032#include <linux/marker.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050033
34#include <asm/io.h>
FUJITA Tomonoridfe1e092008-05-13 19:07:42 +100035#include <asm/time.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050036#include <asm/spu.h>
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +010037#include <asm/spu_info.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050038#include <asm/uaccess.h>
39
40#include "spufs.h"
41
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +020042#define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
43
Christoph Hellwig197b1a82007-12-20 16:39:59 +090044/* Simple attribute files */
45struct spufs_attr {
46 int (*get)(void *, u64 *);
47 int (*set)(void *, u64);
48 char get_buf[24]; /* enough to store a u64 and "\n\0" */
49 char set_buf[24];
50 void *data;
51 const char *fmt; /* format for read operation */
52 struct mutex mutex; /* protects access to these buffers */
53};
54
55static int spufs_attr_open(struct inode *inode, struct file *file,
56 int (*get)(void *, u64 *), int (*set)(void *, u64),
57 const char *fmt)
58{
59 struct spufs_attr *attr;
60
61 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
62 if (!attr)
63 return -ENOMEM;
64
65 attr->get = get;
66 attr->set = set;
67 attr->data = inode->i_private;
68 attr->fmt = fmt;
69 mutex_init(&attr->mutex);
70 file->private_data = attr;
71
72 return nonseekable_open(inode, file);
73}
74
75static int spufs_attr_release(struct inode *inode, struct file *file)
76{
77 kfree(file->private_data);
78 return 0;
79}
80
81static ssize_t spufs_attr_read(struct file *file, char __user *buf,
82 size_t len, loff_t *ppos)
83{
84 struct spufs_attr *attr;
85 size_t size;
86 ssize_t ret;
87
88 attr = file->private_data;
89 if (!attr->get)
90 return -EACCES;
91
92 ret = mutex_lock_interruptible(&attr->mutex);
93 if (ret)
94 return ret;
95
96 if (*ppos) { /* continued read */
97 size = strlen(attr->get_buf);
98 } else { /* first read */
99 u64 val;
100 ret = attr->get(attr->data, &val);
101 if (ret)
102 goto out;
103
104 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
105 attr->fmt, (unsigned long long)val);
106 }
107
108 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
109out:
110 mutex_unlock(&attr->mutex);
111 return ret;
112}
113
114static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
115 size_t len, loff_t *ppos)
116{
117 struct spufs_attr *attr;
118 u64 val;
119 size_t size;
120 ssize_t ret;
121
122 attr = file->private_data;
123 if (!attr->set)
124 return -EACCES;
125
126 ret = mutex_lock_interruptible(&attr->mutex);
127 if (ret)
128 return ret;
129
130 ret = -EFAULT;
131 size = min(sizeof(attr->set_buf) - 1, len);
132 if (copy_from_user(attr->set_buf, buf, size))
133 goto out;
134
135 ret = len; /* claim we got the whole input */
136 attr->set_buf[size] = '\0';
137 val = simple_strtol(attr->set_buf, NULL, 0);
138 attr->set(attr->data, val);
139out:
140 mutex_unlock(&attr->mutex);
141 return ret;
142}
143
144#define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
145static int __fops ## _open(struct inode *inode, struct file *file) \
146{ \
147 __simple_attr_check_format(__fmt, 0ull); \
148 return spufs_attr_open(inode, file, __get, __set, __fmt); \
149} \
150static struct file_operations __fops = { \
151 .owner = THIS_MODULE, \
152 .open = __fops ## _open, \
153 .release = spufs_attr_release, \
154 .read = spufs_attr_read, \
155 .write = spufs_attr_write, \
156};
157
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +1000158
Arnd Bergmann67207b92005-11-15 15:53:48 -0500159static int
160spufs_mem_open(struct inode *inode, struct file *file)
161{
162 struct spufs_inode_info *i = SPUFS_I(inode);
Mark Nutter6df10a82006-03-23 00:00:12 +0100163 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200164
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000165 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100166 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200167 if (!i->i_openers++)
168 ctx->local_store = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000169 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200170 return 0;
171}
172
173static int
174spufs_mem_release(struct inode *inode, struct file *file)
175{
176 struct spufs_inode_info *i = SPUFS_I(inode);
177 struct spu_context *ctx = i->i_ctx;
178
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000179 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200180 if (!--i->i_openers)
181 ctx->local_store = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000182 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500183 return 0;
184}
185
186static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100187__spufs_mem_read(struct spu_context *ctx, char __user *buffer,
188 size_t size, loff_t *pos)
189{
190 char *local_store = ctx->ops->get_ls(ctx);
191 return simple_read_from_buffer(buffer, size, pos, local_store,
192 LS_SIZE);
193}
194
195static ssize_t
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196spufs_mem_read(struct file *file, char __user *buffer,
197 size_t size, loff_t *pos)
198{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100199 struct spu_context *ctx = file->private_data;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100200 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500201
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900202 ret = spu_acquire(ctx);
203 if (ret)
204 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100205 ret = __spufs_mem_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500206 spu_release(ctx);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900207
Arnd Bergmann67207b92005-11-15 15:53:48 -0500208 return ret;
209}
210
211static ssize_t
212spufs_mem_write(struct file *file, const char __user *buffer,
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100213 size_t size, loff_t *ppos)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214{
215 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500216 char *local_store;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100217 loff_t pos = *ppos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500218 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500219
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100220 if (pos < 0)
221 return -EINVAL;
222 if (pos > LS_SIZE)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500223 return -EFBIG;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100224 if (size > LS_SIZE - pos)
225 size = LS_SIZE - pos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500226
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900227 ret = spu_acquire(ctx);
228 if (ret)
229 return ret;
230
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500231 local_store = ctx->ops->get_ls(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100232 ret = copy_from_user(local_store + pos, buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500233 spu_release(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100234
235 if (ret)
236 return -EFAULT;
237 *ppos = pos + size;
238 return size;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500239}
240
Nick Pigginb1e22702008-06-10 09:26:08 +1000241static int
242spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500243{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000244 struct spu_context *ctx = vma->vm_file->private_data;
Nick Pigginb1e22702008-06-10 09:26:08 +1000245 unsigned long address = (unsigned long)vmf->virtual_address;
246 unsigned long pfn, offset;
247
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000248#ifdef CONFIG_SPU_FS_64K_LS
249 struct spu_state *csa = &ctx->csa;
250 int psize;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100251
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000252 /* Check what page size we are using */
253 psize = get_slice_psize(vma->vm_mm, address);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500254
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000255 /* Some sanity checking */
256 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
257
258 /* Wow, 64K, cool, we need to align the address though */
259 if (csa->use_big_pages) {
260 BUG_ON(vma->vm_start & 0xffff);
261 address &= ~0xfffful;
262 }
263#endif /* CONFIG_SPU_FS_64K_LS */
264
Nick Pigginb1e22702008-06-10 09:26:08 +1000265 offset = vmf->pgoff << PAGE_SHIFT;
Masato Noguchi128b8542007-02-13 21:54:30 +0100266 if (offset >= LS_SIZE)
Nick Pigginb1e22702008-06-10 09:26:08 +1000267 return VM_FAULT_SIGBUS;
Masato Noguchi128b8542007-02-13 21:54:30 +0100268
Nick Pigginb1e22702008-06-10 09:26:08 +1000269 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
270 address, offset);
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000271
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900272 if (spu_acquire(ctx))
Nick Pigginb1e22702008-06-10 09:26:08 +1000273 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500274
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200275 if (ctx->state == SPU_STATE_SAVED) {
276 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Arnd Bergmann932f535d2006-11-20 18:45:06 +0100277 & ~_PAGE_NO_CACHE);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100278 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200279 } else {
280 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100281 | _PAGE_NO_CACHE);
282 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200283 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100284 vm_insert_pfn(vma, address, pfn);
285
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500286 spu_release(ctx);
287
Nick Pigginb1e22702008-06-10 09:26:08 +1000288 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500289}
290
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100291
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500292static struct vm_operations_struct spufs_mem_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000293 .fault = spufs_mem_mmap_fault,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500294};
295
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000296static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500297{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000298#ifdef CONFIG_SPU_FS_64K_LS
299 struct spu_context *ctx = file->private_data;
300 struct spu_state *csa = &ctx->csa;
301
302 /* Sanity check VMA alignment */
303 if (csa->use_big_pages) {
304 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
305 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
306 vma->vm_pgoff);
307 if (vma->vm_start & 0xffff)
308 return -EINVAL;
309 if (vma->vm_pgoff & 0xf)
310 return -EINVAL;
311 }
312#endif /* CONFIG_SPU_FS_64K_LS */
313
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500314 if (!(vma->vm_flags & VM_SHARED))
315 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500316
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100317 vma->vm_flags |= VM_IO | VM_PFNMAP;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500318 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
319 | _PAGE_NO_CACHE);
320
321 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500322 return 0;
323}
324
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000325#ifdef CONFIG_SPU_FS_64K_LS
Sebastian Siewior12388192007-09-19 14:38:12 +1000326static unsigned long spufs_get_unmapped_area(struct file *file,
327 unsigned long addr, unsigned long len, unsigned long pgoff,
328 unsigned long flags)
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000329{
330 struct spu_context *ctx = file->private_data;
331 struct spu_state *csa = &ctx->csa;
332
333 /* If not using big pages, fallback to normal MM g_u_a */
334 if (!csa->use_big_pages)
335 return current->mm->get_unmapped_area(file, addr, len,
336 pgoff, flags);
337
338 /* Else, try to obtain a 64K pages slice */
339 return slice_get_unmapped_area(addr, len, flags,
340 MMU_PAGE_64K, 1, 0);
341}
342#endif /* CONFIG_SPU_FS_64K_LS */
343
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800344static const struct file_operations spufs_mem_fops = {
Jeremy Kerr70225432007-06-29 10:58:00 +1000345 .open = spufs_mem_open,
346 .release = spufs_mem_release,
347 .read = spufs_mem_read,
348 .write = spufs_mem_write,
349 .llseek = generic_file_llseek,
350 .mmap = spufs_mem_mmap,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000351#ifdef CONFIG_SPU_FS_64K_LS
352 .get_unmapped_area = spufs_get_unmapped_area,
353#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500354};
355
Nick Pigginb1e22702008-06-10 09:26:08 +1000356static int spufs_ps_fault(struct vm_area_struct *vma,
357 struct vm_fault *vmf,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100358 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200359 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100360{
Mark Nutter6df10a82006-03-23 00:00:12 +0100361 struct spu_context *ctx = vma->vm_file->private_data;
Nick Pigginb1e22702008-06-10 09:26:08 +1000362 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100363 int ret = 0;
Mark Nutter6df10a82006-03-23 00:00:12 +0100364
Nick Pigginb1e22702008-06-10 09:26:08 +1000365 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100366
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200367 if (offset >= ps_size)
Nick Pigginb1e22702008-06-10 09:26:08 +1000368 return VM_FAULT_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100369
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900370 /*
Jeremy Kerrd5883132008-02-26 13:31:42 +1100371 * Because we release the mmap_sem, the context may be destroyed while
372 * we're in spu_wait. Grab an extra reference so it isn't destroyed
373 * in the meantime.
374 */
375 get_spu_context(ctx);
376
377 /*
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900378 * We have to wait for context to be loaded before we have
379 * pages to hand out to the user, but we don't want to wait
380 * with the mmap_sem held.
381 * It is possible to drop the mmap_sem here, but then we need
Nick Pigginb1e22702008-06-10 09:26:08 +1000382 * to return VM_FAULT_NOPAGE because the mappings may have
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900383 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100384 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900385 if (spu_acquire(ctx))
Jeremy Kerrd5883132008-02-26 13:31:42 +1100386 goto refault;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900387
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900388 if (ctx->state == SPU_STATE_SAVED) {
389 up_read(&current->mm->mmap_sem);
Nick Pigginb1e22702008-06-10 09:26:08 +1000390 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100391 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Nick Pigginb1e22702008-06-10 09:26:08 +1000392 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900393 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900394 } else {
395 area = ctx->spu->problem_phys + ps_offs;
Nick Pigginb1e22702008-06-10 09:26:08 +1000396 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
397 (area + offset) >> PAGE_SHIFT);
398 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900399 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100400
Christoph Hellwigeebead52008-02-08 15:50:41 +1100401 if (!ret)
402 spu_release(ctx);
Jeremy Kerrd5883132008-02-26 13:31:42 +1100403
404refault:
405 put_spu_context(ctx);
Nick Pigginb1e22702008-06-10 09:26:08 +1000406 return VM_FAULT_NOPAGE;
Mark Nutter6df10a82006-03-23 00:00:12 +0100407}
408
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200409#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +1000410static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
411 struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +0100412{
Jeremy Kerr87ff6092008-07-01 10:22:50 +1000413 return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +0100414}
415
416static struct vm_operations_struct spufs_cntl_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000417 .fault = spufs_cntl_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +0100418};
419
420/*
421 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100422 */
423static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
424{
425 if (!(vma->vm_flags & VM_SHARED))
426 return -EINVAL;
427
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100428 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100429 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200430 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100431
432 vma->vm_ops = &spufs_cntl_mmap_vmops;
433 return 0;
434}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200435#else /* SPUFS_MMAP_4K */
436#define spufs_cntl_mmap NULL
437#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100438
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900439static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200440{
441 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900442 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200443
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900444 ret = spu_acquire(ctx);
445 if (ret)
446 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900447 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200448 spu_release(ctx);
449
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900450 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200451}
452
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900453static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200454{
455 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900456 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200457
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900458 ret = spu_acquire(ctx);
459 if (ret)
460 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200461 ctx->ops->runcntl_write(ctx, val);
462 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900463
464 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200465}
466
Mark Nutter6df10a82006-03-23 00:00:12 +0100467static int spufs_cntl_open(struct inode *inode, struct file *file)
468{
469 struct spufs_inode_info *i = SPUFS_I(inode);
470 struct spu_context *ctx = i->i_ctx;
471
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000472 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100473 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200474 if (!i->i_openers++)
475 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000476 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800477 return simple_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200478 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100479}
480
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200481static int
482spufs_cntl_release(struct inode *inode, struct file *file)
483{
484 struct spufs_inode_info *i = SPUFS_I(inode);
485 struct spu_context *ctx = i->i_ctx;
486
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800487 simple_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200488
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000489 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200490 if (!--i->i_openers)
491 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000492 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200493 return 0;
494}
495
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800496static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100497 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200498 .release = spufs_cntl_release,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800499 .read = simple_attr_read,
500 .write = simple_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100501 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100502};
503
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500504static int
505spufs_regs_open(struct inode *inode, struct file *file)
506{
507 struct spufs_inode_info *i = SPUFS_I(inode);
508 file->private_data = i->i_ctx;
509 return 0;
510}
511
512static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100513__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
514 size_t size, loff_t *pos)
515{
516 struct spu_lscsa *lscsa = ctx->csa.lscsa;
517 return simple_read_from_buffer(buffer, size, pos,
518 lscsa->gprs, sizeof lscsa->gprs);
519}
520
521static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500522spufs_regs_read(struct file *file, char __user *buffer,
523 size_t size, loff_t *pos)
524{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500525 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100526 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500527
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900528 ret = spu_acquire_saved(ctx);
529 if (ret)
530 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100531 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200532 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500533 return ret;
534}
535
536static ssize_t
537spufs_regs_write(struct file *file, const char __user *buffer,
538 size_t size, loff_t *pos)
539{
540 struct spu_context *ctx = file->private_data;
541 struct spu_lscsa *lscsa = ctx->csa.lscsa;
542 int ret;
543
544 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
545 if (size <= 0)
546 return -EFBIG;
547 *pos += size;
548
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900549 ret = spu_acquire_saved(ctx);
550 if (ret)
551 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500552
553 ret = copy_from_user(lscsa->gprs + *pos - size,
554 buffer, size) ? -EFAULT : size;
555
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200556 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500557 return ret;
558}
559
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800560static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500561 .open = spufs_regs_open,
562 .read = spufs_regs_read,
563 .write = spufs_regs_write,
564 .llseek = generic_file_llseek,
565};
566
567static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100568__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
569 size_t size, loff_t * pos)
570{
571 struct spu_lscsa *lscsa = ctx->csa.lscsa;
572 return simple_read_from_buffer(buffer, size, pos,
573 &lscsa->fpcr, sizeof(lscsa->fpcr));
574}
575
576static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500577spufs_fpcr_read(struct file *file, char __user * buffer,
578 size_t size, loff_t * pos)
579{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500580 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100581 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500582
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900583 ret = spu_acquire_saved(ctx);
584 if (ret)
585 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100586 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200587 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500588 return ret;
589}
590
591static ssize_t
592spufs_fpcr_write(struct file *file, const char __user * buffer,
593 size_t size, loff_t * pos)
594{
595 struct spu_context *ctx = file->private_data;
596 struct spu_lscsa *lscsa = ctx->csa.lscsa;
597 int ret;
598
599 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
600 if (size <= 0)
601 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900602
603 ret = spu_acquire_saved(ctx);
604 if (ret)
605 return ret;
606
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500607 *pos += size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500608 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
609 buffer, size) ? -EFAULT : size;
610
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200611 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500612 return ret;
613}
614
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800615static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500616 .open = spufs_regs_open,
617 .read = spufs_fpcr_read,
618 .write = spufs_fpcr_write,
619 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500620};
621
622/* generic open function for all pipe-like files */
623static int spufs_pipe_open(struct inode *inode, struct file *file)
624{
625 struct spufs_inode_info *i = SPUFS_I(inode);
626 file->private_data = i->i_ctx;
627
628 return nonseekable_open(inode, file);
629}
630
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200631/*
632 * Read as many bytes from the mailbox as possible, until
633 * one of the conditions becomes true:
634 *
635 * - no more data available in the mailbox
636 * - end of the user provided buffer
637 * - end of the mapped area
638 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500639static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
640 size_t len, loff_t *pos)
641{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500642 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200643 u32 mbox_data, __user *udata;
644 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500645
646 if (len < 4)
647 return -EINVAL;
648
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200649 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500650 return -EFAULT;
651
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200652 udata = (void __user *)buf;
653
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900654 count = spu_acquire(ctx);
655 if (count)
656 return count;
657
Arnd Bergmann274cef52006-10-24 18:01:42 +0200658 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200659 int ret;
660 ret = ctx->ops->mbox_read(ctx, &mbox_data);
661 if (ret == 0)
662 break;
663
664 /*
665 * at the end of the mapped area, we can fault
666 * but still need to return the data we have
667 * read successfully so far.
668 */
669 ret = __put_user(mbox_data, udata);
670 if (ret) {
671 if (!count)
672 count = -EFAULT;
673 break;
674 }
675 }
676 spu_release(ctx);
677
678 if (!count)
679 count = -EAGAIN;
680
681 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500682}
683
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800684static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500685 .open = spufs_pipe_open,
686 .read = spufs_mbox_read,
687};
688
689static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
690 size_t len, loff_t *pos)
691{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500692 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900693 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500694 u32 mbox_stat;
695
696 if (len < 4)
697 return -EINVAL;
698
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900699 ret = spu_acquire(ctx);
700 if (ret)
701 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500702
703 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
704
705 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500706
707 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
708 return -EFAULT;
709
710 return 4;
711}
712
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800713static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500714 .open = spufs_pipe_open,
715 .read = spufs_mbox_stat_read,
716};
717
718/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500719size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500720{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500721 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500722}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500723
724static int spufs_ibox_fasync(int fd, struct file *file, int on)
725{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500726 struct spu_context *ctx = file->private_data;
727
728 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
729}
730
731/* interrupt-level ibox callback function. */
732void spufs_ibox_callback(struct spu *spu)
733{
734 struct spu_context *ctx = spu->ctx;
735
Luke Browninge65c2f62007-12-20 16:39:59 +0900736 if (!ctx)
737 return;
738
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500739 wake_up_all(&ctx->ibox_wq);
740 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500741}
742
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200743/*
744 * Read as many bytes from the interrupt mailbox as possible, until
745 * one of the conditions becomes true:
746 *
747 * - no more data available in the mailbox
748 * - end of the user provided buffer
749 * - end of the mapped area
750 *
751 * If the file is opened without O_NONBLOCK, we wait here until
752 * any data is available, but return when we have been able to
753 * read something.
754 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500755static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
756 size_t len, loff_t *pos)
757{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500758 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200759 u32 ibox_data, __user *udata;
760 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500761
762 if (len < 4)
763 return -EINVAL;
764
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200765 if (!access_ok(VERIFY_WRITE, buf, len))
766 return -EFAULT;
767
768 udata = (void __user *)buf;
769
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900770 count = spu_acquire(ctx);
771 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100772 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500773
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200774 /* wait only for the first element */
775 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500776 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100777 if (!spu_ibox_read(ctx, &ibox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200778 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100779 goto out_unlock;
780 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500781 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200782 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100783 if (count)
784 goto out;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200785 }
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200786
787 /* if we can't write at all, return -EFAULT */
788 count = __put_user(ibox_data, udata);
789 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100790 goto out_unlock;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200791
792 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
793 int ret;
794 ret = ctx->ops->ibox_read(ctx, &ibox_data);
795 if (ret == 0)
796 break;
797 /*
798 * at the end of the mapped area, we can fault
799 * but still need to return the data we have
800 * read successfully so far.
801 */
802 ret = __put_user(ibox_data, udata);
803 if (ret)
804 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500805 }
806
Christoph Hellwigeebead52008-02-08 15:50:41 +1100807out_unlock:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500808 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100809out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200810 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500811}
812
813static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
814{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500815 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500816 unsigned int mask;
817
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500818 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500819
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900820 /*
821 * For now keep this uninterruptible and also ignore the rule
822 * that poll should not sleep. Will be fixed later.
823 */
824 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500825 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
826 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500827
828 return mask;
829}
830
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800831static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500832 .open = spufs_pipe_open,
833 .read = spufs_ibox_read,
834 .poll = spufs_ibox_poll,
835 .fasync = spufs_ibox_fasync,
836};
837
838static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
839 size_t len, loff_t *pos)
840{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500841 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900842 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500843 u32 ibox_stat;
844
845 if (len < 4)
846 return -EINVAL;
847
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900848 ret = spu_acquire(ctx);
849 if (ret)
850 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500851 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
852 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500853
854 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
855 return -EFAULT;
856
857 return 4;
858}
859
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800860static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500861 .open = spufs_pipe_open,
862 .read = spufs_ibox_stat_read,
863};
864
865/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500866size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500867{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500868 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500869}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500870
871static int spufs_wbox_fasync(int fd, struct file *file, int on)
872{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500873 struct spu_context *ctx = file->private_data;
874 int ret;
875
876 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
877
878 return ret;
879}
880
881/* interrupt-level wbox callback function. */
882void spufs_wbox_callback(struct spu *spu)
883{
884 struct spu_context *ctx = spu->ctx;
885
Luke Browninge65c2f62007-12-20 16:39:59 +0900886 if (!ctx)
887 return;
888
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500889 wake_up_all(&ctx->wbox_wq);
890 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500891}
892
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200893/*
894 * Write as many bytes to the interrupt mailbox as possible, until
895 * one of the conditions becomes true:
896 *
897 * - the mailbox is full
898 * - end of the user provided buffer
899 * - end of the mapped area
900 *
901 * If the file is opened without O_NONBLOCK, we wait here until
902 * space is availabyl, but return when we have been able to
903 * write something.
904 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500905static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
906 size_t len, loff_t *pos)
907{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500908 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200909 u32 wbox_data, __user *udata;
910 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500911
912 if (len < 4)
913 return -EINVAL;
914
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200915 udata = (void __user *)buf;
916 if (!access_ok(VERIFY_READ, buf, len))
917 return -EFAULT;
918
919 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500920 return -EFAULT;
921
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900922 count = spu_acquire(ctx);
923 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100924 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500925
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200926 /*
927 * make sure we can at least write one element, by waiting
928 * in case of !O_NONBLOCK
929 */
930 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500931 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100932 if (!spu_wbox_write(ctx, wbox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200933 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100934 goto out_unlock;
935 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500936 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200937 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100938 if (count)
939 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500940 }
941
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500942
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200943 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200944 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
945 int ret;
946 ret = __get_user(wbox_data, udata);
947 if (ret)
948 break;
949
950 ret = spu_wbox_write(ctx, wbox_data);
951 if (ret == 0)
952 break;
953 }
954
Christoph Hellwigeebead52008-02-08 15:50:41 +1100955out_unlock:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200956 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100957out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200958 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500959}
960
961static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
962{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500963 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500964 unsigned int mask;
965
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500966 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500967
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900968 /*
969 * For now keep this uninterruptible and also ignore the rule
970 * that poll should not sleep. Will be fixed later.
971 */
972 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500973 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
974 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500975
976 return mask;
977}
978
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800979static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500980 .open = spufs_pipe_open,
981 .write = spufs_wbox_write,
982 .poll = spufs_wbox_poll,
983 .fasync = spufs_wbox_fasync,
984};
985
986static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
987 size_t len, loff_t *pos)
988{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500989 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900990 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500991 u32 wbox_stat;
992
993 if (len < 4)
994 return -EINVAL;
995
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900996 ret = spu_acquire(ctx);
997 if (ret)
998 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500999 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
1000 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001001
1002 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
1003 return -EFAULT;
1004
1005 return 4;
1006}
1007
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001008static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001009 .open = spufs_pipe_open,
1010 .read = spufs_wbox_stat_read,
1011};
1012
Mark Nutter6df10a82006-03-23 00:00:12 +01001013static int spufs_signal1_open(struct inode *inode, struct file *file)
1014{
1015 struct spufs_inode_info *i = SPUFS_I(inode);
1016 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001017
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001018 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001019 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001020 if (!i->i_openers++)
1021 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001022 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001023 return nonseekable_open(inode, file);
1024}
1025
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001026static int
1027spufs_signal1_release(struct inode *inode, struct file *file)
1028{
1029 struct spufs_inode_info *i = SPUFS_I(inode);
1030 struct spu_context *ctx = i->i_ctx;
1031
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001032 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001033 if (!--i->i_openers)
1034 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001035 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001036 return 0;
1037}
1038
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001039static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001040 size_t len, loff_t *pos)
1041{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001042 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001043 u32 data;
1044
Arnd Bergmann67207b92005-11-15 15:53:48 -05001045 if (len < 4)
1046 return -EINVAL;
1047
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001048 if (ctx->csa.spu_chnlcnt_RW[3]) {
1049 data = ctx->csa.spu_chnldata_RW[3];
1050 ret = 4;
1051 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001052
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001053 if (!ret)
1054 goto out;
1055
Arnd Bergmann67207b92005-11-15 15:53:48 -05001056 if (copy_to_user(buf, &data, 4))
1057 return -EFAULT;
1058
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001059out:
1060 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001061}
1062
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001063static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1064 size_t len, loff_t *pos)
1065{
1066 int ret;
1067 struct spu_context *ctx = file->private_data;
1068
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001069 ret = spu_acquire_saved(ctx);
1070 if (ret)
1071 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001072 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001073 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001074
1075 return ret;
1076}
1077
Arnd Bergmann67207b92005-11-15 15:53:48 -05001078static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1079 size_t len, loff_t *pos)
1080{
1081 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001082 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001083 u32 data;
1084
1085 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001086
1087 if (len < 4)
1088 return -EINVAL;
1089
1090 if (copy_from_user(&data, buf, 4))
1091 return -EFAULT;
1092
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001093 ret = spu_acquire(ctx);
1094 if (ret)
1095 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001096 ctx->ops->signal1_write(ctx, data);
1097 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001098
1099 return 4;
1100}
1101
Nick Pigginb1e22702008-06-10 09:26:08 +10001102static int
1103spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001104{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001105#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1106 return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1107#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001108 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1109 * signal 1 and 2 area
1110 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001111 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001112#else
1113#error unsupported page size
1114#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001115}
1116
1117static struct vm_operations_struct spufs_signal1_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001118 .fault = spufs_signal1_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001119};
1120
1121static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1122{
1123 if (!(vma->vm_flags & VM_SHARED))
1124 return -EINVAL;
1125
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001126 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001127 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001128 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001129
1130 vma->vm_ops = &spufs_signal1_mmap_vmops;
1131 return 0;
1132}
Mark Nutter6df10a82006-03-23 00:00:12 +01001133
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001134static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001135 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001136 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001137 .read = spufs_signal1_read,
1138 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001139 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001140};
1141
Jeremy Kerrd054b362007-07-20 21:39:31 +02001142static const struct file_operations spufs_signal1_nosched_fops = {
1143 .open = spufs_signal1_open,
1144 .release = spufs_signal1_release,
1145 .write = spufs_signal1_write,
1146 .mmap = spufs_signal1_mmap,
1147};
1148
Mark Nutter6df10a82006-03-23 00:00:12 +01001149static int spufs_signal2_open(struct inode *inode, struct file *file)
1150{
1151 struct spufs_inode_info *i = SPUFS_I(inode);
1152 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001153
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001154 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001155 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001156 if (!i->i_openers++)
1157 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001158 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001159 return nonseekable_open(inode, file);
1160}
1161
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001162static int
1163spufs_signal2_release(struct inode *inode, struct file *file)
1164{
1165 struct spufs_inode_info *i = SPUFS_I(inode);
1166 struct spu_context *ctx = i->i_ctx;
1167
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001168 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001169 if (!--i->i_openers)
1170 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001171 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001172 return 0;
1173}
1174
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001175static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001176 size_t len, loff_t *pos)
1177{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001178 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001179 u32 data;
1180
Arnd Bergmann67207b92005-11-15 15:53:48 -05001181 if (len < 4)
1182 return -EINVAL;
1183
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001184 if (ctx->csa.spu_chnlcnt_RW[4]) {
1185 data = ctx->csa.spu_chnldata_RW[4];
1186 ret = 4;
1187 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001188
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001189 if (!ret)
1190 goto out;
1191
Arnd Bergmann67207b92005-11-15 15:53:48 -05001192 if (copy_to_user(buf, &data, 4))
1193 return -EFAULT;
1194
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001195out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001196 return ret;
1197}
1198
1199static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1200 size_t len, loff_t *pos)
1201{
1202 struct spu_context *ctx = file->private_data;
1203 int ret;
1204
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001205 ret = spu_acquire_saved(ctx);
1206 if (ret)
1207 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001208 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001209 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001210
1211 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001212}
1213
1214static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1215 size_t len, loff_t *pos)
1216{
1217 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001218 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001219 u32 data;
1220
1221 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001222
1223 if (len < 4)
1224 return -EINVAL;
1225
1226 if (copy_from_user(&data, buf, 4))
1227 return -EFAULT;
1228
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001229 ret = spu_acquire(ctx);
1230 if (ret)
1231 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001232 ctx->ops->signal2_write(ctx, data);
1233 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001234
1235 return 4;
1236}
1237
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001238#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001239static int
1240spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001241{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001242#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1243 return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1244#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001245 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1246 * signal 1 and 2 area
1247 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001248 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001249#else
1250#error unsupported page size
1251#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001252}
1253
1254static struct vm_operations_struct spufs_signal2_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001255 .fault = spufs_signal2_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001256};
1257
1258static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1259{
1260 if (!(vma->vm_flags & VM_SHARED))
1261 return -EINVAL;
1262
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001263 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001264 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001265 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001266
1267 vma->vm_ops = &spufs_signal2_mmap_vmops;
1268 return 0;
1269}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001270#else /* SPUFS_MMAP_4K */
1271#define spufs_signal2_mmap NULL
1272#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001273
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001274static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001275 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001276 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001277 .read = spufs_signal2_read,
1278 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001279 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001280};
1281
Jeremy Kerrd054b362007-07-20 21:39:31 +02001282static const struct file_operations spufs_signal2_nosched_fops = {
1283 .open = spufs_signal2_open,
1284 .release = spufs_signal2_release,
1285 .write = spufs_signal2_write,
1286 .mmap = spufs_signal2_mmap,
1287};
1288
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001289/*
1290 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1291 * work of acquiring (or not) the SPU context before calling through
1292 * to the actual get routine. The set routine is called directly.
1293 */
1294#define SPU_ATTR_NOACQUIRE 0
1295#define SPU_ATTR_ACQUIRE 1
1296#define SPU_ATTR_ACQUIRE_SAVED 2
1297
1298#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001299static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001300{ \
1301 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001302 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001303 \
1304 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001305 ret = spu_acquire(ctx); \
1306 if (ret) \
1307 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001308 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001309 spu_release(ctx); \
1310 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001311 ret = spu_acquire_saved(ctx); \
1312 if (ret) \
1313 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001314 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001315 spu_release_saved(ctx); \
1316 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001317 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001318 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001319 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001320} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001321DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001322
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001323static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001324{
1325 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001326 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001327
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001328 ret = spu_acquire(ctx);
1329 if (ret)
1330 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001331 ctx->ops->signal1_type_set(ctx, val);
1332 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001333
1334 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001335}
1336
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001337static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001338{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001339 return ctx->ops->signal1_type_get(ctx);
1340}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001341DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001342 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001343
Arnd Bergmann67207b92005-11-15 15:53:48 -05001344
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001345static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001346{
1347 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001348 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001349
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001350 ret = spu_acquire(ctx);
1351 if (ret)
1352 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001353 ctx->ops->signal2_type_set(ctx, val);
1354 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001355
1356 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001357}
1358
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001359static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001360{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001361 return ctx->ops->signal2_type_get(ctx);
1362}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001363DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001364 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001365
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001366#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001367static int
1368spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001369{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001370 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001371}
1372
1373static struct vm_operations_struct spufs_mss_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001374 .fault = spufs_mss_mmap_fault,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001375};
1376
1377/*
1378 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001379 */
1380static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1381{
1382 if (!(vma->vm_flags & VM_SHARED))
1383 return -EINVAL;
1384
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001385 vma->vm_flags |= VM_IO | VM_PFNMAP;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001386 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001387 | _PAGE_NO_CACHE | _PAGE_GUARDED);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001388
1389 vma->vm_ops = &spufs_mss_mmap_vmops;
1390 return 0;
1391}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001392#else /* SPUFS_MMAP_4K */
1393#define spufs_mss_mmap NULL
1394#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001395
1396static int spufs_mss_open(struct inode *inode, struct file *file)
1397{
1398 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001399 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001400
1401 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001402
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001403 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001404 if (!i->i_openers++)
1405 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001406 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001407 return nonseekable_open(inode, file);
1408}
1409
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001410static int
1411spufs_mss_release(struct inode *inode, struct file *file)
1412{
1413 struct spufs_inode_info *i = SPUFS_I(inode);
1414 struct spu_context *ctx = i->i_ctx;
1415
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001416 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001417 if (!--i->i_openers)
1418 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001419 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001420 return 0;
1421}
1422
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001423static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001424 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001425 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001426 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001427};
1428
Nick Pigginb1e22702008-06-10 09:26:08 +10001429static int
1430spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001431{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001432 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001433}
1434
1435static struct vm_operations_struct spufs_psmap_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001436 .fault = spufs_psmap_mmap_fault,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001437};
1438
1439/*
1440 * mmap support for full problem state area [0x00000 - 0x1ffff].
1441 */
1442static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1443{
1444 if (!(vma->vm_flags & VM_SHARED))
1445 return -EINVAL;
1446
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001447 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001448 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1449 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1450
1451 vma->vm_ops = &spufs_psmap_mmap_vmops;
1452 return 0;
1453}
1454
1455static int spufs_psmap_open(struct inode *inode, struct file *file)
1456{
1457 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001458 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001459
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001460 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001461 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001462 if (!i->i_openers++)
1463 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001464 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001465 return nonseekable_open(inode, file);
1466}
1467
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001468static int
1469spufs_psmap_release(struct inode *inode, struct file *file)
1470{
1471 struct spufs_inode_info *i = SPUFS_I(inode);
1472 struct spu_context *ctx = i->i_ctx;
1473
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001474 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001475 if (!--i->i_openers)
1476 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001477 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001478 return 0;
1479}
1480
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001481static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001482 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001483 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001484 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001485};
1486
1487
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001488#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001489static int
1490spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001491{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001492 return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +01001493}
1494
1495static struct vm_operations_struct spufs_mfc_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001496 .fault = spufs_mfc_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001497};
1498
1499/*
1500 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001501 */
1502static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1503{
1504 if (!(vma->vm_flags & VM_SHARED))
1505 return -EINVAL;
1506
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001507 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001508 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001509 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001510
1511 vma->vm_ops = &spufs_mfc_mmap_vmops;
1512 return 0;
1513}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001514#else /* SPUFS_MMAP_4K */
1515#define spufs_mfc_mmap NULL
1516#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001517
1518static int spufs_mfc_open(struct inode *inode, struct file *file)
1519{
1520 struct spufs_inode_info *i = SPUFS_I(inode);
1521 struct spu_context *ctx = i->i_ctx;
1522
1523 /* we don't want to deal with DMA into other processes */
1524 if (ctx->owner != current->mm)
1525 return -EINVAL;
1526
1527 if (atomic_read(&inode->i_count) != 1)
1528 return -EBUSY;
1529
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001530 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001531 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001532 if (!i->i_openers++)
1533 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001534 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001535 return nonseekable_open(inode, file);
1536}
1537
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001538static int
1539spufs_mfc_release(struct inode *inode, struct file *file)
1540{
1541 struct spufs_inode_info *i = SPUFS_I(inode);
1542 struct spu_context *ctx = i->i_ctx;
1543
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001544 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001545 if (!--i->i_openers)
1546 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001547 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001548 return 0;
1549}
1550
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001551/* interrupt-level mfc callback function. */
1552void spufs_mfc_callback(struct spu *spu)
1553{
1554 struct spu_context *ctx = spu->ctx;
1555
Luke Browninge65c2f62007-12-20 16:39:59 +09001556 if (!ctx)
1557 return;
1558
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001559 wake_up_all(&ctx->mfc_wq);
1560
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001561 pr_debug("%s %s\n", __func__, spu->name);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001562 if (ctx->mfc_fasync) {
1563 u32 free_elements, tagstatus;
1564 unsigned int mask;
1565
1566 /* no need for spu_acquire in interrupt context */
1567 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1568 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1569
1570 mask = 0;
1571 if (free_elements & 0xffff)
1572 mask |= POLLOUT;
1573 if (tagstatus & ctx->tagwait)
1574 mask |= POLLIN;
1575
1576 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1577 }
1578}
1579
1580static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1581{
1582 /* See if there is one tag group is complete */
1583 /* FIXME we need locking around tagwait */
1584 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1585 ctx->tagwait &= ~*status;
1586 if (*status)
1587 return 1;
1588
1589 /* enable interrupt waiting for any tag group,
1590 may silently fail if interrupts are already enabled */
1591 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1592 return 0;
1593}
1594
1595static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1596 size_t size, loff_t *pos)
1597{
1598 struct spu_context *ctx = file->private_data;
1599 int ret = -EINVAL;
1600 u32 status;
1601
1602 if (size != 4)
1603 goto out;
1604
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001605 ret = spu_acquire(ctx);
1606 if (ret)
1607 return ret;
1608
1609 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001610 if (file->f_flags & O_NONBLOCK) {
1611 status = ctx->ops->read_mfc_tagstatus(ctx);
1612 if (!(status & ctx->tagwait))
1613 ret = -EAGAIN;
1614 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001615 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001616 ctx->tagwait &= ~status;
1617 } else {
1618 ret = spufs_wait(ctx->mfc_wq,
1619 spufs_read_mfc_tagstatus(ctx, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001620 if (ret)
1621 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001622 }
1623 spu_release(ctx);
1624
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001625 ret = 4;
1626 if (copy_to_user(buffer, &status, 4))
1627 ret = -EFAULT;
1628
1629out:
1630 return ret;
1631}
1632
1633static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1634{
1635 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1636 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1637
1638 switch (cmd->cmd) {
1639 case MFC_PUT_CMD:
1640 case MFC_PUTF_CMD:
1641 case MFC_PUTB_CMD:
1642 case MFC_GET_CMD:
1643 case MFC_GETF_CMD:
1644 case MFC_GETB_CMD:
1645 break;
1646 default:
1647 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1648 return -EIO;
1649 }
1650
1651 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1652 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1653 cmd->ea, cmd->lsa);
1654 return -EIO;
1655 }
1656
1657 switch (cmd->size & 0xf) {
1658 case 1:
1659 break;
1660 case 2:
1661 if (cmd->lsa & 1)
1662 goto error;
1663 break;
1664 case 4:
1665 if (cmd->lsa & 3)
1666 goto error;
1667 break;
1668 case 8:
1669 if (cmd->lsa & 7)
1670 goto error;
1671 break;
1672 case 0:
1673 if (cmd->lsa & 15)
1674 goto error;
1675 break;
1676 error:
1677 default:
1678 pr_debug("invalid DMA alignment %x for size %x\n",
1679 cmd->lsa & 0xf, cmd->size);
1680 return -EIO;
1681 }
1682
1683 if (cmd->size > 16 * 1024) {
1684 pr_debug("invalid DMA size %x\n", cmd->size);
1685 return -EIO;
1686 }
1687
1688 if (cmd->tag & 0xfff0) {
1689 /* we reserve the higher tag numbers for kernel use */
1690 pr_debug("invalid DMA tag\n");
1691 return -EIO;
1692 }
1693
1694 if (cmd->class) {
1695 /* not supported in this version */
1696 pr_debug("invalid DMA class\n");
1697 return -EIO;
1698 }
1699
1700 return 0;
1701}
1702
1703static int spu_send_mfc_command(struct spu_context *ctx,
1704 struct mfc_dma_command cmd,
1705 int *error)
1706{
1707 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1708 if (*error == -EAGAIN) {
1709 /* wait for any tag group to complete
1710 so we have space for the new command */
1711 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1712 /* try again, because the queue might be
1713 empty again */
1714 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1715 if (*error == -EAGAIN)
1716 return 0;
1717 }
1718 return 1;
1719}
1720
1721static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1722 size_t size, loff_t *pos)
1723{
1724 struct spu_context *ctx = file->private_data;
1725 struct mfc_dma_command cmd;
1726 int ret = -EINVAL;
1727
1728 if (size != sizeof cmd)
1729 goto out;
1730
1731 ret = -EFAULT;
1732 if (copy_from_user(&cmd, buffer, sizeof cmd))
1733 goto out;
1734
1735 ret = spufs_check_valid_dma(&cmd);
1736 if (ret)
1737 goto out;
1738
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001739 ret = spu_acquire(ctx);
1740 if (ret)
1741 goto out;
1742
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001743 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001744 if (ret)
1745 goto out;
1746
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001747 if (file->f_flags & O_NONBLOCK) {
1748 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1749 } else {
1750 int status;
1751 ret = spufs_wait(ctx->mfc_wq,
1752 spu_send_mfc_command(ctx, cmd, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001753 if (ret)
1754 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001755 if (status)
1756 ret = status;
1757 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001758
1759 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001760 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001761
1762 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001763 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001764
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001765out_unlock:
1766 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001767out:
1768 return ret;
1769}
1770
1771static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1772{
1773 struct spu_context *ctx = file->private_data;
1774 u32 free_elements, tagstatus;
1775 unsigned int mask;
1776
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001777 poll_wait(file, &ctx->mfc_wq, wait);
1778
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001779 /*
1780 * For now keep this uninterruptible and also ignore the rule
1781 * that poll should not sleep. Will be fixed later.
1782 */
1783 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001784 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1785 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1786 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1787 spu_release(ctx);
1788
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001789 mask = 0;
1790 if (free_elements & 0xffff)
1791 mask |= POLLOUT | POLLWRNORM;
1792 if (tagstatus & ctx->tagwait)
1793 mask |= POLLIN | POLLRDNORM;
1794
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001795 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001796 free_elements, tagstatus, ctx->tagwait);
1797
1798 return mask;
1799}
1800
Al Viro73b6af82006-06-25 16:42:33 -07001801static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001802{
1803 struct spu_context *ctx = file->private_data;
1804 int ret;
1805
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001806 ret = spu_acquire(ctx);
1807 if (ret)
Christoph Hellwigeebead52008-02-08 15:50:41 +11001808 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001809#if 0
1810/* this currently hangs */
1811 ret = spufs_wait(ctx->mfc_wq,
1812 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1813 if (ret)
1814 goto out;
1815 ret = spufs_wait(ctx->mfc_wq,
1816 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001817 if (ret)
1818 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001819#else
1820 ret = 0;
1821#endif
1822 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001823out:
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001824 return ret;
1825}
1826
1827static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1828 int datasync)
1829{
Al Viro73b6af82006-06-25 16:42:33 -07001830 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001831}
1832
1833static int spufs_mfc_fasync(int fd, struct file *file, int on)
1834{
1835 struct spu_context *ctx = file->private_data;
1836
1837 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1838}
1839
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001840static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001841 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001842 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001843 .read = spufs_mfc_read,
1844 .write = spufs_mfc_write,
1845 .poll = spufs_mfc_poll,
1846 .flush = spufs_mfc_flush,
1847 .fsync = spufs_mfc_fsync,
1848 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001849 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001850};
1851
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001852static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001853{
1854 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001855 int ret;
1856
1857 ret = spu_acquire(ctx);
1858 if (ret)
1859 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001860 ctx->ops->npc_write(ctx, val);
1861 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001862
1863 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001864}
1865
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001866static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001867{
1868 return ctx->ops->npc_read(ctx);
1869}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001870DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1871 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001872
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001873static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001874{
1875 struct spu_context *ctx = data;
1876 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001877 int ret;
1878
1879 ret = spu_acquire_saved(ctx);
1880 if (ret)
1881 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001882 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001883 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001884
1885 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001886}
1887
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001888static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001889{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001890 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001891 return lscsa->decr.slot[0];
1892}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001893DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1894 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001895
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001896static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001897{
1898 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001899 int ret;
1900
1901 ret = spu_acquire_saved(ctx);
1902 if (ret)
1903 return ret;
Masato Noguchid40a01d2007-07-20 21:39:38 +02001904 if (val)
1905 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1906 else
1907 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001908 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001909
1910 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001911}
1912
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001913static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001914{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001915 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1916 return SPU_DECR_STATUS_RUNNING;
1917 else
1918 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001919}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001920DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1921 spufs_decr_status_set, "0x%llx\n",
1922 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001923
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001924static int spufs_event_mask_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001925{
1926 struct spu_context *ctx = data;
1927 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001928 int ret;
1929
1930 ret = spu_acquire_saved(ctx);
1931 if (ret)
1932 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001933 lscsa->event_mask.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001934 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001935
1936 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001937}
1938
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001939static u64 spufs_event_mask_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001940{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001941 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001942 return lscsa->event_mask.slot[0];
1943}
1944
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001945DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1946 spufs_event_mask_set, "0x%llx\n",
1947 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001948
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001949static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001950{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001951 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001952 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001953 stat = state->spu_chnlcnt_RW[0];
1954 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001955 return state->spu_chnldata_RW[0];
1956 return 0;
1957}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001958DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1959 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001960
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001961static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001962{
1963 struct spu_context *ctx = data;
1964 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001965 int ret;
1966
1967 ret = spu_acquire_saved(ctx);
1968 if (ret)
1969 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001970 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001971 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001972
1973 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001974}
1975
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001976static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001977{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001978 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001979 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001980}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001981DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1982 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001983
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001984static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001985{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001986 u64 num;
1987
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001988 if (ctx->state == SPU_STATE_RUNNABLE)
1989 num = ctx->spu->number;
1990 else
1991 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001992
1993 return num;
1994}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001995DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1996 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001997
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001998static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001999{
2000 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002001 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002002}
2003
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002004static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02002005{
2006 struct spu_context *ctx = data;
2007 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002008
2009 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02002010}
2011
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002012DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2013 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02002014
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002015static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002016{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002017 return ctx->csa.priv2.spu_lslr_RW;
2018}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002019DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2020 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002021
2022static int spufs_info_open(struct inode *inode, struct file *file)
2023{
2024 struct spufs_inode_info *i = SPUFS_I(inode);
2025 struct spu_context *ctx = i->i_ctx;
2026 file->private_data = ctx;
2027 return 0;
2028}
2029
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002030static int spufs_caps_show(struct seq_file *s, void *private)
2031{
2032 struct spu_context *ctx = s->private;
2033
2034 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2035 seq_puts(s, "sched\n");
2036 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2037 seq_puts(s, "step\n");
2038 return 0;
2039}
2040
2041static int spufs_caps_open(struct inode *inode, struct file *file)
2042{
2043 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2044}
2045
2046static const struct file_operations spufs_caps_fops = {
2047 .open = spufs_caps_open,
2048 .read = seq_read,
2049 .llseek = seq_lseek,
2050 .release = single_release,
2051};
2052
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002053static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2054 char __user *buf, size_t len, loff_t *pos)
2055{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002056 u32 data;
2057
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002058 /* EOF if there's no entry in the mbox */
2059 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2060 return 0;
2061
2062 data = ctx->csa.prob.pu_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002063
2064 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2065}
2066
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002067static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2068 size_t len, loff_t *pos)
2069{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002070 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002071 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002072
2073 if (!access_ok(VERIFY_WRITE, buf, len))
2074 return -EFAULT;
2075
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002076 ret = spu_acquire_saved(ctx);
2077 if (ret)
2078 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002079 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002080 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002081 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002082 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002083
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002084 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002085}
2086
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002087static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002088 .open = spufs_info_open,
2089 .read = spufs_mbox_info_read,
2090 .llseek = generic_file_llseek,
2091};
2092
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002093static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2094 char __user *buf, size_t len, loff_t *pos)
2095{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002096 u32 data;
2097
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002098 /* EOF if there's no entry in the ibox */
2099 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2100 return 0;
2101
2102 data = ctx->csa.priv2.puint_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002103
2104 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2105}
2106
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002107static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2108 size_t len, loff_t *pos)
2109{
2110 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002111 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002112
2113 if (!access_ok(VERIFY_WRITE, buf, len))
2114 return -EFAULT;
2115
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002116 ret = spu_acquire_saved(ctx);
2117 if (ret)
2118 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002119 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002120 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002121 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002122 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002123
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002124 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002125}
2126
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002127static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002128 .open = spufs_info_open,
2129 .read = spufs_ibox_info_read,
2130 .llseek = generic_file_llseek,
2131};
2132
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002133static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2134 char __user *buf, size_t len, loff_t *pos)
2135{
2136 int i, cnt;
2137 u32 data[4];
2138 u32 wbox_stat;
2139
2140 wbox_stat = ctx->csa.prob.mb_stat_R;
2141 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2142 for (i = 0; i < cnt; i++) {
2143 data[i] = ctx->csa.spu_mailbox_data[i];
2144 }
2145
2146 return simple_read_from_buffer(buf, len, pos, &data,
2147 cnt * sizeof(u32));
2148}
2149
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002150static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2151 size_t len, loff_t *pos)
2152{
2153 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002154 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002155
2156 if (!access_ok(VERIFY_WRITE, buf, len))
2157 return -EFAULT;
2158
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002159 ret = spu_acquire_saved(ctx);
2160 if (ret)
2161 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002162 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002163 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002164 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002165 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002166
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002167 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002168}
2169
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002170static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002171 .open = spufs_info_open,
2172 .read = spufs_wbox_info_read,
2173 .llseek = generic_file_llseek,
2174};
2175
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002176static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2177 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002178{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002179 struct spu_dma_info info;
2180 struct mfc_cq_sr *qp, *spuqp;
2181 int i;
2182
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002183 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2184 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2185 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2186 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2187 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2188 for (i = 0; i < 16; i++) {
2189 qp = &info.dma_info_command_data[i];
2190 spuqp = &ctx->csa.priv2.spuq[i];
2191
2192 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2193 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2194 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2195 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2196 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002197
2198 return simple_read_from_buffer(buf, len, pos, &info,
2199 sizeof info);
2200}
2201
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002202static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2203 size_t len, loff_t *pos)
2204{
2205 struct spu_context *ctx = file->private_data;
2206 int ret;
2207
2208 if (!access_ok(VERIFY_WRITE, buf, len))
2209 return -EFAULT;
2210
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002211 ret = spu_acquire_saved(ctx);
2212 if (ret)
2213 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002214 spin_lock(&ctx->csa.register_lock);
2215 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2216 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002217 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002218
2219 return ret;
2220}
2221
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002222static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002223 .open = spufs_info_open,
2224 .read = spufs_dma_info_read,
2225};
2226
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002227static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2228 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002229{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002230 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002231 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002232 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002233 int i;
2234
2235 if (len < ret)
2236 return -EINVAL;
2237
2238 if (!access_ok(VERIFY_WRITE, buf, len))
2239 return -EFAULT;
2240
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002241 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2242 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2243 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2244 for (i = 0; i < 8; i++) {
2245 qp = &info.proxydma_info_command_data[i];
2246 puqp = &ctx->csa.priv2.puq[i];
2247
2248 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2249 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2250 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2251 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2252 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002253
2254 return simple_read_from_buffer(buf, len, pos, &info,
2255 sizeof info);
2256}
2257
2258static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2259 size_t len, loff_t *pos)
2260{
2261 struct spu_context *ctx = file->private_data;
2262 int ret;
2263
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002264 ret = spu_acquire_saved(ctx);
2265 if (ret)
2266 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002267 spin_lock(&ctx->csa.register_lock);
2268 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002269 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002270 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002271
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002272 return ret;
2273}
2274
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002275static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002276 .open = spufs_info_open,
2277 .read = spufs_proxydma_info_read,
2278};
2279
Christoph Hellwig476273a2007-06-29 10:58:01 +10002280static int spufs_show_tid(struct seq_file *s, void *private)
2281{
2282 struct spu_context *ctx = s->private;
2283
2284 seq_printf(s, "%d\n", ctx->tid);
2285 return 0;
2286}
2287
2288static int spufs_tid_open(struct inode *inode, struct file *file)
2289{
2290 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2291}
2292
2293static const struct file_operations spufs_tid_fops = {
2294 .open = spufs_tid_open,
2295 .read = seq_read,
2296 .llseek = seq_lseek,
2297 .release = single_release,
2298};
2299
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002300static const char *ctx_state_names[] = {
2301 "user", "system", "iowait", "loaded"
2302};
2303
2304static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002305 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002306{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002307 struct timespec ts;
2308 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002309
Andre Detsch27ec41d2007-07-20 21:39:33 +02002310 /*
2311 * In general, utilization statistics are updated by the controlling
2312 * thread as the spu context moves through various well defined
2313 * state transitions, but if the context is lazily loaded its
2314 * utilization statistics are not updated as the controlling thread
2315 * is not tightly coupled with the execution of the spu context. We
2316 * calculate and apply the time delta from the last recorded state
2317 * of the spu context.
2318 */
2319 if (ctx->spu && ctx->stats.util_state == state) {
2320 ktime_get_ts(&ts);
2321 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2322 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002323
Andre Detsch27ec41d2007-07-20 21:39:33 +02002324 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002325}
2326
2327static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2328{
2329 unsigned long long slb_flts = ctx->stats.slb_flt;
2330
2331 if (ctx->state == SPU_STATE_RUNNABLE) {
2332 slb_flts += (ctx->spu->stats.slb_flt -
2333 ctx->stats.slb_flt_base);
2334 }
2335
2336 return slb_flts;
2337}
2338
2339static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2340{
2341 unsigned long long class2_intrs = ctx->stats.class2_intr;
2342
2343 if (ctx->state == SPU_STATE_RUNNABLE) {
2344 class2_intrs += (ctx->spu->stats.class2_intr -
2345 ctx->stats.class2_intr_base);
2346 }
2347
2348 return class2_intrs;
2349}
2350
2351
2352static int spufs_show_stat(struct seq_file *s, void *private)
2353{
2354 struct spu_context *ctx = s->private;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002355 int ret;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002356
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002357 ret = spu_acquire(ctx);
2358 if (ret)
2359 return ret;
2360
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002361 seq_printf(s, "%s %llu %llu %llu %llu "
2362 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002363 ctx_state_names[ctx->stats.util_state],
2364 spufs_acct_time(ctx, SPU_UTIL_USER),
2365 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2366 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2367 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002368 ctx->stats.vol_ctx_switch,
2369 ctx->stats.invol_ctx_switch,
2370 spufs_slb_flts(ctx),
2371 ctx->stats.hash_flt,
2372 ctx->stats.min_flt,
2373 ctx->stats.maj_flt,
2374 spufs_class2_intrs(ctx),
2375 ctx->stats.libassist);
2376 spu_release(ctx);
2377 return 0;
2378}
2379
2380static int spufs_stat_open(struct inode *inode, struct file *file)
2381{
2382 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2383}
2384
2385static const struct file_operations spufs_stat_fops = {
2386 .open = spufs_stat_open,
2387 .read = seq_read,
2388 .llseek = seq_lseek,
2389 .release = single_release,
2390};
2391
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002392static inline int spufs_switch_log_used(struct spu_context *ctx)
2393{
2394 return (ctx->switch_log->head - ctx->switch_log->tail) %
2395 SWITCH_LOG_BUFSIZE;
2396}
2397
2398static inline int spufs_switch_log_avail(struct spu_context *ctx)
2399{
2400 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2401}
2402
2403static int spufs_switch_log_open(struct inode *inode, struct file *file)
2404{
2405 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2406
2407 /*
2408 * We (ab-)use the mapping_lock here because it serves the similar
2409 * purpose for synchronizing open/close elsewhere. Maybe it should
2410 * be renamed eventually.
2411 */
2412 mutex_lock(&ctx->mapping_lock);
2413 if (ctx->switch_log) {
2414 spin_lock(&ctx->switch_log->lock);
2415 ctx->switch_log->head = 0;
2416 ctx->switch_log->tail = 0;
2417 spin_unlock(&ctx->switch_log->lock);
2418 } else {
2419 /*
2420 * We allocate the switch log data structures on first open.
2421 * They will never be free because we assume a context will
2422 * be traced until it goes away.
2423 */
2424 ctx->switch_log = kzalloc(sizeof(struct switch_log) +
2425 SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
2426 GFP_KERNEL);
2427 if (!ctx->switch_log)
2428 goto out;
2429 spin_lock_init(&ctx->switch_log->lock);
2430 init_waitqueue_head(&ctx->switch_log->wait);
2431 }
2432 mutex_unlock(&ctx->mapping_lock);
2433
2434 return 0;
2435 out:
2436 mutex_unlock(&ctx->mapping_lock);
2437 return -ENOMEM;
2438}
2439
2440static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2441{
2442 struct switch_log_entry *p;
2443
2444 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2445
2446 return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
2447 (unsigned int) p->tstamp.tv_sec,
2448 (unsigned int) p->tstamp.tv_nsec,
2449 p->spu_id,
2450 (unsigned int) p->type,
2451 (unsigned int) p->val,
2452 (unsigned long long) p->timebase);
2453}
2454
2455static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2456 size_t len, loff_t *ppos)
2457{
2458 struct inode *inode = file->f_path.dentry->d_inode;
2459 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2460 int error = 0, cnt = 0;
2461
2462 if (!buf || len < 0)
2463 return -EINVAL;
2464
2465 while (cnt < len) {
2466 char tbuf[128];
2467 int width;
2468
2469 if (file->f_flags & O_NONBLOCK) {
2470 if (spufs_switch_log_used(ctx) <= 0)
2471 return cnt ? cnt : -EAGAIN;
2472 } else {
2473 /* Wait for data in buffer */
2474 error = wait_event_interruptible(ctx->switch_log->wait,
2475 spufs_switch_log_used(ctx) > 0);
2476 if (error)
2477 break;
2478 }
2479
2480 spin_lock(&ctx->switch_log->lock);
2481 if (ctx->switch_log->head == ctx->switch_log->tail) {
2482 /* multiple readers race? */
2483 spin_unlock(&ctx->switch_log->lock);
2484 continue;
2485 }
2486
2487 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
2488 if (width < len) {
2489 ctx->switch_log->tail =
2490 (ctx->switch_log->tail + 1) %
2491 SWITCH_LOG_BUFSIZE;
2492 }
2493
2494 spin_unlock(&ctx->switch_log->lock);
2495
2496 /*
2497 * If the record is greater than space available return
2498 * partial buffer (so far)
2499 */
2500 if (width >= len)
2501 break;
2502
2503 error = copy_to_user(buf + cnt, tbuf, width);
2504 if (error)
2505 break;
2506 cnt += width;
2507 }
2508
2509 return cnt == 0 ? error : cnt;
2510}
2511
2512static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
2513{
2514 struct inode *inode = file->f_path.dentry->d_inode;
2515 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2516 unsigned int mask = 0;
2517
2518 poll_wait(file, &ctx->switch_log->wait, wait);
2519
2520 if (spufs_switch_log_used(ctx) > 0)
2521 mask |= POLLIN;
2522
2523 return mask;
2524}
2525
2526static const struct file_operations spufs_switch_log_fops = {
2527 .owner = THIS_MODULE,
2528 .open = spufs_switch_log_open,
2529 .read = spufs_switch_log_read,
2530 .poll = spufs_switch_log_poll,
2531};
2532
2533void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2534 u32 type, u32 val)
2535{
2536 if (!ctx->switch_log)
2537 return;
2538
2539 spin_lock(&ctx->switch_log->lock);
2540 if (spufs_switch_log_avail(ctx) > 1) {
2541 struct switch_log_entry *p;
2542
2543 p = ctx->switch_log->log + ctx->switch_log->head;
2544 ktime_get_ts(&p->tstamp);
2545 p->timebase = get_tb();
2546 p->spu_id = spu ? spu->number : -1;
2547 p->type = type;
2548 p->val = val;
2549
2550 ctx->switch_log->head =
2551 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2552 }
2553 spin_unlock(&ctx->switch_log->lock);
2554
2555 wake_up(&ctx->switch_log->wait);
2556}
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002557
Luke Browning46deed62008-06-16 11:36:43 +10002558static int spufs_show_ctx(struct seq_file *s, void *private)
2559{
2560 struct spu_context *ctx = s->private;
2561 u64 mfc_control_RW;
2562
2563 mutex_lock(&ctx->state_mutex);
2564 if (ctx->spu) {
2565 struct spu *spu = ctx->spu;
2566 struct spu_priv2 __iomem *priv2 = spu->priv2;
2567
2568 spin_lock_irq(&spu->register_lock);
2569 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2570 spin_unlock_irq(&spu->register_lock);
2571 } else {
2572 struct spu_state *csa = &ctx->csa;
2573
2574 mfc_control_RW = csa->priv2.mfc_control_RW;
2575 }
2576
2577 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
2578 " %c %lx %lx %lx %lx %x %x\n",
2579 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2580 ctx->flags,
2581 ctx->sched_flags,
2582 ctx->prio,
2583 ctx->time_slice,
2584 ctx->spu ? ctx->spu->number : -1,
2585 !list_empty(&ctx->rq) ? 'q' : ' ',
2586 ctx->csa.class_0_pending,
2587 ctx->csa.class_0_dar,
2588 ctx->csa.class_1_dsisr,
2589 mfc_control_RW,
2590 ctx->ops->runcntl_read(ctx),
2591 ctx->ops->status_read(ctx));
2592
2593 mutex_unlock(&ctx->state_mutex);
2594
2595 return 0;
2596}
2597
2598static int spufs_ctx_open(struct inode *inode, struct file *file)
2599{
2600 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2601}
2602
2603static const struct file_operations spufs_ctx_fops = {
2604 .open = spufs_ctx_open,
2605 .read = seq_read,
2606 .llseek = seq_lseek,
2607 .release = single_release,
2608};
2609
Jeremy Kerr23d893f2008-06-30 12:17:28 +10002610struct spufs_tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002611 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002612 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2613 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002614 { "mbox", &spufs_mbox_fops, 0444, },
2615 { "ibox", &spufs_ibox_fops, 0444, },
2616 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002617 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2618 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2619 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002620 { "signal1", &spufs_signal1_fops, 0666, },
2621 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002622 { "signal1_type", &spufs_signal1_type, 0666, },
2623 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002624 { "cntl", &spufs_cntl_fops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002625 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002626 { "lslr", &spufs_lslr_ops, 0444, },
2627 { "mfc", &spufs_mfc_fops, 0666, },
2628 { "mss", &spufs_mss_fops, 0666, },
2629 { "npc", &spufs_npc_ops, 0666, },
2630 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002631 { "decr", &spufs_decr_ops, 0666, },
2632 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002633 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002634 { "event_status", &spufs_event_status_ops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002635 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002636 { "phys-id", &spufs_id_ops, 0666, },
2637 { "object-id", &spufs_object_id_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002638 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
2639 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
2640 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
2641 { "dma_info", &spufs_dma_info_fops, 0444,
2642 sizeof(struct spu_dma_info), },
2643 { "proxydma_info", &spufs_proxydma_info_fops, 0444,
2644 sizeof(struct spu_proxydma_info)},
Christoph Hellwig476273a2007-06-29 10:58:01 +10002645 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002646 { "stat", &spufs_stat_fops, 0444, },
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002647 { "switch_log", &spufs_switch_log_fops, 0444 },
Luke Browning46deed62008-06-16 11:36:43 +10002648 { ".ctx", &spufs_ctx_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002649 {},
2650};
Mark Nutter5737edd2006-10-24 18:31:16 +02002651
Jeremy Kerr23d893f2008-06-30 12:17:28 +10002652struct spufs_tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002653 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002654 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002655 { "mbox", &spufs_mbox_fops, 0444, },
2656 { "ibox", &spufs_ibox_fops, 0444, },
2657 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002658 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2659 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2660 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002661 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2662 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002663 { "signal1_type", &spufs_signal1_type, 0666, },
2664 { "signal2_type", &spufs_signal2_type, 0666, },
2665 { "mss", &spufs_mss_fops, 0666, },
2666 { "mfc", &spufs_mfc_fops, 0666, },
2667 { "cntl", &spufs_cntl_fops, 0666, },
2668 { "npc", &spufs_npc_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002669 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002670 { "phys-id", &spufs_id_ops, 0666, },
2671 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002672 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002673 { "stat", &spufs_stat_fops, 0444, },
Luke Browning46deed62008-06-16 11:36:43 +10002674 { ".ctx", &spufs_ctx_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002675 {},
2676};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002677
2678struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002679 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2680 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002681 { "lslr", NULL, spufs_lslr_get, 19 },
2682 { "decr", NULL, spufs_decr_get, 19 },
2683 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002684 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2685 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002686 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002687 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002688 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2689 { "event_mask", NULL, spufs_event_mask_get, 19 },
2690 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002691 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2692 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2693 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2694 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2695 { "proxydma_info", __spufs_proxydma_info_read,
2696 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002697 { "object-id", NULL, spufs_object_id_get, 19 },
2698 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002699 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002700};