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 23 #undef DEBUG 24 25 #include <linux/fs.h> 26 #include <linux/ioctl.h> 27 #include <linux/module.h> 28 #include <linux/pagemap.h> 29 #include <linux/poll.h> 30 #include <linux/ptrace.h> 31 #include <linux/seq_file.h> 32 #include <linux/marker.h> 33 34 #include <asm/io.h> 35 #include <asm/time.h> 36 #include <asm/spu.h> 37 #include <asm/spu_info.h> 38 #include <asm/uaccess.h> 39 40 #include "spufs.h" 41 42 #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000) 43 44 /* Simple attribute files */ 45 struct spufs_attr { 46 int (*get)(void *, u64 *); 47 int (*set)(void *, u64); 48 char get_buf[24]; /* enough to store a u64 and "\n\0" */ 49 char set_buf[24]; 50 void *data; 51 const char *fmt; /* format for read operation */ 52 struct mutex mutex; /* protects access to these buffers */ 53 }; 54 55 static int spufs_attr_open(struct inode *inode, struct file *file, 56 int (*get)(void *, u64 *), int (*set)(void *, u64), 57 const char *fmt) 58 { 59 struct spufs_attr *attr; 60 61 attr = kmalloc(sizeof(*attr), GFP_KERNEL); 62 if (!attr) 63 return -ENOMEM; 64 65 attr->get = get; 66 attr->set = set; 67 attr->data = inode->i_private; 68 attr->fmt = fmt; 69 mutex_init(&attr->mutex); 70 file->private_data = attr; 71 72 return nonseekable_open(inode, file); 73 } 74 75 static int spufs_attr_release(struct inode *inode, struct file *file) 76 { 77 kfree(file->private_data); 78 return 0; 79 } 80 81 static ssize_t spufs_attr_read(struct file *file, char __user *buf, 82 size_t len, loff_t *ppos) 83 { 84 struct spufs_attr *attr; 85 size_t size; 86 ssize_t ret; 87 88 attr = file->private_data; 89 if (!attr->get) 90 return -EACCES; 91 92 ret = mutex_lock_interruptible(&attr->mutex); 93 if (ret) 94 return ret; 95 96 if (*ppos) { /* continued read */ 97 size = strlen(attr->get_buf); 98 } else { /* first read */ 99 u64 val; 100 ret = attr->get(attr->data, &val); 101 if (ret) 102 goto out; 103 104 size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 105 attr->fmt, (unsigned long long)val); 106 } 107 108 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 109 out: 110 mutex_unlock(&attr->mutex); 111 return ret; 112 } 113 114 static ssize_t spufs_attr_write(struct file *file, const char __user *buf, 115 size_t len, loff_t *ppos) 116 { 117 struct spufs_attr *attr; 118 u64 val; 119 size_t size; 120 ssize_t ret; 121 122 attr = file->private_data; 123 if (!attr->set) 124 return -EACCES; 125 126 ret = mutex_lock_interruptible(&attr->mutex); 127 if (ret) 128 return ret; 129 130 ret = -EFAULT; 131 size = min(sizeof(attr->set_buf) - 1, len); 132 if (copy_from_user(attr->set_buf, buf, size)) 133 goto out; 134 135 ret = len; /* claim we got the whole input */ 136 attr->set_buf[size] = '\0'; 137 val = simple_strtol(attr->set_buf, NULL, 0); 138 attr->set(attr->data, val); 139 out: 140 mutex_unlock(&attr->mutex); 141 return ret; 142 } 143 144 #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \ 145 static int __fops ## _open(struct inode *inode, struct file *file) \ 146 { \ 147 __simple_attr_check_format(__fmt, 0ull); \ 148 return spufs_attr_open(inode, file, __get, __set, __fmt); \ 149 } \ 150 static struct file_operations __fops = { \ 151 .owner = THIS_MODULE, \ 152 .open = __fops ## _open, \ 153 .release = spufs_attr_release, \ 154 .read = spufs_attr_read, \ 155 .write = spufs_attr_write, \ 156 }; 157 158 159 static int 160 spufs_mem_open(struct inode *inode, struct file *file) 161 { 162 struct spufs_inode_info *i = SPUFS_I(inode); 163 struct spu_context *ctx = i->i_ctx; 164 165 mutex_lock(&ctx->mapping_lock); 166 file->private_data = ctx; 167 if (!i->i_openers++) 168 ctx->local_store = inode->i_mapping; 169 mutex_unlock(&ctx->mapping_lock); 170 return 0; 171 } 172 173 static int 174 spufs_mem_release(struct inode *inode, struct file *file) 175 { 176 struct spufs_inode_info *i = SPUFS_I(inode); 177 struct spu_context *ctx = i->i_ctx; 178 179 mutex_lock(&ctx->mapping_lock); 180 if (!--i->i_openers) 181 ctx->local_store = NULL; 182 mutex_unlock(&ctx->mapping_lock); 183 return 0; 184 } 185 186 static ssize_t 187 __spufs_mem_read(struct spu_context *ctx, char __user *buffer, 188 size_t size, loff_t *pos) 189 { 190 char *local_store = ctx->ops->get_ls(ctx); 191 return simple_read_from_buffer(buffer, size, pos, local_store, 192 LS_SIZE); 193 } 194 195 static ssize_t 196 spufs_mem_read(struct file *file, char __user *buffer, 197 size_t size, loff_t *pos) 198 { 199 struct spu_context *ctx = file->private_data; 200 ssize_t ret; 201 202 ret = spu_acquire(ctx); 203 if (ret) 204 return ret; 205 ret = __spufs_mem_read(ctx, buffer, size, pos); 206 spu_release(ctx); 207 208 return ret; 209 } 210 211 static ssize_t 212 spufs_mem_write(struct file *file, const char __user *buffer, 213 size_t size, loff_t *ppos) 214 { 215 struct spu_context *ctx = file->private_data; 216 char *local_store; 217 loff_t pos = *ppos; 218 int ret; 219 220 if (pos < 0) 221 return -EINVAL; 222 if (pos > LS_SIZE) 223 return -EFBIG; 224 if (size > LS_SIZE - pos) 225 size = LS_SIZE - pos; 226 227 ret = spu_acquire(ctx); 228 if (ret) 229 return ret; 230 231 local_store = ctx->ops->get_ls(ctx); 232 ret = copy_from_user(local_store + pos, buffer, size); 233 spu_release(ctx); 234 235 if (ret) 236 return -EFAULT; 237 *ppos = pos + size; 238 return size; 239 } 240 241 static int 242 spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 243 { 244 struct spu_context *ctx = vma->vm_file->private_data; 245 unsigned long address = (unsigned long)vmf->virtual_address; 246 unsigned long pfn, offset; 247 248 #ifdef CONFIG_SPU_FS_64K_LS 249 struct spu_state *csa = &ctx->csa; 250 int psize; 251 252 /* Check what page size we are using */ 253 psize = get_slice_psize(vma->vm_mm, address); 254 255 /* Some sanity checking */ 256 BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K)); 257 258 /* Wow, 64K, cool, we need to align the address though */ 259 if (csa->use_big_pages) { 260 BUG_ON(vma->vm_start & 0xffff); 261 address &= ~0xfffful; 262 } 263 #endif /* CONFIG_SPU_FS_64K_LS */ 264 265 offset = vmf->pgoff << PAGE_SHIFT; 266 if (offset >= LS_SIZE) 267 return VM_FAULT_SIGBUS; 268 269 pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n", 270 address, offset); 271 272 if (spu_acquire(ctx)) 273 return VM_FAULT_NOPAGE; 274 275 if (ctx->state == SPU_STATE_SAVED) { 276 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 277 & ~_PAGE_NO_CACHE); 278 pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset); 279 } else { 280 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 281 | _PAGE_NO_CACHE); 282 pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT; 283 } 284 vm_insert_pfn(vma, address, pfn); 285 286 spu_release(ctx); 287 288 return VM_FAULT_NOPAGE; 289 } 290 291 static int spufs_mem_mmap_access(struct vm_area_struct *vma, 292 unsigned long address, 293 void *buf, int len, int write) 294 { 295 struct spu_context *ctx = vma->vm_file->private_data; 296 unsigned long offset = address - vma->vm_start; 297 char *local_store; 298 299 if (write && !(vma->vm_flags & VM_WRITE)) 300 return -EACCES; 301 if (spu_acquire(ctx)) 302 return -EINTR; 303 if ((offset + len) > vma->vm_end) 304 len = vma->vm_end - offset; 305 local_store = ctx->ops->get_ls(ctx); 306 if (write) 307 memcpy_toio(local_store + offset, buf, len); 308 else 309 memcpy_fromio(buf, local_store + offset, len); 310 spu_release(ctx); 311 return len; 312 } 313 314 static struct vm_operations_struct spufs_mem_mmap_vmops = { 315 .fault = spufs_mem_mmap_fault, 316 .access = spufs_mem_mmap_access, 317 }; 318 319 static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma) 320 { 321 #ifdef CONFIG_SPU_FS_64K_LS 322 struct spu_context *ctx = file->private_data; 323 struct spu_state *csa = &ctx->csa; 324 325 /* Sanity check VMA alignment */ 326 if (csa->use_big_pages) { 327 pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx," 328 " pgoff=0x%lx\n", vma->vm_start, vma->vm_end, 329 vma->vm_pgoff); 330 if (vma->vm_start & 0xffff) 331 return -EINVAL; 332 if (vma->vm_pgoff & 0xf) 333 return -EINVAL; 334 } 335 #endif /* CONFIG_SPU_FS_64K_LS */ 336 337 if (!(vma->vm_flags & VM_SHARED)) 338 return -EINVAL; 339 340 vma->vm_flags |= VM_IO | VM_PFNMAP; 341 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 342 | _PAGE_NO_CACHE); 343 344 vma->vm_ops = &spufs_mem_mmap_vmops; 345 return 0; 346 } 347 348 #ifdef CONFIG_SPU_FS_64K_LS 349 static unsigned long spufs_get_unmapped_area(struct file *file, 350 unsigned long addr, unsigned long len, unsigned long pgoff, 351 unsigned long flags) 352 { 353 struct spu_context *ctx = file->private_data; 354 struct spu_state *csa = &ctx->csa; 355 356 /* If not using big pages, fallback to normal MM g_u_a */ 357 if (!csa->use_big_pages) 358 return current->mm->get_unmapped_area(file, addr, len, 359 pgoff, flags); 360 361 /* Else, try to obtain a 64K pages slice */ 362 return slice_get_unmapped_area(addr, len, flags, 363 MMU_PAGE_64K, 1, 0); 364 } 365 #endif /* CONFIG_SPU_FS_64K_LS */ 366 367 static const struct file_operations spufs_mem_fops = { 368 .open = spufs_mem_open, 369 .release = spufs_mem_release, 370 .read = spufs_mem_read, 371 .write = spufs_mem_write, 372 .llseek = generic_file_llseek, 373 .mmap = spufs_mem_mmap, 374 #ifdef CONFIG_SPU_FS_64K_LS 375 .get_unmapped_area = spufs_get_unmapped_area, 376 #endif 377 }; 378 379 static int spufs_ps_fault(struct vm_area_struct *vma, 380 struct vm_fault *vmf, 381 unsigned long ps_offs, 382 unsigned long ps_size) 383 { 384 struct spu_context *ctx = vma->vm_file->private_data; 385 unsigned long area, offset = vmf->pgoff << PAGE_SHIFT; 386 int ret = 0; 387 388 spu_context_nospu_trace(spufs_ps_fault__enter, ctx); 389 390 if (offset >= ps_size) 391 return VM_FAULT_SIGBUS; 392 393 /* 394 * Because we release the mmap_sem, the context may be destroyed while 395 * we're in spu_wait. Grab an extra reference so it isn't destroyed 396 * in the meantime. 397 */ 398 get_spu_context(ctx); 399 400 /* 401 * We have to wait for context to be loaded before we have 402 * pages to hand out to the user, but we don't want to wait 403 * with the mmap_sem held. 404 * It is possible to drop the mmap_sem here, but then we need 405 * to return VM_FAULT_NOPAGE because the mappings may have 406 * hanged. 407 */ 408 if (spu_acquire(ctx)) 409 goto refault; 410 411 if (ctx->state == SPU_STATE_SAVED) { 412 up_read(¤t->mm->mmap_sem); 413 spu_context_nospu_trace(spufs_ps_fault__sleep, ctx); 414 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE); 415 spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu); 416 down_read(¤t->mm->mmap_sem); 417 } else { 418 area = ctx->spu->problem_phys + ps_offs; 419 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, 420 (area + offset) >> PAGE_SHIFT); 421 spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu); 422 } 423 424 if (!ret) 425 spu_release(ctx); 426 427 refault: 428 put_spu_context(ctx); 429 return VM_FAULT_NOPAGE; 430 } 431 432 #if SPUFS_MMAP_4K 433 static int spufs_cntl_mmap_fault(struct vm_area_struct *vma, 434 struct vm_fault *vmf) 435 { 436 return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE); 437 } 438 439 static struct vm_operations_struct spufs_cntl_mmap_vmops = { 440 .fault = spufs_cntl_mmap_fault, 441 }; 442 443 /* 444 * mmap support for problem state control area [0x4000 - 0x4fff]. 445 */ 446 static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma) 447 { 448 if (!(vma->vm_flags & VM_SHARED)) 449 return -EINVAL; 450 451 vma->vm_flags |= VM_IO | VM_PFNMAP; 452 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 453 | _PAGE_NO_CACHE | _PAGE_GUARDED); 454 455 vma->vm_ops = &spufs_cntl_mmap_vmops; 456 return 0; 457 } 458 #else /* SPUFS_MMAP_4K */ 459 #define spufs_cntl_mmap NULL 460 #endif /* !SPUFS_MMAP_4K */ 461 462 static int spufs_cntl_get(void *data, u64 *val) 463 { 464 struct spu_context *ctx = data; 465 int ret; 466 467 ret = spu_acquire(ctx); 468 if (ret) 469 return ret; 470 *val = ctx->ops->status_read(ctx); 471 spu_release(ctx); 472 473 return 0; 474 } 475 476 static int spufs_cntl_set(void *data, u64 val) 477 { 478 struct spu_context *ctx = data; 479 int ret; 480 481 ret = spu_acquire(ctx); 482 if (ret) 483 return ret; 484 ctx->ops->runcntl_write(ctx, val); 485 spu_release(ctx); 486 487 return 0; 488 } 489 490 static int spufs_cntl_open(struct inode *inode, struct file *file) 491 { 492 struct spufs_inode_info *i = SPUFS_I(inode); 493 struct spu_context *ctx = i->i_ctx; 494 495 mutex_lock(&ctx->mapping_lock); 496 file->private_data = ctx; 497 if (!i->i_openers++) 498 ctx->cntl = inode->i_mapping; 499 mutex_unlock(&ctx->mapping_lock); 500 return simple_attr_open(inode, file, spufs_cntl_get, 501 spufs_cntl_set, "0x%08lx"); 502 } 503 504 static int 505 spufs_cntl_release(struct inode *inode, struct file *file) 506 { 507 struct spufs_inode_info *i = SPUFS_I(inode); 508 struct spu_context *ctx = i->i_ctx; 509 510 simple_attr_release(inode, file); 511 512 mutex_lock(&ctx->mapping_lock); 513 if (!--i->i_openers) 514 ctx->cntl = NULL; 515 mutex_unlock(&ctx->mapping_lock); 516 return 0; 517 } 518 519 static const struct file_operations spufs_cntl_fops = { 520 .open = spufs_cntl_open, 521 .release = spufs_cntl_release, 522 .read = simple_attr_read, 523 .write = simple_attr_write, 524 .mmap = spufs_cntl_mmap, 525 }; 526 527 static int 528 spufs_regs_open(struct inode *inode, struct file *file) 529 { 530 struct spufs_inode_info *i = SPUFS_I(inode); 531 file->private_data = i->i_ctx; 532 return 0; 533 } 534 535 static ssize_t 536 __spufs_regs_read(struct spu_context *ctx, char __user *buffer, 537 size_t size, loff_t *pos) 538 { 539 struct spu_lscsa *lscsa = ctx->csa.lscsa; 540 return simple_read_from_buffer(buffer, size, pos, 541 lscsa->gprs, sizeof lscsa->gprs); 542 } 543 544 static ssize_t 545 spufs_regs_read(struct file *file, char __user *buffer, 546 size_t size, loff_t *pos) 547 { 548 int ret; 549 struct spu_context *ctx = file->private_data; 550 551 /* pre-check for file position: if we'd return EOF, there's no point 552 * causing a deschedule */ 553 if (*pos >= sizeof(ctx->csa.lscsa->gprs)) 554 return 0; 555 556 ret = spu_acquire_saved(ctx); 557 if (ret) 558 return ret; 559 ret = __spufs_regs_read(ctx, buffer, size, pos); 560 spu_release_saved(ctx); 561 return ret; 562 } 563 564 static ssize_t 565 spufs_regs_write(struct file *file, const char __user *buffer, 566 size_t size, loff_t *pos) 567 { 568 struct spu_context *ctx = file->private_data; 569 struct spu_lscsa *lscsa = ctx->csa.lscsa; 570 int ret; 571 572 size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size); 573 if (size <= 0) 574 return -EFBIG; 575 *pos += size; 576 577 ret = spu_acquire_saved(ctx); 578 if (ret) 579 return ret; 580 581 ret = copy_from_user(lscsa->gprs + *pos - size, 582 buffer, size) ? -EFAULT : size; 583 584 spu_release_saved(ctx); 585 return ret; 586 } 587 588 static const struct file_operations spufs_regs_fops = { 589 .open = spufs_regs_open, 590 .read = spufs_regs_read, 591 .write = spufs_regs_write, 592 .llseek = generic_file_llseek, 593 }; 594 595 static ssize_t 596 __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer, 597 size_t size, loff_t * pos) 598 { 599 struct spu_lscsa *lscsa = ctx->csa.lscsa; 600 return simple_read_from_buffer(buffer, size, pos, 601 &lscsa->fpcr, sizeof(lscsa->fpcr)); 602 } 603 604 static ssize_t 605 spufs_fpcr_read(struct file *file, char __user * buffer, 606 size_t size, loff_t * pos) 607 { 608 int ret; 609 struct spu_context *ctx = file->private_data; 610 611 ret = spu_acquire_saved(ctx); 612 if (ret) 613 return ret; 614 ret = __spufs_fpcr_read(ctx, buffer, size, pos); 615 spu_release_saved(ctx); 616 return ret; 617 } 618 619 static ssize_t 620 spufs_fpcr_write(struct file *file, const char __user * buffer, 621 size_t size, loff_t * pos) 622 { 623 struct spu_context *ctx = file->private_data; 624 struct spu_lscsa *lscsa = ctx->csa.lscsa; 625 int ret; 626 627 size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size); 628 if (size <= 0) 629 return -EFBIG; 630 631 ret = spu_acquire_saved(ctx); 632 if (ret) 633 return ret; 634 635 *pos += size; 636 ret = copy_from_user((char *)&lscsa->fpcr + *pos - size, 637 buffer, size) ? -EFAULT : size; 638 639 spu_release_saved(ctx); 640 return ret; 641 } 642 643 static const struct file_operations spufs_fpcr_fops = { 644 .open = spufs_regs_open, 645 .read = spufs_fpcr_read, 646 .write = spufs_fpcr_write, 647 .llseek = generic_file_llseek, 648 }; 649 650 /* generic open function for all pipe-like files */ 651 static int spufs_pipe_open(struct inode *inode, struct file *file) 652 { 653 struct spufs_inode_info *i = SPUFS_I(inode); 654 file->private_data = i->i_ctx; 655 656 return nonseekable_open(inode, file); 657 } 658 659 /* 660 * Read as many bytes from the mailbox as possible, until 661 * one of the conditions becomes true: 662 * 663 * - no more data available in the mailbox 664 * - end of the user provided buffer 665 * - end of the mapped area 666 */ 667 static ssize_t spufs_mbox_read(struct file *file, char __user *buf, 668 size_t len, loff_t *pos) 669 { 670 struct spu_context *ctx = file->private_data; 671 u32 mbox_data, __user *udata; 672 ssize_t count; 673 674 if (len < 4) 675 return -EINVAL; 676 677 if (!access_ok(VERIFY_WRITE, buf, len)) 678 return -EFAULT; 679 680 udata = (void __user *)buf; 681 682 count = spu_acquire(ctx); 683 if (count) 684 return count; 685 686 for (count = 0; (count + 4) <= len; count += 4, udata++) { 687 int ret; 688 ret = ctx->ops->mbox_read(ctx, &mbox_data); 689 if (ret == 0) 690 break; 691 692 /* 693 * at the end of the mapped area, we can fault 694 * but still need to return the data we have 695 * read successfully so far. 696 */ 697 ret = __put_user(mbox_data, udata); 698 if (ret) { 699 if (!count) 700 count = -EFAULT; 701 break; 702 } 703 } 704 spu_release(ctx); 705 706 if (!count) 707 count = -EAGAIN; 708 709 return count; 710 } 711 712 static const struct file_operations spufs_mbox_fops = { 713 .open = spufs_pipe_open, 714 .read = spufs_mbox_read, 715 }; 716 717 static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf, 718 size_t len, loff_t *pos) 719 { 720 struct spu_context *ctx = file->private_data; 721 ssize_t ret; 722 u32 mbox_stat; 723 724 if (len < 4) 725 return -EINVAL; 726 727 ret = spu_acquire(ctx); 728 if (ret) 729 return ret; 730 731 mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff; 732 733 spu_release(ctx); 734 735 if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat)) 736 return -EFAULT; 737 738 return 4; 739 } 740 741 static const struct file_operations spufs_mbox_stat_fops = { 742 .open = spufs_pipe_open, 743 .read = spufs_mbox_stat_read, 744 }; 745 746 /* low-level ibox access function */ 747 size_t spu_ibox_read(struct spu_context *ctx, u32 *data) 748 { 749 return ctx->ops->ibox_read(ctx, data); 750 } 751 752 static int spufs_ibox_fasync(int fd, struct file *file, int on) 753 { 754 struct spu_context *ctx = file->private_data; 755 756 return fasync_helper(fd, file, on, &ctx->ibox_fasync); 757 } 758 759 /* interrupt-level ibox callback function. */ 760 void spufs_ibox_callback(struct spu *spu) 761 { 762 struct spu_context *ctx = spu->ctx; 763 764 if (!ctx) 765 return; 766 767 wake_up_all(&ctx->ibox_wq); 768 kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN); 769 } 770 771 /* 772 * Read as many bytes from the interrupt mailbox as possible, until 773 * one of the conditions becomes true: 774 * 775 * - no more data available in the mailbox 776 * - end of the user provided buffer 777 * - end of the mapped area 778 * 779 * If the file is opened without O_NONBLOCK, we wait here until 780 * any data is available, but return when we have been able to 781 * read something. 782 */ 783 static ssize_t spufs_ibox_read(struct file *file, char __user *buf, 784 size_t len, loff_t *pos) 785 { 786 struct spu_context *ctx = file->private_data; 787 u32 ibox_data, __user *udata; 788 ssize_t count; 789 790 if (len < 4) 791 return -EINVAL; 792 793 if (!access_ok(VERIFY_WRITE, buf, len)) 794 return -EFAULT; 795 796 udata = (void __user *)buf; 797 798 count = spu_acquire(ctx); 799 if (count) 800 goto out; 801 802 /* wait only for the first element */ 803 count = 0; 804 if (file->f_flags & O_NONBLOCK) { 805 if (!spu_ibox_read(ctx, &ibox_data)) { 806 count = -EAGAIN; 807 goto out_unlock; 808 } 809 } else { 810 count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data)); 811 if (count) 812 goto out; 813 } 814 815 /* if we can't write at all, return -EFAULT */ 816 count = __put_user(ibox_data, udata); 817 if (count) 818 goto out_unlock; 819 820 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) { 821 int ret; 822 ret = ctx->ops->ibox_read(ctx, &ibox_data); 823 if (ret == 0) 824 break; 825 /* 826 * at the end of the mapped area, we can fault 827 * but still need to return the data we have 828 * read successfully so far. 829 */ 830 ret = __put_user(ibox_data, udata); 831 if (ret) 832 break; 833 } 834 835 out_unlock: 836 spu_release(ctx); 837 out: 838 return count; 839 } 840 841 static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait) 842 { 843 struct spu_context *ctx = file->private_data; 844 unsigned int mask; 845 846 poll_wait(file, &ctx->ibox_wq, wait); 847 848 /* 849 * For now keep this uninterruptible and also ignore the rule 850 * that poll should not sleep. Will be fixed later. 851 */ 852 mutex_lock(&ctx->state_mutex); 853 mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM); 854 spu_release(ctx); 855 856 return mask; 857 } 858 859 static const struct file_operations spufs_ibox_fops = { 860 .open = spufs_pipe_open, 861 .read = spufs_ibox_read, 862 .poll = spufs_ibox_poll, 863 .fasync = spufs_ibox_fasync, 864 }; 865 866 static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf, 867 size_t len, loff_t *pos) 868 { 869 struct spu_context *ctx = file->private_data; 870 ssize_t ret; 871 u32 ibox_stat; 872 873 if (len < 4) 874 return -EINVAL; 875 876 ret = spu_acquire(ctx); 877 if (ret) 878 return ret; 879 ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff; 880 spu_release(ctx); 881 882 if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat)) 883 return -EFAULT; 884 885 return 4; 886 } 887 888 static const struct file_operations spufs_ibox_stat_fops = { 889 .open = spufs_pipe_open, 890 .read = spufs_ibox_stat_read, 891 }; 892 893 /* low-level mailbox write */ 894 size_t spu_wbox_write(struct spu_context *ctx, u32 data) 895 { 896 return ctx->ops->wbox_write(ctx, data); 897 } 898 899 static int spufs_wbox_fasync(int fd, struct file *file, int on) 900 { 901 struct spu_context *ctx = file->private_data; 902 int ret; 903 904 ret = fasync_helper(fd, file, on, &ctx->wbox_fasync); 905 906 return ret; 907 } 908 909 /* interrupt-level wbox callback function. */ 910 void spufs_wbox_callback(struct spu *spu) 911 { 912 struct spu_context *ctx = spu->ctx; 913 914 if (!ctx) 915 return; 916 917 wake_up_all(&ctx->wbox_wq); 918 kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT); 919 } 920 921 /* 922 * Write as many bytes to the interrupt mailbox as possible, until 923 * one of the conditions becomes true: 924 * 925 * - the mailbox is full 926 * - end of the user provided buffer 927 * - end of the mapped area 928 * 929 * If the file is opened without O_NONBLOCK, we wait here until 930 * space is availabyl, but return when we have been able to 931 * write something. 932 */ 933 static ssize_t spufs_wbox_write(struct file *file, const char __user *buf, 934 size_t len, loff_t *pos) 935 { 936 struct spu_context *ctx = file->private_data; 937 u32 wbox_data, __user *udata; 938 ssize_t count; 939 940 if (len < 4) 941 return -EINVAL; 942 943 udata = (void __user *)buf; 944 if (!access_ok(VERIFY_READ, buf, len)) 945 return -EFAULT; 946 947 if (__get_user(wbox_data, udata)) 948 return -EFAULT; 949 950 count = spu_acquire(ctx); 951 if (count) 952 goto out; 953 954 /* 955 * make sure we can at least write one element, by waiting 956 * in case of !O_NONBLOCK 957 */ 958 count = 0; 959 if (file->f_flags & O_NONBLOCK) { 960 if (!spu_wbox_write(ctx, wbox_data)) { 961 count = -EAGAIN; 962 goto out_unlock; 963 } 964 } else { 965 count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data)); 966 if (count) 967 goto out; 968 } 969 970 971 /* write as much as possible */ 972 for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) { 973 int ret; 974 ret = __get_user(wbox_data, udata); 975 if (ret) 976 break; 977 978 ret = spu_wbox_write(ctx, wbox_data); 979 if (ret == 0) 980 break; 981 } 982 983 out_unlock: 984 spu_release(ctx); 985 out: 986 return count; 987 } 988 989 static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait) 990 { 991 struct spu_context *ctx = file->private_data; 992 unsigned int mask; 993 994 poll_wait(file, &ctx->wbox_wq, wait); 995 996 /* 997 * For now keep this uninterruptible and also ignore the rule 998 * that poll should not sleep. Will be fixed later. 999 */ 1000 mutex_lock(&ctx->state_mutex); 1001 mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM); 1002 spu_release(ctx); 1003 1004 return mask; 1005 } 1006 1007 static const struct file_operations spufs_wbox_fops = { 1008 .open = spufs_pipe_open, 1009 .write = spufs_wbox_write, 1010 .poll = spufs_wbox_poll, 1011 .fasync = spufs_wbox_fasync, 1012 }; 1013 1014 static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf, 1015 size_t len, loff_t *pos) 1016 { 1017 struct spu_context *ctx = file->private_data; 1018 ssize_t ret; 1019 u32 wbox_stat; 1020 1021 if (len < 4) 1022 return -EINVAL; 1023 1024 ret = spu_acquire(ctx); 1025 if (ret) 1026 return ret; 1027 wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff; 1028 spu_release(ctx); 1029 1030 if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat)) 1031 return -EFAULT; 1032 1033 return 4; 1034 } 1035 1036 static const struct file_operations spufs_wbox_stat_fops = { 1037 .open = spufs_pipe_open, 1038 .read = spufs_wbox_stat_read, 1039 }; 1040 1041 static int spufs_signal1_open(struct inode *inode, struct file *file) 1042 { 1043 struct spufs_inode_info *i = SPUFS_I(inode); 1044 struct spu_context *ctx = i->i_ctx; 1045 1046 mutex_lock(&ctx->mapping_lock); 1047 file->private_data = ctx; 1048 if (!i->i_openers++) 1049 ctx->signal1 = inode->i_mapping; 1050 mutex_unlock(&ctx->mapping_lock); 1051 return nonseekable_open(inode, file); 1052 } 1053 1054 static int 1055 spufs_signal1_release(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 mutex_lock(&ctx->mapping_lock); 1061 if (!--i->i_openers) 1062 ctx->signal1 = NULL; 1063 mutex_unlock(&ctx->mapping_lock); 1064 return 0; 1065 } 1066 1067 static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf, 1068 size_t len, loff_t *pos) 1069 { 1070 int ret = 0; 1071 u32 data; 1072 1073 if (len < 4) 1074 return -EINVAL; 1075 1076 if (ctx->csa.spu_chnlcnt_RW[3]) { 1077 data = ctx->csa.spu_chnldata_RW[3]; 1078 ret = 4; 1079 } 1080 1081 if (!ret) 1082 goto out; 1083 1084 if (copy_to_user(buf, &data, 4)) 1085 return -EFAULT; 1086 1087 out: 1088 return ret; 1089 } 1090 1091 static ssize_t spufs_signal1_read(struct file *file, char __user *buf, 1092 size_t len, loff_t *pos) 1093 { 1094 int ret; 1095 struct spu_context *ctx = file->private_data; 1096 1097 ret = spu_acquire_saved(ctx); 1098 if (ret) 1099 return ret; 1100 ret = __spufs_signal1_read(ctx, buf, len, pos); 1101 spu_release_saved(ctx); 1102 1103 return ret; 1104 } 1105 1106 static ssize_t spufs_signal1_write(struct file *file, const char __user *buf, 1107 size_t len, loff_t *pos) 1108 { 1109 struct spu_context *ctx; 1110 ssize_t ret; 1111 u32 data; 1112 1113 ctx = file->private_data; 1114 1115 if (len < 4) 1116 return -EINVAL; 1117 1118 if (copy_from_user(&data, buf, 4)) 1119 return -EFAULT; 1120 1121 ret = spu_acquire(ctx); 1122 if (ret) 1123 return ret; 1124 ctx->ops->signal1_write(ctx, data); 1125 spu_release(ctx); 1126 1127 return 4; 1128 } 1129 1130 static int 1131 spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1132 { 1133 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000 1134 return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE); 1135 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000 1136 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole 1137 * signal 1 and 2 area 1138 */ 1139 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE); 1140 #else 1141 #error unsupported page size 1142 #endif 1143 } 1144 1145 static struct vm_operations_struct spufs_signal1_mmap_vmops = { 1146 .fault = spufs_signal1_mmap_fault, 1147 }; 1148 1149 static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma) 1150 { 1151 if (!(vma->vm_flags & VM_SHARED)) 1152 return -EINVAL; 1153 1154 vma->vm_flags |= VM_IO | VM_PFNMAP; 1155 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 1156 | _PAGE_NO_CACHE | _PAGE_GUARDED); 1157 1158 vma->vm_ops = &spufs_signal1_mmap_vmops; 1159 return 0; 1160 } 1161 1162 static const struct file_operations spufs_signal1_fops = { 1163 .open = spufs_signal1_open, 1164 .release = spufs_signal1_release, 1165 .read = spufs_signal1_read, 1166 .write = spufs_signal1_write, 1167 .mmap = spufs_signal1_mmap, 1168 }; 1169 1170 static const struct file_operations spufs_signal1_nosched_fops = { 1171 .open = spufs_signal1_open, 1172 .release = spufs_signal1_release, 1173 .write = spufs_signal1_write, 1174 .mmap = spufs_signal1_mmap, 1175 }; 1176 1177 static int spufs_signal2_open(struct inode *inode, struct file *file) 1178 { 1179 struct spufs_inode_info *i = SPUFS_I(inode); 1180 struct spu_context *ctx = i->i_ctx; 1181 1182 mutex_lock(&ctx->mapping_lock); 1183 file->private_data = ctx; 1184 if (!i->i_openers++) 1185 ctx->signal2 = inode->i_mapping; 1186 mutex_unlock(&ctx->mapping_lock); 1187 return nonseekable_open(inode, file); 1188 } 1189 1190 static int 1191 spufs_signal2_release(struct inode *inode, struct file *file) 1192 { 1193 struct spufs_inode_info *i = SPUFS_I(inode); 1194 struct spu_context *ctx = i->i_ctx; 1195 1196 mutex_lock(&ctx->mapping_lock); 1197 if (!--i->i_openers) 1198 ctx->signal2 = NULL; 1199 mutex_unlock(&ctx->mapping_lock); 1200 return 0; 1201 } 1202 1203 static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf, 1204 size_t len, loff_t *pos) 1205 { 1206 int ret = 0; 1207 u32 data; 1208 1209 if (len < 4) 1210 return -EINVAL; 1211 1212 if (ctx->csa.spu_chnlcnt_RW[4]) { 1213 data = ctx->csa.spu_chnldata_RW[4]; 1214 ret = 4; 1215 } 1216 1217 if (!ret) 1218 goto out; 1219 1220 if (copy_to_user(buf, &data, 4)) 1221 return -EFAULT; 1222 1223 out: 1224 return ret; 1225 } 1226 1227 static ssize_t spufs_signal2_read(struct file *file, char __user *buf, 1228 size_t len, loff_t *pos) 1229 { 1230 struct spu_context *ctx = file->private_data; 1231 int ret; 1232 1233 ret = spu_acquire_saved(ctx); 1234 if (ret) 1235 return ret; 1236 ret = __spufs_signal2_read(ctx, buf, len, pos); 1237 spu_release_saved(ctx); 1238 1239 return ret; 1240 } 1241 1242 static ssize_t spufs_signal2_write(struct file *file, const char __user *buf, 1243 size_t len, loff_t *pos) 1244 { 1245 struct spu_context *ctx; 1246 ssize_t ret; 1247 u32 data; 1248 1249 ctx = file->private_data; 1250 1251 if (len < 4) 1252 return -EINVAL; 1253 1254 if (copy_from_user(&data, buf, 4)) 1255 return -EFAULT; 1256 1257 ret = spu_acquire(ctx); 1258 if (ret) 1259 return ret; 1260 ctx->ops->signal2_write(ctx, data); 1261 spu_release(ctx); 1262 1263 return 4; 1264 } 1265 1266 #if SPUFS_MMAP_4K 1267 static int 1268 spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1269 { 1270 #if SPUFS_SIGNAL_MAP_SIZE == 0x1000 1271 return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE); 1272 #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000 1273 /* For 64k pages, both signal1 and signal2 can be used to mmap the whole 1274 * signal 1 and 2 area 1275 */ 1276 return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE); 1277 #else 1278 #error unsupported page size 1279 #endif 1280 } 1281 1282 static struct vm_operations_struct spufs_signal2_mmap_vmops = { 1283 .fault = spufs_signal2_mmap_fault, 1284 }; 1285 1286 static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma) 1287 { 1288 if (!(vma->vm_flags & VM_SHARED)) 1289 return -EINVAL; 1290 1291 vma->vm_flags |= VM_IO | VM_PFNMAP; 1292 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 1293 | _PAGE_NO_CACHE | _PAGE_GUARDED); 1294 1295 vma->vm_ops = &spufs_signal2_mmap_vmops; 1296 return 0; 1297 } 1298 #else /* SPUFS_MMAP_4K */ 1299 #define spufs_signal2_mmap NULL 1300 #endif /* !SPUFS_MMAP_4K */ 1301 1302 static const struct file_operations spufs_signal2_fops = { 1303 .open = spufs_signal2_open, 1304 .release = spufs_signal2_release, 1305 .read = spufs_signal2_read, 1306 .write = spufs_signal2_write, 1307 .mmap = spufs_signal2_mmap, 1308 }; 1309 1310 static const struct file_operations spufs_signal2_nosched_fops = { 1311 .open = spufs_signal2_open, 1312 .release = spufs_signal2_release, 1313 .write = spufs_signal2_write, 1314 .mmap = spufs_signal2_mmap, 1315 }; 1316 1317 /* 1318 * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the 1319 * work of acquiring (or not) the SPU context before calling through 1320 * to the actual get routine. The set routine is called directly. 1321 */ 1322 #define SPU_ATTR_NOACQUIRE 0 1323 #define SPU_ATTR_ACQUIRE 1 1324 #define SPU_ATTR_ACQUIRE_SAVED 2 1325 1326 #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \ 1327 static int __##__get(void *data, u64 *val) \ 1328 { \ 1329 struct spu_context *ctx = data; \ 1330 int ret = 0; \ 1331 \ 1332 if (__acquire == SPU_ATTR_ACQUIRE) { \ 1333 ret = spu_acquire(ctx); \ 1334 if (ret) \ 1335 return ret; \ 1336 *val = __get(ctx); \ 1337 spu_release(ctx); \ 1338 } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \ 1339 ret = spu_acquire_saved(ctx); \ 1340 if (ret) \ 1341 return ret; \ 1342 *val = __get(ctx); \ 1343 spu_release_saved(ctx); \ 1344 } else \ 1345 *val = __get(ctx); \ 1346 \ 1347 return 0; \ 1348 } \ 1349 DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt); 1350 1351 static int spufs_signal1_type_set(void *data, u64 val) 1352 { 1353 struct spu_context *ctx = data; 1354 int ret; 1355 1356 ret = spu_acquire(ctx); 1357 if (ret) 1358 return ret; 1359 ctx->ops->signal1_type_set(ctx, val); 1360 spu_release(ctx); 1361 1362 return 0; 1363 } 1364 1365 static u64 spufs_signal1_type_get(struct spu_context *ctx) 1366 { 1367 return ctx->ops->signal1_type_get(ctx); 1368 } 1369 DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get, 1370 spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE); 1371 1372 1373 static int spufs_signal2_type_set(void *data, u64 val) 1374 { 1375 struct spu_context *ctx = data; 1376 int ret; 1377 1378 ret = spu_acquire(ctx); 1379 if (ret) 1380 return ret; 1381 ctx->ops->signal2_type_set(ctx, val); 1382 spu_release(ctx); 1383 1384 return 0; 1385 } 1386 1387 static u64 spufs_signal2_type_get(struct spu_context *ctx) 1388 { 1389 return ctx->ops->signal2_type_get(ctx); 1390 } 1391 DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get, 1392 spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE); 1393 1394 #if SPUFS_MMAP_4K 1395 static int 1396 spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1397 { 1398 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE); 1399 } 1400 1401 static struct vm_operations_struct spufs_mss_mmap_vmops = { 1402 .fault = spufs_mss_mmap_fault, 1403 }; 1404 1405 /* 1406 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff]. 1407 */ 1408 static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma) 1409 { 1410 if (!(vma->vm_flags & VM_SHARED)) 1411 return -EINVAL; 1412 1413 vma->vm_flags |= VM_IO | VM_PFNMAP; 1414 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 1415 | _PAGE_NO_CACHE | _PAGE_GUARDED); 1416 1417 vma->vm_ops = &spufs_mss_mmap_vmops; 1418 return 0; 1419 } 1420 #else /* SPUFS_MMAP_4K */ 1421 #define spufs_mss_mmap NULL 1422 #endif /* !SPUFS_MMAP_4K */ 1423 1424 static int spufs_mss_open(struct inode *inode, struct file *file) 1425 { 1426 struct spufs_inode_info *i = SPUFS_I(inode); 1427 struct spu_context *ctx = i->i_ctx; 1428 1429 file->private_data = i->i_ctx; 1430 1431 mutex_lock(&ctx->mapping_lock); 1432 if (!i->i_openers++) 1433 ctx->mss = inode->i_mapping; 1434 mutex_unlock(&ctx->mapping_lock); 1435 return nonseekable_open(inode, file); 1436 } 1437 1438 static int 1439 spufs_mss_release(struct inode *inode, struct file *file) 1440 { 1441 struct spufs_inode_info *i = SPUFS_I(inode); 1442 struct spu_context *ctx = i->i_ctx; 1443 1444 mutex_lock(&ctx->mapping_lock); 1445 if (!--i->i_openers) 1446 ctx->mss = NULL; 1447 mutex_unlock(&ctx->mapping_lock); 1448 return 0; 1449 } 1450 1451 static const struct file_operations spufs_mss_fops = { 1452 .open = spufs_mss_open, 1453 .release = spufs_mss_release, 1454 .mmap = spufs_mss_mmap, 1455 }; 1456 1457 static int 1458 spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1459 { 1460 return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE); 1461 } 1462 1463 static struct vm_operations_struct spufs_psmap_mmap_vmops = { 1464 .fault = spufs_psmap_mmap_fault, 1465 }; 1466 1467 /* 1468 * mmap support for full problem state area [0x00000 - 0x1ffff]. 1469 */ 1470 static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma) 1471 { 1472 if (!(vma->vm_flags & VM_SHARED)) 1473 return -EINVAL; 1474 1475 vma->vm_flags |= VM_IO | VM_PFNMAP; 1476 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 1477 | _PAGE_NO_CACHE | _PAGE_GUARDED); 1478 1479 vma->vm_ops = &spufs_psmap_mmap_vmops; 1480 return 0; 1481 } 1482 1483 static int spufs_psmap_open(struct inode *inode, struct file *file) 1484 { 1485 struct spufs_inode_info *i = SPUFS_I(inode); 1486 struct spu_context *ctx = i->i_ctx; 1487 1488 mutex_lock(&ctx->mapping_lock); 1489 file->private_data = i->i_ctx; 1490 if (!i->i_openers++) 1491 ctx->psmap = inode->i_mapping; 1492 mutex_unlock(&ctx->mapping_lock); 1493 return nonseekable_open(inode, file); 1494 } 1495 1496 static int 1497 spufs_psmap_release(struct inode *inode, struct file *file) 1498 { 1499 struct spufs_inode_info *i = SPUFS_I(inode); 1500 struct spu_context *ctx = i->i_ctx; 1501 1502 mutex_lock(&ctx->mapping_lock); 1503 if (!--i->i_openers) 1504 ctx->psmap = NULL; 1505 mutex_unlock(&ctx->mapping_lock); 1506 return 0; 1507 } 1508 1509 static const struct file_operations spufs_psmap_fops = { 1510 .open = spufs_psmap_open, 1511 .release = spufs_psmap_release, 1512 .mmap = spufs_psmap_mmap, 1513 }; 1514 1515 1516 #if SPUFS_MMAP_4K 1517 static int 1518 spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1519 { 1520 return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE); 1521 } 1522 1523 static struct vm_operations_struct spufs_mfc_mmap_vmops = { 1524 .fault = spufs_mfc_mmap_fault, 1525 }; 1526 1527 /* 1528 * mmap support for problem state MFC DMA area [0x0000 - 0x0fff]. 1529 */ 1530 static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma) 1531 { 1532 if (!(vma->vm_flags & VM_SHARED)) 1533 return -EINVAL; 1534 1535 vma->vm_flags |= VM_IO | VM_PFNMAP; 1536 vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 1537 | _PAGE_NO_CACHE | _PAGE_GUARDED); 1538 1539 vma->vm_ops = &spufs_mfc_mmap_vmops; 1540 return 0; 1541 } 1542 #else /* SPUFS_MMAP_4K */ 1543 #define spufs_mfc_mmap NULL 1544 #endif /* !SPUFS_MMAP_4K */ 1545 1546 static int spufs_mfc_open(struct inode *inode, struct file *file) 1547 { 1548 struct spufs_inode_info *i = SPUFS_I(inode); 1549 struct spu_context *ctx = i->i_ctx; 1550 1551 /* we don't want to deal with DMA into other processes */ 1552 if (ctx->owner != current->mm) 1553 return -EINVAL; 1554 1555 if (atomic_read(&inode->i_count) != 1) 1556 return -EBUSY; 1557 1558 mutex_lock(&ctx->mapping_lock); 1559 file->private_data = ctx; 1560 if (!i->i_openers++) 1561 ctx->mfc = inode->i_mapping; 1562 mutex_unlock(&ctx->mapping_lock); 1563 return nonseekable_open(inode, file); 1564 } 1565 1566 static int 1567 spufs_mfc_release(struct inode *inode, struct file *file) 1568 { 1569 struct spufs_inode_info *i = SPUFS_I(inode); 1570 struct spu_context *ctx = i->i_ctx; 1571 1572 mutex_lock(&ctx->mapping_lock); 1573 if (!--i->i_openers) 1574 ctx->mfc = NULL; 1575 mutex_unlock(&ctx->mapping_lock); 1576 return 0; 1577 } 1578 1579 /* interrupt-level mfc callback function. */ 1580 void spufs_mfc_callback(struct spu *spu) 1581 { 1582 struct spu_context *ctx = spu->ctx; 1583 1584 if (!ctx) 1585 return; 1586 1587 wake_up_all(&ctx->mfc_wq); 1588 1589 pr_debug("%s %s\n", __func__, spu->name); 1590 if (ctx->mfc_fasync) { 1591 u32 free_elements, tagstatus; 1592 unsigned int mask; 1593 1594 /* no need for spu_acquire in interrupt context */ 1595 free_elements = ctx->ops->get_mfc_free_elements(ctx); 1596 tagstatus = ctx->ops->read_mfc_tagstatus(ctx); 1597 1598 mask = 0; 1599 if (free_elements & 0xffff) 1600 mask |= POLLOUT; 1601 if (tagstatus & ctx->tagwait) 1602 mask |= POLLIN; 1603 1604 kill_fasync(&ctx->mfc_fasync, SIGIO, mask); 1605 } 1606 } 1607 1608 static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status) 1609 { 1610 /* See if there is one tag group is complete */ 1611 /* FIXME we need locking around tagwait */ 1612 *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait; 1613 ctx->tagwait &= ~*status; 1614 if (*status) 1615 return 1; 1616 1617 /* enable interrupt waiting for any tag group, 1618 may silently fail if interrupts are already enabled */ 1619 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1); 1620 return 0; 1621 } 1622 1623 static ssize_t spufs_mfc_read(struct file *file, char __user *buffer, 1624 size_t size, loff_t *pos) 1625 { 1626 struct spu_context *ctx = file->private_data; 1627 int ret = -EINVAL; 1628 u32 status; 1629 1630 if (size != 4) 1631 goto out; 1632 1633 ret = spu_acquire(ctx); 1634 if (ret) 1635 return ret; 1636 1637 ret = -EINVAL; 1638 if (file->f_flags & O_NONBLOCK) { 1639 status = ctx->ops->read_mfc_tagstatus(ctx); 1640 if (!(status & ctx->tagwait)) 1641 ret = -EAGAIN; 1642 else 1643 /* XXX(hch): shouldn't we clear ret here? */ 1644 ctx->tagwait &= ~status; 1645 } else { 1646 ret = spufs_wait(ctx->mfc_wq, 1647 spufs_read_mfc_tagstatus(ctx, &status)); 1648 if (ret) 1649 goto out; 1650 } 1651 spu_release(ctx); 1652 1653 ret = 4; 1654 if (copy_to_user(buffer, &status, 4)) 1655 ret = -EFAULT; 1656 1657 out: 1658 return ret; 1659 } 1660 1661 static int spufs_check_valid_dma(struct mfc_dma_command *cmd) 1662 { 1663 pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa, 1664 cmd->ea, cmd->size, cmd->tag, cmd->cmd); 1665 1666 switch (cmd->cmd) { 1667 case MFC_PUT_CMD: 1668 case MFC_PUTF_CMD: 1669 case MFC_PUTB_CMD: 1670 case MFC_GET_CMD: 1671 case MFC_GETF_CMD: 1672 case MFC_GETB_CMD: 1673 break; 1674 default: 1675 pr_debug("invalid DMA opcode %x\n", cmd->cmd); 1676 return -EIO; 1677 } 1678 1679 if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) { 1680 pr_debug("invalid DMA alignment, ea %lx lsa %x\n", 1681 cmd->ea, cmd->lsa); 1682 return -EIO; 1683 } 1684 1685 switch (cmd->size & 0xf) { 1686 case 1: 1687 break; 1688 case 2: 1689 if (cmd->lsa & 1) 1690 goto error; 1691 break; 1692 case 4: 1693 if (cmd->lsa & 3) 1694 goto error; 1695 break; 1696 case 8: 1697 if (cmd->lsa & 7) 1698 goto error; 1699 break; 1700 case 0: 1701 if (cmd->lsa & 15) 1702 goto error; 1703 break; 1704 error: 1705 default: 1706 pr_debug("invalid DMA alignment %x for size %x\n", 1707 cmd->lsa & 0xf, cmd->size); 1708 return -EIO; 1709 } 1710 1711 if (cmd->size > 16 * 1024) { 1712 pr_debug("invalid DMA size %x\n", cmd->size); 1713 return -EIO; 1714 } 1715 1716 if (cmd->tag & 0xfff0) { 1717 /* we reserve the higher tag numbers for kernel use */ 1718 pr_debug("invalid DMA tag\n"); 1719 return -EIO; 1720 } 1721 1722 if (cmd->class) { 1723 /* not supported in this version */ 1724 pr_debug("invalid DMA class\n"); 1725 return -EIO; 1726 } 1727 1728 return 0; 1729 } 1730 1731 static int spu_send_mfc_command(struct spu_context *ctx, 1732 struct mfc_dma_command cmd, 1733 int *error) 1734 { 1735 *error = ctx->ops->send_mfc_command(ctx, &cmd); 1736 if (*error == -EAGAIN) { 1737 /* wait for any tag group to complete 1738 so we have space for the new command */ 1739 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1); 1740 /* try again, because the queue might be 1741 empty again */ 1742 *error = ctx->ops->send_mfc_command(ctx, &cmd); 1743 if (*error == -EAGAIN) 1744 return 0; 1745 } 1746 return 1; 1747 } 1748 1749 static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer, 1750 size_t size, loff_t *pos) 1751 { 1752 struct spu_context *ctx = file->private_data; 1753 struct mfc_dma_command cmd; 1754 int ret = -EINVAL; 1755 1756 if (size != sizeof cmd) 1757 goto out; 1758 1759 ret = -EFAULT; 1760 if (copy_from_user(&cmd, buffer, sizeof cmd)) 1761 goto out; 1762 1763 ret = spufs_check_valid_dma(&cmd); 1764 if (ret) 1765 goto out; 1766 1767 ret = spu_acquire(ctx); 1768 if (ret) 1769 goto out; 1770 1771 ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE); 1772 if (ret) 1773 goto out; 1774 1775 if (file->f_flags & O_NONBLOCK) { 1776 ret = ctx->ops->send_mfc_command(ctx, &cmd); 1777 } else { 1778 int status; 1779 ret = spufs_wait(ctx->mfc_wq, 1780 spu_send_mfc_command(ctx, cmd, &status)); 1781 if (ret) 1782 goto out; 1783 if (status) 1784 ret = status; 1785 } 1786 1787 if (ret) 1788 goto out_unlock; 1789 1790 ctx->tagwait |= 1 << cmd.tag; 1791 ret = size; 1792 1793 out_unlock: 1794 spu_release(ctx); 1795 out: 1796 return ret; 1797 } 1798 1799 static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait) 1800 { 1801 struct spu_context *ctx = file->private_data; 1802 u32 free_elements, tagstatus; 1803 unsigned int mask; 1804 1805 poll_wait(file, &ctx->mfc_wq, wait); 1806 1807 /* 1808 * For now keep this uninterruptible and also ignore the rule 1809 * that poll should not sleep. Will be fixed later. 1810 */ 1811 mutex_lock(&ctx->state_mutex); 1812 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2); 1813 free_elements = ctx->ops->get_mfc_free_elements(ctx); 1814 tagstatus = ctx->ops->read_mfc_tagstatus(ctx); 1815 spu_release(ctx); 1816 1817 mask = 0; 1818 if (free_elements & 0xffff) 1819 mask |= POLLOUT | POLLWRNORM; 1820 if (tagstatus & ctx->tagwait) 1821 mask |= POLLIN | POLLRDNORM; 1822 1823 pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__, 1824 free_elements, tagstatus, ctx->tagwait); 1825 1826 return mask; 1827 } 1828 1829 static int spufs_mfc_flush(struct file *file, fl_owner_t id) 1830 { 1831 struct spu_context *ctx = file->private_data; 1832 int ret; 1833 1834 ret = spu_acquire(ctx); 1835 if (ret) 1836 goto out; 1837 #if 0 1838 /* this currently hangs */ 1839 ret = spufs_wait(ctx->mfc_wq, 1840 ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2)); 1841 if (ret) 1842 goto out; 1843 ret = spufs_wait(ctx->mfc_wq, 1844 ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait); 1845 if (ret) 1846 goto out; 1847 #else 1848 ret = 0; 1849 #endif 1850 spu_release(ctx); 1851 out: 1852 return ret; 1853 } 1854 1855 static int spufs_mfc_fsync(struct file *file, struct dentry *dentry, 1856 int datasync) 1857 { 1858 return spufs_mfc_flush(file, NULL); 1859 } 1860 1861 static int spufs_mfc_fasync(int fd, struct file *file, int on) 1862 { 1863 struct spu_context *ctx = file->private_data; 1864 1865 return fasync_helper(fd, file, on, &ctx->mfc_fasync); 1866 } 1867 1868 static const struct file_operations spufs_mfc_fops = { 1869 .open = spufs_mfc_open, 1870 .release = spufs_mfc_release, 1871 .read = spufs_mfc_read, 1872 .write = spufs_mfc_write, 1873 .poll = spufs_mfc_poll, 1874 .flush = spufs_mfc_flush, 1875 .fsync = spufs_mfc_fsync, 1876 .fasync = spufs_mfc_fasync, 1877 .mmap = spufs_mfc_mmap, 1878 }; 1879 1880 static int spufs_npc_set(void *data, u64 val) 1881 { 1882 struct spu_context *ctx = data; 1883 int ret; 1884 1885 ret = spu_acquire(ctx); 1886 if (ret) 1887 return ret; 1888 ctx->ops->npc_write(ctx, val); 1889 spu_release(ctx); 1890 1891 return 0; 1892 } 1893 1894 static u64 spufs_npc_get(struct spu_context *ctx) 1895 { 1896 return ctx->ops->npc_read(ctx); 1897 } 1898 DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, 1899 "0x%llx\n", SPU_ATTR_ACQUIRE); 1900 1901 static int spufs_decr_set(void *data, u64 val) 1902 { 1903 struct spu_context *ctx = data; 1904 struct spu_lscsa *lscsa = ctx->csa.lscsa; 1905 int ret; 1906 1907 ret = spu_acquire_saved(ctx); 1908 if (ret) 1909 return ret; 1910 lscsa->decr.slot[0] = (u32) val; 1911 spu_release_saved(ctx); 1912 1913 return 0; 1914 } 1915 1916 static u64 spufs_decr_get(struct spu_context *ctx) 1917 { 1918 struct spu_lscsa *lscsa = ctx->csa.lscsa; 1919 return lscsa->decr.slot[0]; 1920 } 1921 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set, 1922 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED); 1923 1924 static int spufs_decr_status_set(void *data, u64 val) 1925 { 1926 struct spu_context *ctx = data; 1927 int ret; 1928 1929 ret = spu_acquire_saved(ctx); 1930 if (ret) 1931 return ret; 1932 if (val) 1933 ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING; 1934 else 1935 ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING; 1936 spu_release_saved(ctx); 1937 1938 return 0; 1939 } 1940 1941 static u64 spufs_decr_status_get(struct spu_context *ctx) 1942 { 1943 if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING) 1944 return SPU_DECR_STATUS_RUNNING; 1945 else 1946 return 0; 1947 } 1948 DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get, 1949 spufs_decr_status_set, "0x%llx\n", 1950 SPU_ATTR_ACQUIRE_SAVED); 1951 1952 static int spufs_event_mask_set(void *data, u64 val) 1953 { 1954 struct spu_context *ctx = data; 1955 struct spu_lscsa *lscsa = ctx->csa.lscsa; 1956 int ret; 1957 1958 ret = spu_acquire_saved(ctx); 1959 if (ret) 1960 return ret; 1961 lscsa->event_mask.slot[0] = (u32) val; 1962 spu_release_saved(ctx); 1963 1964 return 0; 1965 } 1966 1967 static u64 spufs_event_mask_get(struct spu_context *ctx) 1968 { 1969 struct spu_lscsa *lscsa = ctx->csa.lscsa; 1970 return lscsa->event_mask.slot[0]; 1971 } 1972 1973 DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get, 1974 spufs_event_mask_set, "0x%llx\n", 1975 SPU_ATTR_ACQUIRE_SAVED); 1976 1977 static u64 spufs_event_status_get(struct spu_context *ctx) 1978 { 1979 struct spu_state *state = &ctx->csa; 1980 u64 stat; 1981 stat = state->spu_chnlcnt_RW[0]; 1982 if (stat) 1983 return state->spu_chnldata_RW[0]; 1984 return 0; 1985 } 1986 DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get, 1987 NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED) 1988 1989 static int spufs_srr0_set(void *data, u64 val) 1990 { 1991 struct spu_context *ctx = data; 1992 struct spu_lscsa *lscsa = ctx->csa.lscsa; 1993 int ret; 1994 1995 ret = spu_acquire_saved(ctx); 1996 if (ret) 1997 return ret; 1998 lscsa->srr0.slot[0] = (u32) val; 1999 spu_release_saved(ctx); 2000 2001 return 0; 2002 } 2003 2004 static u64 spufs_srr0_get(struct spu_context *ctx) 2005 { 2006 struct spu_lscsa *lscsa = ctx->csa.lscsa; 2007 return lscsa->srr0.slot[0]; 2008 } 2009 DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set, 2010 "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED) 2011 2012 static u64 spufs_id_get(struct spu_context *ctx) 2013 { 2014 u64 num; 2015 2016 if (ctx->state == SPU_STATE_RUNNABLE) 2017 num = ctx->spu->number; 2018 else 2019 num = (unsigned int)-1; 2020 2021 return num; 2022 } 2023 DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n", 2024 SPU_ATTR_ACQUIRE) 2025 2026 static u64 spufs_object_id_get(struct spu_context *ctx) 2027 { 2028 /* FIXME: Should there really be no locking here? */ 2029 return ctx->object_id; 2030 } 2031 2032 static int spufs_object_id_set(void *data, u64 id) 2033 { 2034 struct spu_context *ctx = data; 2035 ctx->object_id = id; 2036 2037 return 0; 2038 } 2039 2040 DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get, 2041 spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE); 2042 2043 static u64 spufs_lslr_get(struct spu_context *ctx) 2044 { 2045 return ctx->csa.priv2.spu_lslr_RW; 2046 } 2047 DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n", 2048 SPU_ATTR_ACQUIRE_SAVED); 2049 2050 static int spufs_info_open(struct inode *inode, struct file *file) 2051 { 2052 struct spufs_inode_info *i = SPUFS_I(inode); 2053 struct spu_context *ctx = i->i_ctx; 2054 file->private_data = ctx; 2055 return 0; 2056 } 2057 2058 static int spufs_caps_show(struct seq_file *s, void *private) 2059 { 2060 struct spu_context *ctx = s->private; 2061 2062 if (!(ctx->flags & SPU_CREATE_NOSCHED)) 2063 seq_puts(s, "sched\n"); 2064 if (!(ctx->flags & SPU_CREATE_ISOLATE)) 2065 seq_puts(s, "step\n"); 2066 return 0; 2067 } 2068 2069 static int spufs_caps_open(struct inode *inode, struct file *file) 2070 { 2071 return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx); 2072 } 2073 2074 static const struct file_operations spufs_caps_fops = { 2075 .open = spufs_caps_open, 2076 .read = seq_read, 2077 .llseek = seq_lseek, 2078 .release = single_release, 2079 }; 2080 2081 static ssize_t __spufs_mbox_info_read(struct spu_context *ctx, 2082 char __user *buf, size_t len, loff_t *pos) 2083 { 2084 u32 data; 2085 2086 /* EOF if there's no entry in the mbox */ 2087 if (!(ctx->csa.prob.mb_stat_R & 0x0000ff)) 2088 return 0; 2089 2090 data = ctx->csa.prob.pu_mb_R; 2091 2092 return simple_read_from_buffer(buf, len, pos, &data, sizeof data); 2093 } 2094 2095 static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf, 2096 size_t len, loff_t *pos) 2097 { 2098 int ret; 2099 struct spu_context *ctx = file->private_data; 2100 2101 if (!access_ok(VERIFY_WRITE, buf, len)) 2102 return -EFAULT; 2103 2104 ret = spu_acquire_saved(ctx); 2105 if (ret) 2106 return ret; 2107 spin_lock(&ctx->csa.register_lock); 2108 ret = __spufs_mbox_info_read(ctx, buf, len, pos); 2109 spin_unlock(&ctx->csa.register_lock); 2110 spu_release_saved(ctx); 2111 2112 return ret; 2113 } 2114 2115 static const struct file_operations spufs_mbox_info_fops = { 2116 .open = spufs_info_open, 2117 .read = spufs_mbox_info_read, 2118 .llseek = generic_file_llseek, 2119 }; 2120 2121 static ssize_t __spufs_ibox_info_read(struct spu_context *ctx, 2122 char __user *buf, size_t len, loff_t *pos) 2123 { 2124 u32 data; 2125 2126 /* EOF if there's no entry in the ibox */ 2127 if (!(ctx->csa.prob.mb_stat_R & 0xff0000)) 2128 return 0; 2129 2130 data = ctx->csa.priv2.puint_mb_R; 2131 2132 return simple_read_from_buffer(buf, len, pos, &data, sizeof data); 2133 } 2134 2135 static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf, 2136 size_t len, loff_t *pos) 2137 { 2138 struct spu_context *ctx = file->private_data; 2139 int ret; 2140 2141 if (!access_ok(VERIFY_WRITE, buf, len)) 2142 return -EFAULT; 2143 2144 ret = spu_acquire_saved(ctx); 2145 if (ret) 2146 return ret; 2147 spin_lock(&ctx->csa.register_lock); 2148 ret = __spufs_ibox_info_read(ctx, buf, len, pos); 2149 spin_unlock(&ctx->csa.register_lock); 2150 spu_release_saved(ctx); 2151 2152 return ret; 2153 } 2154 2155 static const struct file_operations spufs_ibox_info_fops = { 2156 .open = spufs_info_open, 2157 .read = spufs_ibox_info_read, 2158 .llseek = generic_file_llseek, 2159 }; 2160 2161 static ssize_t __spufs_wbox_info_read(struct spu_context *ctx, 2162 char __user *buf, size_t len, loff_t *pos) 2163 { 2164 int i, cnt; 2165 u32 data[4]; 2166 u32 wbox_stat; 2167 2168 wbox_stat = ctx->csa.prob.mb_stat_R; 2169 cnt = 4 - ((wbox_stat & 0x00ff00) >> 8); 2170 for (i = 0; i < cnt; i++) { 2171 data[i] = ctx->csa.spu_mailbox_data[i]; 2172 } 2173 2174 return simple_read_from_buffer(buf, len, pos, &data, 2175 cnt * sizeof(u32)); 2176 } 2177 2178 static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf, 2179 size_t len, loff_t *pos) 2180 { 2181 struct spu_context *ctx = file->private_data; 2182 int ret; 2183 2184 if (!access_ok(VERIFY_WRITE, buf, len)) 2185 return -EFAULT; 2186 2187 ret = spu_acquire_saved(ctx); 2188 if (ret) 2189 return ret; 2190 spin_lock(&ctx->csa.register_lock); 2191 ret = __spufs_wbox_info_read(ctx, buf, len, pos); 2192 spin_unlock(&ctx->csa.register_lock); 2193 spu_release_saved(ctx); 2194 2195 return ret; 2196 } 2197 2198 static const struct file_operations spufs_wbox_info_fops = { 2199 .open = spufs_info_open, 2200 .read = spufs_wbox_info_read, 2201 .llseek = generic_file_llseek, 2202 }; 2203 2204 static ssize_t __spufs_dma_info_read(struct spu_context *ctx, 2205 char __user *buf, size_t len, loff_t *pos) 2206 { 2207 struct spu_dma_info info; 2208 struct mfc_cq_sr *qp, *spuqp; 2209 int i; 2210 2211 info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW; 2212 info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0]; 2213 info.dma_info_status = ctx->csa.spu_chnldata_RW[24]; 2214 info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25]; 2215 info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27]; 2216 for (i = 0; i < 16; i++) { 2217 qp = &info.dma_info_command_data[i]; 2218 spuqp = &ctx->csa.priv2.spuq[i]; 2219 2220 qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW; 2221 qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW; 2222 qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW; 2223 qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW; 2224 } 2225 2226 return simple_read_from_buffer(buf, len, pos, &info, 2227 sizeof info); 2228 } 2229 2230 static ssize_t spufs_dma_info_read(struct file *file, char __user *buf, 2231 size_t len, loff_t *pos) 2232 { 2233 struct spu_context *ctx = file->private_data; 2234 int ret; 2235 2236 if (!access_ok(VERIFY_WRITE, buf, len)) 2237 return -EFAULT; 2238 2239 ret = spu_acquire_saved(ctx); 2240 if (ret) 2241 return ret; 2242 spin_lock(&ctx->csa.register_lock); 2243 ret = __spufs_dma_info_read(ctx, buf, len, pos); 2244 spin_unlock(&ctx->csa.register_lock); 2245 spu_release_saved(ctx); 2246 2247 return ret; 2248 } 2249 2250 static const struct file_operations spufs_dma_info_fops = { 2251 .open = spufs_info_open, 2252 .read = spufs_dma_info_read, 2253 }; 2254 2255 static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx, 2256 char __user *buf, size_t len, loff_t *pos) 2257 { 2258 struct spu_proxydma_info info; 2259 struct mfc_cq_sr *qp, *puqp; 2260 int ret = sizeof info; 2261 int i; 2262 2263 if (len < ret) 2264 return -EINVAL; 2265 2266 if (!access_ok(VERIFY_WRITE, buf, len)) 2267 return -EFAULT; 2268 2269 info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW; 2270 info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW; 2271 info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R; 2272 for (i = 0; i < 8; i++) { 2273 qp = &info.proxydma_info_command_data[i]; 2274 puqp = &ctx->csa.priv2.puq[i]; 2275 2276 qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW; 2277 qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW; 2278 qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW; 2279 qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW; 2280 } 2281 2282 return simple_read_from_buffer(buf, len, pos, &info, 2283 sizeof info); 2284 } 2285 2286 static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf, 2287 size_t len, loff_t *pos) 2288 { 2289 struct spu_context *ctx = file->private_data; 2290 int ret; 2291 2292 ret = spu_acquire_saved(ctx); 2293 if (ret) 2294 return ret; 2295 spin_lock(&ctx->csa.register_lock); 2296 ret = __spufs_proxydma_info_read(ctx, buf, len, pos); 2297 spin_unlock(&ctx->csa.register_lock); 2298 spu_release_saved(ctx); 2299 2300 return ret; 2301 } 2302 2303 static const struct file_operations spufs_proxydma_info_fops = { 2304 .open = spufs_info_open, 2305 .read = spufs_proxydma_info_read, 2306 }; 2307 2308 static int spufs_show_tid(struct seq_file *s, void *private) 2309 { 2310 struct spu_context *ctx = s->private; 2311 2312 seq_printf(s, "%d\n", ctx->tid); 2313 return 0; 2314 } 2315 2316 static int spufs_tid_open(struct inode *inode, struct file *file) 2317 { 2318 return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx); 2319 } 2320 2321 static const struct file_operations spufs_tid_fops = { 2322 .open = spufs_tid_open, 2323 .read = seq_read, 2324 .llseek = seq_lseek, 2325 .release = single_release, 2326 }; 2327 2328 static const char *ctx_state_names[] = { 2329 "user", "system", "iowait", "loaded" 2330 }; 2331 2332 static unsigned long long spufs_acct_time(struct spu_context *ctx, 2333 enum spu_utilization_state state) 2334 { 2335 struct timespec ts; 2336 unsigned long long time = ctx->stats.times[state]; 2337 2338 /* 2339 * In general, utilization statistics are updated by the controlling 2340 * thread as the spu context moves through various well defined 2341 * state transitions, but if the context is lazily loaded its 2342 * utilization statistics are not updated as the controlling thread 2343 * is not tightly coupled with the execution of the spu context. We 2344 * calculate and apply the time delta from the last recorded state 2345 * of the spu context. 2346 */ 2347 if (ctx->spu && ctx->stats.util_state == state) { 2348 ktime_get_ts(&ts); 2349 time += timespec_to_ns(&ts) - ctx->stats.tstamp; 2350 } 2351 2352 return time / NSEC_PER_MSEC; 2353 } 2354 2355 static unsigned long long spufs_slb_flts(struct spu_context *ctx) 2356 { 2357 unsigned long long slb_flts = ctx->stats.slb_flt; 2358 2359 if (ctx->state == SPU_STATE_RUNNABLE) { 2360 slb_flts += (ctx->spu->stats.slb_flt - 2361 ctx->stats.slb_flt_base); 2362 } 2363 2364 return slb_flts; 2365 } 2366 2367 static unsigned long long spufs_class2_intrs(struct spu_context *ctx) 2368 { 2369 unsigned long long class2_intrs = ctx->stats.class2_intr; 2370 2371 if (ctx->state == SPU_STATE_RUNNABLE) { 2372 class2_intrs += (ctx->spu->stats.class2_intr - 2373 ctx->stats.class2_intr_base); 2374 } 2375 2376 return class2_intrs; 2377 } 2378 2379 2380 static int spufs_show_stat(struct seq_file *s, void *private) 2381 { 2382 struct spu_context *ctx = s->private; 2383 int ret; 2384 2385 ret = spu_acquire(ctx); 2386 if (ret) 2387 return ret; 2388 2389 seq_printf(s, "%s %llu %llu %llu %llu " 2390 "%llu %llu %llu %llu %llu %llu %llu %llu\n", 2391 ctx_state_names[ctx->stats.util_state], 2392 spufs_acct_time(ctx, SPU_UTIL_USER), 2393 spufs_acct_time(ctx, SPU_UTIL_SYSTEM), 2394 spufs_acct_time(ctx, SPU_UTIL_IOWAIT), 2395 spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED), 2396 ctx->stats.vol_ctx_switch, 2397 ctx->stats.invol_ctx_switch, 2398 spufs_slb_flts(ctx), 2399 ctx->stats.hash_flt, 2400 ctx->stats.min_flt, 2401 ctx->stats.maj_flt, 2402 spufs_class2_intrs(ctx), 2403 ctx->stats.libassist); 2404 spu_release(ctx); 2405 return 0; 2406 } 2407 2408 static int spufs_stat_open(struct inode *inode, struct file *file) 2409 { 2410 return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx); 2411 } 2412 2413 static const struct file_operations spufs_stat_fops = { 2414 .open = spufs_stat_open, 2415 .read = seq_read, 2416 .llseek = seq_lseek, 2417 .release = single_release, 2418 }; 2419 2420 static inline int spufs_switch_log_used(struct spu_context *ctx) 2421 { 2422 return (ctx->switch_log->head - ctx->switch_log->tail) % 2423 SWITCH_LOG_BUFSIZE; 2424 } 2425 2426 static inline int spufs_switch_log_avail(struct spu_context *ctx) 2427 { 2428 return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx); 2429 } 2430 2431 static int spufs_switch_log_open(struct inode *inode, struct file *file) 2432 { 2433 struct spu_context *ctx = SPUFS_I(inode)->i_ctx; 2434 int rc; 2435 2436 rc = spu_acquire(ctx); 2437 if (rc) 2438 return rc; 2439 2440 if (ctx->switch_log) { 2441 rc = -EBUSY; 2442 goto out; 2443 } 2444 2445 ctx->switch_log = kmalloc(sizeof(struct switch_log) + 2446 SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry), 2447 GFP_KERNEL); 2448 2449 if (!ctx->switch_log) { 2450 rc = -ENOMEM; 2451 goto out; 2452 } 2453 2454 ctx->switch_log->head = ctx->switch_log->tail = 0; 2455 init_waitqueue_head(&ctx->switch_log->wait); 2456 rc = 0; 2457 2458 out: 2459 spu_release(ctx); 2460 return rc; 2461 } 2462 2463 static int spufs_switch_log_release(struct inode *inode, struct file *file) 2464 { 2465 struct spu_context *ctx = SPUFS_I(inode)->i_ctx; 2466 int rc; 2467 2468 rc = spu_acquire(ctx); 2469 if (rc) 2470 return rc; 2471 2472 kfree(ctx->switch_log); 2473 ctx->switch_log = NULL; 2474 spu_release(ctx); 2475 2476 return 0; 2477 } 2478 2479 static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n) 2480 { 2481 struct switch_log_entry *p; 2482 2483 p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE; 2484 2485 return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n", 2486 (unsigned int) p->tstamp.tv_sec, 2487 (unsigned int) p->tstamp.tv_nsec, 2488 p->spu_id, 2489 (unsigned int) p->type, 2490 (unsigned int) p->val, 2491 (unsigned long long) p->timebase); 2492 } 2493 2494 static ssize_t spufs_switch_log_read(struct file *file, char __user *buf, 2495 size_t len, loff_t *ppos) 2496 { 2497 struct inode *inode = file->f_path.dentry->d_inode; 2498 struct spu_context *ctx = SPUFS_I(inode)->i_ctx; 2499 int error = 0, cnt = 0; 2500 2501 if (!buf || len < 0) 2502 return -EINVAL; 2503 2504 error = spu_acquire(ctx); 2505 if (error) 2506 return error; 2507 2508 while (cnt < len) { 2509 char tbuf[128]; 2510 int width; 2511 2512 if (spufs_switch_log_used(ctx) == 0) { 2513 if (cnt > 0) { 2514 /* If there's data ready to go, we can 2515 * just return straight away */ 2516 break; 2517 2518 } else if (file->f_flags & O_NONBLOCK) { 2519 error = -EAGAIN; 2520 break; 2521 2522 } else { 2523 /* spufs_wait will drop the mutex and 2524 * re-acquire, but since we're in read(), the 2525 * file cannot be _released (and so 2526 * ctx->switch_log is stable). 2527 */ 2528 error = spufs_wait(ctx->switch_log->wait, 2529 spufs_switch_log_used(ctx) > 0); 2530 2531 /* On error, spufs_wait returns without the 2532 * state mutex held */ 2533 if (error) 2534 return error; 2535 2536 /* We may have had entries read from underneath 2537 * us while we dropped the mutex in spufs_wait, 2538 * so re-check */ 2539 if (spufs_switch_log_used(ctx) == 0) 2540 continue; 2541 } 2542 } 2543 2544 width = switch_log_sprint(ctx, tbuf, sizeof(tbuf)); 2545 if (width < len) 2546 ctx->switch_log->tail = 2547 (ctx->switch_log->tail + 1) % 2548 SWITCH_LOG_BUFSIZE; 2549 else 2550 /* If the record is greater than space available return 2551 * partial buffer (so far) */ 2552 break; 2553 2554 error = copy_to_user(buf + cnt, tbuf, width); 2555 if (error) 2556 break; 2557 cnt += width; 2558 } 2559 2560 spu_release(ctx); 2561 2562 return cnt == 0 ? error : cnt; 2563 } 2564 2565 static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait) 2566 { 2567 struct inode *inode = file->f_path.dentry->d_inode; 2568 struct spu_context *ctx = SPUFS_I(inode)->i_ctx; 2569 unsigned int mask = 0; 2570 int rc; 2571 2572 poll_wait(file, &ctx->switch_log->wait, wait); 2573 2574 rc = spu_acquire(ctx); 2575 if (rc) 2576 return rc; 2577 2578 if (spufs_switch_log_used(ctx) > 0) 2579 mask |= POLLIN; 2580 2581 spu_release(ctx); 2582 2583 return mask; 2584 } 2585 2586 static const struct file_operations spufs_switch_log_fops = { 2587 .owner = THIS_MODULE, 2588 .open = spufs_switch_log_open, 2589 .read = spufs_switch_log_read, 2590 .poll = spufs_switch_log_poll, 2591 .release = spufs_switch_log_release, 2592 }; 2593 2594 /** 2595 * Log a context switch event to a switch log reader. 2596 * 2597 * Must be called with ctx->state_mutex held. 2598 */ 2599 void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx, 2600 u32 type, u32 val) 2601 { 2602 if (!ctx->switch_log) 2603 return; 2604 2605 if (spufs_switch_log_avail(ctx) > 1) { 2606 struct switch_log_entry *p; 2607 2608 p = ctx->switch_log->log + ctx->switch_log->head; 2609 ktime_get_ts(&p->tstamp); 2610 p->timebase = get_tb(); 2611 p->spu_id = spu ? spu->number : -1; 2612 p->type = type; 2613 p->val = val; 2614 2615 ctx->switch_log->head = 2616 (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE; 2617 } 2618 2619 wake_up(&ctx->switch_log->wait); 2620 } 2621 2622 static int spufs_show_ctx(struct seq_file *s, void *private) 2623 { 2624 struct spu_context *ctx = s->private; 2625 u64 mfc_control_RW; 2626 2627 mutex_lock(&ctx->state_mutex); 2628 if (ctx->spu) { 2629 struct spu *spu = ctx->spu; 2630 struct spu_priv2 __iomem *priv2 = spu->priv2; 2631 2632 spin_lock_irq(&spu->register_lock); 2633 mfc_control_RW = in_be64(&priv2->mfc_control_RW); 2634 spin_unlock_irq(&spu->register_lock); 2635 } else { 2636 struct spu_state *csa = &ctx->csa; 2637 2638 mfc_control_RW = csa->priv2.mfc_control_RW; 2639 } 2640 2641 seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)" 2642 " %c %lx %lx %lx %lx %x %x\n", 2643 ctx->state == SPU_STATE_SAVED ? 'S' : 'R', 2644 ctx->flags, 2645 ctx->sched_flags, 2646 ctx->prio, 2647 ctx->time_slice, 2648 ctx->spu ? ctx->spu->number : -1, 2649 !list_empty(&ctx->rq) ? 'q' : ' ', 2650 ctx->csa.class_0_pending, 2651 ctx->csa.class_0_dar, 2652 ctx->csa.class_1_dsisr, 2653 mfc_control_RW, 2654 ctx->ops->runcntl_read(ctx), 2655 ctx->ops->status_read(ctx)); 2656 2657 mutex_unlock(&ctx->state_mutex); 2658 2659 return 0; 2660 } 2661 2662 static int spufs_ctx_open(struct inode *inode, struct file *file) 2663 { 2664 return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx); 2665 } 2666 2667 static const struct file_operations spufs_ctx_fops = { 2668 .open = spufs_ctx_open, 2669 .read = seq_read, 2670 .llseek = seq_lseek, 2671 .release = single_release, 2672 }; 2673 2674 struct spufs_tree_descr spufs_dir_contents[] = { 2675 { "capabilities", &spufs_caps_fops, 0444, }, 2676 { "mem", &spufs_mem_fops, 0666, LS_SIZE, }, 2677 { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), }, 2678 { "mbox", &spufs_mbox_fops, 0444, }, 2679 { "ibox", &spufs_ibox_fops, 0444, }, 2680 { "wbox", &spufs_wbox_fops, 0222, }, 2681 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), }, 2682 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), }, 2683 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), }, 2684 { "signal1", &spufs_signal1_fops, 0666, }, 2685 { "signal2", &spufs_signal2_fops, 0666, }, 2686 { "signal1_type", &spufs_signal1_type, 0666, }, 2687 { "signal2_type", &spufs_signal2_type, 0666, }, 2688 { "cntl", &spufs_cntl_fops, 0666, }, 2689 { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), }, 2690 { "lslr", &spufs_lslr_ops, 0444, }, 2691 { "mfc", &spufs_mfc_fops, 0666, }, 2692 { "mss", &spufs_mss_fops, 0666, }, 2693 { "npc", &spufs_npc_ops, 0666, }, 2694 { "srr0", &spufs_srr0_ops, 0666, }, 2695 { "decr", &spufs_decr_ops, 0666, }, 2696 { "decr_status", &spufs_decr_status_ops, 0666, }, 2697 { "event_mask", &spufs_event_mask_ops, 0666, }, 2698 { "event_status", &spufs_event_status_ops, 0444, }, 2699 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, }, 2700 { "phys-id", &spufs_id_ops, 0666, }, 2701 { "object-id", &spufs_object_id_ops, 0666, }, 2702 { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), }, 2703 { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), }, 2704 { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), }, 2705 { "dma_info", &spufs_dma_info_fops, 0444, 2706 sizeof(struct spu_dma_info), }, 2707 { "proxydma_info", &spufs_proxydma_info_fops, 0444, 2708 sizeof(struct spu_proxydma_info)}, 2709 { "tid", &spufs_tid_fops, 0444, }, 2710 { "stat", &spufs_stat_fops, 0444, }, 2711 { "switch_log", &spufs_switch_log_fops, 0444 }, 2712 {}, 2713 }; 2714 2715 struct spufs_tree_descr spufs_dir_nosched_contents[] = { 2716 { "capabilities", &spufs_caps_fops, 0444, }, 2717 { "mem", &spufs_mem_fops, 0666, LS_SIZE, }, 2718 { "mbox", &spufs_mbox_fops, 0444, }, 2719 { "ibox", &spufs_ibox_fops, 0444, }, 2720 { "wbox", &spufs_wbox_fops, 0222, }, 2721 { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), }, 2722 { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), }, 2723 { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), }, 2724 { "signal1", &spufs_signal1_nosched_fops, 0222, }, 2725 { "signal2", &spufs_signal2_nosched_fops, 0222, }, 2726 { "signal1_type", &spufs_signal1_type, 0666, }, 2727 { "signal2_type", &spufs_signal2_type, 0666, }, 2728 { "mss", &spufs_mss_fops, 0666, }, 2729 { "mfc", &spufs_mfc_fops, 0666, }, 2730 { "cntl", &spufs_cntl_fops, 0666, }, 2731 { "npc", &spufs_npc_ops, 0666, }, 2732 { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, }, 2733 { "phys-id", &spufs_id_ops, 0666, }, 2734 { "object-id", &spufs_object_id_ops, 0666, }, 2735 { "tid", &spufs_tid_fops, 0444, }, 2736 { "stat", &spufs_stat_fops, 0444, }, 2737 {}, 2738 }; 2739 2740 struct spufs_tree_descr spufs_dir_debug_contents[] = { 2741 { ".ctx", &spufs_ctx_fops, 0444, }, 2742 {}, 2743 }; 2744 2745 struct spufs_coredump_reader spufs_coredump_read[] = { 2746 { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])}, 2747 { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) }, 2748 { "lslr", NULL, spufs_lslr_get, 19 }, 2749 { "decr", NULL, spufs_decr_get, 19 }, 2750 { "decr_status", NULL, spufs_decr_status_get, 19 }, 2751 { "mem", __spufs_mem_read, NULL, LS_SIZE, }, 2752 { "signal1", __spufs_signal1_read, NULL, sizeof(u32) }, 2753 { "signal1_type", NULL, spufs_signal1_type_get, 19 }, 2754 { "signal2", __spufs_signal2_read, NULL, sizeof(u32) }, 2755 { "signal2_type", NULL, spufs_signal2_type_get, 19 }, 2756 { "event_mask", NULL, spufs_event_mask_get, 19 }, 2757 { "event_status", NULL, spufs_event_status_get, 19 }, 2758 { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) }, 2759 { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) }, 2760 { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)}, 2761 { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)}, 2762 { "proxydma_info", __spufs_proxydma_info_read, 2763 NULL, sizeof(struct spu_proxydma_info)}, 2764 { "object-id", NULL, spufs_object_id_get, 19 }, 2765 { "npc", NULL, spufs_npc_get, 19 }, 2766 { NULL }, 2767 }; 2768