blob: 89a50110a3c50e8bf076f8f8a43195523f2316fc [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>
Arnd Bergmann67207b92005-11-15 15:53:48 -050031
32#include <asm/io.h>
33#include <asm/semaphore.h>
34#include <asm/spu.h>
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +010035#include <asm/spu_info.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050036#include <asm/uaccess.h>
37
38#include "spufs.h"
39
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +020040#define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
41
Arnd Bergmann67207b92005-11-15 15:53:48 -050042static int
43spufs_mem_open(struct inode *inode, struct file *file)
44{
45 struct spufs_inode_info *i = SPUFS_I(inode);
Mark Nutter6df10a82006-03-23 00:00:12 +010046 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +020047
48 spin_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +010049 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +020050 if (!i->i_openers++)
51 ctx->local_store = inode->i_mapping;
52 spin_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +020053 return 0;
54}
55
56static int
57spufs_mem_release(struct inode *inode, struct file *file)
58{
59 struct spufs_inode_info *i = SPUFS_I(inode);
60 struct spu_context *ctx = i->i_ctx;
61
62 spin_lock(&ctx->mapping_lock);
63 if (!--i->i_openers)
64 ctx->local_store = NULL;
65 spin_unlock(&ctx->mapping_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -050066 return 0;
67}
68
69static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010070__spufs_mem_read(struct spu_context *ctx, char __user *buffer,
71 size_t size, loff_t *pos)
72{
73 char *local_store = ctx->ops->get_ls(ctx);
74 return simple_read_from_buffer(buffer, size, pos, local_store,
75 LS_SIZE);
76}
77
78static ssize_t
Arnd Bergmann67207b92005-11-15 15:53:48 -050079spufs_mem_read(struct file *file, char __user *buffer,
80 size_t size, loff_t *pos)
81{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010082 struct spu_context *ctx = file->private_data;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +010083 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -050084
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050085 spu_acquire(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +010086 ret = __spufs_mem_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050087 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -050088 return ret;
89}
90
91static ssize_t
92spufs_mem_write(struct file *file, const char __user *buffer,
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +010093 size_t size, loff_t *ppos)
Arnd Bergmann67207b92005-11-15 15:53:48 -050094{
95 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050096 char *local_store;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +010097 loff_t pos = *ppos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -050098 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -050099
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100100 if (pos < 0)
101 return -EINVAL;
102 if (pos > LS_SIZE)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500103 return -EFBIG;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100104 if (size > LS_SIZE - pos)
105 size = LS_SIZE - pos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500106
107 spu_acquire(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500108 local_store = ctx->ops->get_ls(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100109 ret = copy_from_user(local_store + pos, buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500110 spu_release(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100111
112 if (ret)
113 return -EFAULT;
114 *ppos = pos + size;
115 return size;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500116}
117
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100118static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
119 unsigned long address)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500120{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000121 struct spu_context *ctx = vma->vm_file->private_data;
122 unsigned long pfn, offset, addr0 = address;
123#ifdef CONFIG_SPU_FS_64K_LS
124 struct spu_state *csa = &ctx->csa;
125 int psize;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100126
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000127 /* Check what page size we are using */
128 psize = get_slice_psize(vma->vm_mm, address);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500129
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000130 /* Some sanity checking */
131 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
132
133 /* Wow, 64K, cool, we need to align the address though */
134 if (csa->use_big_pages) {
135 BUG_ON(vma->vm_start & 0xffff);
136 address &= ~0xfffful;
137 }
138#endif /* CONFIG_SPU_FS_64K_LS */
139
140 offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
Masato Noguchi128b8542007-02-13 21:54:30 +0100141 if (offset >= LS_SIZE)
142 return NOPFN_SIGBUS;
143
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000144 pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
145 addr0, address, offset);
146
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500147 spu_acquire(ctx);
148
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200149 if (ctx->state == SPU_STATE_SAVED) {
150 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Arnd Bergmann932f535d2006-11-20 18:45:06 +0100151 & ~_PAGE_NO_CACHE);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100152 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200153 } else {
154 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100155 | _PAGE_NO_CACHE);
156 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200157 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100158 vm_insert_pfn(vma, address, pfn);
159
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500160 spu_release(ctx);
161
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100162 return NOPFN_REFAULT;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500163}
164
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100165
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500166static struct vm_operations_struct spufs_mem_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100167 .nopfn = spufs_mem_mmap_nopfn,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500168};
169
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000170static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500171{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000172#ifdef CONFIG_SPU_FS_64K_LS
173 struct spu_context *ctx = file->private_data;
174 struct spu_state *csa = &ctx->csa;
175
176 /* Sanity check VMA alignment */
177 if (csa->use_big_pages) {
178 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
179 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
180 vma->vm_pgoff);
181 if (vma->vm_start & 0xffff)
182 return -EINVAL;
183 if (vma->vm_pgoff & 0xf)
184 return -EINVAL;
185 }
186#endif /* CONFIG_SPU_FS_64K_LS */
187
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500188 if (!(vma->vm_flags & VM_SHARED))
189 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500190
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100191 vma->vm_flags |= VM_IO | VM_PFNMAP;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500192 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
193 | _PAGE_NO_CACHE);
194
195 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196 return 0;
197}
198
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000199#ifdef CONFIG_SPU_FS_64K_LS
200unsigned long spufs_get_unmapped_area(struct file *file, unsigned long addr,
201 unsigned long len, unsigned long pgoff,
202 unsigned long flags)
203{
204 struct spu_context *ctx = file->private_data;
205 struct spu_state *csa = &ctx->csa;
206
207 /* If not using big pages, fallback to normal MM g_u_a */
208 if (!csa->use_big_pages)
209 return current->mm->get_unmapped_area(file, addr, len,
210 pgoff, flags);
211
212 /* Else, try to obtain a 64K pages slice */
213 return slice_get_unmapped_area(addr, len, flags,
214 MMU_PAGE_64K, 1, 0);
215}
216#endif /* CONFIG_SPU_FS_64K_LS */
217
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800218static const struct file_operations spufs_mem_fops = {
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000219 .open = spufs_mem_open,
Christoph Hellwigce929872007-06-04 23:26:51 +1000220 .release = spufs_mem_release,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000221 .read = spufs_mem_read,
222 .write = spufs_mem_write,
223 .llseek = generic_file_llseek,
224 .mmap = spufs_mem_mmap,
225#ifdef CONFIG_SPU_FS_64K_LS
226 .get_unmapped_area = spufs_get_unmapped_area,
227#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500228};
229
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100230static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
Mark Nutter6df10a82006-03-23 00:00:12 +0100231 unsigned long address,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100232 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200233 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100234{
Mark Nutter6df10a82006-03-23 00:00:12 +0100235 struct spu_context *ctx = vma->vm_file->private_data;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100236 unsigned long area, offset = address - vma->vm_start;
Mark Nutter6df10a82006-03-23 00:00:12 +0100237 int ret;
238
239 offset += vma->vm_pgoff << PAGE_SHIFT;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200240 if (offset >= ps_size)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100241 return NOPFN_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100242
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100243 /* error here usually means a signal.. we might want to test
244 * the error code more precisely though
245 */
Christoph Hellwig26bec672007-02-13 21:54:24 +0100246 ret = spu_acquire_runnable(ctx, 0);
Mark Nutter6df10a82006-03-23 00:00:12 +0100247 if (ret)
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100248 return NOPFN_REFAULT;
Mark Nutter6df10a82006-03-23 00:00:12 +0100249
250 area = ctx->spu->problem_phys + ps_offs;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100251 vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
Mark Nutter6df10a82006-03-23 00:00:12 +0100252 spu_release(ctx);
253
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100254 return NOPFN_REFAULT;
Mark Nutter6df10a82006-03-23 00:00:12 +0100255}
256
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200257#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100258static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
259 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +0100260{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100261 return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +0100262}
263
264static struct vm_operations_struct spufs_cntl_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100265 .nopfn = spufs_cntl_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +0100266};
267
268/*
269 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100270 */
271static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
272{
273 if (!(vma->vm_flags & VM_SHARED))
274 return -EINVAL;
275
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100276 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100277 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200278 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100279
280 vma->vm_ops = &spufs_cntl_mmap_vmops;
281 return 0;
282}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200283#else /* SPUFS_MMAP_4K */
284#define spufs_cntl_mmap NULL
285#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100286
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200287static u64 spufs_cntl_get(void *data)
288{
289 struct spu_context *ctx = data;
290 u64 val;
291
292 spu_acquire(ctx);
293 val = ctx->ops->status_read(ctx);
294 spu_release(ctx);
295
296 return val;
297}
298
299static void spufs_cntl_set(void *data, u64 val)
300{
301 struct spu_context *ctx = data;
302
303 spu_acquire(ctx);
304 ctx->ops->runcntl_write(ctx, val);
305 spu_release(ctx);
306}
307
Mark Nutter6df10a82006-03-23 00:00:12 +0100308static int spufs_cntl_open(struct inode *inode, struct file *file)
309{
310 struct spufs_inode_info *i = SPUFS_I(inode);
311 struct spu_context *ctx = i->i_ctx;
312
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200313 spin_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100314 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200315 if (!i->i_openers++)
316 ctx->cntl = inode->i_mapping;
317 spin_unlock(&ctx->mapping_lock);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200318 return simple_attr_open(inode, file, spufs_cntl_get,
319 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100320}
321
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200322static int
323spufs_cntl_release(struct inode *inode, struct file *file)
324{
325 struct spufs_inode_info *i = SPUFS_I(inode);
326 struct spu_context *ctx = i->i_ctx;
327
328 simple_attr_close(inode, file);
329
330 spin_lock(&ctx->mapping_lock);
331 if (!--i->i_openers)
332 ctx->cntl = NULL;
333 spin_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200334 return 0;
335}
336
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800337static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100338 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200339 .release = spufs_cntl_release,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200340 .read = simple_attr_read,
341 .write = simple_attr_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100342 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100343};
344
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500345static int
346spufs_regs_open(struct inode *inode, struct file *file)
347{
348 struct spufs_inode_info *i = SPUFS_I(inode);
349 file->private_data = i->i_ctx;
350 return 0;
351}
352
353static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100354__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
355 size_t size, loff_t *pos)
356{
357 struct spu_lscsa *lscsa = ctx->csa.lscsa;
358 return simple_read_from_buffer(buffer, size, pos,
359 lscsa->gprs, sizeof lscsa->gprs);
360}
361
362static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500363spufs_regs_read(struct file *file, char __user *buffer,
364 size_t size, loff_t *pos)
365{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500366 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100367 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500368
369 spu_acquire_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100370 ret = __spufs_regs_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500371 spu_release(ctx);
372 return ret;
373}
374
375static ssize_t
376spufs_regs_write(struct file *file, const char __user *buffer,
377 size_t size, loff_t *pos)
378{
379 struct spu_context *ctx = file->private_data;
380 struct spu_lscsa *lscsa = ctx->csa.lscsa;
381 int ret;
382
383 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
384 if (size <= 0)
385 return -EFBIG;
386 *pos += size;
387
388 spu_acquire_saved(ctx);
389
390 ret = copy_from_user(lscsa->gprs + *pos - size,
391 buffer, size) ? -EFAULT : size;
392
393 spu_release(ctx);
394 return ret;
395}
396
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800397static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500398 .open = spufs_regs_open,
399 .read = spufs_regs_read,
400 .write = spufs_regs_write,
401 .llseek = generic_file_llseek,
402};
403
404static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100405__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
406 size_t size, loff_t * pos)
407{
408 struct spu_lscsa *lscsa = ctx->csa.lscsa;
409 return simple_read_from_buffer(buffer, size, pos,
410 &lscsa->fpcr, sizeof(lscsa->fpcr));
411}
412
413static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500414spufs_fpcr_read(struct file *file, char __user * buffer,
415 size_t size, loff_t * pos)
416{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500417 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100418 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500419
420 spu_acquire_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100421 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500422 spu_release(ctx);
423 return ret;
424}
425
426static ssize_t
427spufs_fpcr_write(struct file *file, const char __user * buffer,
428 size_t size, loff_t * pos)
429{
430 struct spu_context *ctx = file->private_data;
431 struct spu_lscsa *lscsa = ctx->csa.lscsa;
432 int ret;
433
434 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
435 if (size <= 0)
436 return -EFBIG;
437 *pos += size;
438
439 spu_acquire_saved(ctx);
440
441 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
442 buffer, size) ? -EFAULT : size;
443
444 spu_release(ctx);
445 return ret;
446}
447
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800448static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500449 .open = spufs_regs_open,
450 .read = spufs_fpcr_read,
451 .write = spufs_fpcr_write,
452 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500453};
454
455/* generic open function for all pipe-like files */
456static int spufs_pipe_open(struct inode *inode, struct file *file)
457{
458 struct spufs_inode_info *i = SPUFS_I(inode);
459 file->private_data = i->i_ctx;
460
461 return nonseekable_open(inode, file);
462}
463
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200464/*
465 * Read as many bytes from the mailbox as possible, until
466 * one of the conditions becomes true:
467 *
468 * - no more data available in the mailbox
469 * - end of the user provided buffer
470 * - end of the mapped area
471 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500472static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
473 size_t len, loff_t *pos)
474{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500475 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200476 u32 mbox_data, __user *udata;
477 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500478
479 if (len < 4)
480 return -EINVAL;
481
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200482 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500483 return -EFAULT;
484
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200485 udata = (void __user *)buf;
486
487 spu_acquire(ctx);
Arnd Bergmann274cef52006-10-24 18:01:42 +0200488 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200489 int ret;
490 ret = ctx->ops->mbox_read(ctx, &mbox_data);
491 if (ret == 0)
492 break;
493
494 /*
495 * at the end of the mapped area, we can fault
496 * but still need to return the data we have
497 * read successfully so far.
498 */
499 ret = __put_user(mbox_data, udata);
500 if (ret) {
501 if (!count)
502 count = -EFAULT;
503 break;
504 }
505 }
506 spu_release(ctx);
507
508 if (!count)
509 count = -EAGAIN;
510
511 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500512}
513
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800514static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500515 .open = spufs_pipe_open,
516 .read = spufs_mbox_read,
517};
518
519static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
520 size_t len, loff_t *pos)
521{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500522 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500523 u32 mbox_stat;
524
525 if (len < 4)
526 return -EINVAL;
527
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500528 spu_acquire(ctx);
529
530 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
531
532 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500533
534 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
535 return -EFAULT;
536
537 return 4;
538}
539
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800540static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500541 .open = spufs_pipe_open,
542 .read = spufs_mbox_stat_read,
543};
544
545/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500546size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500547{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500548 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500549}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500550
551static int spufs_ibox_fasync(int fd, struct file *file, int on)
552{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500553 struct spu_context *ctx = file->private_data;
554
555 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
556}
557
558/* interrupt-level ibox callback function. */
559void spufs_ibox_callback(struct spu *spu)
560{
561 struct spu_context *ctx = spu->ctx;
562
563 wake_up_all(&ctx->ibox_wq);
564 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500565}
566
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200567/*
568 * Read as many bytes from the interrupt mailbox as possible, until
569 * one of the conditions becomes true:
570 *
571 * - no more data available in the mailbox
572 * - end of the user provided buffer
573 * - end of the mapped area
574 *
575 * If the file is opened without O_NONBLOCK, we wait here until
576 * any data is available, but return when we have been able to
577 * read something.
578 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500579static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
580 size_t len, loff_t *pos)
581{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500582 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200583 u32 ibox_data, __user *udata;
584 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500585
586 if (len < 4)
587 return -EINVAL;
588
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200589 if (!access_ok(VERIFY_WRITE, buf, len))
590 return -EFAULT;
591
592 udata = (void __user *)buf;
593
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500594 spu_acquire(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500595
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200596 /* wait only for the first element */
597 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500598 if (file->f_flags & O_NONBLOCK) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500599 if (!spu_ibox_read(ctx, &ibox_data))
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200600 count = -EAGAIN;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500601 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200602 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
603 }
604 if (count)
605 goto out;
606
607 /* if we can't write at all, return -EFAULT */
608 count = __put_user(ibox_data, udata);
609 if (count)
610 goto out;
611
612 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
613 int ret;
614 ret = ctx->ops->ibox_read(ctx, &ibox_data);
615 if (ret == 0)
616 break;
617 /*
618 * at the end of the mapped area, we can fault
619 * but still need to return the data we have
620 * read successfully so far.
621 */
622 ret = __put_user(ibox_data, udata);
623 if (ret)
624 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500625 }
626
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200627out:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500628 spu_release(ctx);
629
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200630 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500631}
632
633static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
634{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500635 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500636 unsigned int mask;
637
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500638 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500639
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500640 spu_acquire(ctx);
641 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
642 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500643
644 return mask;
645}
646
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800647static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500648 .open = spufs_pipe_open,
649 .read = spufs_ibox_read,
650 .poll = spufs_ibox_poll,
651 .fasync = spufs_ibox_fasync,
652};
653
654static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
655 size_t len, loff_t *pos)
656{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500657 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500658 u32 ibox_stat;
659
660 if (len < 4)
661 return -EINVAL;
662
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500663 spu_acquire(ctx);
664 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
665 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500666
667 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
668 return -EFAULT;
669
670 return 4;
671}
672
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800673static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500674 .open = spufs_pipe_open,
675 .read = spufs_ibox_stat_read,
676};
677
678/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500679size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500680{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500681 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500682}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500683
684static int spufs_wbox_fasync(int fd, struct file *file, int on)
685{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500686 struct spu_context *ctx = file->private_data;
687 int ret;
688
689 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
690
691 return ret;
692}
693
694/* interrupt-level wbox callback function. */
695void spufs_wbox_callback(struct spu *spu)
696{
697 struct spu_context *ctx = spu->ctx;
698
699 wake_up_all(&ctx->wbox_wq);
700 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500701}
702
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200703/*
704 * Write as many bytes to the interrupt mailbox as possible, until
705 * one of the conditions becomes true:
706 *
707 * - the mailbox is full
708 * - end of the user provided buffer
709 * - end of the mapped area
710 *
711 * If the file is opened without O_NONBLOCK, we wait here until
712 * space is availabyl, but return when we have been able to
713 * write something.
714 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500715static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
716 size_t len, loff_t *pos)
717{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500718 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200719 u32 wbox_data, __user *udata;
720 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500721
722 if (len < 4)
723 return -EINVAL;
724
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200725 udata = (void __user *)buf;
726 if (!access_ok(VERIFY_READ, buf, len))
727 return -EFAULT;
728
729 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500730 return -EFAULT;
731
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500732 spu_acquire(ctx);
733
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200734 /*
735 * make sure we can at least write one element, by waiting
736 * in case of !O_NONBLOCK
737 */
738 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500739 if (file->f_flags & O_NONBLOCK) {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500740 if (!spu_wbox_write(ctx, wbox_data))
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200741 count = -EAGAIN;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500742 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200743 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Arnd Bergmann67207b92005-11-15 15:53:48 -0500744 }
745
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200746 if (count)
747 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500748
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200749 /* write aѕ much as possible */
750 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
751 int ret;
752 ret = __get_user(wbox_data, udata);
753 if (ret)
754 break;
755
756 ret = spu_wbox_write(ctx, wbox_data);
757 if (ret == 0)
758 break;
759 }
760
761out:
762 spu_release(ctx);
763 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500764}
765
766static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
767{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500768 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500769 unsigned int mask;
770
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500771 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500772
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500773 spu_acquire(ctx);
774 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
775 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500776
777 return mask;
778}
779
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800780static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500781 .open = spufs_pipe_open,
782 .write = spufs_wbox_write,
783 .poll = spufs_wbox_poll,
784 .fasync = spufs_wbox_fasync,
785};
786
787static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
788 size_t len, loff_t *pos)
789{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500790 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500791 u32 wbox_stat;
792
793 if (len < 4)
794 return -EINVAL;
795
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500796 spu_acquire(ctx);
797 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
798 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500799
800 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
801 return -EFAULT;
802
803 return 4;
804}
805
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800806static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500807 .open = spufs_pipe_open,
808 .read = spufs_wbox_stat_read,
809};
810
Mark Nutter6df10a82006-03-23 00:00:12 +0100811static int spufs_signal1_open(struct inode *inode, struct file *file)
812{
813 struct spufs_inode_info *i = SPUFS_I(inode);
814 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200815
816 spin_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100817 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200818 if (!i->i_openers++)
819 ctx->signal1 = inode->i_mapping;
820 spin_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100821 return nonseekable_open(inode, file);
822}
823
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200824static int
825spufs_signal1_release(struct inode *inode, struct file *file)
826{
827 struct spufs_inode_info *i = SPUFS_I(inode);
828 struct spu_context *ctx = i->i_ctx;
829
830 spin_lock(&ctx->mapping_lock);
831 if (!--i->i_openers)
832 ctx->signal1 = NULL;
833 spin_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200834 return 0;
835}
836
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100837static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500838 size_t len, loff_t *pos)
839{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100840 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500841 u32 data;
842
Arnd Bergmann67207b92005-11-15 15:53:48 -0500843 if (len < 4)
844 return -EINVAL;
845
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100846 if (ctx->csa.spu_chnlcnt_RW[3]) {
847 data = ctx->csa.spu_chnldata_RW[3];
848 ret = 4;
849 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500850
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100851 if (!ret)
852 goto out;
853
Arnd Bergmann67207b92005-11-15 15:53:48 -0500854 if (copy_to_user(buf, &data, 4))
855 return -EFAULT;
856
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100857out:
858 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500859}
860
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100861static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
862 size_t len, loff_t *pos)
863{
864 int ret;
865 struct spu_context *ctx = file->private_data;
866
867 spu_acquire_saved(ctx);
868 ret = __spufs_signal1_read(ctx, buf, len, pos);
869 spu_release(ctx);
870
871 return ret;
872}
873
Arnd Bergmann67207b92005-11-15 15:53:48 -0500874static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
875 size_t len, loff_t *pos)
876{
877 struct spu_context *ctx;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500878 u32 data;
879
880 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500881
882 if (len < 4)
883 return -EINVAL;
884
885 if (copy_from_user(&data, buf, 4))
886 return -EFAULT;
887
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500888 spu_acquire(ctx);
889 ctx->ops->signal1_write(ctx, data);
890 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500891
892 return 4;
893}
894
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100895static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
896 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +0100897{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200898#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100899 return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200900#elif PAGE_SIZE == 0x10000
901 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
902 * signal 1 and 2 area
903 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100904 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200905#else
906#error unsupported page size
907#endif
Mark Nutter6df10a82006-03-23 00:00:12 +0100908}
909
910static struct vm_operations_struct spufs_signal1_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100911 .nopfn = spufs_signal1_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +0100912};
913
914static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
915{
916 if (!(vma->vm_flags & VM_SHARED))
917 return -EINVAL;
918
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100919 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +0100920 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +0200921 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +0100922
923 vma->vm_ops = &spufs_signal1_mmap_vmops;
924 return 0;
925}
Mark Nutter6df10a82006-03-23 00:00:12 +0100926
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800927static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100928 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200929 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500930 .read = spufs_signal1_read,
931 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +0100932 .mmap = spufs_signal1_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500933};
934
Mark Nutter6df10a82006-03-23 00:00:12 +0100935static int spufs_signal2_open(struct inode *inode, struct file *file)
936{
937 struct spufs_inode_info *i = SPUFS_I(inode);
938 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200939
940 spin_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100941 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200942 if (!i->i_openers++)
943 ctx->signal2 = inode->i_mapping;
944 spin_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100945 return nonseekable_open(inode, file);
946}
947
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200948static int
949spufs_signal2_release(struct inode *inode, struct file *file)
950{
951 struct spufs_inode_info *i = SPUFS_I(inode);
952 struct spu_context *ctx = i->i_ctx;
953
954 spin_lock(&ctx->mapping_lock);
955 if (!--i->i_openers)
956 ctx->signal2 = NULL;
957 spin_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200958 return 0;
959}
960
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100961static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500962 size_t len, loff_t *pos)
963{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100964 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500965 u32 data;
966
Arnd Bergmann67207b92005-11-15 15:53:48 -0500967 if (len < 4)
968 return -EINVAL;
969
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100970 if (ctx->csa.spu_chnlcnt_RW[4]) {
971 data = ctx->csa.spu_chnldata_RW[4];
972 ret = 4;
973 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500974
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100975 if (!ret)
976 goto out;
977
Arnd Bergmann67207b92005-11-15 15:53:48 -0500978 if (copy_to_user(buf, &data, 4))
979 return -EFAULT;
980
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +0100981out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100982 return ret;
983}
984
985static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
986 size_t len, loff_t *pos)
987{
988 struct spu_context *ctx = file->private_data;
989 int ret;
990
991 spu_acquire_saved(ctx);
992 ret = __spufs_signal2_read(ctx, buf, len, pos);
993 spu_release(ctx);
994
995 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500996}
997
998static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
999 size_t len, loff_t *pos)
1000{
1001 struct spu_context *ctx;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001002 u32 data;
1003
1004 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001005
1006 if (len < 4)
1007 return -EINVAL;
1008
1009 if (copy_from_user(&data, buf, 4))
1010 return -EFAULT;
1011
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001012 spu_acquire(ctx);
1013 ctx->ops->signal2_write(ctx, data);
1014 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001015
1016 return 4;
1017}
1018
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001019#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001020static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
1021 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001022{
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001023#if PAGE_SIZE == 0x1000
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001024 return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001025#elif PAGE_SIZE == 0x10000
1026 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1027 * signal 1 and 2 area
1028 */
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001029 return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001030#else
1031#error unsupported page size
1032#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001033}
1034
1035static struct vm_operations_struct spufs_signal2_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001036 .nopfn = spufs_signal2_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001037};
1038
1039static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1040{
1041 if (!(vma->vm_flags & VM_SHARED))
1042 return -EINVAL;
1043
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001044 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001045 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001046 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001047
1048 vma->vm_ops = &spufs_signal2_mmap_vmops;
1049 return 0;
1050}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001051#else /* SPUFS_MMAP_4K */
1052#define spufs_signal2_mmap NULL
1053#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001054
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001055static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001056 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001057 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001058 .read = spufs_signal2_read,
1059 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001060 .mmap = spufs_signal2_mmap,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001061};
1062
1063static void spufs_signal1_type_set(void *data, u64 val)
1064{
1065 struct spu_context *ctx = data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001066
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001067 spu_acquire(ctx);
1068 ctx->ops->signal1_type_set(ctx, val);
1069 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001070}
1071
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001072static u64 __spufs_signal1_type_get(void *data)
1073{
1074 struct spu_context *ctx = data;
1075 return ctx->ops->signal1_type_get(ctx);
1076}
1077
Arnd Bergmann67207b92005-11-15 15:53:48 -05001078static u64 spufs_signal1_type_get(void *data)
1079{
1080 struct spu_context *ctx = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001081 u64 ret;
1082
1083 spu_acquire(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001084 ret = __spufs_signal1_type_get(data);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001085 spu_release(ctx);
1086
1087 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001088}
1089DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1090 spufs_signal1_type_set, "%llu");
1091
1092static void spufs_signal2_type_set(void *data, u64 val)
1093{
1094 struct spu_context *ctx = data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001095
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001096 spu_acquire(ctx);
1097 ctx->ops->signal2_type_set(ctx, val);
1098 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001099}
1100
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001101static u64 __spufs_signal2_type_get(void *data)
1102{
1103 struct spu_context *ctx = data;
1104 return ctx->ops->signal2_type_get(ctx);
1105}
1106
Arnd Bergmann67207b92005-11-15 15:53:48 -05001107static u64 spufs_signal2_type_get(void *data)
1108{
1109 struct spu_context *ctx = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001110 u64 ret;
1111
1112 spu_acquire(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001113 ret = __spufs_signal2_type_get(data);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001114 spu_release(ctx);
1115
1116 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001117}
1118DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1119 spufs_signal2_type_set, "%llu");
1120
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001121#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001122static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
1123 unsigned long address)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001124{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001125 return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001126}
1127
1128static struct vm_operations_struct spufs_mss_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001129 .nopfn = spufs_mss_mmap_nopfn,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001130};
1131
1132/*
1133 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001134 */
1135static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1136{
1137 if (!(vma->vm_flags & VM_SHARED))
1138 return -EINVAL;
1139
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001140 vma->vm_flags |= VM_IO | VM_PFNMAP;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001141 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001142 | _PAGE_NO_CACHE | _PAGE_GUARDED);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001143
1144 vma->vm_ops = &spufs_mss_mmap_vmops;
1145 return 0;
1146}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001147#else /* SPUFS_MMAP_4K */
1148#define spufs_mss_mmap NULL
1149#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001150
1151static int spufs_mss_open(struct inode *inode, struct file *file)
1152{
1153 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001154 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001155
1156 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001157
1158 spin_lock(&ctx->mapping_lock);
1159 if (!i->i_openers++)
1160 ctx->mss = inode->i_mapping;
1161 spin_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001162 return nonseekable_open(inode, file);
1163}
1164
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001165static int
1166spufs_mss_release(struct inode *inode, struct file *file)
1167{
1168 struct spufs_inode_info *i = SPUFS_I(inode);
1169 struct spu_context *ctx = i->i_ctx;
1170
1171 spin_lock(&ctx->mapping_lock);
1172 if (!--i->i_openers)
1173 ctx->mss = NULL;
1174 spin_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001175 return 0;
1176}
1177
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001178static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001179 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001180 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001181 .mmap = spufs_mss_mmap,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001182};
1183
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001184static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
1185 unsigned long address)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001186{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001187 return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001188}
1189
1190static struct vm_operations_struct spufs_psmap_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001191 .nopfn = spufs_psmap_mmap_nopfn,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001192};
1193
1194/*
1195 * mmap support for full problem state area [0x00000 - 0x1ffff].
1196 */
1197static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1198{
1199 if (!(vma->vm_flags & VM_SHARED))
1200 return -EINVAL;
1201
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001202 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001203 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
1204 | _PAGE_NO_CACHE | _PAGE_GUARDED);
1205
1206 vma->vm_ops = &spufs_psmap_mmap_vmops;
1207 return 0;
1208}
1209
1210static int spufs_psmap_open(struct inode *inode, struct file *file)
1211{
1212 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001213 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001214
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001215 spin_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001216 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001217 if (!i->i_openers++)
1218 ctx->psmap = inode->i_mapping;
1219 spin_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001220 return nonseekable_open(inode, file);
1221}
1222
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001223static int
1224spufs_psmap_release(struct inode *inode, struct file *file)
1225{
1226 struct spufs_inode_info *i = SPUFS_I(inode);
1227 struct spu_context *ctx = i->i_ctx;
1228
1229 spin_lock(&ctx->mapping_lock);
1230 if (!--i->i_openers)
1231 ctx->psmap = NULL;
1232 spin_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001233 return 0;
1234}
1235
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001236static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001237 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001238 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001239 .mmap = spufs_psmap_mmap,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001240};
1241
1242
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001243#if SPUFS_MMAP_4K
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001244static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
1245 unsigned long address)
Mark Nutter6df10a82006-03-23 00:00:12 +01001246{
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001247 return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
Mark Nutter6df10a82006-03-23 00:00:12 +01001248}
1249
1250static struct vm_operations_struct spufs_mfc_mmap_vmops = {
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001251 .nopfn = spufs_mfc_mmap_nopfn,
Mark Nutter6df10a82006-03-23 00:00:12 +01001252};
1253
1254/*
1255 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001256 */
1257static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1258{
1259 if (!(vma->vm_flags & VM_SHARED))
1260 return -EINVAL;
1261
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001262 vma->vm_flags |= VM_IO | VM_PFNMAP;
Mark Nutter6df10a82006-03-23 00:00:12 +01001263 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
Benjamin Herrenschmidt23cc7702006-06-23 20:57:47 +02001264 | _PAGE_NO_CACHE | _PAGE_GUARDED);
Mark Nutter6df10a82006-03-23 00:00:12 +01001265
1266 vma->vm_ops = &spufs_mfc_mmap_vmops;
1267 return 0;
1268}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001269#else /* SPUFS_MMAP_4K */
1270#define spufs_mfc_mmap NULL
1271#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001272
1273static int spufs_mfc_open(struct inode *inode, struct file *file)
1274{
1275 struct spufs_inode_info *i = SPUFS_I(inode);
1276 struct spu_context *ctx = i->i_ctx;
1277
1278 /* we don't want to deal with DMA into other processes */
1279 if (ctx->owner != current->mm)
1280 return -EINVAL;
1281
1282 if (atomic_read(&inode->i_count) != 1)
1283 return -EBUSY;
1284
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001285 spin_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001286 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001287 if (!i->i_openers++)
1288 ctx->mfc = inode->i_mapping;
1289 spin_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001290 return nonseekable_open(inode, file);
1291}
1292
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001293static int
1294spufs_mfc_release(struct inode *inode, struct file *file)
1295{
1296 struct spufs_inode_info *i = SPUFS_I(inode);
1297 struct spu_context *ctx = i->i_ctx;
1298
1299 spin_lock(&ctx->mapping_lock);
1300 if (!--i->i_openers)
1301 ctx->mfc = NULL;
1302 spin_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001303 return 0;
1304}
1305
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001306/* interrupt-level mfc callback function. */
1307void spufs_mfc_callback(struct spu *spu)
1308{
1309 struct spu_context *ctx = spu->ctx;
1310
1311 wake_up_all(&ctx->mfc_wq);
1312
1313 pr_debug("%s %s\n", __FUNCTION__, spu->name);
1314 if (ctx->mfc_fasync) {
1315 u32 free_elements, tagstatus;
1316 unsigned int mask;
1317
1318 /* no need for spu_acquire in interrupt context */
1319 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1320 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1321
1322 mask = 0;
1323 if (free_elements & 0xffff)
1324 mask |= POLLOUT;
1325 if (tagstatus & ctx->tagwait)
1326 mask |= POLLIN;
1327
1328 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1329 }
1330}
1331
1332static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1333{
1334 /* See if there is one tag group is complete */
1335 /* FIXME we need locking around tagwait */
1336 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1337 ctx->tagwait &= ~*status;
1338 if (*status)
1339 return 1;
1340
1341 /* enable interrupt waiting for any tag group,
1342 may silently fail if interrupts are already enabled */
1343 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1344 return 0;
1345}
1346
1347static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1348 size_t size, loff_t *pos)
1349{
1350 struct spu_context *ctx = file->private_data;
1351 int ret = -EINVAL;
1352 u32 status;
1353
1354 if (size != 4)
1355 goto out;
1356
1357 spu_acquire(ctx);
1358 if (file->f_flags & O_NONBLOCK) {
1359 status = ctx->ops->read_mfc_tagstatus(ctx);
1360 if (!(status & ctx->tagwait))
1361 ret = -EAGAIN;
1362 else
1363 ctx->tagwait &= ~status;
1364 } else {
1365 ret = spufs_wait(ctx->mfc_wq,
1366 spufs_read_mfc_tagstatus(ctx, &status));
1367 }
1368 spu_release(ctx);
1369
1370 if (ret)
1371 goto out;
1372
1373 ret = 4;
1374 if (copy_to_user(buffer, &status, 4))
1375 ret = -EFAULT;
1376
1377out:
1378 return ret;
1379}
1380
1381static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1382{
1383 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1384 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1385
1386 switch (cmd->cmd) {
1387 case MFC_PUT_CMD:
1388 case MFC_PUTF_CMD:
1389 case MFC_PUTB_CMD:
1390 case MFC_GET_CMD:
1391 case MFC_GETF_CMD:
1392 case MFC_GETB_CMD:
1393 break;
1394 default:
1395 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1396 return -EIO;
1397 }
1398
1399 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1400 pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1401 cmd->ea, cmd->lsa);
1402 return -EIO;
1403 }
1404
1405 switch (cmd->size & 0xf) {
1406 case 1:
1407 break;
1408 case 2:
1409 if (cmd->lsa & 1)
1410 goto error;
1411 break;
1412 case 4:
1413 if (cmd->lsa & 3)
1414 goto error;
1415 break;
1416 case 8:
1417 if (cmd->lsa & 7)
1418 goto error;
1419 break;
1420 case 0:
1421 if (cmd->lsa & 15)
1422 goto error;
1423 break;
1424 error:
1425 default:
1426 pr_debug("invalid DMA alignment %x for size %x\n",
1427 cmd->lsa & 0xf, cmd->size);
1428 return -EIO;
1429 }
1430
1431 if (cmd->size > 16 * 1024) {
1432 pr_debug("invalid DMA size %x\n", cmd->size);
1433 return -EIO;
1434 }
1435
1436 if (cmd->tag & 0xfff0) {
1437 /* we reserve the higher tag numbers for kernel use */
1438 pr_debug("invalid DMA tag\n");
1439 return -EIO;
1440 }
1441
1442 if (cmd->class) {
1443 /* not supported in this version */
1444 pr_debug("invalid DMA class\n");
1445 return -EIO;
1446 }
1447
1448 return 0;
1449}
1450
1451static int spu_send_mfc_command(struct spu_context *ctx,
1452 struct mfc_dma_command cmd,
1453 int *error)
1454{
1455 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1456 if (*error == -EAGAIN) {
1457 /* wait for any tag group to complete
1458 so we have space for the new command */
1459 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1460 /* try again, because the queue might be
1461 empty again */
1462 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1463 if (*error == -EAGAIN)
1464 return 0;
1465 }
1466 return 1;
1467}
1468
1469static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1470 size_t size, loff_t *pos)
1471{
1472 struct spu_context *ctx = file->private_data;
1473 struct mfc_dma_command cmd;
1474 int ret = -EINVAL;
1475
1476 if (size != sizeof cmd)
1477 goto out;
1478
1479 ret = -EFAULT;
1480 if (copy_from_user(&cmd, buffer, sizeof cmd))
1481 goto out;
1482
1483 ret = spufs_check_valid_dma(&cmd);
1484 if (ret)
1485 goto out;
1486
Akinobu Mita577f8f12007-04-23 21:08:18 +02001487 ret = spu_acquire_runnable(ctx, 0);
1488 if (ret)
1489 goto out;
1490
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001491 if (file->f_flags & O_NONBLOCK) {
1492 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1493 } else {
1494 int status;
1495 ret = spufs_wait(ctx->mfc_wq,
1496 spu_send_mfc_command(ctx, cmd, &status));
1497 if (status)
1498 ret = status;
1499 }
1500 spu_release(ctx);
1501
1502 if (ret)
1503 goto out;
1504
1505 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001506 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001507
1508out:
1509 return ret;
1510}
1511
1512static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1513{
1514 struct spu_context *ctx = file->private_data;
1515 u32 free_elements, tagstatus;
1516 unsigned int mask;
1517
1518 spu_acquire(ctx);
1519 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1520 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1521 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1522 spu_release(ctx);
1523
1524 poll_wait(file, &ctx->mfc_wq, wait);
1525
1526 mask = 0;
1527 if (free_elements & 0xffff)
1528 mask |= POLLOUT | POLLWRNORM;
1529 if (tagstatus & ctx->tagwait)
1530 mask |= POLLIN | POLLRDNORM;
1531
1532 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1533 free_elements, tagstatus, ctx->tagwait);
1534
1535 return mask;
1536}
1537
Al Viro73b6af82006-06-25 16:42:33 -07001538static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001539{
1540 struct spu_context *ctx = file->private_data;
1541 int ret;
1542
1543 spu_acquire(ctx);
1544#if 0
1545/* this currently hangs */
1546 ret = spufs_wait(ctx->mfc_wq,
1547 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1548 if (ret)
1549 goto out;
1550 ret = spufs_wait(ctx->mfc_wq,
1551 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1552out:
1553#else
1554 ret = 0;
1555#endif
1556 spu_release(ctx);
1557
1558 return ret;
1559}
1560
1561static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1562 int datasync)
1563{
Al Viro73b6af82006-06-25 16:42:33 -07001564 return spufs_mfc_flush(file, NULL);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001565}
1566
1567static int spufs_mfc_fasync(int fd, struct file *file, int on)
1568{
1569 struct spu_context *ctx = file->private_data;
1570
1571 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1572}
1573
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001574static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001575 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001576 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001577 .read = spufs_mfc_read,
1578 .write = spufs_mfc_write,
1579 .poll = spufs_mfc_poll,
1580 .flush = spufs_mfc_flush,
1581 .fsync = spufs_mfc_fsync,
1582 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001583 .mmap = spufs_mfc_mmap,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001584};
1585
Arnd Bergmann67207b92005-11-15 15:53:48 -05001586static void spufs_npc_set(void *data, u64 val)
1587{
1588 struct spu_context *ctx = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001589 spu_acquire(ctx);
1590 ctx->ops->npc_write(ctx, val);
1591 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001592}
1593
1594static u64 spufs_npc_get(void *data)
1595{
1596 struct spu_context *ctx = data;
1597 u64 ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001598 spu_acquire(ctx);
1599 ret = ctx->ops->npc_read(ctx);
1600 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001601 return ret;
1602}
Dwayne Grant McConnell9b5047e2006-11-20 18:44:57 +01001603DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1604 "0x%llx\n")
Arnd Bergmann67207b92005-11-15 15:53:48 -05001605
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001606static void spufs_decr_set(void *data, u64 val)
1607{
1608 struct spu_context *ctx = data;
1609 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1610 spu_acquire_saved(ctx);
1611 lscsa->decr.slot[0] = (u32) val;
1612 spu_release(ctx);
1613}
1614
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001615static u64 __spufs_decr_get(void *data)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001616{
1617 struct spu_context *ctx = data;
1618 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001619 return lscsa->decr.slot[0];
1620}
1621
1622static u64 spufs_decr_get(void *data)
1623{
1624 struct spu_context *ctx = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001625 u64 ret;
1626 spu_acquire_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001627 ret = __spufs_decr_get(data);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001628 spu_release(ctx);
1629 return ret;
1630}
1631DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
Dwayne Grant McConnell9b5047e2006-11-20 18:44:57 +01001632 "0x%llx\n")
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001633
1634static void spufs_decr_status_set(void *data, u64 val)
1635{
1636 struct spu_context *ctx = data;
1637 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1638 spu_acquire_saved(ctx);
1639 lscsa->decr_status.slot[0] = (u32) val;
1640 spu_release(ctx);
1641}
1642
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001643static u64 __spufs_decr_status_get(void *data)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001644{
1645 struct spu_context *ctx = data;
1646 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001647 return lscsa->decr_status.slot[0];
1648}
1649
1650static u64 spufs_decr_status_get(void *data)
1651{
1652 struct spu_context *ctx = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001653 u64 ret;
1654 spu_acquire_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001655 ret = __spufs_decr_status_get(data);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001656 spu_release(ctx);
1657 return ret;
1658}
1659DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
Dwayne Grant McConnell9b5047e2006-11-20 18:44:57 +01001660 spufs_decr_status_set, "0x%llx\n")
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001661
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001662static void spufs_event_mask_set(void *data, u64 val)
1663{
1664 struct spu_context *ctx = data;
1665 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1666 spu_acquire_saved(ctx);
1667 lscsa->event_mask.slot[0] = (u32) val;
1668 spu_release(ctx);
1669}
1670
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001671static u64 __spufs_event_mask_get(void *data)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001672{
1673 struct spu_context *ctx = data;
1674 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001675 return lscsa->event_mask.slot[0];
1676}
1677
1678static u64 spufs_event_mask_get(void *data)
1679{
1680 struct spu_context *ctx = data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001681 u64 ret;
1682 spu_acquire_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001683 ret = __spufs_event_mask_get(data);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001684 spu_release(ctx);
1685 return ret;
1686}
1687DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
Dwayne Grant McConnell9b5047e2006-11-20 18:44:57 +01001688 spufs_event_mask_set, "0x%llx\n")
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001689
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001690static u64 __spufs_event_status_get(void *data)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001691{
1692 struct spu_context *ctx = data;
1693 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001694 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001695 stat = state->spu_chnlcnt_RW[0];
1696 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001697 return state->spu_chnldata_RW[0];
1698 return 0;
1699}
1700
1701static u64 spufs_event_status_get(void *data)
1702{
1703 struct spu_context *ctx = data;
1704 u64 ret = 0;
1705
1706 spu_acquire_saved(ctx);
1707 ret = __spufs_event_status_get(data);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001708 spu_release(ctx);
1709 return ret;
1710}
1711DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1712 NULL, "0x%llx\n")
1713
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001714static void spufs_srr0_set(void *data, u64 val)
1715{
1716 struct spu_context *ctx = data;
1717 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1718 spu_acquire_saved(ctx);
1719 lscsa->srr0.slot[0] = (u32) val;
1720 spu_release(ctx);
1721}
1722
1723static u64 spufs_srr0_get(void *data)
1724{
1725 struct spu_context *ctx = data;
1726 struct spu_lscsa *lscsa = ctx->csa.lscsa;
1727 u64 ret;
1728 spu_acquire_saved(ctx);
1729 ret = lscsa->srr0.slot[0];
1730 spu_release(ctx);
1731 return ret;
1732}
1733DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
Dwayne Grant McConnell9b5047e2006-11-20 18:44:57 +01001734 "0x%llx\n")
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001735
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001736static u64 spufs_id_get(void *data)
1737{
1738 struct spu_context *ctx = data;
1739 u64 num;
1740
1741 spu_acquire(ctx);
1742 if (ctx->state == SPU_STATE_RUNNABLE)
1743 num = ctx->spu->number;
1744 else
1745 num = (unsigned int)-1;
1746 spu_release(ctx);
1747
1748 return num;
1749}
Al Viroe45d6632006-09-23 01:37:41 +01001750DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02001751
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001752static u64 __spufs_object_id_get(void *data)
Arnd Bergmann86767272006-10-04 17:26:21 +02001753{
1754 struct spu_context *ctx = data;
1755 return ctx->object_id;
1756}
1757
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001758static u64 spufs_object_id_get(void *data)
1759{
1760 /* FIXME: Should there really be no locking here? */
1761 return __spufs_object_id_get(data);
1762}
1763
Arnd Bergmann86767272006-10-04 17:26:21 +02001764static void spufs_object_id_set(void *data, u64 id)
1765{
1766 struct spu_context *ctx = data;
1767 ctx->object_id = id;
1768}
1769
1770DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
1771 spufs_object_id_set, "0x%llx\n");
1772
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001773static u64 __spufs_lslr_get(void *data)
1774{
1775 struct spu_context *ctx = data;
1776 return ctx->csa.priv2.spu_lslr_RW;
1777}
1778
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001779static u64 spufs_lslr_get(void *data)
1780{
1781 struct spu_context *ctx = data;
1782 u64 ret;
1783
1784 spu_acquire_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001785 ret = __spufs_lslr_get(data);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001786 spu_release(ctx);
1787
1788 return ret;
1789}
1790DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
1791
1792static int spufs_info_open(struct inode *inode, struct file *file)
1793{
1794 struct spufs_inode_info *i = SPUFS_I(inode);
1795 struct spu_context *ctx = i->i_ctx;
1796 file->private_data = ctx;
1797 return 0;
1798}
1799
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001800static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1801 char __user *buf, size_t len, loff_t *pos)
1802{
1803 u32 mbox_stat;
1804 u32 data;
1805
1806 mbox_stat = ctx->csa.prob.mb_stat_R;
1807 if (mbox_stat & 0x0000ff) {
1808 data = ctx->csa.prob.pu_mb_R;
1809 }
1810
1811 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1812}
1813
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001814static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
1815 size_t len, loff_t *pos)
1816{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001817 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001818 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001819
1820 if (!access_ok(VERIFY_WRITE, buf, len))
1821 return -EFAULT;
1822
1823 spu_acquire_saved(ctx);
1824 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001825 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001826 spin_unlock(&ctx->csa.register_lock);
1827 spu_release(ctx);
1828
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001829 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001830}
1831
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001832static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001833 .open = spufs_info_open,
1834 .read = spufs_mbox_info_read,
1835 .llseek = generic_file_llseek,
1836};
1837
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001838static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
1839 char __user *buf, size_t len, loff_t *pos)
1840{
1841 u32 ibox_stat;
1842 u32 data;
1843
1844 ibox_stat = ctx->csa.prob.mb_stat_R;
1845 if (ibox_stat & 0xff0000) {
1846 data = ctx->csa.priv2.puint_mb_R;
1847 }
1848
1849 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1850}
1851
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001852static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
1853 size_t len, loff_t *pos)
1854{
1855 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001856 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001857
1858 if (!access_ok(VERIFY_WRITE, buf, len))
1859 return -EFAULT;
1860
1861 spu_acquire_saved(ctx);
1862 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001863 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001864 spin_unlock(&ctx->csa.register_lock);
1865 spu_release(ctx);
1866
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001867 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001868}
1869
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001870static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001871 .open = spufs_info_open,
1872 .read = spufs_ibox_info_read,
1873 .llseek = generic_file_llseek,
1874};
1875
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001876static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
1877 char __user *buf, size_t len, loff_t *pos)
1878{
1879 int i, cnt;
1880 u32 data[4];
1881 u32 wbox_stat;
1882
1883 wbox_stat = ctx->csa.prob.mb_stat_R;
1884 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
1885 for (i = 0; i < cnt; i++) {
1886 data[i] = ctx->csa.spu_mailbox_data[i];
1887 }
1888
1889 return simple_read_from_buffer(buf, len, pos, &data,
1890 cnt * sizeof(u32));
1891}
1892
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001893static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
1894 size_t len, loff_t *pos)
1895{
1896 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001897 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001898
1899 if (!access_ok(VERIFY_WRITE, buf, len))
1900 return -EFAULT;
1901
1902 spu_acquire_saved(ctx);
1903 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001904 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001905 spin_unlock(&ctx->csa.register_lock);
1906 spu_release(ctx);
1907
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001908 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001909}
1910
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001911static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01001912 .open = spufs_info_open,
1913 .read = spufs_wbox_info_read,
1914 .llseek = generic_file_llseek,
1915};
1916
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001917static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
1918 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001919{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001920 struct spu_dma_info info;
1921 struct mfc_cq_sr *qp, *spuqp;
1922 int i;
1923
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001924 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
1925 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
1926 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
1927 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
1928 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
1929 for (i = 0; i < 16; i++) {
1930 qp = &info.dma_info_command_data[i];
1931 spuqp = &ctx->csa.priv2.spuq[i];
1932
1933 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
1934 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
1935 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
1936 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
1937 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001938
1939 return simple_read_from_buffer(buf, len, pos, &info,
1940 sizeof info);
1941}
1942
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001943static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
1944 size_t len, loff_t *pos)
1945{
1946 struct spu_context *ctx = file->private_data;
1947 int ret;
1948
1949 if (!access_ok(VERIFY_WRITE, buf, len))
1950 return -EFAULT;
1951
1952 spu_acquire_saved(ctx);
1953 spin_lock(&ctx->csa.register_lock);
1954 ret = __spufs_dma_info_read(ctx, buf, len, pos);
1955 spin_unlock(&ctx->csa.register_lock);
1956 spu_release(ctx);
1957
1958 return ret;
1959}
1960
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001961static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001962 .open = spufs_info_open,
1963 .read = spufs_dma_info_read,
1964};
1965
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001966static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
1967 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001968{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001969 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001970 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001971 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001972 int i;
1973
1974 if (len < ret)
1975 return -EINVAL;
1976
1977 if (!access_ok(VERIFY_WRITE, buf, len))
1978 return -EFAULT;
1979
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001980 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
1981 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
1982 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
1983 for (i = 0; i < 8; i++) {
1984 qp = &info.proxydma_info_command_data[i];
1985 puqp = &ctx->csa.priv2.puq[i];
1986
1987 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
1988 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
1989 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
1990 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
1991 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001992
1993 return simple_read_from_buffer(buf, len, pos, &info,
1994 sizeof info);
1995}
1996
1997static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
1998 size_t len, loff_t *pos)
1999{
2000 struct spu_context *ctx = file->private_data;
2001 int ret;
2002
2003 spu_acquire_saved(ctx);
2004 spin_lock(&ctx->csa.register_lock);
2005 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002006 spin_unlock(&ctx->csa.register_lock);
2007 spu_release(ctx);
2008
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002009 return ret;
2010}
2011
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002012static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002013 .open = spufs_info_open,
2014 .read = spufs_proxydma_info_read,
2015};
2016
Arnd Bergmann67207b92005-11-15 15:53:48 -05002017struct tree_descr spufs_dir_contents[] = {
2018 { "mem", &spufs_mem_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002019 { "regs", &spufs_regs_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002020 { "mbox", &spufs_mbox_fops, 0444, },
2021 { "ibox", &spufs_ibox_fops, 0444, },
2022 { "wbox", &spufs_wbox_fops, 0222, },
2023 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2024 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2025 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2026 { "signal1", &spufs_signal1_fops, 0666, },
2027 { "signal2", &spufs_signal2_fops, 0666, },
2028 { "signal1_type", &spufs_signal1_type, 0666, },
2029 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002030 { "cntl", &spufs_cntl_fops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002031 { "fpcr", &spufs_fpcr_fops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002032 { "lslr", &spufs_lslr_ops, 0444, },
2033 { "mfc", &spufs_mfc_fops, 0666, },
2034 { "mss", &spufs_mss_fops, 0666, },
2035 { "npc", &spufs_npc_ops, 0666, },
2036 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002037 { "decr", &spufs_decr_ops, 0666, },
2038 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002039 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002040 { "event_status", &spufs_event_status_ops, 0444, },
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02002041 { "psmap", &spufs_psmap_fops, 0666, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002042 { "phys-id", &spufs_id_ops, 0666, },
2043 { "object-id", &spufs_object_id_ops, 0666, },
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002044 { "mbox_info", &spufs_mbox_info_fops, 0444, },
2045 { "ibox_info", &spufs_ibox_info_fops, 0444, },
2046 { "wbox_info", &spufs_wbox_info_fops, 0444, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002047 { "dma_info", &spufs_dma_info_fops, 0444, },
2048 { "proxydma_info", &spufs_proxydma_info_fops, 0444, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002049 {},
2050};
Mark Nutter5737edd2006-10-24 18:31:16 +02002051
2052struct tree_descr spufs_dir_nosched_contents[] = {
2053 { "mem", &spufs_mem_fops, 0666, },
2054 { "mbox", &spufs_mbox_fops, 0444, },
2055 { "ibox", &spufs_ibox_fops, 0444, },
2056 { "wbox", &spufs_wbox_fops, 0222, },
2057 { "mbox_stat", &spufs_mbox_stat_fops, 0444, },
2058 { "ibox_stat", &spufs_ibox_stat_fops, 0444, },
2059 { "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2060 { "signal1", &spufs_signal1_fops, 0666, },
2061 { "signal2", &spufs_signal2_fops, 0666, },
2062 { "signal1_type", &spufs_signal1_type, 0666, },
2063 { "signal2_type", &spufs_signal2_type, 0666, },
2064 { "mss", &spufs_mss_fops, 0666, },
2065 { "mfc", &spufs_mfc_fops, 0666, },
2066 { "cntl", &spufs_cntl_fops, 0666, },
2067 { "npc", &spufs_npc_ops, 0666, },
2068 { "psmap", &spufs_psmap_fops, 0666, },
2069 { "phys-id", &spufs_id_ops, 0666, },
2070 { "object-id", &spufs_object_id_ops, 0666, },
2071 {},
2072};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002073
2074struct spufs_coredump_reader spufs_coredump_read[] = {
2075 { "regs", __spufs_regs_read, NULL, 128 * 16 },
2076 { "fpcr", __spufs_fpcr_read, NULL, 16 },
2077 { "lslr", NULL, __spufs_lslr_get, 11 },
2078 { "decr", NULL, __spufs_decr_get, 11 },
2079 { "decr_status", NULL, __spufs_decr_status_get, 11 },
2080 { "mem", __spufs_mem_read, NULL, 256 * 1024, },
2081 { "signal1", __spufs_signal1_read, NULL, 4 },
2082 { "signal1_type", NULL, __spufs_signal1_type_get, 2 },
2083 { "signal2", __spufs_signal2_read, NULL, 4 },
2084 { "signal2_type", NULL, __spufs_signal2_type_get, 2 },
2085 { "event_mask", NULL, __spufs_event_mask_get, 8 },
2086 { "event_status", NULL, __spufs_event_status_get, 8 },
2087 { "mbox_info", __spufs_mbox_info_read, NULL, 4 },
2088 { "ibox_info", __spufs_ibox_info_read, NULL, 4 },
2089 { "wbox_info", __spufs_wbox_info_read, NULL, 16 },
2090 { "dma_info", __spufs_dma_info_read, NULL, 69 * 8 },
2091 { "proxydma_info", __spufs_proxydma_info_read, NULL, 35 * 8 },
2092 { "object-id", NULL, __spufs_object_id_get, 19 },
2093 { },
2094};
2095int spufs_coredump_num_notes = ARRAY_SIZE(spufs_coredump_read) - 1;
2096