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 
398b3d6663SArnd Bergmann 
4067207b96SArnd Bergmann static int
4167207b96SArnd Bergmann spufs_mem_open(struct inode *inode, struct file *file)
4267207b96SArnd Bergmann {
4367207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
446df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
456df10a82SMark Nutter 	file->private_data = ctx;
466df10a82SMark Nutter 	file->f_mapping = inode->i_mapping;
476df10a82SMark Nutter 	ctx->local_store = inode->i_mapping;
4867207b96SArnd Bergmann 	return 0;
4967207b96SArnd Bergmann }
5067207b96SArnd Bergmann 
5167207b96SArnd Bergmann static ssize_t
5267207b96SArnd Bergmann spufs_mem_read(struct file *file, char __user *buffer,
5367207b96SArnd Bergmann 				size_t size, loff_t *pos)
5467207b96SArnd Bergmann {
558b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
568b3d6663SArnd Bergmann 	char *local_store;
5767207b96SArnd Bergmann 	int ret;
5867207b96SArnd Bergmann 
598b3d6663SArnd Bergmann 	spu_acquire(ctx);
6067207b96SArnd Bergmann 
618b3d6663SArnd Bergmann 	local_store = ctx->ops->get_ls(ctx);
628b3d6663SArnd Bergmann 	ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE);
6367207b96SArnd Bergmann 
648b3d6663SArnd Bergmann 	spu_release(ctx);
6567207b96SArnd Bergmann 	return ret;
6667207b96SArnd Bergmann }
6767207b96SArnd Bergmann 
6867207b96SArnd Bergmann static ssize_t
6967207b96SArnd Bergmann spufs_mem_write(struct file *file, const char __user *buffer,
7067207b96SArnd Bergmann 					size_t size, loff_t *pos)
7167207b96SArnd Bergmann {
7267207b96SArnd Bergmann 	struct spu_context *ctx = file->private_data;
738b3d6663SArnd Bergmann 	char *local_store;
748b3d6663SArnd Bergmann 	int ret;
7567207b96SArnd Bergmann 
7667207b96SArnd Bergmann 	size = min_t(ssize_t, LS_SIZE - *pos, size);
7767207b96SArnd Bergmann 	if (size <= 0)
7867207b96SArnd Bergmann 		return -EFBIG;
7967207b96SArnd Bergmann 	*pos += size;
808b3d6663SArnd Bergmann 
818b3d6663SArnd Bergmann 	spu_acquire(ctx);
828b3d6663SArnd Bergmann 
838b3d6663SArnd Bergmann 	local_store = ctx->ops->get_ls(ctx);
848b3d6663SArnd Bergmann 	ret = copy_from_user(local_store + *pos - size,
8567207b96SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
868b3d6663SArnd Bergmann 
878b3d6663SArnd Bergmann 	spu_release(ctx);
888b3d6663SArnd Bergmann 	return ret;
8967207b96SArnd Bergmann }
9067207b96SArnd Bergmann 
916df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
928b3d6663SArnd Bergmann static struct page *
938b3d6663SArnd Bergmann spufs_mem_mmap_nopage(struct vm_area_struct *vma,
948b3d6663SArnd Bergmann 		      unsigned long address, int *type)
958b3d6663SArnd Bergmann {
968b3d6663SArnd Bergmann 	struct page *page = NOPAGE_SIGBUS;
978b3d6663SArnd Bergmann 
988b3d6663SArnd Bergmann 	struct spu_context *ctx = vma->vm_file->private_data;
998b3d6663SArnd Bergmann 	unsigned long offset = address - vma->vm_start;
1008b3d6663SArnd Bergmann 	offset += vma->vm_pgoff << PAGE_SHIFT;
1018b3d6663SArnd Bergmann 
1028b3d6663SArnd Bergmann 	spu_acquire(ctx);
1038b3d6663SArnd Bergmann 
1048b3d6663SArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED)
1058b3d6663SArnd Bergmann 		page = vmalloc_to_page(ctx->csa.lscsa->ls + offset);
1068b3d6663SArnd Bergmann 	else
1078b3d6663SArnd Bergmann 		page = pfn_to_page((ctx->spu->local_store_phys + offset)
1088b3d6663SArnd Bergmann 				   >> PAGE_SHIFT);
1098b3d6663SArnd Bergmann 
1108b3d6663SArnd Bergmann 	spu_release(ctx);
1118b3d6663SArnd Bergmann 
1128b3d6663SArnd Bergmann 	if (type)
1138b3d6663SArnd Bergmann 		*type = VM_FAULT_MINOR;
1148b3d6663SArnd Bergmann 
115d88cfffaSArnd Bergmann 	page_cache_get(page);
1168b3d6663SArnd Bergmann 	return page;
1178b3d6663SArnd Bergmann }
1188b3d6663SArnd Bergmann 
1198b3d6663SArnd Bergmann static struct vm_operations_struct spufs_mem_mmap_vmops = {
1208b3d6663SArnd Bergmann 	.nopage = spufs_mem_mmap_nopage,
1218b3d6663SArnd Bergmann };
1228b3d6663SArnd Bergmann 
12367207b96SArnd Bergmann static int
12467207b96SArnd Bergmann spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
12567207b96SArnd Bergmann {
1268b3d6663SArnd Bergmann 	if (!(vma->vm_flags & VM_SHARED))
1278b3d6663SArnd Bergmann 		return -EINVAL;
12867207b96SArnd Bergmann 
1298b3d6663SArnd Bergmann 	/* FIXME: */
13067207b96SArnd Bergmann 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
13167207b96SArnd Bergmann 				     | _PAGE_NO_CACHE);
1328b3d6663SArnd Bergmann 
1338b3d6663SArnd Bergmann 	vma->vm_ops = &spufs_mem_mmap_vmops;
13467207b96SArnd Bergmann 	return 0;
13567207b96SArnd Bergmann }
1368b3d6663SArnd Bergmann #endif
13767207b96SArnd Bergmann 
13867207b96SArnd Bergmann static struct file_operations spufs_mem_fops = {
13967207b96SArnd Bergmann 	.open	 = spufs_mem_open,
14067207b96SArnd Bergmann 	.read    = spufs_mem_read,
14167207b96SArnd Bergmann 	.write   = spufs_mem_write,
1428b3d6663SArnd Bergmann 	.llseek  = generic_file_llseek,
1436df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
14467207b96SArnd Bergmann 	.mmap    = spufs_mem_mmap,
1458b3d6663SArnd Bergmann #endif
1468b3d6663SArnd Bergmann };
1478b3d6663SArnd Bergmann 
1486df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
1496df10a82SMark Nutter static struct page *spufs_ps_nopage(struct vm_area_struct *vma,
1506df10a82SMark Nutter 				    unsigned long address,
1516df10a82SMark Nutter 				    int *type, unsigned long ps_offs)
1526df10a82SMark Nutter {
1536df10a82SMark Nutter 	struct page *page = NOPAGE_SIGBUS;
1546df10a82SMark Nutter 	int fault_type = VM_FAULT_SIGBUS;
1556df10a82SMark Nutter 	struct spu_context *ctx = vma->vm_file->private_data;
1566df10a82SMark Nutter 	unsigned long offset = address - vma->vm_start;
1576df10a82SMark Nutter 	unsigned long area;
1586df10a82SMark Nutter 	int ret;
1596df10a82SMark Nutter 
1606df10a82SMark Nutter 	offset += vma->vm_pgoff << PAGE_SHIFT;
1616df10a82SMark Nutter 	if (offset >= 0x4000)
1626df10a82SMark Nutter 		goto out;
1636df10a82SMark Nutter 
1646df10a82SMark Nutter 	ret = spu_acquire_runnable(ctx);
1656df10a82SMark Nutter 	if (ret)
1666df10a82SMark Nutter 		goto out;
1676df10a82SMark Nutter 
1686df10a82SMark Nutter 	area = ctx->spu->problem_phys + ps_offs;
1696df10a82SMark Nutter 	page = pfn_to_page((area + offset) >> PAGE_SHIFT);
1706df10a82SMark Nutter 	fault_type = VM_FAULT_MINOR;
1716df10a82SMark Nutter 	page_cache_get(page);
1726df10a82SMark Nutter 
1736df10a82SMark Nutter 	spu_release(ctx);
1746df10a82SMark Nutter 
1756df10a82SMark Nutter       out:
1766df10a82SMark Nutter 	if (type)
1776df10a82SMark Nutter 		*type = fault_type;
1786df10a82SMark Nutter 
1796df10a82SMark Nutter 	return page;
1806df10a82SMark Nutter }
1816df10a82SMark Nutter 
1826df10a82SMark Nutter static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma,
1836df10a82SMark Nutter 					   unsigned long address, int *type)
1846df10a82SMark Nutter {
1856df10a82SMark Nutter 	return spufs_ps_nopage(vma, address, type, 0x4000);
1866df10a82SMark Nutter }
1876df10a82SMark Nutter 
1886df10a82SMark Nutter static struct vm_operations_struct spufs_cntl_mmap_vmops = {
1896df10a82SMark Nutter 	.nopage = spufs_cntl_mmap_nopage,
1906df10a82SMark Nutter };
1916df10a82SMark Nutter 
1926df10a82SMark Nutter /*
1936df10a82SMark Nutter  * mmap support for problem state control area [0x4000 - 0x4fff].
1946df10a82SMark Nutter  * Mapping this area requires that the application have CAP_SYS_RAWIO,
1956df10a82SMark Nutter  * as these registers require special care when read/writing.
1966df10a82SMark Nutter  */
1976df10a82SMark Nutter static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
1986df10a82SMark Nutter {
1996df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
2006df10a82SMark Nutter 		return -EINVAL;
2016df10a82SMark Nutter 
2026df10a82SMark Nutter 	if (!capable(CAP_SYS_RAWIO))
2036df10a82SMark Nutter 		return -EPERM;
2046df10a82SMark Nutter 
2056df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
2066df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
2076df10a82SMark Nutter 				     | _PAGE_NO_CACHE);
2086df10a82SMark Nutter 
2096df10a82SMark Nutter 	vma->vm_ops = &spufs_cntl_mmap_vmops;
2106df10a82SMark Nutter 	return 0;
2116df10a82SMark Nutter }
2126df10a82SMark Nutter #endif
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 #ifdef CONFIG_SPUFS_MMAP
2466df10a82SMark Nutter 	.mmap = spufs_cntl_mmap,
2476df10a82SMark Nutter #endif
2486df10a82SMark Nutter };
2496df10a82SMark Nutter 
2508b3d6663SArnd Bergmann static int
2518b3d6663SArnd Bergmann spufs_regs_open(struct inode *inode, struct file *file)
2528b3d6663SArnd Bergmann {
2538b3d6663SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
2548b3d6663SArnd Bergmann 	file->private_data = i->i_ctx;
2558b3d6663SArnd Bergmann 	return 0;
2568b3d6663SArnd Bergmann }
2578b3d6663SArnd Bergmann 
2588b3d6663SArnd Bergmann static ssize_t
2598b3d6663SArnd Bergmann spufs_regs_read(struct file *file, char __user *buffer,
2608b3d6663SArnd Bergmann 		size_t size, loff_t *pos)
2618b3d6663SArnd Bergmann {
2628b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
2638b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
2648b3d6663SArnd Bergmann 	int ret;
2658b3d6663SArnd Bergmann 
2668b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
2678b3d6663SArnd Bergmann 
2688b3d6663SArnd Bergmann 	ret = simple_read_from_buffer(buffer, size, pos,
2698b3d6663SArnd Bergmann 				      lscsa->gprs, sizeof lscsa->gprs);
2708b3d6663SArnd Bergmann 
2718b3d6663SArnd Bergmann 	spu_release(ctx);
2728b3d6663SArnd Bergmann 	return ret;
2738b3d6663SArnd Bergmann }
2748b3d6663SArnd Bergmann 
2758b3d6663SArnd Bergmann static ssize_t
2768b3d6663SArnd Bergmann spufs_regs_write(struct file *file, const char __user *buffer,
2778b3d6663SArnd Bergmann 		 size_t size, loff_t *pos)
2788b3d6663SArnd Bergmann {
2798b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
2808b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
2818b3d6663SArnd Bergmann 	int ret;
2828b3d6663SArnd Bergmann 
2838b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
2848b3d6663SArnd Bergmann 	if (size <= 0)
2858b3d6663SArnd Bergmann 		return -EFBIG;
2868b3d6663SArnd Bergmann 	*pos += size;
2878b3d6663SArnd Bergmann 
2888b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
2898b3d6663SArnd Bergmann 
2908b3d6663SArnd Bergmann 	ret = copy_from_user(lscsa->gprs + *pos - size,
2918b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
2928b3d6663SArnd Bergmann 
2938b3d6663SArnd Bergmann 	spu_release(ctx);
2948b3d6663SArnd Bergmann 	return ret;
2958b3d6663SArnd Bergmann }
2968b3d6663SArnd Bergmann 
2978b3d6663SArnd Bergmann static struct file_operations spufs_regs_fops = {
2988b3d6663SArnd Bergmann 	.open	 = spufs_regs_open,
2998b3d6663SArnd Bergmann 	.read    = spufs_regs_read,
3008b3d6663SArnd Bergmann 	.write   = spufs_regs_write,
3018b3d6663SArnd Bergmann 	.llseek  = generic_file_llseek,
3028b3d6663SArnd Bergmann };
3038b3d6663SArnd Bergmann 
3048b3d6663SArnd Bergmann static ssize_t
3058b3d6663SArnd Bergmann spufs_fpcr_read(struct file *file, char __user * buffer,
3068b3d6663SArnd Bergmann 		size_t size, loff_t * pos)
3078b3d6663SArnd Bergmann {
3088b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
3098b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
3108b3d6663SArnd Bergmann 	int ret;
3118b3d6663SArnd Bergmann 
3128b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
3138b3d6663SArnd Bergmann 
3148b3d6663SArnd Bergmann 	ret = simple_read_from_buffer(buffer, size, pos,
3158b3d6663SArnd Bergmann 				      &lscsa->fpcr, sizeof(lscsa->fpcr));
3168b3d6663SArnd Bergmann 
3178b3d6663SArnd Bergmann 	spu_release(ctx);
3188b3d6663SArnd Bergmann 	return ret;
3198b3d6663SArnd Bergmann }
3208b3d6663SArnd Bergmann 
3218b3d6663SArnd Bergmann static ssize_t
3228b3d6663SArnd Bergmann spufs_fpcr_write(struct file *file, const char __user * buffer,
3238b3d6663SArnd Bergmann 		 size_t size, loff_t * pos)
3248b3d6663SArnd Bergmann {
3258b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
3268b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
3278b3d6663SArnd Bergmann 	int ret;
3288b3d6663SArnd Bergmann 
3298b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
3308b3d6663SArnd Bergmann 	if (size <= 0)
3318b3d6663SArnd Bergmann 		return -EFBIG;
3328b3d6663SArnd Bergmann 	*pos += size;
3338b3d6663SArnd Bergmann 
3348b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
3358b3d6663SArnd Bergmann 
3368b3d6663SArnd Bergmann 	ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
3378b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
3388b3d6663SArnd Bergmann 
3398b3d6663SArnd Bergmann 	spu_release(ctx);
3408b3d6663SArnd Bergmann 	return ret;
3418b3d6663SArnd Bergmann }
3428b3d6663SArnd Bergmann 
3438b3d6663SArnd Bergmann static struct file_operations spufs_fpcr_fops = {
3448b3d6663SArnd Bergmann 	.open = spufs_regs_open,
3458b3d6663SArnd Bergmann 	.read = spufs_fpcr_read,
3468b3d6663SArnd Bergmann 	.write = spufs_fpcr_write,
34767207b96SArnd Bergmann 	.llseek = generic_file_llseek,
34867207b96SArnd Bergmann };
34967207b96SArnd Bergmann 
35067207b96SArnd Bergmann /* generic open function for all pipe-like files */
35167207b96SArnd Bergmann static int spufs_pipe_open(struct inode *inode, struct file *file)
35267207b96SArnd Bergmann {
35367207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
35467207b96SArnd Bergmann 	file->private_data = i->i_ctx;
35567207b96SArnd Bergmann 
35667207b96SArnd Bergmann 	return nonseekable_open(inode, file);
35767207b96SArnd Bergmann }
35867207b96SArnd Bergmann 
35967207b96SArnd Bergmann static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
36067207b96SArnd Bergmann 			size_t len, loff_t *pos)
36167207b96SArnd Bergmann {
3628b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
36367207b96SArnd Bergmann 	u32 mbox_data;
3648b3d6663SArnd Bergmann 	int ret;
36567207b96SArnd Bergmann 
36667207b96SArnd Bergmann 	if (len < 4)
36767207b96SArnd Bergmann 		return -EINVAL;
36867207b96SArnd Bergmann 
3698b3d6663SArnd Bergmann 	spu_acquire(ctx);
3708b3d6663SArnd Bergmann 	ret = ctx->ops->mbox_read(ctx, &mbox_data);
3718b3d6663SArnd Bergmann 	spu_release(ctx);
37267207b96SArnd Bergmann 
3738b3d6663SArnd Bergmann 	if (!ret)
3748b3d6663SArnd Bergmann 		return -EAGAIN;
37567207b96SArnd Bergmann 
37667207b96SArnd Bergmann 	if (copy_to_user(buf, &mbox_data, sizeof mbox_data))
37767207b96SArnd Bergmann 		return -EFAULT;
37867207b96SArnd Bergmann 
37967207b96SArnd Bergmann 	return 4;
38067207b96SArnd Bergmann }
38167207b96SArnd Bergmann 
38267207b96SArnd Bergmann static struct file_operations spufs_mbox_fops = {
38367207b96SArnd Bergmann 	.open	= spufs_pipe_open,
38467207b96SArnd Bergmann 	.read	= spufs_mbox_read,
38567207b96SArnd Bergmann };
38667207b96SArnd Bergmann 
38767207b96SArnd Bergmann static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
38867207b96SArnd Bergmann 			size_t len, loff_t *pos)
38967207b96SArnd Bergmann {
3908b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
39167207b96SArnd Bergmann 	u32 mbox_stat;
39267207b96SArnd Bergmann 
39367207b96SArnd Bergmann 	if (len < 4)
39467207b96SArnd Bergmann 		return -EINVAL;
39567207b96SArnd Bergmann 
3968b3d6663SArnd Bergmann 	spu_acquire(ctx);
3978b3d6663SArnd Bergmann 
3988b3d6663SArnd Bergmann 	mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
3998b3d6663SArnd Bergmann 
4008b3d6663SArnd Bergmann 	spu_release(ctx);
40167207b96SArnd Bergmann 
40267207b96SArnd Bergmann 	if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
40367207b96SArnd Bergmann 		return -EFAULT;
40467207b96SArnd Bergmann 
40567207b96SArnd Bergmann 	return 4;
40667207b96SArnd Bergmann }
40767207b96SArnd Bergmann 
40867207b96SArnd Bergmann static struct file_operations spufs_mbox_stat_fops = {
40967207b96SArnd Bergmann 	.open	= spufs_pipe_open,
41067207b96SArnd Bergmann 	.read	= spufs_mbox_stat_read,
41167207b96SArnd Bergmann };
41267207b96SArnd Bergmann 
41367207b96SArnd Bergmann /* low-level ibox access function */
4148b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
41567207b96SArnd Bergmann {
4168b3d6663SArnd Bergmann 	return ctx->ops->ibox_read(ctx, data);
41767207b96SArnd Bergmann }
41867207b96SArnd Bergmann 
41967207b96SArnd Bergmann static int spufs_ibox_fasync(int fd, struct file *file, int on)
42067207b96SArnd Bergmann {
4218b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
4228b3d6663SArnd Bergmann 
4238b3d6663SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->ibox_fasync);
4248b3d6663SArnd Bergmann }
4258b3d6663SArnd Bergmann 
4268b3d6663SArnd Bergmann /* interrupt-level ibox callback function. */
4278b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu)
4288b3d6663SArnd Bergmann {
4298b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
4308b3d6663SArnd Bergmann 
4318b3d6663SArnd Bergmann 	wake_up_all(&ctx->ibox_wq);
4328b3d6663SArnd Bergmann 	kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
43367207b96SArnd Bergmann }
43467207b96SArnd Bergmann 
43567207b96SArnd Bergmann static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
43667207b96SArnd Bergmann 			size_t len, loff_t *pos)
43767207b96SArnd Bergmann {
4388b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
43967207b96SArnd Bergmann 	u32 ibox_data;
44067207b96SArnd Bergmann 	ssize_t ret;
44167207b96SArnd Bergmann 
44267207b96SArnd Bergmann 	if (len < 4)
44367207b96SArnd Bergmann 		return -EINVAL;
44467207b96SArnd Bergmann 
4458b3d6663SArnd Bergmann 	spu_acquire(ctx);
44667207b96SArnd Bergmann 
44767207b96SArnd Bergmann 	ret = 0;
44867207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
4498b3d6663SArnd Bergmann 		if (!spu_ibox_read(ctx, &ibox_data))
45067207b96SArnd Bergmann 			ret = -EAGAIN;
45167207b96SArnd Bergmann 	} else {
4528b3d6663SArnd Bergmann 		ret = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
45367207b96SArnd Bergmann 	}
45467207b96SArnd Bergmann 
4558b3d6663SArnd Bergmann 	spu_release(ctx);
4568b3d6663SArnd Bergmann 
45767207b96SArnd Bergmann 	if (ret)
45867207b96SArnd Bergmann 		return ret;
45967207b96SArnd Bergmann 
46067207b96SArnd Bergmann 	ret = 4;
46167207b96SArnd Bergmann 	if (copy_to_user(buf, &ibox_data, sizeof ibox_data))
46267207b96SArnd Bergmann 		ret = -EFAULT;
46367207b96SArnd Bergmann 
46467207b96SArnd Bergmann 	return ret;
46567207b96SArnd Bergmann }
46667207b96SArnd Bergmann 
46767207b96SArnd Bergmann static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
46867207b96SArnd Bergmann {
4698b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
47067207b96SArnd Bergmann 	unsigned int mask;
47167207b96SArnd Bergmann 
4728b3d6663SArnd Bergmann 	poll_wait(file, &ctx->ibox_wq, wait);
47367207b96SArnd Bergmann 
4743a843d7cSArnd Bergmann 	spu_acquire(ctx);
4753a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
4763a843d7cSArnd Bergmann 	spu_release(ctx);
47767207b96SArnd Bergmann 
47867207b96SArnd Bergmann 	return mask;
47967207b96SArnd Bergmann }
48067207b96SArnd Bergmann 
48167207b96SArnd Bergmann static struct file_operations spufs_ibox_fops = {
48267207b96SArnd Bergmann 	.open	= spufs_pipe_open,
48367207b96SArnd Bergmann 	.read	= spufs_ibox_read,
48467207b96SArnd Bergmann 	.poll	= spufs_ibox_poll,
48567207b96SArnd Bergmann 	.fasync	= spufs_ibox_fasync,
48667207b96SArnd Bergmann };
48767207b96SArnd Bergmann 
48867207b96SArnd Bergmann static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
48967207b96SArnd Bergmann 			size_t len, loff_t *pos)
49067207b96SArnd Bergmann {
4918b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
49267207b96SArnd Bergmann 	u32 ibox_stat;
49367207b96SArnd Bergmann 
49467207b96SArnd Bergmann 	if (len < 4)
49567207b96SArnd Bergmann 		return -EINVAL;
49667207b96SArnd Bergmann 
4978b3d6663SArnd Bergmann 	spu_acquire(ctx);
4988b3d6663SArnd Bergmann 	ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
4998b3d6663SArnd Bergmann 	spu_release(ctx);
50067207b96SArnd Bergmann 
50167207b96SArnd Bergmann 	if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
50267207b96SArnd Bergmann 		return -EFAULT;
50367207b96SArnd Bergmann 
50467207b96SArnd Bergmann 	return 4;
50567207b96SArnd Bergmann }
50667207b96SArnd Bergmann 
50767207b96SArnd Bergmann static struct file_operations spufs_ibox_stat_fops = {
50867207b96SArnd Bergmann 	.open	= spufs_pipe_open,
50967207b96SArnd Bergmann 	.read	= spufs_ibox_stat_read,
51067207b96SArnd Bergmann };
51167207b96SArnd Bergmann 
51267207b96SArnd Bergmann /* low-level mailbox write */
5138b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data)
51467207b96SArnd Bergmann {
5158b3d6663SArnd Bergmann 	return ctx->ops->wbox_write(ctx, data);
51667207b96SArnd Bergmann }
51767207b96SArnd Bergmann 
51867207b96SArnd Bergmann static int spufs_wbox_fasync(int fd, struct file *file, int on)
51967207b96SArnd Bergmann {
5208b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
5218b3d6663SArnd Bergmann 	int ret;
5228b3d6663SArnd Bergmann 
5238b3d6663SArnd Bergmann 	ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
5248b3d6663SArnd Bergmann 
5258b3d6663SArnd Bergmann 	return ret;
5268b3d6663SArnd Bergmann }
5278b3d6663SArnd Bergmann 
5288b3d6663SArnd Bergmann /* interrupt-level wbox callback function. */
5298b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu)
5308b3d6663SArnd Bergmann {
5318b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
5328b3d6663SArnd Bergmann 
5338b3d6663SArnd Bergmann 	wake_up_all(&ctx->wbox_wq);
5348b3d6663SArnd Bergmann 	kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
53567207b96SArnd Bergmann }
53667207b96SArnd Bergmann 
53767207b96SArnd Bergmann static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
53867207b96SArnd Bergmann 			size_t len, loff_t *pos)
53967207b96SArnd Bergmann {
5408b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
54167207b96SArnd Bergmann 	u32 wbox_data;
54267207b96SArnd Bergmann 	int ret;
54367207b96SArnd Bergmann 
54467207b96SArnd Bergmann 	if (len < 4)
54567207b96SArnd Bergmann 		return -EINVAL;
54667207b96SArnd Bergmann 
54767207b96SArnd Bergmann 	if (copy_from_user(&wbox_data, buf, sizeof wbox_data))
54867207b96SArnd Bergmann 		return -EFAULT;
54967207b96SArnd Bergmann 
5508b3d6663SArnd Bergmann 	spu_acquire(ctx);
5518b3d6663SArnd Bergmann 
55267207b96SArnd Bergmann 	ret = 0;
55367207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
5548b3d6663SArnd Bergmann 		if (!spu_wbox_write(ctx, wbox_data))
55567207b96SArnd Bergmann 			ret = -EAGAIN;
55667207b96SArnd Bergmann 	} else {
5578b3d6663SArnd Bergmann 		ret = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
55867207b96SArnd Bergmann 	}
55967207b96SArnd Bergmann 
5608b3d6663SArnd Bergmann 	spu_release(ctx);
5618b3d6663SArnd Bergmann 
56267207b96SArnd Bergmann 	return ret ? ret : sizeof wbox_data;
56367207b96SArnd Bergmann }
56467207b96SArnd Bergmann 
56567207b96SArnd Bergmann static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
56667207b96SArnd Bergmann {
5678b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
56867207b96SArnd Bergmann 	unsigned int mask;
56967207b96SArnd Bergmann 
5708b3d6663SArnd Bergmann 	poll_wait(file, &ctx->wbox_wq, wait);
57167207b96SArnd Bergmann 
5723a843d7cSArnd Bergmann 	spu_acquire(ctx);
5733a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
5743a843d7cSArnd Bergmann 	spu_release(ctx);
57567207b96SArnd Bergmann 
57667207b96SArnd Bergmann 	return mask;
57767207b96SArnd Bergmann }
57867207b96SArnd Bergmann 
57967207b96SArnd Bergmann static struct file_operations spufs_wbox_fops = {
58067207b96SArnd Bergmann 	.open	= spufs_pipe_open,
58167207b96SArnd Bergmann 	.write	= spufs_wbox_write,
58267207b96SArnd Bergmann 	.poll	= spufs_wbox_poll,
58367207b96SArnd Bergmann 	.fasync	= spufs_wbox_fasync,
58467207b96SArnd Bergmann };
58567207b96SArnd Bergmann 
58667207b96SArnd Bergmann static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
58767207b96SArnd Bergmann 			size_t len, loff_t *pos)
58867207b96SArnd Bergmann {
5898b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
59067207b96SArnd Bergmann 	u32 wbox_stat;
59167207b96SArnd Bergmann 
59267207b96SArnd Bergmann 	if (len < 4)
59367207b96SArnd Bergmann 		return -EINVAL;
59467207b96SArnd Bergmann 
5958b3d6663SArnd Bergmann 	spu_acquire(ctx);
5968b3d6663SArnd Bergmann 	wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
5978b3d6663SArnd Bergmann 	spu_release(ctx);
59867207b96SArnd Bergmann 
59967207b96SArnd Bergmann 	if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
60067207b96SArnd Bergmann 		return -EFAULT;
60167207b96SArnd Bergmann 
60267207b96SArnd Bergmann 	return 4;
60367207b96SArnd Bergmann }
60467207b96SArnd Bergmann 
60567207b96SArnd Bergmann static struct file_operations spufs_wbox_stat_fops = {
60667207b96SArnd Bergmann 	.open	= spufs_pipe_open,
60767207b96SArnd Bergmann 	.read	= spufs_wbox_stat_read,
60867207b96SArnd Bergmann };
60967207b96SArnd Bergmann 
6106df10a82SMark Nutter static int spufs_signal1_open(struct inode *inode, struct file *file)
6116df10a82SMark Nutter {
6126df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
6136df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
6146df10a82SMark Nutter 	file->private_data = ctx;
6156df10a82SMark Nutter 	file->f_mapping = inode->i_mapping;
6166df10a82SMark Nutter 	ctx->signal1 = inode->i_mapping;
6176df10a82SMark Nutter 	return nonseekable_open(inode, file);
6186df10a82SMark Nutter }
6196df10a82SMark Nutter 
62067207b96SArnd Bergmann static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
62167207b96SArnd Bergmann 			size_t len, loff_t *pos)
62267207b96SArnd Bergmann {
6238b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
62467207b96SArnd Bergmann 	u32 data;
62567207b96SArnd Bergmann 
62667207b96SArnd Bergmann 	if (len < 4)
62767207b96SArnd Bergmann 		return -EINVAL;
62867207b96SArnd Bergmann 
6298b3d6663SArnd Bergmann 	spu_acquire(ctx);
6308b3d6663SArnd Bergmann 	data = ctx->ops->signal1_read(ctx);
6318b3d6663SArnd Bergmann 	spu_release(ctx);
6328b3d6663SArnd Bergmann 
63367207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
63467207b96SArnd Bergmann 		return -EFAULT;
63567207b96SArnd Bergmann 
63667207b96SArnd Bergmann 	return 4;
63767207b96SArnd Bergmann }
63867207b96SArnd Bergmann 
63967207b96SArnd Bergmann static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
64067207b96SArnd Bergmann 			size_t len, loff_t *pos)
64167207b96SArnd Bergmann {
64267207b96SArnd Bergmann 	struct spu_context *ctx;
64367207b96SArnd Bergmann 	u32 data;
64467207b96SArnd Bergmann 
64567207b96SArnd Bergmann 	ctx = file->private_data;
64667207b96SArnd Bergmann 
64767207b96SArnd Bergmann 	if (len < 4)
64867207b96SArnd Bergmann 		return -EINVAL;
64967207b96SArnd Bergmann 
65067207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
65167207b96SArnd Bergmann 		return -EFAULT;
65267207b96SArnd Bergmann 
6538b3d6663SArnd Bergmann 	spu_acquire(ctx);
6548b3d6663SArnd Bergmann 	ctx->ops->signal1_write(ctx, data);
6558b3d6663SArnd Bergmann 	spu_release(ctx);
65667207b96SArnd Bergmann 
65767207b96SArnd Bergmann 	return 4;
65867207b96SArnd Bergmann }
65967207b96SArnd Bergmann 
6606df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
6616df10a82SMark Nutter static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma,
6626df10a82SMark Nutter 					      unsigned long address, int *type)
6636df10a82SMark Nutter {
6646df10a82SMark Nutter 	return spufs_ps_nopage(vma, address, type, 0x14000);
6656df10a82SMark Nutter }
6666df10a82SMark Nutter 
6676df10a82SMark Nutter static struct vm_operations_struct spufs_signal1_mmap_vmops = {
6686df10a82SMark Nutter 	.nopage = spufs_signal1_mmap_nopage,
6696df10a82SMark Nutter };
6706df10a82SMark Nutter 
6716df10a82SMark Nutter static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
6726df10a82SMark Nutter {
6736df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
6746df10a82SMark Nutter 		return -EINVAL;
6756df10a82SMark Nutter 
6766df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
6776df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
6786df10a82SMark Nutter 				     | _PAGE_NO_CACHE);
6796df10a82SMark Nutter 
6806df10a82SMark Nutter 	vma->vm_ops = &spufs_signal1_mmap_vmops;
6816df10a82SMark Nutter 	return 0;
6826df10a82SMark Nutter }
6836df10a82SMark Nutter #endif
6846df10a82SMark Nutter 
68567207b96SArnd Bergmann static struct file_operations spufs_signal1_fops = {
6866df10a82SMark Nutter 	.open = spufs_signal1_open,
68767207b96SArnd Bergmann 	.read = spufs_signal1_read,
68867207b96SArnd Bergmann 	.write = spufs_signal1_write,
6896df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
6906df10a82SMark Nutter 	.mmap = spufs_signal1_mmap,
6916df10a82SMark Nutter #endif
69267207b96SArnd Bergmann };
69367207b96SArnd Bergmann 
6946df10a82SMark Nutter static int spufs_signal2_open(struct inode *inode, struct file *file)
6956df10a82SMark Nutter {
6966df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
6976df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
6986df10a82SMark Nutter 	file->private_data = ctx;
6996df10a82SMark Nutter 	file->f_mapping = inode->i_mapping;
7006df10a82SMark Nutter 	ctx->signal2 = inode->i_mapping;
7016df10a82SMark Nutter 	return nonseekable_open(inode, file);
7026df10a82SMark Nutter }
7036df10a82SMark Nutter 
70467207b96SArnd Bergmann static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
70567207b96SArnd Bergmann 			size_t len, loff_t *pos)
70667207b96SArnd Bergmann {
70767207b96SArnd Bergmann 	struct spu_context *ctx;
70867207b96SArnd Bergmann 	u32 data;
70967207b96SArnd Bergmann 
71067207b96SArnd Bergmann 	ctx = file->private_data;
71167207b96SArnd Bergmann 
71267207b96SArnd Bergmann 	if (len < 4)
71367207b96SArnd Bergmann 		return -EINVAL;
71467207b96SArnd Bergmann 
7158b3d6663SArnd Bergmann 	spu_acquire(ctx);
7168b3d6663SArnd Bergmann 	data = ctx->ops->signal2_read(ctx);
7178b3d6663SArnd Bergmann 	spu_release(ctx);
7188b3d6663SArnd Bergmann 
71967207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
72067207b96SArnd Bergmann 		return -EFAULT;
72167207b96SArnd Bergmann 
72267207b96SArnd Bergmann 	return 4;
72367207b96SArnd Bergmann }
72467207b96SArnd Bergmann 
72567207b96SArnd Bergmann static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
72667207b96SArnd Bergmann 			size_t len, loff_t *pos)
72767207b96SArnd Bergmann {
72867207b96SArnd Bergmann 	struct spu_context *ctx;
72967207b96SArnd Bergmann 	u32 data;
73067207b96SArnd Bergmann 
73167207b96SArnd Bergmann 	ctx = file->private_data;
73267207b96SArnd Bergmann 
73367207b96SArnd Bergmann 	if (len < 4)
73467207b96SArnd Bergmann 		return -EINVAL;
73567207b96SArnd Bergmann 
73667207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
73767207b96SArnd Bergmann 		return -EFAULT;
73867207b96SArnd Bergmann 
7398b3d6663SArnd Bergmann 	spu_acquire(ctx);
7408b3d6663SArnd Bergmann 	ctx->ops->signal2_write(ctx, data);
7418b3d6663SArnd Bergmann 	spu_release(ctx);
74267207b96SArnd Bergmann 
74367207b96SArnd Bergmann 	return 4;
74467207b96SArnd Bergmann }
74567207b96SArnd Bergmann 
7466df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
7476df10a82SMark Nutter static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma,
7486df10a82SMark Nutter 					      unsigned long address, int *type)
7496df10a82SMark Nutter {
7506df10a82SMark Nutter 	return spufs_ps_nopage(vma, address, type, 0x1c000);
7516df10a82SMark Nutter }
7526df10a82SMark Nutter 
7536df10a82SMark Nutter static struct vm_operations_struct spufs_signal2_mmap_vmops = {
7546df10a82SMark Nutter 	.nopage = spufs_signal2_mmap_nopage,
7556df10a82SMark Nutter };
7566df10a82SMark Nutter 
7576df10a82SMark Nutter static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
7586df10a82SMark Nutter {
7596df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
7606df10a82SMark Nutter 		return -EINVAL;
7616df10a82SMark Nutter 
7626df10a82SMark Nutter 	/* FIXME: */
7636df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
7646df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
7656df10a82SMark Nutter 				     | _PAGE_NO_CACHE);
7666df10a82SMark Nutter 
7676df10a82SMark Nutter 	vma->vm_ops = &spufs_signal2_mmap_vmops;
7686df10a82SMark Nutter 	return 0;
7696df10a82SMark Nutter }
7706df10a82SMark Nutter #endif
7716df10a82SMark Nutter 
77267207b96SArnd Bergmann static struct file_operations spufs_signal2_fops = {
7736df10a82SMark Nutter 	.open = spufs_signal2_open,
77467207b96SArnd Bergmann 	.read = spufs_signal2_read,
77567207b96SArnd Bergmann 	.write = spufs_signal2_write,
7766df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
7776df10a82SMark Nutter 	.mmap = spufs_signal2_mmap,
7786df10a82SMark Nutter #endif
77967207b96SArnd Bergmann };
78067207b96SArnd Bergmann 
78167207b96SArnd Bergmann static void spufs_signal1_type_set(void *data, u64 val)
78267207b96SArnd Bergmann {
78367207b96SArnd Bergmann 	struct spu_context *ctx = data;
78467207b96SArnd Bergmann 
7858b3d6663SArnd Bergmann 	spu_acquire(ctx);
7868b3d6663SArnd Bergmann 	ctx->ops->signal1_type_set(ctx, val);
7878b3d6663SArnd Bergmann 	spu_release(ctx);
78867207b96SArnd Bergmann }
78967207b96SArnd Bergmann 
79067207b96SArnd Bergmann static u64 spufs_signal1_type_get(void *data)
79167207b96SArnd Bergmann {
79267207b96SArnd Bergmann 	struct spu_context *ctx = data;
7938b3d6663SArnd Bergmann 	u64 ret;
7948b3d6663SArnd Bergmann 
7958b3d6663SArnd Bergmann 	spu_acquire(ctx);
7968b3d6663SArnd Bergmann 	ret = ctx->ops->signal1_type_get(ctx);
7978b3d6663SArnd Bergmann 	spu_release(ctx);
7988b3d6663SArnd Bergmann 
7998b3d6663SArnd Bergmann 	return ret;
80067207b96SArnd Bergmann }
80167207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
80267207b96SArnd Bergmann 					spufs_signal1_type_set, "%llu");
80367207b96SArnd Bergmann 
80467207b96SArnd Bergmann static void spufs_signal2_type_set(void *data, u64 val)
80567207b96SArnd Bergmann {
80667207b96SArnd Bergmann 	struct spu_context *ctx = data;
80767207b96SArnd Bergmann 
8088b3d6663SArnd Bergmann 	spu_acquire(ctx);
8098b3d6663SArnd Bergmann 	ctx->ops->signal2_type_set(ctx, val);
8108b3d6663SArnd Bergmann 	spu_release(ctx);
81167207b96SArnd Bergmann }
81267207b96SArnd Bergmann 
81367207b96SArnd Bergmann static u64 spufs_signal2_type_get(void *data)
81467207b96SArnd Bergmann {
81567207b96SArnd Bergmann 	struct spu_context *ctx = data;
8168b3d6663SArnd Bergmann 	u64 ret;
8178b3d6663SArnd Bergmann 
8188b3d6663SArnd Bergmann 	spu_acquire(ctx);
8198b3d6663SArnd Bergmann 	ret = ctx->ops->signal2_type_get(ctx);
8208b3d6663SArnd Bergmann 	spu_release(ctx);
8218b3d6663SArnd Bergmann 
8228b3d6663SArnd Bergmann 	return ret;
82367207b96SArnd Bergmann }
82467207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
82567207b96SArnd Bergmann 					spufs_signal2_type_set, "%llu");
82667207b96SArnd Bergmann 
8276df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
8286df10a82SMark Nutter static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
8296df10a82SMark Nutter 					   unsigned long address, int *type)
8306df10a82SMark Nutter {
8316df10a82SMark Nutter 	return spufs_ps_nopage(vma, address, type, 0x3000);
8326df10a82SMark Nutter }
8336df10a82SMark Nutter 
8346df10a82SMark Nutter static struct vm_operations_struct spufs_mfc_mmap_vmops = {
8356df10a82SMark Nutter 	.nopage = spufs_mfc_mmap_nopage,
8366df10a82SMark Nutter };
8376df10a82SMark Nutter 
8386df10a82SMark Nutter /*
8396df10a82SMark Nutter  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
8406df10a82SMark Nutter  * Mapping this area requires that the application have CAP_SYS_RAWIO,
8416df10a82SMark Nutter  * as these registers require special care when read/writing.
8426df10a82SMark Nutter  */
8436df10a82SMark Nutter static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
8446df10a82SMark Nutter {
8456df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
8466df10a82SMark Nutter 		return -EINVAL;
8476df10a82SMark Nutter 
8486df10a82SMark Nutter 	if (!capable(CAP_SYS_RAWIO))
8496df10a82SMark Nutter 		return -EPERM;
8506df10a82SMark Nutter 
8516df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
8526df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
8536df10a82SMark Nutter 				     | _PAGE_NO_CACHE);
8546df10a82SMark Nutter 
8556df10a82SMark Nutter 	vma->vm_ops = &spufs_mfc_mmap_vmops;
8566df10a82SMark Nutter 	return 0;
8576df10a82SMark Nutter }
8586df10a82SMark Nutter #endif
859a33a7d73SArnd Bergmann 
860a33a7d73SArnd Bergmann static int spufs_mfc_open(struct inode *inode, struct file *file)
861a33a7d73SArnd Bergmann {
862a33a7d73SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
863a33a7d73SArnd Bergmann 	struct spu_context *ctx = i->i_ctx;
864a33a7d73SArnd Bergmann 
865a33a7d73SArnd Bergmann 	/* we don't want to deal with DMA into other processes */
866a33a7d73SArnd Bergmann 	if (ctx->owner != current->mm)
867a33a7d73SArnd Bergmann 		return -EINVAL;
868a33a7d73SArnd Bergmann 
869a33a7d73SArnd Bergmann 	if (atomic_read(&inode->i_count) != 1)
870a33a7d73SArnd Bergmann 		return -EBUSY;
871a33a7d73SArnd Bergmann 
872a33a7d73SArnd Bergmann 	file->private_data = ctx;
873a33a7d73SArnd Bergmann 	return nonseekable_open(inode, file);
874a33a7d73SArnd Bergmann }
875a33a7d73SArnd Bergmann 
876a33a7d73SArnd Bergmann /* interrupt-level mfc callback function. */
877a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu)
878a33a7d73SArnd Bergmann {
879a33a7d73SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
880a33a7d73SArnd Bergmann 
881a33a7d73SArnd Bergmann 	wake_up_all(&ctx->mfc_wq);
882a33a7d73SArnd Bergmann 
883a33a7d73SArnd Bergmann 	pr_debug("%s %s\n", __FUNCTION__, spu->name);
884a33a7d73SArnd Bergmann 	if (ctx->mfc_fasync) {
885a33a7d73SArnd Bergmann 		u32 free_elements, tagstatus;
886a33a7d73SArnd Bergmann 		unsigned int mask;
887a33a7d73SArnd Bergmann 
888a33a7d73SArnd Bergmann 		/* no need for spu_acquire in interrupt context */
889a33a7d73SArnd Bergmann 		free_elements = ctx->ops->get_mfc_free_elements(ctx);
890a33a7d73SArnd Bergmann 		tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
891a33a7d73SArnd Bergmann 
892a33a7d73SArnd Bergmann 		mask = 0;
893a33a7d73SArnd Bergmann 		if (free_elements & 0xffff)
894a33a7d73SArnd Bergmann 			mask |= POLLOUT;
895a33a7d73SArnd Bergmann 		if (tagstatus & ctx->tagwait)
896a33a7d73SArnd Bergmann 			mask |= POLLIN;
897a33a7d73SArnd Bergmann 
898a33a7d73SArnd Bergmann 		kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
899a33a7d73SArnd Bergmann 	}
900a33a7d73SArnd Bergmann }
901a33a7d73SArnd Bergmann 
902a33a7d73SArnd Bergmann static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
903a33a7d73SArnd Bergmann {
904a33a7d73SArnd Bergmann 	/* See if there is one tag group is complete */
905a33a7d73SArnd Bergmann 	/* FIXME we need locking around tagwait */
906a33a7d73SArnd Bergmann 	*status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
907a33a7d73SArnd Bergmann 	ctx->tagwait &= ~*status;
908a33a7d73SArnd Bergmann 	if (*status)
909a33a7d73SArnd Bergmann 		return 1;
910a33a7d73SArnd Bergmann 
911a33a7d73SArnd Bergmann 	/* enable interrupt waiting for any tag group,
912a33a7d73SArnd Bergmann 	   may silently fail if interrupts are already enabled */
913a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
914a33a7d73SArnd Bergmann 	return 0;
915a33a7d73SArnd Bergmann }
916a33a7d73SArnd Bergmann 
917a33a7d73SArnd Bergmann static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
918a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
919a33a7d73SArnd Bergmann {
920a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
921a33a7d73SArnd Bergmann 	int ret = -EINVAL;
922a33a7d73SArnd Bergmann 	u32 status;
923a33a7d73SArnd Bergmann 
924a33a7d73SArnd Bergmann 	if (size != 4)
925a33a7d73SArnd Bergmann 		goto out;
926a33a7d73SArnd Bergmann 
927a33a7d73SArnd Bergmann 	spu_acquire(ctx);
928a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
929a33a7d73SArnd Bergmann 		status = ctx->ops->read_mfc_tagstatus(ctx);
930a33a7d73SArnd Bergmann 		if (!(status & ctx->tagwait))
931a33a7d73SArnd Bergmann 			ret = -EAGAIN;
932a33a7d73SArnd Bergmann 		else
933a33a7d73SArnd Bergmann 			ctx->tagwait &= ~status;
934a33a7d73SArnd Bergmann 	} else {
935a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
936a33a7d73SArnd Bergmann 			   spufs_read_mfc_tagstatus(ctx, &status));
937a33a7d73SArnd Bergmann 	}
938a33a7d73SArnd Bergmann 	spu_release(ctx);
939a33a7d73SArnd Bergmann 
940a33a7d73SArnd Bergmann 	if (ret)
941a33a7d73SArnd Bergmann 		goto out;
942a33a7d73SArnd Bergmann 
943a33a7d73SArnd Bergmann 	ret = 4;
944a33a7d73SArnd Bergmann 	if (copy_to_user(buffer, &status, 4))
945a33a7d73SArnd Bergmann 		ret = -EFAULT;
946a33a7d73SArnd Bergmann 
947a33a7d73SArnd Bergmann out:
948a33a7d73SArnd Bergmann 	return ret;
949a33a7d73SArnd Bergmann }
950a33a7d73SArnd Bergmann 
951a33a7d73SArnd Bergmann static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
952a33a7d73SArnd Bergmann {
953a33a7d73SArnd Bergmann 	pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
954a33a7d73SArnd Bergmann 		 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
955a33a7d73SArnd Bergmann 
956a33a7d73SArnd Bergmann 	switch (cmd->cmd) {
957a33a7d73SArnd Bergmann 	case MFC_PUT_CMD:
958a33a7d73SArnd Bergmann 	case MFC_PUTF_CMD:
959a33a7d73SArnd Bergmann 	case MFC_PUTB_CMD:
960a33a7d73SArnd Bergmann 	case MFC_GET_CMD:
961a33a7d73SArnd Bergmann 	case MFC_GETF_CMD:
962a33a7d73SArnd Bergmann 	case MFC_GETB_CMD:
963a33a7d73SArnd Bergmann 		break;
964a33a7d73SArnd Bergmann 	default:
965a33a7d73SArnd Bergmann 		pr_debug("invalid DMA opcode %x\n", cmd->cmd);
966a33a7d73SArnd Bergmann 		return -EIO;
967a33a7d73SArnd Bergmann 	}
968a33a7d73SArnd Bergmann 
969a33a7d73SArnd Bergmann 	if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
970a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
971a33a7d73SArnd Bergmann 				cmd->ea, cmd->lsa);
972a33a7d73SArnd Bergmann 		return -EIO;
973a33a7d73SArnd Bergmann 	}
974a33a7d73SArnd Bergmann 
975a33a7d73SArnd Bergmann 	switch (cmd->size & 0xf) {
976a33a7d73SArnd Bergmann 	case 1:
977a33a7d73SArnd Bergmann 		break;
978a33a7d73SArnd Bergmann 	case 2:
979a33a7d73SArnd Bergmann 		if (cmd->lsa & 1)
980a33a7d73SArnd Bergmann 			goto error;
981a33a7d73SArnd Bergmann 		break;
982a33a7d73SArnd Bergmann 	case 4:
983a33a7d73SArnd Bergmann 		if (cmd->lsa & 3)
984a33a7d73SArnd Bergmann 			goto error;
985a33a7d73SArnd Bergmann 		break;
986a33a7d73SArnd Bergmann 	case 8:
987a33a7d73SArnd Bergmann 		if (cmd->lsa & 7)
988a33a7d73SArnd Bergmann 			goto error;
989a33a7d73SArnd Bergmann 		break;
990a33a7d73SArnd Bergmann 	case 0:
991a33a7d73SArnd Bergmann 		if (cmd->lsa & 15)
992a33a7d73SArnd Bergmann 			goto error;
993a33a7d73SArnd Bergmann 		break;
994a33a7d73SArnd Bergmann 	error:
995a33a7d73SArnd Bergmann 	default:
996a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment %x for size %x\n",
997a33a7d73SArnd Bergmann 			cmd->lsa & 0xf, cmd->size);
998a33a7d73SArnd Bergmann 		return -EIO;
999a33a7d73SArnd Bergmann 	}
1000a33a7d73SArnd Bergmann 
1001a33a7d73SArnd Bergmann 	if (cmd->size > 16 * 1024) {
1002a33a7d73SArnd Bergmann 		pr_debug("invalid DMA size %x\n", cmd->size);
1003a33a7d73SArnd Bergmann 		return -EIO;
1004a33a7d73SArnd Bergmann 	}
1005a33a7d73SArnd Bergmann 
1006a33a7d73SArnd Bergmann 	if (cmd->tag & 0xfff0) {
1007a33a7d73SArnd Bergmann 		/* we reserve the higher tag numbers for kernel use */
1008a33a7d73SArnd Bergmann 		pr_debug("invalid DMA tag\n");
1009a33a7d73SArnd Bergmann 		return -EIO;
1010a33a7d73SArnd Bergmann 	}
1011a33a7d73SArnd Bergmann 
1012a33a7d73SArnd Bergmann 	if (cmd->class) {
1013a33a7d73SArnd Bergmann 		/* not supported in this version */
1014a33a7d73SArnd Bergmann 		pr_debug("invalid DMA class\n");
1015a33a7d73SArnd Bergmann 		return -EIO;
1016a33a7d73SArnd Bergmann 	}
1017a33a7d73SArnd Bergmann 
1018a33a7d73SArnd Bergmann 	return 0;
1019a33a7d73SArnd Bergmann }
1020a33a7d73SArnd Bergmann 
1021a33a7d73SArnd Bergmann static int spu_send_mfc_command(struct spu_context *ctx,
1022a33a7d73SArnd Bergmann 				struct mfc_dma_command cmd,
1023a33a7d73SArnd Bergmann 				int *error)
1024a33a7d73SArnd Bergmann {
1025a33a7d73SArnd Bergmann 	*error = ctx->ops->send_mfc_command(ctx, &cmd);
1026a33a7d73SArnd Bergmann 	if (*error == -EAGAIN) {
1027a33a7d73SArnd Bergmann 		/* wait for any tag group to complete
1028a33a7d73SArnd Bergmann 		   so we have space for the new command */
1029a33a7d73SArnd Bergmann 		ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1030a33a7d73SArnd Bergmann 		/* try again, because the queue might be
1031a33a7d73SArnd Bergmann 		   empty again */
1032a33a7d73SArnd Bergmann 		*error = ctx->ops->send_mfc_command(ctx, &cmd);
1033a33a7d73SArnd Bergmann 		if (*error == -EAGAIN)
1034a33a7d73SArnd Bergmann 			return 0;
1035a33a7d73SArnd Bergmann 	}
1036a33a7d73SArnd Bergmann 	return 1;
1037a33a7d73SArnd Bergmann }
1038a33a7d73SArnd Bergmann 
1039a33a7d73SArnd Bergmann static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1040a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1041a33a7d73SArnd Bergmann {
1042a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1043a33a7d73SArnd Bergmann 	struct mfc_dma_command cmd;
1044a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1045a33a7d73SArnd Bergmann 
1046a33a7d73SArnd Bergmann 	if (size != sizeof cmd)
1047a33a7d73SArnd Bergmann 		goto out;
1048a33a7d73SArnd Bergmann 
1049a33a7d73SArnd Bergmann 	ret = -EFAULT;
1050a33a7d73SArnd Bergmann 	if (copy_from_user(&cmd, buffer, sizeof cmd))
1051a33a7d73SArnd Bergmann 		goto out;
1052a33a7d73SArnd Bergmann 
1053a33a7d73SArnd Bergmann 	ret = spufs_check_valid_dma(&cmd);
1054a33a7d73SArnd Bergmann 	if (ret)
1055a33a7d73SArnd Bergmann 		goto out;
1056a33a7d73SArnd Bergmann 
1057a33a7d73SArnd Bergmann 	spu_acquire_runnable(ctx);
1058a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1059a33a7d73SArnd Bergmann 		ret = ctx->ops->send_mfc_command(ctx, &cmd);
1060a33a7d73SArnd Bergmann 	} else {
1061a33a7d73SArnd Bergmann 		int status;
1062a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1063a33a7d73SArnd Bergmann 				 spu_send_mfc_command(ctx, cmd, &status));
1064a33a7d73SArnd Bergmann 		if (status)
1065a33a7d73SArnd Bergmann 			ret = status;
1066a33a7d73SArnd Bergmann 	}
1067a33a7d73SArnd Bergmann 	spu_release(ctx);
1068a33a7d73SArnd Bergmann 
1069a33a7d73SArnd Bergmann 	if (ret)
1070a33a7d73SArnd Bergmann 		goto out;
1071a33a7d73SArnd Bergmann 
1072a33a7d73SArnd Bergmann 	ctx->tagwait |= 1 << cmd.tag;
1073a33a7d73SArnd Bergmann 
1074a33a7d73SArnd Bergmann out:
1075a33a7d73SArnd Bergmann 	return ret;
1076a33a7d73SArnd Bergmann }
1077a33a7d73SArnd Bergmann 
1078a33a7d73SArnd Bergmann static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1079a33a7d73SArnd Bergmann {
1080a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1081a33a7d73SArnd Bergmann 	u32 free_elements, tagstatus;
1082a33a7d73SArnd Bergmann 	unsigned int mask;
1083a33a7d73SArnd Bergmann 
1084a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1085a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1086a33a7d73SArnd Bergmann 	free_elements = ctx->ops->get_mfc_free_elements(ctx);
1087a33a7d73SArnd Bergmann 	tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1088a33a7d73SArnd Bergmann 	spu_release(ctx);
1089a33a7d73SArnd Bergmann 
1090a33a7d73SArnd Bergmann 	poll_wait(file, &ctx->mfc_wq, wait);
1091a33a7d73SArnd Bergmann 
1092a33a7d73SArnd Bergmann 	mask = 0;
1093a33a7d73SArnd Bergmann 	if (free_elements & 0xffff)
1094a33a7d73SArnd Bergmann 		mask |= POLLOUT | POLLWRNORM;
1095a33a7d73SArnd Bergmann 	if (tagstatus & ctx->tagwait)
1096a33a7d73SArnd Bergmann 		mask |= POLLIN | POLLRDNORM;
1097a33a7d73SArnd Bergmann 
1098a33a7d73SArnd Bergmann 	pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1099a33a7d73SArnd Bergmann 		free_elements, tagstatus, ctx->tagwait);
1100a33a7d73SArnd Bergmann 
1101a33a7d73SArnd Bergmann 	return mask;
1102a33a7d73SArnd Bergmann }
1103a33a7d73SArnd Bergmann 
1104a33a7d73SArnd Bergmann static int spufs_mfc_flush(struct file *file)
1105a33a7d73SArnd Bergmann {
1106a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1107a33a7d73SArnd Bergmann 	int ret;
1108a33a7d73SArnd Bergmann 
1109a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1110a33a7d73SArnd Bergmann #if 0
1111a33a7d73SArnd Bergmann /* this currently hangs */
1112a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1113a33a7d73SArnd Bergmann 			 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1114a33a7d73SArnd Bergmann 	if (ret)
1115a33a7d73SArnd Bergmann 		goto out;
1116a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1117a33a7d73SArnd Bergmann 			 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1118a33a7d73SArnd Bergmann out:
1119a33a7d73SArnd Bergmann #else
1120a33a7d73SArnd Bergmann 	ret = 0;
1121a33a7d73SArnd Bergmann #endif
1122a33a7d73SArnd Bergmann 	spu_release(ctx);
1123a33a7d73SArnd Bergmann 
1124a33a7d73SArnd Bergmann 	return ret;
1125a33a7d73SArnd Bergmann }
1126a33a7d73SArnd Bergmann 
1127a33a7d73SArnd Bergmann static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1128a33a7d73SArnd Bergmann 			   int datasync)
1129a33a7d73SArnd Bergmann {
1130a33a7d73SArnd Bergmann 	return spufs_mfc_flush(file);
1131a33a7d73SArnd Bergmann }
1132a33a7d73SArnd Bergmann 
1133a33a7d73SArnd Bergmann static int spufs_mfc_fasync(int fd, struct file *file, int on)
1134a33a7d73SArnd Bergmann {
1135a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1136a33a7d73SArnd Bergmann 
1137a33a7d73SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1138a33a7d73SArnd Bergmann }
1139a33a7d73SArnd Bergmann 
1140a33a7d73SArnd Bergmann static struct file_operations spufs_mfc_fops = {
1141a33a7d73SArnd Bergmann 	.open	 = spufs_mfc_open,
1142a33a7d73SArnd Bergmann 	.read	 = spufs_mfc_read,
1143a33a7d73SArnd Bergmann 	.write	 = spufs_mfc_write,
1144a33a7d73SArnd Bergmann 	.poll	 = spufs_mfc_poll,
1145a33a7d73SArnd Bergmann 	.flush	 = spufs_mfc_flush,
1146a33a7d73SArnd Bergmann 	.fsync	 = spufs_mfc_fsync,
1147a33a7d73SArnd Bergmann 	.fasync	 = spufs_mfc_fasync,
11486df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
11496df10a82SMark Nutter 	.mmap	 = spufs_mfc_mmap,
11506df10a82SMark Nutter #endif
1151a33a7d73SArnd Bergmann };
1152a33a7d73SArnd Bergmann 
115367207b96SArnd Bergmann static void spufs_npc_set(void *data, u64 val)
115467207b96SArnd Bergmann {
115567207b96SArnd Bergmann 	struct spu_context *ctx = data;
11568b3d6663SArnd Bergmann 	spu_acquire(ctx);
11578b3d6663SArnd Bergmann 	ctx->ops->npc_write(ctx, val);
11588b3d6663SArnd Bergmann 	spu_release(ctx);
115967207b96SArnd Bergmann }
116067207b96SArnd Bergmann 
116167207b96SArnd Bergmann static u64 spufs_npc_get(void *data)
116267207b96SArnd Bergmann {
116367207b96SArnd Bergmann 	struct spu_context *ctx = data;
116467207b96SArnd Bergmann 	u64 ret;
11658b3d6663SArnd Bergmann 	spu_acquire(ctx);
11668b3d6663SArnd Bergmann 	ret = ctx->ops->npc_read(ctx);
11678b3d6663SArnd Bergmann 	spu_release(ctx);
116867207b96SArnd Bergmann 	return ret;
116967207b96SArnd Bergmann }
117067207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
117167207b96SArnd Bergmann 
11728b3d6663SArnd Bergmann static void spufs_decr_set(void *data, u64 val)
11738b3d6663SArnd Bergmann {
11748b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
11758b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
11768b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
11778b3d6663SArnd Bergmann 	lscsa->decr.slot[0] = (u32) val;
11788b3d6663SArnd Bergmann 	spu_release(ctx);
11798b3d6663SArnd Bergmann }
11808b3d6663SArnd Bergmann 
11818b3d6663SArnd Bergmann static u64 spufs_decr_get(void *data)
11828b3d6663SArnd Bergmann {
11838b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
11848b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
11858b3d6663SArnd Bergmann 	u64 ret;
11868b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
11878b3d6663SArnd Bergmann 	ret = lscsa->decr.slot[0];
11888b3d6663SArnd Bergmann 	spu_release(ctx);
11898b3d6663SArnd Bergmann 	return ret;
11908b3d6663SArnd Bergmann }
11918b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
11928b3d6663SArnd Bergmann 			"%llx\n")
11938b3d6663SArnd Bergmann 
11948b3d6663SArnd Bergmann static void spufs_decr_status_set(void *data, u64 val)
11958b3d6663SArnd Bergmann {
11968b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
11978b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
11988b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
11998b3d6663SArnd Bergmann 	lscsa->decr_status.slot[0] = (u32) val;
12008b3d6663SArnd Bergmann 	spu_release(ctx);
12018b3d6663SArnd Bergmann }
12028b3d6663SArnd Bergmann 
12038b3d6663SArnd Bergmann static u64 spufs_decr_status_get(void *data)
12048b3d6663SArnd Bergmann {
12058b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12068b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12078b3d6663SArnd Bergmann 	u64 ret;
12088b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12098b3d6663SArnd Bergmann 	ret = lscsa->decr_status.slot[0];
12108b3d6663SArnd Bergmann 	spu_release(ctx);
12118b3d6663SArnd Bergmann 	return ret;
12128b3d6663SArnd Bergmann }
12138b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
12148b3d6663SArnd Bergmann 			spufs_decr_status_set, "%llx\n")
12158b3d6663SArnd Bergmann 
12168b3d6663SArnd Bergmann static void spufs_spu_tag_mask_set(void *data, u64 val)
12178b3d6663SArnd Bergmann {
12188b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12198b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12208b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12218b3d6663SArnd Bergmann 	lscsa->tag_mask.slot[0] = (u32) val;
12228b3d6663SArnd Bergmann 	spu_release(ctx);
12238b3d6663SArnd Bergmann }
12248b3d6663SArnd Bergmann 
12258b3d6663SArnd Bergmann static u64 spufs_spu_tag_mask_get(void *data)
12268b3d6663SArnd Bergmann {
12278b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12288b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12298b3d6663SArnd Bergmann 	u64 ret;
12308b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12318b3d6663SArnd Bergmann 	ret = lscsa->tag_mask.slot[0];
12328b3d6663SArnd Bergmann 	spu_release(ctx);
12338b3d6663SArnd Bergmann 	return ret;
12348b3d6663SArnd Bergmann }
12358b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
12368b3d6663SArnd Bergmann 			spufs_spu_tag_mask_set, "%llx\n")
12378b3d6663SArnd Bergmann 
12388b3d6663SArnd Bergmann static void spufs_event_mask_set(void *data, u64 val)
12398b3d6663SArnd Bergmann {
12408b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12418b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12428b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12438b3d6663SArnd Bergmann 	lscsa->event_mask.slot[0] = (u32) val;
12448b3d6663SArnd Bergmann 	spu_release(ctx);
12458b3d6663SArnd Bergmann }
12468b3d6663SArnd Bergmann 
12478b3d6663SArnd Bergmann static u64 spufs_event_mask_get(void *data)
12488b3d6663SArnd Bergmann {
12498b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12508b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12518b3d6663SArnd Bergmann 	u64 ret;
12528b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12538b3d6663SArnd Bergmann 	ret = lscsa->event_mask.slot[0];
12548b3d6663SArnd Bergmann 	spu_release(ctx);
12558b3d6663SArnd Bergmann 	return ret;
12568b3d6663SArnd Bergmann }
12578b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
12588b3d6663SArnd Bergmann 			spufs_event_mask_set, "%llx\n")
12598b3d6663SArnd Bergmann 
12608b3d6663SArnd Bergmann static void spufs_srr0_set(void *data, u64 val)
12618b3d6663SArnd Bergmann {
12628b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12638b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12648b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12658b3d6663SArnd Bergmann 	lscsa->srr0.slot[0] = (u32) val;
12668b3d6663SArnd Bergmann 	spu_release(ctx);
12678b3d6663SArnd Bergmann }
12688b3d6663SArnd Bergmann 
12698b3d6663SArnd Bergmann static u64 spufs_srr0_get(void *data)
12708b3d6663SArnd Bergmann {
12718b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12728b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12738b3d6663SArnd Bergmann 	u64 ret;
12748b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12758b3d6663SArnd Bergmann 	ret = lscsa->srr0.slot[0];
12768b3d6663SArnd Bergmann 	spu_release(ctx);
12778b3d6663SArnd Bergmann 	return ret;
12788b3d6663SArnd Bergmann }
12798b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
12808b3d6663SArnd Bergmann 			"%llx\n")
12818b3d6663SArnd Bergmann 
128267207b96SArnd Bergmann struct tree_descr spufs_dir_contents[] = {
128367207b96SArnd Bergmann 	{ "mem",  &spufs_mem_fops,  0666, },
12848b3d6663SArnd Bergmann 	{ "regs", &spufs_regs_fops,  0666, },
128567207b96SArnd Bergmann 	{ "mbox", &spufs_mbox_fops, 0444, },
128667207b96SArnd Bergmann 	{ "ibox", &spufs_ibox_fops, 0444, },
128767207b96SArnd Bergmann 	{ "wbox", &spufs_wbox_fops, 0222, },
128867207b96SArnd Bergmann 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, },
128967207b96SArnd Bergmann 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, },
129067207b96SArnd Bergmann 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, },
129167207b96SArnd Bergmann 	{ "signal1", &spufs_signal1_fops, 0666, },
129267207b96SArnd Bergmann 	{ "signal2", &spufs_signal2_fops, 0666, },
129367207b96SArnd Bergmann 	{ "signal1_type", &spufs_signal1_type, 0666, },
129467207b96SArnd Bergmann 	{ "signal2_type", &spufs_signal2_type, 0666, },
1295a33a7d73SArnd Bergmann 	{ "mfc", &spufs_mfc_fops, 0666, },
12966df10a82SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
129767207b96SArnd Bergmann 	{ "npc", &spufs_npc_ops, 0666, },
12988b3d6663SArnd Bergmann 	{ "fpcr", &spufs_fpcr_fops, 0666, },
12998b3d6663SArnd Bergmann 	{ "decr", &spufs_decr_ops, 0666, },
13008b3d6663SArnd Bergmann 	{ "decr_status", &spufs_decr_status_ops, 0666, },
13018b3d6663SArnd Bergmann 	{ "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
13028b3d6663SArnd Bergmann 	{ "event_mask", &spufs_event_mask_ops, 0666, },
13038b3d6663SArnd Bergmann 	{ "srr0", &spufs_srr0_ops, 0666, },
130467207b96SArnd Bergmann 	{},
130567207b96SArnd Bergmann };
1306