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 
551f027faa2SJeremy Kerr 	/* pre-check for file position: if we'd return EOF, there's no point
552f027faa2SJeremy Kerr 	 * causing a deschedule */
553f027faa2SJeremy Kerr 	if (*pos >= sizeof(ctx->csa.lscsa->gprs))
554f027faa2SJeremy Kerr 		return 0;
555f027faa2SJeremy Kerr 
556c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
557c9101bdbSChristoph Hellwig 	if (ret)
558c9101bdbSChristoph Hellwig 		return ret;
559bf1ab978SDwayne Grant McConnell 	ret = __spufs_regs_read(ctx, buffer, size, pos);
56027b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
5618b3d6663SArnd Bergmann 	return ret;
5628b3d6663SArnd Bergmann }
5638b3d6663SArnd Bergmann 
5648b3d6663SArnd Bergmann static ssize_t
5658b3d6663SArnd Bergmann spufs_regs_write(struct file *file, const char __user *buffer,
5668b3d6663SArnd Bergmann 		 size_t size, loff_t *pos)
5678b3d6663SArnd Bergmann {
5688b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
5698b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
5708b3d6663SArnd Bergmann 	int ret;
5718b3d6663SArnd Bergmann 
5728b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size);
5738b3d6663SArnd Bergmann 	if (size <= 0)
5748b3d6663SArnd Bergmann 		return -EFBIG;
5758b3d6663SArnd Bergmann 	*pos += size;
5768b3d6663SArnd Bergmann 
577c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
578c9101bdbSChristoph Hellwig 	if (ret)
579c9101bdbSChristoph Hellwig 		return ret;
5808b3d6663SArnd Bergmann 
5818b3d6663SArnd Bergmann 	ret = copy_from_user(lscsa->gprs + *pos - size,
5828b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
5838b3d6663SArnd Bergmann 
58427b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
5858b3d6663SArnd Bergmann 	return ret;
5868b3d6663SArnd Bergmann }
5878b3d6663SArnd Bergmann 
5885dfe4c96SArjan van de Ven static const struct file_operations spufs_regs_fops = {
5898b3d6663SArnd Bergmann 	.open	 = spufs_regs_open,
5908b3d6663SArnd Bergmann 	.read    = spufs_regs_read,
5918b3d6663SArnd Bergmann 	.write   = spufs_regs_write,
5928b3d6663SArnd Bergmann 	.llseek  = generic_file_llseek,
5938b3d6663SArnd Bergmann };
5948b3d6663SArnd Bergmann 
5958b3d6663SArnd Bergmann static ssize_t
596bf1ab978SDwayne Grant McConnell __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
597bf1ab978SDwayne Grant McConnell 			size_t size, loff_t * pos)
598bf1ab978SDwayne Grant McConnell {
599bf1ab978SDwayne Grant McConnell 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
600bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buffer, size, pos,
601bf1ab978SDwayne Grant McConnell 				      &lscsa->fpcr, sizeof(lscsa->fpcr));
602bf1ab978SDwayne Grant McConnell }
603bf1ab978SDwayne Grant McConnell 
604bf1ab978SDwayne Grant McConnell static ssize_t
6058b3d6663SArnd Bergmann spufs_fpcr_read(struct file *file, char __user * buffer,
6068b3d6663SArnd Bergmann 		size_t size, loff_t * pos)
6078b3d6663SArnd Bergmann {
6088b3d6663SArnd Bergmann 	int ret;
609bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
6108b3d6663SArnd Bergmann 
611c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
612c9101bdbSChristoph Hellwig 	if (ret)
613c9101bdbSChristoph Hellwig 		return ret;
614bf1ab978SDwayne Grant McConnell 	ret = __spufs_fpcr_read(ctx, buffer, size, pos);
61527b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
6168b3d6663SArnd Bergmann 	return ret;
6178b3d6663SArnd Bergmann }
6188b3d6663SArnd Bergmann 
6198b3d6663SArnd Bergmann static ssize_t
6208b3d6663SArnd Bergmann spufs_fpcr_write(struct file *file, const char __user * buffer,
6218b3d6663SArnd Bergmann 		 size_t size, loff_t * pos)
6228b3d6663SArnd Bergmann {
6238b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
6248b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
6258b3d6663SArnd Bergmann 	int ret;
6268b3d6663SArnd Bergmann 
6278b3d6663SArnd Bergmann 	size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size);
6288b3d6663SArnd Bergmann 	if (size <= 0)
6298b3d6663SArnd Bergmann 		return -EFBIG;
630c9101bdbSChristoph Hellwig 
631c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
632c9101bdbSChristoph Hellwig 	if (ret)
633c9101bdbSChristoph Hellwig 		return ret;
634c9101bdbSChristoph Hellwig 
6358b3d6663SArnd Bergmann 	*pos += size;
6368b3d6663SArnd Bergmann 	ret = copy_from_user((char *)&lscsa->fpcr + *pos - size,
6378b3d6663SArnd Bergmann 			     buffer, size) ? -EFAULT : size;
6388b3d6663SArnd Bergmann 
63927b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
6408b3d6663SArnd Bergmann 	return ret;
6418b3d6663SArnd Bergmann }
6428b3d6663SArnd Bergmann 
6435dfe4c96SArjan van de Ven static const struct file_operations spufs_fpcr_fops = {
6448b3d6663SArnd Bergmann 	.open = spufs_regs_open,
6458b3d6663SArnd Bergmann 	.read = spufs_fpcr_read,
6468b3d6663SArnd Bergmann 	.write = spufs_fpcr_write,
64767207b96SArnd Bergmann 	.llseek = generic_file_llseek,
64867207b96SArnd Bergmann };
64967207b96SArnd Bergmann 
65067207b96SArnd Bergmann /* generic open function for all pipe-like files */
65167207b96SArnd Bergmann static int spufs_pipe_open(struct inode *inode, struct file *file)
65267207b96SArnd Bergmann {
65367207b96SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
65467207b96SArnd Bergmann 	file->private_data = i->i_ctx;
65567207b96SArnd Bergmann 
65667207b96SArnd Bergmann 	return nonseekable_open(inode, file);
65767207b96SArnd Bergmann }
65867207b96SArnd Bergmann 
659cdcc89bbSArnd Bergmann /*
660cdcc89bbSArnd Bergmann  * Read as many bytes from the mailbox as possible, until
661cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
662cdcc89bbSArnd Bergmann  *
663cdcc89bbSArnd Bergmann  * - no more data available in the mailbox
664cdcc89bbSArnd Bergmann  * - end of the user provided buffer
665cdcc89bbSArnd Bergmann  * - end of the mapped area
666cdcc89bbSArnd Bergmann  */
66767207b96SArnd Bergmann static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
66867207b96SArnd Bergmann 			size_t len, loff_t *pos)
66967207b96SArnd Bergmann {
6708b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
671cdcc89bbSArnd Bergmann 	u32 mbox_data, __user *udata;
672cdcc89bbSArnd Bergmann 	ssize_t count;
67367207b96SArnd Bergmann 
67467207b96SArnd Bergmann 	if (len < 4)
67567207b96SArnd Bergmann 		return -EINVAL;
67667207b96SArnd Bergmann 
677cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_WRITE, buf, len))
67867207b96SArnd Bergmann 		return -EFAULT;
67967207b96SArnd Bergmann 
680cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
681cdcc89bbSArnd Bergmann 
682c9101bdbSChristoph Hellwig 	count = spu_acquire(ctx);
683c9101bdbSChristoph Hellwig 	if (count)
684c9101bdbSChristoph Hellwig 		return count;
685c9101bdbSChristoph Hellwig 
686274cef5eSArnd Bergmann 	for (count = 0; (count + 4) <= len; count += 4, udata++) {
687cdcc89bbSArnd Bergmann 		int ret;
688cdcc89bbSArnd Bergmann 		ret = ctx->ops->mbox_read(ctx, &mbox_data);
689cdcc89bbSArnd Bergmann 		if (ret == 0)
690cdcc89bbSArnd Bergmann 			break;
691cdcc89bbSArnd Bergmann 
692cdcc89bbSArnd Bergmann 		/*
693cdcc89bbSArnd Bergmann 		 * at the end of the mapped area, we can fault
694cdcc89bbSArnd Bergmann 		 * but still need to return the data we have
695cdcc89bbSArnd Bergmann 		 * read successfully so far.
696cdcc89bbSArnd Bergmann 		 */
697cdcc89bbSArnd Bergmann 		ret = __put_user(mbox_data, udata);
698cdcc89bbSArnd Bergmann 		if (ret) {
699cdcc89bbSArnd Bergmann 			if (!count)
700cdcc89bbSArnd Bergmann 				count = -EFAULT;
701cdcc89bbSArnd Bergmann 			break;
702cdcc89bbSArnd Bergmann 		}
703cdcc89bbSArnd Bergmann 	}
704cdcc89bbSArnd Bergmann 	spu_release(ctx);
705cdcc89bbSArnd Bergmann 
706cdcc89bbSArnd Bergmann 	if (!count)
707cdcc89bbSArnd Bergmann 		count = -EAGAIN;
708cdcc89bbSArnd Bergmann 
709cdcc89bbSArnd Bergmann 	return count;
71067207b96SArnd Bergmann }
71167207b96SArnd Bergmann 
7125dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_fops = {
71367207b96SArnd Bergmann 	.open	= spufs_pipe_open,
71467207b96SArnd Bergmann 	.read	= spufs_mbox_read,
71567207b96SArnd Bergmann };
71667207b96SArnd Bergmann 
71767207b96SArnd Bergmann static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
71867207b96SArnd Bergmann 			size_t len, loff_t *pos)
71967207b96SArnd Bergmann {
7208b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
721c9101bdbSChristoph Hellwig 	ssize_t ret;
72267207b96SArnd Bergmann 	u32 mbox_stat;
72367207b96SArnd Bergmann 
72467207b96SArnd Bergmann 	if (len < 4)
72567207b96SArnd Bergmann 		return -EINVAL;
72667207b96SArnd Bergmann 
727c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
728c9101bdbSChristoph Hellwig 	if (ret)
729c9101bdbSChristoph Hellwig 		return ret;
7308b3d6663SArnd Bergmann 
7318b3d6663SArnd Bergmann 	mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
7328b3d6663SArnd Bergmann 
7338b3d6663SArnd Bergmann 	spu_release(ctx);
73467207b96SArnd Bergmann 
73567207b96SArnd Bergmann 	if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
73667207b96SArnd Bergmann 		return -EFAULT;
73767207b96SArnd Bergmann 
73867207b96SArnd Bergmann 	return 4;
73967207b96SArnd Bergmann }
74067207b96SArnd Bergmann 
7415dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_stat_fops = {
74267207b96SArnd Bergmann 	.open	= spufs_pipe_open,
74367207b96SArnd Bergmann 	.read	= spufs_mbox_stat_read,
74467207b96SArnd Bergmann };
74567207b96SArnd Bergmann 
74667207b96SArnd Bergmann /* low-level ibox access function */
7478b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
74867207b96SArnd Bergmann {
7498b3d6663SArnd Bergmann 	return ctx->ops->ibox_read(ctx, data);
75067207b96SArnd Bergmann }
75167207b96SArnd Bergmann 
75267207b96SArnd Bergmann static int spufs_ibox_fasync(int fd, struct file *file, int on)
75367207b96SArnd Bergmann {
7548b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
7558b3d6663SArnd Bergmann 
7568b3d6663SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->ibox_fasync);
7578b3d6663SArnd Bergmann }
7588b3d6663SArnd Bergmann 
7598b3d6663SArnd Bergmann /* interrupt-level ibox callback function. */
7608b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu)
7618b3d6663SArnd Bergmann {
7628b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
7638b3d6663SArnd Bergmann 
764e65c2f6fSLuke Browning 	if (!ctx)
765e65c2f6fSLuke Browning 		return;
766e65c2f6fSLuke Browning 
7678b3d6663SArnd Bergmann 	wake_up_all(&ctx->ibox_wq);
7688b3d6663SArnd Bergmann 	kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
76967207b96SArnd Bergmann }
77067207b96SArnd Bergmann 
771cdcc89bbSArnd Bergmann /*
772cdcc89bbSArnd Bergmann  * Read as many bytes from the interrupt mailbox as possible, until
773cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
774cdcc89bbSArnd Bergmann  *
775cdcc89bbSArnd Bergmann  * - no more data available in the mailbox
776cdcc89bbSArnd Bergmann  * - end of the user provided buffer
777cdcc89bbSArnd Bergmann  * - end of the mapped area
778cdcc89bbSArnd Bergmann  *
779cdcc89bbSArnd Bergmann  * If the file is opened without O_NONBLOCK, we wait here until
780cdcc89bbSArnd Bergmann  * any data is available, but return when we have been able to
781cdcc89bbSArnd Bergmann  * read something.
782cdcc89bbSArnd Bergmann  */
78367207b96SArnd Bergmann static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
78467207b96SArnd Bergmann 			size_t len, loff_t *pos)
78567207b96SArnd Bergmann {
7868b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
787cdcc89bbSArnd Bergmann 	u32 ibox_data, __user *udata;
788cdcc89bbSArnd Bergmann 	ssize_t count;
78967207b96SArnd Bergmann 
79067207b96SArnd Bergmann 	if (len < 4)
79167207b96SArnd Bergmann 		return -EINVAL;
79267207b96SArnd Bergmann 
793cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_WRITE, buf, len))
794cdcc89bbSArnd Bergmann 		return -EFAULT;
795cdcc89bbSArnd Bergmann 
796cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
797cdcc89bbSArnd Bergmann 
798c9101bdbSChristoph Hellwig 	count = spu_acquire(ctx);
799c9101bdbSChristoph Hellwig 	if (count)
800eebead5bSChristoph Hellwig 		goto out;
80167207b96SArnd Bergmann 
802cdcc89bbSArnd Bergmann 	/* wait only for the first element */
803cdcc89bbSArnd Bergmann 	count = 0;
80467207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
805eebead5bSChristoph Hellwig 		if (!spu_ibox_read(ctx, &ibox_data)) {
806cdcc89bbSArnd Bergmann 			count = -EAGAIN;
807eebead5bSChristoph Hellwig 			goto out_unlock;
808eebead5bSChristoph Hellwig 		}
80967207b96SArnd Bergmann 	} else {
810cdcc89bbSArnd Bergmann 		count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
811cdcc89bbSArnd Bergmann 		if (count)
812cdcc89bbSArnd Bergmann 			goto out;
813eebead5bSChristoph Hellwig 	}
814cdcc89bbSArnd Bergmann 
815cdcc89bbSArnd Bergmann 	/* if we can't write at all, return -EFAULT */
816cdcc89bbSArnd Bergmann 	count = __put_user(ibox_data, udata);
817cdcc89bbSArnd Bergmann 	if (count)
818eebead5bSChristoph Hellwig 		goto out_unlock;
819cdcc89bbSArnd Bergmann 
820cdcc89bbSArnd Bergmann 	for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
821cdcc89bbSArnd Bergmann 		int ret;
822cdcc89bbSArnd Bergmann 		ret = ctx->ops->ibox_read(ctx, &ibox_data);
823cdcc89bbSArnd Bergmann 		if (ret == 0)
824cdcc89bbSArnd Bergmann 			break;
825cdcc89bbSArnd Bergmann 		/*
826cdcc89bbSArnd Bergmann 		 * at the end of the mapped area, we can fault
827cdcc89bbSArnd Bergmann 		 * but still need to return the data we have
828cdcc89bbSArnd Bergmann 		 * read successfully so far.
829cdcc89bbSArnd Bergmann 		 */
830cdcc89bbSArnd Bergmann 		ret = __put_user(ibox_data, udata);
831cdcc89bbSArnd Bergmann 		if (ret)
832cdcc89bbSArnd Bergmann 			break;
83367207b96SArnd Bergmann 	}
83467207b96SArnd Bergmann 
835eebead5bSChristoph Hellwig out_unlock:
8368b3d6663SArnd Bergmann 	spu_release(ctx);
837eebead5bSChristoph Hellwig out:
838cdcc89bbSArnd Bergmann 	return count;
83967207b96SArnd Bergmann }
84067207b96SArnd Bergmann 
84167207b96SArnd Bergmann static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
84267207b96SArnd Bergmann {
8438b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
84467207b96SArnd Bergmann 	unsigned int mask;
84567207b96SArnd Bergmann 
8468b3d6663SArnd Bergmann 	poll_wait(file, &ctx->ibox_wq, wait);
84767207b96SArnd Bergmann 
848c9101bdbSChristoph Hellwig 	/*
849c9101bdbSChristoph Hellwig 	 * For now keep this uninterruptible and also ignore the rule
850c9101bdbSChristoph Hellwig 	 * that poll should not sleep.  Will be fixed later.
851c9101bdbSChristoph Hellwig 	 */
852c9101bdbSChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
8533a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
8543a843d7cSArnd Bergmann 	spu_release(ctx);
85567207b96SArnd Bergmann 
85667207b96SArnd Bergmann 	return mask;
85767207b96SArnd Bergmann }
85867207b96SArnd Bergmann 
8595dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_fops = {
86067207b96SArnd Bergmann 	.open	= spufs_pipe_open,
86167207b96SArnd Bergmann 	.read	= spufs_ibox_read,
86267207b96SArnd Bergmann 	.poll	= spufs_ibox_poll,
86367207b96SArnd Bergmann 	.fasync	= spufs_ibox_fasync,
86467207b96SArnd Bergmann };
86567207b96SArnd Bergmann 
86667207b96SArnd Bergmann static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
86767207b96SArnd Bergmann 			size_t len, loff_t *pos)
86867207b96SArnd Bergmann {
8698b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
870c9101bdbSChristoph Hellwig 	ssize_t ret;
87167207b96SArnd Bergmann 	u32 ibox_stat;
87267207b96SArnd Bergmann 
87367207b96SArnd Bergmann 	if (len < 4)
87467207b96SArnd Bergmann 		return -EINVAL;
87567207b96SArnd Bergmann 
876c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
877c9101bdbSChristoph Hellwig 	if (ret)
878c9101bdbSChristoph Hellwig 		return ret;
8798b3d6663SArnd Bergmann 	ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
8808b3d6663SArnd Bergmann 	spu_release(ctx);
88167207b96SArnd Bergmann 
88267207b96SArnd Bergmann 	if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
88367207b96SArnd Bergmann 		return -EFAULT;
88467207b96SArnd Bergmann 
88567207b96SArnd Bergmann 	return 4;
88667207b96SArnd Bergmann }
88767207b96SArnd Bergmann 
8885dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_stat_fops = {
88967207b96SArnd Bergmann 	.open	= spufs_pipe_open,
89067207b96SArnd Bergmann 	.read	= spufs_ibox_stat_read,
89167207b96SArnd Bergmann };
89267207b96SArnd Bergmann 
89367207b96SArnd Bergmann /* low-level mailbox write */
8948b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data)
89567207b96SArnd Bergmann {
8968b3d6663SArnd Bergmann 	return ctx->ops->wbox_write(ctx, data);
89767207b96SArnd Bergmann }
89867207b96SArnd Bergmann 
89967207b96SArnd Bergmann static int spufs_wbox_fasync(int fd, struct file *file, int on)
90067207b96SArnd Bergmann {
9018b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
9028b3d6663SArnd Bergmann 	int ret;
9038b3d6663SArnd Bergmann 
9048b3d6663SArnd Bergmann 	ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
9058b3d6663SArnd Bergmann 
9068b3d6663SArnd Bergmann 	return ret;
9078b3d6663SArnd Bergmann }
9088b3d6663SArnd Bergmann 
9098b3d6663SArnd Bergmann /* interrupt-level wbox callback function. */
9108b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu)
9118b3d6663SArnd Bergmann {
9128b3d6663SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
9138b3d6663SArnd Bergmann 
914e65c2f6fSLuke Browning 	if (!ctx)
915e65c2f6fSLuke Browning 		return;
916e65c2f6fSLuke Browning 
9178b3d6663SArnd Bergmann 	wake_up_all(&ctx->wbox_wq);
9188b3d6663SArnd Bergmann 	kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
91967207b96SArnd Bergmann }
92067207b96SArnd Bergmann 
921cdcc89bbSArnd Bergmann /*
922cdcc89bbSArnd Bergmann  * Write as many bytes to the interrupt mailbox as possible, until
923cdcc89bbSArnd Bergmann  * one of the conditions becomes true:
924cdcc89bbSArnd Bergmann  *
925cdcc89bbSArnd Bergmann  * - the mailbox is full
926cdcc89bbSArnd Bergmann  * - end of the user provided buffer
927cdcc89bbSArnd Bergmann  * - end of the mapped area
928cdcc89bbSArnd Bergmann  *
929cdcc89bbSArnd Bergmann  * If the file is opened without O_NONBLOCK, we wait here until
930cdcc89bbSArnd Bergmann  * space is availabyl, but return when we have been able to
931cdcc89bbSArnd Bergmann  * write something.
932cdcc89bbSArnd Bergmann  */
93367207b96SArnd Bergmann static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
93467207b96SArnd Bergmann 			size_t len, loff_t *pos)
93567207b96SArnd Bergmann {
9368b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
937cdcc89bbSArnd Bergmann 	u32 wbox_data, __user *udata;
938cdcc89bbSArnd Bergmann 	ssize_t count;
93967207b96SArnd Bergmann 
94067207b96SArnd Bergmann 	if (len < 4)
94167207b96SArnd Bergmann 		return -EINVAL;
94267207b96SArnd Bergmann 
943cdcc89bbSArnd Bergmann 	udata = (void __user *)buf;
944cdcc89bbSArnd Bergmann 	if (!access_ok(VERIFY_READ, buf, len))
945cdcc89bbSArnd Bergmann 		return -EFAULT;
946cdcc89bbSArnd Bergmann 
947cdcc89bbSArnd Bergmann 	if (__get_user(wbox_data, udata))
94867207b96SArnd Bergmann 		return -EFAULT;
94967207b96SArnd Bergmann 
950c9101bdbSChristoph Hellwig 	count = spu_acquire(ctx);
951c9101bdbSChristoph Hellwig 	if (count)
952eebead5bSChristoph Hellwig 		goto out;
9538b3d6663SArnd Bergmann 
954cdcc89bbSArnd Bergmann 	/*
955cdcc89bbSArnd Bergmann 	 * make sure we can at least write one element, by waiting
956cdcc89bbSArnd Bergmann 	 * in case of !O_NONBLOCK
957cdcc89bbSArnd Bergmann 	 */
958cdcc89bbSArnd Bergmann 	count = 0;
95967207b96SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
960eebead5bSChristoph Hellwig 		if (!spu_wbox_write(ctx, wbox_data)) {
961cdcc89bbSArnd Bergmann 			count = -EAGAIN;
962eebead5bSChristoph Hellwig 			goto out_unlock;
963eebead5bSChristoph Hellwig 		}
96467207b96SArnd Bergmann 	} else {
965cdcc89bbSArnd Bergmann 		count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
966cdcc89bbSArnd Bergmann 		if (count)
967cdcc89bbSArnd Bergmann 			goto out;
968eebead5bSChristoph Hellwig 	}
969eebead5bSChristoph Hellwig 
9708b3d6663SArnd Bergmann 
97196de0e25SJan Engelhardt 	/* write as much as possible */
972cdcc89bbSArnd Bergmann 	for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
973cdcc89bbSArnd Bergmann 		int ret;
974cdcc89bbSArnd Bergmann 		ret = __get_user(wbox_data, udata);
975cdcc89bbSArnd Bergmann 		if (ret)
976cdcc89bbSArnd Bergmann 			break;
977cdcc89bbSArnd Bergmann 
978cdcc89bbSArnd Bergmann 		ret = spu_wbox_write(ctx, wbox_data);
979cdcc89bbSArnd Bergmann 		if (ret == 0)
980cdcc89bbSArnd Bergmann 			break;
981cdcc89bbSArnd Bergmann 	}
982cdcc89bbSArnd Bergmann 
983eebead5bSChristoph Hellwig out_unlock:
984cdcc89bbSArnd Bergmann 	spu_release(ctx);
985eebead5bSChristoph Hellwig out:
986cdcc89bbSArnd Bergmann 	return count;
98767207b96SArnd Bergmann }
98867207b96SArnd Bergmann 
98967207b96SArnd Bergmann static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
99067207b96SArnd Bergmann {
9918b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
99267207b96SArnd Bergmann 	unsigned int mask;
99367207b96SArnd Bergmann 
9948b3d6663SArnd Bergmann 	poll_wait(file, &ctx->wbox_wq, wait);
99567207b96SArnd Bergmann 
996c9101bdbSChristoph Hellwig 	/*
997c9101bdbSChristoph Hellwig 	 * For now keep this uninterruptible and also ignore the rule
998c9101bdbSChristoph Hellwig 	 * that poll should not sleep.  Will be fixed later.
999c9101bdbSChristoph Hellwig 	 */
1000c9101bdbSChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
10013a843d7cSArnd Bergmann 	mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
10023a843d7cSArnd Bergmann 	spu_release(ctx);
100367207b96SArnd Bergmann 
100467207b96SArnd Bergmann 	return mask;
100567207b96SArnd Bergmann }
100667207b96SArnd Bergmann 
10075dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_fops = {
100867207b96SArnd Bergmann 	.open	= spufs_pipe_open,
100967207b96SArnd Bergmann 	.write	= spufs_wbox_write,
101067207b96SArnd Bergmann 	.poll	= spufs_wbox_poll,
101167207b96SArnd Bergmann 	.fasync	= spufs_wbox_fasync,
101267207b96SArnd Bergmann };
101367207b96SArnd Bergmann 
101467207b96SArnd Bergmann static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
101567207b96SArnd Bergmann 			size_t len, loff_t *pos)
101667207b96SArnd Bergmann {
10178b3d6663SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1018c9101bdbSChristoph Hellwig 	ssize_t ret;
101967207b96SArnd Bergmann 	u32 wbox_stat;
102067207b96SArnd Bergmann 
102167207b96SArnd Bergmann 	if (len < 4)
102267207b96SArnd Bergmann 		return -EINVAL;
102367207b96SArnd Bergmann 
1024c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1025c9101bdbSChristoph Hellwig 	if (ret)
1026c9101bdbSChristoph Hellwig 		return ret;
10278b3d6663SArnd Bergmann 	wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
10288b3d6663SArnd Bergmann 	spu_release(ctx);
102967207b96SArnd Bergmann 
103067207b96SArnd Bergmann 	if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
103167207b96SArnd Bergmann 		return -EFAULT;
103267207b96SArnd Bergmann 
103367207b96SArnd Bergmann 	return 4;
103467207b96SArnd Bergmann }
103567207b96SArnd Bergmann 
10365dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_stat_fops = {
103767207b96SArnd Bergmann 	.open	= spufs_pipe_open,
103867207b96SArnd Bergmann 	.read	= spufs_wbox_stat_read,
103967207b96SArnd Bergmann };
104067207b96SArnd Bergmann 
10416df10a82SMark Nutter static int spufs_signal1_open(struct inode *inode, struct file *file)
10426df10a82SMark Nutter {
10436df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
10446df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
104543c2bbd9SChristoph Hellwig 
104647d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
10476df10a82SMark Nutter 	file->private_data = ctx;
104843c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
10496df10a82SMark Nutter 		ctx->signal1 = inode->i_mapping;
105047d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
10516df10a82SMark Nutter 	return nonseekable_open(inode, file);
10526df10a82SMark Nutter }
10536df10a82SMark Nutter 
105443c2bbd9SChristoph Hellwig static int
105543c2bbd9SChristoph Hellwig spufs_signal1_release(struct inode *inode, struct file *file)
105643c2bbd9SChristoph Hellwig {
105743c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
105843c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
105943c2bbd9SChristoph Hellwig 
106047d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
106143c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
106243c2bbd9SChristoph Hellwig 		ctx->signal1 = NULL;
106347d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
106443c2bbd9SChristoph Hellwig 	return 0;
106543c2bbd9SChristoph Hellwig }
106643c2bbd9SChristoph Hellwig 
1067bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
106867207b96SArnd Bergmann 			size_t len, loff_t *pos)
106967207b96SArnd Bergmann {
107017f88cebSDwayne Grant McConnell 	int ret = 0;
107167207b96SArnd Bergmann 	u32 data;
107267207b96SArnd Bergmann 
107367207b96SArnd Bergmann 	if (len < 4)
107467207b96SArnd Bergmann 		return -EINVAL;
107567207b96SArnd Bergmann 
107617f88cebSDwayne Grant McConnell 	if (ctx->csa.spu_chnlcnt_RW[3]) {
107717f88cebSDwayne Grant McConnell 		data = ctx->csa.spu_chnldata_RW[3];
107817f88cebSDwayne Grant McConnell 		ret = 4;
107917f88cebSDwayne Grant McConnell 	}
10808b3d6663SArnd Bergmann 
108117f88cebSDwayne Grant McConnell 	if (!ret)
108217f88cebSDwayne Grant McConnell 		goto out;
108317f88cebSDwayne Grant McConnell 
108467207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
108567207b96SArnd Bergmann 		return -EFAULT;
108667207b96SArnd Bergmann 
108717f88cebSDwayne Grant McConnell out:
108817f88cebSDwayne Grant McConnell 	return ret;
108967207b96SArnd Bergmann }
109067207b96SArnd Bergmann 
1091bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1092bf1ab978SDwayne Grant McConnell 			size_t len, loff_t *pos)
1093bf1ab978SDwayne Grant McConnell {
1094bf1ab978SDwayne Grant McConnell 	int ret;
1095bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1096bf1ab978SDwayne Grant McConnell 
1097c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1098c9101bdbSChristoph Hellwig 	if (ret)
1099c9101bdbSChristoph Hellwig 		return ret;
1100bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal1_read(ctx, buf, len, pos);
110127b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1102bf1ab978SDwayne Grant McConnell 
1103bf1ab978SDwayne Grant McConnell 	return ret;
1104bf1ab978SDwayne Grant McConnell }
1105bf1ab978SDwayne Grant McConnell 
110667207b96SArnd Bergmann static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
110767207b96SArnd Bergmann 			size_t len, loff_t *pos)
110867207b96SArnd Bergmann {
110967207b96SArnd Bergmann 	struct spu_context *ctx;
1110c9101bdbSChristoph Hellwig 	ssize_t ret;
111167207b96SArnd Bergmann 	u32 data;
111267207b96SArnd Bergmann 
111367207b96SArnd Bergmann 	ctx = file->private_data;
111467207b96SArnd Bergmann 
111567207b96SArnd Bergmann 	if (len < 4)
111667207b96SArnd Bergmann 		return -EINVAL;
111767207b96SArnd Bergmann 
111867207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
111967207b96SArnd Bergmann 		return -EFAULT;
112067207b96SArnd Bergmann 
1121c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1122c9101bdbSChristoph Hellwig 	if (ret)
1123c9101bdbSChristoph Hellwig 		return ret;
11248b3d6663SArnd Bergmann 	ctx->ops->signal1_write(ctx, data);
11258b3d6663SArnd Bergmann 	spu_release(ctx);
112667207b96SArnd Bergmann 
112767207b96SArnd Bergmann 	return 4;
112867207b96SArnd Bergmann }
112967207b96SArnd Bergmann 
1130b1e2270fSNick Piggin static int
1131b1e2270fSNick Piggin spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
11326df10a82SMark Nutter {
113387ff6090SJeremy Kerr #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
113487ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
113587ff6090SJeremy Kerr #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
113627d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
113727d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
113827d5bf2aSBenjamin Herrenschmidt 	 */
113987ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
114027d5bf2aSBenjamin Herrenschmidt #else
114127d5bf2aSBenjamin Herrenschmidt #error unsupported page size
114227d5bf2aSBenjamin Herrenschmidt #endif
11436df10a82SMark Nutter }
11446df10a82SMark Nutter 
11456df10a82SMark Nutter static struct vm_operations_struct spufs_signal1_mmap_vmops = {
1146b1e2270fSNick Piggin 	.fault = spufs_signal1_mmap_fault,
11476df10a82SMark Nutter };
11486df10a82SMark Nutter 
11496df10a82SMark Nutter static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
11506df10a82SMark Nutter {
11516df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
11526df10a82SMark Nutter 		return -EINVAL;
11536df10a82SMark Nutter 
115478bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
11556df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
115623cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
11576df10a82SMark Nutter 
11586df10a82SMark Nutter 	vma->vm_ops = &spufs_signal1_mmap_vmops;
11596df10a82SMark Nutter 	return 0;
11606df10a82SMark Nutter }
11616df10a82SMark Nutter 
11625dfe4c96SArjan van de Ven static const struct file_operations spufs_signal1_fops = {
11636df10a82SMark Nutter 	.open = spufs_signal1_open,
116443c2bbd9SChristoph Hellwig 	.release = spufs_signal1_release,
116567207b96SArnd Bergmann 	.read = spufs_signal1_read,
116667207b96SArnd Bergmann 	.write = spufs_signal1_write,
11676df10a82SMark Nutter 	.mmap = spufs_signal1_mmap,
116867207b96SArnd Bergmann };
116967207b96SArnd Bergmann 
1170d054b36fSJeremy Kerr static const struct file_operations spufs_signal1_nosched_fops = {
1171d054b36fSJeremy Kerr 	.open = spufs_signal1_open,
1172d054b36fSJeremy Kerr 	.release = spufs_signal1_release,
1173d054b36fSJeremy Kerr 	.write = spufs_signal1_write,
1174d054b36fSJeremy Kerr 	.mmap = spufs_signal1_mmap,
1175d054b36fSJeremy Kerr };
1176d054b36fSJeremy Kerr 
11776df10a82SMark Nutter static int spufs_signal2_open(struct inode *inode, struct file *file)
11786df10a82SMark Nutter {
11796df10a82SMark Nutter 	struct spufs_inode_info *i = SPUFS_I(inode);
11806df10a82SMark Nutter 	struct spu_context *ctx = i->i_ctx;
118143c2bbd9SChristoph Hellwig 
118247d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
11836df10a82SMark Nutter 	file->private_data = ctx;
118443c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
11856df10a82SMark Nutter 		ctx->signal2 = inode->i_mapping;
118647d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
11876df10a82SMark Nutter 	return nonseekable_open(inode, file);
11886df10a82SMark Nutter }
11896df10a82SMark Nutter 
119043c2bbd9SChristoph Hellwig static int
119143c2bbd9SChristoph Hellwig spufs_signal2_release(struct inode *inode, struct file *file)
119243c2bbd9SChristoph Hellwig {
119343c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
119443c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
119543c2bbd9SChristoph Hellwig 
119647d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
119743c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
119843c2bbd9SChristoph Hellwig 		ctx->signal2 = NULL;
119947d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
120043c2bbd9SChristoph Hellwig 	return 0;
120143c2bbd9SChristoph Hellwig }
120243c2bbd9SChristoph Hellwig 
1203bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
120467207b96SArnd Bergmann 			size_t len, loff_t *pos)
120567207b96SArnd Bergmann {
120617f88cebSDwayne Grant McConnell 	int ret = 0;
120767207b96SArnd Bergmann 	u32 data;
120867207b96SArnd Bergmann 
120967207b96SArnd Bergmann 	if (len < 4)
121067207b96SArnd Bergmann 		return -EINVAL;
121167207b96SArnd Bergmann 
121217f88cebSDwayne Grant McConnell 	if (ctx->csa.spu_chnlcnt_RW[4]) {
121317f88cebSDwayne Grant McConnell 		data =  ctx->csa.spu_chnldata_RW[4];
121417f88cebSDwayne Grant McConnell 		ret = 4;
121517f88cebSDwayne Grant McConnell 	}
12168b3d6663SArnd Bergmann 
121717f88cebSDwayne Grant McConnell 	if (!ret)
121817f88cebSDwayne Grant McConnell 		goto out;
121917f88cebSDwayne Grant McConnell 
122067207b96SArnd Bergmann 	if (copy_to_user(buf, &data, 4))
122167207b96SArnd Bergmann 		return -EFAULT;
122267207b96SArnd Bergmann 
122317f88cebSDwayne Grant McConnell out:
1224bf1ab978SDwayne Grant McConnell 	return ret;
1225bf1ab978SDwayne Grant McConnell }
1226bf1ab978SDwayne Grant McConnell 
1227bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1228bf1ab978SDwayne Grant McConnell 			size_t len, loff_t *pos)
1229bf1ab978SDwayne Grant McConnell {
1230bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
1231bf1ab978SDwayne Grant McConnell 	int ret;
1232bf1ab978SDwayne Grant McConnell 
1233c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1234c9101bdbSChristoph Hellwig 	if (ret)
1235c9101bdbSChristoph Hellwig 		return ret;
1236bf1ab978SDwayne Grant McConnell 	ret = __spufs_signal2_read(ctx, buf, len, pos);
123727b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1238bf1ab978SDwayne Grant McConnell 
1239bf1ab978SDwayne Grant McConnell 	return ret;
124067207b96SArnd Bergmann }
124167207b96SArnd Bergmann 
124267207b96SArnd Bergmann static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
124367207b96SArnd Bergmann 			size_t len, loff_t *pos)
124467207b96SArnd Bergmann {
124567207b96SArnd Bergmann 	struct spu_context *ctx;
1246c9101bdbSChristoph Hellwig 	ssize_t ret;
124767207b96SArnd Bergmann 	u32 data;
124867207b96SArnd Bergmann 
124967207b96SArnd Bergmann 	ctx = file->private_data;
125067207b96SArnd Bergmann 
125167207b96SArnd Bergmann 	if (len < 4)
125267207b96SArnd Bergmann 		return -EINVAL;
125367207b96SArnd Bergmann 
125467207b96SArnd Bergmann 	if (copy_from_user(&data, buf, 4))
125567207b96SArnd Bergmann 		return -EFAULT;
125667207b96SArnd Bergmann 
1257c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1258c9101bdbSChristoph Hellwig 	if (ret)
1259c9101bdbSChristoph Hellwig 		return ret;
12608b3d6663SArnd Bergmann 	ctx->ops->signal2_write(ctx, data);
12618b3d6663SArnd Bergmann 	spu_release(ctx);
126267207b96SArnd Bergmann 
126367207b96SArnd Bergmann 	return 4;
126467207b96SArnd Bergmann }
126567207b96SArnd Bergmann 
126627d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
1267b1e2270fSNick Piggin static int
1268b1e2270fSNick Piggin spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
12696df10a82SMark Nutter {
127087ff6090SJeremy Kerr #if SPUFS_SIGNAL_MAP_SIZE == 0x1000
127187ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
127287ff6090SJeremy Kerr #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
127327d5bf2aSBenjamin Herrenschmidt 	/* For 64k pages, both signal1 and signal2 can be used to mmap the whole
127427d5bf2aSBenjamin Herrenschmidt 	 * signal 1 and 2 area
127527d5bf2aSBenjamin Herrenschmidt 	 */
127687ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
127727d5bf2aSBenjamin Herrenschmidt #else
127827d5bf2aSBenjamin Herrenschmidt #error unsupported page size
127927d5bf2aSBenjamin Herrenschmidt #endif
12806df10a82SMark Nutter }
12816df10a82SMark Nutter 
12826df10a82SMark Nutter static struct vm_operations_struct spufs_signal2_mmap_vmops = {
1283b1e2270fSNick Piggin 	.fault = spufs_signal2_mmap_fault,
12846df10a82SMark Nutter };
12856df10a82SMark Nutter 
12866df10a82SMark Nutter static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
12876df10a82SMark Nutter {
12886df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
12896df10a82SMark Nutter 		return -EINVAL;
12906df10a82SMark Nutter 
129178bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
12926df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
129323cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
12946df10a82SMark Nutter 
12956df10a82SMark Nutter 	vma->vm_ops = &spufs_signal2_mmap_vmops;
12966df10a82SMark Nutter 	return 0;
12976df10a82SMark Nutter }
129827d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
129927d5bf2aSBenjamin Herrenschmidt #define spufs_signal2_mmap NULL
130027d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
13016df10a82SMark Nutter 
13025dfe4c96SArjan van de Ven static const struct file_operations spufs_signal2_fops = {
13036df10a82SMark Nutter 	.open = spufs_signal2_open,
130443c2bbd9SChristoph Hellwig 	.release = spufs_signal2_release,
130567207b96SArnd Bergmann 	.read = spufs_signal2_read,
130667207b96SArnd Bergmann 	.write = spufs_signal2_write,
13076df10a82SMark Nutter 	.mmap = spufs_signal2_mmap,
130867207b96SArnd Bergmann };
130967207b96SArnd Bergmann 
1310d054b36fSJeremy Kerr static const struct file_operations spufs_signal2_nosched_fops = {
1311d054b36fSJeremy Kerr 	.open = spufs_signal2_open,
1312d054b36fSJeremy Kerr 	.release = spufs_signal2_release,
1313d054b36fSJeremy Kerr 	.write = spufs_signal2_write,
1314d054b36fSJeremy Kerr 	.mmap = spufs_signal2_mmap,
1315d054b36fSJeremy Kerr };
1316d054b36fSJeremy Kerr 
1317104f0cc2SMichael Ellerman /*
1318104f0cc2SMichael Ellerman  * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1319104f0cc2SMichael Ellerman  * work of acquiring (or not) the SPU context before calling through
1320104f0cc2SMichael Ellerman  * to the actual get routine. The set routine is called directly.
1321104f0cc2SMichael Ellerman  */
1322104f0cc2SMichael Ellerman #define SPU_ATTR_NOACQUIRE	0
1323104f0cc2SMichael Ellerman #define SPU_ATTR_ACQUIRE	1
1324104f0cc2SMichael Ellerman #define SPU_ATTR_ACQUIRE_SAVED	2
1325104f0cc2SMichael Ellerman 
1326104f0cc2SMichael Ellerman #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire)	\
1327197b1a82SChristoph Hellwig static int __##__get(void *data, u64 *val)				\
1328104f0cc2SMichael Ellerman {									\
1329104f0cc2SMichael Ellerman 	struct spu_context *ctx = data;					\
1330c9101bdbSChristoph Hellwig 	int ret = 0;							\
1331104f0cc2SMichael Ellerman 									\
1332104f0cc2SMichael Ellerman 	if (__acquire == SPU_ATTR_ACQUIRE) {				\
1333c9101bdbSChristoph Hellwig 		ret = spu_acquire(ctx);					\
1334c9101bdbSChristoph Hellwig 		if (ret)						\
1335c9101bdbSChristoph Hellwig 			return ret;					\
1336197b1a82SChristoph Hellwig 		*val = __get(ctx);					\
1337104f0cc2SMichael Ellerman 		spu_release(ctx);					\
1338104f0cc2SMichael Ellerman 	} else if (__acquire == SPU_ATTR_ACQUIRE_SAVED)	{		\
1339c9101bdbSChristoph Hellwig 		ret = spu_acquire_saved(ctx);				\
1340c9101bdbSChristoph Hellwig 		if (ret)						\
1341c9101bdbSChristoph Hellwig 			return ret;					\
1342197b1a82SChristoph Hellwig 		*val = __get(ctx);					\
1343104f0cc2SMichael Ellerman 		spu_release_saved(ctx);					\
1344104f0cc2SMichael Ellerman 	} else								\
1345197b1a82SChristoph Hellwig 		*val = __get(ctx);					\
1346104f0cc2SMichael Ellerman 									\
1347197b1a82SChristoph Hellwig 	return 0;							\
1348104f0cc2SMichael Ellerman }									\
1349197b1a82SChristoph Hellwig DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
1350104f0cc2SMichael Ellerman 
1351197b1a82SChristoph Hellwig static int spufs_signal1_type_set(void *data, u64 val)
135267207b96SArnd Bergmann {
135367207b96SArnd Bergmann 	struct spu_context *ctx = data;
1354c9101bdbSChristoph Hellwig 	int ret;
135567207b96SArnd Bergmann 
1356c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1357c9101bdbSChristoph Hellwig 	if (ret)
1358c9101bdbSChristoph Hellwig 		return ret;
13598b3d6663SArnd Bergmann 	ctx->ops->signal1_type_set(ctx, val);
13608b3d6663SArnd Bergmann 	spu_release(ctx);
1361197b1a82SChristoph Hellwig 
1362197b1a82SChristoph Hellwig 	return 0;
136367207b96SArnd Bergmann }
136467207b96SArnd Bergmann 
1365104f0cc2SMichael Ellerman static u64 spufs_signal1_type_get(struct spu_context *ctx)
1366bf1ab978SDwayne Grant McConnell {
1367bf1ab978SDwayne Grant McConnell 	return ctx->ops->signal1_type_get(ctx);
1368bf1ab978SDwayne Grant McConnell }
1369104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
1370af8b44e0SJeremy Kerr 		       spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
1371bf1ab978SDwayne Grant McConnell 
137267207b96SArnd Bergmann 
1373197b1a82SChristoph Hellwig static int spufs_signal2_type_set(void *data, u64 val)
137467207b96SArnd Bergmann {
137567207b96SArnd Bergmann 	struct spu_context *ctx = data;
1376c9101bdbSChristoph Hellwig 	int ret;
137767207b96SArnd Bergmann 
1378c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1379c9101bdbSChristoph Hellwig 	if (ret)
1380c9101bdbSChristoph Hellwig 		return ret;
13818b3d6663SArnd Bergmann 	ctx->ops->signal2_type_set(ctx, val);
13828b3d6663SArnd Bergmann 	spu_release(ctx);
1383197b1a82SChristoph Hellwig 
1384197b1a82SChristoph Hellwig 	return 0;
138567207b96SArnd Bergmann }
138667207b96SArnd Bergmann 
1387104f0cc2SMichael Ellerman static u64 spufs_signal2_type_get(struct spu_context *ctx)
1388bf1ab978SDwayne Grant McConnell {
1389bf1ab978SDwayne Grant McConnell 	return ctx->ops->signal2_type_get(ctx);
1390bf1ab978SDwayne Grant McConnell }
1391104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
1392af8b44e0SJeremy Kerr 		       spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
139367207b96SArnd Bergmann 
139427d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
1395b1e2270fSNick Piggin static int
1396b1e2270fSNick Piggin spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1397d9379c4bSarnd@arndb.de {
139887ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
1399d9379c4bSarnd@arndb.de }
1400d9379c4bSarnd@arndb.de 
1401d9379c4bSarnd@arndb.de static struct vm_operations_struct spufs_mss_mmap_vmops = {
1402b1e2270fSNick Piggin 	.fault = spufs_mss_mmap_fault,
1403d9379c4bSarnd@arndb.de };
1404d9379c4bSarnd@arndb.de 
1405d9379c4bSarnd@arndb.de /*
1406d9379c4bSarnd@arndb.de  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
1407d9379c4bSarnd@arndb.de  */
1408d9379c4bSarnd@arndb.de static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1409d9379c4bSarnd@arndb.de {
1410d9379c4bSarnd@arndb.de 	if (!(vma->vm_flags & VM_SHARED))
1411d9379c4bSarnd@arndb.de 		return -EINVAL;
1412d9379c4bSarnd@arndb.de 
141378bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
1414d9379c4bSarnd@arndb.de 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
141523cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
1416d9379c4bSarnd@arndb.de 
1417d9379c4bSarnd@arndb.de 	vma->vm_ops = &spufs_mss_mmap_vmops;
1418d9379c4bSarnd@arndb.de 	return 0;
1419d9379c4bSarnd@arndb.de }
142027d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
142127d5bf2aSBenjamin Herrenschmidt #define spufs_mss_mmap NULL
142227d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
1423d9379c4bSarnd@arndb.de 
1424d9379c4bSarnd@arndb.de static int spufs_mss_open(struct inode *inode, struct file *file)
1425d9379c4bSarnd@arndb.de {
1426d9379c4bSarnd@arndb.de 	struct spufs_inode_info *i = SPUFS_I(inode);
142717e0e270SBenjamin Herrenschmidt 	struct spu_context *ctx = i->i_ctx;
1428d9379c4bSarnd@arndb.de 
1429d9379c4bSarnd@arndb.de 	file->private_data = i->i_ctx;
143043c2bbd9SChristoph Hellwig 
143147d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
143243c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
143317e0e270SBenjamin Herrenschmidt 		ctx->mss = inode->i_mapping;
143447d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1435d9379c4bSarnd@arndb.de 	return nonseekable_open(inode, file);
1436d9379c4bSarnd@arndb.de }
1437d9379c4bSarnd@arndb.de 
143843c2bbd9SChristoph Hellwig static int
143943c2bbd9SChristoph Hellwig spufs_mss_release(struct inode *inode, struct file *file)
144043c2bbd9SChristoph Hellwig {
144143c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
144243c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
144343c2bbd9SChristoph Hellwig 
144447d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
144543c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
144643c2bbd9SChristoph Hellwig 		ctx->mss = NULL;
144747d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
144843c2bbd9SChristoph Hellwig 	return 0;
144943c2bbd9SChristoph Hellwig }
145043c2bbd9SChristoph Hellwig 
14515dfe4c96SArjan van de Ven static const struct file_operations spufs_mss_fops = {
1452d9379c4bSarnd@arndb.de 	.open	 = spufs_mss_open,
145343c2bbd9SChristoph Hellwig 	.release = spufs_mss_release,
1454d9379c4bSarnd@arndb.de 	.mmap	 = spufs_mss_mmap,
145527d5bf2aSBenjamin Herrenschmidt };
145627d5bf2aSBenjamin Herrenschmidt 
1457b1e2270fSNick Piggin static int
1458b1e2270fSNick Piggin spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
145927d5bf2aSBenjamin Herrenschmidt {
146087ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
146127d5bf2aSBenjamin Herrenschmidt }
146227d5bf2aSBenjamin Herrenschmidt 
146327d5bf2aSBenjamin Herrenschmidt static struct vm_operations_struct spufs_psmap_mmap_vmops = {
1464b1e2270fSNick Piggin 	.fault = spufs_psmap_mmap_fault,
146527d5bf2aSBenjamin Herrenschmidt };
146627d5bf2aSBenjamin Herrenschmidt 
146727d5bf2aSBenjamin Herrenschmidt /*
146827d5bf2aSBenjamin Herrenschmidt  * mmap support for full problem state area [0x00000 - 0x1ffff].
146927d5bf2aSBenjamin Herrenschmidt  */
147027d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
147127d5bf2aSBenjamin Herrenschmidt {
147227d5bf2aSBenjamin Herrenschmidt 	if (!(vma->vm_flags & VM_SHARED))
147327d5bf2aSBenjamin Herrenschmidt 		return -EINVAL;
147427d5bf2aSBenjamin Herrenschmidt 
147578bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
147627d5bf2aSBenjamin Herrenschmidt 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
147727d5bf2aSBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
147827d5bf2aSBenjamin Herrenschmidt 
147927d5bf2aSBenjamin Herrenschmidt 	vma->vm_ops = &spufs_psmap_mmap_vmops;
148027d5bf2aSBenjamin Herrenschmidt 	return 0;
148127d5bf2aSBenjamin Herrenschmidt }
148227d5bf2aSBenjamin Herrenschmidt 
148327d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_open(struct inode *inode, struct file *file)
148427d5bf2aSBenjamin Herrenschmidt {
148527d5bf2aSBenjamin Herrenschmidt 	struct spufs_inode_info *i = SPUFS_I(inode);
148617e0e270SBenjamin Herrenschmidt 	struct spu_context *ctx = i->i_ctx;
148727d5bf2aSBenjamin Herrenschmidt 
148847d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
148927d5bf2aSBenjamin Herrenschmidt 	file->private_data = i->i_ctx;
149043c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
149117e0e270SBenjamin Herrenschmidt 		ctx->psmap = inode->i_mapping;
149247d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
149327d5bf2aSBenjamin Herrenschmidt 	return nonseekable_open(inode, file);
149427d5bf2aSBenjamin Herrenschmidt }
149527d5bf2aSBenjamin Herrenschmidt 
149643c2bbd9SChristoph Hellwig static int
149743c2bbd9SChristoph Hellwig spufs_psmap_release(struct inode *inode, struct file *file)
149843c2bbd9SChristoph Hellwig {
149943c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
150043c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
150143c2bbd9SChristoph Hellwig 
150247d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
150343c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
150443c2bbd9SChristoph Hellwig 		ctx->psmap = NULL;
150547d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
150643c2bbd9SChristoph Hellwig 	return 0;
150743c2bbd9SChristoph Hellwig }
150843c2bbd9SChristoph Hellwig 
15095dfe4c96SArjan van de Ven static const struct file_operations spufs_psmap_fops = {
151027d5bf2aSBenjamin Herrenschmidt 	.open	 = spufs_psmap_open,
151143c2bbd9SChristoph Hellwig 	.release = spufs_psmap_release,
151227d5bf2aSBenjamin Herrenschmidt 	.mmap	 = spufs_psmap_mmap,
1513d9379c4bSarnd@arndb.de };
1514d9379c4bSarnd@arndb.de 
1515d9379c4bSarnd@arndb.de 
151627d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K
1517b1e2270fSNick Piggin static int
1518b1e2270fSNick Piggin spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
15196df10a82SMark Nutter {
152087ff6090SJeremy Kerr 	return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
15216df10a82SMark Nutter }
15226df10a82SMark Nutter 
15236df10a82SMark Nutter static struct vm_operations_struct spufs_mfc_mmap_vmops = {
1524b1e2270fSNick Piggin 	.fault = spufs_mfc_mmap_fault,
15256df10a82SMark Nutter };
15266df10a82SMark Nutter 
15276df10a82SMark Nutter /*
15286df10a82SMark Nutter  * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
15296df10a82SMark Nutter  */
15306df10a82SMark Nutter static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
15316df10a82SMark Nutter {
15326df10a82SMark Nutter 	if (!(vma->vm_flags & VM_SHARED))
15336df10a82SMark Nutter 		return -EINVAL;
15346df10a82SMark Nutter 
153578bde53eSBenjamin Herrenschmidt 	vma->vm_flags |= VM_IO | VM_PFNMAP;
15366df10a82SMark Nutter 	vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot)
153723cc7701SBenjamin Herrenschmidt 				     | _PAGE_NO_CACHE | _PAGE_GUARDED);
15386df10a82SMark Nutter 
15396df10a82SMark Nutter 	vma->vm_ops = &spufs_mfc_mmap_vmops;
15406df10a82SMark Nutter 	return 0;
15416df10a82SMark Nutter }
154227d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */
154327d5bf2aSBenjamin Herrenschmidt #define spufs_mfc_mmap NULL
154427d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */
1545a33a7d73SArnd Bergmann 
1546a33a7d73SArnd Bergmann static int spufs_mfc_open(struct inode *inode, struct file *file)
1547a33a7d73SArnd Bergmann {
1548a33a7d73SArnd Bergmann 	struct spufs_inode_info *i = SPUFS_I(inode);
1549a33a7d73SArnd Bergmann 	struct spu_context *ctx = i->i_ctx;
1550a33a7d73SArnd Bergmann 
1551a33a7d73SArnd Bergmann 	/* we don't want to deal with DMA into other processes */
1552a33a7d73SArnd Bergmann 	if (ctx->owner != current->mm)
1553a33a7d73SArnd Bergmann 		return -EINVAL;
1554a33a7d73SArnd Bergmann 
1555a33a7d73SArnd Bergmann 	if (atomic_read(&inode->i_count) != 1)
1556a33a7d73SArnd Bergmann 		return -EBUSY;
1557a33a7d73SArnd Bergmann 
155847d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
1559a33a7d73SArnd Bergmann 	file->private_data = ctx;
156043c2bbd9SChristoph Hellwig 	if (!i->i_openers++)
156117e0e270SBenjamin Herrenschmidt 		ctx->mfc = inode->i_mapping;
156247d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
1563a33a7d73SArnd Bergmann 	return nonseekable_open(inode, file);
1564a33a7d73SArnd Bergmann }
1565a33a7d73SArnd Bergmann 
156643c2bbd9SChristoph Hellwig static int
156743c2bbd9SChristoph Hellwig spufs_mfc_release(struct inode *inode, struct file *file)
156843c2bbd9SChristoph Hellwig {
156943c2bbd9SChristoph Hellwig 	struct spufs_inode_info *i = SPUFS_I(inode);
157043c2bbd9SChristoph Hellwig 	struct spu_context *ctx = i->i_ctx;
157143c2bbd9SChristoph Hellwig 
157247d3a5faSChristoph Hellwig 	mutex_lock(&ctx->mapping_lock);
157343c2bbd9SChristoph Hellwig 	if (!--i->i_openers)
157443c2bbd9SChristoph Hellwig 		ctx->mfc = NULL;
157547d3a5faSChristoph Hellwig 	mutex_unlock(&ctx->mapping_lock);
157643c2bbd9SChristoph Hellwig 	return 0;
157743c2bbd9SChristoph Hellwig }
157843c2bbd9SChristoph Hellwig 
1579a33a7d73SArnd Bergmann /* interrupt-level mfc callback function. */
1580a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu)
1581a33a7d73SArnd Bergmann {
1582a33a7d73SArnd Bergmann 	struct spu_context *ctx = spu->ctx;
1583a33a7d73SArnd Bergmann 
1584e65c2f6fSLuke Browning 	if (!ctx)
1585e65c2f6fSLuke Browning 		return;
1586e65c2f6fSLuke Browning 
1587a33a7d73SArnd Bergmann 	wake_up_all(&ctx->mfc_wq);
1588a33a7d73SArnd Bergmann 
1589e48b1b45SHarvey Harrison 	pr_debug("%s %s\n", __func__, spu->name);
1590a33a7d73SArnd Bergmann 	if (ctx->mfc_fasync) {
1591a33a7d73SArnd Bergmann 		u32 free_elements, tagstatus;
1592a33a7d73SArnd Bergmann 		unsigned int mask;
1593a33a7d73SArnd Bergmann 
1594a33a7d73SArnd Bergmann 		/* no need for spu_acquire in interrupt context */
1595a33a7d73SArnd Bergmann 		free_elements = ctx->ops->get_mfc_free_elements(ctx);
1596a33a7d73SArnd Bergmann 		tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1597a33a7d73SArnd Bergmann 
1598a33a7d73SArnd Bergmann 		mask = 0;
1599a33a7d73SArnd Bergmann 		if (free_elements & 0xffff)
1600a33a7d73SArnd Bergmann 			mask |= POLLOUT;
1601a33a7d73SArnd Bergmann 		if (tagstatus & ctx->tagwait)
1602a33a7d73SArnd Bergmann 			mask |= POLLIN;
1603a33a7d73SArnd Bergmann 
1604a33a7d73SArnd Bergmann 		kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1605a33a7d73SArnd Bergmann 	}
1606a33a7d73SArnd Bergmann }
1607a33a7d73SArnd Bergmann 
1608a33a7d73SArnd Bergmann static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1609a33a7d73SArnd Bergmann {
1610a33a7d73SArnd Bergmann 	/* See if there is one tag group is complete */
1611a33a7d73SArnd Bergmann 	/* FIXME we need locking around tagwait */
1612a33a7d73SArnd Bergmann 	*status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1613a33a7d73SArnd Bergmann 	ctx->tagwait &= ~*status;
1614a33a7d73SArnd Bergmann 	if (*status)
1615a33a7d73SArnd Bergmann 		return 1;
1616a33a7d73SArnd Bergmann 
1617a33a7d73SArnd Bergmann 	/* enable interrupt waiting for any tag group,
1618a33a7d73SArnd Bergmann 	   may silently fail if interrupts are already enabled */
1619a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1620a33a7d73SArnd Bergmann 	return 0;
1621a33a7d73SArnd Bergmann }
1622a33a7d73SArnd Bergmann 
1623a33a7d73SArnd Bergmann static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1624a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1625a33a7d73SArnd Bergmann {
1626a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1627a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1628a33a7d73SArnd Bergmann 	u32 status;
1629a33a7d73SArnd Bergmann 
1630a33a7d73SArnd Bergmann 	if (size != 4)
1631a33a7d73SArnd Bergmann 		goto out;
1632a33a7d73SArnd Bergmann 
1633c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1634c9101bdbSChristoph Hellwig 	if (ret)
1635c9101bdbSChristoph Hellwig 		return ret;
1636c9101bdbSChristoph Hellwig 
1637c9101bdbSChristoph Hellwig 	ret = -EINVAL;
1638a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1639a33a7d73SArnd Bergmann 		status = ctx->ops->read_mfc_tagstatus(ctx);
1640a33a7d73SArnd Bergmann 		if (!(status & ctx->tagwait))
1641a33a7d73SArnd Bergmann 			ret = -EAGAIN;
1642a33a7d73SArnd Bergmann 		else
1643c9101bdbSChristoph Hellwig 			/* XXX(hch): shouldn't we clear ret here? */
1644a33a7d73SArnd Bergmann 			ctx->tagwait &= ~status;
1645a33a7d73SArnd Bergmann 	} else {
1646a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1647a33a7d73SArnd Bergmann 			   spufs_read_mfc_tagstatus(ctx, &status));
1648a33a7d73SArnd Bergmann 		if (ret)
1649a33a7d73SArnd Bergmann 			goto out;
1650eebead5bSChristoph Hellwig 	}
1651eebead5bSChristoph Hellwig 	spu_release(ctx);
1652a33a7d73SArnd Bergmann 
1653a33a7d73SArnd Bergmann 	ret = 4;
1654a33a7d73SArnd Bergmann 	if (copy_to_user(buffer, &status, 4))
1655a33a7d73SArnd Bergmann 		ret = -EFAULT;
1656a33a7d73SArnd Bergmann 
1657a33a7d73SArnd Bergmann out:
1658a33a7d73SArnd Bergmann 	return ret;
1659a33a7d73SArnd Bergmann }
1660a33a7d73SArnd Bergmann 
1661a33a7d73SArnd Bergmann static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1662a33a7d73SArnd Bergmann {
1663a33a7d73SArnd Bergmann 	pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa,
1664a33a7d73SArnd Bergmann 		 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1665a33a7d73SArnd Bergmann 
1666a33a7d73SArnd Bergmann 	switch (cmd->cmd) {
1667a33a7d73SArnd Bergmann 	case MFC_PUT_CMD:
1668a33a7d73SArnd Bergmann 	case MFC_PUTF_CMD:
1669a33a7d73SArnd Bergmann 	case MFC_PUTB_CMD:
1670a33a7d73SArnd Bergmann 	case MFC_GET_CMD:
1671a33a7d73SArnd Bergmann 	case MFC_GETF_CMD:
1672a33a7d73SArnd Bergmann 	case MFC_GETB_CMD:
1673a33a7d73SArnd Bergmann 		break;
1674a33a7d73SArnd Bergmann 	default:
1675a33a7d73SArnd Bergmann 		pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1676a33a7d73SArnd Bergmann 		return -EIO;
1677a33a7d73SArnd Bergmann 	}
1678a33a7d73SArnd Bergmann 
1679a33a7d73SArnd Bergmann 	if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
1680a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment, ea %lx lsa %x\n",
1681a33a7d73SArnd Bergmann 				cmd->ea, cmd->lsa);
1682a33a7d73SArnd Bergmann 		return -EIO;
1683a33a7d73SArnd Bergmann 	}
1684a33a7d73SArnd Bergmann 
1685a33a7d73SArnd Bergmann 	switch (cmd->size & 0xf) {
1686a33a7d73SArnd Bergmann 	case 1:
1687a33a7d73SArnd Bergmann 		break;
1688a33a7d73SArnd Bergmann 	case 2:
1689a33a7d73SArnd Bergmann 		if (cmd->lsa & 1)
1690a33a7d73SArnd Bergmann 			goto error;
1691a33a7d73SArnd Bergmann 		break;
1692a33a7d73SArnd Bergmann 	case 4:
1693a33a7d73SArnd Bergmann 		if (cmd->lsa & 3)
1694a33a7d73SArnd Bergmann 			goto error;
1695a33a7d73SArnd Bergmann 		break;
1696a33a7d73SArnd Bergmann 	case 8:
1697a33a7d73SArnd Bergmann 		if (cmd->lsa & 7)
1698a33a7d73SArnd Bergmann 			goto error;
1699a33a7d73SArnd Bergmann 		break;
1700a33a7d73SArnd Bergmann 	case 0:
1701a33a7d73SArnd Bergmann 		if (cmd->lsa & 15)
1702a33a7d73SArnd Bergmann 			goto error;
1703a33a7d73SArnd Bergmann 		break;
1704a33a7d73SArnd Bergmann 	error:
1705a33a7d73SArnd Bergmann 	default:
1706a33a7d73SArnd Bergmann 		pr_debug("invalid DMA alignment %x for size %x\n",
1707a33a7d73SArnd Bergmann 			cmd->lsa & 0xf, cmd->size);
1708a33a7d73SArnd Bergmann 		return -EIO;
1709a33a7d73SArnd Bergmann 	}
1710a33a7d73SArnd Bergmann 
1711a33a7d73SArnd Bergmann 	if (cmd->size > 16 * 1024) {
1712a33a7d73SArnd Bergmann 		pr_debug("invalid DMA size %x\n", cmd->size);
1713a33a7d73SArnd Bergmann 		return -EIO;
1714a33a7d73SArnd Bergmann 	}
1715a33a7d73SArnd Bergmann 
1716a33a7d73SArnd Bergmann 	if (cmd->tag & 0xfff0) {
1717a33a7d73SArnd Bergmann 		/* we reserve the higher tag numbers for kernel use */
1718a33a7d73SArnd Bergmann 		pr_debug("invalid DMA tag\n");
1719a33a7d73SArnd Bergmann 		return -EIO;
1720a33a7d73SArnd Bergmann 	}
1721a33a7d73SArnd Bergmann 
1722a33a7d73SArnd Bergmann 	if (cmd->class) {
1723a33a7d73SArnd Bergmann 		/* not supported in this version */
1724a33a7d73SArnd Bergmann 		pr_debug("invalid DMA class\n");
1725a33a7d73SArnd Bergmann 		return -EIO;
1726a33a7d73SArnd Bergmann 	}
1727a33a7d73SArnd Bergmann 
1728a33a7d73SArnd Bergmann 	return 0;
1729a33a7d73SArnd Bergmann }
1730a33a7d73SArnd Bergmann 
1731a33a7d73SArnd Bergmann static int spu_send_mfc_command(struct spu_context *ctx,
1732a33a7d73SArnd Bergmann 				struct mfc_dma_command cmd,
1733a33a7d73SArnd Bergmann 				int *error)
1734a33a7d73SArnd Bergmann {
1735a33a7d73SArnd Bergmann 	*error = ctx->ops->send_mfc_command(ctx, &cmd);
1736a33a7d73SArnd Bergmann 	if (*error == -EAGAIN) {
1737a33a7d73SArnd Bergmann 		/* wait for any tag group to complete
1738a33a7d73SArnd Bergmann 		   so we have space for the new command */
1739a33a7d73SArnd Bergmann 		ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1740a33a7d73SArnd Bergmann 		/* try again, because the queue might be
1741a33a7d73SArnd Bergmann 		   empty again */
1742a33a7d73SArnd Bergmann 		*error = ctx->ops->send_mfc_command(ctx, &cmd);
1743a33a7d73SArnd Bergmann 		if (*error == -EAGAIN)
1744a33a7d73SArnd Bergmann 			return 0;
1745a33a7d73SArnd Bergmann 	}
1746a33a7d73SArnd Bergmann 	return 1;
1747a33a7d73SArnd Bergmann }
1748a33a7d73SArnd Bergmann 
1749a33a7d73SArnd Bergmann static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1750a33a7d73SArnd Bergmann 			size_t size, loff_t *pos)
1751a33a7d73SArnd Bergmann {
1752a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1753a33a7d73SArnd Bergmann 	struct mfc_dma_command cmd;
1754a33a7d73SArnd Bergmann 	int ret = -EINVAL;
1755a33a7d73SArnd Bergmann 
1756a33a7d73SArnd Bergmann 	if (size != sizeof cmd)
1757a33a7d73SArnd Bergmann 		goto out;
1758a33a7d73SArnd Bergmann 
1759a33a7d73SArnd Bergmann 	ret = -EFAULT;
1760a33a7d73SArnd Bergmann 	if (copy_from_user(&cmd, buffer, sizeof cmd))
1761a33a7d73SArnd Bergmann 		goto out;
1762a33a7d73SArnd Bergmann 
1763a33a7d73SArnd Bergmann 	ret = spufs_check_valid_dma(&cmd);
1764a33a7d73SArnd Bergmann 	if (ret)
1765a33a7d73SArnd Bergmann 		goto out;
1766a33a7d73SArnd Bergmann 
1767c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1768c9101bdbSChristoph Hellwig 	if (ret)
1769c9101bdbSChristoph Hellwig 		goto out;
1770c9101bdbSChristoph Hellwig 
177133bfd7a7SArnd Bergmann 	ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
1772577f8f10SAkinobu Mita 	if (ret)
1773577f8f10SAkinobu Mita 		goto out;
1774577f8f10SAkinobu Mita 
1775a33a7d73SArnd Bergmann 	if (file->f_flags & O_NONBLOCK) {
1776a33a7d73SArnd Bergmann 		ret = ctx->ops->send_mfc_command(ctx, &cmd);
1777a33a7d73SArnd Bergmann 	} else {
1778a33a7d73SArnd Bergmann 		int status;
1779a33a7d73SArnd Bergmann 		ret = spufs_wait(ctx->mfc_wq,
1780a33a7d73SArnd Bergmann 				 spu_send_mfc_command(ctx, cmd, &status));
1781eebead5bSChristoph Hellwig 		if (ret)
1782eebead5bSChristoph Hellwig 			goto out;
1783a33a7d73SArnd Bergmann 		if (status)
1784a33a7d73SArnd Bergmann 			ret = status;
1785a33a7d73SArnd Bergmann 	}
1786a33a7d73SArnd Bergmann 
1787a33a7d73SArnd Bergmann 	if (ret)
1788933b0e35SKazunori Asayama 		goto out_unlock;
1789a33a7d73SArnd Bergmann 
1790a33a7d73SArnd Bergmann 	ctx->tagwait |= 1 << cmd.tag;
17913692dc66SMasato Noguchi 	ret = size;
1792a33a7d73SArnd Bergmann 
1793933b0e35SKazunori Asayama out_unlock:
1794933b0e35SKazunori Asayama 	spu_release(ctx);
1795a33a7d73SArnd Bergmann out:
1796a33a7d73SArnd Bergmann 	return ret;
1797a33a7d73SArnd Bergmann }
1798a33a7d73SArnd Bergmann 
1799a33a7d73SArnd Bergmann static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1800a33a7d73SArnd Bergmann {
1801a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1802a33a7d73SArnd Bergmann 	u32 free_elements, tagstatus;
1803a33a7d73SArnd Bergmann 	unsigned int mask;
1804a33a7d73SArnd Bergmann 
1805933b0e35SKazunori Asayama 	poll_wait(file, &ctx->mfc_wq, wait);
1806933b0e35SKazunori Asayama 
1807c9101bdbSChristoph Hellwig 	/*
1808c9101bdbSChristoph Hellwig 	 * For now keep this uninterruptible and also ignore the rule
1809c9101bdbSChristoph Hellwig 	 * that poll should not sleep.  Will be fixed later.
1810c9101bdbSChristoph Hellwig 	 */
1811c9101bdbSChristoph Hellwig 	mutex_lock(&ctx->state_mutex);
1812a33a7d73SArnd Bergmann 	ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1813a33a7d73SArnd Bergmann 	free_elements = ctx->ops->get_mfc_free_elements(ctx);
1814a33a7d73SArnd Bergmann 	tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1815a33a7d73SArnd Bergmann 	spu_release(ctx);
1816a33a7d73SArnd Bergmann 
1817a33a7d73SArnd Bergmann 	mask = 0;
1818a33a7d73SArnd Bergmann 	if (free_elements & 0xffff)
1819a33a7d73SArnd Bergmann 		mask |= POLLOUT | POLLWRNORM;
1820a33a7d73SArnd Bergmann 	if (tagstatus & ctx->tagwait)
1821a33a7d73SArnd Bergmann 		mask |= POLLIN | POLLRDNORM;
1822a33a7d73SArnd Bergmann 
1823e48b1b45SHarvey Harrison 	pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
1824a33a7d73SArnd Bergmann 		free_elements, tagstatus, ctx->tagwait);
1825a33a7d73SArnd Bergmann 
1826a33a7d73SArnd Bergmann 	return mask;
1827a33a7d73SArnd Bergmann }
1828a33a7d73SArnd Bergmann 
182973b6af8aSAl Viro static int spufs_mfc_flush(struct file *file, fl_owner_t id)
1830a33a7d73SArnd Bergmann {
1831a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1832a33a7d73SArnd Bergmann 	int ret;
1833a33a7d73SArnd Bergmann 
1834c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1835c9101bdbSChristoph Hellwig 	if (ret)
1836eebead5bSChristoph Hellwig 		goto out;
1837a33a7d73SArnd Bergmann #if 0
1838a33a7d73SArnd Bergmann /* this currently hangs */
1839a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1840a33a7d73SArnd Bergmann 			 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1841a33a7d73SArnd Bergmann 	if (ret)
1842a33a7d73SArnd Bergmann 		goto out;
1843a33a7d73SArnd Bergmann 	ret = spufs_wait(ctx->mfc_wq,
1844a33a7d73SArnd Bergmann 			 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
1845eebead5bSChristoph Hellwig 	if (ret)
1846eebead5bSChristoph Hellwig 		goto out;
1847a33a7d73SArnd Bergmann #else
1848a33a7d73SArnd Bergmann 	ret = 0;
1849a33a7d73SArnd Bergmann #endif
1850a33a7d73SArnd Bergmann 	spu_release(ctx);
1851eebead5bSChristoph Hellwig out:
1852a33a7d73SArnd Bergmann 	return ret;
1853a33a7d73SArnd Bergmann }
1854a33a7d73SArnd Bergmann 
1855a33a7d73SArnd Bergmann static int spufs_mfc_fsync(struct file *file, struct dentry *dentry,
1856a33a7d73SArnd Bergmann 			   int datasync)
1857a33a7d73SArnd Bergmann {
185873b6af8aSAl Viro 	return spufs_mfc_flush(file, NULL);
1859a33a7d73SArnd Bergmann }
1860a33a7d73SArnd Bergmann 
1861a33a7d73SArnd Bergmann static int spufs_mfc_fasync(int fd, struct file *file, int on)
1862a33a7d73SArnd Bergmann {
1863a33a7d73SArnd Bergmann 	struct spu_context *ctx = file->private_data;
1864a33a7d73SArnd Bergmann 
1865a33a7d73SArnd Bergmann 	return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1866a33a7d73SArnd Bergmann }
1867a33a7d73SArnd Bergmann 
18685dfe4c96SArjan van de Ven static const struct file_operations spufs_mfc_fops = {
1869a33a7d73SArnd Bergmann 	.open	 = spufs_mfc_open,
187043c2bbd9SChristoph Hellwig 	.release = spufs_mfc_release,
1871a33a7d73SArnd Bergmann 	.read	 = spufs_mfc_read,
1872a33a7d73SArnd Bergmann 	.write	 = spufs_mfc_write,
1873a33a7d73SArnd Bergmann 	.poll	 = spufs_mfc_poll,
1874a33a7d73SArnd Bergmann 	.flush	 = spufs_mfc_flush,
1875a33a7d73SArnd Bergmann 	.fsync	 = spufs_mfc_fsync,
1876a33a7d73SArnd Bergmann 	.fasync	 = spufs_mfc_fasync,
18776df10a82SMark Nutter 	.mmap	 = spufs_mfc_mmap,
1878a33a7d73SArnd Bergmann };
1879a33a7d73SArnd Bergmann 
1880197b1a82SChristoph Hellwig static int spufs_npc_set(void *data, u64 val)
188167207b96SArnd Bergmann {
188267207b96SArnd Bergmann 	struct spu_context *ctx = data;
1883c9101bdbSChristoph Hellwig 	int ret;
1884c9101bdbSChristoph Hellwig 
1885c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
1886c9101bdbSChristoph Hellwig 	if (ret)
1887c9101bdbSChristoph Hellwig 		return ret;
18888b3d6663SArnd Bergmann 	ctx->ops->npc_write(ctx, val);
18898b3d6663SArnd Bergmann 	spu_release(ctx);
1890197b1a82SChristoph Hellwig 
1891197b1a82SChristoph Hellwig 	return 0;
189267207b96SArnd Bergmann }
189367207b96SArnd Bergmann 
1894104f0cc2SMichael Ellerman static u64 spufs_npc_get(struct spu_context *ctx)
189578810ff6SMichael Ellerman {
189678810ff6SMichael Ellerman 	return ctx->ops->npc_read(ctx);
189778810ff6SMichael Ellerman }
1898104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1899104f0cc2SMichael Ellerman 		       "0x%llx\n", SPU_ATTR_ACQUIRE);
190067207b96SArnd Bergmann 
1901197b1a82SChristoph Hellwig static int spufs_decr_set(void *data, u64 val)
19028b3d6663SArnd Bergmann {
19038b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
19048b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1905c9101bdbSChristoph Hellwig 	int ret;
1906c9101bdbSChristoph Hellwig 
1907c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1908c9101bdbSChristoph Hellwig 	if (ret)
1909c9101bdbSChristoph Hellwig 		return ret;
19108b3d6663SArnd Bergmann 	lscsa->decr.slot[0] = (u32) val;
191127b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1912197b1a82SChristoph Hellwig 
1913197b1a82SChristoph Hellwig 	return 0;
19148b3d6663SArnd Bergmann }
19158b3d6663SArnd Bergmann 
1916104f0cc2SMichael Ellerman static u64 spufs_decr_get(struct spu_context *ctx)
19178b3d6663SArnd Bergmann {
19188b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1919bf1ab978SDwayne Grant McConnell 	return lscsa->decr.slot[0];
1920bf1ab978SDwayne Grant McConnell }
1921104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1922104f0cc2SMichael Ellerman 		       "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
19238b3d6663SArnd Bergmann 
1924197b1a82SChristoph Hellwig static int spufs_decr_status_set(void *data, u64 val)
19258b3d6663SArnd Bergmann {
19268b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
1927c9101bdbSChristoph Hellwig 	int ret;
1928c9101bdbSChristoph Hellwig 
1929c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1930c9101bdbSChristoph Hellwig 	if (ret)
1931c9101bdbSChristoph Hellwig 		return ret;
1932d40a01d4SMasato Noguchi 	if (val)
1933d40a01d4SMasato Noguchi 		ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1934d40a01d4SMasato Noguchi 	else
1935d40a01d4SMasato Noguchi 		ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
193627b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1937197b1a82SChristoph Hellwig 
1938197b1a82SChristoph Hellwig 	return 0;
19398b3d6663SArnd Bergmann }
19408b3d6663SArnd Bergmann 
1941104f0cc2SMichael Ellerman static u64 spufs_decr_status_get(struct spu_context *ctx)
19428b3d6663SArnd Bergmann {
1943d40a01d4SMasato Noguchi 	if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1944d40a01d4SMasato Noguchi 		return SPU_DECR_STATUS_RUNNING;
1945d40a01d4SMasato Noguchi 	else
1946d40a01d4SMasato Noguchi 		return 0;
1947bf1ab978SDwayne Grant McConnell }
1948104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1949104f0cc2SMichael Ellerman 		       spufs_decr_status_set, "0x%llx\n",
1950104f0cc2SMichael Ellerman 		       SPU_ATTR_ACQUIRE_SAVED);
19518b3d6663SArnd Bergmann 
1952197b1a82SChristoph Hellwig static int spufs_event_mask_set(void *data, u64 val)
19538b3d6663SArnd Bergmann {
19548b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
19558b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1956c9101bdbSChristoph Hellwig 	int ret;
1957c9101bdbSChristoph Hellwig 
1958c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1959c9101bdbSChristoph Hellwig 	if (ret)
1960c9101bdbSChristoph Hellwig 		return ret;
19618b3d6663SArnd Bergmann 	lscsa->event_mask.slot[0] = (u32) val;
196227b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
1963197b1a82SChristoph Hellwig 
1964197b1a82SChristoph Hellwig 	return 0;
19658b3d6663SArnd Bergmann }
19668b3d6663SArnd Bergmann 
1967104f0cc2SMichael Ellerman static u64 spufs_event_mask_get(struct spu_context *ctx)
19688b3d6663SArnd Bergmann {
19698b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1970bf1ab978SDwayne Grant McConnell 	return lscsa->event_mask.slot[0];
1971bf1ab978SDwayne Grant McConnell }
1972bf1ab978SDwayne Grant McConnell 
1973104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1974104f0cc2SMichael Ellerman 		       spufs_event_mask_set, "0x%llx\n",
1975104f0cc2SMichael Ellerman 		       SPU_ATTR_ACQUIRE_SAVED);
19768b3d6663SArnd Bergmann 
1977104f0cc2SMichael Ellerman static u64 spufs_event_status_get(struct spu_context *ctx)
1978b9e3bd77SDwayne Grant McConnell {
1979b9e3bd77SDwayne Grant McConnell 	struct spu_state *state = &ctx->csa;
1980b9e3bd77SDwayne Grant McConnell 	u64 stat;
1981b9e3bd77SDwayne Grant McConnell 	stat = state->spu_chnlcnt_RW[0];
1982b9e3bd77SDwayne Grant McConnell 	if (stat)
1983bf1ab978SDwayne Grant McConnell 		return state->spu_chnldata_RW[0];
1984bf1ab978SDwayne Grant McConnell 	return 0;
1985bf1ab978SDwayne Grant McConnell }
1986104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1987104f0cc2SMichael Ellerman 		       NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
1988b9e3bd77SDwayne Grant McConnell 
1989197b1a82SChristoph Hellwig static int spufs_srr0_set(void *data, u64 val)
19908b3d6663SArnd Bergmann {
19918b3d6663SArnd Bergmann 	struct spu_context *ctx = data;
19928b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
1993c9101bdbSChristoph Hellwig 	int ret;
1994c9101bdbSChristoph Hellwig 
1995c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
1996c9101bdbSChristoph Hellwig 	if (ret)
1997c9101bdbSChristoph Hellwig 		return ret;
19988b3d6663SArnd Bergmann 	lscsa->srr0.slot[0] = (u32) val;
199927b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
2000197b1a82SChristoph Hellwig 
2001197b1a82SChristoph Hellwig 	return 0;
20028b3d6663SArnd Bergmann }
20038b3d6663SArnd Bergmann 
2004104f0cc2SMichael Ellerman static u64 spufs_srr0_get(struct spu_context *ctx)
20058b3d6663SArnd Bergmann {
20068b3d6663SArnd Bergmann 	struct spu_lscsa *lscsa = ctx->csa.lscsa;
2007104f0cc2SMichael Ellerman 	return lscsa->srr0.slot[0];
20088b3d6663SArnd Bergmann }
2009104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
2010104f0cc2SMichael Ellerman 		       "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
20118b3d6663SArnd Bergmann 
2012104f0cc2SMichael Ellerman static u64 spufs_id_get(struct spu_context *ctx)
20137b1a7014Sarnd@arndb.de {
20147b1a7014Sarnd@arndb.de 	u64 num;
20157b1a7014Sarnd@arndb.de 
20167b1a7014Sarnd@arndb.de 	if (ctx->state == SPU_STATE_RUNNABLE)
20177b1a7014Sarnd@arndb.de 		num = ctx->spu->number;
20187b1a7014Sarnd@arndb.de 	else
20197b1a7014Sarnd@arndb.de 		num = (unsigned int)-1;
20207b1a7014Sarnd@arndb.de 
20217b1a7014Sarnd@arndb.de 	return num;
20227b1a7014Sarnd@arndb.de }
2023104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
2024104f0cc2SMichael Ellerman 		       SPU_ATTR_ACQUIRE)
20257b1a7014Sarnd@arndb.de 
2026104f0cc2SMichael Ellerman static u64 spufs_object_id_get(struct spu_context *ctx)
2027bf1ab978SDwayne Grant McConnell {
2028bf1ab978SDwayne Grant McConnell 	/* FIXME: Should there really be no locking here? */
2029104f0cc2SMichael Ellerman 	return ctx->object_id;
2030bf1ab978SDwayne Grant McConnell }
2031bf1ab978SDwayne Grant McConnell 
2032197b1a82SChristoph Hellwig static int spufs_object_id_set(void *data, u64 id)
203386767277SArnd Bergmann {
203486767277SArnd Bergmann 	struct spu_context *ctx = data;
203586767277SArnd Bergmann 	ctx->object_id = id;
2036197b1a82SChristoph Hellwig 
2037197b1a82SChristoph Hellwig 	return 0;
203886767277SArnd Bergmann }
203986767277SArnd Bergmann 
2040104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2041104f0cc2SMichael Ellerman 		       spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
204286767277SArnd Bergmann 
2043104f0cc2SMichael Ellerman static u64 spufs_lslr_get(struct spu_context *ctx)
2044bf1ab978SDwayne Grant McConnell {
2045bf1ab978SDwayne Grant McConnell 	return ctx->csa.priv2.spu_lslr_RW;
2046bf1ab978SDwayne Grant McConnell }
2047104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2048104f0cc2SMichael Ellerman 		       SPU_ATTR_ACQUIRE_SAVED);
2049b9e3bd77SDwayne Grant McConnell 
2050b9e3bd77SDwayne Grant McConnell static int spufs_info_open(struct inode *inode, struct file *file)
2051b9e3bd77SDwayne Grant McConnell {
2052b9e3bd77SDwayne Grant McConnell 	struct spufs_inode_info *i = SPUFS_I(inode);
2053b9e3bd77SDwayne Grant McConnell 	struct spu_context *ctx = i->i_ctx;
2054b9e3bd77SDwayne Grant McConnell 	file->private_data = ctx;
2055b9e3bd77SDwayne Grant McConnell 	return 0;
2056b9e3bd77SDwayne Grant McConnell }
2057b9e3bd77SDwayne Grant McConnell 
2058cbe709c1SBenjamin Herrenschmidt static int spufs_caps_show(struct seq_file *s, void *private)
2059cbe709c1SBenjamin Herrenschmidt {
2060cbe709c1SBenjamin Herrenschmidt 	struct spu_context *ctx = s->private;
2061cbe709c1SBenjamin Herrenschmidt 
2062cbe709c1SBenjamin Herrenschmidt 	if (!(ctx->flags & SPU_CREATE_NOSCHED))
2063cbe709c1SBenjamin Herrenschmidt 		seq_puts(s, "sched\n");
2064cbe709c1SBenjamin Herrenschmidt 	if (!(ctx->flags & SPU_CREATE_ISOLATE))
2065cbe709c1SBenjamin Herrenschmidt 		seq_puts(s, "step\n");
2066cbe709c1SBenjamin Herrenschmidt 	return 0;
2067cbe709c1SBenjamin Herrenschmidt }
2068cbe709c1SBenjamin Herrenschmidt 
2069cbe709c1SBenjamin Herrenschmidt static int spufs_caps_open(struct inode *inode, struct file *file)
2070cbe709c1SBenjamin Herrenschmidt {
2071cbe709c1SBenjamin Herrenschmidt 	return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2072cbe709c1SBenjamin Herrenschmidt }
2073cbe709c1SBenjamin Herrenschmidt 
2074cbe709c1SBenjamin Herrenschmidt static const struct file_operations spufs_caps_fops = {
2075cbe709c1SBenjamin Herrenschmidt 	.open		= spufs_caps_open,
2076cbe709c1SBenjamin Herrenschmidt 	.read		= seq_read,
2077cbe709c1SBenjamin Herrenschmidt 	.llseek		= seq_lseek,
2078cbe709c1SBenjamin Herrenschmidt 	.release	= single_release,
2079cbe709c1SBenjamin Herrenschmidt };
2080cbe709c1SBenjamin Herrenschmidt 
2081bf1ab978SDwayne Grant McConnell static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2082bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2083bf1ab978SDwayne Grant McConnell {
2084bf1ab978SDwayne Grant McConnell 	u32 data;
2085bf1ab978SDwayne Grant McConnell 
2086cbea9238SJeremy Kerr 	/* EOF if there's no entry in the mbox */
2087cbea9238SJeremy Kerr 	if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2088cbea9238SJeremy Kerr 		return 0;
2089cbea9238SJeremy Kerr 
2090bf1ab978SDwayne Grant McConnell 	data = ctx->csa.prob.pu_mb_R;
2091bf1ab978SDwayne Grant McConnell 
2092bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2093bf1ab978SDwayne Grant McConnell }
2094bf1ab978SDwayne Grant McConnell 
209569a2f00cSDwayne Grant McConnell static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
209669a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
209769a2f00cSDwayne Grant McConnell {
2098bf1ab978SDwayne Grant McConnell 	int ret;
209969a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
210069a2f00cSDwayne Grant McConnell 
210169a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
210269a2f00cSDwayne Grant McConnell 		return -EFAULT;
210369a2f00cSDwayne Grant McConnell 
2104c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2105c9101bdbSChristoph Hellwig 	if (ret)
2106c9101bdbSChristoph Hellwig 		return ret;
210769a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2108bf1ab978SDwayne Grant McConnell 	ret = __spufs_mbox_info_read(ctx, buf, len, pos);
210969a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
211027b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
211169a2f00cSDwayne Grant McConnell 
2112bf1ab978SDwayne Grant McConnell 	return ret;
211369a2f00cSDwayne Grant McConnell }
211469a2f00cSDwayne Grant McConnell 
21155dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_info_fops = {
211669a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
211769a2f00cSDwayne Grant McConnell 	.read = spufs_mbox_info_read,
211869a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
211969a2f00cSDwayne Grant McConnell };
212069a2f00cSDwayne Grant McConnell 
2121bf1ab978SDwayne Grant McConnell static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2122bf1ab978SDwayne Grant McConnell 				char __user *buf, size_t len, loff_t *pos)
2123bf1ab978SDwayne Grant McConnell {
2124bf1ab978SDwayne Grant McConnell 	u32 data;
2125bf1ab978SDwayne Grant McConnell 
2126cbea9238SJeremy Kerr 	/* EOF if there's no entry in the ibox */
2127cbea9238SJeremy Kerr 	if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2128cbea9238SJeremy Kerr 		return 0;
2129cbea9238SJeremy Kerr 
2130bf1ab978SDwayne Grant McConnell 	data = ctx->csa.priv2.puint_mb_R;
2131bf1ab978SDwayne Grant McConnell 
2132bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2133bf1ab978SDwayne Grant McConnell }
2134bf1ab978SDwayne Grant McConnell 
213569a2f00cSDwayne Grant McConnell static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
213669a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
213769a2f00cSDwayne Grant McConnell {
213869a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2139bf1ab978SDwayne Grant McConnell 	int ret;
214069a2f00cSDwayne Grant McConnell 
214169a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
214269a2f00cSDwayne Grant McConnell 		return -EFAULT;
214369a2f00cSDwayne Grant McConnell 
2144c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2145c9101bdbSChristoph Hellwig 	if (ret)
2146c9101bdbSChristoph Hellwig 		return ret;
214769a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2148bf1ab978SDwayne Grant McConnell 	ret = __spufs_ibox_info_read(ctx, buf, len, pos);
214969a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
215027b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
215169a2f00cSDwayne Grant McConnell 
2152bf1ab978SDwayne Grant McConnell 	return ret;
215369a2f00cSDwayne Grant McConnell }
215469a2f00cSDwayne Grant McConnell 
21555dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_info_fops = {
215669a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
215769a2f00cSDwayne Grant McConnell 	.read = spufs_ibox_info_read,
215869a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
215969a2f00cSDwayne Grant McConnell };
216069a2f00cSDwayne Grant McConnell 
2161bf1ab978SDwayne Grant McConnell static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2162bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2163bf1ab978SDwayne Grant McConnell {
2164bf1ab978SDwayne Grant McConnell 	int i, cnt;
2165bf1ab978SDwayne Grant McConnell 	u32 data[4];
2166bf1ab978SDwayne Grant McConnell 	u32 wbox_stat;
2167bf1ab978SDwayne Grant McConnell 
2168bf1ab978SDwayne Grant McConnell 	wbox_stat = ctx->csa.prob.mb_stat_R;
2169bf1ab978SDwayne Grant McConnell 	cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2170bf1ab978SDwayne Grant McConnell 	for (i = 0; i < cnt; i++) {
2171bf1ab978SDwayne Grant McConnell 		data[i] = ctx->csa.spu_mailbox_data[i];
2172bf1ab978SDwayne Grant McConnell 	}
2173bf1ab978SDwayne Grant McConnell 
2174bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &data,
2175bf1ab978SDwayne Grant McConnell 				cnt * sizeof(u32));
2176bf1ab978SDwayne Grant McConnell }
2177bf1ab978SDwayne Grant McConnell 
217869a2f00cSDwayne Grant McConnell static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
217969a2f00cSDwayne Grant McConnell 				   size_t len, loff_t *pos)
218069a2f00cSDwayne Grant McConnell {
218169a2f00cSDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2182bf1ab978SDwayne Grant McConnell 	int ret;
218369a2f00cSDwayne Grant McConnell 
218469a2f00cSDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
218569a2f00cSDwayne Grant McConnell 		return -EFAULT;
218669a2f00cSDwayne Grant McConnell 
2187c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2188c9101bdbSChristoph Hellwig 	if (ret)
2189c9101bdbSChristoph Hellwig 		return ret;
219069a2f00cSDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2191bf1ab978SDwayne Grant McConnell 	ret = __spufs_wbox_info_read(ctx, buf, len, pos);
219269a2f00cSDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
219327b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
219469a2f00cSDwayne Grant McConnell 
2195bf1ab978SDwayne Grant McConnell 	return ret;
219669a2f00cSDwayne Grant McConnell }
219769a2f00cSDwayne Grant McConnell 
21985dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_info_fops = {
219969a2f00cSDwayne Grant McConnell 	.open = spufs_info_open,
220069a2f00cSDwayne Grant McConnell 	.read = spufs_wbox_info_read,
220169a2f00cSDwayne Grant McConnell 	.llseek  = generic_file_llseek,
220269a2f00cSDwayne Grant McConnell };
220369a2f00cSDwayne Grant McConnell 
2204bf1ab978SDwayne Grant McConnell static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2205bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2206b9e3bd77SDwayne Grant McConnell {
2207b9e3bd77SDwayne Grant McConnell 	struct spu_dma_info info;
2208b9e3bd77SDwayne Grant McConnell 	struct mfc_cq_sr *qp, *spuqp;
2209b9e3bd77SDwayne Grant McConnell 	int i;
2210b9e3bd77SDwayne Grant McConnell 
2211b9e3bd77SDwayne Grant McConnell 	info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2212b9e3bd77SDwayne Grant McConnell 	info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2213b9e3bd77SDwayne Grant McConnell 	info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2214b9e3bd77SDwayne Grant McConnell 	info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2215b9e3bd77SDwayne Grant McConnell 	info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2216b9e3bd77SDwayne Grant McConnell 	for (i = 0; i < 16; i++) {
2217b9e3bd77SDwayne Grant McConnell 		qp = &info.dma_info_command_data[i];
2218b9e3bd77SDwayne Grant McConnell 		spuqp = &ctx->csa.priv2.spuq[i];
2219b9e3bd77SDwayne Grant McConnell 
2220b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2221b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2222b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2223b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2224b9e3bd77SDwayne Grant McConnell 	}
2225b9e3bd77SDwayne Grant McConnell 
2226b9e3bd77SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &info,
2227b9e3bd77SDwayne Grant McConnell 				sizeof info);
2228b9e3bd77SDwayne Grant McConnell }
2229b9e3bd77SDwayne Grant McConnell 
2230bf1ab978SDwayne Grant McConnell static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2231bf1ab978SDwayne Grant McConnell 			      size_t len, loff_t *pos)
2232bf1ab978SDwayne Grant McConnell {
2233bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2234bf1ab978SDwayne Grant McConnell 	int ret;
2235bf1ab978SDwayne Grant McConnell 
2236bf1ab978SDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
2237bf1ab978SDwayne Grant McConnell 		return -EFAULT;
2238bf1ab978SDwayne Grant McConnell 
2239c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2240c9101bdbSChristoph Hellwig 	if (ret)
2241c9101bdbSChristoph Hellwig 		return ret;
2242bf1ab978SDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2243bf1ab978SDwayne Grant McConnell 	ret = __spufs_dma_info_read(ctx, buf, len, pos);
2244bf1ab978SDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
224527b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
2246bf1ab978SDwayne Grant McConnell 
2247bf1ab978SDwayne Grant McConnell 	return ret;
2248bf1ab978SDwayne Grant McConnell }
2249bf1ab978SDwayne Grant McConnell 
22505dfe4c96SArjan van de Ven static const struct file_operations spufs_dma_info_fops = {
2251b9e3bd77SDwayne Grant McConnell 	.open = spufs_info_open,
2252b9e3bd77SDwayne Grant McConnell 	.read = spufs_dma_info_read,
2253b9e3bd77SDwayne Grant McConnell };
2254b9e3bd77SDwayne Grant McConnell 
2255bf1ab978SDwayne Grant McConnell static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2256bf1ab978SDwayne Grant McConnell 			char __user *buf, size_t len, loff_t *pos)
2257b9e3bd77SDwayne Grant McConnell {
2258b9e3bd77SDwayne Grant McConnell 	struct spu_proxydma_info info;
2259b9e3bd77SDwayne Grant McConnell 	struct mfc_cq_sr *qp, *puqp;
2260bf1ab978SDwayne Grant McConnell 	int ret = sizeof info;
2261b9e3bd77SDwayne Grant McConnell 	int i;
2262b9e3bd77SDwayne Grant McConnell 
2263b9e3bd77SDwayne Grant McConnell 	if (len < ret)
2264b9e3bd77SDwayne Grant McConnell 		return -EINVAL;
2265b9e3bd77SDwayne Grant McConnell 
2266b9e3bd77SDwayne Grant McConnell 	if (!access_ok(VERIFY_WRITE, buf, len))
2267b9e3bd77SDwayne Grant McConnell 		return -EFAULT;
2268b9e3bd77SDwayne Grant McConnell 
2269b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2270b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2271b9e3bd77SDwayne Grant McConnell 	info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2272b9e3bd77SDwayne Grant McConnell 	for (i = 0; i < 8; i++) {
2273b9e3bd77SDwayne Grant McConnell 		qp = &info.proxydma_info_command_data[i];
2274b9e3bd77SDwayne Grant McConnell 		puqp = &ctx->csa.priv2.puq[i];
2275b9e3bd77SDwayne Grant McConnell 
2276b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2277b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2278b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2279b9e3bd77SDwayne Grant McConnell 		qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2280b9e3bd77SDwayne Grant McConnell 	}
2281bf1ab978SDwayne Grant McConnell 
2282bf1ab978SDwayne Grant McConnell 	return simple_read_from_buffer(buf, len, pos, &info,
2283bf1ab978SDwayne Grant McConnell 				sizeof info);
2284bf1ab978SDwayne Grant McConnell }
2285bf1ab978SDwayne Grant McConnell 
2286bf1ab978SDwayne Grant McConnell static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2287bf1ab978SDwayne Grant McConnell 				   size_t len, loff_t *pos)
2288bf1ab978SDwayne Grant McConnell {
2289bf1ab978SDwayne Grant McConnell 	struct spu_context *ctx = file->private_data;
2290bf1ab978SDwayne Grant McConnell 	int ret;
2291bf1ab978SDwayne Grant McConnell 
2292c9101bdbSChristoph Hellwig 	ret = spu_acquire_saved(ctx);
2293c9101bdbSChristoph Hellwig 	if (ret)
2294c9101bdbSChristoph Hellwig 		return ret;
2295bf1ab978SDwayne Grant McConnell 	spin_lock(&ctx->csa.register_lock);
2296bf1ab978SDwayne Grant McConnell 	ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
2297b9e3bd77SDwayne Grant McConnell 	spin_unlock(&ctx->csa.register_lock);
229827b1ea09SChristoph Hellwig 	spu_release_saved(ctx);
2299b9e3bd77SDwayne Grant McConnell 
2300b9e3bd77SDwayne Grant McConnell 	return ret;
2301b9e3bd77SDwayne Grant McConnell }
2302b9e3bd77SDwayne Grant McConnell 
23035dfe4c96SArjan van de Ven static const struct file_operations spufs_proxydma_info_fops = {
2304b9e3bd77SDwayne Grant McConnell 	.open = spufs_info_open,
2305b9e3bd77SDwayne Grant McConnell 	.read = spufs_proxydma_info_read,
2306b9e3bd77SDwayne Grant McConnell };
2307b9e3bd77SDwayne Grant McConnell 
2308476273adSChristoph Hellwig static int spufs_show_tid(struct seq_file *s, void *private)
2309476273adSChristoph Hellwig {
2310476273adSChristoph Hellwig 	struct spu_context *ctx = s->private;
2311476273adSChristoph Hellwig 
2312476273adSChristoph Hellwig 	seq_printf(s, "%d\n", ctx->tid);
2313476273adSChristoph Hellwig 	return 0;
2314476273adSChristoph Hellwig }
2315476273adSChristoph Hellwig 
2316476273adSChristoph Hellwig static int spufs_tid_open(struct inode *inode, struct file *file)
2317476273adSChristoph Hellwig {
2318476273adSChristoph Hellwig 	return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2319476273adSChristoph Hellwig }
2320476273adSChristoph Hellwig 
2321476273adSChristoph Hellwig static const struct file_operations spufs_tid_fops = {
2322476273adSChristoph Hellwig 	.open		= spufs_tid_open,
2323476273adSChristoph Hellwig 	.read		= seq_read,
2324476273adSChristoph Hellwig 	.llseek		= seq_lseek,
2325476273adSChristoph Hellwig 	.release	= single_release,
2326476273adSChristoph Hellwig };
2327476273adSChristoph Hellwig 
2328e9f8a0b6SChristoph Hellwig static const char *ctx_state_names[] = {
2329e9f8a0b6SChristoph Hellwig 	"user", "system", "iowait", "loaded"
2330e9f8a0b6SChristoph Hellwig };
2331e9f8a0b6SChristoph Hellwig 
2332e9f8a0b6SChristoph Hellwig static unsigned long long spufs_acct_time(struct spu_context *ctx,
233327ec41d3SAndre Detsch 		enum spu_utilization_state state)
2334e9f8a0b6SChristoph Hellwig {
233527ec41d3SAndre Detsch 	struct timespec ts;
233627ec41d3SAndre Detsch 	unsigned long long time = ctx->stats.times[state];
2337e9f8a0b6SChristoph Hellwig 
233827ec41d3SAndre Detsch 	/*
233927ec41d3SAndre Detsch 	 * In general, utilization statistics are updated by the controlling
234027ec41d3SAndre Detsch 	 * thread as the spu context moves through various well defined
234127ec41d3SAndre Detsch 	 * state transitions, but if the context is lazily loaded its
234227ec41d3SAndre Detsch 	 * utilization statistics are not updated as the controlling thread
234327ec41d3SAndre Detsch 	 * is not tightly coupled with the execution of the spu context.  We
234427ec41d3SAndre Detsch 	 * calculate and apply the time delta from the last recorded state
234527ec41d3SAndre Detsch 	 * of the spu context.
234627ec41d3SAndre Detsch 	 */
234727ec41d3SAndre Detsch 	if (ctx->spu && ctx->stats.util_state == state) {
234827ec41d3SAndre Detsch 		ktime_get_ts(&ts);
234927ec41d3SAndre Detsch 		time += timespec_to_ns(&ts) - ctx->stats.tstamp;
235027ec41d3SAndre Detsch 	}
2351e9f8a0b6SChristoph Hellwig 
235227ec41d3SAndre Detsch 	return time / NSEC_PER_MSEC;
2353e9f8a0b6SChristoph Hellwig }
2354e9f8a0b6SChristoph Hellwig 
2355e9f8a0b6SChristoph Hellwig static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2356e9f8a0b6SChristoph Hellwig {
2357e9f8a0b6SChristoph Hellwig 	unsigned long long slb_flts = ctx->stats.slb_flt;
2358e9f8a0b6SChristoph Hellwig 
2359e9f8a0b6SChristoph Hellwig 	if (ctx->state == SPU_STATE_RUNNABLE) {
2360e9f8a0b6SChristoph Hellwig 		slb_flts += (ctx->spu->stats.slb_flt -
2361e9f8a0b6SChristoph Hellwig 			     ctx->stats.slb_flt_base);
2362e9f8a0b6SChristoph Hellwig 	}
2363e9f8a0b6SChristoph Hellwig 
2364e9f8a0b6SChristoph Hellwig 	return slb_flts;
2365e9f8a0b6SChristoph Hellwig }
2366e9f8a0b6SChristoph Hellwig 
2367e9f8a0b6SChristoph Hellwig static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2368e9f8a0b6SChristoph Hellwig {
2369e9f8a0b6SChristoph Hellwig 	unsigned long long class2_intrs = ctx->stats.class2_intr;
2370e9f8a0b6SChristoph Hellwig 
2371e9f8a0b6SChristoph Hellwig 	if (ctx->state == SPU_STATE_RUNNABLE) {
2372e9f8a0b6SChristoph Hellwig 		class2_intrs += (ctx->spu->stats.class2_intr -
2373e9f8a0b6SChristoph Hellwig 				 ctx->stats.class2_intr_base);
2374e9f8a0b6SChristoph Hellwig 	}
2375e9f8a0b6SChristoph Hellwig 
2376e9f8a0b6SChristoph Hellwig 	return class2_intrs;
2377e9f8a0b6SChristoph Hellwig }
2378e9f8a0b6SChristoph Hellwig 
2379e9f8a0b6SChristoph Hellwig 
2380e9f8a0b6SChristoph Hellwig static int spufs_show_stat(struct seq_file *s, void *private)
2381e9f8a0b6SChristoph Hellwig {
2382e9f8a0b6SChristoph Hellwig 	struct spu_context *ctx = s->private;
2383c9101bdbSChristoph Hellwig 	int ret;
2384e9f8a0b6SChristoph Hellwig 
2385c9101bdbSChristoph Hellwig 	ret = spu_acquire(ctx);
2386c9101bdbSChristoph Hellwig 	if (ret)
2387c9101bdbSChristoph Hellwig 		return ret;
2388c9101bdbSChristoph Hellwig 
2389e9f8a0b6SChristoph Hellwig 	seq_printf(s, "%s %llu %llu %llu %llu "
2390e9f8a0b6SChristoph Hellwig 		      "%llu %llu %llu %llu %llu %llu %llu %llu\n",
239127ec41d3SAndre Detsch 		ctx_state_names[ctx->stats.util_state],
239227ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_USER),
239327ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
239427ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
239527ec41d3SAndre Detsch 		spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
2396e9f8a0b6SChristoph Hellwig 		ctx->stats.vol_ctx_switch,
2397e9f8a0b6SChristoph Hellwig 		ctx->stats.invol_ctx_switch,
2398e9f8a0b6SChristoph Hellwig 		spufs_slb_flts(ctx),
2399e9f8a0b6SChristoph Hellwig 		ctx->stats.hash_flt,
2400e9f8a0b6SChristoph Hellwig 		ctx->stats.min_flt,
2401e9f8a0b6SChristoph Hellwig 		ctx->stats.maj_flt,
2402e9f8a0b6SChristoph Hellwig 		spufs_class2_intrs(ctx),
2403e9f8a0b6SChristoph Hellwig 		ctx->stats.libassist);
2404e9f8a0b6SChristoph Hellwig 	spu_release(ctx);
2405e9f8a0b6SChristoph Hellwig 	return 0;
2406e9f8a0b6SChristoph Hellwig }
2407e9f8a0b6SChristoph Hellwig 
2408e9f8a0b6SChristoph Hellwig static int spufs_stat_open(struct inode *inode, struct file *file)
2409e9f8a0b6SChristoph Hellwig {
2410e9f8a0b6SChristoph Hellwig 	return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2411e9f8a0b6SChristoph Hellwig }
2412e9f8a0b6SChristoph Hellwig 
2413e9f8a0b6SChristoph Hellwig static const struct file_operations spufs_stat_fops = {
2414e9f8a0b6SChristoph Hellwig 	.open		= spufs_stat_open,
2415e9f8a0b6SChristoph Hellwig 	.read		= seq_read,
2416e9f8a0b6SChristoph Hellwig 	.llseek		= seq_lseek,
2417e9f8a0b6SChristoph Hellwig 	.release	= single_release,
2418e9f8a0b6SChristoph Hellwig };
2419e9f8a0b6SChristoph Hellwig 
24205158e9b5SChristoph Hellwig static inline int spufs_switch_log_used(struct spu_context *ctx)
24215158e9b5SChristoph Hellwig {
24225158e9b5SChristoph Hellwig 	return (ctx->switch_log->head - ctx->switch_log->tail) %
24235158e9b5SChristoph Hellwig 		SWITCH_LOG_BUFSIZE;
24245158e9b5SChristoph Hellwig }
24255158e9b5SChristoph Hellwig 
24265158e9b5SChristoph Hellwig static inline int spufs_switch_log_avail(struct spu_context *ctx)
24275158e9b5SChristoph Hellwig {
24285158e9b5SChristoph Hellwig 	return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
24295158e9b5SChristoph Hellwig }
24305158e9b5SChristoph Hellwig 
24315158e9b5SChristoph Hellwig static int spufs_switch_log_open(struct inode *inode, struct file *file)
24325158e9b5SChristoph Hellwig {
24335158e9b5SChristoph Hellwig 	struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2434f5ed0eb6SJeremy Kerr 	int rc;
24355158e9b5SChristoph Hellwig 
2436f5ed0eb6SJeremy Kerr 	rc = spu_acquire(ctx);
2437f5ed0eb6SJeremy Kerr 	if (rc)
2438f5ed0eb6SJeremy Kerr 		return rc;
2439f5ed0eb6SJeremy Kerr 
24405158e9b5SChristoph Hellwig 	if (ctx->switch_log) {
2441f5ed0eb6SJeremy Kerr 		rc = -EBUSY;
2442f5ed0eb6SJeremy Kerr 		goto out;
2443f5ed0eb6SJeremy Kerr 	}
2444f5ed0eb6SJeremy Kerr 
2445837ef884SJeremy Kerr 	ctx->switch_log = kmalloc(sizeof(struct switch_log) +
24465158e9b5SChristoph Hellwig 		SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
24475158e9b5SChristoph Hellwig 		GFP_KERNEL);
2448f5ed0eb6SJeremy Kerr 
2449f5ed0eb6SJeremy Kerr 	if (!ctx->switch_log) {
2450f5ed0eb6SJeremy Kerr 		rc = -ENOMEM;
24515158e9b5SChristoph Hellwig 		goto out;
24525158e9b5SChristoph Hellwig 	}
2453f5ed0eb6SJeremy Kerr 
2454837ef884SJeremy Kerr 	ctx->switch_log->head = ctx->switch_log->tail = 0;
2455f5ed0eb6SJeremy Kerr 	init_waitqueue_head(&ctx->switch_log->wait);
2456f5ed0eb6SJeremy Kerr 	rc = 0;
2457f5ed0eb6SJeremy Kerr 
2458f5ed0eb6SJeremy Kerr out:
2459f5ed0eb6SJeremy Kerr 	spu_release(ctx);
2460f5ed0eb6SJeremy Kerr 	return rc;
2461f5ed0eb6SJeremy Kerr }
2462f5ed0eb6SJeremy Kerr 
2463f5ed0eb6SJeremy Kerr static int spufs_switch_log_release(struct inode *inode, struct file *file)
2464f5ed0eb6SJeremy Kerr {
2465f5ed0eb6SJeremy Kerr 	struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2466f5ed0eb6SJeremy Kerr 	int rc;
2467f5ed0eb6SJeremy Kerr 
2468f5ed0eb6SJeremy Kerr 	rc = spu_acquire(ctx);
2469f5ed0eb6SJeremy Kerr 	if (rc)
2470f5ed0eb6SJeremy Kerr 		return rc;
2471f5ed0eb6SJeremy Kerr 
2472f5ed0eb6SJeremy Kerr 	kfree(ctx->switch_log);
2473f5ed0eb6SJeremy Kerr 	ctx->switch_log = NULL;
2474f5ed0eb6SJeremy Kerr 	spu_release(ctx);
24755158e9b5SChristoph Hellwig 
24765158e9b5SChristoph Hellwig 	return 0;
24775158e9b5SChristoph Hellwig }
24785158e9b5SChristoph Hellwig 
24795158e9b5SChristoph Hellwig static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
24805158e9b5SChristoph Hellwig {
24815158e9b5SChristoph Hellwig 	struct switch_log_entry *p;
24825158e9b5SChristoph Hellwig 
24835158e9b5SChristoph Hellwig 	p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
24845158e9b5SChristoph Hellwig 
24855158e9b5SChristoph Hellwig 	return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
24865158e9b5SChristoph Hellwig 			(unsigned int) p->tstamp.tv_sec,
24875158e9b5SChristoph Hellwig 			(unsigned int) p->tstamp.tv_nsec,
24885158e9b5SChristoph Hellwig 			p->spu_id,
24895158e9b5SChristoph Hellwig 			(unsigned int) p->type,
24905158e9b5SChristoph Hellwig 			(unsigned int) p->val,
24915158e9b5SChristoph Hellwig 			(unsigned long long) p->timebase);
24925158e9b5SChristoph Hellwig }
24935158e9b5SChristoph Hellwig 
24945158e9b5SChristoph Hellwig static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
24955158e9b5SChristoph Hellwig 			     size_t len, loff_t *ppos)
24965158e9b5SChristoph Hellwig {
24975158e9b5SChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
24985158e9b5SChristoph Hellwig 	struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
24995158e9b5SChristoph Hellwig 	int error = 0, cnt = 0;
25005158e9b5SChristoph Hellwig 
25015158e9b5SChristoph Hellwig 	if (!buf || len < 0)
25025158e9b5SChristoph Hellwig 		return -EINVAL;
25035158e9b5SChristoph Hellwig 
2504f5ed0eb6SJeremy Kerr 	error = spu_acquire(ctx);
2505f5ed0eb6SJeremy Kerr 	if (error)
2506f5ed0eb6SJeremy Kerr 		return error;
2507f5ed0eb6SJeremy Kerr 
25085158e9b5SChristoph Hellwig 	while (cnt < len) {
25095158e9b5SChristoph Hellwig 		char tbuf[128];
25105158e9b5SChristoph Hellwig 		int width;
25115158e9b5SChristoph Hellwig 
2512f5ed0eb6SJeremy Kerr 		if (spufs_switch_log_used(ctx) == 0) {
251314f693eeSJeremy Kerr 			if (cnt > 0) {
251414f693eeSJeremy Kerr 				/* If there's data ready to go, we can
251514f693eeSJeremy Kerr 				 * just return straight away */
251614f693eeSJeremy Kerr 				break;
251714f693eeSJeremy Kerr 
251814f693eeSJeremy Kerr 			} else if (file->f_flags & O_NONBLOCK) {
2519f5ed0eb6SJeremy Kerr 				error = -EAGAIN;
25205158e9b5SChristoph Hellwig 				break;
252114f693eeSJeremy Kerr 
2522f5ed0eb6SJeremy Kerr 			} else {
252314f693eeSJeremy Kerr 				/* spufs_wait will drop the mutex and
252414f693eeSJeremy Kerr 				 * re-acquire, but since we're in read(), the
252514f693eeSJeremy Kerr 				 * file cannot be _released (and so
252614f693eeSJeremy Kerr 				 * ctx->switch_log is stable).
2527f5ed0eb6SJeremy Kerr 				 */
2528f5ed0eb6SJeremy Kerr 				error = spufs_wait(ctx->switch_log->wait,
2529f5ed0eb6SJeremy Kerr 						spufs_switch_log_used(ctx) > 0);
25305158e9b5SChristoph Hellwig 
2531f5ed0eb6SJeremy Kerr 				/* On error, spufs_wait returns without the
2532f5ed0eb6SJeremy Kerr 				 * state mutex held */
2533f5ed0eb6SJeremy Kerr 				if (error)
2534f5ed0eb6SJeremy Kerr 					return error;
25355158e9b5SChristoph Hellwig 
253614f693eeSJeremy Kerr 				/* We may have had entries read from underneath
253714f693eeSJeremy Kerr 				 * us while we dropped the mutex in spufs_wait,
253814f693eeSJeremy Kerr 				 * so re-check */
253914f693eeSJeremy Kerr 				if (spufs_switch_log_used(ctx) == 0)
2540f5ed0eb6SJeremy Kerr 					continue;
254114f693eeSJeremy Kerr 			}
254214f693eeSJeremy Kerr 		}
2543f5ed0eb6SJeremy Kerr 
25445158e9b5SChristoph Hellwig 		width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
2545f5ed0eb6SJeremy Kerr 		if (width < len)
25465158e9b5SChristoph Hellwig 			ctx->switch_log->tail =
25475158e9b5SChristoph Hellwig 				(ctx->switch_log->tail + 1) %
25485158e9b5SChristoph Hellwig 				 SWITCH_LOG_BUFSIZE;
2549f5ed0eb6SJeremy Kerr 		else
2550f5ed0eb6SJeremy Kerr 			/* If the record is greater than space available return
2551f5ed0eb6SJeremy Kerr 			 * partial buffer (so far) */
25525158e9b5SChristoph Hellwig 			break;
25535158e9b5SChristoph Hellwig 
25545158e9b5SChristoph Hellwig 		error = copy_to_user(buf + cnt, tbuf, width);
25555158e9b5SChristoph Hellwig 		if (error)
25565158e9b5SChristoph Hellwig 			break;
25575158e9b5SChristoph Hellwig 		cnt += width;
25585158e9b5SChristoph Hellwig 	}
25595158e9b5SChristoph Hellwig 
2560f5ed0eb6SJeremy Kerr 	spu_release(ctx);
2561f5ed0eb6SJeremy Kerr 
25625158e9b5SChristoph Hellwig 	return cnt == 0 ? error : cnt;
25635158e9b5SChristoph Hellwig }
25645158e9b5SChristoph Hellwig 
25655158e9b5SChristoph Hellwig static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
25665158e9b5SChristoph Hellwig {
25675158e9b5SChristoph Hellwig 	struct inode *inode = file->f_path.dentry->d_inode;
25685158e9b5SChristoph Hellwig 	struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
25695158e9b5SChristoph Hellwig 	unsigned int mask = 0;
2570f5ed0eb6SJeremy Kerr 	int rc;
25715158e9b5SChristoph Hellwig 
25725158e9b5SChristoph Hellwig 	poll_wait(file, &ctx->switch_log->wait, wait);
25735158e9b5SChristoph Hellwig 
2574f5ed0eb6SJeremy Kerr 	rc = spu_acquire(ctx);
2575f5ed0eb6SJeremy Kerr 	if (rc)
2576f5ed0eb6SJeremy Kerr 		return rc;
2577f5ed0eb6SJeremy Kerr 
25785158e9b5SChristoph Hellwig 	if (spufs_switch_log_used(ctx) > 0)
25795158e9b5SChristoph Hellwig 		mask |= POLLIN;
25805158e9b5SChristoph Hellwig 
2581f5ed0eb6SJeremy Kerr 	spu_release(ctx);
2582f5ed0eb6SJeremy Kerr 
25835158e9b5SChristoph Hellwig 	return mask;
25845158e9b5SChristoph Hellwig }
25855158e9b5SChristoph Hellwig 
25865158e9b5SChristoph Hellwig static const struct file_operations spufs_switch_log_fops = {
25875158e9b5SChristoph Hellwig 	.owner		= THIS_MODULE,
25885158e9b5SChristoph Hellwig 	.open		= spufs_switch_log_open,
25895158e9b5SChristoph Hellwig 	.read		= spufs_switch_log_read,
25905158e9b5SChristoph Hellwig 	.poll		= spufs_switch_log_poll,
2591f5ed0eb6SJeremy Kerr 	.release	= spufs_switch_log_release,
25925158e9b5SChristoph Hellwig };
25935158e9b5SChristoph Hellwig 
2594f5ed0eb6SJeremy Kerr /**
2595f5ed0eb6SJeremy Kerr  * Log a context switch event to a switch log reader.
2596f5ed0eb6SJeremy Kerr  *
2597f5ed0eb6SJeremy Kerr  * Must be called with ctx->state_mutex held.
2598f5ed0eb6SJeremy Kerr  */
25995158e9b5SChristoph Hellwig void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
26005158e9b5SChristoph Hellwig 		u32 type, u32 val)
26015158e9b5SChristoph Hellwig {
26025158e9b5SChristoph Hellwig 	if (!ctx->switch_log)
26035158e9b5SChristoph Hellwig 		return;
26045158e9b5SChristoph Hellwig 
26055158e9b5SChristoph Hellwig 	if (spufs_switch_log_avail(ctx) > 1) {
26065158e9b5SChristoph Hellwig 		struct switch_log_entry *p;
26075158e9b5SChristoph Hellwig 
26085158e9b5SChristoph Hellwig 		p = ctx->switch_log->log + ctx->switch_log->head;
26095158e9b5SChristoph Hellwig 		ktime_get_ts(&p->tstamp);
26105158e9b5SChristoph Hellwig 		p->timebase = get_tb();
26115158e9b5SChristoph Hellwig 		p->spu_id = spu ? spu->number : -1;
26125158e9b5SChristoph Hellwig 		p->type = type;
26135158e9b5SChristoph Hellwig 		p->val = val;
26145158e9b5SChristoph Hellwig 
26155158e9b5SChristoph Hellwig 		ctx->switch_log->head =
26165158e9b5SChristoph Hellwig 			(ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
26175158e9b5SChristoph Hellwig 	}
26185158e9b5SChristoph Hellwig 
26195158e9b5SChristoph Hellwig 	wake_up(&ctx->switch_log->wait);
26205158e9b5SChristoph Hellwig }
2621e9f8a0b6SChristoph Hellwig 
262246deed69SLuke Browning static int spufs_show_ctx(struct seq_file *s, void *private)
262346deed69SLuke Browning {
262446deed69SLuke Browning 	struct spu_context *ctx = s->private;
262546deed69SLuke Browning 	u64 mfc_control_RW;
262646deed69SLuke Browning 
262746deed69SLuke Browning 	mutex_lock(&ctx->state_mutex);
262846deed69SLuke Browning 	if (ctx->spu) {
262946deed69SLuke Browning 		struct spu *spu = ctx->spu;
263046deed69SLuke Browning 		struct spu_priv2 __iomem *priv2 = spu->priv2;
263146deed69SLuke Browning 
263246deed69SLuke Browning 		spin_lock_irq(&spu->register_lock);
263346deed69SLuke Browning 		mfc_control_RW = in_be64(&priv2->mfc_control_RW);
263446deed69SLuke Browning 		spin_unlock_irq(&spu->register_lock);
263546deed69SLuke Browning 	} else {
263646deed69SLuke Browning 		struct spu_state *csa = &ctx->csa;
263746deed69SLuke Browning 
263846deed69SLuke Browning 		mfc_control_RW = csa->priv2.mfc_control_RW;
263946deed69SLuke Browning 	}
264046deed69SLuke Browning 
264146deed69SLuke Browning 	seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
264246deed69SLuke Browning 		" %c %lx %lx %lx %lx %x %x\n",
264346deed69SLuke Browning 		ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
264446deed69SLuke Browning 		ctx->flags,
264546deed69SLuke Browning 		ctx->sched_flags,
264646deed69SLuke Browning 		ctx->prio,
264746deed69SLuke Browning 		ctx->time_slice,
264846deed69SLuke Browning 		ctx->spu ? ctx->spu->number : -1,
264946deed69SLuke Browning 		!list_empty(&ctx->rq) ? 'q' : ' ',
265046deed69SLuke Browning 		ctx->csa.class_0_pending,
265146deed69SLuke Browning 		ctx->csa.class_0_dar,
265246deed69SLuke Browning 		ctx->csa.class_1_dsisr,
265346deed69SLuke Browning 		mfc_control_RW,
265446deed69SLuke Browning 		ctx->ops->runcntl_read(ctx),
265546deed69SLuke Browning 		ctx->ops->status_read(ctx));
265646deed69SLuke Browning 
265746deed69SLuke Browning 	mutex_unlock(&ctx->state_mutex);
265846deed69SLuke Browning 
265946deed69SLuke Browning 	return 0;
266046deed69SLuke Browning }
266146deed69SLuke Browning 
266246deed69SLuke Browning static int spufs_ctx_open(struct inode *inode, struct file *file)
266346deed69SLuke Browning {
266446deed69SLuke Browning 	return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
266546deed69SLuke Browning }
266646deed69SLuke Browning 
266746deed69SLuke Browning static const struct file_operations spufs_ctx_fops = {
266846deed69SLuke Browning 	.open           = spufs_ctx_open,
266946deed69SLuke Browning 	.read           = seq_read,
267046deed69SLuke Browning 	.llseek         = seq_lseek,
267146deed69SLuke Browning 	.release        = single_release,
267246deed69SLuke Browning };
267346deed69SLuke Browning 
267423d893f5SJeremy Kerr struct spufs_tree_descr spufs_dir_contents[] = {
2675cbe709c1SBenjamin Herrenschmidt 	{ "capabilities", &spufs_caps_fops, 0444, },
26766f7dde81SJeremy Kerr 	{ "mem",  &spufs_mem_fops,  0666, LS_SIZE, },
26776f7dde81SJeremy Kerr 	{ "regs", &spufs_regs_fops,  0666, sizeof(struct spu_reg128[128]), },
267867207b96SArnd Bergmann 	{ "mbox", &spufs_mbox_fops, 0444, },
267967207b96SArnd Bergmann 	{ "ibox", &spufs_ibox_fops, 0444, },
268067207b96SArnd Bergmann 	{ "wbox", &spufs_wbox_fops, 0222, },
26816f7dde81SJeremy Kerr 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
26826f7dde81SJeremy Kerr 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
26836f7dde81SJeremy Kerr 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2684603c4612SJeremy Kerr 	{ "signal1", &spufs_signal1_fops, 0666, },
2685603c4612SJeremy Kerr 	{ "signal2", &spufs_signal2_fops, 0666, },
268667207b96SArnd Bergmann 	{ "signal1_type", &spufs_signal1_type, 0666, },
268767207b96SArnd Bergmann 	{ "signal2_type", &spufs_signal2_type, 0666, },
26886df10a82SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
26896f7dde81SJeremy Kerr 	{ "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
2690b9e3bd77SDwayne Grant McConnell 	{ "lslr", &spufs_lslr_ops, 0444, },
2691b9e3bd77SDwayne Grant McConnell 	{ "mfc", &spufs_mfc_fops, 0666, },
2692b9e3bd77SDwayne Grant McConnell 	{ "mss", &spufs_mss_fops, 0666, },
2693b9e3bd77SDwayne Grant McConnell 	{ "npc", &spufs_npc_ops, 0666, },
2694b9e3bd77SDwayne Grant McConnell 	{ "srr0", &spufs_srr0_ops, 0666, },
26958b3d6663SArnd Bergmann 	{ "decr", &spufs_decr_ops, 0666, },
26968b3d6663SArnd Bergmann 	{ "decr_status", &spufs_decr_status_ops, 0666, },
26978b3d6663SArnd Bergmann 	{ "event_mask", &spufs_event_mask_ops, 0666, },
2698b9e3bd77SDwayne Grant McConnell 	{ "event_status", &spufs_event_status_ops, 0444, },
26996f7dde81SJeremy Kerr 	{ "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
270086767277SArnd Bergmann 	{ "phys-id", &spufs_id_ops, 0666, },
270186767277SArnd Bergmann 	{ "object-id", &spufs_object_id_ops, 0666, },
27026f7dde81SJeremy Kerr 	{ "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
27036f7dde81SJeremy Kerr 	{ "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
27046f7dde81SJeremy Kerr 	{ "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
27056f7dde81SJeremy Kerr 	{ "dma_info", &spufs_dma_info_fops, 0444,
27066f7dde81SJeremy Kerr 		sizeof(struct spu_dma_info), },
27076f7dde81SJeremy Kerr 	{ "proxydma_info", &spufs_proxydma_info_fops, 0444,
27086f7dde81SJeremy Kerr 		sizeof(struct spu_proxydma_info)},
2709476273adSChristoph Hellwig 	{ "tid", &spufs_tid_fops, 0444, },
2710e9f8a0b6SChristoph Hellwig 	{ "stat", &spufs_stat_fops, 0444, },
27115158e9b5SChristoph Hellwig 	{ "switch_log", &spufs_switch_log_fops, 0444 },
271267207b96SArnd Bergmann 	{},
271367207b96SArnd Bergmann };
27145737edd1SMark Nutter 
271523d893f5SJeremy Kerr struct spufs_tree_descr spufs_dir_nosched_contents[] = {
2716cbe709c1SBenjamin Herrenschmidt 	{ "capabilities", &spufs_caps_fops, 0444, },
27176f7dde81SJeremy Kerr 	{ "mem",  &spufs_mem_fops,  0666, LS_SIZE, },
27185737edd1SMark Nutter 	{ "mbox", &spufs_mbox_fops, 0444, },
27195737edd1SMark Nutter 	{ "ibox", &spufs_ibox_fops, 0444, },
27205737edd1SMark Nutter 	{ "wbox", &spufs_wbox_fops, 0222, },
27216f7dde81SJeremy Kerr 	{ "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
27226f7dde81SJeremy Kerr 	{ "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
27236f7dde81SJeremy Kerr 	{ "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
2724d054b36fSJeremy Kerr 	{ "signal1", &spufs_signal1_nosched_fops, 0222, },
2725d054b36fSJeremy Kerr 	{ "signal2", &spufs_signal2_nosched_fops, 0222, },
27265737edd1SMark Nutter 	{ "signal1_type", &spufs_signal1_type, 0666, },
27275737edd1SMark Nutter 	{ "signal2_type", &spufs_signal2_type, 0666, },
27285737edd1SMark Nutter 	{ "mss", &spufs_mss_fops, 0666, },
27295737edd1SMark Nutter 	{ "mfc", &spufs_mfc_fops, 0666, },
27305737edd1SMark Nutter 	{ "cntl", &spufs_cntl_fops,  0666, },
27315737edd1SMark Nutter 	{ "npc", &spufs_npc_ops, 0666, },
27326f7dde81SJeremy Kerr 	{ "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
27335737edd1SMark Nutter 	{ "phys-id", &spufs_id_ops, 0666, },
27345737edd1SMark Nutter 	{ "object-id", &spufs_object_id_ops, 0666, },
2735476273adSChristoph Hellwig 	{ "tid", &spufs_tid_fops, 0444, },
2736e9f8a0b6SChristoph Hellwig 	{ "stat", &spufs_stat_fops, 0444, },
27372c3e4787SJeremy Kerr 	{},
27382c3e4787SJeremy Kerr };
27392c3e4787SJeremy Kerr 
27402c3e4787SJeremy Kerr struct spufs_tree_descr spufs_dir_debug_contents[] = {
274146deed69SLuke Browning 	{ ".ctx", &spufs_ctx_fops, 0444, },
27425737edd1SMark Nutter 	{},
27435737edd1SMark Nutter };
2744bf1ab978SDwayne Grant McConnell 
2745bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader spufs_coredump_read[] = {
27464fca9c42SMichael Ellerman 	{ "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
27474fca9c42SMichael Ellerman 	{ "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
2748104f0cc2SMichael Ellerman 	{ "lslr", NULL, spufs_lslr_get, 19 },
2749104f0cc2SMichael Ellerman 	{ "decr", NULL, spufs_decr_get, 19 },
2750104f0cc2SMichael Ellerman 	{ "decr_status", NULL, spufs_decr_status_get, 19 },
27514fca9c42SMichael Ellerman 	{ "mem", __spufs_mem_read, NULL, LS_SIZE, },
27524fca9c42SMichael Ellerman 	{ "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
2753104f0cc2SMichael Ellerman 	{ "signal1_type", NULL, spufs_signal1_type_get, 19 },
27544fca9c42SMichael Ellerman 	{ "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
2755104f0cc2SMichael Ellerman 	{ "signal2_type", NULL, spufs_signal2_type_get, 19 },
2756104f0cc2SMichael Ellerman 	{ "event_mask", NULL, spufs_event_mask_get, 19 },
2757104f0cc2SMichael Ellerman 	{ "event_status", NULL, spufs_event_status_get, 19 },
27584fca9c42SMichael Ellerman 	{ "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
27594fca9c42SMichael Ellerman 	{ "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
27604fca9c42SMichael Ellerman 	{ "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
27614fca9c42SMichael Ellerman 	{ "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
27624fca9c42SMichael Ellerman 	{ "proxydma_info", __spufs_proxydma_info_read,
27634fca9c42SMichael Ellerman 			   NULL, sizeof(struct spu_proxydma_info)},
2764104f0cc2SMichael Ellerman 	{ "object-id", NULL, spufs_object_id_get, 19 },
2765104f0cc2SMichael Ellerman 	{ "npc", NULL, spufs_npc_get, 19 },
2766936d5bf1SMichael Ellerman 	{ NULL },
2767bf1ab978SDwayne Grant McConnell };
2768