blob: 3fcd06418b01d165ba1d01f7d9930608d6d15c6d [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
2 * SPU file system -- file contents
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
Arnd Bergmanna33a7d72006-03-23 00:00:11 +010023#undef DEBUG
24
Arnd Bergmann67207b92005-11-15 15:53:48 -050025#include <linux/fs.h>
26#include <linux/ioctl.h>
27#include <linux/module.h>
Arnd Bergmannd88cfff2005-12-05 22:52:22 -050028#include <linux/pagemap.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050029#include <linux/poll.h>
Arnd Bergmann51104592005-12-05 22:52:25 -050030#include <linux/ptrace.h>
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +100031#include <linux/seq_file.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050032
33#include <asm/io.h>
34#include <asm/semaphore.h>
35#include <asm/spu.h>
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +010036#include <asm/spu_info.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050037#include <asm/uaccess.h>
38
39#include "spufs.h"
40
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +020041#define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
42
Christoph Hellwig197b1a82007-12-20 16:39:59 +090043/* Simple attribute files */
44struct spufs_attr {
45 int (*get)(void *, u64 *);
46 int (*set)(void *, u64);
47 char get_buf[24]; /* enough to store a u64 and "\n\0" */
48 char set_buf[24];
49 void *data;
50 const char *fmt; /* format for read operation */
51 struct mutex mutex; /* protects access to these buffers */
52};
53
54static int spufs_attr_open(struct inode *inode, struct file *file,
55 int (*get)(void *, u64 *), int (*set)(void *, u64),
56 const char *fmt)
57{
58 struct spufs_attr *attr;
59
60 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
61 if (!attr)
62 return -ENOMEM;
63
64 attr->get = get;
65 attr->set = set;
66 attr->data = inode->i_private;
67 attr->fmt = fmt;
68 mutex_init(&attr->mutex);
69 file->private_data = attr;
70
71 return nonseekable_open(inode, file);
72}
73
74static int spufs_attr_release(struct inode *inode, struct file *file)
75{
76 kfree(file->private_data);
77 return 0;
78}
79
80static ssize_t spufs_attr_read(struct file *file, char __user *buf,
81 size_t len, loff_t *ppos)
82{
83 struct spufs_attr *attr;
84 size_t size;
85 ssize_t ret;
86
87 attr = file->private_data;
88 if (!attr->get)
89 return -EACCES;
90
91 ret = mutex_lock_interruptible(&attr->mutex);
92 if (ret)
93 return ret;
94
95 if (*ppos) { /* continued read */
96 size = strlen(attr->get_buf);
97 } else { /* first read */
98 u64 val;
99 ret = attr->get(attr->data, &val);
100 if (ret)
101 goto out;
102
103 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
104 attr->fmt, (unsigned long long)val);
105 }
106
107 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
108out:
109 mutex_unlock(&attr->mutex);
110 return ret;
111}
112
113static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
114 size_t len, loff_t *ppos)
115{
116 struct spufs_attr *attr;
117 u64 val;
118 size_t size;
119 ssize_t ret;
120
121 attr = file->private_data;
122 if (!attr->set)
123 return -EACCES;
124
125 ret = mutex_lock_interruptible(&attr->mutex);
126 if (ret)
127 return ret;
128
129 ret = -EFAULT;
130 size = min(sizeof(attr->set_buf) - 1, len);
131 if (copy_from_user(attr->set_buf, buf, size))
132 goto out;
133
134 ret = len; /* claim we got the whole input */
135 attr->set_buf[size] = '\0';
136 val = simple_strtol(attr->set_buf, NULL, 0);
137 attr->set(attr->data, val);
138out:
139 mutex_unlock(&attr->mutex);
140 return ret;
141}
142
143#define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
144static int __fops ## _open(struct inode *inode, struct file *file) \
145{ \
146 __simple_attr_check_format(__fmt, 0ull); \
147 return spufs_attr_open(inode, file, __get, __set, __fmt); \
148} \
149static struct file_operations __fops = { \
150 .owner = THIS_MODULE, \
151 .open = __fops ## _open, \
152 .release = spufs_attr_release, \
153 .read = spufs_attr_read, \
154 .write = spufs_attr_write, \
155};
156
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +1000157
Arnd Bergmann67207b92005-11-15 15:53:48 -0500158static int
159spufs_mem_open(struct inode *inode, struct file *file)
160{
161 struct spufs_inode_info *i = SPUFS_I(inode);
Mark Nutter6df10a82006-03-23 00:00:12 +0100162 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200163
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000164 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100165 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200166 if (!i->i_openers++)
167 ctx->local_store = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000168 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200169 return 0;
170}
171
172static int
173spufs_mem_release(struct inode *inode, struct file *file)
174{
175 struct spufs_inode_info *i = SPUFS_I(inode);
176 struct spu_context *ctx = i->i_ctx;
177
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000178 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200179 if (!--i->i_openers)
180 ctx->local_store = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000181 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500182 return 0;
183}
184
185static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100186__spufs_mem_read(struct spu_context *ctx, char __user *buffer,
187 size_t size, loff_t *pos)
188{
189 char *local_store = ctx->ops->get_ls(ctx);
190 return simple_read_from_buffer(buffer, size, pos, local_store,
191 LS_SIZE);
192}
193
194static ssize_t
Arnd Bergmann67207b92005-11-15 15:53:48 -0500195spufs_mem_read(struct file *file, char __user *buffer,
196 size_t size, loff_t *pos)
197{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100198 struct spu_context *ctx = file->private_data;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100199 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500200
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900201 ret = spu_acquire(ctx);
202 if (ret)
203 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100204 ret = __spufs_mem_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500205 spu_release(ctx);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900206
Arnd Bergmann67207b92005-11-15 15:53:48 -0500207 return ret;
208}
209
210static ssize_t
211spufs_mem_write(struct file *file, const char __user *buffer,
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100212 size_t size, loff_t *ppos)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500213{
214 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500215 char *local_store;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100216 loff_t pos = *ppos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500217 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500218
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100219 if (pos < 0)
220 return -EINVAL;
221 if (pos > LS_SIZE)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500222 return -EFBIG;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100223 if (size > LS_SIZE - pos)
224 size = LS_SIZE - pos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500225
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900226 ret = spu_acquire(ctx);
227 if (ret)
228 return ret;
229
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500230 local_store = ctx->ops->get_ls(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100231 ret = copy_from_user(local_store + pos, buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500232 spu_release(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100233
234 if (ret)
235 return -EFAULT;
236 *ppos = pos + size;
237 return size;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500238}
239
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100240static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
241 unsigned long address)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500242{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000243 struct spu_context *ctx = vma->vm_file->private_data;
244 unsigned long pfn, offset, addr0 = address;
245#ifdef CONFIG_SPU_FS_64K_LS
246 struct spu_state *csa = &ctx->csa;
247 int psize;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100248
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000249 /* Check what page size we are using */
250 psize = get_slice_psize(vma->vm_mm, address);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500251
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000252 /* Some sanity checking */
253 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
254
255 /* Wow, 64K, cool, we need to align the address though */
256 if (csa->use_big_pages) {
257 BUG_ON(vma->vm_start & 0xffff);
258 address &= ~0xfffful;
259 }
260#endif /* CONFIG_SPU_FS_64K_LS */
261
262 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
Masato Noguchi128b8542007-02-13 21:54:30 +0100263 if (offset >= LS_SIZE)
264 return NOPFN_SIGBUS;
265
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000266 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
267 addr0, address, offset);
268
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900269 if (spu_acquire(ctx))
270 return NOPFN_REFAULT;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500271
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200272 if (ctx->state == SPU_STATE_SAVED) {
273 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Arnd Bergmann932f535d2006-11-20 18:45:06 +0100274 & ~_PAGE_NO_CACHE);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100275 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200276 } else {
277 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100278 | _PAGE_NO_CACHE);
279 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200280 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100281 vm_insert_pfn(vma, address, pfn);
282
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500283 spu_release(ctx);
284
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100285 return NOPFN_REFAULT;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500286}
287
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100288
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500289static struct vm_operations_struct spufs_mem_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100290 .nopfn = spufs_mem_mmap_nopfn,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500291};
292
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000293static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500294{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000295#ifdef CONFIG_SPU_FS_64K_LS
296 struct spu_context *ctx = file->private_data;
297 struct spu_state *csa = &ctx->csa;
298
299 /* Sanity check VMA alignment */
300 if (csa->use_big_pages) {
301 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
302 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
303 vma->vm_pgoff);
304 if (vma->vm_start & 0xffff)
305 return -EINVAL;
306 if (vma->vm_pgoff & 0xf)
307 return -EINVAL;
308 }
309#endif /* CONFIG_SPU_FS_64K_LS */
310
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500311 if (!(vma->vm_flags & VM_SHARED))
312 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500313
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100314 vma->vm_flags |= VM_IO | VM_PFNMAP;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500315 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
316 | _PAGE_NO_CACHE);
317
318 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500319 return 0;
320}
321
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000322#ifdef CONFIG_SPU_FS_64K_LS
Sebastian Siewior12388192007-09-19 14:38:12 +1000323static unsigned long spufs_get_unmapped_area(struct file *file,
324 unsigned long addr, unsigned long len, unsigned long pgoff,
325 unsigned long flags)
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000326{
327 struct spu_context *ctx = file->private_data;
328 struct spu_state *csa = &ctx->csa;
329
330 /* If not using big pages, fallback to normal MM g_u_a */
331 if (!csa->use_big_pages)
332 return current->mm->get_unmapped_area(file, addr, len,
333 pgoff, flags);
334
335 /* Else, try to obtain a 64K pages slice */
336 return slice_get_unmapped_area(addr, len, flags,
337 MMU_PAGE_64K, 1, 0);
338}
339#endif /* CONFIG_SPU_FS_64K_LS */
340
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800341static const struct file_operations spufs_mem_fops = {
Jeremy Kerr70225432007-06-29 10:58:00 +1000342 .open = spufs_mem_open,
343 .release = spufs_mem_release,
344 .read = spufs_mem_read,
345 .write = spufs_mem_write,
346 .llseek = generic_file_llseek,
347 .mmap = spufs_mem_mmap,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000348#ifdef CONFIG_SPU_FS_64K_LS
349 .get_unmapped_area = spufs_get_unmapped_area,
350#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500351};
352
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100353static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
Mark Nutter6df10a82006-03-23 00:00:12 +0100354 unsigned long address,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100355 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200356 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100357{
Mark Nutter6df10a82006-03-23 00:00:12 +0100358 struct spu_context *ctx = vma->vm_file->private_data;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100359 unsigned long area, offset = address - vma->vm_start;
Mark Nutter6df10a82006-03-23 00:00:12 +0100360
361 offset += vma->vm_pgoff << PAGE_SHIFT;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200362 if (offset >= ps_size)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100363 return NOPFN_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100364
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900365 /*
366 * We have to wait for context to be loaded before we have
367 * pages to hand out to the user, but we don't want to wait
368 * with the mmap_sem held.
369 * It is possible to drop the mmap_sem here, but then we need
370 * to return NOPFN_REFAULT because the mappings may have
371 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100372 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900373 if (spu_acquire(ctx))
374 return NOPFN_REFAULT;
375
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900376 if (ctx->state == SPU_STATE_SAVED) {
377 up_read(&current->mm->mmap_sem);
378 spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
379 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900380 } else {
381 area = ctx->spu->problem_phys + ps_offs;
382 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900383 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100384
Mark Nutter6df10a82006-03-23 00:00:12 +0100385 spu_release(ctx);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100386 return NOPFN_REFAULT;
Mark Nutter6df10a82006-03-23 00:00:12 +0100387}
388
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200389#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100390static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
391 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +0100392{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100393 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +0100394}
395
396static struct vm_operations_struct spufs_cntl_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100397 .nopfn = spufs_cntl_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +0100398};
399
400/*
401 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100402 */
403static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
404{
405 if (!(vma->vm_flags & VM_SHARED))
406 return -EINVAL;
407
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100408 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100409 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200410 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100411
412 vma->vm_ops = &spufs_cntl_mmap_vmops;
413 return 0;
414}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200415#else /* SPUFS_MMAP_4K */
416#define spufs_cntl_mmap NULL
417#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100418
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900419static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200420{
421 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900422 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200423
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900424 ret = spu_acquire(ctx);
425 if (ret)
426 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900427 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200428 spu_release(ctx);
429
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900430 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200431}
432
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900433static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200434{
435 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900436 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200437
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900438 ret = spu_acquire(ctx);
439 if (ret)
440 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200441 ctx->ops->runcntl_write(ctx, val);
442 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900443
444 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200445}
446
Mark Nutter6df10a82006-03-23 00:00:12 +0100447static int spufs_cntl_open(struct inode *inode, struct file *file)
448{
449 struct spufs_inode_info *i = SPUFS_I(inode);
450 struct spu_context *ctx = i->i_ctx;
451
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000452 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100453 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200454 if (!i->i_openers++)
455 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000456 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900457 return spufs_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200458 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100459}
460
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200461static int
462spufs_cntl_release(struct inode *inode, struct file *file)
463{
464 struct spufs_inode_info *i = SPUFS_I(inode);
465 struct spu_context *ctx = i->i_ctx;
466
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900467 spufs_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200468
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000469 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200470 if (!--i->i_openers)
471 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000472 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200473 return 0;
474}
475
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800476static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100477 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200478 .release = spufs_cntl_release,
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900479 .read = spufs_attr_read,
480 .write = spufs_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100481 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100482};
483
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500484static int
485spufs_regs_open(struct inode *inode, struct file *file)
486{
487 struct spufs_inode_info *i = SPUFS_I(inode);
488 file->private_data = i->i_ctx;
489 return 0;
490}
491
492static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100493__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
494 size_t size, loff_t *pos)
495{
496 struct spu_lscsa *lscsa = ctx->csa.lscsa;
497 return simple_read_from_buffer(buffer, size, pos,
498 lscsa->gprs, sizeof lscsa->gprs);
499}
500
501static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500502spufs_regs_read(struct file *file, char __user *buffer,
503 size_t size, loff_t *pos)
504{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500505 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100506 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500507
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900508 ret = spu_acquire_saved(ctx);
509 if (ret)
510 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100511 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200512 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500513 return ret;
514}
515
516static ssize_t
517spufs_regs_write(struct file *file, const char __user *buffer,
518 size_t size, loff_t *pos)
519{
520 struct spu_context *ctx = file->private_data;
521 struct spu_lscsa *lscsa = ctx->csa.lscsa;
522 int ret;
523
524 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
525 if (size <= 0)
526 return -EFBIG;
527 *pos += size;
528
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900529 ret = spu_acquire_saved(ctx);
530 if (ret)
531 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500532
533 ret = copy_from_user(lscsa->gprs + *pos - size,
534 buffer, size) ? -EFAULT : size;
535
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200536 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500537 return ret;
538}
539
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800540static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500541 .open = spufs_regs_open,
542 .read = spufs_regs_read,
543 .write = spufs_regs_write,
544 .llseek = generic_file_llseek,
545};
546
547static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100548__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
549 size_t size, loff_t * pos)
550{
551 struct spu_lscsa *lscsa = ctx->csa.lscsa;
552 return simple_read_from_buffer(buffer, size, pos,
553 &lscsa->fpcr, sizeof(lscsa->fpcr));
554}
555
556static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500557spufs_fpcr_read(struct file *file, char __user * buffer,
558 size_t size, loff_t * pos)
559{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500560 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100561 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500562
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900563 ret = spu_acquire_saved(ctx);
564 if (ret)
565 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100566 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200567 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500568 return ret;
569}
570
571static ssize_t
572spufs_fpcr_write(struct file *file, const char __user * buffer,
573 size_t size, loff_t * pos)
574{
575 struct spu_context *ctx = file->private_data;
576 struct spu_lscsa *lscsa = ctx->csa.lscsa;
577 int ret;
578
579 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
580 if (size <= 0)
581 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900582
583 ret = spu_acquire_saved(ctx);
584 if (ret)
585 return ret;
586
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500587 *pos += size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500588 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
589 buffer, size) ? -EFAULT : size;
590
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200591 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500592 return ret;
593}
594
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800595static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500596 .open = spufs_regs_open,
597 .read = spufs_fpcr_read,
598 .write = spufs_fpcr_write,
599 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500600};
601
602/* generic open function for all pipe-like files */
603static int spufs_pipe_open(struct inode *inode, struct file *file)
604{
605 struct spufs_inode_info *i = SPUFS_I(inode);
606 file->private_data = i->i_ctx;
607
608 return nonseekable_open(inode, file);
609}
610
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200611/*
612 * Read as many bytes from the mailbox as possible, until
613 * one of the conditions becomes true:
614 *
615 * - no more data available in the mailbox
616 * - end of the user provided buffer
617 * - end of the mapped area
618 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500619static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
620 size_t len, loff_t *pos)
621{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500622 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200623 u32 mbox_data, __user *udata;
624 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500625
626 if (len < 4)
627 return -EINVAL;
628
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200629 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500630 return -EFAULT;
631
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200632 udata = (void __user *)buf;
633
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900634 count = spu_acquire(ctx);
635 if (count)
636 return count;
637
Arnd Bergmann274cef52006-10-24 18:01:42 +0200638 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200639 int ret;
640 ret = ctx->ops->mbox_read(ctx, &mbox_data);
641 if (ret == 0)
642 break;
643
644 /*
645 * at the end of the mapped area, we can fault
646 * but still need to return the data we have
647 * read successfully so far.
648 */
649 ret = __put_user(mbox_data, udata);
650 if (ret) {
651 if (!count)
652 count = -EFAULT;
653 break;
654 }
655 }
656 spu_release(ctx);
657
658 if (!count)
659 count = -EAGAIN;
660
661 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500662}
663
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800664static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500665 .open = spufs_pipe_open,
666 .read = spufs_mbox_read,
667};
668
669static ssize_t spufs_mbox_stat_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;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900673 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500674 u32 mbox_stat;
675
676 if (len < 4)
677 return -EINVAL;
678
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900679 ret = spu_acquire(ctx);
680 if (ret)
681 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500682
683 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
684
685 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500686
687 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
688 return -EFAULT;
689
690 return 4;
691}
692
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800693static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500694 .open = spufs_pipe_open,
695 .read = spufs_mbox_stat_read,
696};
697
698/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500699size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500700{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500701 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500702}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500703
704static int spufs_ibox_fasync(int fd, struct file *file, int on)
705{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500706 struct spu_context *ctx = file->private_data;
707
708 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
709}
710
711/* interrupt-level ibox callback function. */
712void spufs_ibox_callback(struct spu *spu)
713{
714 struct spu_context *ctx = spu->ctx;
715
Luke Browninge65c2f62007-12-20 16:39:59 +0900716 if (!ctx)
717 return;
718
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500719 wake_up_all(&ctx->ibox_wq);
720 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500721}
722
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200723/*
724 * Read as many bytes from the interrupt mailbox as possible, until
725 * one of the conditions becomes true:
726 *
727 * - no more data available in the mailbox
728 * - end of the user provided buffer
729 * - end of the mapped area
730 *
731 * If the file is opened without O_NONBLOCK, we wait here until
732 * any data is available, but return when we have been able to
733 * read something.
734 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500735static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
736 size_t len, loff_t *pos)
737{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500738 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200739 u32 ibox_data, __user *udata;
740 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500741
742 if (len < 4)
743 return -EINVAL;
744
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200745 if (!access_ok(VERIFY_WRITE, buf, len))
746 return -EFAULT;
747
748 udata = (void __user *)buf;
749
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900750 count = spu_acquire(ctx);
751 if (count)
752 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500753
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200754 /* wait only for the first element */
755 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500756 if (file->f_flags & O_NONBLOCK) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500757 if (!spu_ibox_read(ctx, &ibox_data))
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200758 count = -EAGAIN;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500759 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200760 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
761 }
762 if (count)
763 goto out;
764
765 /* if we can't write at all, return -EFAULT */
766 count = __put_user(ibox_data, udata);
767 if (count)
768 goto out;
769
770 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
771 int ret;
772 ret = ctx->ops->ibox_read(ctx, &ibox_data);
773 if (ret == 0)
774 break;
775 /*
776 * at the end of the mapped area, we can fault
777 * but still need to return the data we have
778 * read successfully so far.
779 */
780 ret = __put_user(ibox_data, udata);
781 if (ret)
782 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500783 }
784
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200785out:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500786 spu_release(ctx);
787
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200788 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500789}
790
791static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
792{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500793 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500794 unsigned int mask;
795
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500796 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500797
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900798 /*
799 * For now keep this uninterruptible and also ignore the rule
800 * that poll should not sleep. Will be fixed later.
801 */
802 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500803 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
804 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500805
806 return mask;
807}
808
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800809static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500810 .open = spufs_pipe_open,
811 .read = spufs_ibox_read,
812 .poll = spufs_ibox_poll,
813 .fasync = spufs_ibox_fasync,
814};
815
816static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
817 size_t len, loff_t *pos)
818{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500819 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900820 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500821 u32 ibox_stat;
822
823 if (len < 4)
824 return -EINVAL;
825
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900826 ret = spu_acquire(ctx);
827 if (ret)
828 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500829 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
830 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500831
832 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
833 return -EFAULT;
834
835 return 4;
836}
837
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800838static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500839 .open = spufs_pipe_open,
840 .read = spufs_ibox_stat_read,
841};
842
843/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500844size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500845{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500846 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500847}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500848
849static int spufs_wbox_fasync(int fd, struct file *file, int on)
850{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500851 struct spu_context *ctx = file->private_data;
852 int ret;
853
854 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
855
856 return ret;
857}
858
859/* interrupt-level wbox callback function. */
860void spufs_wbox_callback(struct spu *spu)
861{
862 struct spu_context *ctx = spu->ctx;
863
Luke Browninge65c2f62007-12-20 16:39:59 +0900864 if (!ctx)
865 return;
866
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500867 wake_up_all(&ctx->wbox_wq);
868 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500869}
870
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200871/*
872 * Write as many bytes to the interrupt mailbox as possible, until
873 * one of the conditions becomes true:
874 *
875 * - the mailbox is full
876 * - end of the user provided buffer
877 * - end of the mapped area
878 *
879 * If the file is opened without O_NONBLOCK, we wait here until
880 * space is availabyl, but return when we have been able to
881 * write something.
882 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500883static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
884 size_t len, loff_t *pos)
885{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500886 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200887 u32 wbox_data, __user *udata;
888 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500889
890 if (len < 4)
891 return -EINVAL;
892
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200893 udata = (void __user *)buf;
894 if (!access_ok(VERIFY_READ, buf, len))
895 return -EFAULT;
896
897 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500898 return -EFAULT;
899
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900900 count = spu_acquire(ctx);
901 if (count)
902 return count;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500903
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200904 /*
905 * make sure we can at least write one element, by waiting
906 * in case of !O_NONBLOCK
907 */
908 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500909 if (file->f_flags & O_NONBLOCK) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500910 if (!spu_wbox_write(ctx, wbox_data))
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200911 count = -EAGAIN;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500912 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200913 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Arnd Bergmann67207b92005-11-15 15:53:48 -0500914 }
915
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200916 if (count)
917 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500918
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200919 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200920 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
921 int ret;
922 ret = __get_user(wbox_data, udata);
923 if (ret)
924 break;
925
926 ret = spu_wbox_write(ctx, wbox_data);
927 if (ret == 0)
928 break;
929 }
930
931out:
932 spu_release(ctx);
933 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500934}
935
936static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
937{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500938 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500939 unsigned int mask;
940
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500941 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500942
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900943 /*
944 * For now keep this uninterruptible and also ignore the rule
945 * that poll should not sleep. Will be fixed later.
946 */
947 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500948 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
949 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500950
951 return mask;
952}
953
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800954static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500955 .open = spufs_pipe_open,
956 .write = spufs_wbox_write,
957 .poll = spufs_wbox_poll,
958 .fasync = spufs_wbox_fasync,
959};
960
961static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
962 size_t len, loff_t *pos)
963{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500964 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900965 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500966 u32 wbox_stat;
967
968 if (len < 4)
969 return -EINVAL;
970
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900971 ret = spu_acquire(ctx);
972 if (ret)
973 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500974 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
975 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500976
977 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
978 return -EFAULT;
979
980 return 4;
981}
982
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800983static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500984 .open = spufs_pipe_open,
985 .read = spufs_wbox_stat_read,
986};
987
Mark Nutter6df10a82006-03-23 00:00:12 +0100988static int spufs_signal1_open(struct inode *inode, struct file *file)
989{
990 struct spufs_inode_info *i = SPUFS_I(inode);
991 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200992
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000993 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100994 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200995 if (!i->i_openers++)
996 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000997 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100998 return nonseekable_open(inode, file);
999}
1000
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001001static int
1002spufs_signal1_release(struct inode *inode, struct file *file)
1003{
1004 struct spufs_inode_info *i = SPUFS_I(inode);
1005 struct spu_context *ctx = i->i_ctx;
1006
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001007 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001008 if (!--i->i_openers)
1009 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001010 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001011 return 0;
1012}
1013
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001014static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001015 size_t len, loff_t *pos)
1016{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001017 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001018 u32 data;
1019
Arnd Bergmann67207b92005-11-15 15:53:48 -05001020 if (len < 4)
1021 return -EINVAL;
1022
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001023 if (ctx->csa.spu_chnlcnt_RW[3]) {
1024 data = ctx->csa.spu_chnldata_RW[3];
1025 ret = 4;
1026 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001027
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001028 if (!ret)
1029 goto out;
1030
Arnd Bergmann67207b92005-11-15 15:53:48 -05001031 if (copy_to_user(buf, &data, 4))
1032 return -EFAULT;
1033
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001034out:
1035 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001036}
1037
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001038static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1039 size_t len, loff_t *pos)
1040{
1041 int ret;
1042 struct spu_context *ctx = file->private_data;
1043
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001044 ret = spu_acquire_saved(ctx);
1045 if (ret)
1046 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001047 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001048 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001049
1050 return ret;
1051}
1052
Arnd Bergmann67207b92005-11-15 15:53:48 -05001053static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1054 size_t len, loff_t *pos)
1055{
1056 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001057 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001058 u32 data;
1059
1060 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001061
1062 if (len < 4)
1063 return -EINVAL;
1064
1065 if (copy_from_user(&data, buf, 4))
1066 return -EFAULT;
1067
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001068 ret = spu_acquire(ctx);
1069 if (ret)
1070 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001071 ctx->ops->signal1_write(ctx, data);
1072 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001073
1074 return 4;
1075}
1076
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001077static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1078 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001079{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001080#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001081 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001082#elif PAGE_SIZE == 0x10000
1083 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1084 * signal 1 and 2 area
1085 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001086 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001087#else
1088#error unsupported page size
1089#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001090}
1091
1092static struct vm_operations_struct spufs_signal1_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001093 .nopfn = spufs_signal1_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001094};
1095
1096static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1097{
1098 if (!(vma->vm_flags & VM_SHARED))
1099 return -EINVAL;
1100
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001101 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001102 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001103 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001104
1105 vma->vm_ops = &spufs_signal1_mmap_vmops;
1106 return 0;
1107}
Mark Nutter6df10a82006-03-23 00:00:12 +01001108
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001109static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001110 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001111 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001112 .read = spufs_signal1_read,
1113 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001114 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001115};
1116
Jeremy Kerrd054b362007-07-20 21:39:31 +02001117static const struct file_operations spufs_signal1_nosched_fops = {
1118 .open = spufs_signal1_open,
1119 .release = spufs_signal1_release,
1120 .write = spufs_signal1_write,
1121 .mmap = spufs_signal1_mmap,
1122};
1123
Mark Nutter6df10a82006-03-23 00:00:12 +01001124static int spufs_signal2_open(struct inode *inode, struct file *file)
1125{
1126 struct spufs_inode_info *i = SPUFS_I(inode);
1127 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001128
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001129 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001130 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001131 if (!i->i_openers++)
1132 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001133 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001134 return nonseekable_open(inode, file);
1135}
1136
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001137static int
1138spufs_signal2_release(struct inode *inode, struct file *file)
1139{
1140 struct spufs_inode_info *i = SPUFS_I(inode);
1141 struct spu_context *ctx = i->i_ctx;
1142
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001143 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001144 if (!--i->i_openers)
1145 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001146 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001147 return 0;
1148}
1149
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001150static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001151 size_t len, loff_t *pos)
1152{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001153 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001154 u32 data;
1155
Arnd Bergmann67207b92005-11-15 15:53:48 -05001156 if (len < 4)
1157 return -EINVAL;
1158
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001159 if (ctx->csa.spu_chnlcnt_RW[4]) {
1160 data = ctx->csa.spu_chnldata_RW[4];
1161 ret = 4;
1162 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001163
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001164 if (!ret)
1165 goto out;
1166
Arnd Bergmann67207b92005-11-15 15:53:48 -05001167 if (copy_to_user(buf, &data, 4))
1168 return -EFAULT;
1169
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001170out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001171 return ret;
1172}
1173
1174static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1175 size_t len, loff_t *pos)
1176{
1177 struct spu_context *ctx = file->private_data;
1178 int ret;
1179
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001180 ret = spu_acquire_saved(ctx);
1181 if (ret)
1182 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001183 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001184 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001185
1186 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001187}
1188
1189static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1190 size_t len, loff_t *pos)
1191{
1192 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001193 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001194 u32 data;
1195
1196 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001197
1198 if (len < 4)
1199 return -EINVAL;
1200
1201 if (copy_from_user(&data, buf, 4))
1202 return -EFAULT;
1203
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001204 ret = spu_acquire(ctx);
1205 if (ret)
1206 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001207 ctx->ops->signal2_write(ctx, data);
1208 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001209
1210 return 4;
1211}
1212
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001213#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001214static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1215 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001216{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001217#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001218 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001219#elif PAGE_SIZE == 0x10000
1220 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1221 * signal 1 and 2 area
1222 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001223 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001224#else
1225#error unsupported page size
1226#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001227}
1228
1229static struct vm_operations_struct spufs_signal2_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001230 .nopfn = spufs_signal2_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001231};
1232
1233static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1234{
1235 if (!(vma->vm_flags & VM_SHARED))
1236 return -EINVAL;
1237
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001238 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001239 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001240 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001241
1242 vma->vm_ops = &spufs_signal2_mmap_vmops;
1243 return 0;
1244}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001245#else /* SPUFS_MMAP_4K */
1246#define spufs_signal2_mmap NULL
1247#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001248
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001249static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001250 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001251 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001252 .read = spufs_signal2_read,
1253 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001254 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001255};
1256
Jeremy Kerrd054b362007-07-20 21:39:31 +02001257static const struct file_operations spufs_signal2_nosched_fops = {
1258 .open = spufs_signal2_open,
1259 .release = spufs_signal2_release,
1260 .write = spufs_signal2_write,
1261 .mmap = spufs_signal2_mmap,
1262};
1263
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001264/*
1265 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1266 * work of acquiring (or not) the SPU context before calling through
1267 * to the actual get routine. The set routine is called directly.
1268 */
1269#define SPU_ATTR_NOACQUIRE 0
1270#define SPU_ATTR_ACQUIRE 1
1271#define SPU_ATTR_ACQUIRE_SAVED 2
1272
1273#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001274static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001275{ \
1276 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001277 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001278 \
1279 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001280 ret = spu_acquire(ctx); \
1281 if (ret) \
1282 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001283 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001284 spu_release(ctx); \
1285 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001286 ret = spu_acquire_saved(ctx); \
1287 if (ret) \
1288 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001289 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001290 spu_release_saved(ctx); \
1291 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001292 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001293 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001294 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001295} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001296DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001297
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001298static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001299{
1300 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001301 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001302
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001303 ret = spu_acquire(ctx);
1304 if (ret)
1305 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001306 ctx->ops->signal1_type_set(ctx, val);
1307 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001308
1309 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001310}
1311
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001312static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001313{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001314 return ctx->ops->signal1_type_get(ctx);
1315}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001316DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1317 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001318
Arnd Bergmann67207b92005-11-15 15:53:48 -05001319
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001320static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001321{
1322 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001323 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001324
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001325 ret = spu_acquire(ctx);
1326 if (ret)
1327 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001328 ctx->ops->signal2_type_set(ctx, val);
1329 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001330
1331 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001332}
1333
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001334static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001335{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001336 return ctx->ops->signal2_type_get(ctx);
1337}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001338DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1339 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001340
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001341#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001342static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1343 unsigned long address)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001344{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001345 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001346}
1347
1348static struct vm_operations_struct spufs_mss_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001349 .nopfn = spufs_mss_mmap_nopfn,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001350};
1351
1352/*
1353 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001354 */
1355static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1356{
1357 if (!(vma->vm_flags & VM_SHARED))
1358 return -EINVAL;
1359
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001360 vma->vm_flags |= VM_IO | VM_PFNMAP;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001361 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001362 | _PAGE_NO_CACHE | _PAGE_GUARDED);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001363
1364 vma->vm_ops = &spufs_mss_mmap_vmops;
1365 return 0;
1366}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001367#else /* SPUFS_MMAP_4K */
1368#define spufs_mss_mmap NULL
1369#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001370
1371static int spufs_mss_open(struct inode *inode, struct file *file)
1372{
1373 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001374 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001375
1376 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001377
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001378 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001379 if (!i->i_openers++)
1380 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001381 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001382 return nonseekable_open(inode, file);
1383}
1384
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001385static int
1386spufs_mss_release(struct inode *inode, struct file *file)
1387{
1388 struct spufs_inode_info *i = SPUFS_I(inode);
1389 struct spu_context *ctx = i->i_ctx;
1390
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001391 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001392 if (!--i->i_openers)
1393 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001394 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001395 return 0;
1396}
1397
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001398static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001399 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001400 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001401 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001402};
1403
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001404static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1405 unsigned long address)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001406{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001407 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001408}
1409
1410static struct vm_operations_struct spufs_psmap_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001411 .nopfn = spufs_psmap_mmap_nopfn,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001412};
1413
1414/*
1415 * mmap support for full problem state area [0x00000 - 0x1ffff].
1416 */
1417static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1418{
1419 if (!(vma->vm_flags & VM_SHARED))
1420 return -EINVAL;
1421
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001422 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001423 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1424 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1425
1426 vma->vm_ops = &spufs_psmap_mmap_vmops;
1427 return 0;
1428}
1429
1430static int spufs_psmap_open(struct inode *inode, struct file *file)
1431{
1432 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001433 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001434
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001435 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001436 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001437 if (!i->i_openers++)
1438 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001439 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001440 return nonseekable_open(inode, file);
1441}
1442
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001443static int
1444spufs_psmap_release(struct inode *inode, struct file *file)
1445{
1446 struct spufs_inode_info *i = SPUFS_I(inode);
1447 struct spu_context *ctx = i->i_ctx;
1448
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001449 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001450 if (!--i->i_openers)
1451 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001452 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001453 return 0;
1454}
1455
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001456static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001457 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001458 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001459 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001460};
1461
1462
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001463#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001464static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1465 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001466{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001467 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +01001468}
1469
1470static struct vm_operations_struct spufs_mfc_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001471 .nopfn = spufs_mfc_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001472};
1473
1474/*
1475 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001476 */
1477static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1478{
1479 if (!(vma->vm_flags & VM_SHARED))
1480 return -EINVAL;
1481
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001482 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001483 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001484 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001485
1486 vma->vm_ops = &spufs_mfc_mmap_vmops;
1487 return 0;
1488}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001489#else /* SPUFS_MMAP_4K */
1490#define spufs_mfc_mmap NULL
1491#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001492
1493static int spufs_mfc_open(struct inode *inode, struct file *file)
1494{
1495 struct spufs_inode_info *i = SPUFS_I(inode);
1496 struct spu_context *ctx = i->i_ctx;
1497
1498 /* we don't want to deal with DMA into other processes */
1499 if (ctx->owner != current->mm)
1500 return -EINVAL;
1501
1502 if (atomic_read(&inode->i_count) != 1)
1503 return -EBUSY;
1504
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001505 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001506 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001507 if (!i->i_openers++)
1508 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001509 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001510 return nonseekable_open(inode, file);
1511}
1512
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001513static int
1514spufs_mfc_release(struct inode *inode, struct file *file)
1515{
1516 struct spufs_inode_info *i = SPUFS_I(inode);
1517 struct spu_context *ctx = i->i_ctx;
1518
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001519 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001520 if (!--i->i_openers)
1521 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001522 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001523 return 0;
1524}
1525
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001526/* interrupt-level mfc callback function. */
1527void spufs_mfc_callback(struct spu *spu)
1528{
1529 struct spu_context *ctx = spu->ctx;
1530
Luke Browninge65c2f62007-12-20 16:39:59 +09001531 if (!ctx)
1532 return;
1533
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001534 wake_up_all(&ctx->mfc_wq);
1535
1536 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1537 if (ctx->mfc_fasync) {
1538 u32 free_elements, tagstatus;
1539 unsigned int mask;
1540
1541 /* no need for spu_acquire in interrupt context */
1542 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1543 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1544
1545 mask = 0;
1546 if (free_elements & 0xffff)
1547 mask |= POLLOUT;
1548 if (tagstatus & ctx->tagwait)
1549 mask |= POLLIN;
1550
1551 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1552 }
1553}
1554
1555static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1556{
1557 /* See if there is one tag group is complete */
1558 /* FIXME we need locking around tagwait */
1559 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1560 ctx->tagwait &= ~*status;
1561 if (*status)
1562 return 1;
1563
1564 /* enable interrupt waiting for any tag group,
1565 may silently fail if interrupts are already enabled */
1566 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1567 return 0;
1568}
1569
1570static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1571 size_t size, loff_t *pos)
1572{
1573 struct spu_context *ctx = file->private_data;
1574 int ret = -EINVAL;
1575 u32 status;
1576
1577 if (size != 4)
1578 goto out;
1579
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001580 ret = spu_acquire(ctx);
1581 if (ret)
1582 return ret;
1583
1584 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001585 if (file->f_flags & O_NONBLOCK) {
1586 status = ctx->ops->read_mfc_tagstatus(ctx);
1587 if (!(status & ctx->tagwait))
1588 ret = -EAGAIN;
1589 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001590 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001591 ctx->tagwait &= ~status;
1592 } else {
1593 ret = spufs_wait(ctx->mfc_wq,
1594 spufs_read_mfc_tagstatus(ctx, &status));
1595 }
1596 spu_release(ctx);
1597
1598 if (ret)
1599 goto out;
1600
1601 ret = 4;
1602 if (copy_to_user(buffer, &status, 4))
1603 ret = -EFAULT;
1604
1605out:
1606 return ret;
1607}
1608
1609static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1610{
1611 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1612 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1613
1614 switch (cmd->cmd) {
1615 case MFC_PUT_CMD:
1616 case MFC_PUTF_CMD:
1617 case MFC_PUTB_CMD:
1618 case MFC_GET_CMD:
1619 case MFC_GETF_CMD:
1620 case MFC_GETB_CMD:
1621 break;
1622 default:
1623 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1624 return -EIO;
1625 }
1626
1627 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1628 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1629 cmd->ea, cmd->lsa);
1630 return -EIO;
1631 }
1632
1633 switch (cmd->size & 0xf) {
1634 case 1:
1635 break;
1636 case 2:
1637 if (cmd->lsa & 1)
1638 goto error;
1639 break;
1640 case 4:
1641 if (cmd->lsa & 3)
1642 goto error;
1643 break;
1644 case 8:
1645 if (cmd->lsa & 7)
1646 goto error;
1647 break;
1648 case 0:
1649 if (cmd->lsa & 15)
1650 goto error;
1651 break;
1652 error:
1653 default:
1654 pr_debug("invalid DMA alignment %x for size %x\n",
1655 cmd->lsa & 0xf, cmd->size);
1656 return -EIO;
1657 }
1658
1659 if (cmd->size > 16 * 1024) {
1660 pr_debug("invalid DMA size %x\n", cmd->size);
1661 return -EIO;
1662 }
1663
1664 if (cmd->tag & 0xfff0) {
1665 /* we reserve the higher tag numbers for kernel use */
1666 pr_debug("invalid DMA tag\n");
1667 return -EIO;
1668 }
1669
1670 if (cmd->class) {
1671 /* not supported in this version */
1672 pr_debug("invalid DMA class\n");
1673 return -EIO;
1674 }
1675
1676 return 0;
1677}
1678
1679static int spu_send_mfc_command(struct spu_context *ctx,
1680 struct mfc_dma_command cmd,
1681 int *error)
1682{
1683 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1684 if (*error == -EAGAIN) {
1685 /* wait for any tag group to complete
1686 so we have space for the new command */
1687 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1688 /* try again, because the queue might be
1689 empty again */
1690 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1691 if (*error == -EAGAIN)
1692 return 0;
1693 }
1694 return 1;
1695}
1696
1697static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1698 size_t size, loff_t *pos)
1699{
1700 struct spu_context *ctx = file->private_data;
1701 struct mfc_dma_command cmd;
1702 int ret = -EINVAL;
1703
1704 if (size != sizeof cmd)
1705 goto out;
1706
1707 ret = -EFAULT;
1708 if (copy_from_user(&cmd, buffer, sizeof cmd))
1709 goto out;
1710
1711 ret = spufs_check_valid_dma(&cmd);
1712 if (ret)
1713 goto out;
1714
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001715 ret = spu_acquire(ctx);
1716 if (ret)
1717 goto out;
1718
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001719 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001720 if (ret)
1721 goto out;
1722
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001723 if (file->f_flags & O_NONBLOCK) {
1724 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1725 } else {
1726 int status;
1727 ret = spufs_wait(ctx->mfc_wq,
1728 spu_send_mfc_command(ctx, cmd, &status));
1729 if (status)
1730 ret = status;
1731 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001732
1733 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001734 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001735
1736 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001737 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001738
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001739out_unlock:
1740 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001741out:
1742 return ret;
1743}
1744
1745static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1746{
1747 struct spu_context *ctx = file->private_data;
1748 u32 free_elements, tagstatus;
1749 unsigned int mask;
1750
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001751 poll_wait(file, &ctx->mfc_wq, wait);
1752
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001753 /*
1754 * For now keep this uninterruptible and also ignore the rule
1755 * that poll should not sleep. Will be fixed later.
1756 */
1757 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001758 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1759 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1760 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1761 spu_release(ctx);
1762
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001763 mask = 0;
1764 if (free_elements & 0xffff)
1765 mask |= POLLOUT | POLLWRNORM;
1766 if (tagstatus & ctx->tagwait)
1767 mask |= POLLIN | POLLRDNORM;
1768
1769 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1770 free_elements, tagstatus, ctx->tagwait);
1771
1772 return mask;
1773}
1774
Al Viro73b6af82006-06-25 16:42:33 -07001775static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001776{
1777 struct spu_context *ctx = file->private_data;
1778 int ret;
1779
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001780 ret = spu_acquire(ctx);
1781 if (ret)
1782 return ret;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001783#if 0
1784/* this currently hangs */
1785 ret = spufs_wait(ctx->mfc_wq,
1786 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1787 if (ret)
1788 goto out;
1789 ret = spufs_wait(ctx->mfc_wq,
1790 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1791out:
1792#else
1793 ret = 0;
1794#endif
1795 spu_release(ctx);
1796
1797 return ret;
1798}
1799
1800static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1801 int datasync)
1802{
Al Viro73b6af82006-06-25 16:42:33 -07001803 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001804}
1805
1806static int spufs_mfc_fasync(int fd, struct file *file, int on)
1807{
1808 struct spu_context *ctx = file->private_data;
1809
1810 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1811}
1812
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001813static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001814 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001815 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001816 .read = spufs_mfc_read,
1817 .write = spufs_mfc_write,
1818 .poll = spufs_mfc_poll,
1819 .flush = spufs_mfc_flush,
1820 .fsync = spufs_mfc_fsync,
1821 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001822 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001823};
1824
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001825static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001826{
1827 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001828 int ret;
1829
1830 ret = spu_acquire(ctx);
1831 if (ret)
1832 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001833 ctx->ops->npc_write(ctx, val);
1834 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001835
1836 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001837}
1838
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001839static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001840{
1841 return ctx->ops->npc_read(ctx);
1842}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001843DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1844 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001845
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001846static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001847{
1848 struct spu_context *ctx = data;
1849 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001850 int ret;
1851
1852 ret = spu_acquire_saved(ctx);
1853 if (ret)
1854 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001855 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001856 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001857
1858 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001859}
1860
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001861static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001862{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001863 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001864 return lscsa->decr.slot[0];
1865}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001866DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1867 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001868
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001869static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001870{
1871 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001872 int ret;
1873
1874 ret = spu_acquire_saved(ctx);
1875 if (ret)
1876 return ret;
Masato Noguchid40a01d2007-07-20 21:39:38 +02001877 if (val)
1878 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1879 else
1880 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001881 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001882
1883 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001884}
1885
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001886static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001887{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001888 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1889 return SPU_DECR_STATUS_RUNNING;
1890 else
1891 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001892}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001893DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1894 spufs_decr_status_set, "0x%llx\n",
1895 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001896
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001897static int spufs_event_mask_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->event_mask.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_event_mask_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->event_mask.slot[0];
1916}
1917
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001918DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1919 spufs_event_mask_set, "0x%llx\n",
1920 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001921
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001922static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001923{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001924 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001925 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001926 stat = state->spu_chnlcnt_RW[0];
1927 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001928 return state->spu_chnldata_RW[0];
1929 return 0;
1930}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001931DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1932 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001933
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001934static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001935{
1936 struct spu_context *ctx = data;
1937 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001938 int ret;
1939
1940 ret = spu_acquire_saved(ctx);
1941 if (ret)
1942 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001943 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001944 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001945
1946 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001947}
1948
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001949static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001950{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001951 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001952 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001953}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001954DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1955 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001956
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001957static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001958{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001959 u64 num;
1960
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001961 if (ctx->state == SPU_STATE_RUNNABLE)
1962 num = ctx->spu->number;
1963 else
1964 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001965
1966 return num;
1967}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001968DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1969 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001970
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001971static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001972{
1973 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001974 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001975}
1976
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001977static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02001978{
1979 struct spu_context *ctx = data;
1980 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001981
1982 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02001983}
1984
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001985DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1986 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02001987
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001988static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001989{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001990 return ctx->csa.priv2.spu_lslr_RW;
1991}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001992DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1993 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001994
1995static int spufs_info_open(struct inode *inode, struct file *file)
1996{
1997 struct spufs_inode_info *i = SPUFS_I(inode);
1998 struct spu_context *ctx = i->i_ctx;
1999 file->private_data = ctx;
2000 return 0;
2001}
2002
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002003static int spufs_caps_show(struct seq_file *s, void *private)
2004{
2005 struct spu_context *ctx = s->private;
2006
2007 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2008 seq_puts(s, "sched\n");
2009 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2010 seq_puts(s, "step\n");
2011 return 0;
2012}
2013
2014static int spufs_caps_open(struct inode *inode, struct file *file)
2015{
2016 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2017}
2018
2019static const struct file_operations spufs_caps_fops = {
2020 .open = spufs_caps_open,
2021 .read = seq_read,
2022 .llseek = seq_lseek,
2023 .release = single_release,
2024};
2025
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002026static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2027 char __user *buf, size_t len, loff_t *pos)
2028{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002029 u32 data;
2030
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002031 /* EOF if there's no entry in the mbox */
2032 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2033 return 0;
2034
2035 data = ctx->csa.prob.pu_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002036
2037 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2038}
2039
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002040static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2041 size_t len, loff_t *pos)
2042{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002043 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002044 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002045
2046 if (!access_ok(VERIFY_WRITE, buf, len))
2047 return -EFAULT;
2048
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002049 ret = spu_acquire_saved(ctx);
2050 if (ret)
2051 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002052 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002053 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002054 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002055 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002056
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002057 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002058}
2059
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002060static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002061 .open = spufs_info_open,
2062 .read = spufs_mbox_info_read,
2063 .llseek = generic_file_llseek,
2064};
2065
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002066static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2067 char __user *buf, size_t len, loff_t *pos)
2068{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002069 u32 data;
2070
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002071 /* EOF if there's no entry in the ibox */
2072 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2073 return 0;
2074
2075 data = ctx->csa.priv2.puint_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002076
2077 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2078}
2079
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002080static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2081 size_t len, loff_t *pos)
2082{
2083 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002084 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002085
2086 if (!access_ok(VERIFY_WRITE, buf, len))
2087 return -EFAULT;
2088
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002089 ret = spu_acquire_saved(ctx);
2090 if (ret)
2091 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002092 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002093 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002094 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002095 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002096
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002097 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002098}
2099
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002100static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002101 .open = spufs_info_open,
2102 .read = spufs_ibox_info_read,
2103 .llseek = generic_file_llseek,
2104};
2105
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002106static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2107 char __user *buf, size_t len, loff_t *pos)
2108{
2109 int i, cnt;
2110 u32 data[4];
2111 u32 wbox_stat;
2112
2113 wbox_stat = ctx->csa.prob.mb_stat_R;
2114 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2115 for (i = 0; i < cnt; i++) {
2116 data[i] = ctx->csa.spu_mailbox_data[i];
2117 }
2118
2119 return simple_read_from_buffer(buf, len, pos, &data,
2120 cnt * sizeof(u32));
2121}
2122
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002123static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2124 size_t len, loff_t *pos)
2125{
2126 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002127 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002128
2129 if (!access_ok(VERIFY_WRITE, buf, len))
2130 return -EFAULT;
2131
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002132 ret = spu_acquire_saved(ctx);
2133 if (ret)
2134 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002135 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002136 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002137 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002138 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002139
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002140 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002141}
2142
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002143static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002144 .open = spufs_info_open,
2145 .read = spufs_wbox_info_read,
2146 .llseek = generic_file_llseek,
2147};
2148
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002149static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2150 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002151{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002152 struct spu_dma_info info;
2153 struct mfc_cq_sr *qp, *spuqp;
2154 int i;
2155
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002156 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2157 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2158 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2159 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2160 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2161 for (i = 0; i < 16; i++) {
2162 qp = &info.dma_info_command_data[i];
2163 spuqp = &ctx->csa.priv2.spuq[i];
2164
2165 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2166 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2167 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2168 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2169 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002170
2171 return simple_read_from_buffer(buf, len, pos, &info,
2172 sizeof info);
2173}
2174
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002175static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2176 size_t len, loff_t *pos)
2177{
2178 struct spu_context *ctx = file->private_data;
2179 int ret;
2180
2181 if (!access_ok(VERIFY_WRITE, buf, len))
2182 return -EFAULT;
2183
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002184 ret = spu_acquire_saved(ctx);
2185 if (ret)
2186 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002187 spin_lock(&ctx->csa.register_lock);
2188 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2189 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002190 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002191
2192 return ret;
2193}
2194
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002195static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002196 .open = spufs_info_open,
2197 .read = spufs_dma_info_read,
2198};
2199
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002200static ssize_t __spufs_proxydma_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_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002204 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002205 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002206 int i;
2207
2208 if (len < ret)
2209 return -EINVAL;
2210
2211 if (!access_ok(VERIFY_WRITE, buf, len))
2212 return -EFAULT;
2213
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002214 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2215 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2216 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2217 for (i = 0; i < 8; i++) {
2218 qp = &info.proxydma_info_command_data[i];
2219 puqp = &ctx->csa.priv2.puq[i];
2220
2221 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2222 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2223 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2224 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2225 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002226
2227 return simple_read_from_buffer(buf, len, pos, &info,
2228 sizeof info);
2229}
2230
2231static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2232 size_t len, loff_t *pos)
2233{
2234 struct spu_context *ctx = file->private_data;
2235 int ret;
2236
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002237 ret = spu_acquire_saved(ctx);
2238 if (ret)
2239 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002240 spin_lock(&ctx->csa.register_lock);
2241 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002242 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002243 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002244
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002245 return ret;
2246}
2247
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002248static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002249 .open = spufs_info_open,
2250 .read = spufs_proxydma_info_read,
2251};
2252
Christoph Hellwig476273a2007-06-29 10:58:01 +10002253static int spufs_show_tid(struct seq_file *s, void *private)
2254{
2255 struct spu_context *ctx = s->private;
2256
2257 seq_printf(s, "%d\n", ctx->tid);
2258 return 0;
2259}
2260
2261static int spufs_tid_open(struct inode *inode, struct file *file)
2262{
2263 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2264}
2265
2266static const struct file_operations spufs_tid_fops = {
2267 .open = spufs_tid_open,
2268 .read = seq_read,
2269 .llseek = seq_lseek,
2270 .release = single_release,
2271};
2272
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002273static const char *ctx_state_names[] = {
2274 "user", "system", "iowait", "loaded"
2275};
2276
2277static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002278 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002279{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002280 struct timespec ts;
2281 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002282
Andre Detsch27ec41d2007-07-20 21:39:33 +02002283 /*
2284 * In general, utilization statistics are updated by the controlling
2285 * thread as the spu context moves through various well defined
2286 * state transitions, but if the context is lazily loaded its
2287 * utilization statistics are not updated as the controlling thread
2288 * is not tightly coupled with the execution of the spu context. We
2289 * calculate and apply the time delta from the last recorded state
2290 * of the spu context.
2291 */
2292 if (ctx->spu && ctx->stats.util_state == state) {
2293 ktime_get_ts(&ts);
2294 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2295 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002296
Andre Detsch27ec41d2007-07-20 21:39:33 +02002297 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002298}
2299
2300static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2301{
2302 unsigned long long slb_flts = ctx->stats.slb_flt;
2303
2304 if (ctx->state == SPU_STATE_RUNNABLE) {
2305 slb_flts += (ctx->spu->stats.slb_flt -
2306 ctx->stats.slb_flt_base);
2307 }
2308
2309 return slb_flts;
2310}
2311
2312static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2313{
2314 unsigned long long class2_intrs = ctx->stats.class2_intr;
2315
2316 if (ctx->state == SPU_STATE_RUNNABLE) {
2317 class2_intrs += (ctx->spu->stats.class2_intr -
2318 ctx->stats.class2_intr_base);
2319 }
2320
2321 return class2_intrs;
2322}
2323
2324
2325static int spufs_show_stat(struct seq_file *s, void *private)
2326{
2327 struct spu_context *ctx = s->private;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002328 int ret;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002329
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002330 ret = spu_acquire(ctx);
2331 if (ret)
2332 return ret;
2333
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002334 seq_printf(s, "%s %llu %llu %llu %llu "
2335 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002336 ctx_state_names[ctx->stats.util_state],
2337 spufs_acct_time(ctx, SPU_UTIL_USER),
2338 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2339 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2340 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002341 ctx->stats.vol_ctx_switch,
2342 ctx->stats.invol_ctx_switch,
2343 spufs_slb_flts(ctx),
2344 ctx->stats.hash_flt,
2345 ctx->stats.min_flt,
2346 ctx->stats.maj_flt,
2347 spufs_class2_intrs(ctx),
2348 ctx->stats.libassist);
2349 spu_release(ctx);
2350 return 0;
2351}
2352
2353static int spufs_stat_open(struct inode *inode, struct file *file)
2354{
2355 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2356}
2357
2358static const struct file_operations spufs_stat_fops = {
2359 .open = spufs_stat_open,
2360 .read = seq_read,
2361 .llseek = seq_lseek,
2362 .release = single_release,
2363};
2364
2365
Arnd Bergmann67207b92005-11-15 15:53:48 -05002366struct tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002367 { "capabilities", &spufs_caps_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002368 { "mem", &spufs_mem_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002369 { "regs", &spufs_regs_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002370 { "mbox", &spufs_mbox_fops, 0444, },
2371 { "ibox", &spufs_ibox_fops, 0444, },
2372 { "wbox", &spufs_wbox_fops, 0222, },
2373 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2374 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2375 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002376 { "signal1", &spufs_signal1_fops, 0666, },
2377 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002378 { "signal1_type", &spufs_signal1_type, 0666, },
2379 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002380 { "cntl", &spufs_cntl_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002381 { "fpcr", &spufs_fpcr_fops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002382 { "lslr", &spufs_lslr_ops, 0444, },
2383 { "mfc", &spufs_mfc_fops, 0666, },
2384 { "mss", &spufs_mss_fops, 0666, },
2385 { "npc", &spufs_npc_ops, 0666, },
2386 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002387 { "decr", &spufs_decr_ops, 0666, },
2388 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002389 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002390 { "event_status", &spufs_event_status_ops, 0444, },
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02002391 { "psmap", &spufs_psmap_fops, 0666, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002392 { "phys-id", &spufs_id_ops, 0666, },
2393 { "object-id", &spufs_object_id_ops, 0666, },
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002394 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2395 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2396 { "wbox_info", &spufs_wbox_info_fops, 0444, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002397 { "dma_info", &spufs_dma_info_fops, 0444, },
2398 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002399 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002400 { "stat", &spufs_stat_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002401 {},
2402};
Mark Nutter5737edd2006-10-24 18:31:16 +02002403
2404struct tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002405 { "capabilities", &spufs_caps_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002406 { "mem", &spufs_mem_fops, 0666, },
2407 { "mbox", &spufs_mbox_fops, 0444, },
2408 { "ibox", &spufs_ibox_fops, 0444, },
2409 { "wbox", &spufs_wbox_fops, 0222, },
2410 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2411 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2412 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002413 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2414 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002415 { "signal1_type", &spufs_signal1_type, 0666, },
2416 { "signal2_type", &spufs_signal2_type, 0666, },
2417 { "mss", &spufs_mss_fops, 0666, },
2418 { "mfc", &spufs_mfc_fops, 0666, },
2419 { "cntl", &spufs_cntl_fops, 0666, },
2420 { "npc", &spufs_npc_ops, 0666, },
2421 { "psmap", &spufs_psmap_fops, 0666, },
2422 { "phys-id", &spufs_id_ops, 0666, },
2423 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002424 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002425 { "stat", &spufs_stat_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002426 {},
2427};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002428
2429struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002430 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2431 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002432 { "lslr", NULL, spufs_lslr_get, 19 },
2433 { "decr", NULL, spufs_decr_get, 19 },
2434 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002435 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2436 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002437 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002438 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002439 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2440 { "event_mask", NULL, spufs_event_mask_get, 19 },
2441 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002442 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2443 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2444 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2445 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2446 { "proxydma_info", __spufs_proxydma_info_read,
2447 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002448 { "object-id", NULL, spufs_object_id_get, 19 },
2449 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002450 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002451};