blob: 884e8bcec499ff13718b7bb09f4148467cb6d22d [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>
Arnd Bergmann67207b92005-11-15 15:53:48 -050032
33#include <asm/io.h>
FUJITA Tomonoridfe1e092008-05-13 19:07:42 +100034#include <asm/time.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050035#include <asm/spu.h>
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +010036#include <asm/spu_info.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050037#include <asm/uaccess.h>
38
39#include "spufs.h"
Christoph Hellwigae142e02009-06-12 04:31:52 +000040#include "sputrace.h"
Arnd Bergmann67207b92005-11-15 15:53:48 -050041
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} \
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700150static const struct file_operations __fops = { \
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900151 .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) {
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000276 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100277 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200278 } else {
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000279 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100280 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200281 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100282 vm_insert_pfn(vma, address, pfn);
283
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500284 spu_release(ctx);
285
Nick Pigginb1e22702008-06-10 09:26:08 +1000286 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500287}
288
Benjamin Herrenschmidta3528942008-07-23 21:27:09 -0700289static int spufs_mem_mmap_access(struct vm_area_struct *vma,
290 unsigned long address,
291 void *buf, int len, int write)
292{
293 struct spu_context *ctx = vma->vm_file->private_data;
294 unsigned long offset = address - vma->vm_start;
295 char *local_store;
296
297 if (write && !(vma->vm_flags & VM_WRITE))
298 return -EACCES;
299 if (spu_acquire(ctx))
300 return -EINTR;
301 if ((offset + len) > vma->vm_end)
302 len = vma->vm_end - offset;
303 local_store = ctx->ops->get_ls(ctx);
304 if (write)
305 memcpy_toio(local_store + offset, buf, len);
306 else
307 memcpy_fromio(buf, local_store + offset, len);
308 spu_release(ctx);
309 return len;
310}
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100311
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400312static const struct vm_operations_struct spufs_mem_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000313 .fault = spufs_mem_mmap_fault,
Benjamin Herrenschmidta3528942008-07-23 21:27:09 -0700314 .access = spufs_mem_mmap_access,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500315};
316
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000317static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500318{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000319#ifdef CONFIG_SPU_FS_64K_LS
320 struct spu_context *ctx = file->private_data;
321 struct spu_state *csa = &ctx->csa;
322
323 /* Sanity check VMA alignment */
324 if (csa->use_big_pages) {
325 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
326 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
327 vma->vm_pgoff);
328 if (vma->vm_start & 0xffff)
329 return -EINVAL;
330 if (vma->vm_pgoff & 0xf)
331 return -EINVAL;
332 }
333#endif /* CONFIG_SPU_FS_64K_LS */
334
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500335 if (!(vma->vm_flags & VM_SHARED))
336 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500337
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100338 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000339 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500340
341 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500342 return 0;
343}
344
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000345#ifdef CONFIG_SPU_FS_64K_LS
Sebastian Siewior12388192007-09-19 14:38:12 +1000346static unsigned long spufs_get_unmapped_area(struct file *file,
347 unsigned long addr, unsigned long len, unsigned long pgoff,
348 unsigned long flags)
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000349{
350 struct spu_context *ctx = file->private_data;
351 struct spu_state *csa = &ctx->csa;
352
353 /* If not using big pages, fallback to normal MM g_u_a */
354 if (!csa->use_big_pages)
355 return current->mm->get_unmapped_area(file, addr, len,
356 pgoff, flags);
357
358 /* Else, try to obtain a 64K pages slice */
359 return slice_get_unmapped_area(addr, len, flags,
360 MMU_PAGE_64K, 1, 0);
361}
362#endif /* CONFIG_SPU_FS_64K_LS */
363
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800364static const struct file_operations spufs_mem_fops = {
Jeremy Kerr70225432007-06-29 10:58:00 +1000365 .open = spufs_mem_open,
366 .release = spufs_mem_release,
367 .read = spufs_mem_read,
368 .write = spufs_mem_write,
369 .llseek = generic_file_llseek,
370 .mmap = spufs_mem_mmap,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000371#ifdef CONFIG_SPU_FS_64K_LS
372 .get_unmapped_area = spufs_get_unmapped_area,
373#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500374};
375
Nick Pigginb1e22702008-06-10 09:26:08 +1000376static int spufs_ps_fault(struct vm_area_struct *vma,
377 struct vm_fault *vmf,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100378 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200379 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100380{
Mark Nutter6df10a82006-03-23 00:00:12 +0100381 struct spu_context *ctx = vma->vm_file->private_data;
Nick Pigginb1e22702008-06-10 09:26:08 +1000382 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100383 int ret = 0;
Mark Nutter6df10a82006-03-23 00:00:12 +0100384
Nick Pigginb1e22702008-06-10 09:26:08 +1000385 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100386
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200387 if (offset >= ps_size)
Nick Pigginb1e22702008-06-10 09:26:08 +1000388 return VM_FAULT_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100389
Jeremy Kerr60657262008-11-11 10:22:22 +1100390 if (fatal_signal_pending(current))
391 return VM_FAULT_SIGBUS;
392
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900393 /*
Jeremy Kerrd5883132008-02-26 13:31:42 +1100394 * Because we release the mmap_sem, the context may be destroyed while
395 * we're in spu_wait. Grab an extra reference so it isn't destroyed
396 * in the meantime.
397 */
398 get_spu_context(ctx);
399
400 /*
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900401 * We have to wait for context to be loaded before we have
402 * pages to hand out to the user, but we don't want to wait
403 * with the mmap_sem held.
404 * It is possible to drop the mmap_sem here, but then we need
Nick Pigginb1e22702008-06-10 09:26:08 +1000405 * to return VM_FAULT_NOPAGE because the mappings may have
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900406 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100407 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900408 if (spu_acquire(ctx))
Jeremy Kerrd5883132008-02-26 13:31:42 +1100409 goto refault;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900410
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900411 if (ctx->state == SPU_STATE_SAVED) {
412 up_read(&current->mm->mmap_sem);
Nick Pigginb1e22702008-06-10 09:26:08 +1000413 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100414 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Nick Pigginb1e22702008-06-10 09:26:08 +1000415 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900416 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900417 } else {
418 area = ctx->spu->problem_phys + ps_offs;
Nick Pigginb1e22702008-06-10 09:26:08 +1000419 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
420 (area + offset) >> PAGE_SHIFT);
421 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900422 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100423
Christoph Hellwigeebead52008-02-08 15:50:41 +1100424 if (!ret)
425 spu_release(ctx);
Jeremy Kerrd5883132008-02-26 13:31:42 +1100426
427refault:
428 put_spu_context(ctx);
Nick Pigginb1e22702008-06-10 09:26:08 +1000429 return VM_FAULT_NOPAGE;
Mark Nutter6df10a82006-03-23 00:00:12 +0100430}
431
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200432#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +1000433static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
434 struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +0100435{
Jeremy Kerr87ff6092008-07-01 10:22:50 +1000436 return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +0100437}
438
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400439static const struct vm_operations_struct spufs_cntl_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000440 .fault = spufs_cntl_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +0100441};
442
443/*
444 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100445 */
446static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
447{
448 if (!(vma->vm_flags & VM_SHARED))
449 return -EINVAL;
450
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100451 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000452 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +0100453
454 vma->vm_ops = &spufs_cntl_mmap_vmops;
455 return 0;
456}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200457#else /* SPUFS_MMAP_4K */
458#define spufs_cntl_mmap NULL
459#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100460
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900461static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200462{
463 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900464 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200465
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900466 ret = spu_acquire(ctx);
467 if (ret)
468 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900469 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200470 spu_release(ctx);
471
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900472 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200473}
474
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900475static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200476{
477 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900478 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200479
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900480 ret = spu_acquire(ctx);
481 if (ret)
482 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200483 ctx->ops->runcntl_write(ctx, val);
484 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900485
486 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200487}
488
Mark Nutter6df10a82006-03-23 00:00:12 +0100489static int spufs_cntl_open(struct inode *inode, struct file *file)
490{
491 struct spufs_inode_info *i = SPUFS_I(inode);
492 struct spu_context *ctx = i->i_ctx;
493
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000494 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100495 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200496 if (!i->i_openers++)
497 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000498 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800499 return simple_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200500 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100501}
502
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200503static int
504spufs_cntl_release(struct inode *inode, struct file *file)
505{
506 struct spufs_inode_info *i = SPUFS_I(inode);
507 struct spu_context *ctx = i->i_ctx;
508
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800509 simple_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200510
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000511 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200512 if (!--i->i_openers)
513 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000514 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200515 return 0;
516}
517
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800518static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100519 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200520 .release = spufs_cntl_release,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800521 .read = simple_attr_read,
522 .write = simple_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100523 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100524};
525
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500526static int
527spufs_regs_open(struct inode *inode, struct file *file)
528{
529 struct spufs_inode_info *i = SPUFS_I(inode);
530 file->private_data = i->i_ctx;
531 return 0;
532}
533
534static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100535__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
536 size_t size, loff_t *pos)
537{
538 struct spu_lscsa *lscsa = ctx->csa.lscsa;
539 return simple_read_from_buffer(buffer, size, pos,
540 lscsa->gprs, sizeof lscsa->gprs);
541}
542
543static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500544spufs_regs_read(struct file *file, char __user *buffer,
545 size_t size, loff_t *pos)
546{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500547 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100548 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500549
Jeremy Kerrf027faa2008-10-16 11:11:12 +1100550 /* pre-check for file position: if we'd return EOF, there's no point
551 * causing a deschedule */
552 if (*pos >= sizeof(ctx->csa.lscsa->gprs))
553 return 0;
554
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900555 ret = spu_acquire_saved(ctx);
556 if (ret)
557 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100558 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200559 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500560 return ret;
561}
562
563static ssize_t
564spufs_regs_write(struct file *file, const char __user *buffer,
565 size_t size, loff_t *pos)
566{
567 struct spu_context *ctx = file->private_data;
568 struct spu_lscsa *lscsa = ctx->csa.lscsa;
569 int ret;
570
Jeremy Kerrd2198892009-03-03 19:38:07 +0000571 if (*pos >= sizeof(lscsa->gprs))
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500572 return -EFBIG;
Jeremy Kerrd2198892009-03-03 19:38:07 +0000573
574 size = min_t(ssize_t, sizeof(lscsa->gprs) - *pos, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500575 *pos += size;
576
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900577 ret = spu_acquire_saved(ctx);
578 if (ret)
579 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500580
Jeremy Kerr2fb44232009-03-03 19:39:32 +0000581 ret = copy_from_user((char *)lscsa->gprs + *pos - size,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500582 buffer, size) ? -EFAULT : size;
583
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200584 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500585 return ret;
586}
587
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800588static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500589 .open = spufs_regs_open,
590 .read = spufs_regs_read,
591 .write = spufs_regs_write,
592 .llseek = generic_file_llseek,
593};
594
595static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100596__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
597 size_t size, loff_t * pos)
598{
599 struct spu_lscsa *lscsa = ctx->csa.lscsa;
600 return simple_read_from_buffer(buffer, size, pos,
601 &lscsa->fpcr, sizeof(lscsa->fpcr));
602}
603
604static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500605spufs_fpcr_read(struct file *file, char __user * buffer,
606 size_t size, loff_t * pos)
607{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500608 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100609 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500610
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900611 ret = spu_acquire_saved(ctx);
612 if (ret)
613 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100614 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200615 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500616 return ret;
617}
618
619static ssize_t
620spufs_fpcr_write(struct file *file, const char __user * buffer,
621 size_t size, loff_t * pos)
622{
623 struct spu_context *ctx = file->private_data;
624 struct spu_lscsa *lscsa = ctx->csa.lscsa;
625 int ret;
626
Jeremy Kerrd2198892009-03-03 19:38:07 +0000627 if (*pos >= sizeof(lscsa->fpcr))
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500628 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900629
Jeremy Kerrd2198892009-03-03 19:38:07 +0000630 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
631
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900632 ret = spu_acquire_saved(ctx);
633 if (ret)
634 return ret;
635
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500636 *pos += size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500637 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
638 buffer, size) ? -EFAULT : size;
639
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200640 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500641 return ret;
642}
643
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800644static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500645 .open = spufs_regs_open,
646 .read = spufs_fpcr_read,
647 .write = spufs_fpcr_write,
648 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500649};
650
651/* generic open function for all pipe-like files */
652static int spufs_pipe_open(struct inode *inode, struct file *file)
653{
654 struct spufs_inode_info *i = SPUFS_I(inode);
655 file->private_data = i->i_ctx;
656
657 return nonseekable_open(inode, file);
658}
659
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200660/*
661 * Read as many bytes from the mailbox as possible, until
662 * one of the conditions becomes true:
663 *
664 * - no more data available in the mailbox
665 * - end of the user provided buffer
666 * - end of the mapped area
667 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500668static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
669 size_t len, loff_t *pos)
670{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500671 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200672 u32 mbox_data, __user *udata;
673 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500674
675 if (len < 4)
676 return -EINVAL;
677
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200678 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500679 return -EFAULT;
680
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200681 udata = (void __user *)buf;
682
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900683 count = spu_acquire(ctx);
684 if (count)
685 return count;
686
Arnd Bergmann274cef52006-10-24 18:01:42 +0200687 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200688 int ret;
689 ret = ctx->ops->mbox_read(ctx, &mbox_data);
690 if (ret == 0)
691 break;
692
693 /*
694 * at the end of the mapped area, we can fault
695 * but still need to return the data we have
696 * read successfully so far.
697 */
698 ret = __put_user(mbox_data, udata);
699 if (ret) {
700 if (!count)
701 count = -EFAULT;
702 break;
703 }
704 }
705 spu_release(ctx);
706
707 if (!count)
708 count = -EAGAIN;
709
710 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500711}
712
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800713static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500714 .open = spufs_pipe_open,
715 .read = spufs_mbox_read,
716};
717
718static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
719 size_t len, loff_t *pos)
720{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500721 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900722 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500723 u32 mbox_stat;
724
725 if (len < 4)
726 return -EINVAL;
727
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900728 ret = spu_acquire(ctx);
729 if (ret)
730 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500731
732 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
733
734 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500735
736 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
737 return -EFAULT;
738
739 return 4;
740}
741
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800742static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500743 .open = spufs_pipe_open,
744 .read = spufs_mbox_stat_read,
745};
746
747/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500748size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500749{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500750 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500751}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500752
753static int spufs_ibox_fasync(int fd, struct file *file, int on)
754{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500755 struct spu_context *ctx = file->private_data;
756
757 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
758}
759
760/* interrupt-level ibox callback function. */
761void spufs_ibox_callback(struct spu *spu)
762{
763 struct spu_context *ctx = spu->ctx;
764
Luke Browninge65c2f62007-12-20 16:39:59 +0900765 if (!ctx)
766 return;
767
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500768 wake_up_all(&ctx->ibox_wq);
769 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500770}
771
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200772/*
773 * Read as many bytes from the interrupt mailbox as possible, until
774 * one of the conditions becomes true:
775 *
776 * - no more data available in the mailbox
777 * - end of the user provided buffer
778 * - end of the mapped area
779 *
780 * If the file is opened without O_NONBLOCK, we wait here until
781 * any data is available, but return when we have been able to
782 * read something.
783 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500784static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
785 size_t len, loff_t *pos)
786{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500787 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200788 u32 ibox_data, __user *udata;
789 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500790
791 if (len < 4)
792 return -EINVAL;
793
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200794 if (!access_ok(VERIFY_WRITE, buf, len))
795 return -EFAULT;
796
797 udata = (void __user *)buf;
798
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900799 count = spu_acquire(ctx);
800 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100801 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500802
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200803 /* wait only for the first element */
804 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500805 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100806 if (!spu_ibox_read(ctx, &ibox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200807 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100808 goto out_unlock;
809 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500810 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200811 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100812 if (count)
813 goto out;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200814 }
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200815
816 /* if we can't write at all, return -EFAULT */
817 count = __put_user(ibox_data, udata);
818 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100819 goto out_unlock;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200820
821 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
822 int ret;
823 ret = ctx->ops->ibox_read(ctx, &ibox_data);
824 if (ret == 0)
825 break;
826 /*
827 * at the end of the mapped area, we can fault
828 * but still need to return the data we have
829 * read successfully so far.
830 */
831 ret = __put_user(ibox_data, udata);
832 if (ret)
833 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500834 }
835
Christoph Hellwigeebead52008-02-08 15:50:41 +1100836out_unlock:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500837 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100838out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200839 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500840}
841
842static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
843{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500844 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500845 unsigned int mask;
846
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500847 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500848
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900849 /*
850 * For now keep this uninterruptible and also ignore the rule
851 * that poll should not sleep. Will be fixed later.
852 */
853 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500854 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
855 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500856
857 return mask;
858}
859
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800860static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500861 .open = spufs_pipe_open,
862 .read = spufs_ibox_read,
863 .poll = spufs_ibox_poll,
864 .fasync = spufs_ibox_fasync,
865};
866
867static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
868 size_t len, loff_t *pos)
869{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500870 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900871 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500872 u32 ibox_stat;
873
874 if (len < 4)
875 return -EINVAL;
876
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900877 ret = spu_acquire(ctx);
878 if (ret)
879 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500880 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
881 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500882
883 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
884 return -EFAULT;
885
886 return 4;
887}
888
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800889static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500890 .open = spufs_pipe_open,
891 .read = spufs_ibox_stat_read,
892};
893
894/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500895size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500896{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500897 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500898}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500899
900static int spufs_wbox_fasync(int fd, struct file *file, int on)
901{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500902 struct spu_context *ctx = file->private_data;
903 int ret;
904
905 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
906
907 return ret;
908}
909
910/* interrupt-level wbox callback function. */
911void spufs_wbox_callback(struct spu *spu)
912{
913 struct spu_context *ctx = spu->ctx;
914
Luke Browninge65c2f62007-12-20 16:39:59 +0900915 if (!ctx)
916 return;
917
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500918 wake_up_all(&ctx->wbox_wq);
919 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500920}
921
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200922/*
923 * Write as many bytes to the interrupt mailbox as possible, until
924 * one of the conditions becomes true:
925 *
926 * - the mailbox is full
927 * - end of the user provided buffer
928 * - end of the mapped area
929 *
930 * If the file is opened without O_NONBLOCK, we wait here until
931 * space is availabyl, but return when we have been able to
932 * write something.
933 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500934static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
935 size_t len, loff_t *pos)
936{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500937 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200938 u32 wbox_data, __user *udata;
939 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500940
941 if (len < 4)
942 return -EINVAL;
943
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200944 udata = (void __user *)buf;
945 if (!access_ok(VERIFY_READ, buf, len))
946 return -EFAULT;
947
948 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500949 return -EFAULT;
950
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900951 count = spu_acquire(ctx);
952 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100953 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500954
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200955 /*
956 * make sure we can at least write one element, by waiting
957 * in case of !O_NONBLOCK
958 */
959 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500960 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100961 if (!spu_wbox_write(ctx, wbox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200962 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100963 goto out_unlock;
964 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500965 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200966 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100967 if (count)
968 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500969 }
970
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500971
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200972 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200973 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
974 int ret;
975 ret = __get_user(wbox_data, udata);
976 if (ret)
977 break;
978
979 ret = spu_wbox_write(ctx, wbox_data);
980 if (ret == 0)
981 break;
982 }
983
Christoph Hellwigeebead52008-02-08 15:50:41 +1100984out_unlock:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200985 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100986out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200987 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500988}
989
990static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
991{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500992 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500993 unsigned int mask;
994
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500995 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500996
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900997 /*
998 * For now keep this uninterruptible and also ignore the rule
999 * that poll should not sleep. Will be fixed later.
1000 */
1001 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -05001002 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
1003 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001004
1005 return mask;
1006}
1007
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001008static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001009 .open = spufs_pipe_open,
1010 .write = spufs_wbox_write,
1011 .poll = spufs_wbox_poll,
1012 .fasync = spufs_wbox_fasync,
1013};
1014
1015static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
1016 size_t len, loff_t *pos)
1017{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001018 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001019 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001020 u32 wbox_stat;
1021
1022 if (len < 4)
1023 return -EINVAL;
1024
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001025 ret = spu_acquire(ctx);
1026 if (ret)
1027 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001028 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
1029 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001030
1031 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
1032 return -EFAULT;
1033
1034 return 4;
1035}
1036
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001037static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001038 .open = spufs_pipe_open,
1039 .read = spufs_wbox_stat_read,
1040};
1041
Mark Nutter6df10a82006-03-23 00:00:12 +01001042static int spufs_signal1_open(struct inode *inode, struct file *file)
1043{
1044 struct spufs_inode_info *i = SPUFS_I(inode);
1045 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001046
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001047 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001048 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001049 if (!i->i_openers++)
1050 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001051 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001052 return nonseekable_open(inode, file);
1053}
1054
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001055static int
1056spufs_signal1_release(struct inode *inode, struct file *file)
1057{
1058 struct spufs_inode_info *i = SPUFS_I(inode);
1059 struct spu_context *ctx = i->i_ctx;
1060
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001061 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001062 if (!--i->i_openers)
1063 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001064 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001065 return 0;
1066}
1067
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001068static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001069 size_t len, loff_t *pos)
1070{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001071 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001072 u32 data;
1073
Arnd Bergmann67207b92005-11-15 15:53:48 -05001074 if (len < 4)
1075 return -EINVAL;
1076
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001077 if (ctx->csa.spu_chnlcnt_RW[3]) {
1078 data = ctx->csa.spu_chnldata_RW[3];
1079 ret = 4;
1080 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001081
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001082 if (!ret)
1083 goto out;
1084
Arnd Bergmann67207b92005-11-15 15:53:48 -05001085 if (copy_to_user(buf, &data, 4))
1086 return -EFAULT;
1087
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001088out:
1089 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001090}
1091
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001092static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1093 size_t len, loff_t *pos)
1094{
1095 int ret;
1096 struct spu_context *ctx = file->private_data;
1097
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001098 ret = spu_acquire_saved(ctx);
1099 if (ret)
1100 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001101 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001102 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001103
1104 return ret;
1105}
1106
Arnd Bergmann67207b92005-11-15 15:53:48 -05001107static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1108 size_t len, loff_t *pos)
1109{
1110 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001111 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001112 u32 data;
1113
1114 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001115
1116 if (len < 4)
1117 return -EINVAL;
1118
1119 if (copy_from_user(&data, buf, 4))
1120 return -EFAULT;
1121
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001122 ret = spu_acquire(ctx);
1123 if (ret)
1124 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001125 ctx->ops->signal1_write(ctx, data);
1126 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001127
1128 return 4;
1129}
1130
Nick Pigginb1e22702008-06-10 09:26:08 +10001131static int
1132spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001133{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001134#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1135 return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1136#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001137 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1138 * signal 1 and 2 area
1139 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001140 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001141#else
1142#error unsupported page size
1143#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001144}
1145
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001146static const struct vm_operations_struct spufs_signal1_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001147 .fault = spufs_signal1_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001148};
1149
1150static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1151{
1152 if (!(vma->vm_flags & VM_SHARED))
1153 return -EINVAL;
1154
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001155 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001156 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001157
1158 vma->vm_ops = &spufs_signal1_mmap_vmops;
1159 return 0;
1160}
Mark Nutter6df10a82006-03-23 00:00:12 +01001161
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001162static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001163 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001164 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001165 .read = spufs_signal1_read,
1166 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001167 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001168};
1169
Jeremy Kerrd054b362007-07-20 21:39:31 +02001170static const struct file_operations spufs_signal1_nosched_fops = {
1171 .open = spufs_signal1_open,
1172 .release = spufs_signal1_release,
1173 .write = spufs_signal1_write,
1174 .mmap = spufs_signal1_mmap,
1175};
1176
Mark Nutter6df10a82006-03-23 00:00:12 +01001177static int spufs_signal2_open(struct inode *inode, struct file *file)
1178{
1179 struct spufs_inode_info *i = SPUFS_I(inode);
1180 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001181
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001182 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001183 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001184 if (!i->i_openers++)
1185 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001186 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001187 return nonseekable_open(inode, file);
1188}
1189
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001190static int
1191spufs_signal2_release(struct inode *inode, struct file *file)
1192{
1193 struct spufs_inode_info *i = SPUFS_I(inode);
1194 struct spu_context *ctx = i->i_ctx;
1195
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001196 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001197 if (!--i->i_openers)
1198 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001199 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001200 return 0;
1201}
1202
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001203static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001204 size_t len, loff_t *pos)
1205{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001206 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001207 u32 data;
1208
Arnd Bergmann67207b92005-11-15 15:53:48 -05001209 if (len < 4)
1210 return -EINVAL;
1211
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001212 if (ctx->csa.spu_chnlcnt_RW[4]) {
1213 data = ctx->csa.spu_chnldata_RW[4];
1214 ret = 4;
1215 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001216
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001217 if (!ret)
1218 goto out;
1219
Arnd Bergmann67207b92005-11-15 15:53:48 -05001220 if (copy_to_user(buf, &data, 4))
1221 return -EFAULT;
1222
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001223out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001224 return ret;
1225}
1226
1227static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1228 size_t len, loff_t *pos)
1229{
1230 struct spu_context *ctx = file->private_data;
1231 int ret;
1232
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001233 ret = spu_acquire_saved(ctx);
1234 if (ret)
1235 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001236 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001237 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001238
1239 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001240}
1241
1242static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1243 size_t len, loff_t *pos)
1244{
1245 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001246 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001247 u32 data;
1248
1249 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001250
1251 if (len < 4)
1252 return -EINVAL;
1253
1254 if (copy_from_user(&data, buf, 4))
1255 return -EFAULT;
1256
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001257 ret = spu_acquire(ctx);
1258 if (ret)
1259 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001260 ctx->ops->signal2_write(ctx, data);
1261 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001262
1263 return 4;
1264}
1265
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001266#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001267static int
1268spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001269{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001270#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1271 return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1272#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001273 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1274 * signal 1 and 2 area
1275 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001276 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001277#else
1278#error unsupported page size
1279#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001280}
1281
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001282static const struct vm_operations_struct spufs_signal2_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001283 .fault = spufs_signal2_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001284};
1285
1286static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1287{
1288 if (!(vma->vm_flags & VM_SHARED))
1289 return -EINVAL;
1290
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001291 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001292 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001293
1294 vma->vm_ops = &spufs_signal2_mmap_vmops;
1295 return 0;
1296}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001297#else /* SPUFS_MMAP_4K */
1298#define spufs_signal2_mmap NULL
1299#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001300
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001301static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001302 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001303 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001304 .read = spufs_signal2_read,
1305 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001306 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001307};
1308
Jeremy Kerrd054b362007-07-20 21:39:31 +02001309static const struct file_operations spufs_signal2_nosched_fops = {
1310 .open = spufs_signal2_open,
1311 .release = spufs_signal2_release,
1312 .write = spufs_signal2_write,
1313 .mmap = spufs_signal2_mmap,
1314};
1315
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001316/*
1317 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1318 * work of acquiring (or not) the SPU context before calling through
1319 * to the actual get routine. The set routine is called directly.
1320 */
1321#define SPU_ATTR_NOACQUIRE 0
1322#define SPU_ATTR_ACQUIRE 1
1323#define SPU_ATTR_ACQUIRE_SAVED 2
1324
1325#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001326static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001327{ \
1328 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001329 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001330 \
1331 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001332 ret = spu_acquire(ctx); \
1333 if (ret) \
1334 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001335 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001336 spu_release(ctx); \
1337 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001338 ret = spu_acquire_saved(ctx); \
1339 if (ret) \
1340 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001341 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001342 spu_release_saved(ctx); \
1343 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001344 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001345 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001346 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001347} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001348DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001349
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001350static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001351{
1352 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001353 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001354
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001355 ret = spu_acquire(ctx);
1356 if (ret)
1357 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001358 ctx->ops->signal1_type_set(ctx, val);
1359 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001360
1361 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001362}
1363
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001364static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001365{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001366 return ctx->ops->signal1_type_get(ctx);
1367}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001368DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001369 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001370
Arnd Bergmann67207b92005-11-15 15:53:48 -05001371
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001372static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001373{
1374 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001375 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001376
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001377 ret = spu_acquire(ctx);
1378 if (ret)
1379 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001380 ctx->ops->signal2_type_set(ctx, val);
1381 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001382
1383 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001384}
1385
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001386static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001387{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001388 return ctx->ops->signal2_type_get(ctx);
1389}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001390DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001391 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001392
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001393#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001394static int
1395spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001396{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001397 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001398}
1399
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001400static const struct vm_operations_struct spufs_mss_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001401 .fault = spufs_mss_mmap_fault,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001402};
1403
1404/*
1405 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001406 */
1407static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1408{
1409 if (!(vma->vm_flags & VM_SHARED))
1410 return -EINVAL;
1411
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001412 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001413 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001414
1415 vma->vm_ops = &spufs_mss_mmap_vmops;
1416 return 0;
1417}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001418#else /* SPUFS_MMAP_4K */
1419#define spufs_mss_mmap NULL
1420#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001421
1422static int spufs_mss_open(struct inode *inode, struct file *file)
1423{
1424 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001425 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001426
1427 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001428
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001429 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001430 if (!i->i_openers++)
1431 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001432 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001433 return nonseekable_open(inode, file);
1434}
1435
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001436static int
1437spufs_mss_release(struct inode *inode, struct file *file)
1438{
1439 struct spufs_inode_info *i = SPUFS_I(inode);
1440 struct spu_context *ctx = i->i_ctx;
1441
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001442 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001443 if (!--i->i_openers)
1444 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001445 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001446 return 0;
1447}
1448
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001449static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001450 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001451 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001452 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001453};
1454
Nick Pigginb1e22702008-06-10 09:26:08 +10001455static int
1456spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001457{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001458 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001459}
1460
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001461static const struct vm_operations_struct spufs_psmap_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001462 .fault = spufs_psmap_mmap_fault,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001463};
1464
1465/*
1466 * mmap support for full problem state area [0x00000 - 0x1ffff].
1467 */
1468static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1469{
1470 if (!(vma->vm_flags & VM_SHARED))
1471 return -EINVAL;
1472
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001473 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001474 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001475
1476 vma->vm_ops = &spufs_psmap_mmap_vmops;
1477 return 0;
1478}
1479
1480static int spufs_psmap_open(struct inode *inode, struct file *file)
1481{
1482 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001483 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001484
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001485 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001486 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001487 if (!i->i_openers++)
1488 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001489 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001490 return nonseekable_open(inode, file);
1491}
1492
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001493static int
1494spufs_psmap_release(struct inode *inode, struct file *file)
1495{
1496 struct spufs_inode_info *i = SPUFS_I(inode);
1497 struct spu_context *ctx = i->i_ctx;
1498
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001499 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001500 if (!--i->i_openers)
1501 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001502 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001503 return 0;
1504}
1505
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001506static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001507 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001508 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001509 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001510};
1511
1512
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001513#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001514static int
1515spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001516{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001517 return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +01001518}
1519
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001520static const struct vm_operations_struct spufs_mfc_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001521 .fault = spufs_mfc_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001522};
1523
1524/*
1525 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001526 */
1527static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1528{
1529 if (!(vma->vm_flags & VM_SHARED))
1530 return -EINVAL;
1531
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001532 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001533 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001534
1535 vma->vm_ops = &spufs_mfc_mmap_vmops;
1536 return 0;
1537}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001538#else /* SPUFS_MMAP_4K */
1539#define spufs_mfc_mmap NULL
1540#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001541
1542static int spufs_mfc_open(struct inode *inode, struct file *file)
1543{
1544 struct spufs_inode_info *i = SPUFS_I(inode);
1545 struct spu_context *ctx = i->i_ctx;
1546
1547 /* we don't want to deal with DMA into other processes */
1548 if (ctx->owner != current->mm)
1549 return -EINVAL;
1550
1551 if (atomic_read(&inode->i_count) != 1)
1552 return -EBUSY;
1553
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001554 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001555 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001556 if (!i->i_openers++)
1557 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001558 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001559 return nonseekable_open(inode, file);
1560}
1561
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001562static int
1563spufs_mfc_release(struct inode *inode, struct file *file)
1564{
1565 struct spufs_inode_info *i = SPUFS_I(inode);
1566 struct spu_context *ctx = i->i_ctx;
1567
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001568 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001569 if (!--i->i_openers)
1570 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001571 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001572 return 0;
1573}
1574
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001575/* interrupt-level mfc callback function. */
1576void spufs_mfc_callback(struct spu *spu)
1577{
1578 struct spu_context *ctx = spu->ctx;
1579
Luke Browninge65c2f62007-12-20 16:39:59 +09001580 if (!ctx)
1581 return;
1582
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001583 wake_up_all(&ctx->mfc_wq);
1584
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001585 pr_debug("%s %s\n", __func__, spu->name);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001586 if (ctx->mfc_fasync) {
1587 u32 free_elements, tagstatus;
1588 unsigned int mask;
1589
1590 /* no need for spu_acquire in interrupt context */
1591 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1592 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1593
1594 mask = 0;
1595 if (free_elements & 0xffff)
1596 mask |= POLLOUT;
1597 if (tagstatus & ctx->tagwait)
1598 mask |= POLLIN;
1599
1600 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1601 }
1602}
1603
1604static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1605{
1606 /* See if there is one tag group is complete */
1607 /* FIXME we need locking around tagwait */
1608 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1609 ctx->tagwait &= ~*status;
1610 if (*status)
1611 return 1;
1612
1613 /* enable interrupt waiting for any tag group,
1614 may silently fail if interrupts are already enabled */
1615 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1616 return 0;
1617}
1618
1619static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1620 size_t size, loff_t *pos)
1621{
1622 struct spu_context *ctx = file->private_data;
1623 int ret = -EINVAL;
1624 u32 status;
1625
1626 if (size != 4)
1627 goto out;
1628
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001629 ret = spu_acquire(ctx);
1630 if (ret)
1631 return ret;
1632
1633 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001634 if (file->f_flags & O_NONBLOCK) {
1635 status = ctx->ops->read_mfc_tagstatus(ctx);
1636 if (!(status & ctx->tagwait))
1637 ret = -EAGAIN;
1638 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001639 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001640 ctx->tagwait &= ~status;
1641 } else {
1642 ret = spufs_wait(ctx->mfc_wq,
1643 spufs_read_mfc_tagstatus(ctx, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001644 if (ret)
1645 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001646 }
1647 spu_release(ctx);
1648
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001649 ret = 4;
1650 if (copy_to_user(buffer, &status, 4))
1651 ret = -EFAULT;
1652
1653out:
1654 return ret;
1655}
1656
1657static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1658{
Stephen Rothwell9477e452009-01-06 14:27:38 +00001659 pr_debug("queueing DMA %x %llx %x %x %x\n", cmd->lsa,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001660 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1661
1662 switch (cmd->cmd) {
1663 case MFC_PUT_CMD:
1664 case MFC_PUTF_CMD:
1665 case MFC_PUTB_CMD:
1666 case MFC_GET_CMD:
1667 case MFC_GETF_CMD:
1668 case MFC_GETB_CMD:
1669 break;
1670 default:
1671 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1672 return -EIO;
1673 }
1674
1675 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
Stephen Rothwell9477e452009-01-06 14:27:38 +00001676 pr_debug("invalid DMA alignment, ea %llx lsa %x\n",
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001677 cmd->ea, cmd->lsa);
1678 return -EIO;
1679 }
1680
1681 switch (cmd->size & 0xf) {
1682 case 1:
1683 break;
1684 case 2:
1685 if (cmd->lsa & 1)
1686 goto error;
1687 break;
1688 case 4:
1689 if (cmd->lsa & 3)
1690 goto error;
1691 break;
1692 case 8:
1693 if (cmd->lsa & 7)
1694 goto error;
1695 break;
1696 case 0:
1697 if (cmd->lsa & 15)
1698 goto error;
1699 break;
1700 error:
1701 default:
1702 pr_debug("invalid DMA alignment %x for size %x\n",
1703 cmd->lsa & 0xf, cmd->size);
1704 return -EIO;
1705 }
1706
1707 if (cmd->size > 16 * 1024) {
1708 pr_debug("invalid DMA size %x\n", cmd->size);
1709 return -EIO;
1710 }
1711
1712 if (cmd->tag & 0xfff0) {
1713 /* we reserve the higher tag numbers for kernel use */
1714 pr_debug("invalid DMA tag\n");
1715 return -EIO;
1716 }
1717
1718 if (cmd->class) {
1719 /* not supported in this version */
1720 pr_debug("invalid DMA class\n");
1721 return -EIO;
1722 }
1723
1724 return 0;
1725}
1726
1727static int spu_send_mfc_command(struct spu_context *ctx,
1728 struct mfc_dma_command cmd,
1729 int *error)
1730{
1731 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1732 if (*error == -EAGAIN) {
1733 /* wait for any tag group to complete
1734 so we have space for the new command */
1735 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1736 /* try again, because the queue might be
1737 empty again */
1738 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1739 if (*error == -EAGAIN)
1740 return 0;
1741 }
1742 return 1;
1743}
1744
1745static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1746 size_t size, loff_t *pos)
1747{
1748 struct spu_context *ctx = file->private_data;
1749 struct mfc_dma_command cmd;
1750 int ret = -EINVAL;
1751
1752 if (size != sizeof cmd)
1753 goto out;
1754
1755 ret = -EFAULT;
1756 if (copy_from_user(&cmd, buffer, sizeof cmd))
1757 goto out;
1758
1759 ret = spufs_check_valid_dma(&cmd);
1760 if (ret)
1761 goto out;
1762
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001763 ret = spu_acquire(ctx);
1764 if (ret)
1765 goto out;
1766
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001767 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001768 if (ret)
1769 goto out;
1770
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001771 if (file->f_flags & O_NONBLOCK) {
1772 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1773 } else {
1774 int status;
1775 ret = spufs_wait(ctx->mfc_wq,
1776 spu_send_mfc_command(ctx, cmd, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001777 if (ret)
1778 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001779 if (status)
1780 ret = status;
1781 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001782
1783 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001784 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001785
1786 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001787 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001788
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001789out_unlock:
1790 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001791out:
1792 return ret;
1793}
1794
1795static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1796{
1797 struct spu_context *ctx = file->private_data;
1798 u32 free_elements, tagstatus;
1799 unsigned int mask;
1800
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001801 poll_wait(file, &ctx->mfc_wq, wait);
1802
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001803 /*
1804 * For now keep this uninterruptible and also ignore the rule
1805 * that poll should not sleep. Will be fixed later.
1806 */
1807 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001808 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1809 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1810 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1811 spu_release(ctx);
1812
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001813 mask = 0;
1814 if (free_elements & 0xffff)
1815 mask |= POLLOUT | POLLWRNORM;
1816 if (tagstatus & ctx->tagwait)
1817 mask |= POLLIN | POLLRDNORM;
1818
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001819 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001820 free_elements, tagstatus, ctx->tagwait);
1821
1822 return mask;
1823}
1824
Al Viro73b6af82006-06-25 16:42:33 -07001825static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001826{
1827 struct spu_context *ctx = file->private_data;
1828 int ret;
1829
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001830 ret = spu_acquire(ctx);
1831 if (ret)
Christoph Hellwigeebead52008-02-08 15:50:41 +11001832 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001833#if 0
1834/* this currently hangs */
1835 ret = spufs_wait(ctx->mfc_wq,
1836 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1837 if (ret)
1838 goto out;
1839 ret = spufs_wait(ctx->mfc_wq,
1840 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001841 if (ret)
1842 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001843#else
1844 ret = 0;
1845#endif
1846 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001847out:
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001848 return ret;
1849}
1850
1851static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1852 int datasync)
1853{
Al Viro73b6af82006-06-25 16:42:33 -07001854 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001855}
1856
1857static int spufs_mfc_fasync(int fd, struct file *file, int on)
1858{
1859 struct spu_context *ctx = file->private_data;
1860
1861 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1862}
1863
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001864static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001865 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001866 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001867 .read = spufs_mfc_read,
1868 .write = spufs_mfc_write,
1869 .poll = spufs_mfc_poll,
1870 .flush = spufs_mfc_flush,
1871 .fsync = spufs_mfc_fsync,
1872 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001873 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001874};
1875
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001876static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001877{
1878 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001879 int ret;
1880
1881 ret = spu_acquire(ctx);
1882 if (ret)
1883 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001884 ctx->ops->npc_write(ctx, val);
1885 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001886
1887 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001888}
1889
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001890static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001891{
1892 return ctx->ops->npc_read(ctx);
1893}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001894DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1895 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001896
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001897static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001898{
1899 struct spu_context *ctx = data;
1900 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001901 int ret;
1902
1903 ret = spu_acquire_saved(ctx);
1904 if (ret)
1905 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001906 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001907 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001908
1909 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001910}
1911
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001912static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001913{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001914 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001915 return lscsa->decr.slot[0];
1916}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001917DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1918 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001919
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001920static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001921{
1922 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001923 int ret;
1924
1925 ret = spu_acquire_saved(ctx);
1926 if (ret)
1927 return ret;
Masato Noguchid40a01d2007-07-20 21:39:38 +02001928 if (val)
1929 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1930 else
1931 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001932 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001933
1934 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001935}
1936
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001937static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001938{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001939 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1940 return SPU_DECR_STATUS_RUNNING;
1941 else
1942 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001943}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001944DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1945 spufs_decr_status_set, "0x%llx\n",
1946 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001947
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001948static int spufs_event_mask_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001949{
1950 struct spu_context *ctx = data;
1951 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001952 int ret;
1953
1954 ret = spu_acquire_saved(ctx);
1955 if (ret)
1956 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001957 lscsa->event_mask.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001958 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001959
1960 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001961}
1962
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001963static u64 spufs_event_mask_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001964{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001965 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001966 return lscsa->event_mask.slot[0];
1967}
1968
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001969DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1970 spufs_event_mask_set, "0x%llx\n",
1971 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001972
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001973static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001974{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001975 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001976 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001977 stat = state->spu_chnlcnt_RW[0];
1978 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001979 return state->spu_chnldata_RW[0];
1980 return 0;
1981}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001982DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1983 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001984
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001985static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001986{
1987 struct spu_context *ctx = data;
1988 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001989 int ret;
1990
1991 ret = spu_acquire_saved(ctx);
1992 if (ret)
1993 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001994 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001995 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001996
1997 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001998}
1999
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002000static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002001{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002002 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002003 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002004}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002005DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
2006 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002007
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002008static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002009{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002010 u64 num;
2011
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002012 if (ctx->state == SPU_STATE_RUNNABLE)
2013 num = ctx->spu->number;
2014 else
2015 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002016
2017 return num;
2018}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002019DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
2020 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002021
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002022static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002023{
2024 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002025 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002026}
2027
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002028static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02002029{
2030 struct spu_context *ctx = data;
2031 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002032
2033 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02002034}
2035
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002036DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2037 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02002038
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002039static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002040{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002041 return ctx->csa.priv2.spu_lslr_RW;
2042}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002043DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2044 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002045
2046static int spufs_info_open(struct inode *inode, struct file *file)
2047{
2048 struct spufs_inode_info *i = SPUFS_I(inode);
2049 struct spu_context *ctx = i->i_ctx;
2050 file->private_data = ctx;
2051 return 0;
2052}
2053
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002054static int spufs_caps_show(struct seq_file *s, void *private)
2055{
2056 struct spu_context *ctx = s->private;
2057
2058 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2059 seq_puts(s, "sched\n");
2060 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2061 seq_puts(s, "step\n");
2062 return 0;
2063}
2064
2065static int spufs_caps_open(struct inode *inode, struct file *file)
2066{
2067 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2068}
2069
2070static const struct file_operations spufs_caps_fops = {
2071 .open = spufs_caps_open,
2072 .read = seq_read,
2073 .llseek = seq_lseek,
2074 .release = single_release,
2075};
2076
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002077static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2078 char __user *buf, size_t len, loff_t *pos)
2079{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002080 u32 data;
2081
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002082 /* EOF if there's no entry in the mbox */
2083 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2084 return 0;
2085
2086 data = ctx->csa.prob.pu_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002087
2088 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2089}
2090
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002091static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2092 size_t len, loff_t *pos)
2093{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002094 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002095 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002096
2097 if (!access_ok(VERIFY_WRITE, buf, len))
2098 return -EFAULT;
2099
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002100 ret = spu_acquire_saved(ctx);
2101 if (ret)
2102 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002103 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002104 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002105 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002106 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002107
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002108 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002109}
2110
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002111static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002112 .open = spufs_info_open,
2113 .read = spufs_mbox_info_read,
2114 .llseek = generic_file_llseek,
2115};
2116
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002117static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2118 char __user *buf, size_t len, loff_t *pos)
2119{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002120 u32 data;
2121
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002122 /* EOF if there's no entry in the ibox */
2123 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2124 return 0;
2125
2126 data = ctx->csa.priv2.puint_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002127
2128 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2129}
2130
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002131static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2132 size_t len, loff_t *pos)
2133{
2134 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002135 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002136
2137 if (!access_ok(VERIFY_WRITE, buf, len))
2138 return -EFAULT;
2139
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002140 ret = spu_acquire_saved(ctx);
2141 if (ret)
2142 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002143 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002144 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002145 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002146 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002147
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002148 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002149}
2150
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002151static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002152 .open = spufs_info_open,
2153 .read = spufs_ibox_info_read,
2154 .llseek = generic_file_llseek,
2155};
2156
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002157static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2158 char __user *buf, size_t len, loff_t *pos)
2159{
2160 int i, cnt;
2161 u32 data[4];
2162 u32 wbox_stat;
2163
2164 wbox_stat = ctx->csa.prob.mb_stat_R;
2165 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2166 for (i = 0; i < cnt; i++) {
2167 data[i] = ctx->csa.spu_mailbox_data[i];
2168 }
2169
2170 return simple_read_from_buffer(buf, len, pos, &data,
2171 cnt * sizeof(u32));
2172}
2173
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002174static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2175 size_t len, loff_t *pos)
2176{
2177 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002178 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002179
2180 if (!access_ok(VERIFY_WRITE, buf, len))
2181 return -EFAULT;
2182
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002183 ret = spu_acquire_saved(ctx);
2184 if (ret)
2185 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002186 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002187 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002188 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002189 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002190
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002191 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002192}
2193
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002194static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002195 .open = spufs_info_open,
2196 .read = spufs_wbox_info_read,
2197 .llseek = generic_file_llseek,
2198};
2199
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002200static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2201 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002202{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002203 struct spu_dma_info info;
2204 struct mfc_cq_sr *qp, *spuqp;
2205 int i;
2206
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002207 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2208 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2209 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2210 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2211 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2212 for (i = 0; i < 16; i++) {
2213 qp = &info.dma_info_command_data[i];
2214 spuqp = &ctx->csa.priv2.spuq[i];
2215
2216 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2217 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2218 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2219 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2220 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002221
2222 return simple_read_from_buffer(buf, len, pos, &info,
2223 sizeof info);
2224}
2225
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002226static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2227 size_t len, loff_t *pos)
2228{
2229 struct spu_context *ctx = file->private_data;
2230 int ret;
2231
2232 if (!access_ok(VERIFY_WRITE, buf, len))
2233 return -EFAULT;
2234
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002235 ret = spu_acquire_saved(ctx);
2236 if (ret)
2237 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002238 spin_lock(&ctx->csa.register_lock);
2239 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2240 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002241 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002242
2243 return ret;
2244}
2245
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002246static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002247 .open = spufs_info_open,
2248 .read = spufs_dma_info_read,
2249};
2250
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002251static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2252 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002253{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002254 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002255 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002256 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002257 int i;
2258
2259 if (len < ret)
2260 return -EINVAL;
2261
2262 if (!access_ok(VERIFY_WRITE, buf, len))
2263 return -EFAULT;
2264
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002265 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2266 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2267 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2268 for (i = 0; i < 8; i++) {
2269 qp = &info.proxydma_info_command_data[i];
2270 puqp = &ctx->csa.priv2.puq[i];
2271
2272 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2273 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2274 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2275 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2276 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002277
2278 return simple_read_from_buffer(buf, len, pos, &info,
2279 sizeof info);
2280}
2281
2282static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2283 size_t len, loff_t *pos)
2284{
2285 struct spu_context *ctx = file->private_data;
2286 int ret;
2287
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002288 ret = spu_acquire_saved(ctx);
2289 if (ret)
2290 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002291 spin_lock(&ctx->csa.register_lock);
2292 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002293 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002294 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002295
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002296 return ret;
2297}
2298
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002299static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002300 .open = spufs_info_open,
2301 .read = spufs_proxydma_info_read,
2302};
2303
Christoph Hellwig476273a2007-06-29 10:58:01 +10002304static int spufs_show_tid(struct seq_file *s, void *private)
2305{
2306 struct spu_context *ctx = s->private;
2307
2308 seq_printf(s, "%d\n", ctx->tid);
2309 return 0;
2310}
2311
2312static int spufs_tid_open(struct inode *inode, struct file *file)
2313{
2314 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2315}
2316
2317static const struct file_operations spufs_tid_fops = {
2318 .open = spufs_tid_open,
2319 .read = seq_read,
2320 .llseek = seq_lseek,
2321 .release = single_release,
2322};
2323
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002324static const char *ctx_state_names[] = {
2325 "user", "system", "iowait", "loaded"
2326};
2327
2328static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002329 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002330{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002331 struct timespec ts;
2332 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002333
Andre Detsch27ec41d2007-07-20 21:39:33 +02002334 /*
2335 * In general, utilization statistics are updated by the controlling
2336 * thread as the spu context moves through various well defined
2337 * state transitions, but if the context is lazily loaded its
2338 * utilization statistics are not updated as the controlling thread
2339 * is not tightly coupled with the execution of the spu context. We
2340 * calculate and apply the time delta from the last recorded state
2341 * of the spu context.
2342 */
2343 if (ctx->spu && ctx->stats.util_state == state) {
2344 ktime_get_ts(&ts);
2345 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2346 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002347
Andre Detsch27ec41d2007-07-20 21:39:33 +02002348 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002349}
2350
2351static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2352{
2353 unsigned long long slb_flts = ctx->stats.slb_flt;
2354
2355 if (ctx->state == SPU_STATE_RUNNABLE) {
2356 slb_flts += (ctx->spu->stats.slb_flt -
2357 ctx->stats.slb_flt_base);
2358 }
2359
2360 return slb_flts;
2361}
2362
2363static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2364{
2365 unsigned long long class2_intrs = ctx->stats.class2_intr;
2366
2367 if (ctx->state == SPU_STATE_RUNNABLE) {
2368 class2_intrs += (ctx->spu->stats.class2_intr -
2369 ctx->stats.class2_intr_base);
2370 }
2371
2372 return class2_intrs;
2373}
2374
2375
2376static int spufs_show_stat(struct seq_file *s, void *private)
2377{
2378 struct spu_context *ctx = s->private;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002379 int ret;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002380
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002381 ret = spu_acquire(ctx);
2382 if (ret)
2383 return ret;
2384
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002385 seq_printf(s, "%s %llu %llu %llu %llu "
2386 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002387 ctx_state_names[ctx->stats.util_state],
2388 spufs_acct_time(ctx, SPU_UTIL_USER),
2389 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2390 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2391 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002392 ctx->stats.vol_ctx_switch,
2393 ctx->stats.invol_ctx_switch,
2394 spufs_slb_flts(ctx),
2395 ctx->stats.hash_flt,
2396 ctx->stats.min_flt,
2397 ctx->stats.maj_flt,
2398 spufs_class2_intrs(ctx),
2399 ctx->stats.libassist);
2400 spu_release(ctx);
2401 return 0;
2402}
2403
2404static int spufs_stat_open(struct inode *inode, struct file *file)
2405{
2406 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2407}
2408
2409static const struct file_operations spufs_stat_fops = {
2410 .open = spufs_stat_open,
2411 .read = seq_read,
2412 .llseek = seq_lseek,
2413 .release = single_release,
2414};
2415
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002416static inline int spufs_switch_log_used(struct spu_context *ctx)
2417{
2418 return (ctx->switch_log->head - ctx->switch_log->tail) %
2419 SWITCH_LOG_BUFSIZE;
2420}
2421
2422static inline int spufs_switch_log_avail(struct spu_context *ctx)
2423{
2424 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2425}
2426
2427static int spufs_switch_log_open(struct inode *inode, struct file *file)
2428{
2429 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002430 int rc;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002431
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002432 rc = spu_acquire(ctx);
2433 if (rc)
2434 return rc;
2435
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002436 if (ctx->switch_log) {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002437 rc = -EBUSY;
2438 goto out;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002439 }
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002440
Jeremy Kerr837ef882008-10-17 12:02:31 +11002441 ctx->switch_log = kmalloc(sizeof(struct switch_log) +
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002442 SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
2443 GFP_KERNEL);
2444
2445 if (!ctx->switch_log) {
2446 rc = -ENOMEM;
2447 goto out;
2448 }
2449
Jeremy Kerr837ef882008-10-17 12:02:31 +11002450 ctx->switch_log->head = ctx->switch_log->tail = 0;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002451 init_waitqueue_head(&ctx->switch_log->wait);
2452 rc = 0;
2453
2454out:
2455 spu_release(ctx);
2456 return rc;
2457}
2458
2459static int spufs_switch_log_release(struct inode *inode, struct file *file)
2460{
2461 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2462 int rc;
2463
2464 rc = spu_acquire(ctx);
2465 if (rc)
2466 return rc;
2467
2468 kfree(ctx->switch_log);
2469 ctx->switch_log = NULL;
2470 spu_release(ctx);
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002471
2472 return 0;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002473}
2474
2475static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2476{
2477 struct switch_log_entry *p;
2478
2479 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2480
2481 return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
2482 (unsigned int) p->tstamp.tv_sec,
2483 (unsigned int) p->tstamp.tv_nsec,
2484 p->spu_id,
2485 (unsigned int) p->type,
2486 (unsigned int) p->val,
2487 (unsigned long long) p->timebase);
2488}
2489
2490static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2491 size_t len, loff_t *ppos)
2492{
2493 struct inode *inode = file->f_path.dentry->d_inode;
2494 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2495 int error = 0, cnt = 0;
2496
2497 if (!buf || len < 0)
2498 return -EINVAL;
2499
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002500 error = spu_acquire(ctx);
2501 if (error)
2502 return error;
2503
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002504 while (cnt < len) {
2505 char tbuf[128];
2506 int width;
2507
Jeremy Kerr14f693e2008-10-16 10:51:46 +11002508 if (spufs_switch_log_used(ctx) == 0) {
2509 if (cnt > 0) {
2510 /* If there's data ready to go, we can
2511 * just return straight away */
2512 break;
2513
2514 } else if (file->f_flags & O_NONBLOCK) {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002515 error = -EAGAIN;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002516 break;
Jeremy Kerr14f693e2008-10-16 10:51:46 +11002517
2518 } else {
2519 /* spufs_wait will drop the mutex and
2520 * re-acquire, but since we're in read(), the
2521 * file cannot be _released (and so
2522 * ctx->switch_log is stable).
2523 */
2524 error = spufs_wait(ctx->switch_log->wait,
2525 spufs_switch_log_used(ctx) > 0);
2526
2527 /* On error, spufs_wait returns without the
2528 * state mutex held */
2529 if (error)
2530 return error;
2531
2532 /* We may have had entries read from underneath
2533 * us while we dropped the mutex in spufs_wait,
2534 * so re-check */
2535 if (spufs_switch_log_used(ctx) == 0)
2536 continue;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002537 }
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002538 }
2539
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002540 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002541 if (width < len)
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002542 ctx->switch_log->tail =
2543 (ctx->switch_log->tail + 1) %
2544 SWITCH_LOG_BUFSIZE;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002545 else
2546 /* If the record is greater than space available return
2547 * partial buffer (so far) */
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002548 break;
2549
2550 error = copy_to_user(buf + cnt, tbuf, width);
2551 if (error)
2552 break;
2553 cnt += width;
2554 }
2555
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002556 spu_release(ctx);
2557
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002558 return cnt == 0 ? error : cnt;
2559}
2560
2561static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
2562{
2563 struct inode *inode = file->f_path.dentry->d_inode;
2564 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2565 unsigned int mask = 0;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002566 int rc;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002567
2568 poll_wait(file, &ctx->switch_log->wait, wait);
2569
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002570 rc = spu_acquire(ctx);
2571 if (rc)
2572 return rc;
2573
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002574 if (spufs_switch_log_used(ctx) > 0)
2575 mask |= POLLIN;
2576
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002577 spu_release(ctx);
2578
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002579 return mask;
2580}
2581
2582static const struct file_operations spufs_switch_log_fops = {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002583 .owner = THIS_MODULE,
2584 .open = spufs_switch_log_open,
2585 .read = spufs_switch_log_read,
2586 .poll = spufs_switch_log_poll,
2587 .release = spufs_switch_log_release,
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002588};
2589
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002590/**
2591 * Log a context switch event to a switch log reader.
2592 *
2593 * Must be called with ctx->state_mutex held.
2594 */
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002595void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2596 u32 type, u32 val)
2597{
2598 if (!ctx->switch_log)
2599 return;
2600
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002601 if (spufs_switch_log_avail(ctx) > 1) {
2602 struct switch_log_entry *p;
2603
2604 p = ctx->switch_log->log + ctx->switch_log->head;
2605 ktime_get_ts(&p->tstamp);
2606 p->timebase = get_tb();
2607 p->spu_id = spu ? spu->number : -1;
2608 p->type = type;
2609 p->val = val;
2610
2611 ctx->switch_log->head =
2612 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2613 }
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002614
2615 wake_up(&ctx->switch_log->wait);
2616}
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002617
Luke Browning46deed62008-06-16 11:36:43 +10002618static int spufs_show_ctx(struct seq_file *s, void *private)
2619{
2620 struct spu_context *ctx = s->private;
2621 u64 mfc_control_RW;
2622
2623 mutex_lock(&ctx->state_mutex);
2624 if (ctx->spu) {
2625 struct spu *spu = ctx->spu;
2626 struct spu_priv2 __iomem *priv2 = spu->priv2;
2627
2628 spin_lock_irq(&spu->register_lock);
2629 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2630 spin_unlock_irq(&spu->register_lock);
2631 } else {
2632 struct spu_state *csa = &ctx->csa;
2633
2634 mfc_control_RW = csa->priv2.mfc_control_RW;
2635 }
2636
2637 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
Stephen Rothwell9477e452009-01-06 14:27:38 +00002638 " %c %llx %llx %llx %llx %x %x\n",
Luke Browning46deed62008-06-16 11:36:43 +10002639 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2640 ctx->flags,
2641 ctx->sched_flags,
2642 ctx->prio,
2643 ctx->time_slice,
2644 ctx->spu ? ctx->spu->number : -1,
2645 !list_empty(&ctx->rq) ? 'q' : ' ',
2646 ctx->csa.class_0_pending,
2647 ctx->csa.class_0_dar,
2648 ctx->csa.class_1_dsisr,
2649 mfc_control_RW,
2650 ctx->ops->runcntl_read(ctx),
2651 ctx->ops->status_read(ctx));
2652
2653 mutex_unlock(&ctx->state_mutex);
2654
2655 return 0;
2656}
2657
2658static int spufs_ctx_open(struct inode *inode, struct file *file)
2659{
2660 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2661}
2662
2663static const struct file_operations spufs_ctx_fops = {
2664 .open = spufs_ctx_open,
2665 .read = seq_read,
2666 .llseek = seq_lseek,
2667 .release = single_release,
2668};
2669
Jeremy Kerr74254642009-02-17 11:44:14 +11002670const struct spufs_tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002671 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002672 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2673 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002674 { "mbox", &spufs_mbox_fops, 0444, },
2675 { "ibox", &spufs_ibox_fops, 0444, },
2676 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002677 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2678 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2679 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002680 { "signal1", &spufs_signal1_fops, 0666, },
2681 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002682 { "signal1_type", &spufs_signal1_type, 0666, },
2683 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002684 { "cntl", &spufs_cntl_fops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002685 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002686 { "lslr", &spufs_lslr_ops, 0444, },
2687 { "mfc", &spufs_mfc_fops, 0666, },
2688 { "mss", &spufs_mss_fops, 0666, },
2689 { "npc", &spufs_npc_ops, 0666, },
2690 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002691 { "decr", &spufs_decr_ops, 0666, },
2692 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002693 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002694 { "event_status", &spufs_event_status_ops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002695 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002696 { "phys-id", &spufs_id_ops, 0666, },
2697 { "object-id", &spufs_object_id_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002698 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
2699 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
2700 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
2701 { "dma_info", &spufs_dma_info_fops, 0444,
2702 sizeof(struct spu_dma_info), },
2703 { "proxydma_info", &spufs_proxydma_info_fops, 0444,
2704 sizeof(struct spu_proxydma_info)},
Christoph Hellwig476273a2007-06-29 10:58:01 +10002705 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002706 { "stat", &spufs_stat_fops, 0444, },
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002707 { "switch_log", &spufs_switch_log_fops, 0444 },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002708 {},
2709};
Mark Nutter5737edd2006-10-24 18:31:16 +02002710
Jeremy Kerr74254642009-02-17 11:44:14 +11002711const struct spufs_tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002712 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002713 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002714 { "mbox", &spufs_mbox_fops, 0444, },
2715 { "ibox", &spufs_ibox_fops, 0444, },
2716 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002717 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2718 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2719 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002720 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2721 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002722 { "signal1_type", &spufs_signal1_type, 0666, },
2723 { "signal2_type", &spufs_signal2_type, 0666, },
2724 { "mss", &spufs_mss_fops, 0666, },
2725 { "mfc", &spufs_mfc_fops, 0666, },
2726 { "cntl", &spufs_cntl_fops, 0666, },
2727 { "npc", &spufs_npc_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002728 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002729 { "phys-id", &spufs_id_ops, 0666, },
2730 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002731 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002732 { "stat", &spufs_stat_fops, 0444, },
Jeremy Kerr2c3e4782008-07-03 11:42:20 +10002733 {},
2734};
2735
Jeremy Kerr74254642009-02-17 11:44:14 +11002736const struct spufs_tree_descr spufs_dir_debug_contents[] = {
Luke Browning46deed62008-06-16 11:36:43 +10002737 { ".ctx", &spufs_ctx_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002738 {},
2739};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002740
Jeremy Kerr74254642009-02-17 11:44:14 +11002741const struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002742 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2743 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002744 { "lslr", NULL, spufs_lslr_get, 19 },
2745 { "decr", NULL, spufs_decr_get, 19 },
2746 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002747 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2748 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002749 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002750 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002751 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2752 { "event_mask", NULL, spufs_event_mask_get, 19 },
2753 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002754 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2755 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2756 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2757 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2758 { "proxydma_info", __spufs_proxydma_info_read,
2759 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002760 { "object-id", NULL, spufs_object_id_get, 19 },
2761 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002762 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002763};