blob: 0cfece4cf6ef36dd9e892654ffad946346ea82f8 [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
2 * SPU file system -- file contents
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
Arnd Bergmanna33a7d72006-03-23 00:00:11 +010023#undef DEBUG
24
Arnd Bergmann67207b92005-11-15 15:53:48 -050025#include <linux/fs.h>
26#include <linux/ioctl.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040027#include <linux/export.h>
Arnd Bergmannd88cfff2005-12-05 22:52:22 -050028#include <linux/pagemap.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050029#include <linux/poll.h>
Arnd Bergmann51104592005-12-05 22:52:25 -050030#include <linux/ptrace.h>
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +100031#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050033
34#include <asm/io.h>
FUJITA Tomonoridfe1e092008-05-13 19:07:42 +100035#include <asm/time.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050036#include <asm/spu.h>
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +010037#include <asm/spu_info.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050038#include <asm/uaccess.h>
39
40#include "spufs.h"
Christoph Hellwigae142e02009-06-12 04:31:52 +000041#include "sputrace.h"
Arnd Bergmann67207b92005-11-15 15:53:48 -050042
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +020043#define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000)
44
Christoph Hellwig197b1a82007-12-20 16:39:59 +090045/* Simple attribute files */
46struct spufs_attr {
47 int (*get)(void *, u64 *);
48 int (*set)(void *, u64);
49 char get_buf[24]; /* enough to store a u64 and "\n\0" */
50 char set_buf[24];
51 void *data;
52 const char *fmt; /* format for read operation */
53 struct mutex mutex; /* protects access to these buffers */
54};
55
56static int spufs_attr_open(struct inode *inode, struct file *file,
57 int (*get)(void *, u64 *), int (*set)(void *, u64),
58 const char *fmt)
59{
60 struct spufs_attr *attr;
61
62 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
63 if (!attr)
64 return -ENOMEM;
65
66 attr->get = get;
67 attr->set = set;
68 attr->data = inode->i_private;
69 attr->fmt = fmt;
70 mutex_init(&attr->mutex);
71 file->private_data = attr;
72
73 return nonseekable_open(inode, file);
74}
75
76static int spufs_attr_release(struct inode *inode, struct file *file)
77{
78 kfree(file->private_data);
79 return 0;
80}
81
82static ssize_t spufs_attr_read(struct file *file, char __user *buf,
83 size_t len, loff_t *ppos)
84{
85 struct spufs_attr *attr;
86 size_t size;
87 ssize_t ret;
88
89 attr = file->private_data;
90 if (!attr->get)
91 return -EACCES;
92
93 ret = mutex_lock_interruptible(&attr->mutex);
94 if (ret)
95 return ret;
96
97 if (*ppos) { /* continued read */
98 size = strlen(attr->get_buf);
99 } else { /* first read */
100 u64 val;
101 ret = attr->get(attr->data, &val);
102 if (ret)
103 goto out;
104
105 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
106 attr->fmt, (unsigned long long)val);
107 }
108
109 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
110out:
111 mutex_unlock(&attr->mutex);
112 return ret;
113}
114
115static ssize_t spufs_attr_write(struct file *file, const char __user *buf,
116 size_t len, loff_t *ppos)
117{
118 struct spufs_attr *attr;
119 u64 val;
120 size_t size;
121 ssize_t ret;
122
123 attr = file->private_data;
124 if (!attr->set)
125 return -EACCES;
126
127 ret = mutex_lock_interruptible(&attr->mutex);
128 if (ret)
129 return ret;
130
131 ret = -EFAULT;
132 size = min(sizeof(attr->set_buf) - 1, len);
133 if (copy_from_user(attr->set_buf, buf, size))
134 goto out;
135
136 ret = len; /* claim we got the whole input */
137 attr->set_buf[size] = '\0';
138 val = simple_strtol(attr->set_buf, NULL, 0);
139 attr->set(attr->data, val);
140out:
141 mutex_unlock(&attr->mutex);
142 return ret;
143}
144
145#define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
146static int __fops ## _open(struct inode *inode, struct file *file) \
147{ \
148 __simple_attr_check_format(__fmt, 0ull); \
149 return spufs_attr_open(inode, file, __get, __set, __fmt); \
150} \
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700151static const struct file_operations __fops = { \
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900152 .owner = THIS_MODULE, \
153 .open = __fops ## _open, \
154 .release = spufs_attr_release, \
155 .read = spufs_attr_read, \
156 .write = spufs_attr_write, \
Arnd Bergmannfc153512010-09-14 10:22:33 +0000157 .llseek = generic_file_llseek, \
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900158};
159
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +1000160
Arnd Bergmann67207b92005-11-15 15:53:48 -0500161static int
162spufs_mem_open(struct inode *inode, struct file *file)
163{
164 struct spufs_inode_info *i = SPUFS_I(inode);
Mark Nutter6df10a82006-03-23 00:00:12 +0100165 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200166
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000167 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100168 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200169 if (!i->i_openers++)
170 ctx->local_store = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000171 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200172 return 0;
173}
174
175static int
176spufs_mem_release(struct inode *inode, struct file *file)
177{
178 struct spufs_inode_info *i = SPUFS_I(inode);
179 struct spu_context *ctx = i->i_ctx;
180
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000181 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200182 if (!--i->i_openers)
183 ctx->local_store = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000184 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500185 return 0;
186}
187
188static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100189__spufs_mem_read(struct spu_context *ctx, char __user *buffer,
190 size_t size, loff_t *pos)
191{
192 char *local_store = ctx->ops->get_ls(ctx);
193 return simple_read_from_buffer(buffer, size, pos, local_store,
194 LS_SIZE);
195}
196
197static ssize_t
Arnd Bergmann67207b92005-11-15 15:53:48 -0500198spufs_mem_read(struct file *file, char __user *buffer,
199 size_t size, loff_t *pos)
200{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100201 struct spu_context *ctx = file->private_data;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100202 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500203
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900204 ret = spu_acquire(ctx);
205 if (ret)
206 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100207 ret = __spufs_mem_read(ctx, buffer, size, pos);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500208 spu_release(ctx);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900209
Arnd Bergmann67207b92005-11-15 15:53:48 -0500210 return ret;
211}
212
213static ssize_t
214spufs_mem_write(struct file *file, const char __user *buffer,
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100215 size_t size, loff_t *ppos)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500216{
217 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500218 char *local_store;
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100219 loff_t pos = *ppos;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500220 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500221
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100222 if (pos > LS_SIZE)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500223 return -EFBIG;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500224
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900225 ret = spu_acquire(ctx);
226 if (ret)
227 return ret;
228
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500229 local_store = ctx->ops->get_ls(ctx);
Akinobu Mita63c3b9d2010-12-24 20:03:56 +0000230 size = simple_write_to_buffer(local_store, LS_SIZE, ppos, buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500231 spu_release(ctx);
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100232
Arnd Bergmannaa0ed2b2007-03-10 00:05:35 +0100233 return size;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500234}
235
Nick Pigginb1e22702008-06-10 09:26:08 +1000236static int
237spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500238{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000239 struct spu_context *ctx = vma->vm_file->private_data;
Nick Pigginb1e22702008-06-10 09:26:08 +1000240 unsigned long address = (unsigned long)vmf->virtual_address;
241 unsigned long pfn, offset;
242
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000243#ifdef CONFIG_SPU_FS_64K_LS
244 struct spu_state *csa = &ctx->csa;
245 int psize;
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100246
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000247 /* Check what page size we are using */
248 psize = get_slice_psize(vma->vm_mm, address);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500249
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000250 /* Some sanity checking */
251 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K));
252
253 /* Wow, 64K, cool, we need to align the address though */
254 if (csa->use_big_pages) {
255 BUG_ON(vma->vm_start & 0xffff);
256 address &= ~0xfffful;
257 }
258#endif /* CONFIG_SPU_FS_64K_LS */
259
Nick Pigginb1e22702008-06-10 09:26:08 +1000260 offset = vmf->pgoff << PAGE_SHIFT;
Masato Noguchi128b8542007-02-13 21:54:30 +0100261 if (offset >= LS_SIZE)
Nick Pigginb1e22702008-06-10 09:26:08 +1000262 return VM_FAULT_SIGBUS;
Masato Noguchi128b8542007-02-13 21:54:30 +0100263
Nick Pigginb1e22702008-06-10 09:26:08 +1000264 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n",
265 address, offset);
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000266
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900267 if (spu_acquire(ctx))
Nick Pigginb1e22702008-06-10 09:26:08 +1000268 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500269
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200270 if (ctx->state == SPU_STATE_SAVED) {
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000271 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100272 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset);
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200273 } else {
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000274 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100275 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT;
Arnd Bergmannac91cb82006-10-04 17:26:16 +0200276 }
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100277 vm_insert_pfn(vma, address, pfn);
278
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500279 spu_release(ctx);
280
Nick Pigginb1e22702008-06-10 09:26:08 +1000281 return VM_FAULT_NOPAGE;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500282}
283
Benjamin Herrenschmidta3528942008-07-23 21:27:09 -0700284static int spufs_mem_mmap_access(struct vm_area_struct *vma,
285 unsigned long address,
286 void *buf, int len, int write)
287{
288 struct spu_context *ctx = vma->vm_file->private_data;
289 unsigned long offset = address - vma->vm_start;
290 char *local_store;
291
292 if (write && !(vma->vm_flags & VM_WRITE))
293 return -EACCES;
294 if (spu_acquire(ctx))
295 return -EINTR;
296 if ((offset + len) > vma->vm_end)
297 len = vma->vm_end - offset;
298 local_store = ctx->ops->get_ls(ctx);
299 if (write)
300 memcpy_toio(local_store + offset, buf, len);
301 else
302 memcpy_fromio(buf, local_store + offset, len);
303 spu_release(ctx);
304 return len;
305}
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100306
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400307static const struct vm_operations_struct spufs_mem_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000308 .fault = spufs_mem_mmap_fault,
Benjamin Herrenschmidta3528942008-07-23 21:27:09 -0700309 .access = spufs_mem_mmap_access,
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500310};
311
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000312static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500313{
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000314#ifdef CONFIG_SPU_FS_64K_LS
315 struct spu_context *ctx = file->private_data;
316 struct spu_state *csa = &ctx->csa;
317
318 /* Sanity check VMA alignment */
319 if (csa->use_big_pages) {
320 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx,"
321 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end,
322 vma->vm_pgoff);
323 if (vma->vm_start & 0xffff)
324 return -EINVAL;
325 if (vma->vm_pgoff & 0xf)
326 return -EINVAL;
327 }
328#endif /* CONFIG_SPU_FS_64K_LS */
329
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500330 if (!(vma->vm_flags & VM_SHARED))
331 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500332
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100333 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000334 vma->vm_page_prot = pgprot_noncached_wc(vma->vm_page_prot);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500335
336 vma->vm_ops = &spufs_mem_mmap_vmops;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500337 return 0;
338}
339
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000340#ifdef CONFIG_SPU_FS_64K_LS
Sebastian Siewior12388192007-09-19 14:38:12 +1000341static unsigned long spufs_get_unmapped_area(struct file *file,
342 unsigned long addr, unsigned long len, unsigned long pgoff,
343 unsigned long flags)
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000344{
345 struct spu_context *ctx = file->private_data;
346 struct spu_state *csa = &ctx->csa;
347
348 /* If not using big pages, fallback to normal MM g_u_a */
349 if (!csa->use_big_pages)
350 return current->mm->get_unmapped_area(file, addr, len,
351 pgoff, flags);
352
353 /* Else, try to obtain a 64K pages slice */
354 return slice_get_unmapped_area(addr, len, flags,
355 MMU_PAGE_64K, 1, 0);
356}
357#endif /* CONFIG_SPU_FS_64K_LS */
358
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800359static const struct file_operations spufs_mem_fops = {
Jeremy Kerr70225432007-06-29 10:58:00 +1000360 .open = spufs_mem_open,
361 .release = spufs_mem_release,
362 .read = spufs_mem_read,
363 .write = spufs_mem_write,
364 .llseek = generic_file_llseek,
365 .mmap = spufs_mem_mmap,
Benjamin Herrenschmidtf1fa74f2007-05-08 16:27:29 +1000366#ifdef CONFIG_SPU_FS_64K_LS
367 .get_unmapped_area = spufs_get_unmapped_area,
368#endif
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500369};
370
Nick Pigginb1e22702008-06-10 09:26:08 +1000371static int spufs_ps_fault(struct vm_area_struct *vma,
372 struct vm_fault *vmf,
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100373 unsigned long ps_offs,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200374 unsigned long ps_size)
Mark Nutter6df10a82006-03-23 00:00:12 +0100375{
Mark Nutter6df10a82006-03-23 00:00:12 +0100376 struct spu_context *ctx = vma->vm_file->private_data;
Nick Pigginb1e22702008-06-10 09:26:08 +1000377 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100378 int ret = 0;
Mark Nutter6df10a82006-03-23 00:00:12 +0100379
Nick Pigginb1e22702008-06-10 09:26:08 +1000380 spu_context_nospu_trace(spufs_ps_fault__enter, ctx);
Christoph Hellwig038200c2008-01-11 15:03:26 +1100381
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200382 if (offset >= ps_size)
Nick Pigginb1e22702008-06-10 09:26:08 +1000383 return VM_FAULT_SIGBUS;
Mark Nutter6df10a82006-03-23 00:00:12 +0100384
Jeremy Kerr60657262008-11-11 10:22:22 +1100385 if (fatal_signal_pending(current))
386 return VM_FAULT_SIGBUS;
387
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900388 /*
Jeremy Kerrd5883132008-02-26 13:31:42 +1100389 * Because we release the mmap_sem, the context may be destroyed while
390 * we're in spu_wait. Grab an extra reference so it isn't destroyed
391 * in the meantime.
392 */
393 get_spu_context(ctx);
394
395 /*
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900396 * We have to wait for context to be loaded before we have
397 * pages to hand out to the user, but we don't want to wait
398 * with the mmap_sem held.
399 * It is possible to drop the mmap_sem here, but then we need
Nick Pigginb1e22702008-06-10 09:26:08 +1000400 * to return VM_FAULT_NOPAGE because the mappings may have
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900401 * hanged.
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100402 */
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900403 if (spu_acquire(ctx))
Jeremy Kerrd5883132008-02-26 13:31:42 +1100404 goto refault;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900405
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900406 if (ctx->state == SPU_STATE_SAVED) {
407 up_read(&current->mm->mmap_sem);
Nick Pigginb1e22702008-06-10 09:26:08 +1000408 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100409 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Nick Pigginb1e22702008-06-10 09:26:08 +1000410 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900411 down_read(&current->mm->mmap_sem);
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900412 } else {
413 area = ctx->spu->problem_phys + ps_offs;
Nick Pigginb1e22702008-06-10 09:26:08 +1000414 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address,
415 (area + offset) >> PAGE_SHIFT);
416 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu);
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +0900417 }
Mark Nutter6df10a82006-03-23 00:00:12 +0100418
Christoph Hellwigeebead52008-02-08 15:50:41 +1100419 if (!ret)
420 spu_release(ctx);
Jeremy Kerrd5883132008-02-26 13:31:42 +1100421
422refault:
423 put_spu_context(ctx);
Nick Pigginb1e22702008-06-10 09:26:08 +1000424 return VM_FAULT_NOPAGE;
Mark Nutter6df10a82006-03-23 00:00:12 +0100425}
426
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200427#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +1000428static int spufs_cntl_mmap_fault(struct vm_area_struct *vma,
429 struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +0100430{
Jeremy Kerr87ff6092008-07-01 10:22:50 +1000431 return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +0100432}
433
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400434static const struct vm_operations_struct spufs_cntl_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +1000435 .fault = spufs_cntl_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +0100436};
437
438/*
439 * mmap support for problem state control area [0x4000 - 0x4fff].
Mark Nutter6df10a82006-03-23 00:00:12 +0100440 */
441static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma)
442{
443 if (!(vma->vm_flags & VM_SHARED))
444 return -EINVAL;
445
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +1100446 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000447 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +0100448
449 vma->vm_ops = &spufs_cntl_mmap_vmops;
450 return 0;
451}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +0200452#else /* SPUFS_MMAP_4K */
453#define spufs_cntl_mmap NULL
454#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +0100455
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900456static int spufs_cntl_get(void *data, u64 *val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200457{
458 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900459 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200460
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900461 ret = spu_acquire(ctx);
462 if (ret)
463 return ret;
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900464 *val = ctx->ops->status_read(ctx);
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200465 spu_release(ctx);
466
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900467 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200468}
469
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900470static int spufs_cntl_set(void *data, u64 val)
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200471{
472 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900473 int ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200474
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900475 ret = spu_acquire(ctx);
476 if (ret)
477 return ret;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200478 ctx->ops->runcntl_write(ctx, val);
479 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +0900480
481 return 0;
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200482}
483
Mark Nutter6df10a82006-03-23 00:00:12 +0100484static int spufs_cntl_open(struct inode *inode, struct file *file)
485{
486 struct spufs_inode_info *i = SPUFS_I(inode);
487 struct spu_context *ctx = i->i_ctx;
488
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000489 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +0100490 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200491 if (!i->i_openers++)
492 ctx->cntl = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000493 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800494 return simple_attr_open(inode, file, spufs_cntl_get,
Arnd Bergmanne1dbff22006-10-04 17:26:19 +0200495 spufs_cntl_set, "0x%08lx");
Mark Nutter6df10a82006-03-23 00:00:12 +0100496}
497
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200498static int
499spufs_cntl_release(struct inode *inode, struct file *file)
500{
501 struct spufs_inode_info *i = SPUFS_I(inode);
502 struct spu_context *ctx = i->i_ctx;
503
Christoph Hellwig74bedc42008-02-08 04:20:28 -0800504 simple_attr_release(inode, file);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200505
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000506 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200507 if (!--i->i_openers)
508 ctx->cntl = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +1000509 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200510 return 0;
511}
512
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800513static const struct file_operations spufs_cntl_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +0100514 .open = spufs_cntl_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +0200515 .release = spufs_cntl_release,
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800516 .read = simple_attr_read,
517 .write = simple_attr_write,
Arnd Bergmannfc153512010-09-14 10:22:33 +0000518 .llseek = generic_file_llseek,
Mark Nutter6df10a82006-03-23 00:00:12 +0100519 .mmap = spufs_cntl_mmap,
Mark Nutter6df10a82006-03-23 00:00:12 +0100520};
521
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500522static int
523spufs_regs_open(struct inode *inode, struct file *file)
524{
525 struct spufs_inode_info *i = SPUFS_I(inode);
526 file->private_data = i->i_ctx;
527 return 0;
528}
529
530static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100531__spufs_regs_read(struct spu_context *ctx, char __user *buffer,
532 size_t size, loff_t *pos)
533{
534 struct spu_lscsa *lscsa = ctx->csa.lscsa;
535 return simple_read_from_buffer(buffer, size, pos,
536 lscsa->gprs, sizeof lscsa->gprs);
537}
538
539static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500540spufs_regs_read(struct file *file, char __user *buffer,
541 size_t size, loff_t *pos)
542{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500543 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100544 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500545
Jeremy Kerrf027faa2008-10-16 11:11:12 +1100546 /* pre-check for file position: if we'd return EOF, there's no point
547 * causing a deschedule */
548 if (*pos >= sizeof(ctx->csa.lscsa->gprs))
549 return 0;
550
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900551 ret = spu_acquire_saved(ctx);
552 if (ret)
553 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100554 ret = __spufs_regs_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200555 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500556 return ret;
557}
558
559static ssize_t
560spufs_regs_write(struct file *file, const char __user *buffer,
561 size_t size, loff_t *pos)
562{
563 struct spu_context *ctx = file->private_data;
564 struct spu_lscsa *lscsa = ctx->csa.lscsa;
565 int ret;
566
Jeremy Kerrd2198892009-03-03 19:38:07 +0000567 if (*pos >= sizeof(lscsa->gprs))
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500568 return -EFBIG;
Jeremy Kerrd2198892009-03-03 19:38:07 +0000569
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900570 ret = spu_acquire_saved(ctx);
571 if (ret)
572 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500573
Akinobu Mita63c3b9d2010-12-24 20:03:56 +0000574 size = simple_write_to_buffer(lscsa->gprs, sizeof(lscsa->gprs), pos,
575 buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500576
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200577 spu_release_saved(ctx);
Akinobu Mita63c3b9d2010-12-24 20:03:56 +0000578 return size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500579}
580
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800581static const struct file_operations spufs_regs_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500582 .open = spufs_regs_open,
583 .read = spufs_regs_read,
584 .write = spufs_regs_write,
585 .llseek = generic_file_llseek,
586};
587
588static ssize_t
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100589__spufs_fpcr_read(struct spu_context *ctx, char __user * buffer,
590 size_t size, loff_t * pos)
591{
592 struct spu_lscsa *lscsa = ctx->csa.lscsa;
593 return simple_read_from_buffer(buffer, size, pos,
594 &lscsa->fpcr, sizeof(lscsa->fpcr));
595}
596
597static ssize_t
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500598spufs_fpcr_read(struct file *file, char __user * buffer,
599 size_t size, loff_t * pos)
600{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500601 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100602 struct spu_context *ctx = file->private_data;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500603
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900604 ret = spu_acquire_saved(ctx);
605 if (ret)
606 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100607 ret = __spufs_fpcr_read(ctx, buffer, size, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200608 spu_release_saved(ctx);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500609 return ret;
610}
611
612static ssize_t
613spufs_fpcr_write(struct file *file, const char __user * buffer,
614 size_t size, loff_t * pos)
615{
616 struct spu_context *ctx = file->private_data;
617 struct spu_lscsa *lscsa = ctx->csa.lscsa;
618 int ret;
619
Jeremy Kerrd2198892009-03-03 19:38:07 +0000620 if (*pos >= sizeof(lscsa->fpcr))
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500621 return -EFBIG;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900622
623 ret = spu_acquire_saved(ctx);
624 if (ret)
625 return ret;
626
Akinobu Mita63c3b9d2010-12-24 20:03:56 +0000627 size = simple_write_to_buffer(&lscsa->fpcr, sizeof(lscsa->fpcr), pos,
628 buffer, size);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500629
Christoph Hellwig27b1ea02007-07-20 21:39:34 +0200630 spu_release_saved(ctx);
Akinobu Mita63c3b9d2010-12-24 20:03:56 +0000631 return size;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500632}
633
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800634static const struct file_operations spufs_fpcr_fops = {
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500635 .open = spufs_regs_open,
636 .read = spufs_fpcr_read,
637 .write = spufs_fpcr_write,
638 .llseek = generic_file_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500639};
640
641/* generic open function for all pipe-like files */
642static int spufs_pipe_open(struct inode *inode, struct file *file)
643{
644 struct spufs_inode_info *i = SPUFS_I(inode);
645 file->private_data = i->i_ctx;
646
647 return nonseekable_open(inode, file);
648}
649
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200650/*
651 * Read as many bytes from the mailbox as possible, until
652 * one of the conditions becomes true:
653 *
654 * - no more data available in the mailbox
655 * - end of the user provided buffer
656 * - end of the mapped area
657 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500658static ssize_t spufs_mbox_read(struct file *file, char __user *buf,
659 size_t len, loff_t *pos)
660{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500661 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200662 u32 mbox_data, __user *udata;
663 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500664
665 if (len < 4)
666 return -EINVAL;
667
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200668 if (!access_ok(VERIFY_WRITE, buf, len))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500669 return -EFAULT;
670
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200671 udata = (void __user *)buf;
672
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900673 count = spu_acquire(ctx);
674 if (count)
675 return count;
676
Arnd Bergmann274cef52006-10-24 18:01:42 +0200677 for (count = 0; (count + 4) <= len; count += 4, udata++) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200678 int ret;
679 ret = ctx->ops->mbox_read(ctx, &mbox_data);
680 if (ret == 0)
681 break;
682
683 /*
684 * at the end of the mapped area, we can fault
685 * but still need to return the data we have
686 * read successfully so far.
687 */
688 ret = __put_user(mbox_data, udata);
689 if (ret) {
690 if (!count)
691 count = -EFAULT;
692 break;
693 }
694 }
695 spu_release(ctx);
696
697 if (!count)
698 count = -EAGAIN;
699
700 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500701}
702
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800703static const struct file_operations spufs_mbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500704 .open = spufs_pipe_open,
705 .read = spufs_mbox_read,
Arnd Bergmannfc153512010-09-14 10:22:33 +0000706 .llseek = no_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500707};
708
709static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf,
710 size_t len, loff_t *pos)
711{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500712 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900713 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500714 u32 mbox_stat;
715
716 if (len < 4)
717 return -EINVAL;
718
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900719 ret = spu_acquire(ctx);
720 if (ret)
721 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500722
723 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff;
724
725 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500726
727 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat))
728 return -EFAULT;
729
730 return 4;
731}
732
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800733static const struct file_operations spufs_mbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500734 .open = spufs_pipe_open,
735 .read = spufs_mbox_stat_read,
Arnd Bergmannfc153512010-09-14 10:22:33 +0000736 .llseek = no_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500737};
738
739/* low-level ibox access function */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500740size_t spu_ibox_read(struct spu_context *ctx, u32 *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500741{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500742 return ctx->ops->ibox_read(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500743}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500744
745static int spufs_ibox_fasync(int fd, struct file *file, int on)
746{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500747 struct spu_context *ctx = file->private_data;
748
749 return fasync_helper(fd, file, on, &ctx->ibox_fasync);
750}
751
752/* interrupt-level ibox callback function. */
753void spufs_ibox_callback(struct spu *spu)
754{
755 struct spu_context *ctx = spu->ctx;
756
Luke Browninge65c2f62007-12-20 16:39:59 +0900757 if (!ctx)
758 return;
759
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500760 wake_up_all(&ctx->ibox_wq);
761 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500762}
763
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200764/*
765 * Read as many bytes from the interrupt mailbox as possible, until
766 * one of the conditions becomes true:
767 *
768 * - no more data available in the mailbox
769 * - end of the user provided buffer
770 * - end of the mapped area
771 *
772 * If the file is opened without O_NONBLOCK, we wait here until
773 * any data is available, but return when we have been able to
774 * read something.
775 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500776static ssize_t spufs_ibox_read(struct file *file, char __user *buf,
777 size_t len, loff_t *pos)
778{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500779 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200780 u32 ibox_data, __user *udata;
781 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500782
783 if (len < 4)
784 return -EINVAL;
785
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200786 if (!access_ok(VERIFY_WRITE, buf, len))
787 return -EFAULT;
788
789 udata = (void __user *)buf;
790
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900791 count = spu_acquire(ctx);
792 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100793 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500794
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200795 /* wait only for the first element */
796 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500797 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100798 if (!spu_ibox_read(ctx, &ibox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200799 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100800 goto out_unlock;
801 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500802 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200803 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100804 if (count)
805 goto out;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200806 }
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200807
808 /* if we can't write at all, return -EFAULT */
809 count = __put_user(ibox_data, udata);
810 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100811 goto out_unlock;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200812
813 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
814 int ret;
815 ret = ctx->ops->ibox_read(ctx, &ibox_data);
816 if (ret == 0)
817 break;
818 /*
819 * at the end of the mapped area, we can fault
820 * but still need to return the data we have
821 * read successfully so far.
822 */
823 ret = __put_user(ibox_data, udata);
824 if (ret)
825 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500826 }
827
Christoph Hellwigeebead52008-02-08 15:50:41 +1100828out_unlock:
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500829 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100830out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200831 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500832}
833
834static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait)
835{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500836 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500837 unsigned int mask;
838
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500839 poll_wait(file, &ctx->ibox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500840
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900841 /*
842 * For now keep this uninterruptible and also ignore the rule
843 * that poll should not sleep. Will be fixed later.
844 */
845 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500846 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM);
847 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500848
849 return mask;
850}
851
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800852static const struct file_operations spufs_ibox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500853 .open = spufs_pipe_open,
854 .read = spufs_ibox_read,
855 .poll = spufs_ibox_poll,
856 .fasync = spufs_ibox_fasync,
Arnd Bergmannfc153512010-09-14 10:22:33 +0000857 .llseek = no_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500858};
859
860static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf,
861 size_t len, loff_t *pos)
862{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500863 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900864 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500865 u32 ibox_stat;
866
867 if (len < 4)
868 return -EINVAL;
869
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900870 ret = spu_acquire(ctx);
871 if (ret)
872 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500873 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff;
874 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500875
876 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat))
877 return -EFAULT;
878
879 return 4;
880}
881
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800882static const struct file_operations spufs_ibox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500883 .open = spufs_pipe_open,
884 .read = spufs_ibox_stat_read,
Arnd Bergmannfc153512010-09-14 10:22:33 +0000885 .llseek = no_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500886};
887
888/* low-level mailbox write */
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500889size_t spu_wbox_write(struct spu_context *ctx, u32 data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500890{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500891 return ctx->ops->wbox_write(ctx, data);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500892}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500893
894static int spufs_wbox_fasync(int fd, struct file *file, int on)
895{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500896 struct spu_context *ctx = file->private_data;
897 int ret;
898
899 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync);
900
901 return ret;
902}
903
904/* interrupt-level wbox callback function. */
905void spufs_wbox_callback(struct spu *spu)
906{
907 struct spu_context *ctx = spu->ctx;
908
Luke Browninge65c2f62007-12-20 16:39:59 +0900909 if (!ctx)
910 return;
911
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500912 wake_up_all(&ctx->wbox_wq);
913 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500914}
915
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200916/*
917 * Write as many bytes to the interrupt mailbox as possible, until
918 * one of the conditions becomes true:
919 *
920 * - the mailbox is full
921 * - end of the user provided buffer
922 * - end of the mapped area
923 *
924 * If the file is opened without O_NONBLOCK, we wait here until
925 * space is availabyl, but return when we have been able to
926 * write something.
927 */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500928static ssize_t spufs_wbox_write(struct file *file, const char __user *buf,
929 size_t len, loff_t *pos)
930{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500931 struct spu_context *ctx = file->private_data;
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200932 u32 wbox_data, __user *udata;
933 ssize_t count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500934
935 if (len < 4)
936 return -EINVAL;
937
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200938 udata = (void __user *)buf;
939 if (!access_ok(VERIFY_READ, buf, len))
940 return -EFAULT;
941
942 if (__get_user(wbox_data, udata))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500943 return -EFAULT;
944
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900945 count = spu_acquire(ctx);
946 if (count)
Christoph Hellwigeebead52008-02-08 15:50:41 +1100947 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500948
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200949 /*
950 * make sure we can at least write one element, by waiting
951 * in case of !O_NONBLOCK
952 */
953 count = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500954 if (file->f_flags & O_NONBLOCK) {
Christoph Hellwigeebead52008-02-08 15:50:41 +1100955 if (!spu_wbox_write(ctx, wbox_data)) {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200956 count = -EAGAIN;
Christoph Hellwigeebead52008-02-08 15:50:41 +1100957 goto out_unlock;
958 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500959 } else {
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200960 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data));
Christoph Hellwigeebead52008-02-08 15:50:41 +1100961 if (count)
962 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500963 }
964
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500965
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200966 /* write as much as possible */
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200967 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) {
968 int ret;
969 ret = __get_user(wbox_data, udata);
970 if (ret)
971 break;
972
973 ret = spu_wbox_write(ctx, wbox_data);
974 if (ret == 0)
975 break;
976 }
977
Christoph Hellwigeebead52008-02-08 15:50:41 +1100978out_unlock:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200979 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +1100980out:
Arnd Bergmanncdcc89b2006-10-04 17:26:17 +0200981 return count;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500982}
983
984static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait)
985{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500986 struct spu_context *ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500987 unsigned int mask;
988
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500989 poll_wait(file, &ctx->wbox_wq, wait);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500990
Christoph Hellwigc9101bd2007-12-20 16:39:59 +0900991 /*
992 * For now keep this uninterruptible and also ignore the rule
993 * that poll should not sleep. Will be fixed later.
994 */
995 mutex_lock(&ctx->state_mutex);
Arnd Bergmann3a843d72005-12-05 22:52:27 -0500996 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM);
997 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500998
999 return mask;
1000}
1001
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001002static const struct file_operations spufs_wbox_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001003 .open = spufs_pipe_open,
1004 .write = spufs_wbox_write,
1005 .poll = spufs_wbox_poll,
1006 .fasync = spufs_wbox_fasync,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001007 .llseek = no_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001008};
1009
1010static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf,
1011 size_t len, loff_t *pos)
1012{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001013 struct spu_context *ctx = file->private_data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001014 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001015 u32 wbox_stat;
1016
1017 if (len < 4)
1018 return -EINVAL;
1019
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001020 ret = spu_acquire(ctx);
1021 if (ret)
1022 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001023 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff;
1024 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001025
1026 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat))
1027 return -EFAULT;
1028
1029 return 4;
1030}
1031
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001032static const struct file_operations spufs_wbox_stat_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -05001033 .open = spufs_pipe_open,
1034 .read = spufs_wbox_stat_read,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001035 .llseek = no_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001036};
1037
Mark Nutter6df10a82006-03-23 00:00:12 +01001038static int spufs_signal1_open(struct inode *inode, struct file *file)
1039{
1040 struct spufs_inode_info *i = SPUFS_I(inode);
1041 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001042
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001043 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001044 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001045 if (!i->i_openers++)
1046 ctx->signal1 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001047 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001048 return nonseekable_open(inode, file);
1049}
1050
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001051static int
1052spufs_signal1_release(struct inode *inode, struct file *file)
1053{
1054 struct spufs_inode_info *i = SPUFS_I(inode);
1055 struct spu_context *ctx = i->i_ctx;
1056
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001057 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001058 if (!--i->i_openers)
1059 ctx->signal1 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001060 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001061 return 0;
1062}
1063
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001064static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001065 size_t len, loff_t *pos)
1066{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001067 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001068 u32 data;
1069
Arnd Bergmann67207b92005-11-15 15:53:48 -05001070 if (len < 4)
1071 return -EINVAL;
1072
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001073 if (ctx->csa.spu_chnlcnt_RW[3]) {
1074 data = ctx->csa.spu_chnldata_RW[3];
1075 ret = 4;
1076 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001077
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001078 if (!ret)
1079 goto out;
1080
Arnd Bergmann67207b92005-11-15 15:53:48 -05001081 if (copy_to_user(buf, &data, 4))
1082 return -EFAULT;
1083
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001084out:
1085 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001086}
1087
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001088static ssize_t spufs_signal1_read(struct file *file, char __user *buf,
1089 size_t len, loff_t *pos)
1090{
1091 int ret;
1092 struct spu_context *ctx = file->private_data;
1093
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001094 ret = spu_acquire_saved(ctx);
1095 if (ret)
1096 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001097 ret = __spufs_signal1_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001098 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001099
1100 return ret;
1101}
1102
Arnd Bergmann67207b92005-11-15 15:53:48 -05001103static ssize_t spufs_signal1_write(struct file *file, const char __user *buf,
1104 size_t len, loff_t *pos)
1105{
1106 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001107 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001108 u32 data;
1109
1110 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001111
1112 if (len < 4)
1113 return -EINVAL;
1114
1115 if (copy_from_user(&data, buf, 4))
1116 return -EFAULT;
1117
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001118 ret = spu_acquire(ctx);
1119 if (ret)
1120 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001121 ctx->ops->signal1_write(ctx, data);
1122 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001123
1124 return 4;
1125}
1126
Nick Pigginb1e22702008-06-10 09:26:08 +10001127static int
1128spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001129{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001130#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1131 return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE);
1132#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001133 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1134 * signal 1 and 2 area
1135 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001136 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001137#else
1138#error unsupported page size
1139#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001140}
1141
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001142static const struct vm_operations_struct spufs_signal1_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001143 .fault = spufs_signal1_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001144};
1145
1146static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma)
1147{
1148 if (!(vma->vm_flags & VM_SHARED))
1149 return -EINVAL;
1150
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001151 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001152 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001153
1154 vma->vm_ops = &spufs_signal1_mmap_vmops;
1155 return 0;
1156}
Mark Nutter6df10a82006-03-23 00:00:12 +01001157
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001158static const struct file_operations spufs_signal1_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001159 .open = spufs_signal1_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001160 .release = spufs_signal1_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001161 .read = spufs_signal1_read,
1162 .write = spufs_signal1_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001163 .mmap = spufs_signal1_mmap,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001164 .llseek = no_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001165};
1166
Jeremy Kerrd054b362007-07-20 21:39:31 +02001167static const struct file_operations spufs_signal1_nosched_fops = {
1168 .open = spufs_signal1_open,
1169 .release = spufs_signal1_release,
1170 .write = spufs_signal1_write,
1171 .mmap = spufs_signal1_mmap,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001172 .llseek = no_llseek,
Jeremy Kerrd054b362007-07-20 21:39:31 +02001173};
1174
Mark Nutter6df10a82006-03-23 00:00:12 +01001175static int spufs_signal2_open(struct inode *inode, struct file *file)
1176{
1177 struct spufs_inode_info *i = SPUFS_I(inode);
1178 struct spu_context *ctx = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001179
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001180 mutex_lock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001181 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001182 if (!i->i_openers++)
1183 ctx->signal2 = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001184 mutex_unlock(&ctx->mapping_lock);
Mark Nutter6df10a82006-03-23 00:00:12 +01001185 return nonseekable_open(inode, file);
1186}
1187
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001188static int
1189spufs_signal2_release(struct inode *inode, struct file *file)
1190{
1191 struct spufs_inode_info *i = SPUFS_I(inode);
1192 struct spu_context *ctx = i->i_ctx;
1193
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001194 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001195 if (!--i->i_openers)
1196 ctx->signal2 = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001197 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001198 return 0;
1199}
1200
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001201static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001202 size_t len, loff_t *pos)
1203{
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001204 int ret = 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001205 u32 data;
1206
Arnd Bergmann67207b92005-11-15 15:53:48 -05001207 if (len < 4)
1208 return -EINVAL;
1209
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001210 if (ctx->csa.spu_chnlcnt_RW[4]) {
1211 data = ctx->csa.spu_chnldata_RW[4];
1212 ret = 4;
1213 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001214
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001215 if (!ret)
1216 goto out;
1217
Arnd Bergmann67207b92005-11-15 15:53:48 -05001218 if (copy_to_user(buf, &data, 4))
1219 return -EFAULT;
1220
Dwayne Grant McConnell17f88ce2006-11-20 18:45:01 +01001221out:
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001222 return ret;
1223}
1224
1225static ssize_t spufs_signal2_read(struct file *file, char __user *buf,
1226 size_t len, loff_t *pos)
1227{
1228 struct spu_context *ctx = file->private_data;
1229 int ret;
1230
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001231 ret = spu_acquire_saved(ctx);
1232 if (ret)
1233 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001234 ret = __spufs_signal2_read(ctx, buf, len, pos);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001235 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001236
1237 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001238}
1239
1240static ssize_t spufs_signal2_write(struct file *file, const char __user *buf,
1241 size_t len, loff_t *pos)
1242{
1243 struct spu_context *ctx;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001244 ssize_t ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001245 u32 data;
1246
1247 ctx = file->private_data;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001248
1249 if (len < 4)
1250 return -EINVAL;
1251
1252 if (copy_from_user(&data, buf, 4))
1253 return -EFAULT;
1254
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001255 ret = spu_acquire(ctx);
1256 if (ret)
1257 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001258 ctx->ops->signal2_write(ctx, data);
1259 spu_release(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001260
1261 return 4;
1262}
1263
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001264#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001265static int
1266spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001267{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001268#if SPUFS_SIGNAL_MAP_SIZE == 0x1000
1269 return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE);
1270#elif SPUFS_SIGNAL_MAP_SIZE == 0x10000
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001271 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole
1272 * signal 1 and 2 area
1273 */
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001274 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001275#else
1276#error unsupported page size
1277#endif
Mark Nutter6df10a82006-03-23 00:00:12 +01001278}
1279
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001280static const struct vm_operations_struct spufs_signal2_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001281 .fault = spufs_signal2_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001282};
1283
1284static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma)
1285{
1286 if (!(vma->vm_flags & VM_SHARED))
1287 return -EINVAL;
1288
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001289 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001290 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001291
1292 vma->vm_ops = &spufs_signal2_mmap_vmops;
1293 return 0;
1294}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001295#else /* SPUFS_MMAP_4K */
1296#define spufs_signal2_mmap NULL
1297#endif /* !SPUFS_MMAP_4K */
Mark Nutter6df10a82006-03-23 00:00:12 +01001298
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001299static const struct file_operations spufs_signal2_fops = {
Mark Nutter6df10a82006-03-23 00:00:12 +01001300 .open = spufs_signal2_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001301 .release = spufs_signal2_release,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001302 .read = spufs_signal2_read,
1303 .write = spufs_signal2_write,
Mark Nutter6df10a82006-03-23 00:00:12 +01001304 .mmap = spufs_signal2_mmap,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001305 .llseek = no_llseek,
Arnd Bergmann67207b92005-11-15 15:53:48 -05001306};
1307
Jeremy Kerrd054b362007-07-20 21:39:31 +02001308static const struct file_operations spufs_signal2_nosched_fops = {
1309 .open = spufs_signal2_open,
1310 .release = spufs_signal2_release,
1311 .write = spufs_signal2_write,
1312 .mmap = spufs_signal2_mmap,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001313 .llseek = no_llseek,
Jeremy Kerrd054b362007-07-20 21:39:31 +02001314};
1315
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001316/*
1317 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the
1318 * work of acquiring (or not) the SPU context before calling through
1319 * to the actual get routine. The set routine is called directly.
1320 */
1321#define SPU_ATTR_NOACQUIRE 0
1322#define SPU_ATTR_ACQUIRE 1
1323#define SPU_ATTR_ACQUIRE_SAVED 2
1324
1325#define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001326static int __##__get(void *data, u64 *val) \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001327{ \
1328 struct spu_context *ctx = data; \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001329 int ret = 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001330 \
1331 if (__acquire == SPU_ATTR_ACQUIRE) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001332 ret = spu_acquire(ctx); \
1333 if (ret) \
1334 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001335 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001336 spu_release(ctx); \
1337 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001338 ret = spu_acquire_saved(ctx); \
1339 if (ret) \
1340 return ret; \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001341 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001342 spu_release_saved(ctx); \
1343 } else \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001344 *val = __get(ctx); \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001345 \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001346 return 0; \
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001347} \
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001348DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt);
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001349
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001350static int spufs_signal1_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001351{
1352 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001353 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001354
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001355 ret = spu_acquire(ctx);
1356 if (ret)
1357 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001358 ctx->ops->signal1_type_set(ctx, val);
1359 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001360
1361 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001362}
1363
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001364static u64 spufs_signal1_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001365{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001366 return ctx->ops->signal1_type_get(ctx);
1367}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001368DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001369 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001370
Arnd Bergmann67207b92005-11-15 15:53:48 -05001371
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001372static int spufs_signal2_type_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001373{
1374 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001375 int ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001376
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001377 ret = spu_acquire(ctx);
1378 if (ret)
1379 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001380 ctx->ops->signal2_type_set(ctx, val);
1381 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001382
1383 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001384}
1385
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001386static u64 spufs_signal2_type_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001387{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001388 return ctx->ops->signal2_type_get(ctx);
1389}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001390DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get,
Jeremy Kerraf8b44e2008-03-25 13:15:11 +11001391 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001392
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001393#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001394static int
1395spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001396{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001397 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001398}
1399
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001400static const struct vm_operations_struct spufs_mss_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001401 .fault = spufs_mss_mmap_fault,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001402};
1403
1404/*
1405 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001406 */
1407static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma)
1408{
1409 if (!(vma->vm_flags & VM_SHARED))
1410 return -EINVAL;
1411
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001412 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001413 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001414
1415 vma->vm_ops = &spufs_mss_mmap_vmops;
1416 return 0;
1417}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001418#else /* SPUFS_MMAP_4K */
1419#define spufs_mss_mmap NULL
1420#endif /* !SPUFS_MMAP_4K */
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001421
1422static int spufs_mss_open(struct inode *inode, struct file *file)
1423{
1424 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001425 struct spu_context *ctx = i->i_ctx;
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001426
1427 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001428
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001429 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001430 if (!i->i_openers++)
1431 ctx->mss = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001432 mutex_unlock(&ctx->mapping_lock);
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001433 return nonseekable_open(inode, file);
1434}
1435
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001436static int
1437spufs_mss_release(struct inode *inode, struct file *file)
1438{
1439 struct spufs_inode_info *i = SPUFS_I(inode);
1440 struct spu_context *ctx = i->i_ctx;
1441
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001442 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001443 if (!--i->i_openers)
1444 ctx->mss = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001445 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001446 return 0;
1447}
1448
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001449static const struct file_operations spufs_mss_fops = {
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001450 .open = spufs_mss_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001451 .release = spufs_mss_release,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001452 .mmap = spufs_mss_mmap,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001453 .llseek = no_llseek,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001454};
1455
Nick Pigginb1e22702008-06-10 09:26:08 +10001456static int
1457spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001458{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001459 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001460}
1461
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001462static const struct vm_operations_struct spufs_psmap_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001463 .fault = spufs_psmap_mmap_fault,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001464};
1465
1466/*
1467 * mmap support for full problem state area [0x00000 - 0x1ffff].
1468 */
1469static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma)
1470{
1471 if (!(vma->vm_flags & VM_SHARED))
1472 return -EINVAL;
1473
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001474 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001475 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001476
1477 vma->vm_ops = &spufs_psmap_mmap_vmops;
1478 return 0;
1479}
1480
1481static int spufs_psmap_open(struct inode *inode, struct file *file)
1482{
1483 struct spufs_inode_info *i = SPUFS_I(inode);
Benjamin Herrenschmidt17e0e272007-02-13 11:46:08 +11001484 struct spu_context *ctx = i->i_ctx;
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001485
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001486 mutex_lock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001487 file->private_data = i->i_ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001488 if (!i->i_openers++)
1489 ctx->psmap = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001490 mutex_unlock(&ctx->mapping_lock);
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001491 return nonseekable_open(inode, file);
1492}
1493
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001494static int
1495spufs_psmap_release(struct inode *inode, struct file *file)
1496{
1497 struct spufs_inode_info *i = SPUFS_I(inode);
1498 struct spu_context *ctx = i->i_ctx;
1499
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001500 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001501 if (!--i->i_openers)
1502 ctx->psmap = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001503 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001504 return 0;
1505}
1506
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001507static const struct file_operations spufs_psmap_fops = {
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001508 .open = spufs_psmap_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001509 .release = spufs_psmap_release,
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001510 .mmap = spufs_psmap_mmap,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001511 .llseek = no_llseek,
arnd@arndb.ded9379c42006-06-19 20:33:21 +02001512};
1513
1514
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001515#if SPUFS_MMAP_4K
Nick Pigginb1e22702008-06-10 09:26:08 +10001516static int
1517spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Mark Nutter6df10a82006-03-23 00:00:12 +01001518{
Jeremy Kerr87ff6092008-07-01 10:22:50 +10001519 return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE);
Mark Nutter6df10a82006-03-23 00:00:12 +01001520}
1521
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001522static const struct vm_operations_struct spufs_mfc_mmap_vmops = {
Nick Pigginb1e22702008-06-10 09:26:08 +10001523 .fault = spufs_mfc_mmap_fault,
Mark Nutter6df10a82006-03-23 00:00:12 +01001524};
1525
1526/*
1527 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff].
Mark Nutter6df10a82006-03-23 00:00:12 +01001528 */
1529static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1530{
1531 if (!(vma->vm_flags & VM_SHARED))
1532 return -EINVAL;
1533
Benjamin Herrenschmidt78bde532007-02-13 11:46:06 +11001534 vma->vm_flags |= VM_IO | VM_PFNMAP;
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +00001535 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Mark Nutter6df10a82006-03-23 00:00:12 +01001536
1537 vma->vm_ops = &spufs_mfc_mmap_vmops;
1538 return 0;
1539}
Benjamin Herrenschmidt27d5bf22006-10-04 17:26:11 +02001540#else /* SPUFS_MMAP_4K */
1541#define spufs_mfc_mmap NULL
1542#endif /* !SPUFS_MMAP_4K */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001543
1544static int spufs_mfc_open(struct inode *inode, struct file *file)
1545{
1546 struct spufs_inode_info *i = SPUFS_I(inode);
1547 struct spu_context *ctx = i->i_ctx;
1548
1549 /* we don't want to deal with DMA into other processes */
1550 if (ctx->owner != current->mm)
1551 return -EINVAL;
1552
1553 if (atomic_read(&inode->i_count) != 1)
1554 return -EBUSY;
1555
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001556 mutex_lock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001557 file->private_data = ctx;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001558 if (!i->i_openers++)
1559 ctx->mfc = inode->i_mapping;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001560 mutex_unlock(&ctx->mapping_lock);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001561 return nonseekable_open(inode, file);
1562}
1563
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001564static int
1565spufs_mfc_release(struct inode *inode, struct file *file)
1566{
1567 struct spufs_inode_info *i = SPUFS_I(inode);
1568 struct spu_context *ctx = i->i_ctx;
1569
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001570 mutex_lock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001571 if (!--i->i_openers)
1572 ctx->mfc = NULL;
Christoph Hellwig47d3a5f2007-06-04 23:26:51 +10001573 mutex_unlock(&ctx->mapping_lock);
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001574 return 0;
1575}
1576
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001577/* interrupt-level mfc callback function. */
1578void spufs_mfc_callback(struct spu *spu)
1579{
1580 struct spu_context *ctx = spu->ctx;
1581
Luke Browninge65c2f62007-12-20 16:39:59 +09001582 if (!ctx)
1583 return;
1584
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001585 wake_up_all(&ctx->mfc_wq);
1586
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001587 pr_debug("%s %s\n", __func__, spu->name);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001588 if (ctx->mfc_fasync) {
1589 u32 free_elements, tagstatus;
1590 unsigned int mask;
1591
1592 /* no need for spu_acquire in interrupt context */
1593 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1594 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1595
1596 mask = 0;
1597 if (free_elements & 0xffff)
1598 mask |= POLLOUT;
1599 if (tagstatus & ctx->tagwait)
1600 mask |= POLLIN;
1601
1602 kill_fasync(&ctx->mfc_fasync, SIGIO, mask);
1603 }
1604}
1605
1606static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status)
1607{
1608 /* See if there is one tag group is complete */
1609 /* FIXME we need locking around tagwait */
1610 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait;
1611 ctx->tagwait &= ~*status;
1612 if (*status)
1613 return 1;
1614
1615 /* enable interrupt waiting for any tag group,
1616 may silently fail if interrupts are already enabled */
1617 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1618 return 0;
1619}
1620
1621static ssize_t spufs_mfc_read(struct file *file, char __user *buffer,
1622 size_t size, loff_t *pos)
1623{
1624 struct spu_context *ctx = file->private_data;
1625 int ret = -EINVAL;
1626 u32 status;
1627
1628 if (size != 4)
1629 goto out;
1630
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001631 ret = spu_acquire(ctx);
1632 if (ret)
1633 return ret;
1634
1635 ret = -EINVAL;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001636 if (file->f_flags & O_NONBLOCK) {
1637 status = ctx->ops->read_mfc_tagstatus(ctx);
1638 if (!(status & ctx->tagwait))
1639 ret = -EAGAIN;
1640 else
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001641 /* XXX(hch): shouldn't we clear ret here? */
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001642 ctx->tagwait &= ~status;
1643 } else {
1644 ret = spufs_wait(ctx->mfc_wq,
1645 spufs_read_mfc_tagstatus(ctx, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001646 if (ret)
1647 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001648 }
1649 spu_release(ctx);
1650
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001651 ret = 4;
1652 if (copy_to_user(buffer, &status, 4))
1653 ret = -EFAULT;
1654
1655out:
1656 return ret;
1657}
1658
1659static int spufs_check_valid_dma(struct mfc_dma_command *cmd)
1660{
Stephen Rothwell9477e452009-01-06 14:27:38 +00001661 pr_debug("queueing DMA %x %llx %x %x %x\n", cmd->lsa,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001662 cmd->ea, cmd->size, cmd->tag, cmd->cmd);
1663
1664 switch (cmd->cmd) {
1665 case MFC_PUT_CMD:
1666 case MFC_PUTF_CMD:
1667 case MFC_PUTB_CMD:
1668 case MFC_GET_CMD:
1669 case MFC_GETF_CMD:
1670 case MFC_GETB_CMD:
1671 break;
1672 default:
1673 pr_debug("invalid DMA opcode %x\n", cmd->cmd);
1674 return -EIO;
1675 }
1676
1677 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) {
Stephen Rothwell9477e452009-01-06 14:27:38 +00001678 pr_debug("invalid DMA alignment, ea %llx lsa %x\n",
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001679 cmd->ea, cmd->lsa);
1680 return -EIO;
1681 }
1682
1683 switch (cmd->size & 0xf) {
1684 case 1:
1685 break;
1686 case 2:
1687 if (cmd->lsa & 1)
1688 goto error;
1689 break;
1690 case 4:
1691 if (cmd->lsa & 3)
1692 goto error;
1693 break;
1694 case 8:
1695 if (cmd->lsa & 7)
1696 goto error;
1697 break;
1698 case 0:
1699 if (cmd->lsa & 15)
1700 goto error;
1701 break;
1702 error:
1703 default:
1704 pr_debug("invalid DMA alignment %x for size %x\n",
1705 cmd->lsa & 0xf, cmd->size);
1706 return -EIO;
1707 }
1708
1709 if (cmd->size > 16 * 1024) {
1710 pr_debug("invalid DMA size %x\n", cmd->size);
1711 return -EIO;
1712 }
1713
1714 if (cmd->tag & 0xfff0) {
1715 /* we reserve the higher tag numbers for kernel use */
1716 pr_debug("invalid DMA tag\n");
1717 return -EIO;
1718 }
1719
1720 if (cmd->class) {
1721 /* not supported in this version */
1722 pr_debug("invalid DMA class\n");
1723 return -EIO;
1724 }
1725
1726 return 0;
1727}
1728
1729static int spu_send_mfc_command(struct spu_context *ctx,
1730 struct mfc_dma_command cmd,
1731 int *error)
1732{
1733 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1734 if (*error == -EAGAIN) {
1735 /* wait for any tag group to complete
1736 so we have space for the new command */
1737 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1);
1738 /* try again, because the queue might be
1739 empty again */
1740 *error = ctx->ops->send_mfc_command(ctx, &cmd);
1741 if (*error == -EAGAIN)
1742 return 0;
1743 }
1744 return 1;
1745}
1746
1747static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer,
1748 size_t size, loff_t *pos)
1749{
1750 struct spu_context *ctx = file->private_data;
1751 struct mfc_dma_command cmd;
1752 int ret = -EINVAL;
1753
1754 if (size != sizeof cmd)
1755 goto out;
1756
1757 ret = -EFAULT;
1758 if (copy_from_user(&cmd, buffer, sizeof cmd))
1759 goto out;
1760
1761 ret = spufs_check_valid_dma(&cmd);
1762 if (ret)
1763 goto out;
1764
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001765 ret = spu_acquire(ctx);
1766 if (ret)
1767 goto out;
1768
Arnd Bergmann33bfd7a2007-12-20 16:39:59 +09001769 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE);
Akinobu Mita577f8f12007-04-23 21:08:18 +02001770 if (ret)
1771 goto out;
1772
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001773 if (file->f_flags & O_NONBLOCK) {
1774 ret = ctx->ops->send_mfc_command(ctx, &cmd);
1775 } else {
1776 int status;
1777 ret = spufs_wait(ctx->mfc_wq,
1778 spu_send_mfc_command(ctx, cmd, &status));
Christoph Hellwigeebead52008-02-08 15:50:41 +11001779 if (ret)
1780 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001781 if (status)
1782 ret = status;
1783 }
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001784
1785 if (ret)
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001786 goto out_unlock;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001787
1788 ctx->tagwait |= 1 << cmd.tag;
Masato Noguchi3692dc62006-11-20 18:45:07 +01001789 ret = size;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001790
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001791out_unlock:
1792 spu_release(ctx);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001793out:
1794 return ret;
1795}
1796
1797static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait)
1798{
1799 struct spu_context *ctx = file->private_data;
1800 u32 free_elements, tagstatus;
1801 unsigned int mask;
1802
Kazunori Asayama933b0e32007-06-29 10:58:08 +10001803 poll_wait(file, &ctx->mfc_wq, wait);
1804
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001805 /*
1806 * For now keep this uninterruptible and also ignore the rule
1807 * that poll should not sleep. Will be fixed later.
1808 */
1809 mutex_lock(&ctx->state_mutex);
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001810 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2);
1811 free_elements = ctx->ops->get_mfc_free_elements(ctx);
1812 tagstatus = ctx->ops->read_mfc_tagstatus(ctx);
1813 spu_release(ctx);
1814
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001815 mask = 0;
1816 if (free_elements & 0xffff)
1817 mask |= POLLOUT | POLLWRNORM;
1818 if (tagstatus & ctx->tagwait)
1819 mask |= POLLIN | POLLRDNORM;
1820
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001821 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001822 free_elements, tagstatus, ctx->tagwait);
1823
1824 return mask;
1825}
1826
Al Viro73b6af82006-06-25 16:42:33 -07001827static int spufs_mfc_flush(struct file *file, fl_owner_t id)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001828{
1829 struct spu_context *ctx = file->private_data;
1830 int ret;
1831
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001832 ret = spu_acquire(ctx);
1833 if (ret)
Christoph Hellwigeebead52008-02-08 15:50:41 +11001834 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001835#if 0
1836/* this currently hangs */
1837 ret = spufs_wait(ctx->mfc_wq,
1838 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2));
1839 if (ret)
1840 goto out;
1841 ret = spufs_wait(ctx->mfc_wq,
1842 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001843 if (ret)
1844 goto out;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001845#else
1846 ret = 0;
1847#endif
1848 spu_release(ctx);
Christoph Hellwigeebead52008-02-08 15:50:41 +11001849out:
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001850 return ret;
1851}
1852
Josef Bacik02c24a82011-07-16 20:44:56 -04001853static int spufs_mfc_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001854{
Josef Bacik02c24a82011-07-16 20:44:56 -04001855 struct inode *inode = file->f_path.dentry->d_inode;
1856 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
1857 if (!err) {
1858 mutex_lock(&inode->i_mutex);
1859 err = spufs_mfc_flush(file, NULL);
1860 mutex_unlock(&inode->i_mutex);
1861 }
1862 return err;
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001863}
1864
1865static int spufs_mfc_fasync(int fd, struct file *file, int on)
1866{
1867 struct spu_context *ctx = file->private_data;
1868
1869 return fasync_helper(fd, file, on, &ctx->mfc_fasync);
1870}
1871
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001872static const struct file_operations spufs_mfc_fops = {
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001873 .open = spufs_mfc_open,
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +02001874 .release = spufs_mfc_release,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001875 .read = spufs_mfc_read,
1876 .write = spufs_mfc_write,
1877 .poll = spufs_mfc_poll,
1878 .flush = spufs_mfc_flush,
1879 .fsync = spufs_mfc_fsync,
1880 .fasync = spufs_mfc_fasync,
Mark Nutter6df10a82006-03-23 00:00:12 +01001881 .mmap = spufs_mfc_mmap,
Arnd Bergmannfc153512010-09-14 10:22:33 +00001882 .llseek = no_llseek,
Arnd Bergmanna33a7d72006-03-23 00:00:11 +01001883};
1884
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001885static int spufs_npc_set(void *data, u64 val)
Arnd Bergmann67207b92005-11-15 15:53:48 -05001886{
1887 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001888 int ret;
1889
1890 ret = spu_acquire(ctx);
1891 if (ret)
1892 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001893 ctx->ops->npc_write(ctx, val);
1894 spu_release(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001895
1896 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -05001897}
1898
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001899static u64 spufs_npc_get(struct spu_context *ctx)
Michael Ellerman78810ff2007-09-19 14:38:12 +10001900{
1901 return ctx->ops->npc_read(ctx);
1902}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001903DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set,
1904 "0x%llx\n", SPU_ATTR_ACQUIRE);
Arnd Bergmann67207b92005-11-15 15:53:48 -05001905
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001906static int spufs_decr_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001907{
1908 struct spu_context *ctx = data;
1909 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001910 int ret;
1911
1912 ret = spu_acquire_saved(ctx);
1913 if (ret)
1914 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001915 lscsa->decr.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001916 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001917
1918 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001919}
1920
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001921static u64 spufs_decr_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001922{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001923 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001924 return lscsa->decr.slot[0];
1925}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001926DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set,
1927 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001928
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001929static int spufs_decr_status_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001930{
1931 struct spu_context *ctx = data;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001932 int ret;
1933
1934 ret = spu_acquire_saved(ctx);
1935 if (ret)
1936 return ret;
Masato Noguchid40a01d2007-07-20 21:39:38 +02001937 if (val)
1938 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING;
1939 else
1940 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001941 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001942
1943 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001944}
1945
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001946static u64 spufs_decr_status_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001947{
Masato Noguchid40a01d2007-07-20 21:39:38 +02001948 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING)
1949 return SPU_DECR_STATUS_RUNNING;
1950 else
1951 return 0;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001952}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001953DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get,
1954 spufs_decr_status_set, "0x%llx\n",
1955 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001956
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001957static int spufs_event_mask_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001958{
1959 struct spu_context *ctx = data;
1960 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001961 int ret;
1962
1963 ret = spu_acquire_saved(ctx);
1964 if (ret)
1965 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001966 lscsa->event_mask.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02001967 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001968
1969 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001970}
1971
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001972static u64 spufs_event_mask_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001973{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001974 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001975 return lscsa->event_mask.slot[0];
1976}
1977
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001978DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get,
1979 spufs_event_mask_set, "0x%llx\n",
1980 SPU_ATTR_ACQUIRE_SAVED);
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001981
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001982static u64 spufs_event_status_get(struct spu_context *ctx)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001983{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001984 struct spu_state *state = &ctx->csa;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001985 u64 stat;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001986 stat = state->spu_chnlcnt_RW[0];
1987 if (stat)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001988 return state->spu_chnldata_RW[0];
1989 return 0;
1990}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10001991DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get,
1992 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01001993
Christoph Hellwig197b1a82007-12-20 16:39:59 +09001994static int spufs_srr0_set(void *data, u64 val)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05001995{
1996 struct spu_context *ctx = data;
1997 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09001998 int ret;
1999
2000 ret = spu_acquire_saved(ctx);
2001 if (ret)
2002 return ret;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002003 lscsa->srr0.slot[0] = (u32) val;
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002004 spu_release_saved(ctx);
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002005
2006 return 0;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002007}
2008
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002009static u64 spufs_srr0_get(struct spu_context *ctx)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002010{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002011 struct spu_lscsa *lscsa = ctx->csa.lscsa;
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002012 return lscsa->srr0.slot[0];
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002013}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002014DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set,
2015 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED)
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002016
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002017static u64 spufs_id_get(struct spu_context *ctx)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002018{
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002019 u64 num;
2020
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002021 if (ctx->state == SPU_STATE_RUNNABLE)
2022 num = ctx->spu->number;
2023 else
2024 num = (unsigned int)-1;
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002025
2026 return num;
2027}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002028DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n",
2029 SPU_ATTR_ACQUIRE)
arnd@arndb.de7b1a7012006-06-19 20:33:24 +02002030
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002031static u64 spufs_object_id_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002032{
2033 /* FIXME: Should there really be no locking here? */
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002034 return ctx->object_id;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002035}
2036
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002037static int spufs_object_id_set(void *data, u64 id)
Arnd Bergmann86767272006-10-04 17:26:21 +02002038{
2039 struct spu_context *ctx = data;
2040 ctx->object_id = id;
Christoph Hellwig197b1a82007-12-20 16:39:59 +09002041
2042 return 0;
Arnd Bergmann86767272006-10-04 17:26:21 +02002043}
2044
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002045DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get,
2046 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE);
Arnd Bergmann86767272006-10-04 17:26:21 +02002047
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002048static u64 spufs_lslr_get(struct spu_context *ctx)
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002049{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002050 return ctx->csa.priv2.spu_lslr_RW;
2051}
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002052DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n",
2053 SPU_ATTR_ACQUIRE_SAVED);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002054
2055static int spufs_info_open(struct inode *inode, struct file *file)
2056{
2057 struct spufs_inode_info *i = SPUFS_I(inode);
2058 struct spu_context *ctx = i->i_ctx;
2059 file->private_data = ctx;
2060 return 0;
2061}
2062
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002063static int spufs_caps_show(struct seq_file *s, void *private)
2064{
2065 struct spu_context *ctx = s->private;
2066
2067 if (!(ctx->flags & SPU_CREATE_NOSCHED))
2068 seq_puts(s, "sched\n");
2069 if (!(ctx->flags & SPU_CREATE_ISOLATE))
2070 seq_puts(s, "step\n");
2071 return 0;
2072}
2073
2074static int spufs_caps_open(struct inode *inode, struct file *file)
2075{
2076 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx);
2077}
2078
2079static const struct file_operations spufs_caps_fops = {
2080 .open = spufs_caps_open,
2081 .read = seq_read,
2082 .llseek = seq_lseek,
2083 .release = single_release,
2084};
2085
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002086static ssize_t __spufs_mbox_info_read(struct spu_context *ctx,
2087 char __user *buf, size_t len, loff_t *pos)
2088{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002089 u32 data;
2090
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002091 /* EOF if there's no entry in the mbox */
2092 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff))
2093 return 0;
2094
2095 data = ctx->csa.prob.pu_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002096
2097 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2098}
2099
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002100static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf,
2101 size_t len, loff_t *pos)
2102{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002103 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002104 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002105
2106 if (!access_ok(VERIFY_WRITE, buf, len))
2107 return -EFAULT;
2108
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002109 ret = spu_acquire_saved(ctx);
2110 if (ret)
2111 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002112 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002113 ret = __spufs_mbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002114 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002115 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002116
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002117 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002118}
2119
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002120static const struct file_operations spufs_mbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002121 .open = spufs_info_open,
2122 .read = spufs_mbox_info_read,
2123 .llseek = generic_file_llseek,
2124};
2125
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002126static ssize_t __spufs_ibox_info_read(struct spu_context *ctx,
2127 char __user *buf, size_t len, loff_t *pos)
2128{
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002129 u32 data;
2130
Jeremy Kerrcbea9232007-12-20 16:39:59 +09002131 /* EOF if there's no entry in the ibox */
2132 if (!(ctx->csa.prob.mb_stat_R & 0xff0000))
2133 return 0;
2134
2135 data = ctx->csa.priv2.puint_mb_R;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002136
2137 return simple_read_from_buffer(buf, len, pos, &data, sizeof data);
2138}
2139
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002140static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf,
2141 size_t len, loff_t *pos)
2142{
2143 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002144 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002145
2146 if (!access_ok(VERIFY_WRITE, buf, len))
2147 return -EFAULT;
2148
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002149 ret = spu_acquire_saved(ctx);
2150 if (ret)
2151 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002152 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002153 ret = __spufs_ibox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002154 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002155 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002156
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002157 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002158}
2159
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002160static const struct file_operations spufs_ibox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002161 .open = spufs_info_open,
2162 .read = spufs_ibox_info_read,
2163 .llseek = generic_file_llseek,
2164};
2165
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002166static ssize_t __spufs_wbox_info_read(struct spu_context *ctx,
2167 char __user *buf, size_t len, loff_t *pos)
2168{
2169 int i, cnt;
2170 u32 data[4];
2171 u32 wbox_stat;
2172
2173 wbox_stat = ctx->csa.prob.mb_stat_R;
2174 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8);
2175 for (i = 0; i < cnt; i++) {
2176 data[i] = ctx->csa.spu_mailbox_data[i];
2177 }
2178
2179 return simple_read_from_buffer(buf, len, pos, &data,
2180 cnt * sizeof(u32));
2181}
2182
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002183static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf,
2184 size_t len, loff_t *pos)
2185{
2186 struct spu_context *ctx = file->private_data;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002187 int ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002188
2189 if (!access_ok(VERIFY_WRITE, buf, len))
2190 return -EFAULT;
2191
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002192 ret = spu_acquire_saved(ctx);
2193 if (ret)
2194 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002195 spin_lock(&ctx->csa.register_lock);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002196 ret = __spufs_wbox_info_read(ctx, buf, len, pos);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002197 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002198 spu_release_saved(ctx);
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002199
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002200 return ret;
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002201}
2202
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002203static const struct file_operations spufs_wbox_info_fops = {
Dwayne Grant McConnell69a2f002006-11-20 18:45:00 +01002204 .open = spufs_info_open,
2205 .read = spufs_wbox_info_read,
2206 .llseek = generic_file_llseek,
2207};
2208
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002209static ssize_t __spufs_dma_info_read(struct spu_context *ctx,
2210 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002211{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002212 struct spu_dma_info info;
2213 struct mfc_cq_sr *qp, *spuqp;
2214 int i;
2215
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002216 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW;
2217 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0];
2218 info.dma_info_status = ctx->csa.spu_chnldata_RW[24];
2219 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25];
2220 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27];
2221 for (i = 0; i < 16; i++) {
2222 qp = &info.dma_info_command_data[i];
2223 spuqp = &ctx->csa.priv2.spuq[i];
2224
2225 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW;
2226 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW;
2227 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW;
2228 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW;
2229 }
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002230
2231 return simple_read_from_buffer(buf, len, pos, &info,
2232 sizeof info);
2233}
2234
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002235static ssize_t spufs_dma_info_read(struct file *file, char __user *buf,
2236 size_t len, loff_t *pos)
2237{
2238 struct spu_context *ctx = file->private_data;
2239 int ret;
2240
2241 if (!access_ok(VERIFY_WRITE, buf, len))
2242 return -EFAULT;
2243
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002244 ret = spu_acquire_saved(ctx);
2245 if (ret)
2246 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002247 spin_lock(&ctx->csa.register_lock);
2248 ret = __spufs_dma_info_read(ctx, buf, len, pos);
2249 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002250 spu_release_saved(ctx);
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002251
2252 return ret;
2253}
2254
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002255static const struct file_operations spufs_dma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002256 .open = spufs_info_open,
2257 .read = spufs_dma_info_read,
Arnd Bergmannfc153512010-09-14 10:22:33 +00002258 .llseek = no_llseek,
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002259};
2260
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002261static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx,
2262 char __user *buf, size_t len, loff_t *pos)
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002263{
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002264 struct spu_proxydma_info info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002265 struct mfc_cq_sr *qp, *puqp;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002266 int ret = sizeof info;
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002267 int i;
2268
2269 if (len < ret)
2270 return -EINVAL;
2271
2272 if (!access_ok(VERIFY_WRITE, buf, len))
2273 return -EFAULT;
2274
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002275 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW;
2276 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW;
2277 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R;
2278 for (i = 0; i < 8; i++) {
2279 qp = &info.proxydma_info_command_data[i];
2280 puqp = &ctx->csa.priv2.puq[i];
2281
2282 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW;
2283 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW;
2284 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW;
2285 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW;
2286 }
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002287
2288 return simple_read_from_buffer(buf, len, pos, &info,
2289 sizeof info);
2290}
2291
2292static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf,
2293 size_t len, loff_t *pos)
2294{
2295 struct spu_context *ctx = file->private_data;
2296 int ret;
2297
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002298 ret = spu_acquire_saved(ctx);
2299 if (ret)
2300 return ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002301 spin_lock(&ctx->csa.register_lock);
2302 ret = __spufs_proxydma_info_read(ctx, buf, len, pos);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002303 spin_unlock(&ctx->csa.register_lock);
Christoph Hellwig27b1ea02007-07-20 21:39:34 +02002304 spu_release_saved(ctx);
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002305
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002306 return ret;
2307}
2308
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08002309static const struct file_operations spufs_proxydma_info_fops = {
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002310 .open = spufs_info_open,
2311 .read = spufs_proxydma_info_read,
Arnd Bergmannfc153512010-09-14 10:22:33 +00002312 .llseek = no_llseek,
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002313};
2314
Christoph Hellwig476273a2007-06-29 10:58:01 +10002315static int spufs_show_tid(struct seq_file *s, void *private)
2316{
2317 struct spu_context *ctx = s->private;
2318
2319 seq_printf(s, "%d\n", ctx->tid);
2320 return 0;
2321}
2322
2323static int spufs_tid_open(struct inode *inode, struct file *file)
2324{
2325 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx);
2326}
2327
2328static const struct file_operations spufs_tid_fops = {
2329 .open = spufs_tid_open,
2330 .read = seq_read,
2331 .llseek = seq_lseek,
2332 .release = single_release,
2333};
2334
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002335static const char *ctx_state_names[] = {
2336 "user", "system", "iowait", "loaded"
2337};
2338
2339static unsigned long long spufs_acct_time(struct spu_context *ctx,
Andre Detsch27ec41d2007-07-20 21:39:33 +02002340 enum spu_utilization_state state)
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002341{
Andre Detsch27ec41d2007-07-20 21:39:33 +02002342 struct timespec ts;
2343 unsigned long long time = ctx->stats.times[state];
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002344
Andre Detsch27ec41d2007-07-20 21:39:33 +02002345 /*
2346 * In general, utilization statistics are updated by the controlling
2347 * thread as the spu context moves through various well defined
2348 * state transitions, but if the context is lazily loaded its
2349 * utilization statistics are not updated as the controlling thread
2350 * is not tightly coupled with the execution of the spu context. We
2351 * calculate and apply the time delta from the last recorded state
2352 * of the spu context.
2353 */
2354 if (ctx->spu && ctx->stats.util_state == state) {
2355 ktime_get_ts(&ts);
2356 time += timespec_to_ns(&ts) - ctx->stats.tstamp;
2357 }
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002358
Andre Detsch27ec41d2007-07-20 21:39:33 +02002359 return time / NSEC_PER_MSEC;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002360}
2361
2362static unsigned long long spufs_slb_flts(struct spu_context *ctx)
2363{
2364 unsigned long long slb_flts = ctx->stats.slb_flt;
2365
2366 if (ctx->state == SPU_STATE_RUNNABLE) {
2367 slb_flts += (ctx->spu->stats.slb_flt -
2368 ctx->stats.slb_flt_base);
2369 }
2370
2371 return slb_flts;
2372}
2373
2374static unsigned long long spufs_class2_intrs(struct spu_context *ctx)
2375{
2376 unsigned long long class2_intrs = ctx->stats.class2_intr;
2377
2378 if (ctx->state == SPU_STATE_RUNNABLE) {
2379 class2_intrs += (ctx->spu->stats.class2_intr -
2380 ctx->stats.class2_intr_base);
2381 }
2382
2383 return class2_intrs;
2384}
2385
2386
2387static int spufs_show_stat(struct seq_file *s, void *private)
2388{
2389 struct spu_context *ctx = s->private;
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002390 int ret;
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002391
Christoph Hellwigc9101bd2007-12-20 16:39:59 +09002392 ret = spu_acquire(ctx);
2393 if (ret)
2394 return ret;
2395
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002396 seq_printf(s, "%s %llu %llu %llu %llu "
2397 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
Andre Detsch27ec41d2007-07-20 21:39:33 +02002398 ctx_state_names[ctx->stats.util_state],
2399 spufs_acct_time(ctx, SPU_UTIL_USER),
2400 spufs_acct_time(ctx, SPU_UTIL_SYSTEM),
2401 spufs_acct_time(ctx, SPU_UTIL_IOWAIT),
2402 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED),
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002403 ctx->stats.vol_ctx_switch,
2404 ctx->stats.invol_ctx_switch,
2405 spufs_slb_flts(ctx),
2406 ctx->stats.hash_flt,
2407 ctx->stats.min_flt,
2408 ctx->stats.maj_flt,
2409 spufs_class2_intrs(ctx),
2410 ctx->stats.libassist);
2411 spu_release(ctx);
2412 return 0;
2413}
2414
2415static int spufs_stat_open(struct inode *inode, struct file *file)
2416{
2417 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx);
2418}
2419
2420static const struct file_operations spufs_stat_fops = {
2421 .open = spufs_stat_open,
2422 .read = seq_read,
2423 .llseek = seq_lseek,
2424 .release = single_release,
2425};
2426
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002427static inline int spufs_switch_log_used(struct spu_context *ctx)
2428{
2429 return (ctx->switch_log->head - ctx->switch_log->tail) %
2430 SWITCH_LOG_BUFSIZE;
2431}
2432
2433static inline int spufs_switch_log_avail(struct spu_context *ctx)
2434{
2435 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx);
2436}
2437
2438static int spufs_switch_log_open(struct inode *inode, struct file *file)
2439{
2440 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002441 int rc;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002442
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002443 rc = spu_acquire(ctx);
2444 if (rc)
2445 return rc;
2446
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002447 if (ctx->switch_log) {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002448 rc = -EBUSY;
2449 goto out;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002450 }
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002451
Jeremy Kerr837ef882008-10-17 12:02:31 +11002452 ctx->switch_log = kmalloc(sizeof(struct switch_log) +
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002453 SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry),
2454 GFP_KERNEL);
2455
2456 if (!ctx->switch_log) {
2457 rc = -ENOMEM;
2458 goto out;
2459 }
2460
Jeremy Kerr837ef882008-10-17 12:02:31 +11002461 ctx->switch_log->head = ctx->switch_log->tail = 0;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002462 init_waitqueue_head(&ctx->switch_log->wait);
2463 rc = 0;
2464
2465out:
2466 spu_release(ctx);
2467 return rc;
2468}
2469
2470static int spufs_switch_log_release(struct inode *inode, struct file *file)
2471{
2472 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2473 int rc;
2474
2475 rc = spu_acquire(ctx);
2476 if (rc)
2477 return rc;
2478
2479 kfree(ctx->switch_log);
2480 ctx->switch_log = NULL;
2481 spu_release(ctx);
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002482
2483 return 0;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002484}
2485
2486static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n)
2487{
2488 struct switch_log_entry *p;
2489
2490 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE;
2491
2492 return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n",
2493 (unsigned int) p->tstamp.tv_sec,
2494 (unsigned int) p->tstamp.tv_nsec,
2495 p->spu_id,
2496 (unsigned int) p->type,
2497 (unsigned int) p->val,
2498 (unsigned long long) p->timebase);
2499}
2500
2501static ssize_t spufs_switch_log_read(struct file *file, char __user *buf,
2502 size_t len, loff_t *ppos)
2503{
2504 struct inode *inode = file->f_path.dentry->d_inode;
2505 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2506 int error = 0, cnt = 0;
2507
roel kluin17e37672009-10-14 05:32:28 +00002508 if (!buf)
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002509 return -EINVAL;
2510
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002511 error = spu_acquire(ctx);
2512 if (error)
2513 return error;
2514
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002515 while (cnt < len) {
2516 char tbuf[128];
2517 int width;
2518
Jeremy Kerr14f693e2008-10-16 10:51:46 +11002519 if (spufs_switch_log_used(ctx) == 0) {
2520 if (cnt > 0) {
2521 /* If there's data ready to go, we can
2522 * just return straight away */
2523 break;
2524
2525 } else if (file->f_flags & O_NONBLOCK) {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002526 error = -EAGAIN;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002527 break;
Jeremy Kerr14f693e2008-10-16 10:51:46 +11002528
2529 } else {
2530 /* spufs_wait will drop the mutex and
2531 * re-acquire, but since we're in read(), the
2532 * file cannot be _released (and so
2533 * ctx->switch_log is stable).
2534 */
2535 error = spufs_wait(ctx->switch_log->wait,
2536 spufs_switch_log_used(ctx) > 0);
2537
2538 /* On error, spufs_wait returns without the
2539 * state mutex held */
2540 if (error)
2541 return error;
2542
2543 /* We may have had entries read from underneath
2544 * us while we dropped the mutex in spufs_wait,
2545 * so re-check */
2546 if (spufs_switch_log_used(ctx) == 0)
2547 continue;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002548 }
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002549 }
2550
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002551 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf));
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002552 if (width < len)
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002553 ctx->switch_log->tail =
2554 (ctx->switch_log->tail + 1) %
2555 SWITCH_LOG_BUFSIZE;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002556 else
2557 /* If the record is greater than space available return
2558 * partial buffer (so far) */
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002559 break;
2560
2561 error = copy_to_user(buf + cnt, tbuf, width);
2562 if (error)
2563 break;
2564 cnt += width;
2565 }
2566
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002567 spu_release(ctx);
2568
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002569 return cnt == 0 ? error : cnt;
2570}
2571
2572static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait)
2573{
2574 struct inode *inode = file->f_path.dentry->d_inode;
2575 struct spu_context *ctx = SPUFS_I(inode)->i_ctx;
2576 unsigned int mask = 0;
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002577 int rc;
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002578
2579 poll_wait(file, &ctx->switch_log->wait, wait);
2580
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002581 rc = spu_acquire(ctx);
2582 if (rc)
2583 return rc;
2584
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002585 if (spufs_switch_log_used(ctx) > 0)
2586 mask |= POLLIN;
2587
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002588 spu_release(ctx);
2589
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002590 return mask;
2591}
2592
2593static const struct file_operations spufs_switch_log_fops = {
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002594 .owner = THIS_MODULE,
2595 .open = spufs_switch_log_open,
2596 .read = spufs_switch_log_read,
2597 .poll = spufs_switch_log_poll,
2598 .release = spufs_switch_log_release,
Arnd Bergmannfc153512010-09-14 10:22:33 +00002599 .llseek = no_llseek,
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002600};
2601
Jeremy Kerrf5ed0eb2008-10-16 10:03:46 +11002602/**
2603 * Log a context switch event to a switch log reader.
2604 *
2605 * Must be called with ctx->state_mutex held.
2606 */
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002607void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx,
2608 u32 type, u32 val)
2609{
2610 if (!ctx->switch_log)
2611 return;
2612
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002613 if (spufs_switch_log_avail(ctx) > 1) {
2614 struct switch_log_entry *p;
2615
2616 p = ctx->switch_log->log + ctx->switch_log->head;
2617 ktime_get_ts(&p->tstamp);
2618 p->timebase = get_tb();
2619 p->spu_id = spu ? spu->number : -1;
2620 p->type = type;
2621 p->val = val;
2622
2623 ctx->switch_log->head =
2624 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE;
2625 }
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002626
2627 wake_up(&ctx->switch_log->wait);
2628}
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002629
Luke Browning46deed62008-06-16 11:36:43 +10002630static int spufs_show_ctx(struct seq_file *s, void *private)
2631{
2632 struct spu_context *ctx = s->private;
2633 u64 mfc_control_RW;
2634
2635 mutex_lock(&ctx->state_mutex);
2636 if (ctx->spu) {
2637 struct spu *spu = ctx->spu;
2638 struct spu_priv2 __iomem *priv2 = spu->priv2;
2639
2640 spin_lock_irq(&spu->register_lock);
2641 mfc_control_RW = in_be64(&priv2->mfc_control_RW);
2642 spin_unlock_irq(&spu->register_lock);
2643 } else {
2644 struct spu_state *csa = &ctx->csa;
2645
2646 mfc_control_RW = csa->priv2.mfc_control_RW;
2647 }
2648
2649 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)"
Stephen Rothwell9477e452009-01-06 14:27:38 +00002650 " %c %llx %llx %llx %llx %x %x\n",
Luke Browning46deed62008-06-16 11:36:43 +10002651 ctx->state == SPU_STATE_SAVED ? 'S' : 'R',
2652 ctx->flags,
2653 ctx->sched_flags,
2654 ctx->prio,
2655 ctx->time_slice,
2656 ctx->spu ? ctx->spu->number : -1,
2657 !list_empty(&ctx->rq) ? 'q' : ' ',
2658 ctx->csa.class_0_pending,
2659 ctx->csa.class_0_dar,
2660 ctx->csa.class_1_dsisr,
2661 mfc_control_RW,
2662 ctx->ops->runcntl_read(ctx),
2663 ctx->ops->status_read(ctx));
2664
2665 mutex_unlock(&ctx->state_mutex);
2666
2667 return 0;
2668}
2669
2670static int spufs_ctx_open(struct inode *inode, struct file *file)
2671{
2672 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx);
2673}
2674
2675static const struct file_operations spufs_ctx_fops = {
2676 .open = spufs_ctx_open,
2677 .read = seq_read,
2678 .llseek = seq_lseek,
2679 .release = single_release,
2680};
2681
Jeremy Kerr74254642009-02-17 11:44:14 +11002682const struct spufs_tree_descr spufs_dir_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002683 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002684 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
2685 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002686 { "mbox", &spufs_mbox_fops, 0444, },
2687 { "ibox", &spufs_ibox_fops, 0444, },
2688 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002689 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2690 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2691 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerr603c4612007-09-26 10:53:45 +10002692 { "signal1", &spufs_signal1_fops, 0666, },
2693 { "signal2", &spufs_signal2_fops, 0666, },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002694 { "signal1_type", &spufs_signal1_type, 0666, },
2695 { "signal2_type", &spufs_signal2_type, 0666, },
Mark Nutter6df10a82006-03-23 00:00:12 +01002696 { "cntl", &spufs_cntl_fops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002697 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002698 { "lslr", &spufs_lslr_ops, 0444, },
2699 { "mfc", &spufs_mfc_fops, 0666, },
2700 { "mss", &spufs_mss_fops, 0666, },
2701 { "npc", &spufs_npc_ops, 0666, },
2702 { "srr0", &spufs_srr0_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002703 { "decr", &spufs_decr_ops, 0666, },
2704 { "decr_status", &spufs_decr_status_ops, 0666, },
Arnd Bergmann8b3d6662005-11-15 15:53:52 -05002705 { "event_mask", &spufs_event_mask_ops, 0666, },
Dwayne Grant McConnellb9e3bd72006-11-20 18:44:58 +01002706 { "event_status", &spufs_event_status_ops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002707 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Arnd Bergmann86767272006-10-04 17:26:21 +02002708 { "phys-id", &spufs_id_ops, 0666, },
2709 { "object-id", &spufs_object_id_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002710 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), },
2711 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), },
2712 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), },
2713 { "dma_info", &spufs_dma_info_fops, 0444,
2714 sizeof(struct spu_dma_info), },
2715 { "proxydma_info", &spufs_proxydma_info_fops, 0444,
2716 sizeof(struct spu_proxydma_info)},
Christoph Hellwig476273a2007-06-29 10:58:01 +10002717 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002718 { "stat", &spufs_stat_fops, 0444, },
Christoph Hellwig5158e9b2008-04-29 17:08:38 +10002719 { "switch_log", &spufs_switch_log_fops, 0444 },
Arnd Bergmann67207b92005-11-15 15:53:48 -05002720 {},
2721};
Mark Nutter5737edd2006-10-24 18:31:16 +02002722
Jeremy Kerr74254642009-02-17 11:44:14 +11002723const struct spufs_tree_descr spufs_dir_nosched_contents[] = {
Benjamin Herrenschmidtcbe709c2007-06-04 15:15:38 +10002724 { "capabilities", &spufs_caps_fops, 0444, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002725 { "mem", &spufs_mem_fops, 0666, LS_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002726 { "mbox", &spufs_mbox_fops, 0444, },
2727 { "ibox", &spufs_ibox_fops, 0444, },
2728 { "wbox", &spufs_wbox_fops, 0222, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002729 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), },
2730 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), },
2731 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), },
Jeremy Kerrd054b362007-07-20 21:39:31 +02002732 { "signal1", &spufs_signal1_nosched_fops, 0222, },
2733 { "signal2", &spufs_signal2_nosched_fops, 0222, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002734 { "signal1_type", &spufs_signal1_type, 0666, },
2735 { "signal2_type", &spufs_signal2_type, 0666, },
2736 { "mss", &spufs_mss_fops, 0666, },
2737 { "mfc", &spufs_mfc_fops, 0666, },
2738 { "cntl", &spufs_cntl_fops, 0666, },
2739 { "npc", &spufs_npc_ops, 0666, },
Jeremy Kerr6f7dde82008-06-30 14:38:37 +10002740 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002741 { "phys-id", &spufs_id_ops, 0666, },
2742 { "object-id", &spufs_object_id_ops, 0666, },
Christoph Hellwig476273a2007-06-29 10:58:01 +10002743 { "tid", &spufs_tid_fops, 0444, },
Christoph Hellwige9f8a0b2007-06-29 10:58:03 +10002744 { "stat", &spufs_stat_fops, 0444, },
Jeremy Kerr2c3e4782008-07-03 11:42:20 +10002745 {},
2746};
2747
Jeremy Kerr74254642009-02-17 11:44:14 +11002748const struct spufs_tree_descr spufs_dir_debug_contents[] = {
Luke Browning46deed62008-06-16 11:36:43 +10002749 { ".ctx", &spufs_ctx_fops, 0444, },
Mark Nutter5737edd2006-10-24 18:31:16 +02002750 {},
2751};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002752
Jeremy Kerr74254642009-02-17 11:44:14 +11002753const struct spufs_coredump_reader spufs_coredump_read[] = {
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002754 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])},
2755 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002756 { "lslr", NULL, spufs_lslr_get, 19 },
2757 { "decr", NULL, spufs_decr_get, 19 },
2758 { "decr_status", NULL, spufs_decr_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002759 { "mem", __spufs_mem_read, NULL, LS_SIZE, },
2760 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002761 { "signal1_type", NULL, spufs_signal1_type_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002762 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) },
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002763 { "signal2_type", NULL, spufs_signal2_type_get, 19 },
2764 { "event_mask", NULL, spufs_event_mask_get, 19 },
2765 { "event_status", NULL, spufs_event_status_get, 19 },
Michael Ellerman4fca9c42007-09-19 14:38:12 +10002766 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) },
2767 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) },
2768 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)},
2769 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)},
2770 { "proxydma_info", __spufs_proxydma_info_read,
2771 NULL, sizeof(struct spu_proxydma_info)},
Michael Ellerman104f0cc2007-09-19 14:38:12 +10002772 { "object-id", NULL, spufs_object_id_get, 19 },
2773 { "npc", NULL, spufs_npc_get, 19 },
Michael Ellerman936d5bf2007-09-19 14:38:12 +10002774 { NULL },
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01002775};