Commit | Line | Data |
---|---|---|
67207b96 AB |
1 | /* |
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 | ||
a33a7d73 AB |
23 | #undef DEBUG |
24 | ||
67207b96 AB |
25 | #include <linux/fs.h> |
26 | #include <linux/ioctl.h> | |
27 | #include <linux/module.h> | |
d88cfffa | 28 | #include <linux/pagemap.h> |
67207b96 | 29 | #include <linux/poll.h> |
5110459f | 30 | #include <linux/ptrace.h> |
67207b96 AB |
31 | |
32 | #include <asm/io.h> | |
33 | #include <asm/semaphore.h> | |
34 | #include <asm/spu.h> | |
35 | #include <asm/uaccess.h> | |
36 | ||
37 | #include "spufs.h" | |
38 | ||
27d5bf2a BH |
39 | #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000) |
40 | ||
8b3d6663 | 41 | |
67207b96 AB |
42 | static int |
43 | spufs_mem_open(struct inode *inode, struct file *file) | |
44 | { | |
45 | struct spufs_inode_info *i = SPUFS_I(inode); | |
6df10a82 MN |
46 | struct spu_context *ctx = i->i_ctx; |
47 | file->private_data = ctx; | |
48 | file->f_mapping = inode->i_mapping; | |
49 | ctx->local_store = inode->i_mapping; | |
67207b96 AB |
50 | return 0; |
51 | } | |
52 | ||
53 | static ssize_t | |
54 | spufs_mem_read(struct file *file, char __user *buffer, | |
55 | size_t size, loff_t *pos) | |
56 | { | |
8b3d6663 AB |
57 | struct spu_context *ctx = file->private_data; |
58 | char *local_store; | |
67207b96 AB |
59 | int ret; |
60 | ||
8b3d6663 | 61 | spu_acquire(ctx); |
67207b96 | 62 | |
8b3d6663 AB |
63 | local_store = ctx->ops->get_ls(ctx); |
64 | ret = simple_read_from_buffer(buffer, size, pos, local_store, LS_SIZE); | |
67207b96 | 65 | |
8b3d6663 | 66 | spu_release(ctx); |
67207b96 AB |
67 | return ret; |
68 | } | |
69 | ||
70 | static ssize_t | |
71 | spufs_mem_write(struct file *file, const char __user *buffer, | |
72 | size_t size, loff_t *pos) | |
73 | { | |
74 | struct spu_context *ctx = file->private_data; | |
8b3d6663 AB |
75 | char *local_store; |
76 | int ret; | |
67207b96 AB |
77 | |
78 | size = min_t(ssize_t, LS_SIZE - *pos, size); | |
79 | if (size <= 0) | |
80 | return -EFBIG; | |
81 | *pos += size; | |
8b3d6663 AB |
82 | |
83 | spu_acquire(ctx); | |
84 | ||
85 | local_store = ctx->ops->get_ls(ctx); | |
86 | ret = copy_from_user(local_store + *pos - size, | |
87 | buffer, size) ? -EFAULT : size; | |
88 | ||
89 | spu_release(ctx); | |
90 | return ret; | |
67207b96 AB |
91 | } |
92 | ||
8b3d6663 AB |
93 | static struct page * |
94 | spufs_mem_mmap_nopage(struct vm_area_struct *vma, | |
95 | unsigned long address, int *type) | |
96 | { | |
97 | struct page *page = NOPAGE_SIGBUS; | |
98 | ||
99 | struct spu_context *ctx = vma->vm_file->private_data; | |
100 | unsigned long offset = address - vma->vm_start; | |
101 | offset += vma->vm_pgoff << PAGE_SHIFT; | |
102 | ||
103 | spu_acquire(ctx); | |
104 | ||
ac91cb8d AB |
105 | if (ctx->state == SPU_STATE_SAVED) { |
106 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) | |
107 | & ~(_PAGE_NO_CACHE | _PAGE_GUARDED)); | |
8b3d6663 | 108 | page = vmalloc_to_page(ctx->csa.lscsa->ls + offset); |
ac91cb8d AB |
109 | } else { |
110 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) | |
111 | | _PAGE_NO_CACHE | _PAGE_GUARDED); | |
8b3d6663 AB |
112 | page = pfn_to_page((ctx->spu->local_store_phys + offset) |
113 | >> PAGE_SHIFT); | |
ac91cb8d | 114 | } |
8b3d6663 AB |
115 | spu_release(ctx); |
116 | ||
117 | if (type) | |
118 | *type = VM_FAULT_MINOR; | |
119 | ||
d88cfffa | 120 | page_cache_get(page); |
8b3d6663 AB |
121 | return page; |
122 | } | |
123 | ||
124 | static struct vm_operations_struct spufs_mem_mmap_vmops = { | |
125 | .nopage = spufs_mem_mmap_nopage, | |
126 | }; | |
127 | ||
67207b96 AB |
128 | static int |
129 | spufs_mem_mmap(struct file *file, struct vm_area_struct *vma) | |
130 | { | |
8b3d6663 AB |
131 | if (!(vma->vm_flags & VM_SHARED)) |
132 | return -EINVAL; | |
67207b96 | 133 | |
8b3d6663 | 134 | /* FIXME: */ |
8b3d6663 AB |
135 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) |
136 | | _PAGE_NO_CACHE); | |
137 | ||
138 | vma->vm_ops = &spufs_mem_mmap_vmops; | |
67207b96 AB |
139 | return 0; |
140 | } | |
141 | ||
142 | static struct file_operations spufs_mem_fops = { | |
143 | .open = spufs_mem_open, | |
144 | .read = spufs_mem_read, | |
145 | .write = spufs_mem_write, | |
8b3d6663 | 146 | .llseek = generic_file_llseek, |
67207b96 | 147 | .mmap = spufs_mem_mmap, |
8b3d6663 AB |
148 | }; |
149 | ||
6df10a82 MN |
150 | static struct page *spufs_ps_nopage(struct vm_area_struct *vma, |
151 | unsigned long address, | |
27d5bf2a BH |
152 | int *type, unsigned long ps_offs, |
153 | unsigned long ps_size) | |
6df10a82 MN |
154 | { |
155 | struct page *page = NOPAGE_SIGBUS; | |
156 | int fault_type = VM_FAULT_SIGBUS; | |
157 | struct spu_context *ctx = vma->vm_file->private_data; | |
158 | unsigned long offset = address - vma->vm_start; | |
159 | unsigned long area; | |
160 | int ret; | |
161 | ||
162 | offset += vma->vm_pgoff << PAGE_SHIFT; | |
27d5bf2a | 163 | if (offset >= ps_size) |
6df10a82 MN |
164 | goto out; |
165 | ||
166 | ret = spu_acquire_runnable(ctx); | |
167 | if (ret) | |
168 | goto out; | |
169 | ||
170 | area = ctx->spu->problem_phys + ps_offs; | |
171 | page = pfn_to_page((area + offset) >> PAGE_SHIFT); | |
172 | fault_type = VM_FAULT_MINOR; | |
173 | page_cache_get(page); | |
174 | ||
175 | spu_release(ctx); | |
176 | ||
177 | out: | |
178 | if (type) | |
179 | *type = fault_type; | |
180 | ||
181 | return page; | |
182 | } | |
183 | ||
27d5bf2a | 184 | #if SPUFS_MMAP_4K |
6df10a82 MN |
185 | static struct page *spufs_cntl_mmap_nopage(struct vm_area_struct *vma, |
186 | unsigned long address, int *type) | |
187 | { | |
27d5bf2a | 188 | return spufs_ps_nopage(vma, address, type, 0x4000, 0x1000); |
6df10a82 MN |
189 | } |
190 | ||
191 | static struct vm_operations_struct spufs_cntl_mmap_vmops = { | |
192 | .nopage = spufs_cntl_mmap_nopage, | |
193 | }; | |
194 | ||
195 | /* | |
196 | * mmap support for problem state control area [0x4000 - 0x4fff]. | |
6df10a82 MN |
197 | */ |
198 | static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma) | |
199 | { | |
200 | if (!(vma->vm_flags & VM_SHARED)) | |
201 | return -EINVAL; | |
202 | ||
6df10a82 MN |
203 | vma->vm_flags |= VM_RESERVED; |
204 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) | |
23cc7701 | 205 | | _PAGE_NO_CACHE | _PAGE_GUARDED); |
6df10a82 MN |
206 | |
207 | vma->vm_ops = &spufs_cntl_mmap_vmops; | |
208 | return 0; | |
209 | } | |
27d5bf2a BH |
210 | #else /* SPUFS_MMAP_4K */ |
211 | #define spufs_cntl_mmap NULL | |
212 | #endif /* !SPUFS_MMAP_4K */ | |
6df10a82 | 213 | |
e1dbff2b | 214 | static u64 spufs_cntl_get(void *data) |
6df10a82 | 215 | { |
e1dbff2b AB |
216 | struct spu_context *ctx = data; |
217 | u64 val; | |
6df10a82 | 218 | |
e1dbff2b AB |
219 | spu_acquire(ctx); |
220 | val = ctx->ops->status_read(ctx); | |
221 | spu_release(ctx); | |
222 | ||
223 | return val; | |
6df10a82 MN |
224 | } |
225 | ||
e1dbff2b | 226 | static void spufs_cntl_set(void *data, u64 val) |
6df10a82 | 227 | { |
e1dbff2b AB |
228 | struct spu_context *ctx = data; |
229 | ||
230 | spu_acquire(ctx); | |
231 | ctx->ops->runcntl_write(ctx, val); | |
232 | spu_release(ctx); | |
6df10a82 MN |
233 | } |
234 | ||
e1dbff2b | 235 | static int spufs_cntl_open(struct inode *inode, struct file *file) |
6df10a82 | 236 | { |
e1dbff2b AB |
237 | struct spufs_inode_info *i = SPUFS_I(inode); |
238 | struct spu_context *ctx = i->i_ctx; | |
239 | ||
240 | file->private_data = ctx; | |
241 | file->f_mapping = inode->i_mapping; | |
242 | ctx->cntl = inode->i_mapping; | |
243 | return simple_attr_open(inode, file, spufs_cntl_get, | |
244 | spufs_cntl_set, "0x%08lx"); | |
6df10a82 MN |
245 | } |
246 | ||
247 | static struct file_operations spufs_cntl_fops = { | |
248 | .open = spufs_cntl_open, | |
654e4aee | 249 | .release = simple_attr_close, |
e1dbff2b AB |
250 | .read = simple_attr_read, |
251 | .write = simple_attr_write, | |
6df10a82 | 252 | .mmap = spufs_cntl_mmap, |
6df10a82 MN |
253 | }; |
254 | ||
8b3d6663 AB |
255 | static int |
256 | spufs_regs_open(struct inode *inode, struct file *file) | |
257 | { | |
258 | struct spufs_inode_info *i = SPUFS_I(inode); | |
259 | file->private_data = i->i_ctx; | |
260 | return 0; | |
261 | } | |
262 | ||
263 | static ssize_t | |
264 | spufs_regs_read(struct file *file, char __user *buffer, | |
265 | size_t size, loff_t *pos) | |
266 | { | |
267 | struct spu_context *ctx = file->private_data; | |
268 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
269 | int ret; | |
270 | ||
271 | spu_acquire_saved(ctx); | |
272 | ||
273 | ret = simple_read_from_buffer(buffer, size, pos, | |
274 | lscsa->gprs, sizeof lscsa->gprs); | |
275 | ||
276 | spu_release(ctx); | |
277 | return ret; | |
278 | } | |
279 | ||
280 | static ssize_t | |
281 | spufs_regs_write(struct file *file, const char __user *buffer, | |
282 | size_t size, loff_t *pos) | |
283 | { | |
284 | struct spu_context *ctx = file->private_data; | |
285 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
286 | int ret; | |
287 | ||
288 | size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size); | |
289 | if (size <= 0) | |
290 | return -EFBIG; | |
291 | *pos += size; | |
292 | ||
293 | spu_acquire_saved(ctx); | |
294 | ||
295 | ret = copy_from_user(lscsa->gprs + *pos - size, | |
296 | buffer, size) ? -EFAULT : size; | |
297 | ||
298 | spu_release(ctx); | |
299 | return ret; | |
300 | } | |
301 | ||
302 | static struct file_operations spufs_regs_fops = { | |
303 | .open = spufs_regs_open, | |
304 | .read = spufs_regs_read, | |
305 | .write = spufs_regs_write, | |
67207b96 AB |
306 | .llseek = generic_file_llseek, |
307 | }; | |
308 | ||
8b3d6663 AB |
309 | static ssize_t |
310 | spufs_fpcr_read(struct file *file, char __user * buffer, | |
311 | size_t size, loff_t * pos) | |
312 | { | |
313 | struct spu_context *ctx = file->private_data; | |
314 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
315 | int ret; | |
316 | ||
317 | spu_acquire_saved(ctx); | |
318 | ||
319 | ret = simple_read_from_buffer(buffer, size, pos, | |
320 | &lscsa->fpcr, sizeof(lscsa->fpcr)); | |
321 | ||
322 | spu_release(ctx); | |
323 | return ret; | |
324 | } | |
325 | ||
326 | static ssize_t | |
327 | spufs_fpcr_write(struct file *file, const char __user * buffer, | |
328 | size_t size, loff_t * pos) | |
329 | { | |
330 | struct spu_context *ctx = file->private_data; | |
331 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
332 | int ret; | |
333 | ||
334 | size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size); | |
335 | if (size <= 0) | |
336 | return -EFBIG; | |
337 | *pos += size; | |
338 | ||
339 | spu_acquire_saved(ctx); | |
340 | ||
341 | ret = copy_from_user((char *)&lscsa->fpcr + *pos - size, | |
342 | buffer, size) ? -EFAULT : size; | |
343 | ||
344 | spu_release(ctx); | |
345 | return ret; | |
346 | } | |
347 | ||
348 | static struct file_operations spufs_fpcr_fops = { | |
349 | .open = spufs_regs_open, | |
350 | .read = spufs_fpcr_read, | |
351 | .write = spufs_fpcr_write, | |
352 | .llseek = generic_file_llseek, | |
353 | }; | |
354 | ||
67207b96 AB |
355 | /* generic open function for all pipe-like files */ |
356 | static int spufs_pipe_open(struct inode *inode, struct file *file) | |
357 | { | |
358 | struct spufs_inode_info *i = SPUFS_I(inode); | |
359 | file->private_data = i->i_ctx; | |
360 | ||
361 | return nonseekable_open(inode, file); | |
362 | } | |
363 | ||
cdcc89bb AB |
364 | /* |
365 | * Read as many bytes from the mailbox as possible, until | |
366 | * one of the conditions becomes true: | |
367 | * | |
368 | * - no more data available in the mailbox | |
369 | * - end of the user provided buffer | |
370 | * - end of the mapped area | |
371 | */ | |
67207b96 AB |
372 | static ssize_t spufs_mbox_read(struct file *file, char __user *buf, |
373 | size_t len, loff_t *pos) | |
374 | { | |
8b3d6663 | 375 | struct spu_context *ctx = file->private_data; |
cdcc89bb AB |
376 | u32 mbox_data, __user *udata; |
377 | ssize_t count; | |
67207b96 AB |
378 | |
379 | if (len < 4) | |
380 | return -EINVAL; | |
381 | ||
cdcc89bb AB |
382 | if (!access_ok(VERIFY_WRITE, buf, len)) |
383 | return -EFAULT; | |
384 | ||
385 | udata = (void __user *)buf; | |
386 | ||
8b3d6663 | 387 | spu_acquire(ctx); |
274cef5e | 388 | for (count = 0; (count + 4) <= len; count += 4, udata++) { |
cdcc89bb AB |
389 | int ret; |
390 | ret = ctx->ops->mbox_read(ctx, &mbox_data); | |
391 | if (ret == 0) | |
392 | break; | |
393 | ||
394 | /* | |
395 | * at the end of the mapped area, we can fault | |
396 | * but still need to return the data we have | |
397 | * read successfully so far. | |
398 | */ | |
399 | ret = __put_user(mbox_data, udata); | |
400 | if (ret) { | |
401 | if (!count) | |
402 | count = -EFAULT; | |
403 | break; | |
404 | } | |
405 | } | |
8b3d6663 | 406 | spu_release(ctx); |
67207b96 | 407 | |
cdcc89bb AB |
408 | if (!count) |
409 | count = -EAGAIN; | |
67207b96 | 410 | |
cdcc89bb | 411 | return count; |
67207b96 AB |
412 | } |
413 | ||
414 | static struct file_operations spufs_mbox_fops = { | |
415 | .open = spufs_pipe_open, | |
416 | .read = spufs_mbox_read, | |
417 | }; | |
418 | ||
419 | static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf, | |
420 | size_t len, loff_t *pos) | |
421 | { | |
8b3d6663 | 422 | struct spu_context *ctx = file->private_data; |
67207b96 AB |
423 | u32 mbox_stat; |
424 | ||
425 | if (len < 4) | |
426 | return -EINVAL; | |
427 | ||
8b3d6663 AB |
428 | spu_acquire(ctx); |
429 | ||
430 | mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff; | |
431 | ||
432 | spu_release(ctx); | |
67207b96 AB |
433 | |
434 | if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat)) | |
435 | return -EFAULT; | |
436 | ||
437 | return 4; | |
438 | } | |
439 | ||
440 | static struct file_operations spufs_mbox_stat_fops = { | |
441 | .open = spufs_pipe_open, | |
442 | .read = spufs_mbox_stat_read, | |
443 | }; | |
444 | ||
445 | /* low-level ibox access function */ | |
8b3d6663 | 446 | size_t spu_ibox_read(struct spu_context *ctx, u32 *data) |
67207b96 | 447 | { |
8b3d6663 AB |
448 | return ctx->ops->ibox_read(ctx, data); |
449 | } | |
67207b96 | 450 | |
8b3d6663 AB |
451 | static int spufs_ibox_fasync(int fd, struct file *file, int on) |
452 | { | |
453 | struct spu_context *ctx = file->private_data; | |
67207b96 | 454 | |
8b3d6663 | 455 | return fasync_helper(fd, file, on, &ctx->ibox_fasync); |
67207b96 | 456 | } |
67207b96 | 457 | |
8b3d6663 AB |
458 | /* interrupt-level ibox callback function. */ |
459 | void spufs_ibox_callback(struct spu *spu) | |
67207b96 | 460 | { |
8b3d6663 AB |
461 | struct spu_context *ctx = spu->ctx; |
462 | ||
463 | wake_up_all(&ctx->ibox_wq); | |
464 | kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN); | |
67207b96 AB |
465 | } |
466 | ||
cdcc89bb AB |
467 | /* |
468 | * Read as many bytes from the interrupt mailbox as possible, until | |
469 | * one of the conditions becomes true: | |
470 | * | |
471 | * - no more data available in the mailbox | |
472 | * - end of the user provided buffer | |
473 | * - end of the mapped area | |
474 | * | |
475 | * If the file is opened without O_NONBLOCK, we wait here until | |
476 | * any data is available, but return when we have been able to | |
477 | * read something. | |
478 | */ | |
67207b96 AB |
479 | static ssize_t spufs_ibox_read(struct file *file, char __user *buf, |
480 | size_t len, loff_t *pos) | |
481 | { | |
8b3d6663 | 482 | struct spu_context *ctx = file->private_data; |
cdcc89bb AB |
483 | u32 ibox_data, __user *udata; |
484 | ssize_t count; | |
67207b96 AB |
485 | |
486 | if (len < 4) | |
487 | return -EINVAL; | |
488 | ||
cdcc89bb AB |
489 | if (!access_ok(VERIFY_WRITE, buf, len)) |
490 | return -EFAULT; | |
491 | ||
492 | udata = (void __user *)buf; | |
493 | ||
8b3d6663 | 494 | spu_acquire(ctx); |
67207b96 | 495 | |
cdcc89bb AB |
496 | /* wait only for the first element */ |
497 | count = 0; | |
67207b96 | 498 | if (file->f_flags & O_NONBLOCK) { |
8b3d6663 | 499 | if (!spu_ibox_read(ctx, &ibox_data)) |
cdcc89bb | 500 | count = -EAGAIN; |
67207b96 | 501 | } else { |
cdcc89bb | 502 | count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data)); |
67207b96 | 503 | } |
cdcc89bb AB |
504 | if (count) |
505 | goto out; | |
67207b96 | 506 | |
cdcc89bb AB |
507 | /* if we can't write at all, return -EFAULT */ |
508 | count = __put_user(ibox_data, udata); | |
509 | if (count) | |
510 | goto out; | |
8b3d6663 | 511 | |
cdcc89bb AB |
512 | for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) { |
513 | int ret; | |
514 | ret = ctx->ops->ibox_read(ctx, &ibox_data); | |
515 | if (ret == 0) | |
516 | break; | |
517 | /* | |
518 | * at the end of the mapped area, we can fault | |
519 | * but still need to return the data we have | |
520 | * read successfully so far. | |
521 | */ | |
522 | ret = __put_user(ibox_data, udata); | |
523 | if (ret) | |
524 | break; | |
525 | } | |
67207b96 | 526 | |
cdcc89bb AB |
527 | out: |
528 | spu_release(ctx); | |
67207b96 | 529 | |
cdcc89bb | 530 | return count; |
67207b96 AB |
531 | } |
532 | ||
533 | static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait) | |
534 | { | |
8b3d6663 | 535 | struct spu_context *ctx = file->private_data; |
67207b96 AB |
536 | unsigned int mask; |
537 | ||
8b3d6663 | 538 | poll_wait(file, &ctx->ibox_wq, wait); |
67207b96 | 539 | |
3a843d7c AB |
540 | spu_acquire(ctx); |
541 | mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM); | |
542 | spu_release(ctx); | |
67207b96 AB |
543 | |
544 | return mask; | |
545 | } | |
546 | ||
547 | static struct file_operations spufs_ibox_fops = { | |
548 | .open = spufs_pipe_open, | |
549 | .read = spufs_ibox_read, | |
550 | .poll = spufs_ibox_poll, | |
551 | .fasync = spufs_ibox_fasync, | |
552 | }; | |
553 | ||
554 | static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf, | |
555 | size_t len, loff_t *pos) | |
556 | { | |
8b3d6663 | 557 | struct spu_context *ctx = file->private_data; |
67207b96 AB |
558 | u32 ibox_stat; |
559 | ||
560 | if (len < 4) | |
561 | return -EINVAL; | |
562 | ||
8b3d6663 AB |
563 | spu_acquire(ctx); |
564 | ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff; | |
565 | spu_release(ctx); | |
67207b96 AB |
566 | |
567 | if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat)) | |
568 | return -EFAULT; | |
569 | ||
570 | return 4; | |
571 | } | |
572 | ||
573 | static struct file_operations spufs_ibox_stat_fops = { | |
574 | .open = spufs_pipe_open, | |
575 | .read = spufs_ibox_stat_read, | |
576 | }; | |
577 | ||
578 | /* low-level mailbox write */ | |
8b3d6663 | 579 | size_t spu_wbox_write(struct spu_context *ctx, u32 data) |
67207b96 | 580 | { |
8b3d6663 AB |
581 | return ctx->ops->wbox_write(ctx, data); |
582 | } | |
67207b96 | 583 | |
8b3d6663 AB |
584 | static int spufs_wbox_fasync(int fd, struct file *file, int on) |
585 | { | |
586 | struct spu_context *ctx = file->private_data; | |
587 | int ret; | |
67207b96 | 588 | |
8b3d6663 | 589 | ret = fasync_helper(fd, file, on, &ctx->wbox_fasync); |
67207b96 | 590 | |
67207b96 AB |
591 | return ret; |
592 | } | |
67207b96 | 593 | |
8b3d6663 AB |
594 | /* interrupt-level wbox callback function. */ |
595 | void spufs_wbox_callback(struct spu *spu) | |
67207b96 | 596 | { |
8b3d6663 AB |
597 | struct spu_context *ctx = spu->ctx; |
598 | ||
599 | wake_up_all(&ctx->wbox_wq); | |
600 | kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT); | |
67207b96 AB |
601 | } |
602 | ||
cdcc89bb AB |
603 | /* |
604 | * Write as many bytes to the interrupt mailbox as possible, until | |
605 | * one of the conditions becomes true: | |
606 | * | |
607 | * - the mailbox is full | |
608 | * - end of the user provided buffer | |
609 | * - end of the mapped area | |
610 | * | |
611 | * If the file is opened without O_NONBLOCK, we wait here until | |
612 | * space is availabyl, but return when we have been able to | |
613 | * write something. | |
614 | */ | |
67207b96 AB |
615 | static ssize_t spufs_wbox_write(struct file *file, const char __user *buf, |
616 | size_t len, loff_t *pos) | |
617 | { | |
8b3d6663 | 618 | struct spu_context *ctx = file->private_data; |
cdcc89bb AB |
619 | u32 wbox_data, __user *udata; |
620 | ssize_t count; | |
67207b96 AB |
621 | |
622 | if (len < 4) | |
623 | return -EINVAL; | |
624 | ||
cdcc89bb AB |
625 | udata = (void __user *)buf; |
626 | if (!access_ok(VERIFY_READ, buf, len)) | |
627 | return -EFAULT; | |
628 | ||
629 | if (__get_user(wbox_data, udata)) | |
67207b96 AB |
630 | return -EFAULT; |
631 | ||
8b3d6663 AB |
632 | spu_acquire(ctx); |
633 | ||
cdcc89bb AB |
634 | /* |
635 | * make sure we can at least write one element, by waiting | |
636 | * in case of !O_NONBLOCK | |
637 | */ | |
638 | count = 0; | |
67207b96 | 639 | if (file->f_flags & O_NONBLOCK) { |
8b3d6663 | 640 | if (!spu_wbox_write(ctx, wbox_data)) |
cdcc89bb | 641 | count = -EAGAIN; |
67207b96 | 642 | } else { |
cdcc89bb | 643 | count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data)); |
67207b96 AB |
644 | } |
645 | ||
cdcc89bb AB |
646 | if (count) |
647 | goto out; | |
8b3d6663 | 648 | |
cdcc89bb AB |
649 | /* write aѕ much as possible */ |
650 | for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) { | |
651 | int ret; | |
652 | ret = __get_user(wbox_data, udata); | |
653 | if (ret) | |
654 | break; | |
655 | ||
656 | ret = spu_wbox_write(ctx, wbox_data); | |
657 | if (ret == 0) | |
658 | break; | |
659 | } | |
660 | ||
661 | out: | |
662 | spu_release(ctx); | |
663 | return count; | |
67207b96 AB |
664 | } |
665 | ||
666 | static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait) | |
667 | { | |
8b3d6663 | 668 | struct spu_context *ctx = file->private_data; |
67207b96 AB |
669 | unsigned int mask; |
670 | ||
8b3d6663 | 671 | poll_wait(file, &ctx->wbox_wq, wait); |
67207b96 | 672 | |
3a843d7c AB |
673 | spu_acquire(ctx); |
674 | mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM); | |
675 | spu_release(ctx); | |
67207b96 AB |
676 | |
677 | return mask; | |
678 | } | |
679 | ||
680 | static struct file_operations spufs_wbox_fops = { | |
681 | .open = spufs_pipe_open, | |
682 | .write = spufs_wbox_write, | |
683 | .poll = spufs_wbox_poll, | |
684 | .fasync = spufs_wbox_fasync, | |
685 | }; | |
686 | ||
687 | static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf, | |
688 | size_t len, loff_t *pos) | |
689 | { | |
8b3d6663 | 690 | struct spu_context *ctx = file->private_data; |
67207b96 AB |
691 | u32 wbox_stat; |
692 | ||
693 | if (len < 4) | |
694 | return -EINVAL; | |
695 | ||
8b3d6663 AB |
696 | spu_acquire(ctx); |
697 | wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff; | |
698 | spu_release(ctx); | |
67207b96 AB |
699 | |
700 | if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat)) | |
701 | return -EFAULT; | |
702 | ||
703 | return 4; | |
704 | } | |
705 | ||
706 | static struct file_operations spufs_wbox_stat_fops = { | |
707 | .open = spufs_pipe_open, | |
708 | .read = spufs_wbox_stat_read, | |
709 | }; | |
710 | ||
6df10a82 MN |
711 | static int spufs_signal1_open(struct inode *inode, struct file *file) |
712 | { | |
713 | struct spufs_inode_info *i = SPUFS_I(inode); | |
714 | struct spu_context *ctx = i->i_ctx; | |
715 | file->private_data = ctx; | |
716 | file->f_mapping = inode->i_mapping; | |
717 | ctx->signal1 = inode->i_mapping; | |
718 | return nonseekable_open(inode, file); | |
719 | } | |
720 | ||
67207b96 AB |
721 | static ssize_t spufs_signal1_read(struct file *file, char __user *buf, |
722 | size_t len, loff_t *pos) | |
723 | { | |
8b3d6663 | 724 | struct spu_context *ctx = file->private_data; |
67207b96 AB |
725 | u32 data; |
726 | ||
67207b96 AB |
727 | if (len < 4) |
728 | return -EINVAL; | |
729 | ||
8b3d6663 AB |
730 | spu_acquire(ctx); |
731 | data = ctx->ops->signal1_read(ctx); | |
732 | spu_release(ctx); | |
733 | ||
67207b96 AB |
734 | if (copy_to_user(buf, &data, 4)) |
735 | return -EFAULT; | |
736 | ||
737 | return 4; | |
738 | } | |
739 | ||
740 | static ssize_t spufs_signal1_write(struct file *file, const char __user *buf, | |
741 | size_t len, loff_t *pos) | |
742 | { | |
743 | struct spu_context *ctx; | |
67207b96 AB |
744 | u32 data; |
745 | ||
746 | ctx = file->private_data; | |
67207b96 AB |
747 | |
748 | if (len < 4) | |
749 | return -EINVAL; | |
750 | ||
751 | if (copy_from_user(&data, buf, 4)) | |
752 | return -EFAULT; | |
753 | ||
8b3d6663 AB |
754 | spu_acquire(ctx); |
755 | ctx->ops->signal1_write(ctx, data); | |
756 | spu_release(ctx); | |
67207b96 AB |
757 | |
758 | return 4; | |
759 | } | |
760 | ||
6df10a82 MN |
761 | static struct page *spufs_signal1_mmap_nopage(struct vm_area_struct *vma, |
762 | unsigned long address, int *type) | |
763 | { | |
27d5bf2a BH |
764 | #if PAGE_SIZE == 0x1000 |
765 | return spufs_ps_nopage(vma, address, type, 0x14000, 0x1000); | |
766 | #elif PAGE_SIZE == 0x10000 | |
767 | /* For 64k pages, both signal1 and signal2 can be used to mmap the whole | |
768 | * signal 1 and 2 area | |
769 | */ | |
770 | return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000); | |
771 | #else | |
772 | #error unsupported page size | |
773 | #endif | |
6df10a82 MN |
774 | } |
775 | ||
776 | static struct vm_operations_struct spufs_signal1_mmap_vmops = { | |
777 | .nopage = spufs_signal1_mmap_nopage, | |
778 | }; | |
779 | ||
780 | static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma) | |
781 | { | |
782 | if (!(vma->vm_flags & VM_SHARED)) | |
783 | return -EINVAL; | |
784 | ||
785 | vma->vm_flags |= VM_RESERVED; | |
786 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) | |
23cc7701 | 787 | | _PAGE_NO_CACHE | _PAGE_GUARDED); |
6df10a82 MN |
788 | |
789 | vma->vm_ops = &spufs_signal1_mmap_vmops; | |
790 | return 0; | |
791 | } | |
6df10a82 | 792 | |
67207b96 | 793 | static struct file_operations spufs_signal1_fops = { |
6df10a82 | 794 | .open = spufs_signal1_open, |
67207b96 AB |
795 | .read = spufs_signal1_read, |
796 | .write = spufs_signal1_write, | |
6df10a82 | 797 | .mmap = spufs_signal1_mmap, |
67207b96 AB |
798 | }; |
799 | ||
6df10a82 MN |
800 | static int spufs_signal2_open(struct inode *inode, struct file *file) |
801 | { | |
802 | struct spufs_inode_info *i = SPUFS_I(inode); | |
803 | struct spu_context *ctx = i->i_ctx; | |
804 | file->private_data = ctx; | |
805 | file->f_mapping = inode->i_mapping; | |
806 | ctx->signal2 = inode->i_mapping; | |
807 | return nonseekable_open(inode, file); | |
808 | } | |
809 | ||
67207b96 AB |
810 | static ssize_t spufs_signal2_read(struct file *file, char __user *buf, |
811 | size_t len, loff_t *pos) | |
812 | { | |
813 | struct spu_context *ctx; | |
67207b96 AB |
814 | u32 data; |
815 | ||
816 | ctx = file->private_data; | |
67207b96 AB |
817 | |
818 | if (len < 4) | |
819 | return -EINVAL; | |
820 | ||
8b3d6663 AB |
821 | spu_acquire(ctx); |
822 | data = ctx->ops->signal2_read(ctx); | |
823 | spu_release(ctx); | |
824 | ||
67207b96 AB |
825 | if (copy_to_user(buf, &data, 4)) |
826 | return -EFAULT; | |
827 | ||
828 | return 4; | |
829 | } | |
830 | ||
831 | static ssize_t spufs_signal2_write(struct file *file, const char __user *buf, | |
832 | size_t len, loff_t *pos) | |
833 | { | |
834 | struct spu_context *ctx; | |
67207b96 AB |
835 | u32 data; |
836 | ||
837 | ctx = file->private_data; | |
67207b96 AB |
838 | |
839 | if (len < 4) | |
840 | return -EINVAL; | |
841 | ||
842 | if (copy_from_user(&data, buf, 4)) | |
843 | return -EFAULT; | |
844 | ||
8b3d6663 AB |
845 | spu_acquire(ctx); |
846 | ctx->ops->signal2_write(ctx, data); | |
847 | spu_release(ctx); | |
67207b96 AB |
848 | |
849 | return 4; | |
850 | } | |
851 | ||
27d5bf2a | 852 | #if SPUFS_MMAP_4K |
6df10a82 MN |
853 | static struct page *spufs_signal2_mmap_nopage(struct vm_area_struct *vma, |
854 | unsigned long address, int *type) | |
855 | { | |
27d5bf2a BH |
856 | #if PAGE_SIZE == 0x1000 |
857 | return spufs_ps_nopage(vma, address, type, 0x1c000, 0x1000); | |
858 | #elif PAGE_SIZE == 0x10000 | |
859 | /* For 64k pages, both signal1 and signal2 can be used to mmap the whole | |
860 | * signal 1 and 2 area | |
861 | */ | |
862 | return spufs_ps_nopage(vma, address, type, 0x10000, 0x10000); | |
863 | #else | |
864 | #error unsupported page size | |
865 | #endif | |
6df10a82 MN |
866 | } |
867 | ||
868 | static struct vm_operations_struct spufs_signal2_mmap_vmops = { | |
869 | .nopage = spufs_signal2_mmap_nopage, | |
870 | }; | |
871 | ||
872 | static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma) | |
873 | { | |
874 | if (!(vma->vm_flags & VM_SHARED)) | |
875 | return -EINVAL; | |
876 | ||
877 | /* FIXME: */ | |
878 | vma->vm_flags |= VM_RESERVED; | |
879 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) | |
23cc7701 | 880 | | _PAGE_NO_CACHE | _PAGE_GUARDED); |
6df10a82 MN |
881 | |
882 | vma->vm_ops = &spufs_signal2_mmap_vmops; | |
883 | return 0; | |
884 | } | |
27d5bf2a BH |
885 | #else /* SPUFS_MMAP_4K */ |
886 | #define spufs_signal2_mmap NULL | |
887 | #endif /* !SPUFS_MMAP_4K */ | |
6df10a82 | 888 | |
67207b96 | 889 | static struct file_operations spufs_signal2_fops = { |
6df10a82 | 890 | .open = spufs_signal2_open, |
67207b96 AB |
891 | .read = spufs_signal2_read, |
892 | .write = spufs_signal2_write, | |
6df10a82 | 893 | .mmap = spufs_signal2_mmap, |
67207b96 AB |
894 | }; |
895 | ||
896 | static void spufs_signal1_type_set(void *data, u64 val) | |
897 | { | |
898 | struct spu_context *ctx = data; | |
67207b96 | 899 | |
8b3d6663 AB |
900 | spu_acquire(ctx); |
901 | ctx->ops->signal1_type_set(ctx, val); | |
902 | spu_release(ctx); | |
67207b96 AB |
903 | } |
904 | ||
905 | static u64 spufs_signal1_type_get(void *data) | |
906 | { | |
907 | struct spu_context *ctx = data; | |
8b3d6663 AB |
908 | u64 ret; |
909 | ||
910 | spu_acquire(ctx); | |
911 | ret = ctx->ops->signal1_type_get(ctx); | |
912 | spu_release(ctx); | |
913 | ||
914 | return ret; | |
67207b96 AB |
915 | } |
916 | DEFINE_SIMPLE_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get, | |
917 | spufs_signal1_type_set, "%llu"); | |
918 | ||
919 | static void spufs_signal2_type_set(void *data, u64 val) | |
920 | { | |
921 | struct spu_context *ctx = data; | |
67207b96 | 922 | |
8b3d6663 AB |
923 | spu_acquire(ctx); |
924 | ctx->ops->signal2_type_set(ctx, val); | |
925 | spu_release(ctx); | |
67207b96 AB |
926 | } |
927 | ||
928 | static u64 spufs_signal2_type_get(void *data) | |
929 | { | |
930 | struct spu_context *ctx = data; | |
8b3d6663 AB |
931 | u64 ret; |
932 | ||
933 | spu_acquire(ctx); | |
934 | ret = ctx->ops->signal2_type_get(ctx); | |
935 | spu_release(ctx); | |
936 | ||
937 | return ret; | |
67207b96 AB |
938 | } |
939 | DEFINE_SIMPLE_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get, | |
940 | spufs_signal2_type_set, "%llu"); | |
941 | ||
27d5bf2a | 942 | #if SPUFS_MMAP_4K |
d9379c4b | 943 | static struct page *spufs_mss_mmap_nopage(struct vm_area_struct *vma, |
944 | unsigned long address, int *type) | |
945 | { | |
27d5bf2a | 946 | return spufs_ps_nopage(vma, address, type, 0x0000, 0x1000); |
d9379c4b | 947 | } |
948 | ||
949 | static struct vm_operations_struct spufs_mss_mmap_vmops = { | |
950 | .nopage = spufs_mss_mmap_nopage, | |
951 | }; | |
952 | ||
953 | /* | |
954 | * mmap support for problem state MFC DMA area [0x0000 - 0x0fff]. | |
d9379c4b | 955 | */ |
956 | static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma) | |
957 | { | |
958 | if (!(vma->vm_flags & VM_SHARED)) | |
959 | return -EINVAL; | |
960 | ||
d9379c4b | 961 | vma->vm_flags |= VM_RESERVED; |
962 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) | |
23cc7701 | 963 | | _PAGE_NO_CACHE | _PAGE_GUARDED); |
d9379c4b | 964 | |
965 | vma->vm_ops = &spufs_mss_mmap_vmops; | |
966 | return 0; | |
967 | } | |
27d5bf2a BH |
968 | #else /* SPUFS_MMAP_4K */ |
969 | #define spufs_mss_mmap NULL | |
970 | #endif /* !SPUFS_MMAP_4K */ | |
d9379c4b | 971 | |
972 | static int spufs_mss_open(struct inode *inode, struct file *file) | |
973 | { | |
974 | struct spufs_inode_info *i = SPUFS_I(inode); | |
975 | ||
976 | file->private_data = i->i_ctx; | |
977 | return nonseekable_open(inode, file); | |
978 | } | |
979 | ||
980 | static struct file_operations spufs_mss_fops = { | |
981 | .open = spufs_mss_open, | |
d9379c4b | 982 | .mmap = spufs_mss_mmap, |
27d5bf2a BH |
983 | }; |
984 | ||
985 | static struct page *spufs_psmap_mmap_nopage(struct vm_area_struct *vma, | |
986 | unsigned long address, int *type) | |
987 | { | |
988 | return spufs_ps_nopage(vma, address, type, 0x0000, 0x20000); | |
989 | } | |
990 | ||
991 | static struct vm_operations_struct spufs_psmap_mmap_vmops = { | |
992 | .nopage = spufs_psmap_mmap_nopage, | |
993 | }; | |
994 | ||
995 | /* | |
996 | * mmap support for full problem state area [0x00000 - 0x1ffff]. | |
997 | */ | |
998 | static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma) | |
999 | { | |
1000 | if (!(vma->vm_flags & VM_SHARED)) | |
1001 | return -EINVAL; | |
1002 | ||
1003 | vma->vm_flags |= VM_RESERVED; | |
1004 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) | |
1005 | | _PAGE_NO_CACHE | _PAGE_GUARDED); | |
1006 | ||
1007 | vma->vm_ops = &spufs_psmap_mmap_vmops; | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | static int spufs_psmap_open(struct inode *inode, struct file *file) | |
1012 | { | |
1013 | struct spufs_inode_info *i = SPUFS_I(inode); | |
1014 | ||
1015 | file->private_data = i->i_ctx; | |
1016 | return nonseekable_open(inode, file); | |
1017 | } | |
1018 | ||
1019 | static struct file_operations spufs_psmap_fops = { | |
1020 | .open = spufs_psmap_open, | |
1021 | .mmap = spufs_psmap_mmap, | |
d9379c4b | 1022 | }; |
1023 | ||
1024 | ||
27d5bf2a | 1025 | #if SPUFS_MMAP_4K |
6df10a82 MN |
1026 | static struct page *spufs_mfc_mmap_nopage(struct vm_area_struct *vma, |
1027 | unsigned long address, int *type) | |
1028 | { | |
27d5bf2a | 1029 | return spufs_ps_nopage(vma, address, type, 0x3000, 0x1000); |
6df10a82 MN |
1030 | } |
1031 | ||
1032 | static struct vm_operations_struct spufs_mfc_mmap_vmops = { | |
1033 | .nopage = spufs_mfc_mmap_nopage, | |
1034 | }; | |
1035 | ||
1036 | /* | |
1037 | * mmap support for problem state MFC DMA area [0x0000 - 0x0fff]. | |
6df10a82 MN |
1038 | */ |
1039 | static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma) | |
1040 | { | |
1041 | if (!(vma->vm_flags & VM_SHARED)) | |
1042 | return -EINVAL; | |
1043 | ||
6df10a82 MN |
1044 | vma->vm_flags |= VM_RESERVED; |
1045 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) | |
23cc7701 | 1046 | | _PAGE_NO_CACHE | _PAGE_GUARDED); |
6df10a82 MN |
1047 | |
1048 | vma->vm_ops = &spufs_mfc_mmap_vmops; | |
1049 | return 0; | |
1050 | } | |
27d5bf2a BH |
1051 | #else /* SPUFS_MMAP_4K */ |
1052 | #define spufs_mfc_mmap NULL | |
1053 | #endif /* !SPUFS_MMAP_4K */ | |
a33a7d73 AB |
1054 | |
1055 | static int spufs_mfc_open(struct inode *inode, struct file *file) | |
1056 | { | |
1057 | struct spufs_inode_info *i = SPUFS_I(inode); | |
1058 | struct spu_context *ctx = i->i_ctx; | |
1059 | ||
1060 | /* we don't want to deal with DMA into other processes */ | |
1061 | if (ctx->owner != current->mm) | |
1062 | return -EINVAL; | |
1063 | ||
1064 | if (atomic_read(&inode->i_count) != 1) | |
1065 | return -EBUSY; | |
1066 | ||
1067 | file->private_data = ctx; | |
1068 | return nonseekable_open(inode, file); | |
1069 | } | |
1070 | ||
1071 | /* interrupt-level mfc callback function. */ | |
1072 | void spufs_mfc_callback(struct spu *spu) | |
1073 | { | |
1074 | struct spu_context *ctx = spu->ctx; | |
1075 | ||
1076 | wake_up_all(&ctx->mfc_wq); | |
1077 | ||
1078 | pr_debug("%s %s\n", __FUNCTION__, spu->name); | |
1079 | if (ctx->mfc_fasync) { | |
1080 | u32 free_elements, tagstatus; | |
1081 | unsigned int mask; | |
1082 | ||
1083 | /* no need for spu_acquire in interrupt context */ | |
1084 | free_elements = ctx->ops->get_mfc_free_elements(ctx); | |
1085 | tagstatus = ctx->ops->read_mfc_tagstatus(ctx); | |
1086 | ||
1087 | mask = 0; | |
1088 | if (free_elements & 0xffff) | |
1089 | mask |= POLLOUT; | |
1090 | if (tagstatus & ctx->tagwait) | |
1091 | mask |= POLLIN; | |
1092 | ||
1093 | kill_fasync(&ctx->mfc_fasync, SIGIO, mask); | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status) | |
1098 | { | |
1099 | /* See if there is one tag group is complete */ | |
1100 | /* FIXME we need locking around tagwait */ | |
1101 | *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait; | |
1102 | ctx->tagwait &= ~*status; | |
1103 | if (*status) | |
1104 | return 1; | |
1105 | ||
1106 | /* enable interrupt waiting for any tag group, | |
1107 | may silently fail if interrupts are already enabled */ | |
1108 | ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1); | |
1109 | return 0; | |
1110 | } | |
1111 | ||
1112 | static ssize_t spufs_mfc_read(struct file *file, char __user *buffer, | |
1113 | size_t size, loff_t *pos) | |
1114 | { | |
1115 | struct spu_context *ctx = file->private_data; | |
1116 | int ret = -EINVAL; | |
1117 | u32 status; | |
1118 | ||
1119 | if (size != 4) | |
1120 | goto out; | |
1121 | ||
1122 | spu_acquire(ctx); | |
1123 | if (file->f_flags & O_NONBLOCK) { | |
1124 | status = ctx->ops->read_mfc_tagstatus(ctx); | |
1125 | if (!(status & ctx->tagwait)) | |
1126 | ret = -EAGAIN; | |
1127 | else | |
1128 | ctx->tagwait &= ~status; | |
1129 | } else { | |
1130 | ret = spufs_wait(ctx->mfc_wq, | |
1131 | spufs_read_mfc_tagstatus(ctx, &status)); | |
1132 | } | |
1133 | spu_release(ctx); | |
1134 | ||
1135 | if (ret) | |
1136 | goto out; | |
1137 | ||
1138 | ret = 4; | |
1139 | if (copy_to_user(buffer, &status, 4)) | |
1140 | ret = -EFAULT; | |
1141 | ||
1142 | out: | |
1143 | return ret; | |
1144 | } | |
1145 | ||
1146 | static int spufs_check_valid_dma(struct mfc_dma_command *cmd) | |
1147 | { | |
1148 | pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa, | |
1149 | cmd->ea, cmd->size, cmd->tag, cmd->cmd); | |
1150 | ||
1151 | switch (cmd->cmd) { | |
1152 | case MFC_PUT_CMD: | |
1153 | case MFC_PUTF_CMD: | |
1154 | case MFC_PUTB_CMD: | |
1155 | case MFC_GET_CMD: | |
1156 | case MFC_GETF_CMD: | |
1157 | case MFC_GETB_CMD: | |
1158 | break; | |
1159 | default: | |
1160 | pr_debug("invalid DMA opcode %x\n", cmd->cmd); | |
1161 | return -EIO; | |
1162 | } | |
1163 | ||
1164 | if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) { | |
1165 | pr_debug("invalid DMA alignment, ea %lx lsa %x\n", | |
1166 | cmd->ea, cmd->lsa); | |
1167 | return -EIO; | |
1168 | } | |
1169 | ||
1170 | switch (cmd->size & 0xf) { | |
1171 | case 1: | |
1172 | break; | |
1173 | case 2: | |
1174 | if (cmd->lsa & 1) | |
1175 | goto error; | |
1176 | break; | |
1177 | case 4: | |
1178 | if (cmd->lsa & 3) | |
1179 | goto error; | |
1180 | break; | |
1181 | case 8: | |
1182 | if (cmd->lsa & 7) | |
1183 | goto error; | |
1184 | break; | |
1185 | case 0: | |
1186 | if (cmd->lsa & 15) | |
1187 | goto error; | |
1188 | break; | |
1189 | error: | |
1190 | default: | |
1191 | pr_debug("invalid DMA alignment %x for size %x\n", | |
1192 | cmd->lsa & 0xf, cmd->size); | |
1193 | return -EIO; | |
1194 | } | |
1195 | ||
1196 | if (cmd->size > 16 * 1024) { | |
1197 | pr_debug("invalid DMA size %x\n", cmd->size); | |
1198 | return -EIO; | |
1199 | } | |
1200 | ||
1201 | if (cmd->tag & 0xfff0) { | |
1202 | /* we reserve the higher tag numbers for kernel use */ | |
1203 | pr_debug("invalid DMA tag\n"); | |
1204 | return -EIO; | |
1205 | } | |
1206 | ||
1207 | if (cmd->class) { | |
1208 | /* not supported in this version */ | |
1209 | pr_debug("invalid DMA class\n"); | |
1210 | return -EIO; | |
1211 | } | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | static int spu_send_mfc_command(struct spu_context *ctx, | |
1217 | struct mfc_dma_command cmd, | |
1218 | int *error) | |
1219 | { | |
1220 | *error = ctx->ops->send_mfc_command(ctx, &cmd); | |
1221 | if (*error == -EAGAIN) { | |
1222 | /* wait for any tag group to complete | |
1223 | so we have space for the new command */ | |
1224 | ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1); | |
1225 | /* try again, because the queue might be | |
1226 | empty again */ | |
1227 | *error = ctx->ops->send_mfc_command(ctx, &cmd); | |
1228 | if (*error == -EAGAIN) | |
1229 | return 0; | |
1230 | } | |
1231 | return 1; | |
1232 | } | |
1233 | ||
1234 | static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer, | |
1235 | size_t size, loff_t *pos) | |
1236 | { | |
1237 | struct spu_context *ctx = file->private_data; | |
1238 | struct mfc_dma_command cmd; | |
1239 | int ret = -EINVAL; | |
1240 | ||
1241 | if (size != sizeof cmd) | |
1242 | goto out; | |
1243 | ||
1244 | ret = -EFAULT; | |
1245 | if (copy_from_user(&cmd, buffer, sizeof cmd)) | |
1246 | goto out; | |
1247 | ||
1248 | ret = spufs_check_valid_dma(&cmd); | |
1249 | if (ret) | |
1250 | goto out; | |
1251 | ||
1252 | spu_acquire_runnable(ctx); | |
1253 | if (file->f_flags & O_NONBLOCK) { | |
1254 | ret = ctx->ops->send_mfc_command(ctx, &cmd); | |
1255 | } else { | |
1256 | int status; | |
1257 | ret = spufs_wait(ctx->mfc_wq, | |
1258 | spu_send_mfc_command(ctx, cmd, &status)); | |
1259 | if (status) | |
1260 | ret = status; | |
1261 | } | |
1262 | spu_release(ctx); | |
1263 | ||
1264 | if (ret) | |
1265 | goto out; | |
1266 | ||
1267 | ctx->tagwait |= 1 << cmd.tag; | |
1268 | ||
1269 | out: | |
1270 | return ret; | |
1271 | } | |
1272 | ||
1273 | static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait) | |
1274 | { | |
1275 | struct spu_context *ctx = file->private_data; | |
1276 | u32 free_elements, tagstatus; | |
1277 | unsigned int mask; | |
1278 | ||
1279 | spu_acquire(ctx); | |
1280 | ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2); | |
1281 | free_elements = ctx->ops->get_mfc_free_elements(ctx); | |
1282 | tagstatus = ctx->ops->read_mfc_tagstatus(ctx); | |
1283 | spu_release(ctx); | |
1284 | ||
1285 | poll_wait(file, &ctx->mfc_wq, wait); | |
1286 | ||
1287 | mask = 0; | |
1288 | if (free_elements & 0xffff) | |
1289 | mask |= POLLOUT | POLLWRNORM; | |
1290 | if (tagstatus & ctx->tagwait) | |
1291 | mask |= POLLIN | POLLRDNORM; | |
1292 | ||
1293 | pr_debug("%s: free %d tagstatus %d tagwait %d\n", __FUNCTION__, | |
1294 | free_elements, tagstatus, ctx->tagwait); | |
1295 | ||
1296 | return mask; | |
1297 | } | |
1298 | ||
73b6af8a | 1299 | static int spufs_mfc_flush(struct file *file, fl_owner_t id) |
a33a7d73 AB |
1300 | { |
1301 | struct spu_context *ctx = file->private_data; | |
1302 | int ret; | |
1303 | ||
1304 | spu_acquire(ctx); | |
1305 | #if 0 | |
1306 | /* this currently hangs */ | |
1307 | ret = spufs_wait(ctx->mfc_wq, | |
1308 | ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2)); | |
1309 | if (ret) | |
1310 | goto out; | |
1311 | ret = spufs_wait(ctx->mfc_wq, | |
1312 | ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait); | |
1313 | out: | |
1314 | #else | |
1315 | ret = 0; | |
1316 | #endif | |
1317 | spu_release(ctx); | |
1318 | ||
1319 | return ret; | |
1320 | } | |
1321 | ||
1322 | static int spufs_mfc_fsync(struct file *file, struct dentry *dentry, | |
1323 | int datasync) | |
1324 | { | |
73b6af8a | 1325 | return spufs_mfc_flush(file, NULL); |
a33a7d73 AB |
1326 | } |
1327 | ||
1328 | static int spufs_mfc_fasync(int fd, struct file *file, int on) | |
1329 | { | |
1330 | struct spu_context *ctx = file->private_data; | |
1331 | ||
1332 | return fasync_helper(fd, file, on, &ctx->mfc_fasync); | |
1333 | } | |
1334 | ||
1335 | static struct file_operations spufs_mfc_fops = { | |
1336 | .open = spufs_mfc_open, | |
1337 | .read = spufs_mfc_read, | |
1338 | .write = spufs_mfc_write, | |
1339 | .poll = spufs_mfc_poll, | |
1340 | .flush = spufs_mfc_flush, | |
1341 | .fsync = spufs_mfc_fsync, | |
1342 | .fasync = spufs_mfc_fasync, | |
6df10a82 | 1343 | .mmap = spufs_mfc_mmap, |
a33a7d73 AB |
1344 | }; |
1345 | ||
099814bb JK |
1346 | |
1347 | static int spufs_recycle_open(struct inode *inode, struct file *file) | |
1348 | { | |
1349 | file->private_data = SPUFS_I(inode)->i_ctx; | |
1350 | return nonseekable_open(inode, file); | |
1351 | } | |
1352 | ||
1353 | static ssize_t spufs_recycle_write(struct file *file, | |
1354 | const char __user *buffer, size_t size, loff_t *pos) | |
1355 | { | |
1356 | struct spu_context *ctx = file->private_data; | |
1357 | int ret; | |
1358 | ||
1359 | if (!(ctx->flags & SPU_CREATE_ISOLATE)) | |
1360 | return -EINVAL; | |
1361 | ||
1362 | if (size < 1) | |
1363 | return -EINVAL; | |
1364 | ||
1365 | ret = spu_recycle_isolated(ctx); | |
1366 | ||
1367 | if (ret) | |
1368 | return ret; | |
1369 | return size; | |
1370 | } | |
1371 | ||
1372 | static struct file_operations spufs_recycle_fops = { | |
1373 | .open = spufs_recycle_open, | |
1374 | .write = spufs_recycle_write, | |
1375 | }; | |
1376 | ||
67207b96 AB |
1377 | static void spufs_npc_set(void *data, u64 val) |
1378 | { | |
1379 | struct spu_context *ctx = data; | |
8b3d6663 AB |
1380 | spu_acquire(ctx); |
1381 | ctx->ops->npc_write(ctx, val); | |
1382 | spu_release(ctx); | |
67207b96 AB |
1383 | } |
1384 | ||
1385 | static u64 spufs_npc_get(void *data) | |
1386 | { | |
1387 | struct spu_context *ctx = data; | |
1388 | u64 ret; | |
8b3d6663 AB |
1389 | spu_acquire(ctx); |
1390 | ret = ctx->ops->npc_read(ctx); | |
1391 | spu_release(ctx); | |
67207b96 AB |
1392 | return ret; |
1393 | } | |
1394 | DEFINE_SIMPLE_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, "%llx\n") | |
1395 | ||
8b3d6663 AB |
1396 | static void spufs_decr_set(void *data, u64 val) |
1397 | { | |
1398 | struct spu_context *ctx = data; | |
1399 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1400 | spu_acquire_saved(ctx); | |
1401 | lscsa->decr.slot[0] = (u32) val; | |
1402 | spu_release(ctx); | |
1403 | } | |
1404 | ||
1405 | static u64 spufs_decr_get(void *data) | |
1406 | { | |
1407 | struct spu_context *ctx = data; | |
1408 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1409 | u64 ret; | |
1410 | spu_acquire_saved(ctx); | |
1411 | ret = lscsa->decr.slot[0]; | |
1412 | spu_release(ctx); | |
1413 | return ret; | |
1414 | } | |
1415 | DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set, | |
1416 | "%llx\n") | |
1417 | ||
1418 | static void spufs_decr_status_set(void *data, u64 val) | |
1419 | { | |
1420 | struct spu_context *ctx = data; | |
1421 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1422 | spu_acquire_saved(ctx); | |
1423 | lscsa->decr_status.slot[0] = (u32) val; | |
1424 | spu_release(ctx); | |
1425 | } | |
1426 | ||
1427 | static u64 spufs_decr_status_get(void *data) | |
1428 | { | |
1429 | struct spu_context *ctx = data; | |
1430 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1431 | u64 ret; | |
1432 | spu_acquire_saved(ctx); | |
1433 | ret = lscsa->decr_status.slot[0]; | |
1434 | spu_release(ctx); | |
1435 | return ret; | |
1436 | } | |
1437 | DEFINE_SIMPLE_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get, | |
1438 | spufs_decr_status_set, "%llx\n") | |
1439 | ||
1440 | static void spufs_spu_tag_mask_set(void *data, u64 val) | |
1441 | { | |
1442 | struct spu_context *ctx = data; | |
1443 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1444 | spu_acquire_saved(ctx); | |
1445 | lscsa->tag_mask.slot[0] = (u32) val; | |
1446 | spu_release(ctx); | |
1447 | } | |
1448 | ||
1449 | static u64 spufs_spu_tag_mask_get(void *data) | |
1450 | { | |
1451 | struct spu_context *ctx = data; | |
1452 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1453 | u64 ret; | |
1454 | spu_acquire_saved(ctx); | |
1455 | ret = lscsa->tag_mask.slot[0]; | |
1456 | spu_release(ctx); | |
1457 | return ret; | |
1458 | } | |
1459 | DEFINE_SIMPLE_ATTRIBUTE(spufs_spu_tag_mask_ops, spufs_spu_tag_mask_get, | |
1460 | spufs_spu_tag_mask_set, "%llx\n") | |
1461 | ||
1462 | static void spufs_event_mask_set(void *data, u64 val) | |
1463 | { | |
1464 | struct spu_context *ctx = data; | |
1465 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1466 | spu_acquire_saved(ctx); | |
1467 | lscsa->event_mask.slot[0] = (u32) val; | |
1468 | spu_release(ctx); | |
1469 | } | |
1470 | ||
1471 | static u64 spufs_event_mask_get(void *data) | |
1472 | { | |
1473 | struct spu_context *ctx = data; | |
1474 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1475 | u64 ret; | |
1476 | spu_acquire_saved(ctx); | |
1477 | ret = lscsa->event_mask.slot[0]; | |
1478 | spu_release(ctx); | |
1479 | return ret; | |
1480 | } | |
1481 | DEFINE_SIMPLE_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get, | |
1482 | spufs_event_mask_set, "%llx\n") | |
1483 | ||
1484 | static void spufs_srr0_set(void *data, u64 val) | |
1485 | { | |
1486 | struct spu_context *ctx = data; | |
1487 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1488 | spu_acquire_saved(ctx); | |
1489 | lscsa->srr0.slot[0] = (u32) val; | |
1490 | spu_release(ctx); | |
1491 | } | |
1492 | ||
1493 | static u64 spufs_srr0_get(void *data) | |
1494 | { | |
1495 | struct spu_context *ctx = data; | |
1496 | struct spu_lscsa *lscsa = ctx->csa.lscsa; | |
1497 | u64 ret; | |
1498 | spu_acquire_saved(ctx); | |
1499 | ret = lscsa->srr0.slot[0]; | |
1500 | spu_release(ctx); | |
1501 | return ret; | |
1502 | } | |
1503 | DEFINE_SIMPLE_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set, | |
1504 | "%llx\n") | |
1505 | ||
7b1a7014 | 1506 | static u64 spufs_id_get(void *data) |
1507 | { | |
1508 | struct spu_context *ctx = data; | |
1509 | u64 num; | |
1510 | ||
1511 | spu_acquire(ctx); | |
1512 | if (ctx->state == SPU_STATE_RUNNABLE) | |
1513 | num = ctx->spu->number; | |
1514 | else | |
1515 | num = (unsigned int)-1; | |
1516 | spu_release(ctx); | |
1517 | ||
1518 | return num; | |
1519 | } | |
e45d6634 | 1520 | DEFINE_SIMPLE_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n") |
7b1a7014 | 1521 | |
86767277 AB |
1522 | static u64 spufs_object_id_get(void *data) |
1523 | { | |
1524 | struct spu_context *ctx = data; | |
1525 | return ctx->object_id; | |
1526 | } | |
1527 | ||
1528 | static void spufs_object_id_set(void *data, u64 id) | |
1529 | { | |
1530 | struct spu_context *ctx = data; | |
1531 | ctx->object_id = id; | |
1532 | } | |
1533 | ||
1534 | DEFINE_SIMPLE_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get, | |
1535 | spufs_object_id_set, "0x%llx\n"); | |
1536 | ||
67207b96 AB |
1537 | struct tree_descr spufs_dir_contents[] = { |
1538 | { "mem", &spufs_mem_fops, 0666, }, | |
8b3d6663 | 1539 | { "regs", &spufs_regs_fops, 0666, }, |
67207b96 AB |
1540 | { "mbox", &spufs_mbox_fops, 0444, }, |
1541 | { "ibox", &spufs_ibox_fops, 0444, }, | |
1542 | { "wbox", &spufs_wbox_fops, 0222, }, | |
1543 | { "mbox_stat", &spufs_mbox_stat_fops, 0444, }, | |
1544 | { "ibox_stat", &spufs_ibox_stat_fops, 0444, }, | |
1545 | { "wbox_stat", &spufs_wbox_stat_fops, 0444, }, | |
1546 | { "signal1", &spufs_signal1_fops, 0666, }, | |
1547 | { "signal2", &spufs_signal2_fops, 0666, }, | |
1548 | { "signal1_type", &spufs_signal1_type, 0666, }, | |
1549 | { "signal2_type", &spufs_signal2_type, 0666, }, | |
d9379c4b | 1550 | { "mss", &spufs_mss_fops, 0666, }, |
a33a7d73 | 1551 | { "mfc", &spufs_mfc_fops, 0666, }, |
6df10a82 | 1552 | { "cntl", &spufs_cntl_fops, 0666, }, |
67207b96 | 1553 | { "npc", &spufs_npc_ops, 0666, }, |
8b3d6663 AB |
1554 | { "fpcr", &spufs_fpcr_fops, 0666, }, |
1555 | { "decr", &spufs_decr_ops, 0666, }, | |
1556 | { "decr_status", &spufs_decr_status_ops, 0666, }, | |
1557 | { "spu_tag_mask", &spufs_spu_tag_mask_ops, 0666, }, | |
1558 | { "event_mask", &spufs_event_mask_ops, 0666, }, | |
1559 | { "srr0", &spufs_srr0_ops, 0666, }, | |
27d5bf2a | 1560 | { "psmap", &spufs_psmap_fops, 0666, }, |
86767277 AB |
1561 | { "phys-id", &spufs_id_ops, 0666, }, |
1562 | { "object-id", &spufs_object_id_ops, 0666, }, | |
67207b96 AB |
1563 | {}, |
1564 | }; | |
5737edd1 MN |
1565 | |
1566 | struct tree_descr spufs_dir_nosched_contents[] = { | |
1567 | { "mem", &spufs_mem_fops, 0666, }, | |
1568 | { "mbox", &spufs_mbox_fops, 0444, }, | |
1569 | { "ibox", &spufs_ibox_fops, 0444, }, | |
1570 | { "wbox", &spufs_wbox_fops, 0222, }, | |
1571 | { "mbox_stat", &spufs_mbox_stat_fops, 0444, }, | |
1572 | { "ibox_stat", &spufs_ibox_stat_fops, 0444, }, | |
1573 | { "wbox_stat", &spufs_wbox_stat_fops, 0444, }, | |
1574 | { "signal1", &spufs_signal1_fops, 0666, }, | |
1575 | { "signal2", &spufs_signal2_fops, 0666, }, | |
1576 | { "signal1_type", &spufs_signal1_type, 0666, }, | |
1577 | { "signal2_type", &spufs_signal2_type, 0666, }, | |
1578 | { "mss", &spufs_mss_fops, 0666, }, | |
1579 | { "mfc", &spufs_mfc_fops, 0666, }, | |
1580 | { "cntl", &spufs_cntl_fops, 0666, }, | |
1581 | { "npc", &spufs_npc_ops, 0666, }, | |
1582 | { "psmap", &spufs_psmap_fops, 0666, }, | |
1583 | { "phys-id", &spufs_id_ops, 0666, }, | |
1584 | { "object-id", &spufs_object_id_ops, 0666, }, | |
099814bb | 1585 | { "recycle", &spufs_recycle_fops, 0222, }, |
5737edd1 MN |
1586 | {}, |
1587 | }; |