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
828d9379c4bSarnd@arndb.de static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma,
829d9379c4bSarnd@arndb.de 					   unsigned long address, int *type)
830d9379c4bSarnd@arndb.de {
831d9379c4bSarnd@arndb.de 	return spufs_ps_nopage(vma, address, type, 0x0000);
832d9379c4bSarnd@arndb.de }
833d9379c4bSarnd@arndb.de 
834d9379c4bSarnd@arndb.de static struct vm_operations_struct spufs_mss_mmap_vmops = {
835d9379c4bSarnd@arndb.de 	.nopage = spufs_mss_mmap_nopage,
836d9379c4bSarnd@arndb.de };
837d9379c4bSarnd@arndb.de 
838d9379c4bSarnd@arndb.de /*
839d9379c4bSarnd@arndb.de  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
840d9379c4bSarnd@arndb.de  * Mapping this area requires that the application have CAP_SYS_RAWIO,
841d9379c4bSarnd@arndb.de  * as these registers require special care when read/writing.
842d9379c4bSarnd@arndb.de  */
843d9379c4bSarnd@arndb.de static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
844d9379c4bSarnd@arndb.de {
845d9379c4bSarnd@arndb.de 	if (!(vma->vm_flags & VM_SHARED))
846d9379c4bSarnd@arndb.de 		return -EINVAL;
847d9379c4bSarnd@arndb.de 
848d9379c4bSarnd@arndb.de 	if (!capable(CAP_SYS_RAWIO))
849d9379c4bSarnd@arndb.de 		return -EPERM;
850d9379c4bSarnd@arndb.de 
851d9379c4bSarnd@arndb.de 	vma->vm_flags |= VM_RESERVED;
852d9379c4bSarnd@arndb.de 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
853d9379c4bSarnd@arndb.de 				     | _PAGE_NO_CACHE);
854d9379c4bSarnd@arndb.de 
855d9379c4bSarnd@arndb.de 	vma->vm_ops = &spufs_mss_mmap_vmops;
856d9379c4bSarnd@arndb.de 	return 0;
857d9379c4bSarnd@arndb.de }
858d9379c4bSarnd@arndb.de #endif
859d9379c4bSarnd@arndb.de 
860d9379c4bSarnd@arndb.de static int spufs_mss_open(struct inode *inode, struct file *file)
861d9379c4bSarnd@arndb.de {
862d9379c4bSarnd@arndb.de 	struct spufs_inode_info *i = SPUFS_I(inode);
863d9379c4bSarnd@arndb.de 
864d9379c4bSarnd@arndb.de 	file->private_data = i->i_ctx;
865d9379c4bSarnd@arndb.de 	return nonseekable_open(inode, file);
866d9379c4bSarnd@arndb.de }
867d9379c4bSarnd@arndb.de 
868d9379c4bSarnd@arndb.de static struct file_operations spufs_mss_fops = {
869d9379c4bSarnd@arndb.de 	.open	 = spufs_mss_open,
870d9379c4bSarnd@arndb.de #ifdef CONFIG_SPUFS_MMAP
871d9379c4bSarnd@arndb.de 	.mmap	 = spufs_mss_mmap,
872d9379c4bSarnd@arndb.de #endif
873d9379c4bSarnd@arndb.de };
874d9379c4bSarnd@arndb.de 
875d9379c4bSarnd@arndb.de 
876d9379c4bSarnd@arndb.de #ifdef CONFIG_SPUFS_MMAP
8776df10a82SMark Nutter static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma,
8786df10a82SMark Nutter 					   unsigned long address, int *type)
8796df10a82SMark Nutter {
8806df10a82SMark Nutter 	return spufs_ps_nopage(vma, address, type, 0x3000);
8816df10a82SMark Nutter }
8826df10a82SMark Nutter 
8836df10a82SMark Nutter static struct vm_operations_struct spufs_mfc_mmap_vmops = {
8846df10a82SMark Nutter 	.nopage = spufs_mfc_mmap_nopage,
8856df10a82SMark Nutter };
8866df10a82SMark Nutter 
8876df10a82SMark Nutter /*
8886df10a82SMark Nutter  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
8896df10a82SMark Nutter  * Mapping this area requires that the application have CAP_SYS_RAWIO,
8906df10a82SMark Nutter  * as these registers require special care when read/writing.
8916df10a82SMark Nutter  */
8926df10a82SMark Nutter static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
8936df10a82SMark Nutter {
8946df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
8956df10a82SMark Nutter 		return -EINVAL;
8966df10a82SMark Nutter 
8976df10a82SMark Nutter 	if (!capable(CAP_SYS_RAWIO))
8986df10a82SMark Nutter 		return -EPERM;
8996df10a82SMark Nutter 
9006df10a82SMark Nutter 	vma->vm_flags |= VM_RESERVED;
9016df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
9026df10a82SMark Nutter 				     | _PAGE_NO_CACHE);
9036df10a82SMark Nutter 
9046df10a82SMark Nutter 	vma->vm_ops = &spufs_mfc_mmap_vmops;
9056df10a82SMark Nutter 	return 0;
9066df10a82SMark Nutter }
9076df10a82SMark Nutter #endif
908a33a7d73SArnd Bergmann 
909a33a7d73SArnd Bergmann static int spufs_mfc_open(struct inode *inode, struct file *file)
910a33a7d73SArnd Bergmann {
911a33a7d73SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
912a33a7d73SArnd Bergmann 	struct spu_context *ctx = i->i_ctx;
913a33a7d73SArnd Bergmann 
914a33a7d73SArnd Bergmann 	/* we don't want to deal with DMA into other processes */
915a33a7d73SArnd Bergmann 	if (ctx->owner != current->mm)
916a33a7d73SArnd Bergmann 		return -EINVAL;
917a33a7d73SArnd Bergmann 
918a33a7d73SArnd Bergmann 	if (atomic_read(&inode->i_count) != 1)
919a33a7d73SArnd Bergmann 		return -EBUSY;
920a33a7d73SArnd Bergmann 
921a33a7d73SArnd Bergmann 	file->private_data = ctx;
922a33a7d73SArnd Bergmann 	return nonseekable_open(inode, file);
923a33a7d73SArnd Bergmann }
924a33a7d73SArnd Bergmann 
925a33a7d73SArnd Bergmann /* interrupt-level mfc callback function. */
926a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu)
927a33a7d73SArnd Bergmann {
928a33a7d73SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
929a33a7d73SArnd Bergmann 
930a33a7d73SArnd Bergmann 	wake_up_all(&ctx->mfc_wq);
931a33a7d73SArnd Bergmann 
932a33a7d73SArnd Bergmann 	pr_debug("%s %s\n", __FUNCTION__, spu->name);
933a33a7d73SArnd Bergmann 	if (ctx->mfc_fasync) {
934a33a7d73SArnd Bergmann 		u32 free_elements, tagstatus;
935a33a7d73SArnd Bergmann 		unsigned int mask;
936a33a7d73SArnd Bergmann 
937a33a7d73SArnd Bergmann 		/* no need for spu_acquire in interrupt context */
938a33a7d73SArnd Bergmann 		free_elements = ctx->ops->get_mfc_free_elements(ctx);
939a33a7d73SArnd Bergmann 		tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
940a33a7d73SArnd Bergmann 
941a33a7d73SArnd Bergmann 		mask = 0;
942a33a7d73SArnd Bergmann 		if (free_elements & 0xffff)
943a33a7d73SArnd Bergmann 			mask |= POLLOUT;
944a33a7d73SArnd Bergmann 		if (tagstatus & ctx->tagwait)
945a33a7d73SArnd Bergmann 			mask |= POLLIN;
946a33a7d73SArnd Bergmann 
947a33a7d73SArnd Bergmann 		kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
948a33a7d73SArnd Bergmann 	}
949a33a7d73SArnd Bergmann }
950a33a7d73SArnd Bergmann 
951a33a7d73SArnd Bergmann static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
952a33a7d73SArnd Bergmann {
953a33a7d73SArnd Bergmann 	/* See if there is one tag group is complete */
954a33a7d73SArnd Bergmann 	/* FIXME we need locking around tagwait */
955a33a7d73SArnd Bergmann 	*status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
956a33a7d73SArnd Bergmann 	ctx->tagwait &= ~*status;
957a33a7d73SArnd Bergmann 	if (*status)
958a33a7d73SArnd Bergmann 		return 1;
959a33a7d73SArnd Bergmann 
960a33a7d73SArnd Bergmann 	/* enable interrupt waiting for any tag group,
961a33a7d73SArnd Bergmann 	   may silently fail if interrupts are already enabled */
962a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
963a33a7d73SArnd Bergmann 	return 0;
964a33a7d73SArnd Bergmann }
965a33a7d73SArnd Bergmann 
966a33a7d73SArnd Bergmann static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
967a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
968a33a7d73SArnd Bergmann {
969a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
970a33a7d73SArnd Bergmann 	int ret = -EINVAL;
971a33a7d73SArnd Bergmann 	u32 status;
972a33a7d73SArnd Bergmann 
973a33a7d73SArnd Bergmann 	if (size != 4)
974a33a7d73SArnd Bergmann 		goto out;
975a33a7d73SArnd Bergmann 
976a33a7d73SArnd Bergmann 	spu_acquire(ctx);
977a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
978a33a7d73SArnd Bergmann 		status = ctx->ops->read_mfc_tagstatus(ctx);
979a33a7d73SArnd Bergmann 		if (!(status & ctx->tagwait))
980a33a7d73SArnd Bergmann 			ret = -EAGAIN;
981a33a7d73SArnd Bergmann 		else
982a33a7d73SArnd Bergmann 			ctx->tagwait &= ~status;
983a33a7d73SArnd Bergmann 	} else {
984a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
985a33a7d73SArnd Bergmann 			   spufs_read_mfc_tagstatus(ctx, &status));
986a33a7d73SArnd Bergmann 	}
987a33a7d73SArnd Bergmann 	spu_release(ctx);
988a33a7d73SArnd Bergmann 
989a33a7d73SArnd Bergmann 	if (ret)
990a33a7d73SArnd Bergmann 		goto out;
991a33a7d73SArnd Bergmann 
992a33a7d73SArnd Bergmann 	ret = 4;
993a33a7d73SArnd Bergmann 	if (copy_to_user(buffer, &status, 4))
994a33a7d73SArnd Bergmann 		ret = -EFAULT;
995a33a7d73SArnd Bergmann 
996a33a7d73SArnd Bergmann out:
997a33a7d73SArnd Bergmann 	return ret;
998a33a7d73SArnd Bergmann }
999a33a7d73SArnd Bergmann 
1000a33a7d73SArnd Bergmann static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1001a33a7d73SArnd Bergmann {
1002a33a7d73SArnd Bergmann 	pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1003a33a7d73SArnd Bergmann 		 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1004a33a7d73SArnd Bergmann 
1005a33a7d73SArnd Bergmann 	switch (cmd->cmd) {
1006a33a7d73SArnd Bergmann 	case MFC_PUT_CMD:
1007a33a7d73SArnd Bergmann 	case MFC_PUTF_CMD:
1008a33a7d73SArnd Bergmann 	case MFC_PUTB_CMD:
1009a33a7d73SArnd Bergmann 	case MFC_GET_CMD:
1010a33a7d73SArnd Bergmann 	case MFC_GETF_CMD:
1011a33a7d73SArnd Bergmann 	case MFC_GETB_CMD:
1012a33a7d73SArnd Bergmann 		break;
1013a33a7d73SArnd Bergmann 	default:
1014a33a7d73SArnd Bergmann 		pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1015a33a7d73SArnd Bergmann 		return -EIO;
1016a33a7d73SArnd Bergmann 	}
1017a33a7d73SArnd Bergmann 
1018a33a7d73SArnd Bergmann 	if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1019a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1020a33a7d73SArnd Bergmann 				cmd->ea, cmd->lsa);
1021a33a7d73SArnd Bergmann 		return -EIO;
1022a33a7d73SArnd Bergmann 	}
1023a33a7d73SArnd Bergmann 
1024a33a7d73SArnd Bergmann 	switch (cmd->size & 0xf) {
1025a33a7d73SArnd Bergmann 	case 1:
1026a33a7d73SArnd Bergmann 		break;
1027a33a7d73SArnd Bergmann 	case 2:
1028a33a7d73SArnd Bergmann 		if (cmd->lsa & 1)
1029a33a7d73SArnd Bergmann 			goto error;
1030a33a7d73SArnd Bergmann 		break;
1031a33a7d73SArnd Bergmann 	case 4:
1032a33a7d73SArnd Bergmann 		if (cmd->lsa & 3)
1033a33a7d73SArnd Bergmann 			goto error;
1034a33a7d73SArnd Bergmann 		break;
1035a33a7d73SArnd Bergmann 	case 8:
1036a33a7d73SArnd Bergmann 		if (cmd->lsa & 7)
1037a33a7d73SArnd Bergmann 			goto error;
1038a33a7d73SArnd Bergmann 		break;
1039a33a7d73SArnd Bergmann 	case 0:
1040a33a7d73SArnd Bergmann 		if (cmd->lsa & 15)
1041a33a7d73SArnd Bergmann 			goto error;
1042a33a7d73SArnd Bergmann 		break;
1043a33a7d73SArnd Bergmann 	error:
1044a33a7d73SArnd Bergmann 	default:
1045a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment %x for size %x\n",
1046a33a7d73SArnd Bergmann 			cmd->lsa & 0xf, cmd->size);
1047a33a7d73SArnd Bergmann 		return -EIO;
1048a33a7d73SArnd Bergmann 	}
1049a33a7d73SArnd Bergmann 
1050a33a7d73SArnd Bergmann 	if (cmd->size > 16 * 1024) {
1051a33a7d73SArnd Bergmann 		pr_debug("invalid DMA size %x\n", cmd->size);
1052a33a7d73SArnd Bergmann 		return -EIO;
1053a33a7d73SArnd Bergmann 	}
1054a33a7d73SArnd Bergmann 
1055a33a7d73SArnd Bergmann 	if (cmd->tag & 0xfff0) {
1056a33a7d73SArnd Bergmann 		/* we reserve the higher tag numbers for kernel use */
1057a33a7d73SArnd Bergmann 		pr_debug("invalid DMA tag\n");
1058a33a7d73SArnd Bergmann 		return -EIO;
1059a33a7d73SArnd Bergmann 	}
1060a33a7d73SArnd Bergmann 
1061a33a7d73SArnd Bergmann 	if (cmd->class) {
1062a33a7d73SArnd Bergmann 		/* not supported in this version */
1063a33a7d73SArnd Bergmann 		pr_debug("invalid DMA class\n");
1064a33a7d73SArnd Bergmann 		return -EIO;
1065a33a7d73SArnd Bergmann 	}
1066a33a7d73SArnd Bergmann 
1067a33a7d73SArnd Bergmann 	return 0;
1068a33a7d73SArnd Bergmann }
1069a33a7d73SArnd Bergmann 
1070a33a7d73SArnd Bergmann static int spu_send_mfc_command(struct spu_context *ctx,
1071a33a7d73SArnd Bergmann 				struct mfc_dma_command cmd,
1072a33a7d73SArnd Bergmann 				int *error)
1073a33a7d73SArnd Bergmann {
1074a33a7d73SArnd Bergmann 	*error = ctx->ops->send_mfc_command(ctx, &cmd);
1075a33a7d73SArnd Bergmann 	if (*error == -EAGAIN) {
1076a33a7d73SArnd Bergmann 		/* wait for any tag group to complete
1077a33a7d73SArnd Bergmann 		   so we have space for the new command */
1078a33a7d73SArnd Bergmann 		ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1079a33a7d73SArnd Bergmann 		/* try again, because the queue might be
1080a33a7d73SArnd Bergmann 		   empty again */
1081a33a7d73SArnd Bergmann 		*error = ctx->ops->send_mfc_command(ctx, &cmd);
1082a33a7d73SArnd Bergmann 		if (*error == -EAGAIN)
1083a33a7d73SArnd Bergmann 			return 0;
1084a33a7d73SArnd Bergmann 	}
1085a33a7d73SArnd Bergmann 	return 1;
1086a33a7d73SArnd Bergmann }
1087a33a7d73SArnd Bergmann 
1088a33a7d73SArnd Bergmann static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1089a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1090a33a7d73SArnd Bergmann {
1091a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1092a33a7d73SArnd Bergmann 	struct mfc_dma_command cmd;
1093a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1094a33a7d73SArnd Bergmann 
1095a33a7d73SArnd Bergmann 	if (size != sizeof cmd)
1096a33a7d73SArnd Bergmann 		goto out;
1097a33a7d73SArnd Bergmann 
1098a33a7d73SArnd Bergmann 	ret = -EFAULT;
1099a33a7d73SArnd Bergmann 	if (copy_from_user(&cmd, buffer, sizeof cmd))
1100a33a7d73SArnd Bergmann 		goto out;
1101a33a7d73SArnd Bergmann 
1102a33a7d73SArnd Bergmann 	ret = spufs_check_valid_dma(&cmd);
1103a33a7d73SArnd Bergmann 	if (ret)
1104a33a7d73SArnd Bergmann 		goto out;
1105a33a7d73SArnd Bergmann 
1106a33a7d73SArnd Bergmann 	spu_acquire_runnable(ctx);
1107a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1108a33a7d73SArnd Bergmann 		ret = ctx->ops->send_mfc_command(ctx, &cmd);
1109a33a7d73SArnd Bergmann 	} else {
1110a33a7d73SArnd Bergmann 		int status;
1111a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1112a33a7d73SArnd Bergmann 				 spu_send_mfc_command(ctx, cmd, &status));
1113a33a7d73SArnd Bergmann 		if (status)
1114a33a7d73SArnd Bergmann 			ret = status;
1115a33a7d73SArnd Bergmann 	}
1116a33a7d73SArnd Bergmann 	spu_release(ctx);
1117a33a7d73SArnd Bergmann 
1118a33a7d73SArnd Bergmann 	if (ret)
1119a33a7d73SArnd Bergmann 		goto out;
1120a33a7d73SArnd Bergmann 
1121a33a7d73SArnd Bergmann 	ctx->tagwait |= 1 << cmd.tag;
1122a33a7d73SArnd Bergmann 
1123a33a7d73SArnd Bergmann out:
1124a33a7d73SArnd Bergmann 	return ret;
1125a33a7d73SArnd Bergmann }
1126a33a7d73SArnd Bergmann 
1127a33a7d73SArnd Bergmann static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1128a33a7d73SArnd Bergmann {
1129a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1130a33a7d73SArnd Bergmann 	u32 free_elements, tagstatus;
1131a33a7d73SArnd Bergmann 	unsigned int mask;
1132a33a7d73SArnd Bergmann 
1133a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1134a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1135a33a7d73SArnd Bergmann 	free_elements = ctx->ops->get_mfc_free_elements(ctx);
1136a33a7d73SArnd Bergmann 	tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1137a33a7d73SArnd Bergmann 	spu_release(ctx);
1138a33a7d73SArnd Bergmann 
1139a33a7d73SArnd Bergmann 	poll_wait(file, &ctx->mfc_wq, wait);
1140a33a7d73SArnd Bergmann 
1141a33a7d73SArnd Bergmann 	mask = 0;
1142a33a7d73SArnd Bergmann 	if (free_elements & 0xffff)
1143a33a7d73SArnd Bergmann 		mask |= POLLOUT | POLLWRNORM;
1144a33a7d73SArnd Bergmann 	if (tagstatus & ctx->tagwait)
1145a33a7d73SArnd Bergmann 		mask |= POLLIN | POLLRDNORM;
1146a33a7d73SArnd Bergmann 
1147a33a7d73SArnd Bergmann 	pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__,
1148a33a7d73SArnd Bergmann 		free_elements, tagstatus, ctx->tagwait);
1149a33a7d73SArnd Bergmann 
1150a33a7d73SArnd Bergmann 	return mask;
1151a33a7d73SArnd Bergmann }
1152a33a7d73SArnd Bergmann 
1153a33a7d73SArnd Bergmann static int spufs_mfc_flush(struct file *file)
1154a33a7d73SArnd Bergmann {
1155a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1156a33a7d73SArnd Bergmann 	int ret;
1157a33a7d73SArnd Bergmann 
1158a33a7d73SArnd Bergmann 	spu_acquire(ctx);
1159a33a7d73SArnd Bergmann #if 0
1160a33a7d73SArnd Bergmann /* this currently hangs */
1161a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1162a33a7d73SArnd Bergmann 			 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1163a33a7d73SArnd Bergmann 	if (ret)
1164a33a7d73SArnd Bergmann 		goto out;
1165a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1166a33a7d73SArnd Bergmann 			 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1167a33a7d73SArnd Bergmann out:
1168a33a7d73SArnd Bergmann #else
1169a33a7d73SArnd Bergmann 	ret = 0;
1170a33a7d73SArnd Bergmann #endif
1171a33a7d73SArnd Bergmann 	spu_release(ctx);
1172a33a7d73SArnd Bergmann 
1173a33a7d73SArnd Bergmann 	return ret;
1174a33a7d73SArnd Bergmann }
1175a33a7d73SArnd Bergmann 
1176a33a7d73SArnd Bergmann static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1177a33a7d73SArnd Bergmann 			   int datasync)
1178a33a7d73SArnd Bergmann {
1179a33a7d73SArnd Bergmann 	return spufs_mfc_flush(file);
1180a33a7d73SArnd Bergmann }
1181a33a7d73SArnd Bergmann 
1182a33a7d73SArnd Bergmann static int spufs_mfc_fasync(int fd, struct file *file, int on)
1183a33a7d73SArnd Bergmann {
1184a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1185a33a7d73SArnd Bergmann 
1186a33a7d73SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1187a33a7d73SArnd Bergmann }
1188a33a7d73SArnd Bergmann 
1189a33a7d73SArnd Bergmann static struct file_operations spufs_mfc_fops = {
1190a33a7d73SArnd Bergmann 	.open	 = spufs_mfc_open,
1191a33a7d73SArnd Bergmann 	.read	 = spufs_mfc_read,
1192a33a7d73SArnd Bergmann 	.write	 = spufs_mfc_write,
1193a33a7d73SArnd Bergmann 	.poll	 = spufs_mfc_poll,
1194a33a7d73SArnd Bergmann 	.flush	 = spufs_mfc_flush,
1195a33a7d73SArnd Bergmann 	.fsync	 = spufs_mfc_fsync,
1196a33a7d73SArnd Bergmann 	.fasync	 = spufs_mfc_fasync,
11976df10a82SMark Nutter #ifdef CONFIG_SPUFS_MMAP
11986df10a82SMark Nutter 	.mmap	 = spufs_mfc_mmap,
11996df10a82SMark Nutter #endif
1200a33a7d73SArnd Bergmann };
1201a33a7d73SArnd Bergmann 
120267207b96SArnd Bergmann static void spufs_npc_set(void *data, u64 val)
120367207b96SArnd Bergmann {
120467207b96SArnd Bergmann 	struct spu_context *ctx = data;
12058b3d6663SArnd Bergmann 	spu_acquire(ctx);
12068b3d6663SArnd Bergmann 	ctx->ops->npc_write(ctx, val);
12078b3d6663SArnd Bergmann 	spu_release(ctx);
120867207b96SArnd Bergmann }
120967207b96SArnd Bergmann 
121067207b96SArnd Bergmann static u64 spufs_npc_get(void *data)
121167207b96SArnd Bergmann {
121267207b96SArnd Bergmann 	struct spu_context *ctx = data;
121367207b96SArnd Bergmann 	u64 ret;
12148b3d6663SArnd Bergmann 	spu_acquire(ctx);
12158b3d6663SArnd Bergmann 	ret = ctx->ops->npc_read(ctx);
12168b3d6663SArnd Bergmann 	spu_release(ctx);
121767207b96SArnd Bergmann 	return ret;
121867207b96SArnd Bergmann }
121967207b96SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n")
122067207b96SArnd Bergmann 
12218b3d6663SArnd Bergmann static void spufs_decr_set(void *data, u64 val)
12228b3d6663SArnd Bergmann {
12238b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12248b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12258b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12268b3d6663SArnd Bergmann 	lscsa->decr.slot[0] = (u32) val;
12278b3d6663SArnd Bergmann 	spu_release(ctx);
12288b3d6663SArnd Bergmann }
12298b3d6663SArnd Bergmann 
12308b3d6663SArnd Bergmann static u64 spufs_decr_get(void *data)
12318b3d6663SArnd Bergmann {
12328b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12338b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12348b3d6663SArnd Bergmann 	u64 ret;
12358b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12368b3d6663SArnd Bergmann 	ret = lscsa->decr.slot[0];
12378b3d6663SArnd Bergmann 	spu_release(ctx);
12388b3d6663SArnd Bergmann 	return ret;
12398b3d6663SArnd Bergmann }
12408b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
12418b3d6663SArnd Bergmann 			"%llx\n")
12428b3d6663SArnd Bergmann 
12438b3d6663SArnd Bergmann static void spufs_decr_status_set(void *data, u64 val)
12448b3d6663SArnd Bergmann {
12458b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12468b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12478b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12488b3d6663SArnd Bergmann 	lscsa->decr_status.slot[0] = (u32) val;
12498b3d6663SArnd Bergmann 	spu_release(ctx);
12508b3d6663SArnd Bergmann }
12518b3d6663SArnd Bergmann 
12528b3d6663SArnd Bergmann static u64 spufs_decr_status_get(void *data)
12538b3d6663SArnd Bergmann {
12548b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12558b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12568b3d6663SArnd Bergmann 	u64 ret;
12578b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12588b3d6663SArnd Bergmann 	ret = lscsa->decr_status.slot[0];
12598b3d6663SArnd Bergmann 	spu_release(ctx);
12608b3d6663SArnd Bergmann 	return ret;
12618b3d6663SArnd Bergmann }
12628b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
12638b3d6663SArnd Bergmann 			spufs_decr_status_set, "%llx\n")
12648b3d6663SArnd Bergmann 
12658b3d6663SArnd Bergmann static void spufs_spu_tag_mask_set(void *data, u64 val)
12668b3d6663SArnd Bergmann {
12678b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12688b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12698b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12708b3d6663SArnd Bergmann 	lscsa->tag_mask.slot[0] = (u32) val;
12718b3d6663SArnd Bergmann 	spu_release(ctx);
12728b3d6663SArnd Bergmann }
12738b3d6663SArnd Bergmann 
12748b3d6663SArnd Bergmann static u64 spufs_spu_tag_mask_get(void *data)
12758b3d6663SArnd Bergmann {
12768b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12778b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12788b3d6663SArnd Bergmann 	u64 ret;
12798b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12808b3d6663SArnd Bergmann 	ret = lscsa->tag_mask.slot[0];
12818b3d6663SArnd Bergmann 	spu_release(ctx);
12828b3d6663SArnd Bergmann 	return ret;
12838b3d6663SArnd Bergmann }
12848b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get,
12858b3d6663SArnd Bergmann 			spufs_spu_tag_mask_set, "%llx\n")
12868b3d6663SArnd Bergmann 
12878b3d6663SArnd Bergmann static void spufs_event_mask_set(void *data, u64 val)
12888b3d6663SArnd Bergmann {
12898b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12908b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
12918b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
12928b3d6663SArnd Bergmann 	lscsa->event_mask.slot[0] = (u32) val;
12938b3d6663SArnd Bergmann 	spu_release(ctx);
12948b3d6663SArnd Bergmann }
12958b3d6663SArnd Bergmann 
12968b3d6663SArnd Bergmann static u64 spufs_event_mask_get(void *data)
12978b3d6663SArnd Bergmann {
12988b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
12998b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13008b3d6663SArnd Bergmann 	u64 ret;
13018b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13028b3d6663SArnd Bergmann 	ret = lscsa->event_mask.slot[0];
13038b3d6663SArnd Bergmann 	spu_release(ctx);
13048b3d6663SArnd Bergmann 	return ret;
13058b3d6663SArnd Bergmann }
13068b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
13078b3d6663SArnd Bergmann 			spufs_event_mask_set, "%llx\n")
13088b3d6663SArnd Bergmann 
13098b3d6663SArnd Bergmann static void spufs_srr0_set(void *data, u64 val)
13108b3d6663SArnd Bergmann {
13118b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
13128b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13138b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13148b3d6663SArnd Bergmann 	lscsa->srr0.slot[0] = (u32) val;
13158b3d6663SArnd Bergmann 	spu_release(ctx);
13168b3d6663SArnd Bergmann }
13178b3d6663SArnd Bergmann 
13188b3d6663SArnd Bergmann static u64 spufs_srr0_get(void *data)
13198b3d6663SArnd Bergmann {
13208b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
13218b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
13228b3d6663SArnd Bergmann 	u64 ret;
13238b3d6663SArnd Bergmann 	spu_acquire_saved(ctx);
13248b3d6663SArnd Bergmann 	ret = lscsa->srr0.slot[0];
13258b3d6663SArnd Bergmann 	spu_release(ctx);
13268b3d6663SArnd Bergmann 	return ret;
13278b3d6663SArnd Bergmann }
13288b3d6663SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
13298b3d6663SArnd Bergmann 			"%llx\n")
13308b3d6663SArnd Bergmann 
133167207b96SArnd Bergmann struct tree_descr spufs_dir_contents[] = {
133267207b96SArnd Bergmann 	{ "mem",  &spufs_mem_fops,  0666, },
13338b3d6663SArnd Bergmann 	{ "regs", &spufs_regs_fops,  0666, },
133467207b96SArnd Bergmann 	{ "mbox", &spufs_mbox_fops, 0444, },
133567207b96SArnd Bergmann 	{ "ibox", &spufs_ibox_fops, 0444, },
133667207b96SArnd Bergmann 	{ "wbox", &spufs_wbox_fops, 0222, },
133767207b96SArnd Bergmann 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, },
133867207b96SArnd Bergmann 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, },
133967207b96SArnd Bergmann 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, },
134067207b96SArnd Bergmann 	{ "signal1", &spufs_signal1_fops, 0666, },
134167207b96SArnd Bergmann 	{ "signal2", &spufs_signal2_fops, 0666, },
134267207b96SArnd Bergmann 	{ "signal1_type", &spufs_signal1_type, 0666, },
134367207b96SArnd Bergmann 	{ "signal2_type", &spufs_signal2_type, 0666, },
1344d9379c4bSarnd@arndb.de 	{ "mss", &spufs_mss_fops, 0666, },
1345a33a7d73SArnd Bergmann 	{ "mfc", &spufs_mfc_fops, 0666, },
13466df10a82SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
134767207b96SArnd Bergmann 	{ "npc", &spufs_npc_ops, 0666, },
13488b3d6663SArnd Bergmann 	{ "fpcr", &spufs_fpcr_fops, 0666, },
13498b3d6663SArnd Bergmann 	{ "decr", &spufs_decr_ops, 0666, },
13508b3d6663SArnd Bergmann 	{ "decr_status", &spufs_decr_status_ops, 0666, },
13518b3d6663SArnd Bergmann 	{ "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, },
13528b3d6663SArnd Bergmann 	{ "event_mask", &spufs_event_mask_ops, 0666, },
13538b3d6663SArnd Bergmann 	{ "srr0", &spufs_srr0_ops, 0666, },
135467207b96SArnd Bergmann 	{},
135567207b96SArnd Bergmann };
1356