xref: /linux/arch/powerpc/platforms/cell/spufs/file.c (revision 14f693eeb5b16bc47ffa38d8b8838a654aedd53f)
167207b96SArnd Bergmann /*
267207b96SArnd Bergmann  * SPU file system -- file contents
367207b96SArnd Bergmann  *
467207b96SArnd Bergmann  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
567207b96SArnd Bergmann  *
667207b96SArnd Bergmann  * Author: Arnd Bergmann <arndb@de.ibm.com>
767207b96SArnd Bergmann  *
867207b96SArnd Bergmann  * This program is free software; you can redistribute it and/or modify
967207b96SArnd Bergmann  * it under the terms of the GNU General Public License as published by
1067207b96SArnd Bergmann  * the Free Software Foundation; either version 2, or (at your option)
1167207b96SArnd Bergmann  * any later version.
1267207b96SArnd Bergmann  *
1367207b96SArnd Bergmann  * This program is distributed in the hope that it will be useful,
1467207b96SArnd Bergmann  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1567207b96SArnd Bergmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1667207b96SArnd Bergmann  * GNU General Public License for more details.
1767207b96SArnd Bergmann  *
1867207b96SArnd Bergmann  * You should have received a copy of the GNU General Public License
1967207b96SArnd Bergmann  * along with this program; if not, write to the Free Software
2067207b96SArnd Bergmann  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2167207b96SArnd Bergmann  */
2267207b96SArnd Bergmann 
23a33a7d73SArnd Bergmann #undef DEBUG
24a33a7d73SArnd Bergmann 
2567207b96SArnd Bergmann #include <linux/fs.h>
2667207b96SArnd Bergmann #include <linux/ioctl.h>
2767207b96SArnd Bergmann #include <linux/module.h>
28d88cfffaSArnd Bergmann #include <linux/pagemap.h>
2967207b96SArnd Bergmann #include <linux/poll.h>
305110459fSArnd Bergmann #include <linux/ptrace.h>
31cbe709c1SBenjamin Herrenschmidt #include <linux/seq_file.h>
32038200cfSChristoph Hellwig #include <linux/marker.h>
3367207b96SArnd Bergmann 
3467207b96SArnd Bergmann #include <asm/io.h>
35dfe1e09fSFUJITA Tomonori #include <asm/time.h>
3667207b96SArnd Bergmann #include <asm/spu.h>
37b9e3bd77SDwayne Grant McConnell #include <asm/spu_info.h>
3867207b96SArnd Bergmann #include <asm/uaccess.h>
3967207b96SArnd Bergmann 
4067207b96SArnd Bergmann #include "spufs.h"
4167207b96SArnd Bergmann 
4227d5bf2aSBenjamin Herrenschmidt #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
4327d5bf2aSBenjamin Herrenschmidt 
44197b1a82SChristoph Hellwig /* Simple attribute files */
45197b1a82SChristoph Hellwig struct spufs_attr {
46197b1a82SChristoph Hellwig 	int (*get)(void *, u64 *);
47197b1a82SChristoph Hellwig 	int (*set)(void *, u64);
48197b1a82SChristoph Hellwig 	char get_buf[24];       /* enough to store a u64 and "\n\0" */
49197b1a82SChristoph Hellwig 	char set_buf[24];
50197b1a82SChristoph Hellwig 	void *data;
51197b1a82SChristoph Hellwig 	const char *fmt;        /* format for read operation */
52197b1a82SChristoph Hellwig 	struct mutex mutex;     /* protects access to these buffers */
53197b1a82SChristoph Hellwig };
54197b1a82SChristoph Hellwig 
55197b1a82SChristoph Hellwig static int spufs_attr_open(struct inode *inode, struct file *file,
56197b1a82SChristoph Hellwig 		int (*get)(void *, u64 *), int (*set)(void *, u64),
57197b1a82SChristoph Hellwig 		const char *fmt)
58197b1a82SChristoph Hellwig {
59197b1a82SChristoph Hellwig 	struct spufs_attr *attr;
60197b1a82SChristoph Hellwig 
61197b1a82SChristoph Hellwig 	attr = kmalloc(sizeof(*attr), GFP_KERNEL);
62197b1a82SChristoph Hellwig 	if (!attr)
63197b1a82SChristoph Hellwig 		return -ENOMEM;
64197b1a82SChristoph Hellwig 
65197b1a82SChristoph Hellwig 	attr->get = get;
66197b1a82SChristoph Hellwig 	attr->set = set;
67197b1a82SChristoph Hellwig 	attr->data = inode->i_private;
68197b1a82SChristoph Hellwig 	attr->fmt = fmt;
69197b1a82SChristoph Hellwig 	mutex_init(&attr->mutex);
70197b1a82SChristoph Hellwig 	file->private_data = attr;
71197b1a82SChristoph Hellwig 
72197b1a82SChristoph Hellwig 	return nonseekable_open(inode, file);
73197b1a82SChristoph Hellwig }
74197b1a82SChristoph Hellwig 
75197b1a82SChristoph Hellwig static int spufs_attr_release(struct inode *inode, struct file *file)
76197b1a82SChristoph Hellwig {
77197b1a82SChristoph Hellwig        kfree(file->private_data);
78197b1a82SChristoph Hellwig 	return 0;
79197b1a82SChristoph Hellwig }
80197b1a82SChristoph Hellwig 
81197b1a82SChristoph Hellwig static ssize_t spufs_attr_read(struct file *file, char __user *buf,
82197b1a82SChristoph Hellwig 		size_t len, loff_t *ppos)
83197b1a82SChristoph Hellwig {
84197b1a82SChristoph Hellwig 	struct spufs_attr *attr;
85197b1a82SChristoph Hellwig 	size_t size;
86197b1a82SChristoph Hellwig 	ssize_t ret;
87197b1a82SChristoph Hellwig 
88197b1a82SChristoph Hellwig 	attr = file->private_data;
89197b1a82SChristoph Hellwig 	if (!attr->get)
90197b1a82SChristoph Hellwig 		return -EACCES;
91197b1a82SChristoph Hellwig 
92197b1a82SChristoph Hellwig 	ret = mutex_lock_interruptible(&attr->mutex);
93197b1a82SChristoph Hellwig 	if (ret)
94197b1a82SChristoph Hellwig 		return ret;
95197b1a82SChristoph Hellwig 
96197b1a82SChristoph Hellwig 	if (*ppos) {		/* continued read */
97197b1a82SChristoph Hellwig 		size = strlen(attr->get_buf);
98197b1a82SChristoph Hellwig 	} else {		/* first read */
99197b1a82SChristoph Hellwig 		u64 val;
100197b1a82SChristoph Hellwig 		ret = attr->get(attr->data, &val);
101197b1a82SChristoph Hellwig 		if (ret)
102197b1a82SChristoph Hellwig 			goto out;
103197b1a82SChristoph Hellwig 
104197b1a82SChristoph Hellwig 		size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
105197b1a82SChristoph Hellwig 				 attr->fmt, (unsigned long long)val);
106197b1a82SChristoph Hellwig 	}
107197b1a82SChristoph Hellwig 
108197b1a82SChristoph Hellwig 	ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
109197b1a82SChristoph Hellwig out:
110197b1a82SChristoph Hellwig 	mutex_unlock(&attr->mutex);
111197b1a82SChristoph Hellwig 	return ret;
112197b1a82SChristoph Hellwig }
113197b1a82SChristoph Hellwig 
114197b1a82SChristoph Hellwig static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
115197b1a82SChristoph Hellwig 		size_t len, loff_t *ppos)
116197b1a82SChristoph Hellwig {
117197b1a82SChristoph Hellwig 	struct spufs_attr *attr;
118197b1a82SChristoph Hellwig 	u64 val;
119197b1a82SChristoph Hellwig 	size_t size;
120197b1a82SChristoph Hellwig 	ssize_t ret;
121197b1a82SChristoph Hellwig 
122197b1a82SChristoph Hellwig 	attr = file->private_data;
123197b1a82SChristoph Hellwig 	if (!attr->set)
124197b1a82SChristoph Hellwig 		return -EACCES;
125197b1a82SChristoph Hellwig 
126197b1a82SChristoph Hellwig 	ret = mutex_lock_interruptible(&attr->mutex);
127197b1a82SChristoph Hellwig 	if (ret)
128197b1a82SChristoph Hellwig 		return ret;
129197b1a82SChristoph Hellwig 
130197b1a82SChristoph Hellwig 	ret = -EFAULT;
131197b1a82SChristoph Hellwig 	size = min(sizeof(attr->set_buf) - 1, len);
132197b1a82SChristoph Hellwig 	if (copy_from_user(attr->set_buf, buf, size))
133197b1a82SChristoph Hellwig 		goto out;
134197b1a82SChristoph Hellwig 
135197b1a82SChristoph Hellwig 	ret = len; /* claim we got the whole input */
136197b1a82SChristoph Hellwig 	attr->set_buf[size] = '\0';
137197b1a82SChristoph Hellwig 	val = simple_strtol(attr->set_buf, NULL, 0);
138197b1a82SChristoph Hellwig 	attr->set(attr->data, val);
139197b1a82SChristoph Hellwig out:
140197b1a82SChristoph Hellwig 	mutex_unlock(&attr->mutex);
141197b1a82SChristoph Hellwig 	return ret;
142197b1a82SChristoph Hellwig }
143197b1a82SChristoph Hellwig 
144197b1a82SChristoph Hellwig #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt)	\
145197b1a82SChristoph Hellwig static int __fops ## _open(struct inode *inode, struct file *file)	\
146197b1a82SChristoph Hellwig {									\
147197b1a82SChristoph Hellwig 	__simple_attr_check_format(__fmt, 0ull);			\
148197b1a82SChristoph Hellwig 	return spufs_attr_open(inode, file, __get, __set, __fmt);	\
149197b1a82SChristoph Hellwig }									\
150197b1a82SChristoph Hellwig static struct file_operations __fops = {				\
151197b1a82SChristoph Hellwig 	.owner	 = THIS_MODULE,						\
152197b1a82SChristoph Hellwig 	.open	 = __fops ## _open,					\
153197b1a82SChristoph Hellwig 	.release = spufs_attr_release,					\
154197b1a82SChristoph Hellwig 	.read	 = spufs_attr_read,					\
155197b1a82SChristoph Hellwig 	.write	 = spufs_attr_write,					\
156197b1a82SChristoph Hellwig };
157197b1a82SChristoph Hellwig 
158cbe709c1SBenjamin Herrenschmidt 
15967207b96SArnd Bergmann static int
16067207b96SArnd Bergmann spufs_mem_open(struct inode *inode, struct file *file)
16167207b96SArnd Bergmann {
16267207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
1636df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
16443c2bbd9SChristoph Hellwig 
16547d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
1666df10a82SMark Nutter 	file->private_data = ctx;
16743c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
1686df10a82SMark Nutter 		ctx->local_store = inode->i_mapping;
16947d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
17043c2bbd9SChristoph Hellwig 	return 0;
17143c2bbd9SChristoph Hellwig }
17243c2bbd9SChristoph Hellwig 
17343c2bbd9SChristoph Hellwig static int
17443c2bbd9SChristoph Hellwig spufs_mem_release(struct inode *inode, struct file *file)
17543c2bbd9SChristoph Hellwig {
17643c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
17743c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
17843c2bbd9SChristoph Hellwig 
17947d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
18043c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
18143c2bbd9SChristoph Hellwig 		ctx->local_store = NULL;
18247d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
18367207b96SArnd Bergmann 	return 0;
18467207b96SArnd Bergmann }
18567207b96SArnd Bergmann 
18667207b96SArnd Bergmann static ssize_t
187bf1ab978SDwayne Grant McConnell __spufs_mem_read(struct spu_context *ctx, char __user *buffer,
188bf1ab978SDwayne Grant McConnell 			size_t size, loff_t *pos)
189bf1ab978SDwayne Grant McConnell {
190bf1ab978SDwayne Grant McConnell 	char *local_store = ctx->ops->get_ls(ctx);
191bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buffer, size, pos, local_store,
192bf1ab978SDwayne Grant McConnell 					LS_SIZE);
193bf1ab978SDwayne Grant McConnell }
194bf1ab978SDwayne Grant McConnell 
195bf1ab978SDwayne Grant McConnell static ssize_t
19667207b96SArnd Bergmann spufs_mem_read(struct file *file, char __user *buffer,
19767207b96SArnd Bergmann 				size_t size, loff_t *pos)
19867207b96SArnd Bergmann {
199bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
200aa0ed2bdSArnd Bergmann 	ssize_t ret;
20167207b96SArnd Bergmann 
202c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
203c9101bdbSChristoph Hellwig 	if (ret)
204c9101bdbSChristoph Hellwig 		return ret;
205bf1ab978SDwayne Grant McConnell 	ret = __spufs_mem_read(ctx, buffer, size, pos);
2068b3d6663SArnd Bergmann 	spu_release(ctx);
207c9101bdbSChristoph Hellwig 
20867207b96SArnd Bergmann 	return ret;
20967207b96SArnd Bergmann }
21067207b96SArnd Bergmann 
21167207b96SArnd Bergmann static ssize_t
21267207b96SArnd Bergmann spufs_mem_write(struct file *file, const char __user *buffer,
213aa0ed2bdSArnd Bergmann 					size_t size, loff_t *ppos)
21467207b96SArnd Bergmann {
21567207b96SArnd Bergmann 	struct spu_context *ctx = file->private_data;
2168b3d6663SArnd Bergmann 	char *local_store;
217aa0ed2bdSArnd Bergmann 	loff_t pos = *ppos;
2188b3d6663SArnd Bergmann 	int ret;
21967207b96SArnd Bergmann 
220aa0ed2bdSArnd Bergmann 	if (pos < 0)
221aa0ed2bdSArnd Bergmann 		return -EINVAL;
222aa0ed2bdSArnd Bergmann 	if (pos > LS_SIZE)
22367207b96SArnd Bergmann 		return -EFBIG;
224aa0ed2bdSArnd Bergmann 	if (size > LS_SIZE - pos)
225aa0ed2bdSArnd Bergmann 		size = LS_SIZE - pos;
2268b3d6663SArnd Bergmann 
227c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
228c9101bdbSChristoph Hellwig 	if (ret)
229c9101bdbSChristoph Hellwig 		return ret;
230c9101bdbSChristoph Hellwig 
2318b3d6663SArnd Bergmann 	local_store = ctx->ops->get_ls(ctx);
232aa0ed2bdSArnd Bergmann 	ret = copy_from_user(local_store + pos, buffer, size);
2338b3d6663SArnd Bergmann 	spu_release(ctx);
234aa0ed2bdSArnd Bergmann 
235aa0ed2bdSArnd Bergmann 	if (ret)
236aa0ed2bdSArnd Bergmann 		return -EFAULT;
237aa0ed2bdSArnd Bergmann 	*ppos = pos + size;
238aa0ed2bdSArnd Bergmann 	return size;
23967207b96SArnd Bergmann }
24067207b96SArnd Bergmann 
241b1e2270fSNick Piggin static int
242b1e2270fSNick Piggin spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2438b3d6663SArnd Bergmann {
2448b3d6663SArnd Bergmann 	struct spu_context *ctx	= vma->vm_file->private_data;
245b1e2270fSNick Piggin 	unsigned long address = (unsigned long)vmf->virtual_address;
246b1e2270fSNick Piggin 	unsigned long pfn, offset;
247b1e2270fSNick Piggin 
248f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS
249f1fa74f4SBenjamin Herrenschmidt 	struct spu_state *csa = &ctx->csa;
250f1fa74f4SBenjamin Herrenschmidt 	int psize;
25178bde53eSBenjamin Herrenschmidt 
252f1fa74f4SBenjamin Herrenschmidt 	/* Check what page size we are using */
253f1fa74f4SBenjamin Herrenschmidt 	psize = get_slice_psize(vma->vm_mm, address);
2548b3d6663SArnd Bergmann 
255f1fa74f4SBenjamin Herrenschmidt 	/* Some sanity checking */
256f1fa74f4SBenjamin Herrenschmidt 	BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
257f1fa74f4SBenjamin Herrenschmidt 
258f1fa74f4SBenjamin Herrenschmidt 	/* Wow, 64K, cool, we need to align the address though */
259f1fa74f4SBenjamin Herrenschmidt 	if (csa->use_big_pages) {
260f1fa74f4SBenjamin Herrenschmidt 		BUG_ON(vma->vm_start & 0xffff);
261f1fa74f4SBenjamin Herrenschmidt 		address &= ~0xfffful;
262f1fa74f4SBenjamin Herrenschmidt 	}
263f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */
264f1fa74f4SBenjamin Herrenschmidt 
265b1e2270fSNick Piggin 	offset = vmf->pgoff << PAGE_SHIFT;
266128b8546SMasato Noguchi 	if (offset >= LS_SIZE)
267b1e2270fSNick Piggin 		return VM_FAULT_SIGBUS;
268128b8546SMasato Noguchi 
269b1e2270fSNick Piggin 	pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
270b1e2270fSNick Piggin 			address, offset);
271f1fa74f4SBenjamin Herrenschmidt 
272c9101bdbSChristoph Hellwig 	if (spu_acquire(ctx))
273b1e2270fSNick Piggin 		return VM_FAULT_NOPAGE;
2748b3d6663SArnd Bergmann 
275ac91cb8dSArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED) {
276ac91cb8dSArnd Bergmann 		vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
277932f535dSArnd Bergmann 							& ~_PAGE_NO_CACHE);
27878bde53eSBenjamin Herrenschmidt 		pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
279ac91cb8dSArnd Bergmann 	} else {
280ac91cb8dSArnd Bergmann 		vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
281932f535dSArnd Bergmann 					     | _PAGE_NO_CACHE);
28278bde53eSBenjamin Herrenschmidt 		pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
283ac91cb8dSArnd Bergmann 	}
28478bde53eSBenjamin Herrenschmidt 	vm_insert_pfn(vma, address, pfn);
28578bde53eSBenjamin Herrenschmidt 
2868b3d6663SArnd Bergmann 	spu_release(ctx);
2878b3d6663SArnd Bergmann 
288b1e2270fSNick Piggin 	return VM_FAULT_NOPAGE;
2898b3d6663SArnd Bergmann }
2908b3d6663SArnd Bergmann 
291a352894dSBenjamin Herrenschmidt static int spufs_mem_mmap_access(struct vm_area_struct *vma,
292a352894dSBenjamin Herrenschmidt 				unsigned long address,
293a352894dSBenjamin Herrenschmidt 				void *buf, int len, int write)
294a352894dSBenjamin Herrenschmidt {
295a352894dSBenjamin Herrenschmidt 	struct spu_context *ctx = vma->vm_file->private_data;
296a352894dSBenjamin Herrenschmidt 	unsigned long offset = address - vma->vm_start;
297a352894dSBenjamin Herrenschmidt 	char *local_store;
298a352894dSBenjamin Herrenschmidt 
299a352894dSBenjamin Herrenschmidt 	if (write && !(vma->vm_flags & VM_WRITE))
300a352894dSBenjamin Herrenschmidt 		return -EACCES;
301a352894dSBenjamin Herrenschmidt 	if (spu_acquire(ctx))
302a352894dSBenjamin Herrenschmidt 		return -EINTR;
303a352894dSBenjamin Herrenschmidt 	if ((offset + len) > vma->vm_end)
304a352894dSBenjamin Herrenschmidt 		len = vma->vm_end - offset;
305a352894dSBenjamin Herrenschmidt 	local_store = ctx->ops->get_ls(ctx);
306a352894dSBenjamin Herrenschmidt 	if (write)
307a352894dSBenjamin Herrenschmidt 		memcpy_toio(local_store + offset, buf, len);
308a352894dSBenjamin Herrenschmidt 	else
309a352894dSBenjamin Herrenschmidt 		memcpy_fromio(buf, local_store + offset, len);
310a352894dSBenjamin Herrenschmidt 	spu_release(ctx);
311a352894dSBenjamin Herrenschmidt 	return len;
312a352894dSBenjamin Herrenschmidt }
31378bde53eSBenjamin Herrenschmidt 
3148b3d6663SArnd Bergmann static struct vm_operations_struct spufs_mem_mmap_vmops = {
315b1e2270fSNick Piggin 	.fault = spufs_mem_mmap_fault,
316a352894dSBenjamin Herrenschmidt 	.access = spufs_mem_mmap_access,
3178b3d6663SArnd Bergmann };
3188b3d6663SArnd Bergmann 
319f1fa74f4SBenjamin Herrenschmidt static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
32067207b96SArnd Bergmann {
321f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS
322f1fa74f4SBenjamin Herrenschmidt 	struct spu_context	*ctx = file->private_data;
323f1fa74f4SBenjamin Herrenschmidt 	struct spu_state	*csa = &ctx->csa;
324f1fa74f4SBenjamin Herrenschmidt 
325f1fa74f4SBenjamin Herrenschmidt 	/* Sanity check VMA alignment */
326f1fa74f4SBenjamin Herrenschmidt 	if (csa->use_big_pages) {
327f1fa74f4SBenjamin Herrenschmidt 		pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
328f1fa74f4SBenjamin Herrenschmidt 			 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
329f1fa74f4SBenjamin Herrenschmidt 			 vma->vm_pgoff);
330f1fa74f4SBenjamin Herrenschmidt 		if (vma->vm_start & 0xffff)
331f1fa74f4SBenjamin Herrenschmidt 			return -EINVAL;
332f1fa74f4SBenjamin Herrenschmidt 		if (vma->vm_pgoff & 0xf)
333f1fa74f4SBenjamin Herrenschmidt 			return -EINVAL;
334f1fa74f4SBenjamin Herrenschmidt 	}
335f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */
336f1fa74f4SBenjamin Herrenschmidt 
3378b3d6663SArnd Bergmann 	if (!(vma->vm_flags & VM_SHARED))
3388b3d6663SArnd Bergmann 		return -EINVAL;
33967207b96SArnd Bergmann 
34078bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
34167207b96SArnd Bergmann 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
34267207b96SArnd Bergmann 				     | _PAGE_NO_CACHE);
3438b3d6663SArnd Bergmann 
3448b3d6663SArnd Bergmann 	vma->vm_ops = &spufs_mem_mmap_vmops;
34567207b96SArnd Bergmann 	return 0;
34667207b96SArnd Bergmann }
34767207b96SArnd Bergmann 
348f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS
3491238819aSSebastian Siewior static unsigned long spufs_get_unmapped_area(struct file *file,
3501238819aSSebastian Siewior 		unsigned long addr, unsigned long len, unsigned long pgoff,
351f1fa74f4SBenjamin Herrenschmidt 		unsigned long flags)
352f1fa74f4SBenjamin Herrenschmidt {
353f1fa74f4SBenjamin Herrenschmidt 	struct spu_context	*ctx = file->private_data;
354f1fa74f4SBenjamin Herrenschmidt 	struct spu_state	*csa = &ctx->csa;
355f1fa74f4SBenjamin Herrenschmidt 
356f1fa74f4SBenjamin Herrenschmidt 	/* If not using big pages, fallback to normal MM g_u_a */
357f1fa74f4SBenjamin Herrenschmidt 	if (!csa->use_big_pages)
358f1fa74f4SBenjamin Herrenschmidt 		return current->mm->get_unmapped_area(file, addr, len,
359f1fa74f4SBenjamin Herrenschmidt 						      pgoff, flags);
360f1fa74f4SBenjamin Herrenschmidt 
361f1fa74f4SBenjamin Herrenschmidt 	/* Else, try to obtain a 64K pages slice */
362f1fa74f4SBenjamin Herrenschmidt 	return slice_get_unmapped_area(addr, len, flags,
363f1fa74f4SBenjamin Herrenschmidt 				       MMU_PAGE_64K, 1, 0);
364f1fa74f4SBenjamin Herrenschmidt }
365f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */
366f1fa74f4SBenjamin Herrenschmidt 
3675dfe4c96SArjan van de Ven static const struct file_operations spufs_mem_fops = {
36867207b96SArnd Bergmann 	.open			= spufs_mem_open,
369ce92987bSChristoph Hellwig 	.release		= spufs_mem_release,
37067207b96SArnd Bergmann 	.read			= spufs_mem_read,
37167207b96SArnd Bergmann 	.write			= spufs_mem_write,
3728b3d6663SArnd Bergmann 	.llseek			= generic_file_llseek,
37367207b96SArnd Bergmann 	.mmap			= spufs_mem_mmap,
374f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS
375f1fa74f4SBenjamin Herrenschmidt 	.get_unmapped_area	= spufs_get_unmapped_area,
376f1fa74f4SBenjamin Herrenschmidt #endif
3778b3d6663SArnd Bergmann };
3788b3d6663SArnd Bergmann 
379b1e2270fSNick Piggin static int spufs_ps_fault(struct vm_area_struct *vma,
380b1e2270fSNick Piggin 				    struct vm_fault *vmf,
38178bde53eSBenjamin Herrenschmidt 				    unsigned long ps_offs,
38227d5bf2aSBenjamin Herrenschmidt 				    unsigned long ps_size)
3836df10a82SMark Nutter {
3846df10a82SMark Nutter 	struct spu_context *ctx = vma->vm_file->private_data;
385b1e2270fSNick Piggin 	unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
386eebead5bSChristoph Hellwig 	int ret = 0;
3876df10a82SMark Nutter 
388b1e2270fSNick Piggin 	spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
389038200cfSChristoph Hellwig 
39027d5bf2aSBenjamin Herrenschmidt 	if (offset >= ps_size)
391b1e2270fSNick Piggin 		return VM_FAULT_SIGBUS;
3926df10a82SMark Nutter 
39333bfd7a7SArnd Bergmann 	/*
394d5883137SJeremy Kerr 	 * Because we release the mmap_sem, the context may be destroyed while
395d5883137SJeremy Kerr 	 * we're in spu_wait. Grab an extra reference so it isn't destroyed
396d5883137SJeremy Kerr 	 * in the meantime.
397d5883137SJeremy Kerr 	 */
398d5883137SJeremy Kerr 	get_spu_context(ctx);
399d5883137SJeremy Kerr 
400d5883137SJeremy Kerr 	/*
40133bfd7a7SArnd Bergmann 	 * We have to wait for context to be loaded before we have
40233bfd7a7SArnd Bergmann 	 * pages to hand out to the user, but we don't want to wait
40333bfd7a7SArnd Bergmann 	 * with the mmap_sem held.
40433bfd7a7SArnd Bergmann 	 * It is possible to drop the mmap_sem here, but then we need
405b1e2270fSNick Piggin 	 * to return VM_FAULT_NOPAGE because the mappings may have
40633bfd7a7SArnd Bergmann 	 * hanged.
40778bde53eSBenjamin Herrenschmidt 	 */
408c9101bdbSChristoph Hellwig 	if (spu_acquire(ctx))
409d5883137SJeremy Kerr 		goto refault;
410c9101bdbSChristoph Hellwig 
41133bfd7a7SArnd Bergmann 	if (ctx->state == SPU_STATE_SAVED) {
41233bfd7a7SArnd Bergmann 		up_read(&current->mm->mmap_sem);
413b1e2270fSNick Piggin 		spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
414eebead5bSChristoph Hellwig 		ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
415b1e2270fSNick Piggin 		spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
41633bfd7a7SArnd Bergmann 		down_read(&current->mm->mmap_sem);
417c9101bdbSChristoph Hellwig 	} else {
4186df10a82SMark Nutter 		area = ctx->spu->problem_phys + ps_offs;
419b1e2270fSNick Piggin 		vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
420b1e2270fSNick Piggin 					(area + offset) >> PAGE_SHIFT);
421b1e2270fSNick Piggin 		spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
422c9101bdbSChristoph Hellwig 	}
42333bfd7a7SArnd Bergmann 
424eebead5bSChristoph Hellwig 	if (!ret)
4256df10a82SMark Nutter 		spu_release(ctx);
426d5883137SJeremy Kerr 
427d5883137SJeremy Kerr refault:
428d5883137SJeremy Kerr 	put_spu_context(ctx);
429b1e2270fSNick Piggin 	return VM_FAULT_NOPAGE;
4306df10a82SMark Nutter }
4316df10a82SMark Nutter 
43227d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
433b1e2270fSNick Piggin static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
434b1e2270fSNick Piggin 					   struct vm_fault *vmf)
4356df10a82SMark Nutter {
43687ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
4376df10a82SMark Nutter }
4386df10a82SMark Nutter 
4396df10a82SMark Nutter static struct vm_operations_struct spufs_cntl_mmap_vmops = {
440b1e2270fSNick Piggin 	.fault = spufs_cntl_mmap_fault,
4416df10a82SMark Nutter };
4426df10a82SMark Nutter 
4436df10a82SMark Nutter /*
4446df10a82SMark Nutter  * mmap support for problem state control area [0x4000 - 0x4fff].
4456df10a82SMark Nutter  */
4466df10a82SMark Nutter static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
4476df10a82SMark Nutter {
4486df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
4496df10a82SMark Nutter 		return -EINVAL;
4506df10a82SMark Nutter 
45178bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
4526df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
45323cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
4546df10a82SMark Nutter 
4556df10a82SMark Nutter 	vma->vm_ops = &spufs_cntl_mmap_vmops;
4566df10a82SMark Nutter 	return 0;
4576df10a82SMark Nutter }
45827d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
45927d5bf2aSBenjamin Herrenschmidt #define spufs_cntl_mmap NULL
46027d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
4616df10a82SMark Nutter 
462197b1a82SChristoph Hellwig static int spufs_cntl_get(void *data, u64 *val)
463e1dbff2bSArnd Bergmann {
464e1dbff2bSArnd Bergmann 	struct spu_context *ctx = data;
465c9101bdbSChristoph Hellwig 	int ret;
466e1dbff2bSArnd Bergmann 
467c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
468c9101bdbSChristoph Hellwig 	if (ret)
469c9101bdbSChristoph Hellwig 		return ret;
470197b1a82SChristoph Hellwig 	*val = ctx->ops->status_read(ctx);
471e1dbff2bSArnd Bergmann 	spu_release(ctx);
472e1dbff2bSArnd Bergmann 
473197b1a82SChristoph Hellwig 	return 0;
474e1dbff2bSArnd Bergmann }
475e1dbff2bSArnd Bergmann 
476197b1a82SChristoph Hellwig static int spufs_cntl_set(void *data, u64 val)
477e1dbff2bSArnd Bergmann {
478e1dbff2bSArnd Bergmann 	struct spu_context *ctx = data;
479c9101bdbSChristoph Hellwig 	int ret;
480e1dbff2bSArnd Bergmann 
481c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
482c9101bdbSChristoph Hellwig 	if (ret)
483c9101bdbSChristoph Hellwig 		return ret;
484e1dbff2bSArnd Bergmann 	ctx->ops->runcntl_write(ctx, val);
485e1dbff2bSArnd Bergmann 	spu_release(ctx);
486197b1a82SChristoph Hellwig 
487197b1a82SChristoph Hellwig 	return 0;
488e1dbff2bSArnd Bergmann }
489e1dbff2bSArnd Bergmann 
4906df10a82SMark Nutter static int spufs_cntl_open(struct inode *inode, struct file *file)
4916df10a82SMark Nutter {
4926df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
4936df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
4946df10a82SMark Nutter 
49547d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
4966df10a82SMark Nutter 	file->private_data = ctx;
49743c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
4986df10a82SMark Nutter 		ctx->cntl = inode->i_mapping;
49947d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
5008b88b099SChristoph Hellwig 	return simple_attr_open(inode, file, spufs_cntl_get,
501e1dbff2bSArnd Bergmann 					spufs_cntl_set, "0x%08lx");
5026df10a82SMark Nutter }
5036df10a82SMark Nutter 
50443c2bbd9SChristoph Hellwig static int
50543c2bbd9SChristoph Hellwig spufs_cntl_release(struct inode *inode, struct file *file)
50643c2bbd9SChristoph Hellwig {
50743c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
50843c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
50943c2bbd9SChristoph Hellwig 
51074bedc4dSChristoph Hellwig 	simple_attr_release(inode, file);
51143c2bbd9SChristoph Hellwig 
51247d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
51343c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
51443c2bbd9SChristoph Hellwig 		ctx->cntl = NULL;
51547d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
51643c2bbd9SChristoph Hellwig 	return 0;
51743c2bbd9SChristoph Hellwig }
51843c2bbd9SChristoph Hellwig 
5195dfe4c96SArjan van de Ven static const struct file_operations spufs_cntl_fops = {
5206df10a82SMark Nutter 	.open = spufs_cntl_open,
52143c2bbd9SChristoph Hellwig 	.release = spufs_cntl_release,
5228b88b099SChristoph Hellwig 	.read = simple_attr_read,
5238b88b099SChristoph Hellwig 	.write = simple_attr_write,
5246df10a82SMark Nutter 	.mmap = spufs_cntl_mmap,
5256df10a82SMark Nutter };
5266df10a82SMark Nutter 
5278b3d6663SArnd Bergmann static int
5288b3d6663SArnd Bergmann spufs_regs_open(struct inode *inode, struct file *file)
5298b3d6663SArnd Bergmann {
5308b3d6663SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
5318b3d6663SArnd Bergmann 	file->private_data = i->i_ctx;
5328b3d6663SArnd Bergmann 	return 0;
5338b3d6663SArnd Bergmann }
5348b3d6663SArnd Bergmann 
5358b3d6663SArnd Bergmann static ssize_t
536bf1ab978SDwayne Grant McConnell __spufs_regs_read(struct spu_context *ctx, char __user *buffer,
537bf1ab978SDwayne Grant McConnell 			size_t size, loff_t *pos)
538bf1ab978SDwayne Grant McConnell {
539bf1ab978SDwayne Grant McConnell 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
540bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buffer, size, pos,
541bf1ab978SDwayne Grant McConnell 				      lscsa->gprs, sizeof lscsa->gprs);
542bf1ab978SDwayne Grant McConnell }
543bf1ab978SDwayne Grant McConnell 
544bf1ab978SDwayne Grant McConnell static ssize_t
5458b3d6663SArnd Bergmann spufs_regs_read(struct file *file, char __user *buffer,
5468b3d6663SArnd Bergmann 		size_t size, loff_t *pos)
5478b3d6663SArnd Bergmann {
5488b3d6663SArnd Bergmann 	int ret;
549bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
5508b3d6663SArnd Bergmann 
551c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
552c9101bdbSChristoph Hellwig 	if (ret)
553c9101bdbSChristoph Hellwig 		return ret;
554bf1ab978SDwayne Grant McConnell 	ret = __spufs_regs_read(ctx, buffer, size, pos);
55527b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
5568b3d6663SArnd Bergmann 	return ret;
5578b3d6663SArnd Bergmann }
5588b3d6663SArnd Bergmann 
5598b3d6663SArnd Bergmann static ssize_t
5608b3d6663SArnd Bergmann spufs_regs_write(struct file *file, const char __user *buffer,
5618b3d6663SArnd Bergmann 		 size_t size, loff_t *pos)
5628b3d6663SArnd Bergmann {
5638b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
5648b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
5658b3d6663SArnd Bergmann 	int ret;
5668b3d6663SArnd Bergmann 
5678b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
5688b3d6663SArnd Bergmann 	if (size <= 0)
5698b3d6663SArnd Bergmann 		return -EFBIG;
5708b3d6663SArnd Bergmann 	*pos += size;
5718b3d6663SArnd Bergmann 
572c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
573c9101bdbSChristoph Hellwig 	if (ret)
574c9101bdbSChristoph Hellwig 		return ret;
5758b3d6663SArnd Bergmann 
5768b3d6663SArnd Bergmann 	ret = copy_from_user(lscsa->gprs + *pos - size,
5778b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
5788b3d6663SArnd Bergmann 
57927b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
5808b3d6663SArnd Bergmann 	return ret;
5818b3d6663SArnd Bergmann }
5828b3d6663SArnd Bergmann 
5835dfe4c96SArjan van de Ven static const struct file_operations spufs_regs_fops = {
5848b3d6663SArnd Bergmann 	.open	 = spufs_regs_open,
5858b3d6663SArnd Bergmann 	.read    = spufs_regs_read,
5868b3d6663SArnd Bergmann 	.write   = spufs_regs_write,
5878b3d6663SArnd Bergmann 	.llseek  = generic_file_llseek,
5888b3d6663SArnd Bergmann };
5898b3d6663SArnd Bergmann 
5908b3d6663SArnd Bergmann static ssize_t
591bf1ab978SDwayne Grant McConnell __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
592bf1ab978SDwayne Grant McConnell 			size_t size, loff_t * pos)
593bf1ab978SDwayne Grant McConnell {
594bf1ab978SDwayne Grant McConnell 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
595bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buffer, size, pos,
596bf1ab978SDwayne Grant McConnell 				      &lscsa->fpcr, sizeof(lscsa->fpcr));
597bf1ab978SDwayne Grant McConnell }
598bf1ab978SDwayne Grant McConnell 
599bf1ab978SDwayne Grant McConnell static ssize_t
6008b3d6663SArnd Bergmann spufs_fpcr_read(struct file *file, char __user * buffer,
6018b3d6663SArnd Bergmann 		size_t size, loff_t * pos)
6028b3d6663SArnd Bergmann {
6038b3d6663SArnd Bergmann 	int ret;
604bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
6058b3d6663SArnd Bergmann 
606c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
607c9101bdbSChristoph Hellwig 	if (ret)
608c9101bdbSChristoph Hellwig 		return ret;
609bf1ab978SDwayne Grant McConnell 	ret = __spufs_fpcr_read(ctx, buffer, size, pos);
61027b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
6118b3d6663SArnd Bergmann 	return ret;
6128b3d6663SArnd Bergmann }
6138b3d6663SArnd Bergmann 
6148b3d6663SArnd Bergmann static ssize_t
6158b3d6663SArnd Bergmann spufs_fpcr_write(struct file *file, const char __user * buffer,
6168b3d6663SArnd Bergmann 		 size_t size, loff_t * pos)
6178b3d6663SArnd Bergmann {
6188b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
6198b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
6208b3d6663SArnd Bergmann 	int ret;
6218b3d6663SArnd Bergmann 
6228b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
6238b3d6663SArnd Bergmann 	if (size <= 0)
6248b3d6663SArnd Bergmann 		return -EFBIG;
625c9101bdbSChristoph Hellwig 
626c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
627c9101bdbSChristoph Hellwig 	if (ret)
628c9101bdbSChristoph Hellwig 		return ret;
629c9101bdbSChristoph Hellwig 
6308b3d6663SArnd Bergmann 	*pos += size;
6318b3d6663SArnd Bergmann 	ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
6328b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
6338b3d6663SArnd Bergmann 
63427b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
6358b3d6663SArnd Bergmann 	return ret;
6368b3d6663SArnd Bergmann }
6378b3d6663SArnd Bergmann 
6385dfe4c96SArjan van de Ven static const struct file_operations spufs_fpcr_fops = {
6398b3d6663SArnd Bergmann 	.open = spufs_regs_open,
6408b3d6663SArnd Bergmann 	.read = spufs_fpcr_read,
6418b3d6663SArnd Bergmann 	.write = spufs_fpcr_write,
64267207b96SArnd Bergmann 	.llseek = generic_file_llseek,
64367207b96SArnd Bergmann };
64467207b96SArnd Bergmann 
64567207b96SArnd Bergmann /* generic open function for all pipe-like files */
64667207b96SArnd Bergmann static int spufs_pipe_open(struct inode *inode, struct file *file)
64767207b96SArnd Bergmann {
64867207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
64967207b96SArnd Bergmann 	file->private_data = i->i_ctx;
65067207b96SArnd Bergmann 
65167207b96SArnd Bergmann 	return nonseekable_open(inode, file);
65267207b96SArnd Bergmann }
65367207b96SArnd Bergmann 
654cdcc89bbSArnd Bergmann /*
655cdcc89bbSArnd Bergmann  * Read as many bytes from the mailbox as possible, until
656cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
657cdcc89bbSArnd Bergmann  *
658cdcc89bbSArnd Bergmann  * - no more data available in the mailbox
659cdcc89bbSArnd Bergmann  * - end of the user provided buffer
660cdcc89bbSArnd Bergmann  * - end of the mapped area
661cdcc89bbSArnd Bergmann  */
66267207b96SArnd Bergmann static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
66367207b96SArnd Bergmann 			size_t len, loff_t *pos)
66467207b96SArnd Bergmann {
6658b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
666cdcc89bbSArnd Bergmann 	u32 mbox_data, __user *udata;
667cdcc89bbSArnd Bergmann 	ssize_t count;
66867207b96SArnd Bergmann 
66967207b96SArnd Bergmann 	if (len < 4)
67067207b96SArnd Bergmann 		return -EINVAL;
67167207b96SArnd Bergmann 
672cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_WRITE, buf, len))
67367207b96SArnd Bergmann 		return -EFAULT;
67467207b96SArnd Bergmann 
675cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
676cdcc89bbSArnd Bergmann 
677c9101bdbSChristoph Hellwig 	count = spu_acquire(ctx);
678c9101bdbSChristoph Hellwig 	if (count)
679c9101bdbSChristoph Hellwig 		return count;
680c9101bdbSChristoph Hellwig 
681274cef5eSArnd Bergmann 	for (count = 0; (count + 4) <= len; count += 4, udata++) {
682cdcc89bbSArnd Bergmann 		int ret;
683cdcc89bbSArnd Bergmann 		ret = ctx->ops->mbox_read(ctx, &mbox_data);
684cdcc89bbSArnd Bergmann 		if (ret == 0)
685cdcc89bbSArnd Bergmann 			break;
686cdcc89bbSArnd Bergmann 
687cdcc89bbSArnd Bergmann 		/*
688cdcc89bbSArnd Bergmann 		 * at the end of the mapped area, we can fault
689cdcc89bbSArnd Bergmann 		 * but still need to return the data we have
690cdcc89bbSArnd Bergmann 		 * read successfully so far.
691cdcc89bbSArnd Bergmann 		 */
692cdcc89bbSArnd Bergmann 		ret = __put_user(mbox_data, udata);
693cdcc89bbSArnd Bergmann 		if (ret) {
694cdcc89bbSArnd Bergmann 			if (!count)
695cdcc89bbSArnd Bergmann 				count = -EFAULT;
696cdcc89bbSArnd Bergmann 			break;
697cdcc89bbSArnd Bergmann 		}
698cdcc89bbSArnd Bergmann 	}
699cdcc89bbSArnd Bergmann 	spu_release(ctx);
700cdcc89bbSArnd Bergmann 
701cdcc89bbSArnd Bergmann 	if (!count)
702cdcc89bbSArnd Bergmann 		count = -EAGAIN;
703cdcc89bbSArnd Bergmann 
704cdcc89bbSArnd Bergmann 	return count;
70567207b96SArnd Bergmann }
70667207b96SArnd Bergmann 
7075dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_fops = {
70867207b96SArnd Bergmann 	.open	= spufs_pipe_open,
70967207b96SArnd Bergmann 	.read	= spufs_mbox_read,
71067207b96SArnd Bergmann };
71167207b96SArnd Bergmann 
71267207b96SArnd Bergmann static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
71367207b96SArnd Bergmann 			size_t len, loff_t *pos)
71467207b96SArnd Bergmann {
7158b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
716c9101bdbSChristoph Hellwig 	ssize_t ret;
71767207b96SArnd Bergmann 	u32 mbox_stat;
71867207b96SArnd Bergmann 
71967207b96SArnd Bergmann 	if (len < 4)
72067207b96SArnd Bergmann 		return -EINVAL;
72167207b96SArnd Bergmann 
722c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
723c9101bdbSChristoph Hellwig 	if (ret)
724c9101bdbSChristoph Hellwig 		return ret;
7258b3d6663SArnd Bergmann 
7268b3d6663SArnd Bergmann 	mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
7278b3d6663SArnd Bergmann 
7288b3d6663SArnd Bergmann 	spu_release(ctx);
72967207b96SArnd Bergmann 
73067207b96SArnd Bergmann 	if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
73167207b96SArnd Bergmann 		return -EFAULT;
73267207b96SArnd Bergmann 
73367207b96SArnd Bergmann 	return 4;
73467207b96SArnd Bergmann }
73567207b96SArnd Bergmann 
7365dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_stat_fops = {
73767207b96SArnd Bergmann 	.open	= spufs_pipe_open,
73867207b96SArnd Bergmann 	.read	= spufs_mbox_stat_read,
73967207b96SArnd Bergmann };
74067207b96SArnd Bergmann 
74167207b96SArnd Bergmann /* low-level ibox access function */
7428b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
74367207b96SArnd Bergmann {
7448b3d6663SArnd Bergmann 	return ctx->ops->ibox_read(ctx, data);
74567207b96SArnd Bergmann }
74667207b96SArnd Bergmann 
74767207b96SArnd Bergmann static int spufs_ibox_fasync(int fd, struct file *file, int on)
74867207b96SArnd Bergmann {
7498b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
7508b3d6663SArnd Bergmann 
7518b3d6663SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->ibox_fasync);
7528b3d6663SArnd Bergmann }
7538b3d6663SArnd Bergmann 
7548b3d6663SArnd Bergmann /* interrupt-level ibox callback function. */
7558b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu)
7568b3d6663SArnd Bergmann {
7578b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
7588b3d6663SArnd Bergmann 
759e65c2f6fSLuke Browning 	if (!ctx)
760e65c2f6fSLuke Browning 		return;
761e65c2f6fSLuke Browning 
7628b3d6663SArnd Bergmann 	wake_up_all(&ctx->ibox_wq);
7638b3d6663SArnd Bergmann 	kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
76467207b96SArnd Bergmann }
76567207b96SArnd Bergmann 
766cdcc89bbSArnd Bergmann /*
767cdcc89bbSArnd Bergmann  * Read as many bytes from the interrupt mailbox as possible, until
768cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
769cdcc89bbSArnd Bergmann  *
770cdcc89bbSArnd Bergmann  * - no more data available in the mailbox
771cdcc89bbSArnd Bergmann  * - end of the user provided buffer
772cdcc89bbSArnd Bergmann  * - end of the mapped area
773cdcc89bbSArnd Bergmann  *
774cdcc89bbSArnd Bergmann  * If the file is opened without O_NONBLOCK, we wait here until
775cdcc89bbSArnd Bergmann  * any data is available, but return when we have been able to
776cdcc89bbSArnd Bergmann  * read something.
777cdcc89bbSArnd Bergmann  */
77867207b96SArnd Bergmann static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
77967207b96SArnd Bergmann 			size_t len, loff_t *pos)
78067207b96SArnd Bergmann {
7818b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
782cdcc89bbSArnd Bergmann 	u32 ibox_data, __user *udata;
783cdcc89bbSArnd Bergmann 	ssize_t count;
78467207b96SArnd Bergmann 
78567207b96SArnd Bergmann 	if (len < 4)
78667207b96SArnd Bergmann 		return -EINVAL;
78767207b96SArnd Bergmann 
788cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_WRITE, buf, len))
789cdcc89bbSArnd Bergmann 		return -EFAULT;
790cdcc89bbSArnd Bergmann 
791cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
792cdcc89bbSArnd Bergmann 
793c9101bdbSChristoph Hellwig 	count = spu_acquire(ctx);
794c9101bdbSChristoph Hellwig 	if (count)
795eebead5bSChristoph Hellwig 		goto out;
79667207b96SArnd Bergmann 
797cdcc89bbSArnd Bergmann 	/* wait only for the first element */
798cdcc89bbSArnd Bergmann 	count = 0;
79967207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
800eebead5bSChristoph Hellwig 		if (!spu_ibox_read(ctx, &ibox_data)) {
801cdcc89bbSArnd Bergmann 			count = -EAGAIN;
802eebead5bSChristoph Hellwig 			goto out_unlock;
803eebead5bSChristoph Hellwig 		}
80467207b96SArnd Bergmann 	} else {
805cdcc89bbSArnd Bergmann 		count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
806cdcc89bbSArnd Bergmann 		if (count)
807cdcc89bbSArnd Bergmann 			goto out;
808eebead5bSChristoph Hellwig 	}
809cdcc89bbSArnd Bergmann 
810cdcc89bbSArnd Bergmann 	/* if we can't write at all, return -EFAULT */
811cdcc89bbSArnd Bergmann 	count = __put_user(ibox_data, udata);
812cdcc89bbSArnd Bergmann 	if (count)
813eebead5bSChristoph Hellwig 		goto out_unlock;
814cdcc89bbSArnd Bergmann 
815cdcc89bbSArnd Bergmann 	for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
816cdcc89bbSArnd Bergmann 		int ret;
817cdcc89bbSArnd Bergmann 		ret = ctx->ops->ibox_read(ctx, &ibox_data);
818cdcc89bbSArnd Bergmann 		if (ret == 0)
819cdcc89bbSArnd Bergmann 			break;
820cdcc89bbSArnd Bergmann 		/*
821cdcc89bbSArnd Bergmann 		 * at the end of the mapped area, we can fault
822cdcc89bbSArnd Bergmann 		 * but still need to return the data we have
823cdcc89bbSArnd Bergmann 		 * read successfully so far.
824cdcc89bbSArnd Bergmann 		 */
825cdcc89bbSArnd Bergmann 		ret = __put_user(ibox_data, udata);
826cdcc89bbSArnd Bergmann 		if (ret)
827cdcc89bbSArnd Bergmann 			break;
82867207b96SArnd Bergmann 	}
82967207b96SArnd Bergmann 
830eebead5bSChristoph Hellwig out_unlock:
8318b3d6663SArnd Bergmann 	spu_release(ctx);
832eebead5bSChristoph Hellwig out:
833cdcc89bbSArnd Bergmann 	return count;
83467207b96SArnd Bergmann }
83567207b96SArnd Bergmann 
83667207b96SArnd Bergmann static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
83767207b96SArnd Bergmann {
8388b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
83967207b96SArnd Bergmann 	unsigned int mask;
84067207b96SArnd Bergmann 
8418b3d6663SArnd Bergmann 	poll_wait(file, &ctx->ibox_wq, wait);
84267207b96SArnd Bergmann 
843c9101bdbSChristoph Hellwig 	/*
844c9101bdbSChristoph Hellwig 	 * For now keep this uninterruptible and also ignore the rule
845c9101bdbSChristoph Hellwig 	 * that poll should not sleep.  Will be fixed later.
846c9101bdbSChristoph Hellwig 	 */
847c9101bdbSChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
8483a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
8493a843d7cSArnd Bergmann 	spu_release(ctx);
85067207b96SArnd Bergmann 
85167207b96SArnd Bergmann 	return mask;
85267207b96SArnd Bergmann }
85367207b96SArnd Bergmann 
8545dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_fops = {
85567207b96SArnd Bergmann 	.open	= spufs_pipe_open,
85667207b96SArnd Bergmann 	.read	= spufs_ibox_read,
85767207b96SArnd Bergmann 	.poll	= spufs_ibox_poll,
85867207b96SArnd Bergmann 	.fasync	= spufs_ibox_fasync,
85967207b96SArnd Bergmann };
86067207b96SArnd Bergmann 
86167207b96SArnd Bergmann static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
86267207b96SArnd Bergmann 			size_t len, loff_t *pos)
86367207b96SArnd Bergmann {
8648b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
865c9101bdbSChristoph Hellwig 	ssize_t ret;
86667207b96SArnd Bergmann 	u32 ibox_stat;
86767207b96SArnd Bergmann 
86867207b96SArnd Bergmann 	if (len < 4)
86967207b96SArnd Bergmann 		return -EINVAL;
87067207b96SArnd Bergmann 
871c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
872c9101bdbSChristoph Hellwig 	if (ret)
873c9101bdbSChristoph Hellwig 		return ret;
8748b3d6663SArnd Bergmann 	ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
8758b3d6663SArnd Bergmann 	spu_release(ctx);
87667207b96SArnd Bergmann 
87767207b96SArnd Bergmann 	if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
87867207b96SArnd Bergmann 		return -EFAULT;
87967207b96SArnd Bergmann 
88067207b96SArnd Bergmann 	return 4;
88167207b96SArnd Bergmann }
88267207b96SArnd Bergmann 
8835dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_stat_fops = {
88467207b96SArnd Bergmann 	.open	= spufs_pipe_open,
88567207b96SArnd Bergmann 	.read	= spufs_ibox_stat_read,
88667207b96SArnd Bergmann };
88767207b96SArnd Bergmann 
88867207b96SArnd Bergmann /* low-level mailbox write */
8898b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data)
89067207b96SArnd Bergmann {
8918b3d6663SArnd Bergmann 	return ctx->ops->wbox_write(ctx, data);
89267207b96SArnd Bergmann }
89367207b96SArnd Bergmann 
89467207b96SArnd Bergmann static int spufs_wbox_fasync(int fd, struct file *file, int on)
89567207b96SArnd Bergmann {
8968b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
8978b3d6663SArnd Bergmann 	int ret;
8988b3d6663SArnd Bergmann 
8998b3d6663SArnd Bergmann 	ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
9008b3d6663SArnd Bergmann 
9018b3d6663SArnd Bergmann 	return ret;
9028b3d6663SArnd Bergmann }
9038b3d6663SArnd Bergmann 
9048b3d6663SArnd Bergmann /* interrupt-level wbox callback function. */
9058b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu)
9068b3d6663SArnd Bergmann {
9078b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
9088b3d6663SArnd Bergmann 
909e65c2f6fSLuke Browning 	if (!ctx)
910e65c2f6fSLuke Browning 		return;
911e65c2f6fSLuke Browning 
9128b3d6663SArnd Bergmann 	wake_up_all(&ctx->wbox_wq);
9138b3d6663SArnd Bergmann 	kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
91467207b96SArnd Bergmann }
91567207b96SArnd Bergmann 
916cdcc89bbSArnd Bergmann /*
917cdcc89bbSArnd Bergmann  * Write as many bytes to the interrupt mailbox as possible, until
918cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
919cdcc89bbSArnd Bergmann  *
920cdcc89bbSArnd Bergmann  * - the mailbox is full
921cdcc89bbSArnd Bergmann  * - end of the user provided buffer
922cdcc89bbSArnd Bergmann  * - end of the mapped area
923cdcc89bbSArnd Bergmann  *
924cdcc89bbSArnd Bergmann  * If the file is opened without O_NONBLOCK, we wait here until
925cdcc89bbSArnd Bergmann  * space is availabyl, but return when we have been able to
926cdcc89bbSArnd Bergmann  * write something.
927cdcc89bbSArnd Bergmann  */
92867207b96SArnd Bergmann static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
92967207b96SArnd Bergmann 			size_t len, loff_t *pos)
93067207b96SArnd Bergmann {
9318b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
932cdcc89bbSArnd Bergmann 	u32 wbox_data, __user *udata;
933cdcc89bbSArnd Bergmann 	ssize_t count;
93467207b96SArnd Bergmann 
93567207b96SArnd Bergmann 	if (len < 4)
93667207b96SArnd Bergmann 		return -EINVAL;
93767207b96SArnd Bergmann 
938cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
939cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_READ, buf, len))
940cdcc89bbSArnd Bergmann 		return -EFAULT;
941cdcc89bbSArnd Bergmann 
942cdcc89bbSArnd Bergmann 	if (__get_user(wbox_data, udata))
94367207b96SArnd Bergmann 		return -EFAULT;
94467207b96SArnd Bergmann 
945c9101bdbSChristoph Hellwig 	count = spu_acquire(ctx);
946c9101bdbSChristoph Hellwig 	if (count)
947eebead5bSChristoph Hellwig 		goto out;
9488b3d6663SArnd Bergmann 
949cdcc89bbSArnd Bergmann 	/*
950cdcc89bbSArnd Bergmann 	 * make sure we can at least write one element, by waiting
951cdcc89bbSArnd Bergmann 	 * in case of !O_NONBLOCK
952cdcc89bbSArnd Bergmann 	 */
953cdcc89bbSArnd Bergmann 	count = 0;
95467207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
955eebead5bSChristoph Hellwig 		if (!spu_wbox_write(ctx, wbox_data)) {
956cdcc89bbSArnd Bergmann 			count = -EAGAIN;
957eebead5bSChristoph Hellwig 			goto out_unlock;
958eebead5bSChristoph Hellwig 		}
95967207b96SArnd Bergmann 	} else {
960cdcc89bbSArnd Bergmann 		count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
961cdcc89bbSArnd Bergmann 		if (count)
962cdcc89bbSArnd Bergmann 			goto out;
963eebead5bSChristoph Hellwig 	}
964eebead5bSChristoph Hellwig 
9658b3d6663SArnd Bergmann 
96696de0e25SJan Engelhardt 	/* write as much as possible */
967cdcc89bbSArnd Bergmann 	for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
968cdcc89bbSArnd Bergmann 		int ret;
969cdcc89bbSArnd Bergmann 		ret = __get_user(wbox_data, udata);
970cdcc89bbSArnd Bergmann 		if (ret)
971cdcc89bbSArnd Bergmann 			break;
972cdcc89bbSArnd Bergmann 
973cdcc89bbSArnd Bergmann 		ret = spu_wbox_write(ctx, wbox_data);
974cdcc89bbSArnd Bergmann 		if (ret == 0)
975cdcc89bbSArnd Bergmann 			break;
976cdcc89bbSArnd Bergmann 	}
977cdcc89bbSArnd Bergmann 
978eebead5bSChristoph Hellwig out_unlock:
979cdcc89bbSArnd Bergmann 	spu_release(ctx);
980eebead5bSChristoph Hellwig out:
981cdcc89bbSArnd Bergmann 	return count;
98267207b96SArnd Bergmann }
98367207b96SArnd Bergmann 
98467207b96SArnd Bergmann static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
98567207b96SArnd Bergmann {
9868b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
98767207b96SArnd Bergmann 	unsigned int mask;
98867207b96SArnd Bergmann 
9898b3d6663SArnd Bergmann 	poll_wait(file, &ctx->wbox_wq, wait);
99067207b96SArnd Bergmann 
991c9101bdbSChristoph Hellwig 	/*
992c9101bdbSChristoph Hellwig 	 * For now keep this uninterruptible and also ignore the rule
993c9101bdbSChristoph Hellwig 	 * that poll should not sleep.  Will be fixed later.
994c9101bdbSChristoph Hellwig 	 */
995c9101bdbSChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
9963a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
9973a843d7cSArnd Bergmann 	spu_release(ctx);
99867207b96SArnd Bergmann 
99967207b96SArnd Bergmann 	return mask;
100067207b96SArnd Bergmann }
100167207b96SArnd Bergmann 
10025dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_fops = {
100367207b96SArnd Bergmann 	.open	= spufs_pipe_open,
100467207b96SArnd Bergmann 	.write	= spufs_wbox_write,
100567207b96SArnd Bergmann 	.poll	= spufs_wbox_poll,
100667207b96SArnd Bergmann 	.fasync	= spufs_wbox_fasync,
100767207b96SArnd Bergmann };
100867207b96SArnd Bergmann 
100967207b96SArnd Bergmann static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
101067207b96SArnd Bergmann 			size_t len, loff_t *pos)
101167207b96SArnd Bergmann {
10128b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1013c9101bdbSChristoph Hellwig 	ssize_t ret;
101467207b96SArnd Bergmann 	u32 wbox_stat;
101567207b96SArnd Bergmann 
101667207b96SArnd Bergmann 	if (len < 4)
101767207b96SArnd Bergmann 		return -EINVAL;
101867207b96SArnd Bergmann 
1019c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1020c9101bdbSChristoph Hellwig 	if (ret)
1021c9101bdbSChristoph Hellwig 		return ret;
10228b3d6663SArnd Bergmann 	wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
10238b3d6663SArnd Bergmann 	spu_release(ctx);
102467207b96SArnd Bergmann 
102567207b96SArnd Bergmann 	if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
102667207b96SArnd Bergmann 		return -EFAULT;
102767207b96SArnd Bergmann 
102867207b96SArnd Bergmann 	return 4;
102967207b96SArnd Bergmann }
103067207b96SArnd Bergmann 
10315dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_stat_fops = {
103267207b96SArnd Bergmann 	.open	= spufs_pipe_open,
103367207b96SArnd Bergmann 	.read	= spufs_wbox_stat_read,
103467207b96SArnd Bergmann };
103567207b96SArnd Bergmann 
10366df10a82SMark Nutter static int spufs_signal1_open(struct inode *inode, struct file *file)
10376df10a82SMark Nutter {
10386df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
10396df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
104043c2bbd9SChristoph Hellwig 
104147d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
10426df10a82SMark Nutter 	file->private_data = ctx;
104343c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
10446df10a82SMark Nutter 		ctx->signal1 = inode->i_mapping;
104547d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
10466df10a82SMark Nutter 	return nonseekable_open(inode, file);
10476df10a82SMark Nutter }
10486df10a82SMark Nutter 
104943c2bbd9SChristoph Hellwig static int
105043c2bbd9SChristoph Hellwig spufs_signal1_release(struct inode *inode, struct file *file)
105143c2bbd9SChristoph Hellwig {
105243c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
105343c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
105443c2bbd9SChristoph Hellwig 
105547d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
105643c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
105743c2bbd9SChristoph Hellwig 		ctx->signal1 = NULL;
105847d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
105943c2bbd9SChristoph Hellwig 	return 0;
106043c2bbd9SChristoph Hellwig }
106143c2bbd9SChristoph Hellwig 
1062bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
106367207b96SArnd Bergmann 			size_t len, loff_t *pos)
106467207b96SArnd Bergmann {
106517f88cebSDwayne Grant McConnell 	int ret = 0;
106667207b96SArnd Bergmann 	u32 data;
106767207b96SArnd Bergmann 
106867207b96SArnd Bergmann 	if (len < 4)
106967207b96SArnd Bergmann 		return -EINVAL;
107067207b96SArnd Bergmann 
107117f88cebSDwayne Grant McConnell 	if (ctx->csa.spu_chnlcnt_RW[3]) {
107217f88cebSDwayne Grant McConnell 		data = ctx->csa.spu_chnldata_RW[3];
107317f88cebSDwayne Grant McConnell 		ret = 4;
107417f88cebSDwayne Grant McConnell 	}
10758b3d6663SArnd Bergmann 
107617f88cebSDwayne Grant McConnell 	if (!ret)
107717f88cebSDwayne Grant McConnell 		goto out;
107817f88cebSDwayne Grant McConnell 
107967207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
108067207b96SArnd Bergmann 		return -EFAULT;
108167207b96SArnd Bergmann 
108217f88cebSDwayne Grant McConnell out:
108317f88cebSDwayne Grant McConnell 	return ret;
108467207b96SArnd Bergmann }
108567207b96SArnd Bergmann 
1086bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1087bf1ab978SDwayne Grant McConnell 			size_t len, loff_t *pos)
1088bf1ab978SDwayne Grant McConnell {
1089bf1ab978SDwayne Grant McConnell 	int ret;
1090bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1091bf1ab978SDwayne Grant McConnell 
1092c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1093c9101bdbSChristoph Hellwig 	if (ret)
1094c9101bdbSChristoph Hellwig 		return ret;
1095bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal1_read(ctx, buf, len, pos);
109627b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1097bf1ab978SDwayne Grant McConnell 
1098bf1ab978SDwayne Grant McConnell 	return ret;
1099bf1ab978SDwayne Grant McConnell }
1100bf1ab978SDwayne Grant McConnell 
110167207b96SArnd Bergmann static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
110267207b96SArnd Bergmann 			size_t len, loff_t *pos)
110367207b96SArnd Bergmann {
110467207b96SArnd Bergmann 	struct spu_context *ctx;
1105c9101bdbSChristoph Hellwig 	ssize_t ret;
110667207b96SArnd Bergmann 	u32 data;
110767207b96SArnd Bergmann 
110867207b96SArnd Bergmann 	ctx = file->private_data;
110967207b96SArnd Bergmann 
111067207b96SArnd Bergmann 	if (len < 4)
111167207b96SArnd Bergmann 		return -EINVAL;
111267207b96SArnd Bergmann 
111367207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
111467207b96SArnd Bergmann 		return -EFAULT;
111567207b96SArnd Bergmann 
1116c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1117c9101bdbSChristoph Hellwig 	if (ret)
1118c9101bdbSChristoph Hellwig 		return ret;
11198b3d6663SArnd Bergmann 	ctx->ops->signal1_write(ctx, data);
11208b3d6663SArnd Bergmann 	spu_release(ctx);
112167207b96SArnd Bergmann 
112267207b96SArnd Bergmann 	return 4;
112367207b96SArnd Bergmann }
112467207b96SArnd Bergmann 
1125b1e2270fSNick Piggin static int
1126b1e2270fSNick Piggin spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
11276df10a82SMark Nutter {
112887ff6090SJeremy Kerr #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
112987ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
113087ff6090SJeremy Kerr #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
113127d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
113227d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
113327d5bf2aSBenjamin Herrenschmidt 	 */
113487ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
113527d5bf2aSBenjamin Herrenschmidt #else
113627d5bf2aSBenjamin Herrenschmidt #error unsupported page size
113727d5bf2aSBenjamin Herrenschmidt #endif
11386df10a82SMark Nutter }
11396df10a82SMark Nutter 
11406df10a82SMark Nutter static struct vm_operations_struct spufs_signal1_mmap_vmops = {
1141b1e2270fSNick Piggin 	.fault = spufs_signal1_mmap_fault,
11426df10a82SMark Nutter };
11436df10a82SMark Nutter 
11446df10a82SMark Nutter static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
11456df10a82SMark Nutter {
11466df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
11476df10a82SMark Nutter 		return -EINVAL;
11486df10a82SMark Nutter 
114978bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
11506df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
115123cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
11526df10a82SMark Nutter 
11536df10a82SMark Nutter 	vma->vm_ops = &spufs_signal1_mmap_vmops;
11546df10a82SMark Nutter 	return 0;
11556df10a82SMark Nutter }
11566df10a82SMark Nutter 
11575dfe4c96SArjan van de Ven static const struct file_operations spufs_signal1_fops = {
11586df10a82SMark Nutter 	.open = spufs_signal1_open,
115943c2bbd9SChristoph Hellwig 	.release = spufs_signal1_release,
116067207b96SArnd Bergmann 	.read = spufs_signal1_read,
116167207b96SArnd Bergmann 	.write = spufs_signal1_write,
11626df10a82SMark Nutter 	.mmap = spufs_signal1_mmap,
116367207b96SArnd Bergmann };
116467207b96SArnd Bergmann 
1165d054b36fSJeremy Kerr static const struct file_operations spufs_signal1_nosched_fops = {
1166d054b36fSJeremy Kerr 	.open = spufs_signal1_open,
1167d054b36fSJeremy Kerr 	.release = spufs_signal1_release,
1168d054b36fSJeremy Kerr 	.write = spufs_signal1_write,
1169d054b36fSJeremy Kerr 	.mmap = spufs_signal1_mmap,
1170d054b36fSJeremy Kerr };
1171d054b36fSJeremy Kerr 
11726df10a82SMark Nutter static int spufs_signal2_open(struct inode *inode, struct file *file)
11736df10a82SMark Nutter {
11746df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
11756df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
117643c2bbd9SChristoph Hellwig 
117747d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
11786df10a82SMark Nutter 	file->private_data = ctx;
117943c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
11806df10a82SMark Nutter 		ctx->signal2 = inode->i_mapping;
118147d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
11826df10a82SMark Nutter 	return nonseekable_open(inode, file);
11836df10a82SMark Nutter }
11846df10a82SMark Nutter 
118543c2bbd9SChristoph Hellwig static int
118643c2bbd9SChristoph Hellwig spufs_signal2_release(struct inode *inode, struct file *file)
118743c2bbd9SChristoph Hellwig {
118843c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
118943c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
119043c2bbd9SChristoph Hellwig 
119147d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
119243c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
119343c2bbd9SChristoph Hellwig 		ctx->signal2 = NULL;
119447d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
119543c2bbd9SChristoph Hellwig 	return 0;
119643c2bbd9SChristoph Hellwig }
119743c2bbd9SChristoph Hellwig 
1198bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
119967207b96SArnd Bergmann 			size_t len, loff_t *pos)
120067207b96SArnd Bergmann {
120117f88cebSDwayne Grant McConnell 	int ret = 0;
120267207b96SArnd Bergmann 	u32 data;
120367207b96SArnd Bergmann 
120467207b96SArnd Bergmann 	if (len < 4)
120567207b96SArnd Bergmann 		return -EINVAL;
120667207b96SArnd Bergmann 
120717f88cebSDwayne Grant McConnell 	if (ctx->csa.spu_chnlcnt_RW[4]) {
120817f88cebSDwayne Grant McConnell 		data =  ctx->csa.spu_chnldata_RW[4];
120917f88cebSDwayne Grant McConnell 		ret = 4;
121017f88cebSDwayne Grant McConnell 	}
12118b3d6663SArnd Bergmann 
121217f88cebSDwayne Grant McConnell 	if (!ret)
121317f88cebSDwayne Grant McConnell 		goto out;
121417f88cebSDwayne Grant McConnell 
121567207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
121667207b96SArnd Bergmann 		return -EFAULT;
121767207b96SArnd Bergmann 
121817f88cebSDwayne Grant McConnell out:
1219bf1ab978SDwayne Grant McConnell 	return ret;
1220bf1ab978SDwayne Grant McConnell }
1221bf1ab978SDwayne Grant McConnell 
1222bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1223bf1ab978SDwayne Grant McConnell 			size_t len, loff_t *pos)
1224bf1ab978SDwayne Grant McConnell {
1225bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1226bf1ab978SDwayne Grant McConnell 	int ret;
1227bf1ab978SDwayne Grant McConnell 
1228c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1229c9101bdbSChristoph Hellwig 	if (ret)
1230c9101bdbSChristoph Hellwig 		return ret;
1231bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal2_read(ctx, buf, len, pos);
123227b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1233bf1ab978SDwayne Grant McConnell 
1234bf1ab978SDwayne Grant McConnell 	return ret;
123567207b96SArnd Bergmann }
123667207b96SArnd Bergmann 
123767207b96SArnd Bergmann static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
123867207b96SArnd Bergmann 			size_t len, loff_t *pos)
123967207b96SArnd Bergmann {
124067207b96SArnd Bergmann 	struct spu_context *ctx;
1241c9101bdbSChristoph Hellwig 	ssize_t ret;
124267207b96SArnd Bergmann 	u32 data;
124367207b96SArnd Bergmann 
124467207b96SArnd Bergmann 	ctx = file->private_data;
124567207b96SArnd Bergmann 
124667207b96SArnd Bergmann 	if (len < 4)
124767207b96SArnd Bergmann 		return -EINVAL;
124867207b96SArnd Bergmann 
124967207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
125067207b96SArnd Bergmann 		return -EFAULT;
125167207b96SArnd Bergmann 
1252c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1253c9101bdbSChristoph Hellwig 	if (ret)
1254c9101bdbSChristoph Hellwig 		return ret;
12558b3d6663SArnd Bergmann 	ctx->ops->signal2_write(ctx, data);
12568b3d6663SArnd Bergmann 	spu_release(ctx);
125767207b96SArnd Bergmann 
125867207b96SArnd Bergmann 	return 4;
125967207b96SArnd Bergmann }
126067207b96SArnd Bergmann 
126127d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
1262b1e2270fSNick Piggin static int
1263b1e2270fSNick Piggin spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
12646df10a82SMark Nutter {
126587ff6090SJeremy Kerr #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
126687ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
126787ff6090SJeremy Kerr #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
126827d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
126927d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
127027d5bf2aSBenjamin Herrenschmidt 	 */
127187ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
127227d5bf2aSBenjamin Herrenschmidt #else
127327d5bf2aSBenjamin Herrenschmidt #error unsupported page size
127427d5bf2aSBenjamin Herrenschmidt #endif
12756df10a82SMark Nutter }
12766df10a82SMark Nutter 
12776df10a82SMark Nutter static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1278b1e2270fSNick Piggin 	.fault = spufs_signal2_mmap_fault,
12796df10a82SMark Nutter };
12806df10a82SMark Nutter 
12816df10a82SMark Nutter static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
12826df10a82SMark Nutter {
12836df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
12846df10a82SMark Nutter 		return -EINVAL;
12856df10a82SMark Nutter 
128678bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
12876df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
128823cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
12896df10a82SMark Nutter 
12906df10a82SMark Nutter 	vma->vm_ops = &spufs_signal2_mmap_vmops;
12916df10a82SMark Nutter 	return 0;
12926df10a82SMark Nutter }
129327d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
129427d5bf2aSBenjamin Herrenschmidt #define spufs_signal2_mmap NULL
129527d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
12966df10a82SMark Nutter 
12975dfe4c96SArjan van de Ven static const struct file_operations spufs_signal2_fops = {
12986df10a82SMark Nutter 	.open = spufs_signal2_open,
129943c2bbd9SChristoph Hellwig 	.release = spufs_signal2_release,
130067207b96SArnd Bergmann 	.read = spufs_signal2_read,
130167207b96SArnd Bergmann 	.write = spufs_signal2_write,
13026df10a82SMark Nutter 	.mmap = spufs_signal2_mmap,
130367207b96SArnd Bergmann };
130467207b96SArnd Bergmann 
1305d054b36fSJeremy Kerr static const struct file_operations spufs_signal2_nosched_fops = {
1306d054b36fSJeremy Kerr 	.open = spufs_signal2_open,
1307d054b36fSJeremy Kerr 	.release = spufs_signal2_release,
1308d054b36fSJeremy Kerr 	.write = spufs_signal2_write,
1309d054b36fSJeremy Kerr 	.mmap = spufs_signal2_mmap,
1310d054b36fSJeremy Kerr };
1311d054b36fSJeremy Kerr 
1312104f0cc2SMichael Ellerman /*
1313104f0cc2SMichael Ellerman  * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1314104f0cc2SMichael Ellerman  * work of acquiring (or not) the SPU context before calling through
1315104f0cc2SMichael Ellerman  * to the actual get routine. The set routine is called directly.
1316104f0cc2SMichael Ellerman  */
1317104f0cc2SMichael Ellerman #define SPU_ATTR_NOACQUIRE	0
1318104f0cc2SMichael Ellerman #define SPU_ATTR_ACQUIRE	1
1319104f0cc2SMichael Ellerman #define SPU_ATTR_ACQUIRE_SAVED	2
1320104f0cc2SMichael Ellerman 
1321104f0cc2SMichael Ellerman #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire)	\
1322197b1a82SChristoph Hellwig static int __##__get(void *data, u64 *val)				\
1323104f0cc2SMichael Ellerman {									\
1324104f0cc2SMichael Ellerman 	struct spu_context *ctx = data;					\
1325c9101bdbSChristoph Hellwig 	int ret = 0;							\
1326104f0cc2SMichael Ellerman 									\
1327104f0cc2SMichael Ellerman 	if (__acquire == SPU_ATTR_ACQUIRE) {				\
1328c9101bdbSChristoph Hellwig 		ret = spu_acquire(ctx);					\
1329c9101bdbSChristoph Hellwig 		if (ret)						\
1330c9101bdbSChristoph Hellwig 			return ret;					\
1331197b1a82SChristoph Hellwig 		*val = __get(ctx);					\
1332104f0cc2SMichael Ellerman 		spu_release(ctx);					\
1333104f0cc2SMichael Ellerman 	} else if (__acquire == SPU_ATTR_ACQUIRE_SAVED)	{		\
1334c9101bdbSChristoph Hellwig 		ret = spu_acquire_saved(ctx);				\
1335c9101bdbSChristoph Hellwig 		if (ret)						\
1336c9101bdbSChristoph Hellwig 			return ret;					\
1337197b1a82SChristoph Hellwig 		*val = __get(ctx);					\
1338104f0cc2SMichael Ellerman 		spu_release_saved(ctx);					\
1339104f0cc2SMichael Ellerman 	} else								\
1340197b1a82SChristoph Hellwig 		*val = __get(ctx);					\
1341104f0cc2SMichael Ellerman 									\
1342197b1a82SChristoph Hellwig 	return 0;							\
1343104f0cc2SMichael Ellerman }									\
1344197b1a82SChristoph Hellwig DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1345104f0cc2SMichael Ellerman 
1346197b1a82SChristoph Hellwig static int spufs_signal1_type_set(void *data, u64 val)
134767207b96SArnd Bergmann {
134867207b96SArnd Bergmann 	struct spu_context *ctx = data;
1349c9101bdbSChristoph Hellwig 	int ret;
135067207b96SArnd Bergmann 
1351c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1352c9101bdbSChristoph Hellwig 	if (ret)
1353c9101bdbSChristoph Hellwig 		return ret;
13548b3d6663SArnd Bergmann 	ctx->ops->signal1_type_set(ctx, val);
13558b3d6663SArnd Bergmann 	spu_release(ctx);
1356197b1a82SChristoph Hellwig 
1357197b1a82SChristoph Hellwig 	return 0;
135867207b96SArnd Bergmann }
135967207b96SArnd Bergmann 
1360104f0cc2SMichael Ellerman static u64 spufs_signal1_type_get(struct spu_context *ctx)
1361bf1ab978SDwayne Grant McConnell {
1362bf1ab978SDwayne Grant McConnell 	return ctx->ops->signal1_type_get(ctx);
1363bf1ab978SDwayne Grant McConnell }
1364104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1365af8b44e0SJeremy Kerr 		       spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1366bf1ab978SDwayne Grant McConnell 
136767207b96SArnd Bergmann 
1368197b1a82SChristoph Hellwig static int spufs_signal2_type_set(void *data, u64 val)
136967207b96SArnd Bergmann {
137067207b96SArnd Bergmann 	struct spu_context *ctx = data;
1371c9101bdbSChristoph Hellwig 	int ret;
137267207b96SArnd Bergmann 
1373c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1374c9101bdbSChristoph Hellwig 	if (ret)
1375c9101bdbSChristoph Hellwig 		return ret;
13768b3d6663SArnd Bergmann 	ctx->ops->signal2_type_set(ctx, val);
13778b3d6663SArnd Bergmann 	spu_release(ctx);
1378197b1a82SChristoph Hellwig 
1379197b1a82SChristoph Hellwig 	return 0;
138067207b96SArnd Bergmann }
138167207b96SArnd Bergmann 
1382104f0cc2SMichael Ellerman static u64 spufs_signal2_type_get(struct spu_context *ctx)
1383bf1ab978SDwayne Grant McConnell {
1384bf1ab978SDwayne Grant McConnell 	return ctx->ops->signal2_type_get(ctx);
1385bf1ab978SDwayne Grant McConnell }
1386104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1387af8b44e0SJeremy Kerr 		       spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
138867207b96SArnd Bergmann 
138927d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
1390b1e2270fSNick Piggin static int
1391b1e2270fSNick Piggin spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1392d9379c4bSarnd@arndb.de {
139387ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
1394d9379c4bSarnd@arndb.de }
1395d9379c4bSarnd@arndb.de 
1396d9379c4bSarnd@arndb.de static struct vm_operations_struct spufs_mss_mmap_vmops = {
1397b1e2270fSNick Piggin 	.fault = spufs_mss_mmap_fault,
1398d9379c4bSarnd@arndb.de };
1399d9379c4bSarnd@arndb.de 
1400d9379c4bSarnd@arndb.de /*
1401d9379c4bSarnd@arndb.de  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1402d9379c4bSarnd@arndb.de  */
1403d9379c4bSarnd@arndb.de static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1404d9379c4bSarnd@arndb.de {
1405d9379c4bSarnd@arndb.de 	if (!(vma->vm_flags & VM_SHARED))
1406d9379c4bSarnd@arndb.de 		return -EINVAL;
1407d9379c4bSarnd@arndb.de 
140878bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
1409d9379c4bSarnd@arndb.de 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
141023cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
1411d9379c4bSarnd@arndb.de 
1412d9379c4bSarnd@arndb.de 	vma->vm_ops = &spufs_mss_mmap_vmops;
1413d9379c4bSarnd@arndb.de 	return 0;
1414d9379c4bSarnd@arndb.de }
141527d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
141627d5bf2aSBenjamin Herrenschmidt #define spufs_mss_mmap NULL
141727d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
1418d9379c4bSarnd@arndb.de 
1419d9379c4bSarnd@arndb.de static int spufs_mss_open(struct inode *inode, struct file *file)
1420d9379c4bSarnd@arndb.de {
1421d9379c4bSarnd@arndb.de 	struct spufs_inode_info *i = SPUFS_I(inode);
142217e0e270SBenjamin Herrenschmidt 	struct spu_context *ctx = i->i_ctx;
1423d9379c4bSarnd@arndb.de 
1424d9379c4bSarnd@arndb.de 	file->private_data = i->i_ctx;
142543c2bbd9SChristoph Hellwig 
142647d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
142743c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
142817e0e270SBenjamin Herrenschmidt 		ctx->mss = inode->i_mapping;
142947d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1430d9379c4bSarnd@arndb.de 	return nonseekable_open(inode, file);
1431d9379c4bSarnd@arndb.de }
1432d9379c4bSarnd@arndb.de 
143343c2bbd9SChristoph Hellwig static int
143443c2bbd9SChristoph Hellwig spufs_mss_release(struct inode *inode, struct file *file)
143543c2bbd9SChristoph Hellwig {
143643c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
143743c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
143843c2bbd9SChristoph Hellwig 
143947d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
144043c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
144143c2bbd9SChristoph Hellwig 		ctx->mss = NULL;
144247d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
144343c2bbd9SChristoph Hellwig 	return 0;
144443c2bbd9SChristoph Hellwig }
144543c2bbd9SChristoph Hellwig 
14465dfe4c96SArjan van de Ven static const struct file_operations spufs_mss_fops = {
1447d9379c4bSarnd@arndb.de 	.open	 = spufs_mss_open,
144843c2bbd9SChristoph Hellwig 	.release = spufs_mss_release,
1449d9379c4bSarnd@arndb.de 	.mmap	 = spufs_mss_mmap,
145027d5bf2aSBenjamin Herrenschmidt };
145127d5bf2aSBenjamin Herrenschmidt 
1452b1e2270fSNick Piggin static int
1453b1e2270fSNick Piggin spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
145427d5bf2aSBenjamin Herrenschmidt {
145587ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
145627d5bf2aSBenjamin Herrenschmidt }
145727d5bf2aSBenjamin Herrenschmidt 
145827d5bf2aSBenjamin Herrenschmidt static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1459b1e2270fSNick Piggin 	.fault = spufs_psmap_mmap_fault,
146027d5bf2aSBenjamin Herrenschmidt };
146127d5bf2aSBenjamin Herrenschmidt 
146227d5bf2aSBenjamin Herrenschmidt /*
146327d5bf2aSBenjamin Herrenschmidt  * mmap support for full problem state area [0x00000 - 0x1ffff].
146427d5bf2aSBenjamin Herrenschmidt  */
146527d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
146627d5bf2aSBenjamin Herrenschmidt {
146727d5bf2aSBenjamin Herrenschmidt 	if (!(vma->vm_flags & VM_SHARED))
146827d5bf2aSBenjamin Herrenschmidt 		return -EINVAL;
146927d5bf2aSBenjamin Herrenschmidt 
147078bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
147127d5bf2aSBenjamin Herrenschmidt 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
147227d5bf2aSBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
147327d5bf2aSBenjamin Herrenschmidt 
147427d5bf2aSBenjamin Herrenschmidt 	vma->vm_ops = &spufs_psmap_mmap_vmops;
147527d5bf2aSBenjamin Herrenschmidt 	return 0;
147627d5bf2aSBenjamin Herrenschmidt }
147727d5bf2aSBenjamin Herrenschmidt 
147827d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_open(struct inode *inode, struct file *file)
147927d5bf2aSBenjamin Herrenschmidt {
148027d5bf2aSBenjamin Herrenschmidt 	struct spufs_inode_info *i = SPUFS_I(inode);
148117e0e270SBenjamin Herrenschmidt 	struct spu_context *ctx = i->i_ctx;
148227d5bf2aSBenjamin Herrenschmidt 
148347d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
148427d5bf2aSBenjamin Herrenschmidt 	file->private_data = i->i_ctx;
148543c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
148617e0e270SBenjamin Herrenschmidt 		ctx->psmap = inode->i_mapping;
148747d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
148827d5bf2aSBenjamin Herrenschmidt 	return nonseekable_open(inode, file);
148927d5bf2aSBenjamin Herrenschmidt }
149027d5bf2aSBenjamin Herrenschmidt 
149143c2bbd9SChristoph Hellwig static int
149243c2bbd9SChristoph Hellwig spufs_psmap_release(struct inode *inode, struct file *file)
149343c2bbd9SChristoph Hellwig {
149443c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
149543c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
149643c2bbd9SChristoph Hellwig 
149747d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
149843c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
149943c2bbd9SChristoph Hellwig 		ctx->psmap = NULL;
150047d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
150143c2bbd9SChristoph Hellwig 	return 0;
150243c2bbd9SChristoph Hellwig }
150343c2bbd9SChristoph Hellwig 
15045dfe4c96SArjan van de Ven static const struct file_operations spufs_psmap_fops = {
150527d5bf2aSBenjamin Herrenschmidt 	.open	 = spufs_psmap_open,
150643c2bbd9SChristoph Hellwig 	.release = spufs_psmap_release,
150727d5bf2aSBenjamin Herrenschmidt 	.mmap	 = spufs_psmap_mmap,
1508d9379c4bSarnd@arndb.de };
1509d9379c4bSarnd@arndb.de 
1510d9379c4bSarnd@arndb.de 
151127d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
1512b1e2270fSNick Piggin static int
1513b1e2270fSNick Piggin spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
15146df10a82SMark Nutter {
151587ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
15166df10a82SMark Nutter }
15176df10a82SMark Nutter 
15186df10a82SMark Nutter static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1519b1e2270fSNick Piggin 	.fault = spufs_mfc_mmap_fault,
15206df10a82SMark Nutter };
15216df10a82SMark Nutter 
15226df10a82SMark Nutter /*
15236df10a82SMark Nutter  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
15246df10a82SMark Nutter  */
15256df10a82SMark Nutter static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
15266df10a82SMark Nutter {
15276df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
15286df10a82SMark Nutter 		return -EINVAL;
15296df10a82SMark Nutter 
153078bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
15316df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
153223cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
15336df10a82SMark Nutter 
15346df10a82SMark Nutter 	vma->vm_ops = &spufs_mfc_mmap_vmops;
15356df10a82SMark Nutter 	return 0;
15366df10a82SMark Nutter }
153727d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
153827d5bf2aSBenjamin Herrenschmidt #define spufs_mfc_mmap NULL
153927d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
1540a33a7d73SArnd Bergmann 
1541a33a7d73SArnd Bergmann static int spufs_mfc_open(struct inode *inode, struct file *file)
1542a33a7d73SArnd Bergmann {
1543a33a7d73SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
1544a33a7d73SArnd Bergmann 	struct spu_context *ctx = i->i_ctx;
1545a33a7d73SArnd Bergmann 
1546a33a7d73SArnd Bergmann 	/* we don't want to deal with DMA into other processes */
1547a33a7d73SArnd Bergmann 	if (ctx->owner != current->mm)
1548a33a7d73SArnd Bergmann 		return -EINVAL;
1549a33a7d73SArnd Bergmann 
1550a33a7d73SArnd Bergmann 	if (atomic_read(&inode->i_count) != 1)
1551a33a7d73SArnd Bergmann 		return -EBUSY;
1552a33a7d73SArnd Bergmann 
155347d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
1554a33a7d73SArnd Bergmann 	file->private_data = ctx;
155543c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
155617e0e270SBenjamin Herrenschmidt 		ctx->mfc = inode->i_mapping;
155747d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1558a33a7d73SArnd Bergmann 	return nonseekable_open(inode, file);
1559a33a7d73SArnd Bergmann }
1560a33a7d73SArnd Bergmann 
156143c2bbd9SChristoph Hellwig static int
156243c2bbd9SChristoph Hellwig spufs_mfc_release(struct inode *inode, struct file *file)
156343c2bbd9SChristoph Hellwig {
156443c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
156543c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
156643c2bbd9SChristoph Hellwig 
156747d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
156843c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
156943c2bbd9SChristoph Hellwig 		ctx->mfc = NULL;
157047d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
157143c2bbd9SChristoph Hellwig 	return 0;
157243c2bbd9SChristoph Hellwig }
157343c2bbd9SChristoph Hellwig 
1574a33a7d73SArnd Bergmann /* interrupt-level mfc callback function. */
1575a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu)
1576a33a7d73SArnd Bergmann {
1577a33a7d73SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
1578a33a7d73SArnd Bergmann 
1579e65c2f6fSLuke Browning 	if (!ctx)
1580e65c2f6fSLuke Browning 		return;
1581e65c2f6fSLuke Browning 
1582a33a7d73SArnd Bergmann 	wake_up_all(&ctx->mfc_wq);
1583a33a7d73SArnd Bergmann 
1584e48b1b45SHarvey Harrison 	pr_debug("%s %s\n", __func__, spu->name);
1585a33a7d73SArnd Bergmann 	if (ctx->mfc_fasync) {
1586a33a7d73SArnd Bergmann 		u32 free_elements, tagstatus;
1587a33a7d73SArnd Bergmann 		unsigned int mask;
1588a33a7d73SArnd Bergmann 
1589a33a7d73SArnd Bergmann 		/* no need for spu_acquire in interrupt context */
1590a33a7d73SArnd Bergmann 		free_elements = ctx->ops->get_mfc_free_elements(ctx);
1591a33a7d73SArnd Bergmann 		tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1592a33a7d73SArnd Bergmann 
1593a33a7d73SArnd Bergmann 		mask = 0;
1594a33a7d73SArnd Bergmann 		if (free_elements & 0xffff)
1595a33a7d73SArnd Bergmann 			mask |= POLLOUT;
1596a33a7d73SArnd Bergmann 		if (tagstatus & ctx->tagwait)
1597a33a7d73SArnd Bergmann 			mask |= POLLIN;
1598a33a7d73SArnd Bergmann 
1599a33a7d73SArnd Bergmann 		kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1600a33a7d73SArnd Bergmann 	}
1601a33a7d73SArnd Bergmann }
1602a33a7d73SArnd Bergmann 
1603a33a7d73SArnd Bergmann static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1604a33a7d73SArnd Bergmann {
1605a33a7d73SArnd Bergmann 	/* See if there is one tag group is complete */
1606a33a7d73SArnd Bergmann 	/* FIXME we need locking around tagwait */
1607a33a7d73SArnd Bergmann 	*status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1608a33a7d73SArnd Bergmann 	ctx->tagwait &= ~*status;
1609a33a7d73SArnd Bergmann 	if (*status)
1610a33a7d73SArnd Bergmann 		return 1;
1611a33a7d73SArnd Bergmann 
1612a33a7d73SArnd Bergmann 	/* enable interrupt waiting for any tag group,
1613a33a7d73SArnd Bergmann 	   may silently fail if interrupts are already enabled */
1614a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1615a33a7d73SArnd Bergmann 	return 0;
1616a33a7d73SArnd Bergmann }
1617a33a7d73SArnd Bergmann 
1618a33a7d73SArnd Bergmann static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1619a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1620a33a7d73SArnd Bergmann {
1621a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1622a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1623a33a7d73SArnd Bergmann 	u32 status;
1624a33a7d73SArnd Bergmann 
1625a33a7d73SArnd Bergmann 	if (size != 4)
1626a33a7d73SArnd Bergmann 		goto out;
1627a33a7d73SArnd Bergmann 
1628c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1629c9101bdbSChristoph Hellwig 	if (ret)
1630c9101bdbSChristoph Hellwig 		return ret;
1631c9101bdbSChristoph Hellwig 
1632c9101bdbSChristoph Hellwig 	ret = -EINVAL;
1633a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1634a33a7d73SArnd Bergmann 		status = ctx->ops->read_mfc_tagstatus(ctx);
1635a33a7d73SArnd Bergmann 		if (!(status & ctx->tagwait))
1636a33a7d73SArnd Bergmann 			ret = -EAGAIN;
1637a33a7d73SArnd Bergmann 		else
1638c9101bdbSChristoph Hellwig 			/* XXX(hch): shouldn't we clear ret here? */
1639a33a7d73SArnd Bergmann 			ctx->tagwait &= ~status;
1640a33a7d73SArnd Bergmann 	} else {
1641a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1642a33a7d73SArnd Bergmann 			   spufs_read_mfc_tagstatus(ctx, &status));
1643a33a7d73SArnd Bergmann 		if (ret)
1644a33a7d73SArnd Bergmann 			goto out;
1645eebead5bSChristoph Hellwig 	}
1646eebead5bSChristoph Hellwig 	spu_release(ctx);
1647a33a7d73SArnd Bergmann 
1648a33a7d73SArnd Bergmann 	ret = 4;
1649a33a7d73SArnd Bergmann 	if (copy_to_user(buffer, &status, 4))
1650a33a7d73SArnd Bergmann 		ret = -EFAULT;
1651a33a7d73SArnd Bergmann 
1652a33a7d73SArnd Bergmann out:
1653a33a7d73SArnd Bergmann 	return ret;
1654a33a7d73SArnd Bergmann }
1655a33a7d73SArnd Bergmann 
1656a33a7d73SArnd Bergmann static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1657a33a7d73SArnd Bergmann {
1658a33a7d73SArnd Bergmann 	pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1659a33a7d73SArnd Bergmann 		 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1660a33a7d73SArnd Bergmann 
1661a33a7d73SArnd Bergmann 	switch (cmd->cmd) {
1662a33a7d73SArnd Bergmann 	case MFC_PUT_CMD:
1663a33a7d73SArnd Bergmann 	case MFC_PUTF_CMD:
1664a33a7d73SArnd Bergmann 	case MFC_PUTB_CMD:
1665a33a7d73SArnd Bergmann 	case MFC_GET_CMD:
1666a33a7d73SArnd Bergmann 	case MFC_GETF_CMD:
1667a33a7d73SArnd Bergmann 	case MFC_GETB_CMD:
1668a33a7d73SArnd Bergmann 		break;
1669a33a7d73SArnd Bergmann 	default:
1670a33a7d73SArnd Bergmann 		pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1671a33a7d73SArnd Bergmann 		return -EIO;
1672a33a7d73SArnd Bergmann 	}
1673a33a7d73SArnd Bergmann 
1674a33a7d73SArnd Bergmann 	if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1675a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1676a33a7d73SArnd Bergmann 				cmd->ea, cmd->lsa);
1677a33a7d73SArnd Bergmann 		return -EIO;
1678a33a7d73SArnd Bergmann 	}
1679a33a7d73SArnd Bergmann 
1680a33a7d73SArnd Bergmann 	switch (cmd->size & 0xf) {
1681a33a7d73SArnd Bergmann 	case 1:
1682a33a7d73SArnd Bergmann 		break;
1683a33a7d73SArnd Bergmann 	case 2:
1684a33a7d73SArnd Bergmann 		if (cmd->lsa & 1)
1685a33a7d73SArnd Bergmann 			goto error;
1686a33a7d73SArnd Bergmann 		break;
1687a33a7d73SArnd Bergmann 	case 4:
1688a33a7d73SArnd Bergmann 		if (cmd->lsa & 3)
1689a33a7d73SArnd Bergmann 			goto error;
1690a33a7d73SArnd Bergmann 		break;
1691a33a7d73SArnd Bergmann 	case 8:
1692a33a7d73SArnd Bergmann 		if (cmd->lsa & 7)
1693a33a7d73SArnd Bergmann 			goto error;
1694a33a7d73SArnd Bergmann 		break;
1695a33a7d73SArnd Bergmann 	case 0:
1696a33a7d73SArnd Bergmann 		if (cmd->lsa & 15)
1697a33a7d73SArnd Bergmann 			goto error;
1698a33a7d73SArnd Bergmann 		break;
1699a33a7d73SArnd Bergmann 	error:
1700a33a7d73SArnd Bergmann 	default:
1701a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment %x for size %x\n",
1702a33a7d73SArnd Bergmann 			cmd->lsa & 0xf, cmd->size);
1703a33a7d73SArnd Bergmann 		return -EIO;
1704a33a7d73SArnd Bergmann 	}
1705a33a7d73SArnd Bergmann 
1706a33a7d73SArnd Bergmann 	if (cmd->size > 16 * 1024) {
1707a33a7d73SArnd Bergmann 		pr_debug("invalid DMA size %x\n", cmd->size);
1708a33a7d73SArnd Bergmann 		return -EIO;
1709a33a7d73SArnd Bergmann 	}
1710a33a7d73SArnd Bergmann 
1711a33a7d73SArnd Bergmann 	if (cmd->tag & 0xfff0) {
1712a33a7d73SArnd Bergmann 		/* we reserve the higher tag numbers for kernel use */
1713a33a7d73SArnd Bergmann 		pr_debug("invalid DMA tag\n");
1714a33a7d73SArnd Bergmann 		return -EIO;
1715a33a7d73SArnd Bergmann 	}
1716a33a7d73SArnd Bergmann 
1717a33a7d73SArnd Bergmann 	if (cmd->class) {
1718a33a7d73SArnd Bergmann 		/* not supported in this version */
1719a33a7d73SArnd Bergmann 		pr_debug("invalid DMA class\n");
1720a33a7d73SArnd Bergmann 		return -EIO;
1721a33a7d73SArnd Bergmann 	}
1722a33a7d73SArnd Bergmann 
1723a33a7d73SArnd Bergmann 	return 0;
1724a33a7d73SArnd Bergmann }
1725a33a7d73SArnd Bergmann 
1726a33a7d73SArnd Bergmann static int spu_send_mfc_command(struct spu_context *ctx,
1727a33a7d73SArnd Bergmann 				struct mfc_dma_command cmd,
1728a33a7d73SArnd Bergmann 				int *error)
1729a33a7d73SArnd Bergmann {
1730a33a7d73SArnd Bergmann 	*error = ctx->ops->send_mfc_command(ctx, &cmd);
1731a33a7d73SArnd Bergmann 	if (*error == -EAGAIN) {
1732a33a7d73SArnd Bergmann 		/* wait for any tag group to complete
1733a33a7d73SArnd Bergmann 		   so we have space for the new command */
1734a33a7d73SArnd Bergmann 		ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1735a33a7d73SArnd Bergmann 		/* try again, because the queue might be
1736a33a7d73SArnd Bergmann 		   empty again */
1737a33a7d73SArnd Bergmann 		*error = ctx->ops->send_mfc_command(ctx, &cmd);
1738a33a7d73SArnd Bergmann 		if (*error == -EAGAIN)
1739a33a7d73SArnd Bergmann 			return 0;
1740a33a7d73SArnd Bergmann 	}
1741a33a7d73SArnd Bergmann 	return 1;
1742a33a7d73SArnd Bergmann }
1743a33a7d73SArnd Bergmann 
1744a33a7d73SArnd Bergmann static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1745a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1746a33a7d73SArnd Bergmann {
1747a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1748a33a7d73SArnd Bergmann 	struct mfc_dma_command cmd;
1749a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1750a33a7d73SArnd Bergmann 
1751a33a7d73SArnd Bergmann 	if (size != sizeof cmd)
1752a33a7d73SArnd Bergmann 		goto out;
1753a33a7d73SArnd Bergmann 
1754a33a7d73SArnd Bergmann 	ret = -EFAULT;
1755a33a7d73SArnd Bergmann 	if (copy_from_user(&cmd, buffer, sizeof cmd))
1756a33a7d73SArnd Bergmann 		goto out;
1757a33a7d73SArnd Bergmann 
1758a33a7d73SArnd Bergmann 	ret = spufs_check_valid_dma(&cmd);
1759a33a7d73SArnd Bergmann 	if (ret)
1760a33a7d73SArnd Bergmann 		goto out;
1761a33a7d73SArnd Bergmann 
1762c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1763c9101bdbSChristoph Hellwig 	if (ret)
1764c9101bdbSChristoph Hellwig 		goto out;
1765c9101bdbSChristoph Hellwig 
176633bfd7a7SArnd Bergmann 	ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1767577f8f10SAkinobu Mita 	if (ret)
1768577f8f10SAkinobu Mita 		goto out;
1769577f8f10SAkinobu Mita 
1770a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1771a33a7d73SArnd Bergmann 		ret = ctx->ops->send_mfc_command(ctx, &cmd);
1772a33a7d73SArnd Bergmann 	} else {
1773a33a7d73SArnd Bergmann 		int status;
1774a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1775a33a7d73SArnd Bergmann 				 spu_send_mfc_command(ctx, cmd, &status));
1776eebead5bSChristoph Hellwig 		if (ret)
1777eebead5bSChristoph Hellwig 			goto out;
1778a33a7d73SArnd Bergmann 		if (status)
1779a33a7d73SArnd Bergmann 			ret = status;
1780a33a7d73SArnd Bergmann 	}
1781a33a7d73SArnd Bergmann 
1782a33a7d73SArnd Bergmann 	if (ret)
1783933b0e35SKazunori Asayama 		goto out_unlock;
1784a33a7d73SArnd Bergmann 
1785a33a7d73SArnd Bergmann 	ctx->tagwait |= 1 << cmd.tag;
17863692dc66SMasato Noguchi 	ret = size;
1787a33a7d73SArnd Bergmann 
1788933b0e35SKazunori Asayama out_unlock:
1789933b0e35SKazunori Asayama 	spu_release(ctx);
1790a33a7d73SArnd Bergmann out:
1791a33a7d73SArnd Bergmann 	return ret;
1792a33a7d73SArnd Bergmann }
1793a33a7d73SArnd Bergmann 
1794a33a7d73SArnd Bergmann static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1795a33a7d73SArnd Bergmann {
1796a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1797a33a7d73SArnd Bergmann 	u32 free_elements, tagstatus;
1798a33a7d73SArnd Bergmann 	unsigned int mask;
1799a33a7d73SArnd Bergmann 
1800933b0e35SKazunori Asayama 	poll_wait(file, &ctx->mfc_wq, wait);
1801933b0e35SKazunori Asayama 
1802c9101bdbSChristoph Hellwig 	/*
1803c9101bdbSChristoph Hellwig 	 * For now keep this uninterruptible and also ignore the rule
1804c9101bdbSChristoph Hellwig 	 * that poll should not sleep.  Will be fixed later.
1805c9101bdbSChristoph Hellwig 	 */
1806c9101bdbSChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
1807a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1808a33a7d73SArnd Bergmann 	free_elements = ctx->ops->get_mfc_free_elements(ctx);
1809a33a7d73SArnd Bergmann 	tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1810a33a7d73SArnd Bergmann 	spu_release(ctx);
1811a33a7d73SArnd Bergmann 
1812a33a7d73SArnd Bergmann 	mask = 0;
1813a33a7d73SArnd Bergmann 	if (free_elements & 0xffff)
1814a33a7d73SArnd Bergmann 		mask |= POLLOUT | POLLWRNORM;
1815a33a7d73SArnd Bergmann 	if (tagstatus & ctx->tagwait)
1816a33a7d73SArnd Bergmann 		mask |= POLLIN | POLLRDNORM;
1817a33a7d73SArnd Bergmann 
1818e48b1b45SHarvey Harrison 	pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
1819a33a7d73SArnd Bergmann 		free_elements, tagstatus, ctx->tagwait);
1820a33a7d73SArnd Bergmann 
1821a33a7d73SArnd Bergmann 	return mask;
1822a33a7d73SArnd Bergmann }
1823a33a7d73SArnd Bergmann 
182473b6af8aSAl Viro static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1825a33a7d73SArnd Bergmann {
1826a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1827a33a7d73SArnd Bergmann 	int ret;
1828a33a7d73SArnd Bergmann 
1829c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1830c9101bdbSChristoph Hellwig 	if (ret)
1831eebead5bSChristoph Hellwig 		goto out;
1832a33a7d73SArnd Bergmann #if 0
1833a33a7d73SArnd Bergmann /* this currently hangs */
1834a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1835a33a7d73SArnd Bergmann 			 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1836a33a7d73SArnd Bergmann 	if (ret)
1837a33a7d73SArnd Bergmann 		goto out;
1838a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1839a33a7d73SArnd Bergmann 			 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1840eebead5bSChristoph Hellwig 	if (ret)
1841eebead5bSChristoph Hellwig 		goto out;
1842a33a7d73SArnd Bergmann #else
1843a33a7d73SArnd Bergmann 	ret = 0;
1844a33a7d73SArnd Bergmann #endif
1845a33a7d73SArnd Bergmann 	spu_release(ctx);
1846eebead5bSChristoph Hellwig out:
1847a33a7d73SArnd Bergmann 	return ret;
1848a33a7d73SArnd Bergmann }
1849a33a7d73SArnd Bergmann 
1850a33a7d73SArnd Bergmann static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1851a33a7d73SArnd Bergmann 			   int datasync)
1852a33a7d73SArnd Bergmann {
185373b6af8aSAl Viro 	return spufs_mfc_flush(file, NULL);
1854a33a7d73SArnd Bergmann }
1855a33a7d73SArnd Bergmann 
1856a33a7d73SArnd Bergmann static int spufs_mfc_fasync(int fd, struct file *file, int on)
1857a33a7d73SArnd Bergmann {
1858a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1859a33a7d73SArnd Bergmann 
1860a33a7d73SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1861a33a7d73SArnd Bergmann }
1862a33a7d73SArnd Bergmann 
18635dfe4c96SArjan van de Ven static const struct file_operations spufs_mfc_fops = {
1864a33a7d73SArnd Bergmann 	.open	 = spufs_mfc_open,
186543c2bbd9SChristoph Hellwig 	.release = spufs_mfc_release,
1866a33a7d73SArnd Bergmann 	.read	 = spufs_mfc_read,
1867a33a7d73SArnd Bergmann 	.write	 = spufs_mfc_write,
1868a33a7d73SArnd Bergmann 	.poll	 = spufs_mfc_poll,
1869a33a7d73SArnd Bergmann 	.flush	 = spufs_mfc_flush,
1870a33a7d73SArnd Bergmann 	.fsync	 = spufs_mfc_fsync,
1871a33a7d73SArnd Bergmann 	.fasync	 = spufs_mfc_fasync,
18726df10a82SMark Nutter 	.mmap	 = spufs_mfc_mmap,
1873a33a7d73SArnd Bergmann };
1874a33a7d73SArnd Bergmann 
1875197b1a82SChristoph Hellwig static int spufs_npc_set(void *data, u64 val)
187667207b96SArnd Bergmann {
187767207b96SArnd Bergmann 	struct spu_context *ctx = data;
1878c9101bdbSChristoph Hellwig 	int ret;
1879c9101bdbSChristoph Hellwig 
1880c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1881c9101bdbSChristoph Hellwig 	if (ret)
1882c9101bdbSChristoph Hellwig 		return ret;
18838b3d6663SArnd Bergmann 	ctx->ops->npc_write(ctx, val);
18848b3d6663SArnd Bergmann 	spu_release(ctx);
1885197b1a82SChristoph Hellwig 
1886197b1a82SChristoph Hellwig 	return 0;
188767207b96SArnd Bergmann }
188867207b96SArnd Bergmann 
1889104f0cc2SMichael Ellerman static u64 spufs_npc_get(struct spu_context *ctx)
189078810ff6SMichael Ellerman {
189178810ff6SMichael Ellerman 	return ctx->ops->npc_read(ctx);
189278810ff6SMichael Ellerman }
1893104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1894104f0cc2SMichael Ellerman 		       "0x%llx\n", SPU_ATTR_ACQUIRE);
189567207b96SArnd Bergmann 
1896197b1a82SChristoph Hellwig static int spufs_decr_set(void *data, u64 val)
18978b3d6663SArnd Bergmann {
18988b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
18998b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1900c9101bdbSChristoph Hellwig 	int ret;
1901c9101bdbSChristoph Hellwig 
1902c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1903c9101bdbSChristoph Hellwig 	if (ret)
1904c9101bdbSChristoph Hellwig 		return ret;
19058b3d6663SArnd Bergmann 	lscsa->decr.slot[0] = (u32) val;
190627b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1907197b1a82SChristoph Hellwig 
1908197b1a82SChristoph Hellwig 	return 0;
19098b3d6663SArnd Bergmann }
19108b3d6663SArnd Bergmann 
1911104f0cc2SMichael Ellerman static u64 spufs_decr_get(struct spu_context *ctx)
19128b3d6663SArnd Bergmann {
19138b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1914bf1ab978SDwayne Grant McConnell 	return lscsa->decr.slot[0];
1915bf1ab978SDwayne Grant McConnell }
1916104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1917104f0cc2SMichael Ellerman 		       "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
19188b3d6663SArnd Bergmann 
1919197b1a82SChristoph Hellwig static int spufs_decr_status_set(void *data, u64 val)
19208b3d6663SArnd Bergmann {
19218b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
1922c9101bdbSChristoph Hellwig 	int ret;
1923c9101bdbSChristoph Hellwig 
1924c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1925c9101bdbSChristoph Hellwig 	if (ret)
1926c9101bdbSChristoph Hellwig 		return ret;
1927d40a01d4SMasato Noguchi 	if (val)
1928d40a01d4SMasato Noguchi 		ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1929d40a01d4SMasato Noguchi 	else
1930d40a01d4SMasato Noguchi 		ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
193127b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1932197b1a82SChristoph Hellwig 
1933197b1a82SChristoph Hellwig 	return 0;
19348b3d6663SArnd Bergmann }
19358b3d6663SArnd Bergmann 
1936104f0cc2SMichael Ellerman static u64 spufs_decr_status_get(struct spu_context *ctx)
19378b3d6663SArnd Bergmann {
1938d40a01d4SMasato Noguchi 	if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1939d40a01d4SMasato Noguchi 		return SPU_DECR_STATUS_RUNNING;
1940d40a01d4SMasato Noguchi 	else
1941d40a01d4SMasato Noguchi 		return 0;
1942bf1ab978SDwayne Grant McConnell }
1943104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1944104f0cc2SMichael Ellerman 		       spufs_decr_status_set, "0x%llx\n",
1945104f0cc2SMichael Ellerman 		       SPU_ATTR_ACQUIRE_SAVED);
19468b3d6663SArnd Bergmann 
1947197b1a82SChristoph Hellwig static int spufs_event_mask_set(void *data, u64 val)
19488b3d6663SArnd Bergmann {
19498b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
19508b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1951c9101bdbSChristoph Hellwig 	int ret;
1952c9101bdbSChristoph Hellwig 
1953c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1954c9101bdbSChristoph Hellwig 	if (ret)
1955c9101bdbSChristoph Hellwig 		return ret;
19568b3d6663SArnd Bergmann 	lscsa->event_mask.slot[0] = (u32) val;
195727b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1958197b1a82SChristoph Hellwig 
1959197b1a82SChristoph Hellwig 	return 0;
19608b3d6663SArnd Bergmann }
19618b3d6663SArnd Bergmann 
1962104f0cc2SMichael Ellerman static u64 spufs_event_mask_get(struct spu_context *ctx)
19638b3d6663SArnd Bergmann {
19648b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1965bf1ab978SDwayne Grant McConnell 	return lscsa->event_mask.slot[0];
1966bf1ab978SDwayne Grant McConnell }
1967bf1ab978SDwayne Grant McConnell 
1968104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1969104f0cc2SMichael Ellerman 		       spufs_event_mask_set, "0x%llx\n",
1970104f0cc2SMichael Ellerman 		       SPU_ATTR_ACQUIRE_SAVED);
19718b3d6663SArnd Bergmann 
1972104f0cc2SMichael Ellerman static u64 spufs_event_status_get(struct spu_context *ctx)
1973b9e3bd77SDwayne Grant McConnell {
1974b9e3bd77SDwayne Grant McConnell 	struct spu_state *state = &ctx->csa;
1975b9e3bd77SDwayne Grant McConnell 	u64 stat;
1976b9e3bd77SDwayne Grant McConnell 	stat = state->spu_chnlcnt_RW[0];
1977b9e3bd77SDwayne Grant McConnell 	if (stat)
1978bf1ab978SDwayne Grant McConnell 		return state->spu_chnldata_RW[0];
1979bf1ab978SDwayne Grant McConnell 	return 0;
1980bf1ab978SDwayne Grant McConnell }
1981104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1982104f0cc2SMichael Ellerman 		       NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1983b9e3bd77SDwayne Grant McConnell 
1984197b1a82SChristoph Hellwig static int spufs_srr0_set(void *data, u64 val)
19858b3d6663SArnd Bergmann {
19868b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
19878b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1988c9101bdbSChristoph Hellwig 	int ret;
1989c9101bdbSChristoph Hellwig 
1990c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1991c9101bdbSChristoph Hellwig 	if (ret)
1992c9101bdbSChristoph Hellwig 		return ret;
19938b3d6663SArnd Bergmann 	lscsa->srr0.slot[0] = (u32) val;
199427b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1995197b1a82SChristoph Hellwig 
1996197b1a82SChristoph Hellwig 	return 0;
19978b3d6663SArnd Bergmann }
19988b3d6663SArnd Bergmann 
1999104f0cc2SMichael Ellerman static u64 spufs_srr0_get(struct spu_context *ctx)
20008b3d6663SArnd Bergmann {
20018b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
2002104f0cc2SMichael Ellerman 	return lscsa->srr0.slot[0];
20038b3d6663SArnd Bergmann }
2004104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
2005104f0cc2SMichael Ellerman 		       "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
20068b3d6663SArnd Bergmann 
2007104f0cc2SMichael Ellerman static u64 spufs_id_get(struct spu_context *ctx)
20087b1a7014Sarnd@arndb.de {
20097b1a7014Sarnd@arndb.de 	u64 num;
20107b1a7014Sarnd@arndb.de 
20117b1a7014Sarnd@arndb.de 	if (ctx->state == SPU_STATE_RUNNABLE)
20127b1a7014Sarnd@arndb.de 		num = ctx->spu->number;
20137b1a7014Sarnd@arndb.de 	else
20147b1a7014Sarnd@arndb.de 		num = (unsigned int)-1;
20157b1a7014Sarnd@arndb.de 
20167b1a7014Sarnd@arndb.de 	return num;
20177b1a7014Sarnd@arndb.de }
2018104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
2019104f0cc2SMichael Ellerman 		       SPU_ATTR_ACQUIRE)
20207b1a7014Sarnd@arndb.de 
2021104f0cc2SMichael Ellerman static u64 spufs_object_id_get(struct spu_context *ctx)
2022bf1ab978SDwayne Grant McConnell {
2023bf1ab978SDwayne Grant McConnell 	/* FIXME: Should there really be no locking here? */
2024104f0cc2SMichael Ellerman 	return ctx->object_id;
2025bf1ab978SDwayne Grant McConnell }
2026bf1ab978SDwayne Grant McConnell 
2027197b1a82SChristoph Hellwig static int spufs_object_id_set(void *data, u64 id)
202886767277SArnd Bergmann {
202986767277SArnd Bergmann 	struct spu_context *ctx = data;
203086767277SArnd Bergmann 	ctx->object_id = id;
2031197b1a82SChristoph Hellwig 
2032197b1a82SChristoph Hellwig 	return 0;
203386767277SArnd Bergmann }
203486767277SArnd Bergmann 
2035104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2036104f0cc2SMichael Ellerman 		       spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
203786767277SArnd Bergmann 
2038104f0cc2SMichael Ellerman static u64 spufs_lslr_get(struct spu_context *ctx)
2039bf1ab978SDwayne Grant McConnell {
2040bf1ab978SDwayne Grant McConnell 	return ctx->csa.priv2.spu_lslr_RW;
2041bf1ab978SDwayne Grant McConnell }
2042104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2043104f0cc2SMichael Ellerman 		       SPU_ATTR_ACQUIRE_SAVED);
2044b9e3bd77SDwayne Grant McConnell 
2045b9e3bd77SDwayne Grant McConnell static int spufs_info_open(struct inode *inode, struct file *file)
2046b9e3bd77SDwayne Grant McConnell {
2047b9e3bd77SDwayne Grant McConnell 	struct spufs_inode_info *i = SPUFS_I(inode);
2048b9e3bd77SDwayne Grant McConnell 	struct spu_context *ctx = i->i_ctx;
2049b9e3bd77SDwayne Grant McConnell 	file->private_data = ctx;
2050b9e3bd77SDwayne Grant McConnell 	return 0;
2051b9e3bd77SDwayne Grant McConnell }
2052b9e3bd77SDwayne Grant McConnell 
2053cbe709c1SBenjamin Herrenschmidt static int spufs_caps_show(struct seq_file *s, void *private)
2054cbe709c1SBenjamin Herrenschmidt {
2055cbe709c1SBenjamin Herrenschmidt 	struct spu_context *ctx = s->private;
2056cbe709c1SBenjamin Herrenschmidt 
2057cbe709c1SBenjamin Herrenschmidt 	if (!(ctx->flags & SPU_CREATE_NOSCHED))
2058cbe709c1SBenjamin Herrenschmidt 		seq_puts(s, "sched\n");
2059cbe709c1SBenjamin Herrenschmidt 	if (!(ctx->flags & SPU_CREATE_ISOLATE))
2060cbe709c1SBenjamin Herrenschmidt 		seq_puts(s, "step\n");
2061cbe709c1SBenjamin Herrenschmidt 	return 0;
2062cbe709c1SBenjamin Herrenschmidt }
2063cbe709c1SBenjamin Herrenschmidt 
2064cbe709c1SBenjamin Herrenschmidt static int spufs_caps_open(struct inode *inode, struct file *file)
2065cbe709c1SBenjamin Herrenschmidt {
2066cbe709c1SBenjamin Herrenschmidt 	return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2067cbe709c1SBenjamin Herrenschmidt }
2068cbe709c1SBenjamin Herrenschmidt 
2069cbe709c1SBenjamin Herrenschmidt static const struct file_operations spufs_caps_fops = {
2070cbe709c1SBenjamin Herrenschmidt 	.open		= spufs_caps_open,
2071cbe709c1SBenjamin Herrenschmidt 	.read		= seq_read,
2072cbe709c1SBenjamin Herrenschmidt 	.llseek		= seq_lseek,
2073cbe709c1SBenjamin Herrenschmidt 	.release	= single_release,
2074cbe709c1SBenjamin Herrenschmidt };
2075cbe709c1SBenjamin Herrenschmidt 
2076bf1ab978SDwayne Grant McConnell static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2077bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2078bf1ab978SDwayne Grant McConnell {
2079bf1ab978SDwayne Grant McConnell 	u32 data;
2080bf1ab978SDwayne Grant McConnell 
2081cbea9238SJeremy Kerr 	/* EOF if there's no entry in the mbox */
2082cbea9238SJeremy Kerr 	if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2083cbea9238SJeremy Kerr 		return 0;
2084cbea9238SJeremy Kerr 
2085bf1ab978SDwayne Grant McConnell 	data = ctx->csa.prob.pu_mb_R;
2086bf1ab978SDwayne Grant McConnell 
2087bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2088bf1ab978SDwayne Grant McConnell }
2089bf1ab978SDwayne Grant McConnell 
209069a2f00cSDwayne Grant McConnell static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
209169a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
209269a2f00cSDwayne Grant McConnell {
2093bf1ab978SDwayne Grant McConnell 	int ret;
209469a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
209569a2f00cSDwayne Grant McConnell 
209669a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
209769a2f00cSDwayne Grant McConnell 		return -EFAULT;
209869a2f00cSDwayne Grant McConnell 
2099c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2100c9101bdbSChristoph Hellwig 	if (ret)
2101c9101bdbSChristoph Hellwig 		return ret;
210269a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2103bf1ab978SDwayne Grant McConnell 	ret = __spufs_mbox_info_read(ctx, buf, len, pos);
210469a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
210527b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
210669a2f00cSDwayne Grant McConnell 
2107bf1ab978SDwayne Grant McConnell 	return ret;
210869a2f00cSDwayne Grant McConnell }
210969a2f00cSDwayne Grant McConnell 
21105dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_info_fops = {
211169a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
211269a2f00cSDwayne Grant McConnell 	.read = spufs_mbox_info_read,
211369a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
211469a2f00cSDwayne Grant McConnell };
211569a2f00cSDwayne Grant McConnell 
2116bf1ab978SDwayne Grant McConnell static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2117bf1ab978SDwayne Grant McConnell 				char __user *buf, size_t len, loff_t *pos)
2118bf1ab978SDwayne Grant McConnell {
2119bf1ab978SDwayne Grant McConnell 	u32 data;
2120bf1ab978SDwayne Grant McConnell 
2121cbea9238SJeremy Kerr 	/* EOF if there's no entry in the ibox */
2122cbea9238SJeremy Kerr 	if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2123cbea9238SJeremy Kerr 		return 0;
2124cbea9238SJeremy Kerr 
2125bf1ab978SDwayne Grant McConnell 	data = ctx->csa.priv2.puint_mb_R;
2126bf1ab978SDwayne Grant McConnell 
2127bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2128bf1ab978SDwayne Grant McConnell }
2129bf1ab978SDwayne Grant McConnell 
213069a2f00cSDwayne Grant McConnell static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
213169a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
213269a2f00cSDwayne Grant McConnell {
213369a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2134bf1ab978SDwayne Grant McConnell 	int ret;
213569a2f00cSDwayne Grant McConnell 
213669a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
213769a2f00cSDwayne Grant McConnell 		return -EFAULT;
213869a2f00cSDwayne Grant McConnell 
2139c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2140c9101bdbSChristoph Hellwig 	if (ret)
2141c9101bdbSChristoph Hellwig 		return ret;
214269a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2143bf1ab978SDwayne Grant McConnell 	ret = __spufs_ibox_info_read(ctx, buf, len, pos);
214469a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
214527b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
214669a2f00cSDwayne Grant McConnell 
2147bf1ab978SDwayne Grant McConnell 	return ret;
214869a2f00cSDwayne Grant McConnell }
214969a2f00cSDwayne Grant McConnell 
21505dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_info_fops = {
215169a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
215269a2f00cSDwayne Grant McConnell 	.read = spufs_ibox_info_read,
215369a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
215469a2f00cSDwayne Grant McConnell };
215569a2f00cSDwayne Grant McConnell 
2156bf1ab978SDwayne Grant McConnell static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2157bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2158bf1ab978SDwayne Grant McConnell {
2159bf1ab978SDwayne Grant McConnell 	int i, cnt;
2160bf1ab978SDwayne Grant McConnell 	u32 data[4];
2161bf1ab978SDwayne Grant McConnell 	u32 wbox_stat;
2162bf1ab978SDwayne Grant McConnell 
2163bf1ab978SDwayne Grant McConnell 	wbox_stat = ctx->csa.prob.mb_stat_R;
2164bf1ab978SDwayne Grant McConnell 	cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2165bf1ab978SDwayne Grant McConnell 	for (i = 0; i < cnt; i++) {
2166bf1ab978SDwayne Grant McConnell 		data[i] = ctx->csa.spu_mailbox_data[i];
2167bf1ab978SDwayne Grant McConnell 	}
2168bf1ab978SDwayne Grant McConnell 
2169bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data,
2170bf1ab978SDwayne Grant McConnell 				cnt * sizeof(u32));
2171bf1ab978SDwayne Grant McConnell }
2172bf1ab978SDwayne Grant McConnell 
217369a2f00cSDwayne Grant McConnell static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
217469a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
217569a2f00cSDwayne Grant McConnell {
217669a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2177bf1ab978SDwayne Grant McConnell 	int ret;
217869a2f00cSDwayne Grant McConnell 
217969a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
218069a2f00cSDwayne Grant McConnell 		return -EFAULT;
218169a2f00cSDwayne Grant McConnell 
2182c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2183c9101bdbSChristoph Hellwig 	if (ret)
2184c9101bdbSChristoph Hellwig 		return ret;
218569a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2186bf1ab978SDwayne Grant McConnell 	ret = __spufs_wbox_info_read(ctx, buf, len, pos);
218769a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
218827b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
218969a2f00cSDwayne Grant McConnell 
2190bf1ab978SDwayne Grant McConnell 	return ret;
219169a2f00cSDwayne Grant McConnell }
219269a2f00cSDwayne Grant McConnell 
21935dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_info_fops = {
219469a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
219569a2f00cSDwayne Grant McConnell 	.read = spufs_wbox_info_read,
219669a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
219769a2f00cSDwayne Grant McConnell };
219869a2f00cSDwayne Grant McConnell 
2199bf1ab978SDwayne Grant McConnell static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2200bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2201b9e3bd77SDwayne Grant McConnell {
2202b9e3bd77SDwayne Grant McConnell 	struct spu_dma_info info;
2203b9e3bd77SDwayne Grant McConnell 	struct mfc_cq_sr *qp, *spuqp;
2204b9e3bd77SDwayne Grant McConnell 	int i;
2205b9e3bd77SDwayne Grant McConnell 
2206b9e3bd77SDwayne Grant McConnell 	info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2207b9e3bd77SDwayne Grant McConnell 	info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2208b9e3bd77SDwayne Grant McConnell 	info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2209b9e3bd77SDwayne Grant McConnell 	info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2210b9e3bd77SDwayne Grant McConnell 	info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2211b9e3bd77SDwayne Grant McConnell 	for (i = 0; i < 16; i++) {
2212b9e3bd77SDwayne Grant McConnell 		qp = &info.dma_info_command_data[i];
2213b9e3bd77SDwayne Grant McConnell 		spuqp = &ctx->csa.priv2.spuq[i];
2214b9e3bd77SDwayne Grant McConnell 
2215b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2216b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2217b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2218b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2219b9e3bd77SDwayne Grant McConnell 	}
2220b9e3bd77SDwayne Grant McConnell 
2221b9e3bd77SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &info,
2222b9e3bd77SDwayne Grant McConnell 				sizeof info);
2223b9e3bd77SDwayne Grant McConnell }
2224b9e3bd77SDwayne Grant McConnell 
2225bf1ab978SDwayne Grant McConnell static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2226bf1ab978SDwayne Grant McConnell 			      size_t len, loff_t *pos)
2227bf1ab978SDwayne Grant McConnell {
2228bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2229bf1ab978SDwayne Grant McConnell 	int ret;
2230bf1ab978SDwayne Grant McConnell 
2231bf1ab978SDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
2232bf1ab978SDwayne Grant McConnell 		return -EFAULT;
2233bf1ab978SDwayne Grant McConnell 
2234c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2235c9101bdbSChristoph Hellwig 	if (ret)
2236c9101bdbSChristoph Hellwig 		return ret;
2237bf1ab978SDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2238bf1ab978SDwayne Grant McConnell 	ret = __spufs_dma_info_read(ctx, buf, len, pos);
2239bf1ab978SDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
224027b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
2241bf1ab978SDwayne Grant McConnell 
2242bf1ab978SDwayne Grant McConnell 	return ret;
2243bf1ab978SDwayne Grant McConnell }
2244bf1ab978SDwayne Grant McConnell 
22455dfe4c96SArjan van de Ven static const struct file_operations spufs_dma_info_fops = {
2246b9e3bd77SDwayne Grant McConnell 	.open = spufs_info_open,
2247b9e3bd77SDwayne Grant McConnell 	.read = spufs_dma_info_read,
2248b9e3bd77SDwayne Grant McConnell };
2249b9e3bd77SDwayne Grant McConnell 
2250bf1ab978SDwayne Grant McConnell static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2251bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2252b9e3bd77SDwayne Grant McConnell {
2253b9e3bd77SDwayne Grant McConnell 	struct spu_proxydma_info info;
2254b9e3bd77SDwayne Grant McConnell 	struct mfc_cq_sr *qp, *puqp;
2255bf1ab978SDwayne Grant McConnell 	int ret = sizeof info;
2256b9e3bd77SDwayne Grant McConnell 	int i;
2257b9e3bd77SDwayne Grant McConnell 
2258b9e3bd77SDwayne Grant McConnell 	if (len < ret)
2259b9e3bd77SDwayne Grant McConnell 		return -EINVAL;
2260b9e3bd77SDwayne Grant McConnell 
2261b9e3bd77SDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
2262b9e3bd77SDwayne Grant McConnell 		return -EFAULT;
2263b9e3bd77SDwayne Grant McConnell 
2264b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2265b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2266b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2267b9e3bd77SDwayne Grant McConnell 	for (i = 0; i < 8; i++) {
2268b9e3bd77SDwayne Grant McConnell 		qp = &info.proxydma_info_command_data[i];
2269b9e3bd77SDwayne Grant McConnell 		puqp = &ctx->csa.priv2.puq[i];
2270b9e3bd77SDwayne Grant McConnell 
2271b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2272b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2273b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2274b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2275b9e3bd77SDwayne Grant McConnell 	}
2276bf1ab978SDwayne Grant McConnell 
2277bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &info,
2278bf1ab978SDwayne Grant McConnell 				sizeof info);
2279bf1ab978SDwayne Grant McConnell }
2280bf1ab978SDwayne Grant McConnell 
2281bf1ab978SDwayne Grant McConnell static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2282bf1ab978SDwayne Grant McConnell 				   size_t len, loff_t *pos)
2283bf1ab978SDwayne Grant McConnell {
2284bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2285bf1ab978SDwayne Grant McConnell 	int ret;
2286bf1ab978SDwayne Grant McConnell 
2287c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2288c9101bdbSChristoph Hellwig 	if (ret)
2289c9101bdbSChristoph Hellwig 		return ret;
2290bf1ab978SDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2291bf1ab978SDwayne Grant McConnell 	ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2292b9e3bd77SDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
229327b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
2294b9e3bd77SDwayne Grant McConnell 
2295b9e3bd77SDwayne Grant McConnell 	return ret;
2296b9e3bd77SDwayne Grant McConnell }
2297b9e3bd77SDwayne Grant McConnell 
22985dfe4c96SArjan van de Ven static const struct file_operations spufs_proxydma_info_fops = {
2299b9e3bd77SDwayne Grant McConnell 	.open = spufs_info_open,
2300b9e3bd77SDwayne Grant McConnell 	.read = spufs_proxydma_info_read,
2301b9e3bd77SDwayne Grant McConnell };
2302b9e3bd77SDwayne Grant McConnell 
2303476273adSChristoph Hellwig static int spufs_show_tid(struct seq_file *s, void *private)
2304476273adSChristoph Hellwig {
2305476273adSChristoph Hellwig 	struct spu_context *ctx = s->private;
2306476273adSChristoph Hellwig 
2307476273adSChristoph Hellwig 	seq_printf(s, "%d\n", ctx->tid);
2308476273adSChristoph Hellwig 	return 0;
2309476273adSChristoph Hellwig }
2310476273adSChristoph Hellwig 
2311476273adSChristoph Hellwig static int spufs_tid_open(struct inode *inode, struct file *file)
2312476273adSChristoph Hellwig {
2313476273adSChristoph Hellwig 	return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2314476273adSChristoph Hellwig }
2315476273adSChristoph Hellwig 
2316476273adSChristoph Hellwig static const struct file_operations spufs_tid_fops = {
2317476273adSChristoph Hellwig 	.open		= spufs_tid_open,
2318476273adSChristoph Hellwig 	.read		= seq_read,
2319476273adSChristoph Hellwig 	.llseek		= seq_lseek,
2320476273adSChristoph Hellwig 	.release	= single_release,
2321476273adSChristoph Hellwig };
2322476273adSChristoph Hellwig 
2323e9f8a0b6SChristoph Hellwig static const char *ctx_state_names[] = {
2324e9f8a0b6SChristoph Hellwig 	"user", "system", "iowait", "loaded"
2325e9f8a0b6SChristoph Hellwig };
2326e9f8a0b6SChristoph Hellwig 
2327e9f8a0b6SChristoph Hellwig static unsigned long long spufs_acct_time(struct spu_context *ctx,
232827ec41d3SAndre Detsch 		enum spu_utilization_state state)
2329e9f8a0b6SChristoph Hellwig {
233027ec41d3SAndre Detsch 	struct timespec ts;
233127ec41d3SAndre Detsch 	unsigned long long time = ctx->stats.times[state];
2332e9f8a0b6SChristoph Hellwig 
233327ec41d3SAndre Detsch 	/*
233427ec41d3SAndre Detsch 	 * In general, utilization statistics are updated by the controlling
233527ec41d3SAndre Detsch 	 * thread as the spu context moves through various well defined
233627ec41d3SAndre Detsch 	 * state transitions, but if the context is lazily loaded its
233727ec41d3SAndre Detsch 	 * utilization statistics are not updated as the controlling thread
233827ec41d3SAndre Detsch 	 * is not tightly coupled with the execution of the spu context.  We
233927ec41d3SAndre Detsch 	 * calculate and apply the time delta from the last recorded state
234027ec41d3SAndre Detsch 	 * of the spu context.
234127ec41d3SAndre Detsch 	 */
234227ec41d3SAndre Detsch 	if (ctx->spu && ctx->stats.util_state == state) {
234327ec41d3SAndre Detsch 		ktime_get_ts(&ts);
234427ec41d3SAndre Detsch 		time += timespec_to_ns(&ts) - ctx->stats.tstamp;
234527ec41d3SAndre Detsch 	}
2346e9f8a0b6SChristoph Hellwig 
234727ec41d3SAndre Detsch 	return time / NSEC_PER_MSEC;
2348e9f8a0b6SChristoph Hellwig }
2349e9f8a0b6SChristoph Hellwig 
2350e9f8a0b6SChristoph Hellwig static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2351e9f8a0b6SChristoph Hellwig {
2352e9f8a0b6SChristoph Hellwig 	unsigned long long slb_flts = ctx->stats.slb_flt;
2353e9f8a0b6SChristoph Hellwig 
2354e9f8a0b6SChristoph Hellwig 	if (ctx->state == SPU_STATE_RUNNABLE) {
2355e9f8a0b6SChristoph Hellwig 		slb_flts += (ctx->spu->stats.slb_flt -
2356e9f8a0b6SChristoph Hellwig 			     ctx->stats.slb_flt_base);
2357e9f8a0b6SChristoph Hellwig 	}
2358e9f8a0b6SChristoph Hellwig 
2359e9f8a0b6SChristoph Hellwig 	return slb_flts;
2360e9f8a0b6SChristoph Hellwig }
2361e9f8a0b6SChristoph Hellwig 
2362e9f8a0b6SChristoph Hellwig static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2363e9f8a0b6SChristoph Hellwig {
2364e9f8a0b6SChristoph Hellwig 	unsigned long long class2_intrs = ctx->stats.class2_intr;
2365e9f8a0b6SChristoph Hellwig 
2366e9f8a0b6SChristoph Hellwig 	if (ctx->state == SPU_STATE_RUNNABLE) {
2367e9f8a0b6SChristoph Hellwig 		class2_intrs += (ctx->spu->stats.class2_intr -
2368e9f8a0b6SChristoph Hellwig 				 ctx->stats.class2_intr_base);
2369e9f8a0b6SChristoph Hellwig 	}
2370e9f8a0b6SChristoph Hellwig 
2371e9f8a0b6SChristoph Hellwig 	return class2_intrs;
2372e9f8a0b6SChristoph Hellwig }
2373e9f8a0b6SChristoph Hellwig 
2374e9f8a0b6SChristoph Hellwig 
2375e9f8a0b6SChristoph Hellwig static int spufs_show_stat(struct seq_file *s, void *private)
2376e9f8a0b6SChristoph Hellwig {
2377e9f8a0b6SChristoph Hellwig 	struct spu_context *ctx = s->private;
2378c9101bdbSChristoph Hellwig 	int ret;
2379e9f8a0b6SChristoph Hellwig 
2380c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
2381c9101bdbSChristoph Hellwig 	if (ret)
2382c9101bdbSChristoph Hellwig 		return ret;
2383c9101bdbSChristoph Hellwig 
2384e9f8a0b6SChristoph Hellwig 	seq_printf(s, "%s %llu %llu %llu %llu "
2385e9f8a0b6SChristoph Hellwig 		      "%llu %llu %llu %llu %llu %llu %llu %llu\n",
238627ec41d3SAndre Detsch 		ctx_state_names[ctx->stats.util_state],
238727ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_USER),
238827ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
238927ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
239027ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2391e9f8a0b6SChristoph Hellwig 		ctx->stats.vol_ctx_switch,
2392e9f8a0b6SChristoph Hellwig 		ctx->stats.invol_ctx_switch,
2393e9f8a0b6SChristoph Hellwig 		spufs_slb_flts(ctx),
2394e9f8a0b6SChristoph Hellwig 		ctx->stats.hash_flt,
2395e9f8a0b6SChristoph Hellwig 		ctx->stats.min_flt,
2396e9f8a0b6SChristoph Hellwig 		ctx->stats.maj_flt,
2397e9f8a0b6SChristoph Hellwig 		spufs_class2_intrs(ctx),
2398e9f8a0b6SChristoph Hellwig 		ctx->stats.libassist);
2399e9f8a0b6SChristoph Hellwig 	spu_release(ctx);
2400e9f8a0b6SChristoph Hellwig 	return 0;
2401e9f8a0b6SChristoph Hellwig }
2402e9f8a0b6SChristoph Hellwig 
2403e9f8a0b6SChristoph Hellwig static int spufs_stat_open(struct inode *inode, struct file *file)
2404e9f8a0b6SChristoph Hellwig {
2405e9f8a0b6SChristoph Hellwig 	return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2406e9f8a0b6SChristoph Hellwig }
2407e9f8a0b6SChristoph Hellwig 
2408e9f8a0b6SChristoph Hellwig static const struct file_operations spufs_stat_fops = {
2409e9f8a0b6SChristoph Hellwig 	.open		= spufs_stat_open,
2410e9f8a0b6SChristoph Hellwig 	.read		= seq_read,
2411e9f8a0b6SChristoph Hellwig 	.llseek		= seq_lseek,
2412e9f8a0b6SChristoph Hellwig 	.release	= single_release,
2413e9f8a0b6SChristoph Hellwig };
2414e9f8a0b6SChristoph Hellwig 
24155158e9b5SChristoph Hellwig static inline int spufs_switch_log_used(struct spu_context *ctx)
24165158e9b5SChristoph Hellwig {
24175158e9b5SChristoph Hellwig 	return (ctx->switch_log->head - ctx->switch_log->tail) %
24185158e9b5SChristoph Hellwig 		SWITCH_LOG_BUFSIZE;
24195158e9b5SChristoph Hellwig }
24205158e9b5SChristoph Hellwig 
24215158e9b5SChristoph Hellwig static inline int spufs_switch_log_avail(struct spu_context *ctx)
24225158e9b5SChristoph Hellwig {
24235158e9b5SChristoph Hellwig 	return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
24245158e9b5SChristoph Hellwig }
24255158e9b5SChristoph Hellwig 
24265158e9b5SChristoph Hellwig static int spufs_switch_log_open(struct inode *inode, struct file *file)
24275158e9b5SChristoph Hellwig {
24285158e9b5SChristoph Hellwig 	struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2429f5ed0eb6SJeremy Kerr 	int rc;
24305158e9b5SChristoph Hellwig 
2431f5ed0eb6SJeremy Kerr 	rc = spu_acquire(ctx);
2432f5ed0eb6SJeremy Kerr 	if (rc)
2433f5ed0eb6SJeremy Kerr 		return rc;
2434f5ed0eb6SJeremy Kerr 
24355158e9b5SChristoph Hellwig 	if (ctx->switch_log) {
2436f5ed0eb6SJeremy Kerr 		rc = -EBUSY;
2437f5ed0eb6SJeremy Kerr 		goto out;
2438f5ed0eb6SJeremy Kerr 	}
2439f5ed0eb6SJeremy Kerr 
24405158e9b5SChristoph Hellwig 	ctx->switch_log = kzalloc(sizeof(struct switch_log) +
24415158e9b5SChristoph Hellwig 		SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
24425158e9b5SChristoph Hellwig 		GFP_KERNEL);
2443f5ed0eb6SJeremy Kerr 
2444f5ed0eb6SJeremy Kerr 	if (!ctx->switch_log) {
2445f5ed0eb6SJeremy Kerr 		rc = -ENOMEM;
24465158e9b5SChristoph Hellwig 		goto out;
24475158e9b5SChristoph Hellwig 	}
2448f5ed0eb6SJeremy Kerr 
2449f5ed0eb6SJeremy Kerr 	init_waitqueue_head(&ctx->switch_log->wait);
2450f5ed0eb6SJeremy Kerr 	rc = 0;
2451f5ed0eb6SJeremy Kerr 
2452f5ed0eb6SJeremy Kerr out:
2453f5ed0eb6SJeremy Kerr 	spu_release(ctx);
2454f5ed0eb6SJeremy Kerr 	return rc;
2455f5ed0eb6SJeremy Kerr }
2456f5ed0eb6SJeremy Kerr 
2457f5ed0eb6SJeremy Kerr static int spufs_switch_log_release(struct inode *inode, struct file *file)
2458f5ed0eb6SJeremy Kerr {
2459f5ed0eb6SJeremy Kerr 	struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2460f5ed0eb6SJeremy Kerr 	int rc;
2461f5ed0eb6SJeremy Kerr 
2462f5ed0eb6SJeremy Kerr 	rc = spu_acquire(ctx);
2463f5ed0eb6SJeremy Kerr 	if (rc)
2464f5ed0eb6SJeremy Kerr 		return rc;
2465f5ed0eb6SJeremy Kerr 
2466f5ed0eb6SJeremy Kerr 	kfree(ctx->switch_log);
2467f5ed0eb6SJeremy Kerr 	ctx->switch_log = NULL;
2468f5ed0eb6SJeremy Kerr 	spu_release(ctx);
24695158e9b5SChristoph Hellwig 
24705158e9b5SChristoph Hellwig 	return 0;
24715158e9b5SChristoph Hellwig }
24725158e9b5SChristoph Hellwig 
24735158e9b5SChristoph Hellwig static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
24745158e9b5SChristoph Hellwig {
24755158e9b5SChristoph Hellwig 	struct switch_log_entry *p;
24765158e9b5SChristoph Hellwig 
24775158e9b5SChristoph Hellwig 	p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
24785158e9b5SChristoph Hellwig 
24795158e9b5SChristoph Hellwig 	return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
24805158e9b5SChristoph Hellwig 			(unsigned int) p->tstamp.tv_sec,
24815158e9b5SChristoph Hellwig 			(unsigned int) p->tstamp.tv_nsec,
24825158e9b5SChristoph Hellwig 			p->spu_id,
24835158e9b5SChristoph Hellwig 			(unsigned int) p->type,
24845158e9b5SChristoph Hellwig 			(unsigned int) p->val,
24855158e9b5SChristoph Hellwig 			(unsigned long long) p->timebase);
24865158e9b5SChristoph Hellwig }
24875158e9b5SChristoph Hellwig 
24885158e9b5SChristoph Hellwig static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
24895158e9b5SChristoph Hellwig 			     size_t len, loff_t *ppos)
24905158e9b5SChristoph Hellwig {
24915158e9b5SChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
24925158e9b5SChristoph Hellwig 	struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
24935158e9b5SChristoph Hellwig 	int error = 0, cnt = 0;
24945158e9b5SChristoph Hellwig 
24955158e9b5SChristoph Hellwig 	if (!buf || len < 0)
24965158e9b5SChristoph Hellwig 		return -EINVAL;
24975158e9b5SChristoph Hellwig 
2498f5ed0eb6SJeremy Kerr 	error = spu_acquire(ctx);
2499f5ed0eb6SJeremy Kerr 	if (error)
2500f5ed0eb6SJeremy Kerr 		return error;
2501f5ed0eb6SJeremy Kerr 
25025158e9b5SChristoph Hellwig 	while (cnt < len) {
25035158e9b5SChristoph Hellwig 		char tbuf[128];
25045158e9b5SChristoph Hellwig 		int width;
25055158e9b5SChristoph Hellwig 
2506f5ed0eb6SJeremy Kerr 		if (spufs_switch_log_used(ctx) == 0) {
2507*14f693eeSJeremy Kerr 			if (cnt > 0) {
2508*14f693eeSJeremy Kerr 				/* If there's data ready to go, we can
2509*14f693eeSJeremy Kerr 				 * just return straight away */
2510*14f693eeSJeremy Kerr 				break;
2511*14f693eeSJeremy Kerr 
2512*14f693eeSJeremy Kerr 			} else if (file->f_flags & O_NONBLOCK) {
2513f5ed0eb6SJeremy Kerr 				error = -EAGAIN;
25145158e9b5SChristoph Hellwig 				break;
2515*14f693eeSJeremy Kerr 
2516f5ed0eb6SJeremy Kerr 			} else {
2517*14f693eeSJeremy Kerr 				/* spufs_wait will drop the mutex and
2518*14f693eeSJeremy Kerr 				 * re-acquire, but since we're in read(), the
2519*14f693eeSJeremy Kerr 				 * file cannot be _released (and so
2520*14f693eeSJeremy Kerr 				 * ctx->switch_log is stable).
2521f5ed0eb6SJeremy Kerr 				 */
2522f5ed0eb6SJeremy Kerr 				error = spufs_wait(ctx->switch_log->wait,
2523f5ed0eb6SJeremy Kerr 						spufs_switch_log_used(ctx) > 0);
25245158e9b5SChristoph Hellwig 
2525f5ed0eb6SJeremy Kerr 				/* On error, spufs_wait returns without the
2526f5ed0eb6SJeremy Kerr 				 * state mutex held */
2527f5ed0eb6SJeremy Kerr 				if (error)
2528f5ed0eb6SJeremy Kerr 					return error;
25295158e9b5SChristoph Hellwig 
2530*14f693eeSJeremy Kerr 				/* We may have had entries read from underneath
2531*14f693eeSJeremy Kerr 				 * us while we dropped the mutex in spufs_wait,
2532*14f693eeSJeremy Kerr 				 * so re-check */
2533*14f693eeSJeremy Kerr 				if (spufs_switch_log_used(ctx) == 0)
2534f5ed0eb6SJeremy Kerr 					continue;
2535*14f693eeSJeremy Kerr 			}
2536*14f693eeSJeremy Kerr 		}
2537f5ed0eb6SJeremy Kerr 
25385158e9b5SChristoph Hellwig 		width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
2539f5ed0eb6SJeremy Kerr 		if (width < len)
25405158e9b5SChristoph Hellwig 			ctx->switch_log->tail =
25415158e9b5SChristoph Hellwig 				(ctx->switch_log->tail + 1) %
25425158e9b5SChristoph Hellwig 				 SWITCH_LOG_BUFSIZE;
2543f5ed0eb6SJeremy Kerr 		else
2544f5ed0eb6SJeremy Kerr 			/* If the record is greater than space available return
2545f5ed0eb6SJeremy Kerr 			 * partial buffer (so far) */
25465158e9b5SChristoph Hellwig 			break;
25475158e9b5SChristoph Hellwig 
25485158e9b5SChristoph Hellwig 		error = copy_to_user(buf + cnt, tbuf, width);
25495158e9b5SChristoph Hellwig 		if (error)
25505158e9b5SChristoph Hellwig 			break;
25515158e9b5SChristoph Hellwig 		cnt += width;
25525158e9b5SChristoph Hellwig 	}
25535158e9b5SChristoph Hellwig 
2554f5ed0eb6SJeremy Kerr 	spu_release(ctx);
2555f5ed0eb6SJeremy Kerr 
25565158e9b5SChristoph Hellwig 	return cnt == 0 ? error : cnt;
25575158e9b5SChristoph Hellwig }
25585158e9b5SChristoph Hellwig 
25595158e9b5SChristoph Hellwig static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
25605158e9b5SChristoph Hellwig {
25615158e9b5SChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
25625158e9b5SChristoph Hellwig 	struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
25635158e9b5SChristoph Hellwig 	unsigned int mask = 0;
2564f5ed0eb6SJeremy Kerr 	int rc;
25655158e9b5SChristoph Hellwig 
25665158e9b5SChristoph Hellwig 	poll_wait(file, &ctx->switch_log->wait, wait);
25675158e9b5SChristoph Hellwig 
2568f5ed0eb6SJeremy Kerr 	rc = spu_acquire(ctx);
2569f5ed0eb6SJeremy Kerr 	if (rc)
2570f5ed0eb6SJeremy Kerr 		return rc;
2571f5ed0eb6SJeremy Kerr 
25725158e9b5SChristoph Hellwig 	if (spufs_switch_log_used(ctx) > 0)
25735158e9b5SChristoph Hellwig 		mask |= POLLIN;
25745158e9b5SChristoph Hellwig 
2575f5ed0eb6SJeremy Kerr 	spu_release(ctx);
2576f5ed0eb6SJeremy Kerr 
25775158e9b5SChristoph Hellwig 	return mask;
25785158e9b5SChristoph Hellwig }
25795158e9b5SChristoph Hellwig 
25805158e9b5SChristoph Hellwig static const struct file_operations spufs_switch_log_fops = {
25815158e9b5SChristoph Hellwig 	.owner		= THIS_MODULE,
25825158e9b5SChristoph Hellwig 	.open		= spufs_switch_log_open,
25835158e9b5SChristoph Hellwig 	.read		= spufs_switch_log_read,
25845158e9b5SChristoph Hellwig 	.poll		= spufs_switch_log_poll,
2585f5ed0eb6SJeremy Kerr 	.release	= spufs_switch_log_release,
25865158e9b5SChristoph Hellwig };
25875158e9b5SChristoph Hellwig 
2588f5ed0eb6SJeremy Kerr /**
2589f5ed0eb6SJeremy Kerr  * Log a context switch event to a switch log reader.
2590f5ed0eb6SJeremy Kerr  *
2591f5ed0eb6SJeremy Kerr  * Must be called with ctx->state_mutex held.
2592f5ed0eb6SJeremy Kerr  */
25935158e9b5SChristoph Hellwig void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
25945158e9b5SChristoph Hellwig 		u32 type, u32 val)
25955158e9b5SChristoph Hellwig {
25965158e9b5SChristoph Hellwig 	if (!ctx->switch_log)
25975158e9b5SChristoph Hellwig 		return;
25985158e9b5SChristoph Hellwig 
25995158e9b5SChristoph Hellwig 	if (spufs_switch_log_avail(ctx) > 1) {
26005158e9b5SChristoph Hellwig 		struct switch_log_entry *p;
26015158e9b5SChristoph Hellwig 
26025158e9b5SChristoph Hellwig 		p = ctx->switch_log->log + ctx->switch_log->head;
26035158e9b5SChristoph Hellwig 		ktime_get_ts(&p->tstamp);
26045158e9b5SChristoph Hellwig 		p->timebase = get_tb();
26055158e9b5SChristoph Hellwig 		p->spu_id = spu ? spu->number : -1;
26065158e9b5SChristoph Hellwig 		p->type = type;
26075158e9b5SChristoph Hellwig 		p->val = val;
26085158e9b5SChristoph Hellwig 
26095158e9b5SChristoph Hellwig 		ctx->switch_log->head =
26105158e9b5SChristoph Hellwig 			(ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
26115158e9b5SChristoph Hellwig 	}
26125158e9b5SChristoph Hellwig 
26135158e9b5SChristoph Hellwig 	wake_up(&ctx->switch_log->wait);
26145158e9b5SChristoph Hellwig }
2615e9f8a0b6SChristoph Hellwig 
261646deed69SLuke Browning static int spufs_show_ctx(struct seq_file *s, void *private)
261746deed69SLuke Browning {
261846deed69SLuke Browning 	struct spu_context *ctx = s->private;
261946deed69SLuke Browning 	u64 mfc_control_RW;
262046deed69SLuke Browning 
262146deed69SLuke Browning 	mutex_lock(&ctx->state_mutex);
262246deed69SLuke Browning 	if (ctx->spu) {
262346deed69SLuke Browning 		struct spu *spu = ctx->spu;
262446deed69SLuke Browning 		struct spu_priv2 __iomem *priv2 = spu->priv2;
262546deed69SLuke Browning 
262646deed69SLuke Browning 		spin_lock_irq(&spu->register_lock);
262746deed69SLuke Browning 		mfc_control_RW = in_be64(&priv2->mfc_control_RW);
262846deed69SLuke Browning 		spin_unlock_irq(&spu->register_lock);
262946deed69SLuke Browning 	} else {
263046deed69SLuke Browning 		struct spu_state *csa = &ctx->csa;
263146deed69SLuke Browning 
263246deed69SLuke Browning 		mfc_control_RW = csa->priv2.mfc_control_RW;
263346deed69SLuke Browning 	}
263446deed69SLuke Browning 
263546deed69SLuke Browning 	seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
263646deed69SLuke Browning 		" %c %lx %lx %lx %lx %x %x\n",
263746deed69SLuke Browning 		ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
263846deed69SLuke Browning 		ctx->flags,
263946deed69SLuke Browning 		ctx->sched_flags,
264046deed69SLuke Browning 		ctx->prio,
264146deed69SLuke Browning 		ctx->time_slice,
264246deed69SLuke Browning 		ctx->spu ? ctx->spu->number : -1,
264346deed69SLuke Browning 		!list_empty(&ctx->rq) ? 'q' : ' ',
264446deed69SLuke Browning 		ctx->csa.class_0_pending,
264546deed69SLuke Browning 		ctx->csa.class_0_dar,
264646deed69SLuke Browning 		ctx->csa.class_1_dsisr,
264746deed69SLuke Browning 		mfc_control_RW,
264846deed69SLuke Browning 		ctx->ops->runcntl_read(ctx),
264946deed69SLuke Browning 		ctx->ops->status_read(ctx));
265046deed69SLuke Browning 
265146deed69SLuke Browning 	mutex_unlock(&ctx->state_mutex);
265246deed69SLuke Browning 
265346deed69SLuke Browning 	return 0;
265446deed69SLuke Browning }
265546deed69SLuke Browning 
265646deed69SLuke Browning static int spufs_ctx_open(struct inode *inode, struct file *file)
265746deed69SLuke Browning {
265846deed69SLuke Browning 	return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
265946deed69SLuke Browning }
266046deed69SLuke Browning 
266146deed69SLuke Browning static const struct file_operations spufs_ctx_fops = {
266246deed69SLuke Browning 	.open           = spufs_ctx_open,
266346deed69SLuke Browning 	.read           = seq_read,
266446deed69SLuke Browning 	.llseek         = seq_lseek,
266546deed69SLuke Browning 	.release        = single_release,
266646deed69SLuke Browning };
266746deed69SLuke Browning 
266823d893f5SJeremy Kerr struct spufs_tree_descr spufs_dir_contents[] = {
2669cbe709c1SBenjamin Herrenschmidt 	{ "capabilities", &spufs_caps_fops, 0444, },
26706f7dde81SJeremy Kerr 	{ "mem",  &spufs_mem_fops,  0666, LS_SIZE, },
26716f7dde81SJeremy Kerr 	{ "regs", &spufs_regs_fops,  0666, sizeof(struct spu_reg128[128]), },
267267207b96SArnd Bergmann 	{ "mbox", &spufs_mbox_fops, 0444, },
267367207b96SArnd Bergmann 	{ "ibox", &spufs_ibox_fops, 0444, },
267467207b96SArnd Bergmann 	{ "wbox", &spufs_wbox_fops, 0222, },
26756f7dde81SJeremy Kerr 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
26766f7dde81SJeremy Kerr 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
26776f7dde81SJeremy Kerr 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2678603c4612SJeremy Kerr 	{ "signal1", &spufs_signal1_fops, 0666, },
2679603c4612SJeremy Kerr 	{ "signal2", &spufs_signal2_fops, 0666, },
268067207b96SArnd Bergmann 	{ "signal1_type", &spufs_signal1_type, 0666, },
268167207b96SArnd Bergmann 	{ "signal2_type", &spufs_signal2_type, 0666, },
26826df10a82SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
26836f7dde81SJeremy Kerr 	{ "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
2684b9e3bd77SDwayne Grant McConnell 	{ "lslr", &spufs_lslr_ops, 0444, },
2685b9e3bd77SDwayne Grant McConnell 	{ "mfc", &spufs_mfc_fops, 0666, },
2686b9e3bd77SDwayne Grant McConnell 	{ "mss", &spufs_mss_fops, 0666, },
2687b9e3bd77SDwayne Grant McConnell 	{ "npc", &spufs_npc_ops, 0666, },
2688b9e3bd77SDwayne Grant McConnell 	{ "srr0", &spufs_srr0_ops, 0666, },
26898b3d6663SArnd Bergmann 	{ "decr", &spufs_decr_ops, 0666, },
26908b3d6663SArnd Bergmann 	{ "decr_status", &spufs_decr_status_ops, 0666, },
26918b3d6663SArnd Bergmann 	{ "event_mask", &spufs_event_mask_ops, 0666, },
2692b9e3bd77SDwayne Grant McConnell 	{ "event_status", &spufs_event_status_ops, 0444, },
26936f7dde81SJeremy Kerr 	{ "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
269486767277SArnd Bergmann 	{ "phys-id", &spufs_id_ops, 0666, },
269586767277SArnd Bergmann 	{ "object-id", &spufs_object_id_ops, 0666, },
26966f7dde81SJeremy Kerr 	{ "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
26976f7dde81SJeremy Kerr 	{ "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
26986f7dde81SJeremy Kerr 	{ "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
26996f7dde81SJeremy Kerr 	{ "dma_info", &spufs_dma_info_fops, 0444,
27006f7dde81SJeremy Kerr 		sizeof(struct spu_dma_info), },
27016f7dde81SJeremy Kerr 	{ "proxydma_info", &spufs_proxydma_info_fops, 0444,
27026f7dde81SJeremy Kerr 		sizeof(struct spu_proxydma_info)},
2703476273adSChristoph Hellwig 	{ "tid", &spufs_tid_fops, 0444, },
2704e9f8a0b6SChristoph Hellwig 	{ "stat", &spufs_stat_fops, 0444, },
27055158e9b5SChristoph Hellwig 	{ "switch_log", &spufs_switch_log_fops, 0444 },
270667207b96SArnd Bergmann 	{},
270767207b96SArnd Bergmann };
27085737edd1SMark Nutter 
270923d893f5SJeremy Kerr struct spufs_tree_descr spufs_dir_nosched_contents[] = {
2710cbe709c1SBenjamin Herrenschmidt 	{ "capabilities", &spufs_caps_fops, 0444, },
27116f7dde81SJeremy Kerr 	{ "mem",  &spufs_mem_fops,  0666, LS_SIZE, },
27125737edd1SMark Nutter 	{ "mbox", &spufs_mbox_fops, 0444, },
27135737edd1SMark Nutter 	{ "ibox", &spufs_ibox_fops, 0444, },
27145737edd1SMark Nutter 	{ "wbox", &spufs_wbox_fops, 0222, },
27156f7dde81SJeremy Kerr 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
27166f7dde81SJeremy Kerr 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
27176f7dde81SJeremy Kerr 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2718d054b36fSJeremy Kerr 	{ "signal1", &spufs_signal1_nosched_fops, 0222, },
2719d054b36fSJeremy Kerr 	{ "signal2", &spufs_signal2_nosched_fops, 0222, },
27205737edd1SMark Nutter 	{ "signal1_type", &spufs_signal1_type, 0666, },
27215737edd1SMark Nutter 	{ "signal2_type", &spufs_signal2_type, 0666, },
27225737edd1SMark Nutter 	{ "mss", &spufs_mss_fops, 0666, },
27235737edd1SMark Nutter 	{ "mfc", &spufs_mfc_fops, 0666, },
27245737edd1SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
27255737edd1SMark Nutter 	{ "npc", &spufs_npc_ops, 0666, },
27266f7dde81SJeremy Kerr 	{ "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
27275737edd1SMark Nutter 	{ "phys-id", &spufs_id_ops, 0666, },
27285737edd1SMark Nutter 	{ "object-id", &spufs_object_id_ops, 0666, },
2729476273adSChristoph Hellwig 	{ "tid", &spufs_tid_fops, 0444, },
2730e9f8a0b6SChristoph Hellwig 	{ "stat", &spufs_stat_fops, 0444, },
27312c3e4787SJeremy Kerr 	{},
27322c3e4787SJeremy Kerr };
27332c3e4787SJeremy Kerr 
27342c3e4787SJeremy Kerr struct spufs_tree_descr spufs_dir_debug_contents[] = {
273546deed69SLuke Browning 	{ ".ctx", &spufs_ctx_fops, 0444, },
27365737edd1SMark Nutter 	{},
27375737edd1SMark Nutter };
2738bf1ab978SDwayne Grant McConnell 
2739bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader spufs_coredump_read[] = {
27404fca9c42SMichael Ellerman 	{ "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
27414fca9c42SMichael Ellerman 	{ "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2742104f0cc2SMichael Ellerman 	{ "lslr", NULL, spufs_lslr_get, 19 },
2743104f0cc2SMichael Ellerman 	{ "decr", NULL, spufs_decr_get, 19 },
2744104f0cc2SMichael Ellerman 	{ "decr_status", NULL, spufs_decr_status_get, 19 },
27454fca9c42SMichael Ellerman 	{ "mem", __spufs_mem_read, NULL, LS_SIZE, },
27464fca9c42SMichael Ellerman 	{ "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2747104f0cc2SMichael Ellerman 	{ "signal1_type", NULL, spufs_signal1_type_get, 19 },
27484fca9c42SMichael Ellerman 	{ "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2749104f0cc2SMichael Ellerman 	{ "signal2_type", NULL, spufs_signal2_type_get, 19 },
2750104f0cc2SMichael Ellerman 	{ "event_mask", NULL, spufs_event_mask_get, 19 },
2751104f0cc2SMichael Ellerman 	{ "event_status", NULL, spufs_event_status_get, 19 },
27524fca9c42SMichael Ellerman 	{ "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
27534fca9c42SMichael Ellerman 	{ "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
27544fca9c42SMichael Ellerman 	{ "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
27554fca9c42SMichael Ellerman 	{ "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
27564fca9c42SMichael Ellerman 	{ "proxydma_info", __spufs_proxydma_info_read,
27574fca9c42SMichael Ellerman 			   NULL, sizeof(struct spu_proxydma_info)},
2758104f0cc2SMichael Ellerman 	{ "object-id", NULL, spufs_object_id_get, 19 },
2759104f0cc2SMichael Ellerman 	{ "npc", NULL, spufs_npc_get, 19 },
2760936d5bf1SMichael Ellerman 	{ NULL },
2761bf1ab978SDwayne Grant McConnell };
2762