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
2021238819aSSebastian Siewior static unsigned long spufs_get_unmapped_area(struct file *file,
2031238819aSSebastian Siewior 		unsigned long addr, 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);
37327b1ea09SChristoph Hellwig 	spu_release_saved(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 
39527b1ea09SChristoph Hellwig 	spu_release_saved(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);
42427b1ea09SChristoph Hellwig 	spu_release_saved(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 
44627b1ea09SChristoph Hellwig 	spu_release_saved(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);
87127b1ea09SChristoph Hellwig 	spu_release_saved(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 
937d054b36fSJeremy Kerr static const struct file_operations spufs_signal1_nosched_fops = {
938d054b36fSJeremy Kerr 	.open = spufs_signal1_open,
939d054b36fSJeremy Kerr 	.release = spufs_signal1_release,
940d054b36fSJeremy Kerr 	.write = spufs_signal1_write,
941d054b36fSJeremy Kerr 	.mmap = spufs_signal1_mmap,
942d054b36fSJeremy Kerr };
943d054b36fSJeremy Kerr 
9446df10a82SMark Nutter static int spufs_signal2_open(struct inode *inode, struct file *file)
9456df10a82SMark Nutter {
9466df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
9476df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
94843c2bbd9SChristoph Hellwig 
94947d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
9506df10a82SMark Nutter 	file->private_data = ctx;
95143c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
9526df10a82SMark Nutter 		ctx->signal2 = inode->i_mapping;
95347d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
9546df10a82SMark Nutter 	return nonseekable_open(inode, file);
9556df10a82SMark Nutter }
9566df10a82SMark Nutter 
95743c2bbd9SChristoph Hellwig static int
95843c2bbd9SChristoph Hellwig spufs_signal2_release(struct inode *inode, struct file *file)
95943c2bbd9SChristoph Hellwig {
96043c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
96143c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
96243c2bbd9SChristoph Hellwig 
96347d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
96443c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
96543c2bbd9SChristoph Hellwig 		ctx->signal2 = NULL;
96647d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
96743c2bbd9SChristoph Hellwig 	return 0;
96843c2bbd9SChristoph Hellwig }
96943c2bbd9SChristoph Hellwig 
970bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
97167207b96SArnd Bergmann 			size_t len, loff_t *pos)
97267207b96SArnd Bergmann {
97317f88cebSDwayne Grant McConnell 	int ret = 0;
97467207b96SArnd Bergmann 	u32 data;
97567207b96SArnd Bergmann 
97667207b96SArnd Bergmann 	if (len < 4)
97767207b96SArnd Bergmann 		return -EINVAL;
97867207b96SArnd Bergmann 
97917f88cebSDwayne Grant McConnell 	if (ctx->csa.spu_chnlcnt_RW[4]) {
98017f88cebSDwayne Grant McConnell 		data =  ctx->csa.spu_chnldata_RW[4];
98117f88cebSDwayne Grant McConnell 		ret = 4;
98217f88cebSDwayne Grant McConnell 	}
9838b3d6663SArnd Bergmann 
98417f88cebSDwayne Grant McConnell 	if (!ret)
98517f88cebSDwayne Grant McConnell 		goto out;
98617f88cebSDwayne Grant McConnell 
98767207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
98867207b96SArnd Bergmann 		return -EFAULT;
98967207b96SArnd Bergmann 
99017f88cebSDwayne Grant McConnell out:
991bf1ab978SDwayne Grant McConnell 	return ret;
992bf1ab978SDwayne Grant McConnell }
993bf1ab978SDwayne Grant McConnell 
994bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
995bf1ab978SDwayne Grant McConnell 			size_t len, loff_t *pos)
996bf1ab978SDwayne Grant McConnell {
997bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
998bf1ab978SDwayne Grant McConnell 	int ret;
999bf1ab978SDwayne Grant McConnell 
1000bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
1001bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal2_read(ctx, buf, len, pos);
100227b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1003bf1ab978SDwayne Grant McConnell 
1004bf1ab978SDwayne Grant McConnell 	return ret;
100567207b96SArnd Bergmann }
100667207b96SArnd Bergmann 
100767207b96SArnd Bergmann static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
100867207b96SArnd Bergmann 			size_t len, loff_t *pos)
100967207b96SArnd Bergmann {
101067207b96SArnd Bergmann 	struct spu_context *ctx;
101167207b96SArnd Bergmann 	u32 data;
101267207b96SArnd Bergmann 
101367207b96SArnd Bergmann 	ctx = file->private_data;
101467207b96SArnd Bergmann 
101567207b96SArnd Bergmann 	if (len < 4)
101667207b96SArnd Bergmann 		return -EINVAL;
101767207b96SArnd Bergmann 
101867207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
101967207b96SArnd Bergmann 		return -EFAULT;
102067207b96SArnd Bergmann 
10218b3d6663SArnd Bergmann 	spu_acquire(ctx);
10228b3d6663SArnd Bergmann 	ctx->ops->signal2_write(ctx, data);
10238b3d6663SArnd Bergmann 	spu_release(ctx);
102467207b96SArnd Bergmann 
102567207b96SArnd Bergmann 	return 4;
102667207b96SArnd Bergmann }
102767207b96SArnd Bergmann 
102827d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
102978bde53eSBenjamin Herrenschmidt static unsigned long spufs_signal2_mmap_nopfn(struct vm_area_struct *vma,
103078bde53eSBenjamin Herrenschmidt 					      unsigned long address)
10316df10a82SMark Nutter {
103227d5bf2aSBenjamin Herrenschmidt #if PAGE_SIZE == 0x1000
103378bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x1c000, 0x1000);
103427d5bf2aSBenjamin Herrenschmidt #elif PAGE_SIZE == 0x10000
103527d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
103627d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
103727d5bf2aSBenjamin Herrenschmidt 	 */
103878bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x10000, 0x10000);
103927d5bf2aSBenjamin Herrenschmidt #else
104027d5bf2aSBenjamin Herrenschmidt #error unsupported page size
104127d5bf2aSBenjamin Herrenschmidt #endif
10426df10a82SMark Nutter }
10436df10a82SMark Nutter 
10446df10a82SMark Nutter static struct vm_operations_struct spufs_signal2_mmap_vmops = {
104578bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_signal2_mmap_nopfn,
10466df10a82SMark Nutter };
10476df10a82SMark Nutter 
10486df10a82SMark Nutter static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
10496df10a82SMark Nutter {
10506df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
10516df10a82SMark Nutter 		return -EINVAL;
10526df10a82SMark Nutter 
105378bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
10546df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
105523cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
10566df10a82SMark Nutter 
10576df10a82SMark Nutter 	vma->vm_ops = &spufs_signal2_mmap_vmops;
10586df10a82SMark Nutter 	return 0;
10596df10a82SMark Nutter }
106027d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
106127d5bf2aSBenjamin Herrenschmidt #define spufs_signal2_mmap NULL
106227d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
10636df10a82SMark Nutter 
10645dfe4c96SArjan van de Ven static const struct file_operations spufs_signal2_fops = {
10656df10a82SMark Nutter 	.open = spufs_signal2_open,
106643c2bbd9SChristoph Hellwig 	.release = spufs_signal2_release,
106767207b96SArnd Bergmann 	.read = spufs_signal2_read,
106867207b96SArnd Bergmann 	.write = spufs_signal2_write,
10696df10a82SMark Nutter 	.mmap = spufs_signal2_mmap,
107067207b96SArnd Bergmann };
107167207b96SArnd Bergmann 
1072d054b36fSJeremy Kerr static const struct file_operations spufs_signal2_nosched_fops = {
1073d054b36fSJeremy Kerr 	.open = spufs_signal2_open,
1074d054b36fSJeremy Kerr 	.release = spufs_signal2_release,
1075d054b36fSJeremy Kerr 	.write = spufs_signal2_write,
1076d054b36fSJeremy Kerr 	.mmap = spufs_signal2_mmap,
1077d054b36fSJeremy Kerr };
1078d054b36fSJeremy Kerr 
107967207b96SArnd Bergmann static void spufs_signal1_type_set(void *data, u64 val)
108067207b96SArnd Bergmann {
108167207b96SArnd Bergmann 	struct spu_context *ctx = data;
108267207b96SArnd Bergmann 
10838b3d6663SArnd Bergmann 	spu_acquire(ctx);
10848b3d6663SArnd Bergmann 	ctx->ops->signal1_type_set(ctx, val);
10858b3d6663SArnd Bergmann 	spu_release(ctx);
108667207b96SArnd Bergmann }
108767207b96SArnd Bergmann 
108874de08bcSMichael Ellerman static u64 __spufs_signal1_type_get(struct spu_context *ctx)
1089bf1ab978SDwayne Grant McConnell {
1090bf1ab978SDwayne Grant McConnell 	return ctx->ops->signal1_type_get(ctx);
1091bf1ab978SDwayne Grant McConnell }
1092bf1ab978SDwayne Grant McConnell 
109367207b96SArnd Bergmann static u64 spufs_signal1_type_get(void *data)
109467207b96SArnd Bergmann {
109567207b96SArnd Bergmann 	struct spu_context *ctx = data;
10968b3d6663SArnd Bergmann 	u64 ret;
10978b3d6663SArnd Bergmann 
10988b3d6663SArnd Bergmann 	spu_acquire(ctx);
109974de08bcSMichael Ellerman 	ret = __spufs_signal1_type_get(ctx);
11008b3d6663SArnd Bergmann 	spu_release(ctx);
11018b3d6663SArnd Bergmann 
11028b3d6663SArnd Bergmann 	return ret;
110367207b96SArnd Bergmann }
110467207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
110567207b96SArnd Bergmann 					spufs_signal1_type_set, "%llu");
110667207b96SArnd Bergmann 
110767207b96SArnd Bergmann static void spufs_signal2_type_set(void *data, u64 val)
110867207b96SArnd Bergmann {
110967207b96SArnd Bergmann 	struct spu_context *ctx = data;
111067207b96SArnd Bergmann 
11118b3d6663SArnd Bergmann 	spu_acquire(ctx);
11128b3d6663SArnd Bergmann 	ctx->ops->signal2_type_set(ctx, val);
11138b3d6663SArnd Bergmann 	spu_release(ctx);
111467207b96SArnd Bergmann }
111567207b96SArnd Bergmann 
111674de08bcSMichael Ellerman static u64 __spufs_signal2_type_get(struct spu_context *ctx)
1117bf1ab978SDwayne Grant McConnell {
1118bf1ab978SDwayne Grant McConnell 	return ctx->ops->signal2_type_get(ctx);
1119bf1ab978SDwayne Grant McConnell }
1120bf1ab978SDwayne Grant McConnell 
112167207b96SArnd Bergmann static u64 spufs_signal2_type_get(void *data)
112267207b96SArnd Bergmann {
112367207b96SArnd Bergmann 	struct spu_context *ctx = data;
11248b3d6663SArnd Bergmann 	u64 ret;
11258b3d6663SArnd Bergmann 
11268b3d6663SArnd Bergmann 	spu_acquire(ctx);
112774de08bcSMichael Ellerman 	ret = __spufs_signal2_type_get(ctx);
11288b3d6663SArnd Bergmann 	spu_release(ctx);
11298b3d6663SArnd Bergmann 
11308b3d6663SArnd Bergmann 	return ret;
113167207b96SArnd Bergmann }
113267207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
113367207b96SArnd Bergmann 					spufs_signal2_type_set, "%llu");
113467207b96SArnd Bergmann 
113527d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
113678bde53eSBenjamin Herrenschmidt static unsigned long spufs_mss_mmap_nopfn(struct vm_area_struct *vma,
113778bde53eSBenjamin Herrenschmidt 					  unsigned long address)
1138d9379c4bSarnd@arndb.de {
113978bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x0000, 0x1000);
1140d9379c4bSarnd@arndb.de }
1141d9379c4bSarnd@arndb.de 
1142d9379c4bSarnd@arndb.de static struct vm_operations_struct spufs_mss_mmap_vmops = {
114378bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_mss_mmap_nopfn,
1144d9379c4bSarnd@arndb.de };
1145d9379c4bSarnd@arndb.de 
1146d9379c4bSarnd@arndb.de /*
1147d9379c4bSarnd@arndb.de  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1148d9379c4bSarnd@arndb.de  */
1149d9379c4bSarnd@arndb.de static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1150d9379c4bSarnd@arndb.de {
1151d9379c4bSarnd@arndb.de 	if (!(vma->vm_flags & VM_SHARED))
1152d9379c4bSarnd@arndb.de 		return -EINVAL;
1153d9379c4bSarnd@arndb.de 
115478bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
1155d9379c4bSarnd@arndb.de 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
115623cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
1157d9379c4bSarnd@arndb.de 
1158d9379c4bSarnd@arndb.de 	vma->vm_ops = &spufs_mss_mmap_vmops;
1159d9379c4bSarnd@arndb.de 	return 0;
1160d9379c4bSarnd@arndb.de }
116127d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
116227d5bf2aSBenjamin Herrenschmidt #define spufs_mss_mmap NULL
116327d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
1164d9379c4bSarnd@arndb.de 
1165d9379c4bSarnd@arndb.de static int spufs_mss_open(struct inode *inode, struct file *file)
1166d9379c4bSarnd@arndb.de {
1167d9379c4bSarnd@arndb.de 	struct spufs_inode_info *i = SPUFS_I(inode);
116817e0e270SBenjamin Herrenschmidt 	struct spu_context *ctx = i->i_ctx;
1169d9379c4bSarnd@arndb.de 
1170d9379c4bSarnd@arndb.de 	file->private_data = i->i_ctx;
117143c2bbd9SChristoph Hellwig 
117247d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
117343c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
117417e0e270SBenjamin Herrenschmidt 		ctx->mss = inode->i_mapping;
117547d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1176d9379c4bSarnd@arndb.de 	return nonseekable_open(inode, file);
1177d9379c4bSarnd@arndb.de }
1178d9379c4bSarnd@arndb.de 
117943c2bbd9SChristoph Hellwig static int
118043c2bbd9SChristoph Hellwig spufs_mss_release(struct inode *inode, struct file *file)
118143c2bbd9SChristoph Hellwig {
118243c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
118343c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
118443c2bbd9SChristoph Hellwig 
118547d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
118643c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
118743c2bbd9SChristoph Hellwig 		ctx->mss = NULL;
118847d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
118943c2bbd9SChristoph Hellwig 	return 0;
119043c2bbd9SChristoph Hellwig }
119143c2bbd9SChristoph Hellwig 
11925dfe4c96SArjan van de Ven static const struct file_operations spufs_mss_fops = {
1193d9379c4bSarnd@arndb.de 	.open	 = spufs_mss_open,
119443c2bbd9SChristoph Hellwig 	.release = spufs_mss_release,
1195d9379c4bSarnd@arndb.de 	.mmap	 = spufs_mss_mmap,
119627d5bf2aSBenjamin Herrenschmidt };
119727d5bf2aSBenjamin Herrenschmidt 
119878bde53eSBenjamin Herrenschmidt static unsigned long spufs_psmap_mmap_nopfn(struct vm_area_struct *vma,
119978bde53eSBenjamin Herrenschmidt 					    unsigned long address)
120027d5bf2aSBenjamin Herrenschmidt {
120178bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x0000, 0x20000);
120227d5bf2aSBenjamin Herrenschmidt }
120327d5bf2aSBenjamin Herrenschmidt 
120427d5bf2aSBenjamin Herrenschmidt static struct vm_operations_struct spufs_psmap_mmap_vmops = {
120578bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_psmap_mmap_nopfn,
120627d5bf2aSBenjamin Herrenschmidt };
120727d5bf2aSBenjamin Herrenschmidt 
120827d5bf2aSBenjamin Herrenschmidt /*
120927d5bf2aSBenjamin Herrenschmidt  * mmap support for full problem state area [0x00000 - 0x1ffff].
121027d5bf2aSBenjamin Herrenschmidt  */
121127d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
121227d5bf2aSBenjamin Herrenschmidt {
121327d5bf2aSBenjamin Herrenschmidt 	if (!(vma->vm_flags & VM_SHARED))
121427d5bf2aSBenjamin Herrenschmidt 		return -EINVAL;
121527d5bf2aSBenjamin Herrenschmidt 
121678bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
121727d5bf2aSBenjamin Herrenschmidt 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
121827d5bf2aSBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
121927d5bf2aSBenjamin Herrenschmidt 
122027d5bf2aSBenjamin Herrenschmidt 	vma->vm_ops = &spufs_psmap_mmap_vmops;
122127d5bf2aSBenjamin Herrenschmidt 	return 0;
122227d5bf2aSBenjamin Herrenschmidt }
122327d5bf2aSBenjamin Herrenschmidt 
122427d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_open(struct inode *inode, struct file *file)
122527d5bf2aSBenjamin Herrenschmidt {
122627d5bf2aSBenjamin Herrenschmidt 	struct spufs_inode_info *i = SPUFS_I(inode);
122717e0e270SBenjamin Herrenschmidt 	struct spu_context *ctx = i->i_ctx;
122827d5bf2aSBenjamin Herrenschmidt 
122947d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
123027d5bf2aSBenjamin Herrenschmidt 	file->private_data = i->i_ctx;
123143c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
123217e0e270SBenjamin Herrenschmidt 		ctx->psmap = inode->i_mapping;
123347d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
123427d5bf2aSBenjamin Herrenschmidt 	return nonseekable_open(inode, file);
123527d5bf2aSBenjamin Herrenschmidt }
123627d5bf2aSBenjamin Herrenschmidt 
123743c2bbd9SChristoph Hellwig static int
123843c2bbd9SChristoph Hellwig spufs_psmap_release(struct inode *inode, struct file *file)
123943c2bbd9SChristoph Hellwig {
124043c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
124143c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
124243c2bbd9SChristoph Hellwig 
124347d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
124443c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
124543c2bbd9SChristoph Hellwig 		ctx->psmap = NULL;
124647d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
124743c2bbd9SChristoph Hellwig 	return 0;
124843c2bbd9SChristoph Hellwig }
124943c2bbd9SChristoph Hellwig 
12505dfe4c96SArjan van de Ven static const struct file_operations spufs_psmap_fops = {
125127d5bf2aSBenjamin Herrenschmidt 	.open	 = spufs_psmap_open,
125243c2bbd9SChristoph Hellwig 	.release = spufs_psmap_release,
125327d5bf2aSBenjamin Herrenschmidt 	.mmap	 = spufs_psmap_mmap,
1254d9379c4bSarnd@arndb.de };
1255d9379c4bSarnd@arndb.de 
1256d9379c4bSarnd@arndb.de 
125727d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
125878bde53eSBenjamin Herrenschmidt static unsigned long spufs_mfc_mmap_nopfn(struct vm_area_struct *vma,
125978bde53eSBenjamin Herrenschmidt 					  unsigned long address)
12606df10a82SMark Nutter {
126178bde53eSBenjamin Herrenschmidt 	return spufs_ps_nopfn(vma, address, 0x3000, 0x1000);
12626df10a82SMark Nutter }
12636df10a82SMark Nutter 
12646df10a82SMark Nutter static struct vm_operations_struct spufs_mfc_mmap_vmops = {
126578bde53eSBenjamin Herrenschmidt 	.nopfn = spufs_mfc_mmap_nopfn,
12666df10a82SMark Nutter };
12676df10a82SMark Nutter 
12686df10a82SMark Nutter /*
12696df10a82SMark Nutter  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
12706df10a82SMark Nutter  */
12716df10a82SMark Nutter static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
12726df10a82SMark Nutter {
12736df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
12746df10a82SMark Nutter 		return -EINVAL;
12756df10a82SMark Nutter 
127678bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
12776df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
127823cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
12796df10a82SMark Nutter 
12806df10a82SMark Nutter 	vma->vm_ops = &spufs_mfc_mmap_vmops;
12816df10a82SMark Nutter 	return 0;
12826df10a82SMark Nutter }
128327d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
128427d5bf2aSBenjamin Herrenschmidt #define spufs_mfc_mmap NULL
128527d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
1286a33a7d73SArnd Bergmann 
1287a33a7d73SArnd Bergmann static int spufs_mfc_open(struct inode *inode, struct file *file)
1288a33a7d73SArnd Bergmann {
1289a33a7d73SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
1290a33a7d73SArnd Bergmann 	struct spu_context *ctx = i->i_ctx;
1291a33a7d73SArnd Bergmann 
1292a33a7d73SArnd Bergmann 	/* we don't want to deal with DMA into other processes */
1293a33a7d73SArnd Bergmann 	if (ctx->owner != current->mm)
1294a33a7d73SArnd Bergmann 		return -EINVAL;
1295a33a7d73SArnd Bergmann 
1296a33a7d73SArnd Bergmann 	if (atomic_read(&inode->i_count) != 1)
1297a33a7d73SArnd Bergmann 		return -EBUSY;
1298a33a7d73SArnd Bergmann 
129947d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
1300a33a7d73SArnd Bergmann 	file->private_data = ctx;
130143c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
130217e0e270SBenjamin Herrenschmidt 		ctx->mfc = inode->i_mapping;
130347d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1304a33a7d73SArnd Bergmann 	return nonseekable_open(inode, file);
1305a33a7d73SArnd Bergmann }
1306a33a7d73SArnd Bergmann 
130743c2bbd9SChristoph Hellwig static int
130843c2bbd9SChristoph Hellwig spufs_mfc_release(struct inode *inode, struct file *file)
130943c2bbd9SChristoph Hellwig {
131043c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
131143c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
131243c2bbd9SChristoph Hellwig 
131347d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
131443c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
131543c2bbd9SChristoph Hellwig 		ctx->mfc = NULL;
131647d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
131743c2bbd9SChristoph Hellwig 	return 0;
131843c2bbd9SChristoph Hellwig }
131943c2bbd9SChristoph Hellwig 
1320a33a7d73SArnd Bergmann /* interrupt-level mfc callback function. */
1321a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu)
1322a33a7d73SArnd Bergmann {
1323a33a7d73SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
1324a33a7d73SArnd Bergmann 
1325a33a7d73SArnd Bergmann 	wake_up_all(&ctx->mfc_wq);
1326a33a7d73SArnd Bergmann 
1327a33a7d73SArnd Bergmann 	pr_debug("%s %s\n", __FUNCTION__, spu->name);
1328a33a7d73SArnd Bergmann 	if (ctx->mfc_fasync) {
1329a33a7d73SArnd Bergmann 		u32 free_elements, tagstatus;
1330a33a7d73SArnd Bergmann 		unsigned int mask;
1331a33a7d73SArnd Bergmann 
1332a33a7d73SArnd Bergmann 		/* no need for spu_acquire in interrupt context */
1333a33a7d73SArnd Bergmann 		free_elements = ctx->ops->get_mfc_free_elements(ctx);
1334a33a7d73SArnd Bergmann 		tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1335a33a7d73SArnd Bergmann 
1336a33a7d73SArnd Bergmann 		mask = 0;
1337a33a7d73SArnd Bergmann 		if (free_elements & 0xffff)
1338a33a7d73SArnd Bergmann 			mask |= POLLOUT;
1339a33a7d73SArnd Bergmann 		if (tagstatus & ctx->tagwait)
1340a33a7d73SArnd Bergmann 			mask |= POLLIN;
1341a33a7d73SArnd Bergmann 
1342a33a7d73SArnd Bergmann 		kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1343a33a7d73SArnd Bergmann 	}
1344a33a7d73SArnd Bergmann }
1345a33a7d73SArnd Bergmann 
1346a33a7d73SArnd Bergmann static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1347a33a7d73SArnd Bergmann {
1348a33a7d73SArnd Bergmann 	/* See if there is one tag group is complete */
1349a33a7d73SArnd Bergmann 	/* FIXME we need locking around tagwait */
1350a33a7d73SArnd Bergmann 	*status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1351a33a7d73SArnd Bergmann 	ctx->tagwait &= ~*status;
1352a33a7d73SArnd Bergmann 	if (*status)
1353a33a7d73SArnd Bergmann 		return 1;
1354a33a7d73SArnd Bergmann 
1355a33a7d73SArnd Bergmann 	/* enable interrupt waiting for any tag group,
1356a33a7d73SArnd Bergmann 	   may silently fail if interrupts are already enabled */
1357a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1358a33a7d73SArnd Bergmann 	return 0;
1359a33a7d73SArnd Bergmann }
1360a33a7d73SArnd Bergmann 
1361a33a7d73SArnd Bergmann static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1362a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1363a33a7d73SArnd Bergmann {
1364a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1365a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1366a33a7d73SArnd Bergmann 	u32 status;
1367a33a7d73SArnd Bergmann 
1368a33a7d73SArnd Bergmann 	if (size != 4)
1369a33a7d73SArnd Bergmann 		goto out;
1370a33a7d73SArnd Bergmann 
1371a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1372a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1373a33a7d73SArnd Bergmann 		status = ctx->ops->read_mfc_tagstatus(ctx);
1374a33a7d73SArnd Bergmann 		if (!(status & ctx->tagwait))
1375a33a7d73SArnd Bergmann 			ret = -EAGAIN;
1376a33a7d73SArnd Bergmann 		else
1377a33a7d73SArnd Bergmann 			ctx->tagwait &= ~status;
1378a33a7d73SArnd Bergmann 	} else {
1379a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1380a33a7d73SArnd Bergmann 			   spufs_read_mfc_tagstatus(ctx, &status));
1381a33a7d73SArnd Bergmann 	}
1382a33a7d73SArnd Bergmann 	spu_release(ctx);
1383a33a7d73SArnd Bergmann 
1384a33a7d73SArnd Bergmann 	if (ret)
1385a33a7d73SArnd Bergmann 		goto out;
1386a33a7d73SArnd Bergmann 
1387a33a7d73SArnd Bergmann 	ret = 4;
1388a33a7d73SArnd Bergmann 	if (copy_to_user(buffer, &status, 4))
1389a33a7d73SArnd Bergmann 		ret = -EFAULT;
1390a33a7d73SArnd Bergmann 
1391a33a7d73SArnd Bergmann out:
1392a33a7d73SArnd Bergmann 	return ret;
1393a33a7d73SArnd Bergmann }
1394a33a7d73SArnd Bergmann 
1395a33a7d73SArnd Bergmann static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1396a33a7d73SArnd Bergmann {
1397a33a7d73SArnd Bergmann 	pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1398a33a7d73SArnd Bergmann 		 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1399a33a7d73SArnd Bergmann 
1400a33a7d73SArnd Bergmann 	switch (cmd->cmd) {
1401a33a7d73SArnd Bergmann 	case MFC_PUT_CMD:
1402a33a7d73SArnd Bergmann 	case MFC_PUTF_CMD:
1403a33a7d73SArnd Bergmann 	case MFC_PUTB_CMD:
1404a33a7d73SArnd Bergmann 	case MFC_GET_CMD:
1405a33a7d73SArnd Bergmann 	case MFC_GETF_CMD:
1406a33a7d73SArnd Bergmann 	case MFC_GETB_CMD:
1407a33a7d73SArnd Bergmann 		break;
1408a33a7d73SArnd Bergmann 	default:
1409a33a7d73SArnd Bergmann 		pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1410a33a7d73SArnd Bergmann 		return -EIO;
1411a33a7d73SArnd Bergmann 	}
1412a33a7d73SArnd Bergmann 
1413a33a7d73SArnd Bergmann 	if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1414a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1415a33a7d73SArnd Bergmann 				cmd->ea, cmd->lsa);
1416a33a7d73SArnd Bergmann 		return -EIO;
1417a33a7d73SArnd Bergmann 	}
1418a33a7d73SArnd Bergmann 
1419a33a7d73SArnd Bergmann 	switch (cmd->size & 0xf) {
1420a33a7d73SArnd Bergmann 	case 1:
1421a33a7d73SArnd Bergmann 		break;
1422a33a7d73SArnd Bergmann 	case 2:
1423a33a7d73SArnd Bergmann 		if (cmd->lsa & 1)
1424a33a7d73SArnd Bergmann 			goto error;
1425a33a7d73SArnd Bergmann 		break;
1426a33a7d73SArnd Bergmann 	case 4:
1427a33a7d73SArnd Bergmann 		if (cmd->lsa & 3)
1428a33a7d73SArnd Bergmann 			goto error;
1429a33a7d73SArnd Bergmann 		break;
1430a33a7d73SArnd Bergmann 	case 8:
1431a33a7d73SArnd Bergmann 		if (cmd->lsa & 7)
1432a33a7d73SArnd Bergmann 			goto error;
1433a33a7d73SArnd Bergmann 		break;
1434a33a7d73SArnd Bergmann 	case 0:
1435a33a7d73SArnd Bergmann 		if (cmd->lsa & 15)
1436a33a7d73SArnd Bergmann 			goto error;
1437a33a7d73SArnd Bergmann 		break;
1438a33a7d73SArnd Bergmann 	error:
1439a33a7d73SArnd Bergmann 	default:
1440a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment %x for size %x\n",
1441a33a7d73SArnd Bergmann 			cmd->lsa & 0xf, cmd->size);
1442a33a7d73SArnd Bergmann 		return -EIO;
1443a33a7d73SArnd Bergmann 	}
1444a33a7d73SArnd Bergmann 
1445a33a7d73SArnd Bergmann 	if (cmd->size > 16 * 1024) {
1446a33a7d73SArnd Bergmann 		pr_debug("invalid DMA size %x\n", cmd->size);
1447a33a7d73SArnd Bergmann 		return -EIO;
1448a33a7d73SArnd Bergmann 	}
1449a33a7d73SArnd Bergmann 
1450a33a7d73SArnd Bergmann 	if (cmd->tag & 0xfff0) {
1451a33a7d73SArnd Bergmann 		/* we reserve the higher tag numbers for kernel use */
1452a33a7d73SArnd Bergmann 		pr_debug("invalid DMA tag\n");
1453a33a7d73SArnd Bergmann 		return -EIO;
1454a33a7d73SArnd Bergmann 	}
1455a33a7d73SArnd Bergmann 
1456a33a7d73SArnd Bergmann 	if (cmd->class) {
1457a33a7d73SArnd Bergmann 		/* not supported in this version */
1458a33a7d73SArnd Bergmann 		pr_debug("invalid DMA class\n");
1459a33a7d73SArnd Bergmann 		return -EIO;
1460a33a7d73SArnd Bergmann 	}
1461a33a7d73SArnd Bergmann 
1462a33a7d73SArnd Bergmann 	return 0;
1463a33a7d73SArnd Bergmann }
1464a33a7d73SArnd Bergmann 
1465a33a7d73SArnd Bergmann static int spu_send_mfc_command(struct spu_context *ctx,
1466a33a7d73SArnd Bergmann 				struct mfc_dma_command cmd,
1467a33a7d73SArnd Bergmann 				int *error)
1468a33a7d73SArnd Bergmann {
1469a33a7d73SArnd Bergmann 	*error = ctx->ops->send_mfc_command(ctx, &cmd);
1470a33a7d73SArnd Bergmann 	if (*error == -EAGAIN) {
1471a33a7d73SArnd Bergmann 		/* wait for any tag group to complete
1472a33a7d73SArnd Bergmann 		   so we have space for the new command */
1473a33a7d73SArnd Bergmann 		ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1474a33a7d73SArnd Bergmann 		/* try again, because the queue might be
1475a33a7d73SArnd Bergmann 		   empty again */
1476a33a7d73SArnd Bergmann 		*error = ctx->ops->send_mfc_command(ctx, &cmd);
1477a33a7d73SArnd Bergmann 		if (*error == -EAGAIN)
1478a33a7d73SArnd Bergmann 			return 0;
1479a33a7d73SArnd Bergmann 	}
1480a33a7d73SArnd Bergmann 	return 1;
1481a33a7d73SArnd Bergmann }
1482a33a7d73SArnd Bergmann 
1483a33a7d73SArnd Bergmann static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1484a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1485a33a7d73SArnd Bergmann {
1486a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1487a33a7d73SArnd Bergmann 	struct mfc_dma_command cmd;
1488a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1489a33a7d73SArnd Bergmann 
1490a33a7d73SArnd Bergmann 	if (size != sizeof cmd)
1491a33a7d73SArnd Bergmann 		goto out;
1492a33a7d73SArnd Bergmann 
1493a33a7d73SArnd Bergmann 	ret = -EFAULT;
1494a33a7d73SArnd Bergmann 	if (copy_from_user(&cmd, buffer, sizeof cmd))
1495a33a7d73SArnd Bergmann 		goto out;
1496a33a7d73SArnd Bergmann 
1497a33a7d73SArnd Bergmann 	ret = spufs_check_valid_dma(&cmd);
1498a33a7d73SArnd Bergmann 	if (ret)
1499a33a7d73SArnd Bergmann 		goto out;
1500a33a7d73SArnd Bergmann 
1501577f8f10SAkinobu Mita 	ret = spu_acquire_runnable(ctx, 0);
1502577f8f10SAkinobu Mita 	if (ret)
1503577f8f10SAkinobu Mita 		goto out;
1504577f8f10SAkinobu Mita 
1505a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1506a33a7d73SArnd Bergmann 		ret = ctx->ops->send_mfc_command(ctx, &cmd);
1507a33a7d73SArnd Bergmann 	} else {
1508a33a7d73SArnd Bergmann 		int status;
1509a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1510a33a7d73SArnd Bergmann 				 spu_send_mfc_command(ctx, cmd, &status));
1511a33a7d73SArnd Bergmann 		if (status)
1512a33a7d73SArnd Bergmann 			ret = status;
1513a33a7d73SArnd Bergmann 	}
1514a33a7d73SArnd Bergmann 
1515a33a7d73SArnd Bergmann 	if (ret)
1516933b0e35SKazunori Asayama 		goto out_unlock;
1517a33a7d73SArnd Bergmann 
1518a33a7d73SArnd Bergmann 	ctx->tagwait |= 1 << cmd.tag;
15193692dc66SMasato Noguchi 	ret = size;
1520a33a7d73SArnd Bergmann 
1521933b0e35SKazunori Asayama out_unlock:
1522933b0e35SKazunori Asayama 	spu_release(ctx);
1523a33a7d73SArnd Bergmann out:
1524a33a7d73SArnd Bergmann 	return ret;
1525a33a7d73SArnd Bergmann }
1526a33a7d73SArnd Bergmann 
1527a33a7d73SArnd Bergmann static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1528a33a7d73SArnd Bergmann {
1529a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1530a33a7d73SArnd Bergmann 	u32 free_elements, tagstatus;
1531a33a7d73SArnd Bergmann 	unsigned int mask;
1532a33a7d73SArnd Bergmann 
1533933b0e35SKazunori Asayama 	poll_wait(file, &ctx->mfc_wq, wait);
1534933b0e35SKazunori Asayama 
1535a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1536a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1537a33a7d73SArnd Bergmann 	free_elements = ctx->ops->get_mfc_free_elements(ctx);
1538a33a7d73SArnd Bergmann 	tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1539a33a7d73SArnd Bergmann 	spu_release(ctx);
1540a33a7d73SArnd Bergmann 
1541a33a7d73SArnd Bergmann 	mask = 0;
1542a33a7d73SArnd Bergmann 	if (free_elements & 0xffff)
1543a33a7d73SArnd Bergmann 		mask |= POLLOUT | POLLWRNORM;
1544a33a7d73SArnd Bergmann 	if (tagstatus & ctx->tagwait)
1545a33a7d73SArnd Bergmann 		mask |= POLLIN | POLLRDNORM;
1546a33a7d73SArnd Bergmann 
1547a33a7d73SArnd Bergmann 	pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1548a33a7d73SArnd Bergmann 		free_elements, tagstatus, ctx->tagwait);
1549a33a7d73SArnd Bergmann 
1550a33a7d73SArnd Bergmann 	return mask;
1551a33a7d73SArnd Bergmann }
1552a33a7d73SArnd Bergmann 
155373b6af8aSAl Viro static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1554a33a7d73SArnd Bergmann {
1555a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1556a33a7d73SArnd Bergmann 	int ret;
1557a33a7d73SArnd Bergmann 
1558a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1559a33a7d73SArnd Bergmann #if 0
1560a33a7d73SArnd Bergmann /* this currently hangs */
1561a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1562a33a7d73SArnd Bergmann 			 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1563a33a7d73SArnd Bergmann 	if (ret)
1564a33a7d73SArnd Bergmann 		goto out;
1565a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1566a33a7d73SArnd Bergmann 			 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1567a33a7d73SArnd Bergmann out:
1568a33a7d73SArnd Bergmann #else
1569a33a7d73SArnd Bergmann 	ret = 0;
1570a33a7d73SArnd Bergmann #endif
1571a33a7d73SArnd Bergmann 	spu_release(ctx);
1572a33a7d73SArnd Bergmann 
1573a33a7d73SArnd Bergmann 	return ret;
1574a33a7d73SArnd Bergmann }
1575a33a7d73SArnd Bergmann 
1576a33a7d73SArnd Bergmann static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1577a33a7d73SArnd Bergmann 			   int datasync)
1578a33a7d73SArnd Bergmann {
157973b6af8aSAl Viro 	return spufs_mfc_flush(file, NULL);
1580a33a7d73SArnd Bergmann }
1581a33a7d73SArnd Bergmann 
1582a33a7d73SArnd Bergmann static int spufs_mfc_fasync(int fd, struct file *file, int on)
1583a33a7d73SArnd Bergmann {
1584a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1585a33a7d73SArnd Bergmann 
1586a33a7d73SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1587a33a7d73SArnd Bergmann }
1588a33a7d73SArnd Bergmann 
15895dfe4c96SArjan van de Ven static const struct file_operations spufs_mfc_fops = {
1590a33a7d73SArnd Bergmann 	.open	 = spufs_mfc_open,
159143c2bbd9SChristoph Hellwig 	.release = spufs_mfc_release,
1592a33a7d73SArnd Bergmann 	.read	 = spufs_mfc_read,
1593a33a7d73SArnd Bergmann 	.write	 = spufs_mfc_write,
1594a33a7d73SArnd Bergmann 	.poll	 = spufs_mfc_poll,
1595a33a7d73SArnd Bergmann 	.flush	 = spufs_mfc_flush,
1596a33a7d73SArnd Bergmann 	.fsync	 = spufs_mfc_fsync,
1597a33a7d73SArnd Bergmann 	.fasync	 = spufs_mfc_fasync,
15986df10a82SMark Nutter 	.mmap	 = spufs_mfc_mmap,
1599a33a7d73SArnd Bergmann };
1600a33a7d73SArnd Bergmann 
160167207b96SArnd Bergmann static void spufs_npc_set(void *data, u64 val)
160267207b96SArnd Bergmann {
160367207b96SArnd Bergmann 	struct spu_context *ctx = data;
16048b3d6663SArnd Bergmann 	spu_acquire(ctx);
16058b3d6663SArnd Bergmann 	ctx->ops->npc_write(ctx, val);
16068b3d6663SArnd Bergmann 	spu_release(ctx);
160767207b96SArnd Bergmann }
160867207b96SArnd Bergmann 
160978810ff6SMichael Ellerman static u64 __spufs_npc_get(struct spu_context *ctx)
161078810ff6SMichael Ellerman {
161178810ff6SMichael Ellerman 	return ctx->ops->npc_read(ctx);
161278810ff6SMichael Ellerman }
161378810ff6SMichael Ellerman 
161467207b96SArnd Bergmann static u64 spufs_npc_get(void *data)
161567207b96SArnd Bergmann {
161667207b96SArnd Bergmann 	struct spu_context *ctx = data;
161767207b96SArnd Bergmann 	u64 ret;
16188b3d6663SArnd Bergmann 	spu_acquire(ctx);
161978810ff6SMichael Ellerman 	ret = __spufs_npc_get(ctx);
16208b3d6663SArnd Bergmann 	spu_release(ctx);
162167207b96SArnd Bergmann 	return ret;
162267207b96SArnd Bergmann }
16239b5047e2SDwayne Grant McConnell DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
16249b5047e2SDwayne Grant McConnell 			"0x%llx\n")
162567207b96SArnd Bergmann 
16268b3d6663SArnd Bergmann static void spufs_decr_set(void *data, u64 val)
16278b3d6663SArnd Bergmann {
16288b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16298b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
16308b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
16318b3d6663SArnd Bergmann 	lscsa->decr.slot[0] = (u32) val;
163227b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
16338b3d6663SArnd Bergmann }
16348b3d6663SArnd Bergmann 
163574de08bcSMichael Ellerman static u64 __spufs_decr_get(struct spu_context *ctx)
16368b3d6663SArnd Bergmann {
16378b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1638bf1ab978SDwayne Grant McConnell 	return lscsa->decr.slot[0];
1639bf1ab978SDwayne Grant McConnell }
1640bf1ab978SDwayne Grant McConnell 
1641bf1ab978SDwayne Grant McConnell static u64 spufs_decr_get(void *data)
1642bf1ab978SDwayne Grant McConnell {
1643bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
16448b3d6663SArnd Bergmann 	u64 ret;
16458b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
164674de08bcSMichael Ellerman 	ret = __spufs_decr_get(ctx);
164727b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
16488b3d6663SArnd Bergmann 	return ret;
16498b3d6663SArnd Bergmann }
16508b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
16519b5047e2SDwayne Grant McConnell 			"0x%llx\n")
16528b3d6663SArnd Bergmann 
16538b3d6663SArnd Bergmann static void spufs_decr_status_set(void *data, u64 val)
16548b3d6663SArnd Bergmann {
16558b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16568b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
1657d40a01d4SMasato Noguchi 	if (val)
1658d40a01d4SMasato Noguchi 		ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1659d40a01d4SMasato Noguchi 	else
1660d40a01d4SMasato Noguchi 		ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
166127b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
16628b3d6663SArnd Bergmann }
16638b3d6663SArnd Bergmann 
166474de08bcSMichael Ellerman static u64 __spufs_decr_status_get(struct spu_context *ctx)
16658b3d6663SArnd Bergmann {
1666d40a01d4SMasato Noguchi 	if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1667d40a01d4SMasato Noguchi 		return SPU_DECR_STATUS_RUNNING;
1668d40a01d4SMasato Noguchi 	else
1669d40a01d4SMasato Noguchi 		return 0;
1670bf1ab978SDwayne Grant McConnell }
1671bf1ab978SDwayne Grant McConnell 
1672bf1ab978SDwayne Grant McConnell static u64 spufs_decr_status_get(void *data)
1673bf1ab978SDwayne Grant McConnell {
1674bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
16758b3d6663SArnd Bergmann 	u64 ret;
16768b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
167774de08bcSMichael Ellerman 	ret = __spufs_decr_status_get(ctx);
167827b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
16798b3d6663SArnd Bergmann 	return ret;
16808b3d6663SArnd Bergmann }
16818b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
16829b5047e2SDwayne Grant McConnell 			spufs_decr_status_set, "0x%llx\n")
16838b3d6663SArnd Bergmann 
16848b3d6663SArnd Bergmann static void spufs_event_mask_set(void *data, u64 val)
16858b3d6663SArnd Bergmann {
16868b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
16878b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
16888b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
16898b3d6663SArnd Bergmann 	lscsa->event_mask.slot[0] = (u32) val;
169027b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
16918b3d6663SArnd Bergmann }
16928b3d6663SArnd Bergmann 
169374de08bcSMichael Ellerman static u64 __spufs_event_mask_get(struct spu_context *ctx)
16948b3d6663SArnd Bergmann {
16958b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1696bf1ab978SDwayne Grant McConnell 	return lscsa->event_mask.slot[0];
1697bf1ab978SDwayne Grant McConnell }
1698bf1ab978SDwayne Grant McConnell 
1699bf1ab978SDwayne Grant McConnell static u64 spufs_event_mask_get(void *data)
1700bf1ab978SDwayne Grant McConnell {
1701bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
17028b3d6663SArnd Bergmann 	u64 ret;
17038b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
170474de08bcSMichael Ellerman 	ret = __spufs_event_mask_get(ctx);
170527b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
17068b3d6663SArnd Bergmann 	return ret;
17078b3d6663SArnd Bergmann }
17088b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
17099b5047e2SDwayne Grant McConnell 			spufs_event_mask_set, "0x%llx\n")
17108b3d6663SArnd Bergmann 
171174de08bcSMichael Ellerman static u64 __spufs_event_status_get(struct spu_context *ctx)
1712b9e3bd77SDwayne Grant McConnell {
1713b9e3bd77SDwayne Grant McConnell 	struct spu_state *state = &ctx->csa;
1714b9e3bd77SDwayne Grant McConnell 	u64 stat;
1715b9e3bd77SDwayne Grant McConnell 	stat = state->spu_chnlcnt_RW[0];
1716b9e3bd77SDwayne Grant McConnell 	if (stat)
1717bf1ab978SDwayne Grant McConnell 		return state->spu_chnldata_RW[0];
1718bf1ab978SDwayne Grant McConnell 	return 0;
1719bf1ab978SDwayne Grant McConnell }
1720bf1ab978SDwayne Grant McConnell 
1721bf1ab978SDwayne Grant McConnell static u64 spufs_event_status_get(void *data)
1722bf1ab978SDwayne Grant McConnell {
1723bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = data;
1724bf1ab978SDwayne Grant McConnell 	u64 ret = 0;
1725bf1ab978SDwayne Grant McConnell 
1726bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
172774de08bcSMichael Ellerman 	ret = __spufs_event_status_get(ctx);
172827b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1729b9e3bd77SDwayne Grant McConnell 	return ret;
1730b9e3bd77SDwayne Grant McConnell }
1731b9e3bd77SDwayne Grant McConnell DEFINE_SIMPLE_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1732b9e3bd77SDwayne Grant McConnell 			NULL, "0x%llx\n")
1733b9e3bd77SDwayne Grant McConnell 
17348b3d6663SArnd Bergmann static void spufs_srr0_set(void *data, u64 val)
17358b3d6663SArnd Bergmann {
17368b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
17378b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
17388b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
17398b3d6663SArnd Bergmann 	lscsa->srr0.slot[0] = (u32) val;
174027b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
17418b3d6663SArnd Bergmann }
17428b3d6663SArnd Bergmann 
17438b3d6663SArnd Bergmann static u64 spufs_srr0_get(void *data)
17448b3d6663SArnd Bergmann {
17458b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
17468b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
17478b3d6663SArnd Bergmann 	u64 ret;
17488b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
17498b3d6663SArnd Bergmann 	ret = lscsa->srr0.slot[0];
175027b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
17518b3d6663SArnd Bergmann 	return ret;
17528b3d6663SArnd Bergmann }
17538b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
17549b5047e2SDwayne Grant McConnell 			"0x%llx\n")
17558b3d6663SArnd Bergmann 
17567b1a7014Sarnd@arndb.de static u64 spufs_id_get(void *data)
17577b1a7014Sarnd@arndb.de {
17587b1a7014Sarnd@arndb.de 	struct spu_context *ctx = data;
17597b1a7014Sarnd@arndb.de 	u64 num;
17607b1a7014Sarnd@arndb.de 
17617b1a7014Sarnd@arndb.de 	spu_acquire(ctx);
17627b1a7014Sarnd@arndb.de 	if (ctx->state == SPU_STATE_RUNNABLE)
17637b1a7014Sarnd@arndb.de 		num = ctx->spu->number;
17647b1a7014Sarnd@arndb.de 	else
17657b1a7014Sarnd@arndb.de 		num = (unsigned int)-1;
17667b1a7014Sarnd@arndb.de 	spu_release(ctx);
17677b1a7014Sarnd@arndb.de 
17687b1a7014Sarnd@arndb.de 	return num;
17697b1a7014Sarnd@arndb.de }
1770e45d6634SAl Viro DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
17717b1a7014Sarnd@arndb.de 
177274de08bcSMichael Ellerman static u64 __spufs_object_id_get(struct spu_context *ctx)
177386767277SArnd Bergmann {
177486767277SArnd Bergmann 	return ctx->object_id;
177586767277SArnd Bergmann }
177686767277SArnd Bergmann 
1777bf1ab978SDwayne Grant McConnell static u64 spufs_object_id_get(void *data)
1778bf1ab978SDwayne Grant McConnell {
1779bf1ab978SDwayne Grant McConnell 	/* FIXME: Should there really be no locking here? */
178074de08bcSMichael Ellerman 	return __spufs_object_id_get((struct spu_context *)data);
1781bf1ab978SDwayne Grant McConnell }
1782bf1ab978SDwayne Grant McConnell 
178386767277SArnd Bergmann static void spufs_object_id_set(void *data, u64 id)
178486767277SArnd Bergmann {
178586767277SArnd Bergmann 	struct spu_context *ctx = data;
178686767277SArnd Bergmann 	ctx->object_id = id;
178786767277SArnd Bergmann }
178886767277SArnd Bergmann 
178986767277SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
179086767277SArnd Bergmann 		spufs_object_id_set, "0x%llx\n");
179186767277SArnd Bergmann 
179274de08bcSMichael Ellerman static u64 __spufs_lslr_get(struct spu_context *ctx)
1793bf1ab978SDwayne Grant McConnell {
1794bf1ab978SDwayne Grant McConnell 	return ctx->csa.priv2.spu_lslr_RW;
1795bf1ab978SDwayne Grant McConnell }
1796bf1ab978SDwayne Grant McConnell 
1797b9e3bd77SDwayne Grant McConnell static u64 spufs_lslr_get(void *data)
1798b9e3bd77SDwayne Grant McConnell {
1799b9e3bd77SDwayne Grant McConnell 	struct spu_context *ctx = data;
1800b9e3bd77SDwayne Grant McConnell 	u64 ret;
1801b9e3bd77SDwayne Grant McConnell 
1802b9e3bd77SDwayne Grant McConnell 	spu_acquire_saved(ctx);
180374de08bcSMichael Ellerman 	ret = __spufs_lslr_get(ctx);
180427b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1805b9e3bd77SDwayne Grant McConnell 
1806b9e3bd77SDwayne Grant McConnell 	return ret;
1807b9e3bd77SDwayne Grant McConnell }
1808b9e3bd77SDwayne Grant McConnell DEFINE_SIMPLE_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n")
1809b9e3bd77SDwayne Grant McConnell 
1810b9e3bd77SDwayne Grant McConnell static int spufs_info_open(struct inode *inode, struct file *file)
1811b9e3bd77SDwayne Grant McConnell {
1812b9e3bd77SDwayne Grant McConnell 	struct spufs_inode_info *i = SPUFS_I(inode);
1813b9e3bd77SDwayne Grant McConnell 	struct spu_context *ctx = i->i_ctx;
1814b9e3bd77SDwayne Grant McConnell 	file->private_data = ctx;
1815b9e3bd77SDwayne Grant McConnell 	return 0;
1816b9e3bd77SDwayne Grant McConnell }
1817b9e3bd77SDwayne Grant McConnell 
1818cbe709c1SBenjamin Herrenschmidt static int spufs_caps_show(struct seq_file *s, void *private)
1819cbe709c1SBenjamin Herrenschmidt {
1820cbe709c1SBenjamin Herrenschmidt 	struct spu_context *ctx = s->private;
1821cbe709c1SBenjamin Herrenschmidt 
1822cbe709c1SBenjamin Herrenschmidt 	if (!(ctx->flags & SPU_CREATE_NOSCHED))
1823cbe709c1SBenjamin Herrenschmidt 		seq_puts(s, "sched\n");
1824cbe709c1SBenjamin Herrenschmidt 	if (!(ctx->flags & SPU_CREATE_ISOLATE))
1825cbe709c1SBenjamin Herrenschmidt 		seq_puts(s, "step\n");
1826cbe709c1SBenjamin Herrenschmidt 	return 0;
1827cbe709c1SBenjamin Herrenschmidt }
1828cbe709c1SBenjamin Herrenschmidt 
1829cbe709c1SBenjamin Herrenschmidt static int spufs_caps_open(struct inode *inode, struct file *file)
1830cbe709c1SBenjamin Herrenschmidt {
1831cbe709c1SBenjamin Herrenschmidt 	return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
1832cbe709c1SBenjamin Herrenschmidt }
1833cbe709c1SBenjamin Herrenschmidt 
1834cbe709c1SBenjamin Herrenschmidt static const struct file_operations spufs_caps_fops = {
1835cbe709c1SBenjamin Herrenschmidt 	.open		= spufs_caps_open,
1836cbe709c1SBenjamin Herrenschmidt 	.read		= seq_read,
1837cbe709c1SBenjamin Herrenschmidt 	.llseek		= seq_lseek,
1838cbe709c1SBenjamin Herrenschmidt 	.release	= single_release,
1839cbe709c1SBenjamin Herrenschmidt };
1840cbe709c1SBenjamin Herrenschmidt 
1841bf1ab978SDwayne Grant McConnell static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
1842bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
1843bf1ab978SDwayne Grant McConnell {
1844bf1ab978SDwayne Grant McConnell 	u32 mbox_stat;
1845bf1ab978SDwayne Grant McConnell 	u32 data;
1846bf1ab978SDwayne Grant McConnell 
1847bf1ab978SDwayne Grant McConnell 	mbox_stat = ctx->csa.prob.mb_stat_R;
1848bf1ab978SDwayne Grant McConnell 	if (mbox_stat & 0x0000ff) {
1849bf1ab978SDwayne Grant McConnell 		data = ctx->csa.prob.pu_mb_R;
1850bf1ab978SDwayne Grant McConnell 	}
1851bf1ab978SDwayne Grant McConnell 
1852bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1853bf1ab978SDwayne Grant McConnell }
1854bf1ab978SDwayne Grant McConnell 
185569a2f00cSDwayne Grant McConnell static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
185669a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
185769a2f00cSDwayne Grant McConnell {
1858bf1ab978SDwayne Grant McConnell 	int ret;
185969a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
186069a2f00cSDwayne Grant McConnell 
186169a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
186269a2f00cSDwayne Grant McConnell 		return -EFAULT;
186369a2f00cSDwayne Grant McConnell 
186469a2f00cSDwayne Grant McConnell 	spu_acquire_saved(ctx);
186569a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
1866bf1ab978SDwayne Grant McConnell 	ret = __spufs_mbox_info_read(ctx, buf, len, pos);
186769a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
186827b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
186969a2f00cSDwayne Grant McConnell 
1870bf1ab978SDwayne Grant McConnell 	return ret;
187169a2f00cSDwayne Grant McConnell }
187269a2f00cSDwayne Grant McConnell 
18735dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_info_fops = {
187469a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
187569a2f00cSDwayne Grant McConnell 	.read = spufs_mbox_info_read,
187669a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
187769a2f00cSDwayne Grant McConnell };
187869a2f00cSDwayne Grant McConnell 
1879bf1ab978SDwayne Grant McConnell static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
1880bf1ab978SDwayne Grant McConnell 				char __user *buf, size_t len, loff_t *pos)
1881bf1ab978SDwayne Grant McConnell {
1882bf1ab978SDwayne Grant McConnell 	u32 ibox_stat;
1883bf1ab978SDwayne Grant McConnell 	u32 data;
1884bf1ab978SDwayne Grant McConnell 
1885bf1ab978SDwayne Grant McConnell 	ibox_stat = ctx->csa.prob.mb_stat_R;
1886bf1ab978SDwayne Grant McConnell 	if (ibox_stat & 0xff0000) {
1887bf1ab978SDwayne Grant McConnell 		data = ctx->csa.priv2.puint_mb_R;
1888bf1ab978SDwayne Grant McConnell 	}
1889bf1ab978SDwayne Grant McConnell 
1890bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
1891bf1ab978SDwayne Grant McConnell }
1892bf1ab978SDwayne Grant McConnell 
189369a2f00cSDwayne Grant McConnell static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
189469a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
189569a2f00cSDwayne Grant McConnell {
189669a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1897bf1ab978SDwayne Grant McConnell 	int ret;
189869a2f00cSDwayne Grant McConnell 
189969a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
190069a2f00cSDwayne Grant McConnell 		return -EFAULT;
190169a2f00cSDwayne Grant McConnell 
190269a2f00cSDwayne Grant McConnell 	spu_acquire_saved(ctx);
190369a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
1904bf1ab978SDwayne Grant McConnell 	ret = __spufs_ibox_info_read(ctx, buf, len, pos);
190569a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
190627b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
190769a2f00cSDwayne Grant McConnell 
1908bf1ab978SDwayne Grant McConnell 	return ret;
190969a2f00cSDwayne Grant McConnell }
191069a2f00cSDwayne Grant McConnell 
19115dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_info_fops = {
191269a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
191369a2f00cSDwayne Grant McConnell 	.read = spufs_ibox_info_read,
191469a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
191569a2f00cSDwayne Grant McConnell };
191669a2f00cSDwayne Grant McConnell 
1917bf1ab978SDwayne Grant McConnell static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
1918bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
1919bf1ab978SDwayne Grant McConnell {
1920bf1ab978SDwayne Grant McConnell 	int i, cnt;
1921bf1ab978SDwayne Grant McConnell 	u32 data[4];
1922bf1ab978SDwayne Grant McConnell 	u32 wbox_stat;
1923bf1ab978SDwayne Grant McConnell 
1924bf1ab978SDwayne Grant McConnell 	wbox_stat = ctx->csa.prob.mb_stat_R;
1925bf1ab978SDwayne Grant McConnell 	cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
1926bf1ab978SDwayne Grant McConnell 	for (i = 0; i < cnt; i++) {
1927bf1ab978SDwayne Grant McConnell 		data[i] = ctx->csa.spu_mailbox_data[i];
1928bf1ab978SDwayne Grant McConnell 	}
1929bf1ab978SDwayne Grant McConnell 
1930bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data,
1931bf1ab978SDwayne Grant McConnell 				cnt * sizeof(u32));
1932bf1ab978SDwayne Grant McConnell }
1933bf1ab978SDwayne Grant McConnell 
193469a2f00cSDwayne Grant McConnell static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
193569a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
193669a2f00cSDwayne Grant McConnell {
193769a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1938bf1ab978SDwayne Grant McConnell 	int ret;
193969a2f00cSDwayne Grant McConnell 
194069a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
194169a2f00cSDwayne Grant McConnell 		return -EFAULT;
194269a2f00cSDwayne Grant McConnell 
194369a2f00cSDwayne Grant McConnell 	spu_acquire_saved(ctx);
194469a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
1945bf1ab978SDwayne Grant McConnell 	ret = __spufs_wbox_info_read(ctx, buf, len, pos);
194669a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
194727b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
194869a2f00cSDwayne Grant McConnell 
1949bf1ab978SDwayne Grant McConnell 	return ret;
195069a2f00cSDwayne Grant McConnell }
195169a2f00cSDwayne Grant McConnell 
19525dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_info_fops = {
195369a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
195469a2f00cSDwayne Grant McConnell 	.read = spufs_wbox_info_read,
195569a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
195669a2f00cSDwayne Grant McConnell };
195769a2f00cSDwayne Grant McConnell 
1958bf1ab978SDwayne Grant McConnell static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
1959bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
1960b9e3bd77SDwayne Grant McConnell {
1961b9e3bd77SDwayne Grant McConnell 	struct spu_dma_info info;
1962b9e3bd77SDwayne Grant McConnell 	struct mfc_cq_sr *qp, *spuqp;
1963b9e3bd77SDwayne Grant McConnell 	int i;
1964b9e3bd77SDwayne Grant McConnell 
1965b9e3bd77SDwayne Grant McConnell 	info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
1966b9e3bd77SDwayne Grant McConnell 	info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
1967b9e3bd77SDwayne Grant McConnell 	info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
1968b9e3bd77SDwayne Grant McConnell 	info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
1969b9e3bd77SDwayne Grant McConnell 	info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
1970b9e3bd77SDwayne Grant McConnell 	for (i = 0; i < 16; i++) {
1971b9e3bd77SDwayne Grant McConnell 		qp = &info.dma_info_command_data[i];
1972b9e3bd77SDwayne Grant McConnell 		spuqp = &ctx->csa.priv2.spuq[i];
1973b9e3bd77SDwayne Grant McConnell 
1974b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
1975b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
1976b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
1977b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
1978b9e3bd77SDwayne Grant McConnell 	}
1979b9e3bd77SDwayne Grant McConnell 
1980b9e3bd77SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &info,
1981b9e3bd77SDwayne Grant McConnell 				sizeof info);
1982b9e3bd77SDwayne Grant McConnell }
1983b9e3bd77SDwayne Grant McConnell 
1984bf1ab978SDwayne Grant McConnell static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
1985bf1ab978SDwayne Grant McConnell 			      size_t len, loff_t *pos)
1986bf1ab978SDwayne Grant McConnell {
1987bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1988bf1ab978SDwayne Grant McConnell 	int ret;
1989bf1ab978SDwayne Grant McConnell 
1990bf1ab978SDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
1991bf1ab978SDwayne Grant McConnell 		return -EFAULT;
1992bf1ab978SDwayne Grant McConnell 
1993bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
1994bf1ab978SDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
1995bf1ab978SDwayne Grant McConnell 	ret = __spufs_dma_info_read(ctx, buf, len, pos);
1996bf1ab978SDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
199727b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1998bf1ab978SDwayne Grant McConnell 
1999bf1ab978SDwayne Grant McConnell 	return ret;
2000bf1ab978SDwayne Grant McConnell }
2001bf1ab978SDwayne Grant McConnell 
20025dfe4c96SArjan van de Ven static const struct file_operations spufs_dma_info_fops = {
2003b9e3bd77SDwayne Grant McConnell 	.open = spufs_info_open,
2004b9e3bd77SDwayne Grant McConnell 	.read = spufs_dma_info_read,
2005b9e3bd77SDwayne Grant McConnell };
2006b9e3bd77SDwayne Grant McConnell 
2007bf1ab978SDwayne Grant McConnell static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2008bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2009b9e3bd77SDwayne Grant McConnell {
2010b9e3bd77SDwayne Grant McConnell 	struct spu_proxydma_info info;
2011b9e3bd77SDwayne Grant McConnell 	struct mfc_cq_sr *qp, *puqp;
2012bf1ab978SDwayne Grant McConnell 	int ret = sizeof info;
2013b9e3bd77SDwayne Grant McConnell 	int i;
2014b9e3bd77SDwayne Grant McConnell 
2015b9e3bd77SDwayne Grant McConnell 	if (len < ret)
2016b9e3bd77SDwayne Grant McConnell 		return -EINVAL;
2017b9e3bd77SDwayne Grant McConnell 
2018b9e3bd77SDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
2019b9e3bd77SDwayne Grant McConnell 		return -EFAULT;
2020b9e3bd77SDwayne Grant McConnell 
2021b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2022b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2023b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2024b9e3bd77SDwayne Grant McConnell 	for (i = 0; i < 8; i++) {
2025b9e3bd77SDwayne Grant McConnell 		qp = &info.proxydma_info_command_data[i];
2026b9e3bd77SDwayne Grant McConnell 		puqp = &ctx->csa.priv2.puq[i];
2027b9e3bd77SDwayne Grant McConnell 
2028b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2029b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2030b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2031b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2032b9e3bd77SDwayne Grant McConnell 	}
2033bf1ab978SDwayne Grant McConnell 
2034bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &info,
2035bf1ab978SDwayne Grant McConnell 				sizeof info);
2036bf1ab978SDwayne Grant McConnell }
2037bf1ab978SDwayne Grant McConnell 
2038bf1ab978SDwayne Grant McConnell static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2039bf1ab978SDwayne Grant McConnell 				   size_t len, loff_t *pos)
2040bf1ab978SDwayne Grant McConnell {
2041bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2042bf1ab978SDwayne Grant McConnell 	int ret;
2043bf1ab978SDwayne Grant McConnell 
2044bf1ab978SDwayne Grant McConnell 	spu_acquire_saved(ctx);
2045bf1ab978SDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2046bf1ab978SDwayne Grant McConnell 	ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2047b9e3bd77SDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
204827b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
2049b9e3bd77SDwayne Grant McConnell 
2050b9e3bd77SDwayne Grant McConnell 	return ret;
2051b9e3bd77SDwayne Grant McConnell }
2052b9e3bd77SDwayne Grant McConnell 
20535dfe4c96SArjan van de Ven static const struct file_operations spufs_proxydma_info_fops = {
2054b9e3bd77SDwayne Grant McConnell 	.open = spufs_info_open,
2055b9e3bd77SDwayne Grant McConnell 	.read = spufs_proxydma_info_read,
2056b9e3bd77SDwayne Grant McConnell };
2057b9e3bd77SDwayne Grant McConnell 
2058476273adSChristoph Hellwig static int spufs_show_tid(struct seq_file *s, void *private)
2059476273adSChristoph Hellwig {
2060476273adSChristoph Hellwig 	struct spu_context *ctx = s->private;
2061476273adSChristoph Hellwig 
2062476273adSChristoph Hellwig 	seq_printf(s, "%d\n", ctx->tid);
2063476273adSChristoph Hellwig 	return 0;
2064476273adSChristoph Hellwig }
2065476273adSChristoph Hellwig 
2066476273adSChristoph Hellwig static int spufs_tid_open(struct inode *inode, struct file *file)
2067476273adSChristoph Hellwig {
2068476273adSChristoph Hellwig 	return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2069476273adSChristoph Hellwig }
2070476273adSChristoph Hellwig 
2071476273adSChristoph Hellwig static const struct file_operations spufs_tid_fops = {
2072476273adSChristoph Hellwig 	.open		= spufs_tid_open,
2073476273adSChristoph Hellwig 	.read		= seq_read,
2074476273adSChristoph Hellwig 	.llseek		= seq_lseek,
2075476273adSChristoph Hellwig 	.release	= single_release,
2076476273adSChristoph Hellwig };
2077476273adSChristoph Hellwig 
2078e9f8a0b6SChristoph Hellwig static const char *ctx_state_names[] = {
2079e9f8a0b6SChristoph Hellwig 	"user", "system", "iowait", "loaded"
2080e9f8a0b6SChristoph Hellwig };
2081e9f8a0b6SChristoph Hellwig 
2082e9f8a0b6SChristoph Hellwig static unsigned long long spufs_acct_time(struct spu_context *ctx,
208327ec41d3SAndre Detsch 		enum spu_utilization_state state)
2084e9f8a0b6SChristoph Hellwig {
208527ec41d3SAndre Detsch 	struct timespec ts;
208627ec41d3SAndre Detsch 	unsigned long long time = ctx->stats.times[state];
2087e9f8a0b6SChristoph Hellwig 
208827ec41d3SAndre Detsch 	/*
208927ec41d3SAndre Detsch 	 * In general, utilization statistics are updated by the controlling
209027ec41d3SAndre Detsch 	 * thread as the spu context moves through various well defined
209127ec41d3SAndre Detsch 	 * state transitions, but if the context is lazily loaded its
209227ec41d3SAndre Detsch 	 * utilization statistics are not updated as the controlling thread
209327ec41d3SAndre Detsch 	 * is not tightly coupled with the execution of the spu context.  We
209427ec41d3SAndre Detsch 	 * calculate and apply the time delta from the last recorded state
209527ec41d3SAndre Detsch 	 * of the spu context.
209627ec41d3SAndre Detsch 	 */
209727ec41d3SAndre Detsch 	if (ctx->spu && ctx->stats.util_state == state) {
209827ec41d3SAndre Detsch 		ktime_get_ts(&ts);
209927ec41d3SAndre Detsch 		time += timespec_to_ns(&ts) - ctx->stats.tstamp;
210027ec41d3SAndre Detsch 	}
2101e9f8a0b6SChristoph Hellwig 
210227ec41d3SAndre Detsch 	return time / NSEC_PER_MSEC;
2103e9f8a0b6SChristoph Hellwig }
2104e9f8a0b6SChristoph Hellwig 
2105e9f8a0b6SChristoph Hellwig static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2106e9f8a0b6SChristoph Hellwig {
2107e9f8a0b6SChristoph Hellwig 	unsigned long long slb_flts = ctx->stats.slb_flt;
2108e9f8a0b6SChristoph Hellwig 
2109e9f8a0b6SChristoph Hellwig 	if (ctx->state == SPU_STATE_RUNNABLE) {
2110e9f8a0b6SChristoph Hellwig 		slb_flts += (ctx->spu->stats.slb_flt -
2111e9f8a0b6SChristoph Hellwig 			     ctx->stats.slb_flt_base);
2112e9f8a0b6SChristoph Hellwig 	}
2113e9f8a0b6SChristoph Hellwig 
2114e9f8a0b6SChristoph Hellwig 	return slb_flts;
2115e9f8a0b6SChristoph Hellwig }
2116e9f8a0b6SChristoph Hellwig 
2117e9f8a0b6SChristoph Hellwig static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2118e9f8a0b6SChristoph Hellwig {
2119e9f8a0b6SChristoph Hellwig 	unsigned long long class2_intrs = ctx->stats.class2_intr;
2120e9f8a0b6SChristoph Hellwig 
2121e9f8a0b6SChristoph Hellwig 	if (ctx->state == SPU_STATE_RUNNABLE) {
2122e9f8a0b6SChristoph Hellwig 		class2_intrs += (ctx->spu->stats.class2_intr -
2123e9f8a0b6SChristoph Hellwig 				 ctx->stats.class2_intr_base);
2124e9f8a0b6SChristoph Hellwig 	}
2125e9f8a0b6SChristoph Hellwig 
2126e9f8a0b6SChristoph Hellwig 	return class2_intrs;
2127e9f8a0b6SChristoph Hellwig }
2128e9f8a0b6SChristoph Hellwig 
2129e9f8a0b6SChristoph Hellwig 
2130e9f8a0b6SChristoph Hellwig static int spufs_show_stat(struct seq_file *s, void *private)
2131e9f8a0b6SChristoph Hellwig {
2132e9f8a0b6SChristoph Hellwig 	struct spu_context *ctx = s->private;
2133e9f8a0b6SChristoph Hellwig 
2134e9f8a0b6SChristoph Hellwig 	spu_acquire(ctx);
2135e9f8a0b6SChristoph Hellwig 	seq_printf(s, "%s %llu %llu %llu %llu "
2136e9f8a0b6SChristoph Hellwig 		      "%llu %llu %llu %llu %llu %llu %llu %llu\n",
213727ec41d3SAndre Detsch 		ctx_state_names[ctx->stats.util_state],
213827ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_USER),
213927ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
214027ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
214127ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2142e9f8a0b6SChristoph Hellwig 		ctx->stats.vol_ctx_switch,
2143e9f8a0b6SChristoph Hellwig 		ctx->stats.invol_ctx_switch,
2144e9f8a0b6SChristoph Hellwig 		spufs_slb_flts(ctx),
2145e9f8a0b6SChristoph Hellwig 		ctx->stats.hash_flt,
2146e9f8a0b6SChristoph Hellwig 		ctx->stats.min_flt,
2147e9f8a0b6SChristoph Hellwig 		ctx->stats.maj_flt,
2148e9f8a0b6SChristoph Hellwig 		spufs_class2_intrs(ctx),
2149e9f8a0b6SChristoph Hellwig 		ctx->stats.libassist);
2150e9f8a0b6SChristoph Hellwig 	spu_release(ctx);
2151e9f8a0b6SChristoph Hellwig 	return 0;
2152e9f8a0b6SChristoph Hellwig }
2153e9f8a0b6SChristoph Hellwig 
2154e9f8a0b6SChristoph Hellwig static int spufs_stat_open(struct inode *inode, struct file *file)
2155e9f8a0b6SChristoph Hellwig {
2156e9f8a0b6SChristoph Hellwig 	return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2157e9f8a0b6SChristoph Hellwig }
2158e9f8a0b6SChristoph Hellwig 
2159e9f8a0b6SChristoph Hellwig static const struct file_operations spufs_stat_fops = {
2160e9f8a0b6SChristoph Hellwig 	.open		= spufs_stat_open,
2161e9f8a0b6SChristoph Hellwig 	.read		= seq_read,
2162e9f8a0b6SChristoph Hellwig 	.llseek		= seq_lseek,
2163e9f8a0b6SChristoph Hellwig 	.release	= single_release,
2164e9f8a0b6SChristoph Hellwig };
2165e9f8a0b6SChristoph Hellwig 
2166e9f8a0b6SChristoph Hellwig 
216767207b96SArnd Bergmann struct tree_descr spufs_dir_contents[] = {
2168cbe709c1SBenjamin Herrenschmidt 	{ "capabilities", &spufs_caps_fops, 0444, },
216967207b96SArnd Bergmann 	{ "mem",  &spufs_mem_fops,  0666, },
21708b3d6663SArnd Bergmann 	{ "regs", &spufs_regs_fops,  0666, },
217167207b96SArnd Bergmann 	{ "mbox", &spufs_mbox_fops, 0444, },
217267207b96SArnd Bergmann 	{ "ibox", &spufs_ibox_fops, 0444, },
217367207b96SArnd Bergmann 	{ "wbox", &spufs_wbox_fops, 0222, },
217467207b96SArnd Bergmann 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, },
217567207b96SArnd Bergmann 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, },
217667207b96SArnd Bergmann 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, },
21778b6f50efSJeremy Kerr 	{ "signal1", &spufs_signal1_nosched_fops, 0222, },
21788b6f50efSJeremy Kerr 	{ "signal2", &spufs_signal2_nosched_fops, 0222, },
217967207b96SArnd Bergmann 	{ "signal1_type", &spufs_signal1_type, 0666, },
218067207b96SArnd Bergmann 	{ "signal2_type", &spufs_signal2_type, 0666, },
21816df10a82SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
21828b3d6663SArnd Bergmann 	{ "fpcr", &spufs_fpcr_fops, 0666, },
2183b9e3bd77SDwayne Grant McConnell 	{ "lslr", &spufs_lslr_ops, 0444, },
2184b9e3bd77SDwayne Grant McConnell 	{ "mfc", &spufs_mfc_fops, 0666, },
2185b9e3bd77SDwayne Grant McConnell 	{ "mss", &spufs_mss_fops, 0666, },
2186b9e3bd77SDwayne Grant McConnell 	{ "npc", &spufs_npc_ops, 0666, },
2187b9e3bd77SDwayne Grant McConnell 	{ "srr0", &spufs_srr0_ops, 0666, },
21888b3d6663SArnd Bergmann 	{ "decr", &spufs_decr_ops, 0666, },
21898b3d6663SArnd Bergmann 	{ "decr_status", &spufs_decr_status_ops, 0666, },
21908b3d6663SArnd Bergmann 	{ "event_mask", &spufs_event_mask_ops, 0666, },
2191b9e3bd77SDwayne Grant McConnell 	{ "event_status", &spufs_event_status_ops, 0444, },
219227d5bf2aSBenjamin Herrenschmidt 	{ "psmap", &spufs_psmap_fops, 0666, },
219386767277SArnd Bergmann 	{ "phys-id", &spufs_id_ops, 0666, },
219486767277SArnd Bergmann 	{ "object-id", &spufs_object_id_ops, 0666, },
219569a2f00cSDwayne Grant McConnell 	{ "mbox_info", &spufs_mbox_info_fops, 0444, },
219669a2f00cSDwayne Grant McConnell 	{ "ibox_info", &spufs_ibox_info_fops, 0444, },
219769a2f00cSDwayne Grant McConnell 	{ "wbox_info", &spufs_wbox_info_fops, 0444, },
2198b9e3bd77SDwayne Grant McConnell 	{ "dma_info", &spufs_dma_info_fops, 0444, },
2199b9e3bd77SDwayne Grant McConnell 	{ "proxydma_info", &spufs_proxydma_info_fops, 0444, },
2200476273adSChristoph Hellwig 	{ "tid", &spufs_tid_fops, 0444, },
2201e9f8a0b6SChristoph Hellwig 	{ "stat", &spufs_stat_fops, 0444, },
220267207b96SArnd Bergmann 	{},
220367207b96SArnd Bergmann };
22045737edd1SMark Nutter 
22055737edd1SMark Nutter struct tree_descr spufs_dir_nosched_contents[] = {
2206cbe709c1SBenjamin Herrenschmidt 	{ "capabilities", &spufs_caps_fops, 0444, },
22075737edd1SMark Nutter 	{ "mem",  &spufs_mem_fops,  0666, },
22085737edd1SMark Nutter 	{ "mbox", &spufs_mbox_fops, 0444, },
22095737edd1SMark Nutter 	{ "ibox", &spufs_ibox_fops, 0444, },
22105737edd1SMark Nutter 	{ "wbox", &spufs_wbox_fops, 0222, },
22115737edd1SMark Nutter 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, },
22125737edd1SMark Nutter 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, },
22135737edd1SMark Nutter 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, },
2214d054b36fSJeremy Kerr 	{ "signal1", &spufs_signal1_nosched_fops, 0222, },
2215d054b36fSJeremy Kerr 	{ "signal2", &spufs_signal2_nosched_fops, 0222, },
22165737edd1SMark Nutter 	{ "signal1_type", &spufs_signal1_type, 0666, },
22175737edd1SMark Nutter 	{ "signal2_type", &spufs_signal2_type, 0666, },
22185737edd1SMark Nutter 	{ "mss", &spufs_mss_fops, 0666, },
22195737edd1SMark Nutter 	{ "mfc", &spufs_mfc_fops, 0666, },
22205737edd1SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
22215737edd1SMark Nutter 	{ "npc", &spufs_npc_ops, 0666, },
22225737edd1SMark Nutter 	{ "psmap", &spufs_psmap_fops, 0666, },
22235737edd1SMark Nutter 	{ "phys-id", &spufs_id_ops, 0666, },
22245737edd1SMark Nutter 	{ "object-id", &spufs_object_id_ops, 0666, },
2225476273adSChristoph Hellwig 	{ "tid", &spufs_tid_fops, 0444, },
2226e9f8a0b6SChristoph Hellwig 	{ "stat", &spufs_stat_fops, 0444, },
22275737edd1SMark Nutter 	{},
22285737edd1SMark Nutter };
2229bf1ab978SDwayne Grant McConnell 
2230bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader spufs_coredump_read[] = {
22314fca9c42SMichael Ellerman 	{ "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
22324fca9c42SMichael Ellerman 	{ "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2233d464fb44SMichael Ellerman 	{ "lslr", NULL, __spufs_lslr_get, 19 },
2234d464fb44SMichael Ellerman 	{ "decr", NULL, __spufs_decr_get, 19 },
2235d464fb44SMichael Ellerman 	{ "decr_status", NULL, __spufs_decr_status_get, 19 },
22364fca9c42SMichael Ellerman 	{ "mem", __spufs_mem_read, NULL, LS_SIZE, },
22374fca9c42SMichael Ellerman 	{ "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2238d464fb44SMichael Ellerman 	{ "signal1_type", NULL, __spufs_signal1_type_get, 19 },
22394fca9c42SMichael Ellerman 	{ "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2240d464fb44SMichael Ellerman 	{ "signal2_type", NULL, __spufs_signal2_type_get, 19 },
2241d464fb44SMichael Ellerman 	{ "event_mask", NULL, __spufs_event_mask_get, 19 },
2242d464fb44SMichael Ellerman 	{ "event_status", NULL, __spufs_event_status_get, 19 },
22434fca9c42SMichael Ellerman 	{ "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
22444fca9c42SMichael Ellerman 	{ "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
22454fca9c42SMichael Ellerman 	{ "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
22464fca9c42SMichael Ellerman 	{ "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
22474fca9c42SMichael Ellerman 	{ "proxydma_info", __spufs_proxydma_info_read,
22484fca9c42SMichael Ellerman 			   NULL, sizeof(struct spu_proxydma_info)},
2249bf1ab978SDwayne Grant McConnell 	{ "object-id", NULL, __spufs_object_id_get, 19 },
225078810ff6SMichael Ellerman 	{ "npc", NULL, __spufs_npc_get, 19 },
2251936d5bf1SMichael Ellerman 	{ NULL },
2252bf1ab978SDwayne Grant McConnell };
2253