blob: 510adc57af9077831709521ae02c83968adf59ef [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
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500201 spu_acquire(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100202 ret = __spufs_mem_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500203 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500204 return ret;
205}
206
207static ssize_t
208spufs_mem_write(struct file *file, const char __user *buffer,
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100209 size_t size, loff_t *ppos)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500210{
211 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500212 char *local_store;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100213 loff_t pos = *ppos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500214 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500215
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100216 if (pos < 0)
217 return -EINVAL;
218 if (pos > LS_SIZE)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500219 return -EFBIG;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100220 if (size > LS_SIZE - pos)
221 size = LS_SIZE - pos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500222
223 spu_acquire(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500224 local_store = ctx->ops->get_ls(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100225 ret = copy_from_user(local_store + pos, buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500226 spu_release(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100227
228 if (ret)
229 return -EFAULT;
230 *ppos = pos + size;
231 return size;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500232}
233
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100234static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
235 unsigned long address)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500236{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000237 struct spu_context *ctx = vma->vm_file->private_data;
238 unsigned long pfn, offset, addr0 = address;
239#ifdef CONFIG_SPU_FS_64K_LS
240 struct spu_state *csa = &ctx->csa;
241 int psize;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100242
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000243 /* Check what page size we are using */
244 psize = get_slice_psize(vma->vm_mm, address);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500245
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000246 /* Some sanity checking */
247 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
248
249 /* Wow, 64K, cool, we need to align the address though */
250 if (csa->use_big_pages) {
251 BUG_ON(vma->vm_start & 0xffff);
252 address &= ~0xfffful;
253 }
254#endif /* CONFIG_SPU_FS_64K_LS */
255
256 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
Masato Noguchi128b8542007-02-13 21:54:30 +0100257 if (offset >= LS_SIZE)
258 return NOPFN_SIGBUS;
259
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000260 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
261 addr0, address, offset);
262
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500263 spu_acquire(ctx);
264
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200265 if (ctx->state == SPU_STATE_SAVED) {
266 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Arnd Bergmann932f535d2006-11-20 18:45:06 +0100267 & ~_PAGE_NO_CACHE);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100268 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200269 } else {
270 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100271 | _PAGE_NO_CACHE);
272 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200273 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100274 vm_insert_pfn(vma, address, pfn);
275
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500276 spu_release(ctx);
277
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100278 return NOPFN_REFAULT;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500279}
280
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100281
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500282static struct vm_operations_struct spufs_mem_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100283 .nopfn = spufs_mem_mmap_nopfn,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500284};
285
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000286static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500287{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000288#ifdef CONFIG_SPU_FS_64K_LS
289 struct spu_context *ctx = file->private_data;
290 struct spu_state *csa = &ctx->csa;
291
292 /* Sanity check VMA alignment */
293 if (csa->use_big_pages) {
294 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
295 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
296 vma->vm_pgoff);
297 if (vma->vm_start & 0xffff)
298 return -EINVAL;
299 if (vma->vm_pgoff & 0xf)
300 return -EINVAL;
301 }
302#endif /* CONFIG_SPU_FS_64K_LS */
303
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500304 if (!(vma->vm_flags & VM_SHARED))
305 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500306
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100307 vma->vm_flags |= VM_IO | VM_PFNMAP;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500308 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
309 | _PAGE_NO_CACHE);
310
311 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500312 return 0;
313}
314
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000315#ifdef CONFIG_SPU_FS_64K_LS
Sebastian Siewior12388192007-09-19 14:38:12 +1000316static unsigned long spufs_get_unmapped_area(struct file *file,
317 unsigned long addr, unsigned long len, unsigned long pgoff,
318 unsigned long flags)
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000319{
320 struct spu_context *ctx = file->private_data;
321 struct spu_state *csa = &ctx->csa;
322
323 /* If not using big pages, fallback to normal MM g_u_a */
324 if (!csa->use_big_pages)
325 return current->mm->get_unmapped_area(file, addr, len,
326 pgoff, flags);
327
328 /* Else, try to obtain a 64K pages slice */
329 return slice_get_unmapped_area(addr, len, flags,
330 MMU_PAGE_64K, 1, 0);
331}
332#endif /* CONFIG_SPU_FS_64K_LS */
333
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800334static const struct file_operations spufs_mem_fops = {
Jeremy Kerr70225432007-06-29 10:58:00 +1000335 .open = spufs_mem_open,
336 .release = spufs_mem_release,
337 .read = spufs_mem_read,
338 .write = spufs_mem_write,
339 .llseek = generic_file_llseek,
340 .mmap = spufs_mem_mmap,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000341#ifdef CONFIG_SPU_FS_64K_LS
342 .get_unmapped_area = spufs_get_unmapped_area,
343#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500344};
345
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100346static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
Mark Nutter6df10a82006-03-23 00:00:12 +0100347 unsigned long address,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100348 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200349 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100350{
Mark Nutter6df10a82006-03-23 00:00:12 +0100351 struct spu_context *ctx = vma->vm_file->private_data;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100352 unsigned long area, offset = address - vma->vm_start;
Mark Nutter6df10a82006-03-23 00:00:12 +0100353
354 offset += vma->vm_pgoff << PAGE_SHIFT;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200355 if (offset >= ps_size)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100356 return NOPFN_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100357
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900358 /*
359 * We have to wait for context to be loaded before we have
360 * pages to hand out to the user, but we don't want to wait
361 * with the mmap_sem held.
362 * It is possible to drop the mmap_sem here, but then we need
363 * to return NOPFN_REFAULT because the mappings may have
364 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100365 */
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900366 spu_acquire(ctx);
367 if (ctx->state == SPU_STATE_SAVED) {
368 up_read(&current->mm->mmap_sem);
369 spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
370 down_read(&current->mm->mmap_sem);
371 goto out;
372 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100373
374 area = ctx->spu->problem_phys + ps_offs;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100375 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900376
377out:
Mark Nutter6df10a82006-03-23 00:00:12 +0100378 spu_release(ctx);
379
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100380 return NOPFN_REFAULT;
Mark Nutter6df10a82006-03-23 00:00:12 +0100381}
382
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200383#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100384static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
385 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +0100386{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100387 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +0100388}
389
390static struct vm_operations_struct spufs_cntl_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100391 .nopfn = spufs_cntl_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +0100392};
393
394/*
395 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100396 */
397static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
398{
399 if (!(vma->vm_flags & VM_SHARED))
400 return -EINVAL;
401
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100402 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100403 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200404 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100405
406 vma->vm_ops = &spufs_cntl_mmap_vmops;
407 return 0;
408}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200409#else /* SPUFS_MMAP_4K */
410#define spufs_cntl_mmap NULL
411#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100412
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900413static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200414{
415 struct spu_context *ctx = data;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200416
417 spu_acquire(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900418 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200419 spu_release(ctx);
420
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900421 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200422}
423
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900424static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200425{
426 struct spu_context *ctx = data;
427
428 spu_acquire(ctx);
429 ctx->ops->runcntl_write(ctx, val);
430 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900431
432 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200433}
434
Mark Nutter6df10a82006-03-23 00:00:12 +0100435static int spufs_cntl_open(struct inode *inode, struct file *file)
436{
437 struct spufs_inode_info *i = SPUFS_I(inode);
438 struct spu_context *ctx = i->i_ctx;
439
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000440 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100441 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200442 if (!i->i_openers++)
443 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000444 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900445 return spufs_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200446 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100447}
448
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200449static int
450spufs_cntl_release(struct inode *inode, struct file *file)
451{
452 struct spufs_inode_info *i = SPUFS_I(inode);
453 struct spu_context *ctx = i->i_ctx;
454
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900455 spufs_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200456
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000457 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200458 if (!--i->i_openers)
459 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000460 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200461 return 0;
462}
463
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800464static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100465 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200466 .release = spufs_cntl_release,
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900467 .read = spufs_attr_read,
468 .write = spufs_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100469 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100470};
471
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500472static int
473spufs_regs_open(struct inode *inode, struct file *file)
474{
475 struct spufs_inode_info *i = SPUFS_I(inode);
476 file->private_data = i->i_ctx;
477 return 0;
478}
479
480static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100481__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
482 size_t size, loff_t *pos)
483{
484 struct spu_lscsa *lscsa = ctx->csa.lscsa;
485 return simple_read_from_buffer(buffer, size, pos,
486 lscsa->gprs, sizeof lscsa->gprs);
487}
488
489static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500490spufs_regs_read(struct file *file, char __user *buffer,
491 size_t size, loff_t *pos)
492{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500493 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100494 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500495
496 spu_acquire_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100497 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200498 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500499 return ret;
500}
501
502static ssize_t
503spufs_regs_write(struct file *file, const char __user *buffer,
504 size_t size, loff_t *pos)
505{
506 struct spu_context *ctx = file->private_data;
507 struct spu_lscsa *lscsa = ctx->csa.lscsa;
508 int ret;
509
510 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
511 if (size <= 0)
512 return -EFBIG;
513 *pos += size;
514
515 spu_acquire_saved(ctx);
516
517 ret = copy_from_user(lscsa->gprs + *pos - size,
518 buffer, size) ? -EFAULT : size;
519
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200520 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500521 return ret;
522}
523
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800524static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500525 .open = spufs_regs_open,
526 .read = spufs_regs_read,
527 .write = spufs_regs_write,
528 .llseek = generic_file_llseek,
529};
530
531static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100532__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
533 size_t size, loff_t * pos)
534{
535 struct spu_lscsa *lscsa = ctx->csa.lscsa;
536 return simple_read_from_buffer(buffer, size, pos,
537 &lscsa->fpcr, sizeof(lscsa->fpcr));
538}
539
540static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500541spufs_fpcr_read(struct file *file, char __user * buffer,
542 size_t size, loff_t * pos)
543{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500544 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100545 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500546
547 spu_acquire_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100548 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200549 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500550 return ret;
551}
552
553static ssize_t
554spufs_fpcr_write(struct file *file, const char __user * buffer,
555 size_t size, loff_t * pos)
556{
557 struct spu_context *ctx = file->private_data;
558 struct spu_lscsa *lscsa = ctx->csa.lscsa;
559 int ret;
560
561 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
562 if (size <= 0)
563 return -EFBIG;
564 *pos += size;
565
566 spu_acquire_saved(ctx);
567
568 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
569 buffer, size) ? -EFAULT : size;
570
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200571 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500572 return ret;
573}
574
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800575static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500576 .open = spufs_regs_open,
577 .read = spufs_fpcr_read,
578 .write = spufs_fpcr_write,
579 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500580};
581
582/* generic open function for all pipe-like files */
583static int spufs_pipe_open(struct inode *inode, struct file *file)
584{
585 struct spufs_inode_info *i = SPUFS_I(inode);
586 file->private_data = i->i_ctx;
587
588 return nonseekable_open(inode, file);
589}
590
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200591/*
592 * Read as many bytes from the mailbox as possible, until
593 * one of the conditions becomes true:
594 *
595 * - no more data available in the mailbox
596 * - end of the user provided buffer
597 * - end of the mapped area
598 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500599static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
600 size_t len, loff_t *pos)
601{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500602 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200603 u32 mbox_data, __user *udata;
604 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500605
606 if (len < 4)
607 return -EINVAL;
608
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200609 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500610 return -EFAULT;
611
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200612 udata = (void __user *)buf;
613
614 spu_acquire(ctx);
Arnd Bergmann274cef52006-10-24 18:01:42 +0200615 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200616 int ret;
617 ret = ctx->ops->mbox_read(ctx, &mbox_data);
618 if (ret == 0)
619 break;
620
621 /*
622 * at the end of the mapped area, we can fault
623 * but still need to return the data we have
624 * read successfully so far.
625 */
626 ret = __put_user(mbox_data, udata);
627 if (ret) {
628 if (!count)
629 count = -EFAULT;
630 break;
631 }
632 }
633 spu_release(ctx);
634
635 if (!count)
636 count = -EAGAIN;
637
638 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500639}
640
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800641static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500642 .open = spufs_pipe_open,
643 .read = spufs_mbox_read,
644};
645
646static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
647 size_t len, loff_t *pos)
648{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500649 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500650 u32 mbox_stat;
651
652 if (len < 4)
653 return -EINVAL;
654
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500655 spu_acquire(ctx);
656
657 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
658
659 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500660
661 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
662 return -EFAULT;
663
664 return 4;
665}
666
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800667static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500668 .open = spufs_pipe_open,
669 .read = spufs_mbox_stat_read,
670};
671
672/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500673size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500674{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500675 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500676}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500677
678static int spufs_ibox_fasync(int fd, struct file *file, int on)
679{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500680 struct spu_context *ctx = file->private_data;
681
682 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
683}
684
685/* interrupt-level ibox callback function. */
686void spufs_ibox_callback(struct spu *spu)
687{
688 struct spu_context *ctx = spu->ctx;
689
Luke Browninge65c2f62007-12-20 16:39:59 +0900690 if (!ctx)
691 return;
692
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500693 wake_up_all(&ctx->ibox_wq);
694 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500695}
696
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200697/*
698 * Read as many bytes from the interrupt mailbox as possible, until
699 * one of the conditions becomes true:
700 *
701 * - no more data available in the mailbox
702 * - end of the user provided buffer
703 * - end of the mapped area
704 *
705 * If the file is opened without O_NONBLOCK, we wait here until
706 * any data is available, but return when we have been able to
707 * read something.
708 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500709static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
710 size_t len, loff_t *pos)
711{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500712 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200713 u32 ibox_data, __user *udata;
714 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500715
716 if (len < 4)
717 return -EINVAL;
718
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200719 if (!access_ok(VERIFY_WRITE, buf, len))
720 return -EFAULT;
721
722 udata = (void __user *)buf;
723
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500724 spu_acquire(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500725
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200726 /* wait only for the first element */
727 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500728 if (file->f_flags & O_NONBLOCK) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500729 if (!spu_ibox_read(ctx, &ibox_data))
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200730 count = -EAGAIN;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500731 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200732 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
733 }
734 if (count)
735 goto out;
736
737 /* if we can't write at all, return -EFAULT */
738 count = __put_user(ibox_data, udata);
739 if (count)
740 goto out;
741
742 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
743 int ret;
744 ret = ctx->ops->ibox_read(ctx, &ibox_data);
745 if (ret == 0)
746 break;
747 /*
748 * at the end of the mapped area, we can fault
749 * but still need to return the data we have
750 * read successfully so far.
751 */
752 ret = __put_user(ibox_data, udata);
753 if (ret)
754 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500755 }
756
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200757out:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500758 spu_release(ctx);
759
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200760 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500761}
762
763static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
764{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500765 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500766 unsigned int mask;
767
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500768 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500769
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500770 spu_acquire(ctx);
771 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
772 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500773
774 return mask;
775}
776
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800777static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500778 .open = spufs_pipe_open,
779 .read = spufs_ibox_read,
780 .poll = spufs_ibox_poll,
781 .fasync = spufs_ibox_fasync,
782};
783
784static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
785 size_t len, loff_t *pos)
786{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500787 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500788 u32 ibox_stat;
789
790 if (len < 4)
791 return -EINVAL;
792
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500793 spu_acquire(ctx);
794 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
795 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500796
797 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
798 return -EFAULT;
799
800 return 4;
801}
802
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800803static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500804 .open = spufs_pipe_open,
805 .read = spufs_ibox_stat_read,
806};
807
808/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500809size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500810{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500811 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500812}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500813
814static int spufs_wbox_fasync(int fd, struct file *file, int on)
815{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500816 struct spu_context *ctx = file->private_data;
817 int ret;
818
819 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
820
821 return ret;
822}
823
824/* interrupt-level wbox callback function. */
825void spufs_wbox_callback(struct spu *spu)
826{
827 struct spu_context *ctx = spu->ctx;
828
Luke Browninge65c2f62007-12-20 16:39:59 +0900829 if (!ctx)
830 return;
831
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500832 wake_up_all(&ctx->wbox_wq);
833 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500834}
835
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200836/*
837 * Write as many bytes to the interrupt mailbox as possible, until
838 * one of the conditions becomes true:
839 *
840 * - the mailbox is full
841 * - end of the user provided buffer
842 * - end of the mapped area
843 *
844 * If the file is opened without O_NONBLOCK, we wait here until
845 * space is availabyl, but return when we have been able to
846 * write something.
847 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500848static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
849 size_t len, loff_t *pos)
850{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500851 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200852 u32 wbox_data, __user *udata;
853 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500854
855 if (len < 4)
856 return -EINVAL;
857
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200858 udata = (void __user *)buf;
859 if (!access_ok(VERIFY_READ, buf, len))
860 return -EFAULT;
861
862 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500863 return -EFAULT;
864
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500865 spu_acquire(ctx);
866
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200867 /*
868 * make sure we can at least write one element, by waiting
869 * in case of !O_NONBLOCK
870 */
871 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500872 if (file->f_flags & O_NONBLOCK) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500873 if (!spu_wbox_write(ctx, wbox_data))
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200874 count = -EAGAIN;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500875 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200876 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Arnd Bergmann67207b92005-11-15 15:53:48 -0500877 }
878
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200879 if (count)
880 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500881
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200882 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200883 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
884 int ret;
885 ret = __get_user(wbox_data, udata);
886 if (ret)
887 break;
888
889 ret = spu_wbox_write(ctx, wbox_data);
890 if (ret == 0)
891 break;
892 }
893
894out:
895 spu_release(ctx);
896 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500897}
898
899static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
900{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500901 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500902 unsigned int mask;
903
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500904 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500905
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500906 spu_acquire(ctx);
907 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
908 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500909
910 return mask;
911}
912
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800913static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500914 .open = spufs_pipe_open,
915 .write = spufs_wbox_write,
916 .poll = spufs_wbox_poll,
917 .fasync = spufs_wbox_fasync,
918};
919
920static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
921 size_t len, loff_t *pos)
922{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500923 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500924 u32 wbox_stat;
925
926 if (len < 4)
927 return -EINVAL;
928
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500929 spu_acquire(ctx);
930 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
931 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500932
933 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
934 return -EFAULT;
935
936 return 4;
937}
938
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800939static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500940 .open = spufs_pipe_open,
941 .read = spufs_wbox_stat_read,
942};
943
Mark Nutter6df10a82006-03-23 00:00:12 +0100944static int spufs_signal1_open(struct inode *inode, struct file *file)
945{
946 struct spufs_inode_info *i = SPUFS_I(inode);
947 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200948
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000949 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100950 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200951 if (!i->i_openers++)
952 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000953 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100954 return nonseekable_open(inode, file);
955}
956
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200957static int
958spufs_signal1_release(struct inode *inode, struct file *file)
959{
960 struct spufs_inode_info *i = SPUFS_I(inode);
961 struct spu_context *ctx = i->i_ctx;
962
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000963 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200964 if (!--i->i_openers)
965 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000966 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200967 return 0;
968}
969
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100970static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500971 size_t len, loff_t *pos)
972{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100973 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500974 u32 data;
975
Arnd Bergmann67207b92005-11-15 15:53:48 -0500976 if (len < 4)
977 return -EINVAL;
978
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100979 if (ctx->csa.spu_chnlcnt_RW[3]) {
980 data = ctx->csa.spu_chnldata_RW[3];
981 ret = 4;
982 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500983
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100984 if (!ret)
985 goto out;
986
Arnd Bergmann67207b92005-11-15 15:53:48 -0500987 if (copy_to_user(buf, &data, 4))
988 return -EFAULT;
989
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100990out:
991 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500992}
993
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100994static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
995 size_t len, loff_t *pos)
996{
997 int ret;
998 struct spu_context *ctx = file->private_data;
999
1000 spu_acquire_saved(ctx);
1001 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001002 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001003
1004 return ret;
1005}
1006
Arnd Bergmann67207b92005-11-15 15:53:48 -05001007static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1008 size_t len, loff_t *pos)
1009{
1010 struct spu_context *ctx;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001011 u32 data;
1012
1013 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001014
1015 if (len < 4)
1016 return -EINVAL;
1017
1018 if (copy_from_user(&data, buf, 4))
1019 return -EFAULT;
1020
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001021 spu_acquire(ctx);
1022 ctx->ops->signal1_write(ctx, data);
1023 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001024
1025 return 4;
1026}
1027
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001028static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
1029 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001030{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001031#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001032 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001033#elif PAGE_SIZE == 0x10000
1034 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1035 * signal 1 and 2 area
1036 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001037 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001038#else
1039#error unsupported page size
1040#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001041}
1042
1043static struct vm_operations_struct spufs_signal1_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001044 .nopfn = spufs_signal1_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001045};
1046
1047static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1048{
1049 if (!(vma->vm_flags & VM_SHARED))
1050 return -EINVAL;
1051
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001052 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001053 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001054 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001055
1056 vma->vm_ops = &spufs_signal1_mmap_vmops;
1057 return 0;
1058}
Mark Nutter6df10a82006-03-23 00:00:12 +01001059
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001060static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001061 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001062 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001063 .read = spufs_signal1_read,
1064 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001065 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001066};
1067
Jeremy Kerrd054b362007-07-20 21:39:31 +02001068static const struct file_operations spufs_signal1_nosched_fops = {
1069 .open = spufs_signal1_open,
1070 .release = spufs_signal1_release,
1071 .write = spufs_signal1_write,
1072 .mmap = spufs_signal1_mmap,
1073};
1074
Mark Nutter6df10a82006-03-23 00:00:12 +01001075static int spufs_signal2_open(struct inode *inode, struct file *file)
1076{
1077 struct spufs_inode_info *i = SPUFS_I(inode);
1078 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001079
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001080 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001081 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001082 if (!i->i_openers++)
1083 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001084 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001085 return nonseekable_open(inode, file);
1086}
1087
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001088static int
1089spufs_signal2_release(struct inode *inode, struct file *file)
1090{
1091 struct spufs_inode_info *i = SPUFS_I(inode);
1092 struct spu_context *ctx = i->i_ctx;
1093
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001094 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001095 if (!--i->i_openers)
1096 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001097 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001098 return 0;
1099}
1100
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001101static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001102 size_t len, loff_t *pos)
1103{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001104 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001105 u32 data;
1106
Arnd Bergmann67207b92005-11-15 15:53:48 -05001107 if (len < 4)
1108 return -EINVAL;
1109
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001110 if (ctx->csa.spu_chnlcnt_RW[4]) {
1111 data = ctx->csa.spu_chnldata_RW[4];
1112 ret = 4;
1113 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001114
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001115 if (!ret)
1116 goto out;
1117
Arnd Bergmann67207b92005-11-15 15:53:48 -05001118 if (copy_to_user(buf, &data, 4))
1119 return -EFAULT;
1120
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001121out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001122 return ret;
1123}
1124
1125static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1126 size_t len, loff_t *pos)
1127{
1128 struct spu_context *ctx = file->private_data;
1129 int ret;
1130
1131 spu_acquire_saved(ctx);
1132 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001133 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001134
1135 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001136}
1137
1138static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1139 size_t len, loff_t *pos)
1140{
1141 struct spu_context *ctx;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001142 u32 data;
1143
1144 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001145
1146 if (len < 4)
1147 return -EINVAL;
1148
1149 if (copy_from_user(&data, buf, 4))
1150 return -EFAULT;
1151
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001152 spu_acquire(ctx);
1153 ctx->ops->signal2_write(ctx, data);
1154 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001155
1156 return 4;
1157}
1158
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001159#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001160static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1161 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001162{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001163#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001164 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001165#elif PAGE_SIZE == 0x10000
1166 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1167 * signal 1 and 2 area
1168 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001169 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001170#else
1171#error unsupported page size
1172#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001173}
1174
1175static struct vm_operations_struct spufs_signal2_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001176 .nopfn = spufs_signal2_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001177};
1178
1179static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1180{
1181 if (!(vma->vm_flags & VM_SHARED))
1182 return -EINVAL;
1183
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001184 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001185 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001186 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001187
1188 vma->vm_ops = &spufs_signal2_mmap_vmops;
1189 return 0;
1190}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001191#else /* SPUFS_MMAP_4K */
1192#define spufs_signal2_mmap NULL
1193#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001194
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001195static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001196 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001197 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001198 .read = spufs_signal2_read,
1199 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001200 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001201};
1202
Jeremy Kerrd054b362007-07-20 21:39:31 +02001203static const struct file_operations spufs_signal2_nosched_fops = {
1204 .open = spufs_signal2_open,
1205 .release = spufs_signal2_release,
1206 .write = spufs_signal2_write,
1207 .mmap = spufs_signal2_mmap,
1208};
1209
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001210/*
1211 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1212 * work of acquiring (or not) the SPU context before calling through
1213 * to the actual get routine. The set routine is called directly.
1214 */
1215#define SPU_ATTR_NOACQUIRE 0
1216#define SPU_ATTR_ACQUIRE 1
1217#define SPU_ATTR_ACQUIRE_SAVED 2
1218
1219#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001220static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001221{ \
1222 struct spu_context *ctx = data; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001223 \
1224 if (__acquire == SPU_ATTR_ACQUIRE) { \
1225 spu_acquire(ctx); \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001226 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001227 spu_release(ctx); \
1228 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
1229 spu_acquire_saved(ctx); \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001230 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001231 spu_release_saved(ctx); \
1232 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001233 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001234 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001235 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001236} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001237DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001238
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001239static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001240{
1241 struct spu_context *ctx = data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001242
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001243 spu_acquire(ctx);
1244 ctx->ops->signal1_type_set(ctx, val);
1245 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001246
1247 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001248}
1249
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001250static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001251{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001252 return ctx->ops->signal1_type_get(ctx);
1253}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001254DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1255 spufs_signal1_type_set, "%llu", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001256
Arnd Bergmann67207b92005-11-15 15:53:48 -05001257
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001258static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001259{
1260 struct spu_context *ctx = data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001261
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001262 spu_acquire(ctx);
1263 ctx->ops->signal2_type_set(ctx, val);
1264 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001265
1266 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001267}
1268
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001269static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001270{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001271 return ctx->ops->signal2_type_get(ctx);
1272}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001273DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1274 spufs_signal2_type_set, "%llu", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001275
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001276#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001277static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1278 unsigned long address)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001279{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001280 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001281}
1282
1283static struct vm_operations_struct spufs_mss_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001284 .nopfn = spufs_mss_mmap_nopfn,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001285};
1286
1287/*
1288 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001289 */
1290static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1291{
1292 if (!(vma->vm_flags & VM_SHARED))
1293 return -EINVAL;
1294
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001295 vma->vm_flags |= VM_IO | VM_PFNMAP;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001296 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001297 | _PAGE_NO_CACHE | _PAGE_GUARDED);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001298
1299 vma->vm_ops = &spufs_mss_mmap_vmops;
1300 return 0;
1301}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001302#else /* SPUFS_MMAP_4K */
1303#define spufs_mss_mmap NULL
1304#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001305
1306static int spufs_mss_open(struct inode *inode, struct file *file)
1307{
1308 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001309 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001310
1311 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001312
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001313 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001314 if (!i->i_openers++)
1315 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001316 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001317 return nonseekable_open(inode, file);
1318}
1319
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001320static int
1321spufs_mss_release(struct inode *inode, struct file *file)
1322{
1323 struct spufs_inode_info *i = SPUFS_I(inode);
1324 struct spu_context *ctx = i->i_ctx;
1325
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001326 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001327 if (!--i->i_openers)
1328 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001329 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001330 return 0;
1331}
1332
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001333static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001334 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001335 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001336 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001337};
1338
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001339static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1340 unsigned long address)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001341{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001342 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001343}
1344
1345static struct vm_operations_struct spufs_psmap_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001346 .nopfn = spufs_psmap_mmap_nopfn,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001347};
1348
1349/*
1350 * mmap support for full problem state area [0x00000 - 0x1ffff].
1351 */
1352static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1353{
1354 if (!(vma->vm_flags & VM_SHARED))
1355 return -EINVAL;
1356
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001357 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001358 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1359 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1360
1361 vma->vm_ops = &spufs_psmap_mmap_vmops;
1362 return 0;
1363}
1364
1365static int spufs_psmap_open(struct inode *inode, struct file *file)
1366{
1367 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001368 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001369
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001370 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001371 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001372 if (!i->i_openers++)
1373 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001374 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001375 return nonseekable_open(inode, file);
1376}
1377
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001378static int
1379spufs_psmap_release(struct inode *inode, struct file *file)
1380{
1381 struct spufs_inode_info *i = SPUFS_I(inode);
1382 struct spu_context *ctx = i->i_ctx;
1383
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001384 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001385 if (!--i->i_openers)
1386 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001387 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001388 return 0;
1389}
1390
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001391static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001392 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001393 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001394 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001395};
1396
1397
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001398#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001399static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1400 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001401{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001402 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +01001403}
1404
1405static struct vm_operations_struct spufs_mfc_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001406 .nopfn = spufs_mfc_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001407};
1408
1409/*
1410 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001411 */
1412static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1413{
1414 if (!(vma->vm_flags & VM_SHARED))
1415 return -EINVAL;
1416
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001417 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001418 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001419 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001420
1421 vma->vm_ops = &spufs_mfc_mmap_vmops;
1422 return 0;
1423}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001424#else /* SPUFS_MMAP_4K */
1425#define spufs_mfc_mmap NULL
1426#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001427
1428static int spufs_mfc_open(struct inode *inode, struct file *file)
1429{
1430 struct spufs_inode_info *i = SPUFS_I(inode);
1431 struct spu_context *ctx = i->i_ctx;
1432
1433 /* we don't want to deal with DMA into other processes */
1434 if (ctx->owner != current->mm)
1435 return -EINVAL;
1436
1437 if (atomic_read(&inode->i_count) != 1)
1438 return -EBUSY;
1439
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001440 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001441 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001442 if (!i->i_openers++)
1443 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001444 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001445 return nonseekable_open(inode, file);
1446}
1447
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001448static int
1449spufs_mfc_release(struct inode *inode, struct file *file)
1450{
1451 struct spufs_inode_info *i = SPUFS_I(inode);
1452 struct spu_context *ctx = i->i_ctx;
1453
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001454 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001455 if (!--i->i_openers)
1456 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001457 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001458 return 0;
1459}
1460
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001461/* interrupt-level mfc callback function. */
1462void spufs_mfc_callback(struct spu *spu)
1463{
1464 struct spu_context *ctx = spu->ctx;
1465
Luke Browninge65c2f62007-12-20 16:39:59 +09001466 if (!ctx)
1467 return;
1468
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001469 wake_up_all(&ctx->mfc_wq);
1470
1471 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1472 if (ctx->mfc_fasync) {
1473 u32 free_elements, tagstatus;
1474 unsigned int mask;
1475
1476 /* no need for spu_acquire in interrupt context */
1477 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1478 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1479
1480 mask = 0;
1481 if (free_elements & 0xffff)
1482 mask |= POLLOUT;
1483 if (tagstatus & ctx->tagwait)
1484 mask |= POLLIN;
1485
1486 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1487 }
1488}
1489
1490static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1491{
1492 /* See if there is one tag group is complete */
1493 /* FIXME we need locking around tagwait */
1494 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1495 ctx->tagwait &= ~*status;
1496 if (*status)
1497 return 1;
1498
1499 /* enable interrupt waiting for any tag group,
1500 may silently fail if interrupts are already enabled */
1501 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1502 return 0;
1503}
1504
1505static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1506 size_t size, loff_t *pos)
1507{
1508 struct spu_context *ctx = file->private_data;
1509 int ret = -EINVAL;
1510 u32 status;
1511
1512 if (size != 4)
1513 goto out;
1514
1515 spu_acquire(ctx);
1516 if (file->f_flags & O_NONBLOCK) {
1517 status = ctx->ops->read_mfc_tagstatus(ctx);
1518 if (!(status & ctx->tagwait))
1519 ret = -EAGAIN;
1520 else
1521 ctx->tagwait &= ~status;
1522 } else {
1523 ret = spufs_wait(ctx->mfc_wq,
1524 spufs_read_mfc_tagstatus(ctx, &status));
1525 }
1526 spu_release(ctx);
1527
1528 if (ret)
1529 goto out;
1530
1531 ret = 4;
1532 if (copy_to_user(buffer, &status, 4))
1533 ret = -EFAULT;
1534
1535out:
1536 return ret;
1537}
1538
1539static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1540{
1541 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1542 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1543
1544 switch (cmd->cmd) {
1545 case MFC_PUT_CMD:
1546 case MFC_PUTF_CMD:
1547 case MFC_PUTB_CMD:
1548 case MFC_GET_CMD:
1549 case MFC_GETF_CMD:
1550 case MFC_GETB_CMD:
1551 break;
1552 default:
1553 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1554 return -EIO;
1555 }
1556
1557 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1558 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1559 cmd->ea, cmd->lsa);
1560 return -EIO;
1561 }
1562
1563 switch (cmd->size & 0xf) {
1564 case 1:
1565 break;
1566 case 2:
1567 if (cmd->lsa & 1)
1568 goto error;
1569 break;
1570 case 4:
1571 if (cmd->lsa & 3)
1572 goto error;
1573 break;
1574 case 8:
1575 if (cmd->lsa & 7)
1576 goto error;
1577 break;
1578 case 0:
1579 if (cmd->lsa & 15)
1580 goto error;
1581 break;
1582 error:
1583 default:
1584 pr_debug("invalid DMA alignment %x for size %x\n",
1585 cmd->lsa & 0xf, cmd->size);
1586 return -EIO;
1587 }
1588
1589 if (cmd->size > 16 * 1024) {
1590 pr_debug("invalid DMA size %x\n", cmd->size);
1591 return -EIO;
1592 }
1593
1594 if (cmd->tag & 0xfff0) {
1595 /* we reserve the higher tag numbers for kernel use */
1596 pr_debug("invalid DMA tag\n");
1597 return -EIO;
1598 }
1599
1600 if (cmd->class) {
1601 /* not supported in this version */
1602 pr_debug("invalid DMA class\n");
1603 return -EIO;
1604 }
1605
1606 return 0;
1607}
1608
1609static int spu_send_mfc_command(struct spu_context *ctx,
1610 struct mfc_dma_command cmd,
1611 int *error)
1612{
1613 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1614 if (*error == -EAGAIN) {
1615 /* wait for any tag group to complete
1616 so we have space for the new command */
1617 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1618 /* try again, because the queue might be
1619 empty again */
1620 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1621 if (*error == -EAGAIN)
1622 return 0;
1623 }
1624 return 1;
1625}
1626
1627static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1628 size_t size, loff_t *pos)
1629{
1630 struct spu_context *ctx = file->private_data;
1631 struct mfc_dma_command cmd;
1632 int ret = -EINVAL;
1633
1634 if (size != sizeof cmd)
1635 goto out;
1636
1637 ret = -EFAULT;
1638 if (copy_from_user(&cmd, buffer, sizeof cmd))
1639 goto out;
1640
1641 ret = spufs_check_valid_dma(&cmd);
1642 if (ret)
1643 goto out;
1644
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001645 spu_acquire(ctx);
1646 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001647 if (ret)
1648 goto out;
1649
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001650 if (file->f_flags & O_NONBLOCK) {
1651 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1652 } else {
1653 int status;
1654 ret = spufs_wait(ctx->mfc_wq,
1655 spu_send_mfc_command(ctx, cmd, &status));
1656 if (status)
1657 ret = status;
1658 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001659
1660 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001661 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001662
1663 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001664 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001665
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001666out_unlock:
1667 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001668out:
1669 return ret;
1670}
1671
1672static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1673{
1674 struct spu_context *ctx = file->private_data;
1675 u32 free_elements, tagstatus;
1676 unsigned int mask;
1677
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001678 poll_wait(file, &ctx->mfc_wq, wait);
1679
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001680 spu_acquire(ctx);
1681 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1682 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1683 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1684 spu_release(ctx);
1685
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001686 mask = 0;
1687 if (free_elements & 0xffff)
1688 mask |= POLLOUT | POLLWRNORM;
1689 if (tagstatus & ctx->tagwait)
1690 mask |= POLLIN | POLLRDNORM;
1691
1692 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1693 free_elements, tagstatus, ctx->tagwait);
1694
1695 return mask;
1696}
1697
Al Viro73b6af82006-06-25 16:42:33 -07001698static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001699{
1700 struct spu_context *ctx = file->private_data;
1701 int ret;
1702
1703 spu_acquire(ctx);
1704#if 0
1705/* this currently hangs */
1706 ret = spufs_wait(ctx->mfc_wq,
1707 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1708 if (ret)
1709 goto out;
1710 ret = spufs_wait(ctx->mfc_wq,
1711 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1712out:
1713#else
1714 ret = 0;
1715#endif
1716 spu_release(ctx);
1717
1718 return ret;
1719}
1720
1721static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1722 int datasync)
1723{
Al Viro73b6af82006-06-25 16:42:33 -07001724 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001725}
1726
1727static int spufs_mfc_fasync(int fd, struct file *file, int on)
1728{
1729 struct spu_context *ctx = file->private_data;
1730
1731 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1732}
1733
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001734static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001735 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001736 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001737 .read = spufs_mfc_read,
1738 .write = spufs_mfc_write,
1739 .poll = spufs_mfc_poll,
1740 .flush = spufs_mfc_flush,
1741 .fsync = spufs_mfc_fsync,
1742 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001743 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001744};
1745
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001746static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001747{
1748 struct spu_context *ctx = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001749 spu_acquire(ctx);
1750 ctx->ops->npc_write(ctx, val);
1751 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001752
1753 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001754}
1755
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001756static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001757{
1758 return ctx->ops->npc_read(ctx);
1759}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001760DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1761 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001762
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001763static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001764{
1765 struct spu_context *ctx = data;
1766 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1767 spu_acquire_saved(ctx);
1768 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001769 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001770
1771 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001772}
1773
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001774static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001775{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001776 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001777 return lscsa->decr.slot[0];
1778}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001779DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1780 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001781
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001782static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001783{
1784 struct spu_context *ctx = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001785 spu_acquire_saved(ctx);
Masato Noguchid40a01d2007-07-20 21:39:38 +02001786 if (val)
1787 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1788 else
1789 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001790 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001791
1792 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001793}
1794
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001795static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001796{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001797 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1798 return SPU_DECR_STATUS_RUNNING;
1799 else
1800 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001801}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001802DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1803 spufs_decr_status_set, "0x%llx\n",
1804 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001805
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001806static int spufs_event_mask_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001807{
1808 struct spu_context *ctx = data;
1809 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1810 spu_acquire_saved(ctx);
1811 lscsa->event_mask.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001812 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001813
1814 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001815}
1816
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001817static u64 spufs_event_mask_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001818{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001819 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001820 return lscsa->event_mask.slot[0];
1821}
1822
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001823DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1824 spufs_event_mask_set, "0x%llx\n",
1825 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001826
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001827static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001828{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001829 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001830 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001831 stat = state->spu_chnlcnt_RW[0];
1832 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001833 return state->spu_chnldata_RW[0];
1834 return 0;
1835}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001836DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1837 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001838
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001839static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001840{
1841 struct spu_context *ctx = data;
1842 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1843 spu_acquire_saved(ctx);
1844 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001845 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001846
1847 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001848}
1849
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001850static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001851{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001852 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001853 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001854}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001855DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
1856 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001857
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001858static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001859{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001860 u64 num;
1861
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001862 if (ctx->state == SPU_STATE_RUNNABLE)
1863 num = ctx->spu->number;
1864 else
1865 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001866
1867 return num;
1868}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001869DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
1870 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001871
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001872static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001873{
1874 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001875 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001876}
1877
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001878static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02001879{
1880 struct spu_context *ctx = data;
1881 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001882
1883 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02001884}
1885
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001886DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1887 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02001888
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001889static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001890{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001891 return ctx->csa.priv2.spu_lslr_RW;
1892}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001893DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
1894 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001895
1896static int spufs_info_open(struct inode *inode, struct file *file)
1897{
1898 struct spufs_inode_info *i = SPUFS_I(inode);
1899 struct spu_context *ctx = i->i_ctx;
1900 file->private_data = ctx;
1901 return 0;
1902}
1903
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10001904static int spufs_caps_show(struct seq_file *s, void *private)
1905{
1906 struct spu_context *ctx = s->private;
1907
1908 if (!(ctx->flags & SPU_CREATE_NOSCHED))
1909 seq_puts(s, "sched\n");
1910 if (!(ctx->flags & SPU_CREATE_ISOLATE))
1911 seq_puts(s, "step\n");
1912 return 0;
1913}
1914
1915static int spufs_caps_open(struct inode *inode, struct file *file)
1916{
1917 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1918}
1919
1920static const struct file_operations spufs_caps_fops = {
1921 .open = spufs_caps_open,
1922 .read = seq_read,
1923 .llseek = seq_lseek,
1924 .release = single_release,
1925};
1926
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001927static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1928 char __user *buf, size_t len, loff_t *pos)
1929{
1930 u32 mbox_stat;
1931 u32 data;
1932
1933 mbox_stat = ctx->csa.prob.mb_stat_R;
1934 if (mbox_stat & 0x0000ff) {
1935 data = ctx->csa.prob.pu_mb_R;
1936 }
1937
1938 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1939}
1940
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001941static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1942 size_t len, loff_t *pos)
1943{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001944 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001945 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001946
1947 if (!access_ok(VERIFY_WRITE, buf, len))
1948 return -EFAULT;
1949
1950 spu_acquire_saved(ctx);
1951 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001952 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001953 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001954 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001955
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001956 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001957}
1958
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001959static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001960 .open = spufs_info_open,
1961 .read = spufs_mbox_info_read,
1962 .llseek = generic_file_llseek,
1963};
1964
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001965static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
1966 char __user *buf, size_t len, loff_t *pos)
1967{
1968 u32 ibox_stat;
1969 u32 data;
1970
1971 ibox_stat = ctx->csa.prob.mb_stat_R;
1972 if (ibox_stat & 0xff0000) {
1973 data = ctx->csa.priv2.puint_mb_R;
1974 }
1975
1976 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1977}
1978
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001979static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
1980 size_t len, loff_t *pos)
1981{
1982 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001983 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001984
1985 if (!access_ok(VERIFY_WRITE, buf, len))
1986 return -EFAULT;
1987
1988 spu_acquire_saved(ctx);
1989 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001990 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001991 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001992 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001993
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001994 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001995}
1996
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001997static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001998 .open = spufs_info_open,
1999 .read = spufs_ibox_info_read,
2000 .llseek = generic_file_llseek,
2001};
2002
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002003static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2004 char __user *buf, size_t len, loff_t *pos)
2005{
2006 int i, cnt;
2007 u32 data[4];
2008 u32 wbox_stat;
2009
2010 wbox_stat = ctx->csa.prob.mb_stat_R;
2011 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2012 for (i = 0; i < cnt; i++) {
2013 data[i] = ctx->csa.spu_mailbox_data[i];
2014 }
2015
2016 return simple_read_from_buffer(buf, len, pos, &data,
2017 cnt * sizeof(u32));
2018}
2019
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002020static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2021 size_t len, loff_t *pos)
2022{
2023 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002024 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002025
2026 if (!access_ok(VERIFY_WRITE, buf, len))
2027 return -EFAULT;
2028
2029 spu_acquire_saved(ctx);
2030 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002031 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002032 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002033 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002034
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002035 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002036}
2037
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002038static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002039 .open = spufs_info_open,
2040 .read = spufs_wbox_info_read,
2041 .llseek = generic_file_llseek,
2042};
2043
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002044static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2045 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002046{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002047 struct spu_dma_info info;
2048 struct mfc_cq_sr *qp, *spuqp;
2049 int i;
2050
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002051 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2052 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2053 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2054 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2055 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2056 for (i = 0; i < 16; i++) {
2057 qp = &info.dma_info_command_data[i];
2058 spuqp = &ctx->csa.priv2.spuq[i];
2059
2060 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2061 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2062 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2063 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2064 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002065
2066 return simple_read_from_buffer(buf, len, pos, &info,
2067 sizeof info);
2068}
2069
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002070static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2071 size_t len, loff_t *pos)
2072{
2073 struct spu_context *ctx = file->private_data;
2074 int ret;
2075
2076 if (!access_ok(VERIFY_WRITE, buf, len))
2077 return -EFAULT;
2078
2079 spu_acquire_saved(ctx);
2080 spin_lock(&ctx->csa.register_lock);
2081 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2082 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002083 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002084
2085 return ret;
2086}
2087
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002088static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002089 .open = spufs_info_open,
2090 .read = spufs_dma_info_read,
2091};
2092
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002093static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2094 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002095{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002096 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002097 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002098 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002099 int i;
2100
2101 if (len < ret)
2102 return -EINVAL;
2103
2104 if (!access_ok(VERIFY_WRITE, buf, len))
2105 return -EFAULT;
2106
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002107 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2108 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2109 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2110 for (i = 0; i < 8; i++) {
2111 qp = &info.proxydma_info_command_data[i];
2112 puqp = &ctx->csa.priv2.puq[i];
2113
2114 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2115 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2116 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2117 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2118 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002119
2120 return simple_read_from_buffer(buf, len, pos, &info,
2121 sizeof info);
2122}
2123
2124static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2125 size_t len, loff_t *pos)
2126{
2127 struct spu_context *ctx = file->private_data;
2128 int ret;
2129
2130 spu_acquire_saved(ctx);
2131 spin_lock(&ctx->csa.register_lock);
2132 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002133 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002134 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002135
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002136 return ret;
2137}
2138
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002139static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002140 .open = spufs_info_open,
2141 .read = spufs_proxydma_info_read,
2142};
2143
Christoph Hellwig476273a2007-06-29 10:58:01 +10002144static int spufs_show_tid(struct seq_file *s, void *private)
2145{
2146 struct spu_context *ctx = s->private;
2147
2148 seq_printf(s, "%d\n", ctx->tid);
2149 return 0;
2150}
2151
2152static int spufs_tid_open(struct inode *inode, struct file *file)
2153{
2154 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2155}
2156
2157static const struct file_operations spufs_tid_fops = {
2158 .open = spufs_tid_open,
2159 .read = seq_read,
2160 .llseek = seq_lseek,
2161 .release = single_release,
2162};
2163
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002164static const char *ctx_state_names[] = {
2165 "user", "system", "iowait", "loaded"
2166};
2167
2168static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002169 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002170{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002171 struct timespec ts;
2172 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002173
Andre Detsch27ec41d2007-07-20 21:39:33 +02002174 /*
2175 * In general, utilization statistics are updated by the controlling
2176 * thread as the spu context moves through various well defined
2177 * state transitions, but if the context is lazily loaded its
2178 * utilization statistics are not updated as the controlling thread
2179 * is not tightly coupled with the execution of the spu context. We
2180 * calculate and apply the time delta from the last recorded state
2181 * of the spu context.
2182 */
2183 if (ctx->spu && ctx->stats.util_state == state) {
2184 ktime_get_ts(&ts);
2185 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2186 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002187
Andre Detsch27ec41d2007-07-20 21:39:33 +02002188 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002189}
2190
2191static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2192{
2193 unsigned long long slb_flts = ctx->stats.slb_flt;
2194
2195 if (ctx->state == SPU_STATE_RUNNABLE) {
2196 slb_flts += (ctx->spu->stats.slb_flt -
2197 ctx->stats.slb_flt_base);
2198 }
2199
2200 return slb_flts;
2201}
2202
2203static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2204{
2205 unsigned long long class2_intrs = ctx->stats.class2_intr;
2206
2207 if (ctx->state == SPU_STATE_RUNNABLE) {
2208 class2_intrs += (ctx->spu->stats.class2_intr -
2209 ctx->stats.class2_intr_base);
2210 }
2211
2212 return class2_intrs;
2213}
2214
2215
2216static int spufs_show_stat(struct seq_file *s, void *private)
2217{
2218 struct spu_context *ctx = s->private;
2219
2220 spu_acquire(ctx);
2221 seq_printf(s, "%s %llu %llu %llu %llu "
2222 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002223 ctx_state_names[ctx->stats.util_state],
2224 spufs_acct_time(ctx, SPU_UTIL_USER),
2225 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2226 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2227 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002228 ctx->stats.vol_ctx_switch,
2229 ctx->stats.invol_ctx_switch,
2230 spufs_slb_flts(ctx),
2231 ctx->stats.hash_flt,
2232 ctx->stats.min_flt,
2233 ctx->stats.maj_flt,
2234 spufs_class2_intrs(ctx),
2235 ctx->stats.libassist);
2236 spu_release(ctx);
2237 return 0;
2238}
2239
2240static int spufs_stat_open(struct inode *inode, struct file *file)
2241{
2242 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2243}
2244
2245static const struct file_operations spufs_stat_fops = {
2246 .open = spufs_stat_open,
2247 .read = seq_read,
2248 .llseek = seq_lseek,
2249 .release = single_release,
2250};
2251
2252
Arnd Bergmann67207b92005-11-15 15:53:48 -05002253struct tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002254 { "capabilities", &spufs_caps_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002255 { "mem", &spufs_mem_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002256 { "regs", &spufs_regs_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002257 { "mbox", &spufs_mbox_fops, 0444, },
2258 { "ibox", &spufs_ibox_fops, 0444, },
2259 { "wbox", &spufs_wbox_fops, 0222, },
2260 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2261 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2262 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002263 { "signal1", &spufs_signal1_fops, 0666, },
2264 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002265 { "signal1_type", &spufs_signal1_type, 0666, },
2266 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002267 { "cntl", &spufs_cntl_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002268 { "fpcr", &spufs_fpcr_fops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002269 { "lslr", &spufs_lslr_ops, 0444, },
2270 { "mfc", &spufs_mfc_fops, 0666, },
2271 { "mss", &spufs_mss_fops, 0666, },
2272 { "npc", &spufs_npc_ops, 0666, },
2273 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002274 { "decr", &spufs_decr_ops, 0666, },
2275 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002276 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002277 { "event_status", &spufs_event_status_ops, 0444, },
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02002278 { "psmap", &spufs_psmap_fops, 0666, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002279 { "phys-id", &spufs_id_ops, 0666, },
2280 { "object-id", &spufs_object_id_ops, 0666, },
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002281 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2282 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2283 { "wbox_info", &spufs_wbox_info_fops, 0444, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002284 { "dma_info", &spufs_dma_info_fops, 0444, },
2285 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002286 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002287 { "stat", &spufs_stat_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002288 {},
2289};
Mark Nutter5737edd2006-10-24 18:31:16 +02002290
2291struct tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002292 { "capabilities", &spufs_caps_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002293 { "mem", &spufs_mem_fops, 0666, },
2294 { "mbox", &spufs_mbox_fops, 0444, },
2295 { "ibox", &spufs_ibox_fops, 0444, },
2296 { "wbox", &spufs_wbox_fops, 0222, },
2297 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2298 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2299 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002300 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2301 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002302 { "signal1_type", &spufs_signal1_type, 0666, },
2303 { "signal2_type", &spufs_signal2_type, 0666, },
2304 { "mss", &spufs_mss_fops, 0666, },
2305 { "mfc", &spufs_mfc_fops, 0666, },
2306 { "cntl", &spufs_cntl_fops, 0666, },
2307 { "npc", &spufs_npc_ops, 0666, },
2308 { "psmap", &spufs_psmap_fops, 0666, },
2309 { "phys-id", &spufs_id_ops, 0666, },
2310 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002311 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002312 { "stat", &spufs_stat_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002313 {},
2314};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002315
2316struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002317 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2318 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002319 { "lslr", NULL, spufs_lslr_get, 19 },
2320 { "decr", NULL, spufs_decr_get, 19 },
2321 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002322 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2323 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002324 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002325 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002326 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2327 { "event_mask", NULL, spufs_event_mask_get, 19 },
2328 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002329 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2330 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2331 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2332 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2333 { "proxydma_info", __spufs_proxydma_info_read,
2334 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002335 { "object-id", NULL, spufs_object_id_get, 19 },
2336 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002337 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002338};