167207b96SArnd Bergmann /*
267207b96SArnd Bergmann  * SPU file system -- file contents
367207b96SArnd Bergmann  *
467207b96SArnd Bergmann  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
567207b96SArnd Bergmann  *
667207b96SArnd Bergmann  * Author: Arnd Bergmann <arndb@de.ibm.com>
767207b96SArnd Bergmann  *
867207b96SArnd Bergmann  * This program is free software; you can redistribute it and/or modify
967207b96SArnd Bergmann  * it under the terms of the GNU General Public License as published by
1067207b96SArnd Bergmann  * the Free Software Foundation; either version 2, or (at your option)
1167207b96SArnd Bergmann  * any later version.
1267207b96SArnd Bergmann  *
1367207b96SArnd Bergmann  * This program is distributed in the hope that it will be useful,
1467207b96SArnd Bergmann  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1567207b96SArnd Bergmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1667207b96SArnd Bergmann  * GNU General Public License for more details.
1767207b96SArnd Bergmann  *
1867207b96SArnd Bergmann  * You should have received a copy of the GNU General Public License
1967207b96SArnd Bergmann  * along with this program; if not, write to the Free Software
2067207b96SArnd Bergmann  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2167207b96SArnd Bergmann  */
2267207b96SArnd Bergmann 
23a33a7d73SArnd Bergmann #undef DEBUG
24a33a7d73SArnd Bergmann 
2567207b96SArnd Bergmann #include <linux/fs.h>
2667207b96SArnd Bergmann #include <linux/ioctl.h>
2767207b96SArnd Bergmann #include <linux/module.h>
28d88cfffaSArnd Bergmann #include <linux/pagemap.h>
2967207b96SArnd Bergmann #include <linux/poll.h>
305110459fSArnd Bergmann #include <linux/ptrace.h>
31cbe709c1SBenjamin Herrenschmidt #include <linux/seq_file.h>
3267207b96SArnd Bergmann 
3367207b96SArnd Bergmann #include <asm/io.h>
3467207b96SArnd Bergmann #include <asm/semaphore.h>
3567207b96SArnd Bergmann #include <asm/spu.h>
36b9e3bd77SDwayne Grant McConnell #include <asm/spu_info.h>
3767207b96SArnd Bergmann #include <asm/uaccess.h>
3867207b96SArnd Bergmann 
3967207b96SArnd Bergmann #include "spufs.h"
4067207b96SArnd Bergmann 
4127d5bf2aSBenjamin Herrenschmidt #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
4227d5bf2aSBenjamin Herrenschmidt 
43cbe709c1SBenjamin Herrenschmidt 
4467207b96SArnd Bergmann static int
4567207b96SArnd Bergmann spufs_mem_open(struct inode *inode, struct file *file)
4667207b96SArnd Bergmann {
4767207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
486df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
4943c2bbd9SChristoph Hellwig 
5047d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
516df10a82SMark Nutter 	file->private_data = ctx;
5243c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
536df10a82SMark Nutter 		ctx->local_store = inode->i_mapping;
5447d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
5543c2bbd9SChristoph Hellwig 	return 0;
5643c2bbd9SChristoph Hellwig }
5743c2bbd9SChristoph Hellwig 
5843c2bbd9SChristoph Hellwig static int
5943c2bbd9SChristoph Hellwig spufs_mem_release(struct inode *inode, struct file *file)
6043c2bbd9SChristoph Hellwig {
6143c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
6243c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
6343c2bbd9SChristoph Hellwig 
6447d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
6543c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
6643c2bbd9SChristoph Hellwig 		ctx->local_store = NULL;
6747d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
6867207b96SArnd Bergmann 	return 0;
6967207b96SArnd Bergmann }
7067207b96SArnd Bergmann 
7167207b96SArnd Bergmann static ssize_t
72bf1ab978SDwayne Grant McConnell __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
73bf1ab978SDwayne Grant McConnell 			size_t size, loff_t *pos)
74bf1ab978SDwayne Grant McConnell {
75bf1ab978SDwayne Grant McConnell 	char *local_store = ctx->ops->get_ls(ctx);
76bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buffer, size, pos, local_store,
77bf1ab978SDwayne Grant McConnell 					LS_SIZE);
78bf1ab978SDwayne Grant McConnell }
79bf1ab978SDwayne Grant McConnell 
80bf1ab978SDwayne Grant McConnell static ssize_t
8167207b96SArnd Bergmann spufs_mem_read(struct file *file, char __user *buffer,
8267207b96SArnd Bergmann 				size_t size, loff_t *pos)
8367207b96SArnd Bergmann {
84bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
85aa0ed2bdSArnd Bergmann 	ssize_t ret;
8667207b96SArnd Bergmann 
878b3d6663SArnd Bergmann 	spu_acquire(ctx);
88bf1ab978SDwayne Grant McConnell 	ret = __spufs_mem_read(ctx, buffer, size, pos);
898b3d6663SArnd Bergmann 	spu_release(ctx);
9067207b96SArnd Bergmann 	return ret;
9167207b96SArnd Bergmann }
9267207b96SArnd Bergmann 
9367207b96SArnd Bergmann static ssize_t
9467207b96SArnd Bergmann spufs_mem_write(struct file *file, const char __user *buffer,
95aa0ed2bdSArnd Bergmann 					size_t size, loff_t *ppos)
9667207b96SArnd Bergmann {
9767207b96SArnd Bergmann 	struct spu_context *ctx = file->private_data;
988b3d6663SArnd Bergmann 	char *local_store;
99aa0ed2bdSArnd Bergmann 	loff_t pos = *ppos;
1008b3d6663SArnd Bergmann 	int ret;
10167207b96SArnd Bergmann 
102aa0ed2bdSArnd Bergmann 	if (pos < 0)
103aa0ed2bdSArnd Bergmann 		return -EINVAL;
104aa0ed2bdSArnd Bergmann 	if (pos > LS_SIZE)
10567207b96SArnd Bergmann 		return -EFBIG;
106aa0ed2bdSArnd Bergmann 	if (size > LS_SIZE - pos)
107aa0ed2bdSArnd Bergmann 		size = LS_SIZE - pos;
1088b3d6663SArnd Bergmann 
1098b3d6663SArnd Bergmann 	spu_acquire(ctx);
1108b3d6663SArnd Bergmann 	local_store = ctx->ops->get_ls(ctx);
111aa0ed2bdSArnd Bergmann 	ret = copy_from_user(local_store + pos, buffer, size);
1128b3d6663SArnd Bergmann 	spu_release(ctx);
113aa0ed2bdSArnd Bergmann 
114aa0ed2bdSArnd Bergmann 	if (ret)
115aa0ed2bdSArnd Bergmann 		return -EFAULT;
116aa0ed2bdSArnd Bergmann 	*ppos = pos + size;
117aa0ed2bdSArnd Bergmann 	return size;
11867207b96SArnd Bergmann }
11967207b96SArnd Bergmann 
12078bde53eSBenjamin Herrenschmidt static unsigned long spufs_mem_mmap_nopfn(struct vm_area_struct *vma,
12178bde53eSBenjamin Herrenschmidt 					  unsigned long address)
1228b3d6663SArnd Bergmann {
1238b3d6663SArnd Bergmann 	struct spu_context *ctx	= vma->vm_file->private_data;
124f1fa74f4SBenjamin Herrenschmidt 	unsigned long pfn, offset, addr0 = address;
125f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS
126f1fa74f4SBenjamin Herrenschmidt 	struct spu_state *csa = &ctx->csa;
127f1fa74f4SBenjamin Herrenschmidt 	int psize;
12878bde53eSBenjamin Herrenschmidt 
129f1fa74f4SBenjamin Herrenschmidt 	/* Check what page size we are using */
130f1fa74f4SBenjamin Herrenschmidt 	psize = get_slice_psize(vma->vm_mm, address);
1318b3d6663SArnd Bergmann 
132f1fa74f4SBenjamin Herrenschmidt 	/* Some sanity checking */
133f1fa74f4SBenjamin Herrenschmidt 	BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
134f1fa74f4SBenjamin Herrenschmidt 
135f1fa74f4SBenjamin Herrenschmidt 	/* Wow, 64K, cool, we need to align the address though */
136f1fa74f4SBenjamin Herrenschmidt 	if (csa->use_big_pages) {
137f1fa74f4SBenjamin Herrenschmidt 		BUG_ON(vma->vm_start & 0xffff);
138f1fa74f4SBenjamin Herrenschmidt 		address &= ~0xfffful;
139f1fa74f4SBenjamin Herrenschmidt 	}
140f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */
141f1fa74f4SBenjamin Herrenschmidt 
142f1fa74f4SBenjamin Herrenschmidt 	offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
143128b8546SMasato Noguchi 	if (offset >= LS_SIZE)
144128b8546SMasato Noguchi 		return NOPFN_SIGBUS;
145128b8546SMasato Noguchi 
146f1fa74f4SBenjamin Herrenschmidt 	pr_debug("spufs_mem_mmap_nopfn address=0x%lx -> 0x%lx, offset=0x%lx\n",
147f1fa74f4SBenjamin Herrenschmidt 		 addr0, address, offset);
148f1fa74f4SBenjamin Herrenschmidt 
1498b3d6663SArnd Bergmann 	spu_acquire(ctx);
1508b3d6663SArnd Bergmann 
151ac91cb8dSArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED) {
152ac91cb8dSArnd Bergmann 		vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
153932f535dSArnd Bergmann 							& ~_PAGE_NO_CACHE);
15478bde53eSBenjamin Herrenschmidt 		pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
155ac91cb8dSArnd Bergmann 	} else {
156ac91cb8dSArnd Bergmann 		vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
157932f535dSArnd Bergmann 					     | _PAGE_NO_CACHE);
15878bde53eSBenjamin Herrenschmidt 		pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
159ac91cb8dSArnd Bergmann 	}
16078bde53eSBenjamin Herrenschmidt 	vm_insert_pfn(vma, address, pfn);
16178bde53eSBenjamin Herrenschmidt 
1628b3d6663SArnd Bergmann 	spu_release(ctx);
1638b3d6663SArnd Bergmann 
16478bde53eSBenjamin Herrenschmidt 	return NOPFN_REFAULT;
1658b3d6663SArnd Bergmann }
1668b3d6663SArnd Bergmann 
16778bde53eSBenjamin Herrenschmidt 
1688b3d6663SArnd Bergmann static struct vm_operations_struct spufs_mem_mmap_vmops = {
16978bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_mem_mmap_nopfn,
1708b3d6663SArnd Bergmann };
1718b3d6663SArnd Bergmann 
172f1fa74f4SBenjamin Herrenschmidt static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
17367207b96SArnd Bergmann {
174f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS
175f1fa74f4SBenjamin Herrenschmidt 	struct spu_context	*ctx = file->private_data;
176f1fa74f4SBenjamin Herrenschmidt 	struct spu_state	*csa = &ctx->csa;
177f1fa74f4SBenjamin Herrenschmidt 
178f1fa74f4SBenjamin Herrenschmidt 	/* Sanity check VMA alignment */
179f1fa74f4SBenjamin Herrenschmidt 	if (csa->use_big_pages) {
180f1fa74f4SBenjamin Herrenschmidt 		pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
181f1fa74f4SBenjamin Herrenschmidt 			 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
182f1fa74f4SBenjamin Herrenschmidt 			 vma->vm_pgoff);
183f1fa74f4SBenjamin Herrenschmidt 		if (vma->vm_start & 0xffff)
184f1fa74f4SBenjamin Herrenschmidt 			return -EINVAL;
185f1fa74f4SBenjamin Herrenschmidt 		if (vma->vm_pgoff & 0xf)
186f1fa74f4SBenjamin Herrenschmidt 			return -EINVAL;
187f1fa74f4SBenjamin Herrenschmidt 	}
188f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */
189f1fa74f4SBenjamin Herrenschmidt 
1908b3d6663SArnd Bergmann 	if (!(vma->vm_flags & VM_SHARED))
1918b3d6663SArnd Bergmann 		return -EINVAL;
19267207b96SArnd Bergmann 
19378bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
19467207b96SArnd Bergmann 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
19567207b96SArnd Bergmann 				     | _PAGE_NO_CACHE);
1968b3d6663SArnd Bergmann 
1978b3d6663SArnd Bergmann 	vma->vm_ops = &spufs_mem_mmap_vmops;
19867207b96SArnd Bergmann 	return 0;
19967207b96SArnd Bergmann }
20067207b96SArnd Bergmann 
201f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS
202f1fa74f4SBenjamin Herrenschmidt unsigned long spufs_get_unmapped_area(struct file *file, unsigned long addr,
203f1fa74f4SBenjamin Herrenschmidt 				      unsigned long len, unsigned long pgoff,
204f1fa74f4SBenjamin Herrenschmidt 				      unsigned long flags)
205f1fa74f4SBenjamin Herrenschmidt {
206f1fa74f4SBenjamin Herrenschmidt 	struct spu_context	*ctx = file->private_data;
207f1fa74f4SBenjamin Herrenschmidt 	struct spu_state	*csa = &ctx->csa;
208f1fa74f4SBenjamin Herrenschmidt 
209f1fa74f4SBenjamin Herrenschmidt 	/* If not using big pages, fallback to normal MM g_u_a */
210f1fa74f4SBenjamin Herrenschmidt 	if (!csa->use_big_pages)
211f1fa74f4SBenjamin Herrenschmidt 		return current->mm->get_unmapped_area(file, addr, len,
212f1fa74f4SBenjamin Herrenschmidt 						      pgoff, flags);
213f1fa74f4SBenjamin Herrenschmidt 
214f1fa74f4SBenjamin Herrenschmidt 	/* Else, try to obtain a 64K pages slice */
215f1fa74f4SBenjamin Herrenschmidt 	return slice_get_unmapped_area(addr, len, flags,
216f1fa74f4SBenjamin Herrenschmidt 				       MMU_PAGE_64K, 1, 0);
217f1fa74f4SBenjamin Herrenschmidt }
218f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */
219f1fa74f4SBenjamin Herrenschmidt 
2205dfe4c96SArjan van de Ven static const struct file_operations spufs_mem_fops = {
22167207b96SArnd Bergmann 	.open			= spufs_mem_open,
222ce92987bSChristoph Hellwig 	.release		= spufs_mem_release,
22367207b96SArnd Bergmann 	.read			= spufs_mem_read,
22467207b96SArnd Bergmann 	.write			= spufs_mem_write,
2258b3d6663SArnd Bergmann 	.llseek			= generic_file_llseek,
22667207b96SArnd Bergmann 	.mmap			= spufs_mem_mmap,
227f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS
228f1fa74f4SBenjamin Herrenschmidt 	.get_unmapped_area	= spufs_get_unmapped_area,
229f1fa74f4SBenjamin Herrenschmidt #endif
2308b3d6663SArnd Bergmann };
2318b3d6663SArnd Bergmann 
23278bde53eSBenjamin Herrenschmidt static unsigned long spufs_ps_nopfn(struct vm_area_struct *vma,
2336df10a82SMark Nutter 				    unsigned long address,
23478bde53eSBenjamin Herrenschmidt 				    unsigned long ps_offs,
23527d5bf2aSBenjamin Herrenschmidt 				    unsigned long ps_size)
2366df10a82SMark Nutter {
2376df10a82SMark Nutter 	struct spu_context *ctx = vma->vm_file->private_data;
23878bde53eSBenjamin Herrenschmidt 	unsigned long area, offset = address - vma->vm_start;
2396df10a82SMark Nutter 	int ret;
2406df10a82SMark Nutter 
2416df10a82SMark Nutter 	offset += vma->vm_pgoff << PAGE_SHIFT;
24227d5bf2aSBenjamin Herrenschmidt 	if (offset >= ps_size)
24378bde53eSBenjamin Herrenschmidt 		return NOPFN_SIGBUS;
2446df10a82SMark Nutter 
24578bde53eSBenjamin Herrenschmidt 	/* error here usually means a signal.. we might want to test
24678bde53eSBenjamin Herrenschmidt 	 * the error code more precisely though
24778bde53eSBenjamin Herrenschmidt 	 */
24826bec673SChristoph Hellwig 	ret = spu_acquire_runnable(ctx, 0);
2496df10a82SMark Nutter 	if (ret)
25078bde53eSBenjamin Herrenschmidt 		return NOPFN_REFAULT;
2516df10a82SMark Nutter 
2526df10a82SMark Nutter 	area = ctx->spu->problem_phys + ps_offs;
25378bde53eSBenjamin Herrenschmidt 	vm_insert_pfn(vma, address, (area + offset) >> PAGE_SHIFT);
2546df10a82SMark Nutter 	spu_release(ctx);
2556df10a82SMark Nutter 
25678bde53eSBenjamin Herrenschmidt 	return NOPFN_REFAULT;
2576df10a82SMark Nutter }
2586df10a82SMark Nutter 
25927d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
26078bde53eSBenjamin Herrenschmidt static unsigned long spufs_cntl_mmap_nopfn(struct vm_area_struct *vma,
26178bde53eSBenjamin Herrenschmidt 					   unsigned long address)
2626df10a82SMark Nutter {
26378bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x4000, 0x1000);
2646df10a82SMark Nutter }
2656df10a82SMark Nutter 
2666df10a82SMark Nutter static struct vm_operations_struct spufs_cntl_mmap_vmops = {
26778bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_cntl_mmap_nopfn,
2686df10a82SMark Nutter };
2696df10a82SMark Nutter 
2706df10a82SMark Nutter /*
2716df10a82SMark Nutter  * mmap support for problem state control area [0x4000 - 0x4fff].
2726df10a82SMark Nutter  */
2736df10a82SMark Nutter static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
2746df10a82SMark Nutter {
2756df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
2766df10a82SMark Nutter 		return -EINVAL;
2776df10a82SMark Nutter 
27878bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
2796df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
28023cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
2816df10a82SMark Nutter 
2826df10a82SMark Nutter 	vma->vm_ops = &spufs_cntl_mmap_vmops;
2836df10a82SMark Nutter 	return 0;
2846df10a82SMark Nutter }
28527d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
28627d5bf2aSBenjamin Herrenschmidt #define spufs_cntl_mmap NULL
28727d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
2886df10a82SMark Nutter 
289e1dbff2bSArnd Bergmann static u64 spufs_cntl_get(void *data)
290e1dbff2bSArnd Bergmann {
291e1dbff2bSArnd Bergmann 	struct spu_context *ctx = data;
292e1dbff2bSArnd Bergmann 	u64 val;
293e1dbff2bSArnd Bergmann 
294e1dbff2bSArnd Bergmann 	spu_acquire(ctx);
295e1dbff2bSArnd Bergmann 	val = ctx->ops->status_read(ctx);
296e1dbff2bSArnd Bergmann 	spu_release(ctx);
297e1dbff2bSArnd Bergmann 
298e1dbff2bSArnd Bergmann 	return val;
299e1dbff2bSArnd Bergmann }
300e1dbff2bSArnd Bergmann 
301e1dbff2bSArnd Bergmann static void spufs_cntl_set(void *data, u64 val)
302e1dbff2bSArnd Bergmann {
303e1dbff2bSArnd Bergmann 	struct spu_context *ctx = data;
304e1dbff2bSArnd Bergmann 
305e1dbff2bSArnd Bergmann 	spu_acquire(ctx);
306e1dbff2bSArnd Bergmann 	ctx->ops->runcntl_write(ctx, val);
307e1dbff2bSArnd Bergmann 	spu_release(ctx);
308e1dbff2bSArnd Bergmann }
309e1dbff2bSArnd Bergmann 
3106df10a82SMark Nutter static int spufs_cntl_open(struct inode *inode, struct file *file)
3116df10a82SMark Nutter {
3126df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
3136df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
3146df10a82SMark Nutter 
31547d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
3166df10a82SMark Nutter 	file->private_data = ctx;
31743c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
3186df10a82SMark Nutter 		ctx->cntl = inode->i_mapping;
31947d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
320e1dbff2bSArnd Bergmann 	return simple_attr_open(inode, file, spufs_cntl_get,
321e1dbff2bSArnd Bergmann 					spufs_cntl_set, "0x%08lx");
3226df10a82SMark Nutter }
3236df10a82SMark Nutter 
32443c2bbd9SChristoph Hellwig static int
32543c2bbd9SChristoph Hellwig spufs_cntl_release(struct inode *inode, struct file *file)
32643c2bbd9SChristoph Hellwig {
32743c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
32843c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
32943c2bbd9SChristoph Hellwig 
33043c2bbd9SChristoph Hellwig 	simple_attr_close(inode, file);
33143c2bbd9SChristoph Hellwig 
33247d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
33343c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
33443c2bbd9SChristoph Hellwig 		ctx->cntl = NULL;
33547d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
33643c2bbd9SChristoph Hellwig 	return 0;
33743c2bbd9SChristoph Hellwig }
33843c2bbd9SChristoph Hellwig 
3395dfe4c96SArjan van de Ven static const struct file_operations spufs_cntl_fops = {
3406df10a82SMark Nutter 	.open = spufs_cntl_open,
34143c2bbd9SChristoph Hellwig 	.release = spufs_cntl_release,
342e1dbff2bSArnd Bergmann 	.read = simple_attr_read,
343e1dbff2bSArnd Bergmann 	.write = simple_attr_write,
3446df10a82SMark Nutter 	.mmap = spufs_cntl_mmap,
3456df10a82SMark Nutter };
3466df10a82SMark Nutter 
3478b3d6663SArnd Bergmann static int
3488b3d6663SArnd Bergmann spufs_regs_open(struct inode *inode, struct file *file)
3498b3d6663SArnd Bergmann {
3508b3d6663SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
3518b3d6663SArnd Bergmann 	file->private_data = i->i_ctx;
3528b3d6663SArnd Bergmann 	return 0;
3538b3d6663SArnd Bergmann }
3548b3d6663SArnd Bergmann 
3558b3d6663SArnd Bergmann static ssize_t
356bf1ab978SDwayne Grant McConnell __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
357bf1ab978SDwayne Grant McConnell 			size_t size, loff_t *pos)
358bf1ab978SDwayne Grant McConnell {
359bf1ab978SDwayne Grant McConnell 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
360bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buffer, size, pos,
361bf1ab978SDwayne Grant McConnell 				      lscsa->gprs, sizeof lscsa->gprs);
362bf1ab978SDwayne Grant McConnell }
363bf1ab978SDwayne Grant McConnell 
364bf1ab978SDwayne Grant McConnell static ssize_t
3658b3d6663SArnd Bergmann spufs_regs_read(struct file *file, char __user *buffer,
3668b3d6663SArnd Bergmann 		size_t size, loff_t *pos)
3678b3d6663SArnd Bergmann {
3688b3d6663SArnd Bergmann 	int ret;
369bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
3708b3d6663SArnd Bergmann 
3718b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
372bf1ab978SDwayne Grant McConnell 	ret = __spufs_regs_read(ctx, buffer, size, pos);
3738b3d6663SArnd Bergmann 	spu_release(ctx);
3748b3d6663SArnd Bergmann 	return ret;
3758b3d6663SArnd Bergmann }
3768b3d6663SArnd Bergmann 
3778b3d6663SArnd Bergmann static ssize_t
3788b3d6663SArnd Bergmann spufs_regs_write(struct file *file, const char __user *buffer,
3798b3d6663SArnd Bergmann 		 size_t size, loff_t *pos)
3808b3d6663SArnd Bergmann {
3818b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
3828b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
3838b3d6663SArnd Bergmann 	int ret;
3848b3d6663SArnd Bergmann 
3858b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
3868b3d6663SArnd Bergmann 	if (size <= 0)
3878b3d6663SArnd Bergmann 		return -EFBIG;
3888b3d6663SArnd Bergmann 	*pos += size;
3898b3d6663SArnd Bergmann 
3908b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
3918b3d6663SArnd Bergmann 
3928b3d6663SArnd Bergmann 	ret = copy_from_user(lscsa->gprs + *pos - size,
3938b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
3948b3d6663SArnd Bergmann 
3958b3d6663SArnd Bergmann 	spu_release(ctx);
3968b3d6663SArnd Bergmann 	return ret;
3978b3d6663SArnd Bergmann }
3988b3d6663SArnd Bergmann 
3995dfe4c96SArjan van de Ven static const struct file_operations spufs_regs_fops = {
4008b3d6663SArnd Bergmann 	.open	 = spufs_regs_open,
4018b3d6663SArnd Bergmann 	.read    = spufs_regs_read,
4028b3d6663SArnd Bergmann 	.write   = spufs_regs_write,
4038b3d6663SArnd Bergmann 	.llseek  = generic_file_llseek,
4048b3d6663SArnd Bergmann };
4058b3d6663SArnd Bergmann 
4068b3d6663SArnd Bergmann static ssize_t
407bf1ab978SDwayne Grant McConnell __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
408bf1ab978SDwayne Grant McConnell 			size_t size, loff_t * pos)
409bf1ab978SDwayne Grant McConnell {
410bf1ab978SDwayne Grant McConnell 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
411bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buffer, size, pos,
412bf1ab978SDwayne Grant McConnell 				      &lscsa->fpcr, sizeof(lscsa->fpcr));
413bf1ab978SDwayne Grant McConnell }
414bf1ab978SDwayne Grant McConnell 
415bf1ab978SDwayne Grant McConnell static ssize_t
4168b3d6663SArnd Bergmann spufs_fpcr_read(struct file *file, char __user * buffer,
4178b3d6663SArnd Bergmann 		size_t size, loff_t * pos)
4188b3d6663SArnd Bergmann {
4198b3d6663SArnd Bergmann 	int ret;
420bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
4218b3d6663SArnd Bergmann 
4228b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
423bf1ab978SDwayne Grant McConnell 	ret = __spufs_fpcr_read(ctx, buffer, size, pos);
4248b3d6663SArnd Bergmann 	spu_release(ctx);
4258b3d6663SArnd Bergmann 	return ret;
4268b3d6663SArnd Bergmann }
4278b3d6663SArnd Bergmann 
4288b3d6663SArnd Bergmann static ssize_t
4298b3d6663SArnd Bergmann spufs_fpcr_write(struct file *file, const char __user * buffer,
4308b3d6663SArnd Bergmann 		 size_t size, loff_t * pos)
4318b3d6663SArnd Bergmann {
4328b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
4338b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
4348b3d6663SArnd Bergmann 	int ret;
4358b3d6663SArnd Bergmann 
4368b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
4378b3d6663SArnd Bergmann 	if (size <= 0)
4388b3d6663SArnd Bergmann 		return -EFBIG;
4398b3d6663SArnd Bergmann 	*pos += size;
4408b3d6663SArnd Bergmann 
4418b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
4428b3d6663SArnd Bergmann 
4438b3d6663SArnd Bergmann 	ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
4448b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
4458b3d6663SArnd Bergmann 
4468b3d6663SArnd Bergmann 	spu_release(ctx);
4478b3d6663SArnd Bergmann 	return ret;
4488b3d6663SArnd Bergmann }
4498b3d6663SArnd Bergmann 
4505dfe4c96SArjan van de Ven static const struct file_operations spufs_fpcr_fops = {
4518b3d6663SArnd Bergmann 	.open = spufs_regs_open,
4528b3d6663SArnd Bergmann 	.read = spufs_fpcr_read,
4538b3d6663SArnd Bergmann 	.write = spufs_fpcr_write,
45467207b96SArnd Bergmann 	.llseek = generic_file_llseek,
45567207b96SArnd Bergmann };
45667207b96SArnd Bergmann 
45767207b96SArnd Bergmann /* generic open function for all pipe-like files */
45867207b96SArnd Bergmann static int spufs_pipe_open(struct inode *inode, struct file *file)
45967207b96SArnd Bergmann {
46067207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
46167207b96SArnd Bergmann 	file->private_data = i->i_ctx;
46267207b96SArnd Bergmann 
46367207b96SArnd Bergmann 	return nonseekable_open(inode, file);
46467207b96SArnd Bergmann }
46567207b96SArnd Bergmann 
466cdcc89bbSArnd Bergmann /*
467cdcc89bbSArnd Bergmann  * Read as many bytes from the mailbox as possible, until
468cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
469cdcc89bbSArnd Bergmann  *
470cdcc89bbSArnd Bergmann  * - no more data available in the mailbox
471cdcc89bbSArnd Bergmann  * - end of the user provided buffer
472cdcc89bbSArnd Bergmann  * - end of the mapped area
473cdcc89bbSArnd Bergmann  */
47467207b96SArnd Bergmann static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
47567207b96SArnd Bergmann 			size_t len, loff_t *pos)
47667207b96SArnd Bergmann {
4778b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
478cdcc89bbSArnd Bergmann 	u32 mbox_data, __user *udata;
479cdcc89bbSArnd Bergmann 	ssize_t count;
48067207b96SArnd Bergmann 
48167207b96SArnd Bergmann 	if (len < 4)
48267207b96SArnd Bergmann 		return -EINVAL;
48367207b96SArnd Bergmann 
484cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_WRITE, buf, len))
48567207b96SArnd Bergmann 		return -EFAULT;
48667207b96SArnd Bergmann 
487cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
488cdcc89bbSArnd Bergmann 
489cdcc89bbSArnd Bergmann 	spu_acquire(ctx);
490274cef5eSArnd Bergmann 	for (count = 0; (count + 4) <= len; count += 4, udata++) {
491cdcc89bbSArnd Bergmann 		int ret;
492cdcc89bbSArnd Bergmann 		ret = ctx->ops->mbox_read(ctx, &mbox_data);
493cdcc89bbSArnd Bergmann 		if (ret == 0)
494cdcc89bbSArnd Bergmann 			break;
495cdcc89bbSArnd Bergmann 
496cdcc89bbSArnd Bergmann 		/*
497cdcc89bbSArnd Bergmann 		 * at the end of the mapped area, we can fault
498cdcc89bbSArnd Bergmann 		 * but still need to return the data we have
499cdcc89bbSArnd Bergmann 		 * read successfully so far.
500cdcc89bbSArnd Bergmann 		 */
501cdcc89bbSArnd Bergmann 		ret = __put_user(mbox_data, udata);
502cdcc89bbSArnd Bergmann 		if (ret) {
503cdcc89bbSArnd Bergmann 			if (!count)
504cdcc89bbSArnd Bergmann 				count = -EFAULT;
505cdcc89bbSArnd Bergmann 			break;
506cdcc89bbSArnd Bergmann 		}
507cdcc89bbSArnd Bergmann 	}
508cdcc89bbSArnd Bergmann 	spu_release(ctx);
509cdcc89bbSArnd Bergmann 
510cdcc89bbSArnd Bergmann 	if (!count)
511cdcc89bbSArnd Bergmann 		count = -EAGAIN;
512cdcc89bbSArnd Bergmann 
513cdcc89bbSArnd Bergmann 	return count;
51467207b96SArnd Bergmann }
51567207b96SArnd Bergmann 
5165dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_fops = {
51767207b96SArnd Bergmann 	.open	= spufs_pipe_open,
51867207b96SArnd Bergmann 	.read	= spufs_mbox_read,
51967207b96SArnd Bergmann };
52067207b96SArnd Bergmann 
52167207b96SArnd Bergmann static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
52267207b96SArnd Bergmann 			size_t len, loff_t *pos)
52367207b96SArnd Bergmann {
5248b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
52567207b96SArnd Bergmann 	u32 mbox_stat;
52667207b96SArnd Bergmann 
52767207b96SArnd Bergmann 	if (len < 4)
52867207b96SArnd Bergmann 		return -EINVAL;
52967207b96SArnd Bergmann 
5308b3d6663SArnd Bergmann 	spu_acquire(ctx);
5318b3d6663SArnd Bergmann 
5328b3d6663SArnd Bergmann 	mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
5338b3d6663SArnd Bergmann 
5348b3d6663SArnd Bergmann 	spu_release(ctx);
53567207b96SArnd Bergmann 
53667207b96SArnd Bergmann 	if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
53767207b96SArnd Bergmann 		return -EFAULT;
53867207b96SArnd Bergmann 
53967207b96SArnd Bergmann 	return 4;
54067207b96SArnd Bergmann }
54167207b96SArnd Bergmann 
5425dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_stat_fops = {
54367207b96SArnd Bergmann 	.open	= spufs_pipe_open,
54467207b96SArnd Bergmann 	.read	= spufs_mbox_stat_read,
54567207b96SArnd Bergmann };
54667207b96SArnd Bergmann 
54767207b96SArnd Bergmann /* low-level ibox access function */
5488b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
54967207b96SArnd Bergmann {
5508b3d6663SArnd Bergmann 	return ctx->ops->ibox_read(ctx, data);
55167207b96SArnd Bergmann }
55267207b96SArnd Bergmann 
55367207b96SArnd Bergmann static int spufs_ibox_fasync(int fd, struct file *file, int on)
55467207b96SArnd Bergmann {
5558b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
5568b3d6663SArnd Bergmann 
5578b3d6663SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->ibox_fasync);
5588b3d6663SArnd Bergmann }
5598b3d6663SArnd Bergmann 
5608b3d6663SArnd Bergmann /* interrupt-level ibox callback function. */
5618b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu)
5628b3d6663SArnd Bergmann {
5638b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
5648b3d6663SArnd Bergmann 
5658b3d6663SArnd Bergmann 	wake_up_all(&ctx->ibox_wq);
5668b3d6663SArnd Bergmann 	kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
56767207b96SArnd Bergmann }
56867207b96SArnd Bergmann 
569cdcc89bbSArnd Bergmann /*
570cdcc89bbSArnd Bergmann  * Read as many bytes from the interrupt mailbox as possible, until
571cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
572cdcc89bbSArnd Bergmann  *
573cdcc89bbSArnd Bergmann  * - no more data available in the mailbox
574cdcc89bbSArnd Bergmann  * - end of the user provided buffer
575cdcc89bbSArnd Bergmann  * - end of the mapped area
576cdcc89bbSArnd Bergmann  *
577cdcc89bbSArnd Bergmann  * If the file is opened without O_NONBLOCK, we wait here until
578cdcc89bbSArnd Bergmann  * any data is available, but return when we have been able to
579cdcc89bbSArnd Bergmann  * read something.
580cdcc89bbSArnd Bergmann  */
58167207b96SArnd Bergmann static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
58267207b96SArnd Bergmann 			size_t len, loff_t *pos)
58367207b96SArnd Bergmann {
5848b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
585cdcc89bbSArnd Bergmann 	u32 ibox_data, __user *udata;
586cdcc89bbSArnd Bergmann 	ssize_t count;
58767207b96SArnd Bergmann 
58867207b96SArnd Bergmann 	if (len < 4)
58967207b96SArnd Bergmann 		return -EINVAL;
59067207b96SArnd Bergmann 
591cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_WRITE, buf, len))
592cdcc89bbSArnd Bergmann 		return -EFAULT;
593cdcc89bbSArnd Bergmann 
594cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
595cdcc89bbSArnd Bergmann 
5968b3d6663SArnd Bergmann 	spu_acquire(ctx);
59767207b96SArnd Bergmann 
598cdcc89bbSArnd Bergmann 	/* wait only for the first element */
599cdcc89bbSArnd Bergmann 	count = 0;
60067207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
6018b3d6663SArnd Bergmann 		if (!spu_ibox_read(ctx, &ibox_data))
602cdcc89bbSArnd Bergmann 			count = -EAGAIN;
60367207b96SArnd Bergmann 	} else {
604cdcc89bbSArnd Bergmann 		count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
605cdcc89bbSArnd Bergmann 	}
606cdcc89bbSArnd Bergmann 	if (count)
607cdcc89bbSArnd Bergmann 		goto out;
608cdcc89bbSArnd Bergmann 
609cdcc89bbSArnd Bergmann 	/* if we can't write at all, return -EFAULT */
610cdcc89bbSArnd Bergmann 	count = __put_user(ibox_data, udata);
611cdcc89bbSArnd Bergmann 	if (count)
612cdcc89bbSArnd Bergmann 		goto out;
613cdcc89bbSArnd Bergmann 
614cdcc89bbSArnd Bergmann 	for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
615cdcc89bbSArnd Bergmann 		int ret;
616cdcc89bbSArnd Bergmann 		ret = ctx->ops->ibox_read(ctx, &ibox_data);
617cdcc89bbSArnd Bergmann 		if (ret == 0)
618cdcc89bbSArnd Bergmann 			break;
619cdcc89bbSArnd Bergmann 		/*
620cdcc89bbSArnd Bergmann 		 * at the end of the mapped area, we can fault
621cdcc89bbSArnd Bergmann 		 * but still need to return the data we have
622cdcc89bbSArnd Bergmann 		 * read successfully so far.
623cdcc89bbSArnd Bergmann 		 */
624cdcc89bbSArnd Bergmann 		ret = __put_user(ibox_data, udata);
625cdcc89bbSArnd Bergmann 		if (ret)
626cdcc89bbSArnd Bergmann 			break;
62767207b96SArnd Bergmann 	}
62867207b96SArnd Bergmann 
629cdcc89bbSArnd Bergmann out:
6308b3d6663SArnd Bergmann 	spu_release(ctx);
6318b3d6663SArnd Bergmann 
632cdcc89bbSArnd Bergmann 	return count;
63367207b96SArnd Bergmann }
63467207b96SArnd Bergmann 
63567207b96SArnd Bergmann static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
63667207b96SArnd Bergmann {
6378b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
63867207b96SArnd Bergmann 	unsigned int mask;
63967207b96SArnd Bergmann 
6408b3d6663SArnd Bergmann 	poll_wait(file, &ctx->ibox_wq, wait);
64167207b96SArnd Bergmann 
6423a843d7cSArnd Bergmann 	spu_acquire(ctx);
6433a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
6443a843d7cSArnd Bergmann 	spu_release(ctx);
64567207b96SArnd Bergmann 
64667207b96SArnd Bergmann 	return mask;
64767207b96SArnd Bergmann }
64867207b96SArnd Bergmann 
6495dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_fops = {
65067207b96SArnd Bergmann 	.open	= spufs_pipe_open,
65167207b96SArnd Bergmann 	.read	= spufs_ibox_read,
65267207b96SArnd Bergmann 	.poll	= spufs_ibox_poll,
65367207b96SArnd Bergmann 	.fasync	= spufs_ibox_fasync,
65467207b96SArnd Bergmann };
65567207b96SArnd Bergmann 
65667207b96SArnd Bergmann static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
65767207b96SArnd Bergmann 			size_t len, loff_t *pos)
65867207b96SArnd Bergmann {
6598b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
66067207b96SArnd Bergmann 	u32 ibox_stat;
66167207b96SArnd Bergmann 
66267207b96SArnd Bergmann 	if (len < 4)
66367207b96SArnd Bergmann 		return -EINVAL;
66467207b96SArnd Bergmann 
6658b3d6663SArnd Bergmann 	spu_acquire(ctx);
6668b3d6663SArnd Bergmann 	ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
6678b3d6663SArnd Bergmann 	spu_release(ctx);
66867207b96SArnd Bergmann 
66967207b96SArnd Bergmann 	if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
67067207b96SArnd Bergmann 		return -EFAULT;
67167207b96SArnd Bergmann 
67267207b96SArnd Bergmann 	return 4;
67367207b96SArnd Bergmann }
67467207b96SArnd Bergmann 
6755dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_stat_fops = {
67667207b96SArnd Bergmann 	.open	= spufs_pipe_open,
67767207b96SArnd Bergmann 	.read	= spufs_ibox_stat_read,
67867207b96SArnd Bergmann };
67967207b96SArnd Bergmann 
68067207b96SArnd Bergmann /* low-level mailbox write */
6818b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data)
68267207b96SArnd Bergmann {
6838b3d6663SArnd Bergmann 	return ctx->ops->wbox_write(ctx, data);
68467207b96SArnd Bergmann }
68567207b96SArnd Bergmann 
68667207b96SArnd Bergmann static int spufs_wbox_fasync(int fd, struct file *file, int on)
68767207b96SArnd Bergmann {
6888b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
6898b3d6663SArnd Bergmann 	int ret;
6908b3d6663SArnd Bergmann 
6918b3d6663SArnd Bergmann 	ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
6928b3d6663SArnd Bergmann 
6938b3d6663SArnd Bergmann 	return ret;
6948b3d6663SArnd Bergmann }
6958b3d6663SArnd Bergmann 
6968b3d6663SArnd Bergmann /* interrupt-level wbox callback function. */
6978b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu)
6988b3d6663SArnd Bergmann {
6998b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
7008b3d6663SArnd Bergmann 
7018b3d6663SArnd Bergmann 	wake_up_all(&ctx->wbox_wq);
7028b3d6663SArnd Bergmann 	kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
70367207b96SArnd Bergmann }
70467207b96SArnd Bergmann 
705cdcc89bbSArnd Bergmann /*
706cdcc89bbSArnd Bergmann  * Write as many bytes to the interrupt mailbox as possible, until
707cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
708cdcc89bbSArnd Bergmann  *
709cdcc89bbSArnd Bergmann  * - the mailbox is full
710cdcc89bbSArnd Bergmann  * - end of the user provided buffer
711cdcc89bbSArnd Bergmann  * - end of the mapped area
712cdcc89bbSArnd Bergmann  *
713cdcc89bbSArnd Bergmann  * If the file is opened without O_NONBLOCK, we wait here until
714cdcc89bbSArnd Bergmann  * space is availabyl, but return when we have been able to
715cdcc89bbSArnd Bergmann  * write something.
716cdcc89bbSArnd Bergmann  */
71767207b96SArnd Bergmann static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
71867207b96SArnd Bergmann 			size_t len, loff_t *pos)
71967207b96SArnd Bergmann {
7208b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
721cdcc89bbSArnd Bergmann 	u32 wbox_data, __user *udata;
722cdcc89bbSArnd Bergmann 	ssize_t count;
72367207b96SArnd Bergmann 
72467207b96SArnd Bergmann 	if (len < 4)
72567207b96SArnd Bergmann 		return -EINVAL;
72667207b96SArnd Bergmann 
727cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
728cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_READ, buf, len))
729cdcc89bbSArnd Bergmann 		return -EFAULT;
730cdcc89bbSArnd Bergmann 
731cdcc89bbSArnd Bergmann 	if (__get_user(wbox_data, udata))
73267207b96SArnd Bergmann 		return -EFAULT;
73367207b96SArnd Bergmann 
7348b3d6663SArnd Bergmann 	spu_acquire(ctx);
7358b3d6663SArnd Bergmann 
736cdcc89bbSArnd Bergmann 	/*
737cdcc89bbSArnd Bergmann 	 * make sure we can at least write one element, by waiting
738cdcc89bbSArnd Bergmann 	 * in case of !O_NONBLOCK
739cdcc89bbSArnd Bergmann 	 */
740cdcc89bbSArnd Bergmann 	count = 0;
74167207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
7428b3d6663SArnd Bergmann 		if (!spu_wbox_write(ctx, wbox_data))
743cdcc89bbSArnd Bergmann 			count = -EAGAIN;
74467207b96SArnd Bergmann 	} else {
745cdcc89bbSArnd Bergmann 		count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
74667207b96SArnd Bergmann 	}
74767207b96SArnd Bergmann 
748cdcc89bbSArnd Bergmann 	if (count)
749cdcc89bbSArnd Bergmann 		goto out;
7508b3d6663SArnd Bergmann 
751cdcc89bbSArnd Bergmann 	/* write aѕ much as possible */
752cdcc89bbSArnd Bergmann 	for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
753cdcc89bbSArnd Bergmann 		int ret;
754cdcc89bbSArnd Bergmann 		ret = __get_user(wbox_data, udata);
755cdcc89bbSArnd Bergmann 		if (ret)
756cdcc89bbSArnd Bergmann 			break;
757cdcc89bbSArnd Bergmann 
758cdcc89bbSArnd Bergmann 		ret = spu_wbox_write(ctx, wbox_data);
759cdcc89bbSArnd Bergmann 		if (ret == 0)
760cdcc89bbSArnd Bergmann 			break;
761cdcc89bbSArnd Bergmann 	}
762cdcc89bbSArnd Bergmann 
763cdcc89bbSArnd Bergmann out:
764cdcc89bbSArnd Bergmann 	spu_release(ctx);
765cdcc89bbSArnd Bergmann 	return count;
76667207b96SArnd Bergmann }
76767207b96SArnd Bergmann 
76867207b96SArnd Bergmann static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
76967207b96SArnd Bergmann {
7708b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
77167207b96SArnd Bergmann 	unsigned int mask;
77267207b96SArnd Bergmann 
7738b3d6663SArnd Bergmann 	poll_wait(file, &ctx->wbox_wq, wait);
77467207b96SArnd Bergmann 
7753a843d7cSArnd Bergmann 	spu_acquire(ctx);
7763a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
7773a843d7cSArnd Bergmann 	spu_release(ctx);
77867207b96SArnd Bergmann 
77967207b96SArnd Bergmann 	return mask;
78067207b96SArnd Bergmann }
78167207b96SArnd Bergmann 
7825dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_fops = {
78367207b96SArnd Bergmann 	.open	= spufs_pipe_open,
78467207b96SArnd Bergmann 	.write	= spufs_wbox_write,
78567207b96SArnd Bergmann 	.poll	= spufs_wbox_poll,
78667207b96SArnd Bergmann 	.fasync	= spufs_wbox_fasync,
78767207b96SArnd Bergmann };
78867207b96SArnd Bergmann 
78967207b96SArnd Bergmann static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
79067207b96SArnd Bergmann 			size_t len, loff_t *pos)
79167207b96SArnd Bergmann {
7928b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
79367207b96SArnd Bergmann 	u32 wbox_stat;
79467207b96SArnd Bergmann 
79567207b96SArnd Bergmann 	if (len < 4)
79667207b96SArnd Bergmann 		return -EINVAL;
79767207b96SArnd Bergmann 
7988b3d6663SArnd Bergmann 	spu_acquire(ctx);
7998b3d6663SArnd Bergmann 	wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
8008b3d6663SArnd Bergmann 	spu_release(ctx);
80167207b96SArnd Bergmann 
80267207b96SArnd Bergmann 	if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
80367207b96SArnd Bergmann 		return -EFAULT;
80467207b96SArnd Bergmann 
80567207b96SArnd Bergmann 	return 4;
80667207b96SArnd Bergmann }
80767207b96SArnd Bergmann 
8085dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_stat_fops = {
80967207b96SArnd Bergmann 	.open	= spufs_pipe_open,
81067207b96SArnd Bergmann 	.read	= spufs_wbox_stat_read,
81167207b96SArnd Bergmann };
81267207b96SArnd Bergmann 
8136df10a82SMark Nutter static int spufs_signal1_open(struct inode *inode, struct file *file)
8146df10a82SMark Nutter {
8156df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
8166df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
81743c2bbd9SChristoph Hellwig 
81847d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
8196df10a82SMark Nutter 	file->private_data = ctx;
82043c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
8216df10a82SMark Nutter 		ctx->signal1 = inode->i_mapping;
82247d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
8236df10a82SMark Nutter 	return nonseekable_open(inode, file);
8246df10a82SMark Nutter }
8256df10a82SMark Nutter 
82643c2bbd9SChristoph Hellwig static int
82743c2bbd9SChristoph Hellwig spufs_signal1_release(struct inode *inode, struct file *file)
82843c2bbd9SChristoph Hellwig {
82943c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
83043c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
83143c2bbd9SChristoph Hellwig 
83247d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
83343c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
83443c2bbd9SChristoph Hellwig 		ctx->signal1 = NULL;
83547d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
83643c2bbd9SChristoph Hellwig 	return 0;
83743c2bbd9SChristoph Hellwig }
83843c2bbd9SChristoph Hellwig 
839bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
84067207b96SArnd Bergmann 			size_t len, loff_t *pos)
84167207b96SArnd Bergmann {
84217f88cebSDwayne Grant McConnell 	int ret = 0;
84367207b96SArnd Bergmann 	u32 data;
84467207b96SArnd Bergmann 
84567207b96SArnd Bergmann 	if (len < 4)
84667207b96SArnd Bergmann 		return -EINVAL;
84767207b96SArnd Bergmann 
84817f88cebSDwayne Grant McConnell 	if (ctx->csa.spu_chnlcnt_RW[3]) {
84917f88cebSDwayne Grant McConnell 		data = ctx->csa.spu_chnldata_RW[3];
85017f88cebSDwayne Grant McConnell 		ret = 4;
85117f88cebSDwayne Grant McConnell 	}
8528b3d6663SArnd Bergmann 
85317f88cebSDwayne Grant McConnell 	if (!ret)
85417f88cebSDwayne Grant McConnell 		goto out;
85517f88cebSDwayne Grant McConnell 
85667207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
85767207b96SArnd Bergmann 		return -EFAULT;
85867207b96SArnd Bergmann 
85917f88cebSDwayne Grant McConnell out:
86017f88cebSDwayne Grant McConnell 	return ret;
86167207b96SArnd Bergmann }
86267207b96SArnd Bergmann 
863bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
864bf1ab978SDwayne Grant McConnell 			size_t len, loff_t *pos)
865bf1ab978SDwayne Grant McConnell {
866bf1ab978SDwayne Grant McConnell 	int ret;
867bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
868bf1ab978SDwayne Grant McConnell 
869bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
870bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal1_read(ctx, buf, len, pos);
871bf1ab978SDwayne Grant McConnell 	spu_release(ctx);
872bf1ab978SDwayne Grant McConnell 
873bf1ab978SDwayne Grant McConnell 	return ret;
874bf1ab978SDwayne Grant McConnell }
875bf1ab978SDwayne Grant McConnell 
87667207b96SArnd Bergmann static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
87767207b96SArnd Bergmann 			size_t len, loff_t *pos)
87867207b96SArnd Bergmann {
87967207b96SArnd Bergmann 	struct spu_context *ctx;
88067207b96SArnd Bergmann 	u32 data;
88167207b96SArnd Bergmann 
88267207b96SArnd Bergmann 	ctx = file->private_data;
88367207b96SArnd Bergmann 
88467207b96SArnd Bergmann 	if (len < 4)
88567207b96SArnd Bergmann 		return -EINVAL;
88667207b96SArnd Bergmann 
88767207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
88867207b96SArnd Bergmann 		return -EFAULT;
88967207b96SArnd Bergmann 
8908b3d6663SArnd Bergmann 	spu_acquire(ctx);
8918b3d6663SArnd Bergmann 	ctx->ops->signal1_write(ctx, data);
8928b3d6663SArnd Bergmann 	spu_release(ctx);
89367207b96SArnd Bergmann 
89467207b96SArnd Bergmann 	return 4;
89567207b96SArnd Bergmann }
89667207b96SArnd Bergmann 
89778bde53eSBenjamin Herrenschmidt static unsigned long spufs_signal1_mmap_nopfn(struct vm_area_struct *vma,
89878bde53eSBenjamin Herrenschmidt 					      unsigned long address)
8996df10a82SMark Nutter {
90027d5bf2aSBenjamin Herrenschmidt #if PAGE_SIZE == 0x1000
90178bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x14000, 0x1000);
90227d5bf2aSBenjamin Herrenschmidt #elif PAGE_SIZE == 0x10000
90327d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
90427d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
90527d5bf2aSBenjamin Herrenschmidt 	 */
90678bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
90727d5bf2aSBenjamin Herrenschmidt #else
90827d5bf2aSBenjamin Herrenschmidt #error unsupported page size
90927d5bf2aSBenjamin Herrenschmidt #endif
9106df10a82SMark Nutter }
9116df10a82SMark Nutter 
9126df10a82SMark Nutter static struct vm_operations_struct spufs_signal1_mmap_vmops = {
91378bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_signal1_mmap_nopfn,
9146df10a82SMark Nutter };
9156df10a82SMark Nutter 
9166df10a82SMark Nutter static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
9176df10a82SMark Nutter {
9186df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
9196df10a82SMark Nutter 		return -EINVAL;
9206df10a82SMark Nutter 
92178bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
9226df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
92323cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
9246df10a82SMark Nutter 
9256df10a82SMark Nutter 	vma->vm_ops = &spufs_signal1_mmap_vmops;
9266df10a82SMark Nutter 	return 0;
9276df10a82SMark Nutter }
9286df10a82SMark Nutter 
9295dfe4c96SArjan van de Ven static const struct file_operations spufs_signal1_fops = {
9306df10a82SMark Nutter 	.open = spufs_signal1_open,
93143c2bbd9SChristoph Hellwig 	.release = spufs_signal1_release,
93267207b96SArnd Bergmann 	.read = spufs_signal1_read,
93367207b96SArnd Bergmann 	.write = spufs_signal1_write,
9346df10a82SMark Nutter 	.mmap = spufs_signal1_mmap,
93567207b96SArnd Bergmann };
93667207b96SArnd Bergmann 
9376df10a82SMark Nutter static int spufs_signal2_open(struct inode *inode, struct file *file)
9386df10a82SMark Nutter {
9396df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
9406df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
94143c2bbd9SChristoph Hellwig 
94247d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
9436df10a82SMark Nutter 	file->private_data = ctx;
94443c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
9456df10a82SMark Nutter 		ctx->signal2 = inode->i_mapping;
94647d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
9476df10a82SMark Nutter 	return nonseekable_open(inode, file);
9486df10a82SMark Nutter }
9496df10a82SMark Nutter 
95043c2bbd9SChristoph Hellwig static int
95143c2bbd9SChristoph Hellwig spufs_signal2_release(struct inode *inode, struct file *file)
95243c2bbd9SChristoph Hellwig {
95343c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
95443c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
95543c2bbd9SChristoph Hellwig 
95647d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
95743c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
95843c2bbd9SChristoph Hellwig 		ctx->signal2 = NULL;
95947d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
96043c2bbd9SChristoph Hellwig 	return 0;
96143c2bbd9SChristoph Hellwig }
96243c2bbd9SChristoph Hellwig 
963bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
96467207b96SArnd Bergmann 			size_t len, loff_t *pos)
96567207b96SArnd Bergmann {
96617f88cebSDwayne Grant McConnell 	int ret = 0;
96767207b96SArnd Bergmann 	u32 data;
96867207b96SArnd Bergmann 
96967207b96SArnd Bergmann 	if (len < 4)
97067207b96SArnd Bergmann 		return -EINVAL;
97167207b96SArnd Bergmann 
97217f88cebSDwayne Grant McConnell 	if (ctx->csa.spu_chnlcnt_RW[4]) {
97317f88cebSDwayne Grant McConnell 		data =  ctx->csa.spu_chnldata_RW[4];
97417f88cebSDwayne Grant McConnell 		ret = 4;
97517f88cebSDwayne Grant McConnell 	}
9768b3d6663SArnd Bergmann 
97717f88cebSDwayne Grant McConnell 	if (!ret)
97817f88cebSDwayne Grant McConnell 		goto out;
97917f88cebSDwayne Grant McConnell 
98067207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
98167207b96SArnd Bergmann 		return -EFAULT;
98267207b96SArnd Bergmann 
98317f88cebSDwayne Grant McConnell out:
984bf1ab978SDwayne Grant McConnell 	return ret;
985bf1ab978SDwayne Grant McConnell }
986bf1ab978SDwayne Grant McConnell 
987bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
988bf1ab978SDwayne Grant McConnell 			size_t len, loff_t *pos)
989bf1ab978SDwayne Grant McConnell {
990bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
991bf1ab978SDwayne Grant McConnell 	int ret;
992bf1ab978SDwayne Grant McConnell 
993bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
994bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal2_read(ctx, buf, len, pos);
995bf1ab978SDwayne Grant McConnell 	spu_release(ctx);
996bf1ab978SDwayne Grant McConnell 
997bf1ab978SDwayne Grant McConnell 	return ret;
99867207b96SArnd Bergmann }
99967207b96SArnd Bergmann 
100067207b96SArnd Bergmann static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
100167207b96SArnd Bergmann 			size_t len, loff_t *pos)
100267207b96SArnd Bergmann {
100367207b96SArnd Bergmann 	struct spu_context *ctx;
100467207b96SArnd Bergmann 	u32 data;
100567207b96SArnd Bergmann 
100667207b96SArnd Bergmann 	ctx = file->private_data;
100767207b96SArnd Bergmann 
100867207b96SArnd Bergmann 	if (len < 4)
100967207b96SArnd Bergmann 		return -EINVAL;
101067207b96SArnd Bergmann 
101167207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
101267207b96SArnd Bergmann 		return -EFAULT;
101367207b96SArnd Bergmann 
10148b3d6663SArnd Bergmann 	spu_acquire(ctx);
10158b3d6663SArnd Bergmann 	ctx->ops->signal2_write(ctx, data);
10168b3d6663SArnd Bergmann 	spu_release(ctx);
101767207b96SArnd Bergmann 
101867207b96SArnd Bergmann 	return 4;
101967207b96SArnd Bergmann }
102067207b96SArnd Bergmann 
102127d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
102278bde53eSBenjamin Herrenschmidt static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
102378bde53eSBenjamin Herrenschmidt 					      unsigned long address)
10246df10a82SMark Nutter {
102527d5bf2aSBenjamin Herrenschmidt #if PAGE_SIZE == 0x1000
102678bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
102727d5bf2aSBenjamin Herrenschmidt #elif PAGE_SIZE == 0x10000
102827d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
102927d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
103027d5bf2aSBenjamin Herrenschmidt 	 */
103178bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
103227d5bf2aSBenjamin Herrenschmidt #else
103327d5bf2aSBenjamin Herrenschmidt #error unsupported page size
103427d5bf2aSBenjamin Herrenschmidt #endif
10356df10a82SMark Nutter }
10366df10a82SMark Nutter 
10376df10a82SMark Nutter static struct vm_operations_struct spufs_signal2_mmap_vmops = {
103878bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_signal2_mmap_nopfn,
10396df10a82SMark Nutter };
10406df10a82SMark Nutter 
10416df10a82SMark Nutter static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
10426df10a82SMark Nutter {
10436df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
10446df10a82SMark Nutter 		return -EINVAL;
10456df10a82SMark Nutter 
104678bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
10476df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
104823cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
10496df10a82SMark Nutter 
10506df10a82SMark Nutter 	vma->vm_ops = &spufs_signal2_mmap_vmops;
10516df10a82SMark Nutter 	return 0;
10526df10a82SMark Nutter }
105327d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
105427d5bf2aSBenjamin Herrenschmidt #define spufs_signal2_mmap NULL
105527d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
10566df10a82SMark Nutter 
10575dfe4c96SArjan van de Ven static const struct file_operations spufs_signal2_fops = {
10586df10a82SMark Nutter 	.open = spufs_signal2_open,
105943c2bbd9SChristoph Hellwig 	.release = spufs_signal2_release,
106067207b96SArnd Bergmann 	.read = spufs_signal2_read,
106167207b96SArnd Bergmann 	.write = spufs_signal2_write,
10626df10a82SMark Nutter 	.mmap = spufs_signal2_mmap,
106367207b96SArnd Bergmann };
106467207b96SArnd Bergmann 
106567207b96SArnd Bergmann static void spufs_signal1_type_set(void *data, u64 val)
106667207b96SArnd Bergmann {
106767207b96SArnd Bergmann 	struct spu_context *ctx = data;
106867207b96SArnd Bergmann 
10698b3d6663SArnd Bergmann 	spu_acquire(ctx);
10708b3d6663SArnd Bergmann 	ctx->ops->signal1_type_set(ctx, val);
10718b3d6663SArnd Bergmann 	spu_release(ctx);
107267207b96SArnd Bergmann }
107367207b96SArnd Bergmann 
1074bf1ab978SDwayne Grant McConnell static u64 __spufs_signal1_type_get(void *data)
1075bf1ab978SDwayne Grant McConnell {
1076bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
1077bf1ab978SDwayne Grant McConnell 	return ctx->ops->signal1_type_get(ctx);
1078bf1ab978SDwayne Grant McConnell }
1079bf1ab978SDwayne Grant McConnell 
108067207b96SArnd Bergmann static u64 spufs_signal1_type_get(void *data)
108167207b96SArnd Bergmann {
108267207b96SArnd Bergmann 	struct spu_context *ctx = data;
10838b3d6663SArnd Bergmann 	u64 ret;
10848b3d6663SArnd Bergmann 
10858b3d6663SArnd Bergmann 	spu_acquire(ctx);
1086bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal1_type_get(data);
10878b3d6663SArnd Bergmann 	spu_release(ctx);
10888b3d6663SArnd Bergmann 
10898b3d6663SArnd Bergmann 	return ret;
109067207b96SArnd Bergmann }
109167207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
109267207b96SArnd Bergmann 					spufs_signal1_type_set, "%llu");
109367207b96SArnd Bergmann 
109467207b96SArnd Bergmann static void spufs_signal2_type_set(void *data, u64 val)
109567207b96SArnd Bergmann {
109667207b96SArnd Bergmann 	struct spu_context *ctx = data;
109767207b96SArnd Bergmann 
10988b3d6663SArnd Bergmann 	spu_acquire(ctx);
10998b3d6663SArnd Bergmann 	ctx->ops->signal2_type_set(ctx, val);
11008b3d6663SArnd Bergmann 	spu_release(ctx);
110167207b96SArnd Bergmann }
110267207b96SArnd Bergmann 
1103bf1ab978SDwayne Grant McConnell static u64 __spufs_signal2_type_get(void *data)
1104bf1ab978SDwayne Grant McConnell {
1105bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
1106bf1ab978SDwayne Grant McConnell 	return ctx->ops->signal2_type_get(ctx);
1107bf1ab978SDwayne Grant McConnell }
1108bf1ab978SDwayne Grant McConnell 
110967207b96SArnd Bergmann static u64 spufs_signal2_type_get(void *data)
111067207b96SArnd Bergmann {
111167207b96SArnd Bergmann 	struct spu_context *ctx = data;
11128b3d6663SArnd Bergmann 	u64 ret;
11138b3d6663SArnd Bergmann 
11148b3d6663SArnd Bergmann 	spu_acquire(ctx);
1115bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal2_type_get(data);
11168b3d6663SArnd Bergmann 	spu_release(ctx);
11178b3d6663SArnd Bergmann 
11188b3d6663SArnd Bergmann 	return ret;
111967207b96SArnd Bergmann }
112067207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
112167207b96SArnd Bergmann 					spufs_signal2_type_set, "%llu");
112267207b96SArnd Bergmann 
112327d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
112478bde53eSBenjamin Herrenschmidt static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
112578bde53eSBenjamin Herrenschmidt 					  unsigned long address)
1126d9379c4bSarnd@arndb.de {
112778bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
1128d9379c4bSarnd@arndb.de }
1129d9379c4bSarnd@arndb.de 
1130d9379c4bSarnd@arndb.de static struct vm_operations_struct spufs_mss_mmap_vmops = {
113178bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_mss_mmap_nopfn,
1132d9379c4bSarnd@arndb.de };
1133d9379c4bSarnd@arndb.de 
1134d9379c4bSarnd@arndb.de /*
1135d9379c4bSarnd@arndb.de  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1136d9379c4bSarnd@arndb.de  */
1137d9379c4bSarnd@arndb.de static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1138d9379c4bSarnd@arndb.de {
1139d9379c4bSarnd@arndb.de 	if (!(vma->vm_flags & VM_SHARED))
1140d9379c4bSarnd@arndb.de 		return -EINVAL;
1141d9379c4bSarnd@arndb.de 
114278bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
1143d9379c4bSarnd@arndb.de 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
114423cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
1145d9379c4bSarnd@arndb.de 
1146d9379c4bSarnd@arndb.de 	vma->vm_ops = &spufs_mss_mmap_vmops;
1147d9379c4bSarnd@arndb.de 	return 0;
1148d9379c4bSarnd@arndb.de }
114927d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
115027d5bf2aSBenjamin Herrenschmidt #define spufs_mss_mmap NULL
115127d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
1152d9379c4bSarnd@arndb.de 
1153d9379c4bSarnd@arndb.de static int spufs_mss_open(struct inode *inode, struct file *file)
1154d9379c4bSarnd@arndb.de {
1155d9379c4bSarnd@arndb.de 	struct spufs_inode_info *i = SPUFS_I(inode);
115617e0e270SBenjamin Herrenschmidt 	struct spu_context *ctx = i->i_ctx;
1157d9379c4bSarnd@arndb.de 
1158d9379c4bSarnd@arndb.de 	file->private_data = i->i_ctx;
115943c2bbd9SChristoph Hellwig 
116047d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
116143c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
116217e0e270SBenjamin Herrenschmidt 		ctx->mss = inode->i_mapping;
116347d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1164d9379c4bSarnd@arndb.de 	return nonseekable_open(inode, file);
1165d9379c4bSarnd@arndb.de }
1166d9379c4bSarnd@arndb.de 
116743c2bbd9SChristoph Hellwig static int
116843c2bbd9SChristoph Hellwig spufs_mss_release(struct inode *inode, struct file *file)
116943c2bbd9SChristoph Hellwig {
117043c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
117143c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
117243c2bbd9SChristoph Hellwig 
117347d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
117443c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
117543c2bbd9SChristoph Hellwig 		ctx->mss = NULL;
117647d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
117743c2bbd9SChristoph Hellwig 	return 0;
117843c2bbd9SChristoph Hellwig }
117943c2bbd9SChristoph Hellwig 
11805dfe4c96SArjan van de Ven static const struct file_operations spufs_mss_fops = {
1181d9379c4bSarnd@arndb.de 	.open	 = spufs_mss_open,
118243c2bbd9SChristoph Hellwig 	.release = spufs_mss_release,
1183d9379c4bSarnd@arndb.de 	.mmap	 = spufs_mss_mmap,
118427d5bf2aSBenjamin Herrenschmidt };
118527d5bf2aSBenjamin Herrenschmidt 
118678bde53eSBenjamin Herrenschmidt static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
118778bde53eSBenjamin Herrenschmidt 					    unsigned long address)
118827d5bf2aSBenjamin Herrenschmidt {
118978bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
119027d5bf2aSBenjamin Herrenschmidt }
119127d5bf2aSBenjamin Herrenschmidt 
119227d5bf2aSBenjamin Herrenschmidt static struct vm_operations_struct spufs_psmap_mmap_vmops = {
119378bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_psmap_mmap_nopfn,
119427d5bf2aSBenjamin Herrenschmidt };
119527d5bf2aSBenjamin Herrenschmidt 
119627d5bf2aSBenjamin Herrenschmidt /*
119727d5bf2aSBenjamin Herrenschmidt  * mmap support for full problem state area [0x00000 - 0x1ffff].
119827d5bf2aSBenjamin Herrenschmidt  */
119927d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
120027d5bf2aSBenjamin Herrenschmidt {
120127d5bf2aSBenjamin Herrenschmidt 	if (!(vma->vm_flags & VM_SHARED))
120227d5bf2aSBenjamin Herrenschmidt 		return -EINVAL;
120327d5bf2aSBenjamin Herrenschmidt 
120478bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
120527d5bf2aSBenjamin Herrenschmidt 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
120627d5bf2aSBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
120727d5bf2aSBenjamin Herrenschmidt 
120827d5bf2aSBenjamin Herrenschmidt 	vma->vm_ops = &spufs_psmap_mmap_vmops;
120927d5bf2aSBenjamin Herrenschmidt 	return 0;
121027d5bf2aSBenjamin Herrenschmidt }
121127d5bf2aSBenjamin Herrenschmidt 
121227d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_open(struct inode *inode, struct file *file)
121327d5bf2aSBenjamin Herrenschmidt {
121427d5bf2aSBenjamin Herrenschmidt 	struct spufs_inode_info *i = SPUFS_I(inode);
121517e0e270SBenjamin Herrenschmidt 	struct spu_context *ctx = i->i_ctx;
121627d5bf2aSBenjamin Herrenschmidt 
121747d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
121827d5bf2aSBenjamin Herrenschmidt 	file->private_data = i->i_ctx;
121943c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
122017e0e270SBenjamin Herrenschmidt 		ctx->psmap = inode->i_mapping;
122147d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
122227d5bf2aSBenjamin Herrenschmidt 	return nonseekable_open(inode, file);
122327d5bf2aSBenjamin Herrenschmidt }
122427d5bf2aSBenjamin Herrenschmidt 
122543c2bbd9SChristoph Hellwig static int
122643c2bbd9SChristoph Hellwig spufs_psmap_release(struct inode *inode, struct file *file)
122743c2bbd9SChristoph Hellwig {
122843c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
122943c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
123043c2bbd9SChristoph Hellwig 
123147d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
123243c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
123343c2bbd9SChristoph Hellwig 		ctx->psmap = NULL;
123447d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
123543c2bbd9SChristoph Hellwig 	return 0;
123643c2bbd9SChristoph Hellwig }
123743c2bbd9SChristoph Hellwig 
12385dfe4c96SArjan van de Ven static const struct file_operations spufs_psmap_fops = {
123927d5bf2aSBenjamin Herrenschmidt 	.open	 = spufs_psmap_open,
124043c2bbd9SChristoph Hellwig 	.release = spufs_psmap_release,
124127d5bf2aSBenjamin Herrenschmidt 	.mmap	 = spufs_psmap_mmap,
1242d9379c4bSarnd@arndb.de };
1243d9379c4bSarnd@arndb.de 
1244d9379c4bSarnd@arndb.de 
124527d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
124678bde53eSBenjamin Herrenschmidt static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
124778bde53eSBenjamin Herrenschmidt 					  unsigned long address)
12486df10a82SMark Nutter {
124978bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
12506df10a82SMark Nutter }
12516df10a82SMark Nutter 
12526df10a82SMark Nutter static struct vm_operations_struct spufs_mfc_mmap_vmops = {
125378bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_mfc_mmap_nopfn,
12546df10a82SMark Nutter };
12556df10a82SMark Nutter 
12566df10a82SMark Nutter /*
12576df10a82SMark Nutter  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
12586df10a82SMark Nutter  */
12596df10a82SMark Nutter static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
12606df10a82SMark Nutter {
12616df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
12626df10a82SMark Nutter 		return -EINVAL;
12636df10a82SMark Nutter 
126478bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
12656df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
126623cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
12676df10a82SMark Nutter 
12686df10a82SMark Nutter 	vma->vm_ops = &spufs_mfc_mmap_vmops;
12696df10a82SMark Nutter 	return 0;
12706df10a82SMark Nutter }
127127d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
127227d5bf2aSBenjamin Herrenschmidt #define spufs_mfc_mmap NULL
127327d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
1274a33a7d73SArnd Bergmann 
1275a33a7d73SArnd Bergmann static int spufs_mfc_open(struct inode *inode, struct file *file)
1276a33a7d73SArnd Bergmann {
1277a33a7d73SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
1278a33a7d73SArnd Bergmann 	struct spu_context *ctx = i->i_ctx;
1279a33a7d73SArnd Bergmann 
1280a33a7d73SArnd Bergmann 	/* we don't want to deal with DMA into other processes */
1281a33a7d73SArnd Bergmann 	if (ctx->owner != current->mm)
1282a33a7d73SArnd Bergmann 		return -EINVAL;
1283a33a7d73SArnd Bergmann 
1284a33a7d73SArnd Bergmann 	if (atomic_read(&inode->i_count) != 1)
1285a33a7d73SArnd Bergmann 		return -EBUSY;
1286a33a7d73SArnd Bergmann 
128747d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
1288a33a7d73SArnd Bergmann 	file->private_data = ctx;
128943c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
129017e0e270SBenjamin Herrenschmidt 		ctx->mfc = inode->i_mapping;
129147d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1292a33a7d73SArnd Bergmann 	return nonseekable_open(inode, file);
1293a33a7d73SArnd Bergmann }
1294a33a7d73SArnd Bergmann 
129543c2bbd9SChristoph Hellwig static int
129643c2bbd9SChristoph Hellwig spufs_mfc_release(struct inode *inode, struct file *file)
129743c2bbd9SChristoph Hellwig {
129843c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
129943c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
130043c2bbd9SChristoph Hellwig 
130147d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
130243c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
130343c2bbd9SChristoph Hellwig 		ctx->mfc = NULL;
130447d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
130543c2bbd9SChristoph Hellwig 	return 0;
130643c2bbd9SChristoph Hellwig }
130743c2bbd9SChristoph Hellwig 
1308a33a7d73SArnd Bergmann /* interrupt-level mfc callback function. */
1309a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu)
1310a33a7d73SArnd Bergmann {
1311a33a7d73SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
1312a33a7d73SArnd Bergmann 
1313a33a7d73SArnd Bergmann 	wake_up_all(&ctx->mfc_wq);
1314a33a7d73SArnd Bergmann 
1315a33a7d73SArnd Bergmann 	pr_debug("%s %s\n", __FUNCTION__, spu->name);
1316a33a7d73SArnd Bergmann 	if (ctx->mfc_fasync) {
1317a33a7d73SArnd Bergmann 		u32 free_elements, tagstatus;
1318a33a7d73SArnd Bergmann 		unsigned int mask;
1319a33a7d73SArnd Bergmann 
1320a33a7d73SArnd Bergmann 		/* no need for spu_acquire in interrupt context */
1321a33a7d73SArnd Bergmann 		free_elements = ctx->ops->get_mfc_free_elements(ctx);
1322a33a7d73SArnd Bergmann 		tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1323a33a7d73SArnd Bergmann 
1324a33a7d73SArnd Bergmann 		mask = 0;
1325a33a7d73SArnd Bergmann 		if (free_elements & 0xffff)
1326a33a7d73SArnd Bergmann 			mask |= POLLOUT;
1327a33a7d73SArnd Bergmann 		if (tagstatus & ctx->tagwait)
1328a33a7d73SArnd Bergmann 			mask |= POLLIN;
1329a33a7d73SArnd Bergmann 
1330a33a7d73SArnd Bergmann 		kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1331a33a7d73SArnd Bergmann 	}
1332a33a7d73SArnd Bergmann }
1333a33a7d73SArnd Bergmann 
1334a33a7d73SArnd Bergmann static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1335a33a7d73SArnd Bergmann {
1336a33a7d73SArnd Bergmann 	/* See if there is one tag group is complete */
1337a33a7d73SArnd Bergmann 	/* FIXME we need locking around tagwait */
1338a33a7d73SArnd Bergmann 	*status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1339a33a7d73SArnd Bergmann 	ctx->tagwait &= ~*status;
1340a33a7d73SArnd Bergmann 	if (*status)
1341a33a7d73SArnd Bergmann 		return 1;
1342a33a7d73SArnd Bergmann 
1343a33a7d73SArnd Bergmann 	/* enable interrupt waiting for any tag group,
1344a33a7d73SArnd Bergmann 	   may silently fail if interrupts are already enabled */
1345a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1346a33a7d73SArnd Bergmann 	return 0;
1347a33a7d73SArnd Bergmann }
1348a33a7d73SArnd Bergmann 
1349a33a7d73SArnd Bergmann static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1350a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1351a33a7d73SArnd Bergmann {
1352a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1353a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1354a33a7d73SArnd Bergmann 	u32 status;
1355a33a7d73SArnd Bergmann 
1356a33a7d73SArnd Bergmann 	if (size != 4)
1357a33a7d73SArnd Bergmann 		goto out;
1358a33a7d73SArnd Bergmann 
1359a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1360a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1361a33a7d73SArnd Bergmann 		status = ctx->ops->read_mfc_tagstatus(ctx);
1362a33a7d73SArnd Bergmann 		if (!(status & ctx->tagwait))
1363a33a7d73SArnd Bergmann 			ret = -EAGAIN;
1364a33a7d73SArnd Bergmann 		else
1365a33a7d73SArnd Bergmann 			ctx->tagwait &= ~status;
1366a33a7d73SArnd Bergmann 	} else {
1367a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1368a33a7d73SArnd Bergmann 			   spufs_read_mfc_tagstatus(ctx, &status));
1369a33a7d73SArnd Bergmann 	}
1370a33a7d73SArnd Bergmann 	spu_release(ctx);
1371a33a7d73SArnd Bergmann 
1372a33a7d73SArnd Bergmann 	if (ret)
1373a33a7d73SArnd Bergmann 		goto out;
1374a33a7d73SArnd Bergmann 
1375a33a7d73SArnd Bergmann 	ret = 4;
1376a33a7d73SArnd Bergmann 	if (copy_to_user(buffer, &status, 4))
1377a33a7d73SArnd Bergmann 		ret = -EFAULT;
1378a33a7d73SArnd Bergmann 
1379a33a7d73SArnd Bergmann out:
1380a33a7d73SArnd Bergmann 	return ret;
1381a33a7d73SArnd Bergmann }
1382a33a7d73SArnd Bergmann 
1383a33a7d73SArnd Bergmann static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1384a33a7d73SArnd Bergmann {
1385a33a7d73SArnd Bergmann 	pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1386a33a7d73SArnd Bergmann 		 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1387a33a7d73SArnd Bergmann 
1388a33a7d73SArnd Bergmann 	switch (cmd->cmd) {
1389a33a7d73SArnd Bergmann 	case MFC_PUT_CMD:
1390a33a7d73SArnd Bergmann 	case MFC_PUTF_CMD:
1391a33a7d73SArnd Bergmann 	case MFC_PUTB_CMD:
1392a33a7d73SArnd Bergmann 	case MFC_GET_CMD:
1393a33a7d73SArnd Bergmann 	case MFC_GETF_CMD:
1394a33a7d73SArnd Bergmann 	case MFC_GETB_CMD:
1395a33a7d73SArnd Bergmann 		break;
1396a33a7d73SArnd Bergmann 	default:
1397a33a7d73SArnd Bergmann 		pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1398a33a7d73SArnd Bergmann 		return -EIO;
1399a33a7d73SArnd Bergmann 	}
1400a33a7d73SArnd Bergmann 
1401a33a7d73SArnd Bergmann 	if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1402a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1403a33a7d73SArnd Bergmann 				cmd->ea, cmd->lsa);
1404a33a7d73SArnd Bergmann 		return -EIO;
1405a33a7d73SArnd Bergmann 	}
1406a33a7d73SArnd Bergmann 
1407a33a7d73SArnd Bergmann 	switch (cmd->size & 0xf) {
1408a33a7d73SArnd Bergmann 	case 1:
1409a33a7d73SArnd Bergmann 		break;
1410a33a7d73SArnd Bergmann 	case 2:
1411a33a7d73SArnd Bergmann 		if (cmd->lsa & 1)
1412a33a7d73SArnd Bergmann 			goto error;
1413a33a7d73SArnd Bergmann 		break;
1414a33a7d73SArnd Bergmann 	case 4:
1415a33a7d73SArnd Bergmann 		if (cmd->lsa & 3)
1416a33a7d73SArnd Bergmann 			goto error;
1417a33a7d73SArnd Bergmann 		break;
1418a33a7d73SArnd Bergmann 	case 8:
1419a33a7d73SArnd Bergmann 		if (cmd->lsa & 7)
1420a33a7d73SArnd Bergmann 			goto error;
1421a33a7d73SArnd Bergmann 		break;
1422a33a7d73SArnd Bergmann 	case 0:
1423a33a7d73SArnd Bergmann 		if (cmd->lsa & 15)
1424a33a7d73SArnd Bergmann 			goto error;
1425a33a7d73SArnd Bergmann 		break;
1426a33a7d73SArnd Bergmann 	error:
1427a33a7d73SArnd Bergmann 	default:
1428a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment %x for size %x\n",
1429a33a7d73SArnd Bergmann 			cmd->lsa & 0xf, cmd->size);
1430a33a7d73SArnd Bergmann 		return -EIO;
1431a33a7d73SArnd Bergmann 	}
1432a33a7d73SArnd Bergmann 
1433a33a7d73SArnd Bergmann 	if (cmd->size > 16 * 1024) {
1434a33a7d73SArnd Bergmann 		pr_debug("invalid DMA size %x\n", cmd->size);
1435a33a7d73SArnd Bergmann 		return -EIO;
1436a33a7d73SArnd Bergmann 	}
1437a33a7d73SArnd Bergmann 
1438a33a7d73SArnd Bergmann 	if (cmd->tag & 0xfff0) {
1439a33a7d73SArnd Bergmann 		/* we reserve the higher tag numbers for kernel use */
1440a33a7d73SArnd Bergmann 		pr_debug("invalid DMA tag\n");
1441a33a7d73SArnd Bergmann 		return -EIO;
1442a33a7d73SArnd Bergmann 	}
1443a33a7d73SArnd Bergmann 
1444a33a7d73SArnd Bergmann 	if (cmd->class) {
1445a33a7d73SArnd Bergmann 		/* not supported in this version */
1446a33a7d73SArnd Bergmann 		pr_debug("invalid DMA class\n");
1447a33a7d73SArnd Bergmann 		return -EIO;
1448a33a7d73SArnd Bergmann 	}
1449a33a7d73SArnd Bergmann 
1450a33a7d73SArnd Bergmann 	return 0;
1451a33a7d73SArnd Bergmann }
1452a33a7d73SArnd Bergmann 
1453a33a7d73SArnd Bergmann static int spu_send_mfc_command(struct spu_context *ctx,
1454a33a7d73SArnd Bergmann 				struct mfc_dma_command cmd,
1455a33a7d73SArnd Bergmann 				int *error)
1456a33a7d73SArnd Bergmann {
1457a33a7d73SArnd Bergmann 	*error = ctx->ops->send_mfc_command(ctx, &cmd);
1458a33a7d73SArnd Bergmann 	if (*error == -EAGAIN) {
1459a33a7d73SArnd Bergmann 		/* wait for any tag group to complete
1460a33a7d73SArnd Bergmann 		   so we have space for the new command */
1461a33a7d73SArnd Bergmann 		ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1462a33a7d73SArnd Bergmann 		/* try again, because the queue might be
1463a33a7d73SArnd Bergmann 		   empty again */
1464a33a7d73SArnd Bergmann 		*error = ctx->ops->send_mfc_command(ctx, &cmd);
1465a33a7d73SArnd Bergmann 		if (*error == -EAGAIN)
1466a33a7d73SArnd Bergmann 			return 0;
1467a33a7d73SArnd Bergmann 	}
1468a33a7d73SArnd Bergmann 	return 1;
1469a33a7d73SArnd Bergmann }
1470a33a7d73SArnd Bergmann 
1471a33a7d73SArnd Bergmann static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1472a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1473a33a7d73SArnd Bergmann {
1474a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1475a33a7d73SArnd Bergmann 	struct mfc_dma_command cmd;
1476a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1477a33a7d73SArnd Bergmann 
1478a33a7d73SArnd Bergmann 	if (size != sizeof cmd)
1479a33a7d73SArnd Bergmann 		goto out;
1480a33a7d73SArnd Bergmann 
1481a33a7d73SArnd Bergmann 	ret = -EFAULT;
1482a33a7d73SArnd Bergmann 	if (copy_from_user(&cmd, buffer, sizeof cmd))
1483a33a7d73SArnd Bergmann 		goto out;
1484a33a7d73SArnd Bergmann 
1485a33a7d73SArnd Bergmann 	ret = spufs_check_valid_dma(&cmd);
1486a33a7d73SArnd Bergmann 	if (ret)
1487a33a7d73SArnd Bergmann 		goto out;
1488a33a7d73SArnd Bergmann 
1489577f8f10SAkinobu Mita 	ret = spu_acquire_runnable(ctx, 0);
1490577f8f10SAkinobu Mita 	if (ret)
1491577f8f10SAkinobu Mita 		goto out;
1492577f8f10SAkinobu Mita 
1493a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1494a33a7d73SArnd Bergmann 		ret = ctx->ops->send_mfc_command(ctx, &cmd);
1495a33a7d73SArnd Bergmann 	} else {
1496a33a7d73SArnd Bergmann 		int status;
1497a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1498a33a7d73SArnd Bergmann 				 spu_send_mfc_command(ctx, cmd, &status));
1499a33a7d73SArnd Bergmann 		if (status)
1500a33a7d73SArnd Bergmann 			ret = status;
1501a33a7d73SArnd Bergmann 	}
1502a33a7d73SArnd Bergmann 	spu_release(ctx);
1503a33a7d73SArnd Bergmann 
1504a33a7d73SArnd Bergmann 	if (ret)
1505a33a7d73SArnd Bergmann 		goto out;
1506a33a7d73SArnd Bergmann 
1507a33a7d73SArnd Bergmann 	ctx->tagwait |= 1 << cmd.tag;
15083692dc66SMasato Noguchi 	ret = size;
1509a33a7d73SArnd Bergmann 
1510a33a7d73SArnd Bergmann out:
1511a33a7d73SArnd Bergmann 	return ret;
1512a33a7d73SArnd Bergmann }
1513a33a7d73SArnd Bergmann 
1514a33a7d73SArnd Bergmann static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1515a33a7d73SArnd Bergmann {
1516a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1517a33a7d73SArnd Bergmann 	u32 free_elements, tagstatus;
1518a33a7d73SArnd Bergmann 	unsigned int mask;
1519a33a7d73SArnd Bergmann 
1520a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1521a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1522a33a7d73SArnd Bergmann 	free_elements = ctx->ops->get_mfc_free_elements(ctx);
1523a33a7d73SArnd Bergmann 	tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1524a33a7d73SArnd Bergmann 	spu_release(ctx);
1525a33a7d73SArnd Bergmann 
1526a33a7d73SArnd Bergmann 	poll_wait(file, &ctx->mfc_wq, wait);
1527a33a7d73SArnd Bergmann 
1528a33a7d73SArnd Bergmann 	mask = 0;
1529a33a7d73SArnd Bergmann 	if (free_elements & 0xffff)
1530a33a7d73SArnd Bergmann 		mask |= POLLOUT | POLLWRNORM;
1531a33a7d73SArnd Bergmann 	if (tagstatus & ctx->tagwait)
1532a33a7d73SArnd Bergmann 		mask |= POLLIN | POLLRDNORM;
1533a33a7d73SArnd Bergmann 
1534a33a7d73SArnd Bergmann 	pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1535a33a7d73SArnd Bergmann 		free_elements, tagstatus, ctx->tagwait);
1536a33a7d73SArnd Bergmann 
1537a33a7d73SArnd Bergmann 	return mask;
1538a33a7d73SArnd Bergmann }
1539a33a7d73SArnd Bergmann 
154073b6af8aSAl Viro static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1541a33a7d73SArnd Bergmann {
1542a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1543a33a7d73SArnd Bergmann 	int ret;
1544a33a7d73SArnd Bergmann 
1545a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1546a33a7d73SArnd Bergmann #if 0
1547a33a7d73SArnd Bergmann /* this currently hangs */
1548a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1549a33a7d73SArnd Bergmann 			 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1550a33a7d73SArnd Bergmann 	if (ret)
1551a33a7d73SArnd Bergmann 		goto out;
1552a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1553a33a7d73SArnd Bergmann 			 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1554a33a7d73SArnd Bergmann out:
1555a33a7d73SArnd Bergmann #else
1556a33a7d73SArnd Bergmann 	ret = 0;
1557a33a7d73SArnd Bergmann #endif
1558a33a7d73SArnd Bergmann 	spu_release(ctx);
1559a33a7d73SArnd Bergmann 
1560a33a7d73SArnd Bergmann 	return ret;
1561a33a7d73SArnd Bergmann }
1562a33a7d73SArnd Bergmann 
1563a33a7d73SArnd Bergmann static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1564a33a7d73SArnd Bergmann 			   int datasync)
1565a33a7d73SArnd Bergmann {
156673b6af8aSAl Viro 	return spufs_mfc_flush(file, NULL);
1567a33a7d73SArnd Bergmann }
1568a33a7d73SArnd Bergmann 
1569a33a7d73SArnd Bergmann static int spufs_mfc_fasync(int fd, struct file *file, int on)
1570a33a7d73SArnd Bergmann {
1571a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1572a33a7d73SArnd Bergmann 
1573a33a7d73SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1574a33a7d73SArnd Bergmann }
1575a33a7d73SArnd Bergmann 
15765dfe4c96SArjan van de Ven static const struct file_operations spufs_mfc_fops = {
1577a33a7d73SArnd Bergmann 	.open	 = spufs_mfc_open,
157843c2bbd9SChristoph Hellwig 	.release = spufs_mfc_release,
1579a33a7d73SArnd Bergmann 	.read	 = spufs_mfc_read,
1580a33a7d73SArnd Bergmann 	.write	 = spufs_mfc_write,
1581a33a7d73SArnd Bergmann 	.poll	 = spufs_mfc_poll,
1582a33a7d73SArnd Bergmann 	.flush	 = spufs_mfc_flush,
1583a33a7d73SArnd Bergmann 	.fsync	 = spufs_mfc_fsync,
1584a33a7d73SArnd Bergmann 	.fasync	 = spufs_mfc_fasync,
15856df10a82SMark Nutter 	.mmap	 = spufs_mfc_mmap,
1586a33a7d73SArnd Bergmann };
1587a33a7d73SArnd Bergmann 
158867207b96SArnd Bergmann static void spufs_npc_set(void *data, u64 val)
158967207b96SArnd Bergmann {
159067207b96SArnd Bergmann 	struct spu_context *ctx = data;
15918b3d6663SArnd Bergmann 	spu_acquire(ctx);
15928b3d6663SArnd Bergmann 	ctx->ops->npc_write(ctx, val);
15938b3d6663SArnd Bergmann 	spu_release(ctx);
159467207b96SArnd Bergmann }
159567207b96SArnd Bergmann 
159667207b96SArnd Bergmann static u64 spufs_npc_get(void *data)
159767207b96SArnd Bergmann {
159867207b96SArnd Bergmann 	struct spu_context *ctx = data;
159967207b96SArnd Bergmann 	u64 ret;
16008b3d6663SArnd Bergmann 	spu_acquire(ctx);
16018b3d6663SArnd Bergmann 	ret = ctx->ops->npc_read(ctx);
16028b3d6663SArnd Bergmann 	spu_release(ctx);
160367207b96SArnd Bergmann 	return ret;
160467207b96SArnd Bergmann }
16059b5047e2SDwayne Grant McConnell DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
16069b5047e2SDwayne Grant McConnell 			"0x%llx\n")
160767207b96SArnd Bergmann 
16088b3d6663SArnd Bergmann static void spufs_decr_set(void *data, u64 val)
16098b3d6663SArnd Bergmann {
16108b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16118b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
16128b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
16138b3d6663SArnd Bergmann 	lscsa->decr.slot[0] = (u32) val;
16148b3d6663SArnd Bergmann 	spu_release(ctx);
16158b3d6663SArnd Bergmann }
16168b3d6663SArnd Bergmann 
1617bf1ab978SDwayne Grant McConnell static u64 __spufs_decr_get(void *data)
16188b3d6663SArnd Bergmann {
16198b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16208b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1621bf1ab978SDwayne Grant McConnell 	return lscsa->decr.slot[0];
1622bf1ab978SDwayne Grant McConnell }
1623bf1ab978SDwayne Grant McConnell 
1624bf1ab978SDwayne Grant McConnell static u64 spufs_decr_get(void *data)
1625bf1ab978SDwayne Grant McConnell {
1626bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
16278b3d6663SArnd Bergmann 	u64 ret;
16288b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
1629bf1ab978SDwayne Grant McConnell 	ret = __spufs_decr_get(data);
16308b3d6663SArnd Bergmann 	spu_release(ctx);
16318b3d6663SArnd Bergmann 	return ret;
16328b3d6663SArnd Bergmann }
16338b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
16349b5047e2SDwayne Grant McConnell 			"0x%llx\n")
16358b3d6663SArnd Bergmann 
16368b3d6663SArnd Bergmann static void spufs_decr_status_set(void *data, u64 val)
16378b3d6663SArnd Bergmann {
16388b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16398b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
16408b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
16418b3d6663SArnd Bergmann 	lscsa->decr_status.slot[0] = (u32) val;
16428b3d6663SArnd Bergmann 	spu_release(ctx);
16438b3d6663SArnd Bergmann }
16448b3d6663SArnd Bergmann 
1645bf1ab978SDwayne Grant McConnell static u64 __spufs_decr_status_get(void *data)
16468b3d6663SArnd Bergmann {
16478b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16488b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1649bf1ab978SDwayne Grant McConnell 	return lscsa->decr_status.slot[0];
1650bf1ab978SDwayne Grant McConnell }
1651bf1ab978SDwayne Grant McConnell 
1652bf1ab978SDwayne Grant McConnell static u64 spufs_decr_status_get(void *data)
1653bf1ab978SDwayne Grant McConnell {
1654bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
16558b3d6663SArnd Bergmann 	u64 ret;
16568b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
1657bf1ab978SDwayne Grant McConnell 	ret = __spufs_decr_status_get(data);
16588b3d6663SArnd Bergmann 	spu_release(ctx);
16598b3d6663SArnd Bergmann 	return ret;
16608b3d6663SArnd Bergmann }
16618b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
16629b5047e2SDwayne Grant McConnell 			spufs_decr_status_set, "0x%llx\n")
16638b3d6663SArnd Bergmann 
16648b3d6663SArnd Bergmann static void spufs_event_mask_set(void *data, u64 val)
16658b3d6663SArnd Bergmann {
16668b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16678b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
16688b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
16698b3d6663SArnd Bergmann 	lscsa->event_mask.slot[0] = (u32) val;
16708b3d6663SArnd Bergmann 	spu_release(ctx);
16718b3d6663SArnd Bergmann }
16728b3d6663SArnd Bergmann 
1673bf1ab978SDwayne Grant McConnell static u64 __spufs_event_mask_get(void *data)
16748b3d6663SArnd Bergmann {
16758b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16768b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1677bf1ab978SDwayne Grant McConnell 	return lscsa->event_mask.slot[0];
1678bf1ab978SDwayne Grant McConnell }
1679bf1ab978SDwayne Grant McConnell 
1680bf1ab978SDwayne Grant McConnell static u64 spufs_event_mask_get(void *data)
1681bf1ab978SDwayne Grant McConnell {
1682bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
16838b3d6663SArnd Bergmann 	u64 ret;
16848b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
1685bf1ab978SDwayne Grant McConnell 	ret = __spufs_event_mask_get(data);
16868b3d6663SArnd Bergmann 	spu_release(ctx);
16878b3d6663SArnd Bergmann 	return ret;
16888b3d6663SArnd Bergmann }
16898b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
16909b5047e2SDwayne Grant McConnell 			spufs_event_mask_set, "0x%llx\n")
16918b3d6663SArnd Bergmann 
1692bf1ab978SDwayne Grant McConnell static u64 __spufs_event_status_get(void *data)
1693b9e3bd77SDwayne Grant McConnell {
1694b9e3bd77SDwayne Grant McConnell 	struct spu_context *ctx = data;
1695b9e3bd77SDwayne Grant McConnell 	struct spu_state *state = &ctx->csa;
1696b9e3bd77SDwayne Grant McConnell 	u64 stat;
1697b9e3bd77SDwayne Grant McConnell 	stat = state->spu_chnlcnt_RW[0];
1698b9e3bd77SDwayne Grant McConnell 	if (stat)
1699bf1ab978SDwayne Grant McConnell 		return state->spu_chnldata_RW[0];
1700bf1ab978SDwayne Grant McConnell 	return 0;
1701bf1ab978SDwayne Grant McConnell }
1702bf1ab978SDwayne Grant McConnell 
1703bf1ab978SDwayne Grant McConnell static u64 spufs_event_status_get(void *data)
1704bf1ab978SDwayne Grant McConnell {
1705bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
1706bf1ab978SDwayne Grant McConnell 	u64 ret = 0;
1707bf1ab978SDwayne Grant McConnell 
1708bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
1709bf1ab978SDwayne Grant McConnell 	ret = __spufs_event_status_get(data);
1710b9e3bd77SDwayne Grant McConnell 	spu_release(ctx);
1711b9e3bd77SDwayne Grant McConnell 	return ret;
1712b9e3bd77SDwayne Grant McConnell }
1713b9e3bd77SDwayne Grant McConnell DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1714b9e3bd77SDwayne Grant McConnell 			NULL, "0x%llx\n")
1715b9e3bd77SDwayne Grant McConnell 
17168b3d6663SArnd Bergmann static void spufs_srr0_set(void *data, u64 val)
17178b3d6663SArnd Bergmann {
17188b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
17198b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
17208b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
17218b3d6663SArnd Bergmann 	lscsa->srr0.slot[0] = (u32) val;
17228b3d6663SArnd Bergmann 	spu_release(ctx);
17238b3d6663SArnd Bergmann }
17248b3d6663SArnd Bergmann 
17258b3d6663SArnd Bergmann static u64 spufs_srr0_get(void *data)
17268b3d6663SArnd Bergmann {
17278b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
17288b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
17298b3d6663SArnd Bergmann 	u64 ret;
17308b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
17318b3d6663SArnd Bergmann 	ret = lscsa->srr0.slot[0];
17328b3d6663SArnd Bergmann 	spu_release(ctx);
17338b3d6663SArnd Bergmann 	return ret;
17348b3d6663SArnd Bergmann }
17358b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
17369b5047e2SDwayne Grant McConnell 			"0x%llx\n")
17378b3d6663SArnd Bergmann 
17387b1a7014Sarnd@arndb.de static u64 spufs_id_get(void *data)
17397b1a7014Sarnd@arndb.de {
17407b1a7014Sarnd@arndb.de 	struct spu_context *ctx = data;
17417b1a7014Sarnd@arndb.de 	u64 num;
17427b1a7014Sarnd@arndb.de 
17437b1a7014Sarnd@arndb.de 	spu_acquire(ctx);
17447b1a7014Sarnd@arndb.de 	if (ctx->state == SPU_STATE_RUNNABLE)
17457b1a7014Sarnd@arndb.de 		num = ctx->spu->number;
17467b1a7014Sarnd@arndb.de 	else
17477b1a7014Sarnd@arndb.de 		num = (unsigned int)-1;
17487b1a7014Sarnd@arndb.de 	spu_release(ctx);
17497b1a7014Sarnd@arndb.de 
17507b1a7014Sarnd@arndb.de 	return num;
17517b1a7014Sarnd@arndb.de }
1752e45d6634SAl Viro DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
17537b1a7014Sarnd@arndb.de 
1754bf1ab978SDwayne Grant McConnell static u64 __spufs_object_id_get(void *data)
175586767277SArnd Bergmann {
175686767277SArnd Bergmann 	struct spu_context *ctx = data;
175786767277SArnd Bergmann 	return ctx->object_id;
175886767277SArnd Bergmann }
175986767277SArnd Bergmann 
1760bf1ab978SDwayne Grant McConnell static u64 spufs_object_id_get(void *data)
1761bf1ab978SDwayne Grant McConnell {
1762bf1ab978SDwayne Grant McConnell 	/* FIXME: Should there really be no locking here? */
1763bf1ab978SDwayne Grant McConnell 	return __spufs_object_id_get(data);
1764bf1ab978SDwayne Grant McConnell }
1765bf1ab978SDwayne Grant McConnell 
176686767277SArnd Bergmann static void spufs_object_id_set(void *data, u64 id)
176786767277SArnd Bergmann {
176886767277SArnd Bergmann 	struct spu_context *ctx = data;
176986767277SArnd Bergmann 	ctx->object_id = id;
177086767277SArnd Bergmann }
177186767277SArnd Bergmann 
177286767277SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
177386767277SArnd Bergmann 		spufs_object_id_set, "0x%llx\n");
177486767277SArnd Bergmann 
1775bf1ab978SDwayne Grant McConnell static u64 __spufs_lslr_get(void *data)
1776bf1ab978SDwayne Grant McConnell {
1777bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
1778bf1ab978SDwayne Grant McConnell 	return ctx->csa.priv2.spu_lslr_RW;
1779bf1ab978SDwayne Grant McConnell }
1780bf1ab978SDwayne Grant McConnell 
1781b9e3bd77SDwayne Grant McConnell static u64 spufs_lslr_get(void *data)
1782b9e3bd77SDwayne Grant McConnell {
1783b9e3bd77SDwayne Grant McConnell 	struct spu_context *ctx = data;
1784b9e3bd77SDwayne Grant McConnell 	u64 ret;
1785b9e3bd77SDwayne Grant McConnell 
1786b9e3bd77SDwayne Grant McConnell 	spu_acquire_saved(ctx);
1787bf1ab978SDwayne Grant McConnell 	ret = __spufs_lslr_get(data);
1788b9e3bd77SDwayne Grant McConnell 	spu_release(ctx);
1789b9e3bd77SDwayne Grant McConnell 
1790b9e3bd77SDwayne Grant McConnell 	return ret;
1791b9e3bd77SDwayne Grant McConnell }
1792b9e3bd77SDwayne Grant McConnell DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
1793b9e3bd77SDwayne Grant McConnell 
1794b9e3bd77SDwayne Grant McConnell static int spufs_info_open(struct inode *inode, struct file *file)
1795b9e3bd77SDwayne Grant McConnell {
1796b9e3bd77SDwayne Grant McConnell 	struct spufs_inode_info *i = SPUFS_I(inode);
1797b9e3bd77SDwayne Grant McConnell 	struct spu_context *ctx = i->i_ctx;
1798b9e3bd77SDwayne Grant McConnell 	file->private_data = ctx;
1799b9e3bd77SDwayne Grant McConnell 	return 0;
1800b9e3bd77SDwayne Grant McConnell }
1801b9e3bd77SDwayne Grant McConnell 
1802cbe709c1SBenjamin Herrenschmidt static int spufs_caps_show(struct seq_file *s, void *private)
1803cbe709c1SBenjamin Herrenschmidt {
1804cbe709c1SBenjamin Herrenschmidt 	struct spu_context *ctx = s->private;
1805cbe709c1SBenjamin Herrenschmidt 
1806cbe709c1SBenjamin Herrenschmidt 	if (!(ctx->flags & SPU_CREATE_NOSCHED))
1807cbe709c1SBenjamin Herrenschmidt 		seq_puts(s, "sched\n");
1808cbe709c1SBenjamin Herrenschmidt 	if (!(ctx->flags & SPU_CREATE_ISOLATE))
1809cbe709c1SBenjamin Herrenschmidt 		seq_puts(s, "step\n");
1810cbe709c1SBenjamin Herrenschmidt 	return 0;
1811cbe709c1SBenjamin Herrenschmidt }
1812cbe709c1SBenjamin Herrenschmidt 
1813cbe709c1SBenjamin Herrenschmidt static int spufs_caps_open(struct inode *inode, struct file *file)
1814cbe709c1SBenjamin Herrenschmidt {
1815cbe709c1SBenjamin Herrenschmidt 	return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1816cbe709c1SBenjamin Herrenschmidt }
1817cbe709c1SBenjamin Herrenschmidt 
1818cbe709c1SBenjamin Herrenschmidt static const struct file_operations spufs_caps_fops = {
1819cbe709c1SBenjamin Herrenschmidt 	.open		= spufs_caps_open,
1820cbe709c1SBenjamin Herrenschmidt 	.read		= seq_read,
1821cbe709c1SBenjamin Herrenschmidt 	.llseek		= seq_lseek,
1822cbe709c1SBenjamin Herrenschmidt 	.release	= single_release,
1823cbe709c1SBenjamin Herrenschmidt };
1824cbe709c1SBenjamin Herrenschmidt 
1825bf1ab978SDwayne Grant McConnell static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1826bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
1827bf1ab978SDwayne Grant McConnell {
1828bf1ab978SDwayne Grant McConnell 	u32 mbox_stat;
1829bf1ab978SDwayne Grant McConnell 	u32 data;
1830bf1ab978SDwayne Grant McConnell 
1831bf1ab978SDwayne Grant McConnell 	mbox_stat = ctx->csa.prob.mb_stat_R;
1832bf1ab978SDwayne Grant McConnell 	if (mbox_stat & 0x0000ff) {
1833bf1ab978SDwayne Grant McConnell 		data = ctx->csa.prob.pu_mb_R;
1834bf1ab978SDwayne Grant McConnell 	}
1835bf1ab978SDwayne Grant McConnell 
1836bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1837bf1ab978SDwayne Grant McConnell }
1838bf1ab978SDwayne Grant McConnell 
183969a2f00cSDwayne Grant McConnell static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
184069a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
184169a2f00cSDwayne Grant McConnell {
1842bf1ab978SDwayne Grant McConnell 	int ret;
184369a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
184469a2f00cSDwayne Grant McConnell 
184569a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
184669a2f00cSDwayne Grant McConnell 		return -EFAULT;
184769a2f00cSDwayne Grant McConnell 
184869a2f00cSDwayne Grant McConnell 	spu_acquire_saved(ctx);
184969a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
1850bf1ab978SDwayne Grant McConnell 	ret = __spufs_mbox_info_read(ctx, buf, len, pos);
185169a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
185269a2f00cSDwayne Grant McConnell 	spu_release(ctx);
185369a2f00cSDwayne Grant McConnell 
1854bf1ab978SDwayne Grant McConnell 	return ret;
185569a2f00cSDwayne Grant McConnell }
185669a2f00cSDwayne Grant McConnell 
18575dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_info_fops = {
185869a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
185969a2f00cSDwayne Grant McConnell 	.read = spufs_mbox_info_read,
186069a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
186169a2f00cSDwayne Grant McConnell };
186269a2f00cSDwayne Grant McConnell 
1863bf1ab978SDwayne Grant McConnell static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
1864bf1ab978SDwayne Grant McConnell 				char __user *buf, size_t len, loff_t *pos)
1865bf1ab978SDwayne Grant McConnell {
1866bf1ab978SDwayne Grant McConnell 	u32 ibox_stat;
1867bf1ab978SDwayne Grant McConnell 	u32 data;
1868bf1ab978SDwayne Grant McConnell 
1869bf1ab978SDwayne Grant McConnell 	ibox_stat = ctx->csa.prob.mb_stat_R;
1870bf1ab978SDwayne Grant McConnell 	if (ibox_stat & 0xff0000) {
1871bf1ab978SDwayne Grant McConnell 		data = ctx->csa.priv2.puint_mb_R;
1872bf1ab978SDwayne Grant McConnell 	}
1873bf1ab978SDwayne Grant McConnell 
1874bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1875bf1ab978SDwayne Grant McConnell }
1876bf1ab978SDwayne Grant McConnell 
187769a2f00cSDwayne Grant McConnell static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
187869a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
187969a2f00cSDwayne Grant McConnell {
188069a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1881bf1ab978SDwayne Grant McConnell 	int ret;
188269a2f00cSDwayne Grant McConnell 
188369a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
188469a2f00cSDwayne Grant McConnell 		return -EFAULT;
188569a2f00cSDwayne Grant McConnell 
188669a2f00cSDwayne Grant McConnell 	spu_acquire_saved(ctx);
188769a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
1888bf1ab978SDwayne Grant McConnell 	ret = __spufs_ibox_info_read(ctx, buf, len, pos);
188969a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
189069a2f00cSDwayne Grant McConnell 	spu_release(ctx);
189169a2f00cSDwayne Grant McConnell 
1892bf1ab978SDwayne Grant McConnell 	return ret;
189369a2f00cSDwayne Grant McConnell }
189469a2f00cSDwayne Grant McConnell 
18955dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_info_fops = {
189669a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
189769a2f00cSDwayne Grant McConnell 	.read = spufs_ibox_info_read,
189869a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
189969a2f00cSDwayne Grant McConnell };
190069a2f00cSDwayne Grant McConnell 
1901bf1ab978SDwayne Grant McConnell static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
1902bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
1903bf1ab978SDwayne Grant McConnell {
1904bf1ab978SDwayne Grant McConnell 	int i, cnt;
1905bf1ab978SDwayne Grant McConnell 	u32 data[4];
1906bf1ab978SDwayne Grant McConnell 	u32 wbox_stat;
1907bf1ab978SDwayne Grant McConnell 
1908bf1ab978SDwayne Grant McConnell 	wbox_stat = ctx->csa.prob.mb_stat_R;
1909bf1ab978SDwayne Grant McConnell 	cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
1910bf1ab978SDwayne Grant McConnell 	for (i = 0; i < cnt; i++) {
1911bf1ab978SDwayne Grant McConnell 		data[i] = ctx->csa.spu_mailbox_data[i];
1912bf1ab978SDwayne Grant McConnell 	}
1913bf1ab978SDwayne Grant McConnell 
1914bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data,
1915bf1ab978SDwayne Grant McConnell 				cnt * sizeof(u32));
1916bf1ab978SDwayne Grant McConnell }
1917bf1ab978SDwayne Grant McConnell 
191869a2f00cSDwayne Grant McConnell static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
191969a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
192069a2f00cSDwayne Grant McConnell {
192169a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1922bf1ab978SDwayne Grant McConnell 	int ret;
192369a2f00cSDwayne Grant McConnell 
192469a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
192569a2f00cSDwayne Grant McConnell 		return -EFAULT;
192669a2f00cSDwayne Grant McConnell 
192769a2f00cSDwayne Grant McConnell 	spu_acquire_saved(ctx);
192869a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
1929bf1ab978SDwayne Grant McConnell 	ret = __spufs_wbox_info_read(ctx, buf, len, pos);
193069a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
193169a2f00cSDwayne Grant McConnell 	spu_release(ctx);
193269a2f00cSDwayne Grant McConnell 
1933bf1ab978SDwayne Grant McConnell 	return ret;
193469a2f00cSDwayne Grant McConnell }
193569a2f00cSDwayne Grant McConnell 
19365dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_info_fops = {
193769a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
193869a2f00cSDwayne Grant McConnell 	.read = spufs_wbox_info_read,
193969a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
194069a2f00cSDwayne Grant McConnell };
194169a2f00cSDwayne Grant McConnell 
1942bf1ab978SDwayne Grant McConnell static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
1943bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
1944b9e3bd77SDwayne Grant McConnell {
1945b9e3bd77SDwayne Grant McConnell 	struct spu_dma_info info;
1946b9e3bd77SDwayne Grant McConnell 	struct mfc_cq_sr *qp, *spuqp;
1947b9e3bd77SDwayne Grant McConnell 	int i;
1948b9e3bd77SDwayne Grant McConnell 
1949b9e3bd77SDwayne Grant McConnell 	info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
1950b9e3bd77SDwayne Grant McConnell 	info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
1951b9e3bd77SDwayne Grant McConnell 	info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
1952b9e3bd77SDwayne Grant McConnell 	info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
1953b9e3bd77SDwayne Grant McConnell 	info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
1954b9e3bd77SDwayne Grant McConnell 	for (i = 0; i < 16; i++) {
1955b9e3bd77SDwayne Grant McConnell 		qp = &info.dma_info_command_data[i];
1956b9e3bd77SDwayne Grant McConnell 		spuqp = &ctx->csa.priv2.spuq[i];
1957b9e3bd77SDwayne Grant McConnell 
1958b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
1959b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
1960b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
1961b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
1962b9e3bd77SDwayne Grant McConnell 	}
1963b9e3bd77SDwayne Grant McConnell 
1964b9e3bd77SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &info,
1965b9e3bd77SDwayne Grant McConnell 				sizeof info);
1966b9e3bd77SDwayne Grant McConnell }
1967b9e3bd77SDwayne Grant McConnell 
1968bf1ab978SDwayne Grant McConnell static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
1969bf1ab978SDwayne Grant McConnell 			      size_t len, loff_t *pos)
1970bf1ab978SDwayne Grant McConnell {
1971bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1972bf1ab978SDwayne Grant McConnell 	int ret;
1973bf1ab978SDwayne Grant McConnell 
1974bf1ab978SDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
1975bf1ab978SDwayne Grant McConnell 		return -EFAULT;
1976bf1ab978SDwayne Grant McConnell 
1977bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
1978bf1ab978SDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
1979bf1ab978SDwayne Grant McConnell 	ret = __spufs_dma_info_read(ctx, buf, len, pos);
1980bf1ab978SDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
1981bf1ab978SDwayne Grant McConnell 	spu_release(ctx);
1982bf1ab978SDwayne Grant McConnell 
1983bf1ab978SDwayne Grant McConnell 	return ret;
1984bf1ab978SDwayne Grant McConnell }
1985bf1ab978SDwayne Grant McConnell 
19865dfe4c96SArjan van de Ven static const struct file_operations spufs_dma_info_fops = {
1987b9e3bd77SDwayne Grant McConnell 	.open = spufs_info_open,
1988b9e3bd77SDwayne Grant McConnell 	.read = spufs_dma_info_read,
1989b9e3bd77SDwayne Grant McConnell };
1990b9e3bd77SDwayne Grant McConnell 
1991bf1ab978SDwayne Grant McConnell static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
1992bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
1993b9e3bd77SDwayne Grant McConnell {
1994b9e3bd77SDwayne Grant McConnell 	struct spu_proxydma_info info;
1995b9e3bd77SDwayne Grant McConnell 	struct mfc_cq_sr *qp, *puqp;
1996bf1ab978SDwayne Grant McConnell 	int ret = sizeof info;
1997b9e3bd77SDwayne Grant McConnell 	int i;
1998b9e3bd77SDwayne Grant McConnell 
1999b9e3bd77SDwayne Grant McConnell 	if (len < ret)
2000b9e3bd77SDwayne Grant McConnell 		return -EINVAL;
2001b9e3bd77SDwayne Grant McConnell 
2002b9e3bd77SDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
2003b9e3bd77SDwayne Grant McConnell 		return -EFAULT;
2004b9e3bd77SDwayne Grant McConnell 
2005b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2006b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2007b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2008b9e3bd77SDwayne Grant McConnell 	for (i = 0; i < 8; i++) {
2009b9e3bd77SDwayne Grant McConnell 		qp = &info.proxydma_info_command_data[i];
2010b9e3bd77SDwayne Grant McConnell 		puqp = &ctx->csa.priv2.puq[i];
2011b9e3bd77SDwayne Grant McConnell 
2012b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2013b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2014b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2015b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2016b9e3bd77SDwayne Grant McConnell 	}
2017bf1ab978SDwayne Grant McConnell 
2018bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &info,
2019bf1ab978SDwayne Grant McConnell 				sizeof info);
2020bf1ab978SDwayne Grant McConnell }
2021bf1ab978SDwayne Grant McConnell 
2022bf1ab978SDwayne Grant McConnell static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2023bf1ab978SDwayne Grant McConnell 				   size_t len, loff_t *pos)
2024bf1ab978SDwayne Grant McConnell {
2025bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2026bf1ab978SDwayne Grant McConnell 	int ret;
2027bf1ab978SDwayne Grant McConnell 
2028bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
2029bf1ab978SDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2030bf1ab978SDwayne Grant McConnell 	ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2031b9e3bd77SDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
2032b9e3bd77SDwayne Grant McConnell 	spu_release(ctx);
2033b9e3bd77SDwayne Grant McConnell 
2034b9e3bd77SDwayne Grant McConnell 	return ret;
2035b9e3bd77SDwayne Grant McConnell }
2036b9e3bd77SDwayne Grant McConnell 
20375dfe4c96SArjan van de Ven static const struct file_operations spufs_proxydma_info_fops = {
2038b9e3bd77SDwayne Grant McConnell 	.open = spufs_info_open,
2039b9e3bd77SDwayne Grant McConnell 	.read = spufs_proxydma_info_read,
2040b9e3bd77SDwayne Grant McConnell };
2041b9e3bd77SDwayne Grant McConnell 
2042476273adSChristoph Hellwig static int spufs_show_tid(struct seq_file *s, void *private)
2043476273adSChristoph Hellwig {
2044476273adSChristoph Hellwig 	struct spu_context *ctx = s->private;
2045476273adSChristoph Hellwig 
2046476273adSChristoph Hellwig 	seq_printf(s, "%d\n", ctx->tid);
2047476273adSChristoph Hellwig 	return 0;
2048476273adSChristoph Hellwig }
2049476273adSChristoph Hellwig 
2050476273adSChristoph Hellwig static int spufs_tid_open(struct inode *inode, struct file *file)
2051476273adSChristoph Hellwig {
2052476273adSChristoph Hellwig 	return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2053476273adSChristoph Hellwig }
2054476273adSChristoph Hellwig 
2055476273adSChristoph Hellwig static const struct file_operations spufs_tid_fops = {
2056476273adSChristoph Hellwig 	.open		= spufs_tid_open,
2057476273adSChristoph Hellwig 	.read		= seq_read,
2058476273adSChristoph Hellwig 	.llseek		= seq_lseek,
2059476273adSChristoph Hellwig 	.release	= single_release,
2060476273adSChristoph Hellwig };
2061476273adSChristoph Hellwig 
2062e9f8a0b6SChristoph Hellwig static const char *ctx_state_names[] = {
2063e9f8a0b6SChristoph Hellwig 	"user", "system", "iowait", "loaded"
2064e9f8a0b6SChristoph Hellwig };
2065e9f8a0b6SChristoph Hellwig 
2066e9f8a0b6SChristoph Hellwig static unsigned long long spufs_acct_time(struct spu_context *ctx,
2067e9f8a0b6SChristoph Hellwig 		enum spuctx_execution_state state)
2068e9f8a0b6SChristoph Hellwig {
2069e9f8a0b6SChristoph Hellwig 	unsigned long time = ctx->stats.times[state];
2070e9f8a0b6SChristoph Hellwig 
2071e9f8a0b6SChristoph Hellwig 	if (ctx->stats.execution_state == state)
2072e9f8a0b6SChristoph Hellwig 		time += jiffies - ctx->stats.tstamp;
2073e9f8a0b6SChristoph Hellwig 
2074e9f8a0b6SChristoph Hellwig 	return jiffies_to_msecs(time);
2075e9f8a0b6SChristoph Hellwig }
2076e9f8a0b6SChristoph Hellwig 
2077e9f8a0b6SChristoph Hellwig static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2078e9f8a0b6SChristoph Hellwig {
2079e9f8a0b6SChristoph Hellwig 	unsigned long long slb_flts = ctx->stats.slb_flt;
2080e9f8a0b6SChristoph Hellwig 
2081e9f8a0b6SChristoph Hellwig 	if (ctx->state == SPU_STATE_RUNNABLE) {
2082e9f8a0b6SChristoph Hellwig 		slb_flts += (ctx->spu->stats.slb_flt -
2083e9f8a0b6SChristoph Hellwig 			     ctx->stats.slb_flt_base);
2084e9f8a0b6SChristoph Hellwig 	}
2085e9f8a0b6SChristoph Hellwig 
2086e9f8a0b6SChristoph Hellwig 	return slb_flts;
2087e9f8a0b6SChristoph Hellwig }
2088e9f8a0b6SChristoph Hellwig 
2089e9f8a0b6SChristoph Hellwig static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2090e9f8a0b6SChristoph Hellwig {
2091e9f8a0b6SChristoph Hellwig 	unsigned long long class2_intrs = ctx->stats.class2_intr;
2092e9f8a0b6SChristoph Hellwig 
2093e9f8a0b6SChristoph Hellwig 	if (ctx->state == SPU_STATE_RUNNABLE) {
2094e9f8a0b6SChristoph Hellwig 		class2_intrs += (ctx->spu->stats.class2_intr -
2095e9f8a0b6SChristoph Hellwig 				 ctx->stats.class2_intr_base);
2096e9f8a0b6SChristoph Hellwig 	}
2097e9f8a0b6SChristoph Hellwig 
2098e9f8a0b6SChristoph Hellwig 	return class2_intrs;
2099e9f8a0b6SChristoph Hellwig }
2100e9f8a0b6SChristoph Hellwig 
2101e9f8a0b6SChristoph Hellwig 
2102e9f8a0b6SChristoph Hellwig static int spufs_show_stat(struct seq_file *s, void *private)
2103e9f8a0b6SChristoph Hellwig {
2104e9f8a0b6SChristoph Hellwig 	struct spu_context *ctx = s->private;
2105e9f8a0b6SChristoph Hellwig 
2106e9f8a0b6SChristoph Hellwig 	spu_acquire(ctx);
2107e9f8a0b6SChristoph Hellwig 	seq_printf(s, "%s %llu %llu %llu %llu "
2108e9f8a0b6SChristoph Hellwig 		      "%llu %llu %llu %llu %llu %llu %llu %llu\n",
2109e9f8a0b6SChristoph Hellwig 		ctx_state_names[ctx->stats.execution_state],
2110e9f8a0b6SChristoph Hellwig 		spufs_acct_time(ctx, SPUCTX_UTIL_USER),
2111e9f8a0b6SChristoph Hellwig 		spufs_acct_time(ctx, SPUCTX_UTIL_SYSTEM),
2112e9f8a0b6SChristoph Hellwig 		spufs_acct_time(ctx, SPUCTX_UTIL_IOWAIT),
2113e9f8a0b6SChristoph Hellwig 		spufs_acct_time(ctx, SPUCTX_UTIL_LOADED),
2114e9f8a0b6SChristoph Hellwig 		ctx->stats.vol_ctx_switch,
2115e9f8a0b6SChristoph Hellwig 		ctx->stats.invol_ctx_switch,
2116e9f8a0b6SChristoph Hellwig 		spufs_slb_flts(ctx),
2117e9f8a0b6SChristoph Hellwig 		ctx->stats.hash_flt,
2118e9f8a0b6SChristoph Hellwig 		ctx->stats.min_flt,
2119e9f8a0b6SChristoph Hellwig 		ctx->stats.maj_flt,
2120e9f8a0b6SChristoph Hellwig 		spufs_class2_intrs(ctx),
2121e9f8a0b6SChristoph Hellwig 		ctx->stats.libassist);
2122e9f8a0b6SChristoph Hellwig 	spu_release(ctx);
2123e9f8a0b6SChristoph Hellwig 	return 0;
2124e9f8a0b6SChristoph Hellwig }
2125e9f8a0b6SChristoph Hellwig 
2126e9f8a0b6SChristoph Hellwig static int spufs_stat_open(struct inode *inode, struct file *file)
2127e9f8a0b6SChristoph Hellwig {
2128e9f8a0b6SChristoph Hellwig 	return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2129e9f8a0b6SChristoph Hellwig }
2130e9f8a0b6SChristoph Hellwig 
2131e9f8a0b6SChristoph Hellwig static const struct file_operations spufs_stat_fops = {
2132e9f8a0b6SChristoph Hellwig 	.open		= spufs_stat_open,
2133e9f8a0b6SChristoph Hellwig 	.read		= seq_read,
2134e9f8a0b6SChristoph Hellwig 	.llseek		= seq_lseek,
2135e9f8a0b6SChristoph Hellwig 	.release	= single_release,
2136e9f8a0b6SChristoph Hellwig };
2137e9f8a0b6SChristoph Hellwig 
2138e9f8a0b6SChristoph Hellwig 
213967207b96SArnd Bergmann struct tree_descr spufs_dir_contents[] = {
2140cbe709c1SBenjamin Herrenschmidt 	{ "capabilities", &spufs_caps_fops, 0444, },
214167207b96SArnd Bergmann 	{ "mem",  &spufs_mem_fops,  0666, },
21428b3d6663SArnd Bergmann 	{ "regs", &spufs_regs_fops,  0666, },
214367207b96SArnd Bergmann 	{ "mbox", &spufs_mbox_fops, 0444, },
214467207b96SArnd Bergmann 	{ "ibox", &spufs_ibox_fops, 0444, },
214567207b96SArnd Bergmann 	{ "wbox", &spufs_wbox_fops, 0222, },
214667207b96SArnd Bergmann 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, },
214767207b96SArnd Bergmann 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, },
214867207b96SArnd Bergmann 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, },
214967207b96SArnd Bergmann 	{ "signal1", &spufs_signal1_fops, 0666, },
215067207b96SArnd Bergmann 	{ "signal2", &spufs_signal2_fops, 0666, },
215167207b96SArnd Bergmann 	{ "signal1_type", &spufs_signal1_type, 0666, },
215267207b96SArnd Bergmann 	{ "signal2_type", &spufs_signal2_type, 0666, },
21536df10a82SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
21548b3d6663SArnd Bergmann 	{ "fpcr", &spufs_fpcr_fops, 0666, },
2155b9e3bd77SDwayne Grant McConnell 	{ "lslr", &spufs_lslr_ops, 0444, },
2156b9e3bd77SDwayne Grant McConnell 	{ "mfc", &spufs_mfc_fops, 0666, },
2157b9e3bd77SDwayne Grant McConnell 	{ "mss", &spufs_mss_fops, 0666, },
2158b9e3bd77SDwayne Grant McConnell 	{ "npc", &spufs_npc_ops, 0666, },
2159b9e3bd77SDwayne Grant McConnell 	{ "srr0", &spufs_srr0_ops, 0666, },
21608b3d6663SArnd Bergmann 	{ "decr", &spufs_decr_ops, 0666, },
21618b3d6663SArnd Bergmann 	{ "decr_status", &spufs_decr_status_ops, 0666, },
21628b3d6663SArnd Bergmann 	{ "event_mask", &spufs_event_mask_ops, 0666, },
2163b9e3bd77SDwayne Grant McConnell 	{ "event_status", &spufs_event_status_ops, 0444, },
216427d5bf2aSBenjamin Herrenschmidt 	{ "psmap", &spufs_psmap_fops, 0666, },
216586767277SArnd Bergmann 	{ "phys-id", &spufs_id_ops, 0666, },
216686767277SArnd Bergmann 	{ "object-id", &spufs_object_id_ops, 0666, },
216769a2f00cSDwayne Grant McConnell 	{ "mbox_info", &spufs_mbox_info_fops, 0444, },
216869a2f00cSDwayne Grant McConnell 	{ "ibox_info", &spufs_ibox_info_fops, 0444, },
216969a2f00cSDwayne Grant McConnell 	{ "wbox_info", &spufs_wbox_info_fops, 0444, },
2170b9e3bd77SDwayne Grant McConnell 	{ "dma_info", &spufs_dma_info_fops, 0444, },
2171b9e3bd77SDwayne Grant McConnell 	{ "proxydma_info", &spufs_proxydma_info_fops, 0444, },
2172476273adSChristoph Hellwig 	{ "tid", &spufs_tid_fops, 0444, },
2173e9f8a0b6SChristoph Hellwig 	{ "stat", &spufs_stat_fops, 0444, },
217467207b96SArnd Bergmann 	{},
217567207b96SArnd Bergmann };
21765737edd1SMark Nutter 
21775737edd1SMark Nutter struct tree_descr spufs_dir_nosched_contents[] = {
2178cbe709c1SBenjamin Herrenschmidt 	{ "capabilities", &spufs_caps_fops, 0444, },
21795737edd1SMark Nutter 	{ "mem",  &spufs_mem_fops,  0666, },
21805737edd1SMark Nutter 	{ "mbox", &spufs_mbox_fops, 0444, },
21815737edd1SMark Nutter 	{ "ibox", &spufs_ibox_fops, 0444, },
21825737edd1SMark Nutter 	{ "wbox", &spufs_wbox_fops, 0222, },
21835737edd1SMark Nutter 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, },
21845737edd1SMark Nutter 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, },
21855737edd1SMark Nutter 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, },
21865737edd1SMark Nutter 	{ "signal1", &spufs_signal1_fops, 0666, },
21875737edd1SMark Nutter 	{ "signal2", &spufs_signal2_fops, 0666, },
21885737edd1SMark Nutter 	{ "signal1_type", &spufs_signal1_type, 0666, },
21895737edd1SMark Nutter 	{ "signal2_type", &spufs_signal2_type, 0666, },
21905737edd1SMark Nutter 	{ "mss", &spufs_mss_fops, 0666, },
21915737edd1SMark Nutter 	{ "mfc", &spufs_mfc_fops, 0666, },
21925737edd1SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
21935737edd1SMark Nutter 	{ "npc", &spufs_npc_ops, 0666, },
21945737edd1SMark Nutter 	{ "psmap", &spufs_psmap_fops, 0666, },
21955737edd1SMark Nutter 	{ "phys-id", &spufs_id_ops, 0666, },
21965737edd1SMark Nutter 	{ "object-id", &spufs_object_id_ops, 0666, },
2197476273adSChristoph Hellwig 	{ "tid", &spufs_tid_fops, 0444, },
2198e9f8a0b6SChristoph Hellwig 	{ "stat", &spufs_stat_fops, 0444, },
21995737edd1SMark Nutter 	{},
22005737edd1SMark Nutter };
2201bf1ab978SDwayne Grant McConnell 
2202bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader spufs_coredump_read[] = {
2203bf1ab978SDwayne Grant McConnell 	{ "regs", __spufs_regs_read, NULL, 128 * 16 },
2204bf1ab978SDwayne Grant McConnell 	{ "fpcr", __spufs_fpcr_read, NULL, 16 },
2205bf1ab978SDwayne Grant McConnell 	{ "lslr", NULL, __spufs_lslr_get, 11 },
2206bf1ab978SDwayne Grant McConnell 	{ "decr", NULL, __spufs_decr_get, 11 },
2207bf1ab978SDwayne Grant McConnell 	{ "decr_status", NULL, __spufs_decr_status_get, 11 },
2208bf1ab978SDwayne Grant McConnell 	{ "mem", __spufs_mem_read, NULL, 256 * 1024, },
2209bf1ab978SDwayne Grant McConnell 	{ "signal1", __spufs_signal1_read, NULL, 4 },
2210bf1ab978SDwayne Grant McConnell 	{ "signal1_type", NULL, __spufs_signal1_type_get, 2 },
2211bf1ab978SDwayne Grant McConnell 	{ "signal2", __spufs_signal2_read, NULL, 4 },
2212bf1ab978SDwayne Grant McConnell 	{ "signal2_type", NULL, __spufs_signal2_type_get, 2 },
2213bf1ab978SDwayne Grant McConnell 	{ "event_mask", NULL, __spufs_event_mask_get, 8 },
2214bf1ab978SDwayne Grant McConnell 	{ "event_status", NULL, __spufs_event_status_get, 8 },
2215bf1ab978SDwayne Grant McConnell 	{ "mbox_info", __spufs_mbox_info_read, NULL, 4 },
2216bf1ab978SDwayne Grant McConnell 	{ "ibox_info", __spufs_ibox_info_read, NULL, 4 },
2217bf1ab978SDwayne Grant McConnell 	{ "wbox_info", __spufs_wbox_info_read, NULL, 16 },
2218bf1ab978SDwayne Grant McConnell 	{ "dma_info", __spufs_dma_info_read, NULL, 69 * 8 },
2219bf1ab978SDwayne Grant McConnell 	{ "proxydma_info", __spufs_proxydma_info_read, NULL, 35 * 8 },
2220bf1ab978SDwayne Grant McConnell 	{ "object-id", NULL, __spufs_object_id_get, 19 },
2221bf1ab978SDwayne Grant McConnell 	{ },
2222bf1ab978SDwayne Grant McConnell };
2223bf1ab978SDwayne Grant McConnell int spufs_coredump_num_notes = ARRAY_SIZE(spufs_coredump_read) - 1;
2224bf1ab978SDwayne Grant McConnell 
2225