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>
3167207b96SArnd Bergmann 
3267207b96SArnd Bergmann #include <asm/io.h>
3367207b96SArnd Bergmann #include <asm/semaphore.h>
3467207b96SArnd Bergmann #include <asm/spu.h>
3567207b96SArnd Bergmann #include <asm/uaccess.h>
3667207b96SArnd Bergmann 
3767207b96SArnd Bergmann #include "spufs.h"
3867207b96SArnd Bergmann 
3927d5bf2aSBenjamin Herrenschmidt #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
4027d5bf2aSBenjamin Herrenschmidt 
418b3d6663SArnd Bergmann 
4267207b96SArnd Bergmann static int
4367207b96SArnd Bergmann spufs_mem_open(struct inode *inode, struct file *file)
4467207b96SArnd Bergmann {
4567207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
466df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
476df10a82SMark Nutter 	file->private_data = ctx;
486df10a82SMark Nutter 	file->f_mapping = inode->i_mapping;
496df10a82SMark Nutter 	ctx->local_store = inode->i_mapping;
5067207b96SArnd Bergmann 	return 0;
5167207b96SArnd Bergmann }
5267207b96SArnd Bergmann 
5367207b96SArnd Bergmann static ssize_t
5467207b96SArnd Bergmann spufs_mem_read(struct file *file, char __user *buffer,
5567207b96SArnd Bergmann 				size_t size, loff_t *pos)
5667207b96SArnd Bergmann {
578b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
588b3d6663SArnd Bergmann 	char *local_store;
5967207b96SArnd Bergmann 	int ret;
6067207b96SArnd Bergmann 
618b3d6663SArnd Bergmann 	spu_acquire(ctx);
6267207b96SArnd Bergmann 
638b3d6663SArnd Bergmann 	local_store = ctx->ops->get_ls(ctx);
648b3d6663SArnd Bergmann 	ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE);
6567207b96SArnd Bergmann 
668b3d6663SArnd Bergmann 	spu_release(ctx);
6767207b96SArnd Bergmann 	return ret;
6867207b96SArnd Bergmann }
6967207b96SArnd Bergmann 
7067207b96SArnd Bergmann static ssize_t
7167207b96SArnd Bergmann spufs_mem_write(struct file *file, const char __user *buffer,
7267207b96SArnd Bergmann 					size_t size, loff_t *pos)
7367207b96SArnd Bergmann {
7467207b96SArnd Bergmann 	struct spu_context *ctx = file->private_data;
758b3d6663SArnd Bergmann 	char *local_store;
768b3d6663SArnd Bergmann 	int ret;
7767207b96SArnd Bergmann 
7867207b96SArnd Bergmann 	size = min_t(ssize_t, LS_SIZE - *pos, size);
7967207b96SArnd Bergmann 	if (size <= 0)
8067207b96SArnd Bergmann 		return -EFBIG;
8167207b96SArnd Bergmann 	*pos += size;
828b3d6663SArnd Bergmann 
838b3d6663SArnd Bergmann 	spu_acquire(ctx);
848b3d6663SArnd Bergmann 
858b3d6663SArnd Bergmann 	local_store = ctx->ops->get_ls(ctx);
868b3d6663SArnd Bergmann 	ret = copy_from_user(local_store + *pos - size,
8767207b96SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
888b3d6663SArnd Bergmann 
898b3d6663SArnd Bergmann 	spu_release(ctx);
908b3d6663SArnd Bergmann 	return ret;
9167207b96SArnd Bergmann }
9267207b96SArnd Bergmann 
938b3d6663SArnd Bergmann static struct page *
948b3d6663SArnd Bergmann spufs_mem_mmap_nopage(struct vm_area_struct *vma,
958b3d6663SArnd Bergmann 		      unsigned long address, int *type)
968b3d6663SArnd Bergmann {
978b3d6663SArnd Bergmann 	struct page *page = NOPAGE_SIGBUS;
988b3d6663SArnd Bergmann 
998b3d6663SArnd Bergmann 	struct spu_context *ctx = vma->vm_file->private_data;
1008b3d6663SArnd Bergmann 	unsigned long offset = address - vma->vm_start;
1018b3d6663SArnd Bergmann 	offset += vma->vm_pgoff << PAGE_SHIFT;
1028b3d6663SArnd Bergmann 
1038b3d6663SArnd Bergmann 	spu_acquire(ctx);
1048b3d6663SArnd Bergmann 
105ac91cb8dSArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED) {
106ac91cb8dSArnd Bergmann 		vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
107ac91cb8dSArnd Bergmann 					& ~(_PAGE_NO_CACHE | _PAGE_GUARDED));
1088b3d6663SArnd Bergmann 		page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
109ac91cb8dSArnd Bergmann 	} else {
110ac91cb8dSArnd Bergmann 		vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
111ac91cb8dSArnd Bergmann 					| _PAGE_NO_CACHE | _PAGE_GUARDED);
1128b3d6663SArnd Bergmann 		page = pfn_to_page((ctx->spu->local_store_phys + offset)
1138b3d6663SArnd Bergmann 				   >> PAGE_SHIFT);
114ac91cb8dSArnd Bergmann 	}
1158b3d6663SArnd Bergmann 	spu_release(ctx);
1168b3d6663SArnd Bergmann 
1178b3d6663SArnd Bergmann 	if (type)
1188b3d6663SArnd Bergmann 		*type = VM_FAULT_MINOR;
1198b3d6663SArnd Bergmann 
120d88cfffaSArnd Bergmann 	page_cache_get(page);
1218b3d6663SArnd Bergmann 	return page;
1228b3d6663SArnd Bergmann }
1238b3d6663SArnd Bergmann 
1248b3d6663SArnd Bergmann static struct vm_operations_struct spufs_mem_mmap_vmops = {
1258b3d6663SArnd Bergmann 	.nopage = spufs_mem_mmap_nopage,
1268b3d6663SArnd Bergmann };
1278b3d6663SArnd Bergmann 
12867207b96SArnd Bergmann static int
12967207b96SArnd Bergmann spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
13067207b96SArnd Bergmann {
1318b3d6663SArnd Bergmann 	if (!(vma->vm_flags & VM_SHARED))
1328b3d6663SArnd Bergmann 		return -EINVAL;
13367207b96SArnd Bergmann 
1348b3d6663SArnd Bergmann 	/* FIXME: */
13567207b96SArnd Bergmann 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
13667207b96SArnd Bergmann 				     | _PAGE_NO_CACHE);
1378b3d6663SArnd Bergmann 
1388b3d6663SArnd Bergmann 	vma->vm_ops = &spufs_mem_mmap_vmops;
13967207b96SArnd Bergmann 	return 0;
14067207b96SArnd Bergmann }
14167207b96SArnd Bergmann 
14267207b96SArnd Bergmann static struct file_operations spufs_mem_fops = {
14367207b96SArnd Bergmann 	.open	 = spufs_mem_open,
14467207b96SArnd Bergmann 	.read    = spufs_mem_read,
14567207b96SArnd Bergmann 	.write   = spufs_mem_write,
1468b3d6663SArnd Bergmann 	.llseek  = generic_file_llseek,
14767207b96SArnd Bergmann 	.mmap    = spufs_mem_mmap,
1488b3d6663SArnd Bergmann };
1498b3d6663SArnd Bergmann 
1506df10a82SMark Nutter static struct page *spufs_ps_nopage(struct vm_area_struct *vma,
1516df10a82SMark Nutter 				    unsigned long address,
15227d5bf2aSBenjamin Herrenschmidt 				    int *type, unsigned long ps_offs,
15327d5bf2aSBenjamin Herrenschmidt 				    unsigned long ps_size)
1546df10a82SMark Nutter {
1556df10a82SMark Nutter 	struct page *page = NOPAGE_SIGBUS;
1566df10a82SMark Nutter 	int fault_type = VM_FAULT_SIGBUS;
1576df10a82SMark Nutter 	struct spu_context *ctx = vma->vm_file->private_data;
1586df10a82SMark Nutter 	unsigned long offset = address - vma->vm_start;
1596df10a82SMark Nutter 	unsigned long area;
1606df10a82SMark Nutter 	int ret;
1616df10a82SMark Nutter 
1626df10a82SMark Nutter 	offset += vma->vm_pgoff << PAGE_SHIFT;
16327d5bf2aSBenjamin Herrenschmidt 	if (offset >= ps_size)
1646df10a82SMark Nutter 		goto out;
1656df10a82SMark Nutter 
1666df10a82SMark Nutter 	ret = spu_acquire_runnable(ctx);
1676df10a82SMark Nutter 	if (ret)
1686df10a82SMark Nutter 		goto out;
1696df10a82SMark Nutter 
1706df10a82SMark Nutter 	area = ctx->spu->problem_phys + ps_offs;
1716df10a82SMark Nutter 	page = pfn_to_page((area + offset) >> PAGE_SHIFT);
1726df10a82SMark Nutter 	fault_type = VM_FAULT_MINOR;
1736df10a82SMark Nutter 	page_cache_get(page);
1746df10a82SMark Nutter 
1756df10a82SMark Nutter 	spu_release(ctx);
1766df10a82SMark Nutter 
1776df10a82SMark Nutter       out:
1786df10a82SMark Nutter 	if (type)
1796df10a82SMark Nutter 		*type = fault_type;
1806df10a82SMark Nutter 
1816df10a82SMark Nutter 	return page;
1826df10a82SMark Nutter }
1836df10a82SMark Nutter 
18427d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
1856df10a82SMark Nutter static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma,
1866df10a82SMark Nutter 					   unsigned long address, int *type)
1876df10a82SMark Nutter {
18827d5bf2aSBenjamin Herrenschmidt 	return spufs_ps_nopage(vma, address, type, 0x4000, 0x1000);
1896df10a82SMark Nutter }
1906df10a82SMark Nutter 
1916df10a82SMark Nutter static struct vm_operations_struct spufs_cntl_mmap_vmops = {
1926df10a82SMark Nutter 	.nopage = spufs_cntl_mmap_nopage,
1936df10a82SMark Nutter };
1946df10a82SMark Nutter 
1956df10a82SMark Nutter /*
1966df10a82SMark Nutter  * mmap support for problem state control area [0x4000 - 0x4fff].
1976df10a82SMark Nutter  */
1986df10a82SMark Nutter static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
1996df10a82SMark Nutter {
2006df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
2016df10a82SMark Nutter 		return -EINVAL;
2026df10a82SMark Nutter 
2036df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
2046df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
20523cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
2066df10a82SMark Nutter 
2076df10a82SMark Nutter 	vma->vm_ops = &spufs_cntl_mmap_vmops;
2086df10a82SMark Nutter 	return 0;
2096df10a82SMark Nutter }
21027d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
21127d5bf2aSBenjamin Herrenschmidt #define spufs_cntl_mmap NULL
21227d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
2136df10a82SMark Nutter 
2146df10a82SMark Nutter static int spufs_cntl_open(struct inode *inode, struct file *file)
2156df10a82SMark Nutter {
2166df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
2176df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
2186df10a82SMark Nutter 
2196df10a82SMark Nutter 	file->private_data = ctx;
2206df10a82SMark Nutter 	file->f_mapping = inode->i_mapping;
2216df10a82SMark Nutter 	ctx->cntl = inode->i_mapping;
2226df10a82SMark Nutter 	return 0;
2236df10a82SMark Nutter }
2246df10a82SMark Nutter 
2256df10a82SMark Nutter static ssize_t
2266df10a82SMark Nutter spufs_cntl_read(struct file *file, char __user *buffer,
2276df10a82SMark Nutter 		size_t size, loff_t *pos)
2286df10a82SMark Nutter {
2296df10a82SMark Nutter 	/* FIXME: read from spu status */
2306df10a82SMark Nutter 	return -EINVAL;
2316df10a82SMark Nutter }
2326df10a82SMark Nutter 
2336df10a82SMark Nutter static ssize_t
2346df10a82SMark Nutter spufs_cntl_write(struct file *file, const char __user *buffer,
2356df10a82SMark Nutter 		 size_t size, loff_t *pos)
2366df10a82SMark Nutter {
2376df10a82SMark Nutter 	/* FIXME: write to runctl bit */
2386df10a82SMark Nutter 	return -EINVAL;
2396df10a82SMark Nutter }
2406df10a82SMark Nutter 
2416df10a82SMark Nutter static struct file_operations spufs_cntl_fops = {
2426df10a82SMark Nutter 	.open = spufs_cntl_open,
2436df10a82SMark Nutter 	.read = spufs_cntl_read,
2446df10a82SMark Nutter 	.write = spufs_cntl_write,
2456df10a82SMark Nutter 	.mmap = spufs_cntl_mmap,
2466df10a82SMark Nutter };
2476df10a82SMark Nutter 
2488b3d6663SArnd Bergmann static int
2498b3d6663SArnd Bergmann spufs_regs_open(struct inode *inode, struct file *file)
2508b3d6663SArnd Bergmann {
2518b3d6663SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
2528b3d6663SArnd Bergmann 	file->private_data = i->i_ctx;
2538b3d6663SArnd Bergmann 	return 0;
2548b3d6663SArnd Bergmann }
2558b3d6663SArnd Bergmann 
2568b3d6663SArnd Bergmann static ssize_t
2578b3d6663SArnd Bergmann spufs_regs_read(struct file *file, char __user *buffer,
2588b3d6663SArnd Bergmann 		size_t size, loff_t *pos)
2598b3d6663SArnd Bergmann {
2608b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
2618b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
2628b3d6663SArnd Bergmann 	int ret;
2638b3d6663SArnd Bergmann 
2648b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
2658b3d6663SArnd Bergmann 
2668b3d6663SArnd Bergmann 	ret = simple_read_from_buffer(buffer, size, pos,
2678b3d6663SArnd Bergmann 				      lscsa->gprs, sizeof lscsa->gprs);
2688b3d6663SArnd Bergmann 
2698b3d6663SArnd Bergmann 	spu_release(ctx);
2708b3d6663SArnd Bergmann 	return ret;
2718b3d6663SArnd Bergmann }
2728b3d6663SArnd Bergmann 
2738b3d6663SArnd Bergmann static ssize_t
2748b3d6663SArnd Bergmann spufs_regs_write(struct file *file, const char __user *buffer,
2758b3d6663SArnd Bergmann 		 size_t size, loff_t *pos)
2768b3d6663SArnd Bergmann {
2778b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
2788b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
2798b3d6663SArnd Bergmann 	int ret;
2808b3d6663SArnd Bergmann 
2818b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
2828b3d6663SArnd Bergmann 	if (size <= 0)
2838b3d6663SArnd Bergmann 		return -EFBIG;
2848b3d6663SArnd Bergmann 	*pos += size;
2858b3d6663SArnd Bergmann 
2868b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
2878b3d6663SArnd Bergmann 
2888b3d6663SArnd Bergmann 	ret = copy_from_user(lscsa->gprs + *pos - size,
2898b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
2908b3d6663SArnd Bergmann 
2918b3d6663SArnd Bergmann 	spu_release(ctx);
2928b3d6663SArnd Bergmann 	return ret;
2938b3d6663SArnd Bergmann }
2948b3d6663SArnd Bergmann 
2958b3d6663SArnd Bergmann static struct file_operations spufs_regs_fops = {
2968b3d6663SArnd Bergmann 	.open	 = spufs_regs_open,
2978b3d6663SArnd Bergmann 	.read    = spufs_regs_read,
2988b3d6663SArnd Bergmann 	.write   = spufs_regs_write,
2998b3d6663SArnd Bergmann 	.llseek  = generic_file_llseek,
3008b3d6663SArnd Bergmann };
3018b3d6663SArnd Bergmann 
3028b3d6663SArnd Bergmann static ssize_t
3038b3d6663SArnd Bergmann spufs_fpcr_read(struct file *file, char __user * buffer,
3048b3d6663SArnd Bergmann 		size_t size, loff_t * pos)
3058b3d6663SArnd Bergmann {
3068b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
3078b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
3088b3d6663SArnd Bergmann 	int ret;
3098b3d6663SArnd Bergmann 
3108b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
3118b3d6663SArnd Bergmann 
3128b3d6663SArnd Bergmann 	ret = simple_read_from_buffer(buffer, size, pos,
3138b3d6663SArnd Bergmann 				      &lscsa->fpcr, sizeof(lscsa->fpcr));
3148b3d6663SArnd Bergmann 
3158b3d6663SArnd Bergmann 	spu_release(ctx);
3168b3d6663SArnd Bergmann 	return ret;
3178b3d6663SArnd Bergmann }
3188b3d6663SArnd Bergmann 
3198b3d6663SArnd Bergmann static ssize_t
3208b3d6663SArnd Bergmann spufs_fpcr_write(struct file *file, const char __user * buffer,
3218b3d6663SArnd Bergmann 		 size_t size, loff_t * pos)
3228b3d6663SArnd Bergmann {
3238b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
3248b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
3258b3d6663SArnd Bergmann 	int ret;
3268b3d6663SArnd Bergmann 
3278b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
3288b3d6663SArnd Bergmann 	if (size <= 0)
3298b3d6663SArnd Bergmann 		return -EFBIG;
3308b3d6663SArnd Bergmann 	*pos += size;
3318b3d6663SArnd Bergmann 
3328b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
3338b3d6663SArnd Bergmann 
3348b3d6663SArnd Bergmann 	ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
3358b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
3368b3d6663SArnd Bergmann 
3378b3d6663SArnd Bergmann 	spu_release(ctx);
3388b3d6663SArnd Bergmann 	return ret;
3398b3d6663SArnd Bergmann }
3408b3d6663SArnd Bergmann 
3418b3d6663SArnd Bergmann static struct file_operations spufs_fpcr_fops = {
3428b3d6663SArnd Bergmann 	.open = spufs_regs_open,
3438b3d6663SArnd Bergmann 	.read = spufs_fpcr_read,
3448b3d6663SArnd Bergmann 	.write = spufs_fpcr_write,
34567207b96SArnd Bergmann 	.llseek = generic_file_llseek,
34667207b96SArnd Bergmann };
34767207b96SArnd Bergmann 
34867207b96SArnd Bergmann /* generic open function for all pipe-like files */
34967207b96SArnd Bergmann static int spufs_pipe_open(struct inode *inode, struct file *file)
35067207b96SArnd Bergmann {
35167207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
35267207b96SArnd Bergmann 	file->private_data = i->i_ctx;
35367207b96SArnd Bergmann 
35467207b96SArnd Bergmann 	return nonseekable_open(inode, file);
35567207b96SArnd Bergmann }
35667207b96SArnd Bergmann 
35767207b96SArnd Bergmann static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
35867207b96SArnd Bergmann 			size_t len, loff_t *pos)
35967207b96SArnd Bergmann {
3608b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
36167207b96SArnd Bergmann 	u32 mbox_data;
3628b3d6663SArnd Bergmann 	int ret;
36367207b96SArnd Bergmann 
36467207b96SArnd Bergmann 	if (len < 4)
36567207b96SArnd Bergmann 		return -EINVAL;
36667207b96SArnd Bergmann 
3678b3d6663SArnd Bergmann 	spu_acquire(ctx);
3688b3d6663SArnd Bergmann 	ret = ctx->ops->mbox_read(ctx, &mbox_data);
3698b3d6663SArnd Bergmann 	spu_release(ctx);
37067207b96SArnd Bergmann 
3718b3d6663SArnd Bergmann 	if (!ret)
3728b3d6663SArnd Bergmann 		return -EAGAIN;
37367207b96SArnd Bergmann 
37467207b96SArnd Bergmann 	if (copy_to_user(buf, &mbox_data, sizeof mbox_data))
37567207b96SArnd Bergmann 		return -EFAULT;
37667207b96SArnd Bergmann 
37767207b96SArnd Bergmann 	return 4;
37867207b96SArnd Bergmann }
37967207b96SArnd Bergmann 
38067207b96SArnd Bergmann static struct file_operations spufs_mbox_fops = {
38167207b96SArnd Bergmann 	.open	= spufs_pipe_open,
38267207b96SArnd Bergmann 	.read	= spufs_mbox_read,
38367207b96SArnd Bergmann };
38467207b96SArnd Bergmann 
38567207b96SArnd Bergmann static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
38667207b96SArnd Bergmann 			size_t len, loff_t *pos)
38767207b96SArnd Bergmann {
3888b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
38967207b96SArnd Bergmann 	u32 mbox_stat;
39067207b96SArnd Bergmann 
39167207b96SArnd Bergmann 	if (len < 4)
39267207b96SArnd Bergmann 		return -EINVAL;
39367207b96SArnd Bergmann 
3948b3d6663SArnd Bergmann 	spu_acquire(ctx);
3958b3d6663SArnd Bergmann 
3968b3d6663SArnd Bergmann 	mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
3978b3d6663SArnd Bergmann 
3988b3d6663SArnd Bergmann 	spu_release(ctx);
39967207b96SArnd Bergmann 
40067207b96SArnd Bergmann 	if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
40167207b96SArnd Bergmann 		return -EFAULT;
40267207b96SArnd Bergmann 
40367207b96SArnd Bergmann 	return 4;
40467207b96SArnd Bergmann }
40567207b96SArnd Bergmann 
40667207b96SArnd Bergmann static struct file_operations spufs_mbox_stat_fops = {
40767207b96SArnd Bergmann 	.open	= spufs_pipe_open,
40867207b96SArnd Bergmann 	.read	= spufs_mbox_stat_read,
40967207b96SArnd Bergmann };
41067207b96SArnd Bergmann 
41167207b96SArnd Bergmann /* low-level ibox access function */
4128b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
41367207b96SArnd Bergmann {
4148b3d6663SArnd Bergmann 	return ctx->ops->ibox_read(ctx, data);
41567207b96SArnd Bergmann }
41667207b96SArnd Bergmann 
41767207b96SArnd Bergmann static int spufs_ibox_fasync(int fd, struct file *file, int on)
41867207b96SArnd Bergmann {
4198b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
4208b3d6663SArnd Bergmann 
4218b3d6663SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->ibox_fasync);
4228b3d6663SArnd Bergmann }
4238b3d6663SArnd Bergmann 
4248b3d6663SArnd Bergmann /* interrupt-level ibox callback function. */
4258b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu)
4268b3d6663SArnd Bergmann {
4278b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
4288b3d6663SArnd Bergmann 
4298b3d6663SArnd Bergmann 	wake_up_all(&ctx->ibox_wq);
4308b3d6663SArnd Bergmann 	kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
43167207b96SArnd Bergmann }
43267207b96SArnd Bergmann 
43367207b96SArnd Bergmann static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
43467207b96SArnd Bergmann 			size_t len, loff_t *pos)
43567207b96SArnd Bergmann {
4368b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
43767207b96SArnd Bergmann 	u32 ibox_data;
43867207b96SArnd Bergmann 	ssize_t ret;
43967207b96SArnd Bergmann 
44067207b96SArnd Bergmann 	if (len < 4)
44167207b96SArnd Bergmann 		return -EINVAL;
44267207b96SArnd Bergmann 
4438b3d6663SArnd Bergmann 	spu_acquire(ctx);
44467207b96SArnd Bergmann 
44567207b96SArnd Bergmann 	ret = 0;
44667207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
4478b3d6663SArnd Bergmann 		if (!spu_ibox_read(ctx, &ibox_data))
44867207b96SArnd Bergmann 			ret = -EAGAIN;
44967207b96SArnd Bergmann 	} else {
4508b3d6663SArnd Bergmann 		ret = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
45167207b96SArnd Bergmann 	}
45267207b96SArnd Bergmann 
4538b3d6663SArnd Bergmann 	spu_release(ctx);
4548b3d6663SArnd Bergmann 
45567207b96SArnd Bergmann 	if (ret)
45667207b96SArnd Bergmann 		return ret;
45767207b96SArnd Bergmann 
45867207b96SArnd Bergmann 	ret = 4;
45967207b96SArnd Bergmann 	if (copy_to_user(buf, &ibox_data, sizeof ibox_data))
46067207b96SArnd Bergmann 		ret = -EFAULT;
46167207b96SArnd Bergmann 
46267207b96SArnd Bergmann 	return ret;
46367207b96SArnd Bergmann }
46467207b96SArnd Bergmann 
46567207b96SArnd Bergmann static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
46667207b96SArnd Bergmann {
4678b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
46867207b96SArnd Bergmann 	unsigned int mask;
46967207b96SArnd Bergmann 
4708b3d6663SArnd Bergmann 	poll_wait(file, &ctx->ibox_wq, wait);
47167207b96SArnd Bergmann 
4723a843d7cSArnd Bergmann 	spu_acquire(ctx);
4733a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
4743a843d7cSArnd Bergmann 	spu_release(ctx);
47567207b96SArnd Bergmann 
47667207b96SArnd Bergmann 	return mask;
47767207b96SArnd Bergmann }
47867207b96SArnd Bergmann 
47967207b96SArnd Bergmann static struct file_operations spufs_ibox_fops = {
48067207b96SArnd Bergmann 	.open	= spufs_pipe_open,
48167207b96SArnd Bergmann 	.read	= spufs_ibox_read,
48267207b96SArnd Bergmann 	.poll	= spufs_ibox_poll,
48367207b96SArnd Bergmann 	.fasync	= spufs_ibox_fasync,
48467207b96SArnd Bergmann };
48567207b96SArnd Bergmann 
48667207b96SArnd Bergmann static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
48767207b96SArnd Bergmann 			size_t len, loff_t *pos)
48867207b96SArnd Bergmann {
4898b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
49067207b96SArnd Bergmann 	u32 ibox_stat;
49167207b96SArnd Bergmann 
49267207b96SArnd Bergmann 	if (len < 4)
49367207b96SArnd Bergmann 		return -EINVAL;
49467207b96SArnd Bergmann 
4958b3d6663SArnd Bergmann 	spu_acquire(ctx);
4968b3d6663SArnd Bergmann 	ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
4978b3d6663SArnd Bergmann 	spu_release(ctx);
49867207b96SArnd Bergmann 
49967207b96SArnd Bergmann 	if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
50067207b96SArnd Bergmann 		return -EFAULT;
50167207b96SArnd Bergmann 
50267207b96SArnd Bergmann 	return 4;
50367207b96SArnd Bergmann }
50467207b96SArnd Bergmann 
50567207b96SArnd Bergmann static struct file_operations spufs_ibox_stat_fops = {
50667207b96SArnd Bergmann 	.open	= spufs_pipe_open,
50767207b96SArnd Bergmann 	.read	= spufs_ibox_stat_read,
50867207b96SArnd Bergmann };
50967207b96SArnd Bergmann 
51067207b96SArnd Bergmann /* low-level mailbox write */
5118b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data)
51267207b96SArnd Bergmann {
5138b3d6663SArnd Bergmann 	return ctx->ops->wbox_write(ctx, data);
51467207b96SArnd Bergmann }
51567207b96SArnd Bergmann 
51667207b96SArnd Bergmann static int spufs_wbox_fasync(int fd, struct file *file, int on)
51767207b96SArnd Bergmann {
5188b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
5198b3d6663SArnd Bergmann 	int ret;
5208b3d6663SArnd Bergmann 
5218b3d6663SArnd Bergmann 	ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
5228b3d6663SArnd Bergmann 
5238b3d6663SArnd Bergmann 	return ret;
5248b3d6663SArnd Bergmann }
5258b3d6663SArnd Bergmann 
5268b3d6663SArnd Bergmann /* interrupt-level wbox callback function. */
5278b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu)
5288b3d6663SArnd Bergmann {
5298b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
5308b3d6663SArnd Bergmann 
5318b3d6663SArnd Bergmann 	wake_up_all(&ctx->wbox_wq);
5328b3d6663SArnd Bergmann 	kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
53367207b96SArnd Bergmann }
53467207b96SArnd Bergmann 
53567207b96SArnd Bergmann static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
53667207b96SArnd Bergmann 			size_t len, loff_t *pos)
53767207b96SArnd Bergmann {
5388b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
53967207b96SArnd Bergmann 	u32 wbox_data;
54067207b96SArnd Bergmann 	int ret;
54167207b96SArnd Bergmann 
54267207b96SArnd Bergmann 	if (len < 4)
54367207b96SArnd Bergmann 		return -EINVAL;
54467207b96SArnd Bergmann 
54567207b96SArnd Bergmann 	if (copy_from_user(&wbox_data, buf, sizeof wbox_data))
54667207b96SArnd Bergmann 		return -EFAULT;
54767207b96SArnd Bergmann 
5488b3d6663SArnd Bergmann 	spu_acquire(ctx);
5498b3d6663SArnd Bergmann 
55067207b96SArnd Bergmann 	ret = 0;
55167207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
5528b3d6663SArnd Bergmann 		if (!spu_wbox_write(ctx, wbox_data))
55367207b96SArnd Bergmann 			ret = -EAGAIN;
55467207b96SArnd Bergmann 	} else {
5558b3d6663SArnd Bergmann 		ret = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
55667207b96SArnd Bergmann 	}
55767207b96SArnd Bergmann 
5588b3d6663SArnd Bergmann 	spu_release(ctx);
5598b3d6663SArnd Bergmann 
56067207b96SArnd Bergmann 	return ret ? ret : sizeof wbox_data;
56167207b96SArnd Bergmann }
56267207b96SArnd Bergmann 
56367207b96SArnd Bergmann static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
56467207b96SArnd Bergmann {
5658b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
56667207b96SArnd Bergmann 	unsigned int mask;
56767207b96SArnd Bergmann 
5688b3d6663SArnd Bergmann 	poll_wait(file, &ctx->wbox_wq, wait);
56967207b96SArnd Bergmann 
5703a843d7cSArnd Bergmann 	spu_acquire(ctx);
5713a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
5723a843d7cSArnd Bergmann 	spu_release(ctx);
57367207b96SArnd Bergmann 
57467207b96SArnd Bergmann 	return mask;
57567207b96SArnd Bergmann }
57667207b96SArnd Bergmann 
57767207b96SArnd Bergmann static struct file_operations spufs_wbox_fops = {
57867207b96SArnd Bergmann 	.open	= spufs_pipe_open,
57967207b96SArnd Bergmann 	.write	= spufs_wbox_write,
58067207b96SArnd Bergmann 	.poll	= spufs_wbox_poll,
58167207b96SArnd Bergmann 	.fasync	= spufs_wbox_fasync,
58267207b96SArnd Bergmann };
58367207b96SArnd Bergmann 
58467207b96SArnd Bergmann static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
58567207b96SArnd Bergmann 			size_t len, loff_t *pos)
58667207b96SArnd Bergmann {
5878b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
58867207b96SArnd Bergmann 	u32 wbox_stat;
58967207b96SArnd Bergmann 
59067207b96SArnd Bergmann 	if (len < 4)
59167207b96SArnd Bergmann 		return -EINVAL;
59267207b96SArnd Bergmann 
5938b3d6663SArnd Bergmann 	spu_acquire(ctx);
5948b3d6663SArnd Bergmann 	wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
5958b3d6663SArnd Bergmann 	spu_release(ctx);
59667207b96SArnd Bergmann 
59767207b96SArnd Bergmann 	if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
59867207b96SArnd Bergmann 		return -EFAULT;
59967207b96SArnd Bergmann 
60067207b96SArnd Bergmann 	return 4;
60167207b96SArnd Bergmann }
60267207b96SArnd Bergmann 
60367207b96SArnd Bergmann static struct file_operations spufs_wbox_stat_fops = {
60467207b96SArnd Bergmann 	.open	= spufs_pipe_open,
60567207b96SArnd Bergmann 	.read	= spufs_wbox_stat_read,
60667207b96SArnd Bergmann };
60767207b96SArnd Bergmann 
6086df10a82SMark Nutter static int spufs_signal1_open(struct inode *inode, struct file *file)
6096df10a82SMark Nutter {
6106df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
6116df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
6126df10a82SMark Nutter 	file->private_data = ctx;
6136df10a82SMark Nutter 	file->f_mapping = inode->i_mapping;
6146df10a82SMark Nutter 	ctx->signal1 = inode->i_mapping;
6156df10a82SMark Nutter 	return nonseekable_open(inode, file);
6166df10a82SMark Nutter }
6176df10a82SMark Nutter 
61867207b96SArnd Bergmann static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
61967207b96SArnd Bergmann 			size_t len, loff_t *pos)
62067207b96SArnd Bergmann {
6218b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
62267207b96SArnd Bergmann 	u32 data;
62367207b96SArnd Bergmann 
62467207b96SArnd Bergmann 	if (len < 4)
62567207b96SArnd Bergmann 		return -EINVAL;
62667207b96SArnd Bergmann 
6278b3d6663SArnd Bergmann 	spu_acquire(ctx);
6288b3d6663SArnd Bergmann 	data = ctx->ops->signal1_read(ctx);
6298b3d6663SArnd Bergmann 	spu_release(ctx);
6308b3d6663SArnd Bergmann 
63167207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
63267207b96SArnd Bergmann 		return -EFAULT;
63367207b96SArnd Bergmann 
63467207b96SArnd Bergmann 	return 4;
63567207b96SArnd Bergmann }
63667207b96SArnd Bergmann 
63767207b96SArnd Bergmann static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
63867207b96SArnd Bergmann 			size_t len, loff_t *pos)
63967207b96SArnd Bergmann {
64067207b96SArnd Bergmann 	struct spu_context *ctx;
64167207b96SArnd Bergmann 	u32 data;
64267207b96SArnd Bergmann 
64367207b96SArnd Bergmann 	ctx = file->private_data;
64467207b96SArnd Bergmann 
64567207b96SArnd Bergmann 	if (len < 4)
64667207b96SArnd Bergmann 		return -EINVAL;
64767207b96SArnd Bergmann 
64867207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
64967207b96SArnd Bergmann 		return -EFAULT;
65067207b96SArnd Bergmann 
6518b3d6663SArnd Bergmann 	spu_acquire(ctx);
6528b3d6663SArnd Bergmann 	ctx->ops->signal1_write(ctx, data);
6538b3d6663SArnd Bergmann 	spu_release(ctx);
65467207b96SArnd Bergmann 
65567207b96SArnd Bergmann 	return 4;
65667207b96SArnd Bergmann }
65767207b96SArnd Bergmann 
6586df10a82SMark Nutter static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
6596df10a82SMark Nutter 					      unsigned long address, int *type)
6606df10a82SMark Nutter {
66127d5bf2aSBenjamin Herrenschmidt #if PAGE_SIZE == 0x1000
66227d5bf2aSBenjamin Herrenschmidt 	return spufs_ps_nopage(vma, address, type, 0x14000, 0x1000);
66327d5bf2aSBenjamin Herrenschmidt #elif PAGE_SIZE == 0x10000
66427d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
66527d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
66627d5bf2aSBenjamin Herrenschmidt 	 */
66727d5bf2aSBenjamin Herrenschmidt 	return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
66827d5bf2aSBenjamin Herrenschmidt #else
66927d5bf2aSBenjamin Herrenschmidt #error unsupported page size
67027d5bf2aSBenjamin Herrenschmidt #endif
6716df10a82SMark Nutter }
6726df10a82SMark Nutter 
6736df10a82SMark Nutter static struct vm_operations_struct spufs_signal1_mmap_vmops = {
6746df10a82SMark Nutter 	.nopage = spufs_signal1_mmap_nopage,
6756df10a82SMark Nutter };
6766df10a82SMark Nutter 
6776df10a82SMark Nutter static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
6786df10a82SMark Nutter {
6796df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
6806df10a82SMark Nutter 		return -EINVAL;
6816df10a82SMark Nutter 
6826df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
6836df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
68423cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
6856df10a82SMark Nutter 
6866df10a82SMark Nutter 	vma->vm_ops = &spufs_signal1_mmap_vmops;
6876df10a82SMark Nutter 	return 0;
6886df10a82SMark Nutter }
6896df10a82SMark Nutter 
69067207b96SArnd Bergmann static struct file_operations spufs_signal1_fops = {
6916df10a82SMark Nutter 	.open = spufs_signal1_open,
69267207b96SArnd Bergmann 	.read = spufs_signal1_read,
69367207b96SArnd Bergmann 	.write = spufs_signal1_write,
6946df10a82SMark Nutter 	.mmap = spufs_signal1_mmap,
69567207b96SArnd Bergmann };
69667207b96SArnd Bergmann 
6976df10a82SMark Nutter static int spufs_signal2_open(struct inode *inode, struct file *file)
6986df10a82SMark Nutter {
6996df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
7006df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
7016df10a82SMark Nutter 	file->private_data = ctx;
7026df10a82SMark Nutter 	file->f_mapping = inode->i_mapping;
7036df10a82SMark Nutter 	ctx->signal2 = inode->i_mapping;
7046df10a82SMark Nutter 	return nonseekable_open(inode, file);
7056df10a82SMark Nutter }
7066df10a82SMark Nutter 
70767207b96SArnd Bergmann static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
70867207b96SArnd Bergmann 			size_t len, loff_t *pos)
70967207b96SArnd Bergmann {
71067207b96SArnd Bergmann 	struct spu_context *ctx;
71167207b96SArnd Bergmann 	u32 data;
71267207b96SArnd Bergmann 
71367207b96SArnd Bergmann 	ctx = file->private_data;
71467207b96SArnd Bergmann 
71567207b96SArnd Bergmann 	if (len < 4)
71667207b96SArnd Bergmann 		return -EINVAL;
71767207b96SArnd Bergmann 
7188b3d6663SArnd Bergmann 	spu_acquire(ctx);
7198b3d6663SArnd Bergmann 	data = ctx->ops->signal2_read(ctx);
7208b3d6663SArnd Bergmann 	spu_release(ctx);
7218b3d6663SArnd Bergmann 
72267207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
72367207b96SArnd Bergmann 		return -EFAULT;
72467207b96SArnd Bergmann 
72567207b96SArnd Bergmann 	return 4;
72667207b96SArnd Bergmann }
72767207b96SArnd Bergmann 
72867207b96SArnd Bergmann static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
72967207b96SArnd Bergmann 			size_t len, loff_t *pos)
73067207b96SArnd Bergmann {
73167207b96SArnd Bergmann 	struct spu_context *ctx;
73267207b96SArnd Bergmann 	u32 data;
73367207b96SArnd Bergmann 
73467207b96SArnd Bergmann 	ctx = file->private_data;
73567207b96SArnd Bergmann 
73667207b96SArnd Bergmann 	if (len < 4)
73767207b96SArnd Bergmann 		return -EINVAL;
73867207b96SArnd Bergmann 
73967207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
74067207b96SArnd Bergmann 		return -EFAULT;
74167207b96SArnd Bergmann 
7428b3d6663SArnd Bergmann 	spu_acquire(ctx);
7438b3d6663SArnd Bergmann 	ctx->ops->signal2_write(ctx, data);
7448b3d6663SArnd Bergmann 	spu_release(ctx);
74567207b96SArnd Bergmann 
74667207b96SArnd Bergmann 	return 4;
74767207b96SArnd Bergmann }
74867207b96SArnd Bergmann 
74927d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
7506df10a82SMark Nutter static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
7516df10a82SMark Nutter 					      unsigned long address, int *type)
7526df10a82SMark Nutter {
75327d5bf2aSBenjamin Herrenschmidt #if PAGE_SIZE == 0x1000
75427d5bf2aSBenjamin Herrenschmidt 	return spufs_ps_nopage(vma, address, type, 0x1c000, 0x1000);
75527d5bf2aSBenjamin Herrenschmidt #elif PAGE_SIZE == 0x10000
75627d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
75727d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
75827d5bf2aSBenjamin Herrenschmidt 	 */
75927d5bf2aSBenjamin Herrenschmidt 	return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000);
76027d5bf2aSBenjamin Herrenschmidt #else
76127d5bf2aSBenjamin Herrenschmidt #error unsupported page size
76227d5bf2aSBenjamin Herrenschmidt #endif
7636df10a82SMark Nutter }
7646df10a82SMark Nutter 
7656df10a82SMark Nutter static struct vm_operations_struct spufs_signal2_mmap_vmops = {
7666df10a82SMark Nutter 	.nopage = spufs_signal2_mmap_nopage,
7676df10a82SMark Nutter };
7686df10a82SMark Nutter 
7696df10a82SMark Nutter static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
7706df10a82SMark Nutter {
7716df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
7726df10a82SMark Nutter 		return -EINVAL;
7736df10a82SMark Nutter 
7746df10a82SMark Nutter 	/* FIXME: */
7756df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
7766df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
77723cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
7786df10a82SMark Nutter 
7796df10a82SMark Nutter 	vma->vm_ops = &spufs_signal2_mmap_vmops;
7806df10a82SMark Nutter 	return 0;
7816df10a82SMark Nutter }
78227d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
78327d5bf2aSBenjamin Herrenschmidt #define spufs_signal2_mmap NULL
78427d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
7856df10a82SMark Nutter 
78667207b96SArnd Bergmann static struct file_operations spufs_signal2_fops = {
7876df10a82SMark Nutter 	.open = spufs_signal2_open,
78867207b96SArnd Bergmann 	.read = spufs_signal2_read,
78967207b96SArnd Bergmann 	.write = spufs_signal2_write,
7906df10a82SMark Nutter 	.mmap = spufs_signal2_mmap,
79167207b96SArnd Bergmann };
79267207b96SArnd Bergmann 
79367207b96SArnd Bergmann static void spufs_signal1_type_set(void *data, u64 val)
79467207b96SArnd Bergmann {
79567207b96SArnd Bergmann 	struct spu_context *ctx = data;
79667207b96SArnd Bergmann 
7978b3d6663SArnd Bergmann 	spu_acquire(ctx);
7988b3d6663SArnd Bergmann 	ctx->ops->signal1_type_set(ctx, val);
7998b3d6663SArnd Bergmann 	spu_release(ctx);
80067207b96SArnd Bergmann }
80167207b96SArnd Bergmann 
80267207b96SArnd Bergmann static u64 spufs_signal1_type_get(void *data)
80367207b96SArnd Bergmann {
80467207b96SArnd Bergmann 	struct spu_context *ctx = data;
8058b3d6663SArnd Bergmann 	u64 ret;
8068b3d6663SArnd Bergmann 
8078b3d6663SArnd Bergmann 	spu_acquire(ctx);
8088b3d6663SArnd Bergmann 	ret = ctx->ops->signal1_type_get(ctx);
8098b3d6663SArnd Bergmann 	spu_release(ctx);
8108b3d6663SArnd Bergmann 
8118b3d6663SArnd Bergmann 	return ret;
81267207b96SArnd Bergmann }
81367207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
81467207b96SArnd Bergmann 					spufs_signal1_type_set, "%llu");
81567207b96SArnd Bergmann 
81667207b96SArnd Bergmann static void spufs_signal2_type_set(void *data, u64 val)
81767207b96SArnd Bergmann {
81867207b96SArnd Bergmann 	struct spu_context *ctx = data;
81967207b96SArnd Bergmann 
8208b3d6663SArnd Bergmann 	spu_acquire(ctx);
8218b3d6663SArnd Bergmann 	ctx->ops->signal2_type_set(ctx, val);
8228b3d6663SArnd Bergmann 	spu_release(ctx);
82367207b96SArnd Bergmann }
82467207b96SArnd Bergmann 
82567207b96SArnd Bergmann static u64 spufs_signal2_type_get(void *data)
82667207b96SArnd Bergmann {
82767207b96SArnd Bergmann 	struct spu_context *ctx = data;
8288b3d6663SArnd Bergmann 	u64 ret;
8298b3d6663SArnd Bergmann 
8308b3d6663SArnd Bergmann 	spu_acquire(ctx);
8318b3d6663SArnd Bergmann 	ret = ctx->ops->signal2_type_get(ctx);
8328b3d6663SArnd Bergmann 	spu_release(ctx);
8338b3d6663SArnd Bergmann 
8348b3d6663SArnd Bergmann 	return ret;
83567207b96SArnd Bergmann }
83667207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
83767207b96SArnd Bergmann 					spufs_signal2_type_set, "%llu");
83867207b96SArnd Bergmann 
83927d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
840d9379c4bSarnd@arndb.de static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
841d9379c4bSarnd@arndb.de 					   unsigned long address, int *type)
842d9379c4bSarnd@arndb.de {
84327d5bf2aSBenjamin Herrenschmidt 	return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000);
844d9379c4bSarnd@arndb.de }
845d9379c4bSarnd@arndb.de 
846d9379c4bSarnd@arndb.de static struct vm_operations_struct spufs_mss_mmap_vmops = {
847d9379c4bSarnd@arndb.de 	.nopage = spufs_mss_mmap_nopage,
848d9379c4bSarnd@arndb.de };
849d9379c4bSarnd@arndb.de 
850d9379c4bSarnd@arndb.de /*
851d9379c4bSarnd@arndb.de  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
852d9379c4bSarnd@arndb.de  */
853d9379c4bSarnd@arndb.de static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
854d9379c4bSarnd@arndb.de {
855d9379c4bSarnd@arndb.de 	if (!(vma->vm_flags & VM_SHARED))
856d9379c4bSarnd@arndb.de 		return -EINVAL;
857d9379c4bSarnd@arndb.de 
858d9379c4bSarnd@arndb.de 	vma->vm_flags |= VM_RESERVED;
859d9379c4bSarnd@arndb.de 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
86023cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
861d9379c4bSarnd@arndb.de 
862d9379c4bSarnd@arndb.de 	vma->vm_ops = &spufs_mss_mmap_vmops;
863d9379c4bSarnd@arndb.de 	return 0;
864d9379c4bSarnd@arndb.de }
86527d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
86627d5bf2aSBenjamin Herrenschmidt #define spufs_mss_mmap NULL
86727d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
868d9379c4bSarnd@arndb.de 
869d9379c4bSarnd@arndb.de static int spufs_mss_open(struct inode *inode, struct file *file)
870d9379c4bSarnd@arndb.de {
871d9379c4bSarnd@arndb.de 	struct spufs_inode_info *i = SPUFS_I(inode);
872d9379c4bSarnd@arndb.de 
873d9379c4bSarnd@arndb.de 	file->private_data = i->i_ctx;
874d9379c4bSarnd@arndb.de 	return nonseekable_open(inode, file);
875d9379c4bSarnd@arndb.de }
876d9379c4bSarnd@arndb.de 
877d9379c4bSarnd@arndb.de static struct file_operations spufs_mss_fops = {
878d9379c4bSarnd@arndb.de 	.open	 = spufs_mss_open,
879d9379c4bSarnd@arndb.de 	.mmap	 = spufs_mss_mmap,
88027d5bf2aSBenjamin Herrenschmidt };
88127d5bf2aSBenjamin Herrenschmidt 
88227d5bf2aSBenjamin Herrenschmidt static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma,
88327d5bf2aSBenjamin Herrenschmidt 					   unsigned long address, int *type)
88427d5bf2aSBenjamin Herrenschmidt {
88527d5bf2aSBenjamin Herrenschmidt 	return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000);
88627d5bf2aSBenjamin Herrenschmidt }
88727d5bf2aSBenjamin Herrenschmidt 
88827d5bf2aSBenjamin Herrenschmidt static struct vm_operations_struct spufs_psmap_mmap_vmops = {
88927d5bf2aSBenjamin Herrenschmidt 	.nopage = spufs_psmap_mmap_nopage,
89027d5bf2aSBenjamin Herrenschmidt };
89127d5bf2aSBenjamin Herrenschmidt 
89227d5bf2aSBenjamin Herrenschmidt /*
89327d5bf2aSBenjamin Herrenschmidt  * mmap support for full problem state area [0x00000 - 0x1ffff].
89427d5bf2aSBenjamin Herrenschmidt  */
89527d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
89627d5bf2aSBenjamin Herrenschmidt {
89727d5bf2aSBenjamin Herrenschmidt 	if (!(vma->vm_flags & VM_SHARED))
89827d5bf2aSBenjamin Herrenschmidt 		return -EINVAL;
89927d5bf2aSBenjamin Herrenschmidt 
90027d5bf2aSBenjamin Herrenschmidt 	vma->vm_flags |= VM_RESERVED;
90127d5bf2aSBenjamin Herrenschmidt 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
90227d5bf2aSBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
90327d5bf2aSBenjamin Herrenschmidt 
90427d5bf2aSBenjamin Herrenschmidt 	vma->vm_ops = &spufs_psmap_mmap_vmops;
90527d5bf2aSBenjamin Herrenschmidt 	return 0;
90627d5bf2aSBenjamin Herrenschmidt }
90727d5bf2aSBenjamin Herrenschmidt 
90827d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_open(struct inode *inode, struct file *file)
90927d5bf2aSBenjamin Herrenschmidt {
91027d5bf2aSBenjamin Herrenschmidt 	struct spufs_inode_info *i = SPUFS_I(inode);
91127d5bf2aSBenjamin Herrenschmidt 
91227d5bf2aSBenjamin Herrenschmidt 	file->private_data = i->i_ctx;
91327d5bf2aSBenjamin Herrenschmidt 	return nonseekable_open(inode, file);
91427d5bf2aSBenjamin Herrenschmidt }
91527d5bf2aSBenjamin Herrenschmidt 
91627d5bf2aSBenjamin Herrenschmidt static struct file_operations spufs_psmap_fops = {
91727d5bf2aSBenjamin Herrenschmidt 	.open	 = spufs_psmap_open,
91827d5bf2aSBenjamin Herrenschmidt 	.mmap	 = spufs_psmap_mmap,
919d9379c4bSarnd@arndb.de };
920d9379c4bSarnd@arndb.de 
921d9379c4bSarnd@arndb.de 
92227d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
9236df10a82SMark Nutter static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
9246df10a82SMark Nutter 					   unsigned long address, int *type)
9256df10a82SMark Nutter {
92627d5bf2aSBenjamin Herrenschmidt 	return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000);
9276df10a82SMark Nutter }
9286df10a82SMark Nutter 
9296df10a82SMark Nutter static struct vm_operations_struct spufs_mfc_mmap_vmops = {
9306df10a82SMark Nutter 	.nopage = spufs_mfc_mmap_nopage,
9316df10a82SMark Nutter };
9326df10a82SMark Nutter 
9336df10a82SMark Nutter /*
9346df10a82SMark Nutter  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
9356df10a82SMark Nutter  */
9366df10a82SMark Nutter static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
9376df10a82SMark Nutter {
9386df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
9396df10a82SMark Nutter 		return -EINVAL;
9406df10a82SMark Nutter 
9416df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
9426df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
94323cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
9446df10a82SMark Nutter 
9456df10a82SMark Nutter 	vma->vm_ops = &spufs_mfc_mmap_vmops;
9466df10a82SMark Nutter 	return 0;
9476df10a82SMark Nutter }
94827d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
94927d5bf2aSBenjamin Herrenschmidt #define spufs_mfc_mmap NULL
95027d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
951a33a7d73SArnd Bergmann 
952a33a7d73SArnd Bergmann static int spufs_mfc_open(struct inode *inode, struct file *file)
953a33a7d73SArnd Bergmann {
954a33a7d73SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
955a33a7d73SArnd Bergmann 	struct spu_context *ctx = i->i_ctx;
956a33a7d73SArnd Bergmann 
957a33a7d73SArnd Bergmann 	/* we don't want to deal with DMA into other processes */
958a33a7d73SArnd Bergmann 	if (ctx->owner != current->mm)
959a33a7d73SArnd Bergmann 		return -EINVAL;
960a33a7d73SArnd Bergmann 
961a33a7d73SArnd Bergmann 	if (atomic_read(&inode->i_count) != 1)
962a33a7d73SArnd Bergmann 		return -EBUSY;
963a33a7d73SArnd Bergmann 
964a33a7d73SArnd Bergmann 	file->private_data = ctx;
965a33a7d73SArnd Bergmann 	return nonseekable_open(inode, file);
966a33a7d73SArnd Bergmann }
967a33a7d73SArnd Bergmann 
968a33a7d73SArnd Bergmann /* interrupt-level mfc callback function. */
969a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu)
970a33a7d73SArnd Bergmann {
971a33a7d73SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
972a33a7d73SArnd Bergmann 
973a33a7d73SArnd Bergmann 	wake_up_all(&ctx->mfc_wq);
974a33a7d73SArnd Bergmann 
975a33a7d73SArnd Bergmann 	pr_debug("%s %s\n", __FUNCTION__, spu->name);
976a33a7d73SArnd Bergmann 	if (ctx->mfc_fasync) {
977a33a7d73SArnd Bergmann 		u32 free_elements, tagstatus;
978a33a7d73SArnd Bergmann 		unsigned int mask;
979a33a7d73SArnd Bergmann 
980a33a7d73SArnd Bergmann 		/* no need for spu_acquire in interrupt context */
981a33a7d73SArnd Bergmann 		free_elements = ctx->ops->get_mfc_free_elements(ctx);
982a33a7d73SArnd Bergmann 		tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
983a33a7d73SArnd Bergmann 
984a33a7d73SArnd Bergmann 		mask = 0;
985a33a7d73SArnd Bergmann 		if (free_elements & 0xffff)
986a33a7d73SArnd Bergmann 			mask |= POLLOUT;
987a33a7d73SArnd Bergmann 		if (tagstatus & ctx->tagwait)
988a33a7d73SArnd Bergmann 			mask |= POLLIN;
989a33a7d73SArnd Bergmann 
990a33a7d73SArnd Bergmann 		kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
991a33a7d73SArnd Bergmann 	}
992a33a7d73SArnd Bergmann }
993a33a7d73SArnd Bergmann 
994a33a7d73SArnd Bergmann static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
995a33a7d73SArnd Bergmann {
996a33a7d73SArnd Bergmann 	/* See if there is one tag group is complete */
997a33a7d73SArnd Bergmann 	/* FIXME we need locking around tagwait */
998a33a7d73SArnd Bergmann 	*status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
999a33a7d73SArnd Bergmann 	ctx->tagwait &= ~*status;
1000a33a7d73SArnd Bergmann 	if (*status)
1001a33a7d73SArnd Bergmann 		return 1;
1002a33a7d73SArnd Bergmann 
1003a33a7d73SArnd Bergmann 	/* enable interrupt waiting for any tag group,
1004a33a7d73SArnd Bergmann 	   may silently fail if interrupts are already enabled */
1005a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1006a33a7d73SArnd Bergmann 	return 0;
1007a33a7d73SArnd Bergmann }
1008a33a7d73SArnd Bergmann 
1009a33a7d73SArnd Bergmann static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1010a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1011a33a7d73SArnd Bergmann {
1012a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1013a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1014a33a7d73SArnd Bergmann 	u32 status;
1015a33a7d73SArnd Bergmann 
1016a33a7d73SArnd Bergmann 	if (size != 4)
1017a33a7d73SArnd Bergmann 		goto out;
1018a33a7d73SArnd Bergmann 
1019a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1020a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1021a33a7d73SArnd Bergmann 		status = ctx->ops->read_mfc_tagstatus(ctx);
1022a33a7d73SArnd Bergmann 		if (!(status & ctx->tagwait))
1023a33a7d73SArnd Bergmann 			ret = -EAGAIN;
1024a33a7d73SArnd Bergmann 		else
1025a33a7d73SArnd Bergmann 			ctx->tagwait &= ~status;
1026a33a7d73SArnd Bergmann 	} else {
1027a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1028a33a7d73SArnd Bergmann 			   spufs_read_mfc_tagstatus(ctx, &status));
1029a33a7d73SArnd Bergmann 	}
1030a33a7d73SArnd Bergmann 	spu_release(ctx);
1031a33a7d73SArnd Bergmann 
1032a33a7d73SArnd Bergmann 	if (ret)
1033a33a7d73SArnd Bergmann 		goto out;
1034a33a7d73SArnd Bergmann 
1035a33a7d73SArnd Bergmann 	ret = 4;
1036a33a7d73SArnd Bergmann 	if (copy_to_user(buffer, &status, 4))
1037a33a7d73SArnd Bergmann 		ret = -EFAULT;
1038a33a7d73SArnd Bergmann 
1039a33a7d73SArnd Bergmann out:
1040a33a7d73SArnd Bergmann 	return ret;
1041a33a7d73SArnd Bergmann }
1042a33a7d73SArnd Bergmann 
1043a33a7d73SArnd Bergmann static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1044a33a7d73SArnd Bergmann {
1045a33a7d73SArnd Bergmann 	pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1046a33a7d73SArnd Bergmann 		 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1047a33a7d73SArnd Bergmann 
1048a33a7d73SArnd Bergmann 	switch (cmd->cmd) {
1049a33a7d73SArnd Bergmann 	case MFC_PUT_CMD:
1050a33a7d73SArnd Bergmann 	case MFC_PUTF_CMD:
1051a33a7d73SArnd Bergmann 	case MFC_PUTB_CMD:
1052a33a7d73SArnd Bergmann 	case MFC_GET_CMD:
1053a33a7d73SArnd Bergmann 	case MFC_GETF_CMD:
1054a33a7d73SArnd Bergmann 	case MFC_GETB_CMD:
1055a33a7d73SArnd Bergmann 		break;
1056a33a7d73SArnd Bergmann 	default:
1057a33a7d73SArnd Bergmann 		pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1058a33a7d73SArnd Bergmann 		return -EIO;
1059a33a7d73SArnd Bergmann 	}
1060a33a7d73SArnd Bergmann 
1061a33a7d73SArnd Bergmann 	if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1062a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1063a33a7d73SArnd Bergmann 				cmd->ea, cmd->lsa);
1064a33a7d73SArnd Bergmann 		return -EIO;
1065a33a7d73SArnd Bergmann 	}
1066a33a7d73SArnd Bergmann 
1067a33a7d73SArnd Bergmann 	switch (cmd->size & 0xf) {
1068a33a7d73SArnd Bergmann 	case 1:
1069a33a7d73SArnd Bergmann 		break;
1070a33a7d73SArnd Bergmann 	case 2:
1071a33a7d73SArnd Bergmann 		if (cmd->lsa & 1)
1072a33a7d73SArnd Bergmann 			goto error;
1073a33a7d73SArnd Bergmann 		break;
1074a33a7d73SArnd Bergmann 	case 4:
1075a33a7d73SArnd Bergmann 		if (cmd->lsa & 3)
1076a33a7d73SArnd Bergmann 			goto error;
1077a33a7d73SArnd Bergmann 		break;
1078a33a7d73SArnd Bergmann 	case 8:
1079a33a7d73SArnd Bergmann 		if (cmd->lsa & 7)
1080a33a7d73SArnd Bergmann 			goto error;
1081a33a7d73SArnd Bergmann 		break;
1082a33a7d73SArnd Bergmann 	case 0:
1083a33a7d73SArnd Bergmann 		if (cmd->lsa & 15)
1084a33a7d73SArnd Bergmann 			goto error;
1085a33a7d73SArnd Bergmann 		break;
1086a33a7d73SArnd Bergmann 	error:
1087a33a7d73SArnd Bergmann 	default:
1088a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment %x for size %x\n",
1089a33a7d73SArnd Bergmann 			cmd->lsa & 0xf, cmd->size);
1090a33a7d73SArnd Bergmann 		return -EIO;
1091a33a7d73SArnd Bergmann 	}
1092a33a7d73SArnd Bergmann 
1093a33a7d73SArnd Bergmann 	if (cmd->size > 16 * 1024) {
1094a33a7d73SArnd Bergmann 		pr_debug("invalid DMA size %x\n", cmd->size);
1095a33a7d73SArnd Bergmann 		return -EIO;
1096a33a7d73SArnd Bergmann 	}
1097a33a7d73SArnd Bergmann 
1098a33a7d73SArnd Bergmann 	if (cmd->tag & 0xfff0) {
1099a33a7d73SArnd Bergmann 		/* we reserve the higher tag numbers for kernel use */
1100a33a7d73SArnd Bergmann 		pr_debug("invalid DMA tag\n");
1101a33a7d73SArnd Bergmann 		return -EIO;
1102a33a7d73SArnd Bergmann 	}
1103a33a7d73SArnd Bergmann 
1104a33a7d73SArnd Bergmann 	if (cmd->class) {
1105a33a7d73SArnd Bergmann 		/* not supported in this version */
1106a33a7d73SArnd Bergmann 		pr_debug("invalid DMA class\n");
1107a33a7d73SArnd Bergmann 		return -EIO;
1108a33a7d73SArnd Bergmann 	}
1109a33a7d73SArnd Bergmann 
1110a33a7d73SArnd Bergmann 	return 0;
1111a33a7d73SArnd Bergmann }
1112a33a7d73SArnd Bergmann 
1113a33a7d73SArnd Bergmann static int spu_send_mfc_command(struct spu_context *ctx,
1114a33a7d73SArnd Bergmann 				struct mfc_dma_command cmd,
1115a33a7d73SArnd Bergmann 				int *error)
1116a33a7d73SArnd Bergmann {
1117a33a7d73SArnd Bergmann 	*error = ctx->ops->send_mfc_command(ctx, &cmd);
1118a33a7d73SArnd Bergmann 	if (*error == -EAGAIN) {
1119a33a7d73SArnd Bergmann 		/* wait for any tag group to complete
1120a33a7d73SArnd Bergmann 		   so we have space for the new command */
1121a33a7d73SArnd Bergmann 		ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1122a33a7d73SArnd Bergmann 		/* try again, because the queue might be
1123a33a7d73SArnd Bergmann 		   empty again */
1124a33a7d73SArnd Bergmann 		*error = ctx->ops->send_mfc_command(ctx, &cmd);
1125a33a7d73SArnd Bergmann 		if (*error == -EAGAIN)
1126a33a7d73SArnd Bergmann 			return 0;
1127a33a7d73SArnd Bergmann 	}
1128a33a7d73SArnd Bergmann 	return 1;
1129a33a7d73SArnd Bergmann }
1130a33a7d73SArnd Bergmann 
1131a33a7d73SArnd Bergmann static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1132a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1133a33a7d73SArnd Bergmann {
1134a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1135a33a7d73SArnd Bergmann 	struct mfc_dma_command cmd;
1136a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1137a33a7d73SArnd Bergmann 
1138a33a7d73SArnd Bergmann 	if (size != sizeof cmd)
1139a33a7d73SArnd Bergmann 		goto out;
1140a33a7d73SArnd Bergmann 
1141a33a7d73SArnd Bergmann 	ret = -EFAULT;
1142a33a7d73SArnd Bergmann 	if (copy_from_user(&cmd, buffer, sizeof cmd))
1143a33a7d73SArnd Bergmann 		goto out;
1144a33a7d73SArnd Bergmann 
1145a33a7d73SArnd Bergmann 	ret = spufs_check_valid_dma(&cmd);
1146a33a7d73SArnd Bergmann 	if (ret)
1147a33a7d73SArnd Bergmann 		goto out;
1148a33a7d73SArnd Bergmann 
1149a33a7d73SArnd Bergmann 	spu_acquire_runnable(ctx);
1150a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1151a33a7d73SArnd Bergmann 		ret = ctx->ops->send_mfc_command(ctx, &cmd);
1152a33a7d73SArnd Bergmann 	} else {
1153a33a7d73SArnd Bergmann 		int status;
1154a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1155a33a7d73SArnd Bergmann 				 spu_send_mfc_command(ctx, cmd, &status));
1156a33a7d73SArnd Bergmann 		if (status)
1157a33a7d73SArnd Bergmann 			ret = status;
1158a33a7d73SArnd Bergmann 	}
1159a33a7d73SArnd Bergmann 	spu_release(ctx);
1160a33a7d73SArnd Bergmann 
1161a33a7d73SArnd Bergmann 	if (ret)
1162a33a7d73SArnd Bergmann 		goto out;
1163a33a7d73SArnd Bergmann 
1164a33a7d73SArnd Bergmann 	ctx->tagwait |= 1 << cmd.tag;
1165a33a7d73SArnd Bergmann 
1166a33a7d73SArnd Bergmann out:
1167a33a7d73SArnd Bergmann 	return ret;
1168a33a7d73SArnd Bergmann }
1169a33a7d73SArnd Bergmann 
1170a33a7d73SArnd Bergmann static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1171a33a7d73SArnd Bergmann {
1172a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1173a33a7d73SArnd Bergmann 	u32 free_elements, tagstatus;
1174a33a7d73SArnd Bergmann 	unsigned int mask;
1175a33a7d73SArnd Bergmann 
1176a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1177a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1178a33a7d73SArnd Bergmann 	free_elements = ctx->ops->get_mfc_free_elements(ctx);
1179a33a7d73SArnd Bergmann 	tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1180a33a7d73SArnd Bergmann 	spu_release(ctx);
1181a33a7d73SArnd Bergmann 
1182a33a7d73SArnd Bergmann 	poll_wait(file, &ctx->mfc_wq, wait);
1183a33a7d73SArnd Bergmann 
1184a33a7d73SArnd Bergmann 	mask = 0;
1185a33a7d73SArnd Bergmann 	if (free_elements & 0xffff)
1186a33a7d73SArnd Bergmann 		mask |= POLLOUT | POLLWRNORM;
1187a33a7d73SArnd Bergmann 	if (tagstatus & ctx->tagwait)
1188a33a7d73SArnd Bergmann 		mask |= POLLIN | POLLRDNORM;
1189a33a7d73SArnd Bergmann 
1190a33a7d73SArnd Bergmann 	pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1191a33a7d73SArnd Bergmann 		free_elements, tagstatus, ctx->tagwait);
1192a33a7d73SArnd Bergmann 
1193a33a7d73SArnd Bergmann 	return mask;
1194a33a7d73SArnd Bergmann }
1195a33a7d73SArnd Bergmann 
119673b6af8aSAl Viro static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1197a33a7d73SArnd Bergmann {
1198a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1199a33a7d73SArnd Bergmann 	int ret;
1200a33a7d73SArnd Bergmann 
1201a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1202a33a7d73SArnd Bergmann #if 0
1203a33a7d73SArnd Bergmann /* this currently hangs */
1204a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1205a33a7d73SArnd Bergmann 			 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1206a33a7d73SArnd Bergmann 	if (ret)
1207a33a7d73SArnd Bergmann 		goto out;
1208a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1209a33a7d73SArnd Bergmann 			 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1210a33a7d73SArnd Bergmann out:
1211a33a7d73SArnd Bergmann #else
1212a33a7d73SArnd Bergmann 	ret = 0;
1213a33a7d73SArnd Bergmann #endif
1214a33a7d73SArnd Bergmann 	spu_release(ctx);
1215a33a7d73SArnd Bergmann 
1216a33a7d73SArnd Bergmann 	return ret;
1217a33a7d73SArnd Bergmann }
1218a33a7d73SArnd Bergmann 
1219a33a7d73SArnd Bergmann static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1220a33a7d73SArnd Bergmann 			   int datasync)
1221a33a7d73SArnd Bergmann {
122273b6af8aSAl Viro 	return spufs_mfc_flush(file, NULL);
1223a33a7d73SArnd Bergmann }
1224a33a7d73SArnd Bergmann 
1225a33a7d73SArnd Bergmann static int spufs_mfc_fasync(int fd, struct file *file, int on)
1226a33a7d73SArnd Bergmann {
1227a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1228a33a7d73SArnd Bergmann 
1229a33a7d73SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1230a33a7d73SArnd Bergmann }
1231a33a7d73SArnd Bergmann 
1232a33a7d73SArnd Bergmann static struct file_operations spufs_mfc_fops = {
1233a33a7d73SArnd Bergmann 	.open	 = spufs_mfc_open,
1234a33a7d73SArnd Bergmann 	.read	 = spufs_mfc_read,
1235a33a7d73SArnd Bergmann 	.write	 = spufs_mfc_write,
1236a33a7d73SArnd Bergmann 	.poll	 = spufs_mfc_poll,
1237a33a7d73SArnd Bergmann 	.flush	 = spufs_mfc_flush,
1238a33a7d73SArnd Bergmann 	.fsync	 = spufs_mfc_fsync,
1239a33a7d73SArnd Bergmann 	.fasync	 = spufs_mfc_fasync,
12406df10a82SMark Nutter 	.mmap	 = spufs_mfc_mmap,
1241a33a7d73SArnd Bergmann };
1242a33a7d73SArnd Bergmann 
124367207b96SArnd Bergmann static void spufs_npc_set(void *data, u64 val)
124467207b96SArnd Bergmann {
124567207b96SArnd Bergmann 	struct spu_context *ctx = data;
12468b3d6663SArnd Bergmann 	spu_acquire(ctx);
12478b3d6663SArnd Bergmann 	ctx->ops->npc_write(ctx, val);
12488b3d6663SArnd Bergmann 	spu_release(ctx);
124967207b96SArnd Bergmann }
125067207b96SArnd Bergmann 
125167207b96SArnd Bergmann static u64 spufs_npc_get(void *data)
125267207b96SArnd Bergmann {
125367207b96SArnd Bergmann 	struct spu_context *ctx = data;
125467207b96SArnd Bergmann 	u64 ret;
12558b3d6663SArnd Bergmann 	spu_acquire(ctx);
12568b3d6663SArnd Bergmann 	ret = ctx->ops->npc_read(ctx);
12578b3d6663SArnd Bergmann 	spu_release(ctx);
125867207b96SArnd Bergmann 	return ret;
125967207b96SArnd Bergmann }
126067207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
126167207b96SArnd Bergmann 
12628b3d6663SArnd Bergmann static void spufs_decr_set(void *data, u64 val)
12638b3d6663SArnd Bergmann {
12648b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12658b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12668b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12678b3d6663SArnd Bergmann 	lscsa->decr.slot[0] = (u32) val;
12688b3d6663SArnd Bergmann 	spu_release(ctx);
12698b3d6663SArnd Bergmann }
12708b3d6663SArnd Bergmann 
12718b3d6663SArnd Bergmann static u64 spufs_decr_get(void *data)
12728b3d6663SArnd Bergmann {
12738b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12748b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12758b3d6663SArnd Bergmann 	u64 ret;
12768b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12778b3d6663SArnd Bergmann 	ret = lscsa->decr.slot[0];
12788b3d6663SArnd Bergmann 	spu_release(ctx);
12798b3d6663SArnd Bergmann 	return ret;
12808b3d6663SArnd Bergmann }
12818b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
12828b3d6663SArnd Bergmann 			"%llx\n")
12838b3d6663SArnd Bergmann 
12848b3d6663SArnd Bergmann static void spufs_decr_status_set(void *data, u64 val)
12858b3d6663SArnd Bergmann {
12868b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12878b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12888b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12898b3d6663SArnd Bergmann 	lscsa->decr_status.slot[0] = (u32) val;
12908b3d6663SArnd Bergmann 	spu_release(ctx);
12918b3d6663SArnd Bergmann }
12928b3d6663SArnd Bergmann 
12938b3d6663SArnd Bergmann static u64 spufs_decr_status_get(void *data)
12948b3d6663SArnd Bergmann {
12958b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12968b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12978b3d6663SArnd Bergmann 	u64 ret;
12988b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12998b3d6663SArnd Bergmann 	ret = lscsa->decr_status.slot[0];
13008b3d6663SArnd Bergmann 	spu_release(ctx);
13018b3d6663SArnd Bergmann 	return ret;
13028b3d6663SArnd Bergmann }
13038b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
13048b3d6663SArnd Bergmann 			spufs_decr_status_set, "%llx\n")
13058b3d6663SArnd Bergmann 
13068b3d6663SArnd Bergmann static void spufs_spu_tag_mask_set(void *data, u64 val)
13078b3d6663SArnd Bergmann {
13088b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
13098b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13108b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13118b3d6663SArnd Bergmann 	lscsa->tag_mask.slot[0] = (u32) val;
13128b3d6663SArnd Bergmann 	spu_release(ctx);
13138b3d6663SArnd Bergmann }
13148b3d6663SArnd Bergmann 
13158b3d6663SArnd Bergmann static u64 spufs_spu_tag_mask_get(void *data)
13168b3d6663SArnd Bergmann {
13178b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
13188b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13198b3d6663SArnd Bergmann 	u64 ret;
13208b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13218b3d6663SArnd Bergmann 	ret = lscsa->tag_mask.slot[0];
13228b3d6663SArnd Bergmann 	spu_release(ctx);
13238b3d6663SArnd Bergmann 	return ret;
13248b3d6663SArnd Bergmann }
13258b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
13268b3d6663SArnd Bergmann 			spufs_spu_tag_mask_set, "%llx\n")
13278b3d6663SArnd Bergmann 
13288b3d6663SArnd Bergmann static void spufs_event_mask_set(void *data, u64 val)
13298b3d6663SArnd Bergmann {
13308b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
13318b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13328b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13338b3d6663SArnd Bergmann 	lscsa->event_mask.slot[0] = (u32) val;
13348b3d6663SArnd Bergmann 	spu_release(ctx);
13358b3d6663SArnd Bergmann }
13368b3d6663SArnd Bergmann 
13378b3d6663SArnd Bergmann static u64 spufs_event_mask_get(void *data)
13388b3d6663SArnd Bergmann {
13398b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
13408b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13418b3d6663SArnd Bergmann 	u64 ret;
13428b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13438b3d6663SArnd Bergmann 	ret = lscsa->event_mask.slot[0];
13448b3d6663SArnd Bergmann 	spu_release(ctx);
13458b3d6663SArnd Bergmann 	return ret;
13468b3d6663SArnd Bergmann }
13478b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
13488b3d6663SArnd Bergmann 			spufs_event_mask_set, "%llx\n")
13498b3d6663SArnd Bergmann 
13508b3d6663SArnd Bergmann static void spufs_srr0_set(void *data, u64 val)
13518b3d6663SArnd Bergmann {
13528b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
13538b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13548b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13558b3d6663SArnd Bergmann 	lscsa->srr0.slot[0] = (u32) val;
13568b3d6663SArnd Bergmann 	spu_release(ctx);
13578b3d6663SArnd Bergmann }
13588b3d6663SArnd Bergmann 
13598b3d6663SArnd Bergmann static u64 spufs_srr0_get(void *data)
13608b3d6663SArnd Bergmann {
13618b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
13628b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13638b3d6663SArnd Bergmann 	u64 ret;
13648b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13658b3d6663SArnd Bergmann 	ret = lscsa->srr0.slot[0];
13668b3d6663SArnd Bergmann 	spu_release(ctx);
13678b3d6663SArnd Bergmann 	return ret;
13688b3d6663SArnd Bergmann }
13698b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
13708b3d6663SArnd Bergmann 			"%llx\n")
13718b3d6663SArnd Bergmann 
13727b1a7014Sarnd@arndb.de static u64 spufs_id_get(void *data)
13737b1a7014Sarnd@arndb.de {
13747b1a7014Sarnd@arndb.de 	struct spu_context *ctx = data;
13757b1a7014Sarnd@arndb.de 	u64 num;
13767b1a7014Sarnd@arndb.de 
13777b1a7014Sarnd@arndb.de 	spu_acquire(ctx);
13787b1a7014Sarnd@arndb.de 	if (ctx->state == SPU_STATE_RUNNABLE)
13797b1a7014Sarnd@arndb.de 		num = ctx->spu->number;
13807b1a7014Sarnd@arndb.de 	else
13817b1a7014Sarnd@arndb.de 		num = (unsigned int)-1;
13827b1a7014Sarnd@arndb.de 	spu_release(ctx);
13837b1a7014Sarnd@arndb.de 
13847b1a7014Sarnd@arndb.de 	return num;
13857b1a7014Sarnd@arndb.de }
1386e45d6634SAl Viro DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n")
13877b1a7014Sarnd@arndb.de 
138867207b96SArnd Bergmann struct tree_descr spufs_dir_contents[] = {
138967207b96SArnd Bergmann 	{ "mem",  &spufs_mem_fops,  0666, },
13908b3d6663SArnd Bergmann 	{ "regs", &spufs_regs_fops,  0666, },
139167207b96SArnd Bergmann 	{ "mbox", &spufs_mbox_fops, 0444, },
139267207b96SArnd Bergmann 	{ "ibox", &spufs_ibox_fops, 0444, },
139367207b96SArnd Bergmann 	{ "wbox", &spufs_wbox_fops, 0222, },
139467207b96SArnd Bergmann 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, },
139567207b96SArnd Bergmann 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, },
139667207b96SArnd Bergmann 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, },
139767207b96SArnd Bergmann 	{ "signal1", &spufs_signal1_fops, 0666, },
139867207b96SArnd Bergmann 	{ "signal2", &spufs_signal2_fops, 0666, },
139967207b96SArnd Bergmann 	{ "signal1_type", &spufs_signal1_type, 0666, },
140067207b96SArnd Bergmann 	{ "signal2_type", &spufs_signal2_type, 0666, },
1401d9379c4bSarnd@arndb.de 	{ "mss", &spufs_mss_fops, 0666, },
1402a33a7d73SArnd Bergmann 	{ "mfc", &spufs_mfc_fops, 0666, },
14036df10a82SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
140467207b96SArnd Bergmann 	{ "npc", &spufs_npc_ops, 0666, },
14058b3d6663SArnd Bergmann 	{ "fpcr", &spufs_fpcr_fops, 0666, },
14068b3d6663SArnd Bergmann 	{ "decr", &spufs_decr_ops, 0666, },
14078b3d6663SArnd Bergmann 	{ "decr_status", &spufs_decr_status_ops, 0666, },
14088b3d6663SArnd Bergmann 	{ "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
14098b3d6663SArnd Bergmann 	{ "event_mask", &spufs_event_mask_ops, 0666, },
14108b3d6663SArnd Bergmann 	{ "srr0", &spufs_srr0_ops, 0666, },
14117b1a7014Sarnd@arndb.de 	{ "phys-id", &spufs_id_ops, 0666, },
141227d5bf2aSBenjamin Herrenschmidt 	{ "psmap", &spufs_psmap_fops, 0666, },
141367207b96SArnd Bergmann 	{},
141467207b96SArnd Bergmann };
1415