blob: 1a40da92154c99cab1568cf80d743eca5fab4d2c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050033
34#include <asm/io.h>
FUJITA Tomonoridfe1e092008-05-13 19:07:42 +100035#include <asm/time.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050036#include <asm/spu.h>
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +010037#include <asm/spu_info.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050038#include <asm/uaccess.h>
39
40#include "spufs.h"
Christoph Hellwigae142e02009-06-12 04:31:52 +000041#include "sputrace.h"
Arnd Bergmann67207b92005-11-15 15:53:48 -050042
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +020043#define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
44
Christoph Hellwig197b1a82007-12-20 16:39:59 +090045/* Simple attribute files */
46struct spufs_attr {
47 int (*get)(void *, u64 *);
48 int (*set)(void *, u64);
49 char get_buf[24]; /* enough to store a u64 and "\n\0" */
50 char set_buf[24];
51 void *data;
52 const char *fmt; /* format for read operation */
53 struct mutex mutex; /* protects access to these buffers */
54};
55
56static int spufs_attr_open(struct inode *inode, struct file *file,
57 int (*get)(void *, u64 *), int (*set)(void *, u64),
58 const char *fmt)
59{
60 struct spufs_attr *attr;
61
62 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
63 if (!attr)
64 return -ENOMEM;
65
66 attr->get = get;
67 attr->set = set;
68 attr->data = inode->i_private;
69 attr->fmt = fmt;
70 mutex_init(&attr->mutex);
71 file->private_data = attr;
72
73 return nonseekable_open(inode, file);
74}
75
76static int spufs_attr_release(struct inode *inode, struct file *file)
77{
78 kfree(file->private_data);
79 return 0;
80}
81
82static ssize_t spufs_attr_read(struct file *file, char __user *buf,
83 size_t len, loff_t *ppos)
84{
85 struct spufs_attr *attr;
86 size_t size;
87 ssize_t ret;
88
89 attr = file->private_data;
90 if (!attr->get)
91 return -EACCES;
92
93 ret = mutex_lock_interruptible(&attr->mutex);
94 if (ret)
95 return ret;
96
97 if (*ppos) { /* continued read */
98 size = strlen(attr->get_buf);
99 } else { /* first read */
100 u64 val;
101 ret = attr->get(attr->data, &val);
102 if (ret)
103 goto out;
104
105 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
106 attr->fmt, (unsigned long long)val);
107 }
108
109 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
110out:
111 mutex_unlock(&attr->mutex);
112 return ret;
113}
114
115static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
116 size_t len, loff_t *ppos)
117{
118 struct spufs_attr *attr;
119 u64 val;
120 size_t size;
121 ssize_t ret;
122
123 attr = file->private_data;
124 if (!attr->set)
125 return -EACCES;
126
127 ret = mutex_lock_interruptible(&attr->mutex);
128 if (ret)
129 return ret;
130
131 ret = -EFAULT;
132 size = min(sizeof(attr->set_buf) - 1, len);
133 if (copy_from_user(attr->set_buf, buf, size))
134 goto out;
135
136 ret = len; /* claim we got the whole input */
137 attr->set_buf[size] = '\0';
138 val = simple_strtol(attr->set_buf, NULL, 0);
139 attr->set(attr->data, val);
140out:
141 mutex_unlock(&attr->mutex);
142 return ret;
143}
144
145#define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
146static int __fops ## _open(struct inode *inode, struct file *file) \
147{ \
148 __simple_attr_check_format(__fmt, 0ull); \
149 return spufs_attr_open(inode, file, __get, __set, __fmt); \
150} \
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700151static const struct file_operations __fops = { \
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900152 .owner = THIS_MODULE, \
153 .open = __fops ## _open, \
154 .release = spufs_attr_release, \
155 .read = spufs_attr_read, \
156 .write = spufs_attr_write, \
157};
158
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +1000159
Arnd Bergmann67207b92005-11-15 15:53:48 -0500160static int
161spufs_mem_open(struct inode *inode, struct file *file)
162{
163 struct spufs_inode_info *i = SPUFS_I(inode);
Mark Nutter6df10a82006-03-23 00:00:12 +0100164 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200165
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000166 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100167 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200168 if (!i->i_openers++)
169 ctx->local_store = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000170 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200171 return 0;
172}
173
174static int
175spufs_mem_release(struct inode *inode, struct file *file)
176{
177 struct spufs_inode_info *i = SPUFS_I(inode);
178 struct spu_context *ctx = i->i_ctx;
179
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000180 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200181 if (!--i->i_openers)
182 ctx->local_store = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000183 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500184 return 0;
185}
186
187static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100188__spufs_mem_read(struct spu_context *ctx, char __user *buffer,
189 size_t size, loff_t *pos)
190{
191 char *local_store = ctx->ops->get_ls(ctx);
192 return simple_read_from_buffer(buffer, size, pos, local_store,
193 LS_SIZE);
194}
195
196static ssize_t
Arnd Bergmann67207b92005-11-15 15:53:48 -0500197spufs_mem_read(struct file *file, char __user *buffer,
198 size_t size, loff_t *pos)
199{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100200 struct spu_context *ctx = file->private_data;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100201 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500202
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900203 ret = spu_acquire(ctx);
204 if (ret)
205 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100206 ret = __spufs_mem_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500207 spu_release(ctx);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900208
Arnd Bergmann67207b92005-11-15 15:53:48 -0500209 return ret;
210}
211
212static ssize_t
213spufs_mem_write(struct file *file, const char __user *buffer,
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100214 size_t size, loff_t *ppos)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500215{
216 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500217 char *local_store;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100218 loff_t pos = *ppos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500219 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500220
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100221 if (pos < 0)
222 return -EINVAL;
223 if (pos > LS_SIZE)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500224 return -EFBIG;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100225 if (size > LS_SIZE - pos)
226 size = LS_SIZE - pos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500227
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900228 ret = spu_acquire(ctx);
229 if (ret)
230 return ret;
231
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500232 local_store = ctx->ops->get_ls(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100233 ret = copy_from_user(local_store + pos, buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500234 spu_release(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100235
236 if (ret)
237 return -EFAULT;
238 *ppos = pos + size;
239 return size;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500240}
241
Nick Pigginb1e22702008-06-10 09:26:08 +1000242static int
243spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500244{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000245 struct spu_context *ctx = vma->vm_file->private_data;
Nick Pigginb1e22702008-06-10 09:26:08 +1000246 unsigned long address = (unsigned long)vmf->virtual_address;
247 unsigned long pfn, offset;
248
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000249#ifdef CONFIG_SPU_FS_64K_LS
250 struct spu_state *csa = &ctx->csa;
251 int psize;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100252
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000253 /* Check what page size we are using */
254 psize = get_slice_psize(vma->vm_mm, address);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500255
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000256 /* Some sanity checking */
257 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
258
259 /* Wow, 64K, cool, we need to align the address though */
260 if (csa->use_big_pages) {
261 BUG_ON(vma->vm_start & 0xffff);
262 address &= ~0xfffful;
263 }
264#endif /* CONFIG_SPU_FS_64K_LS */
265
Nick Pigginb1e22702008-06-10 09:26:08 +1000266 offset = vmf->pgoff << PAGE_SHIFT;
Masato Noguchi128b8542007-02-13 21:54:30 +0100267 if (offset >= LS_SIZE)
Nick Pigginb1e22702008-06-10 09:26:08 +1000268 return VM_FAULT_SIGBUS;
Masato Noguchi128b8542007-02-13 21:54:30 +0100269
Nick Pigginb1e22702008-06-10 09:26:08 +1000270 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
271 address, offset);
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000272
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900273 if (spu_acquire(ctx))
Nick Pigginb1e22702008-06-10 09:26:08 +1000274 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500275
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200276 if (ctx->state == SPU_STATE_SAVED) {
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000277 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100278 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200279 } else {
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000280 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100281 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200282 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100283 vm_insert_pfn(vma, address, pfn);
284
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500285 spu_release(ctx);
286
Nick Pigginb1e22702008-06-10 09:26:08 +1000287 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500288}
289
Benjamin Herrenschmidta3528942008-07-23 21:27:09 -0700290static int spufs_mem_mmap_access(struct vm_area_struct *vma,
291 unsigned long address,
292 void *buf, int len, int write)
293{
294 struct spu_context *ctx = vma->vm_file->private_data;
295 unsigned long offset = address - vma->vm_start;
296 char *local_store;
297
298 if (write && !(vma->vm_flags & VM_WRITE))
299 return -EACCES;
300 if (spu_acquire(ctx))
301 return -EINTR;
302 if ((offset + len) > vma->vm_end)
303 len = vma->vm_end - offset;
304 local_store = ctx->ops->get_ls(ctx);
305 if (write)
306 memcpy_toio(local_store + offset, buf, len);
307 else
308 memcpy_fromio(buf, local_store + offset, len);
309 spu_release(ctx);
310 return len;
311}
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100312
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400313static const struct vm_operations_struct spufs_mem_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000314 .fault = spufs_mem_mmap_fault,
Benjamin Herrenschmidta3528942008-07-23 21:27:09 -0700315 .access = spufs_mem_mmap_access,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500316};
317
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000318static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500319{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000320#ifdef CONFIG_SPU_FS_64K_LS
321 struct spu_context *ctx = file->private_data;
322 struct spu_state *csa = &ctx->csa;
323
324 /* Sanity check VMA alignment */
325 if (csa->use_big_pages) {
326 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
327 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
328 vma->vm_pgoff);
329 if (vma->vm_start & 0xffff)
330 return -EINVAL;
331 if (vma->vm_pgoff & 0xf)
332 return -EINVAL;
333 }
334#endif /* CONFIG_SPU_FS_64K_LS */
335
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500336 if (!(vma->vm_flags & VM_SHARED))
337 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500338
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100339 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000340 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500341
342 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500343 return 0;
344}
345
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000346#ifdef CONFIG_SPU_FS_64K_LS
Sebastian Siewior12388192007-09-19 14:38:12 +1000347static unsigned long spufs_get_unmapped_area(struct file *file,
348 unsigned long addr, unsigned long len, unsigned long pgoff,
349 unsigned long flags)
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000350{
351 struct spu_context *ctx = file->private_data;
352 struct spu_state *csa = &ctx->csa;
353
354 /* If not using big pages, fallback to normal MM g_u_a */
355 if (!csa->use_big_pages)
356 return current->mm->get_unmapped_area(file, addr, len,
357 pgoff, flags);
358
359 /* Else, try to obtain a 64K pages slice */
360 return slice_get_unmapped_area(addr, len, flags,
361 MMU_PAGE_64K, 1, 0);
362}
363#endif /* CONFIG_SPU_FS_64K_LS */
364
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800365static const struct file_operations spufs_mem_fops = {
Jeremy Kerr70225432007-06-29 10:58:00 +1000366 .open = spufs_mem_open,
367 .release = spufs_mem_release,
368 .read = spufs_mem_read,
369 .write = spufs_mem_write,
370 .llseek = generic_file_llseek,
371 .mmap = spufs_mem_mmap,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000372#ifdef CONFIG_SPU_FS_64K_LS
373 .get_unmapped_area = spufs_get_unmapped_area,
374#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500375};
376
Nick Pigginb1e22702008-06-10 09:26:08 +1000377static int spufs_ps_fault(struct vm_area_struct *vma,
378 struct vm_fault *vmf,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100379 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200380 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100381{
Mark Nutter6df10a82006-03-23 00:00:12 +0100382 struct spu_context *ctx = vma->vm_file->private_data;
Nick Pigginb1e22702008-06-10 09:26:08 +1000383 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100384 int ret = 0;
Mark Nutter6df10a82006-03-23 00:00:12 +0100385
Nick Pigginb1e22702008-06-10 09:26:08 +1000386 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100387
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200388 if (offset >= ps_size)
Nick Pigginb1e22702008-06-10 09:26:08 +1000389 return VM_FAULT_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100390
Jeremy Kerr60657262008-11-11 10:22:22 +1100391 if (fatal_signal_pending(current))
392 return VM_FAULT_SIGBUS;
393
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900394 /*
Jeremy Kerrd5883132008-02-26 13:31:42 +1100395 * Because we release the mmap_sem, the context may be destroyed while
396 * we're in spu_wait. Grab an extra reference so it isn't destroyed
397 * in the meantime.
398 */
399 get_spu_context(ctx);
400
401 /*
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900402 * We have to wait for context to be loaded before we have
403 * pages to hand out to the user, but we don't want to wait
404 * with the mmap_sem held.
405 * It is possible to drop the mmap_sem here, but then we need
Nick Pigginb1e22702008-06-10 09:26:08 +1000406 * to return VM_FAULT_NOPAGE because the mappings may have
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900407 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100408 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900409 if (spu_acquire(ctx))
Jeremy Kerrd5883132008-02-26 13:31:42 +1100410 goto refault;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900411
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900412 if (ctx->state == SPU_STATE_SAVED) {
413 up_read(&current->mm->mmap_sem);
Nick Pigginb1e22702008-06-10 09:26:08 +1000414 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100415 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Nick Pigginb1e22702008-06-10 09:26:08 +1000416 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900417 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900418 } else {
419 area = ctx->spu->problem_phys + ps_offs;
Nick Pigginb1e22702008-06-10 09:26:08 +1000420 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
421 (area + offset) >> PAGE_SHIFT);
422 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900423 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100424
Christoph Hellwigeebead52008-02-08 15:50:41 +1100425 if (!ret)
426 spu_release(ctx);
Jeremy Kerrd5883132008-02-26 13:31:42 +1100427
428refault:
429 put_spu_context(ctx);
Nick Pigginb1e22702008-06-10 09:26:08 +1000430 return VM_FAULT_NOPAGE;
Mark Nutter6df10a82006-03-23 00:00:12 +0100431}
432
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200433#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +1000434static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
435 struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +0100436{
Jeremy Kerr87ff6092008-07-01 10:22:50 +1000437 return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +0100438}
439
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400440static const struct vm_operations_struct spufs_cntl_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000441 .fault = spufs_cntl_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +0100442};
443
444/*
445 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100446 */
447static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
448{
449 if (!(vma->vm_flags & VM_SHARED))
450 return -EINVAL;
451
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100452 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000453 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +0100454
455 vma->vm_ops = &spufs_cntl_mmap_vmops;
456 return 0;
457}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200458#else /* SPUFS_MMAP_4K */
459#define spufs_cntl_mmap NULL
460#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100461
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900462static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200463{
464 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900465 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200466
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900467 ret = spu_acquire(ctx);
468 if (ret)
469 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900470 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200471 spu_release(ctx);
472
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900473 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200474}
475
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900476static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200477{
478 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900479 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200480
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900481 ret = spu_acquire(ctx);
482 if (ret)
483 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200484 ctx->ops->runcntl_write(ctx, val);
485 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900486
487 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200488}
489
Mark Nutter6df10a82006-03-23 00:00:12 +0100490static int spufs_cntl_open(struct inode *inode, struct file *file)
491{
492 struct spufs_inode_info *i = SPUFS_I(inode);
493 struct spu_context *ctx = i->i_ctx;
494
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000495 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100496 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200497 if (!i->i_openers++)
498 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000499 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800500 return simple_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200501 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100502}
503
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200504static int
505spufs_cntl_release(struct inode *inode, struct file *file)
506{
507 struct spufs_inode_info *i = SPUFS_I(inode);
508 struct spu_context *ctx = i->i_ctx;
509
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800510 simple_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200511
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000512 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200513 if (!--i->i_openers)
514 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000515 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200516 return 0;
517}
518
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800519static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100520 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200521 .release = spufs_cntl_release,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800522 .read = simple_attr_read,
523 .write = simple_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100524 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100525};
526
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500527static int
528spufs_regs_open(struct inode *inode, struct file *file)
529{
530 struct spufs_inode_info *i = SPUFS_I(inode);
531 file->private_data = i->i_ctx;
532 return 0;
533}
534
535static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100536__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
537 size_t size, loff_t *pos)
538{
539 struct spu_lscsa *lscsa = ctx->csa.lscsa;
540 return simple_read_from_buffer(buffer, size, pos,
541 lscsa->gprs, sizeof lscsa->gprs);
542}
543
544static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500545spufs_regs_read(struct file *file, char __user *buffer,
546 size_t size, loff_t *pos)
547{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500548 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100549 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500550
Jeremy Kerrf027faa2008-10-16 11:11:12 +1100551 /* pre-check for file position: if we'd return EOF, there's no point
552 * causing a deschedule */
553 if (*pos >= sizeof(ctx->csa.lscsa->gprs))
554 return 0;
555
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900556 ret = spu_acquire_saved(ctx);
557 if (ret)
558 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100559 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200560 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500561 return ret;
562}
563
564static ssize_t
565spufs_regs_write(struct file *file, const char __user *buffer,
566 size_t size, loff_t *pos)
567{
568 struct spu_context *ctx = file->private_data;
569 struct spu_lscsa *lscsa = ctx->csa.lscsa;
570 int ret;
571
Jeremy Kerrd2198892009-03-03 19:38:07 +0000572 if (*pos >= sizeof(lscsa->gprs))
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500573 return -EFBIG;
Jeremy Kerrd2198892009-03-03 19:38:07 +0000574
575 size = min_t(ssize_t, sizeof(lscsa->gprs) - *pos, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500576 *pos += size;
577
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900578 ret = spu_acquire_saved(ctx);
579 if (ret)
580 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500581
Jeremy Kerr2fb44232009-03-03 19:39:32 +0000582 ret = copy_from_user((char *)lscsa->gprs + *pos - size,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500583 buffer, size) ? -EFAULT : size;
584
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200585 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500586 return ret;
587}
588
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800589static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500590 .open = spufs_regs_open,
591 .read = spufs_regs_read,
592 .write = spufs_regs_write,
593 .llseek = generic_file_llseek,
594};
595
596static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100597__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
598 size_t size, loff_t * pos)
599{
600 struct spu_lscsa *lscsa = ctx->csa.lscsa;
601 return simple_read_from_buffer(buffer, size, pos,
602 &lscsa->fpcr, sizeof(lscsa->fpcr));
603}
604
605static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500606spufs_fpcr_read(struct file *file, char __user * buffer,
607 size_t size, loff_t * pos)
608{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500609 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100610 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500611
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900612 ret = spu_acquire_saved(ctx);
613 if (ret)
614 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100615 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200616 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500617 return ret;
618}
619
620static ssize_t
621spufs_fpcr_write(struct file *file, const char __user * buffer,
622 size_t size, loff_t * pos)
623{
624 struct spu_context *ctx = file->private_data;
625 struct spu_lscsa *lscsa = ctx->csa.lscsa;
626 int ret;
627
Jeremy Kerrd2198892009-03-03 19:38:07 +0000628 if (*pos >= sizeof(lscsa->fpcr))
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500629 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900630
Jeremy Kerrd2198892009-03-03 19:38:07 +0000631 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
632
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900633 ret = spu_acquire_saved(ctx);
634 if (ret)
635 return ret;
636
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500637 *pos += size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500638 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
639 buffer, size) ? -EFAULT : size;
640
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200641 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500642 return ret;
643}
644
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800645static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500646 .open = spufs_regs_open,
647 .read = spufs_fpcr_read,
648 .write = spufs_fpcr_write,
649 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500650};
651
652/* generic open function for all pipe-like files */
653static int spufs_pipe_open(struct inode *inode, struct file *file)
654{
655 struct spufs_inode_info *i = SPUFS_I(inode);
656 file->private_data = i->i_ctx;
657
658 return nonseekable_open(inode, file);
659}
660
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200661/*
662 * Read as many bytes from the mailbox as possible, until
663 * one of the conditions becomes true:
664 *
665 * - no more data available in the mailbox
666 * - end of the user provided buffer
667 * - end of the mapped area
668 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500669static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
670 size_t len, loff_t *pos)
671{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500672 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200673 u32 mbox_data, __user *udata;
674 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500675
676 if (len < 4)
677 return -EINVAL;
678
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200679 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500680 return -EFAULT;
681
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200682 udata = (void __user *)buf;
683
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900684 count = spu_acquire(ctx);
685 if (count)
686 return count;
687
Arnd Bergmann274cef52006-10-24 18:01:42 +0200688 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200689 int ret;
690 ret = ctx->ops->mbox_read(ctx, &mbox_data);
691 if (ret == 0)
692 break;
693
694 /*
695 * at the end of the mapped area, we can fault
696 * but still need to return the data we have
697 * read successfully so far.
698 */
699 ret = __put_user(mbox_data, udata);
700 if (ret) {
701 if (!count)
702 count = -EFAULT;
703 break;
704 }
705 }
706 spu_release(ctx);
707
708 if (!count)
709 count = -EAGAIN;
710
711 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500712}
713
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800714static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500715 .open = spufs_pipe_open,
716 .read = spufs_mbox_read,
717};
718
719static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
720 size_t len, loff_t *pos)
721{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500722 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900723 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500724 u32 mbox_stat;
725
726 if (len < 4)
727 return -EINVAL;
728
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900729 ret = spu_acquire(ctx);
730 if (ret)
731 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500732
733 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
734
735 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500736
737 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
738 return -EFAULT;
739
740 return 4;
741}
742
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800743static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500744 .open = spufs_pipe_open,
745 .read = spufs_mbox_stat_read,
746};
747
748/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500749size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500750{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500751 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500752}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500753
754static int spufs_ibox_fasync(int fd, struct file *file, int on)
755{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500756 struct spu_context *ctx = file->private_data;
757
758 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
759}
760
761/* interrupt-level ibox callback function. */
762void spufs_ibox_callback(struct spu *spu)
763{
764 struct spu_context *ctx = spu->ctx;
765
Luke Browninge65c2f62007-12-20 16:39:59 +0900766 if (!ctx)
767 return;
768
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500769 wake_up_all(&ctx->ibox_wq);
770 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500771}
772
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200773/*
774 * Read as many bytes from the interrupt mailbox as possible, until
775 * one of the conditions becomes true:
776 *
777 * - no more data available in the mailbox
778 * - end of the user provided buffer
779 * - end of the mapped area
780 *
781 * If the file is opened without O_NONBLOCK, we wait here until
782 * any data is available, but return when we have been able to
783 * read something.
784 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500785static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
786 size_t len, loff_t *pos)
787{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500788 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200789 u32 ibox_data, __user *udata;
790 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500791
792 if (len < 4)
793 return -EINVAL;
794
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200795 if (!access_ok(VERIFY_WRITE, buf, len))
796 return -EFAULT;
797
798 udata = (void __user *)buf;
799
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900800 count = spu_acquire(ctx);
801 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100802 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500803
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200804 /* wait only for the first element */
805 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500806 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100807 if (!spu_ibox_read(ctx, &ibox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200808 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100809 goto out_unlock;
810 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500811 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200812 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100813 if (count)
814 goto out;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200815 }
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200816
817 /* if we can't write at all, return -EFAULT */
818 count = __put_user(ibox_data, udata);
819 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100820 goto out_unlock;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200821
822 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
823 int ret;
824 ret = ctx->ops->ibox_read(ctx, &ibox_data);
825 if (ret == 0)
826 break;
827 /*
828 * at the end of the mapped area, we can fault
829 * but still need to return the data we have
830 * read successfully so far.
831 */
832 ret = __put_user(ibox_data, udata);
833 if (ret)
834 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500835 }
836
Christoph Hellwigeebead52008-02-08 15:50:41 +1100837out_unlock:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500838 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100839out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200840 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500841}
842
843static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
844{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500845 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500846 unsigned int mask;
847
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500848 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500849
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900850 /*
851 * For now keep this uninterruptible and also ignore the rule
852 * that poll should not sleep. Will be fixed later.
853 */
854 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500855 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
856 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500857
858 return mask;
859}
860
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800861static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500862 .open = spufs_pipe_open,
863 .read = spufs_ibox_read,
864 .poll = spufs_ibox_poll,
865 .fasync = spufs_ibox_fasync,
866};
867
868static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
869 size_t len, loff_t *pos)
870{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500871 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900872 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500873 u32 ibox_stat;
874
875 if (len < 4)
876 return -EINVAL;
877
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900878 ret = spu_acquire(ctx);
879 if (ret)
880 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500881 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
882 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500883
884 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
885 return -EFAULT;
886
887 return 4;
888}
889
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800890static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500891 .open = spufs_pipe_open,
892 .read = spufs_ibox_stat_read,
893};
894
895/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500896size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500897{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500898 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500899}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500900
901static int spufs_wbox_fasync(int fd, struct file *file, int on)
902{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500903 struct spu_context *ctx = file->private_data;
904 int ret;
905
906 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
907
908 return ret;
909}
910
911/* interrupt-level wbox callback function. */
912void spufs_wbox_callback(struct spu *spu)
913{
914 struct spu_context *ctx = spu->ctx;
915
Luke Browninge65c2f62007-12-20 16:39:59 +0900916 if (!ctx)
917 return;
918
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500919 wake_up_all(&ctx->wbox_wq);
920 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500921}
922
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200923/*
924 * Write as many bytes to the interrupt mailbox as possible, until
925 * one of the conditions becomes true:
926 *
927 * - the mailbox is full
928 * - end of the user provided buffer
929 * - end of the mapped area
930 *
931 * If the file is opened without O_NONBLOCK, we wait here until
932 * space is availabyl, but return when we have been able to
933 * write something.
934 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500935static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
936 size_t len, loff_t *pos)
937{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500938 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200939 u32 wbox_data, __user *udata;
940 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500941
942 if (len < 4)
943 return -EINVAL;
944
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200945 udata = (void __user *)buf;
946 if (!access_ok(VERIFY_READ, buf, len))
947 return -EFAULT;
948
949 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500950 return -EFAULT;
951
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900952 count = spu_acquire(ctx);
953 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100954 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500955
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200956 /*
957 * make sure we can at least write one element, by waiting
958 * in case of !O_NONBLOCK
959 */
960 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500961 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100962 if (!spu_wbox_write(ctx, wbox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200963 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100964 goto out_unlock;
965 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500966 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200967 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100968 if (count)
969 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500970 }
971
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500972
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200973 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200974 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
975 int ret;
976 ret = __get_user(wbox_data, udata);
977 if (ret)
978 break;
979
980 ret = spu_wbox_write(ctx, wbox_data);
981 if (ret == 0)
982 break;
983 }
984
Christoph Hellwigeebead52008-02-08 15:50:41 +1100985out_unlock:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200986 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100987out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200988 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500989}
990
991static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
992{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500993 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500994 unsigned int mask;
995
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500996 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500997
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900998 /*
999 * For now keep this uninterruptible and also ignore the rule
1000 * that poll should not sleep. Will be fixed later.
1001 */
1002 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -05001003 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
1004 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001005
1006 return mask;
1007}
1008
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001009static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001010 .open = spufs_pipe_open,
1011 .write = spufs_wbox_write,
1012 .poll = spufs_wbox_poll,
1013 .fasync = spufs_wbox_fasync,
1014};
1015
1016static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
1017 size_t len, loff_t *pos)
1018{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001019 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001020 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001021 u32 wbox_stat;
1022
1023 if (len < 4)
1024 return -EINVAL;
1025
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001026 ret = spu_acquire(ctx);
1027 if (ret)
1028 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001029 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
1030 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001031
1032 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
1033 return -EFAULT;
1034
1035 return 4;
1036}
1037
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001038static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001039 .open = spufs_pipe_open,
1040 .read = spufs_wbox_stat_read,
1041};
1042
Mark Nutter6df10a82006-03-23 00:00:12 +01001043static int spufs_signal1_open(struct inode *inode, struct file *file)
1044{
1045 struct spufs_inode_info *i = SPUFS_I(inode);
1046 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001047
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001048 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001049 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001050 if (!i->i_openers++)
1051 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001052 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001053 return nonseekable_open(inode, file);
1054}
1055
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001056static int
1057spufs_signal1_release(struct inode *inode, struct file *file)
1058{
1059 struct spufs_inode_info *i = SPUFS_I(inode);
1060 struct spu_context *ctx = i->i_ctx;
1061
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001062 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001063 if (!--i->i_openers)
1064 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001065 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001066 return 0;
1067}
1068
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001069static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001070 size_t len, loff_t *pos)
1071{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001072 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001073 u32 data;
1074
Arnd Bergmann67207b92005-11-15 15:53:48 -05001075 if (len < 4)
1076 return -EINVAL;
1077
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001078 if (ctx->csa.spu_chnlcnt_RW[3]) {
1079 data = ctx->csa.spu_chnldata_RW[3];
1080 ret = 4;
1081 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001082
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001083 if (!ret)
1084 goto out;
1085
Arnd Bergmann67207b92005-11-15 15:53:48 -05001086 if (copy_to_user(buf, &data, 4))
1087 return -EFAULT;
1088
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001089out:
1090 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001091}
1092
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001093static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1094 size_t len, loff_t *pos)
1095{
1096 int ret;
1097 struct spu_context *ctx = file->private_data;
1098
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001099 ret = spu_acquire_saved(ctx);
1100 if (ret)
1101 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001102 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001103 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001104
1105 return ret;
1106}
1107
Arnd Bergmann67207b92005-11-15 15:53:48 -05001108static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1109 size_t len, loff_t *pos)
1110{
1111 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001112 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001113 u32 data;
1114
1115 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001116
1117 if (len < 4)
1118 return -EINVAL;
1119
1120 if (copy_from_user(&data, buf, 4))
1121 return -EFAULT;
1122
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001123 ret = spu_acquire(ctx);
1124 if (ret)
1125 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001126 ctx->ops->signal1_write(ctx, data);
1127 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001128
1129 return 4;
1130}
1131
Nick Pigginb1e22702008-06-10 09:26:08 +10001132static int
1133spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001134{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001135#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1136 return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1137#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001138 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1139 * signal 1 and 2 area
1140 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001141 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001142#else
1143#error unsupported page size
1144#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001145}
1146
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001147static const struct vm_operations_struct spufs_signal1_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001148 .fault = spufs_signal1_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001149};
1150
1151static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1152{
1153 if (!(vma->vm_flags & VM_SHARED))
1154 return -EINVAL;
1155
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001156 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001157 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001158
1159 vma->vm_ops = &spufs_signal1_mmap_vmops;
1160 return 0;
1161}
Mark Nutter6df10a82006-03-23 00:00:12 +01001162
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001163static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001164 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001165 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001166 .read = spufs_signal1_read,
1167 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001168 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001169};
1170
Jeremy Kerrd054b362007-07-20 21:39:31 +02001171static const struct file_operations spufs_signal1_nosched_fops = {
1172 .open = spufs_signal1_open,
1173 .release = spufs_signal1_release,
1174 .write = spufs_signal1_write,
1175 .mmap = spufs_signal1_mmap,
1176};
1177
Mark Nutter6df10a82006-03-23 00:00:12 +01001178static int spufs_signal2_open(struct inode *inode, struct file *file)
1179{
1180 struct spufs_inode_info *i = SPUFS_I(inode);
1181 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001182
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001183 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001184 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001185 if (!i->i_openers++)
1186 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001187 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001188 return nonseekable_open(inode, file);
1189}
1190
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001191static int
1192spufs_signal2_release(struct inode *inode, struct file *file)
1193{
1194 struct spufs_inode_info *i = SPUFS_I(inode);
1195 struct spu_context *ctx = i->i_ctx;
1196
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001197 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001198 if (!--i->i_openers)
1199 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001200 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001201 return 0;
1202}
1203
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001204static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001205 size_t len, loff_t *pos)
1206{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001207 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001208 u32 data;
1209
Arnd Bergmann67207b92005-11-15 15:53:48 -05001210 if (len < 4)
1211 return -EINVAL;
1212
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001213 if (ctx->csa.spu_chnlcnt_RW[4]) {
1214 data = ctx->csa.spu_chnldata_RW[4];
1215 ret = 4;
1216 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001217
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001218 if (!ret)
1219 goto out;
1220
Arnd Bergmann67207b92005-11-15 15:53:48 -05001221 if (copy_to_user(buf, &data, 4))
1222 return -EFAULT;
1223
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001224out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001225 return ret;
1226}
1227
1228static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1229 size_t len, loff_t *pos)
1230{
1231 struct spu_context *ctx = file->private_data;
1232 int ret;
1233
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001234 ret = spu_acquire_saved(ctx);
1235 if (ret)
1236 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001237 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001238 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001239
1240 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001241}
1242
1243static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1244 size_t len, loff_t *pos)
1245{
1246 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001247 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001248 u32 data;
1249
1250 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001251
1252 if (len < 4)
1253 return -EINVAL;
1254
1255 if (copy_from_user(&data, buf, 4))
1256 return -EFAULT;
1257
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001258 ret = spu_acquire(ctx);
1259 if (ret)
1260 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001261 ctx->ops->signal2_write(ctx, data);
1262 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001263
1264 return 4;
1265}
1266
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001267#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001268static int
1269spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001270{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001271#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1272 return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1273#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001274 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1275 * signal 1 and 2 area
1276 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001277 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001278#else
1279#error unsupported page size
1280#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001281}
1282
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001283static const struct vm_operations_struct spufs_signal2_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001284 .fault = spufs_signal2_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001285};
1286
1287static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1288{
1289 if (!(vma->vm_flags & VM_SHARED))
1290 return -EINVAL;
1291
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001292 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001293 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001294
1295 vma->vm_ops = &spufs_signal2_mmap_vmops;
1296 return 0;
1297}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001298#else /* SPUFS_MMAP_4K */
1299#define spufs_signal2_mmap NULL
1300#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001301
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001302static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001303 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001304 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001305 .read = spufs_signal2_read,
1306 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001307 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001308};
1309
Jeremy Kerrd054b362007-07-20 21:39:31 +02001310static const struct file_operations spufs_signal2_nosched_fops = {
1311 .open = spufs_signal2_open,
1312 .release = spufs_signal2_release,
1313 .write = spufs_signal2_write,
1314 .mmap = spufs_signal2_mmap,
1315};
1316
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001317/*
1318 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1319 * work of acquiring (or not) the SPU context before calling through
1320 * to the actual get routine. The set routine is called directly.
1321 */
1322#define SPU_ATTR_NOACQUIRE 0
1323#define SPU_ATTR_ACQUIRE 1
1324#define SPU_ATTR_ACQUIRE_SAVED 2
1325
1326#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001327static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001328{ \
1329 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001330 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001331 \
1332 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001333 ret = spu_acquire(ctx); \
1334 if (ret) \
1335 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001336 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001337 spu_release(ctx); \
1338 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001339 ret = spu_acquire_saved(ctx); \
1340 if (ret) \
1341 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001342 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001343 spu_release_saved(ctx); \
1344 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001345 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001346 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001347 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001348} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001349DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001350
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001351static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001352{
1353 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001354 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001355
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001356 ret = spu_acquire(ctx);
1357 if (ret)
1358 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001359 ctx->ops->signal1_type_set(ctx, val);
1360 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001361
1362 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001363}
1364
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001365static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001366{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001367 return ctx->ops->signal1_type_get(ctx);
1368}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001369DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001370 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001371
Arnd Bergmann67207b92005-11-15 15:53:48 -05001372
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001373static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001374{
1375 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001376 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001377
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001378 ret = spu_acquire(ctx);
1379 if (ret)
1380 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001381 ctx->ops->signal2_type_set(ctx, val);
1382 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001383
1384 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001385}
1386
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001387static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001388{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001389 return ctx->ops->signal2_type_get(ctx);
1390}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001391DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001392 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001393
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001394#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001395static int
1396spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001397{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001398 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001399}
1400
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001401static const struct vm_operations_struct spufs_mss_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001402 .fault = spufs_mss_mmap_fault,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001403};
1404
1405/*
1406 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001407 */
1408static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1409{
1410 if (!(vma->vm_flags & VM_SHARED))
1411 return -EINVAL;
1412
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001413 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001414 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001415
1416 vma->vm_ops = &spufs_mss_mmap_vmops;
1417 return 0;
1418}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001419#else /* SPUFS_MMAP_4K */
1420#define spufs_mss_mmap NULL
1421#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001422
1423static int spufs_mss_open(struct inode *inode, struct file *file)
1424{
1425 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001426 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001427
1428 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001429
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001430 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001431 if (!i->i_openers++)
1432 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001433 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001434 return nonseekable_open(inode, file);
1435}
1436
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001437static int
1438spufs_mss_release(struct inode *inode, struct file *file)
1439{
1440 struct spufs_inode_info *i = SPUFS_I(inode);
1441 struct spu_context *ctx = i->i_ctx;
1442
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001443 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001444 if (!--i->i_openers)
1445 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001446 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001447 return 0;
1448}
1449
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001450static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001451 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001452 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001453 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001454};
1455
Nick Pigginb1e22702008-06-10 09:26:08 +10001456static int
1457spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001458{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001459 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001460}
1461
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001462static const struct vm_operations_struct spufs_psmap_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001463 .fault = spufs_psmap_mmap_fault,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001464};
1465
1466/*
1467 * mmap support for full problem state area [0x00000 - 0x1ffff].
1468 */
1469static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1470{
1471 if (!(vma->vm_flags & VM_SHARED))
1472 return -EINVAL;
1473
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001474 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001475 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001476
1477 vma->vm_ops = &spufs_psmap_mmap_vmops;
1478 return 0;
1479}
1480
1481static int spufs_psmap_open(struct inode *inode, struct file *file)
1482{
1483 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001484 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001485
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001486 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001487 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001488 if (!i->i_openers++)
1489 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001490 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001491 return nonseekable_open(inode, file);
1492}
1493
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001494static int
1495spufs_psmap_release(struct inode *inode, struct file *file)
1496{
1497 struct spufs_inode_info *i = SPUFS_I(inode);
1498 struct spu_context *ctx = i->i_ctx;
1499
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001500 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001501 if (!--i->i_openers)
1502 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001503 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001504 return 0;
1505}
1506
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001507static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001508 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001509 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001510 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001511};
1512
1513
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001514#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001515static int
1516spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001517{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001518 return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +01001519}
1520
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001521static const struct vm_operations_struct spufs_mfc_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001522 .fault = spufs_mfc_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001523};
1524
1525/*
1526 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001527 */
1528static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1529{
1530 if (!(vma->vm_flags & VM_SHARED))
1531 return -EINVAL;
1532
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001533 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001534 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001535
1536 vma->vm_ops = &spufs_mfc_mmap_vmops;
1537 return 0;
1538}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001539#else /* SPUFS_MMAP_4K */
1540#define spufs_mfc_mmap NULL
1541#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001542
1543static int spufs_mfc_open(struct inode *inode, struct file *file)
1544{
1545 struct spufs_inode_info *i = SPUFS_I(inode);
1546 struct spu_context *ctx = i->i_ctx;
1547
1548 /* we don't want to deal with DMA into other processes */
1549 if (ctx->owner != current->mm)
1550 return -EINVAL;
1551
1552 if (atomic_read(&inode->i_count) != 1)
1553 return -EBUSY;
1554
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001555 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001556 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001557 if (!i->i_openers++)
1558 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001559 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001560 return nonseekable_open(inode, file);
1561}
1562
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001563static int
1564spufs_mfc_release(struct inode *inode, struct file *file)
1565{
1566 struct spufs_inode_info *i = SPUFS_I(inode);
1567 struct spu_context *ctx = i->i_ctx;
1568
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001569 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001570 if (!--i->i_openers)
1571 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001572 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001573 return 0;
1574}
1575
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001576/* interrupt-level mfc callback function. */
1577void spufs_mfc_callback(struct spu *spu)
1578{
1579 struct spu_context *ctx = spu->ctx;
1580
Luke Browninge65c2f62007-12-20 16:39:59 +09001581 if (!ctx)
1582 return;
1583
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001584 wake_up_all(&ctx->mfc_wq);
1585
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001586 pr_debug("%s %s\n", __func__, spu->name);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001587 if (ctx->mfc_fasync) {
1588 u32 free_elements, tagstatus;
1589 unsigned int mask;
1590
1591 /* no need for spu_acquire in interrupt context */
1592 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1593 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1594
1595 mask = 0;
1596 if (free_elements & 0xffff)
1597 mask |= POLLOUT;
1598 if (tagstatus & ctx->tagwait)
1599 mask |= POLLIN;
1600
1601 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1602 }
1603}
1604
1605static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1606{
1607 /* See if there is one tag group is complete */
1608 /* FIXME we need locking around tagwait */
1609 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1610 ctx->tagwait &= ~*status;
1611 if (*status)
1612 return 1;
1613
1614 /* enable interrupt waiting for any tag group,
1615 may silently fail if interrupts are already enabled */
1616 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1617 return 0;
1618}
1619
1620static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1621 size_t size, loff_t *pos)
1622{
1623 struct spu_context *ctx = file->private_data;
1624 int ret = -EINVAL;
1625 u32 status;
1626
1627 if (size != 4)
1628 goto out;
1629
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001630 ret = spu_acquire(ctx);
1631 if (ret)
1632 return ret;
1633
1634 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001635 if (file->f_flags & O_NONBLOCK) {
1636 status = ctx->ops->read_mfc_tagstatus(ctx);
1637 if (!(status & ctx->tagwait))
1638 ret = -EAGAIN;
1639 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001640 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001641 ctx->tagwait &= ~status;
1642 } else {
1643 ret = spufs_wait(ctx->mfc_wq,
1644 spufs_read_mfc_tagstatus(ctx, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001645 if (ret)
1646 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001647 }
1648 spu_release(ctx);
1649
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001650 ret = 4;
1651 if (copy_to_user(buffer, &status, 4))
1652 ret = -EFAULT;
1653
1654out:
1655 return ret;
1656}
1657
1658static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1659{
Stephen Rothwell9477e452009-01-06 14:27:38 +00001660 pr_debug("queueing DMA %x %llx %x %x %x\n", cmd->lsa,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001661 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1662
1663 switch (cmd->cmd) {
1664 case MFC_PUT_CMD:
1665 case MFC_PUTF_CMD:
1666 case MFC_PUTB_CMD:
1667 case MFC_GET_CMD:
1668 case MFC_GETF_CMD:
1669 case MFC_GETB_CMD:
1670 break;
1671 default:
1672 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1673 return -EIO;
1674 }
1675
1676 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
Stephen Rothwell9477e452009-01-06 14:27:38 +00001677 pr_debug("invalid DMA alignment, ea %llx lsa %x\n",
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001678 cmd->ea, cmd->lsa);
1679 return -EIO;
1680 }
1681
1682 switch (cmd->size & 0xf) {
1683 case 1:
1684 break;
1685 case 2:
1686 if (cmd->lsa & 1)
1687 goto error;
1688 break;
1689 case 4:
1690 if (cmd->lsa & 3)
1691 goto error;
1692 break;
1693 case 8:
1694 if (cmd->lsa & 7)
1695 goto error;
1696 break;
1697 case 0:
1698 if (cmd->lsa & 15)
1699 goto error;
1700 break;
1701 error:
1702 default:
1703 pr_debug("invalid DMA alignment %x for size %x\n",
1704 cmd->lsa & 0xf, cmd->size);
1705 return -EIO;
1706 }
1707
1708 if (cmd->size > 16 * 1024) {
1709 pr_debug("invalid DMA size %x\n", cmd->size);
1710 return -EIO;
1711 }
1712
1713 if (cmd->tag & 0xfff0) {
1714 /* we reserve the higher tag numbers for kernel use */
1715 pr_debug("invalid DMA tag\n");
1716 return -EIO;
1717 }
1718
1719 if (cmd->class) {
1720 /* not supported in this version */
1721 pr_debug("invalid DMA class\n");
1722 return -EIO;
1723 }
1724
1725 return 0;
1726}
1727
1728static int spu_send_mfc_command(struct spu_context *ctx,
1729 struct mfc_dma_command cmd,
1730 int *error)
1731{
1732 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1733 if (*error == -EAGAIN) {
1734 /* wait for any tag group to complete
1735 so we have space for the new command */
1736 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1737 /* try again, because the queue might be
1738 empty again */
1739 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1740 if (*error == -EAGAIN)
1741 return 0;
1742 }
1743 return 1;
1744}
1745
1746static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1747 size_t size, loff_t *pos)
1748{
1749 struct spu_context *ctx = file->private_data;
1750 struct mfc_dma_command cmd;
1751 int ret = -EINVAL;
1752
1753 if (size != sizeof cmd)
1754 goto out;
1755
1756 ret = -EFAULT;
1757 if (copy_from_user(&cmd, buffer, sizeof cmd))
1758 goto out;
1759
1760 ret = spufs_check_valid_dma(&cmd);
1761 if (ret)
1762 goto out;
1763
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001764 ret = spu_acquire(ctx);
1765 if (ret)
1766 goto out;
1767
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001768 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001769 if (ret)
1770 goto out;
1771
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001772 if (file->f_flags & O_NONBLOCK) {
1773 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1774 } else {
1775 int status;
1776 ret = spufs_wait(ctx->mfc_wq,
1777 spu_send_mfc_command(ctx, cmd, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001778 if (ret)
1779 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001780 if (status)
1781 ret = status;
1782 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001783
1784 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001785 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001786
1787 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001788 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001789
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001790out_unlock:
1791 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001792out:
1793 return ret;
1794}
1795
1796static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1797{
1798 struct spu_context *ctx = file->private_data;
1799 u32 free_elements, tagstatus;
1800 unsigned int mask;
1801
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001802 poll_wait(file, &ctx->mfc_wq, wait);
1803
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001804 /*
1805 * For now keep this uninterruptible and also ignore the rule
1806 * that poll should not sleep. Will be fixed later.
1807 */
1808 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001809 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1810 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1811 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1812 spu_release(ctx);
1813
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001814 mask = 0;
1815 if (free_elements & 0xffff)
1816 mask |= POLLOUT | POLLWRNORM;
1817 if (tagstatus & ctx->tagwait)
1818 mask |= POLLIN | POLLRDNORM;
1819
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001820 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001821 free_elements, tagstatus, ctx->tagwait);
1822
1823 return mask;
1824}
1825
Al Viro73b6af82006-06-25 16:42:33 -07001826static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001827{
1828 struct spu_context *ctx = file->private_data;
1829 int ret;
1830
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001831 ret = spu_acquire(ctx);
1832 if (ret)
Christoph Hellwigeebead52008-02-08 15:50:41 +11001833 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001834#if 0
1835/* this currently hangs */
1836 ret = spufs_wait(ctx->mfc_wq,
1837 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1838 if (ret)
1839 goto out;
1840 ret = spufs_wait(ctx->mfc_wq,
1841 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001842 if (ret)
1843 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001844#else
1845 ret = 0;
1846#endif
1847 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001848out:
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001849 return ret;
1850}
1851
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001852static int spufs_mfc_fsync(struct file *file, int datasync)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001853{
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
roel kluin17e37672009-10-14 05:32:28 +00002497 if (!buf)
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002498 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};