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